1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/spi/spi-loopback-test.c
5 * (c) Martin Sperl <kernel@martin.sperl.org>
7 * Loopback test driver to test several typical spi_message conditions
8 * that a spi_master driver may encounter
9 * this can also get used for regression testing
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/ktime.h>
15 #include <linux/list.h>
16 #include <linux/list_sort.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/printk.h>
20 #include <linux/vmalloc.h>
21 #include <linux/spi/spi.h>
25 /* flag to only simulate transfers */
26 static int simulate_only
;
27 module_param(simulate_only
, int, 0);
28 MODULE_PARM_DESC(simulate_only
, "if not 0 do not execute the spi message");
30 /* dump spi messages */
31 static int dump_messages
;
32 module_param(dump_messages
, int, 0);
33 MODULE_PARM_DESC(dump_messages
,
34 "=1 dump the basic spi_message_structure, " \
35 "=2 dump the spi_message_structure including data, " \
36 "=3 dump the spi_message structure before and after execution");
37 /* the device is jumpered for loopback - enabling some rx_buf tests */
39 module_param(loopback
, int, 0);
40 MODULE_PARM_DESC(loopback
,
41 "if set enable loopback mode, where the rx_buf " \
42 "is checked to match tx_buf after the spi_message " \
46 module_param(loop_req
, int, 0);
47 MODULE_PARM_DESC(loop_req
,
48 "if set controller will be asked to enable test loop mode. " \
49 "If controller supported it, MISO and MOSI will be connected");
52 module_param(no_cs
, int, 0);
53 MODULE_PARM_DESC(no_cs
,
54 "if set Chip Select (CS) will not be used");
56 /* run tests only for a specific length */
57 static int run_only_iter_len
= -1;
58 module_param(run_only_iter_len
, int, 0);
59 MODULE_PARM_DESC(run_only_iter_len
,
60 "only run tests for a length of this number in iterate_len list");
62 /* run only a specific test */
63 static int run_only_test
= -1;
64 module_param(run_only_test
, int, 0);
65 MODULE_PARM_DESC(run_only_test
,
66 "only run the test with this number (0-based !)");
68 /* use vmalloc'ed buffers */
69 static int use_vmalloc
;
70 module_param(use_vmalloc
, int, 0644);
71 MODULE_PARM_DESC(use_vmalloc
,
72 "use vmalloc'ed buffers instead of kmalloc'ed");
75 static int check_ranges
= 1;
76 module_param(check_ranges
, int, 0644);
77 MODULE_PARM_DESC(check_ranges
,
78 "checks rx_buffer pattern are valid");
80 static unsigned int delay_ms
= 100;
81 module_param(delay_ms
, uint
, 0644);
82 MODULE_PARM_DESC(delay_ms
,
83 "delay between tests, in milliseconds (default: 100)");
85 /* the actual tests to execute */
86 static struct spi_test spi_tests
[] = {
88 .description
= "tx/rx-transfer - start of page",
89 .fill_option
= FILL_COUNT_8
,
90 .iterate_len
= { ITERATE_MAX_LEN
},
91 .iterate_tx_align
= ITERATE_ALIGN
,
92 .iterate_rx_align
= ITERATE_ALIGN
,
102 .description
= "tx/rx-transfer - crossing PAGE_SIZE",
103 .fill_option
= FILL_COUNT_8
,
104 .iterate_len
= { ITERATE_LEN
},
105 .iterate_tx_align
= ITERATE_ALIGN
,
106 .iterate_rx_align
= ITERATE_ALIGN
,
110 .tx_buf
= TX(PAGE_SIZE
- 4),
111 .rx_buf
= RX(PAGE_SIZE
- 4),
116 .description
= "tx-transfer - only",
117 .fill_option
= FILL_COUNT_8
,
118 .iterate_len
= { ITERATE_MAX_LEN
},
119 .iterate_tx_align
= ITERATE_ALIGN
,
128 .description
= "rx-transfer - only",
129 .fill_option
= FILL_COUNT_8
,
130 .iterate_len
= { ITERATE_MAX_LEN
},
131 .iterate_rx_align
= ITERATE_ALIGN
,
140 .description
= "two tx-transfers - alter both",
141 .fill_option
= FILL_COUNT_8
,
142 .iterate_len
= { ITERATE_LEN
},
143 .iterate_tx_align
= ITERATE_ALIGN
,
144 .iterate_transfer_mask
= BIT(0) | BIT(1),
151 /* this is why we cant use ITERATE_MAX_LEN */
152 .tx_buf
= TX(SPI_TEST_MAX_SIZE_HALF
),
157 .description
= "two tx-transfers - alter first",
158 .fill_option
= FILL_COUNT_8
,
159 .iterate_len
= { ITERATE_MAX_LEN
},
160 .iterate_tx_align
= ITERATE_ALIGN
,
161 .iterate_transfer_mask
= BIT(0),
174 .description
= "two tx-transfers - alter second",
175 .fill_option
= FILL_COUNT_8
,
176 .iterate_len
= { ITERATE_MAX_LEN
},
177 .iterate_tx_align
= ITERATE_ALIGN
,
178 .iterate_transfer_mask
= BIT(1),
191 .description
= "two transfers tx then rx - alter both",
192 .fill_option
= FILL_COUNT_8
,
193 .iterate_len
= { ITERATE_MAX_LEN
},
194 .iterate_tx_align
= ITERATE_ALIGN
,
195 .iterate_transfer_mask
= BIT(0) | BIT(1),
207 .description
= "two transfers tx then rx - alter tx",
208 .fill_option
= FILL_COUNT_8
,
209 .iterate_len
= { ITERATE_MAX_LEN
},
210 .iterate_tx_align
= ITERATE_ALIGN
,
211 .iterate_transfer_mask
= BIT(0),
224 .description
= "two transfers tx then rx - alter rx",
225 .fill_option
= FILL_COUNT_8
,
226 .iterate_len
= { ITERATE_MAX_LEN
},
227 .iterate_tx_align
= ITERATE_ALIGN
,
228 .iterate_transfer_mask
= BIT(1),
241 .description
= "two tx+rx transfers - alter both",
242 .fill_option
= FILL_COUNT_8
,
243 .iterate_len
= { ITERATE_LEN
},
244 .iterate_tx_align
= ITERATE_ALIGN
,
245 .iterate_transfer_mask
= BIT(0) | BIT(1),
253 /* making sure we align without overwrite
254 * the reason we can not use ITERATE_MAX_LEN
256 .tx_buf
= TX(SPI_TEST_MAX_SIZE_HALF
),
257 .rx_buf
= RX(SPI_TEST_MAX_SIZE_HALF
),
262 .description
= "two tx+rx transfers - alter first",
263 .fill_option
= FILL_COUNT_8
,
264 .iterate_len
= { ITERATE_MAX_LEN
},
265 .iterate_tx_align
= ITERATE_ALIGN
,
266 .iterate_transfer_mask
= BIT(0),
270 /* making sure we align without overwrite */
276 /* making sure we align without overwrite */
283 .description
= "two tx+rx transfers - alter second",
284 .fill_option
= FILL_COUNT_8
,
285 .iterate_len
= { ITERATE_MAX_LEN
},
286 .iterate_tx_align
= ITERATE_ALIGN
,
287 .iterate_transfer_mask
= BIT(1),
296 /* making sure we align without overwrite */
303 .description
= "two tx+rx transfers - delay after transfer",
304 .fill_option
= FILL_COUNT_8
,
305 .iterate_len
= { ITERATE_MAX_LEN
},
306 .iterate_transfer_mask
= BIT(0) | BIT(1),
314 .unit
= SPI_DELAY_UNIT_USECS
,
322 .unit
= SPI_DELAY_UNIT_USECS
,
328 .description
= "three tx+rx transfers with overlapping cache lines",
329 .fill_option
= FILL_COUNT_8
,
331 * This should be large enough for the controller driver to
332 * choose to transfer it with DMA.
334 .iterate_len
= { 512, -1 },
335 .iterate_transfer_mask
= BIT(1),
355 { /* end of tests sequence */ }
358 static int spi_loopback_test_probe(struct spi_device
*spi
)
362 if (loop_req
|| no_cs
) {
363 spi
->mode
|= loop_req
? SPI_LOOP
: 0;
364 spi
->mode
|= no_cs
? SPI_NO_CS
: 0;
365 ret
= spi_setup(spi
);
367 dev_err(&spi
->dev
, "SPI setup with SPI_LOOP or SPI_NO_CS failed (%d)\n",
373 dev_info(&spi
->dev
, "Executing spi-loopback-tests\n");
375 ret
= spi_test_run_tests(spi
, spi_tests
);
377 dev_info(&spi
->dev
, "Finished spi-loopback-tests with return: %i\n",
383 /* non const match table to permit to change via a module parameter */
384 static struct of_device_id spi_loopback_test_of_match
[] = {
385 { .compatible
= "linux,spi-loopback-test", },
389 /* allow to override the compatible string via a module_parameter */
390 module_param_string(compatible
, spi_loopback_test_of_match
[0].compatible
,
391 sizeof(spi_loopback_test_of_match
[0].compatible
),
394 MODULE_DEVICE_TABLE(of
, spi_loopback_test_of_match
);
396 static struct spi_driver spi_loopback_test_driver
= {
398 .name
= "spi-loopback-test",
399 .of_match_table
= spi_loopback_test_of_match
,
401 .probe
= spi_loopback_test_probe
,
404 module_spi_driver(spi_loopback_test_driver
);
406 MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
407 MODULE_DESCRIPTION("test spi_driver to check core functionality");
408 MODULE_LICENSE("GPL");
410 /*-------------------------------------------------------------------------*/
412 /* spi_test implementation */
414 #define RANGE_CHECK(ptr, plen, start, slen) \
415 ((ptr >= start) && (ptr + plen <= start + slen))
417 /* we allocate one page more, to allow for offsets */
418 #define SPI_TEST_MAX_SIZE_PLUS (SPI_TEST_MAX_SIZE + PAGE_SIZE)
420 static void spi_test_print_hex_dump(char *pre
, const void *ptr
, size_t len
)
422 /* limit the hex_dump */
424 print_hex_dump(KERN_INFO
, pre
,
425 DUMP_PREFIX_OFFSET
, 16, 1,
430 print_hex_dump(KERN_INFO
, pre
,
431 DUMP_PREFIX_OFFSET
, 16, 1,
434 pr_info("%s truncated - continuing at offset %04zx\n",
436 print_hex_dump(KERN_INFO
, pre
,
437 DUMP_PREFIX_OFFSET
, 16, 1,
438 ptr
+ (len
- 512), 512, 0);
441 static void spi_test_dump_message(struct spi_device
*spi
,
442 struct spi_message
*msg
,
445 struct spi_transfer
*xfer
;
449 dev_info(&spi
->dev
, " spi_msg@%pK\n", msg
);
451 dev_info(&spi
->dev
, " status: %i\n",
453 dev_info(&spi
->dev
, " frame_length: %i\n",
455 dev_info(&spi
->dev
, " actual_length: %i\n",
458 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
459 dev_info(&spi
->dev
, " spi_transfer@%pK\n", xfer
);
460 dev_info(&spi
->dev
, " len: %i\n", xfer
->len
);
461 dev_info(&spi
->dev
, " tx_buf: %pK\n", xfer
->tx_buf
);
462 if (dump_data
&& xfer
->tx_buf
)
463 spi_test_print_hex_dump(" TX: ",
467 dev_info(&spi
->dev
, " rx_buf: %pK\n", xfer
->rx_buf
);
468 if (dump_data
&& xfer
->rx_buf
)
469 spi_test_print_hex_dump(" RX: ",
472 /* check for unwritten test pattern on rx_buf */
474 for (i
= 0 ; i
< xfer
->len
; i
++) {
475 b
= ((u8
*)xfer
->rx_buf
)[xfer
->len
- 1 - i
];
476 if (b
!= SPI_TEST_PATTERN_UNWRITTEN
)
481 " rx_buf filled with %02x starts at offset: %i\n",
482 SPI_TEST_PATTERN_UNWRITTEN
,
489 struct list_head list
;
494 static int rx_ranges_cmp(void *priv
, const struct list_head
*a
,
495 const struct list_head
*b
)
497 struct rx_ranges
*rx_a
= list_entry(a
, struct rx_ranges
, list
);
498 struct rx_ranges
*rx_b
= list_entry(b
, struct rx_ranges
, list
);
500 if (rx_a
->start
> rx_b
->start
)
502 if (rx_a
->start
< rx_b
->start
)
507 static int spi_check_rx_ranges(struct spi_device
*spi
,
508 struct spi_message
*msg
,
511 struct spi_transfer
*xfer
;
512 struct rx_ranges ranges
[SPI_TEST_MAX_TRANSFERS
], *r
;
514 LIST_HEAD(ranges_list
);
518 /* loop over all transfers to fill in the rx_ranges */
519 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
520 /* if there is no rx, then no check is needed */
523 /* fill in the rx_range */
524 if (RANGE_CHECK(xfer
->rx_buf
, xfer
->len
,
525 rx
, SPI_TEST_MAX_SIZE_PLUS
)) {
526 ranges
[i
].start
= xfer
->rx_buf
;
527 ranges
[i
].end
= xfer
->rx_buf
+ xfer
->len
;
528 list_add(&ranges
[i
].list
, &ranges_list
);
533 /* if no ranges, then we can return and avoid the checks...*/
538 list_sort(NULL
, &ranges_list
, rx_ranges_cmp
);
540 /* and iterate over all the rx addresses */
541 for (addr
= rx
; addr
< (u8
*)rx
+ SPI_TEST_MAX_SIZE_PLUS
; addr
++) {
542 /* if we are the DO not write pattern,
543 * then continue with the loop...
545 if (*addr
== SPI_TEST_PATTERN_DO_NOT_WRITE
)
548 /* check if we are inside a range */
549 list_for_each_entry(r
, &ranges_list
, list
) {
550 /* if so then set to end... */
551 if ((addr
>= r
->start
) && (addr
< r
->end
))
554 /* second test after a (hopefull) translation */
555 if (*addr
== SPI_TEST_PATTERN_DO_NOT_WRITE
)
558 /* if still not found then something has modified too much */
559 /* we could list the "closest" transfer here... */
561 "loopback strangeness - rx changed outside of allowed range at: %pK\n",
563 /* do not return, only set ret,
564 * so that we list all addresses
572 static int spi_test_check_elapsed_time(struct spi_device
*spi
,
573 struct spi_test
*test
)
576 unsigned long long estimated_time
= 0;
577 unsigned long long delay_usecs
= 0;
579 for (i
= 0; i
< test
->transfer_count
; i
++) {
580 struct spi_transfer
*xfer
= test
->transfers
+ i
;
581 unsigned long long nbits
= (unsigned long long)BITS_PER_BYTE
*
584 delay_usecs
+= xfer
->delay
.value
;
587 estimated_time
+= div_u64(nbits
* NSEC_PER_SEC
, xfer
->speed_hz
);
590 estimated_time
+= delay_usecs
* NSEC_PER_USEC
;
591 if (test
->elapsed_time
< estimated_time
) {
593 "elapsed time %lld ns is shorter than minimum estimated time %lld ns\n",
594 test
->elapsed_time
, estimated_time
);
602 static int spi_test_check_loopback_result(struct spi_device
*spi
,
603 struct spi_message
*msg
,
606 struct spi_transfer
*xfer
;
611 /* checks rx_buffer pattern are valid with loopback or without */
613 ret
= spi_check_rx_ranges(spi
, msg
, rx
);
618 /* if we run without loopback, then return now */
622 /* if applicable to transfer check that rx_buf is equal to tx_buf */
623 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
624 /* if there is no rx, then no check is needed */
625 if (!xfer
->len
|| !xfer
->rx_buf
)
627 /* so depending on tx_buf we need to handle things */
629 for (i
= 0; i
< xfer
->len
; i
++) {
630 txb
= ((u8
*)xfer
->tx_buf
)[i
];
631 rxb
= ((u8
*)xfer
->rx_buf
)[i
];
636 /* first byte received */
637 txb
= ((u8
*)xfer
->rx_buf
)[0];
638 /* first byte may be 0 or xff */
639 if (!((txb
== 0) || (txb
== 0xff))) {
641 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n",
645 /* check that all bytes are identical */
646 for (i
= 1; i
< xfer
->len
; i
++) {
647 rxb
= ((u8
*)xfer
->rx_buf
)[i
];
658 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n",
664 static int spi_test_translate(struct spi_device
*spi
,
665 void **ptr
, size_t len
,
674 /* in the MAX_SIZE_HALF case modify the pointer */
675 if (((size_t)*ptr
) & SPI_TEST_MAX_SIZE_HALF
)
676 /* move the pointer to the correct range */
677 *ptr
+= (SPI_TEST_MAX_SIZE_PLUS
/ 2) -
678 SPI_TEST_MAX_SIZE_HALF
;
681 * - we check against MAX_SIZE_PLUS to allow for automated alignment
683 if (RANGE_CHECK(*ptr
, len
, RX(0), SPI_TEST_MAX_SIZE_PLUS
)) {
691 if (RANGE_CHECK(*ptr
, len
, TX(0), SPI_TEST_MAX_SIZE_PLUS
)) {
699 "PointerRange [%pK:%pK[ not in range [%pK:%pK[ or [%pK:%pK[\n",
701 RX(0), RX(SPI_TEST_MAX_SIZE
),
702 TX(0), TX(SPI_TEST_MAX_SIZE
));
707 static int spi_test_fill_pattern(struct spi_device
*spi
,
708 struct spi_test
*test
)
710 struct spi_transfer
*xfers
= test
->transfers
;
716 #define GET_VALUE_BYTE(value, index, bytes) \
717 (value >> (8 * (bytes - 1 - count % bytes)))
719 #define GET_VALUE_BYTE(value, index, bytes) \
720 (value >> (8 * (count % bytes)))
723 /* fill all transfers with the pattern requested */
724 for (i
= 0; i
< test
->transfer_count
; i
++) {
725 /* fill rx_buf with SPI_TEST_PATTERN_UNWRITTEN */
727 memset(xfers
[i
].rx_buf
, SPI_TEST_PATTERN_UNWRITTEN
,
729 /* if tx_buf is NULL then skip */
730 tx_buf
= (u8
*)xfers
[i
].tx_buf
;
733 /* modify all the transfers */
734 for (j
= 0; j
< xfers
[i
].len
; j
++, tx_buf
++, count
++) {
736 switch (test
->fill_option
) {
738 *tx_buf
= test
->fill_pattern
;
741 *tx_buf
= GET_VALUE_BYTE(test
->fill_pattern
,
745 *tx_buf
= GET_VALUE_BYTE(test
->fill_pattern
,
749 *tx_buf
= GET_VALUE_BYTE(test
->fill_pattern
,
756 *tx_buf
= GET_VALUE_BYTE(count
, count
, 2);
759 *tx_buf
= GET_VALUE_BYTE(count
, count
, 3);
762 *tx_buf
= GET_VALUE_BYTE(count
, count
, 4);
764 case FILL_TRANSFER_BYTE_8
:
767 case FILL_TRANSFER_BYTE_16
:
768 *tx_buf
= GET_VALUE_BYTE(j
, j
, 2);
770 case FILL_TRANSFER_BYTE_24
:
771 *tx_buf
= GET_VALUE_BYTE(j
, j
, 3);
773 case FILL_TRANSFER_BYTE_32
:
774 *tx_buf
= GET_VALUE_BYTE(j
, j
, 4);
776 case FILL_TRANSFER_NUM
:
781 "unsupported fill_option: %i\n",
791 static int _spi_test_run_iter(struct spi_device
*spi
,
792 struct spi_test
*test
,
795 struct spi_message
*msg
= &test
->msg
;
796 struct spi_transfer
*x
;
799 /* initialize message - zero-filled via static initialization */
800 spi_message_init_no_memset(msg
);
802 /* fill rx with the DO_NOT_WRITE pattern */
803 memset(rx
, SPI_TEST_PATTERN_DO_NOT_WRITE
, SPI_TEST_MAX_SIZE_PLUS
);
805 /* add the individual transfers */
806 for (i
= 0; i
< test
->transfer_count
; i
++) {
807 x
= &test
->transfers
[i
];
809 /* patch the values of tx_buf */
810 ret
= spi_test_translate(spi
, (void **)&x
->tx_buf
, x
->len
,
815 /* patch the values of rx_buf */
816 ret
= spi_test_translate(spi
, &x
->rx_buf
, x
->len
,
821 /* and add it to the list */
822 spi_message_add_tail(x
, msg
);
825 /* fill in the transfer buffers with pattern */
826 ret
= spi_test_fill_pattern(spi
, test
);
831 if (test
->execute_msg
)
832 ret
= test
->execute_msg(spi
, test
, tx
, rx
);
834 ret
= spi_test_execute_msg(spi
, test
, tx
, rx
);
837 if (ret
== test
->expected_return
)
841 "test failed - test returned %i, but we expect %i\n",
842 ret
, test
->expected_return
);
847 /* if it is 0, as we expected something else,
848 * then return something special
853 static int spi_test_run_iter(struct spi_device
*spi
,
854 const struct spi_test
*testtemplate
,
861 struct spi_test test
;
862 int i
, tx_count
, rx_count
;
864 /* copy the test template to test */
865 memcpy(&test
, testtemplate
, sizeof(test
));
867 /* if iterate_transfer_mask is not set,
868 * then set it to first transfer only
870 if (!(test
.iterate_transfer_mask
& (BIT(test
.transfer_count
) - 1)))
871 test
.iterate_transfer_mask
= 1;
873 /* count number of transfers with tx/rx_buf != NULL */
874 rx_count
= tx_count
= 0;
875 for (i
= 0; i
< test
.transfer_count
; i
++) {
876 if (test
.transfers
[i
].tx_buf
)
878 if (test
.transfers
[i
].rx_buf
)
882 /* in some iteration cases warn and exit early,
883 * as there is nothing to do, that has not been tested already...
885 if (tx_off
&& (!tx_count
)) {
886 dev_warn_once(&spi
->dev
,
887 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n",
891 if (rx_off
&& (!rx_count
)) {
892 dev_warn_once(&spi
->dev
,
893 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n",
899 if (!(len
|| tx_off
|| rx_off
)) {
900 dev_info(&spi
->dev
, "Running test %s\n", test
.description
);
903 " with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n",
904 len
, tx_off
, rx_off
);
907 /* update in the values from iteration values */
908 for (i
= 0; i
< test
.transfer_count
; i
++) {
909 /* only when bit in transfer mask is set */
910 if (!(test
.iterate_transfer_mask
& BIT(i
)))
912 test
.transfers
[i
].len
= len
;
913 if (test
.transfers
[i
].tx_buf
)
914 test
.transfers
[i
].tx_buf
+= tx_off
;
915 if (test
.transfers
[i
].rx_buf
)
916 test
.transfers
[i
].rx_buf
+= rx_off
;
920 return _spi_test_run_iter(spi
, &test
, tx
, rx
);
924 * spi_test_execute_msg - default implementation to run a test
926 * @spi: @spi_device on which to run the @spi_message
927 * @test: the test to execute, which already contains @msg
928 * @tx: the tx buffer allocated for the test sequence
929 * @rx: the rx buffer allocated for the test sequence
931 * Returns: error code of spi_sync as well as basic error checking
933 int spi_test_execute_msg(struct spi_device
*spi
, struct spi_test
*test
,
936 struct spi_message
*msg
= &test
->msg
;
940 /* only if we do not simulate */
941 if (!simulate_only
) {
944 /* dump the complete message before and after the transfer */
945 if (dump_messages
== 3)
946 spi_test_dump_message(spi
, msg
, true);
949 /* run spi message */
950 ret
= spi_sync(spi
, msg
);
951 test
->elapsed_time
= ktime_to_ns(ktime_sub(ktime_get(), start
));
952 if (ret
== -ETIMEDOUT
) {
954 "spi-message timed out - rerunning...\n");
955 /* rerun after a few explicit schedules */
956 for (i
= 0; i
< 16; i
++)
958 ret
= spi_sync(spi
, msg
);
962 "Failed to execute spi_message: %i\n",
967 /* do some extra error checks */
968 if (msg
->frame_length
!= msg
->actual_length
) {
970 "actual length differs from expected\n");
975 /* run rx-buffer tests */
976 ret
= spi_test_check_loopback_result(spi
, msg
, tx
, rx
);
980 ret
= spi_test_check_elapsed_time(spi
, test
);
983 /* if requested or on error dump message (including data) */
985 if (dump_messages
|| ret
)
986 spi_test_dump_message(spi
, msg
,
987 (dump_messages
>= 2) || (ret
));
991 EXPORT_SYMBOL_GPL(spi_test_execute_msg
);
994 * spi_test_run_test - run an individual spi_test
995 * including all the relevant iterations on:
996 * length and buffer alignment
998 * @spi: the spi_device to send the messages to
999 * @test: the test which we need to execute
1000 * @tx: the tx buffer allocated for the test sequence
1001 * @rx: the rx buffer allocated for the test sequence
1003 * Returns: status code of spi_sync or other failures
1006 int spi_test_run_test(struct spi_device
*spi
, const struct spi_test
*test
,
1011 size_t tx_align
, rx_align
;
1014 /* test for transfer limits */
1015 if (test
->transfer_count
>= SPI_TEST_MAX_TRANSFERS
) {
1017 "%s: Exceeded max number of transfers with %i\n",
1018 test
->description
, test
->transfer_count
);
1022 /* setting up some values in spi_message
1023 * based on some settings in spi_master
1024 * some of this can also get done in the run() method
1027 /* iterate over all the iterable values using macros
1028 * (to make it a bit more readable...
1030 #define FOR_EACH_ALIGNMENT(var) \
1032 var < (test->iterate_##var ? \
1033 (spi->controller->dma_alignment ? \
1034 spi->controller->dma_alignment : \
1035 test->iterate_##var) : \
1039 for (idx_len
= 0; idx_len
< SPI_TEST_MAX_ITERATE
&&
1040 (len
= test
->iterate_len
[idx_len
]) != -1; idx_len
++) {
1041 if ((run_only_iter_len
> -1) && len
!= run_only_iter_len
)
1043 FOR_EACH_ALIGNMENT(tx_align
) {
1044 FOR_EACH_ALIGNMENT(rx_align
) {
1045 /* and run the iteration */
1046 ret
= spi_test_run_iter(spi
, test
,
1059 EXPORT_SYMBOL_GPL(spi_test_run_test
);
1062 * spi_test_run_tests - run an array of spi_messages tests
1063 * @spi: the spi device on which to run the tests
1064 * @tests: NULL-terminated array of @spi_test
1066 * Returns: status errors as per @spi_test_run_test()
1069 int spi_test_run_tests(struct spi_device
*spi
,
1070 struct spi_test
*tests
)
1072 char *rx
= NULL
, *tx
= NULL
;
1073 int ret
= 0, count
= 0;
1074 struct spi_test
*test
;
1076 /* allocate rx/tx buffers of 128kB size without devm
1077 * in the hope that is on a page boundary
1080 rx
= vmalloc(SPI_TEST_MAX_SIZE_PLUS
);
1082 rx
= kzalloc(SPI_TEST_MAX_SIZE_PLUS
, GFP_KERNEL
);
1088 tx
= vmalloc(SPI_TEST_MAX_SIZE_PLUS
);
1090 tx
= kzalloc(SPI_TEST_MAX_SIZE_PLUS
, GFP_KERNEL
);
1096 /* now run the individual tests in the table */
1097 for (test
= tests
, count
= 0; test
->description
[0];
1099 /* only run test if requested */
1100 if ((run_only_test
> -1) && (count
!= run_only_test
))
1102 /* run custom implementation */
1104 ret
= test
->run_test(spi
, test
, tx
, rx
);
1106 ret
= spi_test_run_test(spi
, test
, tx
, rx
);
1109 /* add some delays so that we can easily
1110 * detect the individual tests when using a logic analyzer
1111 * we also add scheduling to avoid potential spi_timeouts...
1124 EXPORT_SYMBOL_GPL(spi_test_run_tests
);