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 * $FreeBSD: head/sys/dev/iicbus/iicbb.c 232365 2012-03-01 20:58:20Z kan $
27 */
28
29 /*
30 * Generic I2C bit-banging code
31 *
32 * Example:
33 *
34 * iicbus
35 * / \
36 * iicbb pcf
37 * | \
38 * bti2c lpbb
39 *
40 * From Linux I2C generic interface
41 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
42 *
43 */
44
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
48 #include <sys/module.h>
49 #include <sys/bus.h>
50 #include <sys/uio.h>
51
52 #include <bus/iicbus/iiconf.h>
53 #include <bus/iicbus/iicbus.h>
54
55 #include <bus/smbus/smbconf.h>
56
57 #include "iicbus_if.h"
58 #include "iicbb_if.h"
59
60 struct iicbb_softc {
61 device_t iicbus;
62 int udelay; /* signal toggle delay in usec */
63 };
64
65 static int iicbb_attach(device_t);
66 static void iicbb_child_detached(device_t, device_t);
67 static int iicbb_detach(device_t);
68 static int iicbb_print_child(device_t, device_t);
69 static int iicbb_probe(device_t);
70
71 static int iicbb_callback(device_t, int, caddr_t);
72 static int iicbb_start(device_t, u_char, int);
73 static int iicbb_stop(device_t);
74 static int iicbb_write(device_t, const char *, int, int *, int);
75 static int iicbb_read(device_t, char *, int, int *, int, int);
76 static int iicbb_reset(device_t, u_char, u_char, u_char *);
77 static int iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
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 DEVMETHOD(iicbus_transfer, iicbb_transfer),
98
99 DEVMETHOD_END
100 };
101
102 driver_t iicbb_driver = {
103 "iicbb",
104 iicbb_methods,
105 sizeof(struct iicbb_softc),
106 };
107
108 devclass_t iicbb_devclass;
109
110 static int
iicbb_probe(device_t dev)111 iicbb_probe(device_t dev)
112 {
113 device_set_desc(dev, "I2C bit-banging driver");
114
115 return (0);
116 }
117
118 static int
iicbb_attach(device_t dev)119 iicbb_attach(device_t dev)
120 {
121 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
122
123 sc->iicbus = device_add_child(dev, "iicbus", -1);
124 if (!sc->iicbus)
125 return (ENXIO);
126 sc->udelay = 10; /* 10 uS default */
127 bus_generic_attach(dev);
128
129 return (0);
130 }
131
132 static int
iicbb_detach(device_t dev)133 iicbb_detach(device_t dev)
134 {
135 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
136 device_t child;
137
138 /*
139 * We need to save child because the detach indirectly causes
140 * sc->iicbus to be zeroed. Since we added the device
141 * unconditionally in iicbb_attach, we need to make sure we
142 * delete it here. See iicbb_child_detached. We need that
143 * callback in case newbus detached our children w/o detaching
144 * us (say iicbus is a module and unloaded w/o iicbb being
145 * unloaded).
146 */
147 child = sc->iicbus;
148 bus_generic_detach(dev);
149 if (child)
150 device_delete_child(dev, child);
151
152 return (0);
153 }
154
155 static void
iicbb_child_detached(device_t dev,device_t child)156 iicbb_child_detached( device_t dev, device_t child )
157 {
158 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
159
160 if (child == sc->iicbus)
161 sc->iicbus = NULL;
162 }
163
164 static int
iicbb_print_child(device_t bus,device_t dev)165 iicbb_print_child(device_t bus, device_t dev)
166 {
167 int error;
168 int retval = 0;
169 u_char oldaddr;
170
171 retval += bus_print_child_header(bus, dev);
172 /* retrieve the interface I2C address */
173 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
174 if (error == IIC_ENOADDR) {
175 retval += kprintf(" on %s master-only\n",
176 device_get_nameunit(bus));
177 } else {
178 /* restore the address */
179 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
180
181 retval += kprintf(" on %s addr 0x%x\n",
182 device_get_nameunit(bus), oldaddr & 0xff);
183 }
184
185 return (retval);
186 }
187
188 #define I2C_SETSDA(sc,dev,val) do { \
189 IICBB_SETSDA(device_get_parent(dev), val); \
190 DELAY(sc->udelay); \
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(sc,dev,ctrl,data) do { \
198 I2C_SETSCL(dev, ctrl); \
199 I2C_SETSDA(sc, 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 kprintf(format, args); \
213 } while (0)
214
215 static void
iicbb_setscl(device_t dev,int val,int timeout)216 iicbb_setscl(device_t dev, int val, int timeout)
217 {
218 struct iicbb_softc *sc = device_get_softc(dev);
219 int k = 0;
220
221 IICBB_SETSCL(device_get_parent(dev), val);
222 DELAY(sc->udelay);
223
224 while (val && !I2C_GETSCL(dev) && k++ < timeout) {
225 IICBB_SETSCL(device_get_parent(dev), val);
226 DELAY(sc->udelay);
227 }
228
229 return;
230 }
231
232 static void
iicbb_one(device_t dev,int timeout)233 iicbb_one(device_t dev, int timeout)
234 {
235 struct iicbb_softc *sc = device_get_softc(dev);
236
237 I2C_SET(sc,dev,0,1);
238 I2C_SET(sc,dev,1,1);
239 I2C_SET(sc,dev,0,1);
240 return;
241 }
242
243 static void
iicbb_zero(device_t dev,int timeout)244 iicbb_zero(device_t dev, int timeout)
245 {
246 struct iicbb_softc *sc = device_get_softc(dev);
247
248 I2C_SET(sc,dev,0,0);
249 I2C_SET(sc,dev,1,0);
250 I2C_SET(sc,dev,0,0);
251 return;
252 }
253
254 /*
255 * Waiting for ACKNOWLEDGE.
256 *
257 * When a chip is being addressed or has received data it will issue an
258 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
259 * (set it to high level) and then release the CLOCK line.
260 * Now it must wait for the SLAVE to pull the DATA line low.
261 * Actually on the bus this looks like a START condition so nothing happens
262 * because of the fact that the IC's that have not been addressed are doing
263 * nothing.
264 *
265 * When the SLAVE has pulled this line low the MASTER will take the CLOCK
266 * line low and then the SLAVE will release the SDA (data) line.
267 */
268 static int
iicbb_ack(device_t dev,int timeout)269 iicbb_ack(device_t dev, int timeout)
270 {
271 struct iicbb_softc *sc = device_get_softc(dev);
272 int noack;
273 int k = 0;
274
275 I2C_SET(sc,dev,0,1);
276 I2C_SET(sc,dev,1,1);
277 do {
278 noack = I2C_GETSDA(dev);
279 if (!noack)
280 break;
281 DELAY(1);
282 k++;
283 } while (k < timeout);
284
285 I2C_SET(sc,dev,0,1);
286 I2C_DEBUG(kprintf("%c ",noack?'-':'+'));
287
288 return (noack);
289 }
290
291 static void
iicbb_sendbyte(device_t dev,u_char data,int timeout)292 iicbb_sendbyte(device_t dev, u_char data, int timeout)
293 {
294 int i;
295
296 for (i=7; i>=0; i--) {
297 if (data&(1<<i)) {
298 iicbb_one(dev, timeout);
299 } else {
300 iicbb_zero(dev, timeout);
301 }
302 }
303 I2C_DEBUG(kprintf("w%02x",(int)data));
304 return;
305 }
306
307 static u_char
iicbb_readbyte(device_t dev,int last,int timeout)308 iicbb_readbyte(device_t dev, int last, int timeout)
309 {
310 struct iicbb_softc *sc = device_get_softc(dev);
311 int i;
312 unsigned char data=0;
313
314 I2C_SET(sc,dev,0,1);
315 for (i=7; i>=0; i--)
316 {
317 I2C_SET(sc,dev,1,1);
318 if (I2C_GETSDA(dev))
319 data |= (1<<i);
320 I2C_SET(sc,dev,0,1);
321 }
322 if (last) {
323 iicbb_one(dev, timeout);
324 } else {
325 iicbb_zero(dev, timeout);
326 }
327 I2C_DEBUG(kprintf("r%02x%c ",(int)data,last?'-':'+'));
328 return data;
329 }
330
331 static int
iicbb_callback(device_t dev,int index,caddr_t data)332 iicbb_callback(device_t dev, int index, caddr_t data)
333 {
334 return (IICBB_CALLBACK(device_get_parent(dev), index, data));
335 }
336
337 static int
iicbb_reset(device_t dev,u_char speed,u_char addr,u_char * oldaddr)338 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
339 {
340 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
341 }
342
343 static int
iicbb_start(device_t dev,u_char slave,int timeout)344 iicbb_start(device_t dev, u_char slave, int timeout)
345 {
346 struct iicbb_softc *sc = device_get_softc(dev);
347 int error;
348
349 I2C_DEBUG(kprintf("<"));
350
351 I2C_SET(sc,dev,1,1);
352 I2C_SET(sc,dev,1,0);
353 I2C_SET(sc,dev,0,0);
354
355 /* send address */
356 iicbb_sendbyte(dev, slave, timeout);
357
358 /* check for ack */
359 if (iicbb_ack(dev, timeout)) {
360 error = IIC_ENOACK;
361 goto error;
362 }
363
364 return(0);
365
366 error:
367 iicbb_stop(dev);
368 return (error);
369 }
370
371 static int
iicbb_stop(device_t dev)372 iicbb_stop(device_t dev)
373 {
374 struct iicbb_softc *sc = device_get_softc(dev);
375
376 I2C_SET(sc,dev,0,0);
377 I2C_SET(sc,dev,1,0);
378 I2C_SET(sc,dev,1,1);
379 I2C_DEBUG(kprintf(">"));
380 I2C_DEBUG(kprintf("\n"));
381 return (0);
382 }
383
384 static int
iicbb_write(device_t dev,const char * buf,int len,int * sent,int timeout)385 iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout)
386 {
387 int bytes, error = 0;
388
389 bytes = 0;
390 while (len) {
391 /* send byte */
392 iicbb_sendbyte(dev,(u_char)*buf++, timeout);
393
394 /* check for ack */
395 if (iicbb_ack(dev, timeout)) {
396 error = IIC_ENOACK;
397 goto error;
398 }
399 bytes ++;
400 len --;
401 }
402
403 error:
404 *sent = bytes;
405 return (error);
406 }
407
408 static int
iicbb_read(device_t dev,char * buf,int len,int * read,int last,int delay)409 iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay)
410 {
411 int bytes;
412
413 bytes = 0;
414 while (len) {
415 /* XXX should insert delay here */
416 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
417
418 bytes ++;
419 len --;
420 }
421
422 *read = bytes;
423 return (0);
424 }
425
426 static int
iicbb_transfer(device_t dev,struct iic_msg * msgs,uint32_t nmsgs)427 iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
428 {
429 int error;
430
431 error = IICBB_PRE_XFER(device_get_parent(dev));
432 if (error)
433 return (error);
434
435 error = iicbus_transfer_gen(dev, msgs, nmsgs);
436
437 IICBB_POST_XFER(device_get_parent(dev));
438 return (error);
439 }
440
441 DRIVER_MODULE(iicbb, bti2c, iicbb_driver, iicbb_devclass, NULL, NULL);
442 DRIVER_MODULE(iicbb, cxm_iic, iicbb_driver, iicbb_devclass, NULL, NULL);
443 DRIVER_MODULE(iicbb, intel_iic, iicbb_driver, iicbb_devclass, NULL, NULL);
444 DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, NULL, NULL);
445 DRIVER_MODULE(iicbb, viapm, iicbb_driver, iicbb_devclass, NULL, NULL);
446
447 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
448 MODULE_VERSION(iicbb, IICBB_MODVER);
449