blob: 07d0fb14f28b0d6e3a3dc6363cca8ff421c76af5 [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>
Mario Scheithauer092db952017-01-31 15:45:13 +010018#include <device/device.h>
Mario Scheithauerb83858a2017-09-05 15:32:49 +020019#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020020#include <device/pci_ops.h>
Mario Scheithauerb83858a2017-09-05 15:32:49 +020021#include <device/pci_ids.h>
Mario Scheithauer480eab02017-02-16 13:39:16 +010022#include <hwilib.h>
23#include <i210.h>
Mario Scheithauer0af272c2018-04-10 12:40:11 +020024#include <intelblocks/cpulib.h>
Mario Scheithauer0af272c2018-04-10 12:40:11 +020025#include <intelblocks/systemagent.h>
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070026#include <soc/pci_devs.h>
27#include <string.h>
Werner Zehefd0eb32017-09-12 08:58:44 +020028#include <timer.h>
Mario Scheithauerd127be12018-04-23 10:55:39 +020029#include <baseboard/variants.h>
Mario Scheithauer480eab02017-02-16 13:39:16 +010030
31#define MAX_PATH_DEPTH 12
32#define MAX_NUM_MAPPINGS 10
33
Mario Scheithauer0af272c2018-04-10 12:40:11 +020034#define BIOS_MAILBOX_WAIT_MAX_MS 1000
35#define BIOS_MAILBOX_DATA 0x7080
36#define BIOS_MAILBOX_INTERFACE 0x7084
37#define RUN_BUSY_STS (1 << 31)
38
Werner Zehd5de0632018-09-19 11:06:22 +020039/*
40 * SPI Opcode Menu setup for SPIBAR lock down
41 * should support most common flash chips.
42 */
43#define SPI_OPMENU_0 0x01 /* WRSR: Write Status Register */
44#define SPI_OPTYPE_0 0x01 /* Write, no address */
45
46#define SPI_OPMENU_1 0x02 /* PP: Page Program */
47#define SPI_OPTYPE_1 0x03 /* Write, address required */
48
49#define SPI_OPMENU_2 0x03 /* READ: Read Data */
50#define SPI_OPTYPE_2 0x02 /* Read, address required */
51
52#define SPI_OPMENU_3 0x05 /* RDSR: Read Status Register */
53#define SPI_OPTYPE_3 0x00 /* Read, no address */
54
55#define SPI_OPMENU_4 0x20 /* SE20: Sector Erase 0x20 */
56#define SPI_OPTYPE_4 0x03 /* Write, address required */
57
58#define SPI_OPMENU_5 0x9f /* RDID: Read ID */
59#define SPI_OPTYPE_5 0x00 /* Read, no address */
60
61#define SPI_OPMENU_6 0xd8 /* BED8: Block Erase 0xd8 */
62#define SPI_OPTYPE_6 0x03 /* Write, address required */
63
64#define SPI_OPMENU_7 0x0b /* FAST: Fast Read */
65#define SPI_OPTYPE_7 0x02 /* Read, address required */
66
67#define SPI_OPMENU_UPPER ((SPI_OPMENU_7 << 24) | (SPI_OPMENU_6 << 16) | \
68 (SPI_OPMENU_5 << 8) | SPI_OPMENU_4)
69#define SPI_OPMENU_LOWER ((SPI_OPMENU_3 << 24) | (SPI_OPMENU_2 << 16) | \
70 (SPI_OPMENU_1 << 8) | SPI_OPMENU_0)
71
72#define SPI_OPTYPE ((SPI_OPTYPE_7 << 14) | (SPI_OPTYPE_6 << 12) | \
73 (SPI_OPTYPE_5 << 10) | (SPI_OPTYPE_4 << 8) | \
74 (SPI_OPTYPE_3 << 6) | (SPI_OPTYPE_2 << 4) | \
75 (SPI_OPTYPE_1 << 2) | (SPI_OPTYPE_0))
76
77#define SPI_OPPREFIX ((0x50 << 8) | 0x06) /* EWSR and WREN */
78
79#define SPIBAR_OFFSET 0x3800
80#define SPI_REG_PREOP_OPTYPE 0xa4
81#define SPI_REG_OPMENU_L 0xa8
82#define SPI_REG_OPMENU_H 0xac
83
Mario Scheithauer480eab02017-02-16 13:39:16 +010084/** \brief This function can decide if a given MAC address is valid or not.
85 * Currently, addresses filled with 0xff or 0x00 are not valid.
86 * @param mac Buffer to the MAC address to check
87 * @return 0 if address is not valid, otherwise 1
88 */
89static uint8_t is_mac_adr_valid(uint8_t mac[6])
90{
91 uint8_t buf[6];
92
93 memset(buf, 0, sizeof(buf));
94 if (!memcmp(buf, mac, sizeof(buf)))
95 return 0;
96 memset(buf, 0xff, sizeof(buf));
97 if (!memcmp(buf, mac, sizeof(buf)))
98 return 0;
99 return 1;
100}
101
102/** \brief This function will search for a MAC address which can be assigned
103 * to a MACPHY.
104 * @param dev pointer to PCI device
105 * @param mac buffer where to store the MAC address
106 * @return cb_err CB_ERR or CB_SUCCESS
107 */
108enum cb_err mainboard_get_mac_address(struct device *dev, uint8_t mac[6])
109{
110 struct bus *parent = dev->bus;
111 uint8_t buf[16], mapping[16], i = 0, chain_len = 0;
112
113 memset(buf, 0, sizeof(buf));
114 memset(mapping, 0, sizeof(mapping));
115
116 /* The first entry in the tree is the device itself. */
117 buf[0] = dev->path.pci.devfn;
118 chain_len = 1;
119 for (i = 1; i < MAX_PATH_DEPTH && parent->dev->bus->subordinate; i++) {
120 buf[i] = parent->dev->path.pci.devfn;
121 chain_len++;
122 parent = parent->dev->bus;
123 }
124 if (i == MAX_PATH_DEPTH) {
125 /* The path is deeper than MAX_PATH_DEPTH devices, error. */
126 printk(BIOS_ERR, "Too many bridges for %s\n", dev_path(dev));
127 return CB_ERR;
128 }
129 /*
130 * Now construct the mapping based on the device chain starting from
131 * root bridge device to the device itself.
132 */
133 mapping[0] = 1;
134 mapping[1] = chain_len;
135 for (i = 0; i < chain_len; i++)
136 mapping[i + 4] = buf[chain_len - i - 1];
137
138 /* Open main hwinfo block */
139 if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS)
140 return CB_ERR;
141 /* Now try to find a valid MAC address in hwinfo for this mapping.*/
142 for (i = 0; i < MAX_NUM_MAPPINGS; i++) {
143 if ((hwilib_get_field(XMac1Mapping + i, buf, 16) == 16) &&
144 !(memcmp(buf, mapping, chain_len + 4))) {
145 /* There is a matching mapping available, get MAC address. */
146 if ((hwilib_get_field(XMac1 + i, mac, 6) == 6) &&
147 (is_mac_adr_valid(mac))) {
148 return CB_SUCCESS;
149 } else {
150 return CB_ERR;
151 }
152 } else
153 continue;
154 }
155 /* No MAC address found for */
156 return CB_ERR;
157}
Mario Scheithauer092db952017-01-31 15:45:13 +0100158
Mario Scheithauer0af272c2018-04-10 12:40:11 +0200159/** \brief This function fixes an accuracy issue with IDT PMIC.
160 * The current reported system power consumption is higher than the
161 * actual consumption. With a correction of slope and offset for Vcc
162 * and Vnn, the issue is solved.
163 */
164static void config_pmic_imon(void)
165{
166 struct stopwatch sw;
167 uint32_t power_max;
168
169 printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - Start\n");
170
171 /* Calculate CPU TDP in mW */
172 power_max = cpu_get_power_max();
173 printk(BIOS_INFO, "PMIC: CPU TDP %d mW.\n", power_max);
174
175 /*
176 * Fix Vnn slope and offset value.
177 * slope = 0x4a4 # 2.32
178 * offset = 0xfa0d # -2.975
179 */
180 stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS);
181 /* Read P_CR_BIOS_MAILBOX_INTERFACE_0_0_0_MCHBAR and check RUN_BUSY. */
182 while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) {
183 if (stopwatch_expired(&sw)) {
184 printk(BIOS_ERR, "PMIC: Power consumption measurement "
185 "setup fails for Vnn.\n");
186 return;
187 }
188 }
189 /* Set Vnn values into P_CR_BIOS_MAILBOX_DATA_0_0_0_MCHBAR. */
190 MCHBAR32(BIOS_MAILBOX_DATA) = 0xfa0d04a4;
191 /* Set command, address and busy bit. */
192 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000011d;
193 printk(BIOS_DEBUG, "PMIC: Fix Vnn slope and offset value.\n");
194
195 /*
196 * Fix Vcc slope and offset value.
197 * Premium and High SKU:
198 * slope = 0x466 # 2.2
199 * offset = 0xe833 # -11.9
200 * Low and Intermediate SKU:
201 * slope = 0x3b3 # 1.85
202 * offset = 0xed33 # -9.4
203 */
204 stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS);
205 while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) {
206 if (stopwatch_expired(&sw)) {
207 printk(BIOS_ERR, "PMIC: Power consumption measurement "
208 "setup fails for Vcc.\n");
209 return;
210 }
211 }
212
213 /*
214 * CPU TDP limit between Premium/High and Low/Intermediate SKU
215 * is 9010 mW.
216 */
217 if (power_max > 9010) {
218 MCHBAR32(BIOS_MAILBOX_DATA) = 0xe8330466;
219 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d;
220 printk(BIOS_INFO, "PMIC: Fix Vcc for Premium SKU.\n");
221 } else {
222 MCHBAR32(BIOS_MAILBOX_DATA) = 0xed3303b3;
223 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d;
224 printk(BIOS_INFO, "PMIC: Fix Vcc for Low SKU.\n");
225 }
226
227 printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - End\n");
228}
229
Mario Scheithauer092db952017-01-31 15:45:13 +0100230static void mainboard_init(void *chip_info)
231{
Mario Scheithauer2d981202017-03-27 13:25:57 +0200232 const struct pad_config *pads;
233 size_t num;
234
Mario Scheithauerd127be12018-04-23 10:55:39 +0200235 pads = variant_gpio_table(&num);
Mario Scheithauer2d981202017-03-27 13:25:57 +0200236 gpio_configure_pads(pads, num);
Mario Scheithauer0af272c2018-04-10 12:40:11 +0200237
238 config_pmic_imon();
Mario Scheithauer092db952017-01-31 15:45:13 +0100239}
240
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200241static void mainboard_final(void *chip_info)
242{
Mario Scheithauerb83858a2017-09-05 15:32:49 +0200243 uint16_t cmd = 0;
Elyes HAOUAS47503cd2018-05-04 21:58:51 +0200244 struct device *dev = NULL;
Werner Zehd5de0632018-09-19 11:06:22 +0200245 void *spi_base = NULL;
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200246
Mario Scheithauer61413532018-04-25 14:05:09 +0200247 /* Do board specific things */
248 variant_mainboard_final();
Mario Scheithauerb83858a2017-09-05 15:32:49 +0200249
250 /* Set Master Enable for on-board PCI device. */
251 dev = dev_find_device(PCI_VENDOR_ID_SIEMENS, 0x403f, 0);
252 if (dev) {
253 cmd = pci_read_config16(dev, PCI_COMMAND);
254 cmd |= PCI_COMMAND_MASTER;
255 pci_write_config16(dev, PCI_COMMAND, cmd);
256 }
Werner Zehd5de0632018-09-19 11:06:22 +0200257 /* Set up SPI OPCODE menu before the controller is locked. */
258 dev = PCH_DEV_SPI;
259 spi_base = (void *)pci_read_config32(dev, PCI_BASE_ADDRESS_0);
260 if (!spi_base)
261 return;
262 write32((spi_base + SPI_REG_PREOP_OPTYPE),
263 ((SPI_OPTYPE << 16) | SPI_OPPREFIX));
264 write32((spi_base + SPI_REG_OPMENU_L), SPI_OPMENU_LOWER);
265 write32((spi_base + SPI_REG_OPMENU_H), SPI_OPMENU_UPPER);
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200266}
267
Mario Scheithauer61413532018-04-25 14:05:09 +0200268/* The following function performs board specific things. */
269void __weak variant_mainboard_final(void)
Werner Zehefd0eb32017-09-12 08:58:44 +0200270{
Werner Zehefd0eb32017-09-12 08:58:44 +0200271}
272
Mario Scheithauer092db952017-01-31 15:45:13 +0100273struct chip_operations mainboard_ops = {
274 .init = mainboard_init,
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200275 .final = mainboard_final,
Mario Scheithauer092db952017-01-31 15:45:13 +0100276};