bcm2835_bsc.c revision 1.1.8.4 1 /* $NetBSD: bcm2835_bsc.c,v 1.1.8.4 2017/12/03 11:35:52 jdolecek 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.1.8.4 2017/12/03 11:35:52 jdolecek 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 prop_dictionary_t prop = device_properties(self);
103 struct i2cbus_attach_args iba;
104 u_int bscunit = ~0;
105 bool disable = false;
106 static ONCE_DECL(control);
107
108 switch (aaa->aaa_addr) {
109 case BCM2835_BSC0_BASE:
110 bscunit = 0;
111 break;
112 case BCM2835_BSC1_BASE:
113 bscunit = 1;
114 break;
115 }
116
117 prop_dictionary_get_bool(prop, "disable", &disable);
118 if (disable) {
119 aprint_naive(": disabled\n");
120 aprint_normal(": disabled\n");
121 return;
122 }
123
124 aprint_naive("\n");
125 aprint_normal(": BSC%u\n", bscunit);
126
127 RUN_ONCE(&control, bsciic_init);
128
129 sc->sc_dev = self;
130
131 mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
132
133 sc->sc_iot = aaa->aaa_iot;
134 if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, aaa->aaa_size, 0,
135 &sc->sc_ioh) != 0) {
136 aprint_error_dev(sc->sc_dev, "unable to map device\n");
137 return;
138 }
139 sc->sc_ios = aaa->aaa_size;
140
141 switch (aaa->aaa_addr) {
142 case BCM2835_BSC0_BASE:
143 /* SDA0 on GPIO0, SCL0 on GPIO1 */
144 bcm2835gpio_function_select(0, BCM2835_GPIO_ALT0);
145 bcm2835gpio_function_select(1, BCM2835_GPIO_ALT0);
146 break;
147 case BCM2835_BSC1_BASE:
148 /* SDA1 on GPIO2, SCL1 on GPIO3 */
149 bcm2835gpio_function_select(2, BCM2835_GPIO_ALT0);
150 bcm2835gpio_function_select(3, BCM2835_GPIO_ALT0);
151 break;
152 }
153
154 /* clear FIFO, disable controller */
155 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
156 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
157 BSC_S_ERR | BSC_S_DONE);
158 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DIV,
159 __SHIFTIN(250000000/100000, BSC_DIV_CDIV)); // XXX may not be this
160
161 sc->sc_i2c.ic_cookie = sc;
162 sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
163 sc->sc_i2c.ic_release_bus = bsciic_release_bus;
164 sc->sc_i2c.ic_exec = bsciic_exec;
165
166 memset(&iba, 0, sizeof(iba));
167
168 iba.iba_tag = &sc->sc_i2c;
169 iba.iba_type = 0;
170 config_found_ia(self, "i2cbus", &iba, iicbus_print);
171 }
172
173 void
174 bsciic_dump_regs(struct bsciic_softc * const sc)
175 {
176 KERNHIST_FUNC(__func__);
177 KERNHIST_CALLED(bsciichist);
178
179 KERNHIST_LOG(bsciichist, "C %08jx S %08jx D %08jx A %08jx",
180 bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_C),
181 bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S),
182 bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN),
183 bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_A)
184 );
185 }
186
187 static int
188 bsciic_acquire_bus(void *v, int flags)
189 {
190 struct bsciic_softc * const sc = v;
191 uint32_t s __diagused;
192
193 mutex_enter(&sc->sc_buslock);
194
195 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
196 BSC_S_ERR | BSC_S_DONE);
197 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN |
198 BSC_C_CLEAR_CLEAR);
199 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
200 KASSERT((s & BSC_S_TA) == 0);
201
202 return 0;
203 }
204
205 static void
206 bsciic_release_bus(void *v, int flags)
207 {
208 struct bsciic_softc * const sc = v;
209
210 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
211
212 mutex_exit(&sc->sc_buslock);
213 }
214
215 static int
216 bsciic_exec(void *v, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
217 size_t cmdlen, void *databuf, size_t datalen, int flags)
218 {
219 KERNHIST_FUNC(__func__); KERNHIST_CALLED(bsciichist);
220 struct bsciic_softc * const sc = v;
221 uint32_t c, s, dlen, a;
222 uint32_t j;
223 uint8_t *buf;
224 size_t len;
225 size_t pos;
226 int error = 0;
227 const bool isread = I2C_OP_READ_P(op);
228
229 flags |= I2C_F_POLL;
230
231 #if 0
232 device_printf(sc->sc_dev, "exec: op %d, addr 0x%x, cmdbuf %p, "
233 "cmdlen %zu, databuf %p, datalen %zu, flags 0x%x\n",
234 op, addr, cmdbuf, cmdlen, databuf, datalen, flags);
235 #endif
236
237 a = __SHIFTIN(addr, BSC_A_ADDR);
238 c = BSC_C_I2CEN | BSC_C_CLEAR_CLEAR;
239 #if notyet
240 c |= BSC_C_INTR | BSC_C_INTT | BSC_C_INTD;
241 #endif
242
243 if (isread && cmdlen == 0)
244 goto only_read;
245
246 buf = __UNCONST(cmdbuf);
247 len = cmdlen;
248
249 if (isread)
250 dlen = cmdlen;
251 else
252 dlen = cmdlen + datalen;
253 dlen = __SHIFTIN(dlen, BSC_DLEN_DLEN);
254
255 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
256 if ((s & BSC_S_TA) != 0)
257 bsciic_dump_regs(sc);
258 KASSERT((s & BSC_S_TA) == 0);
259 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
260 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, a);
261 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, c | BSC_C_ST);
262
263 do {
264 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
265 } while ((s & BSC_S_TA) == 0);
266
267 flood_again:
268 KERNHIST_LOG(bsciichist, "flood top %#jx %ju",
269 (uintptr_t)buf, len, 0, 0);
270 j = 10000000;
271 for (pos = 0; pos < len; ) {
272 if (--j == 0)
273 return -1;
274 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
275 KERNHIST_LOG(bsciichist, "w s %08jx", s, 0, 0, 0);
276 if ((s & BSC_S_CLKT) != 0) {
277 error = EIO;
278 goto done;
279 }
280 if ((s & BSC_S_ERR) != 0) {
281 error = EIO;
282 goto done;
283 }
284 if ((s & BSC_S_DONE) != 0)
285 break;
286 if ((s & BSC_S_TXD) == 0)
287 continue;
288 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO, buf[pos]);
289 KERNHIST_LOG(bsciichist, "w %#jx %#jx %02jx",
290 (uintptr_t)buf, (uintptr_t)&buf[pos],
291 buf[pos], 0);
292 pos++;
293 }
294 KERNHIST_LOG(bsciichist, "flood bot %#jx %ju",
295 (uintptr_t)buf, len, 0, 0);
296
297 if (buf == cmdbuf && !isread) {
298 buf = databuf;
299 len = datalen;
300 goto flood_again;
301 }
302
303 do {
304 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
305 } while ((s & BSC_S_TA) != 0);
306
307 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
308 s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
309 KASSERT((s & BSC_S_DONE) != 0);
310 if (s != 0)
311 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
312
313 if (error == 0 && (s & (BSC_S_CLKT|BSC_S_ERR)) != 0)
314 error = EIO;
315
316 if (!isread)
317 goto done;
318
319 only_read:
320 c |= BSC_C_READ;
321
322 buf = databuf;
323 len = datalen;
324 dlen = datalen;
325
326 dlen = __SHIFTIN(dlen, BSC_DLEN_DLEN);
327 KASSERT(dlen >= 1);
328 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
329 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, a);
330 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, c | BSC_C_ST);
331
332 do {
333 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
334 } while ((s & BSC_S_TA) == 0);
335
336 KERNHIST_LOG(bsciichist, "drain top %#jx %ju",
337 (uintptr_t)buf, len, 0, 0);
338 j = 10000000;
339 for (pos = 0; pos < len; ) {
340 if (--j == 0)
341 return -1;
342 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
343 KERNHIST_LOG(bsciichist, "r s %08jx", s, 0, 0, 0);
344 if ((s & BSC_S_CLKT) != 0) {
345 error = EIO;
346 goto done;
347 }
348 if ((s & BSC_S_ERR) != 0) {
349 error = EIO;
350 goto done;
351 }
352 if ((s & BSC_S_DONE) != 0)
353 break;
354 if ((s & BSC_S_RXD) == 0)
355 continue;
356 j = 10000000;
357 buf[pos] = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO);
358 KERNHIST_LOG(bsciichist, "r %#jx %#jx %02jx",
359 (uintptr_t)buf, (uintptr_t)&buf[pos],
360 buf[pos], 0);
361 pos++;
362 }
363 KERNHIST_LOG(bsciichist, "drain bot %#jx %ju", (uintptr_t)buf, len,
364 0, 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 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
371 s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
372 KASSERT((s & BSC_S_DONE) != 0);
373 if (s != 0)
374 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
375
376 done:
377 bsciic_dump_regs(sc);
378
379 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
380
381 do {
382 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
383 } while ((s & BSC_S_TA) != 0);
384
385 bsciic_dump_regs(sc);
386
387 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
388 s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
389 if (s != 0)
390 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
391
392 bsciic_dump_regs(sc);
393
394 if (error == 0 && (s & (BSC_S_CLKT|BSC_S_ERR)) != 0)
395 error = EIO;
396
397 return error;
398 }
399