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