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