spi-loopback-test.c 27 KB

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