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/iiconf.c,v 1.19 2008/08/23 07:38:00 imp Exp $ 27 * $DragonFly: src/sys/bus/iicbus/iiconf.c,v 1.5 2005/06/02 20:40:36 dillon Exp $ 28 * 29 */ 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/malloc.h> 33 #include <sys/module.h> 34 #include <sys/bus.h> 35 #include <sys/thread2.h> 36 37 #include <bus/iicbus/iiconf.h> 38 #include <bus/iicbus/iicbus.h> 39 #include "iicbus_if.h" 40 41 /* 42 * iicbus_intr() 43 */ 44 void 45 iicbus_intr(device_t bus, int event, char *buf) 46 { 47 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 48 49 /* call owner's intr routine */ 50 if (sc->owner) 51 IICBUS_INTR(sc->owner, event, buf); 52 53 return; 54 } 55 56 static int 57 iicbus_poll(struct iicbus_softc *sc, int how) 58 { 59 int error; 60 61 switch (how) { 62 case IIC_WAIT | IIC_INTR: 63 error = tsleep(sc, PCATCH, "iicreq", 0); 64 break; 65 66 case IIC_WAIT | IIC_NOINTR: 67 error = tsleep(sc, 0, "iicreq", 0); 68 break; 69 70 default: 71 return (EWOULDBLOCK); 72 break; 73 } 74 75 return (error); 76 } 77 78 /* 79 * iicbus_request_bus() 80 * 81 * Allocate the device to perform transfers. 82 * 83 * how : IIC_WAIT or IIC_DONTWAIT 84 */ 85 int 86 iicbus_request_bus(device_t bus, device_t dev, int how) 87 { 88 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 89 int error = 0; 90 91 /* first, ask the underlying layers if the request is ok */ 92 do { 93 error = IICBUS_CALLBACK(device_get_parent(bus), 94 IIC_REQUEST_BUS, (caddr_t)&how); 95 if (error) 96 error = iicbus_poll(sc, how); 97 } while (error == EWOULDBLOCK); 98 99 while (!error) { 100 crit_enter(); 101 if (sc->owner && sc->owner != dev) { 102 crit_exit(); 103 error = iicbus_poll(sc, how); 104 } else { 105 sc->owner = dev; 106 crit_exit(); 107 return (0); 108 } 109 110 /* free any allocated resource */ 111 if (error) 112 IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, 113 (caddr_t)&how); 114 } 115 116 return (error); 117 } 118 119 /* 120 * iicbus_release_bus() 121 * 122 * Release the device allocated with iicbus_request_dev() 123 */ 124 int 125 iicbus_release_bus(device_t bus, device_t dev) 126 { 127 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 128 int error; 129 130 /* first, ask the underlying layers if the release is ok */ 131 error = IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, NULL); 132 133 if (error) 134 return (error); 135 136 crit_enter(); 137 if (sc->owner != dev) { 138 crit_exit(); 139 return (EACCES); 140 } 141 sc->owner = NULL; 142 crit_exit(); 143 144 /* wakeup waiting processes */ 145 wakeup(sc); 146 147 return (0); 148 } 149 150 /* 151 * iicbus_started() 152 * 153 * Test if the iicbus is started by the controller 154 */ 155 int 156 iicbus_started(device_t bus) 157 { 158 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 159 160 return (sc->started); 161 } 162 163 /* 164 * iicbus_start() 165 * 166 * Send start condition to the slave addressed by 'slave' 167 */ 168 int 169 iicbus_start(device_t bus, u_char slave, int timeout) 170 { 171 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 172 int error = 0; 173 174 if (sc->started) 175 return (EINVAL); /* bus already started */ 176 177 if (!(error = IICBUS_START(device_get_parent(bus), slave, timeout))) 178 sc->started = slave; 179 else 180 sc->started = 0; 181 182 return (error); 183 } 184 185 /* 186 * iicbus_repeated_start() 187 * 188 * Send start condition to the slave addressed by 'slave' 189 */ 190 int 191 iicbus_repeated_start(device_t bus, u_char slave, int timeout) 192 { 193 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 194 int error = 0; 195 196 if (!sc->started) 197 return (EINVAL); /* bus should have been already started */ 198 199 if (!(error = IICBUS_REPEATED_START(device_get_parent(bus), slave, timeout))) 200 sc->started = slave; 201 else 202 sc->started = 0; 203 204 return (error); 205 } 206 207 /* 208 * iicbus_stop() 209 * 210 * Send stop condition to the bus 211 */ 212 int 213 iicbus_stop(device_t bus) 214 { 215 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 216 int error = 0; 217 218 if (!sc->started) 219 return (EINVAL); /* bus not started */ 220 221 error = IICBUS_STOP(device_get_parent(bus)); 222 223 /* refuse any further access */ 224 sc->started = 0; 225 226 return (error); 227 } 228 229 /* 230 * iicbus_write() 231 * 232 * Write a block of data to the slave previously started by 233 * iicbus_start() call 234 */ 235 int 236 iicbus_write(device_t bus, const char *buf, int len, int *sent, int timeout) 237 { 238 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 239 240 /* a slave must have been started with the appropriate address */ 241 if (!sc->started || (sc->started & LSB)) 242 return (EINVAL); 243 244 return (IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout)); 245 } 246 247 /* 248 * iicbus_read() 249 * 250 * Read a block of data from the slave previously started by 251 * iicbus_read() call 252 */ 253 int 254 iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay) 255 { 256 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 257 258 /* a slave must have been started with the appropriate address */ 259 if (!sc->started || !(sc->started & LSB)) 260 return (EINVAL); 261 262 return (IICBUS_READ(device_get_parent(bus), buf, len, read, last, delay)); 263 } 264 265 /* 266 * iicbus_write_byte() 267 * 268 * Write a byte to the slave previously started by iicbus_start() call 269 */ 270 int 271 iicbus_write_byte(device_t bus, char byte, int timeout) 272 { 273 char data = byte; 274 int sent; 275 276 return (iicbus_write(bus, &data, 1, &sent, timeout)); 277 } 278 279 /* 280 * iicbus_read_byte() 281 * 282 * Read a byte from the slave previously started by iicbus_start() call 283 */ 284 int 285 iicbus_read_byte(device_t bus, char *byte, int timeout) 286 { 287 int read; 288 289 return (iicbus_read(bus, byte, 1, &read, IIC_LAST_READ, timeout)); 290 } 291 292 /* 293 * iicbus_block_write() 294 * 295 * Write a block of data to slave ; start/stop protocol managed 296 */ 297 int 298 iicbus_block_write(device_t bus, u_char slave, char *buf, int len, int *sent) 299 { 300 u_char addr = slave & ~LSB; 301 int error; 302 303 if ((error = iicbus_start(bus, addr, 0))) 304 return (error); 305 306 error = iicbus_write(bus, buf, len, sent, 0); 307 308 iicbus_stop(bus); 309 310 return (error); 311 } 312 313 /* 314 * iicbus_block_read() 315 * 316 * Read a block of data from slave ; start/stop protocol managed 317 */ 318 int 319 iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read) 320 { 321 u_char addr = slave | LSB; 322 int error; 323 324 if ((error = iicbus_start(bus, addr, 0))) 325 return (error); 326 327 error = iicbus_read(bus, buf, len, read, IIC_LAST_READ, 0); 328 329 iicbus_stop(bus); 330 331 return (error); 332 } 333 334 /* 335 * iicbus_transfer() 336 * 337 * Do an aribtrary number of transfers on the iicbus. We pass these 338 * raw requests to the bridge driver. If the bridge driver supports 339 * them directly, then it manages all the details. If not, it can use 340 * the helper function iicbus_transfer_gen() which will do the 341 * transfers at a low level. 342 * 343 * Pointers passed in as part of iic_msg must be kernel pointers. 344 * Callers that have user addresses to manage must do so on their own. 345 */ 346 int 347 iicbus_transfer(device_t bus, struct iic_msg *msgs, uint32_t nmsgs) 348 { 349 return (IICBUS_TRANSFER(device_get_parent(bus), msgs, nmsgs)); 350 } 351 352 /* 353 * Generic version of iicbus_transfer that calls the appropriate 354 * routines to accomplish this. See note above about acceptable 355 * buffer addresses. 356 */ 357 int 358 iicbus_transfer_gen(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) 359 { 360 int i, error, lenread, lenwrote, nkid; 361 device_t *children, bus; 362 363 if ((error = device_get_children(dev, &children, &nkid)) != 0) 364 return (error); 365 if (nkid != 1) { 366 kfree(children, M_TEMP); 367 return (EIO); 368 } 369 bus = children[0]; 370 kfree(children, M_TEMP); 371 for (i = 0, error = 0; i < nmsgs && error == 0; i++) { 372 if (msgs[i].flags & IIC_M_RD) 373 error = iicbus_block_read(bus, msgs[i].slave, 374 msgs[i].buf, msgs[i].len, &lenread); 375 else 376 error = iicbus_block_write(bus, msgs[i].slave, 377 msgs[i].buf, msgs[i].len, &lenwrote); 378 } 379 return (error); 380 } 381