bcm2835_bsc.c revision 1.14 1 /* $NetBSD: bcm2835_bsc.c,v 1.14 2019/12/22 23:24:56 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2019 Jason R. Thorpe
5 * Copyright (c) 2012 Jonathan A. Kollasch
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: bcm2835_bsc.c,v 1.14 2019/12/22 23:24:56 thorpej Exp $");
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/kernhist.h>
37 #include <sys/intr.h>
38 #include <sys/mutex.h>
39 #include <sys/systm.h>
40
41 #include <dev/i2c/i2cvar.h>
42
43 #include <arm/broadcom/bcm2835reg.h>
44 #include <arm/broadcom/bcm2835_bscreg.h>
45
46 #include <dev/fdt/fdtvar.h>
47
48 typedef enum {
49 BSC_EXEC_STATE_IDLE = 0,
50 BSC_EXEC_STATE_SEND_ADDR = 1,
51 BSC_EXEC_STATE_SEND_CMD = 2,
52 BSC_EXEC_STATE_SEND_DATA = 3,
53 BSC_EXEC_STATE_RECV_DATA = 4,
54 BSC_EXEC_STATE_DONE = 5,
55 BSC_EXEC_STATE_ERROR = 6,
56 } bsc_exec_state_t;
57
58 #define BSC_EXEC_STATE_SENDING(sc) \
59 ((sc)->sc_exec_state >= BSC_EXEC_STATE_SEND_ADDR && \
60 (sc)->sc_exec_state <= BSC_EXEC_STATE_SEND_DATA)
61
62 #define BSC_EXEC_STATE_RECEIVING(sc) \
63 ((sc)->sc_exec_state == BSC_EXEC_STATE_RECV_DATA)
64
65 struct bsciic_softc {
66 device_t sc_dev;
67 bus_space_tag_t sc_iot;
68 bus_space_handle_t sc_ioh;
69 struct i2c_controller sc_i2c;
70 void *sc_inth;
71
72 struct clk *sc_clk;
73 u_int sc_frequency;
74 u_int sc_clkrate;
75
76 kmutex_t sc_intr_lock;
77 kcondvar_t sc_intr_wait;
78
79 struct {
80 i2c_op_t op;
81 i2c_addr_t addr;
82 const void *cmdbuf;
83 size_t cmdlen;
84 void *databuf;
85 size_t datalen;
86 int flags;
87 } sc_exec;
88
89 /*
90 * Everything below here protected by the i2c controller lock
91 * /and/ sc_intr_lock (if we're using interrupts).
92 */
93
94 bsc_exec_state_t sc_exec_state;
95
96 uint8_t *sc_buf;
97 size_t sc_bufpos;
98 size_t sc_buflen;
99
100 uint32_t sc_c_bits;
101 bool sc_expecting_interrupt;
102 };
103
104 static void bsciic_exec_func_idle(struct bsciic_softc * const);
105 static void bsciic_exec_func_send_addr(struct bsciic_softc * const);
106 static void bsciic_exec_func_send_cmd(struct bsciic_softc * const);
107 static void bsciic_exec_func_send_data(struct bsciic_softc * const);
108 static void bsciic_exec_func_recv_data(struct bsciic_softc * const);
109 static void bsciic_exec_func_done(struct bsciic_softc * const);
110 static void bsciic_exec_func_error(struct bsciic_softc * const);
111
112 const struct {
113 void (*func)(struct bsciic_softc * const);
114 uint32_t c_bits;
115 uint32_t s_bits;
116 } bsciic_exec_state_data[] = {
117 [BSC_EXEC_STATE_IDLE] = {
118 .func = bsciic_exec_func_idle,
119 },
120 [BSC_EXEC_STATE_SEND_ADDR] = {
121 .func = bsciic_exec_func_send_addr,
122 },
123 [BSC_EXEC_STATE_SEND_CMD] = {
124 .func = bsciic_exec_func_send_cmd,
125 .c_bits = BSC_C_INTT,
126 .s_bits = BSC_S_TXW,
127 },
128 [BSC_EXEC_STATE_SEND_DATA] = {
129 .func = bsciic_exec_func_send_data,
130 .c_bits = BSC_C_INTT,
131 .s_bits = BSC_S_TXW,
132 },
133 [BSC_EXEC_STATE_RECV_DATA] = {
134 .func = bsciic_exec_func_recv_data,
135 .c_bits = BSC_C_READ | BSC_C_INTR,
136 .s_bits = BSC_S_RXR,
137 },
138 [BSC_EXEC_STATE_DONE] = {
139 .func = bsciic_exec_func_done,
140 },
141 [BSC_EXEC_STATE_ERROR] = {
142 .func = bsciic_exec_func_error,
143 },
144 };
145
146 static int bsciic_match(device_t, cfdata_t, void *);
147 static void bsciic_attach(device_t, device_t, void *);
148
149 static int bsciic_acquire_bus(void *, int);
150 static void bsciic_release_bus(void *, int);
151 static int bsciic_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
152 void *, size_t, int);
153
154 static int bsciic_intr(void *);
155
156 int bsciic_debug = 0;
157
158 CFATTACH_DECL_NEW(bsciic, sizeof(struct bsciic_softc),
159 bsciic_match, bsciic_attach, NULL, NULL);
160
161 static int
162 bsciic_match(device_t parent, cfdata_t match, void *aux)
163 {
164 const char * const compatible[] = { "brcm,bcm2835-i2c", NULL };
165 struct fdt_attach_args * const faa = aux;
166
167 return of_match_compatible(faa->faa_phandle, compatible);
168 }
169
170 static void
171 bsciic_attach(device_t parent, device_t self, void *aux)
172 {
173 struct bsciic_softc * const sc = device_private(self);
174 struct fdt_attach_args * const faa = aux;
175 const int phandle = faa->faa_phandle;
176 prop_dictionary_t prop = device_properties(self);
177 bool disable = false;
178
179 bus_addr_t addr;
180 bus_size_t size;
181
182 sc->sc_dev = self;
183 sc->sc_iot = faa->faa_bst;
184
185 int error = fdtbus_get_reg(phandle, 0, &addr, &size);
186 if (error) {
187 aprint_error(": unable to get device registers\n");
188 return;
189 }
190
191 prop_dictionary_get_bool(prop, "disable", &disable);
192 if (disable) {
193 aprint_naive(": disabled\n");
194 aprint_normal(": disabled\n");
195 return;
196 }
197
198 /* Enable clock */
199 sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
200 if (sc->sc_clk == NULL) {
201 aprint_error(": couldn't acquire clock\n");
202 return;
203 }
204
205 if (clk_enable(sc->sc_clk) != 0) {
206 aprint_error(": failed to enable clock\n");
207 return;
208 }
209
210 sc->sc_frequency = clk_get_rate(sc->sc_clk);
211
212 if (of_getprop_uint32(phandle, "clock-frequency",
213 &sc->sc_clkrate) != 0) {
214 sc->sc_clkrate = 100000;
215 }
216
217 if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
218 aprint_error(": unable to map device\n");
219 return;
220 }
221
222 aprint_naive("\n");
223 aprint_normal(": Broadcom Serial Controller\n");
224
225 /* clear FIFO, disable controller */
226 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
227 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
228 BSC_S_ERR | BSC_S_DONE);
229 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
230
231 u_int divider = howmany(sc->sc_frequency, sc->sc_clkrate);
232 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DIV,
233 __SHIFTIN(divider, BSC_DIV_CDIV));
234
235 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
236 cv_init(&sc->sc_intr_wait, device_xname(self));
237
238 char intrstr[128];
239 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
240 aprint_error_dev(sc->sc_dev, "failed to decode interrupt\n");
241 return;
242 }
243 sc->sc_inth = fdtbus_intr_establish(phandle, 0, IPL_VM,
244 FDT_INTR_MPSAFE, bsciic_intr, sc);
245 if (sc->sc_inth == NULL) {
246 aprint_error_dev(sc->sc_dev,
247 "failed to establish interrupt %s\n", intrstr);
248 return;
249 }
250 aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
251
252 iic_tag_init(&sc->sc_i2c);
253 sc->sc_i2c.ic_cookie = sc;
254 sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
255 sc->sc_i2c.ic_release_bus = bsciic_release_bus;
256 sc->sc_i2c.ic_exec = bsciic_exec;
257
258 fdtbus_attach_i2cbus(self, phandle, &sc->sc_i2c, iicbus_print);
259 }
260
261 static int
262 bsciic_acquire_bus(void *v, int flags)
263 {
264 struct bsciic_softc * const sc = v;
265 uint32_t s __diagused;
266
267 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
268 BSC_S_ERR | BSC_S_DONE);
269 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN |
270 BSC_C_CLEAR_CLEAR);
271 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
272 KASSERT((s & BSC_S_TA) == 0);
273
274 return 0;
275 }
276
277 static void
278 bsciic_release_bus(void *v, int flags)
279 {
280 struct bsciic_softc * const sc = v;
281
282 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
283 }
284
285 static void
286 bsciic_exec_lock(struct bsciic_softc * const sc)
287 {
288 if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
289 mutex_enter(&sc->sc_intr_lock);
290 }
291 }
292
293 static void
294 bsciic_exec_unlock(struct bsciic_softc * const sc)
295 {
296 if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
297 mutex_exit(&sc->sc_intr_lock);
298 }
299 }
300
301 static void
302 bsciic_txfill(struct bsciic_softc * const sc)
303 {
304 uint32_t s;
305
306 while (sc->sc_bufpos != sc->sc_buflen) {
307 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
308 if ((s & BSC_S_TXD) == 0)
309 break;
310 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO,
311 sc->sc_buf[sc->sc_bufpos++]);
312 }
313 }
314
315 static void
316 bsciic_rxdrain(struct bsciic_softc * const sc)
317 {
318 uint32_t s;
319
320 while (sc->sc_bufpos != sc->sc_buflen) {
321 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
322 if ((s & BSC_S_RXD) == 0)
323 break;
324 sc->sc_buf[sc->sc_bufpos++] =
325 (uint8_t)bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO);
326 }
327 }
328
329 static bsc_exec_state_t
330 bsciic_next_state(struct bsciic_softc * const sc)
331 {
332 switch (sc->sc_exec_state) {
333 case BSC_EXEC_STATE_IDLE:
334 if (sc->sc_exec.addr > 0x7f) {
335 return BSC_EXEC_STATE_SEND_ADDR;
336 }
337 /* FALLTHROUGH */
338
339 case BSC_EXEC_STATE_SEND_ADDR:
340 if (sc->sc_exec.cmdlen) {
341 return BSC_EXEC_STATE_SEND_CMD;
342 }
343 /* FALLTHROUGH */
344
345 case BSC_EXEC_STATE_SEND_CMD:
346 if (sc->sc_exec.datalen == 0) {
347 return BSC_EXEC_STATE_DONE;
348 }
349
350 if (I2C_OP_READ_P(sc->sc_exec.op)) {
351 return BSC_EXEC_STATE_RECV_DATA;
352 }
353
354 return BSC_EXEC_STATE_SEND_DATA;
355
356 case BSC_EXEC_STATE_SEND_DATA:
357 case BSC_EXEC_STATE_RECV_DATA:
358 return BSC_EXEC_STATE_DONE;
359
360 case BSC_EXEC_STATE_DONE:
361 case BSC_EXEC_STATE_ERROR:
362 return sc->sc_exec_state;
363 }
364
365 panic("bsciic_next_state: invalid state: %d", sc->sc_exec_state);
366 }
367
368 #define BSC_EXEC_PHASE_COMPLETE(sc) \
369 ((sc)->sc_exec_state == BSC_EXEC_STATE_ERROR || \
370 (sc)->sc_bufpos == (sc)->sc_buflen)
371
372 static void
373 bsciic_signal(struct bsciic_softc * const sc)
374 {
375 if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
376 cv_signal(&sc->sc_intr_wait);
377 }
378 }
379
380 static void
381 bsciic_phase_done(struct bsciic_softc * const sc)
382 {
383 sc->sc_exec_state = bsciic_next_state(sc);
384 (*bsciic_exec_state_data[sc->sc_exec_state].func)(sc);
385 }
386
387 static void
388 bsciic_abort(struct bsciic_softc * const sc)
389 {
390 sc->sc_exec_state = BSC_EXEC_STATE_ERROR;
391 bsciic_phase_done(sc);
392 }
393
394 static int
395 bsciic_intr(void *v)
396 {
397 struct bsciic_softc * const sc = v;
398 uint32_t s;
399
400 bsciic_exec_lock(sc);
401
402 if ((sc->sc_exec.flags & I2C_F_POLL) == 0 &&
403 sc->sc_expecting_interrupt == false) {
404 bsciic_exec_unlock(sc);
405 return 0;
406 }
407
408 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
409 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S,
410 BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
411
412 if (s & (BSC_S_CLKT | BSC_S_ERR)) {
413 /*
414 * ERR might be a normal "probing for device" sort
415 * of thing, so don't complain about that one.
416 * Do complain about CLKT, though.
417 */
418 if ((s & BSC_S_CLKT) ||
419 (bsciic_debug && (s & BSC_S_ERR))) {
420 device_printf(sc->sc_dev,
421 "error s=0x%08x, aborting transfer\n", s);
422 }
423 bsciic_abort(sc);
424 goto out;
425 }
426
427 if (BSC_EXEC_STATE_SENDING(sc)) {
428 /*
429 * When transmitting, we need to wait for one final
430 * interrupt after pushing out the last of our data.
431 * Catch that case here and go to the next state.
432 */
433 if (BSC_EXEC_PHASE_COMPLETE(sc)) {
434 bsciic_phase_done(sc);
435 } else {
436 bsciic_txfill(sc);
437 }
438 } else if (BSC_EXEC_STATE_RECEIVING(sc)) {
439 bsciic_rxdrain(sc);
440 /*
441 * If we've received all of the data, go to the next
442 * state now; we might not get another interrupt.
443 */
444 if (BSC_EXEC_PHASE_COMPLETE(sc)) {
445 bsciic_phase_done(sc);
446 }
447 } else {
448 device_printf(sc->sc_dev,
449 "unexpected interrupt: state=%d s=0x%08x\n",
450 sc->sc_exec_state, s);
451 bsciic_abort(sc);
452 }
453
454 out:
455 bsciic_exec_unlock(sc);
456 return (1);
457 }
458
459 static void
460 bsciic_wait(struct bsciic_softc * const sc, const uint32_t events)
461 {
462 if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
463 return;
464 }
465
466 const uint32_t s_bits =
467 events | BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE;
468 uint32_t s;
469
470 /* sc_intr_lock is not held in this case. */
471
472 for (;;) {
473 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
474 if (s & s_bits) {
475 (void) bsciic_intr(sc);
476 }
477 if (BSC_EXEC_PHASE_COMPLETE(sc)) {
478 bsciic_phase_done(sc);
479 }
480 if (sc->sc_exec_state >= BSC_EXEC_STATE_DONE) {
481 return;
482 }
483 delay(1);
484 }
485 }
486
487 static void
488 bsciic_start(struct bsciic_softc * const sc)
489 {
490
491 sc->sc_c_bits = BSC_C_I2CEN | BSC_C_INTD |
492 bsciic_exec_state_data[sc->sc_exec_state].c_bits;
493
494 /* Clear the interrupt-enable bits if we're polling. */
495 if (sc->sc_exec.flags & I2C_F_POLL) {
496 sc->sc_c_bits &= ~(BSC_C_INTD | BSC_C_INTT | BSC_C_INTR);
497 }
498
499 sc->sc_expecting_interrupt =
500 (sc->sc_c_bits & (BSC_C_INTD | BSC_C_INTT | BSC_C_INTR)) ? true
501 : false;
502
503 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
504 sc->sc_c_bits | BSC_C_ST);
505 bsciic_wait(sc, bsciic_exec_state_data[sc->sc_exec_state].s_bits);
506 }
507
508 static void
509 bsciic_exec_func_idle(struct bsciic_softc * const sc)
510 {
511 /* We kick off a transfer by setting the slave address register. */
512 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, sc->sc_exec.addr);
513
514 /* Immediately transition to the next state. */
515 bsciic_phase_done(sc);
516 }
517
518 static void
519 bsciic_exec_func_send_addr(struct bsciic_softc * const sc)
520 {
521 /* XXX For 10-bit addressing; not implemented yet. */
522 panic("bsciic_exec_func_send_addr is not supposed to be called");
523 }
524
525 static void
526 bsciic_exec_func_send_cmd(struct bsciic_softc * const sc)
527 {
528 sc->sc_buf = __UNCONST(sc->sc_exec.cmdbuf);
529 sc->sc_bufpos = 0;
530 sc->sc_buflen = sc->sc_exec.cmdlen;
531
532 uint32_t dlen = sc->sc_exec.cmdlen;
533 if (! I2C_OP_READ_P(sc->sc_exec.op)) {
534 dlen += sc->sc_exec.datalen;
535 }
536 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
537
538 bsciic_start(sc);
539 }
540
541 static void
542 bsciic_exec_func_send_data(struct bsciic_softc * const sc)
543 {
544 sc->sc_buf = sc->sc_exec.databuf;
545 sc->sc_bufpos = 0;
546 sc->sc_buflen = sc->sc_exec.datalen;
547
548 if (sc->sc_exec.cmdlen) {
549 /*
550 * Output has already been started in this case; we just
551 * needed to switch buffers.
552 */
553 bsciic_wait(sc, BSC_S_TXW);
554 } else {
555 uint32_t dlen = sc->sc_exec.datalen;
556 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
557 bsciic_start(sc);
558 }
559 }
560
561 static void
562 bsciic_exec_func_recv_data(struct bsciic_softc * const sc)
563 {
564 sc->sc_buf = sc->sc_exec.databuf;
565 sc->sc_bufpos = 0;
566 sc->sc_buflen = sc->sc_exec.datalen;
567
568 uint32_t dlen = sc->sc_exec.datalen;
569 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
570
571 bsciic_start(sc);
572 }
573
574 static void
575 bsciic_exec_func_done(struct bsciic_softc * const sc)
576 {
577 /* We're done! Disable interrupts. */
578 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN);
579 sc->sc_expecting_interrupt = false;
580 bsciic_signal(sc);
581 }
582
583 static void
584 bsciic_exec_func_error(struct bsciic_softc * const sc)
585 {
586 /* Clear the FIFO and disable interrupts. */
587 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
588 BSC_C_I2CEN | BSC_C_CLEAR_CLEAR);
589 sc->sc_expecting_interrupt = false;
590 bsciic_signal(sc);
591 }
592
593 static int
594 bsciic_exec(void *v, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
595 size_t cmdlen, void *databuf, size_t datalen, int flags)
596 {
597 struct bsciic_softc * const sc = v;
598
599 /* XXX We don't do 10-bit addressing correctly yet. */
600 if (addr > 0x7f)
601 return (ENOTSUP);
602
603 /*
604 * The I2C middle layer has ensured that the client device has
605 * exclusive access to the controller. Copy the parameters
606 * and start the state machine that runs through the necessary
607 * phases of the request.
608 */
609 KASSERT(sc->sc_exec_state == BSC_EXEC_STATE_IDLE);
610 sc->sc_exec.op = op;
611 sc->sc_exec.addr = addr;
612 sc->sc_exec.cmdbuf = cmdbuf;
613 sc->sc_exec.cmdlen = cmdlen;
614 sc->sc_exec.databuf = databuf;
615 sc->sc_exec.datalen = datalen;
616 sc->sc_exec.flags = flags;
617
618 bsciic_exec_lock(sc);
619 (*bsciic_exec_state_data[sc->sc_exec_state].func)(sc);
620 while (sc->sc_exec_state < BSC_EXEC_STATE_DONE) {
621 KASSERT((flags & I2C_F_POLL) == 0);
622 cv_wait(&sc->sc_intr_wait, &sc->sc_intr_lock);
623 }
624 int error = sc->sc_exec_state == BSC_EXEC_STATE_ERROR ? EIO : 0;
625 uint32_t s;
626 do {
627 s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
628 } while ((s & BSC_S_TA) != 0);
629 if (s & (BSC_S_CLKT | BSC_S_ERR)) {
630 error = EIO;
631 }
632 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
633 BSC_C_I2CEN | BSC_C_CLEAR_CLEAR);
634 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S,
635 BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
636 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
637 bsciic_exec_unlock(sc);
638
639 sc->sc_exec.flags = 0;
640 sc->sc_exec_state = BSC_EXEC_STATE_IDLE;
641 memset(&sc->sc_exec, 0, sizeof(sc->sc_exec));
642
643 return error;
644 }
645