ralink_i2c.c revision 1.3 1 1.3 chs /* $NetBSD: ralink_i2c.c,v 1.3 2012/10/27 17:18:02 chs Exp $ */
2 1.2 matt /*-
3 1.2 matt * Copyright (c) 2011 CradlePoint Technology, Inc.
4 1.2 matt * All rights reserved.
5 1.2 matt *
6 1.2 matt *
7 1.2 matt * Redistribution and use in source and binary forms, with or without
8 1.2 matt * modification, are permitted provided that the following conditions
9 1.2 matt * are met:
10 1.2 matt * 1. Redistributions of source code must retain the above copyright
11 1.2 matt * notice, this list of conditions and the following disclaimer.
12 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 matt * notice, this list of conditions and the following disclaimer in the
14 1.2 matt * documentation and/or other materials provided with the distribution.
15 1.2 matt *
16 1.2 matt * THIS SOFTWARE IS PROVIDED BY CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS
17 1.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20 1.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2 matt * POSSIBILITY OF SUCH DAMAGE.
27 1.2 matt */
28 1.2 matt
29 1.2 matt /* ra_i2c.c - Ralink i2c 3052 driver */
30 1.2 matt
31 1.2 matt #include <sys/cdefs.h>
32 1.3 chs __KERNEL_RCSID(0, "$NetBSD: ralink_i2c.c,v 1.3 2012/10/27 17:18:02 chs Exp $");
33 1.2 matt
34 1.2 matt #include <sys/param.h>
35 1.2 matt #include <sys/bus.h>
36 1.2 matt #include <sys/device.h>
37 1.2 matt #include <sys/errno.h>
38 1.2 matt #include <sys/kernel.h>
39 1.2 matt #include <sys/malloc.h>
40 1.2 matt #include <sys/proc.h>
41 1.2 matt #include <sys/systm.h>
42 1.2 matt
43 1.2 matt #include <dev/i2c/i2cvar.h>
44 1.2 matt
45 1.2 matt #include <mips/ralink/ralink_var.h>
46 1.2 matt #include <mips/ralink/ralink_reg.h>
47 1.2 matt
48 1.2 matt #if 0
49 1.2 matt /*
50 1.2 matt * Defined for the Ralink 3050, w/320MHz CPU: milage may vary.
51 1.2 matt * Set the I2C clock to 100K bps (low speed) transfer rate.
52 1.2 matt * Value is based upon the forms defined in the Ralink reference document
53 1.2 matt * for RT3050, page 53. JCL.
54 1.2 matt */
55 1.2 matt #define CLKDIV_VALUE 533
56 1.2 matt #endif
57 1.2 matt
58 1.2 matt /*
59 1.2 matt * Slow the I2C bus clock to 12.5 KHz to work around the misbehavior
60 1.2 matt * of the TI part.
61 1.2 matt */
62 1.2 matt #define CLKDIV_VALUE 4264
63 1.2 matt
64 1.2 matt #define i2c_busy_loop (clkdiv*30)
65 1.2 matt #define max_ee_busy_loop (clkdiv*25)
66 1.2 matt
67 1.2 matt
68 1.2 matt typedef struct ra_i2c_softc {
69 1.2 matt device_t sc_dev;
70 1.2 matt struct i2c_controller sc_i2c;
71 1.2 matt bus_space_tag_t sc_memt;
72 1.2 matt bus_space_handle_t sc_i2c_memh;
73 1.2 matt bus_space_handle_t sc_sy_memh;
74 1.2 matt } ra_i2c_softc_t;
75 1.2 matt
76 1.2 matt
77 1.3 chs static int ra_i2c_match(device_t, cfdata_t, void *);
78 1.3 chs static void ra_i2c_attach(device_t, device_t, void *);
79 1.2 matt
80 1.2 matt /* RT3052 I2C functions */
81 1.2 matt static int i2c_write(ra_i2c_softc_t *, u_long, const u_char *, u_long);
82 1.2 matt static int i2c_read(ra_i2c_softc_t *, u_long, u_char *, u_long);
83 1.2 matt #ifdef NOTYET
84 1.2 matt static void i2c_write_stop(ra_i2c_softc_t *, u_long, u_char *);
85 1.2 matt static void i2c_read_stop(ra_i2c_softc_t *, u_long, u_char *);
86 1.2 matt #endif
87 1.2 matt
88 1.2 matt /* i2c driver functions */
89 1.2 matt int ra_i2c_acquire_bus(void *, int);
90 1.2 matt void ra_i2c_release_bus(void *, int);
91 1.2 matt int ra_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
92 1.2 matt void *, size_t, int);
93 1.2 matt void ra_i2c_reset(ra_i2c_softc_t *);
94 1.2 matt
95 1.2 matt CFATTACH_DECL_NEW(ri2c, sizeof(struct ra_i2c_softc),
96 1.2 matt ra_i2c_match, ra_i2c_attach, NULL, NULL);
97 1.2 matt
98 1.2 matt unsigned int clkdiv = CLKDIV_VALUE;
99 1.2 matt
100 1.2 matt static inline void
101 1.2 matt ra_i2c_busy_wait(ra_i2c_softc_t *sc)
102 1.2 matt {
103 1.2 matt for (int i=0; i < i2c_busy_loop; i++) {
104 1.2 matt uint32_t r;
105 1.2 matt r = bus_space_read_4(sc->sc_memt, sc->sc_i2c_memh,
106 1.2 matt RA_I2C_STATUS);
107 1.2 matt if ((r & I2C_STATUS_BUSY) == 0)
108 1.2 matt break;
109 1.2 matt }
110 1.2 matt }
111 1.2 matt
112 1.2 matt int
113 1.2 matt ra_i2c_match(device_t parent, cfdata_t cf, void *aux)
114 1.2 matt {
115 1.2 matt return 1;
116 1.2 matt }
117 1.2 matt
118 1.2 matt
119 1.2 matt void
120 1.2 matt ra_i2c_attach(device_t parent, device_t self, void *aux)
121 1.2 matt {
122 1.2 matt ra_i2c_softc_t * const sc = device_private(self);
123 1.2 matt const struct mainbus_attach_args *ma = aux;
124 1.2 matt struct i2cbus_attach_args iba;
125 1.2 matt uint32_t r;
126 1.2 matt int error;
127 1.2 matt
128 1.2 matt aprint_naive(": Ralink I2C controller\n");
129 1.2 matt aprint_normal(": Ralink I2C controller\n");
130 1.2 matt
131 1.2 matt /* save out bus space tag */
132 1.2 matt sc->sc_memt = ma->ma_memt;
133 1.2 matt
134 1.2 matt /* Map Sysctl registers */
135 1.2 matt if ((error = bus_space_map(ma->ma_memt, RA_SYSCTL_BASE, 0x10000,
136 1.2 matt 0, &sc->sc_sy_memh)) != 0) {
137 1.2 matt aprint_error_dev(self, "unable to map Sysctl registers, "
138 1.2 matt "error=%d\n", error);
139 1.2 matt return;
140 1.2 matt }
141 1.2 matt
142 1.2 matt /* map the I2C registers */
143 1.2 matt if ((error = bus_space_map(sc->sc_memt, RA_I2C_BASE, 0x100,
144 1.2 matt 0, &sc->sc_i2c_memh)) != 0) {
145 1.2 matt aprint_error_dev(self, "unable to map registers, "
146 1.2 matt "error=%d\n", error);
147 1.2 matt bus_space_unmap(ma->ma_memt, sc->sc_sy_memh, 0x10000);
148 1.2 matt return;
149 1.2 matt }
150 1.2 matt
151 1.2 matt /* Enable I2C block */
152 1.2 matt r = bus_space_read_4(sc->sc_memt, sc->sc_sy_memh,
153 1.2 matt RA_SYSCTL_GPIOMODE);
154 1.2 matt r &= ~GPIOMODE_I2C;
155 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_sy_memh,
156 1.2 matt RA_SYSCTL_GPIOMODE, r);
157 1.2 matt
158 1.2 matt sc->sc_i2c.ic_cookie = sc;
159 1.2 matt sc->sc_i2c.ic_acquire_bus = ra_i2c_acquire_bus;
160 1.2 matt sc->sc_i2c.ic_release_bus = ra_i2c_release_bus;
161 1.2 matt sc->sc_i2c.ic_exec = ra_i2c_exec;
162 1.2 matt
163 1.2 matt memset(&iba, 0, sizeof(iba));
164 1.2 matt iba.iba_type = I2C_TYPE_SMBUS;
165 1.2 matt iba.iba_tag = &sc->sc_i2c;
166 1.2 matt config_found(self, &iba, iicbus_print);
167 1.2 matt }
168 1.2 matt
169 1.2 matt
170 1.2 matt
171 1.2 matt /*
172 1.2 matt * I2C API
173 1.2 matt */
174 1.2 matt
175 1.2 matt /* Might not be needed. JCL. */
176 1.2 matt int
177 1.2 matt ra_i2c_acquire_bus(void *cookie, int flags)
178 1.2 matt {
179 1.2 matt /* nothing */
180 1.2 matt return 0;
181 1.2 matt }
182 1.2 matt
183 1.2 matt
184 1.2 matt
185 1.2 matt /* Might not be needed. JCL. */
186 1.2 matt void
187 1.2 matt ra_i2c_release_bus(void *cookie, int flags)
188 1.2 matt {
189 1.2 matt /* nothing */
190 1.2 matt }
191 1.2 matt
192 1.2 matt int
193 1.2 matt ra_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
194 1.2 matt size_t cmdlen, void *buf, size_t len, int flags)
195 1.2 matt {
196 1.2 matt ra_i2c_softc_t * const sc = cookie;
197 1.2 matt
198 1.2 matt /*
199 1.2 matt * Make sure we only pass a seven-bit device address,
200 1.2 matt * as per I2C standard.
201 1.2 matt */
202 1.2 matt KASSERT(addr <= 127);
203 1.2 matt if (addr > 127)
204 1.2 matt return -1;
205 1.2 matt
206 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_DEVADDR,
207 1.2 matt addr);
208 1.2 matt
209 1.2 matt ra_i2c_reset(sc);
210 1.2 matt
211 1.2 matt /*
212 1.2 matt * Process the requested operation.
213 1.2 matt * There are four I2C operations of interest:
214 1.2 matt * - Write
215 1.2 matt * - Write with stop
216 1.2 matt * - Read
217 1.2 matt * - Read with stop
218 1.2 matt * Because the I2C block on the Ralink part generates stop markers
219 1.2 matt * at approprite places, based upon the byte count, the read and write
220 1.2 matt * with stop operations will rarely be needed. They are included here
221 1.2 matt * as placeholders, but haven't been implemented or tested.
222 1.2 matt */
223 1.2 matt switch(op) {
224 1.2 matt case I2C_OP_WRITE:
225 1.2 matt return i2c_write(sc, addr, cmdbuf, cmdlen);
226 1.2 matt break;
227 1.2 matt case I2C_OP_READ:
228 1.2 matt return i2c_read(sc, addr, buf, len);
229 1.2 matt break;
230 1.2 matt #ifdef NOTYET
231 1.2 matt case I2C_OP_WRITE_WITH_STOP:
232 1.2 matt i2c_write_stop(sc, addr, buf);
233 1.2 matt break;
234 1.2 matt case I2C_OP_READ_WITH_STOP:
235 1.2 matt i2c_read_stop(sc, addr, buf);
236 1.2 matt break;
237 1.2 matt #endif
238 1.2 matt default:
239 1.2 matt return -1; /* Illegal operation, error return. */
240 1.2 matt }
241 1.2 matt
242 1.2 matt return 0;
243 1.2 matt }
244 1.2 matt
245 1.2 matt static int
246 1.2 matt i2c_write(ra_i2c_softc_t *sc, u_long addr, const u_char *data,
247 1.2 matt u_long nbytes)
248 1.2 matt {
249 1.2 matt uint32_t r;
250 1.2 matt int i, j;
251 1.2 matt
252 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_DEVADDR,
253 1.2 matt addr);
254 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_BYTECNT,
255 1.2 matt nbytes - 1);
256 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_STARTXFR,
257 1.2 matt I2C_OP_WRITE);
258 1.2 matt
259 1.2 matt for (i=0; i < nbytes; i++) {
260 1.2 matt for (j=0; j < max_ee_busy_loop; j++) {
261 1.2 matt r = bus_space_read_4(sc->sc_memt, sc->sc_i2c_memh,
262 1.2 matt RA_I2C_STATUS);
263 1.2 matt if ((r & I2C_STATUS_SDOEMPTY) != 0) {
264 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh,
265 1.2 matt RA_I2C_DATAOUT, data[i]);
266 1.2 matt break;
267 1.2 matt }
268 1.2 matt }
269 1.2 matt #if 0
270 1.2 matt if ((r & I2C_STATUS_ACKERR) != 0) {
271 1.2 matt aprint_error_dev(sc->sc_dev, "ACK error in %s\n",
272 1.2 matt __func__);
273 1.2 matt return EAGAIN;
274 1.2 matt }
275 1.2 matt #endif
276 1.2 matt if (j == max_ee_busy_loop) {
277 1.2 matt aprint_error_dev(sc->sc_dev, "timeout error in %s\n",
278 1.2 matt __func__);
279 1.2 matt return EAGAIN;
280 1.2 matt }
281 1.2 matt }
282 1.2 matt
283 1.2 matt ra_i2c_busy_wait(sc);
284 1.2 matt
285 1.2 matt return 0;
286 1.2 matt }
287 1.2 matt
288 1.2 matt
289 1.2 matt static int
290 1.2 matt i2c_read(ra_i2c_softc_t *sc, u_long addr, u_char *data, u_long nbytes)
291 1.2 matt {
292 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_DEVADDR,
293 1.2 matt addr);
294 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_BYTECNT,
295 1.2 matt nbytes - 1);
296 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_STARTXFR,
297 1.2 matt I2C_OP_READ);
298 1.2 matt
299 1.2 matt for (u_int i = 0; i < nbytes; i++) {
300 1.2 matt u_long j;
301 1.2 matt uint32_t r;
302 1.2 matt
303 1.2 matt for (j=0; j < max_ee_busy_loop; j++) {
304 1.2 matt r = bus_space_read_4(sc->sc_memt, sc->sc_i2c_memh,
305 1.2 matt RA_I2C_STATUS);
306 1.2 matt if ((r & I2C_STATUS_DATARDY) != 0) {
307 1.2 matt data[i] = bus_space_read_4(
308 1.2 matt sc->sc_memt, sc->sc_i2c_memh,
309 1.2 matt RA_I2C_DATAIN);
310 1.2 matt break;
311 1.2 matt }
312 1.2 matt }
313 1.2 matt #if 0
314 1.2 matt if ((r & I2C_STATUS_ACKERR) != 0) {
315 1.2 matt aprint_error_dev(sc->sc_dev, "ACK error in %s\n",
316 1.2 matt __func__);
317 1.2 matt return EAGAIN;
318 1.2 matt }
319 1.2 matt #endif
320 1.2 matt if (j == max_ee_busy_loop) {
321 1.2 matt aprint_error_dev(sc->sc_dev, "timeout error in %s\n",
322 1.2 matt __func__);
323 1.2 matt return EAGAIN;
324 1.2 matt }
325 1.2 matt }
326 1.2 matt
327 1.2 matt ra_i2c_busy_wait(sc);
328 1.2 matt
329 1.2 matt return 0;
330 1.2 matt
331 1.2 matt }
332 1.2 matt
333 1.2 matt
334 1.2 matt #ifdef NOTYET
335 1.2 matt static void
336 1.2 matt i2c_write_stop(ra_i2c_softc_t *sc, u_long address, u_char *data)
337 1.2 matt {
338 1.2 matt /* unimplemented */
339 1.2 matt }
340 1.2 matt
341 1.2 matt static void
342 1.2 matt i2c_read_stop(ra_i2c_softc_t *sc, u_long address, u_char *data)
343 1.2 matt {
344 1.2 matt /* unimplemented */
345 1.2 matt }
346 1.2 matt #endif
347 1.2 matt
348 1.2 matt void
349 1.2 matt ra_i2c_reset(ra_i2c_softc_t *sc)
350 1.2 matt {
351 1.2 matt uint32_t r;
352 1.2 matt
353 1.2 matt /* reset i2c block */
354 1.2 matt r = bus_space_read_4(sc->sc_memt, sc->sc_sy_memh, RA_SYSCTL_RST);
355 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_sy_memh, RA_SYSCTL_RST,
356 1.2 matt r | RST_I2C);
357 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_sy_memh, RA_SYSCTL_RST, r);
358 1.2 matt
359 1.2 matt r = I2C_CONFIG_ADDRLEN(I2C_CONFIG_ADDRLEN_8) |
360 1.2 matt I2C_CONFIG_DEVADLEN(I2C_CONFIG_DEVADLEN_7) |
361 1.2 matt I2C_CONFIG_ADDRDIS;
362 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_CONFIG, r);
363 1.2 matt
364 1.2 matt /*
365 1.2 matt * Set the I2C clock divider. Appears to be set to 200,000,
366 1.2 matt * which is strange, as I2C is 100K/400K/3.?M bps.
367 1.2 matt */
368 1.2 matt bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_CLKDIV,
369 1.2 matt clkdiv);
370 1.2 matt }
371