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$ 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 50 struct 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 64 static void 65 aoa_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 106 static struct aoa_dma * 107 aoa_dma_create(struct aoa_softc *sc) 108 { 109 struct aoa_dma *dma; 110 bus_dma_tag_t tag; 111 int err; 112 device_t self; 113 114 self = sc->sc_dev; 115 err = bus_dma_tag_create(bus_get_dma_tag(self), 116 4, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 117 AOA_BUFFER_SIZE, 1, AOA_BUFFER_SIZE, 0, NULL, NULL, &tag); 118 if (err != 0) 119 return (NULL); 120 121 dma = malloc(sizeof(*dma), M_DEVBUF, M_WAITOK | M_ZERO); 122 dma->tag = tag; 123 dma->bufsz = AOA_BUFFER_SIZE; 124 dma->blksz = PAGE_SIZE; /* initial blocksize */ 125 126 mtx_init(&dma->mutex, "AOA", NULL, MTX_DEF); 127 128 sc->sc_intrp = dma; 129 130 return (dma); 131 } 132 133 static void 134 aoa_dma_delete(struct aoa_dma *dma) 135 { 136 bus_dma_tag_destroy(dma->tag); 137 mtx_destroy(&dma->mutex); 138 free(dma, M_DEVBUF); 139 } 140 141 static int 142 aoa_chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksz) 143 { 144 struct aoa_dma *dma = data; 145 int err, lz; 146 147 DPRINTF(("aoa_chan_setblocksize: blocksz = %u, dma->blksz = %u\n", 148 blocksz, dma->blksz)); 149 KASSERT(!dma->running, ("dma is running")); 150 KASSERT(blocksz > 0, ("bad blocksz")); 151 152 /* Round blocksz down to a power of two... */ 153 __asm volatile ("cntlzw %0,%1" : "=r"(lz) : "r"(blocksz)); 154 blocksz = 1 << (31 - lz); 155 DPRINTF(("blocksz = %u\n", blocksz)); 156 157 /* ...but no more than the buffer. */ 158 if (blocksz > dma->bufsz) 159 blocksz = dma->bufsz; 160 161 err = sndbuf_resize(dma->buf, dma->bufsz / blocksz, blocksz); 162 if (err != 0) { 163 DPRINTF(("sndbuf_resize returned %d\n", err)); 164 return (0); 165 } 166 167 if (blocksz == dma->blksz) 168 return (dma->blksz); 169 170 /* One slot per block plus branch to 0 plus STOP. */ 171 err = dbdma_resize_channel(dma->channel, 2 + dma->bufsz / blocksz); 172 if (err != 0) { 173 DPRINTF(("dbdma_resize_channel returned %d\n", err)); 174 return (0); 175 } 176 177 /* Set the new blocksize. */ 178 dma->blksz = blocksz; 179 aoa_dma_set_program(dma); 180 181 return (dma->blksz); 182 } 183 184 static int 185 aoa_chan_setformat(kobj_t obj, void *data, u_int32_t format) 186 { 187 DPRINTF(("aoa_chan_setformat: format = %u\n", format)); 188 189 if (format != (AFMT_STEREO | AFMT_S16_BE)) 190 return (EINVAL); 191 192 return (0); 193 } 194 195 static int 196 aoa_chan_setspeed(kobj_t obj, void *data, u_int32_t speed) 197 { 198 DPRINTF(("aoa_chan_setspeed: speed = %u\n", speed)); 199 200 return (44100); 201 } 202 203 static int 204 aoa_chan_getptr(kobj_t obj, void *data) 205 { 206 struct aoa_dma *dma = data; 207 208 if (!dma->running) 209 return (0); 210 211 return (dma->slot * dma->blksz); 212 } 213 214 static void * 215 aoa_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, 216 struct pcm_channel *c, int dir) 217 { 218 struct aoa_softc *sc = devinfo; 219 struct aoa_dma *dma; 220 int max_slots, err; 221 222 KASSERT(dir == PCMDIR_PLAY, ("bad dir")); 223 224 dma = aoa_dma_create(sc); 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(sc->sc_dev), 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 251 static int 252 aoa_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 298 static int 299 aoa_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 310 void 311 aoa_interrupt(void *xsc) 312 { 313 struct aoa_softc *sc = xsc; 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 334 static u_int32_t sc_fmt[] = { 335 AFMT_S16_BE | AFMT_STEREO, 336 0 337 }; 338 static struct pcmchan_caps aoa_caps = {44100, 44100, sc_fmt, 0}; 339 340 static struct pcmchan_caps * 341 aoa_chan_getcaps(kobj_t obj, void *data) 342 { 343 return (&aoa_caps); 344 } 345 346 static 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 }; 357 CHANNEL_DECLARE(aoa_chan); 358 359 int 360 aoa_attach(void *xsc) 361 { 362 char status[SND_STATUSLEN]; 363 struct aoa_softc *sc; 364 device_t self; 365 int err; 366 367 sc = xsc; 368 self = sc->sc_dev; 369 370 if (pcm_register(self, sc, 1, 0)) 371 return (ENXIO); 372 373 err = pcm_getbuffersize(self, AOA_BUFFER_SIZE, AOA_BUFFER_SIZE, 374 AOA_BUFFER_SIZE); 375 DPRINTF(("pcm_getbuffersize returned %d\n", err)); 376 377 pcm_addchan(self, PCMDIR_PLAY, &aoa_chan_class, sc); 378 379 snprintf(status, sizeof(status), "at %s", ofw_bus_get_name(self)); 380 pcm_setstatus(self, status); 381 382 return (0); 383 } 384 385