blob: 702a8370aeac2f2226f94daa7b2d629be1f75bf2 [file] [log] [blame]
Zheng Baoeff2ffd2010-03-16 01:38:54 +00001/*
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 Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Zheng Baoeff2ffd2010-03-16 01:38:54 +000018 */
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 Wuae8d0692013-08-02 19:29:17 +080030#define HDA_ICII_BUSY (1 << 0)
31#define HDA_ICII_VALID (1 << 1)
Zheng Baoeff2ffd2010-03-16 01:38:54 +000032
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080033static int set_bits(void *port, u32 mask, u32 val)
Zheng Baoeff2ffd2010-03-16 01:38:54 +000034{
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 Rothdcf253c2014-12-16 20:51:31 -070056 /* Timeout occurred */
Zheng Baoeff2ffd2010-03-16 01:38:54 +000057 if (!count)
58 return -1;
59 return 0;
60}
61
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080062static u32 codec_detect(void *base)
Zheng Baoeff2ffd2010-03-16 01:38:54 +000063{
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
85no_codec:
86 /* Codec Not found */
87 /* Put HDA back in reset (BAR + 0x8) [0] */
88 set_bits(base + 0x08, 1, 0);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000089 printk(BIOS_DEBUG, "No codec!\n");
Zheng Baoeff2ffd2010-03-16 01:38:54 +000090 return 0;
91}
92
93/**
Zheng Bao2a5101a2010-10-10 15:18:53 +000094 * Wait 50usec for the codec to indicate it is ready
Zheng Baoeff2ffd2010-03-16 01:38:54 +000095 * no response would imply that the codec is non-operative
96 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080097static int wait_for_ready(void *base)
Zheng Baoeff2ffd2010-03-16 01:38:54 +000098{
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 Bao2a5101a2010-10-10 15:18:53 +0000115 * Wait 50usec for the codec to indicate that it accepted
Zheng Baoeff2ffd2010-03-16 01:38:54 +0000116 * the previous command. No response would imply that the code
117 * is non-operative
118 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800119static int wait_for_valid(void *base)
Zheng Baoeff2ffd2010-03-16 01:38:54 +0000120{
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 Wu9361daf2013-08-02 14:45:03 +0800133 return -1;
Zheng Baoeff2ffd2010-03-16 01:38:54 +0000134}
135
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800136static void codec_init(void *base, int addr)
Zheng Baoeff2ffd2010-03-16 01:38:54 +0000137{
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 Reinauerc02b4fc2010-03-22 11:42:32 +0000153 printk(BIOS_DEBUG, "%x(th) codec viddid: %08x\n", addr, dword);
Zheng Baoeff2ffd2010-03-16 01:38:54 +0000154}
155
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800156static void codecs_init(void *base, u32 codec_mask)
Zheng Baoeff2ffd2010-03-16 01:38:54 +0000157{
158 int i;
159 for (i = 2; i >= 0; i--) {
160 if (codec_mask & (1 << i))
161 codec_init(base, i);
162 }
163}
164
165static void hda_init(struct device *dev)
166{
167 u8 byte;
168 u32 dword;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800169 void *base;
Zheng Baoeff2ffd2010-03-16 01:38:54 +0000170 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 Herbertbde6d302014-12-24 18:43:20 -0800205 base = res2mmio(res, 0, 0);
206 printk(BIOS_DEBUG, "base = 0x%p\n", base);
Zheng Baoeff2ffd2010-03-16 01:38:54 +0000207 codec_mask = codec_detect(base);
208
209 if (codec_mask) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000210 printk(BIOS_DEBUG, "codec_mask = %02x\n", codec_mask);
Zheng Baoeff2ffd2010-03-16 01:38:54 +0000211 codecs_init(base, codec_mask);
212 }
213}
214
215static struct pci_operations lops_pci = {
216 .set_subsystem = pci_dev_set_subsystem,
217};
218
219static 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 Reinauer8e96ba22010-03-16 23:33:29 +0000228static const struct pci_driver hdaaudio_driver __pci_driver = {
Zheng Baoeff2ffd2010-03-16 01:38:54 +0000229 .ops = &hda_audio_ops,
230 .vendor = PCI_VENDOR_ID_ATI,
231 .device = PCI_DEVICE_ID_ATI_SB700_HDA,
232};