Angel Pons | 6ad9176 | 2020-04-03 01:23:24 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 3 | |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 4 | #include <console/console.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 5 | #include <device/mmio.h> |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 6 | #include <device/device.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 7 | #include <device/pci_ops.h> |
Mario Scheithauer | b83858a | 2017-09-05 15:32:49 +0200 | [diff] [blame] | 8 | #include <device/pci_ids.h> |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 9 | #include <hwilib.h> |
| 10 | #include <i210.h> |
Mario Scheithauer | 0af272c | 2018-04-10 12:40:11 +0200 | [diff] [blame] | 11 | #include <intelblocks/cpulib.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 | |
Werner Zeh | d5de063 | 2018-09-19 11:06:22 +0200 | [diff] [blame] | 27 | /* |
| 28 | * SPI Opcode Menu setup for SPIBAR lock down |
| 29 | * should support most common flash chips. |
| 30 | */ |
| 31 | #define SPI_OPMENU_0 0x01 /* WRSR: Write Status Register */ |
| 32 | #define SPI_OPTYPE_0 0x01 /* Write, no address */ |
| 33 | |
| 34 | #define SPI_OPMENU_1 0x02 /* PP: Page Program */ |
| 35 | #define SPI_OPTYPE_1 0x03 /* Write, address required */ |
| 36 | |
| 37 | #define SPI_OPMENU_2 0x03 /* READ: Read Data */ |
| 38 | #define SPI_OPTYPE_2 0x02 /* Read, address required */ |
| 39 | |
| 40 | #define SPI_OPMENU_3 0x05 /* RDSR: Read Status Register */ |
| 41 | #define SPI_OPTYPE_3 0x00 /* Read, no address */ |
| 42 | |
| 43 | #define SPI_OPMENU_4 0x20 /* SE20: Sector Erase 0x20 */ |
| 44 | #define SPI_OPTYPE_4 0x03 /* Write, address required */ |
| 45 | |
| 46 | #define SPI_OPMENU_5 0x9f /* RDID: Read ID */ |
| 47 | #define SPI_OPTYPE_5 0x00 /* Read, no address */ |
| 48 | |
| 49 | #define SPI_OPMENU_6 0xd8 /* BED8: Block Erase 0xd8 */ |
| 50 | #define SPI_OPTYPE_6 0x03 /* Write, address required */ |
| 51 | |
| 52 | #define SPI_OPMENU_7 0x0b /* FAST: Fast Read */ |
| 53 | #define SPI_OPTYPE_7 0x02 /* Read, address required */ |
| 54 | |
| 55 | #define SPI_OPMENU_UPPER ((SPI_OPMENU_7 << 24) | (SPI_OPMENU_6 << 16) | \ |
| 56 | (SPI_OPMENU_5 << 8) | SPI_OPMENU_4) |
| 57 | #define SPI_OPMENU_LOWER ((SPI_OPMENU_3 << 24) | (SPI_OPMENU_2 << 16) | \ |
| 58 | (SPI_OPMENU_1 << 8) | SPI_OPMENU_0) |
| 59 | |
| 60 | #define SPI_OPTYPE ((SPI_OPTYPE_7 << 14) | (SPI_OPTYPE_6 << 12) | \ |
| 61 | (SPI_OPTYPE_5 << 10) | (SPI_OPTYPE_4 << 8) | \ |
| 62 | (SPI_OPTYPE_3 << 6) | (SPI_OPTYPE_2 << 4) | \ |
| 63 | (SPI_OPTYPE_1 << 2) | (SPI_OPTYPE_0)) |
| 64 | |
| 65 | #define SPI_OPPREFIX ((0x50 << 8) | 0x06) /* EWSR and WREN */ |
| 66 | |
| 67 | #define SPIBAR_OFFSET 0x3800 |
| 68 | #define SPI_REG_PREOP_OPTYPE 0xa4 |
| 69 | #define SPI_REG_OPMENU_L 0xa8 |
| 70 | #define SPI_REG_OPMENU_H 0xac |
| 71 | |
Mario Scheithauer | 7815c07 | 2019-07-17 09:40:33 +0200 | [diff] [blame] | 72 | #define SD_CAP_BYP 0x810 |
| 73 | #define SD_CAP_BYP_EN 0x5A |
| 74 | #define SD_CAP_BYP_REG1 0x814 |
| 75 | |
Mario Scheithauer | 480eab0 | 2017-02-16 13:39:16 +0100 | [diff] [blame] | 76 | /** \brief This function can decide if a given MAC address is valid or not. |
| 77 | * Currently, addresses filled with 0xff or 0x00 are not valid. |
| 78 | * @param mac Buffer to the MAC address to check |
| 79 | * @return 0 if address is not valid, otherwise 1 |
| 80 | */ |
| 81 | static uint8_t is_mac_adr_valid(uint8_t mac[6]) |
| 82 | { |
| 83 | uint8_t buf[6]; |
| 84 | |
| 85 | memset(buf, 0, sizeof(buf)); |
| 86 | if (!memcmp(buf, mac, sizeof(buf))) |
| 87 | return 0; |
| 88 | memset(buf, 0xff, sizeof(buf)); |
| 89 | if (!memcmp(buf, mac, sizeof(buf))) |
| 90 | return 0; |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | /** \brief This function will search for a MAC address which can be assigned |
| 95 | * to a MACPHY. |
| 96 | * @param dev pointer to PCI device |
| 97 | * @param mac buffer where to store the MAC address |
| 98 | * @return cb_err CB_ERR or CB_SUCCESS |
| 99 | */ |
| 100 | enum cb_err mainboard_get_mac_address(struct device *dev, uint8_t mac[6]) |
| 101 | { |
| 102 | struct bus *parent = dev->bus; |
| 103 | uint8_t buf[16], mapping[16], i = 0, chain_len = 0; |
| 104 | |
| 105 | memset(buf, 0, sizeof(buf)); |
| 106 | memset(mapping, 0, sizeof(mapping)); |
| 107 | |
| 108 | /* The first entry in the tree is the device itself. */ |
| 109 | buf[0] = dev->path.pci.devfn; |
| 110 | chain_len = 1; |
| 111 | for (i = 1; i < MAX_PATH_DEPTH && parent->dev->bus->subordinate; i++) { |
| 112 | buf[i] = parent->dev->path.pci.devfn; |
| 113 | chain_len++; |
| 114 | parent = parent->dev->bus; |
| 115 | } |
| 116 | if (i == MAX_PATH_DEPTH) { |
| 117 | /* The path is deeper than MAX_PATH_DEPTH devices, error. */ |
| 118 | printk(BIOS_ERR, "Too many bridges for %s\n", dev_path(dev)); |
| 119 | return CB_ERR; |
| 120 | } |
| 121 | /* |
| 122 | * Now construct the mapping based on the device chain starting from |
| 123 | * root bridge device to the device itself. |
| 124 | */ |
| 125 | mapping[0] = 1; |
| 126 | mapping[1] = chain_len; |
| 127 | for (i = 0; i < chain_len; i++) |
| 128 | mapping[i + 4] = buf[chain_len - i - 1]; |
| 129 | |
| 130 | /* Open main hwinfo block */ |
| 131 | if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS) |
| 132 | return CB_ERR; |
| 133 | /* Now try to find a valid MAC address in hwinfo for this mapping.*/ |
| 134 | for (i = 0; i < MAX_NUM_MAPPINGS; i++) { |
| 135 | if ((hwilib_get_field(XMac1Mapping + i, buf, 16) == 16) && |
| 136 | !(memcmp(buf, mapping, chain_len + 4))) { |
| 137 | /* There is a matching mapping available, get MAC address. */ |
| 138 | if ((hwilib_get_field(XMac1 + i, mac, 6) == 6) && |
| 139 | (is_mac_adr_valid(mac))) { |
| 140 | return CB_SUCCESS; |
| 141 | } else { |
| 142 | return CB_ERR; |
| 143 | } |
| 144 | } else |
| 145 | continue; |
| 146 | } |
| 147 | /* No MAC address found for */ |
| 148 | return CB_ERR; |
| 149 | } |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 150 | |
Mario Scheithauer | 0af272c | 2018-04-10 12:40:11 +0200 | [diff] [blame] | 151 | /** \brief This function fixes an accuracy issue with IDT PMIC. |
| 152 | * The current reported system power consumption is higher than the |
| 153 | * actual consumption. With a correction of slope and offset for Vcc |
| 154 | * and Vnn, the issue is solved. |
| 155 | */ |
| 156 | static void config_pmic_imon(void) |
| 157 | { |
| 158 | struct stopwatch sw; |
| 159 | uint32_t power_max; |
| 160 | |
| 161 | printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - Start\n"); |
| 162 | |
| 163 | /* Calculate CPU TDP in mW */ |
| 164 | power_max = cpu_get_power_max(); |
| 165 | printk(BIOS_INFO, "PMIC: CPU TDP %d mW.\n", power_max); |
| 166 | |
| 167 | /* |
| 168 | * Fix Vnn slope and offset value. |
| 169 | * slope = 0x4a4 # 2.32 |
| 170 | * offset = 0xfa0d # -2.975 |
| 171 | */ |
| 172 | stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS); |
| 173 | /* Read P_CR_BIOS_MAILBOX_INTERFACE_0_0_0_MCHBAR and check RUN_BUSY. */ |
| 174 | while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) { |
| 175 | if (stopwatch_expired(&sw)) { |
| 176 | printk(BIOS_ERR, "PMIC: Power consumption measurement " |
| 177 | "setup fails for Vnn.\n"); |
| 178 | return; |
| 179 | } |
| 180 | } |
| 181 | /* Set Vnn values into P_CR_BIOS_MAILBOX_DATA_0_0_0_MCHBAR. */ |
| 182 | MCHBAR32(BIOS_MAILBOX_DATA) = 0xfa0d04a4; |
| 183 | /* Set command, address and busy bit. */ |
| 184 | MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000011d; |
| 185 | printk(BIOS_DEBUG, "PMIC: Fix Vnn slope and offset value.\n"); |
| 186 | |
| 187 | /* |
| 188 | * Fix Vcc slope and offset value. |
| 189 | * Premium and High SKU: |
| 190 | * slope = 0x466 # 2.2 |
| 191 | * offset = 0xe833 # -11.9 |
| 192 | * Low and Intermediate SKU: |
| 193 | * slope = 0x3b3 # 1.85 |
| 194 | * offset = 0xed33 # -9.4 |
| 195 | */ |
| 196 | stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS); |
| 197 | while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) { |
| 198 | if (stopwatch_expired(&sw)) { |
| 199 | printk(BIOS_ERR, "PMIC: Power consumption measurement " |
| 200 | "setup fails for Vcc.\n"); |
| 201 | return; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * CPU TDP limit between Premium/High and Low/Intermediate SKU |
| 207 | * is 9010 mW. |
| 208 | */ |
| 209 | if (power_max > 9010) { |
| 210 | MCHBAR32(BIOS_MAILBOX_DATA) = 0xe8330466; |
| 211 | MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d; |
| 212 | printk(BIOS_INFO, "PMIC: Fix Vcc for Premium SKU.\n"); |
| 213 | } else { |
| 214 | MCHBAR32(BIOS_MAILBOX_DATA) = 0xed3303b3; |
| 215 | MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d; |
| 216 | printk(BIOS_INFO, "PMIC: Fix Vcc for Low SKU.\n"); |
| 217 | } |
| 218 | |
| 219 | printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - End\n"); |
| 220 | } |
| 221 | |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 222 | static void mainboard_init(void *chip_info) |
| 223 | { |
Mario Scheithauer | 2d98120 | 2017-03-27 13:25:57 +0200 | [diff] [blame] | 224 | const struct pad_config *pads; |
| 225 | size_t num; |
| 226 | |
Mario Scheithauer | d127be1 | 2018-04-23 10:55:39 +0200 | [diff] [blame] | 227 | pads = variant_gpio_table(&num); |
Mario Scheithauer | 2d98120 | 2017-03-27 13:25:57 +0200 | [diff] [blame] | 228 | gpio_configure_pads(pads, num); |
Mario Scheithauer | 0af272c | 2018-04-10 12:40:11 +0200 | [diff] [blame] | 229 | |
| 230 | config_pmic_imon(); |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Mario Scheithauer | 956a9f6 | 2017-03-29 17:09:37 +0200 | [diff] [blame] | 233 | static void mainboard_final(void *chip_info) |
| 234 | { |
Mario Scheithauer | b83858a | 2017-09-05 15:32:49 +0200 | [diff] [blame] | 235 | uint16_t cmd = 0; |
Elyes HAOUAS | 47503cd | 2018-05-04 21:58:51 +0200 | [diff] [blame] | 236 | struct device *dev = NULL; |
Werner Zeh | d5de063 | 2018-09-19 11:06:22 +0200 | [diff] [blame] | 237 | void *spi_base = NULL; |
Mario Scheithauer | 956a9f6 | 2017-03-29 17:09:37 +0200 | [diff] [blame] | 238 | |
Mario Scheithauer | 6141353 | 2018-04-25 14:05:09 +0200 | [diff] [blame] | 239 | /* Do board specific things */ |
| 240 | variant_mainboard_final(); |
Mario Scheithauer | b83858a | 2017-09-05 15:32:49 +0200 | [diff] [blame] | 241 | |
| 242 | /* Set Master Enable for on-board PCI device. */ |
| 243 | dev = dev_find_device(PCI_VENDOR_ID_SIEMENS, 0x403f, 0); |
| 244 | if (dev) { |
| 245 | cmd = pci_read_config16(dev, PCI_COMMAND); |
| 246 | cmd |= PCI_COMMAND_MASTER; |
| 247 | pci_write_config16(dev, PCI_COMMAND, cmd); |
| 248 | } |
Werner Zeh | d5de063 | 2018-09-19 11:06:22 +0200 | [diff] [blame] | 249 | /* Set up SPI OPCODE menu before the controller is locked. */ |
| 250 | dev = PCH_DEV_SPI; |
| 251 | spi_base = (void *)pci_read_config32(dev, PCI_BASE_ADDRESS_0); |
| 252 | if (!spi_base) |
| 253 | return; |
| 254 | write32((spi_base + SPI_REG_PREOP_OPTYPE), |
| 255 | ((SPI_OPTYPE << 16) | SPI_OPPREFIX)); |
| 256 | write32((spi_base + SPI_REG_OPMENU_L), SPI_OPMENU_LOWER); |
| 257 | write32((spi_base + SPI_REG_OPMENU_H), SPI_OPMENU_UPPER); |
Mario Scheithauer | 7815c07 | 2019-07-17 09:40:33 +0200 | [diff] [blame] | 258 | |
| 259 | /* Set SD-Card speed to HS mode only. */ |
| 260 | dev = pcidev_path_on_root(PCH_DEVFN_SDCARD); |
| 261 | if (dev) { |
| 262 | uint32_t reg; |
| 263 | struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 264 | if (!res) |
| 265 | return; |
| 266 | |
| 267 | write32(res2mmio(res, SD_CAP_BYP, 0), SD_CAP_BYP_EN); |
| 268 | reg = read32(res2mmio(res, SD_CAP_BYP_REG1, 0)); |
| 269 | /* Disable all UHS-I SD-Card speed modes, keep only HS mode. */ |
| 270 | reg &= ~0x2000f800; |
| 271 | write32(res2mmio(res, SD_CAP_BYP_REG1, 0), reg); |
| 272 | } |
Mario Scheithauer | 956a9f6 | 2017-03-29 17:09:37 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Mario Scheithauer | 6141353 | 2018-04-25 14:05:09 +0200 | [diff] [blame] | 275 | /* The following function performs board specific things. */ |
| 276 | void __weak variant_mainboard_final(void) |
Werner Zeh | efd0eb3 | 2017-09-12 08:58:44 +0200 | [diff] [blame] | 277 | { |
Werner Zeh | efd0eb3 | 2017-09-12 08:58:44 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 280 | struct chip_operations mainboard_ops = { |
| 281 | .init = mainboard_init, |
Mario Scheithauer | 956a9f6 | 2017-03-29 17:09:37 +0200 | [diff] [blame] | 282 | .final = mainboard_final, |
Mario Scheithauer | 092db95 | 2017-01-31 15:45:13 +0100 | [diff] [blame] | 283 | }; |