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