Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_bsc.c revision 1.12
      1  1.12   thorpej /*	$NetBSD: bcm2835_bsc.c,v 1.12 2018/06/07 05:07:28 thorpej 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.12   thorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_bsc.c,v 1.12 2018/06/07 05:07:28 thorpej Exp $");
     31   1.8     skrll 
     32   1.8     skrll #if defined(_KERNEL_OPT)
     33   1.8     skrll #include "opt_kernhist.h"
     34   1.8     skrll #endif
     35   1.1  jakllsch 
     36   1.1  jakllsch #include <sys/param.h>
     37   1.3     skrll #include <sys/bus.h>
     38   1.1  jakllsch #include <sys/device.h>
     39   1.8     skrll #include <sys/kernhist.h>
     40   1.3     skrll #include <sys/intr.h>
     41   1.3     skrll #include <sys/mutex.h>
     42   1.3     skrll #include <sys/once.h>
     43   1.1  jakllsch #include <sys/systm.h>
     44   1.1  jakllsch 
     45   1.1  jakllsch #include <dev/i2c/i2cvar.h>
     46   1.1  jakllsch 
     47   1.1  jakllsch #include <arm/broadcom/bcm2835reg.h>
     48   1.1  jakllsch #include <arm/broadcom/bcm2835_bscreg.h>
     49   1.1  jakllsch 
     50   1.8     skrll #include <dev/fdt/fdtvar.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 	struct i2c_controller sc_i2c;
     59   1.1  jakllsch 	kmutex_t sc_buslock;
     60   1.1  jakllsch 	void *sc_inth;
     61   1.8     skrll 
     62   1.8     skrll 	struct clk *sc_clk;
     63   1.8     skrll 	u_int sc_frequency;
     64   1.8     skrll 	u_int sc_clkrate;
     65   1.1  jakllsch };
     66   1.1  jakllsch 
     67   1.1  jakllsch static int bsciic_match(device_t, cfdata_t, void *);
     68   1.1  jakllsch static void bsciic_attach(device_t, device_t, void *);
     69   1.1  jakllsch 
     70   1.1  jakllsch void bsciic_dump_regs(struct bsciic_softc * const);
     71   1.1  jakllsch 
     72   1.1  jakllsch static int  bsciic_acquire_bus(void *, int);
     73   1.1  jakllsch static void bsciic_release_bus(void *, int);
     74   1.1  jakllsch static int  bsciic_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     75   1.1  jakllsch     void *, size_t, int);
     76   1.1  jakllsch 
     77   1.1  jakllsch CFATTACH_DECL_NEW(bsciic, sizeof(struct bsciic_softc),
     78   1.1  jakllsch     bsciic_match, bsciic_attach, NULL, NULL);
     79   1.1  jakllsch 
     80   1.1  jakllsch static int
     81   1.3     skrll bsciic_init(void)
     82   1.3     skrll {
     83   1.3     skrll 
     84   1.3     skrll 	KERNHIST_INIT(bsciichist, 512);
     85   1.3     skrll 
     86   1.3     skrll 	return 0;
     87   1.3     skrll }
     88   1.3     skrll 
     89   1.3     skrll static int
     90   1.1  jakllsch bsciic_match(device_t parent, cfdata_t match, void *aux)
     91   1.1  jakllsch {
     92   1.8     skrll 	const char * const compatible[] = { "brcm,bcm2835-i2c", NULL };
     93   1.8     skrll 	struct fdt_attach_args * const faa = aux;
     94   1.1  jakllsch 
     95   1.8     skrll 	return of_match_compatible(faa->faa_phandle, compatible);
     96   1.1  jakllsch }
     97   1.1  jakllsch 
     98   1.1  jakllsch static void
     99   1.1  jakllsch bsciic_attach(device_t parent, device_t self, void *aux)
    100   1.1  jakllsch {
    101   1.1  jakllsch 	struct bsciic_softc * const sc = device_private(self);
    102   1.8     skrll 	struct fdt_attach_args * const faa = aux;
    103   1.8     skrll 	const int phandle = faa->faa_phandle;
    104   1.6  jmcneill 	prop_dictionary_t prop = device_properties(self);
    105  1.10     skrll 	prop_dictionary_t devs;
    106  1.10     skrll 	uint32_t address_cells;
    107   1.1  jakllsch 	struct i2cbus_attach_args iba;
    108   1.6  jmcneill 	bool disable = false;
    109   1.8     skrll 
    110   1.3     skrll 	static ONCE_DECL(control);
    111   1.8     skrll 	RUN_ONCE(&control, bsciic_init);
    112   1.8     skrll 
    113   1.8     skrll 	bus_addr_t addr;
    114   1.8     skrll 	bus_size_t size;
    115   1.8     skrll 
    116   1.8     skrll 	sc->sc_dev = self;
    117   1.8     skrll 	sc->sc_iot = faa->faa_bst;
    118   1.1  jakllsch 
    119   1.8     skrll 	int error = fdtbus_get_reg(phandle, 0, &addr, &size);
    120   1.8     skrll 	if (error) {
    121   1.8     skrll 		aprint_error(": unable to get device registers\n");
    122   1.8     skrll 		return;
    123   1.1  jakllsch 	}
    124   1.1  jakllsch 
    125   1.6  jmcneill 	prop_dictionary_get_bool(prop, "disable", &disable);
    126   1.6  jmcneill 	if (disable) {
    127   1.6  jmcneill 		aprint_naive(": disabled\n");
    128   1.6  jmcneill 		aprint_normal(": disabled\n");
    129   1.6  jmcneill 		return;
    130   1.6  jmcneill 	}
    131   1.6  jmcneill 
    132   1.8     skrll 	/* Enable clock */
    133   1.8     skrll 	sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
    134   1.8     skrll 	if (sc->sc_clk == NULL) {
    135   1.8     skrll 		aprint_error(": couldn't acquire clock\n");
    136   1.8     skrll 		return;
    137   1.8     skrll 	}
    138   1.1  jakllsch 
    139   1.8     skrll 	if (clk_enable(sc->sc_clk) != 0) {
    140   1.8     skrll 		aprint_error(": failed to enable clock\n");
    141   1.8     skrll 		return;
    142   1.8     skrll 	}
    143   1.1  jakllsch 
    144   1.8     skrll 	sc->sc_frequency = clk_get_rate(sc->sc_clk);
    145   1.1  jakllsch 
    146   1.8     skrll 	if (of_getprop_uint32(phandle, "clock-frequency",
    147   1.8     skrll 	    &sc->sc_clkrate) != 0) {
    148   1.8     skrll 		sc->sc_clkrate = 100000;
    149   1.8     skrll 	}
    150   1.1  jakllsch 
    151   1.8     skrll 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
    152   1.1  jakllsch 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
    153   1.1  jakllsch 		return;
    154   1.1  jakllsch 	}
    155   1.1  jakllsch 
    156   1.8     skrll 	aprint_naive("\n");
    157   1.8     skrll 	aprint_normal(": Broadcom Serial Controller\n");
    158   1.8     skrll 
    159   1.8     skrll 	mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
    160   1.1  jakllsch 
    161   1.1  jakllsch 	/* clear FIFO, disable controller */
    162   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
    163   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
    164   1.1  jakllsch 	    BSC_S_ERR | BSC_S_DONE);
    165   1.8     skrll 
    166   1.8     skrll 	u_int divider = howmany(sc->sc_frequency, sc->sc_clkrate);
    167   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DIV,
    168   1.8     skrll 	   __SHIFTIN(divider, BSC_DIV_CDIV));
    169   1.1  jakllsch 
    170   1.1  jakllsch 	sc->sc_i2c.ic_cookie = sc;
    171   1.1  jakllsch 	sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
    172   1.1  jakllsch 	sc->sc_i2c.ic_release_bus = bsciic_release_bus;
    173   1.1  jakllsch 	sc->sc_i2c.ic_exec = bsciic_exec;
    174   1.1  jakllsch 
    175  1.10     skrll 	devs = prop_dictionary_create();
    176  1.10     skrll 	if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
    177  1.10     skrll 		address_cells = 1;
    178  1.10     skrll 
    179  1.10     skrll 	of_enter_i2c_devs(devs, phandle, address_cells * 4, 0);
    180  1.10     skrll 
    181   1.1  jakllsch 	memset(&iba, 0, sizeof(iba));
    182  1.10     skrll 	iba.iba_tag = &sc->sc_i2c;
    183  1.10     skrll 	iba.iba_child_devices = prop_dictionary_get(devs, "i2c-child-devices");
    184  1.10     skrll 	if (iba.iba_child_devices)
    185  1.10     skrll 		prop_object_retain(iba.iba_child_devices);
    186  1.10     skrll 	prop_object_release(devs);
    187   1.1  jakllsch 
    188   1.1  jakllsch 	config_found_ia(self, "i2cbus", &iba, iicbus_print);
    189   1.1  jakllsch }
    190   1.1  jakllsch 
    191   1.1  jakllsch void
    192   1.1  jakllsch bsciic_dump_regs(struct bsciic_softc * const sc)
    193   1.1  jakllsch {
    194   1.1  jakllsch 	KERNHIST_FUNC(__func__);
    195   1.1  jakllsch 	KERNHIST_CALLED(bsciichist);
    196   1.1  jakllsch 
    197   1.7  pgoyette 	KERNHIST_LOG(bsciichist, "C %08jx S %08jx D %08jx A %08jx",
    198   1.1  jakllsch 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_C),
    199   1.1  jakllsch 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S),
    200   1.1  jakllsch 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN),
    201   1.1  jakllsch 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_A)
    202   1.1  jakllsch 	    );
    203   1.1  jakllsch }
    204   1.1  jakllsch 
    205   1.1  jakllsch static int
    206   1.1  jakllsch bsciic_acquire_bus(void *v, int flags)
    207   1.1  jakllsch {
    208   1.1  jakllsch 	struct bsciic_softc * const sc = v;
    209   1.2     ozaki 	uint32_t s __diagused;
    210   1.1  jakllsch 
    211   1.1  jakllsch 	mutex_enter(&sc->sc_buslock);
    212   1.1  jakllsch 
    213   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
    214   1.1  jakllsch 	    BSC_S_ERR | BSC_S_DONE);
    215   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN |
    216   1.1  jakllsch 	    BSC_C_CLEAR_CLEAR);
    217   1.1  jakllsch 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    218   1.1  jakllsch 	KASSERT((s & BSC_S_TA) == 0);
    219   1.1  jakllsch 
    220   1.1  jakllsch 	return 0;
    221   1.1  jakllsch }
    222   1.1  jakllsch 
    223   1.1  jakllsch static void
    224   1.1  jakllsch bsciic_release_bus(void *v, int flags)
    225   1.1  jakllsch {
    226   1.1  jakllsch 	struct bsciic_softc * const sc = v;
    227   1.1  jakllsch 
    228   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
    229   1.1  jakllsch 
    230   1.1  jakllsch 	mutex_exit(&sc->sc_buslock);
    231   1.1  jakllsch }
    232   1.1  jakllsch 
    233   1.1  jakllsch static int
    234   1.1  jakllsch bsciic_exec(void *v, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
    235   1.1  jakllsch     size_t cmdlen, void *databuf, size_t datalen, int flags)
    236   1.1  jakllsch {
    237   1.1  jakllsch 	KERNHIST_FUNC(__func__); KERNHIST_CALLED(bsciichist);
    238   1.1  jakllsch 	struct bsciic_softc * const sc = v;
    239   1.1  jakllsch 	uint32_t c, s, dlen, a;
    240   1.1  jakllsch 	uint32_t j;
    241   1.1  jakllsch 	uint8_t *buf;
    242   1.1  jakllsch 	size_t len;
    243   1.1  jakllsch 	size_t pos;
    244   1.1  jakllsch 	int error = 0;
    245  1.12   thorpej 	int whichbuf = 0;
    246   1.1  jakllsch 	const bool isread = I2C_OP_READ_P(op);
    247   1.1  jakllsch 
    248  1.12   thorpej 	/*
    249  1.12   thorpej 	 * XXX We don't do 10-bit addressing correctly yet.
    250  1.12   thorpej 	 */
    251  1.12   thorpej 	if (addr > 0x7f)
    252  1.12   thorpej 		return (ENOTSUP);
    253  1.12   thorpej 
    254   1.1  jakllsch 	flags |= I2C_F_POLL;
    255   1.1  jakllsch 
    256   1.1  jakllsch #if 0
    257   1.1  jakllsch 	device_printf(sc->sc_dev, "exec: op %d, addr 0x%x, cmdbuf %p, "
    258   1.1  jakllsch 	    "cmdlen %zu, databuf %p, datalen %zu, flags 0x%x\n",
    259   1.1  jakllsch 	    op, addr, cmdbuf, cmdlen, databuf, datalen, flags);
    260   1.1  jakllsch #endif
    261   1.1  jakllsch 
    262   1.1  jakllsch 	a = __SHIFTIN(addr, BSC_A_ADDR);
    263   1.1  jakllsch 	c = BSC_C_I2CEN | BSC_C_CLEAR_CLEAR;
    264   1.1  jakllsch #if notyet
    265   1.1  jakllsch 	c |= BSC_C_INTR | BSC_C_INTT | BSC_C_INTD;
    266   1.1  jakllsch #endif
    267   1.1  jakllsch 
    268   1.5  jakllsch 	if (isread && cmdlen == 0)
    269   1.5  jakllsch 		goto only_read;
    270   1.5  jakllsch 
    271   1.1  jakllsch 	buf = __UNCONST(cmdbuf);
    272   1.1  jakllsch 	len = cmdlen;
    273   1.1  jakllsch 
    274   1.1  jakllsch 	if (isread)
    275   1.1  jakllsch 		dlen = cmdlen;
    276   1.1  jakllsch 	else
    277   1.1  jakllsch 		dlen = cmdlen + datalen;
    278   1.1  jakllsch 	dlen = __SHIFTIN(dlen, BSC_DLEN_DLEN);
    279   1.1  jakllsch 
    280   1.1  jakllsch 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    281   1.1  jakllsch 	if ((s & BSC_S_TA) != 0)
    282   1.1  jakllsch 		bsciic_dump_regs(sc);
    283   1.1  jakllsch 	KASSERT((s & BSC_S_TA) == 0);
    284   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
    285   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, a);
    286   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, c | BSC_C_ST);
    287   1.1  jakllsch 
    288   1.1  jakllsch 	do {
    289   1.1  jakllsch 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    290   1.1  jakllsch 	} while ((s & BSC_S_TA) == 0);
    291   1.1  jakllsch 
    292   1.1  jakllsch flood_again:
    293   1.7  pgoyette 	KERNHIST_LOG(bsciichist, "flood top %#jx %ju",
    294   1.7  pgoyette 	    (uintptr_t)buf, len, 0, 0);
    295   1.1  jakllsch 	j = 10000000;
    296   1.1  jakllsch 	for (pos = 0; pos < len; ) {
    297   1.1  jakllsch 		if (--j == 0)
    298   1.1  jakllsch 			return -1;
    299   1.1  jakllsch 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    300   1.7  pgoyette 		KERNHIST_LOG(bsciichist, "w s %08jx", s, 0, 0, 0);
    301   1.1  jakllsch 		if ((s & BSC_S_CLKT) != 0) {
    302   1.1  jakllsch 			error = EIO;
    303   1.1  jakllsch 			goto done;
    304   1.1  jakllsch 		}
    305   1.1  jakllsch 		if ((s & BSC_S_ERR) != 0) {
    306   1.1  jakllsch 			error = EIO;
    307   1.1  jakllsch 			goto done;
    308   1.1  jakllsch 		}
    309   1.1  jakllsch 		if ((s & BSC_S_DONE) != 0)
    310   1.1  jakllsch 			break;
    311   1.1  jakllsch 		if ((s & BSC_S_TXD) == 0)
    312   1.1  jakllsch 			continue;
    313   1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO, buf[pos]);
    314   1.7  pgoyette 		KERNHIST_LOG(bsciichist, "w %#jx %#jx %02jx",
    315   1.7  pgoyette 		    (uintptr_t)buf, (uintptr_t)&buf[pos],
    316   1.1  jakllsch 		    buf[pos], 0);
    317   1.1  jakllsch 		pos++;
    318   1.1  jakllsch 	}
    319   1.7  pgoyette 	KERNHIST_LOG(bsciichist, "flood bot %#jx %ju",
    320   1.7  pgoyette 	    (uintptr_t)buf, len, 0, 0);
    321   1.1  jakllsch 
    322  1.12   thorpej 	if (whichbuf == 0 && !isread) {
    323  1.12   thorpej 		KASSERT(buf == cmdbuf);
    324  1.12   thorpej 		whichbuf++;
    325   1.1  jakllsch 		buf = databuf;
    326   1.1  jakllsch 		len = datalen;
    327  1.12   thorpej 		if (len)
    328  1.12   thorpej 			goto flood_again;
    329   1.1  jakllsch 	}
    330   1.1  jakllsch 
    331   1.1  jakllsch 	do {
    332   1.1  jakllsch 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    333   1.1  jakllsch 	} while ((s & BSC_S_TA) != 0);
    334   1.1  jakllsch 
    335   1.1  jakllsch 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    336   1.1  jakllsch 	s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
    337   1.1  jakllsch 	KASSERT((s & BSC_S_DONE) != 0);
    338   1.1  jakllsch 	if (s != 0)
    339   1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
    340   1.1  jakllsch 
    341   1.4  jakllsch 	if (error == 0 && (s & (BSC_S_CLKT|BSC_S_ERR)) != 0)
    342   1.4  jakllsch 		error = EIO;
    343   1.4  jakllsch 
    344   1.1  jakllsch 	if (!isread)
    345   1.1  jakllsch 		goto done;
    346   1.1  jakllsch 
    347   1.5  jakllsch only_read:
    348   1.1  jakllsch 	c |= BSC_C_READ;
    349   1.1  jakllsch 
    350   1.1  jakllsch 	buf = databuf;
    351   1.1  jakllsch 	len = datalen;
    352   1.1  jakllsch 	dlen = datalen;
    353   1.1  jakllsch 
    354   1.1  jakllsch 	dlen = __SHIFTIN(dlen, BSC_DLEN_DLEN);
    355   1.9  christos #if 0
    356   1.1  jakllsch 	KASSERT(dlen >= 1);
    357   1.9  christos #endif
    358   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
    359   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, a);
    360   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, c | BSC_C_ST);
    361   1.1  jakllsch 
    362   1.1  jakllsch 	do {
    363   1.1  jakllsch 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    364   1.1  jakllsch 	} while ((s & BSC_S_TA) == 0);
    365   1.1  jakllsch 
    366   1.7  pgoyette 	KERNHIST_LOG(bsciichist, "drain top %#jx %ju",
    367   1.7  pgoyette 	    (uintptr_t)buf, len, 0, 0);
    368   1.1  jakllsch 	j = 10000000;
    369   1.1  jakllsch 	for (pos = 0; pos < len; ) {
    370   1.1  jakllsch 		if (--j == 0)
    371   1.1  jakllsch 			return -1;
    372   1.1  jakllsch 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    373   1.7  pgoyette 		KERNHIST_LOG(bsciichist, "r s %08jx", s, 0, 0, 0);
    374   1.1  jakllsch 		if ((s & BSC_S_CLKT) != 0) {
    375   1.1  jakllsch 			error = EIO;
    376   1.1  jakllsch 			goto done;
    377   1.1  jakllsch 		}
    378   1.1  jakllsch 		if ((s & BSC_S_ERR) != 0) {
    379   1.1  jakllsch 			error = EIO;
    380   1.1  jakllsch 			goto done;
    381   1.1  jakllsch 		}
    382   1.1  jakllsch 		if ((s & BSC_S_DONE) != 0)
    383   1.1  jakllsch 			break;
    384   1.1  jakllsch 		if ((s & BSC_S_RXD) == 0)
    385   1.1  jakllsch 			continue;
    386   1.1  jakllsch 		j = 10000000;
    387   1.1  jakllsch 		buf[pos] = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO);
    388   1.7  pgoyette 		KERNHIST_LOG(bsciichist, "r %#jx %#jx %02jx",
    389   1.7  pgoyette 		    (uintptr_t)buf, (uintptr_t)&buf[pos],
    390   1.1  jakllsch 		    buf[pos], 0);
    391   1.1  jakllsch 		pos++;
    392   1.1  jakllsch 	}
    393   1.7  pgoyette 	KERNHIST_LOG(bsciichist, "drain bot %#jx %ju", (uintptr_t)buf, len,
    394   1.7  pgoyette 	    0, 0);
    395   1.1  jakllsch 
    396   1.1  jakllsch 	do {
    397   1.1  jakllsch 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    398   1.1  jakllsch 	} while ((s & BSC_S_TA) != 0);
    399   1.1  jakllsch 
    400   1.1  jakllsch 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    401   1.1  jakllsch 	s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
    402   1.1  jakllsch 	KASSERT((s & BSC_S_DONE) != 0);
    403   1.1  jakllsch 	if (s != 0)
    404   1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
    405   1.1  jakllsch 
    406   1.1  jakllsch done:
    407   1.1  jakllsch 	bsciic_dump_regs(sc);
    408   1.1  jakllsch 
    409   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
    410   1.1  jakllsch 
    411   1.1  jakllsch 	do {
    412   1.1  jakllsch 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    413   1.1  jakllsch 	} while ((s & BSC_S_TA) != 0);
    414   1.1  jakllsch 
    415   1.1  jakllsch 	bsciic_dump_regs(sc);
    416   1.1  jakllsch 
    417   1.1  jakllsch 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    418   1.1  jakllsch 	s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
    419   1.1  jakllsch 	if (s != 0)
    420   1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
    421   1.1  jakllsch 
    422   1.1  jakllsch 	bsciic_dump_regs(sc);
    423   1.1  jakllsch 
    424   1.4  jakllsch 	if (error == 0 && (s & (BSC_S_CLKT|BSC_S_ERR)) != 0)
    425   1.4  jakllsch 		error = EIO;
    426   1.4  jakllsch 
    427   1.1  jakllsch 	return error;
    428   1.1  jakllsch }
    429