Home | History | Annotate | Line # | Download | only in dev
ki2c.c revision 1.33.10.1
      1  1.33.10.1  perseant /*	$NetBSD: ki2c.c,v 1.33.10.1 2025/08/02 05:55:51 perseant 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.33.10.1  perseant #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.33.10.1  perseant 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.1     grant 
     70       1.18  macallan CFATTACH_DECL_NEW(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
     71        1.9    dogcow 	NULL, NULL);
     72        1.1     grant 
     73        1.1     grant int
     74       1.17      matt ki2c_match(device_t parent, cfdata_t match, void *aux)
     75        1.1     grant {
     76        1.1     grant 	struct confargs *ca = aux;
     77        1.1     grant 
     78        1.1     grant 	if (strcmp(ca->ca_name, "i2c") == 0)
     79        1.1     grant 		return 1;
     80        1.1     grant 
     81        1.1     grant 	return 0;
     82        1.1     grant }
     83        1.1     grant 
     84        1.1     grant void
     85       1.17      matt ki2c_attach(device_t parent, device_t self, void *aux)
     86        1.1     grant {
     87       1.17      matt 	struct ki2c_softc *sc = device_private(self);
     88        1.1     grant 	struct confargs *ca = aux;
     89        1.1     grant 	int node = ca->ca_node;
     90  1.33.10.1  perseant 	uint32_t addr, channel, reg, intr[2];
     91       1.28  macallan 	int rate, child, /*namelen,*/ i2cbus[2] = {0, 0};
     92        1.3  macallan 	struct i2cbus_attach_args iba;
     93       1.20  macallan 	prop_dictionary_t dict = device_properties(self);
     94       1.20  macallan 	prop_array_t cfg;
     95  1.33.10.1  perseant 	int devs, devc, intrparent;;
     96       1.26  macallan 	char compat[256], num[8], descr[32];
     97       1.22  macallan 	prop_dictionary_t dev;
     98       1.22  macallan 	prop_data_t data;
     99  1.33.10.1  perseant 	char name[32], intr_xname[32];
    100  1.33.10.1  perseant 	uint32_t picbase;
    101       1.18  macallan 
    102       1.18  macallan 	sc->sc_dev = self;
    103       1.21  macallan 	sc->sc_tag = ca->ca_tag;
    104        1.1     grant 	ca->ca_reg[0] += ca->ca_baseaddr;
    105        1.1     grant 
    106        1.1     grant 	if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
    107       1.20  macallan 		aprint_error(": cannot get i2c-rate\n");
    108        1.1     grant 		return;
    109        1.1     grant 	}
    110       1.20  macallan 	if (OF_getprop(node, "AAPL,address", &addr, 4) != 4) {
    111       1.20  macallan 		aprint_error(": unable to find i2c address\n");
    112        1.1     grant 		return;
    113        1.1     grant 	}
    114       1.21  macallan 	if (bus_space_map(sc->sc_tag, addr, PAGE_SIZE, 0, &sc->sc_bh) != 0) {
    115       1.21  macallan 		aprint_error_dev(sc->sc_dev, "failed to map registers\n");
    116       1.21  macallan 		return;
    117       1.21  macallan 	}
    118       1.21  macallan 
    119        1.1     grant 	if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
    120       1.20  macallan 		aprint_error(": unable to find i2c address step\n");
    121        1.1     grant 		return;
    122        1.1     grant 	}
    123        1.1     grant 
    124  1.33.10.1  perseant 	if(OF_getprop(node, "interrupts", intr, 8) != 8) {
    125  1.33.10.1  perseant 		aprint_error(": can't find interrupt\n");
    126  1.33.10.1  perseant 		return;
    127  1.33.10.1  perseant 	}
    128  1.33.10.1  perseant 
    129  1.33.10.1  perseant 	/*
    130  1.33.10.1  perseant 	 * on some G5 we have two openpics, one in mac-io, one in /u3
    131  1.33.10.1  perseant 	 * in order to get interrupts we need to know which one we're
    132  1.33.10.1  perseant 	 * connected to
    133  1.33.10.1  perseant 	 */
    134  1.33.10.1  perseant 	sc->sc_poll = 0;
    135  1.33.10.1  perseant 
    136  1.33.10.1  perseant 	if(OF_getprop(node, "interrupt-parent", &intrparent, 4) == 4) {
    137  1.33.10.1  perseant 		uint32_t preg[8];
    138  1.33.10.1  perseant 		struct pic_ops *pic;
    139  1.33.10.1  perseant 
    140  1.33.10.1  perseant 		sc->sc_poll = 1;
    141  1.33.10.1  perseant 		if(OF_getprop(intrparent, "reg", preg, 8) > 4) {
    142  1.33.10.1  perseant 			/* now look for a pic with that base... */
    143  1.33.10.1  perseant 			picbase = preg[0];
    144  1.33.10.1  perseant 			if ((picbase & 0x80000000) == 0) {
    145  1.33.10.1  perseant 				/* some OF versions have the openpic's reg as
    146  1.33.10.1  perseant 				 * an offset into mac-io just to be annoying */
    147  1.33.10.1  perseant 				int mio = OF_parent(intrparent);
    148  1.33.10.1  perseant 				if (OF_getprop(mio, "ranges", preg, 20) == 20)
    149  1.33.10.1  perseant 					picbase += preg[3];
    150  1.33.10.1  perseant 			}
    151  1.33.10.1  perseant 			DPRINTF("PIC base %08x\n", picbase);
    152  1.33.10.1  perseant 			pic = find_pic_by_cookie((void *)picbase);
    153  1.33.10.1  perseant 			if (pic != NULL) {
    154  1.33.10.1  perseant 				sc->sc_poll = 0;
    155  1.33.10.1  perseant 				intr[0] += pic->pic_intrbase;
    156  1.33.10.1  perseant 			}
    157  1.33.10.1  perseant 		}
    158  1.33.10.1  perseant 	}
    159  1.33.10.1  perseant 
    160  1.33.10.1  perseant 	if (sc->sc_poll) {
    161  1.33.10.1  perseant 		aprint_normal(" polling");
    162  1.33.10.1  perseant 	} else aprint_normal(" irq %d", intr[0]);
    163  1.33.10.1  perseant 
    164        1.1     grant 	printf("\n");
    165        1.1     grant 
    166        1.1     grant 	ki2c_writereg(sc, STATUS, 0);
    167        1.1     grant 	ki2c_writereg(sc, ISR, 0);
    168        1.1     grant 	ki2c_writereg(sc, IER, 0);
    169        1.1     grant 
    170        1.1     grant 	ki2c_setmode(sc, I2C_STDSUBMODE);
    171        1.1     grant 	ki2c_setspeed(sc, I2C_100kHz);		/* XXX rate */
    172        1.3  macallan 
    173        1.3  macallan 	ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
    174        1.3  macallan 
    175       1.20  macallan 	cfg = prop_array_create();
    176       1.20  macallan 	prop_dictionary_set(dict, "i2c-child-devices", cfg);
    177       1.20  macallan 	prop_object_release(cfg);
    178       1.20  macallan 
    179        1.4  macallan 	/*
    180        1.4  macallan 	 * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
    181        1.4  macallan 	 * directly to the ki2c node so we just check if we have a child named
    182       1.25  macallan 	 * 'i2c-bus' and if so we attach its children, not ours
    183       1.25  macallan 	 *
    184       1.25  macallan 	 * XXX
    185       1.25  macallan 	 * should probably check for multiple i2c-bus children
    186        1.4  macallan 	 */
    187       1.28  macallan 
    188       1.28  macallan 	int found_busnode = 0;
    189       1.25  macallan 	channel = 0;
    190        1.4  macallan 	child = OF_child(node);
    191       1.28  macallan 	while (child != 0) {
    192        1.4  macallan 		OF_getprop(child, "name", name, sizeof(name));
    193       1.25  macallan 		if (strcmp(name, "i2c-bus") == 0) {
    194       1.25  macallan 			OF_getprop(child, "reg", &channel, sizeof(channel));
    195       1.28  macallan 			i2cbus[channel] = child;
    196       1.25  macallan 			DPRINTF("found channel %x\n", channel);
    197       1.28  macallan 			found_busnode = 1;
    198       1.25  macallan 		}
    199        1.4  macallan 		child = OF_peer(child);
    200        1.4  macallan 	}
    201       1.28  macallan 	if (found_busnode == 0)
    202       1.28  macallan 		i2cbus[0] = node;
    203       1.22  macallan 
    204       1.28  macallan 	for (channel = 0; channel < 2; channel++) {
    205       1.28  macallan 		devs = OF_child(i2cbus[channel]);
    206       1.28  macallan 		while (devs != 0) {
    207       1.28  macallan 			if (OF_getprop(devs, "name", name, 32) <= 0)
    208       1.22  macallan 				goto skip;
    209       1.28  macallan 			if (OF_getprop(devs, "compatible", compat, 256) <= 0) {
    210       1.28  macallan 				/* some i2c device nodes don't have 'compatible' */
    211       1.28  macallan 				memset(compat, 0, 256);
    212       1.28  macallan 				strncpy(compat, name, 256);
    213       1.28  macallan 			}
    214       1.28  macallan 			if (OF_getprop(devs, "reg", &addr, 4) <= 0)
    215       1.28  macallan 				if (OF_getprop(devs, "i2c-address", &addr, 4) <= 0)
    216       1.28  macallan 					goto skip;
    217       1.28  macallan 			addr |= channel << 8;
    218       1.28  macallan 			addr = addr >> 1;
    219       1.28  macallan 			DPRINTF("-> %s@%x\n", name, addr);
    220       1.28  macallan 			dev = prop_dictionary_create();
    221       1.30  macallan 			prop_dictionary_set_string(dev, "name", name);
    222       1.30  macallan 			data = prop_data_create_copy(compat, strlen(compat)+1);
    223       1.28  macallan 			prop_dictionary_set(dev, "compatible", data);
    224       1.28  macallan 			prop_object_release(data);
    225       1.28  macallan 			prop_dictionary_set_uint32(dev, "addr", addr);
    226       1.28  macallan 			prop_dictionary_set_uint64(dev, "cookie", devs);
    227       1.28  macallan 			/* look for location info for sensors */
    228       1.28  macallan 			devc = OF_child(devs);
    229  1.33.10.1  perseant 			if (devc == 0) {
    230  1.33.10.1  perseant 				/* old style name info */
    231  1.33.10.1  perseant 				uint32_t ids[4];
    232  1.33.10.1  perseant 				int len = OF_getprop(devs, "hwsensor-id", ids, 16);
    233  1.33.10.1  perseant 				int i = 0, idx = 0;
    234  1.33.10.1  perseant 				char buffer[256];
    235  1.33.10.1  perseant 				memset(buffer, 0, 256);
    236  1.33.10.1  perseant 				OF_getprop(devs, "hwsensor-location", buffer, 256);
    237  1.33.10.1  perseant 				while (len > 0) {
    238  1.33.10.1  perseant 					reg = ids[i];
    239  1.33.10.1  perseant 					strcpy(descr, &buffer[idx]);
    240  1.33.10.1  perseant 					idx += strlen(descr) + 1;
    241  1.33.10.1  perseant 					DPRINTF("found '%s' at %02x\n", descr, reg);
    242  1.33.10.1  perseant 					snprintf(num, 7, "s%02x", i);
    243  1.33.10.1  perseant 					prop_dictionary_set_string(dev, num, descr);
    244  1.33.10.1  perseant 					i++;
    245  1.33.10.1  perseant 					len -= 4;
    246  1.33.10.1  perseant 				}
    247  1.33.10.1  perseant 			} else {
    248  1.33.10.1  perseant 				while (devc != 0) {
    249  1.33.10.1  perseant 					if (OF_getprop(devc, "reg", &reg, 4) < 4) goto nope;
    250  1.33.10.1  perseant 					if (OF_getprop(devc, "location", descr, 32) <= 0)
    251  1.33.10.1  perseant 						goto nope;
    252  1.33.10.1  perseant 					DPRINTF("found '%s' at %02x\n", descr, reg);
    253  1.33.10.1  perseant 					snprintf(num, 7, "s%02x", reg);
    254  1.33.10.1  perseant 					prop_dictionary_set_string(dev, num, descr);
    255  1.33.10.1  perseant 				nope:
    256  1.33.10.1  perseant 					devc = OF_peer(devc);
    257  1.33.10.1  perseant 				}
    258       1.28  macallan 			}
    259  1.33.10.1  perseant 
    260       1.28  macallan 			prop_array_add(cfg, dev);
    261       1.28  macallan 			prop_object_release(dev);
    262       1.28  macallan 		skip:
    263       1.28  macallan 			devs = OF_peer(devs);
    264       1.26  macallan 		}
    265        1.3  macallan 	}
    266        1.3  macallan 
    267  1.33.10.1  perseant 	cv_init(&sc->sc_todev, device_xname(self));
    268  1.33.10.1  perseant 	mutex_init(&sc->sc_todevmtx, MUTEX_DEFAULT, IPL_NONE);
    269  1.33.10.1  perseant 
    270  1.33.10.1  perseant 	if(sc->sc_poll == 0) {
    271  1.33.10.1  perseant 		snprintf(intr_xname, sizeof(intr_xname), "%s intr", device_xname(self));
    272  1.33.10.1  perseant 		intr_establish_xname(intr[0], (intr[1] & 1) ? IST_LEVEL : IST_EDGE,
    273  1.33.10.1  perseant 		    IPL_BIO, ki2c_intr, sc, intr_xname);
    274  1.33.10.1  perseant 
    275  1.33.10.1  perseant 		ki2c_writereg(sc, IER, I2C_INT_DATA | I2C_INT_ADDR| I2C_INT_STOP);
    276  1.33.10.1  perseant 	}
    277  1.33.10.1  perseant 
    278       1.22  macallan 	/* fill in the i2c tag */
    279       1.27   thorpej 	iic_tag_init(&sc->sc_i2c);
    280       1.22  macallan 	sc->sc_i2c.ic_cookie = sc;
    281       1.22  macallan 	sc->sc_i2c.ic_exec = ki2c_i2c_exec;
    282        1.3  macallan 
    283       1.22  macallan 	memset(&iba, 0, sizeof(iba));
    284       1.22  macallan 	iba.iba_tag = &sc->sc_i2c;
    285       1.32   thorpej 	config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
    286       1.22  macallan 
    287        1.1     grant }
    288        1.1     grant 
    289       1.21  macallan uint8_t
    290       1.14       dsl ki2c_readreg(struct ki2c_softc *sc, int reg)
    291        1.1     grant {
    292        1.1     grant 
    293       1.21  macallan 	return bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_regstep * reg);
    294        1.1     grant }
    295        1.1     grant 
    296        1.1     grant void
    297       1.21  macallan ki2c_writereg(struct ki2c_softc *sc, int reg, uint8_t val)
    298        1.1     grant {
    299       1.21  macallan 
    300       1.21  macallan 	bus_space_write_1(sc->sc_tag, sc->sc_bh, reg * sc->sc_regstep, val);
    301        1.1     grant 	delay(10);
    302        1.1     grant }
    303        1.1     grant 
    304        1.1     grant u_int
    305       1.14       dsl ki2c_getmode(struct ki2c_softc *sc)
    306        1.1     grant {
    307        1.1     grant 	return ki2c_readreg(sc, MODE) & I2C_MODE;
    308        1.1     grant }
    309        1.1     grant 
    310        1.1     grant void
    311       1.14       dsl ki2c_setmode(struct ki2c_softc *sc, u_int mode)
    312        1.1     grant {
    313       1.25  macallan 	ki2c_writereg(sc, MODE, mode);
    314        1.1     grant }
    315        1.1     grant 
    316        1.1     grant u_int
    317       1.14       dsl ki2c_getspeed(struct ki2c_softc *sc)
    318        1.1     grant {
    319        1.1     grant 	return ki2c_readreg(sc, MODE) & I2C_SPEED;
    320        1.1     grant }
    321        1.1     grant 
    322        1.1     grant void
    323       1.14       dsl ki2c_setspeed(struct ki2c_softc *sc, u_int speed)
    324        1.1     grant {
    325        1.1     grant 	u_int x;
    326        1.1     grant 
    327        1.1     grant 	KASSERT((speed & ~I2C_SPEED) == 0);
    328        1.1     grant 	x = ki2c_readreg(sc, MODE);
    329        1.1     grant 	x &= ~I2C_SPEED;
    330        1.1     grant 	x |= speed;
    331        1.1     grant 	ki2c_writereg(sc, MODE, x);
    332        1.1     grant }
    333        1.1     grant 
    334        1.1     grant int
    335  1.33.10.1  perseant ki2c_intr(void *cookie)
    336        1.1     grant {
    337  1.33.10.1  perseant 	struct ki2c_softc *sc = cookie;
    338        1.1     grant 	u_int isr, x;
    339        1.1     grant 	isr = ki2c_readreg(sc, ISR);
    340        1.1     grant 	if (isr & I2C_INT_ADDR) {
    341        1.1     grant #if 0
    342        1.1     grant 		if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    343        1.1     grant 			/* No slave responded. */
    344        1.1     grant 			sc->sc_flags |= I2C_ERROR;
    345        1.1     grant 			goto out;
    346        1.1     grant 		}
    347        1.1     grant #endif
    348        1.1     grant 
    349        1.1     grant 		if (sc->sc_flags & I2C_READING) {
    350        1.1     grant 			if (sc->sc_resid > 1) {
    351        1.1     grant 				x = ki2c_readreg(sc, CONTROL);
    352        1.1     grant 				x |= I2C_CT_AAK;
    353        1.1     grant 				ki2c_writereg(sc, CONTROL, x);
    354        1.1     grant 			}
    355        1.1     grant 		} else {
    356        1.1     grant 			ki2c_writereg(sc, DATA, *sc->sc_data++);
    357        1.1     grant 			sc->sc_resid--;
    358        1.1     grant 		}
    359        1.1     grant 	}
    360        1.1     grant 
    361        1.1     grant 	if (isr & I2C_INT_DATA) {
    362        1.1     grant 		if (sc->sc_flags & I2C_READING) {
    363        1.1     grant 			*sc->sc_data++ = ki2c_readreg(sc, DATA);
    364        1.1     grant 			sc->sc_resid--;
    365        1.1     grant 
    366        1.1     grant 			if (sc->sc_resid == 0) {	/* Completed */
    367        1.1     grant 				ki2c_writereg(sc, CONTROL, 0);
    368        1.1     grant 				goto out;
    369        1.1     grant 			}
    370        1.1     grant 		} else {
    371        1.1     grant #if 0
    372        1.1     grant 			if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    373        1.1     grant 				/* No slave responded. */
    374        1.1     grant 				sc->sc_flags |= I2C_ERROR;
    375        1.1     grant 				goto out;
    376        1.1     grant 			}
    377        1.1     grant #endif
    378        1.1     grant 
    379        1.1     grant 			if (sc->sc_resid == 0) {
    380        1.1     grant 				x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
    381        1.1     grant 				ki2c_writereg(sc, CONTROL, x);
    382        1.1     grant 			} else {
    383        1.1     grant 				ki2c_writereg(sc, DATA, *sc->sc_data++);
    384        1.1     grant 				sc->sc_resid--;
    385        1.1     grant 			}
    386        1.1     grant 		}
    387        1.1     grant 	}
    388        1.1     grant 
    389        1.1     grant out:
    390        1.1     grant 	if (isr & I2C_INT_STOP) {
    391        1.1     grant 		ki2c_writereg(sc, CONTROL, 0);
    392        1.1     grant 		sc->sc_flags &= ~I2C_BUSY;
    393  1.33.10.1  perseant 		cv_signal(&sc->sc_todev);
    394        1.1     grant 	}
    395        1.1     grant 
    396        1.1     grant 	ki2c_writereg(sc, ISR, isr);
    397        1.1     grant 
    398        1.1     grant 	return 1;
    399        1.1     grant }
    400        1.1     grant 
    401        1.1     grant int
    402       1.14       dsl ki2c_poll(struct ki2c_softc *sc, int timo)
    403        1.1     grant {
    404  1.33.10.1  perseant 	int bail = 0;
    405        1.1     grant 	while (sc->sc_flags & I2C_BUSY) {
    406  1.33.10.1  perseant 		if ((cold) || (bail > 10) || (sc->sc_poll)) {
    407  1.33.10.1  perseant 			if (ki2c_readreg(sc, ISR))
    408  1.33.10.1  perseant 				ki2c_intr(sc);
    409  1.33.10.1  perseant 			timo -= 10;
    410  1.33.10.1  perseant 			if (timo < 0) {
    411  1.33.10.1  perseant 				DPRINTF("i2c_poll: timeout\n");
    412  1.33.10.1  perseant 				return -1;
    413  1.33.10.1  perseant 			}
    414  1.33.10.1  perseant 			delay(10);
    415  1.33.10.1  perseant 		} else {
    416  1.33.10.1  perseant 			mutex_enter(&sc->sc_todevmtx);
    417  1.33.10.1  perseant 			cv_timedwait_sig(&sc->sc_todev, &sc->sc_todevmtx, hz/10);
    418  1.33.10.1  perseant 			mutex_exit(&sc->sc_todevmtx);
    419  1.33.10.1  perseant 			bail++;
    420        1.1     grant 		}
    421        1.1     grant 	}
    422        1.1     grant 	return 0;
    423        1.1     grant }
    424        1.1     grant 
    425        1.1     grant int
    426       1.15       dsl ki2c_start(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    427        1.1     grant {
    428        1.1     grant 	int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
    429        1.1     grant 	int timo, x;
    430        1.1     grant 
    431        1.1     grant 	KASSERT((addr & 1) == 0);
    432        1.1     grant 
    433        1.1     grant 	sc->sc_data = data;
    434        1.1     grant 	sc->sc_resid = len;
    435        1.1     grant 	sc->sc_flags |= I2C_BUSY;
    436        1.1     grant 
    437        1.1     grant 	timo = 1000 + len * 200;
    438        1.1     grant 
    439        1.1     grant 	/* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
    440        1.1     grant 	/* if (addr == 0x68) */
    441        1.1     grant 		timo += 100000;
    442        1.1     grant 
    443        1.1     grant 	ki2c_writereg(sc, ADDR, addr | rw);
    444        1.1     grant 	ki2c_writereg(sc, SUBADDR, subaddr);
    445        1.1     grant 
    446        1.1     grant 	x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
    447        1.1     grant 	ki2c_writereg(sc, CONTROL, x);
    448        1.1     grant 
    449        1.1     grant 	if (ki2c_poll(sc, timo))
    450        1.1     grant 		return -1;
    451        1.1     grant 	if (sc->sc_flags & I2C_ERROR) {
    452       1.20  macallan 		DPRINTF("I2C_ERROR\n");
    453        1.1     grant 		return -1;
    454        1.1     grant 	}
    455        1.1     grant 	return 0;
    456        1.1     grant }
    457        1.1     grant 
    458        1.1     grant int
    459       1.15       dsl ki2c_read(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    460        1.1     grant {
    461        1.1     grant 	sc->sc_flags = I2C_READING;
    462       1.20  macallan 	DPRINTF("ki2c_read: %02x %d\n", addr, len);
    463        1.1     grant 	return ki2c_start(sc, addr, subaddr, data, len);
    464        1.1     grant }
    465        1.1     grant 
    466        1.1     grant int
    467       1.15       dsl ki2c_write(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    468        1.1     grant {
    469        1.1     grant 	sc->sc_flags = 0;
    470       1.20  macallan 	DPRINTF("ki2c_write: %02x %d\n",addr,len);
    471        1.3  macallan 	return ki2c_start(sc, addr, subaddr, data, len);
    472        1.3  macallan }
    473        1.3  macallan 
    474        1.3  macallan int
    475        1.3  macallan ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    476        1.3  macallan     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    477        1.3  macallan {
    478        1.3  macallan 	struct ki2c_softc *sc = cookie;
    479       1.12  pgoyette 	int i;
    480       1.12  pgoyette 	size_t w_len;
    481       1.12  pgoyette 	uint8_t *wp;
    482       1.33   mlelstv 	uint8_t wrbuf[KI2C_EXEC_MAX_CMDLEN + KI2C_EXEC_MAX_CMDLEN];
    483       1.25  macallan 	uint8_t channel;
    484       1.12  pgoyette 
    485       1.12  pgoyette 	/*
    486       1.12  pgoyette 	 * We don't have any idea if the ki2c controller can execute
    487       1.12  pgoyette 	 * i2c quick_{read,write} operations, so if someone tries one,
    488       1.12  pgoyette 	 * return an error.
    489       1.12  pgoyette 	 */
    490       1.12  pgoyette 	if (cmdlen == 0 && buflen == 0)
    491       1.12  pgoyette 		return -1;
    492       1.12  pgoyette 
    493       1.33   mlelstv 	/*
    494       1.33   mlelstv 	 * Transaction could be much larger now. Bail if it exceeds our
    495       1.33   mlelstv 	 * small combining buffer, we don't expect such devices.
    496       1.33   mlelstv 	 */
    497       1.33   mlelstv 	if (cmdlen + buflen > sizeof(wrbuf))
    498       1.33   mlelstv 		return -1;
    499       1.33   mlelstv 
    500       1.25  macallan 	channel = (addr & 0xf80) ? 0x10 : 0x00;
    501       1.25  macallan 	addr &= 0x7f;
    502       1.25  macallan 
    503       1.25  macallan 
    504        1.3  macallan 	/* we handle the subaddress stuff ourselves */
    505       1.25  macallan 	ki2c_setmode(sc, channel | I2C_STDMODE);
    506       1.28  macallan 	ki2c_setspeed(sc, I2C_50kHz);
    507        1.3  macallan 
    508       1.12  pgoyette 	/* Write-buffer defaults to vcmd */
    509       1.12  pgoyette 	wp = (uint8_t *)(__UNCONST(vcmd));
    510       1.12  pgoyette 	w_len = cmdlen;
    511       1.12  pgoyette 
    512       1.12  pgoyette 	/*
    513       1.12  pgoyette 	 * Concatenate vcmd and vbuf for write operations
    514       1.12  pgoyette 	 *
    515       1.12  pgoyette 	 * Drivers written specifically for ki2c might already do this,
    516       1.12  pgoyette 	 * but "generic" i2c drivers still provide separate arguments
    517       1.12  pgoyette 	 * for the cmd and buf parts of iic_smbus_write_{byte,word}.
    518       1.12  pgoyette 	 */
    519       1.12  pgoyette 	if (I2C_OP_WRITE_P(op) && buflen != 0) {
    520       1.12  pgoyette 		if (cmdlen == 0) {
    521       1.12  pgoyette 			wp = (uint8_t *)vbuf;
    522       1.12  pgoyette 			w_len = buflen;
    523       1.12  pgoyette 		} else {
    524       1.12  pgoyette 			KASSERT((cmdlen + buflen) <= sizeof(wrbuf));
    525       1.12  pgoyette 			wp = (uint8_t *)(__UNCONST(vcmd));
    526       1.12  pgoyette 			w_len = 0;
    527       1.12  pgoyette 			for (i = 0; i < cmdlen; i++)
    528       1.12  pgoyette 				wrbuf[w_len++] = *wp++;
    529       1.12  pgoyette 			wp = (uint8_t *)vbuf;
    530       1.12  pgoyette 			for (i = 0; i < buflen; i++)
    531       1.12  pgoyette 				wrbuf[w_len++] = *wp++;
    532       1.12  pgoyette 			wp = wrbuf;
    533       1.12  pgoyette 		}
    534       1.12  pgoyette 	}
    535       1.12  pgoyette 
    536       1.22  macallan 	if (w_len > 0)
    537       1.22  macallan 		if (ki2c_write(sc, addr << 1, 0, wp, w_len) !=0 )
    538       1.22  macallan 			return -1;
    539       1.10   garbled 
    540       1.10   garbled 	if (I2C_OP_READ_P(op)) {
    541       1.20  macallan 		if (ki2c_read(sc, addr << 1, 0, vbuf, buflen) !=0 )
    542        1.3  macallan 			return -1;
    543        1.3  macallan 	}
    544        1.3  macallan 	return 0;
    545        1.1     grant }
    546