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