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