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