1998-05-30 18:28:12 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* A very simple device driver for PCI cards based on Xilinx 6200 series
|
|
|
|
* FPGA/RPU devices. Current Functionality is to allow you to open and
|
|
|
|
* mmap the entire thing into your program.
|
|
|
|
*
|
|
|
|
* Hardware currently supported:
|
|
|
|
* www.vcc.com HotWorks 1 6216 based card.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 06:34:30 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-05-30 18:28:12 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/kernel.h>
|
1998-10-24 19:47:42 +00:00
|
|
|
#include <sys/malloc.h>
|
2000-03-20 14:09:06 +00:00
|
|
|
#include <sys/timetc.h>
|
1998-10-24 19:47:42 +00:00
|
|
|
#include <sys/timepps.h>
|
|
|
|
#include <sys/xrpuio.h>
|
2000-02-12 07:56:44 +00:00
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <sys/rman.h>
|
|
|
|
#include <machine/resource.h>
|
2003-08-22 07:20:27 +00:00
|
|
|
#include <dev/pci/pcireg.h>
|
|
|
|
#include <dev/pci/pcivar.h>
|
2000-02-12 07:56:44 +00:00
|
|
|
#include "pci_if.h"
|
1998-10-24 19:47:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Device driver initialization stuff
|
|
|
|
*/
|
|
|
|
|
|
|
|
static d_open_t xrpu_open;
|
|
|
|
static d_close_t xrpu_close;
|
|
|
|
static d_ioctl_t xrpu_ioctl;
|
|
|
|
static d_mmap_t xrpu_mmap;
|
|
|
|
|
1999-05-30 16:53:49 +00:00
|
|
|
static struct cdevsw xrpu_cdevsw = {
|
2004-02-21 21:10:55 +00:00
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_flags = D_NEEDGIANT,
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = xrpu_open,
|
|
|
|
.d_close = xrpu_close,
|
|
|
|
.d_ioctl = xrpu_ioctl,
|
|
|
|
.d_mmap = xrpu_mmap,
|
|
|
|
.d_name = "xrpu",
|
1998-10-24 19:47:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static MALLOC_DEFINE(M_XRPU, "xrpu", "XRPU related");
|
|
|
|
|
2000-02-12 07:56:44 +00:00
|
|
|
static devclass_t xrpu_devclass;
|
|
|
|
|
1998-10-24 19:47:42 +00:00
|
|
|
#define dev2unit(devt) (minor(devt) & 0xff)
|
|
|
|
#define dev2pps(devt) ((minor(devt) >> 16)-1)
|
|
|
|
|
2000-02-12 07:56:44 +00:00
|
|
|
struct softc {
|
1998-10-24 19:47:42 +00:00
|
|
|
enum { NORMAL, TIMECOUNTER } mode;
|
|
|
|
vm_offset_t virbase, physbase;
|
|
|
|
u_int *virbase62;
|
|
|
|
struct timecounter tc;
|
|
|
|
u_int *trigger, *latch, dummy;
|
1999-03-11 15:09:51 +00:00
|
|
|
struct pps_state pps[XRPU_MAX_PPS];
|
|
|
|
u_int *assert[XRPU_MAX_PPS], *clear[XRPU_MAX_PPS];
|
2000-02-12 07:56:44 +00:00
|
|
|
};
|
1998-10-24 19:47:42 +00:00
|
|
|
|
|
|
|
static unsigned
|
|
|
|
xrpu_get_timecount(struct timecounter *tc)
|
|
|
|
{
|
|
|
|
struct softc *sc = tc->tc_priv;
|
|
|
|
|
|
|
|
sc->dummy += *sc->trigger;
|
|
|
|
return (*sc->latch & tc->tc_counter_mask);
|
|
|
|
}
|
|
|
|
|
2000-02-12 07:56:44 +00:00
|
|
|
static void
|
1998-10-24 19:47:42 +00:00
|
|
|
xrpu_poll_pps(struct timecounter *tc)
|
|
|
|
{
|
|
|
|
struct softc *sc = tc->tc_priv;
|
1999-07-03 08:23:00 +00:00
|
|
|
int i, j;
|
1998-10-24 19:47:42 +00:00
|
|
|
unsigned count1, ppscount;
|
|
|
|
|
|
|
|
for (i = 0; i < XRPU_MAX_PPS; i++) {
|
1999-03-11 15:09:51 +00:00
|
|
|
if (sc->assert[i]) {
|
2002-04-26 21:22:02 +00:00
|
|
|
pps_capture(&sc->pps[i]);
|
1999-03-11 15:09:51 +00:00
|
|
|
ppscount = *(sc->assert[i]) & tc->tc_counter_mask;
|
1999-07-03 08:23:00 +00:00
|
|
|
j = 0;
|
1998-10-24 19:47:42 +00:00
|
|
|
do {
|
|
|
|
count1 = ppscount;
|
1999-03-11 15:09:51 +00:00
|
|
|
ppscount = *(sc->assert[i]) & tc->tc_counter_mask;
|
1999-07-03 08:23:00 +00:00
|
|
|
} while (ppscount != count1 && ++j < 5);
|
2002-04-26 20:24:28 +00:00
|
|
|
sc->pps[i].capcount = ppscount;
|
|
|
|
pps_event(&sc->pps[i], PPS_CAPTUREASSERT);
|
1998-10-24 19:47:42 +00:00
|
|
|
}
|
1999-03-11 15:09:51 +00:00
|
|
|
if (sc->clear[i]) {
|
2002-04-26 21:22:02 +00:00
|
|
|
pps_capture(&sc->pps[i]);
|
1999-07-03 08:23:00 +00:00
|
|
|
j = 0;
|
1999-03-11 15:09:51 +00:00
|
|
|
ppscount = *(sc->clear[i]) & tc->tc_counter_mask;
|
1998-10-24 19:47:42 +00:00
|
|
|
do {
|
|
|
|
count1 = ppscount;
|
1999-03-11 15:09:51 +00:00
|
|
|
ppscount = *(sc->clear[i]) & tc->tc_counter_mask;
|
1999-07-03 08:23:00 +00:00
|
|
|
} while (ppscount != count1 && ++j < 5);
|
2002-04-26 20:24:28 +00:00
|
|
|
sc->pps[i].capcount = ppscount;
|
|
|
|
pps_event(&sc->pps[i], PPS_CAPTURECLEAR);
|
1998-10-24 19:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-05-30 18:28:12 +00:00
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
xrpu_open(dev_t dev, int flag, int mode, struct thread *td)
|
1998-05-30 18:28:12 +00:00
|
|
|
{
|
2000-02-12 07:56:44 +00:00
|
|
|
struct softc *sc = devclass_get_softc(xrpu_devclass, dev2unit(dev));
|
1999-07-13 08:15:22 +00:00
|
|
|
|
|
|
|
if (!sc)
|
|
|
|
return (ENXIO);
|
2000-02-12 07:56:44 +00:00
|
|
|
dev->si_drv1 = sc;
|
1998-05-30 18:28:12 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
xrpu_close(dev_t dev, int flag, int mode, struct thread *td)
|
1998-05-30 18:28:12 +00:00
|
|
|
{
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-03-25 00:07:06 +00:00
|
|
|
xrpu_mmap(dev_t dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
|
1998-05-30 18:28:12 +00:00
|
|
|
{
|
2000-02-12 07:56:44 +00:00
|
|
|
struct softc *sc = dev->si_drv1;
|
1998-05-30 18:28:12 +00:00
|
|
|
if (offset >= 0x1000000)
|
|
|
|
return (-1);
|
2003-02-25 03:21:22 +00:00
|
|
|
*paddr = sc->physbase + offset;
|
|
|
|
return (0);
|
1998-05-30 18:28:12 +00:00
|
|
|
}
|
|
|
|
|
1998-10-24 19:47:42 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
xrpu_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct thread *tdr)
|
1998-10-24 19:47:42 +00:00
|
|
|
{
|
2000-02-12 07:56:44 +00:00
|
|
|
struct softc *sc = dev->si_drv1;
|
1998-10-24 19:47:42 +00:00
|
|
|
int i, error;
|
1998-05-30 18:28:12 +00:00
|
|
|
|
1998-10-24 19:47:42 +00:00
|
|
|
if (sc->mode == TIMECOUNTER) {
|
|
|
|
i = dev2pps(dev);
|
|
|
|
if (i < 0 || i >= XRPU_MAX_PPS)
|
|
|
|
return ENODEV;
|
1999-03-11 15:09:51 +00:00
|
|
|
error = pps_ioctl(cmd, arg, &sc->pps[i]);
|
1998-10-24 19:47:42 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd == XRPU_IOC_TIMECOUNTING) {
|
|
|
|
struct xrpu_timecounting *xt = (struct xrpu_timecounting *)arg;
|
|
|
|
|
|
|
|
/* Name SHALL be zero terminated */
|
|
|
|
xt->xt_name[sizeof xt->xt_name - 1] = '\0';
|
|
|
|
i = strlen(xt->xt_name);
|
2003-02-19 05:47:46 +00:00
|
|
|
sc->tc.tc_name = (char *)malloc(i + 1, M_XRPU, M_WAITOK);
|
1998-10-24 19:47:42 +00:00
|
|
|
strcpy(sc->tc.tc_name, xt->xt_name);
|
|
|
|
sc->tc.tc_frequency = xt->xt_frequency;
|
|
|
|
sc->tc.tc_get_timecount = xrpu_get_timecount;
|
|
|
|
sc->tc.tc_poll_pps = xrpu_poll_pps;
|
|
|
|
sc->tc.tc_priv = sc;
|
|
|
|
sc->tc.tc_counter_mask = xt->xt_mask;
|
|
|
|
sc->trigger = sc->virbase62 + xt->xt_addr_trigger;
|
|
|
|
sc->latch = sc->virbase62 + xt->xt_addr_latch;
|
|
|
|
|
|
|
|
for (i = 0; i < XRPU_MAX_PPS; i++) {
|
|
|
|
if (xt->xt_pps[i].xt_addr_assert == 0
|
|
|
|
&& xt->xt_pps[i].xt_addr_clear == 0)
|
|
|
|
continue;
|
1999-08-23 20:59:21 +00:00
|
|
|
make_dev(&xrpu_cdevsw, (i+1)<<16,
|
|
|
|
UID_ROOT, GID_WHEEL, 0600, "xpps%d", i);
|
1999-03-11 15:09:51 +00:00
|
|
|
sc->pps[i].ppscap = 0;
|
1998-10-24 19:47:42 +00:00
|
|
|
if (xt->xt_pps[i].xt_addr_assert) {
|
1999-03-11 15:09:51 +00:00
|
|
|
sc->assert[i] = sc->virbase62 + xt->xt_pps[i].xt_addr_assert;
|
|
|
|
sc->pps[i].ppscap |= PPS_CAPTUREASSERT;
|
1998-10-24 19:47:42 +00:00
|
|
|
}
|
|
|
|
if (xt->xt_pps[i].xt_addr_clear) {
|
1999-03-11 15:09:51 +00:00
|
|
|
sc->clear[i] = sc->virbase62 + xt->xt_pps[i].xt_addr_clear;
|
|
|
|
sc->pps[i].ppscap |= PPS_CAPTURECLEAR;
|
1998-10-24 19:47:42 +00:00
|
|
|
}
|
1999-03-11 15:09:51 +00:00
|
|
|
pps_init(&sc->pps[i]);
|
1998-10-24 19:47:42 +00:00
|
|
|
}
|
|
|
|
sc->mode = TIMECOUNTER;
|
2000-03-20 14:09:06 +00:00
|
|
|
tc_init(&sc->tc);
|
1998-10-24 19:47:42 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
error = ENOTTY;
|
|
|
|
return (error);
|
|
|
|
}
|
1998-05-30 18:28:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* PCI initialization stuff
|
|
|
|
*/
|
|
|
|
|
2000-02-12 07:56:44 +00:00
|
|
|
static int
|
|
|
|
xrpu_probe(device_t self)
|
1998-05-30 18:28:12 +00:00
|
|
|
{
|
2000-02-12 07:56:44 +00:00
|
|
|
char *desc;
|
1998-05-30 18:28:12 +00:00
|
|
|
|
2000-02-12 07:56:44 +00:00
|
|
|
desc = NULL;
|
|
|
|
switch (pci_get_devid(self)) {
|
|
|
|
case 0x6216133e:
|
|
|
|
desc = "VCC Hotworks-I xc6216";
|
|
|
|
break;
|
1998-05-30 18:28:12 +00:00
|
|
|
}
|
2000-02-12 07:56:44 +00:00
|
|
|
if (desc == NULL)
|
|
|
|
return ENXIO;
|
|
|
|
|
|
|
|
device_set_desc(self, desc);
|
1998-05-30 18:28:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-12 07:56:44 +00:00
|
|
|
static int
|
|
|
|
xrpu_attach(device_t self)
|
1998-05-30 18:28:12 +00:00
|
|
|
{
|
1998-10-24 19:47:42 +00:00
|
|
|
struct softc *sc;
|
2000-02-12 07:56:44 +00:00
|
|
|
struct resource *res;
|
|
|
|
int rid, unit;
|
1998-10-24 19:47:42 +00:00
|
|
|
|
2000-02-12 07:56:44 +00:00
|
|
|
unit = device_get_unit(self);
|
|
|
|
sc = device_get_softc(self);
|
1998-10-24 19:47:42 +00:00
|
|
|
sc->mode = NORMAL;
|
2003-09-02 17:30:40 +00:00
|
|
|
rid = PCIR_BAR(0);
|
2004-03-17 17:50:55 +00:00
|
|
|
res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
|
2000-02-12 07:56:44 +00:00
|
|
|
if (res == NULL) {
|
|
|
|
device_printf(self, "Could not map memory\n");
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
sc->virbase = (vm_offset_t)rman_get_virtual(res);
|
|
|
|
sc->physbase = rman_get_start(res);
|
1998-10-24 19:47:42 +00:00
|
|
|
sc->virbase62 = (u_int *)(sc->virbase + 0x800000);
|
1998-05-30 18:28:12 +00:00
|
|
|
|
1998-10-24 19:47:42 +00:00
|
|
|
if (bootverbose)
|
|
|
|
printf("Mapped physbase %#lx to virbase %#lx\n",
|
|
|
|
(u_long)sc->physbase, (u_long)sc->virbase);
|
1998-05-30 18:28:12 +00:00
|
|
|
|
1999-08-23 20:59:21 +00:00
|
|
|
make_dev(&xrpu_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "xrpu%d", unit);
|
2000-02-12 07:56:44 +00:00
|
|
|
return 0;
|
1998-05-30 18:28:12 +00:00
|
|
|
}
|
2000-02-12 07:56:44 +00:00
|
|
|
|
|
|
|
static device_method_t xrpu_methods[] = {
|
|
|
|
/* Device interface */
|
|
|
|
DEVMETHOD(device_probe, xrpu_probe),
|
|
|
|
DEVMETHOD(device_attach, xrpu_attach),
|
|
|
|
DEVMETHOD(device_suspend, bus_generic_suspend),
|
|
|
|
DEVMETHOD(device_resume, bus_generic_resume),
|
|
|
|
DEVMETHOD(device_shutdown, bus_generic_shutdown),
|
|
|
|
|
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static driver_t xrpu_driver = {
|
|
|
|
"xrpu",
|
|
|
|
xrpu_methods,
|
|
|
|
sizeof(struct softc)
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
DRIVER_MODULE(xrpu, pci, xrpu_driver, xrpu_devclass, 0, 0);
|
2003-04-15 06:37:30 +00:00
|
|
|
MODULE_DEPEND(xrpu, pci, 1, 1, 1);
|