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