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