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/param.h>
36 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
42 #include <sys/timeet.h>
43 #include <sys/timetc.h>
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>
52 #include <machine/bus.h>
53 #include <machine/fdt.h>
54 #include <machine/cpu.h>
55 #include <machine/intr.h>
57 struct beri_mem_softc {
58 struct resource *res[1];
59 struct cdev *mem_cdev;
65 static struct resource_spec beri_mem_spec[] = {
66 { SYS_RES_MEMORY, 0, RF_ACTIVE },
71 mem_open(struct cdev *dev, int flags __unused,
72 int fmt __unused, struct thread *td __unused)
74 struct beri_mem_softc *sc;
82 mem_close(struct cdev *dev, int flags __unused,
83 int fmt __unused, struct thread *td __unused)
85 struct beri_mem_softc *sc;
93 mem_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
101 mem_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
102 vm_memattr_t *memattr)
104 struct beri_mem_softc *sc;
108 if (offset < sc->mem_size) {
109 *paddr = sc->mem_start + offset;
116 static struct cdevsw mem_cdevsw = {
117 .d_version = D_VERSION,
119 .d_close = mem_close,
120 .d_ioctl = mem_ioctl,
122 .d_name = "BERI memory",
126 beri_mem_probe(device_t dev)
129 if (!ofw_bus_status_okay(dev))
132 if (!ofw_bus_is_compatible(dev, "sri-cambridge,beri-mem"))
135 device_set_desc(dev, "BERI memory");
136 return (BUS_PROBE_DEFAULT);
140 beri_mem_attach(device_t dev)
142 struct beri_mem_softc *sc;
144 sc = device_get_softc(dev);
147 if (bus_alloc_resources(dev, beri_mem_spec, sc->res)) {
148 device_printf(dev, "could not allocate resources\n");
153 sc->mem_size = rman_get_size(sc->res[0]);
154 sc->mem_start = rman_get_start(sc->res[0]);
156 sc->mem_cdev = make_dev(&mem_cdevsw, 0, UID_ROOT, GID_WHEEL,
159 if (sc->mem_cdev == NULL) {
160 device_printf(dev, "Failed to create character device.\n");
164 sc->mem_cdev->si_drv1 = sc;
169 static device_method_t beri_mem_methods[] = {
170 DEVMETHOD(device_probe, beri_mem_probe),
171 DEVMETHOD(device_attach, beri_mem_attach),
175 static driver_t beri_mem_driver = {
178 sizeof(struct beri_mem_softc),
181 DRIVER_MODULE(beri_mem, simplebus, beri_mem_driver, 0, 0);