1 /* 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 * ---------------------------------------------------------------------------- 8 * 9 * $FreeBSD: src/sys/dev/ppbus/pps.c,v 1.24.2.1 2000/05/24 00:20:57 n_hibma Exp $ 10 * $DragonFly: src/sys/dev/misc/pps/pps.c,v 1.15 2006/07/28 02:17:36 dillon Exp $ 11 * 12 * This driver implements a draft-mogul-pps-api-02.txt PPS source. 13 * 14 * The input pin is pin#10 15 * The echo output pin is pin#14 16 * 17 */ 18 19 #include "use_pps.h" 20 21 #include <sys/param.h> 22 #include <sys/kernel.h> 23 #include <sys/systm.h> 24 #include <sys/conf.h> 25 #include <sys/device.h> 26 #include <sys/module.h> 27 #include <sys/bus.h> 28 #include <sys/timepps.h> 29 #include <machine/bus.h> 30 #include <machine/resource.h> 31 #include <sys/rman.h> 32 33 #include <bus/ppbus/ppbconf.h> 34 #include "ppbus_if.h" 35 #include <bus/ppbus/ppbio.h> 36 37 #define PPS_NAME "pps" /* our official name */ 38 39 struct pps_data { 40 int pps_open; 41 struct ppb_device pps_dev; 42 struct pps_state pps; 43 44 struct resource *intr_resource; /* interrupt resource */ 45 void *intr_cookie; /* interrupt registration cookie */ 46 }; 47 48 static void ppsintr(void *arg); 49 50 #define DEVTOSOFTC(dev) \ 51 ((struct pps_data *)device_get_softc(dev)) 52 #define UNITOSOFTC(unit) \ 53 ((struct pps_data *)devclass_get_softc(pps_devclass, (unit))) 54 #define UNITODEVICE(unit) \ 55 (devclass_get_device(pps_devclass, (unit))) 56 57 static devclass_t pps_devclass; 58 59 static d_open_t ppsopen; 60 static d_close_t ppsclose; 61 static d_ioctl_t ppsioctl; 62 63 #define CDEV_MAJOR 89 64 static struct dev_ops pps_ops = { 65 { PPS_NAME, CDEV_MAJOR, 0 }, 66 .d_open = ppsopen, 67 .d_close = ppsclose, 68 .d_ioctl = ppsioctl, 69 }; 70 71 static int 72 ppsprobe(device_t ppsdev) 73 { 74 struct pps_data *sc; 75 76 sc = DEVTOSOFTC(ppsdev); 77 bzero(sc, sizeof(struct pps_data)); 78 79 device_set_desc(ppsdev, "Pulse per second Timing Interface"); 80 81 sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT; 82 pps_init(&sc->pps); 83 return (0); 84 } 85 86 static int 87 ppsattach(device_t ppsdev) 88 { 89 struct pps_data *sc = DEVTOSOFTC(ppsdev); 90 device_t ppbus = device_get_parent(ppsdev); 91 int irq; 92 int unit; 93 int zero = 0; 94 95 /* retrieve the ppbus irq */ 96 BUS_READ_IVAR(ppbus, ppsdev, PPBUS_IVAR_IRQ, &irq); 97 98 if (irq > 0) { 99 /* declare our interrupt handler */ 100 sc->intr_resource = bus_alloc_resource(ppsdev, SYS_RES_IRQ, 101 &zero, irq, irq, 1, RF_SHAREABLE); 102 } 103 /* interrupts seem mandatory */ 104 if (sc->intr_resource == 0) 105 return (ENXIO); 106 107 unit = device_get_unit(ppsdev); 108 dev_ops_add(&pps_ops, -1, unit); 109 make_dev(&pps_ops, unit, UID_ROOT, GID_WHEEL, 0644, 110 PPS_NAME "%d", unit); 111 return (0); 112 } 113 114 static int 115 ppsopen(struct dev_open_args *ap) 116 { 117 dev_t dev = ap->a_head.a_dev; 118 u_int unit = minor(dev); 119 struct pps_data *sc = UNITOSOFTC(unit); 120 device_t ppsdev = UNITODEVICE(unit); 121 device_t ppbus = device_get_parent(ppsdev); 122 int error; 123 124 if (!sc->pps_open) { 125 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR)) 126 return (EINTR); 127 128 /* attach the interrupt handler */ 129 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource, 130 0, ppsintr, ppsdev, 131 &sc->intr_cookie, NULL))) { 132 ppb_release_bus(ppbus, ppsdev); 133 return (error); 134 } 135 136 ppb_wctr(ppbus, 0); 137 ppb_wctr(ppbus, IRQENABLE); 138 sc->pps_open = 1; 139 } 140 141 return(0); 142 } 143 144 static int 145 ppsclose(struct dev_close_args *ap) 146 { 147 dev_t dev = ap->a_head.a_dev; 148 u_int unit = minor(dev); 149 struct pps_data *sc = UNITOSOFTC(unit); 150 device_t ppsdev = UNITODEVICE(unit); 151 device_t ppbus = device_get_parent(ppsdev); 152 153 sc->pps.ppsparam.mode = 0; /* PHK ??? */ 154 155 ppb_wdtr(ppbus, 0); 156 ppb_wctr(ppbus, 0); 157 158 /* Note: the interrupt handler is automatically detached */ 159 ppb_release_bus(ppbus, ppsdev); 160 sc->pps_open = 0; 161 return(0); 162 } 163 164 static void 165 ppsintr(void *arg) 166 { 167 device_t ppsdev = (device_t)arg; 168 device_t ppbus = device_get_parent(ppsdev); 169 struct pps_data *sc = DEVTOSOFTC(ppsdev); 170 sysclock_t count; 171 172 count = sys_cputimer->count(); 173 if (!(ppb_rstr(ppbus) & nACK)) 174 return; 175 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT) 176 ppb_wctr(ppbus, IRQENABLE | AUTOFEED); 177 pps_event(&sc->pps, count, PPS_CAPTUREASSERT); 178 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT) 179 ppb_wctr(ppbus, IRQENABLE); 180 } 181 182 static int 183 ppsioctl(struct dev_ioctl_args *ap) 184 { 185 dev_t dev = ap->a_head.a_dev; 186 u_int unit = minor(dev); 187 struct pps_data *sc = UNITOSOFTC(unit); 188 189 return (pps_ioctl(ap->a_cmd, ap->a_data, &sc->pps)); 190 } 191 192 /* 193 * Becuase pps is a static device under any attached ppbus, and not scanned 194 * by the ppbus, we need an identify function to create the device. 195 */ 196 static device_method_t pps_methods[] = { 197 /* device interface */ 198 DEVMETHOD(device_identify, bus_generic_identify), 199 DEVMETHOD(device_probe, ppsprobe), 200 DEVMETHOD(device_attach, ppsattach), 201 202 { 0, 0 } 203 }; 204 205 static driver_t pps_driver = { 206 PPS_NAME, 207 pps_methods, 208 sizeof(struct pps_data), 209 }; 210 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0); 211