2 * linux/drivers/spi/spi-loopback-test.c
4 * (c) Martin Sperl <kernel@martin.sperl.org>
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
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.
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.
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/vmalloc.h>
29 #include <linux/spi/spi.h>
33 /* flag to only simulate transfers */
35 module_param(simulate_only, int, 0);
36 MODULE_PARM_DESC(simulate_only, "if not 0 do not execute the spi message");
38 /* dump spi messages */
40 module_param(dump_messages, int, 0);
41 MODULE_PARM_DESC(dump_messages,
42 "=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 */
47 module_param(loopback, int, 0);
48 MODULE_PARM_DESC(loopback,
49 "if set enable loopback mode, where the rx_buf " \
50 "is checked to match tx_buf after the spi_message " \
53 /* run only a specific test */
54 int run_only_test = -1;
55 module_param(run_only_test, int, 0);
56 MODULE_PARM_DESC(run_only_test,
57 "only run the test with this number (0-based !)");
59 /* use vmalloc'ed buffers */
61 module_param(use_vmalloc, int, 0644);
62 MODULE_PARM_DESC(use_vmalloc,
63 "use vmalloc'ed buffers instead of kmalloc'ed");
67 module_param(check_ranges, int, 0644);
68 MODULE_PARM_DESC(check_ranges,
69 "checks rx_buffer pattern are valid");
71 /* the actual tests to execute */
72 static struct spi_test spi_tests[] = {
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,
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,
96 .tx_buf = TX(PAGE_SIZE - 4),
97 .rx_buf = RX(PAGE_SIZE - 4),
102 .description = "tx-transfer - only",
103 .fill_option = FILL_COUNT_8,
104 .iterate_len = { ITERATE_MAX_LEN },
105 .iterate_tx_align = ITERATE_ALIGN,
114 .description = "rx-transfer - only",
115 .fill_option = FILL_COUNT_8,
116 .iterate_len = { ITERATE_MAX_LEN },
117 .iterate_rx_align = ITERATE_ALIGN,
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),
138 /* this is why we cant use ITERATE_MAX_LEN */
139 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF),
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),
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),
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),
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),
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),
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),
242 /* making sure we align without overwrite
243 * the reason we can not use ITERATE_MAX_LEN
245 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF),
246 .rx_buf = RX(SPI_TEST_MAX_SIZE_HALF),
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),
259 /* making sure we align without overwrite */
265 /* making sure we align without overwrite */
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,
276 .iterate_transfer_mask = BIT(1),
285 /* making sure we align without overwrite */
292 { /* end of tests sequence */ }
295 static int spi_loopback_test_probe(struct spi_device *spi)
299 dev_info(&spi->dev, "Executing spi-loopback-tests\n");
301 ret = spi_test_run_tests(spi, spi_tests);
303 dev_info(&spi->dev, "Finished spi-loopback-tests with return: %i\n",
309 /* non const match table to permit to change via a module parameter */
310 static struct of_device_id spi_loopback_test_of_match[] = {
311 { .compatible = "linux,spi-loopback-test", },
315 /* allow to override the compatible string via a module_parameter */
316 module_param_string(compatible, spi_loopback_test_of_match[0].compatible,
317 sizeof(spi_loopback_test_of_match[0].compatible),
320 MODULE_DEVICE_TABLE(of, spi_loopback_test_of_match);
322 static struct spi_driver spi_loopback_test_driver = {
324 .name = "spi-loopback-test",
325 .owner = THIS_MODULE,
326 .of_match_table = spi_loopback_test_of_match,
328 .probe = spi_loopback_test_probe,
331 module_spi_driver(spi_loopback_test_driver);
333 MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
334 MODULE_DESCRIPTION("test spi_driver to check core functionality");
335 MODULE_LICENSE("GPL");
337 /*-------------------------------------------------------------------------*/
339 /* spi_test implementation */
341 #define RANGE_CHECK(ptr, plen, start, slen) \
342 ((ptr >= start) && (ptr + plen <= start + slen))
344 /* we allocate one page more, to allow for offsets */
345 #define SPI_TEST_MAX_SIZE_PLUS (SPI_TEST_MAX_SIZE + PAGE_SIZE)
347 static void spi_test_print_hex_dump(char *pre, const void *ptr, size_t len)
349 /* limit the hex_dump */
351 print_hex_dump(KERN_INFO, pre,
352 DUMP_PREFIX_OFFSET, 16, 1,
357 print_hex_dump(KERN_INFO, pre,
358 DUMP_PREFIX_OFFSET, 16, 1,
361 pr_info("%s truncated - continuing at offset %04zx\n",
363 print_hex_dump(KERN_INFO, pre,
364 DUMP_PREFIX_OFFSET, 16, 1,
365 ptr + (len - 512), 512, 0);
368 static void spi_test_dump_message(struct spi_device *spi,
369 struct spi_message *msg,
372 struct spi_transfer *xfer;
376 dev_info(&spi->dev, " spi_msg@%pK\n", msg);
378 dev_info(&spi->dev, " status: %i\n",
380 dev_info(&spi->dev, " frame_length: %i\n",
382 dev_info(&spi->dev, " actual_length: %i\n",
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: ",
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: ",
399 /* check for unwritten test pattern on 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)
408 " rx_buf filled with %02x starts at offset: %i\n",
409 SPI_TEST_PATTERN_UNWRITTEN,
416 struct list_head list;
421 static int rx_ranges_cmp(void *priv, struct list_head *a, struct list_head *b)
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);
426 if (rx_a->start > rx_b->start)
428 if (rx_a->start < rx_b->start)
433 static int spi_check_rx_ranges(struct spi_device *spi,
434 struct spi_message *msg,
437 struct spi_transfer *xfer;
438 struct rx_ranges ranges[SPI_TEST_MAX_TRANSFERS], *r;
440 LIST_HEAD(ranges_list);
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 */
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);
459 /* if no ranges, then we can return and avoid the checks...*/
464 list_sort(NULL, &ranges_list, rx_ranges_cmp);
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...
471 if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE)
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))
480 /* second test after a (hopefull) translation */
481 if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE)
484 /* if still not found then something has modified too much */
485 /* we could list the "closest" transfer here... */
487 "loopback strangeness - rx changed outside of allowed range at: %pK\n",
489 /* do not return, only set ret,
490 * so that we list all addresses
498 static int spi_test_check_loopback_result(struct spi_device *spi,
499 struct spi_message *msg,
502 struct spi_transfer *xfer;
507 /* checks rx_buffer pattern are valid with loopback or without */
509 ret = spi_check_rx_ranges(spi, msg, rx);
514 /* if we run without loopback, then return now */
518 /* if applicable to transfer check that rx_buf is equal to tx_buf */
519 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
520 /* if there is no rx, then no check is needed */
523 /* so depending on tx_buf we need to handle things */
525 for (i = 1; i < xfer->len; i++) {
526 txb = ((u8 *)xfer->tx_buf)[i];
527 rxb = ((u8 *)xfer->rx_buf)[i];
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))) {
537 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n",
541 /* check that all bytes are identical */
542 for (i = 1; i < xfer->len; i++) {
543 rxb = ((u8 *)xfer->rx_buf)[i];
554 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n",
560 static int spi_test_translate(struct spi_device *spi,
561 void **ptr, size_t len,
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;
577 * - we check against MAX_SIZE_PLUS to allow for automated alignment
579 if (RANGE_CHECK(*ptr, len, RX(0), SPI_TEST_MAX_SIZE_PLUS)) {
587 if (RANGE_CHECK(*ptr, len, TX(0), SPI_TEST_MAX_SIZE_PLUS)) {
595 "PointerRange [%pK:%pK[ not in range [%pK:%pK[ or [%pK:%pK[\n",
597 RX(0), RX(SPI_TEST_MAX_SIZE),
598 TX(0), TX(SPI_TEST_MAX_SIZE));
603 static int spi_test_fill_pattern(struct spi_device *spi,
604 struct spi_test *test)
606 struct spi_transfer *xfers = test->transfers;
612 #define GET_VALUE_BYTE(value, index, bytes) \
613 (value >> (8 * (bytes - 1 - count % bytes)))
615 #define GET_VALUE_BYTE(value, index, bytes) \
616 (value >> (8 * (count % bytes)))
619 /* fill all transfers with the pattern requested */
620 for (i = 0; i < test->transfer_count; i++) {
621 /* fill rx_buf with SPI_TEST_PATTERN_UNWRITTEN */
623 memset(xfers[i].rx_buf, SPI_TEST_PATTERN_UNWRITTEN,
625 /* if tx_buf is NULL then skip */
626 tx_buf = (u8 *)xfers[i].tx_buf;
629 /* modify all the transfers */
630 for (j = 0; j < xfers[i].len; j++, tx_buf++, count++) {
632 switch (test->fill_option) {
634 *tx_buf = test->fill_pattern;
637 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
641 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
645 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
652 *tx_buf = GET_VALUE_BYTE(count, count, 2);
655 *tx_buf = GET_VALUE_BYTE(count, count, 3);
658 *tx_buf = GET_VALUE_BYTE(count, count, 4);
660 case FILL_TRANSFER_BYTE_8:
663 case FILL_TRANSFER_BYTE_16:
664 *tx_buf = GET_VALUE_BYTE(j, j, 2);
666 case FILL_TRANSFER_BYTE_24:
667 *tx_buf = GET_VALUE_BYTE(j, j, 3);
669 case FILL_TRANSFER_BYTE_32:
670 *tx_buf = GET_VALUE_BYTE(j, j, 4);
672 case FILL_TRANSFER_NUM:
677 "unsupported fill_option: %i\n",
687 static int _spi_test_run_iter(struct spi_device *spi,
688 struct spi_test *test,
691 struct spi_message *msg = &test->msg;
692 struct spi_transfer *x;
695 /* initialize message - zero-filled via static initialization */
696 spi_message_init_no_memset(msg);
698 /* fill rx with the DO_NOT_WRITE pattern */
699 memset(rx, SPI_TEST_PATTERN_DO_NOT_WRITE, SPI_TEST_MAX_SIZE_PLUS);
701 /* add the individual transfers */
702 for (i = 0; i < test->transfer_count; i++) {
703 x = &test->transfers[i];
705 /* patch the values of tx_buf */
706 ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len,
711 /* patch the values of rx_buf */
712 ret = spi_test_translate(spi, &x->rx_buf, x->len,
717 /* and add it to the list */
718 spi_message_add_tail(x, msg);
721 /* fill in the transfer buffers with pattern */
722 ret = spi_test_fill_pattern(spi, test);
727 if (test->execute_msg)
728 ret = test->execute_msg(spi, test, tx, rx);
730 ret = spi_test_execute_msg(spi, test, tx, rx);
733 if (ret == test->expected_return)
737 "test failed - test returned %i, but we expect %i\n",
738 ret, test->expected_return);
743 /* if it is 0, as we expected something else,
744 * then return something special
749 static int spi_test_run_iter(struct spi_device *spi,
750 const struct spi_test *testtemplate,
757 struct spi_test test;
758 int i, tx_count, rx_count;
760 /* copy the test template to test */
761 memcpy(&test, testtemplate, sizeof(test));
763 /* set up test->transfers to the correct count */
764 if (!test.transfer_count) {
766 (i < SPI_TEST_MAX_TRANSFERS) && test.transfers[i].len;
768 test.transfer_count++;
772 /* if iterate_transfer_mask is not set,
773 * then set it to first transfer only
775 if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1)))
776 test.iterate_transfer_mask = 1;
778 /* count number of transfers with tx/rx_buf != NULL */
779 rx_count = tx_count = 0;
780 for (i = 0; i < test.transfer_count; i++) {
781 if (test.transfers[i].tx_buf)
783 if (test.transfers[i].rx_buf)
787 /* in some iteration cases warn and exit early,
788 * as there is nothing to do, that has not been tested already...
790 if (tx_off && (!tx_count)) {
791 dev_warn_once(&spi->dev,
792 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n",
796 if (rx_off && (!rx_count)) {
797 dev_warn_once(&spi->dev,
798 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n",
804 if (!(len || tx_off || rx_off)) {
805 dev_info(&spi->dev, "Running test %s\n", test.description);
808 " with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n",
809 len, tx_off, rx_off);
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)))
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;
826 return _spi_test_run_iter(spi, &test, tx, rx);
830 * spi_test_execute_msg - default implementation to run a test
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
837 * Returns: error code of spi_sync as well as basic error checking
839 int spi_test_execute_msg(struct spi_device *spi, struct spi_test *test,
842 struct spi_message *msg = &test->msg;
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);
852 /* run spi message */
853 ret = spi_sync(spi, msg);
854 if (ret == -ETIMEDOUT) {
856 "spi-message timed out - reruning...\n");
857 /* rerun after a few explicit schedules */
858 for (i = 0; i < 16; i++)
860 ret = spi_sync(spi, msg);
864 "Failed to execute spi_message: %i\n",
869 /* do some extra error checks */
870 if (msg->frame_length != msg->actual_length) {
872 "actual length differs from expected\n");
877 /* run rx-buffer tests */
878 ret = spi_test_check_loopback_result(spi, msg, tx, rx);
881 /* if requested or on error dump message (including data) */
883 if (dump_messages || ret)
884 spi_test_dump_message(spi, msg,
885 (dump_messages >= 2) || (ret));
889 EXPORT_SYMBOL_GPL(spi_test_execute_msg);
892 * spi_test_run_test - run an individual spi_test
893 * including all the relevant iterations on:
894 * length and buffer alignment
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
901 * Returns: status code of spi_sync or other failures
904 int spi_test_run_test(struct spi_device *spi, const struct spi_test *test,
909 size_t tx_align, rx_align;
912 /* test for transfer limits */
913 if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) {
915 "%s: Exceeded max number of transfers with %i\n",
916 test->description, test->transfer_count);
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
925 /* iterate over all the iterable values using macros
926 * (to make it a bit more readable...
928 #define FOR_EACH_ITERATE(var, defaultvalue) \
929 for (idx_##var = -1, var = defaultvalue; \
930 ((idx_##var < 0) || \
932 (idx_##var < SPI_TEST_MAX_ITERATE) && \
933 (var = test->iterate_##var[idx_##var]) \
937 #define FOR_EACH_ALIGNMENT(var) \
939 var < (test->iterate_##var ? \
940 (spi->master->dma_alignment ? \
941 spi->master->dma_alignment : \
942 test->iterate_##var) : \
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,
963 EXPORT_SYMBOL_GPL(spi_test_run_test);
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
970 * Returns: status errors as per @spi_test_run_test()
973 int spi_test_run_tests(struct spi_device *spi,
974 struct spi_test *tests)
976 char *rx = NULL, *tx = NULL;
977 int ret = 0, count = 0;
978 struct spi_test *test;
980 /* allocate rx/tx buffers of 128kB size without devm
981 * in the hope that is on a page boundary
984 rx = vmalloc(SPI_TEST_MAX_SIZE_PLUS);
986 rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
993 tx = vmalloc(SPI_TEST_MAX_SIZE_PLUS);
995 tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
1001 /* now run the individual tests in the table */
1002 for (test = tests, count = 0; test->description[0];
1004 /* only run test if requested */
1005 if ((run_only_test > -1) && (count != run_only_test))
1007 /* run custom implementation */
1009 ret = test->run_test(spi, test, tx, rx);
1011 ret = spi_test_run_test(spi, test, tx, rx);
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...
1027 EXPORT_SYMBOL_GPL(spi_test_run_tests);