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