2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 * BERI memory interface.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/malloc.h>
45 #include <sys/timeet.h>
46 #include <sys/timetc.h>
50 #include <dev/fdt/fdt_common.h>
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
55 #include <machine/bus.h>
56 #include <machine/fdt.h>
57 #include <machine/cpu.h>
58 #include <machine/intr.h>
60 struct beri_mem_softc {
61 struct resource *res[1];
62 struct cdev *mem_cdev;
68 static struct resource_spec beri_mem_spec[] = {
69 { SYS_RES_MEMORY, 0, RF_ACTIVE },
74 mem_open(struct cdev *dev, int flags __unused,
75 int fmt __unused, struct thread *td __unused)
77 struct beri_mem_softc *sc;
85 mem_close(struct cdev *dev, int flags __unused,
86 int fmt __unused, struct thread *td __unused)
88 struct beri_mem_softc *sc;
96 mem_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
104 mem_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
105 vm_memattr_t *memattr)
107 struct beri_mem_softc *sc;
111 if (offset < sc->mem_size) {
112 *paddr = sc->mem_start + offset;
119 static struct cdevsw mem_cdevsw = {
120 .d_version = D_VERSION,
122 .d_close = mem_close,
123 .d_ioctl = mem_ioctl,
125 .d_name = "BERI memory",
129 beri_mem_probe(device_t dev)
132 if (!ofw_bus_status_okay(dev))
135 if (!ofw_bus_is_compatible(dev, "sri-cambridge,beri-mem"))
138 device_set_desc(dev, "BERI memory");
139 return (BUS_PROBE_DEFAULT);
143 beri_mem_attach(device_t dev)
145 struct beri_mem_softc *sc;
147 sc = device_get_softc(dev);
150 if (bus_alloc_resources(dev, beri_mem_spec, sc->res)) {
151 device_printf(dev, "could not allocate resources\n");
156 sc->mem_size = rman_get_size(sc->res[0]);
157 sc->mem_start = rman_get_start(sc->res[0]);
159 sc->mem_cdev = make_dev(&mem_cdevsw, 0, UID_ROOT, GID_WHEEL,
162 if (sc->mem_cdev == NULL) {
163 device_printf(dev, "Failed to create character device.\n");
167 sc->mem_cdev->si_drv1 = sc;
172 static device_method_t beri_mem_methods[] = {
173 DEVMETHOD(device_probe, beri_mem_probe),
174 DEVMETHOD(device_attach, beri_mem_attach),
178 static driver_t beri_mem_driver = {
181 sizeof(struct beri_mem_softc),
184 static devclass_t beri_mem_devclass;
186 DRIVER_MODULE(beri_mem, simplebus, beri_mem_driver, beri_mem_devclass, 0, 0);