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