Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: ki2c.c,v 1.41 2025/09/18 02:51:03 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 		devhandle_t child_devhandle;
    209 
    210 		devs = OF_child(i2cbus[channel]);
    211 		while (devs != 0) {
    212 			if (OF_getprop(devs, "name", name, 32) <= 0)
    213 				goto skip;
    214 			if (OF_getprop(devs, "compatible", compat, 256) <= 0) {
    215 				/* some i2c device nodes don't have 'compatible' */
    216 				memset(compat, 0, 256);
    217 				strncpy(compat, name, 256);
    218 			}
    219 			if (OF_getprop(devs, "reg", &addr, 4) <= 0)
    220 				if (OF_getprop(devs, "i2c-address", &addr, 4) <= 0)
    221 					goto skip;
    222 			addr |= channel << 8;
    223 			addr = addr >> 1;
    224 			DPRINTF("-> %s@%x\n", name, addr);
    225 			dev = prop_dictionary_create();
    226 			prop_dictionary_set_string(dev, "name", name);
    227 			data = prop_data_create_copy(compat, strlen(compat)+1);
    228 			prop_dictionary_set(dev, "compatible", data);
    229 			prop_object_release(data);
    230 			prop_dictionary_set_uint32(dev, "addr", addr);
    231 			child_devhandle =
    232 			    devhandle_from_of(devhandle_invalid(), devs);
    233 			prop_dictionary_set_data(dev, "devhandle",
    234 			    &child_devhandle, sizeof(child_devhandle));
    235 
    236 			/* look for location info for sensors */
    237 			devc = OF_child(devs);
    238 			if (devc == 0) {
    239 				/* old style name info */
    240 				uint32_t ids[4];
    241 				int len = OF_getprop(devs, "hwsensor-id", ids, 16);
    242 				int i = 0, idx = 0;
    243 				char buffer[256];
    244 				memset(buffer, 0, 256);
    245 				if (len <= 0) {
    246 					/* no info, fill in what we may know */
    247 					if ((strcmp(name, "temp-monitor") == 0) &&
    248 					    (strcmp(model, "RackMac1,2") == 0)) {
    249 						prop_dictionary_set_string(dev, "s00", "CASE");
    250 					}
    251 				} else {
    252 					OF_getprop(devs, "hwsensor-location", buffer, 256);
    253 					while (len > 0) {
    254 						reg = ids[i];
    255 						strcpy(descr, &buffer[idx]);
    256 						idx += strlen(descr) + 1;
    257 						DPRINTF("found '%s' at %02x\n", descr, reg);
    258 						snprintf(num, 7, "s%02x", i);
    259 						prop_dictionary_set_string(dev, num, descr);
    260 						i++;
    261 						len -= 4;
    262 					}
    263 				}
    264 			} else {
    265 				while (devc != 0) {
    266 					if (OF_getprop(devc, "reg", &reg, 4) < 4) goto nope;
    267 					if (OF_getprop(devc, "location", descr, 32) <= 0)
    268 						goto nope;
    269 					DPRINTF("found '%s' at %02x\n", descr, reg);
    270 					snprintf(num, 7, "s%02x", reg);
    271 					prop_dictionary_set_string(dev, num, descr);
    272 				nope:
    273 					devc = OF_peer(devc);
    274 				}
    275 			}
    276 
    277 			prop_array_add(cfg, dev);
    278 			prop_object_release(dev);
    279 		skip:
    280 			devs = OF_peer(devs);
    281 		}
    282 	}
    283 
    284 	cv_init(&sc->sc_todev, device_xname(self));
    285 	mutex_init(&sc->sc_todevmtx, MUTEX_DEFAULT, IPL_NONE);
    286 
    287 	if(sc->sc_poll == 0) {
    288 		snprintf(intr_xname, sizeof(intr_xname), "%s intr", device_xname(self));
    289 		intr_establish_xname(intr[0], (intr[1] & 1) ? IST_LEVEL : IST_EDGE,
    290 		    IPL_BIO, ki2c_intr, sc, intr_xname);
    291 
    292 		ki2c_writereg(sc, IER, I2C_INT_DATA | I2C_INT_ADDR| I2C_INT_STOP);
    293 	}
    294 
    295 	/* fill in the i2c tag */
    296 	iic_tag_init(&sc->sc_i2c);
    297 	sc->sc_i2c.ic_cookie = sc;
    298 	sc->sc_i2c.ic_exec = ki2c_i2c_exec;
    299 
    300 	iicbus_attach(sc->sc_dev, &sc->sc_i2c);
    301 }
    302 
    303 uint8_t
    304 ki2c_readreg(struct ki2c_softc *sc, int reg)
    305 {
    306 
    307 	return bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_regstep * reg);
    308 }
    309 
    310 void
    311 ki2c_writereg(struct ki2c_softc *sc, int reg, uint8_t val)
    312 {
    313 
    314 	bus_space_write_1(sc->sc_tag, sc->sc_bh, reg * sc->sc_regstep, val);
    315 	delay(10);
    316 }
    317 
    318 u_int
    319 ki2c_getmode(struct ki2c_softc *sc)
    320 {
    321 	return ki2c_readreg(sc, MODE) & I2C_MODE;
    322 }
    323 
    324 void
    325 ki2c_setmode(struct ki2c_softc *sc, u_int mode)
    326 {
    327 	ki2c_writereg(sc, MODE, mode);
    328 }
    329 
    330 u_int
    331 ki2c_getspeed(struct ki2c_softc *sc)
    332 {
    333 	return ki2c_readreg(sc, MODE) & I2C_SPEED;
    334 }
    335 
    336 void
    337 ki2c_setspeed(struct ki2c_softc *sc, u_int speed)
    338 {
    339 	u_int x;
    340 
    341 	KASSERT((speed & ~I2C_SPEED) == 0);
    342 	x = ki2c_readreg(sc, MODE);
    343 	x &= ~I2C_SPEED;
    344 	x |= speed;
    345 	ki2c_writereg(sc, MODE, x);
    346 }
    347 
    348 int
    349 ki2c_intr(void *cookie)
    350 {
    351 	struct ki2c_softc *sc = cookie;
    352 	u_int isr, x;
    353 	isr = ki2c_readreg(sc, ISR);
    354 	if (isr & I2C_INT_ADDR) {
    355 #if 0
    356 		if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    357 			/* No slave responded. */
    358 			sc->sc_flags |= I2C_ERROR;
    359 			goto out;
    360 		}
    361 #endif
    362 
    363 		if (sc->sc_flags & I2C_READING) {
    364 			if (sc->sc_resid > 1) {
    365 				x = ki2c_readreg(sc, CONTROL);
    366 				x |= I2C_CT_AAK;
    367 				ki2c_writereg(sc, CONTROL, x);
    368 			}
    369 		} else {
    370 			ki2c_writereg(sc, DATA, *sc->sc_data++);
    371 			sc->sc_resid--;
    372 		}
    373 	}
    374 
    375 	if (isr & I2C_INT_DATA) {
    376 		if (sc->sc_flags & I2C_READING) {
    377 			*sc->sc_data++ = ki2c_readreg(sc, DATA);
    378 			sc->sc_resid--;
    379 
    380 			if (sc->sc_resid == 0) {	/* Completed */
    381 				ki2c_writereg(sc, CONTROL, 0);
    382 				goto out;
    383 			}
    384 		} else {
    385 #if 0
    386 			if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    387 				/* No slave responded. */
    388 				sc->sc_flags |= I2C_ERROR;
    389 				goto out;
    390 			}
    391 #endif
    392 
    393 			if (sc->sc_resid == 0) {
    394 				x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
    395 				ki2c_writereg(sc, CONTROL, x);
    396 			} else {
    397 				ki2c_writereg(sc, DATA, *sc->sc_data++);
    398 				sc->sc_resid--;
    399 			}
    400 		}
    401 	}
    402 
    403 out:
    404 	if (isr & I2C_INT_STOP) {
    405 		ki2c_writereg(sc, CONTROL, 0);
    406 		sc->sc_flags &= ~I2C_BUSY;
    407 		cv_signal(&sc->sc_todev);
    408 	}
    409 
    410 	ki2c_writereg(sc, ISR, isr);
    411 
    412 	return 1;
    413 }
    414 
    415 int
    416 ki2c_poll(struct ki2c_softc *sc, int timo)
    417 {
    418 	int bail = 0;
    419 	while (sc->sc_flags & I2C_BUSY) {
    420 		if ((cold) || (bail > 10) || (sc->sc_poll)) {
    421 			if (ki2c_readreg(sc, ISR))
    422 				ki2c_intr(sc);
    423 			timo -= 10;
    424 			if (timo < 0) {
    425 				DPRINTF("i2c_poll: timeout\n");
    426 				return -1;
    427 			}
    428 			delay(10);
    429 		} else {
    430 			mutex_enter(&sc->sc_todevmtx);
    431 			cv_timedwait_sig(&sc->sc_todev, &sc->sc_todevmtx, hz/10);
    432 			mutex_exit(&sc->sc_todevmtx);
    433 			bail++;
    434 		}
    435 	}
    436 	return 0;
    437 }
    438 
    439 int
    440 ki2c_start(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    441 {
    442 	int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
    443 	int timo, x;
    444 
    445 	KASSERT((addr & 1) == 0);
    446 
    447 	sc->sc_data = data;
    448 	sc->sc_resid = len;
    449 	sc->sc_flags |= I2C_BUSY;
    450 
    451 	timo = 1000 + len * 200;
    452 
    453 	/* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
    454 	/* if (addr == 0x68) */
    455 		timo += 100000;
    456 
    457 	ki2c_writereg(sc, ADDR, addr | rw);
    458 	ki2c_writereg(sc, SUBADDR, subaddr);
    459 
    460 	x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
    461 	ki2c_writereg(sc, CONTROL, x);
    462 
    463 	if (ki2c_poll(sc, timo))
    464 		return -1;
    465 	if (sc->sc_flags & I2C_ERROR) {
    466 		DPRINTF("I2C_ERROR\n");
    467 		return -1;
    468 	}
    469 	return 0;
    470 }
    471 
    472 int
    473 ki2c_read(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    474 {
    475 	sc->sc_flags = I2C_READING;
    476 	DPRINTF("ki2c_read: %02x %d\n", addr, len);
    477 	return ki2c_start(sc, addr, subaddr, data, len);
    478 }
    479 
    480 int
    481 ki2c_write(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    482 {
    483 	sc->sc_flags = 0;
    484 	DPRINTF("ki2c_write: %02x %d\n",addr,len);
    485 	return ki2c_start(sc, addr, subaddr, data, len);
    486 }
    487 
    488 int
    489 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    490     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    491 {
    492 	struct ki2c_softc *sc = cookie;
    493 	int i;
    494 	size_t w_len;
    495 	uint8_t *wp;
    496 	uint8_t wrbuf[KI2C_EXEC_MAX_CMDLEN + KI2C_EXEC_MAX_CMDLEN];
    497 	uint8_t channel;
    498 
    499 	/*
    500 	 * We don't have any idea if the ki2c controller can execute
    501 	 * i2c quick_{read,write} operations, so if someone tries one,
    502 	 * return an error.
    503 	 */
    504 	if (cmdlen == 0 && buflen == 0)
    505 		return -1;
    506 
    507 	/*
    508 	 * Transaction could be much larger now. Bail if it exceeds our
    509 	 * small combining buffer, we don't expect such devices.
    510 	 */
    511 	if (cmdlen + buflen > sizeof(wrbuf))
    512 		return -1;
    513 
    514 	channel = (addr & 0xf80) ? 0x10 : 0x00;
    515 	addr &= 0x7f;
    516 
    517 
    518 	/* we handle the subaddress stuff ourselves */
    519 	ki2c_setmode(sc, channel | I2C_STDMODE);
    520 	ki2c_setspeed(sc, I2C_50kHz);
    521 
    522 	/* Write-buffer defaults to vcmd */
    523 	wp = (uint8_t *)(__UNCONST(vcmd));
    524 	w_len = cmdlen;
    525 
    526 	/*
    527 	 * Concatenate vcmd and vbuf for write operations
    528 	 *
    529 	 * Drivers written specifically for ki2c might already do this,
    530 	 * but "generic" i2c drivers still provide separate arguments
    531 	 * for the cmd and buf parts of iic_smbus_write_{byte,word}.
    532 	 */
    533 	if (I2C_OP_WRITE_P(op) && buflen != 0) {
    534 		if (cmdlen == 0) {
    535 			wp = (uint8_t *)vbuf;
    536 			w_len = buflen;
    537 		} else {
    538 			KASSERT((cmdlen + buflen) <= sizeof(wrbuf));
    539 			wp = (uint8_t *)(__UNCONST(vcmd));
    540 			w_len = 0;
    541 			for (i = 0; i < cmdlen; i++)
    542 				wrbuf[w_len++] = *wp++;
    543 			wp = (uint8_t *)vbuf;
    544 			for (i = 0; i < buflen; i++)
    545 				wrbuf[w_len++] = *wp++;
    546 			wp = wrbuf;
    547 		}
    548 	}
    549 
    550 	if (w_len > 0)
    551 		if (ki2c_write(sc, addr << 1, 0, wp, w_len) !=0 )
    552 			return -1;
    553 
    554 	if (I2C_OP_READ_P(op)) {
    555 		if (ki2c_read(sc, addr << 1, 0, vbuf, buflen) !=0 )
    556 			return -1;
    557 	}
    558 	return 0;
    559 }
    560