2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2008 by Marco Trillo. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Apple Onboard Audio (AOA).
34 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <machine/dbdma.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
47 #include <dev/ofw/ofw_bus.h>
49 #ifdef HAVE_KERNEL_OPTION_HEADERS
53 #include <dev/sound/pcm/sound.h>
54 #include <dev/sound/macio/aoa.h>
60 struct resource *reg; /* DBDMA registers */
61 dbdma_channel_t *channel; /* DBDMA channel */
62 bus_dma_tag_t tag; /* bus_dma tag */
63 struct pcm_channel *pcm; /* PCM channel */
64 struct snd_dbuf *buf; /* PCM buffer */
65 u_int slots; /* # of slots */
66 u_int slot; /* current slot */
67 u_int bufsz; /* buffer size */
68 u_int blksz; /* block size */
73 aoa_dma_set_program(struct aoa_dma *dma)
78 addr = (u_int32_t) sndbuf_getbufaddr(dma->buf);
79 KASSERT(dma->bufsz == sndbuf_getsize(dma->buf), ("bad size"));
81 dma->slots = dma->bufsz / dma->blksz;
83 for (i = 0; i < dma->slots; ++i) {
84 dbdma_insert_command(dma->channel,
86 DBDMA_OUTPUT_MORE, /* command */
89 dma->blksz, /* count */
90 DBDMA_ALWAYS, /* interrupt */
91 DBDMA_COND_TRUE, /* branch */
92 DBDMA_NEVER, /* wait */
93 dma->slots + 1 /* branch_slot */
99 /* Branch back to beginning. */
100 dbdma_insert_branch(dma->channel, dma->slots, 0);
102 /* STOP command to branch when S0 is asserted. */
103 dbdma_insert_stop(dma->channel, dma->slots + 1);
105 /* Set S0 as the condition to branch to STOP. */
106 dbdma_set_branch_selector(dma->channel, 1 << 0, 1 << 0);
107 dbdma_set_device_status(dma->channel, 1 << 0, 0);
109 dbdma_sync_commands(dma->channel, BUS_DMASYNC_PREWRITE);
112 #define AOA_BUFFER_SIZE 65536
114 static struct aoa_dma *
115 aoa_dma_create(struct aoa_softc *sc)
123 err = bus_dma_tag_create(bus_get_dma_tag(self),
124 4, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
125 AOA_BUFFER_SIZE, 1, AOA_BUFFER_SIZE, 0, NULL, NULL, &tag);
129 dma = malloc(sizeof(*dma), M_DEVBUF, M_WAITOK | M_ZERO);
131 dma->bufsz = AOA_BUFFER_SIZE;
132 dma->blksz = PAGE_SIZE; /* initial blocksize */
134 mtx_init(&dma->mutex, "AOA", NULL, MTX_DEF);
142 aoa_dma_delete(struct aoa_dma *dma)
144 bus_dma_tag_destroy(dma->tag);
145 mtx_destroy(&dma->mutex);
150 aoa_chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksz)
152 struct aoa_dma *dma = data;
155 DPRINTF(("aoa_chan_setblocksize: blocksz = %u, dma->blksz = %u\n",
156 blocksz, dma->blksz));
157 KASSERT(!dma->running, ("dma is running"));
158 KASSERT(blocksz > 0, ("bad blocksz"));
160 /* Round blocksz down to a power of two... */
161 __asm volatile ("cntlzw %0,%1" : "=r"(lz) : "r"(blocksz));
162 blocksz = 1 << (31 - lz);
163 DPRINTF(("blocksz = %u\n", blocksz));
165 /* ...but no more than the buffer. */
166 if (blocksz > dma->bufsz)
167 blocksz = dma->bufsz;
169 err = sndbuf_resize(dma->buf, dma->bufsz / blocksz, blocksz);
171 DPRINTF(("sndbuf_resize returned %d\n", err));
175 if (blocksz == dma->blksz)
178 /* One slot per block plus branch to 0 plus STOP. */
179 err = dbdma_resize_channel(dma->channel, 2 + dma->bufsz / blocksz);
181 DPRINTF(("dbdma_resize_channel returned %d\n", err));
185 /* Set the new blocksize. */
186 dma->blksz = blocksz;
187 aoa_dma_set_program(dma);
193 aoa_chan_setformat(kobj_t obj, void *data, u_int32_t format)
195 DPRINTF(("aoa_chan_setformat: format = %u\n", format));
197 if (format != SND_FORMAT(AFMT_S16_BE, 2, 0))
204 aoa_chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
206 DPRINTF(("aoa_chan_setspeed: speed = %u\n", speed));
212 aoa_chan_getptr(kobj_t obj, void *data)
214 struct aoa_dma *dma = data;
219 return (dma->slot * dma->blksz);
223 aoa_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
224 struct pcm_channel *c, int dir)
226 struct aoa_softc *sc = devinfo;
230 KASSERT(dir == PCMDIR_PLAY, ("bad dir"));
232 dma = aoa_dma_create(sc);
237 dma->reg = sc->sc_odma;
239 /* One slot per block, plus branch to 0 plus STOP. */
240 max_slots = 2 + dma->bufsz / dma->blksz;
241 err = dbdma_allocate_channel(dma->reg, 0, bus_get_dma_tag(sc->sc_dev),
242 max_slots, &dma->channel );
248 if (sndbuf_alloc(dma->buf, dma->tag, 0, dma->bufsz) != 0) {
249 dbdma_free_channel(dma->channel);
254 aoa_dma_set_program(dma);
260 aoa_chan_trigger(kobj_t obj, void *data, int go)
262 struct aoa_dma *dma = data;
272 dbdma_set_current_cmd(dma->channel, dma->slot);
274 dbdma_run(dma->channel);
281 mtx_lock(&dma->mutex);
285 /* Make it branch to the STOP command. */
286 dbdma_set_device_status(dma->channel, 1 << 0, 1 << 0);
288 /* XXX should wait for DBDMA_ACTIVE to clear. */
292 dbdma_stop(dma->channel);
293 dbdma_set_device_status(dma->channel, 1 << 0, 0);
295 for (i = 0; i < dma->slots; ++i)
296 dbdma_clear_cmd_status(dma->channel, i);
298 mtx_unlock(&dma->mutex);
307 aoa_chan_free(kobj_t obj, void *data)
309 struct aoa_dma *dma = data;
311 sndbuf_free(dma->buf);
312 dbdma_free_channel(dma->channel);
319 aoa_interrupt(void *xsc)
321 struct aoa_softc *sc = xsc;
324 if (!(dma = sc->sc_intrp) || !dma->running)
327 mtx_lock(&dma->mutex);
329 while (dbdma_get_cmd_status(dma->channel, dma->slot)) {
331 dbdma_clear_cmd_status(dma->channel, dma->slot);
332 dma->slot = (dma->slot + 1) % dma->slots;
334 mtx_unlock(&dma->mutex);
336 mtx_lock(&dma->mutex);
339 mtx_unlock(&dma->mutex);
342 static u_int32_t sc_fmt[] = {
343 SND_FORMAT(AFMT_S16_BE, 2, 0),
346 static struct pcmchan_caps aoa_caps = {44100, 44100, sc_fmt, 0};
348 static struct pcmchan_caps *
349 aoa_chan_getcaps(kobj_t obj, void *data)
354 static kobj_method_t aoa_chan_methods[] = {
355 KOBJMETHOD(channel_init, aoa_chan_init),
356 KOBJMETHOD(channel_free, aoa_chan_free),
357 KOBJMETHOD(channel_setformat, aoa_chan_setformat),
358 KOBJMETHOD(channel_setspeed, aoa_chan_setspeed),
359 KOBJMETHOD(channel_setblocksize,aoa_chan_setblocksize),
360 KOBJMETHOD(channel_trigger, aoa_chan_trigger),
361 KOBJMETHOD(channel_getptr, aoa_chan_getptr),
362 KOBJMETHOD(channel_getcaps, aoa_chan_getcaps),
365 CHANNEL_DECLARE(aoa_chan);
368 aoa_attach(void *xsc)
370 char status[SND_STATUSLEN];
371 struct aoa_softc *sc;
378 if (pcm_register(self, sc, 1, 0))
381 err = pcm_getbuffersize(self, AOA_BUFFER_SIZE, AOA_BUFFER_SIZE,
383 DPRINTF(("pcm_getbuffersize returned %d\n", err));
385 pcm_addchan(self, PCMDIR_PLAY, &aoa_chan_class, sc);
387 snprintf(status, sizeof(status), "at %s", ofw_bus_get_name(self));
388 pcm_setstatus(self, status);