spi.c revision 1.32 1 /* $NetBSD: spi.c,v 1.32 2025/09/11 13:24:11 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 * Copyright (c) 2006 Garrett D'Amore.
6 * All rights reserved.
7 *
8 * Portions of this code were written by Garrett D'Amore for the
9 * Champaign-Urbana Community Wireless Network Project.
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgements:
22 * This product includes software developed by the Urbana-Champaign
23 * Independent Media Center.
24 * This product includes software developed by Garrett D'Amore.
25 * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 * D'Amore's name may not be used to endorse or promote products
27 * derived from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 #include "opt_fdt.h" /* XXX */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.32 2025/09/11 13:24:11 thorpej Exp $");
48
49 #include "locators.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/device.h>
54 #include <sys/conf.h>
55 #include <sys/malloc.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/errno.h>
59
60 #include <dev/spi/spivar.h>
61 #include <dev/spi/spi_io.h>
62
63 #ifdef FDT
64 #include <dev/fdt/fdt_spi.h> /* XXX */
65 #include <dev/ofw/openfirm.h> /* XXX */
66 #endif
67
68 #include "ioconf.h"
69 #include "locators.h"
70
71 struct spi_softc {
72 device_t sc_dev;
73 const struct spi_controller *sc_controller;
74 int sc_mode;
75 int sc_speed;
76 int sc_slave;
77 int sc_nslaves;
78 struct spi_handle *sc_slaves;
79 kmutex_t sc_lock;
80 kcondvar_t sc_cv;
81 kmutex_t sc_dev_lock;
82 int sc_flags;
83 #define SPIC_BUSY 1
84 };
85
86 static dev_type_open(spi_open);
87 static dev_type_close(spi_close);
88 static dev_type_ioctl(spi_ioctl);
89
90 const struct cdevsw spi_cdevsw = {
91 .d_open = spi_open,
92 .d_close = spi_close,
93 .d_read = noread,
94 .d_write = nowrite,
95 .d_ioctl = spi_ioctl,
96 .d_stop = nostop,
97 .d_tty = notty,
98 .d_poll = nopoll,
99 .d_mmap = nommap,
100 .d_kqfilter = nokqfilter,
101 .d_discard = nodiscard,
102 .d_flag = D_OTHER | D_MPSAFE
103 };
104
105 /*
106 * SPI slave device. We have one of these per slave.
107 */
108 struct spi_handle {
109 struct spi_softc *sh_sc;
110 const struct spi_controller *sh_controller;
111 int sh_slave;
112 int sh_mode;
113 int sh_speed;
114 int sh_flags;
115 #define SPIH_ATTACHED 1
116 };
117
118 #define SPI_MAXDATA 4096
119
120 /*
121 * API for bus drivers.
122 */
123
124 int
125 spibus_print(void *aux, const char *pnp)
126 {
127
128 if (pnp != NULL)
129 aprint_normal("spi at %s", pnp);
130
131 return (UNCONF);
132 }
133
134
135 static int
136 spi_match(device_t parent, cfdata_t cf, void *aux)
137 {
138
139 return 1;
140 }
141
142 static int
143 spi_print(void *aux, const char *pnp)
144 {
145 struct spi_attach_args *sa = aux;
146
147 if (sa->sa_handle->sh_slave != -1)
148 aprint_normal(" slave %d", sa->sa_handle->sh_slave);
149
150 return (UNCONF);
151 }
152
153 static int
154 spi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
155 {
156 struct spi_softc *sc = device_private(parent);
157 struct spi_attach_args sa;
158 int addr;
159
160 addr = cf->cf_loc[SPICF_SLAVE];
161 if ((addr < 0) || (addr >= sc->sc_controller->sct_nslaves)) {
162 return -1;
163 }
164
165 memset(&sa, 0, sizeof sa);
166 sa.sa_handle = &sc->sc_slaves[addr];
167 if (ISSET(sa.sa_handle->sh_flags, SPIH_ATTACHED))
168 return -1;
169
170 if (config_probe(parent, cf, &sa)) {
171 SET(sa.sa_handle->sh_flags, SPIH_ATTACHED);
172 config_attach(parent, cf, &sa, spi_print, CFARGS_NONE);
173 }
174
175 return 0;
176 }
177
178 /*
179 * XXX this is the same as i2c_fill_compat. It could be refactored into a
180 * common fill_compat function with pointers to compat & ncompat instead
181 * of attach_args as the first parameter.
182 */
183 static void
184 spi_fill_compat(struct spi_attach_args *sa, const char *compat, size_t len,
185 char **buffer)
186 {
187 int count, i;
188 const char *c, *start, **ptr;
189
190 *buffer = NULL;
191 for (i = count = 0, c = compat; i < len; i++, c++)
192 if (*c == 0)
193 count++;
194 count += 2;
195 ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK);
196 if (!ptr)
197 return;
198
199 for (i = count = 0, start = c = compat; i < len; i++, c++) {
200 if (*c == 0) {
201 ptr[count++] = start;
202 start = c + 1;
203 }
204 }
205 if (start < compat + len) {
206 /* last string not 0 terminated */
207 size_t l = c - start;
208 *buffer = malloc(l + 1, M_TEMP, M_WAITOK);
209 memcpy(*buffer, start, l);
210 (*buffer)[l] = 0;
211 ptr[count++] = *buffer;
212 }
213 ptr[count] = NULL;
214
215 sa->sa_compat = ptr;
216 sa->sa_ncompat = count;
217 }
218
219 static void
220 spi_direct_attach_child_devices(struct spi_softc *sc)
221 {
222 unsigned int count;
223 prop_dictionary_t child;
224 prop_array_t child_devices;
225 prop_data_t cdata;
226 devhandle_t parent_handle = device_handle(sc->sc_dev);
227 devhandle_t child_handle;
228 uint32_t slave;
229 uint64_t cookie;
230 struct spi_attach_args sa;
231 int loc[SPICF_NLOCS];
232 char *buf;
233 int i;
234
235 /* XXX A better way is coming, I promise... */
236 switch (devhandle_type(parent_handle)) {
237 #ifdef FDT
238 case DEVHANDLE_TYPE_OF:
239 child_devices = of_copy_spi_devs(sc->sc_dev);
240 break;
241 #endif
242 default:
243 child_devices = NULL;
244 break;
245 }
246
247 if (child_devices == NULL) {
248 return;
249 }
250
251 memset(loc, 0, sizeof loc);
252 count = prop_array_count(child_devices);
253 for (i = 0; i < count; i++) {
254 child = prop_array_get(child_devices, i);
255 if (!child)
256 continue;
257 if (!prop_dictionary_get_uint32(child, "slave", &slave))
258 continue;
259 if (slave >= sc->sc_controller->sct_nslaves)
260 continue;
261 if (!prop_dictionary_get_uint64(child, "cookie", &cookie))
262 continue;
263 if (!(cdata = prop_dictionary_get(child, "compatible")))
264 continue;
265 loc[SPICF_SLAVE] = slave;
266
267 memset(&sa, 0, sizeof sa);
268 sa.sa_handle = &sc->sc_slaves[slave];
269
270 /* XXX Really, I promise, it'll get better... */
271 switch (devhandle_type(parent_handle)) {
272 #ifdef FDT
273 case DEVHANDLE_TYPE_OF:
274 child_handle = devhandle_from_of(parent_handle,
275 (int)cookie);
276 break;
277 #endif
278 default:
279 child_handle = devhandle_invalid();
280 }
281
282 if (ISSET(sa.sa_handle->sh_flags, SPIH_ATTACHED))
283 continue;
284 SET(sa.sa_handle->sh_flags, SPIH_ATTACHED);
285
286 buf = NULL;
287 spi_fill_compat(&sa,
288 prop_data_value(cdata),
289 prop_data_size(cdata), &buf);
290 config_found(sc->sc_dev, &sa, spi_print,
291 CFARGS(.locators = loc,
292 .devhandle = child_handle));
293
294 if (sa.sa_compat)
295 free(sa.sa_compat, M_TEMP);
296 if (buf)
297 free(buf, M_TEMP);
298 }
299 prop_object_release(child_devices);
300 }
301
302 int
303 spi_compatible_match(const struct spi_attach_args *sa,
304 const cfdata_t cf __unused,
305 const struct device_compatible_entry *compats)
306 {
307 int match_result;
308
309 match_result = device_compatible_match(sa->sa_compat, sa->sa_ncompat,
310 compats);
311 if (match_result) {
312 match_result = SPI_MATCH_DIRECT_COMPATIBLE + match_result - 1;
313 }
314
315 return match_result ? match_result : SPI_MATCH_DEFAULT /* XXX */;
316 }
317
318 const struct device_compatible_entry *
319 spi_compatible_lookup(const struct spi_attach_args *sa,
320 const struct device_compatible_entry *compats)
321 {
322 return device_compatible_lookup(sa->sa_compat, sa->sa_ncompat,
323 compats);
324 }
325
326 bool
327 spi_use_direct_match(const struct spi_attach_args *sa,
328 const struct device_compatible_entry *compats,
329 int *match_resultp)
330 {
331 KASSERT(match_resultp != NULL);
332
333 if (sa->sa_ncompat > 0 && sa->sa_compat != NULL) {
334 *match_resultp = spi_compatible_match(sa, NULL, compats);
335 return true;
336 }
337
338 return false;
339 }
340
341 /*
342 * API for device drivers.
343 *
344 * We provide wrapper routines to decouple the ABI for the SPI
345 * device drivers from the ABI for the SPI bus drivers.
346 */
347 static void
348 spi_attach(device_t parent, device_t self, void *aux)
349 {
350 struct spi_softc *sc = device_private(self);
351 struct spibus_attach_args *sba = aux;
352 int i;
353
354 aprint_naive(": SPI bus\n");
355 aprint_normal(": SPI bus\n");
356
357 mutex_init(&sc->sc_dev_lock, MUTEX_DEFAULT, IPL_NONE);
358 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
359 cv_init(&sc->sc_cv, "spictl");
360
361 sc->sc_dev = self;
362 sc->sc_controller = sba->sba_controller;
363 sc->sc_nslaves = sba->sba_controller->sct_nslaves;
364 /* allocate slave structures */
365 sc->sc_slaves = malloc(sizeof (struct spi_handle) * sc->sc_nslaves,
366 M_DEVBUF, M_WAITOK | M_ZERO);
367
368 sc->sc_speed = 0;
369 sc->sc_mode = -1;
370 sc->sc_slave = -1;
371
372 /*
373 * Initialize slave handles
374 */
375 for (i = 0; i < sc->sc_nslaves; i++) {
376 sc->sc_slaves[i].sh_slave = i;
377 sc->sc_slaves[i].sh_sc = sc;
378 sc->sc_slaves[i].sh_controller = sc->sc_controller;
379 }
380
381 /* XXX Need a better way for this. */
382 switch (devhandle_type(device_handle(sc->sc_dev))) {
383 #ifdef FDT
384 case DEVHANDLE_TYPE_OF:
385 fdtbus_register_spi_controller(self, sc->sc_controller);
386 break;
387 #endif /* FDT */
388 default:
389 break;
390 }
391
392 /* First attach devices known to be present via the device tree. */
393 spi_direct_attach_child_devices(sc);
394
395 /* Then do any other devices the user may have manually wired */
396 config_search(self, NULL,
397 CFARGS(.search = spi_search));
398 }
399
400 static int
401 spi_open(dev_t dev, int flag, int fmt, lwp_t *l)
402 {
403 struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
404
405 if (sc == NULL)
406 return ENXIO;
407
408 return 0;
409 }
410
411 static int
412 spi_close(dev_t dev, int flag, int fmt, lwp_t *l)
413 {
414
415 return 0;
416 }
417
418 static int
419 spi_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
420 {
421 struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
422 struct spi_handle *sh;
423 spi_ioctl_configure_t *sic;
424 spi_ioctl_transfer_t *sit;
425 uint8_t *sbuf, *rbuf;
426 int error;
427
428 if (sc == NULL)
429 return ENXIO;
430
431 mutex_enter(&sc->sc_dev_lock);
432
433 switch (cmd) {
434 case SPI_IOCTL_CONFIGURE:
435 sic = (spi_ioctl_configure_t *)data;
436 if (sic->sic_addr < 0 || sic->sic_addr >= sc->sc_nslaves) {
437 error = EINVAL;
438 break;
439 }
440 sh = &sc->sc_slaves[sic->sic_addr];
441 error = spi_configure(sc->sc_dev, sh, sic->sic_mode,
442 sic->sic_speed);
443 break;
444 case SPI_IOCTL_TRANSFER:
445 sit = (spi_ioctl_transfer_t *)data;
446 if (sit->sit_addr < 0 || sit->sit_addr >= sc->sc_nslaves) {
447 error = EINVAL;
448 break;
449 }
450 if ((sit->sit_send && sit->sit_sendlen == 0)
451 || (sit->sit_recv && sit->sit_recvlen == 0)) {
452 error = EINVAL;
453 break;
454 }
455 sh = &sc->sc_slaves[sit->sit_addr];
456 sbuf = rbuf = NULL;
457 error = 0;
458 if (sit->sit_send && sit->sit_sendlen <= SPI_MAXDATA) {
459 sbuf = malloc(sit->sit_sendlen, M_DEVBUF, M_WAITOK);
460 error = copyin(sit->sit_send, sbuf, sit->sit_sendlen);
461 }
462 if (sit->sit_recv && sit->sit_recvlen <= SPI_MAXDATA) {
463 rbuf = malloc(sit->sit_recvlen, M_DEVBUF, M_WAITOK);
464 }
465 if (error == 0) {
466 if (sbuf && rbuf)
467 error = spi_send_recv(sh,
468 sit->sit_sendlen, sbuf,
469 sit->sit_recvlen, rbuf);
470 else if (sbuf)
471 error = spi_send(sh,
472 sit->sit_sendlen, sbuf);
473 else if (rbuf)
474 error = spi_recv(sh,
475 sit->sit_recvlen, rbuf);
476 }
477 if (rbuf) {
478 if (error == 0)
479 error = copyout(rbuf, sit->sit_recv,
480 sit->sit_recvlen);
481 free(rbuf, M_DEVBUF);
482 }
483 if (sbuf) {
484 free(sbuf, M_DEVBUF);
485 }
486 break;
487 default:
488 error = ENODEV;
489 break;
490 }
491
492 mutex_exit(&sc->sc_dev_lock);
493
494 return error;
495 }
496
497 CFATTACH_DECL_NEW(spi, sizeof(struct spi_softc),
498 spi_match, spi_attach, NULL, NULL);
499
500 /*
501 * Configure. This should be the first thing that the SPI driver
502 * should do, to configure which mode (e.g. SPI_MODE_0, which is the
503 * same as Philips Microwire mode), and speed. If the bus driver
504 * cannot run fast enough, then it should just configure the fastest
505 * mode that it can support. If the bus driver cannot run slow
506 * enough, then the device is incompatible and an error should be
507 * returned.
508 */
509 int
510 spi_configure(device_t dev __unused, struct spi_handle *sh, int mode, int speed)
511 {
512
513 sh->sh_mode = mode;
514 sh->sh_speed = speed;
515
516 /* No need to report errors; no failures. */
517
518 return 0;
519 }
520
521 /*
522 * Acquire controller
523 */
524 static void
525 spi_acquire(struct spi_handle *sh)
526 {
527 struct spi_softc *sc = sh->sh_sc;
528
529 mutex_enter(&sc->sc_lock);
530 while ((sc->sc_flags & SPIC_BUSY) != 0)
531 cv_wait(&sc->sc_cv, &sc->sc_lock);
532 sc->sc_flags |= SPIC_BUSY;
533 mutex_exit(&sc->sc_lock);
534 }
535
536 /*
537 * Release controller
538 */
539 static void
540 spi_release(struct spi_handle *sh)
541 {
542 struct spi_softc *sc = sh->sh_sc;
543
544 mutex_enter(&sc->sc_lock);
545 sc->sc_flags &= ~SPIC_BUSY;
546 cv_broadcast(&sc->sc_cv);
547 mutex_exit(&sc->sc_lock);
548 }
549
550 void
551 spi_transfer_init(struct spi_transfer *st)
552 {
553
554 mutex_init(&st->st_lock, MUTEX_DEFAULT, IPL_VM);
555 cv_init(&st->st_cv, "spixfr");
556
557 st->st_flags = 0;
558 st->st_errno = 0;
559 st->st_done = NULL;
560 st->st_chunks = NULL;
561 st->st_private = NULL;
562 st->st_slave = -1;
563 }
564
565 void
566 spi_chunk_init(struct spi_chunk *chunk, int cnt, const uint8_t *wptr,
567 uint8_t *rptr)
568 {
569
570 chunk->chunk_write = chunk->chunk_wptr = wptr;
571 chunk->chunk_read = chunk->chunk_rptr = rptr;
572 chunk->chunk_rresid = chunk->chunk_wresid = chunk->chunk_count = cnt;
573 chunk->chunk_next = NULL;
574 }
575
576 void
577 spi_transfer_add(struct spi_transfer *st, struct spi_chunk *chunk)
578 {
579 struct spi_chunk **cpp;
580
581 /* this is an O(n) insert -- perhaps we should use a simpleq? */
582 for (cpp = &st->st_chunks; *cpp; cpp = &(*cpp)->chunk_next);
583 *cpp = chunk;
584 }
585
586 int
587 spi_transfer(struct spi_handle *sh, struct spi_transfer *st)
588 {
589 struct spi_softc *sc = sh->sh_sc;
590 const struct spi_controller *tag = sh->sh_controller;
591 struct spi_chunk *chunk;
592 int error;
593
594 /*
595 * Initialize "resid" counters and pointers, so that callers
596 * and bus drivers don't have to.
597 */
598 for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
599 chunk->chunk_wresid = chunk->chunk_rresid = chunk->chunk_count;
600 chunk->chunk_wptr = chunk->chunk_write;
601 chunk->chunk_rptr = chunk->chunk_read;
602 }
603
604 /*
605 * Match slave and parameters to handle
606 */
607 st->st_slave = sh->sh_slave;
608
609 /*
610 * Reserve controller during transaction
611 */
612 spi_acquire(sh);
613
614 st->st_spiprivate = (void *)sh;
615
616 /*
617 * Reconfigure controller
618 *
619 * XXX backends don't configure per-slave parameters
620 * Whenever we switch slaves or change mode or speed, we
621 * need to tell the backend.
622 */
623 if (sc->sc_slave != sh->sh_slave
624 || sc->sc_mode != sh->sh_mode
625 || sc->sc_speed != sh->sh_speed) {
626 error = (*tag->sct_configure)(tag->sct_cookie,
627 sh->sh_slave, sh->sh_mode, sh->sh_speed);
628 if (error)
629 return error;
630 }
631 sc->sc_mode = sh->sh_mode;
632 sc->sc_speed = sh->sh_speed;
633 sc->sc_slave = sh->sh_slave;
634
635 error = (*tag->sct_transfer)(tag->sct_cookie, st);
636
637 return error;
638 }
639
640 void
641 spi_wait(struct spi_transfer *st)
642 {
643 struct spi_handle *sh = st->st_spiprivate;
644
645 mutex_enter(&st->st_lock);
646 while (!(st->st_flags & SPI_F_DONE)) {
647 cv_wait(&st->st_cv, &st->st_lock);
648 }
649 mutex_exit(&st->st_lock);
650 cv_destroy(&st->st_cv);
651 mutex_destroy(&st->st_lock);
652
653 /*
654 * End transaction
655 */
656 spi_release(sh);
657 }
658
659 void
660 spi_done(struct spi_transfer *st, int err)
661 {
662
663 mutex_enter(&st->st_lock);
664 if ((st->st_errno = err) != 0) {
665 st->st_flags |= SPI_F_ERROR;
666 }
667 st->st_flags |= SPI_F_DONE;
668 if (st->st_done != NULL) {
669 (*st->st_done)(st);
670 } else {
671 cv_broadcast(&st->st_cv);
672 }
673 mutex_exit(&st->st_lock);
674 }
675
676 /*
677 * Some convenience routines. These routines block until the work
678 * is done.
679 *
680 * spi_recv - receives data from the bus
681 *
682 * spi_send - sends data to the bus
683 *
684 * spi_send_recv - sends data to the bus, and then receives. Note that this is
685 * done synchronously, i.e. send a command and get the response. This is
686 * not full duplex. If you want full duplex, you can't use these convenience
687 * wrappers.
688 */
689 int
690 spi_recv(struct spi_handle *sh, int cnt, uint8_t *data)
691 {
692 struct spi_transfer trans;
693 struct spi_chunk chunk;
694
695 spi_transfer_init(&trans);
696 spi_chunk_init(&chunk, cnt, NULL, data);
697 spi_transfer_add(&trans, &chunk);
698
699 /* enqueue it and wait for it to complete */
700 spi_transfer(sh, &trans);
701 spi_wait(&trans);
702
703 if (trans.st_flags & SPI_F_ERROR)
704 return trans.st_errno;
705
706 return 0;
707 }
708
709 int
710 spi_send(struct spi_handle *sh, int cnt, const uint8_t *data)
711 {
712 struct spi_transfer trans;
713 struct spi_chunk chunk;
714
715 spi_transfer_init(&trans);
716 spi_chunk_init(&chunk, cnt, data, NULL);
717 spi_transfer_add(&trans, &chunk);
718
719 /* enqueue it and wait for it to complete */
720 spi_transfer(sh, &trans);
721 spi_wait(&trans);
722
723 if (trans.st_flags & SPI_F_ERROR)
724 return trans.st_errno;
725
726 return 0;
727 }
728
729 int
730 spi_send_recv(struct spi_handle *sh, int scnt, const uint8_t *snd,
731 int rcnt, uint8_t *rcv)
732 {
733 struct spi_transfer trans;
734 struct spi_chunk chunk1, chunk2;
735
736 spi_transfer_init(&trans);
737 spi_chunk_init(&chunk1, scnt, snd, NULL);
738 spi_chunk_init(&chunk2, rcnt, NULL, rcv);
739 spi_transfer_add(&trans, &chunk1);
740 spi_transfer_add(&trans, &chunk2);
741
742 /* enqueue it and wait for it to complete */
743 spi_transfer(sh, &trans);
744 spi_wait(&trans);
745
746 if (trans.st_flags & SPI_F_ERROR)
747 return trans.st_errno;
748
749 return 0;
750 }
751