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
26 * $FreeBSD: src/sys/dev/iicbus/iicsmb.c,v 1.18 2009/02/10 22:50:23 imp Exp $
47 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/systm.h>
54 #include <bus/iicbus/iiconf.h>
55 #include <bus/iicbus/iicbus.h>
57 #include <bus/smbus/smbconf.h>
59 #include "iicbus_if.h"
64 #define SMB_WAITING_ADDR 0x0
65 #define SMB_WAITING_LOW 0x1
66 #define SMB_WAITING_HIGH 0x2
70 u_char devaddr
; /* slave device address */
72 char low
; /* low byte received first */
73 char high
; /* high byte */
78 static int iicsmb_probe(device_t
);
79 static int iicsmb_attach(device_t
);
80 static int iicsmb_detach(device_t
);
81 static void iicsmb_identify(driver_t
*driver
, device_t parent
);
83 static int iicsmb_intr(device_t dev
, int event
, char *buf
);
84 static int iicsmb_callback(device_t dev
, int index
, void *data
);
85 static int iicsmb_quick(device_t dev
, u_char slave
, int how
);
86 static int iicsmb_sendb(device_t dev
, u_char slave
, char byte
);
87 static int iicsmb_recvb(device_t dev
, u_char slave
, char *byte
);
88 static int iicsmb_writeb(device_t dev
, u_char slave
, char cmd
, char byte
);
89 static int iicsmb_writew(device_t dev
, u_char slave
, char cmd
, short word
);
90 static int iicsmb_readb(device_t dev
, u_char slave
, char cmd
, char *byte
);
91 static int iicsmb_readw(device_t dev
, u_char slave
, char cmd
, short *word
);
92 static int iicsmb_pcall(device_t dev
, u_char slave
, char cmd
, short sdata
, short *rdata
);
93 static int iicsmb_bwrite(device_t dev
, u_char slave
, char cmd
, u_char count
, char *buf
);
94 static int iicsmb_bread(device_t dev
, u_char slave
, char cmd
, u_char
*count
, char *buf
);
96 static devclass_t iicsmb_devclass
;
98 static device_method_t iicsmb_methods
[] = {
99 /* device interface */
100 DEVMETHOD(device_identify
, iicsmb_identify
),
101 DEVMETHOD(device_probe
, iicsmb_probe
),
102 DEVMETHOD(device_attach
, iicsmb_attach
),
103 DEVMETHOD(device_detach
, iicsmb_detach
),
106 DEVMETHOD(bus_driver_added
, bus_generic_driver_added
),
107 DEVMETHOD(bus_print_child
, bus_generic_print_child
),
109 /* iicbus interface */
110 DEVMETHOD(iicbus_intr
, iicsmb_intr
),
112 /* smbus interface */
113 DEVMETHOD(smbus_callback
, iicsmb_callback
),
114 DEVMETHOD(smbus_quick
, iicsmb_quick
),
115 DEVMETHOD(smbus_sendb
, iicsmb_sendb
),
116 DEVMETHOD(smbus_recvb
, iicsmb_recvb
),
117 DEVMETHOD(smbus_writeb
, iicsmb_writeb
),
118 DEVMETHOD(smbus_writew
, iicsmb_writew
),
119 DEVMETHOD(smbus_readb
, iicsmb_readb
),
120 DEVMETHOD(smbus_readw
, iicsmb_readw
),
121 DEVMETHOD(smbus_pcall
, iicsmb_pcall
),
122 DEVMETHOD(smbus_bwrite
, iicsmb_bwrite
),
123 DEVMETHOD(smbus_bread
, iicsmb_bread
),
128 static driver_t iicsmb_driver
= {
131 sizeof(struct iicsmb_softc
),
134 #define IICBUS_TIMEOUT 100 /* us */
137 iicsmb_identify(driver_t
*driver
, device_t parent
)
140 if (device_find_child(parent
, "iicsmb", -1) == NULL
)
141 BUS_ADD_CHILD(parent
, parent
, 0, "iicsmb", -1);
145 iicsmb_probe(device_t dev
)
147 device_set_desc(dev
, "SMBus over I2C bridge");
152 iicsmb_attach(device_t dev
)
154 struct iicsmb_softc
*sc
= (struct iicsmb_softc
*)device_get_softc(dev
);
156 sc
->smbus
= device_add_child(dev
, "smbus", -1);
158 /* probe and attach the smbus */
159 bus_generic_attach(dev
);
165 iicsmb_detach(device_t dev
)
167 struct iicsmb_softc
*sc
= (struct iicsmb_softc
*)device_get_softc(dev
);
169 bus_generic_detach(dev
);
171 device_delete_child(dev
, sc
->smbus
);
180 * iicbus interrupt handler
183 iicsmb_intr(device_t dev
, int event
, char *buf
)
185 struct iicsmb_softc
*sc
= (struct iicsmb_softc
*)device_get_softc(dev
);
190 sc
->state
= SMB_WAITING_ADDR
;
194 /* call smbus intr handler */
195 smbus_intr(sc
->smbus
, sc
->devaddr
,
196 sc
->low
, sc
->high
, SMB_ENOERR
);
202 /* XXX too much data, discard */
203 kprintf("%s: too much data from 0x%x\n", __func__
,
207 case SMB_WAITING_ADDR
:
208 sc
->devaddr
= (u_char
)*buf
;
209 sc
->state
= SMB_WAITING_LOW
;
212 case SMB_WAITING_LOW
:
214 sc
->state
= SMB_WAITING_HIGH
;
217 case SMB_WAITING_HIGH
:
219 sc
->state
= SMB_DONE
;
232 smbus_intr(sc
->smbus
, sc
->devaddr
, 0, 0, SMB_EBUSERR
);
236 kprintf("%s unknown error 0x%x!\n", __func__
,
243 panic("%s: unknown event (%d)!", __func__
, event
);
250 iicsmb_callback(device_t dev
, int index
, void *data
)
252 device_t parent
= device_get_parent(dev
);
257 case SMB_REQUEST_BUS
:
258 /* request underlying iicbus */
260 error
= iicbus_request_bus(parent
, dev
, how
);
263 case SMB_RELEASE_BUS
:
264 /* release underlying iicbus */
265 error
= iicbus_release_bus(parent
, dev
);
276 iicsmb_quick(device_t dev
, u_char slave
, int how
)
278 device_t parent
= device_get_parent(dev
);
283 error
= iicbus_start(parent
, slave
& ~LSB
, IICBUS_TIMEOUT
);
287 error
= iicbus_start(parent
, slave
| LSB
, IICBUS_TIMEOUT
);
296 error
= iicbus_stop(parent
);
302 iicsmb_sendb(device_t dev
, u_char slave
, char byte
)
304 device_t parent
= device_get_parent(dev
);
307 error
= iicbus_start(parent
, slave
& ~LSB
, IICBUS_TIMEOUT
);
310 error
= iicbus_write(parent
, &byte
, 1, &sent
, IICBUS_TIMEOUT
);
319 iicsmb_recvb(device_t dev
, u_char slave
, char *byte
)
321 device_t parent
= device_get_parent(dev
);
324 error
= iicbus_start(parent
, slave
| LSB
, 0);
327 error
= iicbus_read(parent
, byte
, 1, &read
, IIC_LAST_READ
, IICBUS_TIMEOUT
);
336 iicsmb_writeb(device_t dev
, u_char slave
, char cmd
, char byte
)
338 device_t parent
= device_get_parent(dev
);
341 error
= iicbus_start(parent
, slave
& ~LSB
, 0);
344 if (!(error
= iicbus_write(parent
, &cmd
, 1, &sent
, IICBUS_TIMEOUT
)))
345 error
= iicbus_write(parent
, &byte
, 1, &sent
, IICBUS_TIMEOUT
);
354 iicsmb_writew(device_t dev
, u_char slave
, char cmd
, short word
)
356 device_t parent
= device_get_parent(dev
);
359 char low
= (char)(word
& 0xff);
360 char high
= (char)((word
& 0xff00) >> 8);
362 error
= iicbus_start(parent
, slave
& ~LSB
, 0);
365 if (!(error
= iicbus_write(parent
, &cmd
, 1, &sent
, IICBUS_TIMEOUT
)))
366 if (!(error
= iicbus_write(parent
, &low
, 1, &sent
, IICBUS_TIMEOUT
)))
367 error
= iicbus_write(parent
, &high
, 1, &sent
, IICBUS_TIMEOUT
);
376 iicsmb_readb(device_t dev
, u_char slave
, char cmd
, char *byte
)
378 device_t parent
= device_get_parent(dev
);
379 int error
, sent
, read
;
381 if ((error
= iicbus_start(parent
, slave
& ~LSB
, IICBUS_TIMEOUT
)))
384 if ((error
= iicbus_write(parent
, &cmd
, 1, &sent
, IICBUS_TIMEOUT
)))
387 if ((error
= iicbus_repeated_start(parent
, slave
| LSB
, IICBUS_TIMEOUT
)))
390 if ((error
= iicbus_read(parent
, byte
, 1, &read
, IIC_LAST_READ
, IICBUS_TIMEOUT
)))
398 #define BUF2SHORT(low,high) \
399 ((short)(((high) & 0xff) << 8) | (short)((low) & 0xff))
402 iicsmb_readw(device_t dev
, u_char slave
, char cmd
, short *word
)
404 device_t parent
= device_get_parent(dev
);
405 int error
, sent
, read
;
408 if ((error
= iicbus_start(parent
, slave
& ~LSB
, IICBUS_TIMEOUT
)))
411 if ((error
= iicbus_write(parent
, &cmd
, 1, &sent
, IICBUS_TIMEOUT
)))
414 if ((error
= iicbus_repeated_start(parent
, slave
| LSB
, IICBUS_TIMEOUT
)))
417 if ((error
= iicbus_read(parent
, buf
, 2, &read
, IIC_LAST_READ
, IICBUS_TIMEOUT
)))
420 /* first, receive low, then high byte */
421 *word
= BUF2SHORT(buf
[0], buf
[1]);
429 iicsmb_pcall(device_t dev
, u_char slave
, char cmd
, short sdata
, short *rdata
)
431 device_t parent
= device_get_parent(dev
);
432 int error
, sent
, read
;
435 if ((error
= iicbus_start(parent
, slave
& ~LSB
, IICBUS_TIMEOUT
)))
438 if ((error
= iicbus_write(parent
, &cmd
, 1, &sent
, IICBUS_TIMEOUT
)))
441 /* first, send low, then high byte */
442 buf
[0] = (char)(sdata
& 0xff);
443 buf
[1] = (char)((sdata
& 0xff00) >> 8);
445 if ((error
= iicbus_write(parent
, buf
, 2, &sent
, IICBUS_TIMEOUT
)))
448 if ((error
= iicbus_repeated_start(parent
, slave
| LSB
, IICBUS_TIMEOUT
)))
451 if ((error
= iicbus_read(parent
, buf
, 2, &read
, IIC_LAST_READ
, IICBUS_TIMEOUT
)))
454 /* first, receive low, then high byte */
455 *rdata
= BUF2SHORT(buf
[0], buf
[1]);
463 iicsmb_bwrite(device_t dev
, u_char slave
, char cmd
, u_char count
, char *buf
)
465 device_t parent
= device_get_parent(dev
);
468 if ((error
= iicbus_start(parent
, slave
& ~LSB
, IICBUS_TIMEOUT
)))
471 if ((error
= iicbus_write(parent
, &cmd
, 1, &sent
, IICBUS_TIMEOUT
)))
474 if ((error
= iicbus_write(parent
, buf
, (int)count
, &sent
, IICBUS_TIMEOUT
)))
477 if ((error
= iicbus_stop(parent
)))
485 iicsmb_bread(device_t dev
, u_char slave
, char cmd
, u_char
*count
, char *buf
)
487 device_t parent
= device_get_parent(dev
);
488 int error
, sent
, read
;
490 if ((error
= iicbus_start(parent
, slave
& ~LSB
, IICBUS_TIMEOUT
)))
493 if ((error
= iicbus_write(parent
, &cmd
, 1, &sent
, IICBUS_TIMEOUT
)))
496 if ((error
= iicbus_repeated_start(parent
, slave
| LSB
, IICBUS_TIMEOUT
)))
499 if ((error
= iicbus_read(parent
, buf
, (int)*count
, &read
,
500 IIC_LAST_READ
, IICBUS_TIMEOUT
)))
509 DRIVER_MODULE(iicsmb
, iicbus
, iicsmb_driver
, iicsmb_devclass
, NULL
, NULL
);
510 DRIVER_MODULE(smbus
, iicsmb
, smbus_driver
, smbus_devclass
, NULL
, NULL
);
511 MODULE_DEPEND(iicsmb
, iicbus
, IICBUS_MINVER
, IICBUS_PREFVER
, IICBUS_MAXVER
);
512 MODULE_DEPEND(iicsmb
, smbus
, SMBUS_MINVER
, SMBUS_PREFVER
, SMBUS_MAXVER
);
513 MODULE_VERSION(iicsmb
, 1);