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.14 2005/10/28 03:25:50 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/module.h> 25 #include <sys/bus.h> 26 #include <sys/conf.h> 27 #include <sys/timepps.h> 28 #include <machine/bus.h> 29 #include <machine/resource.h> 30 #include <sys/rman.h> 31 32 #include <bus/ppbus/ppbconf.h> 33 #include "ppbus_if.h" 34 #include <bus/ppbus/ppbio.h> 35 36 #define PPS_NAME "pps" /* our official name */ 37 38 struct pps_data { 39 int pps_open; 40 struct ppb_device pps_dev; 41 struct pps_state pps; 42 43 struct resource *intr_resource; /* interrupt resource */ 44 void *intr_cookie; /* interrupt registration cookie */ 45 }; 46 47 static void ppsintr(void *arg); 48 49 #define DEVTOSOFTC(dev) \ 50 ((struct pps_data *)device_get_softc(dev)) 51 #define UNITOSOFTC(unit) \ 52 ((struct pps_data *)devclass_get_softc(pps_devclass, (unit))) 53 #define UNITODEVICE(unit) \ 54 (devclass_get_device(pps_devclass, (unit))) 55 56 static devclass_t pps_devclass; 57 58 static d_open_t ppsopen; 59 static d_close_t ppsclose; 60 static d_ioctl_t ppsioctl; 61 62 #define CDEV_MAJOR 89 63 static struct cdevsw pps_cdevsw = { 64 /* name */ PPS_NAME, 65 /* maj */ CDEV_MAJOR, 66 /* flags */ 0, 67 /* port */ NULL, 68 /* clone */ NULL, 69 70 /* open */ ppsopen, 71 /* close */ ppsclose, 72 /* read */ noread, 73 /* write */ nowrite, 74 /* ioctl */ ppsioctl, 75 /* poll */ nopoll, 76 /* mmap */ nommap, 77 /* strategy */ nostrategy, 78 /* dump */ nodump, 79 /* psize */ nopsize 80 }; 81 82 static int 83 ppsprobe(device_t ppsdev) 84 { 85 struct pps_data *sc; 86 87 sc = DEVTOSOFTC(ppsdev); 88 bzero(sc, sizeof(struct pps_data)); 89 90 device_set_desc(ppsdev, "Pulse per second Timing Interface"); 91 92 sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT; 93 pps_init(&sc->pps); 94 return (0); 95 } 96 97 static int 98 ppsattach(device_t ppsdev) 99 { 100 struct pps_data *sc = DEVTOSOFTC(ppsdev); 101 device_t ppbus = device_get_parent(ppsdev); 102 int irq; 103 int unit; 104 int zero = 0; 105 106 /* retrieve the ppbus irq */ 107 BUS_READ_IVAR(ppbus, ppsdev, PPBUS_IVAR_IRQ, &irq); 108 109 if (irq > 0) { 110 /* declare our interrupt handler */ 111 sc->intr_resource = bus_alloc_resource(ppsdev, SYS_RES_IRQ, 112 &zero, irq, irq, 1, RF_SHAREABLE); 113 } 114 /* interrupts seem mandatory */ 115 if (sc->intr_resource == 0) 116 return (ENXIO); 117 118 unit = device_get_unit(ppsdev); 119 cdevsw_add(&pps_cdevsw, -1, unit); 120 make_dev(&pps_cdevsw, unit, UID_ROOT, GID_WHEEL, 0644, 121 PPS_NAME "%d", unit); 122 return (0); 123 } 124 125 static int 126 ppsopen(dev_t dev, int flags, int fmt, struct thread *td) 127 { 128 u_int unit = minor(dev); 129 struct pps_data *sc = UNITOSOFTC(unit); 130 device_t ppsdev = UNITODEVICE(unit); 131 device_t ppbus = device_get_parent(ppsdev); 132 int error; 133 134 if (!sc->pps_open) { 135 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR)) 136 return (EINTR); 137 138 /* attach the interrupt handler */ 139 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource, 140 0, ppsintr, ppsdev, 141 &sc->intr_cookie, NULL))) { 142 ppb_release_bus(ppbus, ppsdev); 143 return (error); 144 } 145 146 ppb_wctr(ppbus, 0); 147 ppb_wctr(ppbus, IRQENABLE); 148 sc->pps_open = 1; 149 } 150 151 return(0); 152 } 153 154 static int 155 ppsclose(dev_t dev, int flags, int fmt, struct thread *td) 156 { 157 u_int unit = minor(dev); 158 struct pps_data *sc = UNITOSOFTC(unit); 159 device_t ppsdev = UNITODEVICE(unit); 160 device_t ppbus = device_get_parent(ppsdev); 161 162 sc->pps.ppsparam.mode = 0; /* PHK ??? */ 163 164 ppb_wdtr(ppbus, 0); 165 ppb_wctr(ppbus, 0); 166 167 /* Note: the interrupt handler is automatically detached */ 168 ppb_release_bus(ppbus, ppsdev); 169 sc->pps_open = 0; 170 return(0); 171 } 172 173 static void 174 ppsintr(void *arg) 175 { 176 device_t ppsdev = (device_t)arg; 177 device_t ppbus = device_get_parent(ppsdev); 178 struct pps_data *sc = DEVTOSOFTC(ppsdev); 179 sysclock_t count; 180 181 count = sys_cputimer->count(); 182 if (!(ppb_rstr(ppbus) & nACK)) 183 return; 184 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT) 185 ppb_wctr(ppbus, IRQENABLE | AUTOFEED); 186 pps_event(&sc->pps, count, PPS_CAPTUREASSERT); 187 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT) 188 ppb_wctr(ppbus, IRQENABLE); 189 } 190 191 static int 192 ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) 193 { 194 u_int unit = minor(dev); 195 struct pps_data *sc = UNITOSOFTC(unit); 196 197 return (pps_ioctl(cmd, data, &sc->pps)); 198 } 199 200 /* 201 * Becuase pps is a static device under any attached ppbus, and not scanned 202 * by the ppbus, we need an identify function to create the device. 203 */ 204 static device_method_t pps_methods[] = { 205 /* device interface */ 206 DEVMETHOD(device_identify, bus_generic_identify), 207 DEVMETHOD(device_probe, ppsprobe), 208 DEVMETHOD(device_attach, ppsattach), 209 210 { 0, 0 } 211 }; 212 213 static driver_t pps_driver = { 214 PPS_NAME, 215 pps_methods, 216 sizeof(struct pps_data), 217 }; 218 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0); 219