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