spi-loopback-test.c 27 KB

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