fix the readwrite/readonly clashes for the pci_driver structs in the sis
[coreboot.git] / src / southbridge / sis / sis966 / sis966_aza.c
1 /*
2  * This file is part of the LinuxBIOS project.
3  *
4  * Copyright (C) 2004 Tyan Computer
5  * Written by Yinghai Lu <yhlu@tyan.com> for Tyan Computer.
6  * Copyright (C) 2006,2007 AMD
7  * Written by Yinghai Lu <yinghai.lu@amd.com> for AMD.
8  * Copyright (C) 2007 Silicon Integrated Systems Corp. (SiS)
9  * Written by Morgan Tsai <my_tsai@sis.com> for SiS.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
24  */
25
26 #include <console/console.h>
27 #include <device/device.h>
28 #include <device/pci.h>
29 #include <device/pci_ids.h>
30 #include <device/pci_ops.h>
31 #include <arch/io.h>
32 #include "sis966.h"
33
34 uint8_t SiS_SiS7502_init[7][3]={
35 {0x04, 0xFF, 0x07},                                      
36 {0x2C, 0xFF, 0x39},                                      
37 {0x2D, 0xFF, 0x10},                                      
38 {0x2E, 0xFF, 0x91},                                      
39 {0x2F, 0xFF, 0x01},
40 {0x04, 0xFF, 0x06},
41 {0x00, 0x00, 0x00}                                      //End of table
42 };
43
44 static int set_bits(uint8_t *port, uint32_t mask, uint32_t val)
45 {
46         uint32_t dword;
47         int count;
48
49         val &= mask;
50         dword = readl(port);
51         dword &= ~mask;
52         dword |= val;
53         writel(dword, port);
54
55         count = 50;
56         do {
57                 dword = readl(port);
58                 dword &= mask;
59                 udelay(100);
60         } while ((dword != val) && --count);
61
62         if(!count) return -1;
63
64         udelay(540);
65         return 0;
66
67 }
68
69  uint32_t send_verb(uint8_t *base, uint32_t verb)
70 {
71
72
73      uint32_t dword;
74          
75      dword = readl(base + 0x68);
76      dword=dword|(unsigned long)0x0002;
77      writel(dword,base + 0x68);
78      do {               
79                 dword = readl(base + 0x68);        
80      }  while ((dword & 1)!=0);
81      writel(verb, base + 0x60);
82      udelay(500);        
83      dword = readl(base + 0x68);          
84      dword =(dword |0x1);   
85      writel(dword, base + 0x68);
86      do {
87                 udelay(120);
88                 dword = readl(base + 0x68);
89      } while ((dword & 3) != 2);
90
91      dword = readl(base + 0x64);
92      return dword;
93                 
94 }
95
96
97 #if 1
98 static int codec_detect(uint8_t *base)
99 {
100         uint32_t dword;
101         int idx=0;
102
103         /* 1 */ // controller reset
104         printk_debug("controller reset\n");
105       
106         set_bits(base + 0x08, 1, 1);
107       
108       do{
109                 dword = readl(base + 0x08)&0x1; 
110                 if(idx++>1000) { printk_debug("controller reset fail !!! \n"); break;}
111            } while (dword !=1);
112        
113        dword=send_verb(base,0x000F0000); // get codec VendorId and DeviceId
114        
115        if(dword==0) {
116                 printk_debug("No codec!\n");
117                 return 0;
118        }
119
120          printk_debug("Codec ID = %lx\n", dword);
121           
122 #if 0
123         /* 2 */
124         dword = readl(base + 0x0e);
125         dword |= 7;
126         writel(dword, base + 0x0e);
127
128         /* 3 */
129         set_bits(base + 0x08, 1, 0);
130
131         /* 4 */
132         set_bits(base + 0x08, 1, 1);
133                 
134         /* 5 */
135         dword = readl(base + 0xe);
136         dword &= 7;
137        
138         /* 6 */
139         if(!dword) {
140                 set_bits(base + 0x08, 1, 0);
141                 printk_debug("No codec!\n");
142                 return 0;
143         }
144 #endif 
145        dword=0x1;
146         return dword;
147
148 }
149
150 #else
151
152 static int codec_detect(uint8_t *base)
153 {
154         uint32_t dword;
155
156         /* 1 */
157         set_bits(base + 0x08, 1, 1);
158
159         /* 2 */
160         dword = readl(base + 0x0e);
161         dword |= 7;
162         writel(dword, base + 0x0e);
163
164         /* 3 */
165         set_bits(base + 0x08, 1, 0);
166
167         /* 4 */
168         set_bits(base + 0x08, 1, 1);
169
170         /* 5 */
171         dword = readl(base + 0xe);
172         dword &= 7;
173
174         /* 6 */
175         if(!dword) {
176                 set_bits(base + 0x08, 1, 0);
177                 printk_debug("No codec!\n");
178                 return 0;
179         }
180         return dword;
181
182 }
183
184 #endif
185
186 #if 1
187
188 // For SiS demo board PinConfig
189 static uint32_t verb_data[] = {
190 #if 0
191  00172083h,
192  00172108h,
193  001722ECh,
194  00172310h,
195 #endif
196 //14
197         0x01471c10,
198         0x01471d40,
199         0x01471e01,
200         0x01471f01,
201 //15
202         0x01571c12,
203         0x01571d10,
204         0x01571e01,
205         0x01571f01,
206 //16
207         0x01671c11,
208         0x01671d60,
209         0x01671e01,
210         0x01671f01,
211 //17
212         0x01771c14,
213         0x01771d20,
214         0x01771e01,
215         0x01771f01,
216 //18
217         0x01871c40,
218         0x01871d98,
219         0x01871ea1,
220         0x01871f01,
221 //19
222         0x01971c50,
223         0x01971d98,
224         0x01971ea1,
225         0x01971f02,
226 //1a
227         0x01a71c4f,
228         0x01a71d30,
229         0x01a71e81,
230         0x01a71f01,
231 //1b
232         0x01b71c20,
233         0x01b71d40,
234         0x01b71e01,
235         0x01b71f02,
236 //1c
237         0x01c71cf0,
238         0x01c71d01,
239         0x01c71e33,
240         0x01c71f59,
241 //1d
242         0x01d71c01,
243         0x01d71de6,
244         0x01d71e05,
245         0x01d71f40,
246 //1e
247         0x01e71c30,
248         0x01e71d11,
249         0x01e71e44,
250         0x01e71f01,
251 //1f
252         0x01f71c60,
253         0x01f71d61,
254         0x01f71ec4,
255         0x01f71f01,
256 };
257
258 #else
259 // orginal codec pin configuration setting
260
261 static uint32_t verb_data[] = {
262 #if 0
263         0x00172001,
264         0x001721e6,
265         0x00172200,
266         0x00172300,
267 #endif
268
269         0x01471c10,
270         0x01471d44,
271         0x01471e01,
272         0x01471f01,
273 //1
274         0x01571c12,
275         0x01571d14,
276         0x01571e01,
277         0x01571f01,
278 //2
279         0x01671c11,
280         0x01671d60,
281         0x01671e01,
282         0x01671f01,
283 //3
284         0x01771c14,
285         0x01771d20,
286         0x01771e01,
287         0x01771f01,
288 //4
289         0x01871c30,
290         0x01871d9c,
291         0x01871ea1,
292         0x01871f01,
293 //5
294         0x01971c40,
295         0x01971d9c,
296         0x01971ea1,
297         0x01971f02,
298 //6
299         0x01a71c31,
300         0x01a71d34,
301         0x01a71e81,
302         0x01a71f01,
303 //7
304         0x01b71c1f,
305         0x01b71d44,
306         0x01b71e21,
307         0x01b71f02,
308 //8
309         0x01c71cf0,
310         0x01c71d11,
311         0x01c71e11,
312         0x01c71f41,
313 //9
314         0x01d71c3e,
315         0x01d71d01,
316         0x01d71e83,
317         0x01d71f99,
318 //10
319         0x01e71c20,
320         0x01e71d41,
321         0x01e71e45,
322         0x01e71f01,
323 //11
324         0x01f71c50,
325         0x01f71d91,
326         0x01f71ec5,
327         0x01f71f01,
328 };
329
330 #endif 
331 static unsigned find_verb(uint32_t viddid, uint32_t **verb)
332 {
333         if((viddid == 0x10ec0883) || (viddid == 0x10ec0882) || (viddid == 0x10ec0880)) return 0;
334         *verb =  (uint32_t *)verb_data;
335         return sizeof(verb_data)/sizeof(uint32_t);
336 }
337
338
339 static void codec_init(uint8_t *base, int addr)
340 {
341         uint32_t dword;
342         uint32_t *verb;
343         unsigned verb_size;
344         int i;
345
346         /* 1 */
347         do {
348                 dword = readl(base + 0x68);
349         } while (dword & 1);
350
351         dword = (addr<<28) | 0x000f0000;
352         writel(dword, base + 0x60);
353
354         do {
355                 dword = readl(base + 0x68);
356         } while ((dword & 3)!=2);
357
358         dword = readl(base + 0x64);
359
360         /* 2 */
361         printk_debug("codec viddid: %08x\n", dword);
362         verb_size = find_verb(dword, &verb);
363
364         if(!verb_size) {
365                 printk_debug("No verb!\n");
366                 return;
367         }
368
369         printk_debug("verb_size: %d\n", verb_size);
370         /* 3 */
371         for(i=0; i<verb_size; i++) {
372                 send_verb(base,verb[i]);
373 #if 0
374                 do {
375                         dword = readl(base + 0x68);
376                 } while (dword & 1);
377
378                 writel(verb[i], base + 0x60);
379
380                 do {
381                         dword = readl(base + 0x68);
382                 } while ((dword & 3) != 2);
383 #endif 
384         }
385         printk_debug("verb loaded!\n");
386 }
387
388 static void codecs_init(uint8_t *base, uint32_t codec_mask)
389 {
390         int i;
391         codec_init(base, 0);
392         return;
393         for(i=2; i>=0; i--) {
394                 if( codec_mask & (1<<i) )
395                         codec_init(base, i);
396         }
397 }
398
399 static void aza_init(struct device *dev)
400 {
401         uint8_t *base;
402         struct resource *res;
403         uint32_t codec_mask;
404
405
406 //-------------- enable AZA (SiS7502) -------------------------
407 {
408        uint8_t  temp8;
409        int i=0;
410         while(SiS_SiS7502_init[i][0] != 0)
411         {                               temp8 = pci_read_config8(dev, SiS_SiS7502_init[i][0]);
412                                         temp8 &= SiS_SiS7502_init[i][1];
413                                         temp8 |= SiS_SiS7502_init[i][2];                                                                        
414                                         pci_write_config8(dev, SiS_SiS7502_init[i][0], temp8);
415                                         i++;
416         };
417 }
418 //-----------------------------------------------------------
419
420
421 // put audio to D0 state
422 pci_write_config8(dev, 0x54,0x00);
423
424 #if 0
425 {
426     int i;
427     printk_debug("Azalia PCI config \n");
428     for(i=0;i<0xFF;i+=4)
429     {
430         if((i%16)==0)
431         {
432             print_debug("\r\n");print_debug_hex8(i);print_debug("  ");
433         }
434         print_debug_hex32(pci_read_config32(dev,i));
435         print_debug("  ");    
436     }
437     print_debug("\r\n");
438 }
439 #endif 
440
441         res = find_resource(dev, 0x10);
442         if(!res)
443                 return;
444
445         base =(uint8_t *) res->base;
446         printk_debug("base = %08x\n", base);
447
448         codec_mask = codec_detect(base);
449
450         if(codec_mask) {
451                 printk_debug("codec_mask = %02x\n", codec_mask);
452                 codecs_init(base, codec_mask);
453         }
454
455 #if 0
456 {
457     int i;
458     printk_debug("Azalia PCI config \n");
459     for(i=0;i<0xFF;i+=4)
460     {
461         if((i%16)==0)
462         {
463             print_debug("\r\n");print_debug_hex8(i);print_debug("  ");
464         }
465         outl(0x80000800+i,0xcf8);     
466         print_debug_hex32(inl(0xcfc));
467         print_debug("  ");    
468     }
469     print_debug("\r\n");
470 }
471 #endif 
472
473 }
474
475 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
476 {
477         pci_write_config32(dev, 0x40,
478                 ((device & 0xffff) << 16) | (vendor & 0xffff));
479 }
480
481 static struct pci_operations lops_pci = {
482         .set_subsystem  = lpci_set_subsystem,
483 };
484
485 static struct device_operations aza_audio_ops  = {
486         .read_resources = pci_dev_read_resources,
487         .set_resources  = pci_dev_set_resources,
488         .enable_resources       = pci_dev_enable_resources,
489 //      .enable         = sis966_enable,
490         .init           = aza_init,
491         .scan_bus       = 0,
492         .ops_pci        = &lops_pci,
493 };
494
495 static const struct pci_driver azaaudio_driver __pci_driver = {
496         .ops    = &aza_audio_ops,
497         .vendor = PCI_VENDOR_ID_SIS,
498         .device = PCI_DEVICE_ID_SIS_SIS966_AZA,
499 };
500