spi-loopback-test.c 26 KB

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