2005-01-06 01:43:34 +00:00
|
|
|
/*-
|
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
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-08-24 17:55:58 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
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 {
|
2005-03-10 22:21:16 +00:00
|
|
|
struct ppb_device pps_dev;
|
2001-09-22 16:34:59 +00:00
|
|
|
struct pps_state pps[9];
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *devs[9];
|
2001-09-22 16:34:59 +00:00
|
|
|
device_t ppsdev;
|
|
|
|
device_t ppbus;
|
|
|
|
int busy;
|
|
|
|
struct callout_handle timeout;
|
|
|
|
int lastdata;
|
2000-01-14 00:18:06 +00:00
|
|
|
|
2005-03-11 07:03:46 +00:00
|
|
|
struct mtx mtx;
|
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
|
|
|
|
1999-05-30 16:53:49 +00:00
|
|
|
static struct cdevsw pps_cdevsw = {
|
2004-02-21 21:10:55 +00:00
|
|
|
.d_version = D_VERSION,
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = ppsopen,
|
|
|
|
.d_close = ppsclose,
|
|
|
|
.d_ioctl = ppsioctl,
|
|
|
|
.d_name = PPS_NAME,
|
1999-05-30 16:53:49 +00:00
|
|
|
};
|
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)
|
|
|
|
{
|
|
|
|
|
2004-03-18 21:10:11 +00:00
|
|
|
device_t dev;
|
|
|
|
|
|
|
|
dev = device_find_child(parent, PPS_NAME, 0);
|
|
|
|
if (!dev)
|
|
|
|
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);
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *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
|
|
|
|
2005-03-11 07:03:46 +00:00
|
|
|
mtx_init(&sc->mtx, device_get_nameunit(dev), "pps", MTX_SPIN);
|
2001-12-19 19:37:31 +00:00
|
|
|
/* 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
|
2004-06-16 09:47:26 +00:00
|
|
|
ppsopen(struct cdev *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 */
|
2005-03-11 07:03:46 +00:00
|
|
|
if ((error = bus_setup_intr(ppsdev, sc->intr_resource,
|
|
|
|
(INTR_TYPE_TTY | INTR_MPSAFE | INTR_FAST), ppsintr,
|
2005-04-14 15:56:10 +00:00
|
|
|
sc, &sc->intr_cookie))) {
|
2000-01-14 00:18:06 +00:00
|
|
|
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);
|
2005-03-10 22:21:16 +00:00
|
|
|
}
|
2001-09-22 16:34:59 +00:00
|
|
|
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
|
2004-06-16 09:47:26 +00:00
|
|
|
ppsclose(struct cdev *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);
|
2005-03-10 22:21:16 +00:00
|
|
|
if (subdev > 0 && !(sc->busy & ~1))
|
2001-09-22 16:34:59 +00:00
|
|
|
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;
|
2005-03-17 20:45:24 +00:00
|
|
|
mtx_lock_spin(&sc->mtx);
|
2001-09-22 16:34:59 +00:00
|
|
|
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;
|
2005-03-17 20:45:24 +00:00
|
|
|
mtx_unlock_spin(&sc->mtx);
|
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
|
|
|
{
|
2005-04-14 15:56:10 +00:00
|
|
|
struct pps_data *sc = (struct pps_data *)arg;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2002-04-26 20:24:28 +00:00
|
|
|
pps_capture(&sc->pps[0]);
|
2005-04-14 15:56:10 +00:00
|
|
|
if (!(ppb_rstr(sc->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)
|
2005-04-14 15:56:10 +00:00
|
|
|
ppb_wctr(sc->ppbus, IRQENABLE | AUTOFEED);
|
|
|
|
mtx_lock_spin(&sc->mtx);
|
2002-04-26 20:24:28 +00:00
|
|
|
pps_event(&sc->pps[0], PPS_CAPTUREASSERT);
|
2005-03-17 20:45:24 +00:00
|
|
|
mtx_unlock_spin(&sc->mtx);
|
2005-04-14 15:56:10 +00:00
|
|
|
if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
|
|
|
|
ppb_wctr(sc->ppbus, IRQENABLE);
|
1998-02-13 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
1998-06-07 19:44:22 +00:00
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
ppsioctl(struct cdev *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;
|
2005-03-11 07:03:46 +00:00
|
|
|
int err;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
2005-03-17 20:45:24 +00:00
|
|
|
mtx_lock_spin(&sc->mtx);
|
2005-03-11 07:03:46 +00:00
|
|
|
err = pps_ioctl(cmd, data, &sc->pps[subdev]);
|
2005-03-17 20:45:24 +00:00
|
|
|
mtx_unlock_spin(&sc->mtx);
|
2005-03-11 07:03:46 +00:00
|
|
|
return (err);
|
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);
|
2005-12-21 10:54:47 +00:00
|
|
|
MODULE_DEPEND(pps, ppbus, 1, 1, 1);
|