Angel Pons | 0612b27 | 2020-04-05 15:46:56 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
Elyes Haouas | 8ed5835 | 2022-10-22 22:17:28 +0200 | [diff] [blame] | 4 | #include <delay.h> |
Elyes HAOUAS | 899d5bde | 2020-08-03 15:00:46 +0200 | [diff] [blame] | 5 | #include <device/azalia_device.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 6 | #include <device/mmio.h> |
Elyes Haouas | 8ed5835 | 2022-10-22 22:17:28 +0200 | [diff] [blame] | 7 | #include <stdint.h> |
Elyes HAOUAS | 899d5bde | 2020-08-03 15:00:46 +0200 | [diff] [blame] | 8 | |
Duncan Laurie | f0aaa29 | 2014-04-22 10:48:29 -0700 | [diff] [blame] | 9 | #include "hda_verb.h" |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 10 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 11 | int hda_codec_detect(u8 *base) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 12 | { |
| 13 | u8 reg8; |
| 14 | |
| 15 | /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */ |
Angel Pons | 7f839f6 | 2020-12-05 19:02:14 +0100 | [diff] [blame] | 16 | if (azalia_exit_reset(base) < 0) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 17 | goto no_codec; |
| 18 | |
| 19 | /* Write back the value once reset bit is set. */ |
| 20 | write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG)); |
| 21 | |
Angel Pons | aa566ad | 2021-03-18 17:08:09 +0100 | [diff] [blame] | 22 | /* |
| 23 | * Clear the "State Change Status Register" STATESTS bits |
Kein Yuan | d3b40bf | 2014-02-11 17:40:31 -0800 | [diff] [blame] | 24 | * for each of the "SDIN Stat Change Status Flag" |
Lee Leahy | 0946ec3 | 2015-04-20 15:24:54 -0700 | [diff] [blame] | 25 | */ |
Kein Yuan | d3b40bf | 2014-02-11 17:40:31 -0800 | [diff] [blame] | 26 | write8(base + HDA_STATESTS_REG, 0xf); |
| 27 | |
| 28 | /* Turn off the link and poll RESET# bit until it reads back as 0 */ |
Angel Pons | 2e0053b | 2020-12-05 19:06:55 +0100 | [diff] [blame] | 29 | if (azalia_enter_reset(base) < 0) |
Kein Yuan | d3b40bf | 2014-02-11 17:40:31 -0800 | [diff] [blame] | 30 | goto no_codec; |
| 31 | |
| 32 | /* Turn on the link and poll RESET# bit until it reads back as 1 */ |
Angel Pons | 7f839f6 | 2020-12-05 19:02:14 +0100 | [diff] [blame] | 33 | if (azalia_exit_reset(base) < 0) |
Kein Yuan | d3b40bf | 2014-02-11 17:40:31 -0800 | [diff] [blame] | 34 | goto no_codec; |
| 35 | |
Angel Pons | aa566ad | 2021-03-18 17:08:09 +0100 | [diff] [blame] | 36 | /* Read in Codec location (BAR + 0xe)[2..0] */ |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 37 | reg8 = read8(base + HDA_STATESTS_REG); |
| 38 | reg8 &= 0x0f; |
| 39 | if (!reg8) |
| 40 | goto no_codec; |
| 41 | |
| 42 | return reg8; |
| 43 | |
| 44 | no_codec: |
Angel Pons | 2e0053b | 2020-12-05 19:06:55 +0100 | [diff] [blame] | 45 | /* Codec not found, put HDA back in reset */ |
| 46 | azalia_enter_reset(base); |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 47 | printk(BIOS_DEBUG, "HDA: No codec!\n"); |
| 48 | return 0; |
| 49 | } |
| 50 | |
Subrata Banik | 5885ffe | 2019-11-14 11:08:51 +0530 | [diff] [blame] | 51 | /* |
Elyes HAOUAS | a302205 | 2020-08-11 16:47:47 +0200 | [diff] [blame] | 52 | * Wait 50usec for the codec to indicate it is ready. |
| 53 | * No response would imply that the codec is non-operative. |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 54 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 55 | static int hda_wait_for_ready(u8 *base) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 56 | { |
Elyes HAOUAS | a302205 | 2020-08-11 16:47:47 +0200 | [diff] [blame] | 57 | /* Use a 50 usec timeout - the Linux kernel uses the same duration */ |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 58 | int timeout = 50; |
| 59 | |
Elyes HAOUAS | 4a83f1c | 2016-08-25 21:07:59 +0200 | [diff] [blame] | 60 | while (timeout--) { |
Elyes HAOUAS | a302205 | 2020-08-11 16:47:47 +0200 | [diff] [blame] | 61 | u32 reg32 = read32(base + HDA_ICII_REG); |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 62 | if (!(reg32 & HDA_ICII_BUSY)) |
| 63 | return 0; |
| 64 | udelay(1); |
| 65 | } |
| 66 | |
| 67 | return -1; |
| 68 | } |
| 69 | |
Subrata Banik | 5885ffe | 2019-11-14 11:08:51 +0530 | [diff] [blame] | 70 | /* |
Elyes HAOUAS | a302205 | 2020-08-11 16:47:47 +0200 | [diff] [blame] | 71 | * Wait 50usec for the codec to indicate that it accepted the previous command. |
| 72 | * No response would imply that the code is non-operative. |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 73 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 74 | static int hda_wait_for_valid(u8 *base) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 75 | { |
| 76 | u32 reg32; |
Elyes HAOUAS | a302205 | 2020-08-11 16:47:47 +0200 | [diff] [blame] | 77 | /* Use a 50 usec timeout - the Linux kernel uses the same duration */ |
| 78 | int timeout = 50; |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 79 | |
| 80 | /* Send the verb to the codec */ |
| 81 | reg32 = read32(base + HDA_ICII_REG); |
| 82 | reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID; |
| 83 | write32(base + HDA_ICII_REG, reg32); |
| 84 | |
Elyes HAOUAS | 4a83f1c | 2016-08-25 21:07:59 +0200 | [diff] [blame] | 85 | while (timeout--) { |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 86 | reg32 = read32(base + HDA_ICII_REG); |
Elyes HAOUAS | a302205 | 2020-08-11 16:47:47 +0200 | [diff] [blame] | 87 | if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 88 | return 0; |
| 89 | udelay(1); |
| 90 | } |
| 91 | |
| 92 | return -1; |
| 93 | } |
| 94 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 95 | int hda_codec_write(u8 *base, u32 size, const u32 *data) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 96 | { |
| 97 | int i; |
| 98 | |
| 99 | for (i = 0; i < size; i++) { |
| 100 | if (hda_wait_for_ready(base) < 0) |
| 101 | return -1; |
| 102 | |
| 103 | write32(base + HDA_IC_REG, data[i]); |
| 104 | |
| 105 | if (hda_wait_for_valid(base) < 0) |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 112 | int hda_codec_init(u8 *base, int addr, int verb_size, const u32 *verb_data) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 113 | { |
| 114 | const u32 *verb; |
| 115 | u32 reg32, size; |
| 116 | int rc; |
| 117 | |
| 118 | printk(BIOS_DEBUG, "HDA: Initializing codec #%d\n", addr); |
| 119 | |
| 120 | if (!verb_size || !verb_data) { |
| 121 | printk(BIOS_DEBUG, "HDA: No verb list!\n"); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | /* 1 */ |
| 126 | if (hda_wait_for_ready(base) < 0) { |
| 127 | printk(BIOS_DEBUG, " codec not ready.\n"); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | reg32 = (addr << 28) | 0x000f0000; |
| 132 | write32(base + HDA_IC_REG, reg32); |
| 133 | |
| 134 | if (hda_wait_for_valid(base) < 0) { |
| 135 | printk(BIOS_DEBUG, " codec not valid.\n"); |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | /* 2 */ |
| 140 | reg32 = read32(base + HDA_IR_REG); |
| 141 | printk(BIOS_DEBUG, "HDA: codec viddid: %08x\n", reg32); |
| 142 | |
Angel Pons | d3f7028 | 2020-12-05 18:28:33 +0100 | [diff] [blame] | 143 | size = azalia_find_verb(verb_data, verb_size, reg32, &verb); |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 144 | if (!size) { |
| 145 | printk(BIOS_DEBUG, "HDA: No verb table entry found\n"); |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | /* 3 */ |
| 150 | rc = hda_codec_write(base, size, verb); |
| 151 | |
| 152 | if (rc < 0) |
| 153 | printk(BIOS_DEBUG, "HDA: verb not loaded\n"); |
| 154 | else |
| 155 | printk(BIOS_DEBUG, "HDA: verb loaded.\n"); |
| 156 | |
| 157 | return rc; |
| 158 | } |