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