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>
53 #include <dev/altera/avgen/altera_avgen.h>
56 * Generic device driver for allowing read(), write(), and mmap() on
57 * memory-mapped, Avalon-attached devices. There is no actual dependence on
58 * Avalon, so conceivably this should just be soc_dev or similar, since many
59 * system-on-chip bus environments would work fine with the same code.
62 devclass_t altera_avgen_devclass;
64 static d_mmap_t altera_avgen_mmap;
65 static d_read_t altera_avgen_read;
66 static d_write_t altera_avgen_write;
68 static struct cdevsw avg_cdevsw = {
69 .d_version = D_VERSION,
70 .d_mmap = altera_avgen_mmap,
71 .d_read = altera_avgen_read,
72 .d_write = altera_avgen_write,
73 .d_name = "altera_avgen",
77 altera_avgen_read(struct cdev *dev, struct uio *uio, int flag)
79 struct altera_avgen_softc *sc;
91 if ((sc->avg_flags & ALTERA_AVALON_FLAG_READ) == 0)
93 width = sc->avg_width;
94 if (uio->uio_offset < 0 || uio->uio_offset % width != 0 ||
95 uio->uio_resid % width != 0)
97 size = rman_get_size(sc->avg_res);
98 if ((uio->uio_offset + uio->uio_resid < 0) ||
99 (uio->uio_offset + uio->uio_resid > size))
101 while (uio->uio_resid > 0) {
102 offset = uio->uio_offset;
103 if (offset + width > size)
107 v1 = bus_read_1(sc->avg_res, offset);
108 error = uiomove(&v1, sizeof(v1), uio);
112 v2 = bus_read_2(sc->avg_res, offset);
113 error = uiomove(&v2, sizeof(v2), uio);
117 v4 = bus_read_4(sc->avg_res, offset);
118 error = uiomove(&v4, sizeof(v4), uio);
123 v8 = bus_read_8(sc->avg_res, offset);
124 error = uiomove(&v8, sizeof(v8), uio);
130 panic("%s: unexpected widthment %u", __func__, width);
139 altera_avgen_write(struct cdev *dev, struct uio *uio, int flag)
141 struct altera_avgen_softc *sc;
153 if ((sc->avg_flags & ALTERA_AVALON_FLAG_WRITE) == 0)
155 width = sc->avg_width;
156 if (uio->uio_offset < 0 || uio->uio_offset % width != 0 ||
157 uio->uio_resid % width != 0)
159 size = rman_get_size(sc->avg_res);
160 while (uio->uio_resid > 0) {
161 offset = uio->uio_offset;
162 if (offset + width > size)
166 error = uiomove(&v1, sizeof(v1), uio);
169 bus_write_1(sc->avg_res, offset, v1);
173 error = uiomove(&v2, sizeof(v2), uio);
176 bus_write_2(sc->avg_res, offset, v2);
180 error = uiomove(&v4, sizeof(v4), uio);
183 bus_write_4(sc->avg_res, offset, v4);
188 error = uiomove(&v8, sizeof(v8), uio);
191 bus_write_8(sc->avg_res, offset, v8);
196 panic("%s: unexpected width %u", __func__, width);
203 altera_avgen_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
204 int nprot, vm_memattr_t *memattr)
206 struct altera_avgen_softc *sc;
209 if (nprot & VM_PROT_READ) {
210 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_READ) == 0)
213 if (nprot & VM_PROT_WRITE) {
214 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_WRITE) == 0)
217 if (nprot & VM_PROT_EXECUTE) {
218 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_EXEC) == 0)
221 if (trunc_page(offset) == offset &&
222 rman_get_size(sc->avg_res) >= offset + PAGE_SIZE) {
223 *paddr = rman_get_start(sc->avg_res) + offset;
224 *memattr = VM_MEMATTR_UNCACHEABLE;
232 altera_avgen_process_options(struct altera_avgen_softc *sc,
233 const char *str_fileio, const char *str_mmapio, const char *str_devname,
237 device_t dev = sc->avg_dev;
240 * Check for valid combinations of options.
242 if (str_fileio == NULL && str_mmapio == NULL) {
244 "at least one of %s or %s must be specified\n",
245 ALTERA_AVALON_STR_FILEIO, ALTERA_AVALON_STR_MMAPIO);
248 if (str_devname == NULL && devunit != -1) {
249 device_printf(dev, "%s requires %s be specified\n",
250 ALTERA_AVALON_STR_DEVUNIT, ALTERA_AVALON_STR_DEVNAME);
255 * Extract, digest, and save values.
257 switch (sc->avg_width) {
267 device_printf(dev, "%s unsupported value %u\n",
268 ALTERA_AVALON_STR_WIDTH, sc->avg_width);
272 if (str_fileio != NULL) {
273 for (cp = str_fileio; *cp != '\0'; cp++) {
275 case ALTERA_AVALON_CHAR_READ:
276 sc->avg_flags |= ALTERA_AVALON_FLAG_READ;
279 case ALTERA_AVALON_CHAR_WRITE:
280 sc->avg_flags |= ALTERA_AVALON_FLAG_WRITE;
285 "invalid %s character %c\n",
286 ALTERA_AVALON_STR_FILEIO, *cp);
291 if (str_mmapio != NULL) {
292 for (cp = str_mmapio; *cp != '\0'; cp++) {
294 case ALTERA_AVALON_CHAR_READ:
295 sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_READ;
298 case ALTERA_AVALON_CHAR_WRITE:
300 ALTERA_AVALON_FLAG_MMAP_WRITE;
303 case ALTERA_AVALON_CHAR_EXEC:
304 sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_EXEC;
309 "invalid %s character %c\n",
310 ALTERA_AVALON_STR_MMAPIO, *cp);
319 altera_avgen_attach(struct altera_avgen_softc *sc, const char *str_fileio,
320 const char *str_mmapio, const char *str_devname, int devunit)
322 device_t dev = sc->avg_dev;
325 error = altera_avgen_process_options(sc, str_fileio, str_mmapio,
326 str_devname, devunit);
330 if (rman_get_size(sc->avg_res) >= PAGE_SIZE || str_mmapio != NULL) {
331 if (rman_get_size(sc->avg_res) % PAGE_SIZE != 0) {
333 "memory region not even multiple of page size\n");
336 if (rman_get_start(sc->avg_res) % PAGE_SIZE != 0) {
337 device_printf(dev, "memory region not page-aligned\n");
342 /* Device node allocation. */
343 if (str_devname == NULL) {
344 str_devname = "altera_avgen%d";
345 devunit = sc->avg_unit;
348 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT,
349 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname, devunit);
351 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT,
352 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname);
353 if (sc->avg_cdev == NULL) {
354 device_printf(sc->avg_dev, "%s: make_dev failed\n", __func__);
357 /* XXXRW: Slight race between make_dev(9) and here. */
358 sc->avg_cdev->si_drv1 = sc;
363 altera_avgen_detach(struct altera_avgen_softc *sc)
366 destroy_dev(sc->avg_cdev);