xref: /freebsd/sys/dev/adlink/adlink.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
1 /*-
2  * Copyright (c) 2003-2004 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  * This is a device driver or the Adlink 9812 and 9810 ADC cards, mainly
30  * intended to support Software Defined Radio reception of timesignals
31  * in the VLF band.  See http://phk.freebsd.dk/loran-c
32  *
33  * The driver is configured with ioctls which define a ringbuffer with
34  * a given number of chunks in it.  The a control structure and the
35  * ringbuffer can then be mmap(2)'ed into userland and the application
36  * can operate on the data directly.
37  *
38  * Tested with 10MHz external clock, divisor of 2 (ie: 5MHz sampling),
39  * One channel active (ie: 2 bytes per sample = 10MB/sec) on a 660MHz
40  * Celeron PC.
41  *
42  */
43 
44 #ifdef _KERNEL
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/kthread.h>
54 #include <sys/conf.h>
55 #include <sys/bus.h>
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58 #include <sys/rman.h>
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
61 #include <pci_if.h>
62 #include <vm/vm.h>
63 #include <vm/pmap.h>
64 
65 #endif /* _KERNEL */
66 
67 #include <sys/ioccom.h>
68 
69 #define ADLINK_SETDIVISOR	_IOWR('A', 255, u_int)	/* divisor */
70 #define ADLINK_SETCHUNKSIZE	_IOWR('A', 254, u_int)	/* bytes */
71 #define ADLINK_SETRINGSIZE	_IOWR('A', 253, u_int)	/* bytes */
72 #define ADLINK_START		_IOWR('A', 252, u_int)	/* dummy */
73 #define ADLINK_STOP		_IOWR('A', 251, u_int)	/* dummy */
74 #define ADLINK_RESET		_IOWR('A', 250, u_int)	/* dummy */
75 
76 struct page0 {
77 	u_int			version;
78 	int			state;
79 #	  define STATE_RESET	-1
80 #	  define STATE_RUN	0
81 	u_int			divisor;	/* int */
82 	u_int			chunksize;	/* bytes */
83 	u_int			ringsize;	/* chunks */
84 	u_int			o_sample;	/*
85 						 * offset of ring generation
86 						 * array
87 						 */
88 	u_int			o_ring;		/* offset of ring */
89 };
90 
91 #define PAGE0VERSION	20050219
92 
93 #ifdef _KERNEL
94 
95 struct pgstat {
96 	uint64_t		*sample;
97 	vm_paddr_t		phys;
98 	void			*virt;
99 	struct pgstat		*next;
100 };
101 
102 struct softc {
103 	device_t		device;
104 	void			*intrhand;
105 	struct resource		*res[3];
106 	struct cdev		*dev;
107 	off_t			mapvir;
108 	int			error;
109 	struct page0		*p0;
110 	u_int			nchunks;
111 	struct pgstat		*chunks;
112 	struct pgstat		*next;
113 	uint64_t		sample;
114 };
115 
116 static d_ioctl_t adlink_ioctl;
117 static d_mmap_t	adlink_mmap;
118 static int adlink_intr(void *arg);
119 
120 static struct cdevsw adlink_cdevsw = {
121 	.d_version =	D_VERSION,
122 	.d_ioctl =	adlink_ioctl,
123 	.d_mmap =	adlink_mmap,
124 	.d_name =	"adlink",
125 };
126 
127 static int
128 adlink_intr(void *arg)
129 {
130 	struct softc *sc;
131 	struct pgstat *pg;
132 	uint32_t u;
133 
134 	sc = arg;
135 	u = bus_read_4(sc->res[0], 0x38);
136 	if (!(u & 0x00800000))
137 		return; // XXX - FILTER_STRAY?
138 	bus_write_4(sc->res[0], 0x38, u | 0x003f4000);
139 
140 	sc->sample += sc->p0->chunksize / 2;
141 	pg = sc->next;
142 	*(pg->sample) = sc->sample;
143 
144 	u = bus_read_4(sc->res[1], 0x18);
145 	if (u & 1)
146 		sc->p0->state = EIO;
147 
148 	if (sc->p0->state != STATE_RUN) {
149 		printf("adlink: stopping %d\n", sc->p0->state);
150 		return; // XXX - FILTER_STRAY?
151 	}
152 
153 	pg = pg->next;
154 	sc->next = pg;
155 	*(pg->sample) = 0;
156 	bus_write_4(sc->res[0], 0x24, pg->phys);
157 	bus_write_4(sc->res[0], 0x28, sc->p0->chunksize);
158 	wakeup(sc);
159 	return (FILTER_HANDLED);
160 }
161 
162 static int
163 adlink_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
164 {
165 	struct softc *sc;
166 	vm_offset_t o;
167 	int i;
168 	struct pgstat *pg;
169 
170 	sc = dev->si_drv1;
171 	if (nprot != VM_PROT_READ)
172 		return (-1);
173 	if (offset == 0) {
174 		*paddr = vtophys(sc->p0);
175 		return (0);
176 	}
177 	o = PAGE_SIZE;
178 	pg = sc->chunks;
179 	for (i = 0; i < sc->nchunks; i++, pg++) {
180 		if (offset - o >= sc->p0->chunksize) {
181 			o += sc->p0->chunksize;
182 			continue;
183 		}
184 		*paddr = pg->phys + (offset - o);
185 		return (0);
186 	}
187 	return (-1);
188 }
189 
190 static int
191 adlink_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
192 {
193 	struct softc *sc;
194 	int i, error;
195 	u_int u;
196 	struct pgstat *pg;
197 	uint64_t *sample;
198 
199 	sc = dev->si_drv1;
200 	u = *(u_int*)data;
201 	error = 0;
202 	switch (cmd) {
203 	case ADLINK_SETDIVISOR:
204 		if (sc->p0->state == STATE_RUN)
205 			return (EBUSY);
206 		if (u & 1)
207 			return (EINVAL);
208 		sc->p0->divisor = u;
209 		break;
210 	case ADLINK_SETCHUNKSIZE:
211 		if (sc->p0->state != STATE_RESET)
212 			return (EBUSY);
213 		if (u % PAGE_SIZE)
214 			return (EINVAL);
215 		if (sc->p0->ringsize != 0 && sc->p0->ringsize % u)
216 			return (EINVAL);
217 		sc->p0->chunksize = u;
218 		break;
219 	case ADLINK_SETRINGSIZE:
220 		if (sc->p0->state != STATE_RESET)
221 			return (EBUSY);
222 		if (u % PAGE_SIZE)
223 			return (EINVAL);
224 		if (sc->p0->chunksize != 0 && u % sc->p0->chunksize)
225 			return (EINVAL);
226 		sc->p0->ringsize = u;
227 		break;
228 	case ADLINK_START:
229 		if (sc->p0->state == STATE_RUN)
230 			return (EBUSY);
231 		if (sc->p0->state == STATE_RESET) {
232 
233 			if (sc->p0->chunksize == 0)
234 				sc->p0->chunksize = 4 * PAGE_SIZE;
235 			if (sc->p0->ringsize == 0)
236 				sc->p0->ringsize = 16 * sc->p0->chunksize;
237 			if (sc->p0->divisor == 0)
238 				sc->p0->divisor = 4;
239 
240 			sc->nchunks = sc->p0->ringsize / sc->p0->chunksize;
241 			if (sc->nchunks * sizeof (*pg->sample) +
242 			    sizeof *sc->p0 > PAGE_SIZE)
243 				return (EINVAL);
244 			sc->p0->o_ring = PAGE_SIZE;
245 			sample = (uint64_t *)(sc->p0 + 1);
246 			sc->p0->o_sample =
247 			    (uintptr_t)sample - (uintptr_t)(sc->p0);
248 			pg = malloc(sizeof *pg * sc->nchunks,
249 			    M_DEVBUF, M_WAITOK | M_ZERO);
250 			sc->chunks = pg;
251 			for (i = 0; i < sc->nchunks; i++) {
252 				pg->sample = sample;
253 				*pg->sample = 0;
254 				sample++;
255 				pg->virt = contigmalloc(sc->p0->chunksize,
256 				    M_DEVBUF, M_WAITOK,
257 				    0ul, 0xfffffffful,
258 				    PAGE_SIZE, 0);
259 				pg->phys = vtophys(pg->virt);
260 				if (i == sc->nchunks - 1)
261 					pg->next = sc->chunks;
262 				else
263 					pg->next = pg + 1;
264 				pg++;
265 			}
266 			sc->next = sc->chunks;
267 		}
268 
269 		/* Reset generation numbers */
270 		pg = sc->chunks;
271 		for (i = 0; i < sc->nchunks; i++) {
272 			*pg->sample = 0;
273 			pg++;
274 		}
275 
276 		/* Enable interrupts on write complete */
277 		bus_write_4(sc->res[0], 0x38, 0x00004000);
278 
279 		/* Sample CH0 only */
280 		bus_write_4(sc->res[1], 0x00, 1);
281 
282 		/* Divide clock by four */
283 		bus_write_4(sc->res[1], 0x04, sc->p0->divisor);
284 
285 		/* Software trigger mode: software */
286 		bus_write_4(sc->res[1], 0x08, 0);
287 
288 		/* Trigger level zero */
289 		bus_write_4(sc->res[1], 0x0c, 0);
290 
291 		/* Trigger source CH0 (not used) */
292 		bus_write_4(sc->res[1], 0x10, 0);
293 
294 		/* Fifo control/status: flush */
295 		bus_write_4(sc->res[1], 0x18, 3);
296 
297 		/* Clock source: external sine */
298 		bus_write_4(sc->res[1], 0x20, 2);
299 
300 		/* Chipmunks are go! */
301 		sc->p0->state = STATE_RUN;
302 
303 		/* Set up Write DMA */
304 		pg = sc->next = sc->chunks;
305 		*(pg->sample) = 0;
306 		bus_write_4(sc->res[0], 0x24, pg->phys);
307 		bus_write_4(sc->res[0], 0x28, sc->p0->chunksize);
308 		u = bus_read_4(sc->res[0], 0x3c);
309 		bus_write_4(sc->res[0], 0x3c, u | 0x00000600);
310 
311 		/* Acquisition Enable Register: go! */
312 		bus_write_4(sc->res[1], 0x1c, 1);
313 
314 		break;
315 	case ADLINK_STOP:
316 		if (sc->p0->state == STATE_RESET)
317 			break;
318 		sc->p0->state = EINTR;
319 		while (*(sc->next->sample) == 0)
320 			tsleep(sc, PUSER | PCATCH, "adstop", 1);
321 		break;
322 #ifdef notyet
323 	/*
324 	 * I'm not sure we can actually do this.  How do we revoke
325 	 * the mmap'ed pages from any process having them mmapped ?
326 	 */
327 	case ADLINK_RESET:
328 		if (sc->p0->state == STATE_RESET)
329 			break;
330 		sc->p0->state = EINTR;
331 		while (*(sc->next->samp) == 0)
332 			tsleep(sc, PUSER | PCATCH, "adreset", 1);
333 		/* deallocate ring buffer */
334 		break;
335 #endif
336 	default:
337 		error = ENOIOCTL;
338 		break;
339 	}
340 	return (error);
341 }
342 
343 static devclass_t adlink_devclass;
344 
345 static int
346 adlink_probe(device_t self)
347 {
348 
349 	if (pci_get_devid(self) != 0x80da10e8)
350 		return (ENXIO);
351 	device_set_desc(self, "Adlink PCI-9812 4 ch 12 bit 20 msps");
352 	return (BUS_PROBE_DEFAULT);
353 }
354 
355 static struct resource_spec adlink_res_spec[] = {
356 	{ SYS_RES_IOPORT,	PCIR_BAR(0),	RF_ACTIVE},
357 	{ SYS_RES_IOPORT,	PCIR_BAR(1),	RF_ACTIVE},
358 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE},
359 	{ -1, 0, 0 }
360 };
361 
362 static int
363 adlink_attach(device_t self)
364 {
365 	struct softc *sc;
366 	int i, error;
367 
368 	sc = device_get_softc(self);
369 	bzero(sc, sizeof *sc);
370 	sc->device = self;
371 
372 	error = bus_alloc_resources(self, adlink_res_spec, sc->res);
373 	if (error)
374 		return (error);
375 
376 	/* XXX why do we need INTR_MPSAFE if INTR_FAST was declared too?!?!? */
377 	i = bus_setup_intr(self, sc->res[2],
378 	    INTR_MPSAFE | INTR_TYPE_MISC,
379 	    adlink_intr, NULL, sc, &sc->intrhand);
380 	if (i) {
381 		printf("adlink: Couldn't get FAST intr\n");
382 		i = bus_setup_intr(self, sc->res[2],
383 		    INTR_MPSAFE | INTR_TYPE_MISC,
384 		    NULL, (driver_intr_t *)adlink_intr, sc, &sc->intrhand);
385 	}
386 
387 	if (i) {
388 		bus_release_resources(self, adlink_res_spec, sc->res);
389 		return (ENODEV);
390 	}
391 
392 	sc->p0 = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
393 	sc->p0->version = PAGE0VERSION;
394 	sc->p0->state = STATE_RESET;
395 
396 	sc->dev = make_dev(&adlink_cdevsw, device_get_unit(self),
397 	    UID_ROOT, GID_WHEEL, 0444, "adlink%d", device_get_unit(self));
398 	sc->dev->si_drv1 = sc;
399 
400 	return (0);
401 }
402 
403 static device_method_t adlink_methods[] = {
404 	/* Device interface */
405 	DEVMETHOD(device_probe,		adlink_probe),
406 	DEVMETHOD(device_attach,	adlink_attach),
407 	DEVMETHOD(device_suspend,	bus_generic_suspend),
408 	DEVMETHOD(device_resume,	bus_generic_resume),
409 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
410 	{0, 0}
411 };
412 
413 static driver_t adlink_driver = {
414 	"adlink",
415 	adlink_methods,
416 	sizeof(struct softc)
417 };
418 
419 DRIVER_MODULE(adlink, pci, adlink_driver, adlink_devclass, 0, 0);
420 
421 #endif /* _KERNEL */
422