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 * BERI memory interface. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/bus.h> 38 #include <sys/kernel.h> 39 #include <sys/module.h> 40 #include <sys/malloc.h> 41 #include <sys/rman.h> 42 #include <sys/timeet.h> 43 #include <sys/timetc.h> 44 #include <sys/conf.h> 45 #include <sys/uio.h> 46 47 #include <dev/fdt/fdt_common.h> 48 #include <dev/ofw/openfirm.h> 49 #include <dev/ofw/ofw_bus.h> 50 #include <dev/ofw/ofw_bus_subr.h> 51 52 #include <machine/bus.h> 53 #include <machine/fdt.h> 54 #include <machine/cpu.h> 55 #include <machine/intr.h> 56 57 struct beri_mem_softc { 58 struct resource *res[1]; 59 struct cdev *mem_cdev; 60 device_t dev; 61 int mem_size; 62 int mem_start; 63 }; 64 65 static struct resource_spec beri_mem_spec[] = { 66 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 67 { -1, 0 } 68 }; 69 70 static int 71 mem_open(struct cdev *dev, int flags __unused, 72 int fmt __unused, struct thread *td __unused) 73 { 74 struct beri_mem_softc *sc; 75 76 sc = dev->si_drv1; 77 78 return (0); 79 } 80 81 static int 82 mem_close(struct cdev *dev, int flags __unused, 83 int fmt __unused, struct thread *td __unused) 84 { 85 struct beri_mem_softc *sc; 86 87 sc = dev->si_drv1; 88 89 return (0); 90 } 91 92 static int 93 mem_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, 94 struct thread *td) 95 { 96 97 return (0); 98 } 99 100 static int 101 mem_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, 102 vm_memattr_t *memattr) 103 { 104 struct beri_mem_softc *sc; 105 106 sc = dev->si_drv1; 107 108 if (offset < sc->mem_size) { 109 *paddr = sc->mem_start + offset; 110 return (0); 111 } 112 113 return (EINVAL); 114 } 115 116 static struct cdevsw mem_cdevsw = { 117 .d_version = D_VERSION, 118 .d_open = mem_open, 119 .d_close = mem_close, 120 .d_ioctl = mem_ioctl, 121 .d_mmap = mem_mmap, 122 .d_name = "BERI memory", 123 }; 124 125 static int 126 beri_mem_probe(device_t dev) 127 { 128 129 if (!ofw_bus_status_okay(dev)) 130 return (ENXIO); 131 132 if (!ofw_bus_is_compatible(dev, "sri-cambridge,beri-mem")) 133 return (ENXIO); 134 135 device_set_desc(dev, "BERI memory"); 136 return (BUS_PROBE_DEFAULT); 137 } 138 139 static int 140 beri_mem_attach(device_t dev) 141 { 142 struct beri_mem_softc *sc; 143 144 sc = device_get_softc(dev); 145 sc->dev = dev; 146 147 if (bus_alloc_resources(dev, beri_mem_spec, sc->res)) { 148 device_printf(dev, "could not allocate resources\n"); 149 return (ENXIO); 150 } 151 152 /* Memory info */ 153 sc->mem_size = rman_get_size(sc->res[0]); 154 sc->mem_start = rman_get_start(sc->res[0]); 155 156 sc->mem_cdev = make_dev(&mem_cdevsw, 0, UID_ROOT, GID_WHEEL, 157 0600, "beri_mem"); 158 159 if (sc->mem_cdev == NULL) { 160 device_printf(dev, "Failed to create character device.\n"); 161 return (ENXIO); 162 } 163 164 sc->mem_cdev->si_drv1 = sc; 165 166 return (0); 167 } 168 169 static device_method_t beri_mem_methods[] = { 170 DEVMETHOD(device_probe, beri_mem_probe), 171 DEVMETHOD(device_attach, beri_mem_attach), 172 { 0, 0 } 173 }; 174 175 static driver_t beri_mem_driver = { 176 "beri_mem", 177 beri_mem_methods, 178 sizeof(struct beri_mem_softc), 179 }; 180 181 DRIVER_MODULE(beri_mem, simplebus, beri_mem_driver, 0, 0); 182