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/module.h>
18 #include <linux/of_device.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 only a specific test */
57 static int run_only_test
= -1;
58 module_param(run_only_test
, int, 0);
59 MODULE_PARM_DESC(run_only_test
,
60 "only run the test with this number (0-based !)");
62 /* use vmalloc'ed buffers */
63 static int use_vmalloc
;
64 module_param(use_vmalloc
, int, 0644);
65 MODULE_PARM_DESC(use_vmalloc
,
66 "use vmalloc'ed buffers instead of kmalloc'ed");
69 static int check_ranges
= 1;
70 module_param(check_ranges
, int, 0644);
71 MODULE_PARM_DESC(check_ranges
,
72 "checks rx_buffer pattern are valid");
74 /* the actual tests to execute */
75 static struct spi_test spi_tests
[] = {
77 .description
= "tx/rx-transfer - start of page",
78 .fill_option
= FILL_COUNT_8
,
79 .iterate_len
= { ITERATE_MAX_LEN
},
80 .iterate_tx_align
= ITERATE_ALIGN
,
81 .iterate_rx_align
= ITERATE_ALIGN
,
91 .description
= "tx/rx-transfer - crossing PAGE_SIZE",
92 .fill_option
= FILL_COUNT_8
,
93 .iterate_len
= { ITERATE_MAX_LEN
},
94 .iterate_tx_align
= ITERATE_ALIGN
,
95 .iterate_rx_align
= ITERATE_ALIGN
,
99 .tx_buf
= TX(PAGE_SIZE
- 4),
100 .rx_buf
= RX(PAGE_SIZE
- 4),
105 .description
= "tx-transfer - only",
106 .fill_option
= FILL_COUNT_8
,
107 .iterate_len
= { ITERATE_MAX_LEN
},
108 .iterate_tx_align
= ITERATE_ALIGN
,
117 .description
= "rx-transfer - only",
118 .fill_option
= FILL_COUNT_8
,
119 .iterate_len
= { ITERATE_MAX_LEN
},
120 .iterate_rx_align
= ITERATE_ALIGN
,
129 .description
= "two tx-transfers - alter both",
130 .fill_option
= FILL_COUNT_8
,
131 .iterate_len
= { ITERATE_LEN
},
132 .iterate_tx_align
= ITERATE_ALIGN
,
133 .iterate_transfer_mask
= BIT(0) | BIT(1),
140 /* this is why we cant use ITERATE_MAX_LEN */
141 .tx_buf
= TX(SPI_TEST_MAX_SIZE_HALF
),
146 .description
= "two tx-transfers - alter first",
147 .fill_option
= FILL_COUNT_8
,
148 .iterate_len
= { ITERATE_MAX_LEN
},
149 .iterate_tx_align
= ITERATE_ALIGN
,
150 .iterate_transfer_mask
= BIT(0),
163 .description
= "two tx-transfers - alter second",
164 .fill_option
= FILL_COUNT_8
,
165 .iterate_len
= { ITERATE_MAX_LEN
},
166 .iterate_tx_align
= ITERATE_ALIGN
,
167 .iterate_transfer_mask
= BIT(1),
180 .description
= "two transfers tx then rx - alter both",
181 .fill_option
= FILL_COUNT_8
,
182 .iterate_len
= { ITERATE_MAX_LEN
},
183 .iterate_tx_align
= ITERATE_ALIGN
,
184 .iterate_transfer_mask
= BIT(0) | BIT(1),
196 .description
= "two transfers tx then rx - alter tx",
197 .fill_option
= FILL_COUNT_8
,
198 .iterate_len
= { ITERATE_MAX_LEN
},
199 .iterate_tx_align
= ITERATE_ALIGN
,
200 .iterate_transfer_mask
= BIT(0),
213 .description
= "two transfers tx then rx - alter rx",
214 .fill_option
= FILL_COUNT_8
,
215 .iterate_len
= { ITERATE_MAX_LEN
},
216 .iterate_tx_align
= ITERATE_ALIGN
,
217 .iterate_transfer_mask
= BIT(1),
230 .description
= "two tx+rx transfers - alter both",
231 .fill_option
= FILL_COUNT_8
,
232 .iterate_len
= { ITERATE_LEN
},
233 .iterate_tx_align
= ITERATE_ALIGN
,
234 .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 .description
= "two tx+rx transfers - delay after transfer",
293 .fill_option
= FILL_COUNT_8
,
294 .iterate_len
= { ITERATE_MAX_LEN
},
295 .iterate_transfer_mask
= BIT(0) | BIT(1),
303 .unit
= SPI_DELAY_UNIT_USECS
,
311 .unit
= SPI_DELAY_UNIT_USECS
,
317 { /* end of tests sequence */ }
320 static int spi_loopback_test_probe(struct spi_device
*spi
)
324 if (loop_req
|| no_cs
) {
325 spi
->mode
|= loop_req
? SPI_LOOP
: 0;
326 spi
->mode
|= no_cs
? SPI_NO_CS
: 0;
327 ret
= spi_setup(spi
);
329 dev_err(&spi
->dev
, "SPI setup with SPI_LOOP or SPI_NO_CS failed (%d)\n",
335 dev_info(&spi
->dev
, "Executing spi-loopback-tests\n");
337 ret
= spi_test_run_tests(spi
, spi_tests
);
339 dev_info(&spi
->dev
, "Finished spi-loopback-tests with return: %i\n",
345 /* non const match table to permit to change via a module parameter */
346 static struct of_device_id spi_loopback_test_of_match
[] = {
347 { .compatible
= "linux,spi-loopback-test", },
351 /* allow to override the compatible string via a module_parameter */
352 module_param_string(compatible
, spi_loopback_test_of_match
[0].compatible
,
353 sizeof(spi_loopback_test_of_match
[0].compatible
),
356 MODULE_DEVICE_TABLE(of
, spi_loopback_test_of_match
);
358 static struct spi_driver spi_loopback_test_driver
= {
360 .name
= "spi-loopback-test",
361 .owner
= THIS_MODULE
,
362 .of_match_table
= spi_loopback_test_of_match
,
364 .probe
= spi_loopback_test_probe
,
367 module_spi_driver(spi_loopback_test_driver
);
369 MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
370 MODULE_DESCRIPTION("test spi_driver to check core functionality");
371 MODULE_LICENSE("GPL");
373 /*-------------------------------------------------------------------------*/
375 /* spi_test implementation */
377 #define RANGE_CHECK(ptr, plen, start, slen) \
378 ((ptr >= start) && (ptr + plen <= start + slen))
380 /* we allocate one page more, to allow for offsets */
381 #define SPI_TEST_MAX_SIZE_PLUS (SPI_TEST_MAX_SIZE + PAGE_SIZE)
383 static void spi_test_print_hex_dump(char *pre
, const void *ptr
, size_t len
)
385 /* limit the hex_dump */
387 print_hex_dump(KERN_INFO
, pre
,
388 DUMP_PREFIX_OFFSET
, 16, 1,
393 print_hex_dump(KERN_INFO
, pre
,
394 DUMP_PREFIX_OFFSET
, 16, 1,
397 pr_info("%s truncated - continuing at offset %04zx\n",
399 print_hex_dump(KERN_INFO
, pre
,
400 DUMP_PREFIX_OFFSET
, 16, 1,
401 ptr
+ (len
- 512), 512, 0);
404 static void spi_test_dump_message(struct spi_device
*spi
,
405 struct spi_message
*msg
,
408 struct spi_transfer
*xfer
;
412 dev_info(&spi
->dev
, " spi_msg@%pK\n", msg
);
414 dev_info(&spi
->dev
, " status: %i\n",
416 dev_info(&spi
->dev
, " frame_length: %i\n",
418 dev_info(&spi
->dev
, " actual_length: %i\n",
421 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
422 dev_info(&spi
->dev
, " spi_transfer@%pK\n", xfer
);
423 dev_info(&spi
->dev
, " len: %i\n", xfer
->len
);
424 dev_info(&spi
->dev
, " tx_buf: %pK\n", xfer
->tx_buf
);
425 if (dump_data
&& xfer
->tx_buf
)
426 spi_test_print_hex_dump(" TX: ",
430 dev_info(&spi
->dev
, " rx_buf: %pK\n", xfer
->rx_buf
);
431 if (dump_data
&& xfer
->rx_buf
)
432 spi_test_print_hex_dump(" RX: ",
435 /* check for unwritten test pattern on rx_buf */
437 for (i
= 0 ; i
< xfer
->len
; i
++) {
438 b
= ((u8
*)xfer
->rx_buf
)[xfer
->len
- 1 - i
];
439 if (b
!= SPI_TEST_PATTERN_UNWRITTEN
)
444 " rx_buf filled with %02x starts at offset: %i\n",
445 SPI_TEST_PATTERN_UNWRITTEN
,
452 struct list_head list
;
457 static int rx_ranges_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
459 struct rx_ranges
*rx_a
= list_entry(a
, struct rx_ranges
, list
);
460 struct rx_ranges
*rx_b
= list_entry(b
, struct rx_ranges
, list
);
462 if (rx_a
->start
> rx_b
->start
)
464 if (rx_a
->start
< rx_b
->start
)
469 static int spi_check_rx_ranges(struct spi_device
*spi
,
470 struct spi_message
*msg
,
473 struct spi_transfer
*xfer
;
474 struct rx_ranges ranges
[SPI_TEST_MAX_TRANSFERS
], *r
;
476 LIST_HEAD(ranges_list
);
480 /* loop over all transfers to fill in the rx_ranges */
481 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
482 /* if there is no rx, then no check is needed */
485 /* fill in the rx_range */
486 if (RANGE_CHECK(xfer
->rx_buf
, xfer
->len
,
487 rx
, SPI_TEST_MAX_SIZE_PLUS
)) {
488 ranges
[i
].start
= xfer
->rx_buf
;
489 ranges
[i
].end
= xfer
->rx_buf
+ xfer
->len
;
490 list_add(&ranges
[i
].list
, &ranges_list
);
495 /* if no ranges, then we can return and avoid the checks...*/
500 list_sort(NULL
, &ranges_list
, rx_ranges_cmp
);
502 /* and iterate over all the rx addresses */
503 for (addr
= rx
; addr
< (u8
*)rx
+ SPI_TEST_MAX_SIZE_PLUS
; addr
++) {
504 /* if we are the DO not write pattern,
505 * then continue with the loop...
507 if (*addr
== SPI_TEST_PATTERN_DO_NOT_WRITE
)
510 /* check if we are inside a range */
511 list_for_each_entry(r
, &ranges_list
, list
) {
512 /* if so then set to end... */
513 if ((addr
>= r
->start
) && (addr
< r
->end
))
516 /* second test after a (hopefull) translation */
517 if (*addr
== SPI_TEST_PATTERN_DO_NOT_WRITE
)
520 /* if still not found then something has modified too much */
521 /* we could list the "closest" transfer here... */
523 "loopback strangeness - rx changed outside of allowed range at: %pK\n",
525 /* do not return, only set ret,
526 * so that we list all addresses
534 static int spi_test_check_elapsed_time(struct spi_device
*spi
,
535 struct spi_test
*test
)
538 unsigned long long estimated_time
= 0;
539 unsigned long long delay_usecs
= 0;
541 for (i
= 0; i
< test
->transfer_count
; i
++) {
542 struct spi_transfer
*xfer
= test
->transfers
+ i
;
543 unsigned long long nbits
= (unsigned long long)BITS_PER_BYTE
*
546 delay_usecs
+= xfer
->delay
.value
;
549 estimated_time
+= div_u64(nbits
* NSEC_PER_SEC
, xfer
->speed_hz
);
552 estimated_time
+= delay_usecs
* NSEC_PER_USEC
;
553 if (test
->elapsed_time
< estimated_time
) {
555 "elapsed time %lld ns is shorter than minimum estimated time %lld ns\n",
556 test
->elapsed_time
, estimated_time
);
564 static int spi_test_check_loopback_result(struct spi_device
*spi
,
565 struct spi_message
*msg
,
568 struct spi_transfer
*xfer
;
573 /* checks rx_buffer pattern are valid with loopback or without */
575 ret
= spi_check_rx_ranges(spi
, msg
, rx
);
580 /* if we run without loopback, then return now */
584 /* if applicable to transfer check that rx_buf is equal to tx_buf */
585 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
586 /* if there is no rx, then no check is needed */
587 if (!xfer
->len
|| !xfer
->rx_buf
)
589 /* so depending on tx_buf we need to handle things */
591 for (i
= 0; i
< xfer
->len
; i
++) {
592 txb
= ((u8
*)xfer
->tx_buf
)[i
];
593 rxb
= ((u8
*)xfer
->rx_buf
)[i
];
598 /* first byte received */
599 txb
= ((u8
*)xfer
->rx_buf
)[0];
600 /* first byte may be 0 or xff */
601 if (!((txb
== 0) || (txb
== 0xff))) {
603 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n",
607 /* check that all bytes are identical */
608 for (i
= 1; i
< xfer
->len
; i
++) {
609 rxb
= ((u8
*)xfer
->rx_buf
)[i
];
620 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n",
626 static int spi_test_translate(struct spi_device
*spi
,
627 void **ptr
, size_t len
,
636 /* in the MAX_SIZE_HALF case modify the pointer */
637 if (((size_t)*ptr
) & SPI_TEST_MAX_SIZE_HALF
)
638 /* move the pointer to the correct range */
639 *ptr
+= (SPI_TEST_MAX_SIZE_PLUS
/ 2) -
640 SPI_TEST_MAX_SIZE_HALF
;
643 * - we check against MAX_SIZE_PLUS to allow for automated alignment
645 if (RANGE_CHECK(*ptr
, len
, RX(0), SPI_TEST_MAX_SIZE_PLUS
)) {
653 if (RANGE_CHECK(*ptr
, len
, TX(0), SPI_TEST_MAX_SIZE_PLUS
)) {
661 "PointerRange [%pK:%pK[ not in range [%pK:%pK[ or [%pK:%pK[\n",
663 RX(0), RX(SPI_TEST_MAX_SIZE
),
664 TX(0), TX(SPI_TEST_MAX_SIZE
));
669 static int spi_test_fill_pattern(struct spi_device
*spi
,
670 struct spi_test
*test
)
672 struct spi_transfer
*xfers
= test
->transfers
;
678 #define GET_VALUE_BYTE(value, index, bytes) \
679 (value >> (8 * (bytes - 1 - count % bytes)))
681 #define GET_VALUE_BYTE(value, index, bytes) \
682 (value >> (8 * (count % bytes)))
685 /* fill all transfers with the pattern requested */
686 for (i
= 0; i
< test
->transfer_count
; i
++) {
687 /* fill rx_buf with SPI_TEST_PATTERN_UNWRITTEN */
689 memset(xfers
[i
].rx_buf
, SPI_TEST_PATTERN_UNWRITTEN
,
691 /* if tx_buf is NULL then skip */
692 tx_buf
= (u8
*)xfers
[i
].tx_buf
;
695 /* modify all the transfers */
696 for (j
= 0; j
< xfers
[i
].len
; j
++, tx_buf
++, count
++) {
698 switch (test
->fill_option
) {
700 *tx_buf
= test
->fill_pattern
;
703 *tx_buf
= GET_VALUE_BYTE(test
->fill_pattern
,
707 *tx_buf
= GET_VALUE_BYTE(test
->fill_pattern
,
711 *tx_buf
= GET_VALUE_BYTE(test
->fill_pattern
,
718 *tx_buf
= GET_VALUE_BYTE(count
, count
, 2);
721 *tx_buf
= GET_VALUE_BYTE(count
, count
, 3);
724 *tx_buf
= GET_VALUE_BYTE(count
, count
, 4);
726 case FILL_TRANSFER_BYTE_8
:
729 case FILL_TRANSFER_BYTE_16
:
730 *tx_buf
= GET_VALUE_BYTE(j
, j
, 2);
732 case FILL_TRANSFER_BYTE_24
:
733 *tx_buf
= GET_VALUE_BYTE(j
, j
, 3);
735 case FILL_TRANSFER_BYTE_32
:
736 *tx_buf
= GET_VALUE_BYTE(j
, j
, 4);
738 case FILL_TRANSFER_NUM
:
743 "unsupported fill_option: %i\n",
753 static int _spi_test_run_iter(struct spi_device
*spi
,
754 struct spi_test
*test
,
757 struct spi_message
*msg
= &test
->msg
;
758 struct spi_transfer
*x
;
761 /* initialize message - zero-filled via static initialization */
762 spi_message_init_no_memset(msg
);
764 /* fill rx with the DO_NOT_WRITE pattern */
765 memset(rx
, SPI_TEST_PATTERN_DO_NOT_WRITE
, SPI_TEST_MAX_SIZE_PLUS
);
767 /* add the individual transfers */
768 for (i
= 0; i
< test
->transfer_count
; i
++) {
769 x
= &test
->transfers
[i
];
771 /* patch the values of tx_buf */
772 ret
= spi_test_translate(spi
, (void **)&x
->tx_buf
, x
->len
,
777 /* patch the values of rx_buf */
778 ret
= spi_test_translate(spi
, &x
->rx_buf
, x
->len
,
783 /* and add it to the list */
784 spi_message_add_tail(x
, msg
);
787 /* fill in the transfer buffers with pattern */
788 ret
= spi_test_fill_pattern(spi
, test
);
793 if (test
->execute_msg
)
794 ret
= test
->execute_msg(spi
, test
, tx
, rx
);
796 ret
= spi_test_execute_msg(spi
, test
, tx
, rx
);
799 if (ret
== test
->expected_return
)
803 "test failed - test returned %i, but we expect %i\n",
804 ret
, test
->expected_return
);
809 /* if it is 0, as we expected something else,
810 * then return something special
815 static int spi_test_run_iter(struct spi_device
*spi
,
816 const struct spi_test
*testtemplate
,
823 struct spi_test test
;
824 int i
, tx_count
, rx_count
;
826 /* copy the test template to test */
827 memcpy(&test
, testtemplate
, sizeof(test
));
829 /* if iterate_transfer_mask is not set,
830 * then set it to first transfer only
832 if (!(test
.iterate_transfer_mask
& (BIT(test
.transfer_count
) - 1)))
833 test
.iterate_transfer_mask
= 1;
835 /* count number of transfers with tx/rx_buf != NULL */
836 rx_count
= tx_count
= 0;
837 for (i
= 0; i
< test
.transfer_count
; i
++) {
838 if (test
.transfers
[i
].tx_buf
)
840 if (test
.transfers
[i
].rx_buf
)
844 /* in some iteration cases warn and exit early,
845 * as there is nothing to do, that has not been tested already...
847 if (tx_off
&& (!tx_count
)) {
848 dev_warn_once(&spi
->dev
,
849 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n",
853 if (rx_off
&& (!rx_count
)) {
854 dev_warn_once(&spi
->dev
,
855 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n",
861 if (!(len
|| tx_off
|| rx_off
)) {
862 dev_info(&spi
->dev
, "Running test %s\n", test
.description
);
865 " with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n",
866 len
, tx_off
, rx_off
);
869 /* update in the values from iteration values */
870 for (i
= 0; i
< test
.transfer_count
; i
++) {
871 /* only when bit in transfer mask is set */
872 if (!(test
.iterate_transfer_mask
& BIT(i
)))
874 test
.transfers
[i
].len
= len
;
875 if (test
.transfers
[i
].tx_buf
)
876 test
.transfers
[i
].tx_buf
+= tx_off
;
877 if (test
.transfers
[i
].tx_buf
)
878 test
.transfers
[i
].rx_buf
+= rx_off
;
882 return _spi_test_run_iter(spi
, &test
, tx
, rx
);
886 * spi_test_execute_msg - default implementation to run a test
888 * spi: @spi_device on which to run the @spi_message
889 * test: the test to execute, which already contains @msg
890 * tx: the tx buffer allocated for the test sequence
891 * rx: the rx buffer allocated for the test sequence
893 * Returns: error code of spi_sync as well as basic error checking
895 int spi_test_execute_msg(struct spi_device
*spi
, struct spi_test
*test
,
898 struct spi_message
*msg
= &test
->msg
;
902 /* only if we do not simulate */
903 if (!simulate_only
) {
906 /* dump the complete message before and after the transfer */
907 if (dump_messages
== 3)
908 spi_test_dump_message(spi
, msg
, true);
911 /* run spi message */
912 ret
= spi_sync(spi
, msg
);
913 test
->elapsed_time
= ktime_to_ns(ktime_sub(ktime_get(), start
));
914 if (ret
== -ETIMEDOUT
) {
916 "spi-message timed out - rerunning...\n");
917 /* rerun after a few explicit schedules */
918 for (i
= 0; i
< 16; i
++)
920 ret
= spi_sync(spi
, msg
);
924 "Failed to execute spi_message: %i\n",
929 /* do some extra error checks */
930 if (msg
->frame_length
!= msg
->actual_length
) {
932 "actual length differs from expected\n");
937 /* run rx-buffer tests */
938 ret
= spi_test_check_loopback_result(spi
, msg
, tx
, rx
);
942 ret
= spi_test_check_elapsed_time(spi
, test
);
945 /* if requested or on error dump message (including data) */
947 if (dump_messages
|| ret
)
948 spi_test_dump_message(spi
, msg
,
949 (dump_messages
>= 2) || (ret
));
953 EXPORT_SYMBOL_GPL(spi_test_execute_msg
);
956 * spi_test_run_test - run an individual spi_test
957 * including all the relevant iterations on:
958 * length and buffer alignment
960 * spi: the spi_device to send the messages to
961 * test: the test which we need to execute
962 * tx: the tx buffer allocated for the test sequence
963 * rx: the rx buffer allocated for the test sequence
965 * Returns: status code of spi_sync or other failures
968 int spi_test_run_test(struct spi_device
*spi
, const struct spi_test
*test
,
973 size_t tx_align
, rx_align
;
976 /* test for transfer limits */
977 if (test
->transfer_count
>= SPI_TEST_MAX_TRANSFERS
) {
979 "%s: Exceeded max number of transfers with %i\n",
980 test
->description
, test
->transfer_count
);
984 /* setting up some values in spi_message
985 * based on some settings in spi_master
986 * some of this can also get done in the run() method
989 /* iterate over all the iterable values using macros
990 * (to make it a bit more readable...
992 #define FOR_EACH_ALIGNMENT(var) \
994 var < (test->iterate_##var ? \
995 (spi->master->dma_alignment ? \
996 spi->master->dma_alignment : \
997 test->iterate_##var) : \
1001 for (idx_len
= 0; idx_len
< SPI_TEST_MAX_ITERATE
&&
1002 (len
= test
->iterate_len
[idx_len
]) != -1; idx_len
++) {
1003 FOR_EACH_ALIGNMENT(tx_align
) {
1004 FOR_EACH_ALIGNMENT(rx_align
) {
1005 /* and run the iteration */
1006 ret
= spi_test_run_iter(spi
, test
,
1019 EXPORT_SYMBOL_GPL(spi_test_run_test
);
1022 * spi_test_run_tests - run an array of spi_messages tests
1023 * @spi: the spi device on which to run the tests
1024 * @tests: NULL-terminated array of @spi_test
1026 * Returns: status errors as per @spi_test_run_test()
1029 int spi_test_run_tests(struct spi_device
*spi
,
1030 struct spi_test
*tests
)
1032 char *rx
= NULL
, *tx
= NULL
;
1033 int ret
= 0, count
= 0;
1034 struct spi_test
*test
;
1036 /* allocate rx/tx buffers of 128kB size without devm
1037 * in the hope that is on a page boundary
1040 rx
= vmalloc(SPI_TEST_MAX_SIZE_PLUS
);
1042 rx
= kzalloc(SPI_TEST_MAX_SIZE_PLUS
, GFP_KERNEL
);
1048 tx
= vmalloc(SPI_TEST_MAX_SIZE_PLUS
);
1050 tx
= kzalloc(SPI_TEST_MAX_SIZE_PLUS
, GFP_KERNEL
);
1056 /* now run the individual tests in the table */
1057 for (test
= tests
, count
= 0; test
->description
[0];
1059 /* only run test if requested */
1060 if ((run_only_test
> -1) && (count
!= run_only_test
))
1062 /* run custom implementation */
1064 ret
= test
->run_test(spi
, test
, tx
, rx
);
1066 ret
= spi_test_run_test(spi
, test
, tx
, rx
);
1069 /* add some delays so that we can easily
1070 * detect the individual tests when using a logic analyzer
1071 * we also add scheduling to avoid potential spi_timeouts...
1083 EXPORT_SYMBOL_GPL(spi_test_run_tests
);