ti_iic.c revision 1.1 1 1.1 jmcneill /* $NetBSD: ti_iic.c,v 1.1 2019/10/27 19:11:07 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*
4 1.1 jmcneill * Copyright (c) 2013 Manuel Bouyer. All rights reserved.
5 1.1 jmcneill *
6 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
7 1.1 jmcneill * modification, are permitted provided that the following conditions
8 1.1 jmcneill * are met:
9 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
10 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
11 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
13 1.1 jmcneill * documentation and/or other materials provided with the distribution.
14 1.1 jmcneill *
15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 jmcneill * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 jmcneill * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 jmcneill * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 jmcneill * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 jmcneill * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 jmcneill */
26 1.1 jmcneill
27 1.1 jmcneill /*-
28 1.1 jmcneill * Copyright (c) 2012 Jared D. McNeill <jmcneill (at) invisible.ca>
29 1.1 jmcneill * All rights reserved.
30 1.1 jmcneill *
31 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
32 1.1 jmcneill * modification, are permitted provided that the following conditions
33 1.1 jmcneill * are met:
34 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
35 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
36 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products
37 1.1 jmcneill * derived from this software without specific prior written permission.
38 1.1 jmcneill *
39 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
40 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
43 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
44 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 1.1 jmcneill * SUCH DAMAGE.
50 1.1 jmcneill */
51 1.1 jmcneill
52 1.1 jmcneill #include <sys/cdefs.h>
53 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: ti_iic.c,v 1.1 2019/10/27 19:11:07 jmcneill Exp $");
54 1.1 jmcneill
55 1.1 jmcneill #include <sys/param.h>
56 1.1 jmcneill #include <sys/systm.h>
57 1.1 jmcneill #include <sys/device.h>
58 1.1 jmcneill #include <sys/conf.h>
59 1.1 jmcneill #include <sys/bus.h>
60 1.1 jmcneill #include <sys/proc.h>
61 1.1 jmcneill #include <sys/kernel.h>
62 1.1 jmcneill #include <sys/mutex.h>
63 1.1 jmcneill #include <sys/condvar.h>
64 1.1 jmcneill
65 1.1 jmcneill #include <dev/i2c/i2cvar.h>
66 1.1 jmcneill
67 1.1 jmcneill #include <dev/fdt/fdtvar.h>
68 1.1 jmcneill
69 1.1 jmcneill #include <arm/ti/ti_prcm.h>
70 1.1 jmcneill #include <arm/ti/ti_iicreg.h>
71 1.1 jmcneill
72 1.1 jmcneill #ifndef OMAP2_I2C_SLAVE_ADDR
73 1.1 jmcneill #define OMAP2_I2C_SLAVE_ADDR 0x01
74 1.1 jmcneill #endif
75 1.1 jmcneill
76 1.1 jmcneill #define OMAP2_I2C_FIFOBYTES(fd) (8 << (fd))
77 1.1 jmcneill
78 1.1 jmcneill #ifdef I2CDEBUG
79 1.1 jmcneill #define DPRINTF(args) printf args
80 1.1 jmcneill #else
81 1.1 jmcneill #define DPRINTF(args)
82 1.1 jmcneill #endif
83 1.1 jmcneill
84 1.1 jmcneill static const char * compatible [] = {
85 1.1 jmcneill "ti,omap4-i2c",
86 1.1 jmcneill NULL
87 1.1 jmcneill };
88 1.1 jmcneill
89 1.1 jmcneill /* operation in progress */
90 1.1 jmcneill typedef enum {
91 1.1 jmcneill TI_I2CREAD,
92 1.1 jmcneill TI_I2CWRITE,
93 1.1 jmcneill TI_I2CDONE,
94 1.1 jmcneill TI_I2CERROR
95 1.1 jmcneill } ti_i2cop_t;
96 1.1 jmcneill
97 1.1 jmcneill struct ti_iic_softc {
98 1.1 jmcneill device_t sc_dev;
99 1.1 jmcneill struct i2c_controller sc_ic;
100 1.1 jmcneill kmutex_t sc_lock;
101 1.1 jmcneill device_t sc_i2cdev;
102 1.1 jmcneill
103 1.1 jmcneill bus_space_tag_t sc_iot;
104 1.1 jmcneill bus_space_handle_t sc_ioh;
105 1.1 jmcneill
106 1.1 jmcneill void *sc_ih;
107 1.1 jmcneill kmutex_t sc_mtx;
108 1.1 jmcneill kcondvar_t sc_cv;
109 1.1 jmcneill ti_i2cop_t sc_op;
110 1.1 jmcneill int sc_buflen;
111 1.1 jmcneill int sc_bufidx;
112 1.1 jmcneill char *sc_buf;
113 1.1 jmcneill
114 1.1 jmcneill bool sc_busy;
115 1.1 jmcneill
116 1.1 jmcneill int sc_rxthres;
117 1.1 jmcneill int sc_txthres;
118 1.1 jmcneill };
119 1.1 jmcneill
120 1.1 jmcneill #define I2C_READ_REG(sc, reg) \
121 1.1 jmcneill bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, (reg))
122 1.1 jmcneill #define I2C_READ_DATA(sc) \
123 1.1 jmcneill bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, OMAP2_I2C_DATA);
124 1.1 jmcneill #define I2C_WRITE_REG(sc, reg, val) \
125 1.1 jmcneill bus_space_write_2((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
126 1.1 jmcneill #define I2C_WRITE_DATA(sc, val) \
127 1.1 jmcneill bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, OMAP2_I2C_DATA, (val))
128 1.1 jmcneill
129 1.1 jmcneill static int ti_iic_match(device_t, cfdata_t, void *);
130 1.1 jmcneill static void ti_iic_attach(device_t, device_t, void *);
131 1.1 jmcneill
132 1.1 jmcneill static int ti_iic_intr(void *);
133 1.1 jmcneill
134 1.1 jmcneill static int ti_iic_acquire_bus(void *, int);
135 1.1 jmcneill static void ti_iic_release_bus(void *, int);
136 1.1 jmcneill static int ti_iic_exec(void *, i2c_op_t, i2c_addr_t, const void *,
137 1.1 jmcneill size_t, void *, size_t, int);
138 1.1 jmcneill
139 1.1 jmcneill static int ti_iic_reset(struct ti_iic_softc *);
140 1.1 jmcneill static int ti_iic_op(struct ti_iic_softc *, i2c_addr_t, ti_i2cop_t,
141 1.1 jmcneill uint8_t *, size_t, int);
142 1.1 jmcneill static void ti_iic_handle_intr(struct ti_iic_softc *, uint32_t);
143 1.1 jmcneill static void ti_iic_do_read(struct ti_iic_softc *, uint32_t);
144 1.1 jmcneill static void ti_iic_do_write(struct ti_iic_softc *, uint32_t);
145 1.1 jmcneill
146 1.1 jmcneill static int ti_iic_wait(struct ti_iic_softc *, uint16_t, uint16_t, int);
147 1.1 jmcneill static uint32_t ti_iic_stat(struct ti_iic_softc *, uint32_t);
148 1.1 jmcneill static int ti_iic_flush(struct ti_iic_softc *);
149 1.1 jmcneill
150 1.1 jmcneill static i2c_tag_t ti_iic_get_tag(device_t);
151 1.1 jmcneill
152 1.1 jmcneill static const struct fdtbus_i2c_controller_func ti_iic_funcs = {
153 1.1 jmcneill .get_tag = ti_iic_get_tag,
154 1.1 jmcneill };
155 1.1 jmcneill
156 1.1 jmcneill CFATTACH_DECL_NEW(ti_iic, sizeof(struct ti_iic_softc),
157 1.1 jmcneill ti_iic_match, ti_iic_attach, NULL, NULL);
158 1.1 jmcneill
159 1.1 jmcneill static int
160 1.1 jmcneill ti_iic_match(device_t parent, cfdata_t match, void *opaque)
161 1.1 jmcneill {
162 1.1 jmcneill struct fdt_attach_args * const faa = opaque;
163 1.1 jmcneill
164 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
165 1.1 jmcneill }
166 1.1 jmcneill
167 1.1 jmcneill static void
168 1.1 jmcneill ti_iic_attach(device_t parent, device_t self, void *opaque)
169 1.1 jmcneill {
170 1.1 jmcneill struct ti_iic_softc *sc = device_private(self);
171 1.1 jmcneill struct fdt_attach_args * const faa = opaque;
172 1.1 jmcneill const int phandle = faa->faa_phandle;
173 1.1 jmcneill int scheme, major, minor, fifodepth, fifo;
174 1.1 jmcneill char intrstr[128];
175 1.1 jmcneill bus_addr_t addr;
176 1.1 jmcneill bus_size_t size;
177 1.1 jmcneill uint16_t rev;
178 1.1 jmcneill
179 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
180 1.1 jmcneill aprint_error(": couldn't get registers\n");
181 1.1 jmcneill return;
182 1.1 jmcneill }
183 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
184 1.1 jmcneill aprint_error(": couldn't decode interrupt\n");
185 1.1 jmcneill return;
186 1.1 jmcneill }
187 1.1 jmcneill
188 1.1 jmcneill if (ti_prcm_enable_hwmod(OF_parent(phandle), 0) != 0) {
189 1.1 jmcneill aprint_error(": couldn't enable module\n");
190 1.1 jmcneill return;
191 1.1 jmcneill }
192 1.1 jmcneill
193 1.1 jmcneill sc->sc_dev = self;
194 1.1 jmcneill sc->sc_iot = faa->faa_bst;
195 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
196 1.1 jmcneill mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NET);
197 1.1 jmcneill cv_init(&sc->sc_cv, "tiiic");
198 1.1 jmcneill sc->sc_ic.ic_cookie = sc;
199 1.1 jmcneill sc->sc_ic.ic_acquire_bus = ti_iic_acquire_bus;
200 1.1 jmcneill sc->sc_ic.ic_release_bus = ti_iic_release_bus;
201 1.1 jmcneill sc->sc_ic.ic_exec = ti_iic_exec;
202 1.1 jmcneill
203 1.1 jmcneill if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
204 1.1 jmcneill aprint_error(": couldn't map registers\n");
205 1.1 jmcneill return;
206 1.1 jmcneill }
207 1.1 jmcneill
208 1.1 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_NET, 0,
209 1.1 jmcneill ti_iic_intr, sc);
210 1.1 jmcneill if (sc->sc_ih == NULL) {
211 1.1 jmcneill aprint_error(": couldn't establish interrupt\n");
212 1.1 jmcneill return;
213 1.1 jmcneill }
214 1.1 jmcneill
215 1.1 jmcneill scheme = I2C_REVNB_HI_SCHEME(I2C_READ_REG(sc, OMAP2_I2C_REVNB_HI));
216 1.1 jmcneill rev = I2C_READ_REG(sc, OMAP2_I2C_REVNB_LO);
217 1.1 jmcneill if (scheme == 0) {
218 1.1 jmcneill major = I2C_REV_SCHEME_0_MAJOR(rev);
219 1.1 jmcneill minor = I2C_REV_SCHEME_0_MINOR(rev);
220 1.1 jmcneill } else {
221 1.1 jmcneill major = I2C_REVNB_LO_MAJOR(rev);
222 1.1 jmcneill minor = I2C_REVNB_LO_MINOR(rev);
223 1.1 jmcneill }
224 1.1 jmcneill aprint_normal(": rev %d.%d, scheme %d\n", major, minor, scheme);
225 1.1 jmcneill aprint_naive("\n");
226 1.1 jmcneill
227 1.1 jmcneill fifodepth = I2C_BUFSTAT_FIFODEPTH(I2C_READ_REG(sc, OMAP2_I2C_BUFSTAT));
228 1.1 jmcneill fifo = OMAP2_I2C_FIFOBYTES(fifodepth);
229 1.1 jmcneill aprint_normal_dev(self, "%d-bytes FIFO\n", fifo);
230 1.1 jmcneill sc->sc_rxthres = sc->sc_txthres = fifo >> 1;
231 1.1 jmcneill
232 1.1 jmcneill ti_iic_reset(sc);
233 1.1 jmcneill ti_iic_flush(sc);
234 1.1 jmcneill
235 1.1 jmcneill fdtbus_register_i2c_controller(self, phandle, &ti_iic_funcs);
236 1.1 jmcneill
237 1.1 jmcneill fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
238 1.1 jmcneill }
239 1.1 jmcneill
240 1.1 jmcneill static int
241 1.1 jmcneill ti_iic_intr(void *arg)
242 1.1 jmcneill {
243 1.1 jmcneill struct ti_iic_softc *sc = arg;
244 1.1 jmcneill uint32_t stat;
245 1.1 jmcneill
246 1.1 jmcneill mutex_enter(&sc->sc_mtx);
247 1.1 jmcneill DPRINTF(("ti_iic_intr\n"));
248 1.1 jmcneill stat = I2C_READ_REG(sc, OMAP2_I2C_IRQSTATUS);
249 1.1 jmcneill DPRINTF(("ti_iic_intr pre handle sc->sc_op eq %#x\n", sc->sc_op));
250 1.1 jmcneill ti_iic_handle_intr(sc, stat);
251 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_IRQSTATUS, stat);
252 1.1 jmcneill if (sc->sc_op == TI_I2CERROR || sc->sc_op == TI_I2CDONE) {
253 1.1 jmcneill DPRINTF(("ti_iic_intr post handle sc->sc_op %#x\n", sc->sc_op));
254 1.1 jmcneill cv_broadcast(&sc->sc_cv);
255 1.1 jmcneill }
256 1.1 jmcneill mutex_exit(&sc->sc_mtx);
257 1.1 jmcneill DPRINTF(("ti_iic_intr status 0x%x\n", stat));
258 1.1 jmcneill return 1;
259 1.1 jmcneill }
260 1.1 jmcneill
261 1.1 jmcneill static int
262 1.1 jmcneill ti_iic_acquire_bus(void *opaque, int flags)
263 1.1 jmcneill {
264 1.1 jmcneill struct ti_iic_softc *sc = opaque;
265 1.1 jmcneill
266 1.1 jmcneill mutex_enter(&sc->sc_lock);
267 1.1 jmcneill while (sc->sc_busy)
268 1.1 jmcneill cv_wait(&sc->sc_cv, &sc->sc_lock);
269 1.1 jmcneill sc->sc_busy = true;
270 1.1 jmcneill mutex_exit(&sc->sc_lock);
271 1.1 jmcneill
272 1.1 jmcneill return 0;
273 1.1 jmcneill }
274 1.1 jmcneill
275 1.1 jmcneill static void
276 1.1 jmcneill ti_iic_release_bus(void *opaque, int flags)
277 1.1 jmcneill {
278 1.1 jmcneill struct ti_iic_softc *sc = opaque;
279 1.1 jmcneill
280 1.1 jmcneill mutex_enter(&sc->sc_lock);
281 1.1 jmcneill sc->sc_busy = false;
282 1.1 jmcneill cv_broadcast(&sc->sc_cv);
283 1.1 jmcneill mutex_exit(&sc->sc_lock);
284 1.1 jmcneill }
285 1.1 jmcneill
286 1.1 jmcneill static int
287 1.1 jmcneill ti_iic_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
288 1.1 jmcneill const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
289 1.1 jmcneill {
290 1.1 jmcneill struct ti_iic_softc *sc = opaque;
291 1.1 jmcneill int err;
292 1.1 jmcneill
293 1.1 jmcneill DPRINTF(("ti_iic_exec: op 0x%x cmdlen %zd len %zd flags 0x%x\n",
294 1.1 jmcneill op, cmdlen, len, flags));
295 1.1 jmcneill
296 1.1 jmcneill if (cmdlen > 0) {
297 1.1 jmcneill err = ti_iic_op(sc, addr, TI_I2CWRITE,
298 1.1 jmcneill __UNCONST(cmdbuf), cmdlen,
299 1.1 jmcneill (I2C_OP_READ_P(op) ? 0 : I2C_F_STOP) | flags);
300 1.1 jmcneill if (err)
301 1.1 jmcneill goto done;
302 1.1 jmcneill }
303 1.1 jmcneill
304 1.1 jmcneill if (I2C_OP_STOP_P(op))
305 1.1 jmcneill flags |= I2C_F_STOP;
306 1.1 jmcneill
307 1.1 jmcneill /*
308 1.1 jmcneill * I2C controller doesn't allow for zero-byte transfers.
309 1.1 jmcneill */
310 1.1 jmcneill if (len == 0) {
311 1.1 jmcneill err = EINVAL;
312 1.1 jmcneill goto done;
313 1.1 jmcneill }
314 1.1 jmcneill
315 1.1 jmcneill if (I2C_OP_READ_P(op)) {
316 1.1 jmcneill err = ti_iic_op(sc, addr, TI_I2CREAD, buf, len, flags);
317 1.1 jmcneill } else {
318 1.1 jmcneill err = ti_iic_op(sc, addr, TI_I2CWRITE, buf, len, flags);
319 1.1 jmcneill }
320 1.1 jmcneill
321 1.1 jmcneill done:
322 1.1 jmcneill if (err)
323 1.1 jmcneill ti_iic_reset(sc);
324 1.1 jmcneill
325 1.1 jmcneill ti_iic_flush(sc);
326 1.1 jmcneill
327 1.1 jmcneill DPRINTF(("ti_iic_exec: done %d\n", err));
328 1.1 jmcneill return err;
329 1.1 jmcneill }
330 1.1 jmcneill
331 1.1 jmcneill static int
332 1.1 jmcneill ti_iic_reset(struct ti_iic_softc *sc)
333 1.1 jmcneill {
334 1.1 jmcneill uint32_t psc, scll, sclh;
335 1.1 jmcneill int i;
336 1.1 jmcneill
337 1.1 jmcneill DPRINTF(("ti_iic_reset\n"));
338 1.1 jmcneill
339 1.1 jmcneill /* Disable */
340 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_CON, 0);
341 1.1 jmcneill /* Soft reset */
342 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_SYSC, I2C_SYSC_SRST);
343 1.1 jmcneill delay(1000);
344 1.1 jmcneill /* enable so that we can check for reset complete */
345 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_CON, I2C_CON_EN);
346 1.1 jmcneill delay(1000);
347 1.1 jmcneill for (i = 0; i < 1000; i++) { /* 1s delay for reset */
348 1.1 jmcneill if (I2C_READ_REG(sc, OMAP2_I2C_SYSS) & I2C_SYSS_RDONE)
349 1.1 jmcneill break;
350 1.1 jmcneill }
351 1.1 jmcneill /* Disable again */
352 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_CON, 0);
353 1.1 jmcneill delay(50000);
354 1.1 jmcneill
355 1.1 jmcneill if (i >= 1000) {
356 1.1 jmcneill aprint_error_dev(sc->sc_dev, ": couldn't reset module\n");
357 1.1 jmcneill return 1;
358 1.1 jmcneill }
359 1.1 jmcneill
360 1.1 jmcneill
361 1.1 jmcneill /* XXX standard speed only */
362 1.1 jmcneill psc = 3;
363 1.1 jmcneill scll = 53;
364 1.1 jmcneill sclh = 55;
365 1.1 jmcneill
366 1.1 jmcneill /* Clocks */
367 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_PSC, psc);
368 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_SCLL, scll);
369 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_SCLH, sclh);
370 1.1 jmcneill
371 1.1 jmcneill /* Own I2C address */
372 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_OA, OMAP2_I2C_SLAVE_ADDR);
373 1.1 jmcneill
374 1.1 jmcneill /* 5 bytes fifo */
375 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_BUF,
376 1.1 jmcneill I2C_BUF_RXTRSH(sc->sc_rxthres) | I2C_BUF_TXTRSH(sc->sc_txthres));
377 1.1 jmcneill
378 1.1 jmcneill /* Enable */
379 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_CON, I2C_CON_EN);
380 1.1 jmcneill
381 1.1 jmcneill return 0;
382 1.1 jmcneill }
383 1.1 jmcneill
384 1.1 jmcneill static int
385 1.1 jmcneill ti_iic_op(struct ti_iic_softc *sc, i2c_addr_t addr, ti_i2cop_t op,
386 1.1 jmcneill uint8_t *buf, size_t buflen, int flags)
387 1.1 jmcneill {
388 1.1 jmcneill uint16_t con, stat, mask;
389 1.1 jmcneill int err, retry;
390 1.1 jmcneill
391 1.1 jmcneill KASSERT(op == TI_I2CREAD || op == TI_I2CWRITE);
392 1.1 jmcneill DPRINTF(("ti_iic_op: addr %#x op %#x buf %p buflen %#x flags %#x\n",
393 1.1 jmcneill addr, op, buf, (unsigned int) buflen, flags));
394 1.1 jmcneill
395 1.1 jmcneill mask = I2C_IRQSTATUS_ARDY | I2C_IRQSTATUS_NACK | I2C_IRQSTATUS_AL;
396 1.1 jmcneill if (op == TI_I2CREAD) {
397 1.1 jmcneill mask |= I2C_IRQSTATUS_RDR | I2C_IRQSTATUS_RRDY;
398 1.1 jmcneill } else {
399 1.1 jmcneill mask |= I2C_IRQSTATUS_XDR | I2C_IRQSTATUS_XRDY;
400 1.1 jmcneill }
401 1.1 jmcneill
402 1.1 jmcneill err = ti_iic_wait(sc, I2C_IRQSTATUS_BB, 0, flags);
403 1.1 jmcneill if (err) {
404 1.1 jmcneill DPRINTF(("ti_iic_op: wait error %d\n", err));
405 1.1 jmcneill return err;
406 1.1 jmcneill }
407 1.1 jmcneill
408 1.1 jmcneill con = I2C_CON_EN;
409 1.1 jmcneill con |= I2C_CON_MST;
410 1.1 jmcneill con |= I2C_CON_STT;;
411 1.1 jmcneill if (flags & I2C_F_STOP)
412 1.1 jmcneill con |= I2C_CON_STP;
413 1.1 jmcneill if (addr & ~0x7f)
414 1.1 jmcneill con |= I2C_CON_XSA;
415 1.1 jmcneill if (op == TI_I2CWRITE)
416 1.1 jmcneill con |= I2C_CON_TRX;
417 1.1 jmcneill
418 1.1 jmcneill mutex_enter(&sc->sc_mtx);
419 1.1 jmcneill sc->sc_op = op;
420 1.1 jmcneill sc->sc_buf = buf;
421 1.1 jmcneill sc->sc_buflen = buflen;
422 1.1 jmcneill sc->sc_bufidx = 0;
423 1.1 jmcneill
424 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_CON, I2C_CON_EN | I2C_CON_MST | I2C_CON_STP);
425 1.1 jmcneill DPRINTF(("ti_iic_op: op %d con 0x%x ", op, con));
426 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_CNT, buflen);
427 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_SA, (addr & I2C_SA_MASK));
428 1.1 jmcneill DPRINTF(("SA 0x%x len %d\n", I2C_READ_REG(sc, OMAP2_I2C_SA), I2C_READ_REG(sc, OMAP2_I2C_CNT)));
429 1.1 jmcneill
430 1.1 jmcneill if ((flags & I2C_F_POLL) == 0) {
431 1.1 jmcneill /* clear any pending interrupt */
432 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_IRQSTATUS,
433 1.1 jmcneill I2C_READ_REG(sc, OMAP2_I2C_IRQSTATUS));
434 1.1 jmcneill /* and enable */
435 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_IRQENABLE_SET, mask);
436 1.1 jmcneill }
437 1.1 jmcneill /* start transfer */
438 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_CON, con);
439 1.1 jmcneill
440 1.1 jmcneill if ((flags & I2C_F_POLL) == 0) {
441 1.1 jmcneill /* and wait for completion */
442 1.1 jmcneill DPRINTF(("ti_iic_op waiting, op %#x\n", sc->sc_op));
443 1.1 jmcneill while (sc->sc_op == op) {
444 1.1 jmcneill if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx,
445 1.1 jmcneill mstohz(5000)) == EWOULDBLOCK) {
446 1.1 jmcneill /* timeout */
447 1.1 jmcneill op = TI_I2CERROR;
448 1.1 jmcneill }
449 1.1 jmcneill }
450 1.1 jmcneill DPRINTF(("ti_iic_op waiting done, op %#x\n", sc->sc_op));
451 1.1 jmcneill
452 1.1 jmcneill /* disable interrupts */
453 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_IRQENABLE_CLR, 0xffff);
454 1.1 jmcneill } else {
455 1.1 jmcneill /* poll for completion */
456 1.1 jmcneill DPRINTF(("ti_iic_op polling, op %x\n", sc->sc_op));
457 1.1 jmcneill while (sc->sc_op == op) {
458 1.1 jmcneill stat = ti_iic_stat(sc, mask);
459 1.1 jmcneill DPRINTF(("ti_iic_op stat 0x%x\n", stat));
460 1.1 jmcneill if (stat == 0) {
461 1.1 jmcneill /* timeout */
462 1.1 jmcneill sc->sc_op = TI_I2CERROR;
463 1.1 jmcneill } else {
464 1.1 jmcneill ti_iic_handle_intr(sc, stat);
465 1.1 jmcneill }
466 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_IRQSTATUS, stat);
467 1.1 jmcneill }
468 1.1 jmcneill DPRINTF(("ti_iic_op polling done, op now %x\n", sc->sc_op));
469 1.1 jmcneill }
470 1.1 jmcneill mutex_exit(&sc->sc_mtx);
471 1.1 jmcneill retry = 10000;
472 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_CON, 0);
473 1.1 jmcneill while (I2C_READ_REG(sc, OMAP2_I2C_CON) & I2C_CON_MST) {
474 1.1 jmcneill delay(100);
475 1.1 jmcneill if (--retry == 0)
476 1.1 jmcneill break;
477 1.1 jmcneill }
478 1.1 jmcneill return (sc->sc_op == TI_I2CDONE) ? 0 : EIO;
479 1.1 jmcneill }
480 1.1 jmcneill
481 1.1 jmcneill static void
482 1.1 jmcneill ti_iic_handle_intr(struct ti_iic_softc *sc, uint32_t stat)
483 1.1 jmcneill {
484 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_mtx));
485 1.1 jmcneill KASSERT(stat != 0);
486 1.1 jmcneill DPRINTF(("ti_iic_handle_intr stat %#x\n", stat));
487 1.1 jmcneill
488 1.1 jmcneill if (stat &
489 1.1 jmcneill (I2C_IRQSTATUS_NACK|I2C_IRQSTATUS_AL)) {
490 1.1 jmcneill sc->sc_op = TI_I2CERROR;
491 1.1 jmcneill return;
492 1.1 jmcneill }
493 1.1 jmcneill if (stat & I2C_IRQSTATUS_ARDY) {
494 1.1 jmcneill sc->sc_op = TI_I2CDONE;
495 1.1 jmcneill return;
496 1.1 jmcneill }
497 1.1 jmcneill if (sc->sc_op == TI_I2CREAD)
498 1.1 jmcneill ti_iic_do_read(sc, stat);
499 1.1 jmcneill else if (sc->sc_op == TI_I2CWRITE)
500 1.1 jmcneill ti_iic_do_write(sc, stat);
501 1.1 jmcneill else
502 1.1 jmcneill return;
503 1.1 jmcneill }
504 1.1 jmcneill void
505 1.1 jmcneill ti_iic_do_read(struct ti_iic_softc *sc, uint32_t stat)
506 1.1 jmcneill {
507 1.1 jmcneill int len = 0;
508 1.1 jmcneill
509 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_mtx));
510 1.1 jmcneill DPRINTF(("ti_iic_do_read stat %#x\n", stat));
511 1.1 jmcneill if (stat & I2C_IRQSTATUS_RDR) {
512 1.1 jmcneill len = I2C_READ_REG(sc, OMAP2_I2C_BUFSTAT);
513 1.1 jmcneill len = I2C_BUFSTAT_RXSTAT(len);
514 1.1 jmcneill DPRINTF(("ti_iic_do_read receive drain len %d left %d\n",
515 1.1 jmcneill len, I2C_READ_REG(sc, OMAP2_I2C_CNT)));
516 1.1 jmcneill } else if (stat & I2C_IRQSTATUS_RRDY) {
517 1.1 jmcneill len = sc->sc_rxthres + 1;
518 1.1 jmcneill DPRINTF(("ti_iic_do_read receive len %d left %d\n",
519 1.1 jmcneill len, I2C_READ_REG(sc, OMAP2_I2C_CNT)));
520 1.1 jmcneill }
521 1.1 jmcneill for (;
522 1.1 jmcneill sc->sc_bufidx < sc->sc_buflen && len > 0;
523 1.1 jmcneill sc->sc_bufidx++, len--) {
524 1.1 jmcneill sc->sc_buf[sc->sc_bufidx] = I2C_READ_DATA(sc);
525 1.1 jmcneill DPRINTF(("ti_iic_do_read got b[%d]=0x%x\n", sc->sc_bufidx,
526 1.1 jmcneill sc->sc_buf[sc->sc_bufidx]));
527 1.1 jmcneill }
528 1.1 jmcneill DPRINTF(("ti_iic_do_read done\n"));
529 1.1 jmcneill }
530 1.1 jmcneill
531 1.1 jmcneill void
532 1.1 jmcneill ti_iic_do_write(struct ti_iic_softc *sc, uint32_t stat)
533 1.1 jmcneill {
534 1.1 jmcneill int len = 0;
535 1.1 jmcneill
536 1.1 jmcneill DPRINTF(("ti_iic_do_write stat %#x\n", stat));
537 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_mtx));
538 1.1 jmcneill if (stat & I2C_IRQSTATUS_XDR) {
539 1.1 jmcneill len = I2C_READ_REG(sc, OMAP2_I2C_BUFSTAT);
540 1.1 jmcneill len = I2C_BUFSTAT_TXSTAT(len);
541 1.1 jmcneill DPRINTF(("ti_iic_do_write xmit drain len %d left %d\n",
542 1.1 jmcneill len, I2C_READ_REG(sc, OMAP2_I2C_CNT)));
543 1.1 jmcneill } else if (stat & I2C_IRQSTATUS_XRDY) {
544 1.1 jmcneill len = sc->sc_txthres + 1;
545 1.1 jmcneill DPRINTF(("ti_iic_do_write xmit len %d left %d\n",
546 1.1 jmcneill len, I2C_READ_REG(sc, OMAP2_I2C_CNT)));
547 1.1 jmcneill }
548 1.1 jmcneill for (;
549 1.1 jmcneill sc->sc_bufidx < sc->sc_buflen && len > 0;
550 1.1 jmcneill sc->sc_bufidx++, len--) {
551 1.1 jmcneill DPRINTF(("ti_iic_do_write send b[%d]=0x%x\n",
552 1.1 jmcneill sc->sc_bufidx, sc->sc_buf[sc->sc_bufidx]));
553 1.1 jmcneill I2C_WRITE_DATA(sc, sc->sc_buf[sc->sc_bufidx]);
554 1.1 jmcneill }
555 1.1 jmcneill DPRINTF(("ti_iic_do_write done\n"));
556 1.1 jmcneill }
557 1.1 jmcneill
558 1.1 jmcneill static int
559 1.1 jmcneill ti_iic_wait(struct ti_iic_softc *sc, uint16_t mask, uint16_t val, int flags)
560 1.1 jmcneill {
561 1.1 jmcneill int retry = 10;
562 1.1 jmcneill uint16_t v;
563 1.1 jmcneill DPRINTF(("ti_iic_wait mask %#x val %#x flags %#x\n", mask, val, flags));
564 1.1 jmcneill
565 1.1 jmcneill while (((v = I2C_READ_REG(sc, OMAP2_I2C_IRQSTATUS_RAW)) & mask) != val) {
566 1.1 jmcneill --retry;
567 1.1 jmcneill if (retry == 0) {
568 1.1 jmcneill aprint_error_dev(sc->sc_dev, ": wait timeout, "
569 1.1 jmcneill "mask = %#x val = %#x stat = %#x\n",
570 1.1 jmcneill mask, val, v);
571 1.1 jmcneill return EBUSY;
572 1.1 jmcneill }
573 1.1 jmcneill if (flags & I2C_F_POLL) {
574 1.1 jmcneill delay(50000);
575 1.1 jmcneill } else {
576 1.1 jmcneill kpause("tiiic", false, mstohz(50), NULL);
577 1.1 jmcneill }
578 1.1 jmcneill }
579 1.1 jmcneill DPRINTF(("ti_iic_wait done retry %#x\n", retry));
580 1.1 jmcneill
581 1.1 jmcneill return 0;
582 1.1 jmcneill }
583 1.1 jmcneill
584 1.1 jmcneill static uint32_t
585 1.1 jmcneill ti_iic_stat(struct ti_iic_softc *sc, uint32_t mask)
586 1.1 jmcneill {
587 1.1 jmcneill uint32_t v;
588 1.1 jmcneill int retry = 500;
589 1.1 jmcneill DPRINTF(("ti_iic_wait mask %#x\n", mask));
590 1.1 jmcneill while (--retry > 0) {
591 1.1 jmcneill v = I2C_READ_REG(sc, OMAP2_I2C_IRQSTATUS_RAW) & mask;
592 1.1 jmcneill if (v != 0)
593 1.1 jmcneill break;
594 1.1 jmcneill delay(100);
595 1.1 jmcneill }
596 1.1 jmcneill DPRINTF(("ti_iic_wait done retry %#x\n", retry));
597 1.1 jmcneill return v;
598 1.1 jmcneill }
599 1.1 jmcneill
600 1.1 jmcneill static int
601 1.1 jmcneill ti_iic_flush(struct ti_iic_softc *sc)
602 1.1 jmcneill {
603 1.1 jmcneill DPRINTF(("ti_iic_flush\n"));
604 1.1 jmcneill #if 0
605 1.1 jmcneill int retry = 1000;
606 1.1 jmcneill uint16_t v;
607 1.1 jmcneill
608 1.1 jmcneill while ((v = I2C_READ_REG(sc, OMAP2_I2C_IRQSTATUS_RAW)) & I2C_IRQSTATUS_RRDY) {
609 1.1 jmcneill if (--retry == 0) {
610 1.1 jmcneill aprint_error_dev(sc->sc_dev,
611 1.1 jmcneill ": flush timeout, stat = %#x\n", v);
612 1.1 jmcneill return EBUSY;
613 1.1 jmcneill }
614 1.1 jmcneill (void)I2C_READ_DATA(sc);
615 1.1 jmcneill delay(1000);
616 1.1 jmcneill }
617 1.1 jmcneill #endif
618 1.1 jmcneill
619 1.1 jmcneill I2C_WRITE_REG(sc, OMAP2_I2C_CNT, 0);
620 1.1 jmcneill return 0;
621 1.1 jmcneill }
622 1.1 jmcneill
623 1.1 jmcneill static i2c_tag_t
624 1.1 jmcneill ti_iic_get_tag(device_t dev)
625 1.1 jmcneill {
626 1.1 jmcneill struct ti_iic_softc * const sc = device_private(dev);
627 1.1 jmcneill
628 1.1 jmcneill return &sc->sc_ic;
629 1.1 jmcneill }
630