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