2 * Copyright (c) 2014 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/interrupt.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
42 #include <sys/queue.h>
43 #include <sys/reboot.h>
44 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <machine/stdarg.h>
50 #include <dev/pci/pcivar.h>
52 #include <dev/proto/proto.h>
53 #include <dev/proto/proto_dev.h>
54 #include <dev/proto/proto_busdma.h>
56 CTASSERT(SYS_RES_IRQ != PROTO_RES_UNUSED &&
57 SYS_RES_MEMORY != PROTO_RES_UNUSED &&
58 SYS_RES_IOPORT != PROTO_RES_UNUSED);
59 CTASSERT(SYS_RES_IRQ != PROTO_RES_PCICFG &&
60 SYS_RES_MEMORY != PROTO_RES_PCICFG &&
61 SYS_RES_IOPORT != PROTO_RES_PCICFG);
62 CTASSERT(SYS_RES_IRQ != PROTO_RES_BUSDMA &&
63 SYS_RES_MEMORY != PROTO_RES_BUSDMA &&
64 SYS_RES_IOPORT != PROTO_RES_BUSDMA);
66 devclass_t proto_devclass;
67 char proto_driver_name[] = "proto";
69 static d_open_t proto_open;
70 static d_close_t proto_close;
71 static d_read_t proto_read;
72 static d_write_t proto_write;
73 static d_ioctl_t proto_ioctl;
74 static d_mmap_t proto_mmap;
76 struct cdevsw proto_devsw = {
77 .d_version = D_VERSION,
79 .d_name = proto_driver_name,
81 .d_close = proto_close,
83 .d_write = proto_write,
84 .d_ioctl = proto_ioctl,
88 static MALLOC_DEFINE(M_PROTO, "PROTO", "PROTO driver");
91 proto_add_resource(struct proto_softc *sc, int type, int rid,
96 if (type == PROTO_RES_UNUSED)
98 if (sc->sc_rescnt == PROTO_RES_MAX)
101 r = sc->sc_res + sc->sc_rescnt++;
110 proto_intr(void *arg)
112 struct proto_softc *sc = arg;
115 return (FILTER_HANDLED);
120 proto_attach(device_t dev)
122 struct proto_softc *sc;
126 sc = device_get_softc(dev);
129 for (res = 0; res < sc->sc_rescnt; res++) {
130 r = sc->sc_res + res;
137 r->r_size = rman_get_size(r->r_d.res);
138 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0666,
139 "proto/%s/%02x.%s", device_get_desc(dev), r->r_rid,
140 (r->r_type == SYS_RES_IOPORT) ? "io" : "mem");
141 r->r_u.cdev->si_drv1 = sc;
142 r->r_u.cdev->si_drv2 = r;
144 case PROTO_RES_PCICFG:
146 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0666,
147 "proto/%s/pcicfg", device_get_desc(dev));
148 r->r_u.cdev->si_drv1 = sc;
149 r->r_u.cdev->si_drv2 = r;
151 case PROTO_RES_BUSDMA:
152 r->r_d.busdma = proto_busdma_attach(sc);
153 r->r_size = 0; /* no read(2) nor write(2) */
154 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0666,
155 "proto/%s/busdma", device_get_desc(dev));
156 r->r_u.cdev->si_drv1 = sc;
157 r->r_u.cdev->si_drv2 = r;
165 proto_detach(device_t dev)
167 struct proto_softc *sc;
171 sc = device_get_softc(dev);
173 /* Don't detach if we have open device files. */
174 for (res = 0; res < sc->sc_rescnt; res++) {
175 r = sc->sc_res + res;
180 for (res = 0; res < sc->sc_rescnt; res++) {
181 r = sc->sc_res + res;
185 bus_release_resource(dev, r->r_type, r->r_rid,
190 bus_release_resource(dev, r->r_type, r->r_rid,
192 destroy_dev(r->r_u.cdev);
194 case PROTO_RES_PCICFG:
195 destroy_dev(r->r_u.cdev);
197 case PROTO_RES_BUSDMA:
198 proto_busdma_detach(sc, r->r_d.busdma);
199 destroy_dev(r->r_u.cdev);
202 r->r_type = PROTO_RES_UNUSED;
213 proto_open(struct cdev *cdev, int oflags, int devtype, struct thread *td)
218 if (!atomic_cmpset_acq_ptr(&r->r_opened, 0UL, (uintptr_t)td->td_proc))
224 proto_close(struct cdev *cdev, int fflag, int devtype, struct thread *td)
227 struct proto_softc *sc;
231 if (!atomic_cmpset_acq_ptr(&r->r_opened, (uintptr_t)td->td_proc, 0UL))
233 if (r->r_type == PROTO_RES_BUSDMA)
234 proto_busdma_cleanup(sc, r->r_d.busdma);
239 proto_read(struct cdev *cdev, struct uio *uio, int ioflag)
247 struct proto_softc *sc;
258 width = uio->uio_resid;
259 if (width < 1 || width > 8 || bitcount16(width) > 1)
261 ofs = uio->uio_offset;
262 if (ofs + width > r->r_size)
267 buf.x1[0] = (r->r_type == PROTO_RES_PCICFG) ?
268 pci_read_config(dev, ofs, 1) : bus_read_1(r->r_d.res, ofs);
271 buf.x2[0] = (r->r_type == PROTO_RES_PCICFG) ?
272 pci_read_config(dev, ofs, 2) : bus_read_2(r->r_d.res, ofs);
275 buf.x4[0] = (r->r_type == PROTO_RES_PCICFG) ?
276 pci_read_config(dev, ofs, 4) : bus_read_4(r->r_d.res, ofs);
280 if (r->r_type == PROTO_RES_PCICFG)
282 buf.x8[0] = bus_read_8(r->r_d.res, ofs);
289 error = uiomove(&buf, width, uio);
294 proto_write(struct cdev *cdev, struct uio *uio, int ioflag)
302 struct proto_softc *sc;
313 width = uio->uio_resid;
314 if (width < 1 || width > 8 || bitcount16(width) > 1)
316 ofs = uio->uio_offset;
317 if (ofs + width > r->r_size)
320 error = uiomove(&buf, width, uio);
326 if (r->r_type == PROTO_RES_PCICFG)
327 pci_write_config(dev, ofs, buf.x1[0], 1);
329 bus_write_1(r->r_d.res, ofs, buf.x1[0]);
332 if (r->r_type == PROTO_RES_PCICFG)
333 pci_write_config(dev, ofs, buf.x2[0], 2);
335 bus_write_2(r->r_d.res, ofs, buf.x2[0]);
338 if (r->r_type == PROTO_RES_PCICFG)
339 pci_write_config(dev, ofs, buf.x4[0], 4);
341 bus_write_4(r->r_d.res, ofs, buf.x4[0]);
345 if (r->r_type == PROTO_RES_PCICFG)
347 bus_write_8(r->r_d.res, ofs, buf.x8[0]);
358 proto_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
361 struct proto_ioc_region *region;
362 struct proto_ioc_busdma *busdma;
364 struct proto_softc *sc;
372 case PROTO_IOC_REGION:
373 if (r->r_type == PROTO_RES_BUSDMA) {
377 region = (struct proto_ioc_region *)data;
378 region->size = r->r_size;
379 if (r->r_type == PROTO_RES_PCICFG)
382 region->address = rman_get_start(r->r_d.res);
384 case PROTO_IOC_BUSDMA:
385 if (r->r_type != PROTO_RES_BUSDMA) {
389 busdma = (struct proto_ioc_busdma *)data;
390 error = proto_busdma_ioctl(sc, r->r_d.busdma, busdma);
400 proto_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
401 int prot, vm_memattr_t *memattr)
405 if (offset & PAGE_MASK)
407 if (prot & PROT_EXEC)
414 if (offset >= r->r_size)
416 *paddr = rman_get_start(r->r_d.res) + offset;
418 *memattr = VM_MEMATTR_UNCACHEABLE;
421 case PROTO_RES_BUSDMA:
422 if (!proto_busdma_mmap_allowed(r->r_d.busdma, offset))