1098ca2bdSWarner Losh /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 43f225978SCameron Grant * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 5fd1aaeccSCameron Grant * 6fd1aaeccSCameron Grant * Redistribution and use in source and binary forms, with or without 7fd1aaeccSCameron Grant * modification, are permitted provided that the following conditions 8fd1aaeccSCameron Grant * are met: 9fd1aaeccSCameron Grant * 1. Redistributions of source code must retain the above copyright 10fd1aaeccSCameron Grant * notice, this list of conditions and the following disclaimer. 11fd1aaeccSCameron Grant * 2. Redistributions in binary form must reproduce the above copyright 12fd1aaeccSCameron Grant * notice, this list of conditions and the following disclaimer in the 13fd1aaeccSCameron Grant * documentation and/or other materials provided with the distribution. 14fd1aaeccSCameron Grant * 15fd1aaeccSCameron Grant * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16fd1aaeccSCameron Grant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17fd1aaeccSCameron Grant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18fd1aaeccSCameron Grant * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19fd1aaeccSCameron Grant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20fd1aaeccSCameron Grant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21fd1aaeccSCameron Grant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22fd1aaeccSCameron Grant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23fd1aaeccSCameron Grant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24fd1aaeccSCameron Grant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25fd1aaeccSCameron Grant * SUCH DAMAGE. 26fd1aaeccSCameron Grant */ 27fd1aaeccSCameron Grant 2890da2b28SAriff Abdullah #ifdef HAVE_KERNEL_OPTION_HEADERS 2990da2b28SAriff Abdullah #include "opt_snd.h" 3090da2b28SAriff Abdullah #endif 3190da2b28SAriff Abdullah 32fd1aaeccSCameron Grant #include <dev/sound/pcm/sound.h> 33fd1aaeccSCameron Grant 3490cf0136SWarner Losh #include <dev/pci/pcireg.h> 3590cf0136SWarner Losh #include <dev/pci/pcivar.h> 36fd1aaeccSCameron Grant 37fd1aaeccSCameron Grant #include <dev/sound/isa/sb.h> 38fd1aaeccSCameron Grant #include <dev/sound/chip.h> 39fd1aaeccSCameron Grant 400f55ac6cSCameron Grant #include "mixer_if.h" 410f55ac6cSCameron Grant 4239dbd126SCameron Grant #define SOLO_DEFAULT_BUFSZ 16384 43fd1aaeccSCameron Grant #define ABS(x) (((x) < 0)? -(x) : (x)) 44fd1aaeccSCameron Grant 453ac1ca33SNick Sayer /* if defined, playback always uses the 2nd channel and full duplex works */ 4686b391b2SAriff Abdullah #define ESS18XX_DUPLEX 1 47fd1aaeccSCameron Grant 48fd1aaeccSCameron Grant /* more accurate clocks and split audio1/audio2 rates */ 49fd1aaeccSCameron Grant #define ESS18XX_NEWSPEED 50fd1aaeccSCameron Grant 51513693beSCameron Grant static u_int32_t ess_playfmt[] = { 5290da2b28SAriff Abdullah SND_FORMAT(AFMT_U8, 1, 0), 5390da2b28SAriff Abdullah SND_FORMAT(AFMT_U8, 2, 0), 5490da2b28SAriff Abdullah SND_FORMAT(AFMT_S8, 1, 0), 5590da2b28SAriff Abdullah SND_FORMAT(AFMT_S8, 2, 0), 5690da2b28SAriff Abdullah SND_FORMAT(AFMT_S16_LE, 1, 0), 5790da2b28SAriff Abdullah SND_FORMAT(AFMT_S16_LE, 2, 0), 5890da2b28SAriff Abdullah SND_FORMAT(AFMT_U16_LE, 1, 0), 5990da2b28SAriff Abdullah SND_FORMAT(AFMT_U16_LE, 2, 0), 60513693beSCameron Grant 0 61fd1aaeccSCameron Grant }; 621333b4a4SAriff Abdullah static struct pcmchan_caps ess_playcaps = {6000, 48000, ess_playfmt, 0}; 63fd1aaeccSCameron Grant 643ac1ca33SNick Sayer /* 653ac1ca33SNick Sayer * Recording output is byte-swapped 663ac1ca33SNick Sayer */ 67513693beSCameron Grant static u_int32_t ess_recfmt[] = { 6890da2b28SAriff Abdullah SND_FORMAT(AFMT_U8, 1, 0), 6990da2b28SAriff Abdullah SND_FORMAT(AFMT_U8, 2, 0), 7090da2b28SAriff Abdullah SND_FORMAT(AFMT_S8, 1, 0), 7190da2b28SAriff Abdullah SND_FORMAT(AFMT_S8, 2, 0), 7290da2b28SAriff Abdullah SND_FORMAT(AFMT_S16_BE, 1, 0), 7390da2b28SAriff Abdullah SND_FORMAT(AFMT_S16_BE, 2, 0), 7490da2b28SAriff Abdullah SND_FORMAT(AFMT_U16_BE, 1, 0), 7590da2b28SAriff Abdullah SND_FORMAT(AFMT_U16_BE, 2, 0), 76513693beSCameron Grant 0 77fd1aaeccSCameron Grant }; 781333b4a4SAriff Abdullah static struct pcmchan_caps ess_reccaps = {6000, 48000, ess_recfmt, 0}; 79fd1aaeccSCameron Grant 80fd1aaeccSCameron Grant struct ess_info; 81fd1aaeccSCameron Grant 82fd1aaeccSCameron Grant struct ess_chinfo { 83fd1aaeccSCameron Grant struct ess_info *parent; 8466ef8af5SCameron Grant struct pcm_channel *channel; 8566ef8af5SCameron Grant struct snd_dbuf *buffer; 86fd1aaeccSCameron Grant int dir, hwch, stopping; 87350a5fafSCameron Grant u_int32_t fmt, spd, blksz; 88fd1aaeccSCameron Grant }; 89fd1aaeccSCameron Grant 90fd1aaeccSCameron Grant struct ess_info { 91fd1aaeccSCameron Grant struct resource *io, *sb, *vc, *mpu, *gp; /* I/O address for the board */ 92fd1aaeccSCameron Grant struct resource *irq; 93306f91b6SCameron Grant void *ih; 94fd1aaeccSCameron Grant bus_dma_tag_t parent_dmat; 95fd1aaeccSCameron Grant 9679462204SAriff Abdullah int simplex_dir, type, dmasz[2]; 9779462204SAriff Abdullah unsigned int duplex:1, newspeed:1; 9839dbd126SCameron Grant unsigned int bufsz; 9939dbd126SCameron Grant 100fd1aaeccSCameron Grant struct ess_chinfo pch, rch; 1018b6d3fe1SAriff Abdullah struct mtx *lock; 102fd1aaeccSCameron Grant }; 103fd1aaeccSCameron Grant 1048b6d3fe1SAriff Abdullah #define ess_lock(_ess) snd_mtxlock((_ess)->lock) 1058b6d3fe1SAriff Abdullah #define ess_unlock(_ess) snd_mtxunlock((_ess)->lock) 1068b6d3fe1SAriff Abdullah #define ess_lock_assert(_ess) snd_mtxassert((_ess)->lock) 1078b6d3fe1SAriff Abdullah 108fd1aaeccSCameron Grant static int ess_rd(struct ess_info *sc, int reg); 109fd1aaeccSCameron Grant static void ess_wr(struct ess_info *sc, int reg, u_int8_t val); 110fd1aaeccSCameron Grant static int ess_dspready(struct ess_info *sc); 111fd1aaeccSCameron Grant static int ess_cmd(struct ess_info *sc, u_char val); 112fd1aaeccSCameron Grant static int ess_cmd1(struct ess_info *sc, u_char cmd, int val); 113fd1aaeccSCameron Grant static int ess_get_byte(struct ess_info *sc); 114fd1aaeccSCameron Grant static void ess_setmixer(struct ess_info *sc, u_int port, u_int value); 115fd1aaeccSCameron Grant static int ess_getmixer(struct ess_info *sc, u_int port); 116fd1aaeccSCameron Grant static int ess_reset_dsp(struct ess_info *sc); 117fd1aaeccSCameron Grant 118fd1aaeccSCameron Grant static int ess_write(struct ess_info *sc, u_char reg, int val); 119fd1aaeccSCameron Grant static int ess_read(struct ess_info *sc, u_char reg); 120fd1aaeccSCameron Grant 121fd1aaeccSCameron Grant static void ess_intr(void *arg); 122fd1aaeccSCameron Grant static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len); 123fd1aaeccSCameron Grant static int ess_start(struct ess_chinfo *ch); 124fd1aaeccSCameron Grant static int ess_stop(struct ess_chinfo *ch); 125fd1aaeccSCameron Grant 126fd1aaeccSCameron Grant static int ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir); 127fd1aaeccSCameron Grant static int ess_dmapos(struct ess_info *sc, int ch); 128fd1aaeccSCameron Grant static int ess_dmatrigger(struct ess_info *sc, int ch, int go); 129fd1aaeccSCameron Grant 130fd1aaeccSCameron Grant /* 131fd1aaeccSCameron Grant * Common code for the midi and pcm functions 132fd1aaeccSCameron Grant * 133fd1aaeccSCameron Grant * ess_cmd write a single byte to the CMD port. 134fd1aaeccSCameron Grant * ess_cmd1 write a CMD + 1 byte arg 135fd1aaeccSCameron Grant * ess_cmd2 write a CMD + 2 byte arg 136fd1aaeccSCameron Grant * ess_get_byte returns a single byte from the DSP data port 137fd1aaeccSCameron Grant * 138fd1aaeccSCameron Grant * ess_write is actually ess_cmd1 139fd1aaeccSCameron Grant * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte 140fd1aaeccSCameron Grant */ 141fd1aaeccSCameron Grant 142fd1aaeccSCameron Grant static int 143fd1aaeccSCameron Grant port_rd(struct resource *port, int regno, int size) 144fd1aaeccSCameron Grant { 145fd1aaeccSCameron Grant bus_space_tag_t st = rman_get_bustag(port); 146fd1aaeccSCameron Grant bus_space_handle_t sh = rman_get_bushandle(port); 147fd1aaeccSCameron Grant 148fd1aaeccSCameron Grant switch (size) { 149fd1aaeccSCameron Grant case 1: 150fd1aaeccSCameron Grant return bus_space_read_1(st, sh, regno); 151fd1aaeccSCameron Grant case 2: 152fd1aaeccSCameron Grant return bus_space_read_2(st, sh, regno); 153fd1aaeccSCameron Grant case 4: 154fd1aaeccSCameron Grant return bus_space_read_4(st, sh, regno); 155fd1aaeccSCameron Grant default: 156fd1aaeccSCameron Grant return 0xffffffff; 157fd1aaeccSCameron Grant } 158fd1aaeccSCameron Grant } 159fd1aaeccSCameron Grant 160fd1aaeccSCameron Grant static void 161fd1aaeccSCameron Grant port_wr(struct resource *port, int regno, u_int32_t data, int size) 162fd1aaeccSCameron Grant { 163fd1aaeccSCameron Grant bus_space_tag_t st = rman_get_bustag(port); 164fd1aaeccSCameron Grant bus_space_handle_t sh = rman_get_bushandle(port); 165fd1aaeccSCameron Grant 166fd1aaeccSCameron Grant switch (size) { 167fd1aaeccSCameron Grant case 1: 168fd1aaeccSCameron Grant bus_space_write_1(st, sh, regno, data); 169fd1aaeccSCameron Grant break; 170fd1aaeccSCameron Grant case 2: 171fd1aaeccSCameron Grant bus_space_write_2(st, sh, regno, data); 172fd1aaeccSCameron Grant break; 173fd1aaeccSCameron Grant case 4: 174fd1aaeccSCameron Grant bus_space_write_4(st, sh, regno, data); 175fd1aaeccSCameron Grant break; 176fd1aaeccSCameron Grant } 177fd1aaeccSCameron Grant } 178fd1aaeccSCameron Grant 179fd1aaeccSCameron Grant static int 180fd1aaeccSCameron Grant ess_rd(struct ess_info *sc, int reg) 181fd1aaeccSCameron Grant { 182fd1aaeccSCameron Grant return port_rd(sc->sb, reg, 1); 183fd1aaeccSCameron Grant } 184fd1aaeccSCameron Grant 185fd1aaeccSCameron Grant static void 186fd1aaeccSCameron Grant ess_wr(struct ess_info *sc, int reg, u_int8_t val) 187fd1aaeccSCameron Grant { 188fd1aaeccSCameron Grant port_wr(sc->sb, reg, val, 1); 189fd1aaeccSCameron Grant } 190fd1aaeccSCameron Grant 191fd1aaeccSCameron Grant static int 192fd1aaeccSCameron Grant ess_dspready(struct ess_info *sc) 193fd1aaeccSCameron Grant { 194fd1aaeccSCameron Grant return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0); 195fd1aaeccSCameron Grant } 196fd1aaeccSCameron Grant 197fd1aaeccSCameron Grant static int 198fd1aaeccSCameron Grant ess_dspwr(struct ess_info *sc, u_char val) 199fd1aaeccSCameron Grant { 200fd1aaeccSCameron Grant int i; 201fd1aaeccSCameron Grant 202fd1aaeccSCameron Grant for (i = 0; i < 1000; i++) { 203fd1aaeccSCameron Grant if (ess_dspready(sc)) { 204fd1aaeccSCameron Grant ess_wr(sc, SBDSP_CMD, val); 205fd1aaeccSCameron Grant return 1; 206fd1aaeccSCameron Grant } 207fd1aaeccSCameron Grant if (i > 10) DELAY((i > 100)? 1000 : 10); 208fd1aaeccSCameron Grant } 209fd1aaeccSCameron Grant printf("ess_dspwr(0x%02x) timed out.\n", val); 210fd1aaeccSCameron Grant return 0; 211fd1aaeccSCameron Grant } 212fd1aaeccSCameron Grant 213fd1aaeccSCameron Grant static int 214fd1aaeccSCameron Grant ess_cmd(struct ess_info *sc, u_char val) 215fd1aaeccSCameron Grant { 216a7e11506SNick Sayer DEB(printf("ess_cmd: %x\n", val)); 217fd1aaeccSCameron Grant return ess_dspwr(sc, val); 218fd1aaeccSCameron Grant } 219fd1aaeccSCameron Grant 220fd1aaeccSCameron Grant static int 221fd1aaeccSCameron Grant ess_cmd1(struct ess_info *sc, u_char cmd, int val) 222fd1aaeccSCameron Grant { 223a7e11506SNick Sayer DEB(printf("ess_cmd1: %x, %x\n", cmd, val)); 224fd1aaeccSCameron Grant if (ess_dspwr(sc, cmd)) { 225fd1aaeccSCameron Grant return ess_dspwr(sc, val & 0xff); 226fd1aaeccSCameron Grant } else return 0; 227fd1aaeccSCameron Grant } 228fd1aaeccSCameron Grant 229fd1aaeccSCameron Grant static void 230fd1aaeccSCameron Grant ess_setmixer(struct ess_info *sc, u_int port, u_int value) 231fd1aaeccSCameron Grant { 232fd1aaeccSCameron Grant DEB(printf("ess_setmixer: reg=%x, val=%x\n", port, value);) 233fd1aaeccSCameron Grant ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 234fd1aaeccSCameron Grant DELAY(10); 235fd1aaeccSCameron Grant ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff)); 236fd1aaeccSCameron Grant DELAY(10); 237fd1aaeccSCameron Grant } 238fd1aaeccSCameron Grant 239fd1aaeccSCameron Grant static int 240fd1aaeccSCameron Grant ess_getmixer(struct ess_info *sc, u_int port) 241fd1aaeccSCameron Grant { 242fd1aaeccSCameron Grant int val; 243fd1aaeccSCameron Grant 244fd1aaeccSCameron Grant ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 245fd1aaeccSCameron Grant DELAY(10); 246fd1aaeccSCameron Grant val = ess_rd(sc, SB_MIX_DATA); 247fd1aaeccSCameron Grant DELAY(10); 248fd1aaeccSCameron Grant 249fd1aaeccSCameron Grant return val; 250fd1aaeccSCameron Grant } 251fd1aaeccSCameron Grant 252fd1aaeccSCameron Grant static int 253fd1aaeccSCameron Grant ess_get_byte(struct ess_info *sc) 254fd1aaeccSCameron Grant { 255fd1aaeccSCameron Grant int i; 256fd1aaeccSCameron Grant 257fd1aaeccSCameron Grant for (i = 1000; i > 0; i--) { 2580edeb3dcSNick Sayer if (ess_rd(sc, 0xc) & 0x40) 259fd1aaeccSCameron Grant return ess_rd(sc, DSP_READ); 260fd1aaeccSCameron Grant else 261fd1aaeccSCameron Grant DELAY(20); 262fd1aaeccSCameron Grant } 263fd1aaeccSCameron Grant return -1; 264fd1aaeccSCameron Grant } 265fd1aaeccSCameron Grant 266fd1aaeccSCameron Grant static int 267fd1aaeccSCameron Grant ess_write(struct ess_info *sc, u_char reg, int val) 268fd1aaeccSCameron Grant { 269fd1aaeccSCameron Grant return ess_cmd1(sc, reg, val); 270fd1aaeccSCameron Grant } 271fd1aaeccSCameron Grant 272fd1aaeccSCameron Grant static int 273fd1aaeccSCameron Grant ess_read(struct ess_info *sc, u_char reg) 274fd1aaeccSCameron Grant { 275fd1aaeccSCameron Grant return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1; 276fd1aaeccSCameron Grant } 277fd1aaeccSCameron Grant 278fd1aaeccSCameron Grant static int 279fd1aaeccSCameron Grant ess_reset_dsp(struct ess_info *sc) 280fd1aaeccSCameron Grant { 281a7e11506SNick Sayer DEB(printf("ess_reset_dsp\n")); 282fd1aaeccSCameron Grant ess_wr(sc, SBDSP_RST, 3); 283fd1aaeccSCameron Grant DELAY(100); 284fd1aaeccSCameron Grant ess_wr(sc, SBDSP_RST, 0); 285fd1aaeccSCameron Grant if (ess_get_byte(sc) != 0xAA) { 286a7e11506SNick Sayer DEB(printf("ess_reset_dsp failed\n")); 287a7e11506SNick Sayer /* 288fd1aaeccSCameron Grant rman_get_start(d->io_base))); 289a7e11506SNick Sayer */ 290fd1aaeccSCameron Grant return ENXIO; /* Sorry */ 291fd1aaeccSCameron Grant } 292fd1aaeccSCameron Grant ess_cmd(sc, 0xc6); 293fd1aaeccSCameron Grant return 0; 294fd1aaeccSCameron Grant } 295fd1aaeccSCameron Grant 296fd1aaeccSCameron Grant static void 297fd1aaeccSCameron Grant ess_intr(void *arg) 298fd1aaeccSCameron Grant { 299fd1aaeccSCameron Grant struct ess_info *sc = (struct ess_info *)arg; 3003ac1ca33SNick Sayer int src, pirq = 0, rirq = 0; 301fd1aaeccSCameron Grant 3028b6d3fe1SAriff Abdullah ess_lock(sc); 303fd1aaeccSCameron Grant src = 0; 304fd1aaeccSCameron Grant if (ess_getmixer(sc, 0x7a) & 0x80) 305fd1aaeccSCameron Grant src |= 2; 306fd1aaeccSCameron Grant if (ess_rd(sc, 0x0c) & 0x01) 307fd1aaeccSCameron Grant src |= 1; 308fd1aaeccSCameron Grant 3098d934d50SPyun YongHyeon if (src == 0) { 3108d934d50SPyun YongHyeon ess_unlock(sc); 3114e77c048SCameron Grant return; 3128d934d50SPyun YongHyeon } 3134e77c048SCameron Grant 3143ac1ca33SNick Sayer if (sc->duplex) { 315fd1aaeccSCameron Grant pirq = (src & sc->pch.hwch)? 1 : 0; 316fd1aaeccSCameron Grant rirq = (src & sc->rch.hwch)? 1 : 0; 3173ac1ca33SNick Sayer } else { 3183ac1ca33SNick Sayer if (sc->simplex_dir == PCMDIR_PLAY) 3193ac1ca33SNick Sayer pirq = 1; 3203ac1ca33SNick Sayer if (sc->simplex_dir == PCMDIR_REC) 3213ac1ca33SNick Sayer rirq = 1; 3223ac1ca33SNick Sayer if (!pirq && !rirq) 3233ac1ca33SNick Sayer printf("solo: IRQ neither playback nor rec!\n"); 3243ac1ca33SNick Sayer } 325fd1aaeccSCameron Grant 326a7e11506SNick Sayer DEB(printf("ess_intr: pirq:%d rirq:%d\n",pirq,rirq)); 327a7e11506SNick Sayer 328fd1aaeccSCameron Grant if (pirq) { 329fd1aaeccSCameron Grant if (sc->pch.stopping) { 330fd1aaeccSCameron Grant ess_dmatrigger(sc, sc->pch.hwch, 0); 331fd1aaeccSCameron Grant sc->pch.stopping = 0; 332fd1aaeccSCameron Grant if (sc->pch.hwch == 1) 333fd1aaeccSCameron Grant ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01); 334fd1aaeccSCameron Grant else 335fd1aaeccSCameron Grant ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03); 336fd1aaeccSCameron Grant } 3378b6d3fe1SAriff Abdullah ess_unlock(sc); 338fd1aaeccSCameron Grant chn_intr(sc->pch.channel); 3398b6d3fe1SAriff Abdullah ess_lock(sc); 340fd1aaeccSCameron Grant } 341fd1aaeccSCameron Grant 342fd1aaeccSCameron Grant if (rirq) { 343fd1aaeccSCameron Grant if (sc->rch.stopping) { 344fd1aaeccSCameron Grant ess_dmatrigger(sc, sc->rch.hwch, 0); 345fd1aaeccSCameron Grant sc->rch.stopping = 0; 346fd1aaeccSCameron Grant /* XXX: will this stop audio2? */ 347fd1aaeccSCameron Grant ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01); 348fd1aaeccSCameron Grant } 3498b6d3fe1SAriff Abdullah ess_unlock(sc); 350fd1aaeccSCameron Grant chn_intr(sc->rch.channel); 3518b6d3fe1SAriff Abdullah ess_lock(sc); 352fd1aaeccSCameron Grant } 353fd1aaeccSCameron Grant 354fd1aaeccSCameron Grant if (src & 2) 355fd1aaeccSCameron Grant ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80); 356fd1aaeccSCameron Grant if (src & 1) 357fd1aaeccSCameron Grant ess_rd(sc, DSP_DATA_AVAIL); 3588b6d3fe1SAriff Abdullah 3598b6d3fe1SAriff Abdullah ess_unlock(sc); 360fd1aaeccSCameron Grant } 361fd1aaeccSCameron Grant 362fd1aaeccSCameron Grant /* utility functions for ESS */ 363fd1aaeccSCameron Grant static u_int8_t 364fd1aaeccSCameron Grant ess_calcspeed8(int *spd) 365fd1aaeccSCameron Grant { 366fd1aaeccSCameron Grant int speed = *spd; 367fd1aaeccSCameron Grant u_int32_t t; 368fd1aaeccSCameron Grant 369fd1aaeccSCameron Grant if (speed > 22000) { 370fd1aaeccSCameron Grant t = (795500 + speed / 2) / speed; 371fd1aaeccSCameron Grant speed = (795500 + t / 2) / t; 372fd1aaeccSCameron Grant t = (256 - t) | 0x80; 373fd1aaeccSCameron Grant } else { 374fd1aaeccSCameron Grant t = (397700 + speed / 2) / speed; 375fd1aaeccSCameron Grant speed = (397700 + t / 2) / t; 376fd1aaeccSCameron Grant t = 128 - t; 377fd1aaeccSCameron Grant } 378fd1aaeccSCameron Grant *spd = speed; 379fd1aaeccSCameron Grant return t & 0x000000ff; 380fd1aaeccSCameron Grant } 381fd1aaeccSCameron Grant 382fd1aaeccSCameron Grant static u_int8_t 383fd1aaeccSCameron Grant ess_calcspeed9(int *spd) 384fd1aaeccSCameron Grant { 385fd1aaeccSCameron Grant int speed, s0, s1, use0; 386fd1aaeccSCameron Grant u_int8_t t0, t1; 387fd1aaeccSCameron Grant 388fd1aaeccSCameron Grant /* rate = source / (256 - divisor) */ 389fd1aaeccSCameron Grant /* divisor = 256 - (source / rate) */ 390fd1aaeccSCameron Grant speed = *spd; 391fd1aaeccSCameron Grant t0 = 128 - (793800 / speed); 392fd1aaeccSCameron Grant s0 = 793800 / (128 - t0); 393fd1aaeccSCameron Grant 394fd1aaeccSCameron Grant t1 = 128 - (768000 / speed); 395fd1aaeccSCameron Grant s1 = 768000 / (128 - t1); 396fd1aaeccSCameron Grant t1 |= 0x80; 397fd1aaeccSCameron Grant 398fd1aaeccSCameron Grant use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0; 399fd1aaeccSCameron Grant 400fd1aaeccSCameron Grant *spd = use0? s0 : s1; 401fd1aaeccSCameron Grant return use0? t0 : t1; 402fd1aaeccSCameron Grant } 403fd1aaeccSCameron Grant 404fd1aaeccSCameron Grant static u_int8_t 405fd1aaeccSCameron Grant ess_calcfilter(int spd) 406fd1aaeccSCameron Grant { 407fd1aaeccSCameron Grant int cutoff; 408fd1aaeccSCameron Grant 409fd1aaeccSCameron Grant /* cutoff = 7160000 / (256 - divisor) */ 410fd1aaeccSCameron Grant /* divisor = 256 - (7160000 / cutoff) */ 411fd1aaeccSCameron Grant cutoff = (spd * 9 * 82) / 20; 412fd1aaeccSCameron Grant return (256 - (7160000 / cutoff)); 413fd1aaeccSCameron Grant } 414fd1aaeccSCameron Grant 415fd1aaeccSCameron Grant static int 416fd1aaeccSCameron Grant ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len) 417fd1aaeccSCameron Grant { 418fd1aaeccSCameron Grant int play = (dir == PCMDIR_PLAY)? 1 : 0; 419fd1aaeccSCameron Grant int b16 = (fmt & AFMT_16BIT)? 1 : 0; 42090da2b28SAriff Abdullah int stereo = (AFMT_CHANNEL(fmt) > 1)? 1 : 0; 42190da2b28SAriff Abdullah int unsign = (!(fmt & AFMT_SIGNED))? 1 : 0; 422fd1aaeccSCameron Grant u_int8_t spdval, fmtval; 423fd1aaeccSCameron Grant 424a7e11506SNick Sayer DEB(printf("ess_setupch\n")); 425fd1aaeccSCameron Grant spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd); 426fd1aaeccSCameron Grant 4273ac1ca33SNick Sayer sc->simplex_dir = play ? PCMDIR_PLAY : PCMDIR_REC ; 4283ac1ca33SNick Sayer 429fd1aaeccSCameron Grant if (ch == 1) { 430fd1aaeccSCameron Grant KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad")); 431fd1aaeccSCameron Grant len = -len; 432fd1aaeccSCameron Grant /* transfer length low */ 433fd1aaeccSCameron Grant ess_write(sc, 0xa4, len & 0x00ff); 434fd1aaeccSCameron Grant /* transfer length high */ 435fd1aaeccSCameron Grant ess_write(sc, 0xa5, (len & 0xff00) >> 8); 436fd1aaeccSCameron Grant /* autoinit, dma dir */ 437a7e11506SNick Sayer ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a)); 438fd1aaeccSCameron Grant /* mono/stereo */ 439fd1aaeccSCameron Grant ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02)); 440fd1aaeccSCameron Grant /* demand mode, 4 bytes/xfer */ 441fd1aaeccSCameron Grant ess_write(sc, 0xb9, 0x02); 442fd1aaeccSCameron Grant /* sample rate */ 443fd1aaeccSCameron Grant ess_write(sc, 0xa1, spdval); 444fd1aaeccSCameron Grant /* filter cutoff */ 445fd1aaeccSCameron Grant ess_write(sc, 0xa2, ess_calcfilter(spd)); 446fd1aaeccSCameron Grant /* setup dac/adc */ 447fd1aaeccSCameron Grant /* 448fd1aaeccSCameron Grant if (play) 449fd1aaeccSCameron Grant ess_write(sc, 0xb6, unsign? 0x80 : 0x00); 450fd1aaeccSCameron Grant */ 451fd1aaeccSCameron Grant /* mono, b16: signed, load signal */ 452fd1aaeccSCameron Grant /* 453fd1aaeccSCameron Grant ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20)); 454fd1aaeccSCameron Grant */ 455fd1aaeccSCameron Grant /* setup fifo */ 456a7e11506SNick Sayer ess_write(sc, 0xb7, 0x91 | (unsign? 0x00 : 0x20) | 457fd1aaeccSCameron Grant (b16? 0x04 : 0x00) | 458fd1aaeccSCameron Grant (stereo? 0x08 : 0x40)); 459fd1aaeccSCameron Grant /* irq control */ 460fd1aaeccSCameron Grant ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50); 461fd1aaeccSCameron Grant /* drq control */ 462fd1aaeccSCameron Grant ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50); 463fd1aaeccSCameron Grant } else if (ch == 2) { 464fd1aaeccSCameron Grant KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad")); 465fd1aaeccSCameron Grant len >>= 1; 466fd1aaeccSCameron Grant len = -len; 467fd1aaeccSCameron Grant /* transfer length low */ 468fd1aaeccSCameron Grant ess_setmixer(sc, 0x74, len & 0x00ff); 469fd1aaeccSCameron Grant /* transfer length high */ 470fd1aaeccSCameron Grant ess_setmixer(sc, 0x76, (len & 0xff00) >> 8); 471fd1aaeccSCameron Grant /* autoinit, 4 bytes/req */ 472fd1aaeccSCameron Grant ess_setmixer(sc, 0x78, 0x10); 47380a8e065SNick Sayer fmtval = b16 | (stereo << 1) | ((!unsign) << 2); 474fd1aaeccSCameron Grant /* enable irq, set format */ 475fd1aaeccSCameron Grant ess_setmixer(sc, 0x7a, 0x40 | fmtval); 476fd1aaeccSCameron Grant if (sc->newspeed) { 477fd1aaeccSCameron Grant /* sample rate */ 478fd1aaeccSCameron Grant ess_setmixer(sc, 0x70, spdval); 479fd1aaeccSCameron Grant /* filter cutoff */ 480fd1aaeccSCameron Grant ess_setmixer(sc, 0x72, ess_calcfilter(spd)); 481fd1aaeccSCameron Grant } 482fd1aaeccSCameron Grant } 483fd1aaeccSCameron Grant return 0; 484fd1aaeccSCameron Grant } 485fd1aaeccSCameron Grant static int 486fd1aaeccSCameron Grant ess_start(struct ess_chinfo *ch) 487fd1aaeccSCameron Grant { 488fd1aaeccSCameron Grant struct ess_info *sc = ch->parent; 489fd1aaeccSCameron Grant 490a7e11506SNick Sayer DEB(printf("ess_start\n");); 491350a5fafSCameron Grant ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz); 492fd1aaeccSCameron Grant ch->stopping = 0; 49319a0702eSNick Sayer if (ch->hwch == 1) { 494fd1aaeccSCameron Grant ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01); 49519a0702eSNick Sayer if (ch->dir == PCMDIR_PLAY) { 4960edeb3dcSNick Sayer #if 0 49719a0702eSNick Sayer DELAY(100000); /* 100 ms */ 4980edeb3dcSNick Sayer #endif 49919a0702eSNick Sayer ess_cmd(sc, 0xd1); 50019a0702eSNick Sayer } 50119a0702eSNick Sayer } else 502fd1aaeccSCameron Grant ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03); 503fd1aaeccSCameron Grant return 0; 504fd1aaeccSCameron Grant } 505fd1aaeccSCameron Grant 506fd1aaeccSCameron Grant static int 507fd1aaeccSCameron Grant ess_stop(struct ess_chinfo *ch) 508fd1aaeccSCameron Grant { 509fd1aaeccSCameron Grant struct ess_info *sc = ch->parent; 510fd1aaeccSCameron Grant 511a7e11506SNick Sayer DEB(printf("ess_stop\n")); 512fd1aaeccSCameron Grant ch->stopping = 1; 513fd1aaeccSCameron Grant if (ch->hwch == 1) 514fd1aaeccSCameron Grant ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04); 515fd1aaeccSCameron Grant else 516fd1aaeccSCameron Grant ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10); 517a7e11506SNick Sayer DEB(printf("done with stop\n")); 518fd1aaeccSCameron Grant return 0; 519fd1aaeccSCameron Grant } 520fd1aaeccSCameron Grant 5210f55ac6cSCameron Grant /* -------------------------------------------------------------------- */ 522fd1aaeccSCameron Grant /* channel interface for ESS18xx */ 523fd1aaeccSCameron Grant static void * 52466ef8af5SCameron Grant esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir) 525fd1aaeccSCameron Grant { 526fd1aaeccSCameron Grant struct ess_info *sc = devinfo; 527fd1aaeccSCameron Grant struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch; 528fd1aaeccSCameron Grant 529a7e11506SNick Sayer DEB(printf("esschan_init\n")); 530fd1aaeccSCameron Grant ch->parent = sc; 531fd1aaeccSCameron Grant ch->channel = c; 532fd1aaeccSCameron Grant ch->buffer = b; 533306f91b6SCameron Grant ch->dir = dir; 5342e334adfSAriff Abdullah if (sndbuf_alloc(ch->buffer, sc->parent_dmat, 0, sc->bufsz) != 0) 535fd1aaeccSCameron Grant return NULL; 536fd1aaeccSCameron Grant ch->hwch = 1; 537fd1aaeccSCameron Grant if ((dir == PCMDIR_PLAY) && (sc->duplex)) 538fd1aaeccSCameron Grant ch->hwch = 2; 539fd1aaeccSCameron Grant return ch; 540fd1aaeccSCameron Grant } 541fd1aaeccSCameron Grant 542fd1aaeccSCameron Grant static int 5430f55ac6cSCameron Grant esschan_setformat(kobj_t obj, void *data, u_int32_t format) 544fd1aaeccSCameron Grant { 545fd1aaeccSCameron Grant struct ess_chinfo *ch = data; 546fd1aaeccSCameron Grant 547fd1aaeccSCameron Grant ch->fmt = format; 548fd1aaeccSCameron Grant return 0; 549fd1aaeccSCameron Grant } 550fd1aaeccSCameron Grant 55190da2b28SAriff Abdullah static u_int32_t 5520f55ac6cSCameron Grant esschan_setspeed(kobj_t obj, void *data, u_int32_t speed) 553fd1aaeccSCameron Grant { 554fd1aaeccSCameron Grant struct ess_chinfo *ch = data; 555fd1aaeccSCameron Grant struct ess_info *sc = ch->parent; 556fd1aaeccSCameron Grant 557fd1aaeccSCameron Grant ch->spd = speed; 558fd1aaeccSCameron Grant if (sc->newspeed) 559fd1aaeccSCameron Grant ess_calcspeed9(&ch->spd); 560fd1aaeccSCameron Grant else 561fd1aaeccSCameron Grant ess_calcspeed8(&ch->spd); 562fd1aaeccSCameron Grant return ch->spd; 563fd1aaeccSCameron Grant } 564fd1aaeccSCameron Grant 56590da2b28SAriff Abdullah static u_int32_t 5660f55ac6cSCameron Grant esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize) 567fd1aaeccSCameron Grant { 568350a5fafSCameron Grant struct ess_chinfo *ch = data; 569350a5fafSCameron Grant 570350a5fafSCameron Grant ch->blksz = blocksize; 571350a5fafSCameron Grant return ch->blksz; 572fd1aaeccSCameron Grant } 573fd1aaeccSCameron Grant 574fd1aaeccSCameron Grant static int 5750f55ac6cSCameron Grant esschan_trigger(kobj_t obj, void *data, int go) 576fd1aaeccSCameron Grant { 577fd1aaeccSCameron Grant struct ess_chinfo *ch = data; 578fd1aaeccSCameron Grant struct ess_info *sc = ch->parent; 579fd1aaeccSCameron Grant 580bdfbdcecSAriff Abdullah if (!PCMTRIG_COMMON(go)) 581fd1aaeccSCameron Grant return 0; 582fd1aaeccSCameron Grant 583bdfbdcecSAriff Abdullah DEB(printf("esschan_trigger: %d\n",go)); 584bdfbdcecSAriff Abdullah 5858b6d3fe1SAriff Abdullah ess_lock(sc); 586fd1aaeccSCameron Grant switch (go) { 587fd1aaeccSCameron Grant case PCMTRIG_START: 58838cc9942SOlivier Houchard ess_dmasetup(sc, ch->hwch, sndbuf_getbufaddr(ch->buffer), sndbuf_getsize(ch->buffer), ch->dir); 589fd1aaeccSCameron Grant ess_dmatrigger(sc, ch->hwch, 1); 590fd1aaeccSCameron Grant ess_start(ch); 591fd1aaeccSCameron Grant break; 592fd1aaeccSCameron Grant 593fd1aaeccSCameron Grant case PCMTRIG_STOP: 594fd1aaeccSCameron Grant case PCMTRIG_ABORT: 595fd1aaeccSCameron Grant default: 596fd1aaeccSCameron Grant ess_stop(ch); 597fd1aaeccSCameron Grant break; 598fd1aaeccSCameron Grant } 5998b6d3fe1SAriff Abdullah ess_unlock(sc); 600fd1aaeccSCameron Grant return 0; 601fd1aaeccSCameron Grant } 602fd1aaeccSCameron Grant 60390da2b28SAriff Abdullah static u_int32_t 6040f55ac6cSCameron Grant esschan_getptr(kobj_t obj, void *data) 605fd1aaeccSCameron Grant { 606fd1aaeccSCameron Grant struct ess_chinfo *ch = data; 607fd1aaeccSCameron Grant struct ess_info *sc = ch->parent; 60890da2b28SAriff Abdullah u_int32_t ret; 609fd1aaeccSCameron Grant 6108b6d3fe1SAriff Abdullah ess_lock(sc); 6118b6d3fe1SAriff Abdullah ret = ess_dmapos(sc, ch->hwch); 6128b6d3fe1SAriff Abdullah ess_unlock(sc); 6138b6d3fe1SAriff Abdullah return ret; 614fd1aaeccSCameron Grant } 615fd1aaeccSCameron Grant 61666ef8af5SCameron Grant static struct pcmchan_caps * 6170f55ac6cSCameron Grant esschan_getcaps(kobj_t obj, void *data) 618fd1aaeccSCameron Grant { 619fd1aaeccSCameron Grant struct ess_chinfo *ch = data; 620fd1aaeccSCameron Grant 621fd1aaeccSCameron Grant return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps; 622fd1aaeccSCameron Grant } 623fd1aaeccSCameron Grant 6240f55ac6cSCameron Grant static kobj_method_t esschan_methods[] = { 6250f55ac6cSCameron Grant KOBJMETHOD(channel_init, esschan_init), 6260f55ac6cSCameron Grant KOBJMETHOD(channel_setformat, esschan_setformat), 6270f55ac6cSCameron Grant KOBJMETHOD(channel_setspeed, esschan_setspeed), 6280f55ac6cSCameron Grant KOBJMETHOD(channel_setblocksize, esschan_setblocksize), 6290f55ac6cSCameron Grant KOBJMETHOD(channel_trigger, esschan_trigger), 6300f55ac6cSCameron Grant KOBJMETHOD(channel_getptr, esschan_getptr), 6310f55ac6cSCameron Grant KOBJMETHOD(channel_getcaps, esschan_getcaps), 63290da2b28SAriff Abdullah KOBJMETHOD_END 6330f55ac6cSCameron Grant }; 6340f55ac6cSCameron Grant CHANNEL_DECLARE(esschan); 6350f55ac6cSCameron Grant 636fd1aaeccSCameron Grant /************************************************************/ 637fd1aaeccSCameron Grant 638fd1aaeccSCameron Grant static int 63966ef8af5SCameron Grant essmix_init(struct snd_mixer *m) 640fd1aaeccSCameron Grant { 641fd1aaeccSCameron Grant struct ess_info *sc = mix_getdevinfo(m); 642fd1aaeccSCameron Grant 643fd1aaeccSCameron Grant mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE | 644fd1aaeccSCameron Grant SOUND_MASK_IMIX); 645fd1aaeccSCameron Grant 646fd1aaeccSCameron Grant mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE | 647fd1aaeccSCameron Grant SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME | 648fd1aaeccSCameron Grant SOUND_MASK_LINE1); 649fd1aaeccSCameron Grant 650fd1aaeccSCameron Grant ess_setmixer(sc, 0, 0); /* reset */ 651fd1aaeccSCameron Grant 652fd1aaeccSCameron Grant return 0; 653fd1aaeccSCameron Grant } 654fd1aaeccSCameron Grant 655fd1aaeccSCameron Grant static int 65666ef8af5SCameron Grant essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right) 657fd1aaeccSCameron Grant { 658fd1aaeccSCameron Grant struct ess_info *sc = mix_getdevinfo(m); 659fd1aaeccSCameron Grant int preg = 0, rreg = 0, l, r; 660fd1aaeccSCameron Grant 661fd1aaeccSCameron Grant l = (left * 15) / 100; 662fd1aaeccSCameron Grant r = (right * 15) / 100; 663fd1aaeccSCameron Grant switch (dev) { 664fd1aaeccSCameron Grant case SOUND_MIXER_SYNTH: 665fd1aaeccSCameron Grant preg = 0x36; 666fd1aaeccSCameron Grant rreg = 0x6b; 667fd1aaeccSCameron Grant break; 668fd1aaeccSCameron Grant 669fd1aaeccSCameron Grant case SOUND_MIXER_PCM: 670fd1aaeccSCameron Grant preg = 0x14; 671fd1aaeccSCameron Grant rreg = 0x7c; 672fd1aaeccSCameron Grant break; 673fd1aaeccSCameron Grant 674fd1aaeccSCameron Grant case SOUND_MIXER_LINE: 675fd1aaeccSCameron Grant preg = 0x3e; 676fd1aaeccSCameron Grant rreg = 0x6e; 677fd1aaeccSCameron Grant break; 678fd1aaeccSCameron Grant 679fd1aaeccSCameron Grant case SOUND_MIXER_MIC: 680fd1aaeccSCameron Grant preg = 0x1a; 681fd1aaeccSCameron Grant rreg = 0x68; 682fd1aaeccSCameron Grant break; 683fd1aaeccSCameron Grant 684fd1aaeccSCameron Grant case SOUND_MIXER_LINE1: 685fd1aaeccSCameron Grant preg = 0x3a; 686fd1aaeccSCameron Grant rreg = 0x6c; 687fd1aaeccSCameron Grant break; 688fd1aaeccSCameron Grant 689fd1aaeccSCameron Grant case SOUND_MIXER_CD: 690fd1aaeccSCameron Grant preg = 0x38; 691fd1aaeccSCameron Grant rreg = 0x6a; 692fd1aaeccSCameron Grant break; 693fd1aaeccSCameron Grant 694fd1aaeccSCameron Grant case SOUND_MIXER_VOLUME: 695fd1aaeccSCameron Grant l = left? (left * 63) / 100 : 64; 696fd1aaeccSCameron Grant r = right? (right * 63) / 100 : 64; 697fd1aaeccSCameron Grant ess_setmixer(sc, 0x60, l); 698fd1aaeccSCameron Grant ess_setmixer(sc, 0x62, r); 699fd1aaeccSCameron Grant left = (l == 64)? 0 : (l * 100) / 63; 700fd1aaeccSCameron Grant right = (r == 64)? 0 : (r * 100) / 63; 701fd1aaeccSCameron Grant return left | (right << 8); 702fd1aaeccSCameron Grant } 703fd1aaeccSCameron Grant 704fd1aaeccSCameron Grant if (preg) 705fd1aaeccSCameron Grant ess_setmixer(sc, preg, (l << 4) | r); 706fd1aaeccSCameron Grant if (rreg) 707fd1aaeccSCameron Grant ess_setmixer(sc, rreg, (l << 4) | r); 708fd1aaeccSCameron Grant 709fd1aaeccSCameron Grant left = (l * 100) / 15; 710fd1aaeccSCameron Grant right = (r * 100) / 15; 711fd1aaeccSCameron Grant 712fd1aaeccSCameron Grant return left | (right << 8); 713fd1aaeccSCameron Grant } 714fd1aaeccSCameron Grant 71590da2b28SAriff Abdullah static u_int32_t 71666ef8af5SCameron Grant essmix_setrecsrc(struct snd_mixer *m, u_int32_t src) 717fd1aaeccSCameron Grant { 718fd1aaeccSCameron Grant struct ess_info *sc = mix_getdevinfo(m); 719fd1aaeccSCameron Grant u_char recdev; 720fd1aaeccSCameron Grant 721fd1aaeccSCameron Grant switch (src) { 722fd1aaeccSCameron Grant case SOUND_MASK_CD: 723fd1aaeccSCameron Grant recdev = 0x02; 724fd1aaeccSCameron Grant break; 725fd1aaeccSCameron Grant 726fd1aaeccSCameron Grant case SOUND_MASK_LINE: 727fd1aaeccSCameron Grant recdev = 0x06; 728fd1aaeccSCameron Grant break; 729fd1aaeccSCameron Grant 730fd1aaeccSCameron Grant case SOUND_MASK_IMIX: 731fd1aaeccSCameron Grant recdev = 0x05; 732fd1aaeccSCameron Grant break; 733fd1aaeccSCameron Grant 734fd1aaeccSCameron Grant case SOUND_MASK_MIC: 735fd1aaeccSCameron Grant default: 736fd1aaeccSCameron Grant recdev = 0x00; 737fd1aaeccSCameron Grant src = SOUND_MASK_MIC; 738fd1aaeccSCameron Grant break; 739fd1aaeccSCameron Grant } 740fd1aaeccSCameron Grant 741fd1aaeccSCameron Grant ess_setmixer(sc, 0x1c, recdev); 742fd1aaeccSCameron Grant 743fd1aaeccSCameron Grant return src; 744fd1aaeccSCameron Grant } 745fd1aaeccSCameron Grant 7460f55ac6cSCameron Grant static kobj_method_t solomixer_methods[] = { 7470f55ac6cSCameron Grant KOBJMETHOD(mixer_init, essmix_init), 7480f55ac6cSCameron Grant KOBJMETHOD(mixer_set, essmix_set), 7490f55ac6cSCameron Grant KOBJMETHOD(mixer_setrecsrc, essmix_setrecsrc), 75090da2b28SAriff Abdullah KOBJMETHOD_END 7510f55ac6cSCameron Grant }; 7520f55ac6cSCameron Grant MIXER_DECLARE(solomixer); 7530f55ac6cSCameron Grant 754fd1aaeccSCameron Grant /************************************************************/ 755fd1aaeccSCameron Grant 756fd1aaeccSCameron Grant static int 757fd1aaeccSCameron Grant ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir) 758fd1aaeccSCameron Grant { 759fd1aaeccSCameron Grant KASSERT(ch == 1 || ch == 2, ("bad ch")); 760fd1aaeccSCameron Grant sc->dmasz[ch - 1] = cnt; 761fd1aaeccSCameron Grant if (ch == 1) { 76219a0702eSNick Sayer port_wr(sc->vc, 0x8, 0xc4, 1); /* command */ 763fd1aaeccSCameron Grant port_wr(sc->vc, 0xd, 0xff, 1); /* reset */ 764fd1aaeccSCameron Grant port_wr(sc->vc, 0xf, 0x01, 1); /* mask */ 76519a0702eSNick Sayer port_wr(sc->vc, 0xb, dir == PCMDIR_PLAY? 0x58 : 0x54, 1); /* mode */ 766fd1aaeccSCameron Grant port_wr(sc->vc, 0x0, base, 4); 767bb7f26c3SNick Sayer port_wr(sc->vc, 0x4, cnt - 1, 2); 768fd1aaeccSCameron Grant 769fd1aaeccSCameron Grant } else if (ch == 2) { 770fd1aaeccSCameron Grant port_wr(sc->io, 0x6, 0x08, 1); /* autoinit */ 771fd1aaeccSCameron Grant port_wr(sc->io, 0x0, base, 4); 772fd1aaeccSCameron Grant port_wr(sc->io, 0x4, cnt, 2); 773fd1aaeccSCameron Grant } 774fd1aaeccSCameron Grant return 0; 775fd1aaeccSCameron Grant } 776fd1aaeccSCameron Grant 777fd1aaeccSCameron Grant static int 778fd1aaeccSCameron Grant ess_dmapos(struct ess_info *sc, int ch) 779fd1aaeccSCameron Grant { 7806ba60b3cSNick Sayer int p = 0, i = 0, j = 0; 781fd1aaeccSCameron Grant 782fd1aaeccSCameron Grant KASSERT(ch == 1 || ch == 2, ("bad ch")); 7830edeb3dcSNick Sayer if (ch == 1) { 7840edeb3dcSNick Sayer /* 7850edeb3dcSNick Sayer * During recording, this register is known to give back 7860edeb3dcSNick Sayer * garbage if it's not quiescent while being read. That's 7876ba60b3cSNick Sayer * why we spl, stop the DMA, and try over and over until 7886ba60b3cSNick Sayer * adjacent reads are "close", in the right order and not 7896ba60b3cSNick Sayer * bigger than is otherwise possible. 7900edeb3dcSNick Sayer */ 7910edeb3dcSNick Sayer ess_dmatrigger(sc, ch, 0); 7920edeb3dcSNick Sayer DELAY(20); 7936ba60b3cSNick Sayer do { 7946ba60b3cSNick Sayer DELAY(10); 7956ba60b3cSNick Sayer if (j > 1) 7966ba60b3cSNick Sayer printf("DMA count reg bogus: %04x & %04x\n", 7976ba60b3cSNick Sayer i, p); 7986ba60b3cSNick Sayer i = port_rd(sc->vc, 0x4, 2) + 1; 799fd1aaeccSCameron Grant p = port_rd(sc->vc, 0x4, 2) + 1; 8006ba60b3cSNick Sayer } while ((p > sc->dmasz[ch - 1] || i < p || (p - i) > 0x8) && j++ < 1000); 8010edeb3dcSNick Sayer ess_dmatrigger(sc, ch, 1); 8020edeb3dcSNick Sayer } 803fd1aaeccSCameron Grant else if (ch == 2) 804fd1aaeccSCameron Grant p = port_rd(sc->io, 0x4, 2); 805fd1aaeccSCameron Grant return sc->dmasz[ch - 1] - p; 806fd1aaeccSCameron Grant } 807fd1aaeccSCameron Grant 808fd1aaeccSCameron Grant static int 809fd1aaeccSCameron Grant ess_dmatrigger(struct ess_info *sc, int ch, int go) 810fd1aaeccSCameron Grant { 811fd1aaeccSCameron Grant KASSERT(ch == 1 || ch == 2, ("bad ch")); 812fd1aaeccSCameron Grant if (ch == 1) 8138eb3acc9SNick Sayer port_wr(sc->vc, 0xf, go? 0x00 : 0x01, 1); /* mask */ 814fd1aaeccSCameron Grant else if (ch == 2) 815fd1aaeccSCameron Grant port_wr(sc->io, 0x6, 0x08 | (go? 0x02 : 0x00), 1); /* autoinit */ 816fd1aaeccSCameron Grant return 0; 817fd1aaeccSCameron Grant } 818fd1aaeccSCameron Grant 819fd1aaeccSCameron Grant static void 820fd1aaeccSCameron Grant ess_release_resources(struct ess_info *sc, device_t dev) 821fd1aaeccSCameron Grant { 822fd1aaeccSCameron Grant if (sc->irq) { 823306f91b6SCameron Grant if (sc->ih) 824306f91b6SCameron Grant bus_teardown_intr(dev, sc->irq, sc->ih); 825fd1aaeccSCameron Grant bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq); 82687d8fcc8SPedro F. Giffuni sc->irq = NULL; 827fd1aaeccSCameron Grant } 828fd1aaeccSCameron Grant if (sc->io) { 829e27951b2SJohn Baldwin bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->io); 83087d8fcc8SPedro F. Giffuni sc->io = NULL; 831fd1aaeccSCameron Grant } 832fd1aaeccSCameron Grant 833fd1aaeccSCameron Grant if (sc->sb) { 834e27951b2SJohn Baldwin bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(1), sc->sb); 83587d8fcc8SPedro F. Giffuni sc->sb = NULL; 836fd1aaeccSCameron Grant } 837fd1aaeccSCameron Grant 838fd1aaeccSCameron Grant if (sc->vc) { 839e27951b2SJohn Baldwin bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(2), sc->vc); 84087d8fcc8SPedro F. Giffuni sc->vc = NULL; 841fd1aaeccSCameron Grant } 842fd1aaeccSCameron Grant 843fd1aaeccSCameron Grant if (sc->mpu) { 844e27951b2SJohn Baldwin bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(3), sc->mpu); 84587d8fcc8SPedro F. Giffuni sc->mpu = NULL; 846fd1aaeccSCameron Grant } 847fd1aaeccSCameron Grant 848fd1aaeccSCameron Grant if (sc->gp) { 849e27951b2SJohn Baldwin bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(4), sc->gp); 85087d8fcc8SPedro F. Giffuni sc->gp = NULL; 851fd1aaeccSCameron Grant } 852fd1aaeccSCameron Grant 853306f91b6SCameron Grant if (sc->parent_dmat) { 854306f91b6SCameron Grant bus_dma_tag_destroy(sc->parent_dmat); 855306f91b6SCameron Grant sc->parent_dmat = 0; 856306f91b6SCameron Grant } 857306f91b6SCameron Grant 8588b6d3fe1SAriff Abdullah if (sc->lock) { 8598b6d3fe1SAriff Abdullah snd_mtxfree(sc->lock); 8608b6d3fe1SAriff Abdullah sc->lock = NULL; 8618b6d3fe1SAriff Abdullah } 8628b6d3fe1SAriff Abdullah 863fd1aaeccSCameron Grant free(sc, M_DEVBUF); 864fd1aaeccSCameron Grant } 865fd1aaeccSCameron Grant 866fd1aaeccSCameron Grant static int 867fd1aaeccSCameron Grant ess_alloc_resources(struct ess_info *sc, device_t dev) 868fd1aaeccSCameron Grant { 869fd1aaeccSCameron Grant int rid; 870fd1aaeccSCameron Grant 871e27951b2SJohn Baldwin rid = PCIR_BAR(0); 8725f96beb9SNate Lawson sc->io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 873fd1aaeccSCameron Grant 874e27951b2SJohn Baldwin rid = PCIR_BAR(1); 8755f96beb9SNate Lawson sc->sb = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 876fd1aaeccSCameron Grant 877e27951b2SJohn Baldwin rid = PCIR_BAR(2); 8785f96beb9SNate Lawson sc->vc = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 879fd1aaeccSCameron Grant 880e27951b2SJohn Baldwin rid = PCIR_BAR(3); 8815f96beb9SNate Lawson sc->mpu = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 882fd1aaeccSCameron Grant 883e27951b2SJohn Baldwin rid = PCIR_BAR(4); 8845f96beb9SNate Lawson sc->gp = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 885fd1aaeccSCameron Grant 886fd1aaeccSCameron Grant rid = 0; 8875f96beb9SNate Lawson sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 8885f96beb9SNate Lawson RF_ACTIVE | RF_SHAREABLE); 889fd1aaeccSCameron Grant 8904582b3a1SAriff Abdullah sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_solo softc"); 8918b6d3fe1SAriff Abdullah 8928b6d3fe1SAriff Abdullah return (sc->irq && sc->io && sc->sb && sc->vc && 8938b6d3fe1SAriff Abdullah sc->mpu && sc->gp && sc->lock)? 0 : ENXIO; 894fd1aaeccSCameron Grant } 895fd1aaeccSCameron Grant 896fd1aaeccSCameron Grant static int 897fd1aaeccSCameron Grant ess_probe(device_t dev) 898fd1aaeccSCameron Grant { 899fd1aaeccSCameron Grant char *s = NULL; 900fd1aaeccSCameron Grant u_int32_t subdev; 901fd1aaeccSCameron Grant 902fd1aaeccSCameron Grant subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev); 903fd1aaeccSCameron Grant switch (pci_get_devid(dev)) { 904fd1aaeccSCameron Grant case 0x1969125d: 905fd1aaeccSCameron Grant if (subdev == 0x8888125d) 906fd1aaeccSCameron Grant s = "ESS Solo-1E"; 907fd1aaeccSCameron Grant else if (subdev == 0x1818125d) 908fd1aaeccSCameron Grant s = "ESS Solo-1"; 909fd1aaeccSCameron Grant else 910fd1aaeccSCameron Grant s = "ESS Solo-1 (unknown vendor)"; 911fd1aaeccSCameron Grant break; 912fd1aaeccSCameron Grant } 913fd1aaeccSCameron Grant 914fd1aaeccSCameron Grant if (s) 915fd1aaeccSCameron Grant device_set_desc(dev, s); 916d2b677bbSWarner Losh return s ? BUS_PROBE_DEFAULT : ENXIO; 917fd1aaeccSCameron Grant } 918fd1aaeccSCameron Grant 919933ce6faSOrion Hodson #define ESS_PCI_LEGACYCONTROL 0x40 920933ce6faSOrion Hodson #define ESS_PCI_CONFIG 0x50 921933ce6faSOrion Hodson #define ESS_PCI_DDMACONTROL 0x60 922933ce6faSOrion Hodson 923933ce6faSOrion Hodson static int 924933ce6faSOrion Hodson ess_suspend(device_t dev) 925933ce6faSOrion Hodson { 926933ce6faSOrion Hodson return 0; 927933ce6faSOrion Hodson } 928933ce6faSOrion Hodson 929933ce6faSOrion Hodson static int 930933ce6faSOrion Hodson ess_resume(device_t dev) 931933ce6faSOrion Hodson { 932933ce6faSOrion Hodson uint16_t ddma; 933933ce6faSOrion Hodson struct ess_info *sc = pcm_getdevinfo(dev); 934933ce6faSOrion Hodson 9358b6d3fe1SAriff Abdullah ess_lock(sc); 936933ce6faSOrion Hodson ddma = rman_get_start(sc->vc) | 1; 937933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2); 938933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2); 939933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_CONFIG, 0, 2); 940933ce6faSOrion Hodson 9418b6d3fe1SAriff Abdullah if (ess_reset_dsp(sc)) { 9428b6d3fe1SAriff Abdullah ess_unlock(sc); 943933ce6faSOrion Hodson goto no; 9448b6d3fe1SAriff Abdullah } 9458b6d3fe1SAriff Abdullah ess_unlock(sc); 946933ce6faSOrion Hodson if (mixer_reinit(dev)) 947933ce6faSOrion Hodson goto no; 9488b6d3fe1SAriff Abdullah ess_lock(sc); 949933ce6faSOrion Hodson if (sc->newspeed) 950933ce6faSOrion Hodson ess_setmixer(sc, 0x71, 0x2a); 951933ce6faSOrion Hodson 952933ce6faSOrion Hodson port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */ 9538b6d3fe1SAriff Abdullah ess_unlock(sc); 954933ce6faSOrion Hodson 955933ce6faSOrion Hodson return 0; 956933ce6faSOrion Hodson no: 957933ce6faSOrion Hodson return EIO; 958933ce6faSOrion Hodson } 959fd1aaeccSCameron Grant 960fd1aaeccSCameron Grant static int 961fd1aaeccSCameron Grant ess_attach(device_t dev) 962fd1aaeccSCameron Grant { 963fd1aaeccSCameron Grant struct ess_info *sc; 964fd1aaeccSCameron Grant char status[SND_STATUSLEN]; 965fd1aaeccSCameron Grant u_int16_t ddma; 966fd1aaeccSCameron Grant 967082f6383SAriff Abdullah sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO); 968c68534f1SScott Long pci_enable_busmaster(dev); 969fd1aaeccSCameron Grant 970fd1aaeccSCameron Grant if (ess_alloc_resources(sc, dev)) 971fd1aaeccSCameron Grant goto no; 972fd1aaeccSCameron Grant 97339dbd126SCameron Grant sc->bufsz = pcm_getbuffersize(dev, 4096, SOLO_DEFAULT_BUFSZ, 65536); 97439dbd126SCameron Grant 975fd1aaeccSCameron Grant ddma = rman_get_start(sc->vc) | 1; 976933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2); 977933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2); 978933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_CONFIG, 0, 2); 979fd1aaeccSCameron Grant 980fd1aaeccSCameron Grant port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */ 9813ac1ca33SNick Sayer #ifdef ESS18XX_DUPLEX 982fd1aaeccSCameron Grant sc->duplex = 1; 9833ac1ca33SNick Sayer #else 9843ac1ca33SNick Sayer sc->duplex = 0; 9853ac1ca33SNick Sayer #endif 986fd1aaeccSCameron Grant 9873ac1ca33SNick Sayer #ifdef ESS18XX_NEWSPEED 9883ac1ca33SNick Sayer sc->newspeed = 1; 9893ac1ca33SNick Sayer #else 9903ac1ca33SNick Sayer sc->newspeed = 0; 9913ac1ca33SNick Sayer #endif 9921f7a6325SAlexander Motin if (snd_setup_intr(dev, sc->irq, INTR_MPSAFE, ess_intr, sc, &sc->ih)) { 9938b6d3fe1SAriff Abdullah device_printf(dev, "unable to map interrupt\n"); 9948b6d3fe1SAriff Abdullah goto no; 9958b6d3fe1SAriff Abdullah } 996fd1aaeccSCameron Grant 997fd1aaeccSCameron Grant if (!sc->duplex) 998fd1aaeccSCameron Grant pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX); 999fd1aaeccSCameron Grant 10008b6d3fe1SAriff Abdullah #if 0 10010b989078SAlexander Leidinger if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/65536, /*boundary*/0, 10028b6d3fe1SAriff Abdullah #endif 10030b989078SAlexander Leidinger if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2, /*boundary*/0, 1004fd1aaeccSCameron Grant /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 1005fd1aaeccSCameron Grant /*highaddr*/BUS_SPACE_MAXADDR, 1006fd1aaeccSCameron Grant /*filter*/NULL, /*filterarg*/NULL, 100739dbd126SCameron Grant /*maxsize*/sc->bufsz, /*nsegments*/1, 1008fd1aaeccSCameron Grant /*maxsegz*/0x3ffff, 10098b6d3fe1SAriff Abdullah /*flags*/0, 10108b6d3fe1SAriff Abdullah /*lockfunc*/NULL, /*lockarg*/NULL, 10118b6d3fe1SAriff Abdullah &sc->parent_dmat) != 0) { 1012fd1aaeccSCameron Grant device_printf(dev, "unable to create dma tag\n"); 1013fd1aaeccSCameron Grant goto no; 1014fd1aaeccSCameron Grant } 1015fd1aaeccSCameron Grant 10168b6d3fe1SAriff Abdullah if (ess_reset_dsp(sc)) 10178b6d3fe1SAriff Abdullah goto no; 10188b6d3fe1SAriff Abdullah 10198b6d3fe1SAriff Abdullah if (sc->newspeed) 10208b6d3fe1SAriff Abdullah ess_setmixer(sc, 0x71, 0x2a); 10218b6d3fe1SAriff Abdullah 10228b6d3fe1SAriff Abdullah if (mixer_init(dev, &solomixer_class, sc)) 10238b6d3fe1SAriff Abdullah goto no; 10248b6d3fe1SAriff Abdullah 1025*837cd192SChristos Margiolis snprintf(status, SND_STATUSLEN, "port 0x%jx,0x%jx,0x%jx irq %jd on %s", 1026fd1aaeccSCameron Grant rman_get_start(sc->io), rman_get_start(sc->sb), rman_get_start(sc->vc), 1027*837cd192SChristos Margiolis rman_get_start(sc->irq), 1028*837cd192SChristos Margiolis device_get_nameunit(device_get_parent(dev))); 1029fd1aaeccSCameron Grant 1030fd1aaeccSCameron Grant if (pcm_register(dev, sc, 1, 1)) 1031fd1aaeccSCameron Grant goto no; 10320f55ac6cSCameron Grant pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc); 10330f55ac6cSCameron Grant pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc); 1034fd1aaeccSCameron Grant pcm_setstatus(dev, status); 1035fd1aaeccSCameron Grant 1036fd1aaeccSCameron Grant return 0; 1037fd1aaeccSCameron Grant 1038fd1aaeccSCameron Grant no: 1039fd1aaeccSCameron Grant ess_release_resources(sc, dev); 1040fd1aaeccSCameron Grant return ENXIO; 1041fd1aaeccSCameron Grant } 1042fd1aaeccSCameron Grant 1043306f91b6SCameron Grant static int 1044306f91b6SCameron Grant ess_detach(device_t dev) 1045306f91b6SCameron Grant { 1046306f91b6SCameron Grant int r; 10477dfc9325SCameron Grant struct ess_info *sc; 1048306f91b6SCameron Grant 1049306f91b6SCameron Grant r = pcm_unregister(dev); 1050306f91b6SCameron Grant if (r) 1051306f91b6SCameron Grant return r; 1052306f91b6SCameron Grant 1053306f91b6SCameron Grant sc = pcm_getdevinfo(dev); 1054306f91b6SCameron Grant ess_release_resources(sc, dev); 1055306f91b6SCameron Grant return 0; 1056306f91b6SCameron Grant } 1057306f91b6SCameron Grant 1058fd1aaeccSCameron Grant static device_method_t ess_methods[] = { 1059fd1aaeccSCameron Grant /* Device interface */ 1060fd1aaeccSCameron Grant DEVMETHOD(device_probe, ess_probe), 1061fd1aaeccSCameron Grant DEVMETHOD(device_attach, ess_attach), 1062306f91b6SCameron Grant DEVMETHOD(device_detach, ess_detach), 1063933ce6faSOrion Hodson DEVMETHOD(device_resume, ess_resume), 1064933ce6faSOrion Hodson DEVMETHOD(device_suspend, ess_suspend), 1065fd1aaeccSCameron Grant { 0, 0 } 1066fd1aaeccSCameron Grant }; 1067fd1aaeccSCameron Grant 1068fd1aaeccSCameron Grant static driver_t ess_driver = { 1069fd1aaeccSCameron Grant "pcm", 1070fd1aaeccSCameron Grant ess_methods, 107167b1dce3SCameron Grant PCM_SOFTC_SIZE, 1072fd1aaeccSCameron Grant }; 1073fd1aaeccSCameron Grant 10742287364eSJohn Baldwin DRIVER_MODULE(snd_solo, pci, ess_driver, 0, 0); 10750739ea1dSSeigo Tanimura MODULE_DEPEND(snd_solo, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 1076fd1aaeccSCameron Grant MODULE_VERSION(snd_solo, 1); 1077