blob: 314a1b1d1970ed13b34ded1628c74f0c2d9f19c9 [file] [log] [blame]
Vladimir Serbinenko888d5592013-11-13 17:53:38 +01001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Copyright (C) 2008-2009 coresystems GmbH
6 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <console/console.h>
23#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_ids.h>
26#include <device/pci_ops.h>
27#include <arch/io.h>
28#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020029#include <device/azalia_device.h>
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010030#include "pch.h"
31
32#define HDA_ICII_REG 0x68
33#define HDA_ICII_BUSY (1 << 0)
34#define HDA_ICII_VALID (1 << 1)
35
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010036static int set_bits(u32 port, u32 mask, u32 val)
37{
38 u32 reg32;
39 int count;
40
41 /* Write (val & mask) to port */
42 val &= mask;
43 reg32 = read32(port);
44 reg32 &= ~mask;
45 reg32 |= val;
46 write32(port, reg32);
47
48 /* Wait for readback of register to
49 * match what was just written to it
50 */
51 count = 50;
52 do {
53 /* Wait 1ms based on BKDG wait time */
54 mdelay(1);
55 reg32 = read32(port);
56 reg32 &= mask;
57 } while ((reg32 != val) && --count);
58
59 /* Timeout occurred */
60 if (!count)
61 return -1;
62 return 0;
63}
64
65static int codec_detect(u32 base)
66{
67 u8 reg8;
68
69 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
70 if (set_bits(base + 0x08, 1, 1) == -1)
71 goto no_codec;
72
73 /* Write back the value once reset bit is set. */
74 write16(base + 0x0, read16(base + 0x0));
75
76 /* Read in Codec location (BAR + 0xe)[2..0]*/
77 reg8 = read8(base + 0xe);
78 reg8 &= 0x0f;
79 if (!reg8)
80 goto no_codec;
81
82 return reg8;
83
84no_codec:
85 /* Codec Not found */
86 /* Put HDA back in reset (BAR + 0x8) [0] */
87 set_bits(base + 0x08, 1, 0);
88 printk(BIOS_DEBUG, "Azalia: No codec!\n");
89 return 0;
90}
91
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010092static u32 find_verb(struct device *dev, u32 viddid, const u32 ** verb)
93{
94 int idx=0;
95
96 while (idx < (cim_verb_data_size / sizeof(u32))) {
97 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
98 if (cim_verb_data[idx] != viddid) {
99 idx += verb_size + 3; // skip verb + header
100 continue;
101 }
102 *verb = &cim_verb_data[idx+3];
103 return verb_size;
104 }
105
106 /* Not all codecs need to load another verb */
107 return 0;
108}
109
110/**
111 * Wait 50usec for the codec to indicate it is ready
112 * no response would imply that the codec is non-operative
113 */
114
115static int wait_for_ready(u32 base)
116{
117 /* Use a 1msec timeout */
118
119 int timeout = 1000;
120
121 while(timeout--) {
122 u32 reg32 = read32(base + HDA_ICII_REG);
123 if (!(reg32 & HDA_ICII_BUSY))
124 return 0;
125 udelay(1);
126 }
127
128 return -1;
129}
130
131/**
132 * Wait 50usec for the codec to indicate that it accepted
133 * the previous command. No response would imply that the code
134 * is non-operative
135 */
136
137static int wait_for_valid(u32 base)
138{
139 u32 reg32;
140
141 /* Send the verb to the codec */
142 reg32 = read32(base + HDA_ICII_REG);
143 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
144 write32(base + HDA_ICII_REG, reg32);
145
146 /* Use a 1msec timeout */
147
148 int timeout = 1000;
149 while(timeout--) {
150 reg32 = read32(base + HDA_ICII_REG);
151 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
152 HDA_ICII_VALID)
153 return 0;
154 udelay(1);
155 }
156
157 return -1;
158}
159
160static void codec_init(struct device *dev, u32 base, int addr)
161{
162 u32 reg32;
163 const u32 *verb;
164 u32 verb_size;
165 int i;
166
167 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
168
169 /* 1 */
170 if (wait_for_ready(base) == -1) {
171 printk(BIOS_DEBUG, " codec not ready.\n");
172 return;
173 }
174
175 reg32 = (addr << 28) | 0x000f0000;
176 write32(base + 0x60, reg32);
177
178 if (wait_for_valid(base) == -1) {
179 printk(BIOS_DEBUG, " codec not valid.\n");
180 return;
181 }
182
183 reg32 = read32(base + 0x64);
184
185 /* 2 */
186 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
187 verb_size = find_verb(dev, reg32, &verb);
188
189 if (!verb_size) {
190 printk(BIOS_DEBUG, "Azalia: No verb!\n");
191 return;
192 }
193 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
194
195 /* 3 */
196 for (i = 0; i < verb_size; i++) {
197 if (wait_for_ready(base) == -1)
198 return;
199
200 write32(base + 0x60, verb[i]);
201
202 if (wait_for_valid(base) == -1)
203 return;
204 }
205 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
206}
207
208static void codecs_init(struct device *dev, u32 base, u32 codec_mask)
209{
210 int i;
211 for (i = 3; i >= 0; i--) {
212 if (codec_mask & (1 << i))
213 codec_init(dev, base, i);
214 }
215
216 for (i = 0; i < pc_beep_verbs_size; i++) {
217 if (wait_for_ready(base) == -1)
218 return;
219
220 write32(base + 0x60, pc_beep_verbs[i]);
221
222 if (wait_for_valid(base) == -1)
223 return;
224 }
225}
226
227static void azalia_init(struct device *dev)
228{
229 u32 base;
230 struct resource *res;
231 u32 codec_mask;
232 u8 reg8;
233 u16 reg16;
234 u32 reg32;
235
236 /* Find base address */
237 res = find_resource(dev, PCI_BASE_ADDRESS_0);
238 if (!res)
239 return;
240
241 // NOTE this will break as soon as the Azalia get's a bar above
242 // 4G. Is there anything we can do about it?
243 base = (u32)res->base;
244 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
245
246 if (RCBA32(0x2030) & (1 << 31)) {
247 reg32 = pci_read_config32(dev, 0x120);
248 reg32 &= 0xf8ffff01;
249 reg32 |= (1 << 24); // 2 << 24 for server
250 reg32 |= RCBA32(0x2030) & 0xfe;
251 pci_write_config32(dev, 0x120, reg32);
252
253 reg16 = pci_read_config16(dev, 0x78);
254 reg16 |= (1 << 11);
255 pci_write_config16(dev, 0x78, reg16);
256 } else
257 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
258
259 reg32 = pci_read_config32(dev, 0x114);
260 reg32 &= ~0xfe;
261 pci_write_config32(dev, 0x114, reg32);
262
263 // Set VCi enable bit
264 reg32 = pci_read_config32(dev, 0x120);
265 reg32 |= (1 << 31);
266 pci_write_config32(dev, 0x120, reg32);
267
268 // Enable HDMI codec:
269 reg32 = pci_read_config32(dev, 0xc4);
270 reg32 |= (1 << 1);
271 pci_write_config32(dev, 0xc4, reg32);
272
273 reg8 = pci_read_config8(dev, 0x43);
274 reg8 |= (1 << 6);
275 pci_write_config8(dev, 0x43, reg8);
276
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100277 reg32 = pci_read_config32(dev, 0xd0);
278 reg32 &= ~(1 << 31);
279 pci_write_config32(dev, 0xd0, reg32);
280
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100281 /* Set Bus Master */
282 reg32 = pci_read_config32(dev, PCI_COMMAND);
283 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
284
285 pci_write_config8(dev, 0x3c, 0x0a); // unused?
286
287 /* Codec Initialization Programming Sequence */
288
289 /* Take controller out of reset */
290 reg32 = read32(base + 0x08);
291 reg32 |= (1 << 0);
292 write32(base + 0x08, reg32);
293 /* Wait 1ms */
294 udelay(1000);
295
296 //
297 reg8 = pci_read_config8(dev, 0x40); // Audio Control
298 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
299 pci_write_config8(dev, 0x40, reg8);
300
301 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
302 reg8 &= ~(1 << 7); // Docking not supported
303 pci_write_config8(dev, 0x4d, reg8);
304
305 codec_mask = codec_detect(base);
306
307 if (codec_mask) {
308 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
309 codecs_init(dev, base, codec_mask);
310 }
311
312 /* Enable dynamic clock gating */
313 reg8 = pci_read_config8(dev, 0x43);
314 reg8 &= ~0x7;
315 reg8 |= (1 << 2) | (1 << 0);
316 pci_write_config8(dev, 0x43, reg8);
317}
318
319static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device)
320{
321 if (!vendor || !device) {
322 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
323 pci_read_config32(dev, PCI_VENDOR_ID));
324 } else {
325 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
326 ((device & 0xffff) << 16) | (vendor & 0xffff));
327 }
328}
329
330static struct pci_operations azalia_pci_ops = {
331 .set_subsystem = azalia_set_subsystem,
332};
333
334static struct device_operations azalia_ops = {
335 .read_resources = pci_dev_read_resources,
336 .set_resources = pci_dev_set_resources,
337 .enable_resources = pci_dev_enable_resources,
338 .init = azalia_init,
339 .scan_bus = 0,
340 .ops_pci = &azalia_pci_ops,
341};
342
343static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0x3b56, 0 };
344
345static const struct pci_driver pch_azalia __pci_driver = {
346 .ops = &azalia_ops,
347 .vendor = PCI_VENDOR_ID_INTEL,
348 .devices = pci_device_ids,
349};