Move MMCONF resource into the domain for fam10 for the resource allocator.
[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 occurred */
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
155 static u32 find_verb(u32 viddid, u32 ** verb)
156 {
157         device_t azalia_dev = dev_find_slot(0, PCI_DEVFN(0x14, 2));
158         struct southbridge_amd_sb600_config *cfg =
159             (struct southbridge_amd_sb600_config *)azalia_dev->chip_info;
160         printk(BIOS_DEBUG, "Dev=%s\n", dev_path(azalia_dev));
161         printk(BIOS_DEBUG, "Default viddid=%x\n", cfg->hda_viddid);
162         printk(BIOS_DEBUG, "Reading viddid=%x\n", viddid);
163         if (!cfg)
164                 return 0;
165         if (viddid != cfg->hda_viddid)
166                 return 0;
167         *verb = (u32 *) cim_verb_data;
168         return sizeof(cim_verb_data) / sizeof(u32);
169 }
170
171 /**
172  *  Wait 50usec for the codec to indicate it is ready
173  *  no response would imply that the codec is non-operative
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 the codec to indicate that it accepted
194  *  the previous command.  No response would imply that the code
195  *  is non-operative
196  */
197 static int wait_for_valid(u32 base)
198 {
199         /* Use a 50 usec timeout - the Linux kernel uses the
200          * same duration */
201
202         int timeout = 50;
203         while(timeout--) {
204                 u32 dword = read32(base + HDA_ICII_REG);
205                 if ((dword & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
206                         HDA_ICII_VALID)
207                         return 0;
208                 udelay(1);
209         }
210
211         return 1;
212 }
213
214 static void codec_init(u32 base, int addr)
215 {
216         u32 dword;
217         u32 *verb;
218         u32 verb_size;
219         int i;
220
221         /* 1 */
222         if (wait_for_ready(base) == -1)
223                 return;
224
225         dword = (addr << 28) | 0x000f0000;
226         write32(base + 0x60, dword);
227
228         if (wait_for_valid(base) == -1)
229                 return;
230
231         dword = read32(base + 0x64);
232
233         /* 2 */
234         printk(BIOS_DEBUG, "codec viddid: %08x\n", dword);
235         verb_size = find_verb(dword, &verb);
236
237         if (!verb_size) {
238                 printk(BIOS_DEBUG, "No verb!\n");
239                 return;
240         }
241
242         printk(BIOS_DEBUG, "verb_size: %d\n", verb_size);
243         /* 3 */
244         for (i = 0; i < verb_size; i++) {
245                 if (wait_for_ready(base) == -1)
246                         return;
247
248                 write32(base + 0x60, verb[i]);
249
250                 if (wait_for_valid(base) == -1)
251                         return;
252         }
253         printk(BIOS_DEBUG, "verb loaded!\n");
254 }
255
256 static void codecs_init(u32 base, u32 codec_mask)
257 {
258         int i;
259         for (i = 2; i >= 0; i--) {
260                 if (codec_mask & (1 << i))
261                         codec_init(base, i);
262         }
263 }
264
265 static void hda_init(struct device *dev)
266 {
267         u8 byte;
268         u32 dword;
269         u32 base;
270         struct resource *res;
271         u32 codec_mask;
272         device_t sm_dev;
273
274         /* Enable azalia - PM_io 0x59[4], disable ac97 - PM_io 0x59[1..0] */
275         pm_iowrite(0x59, 0xB);
276
277         /* Find the SMBus */
278         /* FIXME: Need to find out why the call below crashes. */
279         /*sm_dev = dev_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_ATI_SB600_SM, 0);*/
280         sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
281
282         /* Set routing pin - SMBus ExtFunc (0xf8/0xfc) */
283         pci_write_config32(sm_dev, 0xf8, 0x00);
284         pci_write_config8(sm_dev, 0xfc, 0xAA);
285         /* Set INTA - SMBus 0x63 [2..0] */
286         byte = pci_read_config8(sm_dev, 0x63);
287         byte &= ~0x7;
288         byte |= 0x0; /* INTA:0x0 - INTH:0x7 */
289         pci_write_config8(sm_dev, 0x63, byte);
290
291         /* Program the 2C to 0x437b1002 */
292         dword = 0x437b1002;
293         pci_write_config32(dev, 0x2c, dword);
294
295         /* Read in BAR */
296         /* Is this right? HDA allows for a 64-bit BAR
297          * but this is only setup for a 32-bit one
298          */
299         res = find_resource(dev, 0x10);
300         if (!res)
301                 return;
302
303         base = (u32)res->base;
304         printk(BIOS_DEBUG, "base = 0x%x\n", base);
305         codec_mask = codec_detect(base);
306
307         if (codec_mask) {
308                 printk(BIOS_DEBUG, "codec_mask = %02x\n", codec_mask);
309                 codecs_init(base, codec_mask);
310         }
311 }
312
313 static struct pci_operations lops_pci = {
314         .set_subsystem = pci_dev_set_subsystem,
315 };
316
317 static struct device_operations hda_audio_ops = {
318         .read_resources = pci_dev_read_resources,
319         .set_resources = pci_dev_set_resources,
320         .enable_resources = pci_dev_enable_resources,
321         /*.enable           = sb600_enable, */
322         .init = hda_init,
323         .scan_bus = 0,
324         .ops_pci = &lops_pci,
325 };
326
327 static const struct pci_driver hdaaudio_driver __pci_driver = {
328         .ops = &hda_audio_ops,
329         .vendor = PCI_VENDOR_ID_ATI,
330         .device = PCI_DEVICE_ID_ATI_SB600_HDA,
331 };