2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2003-2004 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * This is a device driver or the Adlink 9812 and 9810 ADC cards, mainly
32 * intended to support Software Defined Radio reception of timesignals
33 * in the VLF band. See http://phk.freebsd.dk/loran-c
35 * The driver is configured with ioctls which define a ringbuffer with
36 * a given number of chunks in it. The a control structure and the
37 * ringbuffer can then be mmap(2)'ed into userland and the application
38 * can operate on the data directly.
40 * Tested with 10MHz external clock, divisor of 2 (ie: 5MHz sampling),
41 * One channel active (ie: 2 bytes per sample = 10MB/sec) on a 660MHz
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
53 #include <sys/kernel.h>
54 #include <sys/module.h>
55 #include <sys/kthread.h>
58 #include <machine/bus.h>
59 #include <machine/resource.h>
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcivar.h>
69 #include <sys/ioccom.h>
71 #define ADLINK_SETDIVISOR _IOWR('A', 255, u_int) /* divisor */
72 #define ADLINK_SETCHUNKSIZE _IOWR('A', 254, u_int) /* bytes */
73 #define ADLINK_SETRINGSIZE _IOWR('A', 253, u_int) /* bytes */
74 #define ADLINK_START _IOWR('A', 252, u_int) /* dummy */
75 #define ADLINK_STOP _IOWR('A', 251, u_int) /* dummy */
76 #define ADLINK_RESET _IOWR('A', 250, u_int) /* dummy */
81 # define STATE_RESET -1
83 u_int divisor; /* int */
84 u_int chunksize; /* bytes */
85 u_int ringsize; /* chunks */
87 * offset of ring generation
90 u_int o_ring; /* offset of ring */
93 #define PAGE0VERSION 20050219
107 struct resource *res[3];
113 struct pgstat *chunks;
118 static d_ioctl_t adlink_ioctl;
119 static d_mmap_t adlink_mmap;
120 static int adlink_intr(void *arg);
122 static struct cdevsw adlink_cdevsw = {
123 .d_version = D_VERSION,
124 .d_flags = D_NEEDGIANT,
125 .d_ioctl = adlink_ioctl,
126 .d_mmap = adlink_mmap,
131 adlink_intr(void *arg)
138 u = bus_read_4(sc->res[0], 0x38);
139 if (!(u & 0x00800000))
140 return (FILTER_STRAY);
141 bus_write_4(sc->res[0], 0x38, u | 0x003f4000);
143 sc->sample += sc->p0->chunksize / 2;
145 *(pg->sample) = sc->sample;
147 u = bus_read_4(sc->res[1], 0x18);
151 if (sc->p0->state != STATE_RUN) {
152 printf("adlink: stopping %d\n", sc->p0->state);
153 return (FILTER_STRAY);
159 bus_write_4(sc->res[0], 0x24, pg->phys);
160 bus_write_4(sc->res[0], 0x28, sc->p0->chunksize);
162 return (FILTER_HANDLED);
166 adlink_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
167 int nprot, vm_memattr_t *memattr)
175 if (nprot != VM_PROT_READ)
178 *paddr = vtophys(sc->p0);
183 for (i = 0; i < sc->nchunks; i++, pg++) {
184 if (offset - o >= sc->p0->chunksize) {
185 o += sc->p0->chunksize;
188 *paddr = pg->phys + (offset - o);
195 adlink_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
207 case ADLINK_SETDIVISOR:
208 if (sc->p0->state == STATE_RUN)
214 case ADLINK_SETCHUNKSIZE:
215 if (sc->p0->state != STATE_RESET)
219 if (sc->p0->ringsize != 0 && sc->p0->ringsize % u)
221 sc->p0->chunksize = u;
223 case ADLINK_SETRINGSIZE:
224 if (sc->p0->state != STATE_RESET)
228 if (sc->p0->chunksize != 0 && u % sc->p0->chunksize)
230 sc->p0->ringsize = u;
233 if (sc->p0->state == STATE_RUN)
235 if (sc->p0->state == STATE_RESET) {
237 if (sc->p0->chunksize == 0)
238 sc->p0->chunksize = 4 * PAGE_SIZE;
239 if (sc->p0->ringsize == 0)
240 sc->p0->ringsize = 16 * sc->p0->chunksize;
241 if (sc->p0->divisor == 0)
244 sc->nchunks = sc->p0->ringsize / sc->p0->chunksize;
245 if (sc->nchunks * sizeof (*pg->sample) +
246 sizeof *sc->p0 > PAGE_SIZE)
248 sc->p0->o_ring = PAGE_SIZE;
249 sample = (uint64_t *)(sc->p0 + 1);
251 (uintptr_t)sample - (uintptr_t)(sc->p0);
252 pg = malloc(sizeof *pg * sc->nchunks,
253 M_DEVBUF, M_WAITOK | M_ZERO);
255 for (i = 0; i < sc->nchunks; i++) {
259 pg->virt = contigmalloc(sc->p0->chunksize,
263 pg->phys = vtophys(pg->virt);
264 if (i == sc->nchunks - 1)
265 pg->next = sc->chunks;
270 sc->next = sc->chunks;
273 /* Reset generation numbers */
275 for (i = 0; i < sc->nchunks; i++) {
280 /* Enable interrupts on write complete */
281 bus_write_4(sc->res[0], 0x38, 0x00004000);
283 /* Sample CH0 only */
284 bus_write_4(sc->res[1], 0x00, 1);
286 /* Divide clock by four */
287 bus_write_4(sc->res[1], 0x04, sc->p0->divisor);
289 /* Software trigger mode: software */
290 bus_write_4(sc->res[1], 0x08, 0);
292 /* Trigger level zero */
293 bus_write_4(sc->res[1], 0x0c, 0);
295 /* Trigger source CH0 (not used) */
296 bus_write_4(sc->res[1], 0x10, 0);
298 /* Fifo control/status: flush */
299 bus_write_4(sc->res[1], 0x18, 3);
301 /* Clock source: external sine */
302 bus_write_4(sc->res[1], 0x20, 2);
304 /* Chipmunks are go! */
305 sc->p0->state = STATE_RUN;
307 /* Set up Write DMA */
308 pg = sc->next = sc->chunks;
310 bus_write_4(sc->res[0], 0x24, pg->phys);
311 bus_write_4(sc->res[0], 0x28, sc->p0->chunksize);
312 u = bus_read_4(sc->res[0], 0x3c);
313 bus_write_4(sc->res[0], 0x3c, u | 0x00000600);
315 /* Acquisition Enable Register: go! */
316 bus_write_4(sc->res[1], 0x1c, 1);
320 if (sc->p0->state == STATE_RESET)
322 sc->p0->state = EINTR;
323 while (*(sc->next->sample) == 0)
324 tsleep(sc, PUSER | PCATCH, "adstop", 1);
328 * I'm not sure we can actually do this. How do we revoke
329 * the mmap'ed pages from any process having them mmapped ?
332 if (sc->p0->state == STATE_RESET)
334 sc->p0->state = EINTR;
335 while (*(sc->next->samp) == 0)
336 tsleep(sc, PUSER | PCATCH, "adreset", 1);
337 /* deallocate ring buffer */
347 static devclass_t adlink_devclass;
355 { .vendor = 0x10e8, .device = 0x80da,
356 .desc ="Adlink PCI-9812 4 ch 12 bit 20 msps" }
360 adlink_probe(device_t self)
363 uint16_t vendor, device;
365 vendor = pci_get_vendor(self);
366 device = pci_get_device(self);
367 for (i = 0; i < nitems(adlink_id); i++) {
368 if (adlink_id[i].vendor == vendor &&
369 adlink_id[i].device == device) {
370 device_set_desc(self, adlink_id[i].desc);
371 return (BUS_PROBE_DEFAULT);
377 static struct resource_spec adlink_res_spec[] = {
378 { SYS_RES_IOPORT, PCIR_BAR(0), RF_ACTIVE},
379 { SYS_RES_IOPORT, PCIR_BAR(1), RF_ACTIVE},
380 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE},
385 adlink_attach(device_t self)
390 sc = device_get_softc(self);
391 bzero(sc, sizeof *sc);
394 error = bus_alloc_resources(self, adlink_res_spec, sc->res);
398 i = bus_setup_intr(self, sc->res[2], INTR_TYPE_MISC,
399 adlink_intr, NULL, sc, &sc->intrhand);
401 printf("adlink: Couldn't get FAST intr\n");
402 i = bus_setup_intr(self, sc->res[2],
403 INTR_MPSAFE | INTR_TYPE_MISC,
404 NULL, (driver_intr_t *)adlink_intr, sc, &sc->intrhand);
408 bus_release_resources(self, adlink_res_spec, sc->res);
412 sc->p0 = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
413 sc->p0->version = PAGE0VERSION;
414 sc->p0->state = STATE_RESET;
416 sc->dev = make_dev(&adlink_cdevsw, device_get_unit(self),
417 UID_ROOT, GID_WHEEL, 0444, "adlink%d", device_get_unit(self));
418 sc->dev->si_drv1 = sc;
423 static device_method_t adlink_methods[] = {
424 /* Device interface */
425 DEVMETHOD(device_probe, adlink_probe),
426 DEVMETHOD(device_attach, adlink_attach),
427 DEVMETHOD(device_suspend, bus_generic_suspend),
428 DEVMETHOD(device_resume, bus_generic_resume),
429 DEVMETHOD(device_shutdown, bus_generic_shutdown),
434 static driver_t adlink_driver = {
440 DRIVER_MODULE(adlink, pci, adlink_driver, adlink_devclass, 0, 0);
441 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, adlink, adlink_id,