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/ktime.h>
24 #include <linux/list.h>
25 #include <linux/list_sort.h>
26 #include <linux/module.h>
27 #include <linux/of_device.h>
28 #include <linux/printk.h>
29 #include <linux/vmalloc.h>
30 #include <linux/spi/spi.h>
34 /* flag to only simulate transfers */
35 static int simulate_only
;
36 module_param(simulate_only
, int, 0);
37 MODULE_PARM_DESC(simulate_only
, "if not 0 do not execute the spi message");
39 /* dump spi messages */
40 static int dump_messages
;
41 module_param(dump_messages
, int, 0);
42 MODULE_PARM_DESC(dump_messages
,
43 "=1 dump the basic spi_message_structure, " \
44 "=2 dump the spi_message_structure including data, " \
45 "=3 dump the spi_message structure before and after execution");
46 /* the device is jumpered for loopback - enabling some rx_buf tests */
48 module_param(loopback
, int, 0);
49 MODULE_PARM_DESC(loopback
,
50 "if set enable loopback mode, where the rx_buf " \
51 "is checked to match tx_buf after the spi_message " \
55 module_param(loop_req
, int, 0);
56 MODULE_PARM_DESC(loop_req
,
57 "if set controller will be asked to enable test loop mode. " \
58 "If controller supported it, MISO and MOSI will be connected");
61 module_param(no_cs
, int, 0);
62 MODULE_PARM_DESC(no_cs
,
63 "if set Chip Select (CS) will not be used");
65 /* run only a specific test */
66 static int run_only_test
= -1;
67 module_param(run_only_test
, int, 0);
68 MODULE_PARM_DESC(run_only_test
,
69 "only run the test with this number (0-based !)");
71 /* use vmalloc'ed buffers */
72 static int use_vmalloc
;
73 module_param(use_vmalloc
, int, 0644);
74 MODULE_PARM_DESC(use_vmalloc
,
75 "use vmalloc'ed buffers instead of kmalloc'ed");
78 static int check_ranges
= 1;
79 module_param(check_ranges
, int, 0644);
80 MODULE_PARM_DESC(check_ranges
,
81 "checks rx_buffer pattern are valid");
83 /* the actual tests to execute */
84 static struct spi_test spi_tests
[] = {
86 .description
= "tx/rx-transfer - start of page",
87 .fill_option
= FILL_COUNT_8
,
88 .iterate_len
= { ITERATE_MAX_LEN
},
89 .iterate_tx_align
= ITERATE_ALIGN
,
90 .iterate_rx_align
= ITERATE_ALIGN
,
100 .description
= "tx/rx-transfer - crossing PAGE_SIZE",
101 .fill_option
= FILL_COUNT_8
,
102 .iterate_len
= { ITERATE_MAX_LEN
},
103 .iterate_tx_align
= ITERATE_ALIGN
,
104 .iterate_rx_align
= ITERATE_ALIGN
,
108 .tx_buf
= TX(PAGE_SIZE
- 4),
109 .rx_buf
= RX(PAGE_SIZE
- 4),
114 .description
= "tx-transfer - only",
115 .fill_option
= FILL_COUNT_8
,
116 .iterate_len
= { ITERATE_MAX_LEN
},
117 .iterate_tx_align
= ITERATE_ALIGN
,
126 .description
= "rx-transfer - only",
127 .fill_option
= FILL_COUNT_8
,
128 .iterate_len
= { ITERATE_MAX_LEN
},
129 .iterate_rx_align
= ITERATE_ALIGN
,
138 .description
= "two tx-transfers - alter both",
139 .fill_option
= FILL_COUNT_8
,
140 .iterate_len
= { ITERATE_LEN
},
141 .iterate_tx_align
= ITERATE_ALIGN
,
142 .iterate_transfer_mask
= BIT(0) | BIT(1),
149 /* this is why we cant use ITERATE_MAX_LEN */
150 .tx_buf
= TX(SPI_TEST_MAX_SIZE_HALF
),
155 .description
= "two tx-transfers - alter first",
156 .fill_option
= FILL_COUNT_8
,
157 .iterate_len
= { ITERATE_MAX_LEN
},
158 .iterate_tx_align
= ITERATE_ALIGN
,
159 .iterate_transfer_mask
= BIT(0),
172 .description
= "two tx-transfers - alter second",
173 .fill_option
= FILL_COUNT_8
,
174 .iterate_len
= { ITERATE_MAX_LEN
},
175 .iterate_tx_align
= ITERATE_ALIGN
,
176 .iterate_transfer_mask
= BIT(1),
189 .description
= "two transfers tx then rx - alter both",
190 .fill_option
= FILL_COUNT_8
,
191 .iterate_len
= { ITERATE_MAX_LEN
},
192 .iterate_tx_align
= ITERATE_ALIGN
,
193 .iterate_transfer_mask
= BIT(0) | BIT(1),
205 .description
= "two transfers tx then rx - alter tx",
206 .fill_option
= FILL_COUNT_8
,
207 .iterate_len
= { ITERATE_MAX_LEN
},
208 .iterate_tx_align
= ITERATE_ALIGN
,
209 .iterate_transfer_mask
= BIT(0),
222 .description
= "two transfers tx then rx - alter rx",
223 .fill_option
= FILL_COUNT_8
,
224 .iterate_len
= { ITERATE_MAX_LEN
},
225 .iterate_tx_align
= ITERATE_ALIGN
,
226 .iterate_transfer_mask
= BIT(1),
239 .description
= "two tx+rx transfers - alter both",
240 .fill_option
= FILL_COUNT_8
,
241 .iterate_len
= { ITERATE_LEN
},
242 .iterate_tx_align
= ITERATE_ALIGN
,
243 .iterate_transfer_mask
= BIT(0) | BIT(1),
251 /* making sure we align without overwrite
252 * the reason we can not use ITERATE_MAX_LEN
254 .tx_buf
= TX(SPI_TEST_MAX_SIZE_HALF
),
255 .rx_buf
= RX(SPI_TEST_MAX_SIZE_HALF
),
260 .description
= "two tx+rx transfers - alter first",
261 .fill_option
= FILL_COUNT_8
,
262 .iterate_len
= { ITERATE_MAX_LEN
},
263 .iterate_tx_align
= ITERATE_ALIGN
,
264 .iterate_transfer_mask
= BIT(0),
268 /* making sure we align without overwrite */
274 /* making sure we align without overwrite */
281 .description
= "two tx+rx transfers - alter second",
282 .fill_option
= FILL_COUNT_8
,
283 .iterate_len
= { ITERATE_MAX_LEN
},
284 .iterate_tx_align
= ITERATE_ALIGN
,
285 .iterate_transfer_mask
= BIT(1),
294 /* making sure we align without overwrite */
301 .description
= "two tx+rx transfers - delay after transfer",
302 .fill_option
= FILL_COUNT_8
,
303 .iterate_len
= { ITERATE_MAX_LEN
},
304 .iterate_transfer_mask
= BIT(0) | BIT(1),
320 { /* end of tests sequence */ }
323 static int spi_loopback_test_probe(struct spi_device
*spi
)
327 if (loop_req
|| no_cs
) {
328 spi
->mode
|= loop_req
? SPI_LOOP
: 0;
329 spi
->mode
|= no_cs
? SPI_NO_CS
: 0;
330 ret
= spi_setup(spi
);
332 dev_err(&spi
->dev
, "SPI setup with SPI_LOOP or SPI_NO_CS failed (%d)\n",
338 dev_info(&spi
->dev
, "Executing spi-loopback-tests\n");
340 ret
= spi_test_run_tests(spi
, spi_tests
);
342 dev_info(&spi
->dev
, "Finished spi-loopback-tests with return: %i\n",
348 /* non const match table to permit to change via a module parameter */
349 static struct of_device_id spi_loopback_test_of_match
[] = {
350 { .compatible
= "linux,spi-loopback-test", },
354 /* allow to override the compatible string via a module_parameter */
355 module_param_string(compatible
, spi_loopback_test_of_match
[0].compatible
,
356 sizeof(spi_loopback_test_of_match
[0].compatible
),
359 MODULE_DEVICE_TABLE(of
, spi_loopback_test_of_match
);
361 static struct spi_driver spi_loopback_test_driver
= {
363 .name
= "spi-loopback-test",
364 .owner
= THIS_MODULE
,
365 .of_match_table
= spi_loopback_test_of_match
,
367 .probe
= spi_loopback_test_probe
,
370 module_spi_driver(spi_loopback_test_driver
);
372 MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
373 MODULE_DESCRIPTION("test spi_driver to check core functionality");
374 MODULE_LICENSE("GPL");
376 /*-------------------------------------------------------------------------*/
378 /* spi_test implementation */
380 #define RANGE_CHECK(ptr, plen, start, slen) \
381 ((ptr >= start) && (ptr + plen <= start + slen))
383 /* we allocate one page more, to allow for offsets */
384 #define SPI_TEST_MAX_SIZE_PLUS (SPI_TEST_MAX_SIZE + PAGE_SIZE)
386 static void spi_test_print_hex_dump(char *pre
, const void *ptr
, size_t len
)
388 /* limit the hex_dump */
390 print_hex_dump(KERN_INFO
, pre
,
391 DUMP_PREFIX_OFFSET
, 16, 1,
396 print_hex_dump(KERN_INFO
, pre
,
397 DUMP_PREFIX_OFFSET
, 16, 1,
400 pr_info("%s truncated - continuing at offset %04zx\n",
402 print_hex_dump(KERN_INFO
, pre
,
403 DUMP_PREFIX_OFFSET
, 16, 1,
404 ptr
+ (len
- 512), 512, 0);
407 static void spi_test_dump_message(struct spi_device
*spi
,
408 struct spi_message
*msg
,
411 struct spi_transfer
*xfer
;
415 dev_info(&spi
->dev
, " spi_msg@%pK\n", msg
);
417 dev_info(&spi
->dev
, " status: %i\n",
419 dev_info(&spi
->dev
, " frame_length: %i\n",
421 dev_info(&spi
->dev
, " actual_length: %i\n",
424 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
425 dev_info(&spi
->dev
, " spi_transfer@%pK\n", xfer
);
426 dev_info(&spi
->dev
, " len: %i\n", xfer
->len
);
427 dev_info(&spi
->dev
, " tx_buf: %pK\n", xfer
->tx_buf
);
428 if (dump_data
&& xfer
->tx_buf
)
429 spi_test_print_hex_dump(" TX: ",
433 dev_info(&spi
->dev
, " rx_buf: %pK\n", xfer
->rx_buf
);
434 if (dump_data
&& xfer
->rx_buf
)
435 spi_test_print_hex_dump(" RX: ",
438 /* check for unwritten test pattern on rx_buf */
440 for (i
= 0 ; i
< xfer
->len
; i
++) {
441 b
= ((u8
*)xfer
->rx_buf
)[xfer
->len
- 1 - i
];
442 if (b
!= SPI_TEST_PATTERN_UNWRITTEN
)
447 " rx_buf filled with %02x starts at offset: %i\n",
448 SPI_TEST_PATTERN_UNWRITTEN
,
455 struct list_head list
;
460 static int rx_ranges_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
462 struct rx_ranges
*rx_a
= list_entry(a
, struct rx_ranges
, list
);
463 struct rx_ranges
*rx_b
= list_entry(b
, struct rx_ranges
, list
);
465 if (rx_a
->start
> rx_b
->start
)
467 if (rx_a
->start
< rx_b
->start
)
472 static int spi_check_rx_ranges(struct spi_device
*spi
,
473 struct spi_message
*msg
,
476 struct spi_transfer
*xfer
;
477 struct rx_ranges ranges
[SPI_TEST_MAX_TRANSFERS
], *r
;
479 LIST_HEAD(ranges_list
);
483 /* loop over all transfers to fill in the rx_ranges */
484 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
485 /* if there is no rx, then no check is needed */
488 /* fill in the rx_range */
489 if (RANGE_CHECK(xfer
->rx_buf
, xfer
->len
,
490 rx
, SPI_TEST_MAX_SIZE_PLUS
)) {
491 ranges
[i
].start
= xfer
->rx_buf
;
492 ranges
[i
].end
= xfer
->rx_buf
+ xfer
->len
;
493 list_add(&ranges
[i
].list
, &ranges_list
);
498 /* if no ranges, then we can return and avoid the checks...*/
503 list_sort(NULL
, &ranges_list
, rx_ranges_cmp
);
505 /* and iterate over all the rx addresses */
506 for (addr
= rx
; addr
< (u8
*)rx
+ SPI_TEST_MAX_SIZE_PLUS
; addr
++) {
507 /* if we are the DO not write pattern,
508 * then continue with the loop...
510 if (*addr
== SPI_TEST_PATTERN_DO_NOT_WRITE
)
513 /* check if we are inside a range */
514 list_for_each_entry(r
, &ranges_list
, list
) {
515 /* if so then set to end... */
516 if ((addr
>= r
->start
) && (addr
< r
->end
))
519 /* second test after a (hopefull) translation */
520 if (*addr
== SPI_TEST_PATTERN_DO_NOT_WRITE
)
523 /* if still not found then something has modified too much */
524 /* we could list the "closest" transfer here... */
526 "loopback strangeness - rx changed outside of allowed range at: %pK\n",
528 /* do not return, only set ret,
529 * so that we list all addresses
537 static int spi_test_check_elapsed_time(struct spi_device
*spi
,
538 struct spi_test
*test
)
541 unsigned long long estimated_time
= 0;
542 unsigned long long delay_usecs
= 0;
544 for (i
= 0; i
< test
->transfer_count
; i
++) {
545 struct spi_transfer
*xfer
= test
->transfers
+ i
;
546 unsigned long long nbits
= (unsigned long long)BITS_PER_BYTE
*
549 delay_usecs
+= xfer
->delay_usecs
;
552 estimated_time
+= div_u64(nbits
* NSEC_PER_SEC
, xfer
->speed_hz
);
555 estimated_time
+= delay_usecs
* NSEC_PER_USEC
;
556 if (test
->elapsed_time
< estimated_time
) {
558 "elapsed time %lld ns is shorter than minimum estimated time %lld ns\n",
559 test
->elapsed_time
, estimated_time
);
567 static int spi_test_check_loopback_result(struct spi_device
*spi
,
568 struct spi_message
*msg
,
571 struct spi_transfer
*xfer
;
576 /* checks rx_buffer pattern are valid with loopback or without */
578 ret
= spi_check_rx_ranges(spi
, msg
, rx
);
583 /* if we run without loopback, then return now */
587 /* if applicable to transfer check that rx_buf is equal to tx_buf */
588 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
589 /* if there is no rx, then no check is needed */
590 if (!xfer
->len
|| !xfer
->rx_buf
)
592 /* so depending on tx_buf we need to handle things */
594 for (i
= 0; i
< xfer
->len
; i
++) {
595 txb
= ((u8
*)xfer
->tx_buf
)[i
];
596 rxb
= ((u8
*)xfer
->rx_buf
)[i
];
601 /* first byte received */
602 txb
= ((u8
*)xfer
->rx_buf
)[0];
603 /* first byte may be 0 or xff */
604 if (!((txb
== 0) || (txb
== 0xff))) {
606 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n",
610 /* check that all bytes are identical */
611 for (i
= 1; i
< xfer
->len
; i
++) {
612 rxb
= ((u8
*)xfer
->rx_buf
)[i
];
623 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n",
629 static int spi_test_translate(struct spi_device
*spi
,
630 void **ptr
, size_t len
,
639 /* in the MAX_SIZE_HALF case modify the pointer */
640 if (((size_t)*ptr
) & SPI_TEST_MAX_SIZE_HALF
)
641 /* move the pointer to the correct range */
642 *ptr
+= (SPI_TEST_MAX_SIZE_PLUS
/ 2) -
643 SPI_TEST_MAX_SIZE_HALF
;
646 * - we check against MAX_SIZE_PLUS to allow for automated alignment
648 if (RANGE_CHECK(*ptr
, len
, RX(0), SPI_TEST_MAX_SIZE_PLUS
)) {
656 if (RANGE_CHECK(*ptr
, len
, TX(0), SPI_TEST_MAX_SIZE_PLUS
)) {
664 "PointerRange [%pK:%pK[ not in range [%pK:%pK[ or [%pK:%pK[\n",
666 RX(0), RX(SPI_TEST_MAX_SIZE
),
667 TX(0), TX(SPI_TEST_MAX_SIZE
));
672 static int spi_test_fill_pattern(struct spi_device
*spi
,
673 struct spi_test
*test
)
675 struct spi_transfer
*xfers
= test
->transfers
;
681 #define GET_VALUE_BYTE(value, index, bytes) \
682 (value >> (8 * (bytes - 1 - count % bytes)))
684 #define GET_VALUE_BYTE(value, index, bytes) \
685 (value >> (8 * (count % bytes)))
688 /* fill all transfers with the pattern requested */
689 for (i
= 0; i
< test
->transfer_count
; i
++) {
690 /* fill rx_buf with SPI_TEST_PATTERN_UNWRITTEN */
692 memset(xfers
[i
].rx_buf
, SPI_TEST_PATTERN_UNWRITTEN
,
694 /* if tx_buf is NULL then skip */
695 tx_buf
= (u8
*)xfers
[i
].tx_buf
;
698 /* modify all the transfers */
699 for (j
= 0; j
< xfers
[i
].len
; j
++, tx_buf
++, count
++) {
701 switch (test
->fill_option
) {
703 *tx_buf
= test
->fill_pattern
;
706 *tx_buf
= GET_VALUE_BYTE(test
->fill_pattern
,
710 *tx_buf
= GET_VALUE_BYTE(test
->fill_pattern
,
714 *tx_buf
= GET_VALUE_BYTE(test
->fill_pattern
,
721 *tx_buf
= GET_VALUE_BYTE(count
, count
, 2);
724 *tx_buf
= GET_VALUE_BYTE(count
, count
, 3);
727 *tx_buf
= GET_VALUE_BYTE(count
, count
, 4);
729 case FILL_TRANSFER_BYTE_8
:
732 case FILL_TRANSFER_BYTE_16
:
733 *tx_buf
= GET_VALUE_BYTE(j
, j
, 2);
735 case FILL_TRANSFER_BYTE_24
:
736 *tx_buf
= GET_VALUE_BYTE(j
, j
, 3);
738 case FILL_TRANSFER_BYTE_32
:
739 *tx_buf
= GET_VALUE_BYTE(j
, j
, 4);
741 case FILL_TRANSFER_NUM
:
746 "unsupported fill_option: %i\n",
756 static int _spi_test_run_iter(struct spi_device
*spi
,
757 struct spi_test
*test
,
760 struct spi_message
*msg
= &test
->msg
;
761 struct spi_transfer
*x
;
764 /* initialize message - zero-filled via static initialization */
765 spi_message_init_no_memset(msg
);
767 /* fill rx with the DO_NOT_WRITE pattern */
768 memset(rx
, SPI_TEST_PATTERN_DO_NOT_WRITE
, SPI_TEST_MAX_SIZE_PLUS
);
770 /* add the individual transfers */
771 for (i
= 0; i
< test
->transfer_count
; i
++) {
772 x
= &test
->transfers
[i
];
774 /* patch the values of tx_buf */
775 ret
= spi_test_translate(spi
, (void **)&x
->tx_buf
, x
->len
,
780 /* patch the values of rx_buf */
781 ret
= spi_test_translate(spi
, &x
->rx_buf
, x
->len
,
786 /* and add it to the list */
787 spi_message_add_tail(x
, msg
);
790 /* fill in the transfer buffers with pattern */
791 ret
= spi_test_fill_pattern(spi
, test
);
796 if (test
->execute_msg
)
797 ret
= test
->execute_msg(spi
, test
, tx
, rx
);
799 ret
= spi_test_execute_msg(spi
, test
, tx
, rx
);
802 if (ret
== test
->expected_return
)
806 "test failed - test returned %i, but we expect %i\n",
807 ret
, test
->expected_return
);
812 /* if it is 0, as we expected something else,
813 * then return something special
818 static int spi_test_run_iter(struct spi_device
*spi
,
819 const struct spi_test
*testtemplate
,
826 struct spi_test test
;
827 int i
, tx_count
, rx_count
;
829 /* copy the test template to test */
830 memcpy(&test
, testtemplate
, sizeof(test
));
832 /* if iterate_transfer_mask is not set,
833 * then set it to first transfer only
835 if (!(test
.iterate_transfer_mask
& (BIT(test
.transfer_count
) - 1)))
836 test
.iterate_transfer_mask
= 1;
838 /* count number of transfers with tx/rx_buf != NULL */
839 rx_count
= tx_count
= 0;
840 for (i
= 0; i
< test
.transfer_count
; i
++) {
841 if (test
.transfers
[i
].tx_buf
)
843 if (test
.transfers
[i
].rx_buf
)
847 /* in some iteration cases warn and exit early,
848 * as there is nothing to do, that has not been tested already...
850 if (tx_off
&& (!tx_count
)) {
851 dev_warn_once(&spi
->dev
,
852 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n",
856 if (rx_off
&& (!rx_count
)) {
857 dev_warn_once(&spi
->dev
,
858 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n",
864 if (!(len
|| tx_off
|| rx_off
)) {
865 dev_info(&spi
->dev
, "Running test %s\n", test
.description
);
868 " with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n",
869 len
, tx_off
, rx_off
);
872 /* update in the values from iteration values */
873 for (i
= 0; i
< test
.transfer_count
; i
++) {
874 /* only when bit in transfer mask is set */
875 if (!(test
.iterate_transfer_mask
& BIT(i
)))
877 test
.transfers
[i
].len
= len
;
878 if (test
.transfers
[i
].tx_buf
)
879 test
.transfers
[i
].tx_buf
+= tx_off
;
880 if (test
.transfers
[i
].tx_buf
)
881 test
.transfers
[i
].rx_buf
+= rx_off
;
885 return _spi_test_run_iter(spi
, &test
, tx
, rx
);
889 * spi_test_execute_msg - default implementation to run a test
891 * spi: @spi_device on which to run the @spi_message
892 * test: the test to execute, which already contains @msg
893 * tx: the tx buffer allocated for the test sequence
894 * rx: the rx buffer allocated for the test sequence
896 * Returns: error code of spi_sync as well as basic error checking
898 int spi_test_execute_msg(struct spi_device
*spi
, struct spi_test
*test
,
901 struct spi_message
*msg
= &test
->msg
;
905 /* only if we do not simulate */
906 if (!simulate_only
) {
909 /* dump the complete message before and after the transfer */
910 if (dump_messages
== 3)
911 spi_test_dump_message(spi
, msg
, true);
914 /* run spi message */
915 ret
= spi_sync(spi
, msg
);
916 test
->elapsed_time
= ktime_to_ns(ktime_sub(ktime_get(), start
));
917 if (ret
== -ETIMEDOUT
) {
919 "spi-message timed out - rerunning...\n");
920 /* rerun after a few explicit schedules */
921 for (i
= 0; i
< 16; i
++)
923 ret
= spi_sync(spi
, msg
);
927 "Failed to execute spi_message: %i\n",
932 /* do some extra error checks */
933 if (msg
->frame_length
!= msg
->actual_length
) {
935 "actual length differs from expected\n");
940 /* run rx-buffer tests */
941 ret
= spi_test_check_loopback_result(spi
, msg
, tx
, rx
);
945 ret
= spi_test_check_elapsed_time(spi
, test
);
948 /* if requested or on error dump message (including data) */
950 if (dump_messages
|| ret
)
951 spi_test_dump_message(spi
, msg
,
952 (dump_messages
>= 2) || (ret
));
956 EXPORT_SYMBOL_GPL(spi_test_execute_msg
);
959 * spi_test_run_test - run an individual spi_test
960 * including all the relevant iterations on:
961 * length and buffer alignment
963 * spi: the spi_device to send the messages to
964 * test: the test which we need to execute
965 * tx: the tx buffer allocated for the test sequence
966 * rx: the rx buffer allocated for the test sequence
968 * Returns: status code of spi_sync or other failures
971 int spi_test_run_test(struct spi_device
*spi
, const struct spi_test
*test
,
976 size_t tx_align
, rx_align
;
979 /* test for transfer limits */
980 if (test
->transfer_count
>= SPI_TEST_MAX_TRANSFERS
) {
982 "%s: Exceeded max number of transfers with %i\n",
983 test
->description
, test
->transfer_count
);
987 /* setting up some values in spi_message
988 * based on some settings in spi_master
989 * some of this can also get done in the run() method
992 /* iterate over all the iterable values using macros
993 * (to make it a bit more readable...
995 #define FOR_EACH_ALIGNMENT(var) \
997 var < (test->iterate_##var ? \
998 (spi->master->dma_alignment ? \
999 spi->master->dma_alignment : \
1000 test->iterate_##var) : \
1004 for (idx_len
= 0; idx_len
< SPI_TEST_MAX_ITERATE
&&
1005 (len
= test
->iterate_len
[idx_len
]) != -1; idx_len
++) {
1006 FOR_EACH_ALIGNMENT(tx_align
) {
1007 FOR_EACH_ALIGNMENT(rx_align
) {
1008 /* and run the iteration */
1009 ret
= spi_test_run_iter(spi
, test
,
1022 EXPORT_SYMBOL_GPL(spi_test_run_test
);
1025 * spi_test_run_tests - run an array of spi_messages tests
1026 * @spi: the spi device on which to run the tests
1027 * @tests: NULL-terminated array of @spi_test
1029 * Returns: status errors as per @spi_test_run_test()
1032 int spi_test_run_tests(struct spi_device
*spi
,
1033 struct spi_test
*tests
)
1035 char *rx
= NULL
, *tx
= NULL
;
1036 int ret
= 0, count
= 0;
1037 struct spi_test
*test
;
1039 /* allocate rx/tx buffers of 128kB size without devm
1040 * in the hope that is on a page boundary
1043 rx
= vmalloc(SPI_TEST_MAX_SIZE_PLUS
);
1045 rx
= kzalloc(SPI_TEST_MAX_SIZE_PLUS
, GFP_KERNEL
);
1051 tx
= vmalloc(SPI_TEST_MAX_SIZE_PLUS
);
1053 tx
= kzalloc(SPI_TEST_MAX_SIZE_PLUS
, GFP_KERNEL
);
1059 /* now run the individual tests in the table */
1060 for (test
= tests
, count
= 0; test
->description
[0];
1062 /* only run test if requested */
1063 if ((run_only_test
> -1) && (count
!= run_only_test
))
1065 /* run custom implementation */
1067 ret
= test
->run_test(spi
, test
, tx
, rx
);
1069 ret
= spi_test_run_test(spi
, test
, tx
, rx
);
1072 /* add some delays so that we can easily
1073 * detect the individual tests when using a logic analyzer
1074 * we also add scheduling to avoid potential spi_timeouts...
1086 EXPORT_SYMBOL_GPL(spi_test_run_tests
);