motoi2c.c revision 1.4 1 1.4 phx /* $NetBSD: motoi2c.c,v 1.4 2011/04/17 15:14:59 phx Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2007, 2010 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.2 nisimura * by Matt Thomas.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #include <sys/cdefs.h>
33 1.4 phx __KERNEL_RCSID(0, "$NetBSD: motoi2c.c,v 1.4 2011/04/17 15:14:59 phx Exp $");
34 1.1 matt
35 1.1 matt #include <sys/param.h>
36 1.1 matt #include <sys/device.h>
37 1.1 matt #include <sys/systm.h>
38 1.1 matt #include <sys/mutex.h>
39 1.1 matt #include <sys/bus.h>
40 1.1 matt #include <sys/intr.h>
41 1.1 matt
42 1.1 matt #include <dev/i2c/i2cvar.h>
43 1.1 matt #include <dev/i2c/motoi2creg.h>
44 1.1 matt #include <dev/i2c/motoi2cvar.h>
45 1.1 matt
46 1.1 matt #ifdef DEBUG
47 1.3 phx int motoi2c_debug = 0;
48 1.3 phx #define DPRINTF(x) if (motoi2c_debug) printf x
49 1.1 matt #else
50 1.3 phx #define DPRINTF(x)
51 1.1 matt #endif
52 1.1 matt
53 1.1 matt static int motoi2c_acquire_bus(void *, int);
54 1.1 matt static void motoi2c_release_bus(void *, int);
55 1.1 matt static int motoi2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
56 1.1 matt void *, size_t, int);
57 1.1 matt static int motoi2c_busy_wait(struct motoi2c_softc *, uint8_t);
58 1.1 matt
59 1.1 matt static const struct i2c_controller motoi2c = {
60 1.1 matt .ic_acquire_bus = motoi2c_acquire_bus,
61 1.1 matt .ic_release_bus = motoi2c_release_bus,
62 1.1 matt .ic_exec = motoi2c_exec,
63 1.1 matt };
64 1.1 matt
65 1.1 matt static const struct motoi2c_settings motoi2c_default_settings = {
66 1.1 matt .i2c_adr = MOTOI2C_ADR_DEFAULT,
67 1.1 matt .i2c_fdr = MOTOI2C_FDR_DEFAULT,
68 1.1 matt .i2c_dfsrr = MOTOI2C_DFSRR_DEFAULT,
69 1.1 matt };
70 1.1 matt
71 1.1 matt #define I2C_READ(r) ((*sc->sc_iord)(sc, (r)))
72 1.1 matt #define I2C_WRITE(r,v) ((*sc->sc_iowr)(sc, (r), (v)))
73 1.1 matt #define I2C_SETCLR(r, s, c) \
74 1.1 matt ((*sc->sc_iowr)(sc, (r), ((*sc->sc_iord)(sc, (r)) | (s)) & ~(c)))
75 1.1 matt
76 1.1 matt static uint8_t
77 1.1 matt motoi2c_iord1(struct motoi2c_softc *sc, bus_size_t off)
78 1.1 matt {
79 1.1 matt return bus_space_read_1(sc->sc_iot, sc->sc_ioh, off);
80 1.1 matt }
81 1.1 matt
82 1.1 matt static void
83 1.1 matt motoi2c_iowr1(struct motoi2c_softc *sc, bus_size_t off, uint8_t data)
84 1.1 matt {
85 1.1 matt bus_space_write_1(sc->sc_iot, sc->sc_ioh, off, data);
86 1.1 matt }
87 1.1 matt
88 1.1 matt void
89 1.1 matt motoi2c_attach_common(device_t self, struct motoi2c_softc *sc,
90 1.1 matt const struct motoi2c_settings *i2c)
91 1.1 matt {
92 1.1 matt struct i2cbus_attach_args iba;
93 1.1 matt
94 1.1 matt mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
95 1.1 matt
96 1.1 matt if (i2c == NULL)
97 1.1 matt i2c = &motoi2c_default_settings;
98 1.1 matt
99 1.1 matt sc->sc_i2c = motoi2c;
100 1.1 matt sc->sc_i2c.ic_cookie = sc;
101 1.1 matt if (sc->sc_iord == NULL)
102 1.1 matt sc->sc_iord = motoi2c_iord1;
103 1.1 matt if (sc->sc_iowr == NULL)
104 1.1 matt sc->sc_iowr = motoi2c_iowr1;
105 1.1 matt memset(&iba, 0, sizeof(iba));
106 1.1 matt iba.iba_tag = &sc->sc_i2c;
107 1.1 matt
108 1.1 matt I2C_WRITE(I2CCR, 0); /* reset before changing anything */
109 1.1 matt I2C_WRITE(I2CDFSRR, i2c->i2c_dfsrr); /* sampling units */
110 1.1 matt I2C_WRITE(I2CFDR, i2c->i2c_fdr); /* divider 3072 (0x31) */
111 1.1 matt I2C_WRITE(I2CADR, i2c->i2c_adr); /* our slave address is 0x7f */
112 1.1 matt I2C_WRITE(I2CSR, 0); /* clear status flags */
113 1.1 matt
114 1.1 matt config_found_ia(self, "i2cbus", &iba, iicbus_print);
115 1.1 matt }
116 1.1 matt
117 1.1 matt static int
118 1.1 matt motoi2c_acquire_bus(void *v, int flags)
119 1.1 matt {
120 1.1 matt struct motoi2c_softc * const sc = v;
121 1.1 matt
122 1.1 matt mutex_enter(&sc->sc_buslock);
123 1.1 matt I2C_WRITE(I2CCR, CR_MEN); /* enable the I2C module */
124 1.1 matt
125 1.1 matt return 0;
126 1.1 matt }
127 1.1 matt
128 1.1 matt static void
129 1.1 matt motoi2c_release_bus(void *v, int flags)
130 1.1 matt {
131 1.1 matt struct motoi2c_softc * const sc = v;
132 1.1 matt
133 1.1 matt I2C_WRITE(I2CCR, 0); /* reset before changing anything */
134 1.1 matt mutex_exit(&sc->sc_buslock);
135 1.1 matt }
136 1.1 matt
137 1.1 matt /* busy waiting for byte data transfer completion */
138 1.1 matt static int
139 1.1 matt motoi2c_busy_wait(struct motoi2c_softc *sc, uint8_t cr)
140 1.1 matt {
141 1.1 matt uint8_t sr;
142 1.1 matt u_int timo;
143 1.1 matt int error = 0;
144 1.1 matt
145 1.1 matt timo = 1000;
146 1.1 matt while (((sr = I2C_READ(I2CSR)) & SR_MIF) == 0 && --timo)
147 1.1 matt DELAY(10);
148 1.1 matt
149 1.1 matt if (timo == 0) {
150 1.3 phx DPRINTF(("%s: timeout (sr=%#x, cr=%#x)\n",
151 1.3 phx __func__, sr, I2C_READ(I2CCR)));
152 1.1 matt error = ETIMEDOUT;
153 1.1 matt }
154 1.1 matt /*
155 1.1 matt * RXAK is only valid when transmitting.
156 1.1 matt */
157 1.1 matt if ((cr & CR_MTX) && (sr & SR_RXAK)) {
158 1.3 phx DPRINTF(("%s: missing rx ack (%#x): spin=%u\n",
159 1.3 phx __func__, sr, 1000 - timo));
160 1.1 matt error = EIO;
161 1.1 matt }
162 1.1 matt I2C_WRITE(I2CSR, 0);
163 1.1 matt return error;
164 1.1 matt }
165 1.1 matt
166 1.1 matt int
167 1.1 matt motoi2c_intr(void *v)
168 1.1 matt {
169 1.1 matt struct motoi2c_softc * const sc = v;
170 1.1 matt
171 1.1 matt panic("%s(%p)", __func__, sc);
172 1.1 matt
173 1.1 matt return 0;
174 1.1 matt }
175 1.1 matt
176 1.1 matt int
177 1.1 matt motoi2c_exec(void *v, i2c_op_t op, i2c_addr_t addr,
178 1.1 matt const void *cmdbuf, size_t cmdlen,
179 1.1 matt void *databuf, size_t datalen,
180 1.1 matt int flags)
181 1.1 matt {
182 1.1 matt struct motoi2c_softc * const sc = v;
183 1.1 matt uint8_t sr;
184 1.1 matt uint8_t cr;
185 1.1 matt int error;
186 1.1 matt
187 1.1 matt sr = I2C_READ(I2CSR);
188 1.1 matt cr = I2C_READ(I2CCR);
189 1.1 matt
190 1.1 matt #if 0
191 1.3 phx DPRINTF(("%s(%#x,%#x,%p,%zu,%p,%zu,%#x): sr=%#x cr=%#x\n",
192 1.1 matt __func__, op, addr, cmdbuf, cmdlen, databuf, datalen, flags,
193 1.3 phx sr, cr));
194 1.1 matt #endif
195 1.1 matt
196 1.1 matt if ((cr & CR_MSTA) == 0 && (sr & SR_MBB) != 0) {
197 1.1 matt /* wait for bus becoming available */
198 1.1 matt u_int timo = 100;
199 1.1 matt do {
200 1.1 matt DELAY(10);
201 1.1 matt } while (--timo > 0 && ((sr = I2C_READ(I2CSR)) & SR_MBB) != 0);
202 1.1 matt
203 1.1 matt if (timo == 0) {
204 1.3 phx DPRINTF(("%s: bus is busy (%#x)\n", __func__, sr));
205 1.1 matt return ETIMEDOUT;
206 1.1 matt }
207 1.1 matt }
208 1.1 matt
209 1.1 matt /* reset interrupt and arbitration-lost flags (all others are RO) */
210 1.1 matt I2C_WRITE(I2CSR, 0);
211 1.1 matt sr = I2C_READ(I2CSR);
212 1.1 matt
213 1.1 matt /*
214 1.1 matt * Generate start (or restart) condition
215 1.1 matt */
216 1.1 matt /* CR_RTSA is write-only and transitory */
217 1.1 matt uint8_t rsta = (cr & CR_MSTA ? CR_RSTA : 0);
218 1.1 matt cr = CR_MEN | CR_MTX | CR_MSTA;
219 1.1 matt I2C_WRITE(I2CCR, cr | rsta);
220 1.1 matt
221 1.3 phx DPRINTF(("%s: started: sr=%#x cr=%#x/%#x\n",
222 1.3 phx __func__, I2C_READ(I2CSR), cr, I2C_READ(I2CCR)));
223 1.1 matt
224 1.1 matt sr = I2C_READ(I2CSR);
225 1.1 matt if (sr & SR_MAL) {
226 1.3 phx DPRINTF(("%s: lost bus: sr=%#x cr=%#x/%#x\n",
227 1.3 phx __func__, I2C_READ(I2CSR), cr, I2C_READ(I2CCR)));
228 1.1 matt I2C_WRITE(I2CCR, 0);
229 1.1 matt DELAY(10);
230 1.1 matt I2C_WRITE(I2CCR, CR_MEN | CR_MTX | CR_MSTA);
231 1.1 matt DELAY(10);
232 1.1 matt sr = I2C_READ(I2CSR);
233 1.1 matt if (sr & SR_MAL) {
234 1.1 matt error = EBUSY;
235 1.1 matt goto out;
236 1.1 matt }
237 1.3 phx DPRINTF(("%s: reacquired bus: sr=%#x cr=%#x/%#x\n",
238 1.3 phx __func__, I2C_READ(I2CSR), cr, I2C_READ(I2CCR)));
239 1.1 matt }
240 1.1 matt
241 1.1 matt /* send target address and transfer direction */
242 1.1 matt uint8_t addr_byte = (addr << 1)
243 1.1 matt | (cmdlen == 0 && I2C_OP_READ_P(op) ? 1 : 0);
244 1.1 matt I2C_WRITE(I2CDR, addr_byte);
245 1.1 matt
246 1.1 matt error = motoi2c_busy_wait(sc, cr);
247 1.1 matt if (error) {
248 1.3 phx DPRINTF(("%s: error sending address: %d\n", __func__, error));
249 1.1 matt if (error == EIO)
250 1.1 matt error = ENXIO;
251 1.1 matt goto out;
252 1.1 matt }
253 1.1 matt
254 1.1 matt const uint8_t *cmdptr = cmdbuf;
255 1.1 matt for (size_t i = 0; i < cmdlen; i++) {
256 1.1 matt I2C_WRITE(I2CDR, *cmdptr++);
257 1.1 matt
258 1.1 matt error = motoi2c_busy_wait(sc, cr);
259 1.1 matt if (error) {
260 1.3 phx DPRINTF(("%s: error sending cmd byte %zu (cr=%#x/%#x):"
261 1.3 phx " %d\n", __func__, i, I2C_READ(I2CCR), cr, error));
262 1.1 matt goto out;
263 1.1 matt }
264 1.1 matt }
265 1.1 matt
266 1.1 matt if (cmdlen > 0 && I2C_OP_READ_P(op)) {
267 1.1 matt KASSERT(cr & CR_MTX);
268 1.1 matt KASSERT((cr & CR_TXAK) == 0);
269 1.1 matt I2C_WRITE(I2CCR, cr | CR_RSTA);
270 1.1 matt #if 0
271 1.3 phx DPRINTF(("%s: restarted(read): sr=%#x cr=%#x(%#x)\n",
272 1.3 phx __func__, I2C_READ(I2CSR), cr | CR_RSTA, I2C_READ(I2CCR)));
273 1.1 matt #endif
274 1.1 matt
275 1.1 matt /* send target address and read transfer direction */
276 1.1 matt addr_byte |= 1;
277 1.1 matt I2C_WRITE(I2CDR, addr_byte);
278 1.1 matt
279 1.1 matt error = motoi2c_busy_wait(sc, cr);
280 1.1 matt if (error) {
281 1.1 matt if (error == EIO)
282 1.1 matt error = ENXIO;
283 1.1 matt goto out;
284 1.1 matt }
285 1.1 matt }
286 1.1 matt
287 1.1 matt if (I2C_OP_READ_P(op)) {
288 1.1 matt uint8_t *dataptr = databuf;
289 1.1 matt cr &= ~CR_MTX; /* clear transmit flags */
290 1.4 phx if (datalen <= 1)
291 1.1 matt cr |= CR_TXAK;
292 1.1 matt I2C_WRITE(I2CCR, cr);
293 1.1 matt DELAY(10);
294 1.1 matt (void)I2C_READ(I2CDR); /* dummy read */
295 1.1 matt for (size_t i = 0; i < datalen; i++) {
296 1.1 matt /*
297 1.1 matt * If a master receiver wants to terminate a data
298 1.1 matt * transfer, it must inform the slave transmitter by
299 1.1 matt * not acknowledging the last byte of data (by setting
300 1.1 matt * the transmit acknowledge bit (I2CCR[TXAK])) before
301 1.1 matt * reading the next-to-last byte of data.
302 1.1 matt */
303 1.1 matt error = motoi2c_busy_wait(sc, cr);
304 1.1 matt if (error) {
305 1.3 phx DPRINTF(("%s: error reading byte %zu: %d\n",
306 1.3 phx __func__, i, error));
307 1.1 matt goto out;
308 1.1 matt }
309 1.4 phx if (i == datalen - 2) {
310 1.4 phx cr |= CR_TXAK;
311 1.4 phx I2C_WRITE(I2CCR, cr);
312 1.4 phx } else if (i == datalen - 1 && I2C_OP_STOP_P(op)) {
313 1.4 phx cr = CR_MEN;
314 1.4 phx I2C_WRITE(I2CCR, cr);
315 1.1 matt }
316 1.1 matt *dataptr++ = I2C_READ(I2CDR);
317 1.1 matt }
318 1.1 matt if (datalen == 0) {
319 1.4 phx if (I2C_OP_STOP_P(op)) {
320 1.4 phx cr = CR_MEN;
321 1.4 phx I2C_WRITE(I2CCR, cr);
322 1.4 phx }
323 1.1 matt (void)I2C_READ(I2CDR); /* dummy read */
324 1.1 matt error = motoi2c_busy_wait(sc, cr);
325 1.1 matt if (error) {
326 1.3 phx DPRINTF(("%s: error reading dummy last byte:"
327 1.3 phx "%d\n", __func__, error));
328 1.1 matt goto out;
329 1.1 matt }
330 1.1 matt }
331 1.1 matt } else {
332 1.1 matt const uint8_t *dataptr = databuf;
333 1.1 matt for (size_t i = 0; i < datalen; i++) {
334 1.1 matt I2C_WRITE(I2CDR, *dataptr++);
335 1.1 matt error = motoi2c_busy_wait(sc, cr);
336 1.1 matt if (error) {
337 1.3 phx DPRINTF(("%s: error sending data byte %zu:"
338 1.3 phx " %d\n", __func__, i, error));
339 1.1 matt goto out;
340 1.1 matt }
341 1.1 matt }
342 1.1 matt }
343 1.1 matt
344 1.1 matt out:
345 1.1 matt /*
346 1.1 matt * If we encountered an error condition or caller wants a STOP,
347 1.1 matt * send a STOP.
348 1.1 matt */
349 1.1 matt if (error || (cr & CR_TXAK) || ((cr & CR_MSTA) && I2C_OP_STOP_P(op))) {
350 1.1 matt cr = CR_MEN;
351 1.1 matt I2C_WRITE(I2CCR, cr);
352 1.3 phx DPRINTF(("%s: stopping: cr=%#x/%#x\n", __func__,
353 1.3 phx cr, I2C_READ(I2CCR)));
354 1.1 matt }
355 1.1 matt
356 1.3 phx DPRINTF(("%s: exit sr=%#x cr=%#x: %d\n", __func__,
357 1.3 phx I2C_READ(I2CSR), I2C_READ(I2CCR), error));
358 1.1 matt
359 1.1 matt return error;
360 1.1 matt }
361