blob: c931e1008a1249b3f635fa80a54db92d02c27f79 [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 Scheithauer480eab02017-02-16 13:39:16 +010086/** \brief This function can decide if a given MAC address is valid or not.
87 * Currently, addresses filled with 0xff or 0x00 are not valid.
88 * @param mac Buffer to the MAC address to check
89 * @return 0 if address is not valid, otherwise 1
90 */
91static uint8_t is_mac_adr_valid(uint8_t mac[6])
92{
93 uint8_t buf[6];
94
95 memset(buf, 0, sizeof(buf));
96 if (!memcmp(buf, mac, sizeof(buf)))
97 return 0;
98 memset(buf, 0xff, sizeof(buf));
99 if (!memcmp(buf, mac, sizeof(buf)))
100 return 0;
101 return 1;
102}
103
104/** \brief This function will search for a MAC address which can be assigned
105 * to a MACPHY.
106 * @param dev pointer to PCI device
107 * @param mac buffer where to store the MAC address
108 * @return cb_err CB_ERR or CB_SUCCESS
109 */
110enum cb_err mainboard_get_mac_address(struct device *dev, uint8_t mac[6])
111{
112 struct bus *parent = dev->bus;
113 uint8_t buf[16], mapping[16], i = 0, chain_len = 0;
114
115 memset(buf, 0, sizeof(buf));
116 memset(mapping, 0, sizeof(mapping));
117
118 /* The first entry in the tree is the device itself. */
119 buf[0] = dev->path.pci.devfn;
120 chain_len = 1;
121 for (i = 1; i < MAX_PATH_DEPTH && parent->dev->bus->subordinate; i++) {
122 buf[i] = parent->dev->path.pci.devfn;
123 chain_len++;
124 parent = parent->dev->bus;
125 }
126 if (i == MAX_PATH_DEPTH) {
127 /* The path is deeper than MAX_PATH_DEPTH devices, error. */
128 printk(BIOS_ERR, "Too many bridges for %s\n", dev_path(dev));
129 return CB_ERR;
130 }
131 /*
132 * Now construct the mapping based on the device chain starting from
133 * root bridge device to the device itself.
134 */
135 mapping[0] = 1;
136 mapping[1] = chain_len;
137 for (i = 0; i < chain_len; i++)
138 mapping[i + 4] = buf[chain_len - i - 1];
139
140 /* Open main hwinfo block */
141 if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS)
142 return CB_ERR;
143 /* Now try to find a valid MAC address in hwinfo for this mapping.*/
144 for (i = 0; i < MAX_NUM_MAPPINGS; i++) {
145 if ((hwilib_get_field(XMac1Mapping + i, buf, 16) == 16) &&
146 !(memcmp(buf, mapping, chain_len + 4))) {
147 /* There is a matching mapping available, get MAC address. */
148 if ((hwilib_get_field(XMac1 + i, mac, 6) == 6) &&
149 (is_mac_adr_valid(mac))) {
150 return CB_SUCCESS;
151 } else {
152 return CB_ERR;
153 }
154 } else
155 continue;
156 }
157 /* No MAC address found for */
158 return CB_ERR;
159}
Mario Scheithauer092db952017-01-31 15:45:13 +0100160
Mario Scheithauer0af272c2018-04-10 12:40:11 +0200161/** \brief This function fixes an accuracy issue with IDT PMIC.
162 * The current reported system power consumption is higher than the
163 * actual consumption. With a correction of slope and offset for Vcc
164 * and Vnn, the issue is solved.
165 */
166static void config_pmic_imon(void)
167{
168 struct stopwatch sw;
169 uint32_t power_max;
170
171 printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - Start\n");
172
173 /* Calculate CPU TDP in mW */
174 power_max = cpu_get_power_max();
175 printk(BIOS_INFO, "PMIC: CPU TDP %d mW.\n", power_max);
176
177 /*
178 * Fix Vnn slope and offset value.
179 * slope = 0x4a4 # 2.32
180 * offset = 0xfa0d # -2.975
181 */
182 stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS);
183 /* Read P_CR_BIOS_MAILBOX_INTERFACE_0_0_0_MCHBAR and check RUN_BUSY. */
184 while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) {
185 if (stopwatch_expired(&sw)) {
186 printk(BIOS_ERR, "PMIC: Power consumption measurement "
187 "setup fails for Vnn.\n");
188 return;
189 }
190 }
191 /* Set Vnn values into P_CR_BIOS_MAILBOX_DATA_0_0_0_MCHBAR. */
192 MCHBAR32(BIOS_MAILBOX_DATA) = 0xfa0d04a4;
193 /* Set command, address and busy bit. */
194 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000011d;
195 printk(BIOS_DEBUG, "PMIC: Fix Vnn slope and offset value.\n");
196
197 /*
198 * Fix Vcc slope and offset value.
199 * Premium and High SKU:
200 * slope = 0x466 # 2.2
201 * offset = 0xe833 # -11.9
202 * Low and Intermediate SKU:
203 * slope = 0x3b3 # 1.85
204 * offset = 0xed33 # -9.4
205 */
206 stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS);
207 while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) {
208 if (stopwatch_expired(&sw)) {
209 printk(BIOS_ERR, "PMIC: Power consumption measurement "
210 "setup fails for Vcc.\n");
211 return;
212 }
213 }
214
215 /*
216 * CPU TDP limit between Premium/High and Low/Intermediate SKU
217 * is 9010 mW.
218 */
219 if (power_max > 9010) {
220 MCHBAR32(BIOS_MAILBOX_DATA) = 0xe8330466;
221 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d;
222 printk(BIOS_INFO, "PMIC: Fix Vcc for Premium SKU.\n");
223 } else {
224 MCHBAR32(BIOS_MAILBOX_DATA) = 0xed3303b3;
225 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d;
226 printk(BIOS_INFO, "PMIC: Fix Vcc for Low SKU.\n");
227 }
228
229 printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - End\n");
230}
231
Mario Scheithauer092db952017-01-31 15:45:13 +0100232static void mainboard_init(void *chip_info)
233{
Mario Scheithauer2d981202017-03-27 13:25:57 +0200234 const struct pad_config *pads;
235 size_t num;
236
Mario Scheithauerd127be12018-04-23 10:55:39 +0200237 pads = variant_gpio_table(&num);
Mario Scheithauer2d981202017-03-27 13:25:57 +0200238 gpio_configure_pads(pads, num);
Mario Scheithauer0af272c2018-04-10 12:40:11 +0200239
240 config_pmic_imon();
Mario Scheithauer092db952017-01-31 15:45:13 +0100241}
242
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200243static void mainboard_final(void *chip_info)
244{
Mario Scheithauerb83858a2017-09-05 15:32:49 +0200245 uint16_t cmd = 0;
Elyes HAOUAS47503cd2018-05-04 21:58:51 +0200246 struct device *dev = NULL;
Werner Zehd5de0632018-09-19 11:06:22 +0200247 void *spi_base = NULL;
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200248
Mario Scheithauer61413532018-04-25 14:05:09 +0200249 /* Do board specific things */
250 variant_mainboard_final();
Mario Scheithauerb83858a2017-09-05 15:32:49 +0200251
252 /* Set Master Enable for on-board PCI device. */
253 dev = dev_find_device(PCI_VENDOR_ID_SIEMENS, 0x403f, 0);
254 if (dev) {
255 cmd = pci_read_config16(dev, PCI_COMMAND);
256 cmd |= PCI_COMMAND_MASTER;
257 pci_write_config16(dev, PCI_COMMAND, cmd);
258 }
Werner Zehd5de0632018-09-19 11:06:22 +0200259 /* Set up SPI OPCODE menu before the controller is locked. */
260 dev = PCH_DEV_SPI;
261 spi_base = (void *)pci_read_config32(dev, PCI_BASE_ADDRESS_0);
262 if (!spi_base)
263 return;
264 write32((spi_base + SPI_REG_PREOP_OPTYPE),
265 ((SPI_OPTYPE << 16) | SPI_OPPREFIX));
266 write32((spi_base + SPI_REG_OPMENU_L), SPI_OPMENU_LOWER);
267 write32((spi_base + SPI_REG_OPMENU_H), SPI_OPMENU_UPPER);
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200268}
269
Mario Scheithauer61413532018-04-25 14:05:09 +0200270/* The following function performs board specific things. */
271void __weak variant_mainboard_final(void)
Werner Zehefd0eb32017-09-12 08:58:44 +0200272{
Werner Zehefd0eb32017-09-12 08:58:44 +0200273}
274
Mario Scheithauer092db952017-01-31 15:45:13 +0100275struct chip_operations mainboard_ops = {
276 .init = mainboard_init,
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200277 .final = mainboard_final,
Mario Scheithauer092db952017-01-31 15:45:13 +0100278};