ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pci / au88x0 / au88x0_synth.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or
5  *  (at your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *  GNU Library General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */
16
17 /*
18  * Someday its supposed to make use of the WT DMA engine
19  * for a Wavetable synthesizer.
20  */
21
22 #include "au88x0.h"
23 #include "au88x0_wt.h"
24
25 static void vortex_fifo_setwtvalid(vortex_t * vortex, int fifo, int en);
26 static void vortex_connection_adb_mixin(vortex_t * vortex, int en,
27                                         unsigned char channel,
28                                         unsigned char source,
29                                         unsigned char mixin);
30 static void vortex_connection_mixin_mix(vortex_t * vortex, int en,
31                                         unsigned char mixin,
32                                         unsigned char mix, int a);
33 static void vortex_fifo_wtinitialize(vortex_t * vortex, int fifo, int j);
34 static int vortex_wt_SetReg(vortex_t * vortex, unsigned char reg, int wt,
35                             unsigned long val);
36
37 /* WT */
38
39 static void vortex_wt_setstereo(vortex_t * vortex, u32 wt, u32 stereo)
40 {
41         int temp;
42
43         //temp = hwread(vortex->mmio, 0x80 + ((wt >> 0x5)<< 0xf) + (((wt & 0x1f) >> 1) << 2));
44         temp = hwread(vortex->mmio, WT_STEREO(wt));
45         temp = (temp & 0xfe) | (stereo & 1);
46         //hwwrite(vortex->mmio, 0x80 + ((wt >> 0x5)<< 0xf) + (((wt & 0x1f) >> 1) << 2), temp);
47         hwwrite(vortex->mmio, WT_STEREO(wt), temp);
48 }
49
50 static void vortex_wt_setdsout(vortex_t * vortex, u32 wt, int en)
51 {
52         int temp;
53
54         /* There is one DSREG register for each bank (32 voices each). */
55         temp = hwread(vortex->mmio, WT_DSREG((wt >= 0x20) ? 1 : 0));
56         if (en)
57                 temp |= (1 << (wt & 0x1f));
58         else
59                 temp &= (1 << ~(wt & 0x1f));
60         hwwrite(vortex->mmio, WT_DSREG((wt >= 0x20) ? 1 : 0), temp);
61 }
62
63 // WT routing is still a mistery.
64 static int vortex_wt_allocroute(vortex_t * vortex, int wt, int nr_ch)
65 {
66         wt_voice_t *voice = &(vortex->wt_voice[wt]);
67         int temp;
68
69         //FIXME: WT audio routing.
70         if (nr_ch) {
71                 vortex_fifo_wtinitialize(vortex, wt, 2);
72                 vortex_fifo_setwtvalid(vortex, wt, 1);
73                 vortex_wt_setstereo(vortex, wt, nr_ch - 1);
74         } else
75                 vortex_fifo_setwtvalid(vortex, wt, 0);
76
77         vortex_wt_setdsout(vortex, wt, 0);
78         hwwrite(vortex->mmio, WT_SRAMP(0), 0x880000);
79         //hwwrite(vortex->mmio, WT_GMODE(0), 0xffffffff);
80 #ifdef CHIP_AU8830
81         hwwrite(vortex->mmio, WT_SRAMP(1), 0x880000);
82         //hwwrite(vortex->mmio, WT_GMODE(1), 0xffffffff);
83 #endif
84         hwwrite(vortex->mmio, WT_PARM(wt, 0), 0);
85         hwwrite(vortex->mmio, WT_PARM(wt, 1), 0);
86         hwwrite(vortex->mmio, WT_PARM(wt, 2), 0);
87
88         temp = hwread(vortex->mmio, WT_PARM(wt, 3));
89         printk("vortex: WT PARM3: %x\n", temp);
90         hwwrite(vortex->mmio, WT_PARM(wt, 3), temp);
91
92         hwwrite(vortex->mmio, WT_DELAY(wt, 0), 0);
93         hwwrite(vortex->mmio, WT_DELAY(wt, 1), 0);
94         hwwrite(vortex->mmio, WT_DELAY(wt, 2), 0);
95         hwwrite(vortex->mmio, WT_DELAY(wt, 3), 0);
96
97         printk("vortex: WT GMODE: %x\n", hwread(vortex->mmio, WT_GMODE(wt)));
98
99         hwwrite(vortex->mmio, WT_PARM(wt, 2), 0xffffffff);
100         hwwrite(vortex->mmio, WT_PARM(wt, 3), 0xcff1c810);
101
102         voice->parm0 = voice->parm1 = 0xcfb23e2f;
103         hwwrite(vortex->mmio, WT_PARM(wt, 0), voice->parm0);
104         hwwrite(vortex->mmio, WT_PARM(wt, 1), voice->parm1);
105         printk("vortex: WT GMODE 2 : %x\n", hwread(vortex->mmio, WT_GMODE(wt)));
106         return 0;
107 }
108
109 static void vortex_wt_connect(vortex_t * vortex, int en)
110 {
111         int i, ii, mix;
112
113 #define NR_WTROUTES 6
114 #ifdef CHIP_AU8830
115 #define NR_WTBLOCKS 2
116 #else
117 #define NR_WTBLOCKS 1
118 #endif
119
120         for (i = 0; i < NR_WTBLOCKS; i++) {
121                 for (ii = 0; ii < NR_WTROUTES; ii++) {
122                         mix =
123                             vortex_adb_checkinout(vortex,
124                                                   vortex->fixed_res, en,
125                                                   VORTEX_RESOURCE_MIXIN);
126                         vortex->mixwt[(i * NR_WTROUTES) + ii] = mix;
127
128                         vortex_route(vortex, en, 0x11,
129                                      ADB_WTOUT(i, ii + 0x20), ADB_MIXIN(mix));
130
131                         vortex_connection_mixin_mix(vortex, en, mix,
132                                                     vortex->mixplayb[ii %
133                                                                      2], 0);
134                         if (VORTEX_IS_QUAD(vortex))
135                                 vortex_connection_mixin_mix(vortex, en,
136                                                             mix,
137                                                             vortex->
138                                                             mixplayb[2 +
139                                                                      (ii %
140                                                                       2)], 0);
141                 }
142         }
143         for (i = 0; i < NR_WT; i++) {
144                 hwwrite(vortex->mmio, WT_RUN(i), 1);
145         }
146 }
147
148 /* Read WT Register */
149 #if 0
150 static int vortex_wt_GetReg(vortex_t * vortex, char reg, int wt)
151 {
152         //int eax, esi;
153
154         if (reg == 4) {
155                 return hwread(vortex->mmio, WT_PARM(wt, 3));
156         }
157         if (reg == 7) {
158                 return hwread(vortex->mmio, WT_GMODE(wt));
159         }
160
161         return 0;
162 }
163
164 /* WT hardware abstraction layer generic register interface. */
165 static int
166 vortex_wt_SetReg2(vortex_t * vortex, unsigned char reg, int wt,
167                   unsigned short val)
168 {
169         /*
170            int eax, edx;
171
172            if (wt >= NR_WT)  // 0x40 -> NR_WT
173            return 0;
174
175            if ((reg - 0x20) > 0) {
176            if ((reg - 0x21) != 0) 
177            return 0;
178            eax = ((((b & 0xff) << 0xb) + (edx & 0xff)) << 4) + 0x208; // param 2
179            } else {
180            eax = ((((b & 0xff) << 0xb) + (edx & 0xff)) << 4) + 0x20a; // param 3
181            }
182            hwwrite(vortex->mmio, eax, c);
183          */
184         return 1;
185 }
186
187 /*public: static void __thiscall CWTHal::SetReg(unsigned char,int,unsigned long) */
188 #endif
189 static int
190 vortex_wt_SetReg(vortex_t * vortex, unsigned char reg, int wt,
191                  unsigned long val)
192 {
193         int ecx;
194
195         if ((reg == 5) || ((reg >= 7) && (reg <= 10)) || (reg == 0xc)) {
196                 if (wt >= (NR_WT / NR_WT_PB)) {
197                         printk
198                             ("vortex: WT SetReg: bank out of range. reg=0x%x, wt=%d\n",
199                              reg, wt);
200                         return 0;
201                 }
202         } else {
203                 if (wt >= NR_WT) {
204                         printk("vortex: WT SetReg: voice out of range\n");
205                         return 0;
206                 }
207         }
208         if (reg > 0xc)
209                 return 0;
210
211         switch (reg) {
212                 /* Voice specific parameters */
213         case 0:         /* running */
214                 //printk("vortex: WT SetReg(0x%x) = 0x%08x\n", WT_RUN(wt), (int)val);
215                 hwwrite(vortex->mmio, WT_RUN(wt), val);
216                 return 0xc;
217                 break;
218         case 1:         /* param 0 */
219                 //printk("vortex: WT SetReg(0x%x) = 0x%08x\n", WT_PARM(wt,0), (int)val);
220                 hwwrite(vortex->mmio, WT_PARM(wt, 0), val);
221                 return 0xc;
222                 break;
223         case 2:         /* param 1 */
224                 //printk("vortex: WT SetReg(0x%x) = 0x%08x\n", WT_PARM(wt,1), (int)val);
225                 hwwrite(vortex->mmio, WT_PARM(wt, 1), val);
226                 return 0xc;
227                 break;
228         case 3:         /* param 2 */
229                 //printk("vortex: WT SetReg(0x%x) = 0x%08x\n", WT_PARM(wt,2), (int)val);
230                 hwwrite(vortex->mmio, WT_PARM(wt, 2), val);
231                 return 0xc;
232                 break;
233         case 4:         /* param 3 */
234                 //printk("vortex: WT SetReg(0x%x) = 0x%08x\n", WT_PARM(wt,3), (int)val);
235                 hwwrite(vortex->mmio, WT_PARM(wt, 3), val);
236                 return 0xc;
237                 break;
238         case 6:         /* mute */
239                 //printk("vortex: WT SetReg(0x%x) = 0x%08x\n", WT_MUTE(wt), (int)val);
240                 hwwrite(vortex->mmio, WT_MUTE(wt), val);
241                 return 0xc;
242                 break;
243         case 0xb:
244                 {               /* delay */
245                         //printk("vortex: WT SetReg(0x%x) = 0x%08x\n", WT_DELAY(wt,0), (int)val);
246                         hwwrite(vortex->mmio, WT_DELAY(wt, 3), val);
247                         hwwrite(vortex->mmio, WT_DELAY(wt, 2), val);
248                         hwwrite(vortex->mmio, WT_DELAY(wt, 1), val);
249                         hwwrite(vortex->mmio, WT_DELAY(wt, 0), val);
250                         return 0xc;
251                 }
252                 break;
253                 /* Global WT block parameters */
254         case 5:         /* sramp */
255                 ecx = WT_SRAMP(wt);
256                 break;
257         case 8:         /* aramp */
258                 ecx = WT_ARAMP(wt);
259                 break;
260         case 9:         /* mramp */
261                 ecx = WT_MRAMP(wt);
262                 break;
263         case 0xa:               /* ctrl */
264                 ecx = WT_CTRL(wt);
265                 break;
266         case 0xc:               /* ds_reg */
267                 ecx = WT_DSREG(wt);
268                 break;
269         default:
270                 return 0;
271                 break;
272         }
273         //printk("vortex: WT SetReg(0x%x) = 0x%08x\n", ecx, (int)val);
274         hwwrite(vortex->mmio, ecx, val);
275         return 1;
276 }
277
278 static void vortex_wt_init(vortex_t * vortex)
279 {
280         int var4, var8, varc, var10 = 0, edi;
281
282         var10 &= 0xFFFFFFE3;
283         var10 |= 0x22;
284         var10 &= 0xFFFFFEBF;
285         var10 |= 0x80;
286         var10 |= 0x200;
287         var10 &= 0xfffffffe;
288         var10 &= 0xfffffbff;
289         var10 |= 0x1800;
290         // var10 = 0x1AA2
291         var4 = 0x10000000;
292         varc = 0x00830000;
293         var8 = 0x00830000;
294
295         /* Init Bank registers. */
296         for (edi = 0; edi < (NR_WT / NR_WT_PB); edi++) {
297                 vortex_wt_SetReg(vortex, 0xc, edi, 0);  /* ds_reg */
298                 vortex_wt_SetReg(vortex, 0xa, edi, var10);      /* ctrl  */
299                 vortex_wt_SetReg(vortex, 0x9, edi, var4);       /* mramp */
300                 vortex_wt_SetReg(vortex, 0x8, edi, varc);       /* aramp */
301                 vortex_wt_SetReg(vortex, 0x5, edi, var8);       /* sramp */
302         }
303         /* Init Voice registers. */
304         for (edi = 0; edi < NR_WT; edi++) {
305                 vortex_wt_SetReg(vortex, 0x4, edi, 0);  /* param 3 0x20c */
306                 vortex_wt_SetReg(vortex, 0x3, edi, 0);  /* param 2 0x208 */
307                 vortex_wt_SetReg(vortex, 0x2, edi, 0);  /* param 1 0x204 */
308                 vortex_wt_SetReg(vortex, 0x1, edi, 0);  /* param 0 0x200 */
309                 vortex_wt_SetReg(vortex, 0xb, edi, 0);  /* delay 0x400 - 0x40c */
310         }
311         var10 |= 1;
312         for (edi = 0; edi < (NR_WT / NR_WT_PB); edi++)
313                 vortex_wt_SetReg(vortex, 0xa, edi, var10);      /* ctrl */
314 }
315
316 /* Extract of CAdbTopology::SetVolume(struct _ASPVOLUME *) */
317 #if 0
318 static void vortex_wt_SetVolume(vortex_t * vortex, int wt, int vol[])
319 {
320         wt_voice_t *voice = &(vortex->wt_voice[wt]);
321         int ecx = vol[1], eax = vol[0];
322
323         /* This is pure guess */
324         voice->parm0 &= 0xff00ffff;
325         voice->parm0 |= (vol[0] & 0xff) << 0x10;
326         voice->parm1 &= 0xff00ffff;
327         voice->parm1 |= (vol[1] & 0xff) << 0x10;
328
329         /* This is real */
330         hwwrite(vortex, WT_PARM(wt, 0), voice->parm0);
331         hwwrite(vortex, WT_PARM(wt, 1), voice->parm0);
332
333         if (voice->this_1D0 & 4) {
334                 eax >>= 8;
335                 ecx = eax;
336                 if (ecx < 0x80)
337                         ecx = 0x7f;
338                 voice->parm3 &= 0xFFFFC07F;
339                 voice->parm3 |= (ecx & 0x7f) << 7;
340                 voice->parm3 &= 0xFFFFFF80;
341                 voice->parm3 |= (eax & 0x7f);
342         } else {
343                 voice->parm3 &= 0xFFE03FFF;
344                 voice->parm3 |= (eax & 0xFE00) << 5;
345         }
346
347         hwwrite(vortex, WT_PARM(wt, 3), voice->parm3);
348 }
349
350 /* Extract of CAdbTopology::SetFrequency(unsigned long arg_0) */
351 static void vortex_wt_SetFrequency(vortex_t * vortex, int wt, unsigned int sr)
352 {
353         wt_voice_t *voice = &(vortex->wt_voice[wt]);
354         long int eax, edx;
355
356         //FIXME: 64 bit operation.
357         eax = ((sr << 0xf) * 0x57619F1) & 0xffffffff;
358         edx = (((sr << 0xf) * 0x57619F1)) >> 0x20;
359
360         edx >>= 0xa;
361         edx <<= 1;
362         if (edx) {
363                 if (edx & 0x0FFF80000)
364                         eax = 0x7fff;
365                 else {
366                         edx <<= 0xd;
367                         eax = 7;
368                         while ((edx & 0x80000000) == 0) {
369                                 edx <<= 1;
370                                 eax--;
371                                 if (eax == 0) ;
372                                 break;
373                         }
374                         if (eax)
375                                 edx <<= 1;
376                         eax <<= 0xc;
377                         edx >>= 0x14;
378                         eax |= edx;
379                 }
380         } else
381                 eax = 0;
382         voice->parm0 &= 0xffff0001;
383         voice->parm0 |= (eax & 0x7fff) << 1;
384         voice->parm1 = voice->parm0 | 1;
385         // Wt: this_1D4
386         //AuWt::WriteReg((ulong)(this_1DC<<4)+0x200, (ulong)this_1E4);
387         //AuWt::WriteReg((ulong)(this_1DC<<4)+0x204, (ulong)this_1E8);
388         hwwrite(vortex->mmio, WT_PARM(wt, 0), voice->parm0);
389         hwwrite(vortex->mmio, WT_PARM(wt, 1), voice->parm1);
390 }
391 #endif
392
393 /* End of File */