blob: b39b6817f6ec213c98ecd90e8ed485d31072a5b0 [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>
Mario Scheithauer480eab02017-02-16 13:39:16 +010031
32#define MAX_PATH_DEPTH 12
33#define MAX_NUM_MAPPINGS 10
34
Mario Scheithauer0af272c2018-04-10 12:40:11 +020035#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 Zehd5de0632018-09-19 11:06:22 +020040/*
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 Scheithauer480eab02017-02-16 13:39:16 +010085/** \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 */
90static 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 */
109enum 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 Scheithauer092db952017-01-31 15:45:13 +0100159
Mario Scheithauer0af272c2018-04-10 12:40:11 +0200160/** \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 */
165static 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 Scheithauer092db952017-01-31 15:45:13 +0100231static void mainboard_init(void *chip_info)
232{
Mario Scheithauer2d981202017-03-27 13:25:57 +0200233 const struct pad_config *pads;
234 size_t num;
235
Mario Scheithauerd127be12018-04-23 10:55:39 +0200236 pads = variant_gpio_table(&num);
Mario Scheithauer2d981202017-03-27 13:25:57 +0200237 gpio_configure_pads(pads, num);
Mario Scheithauer0af272c2018-04-10 12:40:11 +0200238
239 config_pmic_imon();
Mario Scheithauer092db952017-01-31 15:45:13 +0100240}
241
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200242static void mainboard_final(void *chip_info)
243{
Mario Scheithauerb83858a2017-09-05 15:32:49 +0200244 uint16_t cmd = 0;
Elyes HAOUAS47503cd2018-05-04 21:58:51 +0200245 struct device *dev = NULL;
Werner Zehd5de0632018-09-19 11:06:22 +0200246 void *spi_base = NULL;
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200247
Mario Scheithauer61413532018-04-25 14:05:09 +0200248 /* Do board specific things */
249 variant_mainboard_final();
Mario Scheithauerb83858a2017-09-05 15:32:49 +0200250
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 Zehd5de0632018-09-19 11:06:22 +0200258 /* 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 Scheithauer956a9f62017-03-29 17:09:37 +0200267}
268
Mario Scheithauer61413532018-04-25 14:05:09 +0200269/* The following function performs board specific things. */
270void __weak variant_mainboard_final(void)
Werner Zehefd0eb32017-09-12 08:58:44 +0200271{
Werner Zehefd0eb32017-09-12 08:58:44 +0200272}
273
Mario Scheithauer092db952017-01-31 15:45:13 +0100274struct chip_operations mainboard_ops = {
275 .init = mainboard_init,
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200276 .final = mainboard_final,
Mario Scheithauer092db952017-01-31 15:45:13 +0100277};