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