Home | History | Annotate | Line # | Download | only in dev
ki2c.c revision 1.43
      1  1.43   thorpej /*	$NetBSD: ki2c.c,v 1.43 2025/09/21 18:03:28 thorpej Exp $	*/
      2   1.1     grant /*	Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp	*/
      3   1.1     grant 
      4   1.1     grant /*-
      5   1.1     grant  * Copyright (c) 2001 Tsubai Masanari.  All rights reserved.
      6   1.1     grant  *
      7   1.1     grant  * Redistribution and use in source and binary forms, with or without
      8   1.1     grant  * modification, are permitted provided that the following conditions
      9   1.1     grant  * are met:
     10   1.1     grant  * 1. Redistributions of source code must retain the above copyright
     11   1.1     grant  *    notice, this list of conditions and the following disclaimer.
     12   1.1     grant  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     grant  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     grant  *    documentation and/or other materials provided with the distribution.
     15   1.1     grant  * 3. The name of the author may not be used to endorse or promote products
     16   1.1     grant  *    derived from this software without specific prior written permission.
     17   1.1     grant  *
     18   1.1     grant  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1     grant  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1     grant  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1     grant  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1     grant  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1     grant  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1     grant  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1     grant  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1     grant  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27   1.1     grant  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1     grant  */
     29   1.1     grant 
     30   1.1     grant #include <sys/param.h>
     31   1.1     grant #include <sys/device.h>
     32   1.1     grant #include <sys/systm.h>
     33  1.11        ad #include <sys/mutex.h>
     34   1.1     grant 
     35   1.1     grant #include <dev/ofw/openfirm.h>
     36   1.1     grant #include <machine/autoconf.h>
     37  1.37  macallan #include <powerpc/pic/picvar.h>
     38   1.1     grant 
     39  1.29  macallan #include "opt_ki2c.h"
     40   1.3  macallan #include <macppc/dev/ki2cvar.h>
     41   1.1     grant 
     42  1.20  macallan #ifdef KI2C_DEBUG
     43  1.20  macallan #define DPRINTF printf
     44  1.20  macallan #else
     45  1.20  macallan #define DPRINTF while (0) printf
     46  1.20  macallan #endif
     47  1.20  macallan 
     48  1.33   mlelstv #define KI2C_EXEC_MAX_CMDLEN	32
     49  1.33   mlelstv #define KI2C_EXEC_MAX_BUFLEN	32
     50  1.33   mlelstv 
     51  1.17      matt int ki2c_match(device_t, cfdata_t, void *);
     52  1.17      matt void ki2c_attach(device_t, device_t, void *);
     53  1.21  macallan inline uint8_t ki2c_readreg(struct ki2c_softc *, int);
     54  1.21  macallan inline void ki2c_writereg(struct ki2c_softc *, int, uint8_t);
     55   1.1     grant u_int ki2c_getmode(struct ki2c_softc *);
     56   1.1     grant void ki2c_setmode(struct ki2c_softc *, u_int);
     57   1.1     grant u_int ki2c_getspeed(struct ki2c_softc *);
     58   1.1     grant void ki2c_setspeed(struct ki2c_softc *, u_int);
     59  1.35  macallan int ki2c_intr(void *);
     60   1.1     grant int ki2c_poll(struct ki2c_softc *, int);
     61   1.1     grant int ki2c_start(struct ki2c_softc *, int, int, void *, int);
     62   1.1     grant int ki2c_read(struct ki2c_softc *, int, int, void *, int);
     63   1.3  macallan int ki2c_write(struct ki2c_softc *, int, int, void *, int);
     64   1.3  macallan 
     65   1.3  macallan /* I2C glue */
     66   1.3  macallan static int ki2c_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     67   1.3  macallan 		    void *, size_t, int);
     68   1.3  macallan 
     69  1.43   thorpej static void
     70  1.43   thorpej ki2c_select_channel(struct ki2c_softc *sc, int channel)
     71  1.43   thorpej {
     72  1.43   thorpej 	uint8_t val = channel ? 0x10 : 0x00;
     73  1.43   thorpej 
     74  1.43   thorpej 	/* We handle the subaddress stuff ourselves. */
     75  1.43   thorpej 	ki2c_setmode(sc, val | I2C_STDMODE);
     76  1.43   thorpej }
     77  1.43   thorpej 
     78  1.43   thorpej static int
     79  1.43   thorpej ki2c_acquire_bus(void *v, int flags)
     80  1.43   thorpej {
     81  1.43   thorpej 	struct ki2c_channel *ch = v;
     82  1.43   thorpej 	struct ki2c_softc *sc = ch->ch_sc;
     83  1.43   thorpej 
     84  1.43   thorpej 	int error = iic_acquire_bus_lock(&sc->sc_mux_lock, flags);
     85  1.43   thorpej 	if (error) {
     86  1.43   thorpej 		return error;
     87  1.43   thorpej 	}
     88  1.43   thorpej 
     89  1.43   thorpej 	ki2c_select_channel(sc, ch->ch_channel);
     90  1.43   thorpej 	return 0;
     91  1.43   thorpej }
     92  1.43   thorpej 
     93  1.43   thorpej static void
     94  1.43   thorpej ki2c_release_bus(void *v, int flags)
     95  1.43   thorpej {
     96  1.43   thorpej 	struct ki2c_channel *ch = v;
     97  1.43   thorpej 	struct ki2c_softc *sc = ch->ch_sc;
     98  1.43   thorpej 
     99  1.43   thorpej 	iic_release_bus_lock(&sc->sc_mux_lock);
    100  1.43   thorpej }
    101  1.43   thorpej 
    102  1.43   thorpej static void
    103  1.43   thorpej ki2c_init_channel(struct ki2c_softc *sc, int channel, int node)
    104  1.43   thorpej {
    105  1.43   thorpej 	struct ki2c_channel *ch = &sc->sc_channels[channel];
    106  1.43   thorpej 
    107  1.43   thorpej 	iic_tag_init(&ch->ch_i2c);
    108  1.43   thorpej 	ch->ch_i2c.ic_channel = channel;
    109  1.43   thorpej 	ch->ch_i2c.ic_cookie = ch;
    110  1.43   thorpej 	ch->ch_i2c.ic_exec = ki2c_i2c_exec;
    111  1.43   thorpej 	ch->ch_i2c.ic_acquire_bus = ki2c_acquire_bus;
    112  1.43   thorpej 	ch->ch_i2c.ic_release_bus = ki2c_release_bus;
    113  1.43   thorpej 
    114  1.43   thorpej 	ch->ch_sc = sc;
    115  1.43   thorpej 	ch->ch_node = node;
    116  1.43   thorpej 	ch->ch_channel = channel;
    117  1.43   thorpej }
    118   1.1     grant 
    119  1.18  macallan CFATTACH_DECL_NEW(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
    120   1.9    dogcow 	NULL, NULL);
    121   1.1     grant 
    122   1.1     grant int
    123  1.17      matt ki2c_match(device_t parent, cfdata_t match, void *aux)
    124   1.1     grant {
    125   1.1     grant 	struct confargs *ca = aux;
    126   1.1     grant 
    127   1.1     grant 	if (strcmp(ca->ca_name, "i2c") == 0)
    128   1.1     grant 		return 1;
    129   1.1     grant 
    130   1.1     grant 	return 0;
    131   1.1     grant }
    132   1.1     grant 
    133   1.1     grant void
    134  1.17      matt ki2c_attach(device_t parent, device_t self, void *aux)
    135   1.1     grant {
    136  1.17      matt 	struct ki2c_softc *sc = device_private(self);
    137   1.1     grant 	struct confargs *ca = aux;
    138  1.39  macallan 	int node = ca->ca_node, root;
    139  1.42   thorpej 	uint32_t addr, channel, intr[2];
    140  1.43   thorpej 	int rate, child;
    141  1.43   thorpej 	int intrparent;
    142  1.39  macallan 	char name[32], intr_xname[32], model[32];
    143  1.38  macallan 	uint32_t picbase;
    144  1.18  macallan 
    145  1.18  macallan 	sc->sc_dev = self;
    146  1.21  macallan 	sc->sc_tag = ca->ca_tag;
    147   1.1     grant 	ca->ca_reg[0] += ca->ca_baseaddr;
    148   1.1     grant 
    149  1.39  macallan 	root = OF_finddevice("/");
    150  1.39  macallan 	model[0] = 0;
    151  1.39  macallan 	OF_getprop(root, "model", model, 32);
    152  1.39  macallan 	DPRINTF("model %s\n", model);
    153   1.1     grant 	if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
    154  1.20  macallan 		aprint_error(": cannot get i2c-rate\n");
    155   1.1     grant 		return;
    156   1.1     grant 	}
    157  1.20  macallan 	if (OF_getprop(node, "AAPL,address", &addr, 4) != 4) {
    158  1.20  macallan 		aprint_error(": unable to find i2c address\n");
    159   1.1     grant 		return;
    160   1.1     grant 	}
    161  1.21  macallan 	if (bus_space_map(sc->sc_tag, addr, PAGE_SIZE, 0, &sc->sc_bh) != 0) {
    162  1.21  macallan 		aprint_error_dev(sc->sc_dev, "failed to map registers\n");
    163  1.21  macallan 		return;
    164  1.21  macallan 	}
    165  1.21  macallan 
    166   1.1     grant 	if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
    167  1.20  macallan 		aprint_error(": unable to find i2c address step\n");
    168   1.1     grant 		return;
    169   1.1     grant 	}
    170   1.1     grant 
    171  1.35  macallan 	if(OF_getprop(node, "interrupts", intr, 8) != 8) {
    172  1.35  macallan 		aprint_error(": can't find interrupt\n");
    173  1.35  macallan 		return;
    174  1.35  macallan 	}
    175  1.35  macallan 
    176  1.37  macallan 	/*
    177  1.37  macallan 	 * on some G5 we have two openpics, one in mac-io, one in /u3
    178  1.37  macallan 	 * in order to get interrupts we need to know which one we're
    179  1.37  macallan 	 * connected to
    180  1.37  macallan 	 */
    181  1.37  macallan 	sc->sc_poll = 0;
    182  1.37  macallan 
    183  1.37  macallan 	if(OF_getprop(node, "interrupt-parent", &intrparent, 4) == 4) {
    184  1.38  macallan 		uint32_t preg[8];
    185  1.37  macallan 		struct pic_ops *pic;
    186  1.37  macallan 
    187  1.37  macallan 		sc->sc_poll = 1;
    188  1.37  macallan 		if(OF_getprop(intrparent, "reg", preg, 8) > 4) {
    189  1.37  macallan 			/* now look for a pic with that base... */
    190  1.38  macallan 			picbase = preg[0];
    191  1.38  macallan 			if ((picbase & 0x80000000) == 0) {
    192  1.38  macallan 				/* some OF versions have the openpic's reg as
    193  1.38  macallan 				 * an offset into mac-io just to be annoying */
    194  1.38  macallan 				int mio = OF_parent(intrparent);
    195  1.38  macallan 				if (OF_getprop(mio, "ranges", preg, 20) == 20)
    196  1.38  macallan 					picbase += preg[3];
    197  1.38  macallan 			}
    198  1.38  macallan 			DPRINTF("PIC base %08x\n", picbase);
    199  1.38  macallan 			pic = find_pic_by_cookie((void *)picbase);
    200  1.37  macallan 			if (pic != NULL) {
    201  1.37  macallan 				sc->sc_poll = 0;
    202  1.37  macallan 				intr[0] += pic->pic_intrbase;
    203  1.37  macallan 			}
    204  1.37  macallan 		}
    205  1.37  macallan 	}
    206  1.37  macallan 
    207  1.37  macallan 	if (sc->sc_poll) {
    208  1.37  macallan 		aprint_normal(" polling");
    209  1.37  macallan 	} else aprint_normal(" irq %d", intr[0]);
    210  1.35  macallan 
    211   1.1     grant 	printf("\n");
    212   1.1     grant 
    213   1.1     grant 	ki2c_writereg(sc, STATUS, 0);
    214   1.1     grant 	ki2c_writereg(sc, ISR, 0);
    215   1.1     grant 	ki2c_writereg(sc, IER, 0);
    216   1.1     grant 
    217   1.1     grant 	ki2c_setmode(sc, I2C_STDSUBMODE);
    218   1.1     grant 	ki2c_setspeed(sc, I2C_100kHz);		/* XXX rate */
    219  1.43   thorpej 
    220   1.3  macallan 	ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
    221  1.43   thorpej 
    222  1.43   thorpej 	/*
    223  1.43   thorpej 	 * Newer OpenFirmware will have "i2c-bus" nodes below the controller
    224  1.43   thorpej 	 * node.  If we don't find any, then the devices are direct children
    225  1.43   thorpej 	 * of the controller node, and the channel number is encoded in
    226  1.43   thorpej 	 * bit 8 of the i2c address cell (see ofw_i2c_machdep.c).
    227   1.4  macallan 	 */
    228  1.43   thorpej 	bool found_busnode = false;
    229  1.43   thorpej 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
    230   1.4  macallan 		OF_getprop(child, "name", name, sizeof(name));
    231  1.25  macallan 		if (strcmp(name, "i2c-bus") == 0) {
    232  1.25  macallan 			OF_getprop(child, "reg", &channel, sizeof(channel));
    233  1.25  macallan 			DPRINTF("found channel %x\n", channel);
    234  1.43   thorpej 			if (channel >= KI2C_MAX_CHANNELS) {
    235  1.43   thorpej 				aprint_error_dev(self,
    236  1.43   thorpej 				    "ignoring invalid I2C channel %u\n",
    237  1.43   thorpej 				    channel);
    238  1.43   thorpej 				continue;
    239  1.43   thorpej 			}
    240  1.43   thorpej 			ki2c_init_channel(sc, channel, child);
    241  1.43   thorpej 			found_busnode = true;
    242  1.25  macallan 		}
    243   1.4  macallan 	}
    244  1.43   thorpej 	if (!found_busnode) {
    245  1.43   thorpej 		for (channel = 0; channel < KI2C_MAX_CHANNELS; channel++) {
    246  1.43   thorpej 			ki2c_init_channel(sc, channel, node);
    247  1.26  macallan 		}
    248   1.3  macallan 	}
    249   1.3  macallan 
    250  1.43   thorpej 	/*
    251  1.43   thorpej 	 * The Keywest I2C controller has a built-in I2C mux, but it's not
    252  1.43   thorpej 	 * really a distinct device in the device tree, so we handle it here.
    253  1.43   thorpej 	 *
    254  1.43   thorpej 	 * The locking order is:
    255  1.43   thorpej 	 *
    256  1.43   thorpej 	 *	iic bus mutex -> mux_lock
    257  1.43   thorpej 	 *
    258  1.43   thorpej 	 * mux_lock is taken in ki2c_acquire_bus().
    259  1.43   thorpej 	 */
    260  1.43   thorpej 	mutex_init(&sc->sc_mux_lock, MUTEX_DEFAULT, IPL_NONE);
    261  1.43   thorpej 
    262  1.35  macallan 	cv_init(&sc->sc_todev, device_xname(self));
    263  1.35  macallan 	mutex_init(&sc->sc_todevmtx, MUTEX_DEFAULT, IPL_NONE);
    264  1.35  macallan 
    265  1.37  macallan 	if(sc->sc_poll == 0) {
    266  1.37  macallan 		snprintf(intr_xname, sizeof(intr_xname), "%s intr", device_xname(self));
    267  1.37  macallan 		intr_establish_xname(intr[0], (intr[1] & 1) ? IST_LEVEL : IST_EDGE,
    268  1.37  macallan 		    IPL_BIO, ki2c_intr, sc, intr_xname);
    269  1.35  macallan 
    270  1.37  macallan 		ki2c_writereg(sc, IER, I2C_INT_DATA | I2C_INT_ADDR| I2C_INT_STOP);
    271  1.37  macallan 	}
    272  1.35  macallan 
    273  1.43   thorpej 	for (channel = 0; channel < KI2C_MAX_CHANNELS; channel++) {
    274  1.43   thorpej 		if (sc->sc_channels[channel].ch_node == 0) {
    275  1.43   thorpej 			continue;
    276  1.43   thorpej 		}
    277  1.43   thorpej 		iicbus_attach_with_devhandle(sc->sc_dev,
    278  1.43   thorpej 		    &sc->sc_channels[channel].ch_i2c,
    279  1.43   thorpej 		    devhandle_from_of(device_handle(sc->sc_dev),
    280  1.43   thorpej 				      sc->sc_channels[channel].ch_node));
    281  1.43   thorpej 	}
    282   1.1     grant }
    283   1.1     grant 
    284  1.21  macallan uint8_t
    285  1.14       dsl ki2c_readreg(struct ki2c_softc *sc, int reg)
    286   1.1     grant {
    287   1.1     grant 
    288  1.21  macallan 	return bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_regstep * reg);
    289   1.1     grant }
    290   1.1     grant 
    291   1.1     grant void
    292  1.21  macallan ki2c_writereg(struct ki2c_softc *sc, int reg, uint8_t val)
    293   1.1     grant {
    294  1.21  macallan 
    295  1.21  macallan 	bus_space_write_1(sc->sc_tag, sc->sc_bh, reg * sc->sc_regstep, val);
    296   1.1     grant 	delay(10);
    297   1.1     grant }
    298   1.1     grant 
    299   1.1     grant u_int
    300  1.14       dsl ki2c_getmode(struct ki2c_softc *sc)
    301   1.1     grant {
    302   1.1     grant 	return ki2c_readreg(sc, MODE) & I2C_MODE;
    303   1.1     grant }
    304   1.1     grant 
    305   1.1     grant void
    306  1.14       dsl ki2c_setmode(struct ki2c_softc *sc, u_int mode)
    307   1.1     grant {
    308  1.25  macallan 	ki2c_writereg(sc, MODE, mode);
    309   1.1     grant }
    310   1.1     grant 
    311   1.1     grant u_int
    312  1.14       dsl ki2c_getspeed(struct ki2c_softc *sc)
    313   1.1     grant {
    314   1.1     grant 	return ki2c_readreg(sc, MODE) & I2C_SPEED;
    315   1.1     grant }
    316   1.1     grant 
    317   1.1     grant void
    318  1.14       dsl ki2c_setspeed(struct ki2c_softc *sc, u_int speed)
    319   1.1     grant {
    320   1.1     grant 	u_int x;
    321   1.1     grant 
    322   1.1     grant 	KASSERT((speed & ~I2C_SPEED) == 0);
    323   1.1     grant 	x = ki2c_readreg(sc, MODE);
    324   1.1     grant 	x &= ~I2C_SPEED;
    325   1.1     grant 	x |= speed;
    326   1.1     grant 	ki2c_writereg(sc, MODE, x);
    327   1.1     grant }
    328   1.1     grant 
    329   1.1     grant int
    330  1.35  macallan ki2c_intr(void *cookie)
    331   1.1     grant {
    332  1.35  macallan 	struct ki2c_softc *sc = cookie;
    333   1.1     grant 	u_int isr, x;
    334   1.1     grant 	isr = ki2c_readreg(sc, ISR);
    335   1.1     grant 	if (isr & I2C_INT_ADDR) {
    336   1.1     grant #if 0
    337   1.1     grant 		if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    338   1.1     grant 			/* No slave responded. */
    339   1.1     grant 			sc->sc_flags |= I2C_ERROR;
    340   1.1     grant 			goto out;
    341   1.1     grant 		}
    342   1.1     grant #endif
    343   1.1     grant 
    344   1.1     grant 		if (sc->sc_flags & I2C_READING) {
    345   1.1     grant 			if (sc->sc_resid > 1) {
    346   1.1     grant 				x = ki2c_readreg(sc, CONTROL);
    347   1.1     grant 				x |= I2C_CT_AAK;
    348   1.1     grant 				ki2c_writereg(sc, CONTROL, x);
    349   1.1     grant 			}
    350   1.1     grant 		} else {
    351   1.1     grant 			ki2c_writereg(sc, DATA, *sc->sc_data++);
    352   1.1     grant 			sc->sc_resid--;
    353   1.1     grant 		}
    354   1.1     grant 	}
    355   1.1     grant 
    356   1.1     grant 	if (isr & I2C_INT_DATA) {
    357   1.1     grant 		if (sc->sc_flags & I2C_READING) {
    358   1.1     grant 			*sc->sc_data++ = ki2c_readreg(sc, DATA);
    359   1.1     grant 			sc->sc_resid--;
    360   1.1     grant 
    361   1.1     grant 			if (sc->sc_resid == 0) {	/* Completed */
    362   1.1     grant 				ki2c_writereg(sc, CONTROL, 0);
    363   1.1     grant 				goto out;
    364   1.1     grant 			}
    365   1.1     grant 		} else {
    366   1.1     grant #if 0
    367   1.1     grant 			if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    368   1.1     grant 				/* No slave responded. */
    369   1.1     grant 				sc->sc_flags |= I2C_ERROR;
    370   1.1     grant 				goto out;
    371   1.1     grant 			}
    372   1.1     grant #endif
    373   1.1     grant 
    374   1.1     grant 			if (sc->sc_resid == 0) {
    375   1.1     grant 				x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
    376   1.1     grant 				ki2c_writereg(sc, CONTROL, x);
    377   1.1     grant 			} else {
    378   1.1     grant 				ki2c_writereg(sc, DATA, *sc->sc_data++);
    379   1.1     grant 				sc->sc_resid--;
    380   1.1     grant 			}
    381   1.1     grant 		}
    382   1.1     grant 	}
    383   1.1     grant 
    384   1.1     grant out:
    385   1.1     grant 	if (isr & I2C_INT_STOP) {
    386   1.1     grant 		ki2c_writereg(sc, CONTROL, 0);
    387   1.1     grant 		sc->sc_flags &= ~I2C_BUSY;
    388  1.35  macallan 		cv_signal(&sc->sc_todev);
    389   1.1     grant 	}
    390   1.1     grant 
    391   1.1     grant 	ki2c_writereg(sc, ISR, isr);
    392   1.1     grant 
    393   1.1     grant 	return 1;
    394   1.1     grant }
    395   1.1     grant 
    396   1.1     grant int
    397  1.14       dsl ki2c_poll(struct ki2c_softc *sc, int timo)
    398   1.1     grant {
    399  1.37  macallan 	int bail = 0;
    400   1.1     grant 	while (sc->sc_flags & I2C_BUSY) {
    401  1.37  macallan 		if ((cold) || (bail > 10) || (sc->sc_poll)) {
    402  1.35  macallan 			if (ki2c_readreg(sc, ISR))
    403  1.35  macallan 				ki2c_intr(sc);
    404  1.35  macallan 			timo -= 10;
    405  1.35  macallan 			if (timo < 0) {
    406  1.35  macallan 				DPRINTF("i2c_poll: timeout\n");
    407  1.35  macallan 				return -1;
    408  1.35  macallan 			}
    409  1.35  macallan 			delay(10);
    410  1.35  macallan 		} else {
    411  1.35  macallan 			mutex_enter(&sc->sc_todevmtx);
    412  1.37  macallan 			cv_timedwait_sig(&sc->sc_todev, &sc->sc_todevmtx, hz/10);
    413  1.35  macallan 			mutex_exit(&sc->sc_todevmtx);
    414  1.37  macallan 			bail++;
    415   1.1     grant 		}
    416   1.1     grant 	}
    417   1.1     grant 	return 0;
    418   1.1     grant }
    419   1.1     grant 
    420   1.1     grant int
    421  1.15       dsl ki2c_start(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    422   1.1     grant {
    423   1.1     grant 	int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
    424   1.1     grant 	int timo, x;
    425   1.1     grant 
    426   1.1     grant 	KASSERT((addr & 1) == 0);
    427   1.1     grant 
    428   1.1     grant 	sc->sc_data = data;
    429   1.1     grant 	sc->sc_resid = len;
    430   1.1     grant 	sc->sc_flags |= I2C_BUSY;
    431   1.1     grant 
    432   1.1     grant 	timo = 1000 + len * 200;
    433   1.1     grant 
    434   1.1     grant 	/* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
    435   1.1     grant 	/* if (addr == 0x68) */
    436   1.1     grant 		timo += 100000;
    437   1.1     grant 
    438   1.1     grant 	ki2c_writereg(sc, ADDR, addr | rw);
    439   1.1     grant 	ki2c_writereg(sc, SUBADDR, subaddr);
    440   1.1     grant 
    441   1.1     grant 	x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
    442   1.1     grant 	ki2c_writereg(sc, CONTROL, x);
    443   1.1     grant 
    444   1.1     grant 	if (ki2c_poll(sc, timo))
    445   1.1     grant 		return -1;
    446   1.1     grant 	if (sc->sc_flags & I2C_ERROR) {
    447  1.20  macallan 		DPRINTF("I2C_ERROR\n");
    448   1.1     grant 		return -1;
    449   1.1     grant 	}
    450   1.1     grant 	return 0;
    451   1.1     grant }
    452   1.1     grant 
    453   1.1     grant int
    454  1.15       dsl ki2c_read(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    455   1.1     grant {
    456   1.1     grant 	sc->sc_flags = I2C_READING;
    457  1.20  macallan 	DPRINTF("ki2c_read: %02x %d\n", addr, len);
    458   1.1     grant 	return ki2c_start(sc, addr, subaddr, data, len);
    459   1.1     grant }
    460   1.1     grant 
    461   1.1     grant int
    462  1.15       dsl ki2c_write(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    463   1.1     grant {
    464   1.1     grant 	sc->sc_flags = 0;
    465  1.20  macallan 	DPRINTF("ki2c_write: %02x %d\n",addr,len);
    466   1.3  macallan 	return ki2c_start(sc, addr, subaddr, data, len);
    467   1.3  macallan }
    468   1.3  macallan 
    469   1.3  macallan int
    470   1.3  macallan ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    471   1.3  macallan     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    472   1.3  macallan {
    473  1.43   thorpej 	struct ki2c_channel *ch = cookie;
    474  1.43   thorpej 	struct ki2c_softc *sc = ch->ch_sc;
    475  1.12  pgoyette 	int i;
    476  1.12  pgoyette 	size_t w_len;
    477  1.12  pgoyette 	uint8_t *wp;
    478  1.33   mlelstv 	uint8_t wrbuf[KI2C_EXEC_MAX_CMDLEN + KI2C_EXEC_MAX_CMDLEN];
    479  1.12  pgoyette 
    480  1.12  pgoyette 	/*
    481  1.12  pgoyette 	 * We don't have any idea if the ki2c controller can execute
    482  1.12  pgoyette 	 * i2c quick_{read,write} operations, so if someone tries one,
    483  1.12  pgoyette 	 * return an error.
    484  1.12  pgoyette 	 */
    485  1.12  pgoyette 	if (cmdlen == 0 && buflen == 0)
    486  1.12  pgoyette 		return -1;
    487  1.12  pgoyette 
    488  1.33   mlelstv 	/*
    489  1.33   mlelstv 	 * Transaction could be much larger now. Bail if it exceeds our
    490  1.33   mlelstv 	 * small combining buffer, we don't expect such devices.
    491  1.33   mlelstv 	 */
    492  1.33   mlelstv 	if (cmdlen + buflen > sizeof(wrbuf))
    493  1.33   mlelstv 		return -1;
    494  1.33   mlelstv 
    495  1.43   thorpej 	addr &= 0x7f;	/* KASSERT((addr & ~0x7f) == 0); ??? */
    496  1.25  macallan 
    497  1.28  macallan 	ki2c_setspeed(sc, I2C_50kHz);
    498   1.3  macallan 
    499  1.12  pgoyette 	/* Write-buffer defaults to vcmd */
    500  1.12  pgoyette 	wp = (uint8_t *)(__UNCONST(vcmd));
    501  1.12  pgoyette 	w_len = cmdlen;
    502  1.12  pgoyette 
    503  1.12  pgoyette 	/*
    504  1.12  pgoyette 	 * Concatenate vcmd and vbuf for write operations
    505  1.12  pgoyette 	 *
    506  1.12  pgoyette 	 * Drivers written specifically for ki2c might already do this,
    507  1.12  pgoyette 	 * but "generic" i2c drivers still provide separate arguments
    508  1.12  pgoyette 	 * for the cmd and buf parts of iic_smbus_write_{byte,word}.
    509  1.12  pgoyette 	 */
    510  1.12  pgoyette 	if (I2C_OP_WRITE_P(op) && buflen != 0) {
    511  1.12  pgoyette 		if (cmdlen == 0) {
    512  1.12  pgoyette 			wp = (uint8_t *)vbuf;
    513  1.12  pgoyette 			w_len = buflen;
    514  1.12  pgoyette 		} else {
    515  1.12  pgoyette 			KASSERT((cmdlen + buflen) <= sizeof(wrbuf));
    516  1.12  pgoyette 			wp = (uint8_t *)(__UNCONST(vcmd));
    517  1.12  pgoyette 			w_len = 0;
    518  1.12  pgoyette 			for (i = 0; i < cmdlen; i++)
    519  1.12  pgoyette 				wrbuf[w_len++] = *wp++;
    520  1.12  pgoyette 			wp = (uint8_t *)vbuf;
    521  1.12  pgoyette 			for (i = 0; i < buflen; i++)
    522  1.12  pgoyette 				wrbuf[w_len++] = *wp++;
    523  1.12  pgoyette 			wp = wrbuf;
    524  1.12  pgoyette 		}
    525  1.12  pgoyette 	}
    526  1.12  pgoyette 
    527  1.22  macallan 	if (w_len > 0)
    528  1.22  macallan 		if (ki2c_write(sc, addr << 1, 0, wp, w_len) !=0 )
    529  1.22  macallan 			return -1;
    530  1.10   garbled 
    531  1.10   garbled 	if (I2C_OP_READ_P(op)) {
    532  1.20  macallan 		if (ki2c_read(sc, addr << 1, 0, vbuf, buflen) !=0 )
    533   1.3  macallan 			return -1;
    534   1.3  macallan 	}
    535   1.3  macallan 	return 0;
    536   1.1     grant }
    537