1 /* $NetBSD: ki2c.c,v 1.42 2025/09/21 13:56:36 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, 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, intrparent; 95 char compat[256]; 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 prop_array_add(cfg, dev); 237 prop_object_release(dev); 238 skip: 239 devs = OF_peer(devs); 240 } 241 } 242 243 cv_init(&sc->sc_todev, device_xname(self)); 244 mutex_init(&sc->sc_todevmtx, MUTEX_DEFAULT, IPL_NONE); 245 246 if(sc->sc_poll == 0) { 247 snprintf(intr_xname, sizeof(intr_xname), "%s intr", device_xname(self)); 248 intr_establish_xname(intr[0], (intr[1] & 1) ? IST_LEVEL : IST_EDGE, 249 IPL_BIO, ki2c_intr, sc, intr_xname); 250 251 ki2c_writereg(sc, IER, I2C_INT_DATA | I2C_INT_ADDR| I2C_INT_STOP); 252 } 253 254 /* fill in the i2c tag */ 255 iic_tag_init(&sc->sc_i2c); 256 sc->sc_i2c.ic_cookie = sc; 257 sc->sc_i2c.ic_exec = ki2c_i2c_exec; 258 259 iicbus_attach(sc->sc_dev, &sc->sc_i2c); 260 } 261 262 uint8_t 263 ki2c_readreg(struct ki2c_softc *sc, int reg) 264 { 265 266 return bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_regstep * reg); 267 } 268 269 void 270 ki2c_writereg(struct ki2c_softc *sc, int reg, uint8_t val) 271 { 272 273 bus_space_write_1(sc->sc_tag, sc->sc_bh, reg * sc->sc_regstep, val); 274 delay(10); 275 } 276 277 u_int 278 ki2c_getmode(struct ki2c_softc *sc) 279 { 280 return ki2c_readreg(sc, MODE) & I2C_MODE; 281 } 282 283 void 284 ki2c_setmode(struct ki2c_softc *sc, u_int mode) 285 { 286 ki2c_writereg(sc, MODE, mode); 287 } 288 289 u_int 290 ki2c_getspeed(struct ki2c_softc *sc) 291 { 292 return ki2c_readreg(sc, MODE) & I2C_SPEED; 293 } 294 295 void 296 ki2c_setspeed(struct ki2c_softc *sc, u_int speed) 297 { 298 u_int x; 299 300 KASSERT((speed & ~I2C_SPEED) == 0); 301 x = ki2c_readreg(sc, MODE); 302 x &= ~I2C_SPEED; 303 x |= speed; 304 ki2c_writereg(sc, MODE, x); 305 } 306 307 int 308 ki2c_intr(void *cookie) 309 { 310 struct ki2c_softc *sc = cookie; 311 u_int isr, x; 312 isr = ki2c_readreg(sc, ISR); 313 if (isr & I2C_INT_ADDR) { 314 #if 0 315 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) { 316 /* No slave responded. */ 317 sc->sc_flags |= I2C_ERROR; 318 goto out; 319 } 320 #endif 321 322 if (sc->sc_flags & I2C_READING) { 323 if (sc->sc_resid > 1) { 324 x = ki2c_readreg(sc, CONTROL); 325 x |= I2C_CT_AAK; 326 ki2c_writereg(sc, CONTROL, x); 327 } 328 } else { 329 ki2c_writereg(sc, DATA, *sc->sc_data++); 330 sc->sc_resid--; 331 } 332 } 333 334 if (isr & I2C_INT_DATA) { 335 if (sc->sc_flags & I2C_READING) { 336 *sc->sc_data++ = ki2c_readreg(sc, DATA); 337 sc->sc_resid--; 338 339 if (sc->sc_resid == 0) { /* Completed */ 340 ki2c_writereg(sc, CONTROL, 0); 341 goto out; 342 } 343 } else { 344 #if 0 345 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) { 346 /* No slave responded. */ 347 sc->sc_flags |= I2C_ERROR; 348 goto out; 349 } 350 #endif 351 352 if (sc->sc_resid == 0) { 353 x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP; 354 ki2c_writereg(sc, CONTROL, x); 355 } else { 356 ki2c_writereg(sc, DATA, *sc->sc_data++); 357 sc->sc_resid--; 358 } 359 } 360 } 361 362 out: 363 if (isr & I2C_INT_STOP) { 364 ki2c_writereg(sc, CONTROL, 0); 365 sc->sc_flags &= ~I2C_BUSY; 366 cv_signal(&sc->sc_todev); 367 } 368 369 ki2c_writereg(sc, ISR, isr); 370 371 return 1; 372 } 373 374 int 375 ki2c_poll(struct ki2c_softc *sc, int timo) 376 { 377 int bail = 0; 378 while (sc->sc_flags & I2C_BUSY) { 379 if ((cold) || (bail > 10) || (sc->sc_poll)) { 380 if (ki2c_readreg(sc, ISR)) 381 ki2c_intr(sc); 382 timo -= 10; 383 if (timo < 0) { 384 DPRINTF("i2c_poll: timeout\n"); 385 return -1; 386 } 387 delay(10); 388 } else { 389 mutex_enter(&sc->sc_todevmtx); 390 cv_timedwait_sig(&sc->sc_todev, &sc->sc_todevmtx, hz/10); 391 mutex_exit(&sc->sc_todevmtx); 392 bail++; 393 } 394 } 395 return 0; 396 } 397 398 int 399 ki2c_start(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len) 400 { 401 int rw = (sc->sc_flags & I2C_READING) ? 1 : 0; 402 int timo, x; 403 404 KASSERT((addr & 1) == 0); 405 406 sc->sc_data = data; 407 sc->sc_resid = len; 408 sc->sc_flags |= I2C_BUSY; 409 410 timo = 1000 + len * 200; 411 412 /* XXX TAS3001 sometimes takes 50ms to finish writing registers. */ 413 /* if (addr == 0x68) */ 414 timo += 100000; 415 416 ki2c_writereg(sc, ADDR, addr | rw); 417 ki2c_writereg(sc, SUBADDR, subaddr); 418 419 x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR; 420 ki2c_writereg(sc, CONTROL, x); 421 422 if (ki2c_poll(sc, timo)) 423 return -1; 424 if (sc->sc_flags & I2C_ERROR) { 425 DPRINTF("I2C_ERROR\n"); 426 return -1; 427 } 428 return 0; 429 } 430 431 int 432 ki2c_read(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len) 433 { 434 sc->sc_flags = I2C_READING; 435 DPRINTF("ki2c_read: %02x %d\n", addr, len); 436 return ki2c_start(sc, addr, subaddr, data, len); 437 } 438 439 int 440 ki2c_write(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len) 441 { 442 sc->sc_flags = 0; 443 DPRINTF("ki2c_write: %02x %d\n",addr,len); 444 return ki2c_start(sc, addr, subaddr, data, len); 445 } 446 447 int 448 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd, 449 size_t cmdlen, void *vbuf, size_t buflen, int flags) 450 { 451 struct ki2c_softc *sc = cookie; 452 int i; 453 size_t w_len; 454 uint8_t *wp; 455 uint8_t wrbuf[KI2C_EXEC_MAX_CMDLEN + KI2C_EXEC_MAX_CMDLEN]; 456 uint8_t channel; 457 458 /* 459 * We don't have any idea if the ki2c controller can execute 460 * i2c quick_{read,write} operations, so if someone tries one, 461 * return an error. 462 */ 463 if (cmdlen == 0 && buflen == 0) 464 return -1; 465 466 /* 467 * Transaction could be much larger now. Bail if it exceeds our 468 * small combining buffer, we don't expect such devices. 469 */ 470 if (cmdlen + buflen > sizeof(wrbuf)) 471 return -1; 472 473 channel = (addr & 0xf80) ? 0x10 : 0x00; 474 addr &= 0x7f; 475 476 477 /* we handle the subaddress stuff ourselves */ 478 ki2c_setmode(sc, channel | I2C_STDMODE); 479 ki2c_setspeed(sc, I2C_50kHz); 480 481 /* Write-buffer defaults to vcmd */ 482 wp = (uint8_t *)(__UNCONST(vcmd)); 483 w_len = cmdlen; 484 485 /* 486 * Concatenate vcmd and vbuf for write operations 487 * 488 * Drivers written specifically for ki2c might already do this, 489 * but "generic" i2c drivers still provide separate arguments 490 * for the cmd and buf parts of iic_smbus_write_{byte,word}. 491 */ 492 if (I2C_OP_WRITE_P(op) && buflen != 0) { 493 if (cmdlen == 0) { 494 wp = (uint8_t *)vbuf; 495 w_len = buflen; 496 } else { 497 KASSERT((cmdlen + buflen) <= sizeof(wrbuf)); 498 wp = (uint8_t *)(__UNCONST(vcmd)); 499 w_len = 0; 500 for (i = 0; i < cmdlen; i++) 501 wrbuf[w_len++] = *wp++; 502 wp = (uint8_t *)vbuf; 503 for (i = 0; i < buflen; i++) 504 wrbuf[w_len++] = *wp++; 505 wp = wrbuf; 506 } 507 } 508 509 if (w_len > 0) 510 if (ki2c_write(sc, addr << 1, 0, wp, w_len) !=0 ) 511 return -1; 512 513 if (I2C_OP_READ_P(op)) { 514 if (ki2c_read(sc, addr << 1, 0, vbuf, buflen) !=0 ) 515 return -1; 516 } 517 return 0; 518 } 519