Lines Matching +full:tx +full:- +full:rx
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/spi/spi-loopback-test.c
23 #include "spi-test.h"
37 /* the device is jumpered for loopback - enabling some rx_buf tests */
57 static int run_only_iter_len = -1;
63 static int run_only_test = -1;
66 "only run the test with this number (0-based !)");
74 /* check rx ranges */
88 .description = "tx/rx-transfer - start of page",
96 .tx_buf = TX(0),
97 .rx_buf = RX(0),
102 .description = "tx/rx-transfer - crossing PAGE_SIZE",
110 .tx_buf = TX(PAGE_SIZE - 4),
111 .rx_buf = RX(PAGE_SIZE - 4),
116 .description = "tx-transfer - only",
123 .tx_buf = TX(0),
128 .description = "rx-transfer - only",
135 .rx_buf = RX(0),
140 .description = "two tx-transfers - alter both",
148 .tx_buf = TX(0),
152 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF),
157 .description = "two tx-transfers - alter first",
165 .tx_buf = TX(64),
169 .tx_buf = TX(0),
174 .description = "two tx-transfers - alter second",
183 .tx_buf = TX(0),
186 .tx_buf = TX(64),
191 .description = "two transfers tx then rx - alter both",
199 .tx_buf = TX(0),
202 .rx_buf = RX(0),
207 .description = "two transfers tx then rx - alter tx",
215 .tx_buf = TX(0),
219 .rx_buf = RX(0),
224 .description = "two transfers tx then rx - alter rx",
233 .tx_buf = TX(0),
236 .rx_buf = RX(0),
241 .description = "two tx+rx transfers - alter both",
249 .tx_buf = TX(0),
250 .rx_buf = RX(0),
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",
271 .tx_buf = TX(1024),
272 .rx_buf = RX(1024),
277 .tx_buf = TX(0),
278 .rx_buf = RX(0),
283 .description = "two tx+rx transfers - alter second",
292 .tx_buf = TX(0),
293 .rx_buf = RX(0),
297 .tx_buf = TX(1024),
298 .rx_buf = RX(1024),
303 .description = "two tx+rx transfers - delay after transfer",
310 .tx_buf = TX(0),
311 .rx_buf = RX(0),
318 .tx_buf = TX(0),
319 .rx_buf = RX(0),
328 .description = "three tx+rx transfers with overlapping cache lines",
334 .iterate_len = { 512, -1 },
340 .tx_buf = TX(0),
341 .rx_buf = RX(0),
344 .tx_buf = TX(1),
345 .rx_buf = RX(1),
349 .tx_buf = TX(513),
350 .rx_buf = RX(513),
363 spi->mode |= loop_req ? SPI_LOOP : 0; in spi_loopback_test_probe()
364 spi->mode |= no_cs ? SPI_NO_CS : 0; in spi_loopback_test_probe()
367 dev_err(&spi->dev, "SPI setup with SPI_LOOP or SPI_NO_CS failed (%d)\n", in spi_loopback_test_probe()
373 dev_info(&spi->dev, "Executing spi-loopback-tests\n"); in spi_loopback_test_probe()
377 dev_info(&spi->dev, "Finished spi-loopback-tests with return: %i\n", in spi_loopback_test_probe()
385 { .compatible = "linux,spi-loopback-test", },
398 .name = "spi-loopback-test",
411 /*-------------------------------------------------------------------------*/
435 pr_info("%s truncated - continuing at offset %04zx\n", in spi_test_print_hex_dump()
436 pre, len - 512); in spi_test_print_hex_dump()
439 ptr + (len - 512), 512, 0); in spi_test_print_hex_dump()
450 dev_info(&spi->dev, " spi_msg@%pK\n", msg); in spi_test_dump_message()
451 if (msg->status) in spi_test_dump_message()
452 dev_info(&spi->dev, " status: %i\n", in spi_test_dump_message()
453 msg->status); in spi_test_dump_message()
454 dev_info(&spi->dev, " frame_length: %i\n", in spi_test_dump_message()
455 msg->frame_length); in spi_test_dump_message()
456 dev_info(&spi->dev, " actual_length: %i\n", in spi_test_dump_message()
457 msg->actual_length); in spi_test_dump_message()
459 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_test_dump_message()
460 dev_info(&spi->dev, " spi_transfer@%pK\n", xfer); in spi_test_dump_message()
461 dev_info(&spi->dev, " len: %i\n", xfer->len); in spi_test_dump_message()
462 dev_info(&spi->dev, " tx_buf: %pK\n", xfer->tx_buf); in spi_test_dump_message()
463 if (dump_data && xfer->tx_buf) in spi_test_dump_message()
464 spi_test_print_hex_dump(" TX: ", in spi_test_dump_message()
465 xfer->tx_buf, in spi_test_dump_message()
466 xfer->len); in spi_test_dump_message()
468 dev_info(&spi->dev, " rx_buf: %pK\n", xfer->rx_buf); in spi_test_dump_message()
469 if (dump_data && xfer->rx_buf) in spi_test_dump_message()
470 spi_test_print_hex_dump(" RX: ", in spi_test_dump_message()
471 xfer->rx_buf, in spi_test_dump_message()
472 xfer->len); in spi_test_dump_message()
474 if (xfer->rx_buf) { in spi_test_dump_message()
475 for (i = 0 ; i < xfer->len ; i++) { in spi_test_dump_message()
476 b = ((u8 *)xfer->rx_buf)[xfer->len - 1 - i]; in spi_test_dump_message()
481 dev_info(&spi->dev, in spi_test_dump_message()
484 xfer->len - i); in spi_test_dump_message()
501 if (rx_a->start > rx_b->start) in rx_ranges_cmp()
503 if (rx_a->start < rx_b->start) in rx_ranges_cmp()
504 return -1; in rx_ranges_cmp()
510 void *rx) in spi_check_rx_ranges() argument
520 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_check_rx_ranges()
521 /* if there is no rx, then no check is needed */ in spi_check_rx_ranges()
522 if (!xfer->rx_buf) in spi_check_rx_ranges()
525 if (RANGE_CHECK(xfer->rx_buf, xfer->len, in spi_check_rx_ranges()
526 rx, SPI_TEST_MAX_SIZE_PLUS)) { in spi_check_rx_ranges()
527 ranges[i].start = xfer->rx_buf; in spi_check_rx_ranges()
528 ranges[i].end = xfer->rx_buf + xfer->len; in spi_check_rx_ranges()
541 /* and iterate over all the rx addresses */ in spi_check_rx_ranges()
542 for (addr = rx; addr < (u8 *)rx + SPI_TEST_MAX_SIZE_PLUS; addr++) { in spi_check_rx_ranges()
552 if ((addr >= r->start) && (addr < r->end)) in spi_check_rx_ranges()
553 addr = r->end; in spi_check_rx_ranges()
561 dev_err(&spi->dev, in spi_check_rx_ranges()
562 "loopback strangeness - rx changed outside of allowed range at: %pK\n", in spi_check_rx_ranges()
567 ret = -ERANGE; in spi_check_rx_ranges()
580 for (i = 0; i < test->transfer_count; i++) { in spi_test_check_elapsed_time()
581 struct spi_transfer *xfer = test->transfers + i; in spi_test_check_elapsed_time()
583 xfer->len; in spi_test_check_elapsed_time()
585 delay_usecs += xfer->delay.value; in spi_test_check_elapsed_time()
586 if (!xfer->speed_hz) in spi_test_check_elapsed_time()
588 estimated_time += div_u64(nbits * NSEC_PER_SEC, xfer->speed_hz); in spi_test_check_elapsed_time()
592 if (test->elapsed_time < estimated_time) { in spi_test_check_elapsed_time()
593 dev_err(&spi->dev, in spi_test_check_elapsed_time()
595 test->elapsed_time, estimated_time); in spi_test_check_elapsed_time()
597 return -EINVAL; in spi_test_check_elapsed_time()
605 void *tx, void *rx) in spi_test_check_loopback_result() argument
614 ret = spi_check_rx_ranges(spi, msg, rx); in spi_test_check_loopback_result()
624 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_test_check_loopback_result()
625 /* if there is no rx, then no check is needed */ in spi_test_check_loopback_result()
626 if (!xfer->len || !xfer->rx_buf) in spi_test_check_loopback_result()
629 if (xfer->tx_buf) { in spi_test_check_loopback_result()
630 for (i = 0; i < xfer->len; i++) { in spi_test_check_loopback_result()
631 txb = ((u8 *)xfer->tx_buf)[i]; in spi_test_check_loopback_result()
632 rxb = ((u8 *)xfer->rx_buf)[i]; in spi_test_check_loopback_result()
638 txb = ((u8 *)xfer->rx_buf)[0]; in spi_test_check_loopback_result()
641 dev_err(&spi->dev, in spi_test_check_loopback_result()
642 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n", in spi_test_check_loopback_result()
644 return -EINVAL; in spi_test_check_loopback_result()
647 for (i = 1; i < xfer->len; i++) { in spi_test_check_loopback_result()
648 rxb = ((u8 *)xfer->rx_buf)[i]; in spi_test_check_loopback_result()
658 dev_err(&spi->dev, in spi_test_check_loopback_result()
659 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n", in spi_test_check_loopback_result()
662 return -EINVAL; in spi_test_check_loopback_result()
667 void *tx, void *rx) in spi_test_translate() argument
678 *ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) - in spi_test_translate()
681 /* RX range in spi_test_translate()
682 * - we check against MAX_SIZE_PLUS to allow for automated alignment in spi_test_translate()
684 if (RANGE_CHECK(*ptr, len, RX(0), SPI_TEST_MAX_SIZE_PLUS)) { in spi_test_translate()
685 off = *ptr - RX(0); in spi_test_translate()
686 *ptr = rx + off; in spi_test_translate()
691 /* TX range */ in spi_test_translate()
692 if (RANGE_CHECK(*ptr, len, TX(0), SPI_TEST_MAX_SIZE_PLUS)) { in spi_test_translate()
693 off = *ptr - TX(0); in spi_test_translate()
694 *ptr = tx + off; in spi_test_translate()
699 dev_err(&spi->dev, in spi_test_translate()
702 RX(0), RX(SPI_TEST_MAX_SIZE), in spi_test_translate()
703 TX(0), TX(SPI_TEST_MAX_SIZE)); in spi_test_translate()
705 return -EINVAL; in spi_test_translate()
711 struct spi_transfer *xfers = test->transfers; in spi_test_fill_pattern()
718 (value >> (8 * (bytes - 1 - count % bytes))) in spi_test_fill_pattern()
725 for (i = 0; i < test->transfer_count; i++) { in spi_test_fill_pattern()
736 /* fill tx */ in spi_test_fill_pattern()
737 switch (test->fill_option) { in spi_test_fill_pattern()
739 *tx_buf = test->fill_pattern; in spi_test_fill_pattern()
742 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
746 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
750 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
781 dev_err(&spi->dev, in spi_test_fill_pattern()
783 test->fill_option); in spi_test_fill_pattern()
784 return -EINVAL; in spi_test_fill_pattern()
794 void *tx, void *rx) in _spi_test_run_iter() argument
796 struct spi_message *msg = &test->msg; in _spi_test_run_iter()
800 /* initialize message - zero-filled via static initialization */ in _spi_test_run_iter()
803 /* fill rx with the DO_NOT_WRITE pattern */ in _spi_test_run_iter()
804 memset(rx, SPI_TEST_PATTERN_DO_NOT_WRITE, SPI_TEST_MAX_SIZE_PLUS); in _spi_test_run_iter()
807 for (i = 0; i < test->transfer_count; i++) { in _spi_test_run_iter()
808 x = &test->transfers[i]; in _spi_test_run_iter()
811 ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len, in _spi_test_run_iter()
812 (void *)tx, rx); in _spi_test_run_iter()
817 ret = spi_test_translate(spi, &x->rx_buf, x->len, in _spi_test_run_iter()
818 (void *)tx, rx); in _spi_test_run_iter()
832 if (test->execute_msg) in _spi_test_run_iter()
833 ret = test->execute_msg(spi, test, tx, rx); in _spi_test_run_iter()
835 ret = spi_test_execute_msg(spi, test, tx, rx); in _spi_test_run_iter()
838 if (ret == test->expected_return) in _spi_test_run_iter()
841 dev_err(&spi->dev, in _spi_test_run_iter()
842 "test failed - test returned %i, but we expect %i\n", in _spi_test_run_iter()
843 ret, test->expected_return); in _spi_test_run_iter()
851 return -EFAULT; in _spi_test_run_iter()
856 void *tx, void *rx, in spi_test_run_iter() argument
871 if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1))) in spi_test_run_iter()
874 /* count number of transfers with tx/rx_buf != NULL */ in spi_test_run_iter()
887 dev_warn_once(&spi->dev, in spi_test_run_iter()
888 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n", in spi_test_run_iter()
893 dev_warn_once(&spi->dev, in spi_test_run_iter()
894 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n", in spi_test_run_iter()
901 dev_info(&spi->dev, "Running test %s\n", test.description); in spi_test_run_iter()
903 dev_info(&spi->dev, in spi_test_run_iter()
921 return _spi_test_run_iter(spi, &test, tx, rx); in spi_test_run_iter()
925 * spi_test_execute_msg - default implementation to run a test
929 * @tx: the tx buffer allocated for the test sequence
930 * @rx: the rx buffer allocated for the test sequence
935 void *tx, void *rx) in spi_test_execute_msg() argument
937 struct spi_message *msg = &test->msg; in spi_test_execute_msg()
952 test->elapsed_time = ktime_to_ns(ktime_sub(ktime_get(), start)); in spi_test_execute_msg()
953 if (ret == -ETIMEDOUT) { in spi_test_execute_msg()
954 dev_info(&spi->dev, in spi_test_execute_msg()
955 "spi-message timed out - rerunning...\n"); in spi_test_execute_msg()
962 dev_err(&spi->dev, in spi_test_execute_msg()
969 if (msg->frame_length != msg->actual_length) { in spi_test_execute_msg()
970 dev_err(&spi->dev, in spi_test_execute_msg()
972 ret = -EIO; in spi_test_execute_msg()
976 /* run rx-buffer tests */ in spi_test_execute_msg()
977 ret = spi_test_check_loopback_result(spi, msg, tx, rx); in spi_test_execute_msg()
995 * spi_test_run_test - run an individual spi_test
1001 * @tx: the tx buffer allocated for the test sequence
1002 * @rx: the rx buffer allocated for the test sequence
1008 void *tx, void *rx) in spi_test_run_test() argument
1016 if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) { in spi_test_run_test()
1017 dev_err(&spi->dev, in spi_test_run_test()
1019 test->description, test->transfer_count); in spi_test_run_test()
1020 return -E2BIG; in spi_test_run_test()
1033 var < (test->iterate_##var ? \ in spi_test_run_test()
1034 (spi->master->dma_alignment ? \ in spi_test_run_test()
1035 spi->master->dma_alignment : \ in spi_test_run_test()
1036 test->iterate_##var) : \ in spi_test_run_test()
1041 (len = test->iterate_len[idx_len]) != -1; idx_len++) { in spi_test_run_test()
1042 if ((run_only_iter_len > -1) && len != run_only_iter_len) in spi_test_run_test()
1048 tx, rx, in spi_test_run_test()
1063 * spi_test_run_tests - run an array of spi_messages tests
1065 * @tests: NULL-terminated array of @spi_test
1073 char *rx = NULL, *tx = NULL; in spi_test_run_tests() local
1077 /* allocate rx/tx buffers of 128kB size without devm in spi_test_run_tests()
1081 rx = vmalloc(SPI_TEST_MAX_SIZE_PLUS); in spi_test_run_tests()
1083 rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL); in spi_test_run_tests()
1084 if (!rx) in spi_test_run_tests()
1085 return -ENOMEM; in spi_test_run_tests()
1089 tx = vmalloc(SPI_TEST_MAX_SIZE_PLUS); in spi_test_run_tests()
1091 tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL); in spi_test_run_tests()
1092 if (!tx) { in spi_test_run_tests()
1093 ret = -ENOMEM; in spi_test_run_tests()
1098 for (test = tests, count = 0; test->description[0]; in spi_test_run_tests()
1101 if ((run_only_test > -1) && (count != run_only_test)) in spi_test_run_tests()
1104 if (test->run_test) in spi_test_run_tests()
1105 ret = test->run_test(spi, test, tx, rx); in spi_test_run_tests()
1107 ret = spi_test_run_test(spi, test, tx, rx); in spi_test_run_tests()
1120 kvfree(tx); in spi_test_run_tests()
1122 kvfree(rx); in spi_test_run_tests()