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()
463 if (rx_a->start > rx_b->start) in rx_ranges_cmp()
465 if (rx_a->start < rx_b->start) in rx_ranges_cmp()
466 return -1; in rx_ranges_cmp()
472 void *rx) in spi_check_rx_ranges() argument
482 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_check_rx_ranges()
483 /* if there is no rx, then no check is needed */ in spi_check_rx_ranges()
484 if (!xfer->rx_buf) in spi_check_rx_ranges()
487 if (RANGE_CHECK(xfer->rx_buf, xfer->len, in spi_check_rx_ranges()
488 rx, SPI_TEST_MAX_SIZE_PLUS)) { in spi_check_rx_ranges()
489 ranges[i].start = xfer->rx_buf; in spi_check_rx_ranges()
490 ranges[i].end = xfer->rx_buf + xfer->len; in spi_check_rx_ranges()
503 /* and iterate over all the rx addresses */ in spi_check_rx_ranges()
504 for (addr = rx; addr < (u8 *)rx + SPI_TEST_MAX_SIZE_PLUS; addr++) { in spi_check_rx_ranges()
514 if ((addr >= r->start) && (addr < r->end)) in spi_check_rx_ranges()
515 addr = r->end; in spi_check_rx_ranges()
523 dev_err(&spi->dev, in spi_check_rx_ranges()
524 "loopback strangeness - rx changed outside of allowed range at: %pK\n", in spi_check_rx_ranges()
529 ret = -ERANGE; in spi_check_rx_ranges()
542 for (i = 0; i < test->transfer_count; i++) { in spi_test_check_elapsed_time()
543 struct spi_transfer *xfer = test->transfers + i; in spi_test_check_elapsed_time()
545 xfer->len; in spi_test_check_elapsed_time()
547 delay_usecs += xfer->delay.value; in spi_test_check_elapsed_time()
548 if (!xfer->speed_hz) in spi_test_check_elapsed_time()
550 estimated_time += div_u64(nbits * NSEC_PER_SEC, xfer->speed_hz); in spi_test_check_elapsed_time()
554 if (test->elapsed_time < estimated_time) { in spi_test_check_elapsed_time()
555 dev_err(&spi->dev, in spi_test_check_elapsed_time()
557 test->elapsed_time, estimated_time); in spi_test_check_elapsed_time()
559 return -EINVAL; in spi_test_check_elapsed_time()
567 void *tx, void *rx) in spi_test_check_loopback_result() argument
576 ret = spi_check_rx_ranges(spi, msg, rx); in spi_test_check_loopback_result()
586 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_test_check_loopback_result()
587 /* if there is no rx, then no check is needed */ in spi_test_check_loopback_result()
588 if (!xfer->len || !xfer->rx_buf) in spi_test_check_loopback_result()
591 if (xfer->tx_buf) { in spi_test_check_loopback_result()
592 for (i = 0; i < xfer->len; i++) { in spi_test_check_loopback_result()
593 txb = ((u8 *)xfer->tx_buf)[i]; in spi_test_check_loopback_result()
594 rxb = ((u8 *)xfer->rx_buf)[i]; in spi_test_check_loopback_result()
600 txb = ((u8 *)xfer->rx_buf)[0]; in spi_test_check_loopback_result()
603 dev_err(&spi->dev, in spi_test_check_loopback_result()
604 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n", in spi_test_check_loopback_result()
606 return -EINVAL; in spi_test_check_loopback_result()
609 for (i = 1; i < xfer->len; i++) { in spi_test_check_loopback_result()
610 rxb = ((u8 *)xfer->rx_buf)[i]; in spi_test_check_loopback_result()
620 dev_err(&spi->dev, in spi_test_check_loopback_result()
621 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n", in spi_test_check_loopback_result()
624 return -EINVAL; in spi_test_check_loopback_result()
629 void *tx, void *rx) in spi_test_translate() argument
640 *ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) - in spi_test_translate()
643 /* RX range in spi_test_translate()
644 * - we check against MAX_SIZE_PLUS to allow for automated alignment in spi_test_translate()
646 if (RANGE_CHECK(*ptr, len, RX(0), SPI_TEST_MAX_SIZE_PLUS)) { in spi_test_translate()
647 off = *ptr - RX(0); in spi_test_translate()
648 *ptr = rx + off; in spi_test_translate()
653 /* TX range */ in spi_test_translate()
654 if (RANGE_CHECK(*ptr, len, TX(0), SPI_TEST_MAX_SIZE_PLUS)) { in spi_test_translate()
655 off = *ptr - TX(0); in spi_test_translate()
656 *ptr = tx + off; in spi_test_translate()
661 dev_err(&spi->dev, in spi_test_translate()
664 RX(0), RX(SPI_TEST_MAX_SIZE), in spi_test_translate()
665 TX(0), TX(SPI_TEST_MAX_SIZE)); in spi_test_translate()
667 return -EINVAL; in spi_test_translate()
673 struct spi_transfer *xfers = test->transfers; in spi_test_fill_pattern()
680 (value >> (8 * (bytes - 1 - count % bytes))) in spi_test_fill_pattern()
687 for (i = 0; i < test->transfer_count; i++) { in spi_test_fill_pattern()
698 /* fill tx */ in spi_test_fill_pattern()
699 switch (test->fill_option) { in spi_test_fill_pattern()
701 *tx_buf = test->fill_pattern; in spi_test_fill_pattern()
704 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
708 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
712 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
743 dev_err(&spi->dev, in spi_test_fill_pattern()
745 test->fill_option); in spi_test_fill_pattern()
746 return -EINVAL; in spi_test_fill_pattern()
756 void *tx, void *rx) in _spi_test_run_iter() argument
758 struct spi_message *msg = &test->msg; in _spi_test_run_iter()
762 /* initialize message - zero-filled via static initialization */ in _spi_test_run_iter()
765 /* fill rx with the DO_NOT_WRITE pattern */ in _spi_test_run_iter()
766 memset(rx, SPI_TEST_PATTERN_DO_NOT_WRITE, SPI_TEST_MAX_SIZE_PLUS); in _spi_test_run_iter()
769 for (i = 0; i < test->transfer_count; i++) { in _spi_test_run_iter()
770 x = &test->transfers[i]; in _spi_test_run_iter()
773 ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len, in _spi_test_run_iter()
774 (void *)tx, rx); in _spi_test_run_iter()
779 ret = spi_test_translate(spi, &x->rx_buf, x->len, in _spi_test_run_iter()
780 (void *)tx, rx); in _spi_test_run_iter()
794 if (test->execute_msg) in _spi_test_run_iter()
795 ret = test->execute_msg(spi, test, tx, rx); in _spi_test_run_iter()
797 ret = spi_test_execute_msg(spi, test, tx, rx); in _spi_test_run_iter()
800 if (ret == test->expected_return) in _spi_test_run_iter()
803 dev_err(&spi->dev, in _spi_test_run_iter()
804 "test failed - test returned %i, but we expect %i\n", in _spi_test_run_iter()
805 ret, test->expected_return); in _spi_test_run_iter()
813 return -EFAULT; in _spi_test_run_iter()
818 void *tx, void *rx, in spi_test_run_iter() argument
833 if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1))) in spi_test_run_iter()
836 /* count number of transfers with tx/rx_buf != NULL */ in spi_test_run_iter()
849 dev_warn_once(&spi->dev, in spi_test_run_iter()
850 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n", in spi_test_run_iter()
855 dev_warn_once(&spi->dev, in spi_test_run_iter()
856 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n", in spi_test_run_iter()
863 dev_info(&spi->dev, "Running test %s\n", test.description); in spi_test_run_iter()
865 dev_info(&spi->dev, in spi_test_run_iter()
883 return _spi_test_run_iter(spi, &test, tx, rx); in spi_test_run_iter()
887 * spi_test_execute_msg - default implementation to run a test
891 * @tx: the tx buffer allocated for the test sequence
892 * @rx: the rx buffer allocated for the test sequence
897 void *tx, void *rx) in spi_test_execute_msg() argument
899 struct spi_message *msg = &test->msg; in spi_test_execute_msg()
914 test->elapsed_time = ktime_to_ns(ktime_sub(ktime_get(), start)); in spi_test_execute_msg()
915 if (ret == -ETIMEDOUT) { in spi_test_execute_msg()
916 dev_info(&spi->dev, in spi_test_execute_msg()
917 "spi-message timed out - rerunning...\n"); in spi_test_execute_msg()
924 dev_err(&spi->dev, in spi_test_execute_msg()
931 if (msg->frame_length != msg->actual_length) { in spi_test_execute_msg()
932 dev_err(&spi->dev, in spi_test_execute_msg()
934 ret = -EIO; in spi_test_execute_msg()
938 /* run rx-buffer tests */ in spi_test_execute_msg()
939 ret = spi_test_check_loopback_result(spi, msg, tx, rx); in spi_test_execute_msg()
957 * spi_test_run_test - run an individual spi_test
963 * @tx: the tx buffer allocated for the test sequence
964 * @rx: the rx buffer allocated for the test sequence
970 void *tx, void *rx) in spi_test_run_test() argument
978 if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) { in spi_test_run_test()
979 dev_err(&spi->dev, in spi_test_run_test()
981 test->description, test->transfer_count); in spi_test_run_test()
982 return -E2BIG; in spi_test_run_test()
995 var < (test->iterate_##var ? \ in spi_test_run_test()
996 (spi->master->dma_alignment ? \ in spi_test_run_test()
997 spi->master->dma_alignment : \ in spi_test_run_test()
998 test->iterate_##var) : \ in spi_test_run_test()
1003 (len = test->iterate_len[idx_len]) != -1; idx_len++) { in spi_test_run_test()
1008 tx, rx, in spi_test_run_test()
1023 * spi_test_run_tests - run an array of spi_messages tests
1025 * @tests: NULL-terminated array of @spi_test
1033 char *rx = NULL, *tx = NULL; in spi_test_run_tests() local
1037 /* allocate rx/tx buffers of 128kB size without devm in spi_test_run_tests()
1041 rx = vmalloc(SPI_TEST_MAX_SIZE_PLUS); in spi_test_run_tests()
1043 rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL); in spi_test_run_tests()
1044 if (!rx) in spi_test_run_tests()
1045 return -ENOMEM; in spi_test_run_tests()
1049 tx = vmalloc(SPI_TEST_MAX_SIZE_PLUS); in spi_test_run_tests()
1051 tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL); in spi_test_run_tests()
1052 if (!tx) { in spi_test_run_tests()
1053 ret = -ENOMEM; in spi_test_run_tests()
1058 for (test = tests, count = 0; test->description[0]; in spi_test_run_tests()
1061 if ((run_only_test > -1) && (count != run_only_test)) in spi_test_run_tests()
1064 if (test->run_test) in spi_test_run_tests()
1065 ret = test->run_test(spi, test, tx, rx); in spi_test_run_tests()
1067 ret = spi_test_run_test(spi, test, tx, rx); in spi_test_run_tests()
1079 kvfree(tx); in spi_test_run_tests()
1081 kvfree(rx); in spi_test_run_tests()