Home | History | Annotate | Line # | Download | only in ppbus
pps_ppbus.c revision 1.13.2.1
      1  1.13.2.1      yamt /* $NetBSD: pps_ppbus.c,v 1.13.2.1 2009/05/04 08:13:15 yamt Exp $ */
      2       1.1  drochner 
      3       1.1  drochner /*
      4       1.7    kardel  * ported to timecounters by Frank Kardel 2006
      5       1.7    kardel  *
      6       1.1  drochner  * Copyright (c) 2004
      7       1.1  drochner  * 	Matthias Drochner.  All rights reserved.
      8       1.1  drochner  *
      9       1.1  drochner  * Redistribution and use in source and binary forms, with or without
     10       1.1  drochner  * modification, are permitted provided that the following conditions
     11       1.1  drochner  * are met:
     12       1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     13       1.1  drochner  *    notice, this list of conditions, and the following disclaimer.
     14       1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     16       1.1  drochner  *    documentation and/or other materials provided with the distribution.
     17       1.1  drochner  *
     18       1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19       1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20       1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21       1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22       1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23       1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24       1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25       1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26       1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27       1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28       1.1  drochner  * SUCH DAMAGE.
     29       1.1  drochner  */
     30       1.1  drochner 
     31       1.1  drochner #include <sys/cdefs.h>
     32  1.13.2.1      yamt __KERNEL_RCSID(0, "$NetBSD: pps_ppbus.c,v 1.13.2.1 2009/05/04 08:13:15 yamt Exp $");
     33       1.1  drochner 
     34       1.1  drochner #include "opt_ntp.h"
     35       1.1  drochner 
     36       1.1  drochner #include <sys/param.h>
     37       1.1  drochner #include <sys/systm.h>
     38       1.1  drochner #include <sys/conf.h>
     39       1.1  drochner #include <sys/proc.h>
     40       1.1  drochner #include <sys/ioctl.h>
     41       1.1  drochner #include <sys/timepps.h>
     42       1.1  drochner 
     43       1.1  drochner #include <dev/ppbus/ppbus_base.h>
     44       1.1  drochner #include <dev/ppbus/ppbus_device.h>
     45       1.1  drochner #include <dev/ppbus/ppbus_io.h>
     46       1.1  drochner #include <dev/ppbus/ppbus_var.h>
     47       1.1  drochner 
     48       1.1  drochner struct pps_softc {
     49       1.2     perry 	struct ppbus_device_softc pps_dev;
     50      1.12    cegger 	device_t ppbus;
     51       1.1  drochner 	int busy;
     52       1.7    kardel 	struct pps_state pps_state;	/* pps state */
     53       1.1  drochner };
     54       1.1  drochner 
     55      1.12    cegger static int pps_probe(device_t, cfdata_t, void *);
     56      1.12    cegger static void pps_attach(device_t, device_t, void *);
     57      1.11    cegger CFATTACH_DECL_NEW(pps, sizeof(struct pps_softc), pps_probe, pps_attach,
     58       1.1  drochner 	NULL, NULL);
     59       1.1  drochner extern struct cfdriver pps_cd;
     60       1.1  drochner 
     61       1.1  drochner static dev_type_open(ppsopen);
     62       1.1  drochner static dev_type_close(ppsclose);
     63       1.1  drochner static dev_type_ioctl(ppsioctl);
     64       1.1  drochner const struct cdevsw pps_cdevsw = {
     65       1.1  drochner 	ppsopen, ppsclose, noread, nowrite, ppsioctl,
     66       1.8  drochner 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
     67       1.1  drochner };
     68       1.1  drochner 
     69       1.1  drochner static void ppsintr(void *arg);
     70       1.1  drochner 
     71       1.1  drochner static int
     72      1.12    cegger pps_probe(device_t parent, cfdata_t match, void *aux)
     73       1.1  drochner {
     74       1.1  drochner 	struct ppbus_attach_args *args = aux;
     75       1.1  drochner 
     76       1.2     perry 	/* we need an interrupt */
     77       1.1  drochner 	if (!(args->capabilities & PPBUS_HAS_INTR))
     78       1.1  drochner 		return 0;
     79       1.1  drochner 
     80       1.1  drochner 	return 1;
     81       1.1  drochner }
     82       1.1  drochner 
     83       1.1  drochner static void
     84      1.12    cegger pps_attach(device_t parent, device_t self, void *aux)
     85       1.1  drochner {
     86       1.5   thorpej 	struct pps_softc *sc = device_private(self);
     87       1.1  drochner 
     88       1.1  drochner 	sc->ppbus = parent;
     89      1.12    cegger 	sc->pps_dev.sc_dev = self;
     90       1.1  drochner 
     91       1.1  drochner 	printf("\n");
     92       1.1  drochner }
     93       1.1  drochner 
     94       1.1  drochner static int
     95       1.4    rpaulo ppsopen(dev_t dev, int flags, int fmt, struct lwp *l)
     96       1.1  drochner {
     97       1.1  drochner 	struct pps_softc *sc;
     98       1.6  drochner 	int res, weg = 0;
     99       1.1  drochner 
    100  1.13.2.1      yamt 	sc = device_lookup_private(&pps_cd, minor(dev));
    101       1.1  drochner 	if (!sc)
    102       1.1  drochner 		return (ENXIO);
    103       1.1  drochner 
    104       1.1  drochner 	if (sc->busy)
    105       1.1  drochner 		return (0);
    106       1.1  drochner 
    107      1.11    cegger 	if (ppbus_request_bus(sc->ppbus, sc->pps_dev.sc_dev,
    108       1.1  drochner 			      PPBUS_WAIT|PPBUS_INTR, 0))
    109       1.1  drochner 		return (EINTR);
    110       1.1  drochner 
    111       1.6  drochner 	ppbus_write_ivar(sc->ppbus, PPBUS_IVAR_IEEE, &weg);
    112       1.6  drochner 
    113       1.1  drochner 	/* attach the interrupt handler */
    114       1.1  drochner 	/* XXX priority should be set here */
    115       1.1  drochner 	res = ppbus_add_handler(sc->ppbus, ppsintr, sc);
    116       1.1  drochner 	if (res) {
    117      1.11    cegger 		ppbus_release_bus(sc->ppbus, sc->pps_dev.sc_dev,
    118       1.1  drochner 				  PPBUS_WAIT, 0);
    119       1.1  drochner 		return (res);
    120       1.1  drochner 	}
    121       1.1  drochner 
    122       1.1  drochner 	ppbus_set_mode(sc->ppbus, PPBUS_PS2, 0);
    123       1.1  drochner 	ppbus_wctr(sc->ppbus, IRQENABLE | PCD | nINIT | SELECTIN);
    124       1.1  drochner 
    125      1.13        ad 	mutex_spin_enter(&timecounter_lock);
    126       1.7    kardel 	memset((void *)&sc->pps_state, 0, sizeof(sc->pps_state));
    127       1.7    kardel 	sc->pps_state.ppscap = PPS_CAPTUREASSERT;
    128       1.7    kardel 	pps_init(&sc->pps_state);
    129      1.13        ad 	mutex_spin_exit(&timecounter_lock);
    130       1.7    kardel 
    131       1.1  drochner 	sc->busy = 1;
    132      1.13        ad 
    133       1.1  drochner 	return (0);
    134       1.1  drochner }
    135       1.1  drochner 
    136       1.1  drochner static int
    137       1.4    rpaulo ppsclose(dev_t dev, int flags, int fmt, struct lwp *l)
    138       1.1  drochner {
    139  1.13.2.1      yamt 	struct pps_softc *sc = device_lookup_private(&pps_cd, minor(dev));
    140      1.12    cegger 	device_t ppbus = sc->ppbus;
    141       1.1  drochner 
    142       1.7    kardel 	sc->busy = 0;
    143      1.13        ad 	mutex_spin_enter(&timecounter_lock);
    144       1.7    kardel 	sc->pps_state.ppsparam.mode = 0;
    145      1.13        ad 	mutex_spin_exit(&timecounter_lock);
    146       1.7    kardel 
    147       1.1  drochner 	ppbus_wdtr(ppbus, 0);
    148       1.1  drochner 	ppbus_wctr(ppbus, 0);
    149       1.1  drochner 
    150       1.1  drochner 	ppbus_remove_handler(ppbus, ppsintr);
    151       1.1  drochner 	ppbus_set_mode(ppbus, PPBUS_COMPATIBLE, 0);
    152      1.11    cegger 	ppbus_release_bus(ppbus, sc->pps_dev.sc_dev, PPBUS_WAIT, 0);
    153       1.1  drochner 	return (0);
    154       1.1  drochner }
    155       1.1  drochner 
    156       1.1  drochner static void
    157       1.1  drochner ppsintr(void *arg)
    158       1.1  drochner {
    159       1.1  drochner 	struct pps_softc *sc = arg;
    160      1.12    cegger 	device_t ppbus = sc->ppbus;
    161       1.1  drochner 
    162      1.13        ad 	mutex_spin_enter(&timecounter_lock);
    163       1.7    kardel 	pps_capture(&sc->pps_state);
    164      1.13        ad 	if (!(ppbus_rstr(ppbus) & nACK)) {
    165      1.13        ad 		mutex_spin_exit(&timecounter_lock);
    166       1.1  drochner 		return;
    167      1.13        ad 	}
    168       1.7    kardel 	if (sc->pps_state.ppsparam.mode & PPS_ECHOASSERT)
    169       1.7    kardel 		ppbus_wctr(ppbus, IRQENABLE | AUTOFEED);
    170       1.7    kardel 	pps_event(&sc->pps_state, PPS_CAPTUREASSERT);
    171       1.7    kardel 	if (sc->pps_state.ppsparam.mode & PPS_ECHOASSERT)
    172       1.7    kardel 		ppbus_wctr(ppbus, IRQENABLE);
    173      1.13        ad 	mutex_spin_exit(&timecounter_lock);
    174       1.1  drochner }
    175       1.1  drochner 
    176       1.1  drochner static int
    177       1.9  christos ppsioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    178       1.1  drochner {
    179  1.13.2.1      yamt 	struct pps_softc *sc = device_lookup_private(&pps_cd, minor(dev));
    180       1.1  drochner 	int error = 0;
    181       1.1  drochner 
    182       1.1  drochner 	switch (cmd) {
    183       1.7    kardel 	case PPS_IOC_CREATE:
    184       1.7    kardel 	case PPS_IOC_DESTROY:
    185       1.7    kardel 	case PPS_IOC_GETPARAMS:
    186       1.7    kardel 	case PPS_IOC_SETPARAMS:
    187       1.7    kardel 	case PPS_IOC_GETCAP:
    188       1.7    kardel 	case PPS_IOC_FETCH:
    189       1.7    kardel #ifdef PPS_SYNC
    190       1.7    kardel 	case PPS_IOC_KCBIND:
    191       1.7    kardel #endif
    192      1.13        ad 		mutex_spin_enter(&timecounter_lock);
    193       1.7    kardel 		error = pps_ioctl(cmd, data, &sc->pps_state);
    194      1.13        ad 		mutex_spin_exit(&timecounter_lock);
    195       1.7    kardel 		break;
    196       1.1  drochner 
    197       1.1  drochner 	default:
    198       1.1  drochner 		error = EPASSTHROUGH;
    199       1.1  drochner 		break;
    200       1.1  drochner 	}
    201       1.1  drochner 	return (error);
    202       1.1  drochner }
    203