1be9ddf43SLuiz Otavio O Souza /*- 2be9ddf43SLuiz Otavio O Souza * Copyright (c) 2001 Tsubai Masanari. 3be9ddf43SLuiz Otavio O Souza * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org> 4be9ddf43SLuiz Otavio O Souza * Copyright (c) 2013 Luiz Otavio O Souza <loos@freebsd.org> 5be9ddf43SLuiz Otavio O Souza * All rights reserved. 6be9ddf43SLuiz Otavio O Souza * 7be9ddf43SLuiz Otavio O Souza * Redistribution and use in source and binary forms, with or without 8be9ddf43SLuiz Otavio O Souza * modification, are permitted provided that the following conditions 9be9ddf43SLuiz Otavio O Souza * are met: 10be9ddf43SLuiz Otavio O Souza * 1. Redistributions of source code must retain the above copyright 11be9ddf43SLuiz Otavio O Souza * notice, this list of conditions and the following disclaimer. 12be9ddf43SLuiz Otavio O Souza * 2. Redistributions in binary form must reproduce the above copyright 13be9ddf43SLuiz Otavio O Souza * notice, this list of conditions and the following disclaimer in the 14be9ddf43SLuiz Otavio O Souza * documentation and/or other materials provided with the distribution. 15be9ddf43SLuiz Otavio O Souza * 16be9ddf43SLuiz Otavio O Souza * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17be9ddf43SLuiz Otavio O Souza * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18be9ddf43SLuiz Otavio O Souza * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19be9ddf43SLuiz Otavio O Souza * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20be9ddf43SLuiz Otavio O Souza * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21be9ddf43SLuiz Otavio O Souza * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22be9ddf43SLuiz Otavio O Souza * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23be9ddf43SLuiz Otavio O Souza * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24be9ddf43SLuiz Otavio O Souza * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25be9ddf43SLuiz Otavio O Souza * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26be9ddf43SLuiz Otavio O Souza * SUCH DAMAGE. 27be9ddf43SLuiz Otavio O Souza * 28be9ddf43SLuiz Otavio O Souza */ 29be9ddf43SLuiz Otavio O Souza #include <sys/cdefs.h> 30be9ddf43SLuiz Otavio O Souza __FBSDID("$FreeBSD$"); 31be9ddf43SLuiz Otavio O Souza 32be9ddf43SLuiz Otavio O Souza #include <sys/param.h> 33be9ddf43SLuiz Otavio O Souza #include <sys/systm.h> 34be9ddf43SLuiz Otavio O Souza #include <sys/kernel.h> 35be9ddf43SLuiz Otavio O Souza #include <sys/lock.h> 36be9ddf43SLuiz Otavio O Souza #include <sys/module.h> 37be9ddf43SLuiz Otavio O Souza #include <sys/mutex.h> 38be9ddf43SLuiz Otavio O Souza #include <sys/bus.h> 39be9ddf43SLuiz Otavio O Souza #include <machine/resource.h> 40be9ddf43SLuiz Otavio O Souza #include <machine/bus.h> 41be9ddf43SLuiz Otavio O Souza #include <sys/rman.h> 42be9ddf43SLuiz Otavio O Souza #include <sys/sysctl.h> 43be9ddf43SLuiz Otavio O Souza 44be9ddf43SLuiz Otavio O Souza #include <dev/iicbus/iicbus.h> 45be9ddf43SLuiz Otavio O Souza #include <dev/iicbus/iiconf.h> 46be9ddf43SLuiz Otavio O Souza #include <dev/ofw/ofw_bus.h> 47be9ddf43SLuiz Otavio O Souza #include <dev/ofw/ofw_bus_subr.h> 48be9ddf43SLuiz Otavio O Souza 49be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_gpio.h> 50be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_bscreg.h> 51be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_bscvar.h> 52be9ddf43SLuiz Otavio O Souza 53be9ddf43SLuiz Otavio O Souza #include "iicbus_if.h" 54be9ddf43SLuiz Otavio O Souza 55*9d6eb8bbSOleksandr Tymoshenko static struct ofw_compat_data compat_data[] = { 56*9d6eb8bbSOleksandr Tymoshenko {"broadcom,bcm2835-bsc", 1}, 57*9d6eb8bbSOleksandr Tymoshenko {"brcm,bcm2708-i2c", 1}, 58*9d6eb8bbSOleksandr Tymoshenko {NULL, 0} 59*9d6eb8bbSOleksandr Tymoshenko }; 60*9d6eb8bbSOleksandr Tymoshenko 61be9ddf43SLuiz Otavio O Souza static void bcm_bsc_intr(void *); 629e93dfcfSLuiz Otavio O Souza static int bcm_bsc_detach(device_t); 63be9ddf43SLuiz Otavio O Souza 64be9ddf43SLuiz Otavio O Souza static void 65be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(struct bcm_bsc_softc *sc, uint32_t off, uint32_t mask, 66be9ddf43SLuiz Otavio O Souza uint32_t value) 67be9ddf43SLuiz Otavio O Souza { 68be9ddf43SLuiz Otavio O Souza uint32_t reg; 69be9ddf43SLuiz Otavio O Souza 70be9ddf43SLuiz Otavio O Souza mtx_assert(&sc->sc_mtx, MA_OWNED); 71be9ddf43SLuiz Otavio O Souza reg = BCM_BSC_READ(sc, off); 72be9ddf43SLuiz Otavio O Souza reg &= ~mask; 73be9ddf43SLuiz Otavio O Souza reg |= value; 74be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, off, reg); 75be9ddf43SLuiz Otavio O Souza } 76be9ddf43SLuiz Otavio O Souza 77be9ddf43SLuiz Otavio O Souza static int 78be9ddf43SLuiz Otavio O Souza bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS) 79be9ddf43SLuiz Otavio O Souza { 80be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 81be9ddf43SLuiz Otavio O Souza uint32_t clk; 82be9ddf43SLuiz Otavio O Souza 83be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg1; 84be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 85be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK); 86be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 87be9ddf43SLuiz Otavio O Souza clk &= 0xffff; 88be9ddf43SLuiz Otavio O Souza if (clk == 0) 89be9ddf43SLuiz Otavio O Souza clk = 32768; 90be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_CORE_CLK / clk; 91be9ddf43SLuiz Otavio O Souza 929e93dfcfSLuiz Otavio O Souza return (sysctl_handle_int(oidp, &clk, 0, req)); 93be9ddf43SLuiz Otavio O Souza } 94be9ddf43SLuiz Otavio O Souza 95be9ddf43SLuiz Otavio O Souza static int 96be9ddf43SLuiz Otavio O Souza bcm_bsc_clkt_proc(SYSCTL_HANDLER_ARGS) 97be9ddf43SLuiz Otavio O Souza { 98be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 99be9ddf43SLuiz Otavio O Souza uint32_t clkt; 100be9ddf43SLuiz Otavio O Souza int error; 101be9ddf43SLuiz Otavio O Souza 102be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg1; 103be9ddf43SLuiz Otavio O Souza 104be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 105be9ddf43SLuiz Otavio O Souza clkt = BCM_BSC_READ(sc, BCM_BSC_CLKT); 106be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 107be9ddf43SLuiz Otavio O Souza clkt &= 0xffff; 108be9ddf43SLuiz Otavio O Souza error = sysctl_handle_int(oidp, &clkt, sizeof(clkt), req); 109be9ddf43SLuiz Otavio O Souza if (error != 0 || req->newptr == NULL) 110be9ddf43SLuiz Otavio O Souza return (error); 111be9ddf43SLuiz Otavio O Souza 112be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 113be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CLKT, clkt & 0xffff); 114be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 115be9ddf43SLuiz Otavio O Souza 116be9ddf43SLuiz Otavio O Souza return (0); 117be9ddf43SLuiz Otavio O Souza } 118be9ddf43SLuiz Otavio O Souza 119be9ddf43SLuiz Otavio O Souza static int 120be9ddf43SLuiz Otavio O Souza bcm_bsc_fall_proc(SYSCTL_HANDLER_ARGS) 121be9ddf43SLuiz Otavio O Souza { 122be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 123be9ddf43SLuiz Otavio O Souza uint32_t clk, reg; 124be9ddf43SLuiz Otavio O Souza int error; 125be9ddf43SLuiz Otavio O Souza 126be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg1; 127be9ddf43SLuiz Otavio O Souza 128be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 129be9ddf43SLuiz Otavio O Souza reg = BCM_BSC_READ(sc, BCM_BSC_DELAY); 130be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 131be9ddf43SLuiz Otavio O Souza reg >>= 16; 132be9ddf43SLuiz Otavio O Souza error = sysctl_handle_int(oidp, ®, sizeof(reg), req); 133be9ddf43SLuiz Otavio O Souza if (error != 0 || req->newptr == NULL) 134be9ddf43SLuiz Otavio O Souza return (error); 135be9ddf43SLuiz Otavio O Souza 136be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 137be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK); 138be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_CORE_CLK / clk; 139be9ddf43SLuiz Otavio O Souza if (reg > clk / 2) 140be9ddf43SLuiz Otavio O Souza reg = clk / 2 - 1; 141be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff0000, reg << 16); 142be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 143be9ddf43SLuiz Otavio O Souza 144be9ddf43SLuiz Otavio O Souza return (0); 145be9ddf43SLuiz Otavio O Souza } 146be9ddf43SLuiz Otavio O Souza 147be9ddf43SLuiz Otavio O Souza static int 148be9ddf43SLuiz Otavio O Souza bcm_bsc_rise_proc(SYSCTL_HANDLER_ARGS) 149be9ddf43SLuiz Otavio O Souza { 150be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 151be9ddf43SLuiz Otavio O Souza uint32_t clk, reg; 152be9ddf43SLuiz Otavio O Souza int error; 153be9ddf43SLuiz Otavio O Souza 154be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg1; 155be9ddf43SLuiz Otavio O Souza 156be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 157be9ddf43SLuiz Otavio O Souza reg = BCM_BSC_READ(sc, BCM_BSC_DELAY); 158be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 159be9ddf43SLuiz Otavio O Souza reg &= 0xffff; 160be9ddf43SLuiz Otavio O Souza error = sysctl_handle_int(oidp, ®, sizeof(reg), req); 161be9ddf43SLuiz Otavio O Souza if (error != 0 || req->newptr == NULL) 162be9ddf43SLuiz Otavio O Souza return (error); 163be9ddf43SLuiz Otavio O Souza 164be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 165be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK); 166be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_CORE_CLK / clk; 167be9ddf43SLuiz Otavio O Souza if (reg > clk / 2) 168be9ddf43SLuiz Otavio O Souza reg = clk / 2 - 1; 169be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff, reg); 170be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 171be9ddf43SLuiz Otavio O Souza 172be9ddf43SLuiz Otavio O Souza return (0); 173be9ddf43SLuiz Otavio O Souza } 174be9ddf43SLuiz Otavio O Souza 175be9ddf43SLuiz Otavio O Souza static void 176be9ddf43SLuiz Otavio O Souza bcm_bsc_sysctl_init(struct bcm_bsc_softc *sc) 177be9ddf43SLuiz Otavio O Souza { 178be9ddf43SLuiz Otavio O Souza struct sysctl_ctx_list *ctx; 179be9ddf43SLuiz Otavio O Souza struct sysctl_oid *tree_node; 180be9ddf43SLuiz Otavio O Souza struct sysctl_oid_list *tree; 181be9ddf43SLuiz Otavio O Souza 182be9ddf43SLuiz Otavio O Souza /* 183be9ddf43SLuiz Otavio O Souza * Add system sysctl tree/handlers. 184be9ddf43SLuiz Otavio O Souza */ 185be9ddf43SLuiz Otavio O Souza ctx = device_get_sysctl_ctx(sc->sc_dev); 186be9ddf43SLuiz Otavio O Souza tree_node = device_get_sysctl_tree(sc->sc_dev); 187be9ddf43SLuiz Otavio O Souza tree = SYSCTL_CHILDREN(tree_node); 1889e93dfcfSLuiz Otavio O Souza SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "frequency", 189be9ddf43SLuiz Otavio O Souza CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc), 190be9ddf43SLuiz Otavio O Souza bcm_bsc_clock_proc, "IU", "I2C BUS clock frequency"); 191be9ddf43SLuiz Otavio O Souza SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock_stretch", 192be9ddf43SLuiz Otavio O Souza CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc), 193be9ddf43SLuiz Otavio O Souza bcm_bsc_clkt_proc, "IU", "I2C BUS clock stretch timeout"); 194be9ddf43SLuiz Otavio O Souza SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "fall_edge_delay", 195be9ddf43SLuiz Otavio O Souza CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc), 196be9ddf43SLuiz Otavio O Souza bcm_bsc_fall_proc, "IU", "I2C BUS falling edge delay"); 197be9ddf43SLuiz Otavio O Souza SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rise_edge_delay", 198be9ddf43SLuiz Otavio O Souza CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc), 199be9ddf43SLuiz Otavio O Souza bcm_bsc_rise_proc, "IU", "I2C BUS rising edge delay"); 200be9ddf43SLuiz Otavio O Souza } 201be9ddf43SLuiz Otavio O Souza 202be9ddf43SLuiz Otavio O Souza static void 203be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(struct bcm_bsc_softc *sc) 204be9ddf43SLuiz Otavio O Souza { 205be9ddf43SLuiz Otavio O Souza 206e50c6241SLuiz Otavio O Souza /* Enable the BSC Controller, disable interrupts. */ 207e50c6241SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN); 208be9ddf43SLuiz Otavio O Souza /* Clear pending interrupts. */ 209be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_STATUS, BCM_BSC_STATUS_CLKT | 210be9ddf43SLuiz Otavio O Souza BCM_BSC_STATUS_ERR | BCM_BSC_STATUS_DONE); 211be9ddf43SLuiz Otavio O Souza /* Clear the FIFO. */ 212be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_CLEAR0, 213be9ddf43SLuiz Otavio O Souza BCM_BSC_CTRL_CLEAR0); 214be9ddf43SLuiz Otavio O Souza } 215be9ddf43SLuiz Otavio O Souza 216be9ddf43SLuiz Otavio O Souza static int 217be9ddf43SLuiz Otavio O Souza bcm_bsc_probe(device_t dev) 218be9ddf43SLuiz Otavio O Souza { 219be9ddf43SLuiz Otavio O Souza 220add35ed5SIan Lepore if (!ofw_bus_status_okay(dev)) 221add35ed5SIan Lepore return (ENXIO); 222add35ed5SIan Lepore 223*9d6eb8bbSOleksandr Tymoshenko if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 224be9ddf43SLuiz Otavio O Souza return (ENXIO); 225be9ddf43SLuiz Otavio O Souza 226be9ddf43SLuiz Otavio O Souza device_set_desc(dev, "BCM2708/2835 BSC controller"); 227be9ddf43SLuiz Otavio O Souza 228be9ddf43SLuiz Otavio O Souza return (BUS_PROBE_DEFAULT); 229be9ddf43SLuiz Otavio O Souza } 230be9ddf43SLuiz Otavio O Souza 231be9ddf43SLuiz Otavio O Souza static int 232be9ddf43SLuiz Otavio O Souza bcm_bsc_attach(device_t dev) 233be9ddf43SLuiz Otavio O Souza { 234be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 23521cb1342SLuiz Otavio O Souza unsigned long start; 236be9ddf43SLuiz Otavio O Souza device_t gpio; 23721cb1342SLuiz Otavio O Souza int i, rid; 238be9ddf43SLuiz Otavio O Souza 239be9ddf43SLuiz Otavio O Souza sc = device_get_softc(dev); 240be9ddf43SLuiz Otavio O Souza sc->sc_dev = dev; 241be9ddf43SLuiz Otavio O Souza 242be9ddf43SLuiz Otavio O Souza rid = 0; 243be9ddf43SLuiz Otavio O Souza sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 244be9ddf43SLuiz Otavio O Souza RF_ACTIVE); 245be9ddf43SLuiz Otavio O Souza if (!sc->sc_mem_res) { 246be9ddf43SLuiz Otavio O Souza device_printf(dev, "cannot allocate memory window\n"); 247be9ddf43SLuiz Otavio O Souza return (ENXIO); 248be9ddf43SLuiz Otavio O Souza } 249be9ddf43SLuiz Otavio O Souza 250be9ddf43SLuiz Otavio O Souza sc->sc_bst = rman_get_bustag(sc->sc_mem_res); 251be9ddf43SLuiz Otavio O Souza sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res); 252be9ddf43SLuiz Otavio O Souza 25321cb1342SLuiz Otavio O Souza /* Check the unit we are attaching by its base address. */ 25421cb1342SLuiz Otavio O Souza start = rman_get_start(sc->sc_mem_res); 25521cb1342SLuiz Otavio O Souza for (i = 0; i < nitems(bcm_bsc_pins); i++) { 256b4ae2f3cSLuiz Otavio O Souza if (bcm_bsc_pins[i].start == (start & BCM_BSC_BASE_MASK)) 25721cb1342SLuiz Otavio O Souza break; 25821cb1342SLuiz Otavio O Souza } 25921cb1342SLuiz Otavio O Souza if (i == nitems(bcm_bsc_pins)) { 26021cb1342SLuiz Otavio O Souza device_printf(dev, "only bsc0 and bsc1 are supported\n"); 26102a42e68SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); 26221cb1342SLuiz Otavio O Souza return (ENXIO); 26321cb1342SLuiz Otavio O Souza } 26421cb1342SLuiz Otavio O Souza 26521cb1342SLuiz Otavio O Souza /* 26621cb1342SLuiz Otavio O Souza * Configure the GPIO pins to ALT0 function to enable BSC control 26721cb1342SLuiz Otavio O Souza * over the pins. 26821cb1342SLuiz Otavio O Souza */ 26921cb1342SLuiz Otavio O Souza gpio = devclass_get_device(devclass_find("gpio"), 0); 27021cb1342SLuiz Otavio O Souza if (!gpio) { 27121cb1342SLuiz Otavio O Souza device_printf(dev, "cannot find gpio0\n"); 27202a42e68SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); 27321cb1342SLuiz Otavio O Souza return (ENXIO); 27421cb1342SLuiz Otavio O Souza } 27521cb1342SLuiz Otavio O Souza bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].sda, BCM_GPIO_ALT0); 27621cb1342SLuiz Otavio O Souza bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].scl, BCM_GPIO_ALT0); 27721cb1342SLuiz Otavio O Souza 278be9ddf43SLuiz Otavio O Souza rid = 0; 279be9ddf43SLuiz Otavio O Souza sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 280be9ddf43SLuiz Otavio O Souza RF_ACTIVE | RF_SHAREABLE); 281be9ddf43SLuiz Otavio O Souza if (!sc->sc_irq_res) { 282be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); 283be9ddf43SLuiz Otavio O Souza device_printf(dev, "cannot allocate interrupt\n"); 284be9ddf43SLuiz Otavio O Souza return (ENXIO); 285be9ddf43SLuiz Otavio O Souza } 286be9ddf43SLuiz Otavio O Souza 287be9ddf43SLuiz Otavio O Souza /* Hook up our interrupt handler. */ 288be9ddf43SLuiz Otavio O Souza if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE, 289be9ddf43SLuiz Otavio O Souza NULL, bcm_bsc_intr, sc, &sc->sc_intrhand)) { 290be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res); 291be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); 292be9ddf43SLuiz Otavio O Souza device_printf(dev, "cannot setup the interrupt handler\n"); 293be9ddf43SLuiz Otavio O Souza return (ENXIO); 294be9ddf43SLuiz Otavio O Souza } 295be9ddf43SLuiz Otavio O Souza 296be9ddf43SLuiz Otavio O Souza mtx_init(&sc->sc_mtx, "bcm_bsc", NULL, MTX_DEF); 297be9ddf43SLuiz Otavio O Souza 298be9ddf43SLuiz Otavio O Souza bcm_bsc_sysctl_init(sc); 299be9ddf43SLuiz Otavio O Souza 300be9ddf43SLuiz Otavio O Souza /* Enable the BSC controller. Flush the FIFO. */ 301be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 302be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(sc); 303be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 304be9ddf43SLuiz Otavio O Souza 3059e93dfcfSLuiz Otavio O Souza sc->sc_iicbus = device_add_child(dev, "iicbus", -1); 3069e93dfcfSLuiz Otavio O Souza if (sc->sc_iicbus == NULL) { 3079e93dfcfSLuiz Otavio O Souza bcm_bsc_detach(dev); 3089e93dfcfSLuiz Otavio O Souza return (ENXIO); 3099e93dfcfSLuiz Otavio O Souza } 310be9ddf43SLuiz Otavio O Souza 311be9ddf43SLuiz Otavio O Souza return (bus_generic_attach(dev)); 312be9ddf43SLuiz Otavio O Souza } 313be9ddf43SLuiz Otavio O Souza 314be9ddf43SLuiz Otavio O Souza static int 315be9ddf43SLuiz Otavio O Souza bcm_bsc_detach(device_t dev) 316be9ddf43SLuiz Otavio O Souza { 317be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 318be9ddf43SLuiz Otavio O Souza 319be9ddf43SLuiz Otavio O Souza bus_generic_detach(dev); 320be9ddf43SLuiz Otavio O Souza 321be9ddf43SLuiz Otavio O Souza sc = device_get_softc(dev); 322be9ddf43SLuiz Otavio O Souza mtx_destroy(&sc->sc_mtx); 323be9ddf43SLuiz Otavio O Souza if (sc->sc_intrhand) 324be9ddf43SLuiz Otavio O Souza bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand); 325be9ddf43SLuiz Otavio O Souza if (sc->sc_irq_res) 326be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res); 327be9ddf43SLuiz Otavio O Souza if (sc->sc_mem_res) 328be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); 329be9ddf43SLuiz Otavio O Souza 330be9ddf43SLuiz Otavio O Souza return (0); 331be9ddf43SLuiz Otavio O Souza } 332be9ddf43SLuiz Otavio O Souza 333be9ddf43SLuiz Otavio O Souza static void 334be9ddf43SLuiz Otavio O Souza bcm_bsc_intr(void *arg) 335be9ddf43SLuiz Otavio O Souza { 336be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 337be9ddf43SLuiz Otavio O Souza uint32_t status; 338be9ddf43SLuiz Otavio O Souza 339be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg; 340be9ddf43SLuiz Otavio O Souza 341be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 342be9ddf43SLuiz Otavio O Souza 343be9ddf43SLuiz Otavio O Souza /* The I2C interrupt is shared among all the BSC controllers. */ 344be9ddf43SLuiz Otavio O Souza if ((sc->sc_flags & BCM_I2C_BUSY) == 0) { 345be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 346be9ddf43SLuiz Otavio O Souza return; 347be9ddf43SLuiz Otavio O Souza } 348be9ddf43SLuiz Otavio O Souza 349be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS); 350be9ddf43SLuiz Otavio O Souza 351be9ddf43SLuiz Otavio O Souza /* Check for errors. */ 352be9ddf43SLuiz Otavio O Souza if (status & (BCM_BSC_STATUS_CLKT | BCM_BSC_STATUS_ERR)) { 353be9ddf43SLuiz Otavio O Souza /* Disable interrupts. */ 354be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(sc); 355e50c6241SLuiz Otavio O Souza sc->sc_flags |= BCM_I2C_ERROR; 356be9ddf43SLuiz Otavio O Souza wakeup(sc->sc_dev); 357be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 358be9ddf43SLuiz Otavio O Souza return; 359be9ddf43SLuiz Otavio O Souza } 360be9ddf43SLuiz Otavio O Souza 361be9ddf43SLuiz Otavio O Souza if (sc->sc_flags & BCM_I2C_READ) { 362be9ddf43SLuiz Otavio O Souza while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD)) { 363be9ddf43SLuiz Otavio O Souza *sc->sc_data++ = BCM_BSC_READ(sc, BCM_BSC_DATA); 364be9ddf43SLuiz Otavio O Souza sc->sc_resid--; 365be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS); 366be9ddf43SLuiz Otavio O Souza } 367be9ddf43SLuiz Otavio O Souza } else { 368be9ddf43SLuiz Otavio O Souza while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD)) { 369be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data++); 370be9ddf43SLuiz Otavio O Souza sc->sc_resid--; 371be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS); 372be9ddf43SLuiz Otavio O Souza } 373be9ddf43SLuiz Otavio O Souza } 374be9ddf43SLuiz Otavio O Souza 375be9ddf43SLuiz Otavio O Souza if (status & BCM_BSC_STATUS_DONE) { 376be9ddf43SLuiz Otavio O Souza /* Disable interrupts. */ 377be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(sc); 378be9ddf43SLuiz Otavio O Souza wakeup(sc->sc_dev); 379be9ddf43SLuiz Otavio O Souza } 380be9ddf43SLuiz Otavio O Souza 381be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 382be9ddf43SLuiz Otavio O Souza } 383be9ddf43SLuiz Otavio O Souza 384be9ddf43SLuiz Otavio O Souza static int 385be9ddf43SLuiz Otavio O Souza bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) 386be9ddf43SLuiz Otavio O Souza { 387be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 388be9ddf43SLuiz Otavio O Souza uint32_t intr, read, status; 389be9ddf43SLuiz Otavio O Souza int i, err; 390be9ddf43SLuiz Otavio O Souza 391be9ddf43SLuiz Otavio O Souza sc = device_get_softc(dev); 392be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 393be9ddf43SLuiz Otavio O Souza 394be9ddf43SLuiz Otavio O Souza /* If the controller is busy wait until it is available. */ 395be9ddf43SLuiz Otavio O Souza while (sc->sc_flags & BCM_I2C_BUSY) 396a141b672SLuiz Otavio O Souza mtx_sleep(dev, &sc->sc_mtx, 0, "bscbusw", 0); 397be9ddf43SLuiz Otavio O Souza 398be9ddf43SLuiz Otavio O Souza /* Now we have control over the BSC controller. */ 399be9ddf43SLuiz Otavio O Souza sc->sc_flags = BCM_I2C_BUSY; 400be9ddf43SLuiz Otavio O Souza 401be9ddf43SLuiz Otavio O Souza /* Clear the FIFO and the pending interrupts. */ 402be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(sc); 403be9ddf43SLuiz Otavio O Souza 404be9ddf43SLuiz Otavio O Souza err = 0; 405be9ddf43SLuiz Otavio O Souza for (i = 0; i < nmsgs; i++) { 406be9ddf43SLuiz Otavio O Souza 407be9ddf43SLuiz Otavio O Souza /* Write the slave address. */ 408d6cf3c63SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, msgs[i].slave >> 1); 409be9ddf43SLuiz Otavio O Souza 410be9ddf43SLuiz Otavio O Souza /* Write the data length. */ 411be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_DLEN, msgs[i].len); 412be9ddf43SLuiz Otavio O Souza 413be9ddf43SLuiz Otavio O Souza sc->sc_data = msgs[i].buf; 414be9ddf43SLuiz Otavio O Souza sc->sc_resid = msgs[i].len; 415be9ddf43SLuiz Otavio O Souza if ((msgs[i].flags & IIC_M_RD) == 0) { 416be9ddf43SLuiz Otavio O Souza /* Fill up the TX FIFO. */ 417be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS); 418be9ddf43SLuiz Otavio O Souza while (sc->sc_resid > 0 && 419be9ddf43SLuiz Otavio O Souza (status & BCM_BSC_STATUS_TXD)) { 420be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data); 421be9ddf43SLuiz Otavio O Souza sc->sc_data++; 422be9ddf43SLuiz Otavio O Souza sc->sc_resid--; 423be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS); 424be9ddf43SLuiz Otavio O Souza } 425be9ddf43SLuiz Otavio O Souza read = 0; 426be9ddf43SLuiz Otavio O Souza intr = BCM_BSC_CTRL_INTT; 427be9ddf43SLuiz Otavio O Souza sc->sc_flags &= ~BCM_I2C_READ; 428be9ddf43SLuiz Otavio O Souza } else { 429be9ddf43SLuiz Otavio O Souza sc->sc_flags |= BCM_I2C_READ; 430be9ddf43SLuiz Otavio O Souza read = BCM_BSC_CTRL_READ; 431be9ddf43SLuiz Otavio O Souza intr = BCM_BSC_CTRL_INTR; 432be9ddf43SLuiz Otavio O Souza } 433be9ddf43SLuiz Otavio O Souza intr |= BCM_BSC_CTRL_INTD; 434be9ddf43SLuiz Otavio O Souza 435be9ddf43SLuiz Otavio O Souza /* Start the transfer. */ 436be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN | 437be9ddf43SLuiz Otavio O Souza BCM_BSC_CTRL_ST | read | intr); 438be9ddf43SLuiz Otavio O Souza 439be9ddf43SLuiz Otavio O Souza /* Wait for the transaction to complete. */ 440a141b672SLuiz Otavio O Souza err = mtx_sleep(dev, &sc->sc_mtx, 0, "bsciow", hz); 441be9ddf43SLuiz Otavio O Souza 44271b37cb9SLuiz Otavio O Souza /* Check for errors. */ 4430a39cc71SLuiz Otavio O Souza if (err == 0 && (sc->sc_flags & BCM_I2C_ERROR)) 444be9ddf43SLuiz Otavio O Souza err = EIO; 44571b37cb9SLuiz Otavio O Souza if (err != 0) 446be9ddf43SLuiz Otavio O Souza break; 447be9ddf43SLuiz Otavio O Souza } 448be9ddf43SLuiz Otavio O Souza 449be9ddf43SLuiz Otavio O Souza /* Clean the controller flags. */ 450be9ddf43SLuiz Otavio O Souza sc->sc_flags = 0; 451be9ddf43SLuiz Otavio O Souza 452a141b672SLuiz Otavio O Souza /* Wake up the threads waiting for bus. */ 453a141b672SLuiz Otavio O Souza wakeup(dev); 454a141b672SLuiz Otavio O Souza 455be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 456be9ddf43SLuiz Otavio O Souza 457be9ddf43SLuiz Otavio O Souza return (err); 458be9ddf43SLuiz Otavio O Souza } 459be9ddf43SLuiz Otavio O Souza 460e50c6241SLuiz Otavio O Souza static int 461e50c6241SLuiz Otavio O Souza bcm_bsc_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) 462e50c6241SLuiz Otavio O Souza { 463e50c6241SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 4649e93dfcfSLuiz Otavio O Souza uint32_t busfreq; 465e50c6241SLuiz Otavio O Souza 466e50c6241SLuiz Otavio O Souza sc = device_get_softc(dev); 467e50c6241SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 468e50c6241SLuiz Otavio O Souza bcm_bsc_reset(sc); 4699e93dfcfSLuiz Otavio O Souza if (sc->sc_iicbus == NULL) 4709e93dfcfSLuiz Otavio O Souza busfreq = 100000; 4719e93dfcfSLuiz Otavio O Souza else 4729e93dfcfSLuiz Otavio O Souza busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed); 4739e93dfcfSLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, BCM_BSC_CORE_CLK / busfreq); 474e50c6241SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 475e50c6241SLuiz Otavio O Souza 476e50c6241SLuiz Otavio O Souza return (IIC_ENOADDR); 477e50c6241SLuiz Otavio O Souza } 478e50c6241SLuiz Otavio O Souza 479be9ddf43SLuiz Otavio O Souza static phandle_t 480be9ddf43SLuiz Otavio O Souza bcm_bsc_get_node(device_t bus, device_t dev) 481be9ddf43SLuiz Otavio O Souza { 482be9ddf43SLuiz Otavio O Souza 483be9ddf43SLuiz Otavio O Souza /* We only have one child, the I2C bus, which needs our own node. */ 484be9ddf43SLuiz Otavio O Souza return (ofw_bus_get_node(bus)); 485be9ddf43SLuiz Otavio O Souza } 486be9ddf43SLuiz Otavio O Souza 487be9ddf43SLuiz Otavio O Souza static device_method_t bcm_bsc_methods[] = { 488be9ddf43SLuiz Otavio O Souza /* Device interface */ 489be9ddf43SLuiz Otavio O Souza DEVMETHOD(device_probe, bcm_bsc_probe), 490be9ddf43SLuiz Otavio O Souza DEVMETHOD(device_attach, bcm_bsc_attach), 491be9ddf43SLuiz Otavio O Souza DEVMETHOD(device_detach, bcm_bsc_detach), 492be9ddf43SLuiz Otavio O Souza 493be9ddf43SLuiz Otavio O Souza /* iicbus interface */ 494e50c6241SLuiz Otavio O Souza DEVMETHOD(iicbus_reset, bcm_bsc_iicbus_reset), 495be9ddf43SLuiz Otavio O Souza DEVMETHOD(iicbus_callback, iicbus_null_callback), 496be9ddf43SLuiz Otavio O Souza DEVMETHOD(iicbus_transfer, bcm_bsc_transfer), 497be9ddf43SLuiz Otavio O Souza 498be9ddf43SLuiz Otavio O Souza /* ofw_bus interface */ 499be9ddf43SLuiz Otavio O Souza DEVMETHOD(ofw_bus_get_node, bcm_bsc_get_node), 500be9ddf43SLuiz Otavio O Souza 501be9ddf43SLuiz Otavio O Souza DEVMETHOD_END 502be9ddf43SLuiz Otavio O Souza }; 503be9ddf43SLuiz Otavio O Souza 504be9ddf43SLuiz Otavio O Souza static devclass_t bcm_bsc_devclass; 505be9ddf43SLuiz Otavio O Souza 506be9ddf43SLuiz Otavio O Souza static driver_t bcm_bsc_driver = { 507be9ddf43SLuiz Otavio O Souza "iichb", 508be9ddf43SLuiz Otavio O Souza bcm_bsc_methods, 509be9ddf43SLuiz Otavio O Souza sizeof(struct bcm_bsc_softc), 510be9ddf43SLuiz Otavio O Souza }; 511be9ddf43SLuiz Otavio O Souza 512be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, iicbus_devclass, 0, 0); 513be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, bcm_bsc_devclass, 0, 0); 514