Lines Matching +full:no +full:- +full:loopback +full:- +full:test
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/spi/spi-loopback-test.c
7 * Loopback test driver to test several typical spi_message conditions
23 #include "spi-test.h"
37 /* the device is jumpered for loopback - enabling some rx_buf tests */
38 static int loopback; variable
39 module_param(loopback, int, 0);
40 MODULE_PARM_DESC(loopback,
41 "if set enable loopback mode, where the rx_buf " \
48 "if set controller will be asked to enable test loop mode. " \
57 static int run_only_iter_len = -1;
62 /* run only a specific test */
63 static int run_only_test = -1;
66 "only run the test with this number (0-based !)");
88 .description = "tx/rx-transfer - start of page",
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",
128 .description = "rx-transfer - only",
140 .description = "two tx-transfers - alter both",
157 .description = "two tx-transfers - alter first",
174 .description = "two tx-transfers - alter second",
191 .description = "two transfers tx then rx - alter both",
207 .description = "two transfers tx then rx - alter tx",
224 .description = "two transfers tx then rx - alter rx",
241 .description = "two tx+rx transfers - alter both",
262 .description = "two tx+rx transfers - alter first",
283 .description = "two tx+rx transfers - alter second",
303 .description = "two tx+rx transfers - delay after transfer",
334 .iterate_len = { 512, -1 },
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",
407 MODULE_DESCRIPTION("test spi_driver to check core functionality");
410 /*-------------------------------------------------------------------------*/
434 pr_info("%s truncated - continuing at offset %04zx\n", in spi_test_print_hex_dump()
435 pre, len - 512); in spi_test_print_hex_dump()
438 ptr + (len - 512), 512, 0); in spi_test_print_hex_dump()
449 dev_info(&spi->dev, " spi_msg@%pK\n", msg); in spi_test_dump_message()
450 if (msg->status) in spi_test_dump_message()
451 dev_info(&spi->dev, " status: %i\n", in spi_test_dump_message()
452 msg->status); in spi_test_dump_message()
453 dev_info(&spi->dev, " frame_length: %i\n", in spi_test_dump_message()
454 msg->frame_length); in spi_test_dump_message()
455 dev_info(&spi->dev, " actual_length: %i\n", in spi_test_dump_message()
456 msg->actual_length); in spi_test_dump_message()
458 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_test_dump_message()
459 dev_info(&spi->dev, " spi_transfer@%pK\n", xfer); in spi_test_dump_message()
460 dev_info(&spi->dev, " len: %i\n", xfer->len); in spi_test_dump_message()
461 dev_info(&spi->dev, " tx_buf: %pK\n", xfer->tx_buf); in spi_test_dump_message()
462 if (dump_data && xfer->tx_buf) in spi_test_dump_message()
464 xfer->tx_buf, in spi_test_dump_message()
465 xfer->len); in spi_test_dump_message()
467 dev_info(&spi->dev, " rx_buf: %pK\n", xfer->rx_buf); in spi_test_dump_message()
468 if (dump_data && xfer->rx_buf) in spi_test_dump_message()
470 xfer->rx_buf, in spi_test_dump_message()
471 xfer->len); in spi_test_dump_message()
472 /* check for unwritten test pattern on rx_buf */ in spi_test_dump_message()
473 if (xfer->rx_buf) { in spi_test_dump_message()
474 for (i = 0 ; i < xfer->len ; i++) { in spi_test_dump_message()
475 b = ((u8 *)xfer->rx_buf)[xfer->len - 1 - i]; in spi_test_dump_message()
480 dev_info(&spi->dev, in spi_test_dump_message()
483 xfer->len - i); in spi_test_dump_message()
500 if (rx_a->start > rx_b->start) in rx_ranges_cmp()
502 if (rx_a->start < rx_b->start) in rx_ranges_cmp()
503 return -1; in rx_ranges_cmp()
519 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_check_rx_ranges()
520 /* if there is no rx, then no check is needed */ in spi_check_rx_ranges()
521 if (!xfer->rx_buf) in spi_check_rx_ranges()
524 if (RANGE_CHECK(xfer->rx_buf, xfer->len, in spi_check_rx_ranges()
526 ranges[i].start = xfer->rx_buf; in spi_check_rx_ranges()
527 ranges[i].end = xfer->rx_buf + xfer->len; in spi_check_rx_ranges()
533 /* if no ranges, then we can return and avoid the checks...*/ in spi_check_rx_ranges()
551 if ((addr >= r->start) && (addr < r->end)) in spi_check_rx_ranges()
552 addr = r->end; in spi_check_rx_ranges()
554 /* second test after a (hopefull) translation */ in spi_check_rx_ranges()
560 dev_err(&spi->dev, in spi_check_rx_ranges()
561 "loopback strangeness - rx changed outside of allowed range at: %pK\n", in spi_check_rx_ranges()
566 ret = -ERANGE; in spi_check_rx_ranges()
573 struct spi_test *test) in spi_test_check_elapsed_time() argument
579 for (i = 0; i < test->transfer_count; i++) { in spi_test_check_elapsed_time()
580 struct spi_transfer *xfer = test->transfers + i; in spi_test_check_elapsed_time()
582 xfer->len; in spi_test_check_elapsed_time()
584 delay_usecs += xfer->delay.value; in spi_test_check_elapsed_time()
585 if (!xfer->speed_hz) in spi_test_check_elapsed_time()
587 estimated_time += div_u64(nbits * NSEC_PER_SEC, xfer->speed_hz); in spi_test_check_elapsed_time()
591 if (test->elapsed_time < estimated_time) { in spi_test_check_elapsed_time()
592 dev_err(&spi->dev, in spi_test_check_elapsed_time()
594 test->elapsed_time, estimated_time); in spi_test_check_elapsed_time()
596 return -EINVAL; in spi_test_check_elapsed_time()
611 /* checks rx_buffer pattern are valid with loopback or without */ in spi_test_check_loopback_result()
618 /* if we run without loopback, then return now */ in spi_test_check_loopback_result()
619 if (!loopback) in spi_test_check_loopback_result()
623 list_for_each_entry(xfer, &msg->transfers, transfer_list) { in spi_test_check_loopback_result()
624 /* if there is no rx, then no check is needed */ in spi_test_check_loopback_result()
625 if (!xfer->len || !xfer->rx_buf) in spi_test_check_loopback_result()
628 if (xfer->tx_buf) { in spi_test_check_loopback_result()
629 for (i = 0; i < xfer->len; i++) { in spi_test_check_loopback_result()
630 txb = ((u8 *)xfer->tx_buf)[i]; in spi_test_check_loopback_result()
631 rxb = ((u8 *)xfer->rx_buf)[i]; in spi_test_check_loopback_result()
637 txb = ((u8 *)xfer->rx_buf)[0]; in spi_test_check_loopback_result()
640 dev_err(&spi->dev, in spi_test_check_loopback_result()
641 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n", in spi_test_check_loopback_result()
643 return -EINVAL; in spi_test_check_loopback_result()
646 for (i = 1; i < xfer->len; i++) { in spi_test_check_loopback_result()
647 rxb = ((u8 *)xfer->rx_buf)[i]; in spi_test_check_loopback_result()
657 dev_err(&spi->dev, in spi_test_check_loopback_result()
658 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n", in spi_test_check_loopback_result()
661 return -EINVAL; in spi_test_check_loopback_result()
677 *ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) - in spi_test_translate()
681 * - we check against MAX_SIZE_PLUS to allow for automated alignment in spi_test_translate()
684 off = *ptr - RX(0); in spi_test_translate()
692 off = *ptr - TX(0); in spi_test_translate()
698 dev_err(&spi->dev, in spi_test_translate()
704 return -EINVAL; in spi_test_translate()
708 struct spi_test *test) in spi_test_fill_pattern() argument
710 struct spi_transfer *xfers = test->transfers; in spi_test_fill_pattern()
717 (value >> (8 * (bytes - 1 - count % bytes))) in spi_test_fill_pattern()
724 for (i = 0; i < test->transfer_count; i++) { in spi_test_fill_pattern()
736 switch (test->fill_option) { in spi_test_fill_pattern()
738 *tx_buf = test->fill_pattern; in spi_test_fill_pattern()
741 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
745 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
749 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, in spi_test_fill_pattern()
780 dev_err(&spi->dev, in spi_test_fill_pattern()
782 test->fill_option); in spi_test_fill_pattern()
783 return -EINVAL; in spi_test_fill_pattern()
792 struct spi_test *test, in _spi_test_run_iter() argument
795 struct spi_message *msg = &test->msg; in _spi_test_run_iter()
799 /* initialize message - zero-filled via static initialization */ in _spi_test_run_iter()
806 for (i = 0; i < test->transfer_count; i++) { in _spi_test_run_iter()
807 x = &test->transfers[i]; in _spi_test_run_iter()
810 ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len, in _spi_test_run_iter()
816 ret = spi_test_translate(spi, &x->rx_buf, x->len, in _spi_test_run_iter()
826 ret = spi_test_fill_pattern(spi, test); in _spi_test_run_iter()
831 if (test->execute_msg) in _spi_test_run_iter()
832 ret = test->execute_msg(spi, test, tx, rx); in _spi_test_run_iter()
834 ret = spi_test_execute_msg(spi, test, tx, rx); in _spi_test_run_iter()
837 if (ret == test->expected_return) in _spi_test_run_iter()
840 dev_err(&spi->dev, in _spi_test_run_iter()
841 "test failed - test returned %i, but we expect %i\n", in _spi_test_run_iter()
842 ret, test->expected_return); in _spi_test_run_iter()
850 return -EFAULT; in _spi_test_run_iter()
861 struct spi_test test; in spi_test_run_iter() local
864 /* copy the test template to test */ in spi_test_run_iter()
865 memcpy(&test, testtemplate, sizeof(test)); in spi_test_run_iter()
870 if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1))) in spi_test_run_iter()
871 test.iterate_transfer_mask = 1; in spi_test_run_iter()
875 for (i = 0; i < test.transfer_count; i++) { in spi_test_run_iter()
876 if (test.transfers[i].tx_buf) in spi_test_run_iter()
878 if (test.transfers[i].rx_buf) in spi_test_run_iter()
886 dev_warn_once(&spi->dev, in spi_test_run_iter()
887 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n", in spi_test_run_iter()
888 test.description); in spi_test_run_iter()
892 dev_warn_once(&spi->dev, in spi_test_run_iter()
893 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n", in spi_test_run_iter()
894 test.description); in spi_test_run_iter()
900 dev_info(&spi->dev, "Running test %s\n", test.description); in spi_test_run_iter()
902 dev_info(&spi->dev, in spi_test_run_iter()
908 for (i = 0; i < test.transfer_count; i++) { in spi_test_run_iter()
910 if (!(test.iterate_transfer_mask & BIT(i))) in spi_test_run_iter()
912 test.transfers[i].len = len; in spi_test_run_iter()
913 if (test.transfers[i].tx_buf) in spi_test_run_iter()
914 test.transfers[i].tx_buf += tx_off; in spi_test_run_iter()
915 if (test.transfers[i].rx_buf) in spi_test_run_iter()
916 test.transfers[i].rx_buf += rx_off; in spi_test_run_iter()
920 return _spi_test_run_iter(spi, &test, tx, rx); in spi_test_run_iter()
924 * spi_test_execute_msg - default implementation to run a test
927 * @test: the test to execute, which already contains @msg
928 * @tx: the tx buffer allocated for the test sequence
929 * @rx: the rx buffer allocated for the test sequence
933 int spi_test_execute_msg(struct spi_device *spi, struct spi_test *test, in spi_test_execute_msg() argument
936 struct spi_message *msg = &test->msg; in spi_test_execute_msg()
951 test->elapsed_time = ktime_to_ns(ktime_sub(ktime_get(), start)); in spi_test_execute_msg()
952 if (ret == -ETIMEDOUT) { in spi_test_execute_msg()
953 dev_info(&spi->dev, in spi_test_execute_msg()
954 "spi-message timed out - rerunning...\n"); in spi_test_execute_msg()
961 dev_err(&spi->dev, in spi_test_execute_msg()
968 if (msg->frame_length != msg->actual_length) { in spi_test_execute_msg()
969 dev_err(&spi->dev, in spi_test_execute_msg()
971 ret = -EIO; in spi_test_execute_msg()
975 /* run rx-buffer tests */ in spi_test_execute_msg()
980 ret = spi_test_check_elapsed_time(spi, test); in spi_test_execute_msg()
994 * spi_test_run_test - run an individual spi_test
999 * @test: the test which we need to execute
1000 * @tx: the tx buffer allocated for the test sequence
1001 * @rx: the rx buffer allocated for the test sequence
1006 int spi_test_run_test(struct spi_device *spi, const struct spi_test *test, in spi_test_run_test() argument
1014 /* test for transfer limits */ in spi_test_run_test()
1015 if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) { in spi_test_run_test()
1016 dev_err(&spi->dev, in spi_test_run_test()
1018 test->description, test->transfer_count); in spi_test_run_test()
1019 return -E2BIG; in spi_test_run_test()
1032 var < (test->iterate_##var ? \ in spi_test_run_test()
1033 (spi->controller->dma_alignment ? \ in spi_test_run_test()
1034 spi->controller->dma_alignment : \ in spi_test_run_test()
1035 test->iterate_##var) : \ in spi_test_run_test()
1040 (len = test->iterate_len[idx_len]) != -1; idx_len++) { in spi_test_run_test()
1041 if ((run_only_iter_len > -1) && len != run_only_iter_len) in spi_test_run_test()
1046 ret = spi_test_run_iter(spi, test, in spi_test_run_test()
1062 * spi_test_run_tests - run an array of spi_messages tests
1064 * @tests: NULL-terminated array of @spi_test
1074 struct spi_test *test; in spi_test_run_tests() local
1084 return -ENOMEM; in spi_test_run_tests()
1092 ret = -ENOMEM; in spi_test_run_tests()
1097 for (test = tests, count = 0; test->description[0]; in spi_test_run_tests()
1098 test++, count++) { in spi_test_run_tests()
1099 /* only run test if requested */ in spi_test_run_tests()
1100 if ((run_only_test > -1) && (count != run_only_test)) in spi_test_run_tests()
1103 if (test->run_test) in spi_test_run_tests()
1104 ret = test->run_test(spi, test, tx, rx); in spi_test_run_tests()
1106 ret = spi_test_run_test(spi, test, tx, rx); in spi_test_run_tests()