blob: 5b8f70f353977ad4466ea2643764ce50e5a3a870 [file] [log] [blame]
Angel Pons6ad91762020-04-03 01:23:24 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Mario Scheithauer092db952017-01-31 15:45:13 +01002
Ravi Sarawadiefa606b2017-08-04 16:26:09 -07003#include <console/console.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02004#include <device/mmio.h>
Mario Scheithauer092db952017-01-31 15:45:13 +01005#include <device/device.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Mario Scheithauerb83858a2017-09-05 15:32:49 +02007#include <device/pci_ids.h>
Mario Scheithauer480eab02017-02-16 13:39:16 +01008#include <hwilib.h>
9#include <i210.h>
Mario Scheithauer0af272c2018-04-10 12:40:11 +020010#include <intelblocks/cpulib.h>
Werner Zeh2cb3cc52020-07-07 09:21:43 +020011#include <intelblocks/fast_spi.h>
Mario Scheithauer0af272c2018-04-10 12:40:11 +020012#include <intelblocks/systemagent.h>
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070013#include <soc/pci_devs.h>
14#include <string.h>
Werner Zehefd0eb32017-09-12 08:58:44 +020015#include <timer.h>
Mario Scheithauerd127be12018-04-23 10:55:39 +020016#include <baseboard/variants.h>
Elyes HAOUASe39db682019-05-15 21:12:31 +020017#include <types.h>
Mario Scheithauer480eab02017-02-16 13:39:16 +010018
19#define MAX_PATH_DEPTH 12
20#define MAX_NUM_MAPPINGS 10
21
Mario Scheithauer0af272c2018-04-10 12:40:11 +020022#define BIOS_MAILBOX_WAIT_MAX_MS 1000
23#define BIOS_MAILBOX_DATA 0x7080
24#define BIOS_MAILBOX_INTERFACE 0x7084
25#define RUN_BUSY_STS (1 << 31)
26
Mario Scheithauer7815c072019-07-17 09:40:33 +020027#define SD_CAP_BYP 0x810
28#define SD_CAP_BYP_EN 0x5A
29#define SD_CAP_BYP_REG1 0x814
30
Mario Scheithauer480eab02017-02-16 13:39:16 +010031/** \brief This function can decide if a given MAC address is valid or not.
32 * Currently, addresses filled with 0xff or 0x00 are not valid.
33 * @param mac Buffer to the MAC address to check
34 * @return 0 if address is not valid, otherwise 1
35 */
36static uint8_t is_mac_adr_valid(uint8_t mac[6])
37{
38 uint8_t buf[6];
39
40 memset(buf, 0, sizeof(buf));
41 if (!memcmp(buf, mac, sizeof(buf)))
42 return 0;
43 memset(buf, 0xff, sizeof(buf));
44 if (!memcmp(buf, mac, sizeof(buf)))
45 return 0;
46 return 1;
47}
48
49/** \brief This function will search for a MAC address which can be assigned
50 * to a MACPHY.
51 * @param dev pointer to PCI device
52 * @param mac buffer where to store the MAC address
53 * @return cb_err CB_ERR or CB_SUCCESS
54 */
55enum cb_err mainboard_get_mac_address(struct device *dev, uint8_t mac[6])
56{
57 struct bus *parent = dev->bus;
58 uint8_t buf[16], mapping[16], i = 0, chain_len = 0;
59
60 memset(buf, 0, sizeof(buf));
61 memset(mapping, 0, sizeof(mapping));
62
63 /* The first entry in the tree is the device itself. */
64 buf[0] = dev->path.pci.devfn;
65 chain_len = 1;
66 for (i = 1; i < MAX_PATH_DEPTH && parent->dev->bus->subordinate; i++) {
67 buf[i] = parent->dev->path.pci.devfn;
68 chain_len++;
69 parent = parent->dev->bus;
70 }
71 if (i == MAX_PATH_DEPTH) {
72 /* The path is deeper than MAX_PATH_DEPTH devices, error. */
73 printk(BIOS_ERR, "Too many bridges for %s\n", dev_path(dev));
74 return CB_ERR;
75 }
76 /*
77 * Now construct the mapping based on the device chain starting from
78 * root bridge device to the device itself.
79 */
80 mapping[0] = 1;
81 mapping[1] = chain_len;
82 for (i = 0; i < chain_len; i++)
83 mapping[i + 4] = buf[chain_len - i - 1];
84
85 /* Open main hwinfo block */
86 if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS)
87 return CB_ERR;
Angel Ponsa9db4bd2020-11-10 18:17:10 +010088 /* Now try to find a valid MAC address in hwinfo for this mapping. */
Mario Scheithauer480eab02017-02-16 13:39:16 +010089 for (i = 0; i < MAX_NUM_MAPPINGS; i++) {
Angel Ponsa9db4bd2020-11-10 18:17:10 +010090 if (hwilib_get_field(XMac1Mapping + i, buf, 16) != 16)
Mario Scheithauer480eab02017-02-16 13:39:16 +010091 continue;
Angel Ponsa9db4bd2020-11-10 18:17:10 +010092 if (memcmp(buf, mapping, chain_len + 4))
93 continue;
94 /* There is a matching mapping available, get MAC address. */
95 if (hwilib_get_field(XMac1 + i, mac, 6) == 6) {
96 if (is_mac_adr_valid(mac))
97 return CB_SUCCESS;
98 }
99 return CB_ERR;
Mario Scheithauer480eab02017-02-16 13:39:16 +0100100 }
101 /* No MAC address found for */
102 return CB_ERR;
103}
Mario Scheithauer092db952017-01-31 15:45:13 +0100104
Mario Scheithauer0af272c2018-04-10 12:40:11 +0200105/** \brief This function fixes an accuracy issue with IDT PMIC.
106 * The current reported system power consumption is higher than the
107 * actual consumption. With a correction of slope and offset for Vcc
108 * and Vnn, the issue is solved.
109 */
110static void config_pmic_imon(void)
111{
112 struct stopwatch sw;
113 uint32_t power_max;
114
115 printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - Start\n");
116
117 /* Calculate CPU TDP in mW */
118 power_max = cpu_get_power_max();
119 printk(BIOS_INFO, "PMIC: CPU TDP %d mW.\n", power_max);
120
121 /*
122 * Fix Vnn slope and offset value.
123 * slope = 0x4a4 # 2.32
124 * offset = 0xfa0d # -2.975
125 */
126 stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS);
127 /* Read P_CR_BIOS_MAILBOX_INTERFACE_0_0_0_MCHBAR and check RUN_BUSY. */
128 while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) {
129 if (stopwatch_expired(&sw)) {
130 printk(BIOS_ERR, "PMIC: Power consumption measurement "
131 "setup fails for Vnn.\n");
132 return;
133 }
134 }
135 /* Set Vnn values into P_CR_BIOS_MAILBOX_DATA_0_0_0_MCHBAR. */
136 MCHBAR32(BIOS_MAILBOX_DATA) = 0xfa0d04a4;
137 /* Set command, address and busy bit. */
138 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000011d;
139 printk(BIOS_DEBUG, "PMIC: Fix Vnn slope and offset value.\n");
140
141 /*
142 * Fix Vcc slope and offset value.
143 * Premium and High SKU:
144 * slope = 0x466 # 2.2
145 * offset = 0xe833 # -11.9
146 * Low and Intermediate SKU:
147 * slope = 0x3b3 # 1.85
148 * offset = 0xed33 # -9.4
149 */
150 stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS);
151 while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) {
152 if (stopwatch_expired(&sw)) {
153 printk(BIOS_ERR, "PMIC: Power consumption measurement "
154 "setup fails for Vcc.\n");
155 return;
156 }
157 }
158
159 /*
160 * CPU TDP limit between Premium/High and Low/Intermediate SKU
161 * is 9010 mW.
162 */
163 if (power_max > 9010) {
164 MCHBAR32(BIOS_MAILBOX_DATA) = 0xe8330466;
165 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d;
166 printk(BIOS_INFO, "PMIC: Fix Vcc for Premium SKU.\n");
167 } else {
168 MCHBAR32(BIOS_MAILBOX_DATA) = 0xed3303b3;
169 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d;
170 printk(BIOS_INFO, "PMIC: Fix Vcc for Low SKU.\n");
171 }
172
173 printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - End\n");
174}
175
Mario Scheithauer092db952017-01-31 15:45:13 +0100176static void mainboard_init(void *chip_info)
177{
Mario Scheithauer2d981202017-03-27 13:25:57 +0200178 const struct pad_config *pads;
179 size_t num;
180
Mario Scheithauerd127be12018-04-23 10:55:39 +0200181 pads = variant_gpio_table(&num);
Mario Scheithauer2d981202017-03-27 13:25:57 +0200182 gpio_configure_pads(pads, num);
Mario Scheithauer0af272c2018-04-10 12:40:11 +0200183
184 config_pmic_imon();
Mario Scheithauer092db952017-01-31 15:45:13 +0100185}
186
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200187static void mainboard_final(void *chip_info)
188{
Elyes HAOUAS47503cd2018-05-04 21:58:51 +0200189 struct device *dev = NULL;
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200190
Mario Scheithauer61413532018-04-25 14:05:09 +0200191 /* Do board specific things */
192 variant_mainboard_final();
Mario Scheithauerb83858a2017-09-05 15:32:49 +0200193
194 /* Set Master Enable for on-board PCI device. */
195 dev = dev_find_device(PCI_VENDOR_ID_SIEMENS, 0x403f, 0);
196 if (dev) {
Angel Pons28ed7872020-11-10 20:07:33 +0100197 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Mario Scheithauerb83858a2017-09-05 15:32:49 +0200198 }
Werner Zehd5de0632018-09-19 11:06:22 +0200199 /* Set up SPI OPCODE menu before the controller is locked. */
Werner Zeh2cb3cc52020-07-07 09:21:43 +0200200 fast_spi_set_opcode_menu();
Mario Scheithauer7815c072019-07-17 09:40:33 +0200201
202 /* Set SD-Card speed to HS mode only. */
203 dev = pcidev_path_on_root(PCH_DEVFN_SDCARD);
204 if (dev) {
205 uint32_t reg;
206 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
207 if (!res)
208 return;
209
210 write32(res2mmio(res, SD_CAP_BYP, 0), SD_CAP_BYP_EN);
211 reg = read32(res2mmio(res, SD_CAP_BYP_REG1, 0));
212 /* Disable all UHS-I SD-Card speed modes, keep only HS mode. */
213 reg &= ~0x2000f800;
214 write32(res2mmio(res, SD_CAP_BYP_REG1, 0), reg);
215 }
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200216}
217
Mario Scheithauer61413532018-04-25 14:05:09 +0200218/* The following function performs board specific things. */
219void __weak variant_mainboard_final(void)
Werner Zehefd0eb32017-09-12 08:58:44 +0200220{
Werner Zehefd0eb32017-09-12 08:58:44 +0200221}
222
Mario Scheithauer092db952017-01-31 15:45:13 +0100223struct chip_operations mainboard_ops = {
224 .init = mainboard_init,
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200225 .final = mainboard_final,
Mario Scheithauer092db952017-01-31 15:45:13 +0100226};