bcm2835_bsc.c revision 1.5 1 /* $NetBSD: bcm2835_bsc.c,v 1.5 2015/01/24 00:27:31 jakllsch Exp $ */
2
3 /*
4 * Copyright (c) 2012 Jonathan A. Kollasch
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: bcm2835_bsc.c,v 1.5 2015/01/24 00:27:31 jakllsch Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/intr.h>
36 #include <sys/mutex.h>
37 #include <sys/once.h>
38 #include <sys/systm.h>
39
40 #include <dev/i2c/i2cvar.h>
41
42 #include <arm/broadcom/bcm_amba.h>
43 #include <arm/broadcom/bcm2835reg.h>
44 #include <arm/broadcom/bcm2835_bscreg.h>
45 #include <arm/broadcom/bcm2835_gpio_subr.h>
46
47 #if defined(_KERNEL_OPT)
48 #include "opt_kernhist.h"
49 #endif
50 #include <sys/kernhist.h>
51
52 KERNHIST_DEFINE(bsciichist);
53
54 struct bsciic_softc {
55 device_t sc_dev;
56 bus_space_tag_t sc_iot;
57 bus_space_handle_t sc_ioh;
58 bus_size_t sc_ios;
59 struct i2c_controller sc_i2c;
60 kmutex_t sc_buslock;
61 void *sc_inth;
62 };
63
64 static int bsciic_match(device_t, cfdata_t, void *);
65 static void bsciic_attach(device_t, device_t, void *);
66
67 void bsciic_dump_regs(struct bsciic_softc * const);
68
69 static int bsciic_acquire_bus(void *, int);
70 static void bsciic_release_bus(void *, int);
71 static int bsciic_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
72 void *, size_t, int);
73
74 CFATTACH_DECL_NEW(bsciic, sizeof(struct bsciic_softc),
75 bsciic_match, bsciic_attach, NULL, NULL);
76
77 static int
78 bsciic_init(void)
79 {
80
81 KERNHIST_INIT(bsciichist, 512);
82
83 return 0;
84 }
85
86 static int
87 bsciic_match(device_t parent, cfdata_t match, void *aux)
88 {
89 struct amba_attach_args * const aaa = aux;
90
91 if (strcmp(aaa->aaa_name, "bcmbsc") != 0)
92 return 0;
93
94 return 1;
95 }
96
97 static void
98 bsciic_attach(device_t parent, device_t self, void *aux)
99 {
100 struct bsciic_softc * const sc = device_private(self);
101 struct amba_attach_args * const aaa = aux;
102 struct i2cbus_attach_args iba;
103 u_int bscunit = ~0;
104 static ONCE_DECL(control);
105
106 switch (aaa->aaa_addr) {
107 case BCM2835_BSC0_BASE:
108 bscunit = 0;
109 break;
110 case BCM2835_BSC1_BASE:
111 bscunit = 1;
112 break;
113 }
114
115 aprint_naive("\n");
116 aprint_normal(": BSC%u\n", bscunit);
117
118 RUN_ONCE(&control, bsciic_init);
119
120 sc->sc_dev = self;
121
122 mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
123
124 sc->sc_iot = aaa->aaa_iot;
125 if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, aaa->aaa_size, 0,
126 &sc->sc_ioh) != 0) {
127 aprint_error_dev(sc->sc_dev, "unable to map device\n");
128 return;
129 }
130 sc->sc_ios = aaa->aaa_size;
131
132 switch (aaa->aaa_addr) {
133 case BCM2835_BSC0_BASE:
134 /* SDA0 on GPIO0, SCL0 on GPIO1 */
135 bcm2835gpio_function_select(0, BCM2835_GPIO_ALT0);
136 bcm2835gpio_function_select(1, BCM2835_GPIO_ALT0);
137 break;
138 case BCM2835_BSC1_BASE:
139 /* SDA1 on GPIO2, SCL1 on GPIO3 */
140 bcm2835gpio_function_select(2, BCM2835_GPIO_ALT0);
141 bcm2835gpio_function_select(3, BCM2835_GPIO_ALT0);
142 break;
143 }
144
145 /* clear FIFO, disable controller */
146 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
147 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
148 BSC_S_ERR | BSC_S_DONE);
149 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DIV,
150 __SHIFTIN(250000000/100000, BSC_DIV_CDIV)); // XXX may not be this
151
152 sc->sc_i2c.ic_cookie = sc;
153 sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
154 sc->sc_i2c.ic_release_bus = bsciic_release_bus;
155 sc->sc_i2c.ic_exec = bsciic_exec;
156
157 memset(&iba, 0, sizeof(iba));
158
159 iba.iba_tag = &sc->sc_i2c;
160 iba.iba_type = 0;
161 config_found_ia(self, "i2cbus", &iba, iicbus_print);
162 }
163
164 void
165 bsciic_dump_regs(struct bsciic_softc * const sc)
166 {
167 KERNHIST_FUNC(__func__);
168 KERNHIST_CALLED(bsciichist);
169
170 KERNHIST_LOG(bsciichist, "C %08x S %08x D %08x A %08x",
171 bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_C),
172 bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S),
173 bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN),
174 bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_A)
175 );
176 }
177
178 static int
179 bsciic_acquire_bus(void *v, int flags)
180 {
181 struct bsciic_softc * const sc = v;
182 uint32_t s __diagused;
183
184 mutex_enter(&sc->sc_buslock);
185
186 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
187 BSC_S_ERR | BSC_S_DONE);
188 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN |
189 BSC_C_CLEAR_CLEAR);
190 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
191 KASSERT((s & BSC_S_TA) == 0);
192
193 return 0;
194 }
195
196 static void
197 bsciic_release_bus(void *v, int flags)
198 {
199 struct bsciic_softc * const sc = v;
200
201 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
202
203 mutex_exit(&sc->sc_buslock);
204 }
205
206 static int
207 bsciic_exec(void *v, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
208 size_t cmdlen, void *databuf, size_t datalen, int flags)
209 {
210 KERNHIST_FUNC(__func__); KERNHIST_CALLED(bsciichist);
211 struct bsciic_softc * const sc = v;
212 uint32_t c, s, dlen, a;
213 uint32_t j;
214 uint8_t *buf;
215 size_t len;
216 size_t pos;
217 int error = 0;
218 const bool isread = I2C_OP_READ_P(op);
219
220 flags |= I2C_F_POLL;
221
222 #if 0
223 device_printf(sc->sc_dev, "exec: op %d, addr 0x%x, cmdbuf %p, "
224 "cmdlen %zu, databuf %p, datalen %zu, flags 0x%x\n",
225 op, addr, cmdbuf, cmdlen, databuf, datalen, flags);
226 #endif
227
228 a = __SHIFTIN(addr, BSC_A_ADDR);
229 c = BSC_C_I2CEN | BSC_C_CLEAR_CLEAR;
230 #if notyet
231 c |= BSC_C_INTR | BSC_C_INTT | BSC_C_INTD;
232 #endif
233
234 if (isread && cmdlen == 0)
235 goto only_read;
236
237 buf = __UNCONST(cmdbuf);
238 len = cmdlen;
239
240 if (isread)
241 dlen = cmdlen;
242 else
243 dlen = cmdlen + datalen;
244 dlen = __SHIFTIN(dlen, BSC_DLEN_DLEN);
245
246 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
247 if ((s & BSC_S_TA) != 0)
248 bsciic_dump_regs(sc);
249 KASSERT((s & BSC_S_TA) == 0);
250 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
251 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, a);
252 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, c | BSC_C_ST);
253
254 do {
255 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
256 } while ((s & BSC_S_TA) == 0);
257
258 flood_again:
259 KERNHIST_LOG(bsciichist, "flood top %p %zu", buf, len, 0, 0);
260 j = 10000000;
261 for (pos = 0; pos < len; ) {
262 if (--j == 0)
263 return -1;
264 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
265 KERNHIST_LOG(bsciichist, "w s %08x", s, 0, 0, 0);
266 if ((s & BSC_S_CLKT) != 0) {
267 error = EIO;
268 goto done;
269 }
270 if ((s & BSC_S_ERR) != 0) {
271 error = EIO;
272 goto done;
273 }
274 if ((s & BSC_S_DONE) != 0)
275 break;
276 if ((s & BSC_S_TXD) == 0)
277 continue;
278 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO, buf[pos]);
279 KERNHIST_LOG(bsciichist, "w %p %p %02x", buf, &buf[pos],
280 buf[pos], 0);
281 pos++;
282 }
283 KERNHIST_LOG(bsciichist, "flood bot %p %zu", buf, len, 0, 0);
284
285 if (buf == cmdbuf && !isread) {
286 buf = databuf;
287 len = datalen;
288 goto flood_again;
289 }
290
291 do {
292 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
293 } while ((s & BSC_S_TA) != 0);
294
295 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
296 s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
297 KASSERT((s & BSC_S_DONE) != 0);
298 if (s != 0)
299 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
300
301 if (error == 0 && (s & (BSC_S_CLKT|BSC_S_ERR)) != 0)
302 error = EIO;
303
304 if (!isread)
305 goto done;
306
307 only_read:
308 c |= BSC_C_READ;
309
310 buf = databuf;
311 len = datalen;
312 dlen = datalen;
313
314 dlen = __SHIFTIN(dlen, BSC_DLEN_DLEN);
315 KASSERT(dlen >= 1);
316 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
317 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, a);
318 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, c | BSC_C_ST);
319
320 do {
321 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
322 } while ((s & BSC_S_TA) == 0);
323
324 KERNHIST_LOG(bsciichist, "drain top %p %zu", buf, len, 0, 0);
325 j = 10000000;
326 for (pos = 0; pos < len; ) {
327 if (--j == 0)
328 return -1;
329 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
330 KERNHIST_LOG(bsciichist, "r s %08x", s, 0, 0, 0);
331 if ((s & BSC_S_CLKT) != 0) {
332 error = EIO;
333 goto done;
334 }
335 if ((s & BSC_S_ERR) != 0) {
336 error = EIO;
337 goto done;
338 }
339 if ((s & BSC_S_DONE) != 0)
340 break;
341 if ((s & BSC_S_RXD) == 0)
342 continue;
343 j = 10000000;
344 buf[pos] = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO);
345 KERNHIST_LOG(bsciichist, "r %p %p %02x", buf, &buf[pos],
346 buf[pos], 0);
347 pos++;
348 }
349 KERNHIST_LOG(bsciichist, "drain bot %p %zu", buf, len, 0, 0);
350
351 do {
352 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
353 } while ((s & BSC_S_TA) != 0);
354
355 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
356 s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
357 KASSERT((s & BSC_S_DONE) != 0);
358 if (s != 0)
359 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
360
361 done:
362 bsciic_dump_regs(sc);
363
364 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
365
366 do {
367 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
368 } while ((s & BSC_S_TA) != 0);
369
370 bsciic_dump_regs(sc);
371
372 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
373 s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
374 if (s != 0)
375 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
376
377 bsciic_dump_regs(sc);
378
379 if (error == 0 && (s & (BSC_S_CLKT|BSC_S_ERR)) != 0)
380 error = EIO;
381
382 return error;
383 }
384