spi-loopback-test.c 24 KB

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