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_DRQ != PROTO_RES_UNUSED &&
58 SYS_RES_MEMORY != PROTO_RES_UNUSED &&
59 SYS_RES_IOPORT != PROTO_RES_UNUSED);
60 CTASSERT(SYS_RES_IRQ != PROTO_RES_PCICFG &&
61 SYS_RES_DRQ != PROTO_RES_PCICFG &&
62 SYS_RES_MEMORY != PROTO_RES_PCICFG &&
63 SYS_RES_IOPORT != PROTO_RES_PCICFG);
64 CTASSERT(SYS_RES_IRQ != PROTO_RES_BUSDMA &&
65 SYS_RES_DRQ != PROTO_RES_BUSDMA &&
66 SYS_RES_MEMORY != PROTO_RES_BUSDMA &&
67 SYS_RES_IOPORT != PROTO_RES_BUSDMA);
69 devclass_t proto_devclass;
70 char proto_driver_name[] = "proto";
72 static d_open_t proto_open;
73 static d_close_t proto_close;
74 static d_read_t proto_read;
75 static d_write_t proto_write;
76 static d_ioctl_t proto_ioctl;
77 static d_mmap_t proto_mmap;
79 struct cdevsw proto_devsw = {
80 .d_version = D_VERSION,
82 .d_name = proto_driver_name,
84 .d_close = proto_close,
86 .d_write = proto_write,
87 .d_ioctl = proto_ioctl,
91 static MALLOC_DEFINE(M_PROTO, "PROTO", "PROTO driver");
94 proto_add_resource(struct proto_softc *sc, int type, int rid,
99 if (type == PROTO_RES_UNUSED)
101 if (sc->sc_rescnt == PROTO_RES_MAX)
104 r = sc->sc_res + sc->sc_rescnt++;
113 proto_intr(void *arg)
115 struct proto_softc *sc = arg;
118 return (FILTER_HANDLED);
123 proto_attach(device_t dev)
125 struct proto_softc *sc;
129 sc = device_get_softc(dev);
132 for (res = 0; res < sc->sc_rescnt; res++) {
133 r = sc->sc_res + res;
142 r->r_size = rman_get_size(r->r_d.res);
143 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0666,
144 "proto/%s/%02x.%s", device_get_desc(dev), r->r_rid,
145 (r->r_type == SYS_RES_IOPORT) ? "io" : "mem");
146 r->r_u.cdev->si_drv1 = sc;
147 r->r_u.cdev->si_drv2 = r;
149 case PROTO_RES_PCICFG:
151 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0666,
152 "proto/%s/pcicfg", device_get_desc(dev));
153 r->r_u.cdev->si_drv1 = sc;
154 r->r_u.cdev->si_drv2 = r;
156 case PROTO_RES_BUSDMA:
157 r->r_d.busdma = proto_busdma_attach(sc);
158 r->r_size = 0; /* no read(2) nor write(2) */
159 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0666,
160 "proto/%s/busdma", device_get_desc(dev));
161 r->r_u.cdev->si_drv1 = sc;
162 r->r_u.cdev->si_drv2 = r;
170 proto_detach(device_t dev)
172 struct proto_softc *sc;
176 sc = device_get_softc(dev);
178 /* Don't detach if we have open device files. */
179 for (res = 0; res < sc->sc_rescnt; res++) {
180 r = sc->sc_res + res;
185 for (res = 0; res < sc->sc_rescnt; res++) {
186 r = sc->sc_res + res;
190 bus_release_resource(dev, r->r_type, r->r_rid,
194 bus_release_resource(dev, r->r_type, r->r_rid,
199 bus_release_resource(dev, r->r_type, r->r_rid,
201 destroy_dev(r->r_u.cdev);
203 case PROTO_RES_PCICFG:
204 destroy_dev(r->r_u.cdev);
206 case PROTO_RES_BUSDMA:
207 proto_busdma_detach(sc, r->r_d.busdma);
208 destroy_dev(r->r_u.cdev);
211 r->r_type = PROTO_RES_UNUSED;
222 proto_open(struct cdev *cdev, int oflags, int devtype, struct thread *td)
227 if (!atomic_cmpset_acq_ptr(&r->r_opened, 0UL, (uintptr_t)td->td_proc))
233 proto_close(struct cdev *cdev, int fflag, int devtype, struct thread *td)
236 struct proto_softc *sc;
240 if (!atomic_cmpset_acq_ptr(&r->r_opened, (uintptr_t)td->td_proc, 0UL))
242 if (r->r_type == PROTO_RES_BUSDMA)
243 proto_busdma_cleanup(sc, r->r_d.busdma);
248 proto_read(struct cdev *cdev, struct uio *uio, int ioflag)
256 struct proto_softc *sc;
267 width = uio->uio_resid;
268 if (width < 1 || width > 8 || bitcount16(width) > 1)
270 ofs = uio->uio_offset;
271 if (ofs + width > r->r_size)
276 buf.x1[0] = (r->r_type == PROTO_RES_PCICFG) ?
277 pci_read_config(dev, ofs, 1) : bus_read_1(r->r_d.res, ofs);
280 buf.x2[0] = (r->r_type == PROTO_RES_PCICFG) ?
281 pci_read_config(dev, ofs, 2) : bus_read_2(r->r_d.res, ofs);
284 buf.x4[0] = (r->r_type == PROTO_RES_PCICFG) ?
285 pci_read_config(dev, ofs, 4) : bus_read_4(r->r_d.res, ofs);
289 if (r->r_type == PROTO_RES_PCICFG)
291 buf.x8[0] = bus_read_8(r->r_d.res, ofs);
298 error = uiomove(&buf, width, uio);
303 proto_write(struct cdev *cdev, struct uio *uio, int ioflag)
311 struct proto_softc *sc;
322 width = uio->uio_resid;
323 if (width < 1 || width > 8 || bitcount16(width) > 1)
325 ofs = uio->uio_offset;
326 if (ofs + width > r->r_size)
329 error = uiomove(&buf, width, uio);
335 if (r->r_type == PROTO_RES_PCICFG)
336 pci_write_config(dev, ofs, buf.x1[0], 1);
338 bus_write_1(r->r_d.res, ofs, buf.x1[0]);
341 if (r->r_type == PROTO_RES_PCICFG)
342 pci_write_config(dev, ofs, buf.x2[0], 2);
344 bus_write_2(r->r_d.res, ofs, buf.x2[0]);
347 if (r->r_type == PROTO_RES_PCICFG)
348 pci_write_config(dev, ofs, buf.x4[0], 4);
350 bus_write_4(r->r_d.res, ofs, buf.x4[0]);
354 if (r->r_type == PROTO_RES_PCICFG)
356 bus_write_8(r->r_d.res, ofs, buf.x8[0]);
367 proto_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
370 struct proto_ioc_region *region;
371 struct proto_ioc_busdma *busdma;
373 struct proto_softc *sc;
381 case PROTO_IOC_REGION:
382 if (r->r_type == PROTO_RES_BUSDMA) {
386 region = (struct proto_ioc_region *)data;
387 region->size = r->r_size;
388 if (r->r_type == PROTO_RES_PCICFG)
391 region->address = rman_get_start(r->r_d.res);
393 case PROTO_IOC_BUSDMA:
394 if (r->r_type != PROTO_RES_BUSDMA) {
398 busdma = (struct proto_ioc_busdma *)data;
399 error = proto_busdma_ioctl(sc, r->r_d.busdma, busdma, td);
409 proto_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
410 int prot, vm_memattr_t *memattr)
414 if (offset & PAGE_MASK)
416 if (prot & PROT_EXEC)
423 if (offset >= r->r_size)
425 *paddr = rman_get_start(r->r_d.res) + offset;
427 *memattr = VM_MEMATTR_UNCACHEABLE;
430 case PROTO_RES_BUSDMA:
431 if (!proto_busdma_mmap_allowed(r->r_d.busdma, offset))