Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / southbridge / intel / i82801gx / i82801gx_azalia.c
1 /*
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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <console/console.h>
22 #include <device/device.h>
23 #include <device/pci.h>
24 #include <device/pci_ids.h>
25 #include <device/pci_ops.h>
26 #include <arch/io.h>
27 #include <delay.h>
28 #include "i82801gx.h"
29
30 #define HDA_ICII_REG 0x68
31 #define   HDA_ICII_BUSY (1 << 0)
32 #define   HDA_ICII_VALID  (1 << 1)
33
34 typedef struct southbridge_intel_i82801gx_config config_t;
35
36 static 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 occured */
60         if (!count)
61                 return -1;
62         return 0;
63 }
64
65 static int codec_detect(u32 base)
66 {
67         u32 reg32;
68
69         /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */
70         if (set_bits(base + 0x08, 1, 0) == -1)
71                 goto no_codec;
72
73         /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
74         if (set_bits(base + 0x08, 1, 1) == -1)
75                 goto no_codec;
76
77         /* Read in Codec location (BAR + 0xe)[2..0]*/
78         reg32 = read32(base + 0xe);
79         reg32 &= 0x0f;
80         if (!reg32)
81                 goto no_codec;
82
83         return reg32;
84
85 no_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
93 u32 * cim_verb_data = NULL;
94 u32 cim_verb_data_size = 0;
95
96 static u32 find_verb(struct device *dev, u32 viddid, 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 for the codec to indicate it is ready
116  *  no response would imply that the codec is non-operative
117  */
118
119 static int wait_for_ready(u32 base)
120 {
121         /* Use a 50 usec timeout - the Linux kernel uses the
122          * same duration */
123
124         int timeout = 50;
125
126         while(timeout--) {
127                 u32 reg32 = read32(base +  HDA_ICII_REG);
128                 if (!(reg32 & HDA_ICII_BUSY))
129                         return 0;
130                 udelay(1);
131         }
132
133         return -1;
134 }
135
136 /**
137  *  Wait 50usec for for the codec to indicate that it accepted
138  *  the previous command.  No response would imply that the code
139  *  is non-operative
140  */
141
142 static int wait_for_valid(u32 base)
143 {
144         u32 reg32;
145
146         /* Send the verb to the codec */
147         reg32 = read32(base + 0x68);
148         reg32 |= (1 << 0) | (1 << 1);
149         write32(base + 0x68, reg32);
150
151         /* Use a 50 usec timeout - the Linux kernel uses the
152          * same duration */
153
154         int timeout = 50;
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
166 static void codec_init(struct device *dev, u32 base, int addr)
167 {
168         u32 reg32;
169         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                 return;
178
179         reg32 = (addr << 28) | 0x000f0000;
180         write32(base + 0x60, reg32);
181
182         if (wait_for_valid(base) == -1)
183                 return;
184
185         reg32 = read32(base + 0x64);
186
187         /* 2 */
188         printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
189         verb_size = find_verb(dev, reg32, &verb);
190
191         if (!verb_size) {
192                 printk(BIOS_DEBUG, "Azalia: No verb!\n");
193                 return;
194         }
195         printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
196
197         /* 3 */
198         for (i = 0; i < verb_size; i++) {
199                 if (wait_for_ready(base) == -1)
200                         return;
201
202                 write32(base + 0x60, verb[i]);
203
204                 if (wait_for_valid(base) == -1)
205                         return;
206         }
207         printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
208 }
209
210 static void codecs_init(struct device *dev, u32 base, u32 codec_mask)
211 {
212         int i;
213         for (i = 2; i >= 0; i--) {
214                 if (codec_mask & (1 << i))
215                         codec_init(dev, base, i);
216         }
217 }
218
219 static void azalia_init(struct device *dev)
220 {
221         u32 base;
222         struct resource *res;
223         u32 codec_mask;
224         u8 reg8;
225         u32 reg32;
226
227 #if CONFIG_MMCONF_SUPPORT
228         // ESD
229         reg32 = pci_mmio_read_config32(dev, 0x134);
230         reg32 &= 0xff00ffff;
231         reg32 |= (2 << 16);
232         pci_mmio_write_config32(dev, 0x134, reg32);
233
234         // Link1 description
235         reg32 = pci_mmio_read_config32(dev, 0x140);
236         reg32 &= 0xff00ffff;
237         reg32 |= (2 << 16);
238         pci_mmio_write_config32(dev, 0x140, reg32);
239
240         // Port VC0 Resource Control Register
241         reg32 = pci_mmio_read_config32(dev, 0x114);
242         reg32 &= 0xffffff00;
243         reg32 |= 1;
244         pci_mmio_write_config32(dev, 0x114, reg32);
245
246         // VCi traffic class
247         reg8 = pci_mmio_read_config8(dev, 0x44);
248         reg8 |= (7 << 0); // TC7
249         pci_mmio_write_config8(dev, 0x44, reg8);
250
251         // VCi Resource Control
252         reg32 = pci_mmio_read_config32(dev, 0x120);
253         reg32 |= (1 << 31);
254         reg32 |= (1 << 24); // VCi ID
255         reg32 |= (0x80 << 0); // VCi map
256         pci_mmio_write_config32(dev, 0x120, reg32);
257 #else
258 #error ICH7 Azalia required CONFIG_MMCONF_SUPPORT
259 #endif
260
261         /* Set Bus Master */
262         reg32 = pci_read_config32(dev, PCI_COMMAND);
263         pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
264
265         pci_write_config8(dev, 0x3c, 0x0a); // unused?
266
267         // TODO Actually check if we're AC97 or HDA instead of hardcoding this
268         // here, in devicetree.cb and/or romstage.c.
269         reg8 = pci_read_config8(dev, 0x40);
270         reg8 |= (1 << 3); // Clear Clock Detect Bit
271         pci_write_config8(dev, 0x40, reg8);
272         reg8 &= ~(1 << 3); // Keep CLKDETCLR from clearing the bit over and over
273         pci_write_config8(dev, 0x40, reg8);
274         reg8 |= (1 << 2); // Enable clock detection
275         pci_write_config8(dev, 0x40, reg8);
276         mdelay(1);
277         reg8 = pci_read_config8(dev, 0x40);
278         printk(BIOS_DEBUG, "Azalia: codec type: %s\n", (reg8 & (1 << 1))?"Azalia":"AC97");
279
280         //
281         reg8 = pci_read_config8(dev, 0x40); // Audio Control
282         reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
283         pci_write_config8(dev, 0x40, reg8);
284
285         reg8 = pci_read_config8(dev, 0x4d); // Docking Status
286         reg8 &= ~(1 << 7); // Docking not supported
287         pci_write_config8(dev, 0x4d, reg8);
288 #if 0
289         /* Set routing pin */
290         pci_write_config32(dev, 0xf8, 0x0);
291         pci_write_config8(dev, 0xfc, 0xAA);
292
293         /* Set INTA */
294         pci_write_config8(dev, 0x63, 0x0);
295
296         /* Enable azalia, disable ac97 */
297         // pm_iowrite(0x59, 0xB);
298 #endif
299
300         res = find_resource(dev, 0x10);
301         if (!res)
302                 return;
303
304         // NOTE this will break as soon as the Azalia get's a bar above
305         // 4G. Is there anything we can do about it?
306         base = (u32)res->base;
307         printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
308         codec_mask = codec_detect(base);
309
310         if (codec_mask) {
311                 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
312                 codecs_init(dev, base, codec_mask);
313         }
314 }
315
316 static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device)
317 {
318         if (!vendor || !device) {
319                 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
320                                 pci_read_config32(dev, PCI_VENDOR_ID));
321         } else {
322                 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
323                                 ((device & 0xffff) << 16) | (vendor & 0xffff));
324         }
325 }
326
327 static struct pci_operations azalia_pci_ops = {
328         .set_subsystem    = azalia_set_subsystem,
329 };
330
331 static struct device_operations azalia_ops = {
332         .read_resources         = pci_dev_read_resources,
333         .set_resources          = pci_dev_set_resources,
334         .enable_resources       = pci_dev_enable_resources,
335         .init                   = azalia_init,
336         .scan_bus               = 0,
337         .enable                 = i82801gx_enable,
338         .ops_pci                = &azalia_pci_ops,
339 };
340
341 /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
342 static const struct pci_driver i82801gx_azalia __pci_driver = {
343         .ops    = &azalia_ops,
344         .vendor = PCI_VENDOR_ID_INTEL,
345         .device = 0x27d8,
346 };
347