493c98f7b2333c1c928e1ba467c44dd08d63a916
[coreboot.git] / src / southbridge / nvidia / mcp55 / 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-2010 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 "mcp55.h"
29
30 #define HDA_ICII_REG 0x68
31 #define   HDA_ICII_BUSY (1 << 0)
32 #define   HDA_ICII_VALID  (1 << 1)
33
34 static int set_bits(u32 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
63 static int codec_detect(u32 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
83 no_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
91 u32 * cim_verb_data = NULL;
92 u32 cim_verb_data_size = 0;
93
94 static u32 find_verb(struct device *dev, u32 viddid, u32 ** verb)
95 {
96         int idx=0;
97
98         while (idx < (cim_verb_data_size / sizeof(u32))) {
99                 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
100                 if (cim_verb_data[idx] != viddid) {
101                         idx += verb_size + 3; // skip verb + header
102                         continue;
103                 }
104                 *verb = &cim_verb_data[idx+3];
105                 return verb_size;
106         }
107
108         /* Not all codecs need to load another verb */
109         return 0;
110 }
111
112 /**
113  *  Wait 50usec for the codec to indicate it is ready
114  *  no response would imply that the codec is non-operative
115  */
116
117 static int wait_for_ready(u32 base)
118 {
119         /* Use a 50 usec timeout - the Linux kernel uses the
120          * same duration */
121
122         int timeout = 50;
123
124         while(timeout--) {
125                 u32 reg32 = read32(base +  HDA_ICII_REG);
126                 if (!(reg32 & HDA_ICII_BUSY))
127                         return 0;
128                 udelay(1);
129         }
130
131         return -1;
132 }
133
134 /**
135  *  Wait 50usec for the codec to indicate that it accepted
136  *  the previous command.  No response would imply that the code
137  *  is non-operative
138  */
139
140 static int wait_for_valid(u32 base)
141 {
142         u32 reg32;
143
144         /* Send the verb to the codec */
145         reg32 = read32(base + 0x68);
146         reg32 |= (1 << 0) | (1 << 1);
147         write32(base + 0x68, reg32);
148
149         /* Use a 50 usec timeout - the Linux kernel uses the
150          * same duration */
151
152         int timeout = 50;
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
164 static void codec_init(struct device *dev, u32 base, int addr)
165 {
166         u32 reg32;
167         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                 return;
176
177         reg32 = (addr << 28) | 0x000f0000;
178         write32(base + 0x60, reg32);
179
180         if (wait_for_valid(base) == -1)
181                 return;
182
183         reg32 = read32(base + 0x64);
184
185         /* 2 */
186         printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
187         verb_size = find_verb(dev, reg32, &verb);
188
189         if (!verb_size) {
190                 printk(BIOS_DEBUG, "Azalia: No verb!\n");
191                 return;
192         }
193         printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
194
195         /* 3 */
196         for (i = 0; i < verb_size; i++) {
197                 if (wait_for_ready(base) == -1)
198                         return;
199
200                 write32(base + 0x60, verb[i]);
201
202                 if (wait_for_valid(base) == -1)
203                         return;
204         }
205         printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
206 }
207
208 static void codecs_init(struct device *dev, u32 base, u32 codec_mask)
209 {
210         int i;
211         for (i = 2; i >= 0; i--) {
212                 if (codec_mask & (1 << i))
213                         codec_init(dev, base, i);
214         }
215 }
216
217 static void azalia_init(struct device *dev)
218 {
219         u32 base;
220         struct resource *res;
221         u32 codec_mask;
222         u8 reg8;
223         u32 reg32;
224
225         /* Set Bus Master */
226         reg32 = pci_read_config32(dev, PCI_COMMAND);
227         pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
228
229         pci_write_config8(dev, 0x3c, 0x0a); // unused?
230
231         reg8 = pci_read_config8(dev, 0x40);
232         reg8 |= (1 << 3); // Clear Clock Detect Bit
233         pci_write_config8(dev, 0x40, reg8);
234         reg8 &= ~(1 << 3); // Keep CLKDETCLR from clearing the bit over and over
235         pci_write_config8(dev, 0x40, reg8);
236         reg8 |= (1 << 2); // Enable clock detection
237         pci_write_config8(dev, 0x40, reg8);
238         mdelay(1);
239         reg8 = pci_read_config8(dev, 0x40);
240         printk(BIOS_DEBUG, "Azalia: codec type: %s\n", (reg8 & (1 << 1))?"Azalia":"AC97");
241
242         //
243         reg8 = pci_read_config8(dev, 0x40); // Audio Control
244         reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
245         pci_write_config8(dev, 0x40, reg8);
246
247         reg8 = pci_read_config8(dev, 0x4d); // Docking Status
248         reg8 &= ~(1 << 7); // Docking not supported
249         pci_write_config8(dev, 0x4d, reg8);
250
251         res = find_resource(dev, 0x10);
252         if (!res)
253                 return;
254
255         // NOTE this will break as soon as the Azalia get's a bar above
256         // 4G. Is there anything we can do about it?
257         base = (u32)res->base;
258         printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
259         codec_mask = codec_detect(base);
260
261         if (codec_mask) {
262                 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
263                 codecs_init(dev, base, codec_mask);
264         }
265 }
266
267 static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device)
268 {
269         if (!vendor || !device) {
270                 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
271                                 pci_read_config32(dev, PCI_VENDOR_ID));
272         } else {
273                 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
274                                 ((device & 0xffff) << 16) | (vendor & 0xffff));
275         }
276 }
277
278 static struct pci_operations azalia_pci_ops = {
279         .set_subsystem    = azalia_set_subsystem,
280 };
281
282 static struct device_operations azalia_ops = {
283         .read_resources         = pci_dev_read_resources,
284         .set_resources          = pci_dev_set_resources,
285         .enable_resources       = pci_dev_enable_resources,
286         .init                   = azalia_init,
287         .scan_bus               = 0,
288 //      .enable                 = mcp55_enable,
289         .ops_pci                = &azalia_pci_ops,
290 };
291
292 static const struct pci_driver azalia __pci_driver = {
293         .ops    = &azalia_ops,
294         .vendor = PCI_VENDOR_ID_NVIDIA,
295         .device = PCI_DEVICE_ID_NVIDIA_MCP55_AZA,
296 };
297