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.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed for the NetBSD Project by
15 * Wasabi Systems, Inc.
16 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
17 * or promote products derived from this software without specific prior
20 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
43 #include <arm/xscale/ixp425/ixp425reg.h>
44 #include <arm/xscale/ixp425/ixp425var.h>
45 #include <arm/xscale/ixp425/ixdp425reg.h>
47 #include <dev/iicbus/iiconf.h>
48 #include <dev/iicbus/iicbus.h>
54 /* bit clr/set shorthands */
55 #define GPIO_CONF_CLR(sc, reg, mask) \
56 GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, reg) &~ (mask))
57 #define GPIO_CONF_SET(sc, reg, mask) \
58 GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, reg) | (mask))
62 bus_space_tag_t sc_iot;
63 bus_space_handle_t sc_gpio_ioh;
68 static struct ixpiic_softc *ixpiic_sc = NULL;
71 ixpiic_probe(device_t dev)
73 device_set_desc(dev, "IXP425 GPIO-Based I2C Interface");
78 ixpiic_attach(device_t dev)
80 struct ixpiic_softc *sc = device_get_softc(dev);
81 struct ixp425_softc *sa = device_get_softc(device_get_parent(dev));
86 sc->sc_iot = sa->sc_iot;
87 sc->sc_gpio_ioh = sa->sc_gpio_ioh;
89 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER,
90 GPIO_I2C_SCL_BIT | GPIO_I2C_SDA_BIT);
91 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR,
92 GPIO_I2C_SCL_BIT | GPIO_I2C_SDA_BIT);
94 /* add generic bit-banging code */
95 if ((sc->iicbb = device_add_child(dev, "iicbb", -1)) == NULL)
96 device_printf(dev, "could not add iicbb\n");
98 /* probe and attach the bit-banging code */
99 device_probe_and_attach(sc->iicbb);
105 ixpiic_callback(device_t dev, int index, caddr_t *data)
111 ixpiic_getscl(device_t dev)
113 struct ixpiic_softc *sc = ixpiic_sc;
116 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
118 reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR);
119 return (reg & GPIO_I2C_SCL_BIT);
123 ixpiic_getsda(device_t dev)
125 struct ixpiic_softc *sc = ixpiic_sc;
128 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
130 reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR);
131 return (reg & GPIO_I2C_SDA_BIT);
135 ixpiic_setsda(device_t dev, char val)
137 struct ixpiic_softc *sc = ixpiic_sc;
139 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR, GPIO_I2C_SDA_BIT);
141 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
143 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
148 ixpiic_setscl(device_t dev, char val)
150 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);
161 ixpiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
164 ixpiic_setsda(dev, 1);
165 ixpiic_setscl(dev, 1);
167 return (IIC_ENOADDR);
170 static device_method_t ixpiic_methods[] = {
171 /* device interface */
172 DEVMETHOD(device_probe, ixpiic_probe),
173 DEVMETHOD(device_attach, ixpiic_attach),
175 /* iicbb interface */
176 DEVMETHOD(iicbb_callback, ixpiic_callback),
177 DEVMETHOD(iicbb_setsda, ixpiic_setsda),
178 DEVMETHOD(iicbb_setscl, ixpiic_setscl),
179 DEVMETHOD(iicbb_getsda, ixpiic_getsda),
180 DEVMETHOD(iicbb_getscl, ixpiic_getscl),
181 DEVMETHOD(iicbb_reset, ixpiic_reset),
186 static driver_t ixpiic_driver = {
189 sizeof(struct ixpiic_softc),
191 static devclass_t ixpiic_devclass;
193 DRIVER_MODULE(ixpiic, ixp, ixpiic_driver, ixpiic_devclass, 0, 0);