blob: b97bdec6ac7fdbd2ddcd528609016fe079ba337f [file] [log] [blame]
Michael Xie7586cef2008-09-22 13:11:39 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008 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.
Michael Xie7586cef2008-09-22 13:11:39 +000014 */
15
16#include <console/console.h>
17#include <device/device.h>
18#include <device/pci.h>
19#include <device/pci_ids.h>
20#include <device/pci_ops.h>
21#include <arch/io.h>
22#include <delay.h>
23#include "sb600.h"
24
Jordan Crouse964f0662008-10-07 16:25:10 +000025#define HDA_ICII_REG 0x68
Andrew Wuae8d0692013-08-02 19:29:17 +080026#define HDA_ICII_BUSY (1 << 0)
27#define HDA_ICII_VALID (1 << 1)
Jordan Crouse964f0662008-10-07 16:25:10 +000028
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080029static int set_bits(void *port, u32 mask, u32 val)
Michael Xie7586cef2008-09-22 13:11:39 +000030{
31 u32 dword;
32 int count;
33
Dan Lykowski45059482009-02-05 02:18:42 +000034 /* Write (val & ~mask) to port */
Michael Xie7586cef2008-09-22 13:11:39 +000035 val &= mask;
Stefan Reinauer9fe4d792010-01-16 17:53:38 +000036 dword = read32(port);
Michael Xie7586cef2008-09-22 13:11:39 +000037 dword &= ~mask;
38 dword |= val;
Stefan Reinauer9fe4d792010-01-16 17:53:38 +000039 write32(port, dword);
Michael Xie7586cef2008-09-22 13:11:39 +000040
Zheng Baocb69cb32009-10-14 02:56:00 +000041 /* Wait for readback of register to
42 * match what was just written to it
Dan Lykowski45059482009-02-05 02:18:42 +000043 */
Michael Xie7586cef2008-09-22 13:11:39 +000044 count = 50;
45 do {
Dan Lykowski45059482009-02-05 02:18:42 +000046 /* Wait 1ms based on BKDG wait time */
47 mdelay(1);
Stefan Reinauer9fe4d792010-01-16 17:53:38 +000048 dword = read32(port);
Michael Xie7586cef2008-09-22 13:11:39 +000049 dword &= mask;
Michael Xie7586cef2008-09-22 13:11:39 +000050 } while ((dword != val) && --count);
51
Zheng Bao2a5101a2010-10-10 15:18:53 +000052 /* Timeout occurred */
Michael Xie7586cef2008-09-22 13:11:39 +000053 if (!count)
54 return -1;
Michael Xie7586cef2008-09-22 13:11:39 +000055 return 0;
56}
57
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080058static u32 codec_detect(void *base)
Michael Xie7586cef2008-09-22 13:11:39 +000059{
60 u32 dword;
61
Dan Lykowski45059482009-02-05 02:18:42 +000062 /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */
Zheng Baocb69cb32009-10-14 02:56:00 +000063 if (set_bits(base + 0x08, 1, 0) == -1)
Dan Lykowski45059482009-02-05 02:18:42 +000064 goto no_codec;
Michael Xie7586cef2008-09-22 13:11:39 +000065
Dan Lykowski45059482009-02-05 02:18:42 +000066 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
Zheng Baocb69cb32009-10-14 02:56:00 +000067 if (set_bits(base + 0x08, 1, 1) == -1)
Dan Lykowski45059482009-02-05 02:18:42 +000068 goto no_codec;
Michael Xie7586cef2008-09-22 13:11:39 +000069
Dan Lykowski45059482009-02-05 02:18:42 +000070 /* Delay for 1 ms since the BKDG does */
71 mdelay(1);
Michael Xie7586cef2008-09-22 13:11:39 +000072
Dan Lykowski45059482009-02-05 02:18:42 +000073 /* Read in Codec location (BAR + 0xe)[3..0]*/
Stefan Reinauer9fe4d792010-01-16 17:53:38 +000074 dword = read32(base + 0xe);
Dan Lykowski45059482009-02-05 02:18:42 +000075 dword &= 0x0F;
76 if (!dword)
77 goto no_codec;
Zheng Baocb69cb32009-10-14 02:56:00 +000078
Michael Xie7586cef2008-09-22 13:11:39 +000079 return dword;
80
Dan Lykowski45059482009-02-05 02:18:42 +000081no_codec:
82 /* Codec Not found */
83 /* Put HDA back in reset (BAR + 0x8) [0] */
84 set_bits(base + 0x08, 1, 0);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000085 printk(BIOS_DEBUG, "No codec!\n");
Dan Lykowski45059482009-02-05 02:18:42 +000086 return 0;
Michael Xie7586cef2008-09-22 13:11:39 +000087}
88
89static u32 cim_verb_data[] = {
90 0x01471c10,
91 0x01471d40,
92 0x01471e01,
93 0x01471f01,
94/* 1 */
95 0x01571c12,
96 0x01571d10,
97 0x01571e01,
98 0x01571f01,
99/* 2 */
100 0x01671c11,
101 0x01671d60,
102 0x01671e01,
103 0x01671f01,
104/* 3 */
105 0x01771c14,
106 0x01771d20,
107 0x01771e01,
108 0x01771f01,
109/* 4 */
110 0x01871c30,
111 0x01871d90,
112 0x01871ea1,
113 0x01871f01,
114/* 5 */
115 0x01971cf0,
116 0x01971d11,
117 0x01971e11,
118 0x01971f41,
119/* 6 */
120 0x01a71c80,
121 0x01a71d30,
122 0x01a71e81,
123 0x01a71f01,
124/* 7 */
125 0x01b71cf0,
126 0x01b71d11,
127 0x01b71e11,
128 0x01b71f41,
129/* 8 */
130 0x01c71cf0,
131 0x01c71d11,
132 0x01c71e11,
133 0x01c71f41,
134/* 9 */
135 0x01d71cf0,
136 0x01d71d11,
137 0x01d71e11,
138 0x01d71f41,
139/* 10 */
140 0x01e71c50,
141 0x01e71d11,
142 0x01e71e44,
143 0x01e71f01,
144/* 11 */
145 0x01f71c60,
146 0x01f71d61,
147 0x01f71ec4,
148 0x01f71f01,
149};
Uwe Hermannff492b12010-09-24 23:37:25 +0000150
Joe Bao164463c2008-12-01 19:37:21 +0000151static u32 find_verb(u32 viddid, u32 ** verb)
Michael Xie7586cef2008-09-22 13:11:39 +0000152{
153 device_t azalia_dev = dev_find_slot(0, PCI_DEVFN(0x14, 2));
154 struct southbridge_amd_sb600_config *cfg =
155 (struct southbridge_amd_sb600_config *)azalia_dev->chip_info;
Patrick Georgibdd185a2015-02-22 17:54:18 +0100156 if (!cfg)
157 return 0;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000158 printk(BIOS_DEBUG, "Dev=%s\n", dev_path(azalia_dev));
159 printk(BIOS_DEBUG, "Default viddid=%x\n", cfg->hda_viddid);
160 printk(BIOS_DEBUG, "Reading viddid=%x\n", viddid);
Michael Xie7586cef2008-09-22 13:11:39 +0000161 if (viddid != cfg->hda_viddid)
162 return 0;
163 *verb = (u32 *) cim_verb_data;
164 return sizeof(cim_verb_data) / sizeof(u32);
165}
166
Jordan Crouse964f0662008-10-07 16:25:10 +0000167/**
Zheng Bao2a5101a2010-10-10 15:18:53 +0000168 * Wait 50usec for the codec to indicate it is ready
Jordan Crouse964f0662008-10-07 16:25:10 +0000169 * no response would imply that the codec is non-operative
170 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800171static int wait_for_ready(void *base)
Jordan Crouse964f0662008-10-07 16:25:10 +0000172{
173 /* Use a 50 usec timeout - the Linux kernel uses the
174 * same duration */
175
176 int timeout = 50;
177
178 while(timeout--) {
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000179 u32 dword=read32(base + HDA_ICII_REG);
Jordan Crouse964f0662008-10-07 16:25:10 +0000180 if (!(dword & HDA_ICII_BUSY))
181 return 0;
182 udelay(1);
183 }
184
185 return -1;
186}
187
188/**
Zheng Bao2a5101a2010-10-10 15:18:53 +0000189 * Wait 50usec for the codec to indicate that it accepted
Jordan Crouse964f0662008-10-07 16:25:10 +0000190 * the previous command. No response would imply that the code
191 * is non-operative
192 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800193static int wait_for_valid(void *base)
Jordan Crouse964f0662008-10-07 16:25:10 +0000194{
195 /* Use a 50 usec timeout - the Linux kernel uses the
196 * same duration */
197
198 int timeout = 50;
199 while(timeout--) {
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000200 u32 dword = read32(base + HDA_ICII_REG);
Jordan Crouse964f0662008-10-07 16:25:10 +0000201 if ((dword & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
202 HDA_ICII_VALID)
203 return 0;
204 udelay(1);
205 }
206
Andrew Wu9361daf2013-08-02 14:45:03 +0800207 return -1;
Jordan Crouse964f0662008-10-07 16:25:10 +0000208}
209
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800210static void codec_init(void *base, int addr)
Michael Xie7586cef2008-09-22 13:11:39 +0000211{
212 u32 dword;
213 u32 *verb;
214 u32 verb_size;
215 int i;
216
217 /* 1 */
Jordan Crouse964f0662008-10-07 16:25:10 +0000218 if (wait_for_ready(base) == -1)
219 return;
Michael Xie7586cef2008-09-22 13:11:39 +0000220
221 dword = (addr << 28) | 0x000f0000;
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000222 write32(base + 0x60, dword);
Michael Xie7586cef2008-09-22 13:11:39 +0000223
Jordan Crouse964f0662008-10-07 16:25:10 +0000224 if (wait_for_valid(base) == -1)
225 return;
Michael Xie7586cef2008-09-22 13:11:39 +0000226
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000227 dword = read32(base + 0x64);
Michael Xie7586cef2008-09-22 13:11:39 +0000228
229 /* 2 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000230 printk(BIOS_DEBUG, "codec viddid: %08x\n", dword);
Michael Xie7586cef2008-09-22 13:11:39 +0000231 verb_size = find_verb(dword, &verb);
232
233 if (!verb_size) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000234 printk(BIOS_DEBUG, "No verb!\n");
Michael Xie7586cef2008-09-22 13:11:39 +0000235 return;
236 }
237
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000238 printk(BIOS_DEBUG, "verb_size: %d\n", verb_size);
Michael Xie7586cef2008-09-22 13:11:39 +0000239 /* 3 */
240 for (i = 0; i < verb_size; i++) {
Jordan Crouse964f0662008-10-07 16:25:10 +0000241 if (wait_for_ready(base) == -1)
242 return;
Michael Xie7586cef2008-09-22 13:11:39 +0000243
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000244 write32(base + 0x60, verb[i]);
Michael Xie7586cef2008-09-22 13:11:39 +0000245
Jordan Crouse964f0662008-10-07 16:25:10 +0000246 if (wait_for_valid(base) == -1)
247 return;
Michael Xie7586cef2008-09-22 13:11:39 +0000248 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000249 printk(BIOS_DEBUG, "verb loaded!\n");
Michael Xie7586cef2008-09-22 13:11:39 +0000250}
251
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800252static void codecs_init(void *base, u32 codec_mask)
Michael Xie7586cef2008-09-22 13:11:39 +0000253{
254 int i;
255 for (i = 2; i >= 0; i--) {
256 if (codec_mask & (1 << i))
257 codec_init(base, i);
258 }
259}
260
261static void hda_init(struct device *dev)
262{
Dan Lykowski45059482009-02-05 02:18:42 +0000263 u8 byte;
264 u32 dword;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800265 void *base;
Michael Xie7586cef2008-09-22 13:11:39 +0000266 struct resource *res;
267 u32 codec_mask;
Dan Lykowski45059482009-02-05 02:18:42 +0000268 device_t sm_dev;
Zheng Baocb69cb32009-10-14 02:56:00 +0000269
Dan Lykowski45059482009-02-05 02:18:42 +0000270 /* Enable azalia - PM_io 0x59[4], disable ac97 - PM_io 0x59[1..0] */
Michael Xie7586cef2008-09-22 13:11:39 +0000271 pm_iowrite(0x59, 0xB);
Zheng Baocb69cb32009-10-14 02:56:00 +0000272
Dan Lykowski45059482009-02-05 02:18:42 +0000273 /* Find the SMBus */
274 /* FIXME: Need to find out why the call below crashes. */
275 /*sm_dev = dev_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_ATI_SB600_SM, 0);*/
276 sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
Michael Xie7586cef2008-09-22 13:11:39 +0000277
Dan Lykowski45059482009-02-05 02:18:42 +0000278 /* Set routing pin - SMBus ExtFunc (0xf8/0xfc) */
279 pci_write_config32(sm_dev, 0xf8, 0x00);
280 pci_write_config8(sm_dev, 0xfc, 0xAA);
281 /* Set INTA - SMBus 0x63 [2..0] */
282 byte = pci_read_config8(sm_dev, 0x63);
283 byte &= ~0x7;
284 byte |= 0x0; /* INTA:0x0 - INTH:0x7 */
285 pci_write_config8(sm_dev, 0x63, byte);
286
287 /* Program the 2C to 0x437b1002 */
288 dword = 0x437b1002;
289 pci_write_config32(dev, 0x2c, dword);
290
291 /* Read in BAR */
Zheng Baocb69cb32009-10-14 02:56:00 +0000292 /* Is this right? HDA allows for a 64-bit BAR
293 * but this is only setup for a 32-bit one
Dan Lykowski45059482009-02-05 02:18:42 +0000294 */
Michael Xie7586cef2008-09-22 13:11:39 +0000295 res = find_resource(dev, 0x10);
296 if (!res)
297 return;
298
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800299 base = res2mmio(res, 0, 0);
300 printk(BIOS_DEBUG, "base = 0x%p\n", base);
Michael Xie7586cef2008-09-22 13:11:39 +0000301 codec_mask = codec_detect(base);
302
303 if (codec_mask) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000304 printk(BIOS_DEBUG, "codec_mask = %02x\n", codec_mask);
Michael Xie7586cef2008-09-22 13:11:39 +0000305 codecs_init(base, codec_mask);
306 }
307}
308
309static struct pci_operations lops_pci = {
310 .set_subsystem = pci_dev_set_subsystem,
311};
312
313static struct device_operations hda_audio_ops = {
314 .read_resources = pci_dev_read_resources,
315 .set_resources = pci_dev_set_resources,
316 .enable_resources = pci_dev_enable_resources,
317 /*.enable = sb600_enable, */
318 .init = hda_init,
319 .scan_bus = 0,
320 .ops_pci = &lops_pci,
321};
322
Stefan Reinauer8e96ba22010-03-16 23:33:29 +0000323static const struct pci_driver hdaaudio_driver __pci_driver = {
Michael Xie7586cef2008-09-22 13:11:39 +0000324 .ops = &hda_audio_ops,
325 .vendor = PCI_VENDOR_ID_ATI,
326 .device = PCI_DEVICE_ID_ATI_SB600_HDA,
327};