2 * Copyright (c) 2006 Kevin Lo. 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.
13 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 * POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
36 #include <arm/xscale/ixp425/ixp425reg.h>
37 #include <arm/xscale/ixp425/ixp425var.h>
38 #include <arm/xscale/ixp425/ixdp425reg.h>
40 #include <dev/iicbus/iiconf.h>
41 #include <dev/iicbus/iicbus.h>
47 /* bit clr/set shorthands */
48 #define GPIO_CONF_CLR(sc, reg, mask) \
49 GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, reg) &~ (mask))
50 #define GPIO_CONF_SET(sc, reg, mask) \
51 GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, reg) | (mask))
55 bus_space_tag_t sc_iot;
56 bus_space_handle_t sc_gpio_ioh;
61 static struct ixpiic_softc *ixpiic_sc = NULL;
64 ixpiic_probe(device_t dev)
66 device_set_desc(dev, "IXP4XX GPIO-Based I2C Interface");
71 ixpiic_attach(device_t dev)
73 struct ixpiic_softc *sc = device_get_softc(dev);
74 struct ixp425_softc *sa = device_get_softc(device_get_parent(dev));
79 sc->sc_iot = sa->sc_iot;
80 sc->sc_gpio_ioh = sa->sc_gpio_ioh;
82 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER,
83 GPIO_I2C_SCL_BIT | GPIO_I2C_SDA_BIT);
84 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR,
85 GPIO_I2C_SCL_BIT | GPIO_I2C_SDA_BIT);
87 /* add generic bit-banging code */
88 if ((sc->iicbb = device_add_child(dev, "iicbb", -1)) == NULL)
89 device_printf(dev, "could not add iicbb\n");
91 /* probe and attach the bit-banging code */
92 device_probe_and_attach(sc->iicbb);
98 ixpiic_callback(device_t dev, int index, caddr_t data)
104 ixpiic_getscl(device_t dev)
106 struct ixpiic_softc *sc = ixpiic_sc;
110 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
112 reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR);
113 IXP4XX_GPIO_UNLOCK();
114 return (reg & GPIO_I2C_SCL_BIT);
118 ixpiic_getsda(device_t dev)
120 struct ixpiic_softc *sc = ixpiic_sc;
124 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
126 reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR);
127 IXP4XX_GPIO_UNLOCK();
128 return (reg & GPIO_I2C_SDA_BIT);
132 ixpiic_setsda(device_t dev, int val)
134 struct ixpiic_softc *sc = ixpiic_sc;
137 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR, GPIO_I2C_SDA_BIT);
139 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
141 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
142 IXP4XX_GPIO_UNLOCK();
147 ixpiic_setscl(device_t dev, int val)
149 struct ixpiic_softc *sc = ixpiic_sc;
152 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR, GPIO_I2C_SCL_BIT);
154 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
156 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
157 IXP4XX_GPIO_UNLOCK();
162 ixpiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
165 ixpiic_setsda(dev, 1);
166 ixpiic_setscl(dev, 1);
168 return (IIC_ENOADDR);
171 static device_method_t ixpiic_methods[] = {
172 /* device interface */
173 DEVMETHOD(device_probe, ixpiic_probe),
174 DEVMETHOD(device_attach, ixpiic_attach),
176 /* iicbb interface */
177 DEVMETHOD(iicbb_callback, ixpiic_callback),
178 DEVMETHOD(iicbb_setsda, ixpiic_setsda),
179 DEVMETHOD(iicbb_setscl, ixpiic_setscl),
180 DEVMETHOD(iicbb_getsda, ixpiic_getsda),
181 DEVMETHOD(iicbb_getscl, ixpiic_getscl),
182 DEVMETHOD(iicbb_reset, ixpiic_reset),
187 static driver_t ixpiic_driver = {
190 sizeof(struct ixpiic_softc),
192 static devclass_t ixpiic_devclass;
194 DRIVER_MODULE(ixpiic, ixp, ixpiic_driver, ixpiic_devclass, 0, 0);
195 DRIVER_MODULE(iicbb, ixpiic, iicbb_driver, iicbb_devclass, 0, 0);