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