2 * Copyright (c) 2001 Tsubai Masanari. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * NetBSD: ki2c.c,v 1.11 2007/12/06 17:00:33 ad Exp
28 * Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp
32 * Support routines for the Keywest I2C controller.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <machine/resource.h>
43 #include <machine/bus.h>
46 #include <dev/iicbus/iicbus.h>
47 #include <dev/iicbus/iiconf.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include "iicbus_if.h"
51 /* Keywest I2C Register offsets */
62 #define I2C_SPEED 0x03 /* Speed mask */
63 #define I2C_100kHz 0x00
64 #define I2C_50kHz 0x01
65 #define I2C_25kHz 0x02
66 #define I2C_MODE 0x0c /* Mode mask */
67 #define I2C_DUMBMODE 0x00 /* Dumb mode */
68 #define I2C_STDMODE 0x04 /* Standard mode */
69 #define I2C_STDSUBMODE 0x08 /* Standard mode + sub address */
70 #define I2C_COMBMODE 0x0c /* Combined mode */
71 #define I2C_PORT 0xf0 /* Port mask */
74 #define I2C_CT_AAK 0x01 /* Send AAK */
75 #define I2C_CT_ADDR 0x02 /* Send address(es) */
76 #define I2C_CT_STOP 0x04 /* Send STOP */
77 #define I2C_CT_START 0x08 /* Send START */
80 #define I2C_ST_BUSY 0x01 /* Busy */
81 #define I2C_ST_LASTAAK 0x02 /* Last AAK */
82 #define I2C_ST_LASTRW 0x04 /* Last R/W */
83 #define I2C_ST_SDA 0x08 /* SDA */
84 #define I2C_ST_SCL 0x10 /* SCL */
87 #define I2C_INT_DATA 0x01 /* Data byte sent/received */
88 #define I2C_INT_ADDR 0x02 /* Address sent */
89 #define I2C_INT_STOP 0x04 /* STOP condition sent */
90 #define I2C_INT_START 0x08 /* START condition sent */
94 #define I2C_READING 0x02
95 #define I2C_ERROR 0x04
96 #define I2C_SELECTED 0x08
102 struct resource *sc_reg;
104 struct resource *sc_irq;
113 static int kiic_probe(device_t dev);
114 static int kiic_attach(device_t dev);
115 static void kiic_writereg(struct kiic_softc *sc, u_int, u_int);
116 static u_int kiic_readreg(struct kiic_softc *, u_int);
117 static void kiic_setmode(struct kiic_softc *, u_int);
118 static void kiic_setspeed(struct kiic_softc *, u_int);
119 static void kiic_intr(void *xsc);
120 static int kiic_transfer(device_t dev, struct iic_msg *msgs,
122 static phandle_t kiic_get_node(device_t bus, device_t dev);
124 static device_method_t kiic_methods[] = {
125 /* device interface */
126 DEVMETHOD(device_probe, kiic_probe),
127 DEVMETHOD(device_attach, kiic_attach),
129 /* iicbus interface */
130 DEVMETHOD(iicbus_callback, iicbus_null_callback),
131 DEVMETHOD(iicbus_transfer, kiic_transfer),
133 /* ofw_bus interface */
134 DEVMETHOD(ofw_bus_get_node, kiic_get_node),
139 static driver_t kiic_driver = {
142 sizeof(struct kiic_softc)
144 static devclass_t kiic_devclass;
146 DRIVER_MODULE(kiic, macio, kiic_driver, kiic_devclass, 0, 0);
149 kiic_probe(device_t self)
153 name = ofw_bus_get_name(self);
154 if (name && strcmp(name, "i2c") == 0) {
155 device_set_desc(self, "Keywest I2C controller");
163 kiic_attach(device_t self)
165 struct kiic_softc *sc = device_get_softc(self);
170 bzero(sc, sizeof(*sc));
173 node = ofw_bus_get_node(self);
174 if (node == 0 || node == -1) {
179 sc->sc_reg = bus_alloc_resource_any(self, SYS_RES_MEMORY,
181 if (sc->sc_reg == NULL) {
185 if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
186 device_printf(self, "cannot get i2c-rate\n");
189 if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
190 device_printf(self, "unable to find i2c address step\n");
195 * Some Keywest I2C devices have their children attached directly
196 * underneath them. Some have a single 'iicbus' child with the
197 * devices underneath that. Sort this out, and make sure that the
198 * OFW I2C layer has the correct node.
201 sc->sc_node = OF_child(node);
202 if (OF_getprop(sc->sc_node,"name",name,sizeof(name)) > 0) {
203 if (strcmp(name,"i2c-bus") != 0)
207 mtx_init(&sc->sc_mutex, "kiic", NULL, MTX_DEF);
209 sc->sc_irq = bus_alloc_resource_any(self, SYS_RES_IRQ, &sc->sc_irqrid,
211 bus_setup_intr(self, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE, NULL,
212 kiic_intr, sc, &sc->sc_ih);
214 kiic_writereg(sc, STATUS, 0);
215 kiic_writereg(sc, ISR, 0);
216 kiic_writereg(sc, IER, 0);
218 kiic_setmode(sc, I2C_STDMODE);
219 kiic_setspeed(sc, I2C_100kHz); /* XXX rate */
221 kiic_writereg(sc, IER, I2C_INT_DATA | I2C_INT_ADDR | I2C_INT_STOP);
223 /* Add the IIC bus layer */
224 sc->sc_iicbus = device_add_child(self, "iicbus", -1);
226 return (bus_generic_attach(self));
230 kiic_writereg(struct kiic_softc *sc, u_int reg, u_int val)
232 bus_write_1(sc->sc_reg, sc->sc_regstep * reg, val);
233 DELAY(10); /* register access delay */
237 kiic_readreg(struct kiic_softc *sc, u_int reg)
239 return bus_read_1(sc->sc_reg, sc->sc_regstep * reg);
243 kiic_setmode(struct kiic_softc *sc, u_int mode)
247 KASSERT((mode & ~I2C_MODE) == 0, ("bad mode"));
248 x = kiic_readreg(sc, MODE);
251 kiic_writereg(sc, MODE, x);
255 kiic_setspeed(struct kiic_softc *sc, u_int speed)
259 KASSERT((speed & ~I2C_SPEED) == 0, ("bad speed"));
260 x = kiic_readreg(sc, MODE);
263 kiic_writereg(sc, MODE, x);
269 struct kiic_softc *sc = xsc;
273 mtx_lock(&sc->sc_mutex);
274 isr = kiic_readreg(sc, ISR);
276 if (isr & I2C_INT_ADDR) {
277 sc->sc_flags |= I2C_SELECTED;
279 if (sc->sc_flags & I2C_READING) {
280 if (sc->sc_resid > 1) {
281 x = kiic_readreg(sc, CONTROL);
283 kiic_writereg(sc, CONTROL, x);
286 kiic_writereg(sc, DATA, *sc->sc_data++);
291 if (isr & I2C_INT_DATA) {
292 if (sc->sc_flags & I2C_READING) {
293 if (sc->sc_resid > 0) {
294 *sc->sc_data++ = kiic_readreg(sc, DATA);
299 if (sc->sc_resid == 0) {
300 x = kiic_readreg(sc, CONTROL);
302 kiic_writereg(sc, CONTROL, x);
304 kiic_writereg(sc, DATA, *sc->sc_data++);
310 if (isr & I2C_INT_STOP) {
311 kiic_writereg(sc, CONTROL, 0);
312 sc->sc_flags &= ~I2C_SELECTED;
316 kiic_writereg(sc, ISR, isr);
317 mtx_unlock(&sc->sc_mutex);
321 kiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
323 struct kiic_softc *sc;
327 sc = device_get_softc(dev);
330 mtx_lock(&sc->sc_mutex);
332 if (sc->sc_flags & I2C_BUSY)
333 mtx_sleep(dev, &sc->sc_mutex, 0, "kiic", timo);
335 if (sc->sc_flags & I2C_BUSY) {
336 mtx_unlock(&sc->sc_mutex);
340 sc->sc_flags = I2C_BUSY;
342 for (i = 0; i < nmsgs; i++) {
343 sc->sc_data = msgs[i].buf;
344 sc->sc_resid = msgs[i].len;
345 sc->sc_flags = I2C_BUSY;
346 addr = msgs[i].slave;
347 timo = 1000 + sc->sc_resid * 200;
350 if (msgs[i].flags & IIC_M_RD) {
351 sc->sc_flags |= I2C_READING;
355 kiic_writereg(sc, ADDR, addr);
356 kiic_writereg(sc, SUBADDR, 0x04);
358 x = kiic_readreg(sc, CONTROL) | I2C_CT_ADDR;
359 kiic_writereg(sc, CONTROL, x);
361 err = mtx_sleep(dev, &sc->sc_mutex, 0, "kiic", timo);
363 msgs[i].len -= sc->sc_resid;
365 if ((sc->sc_flags & I2C_ERROR) || err == EWOULDBLOCK) {
366 device_printf(sc->sc_dev, "I2C error\n");
368 mtx_unlock(&sc->sc_mutex);
375 mtx_unlock(&sc->sc_mutex);
381 kiic_get_node(device_t bus, device_t dev)
383 struct kiic_softc *sc;
385 sc = device_get_softc(bus);
386 /* We only have one child, the I2C bus, which needs our own node. */