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