Please bear with me - another rename checkin. This qualifies as trivial, no
[coreboot.git] / src / southbridge / nvidia / mcp55 / mcp55_aza.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2004 Tyan Computer
5  * Written by Yinghai Lu <yhlu@tyan.com> for Tyan Computer.
6  * Copyright (C) 2006,2007 AMD
7  * Written by Yinghai Lu <yinghai.lu@amd.com> for AMD.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23
24 #include <console/console.h>
25 #include <device/device.h>
26 #include <device/pci.h>
27 #include <device/pci_ids.h>
28 #include <device/pci_ops.h>
29 #include <arch/io.h>
30 #include "mcp55.h"
31
32 static int set_bits(uint8_t *port, uint32_t mask, uint32_t val)
33 {
34         uint32_t dword;
35         int count;
36
37         val &= mask;
38         dword = readl(port);
39         dword &= ~mask;
40         dword |= val;
41         writel(dword, port);
42
43         count = 50;
44         do {
45                 dword = readl(port);
46                 dword &= mask;
47                 udelay(100);
48         } while ((dword != val) && --count);
49
50         if(!count) return -1;
51
52         udelay(540);
53         return 0;
54
55 }
56
57 static int codec_detect(uint8_t *base)
58 {
59         uint32_t dword;
60
61         /* 1 */
62         set_bits(base + 0x08, 1, 1);
63
64         /* 2 */
65         dword = readl(base + 0x0e);
66         dword |= 7;
67         writel(dword, base + 0x0e);
68
69         /* 3 */
70         set_bits(base + 0x08, 1, 0);
71
72         /* 4 */
73         set_bits(base + 0x08, 1, 1);
74
75         /* 5 */
76         dword = readl(base + 0xe);
77         dword &= 7;
78
79         /* 6 */
80         if(!dword) {
81                 set_bits(base + 0x08, 1, 0);
82                 printk_debug("No codec!\n");
83                 return 0;
84         }
85         return dword;
86
87 }
88
89 static uint32_t verb_data[] = {
90 #if 0
91         0x00172001,
92         0x001721e6,
93         0x00172200,
94         0x00172300,
95 #endif
96
97         0x01471c10,
98         0x01471d44,
99         0x01471e01,
100         0x01471f01,
101 //1
102         0x01571c12,
103         0x01571d14,
104         0x01571e01,
105         0x01571f01,
106 //2
107         0x01671c11,
108         0x01671d60,
109         0x01671e01,
110         0x01671f01,
111 //3
112         0x01771c14,
113         0x01771d20,
114         0x01771e01,
115         0x01771f01,
116 //4
117         0x01871c30,
118         0x01871d9c,
119         0x01871ea1,
120         0x01871f01,
121 //5
122         0x01971c40,
123         0x01971d9c,
124         0x01971ea1,
125         0x01971f02,
126 //6
127         0x01a71c31,
128         0x01a71d34,
129         0x01a71e81,
130         0x01a71f01,
131 //7
132         0x01b71c1f,
133         0x01b71d44,
134         0x01b71e21,
135         0x01b71f02,
136 //8
137         0x01c71cf0,
138         0x01c71d11,
139         0x01c71e11,
140         0x01c71f41,
141 //9
142         0x01d71c3e,
143         0x01d71d01,
144         0x01d71e83,
145         0x01d71f99,
146 //10
147         0x01e71c20,
148         0x01e71d41,
149         0x01e71e45,
150         0x01e71f01,
151 //11
152         0x01f71c50,
153         0x01f71d91,
154         0x01f71ec5,
155         0x01f71f01,
156 };
157
158 static unsigned find_verb(uint32_t viddid, uint32_t **verb)
159 {
160         if(viddid != 0x10ec0880) return 0;
161         *verb =  (uint32_t *)verb_data;
162         return sizeof(verb_data)/sizeof(uint32_t);
163 }
164
165
166 static void codec_init(uint8_t *base, int addr)
167 {
168         uint32_t dword;
169         uint32_t *verb;
170         unsigned verb_size;
171         int i;
172
173         /* 1 */
174         do {
175                 dword = readl(base + 0x68);
176         } while (dword & 1);
177
178         dword = (addr<<28) | 0x000f0000;
179         writel(dword, base + 0x60);
180
181         do {
182                 dword = readl(base + 0x68);
183         } while ((dword & 3)!=2);
184
185         dword = readl(base + 0x64);
186
187         /* 2 */
188         printk_debug("codec viddid: %08x\n", dword);
189         verb_size = find_verb(dword, &verb);
190
191         if(!verb_size) {
192                 printk_debug("No verb!\n");
193                 return;
194         }
195
196         printk_debug("verb_size: %d\n", verb_size);
197         /* 3 */
198         for(i=0; i<verb_size; i++) {
199                 do {
200                         dword = readl(base + 0x68);
201                 } while (dword & 1);
202
203                 writel(verb[i], base + 0x60);
204
205                 do {
206                         dword = readl(base + 0x68);
207                 } while ((dword & 3) != 2);
208         }
209         printk_debug("verb loaded!\n");
210 }
211
212 static void codecs_init(uint8_t *base, uint32_t codec_mask)
213 {
214         int i;
215         for(i=2; i>=0; i--) {
216                 if( codec_mask & (1<<i) )
217                         codec_init(base, i);
218         }
219 }
220
221 static void aza_init(struct device *dev)
222 {
223         uint8_t *base;
224         struct resource *res;
225         uint32_t codec_mask;
226
227         res = find_resource(dev, 0x10);
228         if(!res)
229                 return;
230
231         base =(uint8_t *) res->base;
232         printk_debug("base = %08x\n", base);
233
234         codec_mask = codec_detect(base);
235
236         if(codec_mask) {
237                 printk_debug("codec_mask = %02x\n", codec_mask);
238                 codecs_init(base, codec_mask);
239         }
240 }
241
242 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
243 {
244         pci_write_config32(dev, 0x40,
245                 ((device & 0xffff) << 16) | (vendor & 0xffff));
246 }
247
248 static struct pci_operations lops_pci = {
249         .set_subsystem  = lpci_set_subsystem,
250 };
251
252 static struct device_operations aza_audio_ops  = {
253         .read_resources = pci_dev_read_resources,
254         .set_resources  = pci_dev_set_resources,
255         .enable_resources       = pci_dev_enable_resources,
256 //      .enable         = mcp55_enable,
257         .init           = aza_init,
258         .scan_bus       = 0,
259         .ops_pci        = &lops_pci,
260 };
261
262 static const struct pci_driver azaaudio_driver __pci_driver = {
263         .ops    = &aza_audio_ops,
264         .vendor = PCI_VENDOR_ID_NVIDIA,
265         .device = PCI_DEVICE_ID_NVIDIA_MCP55_AZA,
266 };
267