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