spi-loopback-test.c 26 KB

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