2 * Copyright (c) 2012-2013 Robert N. M. Watson
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/condvar.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
45 #include <sys/systm.h>
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <machine/vm.h>
54 #include <dev/altera/avgen/altera_avgen.h>
57 * Generic device driver for allowing read(), write(), and mmap() on
58 * memory-mapped, Avalon-attached devices. There is no actual dependence on
59 * Avalon, so conceivably this should just be soc_dev or similar, since many
60 * system-on-chip bus environments would work fine with the same code.
63 devclass_t altera_avgen_devclass;
65 static d_mmap_t altera_avgen_mmap;
66 static d_read_t altera_avgen_read;
67 static d_write_t altera_avgen_write;
69 static struct cdevsw avg_cdevsw = {
70 .d_version = D_VERSION,
71 .d_mmap = altera_avgen_mmap,
72 .d_read = altera_avgen_read,
73 .d_write = altera_avgen_write,
74 .d_name = "altera_avgen",
78 altera_avgen_read(struct cdev *dev, struct uio *uio, int flag)
80 struct altera_avgen_softc *sc;
92 if ((sc->avg_flags & ALTERA_AVALON_FLAG_READ) == 0)
94 width = sc->avg_width;
95 if (uio->uio_offset < 0 || uio->uio_offset % width != 0 ||
96 uio->uio_resid % width != 0)
98 size = rman_get_size(sc->avg_res);
99 if ((uio->uio_offset + uio->uio_resid < 0) ||
100 (uio->uio_offset + uio->uio_resid > size))
102 while (uio->uio_resid > 0) {
103 offset = uio->uio_offset;
104 if (offset + width > size)
108 v1 = bus_read_1(sc->avg_res, offset);
109 error = uiomove(&v1, sizeof(v1), uio);
113 v2 = bus_read_2(sc->avg_res, offset);
114 error = uiomove(&v2, sizeof(v2), uio);
118 v4 = bus_read_4(sc->avg_res, offset);
119 error = uiomove(&v4, sizeof(v4), uio);
124 v8 = bus_read_8(sc->avg_res, offset);
125 error = uiomove(&v8, sizeof(v8), uio);
131 panic("%s: unexpected widthment %u", __func__, width);
140 altera_avgen_write(struct cdev *dev, struct uio *uio, int flag)
142 struct altera_avgen_softc *sc;
154 if ((sc->avg_flags & ALTERA_AVALON_FLAG_WRITE) == 0)
156 width = sc->avg_width;
157 if (uio->uio_offset < 0 || uio->uio_offset % width != 0 ||
158 uio->uio_resid % width != 0)
160 size = rman_get_size(sc->avg_res);
161 while (uio->uio_resid > 0) {
162 offset = uio->uio_offset;
163 if (offset + width > size)
167 error = uiomove(&v1, sizeof(v1), uio);
170 bus_write_1(sc->avg_res, offset, v1);
174 error = uiomove(&v2, sizeof(v2), uio);
177 bus_write_2(sc->avg_res, offset, v2);
181 error = uiomove(&v4, sizeof(v4), uio);
184 bus_write_4(sc->avg_res, offset, v4);
189 error = uiomove(&v8, sizeof(v8), uio);
192 bus_write_8(sc->avg_res, offset, v8);
197 panic("%s: unexpected width %u", __func__, width);
204 altera_avgen_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
205 int nprot, vm_memattr_t *memattr)
207 struct altera_avgen_softc *sc;
210 if (nprot & VM_PROT_READ) {
211 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_READ) == 0)
214 if (nprot & VM_PROT_WRITE) {
215 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_WRITE) == 0)
218 if (nprot & VM_PROT_EXECUTE) {
219 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_EXEC) == 0)
222 if (trunc_page(offset) == offset &&
223 rman_get_size(sc->avg_res) >= offset + PAGE_SIZE) {
224 *paddr = rman_get_start(sc->avg_res) + offset;
225 *memattr = VM_MEMATTR_UNCACHEABLE;
233 altera_avgen_process_options(struct altera_avgen_softc *sc,
234 const char *str_fileio, const char *str_mmapio, const char *str_devname,
238 device_t dev = sc->avg_dev;
241 * Check for valid combinations of options.
243 if (str_fileio == NULL && str_mmapio == NULL) {
245 "at least one of %s or %s must be specified\n",
246 ALTERA_AVALON_STR_FILEIO, ALTERA_AVALON_STR_MMAPIO);
249 if (str_devname == NULL && devunit != -1) {
250 device_printf(dev, "%s requires %s be specified\n",
251 ALTERA_AVALON_STR_DEVUNIT, ALTERA_AVALON_STR_DEVNAME);
256 * Extract, digest, and save values.
258 switch (sc->avg_width) {
268 device_printf(dev, "%s unsupported value %u\n",
269 ALTERA_AVALON_STR_WIDTH, sc->avg_width);
273 if (str_fileio != NULL) {
274 for (cp = str_fileio; *cp != '\0'; cp++) {
276 case ALTERA_AVALON_CHAR_READ:
277 sc->avg_flags |= ALTERA_AVALON_FLAG_READ;
280 case ALTERA_AVALON_CHAR_WRITE:
281 sc->avg_flags |= ALTERA_AVALON_FLAG_WRITE;
286 "invalid %s character %c\n",
287 ALTERA_AVALON_STR_FILEIO, *cp);
292 if (str_mmapio != NULL) {
293 for (cp = str_mmapio; *cp != '\0'; cp++) {
295 case ALTERA_AVALON_CHAR_READ:
296 sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_READ;
299 case ALTERA_AVALON_CHAR_WRITE:
301 ALTERA_AVALON_FLAG_MMAP_WRITE;
304 case ALTERA_AVALON_CHAR_EXEC:
305 sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_EXEC;
310 "invalid %s character %c\n",
311 ALTERA_AVALON_STR_MMAPIO, *cp);
320 altera_avgen_attach(struct altera_avgen_softc *sc, const char *str_fileio,
321 const char *str_mmapio, const char *str_devname, int devunit)
323 device_t dev = sc->avg_dev;
326 error = altera_avgen_process_options(sc, str_fileio, str_mmapio,
327 str_devname, devunit);
331 if (rman_get_size(sc->avg_res) >= PAGE_SIZE || str_mmapio != NULL) {
332 if (rman_get_size(sc->avg_res) % PAGE_SIZE != 0) {
334 "memory region not even multiple of page size\n");
337 if (rman_get_start(sc->avg_res) % PAGE_SIZE != 0) {
338 device_printf(dev, "memory region not page-aligned\n");
343 /* Device node allocation. */
344 if (str_devname == NULL) {
345 str_devname = "altera_avgen%d";
346 devunit = sc->avg_unit;
349 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT,
350 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname, devunit);
352 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT,
353 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname);
354 if (sc->avg_cdev == NULL) {
355 device_printf(sc->avg_dev, "%s: make_dev failed\n", __func__);
358 /* XXXRW: Slight race between make_dev(9) and here. */
359 sc->avg_cdev->si_drv1 = sc;
364 altera_avgen_detach(struct altera_avgen_softc *sc)
367 destroy_dev(sc->avg_cdev);