xref: /openbsd/sys/arch/armv7/omap/ti_iic.c (revision 3cab2bb3)
1 /*	$OpenBSD: ti_iic.c,v 1.13 2020/04/10 22:02:45 kettenis 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 
70 #include <dev/ofw/openfirm.h>
71 #include <dev/ofw/ofw_pinctrl.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 = -1;
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 	pinctrl_byname(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 	if (unit != -1)
199 		prcm_enablemodule(PRCM_I2C0 + unit);
200 
201 	rev = I2C_READ_REG(sc, AM335X_I2C_REVNB_LO);
202 	printf(" rev %d.%d\n",
203 	    (int)I2C_REVNB_LO_MAJOR(rev),
204 	    (int)I2C_REVNB_LO_MINOR(rev));
205 
206 	ti_iic_reset(sc);
207 	ti_iic_flush(sc);
208 
209 	sc->sc_ic.ic_cookie = sc;
210 	sc->sc_ic.ic_acquire_bus = ti_iic_acquire_bus;
211 	sc->sc_ic.ic_release_bus = ti_iic_release_bus;
212 	sc->sc_ic.ic_exec = ti_iic_exec;
213 
214 	bzero(&iba, sizeof iba);
215 	iba.iba_name = "iic";
216 	iba.iba_tag = &sc->sc_ic;
217 	iba.iba_bus_scan = ti_iic_scan;
218 	iba.iba_bus_scan_arg = &sc->sc_node;
219 	(void) config_found(&sc->sc_dev, &iba, iicbus_print);
220 }
221 
222 int
223 ti_iic_intr(void *arg)
224 {
225 	struct ti_iic_softc *sc = arg;
226 	uint32_t stat;
227 
228 	DPRINTF(("ti_iic_intr\n"));
229 	stat = I2C_READ_REG(sc, AM335X_I2C_IRQSTATUS);
230 	I2C_WRITE_REG(sc, AM335X_I2C_IRQSTATUS, stat);
231 	DPRINTF(("ti_iic_intr pre handle sc->sc_op eq %#x\n", sc->sc_op));
232 
233 	ti_iic_handle_intr(sc, stat);
234 
235 	if (sc->sc_op == TI_I2CERROR || sc->sc_op == TI_I2CDONE) {
236 		DPRINTF(("ti_iic_intr post handle sc->sc_op %#x\n", sc->sc_op));
237 		wakeup(&sc->sc_dev);
238 	}
239 
240 	DPRINTF(("ti_iic_intr status 0x%x\n", stat));
241 
242 	return 1;
243 }
244 
245 int
246 ti_iic_acquire_bus(void *opaque, int flags)
247 {
248 	struct ti_iic_softc *sc = opaque;
249 
250 	if (flags & I2C_F_POLL)
251 		return 0;
252 
253 	return (rw_enter(&sc->sc_buslock, RW_WRITE));
254 }
255 
256 void
257 ti_iic_release_bus(void *opaque, int flags)
258 {
259 	struct ti_iic_softc *sc = opaque;
260 
261 	if (flags & I2C_F_POLL)
262 		return;
263 
264 	rw_exit(&sc->sc_buslock);
265 }
266 
267 int
268 ti_iic_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
269     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
270 {
271 	struct ti_iic_softc *sc = opaque;
272 	int err = 0;
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_nsec(&sc->sc_dev, PWAIT, "tiiic",
421 			    SEC_TO_NSEC(5)) == 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_nsec(&sc->sc_dev, PWAIT, "tiiic",
547 			    MSEC_TO_NSEC(50));
548 	}
549 	DPRINTF(("ti_iic_wait done retry %#x\n", retry));
550 
551 	return 0;
552 }
553 
554 uint32_t
555 ti_iic_stat(struct ti_iic_softc *sc, uint32_t mask)
556 {
557 	uint32_t v;
558 	int retry = 500;
559 	DPRINTF(("ti_iic_wait mask %#x\n", mask));
560 	while (--retry > 0) {
561 		v = I2C_READ_REG(sc, AM335X_I2C_IRQSTATUS_RAW) & mask;
562 		if (v != 0)
563 			break;
564 		delay(100);
565 	}
566 	DPRINTF(("ti_iic_wait done retry %#x\n", retry));
567 	return v;
568 }
569 
570 int
571 ti_iic_flush(struct ti_iic_softc *sc)
572 {
573 	DPRINTF(("ti_iic_flush\n"));
574 #if 0
575 	int retry = 1000;
576 	uint16_t v;
577 
578 	while ((v =
579 	    I2C_READ_REG(sc, AM335X_I2C_IRQSTATUS_RAW)) & I2C_IRQSTATUS_RRDY) {
580 		if (--retry == 0) {
581 			printf("%s: flush timeout, stat = %#x\n", DEVNAME(sc), v);
582 			return EBUSY;
583 		}
584 		(void)I2C_READ_DATA(sc);
585 		delay(1000);
586 	}
587 #endif
588 
589 	I2C_WRITE_REG(sc, AM335X_I2C_CNT, 0);
590 	return 0;
591 }
592 
593 void
594 ti_iic_scan(struct device *self, struct i2cbus_attach_args *iba, void *aux)
595 {
596 	int iba_node = *(int *)aux;
597 	extern int iic_print(void *, const char *);
598 	struct i2c_attach_args ia;
599 	char name[32];
600 	uint32_t reg[1];
601 	int node;
602 
603 	for (node = OF_child(iba_node); node; node = OF_peer(node)) {
604 		memset(name, 0, sizeof(name));
605 		memset(reg, 0, sizeof(reg));
606 
607 		if (OF_getprop(node, "compatible", name, sizeof(name)) == -1)
608 			continue;
609 		if (name[0] == '\0')
610 			continue;
611 
612 		if (OF_getprop(node, "reg", &reg, sizeof(reg)) != sizeof(reg))
613 			continue;
614 
615 		memset(&ia, 0, sizeof(ia));
616 		ia.ia_tag = iba->iba_tag;
617 		ia.ia_addr = bemtoh32(&reg[0]);
618 		ia.ia_name = name;
619 		ia.ia_cookie = &node;
620 
621 		config_found(self, &ia, iic_print);
622 	}
623 }
624