spi-loopback-test.c 25 KB

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