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 /* 304b9a54a9SJared McNeill * Allwinner RSB (Reduced Serial Bus) 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> 414b9a54a9SJared McNeill #include <sys/module.h> 424b9a54a9SJared McNeill #include <machine/bus.h> 434b9a54a9SJared McNeill 444b9a54a9SJared McNeill #include <dev/ofw/ofw_bus.h> 454b9a54a9SJared McNeill #include <dev/ofw/ofw_bus_subr.h> 464b9a54a9SJared McNeill 474b9a54a9SJared McNeill #include <dev/iicbus/iiconf.h> 484b9a54a9SJared McNeill #include <dev/iicbus/iicbus.h> 494b9a54a9SJared McNeill 504b9a54a9SJared McNeill #include <dev/extres/clk/clk.h> 514b9a54a9SJared McNeill #include <dev/extres/hwreset/hwreset.h> 524b9a54a9SJared McNeill 534b9a54a9SJared McNeill #include "iicbus_if.h" 544b9a54a9SJared McNeill 554b9a54a9SJared McNeill #define RSB_CTRL 0x00 564b9a54a9SJared McNeill #define START_TRANS (1 << 7) 574b9a54a9SJared McNeill #define GLOBAL_INT_ENB (1 << 1) 584b9a54a9SJared McNeill #define SOFT_RESET (1 << 0) 594b9a54a9SJared McNeill #define RSB_CCR 0x04 604b9a54a9SJared McNeill #define RSB_INTE 0x08 614b9a54a9SJared McNeill #define RSB_INTS 0x0c 624b9a54a9SJared McNeill #define INT_TRANS_ERR_ID(x) (((x) >> 8) & 0xf) 634b9a54a9SJared McNeill #define INT_LOAD_BSY (1 << 2) 644b9a54a9SJared McNeill #define INT_TRANS_ERR (1 << 1) 654b9a54a9SJared McNeill #define INT_TRANS_OVER (1 << 0) 664b9a54a9SJared McNeill #define INT_MASK (INT_LOAD_BSY|INT_TRANS_ERR|INT_TRANS_OVER) 674b9a54a9SJared McNeill #define RSB_DADDR0 0x10 684b9a54a9SJared McNeill #define RSB_DADDR1 0x14 694b9a54a9SJared McNeill #define RSB_DLEN 0x18 704b9a54a9SJared McNeill #define DLEN_READ (1 << 4) 714b9a54a9SJared McNeill #define RSB_DATA0 0x1c 724b9a54a9SJared McNeill #define RSB_DATA1 0x20 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 954b9a54a9SJared McNeill static struct ofw_compat_data compat_data[] = { 964b9a54a9SJared McNeill { "allwinner,sun8i-a23-rsb", 1 }, 974b9a54a9SJared McNeill { NULL, 0 } 984b9a54a9SJared McNeill }; 994b9a54a9SJared McNeill 1004b9a54a9SJared McNeill static struct resource_spec rsb_spec[] = { 1014b9a54a9SJared McNeill { SYS_RES_MEMORY, 0, RF_ACTIVE }, 1024b9a54a9SJared McNeill { -1, 0 } 1034b9a54a9SJared McNeill }; 1044b9a54a9SJared McNeill 1054b9a54a9SJared McNeill /* 1064b9a54a9SJared McNeill * Device address to Run-time address mappings. 1074b9a54a9SJared McNeill * 1084b9a54a9SJared McNeill * Run-time address (RTA) is an 8-bit value used to address the device during 1094b9a54a9SJared McNeill * a read or write transaction. The following are valid RTAs: 1104b9a54a9SJared McNeill * 0x17 0x2d 0x3a 0x4e 0x59 0x63 0x74 0x8b 0x9c 0xa6 0xb1 0xc5 0xd2 0xe8 0xff 1114b9a54a9SJared McNeill * 1124b9a54a9SJared McNeill * Allwinner uses RTA 0x2d for the primary PMIC, 0x3a for the secondary PMIC, 1134b9a54a9SJared McNeill * and 0x4e for the peripheral IC (where applicable). 1144b9a54a9SJared McNeill */ 1154b9a54a9SJared McNeill static const struct { 1164b9a54a9SJared McNeill uint16_t addr; 1174b9a54a9SJared McNeill uint8_t rta; 1184b9a54a9SJared McNeill } rsb_rtamap[] = { 1194b9a54a9SJared McNeill { .addr = RSB_ADDR_PMIC_PRIMARY, .rta = 0x2d }, 1204b9a54a9SJared McNeill { .addr = RSB_ADDR_PMIC_SECONDARY, .rta = 0x3a }, 1214b9a54a9SJared McNeill { .addr = RSB_ADDR_PERIPH_IC, .rta = 0x4e }, 1224b9a54a9SJared McNeill { .addr = 0, .rta = 0 } 1234b9a54a9SJared McNeill }; 1244b9a54a9SJared McNeill 1254b9a54a9SJared McNeill struct rsb_softc { 1266a94069aSJared McNeill struct resource *res; 1274b9a54a9SJared McNeill struct mtx mtx; 1284b9a54a9SJared McNeill clk_t clk; 1294b9a54a9SJared McNeill hwreset_t rst; 1304b9a54a9SJared McNeill device_t iicbus; 1314b9a54a9SJared McNeill int busy; 1324b9a54a9SJared McNeill uint32_t status; 1334b9a54a9SJared McNeill uint16_t cur_addr; 1344b9a54a9SJared McNeill 1354b9a54a9SJared McNeill struct iic_msg *msg; 1364b9a54a9SJared McNeill }; 1374b9a54a9SJared McNeill 1384b9a54a9SJared McNeill #define RSB_LOCK(sc) mtx_lock(&(sc)->mtx) 1394b9a54a9SJared McNeill #define RSB_UNLOCK(sc) mtx_unlock(&(sc)->mtx) 1404b9a54a9SJared McNeill #define RSB_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED) 1416a94069aSJared McNeill #define RSB_READ(sc, reg) bus_read_4((sc)->res, (reg)) 1426a94069aSJared McNeill #define RSB_WRITE(sc, reg, val) bus_write_4((sc)->res, (reg), (val)) 1434b9a54a9SJared McNeill 1444b9a54a9SJared McNeill static phandle_t 1454b9a54a9SJared McNeill rsb_get_node(device_t bus, device_t dev) 1464b9a54a9SJared McNeill { 1474b9a54a9SJared McNeill return (ofw_bus_get_node(bus)); 1484b9a54a9SJared McNeill } 1494b9a54a9SJared McNeill 1504b9a54a9SJared McNeill static int 1514b9a54a9SJared McNeill rsb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) 1524b9a54a9SJared McNeill { 1534b9a54a9SJared McNeill struct rsb_softc *sc; 1544b9a54a9SJared McNeill int retry; 1554b9a54a9SJared McNeill 1564b9a54a9SJared McNeill sc = device_get_softc(dev); 1574b9a54a9SJared McNeill 1584b9a54a9SJared McNeill RSB_LOCK(sc); 1594b9a54a9SJared McNeill 1604b9a54a9SJared McNeill /* Write soft-reset bit and wait for it to self-clear. */ 1614b9a54a9SJared McNeill RSB_WRITE(sc, RSB_CTRL, SOFT_RESET); 1624b9a54a9SJared McNeill for (retry = RSB_RESET_RETRY; retry > 0; retry--) 1634b9a54a9SJared McNeill if ((RSB_READ(sc, RSB_CTRL) & SOFT_RESET) == 0) 1644b9a54a9SJared McNeill break; 1654b9a54a9SJared McNeill 1664b9a54a9SJared McNeill RSB_UNLOCK(sc); 1674b9a54a9SJared McNeill 1684b9a54a9SJared McNeill if (retry == 0) { 1694b9a54a9SJared McNeill device_printf(dev, "soft reset timeout\n"); 1704b9a54a9SJared McNeill return (ETIMEDOUT); 1714b9a54a9SJared McNeill } 1724b9a54a9SJared McNeill 1734b9a54a9SJared McNeill return (IIC_ENOADDR); 1744b9a54a9SJared McNeill } 1754b9a54a9SJared McNeill 1764b9a54a9SJared McNeill static uint32_t 1774b9a54a9SJared McNeill rsb_encode(const uint8_t *buf, u_int len, u_int off) 1784b9a54a9SJared McNeill { 1794b9a54a9SJared McNeill uint32_t val; 1804b9a54a9SJared McNeill u_int n; 1814b9a54a9SJared McNeill 1824b9a54a9SJared McNeill val = 0; 1834b9a54a9SJared McNeill for (n = off; n < MIN(len, 4 + off); n++) 1844b9a54a9SJared McNeill val |= ((uint32_t)buf[n] << ((n - off) * NBBY)); 1854b9a54a9SJared McNeill 1864b9a54a9SJared McNeill return val; 1874b9a54a9SJared McNeill } 1884b9a54a9SJared McNeill 1894b9a54a9SJared McNeill static void 1904b9a54a9SJared McNeill rsb_decode(const uint32_t val, uint8_t *buf, u_int len, u_int off) 1914b9a54a9SJared McNeill { 1924b9a54a9SJared McNeill u_int n; 1934b9a54a9SJared McNeill 1944b9a54a9SJared McNeill for (n = off; n < MIN(len, 4 + off); n++) 1954b9a54a9SJared McNeill buf[n] = (val >> ((n - off) * NBBY)) & 0xff; 1964b9a54a9SJared McNeill } 1974b9a54a9SJared McNeill 1984b9a54a9SJared McNeill static int 1994b9a54a9SJared McNeill rsb_start(device_t dev) 2004b9a54a9SJared McNeill { 2014b9a54a9SJared McNeill struct rsb_softc *sc; 2026a94069aSJared McNeill int error, retry; 2034b9a54a9SJared McNeill 2044b9a54a9SJared McNeill sc = device_get_softc(dev); 2054b9a54a9SJared McNeill 2064b9a54a9SJared McNeill RSB_ASSERT_LOCKED(sc); 2074b9a54a9SJared McNeill 2084b9a54a9SJared McNeill /* Start the transfer */ 2094b9a54a9SJared McNeill RSB_WRITE(sc, RSB_CTRL, GLOBAL_INT_ENB | START_TRANS); 2104b9a54a9SJared McNeill 2114b9a54a9SJared McNeill /* Wait for transfer to complete */ 2124b9a54a9SJared McNeill error = ETIMEDOUT; 2134b9a54a9SJared McNeill for (retry = RSB_I2C_TIMEOUT; retry > 0; retry--) { 2144b9a54a9SJared McNeill sc->status |= RSB_READ(sc, RSB_INTS); 2154b9a54a9SJared McNeill if ((sc->status & INT_TRANS_OVER) != 0) { 2164b9a54a9SJared McNeill error = 0; 2174b9a54a9SJared McNeill break; 2184b9a54a9SJared McNeill } 2194b9a54a9SJared McNeill DELAY((1000 * hz) / RSB_I2C_TIMEOUT); 2204b9a54a9SJared McNeill } 2214b9a54a9SJared McNeill if (error == 0 && (sc->status & INT_TRANS_OVER) == 0) { 2224b9a54a9SJared McNeill device_printf(dev, "transfer error, status 0x%08x\n", 2234b9a54a9SJared McNeill sc->status); 2244b9a54a9SJared McNeill error = EIO; 2254b9a54a9SJared McNeill } 2264b9a54a9SJared McNeill 2274b9a54a9SJared McNeill return (error); 2284b9a54a9SJared McNeill 2294b9a54a9SJared McNeill } 2304b9a54a9SJared McNeill 2314b9a54a9SJared McNeill static int 2324b9a54a9SJared McNeill rsb_set_rta(device_t dev, uint16_t addr) 2334b9a54a9SJared McNeill { 2344b9a54a9SJared McNeill struct rsb_softc *sc; 2354b9a54a9SJared McNeill uint8_t rta; 2364b9a54a9SJared McNeill int i; 2374b9a54a9SJared McNeill 2384b9a54a9SJared McNeill sc = device_get_softc(dev); 2394b9a54a9SJared McNeill 2404b9a54a9SJared McNeill RSB_ASSERT_LOCKED(sc); 2414b9a54a9SJared McNeill 2424b9a54a9SJared McNeill /* Lookup run-time address for given device address */ 2434b9a54a9SJared McNeill for (rta = 0, i = 0; rsb_rtamap[i].rta != 0; i++) 2444b9a54a9SJared McNeill if (rsb_rtamap[i].addr == addr) { 2454b9a54a9SJared McNeill rta = rsb_rtamap[i].rta; 2464b9a54a9SJared McNeill break; 2474b9a54a9SJared McNeill } 2484b9a54a9SJared McNeill if (rta == 0) { 2494b9a54a9SJared McNeill device_printf(dev, "RTA not known for address %#x\n", addr); 2504b9a54a9SJared McNeill return (ENXIO); 2514b9a54a9SJared McNeill } 2524b9a54a9SJared McNeill 2534b9a54a9SJared McNeill /* Set run-time address */ 2544b9a54a9SJared McNeill RSB_WRITE(sc, RSB_INTS, RSB_READ(sc, RSB_INTS)); 2554b9a54a9SJared McNeill RSB_WRITE(sc, RSB_DAR, (addr << DAR_DA_SHIFT) | (rta << DAR_RTA_SHIFT)); 2564b9a54a9SJared McNeill RSB_WRITE(sc, RSB_CMD, CMD_SRTA); 2574b9a54a9SJared McNeill 2584b9a54a9SJared McNeill return (rsb_start(dev)); 2594b9a54a9SJared McNeill } 2604b9a54a9SJared McNeill 2614b9a54a9SJared McNeill static int 2624b9a54a9SJared McNeill rsb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) 2634b9a54a9SJared McNeill { 2644b9a54a9SJared McNeill struct rsb_softc *sc; 2654b9a54a9SJared McNeill uint32_t daddr[2], data[2], dlen; 2664b9a54a9SJared McNeill uint16_t device_addr; 2674b9a54a9SJared McNeill uint8_t cmd; 2684b9a54a9SJared McNeill int error; 2694b9a54a9SJared McNeill 2704b9a54a9SJared McNeill sc = device_get_softc(dev); 2714b9a54a9SJared McNeill 2724b9a54a9SJared McNeill /* 2734b9a54a9SJared McNeill * RSB is not really an I2C or SMBus controller, so there are some 2744b9a54a9SJared McNeill * restrictions imposed by the driver. 2754b9a54a9SJared McNeill * 2764b9a54a9SJared McNeill * Transfers must contain exactly two messages. The first is always 2774b9a54a9SJared McNeill * a write, containing a single data byte offset. Data will either 2784b9a54a9SJared McNeill * be read from or written to the corresponding data byte in the 2794b9a54a9SJared McNeill * second message. The slave address in both messages must be the 2804b9a54a9SJared McNeill * same. 2814b9a54a9SJared McNeill */ 2824b9a54a9SJared McNeill if (nmsgs != 2 || (msgs[0].flags & IIC_M_RD) == IIC_M_RD || 2834b9a54a9SJared McNeill (msgs[0].slave >> 1) != (msgs[1].slave >> 1) || 2844b9a54a9SJared McNeill msgs[0].len != 1 || msgs[1].len > RSB_MAXLEN) 2854b9a54a9SJared McNeill return (EINVAL); 2864b9a54a9SJared McNeill 2874b9a54a9SJared McNeill /* The controller can read or write 1, 2, or 4 bytes at a time. */ 2884b9a54a9SJared McNeill if ((msgs[1].flags & IIC_M_RD) != 0) { 2894b9a54a9SJared McNeill switch (msgs[1].len) { 2904b9a54a9SJared McNeill case 1: 2914b9a54a9SJared McNeill cmd = CMD_RD8; 2924b9a54a9SJared McNeill break; 2934b9a54a9SJared McNeill case 2: 2944b9a54a9SJared McNeill cmd = CMD_RD16; 2954b9a54a9SJared McNeill break; 2964b9a54a9SJared McNeill case 4: 2974b9a54a9SJared McNeill cmd = CMD_RD32; 2984b9a54a9SJared McNeill break; 2994b9a54a9SJared McNeill default: 3004b9a54a9SJared McNeill return (EINVAL); 3014b9a54a9SJared McNeill } 3024b9a54a9SJared McNeill } else { 3034b9a54a9SJared McNeill switch (msgs[1].len) { 3044b9a54a9SJared McNeill case 1: 3054b9a54a9SJared McNeill cmd = CMD_WR8; 3064b9a54a9SJared McNeill break; 3074b9a54a9SJared McNeill case 2: 3084b9a54a9SJared McNeill cmd = CMD_WR16; 3094b9a54a9SJared McNeill break; 3104b9a54a9SJared McNeill case 4: 3114b9a54a9SJared McNeill cmd = CMD_WR32; 3124b9a54a9SJared McNeill break; 3134b9a54a9SJared McNeill default: 3144b9a54a9SJared McNeill return (EINVAL); 3154b9a54a9SJared McNeill } 3164b9a54a9SJared McNeill } 3174b9a54a9SJared McNeill 3184b9a54a9SJared McNeill RSB_LOCK(sc); 3194b9a54a9SJared McNeill while (sc->busy) 3204b9a54a9SJared McNeill mtx_sleep(sc, &sc->mtx, 0, "i2cbuswait", 0); 3214b9a54a9SJared McNeill sc->busy = 1; 3224b9a54a9SJared McNeill sc->status = 0; 3234b9a54a9SJared McNeill 3244b9a54a9SJared McNeill /* Select current run-time address if necessary */ 3254b9a54a9SJared McNeill device_addr = msgs[0].slave >> 1; 3264b9a54a9SJared McNeill if (sc->cur_addr != device_addr) { 3274b9a54a9SJared McNeill error = rsb_set_rta(dev, device_addr); 3284b9a54a9SJared McNeill if (error != 0) 3294b9a54a9SJared McNeill goto done; 3304b9a54a9SJared McNeill sc->cur_addr = device_addr; 3314b9a54a9SJared McNeill sc->status = 0; 3324b9a54a9SJared McNeill } 3334b9a54a9SJared McNeill 3344b9a54a9SJared McNeill /* Clear interrupt status */ 3354b9a54a9SJared McNeill RSB_WRITE(sc, RSB_INTS, RSB_READ(sc, RSB_INTS)); 3364b9a54a9SJared McNeill 3374b9a54a9SJared McNeill /* Program data access address registers */ 3384b9a54a9SJared McNeill daddr[0] = rsb_encode(msgs[0].buf, msgs[0].len, 0); 3394b9a54a9SJared McNeill RSB_WRITE(sc, RSB_DADDR0, daddr[0]); 3404b9a54a9SJared McNeill 3414b9a54a9SJared McNeill /* Write data */ 3424b9a54a9SJared McNeill if ((msgs[1].flags & IIC_M_RD) == 0) { 3434b9a54a9SJared McNeill data[0] = rsb_encode(msgs[1].buf, msgs[1].len, 0); 3444b9a54a9SJared McNeill RSB_WRITE(sc, RSB_DATA0, data[0]); 3454b9a54a9SJared McNeill } 3464b9a54a9SJared McNeill 3474b9a54a9SJared McNeill /* Set command type */ 3484b9a54a9SJared McNeill RSB_WRITE(sc, RSB_CMD, cmd); 3494b9a54a9SJared McNeill 3504b9a54a9SJared McNeill /* Program data length register and transfer direction */ 3514b9a54a9SJared McNeill dlen = msgs[0].len - 1; 3524b9a54a9SJared McNeill if ((msgs[1].flags & IIC_M_RD) == IIC_M_RD) 3534b9a54a9SJared McNeill dlen |= DLEN_READ; 3544b9a54a9SJared McNeill RSB_WRITE(sc, RSB_DLEN, dlen); 3554b9a54a9SJared McNeill 3564b9a54a9SJared McNeill /* Start transfer */ 3574b9a54a9SJared McNeill error = rsb_start(dev); 3584b9a54a9SJared McNeill if (error != 0) 3594b9a54a9SJared McNeill goto done; 3604b9a54a9SJared McNeill 3614b9a54a9SJared McNeill /* Read data */ 3624b9a54a9SJared McNeill if ((msgs[1].flags & IIC_M_RD) == IIC_M_RD) { 3634b9a54a9SJared McNeill data[0] = RSB_READ(sc, RSB_DATA0); 3644b9a54a9SJared McNeill rsb_decode(data[0], msgs[1].buf, msgs[1].len, 0); 3654b9a54a9SJared McNeill } 3664b9a54a9SJared McNeill 3674b9a54a9SJared McNeill done: 3684b9a54a9SJared McNeill sc->msg = NULL; 3694b9a54a9SJared McNeill sc->busy = 0; 3704b9a54a9SJared McNeill wakeup(sc); 3714b9a54a9SJared McNeill RSB_UNLOCK(sc); 3724b9a54a9SJared McNeill 3734b9a54a9SJared McNeill return (error); 3744b9a54a9SJared McNeill } 3754b9a54a9SJared McNeill 3764b9a54a9SJared McNeill static int 3774b9a54a9SJared McNeill rsb_probe(device_t dev) 3784b9a54a9SJared McNeill { 3794b9a54a9SJared McNeill if (!ofw_bus_status_okay(dev)) 3804b9a54a9SJared McNeill return (ENXIO); 3814b9a54a9SJared McNeill 3824b9a54a9SJared McNeill if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 3834b9a54a9SJared McNeill return (ENXIO); 3844b9a54a9SJared McNeill 3854b9a54a9SJared McNeill device_set_desc(dev, "Allwinner RSB"); 3864b9a54a9SJared McNeill return (BUS_PROBE_DEFAULT); 3874b9a54a9SJared McNeill } 3884b9a54a9SJared McNeill 3894b9a54a9SJared McNeill static int 3904b9a54a9SJared McNeill rsb_attach(device_t dev) 3914b9a54a9SJared McNeill { 3924b9a54a9SJared McNeill struct rsb_softc *sc; 3934b9a54a9SJared McNeill int error; 3944b9a54a9SJared McNeill 3954b9a54a9SJared McNeill sc = device_get_softc(dev); 3964b9a54a9SJared McNeill mtx_init(&sc->mtx, device_get_nameunit(dev), "rsb", MTX_DEF); 3974b9a54a9SJared McNeill 398dac93553SMichal Meloun if (clk_get_by_ofw_index(dev, 0, 0, &sc->clk) == 0) { 3994b9a54a9SJared McNeill error = clk_enable(sc->clk); 4004b9a54a9SJared McNeill if (error != 0) { 4014b9a54a9SJared McNeill device_printf(dev, "cannot enable clock\n"); 4024b9a54a9SJared McNeill goto fail; 4034b9a54a9SJared McNeill } 4044b9a54a9SJared McNeill } 405dac93553SMichal Meloun if (hwreset_get_by_ofw_idx(dev, 0, 0, &sc->rst) == 0) { 4064b9a54a9SJared McNeill error = hwreset_deassert(sc->rst); 4074b9a54a9SJared McNeill if (error != 0) { 4084b9a54a9SJared McNeill device_printf(dev, "cannot de-assert reset\n"); 4094b9a54a9SJared McNeill goto fail; 4104b9a54a9SJared McNeill } 4114b9a54a9SJared McNeill } 4124b9a54a9SJared McNeill 4136a94069aSJared McNeill if (bus_alloc_resources(dev, rsb_spec, &sc->res) != 0) { 4144b9a54a9SJared McNeill device_printf(dev, "cannot allocate resources for device\n"); 4154b9a54a9SJared McNeill error = ENXIO; 4164b9a54a9SJared McNeill goto fail; 4174b9a54a9SJared McNeill } 4184b9a54a9SJared McNeill 4194b9a54a9SJared McNeill sc->iicbus = device_add_child(dev, "iicbus", -1); 4204b9a54a9SJared McNeill if (sc->iicbus == NULL) { 4214b9a54a9SJared McNeill device_printf(dev, "cannot add iicbus child device\n"); 4224b9a54a9SJared McNeill error = ENXIO; 4234b9a54a9SJared McNeill goto fail; 4244b9a54a9SJared McNeill } 4254b9a54a9SJared McNeill 4264b9a54a9SJared McNeill bus_generic_attach(dev); 4274b9a54a9SJared McNeill 4284b9a54a9SJared McNeill return (0); 4294b9a54a9SJared McNeill 4304b9a54a9SJared McNeill fail: 4316a94069aSJared McNeill bus_release_resources(dev, rsb_spec, &sc->res); 4324b9a54a9SJared McNeill if (sc->rst != NULL) 4334b9a54a9SJared McNeill hwreset_release(sc->rst); 4344b9a54a9SJared McNeill if (sc->clk != NULL) 4354b9a54a9SJared McNeill clk_release(sc->clk); 4364b9a54a9SJared McNeill mtx_destroy(&sc->mtx); 4374b9a54a9SJared McNeill return (error); 4384b9a54a9SJared McNeill } 4394b9a54a9SJared McNeill 4404b9a54a9SJared McNeill static device_method_t rsb_methods[] = { 4414b9a54a9SJared McNeill /* Device interface */ 4424b9a54a9SJared McNeill DEVMETHOD(device_probe, rsb_probe), 4434b9a54a9SJared McNeill DEVMETHOD(device_attach, rsb_attach), 4444b9a54a9SJared McNeill 44537cc9a03SJared McNeill /* Bus interface */ 44637cc9a03SJared McNeill DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 44737cc9a03SJared McNeill DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 44837cc9a03SJared McNeill DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 44937cc9a03SJared McNeill DEVMETHOD(bus_release_resource, bus_generic_release_resource), 45037cc9a03SJared McNeill DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 45137cc9a03SJared McNeill DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 45237cc9a03SJared McNeill DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 45337cc9a03SJared McNeill DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 45437cc9a03SJared McNeill DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 45537cc9a03SJared McNeill 4564b9a54a9SJared McNeill /* OFW methods */ 4574b9a54a9SJared McNeill DEVMETHOD(ofw_bus_get_node, rsb_get_node), 4584b9a54a9SJared McNeill 4594b9a54a9SJared McNeill /* iicbus interface */ 4604b9a54a9SJared McNeill DEVMETHOD(iicbus_callback, iicbus_null_callback), 4614b9a54a9SJared McNeill DEVMETHOD(iicbus_reset, rsb_reset), 4624b9a54a9SJared McNeill DEVMETHOD(iicbus_transfer, rsb_transfer), 4634b9a54a9SJared McNeill 4644b9a54a9SJared McNeill DEVMETHOD_END 4654b9a54a9SJared McNeill }; 4664b9a54a9SJared McNeill 4674b9a54a9SJared McNeill static driver_t rsb_driver = { 4684b9a54a9SJared McNeill "iichb", 4694b9a54a9SJared McNeill rsb_methods, 4704b9a54a9SJared McNeill sizeof(struct rsb_softc), 4714b9a54a9SJared McNeill }; 4724b9a54a9SJared McNeill 4734b9a54a9SJared McNeill static devclass_t rsb_devclass; 4744b9a54a9SJared McNeill 475*221a9d6dSJared McNeill EARLY_DRIVER_MODULE(iicbus, rsb, iicbus_driver, iicbus_devclass, 0, 0, 476*221a9d6dSJared McNeill BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE); 477*221a9d6dSJared McNeill EARLY_DRIVER_MODULE(rsb, simplebus, rsb_driver, rsb_devclass, 0, 0, 478*221a9d6dSJared McNeill BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE); 4794b9a54a9SJared McNeill MODULE_VERSION(rsb, 1); 480