first round name simplification. drop the <component>_ prefix.
[coreboot.git] / src / southbridge / sis / sis966 / aza.c
1 /*
2  * This file is part of the coreboot 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 <delay.h>
33 #include "sis966.h"
34
35 u8      SiS_SiS7502_init[7][3]={
36 {0x04, 0xFF, 0x07},
37 {0x2C, 0xFF, 0x39},
38 {0x2D, 0xFF, 0x10},
39 {0x2E, 0xFF, 0x91},
40 {0x2F, 0xFF, 0x01},
41 {0x04, 0xFF, 0x06},
42 {0x00, 0x00, 0x00}                                      //End of table
43 };
44
45 static int set_bits(u32 port, u32 mask, u32 val)
46 {
47         u32 dword;
48         int count;
49
50         val &= mask;
51         dword = read32(port);
52         dword &= ~mask;
53         dword |= val;
54         write32(port, dword);
55
56         count = 50;
57         do {
58                 dword = read32(port);
59                 dword &= mask;
60                 udelay(100);
61         } while ((dword != val) && --count);
62
63         if(!count) return -1;
64
65         udelay(500);
66         return 0;
67
68 }
69
70 static u32 send_verb(u32 base, u32 verb)
71 {
72      u32 dword;
73
74      dword = read32(base + 0x68);
75      dword=dword|(unsigned long)0x0002;
76      write32(base + 0x68, dword);
77      do {
78                 dword = read32(base + 0x68);
79      }  while ((dword & 1)!=0);
80      write32(base + 0x60, verb);
81      udelay(500);
82      dword = read32(base + 0x68);
83      dword =(dword |0x1);
84      write32(base + 0x68, dword);
85      do {
86                 udelay(100);
87                 dword = read32(base + 0x68);
88      } while ((dword & 3) != 2);
89
90      dword = read32(base + 0x64);
91      return dword;
92 }
93
94
95 static int codec_detect(u32 base)
96 {
97         u32 dword;
98         int idx=0;
99
100         /* 1 */ // controller reset
101         printk(BIOS_DEBUG, "controller reset\n");
102
103         set_bits(base + 0x08, 1, 1);
104
105       do{
106                 dword = read32(base + 0x08)&0x1;
107                 if(idx++>1000) { printk(BIOS_DEBUG, "controller reset fail !!! \n"); break;}
108            } while (dword !=1);
109
110        dword=send_verb(base,0x000F0000); // get codec VendorId and DeviceId
111
112        if(dword==0) {
113                 printk(BIOS_DEBUG, "No codec!\n");
114                 return 0;
115        }
116
117          printk(BIOS_DEBUG, "Codec ID = %x\n", dword);
118
119        dword=0x1;
120         return dword;
121
122 }
123
124
125 static u32 verb_data[] = {
126
127 //14
128         0x01471c10,
129         0x01471d40,
130         0x01471e01,
131         0x01471f01,
132 //15
133         0x01571c12,
134         0x01571d10,
135         0x01571e01,
136         0x01571f01,
137 //16
138         0x01671c11,
139         0x01671d60,
140         0x01671e01,
141         0x01671f01,
142 //17
143         0x01771c14,
144         0x01771d20,
145         0x01771e01,
146         0x01771f01,
147 //18
148         0x01871c40,
149         0x01871d98,
150         0x01871ea1,
151         0x01871f01,
152 //19
153         0x01971c50,
154         0x01971d98,
155         0x01971ea1,
156         0x01971f02,
157 //1a
158         0x01a71c4f,
159         0x01a71d30,
160         0x01a71e81,
161         0x01a71f01,
162 //1b
163         0x01b71c20,
164         0x01b71d40,
165         0x01b71e01,
166         0x01b71f02,
167 //1c
168         0x01c71cf0,
169         0x01c71d01,
170         0x01c71e33,
171         0x01c71f59,
172 //1d
173         0x01d71c01,
174         0x01d71de6,
175         0x01d71e05,
176         0x01d71f40,
177 //1e
178         0x01e71c30,
179         0x01e71d11,
180         0x01e71e44,
181         0x01e71f01,
182 //1f
183         0x01f71c60,
184         0x01f71d61,
185         0x01f71ec4,
186         0x01f71f01,
187 };
188
189 static unsigned find_verb(u32 viddid, u32 **verb)
190 {
191         if((viddid == 0x10ec0883) || (viddid == 0x10ec0882) || (viddid == 0x10ec0880)) return 0;
192         *verb =  (u32 *)verb_data;
193         return sizeof(verb_data)/sizeof(u32);
194 }
195
196
197 static void codec_init(u32 base, int addr)
198 {
199         u32 dword;
200         u32 *verb;
201         unsigned verb_size;
202         int i;
203
204         /* 1 */
205         do {
206                 dword = read32(base + 0x68);
207         } while (dword & 1);
208
209         dword = (addr<<28) | 0x000f0000;
210         write32(base + 0x60, dword);
211
212         do {
213                 dword = read32(base + 0x68);
214         } while ((dword & 3)!=2);
215
216         dword = read32(base + 0x64);
217
218         /* 2 */
219         printk(BIOS_DEBUG, "codec viddid: %08x\n", dword);
220         verb_size = find_verb(dword, &verb);
221
222         if(!verb_size) {
223                 printk(BIOS_DEBUG, "No verb!\n");
224                 return;
225         }
226
227         printk(BIOS_DEBUG, "verb_size: %d\n", verb_size);
228         /* 3 */
229         for(i=0; i<verb_size; i++) {
230                 send_verb(base,verb[i]);
231         }
232         printk(BIOS_DEBUG, "verb loaded!\n");
233 }
234
235 static void codecs_init(u32 base, u32 codec_mask)
236 {
237         codec_init(base, 0);
238         return;
239 }
240
241 static void aza_init(struct device *dev)
242 {
243         u32 base;
244         struct resource *res;
245         u32 codec_mask;
246
247         print_debug("AZALIA_INIT:---------->\n");
248
249 //-------------- enable AZA (SiS7502) -------------------------
250 {
251         u8  temp8;
252         int i=0;
253         while(SiS_SiS7502_init[i][0] != 0)
254         {
255                 temp8 = pci_read_config8(dev, SiS_SiS7502_init[i][0]);
256                 temp8 &= SiS_SiS7502_init[i][1];
257                 temp8 |= SiS_SiS7502_init[i][2];
258                 pci_write_config8(dev, SiS_SiS7502_init[i][0], temp8);
259                 i++;
260         };
261 }
262 //-----------------------------------------------------------
263
264
265         // put audio to D0 state
266         pci_write_config8(dev, 0x54,0x00);
267
268 #if DEBUG_AZA
269 {
270         int i;
271
272         print_debug("****** Azalia PCI config ******");
273         print_debug("\n    03020100  07060504  0B0A0908  0F0E0D0C");
274
275         for(i=0;i<0xff;i+=4){
276                 if((i%16)==0){
277                         print_debug("\n");
278                         print_debug_hex8(i);
279                         print_debug(": ");
280                 }
281                 print_debug_hex32(pci_read_config32(dev,i));
282                 print_debug("  ");
283         }
284         print_debug("\n");
285 }
286 #endif
287
288         res = find_resource(dev, 0x10);
289         if(!res)
290                 return;
291
292         base = res->base;
293         printk(BIOS_DEBUG, "base = 0x%08x\n", base);
294
295         codec_mask = codec_detect(base);
296
297         if(codec_mask) {
298                 printk(BIOS_DEBUG, "codec_mask = %02x\n", codec_mask);
299                 codecs_init(base, codec_mask);
300         }
301
302         print_debug("AZALIA_INIT:<----------\n");
303 }
304
305 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
306 {
307         pci_write_config32(dev, 0x40,
308                 ((device & 0xffff) << 16) | (vendor & 0xffff));
309 }
310
311 static struct pci_operations lops_pci = {
312         .set_subsystem  = lpci_set_subsystem,
313 };
314
315 static struct device_operations aza_audio_ops  = {
316         .read_resources = pci_dev_read_resources,
317         .set_resources  = pci_dev_set_resources,
318         .enable_resources       = pci_dev_enable_resources,
319 //      .enable         = sis966_enable,
320         .init           = aza_init,
321         .scan_bus       = 0,
322         .ops_pci        = &lops_pci,
323 };
324
325 static const struct pci_driver azaaudio_driver __pci_driver = {
326         .ops    = &aza_audio_ops,
327         .vendor = PCI_VENDOR_ID_SIS,
328         .device = PCI_DEVICE_ID_SIS_SIS966_HD_AUDIO,
329 };
330