1 /*- 2 * Copyright (c) 1998 Nicolas Souchu, Marc Bouget 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 * $Id$ 27 * 28 */ 29 30 /* 31 * I2C Bit-Banging over parallel port 32 * 33 * See the Official Philips interface description in lpbb(4) 34 */ 35 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 #include <sys/systm.h> 39 #include <sys/module.h> 40 #include <sys/bus.h> 41 #include <sys/conf.h> 42 #include <sys/buf.h> 43 #include <sys/uio.h> 44 #include <sys/malloc.h> 45 46 #include <machine/clock.h> 47 48 #include <dev/ppbus/ppbconf.h> 49 50 #include <dev/iicbus/iiconf.h> 51 #include <dev/iicbus/iicbus.h> 52 53 #include "iicbb_if.h" 54 55 /* iicbus softc */ 56 struct lpbb_softc { 57 58 struct ppb_device lpbb_dev; 59 }; 60 61 static int lpbb_detect(struct lpbb_softc *); 62 63 static int lpbb_probe(device_t); 64 static int lpbb_attach(device_t); 65 static void lpbb_print_child(device_t, device_t); 66 67 static int lpbb_callback(device_t, int, caddr_t *); 68 static void lpbb_setlines(device_t, int, int); 69 static int lpbb_getdataline(device_t); 70 static int lpbb_reset(device_t, u_char, u_char, u_char *); 71 72 static devclass_t lpbb_devclass; 73 74 static device_method_t lpbb_methods[] = { 75 /* device interface */ 76 DEVMETHOD(device_probe, lpbb_probe), 77 DEVMETHOD(device_attach, lpbb_attach), 78 79 /* bus interface */ 80 DEVMETHOD(bus_print_child, lpbb_print_child), 81 82 /* iicbb interface */ 83 DEVMETHOD(iicbb_callback, lpbb_callback), 84 DEVMETHOD(iicbb_setlines, lpbb_setlines), 85 DEVMETHOD(iicbb_getdataline, lpbb_getdataline), 86 DEVMETHOD(iicbb_reset, lpbb_reset), 87 88 { 0, 0 } 89 }; 90 91 static driver_t lpbb_driver = { 92 "lpbb", 93 lpbb_methods, 94 DRIVER_TYPE_MISC, 95 sizeof(struct lpbb_softc), 96 }; 97 98 /* 99 * Make ourselves visible as a ppbus driver 100 */ 101 static struct ppb_device *lpbb_ppb_probe(struct ppb_data *ppb); 102 static int lpbb_ppb_attach(struct ppb_device *dev); 103 104 #define MAXLPBB 8 /* XXX not much better! */ 105 static struct lpbb_softc *lpbbdata[MAXLPBB]; 106 static int nlpbb = 0; 107 108 #ifdef KERNEL 109 110 static struct ppb_driver lpbbdriver = { 111 lpbb_ppb_probe, lpbb_ppb_attach, "lpbb" 112 }; 113 DATA_SET(ppbdriver_set, lpbbdriver); 114 115 #endif /* KERNEL */ 116 117 static int 118 lpbb_probe(device_t dev) 119 { 120 struct lpbb_softc *sc = lpbbdata[device_get_unit(dev)]; 121 struct lpbb_softc *scdst = (struct lpbb_softc *)device_get_softc(dev); 122 123 /* XXX copy softc. Yet, ppbus device is sc->lpbb_dev, but will be 124 * dev->parent when ppbus will be ported to the new bus architecture */ 125 bcopy(sc, scdst, sizeof(struct lpbb_softc)); 126 127 device_set_desc(dev, "parallel I2C bit-banging interface"); 128 129 /* probe done by ppbus initialization */ 130 return (0); 131 } 132 133 static int 134 lpbb_attach(device_t dev) 135 { 136 struct lpbb_softc *sc = (struct lpbb_softc *)device_get_softc(dev); 137 device_t bitbang, iicbus; 138 139 /* add generic bit-banging code */ 140 bitbang = device_add_child(dev, "iicbb", -1, NULL); 141 142 /* add the iicbus to the tree */ 143 iicbus = iicbus_alloc_bus(bitbang); 144 145 device_probe_and_attach(bitbang); 146 147 /* XXX should be in iicbb_attach! */ 148 device_probe_and_attach(iicbus); 149 150 return (0); 151 } 152 153 /* 154 * lppbb_ppb_probe() 155 */ 156 static struct ppb_device * 157 lpbb_ppb_probe(struct ppb_data *ppb) 158 { 159 struct lpbb_softc *sc; 160 161 sc = (struct lpbb_softc *) malloc(sizeof(struct lpbb_softc), 162 M_TEMP, M_NOWAIT); 163 if (!sc) { 164 printf("lpbb: cannot malloc!\n"); 165 return (0); 166 } 167 bzero(sc, sizeof(struct lpbb_softc)); 168 169 lpbbdata[nlpbb] = sc; 170 171 /* 172 * ppbus dependent initialisation. 173 */ 174 sc->lpbb_dev.id_unit = nlpbb; 175 sc->lpbb_dev.name = lpbbdriver.name; 176 sc->lpbb_dev.ppb = ppb; 177 sc->lpbb_dev.intr = 0; 178 179 if (!lpbb_detect(sc)) { 180 free(sc, M_TEMP); 181 return (NULL); 182 } 183 184 /* Ok, go to next device on next probe */ 185 nlpbb ++; 186 187 /* XXX wrong according to new bus architecture. ppbus needs to be 188 * ported 189 */ 190 return (&sc->lpbb_dev); 191 } 192 193 static int 194 lpbb_ppb_attach(struct ppb_device *dev) 195 { 196 /* add the parallel port I2C interface to the bus tree */ 197 if (!device_add_child(root_bus, "lpbb", dev->id_unit, NULL)) 198 return (0); 199 200 return (1); 201 } 202 203 static int 204 lpbb_callback(device_t dev, int index, caddr_t *data) 205 { 206 struct lpbb_softc *sc = (struct lpbb_softc *)device_get_softc(dev); 207 int error = 0; 208 int how; 209 210 switch (index) { 211 case IIC_REQUEST_BUS: 212 /* request the ppbus */ 213 how = *(int *)data; 214 error = ppb_request_bus(&sc->lpbb_dev, how); 215 break; 216 217 case IIC_RELEASE_BUS: 218 /* release the ppbus */ 219 error = ppb_release_bus(&sc->lpbb_dev); 220 break; 221 222 default: 223 error = EINVAL; 224 } 225 226 return (error); 227 } 228 229 #define SDA_out 0x80 230 #define SCL_out 0x08 231 #define SDA_in 0x80 232 #define SCL_in 0x08 233 #define ALIM 0x20 234 #define I2CKEY 0x50 235 236 static int getSDA(struct lpbb_softc *sc) 237 { 238 if((ppb_rstr(&sc->lpbb_dev)&SDA_in)==SDA_in) 239 return 1; 240 else 241 return 0; 242 } 243 244 static int getSCL(struct lpbb_softc *sc) 245 { 246 if((ppb_rstr(&sc->lpbb_dev)&SCL_in)==SCL_in) 247 return 1; 248 else 249 return 0; 250 } 251 252 static void setSDA(struct lpbb_softc *sc, char val) 253 { 254 if(val==0) 255 ppb_wdtr(&sc->lpbb_dev, (u_char)SDA_out); 256 else 257 ppb_wdtr(&sc->lpbb_dev, (u_char)~SDA_out); 258 } 259 260 static void setSCL(struct lpbb_softc *sc, unsigned char val) 261 { 262 if(val==0) 263 ppb_wctr(&sc->lpbb_dev, (u_char)(ppb_rctr(&sc->lpbb_dev)&~SCL_out)); 264 else 265 ppb_wctr(&sc->lpbb_dev, (u_char)(ppb_rctr(&sc->lpbb_dev)|SCL_out)); 266 } 267 268 static int lpbb_detect(struct lpbb_softc *sc) 269 { 270 if (ppb_request_bus(&sc->lpbb_dev, PPB_DONTWAIT)) { 271 printf("lpbb: can't allocate ppbus\n"); 272 return (0); 273 } 274 275 /* reset bus */ 276 setSDA(sc, 1); 277 setSCL(sc, 1); 278 279 if ((ppb_rstr(&sc->lpbb_dev) & I2CKEY) || 280 ((ppb_rstr(&sc->lpbb_dev) & ALIM) != ALIM)) 281 return (0); 282 283 ppb_release_bus(&sc->lpbb_dev); 284 285 return (1); 286 } 287 288 static int 289 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr) 290 { 291 struct lpbb_softc *sc = (struct lpbb_softc *)device_get_softc(dev); 292 293 /* reset bus */ 294 setSDA(sc, 1); 295 setSCL(sc, 1); 296 297 return (IIC_ENOADDR); 298 } 299 300 static void 301 lpbb_setlines(device_t dev, int ctrl, int data) 302 { 303 struct lpbb_softc *sc = (struct lpbb_softc *)device_get_softc(dev); 304 305 setSCL(sc, ctrl); 306 setSDA(sc, data); 307 } 308 309 static int 310 lpbb_getdataline(device_t dev) 311 { 312 struct lpbb_softc *sc = (struct lpbb_softc *)device_get_softc(dev); 313 314 return (getSDA(sc)); 315 } 316 317 static void 318 lpbb_print_child(device_t bus, device_t dev) 319 { 320 printf(" on %s%d", device_get_name(bus), device_get_unit(bus)); 321 322 return; 323 } 324 325 DRIVER_MODULE(lpbb, root, lpbb_driver, lpbb_devclass, 0, 0); 326