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