2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012-2016 Ruslan Bukin <br@bsdpad.com>
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * RME HDSPe driver for FreeBSD.
31 * Supported cards: AIO, RayDAT.
34 #include <dev/sound/pcm/sound.h>
35 #include <dev/sound/pci/hdspe.h>
36 #include <dev/sound/chip.h>
38 #include <dev/pci/pcireg.h>
39 #include <dev/pci/pcivar.h>
43 SND_DECLARE_FILE("$FreeBSD$");
45 static struct hdspe_channel chan_map_aio[] = {
46 { 0, 1, "line", 1, 1 },
47 { 6, 7, "phone", 1, 0 },
48 { 8, 9, "aes", 1, 1 },
49 { 10, 11, "s/pdif", 1, 1 },
50 { 12, 16, "adat", 1, 1 },
52 /* Single or double speed. */
53 { 14, 18, "adat", 1, 1 },
55 /* Single speed only. */
56 { 13, 15, "adat", 1, 1 },
57 { 17, 19, "adat", 1, 1 },
62 static struct hdspe_channel chan_map_rd[] = {
63 { 0, 1, "aes", 1, 1 },
64 { 2, 3, "s/pdif", 1, 1 },
65 { 4, 5, "adat", 1, 1 },
66 { 6, 7, "adat", 1, 1 },
67 { 8, 9, "adat", 1, 1 },
68 { 10, 11, "adat", 1, 1 },
70 /* Single or double speed. */
71 { 12, 13, "adat", 1, 1 },
72 { 14, 15, "adat", 1, 1 },
73 { 16, 17, "adat", 1, 1 },
74 { 18, 19, "adat", 1, 1 },
76 /* Single speed only. */
77 { 20, 21, "adat", 1, 1 },
78 { 22, 23, "adat", 1, 1 },
79 { 24, 25, "adat", 1, 1 },
80 { 26, 27, "adat", 1, 1 },
81 { 28, 29, "adat", 1, 1 },
82 { 30, 31, "adat", 1, 1 },
83 { 32, 33, "adat", 1, 1 },
84 { 34, 35, "adat", 1, 1 },
92 struct sc_pcminfo *scp;
100 sc = (struct sc_info *)p;
102 snd_mtxlock(sc->lock);
104 status = hdspe_read_1(sc, HDSPE_STATUS_REG);
105 if (status & HDSPE_AUDIO_IRQ_PENDING) {
106 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
109 for (i = 0; i < devcount; i++) {
110 scp = device_get_ivars(devlist[i]);
115 hdspe_write_1(sc, HDSPE_INTERRUPT_ACK, 0);
116 free(devlist, M_TEMP);
119 snd_mtxunlock(sc->lock);
123 hdspe_dmapsetmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
127 sc = (struct sc_info *)arg;
130 device_printf(sc->dev, "hdspe_dmapsetmap()\n");
135 hdspe_alloc_resources(struct sc_info *sc)
138 /* Allocate resource. */
139 sc->csid = PCIR_BAR(0);
140 sc->cs = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
141 &sc->csid, RF_ACTIVE);
144 device_printf(sc->dev, "Unable to map SYS_RES_MEMORY.\n");
148 sc->cst = rman_get_bustag(sc->cs);
149 sc->csh = rman_get_bushandle(sc->cs);
151 /* Allocate interrupt resource. */
153 sc->irq = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &sc->irqid,
154 RF_ACTIVE | RF_SHAREABLE);
157 bus_setup_intr(sc->dev, sc->irq, INTR_MPSAFE | INTR_TYPE_AV,
158 NULL, hdspe_intr, sc, &sc->ih)) {
159 device_printf(sc->dev, "Unable to alloc interrupt resource.\n");
163 /* Allocate DMA resources. */
164 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(sc->dev),
167 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
168 /*highaddr*/BUS_SPACE_MAXADDR,
171 /*maxsize*/2 * HDSPE_DMASEGSIZE,
173 /*maxsegsz*/HDSPE_DMASEGSIZE,
175 /*lockfunc*/busdma_lock_mutex,
177 /*dmatag*/&sc->dmat) != 0) {
178 device_printf(sc->dev, "Unable to create dma tag.\n");
182 sc->bufsize = HDSPE_DMASEGSIZE;
184 /* pbuf (play buffer). */
185 if (bus_dmamem_alloc(sc->dmat, (void **)&sc->pbuf,
186 BUS_DMA_NOWAIT, &sc->pmap)) {
187 device_printf(sc->dev, "Can't alloc pbuf.\n");
191 if (bus_dmamap_load(sc->dmat, sc->pmap, sc->pbuf, sc->bufsize,
192 hdspe_dmapsetmap, sc, 0)) {
193 device_printf(sc->dev, "Can't load pbuf.\n");
197 /* rbuf (rec buffer). */
198 if (bus_dmamem_alloc(sc->dmat, (void **)&sc->rbuf,
199 BUS_DMA_NOWAIT, &sc->rmap)) {
200 device_printf(sc->dev, "Can't alloc rbuf.\n");
204 if (bus_dmamap_load(sc->dmat, sc->rmap, sc->rbuf, sc->bufsize,
205 hdspe_dmapsetmap, sc, 0)) {
206 device_printf(sc->dev, "Can't load rbuf.\n");
210 bzero(sc->pbuf, sc->bufsize);
211 bzero(sc->rbuf, sc->bufsize);
217 hdspe_map_dmabuf(struct sc_info *sc)
219 uint32_t paddr, raddr;
222 paddr = vtophys(sc->pbuf);
223 raddr = vtophys(sc->rbuf);
225 for (i = 0; i < HDSPE_MAX_SLOTS * 16; i++) {
226 hdspe_write_4(sc, HDSPE_PAGE_ADDR_BUF_OUT + 4 * i,
228 hdspe_write_4(sc, HDSPE_PAGE_ADDR_BUF_IN + 4 * i,
234 hdspe_probe(device_t dev)
238 if (pci_get_vendor(dev) == PCI_VENDOR_XILINX &&
239 pci_get_device(dev) == PCI_DEVICE_XILINX_HDSPE) {
240 rev = pci_get_revid(dev);
242 case PCI_REVISION_AIO:
243 device_set_desc(dev, "RME HDSPe AIO");
245 case PCI_REVISION_RAYDAT:
246 device_set_desc(dev, "RME HDSPe RayDAT");
255 hdspe_init(struct sc_info *sc)
260 sc->ctrl_register |= HDSPM_CLOCK_MODE_MASTER;
264 sc->ctrl_register = hdspe_encode_latency(7);
267 sc->speed = HDSPE_SPEED_DEFAULT;
268 sc->ctrl_register &= ~HDSPE_FREQ_MASK;
269 sc->ctrl_register |= HDSPE_FREQ_MASK_DEFAULT;
270 hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
275 period = HDSPE_FREQ_AIO;
283 hdspe_write_4(sc, HDSPE_FREQ_REG, period);
285 /* Other settings. */
286 sc->settings_register = 0;
287 hdspe_write_4(sc, HDSPE_SETTINGS_REG, sc->settings_register);
293 hdspe_attach(device_t dev)
295 struct hdspe_channel *chan_map;
296 struct sc_pcminfo *scp;
302 device_printf(dev, "hdspe_attach()\n");
305 sc = device_get_softc(dev);
306 sc->lock = snd_mtxcreate(device_get_nameunit(dev),
310 pci_enable_busmaster(dev);
311 rev = pci_get_revid(dev);
313 case PCI_REVISION_AIO:
315 chan_map = chan_map_aio;
317 case PCI_REVISION_RAYDAT:
319 chan_map = chan_map_rd;
325 /* Allocate resources. */
326 err = hdspe_alloc_resources(sc);
328 device_printf(dev, "Unable to allocate system resources.\n");
332 if (hdspe_init(sc) != 0)
335 for (i = 0; i < HDSPE_MAX_CHANS && chan_map[i].descr != NULL; i++) {
336 scp = malloc(sizeof(struct sc_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
337 scp->hc = &chan_map[i];
339 scp->dev = device_add_child(dev, "pcm", -1);
340 device_set_ivars(scp->dev, scp);
343 hdspe_map_dmabuf(sc);
345 return (bus_generic_attach(dev));
349 hdspe_dmafree(struct sc_info *sc)
352 bus_dmamap_unload(sc->dmat, sc->rmap);
353 bus_dmamap_unload(sc->dmat, sc->pmap);
354 bus_dmamem_free(sc->dmat, sc->rbuf, sc->rmap);
355 bus_dmamem_free(sc->dmat, sc->pbuf, sc->pmap);
356 sc->rbuf = sc->pbuf = NULL;
360 hdspe_detach(device_t dev)
365 sc = device_get_softc(dev);
367 device_printf(dev,"Can't detach: softc is null.\n");
371 err = device_delete_children(dev);
378 bus_teardown_intr(dev, sc->irq, sc->ih);
380 bus_dma_tag_destroy(sc->dmat);
382 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
384 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0), sc->cs);
386 snd_mtxfree(sc->lock);
391 static device_method_t hdspe_methods[] = {
392 DEVMETHOD(device_probe, hdspe_probe),
393 DEVMETHOD(device_attach, hdspe_attach),
394 DEVMETHOD(device_detach, hdspe_detach),
398 static driver_t hdspe_driver = {
404 static devclass_t hdspe_devclass;
406 DRIVER_MODULE(snd_hdspe, pci, hdspe_driver, hdspe_devclass, 0, 0);