2f55c944c72f3d73d6a7ad43c51162d3a0558270
[coreboot.git] / src / southbridge / amd / sb700 / sb700_hda.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2010 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <console/console.h>
21 #include <device/device.h>
22 #include <device/pci.h>
23 #include <device/pci_ids.h>
24 #include <device/pci_ops.h>
25 #include <arch/io.h>
26 #include <delay.h>
27 #include "sb700.h"
28
29 #define HDA_ICII_REG 0x68
30 #define   HDA_ICII_BUSY (1 << 0)
31 #define   HDA_ICII_VALID  (1 << 1)
32
33 static int set_bits(u32 port, u32 mask, u32 val)
34 {
35         u32 dword;
36         int count;
37
38         /* Write (val & ~mask) to port */
39         val &= mask;
40         dword = read32(port);
41         dword &= ~mask;
42         dword |= val;
43         write32(port, dword);
44
45         /* Wait for readback of register to
46          * match what was just written to it
47          */
48         count = 50;
49         do {
50                 /* Wait 1ms based on BKDG wait time */
51                 mdelay(1);
52                 dword = read32(port);
53                 dword &= mask;
54         } while ((dword != val) && --count);
55
56         /* Timeout occured */
57         if (!count)
58                 return -1;
59         return 0;
60 }
61
62 static u32 codec_detect(u32 base)
63 {
64         u32 dword;
65
66         /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */
67         if (set_bits(base + 0x08, 1, 0) == -1)
68                 goto no_codec;
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         /* Delay for 1 ms since the BKDG does */
75         mdelay(1);
76
77         /* Read in Codec location (BAR + 0xe)[3..0]*/
78         dword = read32(base + 0xe);
79         dword &= 0x0F;
80         if (!dword)
81                 goto no_codec;
82
83         return dword;
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_debug("No codec!\n");
90         return 0;
91 }
92
93 /**
94  *  Wait 50usec for for the codec to indicate it is ready
95  *  no response would imply that the codec is non-operative
96  */
97 static int wait_for_ready(u32 base)
98 {
99         /* Use a 50 usec timeout - the Linux kernel uses the
100          * same duration */
101
102         int timeout = 50;
103
104         while(timeout--) {
105                 u32 dword=read32(base +  HDA_ICII_REG);
106                 if (!(dword & HDA_ICII_BUSY))
107                         return 0;
108                 udelay(1);
109         }
110
111         return -1;
112 }
113
114 /**
115  *  Wait 50usec for for the codec to indicate that it accepted
116  *  the previous command.  No response would imply that the code
117  *  is non-operative
118  */
119 static int wait_for_valid(u32 base)
120 {
121         /* Use a 50 usec timeout - the Linux kernel uses the
122          * same duration */
123
124         int timeout = 50;
125         while(timeout--) {
126                 u32 dword = read32(base + HDA_ICII_REG);
127                 if ((dword & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
128                         HDA_ICII_VALID)
129                         return 0;
130                 udelay(1);
131         }
132
133         return 1;
134 }
135
136 static void codec_init(u32 base, int addr)
137 {
138         u32 dword;
139
140         /* 1 */
141         if (wait_for_ready(base) == -1)
142                 return;
143
144         dword = (addr << 28) | 0x000f0000;
145         write32(base + 0x60, dword);
146
147         if (wait_for_valid(base) == -1)
148                 return;
149
150         dword = read32(base + 0x64);
151
152         /* 2 */
153         printk_debug("%x(th) codec viddid: %08x\n", addr, dword);
154 }
155
156 static void codecs_init(u32 base, u32 codec_mask)
157 {
158         int i;
159         for (i = 2; i >= 0; i--) {
160                 if (codec_mask & (1 << i))
161                         codec_init(base, i);
162         }
163 }
164
165 static void hda_init(struct device *dev)
166 {
167         u8 byte;
168         u32 dword;
169         u32 base;
170         struct resource *res;
171         u32 codec_mask;
172         device_t sm_dev;
173
174         /* Enable azalia - PM_io 0x59[3], no ac97 in sb700. */
175         byte = pm_ioread(0x59);
176         byte |= 1 << 3;
177         pm_iowrite(0x59, byte);
178
179         /* Find the SMBus */
180         /* FIXME: Need to find out why the call below crashes. */
181         /*sm_dev = dev_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_ATI_SB700_SM, 0);*/
182         sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
183
184         /* Set routing pin - SMBus ExtFunc (0xf8/0xfc) */
185         pci_write_config32(sm_dev, 0xf8, 0x00);
186         pci_write_config8(sm_dev, 0xfc, 0xAA);
187         /* Set INTA - SMBus 0x63 [2..0] */
188         byte = pci_read_config8(sm_dev, 0x63);
189         byte &= ~0x7;
190         byte |= 0x0; /* INTA:0x0 - INTH:0x7 */
191         pci_write_config8(sm_dev, 0x63, byte);
192
193         /* Program the 2C to 0x437b1002 */
194         dword = 0x437b1002;
195         pci_write_config32(dev, 0x2c, dword);
196
197         /* Read in BAR */
198         /* Is this right? HDA allows for a 64-bit BAR
199          * but this is only setup for a 32-bit one
200          */
201         res = find_resource(dev, 0x10);
202         if (!res)
203                 return;
204
205         base = (u32)res->base;
206         printk_debug("base = 0x%x\n", base);
207         codec_mask = codec_detect(base);
208
209         if (codec_mask) {
210                 printk_debug("codec_mask = %02x\n", codec_mask);
211                 codecs_init(base, codec_mask);
212         }
213 }
214
215 static struct pci_operations lops_pci = {
216         .set_subsystem = pci_dev_set_subsystem,
217 };
218
219 static struct device_operations hda_audio_ops = {
220         .read_resources = pci_dev_read_resources,
221         .set_resources = pci_dev_set_resources,
222         .enable_resources = pci_dev_enable_resources,
223         .init = hda_init,
224         .scan_bus = 0,
225         .ops_pci = &lops_pci,
226 };
227
228 static const struct pci_driver hdaaudio_driver __pci_driver = {
229         .ops = &hda_audio_ops,
230         .vendor = PCI_VENDOR_ID_ATI,
231         .device = PCI_DEVICE_ID_ATI_SB700_HDA,
232 };