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