spi-loopback-test.c 26 KB

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