// SPDX-License-Identifier: GPL-2.0-or-later /* * linux/drivers/spi/spi-loopback-test.c * * (c) Martin Sperl <kernel@martin.sperl.org> * * Loopback test driver to test several typical spi_message conditions * that a spi_master driver may encounter * this can also get used for regression testing
*/
/* flag to only simulate transfers */ staticint simulate_only;
module_param(simulate_only, int, 0);
MODULE_PARM_DESC(simulate_only, "if not 0 do not execute the spi message");
/* dump spi messages */ staticint dump_messages;
module_param(dump_messages, int, 0);
MODULE_PARM_DESC(dump_messages, "=1 dump the basic spi_message_structure, " \ "=2 dump the spi_message_structure including data, " \ "=3 dump the spi_message structure before and after execution"); /* the device is jumpered for loopback - enabling some rx_buf tests */ staticint loopback;
module_param(loopback, int, 0);
MODULE_PARM_DESC(loopback, "if set enable loopback mode, where the rx_buf " \ "is checked to match tx_buf after the spi_message " \ "is executed");
staticint loop_req;
module_param(loop_req, int, 0);
MODULE_PARM_DESC(loop_req, "if set controller will be asked to enable test loop mode. " \ "If controller supported it, MISO and MOSI will be connected");
staticint no_cs;
module_param(no_cs, int, 0);
MODULE_PARM_DESC(no_cs, "if set Chip Select (CS) will not be used");
/* run tests only for a specific length */ staticint run_only_iter_len = -1;
module_param(run_only_iter_len, int, 0);
MODULE_PARM_DESC(run_only_iter_len, "only run tests for a length of this number in iterate_len list");
/* run only a specific test */ staticint run_only_test = -1;
module_param(run_only_test, int, 0);
MODULE_PARM_DESC(run_only_test, "only run the test with this number (0-based !)");
/* use vmalloc'ed buffers */ staticint use_vmalloc;
module_param(use_vmalloc, int, 0644);
MODULE_PARM_DESC(use_vmalloc, "use vmalloc'ed buffers instead of kmalloc'ed");
dev_info(&spi->dev, "Finished spi-loopback-tests with return: %i\n",
ret);
return ret;
}
/* non const match table to permit to change via a module parameter */ staticstruct of_device_id spi_loopback_test_of_match[] = {
{ .compatible = "linux,spi-loopback-test", },
{ }
};
/* allow to override the compatible string via a module_parameter */
module_param_string(compatible, spi_loopback_test_of_match[0].compatible, sizeof(spi_loopback_test_of_match[0].compatible),
0000);
if (rx_a->start > rx_b->start) return 1; if (rx_a->start < rx_b->start) return -1; return 0;
}
staticint spi_check_rx_ranges(struct spi_device *spi, struct spi_message *msg, void *rx)
{ struct spi_transfer *xfer; struct rx_ranges ranges[SPI_TEST_MAX_TRANSFERS], *r; int i = 0;
LIST_HEAD(ranges_list);
u8 *addr; int ret = 0;
/* loop over all transfers to fill in the rx_ranges */
list_for_each_entry(xfer, &msg->transfers, transfer_list) { /* if there is no rx, then no check is needed */ if (!xfer->rx_buf) continue; /* fill in the rx_range */ if (RANGE_CHECK(xfer->rx_buf, xfer->len,
rx, SPI_TEST_MAX_SIZE_PLUS)) {
ranges[i].start = xfer->rx_buf;
ranges[i].end = xfer->rx_buf + xfer->len;
list_add(&ranges[i].list, &ranges_list);
i++;
}
}
/* if no ranges, then we can return and avoid the checks...*/ if (!i) return 0;
/* sort the list */
list_sort(NULL, &ranges_list, rx_ranges_cmp);
/* and iterate over all the rx addresses */ for (addr = rx; addr < (u8 *)rx + SPI_TEST_MAX_SIZE_PLUS; addr++) { /* if we are the DO not write pattern, * then continue with the loop...
*/ if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE) continue;
/* check if we are inside a range */
list_for_each_entry(r, &ranges_list, list) { /* if so then set to end... */ if ((addr >= r->start) && (addr < r->end))
addr = r->end;
} /* second test after a (hopefull) translation */ if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE) continue;
/* if still not found then something has modified too much */ /* we could list the "closest" transfer here... */
dev_err(&spi->dev, "loopback strangeness - rx changed outside of allowed range at: %p\n",
addr); /* do not return, only set ret, * so that we list all addresses
*/
ret = -ERANGE;
}
estimated_time += delay_usecs * NSEC_PER_USEC; if (test->elapsed_time < estimated_time) {
dev_err(&spi->dev, "elapsed time %lld ns is shorter than minimum estimated time %lld ns\n",
test->elapsed_time, estimated_time);
/* checks rx_buffer pattern are valid with loopback or without */ if (check_ranges) {
ret = spi_check_rx_ranges(spi, msg, rx); if (ret) return ret;
}
/* if we run without loopback, then return now */ if (!loopback) return 0;
/* if applicable to transfer check that rx_buf is equal to tx_buf */
list_for_each_entry(xfer, &msg->transfers, transfer_list) { /* if there is no rx, then no check is needed */ if (!xfer->len || !xfer->rx_buf) continue; /* so depending on tx_buf we need to handle things */ if (xfer->tx_buf) { for (i = 0; i < xfer->len; i++) {
txb = ((u8 *)xfer->tx_buf)[i];
rxb = ((u8 *)xfer->rx_buf)[i]; if (txb != rxb) goto mismatch_error;
}
} else { /* first byte received */
txb = ((u8 *)xfer->rx_buf)[0]; /* first byte may be 0 or 0xff */ if (txb != 0 && txb != 0xff) {
dev_err(&spi->dev, "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n",
txb); return -EINVAL;
} /* check that all bytes are identical */ for (i = 1; i < xfer->len; i++) {
rxb = ((u8 *)xfer->rx_buf)[i]; if (rxb != txb) goto mismatch_error;
}
}
}
return 0;
mismatch_error:
dev_err(&spi->dev, "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n",
i, txb, rxb);
/* in the MAX_SIZE_HALF case modify the pointer */ if (((size_t)*ptr) & SPI_TEST_MAX_SIZE_HALF) /* move the pointer to the correct range */
*ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) -
SPI_TEST_MAX_SIZE_HALF;
/* RX range * - we check against MAX_SIZE_PLUS to allow for automated alignment
*/ if (RANGE_CHECK(*ptr, len, RX(0), SPI_TEST_MAX_SIZE_PLUS)) {
off = *ptr - RX(0);
*ptr = rx + off;
return 0;
}
/* TX range */ if (RANGE_CHECK(*ptr, len, TX(0), SPI_TEST_MAX_SIZE_PLUS)) {
off = *ptr - TX(0);
*ptr = tx + off;
return 0;
}
dev_err(&spi->dev, "PointerRange [%p:%p[ not in range [%p:%p[ or [%p:%p[\n",
*ptr, *ptr + len,
RX(0), RX(SPI_TEST_MAX_SIZE),
TX(0), TX(SPI_TEST_MAX_SIZE));
/* copy the test template to test */
memcpy(&test, testtemplate, sizeof(test));
/* if iterate_transfer_mask is not set, * then set it to first transfer only
*/ if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1)))
test.iterate_transfer_mask = 1;
/* count number of transfers with tx/rx_buf != NULL */
rx_count = tx_count = 0; for (i = 0; i < test.transfer_count; i++) { if (test.transfers[i].tx_buf)
tx_count++; if (test.transfers[i].rx_buf)
rx_count++;
}
/* in some iteration cases warn and exit early, * as there is nothing to do, that has not been tested already...
*/ if (tx_off && (!tx_count)) {
dev_warn_once(&spi->dev, "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n",
test.description); return 0;
} if (rx_off && (!rx_count)) {
dev_warn_once(&spi->dev, "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n",
test.description); return 0;
}
/* write out info */ if (!(len || tx_off || rx_off)) {
dev_info(&spi->dev, "Running test %s\n", test.description);
} else {
dev_info(&spi->dev, " with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n",
len, tx_off, rx_off);
}
/* update in the values from iteration values */ for (i = 0; i < test.transfer_count; i++) { /* only when bit in transfer mask is set */ if (!(test.iterate_transfer_mask & BIT(i))) continue;
test.transfers[i].len = len; if (test.transfers[i].tx_buf)
test.transfers[i].tx_buf += tx_off; if (test.transfers[i].rx_buf)
test.transfers[i].rx_buf += rx_off;
}
/* and execute */ return _spi_test_run_iter(spi, &test, tx, rx);
}
/** * spi_test_execute_msg - default implementation to run a test * * @spi: @spi_device on which to run the @spi_message * @test: the test to execute, which already contains @msg * @tx: the tx buffer allocated for the test sequence * @rx: the rx buffer allocated for the test sequence * * Returns: error code of spi_sync as well as basic error checking
*/ int spi_test_execute_msg(struct spi_device *spi, struct spi_test *test, void *tx, void *rx)
{ struct spi_message *msg = &test->msg; int ret = 0; int i;
/* only if we do not simulate */ if (!simulate_only) {
ktime_t start;
/* dump the complete message before and after the transfer */ if (dump_messages == 3)
spi_test_dump_message(spi, msg, true);
start = ktime_get(); /* run spi message */
ret = spi_sync(spi, msg);
test->elapsed_time = ktime_to_ns(ktime_sub(ktime_get(), start)); if (ret == -ETIMEDOUT) {
dev_info(&spi->dev, "spi-message timed out - rerunning...\n"); /* rerun after a few explicit schedules */ for (i = 0; i < 16; i++)
schedule();
ret = spi_sync(spi, msg);
} if (ret) {
dev_err(&spi->dev, "Failed to execute spi_message: %i\n",
ret); gotoexit;
}
/* do some extra error checks */ if (msg->frame_length != msg->actual_length) {
dev_err(&spi->dev, "actual length differs from expected\n");
ret = -EIO; gotoexit;
}
/* run rx-buffer tests */
ret = spi_test_check_loopback_result(spi, msg, tx, rx); if (ret) gotoexit;
ret = spi_test_check_elapsed_time(spi, test);
}
/* if requested or on error dump message (including data) */ exit: if (dump_messages || ret)
spi_test_dump_message(spi, msg,
(dump_messages >= 2) || (ret));
/** * spi_test_run_test - run an individual spi_test * including all the relevant iterations on: * length and buffer alignment * * @spi: the spi_device to send the messages to * @test: the test which we need to execute * @tx: the tx buffer allocated for the test sequence * @rx: the rx buffer allocated for the test sequence * * Returns: status code of spi_sync or other failures
*/
int spi_test_run_test(struct spi_device *spi, conststruct spi_test *test, void *tx, void *rx)
{ int idx_len;
size_t len;
size_t tx_align, rx_align; int ret;
/* test for transfer limits */ if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) {
dev_err(&spi->dev, "%s: Exceeded max number of transfers with %i\n",
test->description, test->transfer_count); return -E2BIG;
}
/* setting up some values in spi_message * based on some settings in spi_master * some of this can also get done in the run() method
*/
/* iterate over all the iterable values using macros * (to make it a bit more readable...
*/ #define FOR_EACH_ALIGNMENT(var) \ for (var = 0; \
var < (test->iterate_##var ? \
(spi->controller->dma_alignment ? \
spi->controller->dma_alignment : \
test->iterate_##var) : \
1); \
var++)
for (idx_len = 0; idx_len < SPI_TEST_MAX_ITERATE &&
(len = test->iterate_len[idx_len]) != -1; idx_len++) { if ((run_only_iter_len > -1) && len != run_only_iter_len) continue;
FOR_EACH_ALIGNMENT(tx_align) {
FOR_EACH_ALIGNMENT(rx_align) { /* and run the iteration */
ret = spi_test_run_iter(spi, test,
tx, rx,
len,
tx_align,
rx_align); if (ret) return ret;
}
}
}
return 0;
}
EXPORT_SYMBOL_GPL(spi_test_run_test);
/** * spi_test_run_tests - run an array of spi_messages tests * @spi: the spi device on which to run the tests * @tests: NULL-terminated array of @spi_test * * Returns: status errors as per @spi_test_run_test()
*/
int spi_test_run_tests(struct spi_device *spi, struct spi_test *tests)
{ char *rx = NULL, *tx = NULL; int ret = 0, count = 0; struct spi_test *test;
/* allocate rx/tx buffers of 128kB size without devm * in the hope that is on a page boundary
*/ if (use_vmalloc)
rx = vmalloc(SPI_TEST_MAX_SIZE_PLUS); else
rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL); if (!rx) return -ENOMEM;
if (use_vmalloc)
tx = vmalloc(SPI_TEST_MAX_SIZE_PLUS); else
tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL); if (!tx) {
ret = -ENOMEM; goto err_tx;
}
/* now run the individual tests in the table */ for (test = tests, count = 0; test->description[0];
test++, count++) { /* only run test if requested */ if ((run_only_test > -1) && (count != run_only_test)) continue; /* run custom implementation */ if (test->run_test)
ret = test->run_test(spi, test, tx, rx); else
ret = spi_test_run_test(spi, test, tx, rx); if (ret) goto out; /* add some delays so that we can easily * detect the individual tests when using a logic analyzer * we also add scheduling to avoid potential spi_timeouts...
*/ if (delay_ms)
mdelay(delay_ms);
schedule();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.