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 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/sys/pci/xrpu.c,v 1.19.2.1 2000/08/02 22:19:57 peter Exp $
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.
20 #include <sys/param.h>
21 #include <sys/systm.h>
23 #include <sys/kernel.h>
24 #include <sys/malloc.h>
25 #include <sys/timepps.h>
26 #include <sys/xrpuio.h>
28 #include <machine/bus.h>
30 #include <machine/resource.h>
31 #include <pci/pcireg.h>
32 #include <pci/pcivar.h>
36 * Device driver initialization stuff
39 static d_open_t xrpu_open;
40 static d_close_t xrpu_close;
41 static d_ioctl_t xrpu_ioctl;
42 static d_mmap_t xrpu_mmap;
44 #define CDEV_MAJOR 100
45 static struct cdevsw xrpu_cdevsw = {
47 /* close */ xrpu_close,
50 /* ioctl */ xrpu_ioctl,
53 /* strategy */ nostrategy,
62 static MALLOC_DEFINE(M_XRPU, "xrpu", "XRPU related");
64 static devclass_t xrpu_devclass;
66 #define dev2unit(devt) (minor(devt) & 0xff)
67 #define dev2pps(devt) ((minor(devt) >> 16)-1)
70 enum { NORMAL, TIMECOUNTER } mode;
71 vm_offset_t virbase, physbase;
73 struct timecounter tc;
74 u_int *trigger, *latch, dummy;
75 struct pps_state pps[XRPU_MAX_PPS];
76 u_int *assert[XRPU_MAX_PPS], *clear[XRPU_MAX_PPS];
80 xrpu_get_timecount(struct timecounter *tc)
82 struct softc *sc = tc->tc_priv;
84 sc->dummy += *sc->trigger;
85 return (*sc->latch & tc->tc_counter_mask);
89 xrpu_poll_pps(struct timecounter *tc)
91 struct softc *sc = tc->tc_priv;
93 unsigned count1, ppscount;
95 for (i = 0; i < XRPU_MAX_PPS; i++) {
97 ppscount = *(sc->assert[i]) & tc->tc_counter_mask;
101 ppscount = *(sc->assert[i]) & tc->tc_counter_mask;
102 } while (ppscount != count1 && ++j < 5);
103 pps_event(&sc->pps[i], tc, ppscount, PPS_CAPTUREASSERT);
107 ppscount = *(sc->clear[i]) & tc->tc_counter_mask;
110 ppscount = *(sc->clear[i]) & tc->tc_counter_mask;
111 } while (ppscount != count1 && ++j < 5);
112 pps_event(&sc->pps[i], tc, ppscount, PPS_CAPTURECLEAR);
118 xrpu_open(dev_t dev, int flag, int mode, struct proc *p)
120 struct softc *sc = devclass_get_softc(xrpu_devclass, dev2unit(dev));
129 xrpu_close(dev_t dev, int flag, int mode, struct proc *p)
135 xrpu_mmap(dev_t dev, vm_offset_t offset, int nprot)
137 struct softc *sc = dev->si_drv1;
138 if (offset >= 0x1000000)
140 return (i386_btop(sc->physbase + offset));
144 xrpu_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *pr)
146 struct softc *sc = dev->si_drv1;
149 if (sc->mode == TIMECOUNTER) {
151 if (i < 0 || i >= XRPU_MAX_PPS)
153 error = pps_ioctl(cmd, arg, &sc->pps[i]);
157 if (cmd == XRPU_IOC_TIMECOUNTING) {
158 struct xrpu_timecounting *xt = (struct xrpu_timecounting *)arg;
160 /* Name SHALL be zero terminated */
161 xt->xt_name[sizeof xt->xt_name - 1] = '\0';
162 i = strlen(xt->xt_name);
163 sc->tc.tc_name = (char *)malloc(i + 1, M_XRPU, M_WAITOK);
164 strcpy(sc->tc.tc_name, xt->xt_name);
165 sc->tc.tc_frequency = xt->xt_frequency;
166 sc->tc.tc_get_timecount = xrpu_get_timecount;
167 sc->tc.tc_poll_pps = xrpu_poll_pps;
169 sc->tc.tc_counter_mask = xt->xt_mask;
170 sc->trigger = sc->virbase62 + xt->xt_addr_trigger;
171 sc->latch = sc->virbase62 + xt->xt_addr_latch;
173 for (i = 0; i < XRPU_MAX_PPS; i++) {
174 if (xt->xt_pps[i].xt_addr_assert == 0
175 && xt->xt_pps[i].xt_addr_clear == 0)
177 make_dev(&xrpu_cdevsw, (i+1)<<16,
178 UID_ROOT, GID_WHEEL, 0600, "xpps%d", i);
179 sc->pps[i].ppscap = 0;
180 if (xt->xt_pps[i].xt_addr_assert) {
181 sc->assert[i] = sc->virbase62 + xt->xt_pps[i].xt_addr_assert;
182 sc->pps[i].ppscap |= PPS_CAPTUREASSERT;
184 if (xt->xt_pps[i].xt_addr_clear) {
185 sc->clear[i] = sc->virbase62 + xt->xt_pps[i].xt_addr_clear;
186 sc->pps[i].ppscap |= PPS_CAPTURECLEAR;
188 pps_init(&sc->pps[i]);
190 sc->mode = TIMECOUNTER;
191 init_timecounter(&sc->tc);
199 * PCI initialization stuff
203 xrpu_probe(device_t self)
208 switch (pci_get_devid(self)) {
210 desc = "VCC Hotworks-I xc6216";
216 device_set_desc(self, desc);
221 xrpu_attach(device_t self)
224 struct resource *res;
227 unit = device_get_unit(self);
228 sc = device_get_softc(self);
231 res = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
232 0, ~0, 1, 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);