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/timetc.h> 25 #include <sys/timepps.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 34 #define PPS_NAME "pps" /* our official name */ 35 36 #define PRVERBOSE(arg...) if (bootverbose) printf(##arg); 37 38 struct pps_data { 39 struct ppb_device pps_dev; 40 struct pps_state pps[9]; 41 dev_t devs[9]; 42 device_t ppsdev; 43 device_t ppbus; 44 int busy; 45 struct callout_handle timeout; 46 int lastdata; 47 48 struct resource *intr_resource; /* interrupt resource */ 49 void *intr_cookie; /* interrupt registration cookie */ 50 }; 51 52 static void ppsintr(void *arg); 53 static void ppshcpoll(void *arg); 54 55 #define DEVTOSOFTC(dev) \ 56 ((struct pps_data *)device_get_softc(dev)) 57 58 static devclass_t pps_devclass; 59 60 static d_open_t ppsopen; 61 static d_close_t ppsclose; 62 static d_ioctl_t ppsioctl; 63 64 #define CDEV_MAJOR 89 65 static struct cdevsw pps_cdevsw = { 66 /* open */ ppsopen, 67 /* close */ ppsclose, 68 /* read */ noread, 69 /* write */ nowrite, 70 /* ioctl */ ppsioctl, 71 /* poll */ nopoll, 72 /* mmap */ nommap, 73 /* strategy */ nostrategy, 74 /* name */ PPS_NAME, 75 /* maj */ CDEV_MAJOR, 76 /* dump */ nodump, 77 /* psize */ nopsize, 78 /* flags */ 0, 79 }; 80 81 static void 82 ppsidentify(driver_t *driver, device_t parent) 83 { 84 85 BUS_ADD_CHILD(parent, 0, PPS_NAME, 0); 86 } 87 88 static int 89 ppstry(device_t ppbus, int send, int expect) 90 { 91 int i; 92 93 ppb_wdtr(ppbus, send); 94 i = ppb_rdtr(ppbus); 95 PRVERBOSE("S: %02x E: %02x G: %02x\n", send, expect, i); 96 return (i != expect); 97 } 98 99 static int 100 ppsprobe(device_t ppsdev) 101 { 102 device_set_desc(ppsdev, "Pulse per second Timing Interface"); 103 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 dev_t d; 114 int unit, i; 115 116 bzero(sc, sizeof(struct pps_data)); /* XXX doesn't newbus do this? */ 117 118 /* retrieve the ppbus irq */ 119 BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq); 120 121 if (irq > 0) { 122 /* declare our interrupt handler */ 123 sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ, 124 &zero, irq, irq, 1, RF_SHAREABLE); 125 } 126 /* interrupts seem mandatory */ 127 if (sc->intr_resource == NULL) 128 return (ENXIO); 129 130 sc->ppsdev = dev; 131 sc->ppbus = ppbus; 132 unit = device_get_unit(ppbus); 133 d = make_dev(&pps_cdevsw, unit, 134 UID_ROOT, GID_WHEEL, 0644, PPS_NAME "%d", unit); 135 sc->devs[0] = d; 136 sc->pps[0].ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT; 137 d->si_drv1 = sc; 138 d->si_drv2 = (void*)0; 139 pps_init(&sc->pps[0]); 140 141 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) 142 return (0); 143 144 do { 145 i = ppb_set_mode(sc->ppbus, PPB_EPP); 146 PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus)); 147 if (i == -1) 148 break; 149 i = 0; 150 ppb_wctr(ppbus, i); 151 if (ppstry(ppbus, 0x00, 0x00)) 152 break; 153 if (ppstry(ppbus, 0x55, 0x55)) 154 break; 155 if (ppstry(ppbus, 0xaa, 0xaa)) 156 break; 157 if (ppstry(ppbus, 0xff, 0xff)) 158 break; 159 160 i = IRQENABLE | PCD | STROBE | nINIT | SELECTIN; 161 ppb_wctr(ppbus, i); 162 PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i); 163 if (ppstry(ppbus, 0x00, 0x00)) 164 break; 165 if (ppstry(ppbus, 0x55, 0x00)) 166 break; 167 if (ppstry(ppbus, 0xaa, 0x00)) 168 break; 169 if (ppstry(ppbus, 0xff, 0x00)) 170 break; 171 172 i = IRQENABLE | PCD | nINIT | SELECTIN; 173 ppb_wctr(ppbus, i); 174 PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i); 175 ppstry(ppbus, 0x00, 0xff); 176 ppstry(ppbus, 0x55, 0xff); 177 ppstry(ppbus, 0xaa, 0xff); 178 ppstry(ppbus, 0xff, 0xff); 179 180 for (i = 1; i < 9; i++) { 181 d = make_dev(&pps_cdevsw, unit + 0x10000 * i, 182 UID_ROOT, GID_WHEEL, 0644, PPS_NAME "%db%d", unit, i - 1); 183 sc->devs[i] = d; 184 sc->pps[i].ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 185 d->si_drv1 = sc; 186 d->si_drv2 = (void*)i; 187 pps_init(&sc->pps[i]); 188 } 189 } while (0); 190 i = ppb_set_mode(sc->ppbus, PPB_COMPATIBLE); 191 ppb_release_bus(ppbus, dev); 192 193 return (0); 194 } 195 196 static int 197 ppsopen(dev_t dev, int flags, int fmt, struct thread *td) 198 { 199 struct pps_data *sc = dev->si_drv1; 200 int subdev = (int)dev->si_drv2; 201 int error, i; 202 203 if (!sc->busy) { 204 device_t ppsdev = sc->ppsdev; 205 device_t ppbus = sc->ppbus; 206 207 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR)) 208 return (EINTR); 209 210 /* attach the interrupt handler */ 211 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource, 212 INTR_TYPE_TTY, ppsintr, ppsdev, 213 &sc->intr_cookie))) { 214 ppb_release_bus(ppbus, ppsdev); 215 return (error); 216 } 217 218 i = ppb_set_mode(sc->ppbus, PPB_PS2); 219 PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus)); 220 221 i = IRQENABLE | PCD | nINIT | SELECTIN; 222 ppb_wctr(ppbus, i); 223 } 224 if (subdev > 0 && !(sc->busy & ~1)) { 225 sc->timeout = timeout(ppshcpoll, sc, 1); 226 sc->lastdata = ppb_rdtr(sc->ppbus); 227 } 228 sc->busy |= (1 << subdev); 229 return(0); 230 } 231 232 static int 233 ppsclose(dev_t dev, int flags, int fmt, struct thread *td) 234 { 235 struct pps_data *sc = dev->si_drv1; 236 int subdev = (int)dev->si_drv2; 237 238 sc->pps[subdev].ppsparam.mode = 0; /* PHK ??? */ 239 sc->busy &= ~(1 << subdev); 240 if (subdev > 0 && !(sc->busy & ~1)) 241 untimeout(ppshcpoll, sc, sc->timeout); 242 if (!sc->busy) { 243 device_t ppsdev = sc->ppsdev; 244 device_t ppbus = sc->ppbus; 245 246 ppb_wdtr(ppbus, 0); 247 ppb_wctr(ppbus, 0); 248 249 /* Note: the interrupt handler is automatically detached */ 250 ppb_set_mode(ppbus, PPB_COMPATIBLE); 251 ppb_release_bus(ppbus, ppsdev); 252 } 253 return(0); 254 } 255 256 static void 257 ppshcpoll(void *arg) 258 { 259 struct pps_data *sc = arg; 260 int i, j, k, l; 261 struct timecounter *tc; 262 unsigned count; 263 264 if (!(sc->busy & ~1)) 265 return; 266 sc->timeout = timeout(ppshcpoll, sc, 1); 267 i = ppb_rdtr(sc->ppbus); 268 if (i == sc->lastdata) 269 return; 270 tc = timecounter; 271 count = timecounter->tc_get_timecount(tc); 272 l = sc->lastdata ^ i; 273 k = 1; 274 for (j = 1; j < 9; j ++) { 275 if (l & k) 276 pps_event(&sc->pps[j], tc, count, 277 i & k ? 278 PPS_CAPTUREASSERT : PPS_CAPTURECLEAR 279 ); 280 k += k; 281 } 282 sc->lastdata = i; 283 } 284 285 static void 286 ppsintr(void *arg) 287 { 288 device_t ppsdev = (device_t)arg; 289 struct pps_data *sc = DEVTOSOFTC(ppsdev); 290 device_t ppbus = sc->ppbus; 291 struct timecounter *tc; 292 unsigned count; 293 294 tc = timecounter; 295 count = timecounter->tc_get_timecount(tc); 296 if (!(ppb_rstr(ppbus) & nACK)) 297 return; 298 if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT) 299 ppb_wctr(ppbus, IRQENABLE | AUTOFEED); 300 pps_event(&sc->pps[0], tc, count, PPS_CAPTUREASSERT); 301 if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT) 302 ppb_wctr(ppbus, IRQENABLE); 303 } 304 305 static int 306 ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) 307 { 308 struct pps_data *sc = dev->si_drv1; 309 int subdev = (int)dev->si_drv2; 310 311 return (pps_ioctl(cmd, data, &sc->pps[subdev])); 312 } 313 314 static device_method_t pps_methods[] = { 315 /* device interface */ 316 DEVMETHOD(device_identify, ppsidentify), 317 DEVMETHOD(device_probe, ppsprobe), 318 DEVMETHOD(device_attach, ppsattach), 319 320 { 0, 0 } 321 }; 322 323 static driver_t pps_driver = { 324 PPS_NAME, 325 pps_methods, 326 sizeof(struct pps_data), 327 }; 328 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0); 329