spi-loopback-test.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. .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. int ret;
  454. /* checks rx_buffer pattern are valid with loopback or without */
  455. ret = spi_check_rx_ranges(spi, msg, rx);
  456. if (ret)
  457. return ret;
  458. /* if we run without loopback, then return now */
  459. if (!loopback)
  460. return 0;
  461. /* if applicable to transfer check that rx_buf is equal to tx_buf */
  462. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  463. /* if there is no rx, then no check is needed */
  464. if (!xfer->rx_buf)
  465. continue;
  466. /* so depending on tx_buf we need to handle things */
  467. if (xfer->tx_buf) {
  468. for (i = 1; i < xfer->len; i++) {
  469. txb = ((u8 *)xfer->tx_buf)[i];
  470. rxb = ((u8 *)xfer->rx_buf)[i];
  471. if (txb != rxb)
  472. goto mismatch_error;
  473. }
  474. } else {
  475. /* first byte received */
  476. txb = ((u8 *)xfer->rx_buf)[0];
  477. /* first byte may be 0 or xff */
  478. if (!((txb == 0) || (txb == 0xff))) {
  479. dev_err(&spi->dev,
  480. "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n",
  481. txb);
  482. return -EINVAL;
  483. }
  484. /* check that all bytes are identical */
  485. for (i = 1; i < xfer->len; i++) {
  486. rxb = ((u8 *)xfer->rx_buf)[i];
  487. if (rxb != txb)
  488. goto mismatch_error;
  489. }
  490. }
  491. }
  492. return 0;
  493. mismatch_error:
  494. dev_err(&spi->dev,
  495. "loopback strangeness - transfer missmatch on byte %04zx - expected 0x%02x, but got 0x%02x\n",
  496. i, txb, rxb);
  497. return -EINVAL;
  498. }
  499. static int spi_test_translate(struct spi_device *spi,
  500. void **ptr, size_t len,
  501. void *tx, void *rx)
  502. {
  503. size_t off;
  504. /* return on null */
  505. if (!*ptr)
  506. return 0;
  507. /* in the MAX_SIZE_HALF case modify the pointer */
  508. if (((size_t)*ptr) & SPI_TEST_MAX_SIZE_HALF)
  509. /* move the pointer to the correct range */
  510. *ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) -
  511. SPI_TEST_MAX_SIZE_HALF;
  512. /* RX range
  513. * - we check against MAX_SIZE_PLUS to allow for automated alignment
  514. */
  515. if (RANGE_CHECK(*ptr, len, RX(0), SPI_TEST_MAX_SIZE_PLUS)) {
  516. off = *ptr - RX(0);
  517. *ptr = rx + off;
  518. return 0;
  519. }
  520. /* TX range */
  521. if (RANGE_CHECK(*ptr, len, TX(0), SPI_TEST_MAX_SIZE_PLUS)) {
  522. off = *ptr - TX(0);
  523. *ptr = tx + off;
  524. return 0;
  525. }
  526. dev_err(&spi->dev,
  527. "PointerRange [%pK:%pK[ not in range [%pK:%pK[ or [%pK:%pK[\n",
  528. *ptr, *ptr + len,
  529. RX(0), RX(SPI_TEST_MAX_SIZE),
  530. TX(0), TX(SPI_TEST_MAX_SIZE));
  531. return -EINVAL;
  532. }
  533. static int spi_test_fill_pattern(struct spi_device *spi,
  534. struct spi_test *test)
  535. {
  536. struct spi_transfer *xfers = test->transfers;
  537. u8 *tx_buf;
  538. size_t count = 0;
  539. int i, j;
  540. #ifdef __BIG_ENDIAN
  541. #define GET_VALUE_BYTE(value, index, bytes) \
  542. (value >> (8 * (bytes - 1 - count % bytes)))
  543. #else
  544. #define GET_VALUE_BYTE(value, index, bytes) \
  545. (value >> (8 * (count % bytes)))
  546. #endif
  547. /* fill all transfers with the pattern requested */
  548. for (i = 0; i < test->transfer_count; i++) {
  549. /* fill rx_buf with SPI_TEST_PATTERN_UNWRITTEN */
  550. if (xfers[i].rx_buf)
  551. memset(xfers[i].rx_buf, SPI_TEST_PATTERN_UNWRITTEN,
  552. xfers[i].len);
  553. /* if tx_buf is NULL then skip */
  554. tx_buf = (u8 *)xfers[i].tx_buf;
  555. if (!tx_buf)
  556. continue;
  557. /* modify all the transfers */
  558. for (j = 0; j < xfers[i].len; j++, tx_buf++, count++) {
  559. /* fill tx */
  560. switch (test->fill_option) {
  561. case FILL_MEMSET_8:
  562. *tx_buf = test->fill_pattern;
  563. break;
  564. case FILL_MEMSET_16:
  565. *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
  566. count, 2);
  567. break;
  568. case FILL_MEMSET_24:
  569. *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
  570. count, 3);
  571. break;
  572. case FILL_MEMSET_32:
  573. *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
  574. count, 4);
  575. break;
  576. case FILL_COUNT_8:
  577. *tx_buf = count;
  578. break;
  579. case FILL_COUNT_16:
  580. *tx_buf = GET_VALUE_BYTE(count, count, 2);
  581. break;
  582. case FILL_COUNT_24:
  583. *tx_buf = GET_VALUE_BYTE(count, count, 3);
  584. break;
  585. case FILL_COUNT_32:
  586. *tx_buf = GET_VALUE_BYTE(count, count, 4);
  587. break;
  588. case FILL_TRANSFER_BYTE_8:
  589. *tx_buf = j;
  590. break;
  591. case FILL_TRANSFER_BYTE_16:
  592. *tx_buf = GET_VALUE_BYTE(j, j, 2);
  593. break;
  594. case FILL_TRANSFER_BYTE_24:
  595. *tx_buf = GET_VALUE_BYTE(j, j, 3);
  596. break;
  597. case FILL_TRANSFER_BYTE_32:
  598. *tx_buf = GET_VALUE_BYTE(j, j, 4);
  599. break;
  600. case FILL_TRANSFER_NUM:
  601. *tx_buf = i;
  602. break;
  603. default:
  604. dev_err(&spi->dev,
  605. "unsupported fill_option: %i\n",
  606. test->fill_option);
  607. return -EINVAL;
  608. }
  609. }
  610. }
  611. return 0;
  612. }
  613. static int _spi_test_run_iter(struct spi_device *spi,
  614. struct spi_test *test,
  615. void *tx, void *rx)
  616. {
  617. struct spi_message *msg = &test->msg;
  618. struct spi_transfer *x;
  619. int i, ret;
  620. /* initialize message - zero-filled via static initialization */
  621. spi_message_init_no_memset(msg);
  622. /* fill rx with the DO_NOT_WRITE pattern */
  623. memset(rx, SPI_TEST_PATTERN_DO_NOT_WRITE, SPI_TEST_MAX_SIZE_PLUS);
  624. /* add the individual transfers */
  625. for (i = 0; i < test->transfer_count; i++) {
  626. x = &test->transfers[i];
  627. /* patch the values of tx_buf */
  628. ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len,
  629. (void *)tx, rx);
  630. if (ret)
  631. return ret;
  632. /* patch the values of rx_buf */
  633. ret = spi_test_translate(spi, &x->rx_buf, x->len,
  634. (void *)tx, rx);
  635. if (ret)
  636. return ret;
  637. /* and add it to the list */
  638. spi_message_add_tail(x, msg);
  639. }
  640. /* fill in the transfer buffers with pattern */
  641. ret = spi_test_fill_pattern(spi, test);
  642. if (ret)
  643. return ret;
  644. /* and execute */
  645. if (test->execute_msg)
  646. ret = test->execute_msg(spi, test, tx, rx);
  647. else
  648. ret = spi_test_execute_msg(spi, test, tx, rx);
  649. /* handle result */
  650. if (ret == test->expected_return)
  651. return 0;
  652. dev_err(&spi->dev,
  653. "test failed - test returned %i, but we expect %i\n",
  654. ret, test->expected_return);
  655. if (ret)
  656. return ret;
  657. /* if it is 0, as we expected something else,
  658. * then return something special
  659. */
  660. return -EFAULT;
  661. }
  662. static int spi_test_run_iter(struct spi_device *spi,
  663. const struct spi_test *testtemplate,
  664. void *tx, void *rx,
  665. size_t len,
  666. size_t tx_off,
  667. size_t rx_off
  668. )
  669. {
  670. struct spi_test test;
  671. int i, tx_count, rx_count;
  672. /* copy the test template to test */
  673. memcpy(&test, testtemplate, sizeof(test));
  674. /* set up test->transfers to the correct count */
  675. if (!test.transfer_count) {
  676. for (i = 0;
  677. (i < SPI_TEST_MAX_TRANSFERS) && test.transfers[i].len;
  678. i++) {
  679. test.transfer_count++;
  680. }
  681. }
  682. /* if iterate_transfer_mask is not set,
  683. * then set it to first transfer only
  684. */
  685. if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1)))
  686. test.iterate_transfer_mask = 1;
  687. /* count number of transfers with tx/rx_buf != NULL */
  688. for (i = 0; i < test.transfer_count; i++) {
  689. if (test.transfers[i].tx_buf)
  690. tx_count++;
  691. if (test.transfers[i].rx_buf)
  692. rx_count++;
  693. }
  694. /* in some iteration cases warn and exit early,
  695. * as there is nothing to do, that has not been tested already...
  696. */
  697. if (tx_off && (!tx_count)) {
  698. dev_warn_once(&spi->dev,
  699. "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n",
  700. test.description);
  701. return 0;
  702. }
  703. if (rx_off && (!rx_count)) {
  704. dev_warn_once(&spi->dev,
  705. "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n",
  706. test.description);
  707. return 0;
  708. }
  709. /* write out info */
  710. if (!(len || tx_off || rx_off)) {
  711. dev_info(&spi->dev, "Running test %s\n", test.description);
  712. } else {
  713. dev_info(&spi->dev,
  714. " with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n",
  715. len, tx_off, rx_off);
  716. }
  717. /* update in the values from iteration values */
  718. for (i = 0; i < test.transfer_count; i++) {
  719. /* only when bit in transfer mask is set */
  720. if (!(test.iterate_transfer_mask & BIT(i)))
  721. continue;
  722. if (len)
  723. test.transfers[i].len = len;
  724. if (test.transfers[i].tx_buf)
  725. test.transfers[i].tx_buf += tx_off;
  726. if (test.transfers[i].tx_buf)
  727. test.transfers[i].rx_buf += rx_off;
  728. }
  729. /* and execute */
  730. return _spi_test_run_iter(spi, &test, tx, rx);
  731. }
  732. /**
  733. * spi_test_execute_msg - default implementation to run a test
  734. *
  735. * spi: @spi_device on which to run the @spi_message
  736. * test: the test to execute, which already contains @msg
  737. * tx: the tx buffer allocated for the test sequence
  738. * rx: the rx buffer allocated for the test sequence
  739. *
  740. * Returns: error code of spi_sync as well as basic error checking
  741. */
  742. int spi_test_execute_msg(struct spi_device *spi, struct spi_test *test,
  743. void *tx, void *rx)
  744. {
  745. struct spi_message *msg = &test->msg;
  746. int ret = 0;
  747. int i;
  748. /* only if we do not simulate */
  749. if (!simulate_only) {
  750. /* dump the complete message before and after the transfer */
  751. if (dump_messages == 3)
  752. spi_test_dump_message(spi, msg, true);
  753. /* run spi message */
  754. ret = spi_sync(spi, msg);
  755. if (ret == -ETIMEDOUT) {
  756. dev_info(&spi->dev,
  757. "spi-message timed out - reruning...\n");
  758. /* rerun after a few explicit schedules */
  759. for (i = 0; i < 16; i++)
  760. schedule();
  761. ret = spi_sync(spi, msg);
  762. }
  763. if (ret) {
  764. dev_err(&spi->dev,
  765. "Failed to execute spi_message: %i\n",
  766. ret);
  767. goto exit;
  768. }
  769. /* do some extra error checks */
  770. if (msg->frame_length != msg->actual_length) {
  771. dev_err(&spi->dev,
  772. "actual length differs from expected\n");
  773. ret = -EIO;
  774. goto exit;
  775. }
  776. /* run rx-buffer tests */
  777. ret = spi_test_check_loopback_result(spi, msg, tx, rx);
  778. }
  779. /* if requested or on error dump message (including data) */
  780. exit:
  781. if (dump_messages || ret)
  782. spi_test_dump_message(spi, msg,
  783. (dump_messages >= 2) || (ret));
  784. return ret;
  785. }
  786. EXPORT_SYMBOL_GPL(spi_test_execute_msg);
  787. /**
  788. * spi_test_run_test - run an individual spi_test
  789. * including all the relevant iterations on:
  790. * length and buffer alignment
  791. *
  792. * spi: the spi_device to send the messages to
  793. * test: the test which we need to execute
  794. * tx: the tx buffer allocated for the test sequence
  795. * rx: the rx buffer allocated for the test sequence
  796. *
  797. * Returns: status code of spi_sync or other failures
  798. */
  799. int spi_test_run_test(struct spi_device *spi, const struct spi_test *test,
  800. void *tx, void *rx)
  801. {
  802. int idx_len;
  803. size_t len;
  804. size_t tx_align, rx_align;
  805. int ret;
  806. /* test for transfer limits */
  807. if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) {
  808. dev_err(&spi->dev,
  809. "%s: Exceeded max number of transfers with %i\n",
  810. test->description, test->transfer_count);
  811. return -E2BIG;
  812. }
  813. /* setting up some values in spi_message
  814. * based on some settings in spi_master
  815. * some of this can also get done in the run() method
  816. */
  817. /* iterate over all the iterable values using macros
  818. * (to make it a bit more readable...
  819. */
  820. #define FOR_EACH_ITERATE(var, defaultvalue) \
  821. for (idx_##var = -1, var = defaultvalue; \
  822. ((idx_##var < 0) || \
  823. ( \
  824. (idx_##var < SPI_TEST_MAX_ITERATE) && \
  825. (var = test->iterate_##var[idx_##var]) \
  826. ) \
  827. ); \
  828. idx_##var++)
  829. #define FOR_EACH_ALIGNMENT(var) \
  830. for (var = 0; \
  831. var < (test->iterate_##var ? \
  832. (spi->master->dma_alignment ? \
  833. spi->master->dma_alignment : \
  834. test->iterate_##var) : \
  835. 1); \
  836. var++)
  837. FOR_EACH_ITERATE(len, 0) {
  838. FOR_EACH_ALIGNMENT(tx_align) {
  839. FOR_EACH_ALIGNMENT(rx_align) {
  840. /* and run the iteration */
  841. ret = spi_test_run_iter(spi, test,
  842. tx, rx,
  843. len,
  844. tx_align,
  845. rx_align);
  846. if (ret)
  847. return ret;
  848. }
  849. }
  850. }
  851. return 0;
  852. }
  853. EXPORT_SYMBOL_GPL(spi_test_run_test);
  854. /**
  855. * spi_test_run_tests - run an array of spi_messages tests
  856. * @spi: the spi device on which to run the tests
  857. * @tests: NULL-terminated array of @spi_test
  858. *
  859. * Returns: status errors as per @spi_test_run_test()
  860. */
  861. int spi_test_run_tests(struct spi_device *spi,
  862. struct spi_test *tests)
  863. {
  864. char *rx = NULL, *tx = NULL;
  865. int ret = 0, count = 0;
  866. struct spi_test *test;
  867. /* allocate rx/tx buffers of 128kB size without devm
  868. * in the hope that is on a page boundary
  869. */
  870. rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
  871. if (!rx) {
  872. ret = -ENOMEM;
  873. goto out;
  874. }
  875. tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
  876. if (!tx) {
  877. ret = -ENOMEM;
  878. goto out;
  879. }
  880. /* now run the individual tests in the table */
  881. for (test = tests, count = 0; test->description[0];
  882. test++, count++) {
  883. /* only run test if requested */
  884. if ((run_only_test > -1) && (count != run_only_test))
  885. continue;
  886. /* run custom implementation */
  887. if (test->run_test)
  888. ret = test->run_test(spi, test, tx, rx);
  889. else
  890. ret = spi_test_run_test(spi, test, tx, rx);
  891. if (ret)
  892. goto out;
  893. /* add some delays so that we can easily
  894. * detect the individual tests when using a logic analyzer
  895. * we also add scheduling to avoid potential spi_timeouts...
  896. */
  897. mdelay(100);
  898. schedule();
  899. }
  900. out:
  901. kfree(rx);
  902. kfree(tx);
  903. return ret;
  904. }
  905. EXPORT_SYMBOL_GPL(spi_test_run_tests);