1098ca2bdSWarner Losh /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 4267b1dce3SCameron Grant SND_DECLARE_FILE("$FreeBSD$"); 4367b1dce3SCameron Grant 4439dbd126SCameron Grant #define SOLO_DEFAULT_BUFSZ 16384 45fd1aaeccSCameron Grant #define ABS(x) (((x) < 0)? -(x) : (x)) 46fd1aaeccSCameron Grant 473ac1ca33SNick Sayer /* if defined, playback always uses the 2nd channel and full duplex works */ 4886b391b2SAriff Abdullah #define ESS18XX_DUPLEX 1 49fd1aaeccSCameron Grant 50fd1aaeccSCameron Grant /* more accurate clocks and split audio1/audio2 rates */ 51fd1aaeccSCameron Grant #define ESS18XX_NEWSPEED 52fd1aaeccSCameron Grant 53513693beSCameron Grant static u_int32_t ess_playfmt[] = { 5490da2b28SAriff Abdullah SND_FORMAT(AFMT_U8, 1, 0), 5590da2b28SAriff Abdullah SND_FORMAT(AFMT_U8, 2, 0), 5690da2b28SAriff Abdullah SND_FORMAT(AFMT_S8, 1, 0), 5790da2b28SAriff Abdullah SND_FORMAT(AFMT_S8, 2, 0), 5890da2b28SAriff Abdullah SND_FORMAT(AFMT_S16_LE, 1, 0), 5990da2b28SAriff Abdullah SND_FORMAT(AFMT_S16_LE, 2, 0), 6090da2b28SAriff Abdullah SND_FORMAT(AFMT_U16_LE, 1, 0), 6190da2b28SAriff Abdullah SND_FORMAT(AFMT_U16_LE, 2, 0), 62513693beSCameron Grant 0 63fd1aaeccSCameron Grant }; 641333b4a4SAriff Abdullah static struct pcmchan_caps ess_playcaps = {6000, 48000, ess_playfmt, 0}; 65fd1aaeccSCameron Grant 663ac1ca33SNick Sayer /* 673ac1ca33SNick Sayer * Recording output is byte-swapped 683ac1ca33SNick Sayer */ 69513693beSCameron Grant static u_int32_t ess_recfmt[] = { 7090da2b28SAriff Abdullah SND_FORMAT(AFMT_U8, 1, 0), 7190da2b28SAriff Abdullah SND_FORMAT(AFMT_U8, 2, 0), 7290da2b28SAriff Abdullah SND_FORMAT(AFMT_S8, 1, 0), 7390da2b28SAriff Abdullah SND_FORMAT(AFMT_S8, 2, 0), 7490da2b28SAriff Abdullah SND_FORMAT(AFMT_S16_BE, 1, 0), 7590da2b28SAriff Abdullah SND_FORMAT(AFMT_S16_BE, 2, 0), 7690da2b28SAriff Abdullah SND_FORMAT(AFMT_U16_BE, 1, 0), 7790da2b28SAriff Abdullah SND_FORMAT(AFMT_U16_BE, 2, 0), 78513693beSCameron Grant 0 79fd1aaeccSCameron Grant }; 801333b4a4SAriff Abdullah static struct pcmchan_caps ess_reccaps = {6000, 48000, ess_recfmt, 0}; 81fd1aaeccSCameron Grant 82fd1aaeccSCameron Grant struct ess_info; 83fd1aaeccSCameron Grant 84fd1aaeccSCameron Grant struct ess_chinfo { 85fd1aaeccSCameron Grant struct ess_info *parent; 8666ef8af5SCameron Grant struct pcm_channel *channel; 8766ef8af5SCameron Grant struct snd_dbuf *buffer; 88fd1aaeccSCameron Grant int dir, hwch, stopping; 89350a5fafSCameron Grant u_int32_t fmt, spd, blksz; 90fd1aaeccSCameron Grant }; 91fd1aaeccSCameron Grant 92fd1aaeccSCameron Grant struct ess_info { 93fd1aaeccSCameron Grant struct resource *io, *sb, *vc, *mpu, *gp; /* I/O address for the board */ 94fd1aaeccSCameron Grant struct resource *irq; 95306f91b6SCameron Grant void *ih; 96fd1aaeccSCameron Grant bus_dma_tag_t parent_dmat; 97fd1aaeccSCameron Grant 9879462204SAriff Abdullah int simplex_dir, type, dmasz[2]; 9979462204SAriff Abdullah unsigned int duplex:1, newspeed:1; 10039dbd126SCameron Grant unsigned int bufsz; 10139dbd126SCameron Grant 102fd1aaeccSCameron Grant struct ess_chinfo pch, rch; 1038b6d3fe1SAriff Abdullah struct mtx *lock; 104fd1aaeccSCameron Grant }; 105fd1aaeccSCameron Grant 1068b6d3fe1SAriff Abdullah #define ess_lock(_ess) snd_mtxlock((_ess)->lock) 1078b6d3fe1SAriff Abdullah #define ess_unlock(_ess) snd_mtxunlock((_ess)->lock) 1088b6d3fe1SAriff Abdullah #define ess_lock_assert(_ess) snd_mtxassert((_ess)->lock) 1098b6d3fe1SAriff Abdullah 110fd1aaeccSCameron Grant static int ess_rd(struct ess_info *sc, int reg); 111fd1aaeccSCameron Grant static void ess_wr(struct ess_info *sc, int reg, u_int8_t val); 112fd1aaeccSCameron Grant static int ess_dspready(struct ess_info *sc); 113fd1aaeccSCameron Grant static int ess_cmd(struct ess_info *sc, u_char val); 114fd1aaeccSCameron Grant static int ess_cmd1(struct ess_info *sc, u_char cmd, int val); 115fd1aaeccSCameron Grant static int ess_get_byte(struct ess_info *sc); 116fd1aaeccSCameron Grant static void ess_setmixer(struct ess_info *sc, u_int port, u_int value); 117fd1aaeccSCameron Grant static int ess_getmixer(struct ess_info *sc, u_int port); 118fd1aaeccSCameron Grant static int ess_reset_dsp(struct ess_info *sc); 119fd1aaeccSCameron Grant 120fd1aaeccSCameron Grant static int ess_write(struct ess_info *sc, u_char reg, int val); 121fd1aaeccSCameron Grant static int ess_read(struct ess_info *sc, u_char reg); 122fd1aaeccSCameron Grant 123fd1aaeccSCameron Grant static void ess_intr(void *arg); 124fd1aaeccSCameron Grant static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len); 125fd1aaeccSCameron Grant static int ess_start(struct ess_chinfo *ch); 126fd1aaeccSCameron Grant static int ess_stop(struct ess_chinfo *ch); 127fd1aaeccSCameron Grant 128fd1aaeccSCameron Grant static int ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir); 129fd1aaeccSCameron Grant static int ess_dmapos(struct ess_info *sc, int ch); 130fd1aaeccSCameron Grant static int ess_dmatrigger(struct ess_info *sc, int ch, int go); 131fd1aaeccSCameron Grant 132fd1aaeccSCameron Grant /* 133fd1aaeccSCameron Grant * Common code for the midi and pcm functions 134fd1aaeccSCameron Grant * 135fd1aaeccSCameron Grant * ess_cmd write a single byte to the CMD port. 136fd1aaeccSCameron Grant * ess_cmd1 write a CMD + 1 byte arg 137fd1aaeccSCameron Grant * ess_cmd2 write a CMD + 2 byte arg 138fd1aaeccSCameron Grant * ess_get_byte returns a single byte from the DSP data port 139fd1aaeccSCameron Grant * 140fd1aaeccSCameron Grant * ess_write is actually ess_cmd1 141fd1aaeccSCameron Grant * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte 142fd1aaeccSCameron Grant */ 143fd1aaeccSCameron Grant 144fd1aaeccSCameron Grant static int 145fd1aaeccSCameron Grant port_rd(struct resource *port, int regno, int size) 146fd1aaeccSCameron Grant { 147fd1aaeccSCameron Grant bus_space_tag_t st = rman_get_bustag(port); 148fd1aaeccSCameron Grant bus_space_handle_t sh = rman_get_bushandle(port); 149fd1aaeccSCameron Grant 150fd1aaeccSCameron Grant switch (size) { 151fd1aaeccSCameron Grant case 1: 152fd1aaeccSCameron Grant return bus_space_read_1(st, sh, regno); 153fd1aaeccSCameron Grant case 2: 154fd1aaeccSCameron Grant return bus_space_read_2(st, sh, regno); 155fd1aaeccSCameron Grant case 4: 156fd1aaeccSCameron Grant return bus_space_read_4(st, sh, regno); 157fd1aaeccSCameron Grant default: 158fd1aaeccSCameron Grant return 0xffffffff; 159fd1aaeccSCameron Grant } 160fd1aaeccSCameron Grant } 161fd1aaeccSCameron Grant 162fd1aaeccSCameron Grant static void 163fd1aaeccSCameron Grant port_wr(struct resource *port, int regno, u_int32_t data, int size) 164fd1aaeccSCameron Grant { 165fd1aaeccSCameron Grant bus_space_tag_t st = rman_get_bustag(port); 166fd1aaeccSCameron Grant bus_space_handle_t sh = rman_get_bushandle(port); 167fd1aaeccSCameron Grant 168fd1aaeccSCameron Grant switch (size) { 169fd1aaeccSCameron Grant case 1: 170fd1aaeccSCameron Grant bus_space_write_1(st, sh, regno, data); 171fd1aaeccSCameron Grant break; 172fd1aaeccSCameron Grant case 2: 173fd1aaeccSCameron Grant bus_space_write_2(st, sh, regno, data); 174fd1aaeccSCameron Grant break; 175fd1aaeccSCameron Grant case 4: 176fd1aaeccSCameron Grant bus_space_write_4(st, sh, regno, data); 177fd1aaeccSCameron Grant break; 178fd1aaeccSCameron Grant } 179fd1aaeccSCameron Grant } 180fd1aaeccSCameron Grant 181fd1aaeccSCameron Grant static int 182fd1aaeccSCameron Grant ess_rd(struct ess_info *sc, int reg) 183fd1aaeccSCameron Grant { 184fd1aaeccSCameron Grant return port_rd(sc->sb, reg, 1); 185fd1aaeccSCameron Grant } 186fd1aaeccSCameron Grant 187fd1aaeccSCameron Grant static void 188fd1aaeccSCameron Grant ess_wr(struct ess_info *sc, int reg, u_int8_t val) 189fd1aaeccSCameron Grant { 190fd1aaeccSCameron Grant port_wr(sc->sb, reg, val, 1); 191fd1aaeccSCameron Grant } 192fd1aaeccSCameron Grant 193fd1aaeccSCameron Grant static int 194fd1aaeccSCameron Grant ess_dspready(struct ess_info *sc) 195fd1aaeccSCameron Grant { 196fd1aaeccSCameron Grant return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0); 197fd1aaeccSCameron Grant } 198fd1aaeccSCameron Grant 199fd1aaeccSCameron Grant static int 200fd1aaeccSCameron Grant ess_dspwr(struct ess_info *sc, u_char val) 201fd1aaeccSCameron Grant { 202fd1aaeccSCameron Grant int i; 203fd1aaeccSCameron Grant 204fd1aaeccSCameron Grant for (i = 0; i < 1000; i++) { 205fd1aaeccSCameron Grant if (ess_dspready(sc)) { 206fd1aaeccSCameron Grant ess_wr(sc, SBDSP_CMD, val); 207fd1aaeccSCameron Grant return 1; 208fd1aaeccSCameron Grant } 209fd1aaeccSCameron Grant if (i > 10) DELAY((i > 100)? 1000 : 10); 210fd1aaeccSCameron Grant } 211fd1aaeccSCameron Grant printf("ess_dspwr(0x%02x) timed out.\n", val); 212fd1aaeccSCameron Grant return 0; 213fd1aaeccSCameron Grant } 214fd1aaeccSCameron Grant 215fd1aaeccSCameron Grant static int 216fd1aaeccSCameron Grant ess_cmd(struct ess_info *sc, u_char val) 217fd1aaeccSCameron Grant { 218a7e11506SNick Sayer DEB(printf("ess_cmd: %x\n", val)); 219fd1aaeccSCameron Grant return ess_dspwr(sc, val); 220fd1aaeccSCameron Grant } 221fd1aaeccSCameron Grant 222fd1aaeccSCameron Grant static int 223fd1aaeccSCameron Grant ess_cmd1(struct ess_info *sc, u_char cmd, int val) 224fd1aaeccSCameron Grant { 225a7e11506SNick Sayer DEB(printf("ess_cmd1: %x, %x\n", cmd, val)); 226fd1aaeccSCameron Grant if (ess_dspwr(sc, cmd)) { 227fd1aaeccSCameron Grant return ess_dspwr(sc, val & 0xff); 228fd1aaeccSCameron Grant } else return 0; 229fd1aaeccSCameron Grant } 230fd1aaeccSCameron Grant 231fd1aaeccSCameron Grant static void 232fd1aaeccSCameron Grant ess_setmixer(struct ess_info *sc, u_int port, u_int value) 233fd1aaeccSCameron Grant { 234fd1aaeccSCameron Grant DEB(printf("ess_setmixer: reg=%x, val=%x\n", port, value);) 235fd1aaeccSCameron Grant ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 236fd1aaeccSCameron Grant DELAY(10); 237fd1aaeccSCameron Grant ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff)); 238fd1aaeccSCameron Grant DELAY(10); 239fd1aaeccSCameron Grant } 240fd1aaeccSCameron Grant 241fd1aaeccSCameron Grant static int 242fd1aaeccSCameron Grant ess_getmixer(struct ess_info *sc, u_int port) 243fd1aaeccSCameron Grant { 244fd1aaeccSCameron Grant int val; 245fd1aaeccSCameron Grant 246fd1aaeccSCameron Grant ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 247fd1aaeccSCameron Grant DELAY(10); 248fd1aaeccSCameron Grant val = ess_rd(sc, SB_MIX_DATA); 249fd1aaeccSCameron Grant DELAY(10); 250fd1aaeccSCameron Grant 251fd1aaeccSCameron Grant return val; 252fd1aaeccSCameron Grant } 253fd1aaeccSCameron Grant 254fd1aaeccSCameron Grant static int 255fd1aaeccSCameron Grant ess_get_byte(struct ess_info *sc) 256fd1aaeccSCameron Grant { 257fd1aaeccSCameron Grant int i; 258fd1aaeccSCameron Grant 259fd1aaeccSCameron Grant for (i = 1000; i > 0; i--) { 2600edeb3dcSNick Sayer if (ess_rd(sc, 0xc) & 0x40) 261fd1aaeccSCameron Grant return ess_rd(sc, DSP_READ); 262fd1aaeccSCameron Grant else 263fd1aaeccSCameron Grant DELAY(20); 264fd1aaeccSCameron Grant } 265fd1aaeccSCameron Grant return -1; 266fd1aaeccSCameron Grant } 267fd1aaeccSCameron Grant 268fd1aaeccSCameron Grant static int 269fd1aaeccSCameron Grant ess_write(struct ess_info *sc, u_char reg, int val) 270fd1aaeccSCameron Grant { 271fd1aaeccSCameron Grant return ess_cmd1(sc, reg, val); 272fd1aaeccSCameron Grant } 273fd1aaeccSCameron Grant 274fd1aaeccSCameron Grant static int 275fd1aaeccSCameron Grant ess_read(struct ess_info *sc, u_char reg) 276fd1aaeccSCameron Grant { 277fd1aaeccSCameron Grant return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1; 278fd1aaeccSCameron Grant } 279fd1aaeccSCameron Grant 280fd1aaeccSCameron Grant static int 281fd1aaeccSCameron Grant ess_reset_dsp(struct ess_info *sc) 282fd1aaeccSCameron Grant { 283a7e11506SNick Sayer DEB(printf("ess_reset_dsp\n")); 284fd1aaeccSCameron Grant ess_wr(sc, SBDSP_RST, 3); 285fd1aaeccSCameron Grant DELAY(100); 286fd1aaeccSCameron Grant ess_wr(sc, SBDSP_RST, 0); 287fd1aaeccSCameron Grant if (ess_get_byte(sc) != 0xAA) { 288a7e11506SNick Sayer DEB(printf("ess_reset_dsp failed\n")); 289a7e11506SNick Sayer /* 290fd1aaeccSCameron Grant rman_get_start(d->io_base))); 291a7e11506SNick Sayer */ 292fd1aaeccSCameron Grant return ENXIO; /* Sorry */ 293fd1aaeccSCameron Grant } 294fd1aaeccSCameron Grant ess_cmd(sc, 0xc6); 295fd1aaeccSCameron Grant return 0; 296fd1aaeccSCameron Grant } 297fd1aaeccSCameron Grant 298fd1aaeccSCameron Grant static void 299fd1aaeccSCameron Grant ess_intr(void *arg) 300fd1aaeccSCameron Grant { 301fd1aaeccSCameron Grant struct ess_info *sc = (struct ess_info *)arg; 3023ac1ca33SNick Sayer int src, pirq = 0, rirq = 0; 303fd1aaeccSCameron Grant 3048b6d3fe1SAriff Abdullah ess_lock(sc); 305fd1aaeccSCameron Grant src = 0; 306fd1aaeccSCameron Grant if (ess_getmixer(sc, 0x7a) & 0x80) 307fd1aaeccSCameron Grant src |= 2; 308fd1aaeccSCameron Grant if (ess_rd(sc, 0x0c) & 0x01) 309fd1aaeccSCameron Grant src |= 1; 310fd1aaeccSCameron Grant 3118d934d50SPyun YongHyeon if (src == 0) { 3128d934d50SPyun YongHyeon ess_unlock(sc); 3134e77c048SCameron Grant return; 3148d934d50SPyun YongHyeon } 3154e77c048SCameron Grant 3163ac1ca33SNick Sayer if (sc->duplex) { 317fd1aaeccSCameron Grant pirq = (src & sc->pch.hwch)? 1 : 0; 318fd1aaeccSCameron Grant rirq = (src & sc->rch.hwch)? 1 : 0; 3193ac1ca33SNick Sayer } else { 3203ac1ca33SNick Sayer if (sc->simplex_dir == PCMDIR_PLAY) 3213ac1ca33SNick Sayer pirq = 1; 3223ac1ca33SNick Sayer if (sc->simplex_dir == PCMDIR_REC) 3233ac1ca33SNick Sayer rirq = 1; 3243ac1ca33SNick Sayer if (!pirq && !rirq) 3253ac1ca33SNick Sayer printf("solo: IRQ neither playback nor rec!\n"); 3263ac1ca33SNick Sayer } 327fd1aaeccSCameron Grant 328a7e11506SNick Sayer DEB(printf("ess_intr: pirq:%d rirq:%d\n",pirq,rirq)); 329a7e11506SNick Sayer 330fd1aaeccSCameron Grant if (pirq) { 331fd1aaeccSCameron Grant if (sc->pch.stopping) { 332fd1aaeccSCameron Grant ess_dmatrigger(sc, sc->pch.hwch, 0); 333fd1aaeccSCameron Grant sc->pch.stopping = 0; 334fd1aaeccSCameron Grant if (sc->pch.hwch == 1) 335fd1aaeccSCameron Grant ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01); 336fd1aaeccSCameron Grant else 337fd1aaeccSCameron Grant ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03); 338fd1aaeccSCameron Grant } 3398b6d3fe1SAriff Abdullah ess_unlock(sc); 340fd1aaeccSCameron Grant chn_intr(sc->pch.channel); 3418b6d3fe1SAriff Abdullah ess_lock(sc); 342fd1aaeccSCameron Grant } 343fd1aaeccSCameron Grant 344fd1aaeccSCameron Grant if (rirq) { 345fd1aaeccSCameron Grant if (sc->rch.stopping) { 346fd1aaeccSCameron Grant ess_dmatrigger(sc, sc->rch.hwch, 0); 347fd1aaeccSCameron Grant sc->rch.stopping = 0; 348fd1aaeccSCameron Grant /* XXX: will this stop audio2? */ 349fd1aaeccSCameron Grant ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01); 350fd1aaeccSCameron Grant } 3518b6d3fe1SAriff Abdullah ess_unlock(sc); 352fd1aaeccSCameron Grant chn_intr(sc->rch.channel); 3538b6d3fe1SAriff Abdullah ess_lock(sc); 354fd1aaeccSCameron Grant } 355fd1aaeccSCameron Grant 356fd1aaeccSCameron Grant if (src & 2) 357fd1aaeccSCameron Grant ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80); 358fd1aaeccSCameron Grant if (src & 1) 359fd1aaeccSCameron Grant ess_rd(sc, DSP_DATA_AVAIL); 3608b6d3fe1SAriff Abdullah 3618b6d3fe1SAriff Abdullah ess_unlock(sc); 362fd1aaeccSCameron Grant } 363fd1aaeccSCameron Grant 364fd1aaeccSCameron Grant /* utility functions for ESS */ 365fd1aaeccSCameron Grant static u_int8_t 366fd1aaeccSCameron Grant ess_calcspeed8(int *spd) 367fd1aaeccSCameron Grant { 368fd1aaeccSCameron Grant int speed = *spd; 369fd1aaeccSCameron Grant u_int32_t t; 370fd1aaeccSCameron Grant 371fd1aaeccSCameron Grant if (speed > 22000) { 372fd1aaeccSCameron Grant t = (795500 + speed / 2) / speed; 373fd1aaeccSCameron Grant speed = (795500 + t / 2) / t; 374fd1aaeccSCameron Grant t = (256 - t) | 0x80; 375fd1aaeccSCameron Grant } else { 376fd1aaeccSCameron Grant t = (397700 + speed / 2) / speed; 377fd1aaeccSCameron Grant speed = (397700 + t / 2) / t; 378fd1aaeccSCameron Grant t = 128 - t; 379fd1aaeccSCameron Grant } 380fd1aaeccSCameron Grant *spd = speed; 381fd1aaeccSCameron Grant return t & 0x000000ff; 382fd1aaeccSCameron Grant } 383fd1aaeccSCameron Grant 384fd1aaeccSCameron Grant static u_int8_t 385fd1aaeccSCameron Grant ess_calcspeed9(int *spd) 386fd1aaeccSCameron Grant { 387fd1aaeccSCameron Grant int speed, s0, s1, use0; 388fd1aaeccSCameron Grant u_int8_t t0, t1; 389fd1aaeccSCameron Grant 390fd1aaeccSCameron Grant /* rate = source / (256 - divisor) */ 391fd1aaeccSCameron Grant /* divisor = 256 - (source / rate) */ 392fd1aaeccSCameron Grant speed = *spd; 393fd1aaeccSCameron Grant t0 = 128 - (793800 / speed); 394fd1aaeccSCameron Grant s0 = 793800 / (128 - t0); 395fd1aaeccSCameron Grant 396fd1aaeccSCameron Grant t1 = 128 - (768000 / speed); 397fd1aaeccSCameron Grant s1 = 768000 / (128 - t1); 398fd1aaeccSCameron Grant t1 |= 0x80; 399fd1aaeccSCameron Grant 400fd1aaeccSCameron Grant use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0; 401fd1aaeccSCameron Grant 402fd1aaeccSCameron Grant *spd = use0? s0 : s1; 403fd1aaeccSCameron Grant return use0? t0 : t1; 404fd1aaeccSCameron Grant } 405fd1aaeccSCameron Grant 406fd1aaeccSCameron Grant static u_int8_t 407fd1aaeccSCameron Grant ess_calcfilter(int spd) 408fd1aaeccSCameron Grant { 409fd1aaeccSCameron Grant int cutoff; 410fd1aaeccSCameron Grant 411fd1aaeccSCameron Grant /* cutoff = 7160000 / (256 - divisor) */ 412fd1aaeccSCameron Grant /* divisor = 256 - (7160000 / cutoff) */ 413fd1aaeccSCameron Grant cutoff = (spd * 9 * 82) / 20; 414fd1aaeccSCameron Grant return (256 - (7160000 / cutoff)); 415fd1aaeccSCameron Grant } 416fd1aaeccSCameron Grant 417fd1aaeccSCameron Grant static int 418fd1aaeccSCameron Grant ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len) 419fd1aaeccSCameron Grant { 420fd1aaeccSCameron Grant int play = (dir == PCMDIR_PLAY)? 1 : 0; 421fd1aaeccSCameron Grant int b16 = (fmt & AFMT_16BIT)? 1 : 0; 42290da2b28SAriff Abdullah int stereo = (AFMT_CHANNEL(fmt) > 1)? 1 : 0; 42390da2b28SAriff Abdullah int unsign = (!(fmt & AFMT_SIGNED))? 1 : 0; 424fd1aaeccSCameron Grant u_int8_t spdval, fmtval; 425fd1aaeccSCameron Grant 426a7e11506SNick Sayer DEB(printf("ess_setupch\n")); 427fd1aaeccSCameron Grant spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd); 428fd1aaeccSCameron Grant 4293ac1ca33SNick Sayer sc->simplex_dir = play ? PCMDIR_PLAY : PCMDIR_REC ; 4303ac1ca33SNick Sayer 431fd1aaeccSCameron Grant if (ch == 1) { 432fd1aaeccSCameron Grant KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad")); 433fd1aaeccSCameron Grant len = -len; 434fd1aaeccSCameron Grant /* transfer length low */ 435fd1aaeccSCameron Grant ess_write(sc, 0xa4, len & 0x00ff); 436fd1aaeccSCameron Grant /* transfer length high */ 437fd1aaeccSCameron Grant ess_write(sc, 0xa5, (len & 0xff00) >> 8); 438fd1aaeccSCameron Grant /* autoinit, dma dir */ 439a7e11506SNick Sayer ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a)); 440fd1aaeccSCameron Grant /* mono/stereo */ 441fd1aaeccSCameron Grant ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02)); 442fd1aaeccSCameron Grant /* demand mode, 4 bytes/xfer */ 443fd1aaeccSCameron Grant ess_write(sc, 0xb9, 0x02); 444fd1aaeccSCameron Grant /* sample rate */ 445fd1aaeccSCameron Grant ess_write(sc, 0xa1, spdval); 446fd1aaeccSCameron Grant /* filter cutoff */ 447fd1aaeccSCameron Grant ess_write(sc, 0xa2, ess_calcfilter(spd)); 448fd1aaeccSCameron Grant /* setup dac/adc */ 449fd1aaeccSCameron Grant /* 450fd1aaeccSCameron Grant if (play) 451fd1aaeccSCameron Grant ess_write(sc, 0xb6, unsign? 0x80 : 0x00); 452fd1aaeccSCameron Grant */ 453fd1aaeccSCameron Grant /* mono, b16: signed, load signal */ 454fd1aaeccSCameron Grant /* 455fd1aaeccSCameron Grant ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20)); 456fd1aaeccSCameron Grant */ 457fd1aaeccSCameron Grant /* setup fifo */ 458a7e11506SNick Sayer ess_write(sc, 0xb7, 0x91 | (unsign? 0x00 : 0x20) | 459fd1aaeccSCameron Grant (b16? 0x04 : 0x00) | 460fd1aaeccSCameron Grant (stereo? 0x08 : 0x40)); 461fd1aaeccSCameron Grant /* irq control */ 462fd1aaeccSCameron Grant ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50); 463fd1aaeccSCameron Grant /* drq control */ 464fd1aaeccSCameron Grant ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50); 465fd1aaeccSCameron Grant } else if (ch == 2) { 466fd1aaeccSCameron Grant KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad")); 467fd1aaeccSCameron Grant len >>= 1; 468fd1aaeccSCameron Grant len = -len; 469fd1aaeccSCameron Grant /* transfer length low */ 470fd1aaeccSCameron Grant ess_setmixer(sc, 0x74, len & 0x00ff); 471fd1aaeccSCameron Grant /* transfer length high */ 472fd1aaeccSCameron Grant ess_setmixer(sc, 0x76, (len & 0xff00) >> 8); 473fd1aaeccSCameron Grant /* autoinit, 4 bytes/req */ 474fd1aaeccSCameron Grant ess_setmixer(sc, 0x78, 0x10); 47580a8e065SNick Sayer fmtval = b16 | (stereo << 1) | ((!unsign) << 2); 476fd1aaeccSCameron Grant /* enable irq, set format */ 477fd1aaeccSCameron Grant ess_setmixer(sc, 0x7a, 0x40 | fmtval); 478fd1aaeccSCameron Grant if (sc->newspeed) { 479fd1aaeccSCameron Grant /* sample rate */ 480fd1aaeccSCameron Grant ess_setmixer(sc, 0x70, spdval); 481fd1aaeccSCameron Grant /* filter cutoff */ 482fd1aaeccSCameron Grant ess_setmixer(sc, 0x72, ess_calcfilter(spd)); 483fd1aaeccSCameron Grant } 484fd1aaeccSCameron Grant } 485fd1aaeccSCameron Grant return 0; 486fd1aaeccSCameron Grant } 487fd1aaeccSCameron Grant static int 488fd1aaeccSCameron Grant ess_start(struct ess_chinfo *ch) 489fd1aaeccSCameron Grant { 490fd1aaeccSCameron Grant struct ess_info *sc = ch->parent; 491fd1aaeccSCameron Grant 492a7e11506SNick Sayer DEB(printf("ess_start\n");); 493350a5fafSCameron Grant ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz); 494fd1aaeccSCameron Grant ch->stopping = 0; 49519a0702eSNick Sayer if (ch->hwch == 1) { 496fd1aaeccSCameron Grant ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01); 49719a0702eSNick Sayer if (ch->dir == PCMDIR_PLAY) { 4980edeb3dcSNick Sayer #if 0 49919a0702eSNick Sayer DELAY(100000); /* 100 ms */ 5000edeb3dcSNick Sayer #endif 50119a0702eSNick Sayer ess_cmd(sc, 0xd1); 50219a0702eSNick Sayer } 50319a0702eSNick Sayer } else 504fd1aaeccSCameron Grant ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03); 505fd1aaeccSCameron Grant return 0; 506fd1aaeccSCameron Grant } 507fd1aaeccSCameron Grant 508fd1aaeccSCameron Grant static int 509fd1aaeccSCameron Grant ess_stop(struct ess_chinfo *ch) 510fd1aaeccSCameron Grant { 511fd1aaeccSCameron Grant struct ess_info *sc = ch->parent; 512fd1aaeccSCameron Grant 513a7e11506SNick Sayer DEB(printf("ess_stop\n")); 514fd1aaeccSCameron Grant ch->stopping = 1; 515fd1aaeccSCameron Grant if (ch->hwch == 1) 516fd1aaeccSCameron Grant ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04); 517fd1aaeccSCameron Grant else 518fd1aaeccSCameron Grant ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10); 519a7e11506SNick Sayer DEB(printf("done with stop\n")); 520fd1aaeccSCameron Grant return 0; 521fd1aaeccSCameron Grant } 522fd1aaeccSCameron Grant 5230f55ac6cSCameron Grant /* -------------------------------------------------------------------- */ 524fd1aaeccSCameron Grant /* channel interface for ESS18xx */ 525fd1aaeccSCameron Grant static void * 52666ef8af5SCameron Grant esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir) 527fd1aaeccSCameron Grant { 528fd1aaeccSCameron Grant struct ess_info *sc = devinfo; 529fd1aaeccSCameron Grant struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch; 530fd1aaeccSCameron Grant 531a7e11506SNick Sayer DEB(printf("esschan_init\n")); 532fd1aaeccSCameron Grant ch->parent = sc; 533fd1aaeccSCameron Grant ch->channel = c; 534fd1aaeccSCameron Grant ch->buffer = b; 535306f91b6SCameron Grant ch->dir = dir; 5362e334adfSAriff Abdullah if (sndbuf_alloc(ch->buffer, sc->parent_dmat, 0, sc->bufsz) != 0) 537fd1aaeccSCameron Grant return NULL; 538fd1aaeccSCameron Grant ch->hwch = 1; 539fd1aaeccSCameron Grant if ((dir == PCMDIR_PLAY) && (sc->duplex)) 540fd1aaeccSCameron Grant ch->hwch = 2; 541fd1aaeccSCameron Grant return ch; 542fd1aaeccSCameron Grant } 543fd1aaeccSCameron Grant 544fd1aaeccSCameron Grant static int 5450f55ac6cSCameron Grant esschan_setformat(kobj_t obj, void *data, u_int32_t format) 546fd1aaeccSCameron Grant { 547fd1aaeccSCameron Grant struct ess_chinfo *ch = data; 548fd1aaeccSCameron Grant 549fd1aaeccSCameron Grant ch->fmt = format; 550fd1aaeccSCameron Grant return 0; 551fd1aaeccSCameron Grant } 552fd1aaeccSCameron Grant 55390da2b28SAriff Abdullah static u_int32_t 5540f55ac6cSCameron Grant esschan_setspeed(kobj_t obj, void *data, u_int32_t speed) 555fd1aaeccSCameron Grant { 556fd1aaeccSCameron Grant struct ess_chinfo *ch = data; 557fd1aaeccSCameron Grant struct ess_info *sc = ch->parent; 558fd1aaeccSCameron Grant 559fd1aaeccSCameron Grant ch->spd = speed; 560fd1aaeccSCameron Grant if (sc->newspeed) 561fd1aaeccSCameron Grant ess_calcspeed9(&ch->spd); 562fd1aaeccSCameron Grant else 563fd1aaeccSCameron Grant ess_calcspeed8(&ch->spd); 564fd1aaeccSCameron Grant return ch->spd; 565fd1aaeccSCameron Grant } 566fd1aaeccSCameron Grant 56790da2b28SAriff Abdullah static u_int32_t 5680f55ac6cSCameron Grant esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize) 569fd1aaeccSCameron Grant { 570350a5fafSCameron Grant struct ess_chinfo *ch = data; 571350a5fafSCameron Grant 572350a5fafSCameron Grant ch->blksz = blocksize; 573350a5fafSCameron Grant return ch->blksz; 574fd1aaeccSCameron Grant } 575fd1aaeccSCameron Grant 576fd1aaeccSCameron Grant static int 5770f55ac6cSCameron Grant esschan_trigger(kobj_t obj, void *data, int go) 578fd1aaeccSCameron Grant { 579fd1aaeccSCameron Grant struct ess_chinfo *ch = data; 580fd1aaeccSCameron Grant struct ess_info *sc = ch->parent; 581fd1aaeccSCameron Grant 582bdfbdcecSAriff Abdullah if (!PCMTRIG_COMMON(go)) 583fd1aaeccSCameron Grant return 0; 584fd1aaeccSCameron Grant 585bdfbdcecSAriff Abdullah DEB(printf("esschan_trigger: %d\n",go)); 586bdfbdcecSAriff Abdullah 5878b6d3fe1SAriff Abdullah ess_lock(sc); 588fd1aaeccSCameron Grant switch (go) { 589fd1aaeccSCameron Grant case PCMTRIG_START: 59038cc9942SOlivier Houchard ess_dmasetup(sc, ch->hwch, sndbuf_getbufaddr(ch->buffer), sndbuf_getsize(ch->buffer), ch->dir); 591fd1aaeccSCameron Grant ess_dmatrigger(sc, ch->hwch, 1); 592fd1aaeccSCameron Grant ess_start(ch); 593fd1aaeccSCameron Grant break; 594fd1aaeccSCameron Grant 595fd1aaeccSCameron Grant case PCMTRIG_STOP: 596fd1aaeccSCameron Grant case PCMTRIG_ABORT: 597fd1aaeccSCameron Grant default: 598fd1aaeccSCameron Grant ess_stop(ch); 599fd1aaeccSCameron Grant break; 600fd1aaeccSCameron Grant } 6018b6d3fe1SAriff Abdullah ess_unlock(sc); 602fd1aaeccSCameron Grant return 0; 603fd1aaeccSCameron Grant } 604fd1aaeccSCameron Grant 60590da2b28SAriff Abdullah static u_int32_t 6060f55ac6cSCameron Grant esschan_getptr(kobj_t obj, void *data) 607fd1aaeccSCameron Grant { 608fd1aaeccSCameron Grant struct ess_chinfo *ch = data; 609fd1aaeccSCameron Grant struct ess_info *sc = ch->parent; 61090da2b28SAriff Abdullah u_int32_t ret; 611fd1aaeccSCameron Grant 6128b6d3fe1SAriff Abdullah ess_lock(sc); 6138b6d3fe1SAriff Abdullah ret = ess_dmapos(sc, ch->hwch); 6148b6d3fe1SAriff Abdullah ess_unlock(sc); 6158b6d3fe1SAriff Abdullah return ret; 616fd1aaeccSCameron Grant } 617fd1aaeccSCameron Grant 61866ef8af5SCameron Grant static struct pcmchan_caps * 6190f55ac6cSCameron Grant esschan_getcaps(kobj_t obj, void *data) 620fd1aaeccSCameron Grant { 621fd1aaeccSCameron Grant struct ess_chinfo *ch = data; 622fd1aaeccSCameron Grant 623fd1aaeccSCameron Grant return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps; 624fd1aaeccSCameron Grant } 625fd1aaeccSCameron Grant 6260f55ac6cSCameron Grant static kobj_method_t esschan_methods[] = { 6270f55ac6cSCameron Grant KOBJMETHOD(channel_init, esschan_init), 6280f55ac6cSCameron Grant KOBJMETHOD(channel_setformat, esschan_setformat), 6290f55ac6cSCameron Grant KOBJMETHOD(channel_setspeed, esschan_setspeed), 6300f55ac6cSCameron Grant KOBJMETHOD(channel_setblocksize, esschan_setblocksize), 6310f55ac6cSCameron Grant KOBJMETHOD(channel_trigger, esschan_trigger), 6320f55ac6cSCameron Grant KOBJMETHOD(channel_getptr, esschan_getptr), 6330f55ac6cSCameron Grant KOBJMETHOD(channel_getcaps, esschan_getcaps), 63490da2b28SAriff Abdullah KOBJMETHOD_END 6350f55ac6cSCameron Grant }; 6360f55ac6cSCameron Grant CHANNEL_DECLARE(esschan); 6370f55ac6cSCameron Grant 638fd1aaeccSCameron Grant /************************************************************/ 639fd1aaeccSCameron Grant 640fd1aaeccSCameron Grant static int 64166ef8af5SCameron Grant essmix_init(struct snd_mixer *m) 642fd1aaeccSCameron Grant { 643fd1aaeccSCameron Grant struct ess_info *sc = mix_getdevinfo(m); 644fd1aaeccSCameron Grant 645fd1aaeccSCameron Grant mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE | 646fd1aaeccSCameron Grant SOUND_MASK_IMIX); 647fd1aaeccSCameron Grant 648fd1aaeccSCameron Grant mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE | 649fd1aaeccSCameron Grant SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME | 650fd1aaeccSCameron Grant SOUND_MASK_LINE1); 651fd1aaeccSCameron Grant 652fd1aaeccSCameron Grant ess_setmixer(sc, 0, 0); /* reset */ 653fd1aaeccSCameron Grant 654fd1aaeccSCameron Grant return 0; 655fd1aaeccSCameron Grant } 656fd1aaeccSCameron Grant 657fd1aaeccSCameron Grant static int 65866ef8af5SCameron Grant essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right) 659fd1aaeccSCameron Grant { 660fd1aaeccSCameron Grant struct ess_info *sc = mix_getdevinfo(m); 661fd1aaeccSCameron Grant int preg = 0, rreg = 0, l, r; 662fd1aaeccSCameron Grant 663fd1aaeccSCameron Grant l = (left * 15) / 100; 664fd1aaeccSCameron Grant r = (right * 15) / 100; 665fd1aaeccSCameron Grant switch (dev) { 666fd1aaeccSCameron Grant case SOUND_MIXER_SYNTH: 667fd1aaeccSCameron Grant preg = 0x36; 668fd1aaeccSCameron Grant rreg = 0x6b; 669fd1aaeccSCameron Grant break; 670fd1aaeccSCameron Grant 671fd1aaeccSCameron Grant case SOUND_MIXER_PCM: 672fd1aaeccSCameron Grant preg = 0x14; 673fd1aaeccSCameron Grant rreg = 0x7c; 674fd1aaeccSCameron Grant break; 675fd1aaeccSCameron Grant 676fd1aaeccSCameron Grant case SOUND_MIXER_LINE: 677fd1aaeccSCameron Grant preg = 0x3e; 678fd1aaeccSCameron Grant rreg = 0x6e; 679fd1aaeccSCameron Grant break; 680fd1aaeccSCameron Grant 681fd1aaeccSCameron Grant case SOUND_MIXER_MIC: 682fd1aaeccSCameron Grant preg = 0x1a; 683fd1aaeccSCameron Grant rreg = 0x68; 684fd1aaeccSCameron Grant break; 685fd1aaeccSCameron Grant 686fd1aaeccSCameron Grant case SOUND_MIXER_LINE1: 687fd1aaeccSCameron Grant preg = 0x3a; 688fd1aaeccSCameron Grant rreg = 0x6c; 689fd1aaeccSCameron Grant break; 690fd1aaeccSCameron Grant 691fd1aaeccSCameron Grant case SOUND_MIXER_CD: 692fd1aaeccSCameron Grant preg = 0x38; 693fd1aaeccSCameron Grant rreg = 0x6a; 694fd1aaeccSCameron Grant break; 695fd1aaeccSCameron Grant 696fd1aaeccSCameron Grant case SOUND_MIXER_VOLUME: 697fd1aaeccSCameron Grant l = left? (left * 63) / 100 : 64; 698fd1aaeccSCameron Grant r = right? (right * 63) / 100 : 64; 699fd1aaeccSCameron Grant ess_setmixer(sc, 0x60, l); 700fd1aaeccSCameron Grant ess_setmixer(sc, 0x62, r); 701fd1aaeccSCameron Grant left = (l == 64)? 0 : (l * 100) / 63; 702fd1aaeccSCameron Grant right = (r == 64)? 0 : (r * 100) / 63; 703fd1aaeccSCameron Grant return left | (right << 8); 704fd1aaeccSCameron Grant } 705fd1aaeccSCameron Grant 706fd1aaeccSCameron Grant if (preg) 707fd1aaeccSCameron Grant ess_setmixer(sc, preg, (l << 4) | r); 708fd1aaeccSCameron Grant if (rreg) 709fd1aaeccSCameron Grant ess_setmixer(sc, rreg, (l << 4) | r); 710fd1aaeccSCameron Grant 711fd1aaeccSCameron Grant left = (l * 100) / 15; 712fd1aaeccSCameron Grant right = (r * 100) / 15; 713fd1aaeccSCameron Grant 714fd1aaeccSCameron Grant return left | (right << 8); 715fd1aaeccSCameron Grant } 716fd1aaeccSCameron Grant 71790da2b28SAriff Abdullah static u_int32_t 71866ef8af5SCameron Grant essmix_setrecsrc(struct snd_mixer *m, u_int32_t src) 719fd1aaeccSCameron Grant { 720fd1aaeccSCameron Grant struct ess_info *sc = mix_getdevinfo(m); 721fd1aaeccSCameron Grant u_char recdev; 722fd1aaeccSCameron Grant 723fd1aaeccSCameron Grant switch (src) { 724fd1aaeccSCameron Grant case SOUND_MASK_CD: 725fd1aaeccSCameron Grant recdev = 0x02; 726fd1aaeccSCameron Grant break; 727fd1aaeccSCameron Grant 728fd1aaeccSCameron Grant case SOUND_MASK_LINE: 729fd1aaeccSCameron Grant recdev = 0x06; 730fd1aaeccSCameron Grant break; 731fd1aaeccSCameron Grant 732fd1aaeccSCameron Grant case SOUND_MASK_IMIX: 733fd1aaeccSCameron Grant recdev = 0x05; 734fd1aaeccSCameron Grant break; 735fd1aaeccSCameron Grant 736fd1aaeccSCameron Grant case SOUND_MASK_MIC: 737fd1aaeccSCameron Grant default: 738fd1aaeccSCameron Grant recdev = 0x00; 739fd1aaeccSCameron Grant src = SOUND_MASK_MIC; 740fd1aaeccSCameron Grant break; 741fd1aaeccSCameron Grant } 742fd1aaeccSCameron Grant 743fd1aaeccSCameron Grant ess_setmixer(sc, 0x1c, recdev); 744fd1aaeccSCameron Grant 745fd1aaeccSCameron Grant return src; 746fd1aaeccSCameron Grant } 747fd1aaeccSCameron Grant 7480f55ac6cSCameron Grant static kobj_method_t solomixer_methods[] = { 7490f55ac6cSCameron Grant KOBJMETHOD(mixer_init, essmix_init), 7500f55ac6cSCameron Grant KOBJMETHOD(mixer_set, essmix_set), 7510f55ac6cSCameron Grant KOBJMETHOD(mixer_setrecsrc, essmix_setrecsrc), 75290da2b28SAriff Abdullah KOBJMETHOD_END 7530f55ac6cSCameron Grant }; 7540f55ac6cSCameron Grant MIXER_DECLARE(solomixer); 7550f55ac6cSCameron Grant 756fd1aaeccSCameron Grant /************************************************************/ 757fd1aaeccSCameron Grant 758fd1aaeccSCameron Grant static int 759fd1aaeccSCameron Grant ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir) 760fd1aaeccSCameron Grant { 761fd1aaeccSCameron Grant KASSERT(ch == 1 || ch == 2, ("bad ch")); 762fd1aaeccSCameron Grant sc->dmasz[ch - 1] = cnt; 763fd1aaeccSCameron Grant if (ch == 1) { 76419a0702eSNick Sayer port_wr(sc->vc, 0x8, 0xc4, 1); /* command */ 765fd1aaeccSCameron Grant port_wr(sc->vc, 0xd, 0xff, 1); /* reset */ 766fd1aaeccSCameron Grant port_wr(sc->vc, 0xf, 0x01, 1); /* mask */ 76719a0702eSNick Sayer port_wr(sc->vc, 0xb, dir == PCMDIR_PLAY? 0x58 : 0x54, 1); /* mode */ 768fd1aaeccSCameron Grant port_wr(sc->vc, 0x0, base, 4); 769bb7f26c3SNick Sayer port_wr(sc->vc, 0x4, cnt - 1, 2); 770fd1aaeccSCameron Grant 771fd1aaeccSCameron Grant } else if (ch == 2) { 772fd1aaeccSCameron Grant port_wr(sc->io, 0x6, 0x08, 1); /* autoinit */ 773fd1aaeccSCameron Grant port_wr(sc->io, 0x0, base, 4); 774fd1aaeccSCameron Grant port_wr(sc->io, 0x4, cnt, 2); 775fd1aaeccSCameron Grant } 776fd1aaeccSCameron Grant return 0; 777fd1aaeccSCameron Grant } 778fd1aaeccSCameron Grant 779fd1aaeccSCameron Grant static int 780fd1aaeccSCameron Grant ess_dmapos(struct ess_info *sc, int ch) 781fd1aaeccSCameron Grant { 7826ba60b3cSNick Sayer int p = 0, i = 0, j = 0; 783fd1aaeccSCameron Grant 784fd1aaeccSCameron Grant KASSERT(ch == 1 || ch == 2, ("bad ch")); 7850edeb3dcSNick Sayer if (ch == 1) { 7860edeb3dcSNick Sayer /* 7870edeb3dcSNick Sayer * During recording, this register is known to give back 7880edeb3dcSNick Sayer * garbage if it's not quiescent while being read. That's 7896ba60b3cSNick Sayer * why we spl, stop the DMA, and try over and over until 7906ba60b3cSNick Sayer * adjacent reads are "close", in the right order and not 7916ba60b3cSNick Sayer * bigger than is otherwise possible. 7920edeb3dcSNick Sayer */ 7930edeb3dcSNick Sayer ess_dmatrigger(sc, ch, 0); 7940edeb3dcSNick Sayer DELAY(20); 7956ba60b3cSNick Sayer do { 7966ba60b3cSNick Sayer DELAY(10); 7976ba60b3cSNick Sayer if (j > 1) 7986ba60b3cSNick Sayer printf("DMA count reg bogus: %04x & %04x\n", 7996ba60b3cSNick Sayer i, p); 8006ba60b3cSNick Sayer i = port_rd(sc->vc, 0x4, 2) + 1; 801fd1aaeccSCameron Grant p = port_rd(sc->vc, 0x4, 2) + 1; 8026ba60b3cSNick Sayer } while ((p > sc->dmasz[ch - 1] || i < p || (p - i) > 0x8) && j++ < 1000); 8030edeb3dcSNick Sayer ess_dmatrigger(sc, ch, 1); 8040edeb3dcSNick Sayer } 805fd1aaeccSCameron Grant else if (ch == 2) 806fd1aaeccSCameron Grant p = port_rd(sc->io, 0x4, 2); 807fd1aaeccSCameron Grant return sc->dmasz[ch - 1] - p; 808fd1aaeccSCameron Grant } 809fd1aaeccSCameron Grant 810fd1aaeccSCameron Grant static int 811fd1aaeccSCameron Grant ess_dmatrigger(struct ess_info *sc, int ch, int go) 812fd1aaeccSCameron Grant { 813fd1aaeccSCameron Grant KASSERT(ch == 1 || ch == 2, ("bad ch")); 814fd1aaeccSCameron Grant if (ch == 1) 8158eb3acc9SNick Sayer port_wr(sc->vc, 0xf, go? 0x00 : 0x01, 1); /* mask */ 816fd1aaeccSCameron Grant else if (ch == 2) 817fd1aaeccSCameron Grant port_wr(sc->io, 0x6, 0x08 | (go? 0x02 : 0x00), 1); /* autoinit */ 818fd1aaeccSCameron Grant return 0; 819fd1aaeccSCameron Grant } 820fd1aaeccSCameron Grant 821fd1aaeccSCameron Grant static void 822fd1aaeccSCameron Grant ess_release_resources(struct ess_info *sc, device_t dev) 823fd1aaeccSCameron Grant { 824fd1aaeccSCameron Grant if (sc->irq) { 825306f91b6SCameron Grant if (sc->ih) 826306f91b6SCameron Grant bus_teardown_intr(dev, sc->irq, sc->ih); 827fd1aaeccSCameron Grant bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq); 82887d8fcc8SPedro F. Giffuni sc->irq = NULL; 829fd1aaeccSCameron Grant } 830fd1aaeccSCameron Grant if (sc->io) { 831e27951b2SJohn Baldwin bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->io); 83287d8fcc8SPedro F. Giffuni sc->io = NULL; 833fd1aaeccSCameron Grant } 834fd1aaeccSCameron Grant 835fd1aaeccSCameron Grant if (sc->sb) { 836e27951b2SJohn Baldwin bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(1), sc->sb); 83787d8fcc8SPedro F. Giffuni sc->sb = NULL; 838fd1aaeccSCameron Grant } 839fd1aaeccSCameron Grant 840fd1aaeccSCameron Grant if (sc->vc) { 841e27951b2SJohn Baldwin bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(2), sc->vc); 84287d8fcc8SPedro F. Giffuni sc->vc = NULL; 843fd1aaeccSCameron Grant } 844fd1aaeccSCameron Grant 845fd1aaeccSCameron Grant if (sc->mpu) { 846e27951b2SJohn Baldwin bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(3), sc->mpu); 84787d8fcc8SPedro F. Giffuni sc->mpu = NULL; 848fd1aaeccSCameron Grant } 849fd1aaeccSCameron Grant 850fd1aaeccSCameron Grant if (sc->gp) { 851e27951b2SJohn Baldwin bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(4), sc->gp); 85287d8fcc8SPedro F. Giffuni sc->gp = NULL; 853fd1aaeccSCameron Grant } 854fd1aaeccSCameron Grant 855306f91b6SCameron Grant if (sc->parent_dmat) { 856306f91b6SCameron Grant bus_dma_tag_destroy(sc->parent_dmat); 857306f91b6SCameron Grant sc->parent_dmat = 0; 858306f91b6SCameron Grant } 859306f91b6SCameron Grant 8608b6d3fe1SAriff Abdullah if (sc->lock) { 8618b6d3fe1SAriff Abdullah snd_mtxfree(sc->lock); 8628b6d3fe1SAriff Abdullah sc->lock = NULL; 8638b6d3fe1SAriff Abdullah } 8648b6d3fe1SAriff Abdullah 865fd1aaeccSCameron Grant free(sc, M_DEVBUF); 866fd1aaeccSCameron Grant } 867fd1aaeccSCameron Grant 868fd1aaeccSCameron Grant static int 869fd1aaeccSCameron Grant ess_alloc_resources(struct ess_info *sc, device_t dev) 870fd1aaeccSCameron Grant { 871fd1aaeccSCameron Grant int rid; 872fd1aaeccSCameron Grant 873e27951b2SJohn Baldwin rid = PCIR_BAR(0); 8745f96beb9SNate Lawson sc->io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 875fd1aaeccSCameron Grant 876e27951b2SJohn Baldwin rid = PCIR_BAR(1); 8775f96beb9SNate Lawson sc->sb = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 878fd1aaeccSCameron Grant 879e27951b2SJohn Baldwin rid = PCIR_BAR(2); 8805f96beb9SNate Lawson sc->vc = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 881fd1aaeccSCameron Grant 882e27951b2SJohn Baldwin rid = PCIR_BAR(3); 8835f96beb9SNate Lawson sc->mpu = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 884fd1aaeccSCameron Grant 885e27951b2SJohn Baldwin rid = PCIR_BAR(4); 8865f96beb9SNate Lawson sc->gp = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 887fd1aaeccSCameron Grant 888fd1aaeccSCameron Grant rid = 0; 8895f96beb9SNate Lawson sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 8905f96beb9SNate Lawson RF_ACTIVE | RF_SHAREABLE); 891fd1aaeccSCameron Grant 8924582b3a1SAriff Abdullah sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_solo softc"); 8938b6d3fe1SAriff Abdullah 8948b6d3fe1SAriff Abdullah return (sc->irq && sc->io && sc->sb && sc->vc && 8958b6d3fe1SAriff Abdullah sc->mpu && sc->gp && sc->lock)? 0 : ENXIO; 896fd1aaeccSCameron Grant } 897fd1aaeccSCameron Grant 898fd1aaeccSCameron Grant static int 899fd1aaeccSCameron Grant ess_probe(device_t dev) 900fd1aaeccSCameron Grant { 901fd1aaeccSCameron Grant char *s = NULL; 902fd1aaeccSCameron Grant u_int32_t subdev; 903fd1aaeccSCameron Grant 904fd1aaeccSCameron Grant subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev); 905fd1aaeccSCameron Grant switch (pci_get_devid(dev)) { 906fd1aaeccSCameron Grant case 0x1969125d: 907fd1aaeccSCameron Grant if (subdev == 0x8888125d) 908fd1aaeccSCameron Grant s = "ESS Solo-1E"; 909fd1aaeccSCameron Grant else if (subdev == 0x1818125d) 910fd1aaeccSCameron Grant s = "ESS Solo-1"; 911fd1aaeccSCameron Grant else 912fd1aaeccSCameron Grant s = "ESS Solo-1 (unknown vendor)"; 913fd1aaeccSCameron Grant break; 914fd1aaeccSCameron Grant } 915fd1aaeccSCameron Grant 916fd1aaeccSCameron Grant if (s) 917fd1aaeccSCameron Grant device_set_desc(dev, s); 918d2b677bbSWarner Losh return s ? BUS_PROBE_DEFAULT : ENXIO; 919fd1aaeccSCameron Grant } 920fd1aaeccSCameron Grant 921933ce6faSOrion Hodson #define ESS_PCI_LEGACYCONTROL 0x40 922933ce6faSOrion Hodson #define ESS_PCI_CONFIG 0x50 923933ce6faSOrion Hodson #define ESS_PCI_DDMACONTROL 0x60 924933ce6faSOrion Hodson 925933ce6faSOrion Hodson static int 926933ce6faSOrion Hodson ess_suspend(device_t dev) 927933ce6faSOrion Hodson { 928933ce6faSOrion Hodson return 0; 929933ce6faSOrion Hodson } 930933ce6faSOrion Hodson 931933ce6faSOrion Hodson static int 932933ce6faSOrion Hodson ess_resume(device_t dev) 933933ce6faSOrion Hodson { 934933ce6faSOrion Hodson uint16_t ddma; 935933ce6faSOrion Hodson struct ess_info *sc = pcm_getdevinfo(dev); 936933ce6faSOrion Hodson 9378b6d3fe1SAriff Abdullah ess_lock(sc); 938933ce6faSOrion Hodson ddma = rman_get_start(sc->vc) | 1; 939933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2); 940933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2); 941933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_CONFIG, 0, 2); 942933ce6faSOrion Hodson 9438b6d3fe1SAriff Abdullah if (ess_reset_dsp(sc)) { 9448b6d3fe1SAriff Abdullah ess_unlock(sc); 945933ce6faSOrion Hodson goto no; 9468b6d3fe1SAriff Abdullah } 9478b6d3fe1SAriff Abdullah ess_unlock(sc); 948933ce6faSOrion Hodson if (mixer_reinit(dev)) 949933ce6faSOrion Hodson goto no; 9508b6d3fe1SAriff Abdullah ess_lock(sc); 951933ce6faSOrion Hodson if (sc->newspeed) 952933ce6faSOrion Hodson ess_setmixer(sc, 0x71, 0x2a); 953933ce6faSOrion Hodson 954933ce6faSOrion Hodson port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */ 9558b6d3fe1SAriff Abdullah ess_unlock(sc); 956933ce6faSOrion Hodson 957933ce6faSOrion Hodson return 0; 958933ce6faSOrion Hodson no: 959933ce6faSOrion Hodson return EIO; 960933ce6faSOrion Hodson } 961fd1aaeccSCameron Grant 962fd1aaeccSCameron Grant static int 963fd1aaeccSCameron Grant ess_attach(device_t dev) 964fd1aaeccSCameron Grant { 965fd1aaeccSCameron Grant struct ess_info *sc; 966fd1aaeccSCameron Grant char status[SND_STATUSLEN]; 967fd1aaeccSCameron Grant u_int16_t ddma; 968fd1aaeccSCameron Grant 969082f6383SAriff Abdullah sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO); 970c68534f1SScott Long pci_enable_busmaster(dev); 971fd1aaeccSCameron Grant 972fd1aaeccSCameron Grant if (ess_alloc_resources(sc, dev)) 973fd1aaeccSCameron Grant goto no; 974fd1aaeccSCameron Grant 97539dbd126SCameron Grant sc->bufsz = pcm_getbuffersize(dev, 4096, SOLO_DEFAULT_BUFSZ, 65536); 97639dbd126SCameron Grant 977fd1aaeccSCameron Grant ddma = rman_get_start(sc->vc) | 1; 978933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2); 979933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2); 980933ce6faSOrion Hodson pci_write_config(dev, ESS_PCI_CONFIG, 0, 2); 981fd1aaeccSCameron Grant 982fd1aaeccSCameron Grant port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */ 9833ac1ca33SNick Sayer #ifdef ESS18XX_DUPLEX 984fd1aaeccSCameron Grant sc->duplex = 1; 9853ac1ca33SNick Sayer #else 9863ac1ca33SNick Sayer sc->duplex = 0; 9873ac1ca33SNick Sayer #endif 988fd1aaeccSCameron Grant 9893ac1ca33SNick Sayer #ifdef ESS18XX_NEWSPEED 9903ac1ca33SNick Sayer sc->newspeed = 1; 9913ac1ca33SNick Sayer #else 9923ac1ca33SNick Sayer sc->newspeed = 0; 9933ac1ca33SNick Sayer #endif 994*1f7a6325SAlexander Motin if (snd_setup_intr(dev, sc->irq, INTR_MPSAFE, ess_intr, sc, &sc->ih)) { 9958b6d3fe1SAriff Abdullah device_printf(dev, "unable to map interrupt\n"); 9968b6d3fe1SAriff Abdullah goto no; 9978b6d3fe1SAriff Abdullah } 998fd1aaeccSCameron Grant 999fd1aaeccSCameron Grant if (!sc->duplex) 1000fd1aaeccSCameron Grant pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX); 1001fd1aaeccSCameron Grant 10028b6d3fe1SAriff Abdullah #if 0 10030b989078SAlexander Leidinger if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/65536, /*boundary*/0, 10048b6d3fe1SAriff Abdullah #endif 10050b989078SAlexander Leidinger if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2, /*boundary*/0, 1006fd1aaeccSCameron Grant /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 1007fd1aaeccSCameron Grant /*highaddr*/BUS_SPACE_MAXADDR, 1008fd1aaeccSCameron Grant /*filter*/NULL, /*filterarg*/NULL, 100939dbd126SCameron Grant /*maxsize*/sc->bufsz, /*nsegments*/1, 1010fd1aaeccSCameron Grant /*maxsegz*/0x3ffff, 10118b6d3fe1SAriff Abdullah /*flags*/0, 10128b6d3fe1SAriff Abdullah /*lockfunc*/NULL, /*lockarg*/NULL, 10138b6d3fe1SAriff Abdullah &sc->parent_dmat) != 0) { 1014fd1aaeccSCameron Grant device_printf(dev, "unable to create dma tag\n"); 1015fd1aaeccSCameron Grant goto no; 1016fd1aaeccSCameron Grant } 1017fd1aaeccSCameron Grant 10188b6d3fe1SAriff Abdullah if (ess_reset_dsp(sc)) 10198b6d3fe1SAriff Abdullah goto no; 10208b6d3fe1SAriff Abdullah 10218b6d3fe1SAriff Abdullah if (sc->newspeed) 10228b6d3fe1SAriff Abdullah ess_setmixer(sc, 0x71, 0x2a); 10238b6d3fe1SAriff Abdullah 10248b6d3fe1SAriff Abdullah if (mixer_init(dev, &solomixer_class, sc)) 10258b6d3fe1SAriff Abdullah goto no; 10268b6d3fe1SAriff Abdullah 1027da1b038aSJustin Hibbits snprintf(status, SND_STATUSLEN, "at io 0x%jx,0x%jx,0x%jx irq %jd %s", 1028fd1aaeccSCameron Grant rman_get_start(sc->io), rman_get_start(sc->sb), rman_get_start(sc->vc), 10290d8ed52eSMathew Kanner rman_get_start(sc->irq),PCM_KLDSTRING(snd_solo)); 1030fd1aaeccSCameron Grant 1031fd1aaeccSCameron Grant if (pcm_register(dev, sc, 1, 1)) 1032fd1aaeccSCameron Grant goto no; 10330f55ac6cSCameron Grant pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc); 10340f55ac6cSCameron Grant pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc); 1035fd1aaeccSCameron Grant pcm_setstatus(dev, status); 1036fd1aaeccSCameron Grant 1037fd1aaeccSCameron Grant return 0; 1038fd1aaeccSCameron Grant 1039fd1aaeccSCameron Grant no: 1040fd1aaeccSCameron Grant ess_release_resources(sc, dev); 1041fd1aaeccSCameron Grant return ENXIO; 1042fd1aaeccSCameron Grant } 1043fd1aaeccSCameron Grant 1044306f91b6SCameron Grant static int 1045306f91b6SCameron Grant ess_detach(device_t dev) 1046306f91b6SCameron Grant { 1047306f91b6SCameron Grant int r; 10487dfc9325SCameron Grant struct ess_info *sc; 1049306f91b6SCameron Grant 1050306f91b6SCameron Grant r = pcm_unregister(dev); 1051306f91b6SCameron Grant if (r) 1052306f91b6SCameron Grant return r; 1053306f91b6SCameron Grant 1054306f91b6SCameron Grant sc = pcm_getdevinfo(dev); 1055306f91b6SCameron Grant ess_release_resources(sc, dev); 1056306f91b6SCameron Grant return 0; 1057306f91b6SCameron Grant } 1058306f91b6SCameron Grant 1059fd1aaeccSCameron Grant static device_method_t ess_methods[] = { 1060fd1aaeccSCameron Grant /* Device interface */ 1061fd1aaeccSCameron Grant DEVMETHOD(device_probe, ess_probe), 1062fd1aaeccSCameron Grant DEVMETHOD(device_attach, ess_attach), 1063306f91b6SCameron Grant DEVMETHOD(device_detach, ess_detach), 1064933ce6faSOrion Hodson DEVMETHOD(device_resume, ess_resume), 1065933ce6faSOrion Hodson DEVMETHOD(device_suspend, ess_suspend), 1066fd1aaeccSCameron Grant { 0, 0 } 1067fd1aaeccSCameron Grant }; 1068fd1aaeccSCameron Grant 1069fd1aaeccSCameron Grant static driver_t ess_driver = { 1070fd1aaeccSCameron Grant "pcm", 1071fd1aaeccSCameron Grant ess_methods, 107267b1dce3SCameron Grant PCM_SOFTC_SIZE, 1073fd1aaeccSCameron Grant }; 1074fd1aaeccSCameron Grant 1075fd1aaeccSCameron Grant DRIVER_MODULE(snd_solo, pci, ess_driver, pcm_devclass, 0, 0); 10760739ea1dSSeigo Tanimura MODULE_DEPEND(snd_solo, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 1077fd1aaeccSCameron Grant MODULE_VERSION(snd_solo, 1); 1078