xref: /freebsd/sys/dev/sound/pci/hdspe.c (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
1 /*-
2  * Copyright (c) 2012 Ruslan Bukin <br@bsdpad.com>
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * RME HDSPe driver for FreeBSD.
29  * Supported cards: AIO, RayDAT.
30  */
31 
32 #include <dev/sound/pcm/sound.h>
33 #include <dev/sound/pci/hdspe.h>
34 #include <dev/sound/chip.h>
35 
36 #include <dev/pci/pcireg.h>
37 #include <dev/pci/pcivar.h>
38 
39 #include <mixer_if.h>
40 
41 SND_DECLARE_FILE("$FreeBSD$");
42 
43 static struct hdspe_channel chan_map_aio[] = {
44 	{  0,  1,   "line", 1, 1 },
45 	{  6,  7,  "phone", 1, 0 },
46 	{  8,  9,    "aes", 1, 1 },
47 	{ 10, 11, "s/pdif", 1, 1 },
48 	{ 12, 16,   "adat", 1, 1 },
49 
50 	/* Single or double speed. */
51 	{ 14, 18,   "adat", 1, 1 },
52 
53 	/* Single speed only. */
54 	{ 13, 15,   "adat", 1, 1 },
55 	{ 17, 19,   "adat", 1, 1 },
56 
57 	{  0,  0,     NULL, 0, 0 },
58 };
59 
60 static struct hdspe_channel chan_map_rd[] = {
61 	{   0, 1,    "aes", 1, 1 },
62 	{   2, 3, "s/pdif", 1, 1 },
63 	{   4, 5,   "adat", 1, 1 },
64 	{   6, 7,   "adat", 1, 1 },
65 	{   8, 9,   "adat", 1, 1 },
66 	{ 10, 11,   "adat", 1, 1 },
67 
68 	/* Single or double speed. */
69 	{ 12, 13,   "adat", 1, 1 },
70 	{ 14, 15,   "adat", 1, 1 },
71 	{ 16, 17,   "adat", 1, 1 },
72 	{ 18, 19,   "adat", 1, 1 },
73 
74 	/* Single speed only. */
75 	{ 20, 21,   "adat", 1, 1 },
76 	{ 22, 23,   "adat", 1, 1 },
77 	{ 24, 25,   "adat", 1, 1 },
78 	{ 26, 27,   "adat", 1, 1 },
79 	{ 28, 29,   "adat", 1, 1 },
80 	{ 30, 31,   "adat", 1, 1 },
81 	{ 32, 33,   "adat", 1, 1 },
82 	{ 34, 35,   "adat", 1, 1 },
83 
84 	{ 0,  0,      NULL, 0, 0 },
85 };
86 
87 static void
88 hdspe_intr(void *p)
89 {
90 	struct sc_info *sc = (struct sc_info *)p;
91 	struct sc_pcminfo *scp;
92 	device_t *devlist;
93 	int devcount, status;
94 	int i, err;
95 
96 	snd_mtxlock(sc->lock);
97 
98 	status = hdspe_read_1(sc, HDSPE_STATUS_REG);
99 	if (status & HDSPE_AUDIO_IRQ_PENDING) {
100 		if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
101 			return;
102 
103 		for (i = 0; i < devcount; i++) {
104 			scp = device_get_ivars(devlist[i]);
105 			if (scp->ih != NULL)
106 				scp->ih(scp);
107 		}
108 
109 		hdspe_write_1(sc, HDSPE_INTERRUPT_ACK, 0);
110 		free(devlist, M_TEMP);
111 	}
112 
113 	snd_mtxunlock(sc->lock);
114 }
115 
116 static void
117 hdspe_dmapsetmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
118 {
119 #if 0
120 	struct sc_info *sc = (struct sc_info *)arg;
121 	device_printf(sc->dev, "hdspe_dmapsetmap()\n");
122 #endif
123 }
124 
125 static int
126 hdspe_alloc_resources(struct sc_info *sc)
127 {
128 
129 	/* Allocate resource. */
130 	sc->csid = PCIR_BAR(0);
131 	sc->cs = bus_alloc_resource(sc->dev, SYS_RES_MEMORY,
132 	    &sc->csid, 0, ~0, 1, RF_ACTIVE);
133 
134 	if (!sc->cs) {
135 		device_printf(sc->dev, "Unable to map SYS_RES_MEMORY.\n");
136 		return (ENXIO);
137 	}
138 	sc->cst = rman_get_bustag(sc->cs);
139 	sc->csh = rman_get_bushandle(sc->cs);
140 
141 
142 	/* Allocate interrupt resource. */
143 	sc->irqid = 0;
144 	sc->irq = bus_alloc_resource(sc->dev, SYS_RES_IRQ, &sc->irqid,
145 	    0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
146 
147 	if (!sc->irq ||
148 	    bus_setup_intr(sc->dev, sc->irq, INTR_MPSAFE | INTR_TYPE_AV,
149 		NULL, hdspe_intr, sc, &sc->ih)) {
150 		device_printf(sc->dev, "Unable to alloc interrupt resource.\n");
151 		return (ENXIO);
152 	}
153 
154 	/* Allocate DMA resources. */
155 	if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(sc->dev),
156 		/*alignment*/4,
157 		/*boundary*/0,
158 		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
159 		/*highaddr*/BUS_SPACE_MAXADDR,
160 		/*filter*/NULL,
161 		/*filterarg*/NULL,
162 		/*maxsize*/2 * HDSPE_DMASEGSIZE,
163 		/*nsegments*/2,
164 		/*maxsegsz*/HDSPE_DMASEGSIZE,
165 		/*flags*/0,
166 		/*lockfunc*/busdma_lock_mutex,
167 		/*lockarg*/&Giant,
168 		/*dmatag*/&sc->dmat) != 0) {
169 		device_printf(sc->dev, "Unable to create dma tag.\n");
170 		return (ENXIO);
171 	}
172 
173 	sc->bufsize = HDSPE_DMASEGSIZE;
174 
175 	/* pbuf (play buffer). */
176 	if (bus_dmamem_alloc(sc->dmat, (void **)&sc->pbuf,
177 		BUS_DMA_NOWAIT, &sc->pmap)) {
178 		device_printf(sc->dev, "Can't alloc pbuf.\n");
179 		return (ENXIO);
180 	}
181 
182 	if (bus_dmamap_load(sc->dmat, sc->pmap, sc->pbuf, sc->bufsize,
183 		hdspe_dmapsetmap, sc, 0)) {
184 		device_printf(sc->dev, "Can't load pbuf.\n");
185 		return (ENXIO);
186 	}
187 
188 	/* rbuf (rec buffer). */
189 	if (bus_dmamem_alloc(sc->dmat, (void **)&sc->rbuf,
190 		BUS_DMA_NOWAIT, &sc->rmap)) {
191 		device_printf(sc->dev, "Can't alloc rbuf.\n");
192 		return (ENXIO);
193 	}
194 
195 	if (bus_dmamap_load(sc->dmat, sc->rmap, sc->rbuf, sc->bufsize,
196 		hdspe_dmapsetmap, sc, 0)) {
197 		device_printf(sc->dev, "Can't load rbuf.\n");
198 		return (ENXIO);
199 	}
200 
201 	bzero(sc->pbuf, sc->bufsize);
202 	bzero(sc->rbuf, sc->bufsize);
203 
204 	return (0);
205 }
206 
207 static void
208 hdspe_map_dmabuf(struct sc_info *sc)
209 {
210 	uint32_t paddr,raddr;
211 	int i;
212 
213 	paddr = vtophys(sc->pbuf);
214 	raddr = vtophys(sc->rbuf);
215 
216 	for (i = 0; i < HDSPE_MAX_SLOTS * 16; i++) {
217 		hdspe_write_4(sc, HDSPE_PAGE_ADDR_BUF_OUT + 4 * i,
218                     paddr + i * 4096);
219 		hdspe_write_4(sc, HDSPE_PAGE_ADDR_BUF_IN + 4 * i,
220                     raddr + i * 4096);
221 	}
222 }
223 
224 static int
225 hdspe_probe(device_t dev)
226 {
227 	uint32_t rev;
228 
229 	if (pci_get_vendor(dev) == PCI_VENDOR_XILINX &&
230 	    pci_get_device(dev) == PCI_DEVICE_XILINX_HDSPE) {
231 		rev = pci_get_revid(dev);
232 		switch (rev) {
233 		case PCI_REVISION_AIO:
234 			device_set_desc(dev, "RME HDSPe AIO");
235 			return 0;
236 		case PCI_REVISION_RAYDAT:
237 			device_set_desc(dev, "RME HDSPe RayDAT");
238 			return 0;
239 		}
240 	}
241 
242 	return (ENXIO);
243 }
244 
245 static int
246 set_pci_config(device_t dev)
247 {
248 	uint32_t data;
249 
250 	pci_enable_busmaster(dev);
251 
252 	data = pci_get_revid(dev);
253 	data |= PCIM_CMD_PORTEN;
254 	pci_write_config(dev, PCIR_COMMAND, data, 2);
255 
256 	return 0;
257 }
258 
259 static int
260 hdspe_init(struct sc_info *sc)
261 {
262 	long long period;
263 
264 	/* Set defaults. */
265 	sc->ctrl_register |= HDSPM_CLOCK_MODE_MASTER;
266 
267 	/* Set latency. */
268 	sc->period = 32;
269 	sc->ctrl_register = hdspe_encode_latency(7);
270 
271 	/* Set rate. */
272 	sc->speed = HDSPE_SPEED_DEFAULT;
273 	sc->ctrl_register &= ~HDSPE_FREQ_MASK;
274 	sc->ctrl_register |= HDSPE_FREQ_MASK_DEFAULT;
275 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
276 
277 	switch (sc->type) {
278 	case RAYDAT:
279 	case AIO:
280 		period = HDSPE_FREQ_AIO;
281 		break;
282 	default:
283 		return (ENXIO);
284 	}
285 
286 	/* Set DDS value. */
287 	period /= sc->speed;
288 	hdspe_write_4(sc, HDSPE_FREQ_REG, period);
289 
290 	/* Other settings. */
291 	sc->settings_register = 0;
292 	hdspe_write_4(sc, HDSPE_SETTINGS_REG, sc->settings_register);
293 
294 	return 0;
295 }
296 
297 static int
298 hdspe_attach(device_t dev)
299 {
300 	struct sc_info *sc;
301 	struct sc_pcminfo *scp;
302 	struct hdspe_channel *chan_map;
303 	uint32_t rev;
304 	int i, err;
305 
306 #if 0
307 	device_printf(dev, "hdspe_attach()\n");
308 #endif
309 
310 	set_pci_config(dev);
311 
312 	sc = device_get_softc(dev);
313 	sc->lock = snd_mtxcreate(device_get_nameunit(dev),
314 	    "snd_hdspe softc");
315 	sc->dev = dev;
316 
317 	rev = pci_get_revid(dev);
318 	switch (rev) {
319 	case PCI_REVISION_AIO:
320 		sc->type = AIO;
321 		chan_map = chan_map_aio;
322 		break;
323 	case PCI_REVISION_RAYDAT:
324 		sc->type = RAYDAT;
325 		chan_map = chan_map_rd;
326 		break;
327 	default:
328 		return ENXIO;
329 	}
330 
331 	/* Allocate resources. */
332 	err = hdspe_alloc_resources(sc);
333 	if (err) {
334 		device_printf(dev, "Unable to allocate system resources.\n");
335 		return ENXIO;
336 	}
337 
338 	if (hdspe_init(sc) != 0)
339 		return ENXIO;
340 
341 	for (i = 0; i < HDSPE_MAX_CHANS && chan_map[i].descr != NULL; i++) {
342 		scp = malloc(sizeof(struct sc_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
343 		scp->hc = &chan_map[i];
344 		scp->sc = sc;
345 		scp->dev = device_add_child(dev, "pcm", -1);
346 		device_set_ivars(scp->dev, scp);
347 	}
348 
349 	hdspe_map_dmabuf(sc);
350 
351 	return (bus_generic_attach(dev));
352 }
353 
354 static void
355 hdspe_dmafree(struct sc_info *sc)
356 {
357 
358 	bus_dmamap_unload(sc->dmat, sc->rmap);
359 	bus_dmamap_unload(sc->dmat, sc->pmap);
360 	bus_dmamem_free(sc->dmat, sc->rbuf, sc->rmap);
361 	bus_dmamem_free(sc->dmat, sc->pbuf, sc->pmap);
362 	sc->rmap = sc->pmap = NULL;
363 	sc->rbuf = sc->pbuf = NULL;
364 }
365 
366 static int
367 hdspe_detach(device_t dev)
368 {
369 	struct sc_info *sc;
370 	int err;
371 
372 	sc = device_get_softc(dev);
373 	if (sc == NULL) {
374 		device_printf(dev,"Can't detach: softc is null.\n");
375 		return 0;
376 	}
377 
378 	err = device_delete_children(dev);
379 	if (err)
380 		return (err);
381 
382 	hdspe_dmafree(sc);
383 
384 	if (sc->ih)
385 		bus_teardown_intr(dev, sc->irq, sc->ih);
386 	if (sc->dmat)
387 		bus_dma_tag_destroy(sc->dmat);
388 	if (sc->irq)
389 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
390 	if (sc->cs)
391 		bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0), sc->cs);
392 	if (sc->lock)
393 		snd_mtxfree(sc->lock);
394 
395 	return 0;
396 }
397 
398 static device_method_t hdspe_methods[] = {
399 	DEVMETHOD(device_probe,     hdspe_probe),
400 	DEVMETHOD(device_attach,    hdspe_attach),
401 	DEVMETHOD(device_detach,    hdspe_detach),
402 	{ 0, 0 }
403 };
404 
405 static driver_t hdspe_driver = {
406 	"hdspe",
407 	hdspe_methods,
408 	PCM_SOFTC_SIZE,
409 };
410 
411 static devclass_t hdspe_devclass;
412 
413 DRIVER_MODULE(snd_hdspe, pci, hdspe_driver, hdspe_devclass, 0, 0);
414