aoa.c revision 187717
1/*- 2 * Copyright 2008 by Marco Trillo. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD: head/sys/dev/sound/macio/aoa.c 187717 2009-01-26 14:43:18Z nwhitehorn $ 26 */ 27 28/* 29 * Apple Onboard Audio (AOA). 30 */ 31 32#include <sys/cdefs.h> 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/kernel.h> 37#include <sys/bus.h> 38#include <sys/malloc.h> 39#include <sys/lock.h> 40#include <sys/mutex.h> 41#include <machine/dbdma.h> 42#include <machine/resource.h> 43#include <machine/bus.h> 44#include <sys/rman.h> 45#include <dev/ofw/ofw_bus.h> 46#include <dev/sound/pcm/sound.h> 47#include <dev/sound/macio/aoa.h> 48#include "mixer_if.h" 49 50struct aoa_dma { 51 struct mtx mutex; 52 struct resource *reg; /* DBDMA registers */ 53 dbdma_channel_t *channel; /* DBDMA channel */ 54 bus_dma_tag_t tag; /* bus_dma tag */ 55 struct pcm_channel *pcm; /* PCM channel */ 56 struct snd_dbuf *buf; /* PCM buffer */ 57 u_int slots; /* # of slots */ 58 u_int slot; /* current slot */ 59 u_int bufsz; /* buffer size */ 60 u_int blksz; /* block size */ 61 int running; 62}; 63 64static void 65aoa_dma_set_program(struct aoa_dma *dma) 66{ 67 u_int32_t addr; 68 int i; 69 70 addr = (u_int32_t) sndbuf_getbufaddr(dma->buf); 71 KASSERT(dma->bufsz == sndbuf_getsize(dma->buf), ("bad size")); 72 73 dma->slots = dma->bufsz / dma->blksz; 74 75 for (i = 0; i < dma->slots; ++i) { 76 dbdma_insert_command(dma->channel, 77 i, /* slot */ 78 DBDMA_OUTPUT_MORE, /* command */ 79 0, /* stream */ 80 addr, /* data */ 81 dma->blksz, /* count */ 82 DBDMA_ALWAYS, /* interrupt */ 83 DBDMA_COND_TRUE, /* branch */ 84 DBDMA_NEVER, /* wait */ 85 dma->slots + 1 /* branch_slot */ 86 ); 87 88 addr += dma->blksz; 89 } 90 91 /* Branch back to beginning. */ 92 dbdma_insert_branch(dma->channel, dma->slots, 0); 93 94 /* STOP command to branch when S0 is asserted. */ 95 dbdma_insert_stop(dma->channel, dma->slots + 1); 96 97 /* Set S0 as the condition to branch to STOP. */ 98 dbdma_set_branch_selector(dma->channel, 1 << 0, 1 << 0); 99 dbdma_set_device_status(dma->channel, 1 << 0, 0); 100 101 dbdma_sync_commands(dma->channel, BUS_DMASYNC_PREWRITE); 102} 103 104#define AOA_BUFFER_SIZE 65536 105 106static struct aoa_dma * 107aoa_dma_create(device_t self) 108{ 109 struct aoa_softc *sc = pcm_getdevinfo(self); 110 struct aoa_dma *dma; 111 bus_dma_tag_t tag; 112 int err; 113 114 err = bus_dma_tag_create(bus_get_dma_tag(self), 115 4, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 116 AOA_BUFFER_SIZE, 1, AOA_BUFFER_SIZE, 0, NULL, NULL, &tag); 117 if (err != 0) 118 return (NULL); 119 120 dma = malloc(sizeof(*dma), M_DEVBUF, M_WAITOK | M_ZERO); 121 dma->tag = tag; 122 dma->bufsz = AOA_BUFFER_SIZE; 123 dma->blksz = PAGE_SIZE; /* initial blocksize */ 124 125 mtx_init(&dma->mutex, "AOA", NULL, MTX_DEF); 126 127 sc->sc_intrp = dma; 128 129 return (dma); 130} 131 132static void 133aoa_dma_delete(struct aoa_dma *dma) 134{ 135 bus_dma_tag_destroy(dma->tag); 136 mtx_destroy(&dma->mutex); 137 free(dma, M_DEVBUF); 138} 139 140static int 141aoa_chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksz) 142{ 143 struct aoa_dma *dma = data; 144 int err, lz; 145 146 DPRINTF(("aoa_chan_setblocksize: blocksz = %u, dma->blksz = %u\n", 147 blocksz, dma->blksz)); 148 KASSERT(!dma->running, ("dma is running")); 149 KASSERT(blocksz > 0, ("bad blocksz")); 150 151 /* Round blocksz down to a power of two... */ 152 __asm volatile ("cntlzw %0,%1" : "=r"(lz) : "r"(blocksz)); 153 blocksz = 1 << (31 - lz); 154 DPRINTF(("blocksz = %u\n", blocksz)); 155 156 /* ...but no more than the buffer. */ 157 if (blocksz > dma->bufsz) 158 blocksz = dma->bufsz; 159 160 err = sndbuf_resize(dma->buf, dma->bufsz / blocksz, blocksz); 161 if (err != 0) { 162 DPRINTF(("sndbuf_resize returned %d\n", err)); 163 return (0); 164 } 165 166 if (blocksz == dma->blksz) 167 return (dma->blksz); 168 169 /* One slot per block plus branch to 0 plus STOP. */ 170 err = dbdma_resize_channel(dma->channel, 2 + dma->bufsz / blocksz); 171 if (err != 0) { 172 DPRINTF(("dbdma_resize_channel returned %d\n", err)); 173 return (0); 174 } 175 176 /* Set the new blocksize. */ 177 dma->blksz = blocksz; 178 aoa_dma_set_program(dma); 179 180 return (dma->blksz); 181} 182 183static int 184aoa_chan_setformat(kobj_t obj, void *data, u_int32_t format) 185{ 186 DPRINTF(("aoa_chan_setformat: format = %u\n", format)); 187 188 if (format != (AFMT_STEREO | AFMT_S16_BE)) 189 return (EINVAL); 190 191 return (0); 192} 193 194static int 195aoa_chan_setspeed(kobj_t obj, void *data, u_int32_t speed) 196{ 197 DPRINTF(("aoa_chan_setspeed: speed = %u\n", speed)); 198 199 return (44100); 200} 201 202static int 203aoa_chan_getptr(kobj_t obj, void *data) 204{ 205 struct aoa_dma *dma = data; 206 207 if (!dma->running) 208 return (0); 209 210 return (dma->slot * dma->blksz); 211} 212 213static void * 214aoa_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, 215 struct pcm_channel *c, int dir) 216{ 217 device_t self = devinfo; 218 struct aoa_softc *sc = pcm_getdevinfo(self); 219 struct aoa_dma *dma; 220 int max_slots, err; 221 222 KASSERT(dir == PCMDIR_PLAY, ("bad dir")); 223 224 dma = aoa_dma_create(self); 225 if (!dma) 226 return (NULL); 227 dma->pcm = c; 228 dma->buf = b; 229 dma->reg = sc->sc_odma; 230 231 /* One slot per block, plus branch to 0 plus STOP. */ 232 max_slots = 2 + dma->bufsz / dma->blksz; 233 err = dbdma_allocate_channel(dma->reg, 0, bus_get_dma_tag(self), 234 max_slots, &dma->channel ); 235 if (err != 0) { 236 aoa_dma_delete(dma); 237 return (NULL); 238 } 239 240 if (sndbuf_alloc(dma->buf, dma->tag, 0, dma->bufsz) != 0) { 241 dbdma_free_channel(dma->channel); 242 aoa_dma_delete(dma); 243 return (NULL); 244 } 245 246 aoa_dma_set_program(dma); 247 248 return (dma); 249} 250 251static int 252aoa_chan_trigger(kobj_t obj, void *data, int go) 253{ 254 struct aoa_dma *dma = data; 255 int i; 256 257 switch (go) { 258 case PCMTRIG_START: 259 260 /* Start the DMA. */ 261 dma->running = 1; 262 263 dma->slot = 0; 264 dbdma_set_current_cmd(dma->channel, dma->slot); 265 266 dbdma_run(dma->channel); 267 268 return (0); 269 270 case PCMTRIG_STOP: 271 case PCMTRIG_ABORT: 272 273 mtx_lock(&dma->mutex); 274 275 dma->running = 0; 276 277 /* Make it branch to the STOP command. */ 278 dbdma_set_device_status(dma->channel, 1 << 0, 1 << 0); 279 280 /* XXX should wait for DBDMA_ACTIVE to clear. */ 281 DELAY(40000); 282 283 /* Reset the DMA. */ 284 dbdma_stop(dma->channel); 285 dbdma_set_device_status(dma->channel, 1 << 0, 0); 286 287 for (i = 0; i < dma->slots; ++i) 288 dbdma_clear_cmd_status(dma->channel, i); 289 290 mtx_unlock(&dma->mutex); 291 292 return (0); 293 } 294 295 return (0); 296} 297 298static int 299aoa_chan_free(kobj_t obj, void *data) 300{ 301 struct aoa_dma *dma = data; 302 303 sndbuf_free(dma->buf); 304 dbdma_free_channel(dma->channel); 305 aoa_dma_delete(dma); 306 307 return (0); 308} 309 310void 311aoa_interrupt(void *arg) 312{ 313 struct aoa_softc *sc = arg; 314 struct aoa_dma *dma; 315 316 if (!(dma = sc->sc_intrp) || !dma->running) 317 return; 318 319 mtx_lock(&dma->mutex); 320 321 while (dbdma_get_cmd_status(dma->channel, dma->slot)) { 322 323 dbdma_clear_cmd_status(dma->channel, dma->slot); 324 dma->slot = (dma->slot + 1) % dma->slots; 325 326 mtx_unlock(&dma->mutex); 327 chn_intr(dma->pcm); 328 mtx_lock(&dma->mutex); 329 } 330 331 mtx_unlock(&dma->mutex); 332} 333 334static u_int32_t sc_fmt[] = { 335 AFMT_S16_BE | AFMT_STEREO, 336 0 337}; 338static struct pcmchan_caps aoa_caps = {44100, 44100, sc_fmt, 0}; 339 340static struct pcmchan_caps * 341aoa_chan_getcaps(kobj_t obj, void *data) 342{ 343 return (&aoa_caps); 344} 345 346static kobj_method_t aoa_chan_methods[] = { 347 KOBJMETHOD(channel_init, aoa_chan_init), 348 KOBJMETHOD(channel_free, aoa_chan_free), 349 KOBJMETHOD(channel_setformat, aoa_chan_setformat), 350 KOBJMETHOD(channel_setspeed, aoa_chan_setspeed), 351 KOBJMETHOD(channel_setblocksize,aoa_chan_setblocksize), 352 KOBJMETHOD(channel_trigger, aoa_chan_trigger), 353 KOBJMETHOD(channel_getptr, aoa_chan_getptr), 354 KOBJMETHOD(channel_getcaps, aoa_chan_getcaps), 355 { 0, 0 } 356}; 357CHANNEL_DECLARE(aoa_chan); 358 359int 360aoa_attach(device_t self, void *sc) 361{ 362 char status[SND_STATUSLEN]; 363 int err; 364 365 if (pcm_register(self, sc, 1, 0)) 366 return (ENXIO); 367 368 err = pcm_getbuffersize(self, AOA_BUFFER_SIZE, AOA_BUFFER_SIZE, 369 AOA_BUFFER_SIZE); 370 DPRINTF(("pcm_getbuffersize returned %d\n", err)); 371 372 pcm_addchan(self, PCMDIR_PLAY, &aoa_chan_class, self); 373 374 snprintf(status, sizeof(status), "at %s", ofw_bus_get_name(self)); 375 pcm_setstatus(self, status); 376 377 return (0); 378} 379 380