blob: 558f9cda9c2ef0029b73e98d39223c7cd52d7108 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgie72a8a32012-11-06 11:05:09 +01002
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
6#include <device/pci_ids.h>
7#include <device/pci_ops.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Patrick Georgie72a8a32012-11-06 11:05:09 +01009#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020010#include <device/azalia_device.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030011#include "chip.h"
Patrick Georgie72a8a32012-11-06 11:05:09 +010012#include "i82801ix.h"
13
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080014static int codec_detect(u8 *base)
Patrick Georgie72a8a32012-11-06 11:05:09 +010015{
16 u32 reg32;
17
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020018 /* Set Bit 0 to 0 to enter reset state (BAR + 0x8)[0] */
Angel Pons61dd8362020-12-05 18:02:32 +010019 if (azalia_set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, 0) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +010020 goto no_codec;
21
Angel Pons7f839f62020-12-05 19:02:14 +010022 if (azalia_exit_reset(base) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +010023 goto no_codec;
24
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020025 /* Read in Codec location (BAR + 0xe)[2..0] */
Elyes HAOUASe5954ba2020-08-03 15:35:47 +020026 reg32 = read32(base + HDA_STATESTS_REG);
Patrick Georgie72a8a32012-11-06 11:05:09 +010027 reg32 &= 0x0f;
28 if (!reg32)
29 goto no_codec;
30
31 return reg32;
32
33no_codec:
34 /* Codec Not found */
35 /* Put HDA back in reset (BAR + 0x8) [0] */
Angel Pons61dd8362020-12-05 18:02:32 +010036 azalia_set_bits(base + HDA_GCTL_REG, 1, 0);
Patrick Georgie72a8a32012-11-06 11:05:09 +010037 printk(BIOS_DEBUG, "Azalia: No codec!\n");
38 return 0;
39}
40
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020041/*
42 * Wait 50usec for the codec to indicate it is ready.
43 * No response would imply that the codec is non-operative.
Patrick Georgie72a8a32012-11-06 11:05:09 +010044 */
45
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080046static int wait_for_ready(u8 *base)
Patrick Georgie72a8a32012-11-06 11:05:09 +010047{
Angel Ponsc9d63332020-06-21 15:38:29 +020048 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
Patrick Georgie72a8a32012-11-06 11:05:09 +010049 int timeout = 50;
50
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020051 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080052 u32 reg32 = read32(base + HDA_ICII_REG);
Patrick Georgie72a8a32012-11-06 11:05:09 +010053 if (!(reg32 & HDA_ICII_BUSY))
54 return 0;
55 udelay(1);
56 }
57
58 return -1;
59}
60
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020061/*
62 * Wait 50usec for the codec to indicate that it accepted the previous command.
63 * No response would imply that the code is non-operative.
Patrick Georgie72a8a32012-11-06 11:05:09 +010064 */
65
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080066static int wait_for_valid(u8 *base)
Patrick Georgie72a8a32012-11-06 11:05:09 +010067{
68 u32 reg32;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020069 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
70 int timeout = 50;
Patrick Georgie72a8a32012-11-06 11:05:09 +010071
72 /* Send the verb to the codec */
Elyes HAOUASe5954ba2020-08-03 15:35:47 +020073 reg32 = read32(base + HDA_ICII_REG);
74 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
75 write32(base + HDA_ICII_REG, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +010076
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020077 while (timeout--) {
Patrick Georgie72a8a32012-11-06 11:05:09 +010078 reg32 = read32(base + HDA_ICII_REG);
Angel Ponsc9d63332020-06-21 15:38:29 +020079 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Patrick Georgie72a8a32012-11-06 11:05:09 +010080 return 0;
81 udelay(1);
82 }
83
84 return -1;
85}
86
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080087static void codec_init(struct device *dev, u8 *base, int addr)
Patrick Georgie72a8a32012-11-06 11:05:09 +010088{
89 u32 reg32;
90 const u32 *verb;
91 u32 verb_size;
92 int i;
93
Angel Ponsaaa8ab72020-06-21 15:33:24 +020094 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
Patrick Georgie72a8a32012-11-06 11:05:09 +010095
96 /* 1 */
Angel Pons554713e2020-10-24 23:23:07 +020097 if (wait_for_ready(base) < 0) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020098 printk(BIOS_DEBUG, " codec not ready.\n");
Patrick Georgie72a8a32012-11-06 11:05:09 +010099 return;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200100 }
Patrick Georgie72a8a32012-11-06 11:05:09 +0100101
102 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUASe5954ba2020-08-03 15:35:47 +0200103 write32(base + HDA_IC_REG, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100104
Angel Pons554713e2020-10-24 23:23:07 +0200105 if (wait_for_valid(base) < 0) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200106 printk(BIOS_DEBUG, " codec not valid.\n");
Patrick Georgie72a8a32012-11-06 11:05:09 +0100107 return;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200108 }
Patrick Georgie72a8a32012-11-06 11:05:09 +0100109
110 /* 2 */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200111 reg32 = read32(base + HDA_IR_REG);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100112 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
Angel Ponsd425ddd2020-12-05 18:22:58 +0100113 verb_size = azalia_find_verb(cim_verb_data, cim_verb_data_size, reg32, &verb);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100114
115 if (!verb_size) {
116 printk(BIOS_DEBUG, "Azalia: No verb!\n");
117 return;
118 }
119 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
120
121 /* 3 */
122 for (i = 0; i < verb_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200123 if (wait_for_ready(base) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100124 return;
125
Elyes HAOUASe5954ba2020-08-03 15:35:47 +0200126 write32(base + HDA_IC_REG, verb[i]);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100127
Angel Pons554713e2020-10-24 23:23:07 +0200128 if (wait_for_valid(base) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100129 return;
130 }
131 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
132}
133
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800134static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100135{
136 int i;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200137
Patrick Georgie72a8a32012-11-06 11:05:09 +0100138 for (i = 2; i >= 0; i--) {
139 if (codec_mask & (1 << i))
140 codec_init(dev, base, i);
141 }
142
143 for (i = 0; i < pc_beep_verbs_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200144 if (wait_for_ready(base) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100145 return;
146
Elyes HAOUASe5954ba2020-08-03 15:35:47 +0200147 write32(base + HDA_IC_REG, pc_beep_verbs[i]);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100148
Angel Pons554713e2020-10-24 23:23:07 +0200149 if (wait_for_valid(base) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100150 return;
151 }
152}
153
154static void azalia_init(struct device *dev)
155{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800156 u8 *base;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100157 struct resource *res;
158 u32 codec_mask;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100159
Patrick Georgie72a8a32012-11-06 11:05:09 +0100160 // ESD
Angel Pons67406472020-06-08 11:13:42 +0200161 pci_update_config32(dev, 0x134, ~0x00ff0000, 2 << 16);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100162
163 // Link1 description
Angel Pons67406472020-06-08 11:13:42 +0200164 pci_update_config32(dev, 0x140, ~0x00ff0000, 2 << 16);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100165
166 // Port VC0 Resource Control Register
Angel Pons67406472020-06-08 11:13:42 +0200167 pci_update_config32(dev, 0x114, ~0x000000ff, 1);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100168
169 // VCi traffic class
Angel Pons67406472020-06-08 11:13:42 +0200170 pci_or_config8(dev, 0x44, 7 << 0); // TC7
Patrick Georgie72a8a32012-11-06 11:05:09 +0100171
172 // VCi Resource Control
Angel Pons67406472020-06-08 11:13:42 +0200173 pci_or_config32(dev, 0x120, (1 << 31) | (1 << 24) | (0x80 << 0)); /* VCi ID and map */
Patrick Georgie72a8a32012-11-06 11:05:09 +0100174
175 /* Set Bus Master */
Elyes HAOUASb9d2e222020-04-28 10:25:12 +0200176 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100177
Angel Pons67406472020-06-08 11:13:42 +0200178 // Docking not supported
179 pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
Patrick Georgie72a8a32012-11-06 11:05:09 +0100180
181 /* Lock some R/WO bits by writing their current value. */
Angel Pons67406472020-06-08 11:13:42 +0200182 pci_update_config32(dev, 0x74, ~0, 0);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100183
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200184 res = find_resource(dev, PCI_BASE_ADDRESS_0);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100185 if (!res)
186 return;
187
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200188 // NOTE this will break as soon as the Azalia get's a bar above 4G.
189 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800190 base = res2mmio(res, 0, 0);
Patrick Rudolph4af2add2018-11-26 15:56:11 +0100191 printk(BIOS_DEBUG, "Azalia: base = %p\n", base);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100192 codec_mask = codec_detect(base);
193
194 if (codec_mask) {
195 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
196 codecs_init(dev, base, codec_mask);
197 }
198}
199
Patrick Georgie72a8a32012-11-06 11:05:09 +0100200static struct device_operations azalia_ops = {
201 .read_resources = pci_dev_read_resources,
202 .set_resources = pci_dev_set_resources,
203 .enable_resources = pci_dev_enable_resources,
204 .init = azalia_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200205 .ops_pci = &pci_dev_ops_pci,
Patrick Georgie72a8a32012-11-06 11:05:09 +0100206};
207
208/* ICH9DH/ICH9DO/ICH9R/ICH9/ICH9M-E/ICH9M */
209static const struct pci_driver i82801ix_azalia __pci_driver = {
210 .ops = &azalia_ops,
211 .vendor = PCI_VENDOR_ID_INTEL,
Felix Singer7f8b0cd2019-11-10 11:04:08 +0100212 .device = PCI_DEVICE_ID_INTEL_82801IB_HD_AUDIO,
Patrick Georgie72a8a32012-11-06 11:05:09 +0100213};