blob: c75eee7d3b5f2efbf074224e44b4afa7ae052ddc [file] [log] [blame]
Arthur Heymans7b9c1392017-04-09 20:40:39 +02001/*
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) 2012 secunet Security Networks AG
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
18#include <console/console.h>
19#include <device/device.h>
20#include <device/pci.h>
21#include <device/pci_ids.h>
22#include <device/pci_ops.h>
23#include <arch/io.h>
24#include <delay.h>
25#include <device/azalia_device.h>
Arthur Heymans349e0852017-04-09 20:48:37 +020026#include "i82801jx.h"
Arthur Heymans7b9c1392017-04-09 20:40:39 +020027
28#define HDA_ICII_REG 0x68
29#define HDA_ICII_BUSY (1 << 0)
30#define HDA_ICII_VALID (1 << 1)
31
Arthur Heymans349e0852017-04-09 20:48:37 +020032typedef struct southbridge_intel_i82801jx_config config_t;
Arthur Heymans7b9c1392017-04-09 20:40:39 +020033
34static int set_bits(void *port, u32 mask, u32 val)
35{
36 u32 reg32;
37 int count;
38
39 /* Write (val & mask) to port */
40 val &= mask;
41 reg32 = read32(port);
42 reg32 &= ~mask;
43 reg32 |= val;
44 write32(port, reg32);
45
46 /* Wait for readback of register to
47 * match what was just written to it
48 */
49 count = 50;
50 do {
51 /* Wait 1ms based on BKDG wait time */
52 mdelay(1);
53 reg32 = read32(port);
54 reg32 &= mask;
55 } while ((reg32 != val) && --count);
56
57 /* Timeout occurred */
58 if (!count)
59 return -1;
60 return 0;
61}
62
63static int codec_detect(u8 *base)
64{
65 u32 reg32;
66
67 /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */
68 if (set_bits(base + 0x08, 1, 0) == -1)
69 goto no_codec;
70
71 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
72 if (set_bits(base + 0x08, 1, 1) == -1)
73 goto no_codec;
74
75 /* Read in Codec location (BAR + 0xe)[2..0]*/
76 reg32 = read32(base + 0xe);
77 reg32 &= 0x0f;
78 if (!reg32)
79 goto no_codec;
80
81 return reg32;
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
91static u32 find_verb(struct device *dev, u32 viddid, const u32 ** verb)
92{
93 int idx=0;
94
95 while (idx < (cim_verb_data_size / sizeof(u32))) {
96 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
97 if (cim_verb_data[idx] != viddid) {
98 idx += verb_size + 3; // skip verb + header
99 continue;
100 }
101 *verb = &cim_verb_data[idx+3];
102 return verb_size;
103 }
104
105 /* Not all codecs need to load another verb */
106 return 0;
107}
108
109/**
110 * Wait 50usec for the codec to indicate it is ready
111 * no response would imply that the codec is non-operative
112 */
113
114static int wait_for_ready(u8 *base)
115{
116 /* Use a 50 usec timeout - the Linux kernel uses the
117 * same duration */
118
119 int timeout = 50;
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(u8 *base)
138{
139 u32 reg32;
140
141 /* Send the verb to the codec */
142 reg32 = read32(base + 0x68);
143 reg32 |= (1 << 0) | (1 << 1);
144 write32(base + 0x68, reg32);
145
146 /* Use a 50 usec timeout - the Linux kernel uses the
147 * same duration */
148
149 int timeout = 50;
150 while (timeout--) {
151 reg32 = read32(base + HDA_ICII_REG);
152 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
153 HDA_ICII_VALID)
154 return 0;
155 udelay(1);
156 }
157
158 return -1;
159}
160
161static void codec_init(struct device *dev, u8 *base, int addr)
162{
163 u32 reg32;
164 const u32 *verb;
165 u32 verb_size;
166 int i;
167
168 printk(BIOS_DEBUG, "HD Audio: Initializing codec #%d\n", addr);
169
170 /* 1 */
171 if (wait_for_ready(base) == -1)
172 return;
173
174 reg32 = (addr << 28) | 0x000f0000;
175 write32(base + 0x60, reg32);
176
177 if (wait_for_valid(base) == -1)
178 return;
179
180 reg32 = read32(base + 0x64);
181
182 /* 2 */
183 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
184 verb_size = find_verb(dev, reg32, &verb);
185
186 if (!verb_size) {
187 printk(BIOS_DEBUG, "Azalia: No verb!\n");
188 return;
189 }
190 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
191
192 /* 3 */
193 for (i = 0; i < verb_size; i++) {
194 if (wait_for_ready(base) == -1)
195 return;
196
197 write32(base + 0x60, verb[i]);
198
199 if (wait_for_valid(base) == -1)
200 return;
201 }
202 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
203}
204
205static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
206{
207 int i;
208 for (i = 2; i >= 0; i--) {
209 if (codec_mask & (1 << i))
210 codec_init(dev, base, i);
211 }
212
213 for (i = 0; i < pc_beep_verbs_size; i++) {
214 if (wait_for_ready(base) == -1)
215 return;
216
217 write32(base + 0x60, pc_beep_verbs[i]);
218
219 if (wait_for_valid(base) == -1)
220 return;
221 }
222}
223
224static void azalia_init(struct device *dev)
225{
226 u8 *base;
227 struct resource *res;
228 u32 codec_mask;
229 u8 reg8;
230 u32 reg32;
231
232 // ESD
233 reg32 = pci_read_config32(dev, 0x134);
234 reg32 &= 0xff00ffff;
235 reg32 |= (2 << 16);
236 pci_write_config32(dev, 0x134, reg32);
237
238 // Link1 description
239 reg32 = pci_read_config32(dev, 0x140);
240 reg32 &= 0xff00ffff;
241 reg32 |= (2 << 16);
242 pci_write_config32(dev, 0x140, reg32);
243
244 // Port VC0 Resource Control Register
245 reg32 = pci_read_config32(dev, 0x114);
246 reg32 &= 0xffffff00;
247 reg32 |= 1;
248 pci_write_config32(dev, 0x114, reg32);
249
250 // VCi traffic class
251 reg8 = pci_read_config8(dev, 0x44);
252 reg8 |= (7 << 0); // TC7
253 pci_write_config8(dev, 0x44, reg8);
254
255 // VCi Resource Control
256 reg32 = pci_read_config32(dev, 0x120);
257 reg32 |= (1 << 31);
258 reg32 |= (1 << 24); // VCi ID
259 reg32 |= (0x80 << 0); // VCi map
260 pci_write_config32(dev, 0x120, reg32);
261
262 /* Set Bus Master */
263 reg32 = pci_read_config32(dev, PCI_COMMAND);
264 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
265
266 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
267 reg8 &= ~(1 << 7); // Docking not supported
268 pci_write_config8(dev, 0x4d, reg8);
269
270 /* Lock some R/WO bits by writing their current value. */
271 reg32 = pci_read_config32(dev, 0x74);
272 pci_write_config32(dev, 0x74, reg32);
273
274 res = find_resource(dev, 0x10);
275 if (!res)
276 return;
277
278 // NOTE this will break as soon as the Azalia get's a bar above
279 // 4G. Is there anything we can do about it?
280 base = res2mmio(res, 0, 0);
281 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
282 codec_mask = codec_detect(base);
283
284 if (codec_mask) {
285 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
286 codecs_init(dev, base, codec_mask);
287 }
288}
289
290static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device)
291{
292 if (!vendor || !device) {
293 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
294 pci_read_config32(dev, PCI_VENDOR_ID));
295 } else {
296 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
297 ((device & 0xffff) << 16) | (vendor & 0xffff));
298 }
299}
300
301static struct pci_operations azalia_pci_ops = {
302 .set_subsystem = azalia_set_subsystem,
303};
304
305static struct device_operations azalia_ops = {
306 .read_resources = pci_dev_read_resources,
307 .set_resources = pci_dev_set_resources,
308 .enable_resources = pci_dev_enable_resources,
309 .init = azalia_init,
310 .scan_bus = 0,
311 .ops_pci = &azalia_pci_ops,
312};
313
Arthur Heymans349e0852017-04-09 20:48:37 +0200314static const unsigned short pci_device_ids[] = {
315 0x3a3e,
316 0x3a6e,
317 0
318};
319
320static const struct pci_driver i82801jx_azalia __pci_driver = {
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200321 .ops = &azalia_ops,
322 .vendor = PCI_VENDOR_ID_INTEL,
Arthur Heymans349e0852017-04-09 20:48:37 +0200323 .devices = pci_device_ids,
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200324};