1232337Smav/*- 2331722Seadler * Copyright (c) 2012 Ruslan Bukin <br@bsdpad.com> 3232337Smav * All rights reserved. 4232337Smav * 5232337Smav * Redistribution and use in source and binary forms, with or without 6232337Smav * modification, are permitted provided that the following conditions 7232337Smav * are met: 8232337Smav * 1. Redistributions of source code must retain the above copyright 9232337Smav * notice, this list of conditions and the following disclaimer. 10232337Smav * 2. Redistributions in binary form must reproduce the above copyright 11232337Smav * notice, this list of conditions and the following disclaimer in the 12232337Smav * documentation and/or other materials provided with the distribution. 13232337Smav * 14232337Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15232337Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16232337Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17232337Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18232337Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19232337Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20232337Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21232337Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22232337Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23232337Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24232337Smav * SUCH DAMAGE. 25232337Smav */ 26232337Smav 27232337Smav/* 28232337Smav * RME HDSPe driver for FreeBSD. 29232337Smav * Supported cards: AIO, RayDAT. 30232337Smav */ 31232337Smav 32232337Smav#include <dev/sound/pcm/sound.h> 33232337Smav#include <dev/sound/pci/hdspe.h> 34232337Smav#include <dev/sound/chip.h> 35232337Smav 36232337Smav#include <dev/pci/pcireg.h> 37232337Smav#include <dev/pci/pcivar.h> 38232337Smav 39232337Smav#include <mixer_if.h> 40232337Smav 41232337SmavSND_DECLARE_FILE("$FreeBSD$"); 42232337Smav 43232337Smavstatic struct hdspe_channel chan_map_aio[] = { 44232337Smav { 0, 1, "line", 1, 1 }, 45232337Smav { 6, 7, "phone", 1, 0 }, 46232337Smav { 8, 9, "aes", 1, 1 }, 47232337Smav { 10, 11, "s/pdif", 1, 1 }, 48232337Smav { 12, 16, "adat", 1, 1 }, 49232337Smav 50232337Smav /* Single or double speed. */ 51232337Smav { 14, 18, "adat", 1, 1 }, 52232337Smav 53232337Smav /* Single speed only. */ 54232337Smav { 13, 15, "adat", 1, 1 }, 55232337Smav { 17, 19, "adat", 1, 1 }, 56232337Smav 57232337Smav { 0, 0, NULL, 0, 0 }, 58232337Smav}; 59232337Smav 60232337Smavstatic struct hdspe_channel chan_map_rd[] = { 61232337Smav { 0, 1, "aes", 1, 1 }, 62232337Smav { 2, 3, "s/pdif", 1, 1 }, 63232337Smav { 4, 5, "adat", 1, 1 }, 64232337Smav { 6, 7, "adat", 1, 1 }, 65232337Smav { 8, 9, "adat", 1, 1 }, 66232337Smav { 10, 11, "adat", 1, 1 }, 67232337Smav 68232337Smav /* Single or double speed. */ 69232337Smav { 12, 13, "adat", 1, 1 }, 70232337Smav { 14, 15, "adat", 1, 1 }, 71232337Smav { 16, 17, "adat", 1, 1 }, 72232337Smav { 18, 19, "adat", 1, 1 }, 73232337Smav 74232337Smav /* Single speed only. */ 75232337Smav { 20, 21, "adat", 1, 1 }, 76232337Smav { 22, 23, "adat", 1, 1 }, 77232337Smav { 24, 25, "adat", 1, 1 }, 78232337Smav { 26, 27, "adat", 1, 1 }, 79232337Smav { 28, 29, "adat", 1, 1 }, 80232337Smav { 30, 31, "adat", 1, 1 }, 81232337Smav { 32, 33, "adat", 1, 1 }, 82232337Smav { 34, 35, "adat", 1, 1 }, 83232337Smav 84232337Smav { 0, 0, NULL, 0, 0 }, 85232337Smav}; 86232337Smav 87232337Smavstatic void 88232337Smavhdspe_intr(void *p) 89232337Smav{ 90331722Seadler struct sc_info *sc = (struct sc_info *)p; 91232337Smav struct sc_pcminfo *scp; 92232337Smav device_t *devlist; 93331722Seadler int devcount, status; 94331722Seadler int i, err; 95232337Smav 96232337Smav snd_mtxlock(sc->lock); 97232337Smav 98232337Smav status = hdspe_read_1(sc, HDSPE_STATUS_REG); 99232337Smav if (status & HDSPE_AUDIO_IRQ_PENDING) { 100232337Smav if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 101232337Smav return; 102232337Smav 103232337Smav for (i = 0; i < devcount; i++) { 104232337Smav scp = device_get_ivars(devlist[i]); 105232337Smav if (scp->ih != NULL) 106232337Smav scp->ih(scp); 107232337Smav } 108232337Smav 109232337Smav hdspe_write_1(sc, HDSPE_INTERRUPT_ACK, 0); 110241066Skevlo free(devlist, M_TEMP); 111232337Smav } 112232337Smav 113232337Smav snd_mtxunlock(sc->lock); 114232337Smav} 115232337Smav 116232337Smavstatic void 117232337Smavhdspe_dmapsetmap(void *arg, bus_dma_segment_t *segs, int nseg, int error) 118232337Smav{ 119232337Smav#if 0 120331722Seadler struct sc_info *sc = (struct sc_info *)arg; 121232337Smav device_printf(sc->dev, "hdspe_dmapsetmap()\n"); 122232337Smav#endif 123232337Smav} 124232337Smav 125232337Smavstatic int 126232337Smavhdspe_alloc_resources(struct sc_info *sc) 127232337Smav{ 128232337Smav 129232337Smav /* Allocate resource. */ 130232337Smav sc->csid = PCIR_BAR(0); 131295790Sjhibbits sc->cs = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY, 132295790Sjhibbits &sc->csid, RF_ACTIVE); 133232337Smav 134232337Smav if (!sc->cs) { 135232337Smav device_printf(sc->dev, "Unable to map SYS_RES_MEMORY.\n"); 136232337Smav return (ENXIO); 137232337Smav } 138232337Smav sc->cst = rman_get_bustag(sc->cs); 139232337Smav sc->csh = rman_get_bushandle(sc->cs); 140232337Smav 141331722Seadler 142232337Smav /* Allocate interrupt resource. */ 143232337Smav sc->irqid = 0; 144295790Sjhibbits sc->irq = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &sc->irqid, 145295790Sjhibbits RF_ACTIVE | RF_SHAREABLE); 146232337Smav 147232337Smav if (!sc->irq || 148232337Smav bus_setup_intr(sc->dev, sc->irq, INTR_MPSAFE | INTR_TYPE_AV, 149232337Smav NULL, hdspe_intr, sc, &sc->ih)) { 150232337Smav device_printf(sc->dev, "Unable to alloc interrupt resource.\n"); 151232337Smav return (ENXIO); 152232337Smav } 153232337Smav 154232337Smav /* Allocate DMA resources. */ 155232337Smav if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(sc->dev), 156232337Smav /*alignment*/4, 157232337Smav /*boundary*/0, 158232337Smav /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 159232337Smav /*highaddr*/BUS_SPACE_MAXADDR, 160232337Smav /*filter*/NULL, 161232337Smav /*filterarg*/NULL, 162232337Smav /*maxsize*/2 * HDSPE_DMASEGSIZE, 163232337Smav /*nsegments*/2, 164232337Smav /*maxsegsz*/HDSPE_DMASEGSIZE, 165232337Smav /*flags*/0, 166232337Smav /*lockfunc*/busdma_lock_mutex, 167232337Smav /*lockarg*/&Giant, 168232337Smav /*dmatag*/&sc->dmat) != 0) { 169232337Smav device_printf(sc->dev, "Unable to create dma tag.\n"); 170232337Smav return (ENXIO); 171232337Smav } 172232337Smav 173232337Smav sc->bufsize = HDSPE_DMASEGSIZE; 174232337Smav 175232337Smav /* pbuf (play buffer). */ 176232337Smav if (bus_dmamem_alloc(sc->dmat, (void **)&sc->pbuf, 177232337Smav BUS_DMA_NOWAIT, &sc->pmap)) { 178232337Smav device_printf(sc->dev, "Can't alloc pbuf.\n"); 179232337Smav return (ENXIO); 180232337Smav } 181232337Smav 182232337Smav if (bus_dmamap_load(sc->dmat, sc->pmap, sc->pbuf, sc->bufsize, 183232337Smav hdspe_dmapsetmap, sc, 0)) { 184232337Smav device_printf(sc->dev, "Can't load pbuf.\n"); 185232337Smav return (ENXIO); 186232337Smav } 187232337Smav 188232337Smav /* rbuf (rec buffer). */ 189232337Smav if (bus_dmamem_alloc(sc->dmat, (void **)&sc->rbuf, 190232337Smav BUS_DMA_NOWAIT, &sc->rmap)) { 191232337Smav device_printf(sc->dev, "Can't alloc rbuf.\n"); 192232337Smav return (ENXIO); 193232337Smav } 194232337Smav 195232337Smav if (bus_dmamap_load(sc->dmat, sc->rmap, sc->rbuf, sc->bufsize, 196232337Smav hdspe_dmapsetmap, sc, 0)) { 197232337Smav device_printf(sc->dev, "Can't load rbuf.\n"); 198232337Smav return (ENXIO); 199232337Smav } 200232337Smav 201232337Smav bzero(sc->pbuf, sc->bufsize); 202232337Smav bzero(sc->rbuf, sc->bufsize); 203232337Smav 204232337Smav return (0); 205232337Smav} 206232337Smav 207232337Smavstatic void 208232337Smavhdspe_map_dmabuf(struct sc_info *sc) 209232337Smav{ 210331722Seadler uint32_t paddr,raddr; 211232337Smav int i; 212232337Smav 213232337Smav paddr = vtophys(sc->pbuf); 214232337Smav raddr = vtophys(sc->rbuf); 215232337Smav 216232337Smav for (i = 0; i < HDSPE_MAX_SLOTS * 16; i++) { 217232337Smav hdspe_write_4(sc, HDSPE_PAGE_ADDR_BUF_OUT + 4 * i, 218232337Smav paddr + i * 4096); 219232337Smav hdspe_write_4(sc, HDSPE_PAGE_ADDR_BUF_IN + 4 * i, 220232337Smav raddr + i * 4096); 221232337Smav } 222232337Smav} 223232337Smav 224232337Smavstatic int 225232337Smavhdspe_probe(device_t dev) 226232337Smav{ 227232337Smav uint32_t rev; 228232337Smav 229232337Smav if (pci_get_vendor(dev) == PCI_VENDOR_XILINX && 230232337Smav pci_get_device(dev) == PCI_DEVICE_XILINX_HDSPE) { 231232337Smav rev = pci_get_revid(dev); 232232337Smav switch (rev) { 233232337Smav case PCI_REVISION_AIO: 234232337Smav device_set_desc(dev, "RME HDSPe AIO"); 235331722Seadler return 0; 236232337Smav case PCI_REVISION_RAYDAT: 237232337Smav device_set_desc(dev, "RME HDSPe RayDAT"); 238331722Seadler return 0; 239232337Smav } 240232337Smav } 241232337Smav 242232337Smav return (ENXIO); 243232337Smav} 244232337Smav 245232337Smavstatic int 246232337Smavhdspe_init(struct sc_info *sc) 247232337Smav{ 248232337Smav long long period; 249232337Smav 250232337Smav /* Set defaults. */ 251232337Smav sc->ctrl_register |= HDSPM_CLOCK_MODE_MASTER; 252232337Smav 253232337Smav /* Set latency. */ 254232337Smav sc->period = 32; 255232337Smav sc->ctrl_register = hdspe_encode_latency(7); 256232337Smav 257232337Smav /* Set rate. */ 258232337Smav sc->speed = HDSPE_SPEED_DEFAULT; 259232337Smav sc->ctrl_register &= ~HDSPE_FREQ_MASK; 260232337Smav sc->ctrl_register |= HDSPE_FREQ_MASK_DEFAULT; 261232337Smav hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register); 262232337Smav 263232337Smav switch (sc->type) { 264232337Smav case RAYDAT: 265232337Smav case AIO: 266232337Smav period = HDSPE_FREQ_AIO; 267232337Smav break; 268232337Smav default: 269232337Smav return (ENXIO); 270232337Smav } 271232337Smav 272232337Smav /* Set DDS value. */ 273232337Smav period /= sc->speed; 274232337Smav hdspe_write_4(sc, HDSPE_FREQ_REG, period); 275232337Smav 276232337Smav /* Other settings. */ 277232337Smav sc->settings_register = 0; 278232337Smav hdspe_write_4(sc, HDSPE_SETTINGS_REG, sc->settings_register); 279232337Smav 280331722Seadler return 0; 281232337Smav} 282232337Smav 283232337Smavstatic int 284232337Smavhdspe_attach(device_t dev) 285232337Smav{ 286331722Seadler struct sc_info *sc; 287331722Seadler struct sc_pcminfo *scp; 288330897Seadler struct hdspe_channel *chan_map; 289232337Smav uint32_t rev; 290232337Smav int i, err; 291232337Smav 292232337Smav#if 0 293232337Smav device_printf(dev, "hdspe_attach()\n"); 294232337Smav#endif 295232337Smav 296232337Smav sc = device_get_softc(dev); 297232337Smav sc->lock = snd_mtxcreate(device_get_nameunit(dev), 298232337Smav "snd_hdspe softc"); 299232337Smav sc->dev = dev; 300232337Smav 301254263Sscottl pci_enable_busmaster(dev); 302232337Smav rev = pci_get_revid(dev); 303232337Smav switch (rev) { 304232337Smav case PCI_REVISION_AIO: 305232337Smav sc->type = AIO; 306232337Smav chan_map = chan_map_aio; 307232337Smav break; 308232337Smav case PCI_REVISION_RAYDAT: 309232337Smav sc->type = RAYDAT; 310232337Smav chan_map = chan_map_rd; 311232337Smav break; 312232337Smav default: 313331722Seadler return ENXIO; 314232337Smav } 315232337Smav 316232337Smav /* Allocate resources. */ 317232337Smav err = hdspe_alloc_resources(sc); 318232337Smav if (err) { 319232337Smav device_printf(dev, "Unable to allocate system resources.\n"); 320331722Seadler return ENXIO; 321232337Smav } 322232337Smav 323232337Smav if (hdspe_init(sc) != 0) 324331722Seadler return ENXIO; 325232337Smav 326232337Smav for (i = 0; i < HDSPE_MAX_CHANS && chan_map[i].descr != NULL; i++) { 327232337Smav scp = malloc(sizeof(struct sc_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO); 328232337Smav scp->hc = &chan_map[i]; 329232337Smav scp->sc = sc; 330232337Smav scp->dev = device_add_child(dev, "pcm", -1); 331232337Smav device_set_ivars(scp->dev, scp); 332232337Smav } 333232337Smav 334232337Smav hdspe_map_dmabuf(sc); 335232337Smav 336237975Sglebius return (bus_generic_attach(dev)); 337232337Smav} 338232337Smav 339232337Smavstatic void 340232337Smavhdspe_dmafree(struct sc_info *sc) 341232337Smav{ 342232337Smav 343232337Smav bus_dmamap_unload(sc->dmat, sc->rmap); 344232337Smav bus_dmamap_unload(sc->dmat, sc->pmap); 345232337Smav bus_dmamem_free(sc->dmat, sc->rbuf, sc->rmap); 346232337Smav bus_dmamem_free(sc->dmat, sc->pbuf, sc->pmap); 347232337Smav sc->rbuf = sc->pbuf = NULL; 348232337Smav} 349232337Smav 350232337Smavstatic int 351232337Smavhdspe_detach(device_t dev) 352232337Smav{ 353232337Smav struct sc_info *sc; 354232337Smav int err; 355232337Smav 356232337Smav sc = device_get_softc(dev); 357232337Smav if (sc == NULL) { 358232337Smav device_printf(dev,"Can't detach: softc is null.\n"); 359331722Seadler return 0; 360232337Smav } 361232337Smav 362232337Smav err = device_delete_children(dev); 363232337Smav if (err) 364232337Smav return (err); 365232337Smav 366232337Smav hdspe_dmafree(sc); 367232337Smav 368232337Smav if (sc->ih) 369232337Smav bus_teardown_intr(dev, sc->irq, sc->ih); 370232337Smav if (sc->dmat) 371232337Smav bus_dma_tag_destroy(sc->dmat); 372232337Smav if (sc->irq) 373232337Smav bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq); 374232337Smav if (sc->cs) 375232337Smav bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0), sc->cs); 376232337Smav if (sc->lock) 377232337Smav snd_mtxfree(sc->lock); 378232337Smav 379331722Seadler return 0; 380232337Smav} 381232337Smav 382232337Smavstatic device_method_t hdspe_methods[] = { 383232337Smav DEVMETHOD(device_probe, hdspe_probe), 384232337Smav DEVMETHOD(device_attach, hdspe_attach), 385232337Smav DEVMETHOD(device_detach, hdspe_detach), 386232337Smav { 0, 0 } 387232337Smav}; 388232337Smav 389232337Smavstatic driver_t hdspe_driver = { 390232337Smav "hdspe", 391232337Smav hdspe_methods, 392232337Smav PCM_SOFTC_SIZE, 393232337Smav}; 394232337Smav 395237975Sglebiusstatic devclass_t hdspe_devclass; 396237975Sglebius 397237975SglebiusDRIVER_MODULE(snd_hdspe, pci, hdspe_driver, hdspe_devclass, 0, 0); 398