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