[PATCH] coreboot: Don't loop forever waiting for HDA codecs
[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 #define HDA_ICII_REG 0x68
30 #define   HDA_ICII_BUSY (1 << 0)
31 #define   HDA_ICII_VALID  (1 << 1)
32
33 static int set_bits(u8 * port, u32 mask, u32 val)
34 {
35         u32 dword;
36         int count;
37
38         val &= mask;
39         dword = readl(port);
40         dword &= ~mask;
41         dword |= val;
42         writel(dword, port);
43
44         count = 50;
45         do {
46                 dword = readl(port);
47                 dword &= mask;
48                 udelay(100);
49         } while ((dword != val) && --count);
50
51         if (!count)
52                 return -1;
53
54         udelay(540);
55         return 0;
56 }
57
58 static int codec_detect(u8 * base)
59 {
60         u32 dword;
61
62         /* 1 */
63         set_bits(base + 0x08, 1, 1);
64
65         /* 2 */
66         dword = readl(base + 0x0e);
67         dword |= 7;
68         writel(dword, base + 0x0e);
69
70         /* 3 */
71         set_bits(base + 0x08, 1, 0);
72
73         /* 4 */
74         set_bits(base + 0x08, 1, 1);
75
76         /* 5 */
77         dword = readl(base + 0xe);
78         dword &= 7;
79
80         /* 6 */
81         if (!dword) {
82                 set_bits(base + 0x08, 1, 0);
83                 printk_debug("No codec!\n");
84                 return 0;
85         }
86         return dword;
87
88 }
89
90 static u32 cim_verb_data[] = {
91         0x01471c10,
92         0x01471d40,
93         0x01471e01,
94         0x01471f01,
95 /* 1 */
96         0x01571c12,
97         0x01571d10,
98         0x01571e01,
99         0x01571f01,
100 /* 2 */
101         0x01671c11,
102         0x01671d60,
103         0x01671e01,
104         0x01671f01,
105 /* 3 */
106         0x01771c14,
107         0x01771d20,
108         0x01771e01,
109         0x01771f01,
110 /* 4 */
111         0x01871c30,
112         0x01871d90,
113         0x01871ea1,
114         0x01871f01,
115 /* 5 */
116         0x01971cf0,
117         0x01971d11,
118         0x01971e11,
119         0x01971f41,
120 /* 6 */
121         0x01a71c80,
122         0x01a71d30,
123         0x01a71e81,
124         0x01a71f01,
125 /* 7 */
126         0x01b71cf0,
127         0x01b71d11,
128         0x01b71e11,
129         0x01b71f41,
130 /* 8 */
131         0x01c71cf0,
132         0x01c71d11,
133         0x01c71e11,
134         0x01c71f41,
135 /* 9 */
136         0x01d71cf0,
137         0x01d71d11,
138         0x01d71e11,
139         0x01d71f41,
140 /* 10 */
141         0x01e71c50,
142         0x01e71d11,
143         0x01e71e44,
144         0x01e71f01,
145 /* 11 */
146         0x01f71c60,
147         0x01f71d61,
148         0x01f71ec4,
149         0x01f71f01,
150 };
151 static unsigned find_verb(u32 viddid, u32 ** verb)
152 {
153         device_t azalia_dev = dev_find_slot(0, PCI_DEVFN(0x14, 2));
154         struct southbridge_amd_sb600_config *cfg =
155             (struct southbridge_amd_sb600_config *)azalia_dev->chip_info;
156         printk_debug("Dev=%s\n", dev_path(azalia_dev));
157         printk_debug("Default viddid=%x\n", cfg->hda_viddid);
158         printk_debug("Reading viddid=%x\n", viddid);
159         if (!cfg)
160                 return 0;
161         if (viddid != cfg->hda_viddid)
162                 return 0;
163         *verb = (u32 *) cim_verb_data;
164         return sizeof(cim_verb_data) / sizeof(u32);
165 }
166
167 /**
168  *  Wait 50usec for for the codec to indicate it is ready
169  *  no response would imply that the codec is non-operative
170  */
171
172 static int wait_for_ready(u8 *base)
173 {
174         /* Use a 50 usec timeout - the Linux kernel uses the
175          * same duration */
176
177         int timeout = 50;
178
179         while(timeout--) {
180                 u32 dword=readl(base +  HDA_ICII_REG);
181                 if (!(dword & HDA_ICII_BUSY))
182                         return 0;
183                 udelay(1);
184         }
185
186         return -1;
187 }
188
189 /**
190  *  Wait 50usec for for the codec to indicate that it accepted
191  *  the previous command.  No response would imply that the code
192  *  is non-operative
193  */
194
195 static int wait_for_valid(u8 *base)
196 {
197         /* Use a 50 usec timeout - the Linux kernel uses the
198          * same duration */
199
200         int timeout = 50;
201         while(timeout--) {
202                 u32 dword = readl(base + HDA_ICII_REG);
203                 if ((dword & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
204                         HDA_ICII_VALID)
205                         return 0;
206                 udelay(1);
207         }
208
209         return 1;
210 }
211
212 static void codec_init(u8 * base, int addr)
213 {
214         u32 dword;
215         u32 *verb;
216         u32 verb_size;
217         int i;
218
219         /* 1 */
220         if (wait_for_ready(base) == -1)
221                 return;
222
223         dword = (addr << 28) | 0x000f0000;
224         writel(dword, base + 0x60);
225
226         if (wait_for_valid(base) == -1)
227                 return;
228
229         dword = readl(base + 0x64);
230
231         /* 2 */
232         printk_debug("codec viddid: %08x\n", dword);
233         verb_size = find_verb(dword, &verb);
234
235         if (!verb_size) {
236                 printk_debug("No verb!\n");
237                 return;
238         }
239
240         printk_debug("verb_size: %d\n", verb_size);
241         /* 3 */
242         for (i = 0; i < verb_size; i++) {
243                 if (wait_for_ready(base) == -1)
244                         return;
245
246                 writel(verb[i], base + 0x60);
247
248                 if (wait_for_valid(base) == -1)
249                         return;
250         }
251         printk_debug("verb loaded!\n");
252 }
253
254 static void codecs_init(u8 * base, u32 codec_mask)
255 {
256         int i;
257         for (i = 2; i >= 0; i--) {
258                 if (codec_mask & (1 << i))
259                         codec_init(base, i);
260         }
261 }
262
263 static void hda_init(struct device *dev)
264 {
265         u8 *base;
266         struct resource *res;
267         u32 codec_mask;
268
269         /* SM Setting */
270         device_t hda_dev;
271         hda_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
272         /* Set routing pin */
273         pci_write_config32(dev, 0xf8, 0x0);
274         pci_write_config8(dev, 0xfc, 0xAA);
275         /* Set INTA */
276         pci_write_config8(dev, 0x63, 0x0);
277         /* Enable azalia, disable ac97 */
278         pm_iowrite(0x59, 0xB);
279
280         res = find_resource(dev, 0x10);
281         if (!res)
282                 return;
283
284         base = (u8 *) ((u32)res->base);
285         printk_debug("base = %08x\n", base);
286         codec_mask = codec_detect(base);
287
288         if (codec_mask) {
289                 printk_debug("codec_mask = %02x\n", codec_mask);
290                 codecs_init(base, codec_mask);
291         }
292 }
293
294 static struct pci_operations lops_pci = {
295         .set_subsystem = pci_dev_set_subsystem,
296 };
297
298 static struct device_operations hda_audio_ops = {
299         .read_resources = pci_dev_read_resources,
300         .set_resources = pci_dev_set_resources,
301         .enable_resources = pci_dev_enable_resources,
302         /*.enable           = sb600_enable, */
303         .init = hda_init,
304         .scan_bus = 0,
305         .ops_pci = &lops_pci,
306 };
307
308 static struct pci_driver hdaaudio_driver __pci_driver = {
309         .ops = &hda_audio_ops,
310         .vendor = PCI_VENDOR_ID_ATI,
311         .device = PCI_DEVICE_ID_ATI_SB600_HDA,
312 };