spi-loopback-test.c 24 KB

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