1. vgabios removed, will go to extra repository
[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(500);
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(100);
88                 dword = readl(base + 0x68);
89      } while ((dword & 3) != 2);
90
91      dword = readl(base + 0x64);
92      return dword;
93
94 }
95
96
97 static int codec_detect(uint8_t *base)
98 {
99         uint32_t dword;
100         int idx=0;
101
102         /* 1 */ // controller reset
103         printk_debug("controller reset\n");
104
105         set_bits(base + 0x08, 1, 1);
106
107       do{
108                 dword = readl(base + 0x08)&0x1;
109                 if(idx++>1000) { printk_debug("controller reset fail !!! \n"); break;}
110            } while (dword !=1);
111
112        dword=send_verb(base,0x000F0000); // get codec VendorId and DeviceId
113
114        if(dword==0) {
115                 printk_debug("No codec!\n");
116                 return 0;
117        }
118
119          printk_debug("Codec ID = %lx\n", dword);
120
121        dword=0x1;
122         return dword;
123
124 }
125
126
127 static uint32_t verb_data[] = {
128
129 //14
130         0x01471c10,
131         0x01471d40,
132         0x01471e01,
133         0x01471f01,
134 //15
135         0x01571c12,
136         0x01571d10,
137         0x01571e01,
138         0x01571f01,
139 //16
140         0x01671c11,
141         0x01671d60,
142         0x01671e01,
143         0x01671f01,
144 //17
145         0x01771c14,
146         0x01771d20,
147         0x01771e01,
148         0x01771f01,
149 //18
150         0x01871c40,
151         0x01871d98,
152         0x01871ea1,
153         0x01871f01,
154 //19
155         0x01971c50,
156         0x01971d98,
157         0x01971ea1,
158         0x01971f02,
159 //1a
160         0x01a71c4f,
161         0x01a71d30,
162         0x01a71e81,
163         0x01a71f01,
164 //1b
165         0x01b71c20,
166         0x01b71d40,
167         0x01b71e01,
168         0x01b71f02,
169 //1c
170         0x01c71cf0,
171         0x01c71d01,
172         0x01c71e33,
173         0x01c71f59,
174 //1d
175         0x01d71c01,
176         0x01d71de6,
177         0x01d71e05,
178         0x01d71f40,
179 //1e
180         0x01e71c30,
181         0x01e71d11,
182         0x01e71e44,
183         0x01e71f01,
184 //1f
185         0x01f71c60,
186         0x01f71d61,
187         0x01f71ec4,
188         0x01f71f01,
189 };
190
191 static unsigned find_verb(uint32_t viddid, uint32_t **verb)
192 {
193         if((viddid == 0x10ec0883) || (viddid == 0x10ec0882) || (viddid == 0x10ec0880)) return 0;
194         *verb =  (uint32_t *)verb_data;
195         return sizeof(verb_data)/sizeof(uint32_t);
196 }
197
198
199 static void codec_init(uint8_t *base, int addr)
200 {
201         uint32_t dword;
202         uint32_t *verb;
203         unsigned verb_size;
204         int i;
205
206         /* 1 */
207         do {
208                 dword = readl(base + 0x68);
209         } while (dword & 1);
210
211         dword = (addr<<28) | 0x000f0000;
212         writel(dword, base + 0x60);
213
214         do {
215                 dword = readl(base + 0x68);
216         } while ((dword & 3)!=2);
217
218         dword = readl(base + 0x64);
219
220         /* 2 */
221         printk_debug("codec viddid: %08x\n", dword);
222         verb_size = find_verb(dword, &verb);
223
224         if(!verb_size) {
225                 printk_debug("No verb!\n");
226                 return;
227         }
228
229         printk_debug("verb_size: %d\n", verb_size);
230         /* 3 */
231         for(i=0; i<verb_size; i++) {
232                 send_verb(base,verb[i]);
233         }
234         printk_debug("verb loaded!\n");
235 }
236
237 static void codecs_init(uint8_t *base, uint32_t codec_mask)
238 {
239         int i;
240         codec_init(base, 0);
241         return;
242         for(i=2; i>=0; i--) {
243                 if( codec_mask & (1<<i) )
244                         codec_init(base, i);
245         }
246 }
247
248 static void aza_init(struct device *dev)
249 {
250         uint8_t *base;
251         struct resource *res;
252         uint32_t codec_mask;
253
254         print_debug("AZALIA_INIT:---------->\n");
255
256 //-------------- enable AZA (SiS7502) -------------------------
257 {
258         uint8_t  temp8;
259         int i=0;
260         while(SiS_SiS7502_init[i][0] != 0)
261         {
262                 temp8 = pci_read_config8(dev, SiS_SiS7502_init[i][0]);
263                 temp8 &= SiS_SiS7502_init[i][1];
264                 temp8 |= SiS_SiS7502_init[i][2];
265                 pci_write_config8(dev, SiS_SiS7502_init[i][0], temp8);
266                 i++;
267         };
268 }
269 //-----------------------------------------------------------
270
271
272         // put audio to D0 state
273         pci_write_config8(dev, 0x54,0x00);
274
275 #if DEBUG_AZA
276 {
277         int i;
278
279         print_debug("****** Azalia PCI config ******");
280         print_debug("\n    03020100  07060504  0B0A0908  0F0E0D0C");
281
282         for(i=0;i<0xff;i+=4){
283                 if((i%16)==0){
284                         print_debug("\r\n");
285                         print_debug_hex8(i);
286                         print_debug(": ");
287                 }
288                 print_debug_hex32(pci_read_config32(dev,i));
289                 print_debug("  ");
290         }
291         print_debug("\r\n");
292 }
293 #endif
294
295         res = find_resource(dev, 0x10);
296         if(!res)
297                 return;
298
299         base =(uint8_t *) res->base;
300         printk_debug("base = %08x\n", base);
301
302         codec_mask = codec_detect(base);
303
304         if(codec_mask) {
305                 printk_debug("codec_mask = %02x\n", codec_mask);
306                 codecs_init(base, codec_mask);
307         }
308
309         print_debug("AZALIA_INIT:<----------\n");
310 }
311
312 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
313 {
314         pci_write_config32(dev, 0x40,
315                 ((device & 0xffff) << 16) | (vendor & 0xffff));
316 }
317
318 static struct pci_operations lops_pci = {
319         .set_subsystem  = lpci_set_subsystem,
320 };
321
322 static struct device_operations aza_audio_ops  = {
323         .read_resources = pci_dev_read_resources,
324         .set_resources  = pci_dev_set_resources,
325         .enable_resources       = pci_dev_enable_resources,
326 //      .enable         = sis966_enable,
327         .init           = aza_init,
328         .scan_bus       = 0,
329         .ops_pci        = &lops_pci,
330 };
331
332 static const struct pci_driver azaaudio_driver __pci_driver = {
333         .ops    = &aza_audio_ops,
334         .vendor = PCI_VENDOR_ID_SIS,
335         .device = PCI_DEVICE_ID_SIS_SIS966_AZA,
336 };
337