1 /* 2 * linux/drivers/spi/spi-loopback-test.c 3 * 4 * (c) Martin Sperl <[email protected]> 5 * 6 * Loopback test driver to test several typical spi_message conditions 7 * that a spi_master driver may encounter 8 * this can also get used for regression testing 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 #include <linux/delay.h> 22 #include <linux/kernel.h> 23 #include <linux/ktime.h> 24 #include <linux/list.h> 25 #include <linux/list_sort.h> 26 #include <linux/module.h> 27 #include <linux/of_device.h> 28 #include <linux/printk.h> 29 #include <linux/vmalloc.h> 30 #include <linux/spi/spi.h> 31 32 #include "spi-test.h" 33 34 /* flag to only simulate transfers */ 35 int simulate_only; 36 module_param(simulate_only, int, 0); 37 MODULE_PARM_DESC(simulate_only, "if not 0 do not execute the spi message"); 38 39 /* dump spi messages */ 40 int dump_messages; 41 module_param(dump_messages, int, 0); 42 MODULE_PARM_DESC(dump_messages, 43 "=1 dump the basic spi_message_structure, " \ 44 "=2 dump the spi_message_structure including data, " \ 45 "=3 dump the spi_message structure before and after execution"); 46 /* the device is jumpered for loopback - enabling some rx_buf tests */ 47 int loopback; 48 module_param(loopback, int, 0); 49 MODULE_PARM_DESC(loopback, 50 "if set enable loopback mode, where the rx_buf " \ 51 "is checked to match tx_buf after the spi_message " \ 52 "is executed"); 53 54 int loop_req; 55 module_param(loop_req, int, 0); 56 MODULE_PARM_DESC(loop_req, 57 "if set controller will be asked to enable test loop mode. " \ 58 "If controller supported it, MISO and MOSI will be connected"); 59 60 /* run only a specific test */ 61 int run_only_test = -1; 62 module_param(run_only_test, int, 0); 63 MODULE_PARM_DESC(run_only_test, 64 "only run the test with this number (0-based !)"); 65 66 /* use vmalloc'ed buffers */ 67 int use_vmalloc; 68 module_param(use_vmalloc, int, 0644); 69 MODULE_PARM_DESC(use_vmalloc, 70 "use vmalloc'ed buffers instead of kmalloc'ed"); 71 72 /* check rx ranges */ 73 int check_ranges = 1; 74 module_param(check_ranges, int, 0644); 75 MODULE_PARM_DESC(check_ranges, 76 "checks rx_buffer pattern are valid"); 77 78 /* the actual tests to execute */ 79 static struct spi_test spi_tests[] = { 80 { 81 .description = "tx/rx-transfer - start of page", 82 .fill_option = FILL_COUNT_8, 83 .iterate_len = { ITERATE_MAX_LEN }, 84 .iterate_tx_align = ITERATE_ALIGN, 85 .iterate_rx_align = ITERATE_ALIGN, 86 .transfer_count = 1, 87 .transfers = { 88 { 89 .tx_buf = TX(0), 90 .rx_buf = RX(0), 91 }, 92 }, 93 }, 94 { 95 .description = "tx/rx-transfer - crossing PAGE_SIZE", 96 .fill_option = FILL_COUNT_8, 97 .iterate_len = { ITERATE_MAX_LEN }, 98 .iterate_tx_align = ITERATE_ALIGN, 99 .iterate_rx_align = ITERATE_ALIGN, 100 .transfer_count = 1, 101 .transfers = { 102 { 103 .tx_buf = TX(PAGE_SIZE - 4), 104 .rx_buf = RX(PAGE_SIZE - 4), 105 }, 106 }, 107 }, 108 { 109 .description = "tx-transfer - only", 110 .fill_option = FILL_COUNT_8, 111 .iterate_len = { ITERATE_MAX_LEN }, 112 .iterate_tx_align = ITERATE_ALIGN, 113 .transfer_count = 1, 114 .transfers = { 115 { 116 .tx_buf = TX(0), 117 }, 118 }, 119 }, 120 { 121 .description = "rx-transfer - only", 122 .fill_option = FILL_COUNT_8, 123 .iterate_len = { ITERATE_MAX_LEN }, 124 .iterate_rx_align = ITERATE_ALIGN, 125 .transfer_count = 1, 126 .transfers = { 127 { 128 .rx_buf = RX(0), 129 }, 130 }, 131 }, 132 { 133 .description = "two tx-transfers - alter both", 134 .fill_option = FILL_COUNT_8, 135 .iterate_len = { ITERATE_LEN }, 136 .iterate_tx_align = ITERATE_ALIGN, 137 .iterate_transfer_mask = BIT(0) | BIT(1), 138 .transfer_count = 2, 139 .transfers = { 140 { 141 .tx_buf = TX(0), 142 }, 143 { 144 /* this is why we cant use ITERATE_MAX_LEN */ 145 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF), 146 }, 147 }, 148 }, 149 { 150 .description = "two tx-transfers - alter first", 151 .fill_option = FILL_COUNT_8, 152 .iterate_len = { ITERATE_MAX_LEN }, 153 .iterate_tx_align = ITERATE_ALIGN, 154 .iterate_transfer_mask = BIT(0), 155 .transfer_count = 2, 156 .transfers = { 157 { 158 .tx_buf = TX(64), 159 }, 160 { 161 .len = 1, 162 .tx_buf = TX(0), 163 }, 164 }, 165 }, 166 { 167 .description = "two tx-transfers - alter second", 168 .fill_option = FILL_COUNT_8, 169 .iterate_len = { ITERATE_MAX_LEN }, 170 .iterate_tx_align = ITERATE_ALIGN, 171 .iterate_transfer_mask = BIT(1), 172 .transfer_count = 2, 173 .transfers = { 174 { 175 .len = 16, 176 .tx_buf = TX(0), 177 }, 178 { 179 .tx_buf = TX(64), 180 }, 181 }, 182 }, 183 { 184 .description = "two transfers tx then rx - alter both", 185 .fill_option = FILL_COUNT_8, 186 .iterate_len = { ITERATE_MAX_LEN }, 187 .iterate_tx_align = ITERATE_ALIGN, 188 .iterate_transfer_mask = BIT(0) | BIT(1), 189 .transfer_count = 2, 190 .transfers = { 191 { 192 .tx_buf = TX(0), 193 }, 194 { 195 .rx_buf = RX(0), 196 }, 197 }, 198 }, 199 { 200 .description = "two transfers tx then rx - alter tx", 201 .fill_option = FILL_COUNT_8, 202 .iterate_len = { ITERATE_MAX_LEN }, 203 .iterate_tx_align = ITERATE_ALIGN, 204 .iterate_transfer_mask = BIT(0), 205 .transfer_count = 2, 206 .transfers = { 207 { 208 .tx_buf = TX(0), 209 }, 210 { 211 .len = 1, 212 .rx_buf = RX(0), 213 }, 214 }, 215 }, 216 { 217 .description = "two transfers tx then rx - alter rx", 218 .fill_option = FILL_COUNT_8, 219 .iterate_len = { ITERATE_MAX_LEN }, 220 .iterate_tx_align = ITERATE_ALIGN, 221 .iterate_transfer_mask = BIT(1), 222 .transfer_count = 2, 223 .transfers = { 224 { 225 .len = 1, 226 .tx_buf = TX(0), 227 }, 228 { 229 .rx_buf = RX(0), 230 }, 231 }, 232 }, 233 { 234 .description = "two tx+rx transfers - alter both", 235 .fill_option = FILL_COUNT_8, 236 .iterate_len = { ITERATE_LEN }, 237 .iterate_tx_align = ITERATE_ALIGN, 238 .iterate_transfer_mask = BIT(0) | BIT(1), 239 .transfer_count = 2, 240 .transfers = { 241 { 242 .tx_buf = TX(0), 243 .rx_buf = RX(0), 244 }, 245 { 246 /* making sure we align without overwrite 247 * the reason we can not use ITERATE_MAX_LEN 248 */ 249 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF), 250 .rx_buf = RX(SPI_TEST_MAX_SIZE_HALF), 251 }, 252 }, 253 }, 254 { 255 .description = "two tx+rx transfers - alter first", 256 .fill_option = FILL_COUNT_8, 257 .iterate_len = { ITERATE_MAX_LEN }, 258 .iterate_tx_align = ITERATE_ALIGN, 259 .iterate_transfer_mask = BIT(0), 260 .transfer_count = 2, 261 .transfers = { 262 { 263 /* making sure we align without overwrite */ 264 .tx_buf = TX(1024), 265 .rx_buf = RX(1024), 266 }, 267 { 268 .len = 1, 269 /* making sure we align without overwrite */ 270 .tx_buf = TX(0), 271 .rx_buf = RX(0), 272 }, 273 }, 274 }, 275 { 276 .description = "two tx+rx transfers - alter second", 277 .fill_option = FILL_COUNT_8, 278 .iterate_len = { ITERATE_MAX_LEN }, 279 .iterate_tx_align = ITERATE_ALIGN, 280 .iterate_transfer_mask = BIT(1), 281 .transfer_count = 2, 282 .transfers = { 283 { 284 .len = 1, 285 .tx_buf = TX(0), 286 .rx_buf = RX(0), 287 }, 288 { 289 /* making sure we align without overwrite */ 290 .tx_buf = TX(1024), 291 .rx_buf = RX(1024), 292 }, 293 }, 294 }, 295 { 296 .description = "two tx+rx transfers - delay after transfer", 297 .fill_option = FILL_COUNT_8, 298 .iterate_len = { ITERATE_MAX_LEN }, 299 .iterate_transfer_mask = BIT(0) | BIT(1), 300 .transfer_count = 2, 301 .transfers = { 302 { 303 .tx_buf = TX(0), 304 .rx_buf = RX(0), 305 .delay_usecs = 1000, 306 }, 307 { 308 .tx_buf = TX(0), 309 .rx_buf = RX(0), 310 .delay_usecs = 1000, 311 }, 312 }, 313 }, 314 315 { /* end of tests sequence */ } 316 }; 317 318 static int spi_loopback_test_probe(struct spi_device *spi) 319 { 320 int ret; 321 322 if (loop_req) { 323 spi->mode = SPI_LOOP | spi->mode; 324 ret = spi_setup(spi); 325 if (ret) { 326 dev_err(&spi->dev, "SPI setup with SPI_LOOP failed (%d)\n", 327 ret); 328 return ret; 329 } 330 } 331 332 dev_info(&spi->dev, "Executing spi-loopback-tests\n"); 333 334 ret = spi_test_run_tests(spi, spi_tests); 335 336 dev_info(&spi->dev, "Finished spi-loopback-tests with return: %i\n", 337 ret); 338 339 return ret; 340 } 341 342 /* non const match table to permit to change via a module parameter */ 343 static struct of_device_id spi_loopback_test_of_match[] = { 344 { .compatible = "linux,spi-loopback-test", }, 345 { } 346 }; 347 348 /* allow to override the compatible string via a module_parameter */ 349 module_param_string(compatible, spi_loopback_test_of_match[0].compatible, 350 sizeof(spi_loopback_test_of_match[0].compatible), 351 0000); 352 353 MODULE_DEVICE_TABLE(of, spi_loopback_test_of_match); 354 355 static struct spi_driver spi_loopback_test_driver = { 356 .driver = { 357 .name = "spi-loopback-test", 358 .owner = THIS_MODULE, 359 .of_match_table = spi_loopback_test_of_match, 360 }, 361 .probe = spi_loopback_test_probe, 362 }; 363 364 module_spi_driver(spi_loopback_test_driver); 365 366 MODULE_AUTHOR("Martin Sperl <[email protected]>"); 367 MODULE_DESCRIPTION("test spi_driver to check core functionality"); 368 MODULE_LICENSE("GPL"); 369 370 /*-------------------------------------------------------------------------*/ 371 372 /* spi_test implementation */ 373 374 #define RANGE_CHECK(ptr, plen, start, slen) \ 375 ((ptr >= start) && (ptr + plen <= start + slen)) 376 377 /* we allocate one page more, to allow for offsets */ 378 #define SPI_TEST_MAX_SIZE_PLUS (SPI_TEST_MAX_SIZE + PAGE_SIZE) 379 380 static void spi_test_print_hex_dump(char *pre, const void *ptr, size_t len) 381 { 382 /* limit the hex_dump */ 383 if (len < 1024) { 384 print_hex_dump(KERN_INFO, pre, 385 DUMP_PREFIX_OFFSET, 16, 1, 386 ptr, len, 0); 387 return; 388 } 389 /* print head */ 390 print_hex_dump(KERN_INFO, pre, 391 DUMP_PREFIX_OFFSET, 16, 1, 392 ptr, 512, 0); 393 /* print tail */ 394 pr_info("%s truncated - continuing at offset %04zx\n", 395 pre, len - 512); 396 print_hex_dump(KERN_INFO, pre, 397 DUMP_PREFIX_OFFSET, 16, 1, 398 ptr + (len - 512), 512, 0); 399 } 400 401 static void spi_test_dump_message(struct spi_device *spi, 402 struct spi_message *msg, 403 bool dump_data) 404 { 405 struct spi_transfer *xfer; 406 int i; 407 u8 b; 408 409 dev_info(&spi->dev, " spi_msg@%pK\n", msg); 410 if (msg->status) 411 dev_info(&spi->dev, " status: %i\n", 412 msg->status); 413 dev_info(&spi->dev, " frame_length: %i\n", 414 msg->frame_length); 415 dev_info(&spi->dev, " actual_length: %i\n", 416 msg->actual_length); 417 418 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 419 dev_info(&spi->dev, " spi_transfer@%pK\n", xfer); 420 dev_info(&spi->dev, " len: %i\n", xfer->len); 421 dev_info(&spi->dev, " tx_buf: %pK\n", xfer->tx_buf); 422 if (dump_data && xfer->tx_buf) 423 spi_test_print_hex_dump(" TX: ", 424 xfer->tx_buf, 425 xfer->len); 426 427 dev_info(&spi->dev, " rx_buf: %pK\n", xfer->rx_buf); 428 if (dump_data && xfer->rx_buf) 429 spi_test_print_hex_dump(" RX: ", 430 xfer->rx_buf, 431 xfer->len); 432 /* check for unwritten test pattern on rx_buf */ 433 if (xfer->rx_buf) { 434 for (i = 0 ; i < xfer->len ; i++) { 435 b = ((u8 *)xfer->rx_buf)[xfer->len - 1 - i]; 436 if (b != SPI_TEST_PATTERN_UNWRITTEN) 437 break; 438 } 439 if (i) 440 dev_info(&spi->dev, 441 " rx_buf filled with %02x starts at offset: %i\n", 442 SPI_TEST_PATTERN_UNWRITTEN, 443 xfer->len - i); 444 } 445 } 446 } 447 448 struct rx_ranges { 449 struct list_head list; 450 u8 *start; 451 u8 *end; 452 }; 453 454 static int rx_ranges_cmp(void *priv, struct list_head *a, struct list_head *b) 455 { 456 struct rx_ranges *rx_a = list_entry(a, struct rx_ranges, list); 457 struct rx_ranges *rx_b = list_entry(b, struct rx_ranges, list); 458 459 if (rx_a->start > rx_b->start) 460 return 1; 461 if (rx_a->start < rx_b->start) 462 return -1; 463 return 0; 464 } 465 466 static int spi_check_rx_ranges(struct spi_device *spi, 467 struct spi_message *msg, 468 void *rx) 469 { 470 struct spi_transfer *xfer; 471 struct rx_ranges ranges[SPI_TEST_MAX_TRANSFERS], *r; 472 int i = 0; 473 LIST_HEAD(ranges_list); 474 u8 *addr; 475 int ret = 0; 476 477 /* loop over all transfers to fill in the rx_ranges */ 478 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 479 /* if there is no rx, then no check is needed */ 480 if (!xfer->rx_buf) 481 continue; 482 /* fill in the rx_range */ 483 if (RANGE_CHECK(xfer->rx_buf, xfer->len, 484 rx, SPI_TEST_MAX_SIZE_PLUS)) { 485 ranges[i].start = xfer->rx_buf; 486 ranges[i].end = xfer->rx_buf + xfer->len; 487 list_add(&ranges[i].list, &ranges_list); 488 i++; 489 } 490 } 491 492 /* if no ranges, then we can return and avoid the checks...*/ 493 if (!i) 494 return 0; 495 496 /* sort the list */ 497 list_sort(NULL, &ranges_list, rx_ranges_cmp); 498 499 /* and iterate over all the rx addresses */ 500 for (addr = rx; addr < (u8 *)rx + SPI_TEST_MAX_SIZE_PLUS; addr++) { 501 /* if we are the DO not write pattern, 502 * then continue with the loop... 503 */ 504 if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE) 505 continue; 506 507 /* check if we are inside a range */ 508 list_for_each_entry(r, &ranges_list, list) { 509 /* if so then set to end... */ 510 if ((addr >= r->start) && (addr < r->end)) 511 addr = r->end; 512 } 513 /* second test after a (hopefull) translation */ 514 if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE) 515 continue; 516 517 /* if still not found then something has modified too much */ 518 /* we could list the "closest" transfer here... */ 519 dev_err(&spi->dev, 520 "loopback strangeness - rx changed outside of allowed range at: %pK\n", 521 addr); 522 /* do not return, only set ret, 523 * so that we list all addresses 524 */ 525 ret = -ERANGE; 526 } 527 528 return ret; 529 } 530 531 static int spi_test_check_elapsed_time(struct spi_device *spi, 532 struct spi_test *test) 533 { 534 int i; 535 unsigned long long estimated_time = 0; 536 unsigned long long delay_usecs = 0; 537 538 for (i = 0; i < test->transfer_count; i++) { 539 struct spi_transfer *xfer = test->transfers + i; 540 unsigned long long nbits = (unsigned long long)BITS_PER_BYTE * 541 xfer->len; 542 543 delay_usecs += xfer->delay_usecs; 544 if (!xfer->speed_hz) 545 continue; 546 estimated_time += div_u64(nbits * NSEC_PER_SEC, xfer->speed_hz); 547 } 548 549 estimated_time += delay_usecs * NSEC_PER_USEC; 550 if (test->elapsed_time < estimated_time) { 551 dev_err(&spi->dev, 552 "elapsed time %lld ns is shorter than minimum estimated time %lld ns\n", 553 test->elapsed_time, estimated_time); 554 555 return -EINVAL; 556 } 557 558 return 0; 559 } 560 561 static int spi_test_check_loopback_result(struct spi_device *spi, 562 struct spi_message *msg, 563 void *tx, void *rx) 564 { 565 struct spi_transfer *xfer; 566 u8 rxb, txb; 567 size_t i; 568 int ret; 569 570 /* checks rx_buffer pattern are valid with loopback or without */ 571 if (check_ranges) { 572 ret = spi_check_rx_ranges(spi, msg, rx); 573 if (ret) 574 return ret; 575 } 576 577 /* if we run without loopback, then return now */ 578 if (!loopback) 579 return 0; 580 581 /* if applicable to transfer check that rx_buf is equal to tx_buf */ 582 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 583 /* if there is no rx, then no check is needed */ 584 if (!xfer->len || !xfer->rx_buf) 585 continue; 586 /* so depending on tx_buf we need to handle things */ 587 if (xfer->tx_buf) { 588 for (i = 0; i < xfer->len; i++) { 589 txb = ((u8 *)xfer->tx_buf)[i]; 590 rxb = ((u8 *)xfer->rx_buf)[i]; 591 if (txb != rxb) 592 goto mismatch_error; 593 } 594 } else { 595 /* first byte received */ 596 txb = ((u8 *)xfer->rx_buf)[0]; 597 /* first byte may be 0 or xff */ 598 if (!((txb == 0) || (txb == 0xff))) { 599 dev_err(&spi->dev, 600 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n", 601 txb); 602 return -EINVAL; 603 } 604 /* check that all bytes are identical */ 605 for (i = 1; i < xfer->len; i++) { 606 rxb = ((u8 *)xfer->rx_buf)[i]; 607 if (rxb != txb) 608 goto mismatch_error; 609 } 610 } 611 } 612 613 return 0; 614 615 mismatch_error: 616 dev_err(&spi->dev, 617 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n", 618 i, txb, rxb); 619 620 return -EINVAL; 621 } 622 623 static int spi_test_translate(struct spi_device *spi, 624 void **ptr, size_t len, 625 void *tx, void *rx) 626 { 627 size_t off; 628 629 /* return on null */ 630 if (!*ptr) 631 return 0; 632 633 /* in the MAX_SIZE_HALF case modify the pointer */ 634 if (((size_t)*ptr) & SPI_TEST_MAX_SIZE_HALF) 635 /* move the pointer to the correct range */ 636 *ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) - 637 SPI_TEST_MAX_SIZE_HALF; 638 639 /* RX range 640 * - we check against MAX_SIZE_PLUS to allow for automated alignment 641 */ 642 if (RANGE_CHECK(*ptr, len, RX(0), SPI_TEST_MAX_SIZE_PLUS)) { 643 off = *ptr - RX(0); 644 *ptr = rx + off; 645 646 return 0; 647 } 648 649 /* TX range */ 650 if (RANGE_CHECK(*ptr, len, TX(0), SPI_TEST_MAX_SIZE_PLUS)) { 651 off = *ptr - TX(0); 652 *ptr = tx + off; 653 654 return 0; 655 } 656 657 dev_err(&spi->dev, 658 "PointerRange [%pK:%pK[ not in range [%pK:%pK[ or [%pK:%pK[\n", 659 *ptr, *ptr + len, 660 RX(0), RX(SPI_TEST_MAX_SIZE), 661 TX(0), TX(SPI_TEST_MAX_SIZE)); 662 663 return -EINVAL; 664 } 665 666 static int spi_test_fill_pattern(struct spi_device *spi, 667 struct spi_test *test) 668 { 669 struct spi_transfer *xfers = test->transfers; 670 u8 *tx_buf; 671 size_t count = 0; 672 int i, j; 673 674 #ifdef __BIG_ENDIAN 675 #define GET_VALUE_BYTE(value, index, bytes) \ 676 (value >> (8 * (bytes - 1 - count % bytes))) 677 #else 678 #define GET_VALUE_BYTE(value, index, bytes) \ 679 (value >> (8 * (count % bytes))) 680 #endif 681 682 /* fill all transfers with the pattern requested */ 683 for (i = 0; i < test->transfer_count; i++) { 684 /* fill rx_buf with SPI_TEST_PATTERN_UNWRITTEN */ 685 if (xfers[i].rx_buf) 686 memset(xfers[i].rx_buf, SPI_TEST_PATTERN_UNWRITTEN, 687 xfers[i].len); 688 /* if tx_buf is NULL then skip */ 689 tx_buf = (u8 *)xfers[i].tx_buf; 690 if (!tx_buf) 691 continue; 692 /* modify all the transfers */ 693 for (j = 0; j < xfers[i].len; j++, tx_buf++, count++) { 694 /* fill tx */ 695 switch (test->fill_option) { 696 case FILL_MEMSET_8: 697 *tx_buf = test->fill_pattern; 698 break; 699 case FILL_MEMSET_16: 700 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, 701 count, 2); 702 break; 703 case FILL_MEMSET_24: 704 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, 705 count, 3); 706 break; 707 case FILL_MEMSET_32: 708 *tx_buf = GET_VALUE_BYTE(test->fill_pattern, 709 count, 4); 710 break; 711 case FILL_COUNT_8: 712 *tx_buf = count; 713 break; 714 case FILL_COUNT_16: 715 *tx_buf = GET_VALUE_BYTE(count, count, 2); 716 break; 717 case FILL_COUNT_24: 718 *tx_buf = GET_VALUE_BYTE(count, count, 3); 719 break; 720 case FILL_COUNT_32: 721 *tx_buf = GET_VALUE_BYTE(count, count, 4); 722 break; 723 case FILL_TRANSFER_BYTE_8: 724 *tx_buf = j; 725 break; 726 case FILL_TRANSFER_BYTE_16: 727 *tx_buf = GET_VALUE_BYTE(j, j, 2); 728 break; 729 case FILL_TRANSFER_BYTE_24: 730 *tx_buf = GET_VALUE_BYTE(j, j, 3); 731 break; 732 case FILL_TRANSFER_BYTE_32: 733 *tx_buf = GET_VALUE_BYTE(j, j, 4); 734 break; 735 case FILL_TRANSFER_NUM: 736 *tx_buf = i; 737 break; 738 default: 739 dev_err(&spi->dev, 740 "unsupported fill_option: %i\n", 741 test->fill_option); 742 return -EINVAL; 743 } 744 } 745 } 746 747 return 0; 748 } 749 750 static int _spi_test_run_iter(struct spi_device *spi, 751 struct spi_test *test, 752 void *tx, void *rx) 753 { 754 struct spi_message *msg = &test->msg; 755 struct spi_transfer *x; 756 int i, ret; 757 758 /* initialize message - zero-filled via static initialization */ 759 spi_message_init_no_memset(msg); 760 761 /* fill rx with the DO_NOT_WRITE pattern */ 762 memset(rx, SPI_TEST_PATTERN_DO_NOT_WRITE, SPI_TEST_MAX_SIZE_PLUS); 763 764 /* add the individual transfers */ 765 for (i = 0; i < test->transfer_count; i++) { 766 x = &test->transfers[i]; 767 768 /* patch the values of tx_buf */ 769 ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len, 770 (void *)tx, rx); 771 if (ret) 772 return ret; 773 774 /* patch the values of rx_buf */ 775 ret = spi_test_translate(spi, &x->rx_buf, x->len, 776 (void *)tx, rx); 777 if (ret) 778 return ret; 779 780 /* and add it to the list */ 781 spi_message_add_tail(x, msg); 782 } 783 784 /* fill in the transfer buffers with pattern */ 785 ret = spi_test_fill_pattern(spi, test); 786 if (ret) 787 return ret; 788 789 /* and execute */ 790 if (test->execute_msg) 791 ret = test->execute_msg(spi, test, tx, rx); 792 else 793 ret = spi_test_execute_msg(spi, test, tx, rx); 794 795 /* handle result */ 796 if (ret == test->expected_return) 797 return 0; 798 799 dev_err(&spi->dev, 800 "test failed - test returned %i, but we expect %i\n", 801 ret, test->expected_return); 802 803 if (ret) 804 return ret; 805 806 /* if it is 0, as we expected something else, 807 * then return something special 808 */ 809 return -EFAULT; 810 } 811 812 static int spi_test_run_iter(struct spi_device *spi, 813 const struct spi_test *testtemplate, 814 void *tx, void *rx, 815 size_t len, 816 size_t tx_off, 817 size_t rx_off 818 ) 819 { 820 struct spi_test test; 821 int i, tx_count, rx_count; 822 823 /* copy the test template to test */ 824 memcpy(&test, testtemplate, sizeof(test)); 825 826 /* if iterate_transfer_mask is not set, 827 * then set it to first transfer only 828 */ 829 if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1))) 830 test.iterate_transfer_mask = 1; 831 832 /* count number of transfers with tx/rx_buf != NULL */ 833 rx_count = tx_count = 0; 834 for (i = 0; i < test.transfer_count; i++) { 835 if (test.transfers[i].tx_buf) 836 tx_count++; 837 if (test.transfers[i].rx_buf) 838 rx_count++; 839 } 840 841 /* in some iteration cases warn and exit early, 842 * as there is nothing to do, that has not been tested already... 843 */ 844 if (tx_off && (!tx_count)) { 845 dev_warn_once(&spi->dev, 846 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n", 847 test.description); 848 return 0; 849 } 850 if (rx_off && (!rx_count)) { 851 dev_warn_once(&spi->dev, 852 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n", 853 test.description); 854 return 0; 855 } 856 857 /* write out info */ 858 if (!(len || tx_off || rx_off)) { 859 dev_info(&spi->dev, "Running test %s\n", test.description); 860 } else { 861 dev_info(&spi->dev, 862 " with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n", 863 len, tx_off, rx_off); 864 } 865 866 /* update in the values from iteration values */ 867 for (i = 0; i < test.transfer_count; i++) { 868 /* only when bit in transfer mask is set */ 869 if (!(test.iterate_transfer_mask & BIT(i))) 870 continue; 871 test.transfers[i].len = len; 872 if (test.transfers[i].tx_buf) 873 test.transfers[i].tx_buf += tx_off; 874 if (test.transfers[i].tx_buf) 875 test.transfers[i].rx_buf += rx_off; 876 } 877 878 /* and execute */ 879 return _spi_test_run_iter(spi, &test, tx, rx); 880 } 881 882 /** 883 * spi_test_execute_msg - default implementation to run a test 884 * 885 * spi: @spi_device on which to run the @spi_message 886 * test: the test to execute, which already contains @msg 887 * tx: the tx buffer allocated for the test sequence 888 * rx: the rx buffer allocated for the test sequence 889 * 890 * Returns: error code of spi_sync as well as basic error checking 891 */ 892 int spi_test_execute_msg(struct spi_device *spi, struct spi_test *test, 893 void *tx, void *rx) 894 { 895 struct spi_message *msg = &test->msg; 896 int ret = 0; 897 int i; 898 899 /* only if we do not simulate */ 900 if (!simulate_only) { 901 ktime_t start; 902 903 /* dump the complete message before and after the transfer */ 904 if (dump_messages == 3) 905 spi_test_dump_message(spi, msg, true); 906 907 start = ktime_get(); 908 /* run spi message */ 909 ret = spi_sync(spi, msg); 910 test->elapsed_time = ktime_to_ns(ktime_sub(ktime_get(), start)); 911 if (ret == -ETIMEDOUT) { 912 dev_info(&spi->dev, 913 "spi-message timed out - rerunning...\n"); 914 /* rerun after a few explicit schedules */ 915 for (i = 0; i < 16; i++) 916 schedule(); 917 ret = spi_sync(spi, msg); 918 } 919 if (ret) { 920 dev_err(&spi->dev, 921 "Failed to execute spi_message: %i\n", 922 ret); 923 goto exit; 924 } 925 926 /* do some extra error checks */ 927 if (msg->frame_length != msg->actual_length) { 928 dev_err(&spi->dev, 929 "actual length differs from expected\n"); 930 ret = -EIO; 931 goto exit; 932 } 933 934 /* run rx-buffer tests */ 935 ret = spi_test_check_loopback_result(spi, msg, tx, rx); 936 if (ret) 937 goto exit; 938 939 ret = spi_test_check_elapsed_time(spi, test); 940 } 941 942 /* if requested or on error dump message (including data) */ 943 exit: 944 if (dump_messages || ret) 945 spi_test_dump_message(spi, msg, 946 (dump_messages >= 2) || (ret)); 947 948 return ret; 949 } 950 EXPORT_SYMBOL_GPL(spi_test_execute_msg); 951 952 /** 953 * spi_test_run_test - run an individual spi_test 954 * including all the relevant iterations on: 955 * length and buffer alignment 956 * 957 * spi: the spi_device to send the messages to 958 * test: the test which we need to execute 959 * tx: the tx buffer allocated for the test sequence 960 * rx: the rx buffer allocated for the test sequence 961 * 962 * Returns: status code of spi_sync or other failures 963 */ 964 965 int spi_test_run_test(struct spi_device *spi, const struct spi_test *test, 966 void *tx, void *rx) 967 { 968 int idx_len; 969 size_t len; 970 size_t tx_align, rx_align; 971 int ret; 972 973 /* test for transfer limits */ 974 if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) { 975 dev_err(&spi->dev, 976 "%s: Exceeded max number of transfers with %i\n", 977 test->description, test->transfer_count); 978 return -E2BIG; 979 } 980 981 /* setting up some values in spi_message 982 * based on some settings in spi_master 983 * some of this can also get done in the run() method 984 */ 985 986 /* iterate over all the iterable values using macros 987 * (to make it a bit more readable... 988 */ 989 #define FOR_EACH_ALIGNMENT(var) \ 990 for (var = 0; \ 991 var < (test->iterate_##var ? \ 992 (spi->master->dma_alignment ? \ 993 spi->master->dma_alignment : \ 994 test->iterate_##var) : \ 995 1); \ 996 var++) 997 998 for (idx_len = 0; idx_len < SPI_TEST_MAX_ITERATE && 999 (len = test->iterate_len[idx_len]) != -1; idx_len++) { 1000 FOR_EACH_ALIGNMENT(tx_align) { 1001 FOR_EACH_ALIGNMENT(rx_align) { 1002 /* and run the iteration */ 1003 ret = spi_test_run_iter(spi, test, 1004 tx, rx, 1005 len, 1006 tx_align, 1007 rx_align); 1008 if (ret) 1009 return ret; 1010 } 1011 } 1012 } 1013 1014 return 0; 1015 } 1016 EXPORT_SYMBOL_GPL(spi_test_run_test); 1017 1018 /** 1019 * spi_test_run_tests - run an array of spi_messages tests 1020 * @spi: the spi device on which to run the tests 1021 * @tests: NULL-terminated array of @spi_test 1022 * 1023 * Returns: status errors as per @spi_test_run_test() 1024 */ 1025 1026 int spi_test_run_tests(struct spi_device *spi, 1027 struct spi_test *tests) 1028 { 1029 char *rx = NULL, *tx = NULL; 1030 int ret = 0, count = 0; 1031 struct spi_test *test; 1032 1033 /* allocate rx/tx buffers of 128kB size without devm 1034 * in the hope that is on a page boundary 1035 */ 1036 if (use_vmalloc) 1037 rx = vmalloc(SPI_TEST_MAX_SIZE_PLUS); 1038 else 1039 rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL); 1040 if (!rx) 1041 return -ENOMEM; 1042 1043 1044 if (use_vmalloc) 1045 tx = vmalloc(SPI_TEST_MAX_SIZE_PLUS); 1046 else 1047 tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL); 1048 if (!tx) { 1049 ret = -ENOMEM; 1050 goto err_tx; 1051 } 1052 1053 /* now run the individual tests in the table */ 1054 for (test = tests, count = 0; test->description[0]; 1055 test++, count++) { 1056 /* only run test if requested */ 1057 if ((run_only_test > -1) && (count != run_only_test)) 1058 continue; 1059 /* run custom implementation */ 1060 if (test->run_test) 1061 ret = test->run_test(spi, test, tx, rx); 1062 else 1063 ret = spi_test_run_test(spi, test, tx, rx); 1064 if (ret) 1065 goto out; 1066 /* add some delays so that we can easily 1067 * detect the individual tests when using a logic analyzer 1068 * we also add scheduling to avoid potential spi_timeouts... 1069 */ 1070 mdelay(100); 1071 schedule(); 1072 } 1073 1074 out: 1075 kvfree(tx); 1076 err_tx: 1077 kvfree(rx); 1078 return ret; 1079 } 1080 EXPORT_SYMBOL_GPL(spi_test_run_tests); 1081