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