1 1.2 jmcneill /* $NetBSD: bwdsp.c,v 1.2 2024/01/23 21:49:51 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2024 Jared McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jmcneill * SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include <sys/cdefs.h> 30 1.2 jmcneill __KERNEL_RCSID(0, "$NetBSD: bwdsp.c,v 1.2 2024/01/23 21:49:51 jmcneill Exp $"); 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/param.h> 33 1.1 jmcneill #include <sys/bus.h> 34 1.1 jmcneill #include <sys/cpu.h> 35 1.1 jmcneill #include <sys/device.h> 36 1.1 jmcneill #include <sys/kmem.h> 37 1.1 jmcneill 38 1.1 jmcneill #include <sys/audioio.h> 39 1.1 jmcneill #include <dev/audio/audio_if.h> 40 1.1 jmcneill #include <dev/audio/audio_dai.h> 41 1.1 jmcneill 42 1.1 jmcneill #include "mainbus.h" 43 1.1 jmcneill #include "bwai.h" 44 1.1 jmcneill 45 1.1 jmcneill #define BWDSP_MAP_FLAGS BUS_DMA_NOCACHE 46 1.1 jmcneill 47 1.1 jmcneill #define DSP_DMA_START_ADDR_H 0x30 48 1.1 jmcneill #define DSP_DMA_START_ADDR_L 0x32 49 1.1 jmcneill #define DSP_DMA_CONTROL_LENGTH 0x36 50 1.1 jmcneill #define DSP_DMA_CONTROL_LENGTH_CTRL __BIT(15) 51 1.1 jmcneill #define DSP_DMA_CONTROL_LENGTH_NUM_CLS __BITS(14,0) 52 1.1 jmcneill 53 1.1 jmcneill #define DSP_DMA_ALIGN 32 54 1.1 jmcneill #define DSP_DMA_MAX_BUFSIZE (DSP_DMA_CONTROL_LENGTH_NUM_CLS * 32) 55 1.1 jmcneill 56 1.1 jmcneill struct bwdsp_dma { 57 1.1 jmcneill LIST_ENTRY(bwdsp_dma) dma_list; 58 1.1 jmcneill bus_dmamap_t dma_map; 59 1.1 jmcneill void *dma_addr; 60 1.1 jmcneill size_t dma_size; 61 1.1 jmcneill bus_dma_segment_t dma_segs[1]; 62 1.1 jmcneill int dma_nsegs; 63 1.1 jmcneill }; 64 1.1 jmcneill 65 1.1 jmcneill struct bwdsp_softc { 66 1.1 jmcneill device_t sc_dev; 67 1.1 jmcneill bus_space_tag_t sc_bst; 68 1.1 jmcneill bus_space_handle_t sc_bsh; 69 1.1 jmcneill bus_dma_tag_t sc_dmat; 70 1.1 jmcneill 71 1.1 jmcneill LIST_HEAD(, bwdsp_dma) sc_dmalist; 72 1.1 jmcneill 73 1.1 jmcneill kmutex_t sc_lock; 74 1.1 jmcneill kmutex_t sc_intr_lock; 75 1.1 jmcneill 76 1.1 jmcneill struct audio_format sc_format; 77 1.1 jmcneill 78 1.1 jmcneill audio_dai_tag_t sc_dai; 79 1.1 jmcneill }; 80 1.1 jmcneill 81 1.1 jmcneill #define RD2(sc, reg) \ 82 1.1 jmcneill bus_space_read_2((sc)->sc_bst, (sc)->sc_bsh, (reg)) 83 1.1 jmcneill #define WR2(sc, reg, val) \ 84 1.1 jmcneill bus_space_write_2((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 85 1.1 jmcneill 86 1.1 jmcneill static int 87 1.1 jmcneill bwdsp_allocdma(struct bwdsp_softc *sc, size_t size, 88 1.1 jmcneill size_t align, struct bwdsp_dma *dma) 89 1.1 jmcneill { 90 1.1 jmcneill int error; 91 1.1 jmcneill 92 1.1 jmcneill dma->dma_size = size; 93 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, dma->dma_size, align, 0, 94 1.1 jmcneill dma->dma_segs, 1, &dma->dma_nsegs, BUS_DMA_WAITOK); 95 1.1 jmcneill if (error) 96 1.1 jmcneill return error; 97 1.1 jmcneill 98 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs, 99 1.1 jmcneill dma->dma_size, &dma->dma_addr, BUS_DMA_WAITOK | BWDSP_MAP_FLAGS); 100 1.1 jmcneill if (error) 101 1.1 jmcneill goto free; 102 1.1 jmcneill 103 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, dma->dma_size, dma->dma_nsegs, 104 1.1 jmcneill dma->dma_size, 0, BUS_DMA_WAITOK, &dma->dma_map); 105 1.1 jmcneill if (error) 106 1.1 jmcneill goto unmap; 107 1.1 jmcneill 108 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_addr, 109 1.1 jmcneill dma->dma_size, NULL, BUS_DMA_WAITOK); 110 1.1 jmcneill if (error) 111 1.1 jmcneill goto destroy; 112 1.1 jmcneill 113 1.1 jmcneill return 0; 114 1.1 jmcneill 115 1.1 jmcneill destroy: 116 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, dma->dma_map); 117 1.1 jmcneill unmap: 118 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size); 119 1.1 jmcneill free: 120 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs); 121 1.1 jmcneill 122 1.1 jmcneill return error; 123 1.1 jmcneill } 124 1.1 jmcneill 125 1.1 jmcneill static void 126 1.1 jmcneill bwdsp_freedma(struct bwdsp_softc *sc, struct bwdsp_dma *dma) 127 1.1 jmcneill { 128 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, dma->dma_map); 129 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, dma->dma_map); 130 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size); 131 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs); 132 1.1 jmcneill } 133 1.1 jmcneill 134 1.1 jmcneill static int 135 1.1 jmcneill bwdsp_query_format(void *priv, audio_format_query_t *afp) 136 1.1 jmcneill { 137 1.1 jmcneill struct bwdsp_softc * const sc = priv; 138 1.1 jmcneill 139 1.1 jmcneill return audio_query_format(&sc->sc_format, 1, afp); 140 1.1 jmcneill } 141 1.1 jmcneill 142 1.1 jmcneill static int 143 1.1 jmcneill bwdsp_set_format(void *priv, int setmode, 144 1.1 jmcneill const audio_params_t *play, const audio_params_t *rec, 145 1.1 jmcneill audio_filter_reg_t *pfil, audio_filter_reg_t *rfil) 146 1.1 jmcneill { 147 1.1 jmcneill struct bwdsp_softc * const sc = priv; 148 1.1 jmcneill 149 1.1 jmcneill return audio_dai_mi_set_format(sc->sc_dai, setmode, play, rec, 150 1.1 jmcneill pfil, rfil); 151 1.1 jmcneill } 152 1.1 jmcneill 153 1.1 jmcneill static int 154 1.1 jmcneill bwdsp_set_port(void *priv, mixer_ctrl_t *mc) 155 1.1 jmcneill { 156 1.1 jmcneill struct bwdsp_softc * const sc = priv; 157 1.1 jmcneill 158 1.1 jmcneill return audio_dai_set_port(sc->sc_dai, mc); 159 1.1 jmcneill } 160 1.1 jmcneill 161 1.1 jmcneill static int 162 1.1 jmcneill bwdsp_get_port(void *priv, mixer_ctrl_t *mc) 163 1.1 jmcneill { 164 1.1 jmcneill struct bwdsp_softc * const sc = priv; 165 1.1 jmcneill 166 1.1 jmcneill return audio_dai_get_port(sc->sc_dai, mc); 167 1.1 jmcneill } 168 1.1 jmcneill 169 1.1 jmcneill static int 170 1.1 jmcneill bwdsp_query_devinfo(void *priv, mixer_devinfo_t *di) 171 1.1 jmcneill { 172 1.1 jmcneill struct bwdsp_softc * const sc = priv; 173 1.1 jmcneill 174 1.1 jmcneill return audio_dai_query_devinfo(sc->sc_dai, di); 175 1.1 jmcneill } 176 1.1 jmcneill 177 1.1 jmcneill static void * 178 1.1 jmcneill bwdsp_allocm(void *priv, int dir, size_t size) 179 1.1 jmcneill { 180 1.1 jmcneill struct bwdsp_softc * const sc = priv; 181 1.1 jmcneill struct bwdsp_dma *dma; 182 1.1 jmcneill int error; 183 1.1 jmcneill 184 1.1 jmcneill dma = kmem_alloc(sizeof(*dma), KM_SLEEP); 185 1.1 jmcneill 186 1.1 jmcneill error = bwdsp_allocdma(sc, size, DSP_DMA_ALIGN, dma); 187 1.1 jmcneill if (error) { 188 1.1 jmcneill kmem_free(dma, sizeof(*dma)); 189 1.1 jmcneill device_printf(sc->sc_dev, "couldn't allocate DMA memory (%d)\n", 190 1.1 jmcneill error); 191 1.1 jmcneill return NULL; 192 1.1 jmcneill } 193 1.1 jmcneill 194 1.1 jmcneill LIST_INSERT_HEAD(&sc->sc_dmalist, dma, dma_list); 195 1.1 jmcneill 196 1.1 jmcneill return dma->dma_addr; 197 1.1 jmcneill } 198 1.1 jmcneill 199 1.1 jmcneill static void 200 1.1 jmcneill bwdsp_freem(void *priv, void *addr, size_t size) 201 1.1 jmcneill { 202 1.1 jmcneill struct bwdsp_softc * const sc = priv; 203 1.1 jmcneill struct bwdsp_dma *dma; 204 1.1 jmcneill 205 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list) 206 1.1 jmcneill if (dma->dma_addr == addr) { 207 1.1 jmcneill bwdsp_freedma(sc, dma); 208 1.1 jmcneill LIST_REMOVE(dma, dma_list); 209 1.1 jmcneill kmem_free(dma, sizeof(*dma)); 210 1.1 jmcneill break; 211 1.1 jmcneill } 212 1.1 jmcneill } 213 1.1 jmcneill 214 1.1 jmcneill static int 215 1.1 jmcneill bwdsp_getdev(void *priv, struct audio_device *adev) 216 1.1 jmcneill { 217 1.1 jmcneill snprintf(adev->name, sizeof(adev->name), "Broadway DSP"); 218 1.1 jmcneill snprintf(adev->version, sizeof(adev->version), ""); 219 1.1 jmcneill snprintf(adev->config, sizeof(adev->config), "bwdsp"); 220 1.1 jmcneill 221 1.1 jmcneill return 0; 222 1.1 jmcneill } 223 1.1 jmcneill 224 1.1 jmcneill static int 225 1.1 jmcneill bwdsp_get_props(void *priv) 226 1.1 jmcneill { 227 1.1 jmcneill return AUDIO_PROP_PLAYBACK; 228 1.1 jmcneill } 229 1.1 jmcneill 230 1.1 jmcneill static int 231 1.1 jmcneill bwdsp_round_blocksize(void *priv, int bs, int mode, 232 1.1 jmcneill const audio_params_t *params) 233 1.1 jmcneill { 234 1.1 jmcneill bs = roundup(bs, DSP_DMA_ALIGN); 235 1.1 jmcneill if (bs > DSP_DMA_MAX_BUFSIZE) { 236 1.1 jmcneill bs = DSP_DMA_MAX_BUFSIZE; 237 1.1 jmcneill } 238 1.1 jmcneill return bs; 239 1.1 jmcneill } 240 1.1 jmcneill 241 1.1 jmcneill static size_t 242 1.1 jmcneill bwdsp_round_buffersize(void *priv, int dir, size_t bufsize) 243 1.1 jmcneill { 244 1.1 jmcneill if (bufsize > DSP_DMA_MAX_BUFSIZE) { 245 1.1 jmcneill bufsize = DSP_DMA_MAX_BUFSIZE; 246 1.1 jmcneill } 247 1.1 jmcneill return bufsize; 248 1.1 jmcneill } 249 1.1 jmcneill 250 1.1 jmcneill static void 251 1.1 jmcneill bwdsp_transfer(struct bwdsp_softc *sc, uint32_t phys_addr, size_t bufsize) 252 1.1 jmcneill { 253 1.1 jmcneill if (bufsize != 0) { 254 1.1 jmcneill WR2(sc, DSP_DMA_START_ADDR_H, phys_addr >> 16); 255 1.1 jmcneill WR2(sc, DSP_DMA_START_ADDR_L, phys_addr & 0xffff); 256 1.1 jmcneill WR2(sc, DSP_DMA_CONTROL_LENGTH, 257 1.1 jmcneill DSP_DMA_CONTROL_LENGTH_CTRL | (bufsize / 32)); 258 1.1 jmcneill } else { 259 1.1 jmcneill WR2(sc, DSP_DMA_CONTROL_LENGTH, 0); 260 1.1 jmcneill } 261 1.1 jmcneill } 262 1.1 jmcneill 263 1.1 jmcneill static int 264 1.1 jmcneill bwdsp_trigger_output(void *priv, void *start, void *end, int blksize, 265 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params) 266 1.1 jmcneill { 267 1.1 jmcneill struct bwdsp_softc * const sc = priv; 268 1.1 jmcneill struct bwdsp_dma *dma; 269 1.1 jmcneill bus_addr_t pstart; 270 1.1 jmcneill bus_size_t psize; 271 1.1 jmcneill int error; 272 1.1 jmcneill 273 1.1 jmcneill pstart = 0; 274 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start; 275 1.1 jmcneill 276 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list) 277 1.1 jmcneill if (dma->dma_addr == start) { 278 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr; 279 1.1 jmcneill break; 280 1.1 jmcneill } 281 1.1 jmcneill if (pstart == 0) { 282 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start); 283 1.1 jmcneill return EINVAL; 284 1.1 jmcneill } 285 1.1 jmcneill 286 1.1 jmcneill error = audio_dai_trigger(sc->sc_dai, start, end, blksize, 287 1.1 jmcneill intr, intrarg, params, AUMODE_PLAY); 288 1.1 jmcneill if (error != 0) { 289 1.1 jmcneill return error; 290 1.1 jmcneill } 291 1.1 jmcneill 292 1.1 jmcneill /* Start DMA transfer */ 293 1.1 jmcneill bwdsp_transfer(sc, pstart, psize); 294 1.1 jmcneill 295 1.1 jmcneill return 0; 296 1.1 jmcneill } 297 1.1 jmcneill 298 1.1 jmcneill static int 299 1.1 jmcneill bwdsp_halt_output(void *priv) 300 1.1 jmcneill { 301 1.1 jmcneill struct bwdsp_softc * const sc = priv; 302 1.1 jmcneill 303 1.1 jmcneill /* Stop DMA transfer */ 304 1.1 jmcneill bwdsp_transfer(sc, 0, 0); 305 1.1 jmcneill 306 1.1 jmcneill return audio_dai_halt(sc->sc_dai, AUMODE_PLAY); 307 1.1 jmcneill } 308 1.1 jmcneill 309 1.1 jmcneill static void 310 1.1 jmcneill bwdsp_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread) 311 1.1 jmcneill { 312 1.1 jmcneill struct bwdsp_softc * const sc = priv; 313 1.1 jmcneill 314 1.1 jmcneill *intr = &sc->sc_intr_lock; 315 1.1 jmcneill *thread = &sc->sc_lock; 316 1.1 jmcneill } 317 1.1 jmcneill 318 1.1 jmcneill static const struct audio_hw_if bwdsp_hw_if = { 319 1.1 jmcneill .query_format = bwdsp_query_format, 320 1.1 jmcneill .set_format = bwdsp_set_format, 321 1.1 jmcneill .allocm = bwdsp_allocm, 322 1.1 jmcneill .freem = bwdsp_freem, 323 1.1 jmcneill .getdev = bwdsp_getdev, 324 1.1 jmcneill .set_port = bwdsp_set_port, 325 1.1 jmcneill .get_port = bwdsp_get_port, 326 1.1 jmcneill .query_devinfo = bwdsp_query_devinfo, 327 1.1 jmcneill .get_props = bwdsp_get_props, 328 1.1 jmcneill .round_blocksize = bwdsp_round_blocksize, 329 1.1 jmcneill .round_buffersize = bwdsp_round_buffersize, 330 1.1 jmcneill .trigger_output = bwdsp_trigger_output, 331 1.1 jmcneill .halt_output = bwdsp_halt_output, 332 1.1 jmcneill .get_locks = bwdsp_get_locks, 333 1.1 jmcneill }; 334 1.1 jmcneill 335 1.1 jmcneill static void 336 1.1 jmcneill bwdsp_late_attach(device_t dev) 337 1.1 jmcneill { 338 1.1 jmcneill struct bwdsp_softc * const sc = device_private(dev); 339 1.1 jmcneill 340 1.1 jmcneill sc->sc_dai = bwai_dsp_init(&sc->sc_intr_lock); 341 1.1 jmcneill if (sc->sc_dai == NULL) { 342 1.1 jmcneill aprint_error_dev(dev, "can't find bwai device\n"); 343 1.1 jmcneill return; 344 1.1 jmcneill } 345 1.1 jmcneill 346 1.1 jmcneill audio_attach_mi(&bwdsp_hw_if, sc, dev); 347 1.1 jmcneill } 348 1.1 jmcneill 349 1.1 jmcneill static int 350 1.1 jmcneill bwdsp_match(device_t parent, cfdata_t cf, void *aux) 351 1.1 jmcneill { 352 1.1 jmcneill struct mainbus_attach_args * const maa = aux; 353 1.1 jmcneill 354 1.1 jmcneill return strcmp(maa->maa_name, "bwdsp") == 0; 355 1.1 jmcneill } 356 1.1 jmcneill 357 1.1 jmcneill static void 358 1.1 jmcneill bwdsp_attach(device_t parent, device_t self, void *aux) 359 1.1 jmcneill { 360 1.1 jmcneill struct bwdsp_softc * const sc = device_private(self); 361 1.1 jmcneill struct mainbus_attach_args * const maa = aux; 362 1.1 jmcneill bus_addr_t addr = maa->maa_addr; 363 1.1 jmcneill bus_size_t size = 0x200; 364 1.1 jmcneill 365 1.1 jmcneill sc->sc_dev = self; 366 1.1 jmcneill sc->sc_bst = maa->maa_bst; 367 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 368 1.1 jmcneill aprint_error(": couldn't map registers\n"); 369 1.1 jmcneill return; 370 1.1 jmcneill } 371 1.1 jmcneill sc->sc_dmat = maa->maa_dmat; 372 1.1 jmcneill LIST_INIT(&sc->sc_dmalist); 373 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 374 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED); 375 1.1 jmcneill 376 1.1 jmcneill aprint_naive("\n"); 377 1.1 jmcneill aprint_normal(": DSP\n"); 378 1.1 jmcneill 379 1.1 jmcneill sc->sc_format.mode = AUMODE_PLAY; 380 1.1 jmcneill sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_BE; 381 1.1 jmcneill sc->sc_format.validbits = 16; 382 1.1 jmcneill sc->sc_format.precision = 16; 383 1.1 jmcneill sc->sc_format.channels = 2; 384 1.1 jmcneill sc->sc_format.channel_mask = AUFMT_STEREO; 385 1.1 jmcneill sc->sc_format.frequency_type = 1; 386 1.1 jmcneill sc->sc_format.frequency[0] = 48000; 387 1.1 jmcneill 388 1.1 jmcneill config_defer(self, bwdsp_late_attach); 389 1.1 jmcneill } 390 1.1 jmcneill 391 1.1 jmcneill CFATTACH_DECL_NEW(bwdsp, sizeof(struct bwdsp_softc), 392 1.1 jmcneill bwdsp_match, bwdsp_attach, NULL, NULL); 393