1998-02-13 13:00:02 +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
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1998-06-12 23:15:53 +00:00
|
|
|
*
|
|
|
|
* This driver implements a draft-mogul-pps-api-02.txt PPS source.
|
|
|
|
*
|
|
|
|
* The input pin is pin#10
|
|
|
|
* The echo output pin is pin#14
|
1998-02-13 13:00:02 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
2000-01-14 00:18:06 +00:00
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
1998-02-13 13:00:02 +00:00
|
|
|
#include <sys/conf.h>
|
1998-06-07 19:44:22 +00:00
|
|
|
#include <sys/timepps.h>
|
2000-01-14 00:18:06 +00:00
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/resource.h>
|
|
|
|
#include <sys/rman.h>
|
1998-02-13 13:00:02 +00:00
|
|
|
|
|
|
|
#include <dev/ppbus/ppbconf.h>
|
2000-01-14 00:18:06 +00:00
|
|
|
#include "ppbus_if.h"
|
|
|
|
#include <dev/ppbus/ppbio.h>
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1999-08-08 22:25:46 +00:00
|
|
|
#define PPS_NAME "pps" /* our official name */
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2002-05-24 06:32:18 +00:00
|
|
|
#define PRVERBOSE(fmt, arg...) if (bootverbose) printf(fmt, ##arg);
|
2001-12-19 19:37:31 +00:00
|
|
|
|
1999-08-08 22:25:46 +00:00
|
|
|
struct pps_data {
|
1998-02-15 14:54:09 +00:00
|
|
|
struct ppb_device pps_dev;
|
2001-09-22 16:34:59 +00:00
|
|
|
struct pps_state pps[9];
|
|
|
|
dev_t devs[9];
|
|
|
|
device_t ppsdev;
|
|
|
|
device_t ppbus;
|
|
|
|
int busy;
|
|
|
|
struct callout_handle timeout;
|
|
|
|
int lastdata;
|
2000-01-14 00:18:06 +00:00
|
|
|
|
|
|
|
struct resource *intr_resource; /* interrupt resource */
|
|
|
|
void *intr_cookie; /* interrupt registration cookie */
|
1999-08-08 22:25:46 +00:00
|
|
|
};
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
static void ppsintr(void *arg);
|
2001-09-22 16:34:59 +00:00
|
|
|
static void ppshcpoll(void *arg);
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
#define DEVTOSOFTC(dev) \
|
|
|
|
((struct pps_data *)device_get_softc(dev))
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
static devclass_t pps_devclass;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
static d_open_t ppsopen;
|
|
|
|
static d_close_t ppsclose;
|
1998-06-07 19:44:22 +00:00
|
|
|
static d_ioctl_t ppsioctl;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
|
|
|
#define CDEV_MAJOR 89
|
1999-05-30 16:53:49 +00:00
|
|
|
static struct cdevsw pps_cdevsw = {
|
|
|
|
/* open */ ppsopen,
|
|
|
|
/* close */ ppsclose,
|
|
|
|
/* read */ noread,
|
|
|
|
/* write */ nowrite,
|
|
|
|
/* ioctl */ ppsioctl,
|
|
|
|
/* poll */ nopoll,
|
|
|
|
/* mmap */ nommap,
|
|
|
|
/* strategy */ nostrategy,
|
|
|
|
/* name */ PPS_NAME,
|
|
|
|
/* maj */ CDEV_MAJOR,
|
|
|
|
/* dump */ nodump,
|
|
|
|
/* psize */ nopsize,
|
|
|
|
/* flags */ 0,
|
|
|
|
};
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2000-01-23 14:41:04 +00:00
|
|
|
static void
|
|
|
|
ppsidentify(driver_t *driver, device_t parent)
|
|
|
|
{
|
|
|
|
|
2002-04-07 22:06:20 +00:00
|
|
|
BUS_ADD_CHILD(parent, 0, PPS_NAME, -1);
|
2000-01-23 14:41:04 +00:00
|
|
|
}
|
|
|
|
|
2001-09-22 16:34:59 +00:00
|
|
|
static int
|
|
|
|
ppstry(device_t ppbus, int send, int expect)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ppb_wdtr(ppbus, send);
|
|
|
|
i = ppb_rdtr(ppbus);
|
2001-12-19 19:37:31 +00:00
|
|
|
PRVERBOSE("S: %02x E: %02x G: %02x\n", send, expect, i);
|
2001-09-22 16:34:59 +00:00
|
|
|
return (i != expect);
|
|
|
|
}
|
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
static int
|
|
|
|
ppsprobe(device_t ppsdev)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
2001-12-19 19:37:31 +00:00
|
|
|
device_set_desc(ppsdev, "Pulse per second Timing Interface");
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ppsattach(device_t dev)
|
|
|
|
{
|
|
|
|
struct pps_data *sc = DEVTOSOFTC(dev);
|
|
|
|
device_t ppbus = device_get_parent(dev);
|
|
|
|
dev_t d;
|
2002-11-07 17:25:33 +00:00
|
|
|
intptr_t irq;
|
|
|
|
int i, unit, zero = 0;
|
2001-09-22 16:34:59 +00:00
|
|
|
|
2001-12-19 19:37:31 +00:00
|
|
|
bzero(sc, sizeof(struct pps_data)); /* XXX doesn't newbus do this? */
|
|
|
|
|
|
|
|
/* retrieve the ppbus irq */
|
|
|
|
BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
|
|
|
|
|
|
|
|
if (irq > 0) {
|
|
|
|
/* declare our interrupt handler */
|
|
|
|
sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
|
|
|
|
&zero, irq, irq, 1, RF_SHAREABLE);
|
|
|
|
}
|
|
|
|
/* interrupts seem mandatory */
|
|
|
|
if (sc->intr_resource == NULL)
|
|
|
|
return (ENXIO);
|
1999-05-31 11:29:30 +00:00
|
|
|
|
2001-12-19 19:37:31 +00:00
|
|
|
sc->ppsdev = dev;
|
|
|
|
sc->ppbus = ppbus;
|
2001-09-22 16:34:59 +00:00
|
|
|
unit = device_get_unit(ppbus);
|
2001-12-19 19:37:31 +00:00
|
|
|
d = make_dev(&pps_cdevsw, unit,
|
2002-12-27 16:34:12 +00:00
|
|
|
UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", unit);
|
2001-12-19 19:37:31 +00:00
|
|
|
sc->devs[0] = d;
|
2001-09-22 16:34:59 +00:00
|
|
|
sc->pps[0].ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
|
2001-12-19 19:37:31 +00:00
|
|
|
d->si_drv1 = sc;
|
|
|
|
d->si_drv2 = (void*)0;
|
2001-09-22 16:34:59 +00:00
|
|
|
pps_init(&sc->pps[0]);
|
|
|
|
|
2001-12-19 19:37:31 +00:00
|
|
|
if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT))
|
2001-09-22 16:34:59 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
do {
|
|
|
|
i = ppb_set_mode(sc->ppbus, PPB_EPP);
|
2001-12-19 19:37:31 +00:00
|
|
|
PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
|
2001-09-22 16:34:59 +00:00
|
|
|
if (i == -1)
|
|
|
|
break;
|
|
|
|
i = 0;
|
|
|
|
ppb_wctr(ppbus, i);
|
|
|
|
if (ppstry(ppbus, 0x00, 0x00))
|
|
|
|
break;
|
|
|
|
if (ppstry(ppbus, 0x55, 0x55))
|
|
|
|
break;
|
|
|
|
if (ppstry(ppbus, 0xaa, 0xaa))
|
|
|
|
break;
|
|
|
|
if (ppstry(ppbus, 0xff, 0xff))
|
|
|
|
break;
|
|
|
|
|
|
|
|
i = IRQENABLE | PCD | STROBE | nINIT | SELECTIN;
|
|
|
|
ppb_wctr(ppbus, i);
|
2001-12-19 19:37:31 +00:00
|
|
|
PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
|
2001-09-22 16:34:59 +00:00
|
|
|
if (ppstry(ppbus, 0x00, 0x00))
|
|
|
|
break;
|
|
|
|
if (ppstry(ppbus, 0x55, 0x00))
|
|
|
|
break;
|
|
|
|
if (ppstry(ppbus, 0xaa, 0x00))
|
|
|
|
break;
|
|
|
|
if (ppstry(ppbus, 0xff, 0x00))
|
|
|
|
break;
|
|
|
|
|
|
|
|
i = IRQENABLE | PCD | nINIT | SELECTIN;
|
|
|
|
ppb_wctr(ppbus, i);
|
2001-12-19 19:37:31 +00:00
|
|
|
PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
|
2001-09-22 16:34:59 +00:00
|
|
|
ppstry(ppbus, 0x00, 0xff);
|
|
|
|
ppstry(ppbus, 0x55, 0xff);
|
|
|
|
ppstry(ppbus, 0xaa, 0xff);
|
|
|
|
ppstry(ppbus, 0xff, 0xff);
|
|
|
|
|
|
|
|
for (i = 1; i < 9; i++) {
|
2001-12-19 19:37:31 +00:00
|
|
|
d = make_dev(&pps_cdevsw, unit + 0x10000 * i,
|
2002-12-27 16:34:12 +00:00
|
|
|
UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%db%d", unit, i - 1);
|
2001-12-19 19:37:31 +00:00
|
|
|
sc->devs[i] = d;
|
2001-09-22 16:34:59 +00:00
|
|
|
sc->pps[i].ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
|
2001-12-19 19:37:31 +00:00
|
|
|
d->si_drv1 = sc;
|
2002-11-07 17:25:33 +00:00
|
|
|
d->si_drv2 = (void *)(intptr_t)i;
|
2001-09-22 16:34:59 +00:00
|
|
|
pps_init(&sc->pps[i]);
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
i = ppb_set_mode(sc->ppbus, PPB_COMPATIBLE);
|
2001-12-19 19:37:31 +00:00
|
|
|
ppb_release_bus(ppbus, dev);
|
2000-01-14 00:18:06 +00:00
|
|
|
|
|
|
|
return (0);
|
1998-02-13 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
ppsopen(dev_t dev, int flags, int fmt, struct thread *td)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
2001-09-22 16:34:59 +00:00
|
|
|
struct pps_data *sc = dev->si_drv1;
|
2002-11-07 17:25:33 +00:00
|
|
|
int subdev = (intptr_t)dev->si_drv2;
|
2001-09-22 16:34:59 +00:00
|
|
|
int error, i;
|
|
|
|
|
|
|
|
if (!sc->busy) {
|
|
|
|
device_t ppsdev = sc->ppsdev;
|
|
|
|
device_t ppbus = sc->ppbus;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
|
1999-04-25 08:58:10 +00:00
|
|
|
return (EINTR);
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
/* attach the interrupt handler */
|
|
|
|
if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
|
|
|
|
INTR_TYPE_TTY, ppsintr, ppsdev,
|
|
|
|
&sc->intr_cookie))) {
|
|
|
|
ppb_release_bus(ppbus, ppsdev);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2001-09-22 16:34:59 +00:00
|
|
|
i = ppb_set_mode(sc->ppbus, PPB_PS2);
|
2001-12-19 19:37:31 +00:00
|
|
|
PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2001-09-22 16:34:59 +00:00
|
|
|
i = IRQENABLE | PCD | nINIT | SELECTIN;
|
|
|
|
ppb_wctr(ppbus, i);
|
|
|
|
}
|
|
|
|
if (subdev > 0 && !(sc->busy & ~1)) {
|
|
|
|
sc->timeout = timeout(ppshcpoll, sc, 1);
|
|
|
|
sc->lastdata = ppb_rdtr(sc->ppbus);
|
|
|
|
}
|
|
|
|
sc->busy |= (1 << subdev);
|
1998-02-13 13:00:02 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
ppsclose(dev_t dev, int flags, int fmt, struct thread *td)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
2001-09-22 16:34:59 +00:00
|
|
|
struct pps_data *sc = dev->si_drv1;
|
2002-11-07 17:25:33 +00:00
|
|
|
int subdev = (intptr_t)dev->si_drv2;
|
2001-09-22 16:34:59 +00:00
|
|
|
|
|
|
|
sc->pps[subdev].ppsparam.mode = 0; /* PHK ??? */
|
|
|
|
sc->busy &= ~(1 << subdev);
|
|
|
|
if (subdev > 0 && !(sc->busy & ~1))
|
|
|
|
untimeout(ppshcpoll, sc, sc->timeout);
|
|
|
|
if (!sc->busy) {
|
|
|
|
device_t ppsdev = sc->ppsdev;
|
|
|
|
device_t ppbus = sc->ppbus;
|
|
|
|
|
|
|
|
ppb_wdtr(ppbus, 0);
|
|
|
|
ppb_wctr(ppbus, 0);
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2001-09-22 16:34:59 +00:00
|
|
|
/* Note: the interrupt handler is automatically detached */
|
|
|
|
ppb_set_mode(ppbus, PPB_COMPATIBLE);
|
|
|
|
ppb_release_bus(ppbus, ppsdev);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
1999-01-30 15:35:39 +00:00
|
|
|
|
2001-09-22 16:34:59 +00:00
|
|
|
static void
|
|
|
|
ppshcpoll(void *arg)
|
|
|
|
{
|
|
|
|
struct pps_data *sc = arg;
|
|
|
|
int i, j, k, l;
|
1999-01-30 15:35:39 +00:00
|
|
|
|
2001-09-22 16:34:59 +00:00
|
|
|
if (!(sc->busy & ~1))
|
|
|
|
return;
|
|
|
|
sc->timeout = timeout(ppshcpoll, sc, 1);
|
|
|
|
i = ppb_rdtr(sc->ppbus);
|
|
|
|
if (i == sc->lastdata)
|
|
|
|
return;
|
|
|
|
l = sc->lastdata ^ i;
|
|
|
|
k = 1;
|
|
|
|
for (j = 1; j < 9; j ++) {
|
2002-04-26 20:24:28 +00:00
|
|
|
if (l & k) {
|
|
|
|
pps_capture(&sc->pps[j]);
|
|
|
|
pps_event(&sc->pps[j],
|
|
|
|
i & k ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
|
|
|
|
}
|
2001-09-22 16:34:59 +00:00
|
|
|
k += k;
|
|
|
|
}
|
|
|
|
sc->lastdata = i;
|
1998-02-13 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-01-14 00:18:06 +00:00
|
|
|
ppsintr(void *arg)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
2000-01-14 00:18:06 +00:00
|
|
|
device_t ppsdev = (device_t)arg;
|
|
|
|
struct pps_data *sc = DEVTOSOFTC(ppsdev);
|
2001-09-22 16:34:59 +00:00
|
|
|
device_t ppbus = sc->ppbus;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2002-04-26 20:24:28 +00:00
|
|
|
pps_capture(&sc->pps[0]);
|
2000-01-14 00:18:06 +00:00
|
|
|
if (!(ppb_rstr(ppbus) & nACK))
|
1998-02-13 13:00:02 +00:00
|
|
|
return;
|
2001-09-22 16:34:59 +00:00
|
|
|
if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
|
2002-04-26 20:24:28 +00:00
|
|
|
pps_event(&sc->pps[0], PPS_CAPTUREASSERT);
|
2001-09-22 16:34:59 +00:00
|
|
|
if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wctr(ppbus, IRQENABLE);
|
1998-02-13 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
1998-06-07 19:44:22 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
2001-09-22 16:34:59 +00:00
|
|
|
struct pps_data *sc = dev->si_drv1;
|
2002-11-07 17:25:33 +00:00
|
|
|
int subdev = (intptr_t)dev->si_drv2;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2001-09-22 16:34:59 +00:00
|
|
|
return (pps_ioctl(cmd, data, &sc->pps[subdev]));
|
1998-02-13 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
2000-01-23 14:41:04 +00:00
|
|
|
static device_method_t pps_methods[] = {
|
|
|
|
/* device interface */
|
|
|
|
DEVMETHOD(device_identify, ppsidentify),
|
|
|
|
DEVMETHOD(device_probe, ppsprobe),
|
|
|
|
DEVMETHOD(device_attach, ppsattach),
|
|
|
|
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static driver_t pps_driver = {
|
|
|
|
PPS_NAME,
|
|
|
|
pps_methods,
|
|
|
|
sizeof(struct pps_data),
|
|
|
|
};
|
2000-01-14 00:18:06 +00:00
|
|
|
DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);
|