2 * Copyright (c) 1998, 2001 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
48 #include <sys/param.h>
50 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/mutex.h>
54 #include <sys/systm.h>
57 #include <dev/iicbus/iiconf.h>
58 #include <dev/iicbus/iicbus.h>
60 #include <dev/smbus/smbconf.h>
62 #include "iicbus_if.h"
67 #define SMB_WAITING_ADDR 0x0
68 #define SMB_WAITING_LOW 0x1
69 #define SMB_WAITING_HIGH 0x2
73 u_char devaddr; /* slave device address */
75 char low; /* low byte received first */
76 char high; /* high byte */
82 static int iicsmb_probe(device_t);
83 static int iicsmb_attach(device_t);
84 static int iicsmb_detach(device_t);
85 static void iicsmb_identify(driver_t *driver, device_t parent);
87 static int iicsmb_intr(device_t dev, int event, char *buf);
88 static int iicsmb_callback(device_t dev, int index, void *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_identify, iicsmb_identify),
105 DEVMETHOD(device_probe, iicsmb_probe),
106 DEVMETHOD(device_attach, iicsmb_attach),
107 DEVMETHOD(device_detach, iicsmb_detach),
110 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
111 DEVMETHOD(bus_print_child, bus_generic_print_child),
113 /* iicbus interface */
114 DEVMETHOD(iicbus_intr, iicsmb_intr),
116 /* smbus interface */
117 DEVMETHOD(smbus_callback, iicsmb_callback),
118 DEVMETHOD(smbus_quick, iicsmb_quick),
119 DEVMETHOD(smbus_sendb, iicsmb_sendb),
120 DEVMETHOD(smbus_recvb, iicsmb_recvb),
121 DEVMETHOD(smbus_writeb, iicsmb_writeb),
122 DEVMETHOD(smbus_writew, iicsmb_writew),
123 DEVMETHOD(smbus_readb, iicsmb_readb),
124 DEVMETHOD(smbus_readw, iicsmb_readw),
125 DEVMETHOD(smbus_pcall, iicsmb_pcall),
126 DEVMETHOD(smbus_bwrite, iicsmb_bwrite),
127 DEVMETHOD(smbus_bread, iicsmb_bread),
132 static driver_t iicsmb_driver = {
135 sizeof(struct iicsmb_softc),
138 #define IICBUS_TIMEOUT 100 /* us */
141 iicsmb_identify(driver_t *driver, device_t parent)
144 if (device_find_child(parent, "iicsmb", -1) == NULL)
145 BUS_ADD_CHILD(parent, 0, "iicsmb", -1);
149 iicsmb_probe(device_t dev)
151 device_set_desc(dev, "SMBus over I2C bridge");
152 return (BUS_PROBE_NOWILDCARD);
156 iicsmb_attach(device_t dev)
158 struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
160 mtx_init(&sc->lock, "iicsmb", NULL, MTX_DEF);
162 sc->smbus = device_add_child(dev, "smbus", -1);
164 /* probe and attach the smbus */
165 bus_generic_attach(dev);
171 iicsmb_detach(device_t dev)
173 struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
175 bus_generic_detach(dev);
177 device_delete_child(dev, sc->smbus);
179 mtx_destroy(&sc->lock);
187 * iicbus interrupt handler
190 iicsmb_intr(device_t dev, int event, char *buf)
192 struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
198 sc->state = SMB_WAITING_ADDR;
202 /* call smbus intr handler */
203 smbus_intr(sc->smbus, sc->devaddr,
204 sc->low, sc->high, SMB_ENOERR);
210 /* XXX too much data, discard */
211 printf("%s: too much data from 0x%x\n", __func__,
215 case SMB_WAITING_ADDR:
216 sc->devaddr = (u_char)*buf;
217 sc->state = SMB_WAITING_LOW;
220 case SMB_WAITING_LOW:
222 sc->state = SMB_WAITING_HIGH;
225 case SMB_WAITING_HIGH:
227 sc->state = SMB_DONE;
240 smbus_intr(sc->smbus, sc->devaddr, 0, 0, SMB_EBUSERR);
244 printf("%s unknown error 0x%x!\n", __func__,
251 panic("%s: unknown event (%d)!", __func__, event);
253 mtx_unlock(&sc->lock);
259 iicsmb_callback(device_t dev, int index, void *data)
261 device_t parent = device_get_parent(dev);
266 case SMB_REQUEST_BUS:
267 /* request underlying iicbus */
269 error = iicbus_request_bus(parent, dev, how);
272 case SMB_RELEASE_BUS:
273 /* release underlying iicbus */
274 error = iicbus_release_bus(parent, dev);
285 iicsmb_quick(device_t dev, u_char slave, int how)
287 device_t parent = device_get_parent(dev);
292 error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT);
296 error = iicbus_start(parent, slave | LSB, IICBUS_TIMEOUT);
305 error = iicbus_stop(parent);
311 iicsmb_sendb(device_t dev, u_char slave, char byte)
313 device_t parent = device_get_parent(dev);
316 error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT);
319 error = iicbus_write(parent, &byte, 1, &sent, IICBUS_TIMEOUT);
328 iicsmb_recvb(device_t dev, u_char slave, char *byte)
330 device_t parent = device_get_parent(dev);
333 error = iicbus_start(parent, slave | LSB, 0);
336 error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, IICBUS_TIMEOUT);
345 iicsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
347 device_t parent = device_get_parent(dev);
350 error = iicbus_start(parent, slave & ~LSB, 0);
353 if (!(error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
354 error = iicbus_write(parent, &byte, 1, &sent, IICBUS_TIMEOUT);
363 iicsmb_writew(device_t dev, u_char slave, char cmd, short word)
365 device_t parent = device_get_parent(dev);
368 char low = (char)(word & 0xff);
369 char high = (char)((word & 0xff00) >> 8);
371 error = iicbus_start(parent, slave & ~LSB, 0);
374 if (!(error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
375 if (!(error = iicbus_write(parent, &low, 1, &sent, IICBUS_TIMEOUT)))
376 error = iicbus_write(parent, &high, 1, &sent, IICBUS_TIMEOUT);
385 iicsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
387 device_t parent = device_get_parent(dev);
388 int error, sent, read;
390 if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
393 if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
396 if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
399 if ((error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, IICBUS_TIMEOUT)))
407 #define BUF2SHORT(low,high) \
408 ((short)(((high) & 0xff) << 8) | (short)((low) & 0xff))
411 iicsmb_readw(device_t dev, u_char slave, char cmd, short *word)
413 device_t parent = device_get_parent(dev);
414 int error, sent, read;
417 if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
420 if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
423 if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
426 if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, IICBUS_TIMEOUT)))
429 /* first, receive low, then high byte */
430 *word = BUF2SHORT(buf[0], buf[1]);
438 iicsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata)
440 device_t parent = device_get_parent(dev);
441 int error, sent, read;
444 if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
447 if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
450 /* first, send low, then high byte */
451 buf[0] = (char)(sdata & 0xff);
452 buf[1] = (char)((sdata & 0xff00) >> 8);
454 if ((error = iicbus_write(parent, buf, 2, &sent, IICBUS_TIMEOUT)))
457 if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
460 if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, IICBUS_TIMEOUT)))
463 /* first, receive low, then high byte */
464 *rdata = BUF2SHORT(buf[0], buf[1]);
472 iicsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
474 device_t parent = device_get_parent(dev);
477 if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
480 if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
483 if ((error = iicbus_write(parent, buf, (int)count, &sent, IICBUS_TIMEOUT)))
486 if ((error = iicbus_stop(parent)))
494 iicsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
496 device_t parent = device_get_parent(dev);
497 int error, sent, read;
499 if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
502 if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
505 if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
508 if ((error = iicbus_read(parent, buf, (int)*count, &read,
509 IIC_LAST_READ, IICBUS_TIMEOUT)))
518 DRIVER_MODULE(iicsmb, iicbus, iicsmb_driver, iicsmb_devclass, 0, 0);
519 DRIVER_MODULE(smbus, iicsmb, smbus_driver, smbus_devclass, 0, 0);
520 MODULE_DEPEND(iicsmb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
521 MODULE_DEPEND(iicsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
522 MODULE_VERSION(iicsmb, 1);