spi-loopback-test.c 24 KB

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