blob: 8a1116a2d6f32a5c78a989ff109fbb1139426641 [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
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 1msec timeout */
124
125 int timeout = 1000;
126
127 while(timeout--) {
128 u32 reg32 = read32(base + HDA_ICII_REG);
129 if (!(reg32 & HDA_ICII_BUSY))
130 return 0;
131 udelay(1);
132 }
133
134 return -1;
135}
136
137/**
138 * Wait 50usec for the codec to indicate that it accepted
139 * the previous command. No response would imply that the code
140 * is non-operative
141 */
142
143static int wait_for_valid(u32 base)
144{
145 u32 reg32;
146
147 /* Send the verb to the codec */
148 reg32 = read32(base + HDA_ICII_REG);
149 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
150 write32(base + HDA_ICII_REG, reg32);
151
152 /* Use a 1msec timeout */
153
154 int timeout = 1000;
155 while(timeout--) {
156 reg32 = read32(base + HDA_ICII_REG);
157 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
158 HDA_ICII_VALID)
159 return 0;
160 udelay(1);
161 }
162
163 return -1;
164}
165
166static void codec_init(struct device *dev, u32 base, int addr)
167{
168 u32 reg32;
169 const u32 *verb;
170 u32 verb_size;
171 int i;
172
173 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
174
175 /* 1 */
176 if (wait_for_ready(base) == -1) {
177 printk(BIOS_DEBUG, " codec not ready.\n");
178 return;
179 }
180
181 reg32 = (addr << 28) | 0x000f0000;
182 write32(base + 0x60, reg32);
183
184 if (wait_for_valid(base) == -1) {
185 printk(BIOS_DEBUG, " codec not valid.\n");
186 return;
187 }
188
189 reg32 = read32(base + 0x64);
190
191 /* 2 */
192 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
193 verb_size = find_verb(dev, reg32, &verb);
194
195 if (!verb_size) {
196 printk(BIOS_DEBUG, "Azalia: No verb!\n");
197 return;
198 }
199 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
200
201 /* 3 */
202 for (i = 0; i < verb_size; i++) {
203 if (wait_for_ready(base) == -1)
204 return;
205
206 write32(base + 0x60, verb[i]);
207
208 if (wait_for_valid(base) == -1)
209 return;
210 }
211 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
212}
213
214static void codecs_init(struct device *dev, u32 base, u32 codec_mask)
215{
216 int i;
217 for (i = 3; i >= 0; i--) {
218 if (codec_mask & (1 << i))
219 codec_init(dev, base, i);
220 }
221
222 for (i = 0; i < pc_beep_verbs_size; i++) {
223 if (wait_for_ready(base) == -1)
224 return;
225
226 write32(base + 0x60, pc_beep_verbs[i]);
227
228 if (wait_for_valid(base) == -1)
229 return;
230 }
231}
232
233static void azalia_init(struct device *dev)
234{
235 u32 base;
236 struct resource *res;
237 u32 codec_mask;
238 u8 reg8;
239 u16 reg16;
240 u32 reg32;
241
242 /* Find base address */
243 res = find_resource(dev, PCI_BASE_ADDRESS_0);
244 if (!res)
245 return;
246
247 // NOTE this will break as soon as the Azalia get's a bar above
248 // 4G. Is there anything we can do about it?
249 base = (u32)res->base;
250 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
251
252 if (RCBA32(0x2030) & (1 << 31)) {
253 reg32 = pci_read_config32(dev, 0x120);
254 reg32 &= 0xf8ffff01;
255 reg32 |= (1 << 24); // 2 << 24 for server
256 reg32 |= RCBA32(0x2030) & 0xfe;
257 pci_write_config32(dev, 0x120, reg32);
258
259 reg16 = pci_read_config16(dev, 0x78);
260 reg16 |= (1 << 11);
261 pci_write_config16(dev, 0x78, reg16);
262 } else
263 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
264
265 reg32 = pci_read_config32(dev, 0x114);
266 reg32 &= ~0xfe;
267 pci_write_config32(dev, 0x114, reg32);
268
269 // Set VCi enable bit
270 reg32 = pci_read_config32(dev, 0x120);
271 reg32 |= (1 << 31);
272 pci_write_config32(dev, 0x120, reg32);
273
274 // Enable HDMI codec:
275 reg32 = pci_read_config32(dev, 0xc4);
276 reg32 |= (1 << 1);
277 pci_write_config32(dev, 0xc4, reg32);
278
279 reg8 = pci_read_config8(dev, 0x43);
280 reg8 |= (1 << 6);
281 pci_write_config8(dev, 0x43, reg8);
282
283 /* Additional programming steps */
284 reg32 = pci_read_config32(dev, 0xc4);
285 reg32 |= (1 << 13);
286 pci_write_config32(dev, 0xc4, reg32);
287
288 reg32 = pci_read_config32(dev, 0xc4);
289 reg32 |= (1 << 10);
290 pci_write_config32(dev, 0xc4, reg32);
291
292 reg32 = pci_read_config32(dev, 0xd0);
293 reg32 &= ~(1 << 31);
294 pci_write_config32(dev, 0xd0, reg32);
295
296 if (dev->device == 0x1e20) {
297 /* Additional step on Panther Point */
298 reg32 = pci_read_config32(dev, 0xc4);
299 reg32 |= (1 << 17);
300 pci_write_config32(dev, 0xc4, reg32);
301 }
302
303 /* Set Bus Master */
304 reg32 = pci_read_config32(dev, PCI_COMMAND);
305 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
306
307 pci_write_config8(dev, 0x3c, 0x0a); // unused?
308
309 /* Codec Initialization Programming Sequence */
310
311 /* Take controller out of reset */
312 reg32 = read32(base + 0x08);
313 reg32 |= (1 << 0);
314 write32(base + 0x08, reg32);
315 /* Wait 1ms */
316 udelay(1000);
317
318 //
319 reg8 = pci_read_config8(dev, 0x40); // Audio Control
320 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
321 pci_write_config8(dev, 0x40, reg8);
322
323 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
324 reg8 &= ~(1 << 7); // Docking not supported
325 pci_write_config8(dev, 0x4d, reg8);
326
327 codec_mask = codec_detect(base);
328
329 if (codec_mask) {
330 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
331 codecs_init(dev, base, codec_mask);
332 }
333
334 /* Enable dynamic clock gating */
335 reg8 = pci_read_config8(dev, 0x43);
336 reg8 &= ~0x7;
337 reg8 |= (1 << 2) | (1 << 0);
338 pci_write_config8(dev, 0x43, reg8);
339}
340
341static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device)
342{
343 if (!vendor || !device) {
344 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
345 pci_read_config32(dev, PCI_VENDOR_ID));
346 } else {
347 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
348 ((device & 0xffff) << 16) | (vendor & 0xffff));
349 }
350}
351
352static struct pci_operations azalia_pci_ops = {
353 .set_subsystem = azalia_set_subsystem,
354};
355
356static struct device_operations azalia_ops = {
357 .read_resources = pci_dev_read_resources,
358 .set_resources = pci_dev_set_resources,
359 .enable_resources = pci_dev_enable_resources,
360 .init = azalia_init,
361 .scan_bus = 0,
362 .ops_pci = &azalia_pci_ops,
363};
364
365static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0x3b56, 0 };
366
367static const struct pci_driver pch_azalia __pci_driver = {
368 .ops = &azalia_ops,
369 .vendor = PCI_VENDOR_ID_INTEL,
370 .devices = pci_device_ids,
371};
372