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