1 /*- 2 * Copyright (c) 1998, 2001 Nicolas Souchu 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * Generic I2C bit-banging code 32 * 33 * Example: 34 * 35 * iicbus 36 * / \ 37 * iicbb pcf 38 * | \ 39 * bti2c lpbb 40 * 41 * From Linux I2C generic interface 42 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de> 43 * 44 */ 45 46 #include <sys/param.h> 47 #include <sys/kernel.h> 48 #include <sys/systm.h> 49 #include <sys/module.h> 50 #include <sys/bus.h> 51 #include <sys/uio.h> 52 53 54 #include <dev/iicbus/iiconf.h> 55 #include <dev/iicbus/iicbus.h> 56 57 #include <dev/smbus/smbconf.h> 58 59 #include "iicbus_if.h" 60 #include "iicbb_if.h" 61 62 struct iicbb_softc { 63 device_t iicbus; 64 }; 65 66 static int iicbb_attach(device_t); 67 static void iicbb_child_detached(device_t, device_t); 68 static int iicbb_detach(device_t); 69 static int iicbb_print_child(device_t, device_t); 70 static int iicbb_probe(device_t); 71 72 static int iicbb_callback(device_t, int, caddr_t); 73 static int iicbb_start(device_t, u_char, int); 74 static int iicbb_stop(device_t); 75 static int iicbb_write(device_t, char *, int, int *, int); 76 static int iicbb_read(device_t, char *, int, int *, int, int); 77 static int iicbb_reset(device_t, u_char, u_char, u_char *); 78 79 static device_method_t iicbb_methods[] = { 80 /* device interface */ 81 DEVMETHOD(device_probe, iicbb_probe), 82 DEVMETHOD(device_attach, iicbb_attach), 83 DEVMETHOD(device_detach, iicbb_detach), 84 85 /* bus interface */ 86 DEVMETHOD(bus_child_detached, iicbb_child_detached), 87 DEVMETHOD(bus_print_child, iicbb_print_child), 88 89 /* iicbus interface */ 90 DEVMETHOD(iicbus_callback, iicbb_callback), 91 DEVMETHOD(iicbus_start, iicbb_start), 92 DEVMETHOD(iicbus_repeated_start, iicbb_start), 93 DEVMETHOD(iicbus_stop, iicbb_stop), 94 DEVMETHOD(iicbus_write, iicbb_write), 95 DEVMETHOD(iicbus_read, iicbb_read), 96 DEVMETHOD(iicbus_reset, iicbb_reset), 97 98 { 0, 0 } 99 }; 100 101 driver_t iicbb_driver = { 102 "iicbb", 103 iicbb_methods, 104 sizeof(struct iicbb_softc), 105 }; 106 107 devclass_t iicbb_devclass; 108 109 static int 110 iicbb_probe(device_t dev) 111 { 112 device_set_desc(dev, "I2C bit-banging driver"); 113 114 return (0); 115 } 116 117 static int 118 iicbb_attach(device_t dev) 119 { 120 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 121 122 sc->iicbus = device_add_child(dev, "iicbus", -1); 123 if (!sc->iicbus) 124 return (ENXIO); 125 bus_generic_attach(dev); 126 127 return (0); 128 } 129 130 static int 131 iicbb_detach(device_t dev) 132 { 133 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 134 device_t child; 135 136 /* 137 * We need to save child because the detach indirectly causes 138 * sc->iicbus to be zeroed. Since we added the device 139 * unconditionally in iicbb_attach, we need to make sure we 140 * delete it here. See iicbb_child_detached. We need that 141 * callback in case newbus detached our children w/o detaching 142 * us (say iicbus is a module and unloaded w/o iicbb being 143 * unloaded). 144 */ 145 child = sc->iicbus; 146 bus_generic_detach(dev); 147 if (child) 148 device_delete_child(dev, child); 149 150 return (0); 151 } 152 153 static void 154 iicbb_child_detached( device_t dev, device_t child ) 155 { 156 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 157 158 if (child == sc->iicbus) 159 sc->iicbus = NULL; 160 } 161 162 static int 163 iicbb_print_child(device_t bus, device_t dev) 164 { 165 int error; 166 int retval = 0; 167 u_char oldaddr; 168 169 retval += bus_print_child_header(bus, dev); 170 /* retrieve the interface I2C address */ 171 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr); 172 if (error == IIC_ENOADDR) { 173 retval += printf(" on %s master-only\n", 174 device_get_nameunit(bus)); 175 } else { 176 /* restore the address */ 177 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL); 178 179 retval += printf(" on %s addr 0x%x\n", 180 device_get_nameunit(bus), oldaddr & 0xff); 181 } 182 183 return (retval); 184 } 185 186 #define IIC_DELAY 10 187 188 #define I2C_SETSDA(dev,val) do { \ 189 IICBB_SETSDA(device_get_parent(dev), val); \ 190 DELAY(IIC_DELAY); \ 191 } while (0) 192 193 #define I2C_SETSCL(dev,val) do { \ 194 iicbb_setscl(dev, val, 100); \ 195 } while (0) 196 197 #define I2C_SET(dev,ctrl,data) do { \ 198 I2C_SETSCL(dev, ctrl); \ 199 I2C_SETSDA(dev, data); \ 200 } while (0) 201 202 #define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev))) 203 204 #define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev))) 205 206 static int i2c_debug = 0; 207 #define I2C_DEBUG(x) do { \ 208 if (i2c_debug) (x); \ 209 } while (0) 210 211 #define I2C_LOG(format,args...) do { \ 212 printf(format, args); \ 213 } while (0) 214 215 static void 216 iicbb_setscl(device_t dev, int val, int timeout) 217 { 218 int k = 0; 219 220 IICBB_SETSCL(device_get_parent(dev), val); 221 DELAY(IIC_DELAY); 222 223 while (val && !I2C_GETSCL(dev) && k++ < timeout) { 224 IICBB_SETSCL(device_get_parent(dev), val); 225 DELAY(IIC_DELAY); 226 } 227 228 return; 229 } 230 231 static void 232 iicbb_one(device_t dev, int timeout) 233 { 234 I2C_SET(dev,0,1); 235 I2C_SET(dev,1,1); 236 I2C_SET(dev,0,1); 237 return; 238 } 239 240 static void 241 iicbb_zero(device_t dev, int timeout) 242 { 243 I2C_SET(dev,0,0); 244 I2C_SET(dev,1,0); 245 I2C_SET(dev,0,0); 246 return; 247 } 248 249 /* 250 * Waiting for ACKNOWLEDGE. 251 * 252 * When a chip is being addressed or has received data it will issue an 253 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line 254 * (set it to high level) and then release the CLOCK line. 255 * Now it must wait for the SLAVE to pull the DATA line low. 256 * Actually on the bus this looks like a START condition so nothing happens 257 * because of the fact that the IC's that have not been addressed are doing 258 * nothing. 259 * 260 * When the SLAVE has pulled this line low the MASTER will take the CLOCK 261 * line low and then the SLAVE will release the SDA (data) line. 262 */ 263 static int 264 iicbb_ack(device_t dev, int timeout) 265 { 266 int noack; 267 int k = 0; 268 269 I2C_SET(dev,0,1); 270 I2C_SET(dev,1,1); 271 do { 272 noack = I2C_GETSDA(dev); 273 if (!noack) 274 break; 275 DELAY(10); 276 k += 10; 277 } while (k < timeout); 278 279 I2C_SET(dev,0,1); 280 I2C_DEBUG(printf("%c ",noack?'-':'+')); 281 282 return (noack); 283 } 284 285 static void 286 iicbb_sendbyte(device_t dev, u_char data, int timeout) 287 { 288 int i; 289 290 for (i=7; i>=0; i--) { 291 if (data&(1<<i)) { 292 iicbb_one(dev, timeout); 293 } else { 294 iicbb_zero(dev, timeout); 295 } 296 } 297 I2C_DEBUG(printf("w%02x",(int)data)); 298 return; 299 } 300 301 static u_char 302 iicbb_readbyte(device_t dev, int last, int timeout) 303 { 304 int i; 305 unsigned char data=0; 306 307 I2C_SET(dev,0,1); 308 for (i=7; i>=0; i--) 309 { 310 I2C_SET(dev,1,1); 311 if (I2C_GETSDA(dev)) 312 data |= (1<<i); 313 I2C_SET(dev,0,1); 314 } 315 if (last) { 316 iicbb_one(dev, timeout); 317 } else { 318 iicbb_zero(dev, timeout); 319 } 320 I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+')); 321 return data; 322 } 323 324 static int 325 iicbb_callback(device_t dev, int index, caddr_t data) 326 { 327 return (IICBB_CALLBACK(device_get_parent(dev), index, data)); 328 } 329 330 static int 331 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) 332 { 333 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr)); 334 } 335 336 static int 337 iicbb_start(device_t dev, u_char slave, int timeout) 338 { 339 int error; 340 341 I2C_DEBUG(printf("<")); 342 343 I2C_SET(dev,1,1); 344 I2C_SET(dev,1,0); 345 I2C_SET(dev,0,0); 346 347 /* send address */ 348 iicbb_sendbyte(dev, slave, timeout); 349 350 /* check for ack */ 351 if (iicbb_ack(dev, timeout)) { 352 error = IIC_ENOACK; 353 goto error; 354 } 355 356 return(0); 357 358 error: 359 iicbb_stop(dev); 360 return (error); 361 } 362 363 static int 364 iicbb_stop(device_t dev) 365 { 366 I2C_SET(dev,0,0); 367 I2C_SET(dev,1,0); 368 I2C_SET(dev,1,1); 369 I2C_DEBUG(printf(">")); 370 return (0); 371 } 372 373 static int 374 iicbb_write(device_t dev, char * buf, int len, int *sent, int timeout) 375 { 376 int bytes, error = 0; 377 378 bytes = 0; 379 while (len) { 380 /* send byte */ 381 iicbb_sendbyte(dev,(u_char)*buf++, timeout); 382 383 /* check for ack */ 384 if (iicbb_ack(dev, timeout)) { 385 error = IIC_ENOACK; 386 goto error; 387 } 388 bytes ++; 389 len --; 390 } 391 392 error: 393 *sent = bytes; 394 return (error); 395 } 396 397 static int 398 iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay) 399 { 400 int bytes; 401 402 bytes = 0; 403 while (len) { 404 /* XXX should insert delay here */ 405 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay); 406 407 bytes ++; 408 len --; 409 } 410 411 *read = bytes; 412 return (0); 413 } 414 415 /* 416 * XXX This is lame. We need to have a base iicbb_bridge class that all these 417 * XXX derive from. 418 */ 419 DRIVER_MODULE(iicbb, bktr, iicbb_driver, iicbb_devclass, 0, 0); 420 DRIVER_MODULE(iicbb, ixpiic, iicbb_driver, iicbb_devclass, 0, 0); 421 DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0); 422 DRIVER_MODULE(iicbb, viapm, iicbb_driver, iicbb_devclass, 0, 0); 423 DRIVER_MODULE(iicbb, cxm_iic, iicbb_driver, iicbb_devclass, 0, 0); 424 425 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 426 MODULE_VERSION(iicbb, IICBB_MODVER); 427