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