nslu2_iic.c revision 1.3.14.2 1 1.3.14.2 yamt /* $NetBSD: nslu2_iic.c,v 1.3.14.2 2008/06/17 09:13:59 yamt Exp $ */
2 1.1 scw
3 1.1 scw /*-
4 1.1 scw * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1 scw * All rights reserved.
6 1.1 scw *
7 1.1 scw * This code is derived from software contributed to The NetBSD Foundation
8 1.1 scw * by Steve C. Woodford.
9 1.1 scw *
10 1.1 scw * Redistribution and use in source and binary forms, with or without
11 1.1 scw * modification, are permitted provided that the following conditions
12 1.1 scw * are met:
13 1.1 scw * 1. Redistributions of source code must retain the above copyright
14 1.1 scw * notice, this list of conditions and the following disclaimer.
15 1.1 scw * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 scw * notice, this list of conditions and the following disclaimer in the
17 1.1 scw * documentation and/or other materials provided with the distribution.
18 1.1 scw *
19 1.1 scw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 scw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 scw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 scw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 scw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 scw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 scw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 scw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 scw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 scw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 scw * POSSIBILITY OF SUCH DAMAGE.
30 1.1 scw */
31 1.1 scw
32 1.1 scw #include <sys/param.h>
33 1.1 scw #include <sys/systm.h>
34 1.1 scw #include <sys/kernel.h>
35 1.1 scw #include <sys/device.h>
36 1.3 ad #include <sys/mutex.h>
37 1.3 ad #include <sys/bus.h>
38 1.1 scw
39 1.1 scw #include <dev/i2c/i2cvar.h>
40 1.1 scw #include <dev/i2c/i2c_bitbang.h>
41 1.1 scw
42 1.1 scw #include <arm/xscale/ixp425reg.h>
43 1.1 scw #include <arm/xscale/ixp425var.h>
44 1.1 scw
45 1.1 scw #include <evbarm/nslu2/nslu2reg.h>
46 1.1 scw
47 1.1 scw struct slugiic_softc {
48 1.1 scw struct device sc_dev;
49 1.1 scw struct i2c_controller sc_ic;
50 1.1 scw struct i2c_bitbang_ops sc_ibo;
51 1.3 ad kmutex_t sc_lock;
52 1.1 scw uint32_t sc_dirout;
53 1.1 scw };
54 1.1 scw
55 1.1 scw static int
56 1.1 scw slugiic_acquire_bus(void *arg, int flags)
57 1.1 scw {
58 1.1 scw struct slugiic_softc *sc = arg;
59 1.1 scw
60 1.1 scw if (flags & I2C_F_POLL)
61 1.1 scw return (0);
62 1.1 scw
63 1.3 ad mutex_enter(&sc->sc_lock);
64 1.3 ad return (0);
65 1.1 scw }
66 1.1 scw
67 1.1 scw static void
68 1.1 scw slugiic_release_bus(void *arg, int flags)
69 1.1 scw {
70 1.1 scw struct slugiic_softc *sc = arg;
71 1.1 scw
72 1.1 scw if (flags & I2C_F_POLL)
73 1.1 scw return;
74 1.1 scw
75 1.3 ad mutex_exit(&sc->sc_lock);
76 1.1 scw }
77 1.1 scw
78 1.1 scw static int
79 1.1 scw slugiic_send_start(void *arg, int flags)
80 1.1 scw {
81 1.1 scw struct slugiic_softc *sc = arg;
82 1.1 scw
83 1.1 scw return (i2c_bitbang_send_start(sc, flags, &sc->sc_ibo));
84 1.1 scw }
85 1.1 scw
86 1.1 scw static int
87 1.1 scw slugiic_send_stop(void *arg, int flags)
88 1.1 scw {
89 1.1 scw struct slugiic_softc *sc = arg;
90 1.1 scw
91 1.1 scw return (i2c_bitbang_send_stop(sc, flags, &sc->sc_ibo));
92 1.1 scw }
93 1.1 scw
94 1.1 scw static int
95 1.1 scw slugiic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
96 1.1 scw {
97 1.1 scw struct slugiic_softc *sc = arg;
98 1.1 scw
99 1.1 scw return (i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_ibo));
100 1.1 scw }
101 1.1 scw
102 1.1 scw static int
103 1.1 scw slugiic_read_byte(void *arg, uint8_t *vp, int flags)
104 1.1 scw {
105 1.1 scw struct slugiic_softc *sc = arg;
106 1.1 scw
107 1.1 scw return (i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_ibo));
108 1.1 scw }
109 1.1 scw
110 1.1 scw static int
111 1.1 scw slugiic_write_byte(void *arg, uint8_t v, int flags)
112 1.1 scw {
113 1.1 scw struct slugiic_softc *sc = arg;
114 1.1 scw
115 1.1 scw return (i2c_bitbang_write_byte(sc, v, flags, &sc->sc_ibo));
116 1.1 scw }
117 1.1 scw
118 1.1 scw static void
119 1.1 scw slugiic_set_dir(void *arg, uint32_t bits)
120 1.1 scw {
121 1.1 scw struct slugiic_softc *sc = arg;
122 1.3.14.2 yamt uint32_t reg;
123 1.3.14.2 yamt int s;
124 1.3.14.2 yamt
125 1.3.14.2 yamt if (sc->sc_dirout == bits)
126 1.3.14.2 yamt return;
127 1.3.14.2 yamt
128 1.3.14.2 yamt s = splhigh();
129 1.1 scw
130 1.3.14.1 yamt sc->sc_dirout = bits;
131 1.3.14.2 yamt
132 1.3.14.2 yamt if (sc->sc_dirout) {
133 1.3.14.2 yamt /* SDA is output; enable SDA output if SDA OUTR is low */
134 1.3.14.2 yamt reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
135 1.3.14.2 yamt if ((reg & GPIO_I2C_SDA_BIT) == 0) {
136 1.3.14.2 yamt reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
137 1.3.14.2 yamt reg |= GPIO_I2C_SDA_BIT;
138 1.3.14.2 yamt GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, reg);
139 1.3.14.2 yamt }
140 1.3.14.2 yamt } else {
141 1.3.14.2 yamt /* SDA is input; disable SDA output */
142 1.3.14.2 yamt reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
143 1.3.14.2 yamt reg &= ~GPIO_I2C_SDA_BIT;
144 1.3.14.2 yamt GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, reg);
145 1.3.14.2 yamt }
146 1.3.14.2 yamt
147 1.3.14.2 yamt splx(s);
148 1.1 scw }
149 1.1 scw
150 1.1 scw static void
151 1.1 scw slugiic_set_bits(void *arg, uint32_t bits)
152 1.1 scw {
153 1.3.14.1 yamt struct slugiic_softc *sc = arg;
154 1.3.14.2 yamt uint32_t oer, outr;
155 1.3.14.2 yamt int s;
156 1.1 scw
157 1.1 scw s = splhigh();
158 1.1 scw
159 1.3.14.1 yamt /*
160 1.3.14.2 yamt * Enable SCL output if the SCL line is to be driven low.
161 1.3.14.2 yamt * Enable SDA output if the SDA line is to be driven low and
162 1.3.14.2 yamt * SDA direction is output.
163 1.3.14.2 yamt * Otherwise switch them to input even if directions are output
164 1.3.14.2 yamt * so that we can emulate open collector output with the pullup
165 1.3.14.2 yamt * resistors.
166 1.3.14.2 yamt * If lines are to be set to high, disable OER first then set OUTR.
167 1.3.14.2 yamt * If lines are to be set to low, set OUTR first then enable OER.
168 1.3.14.1 yamt */
169 1.3.14.2 yamt oer = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
170 1.3.14.2 yamt if ((bits & GPIO_I2C_SCL_BIT) != 0)
171 1.3.14.2 yamt oer &= ~GPIO_I2C_SCL_BIT;
172 1.3.14.2 yamt if ((bits & GPIO_I2C_SDA_BIT) != 0)
173 1.3.14.2 yamt oer &= ~GPIO_I2C_SDA_BIT;
174 1.3.14.2 yamt GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, oer);
175 1.3.14.2 yamt
176 1.3.14.2 yamt outr = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
177 1.3.14.2 yamt outr &= ~(GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT);
178 1.3.14.2 yamt GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, outr | bits);
179 1.3.14.2 yamt
180 1.3.14.2 yamt if ((bits & GPIO_I2C_SCL_BIT) == 0)
181 1.3.14.2 yamt oer |= GPIO_I2C_SCL_BIT;
182 1.3.14.2 yamt if ((bits & GPIO_I2C_SDA_BIT) == 0 && sc->sc_dirout)
183 1.3.14.2 yamt oer |= GPIO_I2C_SDA_BIT;
184 1.3.14.2 yamt GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, oer);
185 1.3.14.1 yamt
186 1.1 scw splx(s);
187 1.1 scw }
188 1.1 scw
189 1.1 scw static uint32_t
190 1.1 scw slugiic_read_bits(void *arg)
191 1.1 scw {
192 1.1 scw uint32_t reg;
193 1.1 scw
194 1.1 scw reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPINR);
195 1.1 scw return (reg & (GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT));
196 1.1 scw }
197 1.1 scw
198 1.1 scw static void
199 1.1 scw slugiic_deferred_attach(struct device *device)
200 1.1 scw {
201 1.1 scw struct slugiic_softc *sc = (struct slugiic_softc *)device;
202 1.1 scw struct i2cbus_attach_args iba;
203 1.1 scw uint32_t reg;
204 1.1 scw
205 1.1 scw reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
206 1.1 scw reg |= GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT;
207 1.1 scw GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
208 1.1 scw
209 1.1 scw reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
210 1.1 scw reg &= ~GPIO_I2C_SCL_BIT;
211 1.1 scw reg |= GPIO_I2C_SDA_BIT;
212 1.1 scw GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, reg);
213 1.1 scw
214 1.1 scw iba.iba_tag = &sc->sc_ic;
215 1.2 drochner (void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
216 1.1 scw }
217 1.1 scw
218 1.1 scw static int
219 1.1 scw slugiic_match(struct device *parent, struct cfdata *cf, void *arg)
220 1.1 scw {
221 1.1 scw
222 1.1 scw return (1);
223 1.1 scw }
224 1.1 scw
225 1.1 scw static void
226 1.1 scw slugiic_attach(struct device *parent, struct device *self, void *arg)
227 1.1 scw {
228 1.1 scw struct slugiic_softc *sc = (struct slugiic_softc *)self;
229 1.1 scw
230 1.1 scw aprint_naive("\n");
231 1.1 scw aprint_normal(": I2C bus\n");
232 1.1 scw
233 1.1 scw sc->sc_ic.ic_cookie = sc;
234 1.1 scw sc->sc_ic.ic_acquire_bus = slugiic_acquire_bus;
235 1.1 scw sc->sc_ic.ic_release_bus = slugiic_release_bus;
236 1.1 scw sc->sc_ic.ic_exec = NULL;
237 1.1 scw sc->sc_ic.ic_send_start = slugiic_send_start;
238 1.1 scw sc->sc_ic.ic_send_stop = slugiic_send_stop;
239 1.1 scw sc->sc_ic.ic_initiate_xfer = slugiic_initiate_xfer;
240 1.1 scw sc->sc_ic.ic_read_byte = slugiic_read_byte;
241 1.1 scw sc->sc_ic.ic_write_byte = slugiic_write_byte;
242 1.1 scw
243 1.1 scw sc->sc_ibo.ibo_set_dir = slugiic_set_dir;
244 1.1 scw sc->sc_ibo.ibo_set_bits = slugiic_set_bits;
245 1.1 scw sc->sc_ibo.ibo_read_bits = slugiic_read_bits;
246 1.1 scw sc->sc_ibo.ibo_bits[I2C_BIT_SDA] = GPIO_I2C_SDA_BIT;
247 1.1 scw sc->sc_ibo.ibo_bits[I2C_BIT_SCL] = GPIO_I2C_SCL_BIT;
248 1.3.14.1 yamt sc->sc_ibo.ibo_bits[I2C_BIT_OUTPUT] = 1;
249 1.3.14.1 yamt sc->sc_ibo.ibo_bits[I2C_BIT_INPUT] = 0;
250 1.1 scw
251 1.3 ad mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
252 1.1 scw
253 1.1 scw sc->sc_dirout = 0;
254 1.1 scw
255 1.1 scw /*
256 1.1 scw * Defer until ixp425_softc has been initialised
257 1.1 scw */
258 1.1 scw config_interrupts(self, slugiic_deferred_attach);
259 1.1 scw }
260 1.1 scw
261 1.1 scw CFATTACH_DECL(slugiic, sizeof(struct slugiic_softc),
262 1.1 scw slugiic_match, slugiic_attach, NULL, NULL);
263