2 * Copyright (c) 1998 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/dev/iicbus/iicsmb.c,v 1.5.2.2 2000/08/09 00:59:27 peter Exp $
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/module.h>
56 #include <sys/malloc.h>
58 #include <machine/clock.h>
60 #include <dev/iicbus/iiconf.h>
61 #include <dev/iicbus/iicbus.h>
63 #include <dev/smbus/smbconf.h>
65 #include "iicbus_if.h"
70 #define SMB_WAITING_ADDR 0x0
71 #define SMB_WAITING_LOW 0x1
72 #define SMB_WAITING_HIGH 0x2
76 u_char devaddr; /* slave device address */
78 char low; /* low byte received first */
79 char high; /* high byte */
84 static int iicsmb_probe(device_t);
85 static int iicsmb_attach(device_t);
87 static void iicsmb_intr(device_t dev, int event, char *buf);
88 static int iicsmb_callback(device_t dev, int index, caddr_t data);
89 static int iicsmb_quick(device_t dev, u_char slave, int how);
90 static int iicsmb_sendb(device_t dev, u_char slave, char byte);
91 static int iicsmb_recvb(device_t dev, u_char slave, char *byte);
92 static int iicsmb_writeb(device_t dev, u_char slave, char cmd, char byte);
93 static int iicsmb_writew(device_t dev, u_char slave, char cmd, short word);
94 static int iicsmb_readb(device_t dev, u_char slave, char cmd, char *byte);
95 static int iicsmb_readw(device_t dev, u_char slave, char cmd, short *word);
96 static int iicsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata);
97 static int iicsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf);
98 static int iicsmb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf);
100 static devclass_t iicsmb_devclass;
102 static device_method_t iicsmb_methods[] = {
103 /* device interface */
104 DEVMETHOD(device_probe, iicsmb_probe),
105 DEVMETHOD(device_attach, iicsmb_attach),
106 DEVMETHOD(device_detach, bus_generic_detach),
109 DEVMETHOD(bus_print_child, bus_generic_print_child),
111 /* iicbus interface */
112 DEVMETHOD(iicbus_intr, iicsmb_intr),
114 /* smbus interface */
115 DEVMETHOD(smbus_callback, iicsmb_callback),
116 DEVMETHOD(smbus_quick, iicsmb_quick),
117 DEVMETHOD(smbus_sendb, iicsmb_sendb),
118 DEVMETHOD(smbus_recvb, iicsmb_recvb),
119 DEVMETHOD(smbus_writeb, iicsmb_writeb),
120 DEVMETHOD(smbus_writew, iicsmb_writew),
121 DEVMETHOD(smbus_readb, iicsmb_readb),
122 DEVMETHOD(smbus_readw, iicsmb_readw),
123 DEVMETHOD(smbus_pcall, iicsmb_pcall),
124 DEVMETHOD(smbus_bwrite, iicsmb_bwrite),
125 DEVMETHOD(smbus_bread, iicsmb_bread),
130 static driver_t iicsmb_driver = {
133 sizeof(struct iicsmb_softc),
137 iicsmb_probe(device_t dev)
139 struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
141 sc->smbus = smbus_alloc_bus(dev);
144 return (EINVAL); /* XXX don't know what to return else */
150 iicsmb_attach(device_t dev)
152 struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
154 /* probe and attach the smbus */
155 device_probe_and_attach(sc->smbus);
163 * iicbus interrupt handler
166 iicsmb_intr(device_t dev, int event, char *buf)
168 struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
173 sc->state = SMB_WAITING_ADDR;
177 /* call smbus intr handler */
178 smbus_intr(sc->smbus, sc->devaddr,
179 sc->low, sc->high, SMB_ENOERR);
185 /* XXX too much data, discard */
186 printf("%s: too much data from 0x%x\n", __FUNCTION__,
190 case SMB_WAITING_ADDR:
191 sc->devaddr = (u_char)*buf;
192 sc->state = SMB_WAITING_LOW;
195 case SMB_WAITING_LOW:
197 sc->state = SMB_WAITING_HIGH;
200 case SMB_WAITING_HIGH:
202 sc->state = SMB_DONE;
215 smbus_intr(sc->smbus, sc->devaddr, 0, 0, SMB_EBUSERR);
219 printf("%s unknown error 0x%x!\n", __FUNCTION__,
226 panic("%s: unknown event (%d)!", __FUNCTION__, event);
233 iicsmb_callback(device_t dev, int index, caddr_t data)
235 device_t parent = device_get_parent(dev);
240 case SMB_REQUEST_BUS:
241 /* request underlying iicbus */
243 error = iicbus_request_bus(parent, dev, how);
246 case SMB_RELEASE_BUS:
247 /* release underlying iicbus */
248 error = iicbus_release_bus(parent, dev);
259 iicsmb_quick(device_t dev, u_char slave, int how)
261 device_t parent = device_get_parent(dev);
266 error = iicbus_start(parent, slave & ~LSB, 0);
270 error = iicbus_start(parent, slave | LSB, 0);
279 error = iicbus_stop(parent);
285 iicsmb_sendb(device_t dev, u_char slave, char byte)
287 device_t parent = device_get_parent(dev);
290 error = iicbus_start(parent, slave & ~LSB, 0);
293 error = iicbus_write(parent, &byte, 1, &sent, 0);
302 iicsmb_recvb(device_t dev, u_char slave, char *byte)
304 device_t parent = device_get_parent(dev);
307 error = iicbus_start(parent, slave | LSB, 0);
310 error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, 0);
319 iicsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
321 device_t parent = device_get_parent(dev);
324 error = iicbus_start(parent, slave & ~LSB, 0);
327 if (!(error = iicbus_write(parent, &cmd, 1, &sent, 0)))
328 error = iicbus_write(parent, &byte, 1, &sent, 0);
337 iicsmb_writew(device_t dev, u_char slave, char cmd, short word)
339 device_t parent = device_get_parent(dev);
342 char low = (char)(word & 0xff);
343 char high = (char)((word & 0xff00) >> 8);
345 error = iicbus_start(parent, slave & ~LSB, 0);
348 if (!(error = iicbus_write(parent, &cmd, 1, &sent, 0)))
349 if (!(error = iicbus_write(parent, &low, 1, &sent, 0)))
350 error = iicbus_write(parent, &high, 1, &sent, 0);
359 iicsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
361 device_t parent = device_get_parent(dev);
362 int error, sent, read;
364 if ((error = iicbus_start(parent, slave & ~LSB, 0)))
367 if ((error = iicbus_write(parent, &cmd, 1, &sent, 0)))
370 if ((error = iicbus_repeated_start(parent, slave | LSB, 0)))
373 if ((error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, 0)))
381 #define BUF2SHORT(low,high) \
382 ((short)(((high) & 0xff) << 8) | (short)((low) & 0xff))
385 iicsmb_readw(device_t dev, u_char slave, char cmd, short *word)
387 device_t parent = device_get_parent(dev);
388 int error, sent, read;
391 if ((error = iicbus_start(parent, slave & ~LSB, 0)))
394 if ((error = iicbus_write(parent, &cmd, 1, &sent, 0)))
397 if ((error = iicbus_repeated_start(parent, slave | LSB, 0)))
400 if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, 0)))
403 /* first, receive low, then high byte */
404 *word = BUF2SHORT(buf[0], buf[1]);
412 iicsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata)
414 device_t parent = device_get_parent(dev);
415 int error, sent, read;
418 if ((error = iicbus_start(parent, slave & ~LSB, 0)))
421 if ((error = iicbus_write(parent, &cmd, 1, &sent, 0)))
424 /* first, send low, then high byte */
425 buf[0] = (char)(sdata & 0xff);
426 buf[1] = (char)((sdata & 0xff00) >> 8);
428 if ((error = iicbus_write(parent, buf, 2, &sent, 0)))
431 if ((error = iicbus_repeated_start(parent, slave | LSB, 0)))
434 if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, 0)))
437 /* first, receive low, then high byte */
438 *rdata = BUF2SHORT(buf[0], buf[1]);
446 iicsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
448 device_t parent = device_get_parent(dev);
451 if ((error = iicbus_start(parent, slave & ~LSB, 0)))
454 if ((error = iicbus_write(parent, &cmd, 1, &sent, 0)))
457 if ((error = iicbus_write(parent, buf, (int)count, &sent, 0)))
460 if ((error = iicbus_stop(parent)))
468 iicsmb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf)
470 device_t parent = device_get_parent(dev);
471 int error, sent, read;
473 if ((error = iicbus_start(parent, slave & ~LSB, 0)))
476 if ((error = iicbus_write(parent, &cmd, 1, &sent, 0)))
479 if ((error = iicbus_repeated_start(parent, slave | LSB, 0)))
482 if ((error = iicbus_read(parent, buf, (int)count, &read,
491 DRIVER_MODULE(iicsmb, iicbus, iicsmb_driver, iicsmb_devclass, 0, 0);