1 /*
2 * Copyright (c) 2006 Kevin Lo. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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 *
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.
24 */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/uio.h>
35
36 #include <arm/xscale/ixp425/ixp425reg.h>
37 #include <arm/xscale/ixp425/ixp425var.h>
38 #include <arm/xscale/ixp425/ixdp425reg.h>
39
40 #include <dev/iicbus/iiconf.h>
41 #include <dev/iicbus/iicbus.h>
42
43 #include "iicbb_if.h"
44
45 #define I2C_DELAY 10
46
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))
52
53 struct ixpiic_softc {
54 device_t sc_dev;
55 bus_space_tag_t sc_iot;
56 bus_space_handle_t sc_gpio_ioh;
57
58 device_t iicbb;
59 };
60
61 static struct ixpiic_softc *ixpiic_sc = NULL;
62
63 static int
64 ixpiic_probe(device_t dev)
65 {
66 device_set_desc(dev, "IXP425 GPIO-Based I2C Interface");
67 return (0);
68 }
69
70 static int
71 ixpiic_attach(device_t dev)
72 {
73 struct ixpiic_softc *sc = device_get_softc(dev);
74 struct ixp425_softc *sa = device_get_softc(device_get_parent(dev));
75
76 ixpiic_sc = sc;
77
78 sc->sc_dev = dev;
79 sc->sc_iot = sa->sc_iot;
80 sc->sc_gpio_ioh = sa->sc_gpio_ioh;
81
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);
86
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");
90
91 /* probe and attach the bit-banging code */
92 device_probe_and_attach(sc->iicbb);
93
94 return (0);
95 }
96
97 static int
98 ixpiic_callback(device_t dev, int index, caddr_t *data)
99 {
100 return (0);
101 }
102
103 static int
104 ixpiic_getscl(device_t dev)
105 {
106 struct ixpiic_softc *sc = ixpiic_sc;
107 uint32_t reg;
108
109 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
110
111 reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR);
112 return (reg & GPIO_I2C_SCL_BIT);
113 }
114
115 static int
116 ixpiic_getsda(device_t dev)
117 {
118 struct ixpiic_softc *sc = ixpiic_sc;
119 uint32_t reg;
120
121 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
122
123 reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR);
124 return (reg & GPIO_I2C_SDA_BIT);
125 }
126
127 static void
128 ixpiic_setsda(device_t dev, char val)
129 {
130 struct ixpiic_softc *sc = ixpiic_sc;
131
132 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR, GPIO_I2C_SDA_BIT);
133 if (val)
134 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
135 else
136 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
137 DELAY(I2C_DELAY);
138 }
139
140 static void
141 ixpiic_setscl(device_t dev, char val)
142 {
143 struct ixpiic_softc *sc = ixpiic_sc;
144
145 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR, GPIO_I2C_SCL_BIT);
146 if (val)
147 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
148 else
149 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
150 DELAY(I2C_DELAY);
151 }
152
153 static int
154 ixpiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
155 {
156 /* reset bus */
157 ixpiic_setsda(dev, 1);
158 ixpiic_setscl(dev, 1);
159
160 return (IIC_ENOADDR);
161 }
162
163 static device_method_t ixpiic_methods[] = {
164 /* device interface */
165 DEVMETHOD(device_probe, ixpiic_probe),
166 DEVMETHOD(device_attach, ixpiic_attach),
167
168 /* iicbb interface */
169 DEVMETHOD(iicbb_callback, ixpiic_callback),
170 DEVMETHOD(iicbb_setsda, ixpiic_setsda),
171 DEVMETHOD(iicbb_setscl, ixpiic_setscl),
172 DEVMETHOD(iicbb_getsda, ixpiic_getsda),
173 DEVMETHOD(iicbb_getscl, ixpiic_getscl),
174 DEVMETHOD(iicbb_reset, ixpiic_reset),
175
176 { 0, 0 }
177 };
178
179 static driver_t ixpiic_driver = {
180 "ixpiic",
181 ixpiic_methods,
182 sizeof(struct ixpiic_softc),
183 };
184 static devclass_t ixpiic_devclass;
185
186 DRIVER_MODULE(ixpiic, ixp, ixpiic_driver, ixpiic_devclass, 0, 0);
Cache object: b597d2d4ef0341ed1c0cb4f581ddfdb2
|