1/*- 2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31/* 32 * Altera PIO (Parallel IO) device driver 33 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: releng/11.0/sys/dev/altera/pio/pio.c 276670 2015-01-04 23:14:04Z br $"); 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/bus.h> 41#include <sys/kernel.h> 42#include <sys/module.h> 43#include <sys/malloc.h> 44#include <sys/rman.h> 45#include <sys/timeet.h> 46#include <sys/timetc.h> 47 48#include <dev/fdt/fdt_common.h> 49#include <dev/ofw/openfirm.h> 50#include <dev/ofw/ofw_bus.h> 51#include <dev/ofw/ofw_bus_subr.h> 52 53#include <machine/bus.h> 54#include <machine/fdt.h> 55#include <machine/cpu.h> 56 57#include <dev/altera/pio/pio.h> 58#include "pio_if.h" 59 60#define READ4(_sc, _reg) bus_read_4((_sc)->res[0], _reg) 61#define READ2(_sc, _reg) bus_read_2((_sc)->res[0], _reg) 62#define READ1(_sc, _reg) bus_read_1((_sc)->res[0], _reg) 63#define WRITE4(_sc, _reg, _val) bus_write_4((_sc)->res[0], _reg, _val) 64#define WRITE2(_sc, _reg, _val) bus_write_2((_sc)->res[0], _reg, _val) 65#define WRITE1(_sc, _reg, _val) bus_write_1((_sc)->res[0], _reg, _val) 66 67struct pio_softc { 68 struct resource *res[2]; 69 bus_space_tag_t bst; 70 bus_space_handle_t bsh; 71 device_t dev; 72 void *ih; 73}; 74 75static struct resource_spec pio_spec[] = { 76 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 77 { SYS_RES_IRQ, 0, RF_ACTIVE }, 78 { -1, 0 } 79}; 80 81static int 82pio_setup_irq(device_t dev, void *intr_handler, void *ih_user) 83{ 84 struct pio_softc *sc; 85 86 sc = device_get_softc(dev); 87 88 /* Setup interrupt handlers */ 89 if (bus_setup_intr(sc->dev, sc->res[1], INTR_TYPE_BIO | INTR_MPSAFE, 90 NULL, intr_handler, ih_user, &sc->ih)) { 91 device_printf(sc->dev, "Unable to setup intr\n"); 92 return (1); 93 } 94 95 return (0); 96} 97 98static int 99pio_teardown_irq(device_t dev) 100{ 101 struct pio_softc *sc; 102 103 sc = device_get_softc(dev); 104 105 bus_teardown_intr(sc->dev, sc->res[1], sc->ih); 106 107 return (0); 108} 109 110static int 111pio_read(device_t dev) 112{ 113 struct pio_softc *sc; 114 115 sc = device_get_softc(dev); 116 117 return (READ4(sc, PIO_DATA)); 118} 119 120static int 121pio_set(device_t dev, int bit, int enable) 122{ 123 struct pio_softc *sc; 124 125 sc = device_get_softc(dev); 126 127 if (enable) 128 WRITE4(sc, PIO_OUTSET, bit); 129 else 130 WRITE4(sc, PIO_OUTCLR, bit); 131 132 return (0); 133} 134 135static int 136pio_configure(device_t dev, int dir, int mask) 137{ 138 struct pio_softc *sc; 139 140 sc = device_get_softc(dev); 141 142 WRITE4(sc, PIO_INT_MASK, mask); 143 WRITE4(sc, PIO_DIR, dir); 144 145 return (0); 146} 147 148static int 149pio_probe(device_t dev) 150{ 151 152 if (!ofw_bus_status_okay(dev)) 153 return (ENXIO); 154 155 if (!ofw_bus_is_compatible(dev, "altr,pio")) 156 return (ENXIO); 157 158 device_set_desc(dev, "Altera PIO"); 159 return (BUS_PROBE_DEFAULT); 160} 161 162static int 163pio_attach(device_t dev) 164{ 165 struct pio_softc *sc; 166 struct fdt_ic *fic; 167 phandle_t node; 168 169 sc = device_get_softc(dev); 170 sc->dev = dev; 171 172 if (bus_alloc_resources(dev, pio_spec, sc->res)) { 173 device_printf(dev, "could not allocate resources\n"); 174 return (ENXIO); 175 } 176 177 /* Memory interface */ 178 sc->bst = rman_get_bustag(sc->res[0]); 179 sc->bsh = rman_get_bushandle(sc->res[0]); 180 181 if ((node = ofw_bus_get_node(sc->dev)) == -1) 182 return (ENXIO); 183 184 fic = malloc(sizeof(*fic), M_DEVBUF, M_WAITOK|M_ZERO); 185 fic->iph = node; 186 fic->dev = dev; 187 SLIST_INSERT_HEAD(&fdt_ic_list_head, fic, fdt_ics); 188 189 return (0); 190} 191 192static device_method_t pio_methods[] = { 193 DEVMETHOD(device_probe, pio_probe), 194 DEVMETHOD(device_attach, pio_attach), 195 196 /* pio_if.m */ 197 DEVMETHOD(pio_read, pio_read), 198 DEVMETHOD(pio_configure, pio_configure), 199 DEVMETHOD(pio_set, pio_set), 200 DEVMETHOD(pio_setup_irq, pio_setup_irq), 201 DEVMETHOD(pio_teardown_irq, pio_teardown_irq), 202 DEVMETHOD_END 203}; 204 205static driver_t pio_driver = { 206 "altera_pio", 207 pio_methods, 208 sizeof(struct pio_softc), 209}; 210 211static devclass_t pio_devclass; 212 213DRIVER_MODULE(altera_pio, simplebus, pio_driver, pio_devclass, 0, 0); 214