2 * Copyright 2008 by Marco Trillo. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
29 * Apple Onboard Audio (AOA).
32 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <machine/dbdma.h>
42 #include <machine/resource.h>
43 #include <machine/bus.h>
45 #include <dev/ofw/ofw_bus.h>
47 #ifdef HAVE_KERNEL_OPTION_HEADERS
51 #include <dev/sound/pcm/sound.h>
52 #include <dev/sound/macio/aoa.h>
58 struct resource *reg; /* DBDMA registers */
59 dbdma_channel_t *channel; /* DBDMA channel */
60 bus_dma_tag_t tag; /* bus_dma tag */
61 struct pcm_channel *pcm; /* PCM channel */
62 struct snd_dbuf *buf; /* PCM buffer */
63 u_int slots; /* # of slots */
64 u_int slot; /* current slot */
65 u_int bufsz; /* buffer size */
66 u_int blksz; /* block size */
71 aoa_dma_set_program(struct aoa_dma *dma)
76 addr = (u_int32_t) sndbuf_getbufaddr(dma->buf);
77 KASSERT(dma->bufsz == sndbuf_getsize(dma->buf), ("bad size"));
79 dma->slots = dma->bufsz / dma->blksz;
81 for (i = 0; i < dma->slots; ++i) {
82 dbdma_insert_command(dma->channel,
84 DBDMA_OUTPUT_MORE, /* command */
87 dma->blksz, /* count */
88 DBDMA_ALWAYS, /* interrupt */
89 DBDMA_COND_TRUE, /* branch */
90 DBDMA_NEVER, /* wait */
91 dma->slots + 1 /* branch_slot */
97 /* Branch back to beginning. */
98 dbdma_insert_branch(dma->channel, dma->slots, 0);
100 /* STOP command to branch when S0 is asserted. */
101 dbdma_insert_stop(dma->channel, dma->slots + 1);
103 /* Set S0 as the condition to branch to STOP. */
104 dbdma_set_branch_selector(dma->channel, 1 << 0, 1 << 0);
105 dbdma_set_device_status(dma->channel, 1 << 0, 0);
107 dbdma_sync_commands(dma->channel, BUS_DMASYNC_PREWRITE);
110 #define AOA_BUFFER_SIZE 65536
112 static struct aoa_dma *
113 aoa_dma_create(struct aoa_softc *sc)
121 err = bus_dma_tag_create(bus_get_dma_tag(self),
122 4, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
123 AOA_BUFFER_SIZE, 1, AOA_BUFFER_SIZE, 0, NULL, NULL, &tag);
127 dma = malloc(sizeof(*dma), M_DEVBUF, M_WAITOK | M_ZERO);
129 dma->bufsz = AOA_BUFFER_SIZE;
130 dma->blksz = PAGE_SIZE; /* initial blocksize */
132 mtx_init(&dma->mutex, "AOA", NULL, MTX_DEF);
140 aoa_dma_delete(struct aoa_dma *dma)
142 bus_dma_tag_destroy(dma->tag);
143 mtx_destroy(&dma->mutex);
148 aoa_chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksz)
150 struct aoa_dma *dma = data;
153 DPRINTF(("aoa_chan_setblocksize: blocksz = %u, dma->blksz = %u\n",
154 blocksz, dma->blksz));
155 KASSERT(!dma->running, ("dma is running"));
156 KASSERT(blocksz > 0, ("bad blocksz"));
158 /* Round blocksz down to a power of two... */
159 __asm volatile ("cntlzw %0,%1" : "=r"(lz) : "r"(blocksz));
160 blocksz = 1 << (31 - lz);
161 DPRINTF(("blocksz = %u\n", blocksz));
163 /* ...but no more than the buffer. */
164 if (blocksz > dma->bufsz)
165 blocksz = dma->bufsz;
167 err = sndbuf_resize(dma->buf, dma->bufsz / blocksz, blocksz);
169 DPRINTF(("sndbuf_resize returned %d\n", err));
173 if (blocksz == dma->blksz)
176 /* One slot per block plus branch to 0 plus STOP. */
177 err = dbdma_resize_channel(dma->channel, 2 + dma->bufsz / blocksz);
179 DPRINTF(("dbdma_resize_channel returned %d\n", err));
183 /* Set the new blocksize. */
184 dma->blksz = blocksz;
185 aoa_dma_set_program(dma);
191 aoa_chan_setformat(kobj_t obj, void *data, u_int32_t format)
193 DPRINTF(("aoa_chan_setformat: format = %u\n", format));
195 if (format != SND_FORMAT(AFMT_S16_BE, 2, 0))
202 aoa_chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
204 DPRINTF(("aoa_chan_setspeed: speed = %u\n", speed));
210 aoa_chan_getptr(kobj_t obj, void *data)
212 struct aoa_dma *dma = data;
217 return (dma->slot * dma->blksz);
221 aoa_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
222 struct pcm_channel *c, int dir)
224 struct aoa_softc *sc = devinfo;
228 KASSERT(dir == PCMDIR_PLAY, ("bad dir"));
230 dma = aoa_dma_create(sc);
235 dma->reg = sc->sc_odma;
237 /* One slot per block, plus branch to 0 plus STOP. */
238 max_slots = 2 + dma->bufsz / dma->blksz;
239 err = dbdma_allocate_channel(dma->reg, 0, bus_get_dma_tag(sc->sc_dev),
240 max_slots, &dma->channel );
246 if (sndbuf_alloc(dma->buf, dma->tag, 0, dma->bufsz) != 0) {
247 dbdma_free_channel(dma->channel);
252 aoa_dma_set_program(dma);
258 aoa_chan_trigger(kobj_t obj, void *data, int go)
260 struct aoa_dma *dma = data;
270 dbdma_set_current_cmd(dma->channel, dma->slot);
272 dbdma_run(dma->channel);
279 mtx_lock(&dma->mutex);
283 /* Make it branch to the STOP command. */
284 dbdma_set_device_status(dma->channel, 1 << 0, 1 << 0);
286 /* XXX should wait for DBDMA_ACTIVE to clear. */
290 dbdma_stop(dma->channel);
291 dbdma_set_device_status(dma->channel, 1 << 0, 0);
293 for (i = 0; i < dma->slots; ++i)
294 dbdma_clear_cmd_status(dma->channel, i);
296 mtx_unlock(&dma->mutex);
305 aoa_chan_free(kobj_t obj, void *data)
307 struct aoa_dma *dma = data;
309 sndbuf_free(dma->buf);
310 dbdma_free_channel(dma->channel);
317 aoa_interrupt(void *xsc)
319 struct aoa_softc *sc = xsc;
322 if (!(dma = sc->sc_intrp) || !dma->running)
325 mtx_lock(&dma->mutex);
327 while (dbdma_get_cmd_status(dma->channel, dma->slot)) {
329 dbdma_clear_cmd_status(dma->channel, dma->slot);
330 dma->slot = (dma->slot + 1) % dma->slots;
332 mtx_unlock(&dma->mutex);
334 mtx_lock(&dma->mutex);
337 mtx_unlock(&dma->mutex);
340 static u_int32_t sc_fmt[] = {
341 SND_FORMAT(AFMT_S16_BE, 2, 0),
344 static struct pcmchan_caps aoa_caps = {44100, 44100, sc_fmt, 0};
346 static struct pcmchan_caps *
347 aoa_chan_getcaps(kobj_t obj, void *data)
352 static kobj_method_t aoa_chan_methods[] = {
353 KOBJMETHOD(channel_init, aoa_chan_init),
354 KOBJMETHOD(channel_free, aoa_chan_free),
355 KOBJMETHOD(channel_setformat, aoa_chan_setformat),
356 KOBJMETHOD(channel_setspeed, aoa_chan_setspeed),
357 KOBJMETHOD(channel_setblocksize,aoa_chan_setblocksize),
358 KOBJMETHOD(channel_trigger, aoa_chan_trigger),
359 KOBJMETHOD(channel_getptr, aoa_chan_getptr),
360 KOBJMETHOD(channel_getcaps, aoa_chan_getcaps),
363 CHANNEL_DECLARE(aoa_chan);
366 aoa_attach(void *xsc)
368 char status[SND_STATUSLEN];
369 struct aoa_softc *sc;
376 if (pcm_register(self, sc, 1, 0))
379 err = pcm_getbuffersize(self, AOA_BUFFER_SIZE, AOA_BUFFER_SIZE,
381 DPRINTF(("pcm_getbuffersize returned %d\n", err));
383 pcm_addchan(self, PCMDIR_PLAY, &aoa_chan_class, sc);
385 snprintf(status, sizeof(status), "at %s", ofw_bus_get_name(self));
386 pcm_setstatus(self, status);