rt305x_ic.c revision 331722
1/*- 2 * Copyright (c) 2010 Aleksandr Rybalko. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: stable/11/sys/mips/rt305x/rt305x_ic.c 331722 2018-03-29 02:50:57Z eadler $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/bus.h> 33#include <sys/interrupt.h> 34#include <sys/kernel.h> 35#include <sys/module.h> 36#include <sys/rman.h> 37#include <sys/malloc.h> 38 39#include <machine/bus.h> 40 41#include <mips/rt305x/rt305xreg.h> 42#include <mips/rt305x/rt305x_icvar.h> 43 44 45static int rt305x_ic_probe(device_t); 46static int rt305x_ic_attach(device_t); 47static int rt305x_ic_detach(device_t); 48 49 50static struct rt305x_ic_softc *rt305x_ic_softc = NULL; 51 52static int 53rt305x_ic_probe(device_t dev) 54{ 55 device_set_desc(dev, "RT305X Interrupt Controller driver"); 56 return (0); 57} 58 59static int 60rt305x_ic_attach(device_t dev) 61{ 62 struct rt305x_ic_softc *sc = device_get_softc(dev); 63 int error = 0; 64 65 KASSERT((device_get_unit(dev) == 0), 66 ("rt305x_ic: Only one Interrupt Controller module supported")); 67 68 if (rt305x_ic_softc != NULL) 69 return (ENXIO); 70 rt305x_ic_softc = sc; 71 72 73 /* Map control/status registers. */ 74 sc->mem_rid = 0; 75 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 76 &sc->mem_rid, RF_ACTIVE); 77 78 if (sc->mem_res == NULL) { 79 device_printf(dev, "couldn't map memory\n"); 80 error = ENXIO; 81 rt305x_ic_detach(dev); 82 return(error); 83 } 84 return (bus_generic_attach(dev)); 85} 86 87static int 88rt305x_ic_detach(device_t dev) 89{ 90 struct rt305x_ic_softc *sc = device_get_softc(dev); 91 92 bus_generic_detach(dev); 93 94 if (sc->mem_res) 95 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, 96 sc->mem_res); 97 return(0); 98} 99 100 101uint32_t 102rt305x_ic_get(uint32_t reg) 103{ 104 struct rt305x_ic_softc *sc = rt305x_ic_softc; 105 106 if (!sc) 107 return (0); 108 109 return (bus_read_4(sc->mem_res, reg)); 110} 111 112void 113rt305x_ic_set(uint32_t reg, uint32_t val) 114{ 115 struct rt305x_ic_softc *sc = rt305x_ic_softc; 116 117 if (!sc) 118 return; 119 120 bus_write_4(sc->mem_res, reg, val); 121 122 return; 123} 124 125 126static device_method_t rt305x_ic_methods[] = { 127 DEVMETHOD(device_probe, rt305x_ic_probe), 128 DEVMETHOD(device_attach, rt305x_ic_attach), 129 DEVMETHOD(device_detach, rt305x_ic_detach), 130 131 {0, 0}, 132}; 133 134static driver_t rt305x_ic_driver = { 135 "rt305x_ic", 136 rt305x_ic_methods, 137 sizeof(struct rt305x_ic_softc), 138}; 139static devclass_t rt305x_ic_devclass; 140 141DRIVER_MODULE(rt305x_ic, obio, rt305x_ic_driver, rt305x_ic_devclass, 0, 0); 142