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