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