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