1/* $NetBSD: spi.c,v 1.6 2011/07/08 03:29:15 mrg 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 <sys/cdefs.h>
45__KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.6 2011/07/08 03:29:15 mrg Exp $");
46
47#include "locators.h"
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/device.h>
52#include <sys/malloc.h>
53#include <sys/mutex.h>
54#include <sys/condvar.h>
55#include <sys/errno.h>
56
57#include <dev/spi/spivar.h>
58
59struct spi_softc {
60	struct spi_controller	sc_controller;
61	int			sc_mode;
62	int			sc_speed;
63	int			sc_nslaves;
64	struct spi_handle	*sc_slaves;
65};
66
67/*
68 * SPI slave device.  We have one of these per slave.
69 */
70struct spi_handle {
71	struct spi_softc	*sh_sc;
72	struct spi_controller	*sh_controller;
73	int			sh_slave;
74};
75
76/*
77 * API for bus drivers.
78 */
79
80int
81spibus_print(void *aux, const char *pnp)
82{
83
84	if (pnp != NULL)
85		aprint_normal("spi at %s", pnp);
86
87	return (UNCONF);
88}
89
90
91static int
92spi_match(device_t parent, cfdata_t cf, void *aux)
93{
94
95	return 1;
96}
97
98static int
99spi_print(void *aux, const char *pnp)
100{
101	struct spi_attach_args *sa = aux;
102
103	if (sa->sa_handle->sh_slave != -1)
104		aprint_normal(" slave %d", sa->sa_handle->sh_slave);
105
106	return (UNCONF);
107}
108
109static int
110spi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
111{
112	struct spi_softc *sc = device_private(parent);
113	struct spi_attach_args sa;
114	int addr;
115
116	addr = cf->cf_loc[SPICF_SLAVE];
117	if ((addr < 0) || (addr >= sc->sc_controller.sct_nslaves)) {
118		return -1;
119	}
120
121	sa.sa_handle = &sc->sc_slaves[addr];
122
123	if (config_match(parent, cf, &sa) > 0)
124		config_attach(parent, cf, &sa, spi_print);
125
126	return 0;
127}
128
129/*
130 * API for device drivers.
131 *
132 * We provide wrapper routines to decouple the ABI for the SPI
133 * device drivers from the ABI for the SPI bus drivers.
134 */
135static void
136spi_attach(device_t parent, device_t self, void *aux)
137{
138	struct spi_softc *sc = device_private(self);
139	struct spibus_attach_args *sba = aux;
140	int i;
141
142	aprint_naive(": SPI bus\n");
143	aprint_normal(": SPI bus\n");
144
145	sc->sc_controller = *sba->sba_controller;
146	/* allocate slave structures */
147	sc->sc_slaves = malloc(sizeof (struct spi_handle) * sc->sc_nslaves,
148	    M_DEVBUF, M_WAITOK | M_ZERO);
149
150	sc->sc_speed = 0;
151	sc->sc_mode = -1;
152
153	/*
154	 * Initialize slave handles
155	 */
156	sc->sc_nslaves = sba->sba_controller->sct_nslaves;
157	for (i = 0; i < sc->sc_nslaves; i++) {
158		sc->sc_slaves[i].sh_slave = i;
159		sc->sc_slaves[i].sh_sc = sc;
160		sc->sc_slaves[i].sh_controller = &sc->sc_controller;
161	}
162
163	/*
164	 * Locate and attach child devices
165	 */
166	config_search_ia(spi_search, self, "spi", NULL);
167}
168
169CFATTACH_DECL_NEW(spi, sizeof(struct spi_softc),
170    spi_match, spi_attach, NULL, NULL);
171
172/*
173 * Configure.  This should be the first thing that the SPI driver
174 * should do, to configure which mode (e.g. SPI_MODE_0, which is the
175 * same as Philips Microwire mode), and speed.  If the bus driver
176 * cannot run fast enough, then it should just configure the fastest
177 * mode that it can support.  If the bus driver cannot run slow
178 * enough, then the device is incompatible and an error should be
179 * returned.
180 */
181int
182spi_configure(struct spi_handle *sh, int mode, int speed)
183{
184	int			s, rv;
185	struct spi_softc	*sc = sh->sh_sc;
186	struct spi_controller	*tag = sh->sh_controller;
187
188	/* ensure that request is compatible with other devices on the bus */
189	if ((sc->sc_mode >= 0) && (sc->sc_mode != mode))
190		return EINVAL;
191
192	s = splbio();
193	/* pick lowest configured speed */
194	if (speed == 0)
195		speed = sc->sc_speed;
196	if (sc->sc_speed)
197		speed = min(sc->sc_speed, speed);
198
199	rv = (*tag->sct_configure)(tag->sct_cookie, sh->sh_slave,
200	    mode, speed);
201
202	if (rv == 0) {
203		sc->sc_mode = mode;
204		sc->sc_speed = speed;
205	}
206	splx(s);
207	return rv;
208}
209
210void
211spi_transfer_init(struct spi_transfer *st)
212{
213
214	mutex_init(&st->st_lock, MUTEX_DEFAULT, IPL_BIO);
215	cv_init(&st->st_cv, "spicv");
216
217	st->st_flags = 0;
218	st->st_errno = 0;
219	st->st_done = NULL;
220	st->st_chunks = NULL;
221	st->st_private = NULL;
222	st->st_slave = -1;
223}
224
225void
226spi_chunk_init(struct spi_chunk *chunk, int cnt, const uint8_t *wptr,
227    uint8_t *rptr)
228{
229
230	chunk->chunk_write = chunk->chunk_wptr = wptr;
231	chunk->chunk_read = chunk->chunk_rptr = rptr;
232	chunk->chunk_rresid = chunk->chunk_wresid = chunk->chunk_count = cnt;
233	chunk->chunk_next = NULL;
234}
235
236void
237spi_transfer_add(struct spi_transfer *st, struct spi_chunk *chunk)
238{
239	struct spi_chunk **cpp;
240
241	/* this is an O(n) insert -- perhaps we should use a simpleq? */
242	for (cpp = &st->st_chunks; *cpp; cpp = &(*cpp)->chunk_next);
243	*cpp = chunk;
244}
245
246int
247spi_transfer(struct spi_handle *sh, struct spi_transfer *st)
248{
249	struct spi_controller	*tag = sh->sh_controller;
250	struct spi_chunk	*chunk;
251
252	/*
253	 * Initialize "resid" counters and pointers, so that callers
254	 * and bus drivers don't have to.
255	 */
256	for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
257		chunk->chunk_wresid = chunk->chunk_rresid = chunk->chunk_count;
258		chunk->chunk_wptr = chunk->chunk_write;
259		chunk->chunk_rptr = chunk->chunk_read;
260	}
261
262	/*
263	 * Match slave to handle's slave.
264	 */
265	st->st_slave = sh->sh_slave;
266
267	return (*tag->sct_transfer)(tag->sct_cookie, st);
268}
269
270void
271spi_wait(struct spi_transfer *st)
272{
273
274	mutex_enter(&st->st_lock);
275	while (!(st->st_flags & SPI_F_DONE)) {
276		cv_wait(&st->st_cv, &st->st_lock);
277	}
278	mutex_exit(&st->st_lock);
279	cv_destroy(&st->st_cv);
280	mutex_destroy(&st->st_lock);
281}
282
283void
284spi_done(struct spi_transfer *st, int err)
285{
286
287	mutex_enter(&st->st_lock);
288	if ((st->st_errno = err) != 0) {
289		st->st_flags |= SPI_F_ERROR;
290	}
291	st->st_flags |= SPI_F_DONE;
292	if (st->st_done != NULL) {
293		(*st->st_done)(st);
294	} else {
295		cv_broadcast(&st->st_cv);
296	}
297	mutex_exit(&st->st_lock);
298}
299
300/*
301 * Some convenience routines.  These routines block until the work
302 * is done.
303 *
304 * spi_recv - receives data from the bus
305 *
306 * spi_send - sends data to the bus
307 *
308 * spi_send_recv - sends data to the bus, and then receives.  Note that this is
309 * done synchronously, i.e. send a command and get the response.  This is
310 * not full duplex.  If you wnat full duplex, you can't use these convenience
311 * wrappers.
312 */
313int
314spi_recv(struct spi_handle *sh, int cnt, uint8_t *data)
315{
316	struct spi_transfer	trans;
317	struct spi_chunk	chunk;
318
319	spi_transfer_init(&trans);
320	spi_chunk_init(&chunk, cnt, NULL, data);
321	spi_transfer_add(&trans, &chunk);
322
323	/* enqueue it and wait for it to complete */
324	spi_transfer(sh, &trans);
325	spi_wait(&trans);
326
327	if (trans.st_flags & SPI_F_ERROR)
328		return trans.st_errno;
329
330	return 0;
331}
332
333int
334spi_send(struct spi_handle *sh, int cnt, const uint8_t *data)
335{
336	struct spi_transfer	trans;
337	struct spi_chunk	chunk;
338
339	spi_transfer_init(&trans);
340	spi_chunk_init(&chunk, cnt, data, NULL);
341	spi_transfer_add(&trans, &chunk);
342
343	/* enqueue it and wait for it to complete */
344	spi_transfer(sh, &trans);
345	spi_wait(&trans);
346
347	if (trans.st_flags & SPI_F_ERROR)
348		return trans.st_errno;
349
350	return 0;
351}
352
353int
354spi_send_recv(struct spi_handle *sh, int scnt, const uint8_t *snd,
355    int rcnt, uint8_t *rcv)
356{
357	struct spi_transfer	trans;
358	struct spi_chunk	chunk1, chunk2;
359
360	spi_transfer_init(&trans);
361	spi_chunk_init(&chunk1, scnt, snd, NULL);
362	spi_chunk_init(&chunk2, rcnt, NULL, rcv);
363	spi_transfer_add(&trans, &chunk1);
364	spi_transfer_add(&trans, &chunk2);
365
366	/* enqueue it and wait for it to complete */
367	spi_transfer(sh, &trans);
368	spi_wait(&trans);
369
370	if (trans.st_flags & SPI_F_ERROR)
371		return trans.st_errno;
372
373	return 0;
374}
375