Lines Matching +full:rx +full:- +full:tx
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_test = -1;
60 "only run the test with this number (0-based !)");
68 /* check rx ranges */
77 .description = "tx/rx-transfer - start of page",
85 .tx_buf = TX(0),
86 .rx_buf = RX(0),
91 .description = "tx/rx-transfer - crossing PAGE_SIZE",
99 .tx_buf = TX(PAGE_SIZE - 4),
100 .rx_buf = RX(PAGE_SIZE - 4),
105 .description = "tx-transfer - only",
112 .tx_buf = TX(0),
117 .description = "rx-transfer - only",
124 .rx_buf = RX(0),
129 .description = "two tx-transfers - alter both",
137 .tx_buf = TX(0),
141 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF),
146 .description = "two tx-transfers - alter first",
154 .tx_buf = TX(64),
158 .tx_buf = TX(0),
163 .description = "two tx-transfers - alter second",
172 .tx_buf = TX(0),
175 .tx_buf = TX(64),
180 .description = "two transfers tx then rx - alter both",
188 .tx_buf = TX(0),
191 .rx_buf = RX(0),
196 .description = "two transfers tx then rx - alter tx",
204 .tx_buf = TX(0),
208 .rx_buf = RX(0),
213 .description = "two transfers tx then rx - alter rx",
222 .tx_buf = TX(0),
225 .rx_buf = RX(0),
230 .description = "two tx+rx transfers - alter both",
238 .tx_buf = TX(0),
239 .rx_buf = RX(0),
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",
260 .tx_buf = TX(1024),
261 .rx_buf = RX(1024),
266 .tx_buf = TX(0),
267 .rx_buf = RX(0),
272 .description = "two tx+rx transfers - alter second",
281 .tx_buf = TX(0),
282 .rx_buf = RX(0),
286 .tx_buf = TX(1024),
287 .rx_buf = RX(1024),
292 .description = "two tx+rx transfers - delay after transfer",
299 .tx_buf = TX(0),
300 .rx_buf = RX(0),
307 .tx_buf = TX(0),
308 .rx_buf = RX(0),
325 spi->mode |= loop_req ? SPI_LOOP : 0; in spi_loopback_test_probe()
326 spi->mode |= no_cs ? SPI_NO_CS : 0; in spi_loopback_test_probe()
329 dev_err(&spi->dev, "SPI setup with SPI_LOOP or SPI_NO_CS failed (%d)\n", in spi_loopback_test_probe()
335 dev_info(&spi->dev, "Executing spi-loopback-tests\n"); in spi_loopback_test_probe()
339 dev_info(&spi->dev, "Finished spi-loopback-tests with return: %i\n", in spi_loopback_test_probe()
347 { .compatible = "linux,spi-loopback-test", },
360 .name = "spi-loopback-test",
373 /*-------------------------------------------------------------------------*/
397 pr_info("%s truncated - continuing at offset %04zx\n", in spi_test_print_hex_dump()
398 pre, len - 512); in spi_test_print_hex_dump()
401 ptr + (len - 512), 512, 0); in spi_test_print_hex_dump()
412 dev_info(&spi->dev, " spi_msg@%pK\n", msg); in spi_test_dump_message()
413 if (msg->status) in spi_test_dump_message()
414 dev_info(&spi->dev, " status: %i\n", in spi_test_dump_message()
415 msg->status); in spi_test_dump_message()
416 dev_info(&spi->dev, " frame_length: %i\n", in spi_test_dump_message()
417 msg->frame_length); in spi_test_dump_message()
418 dev_info(&spi->dev, " actual_length: %i\n", in spi_test_dump_message()
419 msg->actual_length); in spi_test_dump_message()
421 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_test_dump_message()
422 dev_info(&spi->dev, " spi_transfer@%pK\n", xfer); in spi_test_dump_message()
423 dev_info(&spi->dev, " len: %i\n", xfer->len); in spi_test_dump_message()
424 dev_info(&spi->dev, " tx_buf: %pK\n", xfer->tx_buf); in spi_test_dump_message()
425 if (dump_data && xfer->tx_buf) in spi_test_dump_message()
426 spi_test_print_hex_dump(" TX: ", in spi_test_dump_message()
427 xfer->tx_buf, in spi_test_dump_message()
428 xfer->len); in spi_test_dump_message()
430 dev_info(&spi->dev, " rx_buf: %pK\n", xfer->rx_buf); in spi_test_dump_message()
431 if (dump_data && xfer->rx_buf) in spi_test_dump_message()
432 spi_test_print_hex_dump(" RX: ", in spi_test_dump_message()
433 xfer->rx_buf, in spi_test_dump_message()
434 xfer->len); in spi_test_dump_message()
436 if (xfer->rx_buf) { in spi_test_dump_message()
437 for (i = 0 ; i < xfer->len ; i++) { in spi_test_dump_message()
438 b = ((u8 *)xfer->rx_buf)[xfer->len - 1 - i]; in spi_test_dump_message()
443 dev_info(&spi->dev, in spi_test_dump_message()
446 xfer->len - i); in spi_test_dump_message()
462 if (rx_a->start > rx_b->start) in rx_ranges_cmp()
464 if (rx_a->start < rx_b->start) in rx_ranges_cmp()
465 return -1; in rx_ranges_cmp()
471 void *rx) in spi_check_rx_ranges() argument
481 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_check_rx_ranges()
482 /* if there is no rx, then no check is needed */ in spi_check_rx_ranges()
483 if (!xfer->rx_buf) in spi_check_rx_ranges()
486 if (RANGE_CHECK(xfer->rx_buf, xfer->len, in spi_check_rx_ranges()
487 rx, SPI_TEST_MAX_SIZE_PLUS)) { in spi_check_rx_ranges()
488 ranges[i].start = xfer->rx_buf; in spi_check_rx_ranges()
489 ranges[i].end = xfer->rx_buf + xfer->len; in spi_check_rx_ranges()
502 /* and iterate over all the rx addresses */ in spi_check_rx_ranges()
503 for (addr = rx; addr < (u8 *)rx + SPI_TEST_MAX_SIZE_PLUS; addr++) { in spi_check_rx_ranges()
513 if ((addr >= r->start) && (addr < r->end)) in spi_check_rx_ranges()
514 addr = r->end; in spi_check_rx_ranges()
522 dev_err(&spi->dev, in spi_check_rx_ranges()
523 "loopback strangeness - rx changed outside of allowed range at: %pK\n", in spi_check_rx_ranges()
528 ret = -ERANGE; in spi_check_rx_ranges()
541 for (i = 0; i < test->transfer_count; i++) { in spi_test_check_elapsed_time()
542 struct spi_transfer *xfer = test->transfers + i; in spi_test_check_elapsed_time()
544 xfer->len; in spi_test_check_elapsed_time()
546 delay_usecs += xfer->delay.value; in spi_test_check_elapsed_time()
547 if (!xfer->speed_hz) in spi_test_check_elapsed_time()
549 estimated_time += div_u64(nbits * NSEC_PER_SEC, xfer->speed_hz); in spi_test_check_elapsed_time()
553 if (test->elapsed_time < estimated_time) { in spi_test_check_elapsed_time()
554 dev_err(&spi->dev, in spi_test_check_elapsed_time()
556 test->elapsed_time, estimated_time); in spi_test_check_elapsed_time()
558 return -EINVAL; in spi_test_check_elapsed_time()
566 void *tx, void *rx) in spi_test_check_loopback_result() argument
575 ret = spi_check_rx_ranges(spi, msg, rx); in spi_test_check_loopback_result()
585 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_test_check_loopback_result()
586 /* if there is no rx, then no check is needed */ in spi_test_check_loopback_result()
587 if (!xfer->len || !xfer->rx_buf) in spi_test_check_loopback_result()
590 if (xfer->tx_buf) { in spi_test_check_loopback_result()
591 for (i = 0; i < xfer->len; i++) { in spi_test_check_loopback_result()
592 txb = ((u8 *)xfer->tx_buf)[i]; in spi_test_check_loopback_result()
593 rxb = ((u8 *)xfer->rx_buf)[i]; in spi_test_check_loopback_result()
599 txb = ((u8 *)xfer->rx_buf)[0]; in spi_test_check_loopback_result()
602 dev_err(&spi->dev, in spi_test_check_loopback_result()
603 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n", in spi_test_check_loopback_result()
605 return -EINVAL; in spi_test_check_loopback_result()
608 for (i = 1; i < xfer->len; i++) { in spi_test_check_loopback_result()
609 rxb = ((u8 *)xfer->rx_buf)[i]; in spi_test_check_loopback_result()
619 dev_err(&spi->dev, in spi_test_check_loopback_result()
620 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n", in spi_test_check_loopback_result()
623 return -EINVAL; in spi_test_check_loopback_result()
628 void *tx, void *rx) in spi_test_translate() argument
639 *ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) - in spi_test_translate()
642 /* RX range in spi_test_translate()
643 * - we check against MAX_SIZE_PLUS to allow for automated alignment in spi_test_translate()
645 if (RANGE_CHECK(*ptr, len, RX(0), SPI_TEST_MAX_SIZE_PLUS)) { in spi_test_translate()
646 off = *ptr - RX(0); in spi_test_translate()
647 *ptr = rx + off; in spi_test_translate()
652 /* TX range */ in spi_test_translate()
653 if (RANGE_CHECK(*ptr, len, TX(0), SPI_TEST_MAX_SIZE_PLUS)) { in spi_test_translate()
654 off = *ptr - TX(0); in spi_test_translate()
655 *ptr = tx + off; in spi_test_translate()
660 dev_err(&spi->dev, in spi_test_translate()
663 RX(0), RX(SPI_TEST_MAX_SIZE), in spi_test_translate()
664 TX(0), TX(SPI_TEST_MAX_SIZE)); in spi_test_translate()
666 return -EINVAL; in spi_test_translate()
672 struct spi_transfer *xfers = test->transfers; in spi_test_fill_pattern()
679 (value >> (8 * (bytes - 1 - count % bytes))) in spi_test_fill_pattern()
686 for (i = 0; i < test->transfer_count; i++) { in spi_test_fill_pattern()
697 /* fill tx */ in spi_test_fill_pattern()
698 switch (test->fill_option) { in spi_test_fill_pattern()
700 *tx_buf = test->fill_pattern; in spi_test_fill_pattern()
703 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
707 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
711 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
742 dev_err(&spi->dev, in spi_test_fill_pattern()
744 test->fill_option); in spi_test_fill_pattern()
745 return -EINVAL; in spi_test_fill_pattern()
755 void *tx, void *rx) in _spi_test_run_iter() argument
757 struct spi_message *msg = &test->msg; in _spi_test_run_iter()
761 /* initialize message - zero-filled via static initialization */ in _spi_test_run_iter()
764 /* fill rx with the DO_NOT_WRITE pattern */ in _spi_test_run_iter()
765 memset(rx, SPI_TEST_PATTERN_DO_NOT_WRITE, SPI_TEST_MAX_SIZE_PLUS); in _spi_test_run_iter()
768 for (i = 0; i < test->transfer_count; i++) { in _spi_test_run_iter()
769 x = &test->transfers[i]; in _spi_test_run_iter()
772 ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len, in _spi_test_run_iter()
773 (void *)tx, rx); in _spi_test_run_iter()
778 ret = spi_test_translate(spi, &x->rx_buf, x->len, in _spi_test_run_iter()
779 (void *)tx, rx); in _spi_test_run_iter()
793 if (test->execute_msg) in _spi_test_run_iter()
794 ret = test->execute_msg(spi, test, tx, rx); in _spi_test_run_iter()
796 ret = spi_test_execute_msg(spi, test, tx, rx); in _spi_test_run_iter()
799 if (ret == test->expected_return) in _spi_test_run_iter()
802 dev_err(&spi->dev, in _spi_test_run_iter()
803 "test failed - test returned %i, but we expect %i\n", in _spi_test_run_iter()
804 ret, test->expected_return); in _spi_test_run_iter()
812 return -EFAULT; in _spi_test_run_iter()
817 void *tx, void *rx, in spi_test_run_iter() argument
832 if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1))) in spi_test_run_iter()
835 /* count number of transfers with tx/rx_buf != NULL */ in spi_test_run_iter()
848 dev_warn_once(&spi->dev, in spi_test_run_iter()
849 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n", in spi_test_run_iter()
854 dev_warn_once(&spi->dev, in spi_test_run_iter()
855 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n", in spi_test_run_iter()
862 dev_info(&spi->dev, "Running test %s\n", test.description); in spi_test_run_iter()
864 dev_info(&spi->dev, in spi_test_run_iter()
882 return _spi_test_run_iter(spi, &test, tx, rx); in spi_test_run_iter()
886 * spi_test_execute_msg - default implementation to run a test
890 * @tx: the tx buffer allocated for the test sequence
891 * @rx: the rx buffer allocated for the test sequence
896 void *tx, void *rx) in spi_test_execute_msg() argument
898 struct spi_message *msg = &test->msg; in spi_test_execute_msg()
913 test->elapsed_time = ktime_to_ns(ktime_sub(ktime_get(), start)); in spi_test_execute_msg()
914 if (ret == -ETIMEDOUT) { in spi_test_execute_msg()
915 dev_info(&spi->dev, in spi_test_execute_msg()
916 "spi-message timed out - rerunning...\n"); in spi_test_execute_msg()
923 dev_err(&spi->dev, in spi_test_execute_msg()
930 if (msg->frame_length != msg->actual_length) { in spi_test_execute_msg()
931 dev_err(&spi->dev, in spi_test_execute_msg()
933 ret = -EIO; in spi_test_execute_msg()
937 /* run rx-buffer tests */ in spi_test_execute_msg()
938 ret = spi_test_check_loopback_result(spi, msg, tx, rx); in spi_test_execute_msg()
956 * spi_test_run_test - run an individual spi_test
962 * @tx: the tx buffer allocated for the test sequence
963 * @rx: the rx buffer allocated for the test sequence
969 void *tx, void *rx) in spi_test_run_test() argument
977 if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) { in spi_test_run_test()
978 dev_err(&spi->dev, in spi_test_run_test()
980 test->description, test->transfer_count); in spi_test_run_test()
981 return -E2BIG; in spi_test_run_test()
994 var < (test->iterate_##var ? \ in spi_test_run_test()
995 (spi->master->dma_alignment ? \ in spi_test_run_test()
996 spi->master->dma_alignment : \ in spi_test_run_test()
997 test->iterate_##var) : \ in spi_test_run_test()
1002 (len = test->iterate_len[idx_len]) != -1; idx_len++) { in spi_test_run_test()
1007 tx, rx, in spi_test_run_test()
1022 * spi_test_run_tests - run an array of spi_messages tests
1024 * @tests: NULL-terminated array of @spi_test
1032 char *rx = NULL, *tx = NULL; in spi_test_run_tests() local
1036 /* allocate rx/tx buffers of 128kB size without devm in spi_test_run_tests()
1040 rx = vmalloc(SPI_TEST_MAX_SIZE_PLUS); in spi_test_run_tests()
1042 rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL); in spi_test_run_tests()
1043 if (!rx) in spi_test_run_tests()
1044 return -ENOMEM; in spi_test_run_tests()
1048 tx = vmalloc(SPI_TEST_MAX_SIZE_PLUS); in spi_test_run_tests()
1050 tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL); in spi_test_run_tests()
1051 if (!tx) { in spi_test_run_tests()
1052 ret = -ENOMEM; in spi_test_run_tests()
1057 for (test = tests, count = 0; test->description[0]; in spi_test_run_tests()
1060 if ((run_only_test > -1) && (count != run_only_test)) in spi_test_run_tests()
1063 if (test->run_test) in spi_test_run_tests()
1064 ret = test->run_test(spi, test, tx, rx); in spi_test_run_tests()
1066 ret = spi_test_run_test(spi, test, tx, rx); in spi_test_run_tests()
1078 kvfree(tx); in spi_test_run_tests()
1080 kvfree(rx); in spi_test_run_tests()