Home | History | Annotate | Line # | Download | only in dev
ki2c.c revision 1.8.22.1
      1 /*	$NetBSD: ki2c.c,v 1.8.22.1 2007/09/27 07:13:54 macallan 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 CFATTACH_DECL(ki2c, sizeof(struct ki2c_softc),
     62     ki2c_match, ki2c_attach, NULL, NULL);
     63 
     64 int
     65 ki2c_match(parent, match, aux)
     66 	struct device *parent;
     67 	struct cfdata *match;
     68 	void *aux;
     69 {
     70 	struct confargs *ca = aux;
     71 
     72 	if (strcmp(ca->ca_name, "i2c") == 0)
     73 		return 1;
     74 
     75 	return 0;
     76 }
     77 
     78 void
     79 ki2c_attach(parent, self, aux)
     80 	struct device *parent;
     81 	struct device *self;
     82 	void *aux;
     83 {
     84 	struct ki2c_softc *sc = (struct ki2c_softc *)self;
     85 	struct confargs *ca = aux;
     86 	int node = ca->ca_node;
     87 	int rate, child, namelen, i2cbus;
     88 	struct ki2c_confargs ka;
     89 	struct i2cbus_attach_args iba;
     90 
     91 	char name[32];
     92 	u_int reg[20];
     93 
     94 	ca->ca_reg[0] += ca->ca_baseaddr;
     95 
     96 	if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
     97 		printf(": cannot get i2c-rate\n");
     98 		return;
     99 	}
    100 	if (OF_getprop(node, "AAPL,address", &sc->sc_reg, 4) != 4) {
    101 		printf(": unable to find i2c address\n");
    102 		return;
    103 	}
    104 	if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
    105 		printf(": unable to find i2c address step\n");
    106 		return;
    107 	}
    108 
    109 	printf("\n");
    110 
    111 	ki2c_writereg(sc, STATUS, 0);
    112 	ki2c_writereg(sc, ISR, 0);
    113 	ki2c_writereg(sc, IER, 0);
    114 
    115 	ki2c_setmode(sc, I2C_STDSUBMODE);
    116 	ki2c_setspeed(sc, I2C_100kHz);		/* XXX rate */
    117 
    118 	lockinit(&sc->sc_buslock, PRIBIO|PCATCH, sc->sc_dev.dv_xname, 0, 0);
    119 	ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
    120 
    121 	/* fill in the i2c tag */
    122 	sc->sc_i2c.ic_cookie = sc;
    123 	sc->sc_i2c.ic_acquire_bus = ki2c_i2c_acquire_bus;
    124 	sc->sc_i2c.ic_release_bus = ki2c_i2c_release_bus;
    125 	sc->sc_i2c.ic_send_start = NULL;
    126 	sc->sc_i2c.ic_send_stop = NULL;
    127 	sc->sc_i2c.ic_initiate_xfer = NULL;
    128 	sc->sc_i2c.ic_read_byte = NULL;
    129 	sc->sc_i2c.ic_write_byte = NULL;
    130 	sc->sc_i2c.ic_exec = ki2c_i2c_exec;
    131 
    132 	iba.iba_tag = &sc->sc_i2c;
    133 	(void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
    134 
    135 	/*
    136 	 * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
    137 	 * directly to the ki2c node so we just check if we have a child named
    138 	 * 'i2c-bus' and if so we attach its children, not ours
    139 	 */
    140 	i2cbus = 0;
    141 	child = OF_child(node);
    142 	while ((child != 0) && (i2cbus == 0)) {
    143 		OF_getprop(child, "name", name, sizeof(name));
    144 		if (strcmp(name, "i2c-bus") == 0)
    145 			i2cbus = child;
    146 		child = OF_peer(child);
    147 	}
    148 	if (i2cbus == 0)
    149 		i2cbus = node;
    150 
    151 	for (child = OF_child(i2cbus); child; child = OF_peer(child)) {
    152 		int ok = 0;
    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 		ok = OF_getprop(child, "reg", reg, sizeof(reg));
    163 		if (ok <= 0) {
    164 			ok = OF_getprop(child, "i2c-address", reg,
    165 			    sizeof(reg));
    166 		}
    167 		if (ok > 0) {
    168 			ka.ka_addr = reg[0];
    169 			ka.ka_tag = &sc->sc_i2c;
    170 			config_found_ia(self, "ki2c", &ka, ki2c_print);
    171 		}
    172 #ifdef DIAGNOSTIC
    173 		else {
    174 			printf("%s: device (%s) has no reg or i2c-address property.\n",
    175 			    sc->sc_dev.dv_xname, name);
    176 		}
    177 #endif
    178 	}
    179 }
    180 
    181 int
    182 ki2c_print(aux, ki2c)
    183 	void *aux;
    184 	const char *ki2c;
    185 {
    186 	struct ki2c_confargs *ka = aux;
    187 
    188 	if (ki2c) {
    189 		aprint_normal("%s at %s", ka->ka_name, ki2c);
    190 		aprint_normal(" address 0x%x", ka->ka_addr);
    191 	}
    192 	return UNCONF;
    193 }
    194 
    195 u_int
    196 ki2c_readreg(sc, reg)
    197 	struct ki2c_softc *sc;
    198 	int reg;
    199 {
    200 	u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
    201 
    202 	return *addr;
    203 }
    204 
    205 void
    206 ki2c_writereg(sc, reg, val)
    207 	struct ki2c_softc *sc;
    208 	int reg;
    209 	u_int val;
    210 {
    211 	u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
    212 
    213 	*addr = val;
    214 	__asm volatile ("eieio");
    215 	delay(10);
    216 }
    217 
    218 u_int
    219 ki2c_getmode(sc)
    220 	struct ki2c_softc *sc;
    221 {
    222 	return ki2c_readreg(sc, MODE) & I2C_MODE;
    223 }
    224 
    225 void
    226 ki2c_setmode(sc, mode)
    227 	struct ki2c_softc *sc;
    228 	u_int mode;
    229 {
    230 	u_int x;
    231 
    232 	KASSERT((mode & ~I2C_MODE) == 0);
    233 	x = ki2c_readreg(sc, MODE);
    234 	x &= ~I2C_MODE;
    235 	x |= mode;
    236 	ki2c_writereg(sc, MODE, x);
    237 }
    238 
    239 u_int
    240 ki2c_getspeed(sc)
    241 	struct ki2c_softc *sc;
    242 {
    243 	return ki2c_readreg(sc, MODE) & I2C_SPEED;
    244 }
    245 
    246 void
    247 ki2c_setspeed(sc, speed)
    248 	struct ki2c_softc *sc;
    249 	u_int speed;
    250 {
    251 	u_int x;
    252 
    253 	KASSERT((speed & ~I2C_SPEED) == 0);
    254 	x = ki2c_readreg(sc, MODE);
    255 	x &= ~I2C_SPEED;
    256 	x |= speed;
    257 	ki2c_writereg(sc, MODE, x);
    258 }
    259 
    260 int
    261 ki2c_intr(sc)
    262 	struct ki2c_softc *sc;
    263 {
    264 	u_int isr, x;
    265 
    266 	isr = ki2c_readreg(sc, ISR);
    267 	if (isr & I2C_INT_ADDR) {
    268 #if 0
    269 		if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    270 			/* No slave responded. */
    271 			sc->sc_flags |= I2C_ERROR;
    272 			goto out;
    273 		}
    274 #endif
    275 
    276 		if (sc->sc_flags & I2C_READING) {
    277 			if (sc->sc_resid > 1) {
    278 				x = ki2c_readreg(sc, CONTROL);
    279 				x |= I2C_CT_AAK;
    280 				ki2c_writereg(sc, CONTROL, x);
    281 			}
    282 		} else {
    283 			ki2c_writereg(sc, DATA, *sc->sc_data++);
    284 			sc->sc_resid--;
    285 		}
    286 	}
    287 
    288 	if (isr & I2C_INT_DATA) {
    289 		if (sc->sc_flags & I2C_READING) {
    290 			*sc->sc_data++ = ki2c_readreg(sc, DATA);
    291 			sc->sc_resid--;
    292 
    293 			if (sc->sc_resid == 0) {	/* Completed */
    294 				ki2c_writereg(sc, CONTROL, 0);
    295 				goto out;
    296 			}
    297 		} else {
    298 #if 0
    299 			if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    300 				/* No slave responded. */
    301 				sc->sc_flags |= I2C_ERROR;
    302 				goto out;
    303 			}
    304 #endif
    305 
    306 			if (sc->sc_resid == 0) {
    307 				x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
    308 				ki2c_writereg(sc, CONTROL, x);
    309 			} else {
    310 				ki2c_writereg(sc, DATA, *sc->sc_data++);
    311 				sc->sc_resid--;
    312 			}
    313 		}
    314 	}
    315 
    316 out:
    317 	if (isr & I2C_INT_STOP) {
    318 		ki2c_writereg(sc, CONTROL, 0);
    319 		sc->sc_flags &= ~I2C_BUSY;
    320 	}
    321 
    322 	ki2c_writereg(sc, ISR, isr);
    323 
    324 	return 1;
    325 }
    326 
    327 int
    328 ki2c_poll(sc, timo)
    329 	struct ki2c_softc *sc;
    330 	int timo;
    331 {
    332 	while (sc->sc_flags & I2C_BUSY) {
    333 		if (ki2c_readreg(sc, ISR))
    334 			ki2c_intr(sc);
    335 		timo -= 100;
    336 		if (timo < 0) {
    337 			printf("i2c_poll: timeout\n");
    338 			return -1;
    339 		}
    340 		delay(100);
    341 	}
    342 	return 0;
    343 }
    344 
    345 int
    346 ki2c_start(sc, addr, subaddr, data, len)
    347 	struct ki2c_softc *sc;
    348 	int addr, subaddr, len;
    349 	void *data;
    350 {
    351 	int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
    352 	int timo, x;
    353 
    354 	KASSERT((addr & 1) == 0);
    355 
    356 	sc->sc_data = data;
    357 	sc->sc_resid = len;
    358 	sc->sc_flags |= I2C_BUSY;
    359 
    360 	timo = 1000 + len * 200;
    361 
    362 	/* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
    363 	/* if (addr == 0x68) */
    364 		timo += 100000;
    365 
    366 	ki2c_writereg(sc, ADDR, addr | rw);
    367 	ki2c_writereg(sc, SUBADDR, subaddr);
    368 
    369 	x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
    370 	ki2c_writereg(sc, CONTROL, x);
    371 
    372 	if (ki2c_poll(sc, timo))
    373 		return -1;
    374 	if (sc->sc_flags & I2C_ERROR) {
    375 		printf("I2C_ERROR\n");
    376 		return -1;
    377 	}
    378 	return 0;
    379 }
    380 
    381 int
    382 ki2c_read(sc, addr, subaddr, data, len)
    383 	struct ki2c_softc *sc;
    384 	int addr, subaddr, len;
    385 	void *data;
    386 {
    387 	sc->sc_flags = I2C_READING;
    388 	#ifdef KI2C_DEBUG
    389 		printf("ki2c_read: %02x %d\n", addr, len);
    390 	#endif
    391 	return ki2c_start(sc, addr, subaddr, data, len);
    392 }
    393 
    394 int
    395 ki2c_write(sc, addr, subaddr, data, len)
    396 	struct ki2c_softc *sc;
    397 	int addr, subaddr, len;
    398 	void *data;
    399 {
    400 	sc->sc_flags = 0;
    401 	#ifdef KI2C_DEBUG
    402 		printf("ki2c_write: %02x %d\n",addr,len);
    403 	#endif
    404 	return ki2c_start(sc, addr, subaddr, data, len);
    405 }
    406 
    407 static int
    408 ki2c_i2c_acquire_bus(void *cookie, int flags)
    409 {
    410 	struct ki2c_softc *sc = cookie;
    411 
    412 	return (lockmgr(&sc->sc_buslock, LK_EXCLUSIVE, NULL));
    413 }
    414 
    415 static void
    416 ki2c_i2c_release_bus(void *cookie, int flags)
    417 {
    418 	struct ki2c_softc *sc = cookie;
    419 
    420 	(void) lockmgr(&sc->sc_buslock, LK_RELEASE, NULL);
    421 }
    422 
    423 int
    424 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    425     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    426 {
    427 	struct ki2c_softc *sc = cookie;
    428 
    429 	/* we handle the subaddress stuff ourselves */
    430 	ki2c_setmode(sc, I2C_STDMODE);
    431 
    432 	if (ki2c_write(sc, addr, 0, __UNCONST(vcmd), cmdlen) !=0 )
    433 		return -1;
    434 
    435 	if (I2C_OP_READ_P(op)) {
    436 		if (ki2c_read(sc, addr, 0, vbuf, buflen) !=0 )
    437 			return -1;
    438 	}
    439 	return 0;
    440 }
    441