spi-loopback-test.c 25 KB

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