Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2010 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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 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 "sb700.h" |
| 28 | |
| 29 | #define HDA_ICII_REG 0x68 |
Andrew Wu | ae8d069 | 2013-08-02 19:29:17 +0800 | [diff] [blame] | 30 | #define HDA_ICII_BUSY (1 << 0) |
| 31 | #define HDA_ICII_VALID (1 << 1) |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 32 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 33 | static int set_bits(void *port, u32 mask, u32 val) |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 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 | |
Martin Roth | dcf253c | 2014-12-16 20:51:31 -0700 | [diff] [blame] | 56 | /* Timeout occurred */ |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 57 | if (!count) |
| 58 | return -1; |
| 59 | return 0; |
| 60 | } |
| 61 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 62 | static u32 codec_detect(void *base) |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 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); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 89 | printk(BIOS_DEBUG, "No codec!\n"); |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /** |
Zheng Bao | 2a5101a | 2010-10-10 15:18:53 +0000 | [diff] [blame] | 94 | * Wait 50usec for the codec to indicate it is ready |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 95 | * no response would imply that the codec is non-operative |
| 96 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 97 | static int wait_for_ready(void *base) |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 98 | { |
| 99 | /* Use a 50 usec timeout - the Linux kernel uses the |
| 100 | * same duration */ |
| 101 | |
| 102 | int timeout = 50; |
| 103 | |
| 104 | while(timeout--) { |
| 105 | u32 dword=read32(base + HDA_ICII_REG); |
| 106 | if (!(dword & HDA_ICII_BUSY)) |
| 107 | return 0; |
| 108 | udelay(1); |
| 109 | } |
| 110 | |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | /** |
Zheng Bao | 2a5101a | 2010-10-10 15:18:53 +0000 | [diff] [blame] | 115 | * Wait 50usec for the codec to indicate that it accepted |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 116 | * the previous command. No response would imply that the code |
| 117 | * is non-operative |
| 118 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 119 | static int wait_for_valid(void *base) |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 120 | { |
| 121 | /* Use a 50 usec timeout - the Linux kernel uses the |
| 122 | * same duration */ |
| 123 | |
| 124 | int timeout = 50; |
| 125 | while(timeout--) { |
| 126 | u32 dword = read32(base + HDA_ICII_REG); |
| 127 | if ((dword & (HDA_ICII_VALID | HDA_ICII_BUSY)) == |
| 128 | HDA_ICII_VALID) |
| 129 | return 0; |
| 130 | udelay(1); |
| 131 | } |
| 132 | |
Andrew Wu | 9361daf | 2013-08-02 14:45:03 +0800 | [diff] [blame] | 133 | return -1; |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 136 | static void codec_init(void *base, int addr) |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 137 | { |
| 138 | u32 dword; |
| 139 | |
| 140 | /* 1 */ |
| 141 | if (wait_for_ready(base) == -1) |
| 142 | return; |
| 143 | |
| 144 | dword = (addr << 28) | 0x000f0000; |
| 145 | write32(base + 0x60, dword); |
| 146 | |
| 147 | if (wait_for_valid(base) == -1) |
| 148 | return; |
| 149 | |
| 150 | dword = read32(base + 0x64); |
| 151 | |
| 152 | /* 2 */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 153 | printk(BIOS_DEBUG, "%x(th) codec viddid: %08x\n", addr, dword); |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 156 | static void codecs_init(void *base, u32 codec_mask) |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 157 | { |
| 158 | int i; |
| 159 | for (i = 2; i >= 0; i--) { |
| 160 | if (codec_mask & (1 << i)) |
| 161 | codec_init(base, i); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void hda_init(struct device *dev) |
| 166 | { |
| 167 | u8 byte; |
| 168 | u32 dword; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 169 | void *base; |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 170 | struct resource *res; |
| 171 | u32 codec_mask; |
| 172 | device_t sm_dev; |
| 173 | |
| 174 | /* Enable azalia - PM_io 0x59[3], no ac97 in sb700. */ |
| 175 | byte = pm_ioread(0x59); |
| 176 | byte |= 1 << 3; |
| 177 | pm_iowrite(0x59, byte); |
| 178 | |
| 179 | /* Find the SMBus */ |
| 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)); |
| 183 | |
| 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); |
| 189 | byte &= ~0x7; |
| 190 | byte |= 0x0; /* INTA:0x0 - INTH:0x7 */ |
| 191 | pci_write_config8(sm_dev, 0x63, byte); |
| 192 | |
| 193 | /* Program the 2C to 0x437b1002 */ |
| 194 | dword = 0x437b1002; |
| 195 | pci_write_config32(dev, 0x2c, dword); |
| 196 | |
| 197 | /* Read in BAR */ |
| 198 | /* Is this right? HDA allows for a 64-bit BAR |
| 199 | * but this is only setup for a 32-bit one |
| 200 | */ |
| 201 | res = find_resource(dev, 0x10); |
| 202 | if (!res) |
| 203 | return; |
| 204 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 205 | base = res2mmio(res, 0, 0); |
| 206 | printk(BIOS_DEBUG, "base = 0x%p\n", base); |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 207 | codec_mask = codec_detect(base); |
| 208 | |
| 209 | if (codec_mask) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 210 | printk(BIOS_DEBUG, "codec_mask = %02x\n", codec_mask); |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 211 | codecs_init(base, codec_mask); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | static struct pci_operations lops_pci = { |
| 216 | .set_subsystem = pci_dev_set_subsystem, |
| 217 | }; |
| 218 | |
| 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, |
| 223 | .init = hda_init, |
| 224 | .scan_bus = 0, |
| 225 | .ops_pci = &lops_pci, |
| 226 | }; |
| 227 | |
Stefan Reinauer | 8e96ba2 | 2010-03-16 23:33:29 +0000 | [diff] [blame] | 228 | static const struct pci_driver hdaaudio_driver __pci_driver = { |
Zheng Bao | eff2ffd | 2010-03-16 01:38:54 +0000 | [diff] [blame] | 229 | .ops = &hda_audio_ops, |
| 230 | .vendor = PCI_VENDOR_ID_ATI, |
| 231 | .device = PCI_DEVICE_ID_ATI_SB700_HDA, |
| 232 | }; |