2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
11 * A very simple device driver for PCI cards based on Xilinx 6200 series
12 * FPGA/RPU devices. Current Functionality is to allow you to open and
13 * mmap the entire thing into your program.
15 * Hardware currently supported:
16 * www.vcc.com HotWorks 1 6216 based card.
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
22 #include <sys/param.h>
23 #include <sys/systm.h>
25 #include <sys/kernel.h>
26 #include <sys/malloc.h>
27 #include <sys/module.h>
28 #include <sys/timetc.h>
29 #include <sys/timepps.h>
30 #include <sys/xrpuio.h>
32 #include <machine/bus.h>
34 #include <machine/resource.h>
35 #include <dev/pci/pcireg.h>
36 #include <dev/pci/pcivar.h>
40 * Device driver initialization stuff
43 static d_open_t xrpu_open;
44 static d_close_t xrpu_close;
45 static d_ioctl_t xrpu_ioctl;
46 static d_mmap_t xrpu_mmap;
48 static struct cdevsw xrpu_cdevsw = {
49 .d_version = D_VERSION,
50 .d_flags = D_NEEDGIANT,
52 .d_close = xrpu_close,
53 .d_ioctl = xrpu_ioctl,
58 static MALLOC_DEFINE(M_XRPU, "xrpu", "XRPU related");
60 static devclass_t xrpu_devclass;
62 #define dev2unit(devt) (minor(devt) & 0xff)
63 #define dev2pps(devt) ((minor(devt) >> 16)-1)
66 enum { NORMAL, TIMECOUNTER } mode;
67 vm_offset_t virbase, physbase;
69 struct timecounter tc;
70 u_int *trigger, *latch, dummy;
71 struct pps_state pps[XRPU_MAX_PPS];
72 u_int *assert[XRPU_MAX_PPS], *clear[XRPU_MAX_PPS];
76 xrpu_get_timecount(struct timecounter *tc)
78 struct softc *sc = tc->tc_priv;
80 sc->dummy += *sc->trigger;
81 return (*sc->latch & tc->tc_counter_mask);
85 xrpu_poll_pps(struct timecounter *tc)
87 struct softc *sc = tc->tc_priv;
89 unsigned count1, ppscount;
91 for (i = 0; i < XRPU_MAX_PPS; i++) {
93 pps_capture(&sc->pps[i]);
94 ppscount = *(sc->assert[i]) & tc->tc_counter_mask;
98 ppscount = *(sc->assert[i]) & tc->tc_counter_mask;
99 } while (ppscount != count1 && ++j < 5);
100 sc->pps[i].capcount = ppscount;
101 pps_event(&sc->pps[i], PPS_CAPTUREASSERT);
104 pps_capture(&sc->pps[i]);
106 ppscount = *(sc->clear[i]) & tc->tc_counter_mask;
109 ppscount = *(sc->clear[i]) & tc->tc_counter_mask;
110 } while (ppscount != count1 && ++j < 5);
111 sc->pps[i].capcount = ppscount;
112 pps_event(&sc->pps[i], PPS_CAPTURECLEAR);
118 xrpu_open(struct cdev *dev, int flag, int mode, struct thread *td)
120 struct softc *sc = devclass_get_softc(xrpu_devclass, dev2unit(dev));
129 xrpu_close(struct cdev *dev, int flag, int mode, struct thread *td)
135 xrpu_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
137 struct softc *sc = dev->si_drv1;
138 if (offset >= 0x1000000)
140 *paddr = sc->physbase + offset;
145 xrpu_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *tdr)
147 struct softc *sc = dev->si_drv1;
150 if (sc->mode == TIMECOUNTER) {
152 if (i < 0 || i >= XRPU_MAX_PPS)
154 error = pps_ioctl(cmd, arg, &sc->pps[i]);
158 if (cmd == XRPU_IOC_TIMECOUNTING) {
159 struct xrpu_timecounting *xt = (struct xrpu_timecounting *)arg;
161 /* Name SHALL be zero terminated */
162 xt->xt_name[sizeof xt->xt_name - 1] = '\0';
163 i = strlen(xt->xt_name);
164 sc->tc.tc_name = (char *)malloc(i + 1, M_XRPU, M_WAITOK);
165 strcpy(sc->tc.tc_name, xt->xt_name);
166 sc->tc.tc_frequency = xt->xt_frequency;
167 sc->tc.tc_get_timecount = xrpu_get_timecount;
168 sc->tc.tc_poll_pps = xrpu_poll_pps;
170 sc->tc.tc_counter_mask = xt->xt_mask;
171 sc->trigger = sc->virbase62 + xt->xt_addr_trigger;
172 sc->latch = sc->virbase62 + xt->xt_addr_latch;
174 for (i = 0; i < XRPU_MAX_PPS; i++) {
175 if (xt->xt_pps[i].xt_addr_assert == 0
176 && xt->xt_pps[i].xt_addr_clear == 0)
178 make_dev(&xrpu_cdevsw, (i+1)<<16,
179 UID_ROOT, GID_WHEEL, 0600, "xpps%d", i);
180 sc->pps[i].ppscap = 0;
181 if (xt->xt_pps[i].xt_addr_assert) {
182 sc->assert[i] = sc->virbase62 + xt->xt_pps[i].xt_addr_assert;
183 sc->pps[i].ppscap |= PPS_CAPTUREASSERT;
185 if (xt->xt_pps[i].xt_addr_clear) {
186 sc->clear[i] = sc->virbase62 + xt->xt_pps[i].xt_addr_clear;
187 sc->pps[i].ppscap |= PPS_CAPTURECLEAR;
189 pps_init(&sc->pps[i]);
191 sc->mode = TIMECOUNTER;
200 * PCI initialization stuff
204 xrpu_probe(device_t self)
209 switch (pci_get_devid(self)) {
211 desc = "VCC Hotworks-I xc6216";
217 device_set_desc(self, desc);
218 return (BUS_PROBE_DEFAULT);
222 xrpu_attach(device_t self)
225 struct resource *res;
228 unit = device_get_unit(self);
229 sc = device_get_softc(self);
232 res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
234 device_printf(self, "Could not map memory\n");
237 sc->virbase = (vm_offset_t)rman_get_virtual(res);
238 sc->physbase = rman_get_start(res);
239 sc->virbase62 = (u_int *)(sc->virbase + 0x800000);
242 printf("Mapped physbase %#lx to virbase %#lx\n",
243 (u_long)sc->physbase, (u_long)sc->virbase);
245 make_dev(&xrpu_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "xrpu%d", unit);
249 static device_method_t xrpu_methods[] = {
250 /* Device interface */
251 DEVMETHOD(device_probe, xrpu_probe),
252 DEVMETHOD(device_attach, xrpu_attach),
253 DEVMETHOD(device_suspend, bus_generic_suspend),
254 DEVMETHOD(device_resume, bus_generic_resume),
255 DEVMETHOD(device_shutdown, bus_generic_shutdown),
260 static driver_t xrpu_driver = {
267 DRIVER_MODULE(xrpu, pci, xrpu_driver, xrpu_devclass, 0, 0);
268 MODULE_DEPEND(xrpu, pci, 1, 1, 1);