1 /*- 2 * Copyright (c) 2003 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The names of the authors may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #ifdef _KERNEL 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/kernel.h> 37 #include <sys/kthread.h> 38 #include <sys/conf.h> 39 #include <sys/bus.h> 40 #include <machine/bus.h> 41 #include <machine/resource.h> 42 #include <sys/rman.h> 43 #include <pci/pcireg.h> 44 #include <pci/pcivar.h> 45 #include <pci_if.h> 46 #include <vm/vm.h> 47 #include <vm/pmap.h> 48 49 #endif /* _KERNEL */ 50 51 #include <sys/ioccom.h> 52 53 struct wave { 54 int index; 55 int period; 56 int offset; 57 int length; 58 int avg; 59 off_t mapvir; 60 int flags; 61 62 int npages; 63 void **virtual; 64 }; 65 66 #define ADLINK_SETWAVE _IOWR('A', 232, struct wave) 67 #define ADLINK_GETWAVE _IOWR('A', 233, struct wave) 68 69 #ifdef _KERNEL 70 71 #define INTPERPAGE (PAGE_SIZE / sizeof(int)) 72 #define I16PERPAGE (PAGE_SIZE / sizeof(int16_t)) 73 74 /* 75 * Sample rate 76 */ 77 #define SPS 1250000 78 79 /* 80 * We sample one channel (= 16 bits) at 1.25 msps giving 2.5Mbyte/sec, 81 * 100 pages will give us about 1/6 second buffering. 82 */ 83 #define NRING 100 84 85 /* 86 * How many waves are we willing to entertain 87 */ 88 #define NWAVE 25 89 90 struct info { 91 int nring; 92 off_t o_ring; 93 94 int ngri; 95 int ppgri; 96 off_t o_gri; 97 }; 98 99 struct softc { 100 device_t device; 101 void *intrhand; 102 struct resource *r0, *r1, *ri; 103 bus_space_tag_t t0, t1; 104 bus_space_handle_t h0, h1; 105 dev_t dev; 106 off_t mapvir; 107 108 struct proc *procp; 109 110 struct info *info; 111 112 struct wave *wave[NWAVE]; 113 114 int idx; 115 void *ring[NRING]; 116 vm_paddr_t pring[NRING]; 117 int stat[NRING]; 118 119 uint64_t cnt; 120 121 u_char flags[I16PERPAGE]; 122 }; 123 124 static void 125 adlink_wave(struct softc *sc, struct wave *wp, int16_t *sp) 126 { 127 int f, i, k, m, *ip; 128 129 f = 0; 130 for (i = 0; i < I16PERPAGE; ) { 131 k = (sc->cnt - wp->offset + i) % wp->period; 132 if (k >= wp->length) { 133 i += wp->period - k; 134 sp += wp->period - k; 135 continue; 136 } 137 m = k % INTPERPAGE; 138 ip = (int *)(wp->virtual[k / INTPERPAGE]) + m; 139 while (m < INTPERPAGE && i < I16PERPAGE && k < wp->length) { 140 if (sc->flags[i] >= wp->index) 141 *ip += (*sp * 8 - *ip) >> wp->avg; 142 if (wp->flags & 1) 143 sc->flags[i] = wp->index; 144 sp++; 145 ip++; 146 m++; 147 i++; 148 k++; 149 } 150 } 151 } 152 153 static void 154 adlink_tickle(struct softc *sc) 155 { 156 157 wakeup(sc); 158 tsleep(&sc->ring, PUSER | PCATCH, "tickle", 1); 159 } 160 161 static int 162 adlink_new_wave(struct softc *sc, int index, int period, int offset, int length, int avg, int flags) 163 { 164 struct wave *wp; 165 int l, i; 166 void **oldvir, **newvir; 167 168 if (index < 0 || index >= NWAVE) 169 return (EINVAL); 170 wp = sc->wave[index]; 171 if (wp == NULL) { 172 adlink_tickle(sc); 173 wp = malloc(sizeof *wp, M_DEVBUF, M_WAITOK | M_ZERO); 174 } 175 l = howmany(length, INTPERPAGE); 176 /* Setting a high average here to neuter the realtime bits */ 177 wp->avg = 31; 178 if (wp->npages < l) { 179 oldvir = wp->virtual; 180 adlink_tickle(sc); 181 newvir = malloc(sizeof(void *) * l, M_DEVBUF, M_WAITOK | M_ZERO); 182 if (wp->npages > 0) { 183 adlink_tickle(sc); 184 bcopy(oldvir, newvir, wp->npages * sizeof(void *)); 185 } 186 for (i = wp->npages; i < l; i++) { 187 adlink_tickle(sc); 188 newvir[i] = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK); 189 } 190 wp->virtual = newvir; 191 wp->npages = l; 192 wp->mapvir = sc->mapvir; 193 sc->mapvir += l * PAGE_SIZE; 194 } else { 195 oldvir = NULL; 196 } 197 wp->index = index; 198 wp->period = period; 199 wp->offset = offset; 200 wp->length = length; 201 wp->flags = flags; 202 203 for (i = 0; i < l; i++) { 204 adlink_tickle(sc); 205 bzero(wp->virtual[i], PAGE_SIZE); 206 } 207 wp->avg = avg; 208 sc->wave[index] = wp; 209 printf("Wave[%d] {period %d, offset %d, length %d, avg %d, flags %x}\n", 210 wp->index, wp->period, wp->offset, wp->length, wp->avg, wp->flags); 211 free(oldvir, M_DEVBUF); 212 return (0); 213 } 214 215 static void 216 adlink_loran(void *arg) 217 { 218 struct softc *sc; 219 int idx, i; 220 221 sc = arg; 222 idx = 0; 223 for (;;) { 224 while (sc->stat[idx] == 0) 225 msleep(sc, NULL, PRIBIO, "loran", 1); 226 memset(sc->flags, NWAVE, sizeof sc->flags); 227 for (i = 0; i < NWAVE; i++) { 228 if (sc->wave[i] != NULL) 229 adlink_wave(sc, sc->wave[i], sc->ring[idx]); 230 } 231 sc->cnt += I16PERPAGE; 232 sc->stat[idx] = 0; 233 idx++; 234 idx %= NRING; 235 } 236 kthread_exit(0); 237 } 238 239 static int 240 adlink_open(dev_t dev, int oflags, int devtype, struct thread *td) 241 { 242 static int once; 243 struct softc *sc; 244 int i, error; 245 uint32_t u; 246 247 if (once) 248 return (0); 249 once = 1; 250 251 sc = dev->si_drv1; 252 sc->info = malloc(PAGE_SIZE, M_DEVBUF, M_ZERO | M_WAITOK); 253 sc->info->nring = NRING; 254 255 sc->info->o_ring = PAGE_SIZE; 256 for (i = 0; i < NRING; i++) { 257 sc->ring[i] = malloc(PAGE_SIZE, M_DEVBUF, M_ZERO | M_WAITOK); 258 sc->pring[i] = vtophys(sc->ring[i]); 259 } 260 261 error = adlink_new_wave(sc, NWAVE - 1, SPS, 0, SPS, 7, 0); 262 if (error) 263 return (error); 264 265 error = kthread_create(adlink_loran, sc, &sc->procp, 266 0, 0, "adlink%d", device_get_unit(sc->device)); 267 if (error) 268 return (error); 269 270 /* Enable interrupts on write complete */ 271 bus_space_write_4(sc->t0, sc->h0, 0x38, 0x00004000); 272 273 /* Sample CH0 only */ 274 bus_space_write_4(sc->t1, sc->h1, 0x00, 1); 275 276 /* Divide clock by ten */ 277 bus_space_write_4(sc->t1, sc->h1, 0x04, 4); 278 279 /* Software trigger mode: software */ 280 bus_space_write_4(sc->t1, sc->h1, 0x08, 0); 281 282 /* Trigger level zero */ 283 bus_space_write_4(sc->t1, sc->h1, 0x0c, 0); 284 285 /* Trigger source CH0 (not used) */ 286 bus_space_write_4(sc->t1, sc->h1, 0x10, 0); 287 288 /* Fifo control/status: flush */ 289 bus_space_write_4(sc->t1, sc->h1, 0x18, 3); 290 291 /* Clock source: external sine */ 292 bus_space_write_4(sc->t1, sc->h1, 0x20, 2); 293 294 /* Set up Write DMA */ 295 bus_space_write_4(sc->t0, sc->h0, 0x24, sc->pring[i]); 296 bus_space_write_4(sc->t0, sc->h0, 0x28, PAGE_SIZE); 297 u = bus_space_read_4(sc->t0, sc->h0, 0x3c); 298 bus_space_write_4(sc->t0, sc->h0, 0x3c, u | 0x00000600); 299 300 /* Acquisition Enable Register: go! */ 301 bus_space_write_4(sc->t1, sc->h1, 0x1c, 1); 302 return (0); 303 } 304 305 static int 306 adlink_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 307 { 308 struct softc *sc; 309 struct wave *wp; 310 int i, error; 311 312 sc = dev->si_drv1; 313 wp = (struct wave *)data; 314 i = wp->index; 315 if (i < 0 || i >= NWAVE) 316 return (EINVAL); 317 if (cmd == ADLINK_GETWAVE) { 318 if (sc->wave[i] == NULL) 319 return (ENOENT); 320 bcopy(sc->wave[i], wp, sizeof(*wp)); 321 return (0); 322 } 323 if (cmd == ADLINK_SETWAVE) { 324 error = adlink_new_wave(sc, 325 i, 326 wp->period, 327 wp->offset, 328 wp->length, 329 wp->avg, 330 wp->flags); 331 if (error) 332 return (error); 333 bcopy(sc->wave[i], wp, sizeof(*wp)); 334 return (0); 335 } 336 return (ENOIOCTL); 337 } 338 339 static int 340 adlink_mmap(dev_t dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) 341 { 342 struct softc *sc; 343 struct wave *wp; 344 int i, j; 345 346 sc = dev->si_drv1; 347 if (nprot != VM_PROT_READ) 348 return (-1); 349 for (i = 0; i < NWAVE; i++) { 350 if (sc->wave[i] == NULL) 351 continue; 352 wp = sc->wave[i]; 353 if (offset < wp->mapvir) 354 continue; 355 j = (offset - wp->mapvir) / PAGE_SIZE; 356 if (j >= wp->npages) 357 continue; 358 *paddr = vtophys(wp->virtual[j]); 359 return (0); 360 } 361 return (-1); 362 } 363 364 static void 365 adlink_intr(void *arg) 366 { 367 struct softc *sc; 368 uint32_t u; 369 int i, j; 370 371 sc = arg; 372 u = bus_space_read_4(sc->t0, sc->h0, 0x38); 373 if (!(u & 0x00800000)) 374 return; 375 bus_space_write_4(sc->t0, sc->h0, 0x38, u | 0x003f4000); 376 377 j = sc->idx; 378 sc->stat[j] = 1; 379 i = (j + 1) % NRING; 380 sc->idx = i; 381 u = bus_space_read_4(sc->t1, sc->h1, 0x18); 382 if (u & 1) { 383 printf("adlink FIFO overrun\n"); 384 return; 385 } 386 bus_space_write_4(sc->t0, sc->h0, 0x24, sc->pring[i]); 387 bus_space_write_4(sc->t0, sc->h0, 0x28, PAGE_SIZE); 388 wakeup(sc); 389 if (sc->stat[i]) { 390 printf("adlink page busy\n"); 391 } 392 } 393 394 static struct cdevsw adlink_cdevsw = { 395 .d_open = adlink_open, 396 .d_close = nullclose, 397 .d_ioctl = adlink_ioctl, 398 .d_mmap = adlink_mmap, 399 .d_name = "adlink", 400 }; 401 402 static devclass_t adlink_devclass; 403 404 static int 405 adlink_probe(device_t self) 406 { 407 408 if (pci_get_devid(self) != 0x80da10e8) 409 return (ENXIO); 410 device_set_desc(self, "Adlink PCI-9812 4 ch 12 bit 20 msps"); 411 return (0); 412 } 413 414 static int 415 adlink_attach(device_t self) 416 { 417 struct softc *sc; 418 int rid, i; 419 420 sc = device_get_softc(self); 421 bzero(sc, sizeof *sc); 422 sc->device = self; 423 424 /* 425 * This is the PCI mapped registers of the AMCC 9535 "matchmaker" 426 * chip. 427 */ 428 rid = 0x10; 429 sc->r0 = bus_alloc_resource(self, SYS_RES_IOPORT, &rid, 430 0, ~0, 1, RF_ACTIVE); 431 if (sc->r0 == NULL) 432 return(ENODEV); 433 sc->t0 = rman_get_bustag(sc->r0); 434 sc->h0 = rman_get_bushandle(sc->r0); 435 printf("Res0 %x %x\n", sc->t0, sc->h0); 436 437 /* 438 * This is the PCI mapped registers of the ADC hardware, they 439 * are described in the manual which comes with the card. 440 */ 441 rid = 0x14; 442 sc->r1 = bus_alloc_resource(self, SYS_RES_IOPORT, &rid, 443 0, ~0, 1, RF_ACTIVE); 444 if (sc->r1 == NULL) 445 return(ENODEV); 446 sc->t1 = rman_get_bustag(sc->r1); 447 sc->h1 = rman_get_bushandle(sc->r1); 448 printf("Res1 %x %x\n", sc->t1, sc->h1); 449 450 rid = 0x0; 451 sc->ri = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 452 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 453 if (sc->ri == NULL) 454 return (ENODEV); 455 456 i = bus_setup_intr(self, sc->ri, INTR_MPSAFE | INTR_TYPE_MISC | INTR_FAST, 457 adlink_intr, sc, &sc->intrhand); 458 if (i) { 459 printf("adlink: Couldn't get FAST intr\n"); 460 i = bus_setup_intr(self, sc->ri, INTR_TYPE_MISC, 461 adlink_intr, sc, &sc->intrhand); 462 } 463 464 if (i) 465 return (ENODEV); 466 467 sc->dev = make_dev(&adlink_cdevsw, device_get_unit(self), 468 UID_ROOT, GID_WHEEL, 0444, "adlink%d", device_get_unit(self)); 469 sc->dev->si_drv1 = sc; 470 471 return (0); 472 } 473 474 static device_method_t adlink_methods[] = { 475 /* Device interface */ 476 DEVMETHOD(device_probe, adlink_probe), 477 DEVMETHOD(device_attach, adlink_attach), 478 DEVMETHOD(device_suspend, bus_generic_suspend), 479 DEVMETHOD(device_resume, bus_generic_resume), 480 DEVMETHOD(device_shutdown, bus_generic_shutdown), 481 {0, 0} 482 }; 483 484 static driver_t adlink_driver = { 485 "adlink", 486 adlink_methods, 487 sizeof(struct softc) 488 }; 489 490 DRIVER_MODULE(adlink, pci, adlink_driver, adlink_devclass, 0, 0); 491 #endif /* _KERNEL */ 492