Angel Pons | 6ad9176 | 2020-04-03 01:23:24 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 2 | |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 3 | #include <console/console.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 4 | #include <device/mmio.h> |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 5 | #include <device/device.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 6 | #include <device/pci_ops.h> |
Mario Scheithauer | b83858a | 2017-09-05 15:32:49 +0200 | [diff] [blame] | 7 | #include <device/pci_ids.h> |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 8 | #include <hwilib.h> |
| 9 | #include <i210.h> |
Mario Scheithauer | 0af272c | 2018-04-10 12:40:11 +0200 | [diff] [blame] | 10 | #include <intelblocks/cpulib.h> |
Werner Zeh | 2cb3cc5 | 2020-07-07 09:21:43 +0200 | [diff] [blame] | 11 | #include <intelblocks/fast_spi.h> |
Mario Scheithauer | 0af272c | 2018-04-10 12:40:11 +0200 | [diff] [blame] | 12 | #include <intelblocks/systemagent.h> |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 13 | #include <soc/pci_devs.h> |
| 14 | #include <string.h> |
Werner Zeh | efd0eb3 | 2017-09-12 08:58:44 +0200 | [diff] [blame] | 15 | #include <timer.h> |
Mario Scheithauer | d127be1 | 2018-04-23 10:55:39 +0200 | [diff] [blame] | 16 | #include <baseboard/variants.h> |
Elyes HAOUAS | e39db68 | 2019-05-15 21:12:31 +0200 | [diff] [blame] | 17 | #include <types.h> |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 18 | |
| 19 | #define MAX_PATH_DEPTH 12 |
| 20 | #define MAX_NUM_MAPPINGS 10 |
| 21 | |
Mario Scheithauer | 0af272c | 2018-04-10 12:40:11 +0200 | [diff] [blame] | 22 | #define BIOS_MAILBOX_WAIT_MAX_MS 1000 |
| 23 | #define BIOS_MAILBOX_DATA 0x7080 |
| 24 | #define BIOS_MAILBOX_INTERFACE 0x7084 |
| 25 | #define RUN_BUSY_STS (1 << 31) |
| 26 | |
Mario Scheithauer | 7815c07 | 2019-07-17 09:40:33 +0200 | [diff] [blame] | 27 | #define SD_CAP_BYP 0x810 |
| 28 | #define SD_CAP_BYP_EN 0x5A |
| 29 | #define SD_CAP_BYP_REG1 0x814 |
| 30 | |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 31 | /** \brief This function can decide if a given MAC address is valid or not. |
| 32 | * Currently, addresses filled with 0xff or 0x00 are not valid. |
| 33 | * @param mac Buffer to the MAC address to check |
| 34 | * @return 0 if address is not valid, otherwise 1 |
| 35 | */ |
Angel Pons | c19a9a5 | 2020-11-10 17:08:11 +0100 | [diff] [blame] | 36 | static uint8_t is_mac_adr_valid(uint8_t mac[MAC_ADDR_LEN]) |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 37 | { |
Angel Pons | afb60e7 | 2020-11-10 17:10:01 +0100 | [diff] [blame^] | 38 | for (size_t i = 0; i < MAC_ADDR_LEN; i++) { |
| 39 | if (mac[i] != 0x00 && mac[i] != 0xff) |
| 40 | return 1; |
| 41 | if (mac[i] != mac[0]) |
| 42 | return 1; |
| 43 | } |
| 44 | return 0; |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** \brief This function will search for a MAC address which can be assigned |
| 48 | * to a MACPHY. |
| 49 | * @param dev pointer to PCI device |
| 50 | * @param mac buffer where to store the MAC address |
| 51 | * @return cb_err CB_ERR or CB_SUCCESS |
| 52 | */ |
Angel Pons | c19a9a5 | 2020-11-10 17:08:11 +0100 | [diff] [blame] | 53 | enum cb_err mainboard_get_mac_address(struct device *dev, uint8_t mac[MAC_ADDR_LEN]) |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 54 | { |
| 55 | struct bus *parent = dev->bus; |
| 56 | uint8_t buf[16], mapping[16], i = 0, chain_len = 0; |
| 57 | |
| 58 | memset(buf, 0, sizeof(buf)); |
| 59 | memset(mapping, 0, sizeof(mapping)); |
| 60 | |
| 61 | /* The first entry in the tree is the device itself. */ |
| 62 | buf[0] = dev->path.pci.devfn; |
| 63 | chain_len = 1; |
| 64 | for (i = 1; i < MAX_PATH_DEPTH && parent->dev->bus->subordinate; i++) { |
| 65 | buf[i] = parent->dev->path.pci.devfn; |
| 66 | chain_len++; |
| 67 | parent = parent->dev->bus; |
| 68 | } |
| 69 | if (i == MAX_PATH_DEPTH) { |
| 70 | /* The path is deeper than MAX_PATH_DEPTH devices, error. */ |
| 71 | printk(BIOS_ERR, "Too many bridges for %s\n", dev_path(dev)); |
| 72 | return CB_ERR; |
| 73 | } |
| 74 | /* |
| 75 | * Now construct the mapping based on the device chain starting from |
| 76 | * root bridge device to the device itself. |
| 77 | */ |
| 78 | mapping[0] = 1; |
| 79 | mapping[1] = chain_len; |
| 80 | for (i = 0; i < chain_len; i++) |
| 81 | mapping[i + 4] = buf[chain_len - i - 1]; |
| 82 | |
| 83 | /* Open main hwinfo block */ |
| 84 | if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS) |
| 85 | return CB_ERR; |
Angel Pons | a9db4bd | 2020-11-10 18:17:10 +0100 | [diff] [blame] | 86 | /* Now try to find a valid MAC address in hwinfo for this mapping. */ |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 87 | for (i = 0; i < MAX_NUM_MAPPINGS; i++) { |
Angel Pons | a9db4bd | 2020-11-10 18:17:10 +0100 | [diff] [blame] | 88 | if (hwilib_get_field(XMac1Mapping + i, buf, 16) != 16) |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 89 | continue; |
Angel Pons | a9db4bd | 2020-11-10 18:17:10 +0100 | [diff] [blame] | 90 | if (memcmp(buf, mapping, chain_len + 4)) |
| 91 | continue; |
| 92 | /* There is a matching mapping available, get MAC address. */ |
Angel Pons | c19a9a5 | 2020-11-10 17:08:11 +0100 | [diff] [blame] | 93 | if (hwilib_get_field(XMac1 + i, mac, MAC_ADDR_LEN) == MAC_ADDR_LEN) { |
Angel Pons | a9db4bd | 2020-11-10 18:17:10 +0100 | [diff] [blame] | 94 | if (is_mac_adr_valid(mac)) |
| 95 | return CB_SUCCESS; |
| 96 | } |
| 97 | return CB_ERR; |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 98 | } |
| 99 | /* No MAC address found for */ |
| 100 | return CB_ERR; |
| 101 | } |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 102 | |
Mario Scheithauer | 0af272c | 2018-04-10 12:40:11 +0200 | [diff] [blame] | 103 | /** \brief This function fixes an accuracy issue with IDT PMIC. |
| 104 | * The current reported system power consumption is higher than the |
| 105 | * actual consumption. With a correction of slope and offset for Vcc |
| 106 | * and Vnn, the issue is solved. |
| 107 | */ |
| 108 | static void config_pmic_imon(void) |
| 109 | { |
| 110 | struct stopwatch sw; |
| 111 | uint32_t power_max; |
| 112 | |
| 113 | printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - Start\n"); |
| 114 | |
| 115 | /* Calculate CPU TDP in mW */ |
| 116 | power_max = cpu_get_power_max(); |
| 117 | printk(BIOS_INFO, "PMIC: CPU TDP %d mW.\n", power_max); |
| 118 | |
| 119 | /* |
| 120 | * Fix Vnn slope and offset value. |
| 121 | * slope = 0x4a4 # 2.32 |
| 122 | * offset = 0xfa0d # -2.975 |
| 123 | */ |
| 124 | stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS); |
| 125 | /* Read P_CR_BIOS_MAILBOX_INTERFACE_0_0_0_MCHBAR and check RUN_BUSY. */ |
| 126 | while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) { |
| 127 | if (stopwatch_expired(&sw)) { |
| 128 | printk(BIOS_ERR, "PMIC: Power consumption measurement " |
| 129 | "setup fails for Vnn.\n"); |
| 130 | return; |
| 131 | } |
| 132 | } |
| 133 | /* Set Vnn values into P_CR_BIOS_MAILBOX_DATA_0_0_0_MCHBAR. */ |
| 134 | MCHBAR32(BIOS_MAILBOX_DATA) = 0xfa0d04a4; |
| 135 | /* Set command, address and busy bit. */ |
| 136 | MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000011d; |
| 137 | printk(BIOS_DEBUG, "PMIC: Fix Vnn slope and offset value.\n"); |
| 138 | |
| 139 | /* |
| 140 | * Fix Vcc slope and offset value. |
| 141 | * Premium and High SKU: |
| 142 | * slope = 0x466 # 2.2 |
| 143 | * offset = 0xe833 # -11.9 |
| 144 | * Low and Intermediate SKU: |
| 145 | * slope = 0x3b3 # 1.85 |
| 146 | * offset = 0xed33 # -9.4 |
| 147 | */ |
| 148 | stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS); |
| 149 | while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) { |
| 150 | if (stopwatch_expired(&sw)) { |
| 151 | printk(BIOS_ERR, "PMIC: Power consumption measurement " |
| 152 | "setup fails for Vcc.\n"); |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * CPU TDP limit between Premium/High and Low/Intermediate SKU |
| 159 | * is 9010 mW. |
| 160 | */ |
| 161 | if (power_max > 9010) { |
| 162 | MCHBAR32(BIOS_MAILBOX_DATA) = 0xe8330466; |
| 163 | MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d; |
| 164 | printk(BIOS_INFO, "PMIC: Fix Vcc for Premium SKU.\n"); |
| 165 | } else { |
| 166 | MCHBAR32(BIOS_MAILBOX_DATA) = 0xed3303b3; |
| 167 | MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d; |
| 168 | printk(BIOS_INFO, "PMIC: Fix Vcc for Low SKU.\n"); |
| 169 | } |
| 170 | |
| 171 | printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - End\n"); |
| 172 | } |
| 173 | |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 174 | static void mainboard_init(void *chip_info) |
| 175 | { |
Mario Scheithauer | 2d98120 | 2017-03-27 13:25:57 +0200 | [diff] [blame] | 176 | const struct pad_config *pads; |
| 177 | size_t num; |
| 178 | |
Mario Scheithauer | d127be1 | 2018-04-23 10:55:39 +0200 | [diff] [blame] | 179 | pads = variant_gpio_table(&num); |
Mario Scheithauer | 2d98120 | 2017-03-27 13:25:57 +0200 | [diff] [blame] | 180 | gpio_configure_pads(pads, num); |
Mario Scheithauer | 0af272c | 2018-04-10 12:40:11 +0200 | [diff] [blame] | 181 | |
| 182 | config_pmic_imon(); |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Mario Scheithauer | 956a9f6 | 2017-03-29 17:09:37 +0200 | [diff] [blame] | 185 | static void mainboard_final(void *chip_info) |
| 186 | { |
Elyes HAOUAS | 47503cd | 2018-05-04 21:58:51 +0200 | [diff] [blame] | 187 | struct device *dev = NULL; |
Mario Scheithauer | 956a9f6 | 2017-03-29 17:09:37 +0200 | [diff] [blame] | 188 | |
Mario Scheithauer | 6141353 | 2018-04-25 14:05:09 +0200 | [diff] [blame] | 189 | /* Do board specific things */ |
| 190 | variant_mainboard_final(); |
Mario Scheithauer | b83858a | 2017-09-05 15:32:49 +0200 | [diff] [blame] | 191 | |
| 192 | /* Set Master Enable for on-board PCI device. */ |
| 193 | dev = dev_find_device(PCI_VENDOR_ID_SIEMENS, 0x403f, 0); |
| 194 | if (dev) { |
Angel Pons | 28ed787 | 2020-11-10 20:07:33 +0100 | [diff] [blame] | 195 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER); |
Mario Scheithauer | b83858a | 2017-09-05 15:32:49 +0200 | [diff] [blame] | 196 | } |
Werner Zeh | d5de063 | 2018-09-19 11:06:22 +0200 | [diff] [blame] | 197 | /* Set up SPI OPCODE menu before the controller is locked. */ |
Werner Zeh | 2cb3cc5 | 2020-07-07 09:21:43 +0200 | [diff] [blame] | 198 | fast_spi_set_opcode_menu(); |
Mario Scheithauer | 7815c07 | 2019-07-17 09:40:33 +0200 | [diff] [blame] | 199 | |
| 200 | /* Set SD-Card speed to HS mode only. */ |
| 201 | dev = pcidev_path_on_root(PCH_DEVFN_SDCARD); |
| 202 | if (dev) { |
| 203 | uint32_t reg; |
| 204 | struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 205 | if (!res) |
| 206 | return; |
| 207 | |
| 208 | write32(res2mmio(res, SD_CAP_BYP, 0), SD_CAP_BYP_EN); |
| 209 | reg = read32(res2mmio(res, SD_CAP_BYP_REG1, 0)); |
| 210 | /* Disable all UHS-I SD-Card speed modes, keep only HS mode. */ |
| 211 | reg &= ~0x2000f800; |
| 212 | write32(res2mmio(res, SD_CAP_BYP_REG1, 0), reg); |
| 213 | } |
Mario Scheithauer | 956a9f6 | 2017-03-29 17:09:37 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Mario Scheithauer | 6141353 | 2018-04-25 14:05:09 +0200 | [diff] [blame] | 216 | /* The following function performs board specific things. */ |
| 217 | void __weak variant_mainboard_final(void) |
Werner Zeh | efd0eb3 | 2017-09-12 08:58:44 +0200 | [diff] [blame] | 218 | { |
Werner Zeh | efd0eb3 | 2017-09-12 08:58:44 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 221 | struct chip_operations mainboard_ops = { |
| 222 | .init = mainboard_init, |
Mario Scheithauer | 956a9f6 | 2017-03-29 17:09:37 +0200 | [diff] [blame] | 223 | .final = mainboard_final, |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 224 | }; |