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 55be9ddf43SLuiz Otavio O Souza static void bcm_bsc_intr(void *); 56be9ddf43SLuiz Otavio O Souza 57be9ddf43SLuiz Otavio O Souza static void 58be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(struct bcm_bsc_softc *sc, uint32_t off, uint32_t mask, 59be9ddf43SLuiz Otavio O Souza uint32_t value) 60be9ddf43SLuiz Otavio O Souza { 61be9ddf43SLuiz Otavio O Souza uint32_t reg; 62be9ddf43SLuiz Otavio O Souza 63be9ddf43SLuiz Otavio O Souza mtx_assert(&sc->sc_mtx, MA_OWNED); 64be9ddf43SLuiz Otavio O Souza reg = BCM_BSC_READ(sc, off); 65be9ddf43SLuiz Otavio O Souza reg &= ~mask; 66be9ddf43SLuiz Otavio O Souza reg |= value; 67be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, off, reg); 68be9ddf43SLuiz Otavio O Souza } 69be9ddf43SLuiz Otavio O Souza 70be9ddf43SLuiz Otavio O Souza static int 71be9ddf43SLuiz Otavio O Souza bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS) 72be9ddf43SLuiz Otavio O Souza { 73be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 74be9ddf43SLuiz Otavio O Souza uint32_t clk; 75be9ddf43SLuiz Otavio O Souza int error; 76be9ddf43SLuiz Otavio O Souza 77be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg1; 78be9ddf43SLuiz Otavio O Souza 79be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 80be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK); 81be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 82be9ddf43SLuiz Otavio O Souza clk &= 0xffff; 83be9ddf43SLuiz Otavio O Souza if (clk == 0) 84be9ddf43SLuiz Otavio O Souza clk = 32768; 85be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_CORE_CLK / clk; 86be9ddf43SLuiz Otavio O Souza error = sysctl_handle_int(oidp, &clk, sizeof(clk), req); 87be9ddf43SLuiz Otavio O Souza if (error != 0 || req->newptr == NULL) 88be9ddf43SLuiz Otavio O Souza return (error); 89be9ddf43SLuiz Otavio O Souza 90be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_CORE_CLK / clk; 91be9ddf43SLuiz Otavio O Souza if (clk % 2) 92be9ddf43SLuiz Otavio O Souza clk--; 93be9ddf43SLuiz Otavio O Souza if (clk > 0xffff) 94be9ddf43SLuiz Otavio O Souza clk = 0xffff; 95be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 96be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, clk); 97be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 98be9ddf43SLuiz Otavio O Souza 99be9ddf43SLuiz Otavio O Souza return (0); 100be9ddf43SLuiz Otavio O Souza } 101be9ddf43SLuiz Otavio O Souza 102be9ddf43SLuiz Otavio O Souza static int 103be9ddf43SLuiz Otavio O Souza bcm_bsc_clkt_proc(SYSCTL_HANDLER_ARGS) 104be9ddf43SLuiz Otavio O Souza { 105be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 106be9ddf43SLuiz Otavio O Souza uint32_t clkt; 107be9ddf43SLuiz Otavio O Souza int error; 108be9ddf43SLuiz Otavio O Souza 109be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg1; 110be9ddf43SLuiz Otavio O Souza 111be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 112be9ddf43SLuiz Otavio O Souza clkt = BCM_BSC_READ(sc, BCM_BSC_CLKT); 113be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 114be9ddf43SLuiz Otavio O Souza clkt &= 0xffff; 115be9ddf43SLuiz Otavio O Souza error = sysctl_handle_int(oidp, &clkt, sizeof(clkt), req); 116be9ddf43SLuiz Otavio O Souza if (error != 0 || req->newptr == NULL) 117be9ddf43SLuiz Otavio O Souza return (error); 118be9ddf43SLuiz Otavio O Souza 119be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 120be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CLKT, clkt & 0xffff); 121be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 122be9ddf43SLuiz Otavio O Souza 123be9ddf43SLuiz Otavio O Souza return (0); 124be9ddf43SLuiz Otavio O Souza } 125be9ddf43SLuiz Otavio O Souza 126be9ddf43SLuiz Otavio O Souza static int 127be9ddf43SLuiz Otavio O Souza bcm_bsc_fall_proc(SYSCTL_HANDLER_ARGS) 128be9ddf43SLuiz Otavio O Souza { 129be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 130be9ddf43SLuiz Otavio O Souza uint32_t clk, reg; 131be9ddf43SLuiz Otavio O Souza int error; 132be9ddf43SLuiz Otavio O Souza 133be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg1; 134be9ddf43SLuiz Otavio O Souza 135be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 136be9ddf43SLuiz Otavio O Souza reg = BCM_BSC_READ(sc, BCM_BSC_DELAY); 137be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 138be9ddf43SLuiz Otavio O Souza reg >>= 16; 139be9ddf43SLuiz Otavio O Souza error = sysctl_handle_int(oidp, ®, sizeof(reg), req); 140be9ddf43SLuiz Otavio O Souza if (error != 0 || req->newptr == NULL) 141be9ddf43SLuiz Otavio O Souza return (error); 142be9ddf43SLuiz Otavio O Souza 143be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 144be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK); 145be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_CORE_CLK / clk; 146be9ddf43SLuiz Otavio O Souza if (reg > clk / 2) 147be9ddf43SLuiz Otavio O Souza reg = clk / 2 - 1; 148be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff0000, reg << 16); 149be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 150be9ddf43SLuiz Otavio O Souza 151be9ddf43SLuiz Otavio O Souza return (0); 152be9ddf43SLuiz Otavio O Souza } 153be9ddf43SLuiz Otavio O Souza 154be9ddf43SLuiz Otavio O Souza static int 155be9ddf43SLuiz Otavio O Souza bcm_bsc_rise_proc(SYSCTL_HANDLER_ARGS) 156be9ddf43SLuiz Otavio O Souza { 157be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 158be9ddf43SLuiz Otavio O Souza uint32_t clk, reg; 159be9ddf43SLuiz Otavio O Souza int error; 160be9ddf43SLuiz Otavio O Souza 161be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg1; 162be9ddf43SLuiz Otavio O Souza 163be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 164be9ddf43SLuiz Otavio O Souza reg = BCM_BSC_READ(sc, BCM_BSC_DELAY); 165be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 166be9ddf43SLuiz Otavio O Souza reg &= 0xffff; 167be9ddf43SLuiz Otavio O Souza error = sysctl_handle_int(oidp, ®, sizeof(reg), req); 168be9ddf43SLuiz Otavio O Souza if (error != 0 || req->newptr == NULL) 169be9ddf43SLuiz Otavio O Souza return (error); 170be9ddf43SLuiz Otavio O Souza 171be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 172be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK); 173be9ddf43SLuiz Otavio O Souza clk = BCM_BSC_CORE_CLK / clk; 174be9ddf43SLuiz Otavio O Souza if (reg > clk / 2) 175be9ddf43SLuiz Otavio O Souza reg = clk / 2 - 1; 176be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff, reg); 177be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 178be9ddf43SLuiz Otavio O Souza 179be9ddf43SLuiz Otavio O Souza return (0); 180be9ddf43SLuiz Otavio O Souza } 181be9ddf43SLuiz Otavio O Souza 182be9ddf43SLuiz Otavio O Souza static void 183be9ddf43SLuiz Otavio O Souza bcm_bsc_sysctl_init(struct bcm_bsc_softc *sc) 184be9ddf43SLuiz Otavio O Souza { 185be9ddf43SLuiz Otavio O Souza struct sysctl_ctx_list *ctx; 186be9ddf43SLuiz Otavio O Souza struct sysctl_oid *tree_node; 187be9ddf43SLuiz Otavio O Souza struct sysctl_oid_list *tree; 188be9ddf43SLuiz Otavio O Souza 189be9ddf43SLuiz Otavio O Souza /* 190be9ddf43SLuiz Otavio O Souza * Add system sysctl tree/handlers. 191be9ddf43SLuiz Otavio O Souza */ 192be9ddf43SLuiz Otavio O Souza ctx = device_get_sysctl_ctx(sc->sc_dev); 193be9ddf43SLuiz Otavio O Souza tree_node = device_get_sysctl_tree(sc->sc_dev); 194be9ddf43SLuiz Otavio O Souza tree = SYSCTL_CHILDREN(tree_node); 195be9ddf43SLuiz Otavio O Souza SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock", 196be9ddf43SLuiz Otavio O Souza CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc), 197be9ddf43SLuiz Otavio O Souza bcm_bsc_clock_proc, "IU", "I2C BUS clock frequency"); 198be9ddf43SLuiz Otavio O Souza SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock_stretch", 199be9ddf43SLuiz Otavio O Souza CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc), 200be9ddf43SLuiz Otavio O Souza bcm_bsc_clkt_proc, "IU", "I2C BUS clock stretch timeout"); 201be9ddf43SLuiz Otavio O Souza SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "fall_edge_delay", 202be9ddf43SLuiz Otavio O Souza CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc), 203be9ddf43SLuiz Otavio O Souza bcm_bsc_fall_proc, "IU", "I2C BUS falling edge delay"); 204be9ddf43SLuiz Otavio O Souza SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rise_edge_delay", 205be9ddf43SLuiz Otavio O Souza CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc), 206be9ddf43SLuiz Otavio O Souza bcm_bsc_rise_proc, "IU", "I2C BUS rising edge delay"); 207be9ddf43SLuiz Otavio O Souza } 208be9ddf43SLuiz Otavio O Souza 209be9ddf43SLuiz Otavio O Souza static void 210be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(struct bcm_bsc_softc *sc) 211be9ddf43SLuiz Otavio O Souza { 212be9ddf43SLuiz Otavio O Souza 213e50c6241SLuiz Otavio O Souza /* Enable the BSC Controller, disable interrupts. */ 214e50c6241SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN); 215be9ddf43SLuiz Otavio O Souza /* Clear pending interrupts. */ 216be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_STATUS, BCM_BSC_STATUS_CLKT | 217be9ddf43SLuiz Otavio O Souza BCM_BSC_STATUS_ERR | BCM_BSC_STATUS_DONE); 218be9ddf43SLuiz Otavio O Souza /* Clear the FIFO. */ 219be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_CLEAR0, 220be9ddf43SLuiz Otavio O Souza BCM_BSC_CTRL_CLEAR0); 221be9ddf43SLuiz Otavio O Souza } 222be9ddf43SLuiz Otavio O Souza 223be9ddf43SLuiz Otavio O Souza static int 224be9ddf43SLuiz Otavio O Souza bcm_bsc_probe(device_t dev) 225be9ddf43SLuiz Otavio O Souza { 226be9ddf43SLuiz Otavio O Souza 227add35ed5SIan Lepore if (!ofw_bus_status_okay(dev)) 228add35ed5SIan Lepore return (ENXIO); 229add35ed5SIan Lepore 230be9ddf43SLuiz Otavio O Souza if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-bsc")) 231be9ddf43SLuiz Otavio O Souza return (ENXIO); 232be9ddf43SLuiz Otavio O Souza 233be9ddf43SLuiz Otavio O Souza device_set_desc(dev, "BCM2708/2835 BSC controller"); 234be9ddf43SLuiz Otavio O Souza 235be9ddf43SLuiz Otavio O Souza return (BUS_PROBE_DEFAULT); 236be9ddf43SLuiz Otavio O Souza } 237be9ddf43SLuiz Otavio O Souza 238be9ddf43SLuiz Otavio O Souza static int 239be9ddf43SLuiz Otavio O Souza bcm_bsc_attach(device_t dev) 240be9ddf43SLuiz Otavio O Souza { 241be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 24221cb1342SLuiz Otavio O Souza unsigned long start; 243be9ddf43SLuiz Otavio O Souza device_t gpio; 24421cb1342SLuiz Otavio O Souza int i, rid; 245be9ddf43SLuiz Otavio O Souza 246be9ddf43SLuiz Otavio O Souza sc = device_get_softc(dev); 247be9ddf43SLuiz Otavio O Souza sc->sc_dev = dev; 248be9ddf43SLuiz Otavio O Souza 249be9ddf43SLuiz Otavio O Souza rid = 0; 250be9ddf43SLuiz Otavio O Souza sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 251be9ddf43SLuiz Otavio O Souza RF_ACTIVE); 252be9ddf43SLuiz Otavio O Souza if (!sc->sc_mem_res) { 253be9ddf43SLuiz Otavio O Souza device_printf(dev, "cannot allocate memory window\n"); 254be9ddf43SLuiz Otavio O Souza return (ENXIO); 255be9ddf43SLuiz Otavio O Souza } 256be9ddf43SLuiz Otavio O Souza 257be9ddf43SLuiz Otavio O Souza sc->sc_bst = rman_get_bustag(sc->sc_mem_res); 258be9ddf43SLuiz Otavio O Souza sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res); 259be9ddf43SLuiz Otavio O Souza 26021cb1342SLuiz Otavio O Souza /* Check the unit we are attaching by its base address. */ 26121cb1342SLuiz Otavio O Souza start = rman_get_start(sc->sc_mem_res); 26221cb1342SLuiz Otavio O Souza for (i = 0; i < nitems(bcm_bsc_pins); i++) { 26321cb1342SLuiz Otavio O Souza if (bcm_bsc_pins[i].start == start) 26421cb1342SLuiz Otavio O Souza break; 26521cb1342SLuiz Otavio O Souza } 26621cb1342SLuiz Otavio O Souza if (i == nitems(bcm_bsc_pins)) { 26721cb1342SLuiz Otavio O Souza device_printf(dev, "only bsc0 and bsc1 are supported\n"); 26821cb1342SLuiz Otavio O Souza return (ENXIO); 26921cb1342SLuiz Otavio O Souza } 27021cb1342SLuiz Otavio O Souza 27121cb1342SLuiz Otavio O Souza /* 27221cb1342SLuiz Otavio O Souza * Configure the GPIO pins to ALT0 function to enable BSC control 27321cb1342SLuiz Otavio O Souza * over the pins. 27421cb1342SLuiz Otavio O Souza */ 27521cb1342SLuiz Otavio O Souza gpio = devclass_get_device(devclass_find("gpio"), 0); 27621cb1342SLuiz Otavio O Souza if (!gpio) { 27721cb1342SLuiz Otavio O Souza device_printf(dev, "cannot find gpio0\n"); 27821cb1342SLuiz Otavio O Souza return (ENXIO); 27921cb1342SLuiz Otavio O Souza } 28021cb1342SLuiz Otavio O Souza bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].sda, BCM_GPIO_ALT0); 28121cb1342SLuiz Otavio O Souza bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].scl, BCM_GPIO_ALT0); 28221cb1342SLuiz Otavio O Souza 283be9ddf43SLuiz Otavio O Souza rid = 0; 284be9ddf43SLuiz Otavio O Souza sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 285be9ddf43SLuiz Otavio O Souza RF_ACTIVE | RF_SHAREABLE); 286be9ddf43SLuiz Otavio O Souza if (!sc->sc_irq_res) { 287be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); 288be9ddf43SLuiz Otavio O Souza device_printf(dev, "cannot allocate interrupt\n"); 289be9ddf43SLuiz Otavio O Souza return (ENXIO); 290be9ddf43SLuiz Otavio O Souza } 291be9ddf43SLuiz Otavio O Souza 292be9ddf43SLuiz Otavio O Souza /* Hook up our interrupt handler. */ 293be9ddf43SLuiz Otavio O Souza if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE, 294be9ddf43SLuiz Otavio O Souza NULL, bcm_bsc_intr, sc, &sc->sc_intrhand)) { 295be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res); 296be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); 297be9ddf43SLuiz Otavio O Souza device_printf(dev, "cannot setup the interrupt handler\n"); 298be9ddf43SLuiz Otavio O Souza return (ENXIO); 299be9ddf43SLuiz Otavio O Souza } 300be9ddf43SLuiz Otavio O Souza 301be9ddf43SLuiz Otavio O Souza mtx_init(&sc->sc_mtx, "bcm_bsc", NULL, MTX_DEF); 302be9ddf43SLuiz Otavio O Souza 303be9ddf43SLuiz Otavio O Souza bcm_bsc_sysctl_init(sc); 304be9ddf43SLuiz Otavio O Souza 305be9ddf43SLuiz Otavio O Souza /* Enable the BSC controller. Flush the FIFO. */ 306be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 307be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(sc); 308be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 309be9ddf43SLuiz Otavio O Souza 310be9ddf43SLuiz Otavio O Souza device_add_child(dev, "iicbus", -1); 311be9ddf43SLuiz Otavio O Souza 312be9ddf43SLuiz Otavio O Souza return (bus_generic_attach(dev)); 313be9ddf43SLuiz Otavio O Souza } 314be9ddf43SLuiz Otavio O Souza 315be9ddf43SLuiz Otavio O Souza static int 316be9ddf43SLuiz Otavio O Souza bcm_bsc_detach(device_t dev) 317be9ddf43SLuiz Otavio O Souza { 318be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 319be9ddf43SLuiz Otavio O Souza 320be9ddf43SLuiz Otavio O Souza bus_generic_detach(dev); 321be9ddf43SLuiz Otavio O Souza 322be9ddf43SLuiz Otavio O Souza sc = device_get_softc(dev); 323be9ddf43SLuiz Otavio O Souza mtx_destroy(&sc->sc_mtx); 324be9ddf43SLuiz Otavio O Souza if (sc->sc_intrhand) 325be9ddf43SLuiz Otavio O Souza bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand); 326be9ddf43SLuiz Otavio O Souza if (sc->sc_irq_res) 327be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res); 328be9ddf43SLuiz Otavio O Souza if (sc->sc_mem_res) 329be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); 330be9ddf43SLuiz Otavio O Souza 331be9ddf43SLuiz Otavio O Souza return (0); 332be9ddf43SLuiz Otavio O Souza } 333be9ddf43SLuiz Otavio O Souza 334be9ddf43SLuiz Otavio O Souza static void 335be9ddf43SLuiz Otavio O Souza bcm_bsc_intr(void *arg) 336be9ddf43SLuiz Otavio O Souza { 337be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 338be9ddf43SLuiz Otavio O Souza uint32_t status; 339be9ddf43SLuiz Otavio O Souza 340be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg; 341be9ddf43SLuiz Otavio O Souza 342be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 343be9ddf43SLuiz Otavio O Souza 344be9ddf43SLuiz Otavio O Souza /* The I2C interrupt is shared among all the BSC controllers. */ 345be9ddf43SLuiz Otavio O Souza if ((sc->sc_flags & BCM_I2C_BUSY) == 0) { 346be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 347be9ddf43SLuiz Otavio O Souza return; 348be9ddf43SLuiz Otavio O Souza } 349be9ddf43SLuiz Otavio O Souza 350be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS); 351be9ddf43SLuiz Otavio O Souza 352be9ddf43SLuiz Otavio O Souza /* Check for errors. */ 353be9ddf43SLuiz Otavio O Souza if (status & (BCM_BSC_STATUS_CLKT | BCM_BSC_STATUS_ERR)) { 354be9ddf43SLuiz Otavio O Souza /* Disable interrupts. */ 355be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(sc); 356e50c6241SLuiz Otavio O Souza sc->sc_flags |= BCM_I2C_ERROR; 357be9ddf43SLuiz Otavio O Souza wakeup(sc->sc_dev); 358be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 359be9ddf43SLuiz Otavio O Souza return; 360be9ddf43SLuiz Otavio O Souza } 361be9ddf43SLuiz Otavio O Souza 362be9ddf43SLuiz Otavio O Souza if (sc->sc_flags & BCM_I2C_READ) { 363be9ddf43SLuiz Otavio O Souza while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD)) { 364be9ddf43SLuiz Otavio O Souza *sc->sc_data++ = BCM_BSC_READ(sc, BCM_BSC_DATA); 365be9ddf43SLuiz Otavio O Souza sc->sc_resid--; 366be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS); 367be9ddf43SLuiz Otavio O Souza } 368be9ddf43SLuiz Otavio O Souza } else { 369be9ddf43SLuiz Otavio O Souza while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD)) { 370be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data++); 371be9ddf43SLuiz Otavio O Souza sc->sc_resid--; 372be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS); 373be9ddf43SLuiz Otavio O Souza } 374be9ddf43SLuiz Otavio O Souza } 375be9ddf43SLuiz Otavio O Souza 376be9ddf43SLuiz Otavio O Souza if (status & BCM_BSC_STATUS_DONE) { 377be9ddf43SLuiz Otavio O Souza /* Disable interrupts. */ 378be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(sc); 379be9ddf43SLuiz Otavio O Souza wakeup(sc->sc_dev); 380be9ddf43SLuiz Otavio O Souza } 381be9ddf43SLuiz Otavio O Souza 382be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 383be9ddf43SLuiz Otavio O Souza } 384be9ddf43SLuiz Otavio O Souza 385be9ddf43SLuiz Otavio O Souza static int 386be9ddf43SLuiz Otavio O Souza bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) 387be9ddf43SLuiz Otavio O Souza { 388be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 389be9ddf43SLuiz Otavio O Souza uint32_t intr, read, status; 390be9ddf43SLuiz Otavio O Souza int i, err; 391be9ddf43SLuiz Otavio O Souza 392be9ddf43SLuiz Otavio O Souza sc = device_get_softc(dev); 393be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 394be9ddf43SLuiz Otavio O Souza 395be9ddf43SLuiz Otavio O Souza /* If the controller is busy wait until it is available. */ 396be9ddf43SLuiz Otavio O Souza while (sc->sc_flags & BCM_I2C_BUSY) 397a141b672SLuiz Otavio O Souza mtx_sleep(dev, &sc->sc_mtx, 0, "bscbusw", 0); 398be9ddf43SLuiz Otavio O Souza 399be9ddf43SLuiz Otavio O Souza /* Now we have control over the BSC controller. */ 400be9ddf43SLuiz Otavio O Souza sc->sc_flags = BCM_I2C_BUSY; 401be9ddf43SLuiz Otavio O Souza 402be9ddf43SLuiz Otavio O Souza /* Clear the FIFO and the pending interrupts. */ 403be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(sc); 404be9ddf43SLuiz Otavio O Souza 405be9ddf43SLuiz Otavio O Souza err = 0; 406be9ddf43SLuiz Otavio O Souza for (i = 0; i < nmsgs; i++) { 407be9ddf43SLuiz Otavio O Souza 408be9ddf43SLuiz Otavio O Souza /* Write the slave address. */ 409d6cf3c63SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, msgs[i].slave >> 1); 410be9ddf43SLuiz Otavio O Souza 411be9ddf43SLuiz Otavio O Souza /* Write the data length. */ 412be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_DLEN, msgs[i].len); 413be9ddf43SLuiz Otavio O Souza 414be9ddf43SLuiz Otavio O Souza sc->sc_data = msgs[i].buf; 415be9ddf43SLuiz Otavio O Souza sc->sc_resid = msgs[i].len; 416be9ddf43SLuiz Otavio O Souza if ((msgs[i].flags & IIC_M_RD) == 0) { 417be9ddf43SLuiz Otavio O Souza /* Fill up the TX FIFO. */ 418be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS); 419be9ddf43SLuiz Otavio O Souza while (sc->sc_resid > 0 && 420be9ddf43SLuiz Otavio O Souza (status & BCM_BSC_STATUS_TXD)) { 421be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data); 422be9ddf43SLuiz Otavio O Souza sc->sc_data++; 423be9ddf43SLuiz Otavio O Souza sc->sc_resid--; 424be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS); 425be9ddf43SLuiz Otavio O Souza } 426be9ddf43SLuiz Otavio O Souza read = 0; 427be9ddf43SLuiz Otavio O Souza intr = BCM_BSC_CTRL_INTT; 428be9ddf43SLuiz Otavio O Souza sc->sc_flags &= ~BCM_I2C_READ; 429be9ddf43SLuiz Otavio O Souza } else { 430be9ddf43SLuiz Otavio O Souza sc->sc_flags |= BCM_I2C_READ; 431be9ddf43SLuiz Otavio O Souza read = BCM_BSC_CTRL_READ; 432be9ddf43SLuiz Otavio O Souza intr = BCM_BSC_CTRL_INTR; 433be9ddf43SLuiz Otavio O Souza } 434be9ddf43SLuiz Otavio O Souza intr |= BCM_BSC_CTRL_INTD; 435be9ddf43SLuiz Otavio O Souza 436be9ddf43SLuiz Otavio O Souza /* Start the transfer. */ 437be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN | 438be9ddf43SLuiz Otavio O Souza BCM_BSC_CTRL_ST | read | intr); 439be9ddf43SLuiz Otavio O Souza 440be9ddf43SLuiz Otavio O Souza /* Wait for the transaction to complete. */ 441a141b672SLuiz Otavio O Souza err = mtx_sleep(dev, &sc->sc_mtx, 0, "bsciow", hz); 442be9ddf43SLuiz Otavio O Souza 44371b37cb9SLuiz Otavio O Souza /* Check for errors. */ 444*0a39cc71SLuiz Otavio O Souza if (err == 0 && (sc->sc_flags & BCM_I2C_ERROR)) 445be9ddf43SLuiz Otavio O Souza err = EIO; 44671b37cb9SLuiz Otavio O Souza if (err != 0) 447be9ddf43SLuiz Otavio O Souza break; 448be9ddf43SLuiz Otavio O Souza } 449be9ddf43SLuiz Otavio O Souza 450be9ddf43SLuiz Otavio O Souza /* Clean the controller flags. */ 451be9ddf43SLuiz Otavio O Souza sc->sc_flags = 0; 452be9ddf43SLuiz Otavio O Souza 453a141b672SLuiz Otavio O Souza /* Wake up the threads waiting for bus. */ 454a141b672SLuiz Otavio O Souza wakeup(dev); 455a141b672SLuiz Otavio O Souza 456be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 457be9ddf43SLuiz Otavio O Souza 458be9ddf43SLuiz Otavio O Souza return (err); 459be9ddf43SLuiz Otavio O Souza } 460be9ddf43SLuiz Otavio O Souza 461e50c6241SLuiz Otavio O Souza static int 462e50c6241SLuiz Otavio O Souza bcm_bsc_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) 463e50c6241SLuiz Otavio O Souza { 464e50c6241SLuiz Otavio O Souza struct bcm_bsc_softc *sc; 465e50c6241SLuiz Otavio O Souza uint32_t freq; 466e50c6241SLuiz Otavio O Souza 467e50c6241SLuiz Otavio O Souza sc = device_get_softc(dev); 468e50c6241SLuiz Otavio O Souza BCM_BSC_LOCK(sc); 469e50c6241SLuiz Otavio O Souza bcm_bsc_reset(sc); 470e50c6241SLuiz Otavio O Souza freq = 0; 471e50c6241SLuiz Otavio O Souza switch (speed) { 472e50c6241SLuiz Otavio O Souza case IIC_SLOW: 473e50c6241SLuiz Otavio O Souza freq = BCM_BSC_SLOW; 474e50c6241SLuiz Otavio O Souza break; 475e50c6241SLuiz Otavio O Souza case IIC_FAST: 476e50c6241SLuiz Otavio O Souza freq = BCM_BSC_FAST; 477e50c6241SLuiz Otavio O Souza break; 478e50c6241SLuiz Otavio O Souza case IIC_FASTEST: 479e50c6241SLuiz Otavio O Souza freq = BCM_BSC_FASTEST; 480e50c6241SLuiz Otavio O Souza break; 481e50c6241SLuiz Otavio O Souza case IIC_UNKNOWN: 482e50c6241SLuiz Otavio O Souza default: 483e50c6241SLuiz Otavio O Souza /* Reuse last frequency. */ 484e50c6241SLuiz Otavio O Souza break; 485e50c6241SLuiz Otavio O Souza } 486e50c6241SLuiz Otavio O Souza if (freq != 0) 487e50c6241SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, BCM_BSC_CORE_CLK / freq); 488e50c6241SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc); 489e50c6241SLuiz Otavio O Souza 490e50c6241SLuiz Otavio O Souza return (IIC_ENOADDR); 491e50c6241SLuiz Otavio O Souza } 492e50c6241SLuiz Otavio O Souza 493be9ddf43SLuiz Otavio O Souza static phandle_t 494be9ddf43SLuiz Otavio O Souza bcm_bsc_get_node(device_t bus, device_t dev) 495be9ddf43SLuiz Otavio O Souza { 496be9ddf43SLuiz Otavio O Souza 497be9ddf43SLuiz Otavio O Souza /* We only have one child, the I2C bus, which needs our own node. */ 498be9ddf43SLuiz Otavio O Souza return (ofw_bus_get_node(bus)); 499be9ddf43SLuiz Otavio O Souza } 500be9ddf43SLuiz Otavio O Souza 501be9ddf43SLuiz Otavio O Souza static device_method_t bcm_bsc_methods[] = { 502be9ddf43SLuiz Otavio O Souza /* Device interface */ 503be9ddf43SLuiz Otavio O Souza DEVMETHOD(device_probe, bcm_bsc_probe), 504be9ddf43SLuiz Otavio O Souza DEVMETHOD(device_attach, bcm_bsc_attach), 505be9ddf43SLuiz Otavio O Souza DEVMETHOD(device_detach, bcm_bsc_detach), 506be9ddf43SLuiz Otavio O Souza 507be9ddf43SLuiz Otavio O Souza /* iicbus interface */ 508e50c6241SLuiz Otavio O Souza DEVMETHOD(iicbus_reset, bcm_bsc_iicbus_reset), 509be9ddf43SLuiz Otavio O Souza DEVMETHOD(iicbus_callback, iicbus_null_callback), 510be9ddf43SLuiz Otavio O Souza DEVMETHOD(iicbus_transfer, bcm_bsc_transfer), 511be9ddf43SLuiz Otavio O Souza 512be9ddf43SLuiz Otavio O Souza /* ofw_bus interface */ 513be9ddf43SLuiz Otavio O Souza DEVMETHOD(ofw_bus_get_node, bcm_bsc_get_node), 514be9ddf43SLuiz Otavio O Souza 515be9ddf43SLuiz Otavio O Souza DEVMETHOD_END 516be9ddf43SLuiz Otavio O Souza }; 517be9ddf43SLuiz Otavio O Souza 518be9ddf43SLuiz Otavio O Souza static devclass_t bcm_bsc_devclass; 519be9ddf43SLuiz Otavio O Souza 520be9ddf43SLuiz Otavio O Souza static driver_t bcm_bsc_driver = { 521be9ddf43SLuiz Otavio O Souza "iichb", 522be9ddf43SLuiz Otavio O Souza bcm_bsc_methods, 523be9ddf43SLuiz Otavio O Souza sizeof(struct bcm_bsc_softc), 524be9ddf43SLuiz Otavio O Souza }; 525be9ddf43SLuiz Otavio O Souza 526be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, iicbus_devclass, 0, 0); 527be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, bcm_bsc_devclass, 0, 0); 528