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