motoi2c.c revision 1.13 1 1.13 thorpej /* $NetBSD: motoi2c.c,v 1.13 2022/07/22 23:43:23 thorpej 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.13 thorpej __KERNEL_RCSID(0, "$NetBSD: motoi2c.c,v 1.13 2022/07/22 23:43:23 thorpej Exp $");
34 1.5 hkenken
35 1.5 hkenken #if defined(__arm__) || defined(__aarch64__)
36 1.5 hkenken #include "opt_fdt.h"
37 1.5 hkenken #endif
38 1.1 matt
39 1.1 matt #include <sys/param.h>
40 1.1 matt #include <sys/device.h>
41 1.1 matt #include <sys/systm.h>
42 1.1 matt #include <sys/mutex.h>
43 1.1 matt #include <sys/bus.h>
44 1.1 matt #include <sys/intr.h>
45 1.1 matt
46 1.1 matt #include <dev/i2c/i2cvar.h>
47 1.1 matt #include <dev/i2c/motoi2creg.h>
48 1.1 matt #include <dev/i2c/motoi2cvar.h>
49 1.1 matt
50 1.5 hkenken #ifdef FDT
51 1.5 hkenken #include <dev/fdt/fdtvar.h>
52 1.5 hkenken #endif
53 1.5 hkenken
54 1.1 matt #ifdef DEBUG
55 1.3 phx int motoi2c_debug = 0;
56 1.3 phx #define DPRINTF(x) if (motoi2c_debug) printf x
57 1.1 matt #else
58 1.3 phx #define DPRINTF(x)
59 1.1 matt #endif
60 1.1 matt
61 1.1 matt static int motoi2c_acquire_bus(void *, int);
62 1.1 matt static void motoi2c_release_bus(void *, int);
63 1.1 matt static int motoi2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
64 1.1 matt void *, size_t, int);
65 1.13 thorpej static void motoi2c_clear_status(struct motoi2c_softc *, uint8_t);
66 1.1 matt static int motoi2c_busy_wait(struct motoi2c_softc *, uint8_t);
67 1.1 matt
68 1.1 matt static const struct motoi2c_settings motoi2c_default_settings = {
69 1.1 matt .i2c_adr = MOTOI2C_ADR_DEFAULT,
70 1.1 matt .i2c_fdr = MOTOI2C_FDR_DEFAULT,
71 1.1 matt .i2c_dfsrr = MOTOI2C_DFSRR_DEFAULT,
72 1.1 matt };
73 1.1 matt
74 1.1 matt #define I2C_READ(r) ((*sc->sc_iord)(sc, (r)))
75 1.1 matt #define I2C_WRITE(r,v) ((*sc->sc_iowr)(sc, (r), (v)))
76 1.1 matt #define I2C_SETCLR(r, s, c) \
77 1.1 matt ((*sc->sc_iowr)(sc, (r), ((*sc->sc_iord)(sc, (r)) | (s)) & ~(c)))
78 1.1 matt
79 1.1 matt static uint8_t
80 1.1 matt motoi2c_iord1(struct motoi2c_softc *sc, bus_size_t off)
81 1.1 matt {
82 1.1 matt return bus_space_read_1(sc->sc_iot, sc->sc_ioh, off);
83 1.1 matt }
84 1.1 matt
85 1.1 matt static void
86 1.1 matt motoi2c_iowr1(struct motoi2c_softc *sc, bus_size_t off, uint8_t data)
87 1.1 matt {
88 1.1 matt bus_space_write_1(sc->sc_iot, sc->sc_ioh, off, data);
89 1.1 matt }
90 1.1 matt
91 1.1 matt void
92 1.13 thorpej motoi2c_attach(struct motoi2c_softc *sc,
93 1.13 thorpej const struct motoi2c_settings *settings)
94 1.1 matt {
95 1.1 matt struct i2cbus_attach_args iba;
96 1.1 matt
97 1.13 thorpej if (settings == NULL) {
98 1.13 thorpej sc->sc_settings = motoi2c_default_settings;
99 1.13 thorpej } else {
100 1.13 thorpej sc->sc_settings = *settings;
101 1.13 thorpej }
102 1.13 thorpej if (sc->sc_iord == NULL)
103 1.13 thorpej sc->sc_iord = motoi2c_iord1;
104 1.13 thorpej if (sc->sc_iowr == NULL)
105 1.13 thorpej sc->sc_iowr = motoi2c_iowr1;
106 1.1 matt
107 1.7 thorpej iic_tag_init(&sc->sc_i2c);
108 1.1 matt sc->sc_i2c.ic_cookie = sc;
109 1.7 thorpej sc->sc_i2c.ic_acquire_bus = motoi2c_acquire_bus;
110 1.7 thorpej sc->sc_i2c.ic_release_bus = motoi2c_release_bus;
111 1.7 thorpej sc->sc_i2c.ic_exec = motoi2c_exec;
112 1.1 matt memset(&iba, 0, sizeof(iba));
113 1.1 matt iba.iba_tag = &sc->sc_i2c;
114 1.9 jmcneill iba.iba_child_devices = sc->sc_child_devices;
115 1.1 matt
116 1.10 jmcneill if ((sc->sc_flags & MOTOI2C_F_ENABLE_INV) != 0) {
117 1.10 jmcneill sc->sc_enable_mask = 0;
118 1.10 jmcneill sc->sc_disable_mask = CR_MEN;
119 1.10 jmcneill } else {
120 1.10 jmcneill sc->sc_enable_mask = CR_MEN;
121 1.10 jmcneill sc->sc_disable_mask = 0;
122 1.10 jmcneill }
123 1.10 jmcneill
124 1.10 jmcneill I2C_WRITE(I2CCR, sc->sc_disable_mask); /* reset before config */
125 1.13 thorpej I2C_WRITE(I2CDFSRR, sc->sc_settings.i2c_dfsrr); /* sampling units */
126 1.13 thorpej I2C_WRITE(I2CFDR, sc->sc_settings.i2c_fdr); /* divider 3072 */
127 1.13 thorpej I2C_WRITE(I2CADR, sc->sc_settings.i2c_adr); /* our slave address */
128 1.13 thorpej motoi2c_clear_status(sc, I2C_READ(I2CSR));
129 1.1 matt
130 1.5 hkenken #ifdef FDT
131 1.9 jmcneill if (sc->sc_phandle != 0) {
132 1.9 jmcneill fdtbus_register_i2c_controller(&sc->sc_i2c, sc->sc_phandle);
133 1.13 thorpej fdtbus_attach_i2cbus(sc->sc_dev, sc->sc_phandle, &sc->sc_i2c,
134 1.9 jmcneill iicbus_print);
135 1.9 jmcneill } else
136 1.9 jmcneill #endif
137 1.13 thorpej config_found(sc->sc_dev, &iba, iicbus_print,
138 1.12 thorpej CFARGS(.iattr = "i2cbus"));
139 1.1 matt }
140 1.1 matt
141 1.1 matt static int
142 1.1 matt motoi2c_acquire_bus(void *v, int flags)
143 1.1 matt {
144 1.1 matt struct motoi2c_softc * const sc = v;
145 1.1 matt
146 1.10 jmcneill I2C_WRITE(I2CCR, sc->sc_enable_mask); /* enable the I2C module */
147 1.1 matt
148 1.1 matt return 0;
149 1.1 matt }
150 1.1 matt
151 1.1 matt static void
152 1.1 matt motoi2c_release_bus(void *v, int flags)
153 1.1 matt {
154 1.1 matt struct motoi2c_softc * const sc = v;
155 1.1 matt
156 1.10 jmcneill I2C_WRITE(I2CCR, sc->sc_disable_mask); /* disable the I2C module */
157 1.1 matt }
158 1.1 matt
159 1.6 hkenken static int
160 1.6 hkenken motoi2c_stop_wait(struct motoi2c_softc *sc)
161 1.6 hkenken {
162 1.6 hkenken u_int timo;
163 1.6 hkenken int error = 0;
164 1.6 hkenken
165 1.6 hkenken timo = 1000;
166 1.6 hkenken while ((I2C_READ(I2CSR) & SR_MBB) != 0 && --timo)
167 1.6 hkenken DELAY(1);
168 1.6 hkenken
169 1.6 hkenken if (timo == 0) {
170 1.6 hkenken DPRINTF(("%s: timeout (sr=%#x)\n", __func__, I2C_READ(I2CSR)));
171 1.6 hkenken error = ETIMEDOUT;
172 1.6 hkenken }
173 1.6 hkenken
174 1.6 hkenken return error;
175 1.6 hkenken }
176 1.6 hkenken
177 1.10 jmcneill static void
178 1.10 jmcneill motoi2c_clear_status(struct motoi2c_softc *sc, uint8_t sr)
179 1.10 jmcneill {
180 1.10 jmcneill if ((sc->sc_flags & MOTOI2C_F_STATUS_W1C) != 0) {
181 1.10 jmcneill I2C_WRITE(I2CSR, sr);
182 1.10 jmcneill } else {
183 1.10 jmcneill I2C_WRITE(I2CSR, 0);
184 1.10 jmcneill }
185 1.10 jmcneill }
186 1.10 jmcneill
187 1.1 matt /* busy waiting for byte data transfer completion */
188 1.1 matt static int
189 1.1 matt motoi2c_busy_wait(struct motoi2c_softc *sc, uint8_t cr)
190 1.1 matt {
191 1.1 matt uint8_t sr;
192 1.1 matt u_int timo;
193 1.1 matt int error = 0;
194 1.1 matt
195 1.1 matt timo = 1000;
196 1.1 matt while (((sr = I2C_READ(I2CSR)) & SR_MIF) == 0 && --timo)
197 1.1 matt DELAY(10);
198 1.1 matt
199 1.1 matt if (timo == 0) {
200 1.3 phx DPRINTF(("%s: timeout (sr=%#x, cr=%#x)\n",
201 1.3 phx __func__, sr, I2C_READ(I2CCR)));
202 1.1 matt error = ETIMEDOUT;
203 1.1 matt }
204 1.1 matt /*
205 1.1 matt * RXAK is only valid when transmitting.
206 1.1 matt */
207 1.1 matt if ((cr & CR_MTX) && (sr & SR_RXAK)) {
208 1.3 phx DPRINTF(("%s: missing rx ack (%#x): spin=%u\n",
209 1.3 phx __func__, sr, 1000 - timo));
210 1.1 matt error = EIO;
211 1.1 matt }
212 1.10 jmcneill motoi2c_clear_status(sc, sr);
213 1.1 matt return error;
214 1.1 matt }
215 1.1 matt
216 1.1 matt int
217 1.1 matt motoi2c_intr(void *v)
218 1.1 matt {
219 1.1 matt struct motoi2c_softc * const sc = v;
220 1.1 matt
221 1.1 matt panic("%s(%p)", __func__, sc);
222 1.1 matt
223 1.1 matt return 0;
224 1.1 matt }
225 1.1 matt
226 1.1 matt int
227 1.1 matt motoi2c_exec(void *v, i2c_op_t op, i2c_addr_t addr,
228 1.1 matt const void *cmdbuf, size_t cmdlen,
229 1.1 matt void *databuf, size_t datalen,
230 1.1 matt int flags)
231 1.1 matt {
232 1.1 matt struct motoi2c_softc * const sc = v;
233 1.1 matt uint8_t sr;
234 1.1 matt uint8_t cr;
235 1.1 matt int error;
236 1.1 matt
237 1.1 matt sr = I2C_READ(I2CSR);
238 1.1 matt cr = I2C_READ(I2CCR);
239 1.1 matt
240 1.1 matt #if 0
241 1.3 phx DPRINTF(("%s(%#x,%#x,%p,%zu,%p,%zu,%#x): sr=%#x cr=%#x\n",
242 1.1 matt __func__, op, addr, cmdbuf, cmdlen, databuf, datalen, flags,
243 1.3 phx sr, cr));
244 1.1 matt #endif
245 1.1 matt
246 1.1 matt if ((cr & CR_MSTA) == 0 && (sr & SR_MBB) != 0) {
247 1.1 matt /* wait for bus becoming available */
248 1.6 hkenken error = motoi2c_stop_wait(sc);
249 1.6 hkenken if (error)
250 1.1 matt return ETIMEDOUT;
251 1.1 matt }
252 1.1 matt
253 1.1 matt /* reset interrupt and arbitration-lost flags (all others are RO) */
254 1.10 jmcneill motoi2c_clear_status(sc, sr);
255 1.1 matt sr = I2C_READ(I2CSR);
256 1.1 matt
257 1.1 matt /*
258 1.6 hkenken * Generate start condition
259 1.1 matt */
260 1.10 jmcneill cr = sc->sc_enable_mask | CR_MTX | CR_MSTA;
261 1.6 hkenken I2C_WRITE(I2CCR, cr);
262 1.1 matt
263 1.3 phx DPRINTF(("%s: started: sr=%#x cr=%#x/%#x\n",
264 1.3 phx __func__, I2C_READ(I2CSR), cr, I2C_READ(I2CCR)));
265 1.1 matt
266 1.1 matt sr = I2C_READ(I2CSR);
267 1.1 matt if (sr & SR_MAL) {
268 1.3 phx DPRINTF(("%s: lost bus: sr=%#x cr=%#x/%#x\n",
269 1.3 phx __func__, I2C_READ(I2CSR), cr, I2C_READ(I2CCR)));
270 1.10 jmcneill I2C_WRITE(I2CCR, sc->sc_disable_mask);
271 1.1 matt DELAY(10);
272 1.10 jmcneill I2C_WRITE(I2CCR, sc->sc_enable_mask | CR_MTX | CR_MSTA);
273 1.1 matt DELAY(10);
274 1.1 matt sr = I2C_READ(I2CSR);
275 1.1 matt if (sr & SR_MAL) {
276 1.1 matt error = EBUSY;
277 1.1 matt goto out;
278 1.1 matt }
279 1.3 phx DPRINTF(("%s: reacquired bus: sr=%#x cr=%#x/%#x\n",
280 1.3 phx __func__, I2C_READ(I2CSR), cr, I2C_READ(I2CCR)));
281 1.1 matt }
282 1.1 matt
283 1.1 matt /* send target address and transfer direction */
284 1.1 matt uint8_t addr_byte = (addr << 1)
285 1.1 matt | (cmdlen == 0 && I2C_OP_READ_P(op) ? 1 : 0);
286 1.1 matt I2C_WRITE(I2CDR, addr_byte);
287 1.1 matt
288 1.1 matt error = motoi2c_busy_wait(sc, cr);
289 1.1 matt if (error) {
290 1.3 phx DPRINTF(("%s: error sending address: %d\n", __func__, error));
291 1.1 matt if (error == EIO)
292 1.1 matt error = ENXIO;
293 1.1 matt goto out;
294 1.1 matt }
295 1.1 matt
296 1.1 matt const uint8_t *cmdptr = cmdbuf;
297 1.1 matt for (size_t i = 0; i < cmdlen; i++) {
298 1.1 matt I2C_WRITE(I2CDR, *cmdptr++);
299 1.1 matt
300 1.1 matt error = motoi2c_busy_wait(sc, cr);
301 1.1 matt if (error) {
302 1.3 phx DPRINTF(("%s: error sending cmd byte %zu (cr=%#x/%#x):"
303 1.3 phx " %d\n", __func__, i, I2C_READ(I2CCR), cr, error));
304 1.1 matt goto out;
305 1.1 matt }
306 1.1 matt }
307 1.1 matt
308 1.1 matt if (cmdlen > 0 && I2C_OP_READ_P(op)) {
309 1.1 matt KASSERT(cr & CR_MTX);
310 1.1 matt KASSERT((cr & CR_TXAK) == 0);
311 1.1 matt I2C_WRITE(I2CCR, cr | CR_RSTA);
312 1.1 matt #if 0
313 1.3 phx DPRINTF(("%s: restarted(read): sr=%#x cr=%#x(%#x)\n",
314 1.3 phx __func__, I2C_READ(I2CSR), cr | CR_RSTA, I2C_READ(I2CCR)));
315 1.1 matt #endif
316 1.1 matt
317 1.1 matt /* send target address and read transfer direction */
318 1.1 matt addr_byte |= 1;
319 1.1 matt I2C_WRITE(I2CDR, addr_byte);
320 1.1 matt
321 1.1 matt error = motoi2c_busy_wait(sc, cr);
322 1.1 matt if (error) {
323 1.1 matt if (error == EIO)
324 1.1 matt error = ENXIO;
325 1.1 matt goto out;
326 1.1 matt }
327 1.1 matt }
328 1.1 matt
329 1.1 matt if (I2C_OP_READ_P(op)) {
330 1.1 matt uint8_t *dataptr = databuf;
331 1.1 matt cr &= ~CR_MTX; /* clear transmit flags */
332 1.4 phx if (datalen <= 1)
333 1.1 matt cr |= CR_TXAK;
334 1.1 matt I2C_WRITE(I2CCR, cr);
335 1.1 matt DELAY(10);
336 1.1 matt (void)I2C_READ(I2CDR); /* dummy read */
337 1.1 matt for (size_t i = 0; i < datalen; i++) {
338 1.1 matt /*
339 1.1 matt * If a master receiver wants to terminate a data
340 1.1 matt * transfer, it must inform the slave transmitter by
341 1.1 matt * not acknowledging the last byte of data (by setting
342 1.1 matt * the transmit acknowledge bit (I2CCR[TXAK])) before
343 1.1 matt * reading the next-to-last byte of data.
344 1.1 matt */
345 1.1 matt error = motoi2c_busy_wait(sc, cr);
346 1.1 matt if (error) {
347 1.3 phx DPRINTF(("%s: error reading byte %zu: %d\n",
348 1.3 phx __func__, i, error));
349 1.1 matt goto out;
350 1.1 matt }
351 1.4 phx if (i == datalen - 2) {
352 1.4 phx cr |= CR_TXAK;
353 1.4 phx I2C_WRITE(I2CCR, cr);
354 1.4 phx } else if (i == datalen - 1 && I2C_OP_STOP_P(op)) {
355 1.10 jmcneill cr = sc->sc_enable_mask | CR_TXAK;
356 1.4 phx I2C_WRITE(I2CCR, cr);
357 1.1 matt }
358 1.1 matt *dataptr++ = I2C_READ(I2CDR);
359 1.1 matt }
360 1.1 matt if (datalen == 0) {
361 1.4 phx if (I2C_OP_STOP_P(op)) {
362 1.10 jmcneill cr = sc->sc_enable_mask | CR_TXAK;
363 1.4 phx I2C_WRITE(I2CCR, cr);
364 1.4 phx }
365 1.1 matt (void)I2C_READ(I2CDR); /* dummy read */
366 1.1 matt error = motoi2c_busy_wait(sc, cr);
367 1.1 matt if (error) {
368 1.3 phx DPRINTF(("%s: error reading dummy last byte:"
369 1.3 phx "%d\n", __func__, error));
370 1.1 matt goto out;
371 1.1 matt }
372 1.1 matt }
373 1.1 matt } else {
374 1.1 matt const uint8_t *dataptr = databuf;
375 1.1 matt for (size_t i = 0; i < datalen; i++) {
376 1.1 matt I2C_WRITE(I2CDR, *dataptr++);
377 1.1 matt error = motoi2c_busy_wait(sc, cr);
378 1.1 matt if (error) {
379 1.3 phx DPRINTF(("%s: error sending data byte %zu:"
380 1.3 phx " %d\n", __func__, i, error));
381 1.1 matt goto out;
382 1.1 matt }
383 1.1 matt }
384 1.1 matt }
385 1.1 matt
386 1.1 matt out:
387 1.1 matt /*
388 1.1 matt * If we encountered an error condition or caller wants a STOP,
389 1.1 matt * send a STOP.
390 1.1 matt */
391 1.1 matt if (error || (cr & CR_TXAK) || ((cr & CR_MSTA) && I2C_OP_STOP_P(op))) {
392 1.10 jmcneill cr = sc->sc_enable_mask;
393 1.1 matt I2C_WRITE(I2CCR, cr);
394 1.6 hkenken motoi2c_stop_wait(sc);
395 1.3 phx DPRINTF(("%s: stopping: cr=%#x/%#x\n", __func__,
396 1.3 phx cr, I2C_READ(I2CCR)));
397 1.1 matt }
398 1.1 matt
399 1.3 phx DPRINTF(("%s: exit sr=%#x cr=%#x: %d\n", __func__,
400 1.3 phx I2C_READ(I2CSR), I2C_READ(I2CCR), error));
401 1.1 matt
402 1.1 matt return error;
403 1.1 matt }
404