Home | History | Annotate | Line # | Download | only in sunxi
sunxi_rsb.c revision 1.6
      1 /* $NetBSD: sunxi_rsb.c,v 1.6 2018/11/17 19:30:51 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014-2017 Jared McNeill <jmcneill (at) invisible.ca>
      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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: sunxi_rsb.c,v 1.6 2018/11/17 19:30:51 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/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/mutex.h>
     39 #include <sys/condvar.h>
     40 
     41 #include <dev/i2c/i2cvar.h>
     42 
     43 #include <dev/fdt/fdtvar.h>
     44 
     45 #include <arm/sunxi/sunxi_rsb.h>
     46 
     47 enum sunxi_rsb_type {
     48 	SUNXI_P2WI,
     49 	SUNXI_RSB,
     50 };
     51 
     52 static const struct of_compat_data compat_data[] = {
     53 	{ "allwinner,sun6i-a31-p2wi",	SUNXI_P2WI },
     54 	{ "allwinner,sun8i-a23-rsb",	SUNXI_RSB },
     55 	{ NULL }
     56 };
     57 
     58 #define RSB_ADDR_PMIC_PRIMARY	0x3a3
     59 #define RSB_ADDR_PMIC_SECONDARY	0x745
     60 #define RSB_ADDR_PERIPH_IC	0xe89
     61 
     62 /*
     63  * Device address to Run-time address mappings.
     64  *
     65  * Run-time address (RTA) is an 8-bit value used to address the device during
     66  * a read or write transaction. The following are valid RTAs:
     67  *  0x17 0x2d 0x3a 0x4e 0x59 0x63 0x74 0x8b 0x9c 0xa6 0xb1 0xc5 0xd2 0xe8 0xff
     68  *
     69  * Allwinner uses RTA 0x2d for the primary PMIC, 0x3a for the secondary PMIC,
     70  * and 0x4e for the peripheral IC (where applicable).
     71  */
     72 static const struct {
     73 	uint16_t        addr;
     74 	uint8_t         rta;
     75 } rsb_rtamap[] = {
     76 	{ .addr = RSB_ADDR_PMIC_PRIMARY,	.rta = 0x2d },
     77 	{ .addr = RSB_ADDR_PMIC_SECONDARY,	.rta = 0x3a },
     78 	{ .addr = RSB_ADDR_PERIPH_IC,		.rta = 0x4e },
     79 	{ .addr = 0,				.rta = 0 }
     80 };
     81 
     82 struct sunxi_rsb_softc {
     83 	device_t sc_dev;
     84 	bus_space_tag_t sc_bst;
     85 	bus_space_handle_t sc_bsh;
     86 	enum sunxi_rsb_type sc_type;
     87 	struct i2c_controller sc_ic;
     88 	kmutex_t sc_lock;
     89 	kcondvar_t sc_cv;
     90 	device_t sc_i2cdev;
     91 	void *sc_ih;
     92 	uint32_t sc_stat;
     93 	bool sc_busy;
     94 
     95 	uint16_t sc_rsb_last_da;
     96 };
     97 
     98 #define RSB_READ(sc, reg) \
     99     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    100 #define RSB_WRITE(sc, reg, val) \
    101     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    102 
    103 static int	sunxi_rsb_acquire_bus(void *, int);
    104 static void     sunxi_rsb_release_bus(void *, int);
    105 static int	sunxi_rsb_exec(void *, i2c_op_t, i2c_addr_t, const void *,
    106 			       size_t, void *, size_t, int);
    107 
    108 static int	sunxi_rsb_intr(void *);
    109 static int	sunxi_rsb_wait(struct sunxi_rsb_softc *, int);
    110 static int	sunxi_rsb_rsb_config(struct sunxi_rsb_softc *,
    111 				     uint8_t, i2c_addr_t, int);
    112 
    113 static int	sunxi_rsb_match(device_t, cfdata_t, void *);
    114 static void	sunxi_rsb_attach(device_t, device_t, void *);
    115 
    116 static i2c_tag_t
    117 sunxi_rsb_get_tag(device_t dev)
    118 {
    119 	struct sunxi_rsb_softc * const sc = device_private(dev);
    120 
    121 	return &sc->sc_ic;
    122 }
    123 
    124 static const struct fdtbus_i2c_controller_func sunxi_rsb_funcs = {
    125 	.get_tag = sunxi_rsb_get_tag,
    126 };
    127 
    128 CFATTACH_DECL_NEW(sunxi_rsb, sizeof(struct sunxi_rsb_softc),
    129 	sunxi_rsb_match, sunxi_rsb_attach, NULL, NULL);
    130 
    131 static int
    132 sunxi_rsb_match(device_t parent, cfdata_t cf, void *aux)
    133 {
    134 	struct fdt_attach_args * const faa = aux;
    135 
    136 	return of_match_compat_data(faa->faa_phandle, compat_data);
    137 }
    138 
    139 static void
    140 sunxi_rsb_attach(device_t parent, device_t self, void *aux)
    141 {
    142 	struct sunxi_rsb_softc * const sc = device_private(self);
    143 	struct fdt_attach_args * const faa = aux;
    144 	const int phandle = faa->faa_phandle;
    145 	struct fdtbus_reset *rst;
    146 	struct clk *clk;
    147 	char intrstr[128];
    148 	bus_addr_t addr;
    149 	bus_size_t size;
    150 
    151 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    152 		aprint_error(": couldn't get registers\n");
    153 		return;
    154 	}
    155 
    156 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    157 		aprint_error(": couldn't decode interrupt\n");
    158 		return;
    159 	}
    160 
    161 	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
    162 		if (clk_enable(clk) != 0) {
    163 			aprint_error(": couldn't enable clock\n");
    164 			return;
    165 		}
    166 	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
    167 		if (fdtbus_reset_deassert(rst) != 0) {
    168 			aprint_error(": couldn't de-assert reset\n");
    169 			return;
    170 		}
    171 
    172 	sc->sc_dev = self;
    173 	sc->sc_type = of_search_compatible(phandle, compat_data)->data;
    174 	sc->sc_bst = faa->faa_bst;
    175 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    176 		aprint_error(": couldn't map registers\n");
    177 		return;
    178 	}
    179 
    180 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
    181 	cv_init(&sc->sc_cv, "awinp2wi");
    182 
    183 	aprint_naive("\n");
    184 	aprint_normal(": %s\n", sc->sc_type == SUNXI_P2WI ? "P2WI" : "RSB");
    185 
    186 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM, 0,
    187 	    sunxi_rsb_intr, sc);
    188 	if (sc->sc_ih == NULL) {
    189 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    190 		    intrstr);
    191 		return;
    192 	}
    193 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    194 
    195 	sc->sc_ic.ic_cookie = sc;
    196 	sc->sc_ic.ic_acquire_bus = sunxi_rsb_acquire_bus;
    197 	sc->sc_ic.ic_release_bus = sunxi_rsb_release_bus;
    198 	sc->sc_ic.ic_exec = sunxi_rsb_exec;
    199 
    200 	fdtbus_register_i2c_controller(self, phandle, &sunxi_rsb_funcs);
    201 
    202 	fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
    203 }
    204 
    205 static int
    206 sunxi_rsb_intr(void *priv)
    207 {
    208 	struct sunxi_rsb_softc *sc = priv;
    209 	uint32_t stat;
    210 
    211 	stat = RSB_READ(sc, RSB_STAT_REG);
    212 	if ((stat & RSB_STAT_MASK) == 0)
    213 		return 0;
    214 
    215 	RSB_WRITE(sc, RSB_STAT_REG, stat & RSB_STAT_MASK);
    216 
    217 	mutex_enter(&sc->sc_lock);
    218 	sc->sc_stat |= stat;
    219 	cv_broadcast(&sc->sc_cv);
    220 	mutex_exit(&sc->sc_lock);
    221 
    222 	return 1;
    223 }
    224 
    225 static int
    226 sunxi_rsb_soft_reset(struct sunxi_rsb_softc *sc)
    227 {
    228 	int retry = 1000;
    229 
    230 	RSB_WRITE(sc, RSB_CTRL_REG, RSB_CTRL_SOFT_RESET);
    231 	while (--retry > 0) {
    232 		if ((RSB_READ(sc, RSB_CTRL_REG) & RSB_CTRL_SOFT_RESET) == 0)
    233 			break;
    234 		delay(10);
    235 	}
    236 	if (retry == 0)
    237 		return EIO;
    238 
    239 	return 0;
    240 }
    241 
    242 static int
    243 sunxi_rsb_wait(struct sunxi_rsb_softc *sc, int flags)
    244 {
    245 	int error = 0, retry;
    246 
    247 	/* Wait up to 5 seconds for a transfer to complete */
    248 	sc->sc_stat = 0;
    249 	for (retry = (flags & I2C_F_POLL) ? 100 : 5; retry > 0; retry--) {
    250 		if (flags & I2C_F_POLL) {
    251 			sc->sc_stat |= RSB_READ(sc, RSB_STAT_REG);
    252 		} else {
    253 			error = cv_timedwait(&sc->sc_cv, &sc->sc_lock, hz);
    254 			if (error && error != EWOULDBLOCK) {
    255 				break;
    256 			}
    257 		}
    258 		if (sc->sc_stat & RSB_STAT_MASK) {
    259 			break;
    260 		}
    261 		if (flags & I2C_F_POLL) {
    262 			delay(10000);
    263 		}
    264 	}
    265 	if (retry == 0)
    266 		error = EAGAIN;
    267 
    268 	if (flags & I2C_F_POLL) {
    269 		RSB_WRITE(sc, RSB_STAT_REG,
    270 		    sc->sc_stat & RSB_STAT_MASK);
    271 	}
    272 
    273 	if (error) {
    274 		/* Abort transaction */
    275 		device_printf(sc->sc_dev, "transfer timeout, error = %d\n",
    276 		    error);
    277 		RSB_WRITE(sc, RSB_CTRL_REG,
    278 		    RSB_CTRL_ABORT_TRANS);
    279 		return error;
    280 	}
    281 
    282 	if (sc->sc_stat & RSB_STAT_LOAD_BSY) {
    283 		device_printf(sc->sc_dev, "transfer busy\n");
    284 		return EBUSY;
    285 	}
    286 	if (sc->sc_stat & RSB_STAT_TRANS_ERR) {
    287 		device_printf(sc->sc_dev, "transfer error, id 0x%02" PRIx64
    288 		    "\n", __SHIFTOUT(sc->sc_stat, RSB_STAT_TRANS_ERR_ID));
    289 		return EIO;
    290 	}
    291 
    292 	return 0;
    293 }
    294 
    295 static int
    296 sunxi_rsb_rsb_config(struct sunxi_rsb_softc *sc, uint8_t rta, i2c_addr_t da,
    297     int flags)
    298 {
    299 	uint32_t dar, ctrl;
    300 
    301 	KASSERT(mutex_owned(&sc->sc_lock));
    302 
    303 	RSB_WRITE(sc, RSB_STAT_REG,
    304 	    RSB_READ(sc, RSB_STAT_REG) & RSB_STAT_MASK);
    305 
    306 	dar = __SHIFTIN(rta, RSB_DAR_RTA);
    307 	dar |= __SHIFTIN(da, RSB_DAR_DA);
    308 	RSB_WRITE(sc, RSB_DAR_REG, dar);
    309 	RSB_WRITE(sc, RSB_CMD_REG, RSB_CMD_IDX_SRTA);
    310 
    311 	/* Make sure the controller is idle */
    312 	ctrl = RSB_READ(sc, RSB_CTRL_REG);
    313 	if (ctrl & RSB_CTRL_START_TRANS) {
    314 		device_printf(sc->sc_dev, "device is busy\n");
    315 		return EBUSY;
    316 	}
    317 
    318 	/* Start the transfer */
    319 	RSB_WRITE(sc, RSB_CTRL_REG,
    320 	    ctrl | RSB_CTRL_START_TRANS);
    321 
    322 	return sunxi_rsb_wait(sc, flags);
    323 }
    324 
    325 static int
    326 sunxi_rsb_acquire_bus(void *priv, int flags)
    327 {
    328 	struct sunxi_rsb_softc *sc = priv;
    329 
    330 	for (;;) {
    331 		mutex_enter(&sc->sc_lock);
    332 		if (sc->sc_busy == false)
    333 			break;
    334 		mutex_exit(&sc->sc_lock);
    335 	}
    336 	sc->sc_busy = true;
    337 
    338 	return 0;
    339 }
    340 
    341 static void
    342 sunxi_rsb_release_bus(void *priv, int flags)
    343 {
    344 	struct sunxi_rsb_softc *sc = priv;
    345 
    346 	sc->sc_busy = false;
    347 	mutex_exit(&sc->sc_lock);
    348 }
    349 
    350 static int
    351 sunxi_rsb_exec(void *priv, i2c_op_t op, i2c_addr_t addr,
    352     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    353 {
    354 	struct sunxi_rsb_softc *sc = priv;
    355 	uint32_t dlen, ctrl;
    356 	uint8_t rta;
    357 	int error, i;
    358 
    359 	KASSERT(mutex_owned(&sc->sc_lock));
    360 	KASSERT(sc->sc_busy == true);
    361 
    362 	if (cmdlen != 1 || (len != 1 && len != 2 && len != 4))
    363 		return EINVAL;
    364 
    365 	error = sunxi_rsb_soft_reset(sc);
    366 	if (error != 0) {
    367 		device_printf(sc->sc_dev, "soft reset timed out\n");
    368 		return error;
    369 	}
    370 
    371 	if ((flags & I2C_F_POLL) == 0) {
    372 		/* Enable interrupts */
    373 		RSB_WRITE(sc, RSB_INTE_REG,
    374 		    RSB_INTE_LOAD_BSY_ENB |
    375 		    RSB_INTE_TRANS_ERR_ENB |
    376 		    RSB_INTE_TRANS_OVER_ENB);
    377 		RSB_WRITE(sc, RSB_CTRL_REG,
    378 		    RSB_CTRL_GLOBAL_INT_ENB);
    379 	}
    380 
    381 	if (sc->sc_type == SUNXI_RSB && sc->sc_rsb_last_da != addr) {
    382 		/* Lookup run-time address for given device address */
    383 		for (rta = 0, i = 0; rsb_rtamap[i].rta != 0; i++)
    384 			if (rsb_rtamap[i].addr == addr) {
    385 				rta = rsb_rtamap[i].rta;
    386 				break;
    387 			}
    388 		if (rta == 0) {
    389 			device_printf(sc->sc_dev,
    390 			    "RTA not known for address %#x\n", addr);
    391 			return ENXIO;
    392 		}
    393 		error = sunxi_rsb_rsb_config(sc, rta, addr, flags);
    394 		if (error) {
    395 			device_printf(sc->sc_dev,
    396 			    "SRTA failed, flags = %x, error = %d\n",
    397 			    flags, error);
    398 			sc->sc_rsb_last_da = 0;
    399 			goto done;
    400 		}
    401 
    402 		sc->sc_rsb_last_da = addr;
    403 	}
    404 
    405 	/* Data byte register */
    406 	RSB_WRITE(sc, RSB_DADDR0_REG, *(const uint8_t *)cmdbuf);
    407 
    408 	if (I2C_OP_WRITE_P(op)) {
    409 		uint8_t *pbuf = buf;
    410 		uint32_t data;
    411 		/* Write data */
    412 		switch (len) {
    413 		case 1:
    414 			data = pbuf[0];
    415 			break;
    416 		case 2:
    417 			data = pbuf[0] | (pbuf[1] << 8);
    418 			break;
    419 		case 4:
    420 			data = pbuf[0] | (pbuf[1] << 8) |
    421 			    (pbuf[2] << 16) | (pbuf[3] << 24);
    422 			break;
    423 		default:
    424 			error = EINVAL;
    425 			goto done;
    426 		}
    427 		RSB_WRITE(sc, RSB_DATA0_REG, data);
    428 	}
    429 
    430 	if (sc->sc_type == SUNXI_RSB) {
    431 		uint8_t cmd;
    432 		if (I2C_OP_WRITE_P(op)) {
    433 			switch (len) {
    434 			case 1:	cmd = RSB_CMD_IDX_WR8; break;
    435 			case 2: cmd = RSB_CMD_IDX_WR16; break;
    436 			case 4: cmd = RSB_CMD_IDX_WR32; break;
    437 			default: error = EINVAL; goto done;
    438 			}
    439 		} else {
    440 			switch (len) {
    441 			case 1:	cmd = RSB_CMD_IDX_RD8; break;
    442 			case 2: cmd = RSB_CMD_IDX_RD16; break;
    443 			case 4: cmd = RSB_CMD_IDX_RD32; break;
    444 			default: error = EINVAL; goto done;
    445 			}
    446 		}
    447 		RSB_WRITE(sc, RSB_CMD_REG, cmd);
    448 	}
    449 
    450 	/* Program data length register; if reading, set read/write bit */
    451 	dlen = __SHIFTIN(len - 1, RSB_DLEN_ACCESS_LENGTH);
    452 	if (I2C_OP_READ_P(op)) {
    453 		dlen |= RSB_DLEN_READ_WRITE_FLAG;
    454 	}
    455 	RSB_WRITE(sc, RSB_DLEN_REG, dlen);
    456 
    457 	/* Make sure the controller is idle */
    458 	ctrl = RSB_READ(sc, RSB_CTRL_REG);
    459 	if (ctrl & RSB_CTRL_START_TRANS) {
    460 		device_printf(sc->sc_dev, "device is busy\n");
    461 		error = EBUSY;
    462 		goto done;
    463 	}
    464 
    465 	/* Start the transfer */
    466 	RSB_WRITE(sc, RSB_CTRL_REG,
    467 	    ctrl | RSB_CTRL_START_TRANS);
    468 
    469 	error = sunxi_rsb_wait(sc, flags);
    470 	if (error)
    471 		goto done;
    472 
    473 	if (I2C_OP_READ_P(op)) {
    474 		uint32_t data = RSB_READ(sc, RSB_DATA0_REG);
    475 		switch (len) {
    476 		case 4:
    477 			*(uint32_t *)buf = data;
    478 			break;
    479 		case 2:
    480 			*(uint16_t *)buf = data & 0xffff;
    481 			break;
    482 		case 1:
    483 			*(uint8_t *)buf = data & 0xff;
    484 			break;
    485 		default:
    486 			error = EINVAL;
    487 			goto done;
    488 		}
    489 	}
    490 
    491 	error = 0;
    492 
    493 done:
    494 	RSB_WRITE(sc, RSB_CTRL_REG, 0);
    495 
    496 	return error;
    497 }
    498