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