Patch for AMD SB600 chipset.
[coreboot.git] / src / southbridge / amd / sb600 / sb600_hda.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <console/console.h>
21 #include <device/device.h>
22 #include <device/pci.h>
23 #include <device/pci_ids.h>
24 #include <device/pci_ops.h>
25 #include <arch/io.h>
26 #include <delay.h>
27 #include "sb600.h"
28
29 static int set_bits(u8 * port, u32 mask, u32 val)
30 {
31         u32 dword;
32         int count;
33
34         val &= mask;
35         dword = readl(port);
36         dword &= ~mask;
37         dword |= val;
38         writel(dword, port);
39
40         count = 50;
41         do {
42                 dword = readl(port);
43                 dword &= mask;
44                 udelay(100);
45         } while ((dword != val) && --count);
46
47         if (!count)
48                 return -1;
49
50         udelay(540);
51         return 0;
52 }
53
54 static int codec_detect(u8 * base)
55 {
56         u32 dword;
57
58         /* 1 */
59         set_bits(base + 0x08, 1, 1);
60
61         /* 2 */
62         dword = readl(base + 0x0e);
63         dword |= 7;
64         writel(dword, base + 0x0e);
65
66         /* 3 */
67         set_bits(base + 0x08, 1, 0);
68
69         /* 4 */
70         set_bits(base + 0x08, 1, 1);
71
72         /* 5 */
73         dword = readl(base + 0xe);
74         dword &= 7;
75
76         /* 6 */
77         if (!dword) {
78                 set_bits(base + 0x08, 1, 0);
79                 printk_debug("No codec!\n");
80                 return 0;
81         }
82         return dword;
83
84 }
85
86 static u32 cim_verb_data[] = {
87         0x01471c10,
88         0x01471d40,
89         0x01471e01,
90         0x01471f01,
91 /* 1 */
92         0x01571c12,
93         0x01571d10,
94         0x01571e01,
95         0x01571f01,
96 /* 2 */
97         0x01671c11,
98         0x01671d60,
99         0x01671e01,
100         0x01671f01,
101 /* 3 */
102         0x01771c14,
103         0x01771d20,
104         0x01771e01,
105         0x01771f01,
106 /* 4 */
107         0x01871c30,
108         0x01871d90,
109         0x01871ea1,
110         0x01871f01,
111 /* 5 */
112         0x01971cf0,
113         0x01971d11,
114         0x01971e11,
115         0x01971f41,
116 /* 6 */
117         0x01a71c80,
118         0x01a71d30,
119         0x01a71e81,
120         0x01a71f01,
121 /* 7 */
122         0x01b71cf0,
123         0x01b71d11,
124         0x01b71e11,
125         0x01b71f41,
126 /* 8 */
127         0x01c71cf0,
128         0x01c71d11,
129         0x01c71e11,
130         0x01c71f41,
131 /* 9 */
132         0x01d71cf0,
133         0x01d71d11,
134         0x01d71e11,
135         0x01d71f41,
136 /* 10 */
137         0x01e71c50,
138         0x01e71d11,
139         0x01e71e44,
140         0x01e71f01,
141 /* 11 */
142         0x01f71c60,
143         0x01f71d61,
144         0x01f71ec4,
145         0x01f71f01,
146 };
147 static unsigned find_verb(u32 viddid, u32 ** verb)
148 {
149         device_t azalia_dev = dev_find_slot(0, PCI_DEVFN(0x14, 2));
150         struct southbridge_amd_sb600_config *cfg =
151             (struct southbridge_amd_sb600_config *)azalia_dev->chip_info;
152         printk_debug("Dev=%s\n", dev_path(azalia_dev));
153         printk_debug("Default viddid=%x\n", cfg->hda_viddid);
154         printk_debug("Reading viddid=%x\n", viddid);
155         if (!cfg)
156                 return 0;
157         if (viddid != cfg->hda_viddid)
158                 return 0;
159         *verb = (u32 *) cim_verb_data;
160         return sizeof(cim_verb_data) / sizeof(u32);
161 }
162
163 static void codec_init(u8 * base, int addr)
164 {
165         u32 dword;
166         u32 *verb;
167         u32 verb_size;
168         int i;
169
170         /* 1 */
171         do {
172                 dword = readl(base + 0x68);
173         } while (dword & 1);
174
175         dword = (addr << 28) | 0x000f0000;
176         writel(dword, base + 0x60);
177
178         do {
179                 dword = readl(base + 0x68);
180         } while ((dword & 3) != 2);
181
182         dword = readl(base + 0x64);
183
184         /* 2 */
185         printk_debug("codec viddid: %08x\n", dword);
186         verb_size = find_verb(dword, &verb);
187
188         if (!verb_size) {
189                 printk_debug("No verb!\n");
190                 return;
191         }
192
193         printk_debug("verb_size: %d\n", verb_size);
194         /* 3 */
195         for (i = 0; i < verb_size; i++) {
196                 do {
197                         dword = readl(base + 0x68);
198                 } while (dword & 1);
199
200                 writel(verb[i], base + 0x60);
201
202                 do {
203                         dword = readl(base + 0x68);
204                 } while ((dword & 3) != 2);
205         }
206         printk_debug("verb loaded!\n");
207 }
208
209 static void codecs_init(u8 * base, u32 codec_mask)
210 {
211         int i;
212         for (i = 2; i >= 0; i--) {
213                 if (codec_mask & (1 << i))
214                         codec_init(base, i);
215         }
216 }
217
218 static void hda_init(struct device *dev)
219 {
220         u8 *base;
221         struct resource *res;
222         u32 codec_mask;
223
224         /* SM Setting */
225         device_t hda_dev;
226         hda_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
227         /* Set routing pin */
228         pci_write_config32(dev, 0xf8, 0x0);
229         pci_write_config8(dev, 0xfc, 0xAA);
230         /* Set INTA */
231         pci_write_config8(dev, 0x63, 0x0);
232         /* Enable azalia, disable ac97 */
233         pm_iowrite(0x59, 0xB);
234
235         res = find_resource(dev, 0x10);
236         if (!res)
237                 return;
238
239         base = (u8 *) ((u32)res->base);
240         printk_debug("base = %08x\n", base);
241         codec_mask = codec_detect(base);
242
243         if (codec_mask) {
244                 printk_debug("codec_mask = %02x\n", codec_mask);
245                 codecs_init(base, codec_mask);
246         }
247 }
248
249 static struct pci_operations lops_pci = {
250         .set_subsystem = pci_dev_set_subsystem,
251 };
252
253 static struct device_operations hda_audio_ops = {
254         .read_resources = pci_dev_read_resources,
255         .set_resources = pci_dev_set_resources,
256         .enable_resources = pci_dev_enable_resources,
257         /*.enable           = sb600_enable, */
258         .init = hda_init,
259         .scan_bus = 0,
260         .ops_pci = &lops_pci,
261 };
262
263 static struct pci_driver hdaaudio_driver __pci_driver = {
264         .ops = &hda_audio_ops,
265         .vendor = PCI_VENDOR_ID_ATI,
266         .device = PCI_DEVICE_ID_ATI_SB600_HDA,
267 };