blob: ad78e833ddc02a0e5fb92ba96539a56f121f6c7e [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
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>
29#include "pch.h"
30
31#define HDA_ICII_REG 0x68
32#define HDA_ICII_BUSY (1 << 0)
33#define HDA_ICII_VALID (1 << 1)
34
35typedef struct southbridge_intel_bd82x6x_config config_t;
36
37static int set_bits(u32 port, u32 mask, u32 val)
38{
39 u32 reg32;
40 int count;
41
42 /* Write (val & mask) to port */
43 val &= mask;
44 reg32 = read32(port);
45 reg32 &= ~mask;
46 reg32 |= val;
47 write32(port, reg32);
48
49 /* Wait for readback of register to
50 * match what was just written to it
51 */
52 count = 50;
53 do {
54 /* Wait 1ms based on BKDG wait time */
55 mdelay(1);
56 reg32 = read32(port);
57 reg32 &= mask;
58 } while ((reg32 != val) && --count);
59
60 /* Timeout occurred */
61 if (!count)
62 return -1;
63 return 0;
64}
65
66static int codec_detect(u32 base)
67{
68 u8 reg8;
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 /* Write back the value once reset bit is set. */
75 write16(base + 0x0, read16(base + 0x0));
76
77 /* Read in Codec location (BAR + 0xe)[2..0]*/
78 reg8 = read8(base + 0xe);
79 reg8 &= 0x0f;
80 if (!reg8)
81 goto no_codec;
82
83 return reg8;
84
85no_codec:
86 /* Codec Not found */
87 /* Put HDA back in reset (BAR + 0x8) [0] */
88 set_bits(base + 0x08, 1, 0);
89 printk(BIOS_DEBUG, "Azalia: No codec!\n");
90 return 0;
91}
92
93const u32 * cim_verb_data = NULL;
94u32 cim_verb_data_size = 0;
95const u32 * pc_beep_verbs = NULL;
96u32 pc_beep_verbs_size = 0;
97
98static u32 find_verb(struct device *dev, u32 viddid, const u32 ** verb)
99{
100 int idx=0;
101
102 while (idx < (cim_verb_data_size / sizeof(u32))) {
103 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
104 if (cim_verb_data[idx] != viddid) {
105 idx += verb_size + 3; // skip verb + header
106 continue;
107 }
108 *verb = &cim_verb_data[idx+3];
109 return verb_size;
110 }
111
112 /* Not all codecs need to load another verb */
113 return 0;
114}
115
116/**
117 * Wait 50usec for the codec to indicate it is ready
118 * no response would imply that the codec is non-operative
119 */
120
121static int wait_for_ready(u32 base)
122{
123 /* Use a 50 usec timeout - the Linux kernel uses the
124 * same duration */
125
126 int timeout = 50;
127
128 while(timeout--) {
129 u32 reg32 = read32(base + HDA_ICII_REG);
130 if (!(reg32 & HDA_ICII_BUSY))
131 return 0;
132 udelay(1);
133 }
134
135 return -1;
136}
137
138/**
139 * Wait 50usec for the codec to indicate that it accepted
140 * the previous command. No response would imply that the code
141 * is non-operative
142 */
143
144static int wait_for_valid(u32 base)
145{
146 u32 reg32;
147
148 /* Send the verb to the codec */
149 reg32 = read32(base + HDA_ICII_REG);
150 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
151 write32(base + HDA_ICII_REG, reg32);
152
153 /* Use a 50 usec timeout - the Linux kernel uses the
154 * same duration */
155
156 int timeout = 50;
157 while(timeout--) {
158 reg32 = read32(base + HDA_ICII_REG);
159 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
160 HDA_ICII_VALID)
161 return 0;
162 udelay(1);
163 }
164
165 return -1;
166}
167
168static void codec_init(struct device *dev, u32 base, int addr)
169{
170 u32 reg32;
171 const u32 *verb;
172 u32 verb_size;
173 int i;
174
175 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
176
177 /* 1 */
178 if (wait_for_ready(base) == -1) {
179 printk(BIOS_DEBUG, " codec not ready.\n");
180 return;
181 }
182
183 reg32 = (addr << 28) | 0x000f0000;
184 write32(base + 0x60, reg32);
185
186 if (wait_for_valid(base) == -1) {
187 printk(BIOS_DEBUG, " codec not valid.\n");
188 return;
189 }
190
191 reg32 = read32(base + 0x64);
192
193 /* 2 */
194 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
195 verb_size = find_verb(dev, reg32, &verb);
196
197 if (!verb_size) {
198 printk(BIOS_DEBUG, "Azalia: No verb!\n");
199 return;
200 }
201 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
202
203 /* 3 */
204 for (i = 0; i < verb_size; i++) {
205 if (wait_for_ready(base) == -1)
206 return;
207
208 write32(base + 0x60, verb[i]);
209
210 if (wait_for_valid(base) == -1)
211 return;
212 }
213 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
214}
215
216static void codecs_init(struct device *dev, u32 base, u32 codec_mask)
217{
218 int i;
219 for (i = 3; i >= 0; i--) {
220 if (codec_mask & (1 << i))
221 codec_init(dev, base, i);
222 }
223
224 for (i = 0; i < pc_beep_verbs_size; i++) {
225 if (wait_for_ready(base) == -1)
226 return;
227
228 write32(base + 0x60, pc_beep_verbs[i]);
229
230 if (wait_for_valid(base) == -1)
231 return;
232 }
233}
234
235static void azalia_init(struct device *dev)
236{
237 u32 base;
238 struct resource *res;
239 u32 codec_mask;
240 u8 reg8;
241 u16 reg16;
242 u32 reg32;
243
244 /* Find base address */
245 res = find_resource(dev, PCI_BASE_ADDRESS_0);
246 if (!res)
247 return;
248
249 // NOTE this will break as soon as the Azalia get's a bar above
250 // 4G. Is there anything we can do about it?
251 base = (u32)res->base;
252 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
253
254 if (RCBA32(0x2030) & (1 << 31)) {
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300255 reg32 = pci_read_config32(dev, 0x120);
Aaron Durbin76c37002012-10-30 09:03:43 -0500256 reg32 &= 0xf8ffff01;
257 reg32 |= (1 << 24); // 25 for server
258 reg32 |= RCBA32(0x2030) & 0xfe;
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300259 pci_write_config32(dev, 0x120, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500260
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300261 reg16 = pci_read_config16(dev, 0x78);
Aaron Durbin76c37002012-10-30 09:03:43 -0500262 reg16 &= ~(1 << 11);
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300263 pci_write_config16(dev, 0x78, reg16);
Aaron Durbin76c37002012-10-30 09:03:43 -0500264 } else
265 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
266
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300267 reg32 = pci_read_config32(dev, 0x114);
Aaron Durbin76c37002012-10-30 09:03:43 -0500268 reg32 &= ~0xfe;
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300269 pci_write_config32(dev, 0x114, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500270
271 // Set VCi enable bit
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300272 if (pci_read_config32(dev, 0x120) & ((1 << 24) |
Aaron Durbin76c37002012-10-30 09:03:43 -0500273 (1 << 25) | (1 << 26))) {
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300274 reg32 = pci_read_config32(dev, 0x120);
Aaron Durbin76c37002012-10-30 09:03:43 -0500275 reg32 |= (1 << 31);
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300276 pci_write_config32(dev, 0x120, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500277 }
278
279 // Enable HDMI codec:
280 reg32 = pci_read_config32(dev, 0xc4);
281 reg32 |= (1 << 1);
282 pci_write_config32(dev, 0xc4, reg32);
283
284 reg8 = pci_read_config8(dev, 0x43);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800285 reg8 |= (1 << 5) | (1 << 6) | (1 << 2) | (1 << 1) | (1 << 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500286 pci_write_config8(dev, 0x43, reg8);
287
288 /* Additional programming steps */
289 reg32 = pci_read_config32(dev, 0xc4);
290 reg32 |= (1 << 13) | (1 << 10);
291 pci_write_config32(dev, 0xc4, reg32);
292
293 reg32 = pci_read_config32(dev, 0xd0);
294 reg32 &= ~(1 << 31);
295 pci_write_config32(dev, 0xd0, reg32);
296
297 /* Additional programming steps */
298 reg32 = pci_read_config32(dev, 0xc4);
299 reg32 |= (1 << 13);
300 pci_write_config32(dev, 0xc4, reg32);
301
302 reg32 = pci_read_config32(dev, 0xc4);
303 reg32 |= (1 << 10);
304 pci_write_config32(dev, 0xc4, reg32);
305
306 reg32 = pci_read_config32(dev, 0xd0);
307 reg32 &= ~(1 << 31);
308 pci_write_config32(dev, 0xd0, reg32);
309
310 /* Set Bus Master */
311 reg32 = pci_read_config32(dev, PCI_COMMAND);
312 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
313
314 pci_write_config8(dev, 0x3c, 0x0a); // unused?
315
316 /* Codec Initialization Programming Sequence */
317 reg32 = read32(base + 0x08);
318 reg32 |= (1 << 0);
319 write32(base + 0x08, reg32);
320
321 //
322 reg8 = pci_read_config8(dev, 0x40); // Audio Control
323 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
324 pci_write_config8(dev, 0x40, reg8);
325
326 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
327 reg8 &= ~(1 << 7); // Docking not supported
328 pci_write_config8(dev, 0x4d, reg8);
329
330 codec_mask = codec_detect(base);
331
332 if (codec_mask) {
333 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
334 codecs_init(dev, base, codec_mask);
335 }
336
337 /* Enable dynamic clock gating */
338 reg8 = pci_read_config8(dev, 0x43);
339 reg8 &= ~0x7;
340 reg8 |= (1 << 2) | (1 << 0);
341 pci_write_config8(dev, 0x43, reg8);
342}
343
344static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device)
345{
346 if (!vendor || !device) {
347 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
348 pci_read_config32(dev, PCI_VENDOR_ID));
349 } else {
350 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
351 ((device & 0xffff) << 16) | (vendor & 0xffff));
352 }
353}
354
355static struct pci_operations azalia_pci_ops = {
356 .set_subsystem = azalia_set_subsystem,
357};
358
359static struct device_operations azalia_ops = {
360 .read_resources = pci_dev_read_resources,
361 .set_resources = pci_dev_set_resources,
362 .enable_resources = pci_dev_enable_resources,
363 .init = azalia_init,
364 .scan_bus = 0,
365 .ops_pci = &azalia_pci_ops,
366};
367
368static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 };
369
370static const struct pci_driver pch_azalia __pci_driver = {
371 .ops = &azalia_ops,
372 .vendor = PCI_VENDOR_ID_INTEL,
373 .devices = pci_device_ids,
374};
375