2 * This file is part of the coreboot project.
4 * Copyright (C) 2010 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)
38 /* Write (val & ~mask) to port */
45 /* Wait for readback of register to
46 * match what was just written to it
50 /* Wait 1ms based on BKDG wait time */
54 } while ((dword != val) && --count);
62 static u32 codec_detect(u8 * base)
66 /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */
67 if (set_bits(base + 0x08, 1, 0) == -1)
70 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
71 if (set_bits(base + 0x08, 1, 1) == -1)
74 /* Delay for 1 ms since the BKDG does */
77 /* Read in Codec location (BAR + 0xe)[3..0]*/
78 dword = read32(base + 0xe);
87 /* Put HDA back in reset (BAR + 0x8) [0] */
88 set_bits(base + 0x08, 1, 0);
89 printk_debug("No codec!\n");
94 * Wait 50usec for for the codec to indicate it is ready
95 * no response would imply that the codec is non-operative
97 static int wait_for_ready(u8 *base)
99 /* Use a 50 usec timeout - the Linux kernel uses the
105 u32 dword=read32(base + HDA_ICII_REG);
106 if (!(dword & HDA_ICII_BUSY))
115 * Wait 50usec for for the codec to indicate that it accepted
116 * the previous command. No response would imply that the code
119 static int wait_for_valid(u8 *base)
121 /* Use a 50 usec timeout - the Linux kernel uses the
126 u32 dword = read32(base + HDA_ICII_REG);
127 if ((dword & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
136 static void codec_init(u8 * base, int addr)
141 if (wait_for_ready(base) == -1)
144 dword = (addr << 28) | 0x000f0000;
145 write32(base + 0x60, dword);
147 if (wait_for_valid(base) == -1)
150 dword = read32(base + 0x64);
153 printk_debug("%x(th) codec viddid: %08x\n", addr, dword);
156 static void codecs_init(u8 * base, u32 codec_mask)
159 for (i = 2; i >= 0; i--) {
160 if (codec_mask & (1 << i))
165 static void hda_init(struct device *dev)
170 struct resource *res;
174 /* Enable azalia - PM_io 0x59[3], no ac97 in sb700. */
175 byte = pm_ioread(0x59);
177 pm_iowrite(0x59, byte);
180 /* FIXME: Need to find out why the call below crashes. */
181 /*sm_dev = dev_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_ATI_SB700_SM, 0);*/
182 sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
184 /* Set routing pin - SMBus ExtFunc (0xf8/0xfc) */
185 pci_write_config32(sm_dev, 0xf8, 0x00);
186 pci_write_config8(sm_dev, 0xfc, 0xAA);
187 /* Set INTA - SMBus 0x63 [2..0] */
188 byte = pci_read_config8(sm_dev, 0x63);
190 byte |= 0x0; /* INTA:0x0 - INTH:0x7 */
191 pci_write_config8(sm_dev, 0x63, byte);
193 /* Program the 2C to 0x437b1002 */
195 pci_write_config32(dev, 0x2c, dword);
198 /* Is this right? HDA allows for a 64-bit BAR
199 * but this is only setup for a 32-bit one
201 res = find_resource(dev, 0x10);
205 base = (u8 *) ((u32)res->base);
206 printk_debug("base = %p\n", base);
207 codec_mask = codec_detect(base);
210 printk_debug("codec_mask = %02x\n", codec_mask);
211 codecs_init(base, codec_mask);
215 static struct pci_operations lops_pci = {
216 .set_subsystem = pci_dev_set_subsystem,
219 static struct device_operations hda_audio_ops = {
220 .read_resources = pci_dev_read_resources,
221 .set_resources = pci_dev_set_resources,
222 .enable_resources = pci_dev_enable_resources,
225 .ops_pci = &lops_pci,
228 static struct pci_driver hdaaudio_driver __pci_driver = {
229 .ops = &hda_audio_ops,
230 .vendor = PCI_VENDOR_ID_ATI,
231 .device = PCI_DEVICE_ID_ATI_SB700_HDA,