| 1 | /* $NetBSD: esa.c,v 1.60 2014/03/29 19:28:24 christos Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2001-2008 Jared D. McNeill <jmcneill@invisible.ca> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. The name of the author may not be used to endorse or promote products |
| 13 | * derived from this software without specific prior written permission. |
| 14 | * |
| 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 |
| 25 | * SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | /* |
| 29 | * ESS Allegro-1 / Maestro3 Audio Driver |
| 30 | * |
| 31 | * Based on the FreeBSD maestro3 driver and the NetBSD eap driver. |
| 32 | * Original driver by Don Kim. |
| 33 | * |
| 34 | * The list management code could possibly be written better, but what |
| 35 | * we have right now does the job nicely. Thanks to Zach Brown <zab@zabbo.net> |
| 36 | * and Andrew MacDonald <amac@epsilon.yi.org> for helping me debug the |
| 37 | * problems with the original list management code present in the Linux |
| 38 | * driver. |
| 39 | */ |
| 40 | |
| 41 | #include <sys/cdefs.h> |
| 42 | __KERNEL_RCSID(0, "$NetBSD: esa.c,v 1.60 2014/03/29 19:28:24 christos Exp $" ); |
| 43 | |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/errno.h> |
| 46 | #include <sys/null.h> |
| 47 | #include <sys/param.h> |
| 48 | #include <sys/systm.h> |
| 49 | #include <sys/kmem.h> |
| 50 | #include <sys/device.h> |
| 51 | #include <sys/conf.h> |
| 52 | #include <sys/exec.h> |
| 53 | #include <sys/select.h> |
| 54 | #include <sys/audioio.h> |
| 55 | #include <sys/bus.h> |
| 56 | #include <sys/intr.h> |
| 57 | |
| 58 | #include <dev/audio_if.h> |
| 59 | #include <dev/mulaw.h> |
| 60 | #include <dev/auconv.h> |
| 61 | |
| 62 | #include <dev/ic/ac97var.h> |
| 63 | #include <dev/ic/ac97reg.h> |
| 64 | |
| 65 | #include <dev/pci/pcidevs.h> |
| 66 | #include <dev/pci/pcivar.h> |
| 67 | #include <dev/pci/esareg.h> |
| 68 | #include <dev/pci/esadsp.h> |
| 69 | #include <dev/pci/esavar.h> |
| 70 | |
| 71 | #define PCI_CBIO 0x10 |
| 72 | |
| 73 | #define ESA_DAC_DATA 0x1100 |
| 74 | |
| 75 | enum { |
| 76 | ESS_ALLEGRO1, |
| 77 | ESS_MAESTRO3 |
| 78 | }; |
| 79 | |
| 80 | static const struct esa_card_type { |
| 81 | uint16_t pci_vendor_id; |
| 82 | uint16_t pci_product_id; |
| 83 | int type; |
| 84 | int delay1, delay2; |
| 85 | } esa_card_types[] = { |
| 86 | { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ALLEGRO1, |
| 87 | ESS_ALLEGRO1, 50, 800 }, |
| 88 | { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3, |
| 89 | ESS_MAESTRO3, 20, 500 }, |
| 90 | { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2, |
| 91 | ESS_MAESTRO3, 20, 500 }, |
| 92 | { 0, 0, 0, 0, 0 } |
| 93 | }; |
| 94 | |
| 95 | static struct audio_device esa_device = { |
| 96 | "ESS Allegro" , |
| 97 | "" , |
| 98 | "esa" |
| 99 | }; |
| 100 | |
| 101 | static int esa_match(device_t, cfdata_t, void *); |
| 102 | static void esa_attach(device_t, device_t, void *); |
| 103 | static int esa_detach(device_t, int); |
| 104 | static void esa_childdet(device_t, device_t); |
| 105 | |
| 106 | /* audio(9) functions */ |
| 107 | static int esa_query_encoding(void *, struct audio_encoding *); |
| 108 | static int esa_set_params(void *, int, int, audio_params_t *, |
| 109 | audio_params_t *, stream_filter_list_t *, |
| 110 | stream_filter_list_t *); |
| 111 | static int esa_round_blocksize(void *, int, int, |
| 112 | const audio_params_t *); |
| 113 | static int esa_commit_settings(void *); |
| 114 | static int esa_halt_output(void *); |
| 115 | static int esa_halt_input(void *); |
| 116 | static int esa_set_port(void *, mixer_ctrl_t *); |
| 117 | static int esa_get_port(void *, mixer_ctrl_t *); |
| 118 | static int esa_query_devinfo(void *, mixer_devinfo_t *); |
| 119 | static void * esa_malloc(void *, int, size_t); |
| 120 | static void esa_free(void *, void *, size_t); |
| 121 | static int esa_getdev(void *, struct audio_device *); |
| 122 | static size_t esa_round_buffersize(void *, int, size_t); |
| 123 | static int esa_get_props(void *); |
| 124 | static int esa_trigger_output(void *, void *, void *, int, |
| 125 | void (*)(void *), void *, |
| 126 | const audio_params_t *); |
| 127 | static int esa_trigger_input(void *, void *, void *, int, |
| 128 | void (*)(void *), void *, |
| 129 | const audio_params_t *); |
| 130 | static void esa_get_locks(void *, kmutex_t **, kmutex_t **); |
| 131 | |
| 132 | static int esa_intr(void *); |
| 133 | static int esa_allocmem(struct esa_softc *, size_t, size_t, |
| 134 | struct esa_dma *); |
| 135 | static int esa_freemem(struct esa_softc *, struct esa_dma *); |
| 136 | static paddr_t esa_mappage(void *, void *, off_t, int); |
| 137 | |
| 138 | /* Supporting subroutines */ |
| 139 | static uint16_t esa_read_assp(struct esa_softc *, uint16_t, uint16_t); |
| 140 | static void esa_write_assp(struct esa_softc *, uint16_t, uint16_t, |
| 141 | uint16_t); |
| 142 | static int esa_init_codec(struct esa_softc *); |
| 143 | static int esa_attach_codec(void *, struct ac97_codec_if *); |
| 144 | static int esa_read_codec(void *, uint8_t, uint16_t *); |
| 145 | static int esa_write_codec(void *, uint8_t, uint16_t); |
| 146 | static int esa_reset_codec(void *); |
| 147 | static enum ac97_host_flags esa_flags_codec(void *); |
| 148 | static int esa_wait(struct esa_softc *); |
| 149 | static int esa_init(struct esa_softc *); |
| 150 | static void esa_config(struct esa_softc *); |
| 151 | static uint8_t esa_assp_halt(struct esa_softc *); |
| 152 | static void esa_codec_reset(struct esa_softc *); |
| 153 | static int esa_amp_enable(struct esa_softc *); |
| 154 | static void esa_enable_interrupts(struct esa_softc *); |
| 155 | static uint32_t esa_get_pointer(struct esa_softc *, |
| 156 | struct esa_channel *); |
| 157 | |
| 158 | /* list management */ |
| 159 | static int esa_add_list(struct esa_voice *, struct esa_list *, |
| 160 | uint16_t, int); |
| 161 | static void esa_remove_list(struct esa_voice *, struct esa_list *, |
| 162 | int); |
| 163 | |
| 164 | /* power management */ |
| 165 | static bool esa_suspend(device_t, const pmf_qual_t *); |
| 166 | static bool esa_resume(device_t, const pmf_qual_t *); |
| 167 | |
| 168 | |
| 169 | #define ESA_NENCODINGS 8 |
| 170 | static audio_encoding_t esa_encoding[ESA_NENCODINGS] = { |
| 171 | { 0, AudioEulinear, AUDIO_ENCODING_ULINEAR, 8, 0 }, |
| 172 | { 1, AudioEmulaw, AUDIO_ENCODING_ULAW, 8, |
| 173 | AUDIO_ENCODINGFLAG_EMULATED }, |
| 174 | { 2, AudioEalaw, AUDIO_ENCODING_ALAW, 8, AUDIO_ENCODINGFLAG_EMULATED }, |
| 175 | { 3, AudioEslinear, AUDIO_ENCODING_SLINEAR, 8, |
| 176 | AUDIO_ENCODINGFLAG_EMULATED }, /* XXX: Are you sure? */ |
| 177 | { 4, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 16, 0 }, |
| 178 | { 5, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 16, |
| 179 | AUDIO_ENCODINGFLAG_EMULATED }, |
| 180 | { 6, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16, |
| 181 | AUDIO_ENCODINGFLAG_EMULATED }, |
| 182 | { 7, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 16, |
| 183 | AUDIO_ENCODINGFLAG_EMULATED } |
| 184 | }; |
| 185 | |
| 186 | #define ESA_NFORMATS 4 |
| 187 | static const struct audio_format esa_formats[ESA_NFORMATS] = { |
| 188 | {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16, |
| 189 | 2, AUFMT_STEREO, 0, {ESA_MINRATE, ESA_MAXRATE}}, |
| 190 | {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16, |
| 191 | 1, AUFMT_MONAURAL, 0, {ESA_MINRATE, ESA_MAXRATE}}, |
| 192 | {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8, |
| 193 | 2, AUFMT_STEREO, 0, {ESA_MINRATE, ESA_MAXRATE}}, |
| 194 | {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8, |
| 195 | 1, AUFMT_MONAURAL, 0, {ESA_MINRATE, ESA_MAXRATE}}, |
| 196 | }; |
| 197 | |
| 198 | static const struct audio_hw_if esa_hw_if = { |
| 199 | NULL, /* open */ |
| 200 | NULL, /* close */ |
| 201 | NULL, /* drain */ |
| 202 | esa_query_encoding, |
| 203 | esa_set_params, |
| 204 | esa_round_blocksize, |
| 205 | esa_commit_settings, |
| 206 | NULL, /* init_output */ |
| 207 | NULL, /* init_input */ |
| 208 | NULL, /* start_output */ |
| 209 | NULL, /* start_input */ |
| 210 | esa_halt_output, |
| 211 | esa_halt_input, |
| 212 | NULL, /* speaker_ctl */ |
| 213 | esa_getdev, |
| 214 | NULL, /* getfd */ |
| 215 | esa_set_port, |
| 216 | esa_get_port, |
| 217 | esa_query_devinfo, |
| 218 | esa_malloc, |
| 219 | esa_free, |
| 220 | esa_round_buffersize, |
| 221 | esa_mappage, |
| 222 | esa_get_props, |
| 223 | esa_trigger_output, |
| 224 | esa_trigger_input, |
| 225 | NULL, /* dev_ioctl */ |
| 226 | esa_get_locks, |
| 227 | }; |
| 228 | |
| 229 | CFATTACH_DECL2_NEW(esa, sizeof(struct esa_softc), esa_match, esa_attach, |
| 230 | esa_detach, NULL, NULL, esa_childdet); |
| 231 | |
| 232 | /* |
| 233 | * audio(9) functions |
| 234 | */ |
| 235 | |
| 236 | static int |
| 237 | esa_query_encoding(void *hdl, struct audio_encoding *ae) |
| 238 | { |
| 239 | |
| 240 | if (ae->index < 0 || ae->index >= ESA_NENCODINGS) |
| 241 | return EINVAL; |
| 242 | *ae = esa_encoding[ae->index]; |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static int |
| 248 | esa_set_params(void *hdl, int setmode, int usemode, |
| 249 | audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil, |
| 250 | stream_filter_list_t *rfil) |
| 251 | { |
| 252 | struct esa_voice *vc; |
| 253 | struct esa_channel *ch; |
| 254 | struct audio_params *p; |
| 255 | stream_filter_list_t *fil; |
| 256 | int mode, i; |
| 257 | |
| 258 | vc = hdl; |
| 259 | for (mode = AUMODE_RECORD; mode != -1; |
| 260 | mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) { |
| 261 | if ((setmode & mode) == 0) |
| 262 | continue; |
| 263 | |
| 264 | switch (mode) { |
| 265 | case AUMODE_PLAY: |
| 266 | p = play; |
| 267 | ch = &vc->play; |
| 268 | fil = pfil; |
| 269 | break; |
| 270 | case AUMODE_RECORD: |
| 271 | p = rec; |
| 272 | ch = &vc->rec; |
| 273 | fil = rfil; |
| 274 | break; |
| 275 | default: |
| 276 | return EINVAL; |
| 277 | } |
| 278 | |
| 279 | if (p->sample_rate < ESA_MINRATE || |
| 280 | p->sample_rate > ESA_MAXRATE || |
| 281 | (p->precision != 8 && p->precision != 16) || |
| 282 | (p->channels < 1 || p->channels > 2)) |
| 283 | return EINVAL; |
| 284 | |
| 285 | i = auconv_set_converter(esa_formats, ESA_NFORMATS, |
| 286 | mode, p, FALSE, fil); |
| 287 | if (i < 0) |
| 288 | return EINVAL; |
| 289 | if (fil->req_size > 0) |
| 290 | p = &fil->filters[0].param; |
| 291 | ch->mode = *p; |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int |
| 298 | esa_commit_settings(void *hdl) |
| 299 | { |
| 300 | struct esa_voice *vc; |
| 301 | struct esa_softc *sc; |
| 302 | const audio_params_t *p; |
| 303 | const audio_params_t *r; |
| 304 | uint32_t data; |
| 305 | uint32_t freq; |
| 306 | int data_bytes; |
| 307 | |
| 308 | vc = hdl; |
| 309 | sc = device_private(vc->parent); |
| 310 | p = &vc->play.mode; |
| 311 | r = &vc->rec.mode; |
| 312 | data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) + |
| 313 | (ESA_MINISRC_IN_BUFFER_SIZE & ~1) + |
| 314 | (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) |
| 315 | &~ 255; |
| 316 | /* playback */ |
| 317 | vc->play.data_offset = ESA_DAC_DATA + (data_bytes * vc->index); |
| 318 | if (p->channels == 1) |
| 319 | data = 1; |
| 320 | else |
| 321 | data = 0; |
| 322 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 323 | vc->play.data_offset + ESA_SRC3_MODE_OFFSET, |
| 324 | data); |
| 325 | if (p->precision == 8) |
| 326 | data = 1; |
| 327 | else |
| 328 | data = 0; |
| 329 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 330 | vc->play.data_offset + ESA_SRC3_WORD_LENGTH_OFFSET, |
| 331 | data); |
| 332 | if ((freq = ((p->sample_rate << 15) + 24000) / 48000) != 0) { |
| 333 | freq--; |
| 334 | } |
| 335 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 336 | vc->play.data_offset + ESA_CDATA_FREQUENCY, freq); |
| 337 | |
| 338 | /* recording */ |
| 339 | vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * vc->index) + |
| 340 | (data_bytes / 2); |
| 341 | if (r->channels == 1) |
| 342 | data = 1; |
| 343 | else |
| 344 | data = 0; |
| 345 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 346 | vc->rec.data_offset + ESA_SRC3_MODE_OFFSET, |
| 347 | data); |
| 348 | if (r->precision == 8) |
| 349 | data = 1; |
| 350 | else |
| 351 | data = 0; |
| 352 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 353 | vc->rec.data_offset + ESA_SRC3_WORD_LENGTH_OFFSET, |
| 354 | data); |
| 355 | if ((freq = ((r->sample_rate << 15) + 24000) / 48000) != 0) { |
| 356 | freq--; |
| 357 | } |
| 358 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 359 | vc->rec.data_offset + ESA_CDATA_FREQUENCY, freq); |
| 360 | |
| 361 | return 0; |
| 362 | }; |
| 363 | |
| 364 | static int |
| 365 | esa_round_blocksize(void *hdl, int bs, int mode, |
| 366 | const audio_params_t *param) |
| 367 | { |
| 368 | |
| 369 | return bs & ~0x20; /* Be conservative; align to 32 bytes */ |
| 370 | } |
| 371 | |
| 372 | static int |
| 373 | esa_halt_output(void *hdl) |
| 374 | { |
| 375 | struct esa_voice *vc; |
| 376 | struct esa_softc *sc; |
| 377 | bus_space_tag_t iot; |
| 378 | bus_space_handle_t ioh; |
| 379 | uint16_t data; |
| 380 | |
| 381 | vc = hdl; |
| 382 | sc = device_private(vc->parent); |
| 383 | iot = sc->sc_iot; |
| 384 | ioh = sc->sc_ioh; |
| 385 | if (vc->play.active == 0) |
| 386 | return 0; |
| 387 | |
| 388 | vc->play.active = 0; |
| 389 | |
| 390 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 391 | ESA_CDATA_INSTANCE_READY + vc->play.data_offset, 0); |
| 392 | |
| 393 | sc->sc_ntimers--; |
| 394 | if (sc->sc_ntimers == 0) { |
| 395 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 396 | ESA_KDATA_TIMER_COUNT_RELOAD, 0); |
| 397 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 398 | ESA_KDATA_TIMER_COUNT_CURRENT, 0); |
| 399 | data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL); |
| 400 | bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, |
| 401 | data & ~ESA_CLKRUN_GEN_ENABLE); |
| 402 | } |
| 403 | |
| 404 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 405 | ESA_KDATA_MIXER_TASK_NUMBER, |
| 406 | sc->mixer_list.indexmap[vc->index]); |
| 407 | /* remove ourselves from the packed lists */ |
| 408 | esa_remove_list(vc, &sc->mixer_list, vc->index); |
| 409 | esa_remove_list(vc, &sc->dma_list, vc->index); |
| 410 | esa_remove_list(vc, &sc->msrc_list, vc->index); |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int |
| 416 | esa_halt_input(void *hdl) |
| 417 | { |
| 418 | struct esa_voice *vc; |
| 419 | struct esa_softc *sc; |
| 420 | bus_space_tag_t iot; |
| 421 | bus_space_handle_t ioh; |
| 422 | uint32_t data; |
| 423 | |
| 424 | vc = hdl; |
| 425 | sc = device_private(vc->parent); |
| 426 | iot = sc->sc_iot; |
| 427 | ioh = sc->sc_ioh; |
| 428 | if (vc->rec.active == 0) |
| 429 | return 0; |
| 430 | |
| 431 | vc->rec.active = 0; |
| 432 | |
| 433 | sc->sc_ntimers--; |
| 434 | if (sc->sc_ntimers == 0) { |
| 435 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 436 | ESA_KDATA_TIMER_COUNT_RELOAD, 0); |
| 437 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 438 | ESA_KDATA_TIMER_COUNT_CURRENT, 0); |
| 439 | data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL); |
| 440 | bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, |
| 441 | data & ~ESA_CLKRUN_GEN_ENABLE); |
| 442 | } |
| 443 | |
| 444 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, vc->rec.data_offset + |
| 445 | ESA_CDATA_INSTANCE_READY, 0); |
| 446 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST, |
| 447 | 0); |
| 448 | |
| 449 | /* remove ourselves from the packed lists */ |
| 450 | esa_remove_list(vc, &sc->adc1_list, vc->index + ESA_NUM_VOICES); |
| 451 | esa_remove_list(vc, &sc->dma_list, vc->index + ESA_NUM_VOICES); |
| 452 | esa_remove_list(vc, &sc->msrc_list, vc->index + ESA_NUM_VOICES); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static void * |
| 458 | esa_malloc(void *hdl, int direction, size_t size) |
| 459 | { |
| 460 | struct esa_voice *vc; |
| 461 | struct esa_softc *sc; |
| 462 | struct esa_dma *p; |
| 463 | int error; |
| 464 | |
| 465 | p = kmem_alloc(sizeof(*p), KM_SLEEP); |
| 466 | if (p == NULL) |
| 467 | return NULL; |
| 468 | vc = hdl; |
| 469 | sc = device_private(vc->parent); |
| 470 | error = esa_allocmem(sc, size, 16, p); |
| 471 | if (error) { |
| 472 | kmem_free(p, sizeof(*p)); |
| 473 | aprint_error_dev(sc->sc_dev, |
| 474 | "%s: not enough memory\n" , __func__); |
| 475 | return 0; |
| 476 | } |
| 477 | p->next = vc->dma; |
| 478 | vc->dma = p; |
| 479 | |
| 480 | return KERNADDR(p); |
| 481 | } |
| 482 | |
| 483 | static void |
| 484 | esa_free(void *hdl, void *addr, size_t size) |
| 485 | { |
| 486 | struct esa_voice *vc; |
| 487 | struct esa_softc *sc; |
| 488 | struct esa_dma *p; |
| 489 | struct esa_dma **pp; |
| 490 | |
| 491 | vc = hdl; |
| 492 | sc = device_private(vc->parent); |
| 493 | for (pp = &vc->dma; (p = *pp) != NULL; pp = &p->next) |
| 494 | if (KERNADDR(p) == addr) { |
| 495 | esa_freemem(sc, p); |
| 496 | *pp = p->next; |
| 497 | kmem_free(p, sizeof(*p)); |
| 498 | return; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | static int |
| 503 | esa_getdev(void *hdl, struct audio_device *ret) |
| 504 | { |
| 505 | |
| 506 | *ret = esa_device; |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static int |
| 511 | esa_set_port(void *hdl, mixer_ctrl_t *mc) |
| 512 | { |
| 513 | struct esa_voice *vc; |
| 514 | struct esa_softc *sc; |
| 515 | |
| 516 | vc = hdl; |
| 517 | sc = device_private(vc->parent); |
| 518 | return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, mc); |
| 519 | } |
| 520 | |
| 521 | static int |
| 522 | esa_get_port(void *hdl, mixer_ctrl_t *mc) |
| 523 | { |
| 524 | struct esa_voice *vc; |
| 525 | struct esa_softc *sc; |
| 526 | |
| 527 | vc = hdl; |
| 528 | sc = device_private(vc->parent); |
| 529 | return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, mc); |
| 530 | } |
| 531 | |
| 532 | static int |
| 533 | esa_query_devinfo(void *hdl, mixer_devinfo_t *di) |
| 534 | { |
| 535 | struct esa_voice *vc; |
| 536 | struct esa_softc *sc; |
| 537 | |
| 538 | vc = hdl; |
| 539 | sc = device_private(vc->parent); |
| 540 | return sc->codec_if->vtbl->query_devinfo(sc->codec_if, di); |
| 541 | } |
| 542 | |
| 543 | static size_t |
| 544 | esa_round_buffersize(void *hdl, int direction, size_t bufsize) |
| 545 | { |
| 546 | |
| 547 | return bufsize; |
| 548 | } |
| 549 | |
| 550 | static int |
| 551 | esa_get_props(void *hdl) |
| 552 | { |
| 553 | |
| 554 | return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX; |
| 555 | } |
| 556 | |
| 557 | static int |
| 558 | esa_trigger_output(void *hdl, void *start, void *end, int blksize, |
| 559 | void (*intr)(void *), void *intrarg, const audio_params_t *param) |
| 560 | { |
| 561 | struct esa_voice *vc; |
| 562 | struct esa_softc *sc; |
| 563 | struct esa_dma *p; |
| 564 | bus_space_tag_t iot; |
| 565 | bus_space_handle_t ioh; |
| 566 | size_t size; |
| 567 | uint32_t data, bufaddr, i; |
| 568 | int data_bytes, dac_data, dsp_in_size; |
| 569 | int dsp_out_size, dsp_in_buf, dsp_out_buf; |
| 570 | |
| 571 | vc = hdl; |
| 572 | sc = device_private(vc->parent); |
| 573 | iot = sc->sc_iot; |
| 574 | ioh = sc->sc_ioh; |
| 575 | data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) + |
| 576 | (ESA_MINISRC_IN_BUFFER_SIZE & ~1) + |
| 577 | (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) & ~255; |
| 578 | dac_data = ESA_DAC_DATA + (data_bytes * vc->index); |
| 579 | dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x20 * 2); |
| 580 | dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x20 * 2); |
| 581 | dsp_in_buf = dac_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2); |
| 582 | dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1; |
| 583 | |
| 584 | if (vc->play.active) |
| 585 | return EINVAL; |
| 586 | |
| 587 | for (p = vc->dma; p && KERNADDR(p) != start; p = p->next) |
| 588 | continue; |
| 589 | if (p == NULL) { |
| 590 | aprint_error_dev(sc->sc_dev, "%s: bad addr %p\n" , __func__, |
| 591 | start); |
| 592 | return EINVAL; |
| 593 | } |
| 594 | |
| 595 | vc->play.active = 1; |
| 596 | vc->play.intr = intr; |
| 597 | vc->play.arg = intrarg; |
| 598 | vc->play.pos = 0; |
| 599 | vc->play.count = 0; |
| 600 | vc->play.buf = start; |
| 601 | vc->play.bufsize = size = (size_t)(((char *)end - (char *)start)); |
| 602 | vc->play.blksize = blksize; |
| 603 | bufaddr = DMAADDR(p); |
| 604 | vc->play.start = bufaddr; |
| 605 | |
| 606 | #define LO(x) ((x) & 0x0000ffff) |
| 607 | #define HI(x) ((x) >> 16) |
| 608 | |
| 609 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 610 | ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr)); |
| 611 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 612 | ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr)); |
| 613 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 614 | ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size)); |
| 615 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 616 | ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size)); |
| 617 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 618 | ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr)); |
| 619 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 620 | ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr)); |
| 621 | |
| 622 | /* DSP buffers */ |
| 623 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 624 | ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf); |
| 625 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 626 | ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2)); |
| 627 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 628 | ESA_CDATA_IN_BUF_HEAD, dsp_in_buf); |
| 629 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 630 | ESA_CDATA_IN_BUF_TAIL, dsp_in_buf); |
| 631 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 632 | ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf); |
| 633 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 634 | ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2)); |
| 635 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 636 | ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf); |
| 637 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 638 | ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf); |
| 639 | |
| 640 | /* Some per-client initializers */ |
| 641 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 642 | ESA_SRC3_DIRECTION_OFFSET + 12, dac_data + 40 + 8); |
| 643 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 644 | ESA_SRC3_DIRECTION_OFFSET + 19, 0x400 + ESA_MINISRC_COEF_LOC); |
| 645 | /* Enable or disable low-pass filter? (0xff if rate > 45000) */ |
| 646 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 647 | ESA_SRC3_DIRECTION_OFFSET + 22, |
| 648 | vc->play.mode.sample_rate > 45000 ? 0xff : 0); |
| 649 | /* Tell it which way DMA is going */ |
| 650 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 651 | ESA_CDATA_DMA_CONTROL, |
| 652 | ESA_DMACONTROL_AUTOREPEAT + ESA_DMAC_PAGE3_SELECTOR + |
| 653 | ESA_DMAC_BLOCKF_SELECTOR); |
| 654 | |
| 655 | /* Set an armload of static initializers */ |
| 656 | for (i = 0; i < __arraycount(esa_playvals); i++) |
| 657 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 658 | esa_playvals[i].addr, esa_playvals[i].val); |
| 659 | |
| 660 | /* Put us in the packed task lists */ |
| 661 | esa_add_list(vc, &sc->msrc_list, dac_data >> ESA_DP_SHIFT_COUNT, |
| 662 | vc->index); |
| 663 | esa_add_list(vc, &sc->dma_list, dac_data >> ESA_DP_SHIFT_COUNT, |
| 664 | vc->index); |
| 665 | esa_add_list(vc, &sc->mixer_list, dac_data >> ESA_DP_SHIFT_COUNT, |
| 666 | vc->index); |
| 667 | #undef LO |
| 668 | #undef HI |
| 669 | |
| 670 | sc->sc_ntimers++; |
| 671 | |
| 672 | if (sc->sc_ntimers == 1) { |
| 673 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 674 | ESA_KDATA_TIMER_COUNT_RELOAD, 240); |
| 675 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 676 | ESA_KDATA_TIMER_COUNT_CURRENT, 240); |
| 677 | data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL); |
| 678 | bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, |
| 679 | data | ESA_CLKRUN_GEN_ENABLE); |
| 680 | } |
| 681 | |
| 682 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + |
| 683 | ESA_CDATA_INSTANCE_READY, 1); |
| 684 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 685 | ESA_KDATA_MIXER_TASK_NUMBER, |
| 686 | sc->mixer_list.indexmap[vc->index]); |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | static int |
| 692 | esa_trigger_input(void *hdl, void *start, void *end, int blksize, |
| 693 | void (*intr)(void *), void *intrarg, const audio_params_t *param) |
| 694 | { |
| 695 | struct esa_voice *vc; |
| 696 | struct esa_softc *sc; |
| 697 | struct esa_dma *p; |
| 698 | bus_space_tag_t iot; |
| 699 | bus_space_handle_t ioh; |
| 700 | uint32_t data, bufaddr, i; |
| 701 | size_t size; |
| 702 | int data_bytes, adc_data, dsp_in_size; |
| 703 | int dsp_out_size, dsp_in_buf, dsp_out_buf; |
| 704 | |
| 705 | vc = hdl; |
| 706 | sc = device_private(vc->parent); |
| 707 | iot = sc->sc_iot; |
| 708 | ioh = sc->sc_ioh; |
| 709 | data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) + |
| 710 | (ESA_MINISRC_IN_BUFFER_SIZE & ~1) + |
| 711 | (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) & ~255; |
| 712 | adc_data = ESA_DAC_DATA + (data_bytes * vc->index) + (data_bytes / 2); |
| 713 | dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x10 * 2); |
| 714 | dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x10 * 2); |
| 715 | dsp_in_buf = adc_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2); |
| 716 | dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1; |
| 717 | |
| 718 | vc->rec.data_offset = adc_data; |
| 719 | |
| 720 | /* We only support 1 recording channel */ |
| 721 | if (vc->index > 0) |
| 722 | return ENODEV; |
| 723 | |
| 724 | if (vc->rec.active) |
| 725 | return EINVAL; |
| 726 | |
| 727 | for (p = vc->dma; p && KERNADDR(p) != start; p = p->next) |
| 728 | continue; |
| 729 | if (p == NULL) { |
| 730 | aprint_error_dev(sc->sc_dev, "%s: bad addr %p\n" , |
| 731 | __func__, start); |
| 732 | return EINVAL; |
| 733 | } |
| 734 | |
| 735 | vc->rec.active = 1; |
| 736 | vc->rec.intr = intr; |
| 737 | vc->rec.arg = intrarg; |
| 738 | vc->rec.pos = 0; |
| 739 | vc->rec.count = 0; |
| 740 | vc->rec.buf = start; |
| 741 | vc->rec.bufsize = size = (size_t)(((char *)end - (char *)start)); |
| 742 | vc->rec.blksize = blksize; |
| 743 | bufaddr = DMAADDR(p); |
| 744 | vc->rec.start = bufaddr; |
| 745 | |
| 746 | #define LO(x) ((x) & 0x0000ffff) |
| 747 | #define HI(x) ((x) >> 16) |
| 748 | |
| 749 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 750 | ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr)); |
| 751 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 752 | ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr)); |
| 753 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 754 | ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size)); |
| 755 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 756 | ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size)); |
| 757 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 758 | ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr)); |
| 759 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 760 | ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr)); |
| 761 | |
| 762 | /* DSP buffers */ |
| 763 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 764 | ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf); |
| 765 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 766 | ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2)); |
| 767 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 768 | ESA_CDATA_IN_BUF_HEAD, dsp_in_buf); |
| 769 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 770 | ESA_CDATA_IN_BUF_TAIL, dsp_in_buf); |
| 771 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 772 | ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf); |
| 773 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 774 | ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2)); |
| 775 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 776 | ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf); |
| 777 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 778 | ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf); |
| 779 | |
| 780 | /* Some per-client initializers */ |
| 781 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 782 | ESA_SRC3_DIRECTION_OFFSET + 12, adc_data + 40 + 8); |
| 783 | /* Tell it which way DMA is going */ |
| 784 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 785 | ESA_CDATA_DMA_CONTROL, |
| 786 | ESA_DMACONTROL_DIRECTION + ESA_DMACONTROL_AUTOREPEAT + |
| 787 | ESA_DMAC_PAGE3_SELECTOR + ESA_DMAC_BLOCKF_SELECTOR); |
| 788 | |
| 789 | /* Set an armload of static initializers */ |
| 790 | for (i = 0; i < __arraycount(esa_recvals); i++) |
| 791 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 792 | esa_recvals[i].addr, esa_recvals[i].val); |
| 793 | |
| 794 | /* Put us in the packed task lists */ |
| 795 | esa_add_list(vc, &sc->adc1_list, adc_data >> ESA_DP_SHIFT_COUNT, |
| 796 | vc->index + ESA_NUM_VOICES); |
| 797 | esa_add_list(vc, &sc->msrc_list, adc_data >> ESA_DP_SHIFT_COUNT, |
| 798 | vc->index + ESA_NUM_VOICES); |
| 799 | esa_add_list(vc, &sc->dma_list, adc_data >> ESA_DP_SHIFT_COUNT, |
| 800 | vc->index + ESA_NUM_VOICES); |
| 801 | #undef LO |
| 802 | #undef HI |
| 803 | |
| 804 | sc->sc_ntimers++; |
| 805 | if (sc->sc_ntimers == 1) { |
| 806 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 807 | ESA_KDATA_TIMER_COUNT_RELOAD, 240); |
| 808 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 809 | ESA_KDATA_TIMER_COUNT_CURRENT, 240); |
| 810 | data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL); |
| 811 | bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, |
| 812 | data | ESA_CLKRUN_GEN_ENABLE); |
| 813 | } |
| 814 | |
| 815 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + |
| 816 | ESA_CDATA_INSTANCE_READY, 1); |
| 817 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST, |
| 818 | 1); |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | /* Interrupt handler */ |
| 824 | static int |
| 825 | esa_intr(void *hdl) |
| 826 | { |
| 827 | struct esa_softc *sc; |
| 828 | struct esa_voice *vc; |
| 829 | bus_space_tag_t iot; |
| 830 | bus_space_handle_t ioh; |
| 831 | uint8_t status; |
| 832 | uint32_t pos; |
| 833 | uint32_t diff; |
| 834 | uint32_t blksize; |
| 835 | int i; |
| 836 | |
| 837 | sc = hdl; |
| 838 | mutex_spin_enter(&sc->sc_intr_lock); |
| 839 | |
| 840 | iot = sc->sc_iot; |
| 841 | ioh = sc->sc_ioh; |
| 842 | |
| 843 | status = bus_space_read_1(iot, ioh, ESA_HOST_INT_STATUS); |
| 844 | if (status == 0xff) { |
| 845 | mutex_spin_exit(&sc->sc_intr_lock); |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | /* ack the interrupt */ |
| 850 | bus_space_write_1(iot, ioh, ESA_HOST_INT_STATUS, status); |
| 851 | |
| 852 | if (status & ESA_HV_INT_PENDING) { |
| 853 | uint8_t event; |
| 854 | |
| 855 | aprint_normal_dev(sc->sc_dev, "hardware volume interrupt\n" ); |
| 856 | event = bus_space_read_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER); |
| 857 | switch(event) { |
| 858 | case 0xaa: /* volume up */ |
| 859 | pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP); |
| 860 | break; |
| 861 | case 0x66: /* volume down */ |
| 862 | pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN); |
| 863 | break; |
| 864 | case 0x88: /* mute */ |
| 865 | pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE); |
| 866 | break; |
| 867 | default: |
| 868 | aprint_normal_dev(sc->sc_dev, |
| 869 | "unknown hwvol event 0x%02x\n" , event); |
| 870 | break; |
| 871 | } |
| 872 | bus_space_write_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER, 0x88); |
| 873 | } |
| 874 | |
| 875 | if ((status & ESA_ASSP_INT_PENDING) == 0 || |
| 876 | (bus_space_read_1(iot, ioh, |
| 877 | ESA_ASSP_CONTROL_B) & ESA_STOP_ASSP_CLOCK) != 0 || |
| 878 | (bus_space_read_1(iot, ioh, |
| 879 | ESA_ASSP_HOST_INT_STATUS) & ESA_DSP2HOST_REQ_TIMER) == 0) { |
| 880 | mutex_spin_exit(&sc->sc_intr_lock); |
| 881 | return 1; |
| 882 | } |
| 883 | |
| 884 | bus_space_write_1(iot, ioh, ESA_ASSP_HOST_INT_STATUS, |
| 885 | ESA_DSP2HOST_REQ_TIMER); |
| 886 | |
| 887 | for (i = 0; i < ESA_NUM_VOICES; i++) { |
| 888 | vc = &sc->voice[i]; |
| 889 | |
| 890 | if (vc->play.active) { |
| 891 | pos = esa_get_pointer(sc, &vc->play) % vc->play.bufsize; |
| 892 | diff = (vc->play.bufsize + pos - vc->play.pos) % |
| 893 | vc->play.bufsize; |
| 894 | |
| 895 | vc->play.pos = pos; |
| 896 | vc->play.count += diff; |
| 897 | blksize = vc->play.blksize; |
| 898 | |
| 899 | while (vc->play.count >= blksize) { |
| 900 | vc->play.count -= blksize; |
| 901 | (*vc->play.intr)(vc->play.arg); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | if (vc->rec.active) { |
| 906 | pos = esa_get_pointer(sc, &vc->rec) % vc->rec.bufsize; |
| 907 | diff = (vc->rec.bufsize + pos - vc->rec.pos) % |
| 908 | vc->rec.bufsize; |
| 909 | |
| 910 | vc->rec.pos = pos; |
| 911 | vc->rec.count += diff; |
| 912 | blksize = vc->rec.blksize; |
| 913 | |
| 914 | while (vc->rec.count >= blksize) { |
| 915 | vc->rec.count -= blksize; |
| 916 | (*vc->rec.intr)(vc->rec.arg); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | mutex_spin_exit(&sc->sc_intr_lock); |
| 922 | return 1; |
| 923 | } |
| 924 | |
| 925 | static int |
| 926 | esa_allocmem(struct esa_softc *sc, size_t size, size_t align, |
| 927 | struct esa_dma *p) |
| 928 | { |
| 929 | int error; |
| 930 | |
| 931 | p->size = size; |
| 932 | error = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0, |
| 933 | p->segs, __arraycount(p->segs), |
| 934 | &p->nsegs, BUS_DMA_WAITOK); |
| 935 | if (error) |
| 936 | return error; |
| 937 | |
| 938 | error = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size, |
| 939 | &p->addr, BUS_DMA_WAITOK | BUS_DMA_COHERENT); |
| 940 | if (error) |
| 941 | goto free; |
| 942 | |
| 943 | error = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0, |
| 944 | BUS_DMA_WAITOK, &p->map); |
| 945 | if (error) |
| 946 | goto unmap; |
| 947 | |
| 948 | error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL, |
| 949 | BUS_DMA_WAITOK); |
| 950 | if (error) |
| 951 | goto destroy; |
| 952 | |
| 953 | return 0; |
| 954 | |
| 955 | destroy: |
| 956 | bus_dmamap_destroy(sc->sc_dmat, p->map); |
| 957 | unmap: |
| 958 | bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); |
| 959 | free: |
| 960 | bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs); |
| 961 | |
| 962 | return error; |
| 963 | } |
| 964 | |
| 965 | static int |
| 966 | esa_freemem(struct esa_softc *sc, struct esa_dma *p) |
| 967 | { |
| 968 | |
| 969 | bus_dmamap_unload(sc->sc_dmat, p->map); |
| 970 | bus_dmamap_destroy(sc->sc_dmat, p->map); |
| 971 | bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); |
| 972 | bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs); |
| 973 | |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | /* |
| 978 | * Supporting Subroutines |
| 979 | */ |
| 980 | |
| 981 | static int |
| 982 | esa_match(device_t dev, cfdata_t match, void *aux) |
| 983 | { |
| 984 | struct pci_attach_args *pa; |
| 985 | |
| 986 | pa = (struct pci_attach_args *)aux; |
| 987 | switch (PCI_VENDOR(pa->pa_id)) { |
| 988 | case PCI_VENDOR_ESSTECH: |
| 989 | switch(PCI_PRODUCT(pa->pa_id)) { |
| 990 | case PCI_PRODUCT_ESSTECH_ALLEGRO1: |
| 991 | case PCI_PRODUCT_ESSTECH_MAESTRO3: |
| 992 | case PCI_PRODUCT_ESSTECH_MAESTRO3_2: |
| 993 | return 1; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | static void |
| 1001 | esa_attach(device_t parent, device_t self, void *aux) |
| 1002 | { |
| 1003 | struct esa_softc *sc; |
| 1004 | struct pci_attach_args *pa; |
| 1005 | pcitag_t tag; |
| 1006 | pci_chipset_tag_t pc; |
| 1007 | pci_intr_handle_t ih; |
| 1008 | const struct esa_card_type *card; |
| 1009 | const char *intrstr; |
| 1010 | uint32_t data; |
| 1011 | int revision, i, error; |
| 1012 | char intrbuf[PCI_INTRSTR_LEN]; |
| 1013 | |
| 1014 | sc = device_private(self); |
| 1015 | pa = (struct pci_attach_args *)aux; |
| 1016 | tag = pa->pa_tag; |
| 1017 | pc = pa->pa_pc; |
| 1018 | |
| 1019 | pci_aprint_devinfo(pa, "Audio controller" ); |
| 1020 | |
| 1021 | revision = PCI_REVISION(pa->pa_class); |
| 1022 | |
| 1023 | for (card = esa_card_types; card->pci_vendor_id; card++) |
| 1024 | if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id && |
| 1025 | PCI_PRODUCT(pa->pa_id) == card->pci_product_id) { |
| 1026 | sc->type = card->type; |
| 1027 | sc->delay1 = card->delay1; |
| 1028 | sc->delay2 = card->delay2; |
| 1029 | break; |
| 1030 | } |
| 1031 | |
| 1032 | data = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG); |
| 1033 | data |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
| 1034 | | PCI_COMMAND_MASTER_ENABLE); |
| 1035 | pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, data); |
| 1036 | |
| 1037 | /* Map I/O register */ |
| 1038 | if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, |
| 1039 | &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) { |
| 1040 | aprint_error_dev(sc->sc_dev, "can't map i/o space\n" ); |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | /* Initialize softc */ |
| 1045 | sc->sc_dev = self; |
| 1046 | sc->sc_tag = tag; |
| 1047 | sc->sc_pct = pc; |
| 1048 | sc->sc_dmat = pa->pa_dmat; |
| 1049 | |
| 1050 | mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); |
| 1051 | mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO); |
| 1052 | |
| 1053 | /* Map and establish an interrupt */ |
| 1054 | if (pci_intr_map(pa, &ih)) { |
| 1055 | aprint_error_dev(sc->sc_dev, "can't map interrupt\n" ); |
| 1056 | mutex_destroy(&sc->sc_lock); |
| 1057 | mutex_destroy(&sc->sc_intr_lock); |
| 1058 | return; |
| 1059 | } |
| 1060 | intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf)); |
| 1061 | sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, esa_intr, sc); |
| 1062 | if (sc->sc_ih == NULL) { |
| 1063 | aprint_error_dev(sc->sc_dev, "can't establish interrupt" ); |
| 1064 | if (intrstr != NULL) |
| 1065 | aprint_error(" at %s" , intrstr); |
| 1066 | aprint_error("\n" ); |
| 1067 | mutex_destroy(&sc->sc_lock); |
| 1068 | mutex_destroy(&sc->sc_intr_lock); |
| 1069 | return; |
| 1070 | } |
| 1071 | aprint_normal_dev(sc->sc_dev, "interrupting at %s\n" , intrstr); |
| 1072 | |
| 1073 | /* power up chip */ |
| 1074 | if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self, |
| 1075 | pci_activate_null)) && error != EOPNOTSUPP) { |
| 1076 | aprint_error_dev(sc->sc_dev, "cannot activate %d\n" , error); |
| 1077 | mutex_destroy(&sc->sc_lock); |
| 1078 | mutex_destroy(&sc->sc_intr_lock); |
| 1079 | return; |
| 1080 | } |
| 1081 | |
| 1082 | /* Init chip */ |
| 1083 | if (esa_init(sc) == -1) { |
| 1084 | aprint_error_dev(sc->sc_dev, |
| 1085 | "esa_attach: unable to initialize the card\n" ); |
| 1086 | mutex_destroy(&sc->sc_lock); |
| 1087 | mutex_destroy(&sc->sc_intr_lock); |
| 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | /* create suspend save area */ |
| 1092 | sc->savememsz = sizeof(uint16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH |
| 1093 | + ESA_REV_B_DATA_MEMORY_LENGTH + 1); |
| 1094 | sc->savemem = kmem_zalloc(sc->savememsz, KM_SLEEP); |
| 1095 | if (sc->savemem == NULL) { |
| 1096 | aprint_error_dev(sc->sc_dev, |
| 1097 | "unable to allocate suspend buffer\n" ); |
| 1098 | mutex_destroy(&sc->sc_lock); |
| 1099 | mutex_destroy(&sc->sc_intr_lock); |
| 1100 | return; |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * Every card I've seen has had their channels swapped with respect |
| 1105 | * to the mixer. Ie: |
| 1106 | * $ mixerctl -w outputs.master=0,191 |
| 1107 | * Would result in the _right_ speaker being turned off. |
| 1108 | * |
| 1109 | * So, we will swap the left and right mixer channels to compensate |
| 1110 | * for this. |
| 1111 | * |
| 1112 | * XXX PR# 23620: The Dell C810 channels are not swapped. Match |
| 1113 | * on revision ID for now; this is probably wrong. |
| 1114 | */ |
| 1115 | if (revision == 0x10 && sc->type == ESS_MAESTRO3) |
| 1116 | sc->codec_flags = 0; |
| 1117 | else |
| 1118 | sc->codec_flags = AC97_HOST_SWAPPED_CHANNELS; |
| 1119 | |
| 1120 | /* initialize list management structures */ |
| 1121 | sc->mixer_list.mem_addr = ESA_KDATA_MIXER_XFER0; |
| 1122 | sc->mixer_list.max = ESA_MAX_VIRTUAL_MIXER_CHANNELS; |
| 1123 | sc->adc1_list.mem_addr = ESA_KDATA_ADC1_XFER0; |
| 1124 | sc->adc1_list.max = ESA_MAX_VIRTUAL_ADC1_CHANNELS; |
| 1125 | sc->dma_list.mem_addr = ESA_KDATA_DMA_XFER0; |
| 1126 | sc->dma_list.max = ESA_MAX_VIRTUAL_DMA_CHANNELS; |
| 1127 | sc->msrc_list.mem_addr = ESA_KDATA_INSTANCE0_MINISRC; |
| 1128 | sc->msrc_list.max = ESA_MAX_INSTANCE_MINISRC; |
| 1129 | |
| 1130 | /* initialize index maps */ |
| 1131 | for (i = 0; i < ESA_NUM_VOICES * 2; i++) { |
| 1132 | sc->mixer_list.indexmap[i] = -1; |
| 1133 | sc->msrc_list.indexmap[i] = -1; |
| 1134 | sc->dma_list.indexmap[i] = -1; |
| 1135 | sc->adc1_list.indexmap[i] = -1; |
| 1136 | } |
| 1137 | |
| 1138 | /* Attach AC97 host interface */ |
| 1139 | sc->host_if.arg = sc; |
| 1140 | sc->host_if.attach = esa_attach_codec; |
| 1141 | sc->host_if.read = esa_read_codec; |
| 1142 | sc->host_if.write = esa_write_codec; |
| 1143 | sc->host_if.reset = esa_reset_codec; |
| 1144 | sc->host_if.flags = esa_flags_codec; |
| 1145 | |
| 1146 | if (ac97_attach(&sc->host_if, self, &sc->sc_lock) != 0) { |
| 1147 | mutex_destroy(&sc->sc_lock); |
| 1148 | mutex_destroy(&sc->sc_intr_lock); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | /* Attach audio interface. */ |
| 1153 | for (i = 0; i < ESA_NUM_VOICES; i++) { |
| 1154 | sc->voice[i].parent = sc->sc_dev; |
| 1155 | sc->voice[i].index = i; |
| 1156 | sc->sc_audiodev[i] = |
| 1157 | audio_attach_mi(&esa_hw_if, &sc->voice[i], sc->sc_dev); |
| 1158 | } |
| 1159 | |
| 1160 | if (!pmf_device_register(self, esa_suspend, esa_resume)) |
| 1161 | aprint_error_dev(self, "couldn't establish power handler\n" ); |
| 1162 | |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | void |
| 1167 | esa_childdet(device_t self, device_t child) |
| 1168 | { |
| 1169 | struct esa_softc *sc = device_private(self); |
| 1170 | int i; |
| 1171 | |
| 1172 | for (i = 0; i < ESA_NUM_VOICES; i++) { |
| 1173 | if (sc->sc_audiodev[i] == child) { |
| 1174 | sc->sc_audiodev[i] = NULL; |
| 1175 | break; |
| 1176 | } |
| 1177 | } |
| 1178 | KASSERT(i < ESA_NUM_VOICES); |
| 1179 | } |
| 1180 | |
| 1181 | static int |
| 1182 | esa_detach(device_t self, int flags) |
| 1183 | { |
| 1184 | struct esa_softc *sc; |
| 1185 | int i; |
| 1186 | |
| 1187 | sc = device_private(self); |
| 1188 | for (i = 0; i < ESA_NUM_VOICES; i++) { |
| 1189 | if (sc->sc_audiodev[i] != NULL) |
| 1190 | config_detach(sc->sc_audiodev[i], flags); |
| 1191 | } |
| 1192 | |
| 1193 | if (sc->sc_ih != NULL) |
| 1194 | pci_intr_disestablish(sc->sc_pct, sc->sc_ih); |
| 1195 | if (sc->sc_ios) |
| 1196 | bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); |
| 1197 | |
| 1198 | kmem_free(sc->savemem, sc->savememsz); |
| 1199 | mutex_destroy(&sc->sc_lock); |
| 1200 | mutex_destroy(&sc->sc_intr_lock); |
| 1201 | |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | static uint16_t |
| 1206 | esa_read_assp(struct esa_softc *sc, uint16_t region, uint16_t index) |
| 1207 | { |
| 1208 | uint16_t data; |
| 1209 | bus_space_tag_t iot; |
| 1210 | bus_space_handle_t ioh; |
| 1211 | |
| 1212 | iot = sc->sc_iot; |
| 1213 | ioh = sc->sc_ioh; |
| 1214 | bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE, |
| 1215 | region & ESA_MEMTYPE_MASK); |
| 1216 | bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index); |
| 1217 | data = bus_space_read_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA); |
| 1218 | |
| 1219 | return data; |
| 1220 | } |
| 1221 | |
| 1222 | static void |
| 1223 | esa_write_assp(struct esa_softc *sc, uint16_t region, uint16_t index, |
| 1224 | uint16_t data) |
| 1225 | { |
| 1226 | bus_space_tag_t iot; |
| 1227 | bus_space_handle_t ioh; |
| 1228 | |
| 1229 | iot = sc->sc_iot; |
| 1230 | ioh = sc->sc_ioh; |
| 1231 | bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE, |
| 1232 | region & ESA_MEMTYPE_MASK); |
| 1233 | bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index); |
| 1234 | bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA, data); |
| 1235 | |
| 1236 | return; |
| 1237 | } |
| 1238 | |
| 1239 | static int |
| 1240 | esa_init_codec(struct esa_softc *sc) |
| 1241 | { |
| 1242 | bus_space_tag_t iot; |
| 1243 | bus_space_handle_t ioh; |
| 1244 | uint32_t data; |
| 1245 | |
| 1246 | iot = sc->sc_iot; |
| 1247 | ioh = sc->sc_ioh; |
| 1248 | data = bus_space_read_1(iot, ioh, ESA_CODEC_COMMAND); |
| 1249 | |
| 1250 | return (data & 0x1) ? 0 : 1; |
| 1251 | } |
| 1252 | |
| 1253 | static int |
| 1254 | esa_attach_codec(void *aux, struct ac97_codec_if *codec_if) |
| 1255 | { |
| 1256 | struct esa_softc *sc; |
| 1257 | |
| 1258 | sc = aux; |
| 1259 | sc->codec_if = codec_if; |
| 1260 | |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | static int |
| 1265 | esa_read_codec(void *aux, uint8_t reg, uint16_t *result) |
| 1266 | { |
| 1267 | struct esa_softc *sc; |
| 1268 | bus_space_tag_t iot; |
| 1269 | bus_space_handle_t ioh; |
| 1270 | |
| 1271 | sc = aux; |
| 1272 | iot = sc->sc_iot; |
| 1273 | ioh = sc->sc_ioh; |
| 1274 | if (esa_wait(sc)) |
| 1275 | aprint_error_dev(sc->sc_dev, "esa_read_codec: timed out\n" ); |
| 1276 | bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, (reg & 0x7f) | 0x80); |
| 1277 | delay(50); |
| 1278 | if (esa_wait(sc)) |
| 1279 | aprint_error_dev(sc->sc_dev, "esa_read_codec: timed out\n" ); |
| 1280 | *result = bus_space_read_2(iot, ioh, ESA_CODEC_DATA); |
| 1281 | |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | static int |
| 1286 | esa_write_codec(void *aux, uint8_t reg, uint16_t data) |
| 1287 | { |
| 1288 | struct esa_softc *sc; |
| 1289 | bus_space_tag_t iot; |
| 1290 | bus_space_handle_t ioh; |
| 1291 | |
| 1292 | sc = aux; |
| 1293 | iot = sc->sc_iot; |
| 1294 | ioh = sc->sc_ioh; |
| 1295 | if (esa_wait(sc)) { |
| 1296 | aprint_error_dev(sc->sc_dev, "esa_write_codec: timed out\n" ); |
| 1297 | return -1; |
| 1298 | } |
| 1299 | bus_space_write_2(iot, ioh, ESA_CODEC_DATA, data); |
| 1300 | bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, reg & 0x7f); |
| 1301 | delay(50); |
| 1302 | |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | static int |
| 1307 | esa_reset_codec(void *aux) |
| 1308 | { |
| 1309 | |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | static enum ac97_host_flags |
| 1314 | esa_flags_codec(void *aux) |
| 1315 | { |
| 1316 | struct esa_softc *sc; |
| 1317 | |
| 1318 | sc = aux; |
| 1319 | return sc->codec_flags; |
| 1320 | } |
| 1321 | |
| 1322 | static int |
| 1323 | esa_wait(struct esa_softc *sc) |
| 1324 | { |
| 1325 | int i, val; |
| 1326 | bus_space_tag_t iot; |
| 1327 | bus_space_handle_t ioh; |
| 1328 | |
| 1329 | iot = sc->sc_iot; |
| 1330 | ioh = sc->sc_ioh; |
| 1331 | for (i = 0; i < 20; i++) { |
| 1332 | val = bus_space_read_1(iot, ioh, ESA_CODEC_STATUS); |
| 1333 | if ((val & 1) == 0) |
| 1334 | return 0; |
| 1335 | delay(2); |
| 1336 | } |
| 1337 | |
| 1338 | return -1; |
| 1339 | } |
| 1340 | |
| 1341 | static int |
| 1342 | esa_init(struct esa_softc *sc) |
| 1343 | { |
| 1344 | struct esa_voice *vc; |
| 1345 | bus_space_tag_t iot; |
| 1346 | bus_space_handle_t ioh; |
| 1347 | pcitag_t tag; |
| 1348 | pci_chipset_tag_t pc; |
| 1349 | uint32_t data, i, size; |
| 1350 | uint8_t reset_state; |
| 1351 | int data_bytes; |
| 1352 | |
| 1353 | iot = sc->sc_iot; |
| 1354 | ioh = sc->sc_ioh; |
| 1355 | tag = sc->sc_tag; |
| 1356 | pc = sc->sc_pct; |
| 1357 | data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) + |
| 1358 | (ESA_MINISRC_IN_BUFFER_SIZE & ~1) + |
| 1359 | (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) & ~255; |
| 1360 | |
| 1361 | mutex_spin_enter(&sc->sc_intr_lock); |
| 1362 | |
| 1363 | /* Disable legacy emulation */ |
| 1364 | data = pci_conf_read(pc, tag, PCI_LEGACY_AUDIO_CTRL); |
| 1365 | data |= DISABLE_LEGACY; |
| 1366 | pci_conf_write(pc, tag, PCI_LEGACY_AUDIO_CTRL, data); |
| 1367 | |
| 1368 | esa_config(sc); |
| 1369 | |
| 1370 | reset_state = esa_assp_halt(sc); |
| 1371 | |
| 1372 | esa_init_codec(sc); |
| 1373 | esa_codec_reset(sc); |
| 1374 | |
| 1375 | /* Zero kernel and mixer data */ |
| 1376 | size = ESA_REV_B_DATA_MEMORY_UNIT_LENGTH * ESA_NUM_UNITS_KERNEL_DATA; |
| 1377 | for (i = 0; i < size / 2; i++) { |
| 1378 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 1379 | ESA_KDATA_BASE_ADDR + i, 0); |
| 1380 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 1381 | ESA_KDATA_BASE_ADDR2 + i, 0); |
| 1382 | } |
| 1383 | |
| 1384 | /* Init DMA pointer */ |
| 1385 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_CURRENT_DMA, |
| 1386 | ESA_KDATA_DMA_XFER0); |
| 1387 | |
| 1388 | /* Write kernel code into memory */ |
| 1389 | for (i = 0; i < __arraycount(esa_assp_kernel_image); i++) |
| 1390 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, |
| 1391 | ESA_REV_B_CODE_MEMORY_BEGIN + i, esa_assp_kernel_image[i]); |
| 1392 | |
| 1393 | for (i = 0; i < __arraycount(esa_assp_minisrc_image); i++) |
| 1394 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 0x400 + i, |
| 1395 | esa_assp_minisrc_image[i]); |
| 1396 | |
| 1397 | /* Write the coefficients for the low pass filter */ |
| 1398 | for (i = 0; i < __arraycount(esa_minisrc_lpf_image); i++) |
| 1399 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, |
| 1400 | 0x400 + ESA_MINISRC_COEF_LOC + i, |
| 1401 | esa_minisrc_lpf_image[i]); |
| 1402 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, |
| 1403 | 0x400 + ESA_MINISRC_COEF_LOC + size, 0x8000); |
| 1404 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_TASK0, 0x400); |
| 1405 | /* Init the mixer number */ |
| 1406 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 1407 | ESA_KDATA_MIXER_TASK_NUMBER, 0); |
| 1408 | /* Extreme kernel master volume */ |
| 1409 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 1410 | ESA_KDATA_DAC_LEFT_VOLUME, ESA_ARB_VOLUME); |
| 1411 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 1412 | ESA_KDATA_DAC_RIGHT_VOLUME, ESA_ARB_VOLUME); |
| 1413 | |
| 1414 | if (esa_amp_enable(sc)) |
| 1415 | return -1; |
| 1416 | |
| 1417 | /* Zero entire DAC/ADC area */ |
| 1418 | for (i = 0x1100; i < 0x1c00; i++) |
| 1419 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, 0); |
| 1420 | |
| 1421 | /* set some sane defaults */ |
| 1422 | for (i = 0; i < ESA_NUM_VOICES; i++) { |
| 1423 | vc = &sc->voice[i]; |
| 1424 | vc->play.data_offset = ESA_DAC_DATA + (data_bytes * i); |
| 1425 | vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * i * 2); |
| 1426 | } |
| 1427 | |
| 1428 | esa_enable_interrupts(sc); |
| 1429 | |
| 1430 | bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B, |
| 1431 | reset_state | ESA_REGB_ENABLE_RESET); |
| 1432 | |
| 1433 | mutex_spin_exit(&sc->sc_intr_lock); |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | static void |
| 1439 | esa_config(struct esa_softc *sc) |
| 1440 | { |
| 1441 | bus_space_tag_t iot; |
| 1442 | bus_space_handle_t ioh; |
| 1443 | pcitag_t tag; |
| 1444 | pci_chipset_tag_t pc; |
| 1445 | uint32_t data; |
| 1446 | |
| 1447 | iot = sc->sc_iot; |
| 1448 | ioh = sc->sc_ioh; |
| 1449 | tag = sc->sc_tag; |
| 1450 | pc = sc->sc_pct; |
| 1451 | |
| 1452 | data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG); |
| 1453 | data &= ESA_REDUCED_DEBOUNCE; |
| 1454 | data |= ESA_PM_CTRL_ENABLE | ESA_CLK_DIV_BY_49 | ESA_USE_PCI_TIMING; |
| 1455 | pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data); |
| 1456 | |
| 1457 | bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RESET_ASSP); |
| 1458 | data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG); |
| 1459 | data &= ~ESA_INT_CLK_SELECT; |
| 1460 | if (sc->type == ESS_MAESTRO3) { |
| 1461 | data &= ~ESA_INT_CLK_MULT_ENABLE; |
| 1462 | data |= ESA_INT_CLK_SRC_NOT_PCI; |
| 1463 | } |
| 1464 | data &= ~(ESA_CLK_MULT_MODE_SELECT | ESA_CLK_MULT_MODE_SELECT_2); |
| 1465 | pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data); |
| 1466 | |
| 1467 | if (sc->type == ESS_ALLEGRO1) { |
| 1468 | data = pci_conf_read(pc, tag, ESA_PCI_USER_CONFIG); |
| 1469 | data |= ESA_IN_CLK_12MHZ_SELECT; |
| 1470 | pci_conf_write(pc, tag, ESA_PCI_USER_CONFIG, data); |
| 1471 | } |
| 1472 | |
| 1473 | data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_A); |
| 1474 | data &= ~(ESA_DSP_CLK_36MHZ_SELECT | ESA_ASSP_CLK_49MHZ_SELECT); |
| 1475 | data |= ESA_ASSP_CLK_49MHZ_SELECT; /* XXX: Assumes 49MHz DSP */ |
| 1476 | data |= ESA_ASSP_0_WS_ENABLE; |
| 1477 | bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_A, data); |
| 1478 | |
| 1479 | bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RUN_ASSP); |
| 1480 | |
| 1481 | return; |
| 1482 | } |
| 1483 | |
| 1484 | static uint8_t |
| 1485 | esa_assp_halt(struct esa_softc *sc) |
| 1486 | { |
| 1487 | bus_space_tag_t iot; |
| 1488 | bus_space_handle_t ioh; |
| 1489 | uint8_t data, reset_state; |
| 1490 | |
| 1491 | iot = sc->sc_iot; |
| 1492 | ioh = sc->sc_ioh; |
| 1493 | data = bus_space_read_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B); |
| 1494 | reset_state = data & ~ESA_REGB_STOP_CLOCK; |
| 1495 | delay(10000); |
| 1496 | bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B, |
| 1497 | reset_state & ~ESA_REGB_ENABLE_RESET); |
| 1498 | delay(10000); |
| 1499 | |
| 1500 | return reset_state; |
| 1501 | } |
| 1502 | |
| 1503 | static void |
| 1504 | esa_codec_reset(struct esa_softc *sc) |
| 1505 | { |
| 1506 | bus_space_tag_t iot; |
| 1507 | bus_space_handle_t ioh; |
| 1508 | uint16_t data, dir; |
| 1509 | int retry; |
| 1510 | |
| 1511 | iot = sc->sc_iot; |
| 1512 | ioh = sc->sc_ioh; |
| 1513 | retry = 0; |
| 1514 | do { |
| 1515 | data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION); |
| 1516 | dir = data | 0x10; /* assuming pci bus master? */ |
| 1517 | |
| 1518 | /* remote codec config */ |
| 1519 | data = bus_space_read_2(iot, ioh, ESA_RING_BUS_CTRL_B); |
| 1520 | bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_B, |
| 1521 | data & ~ESA_SECOND_CODEC_ID_MASK); |
| 1522 | data = bus_space_read_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL); |
| 1523 | bus_space_write_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL, |
| 1524 | data & ~ESA_COMMAND_ADDR_OUT); |
| 1525 | data = bus_space_read_2(iot, ioh, ESA_SDO_IN_DEST_CTRL); |
| 1526 | bus_space_write_2(iot, ioh, ESA_SDO_IN_DEST_CTRL, |
| 1527 | data & ~ESA_STATUS_ADDR_IN); |
| 1528 | |
| 1529 | bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A, |
| 1530 | ESA_IO_SRAM_ENABLE); |
| 1531 | delay(20); |
| 1532 | |
| 1533 | bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, |
| 1534 | dir & ~ESA_GPO_PRIMARY_AC97); |
| 1535 | bus_space_write_2(iot, ioh, ESA_GPIO_MASK, |
| 1536 | ~ESA_GPO_PRIMARY_AC97); |
| 1537 | bus_space_write_2(iot, ioh, ESA_GPIO_DATA, 0); |
| 1538 | bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, |
| 1539 | dir | ESA_GPO_PRIMARY_AC97); |
| 1540 | delay(sc->delay1 * 1000); |
| 1541 | bus_space_write_2(iot, ioh, ESA_GPIO_DATA, |
| 1542 | ESA_GPO_PRIMARY_AC97); |
| 1543 | delay(5); |
| 1544 | bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A, |
| 1545 | ESA_IO_SRAM_ENABLE | ESA_SERIAL_AC_LINK_ENABLE); |
| 1546 | bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0); |
| 1547 | delay(sc->delay2 * 1000); |
| 1548 | |
| 1549 | esa_read_codec(sc, 0x7c, &data); |
| 1550 | if ((data == 0) || (data == 0xffff)) { |
| 1551 | retry++; |
| 1552 | if (retry > 3) { |
| 1553 | aprint_error_dev(sc->sc_dev, |
| 1554 | "esa_codec_reset: failed\n" ); |
| 1555 | break; |
| 1556 | } |
| 1557 | aprint_normal_dev(sc->sc_dev, |
| 1558 | "esa_codec_reset: retrying\n" ); |
| 1559 | } else |
| 1560 | retry = 0; |
| 1561 | } while (retry); |
| 1562 | |
| 1563 | return; |
| 1564 | } |
| 1565 | |
| 1566 | static int |
| 1567 | esa_amp_enable(struct esa_softc *sc) |
| 1568 | { |
| 1569 | bus_space_tag_t iot; |
| 1570 | bus_space_handle_t ioh; |
| 1571 | uint32_t gpo, polarity_port, polarity; |
| 1572 | uint16_t data; |
| 1573 | |
| 1574 | iot = sc->sc_iot; |
| 1575 | ioh = sc->sc_ioh; |
| 1576 | switch (sc->type) { |
| 1577 | case ESS_ALLEGRO1: |
| 1578 | polarity_port = 0x1800; |
| 1579 | break; |
| 1580 | case ESS_MAESTRO3: |
| 1581 | polarity_port = 0x1100; |
| 1582 | break; |
| 1583 | default: |
| 1584 | aprint_error_dev(sc->sc_dev, |
| 1585 | "esa_amp_enable: Unknown chip type!!!\n" ); |
| 1586 | return 1; |
| 1587 | } |
| 1588 | |
| 1589 | gpo = (polarity_port >> 8) & 0x0f; |
| 1590 | polarity = polarity_port >> 12; |
| 1591 | polarity = !polarity; /* Enable */ |
| 1592 | polarity = polarity << gpo; |
| 1593 | gpo = 1 << gpo; |
| 1594 | bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~gpo); |
| 1595 | data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION); |
| 1596 | bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, data | gpo); |
| 1597 | data = ESA_GPO_SECONDARY_AC97 | ESA_GPO_PRIMARY_AC97 | polarity; |
| 1598 | bus_space_write_2(iot, ioh, ESA_GPIO_DATA, data); |
| 1599 | bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0); |
| 1600 | |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | static void |
| 1605 | esa_enable_interrupts(struct esa_softc *sc) |
| 1606 | { |
| 1607 | bus_space_tag_t iot; |
| 1608 | bus_space_handle_t ioh; |
| 1609 | uint8_t data; |
| 1610 | |
| 1611 | iot = sc->sc_iot; |
| 1612 | ioh = sc->sc_ioh; |
| 1613 | bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, |
| 1614 | ESA_ASSP_INT_ENABLE | ESA_HV_INT_ENABLE); |
| 1615 | data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_C); |
| 1616 | bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C, |
| 1617 | data | ESA_ASSP_HOST_INT_ENABLE); |
| 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | * List management |
| 1622 | */ |
| 1623 | static int |
| 1624 | esa_add_list(struct esa_voice *vc, struct esa_list *el, |
| 1625 | uint16_t val, int index) |
| 1626 | { |
| 1627 | struct esa_softc *sc; |
| 1628 | |
| 1629 | sc = device_private(vc->parent); |
| 1630 | el->indexmap[index] = el->currlen; |
| 1631 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 1632 | el->mem_addr + el->currlen, |
| 1633 | val); |
| 1634 | |
| 1635 | return el->currlen++; |
| 1636 | } |
| 1637 | |
| 1638 | static void |
| 1639 | esa_remove_list(struct esa_voice *vc, struct esa_list *el, int index) |
| 1640 | { |
| 1641 | struct esa_softc *sc; |
| 1642 | uint16_t val; |
| 1643 | int lastindex; |
| 1644 | int vindex; |
| 1645 | int i; |
| 1646 | |
| 1647 | sc = device_private(vc->parent); |
| 1648 | lastindex = el->currlen - 1; |
| 1649 | vindex = el->indexmap[index]; |
| 1650 | |
| 1651 | /* reset our virtual index */ |
| 1652 | el->indexmap[index] = -1; |
| 1653 | |
| 1654 | if (vindex != lastindex) { |
| 1655 | val = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 1656 | el->mem_addr + lastindex); |
| 1657 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 1658 | el->mem_addr + vindex, |
| 1659 | val); |
| 1660 | for (i = 0; i < ESA_NUM_VOICES * 2; i++) |
| 1661 | if (el->indexmap[i] == lastindex) |
| 1662 | break; |
| 1663 | if (i >= ESA_NUM_VOICES * 2) |
| 1664 | aprint_error_dev(sc->sc_dev, |
| 1665 | "esa_remove_list: invalid task index\n" ); |
| 1666 | else |
| 1667 | el->indexmap[i] = vindex; |
| 1668 | } |
| 1669 | |
| 1670 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, |
| 1671 | el->mem_addr + lastindex, 0); |
| 1672 | el->currlen--; |
| 1673 | |
| 1674 | return; |
| 1675 | } |
| 1676 | |
| 1677 | static bool |
| 1678 | esa_suspend(device_t dv, const pmf_qual_t *qual) |
| 1679 | { |
| 1680 | struct esa_softc *sc = device_private(dv); |
| 1681 | bus_space_tag_t iot = sc->sc_iot; |
| 1682 | bus_space_handle_t ioh = sc->sc_ioh; |
| 1683 | int i, index; |
| 1684 | |
| 1685 | index = 0; |
| 1686 | |
| 1687 | mutex_enter(&sc->sc_lock); |
| 1688 | mutex_spin_enter(&sc->sc_intr_lock); |
| 1689 | |
| 1690 | bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 0); |
| 1691 | bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C, 0); |
| 1692 | |
| 1693 | esa_assp_halt(sc); |
| 1694 | |
| 1695 | /* Save ASSP state */ |
| 1696 | for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END; |
| 1697 | i++) |
| 1698 | sc->savemem[index++] = esa_read_assp(sc, |
| 1699 | ESA_MEMTYPE_INTERNAL_CODE, i); |
| 1700 | for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END; |
| 1701 | i++) |
| 1702 | sc->savemem[index++] = esa_read_assp(sc, |
| 1703 | ESA_MEMTYPE_INTERNAL_DATA, i); |
| 1704 | |
| 1705 | mutex_spin_exit(&sc->sc_intr_lock); |
| 1706 | mutex_exit(&sc->sc_lock); |
| 1707 | |
| 1708 | return true; |
| 1709 | } |
| 1710 | |
| 1711 | static bool |
| 1712 | esa_resume(device_t dv, const pmf_qual_t *qual) |
| 1713 | { |
| 1714 | struct esa_softc *sc = device_private(dv); |
| 1715 | bus_space_tag_t iot = sc->sc_iot; |
| 1716 | bus_space_handle_t ioh = sc->sc_ioh; |
| 1717 | int i, index; |
| 1718 | uint8_t reset_state; |
| 1719 | pcireg_t data; |
| 1720 | |
| 1721 | index = 0; |
| 1722 | |
| 1723 | delay(10000); |
| 1724 | |
| 1725 | mutex_enter(&sc->sc_lock); |
| 1726 | mutex_spin_enter(&sc->sc_intr_lock); |
| 1727 | |
| 1728 | data = pci_conf_read(sc->sc_pct, sc->sc_tag, PCI_LEGACY_AUDIO_CTRL); |
| 1729 | pci_conf_write(sc->sc_pct, sc->sc_tag, PCI_LEGACY_AUDIO_CTRL, |
| 1730 | data | DISABLE_LEGACY); |
| 1731 | |
| 1732 | bus_space_write_4(iot, ioh, ESA_PCI_ACPI_CONTROL, ESA_PCI_ACPI_D0); |
| 1733 | |
| 1734 | esa_config(sc); |
| 1735 | |
| 1736 | reset_state = esa_assp_halt(sc); |
| 1737 | |
| 1738 | esa_init_codec(sc); |
| 1739 | esa_codec_reset(sc); |
| 1740 | |
| 1741 | /* restore ASSP */ |
| 1742 | for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END; |
| 1743 | i++) |
| 1744 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, i, |
| 1745 | sc->savemem[index++]); |
| 1746 | for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END; |
| 1747 | i++) |
| 1748 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, |
| 1749 | sc->savemem[index++]); |
| 1750 | |
| 1751 | esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_ACTIVE, 0); |
| 1752 | bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B, |
| 1753 | reset_state | ESA_REGB_ENABLE_RESET); |
| 1754 | |
| 1755 | esa_enable_interrupts(sc); |
| 1756 | esa_amp_enable(sc); |
| 1757 | |
| 1758 | mutex_spin_exit(&sc->sc_intr_lock); |
| 1759 | |
| 1760 | /* Finally, power up AC97 codec */ |
| 1761 | delay(1000); |
| 1762 | |
| 1763 | sc->codec_if->vtbl->restore_ports(sc->codec_if); |
| 1764 | |
| 1765 | mutex_exit(&sc->sc_lock); |
| 1766 | |
| 1767 | return true; |
| 1768 | } |
| 1769 | |
| 1770 | static uint32_t |
| 1771 | esa_get_pointer(struct esa_softc *sc, struct esa_channel *ch) |
| 1772 | { |
| 1773 | uint16_t hi, lo; |
| 1774 | uint32_t addr; |
| 1775 | int data_offset; |
| 1776 | |
| 1777 | data_offset = ch->data_offset; |
| 1778 | hi = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset + |
| 1779 | ESA_CDATA_HOST_SRC_CURRENTH); |
| 1780 | lo = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset + |
| 1781 | ESA_CDATA_HOST_SRC_CURRENTL); |
| 1782 | |
| 1783 | addr = lo | ((uint32_t)hi << 16); |
| 1784 | return (addr - ch->start); |
| 1785 | } |
| 1786 | |
| 1787 | static paddr_t |
| 1788 | esa_mappage(void *addr, void *mem, off_t off, int prot) |
| 1789 | { |
| 1790 | struct esa_voice *vc; |
| 1791 | struct esa_softc *sc; |
| 1792 | struct esa_dma *p; |
| 1793 | |
| 1794 | vc = addr; |
| 1795 | sc = device_private(vc->parent); |
| 1796 | if (off < 0) |
| 1797 | return -1; |
| 1798 | for (p = vc->dma; p && KERNADDR(p) != mem; p = p->next) |
| 1799 | continue; |
| 1800 | if (p == NULL) |
| 1801 | return -1; |
| 1802 | return bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nsegs, |
| 1803 | off, prot, BUS_DMA_WAITOK); |
| 1804 | } |
| 1805 | |
| 1806 | static void |
| 1807 | esa_get_locks(void *addr, kmutex_t **intr, kmutex_t **proc) |
| 1808 | { |
| 1809 | struct esa_voice *vc; |
| 1810 | struct esa_softc *sc; |
| 1811 | |
| 1812 | vc = addr; |
| 1813 | sc = device_private(vc->parent); |
| 1814 | |
| 1815 | *intr = &sc->sc_intr_lock; |
| 1816 | *proc = &sc->sc_lock; |
| 1817 | } |
| 1818 | |