adlink.c revision 119274
1113094Sphk/*- 2113094Sphk * Copyright (c) 2003 Poul-Henning Kamp 3113094Sphk * All rights reserved. 4113094Sphk * 5113094Sphk * Redistribution and use in source and binary forms, with or without 6113094Sphk * modification, are permitted provided that the following conditions 7113094Sphk * are met: 8113094Sphk * 1. Redistributions of source code must retain the above copyright 9113094Sphk * notice, this list of conditions and the following disclaimer. 10113094Sphk * 2. Redistributions in binary form must reproduce the above copyright 11113094Sphk * notice, this list of conditions and the following disclaimer in the 12113094Sphk * documentation and/or other materials provided with the distribution. 13113094Sphk * 3. The names of the authors may not be used to endorse or promote 14113094Sphk * products derived from this software without specific prior written 15113094Sphk * permission. 16113094Sphk * 17113094Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18113094Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19113094Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20113094Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21113094Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22113094Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23113094Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24113094Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25113094Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26113094Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27113094Sphk * SUCH DAMAGE. 28113094Sphk * 29113094Sphk * $FreeBSD: head/sys/dev/adlink/adlink.c 119274 2003-08-22 05:11:30Z imp $ 30113094Sphk */ 31113094Sphk 32113270Sphk#ifdef _KERNEL 33113094Sphk#include <sys/param.h> 34113094Sphk#include <sys/systm.h> 35113094Sphk#include <sys/malloc.h> 36113094Sphk#include <sys/kernel.h> 37113270Sphk#include <sys/kthread.h> 38113094Sphk#include <sys/conf.h> 39113094Sphk#include <sys/bus.h> 40113094Sphk#include <machine/bus.h> 41113094Sphk#include <machine/resource.h> 42113094Sphk#include <sys/rman.h> 43119274Simp#include <dev/pci/pcireg.h> 44119274Simp#include <dev/pci/pcivar.h> 45113094Sphk#include <pci_if.h> 46113094Sphk#include <vm/vm.h> 47113094Sphk#include <vm/pmap.h> 48113094Sphk 49113270Sphk#endif /* _KERNEL */ 50113270Sphk 51113270Sphk#include <sys/ioccom.h> 52113270Sphk 53113270Sphkstruct wave { 54113270Sphk int index; 55113270Sphk int period; 56113270Sphk int offset; 57113270Sphk int length; 58113270Sphk int avg; 59113270Sphk off_t mapvir; 60113270Sphk int flags; 61113270Sphk 62113270Sphk int npages; 63113270Sphk void **virtual; 64113270Sphk}; 65113270Sphk 66113270Sphk#define ADLINK_SETWAVE _IOWR('A', 232, struct wave) 67113270Sphk#define ADLINK_GETWAVE _IOWR('A', 233, struct wave) 68113270Sphk 69113270Sphk#ifdef _KERNEL 70113270Sphk 71113270Sphk#define INTPERPAGE (PAGE_SIZE / sizeof(int)) 72113270Sphk#define I16PERPAGE (PAGE_SIZE / sizeof(int16_t)) 73113270Sphk 74113094Sphk/* 75113270Sphk * Sample rate 76113094Sphk */ 77113270Sphk#define SPS 1250000 78113094Sphk 79113270Sphk/* 80113270Sphk * We sample one channel (= 16 bits) at 1.25 msps giving 2.5Mbyte/sec, 81113270Sphk * 100 pages will give us about 1/6 second buffering. 82113270Sphk */ 83113270Sphk#define NRING 100 84113094Sphk 85113270Sphk/* 86113270Sphk * How many waves are we willing to entertain 87113270Sphk */ 88113270Sphk#define NWAVE 25 89113270Sphk 90113094Sphkstruct info { 91113094Sphk int nring; 92113094Sphk off_t o_ring; 93113270Sphk 94113270Sphk int ngri; 95113270Sphk int ppgri; 96113270Sphk off_t o_gri; 97113094Sphk}; 98113094Sphk 99113094Sphkstruct softc { 100113094Sphk device_t device; 101113094Sphk void *intrhand; 102113094Sphk struct resource *r0, *r1, *ri; 103113094Sphk bus_space_tag_t t0, t1; 104113094Sphk bus_space_handle_t h0, h1; 105113094Sphk dev_t dev; 106113270Sphk off_t mapvir; 107113094Sphk 108113270Sphk struct proc *procp; 109113270Sphk 110113094Sphk struct info *info; 111113094Sphk 112113270Sphk struct wave *wave[NWAVE]; 113113270Sphk 114113094Sphk int idx; 115113094Sphk void *ring[NRING]; 116113270Sphk vm_paddr_t pring[NRING]; 117113094Sphk int stat[NRING]; 118113270Sphk 119113270Sphk uint64_t cnt; 120113270Sphk 121113270Sphk u_char flags[I16PERPAGE]; 122113094Sphk}; 123113094Sphk 124113270Sphkstatic void 125113270Sphkadlink_wave(struct softc *sc, struct wave *wp, int16_t *sp) 126113270Sphk{ 127113270Sphk int f, i, k, m, *ip; 128113270Sphk 129113270Sphk f = 0; 130113270Sphk for (i = 0; i < I16PERPAGE; ) { 131113270Sphk k = (sc->cnt - wp->offset + i) % wp->period; 132113270Sphk if (k >= wp->length) { 133113270Sphk i += wp->period - k; 134113270Sphk sp += wp->period - k; 135113270Sphk continue; 136113270Sphk } 137113270Sphk m = k % INTPERPAGE; 138113270Sphk ip = (int *)(wp->virtual[k / INTPERPAGE]) + m; 139113270Sphk while (m < INTPERPAGE && i < I16PERPAGE && k < wp->length) { 140113270Sphk if (sc->flags[i] >= wp->index) 141113270Sphk *ip += (*sp * 8 - *ip) >> wp->avg; 142113270Sphk if (wp->flags & 1) 143113270Sphk sc->flags[i] = wp->index; 144113270Sphk sp++; 145113270Sphk ip++; 146113270Sphk m++; 147113270Sphk i++; 148113270Sphk k++; 149113270Sphk } 150113270Sphk } 151113270Sphk} 152113270Sphk 153113270Sphkstatic void 154113270Sphkadlink_tickle(struct softc *sc) 155113270Sphk{ 156113270Sphk 157113270Sphk wakeup(sc); 158113270Sphk tsleep(&sc->ring, PUSER | PCATCH, "tickle", 1); 159113270Sphk} 160113270Sphk 161113094Sphkstatic int 162113270Sphkadlink_new_wave(struct softc *sc, int index, int period, int offset, int length, int avg, int flags) 163113270Sphk{ 164113270Sphk struct wave *wp; 165113270Sphk int l, i; 166113270Sphk void **oldvir, **newvir; 167113270Sphk 168113270Sphk if (index < 0 || index >= NWAVE) 169113270Sphk return (EINVAL); 170113270Sphk wp = sc->wave[index]; 171113270Sphk if (wp == NULL) { 172113270Sphk adlink_tickle(sc); 173113270Sphk wp = malloc(sizeof *wp, M_DEVBUF, M_WAITOK | M_ZERO); 174113270Sphk } 175113270Sphk l = howmany(length, INTPERPAGE); 176113270Sphk /* Setting a high average here to neuter the realtime bits */ 177113270Sphk wp->avg = 31; 178113270Sphk if (wp->npages < l) { 179113270Sphk oldvir = wp->virtual; 180113270Sphk adlink_tickle(sc); 181113270Sphk newvir = malloc(sizeof(void *) * l, M_DEVBUF, M_WAITOK | M_ZERO); 182113270Sphk if (wp->npages > 0) { 183113270Sphk adlink_tickle(sc); 184113270Sphk bcopy(oldvir, newvir, wp->npages * sizeof(void *)); 185113270Sphk } 186113270Sphk for (i = wp->npages; i < l; i++) { 187113270Sphk adlink_tickle(sc); 188113270Sphk newvir[i] = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK); 189113270Sphk } 190113270Sphk wp->virtual = newvir; 191113270Sphk wp->npages = l; 192113270Sphk wp->mapvir = sc->mapvir; 193113270Sphk sc->mapvir += l * PAGE_SIZE; 194113270Sphk } else { 195113270Sphk oldvir = NULL; 196113270Sphk } 197113270Sphk wp->index = index; 198113270Sphk wp->period = period; 199113270Sphk wp->offset = offset; 200113270Sphk wp->length = length; 201113270Sphk wp->flags = flags; 202113270Sphk 203113270Sphk for (i = 0; i < l; i++) { 204113270Sphk adlink_tickle(sc); 205113270Sphk bzero(wp->virtual[i], PAGE_SIZE); 206113270Sphk } 207113270Sphk wp->avg = avg; 208113270Sphk sc->wave[index] = wp; 209113270Sphk printf("Wave[%d] {period %d, offset %d, length %d, avg %d, flags %x}\n", 210113270Sphk wp->index, wp->period, wp->offset, wp->length, wp->avg, wp->flags); 211113270Sphk free(oldvir, M_DEVBUF); 212113270Sphk return (0); 213113270Sphk} 214113270Sphk 215113270Sphkstatic void 216113270Sphkadlink_loran(void *arg) 217113270Sphk{ 218113270Sphk struct softc *sc; 219113270Sphk int idx, i; 220113270Sphk 221113270Sphk sc = arg; 222113270Sphk idx = 0; 223113270Sphk for (;;) { 224113270Sphk while (sc->stat[idx] == 0) 225113270Sphk msleep(sc, NULL, PRIBIO, "loran", 1); 226113270Sphk memset(sc->flags, NWAVE, sizeof sc->flags); 227113270Sphk for (i = 0; i < NWAVE; i++) { 228113270Sphk if (sc->wave[i] != NULL) 229113270Sphk adlink_wave(sc, sc->wave[i], sc->ring[idx]); 230113270Sphk } 231113270Sphk sc->cnt += I16PERPAGE; 232113270Sphk sc->stat[idx] = 0; 233113270Sphk idx++; 234113270Sphk idx %= NRING; 235113270Sphk } 236113270Sphk kthread_exit(0); 237113270Sphk} 238113270Sphk 239113270Sphkstatic int 240113094Sphkadlink_open(dev_t dev, int oflags, int devtype, struct thread *td) 241113094Sphk{ 242113094Sphk static int once; 243113094Sphk struct softc *sc; 244113270Sphk int i, error; 245113094Sphk uint32_t u; 246113094Sphk 247113094Sphk if (once) 248113094Sphk return (0); 249113094Sphk once = 1; 250113094Sphk 251113094Sphk sc = dev->si_drv1; 252113094Sphk sc->info = malloc(PAGE_SIZE, M_DEVBUF, M_ZERO | M_WAITOK); 253113094Sphk sc->info->nring = NRING; 254113270Sphk 255113094Sphk sc->info->o_ring = PAGE_SIZE; 256113094Sphk for (i = 0; i < NRING; i++) { 257113094Sphk sc->ring[i] = malloc(PAGE_SIZE, M_DEVBUF, M_ZERO | M_WAITOK); 258113270Sphk sc->pring[i] = vtophys(sc->ring[i]); 259113094Sphk } 260113094Sphk 261113270Sphk error = adlink_new_wave(sc, NWAVE - 1, SPS, 0, SPS, 7, 0); 262113270Sphk if (error) 263113270Sphk return (error); 264113270Sphk 265113270Sphk error = kthread_create(adlink_loran, sc, &sc->procp, 266113270Sphk 0, 0, "adlink%d", device_get_unit(sc->device)); 267113270Sphk if (error) 268113270Sphk return (error); 269113270Sphk 270113270Sphk /* Enable interrupts on write complete */ 271113094Sphk bus_space_write_4(sc->t0, sc->h0, 0x38, 0x00004000); 272113270Sphk 273113270Sphk /* Sample CH0 only */ 274113094Sphk bus_space_write_4(sc->t1, sc->h1, 0x00, 1); 275113270Sphk 276113270Sphk /* Divide clock by ten */ 277113270Sphk bus_space_write_4(sc->t1, sc->h1, 0x04, 4); 278113270Sphk 279113270Sphk /* Software trigger mode: software */ 280113094Sphk bus_space_write_4(sc->t1, sc->h1, 0x08, 0); 281113270Sphk 282113270Sphk /* Trigger level zero */ 283113094Sphk bus_space_write_4(sc->t1, sc->h1, 0x0c, 0); 284113270Sphk 285113270Sphk /* Trigger source CH0 (not used) */ 286113094Sphk bus_space_write_4(sc->t1, sc->h1, 0x10, 0); 287113270Sphk 288113270Sphk /* Fifo control/status: flush */ 289113094Sphk bus_space_write_4(sc->t1, sc->h1, 0x18, 3); 290113270Sphk 291113270Sphk /* Clock source: external sine */ 292113094Sphk bus_space_write_4(sc->t1, sc->h1, 0x20, 2); 293113094Sphk 294113270Sphk /* Set up Write DMA */ 295113270Sphk bus_space_write_4(sc->t0, sc->h0, 0x24, sc->pring[i]); 296113094Sphk bus_space_write_4(sc->t0, sc->h0, 0x28, PAGE_SIZE); 297113094Sphk u = bus_space_read_4(sc->t0, sc->h0, 0x3c); 298113094Sphk bus_space_write_4(sc->t0, sc->h0, 0x3c, u | 0x00000600); 299113094Sphk 300113270Sphk /* Acquisition Enable Register: go! */ 301113094Sphk bus_space_write_4(sc->t1, sc->h1, 0x1c, 1); 302113094Sphk return (0); 303113094Sphk} 304113094Sphk 305113094Sphkstatic int 306113270Sphkadlink_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 307113270Sphk{ 308113270Sphk struct softc *sc; 309113270Sphk struct wave *wp; 310113270Sphk int i, error; 311113270Sphk 312113270Sphk sc = dev->si_drv1; 313113270Sphk wp = (struct wave *)data; 314113270Sphk i = wp->index; 315113270Sphk if (i < 0 || i >= NWAVE) 316113270Sphk return (EINVAL); 317113270Sphk if (cmd == ADLINK_GETWAVE) { 318113270Sphk if (sc->wave[i] == NULL) 319113270Sphk return (ENOENT); 320113270Sphk bcopy(sc->wave[i], wp, sizeof(*wp)); 321113270Sphk return (0); 322113270Sphk } 323113270Sphk if (cmd == ADLINK_SETWAVE) { 324113270Sphk error = adlink_new_wave(sc, 325113270Sphk i, 326113270Sphk wp->period, 327113270Sphk wp->offset, 328113270Sphk wp->length, 329113270Sphk wp->avg, 330113270Sphk wp->flags); 331113270Sphk if (error) 332113270Sphk return (error); 333113270Sphk bcopy(sc->wave[i], wp, sizeof(*wp)); 334113270Sphk return (0); 335113270Sphk } 336113270Sphk return (ENOIOCTL); 337113270Sphk} 338113270Sphk 339113270Sphkstatic int 340113094Sphkadlink_mmap(dev_t dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) 341113094Sphk{ 342113094Sphk struct softc *sc; 343113270Sphk struct wave *wp; 344113270Sphk int i, j; 345113094Sphk 346113094Sphk sc = dev->si_drv1; 347113094Sphk if (nprot != VM_PROT_READ) 348113094Sphk return (-1); 349113270Sphk for (i = 0; i < NWAVE; i++) { 350113270Sphk if (sc->wave[i] == NULL) 351113270Sphk continue; 352113270Sphk wp = sc->wave[i]; 353113270Sphk if (offset < wp->mapvir) 354113270Sphk continue; 355113270Sphk j = (offset - wp->mapvir) / PAGE_SIZE; 356113270Sphk if (j >= wp->npages) 357113270Sphk continue; 358113270Sphk *paddr = vtophys(wp->virtual[j]); 359113094Sphk return (0); 360113094Sphk } 361113270Sphk return (-1); 362113094Sphk} 363113094Sphk 364113094Sphkstatic void 365113094Sphkadlink_intr(void *arg) 366113094Sphk{ 367113094Sphk struct softc *sc; 368113094Sphk uint32_t u; 369113270Sphk int i, j; 370113094Sphk 371113094Sphk sc = arg; 372113094Sphk u = bus_space_read_4(sc->t0, sc->h0, 0x38); 373113094Sphk if (!(u & 0x00800000)) 374113094Sphk return; 375113094Sphk bus_space_write_4(sc->t0, sc->h0, 0x38, u | 0x003f4000); 376113094Sphk 377113270Sphk j = sc->idx; 378113270Sphk sc->stat[j] = 1; 379113270Sphk i = (j + 1) % NRING; 380113094Sphk sc->idx = i; 381113270Sphk u = bus_space_read_4(sc->t1, sc->h1, 0x18); 382113270Sphk if (u & 1) { 383113270Sphk printf("adlink FIFO overrun\n"); 384113270Sphk return; 385113270Sphk } 386113270Sphk bus_space_write_4(sc->t0, sc->h0, 0x24, sc->pring[i]); 387113094Sphk bus_space_write_4(sc->t0, sc->h0, 0x28, PAGE_SIZE); 388113270Sphk wakeup(sc); 389113270Sphk if (sc->stat[i]) { 390113270Sphk printf("adlink page busy\n"); 391113270Sphk } 392113094Sphk} 393113094Sphk 394113094Sphkstatic struct cdevsw adlink_cdevsw = { 395113094Sphk .d_open = adlink_open, 396113094Sphk .d_close = nullclose, 397113270Sphk .d_ioctl = adlink_ioctl, 398113094Sphk .d_mmap = adlink_mmap, 399113094Sphk .d_name = "adlink", 400113094Sphk}; 401113094Sphk 402113094Sphkstatic devclass_t adlink_devclass; 403113094Sphk 404113094Sphkstatic int 405113094Sphkadlink_probe(device_t self) 406113094Sphk{ 407113094Sphk 408113094Sphk if (pci_get_devid(self) != 0x80da10e8) 409113094Sphk return (ENXIO); 410113094Sphk device_set_desc(self, "Adlink PCI-9812 4 ch 12 bit 20 msps"); 411113094Sphk return (0); 412113094Sphk} 413113094Sphk 414113094Sphkstatic int 415113094Sphkadlink_attach(device_t self) 416113094Sphk{ 417113094Sphk struct softc *sc; 418113094Sphk int rid, i; 419113094Sphk 420113094Sphk sc = device_get_softc(self); 421113094Sphk bzero(sc, sizeof *sc); 422113094Sphk sc->device = self; 423113094Sphk 424113270Sphk /* 425113270Sphk * This is the PCI mapped registers of the AMCC 9535 "matchmaker" 426113270Sphk * chip. 427113270Sphk */ 428113094Sphk rid = 0x10; 429113094Sphk sc->r0 = bus_alloc_resource(self, SYS_RES_IOPORT, &rid, 430113094Sphk 0, ~0, 1, RF_ACTIVE); 431113094Sphk if (sc->r0 == NULL) 432113094Sphk return(ENODEV); 433113094Sphk sc->t0 = rman_get_bustag(sc->r0); 434113094Sphk sc->h0 = rman_get_bushandle(sc->r0); 435113094Sphk printf("Res0 %x %x\n", sc->t0, sc->h0); 436113094Sphk 437113270Sphk /* 438113270Sphk * This is the PCI mapped registers of the ADC hardware, they 439113270Sphk * are described in the manual which comes with the card. 440113270Sphk */ 441113094Sphk rid = 0x14; 442113094Sphk sc->r1 = bus_alloc_resource(self, SYS_RES_IOPORT, &rid, 443113094Sphk 0, ~0, 1, RF_ACTIVE); 444113094Sphk if (sc->r1 == NULL) 445113094Sphk return(ENODEV); 446113094Sphk sc->t1 = rman_get_bustag(sc->r1); 447113094Sphk sc->h1 = rman_get_bushandle(sc->r1); 448113094Sphk printf("Res1 %x %x\n", sc->t1, sc->h1); 449113094Sphk 450113094Sphk rid = 0x0; 451113094Sphk sc->ri = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 452113094Sphk 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 453113094Sphk if (sc->ri == NULL) 454113094Sphk return (ENODEV); 455113094Sphk 456113270Sphk i = bus_setup_intr(self, sc->ri, INTR_MPSAFE | INTR_TYPE_MISC | INTR_FAST, 457113094Sphk adlink_intr, sc, &sc->intrhand); 458113270Sphk if (i) { 459113270Sphk printf("adlink: Couldn't get FAST intr\n"); 460113270Sphk i = bus_setup_intr(self, sc->ri, INTR_TYPE_MISC, 461113270Sphk adlink_intr, sc, &sc->intrhand); 462113270Sphk } 463113094Sphk 464113094Sphk if (i) 465113094Sphk return (ENODEV); 466113094Sphk 467113094Sphk sc->dev = make_dev(&adlink_cdevsw, device_get_unit(self), 468113094Sphk UID_ROOT, GID_WHEEL, 0444, "adlink%d", device_get_unit(self)); 469113094Sphk sc->dev->si_drv1 = sc; 470113094Sphk 471113094Sphk return (0); 472113094Sphk} 473113094Sphk 474113094Sphkstatic device_method_t adlink_methods[] = { 475113094Sphk /* Device interface */ 476113094Sphk DEVMETHOD(device_probe, adlink_probe), 477113094Sphk DEVMETHOD(device_attach, adlink_attach), 478113094Sphk DEVMETHOD(device_suspend, bus_generic_suspend), 479113094Sphk DEVMETHOD(device_resume, bus_generic_resume), 480113094Sphk DEVMETHOD(device_shutdown, bus_generic_shutdown), 481113094Sphk {0, 0} 482113094Sphk}; 483113094Sphk 484113094Sphkstatic driver_t adlink_driver = { 485113094Sphk "adlink", 486113094Sphk adlink_methods, 487113094Sphk sizeof(struct softc) 488113094Sphk}; 489113094Sphk 490113094SphkDRIVER_MODULE(adlink, pci, adlink_driver, adlink_devclass, 0, 0); 491113270Sphk#endif /* _KERNEL */ 492