1 /*- 2 * Copyright (c) 1998 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 * $FreeBSD: src/sys/dev/iicbus/iicbb.c,v 1.6.2.2 2002/04/19 05:52:12 nsouch Exp $ 27 * $DragonFly: src/sys/bus/iicbus/iicbb.c,v 1.5 2006/12/22 23:12:16 swildner Exp $ 28 * 29 */ 30 31 /* 32 * Generic I2C bit-banging code 33 * 34 * Example: 35 * 36 * iicbus 37 * / \ 38 * iicbb pcf 39 * | \ 40 * bti2c lpbb 41 * 42 * From Linux I2C generic interface 43 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de> 44 * 45 * TODO: port Peter's generic bit-banging code <dufault@hda.com> 46 */ 47 48 #include <sys/param.h> 49 #include <sys/kernel.h> 50 #include <sys/systm.h> 51 #include <sys/module.h> 52 #include <sys/bus.h> 53 #include <sys/conf.h> 54 #include <sys/buf.h> 55 #include <sys/uio.h> 56 #include <sys/malloc.h> 57 58 #include <machine/clock.h> 59 60 #include "iiconf.h" 61 #include "iicbus.h" 62 63 #include <bus/smbus/smbconf.h> 64 65 #include "iicbus_if.h" 66 #include "iicbb_if.h" 67 68 struct iicbb_softc { 69 device_t iicbus; 70 }; 71 72 static int iicbb_probe(device_t); 73 static int iicbb_attach(device_t); 74 static int iicbb_detach(device_t); 75 static int iicbb_print_child(device_t, device_t); 76 77 static int iicbb_callback(device_t, int, caddr_t); 78 static int iicbb_start(device_t, u_char, int); 79 static int iicbb_stop(device_t); 80 static int iicbb_write(device_t, char *, int, int *, int); 81 static int iicbb_read(device_t, char *, int, int *, int, int); 82 static int iicbb_reset(device_t, u_char, u_char, u_char *); 83 84 static device_method_t iicbb_methods[] = { 85 /* device interface */ 86 DEVMETHOD(device_probe, iicbb_probe), 87 DEVMETHOD(device_attach, iicbb_attach), 88 DEVMETHOD(device_detach, iicbb_detach), 89 90 /* bus interface */ 91 DEVMETHOD(bus_print_child, iicbb_print_child), 92 93 /* iicbus interface */ 94 DEVMETHOD(iicbus_callback, iicbb_callback), 95 DEVMETHOD(iicbus_start, iicbb_start), 96 DEVMETHOD(iicbus_repeated_start, iicbb_start), 97 DEVMETHOD(iicbus_stop, iicbb_stop), 98 DEVMETHOD(iicbus_write, iicbb_write), 99 DEVMETHOD(iicbus_read, iicbb_read), 100 DEVMETHOD(iicbus_reset, iicbb_reset), 101 102 { 0, 0 } 103 }; 104 105 static driver_t iicbb_driver = { 106 "iicbb", 107 iicbb_methods, 108 sizeof(struct iicbb_softc), 109 }; 110 111 static devclass_t iicbb_devclass; 112 113 static int 114 iicbb_probe(device_t dev) 115 { 116 device_set_desc(dev, "I2C bit-banging driver"); 117 118 return (0); 119 } 120 121 static int 122 iicbb_attach(device_t dev) 123 { 124 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 125 126 bzero(sc, sizeof(struct iicbb_softc)); 127 128 sc->iicbus = device_add_child(dev, "iicbus", -1); 129 130 if (!sc->iicbus) 131 return (ENXIO); 132 133 bus_generic_attach(dev); 134 135 return (0); 136 } 137 138 static int 139 iicbb_detach(device_t dev) 140 { 141 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 142 143 if (sc->iicbus) { 144 bus_generic_detach(dev); 145 device_delete_child(dev, sc->iicbus); 146 } 147 148 return (0); 149 } 150 151 static int 152 iicbb_print_child(device_t bus, device_t dev) 153 { 154 int error; 155 int retval = 0; 156 u_char oldaddr; 157 158 retval += bus_print_child_header(bus, dev); 159 /* retrieve the interface I2C address */ 160 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr); 161 if (error == IIC_ENOADDR) { 162 retval += kprintf(" on %s master-only\n", 163 device_get_nameunit(bus)); 164 } else { 165 /* restore the address */ 166 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL); 167 168 retval += kprintf(" on %s addr 0x%x\n", 169 device_get_nameunit(bus), oldaddr & 0xff); 170 } 171 172 return (retval); 173 } 174 175 #define I2C_SET(dev,ctrl,data) \ 176 IICBB_SETLINES(device_get_parent(dev), ctrl, data) 177 178 #define I2C_GET(dev) (IICBB_GETDATALINE(device_get_parent(dev))) 179 180 static int i2c_debug = 0; 181 #define I2C_DEBUG(x) if (i2c_debug) (x) 182 183 static void 184 iicbb_one(device_t dev) 185 { 186 I2C_SET(dev,0,1); 187 I2C_SET(dev,1,1); 188 I2C_SET(dev,0,1); 189 return; 190 } 191 192 static void 193 iicbb_zero(device_t dev) 194 { 195 I2C_SET(dev,0,0); 196 I2C_SET(dev,1,0); 197 I2C_SET(dev,0,0); 198 return; 199 } 200 201 /* 202 * Waiting for ACKNOWLEDGE. 203 * 204 * When a chip is being addressed or has received data it will issue an 205 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line 206 * (set it to high level) and then release the CLOCK line. 207 * Now it must wait for the SLAVE to pull the DATA line low. 208 * Actually on the bus this looks like a START condition so nothing happens 209 * because of the fact that the IC's that have not been addressed are doing 210 * nothing. 211 * 212 * When the SLAVE has pulled this line low the MASTER will take the CLOCK 213 * line low and then the SLAVE will release the SDA (data) line. 214 */ 215 static int 216 iicbb_ack(device_t dev, int timeout) 217 { 218 int noack; 219 int k = timeout/10; 220 221 I2C_SET(dev,0,1); 222 I2C_SET(dev,1,1); 223 224 do { 225 noack = I2C_GET(dev); 226 if (!noack) 227 break; 228 DELAY(10); /* XXX wait 10us */ 229 } while (k--); 230 231 I2C_SET(dev,0,1); 232 I2C_DEBUG(kprintf("%c ",noack?'-':'+')); 233 234 return (noack); 235 } 236 237 static void 238 iicbb_sendbyte(device_t dev, u_char data) 239 { 240 int i; 241 242 I2C_SET(dev,0,0); 243 for (i=7; i>=0; i--) 244 (data&(1<<i)) ? iicbb_one(dev) : iicbb_zero(dev); 245 I2C_DEBUG(kprintf("w%02x",(int)data)); 246 return; 247 } 248 249 static u_char 250 iicbb_readbyte(device_t dev, int last) 251 { 252 int i; 253 unsigned char data=0; 254 255 I2C_SET(dev,0,1); 256 for (i=7; i>=0; i--) 257 { 258 I2C_SET(dev,1,1); 259 if (I2C_GET(dev)) 260 data |= (1<<i); 261 I2C_SET(dev,0,1); 262 } 263 last ? iicbb_one(dev) : iicbb_zero(dev); 264 I2C_DEBUG(kprintf("r%02x%c ",(int)data,last?'-':'+')); 265 return data; 266 } 267 268 static int 269 iicbb_callback(device_t dev, int index, caddr_t data) 270 { 271 return (IICBB_CALLBACK(device_get_parent(dev), index, data)); 272 } 273 274 static int 275 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) 276 { 277 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr)); 278 } 279 280 static int 281 iicbb_start(device_t dev, u_char slave, int timeout) 282 { 283 int error; 284 285 I2C_DEBUG(kprintf("<")); 286 287 I2C_SET(dev,0,1); 288 I2C_SET(dev,1,1); 289 I2C_SET(dev,1,0); 290 I2C_SET(dev,0,0); 291 292 /* send address */ 293 iicbb_sendbyte(dev, slave); 294 295 /* check for ack */ 296 if (iicbb_ack(dev, timeout)) { 297 error = IIC_ENOACK; 298 goto error; 299 } 300 301 return(0); 302 303 error: 304 iicbb_stop(dev); 305 return (error); 306 } 307 308 static int 309 iicbb_stop(device_t dev) 310 { 311 I2C_SET(dev,0,0); 312 I2C_SET(dev,1,0); 313 I2C_SET(dev,1,1); 314 I2C_DEBUG(kprintf(">")); 315 return (0); 316 } 317 318 static int 319 iicbb_write(device_t dev, char * buf, int len, int *sent, int timeout) 320 { 321 int bytes, error = 0; 322 323 bytes = 0; 324 while (len) { 325 /* send byte */ 326 iicbb_sendbyte(dev,(u_char)*buf++); 327 328 /* check for ack */ 329 if (iicbb_ack(dev, timeout)) { 330 error = IIC_ENOACK; 331 goto error; 332 } 333 bytes ++; 334 len --; 335 } 336 337 error: 338 *sent = bytes; 339 return (error); 340 } 341 342 static int 343 iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay) 344 { 345 int bytes; 346 347 bytes = 0; 348 while (len) { 349 /* XXX should insert delay here */ 350 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0); 351 352 bytes ++; 353 len --; 354 } 355 356 *read = bytes; 357 return (0); 358 } 359 360 DRIVER_MODULE(iicbb, bti2c, iicbb_driver, iicbb_devclass, 0, 0); 361 DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0); 362 DRIVER_MODULE(iicbb, viapm, iicbb_driver, iicbb_devclass, 0, 0); 363