Home | History | Annotate | Line # | Download | only in dev
ki2c.c revision 1.9
      1 /*	$NetBSD: ki2c.c,v 1.9 2007/09/27 08:49:33 dogcow Exp $	*/
      2 /*	Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp	*/
      3 
      4 /*-
      5  * Copyright (c) 2001 Tsubai Masanari.  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  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/device.h>
     32 #include <sys/systm.h>
     33 
     34 #include <dev/ofw/openfirm.h>
     35 #include <uvm/uvm_extern.h>
     36 #include <machine/autoconf.h>
     37 
     38 #include <macppc/dev/ki2cvar.h>
     39 
     40 int ki2c_match(struct device *, struct cfdata *, void *);
     41 void ki2c_attach(struct device *, struct device *, void *);
     42 inline u_int ki2c_readreg(struct ki2c_softc *, int);
     43 inline void ki2c_writereg(struct ki2c_softc *, int, u_int);
     44 u_int ki2c_getmode(struct ki2c_softc *);
     45 void ki2c_setmode(struct ki2c_softc *, u_int);
     46 u_int ki2c_getspeed(struct ki2c_softc *);
     47 void ki2c_setspeed(struct ki2c_softc *, u_int);
     48 int ki2c_intr(struct ki2c_softc *);
     49 int ki2c_poll(struct ki2c_softc *, int);
     50 int ki2c_start(struct ki2c_softc *, int, int, void *, int);
     51 int ki2c_read(struct ki2c_softc *, int, int, void *, int);
     52 int ki2c_write(struct ki2c_softc *, int, int, void *, int);
     53 int ki2c_print __P((void *, const char *));
     54 
     55 /* I2C glue */
     56 static int ki2c_i2c_acquire_bus(void *, int);
     57 static void ki2c_i2c_release_bus(void *, int);
     58 static int ki2c_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     59 		    void *, size_t, int);
     60 
     61 
     62 CFATTACH_DECL(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
     63 	NULL, NULL);
     64 
     65 int
     66 ki2c_match(parent, match, aux)
     67 	struct device *parent;
     68 	struct cfdata *match;
     69 	void *aux;
     70 {
     71 	struct confargs *ca = aux;
     72 
     73 	if (strcmp(ca->ca_name, "i2c") == 0)
     74 		return 1;
     75 
     76 	return 0;
     77 }
     78 
     79 void
     80 ki2c_attach(parent, self, aux)
     81 	struct device *parent;
     82 	struct device *self;
     83 	void *aux;
     84 {
     85 	struct ki2c_softc *sc = (struct ki2c_softc *)self;
     86 	struct confargs *ca = aux;
     87 	int node = ca->ca_node;
     88 	int rate, child, namelen, i2cbus;
     89 	struct ki2c_confargs ka;
     90 	struct i2cbus_attach_args iba;
     91 
     92 	char name[32];
     93 	u_int reg[20];
     94 
     95 	ca->ca_reg[0] += ca->ca_baseaddr;
     96 
     97 	if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
     98 		printf(": cannot get i2c-rate\n");
     99 		return;
    100 	}
    101 	if (OF_getprop(node, "AAPL,address", &sc->sc_reg, 4) != 4) {
    102 		printf(": unable to find i2c address\n");
    103 		return;
    104 	}
    105 	if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
    106 		printf(": unable to find i2c address step\n");
    107 		return;
    108 	}
    109 
    110 	printf("\n");
    111 
    112 	ki2c_writereg(sc, STATUS, 0);
    113 	ki2c_writereg(sc, ISR, 0);
    114 	ki2c_writereg(sc, IER, 0);
    115 
    116 	ki2c_setmode(sc, I2C_STDSUBMODE);
    117 	ki2c_setspeed(sc, I2C_100kHz);		/* XXX rate */
    118 
    119 	lockinit(&sc->sc_buslock, PRIBIO|PCATCH, sc->sc_dev.dv_xname, 0, 0);
    120 	ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
    121 
    122 	/* fill in the i2c tag */
    123 	sc->sc_i2c.ic_cookie = sc;
    124 	sc->sc_i2c.ic_acquire_bus = ki2c_i2c_acquire_bus;
    125 	sc->sc_i2c.ic_release_bus = ki2c_i2c_release_bus;
    126 	sc->sc_i2c.ic_send_start = NULL;
    127 	sc->sc_i2c.ic_send_stop = NULL;
    128 	sc->sc_i2c.ic_initiate_xfer = NULL;
    129 	sc->sc_i2c.ic_read_byte = NULL;
    130 	sc->sc_i2c.ic_write_byte = NULL;
    131 	sc->sc_i2c.ic_exec = ki2c_i2c_exec;
    132 
    133 	iba.iba_tag = &sc->sc_i2c;
    134 	(void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
    135 
    136 	/*
    137 	 * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
    138 	 * directly to the ki2c node so we just check if we have a child named
    139 	 * 'i2c-bus' and if so we attach its children, not ours
    140 	 */
    141 	i2cbus = 0;
    142 	child = OF_child(node);
    143 	while ((child != 0) && (i2cbus == 0)) {
    144 		OF_getprop(child, "name", name, sizeof(name));
    145 		if (strcmp(name, "i2c-bus") == 0)
    146 			i2cbus = child;
    147 		child = OF_peer(child);
    148 	}
    149 	if (i2cbus == 0)
    150 		i2cbus = node;
    151 
    152 	for (child = OF_child(i2cbus); child; child = OF_peer(child)) {
    153 		namelen = OF_getprop(child, "name", name, sizeof(name));
    154 		if (namelen < 0)
    155 			continue;
    156 		if (namelen >= sizeof(name))
    157 			continue;
    158 
    159 		name[namelen] = 0;
    160 		ka.ka_name = name;
    161 		ka.ka_node = child;
    162 		if (OF_getprop(child, "reg", reg, sizeof(reg))>0) {
    163 			ka.ka_addr = reg[0];
    164 			ka.ka_tag = &sc->sc_i2c;
    165 			config_found_ia(self, "ki2c", &ka, ki2c_print);
    166 		}
    167 	}
    168 }
    169 
    170 int
    171 ki2c_print(aux, ki2c)
    172 	void *aux;
    173 	const char *ki2c;
    174 {
    175 	struct ki2c_confargs *ka = aux;
    176 
    177 	if (ki2c) {
    178 		aprint_normal("%s at %s", ka->ka_name, ki2c);
    179 		aprint_normal(" address 0x%x", ka->ka_addr);
    180 	}
    181 	return UNCONF;
    182 }
    183 
    184 u_int
    185 ki2c_readreg(sc, reg)
    186 	struct ki2c_softc *sc;
    187 	int reg;
    188 {
    189 	u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
    190 
    191 	return *addr;
    192 }
    193 
    194 void
    195 ki2c_writereg(sc, reg, val)
    196 	struct ki2c_softc *sc;
    197 	int reg;
    198 	u_int val;
    199 {
    200 	u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
    201 
    202 	*addr = val;
    203 	__asm volatile ("eieio");
    204 	delay(10);
    205 }
    206 
    207 u_int
    208 ki2c_getmode(sc)
    209 	struct ki2c_softc *sc;
    210 {
    211 	return ki2c_readreg(sc, MODE) & I2C_MODE;
    212 }
    213 
    214 void
    215 ki2c_setmode(sc, mode)
    216 	struct ki2c_softc *sc;
    217 	u_int mode;
    218 {
    219 	u_int x;
    220 
    221 	KASSERT((mode & ~I2C_MODE) == 0);
    222 	x = ki2c_readreg(sc, MODE);
    223 	x &= ~I2C_MODE;
    224 	x |= mode;
    225 	ki2c_writereg(sc, MODE, x);
    226 }
    227 
    228 u_int
    229 ki2c_getspeed(sc)
    230 	struct ki2c_softc *sc;
    231 {
    232 	return ki2c_readreg(sc, MODE) & I2C_SPEED;
    233 }
    234 
    235 void
    236 ki2c_setspeed(sc, speed)
    237 	struct ki2c_softc *sc;
    238 	u_int speed;
    239 {
    240 	u_int x;
    241 
    242 	KASSERT((speed & ~I2C_SPEED) == 0);
    243 	x = ki2c_readreg(sc, MODE);
    244 	x &= ~I2C_SPEED;
    245 	x |= speed;
    246 	ki2c_writereg(sc, MODE, x);
    247 }
    248 
    249 int
    250 ki2c_intr(sc)
    251 	struct ki2c_softc *sc;
    252 {
    253 	u_int isr, x;
    254 
    255 	isr = ki2c_readreg(sc, ISR);
    256 	if (isr & I2C_INT_ADDR) {
    257 #if 0
    258 		if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    259 			/* No slave responded. */
    260 			sc->sc_flags |= I2C_ERROR;
    261 			goto out;
    262 		}
    263 #endif
    264 
    265 		if (sc->sc_flags & I2C_READING) {
    266 			if (sc->sc_resid > 1) {
    267 				x = ki2c_readreg(sc, CONTROL);
    268 				x |= I2C_CT_AAK;
    269 				ki2c_writereg(sc, CONTROL, x);
    270 			}
    271 		} else {
    272 			ki2c_writereg(sc, DATA, *sc->sc_data++);
    273 			sc->sc_resid--;
    274 		}
    275 	}
    276 
    277 	if (isr & I2C_INT_DATA) {
    278 		if (sc->sc_flags & I2C_READING) {
    279 			*sc->sc_data++ = ki2c_readreg(sc, DATA);
    280 			sc->sc_resid--;
    281 
    282 			if (sc->sc_resid == 0) {	/* Completed */
    283 				ki2c_writereg(sc, CONTROL, 0);
    284 				goto out;
    285 			}
    286 		} else {
    287 #if 0
    288 			if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    289 				/* No slave responded. */
    290 				sc->sc_flags |= I2C_ERROR;
    291 				goto out;
    292 			}
    293 #endif
    294 
    295 			if (sc->sc_resid == 0) {
    296 				x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
    297 				ki2c_writereg(sc, CONTROL, x);
    298 			} else {
    299 				ki2c_writereg(sc, DATA, *sc->sc_data++);
    300 				sc->sc_resid--;
    301 			}
    302 		}
    303 	}
    304 
    305 out:
    306 	if (isr & I2C_INT_STOP) {
    307 		ki2c_writereg(sc, CONTROL, 0);
    308 		sc->sc_flags &= ~I2C_BUSY;
    309 	}
    310 
    311 	ki2c_writereg(sc, ISR, isr);
    312 
    313 	return 1;
    314 }
    315 
    316 int
    317 ki2c_poll(sc, timo)
    318 	struct ki2c_softc *sc;
    319 	int timo;
    320 {
    321 	while (sc->sc_flags & I2C_BUSY) {
    322 		if (ki2c_readreg(sc, ISR))
    323 			ki2c_intr(sc);
    324 		timo -= 100;
    325 		if (timo < 0) {
    326 			printf("i2c_poll: timeout\n");
    327 			return -1;
    328 		}
    329 		delay(100);
    330 	}
    331 	return 0;
    332 }
    333 
    334 int
    335 ki2c_start(sc, addr, subaddr, data, len)
    336 	struct ki2c_softc *sc;
    337 	int addr, subaddr, len;
    338 	void *data;
    339 {
    340 	int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
    341 	int timo, x;
    342 
    343 	KASSERT((addr & 1) == 0);
    344 
    345 	sc->sc_data = data;
    346 	sc->sc_resid = len;
    347 	sc->sc_flags |= I2C_BUSY;
    348 
    349 	timo = 1000 + len * 200;
    350 
    351 	/* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
    352 	/* if (addr == 0x68) */
    353 		timo += 100000;
    354 
    355 	ki2c_writereg(sc, ADDR, addr | rw);
    356 	ki2c_writereg(sc, SUBADDR, subaddr);
    357 
    358 	x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
    359 	ki2c_writereg(sc, CONTROL, x);
    360 
    361 	if (ki2c_poll(sc, timo))
    362 		return -1;
    363 	if (sc->sc_flags & I2C_ERROR) {
    364 		printf("I2C_ERROR\n");
    365 		return -1;
    366 	}
    367 	return 0;
    368 }
    369 
    370 int
    371 ki2c_read(sc, addr, subaddr, data, len)
    372 	struct ki2c_softc *sc;
    373 	int addr, subaddr, len;
    374 	void *data;
    375 {
    376 	sc->sc_flags = I2C_READING;
    377 	#ifdef KI2C_DEBUG
    378 		printf("ki2c_read: %02x %d\n", addr, len);
    379 	#endif
    380 	return ki2c_start(sc, addr, subaddr, data, len);
    381 }
    382 
    383 int
    384 ki2c_write(sc, addr, subaddr, data, len)
    385 	struct ki2c_softc *sc;
    386 	int addr, subaddr, len;
    387 	void *data;
    388 {
    389 	sc->sc_flags = 0;
    390 	#ifdef KI2C_DEBUG
    391 		printf("ki2c_write: %02x %d\n",addr,len);
    392 	#endif
    393 	return ki2c_start(sc, addr, subaddr, data, len);
    394 }
    395 
    396 static int
    397 ki2c_i2c_acquire_bus(void *cookie, int flags)
    398 {
    399 	struct ki2c_softc *sc = cookie;
    400 
    401 	return (lockmgr(&sc->sc_buslock, LK_EXCLUSIVE, NULL));
    402 }
    403 
    404 static void
    405 ki2c_i2c_release_bus(void *cookie, int flags)
    406 {
    407 	struct ki2c_softc *sc = cookie;
    408 
    409 	(void) lockmgr(&sc->sc_buslock, LK_RELEASE, NULL);
    410 }
    411 
    412 int
    413 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    414     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    415 {
    416 	struct ki2c_softc *sc = cookie;
    417 
    418 	/* we handle the subaddress stuff ourselves */
    419 	ki2c_setmode(sc, I2C_STDMODE);
    420 
    421 	if (ki2c_write(sc, addr, 0, __UNCONST(vcmd), cmdlen) !=0 )
    422 		return -1;
    423 	if (I2C_OP_READ_P(op))
    424 	{
    425 		if (ki2c_read(sc, addr, 0, vbuf, buflen) !=0 )
    426 			return -1;
    427 	}
    428 	return 0;
    429 }
    430