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