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. " \
56 /* run only a specific test */
57 static int run_only_test = -1;
60 "only run the test with this number (0-based !)");
77 .description = "tx/rx-transfer - start of page",
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",
117 .description = "rx-transfer - only",
129 .description = "two tx-transfers - alter both",
146 .description = "two tx-transfers - alter first",
163 .description = "two tx-transfers - alter second",
180 .description = "two transfers tx then rx - alter both",
196 .description = "two transfers tx then rx - alter tx",
213 .description = "two transfers tx then rx - alter rx",
230 .description = "two tx+rx transfers - alter both",
251 .description = "two tx+rx transfers - alter first",
272 .description = "two tx+rx transfers - alter second",
292 .description = "two tx+rx transfers - delay after transfer",
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",
370 MODULE_DESCRIPTION("test spi_driver to check core functionality");
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()
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()
433 xfer->rx_buf, in spi_test_dump_message()
434 xfer->len); in spi_test_dump_message()
435 /* check for unwritten test pattern on rx_buf */ 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()
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()
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()
495 /* if no ranges, then we can return and avoid the checks...*/ 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()
516 /* second test after a (hopefull) translation */ 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()
535 struct spi_test *test) in spi_test_check_elapsed_time() argument
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()
573 /* checks rx_buffer pattern are valid with loopback or without */ in spi_test_check_loopback_result()
580 /* if we run without loopback, then return now */ in spi_test_check_loopback_result()
581 if (!loopback) 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()
639 *ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) - in spi_test_translate()
643 * - we check against MAX_SIZE_PLUS to allow for automated alignment in spi_test_translate()
646 off = *ptr - RX(0); in spi_test_translate()
654 off = *ptr - TX(0); in spi_test_translate()
660 dev_err(&spi->dev, in spi_test_translate()
666 return -EINVAL; in spi_test_translate()
670 struct spi_test *test) in spi_test_fill_pattern() argument
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()
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()
754 struct spi_test *test, 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()
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()
778 ret = spi_test_translate(spi, &x->rx_buf, x->len, in _spi_test_run_iter()
788 ret = spi_test_fill_pattern(spi, test); 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()
823 struct spi_test test; in spi_test_run_iter() local
826 /* copy the test template to test */ in spi_test_run_iter()
827 memcpy(&test, testtemplate, sizeof(test)); in spi_test_run_iter()
832 if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1))) in spi_test_run_iter()
833 test.iterate_transfer_mask = 1; in spi_test_run_iter()
837 for (i = 0; i < test.transfer_count; i++) { in spi_test_run_iter()
838 if (test.transfers[i].tx_buf) in spi_test_run_iter()
840 if (test.transfers[i].rx_buf) 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()
850 test.description); 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()
856 test.description); 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()
870 for (i = 0; i < test.transfer_count; i++) { in spi_test_run_iter()
872 if (!(test.iterate_transfer_mask & BIT(i))) in spi_test_run_iter()
874 test.transfers[i].len = len; in spi_test_run_iter()
875 if (test.transfers[i].tx_buf) in spi_test_run_iter()
876 test.transfers[i].tx_buf += tx_off; in spi_test_run_iter()
877 if (test.transfers[i].tx_buf) in spi_test_run_iter()
878 test.transfers[i].rx_buf += rx_off; 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
889 * @test: the test to execute, which already contains @msg
890 * @tx: the tx buffer allocated for the test sequence
891 * @rx: the rx buffer allocated for the test sequence
895 int spi_test_execute_msg(struct spi_device *spi, struct spi_test *test, 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()
942 ret = spi_test_check_elapsed_time(spi, test); in spi_test_execute_msg()
956 * spi_test_run_test - run an individual spi_test
961 * @test: the test which we need to execute
962 * @tx: the tx buffer allocated for the test sequence
963 * @rx: the rx buffer allocated for the test sequence
968 int spi_test_run_test(struct spi_device *spi, const struct spi_test *test, in spi_test_run_test() argument
976 /* test for transfer limits */ in spi_test_run_test()
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()
1006 ret = spi_test_run_iter(spi, test, 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
1034 struct spi_test *test; in spi_test_run_tests() local
1044 return -ENOMEM; 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()
1058 test++, count++) { in spi_test_run_tests()
1059 /* only run test if requested */ 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()