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