2 **********************************************************************
3 * hwaccess.c -- Hardware access layer
4 * Copyright 1999, 2000 Creative Labs, Inc.
6 **********************************************************************
8 * Date Author Summary of changes
9 * ---- ------ ------------------
10 * October 20, 1999 Bertrand Lee base code release
11 * December 9, 1999 Jon Taylor rewrote the I/O subsystem
13 **********************************************************************
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public
26 * License along with this program; if not, write to the Free
27 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
30 **********************************************************************
39 /*************************************************************************
40 * Function : srToPitch *
41 * Input : sampleRate - sampling rate *
42 * Return : pitch value *
43 * About : convert sampling rate to pitch *
44 * Note : for 8010, sampling rate is at 48kHz, this function should *
46 *************************************************************************/
47 u32 srToPitch(u32 sampleRate)
51 /* FIXME: These tables should be defined in a headerfile */
52 static u32 logMagTable[128] = {
53 0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3, 0x13aa2,
54 0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a, 0x2655d, 0x28ed5,
55 0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb, 0x381b6, 0x3a93d, 0x3d081,
56 0x3f782, 0x41e42, 0x444c1, 0x46b01, 0x49101, 0x4b6c4, 0x4dc49, 0x50191,
57 0x5269e, 0x54b6f, 0x57006, 0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7,
58 0x646ee, 0x66a00, 0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829,
59 0x759d4, 0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e,
60 0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20, 0x93d26,
61 0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec, 0xa11d8, 0xa2f9d,
62 0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241, 0xadf26, 0xafbe7, 0xb1885,
63 0xb3500, 0xb5157, 0xb6d8c, 0xb899f, 0xba58f, 0xbc15e, 0xbdd0c, 0xbf899,
64 0xc1404, 0xc2f50, 0xc4a7b, 0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c,
65 0xceaec, 0xd053f, 0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3,
66 0xdba4a, 0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3,
67 0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a, 0xf2c83,
68 0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57, 0xfd1a7, 0xfe8df
71 static char logSlopeTable[128] = {
72 0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58,
73 0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53,
74 0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f,
75 0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b,
76 0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47,
77 0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44,
78 0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41,
79 0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e,
80 0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c,
81 0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39,
82 0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37,
83 0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35,
84 0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34,
85 0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32,
86 0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30,
87 0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f
91 return 0; /* Bail out if no leading "1" */
93 sampleRate *= 11185; /* Scale 48000 to 0x20002380 */
95 for (i = 31; i > 0; i--) {
96 if (sampleRate & 0x80000000) { /* Detect leading "1" */
97 return (u32) (((s32) (i - 15) << 20) +
98 logMagTable[0x7f & (sampleRate >> 24)] +
99 (0x7f & (sampleRate >> 17)) * logSlopeTable[0x7f & (sampleRate >> 24)]);
101 sampleRate = sampleRate << 1;
104 DPF(2, "srToPitch: BUG!\n");
105 return 0; /* Should never reach this point */
108 /*******************************************
109 * write/read PCI function 0 registers *
110 ********************************************/
111 void emu10k1_writefn0(struct emu10k1_card *card, u32 reg, u32 data)
115 if (reg & 0xff000000) {
119 size = (reg >> 24) & 0x3f;
120 offset = (reg >> 16) & 0x1f;
121 mask = ((1 << size) - 1) << offset;
122 data = (data << offset) & mask;
125 spin_lock_irqsave(&card->lock, flags);
126 data |= inl(card->iobase + reg) & ~mask;
127 outl(data, card->iobase + reg);
128 spin_unlock_irqrestore(&card->lock, flags);
130 spin_lock_irqsave(&card->lock, flags);
131 outl(data, card->iobase + reg);
132 spin_unlock_irqrestore(&card->lock, flags);
139 void emu10k1_writefn0_2(struct emu10k1_card *card, u32 reg, u32 data, int size)
143 spin_lock_irqsave(&card->lock, flags);
146 outl(data, card->iobase + (reg & 0x1F));
148 outw(data, card->iobase + (reg & 0x1F));
150 outb(data, card->iobase + (reg & 0x1F));
152 spin_unlock_irqrestore(&card->lock, flags);
158 u32 emu10k1_readfn0(struct emu10k1_card * card, u32 reg)
163 if (reg & 0xff000000) {
167 size = (reg >> 24) & 0x3f;
168 offset = (reg >> 16) & 0x1f;
169 mask = ((1 << size) - 1) << offset;
172 spin_lock_irqsave(&card->lock, flags);
173 val = inl(card->iobase + reg);
174 spin_unlock_irqrestore(&card->lock, flags);
176 return (val & mask) >> offset;
178 spin_lock_irqsave(&card->lock, flags);
179 val = inl(card->iobase + reg);
180 spin_unlock_irqrestore(&card->lock, flags);
185 void emu10k1_timer_set(struct emu10k1_card * card, u16 data)
189 spin_lock_irqsave(&card->lock, flags);
190 outw(data & TIMER_RATE_MASK, card->iobase + TIMER);
191 spin_unlock_irqrestore(&card->lock, flags);
194 /************************************************************************
195 * write/read Emu10k1 pointer-offset register set, accessed through *
196 * the PTR and DATA registers *
197 *************************************************************************/
198 #define A_PTR_ADDRESS_MASK 0x0fff0000
199 void sblive_writeptr(struct emu10k1_card *card, u32 reg, u32 channel, u32 data)
204 regptr = ((reg << 16) & A_PTR_ADDRESS_MASK) | (channel & PTR_CHANNELNUM_MASK);
206 if (reg & 0xff000000) {
210 size = (reg >> 24) & 0x3f;
211 offset = (reg >> 16) & 0x1f;
212 mask = ((1 << size) - 1) << offset;
213 data = (data << offset) & mask;
215 spin_lock_irqsave(&card->lock, flags);
216 outl(regptr, card->iobase + PTR);
217 data |= inl(card->iobase + DATA) & ~mask;
218 outl(data, card->iobase + DATA);
219 spin_unlock_irqrestore(&card->lock, flags);
221 spin_lock_irqsave(&card->lock, flags);
222 outl(regptr, card->iobase + PTR);
223 outl(data, card->iobase + DATA);
224 spin_unlock_irqrestore(&card->lock, flags);
228 /* ... : data, reg, ... , TAGLIST_END */
229 void sblive_writeptr_tag(struct emu10k1_card *card, u32 channel, ...)
236 va_start(args, channel);
238 spin_lock_irqsave(&card->lock, flags);
239 while ((reg = va_arg(args, u32)) != TAGLIST_END) {
240 u32 data = va_arg(args, u32);
241 u32 regptr = (((reg << 16) & A_PTR_ADDRESS_MASK)
242 | (channel & PTR_CHANNELNUM_MASK));
243 outl(regptr, card->iobase + PTR);
244 if (reg & 0xff000000) {
245 int size = (reg >> 24) & 0x3f;
246 int offset = (reg >> 16) & 0x1f;
247 u32 mask = ((1 << size) - 1) << offset;
248 data = (data << offset) & mask;
250 data |= inl(card->iobase + DATA) & ~mask;
252 outl(data, card->iobase + DATA);
254 spin_unlock_irqrestore(&card->lock, flags);
261 u32 sblive_readptr(struct emu10k1_card * card, u32 reg, u32 channel)
266 regptr = ((reg << 16) & A_PTR_ADDRESS_MASK) | (channel & PTR_CHANNELNUM_MASK);
268 if (reg & 0xff000000) {
272 size = (reg >> 24) & 0x3f;
273 offset = (reg >> 16) & 0x1f;
274 mask = ((1 << size) - 1) << offset;
276 spin_lock_irqsave(&card->lock, flags);
277 outl(regptr, card->iobase + PTR);
278 val = inl(card->iobase + DATA);
279 spin_unlock_irqrestore(&card->lock, flags);
281 return (val & mask) >> offset;
283 spin_lock_irqsave(&card->lock, flags);
284 outl(regptr, card->iobase + PTR);
285 val = inl(card->iobase + DATA);
286 spin_unlock_irqrestore(&card->lock, flags);
292 void emu10k1_irq_enable(struct emu10k1_card *card, u32 irq_mask)
297 DPF(2,"emu10k1_irq_enable()\n");
299 spin_lock_irqsave(&card->lock, flags);
300 val = inl(card->iobase + INTE) | irq_mask;
301 outl(val, card->iobase + INTE);
302 spin_unlock_irqrestore(&card->lock, flags);
306 void emu10k1_irq_disable(struct emu10k1_card *card, u32 irq_mask)
311 DPF(2,"emu10k1_irq_disable()\n");
313 spin_lock_irqsave(&card->lock, flags);
314 val = inl(card->iobase + INTE) & ~irq_mask;
315 outl(val, card->iobase + INTE);
316 spin_unlock_irqrestore(&card->lock, flags);
320 void emu10k1_clear_stop_on_loop(struct emu10k1_card *card, u32 voicenum)
322 /* Voice interrupt */
324 sblive_writeptr(card, SOLEH | ((0x0100 | (voicenum - 32)) << 16), 0, 0);
326 sblive_writeptr(card, SOLEL | ((0x0100 | voicenum) << 16), 0, 0);
331 static void sblive_wcwait(struct emu10k1_card *card, u32 wait)
333 volatile unsigned uCount;
334 u32 newtime = 0, curtime;
336 curtime = emu10k1_readfn0(card, WC_SAMPLECOUNTER);
339 while (uCount++ < TIMEOUT) {
340 newtime = emu10k1_readfn0(card, WC_SAMPLECOUNTER);
341 if (newtime != curtime)
345 if (uCount >= TIMEOUT)
352 u16 emu10k1_ac97_read(struct ac97_codec *codec, u8 reg)
354 struct emu10k1_card *card = codec->private_data;
358 spin_lock_irqsave(&card->lock, flags);
360 outb(reg, card->iobase + AC97ADDRESS);
361 data = inw(card->iobase + AC97DATA);
363 spin_unlock_irqrestore(&card->lock, flags);
368 void emu10k1_ac97_write(struct ac97_codec *codec, u8 reg, u16 value)
370 struct emu10k1_card *card = codec->private_data;
373 spin_lock_irqsave(&card->lock, flags);
375 outb(reg, card->iobase + AC97ADDRESS);
376 outw(value, card->iobase + AC97DATA);
377 outb( AC97_EXTENDED_ID, card->iobase + AC97ADDRESS);
378 spin_unlock_irqrestore(&card->lock, flags);
381 /*********************************************************
382 * MPU access functions *
383 **********************************************************/
385 int emu10k1_mpu_write_data(struct emu10k1_card *card, u8 data)
390 if (card->is_audigy) {
391 if ((sblive_readptr(card, A_MUSTAT,0) & MUSTAT_ORDYN) == 0) {
392 sblive_writeptr(card, A_MUDATA, 0, data);
397 spin_lock_irqsave(&card->lock, flags);
399 if ((inb(card->iobase + MUSTAT) & MUSTAT_ORDYN) == 0) {
400 outb(data, card->iobase + MUDATA);
405 spin_unlock_irqrestore(&card->lock, flags);
411 int emu10k1_mpu_read_data(struct emu10k1_card *card, u8 * data)
416 if (card->is_audigy) {
417 if ((sblive_readptr(card, A_MUSTAT,0) & MUSTAT_IRDYN) == 0) {
418 *data = sblive_readptr(card, A_MUDATA,0);
423 spin_lock_irqsave(&card->lock, flags);
425 if ((inb(card->iobase + MUSTAT) & MUSTAT_IRDYN) == 0) {
426 *data = inb(card->iobase + MUDATA);
431 spin_unlock_irqrestore(&card->lock, flags);
437 int emu10k1_mpu_reset(struct emu10k1_card *card)
442 DPF(2, "emu10k1_mpu_reset()\n");
443 if (card->is_audigy) {
444 if (card->mpuacqcount == 0) {
445 sblive_writeptr(card, A_MUCMD, 0, MUCMD_RESET);
446 sblive_wcwait(card, 8);
447 sblive_writeptr(card, A_MUCMD, 0, MUCMD_RESET);
448 sblive_wcwait(card, 8);
449 sblive_writeptr(card, A_MUCMD, 0, MUCMD_ENTERUARTMODE);
450 sblive_wcwait(card, 8);
451 status = sblive_readptr(card, A_MUDATA, 0);
460 if (card->mpuacqcount == 0) {
461 spin_lock_irqsave(&card->lock, flags);
462 outb(MUCMD_RESET, card->iobase + MUCMD);
463 spin_unlock_irqrestore(&card->lock, flags);
465 sblive_wcwait(card, 8);
467 spin_lock_irqsave(&card->lock, flags);
468 outb(MUCMD_RESET, card->iobase + MUCMD);
469 spin_unlock_irqrestore(&card->lock, flags);
471 sblive_wcwait(card, 8);
473 spin_lock_irqsave(&card->lock, flags);
474 outb(MUCMD_ENTERUARTMODE, card->iobase + MUCMD);
475 spin_unlock_irqrestore(&card->lock, flags);
477 sblive_wcwait(card, 8);
479 spin_lock_irqsave(&card->lock, flags);
480 status = inb(card->iobase + MUDATA);
481 spin_unlock_irqrestore(&card->lock, flags);
493 int emu10k1_mpu_acquire(struct emu10k1_card *card)
495 /* FIXME: This should be a macro */
501 int emu10k1_mpu_release(struct emu10k1_card *card)
503 /* FIXME: this should be a macro */