2 * This file is part of the coreboot project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
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.
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.
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
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>
29 #define HDA_ICII_REG 0x68
30 #define HDA_ICII_BUSY (1 << 0)
31 #define HDA_ICII_VALID (1 << 1)
33 static int set_bits(u8 * port, u32 mask, u32 val)
49 } while ((dword != val) && --count);
58 static int codec_detect(u8 * base)
63 set_bits(base + 0x08, 1, 1);
66 dword = readl(base + 0x0e);
68 writel(dword, base + 0x0e);
71 set_bits(base + 0x08, 1, 0);
74 set_bits(base + 0x08, 1, 1);
77 dword = readl(base + 0xe);
82 set_bits(base + 0x08, 1, 0);
83 printk_debug("No codec!\n");
90 static u32 cim_verb_data[] = {
151 static unsigned find_verb(u32 viddid, u32 ** verb)
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);
161 if (viddid != cfg->hda_viddid)
163 *verb = (u32 *) cim_verb_data;
164 return sizeof(cim_verb_data) / sizeof(u32);
168 * Wait 50usec for for the codec to indicate it is ready
169 * no response would imply that the codec is non-operative
172 static int wait_for_ready(u8 *base)
174 /* Use a 50 usec timeout - the Linux kernel uses the
180 u32 dword=readl(base + HDA_ICII_REG);
181 if (!(dword & HDA_ICII_BUSY))
190 * Wait 50usec for for the codec to indicate that it accepted
191 * the previous command. No response would imply that the code
195 static int wait_for_valid(u8 *base)
197 /* Use a 50 usec timeout - the Linux kernel uses the
202 u32 dword = readl(base + HDA_ICII_REG);
203 if ((dword & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
212 static void codec_init(u8 * base, int addr)
220 if (wait_for_ready(base) == -1)
223 dword = (addr << 28) | 0x000f0000;
224 writel(dword, base + 0x60);
226 if (wait_for_valid(base) == -1)
229 dword = readl(base + 0x64);
232 printk_debug("codec viddid: %08x\n", dword);
233 verb_size = find_verb(dword, &verb);
236 printk_debug("No verb!\n");
240 printk_debug("verb_size: %d\n", verb_size);
242 for (i = 0; i < verb_size; i++) {
243 if (wait_for_ready(base) == -1)
246 writel(verb[i], base + 0x60);
248 if (wait_for_valid(base) == -1)
251 printk_debug("verb loaded!\n");
254 static void codecs_init(u8 * base, u32 codec_mask)
257 for (i = 2; i >= 0; i--) {
258 if (codec_mask & (1 << i))
263 static void hda_init(struct device *dev)
266 struct resource *res;
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);
276 pci_write_config8(dev, 0x63, 0x0);
277 /* Enable azalia, disable ac97 */
278 pm_iowrite(0x59, 0xB);
280 res = find_resource(dev, 0x10);
284 base = (u8 *) ((u32)res->base);
285 printk_debug("base = %08x\n", base);
286 codec_mask = codec_detect(base);
289 printk_debug("codec_mask = %02x\n", codec_mask);
290 codecs_init(base, codec_mask);
294 static struct pci_operations lops_pci = {
295 .set_subsystem = pci_dev_set_subsystem,
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, */
305 .ops_pci = &lops_pci,
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,