Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_bsc.c revision 1.2.4.2
      1 /*	$NetBSD: bcm2835_bsc.c,v 1.2.4.2 2017/07/26 15:22:36 snj 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.2.4.2 2017/07/26 15:22:36 snj 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 	buf = __UNCONST(cmdbuf);
    244 	len = cmdlen;
    245 
    246 	if (isread)
    247 		dlen = cmdlen;
    248 	else
    249 		dlen = cmdlen + datalen;
    250 	dlen = __SHIFTIN(dlen, BSC_DLEN_DLEN);
    251 
    252 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    253 	if ((s & BSC_S_TA) != 0)
    254 		bsciic_dump_regs(sc);
    255 	KASSERT((s & BSC_S_TA) == 0);
    256 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
    257 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, a);
    258 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, c | BSC_C_ST);
    259 
    260 	do {
    261 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    262 	} while ((s & BSC_S_TA) == 0);
    263 
    264 flood_again:
    265 	KERNHIST_LOG(bsciichist, "flood top %p %zu", buf, len, 0, 0);
    266 	j = 10000000;
    267 	for (pos = 0; pos < len; ) {
    268 		if (--j == 0)
    269 			return -1;
    270 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    271 		KERNHIST_LOG(bsciichist, "w s %08x", s, 0, 0, 0);
    272 		if ((s & BSC_S_CLKT) != 0) {
    273 			error = EIO;
    274 			goto done;
    275 		}
    276 		if ((s & BSC_S_ERR) != 0) {
    277 			error = EIO;
    278 			goto done;
    279 		}
    280 		if ((s & BSC_S_DONE) != 0)
    281 			break;
    282 		if ((s & BSC_S_TXD) == 0)
    283 			continue;
    284 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO, buf[pos]);
    285 		KERNHIST_LOG(bsciichist, "w %p %p %02x", buf, &buf[pos],
    286 		    buf[pos], 0);
    287 		pos++;
    288 	}
    289 	KERNHIST_LOG(bsciichist, "flood bot %p %zu", buf, len, 0, 0);
    290 
    291 	if (buf == cmdbuf && !isread) {
    292 		buf = databuf;
    293 		len = datalen;
    294 		goto flood_again;
    295 	}
    296 
    297 	do {
    298 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    299 	} while ((s & BSC_S_TA) != 0);
    300 
    301 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    302 	s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
    303 	KASSERT((s & BSC_S_DONE) != 0);
    304 	if (s != 0)
    305 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
    306 
    307 	if (!isread)
    308 		goto done;
    309 
    310 	c |= BSC_C_READ;
    311 
    312 	buf = databuf;
    313 	len = datalen;
    314 	dlen = datalen;
    315 
    316 	dlen = __SHIFTIN(dlen, BSC_DLEN_DLEN);
    317 	KASSERT(dlen >= 1);
    318 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
    319 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, a);
    320 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, c | BSC_C_ST);
    321 
    322 	do {
    323 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    324 	} while ((s & BSC_S_TA) == 0);
    325 
    326 	KERNHIST_LOG(bsciichist, "drain top %p %zu", buf, len, 0, 0);
    327 	j = 10000000;
    328 	for (pos = 0; pos < len; ) {
    329 		if (--j == 0)
    330 			return -1;
    331 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    332 		KERNHIST_LOG(bsciichist, "r s %08x", s, 0, 0, 0);
    333 		if ((s & BSC_S_CLKT) != 0) {
    334 			error = EIO;
    335 			goto done;
    336 		}
    337 		if ((s & BSC_S_ERR) != 0) {
    338 			error = EIO;
    339 			goto done;
    340 		}
    341 		if ((s & BSC_S_DONE) != 0)
    342 			break;
    343 		if ((s & BSC_S_RXD) == 0)
    344 			continue;
    345 		j = 10000000;
    346 		buf[pos] = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO);
    347 		KERNHIST_LOG(bsciichist, "r %p %p %02x", buf, &buf[pos],
    348 		    buf[pos], 0);
    349 		pos++;
    350 	}
    351 	KERNHIST_LOG(bsciichist, "drain bot %p %zu", buf, len, 0, 0);
    352 
    353 	do {
    354 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    355 	} while ((s & BSC_S_TA) != 0);
    356 
    357 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    358 	s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
    359 	KASSERT((s & BSC_S_DONE) != 0);
    360 	if (s != 0)
    361 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
    362 
    363 done:
    364 	bsciic_dump_regs(sc);
    365 
    366 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
    367 
    368 	do {
    369 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    370 	} while ((s & BSC_S_TA) != 0);
    371 
    372 	bsciic_dump_regs(sc);
    373 
    374 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    375 	s &= BSC_S_CLKT|BSC_S_ERR|BSC_S_DONE;
    376 	if (s != 0)
    377 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, s);
    378 
    379 	bsciic_dump_regs(sc);
    380 
    381 	return error;
    382 }
    383