1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * linux/drivers/spi/spi-loopback-test.c
4 *
5 * (c) Martin Sperl <kernel@martin.sperl.org>
6 *
7 * Loopback test driver to test several typical spi_message conditions
8 * that a spi_master driver may encounter
9 * this can also get used for regression testing
10 */
11
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/ktime.h>
15 #include <linux/list.h>
16 #include <linux/list_sort.h>
17 #include <linux/module.h>
18 #include <linux/printk.h>
19 #include <linux/vmalloc.h>
20 #include <linux/spi/spi.h>
21
22 #include "spi-test.h"
23
24 /* flag to only simulate transfers */
25 static int simulate_only;
26 module_param(simulate_only, int, 0);
27 MODULE_PARM_DESC(simulate_only, "if not 0 do not execute the spi message");
28
29 /* dump spi messages */
30 static int dump_messages;
31 module_param(dump_messages, int, 0);
32 MODULE_PARM_DESC(dump_messages,
33 "=1 dump the basic spi_message_structure, " \
34 "=2 dump the spi_message_structure including data, " \
35 "=3 dump the spi_message structure before and after execution");
36 /* the device is jumpered for loopback - enabling some rx_buf tests */
37 static int loopback;
38 module_param(loopback, int, 0);
39 MODULE_PARM_DESC(loopback,
40 "if set enable loopback mode, where the rx_buf " \
41 "is checked to match tx_buf after the spi_message " \
42 "is executed");
43
44 static int loop_req;
45 module_param(loop_req, int, 0);
46 MODULE_PARM_DESC(loop_req,
47 "if set controller will be asked to enable test loop mode. " \
48 "If controller supported it, MISO and MOSI will be connected");
49
50 static int no_cs;
51 module_param(no_cs, int, 0);
52 MODULE_PARM_DESC(no_cs,
53 "if set Chip Select (CS) will not be used");
54
55 /* run tests only for a specific length */
56 static int run_only_iter_len = -1;
57 module_param(run_only_iter_len, int, 0);
58 MODULE_PARM_DESC(run_only_iter_len,
59 "only run tests for a length of this number in iterate_len list");
60
61 /* run only a specific test */
62 static int run_only_test = -1;
63 module_param(run_only_test, int, 0);
64 MODULE_PARM_DESC(run_only_test,
65 "only run the test with this number (0-based !)");
66
67 /* use vmalloc'ed buffers */
68 static int use_vmalloc;
69 module_param(use_vmalloc, int, 0644);
70 MODULE_PARM_DESC(use_vmalloc,
71 "use vmalloc'ed buffers instead of kmalloc'ed");
72
73 /* check rx ranges */
74 static int check_ranges = 1;
75 module_param(check_ranges, int, 0644);
76 MODULE_PARM_DESC(check_ranges,
77 "checks rx_buffer pattern are valid");
78
79 static unsigned int delay_ms = 100;
80 module_param(delay_ms, uint, 0644);
81 MODULE_PARM_DESC(delay_ms,
82 "delay between tests, in milliseconds (default: 100)");
83
84 /* the actual tests to execute */
85 static struct spi_test spi_tests[] = {
86 {
87 .description = "tx/rx-transfer - start of page",
88 .fill_option = FILL_COUNT_8,
89 .iterate_len = { ITERATE_MAX_LEN },
90 .iterate_tx_align = ITERATE_ALIGN,
91 .iterate_rx_align = ITERATE_ALIGN,
92 .transfer_count = 1,
93 .transfers = {
94 {
95 .tx_buf = TX(0),
96 .rx_buf = RX(0),
97 },
98 },
99 },
100 {
101 .description = "tx/rx-transfer - crossing PAGE_SIZE",
102 .fill_option = FILL_COUNT_8,
103 .iterate_len = { ITERATE_LEN },
104 .iterate_tx_align = ITERATE_ALIGN,
105 .iterate_rx_align = ITERATE_ALIGN,
106 .transfer_count = 1,
107 .transfers = {
108 {
109 .tx_buf = TX(PAGE_SIZE - 4),
110 .rx_buf = RX(PAGE_SIZE - 4),
111 },
112 },
113 },
114 {
115 .description = "tx-transfer - only",
116 .fill_option = FILL_COUNT_8,
117 .iterate_len = { ITERATE_MAX_LEN },
118 .iterate_tx_align = ITERATE_ALIGN,
119 .transfer_count = 1,
120 .transfers = {
121 {
122 .tx_buf = TX(0),
123 },
124 },
125 },
126 {
127 .description = "rx-transfer - only",
128 .fill_option = FILL_COUNT_8,
129 .iterate_len = { ITERATE_MAX_LEN },
130 .iterate_rx_align = ITERATE_ALIGN,
131 .transfer_count = 1,
132 .transfers = {
133 {
134 .rx_buf = RX(0),
135 },
136 },
137 },
138 {
139 .description = "two tx-transfers - alter both",
140 .fill_option = FILL_COUNT_8,
141 .iterate_len = { ITERATE_LEN },
142 .iterate_tx_align = ITERATE_ALIGN,
143 .iterate_transfer_mask = BIT(0) | BIT(1),
144 .transfer_count = 2,
145 .transfers = {
146 {
147 .tx_buf = TX(0),
148 },
149 {
150 /* this is why we cant use ITERATE_MAX_LEN */
151 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF),
152 },
153 },
154 },
155 {
156 .description = "two tx-transfers - alter first",
157 .fill_option = FILL_COUNT_8,
158 .iterate_len = { ITERATE_MAX_LEN },
159 .iterate_tx_align = ITERATE_ALIGN,
160 .iterate_transfer_mask = BIT(0),
161 .transfer_count = 2,
162 .transfers = {
163 {
164 .tx_buf = TX(64),
165 },
166 {
167 .len = 1,
168 .tx_buf = TX(0),
169 },
170 },
171 },
172 {
173 .description = "two tx-transfers - alter second",
174 .fill_option = FILL_COUNT_8,
175 .iterate_len = { ITERATE_MAX_LEN },
176 .iterate_tx_align = ITERATE_ALIGN,
177 .iterate_transfer_mask = BIT(1),
178 .transfer_count = 2,
179 .transfers = {
180 {
181 .len = 16,
182 .tx_buf = TX(0),
183 },
184 {
185 .tx_buf = TX(64),
186 },
187 },
188 },
189 {
190 .description = "two transfers tx then rx - alter both",
191 .fill_option = FILL_COUNT_8,
192 .iterate_len = { ITERATE_MAX_LEN },
193 .iterate_tx_align = ITERATE_ALIGN,
194 .iterate_transfer_mask = BIT(0) | BIT(1),
195 .transfer_count = 2,
196 .transfers = {
197 {
198 .tx_buf = TX(0),
199 },
200 {
201 .rx_buf = RX(0),
202 },
203 },
204 },
205 {
206 .description = "two transfers tx then rx - alter tx",
207 .fill_option = FILL_COUNT_8,
208 .iterate_len = { ITERATE_MAX_LEN },
209 .iterate_tx_align = ITERATE_ALIGN,
210 .iterate_transfer_mask = BIT(0),
211 .transfer_count = 2,
212 .transfers = {
213 {
214 .tx_buf = TX(0),
215 },
216 {
217 .len = 1,
218 .rx_buf = RX(0),
219 },
220 },
221 },
222 {
223 .description = "two transfers tx then rx - alter rx",
224 .fill_option = FILL_COUNT_8,
225 .iterate_len = { ITERATE_MAX_LEN },
226 .iterate_tx_align = ITERATE_ALIGN,
227 .iterate_transfer_mask = BIT(1),
228 .transfer_count = 2,
229 .transfers = {
230 {
231 .len = 1,
232 .tx_buf = TX(0),
233 },
234 {
235 .rx_buf = RX(0),
236 },
237 },
238 },
239 {
240 .description = "two tx+rx transfers - alter both",
241 .fill_option = FILL_COUNT_8,
242 .iterate_len = { ITERATE_LEN },
243 .iterate_tx_align = ITERATE_ALIGN,
244 .iterate_transfer_mask = BIT(0) | BIT(1),
245 .transfer_count = 2,
246 .transfers = {
247 {
248 .tx_buf = TX(0),
249 .rx_buf = RX(0),
250 },
251 {
252 /* making sure we align without overwrite
253 * the reason we can not use ITERATE_MAX_LEN
254 */
255 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF),
256 .rx_buf = RX(SPI_TEST_MAX_SIZE_HALF),
257 },
258 },
259 },
260 {
261 .description = "two tx+rx transfers - alter first",
262 .fill_option = FILL_COUNT_8,
263 .iterate_len = { ITERATE_MAX_LEN },
264 .iterate_tx_align = ITERATE_ALIGN,
265 .iterate_transfer_mask = BIT(0),
266 .transfer_count = 2,
267 .transfers = {
268 {
269 /* making sure we align without overwrite */
270 .tx_buf = TX(1024),
271 .rx_buf = RX(1024),
272 },
273 {
274 .len = 1,
275 /* making sure we align without overwrite */
276 .tx_buf = TX(0),
277 .rx_buf = RX(0),
278 },
279 },
280 },
281 {
282 .description = "two tx+rx transfers - alter second",
283 .fill_option = FILL_COUNT_8,
284 .iterate_len = { ITERATE_MAX_LEN },
285 .iterate_tx_align = ITERATE_ALIGN,
286 .iterate_transfer_mask = BIT(1),
287 .transfer_count = 2,
288 .transfers = {
289 {
290 .len = 1,
291 .tx_buf = TX(0),
292 .rx_buf = RX(0),
293 },
294 {
295 /* making sure we align without overwrite */
296 .tx_buf = TX(1024),
297 .rx_buf = RX(1024),
298 },
299 },
300 },
301 {
302 .description = "two tx+rx transfers - delay after transfer",
303 .fill_option = FILL_COUNT_8,
304 .iterate_len = { ITERATE_MAX_LEN },
305 .iterate_transfer_mask = BIT(0) | BIT(1),
306 .transfer_count = 2,
307 .transfers = {
308 {
309 .tx_buf = TX(0),
310 .rx_buf = RX(0),
311 .delay = {
312 .value = 1000,
313 .unit = SPI_DELAY_UNIT_USECS,
314 },
315 },
316 {
317 .tx_buf = TX(0),
318 .rx_buf = RX(0),
319 .delay = {
320 .value = 1000,
321 .unit = SPI_DELAY_UNIT_USECS,
322 },
323 },
324 },
325 },
326 {
327 .description = "three tx+rx transfers with overlapping cache lines",
328 .fill_option = FILL_COUNT_8,
329 /*
330 * This should be large enough for the controller driver to
331 * choose to transfer it with DMA.
332 */
333 .iterate_len = { 512, -1 },
334 .iterate_transfer_mask = BIT(1),
335 .transfer_count = 3,
336 .transfers = {
337 {
338 .len = 1,
339 .tx_buf = TX(0),
340 .rx_buf = RX(0),
341 },
342 {
343 .tx_buf = TX(1),
344 .rx_buf = RX(1),
345 },
346 {
347 .len = 1,
348 .tx_buf = TX(513),
349 .rx_buf = RX(513),
350 },
351 },
352 },
353
354 { /* end of tests sequence */ }
355 };
356
spi_loopback_test_probe(struct spi_device * spi)357 static int spi_loopback_test_probe(struct spi_device *spi)
358 {
359 int ret;
360
361 if (loop_req || no_cs) {
362 spi->mode |= loop_req ? SPI_LOOP : 0;
363 spi->mode |= no_cs ? SPI_NO_CS : 0;
364 ret = spi_setup(spi);
365 if (ret) {
366 dev_err(&spi->dev, "SPI setup with SPI_LOOP or SPI_NO_CS failed (%d)\n",
367 ret);
368 return ret;
369 }
370 }
371
372 dev_info(&spi->dev, "Executing spi-loopback-tests\n");
373
374 ret = spi_test_run_tests(spi, spi_tests);
375
376 dev_info(&spi->dev, "Finished spi-loopback-tests with return: %i\n",
377 ret);
378
379 return ret;
380 }
381
382 /* non const match table to permit to change via a module parameter */
383 static struct of_device_id spi_loopback_test_of_match[] = {
384 { .compatible = "linux,spi-loopback-test", },
385 { }
386 };
387
388 /* allow to override the compatible string via a module_parameter */
389 module_param_string(compatible, spi_loopback_test_of_match[0].compatible,
390 sizeof(spi_loopback_test_of_match[0].compatible),
391 0000);
392
393 MODULE_DEVICE_TABLE(of, spi_loopback_test_of_match);
394
395 static struct spi_driver spi_loopback_test_driver = {
396 .driver = {
397 .name = "spi-loopback-test",
398 .of_match_table = spi_loopback_test_of_match,
399 },
400 .probe = spi_loopback_test_probe,
401 };
402
403 module_spi_driver(spi_loopback_test_driver);
404
405 MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
406 MODULE_DESCRIPTION("test spi_driver to check core functionality");
407 MODULE_LICENSE("GPL");
408
409 /*-------------------------------------------------------------------------*/
410
411 /* spi_test implementation */
412
413 #define RANGE_CHECK(ptr, plen, start, slen) \
414 ((ptr >= start) && (ptr + plen <= start + slen))
415
416 /* we allocate one page more, to allow for offsets */
417 #define SPI_TEST_MAX_SIZE_PLUS (SPI_TEST_MAX_SIZE + PAGE_SIZE)
418
spi_test_print_hex_dump(char * pre,const void * ptr,size_t len)419 static void spi_test_print_hex_dump(char *pre, const void *ptr, size_t len)
420 {
421 /* limit the hex_dump */
422 if (len <= 1024) {
423 print_hex_dump(KERN_INFO, pre,
424 DUMP_PREFIX_OFFSET, 16, 1,
425 ptr, len, 0);
426 return;
427 }
428 /* print head */
429 print_hex_dump(KERN_INFO, pre,
430 DUMP_PREFIX_OFFSET, 16, 1,
431 ptr, 512, 0);
432 /* print tail */
433 pr_info("%s truncated - continuing at offset %04zx\n",
434 pre, len - 512);
435 print_hex_dump(KERN_INFO, pre,
436 DUMP_PREFIX_OFFSET, 16, 1,
437 ptr + (len - 512), 512, 0);
438 }
439
spi_test_dump_message(struct spi_device * spi,struct spi_message * msg,bool dump_data)440 static void spi_test_dump_message(struct spi_device *spi,
441 struct spi_message *msg,
442 bool dump_data)
443 {
444 struct spi_transfer *xfer;
445 int i;
446 u8 b;
447
448 dev_info(&spi->dev, " spi_msg@%p\n", msg);
449 if (msg->status)
450 dev_info(&spi->dev, " status: %i\n",
451 msg->status);
452 dev_info(&spi->dev, " frame_length: %i\n",
453 msg->frame_length);
454 dev_info(&spi->dev, " actual_length: %i\n",
455 msg->actual_length);
456
457 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
458 dev_info(&spi->dev, " spi_transfer@%p\n", xfer);
459 dev_info(&spi->dev, " len: %i\n", xfer->len);
460 dev_info(&spi->dev, " tx_buf: %p\n", xfer->tx_buf);
461 if (dump_data && xfer->tx_buf)
462 spi_test_print_hex_dump(" TX: ",
463 xfer->tx_buf,
464 xfer->len);
465
466 dev_info(&spi->dev, " rx_buf: %p\n", xfer->rx_buf);
467 if (dump_data && xfer->rx_buf)
468 spi_test_print_hex_dump(" RX: ",
469 xfer->rx_buf,
470 xfer->len);
471 /* check for unwritten test pattern on rx_buf */
472 if (xfer->rx_buf) {
473 for (i = 0 ; i < xfer->len ; i++) {
474 b = ((u8 *)xfer->rx_buf)[xfer->len - 1 - i];
475 if (b != SPI_TEST_PATTERN_UNWRITTEN)
476 break;
477 }
478 if (i)
479 dev_info(&spi->dev,
480 " rx_buf filled with %02x starts at offset: %i\n",
481 SPI_TEST_PATTERN_UNWRITTEN,
482 xfer->len - i);
483 }
484 }
485 }
486
487 struct rx_ranges {
488 struct list_head list;
489 u8 *start;
490 u8 *end;
491 };
492
rx_ranges_cmp(void * priv,const struct list_head * a,const struct list_head * b)493 static int rx_ranges_cmp(void *priv, const struct list_head *a,
494 const struct list_head *b)
495 {
496 const struct rx_ranges *rx_a = list_entry(a, struct rx_ranges, list);
497 const struct rx_ranges *rx_b = list_entry(b, struct rx_ranges, list);
498
499 if (rx_a->start > rx_b->start)
500 return 1;
501 if (rx_a->start < rx_b->start)
502 return -1;
503 return 0;
504 }
505
spi_check_rx_ranges(struct spi_device * spi,struct spi_message * msg,void * rx)506 static int spi_check_rx_ranges(struct spi_device *spi,
507 struct spi_message *msg,
508 void *rx)
509 {
510 struct spi_transfer *xfer;
511 struct rx_ranges ranges[SPI_TEST_MAX_TRANSFERS], *r;
512 int i = 0;
513 LIST_HEAD(ranges_list);
514 u8 *addr;
515 int ret = 0;
516
517 /* loop over all transfers to fill in the rx_ranges */
518 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
519 /* if there is no rx, then no check is needed */
520 if (!xfer->rx_buf)
521 continue;
522 /* fill in the rx_range */
523 if (RANGE_CHECK(xfer->rx_buf, xfer->len,
524 rx, SPI_TEST_MAX_SIZE_PLUS)) {
525 ranges[i].start = xfer->rx_buf;
526 ranges[i].end = xfer->rx_buf + xfer->len;
527 list_add(&ranges[i].list, &ranges_list);
528 i++;
529 }
530 }
531
532 /* if no ranges, then we can return and avoid the checks...*/
533 if (!i)
534 return 0;
535
536 /* sort the list */
537 list_sort(NULL, &ranges_list, rx_ranges_cmp);
538
539 /* and iterate over all the rx addresses */
540 for (addr = rx; addr < (u8 *)rx + SPI_TEST_MAX_SIZE_PLUS; addr++) {
541 /* if we are the DO not write pattern,
542 * then continue with the loop...
543 */
544 if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE)
545 continue;
546
547 /* check if we are inside a range */
548 list_for_each_entry(r, &ranges_list, list) {
549 /* if so then set to end... */
550 if ((addr >= r->start) && (addr < r->end))
551 addr = r->end;
552 }
553 /* second test after a (hopefull) translation */
554 if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE)
555 continue;
556
557 /* if still not found then something has modified too much */
558 /* we could list the "closest" transfer here... */
559 dev_err(&spi->dev,
560 "loopback strangeness - rx changed outside of allowed range at: %p\n",
561 addr);
562 /* do not return, only set ret,
563 * so that we list all addresses
564 */
565 ret = -ERANGE;
566 }
567
568 return ret;
569 }
570
spi_test_check_elapsed_time(struct spi_device * spi,struct spi_test * test)571 static int spi_test_check_elapsed_time(struct spi_device *spi,
572 struct spi_test *test)
573 {
574 int i;
575 unsigned long long estimated_time = 0;
576 unsigned long long delay_usecs = 0;
577
578 for (i = 0; i < test->transfer_count; i++) {
579 struct spi_transfer *xfer = test->transfers + i;
580 unsigned long long nbits = (unsigned long long)BITS_PER_BYTE *
581 xfer->len;
582
583 delay_usecs += xfer->delay.value;
584 if (!xfer->speed_hz)
585 continue;
586 estimated_time += div_u64(nbits * NSEC_PER_SEC, xfer->speed_hz);
587 }
588
589 estimated_time += delay_usecs * NSEC_PER_USEC;
590 if (test->elapsed_time < estimated_time) {
591 dev_err(&spi->dev,
592 "elapsed time %lld ns is shorter than minimum estimated time %lld ns\n",
593 test->elapsed_time, estimated_time);
594
595 return -EINVAL;
596 }
597
598 return 0;
599 }
600
spi_test_check_loopback_result(struct spi_device * spi,struct spi_message * msg,void * tx,void * rx)601 static int spi_test_check_loopback_result(struct spi_device *spi,
602 struct spi_message *msg,
603 void *tx, void *rx)
604 {
605 struct spi_transfer *xfer;
606 u8 rxb, txb;
607 size_t i;
608 int ret;
609
610 /* checks rx_buffer pattern are valid with loopback or without */
611 if (check_ranges) {
612 ret = spi_check_rx_ranges(spi, msg, rx);
613 if (ret)
614 return ret;
615 }
616
617 /* if we run without loopback, then return now */
618 if (!loopback)
619 return 0;
620
621 /* if applicable to transfer check that rx_buf is equal to tx_buf */
622 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
623 /* if there is no rx, then no check is needed */
624 if (!xfer->len || !xfer->rx_buf)
625 continue;
626 /* so depending on tx_buf we need to handle things */
627 if (xfer->tx_buf) {
628 for (i = 0; i < xfer->len; i++) {
629 txb = ((u8 *)xfer->tx_buf)[i];
630 rxb = ((u8 *)xfer->rx_buf)[i];
631 if (txb != rxb)
632 goto mismatch_error;
633 }
634 } else {
635 /* first byte received */
636 txb = ((u8 *)xfer->rx_buf)[0];
637 /* first byte may be 0 or 0xff */
638 if (txb != 0 && txb != 0xff) {
639 dev_err(&spi->dev,
640 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n",
641 txb);
642 return -EINVAL;
643 }
644 /* check that all bytes are identical */
645 for (i = 1; i < xfer->len; i++) {
646 rxb = ((u8 *)xfer->rx_buf)[i];
647 if (rxb != txb)
648 goto mismatch_error;
649 }
650 }
651 }
652
653 return 0;
654
655 mismatch_error:
656 dev_err(&spi->dev,
657 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n",
658 i, txb, rxb);
659
660 return -EINVAL;
661 }
662
spi_test_translate(struct spi_device * spi,void ** ptr,size_t len,void * tx,void * rx)663 static int spi_test_translate(struct spi_device *spi,
664 void **ptr, size_t len,
665 void *tx, void *rx)
666 {
667 size_t off;
668
669 /* return on null */
670 if (!*ptr)
671 return 0;
672
673 /* in the MAX_SIZE_HALF case modify the pointer */
674 if (((size_t)*ptr) & SPI_TEST_MAX_SIZE_HALF)
675 /* move the pointer to the correct range */
676 *ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) -
677 SPI_TEST_MAX_SIZE_HALF;
678
679 /* RX range
680 * - we check against MAX_SIZE_PLUS to allow for automated alignment
681 */
682 if (RANGE_CHECK(*ptr, len, RX(0), SPI_TEST_MAX_SIZE_PLUS)) {
683 off = *ptr - RX(0);
684 *ptr = rx + off;
685
686 return 0;
687 }
688
689 /* TX range */
690 if (RANGE_CHECK(*ptr, len, TX(0), SPI_TEST_MAX_SIZE_PLUS)) {
691 off = *ptr - TX(0);
692 *ptr = tx + off;
693
694 return 0;
695 }
696
697 dev_err(&spi->dev,
698 "PointerRange [%p:%p[ not in range [%p:%p[ or [%p:%p[\n",
699 *ptr, *ptr + len,
700 RX(0), RX(SPI_TEST_MAX_SIZE),
701 TX(0), TX(SPI_TEST_MAX_SIZE));
702
703 return -EINVAL;
704 }
705
spi_test_fill_pattern(struct spi_device * spi,struct spi_test * test)706 static int spi_test_fill_pattern(struct spi_device *spi,
707 struct spi_test *test)
708 {
709 struct spi_transfer *xfers = test->transfers;
710 u8 *tx_buf;
711 size_t count = 0;
712 int i, j;
713
714 #ifdef __BIG_ENDIAN
715 #define GET_VALUE_BYTE(value, index, bytes) \
716 (value >> (8 * (bytes - 1 - count % bytes)))
717 #else
718 #define GET_VALUE_BYTE(value, index, bytes) \
719 (value >> (8 * (count % bytes)))
720 #endif
721
722 /* fill all transfers with the pattern requested */
723 for (i = 0; i < test->transfer_count; i++) {
724 /* fill rx_buf with SPI_TEST_PATTERN_UNWRITTEN */
725 if (xfers[i].rx_buf)
726 memset(xfers[i].rx_buf, SPI_TEST_PATTERN_UNWRITTEN,
727 xfers[i].len);
728 /* if tx_buf is NULL then skip */
729 tx_buf = (u8 *)xfers[i].tx_buf;
730 if (!tx_buf)
731 continue;
732 /* modify all the transfers */
733 for (j = 0; j < xfers[i].len; j++, tx_buf++, count++) {
734 /* fill tx */
735 switch (test->fill_option) {
736 case FILL_MEMSET_8:
737 *tx_buf = test->fill_pattern;
738 break;
739 case FILL_MEMSET_16:
740 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
741 count, 2);
742 break;
743 case FILL_MEMSET_24:
744 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
745 count, 3);
746 break;
747 case FILL_MEMSET_32:
748 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
749 count, 4);
750 break;
751 case FILL_COUNT_8:
752 *tx_buf = count;
753 break;
754 case FILL_COUNT_16:
755 *tx_buf = GET_VALUE_BYTE(count, count, 2);
756 break;
757 case FILL_COUNT_24:
758 *tx_buf = GET_VALUE_BYTE(count, count, 3);
759 break;
760 case FILL_COUNT_32:
761 *tx_buf = GET_VALUE_BYTE(count, count, 4);
762 break;
763 case FILL_TRANSFER_BYTE_8:
764 *tx_buf = j;
765 break;
766 case FILL_TRANSFER_BYTE_16:
767 *tx_buf = GET_VALUE_BYTE(j, j, 2);
768 break;
769 case FILL_TRANSFER_BYTE_24:
770 *tx_buf = GET_VALUE_BYTE(j, j, 3);
771 break;
772 case FILL_TRANSFER_BYTE_32:
773 *tx_buf = GET_VALUE_BYTE(j, j, 4);
774 break;
775 case FILL_TRANSFER_NUM:
776 *tx_buf = i;
777 break;
778 default:
779 dev_err(&spi->dev,
780 "unsupported fill_option: %i\n",
781 test->fill_option);
782 return -EINVAL;
783 }
784 }
785 }
786
787 return 0;
788 }
789
_spi_test_run_iter(struct spi_device * spi,struct spi_test * test,void * tx,void * rx)790 static int _spi_test_run_iter(struct spi_device *spi,
791 struct spi_test *test,
792 void *tx, void *rx)
793 {
794 struct spi_message *msg = &test->msg;
795 struct spi_transfer *x;
796 int i, ret;
797
798 /* initialize message - zero-filled via static initialization */
799 spi_message_init_no_memset(msg);
800
801 /* fill rx with the DO_NOT_WRITE pattern */
802 memset(rx, SPI_TEST_PATTERN_DO_NOT_WRITE, SPI_TEST_MAX_SIZE_PLUS);
803
804 /* add the individual transfers */
805 for (i = 0; i < test->transfer_count; i++) {
806 x = &test->transfers[i];
807
808 /* patch the values of tx_buf */
809 ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len,
810 (void *)tx, rx);
811 if (ret)
812 return ret;
813
814 /* patch the values of rx_buf */
815 ret = spi_test_translate(spi, &x->rx_buf, x->len,
816 (void *)tx, rx);
817 if (ret)
818 return ret;
819
820 /* and add it to the list */
821 spi_message_add_tail(x, msg);
822 }
823
824 /* fill in the transfer buffers with pattern */
825 ret = spi_test_fill_pattern(spi, test);
826 if (ret)
827 return ret;
828
829 /* and execute */
830 if (test->execute_msg)
831 ret = test->execute_msg(spi, test, tx, rx);
832 else
833 ret = spi_test_execute_msg(spi, test, tx, rx);
834
835 /* handle result */
836 if (ret == test->expected_return)
837 return 0;
838
839 dev_err(&spi->dev,
840 "test failed - test returned %i, but we expect %i\n",
841 ret, test->expected_return);
842
843 if (ret)
844 return ret;
845
846 /* if it is 0, as we expected something else,
847 * then return something special
848 */
849 return -EFAULT;
850 }
851
spi_test_run_iter(struct spi_device * spi,const struct spi_test * testtemplate,void * tx,void * rx,size_t len,size_t tx_off,size_t rx_off)852 static int spi_test_run_iter(struct spi_device *spi,
853 const struct spi_test *testtemplate,
854 void *tx, void *rx,
855 size_t len,
856 size_t tx_off,
857 size_t rx_off
858 )
859 {
860 struct spi_test test;
861 int i, tx_count, rx_count;
862
863 /* copy the test template to test */
864 memcpy(&test, testtemplate, sizeof(test));
865
866 /* if iterate_transfer_mask is not set,
867 * then set it to first transfer only
868 */
869 if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1)))
870 test.iterate_transfer_mask = 1;
871
872 /* count number of transfers with tx/rx_buf != NULL */
873 rx_count = tx_count = 0;
874 for (i = 0; i < test.transfer_count; i++) {
875 if (test.transfers[i].tx_buf)
876 tx_count++;
877 if (test.transfers[i].rx_buf)
878 rx_count++;
879 }
880
881 /* in some iteration cases warn and exit early,
882 * as there is nothing to do, that has not been tested already...
883 */
884 if (tx_off && (!tx_count)) {
885 dev_warn_once(&spi->dev,
886 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n",
887 test.description);
888 return 0;
889 }
890 if (rx_off && (!rx_count)) {
891 dev_warn_once(&spi->dev,
892 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n",
893 test.description);
894 return 0;
895 }
896
897 /* write out info */
898 if (!(len || tx_off || rx_off)) {
899 dev_info(&spi->dev, "Running test %s\n", test.description);
900 } else {
901 dev_info(&spi->dev,
902 " with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n",
903 len, tx_off, rx_off);
904 }
905
906 /* update in the values from iteration values */
907 for (i = 0; i < test.transfer_count; i++) {
908 /* only when bit in transfer mask is set */
909 if (!(test.iterate_transfer_mask & BIT(i)))
910 continue;
911 test.transfers[i].len = len;
912 if (test.transfers[i].tx_buf)
913 test.transfers[i].tx_buf += tx_off;
914 if (test.transfers[i].rx_buf)
915 test.transfers[i].rx_buf += rx_off;
916 }
917
918 /* and execute */
919 return _spi_test_run_iter(spi, &test, tx, rx);
920 }
921
922 /**
923 * spi_test_execute_msg - default implementation to run a test
924 *
925 * @spi: @spi_device on which to run the @spi_message
926 * @test: the test to execute, which already contains @msg
927 * @tx: the tx buffer allocated for the test sequence
928 * @rx: the rx buffer allocated for the test sequence
929 *
930 * Returns: error code of spi_sync as well as basic error checking
931 */
spi_test_execute_msg(struct spi_device * spi,struct spi_test * test,void * tx,void * rx)932 int spi_test_execute_msg(struct spi_device *spi, struct spi_test *test,
933 void *tx, void *rx)
934 {
935 struct spi_message *msg = &test->msg;
936 int ret = 0;
937 int i;
938
939 /* only if we do not simulate */
940 if (!simulate_only) {
941 ktime_t start;
942
943 /* dump the complete message before and after the transfer */
944 if (dump_messages == 3)
945 spi_test_dump_message(spi, msg, true);
946
947 start = ktime_get();
948 /* run spi message */
949 ret = spi_sync(spi, msg);
950 test->elapsed_time = ktime_to_ns(ktime_sub(ktime_get(), start));
951 if (ret == -ETIMEDOUT) {
952 dev_info(&spi->dev,
953 "spi-message timed out - rerunning...\n");
954 /* rerun after a few explicit schedules */
955 for (i = 0; i < 16; i++)
956 schedule();
957 ret = spi_sync(spi, msg);
958 }
959 if (ret) {
960 dev_err(&spi->dev,
961 "Failed to execute spi_message: %i\n",
962 ret);
963 goto exit;
964 }
965
966 /* do some extra error checks */
967 if (msg->frame_length != msg->actual_length) {
968 dev_err(&spi->dev,
969 "actual length differs from expected\n");
970 ret = -EIO;
971 goto exit;
972 }
973
974 /* run rx-buffer tests */
975 ret = spi_test_check_loopback_result(spi, msg, tx, rx);
976 if (ret)
977 goto exit;
978
979 ret = spi_test_check_elapsed_time(spi, test);
980 }
981
982 /* if requested or on error dump message (including data) */
983 exit:
984 if (dump_messages || ret)
985 spi_test_dump_message(spi, msg,
986 (dump_messages >= 2) || (ret));
987
988 return ret;
989 }
990 EXPORT_SYMBOL_GPL(spi_test_execute_msg);
991
992 /**
993 * spi_test_run_test - run an individual spi_test
994 * including all the relevant iterations on:
995 * length and buffer alignment
996 *
997 * @spi: the spi_device to send the messages to
998 * @test: the test which we need to execute
999 * @tx: the tx buffer allocated for the test sequence
1000 * @rx: the rx buffer allocated for the test sequence
1001 *
1002 * Returns: status code of spi_sync or other failures
1003 */
1004
spi_test_run_test(struct spi_device * spi,const struct spi_test * test,void * tx,void * rx)1005 int spi_test_run_test(struct spi_device *spi, const struct spi_test *test,
1006 void *tx, void *rx)
1007 {
1008 int idx_len;
1009 size_t len;
1010 size_t tx_align, rx_align;
1011 int ret;
1012
1013 /* test for transfer limits */
1014 if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) {
1015 dev_err(&spi->dev,
1016 "%s: Exceeded max number of transfers with %i\n",
1017 test->description, test->transfer_count);
1018 return -E2BIG;
1019 }
1020
1021 /* setting up some values in spi_message
1022 * based on some settings in spi_master
1023 * some of this can also get done in the run() method
1024 */
1025
1026 /* iterate over all the iterable values using macros
1027 * (to make it a bit more readable...
1028 */
1029 #define FOR_EACH_ALIGNMENT(var) \
1030 for (var = 0; \
1031 var < (test->iterate_##var ? \
1032 (spi->controller->dma_alignment ? \
1033 spi->controller->dma_alignment : \
1034 test->iterate_##var) : \
1035 1); \
1036 var++)
1037
1038 for (idx_len = 0; idx_len < SPI_TEST_MAX_ITERATE &&
1039 (len = test->iterate_len[idx_len]) != -1; idx_len++) {
1040 if ((run_only_iter_len > -1) && len != run_only_iter_len)
1041 continue;
1042 FOR_EACH_ALIGNMENT(tx_align) {
1043 FOR_EACH_ALIGNMENT(rx_align) {
1044 /* and run the iteration */
1045 ret = spi_test_run_iter(spi, test,
1046 tx, rx,
1047 len,
1048 tx_align,
1049 rx_align);
1050 if (ret)
1051 return ret;
1052 }
1053 }
1054 }
1055
1056 return 0;
1057 }
1058 EXPORT_SYMBOL_GPL(spi_test_run_test);
1059
1060 /**
1061 * spi_test_run_tests - run an array of spi_messages tests
1062 * @spi: the spi device on which to run the tests
1063 * @tests: NULL-terminated array of @spi_test
1064 *
1065 * Returns: status errors as per @spi_test_run_test()
1066 */
1067
spi_test_run_tests(struct spi_device * spi,struct spi_test * tests)1068 int spi_test_run_tests(struct spi_device *spi,
1069 struct spi_test *tests)
1070 {
1071 char *rx = NULL, *tx = NULL;
1072 int ret = 0, count = 0;
1073 struct spi_test *test;
1074
1075 /* allocate rx/tx buffers of 128kB size without devm
1076 * in the hope that is on a page boundary
1077 */
1078 if (use_vmalloc)
1079 rx = vmalloc(SPI_TEST_MAX_SIZE_PLUS);
1080 else
1081 rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
1082 if (!rx)
1083 return -ENOMEM;
1084
1085
1086 if (use_vmalloc)
1087 tx = vmalloc(SPI_TEST_MAX_SIZE_PLUS);
1088 else
1089 tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
1090 if (!tx) {
1091 ret = -ENOMEM;
1092 goto err_tx;
1093 }
1094
1095 /* now run the individual tests in the table */
1096 for (test = tests, count = 0; test->description[0];
1097 test++, count++) {
1098 /* only run test if requested */
1099 if ((run_only_test > -1) && (count != run_only_test))
1100 continue;
1101 /* run custom implementation */
1102 if (test->run_test)
1103 ret = test->run_test(spi, test, tx, rx);
1104 else
1105 ret = spi_test_run_test(spi, test, tx, rx);
1106 if (ret)
1107 goto out;
1108 /* add some delays so that we can easily
1109 * detect the individual tests when using a logic analyzer
1110 * we also add scheduling to avoid potential spi_timeouts...
1111 */
1112 if (delay_ms)
1113 mdelay(delay_ms);
1114 schedule();
1115 }
1116
1117 out:
1118 kvfree(tx);
1119 err_tx:
1120 kvfree(rx);
1121 return ret;
1122 }
1123 EXPORT_SYMBOL_GPL(spi_test_run_tests);
1124