14b9a54a9SJared McNeill /*-
24b9a54a9SJared McNeill * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
34b9a54a9SJared McNeill *
44b9a54a9SJared McNeill * Redistribution and use in source and binary forms, with or without
54b9a54a9SJared McNeill * modification, are permitted provided that the following conditions
64b9a54a9SJared McNeill * are met:
74b9a54a9SJared McNeill * 1. Redistributions of source code must retain the above copyright
84b9a54a9SJared McNeill * notice, this list of conditions and the following disclaimer.
94b9a54a9SJared McNeill * 2. Redistributions in binary form must reproduce the above copyright
104b9a54a9SJared McNeill * notice, this list of conditions and the following disclaimer in the
114b9a54a9SJared McNeill * documentation and/or other materials provided with the distribution.
124b9a54a9SJared McNeill *
134b9a54a9SJared McNeill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
144b9a54a9SJared McNeill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
154b9a54a9SJared McNeill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
164b9a54a9SJared McNeill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
174b9a54a9SJared McNeill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
184b9a54a9SJared McNeill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
194b9a54a9SJared McNeill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
204b9a54a9SJared McNeill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
214b9a54a9SJared McNeill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
224b9a54a9SJared McNeill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
234b9a54a9SJared McNeill * SUCH DAMAGE.
244b9a54a9SJared McNeill */
254b9a54a9SJared McNeill
264b9a54a9SJared McNeill /*
27cd2b868bSEmmanuel Vadot * Allwinner RSB (Reduced Serial Bus) and P2WI (Push-Pull Two Wire Interface)
284b9a54a9SJared McNeill */
294b9a54a9SJared McNeill
304b9a54a9SJared McNeill #include <sys/param.h>
314b9a54a9SJared McNeill #include <sys/systm.h>
324b9a54a9SJared McNeill #include <sys/bus.h>
334b9a54a9SJared McNeill #include <sys/rman.h>
344b9a54a9SJared McNeill #include <sys/kernel.h>
35e2e050c8SConrad Meyer #include <sys/lock.h>
364b9a54a9SJared McNeill #include <sys/module.h>
37e2e050c8SConrad Meyer #include <sys/mutex.h>
384b9a54a9SJared McNeill #include <machine/bus.h>
394b9a54a9SJared McNeill
404b9a54a9SJared McNeill #include <dev/ofw/ofw_bus.h>
414b9a54a9SJared McNeill #include <dev/ofw/ofw_bus_subr.h>
424b9a54a9SJared McNeill
434b9a54a9SJared McNeill #include <dev/iicbus/iiconf.h>
444b9a54a9SJared McNeill #include <dev/iicbus/iicbus.h>
454b9a54a9SJared McNeill
46be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
471f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
484b9a54a9SJared McNeill
494b9a54a9SJared McNeill #include "iicbus_if.h"
504b9a54a9SJared McNeill
514b9a54a9SJared McNeill #define RSB_CTRL 0x00
524b9a54a9SJared McNeill #define START_TRANS (1 << 7)
534b9a54a9SJared McNeill #define GLOBAL_INT_ENB (1 << 1)
544b9a54a9SJared McNeill #define SOFT_RESET (1 << 0)
554b9a54a9SJared McNeill #define RSB_CCR 0x04
564b9a54a9SJared McNeill #define RSB_INTE 0x08
574b9a54a9SJared McNeill #define RSB_INTS 0x0c
584b9a54a9SJared McNeill #define INT_TRANS_ERR_ID(x) (((x) >> 8) & 0xf)
594b9a54a9SJared McNeill #define INT_LOAD_BSY (1 << 2)
604b9a54a9SJared McNeill #define INT_TRANS_ERR (1 << 1)
614b9a54a9SJared McNeill #define INT_TRANS_OVER (1 << 0)
624b9a54a9SJared McNeill #define INT_MASK (INT_LOAD_BSY|INT_TRANS_ERR|INT_TRANS_OVER)
634b9a54a9SJared McNeill #define RSB_DADDR0 0x10
644b9a54a9SJared McNeill #define RSB_DADDR1 0x14
654b9a54a9SJared McNeill #define RSB_DLEN 0x18
664b9a54a9SJared McNeill #define DLEN_READ (1 << 4)
674b9a54a9SJared McNeill #define RSB_DATA0 0x1c
684b9a54a9SJared McNeill #define RSB_DATA1 0x20
6912faeba9SEmmanuel Vadot #define RSB_PMCR 0x28
7012faeba9SEmmanuel Vadot #define RSB_PMCR_START (1 << 31)
7112faeba9SEmmanuel Vadot #define RSB_PMCR_DATA(x) (x << 16)
7212faeba9SEmmanuel Vadot #define RSB_PMCR_REG(x) (x << 8)
734b9a54a9SJared McNeill #define RSB_CMD 0x2c
744b9a54a9SJared McNeill #define CMD_SRTA 0xe8
754b9a54a9SJared McNeill #define CMD_RD8 0x8b
764b9a54a9SJared McNeill #define CMD_RD16 0x9c
774b9a54a9SJared McNeill #define CMD_RD32 0xa6
784b9a54a9SJared McNeill #define CMD_WR8 0x4e
794b9a54a9SJared McNeill #define CMD_WR16 0x59
804b9a54a9SJared McNeill #define CMD_WR32 0x63
814b9a54a9SJared McNeill #define RSB_DAR 0x30
824b9a54a9SJared McNeill #define DAR_RTA (0xff << 16)
834b9a54a9SJared McNeill #define DAR_RTA_SHIFT 16
844b9a54a9SJared McNeill #define DAR_DA (0xffff << 0)
854b9a54a9SJared McNeill #define DAR_DA_SHIFT 0
864b9a54a9SJared McNeill
874b9a54a9SJared McNeill #define RSB_MAXLEN 8
884b9a54a9SJared McNeill #define RSB_RESET_RETRY 100
894b9a54a9SJared McNeill #define RSB_I2C_TIMEOUT hz
904b9a54a9SJared McNeill
914b9a54a9SJared McNeill #define RSB_ADDR_PMIC_PRIMARY 0x3a3
924b9a54a9SJared McNeill #define RSB_ADDR_PMIC_SECONDARY 0x745
934b9a54a9SJared McNeill #define RSB_ADDR_PERIPH_IC 0xe89
944b9a54a9SJared McNeill
9512faeba9SEmmanuel Vadot #define PMIC_MODE_REG 0x3e
9612faeba9SEmmanuel Vadot #define PMIC_MODE_I2C 0x00
9712faeba9SEmmanuel Vadot #define PMIC_MODE_RSB 0x7c
9812faeba9SEmmanuel Vadot
99cd2b868bSEmmanuel Vadot #define A31_P2WI 1
100cd2b868bSEmmanuel Vadot #define A23_RSB 2
101cd2b868bSEmmanuel Vadot
1024b9a54a9SJared McNeill static struct ofw_compat_data compat_data[] = {
103cd2b868bSEmmanuel Vadot { "allwinner,sun6i-a31-p2wi", A31_P2WI },
104cd2b868bSEmmanuel Vadot { "allwinner,sun8i-a23-rsb", A23_RSB },
1054b9a54a9SJared McNeill { NULL, 0 }
1064b9a54a9SJared McNeill };
1074b9a54a9SJared McNeill
1084b9a54a9SJared McNeill static struct resource_spec rsb_spec[] = {
1094b9a54a9SJared McNeill { SYS_RES_MEMORY, 0, RF_ACTIVE },
1104b9a54a9SJared McNeill { -1, 0 }
1114b9a54a9SJared McNeill };
1124b9a54a9SJared McNeill
1134b9a54a9SJared McNeill /*
1144b9a54a9SJared McNeill * Device address to Run-time address mappings.
1154b9a54a9SJared McNeill *
1164b9a54a9SJared McNeill * Run-time address (RTA) is an 8-bit value used to address the device during
1174b9a54a9SJared McNeill * a read or write transaction. The following are valid RTAs:
1184b9a54a9SJared McNeill * 0x17 0x2d 0x3a 0x4e 0x59 0x63 0x74 0x8b 0x9c 0xa6 0xb1 0xc5 0xd2 0xe8 0xff
1194b9a54a9SJared McNeill *
1204b9a54a9SJared McNeill * Allwinner uses RTA 0x2d for the primary PMIC, 0x3a for the secondary PMIC,
1214b9a54a9SJared McNeill * and 0x4e for the peripheral IC (where applicable).
1224b9a54a9SJared McNeill */
1234b9a54a9SJared McNeill static const struct {
1244b9a54a9SJared McNeill uint16_t addr;
1254b9a54a9SJared McNeill uint8_t rta;
1264b9a54a9SJared McNeill } rsb_rtamap[] = {
1274b9a54a9SJared McNeill { .addr = RSB_ADDR_PMIC_PRIMARY, .rta = 0x2d },
1284b9a54a9SJared McNeill { .addr = RSB_ADDR_PMIC_SECONDARY, .rta = 0x3a },
1294b9a54a9SJared McNeill { .addr = RSB_ADDR_PERIPH_IC, .rta = 0x4e },
1304b9a54a9SJared McNeill { .addr = 0, .rta = 0 }
1314b9a54a9SJared McNeill };
1324b9a54a9SJared McNeill
1334b9a54a9SJared McNeill struct rsb_softc {
1346a94069aSJared McNeill struct resource *res;
1354b9a54a9SJared McNeill struct mtx mtx;
1364b9a54a9SJared McNeill clk_t clk;
1374b9a54a9SJared McNeill hwreset_t rst;
1384b9a54a9SJared McNeill device_t iicbus;
1394b9a54a9SJared McNeill int busy;
1404b9a54a9SJared McNeill uint32_t status;
1414b9a54a9SJared McNeill uint16_t cur_addr;
142cd2b868bSEmmanuel Vadot int type;
1434b9a54a9SJared McNeill
1444b9a54a9SJared McNeill struct iic_msg *msg;
1454b9a54a9SJared McNeill };
1464b9a54a9SJared McNeill
1474b9a54a9SJared McNeill #define RSB_LOCK(sc) mtx_lock(&(sc)->mtx)
1484b9a54a9SJared McNeill #define RSB_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
1494b9a54a9SJared McNeill #define RSB_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED)
1506a94069aSJared McNeill #define RSB_READ(sc, reg) bus_read_4((sc)->res, (reg))
1516a94069aSJared McNeill #define RSB_WRITE(sc, reg, val) bus_write_4((sc)->res, (reg), (val))
1524b9a54a9SJared McNeill
1534b9a54a9SJared McNeill static phandle_t
rsb_get_node(device_t bus,device_t dev)1544b9a54a9SJared McNeill rsb_get_node(device_t bus, device_t dev)
1554b9a54a9SJared McNeill {
1564b9a54a9SJared McNeill return (ofw_bus_get_node(bus));
1574b9a54a9SJared McNeill }
1584b9a54a9SJared McNeill
1594b9a54a9SJared McNeill static int
rsb_reset(device_t dev,u_char speed,u_char addr,u_char * oldaddr)1604b9a54a9SJared McNeill rsb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
1614b9a54a9SJared McNeill {
1624b9a54a9SJared McNeill struct rsb_softc *sc;
1634b9a54a9SJared McNeill int retry;
1644b9a54a9SJared McNeill
1654b9a54a9SJared McNeill sc = device_get_softc(dev);
1664b9a54a9SJared McNeill
1674b9a54a9SJared McNeill RSB_LOCK(sc);
1684b9a54a9SJared McNeill
1694b9a54a9SJared McNeill /* Write soft-reset bit and wait for it to self-clear. */
1704b9a54a9SJared McNeill RSB_WRITE(sc, RSB_CTRL, SOFT_RESET);
1714b9a54a9SJared McNeill for (retry = RSB_RESET_RETRY; retry > 0; retry--)
1724b9a54a9SJared McNeill if ((RSB_READ(sc, RSB_CTRL) & SOFT_RESET) == 0)
1734b9a54a9SJared McNeill break;
1744b9a54a9SJared McNeill
1754b9a54a9SJared McNeill RSB_UNLOCK(sc);
1764b9a54a9SJared McNeill
1774b9a54a9SJared McNeill if (retry == 0) {
1784b9a54a9SJared McNeill device_printf(dev, "soft reset timeout\n");
1794b9a54a9SJared McNeill return (ETIMEDOUT);
1804b9a54a9SJared McNeill }
1814b9a54a9SJared McNeill
1824b9a54a9SJared McNeill return (IIC_ENOADDR);
1834b9a54a9SJared McNeill }
1844b9a54a9SJared McNeill
1854b9a54a9SJared McNeill static uint32_t
rsb_encode(const uint8_t * buf,u_int len,u_int off)1864b9a54a9SJared McNeill rsb_encode(const uint8_t *buf, u_int len, u_int off)
1874b9a54a9SJared McNeill {
1884b9a54a9SJared McNeill uint32_t val;
1894b9a54a9SJared McNeill u_int n;
1904b9a54a9SJared McNeill
1914b9a54a9SJared McNeill val = 0;
1924b9a54a9SJared McNeill for (n = off; n < MIN(len, 4 + off); n++)
1934b9a54a9SJared McNeill val |= ((uint32_t)buf[n] << ((n - off) * NBBY));
1944b9a54a9SJared McNeill
1954b9a54a9SJared McNeill return val;
1964b9a54a9SJared McNeill }
1974b9a54a9SJared McNeill
1984b9a54a9SJared McNeill static void
rsb_decode(const uint32_t val,uint8_t * buf,u_int len,u_int off)1994b9a54a9SJared McNeill rsb_decode(const uint32_t val, uint8_t *buf, u_int len, u_int off)
2004b9a54a9SJared McNeill {
2014b9a54a9SJared McNeill u_int n;
2024b9a54a9SJared McNeill
2034b9a54a9SJared McNeill for (n = off; n < MIN(len, 4 + off); n++)
2044b9a54a9SJared McNeill buf[n] = (val >> ((n - off) * NBBY)) & 0xff;
2054b9a54a9SJared McNeill }
2064b9a54a9SJared McNeill
2074b9a54a9SJared McNeill static int
rsb_start(device_t dev)2084b9a54a9SJared McNeill rsb_start(device_t dev)
2094b9a54a9SJared McNeill {
2104b9a54a9SJared McNeill struct rsb_softc *sc;
2116a94069aSJared McNeill int error, retry;
2124b9a54a9SJared McNeill
2134b9a54a9SJared McNeill sc = device_get_softc(dev);
2144b9a54a9SJared McNeill
2154b9a54a9SJared McNeill RSB_ASSERT_LOCKED(sc);
2164b9a54a9SJared McNeill
2174b9a54a9SJared McNeill /* Start the transfer */
2184b9a54a9SJared McNeill RSB_WRITE(sc, RSB_CTRL, GLOBAL_INT_ENB | START_TRANS);
2194b9a54a9SJared McNeill
2204b9a54a9SJared McNeill /* Wait for transfer to complete */
2214b9a54a9SJared McNeill error = ETIMEDOUT;
2224b9a54a9SJared McNeill for (retry = RSB_I2C_TIMEOUT; retry > 0; retry--) {
2234b9a54a9SJared McNeill sc->status |= RSB_READ(sc, RSB_INTS);
2244b9a54a9SJared McNeill if ((sc->status & INT_TRANS_OVER) != 0) {
2254b9a54a9SJared McNeill error = 0;
2264b9a54a9SJared McNeill break;
2274b9a54a9SJared McNeill }
2284b9a54a9SJared McNeill DELAY((1000 * hz) / RSB_I2C_TIMEOUT);
2294b9a54a9SJared McNeill }
2304b9a54a9SJared McNeill if (error == 0 && (sc->status & INT_TRANS_OVER) == 0) {
2314b9a54a9SJared McNeill device_printf(dev, "transfer error, status 0x%08x\n",
2324b9a54a9SJared McNeill sc->status);
2334b9a54a9SJared McNeill error = EIO;
2344b9a54a9SJared McNeill }
2354b9a54a9SJared McNeill
2364b9a54a9SJared McNeill return (error);
2374b9a54a9SJared McNeill
2384b9a54a9SJared McNeill }
2394b9a54a9SJared McNeill
2404b9a54a9SJared McNeill static int
rsb_set_rta(device_t dev,uint16_t addr)2414b9a54a9SJared McNeill rsb_set_rta(device_t dev, uint16_t addr)
2424b9a54a9SJared McNeill {
2434b9a54a9SJared McNeill struct rsb_softc *sc;
2444b9a54a9SJared McNeill uint8_t rta;
2454b9a54a9SJared McNeill int i;
2464b9a54a9SJared McNeill
2474b9a54a9SJared McNeill sc = device_get_softc(dev);
2484b9a54a9SJared McNeill
2494b9a54a9SJared McNeill RSB_ASSERT_LOCKED(sc);
2504b9a54a9SJared McNeill
2514b9a54a9SJared McNeill /* Lookup run-time address for given device address */
2524b9a54a9SJared McNeill for (rta = 0, i = 0; rsb_rtamap[i].rta != 0; i++)
2534b9a54a9SJared McNeill if (rsb_rtamap[i].addr == addr) {
2544b9a54a9SJared McNeill rta = rsb_rtamap[i].rta;
2554b9a54a9SJared McNeill break;
2564b9a54a9SJared McNeill }
2574b9a54a9SJared McNeill if (rta == 0) {
2584b9a54a9SJared McNeill device_printf(dev, "RTA not known for address %#x\n", addr);
2594b9a54a9SJared McNeill return (ENXIO);
2604b9a54a9SJared McNeill }
2614b9a54a9SJared McNeill
2624b9a54a9SJared McNeill /* Set run-time address */
2634b9a54a9SJared McNeill RSB_WRITE(sc, RSB_INTS, RSB_READ(sc, RSB_INTS));
2644b9a54a9SJared McNeill RSB_WRITE(sc, RSB_DAR, (addr << DAR_DA_SHIFT) | (rta << DAR_RTA_SHIFT));
2654b9a54a9SJared McNeill RSB_WRITE(sc, RSB_CMD, CMD_SRTA);
2664b9a54a9SJared McNeill
2674b9a54a9SJared McNeill return (rsb_start(dev));
2684b9a54a9SJared McNeill }
2694b9a54a9SJared McNeill
2704b9a54a9SJared McNeill static int
rsb_transfer(device_t dev,struct iic_msg * msgs,uint32_t nmsgs)2714b9a54a9SJared McNeill rsb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
2724b9a54a9SJared McNeill {
2734b9a54a9SJared McNeill struct rsb_softc *sc;
2744b9a54a9SJared McNeill uint32_t daddr[2], data[2], dlen;
2754b9a54a9SJared McNeill uint16_t device_addr;
2764b9a54a9SJared McNeill uint8_t cmd;
2774b9a54a9SJared McNeill int error;
2784b9a54a9SJared McNeill
2794b9a54a9SJared McNeill sc = device_get_softc(dev);
2804b9a54a9SJared McNeill
2814b9a54a9SJared McNeill /*
282cd2b868bSEmmanuel Vadot * P2WI and RSB are not really I2C or SMBus controllers, so there are
283cd2b868bSEmmanuel Vadot * some restrictions imposed by the driver.
2844b9a54a9SJared McNeill *
2854b9a54a9SJared McNeill * Transfers must contain exactly two messages. The first is always
2864b9a54a9SJared McNeill * a write, containing a single data byte offset. Data will either
2874b9a54a9SJared McNeill * be read from or written to the corresponding data byte in the
2884b9a54a9SJared McNeill * second message. The slave address in both messages must be the
2894b9a54a9SJared McNeill * same.
2904b9a54a9SJared McNeill */
2914b9a54a9SJared McNeill if (nmsgs != 2 || (msgs[0].flags & IIC_M_RD) == IIC_M_RD ||
2924b9a54a9SJared McNeill (msgs[0].slave >> 1) != (msgs[1].slave >> 1) ||
2934b9a54a9SJared McNeill msgs[0].len != 1 || msgs[1].len > RSB_MAXLEN)
2944b9a54a9SJared McNeill return (EINVAL);
2954b9a54a9SJared McNeill
296cd2b868bSEmmanuel Vadot /* The RSB controller can read or write 1, 2, or 4 bytes at a time. */
297cd2b868bSEmmanuel Vadot if (sc->type == A23_RSB) {
2984b9a54a9SJared McNeill if ((msgs[1].flags & IIC_M_RD) != 0) {
2994b9a54a9SJared McNeill switch (msgs[1].len) {
3004b9a54a9SJared McNeill case 1:
3014b9a54a9SJared McNeill cmd = CMD_RD8;
3024b9a54a9SJared McNeill break;
3034b9a54a9SJared McNeill case 2:
3044b9a54a9SJared McNeill cmd = CMD_RD16;
3054b9a54a9SJared McNeill break;
3064b9a54a9SJared McNeill case 4:
3074b9a54a9SJared McNeill cmd = CMD_RD32;
3084b9a54a9SJared McNeill break;
3094b9a54a9SJared McNeill default:
3104b9a54a9SJared McNeill return (EINVAL);
3114b9a54a9SJared McNeill }
3124b9a54a9SJared McNeill } else {
3134b9a54a9SJared McNeill switch (msgs[1].len) {
3144b9a54a9SJared McNeill case 1:
3154b9a54a9SJared McNeill cmd = CMD_WR8;
3164b9a54a9SJared McNeill break;
3174b9a54a9SJared McNeill case 2:
3184b9a54a9SJared McNeill cmd = CMD_WR16;
3194b9a54a9SJared McNeill break;
3204b9a54a9SJared McNeill case 4:
3214b9a54a9SJared McNeill cmd = CMD_WR32;
3224b9a54a9SJared McNeill break;
3234b9a54a9SJared McNeill default:
3244b9a54a9SJared McNeill return (EINVAL);
3254b9a54a9SJared McNeill }
3264b9a54a9SJared McNeill }
327cd2b868bSEmmanuel Vadot }
3284b9a54a9SJared McNeill
3294b9a54a9SJared McNeill RSB_LOCK(sc);
3304b9a54a9SJared McNeill while (sc->busy)
3314b9a54a9SJared McNeill mtx_sleep(sc, &sc->mtx, 0, "i2cbuswait", 0);
3324b9a54a9SJared McNeill sc->busy = 1;
3334b9a54a9SJared McNeill sc->status = 0;
3344b9a54a9SJared McNeill
3354b9a54a9SJared McNeill /* Select current run-time address if necessary */
336cd2b868bSEmmanuel Vadot if (sc->type == A23_RSB) {
3374b9a54a9SJared McNeill device_addr = msgs[0].slave >> 1;
3384b9a54a9SJared McNeill if (sc->cur_addr != device_addr) {
3394b9a54a9SJared McNeill error = rsb_set_rta(dev, device_addr);
3404b9a54a9SJared McNeill if (error != 0)
3414b9a54a9SJared McNeill goto done;
3424b9a54a9SJared McNeill sc->cur_addr = device_addr;
3434b9a54a9SJared McNeill sc->status = 0;
3444b9a54a9SJared McNeill }
345cd2b868bSEmmanuel Vadot }
3464b9a54a9SJared McNeill
3474b9a54a9SJared McNeill /* Clear interrupt status */
3484b9a54a9SJared McNeill RSB_WRITE(sc, RSB_INTS, RSB_READ(sc, RSB_INTS));
3494b9a54a9SJared McNeill
3504b9a54a9SJared McNeill /* Program data access address registers */
3514b9a54a9SJared McNeill daddr[0] = rsb_encode(msgs[0].buf, msgs[0].len, 0);
3524b9a54a9SJared McNeill RSB_WRITE(sc, RSB_DADDR0, daddr[0]);
3534b9a54a9SJared McNeill
3544b9a54a9SJared McNeill /* Write data */
3554b9a54a9SJared McNeill if ((msgs[1].flags & IIC_M_RD) == 0) {
3564b9a54a9SJared McNeill data[0] = rsb_encode(msgs[1].buf, msgs[1].len, 0);
3574b9a54a9SJared McNeill RSB_WRITE(sc, RSB_DATA0, data[0]);
3584b9a54a9SJared McNeill }
3594b9a54a9SJared McNeill
360cd2b868bSEmmanuel Vadot /* Set command type for RSB */
361cd2b868bSEmmanuel Vadot if (sc->type == A23_RSB)
3624b9a54a9SJared McNeill RSB_WRITE(sc, RSB_CMD, cmd);
3634b9a54a9SJared McNeill
3644b9a54a9SJared McNeill /* Program data length register and transfer direction */
3654b9a54a9SJared McNeill dlen = msgs[0].len - 1;
3664b9a54a9SJared McNeill if ((msgs[1].flags & IIC_M_RD) == IIC_M_RD)
3674b9a54a9SJared McNeill dlen |= DLEN_READ;
3684b9a54a9SJared McNeill RSB_WRITE(sc, RSB_DLEN, dlen);
3694b9a54a9SJared McNeill
3704b9a54a9SJared McNeill /* Start transfer */
3714b9a54a9SJared McNeill error = rsb_start(dev);
3724b9a54a9SJared McNeill if (error != 0)
3734b9a54a9SJared McNeill goto done;
3744b9a54a9SJared McNeill
3754b9a54a9SJared McNeill /* Read data */
3764b9a54a9SJared McNeill if ((msgs[1].flags & IIC_M_RD) == IIC_M_RD) {
3774b9a54a9SJared McNeill data[0] = RSB_READ(sc, RSB_DATA0);
3784b9a54a9SJared McNeill rsb_decode(data[0], msgs[1].buf, msgs[1].len, 0);
3794b9a54a9SJared McNeill }
3804b9a54a9SJared McNeill
3814b9a54a9SJared McNeill done:
3824b9a54a9SJared McNeill sc->msg = NULL;
3834b9a54a9SJared McNeill sc->busy = 0;
3844b9a54a9SJared McNeill wakeup(sc);
3854b9a54a9SJared McNeill RSB_UNLOCK(sc);
3864b9a54a9SJared McNeill
3874b9a54a9SJared McNeill return (error);
3884b9a54a9SJared McNeill }
3894b9a54a9SJared McNeill
3904b9a54a9SJared McNeill static int
rsb_probe(device_t dev)3914b9a54a9SJared McNeill rsb_probe(device_t dev)
3924b9a54a9SJared McNeill {
3934b9a54a9SJared McNeill if (!ofw_bus_status_okay(dev))
3944b9a54a9SJared McNeill return (ENXIO);
3954b9a54a9SJared McNeill
396cd2b868bSEmmanuel Vadot switch (ofw_bus_search_compatible(dev, compat_data)->ocd_data) {
397cd2b868bSEmmanuel Vadot case A23_RSB:
3984b9a54a9SJared McNeill device_set_desc(dev, "Allwinner RSB");
399cd2b868bSEmmanuel Vadot break;
400cd2b868bSEmmanuel Vadot case A31_P2WI:
401cd2b868bSEmmanuel Vadot device_set_desc(dev, "Allwinner P2WI");
402cd2b868bSEmmanuel Vadot break;
403cd2b868bSEmmanuel Vadot default:
404cd2b868bSEmmanuel Vadot return (ENXIO);
405cd2b868bSEmmanuel Vadot }
406cd2b868bSEmmanuel Vadot
4074b9a54a9SJared McNeill return (BUS_PROBE_DEFAULT);
4084b9a54a9SJared McNeill }
4094b9a54a9SJared McNeill
4104b9a54a9SJared McNeill static int
rsb_attach(device_t dev)4114b9a54a9SJared McNeill rsb_attach(device_t dev)
4124b9a54a9SJared McNeill {
4134b9a54a9SJared McNeill struct rsb_softc *sc;
4144b9a54a9SJared McNeill int error;
4154b9a54a9SJared McNeill
4164b9a54a9SJared McNeill sc = device_get_softc(dev);
4174b9a54a9SJared McNeill mtx_init(&sc->mtx, device_get_nameunit(dev), "rsb", MTX_DEF);
4184b9a54a9SJared McNeill
419cd2b868bSEmmanuel Vadot sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
420cd2b868bSEmmanuel Vadot
421dac93553SMichal Meloun if (clk_get_by_ofw_index(dev, 0, 0, &sc->clk) == 0) {
4224b9a54a9SJared McNeill error = clk_enable(sc->clk);
4234b9a54a9SJared McNeill if (error != 0) {
4244b9a54a9SJared McNeill device_printf(dev, "cannot enable clock\n");
4254b9a54a9SJared McNeill goto fail;
4264b9a54a9SJared McNeill }
4274b9a54a9SJared McNeill }
428dac93553SMichal Meloun if (hwreset_get_by_ofw_idx(dev, 0, 0, &sc->rst) == 0) {
4294b9a54a9SJared McNeill error = hwreset_deassert(sc->rst);
4304b9a54a9SJared McNeill if (error != 0) {
4314b9a54a9SJared McNeill device_printf(dev, "cannot de-assert reset\n");
4324b9a54a9SJared McNeill goto fail;
4334b9a54a9SJared McNeill }
4344b9a54a9SJared McNeill }
4354b9a54a9SJared McNeill
4366a94069aSJared McNeill if (bus_alloc_resources(dev, rsb_spec, &sc->res) != 0) {
4374b9a54a9SJared McNeill device_printf(dev, "cannot allocate resources for device\n");
4384b9a54a9SJared McNeill error = ENXIO;
4394b9a54a9SJared McNeill goto fail;
4404b9a54a9SJared McNeill }
4414b9a54a9SJared McNeill
44212faeba9SEmmanuel Vadot /* Set the PMIC into RSB mode as ATF might have leave it in I2C mode */
44312faeba9SEmmanuel Vadot RSB_WRITE(sc, RSB_PMCR, RSB_PMCR_REG(PMIC_MODE_REG) | RSB_PMCR_DATA(PMIC_MODE_RSB) | RSB_PMCR_START);
44412faeba9SEmmanuel Vadot
4455b56413dSWarner Losh sc->iicbus = device_add_child(dev, "iicbus", DEVICE_UNIT_ANY);
4464b9a54a9SJared McNeill if (sc->iicbus == NULL) {
4474b9a54a9SJared McNeill device_printf(dev, "cannot add iicbus child device\n");
4484b9a54a9SJared McNeill error = ENXIO;
4494b9a54a9SJared McNeill goto fail;
4504b9a54a9SJared McNeill }
4514b9a54a9SJared McNeill
452*18250ec6SJohn Baldwin bus_attach_children(dev);
4534b9a54a9SJared McNeill
4544b9a54a9SJared McNeill return (0);
4554b9a54a9SJared McNeill
4564b9a54a9SJared McNeill fail:
4576a94069aSJared McNeill bus_release_resources(dev, rsb_spec, &sc->res);
4584b9a54a9SJared McNeill if (sc->rst != NULL)
4594b9a54a9SJared McNeill hwreset_release(sc->rst);
4604b9a54a9SJared McNeill if (sc->clk != NULL)
4614b9a54a9SJared McNeill clk_release(sc->clk);
4624b9a54a9SJared McNeill mtx_destroy(&sc->mtx);
4634b9a54a9SJared McNeill return (error);
4644b9a54a9SJared McNeill }
4654b9a54a9SJared McNeill
4664b9a54a9SJared McNeill static device_method_t rsb_methods[] = {
4674b9a54a9SJared McNeill /* Device interface */
4684b9a54a9SJared McNeill DEVMETHOD(device_probe, rsb_probe),
4694b9a54a9SJared McNeill DEVMETHOD(device_attach, rsb_attach),
4704b9a54a9SJared McNeill
47137cc9a03SJared McNeill /* Bus interface */
47237cc9a03SJared McNeill DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
47337cc9a03SJared McNeill DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
47437cc9a03SJared McNeill DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
47537cc9a03SJared McNeill DEVMETHOD(bus_release_resource, bus_generic_release_resource),
47637cc9a03SJared McNeill DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
47737cc9a03SJared McNeill DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
47837cc9a03SJared McNeill DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
47937cc9a03SJared McNeill DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
48037cc9a03SJared McNeill DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
48137cc9a03SJared McNeill
4824b9a54a9SJared McNeill /* OFW methods */
4834b9a54a9SJared McNeill DEVMETHOD(ofw_bus_get_node, rsb_get_node),
4844b9a54a9SJared McNeill
4854b9a54a9SJared McNeill /* iicbus interface */
4864b9a54a9SJared McNeill DEVMETHOD(iicbus_callback, iicbus_null_callback),
4874b9a54a9SJared McNeill DEVMETHOD(iicbus_reset, rsb_reset),
4884b9a54a9SJared McNeill DEVMETHOD(iicbus_transfer, rsb_transfer),
4894b9a54a9SJared McNeill
4904b9a54a9SJared McNeill DEVMETHOD_END
4914b9a54a9SJared McNeill };
4924b9a54a9SJared McNeill
4934b9a54a9SJared McNeill static driver_t rsb_driver = {
4944b9a54a9SJared McNeill "iichb",
4954b9a54a9SJared McNeill rsb_methods,
4964b9a54a9SJared McNeill sizeof(struct rsb_softc),
4974b9a54a9SJared McNeill };
4984b9a54a9SJared McNeill
499676ea8e1SJohn Baldwin EARLY_DRIVER_MODULE(iicbus, rsb, iicbus_driver, 0, 0,
500cfc8861bSEmmanuel Vadot BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
5017e1e2ba1SJohn Baldwin EARLY_DRIVER_MODULE(rsb, simplebus, rsb_driver, 0, 0,
502cfc8861bSEmmanuel Vadot BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
5034b9a54a9SJared McNeill MODULE_VERSION(rsb, 1);
5043de30075SEmmanuel Vadot MODULE_DEPEND(rsb, iicbus, 1, 1, 1);
5053de30075SEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
506