blob: 09667cccb12f7b8e621abc33746368b62118fd79 [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
Mario Scheithauer61413532018-04-25 14:05:09 +020017#include <compiler.h>
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070018#include <console/console.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>
21#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
Mario Scheithauer480eab02017-02-16 13:39:16 +010039/** \brief This function can decide if a given MAC address is valid or not.
40 * Currently, addresses filled with 0xff or 0x00 are not valid.
41 * @param mac Buffer to the MAC address to check
42 * @return 0 if address is not valid, otherwise 1
43 */
44static uint8_t is_mac_adr_valid(uint8_t mac[6])
45{
46 uint8_t buf[6];
47
48 memset(buf, 0, sizeof(buf));
49 if (!memcmp(buf, mac, sizeof(buf)))
50 return 0;
51 memset(buf, 0xff, sizeof(buf));
52 if (!memcmp(buf, mac, sizeof(buf)))
53 return 0;
54 return 1;
55}
56
57/** \brief This function will search for a MAC address which can be assigned
58 * to a MACPHY.
59 * @param dev pointer to PCI device
60 * @param mac buffer where to store the MAC address
61 * @return cb_err CB_ERR or CB_SUCCESS
62 */
63enum cb_err mainboard_get_mac_address(struct device *dev, uint8_t mac[6])
64{
65 struct bus *parent = dev->bus;
66 uint8_t buf[16], mapping[16], i = 0, chain_len = 0;
67
68 memset(buf, 0, sizeof(buf));
69 memset(mapping, 0, sizeof(mapping));
70
71 /* The first entry in the tree is the device itself. */
72 buf[0] = dev->path.pci.devfn;
73 chain_len = 1;
74 for (i = 1; i < MAX_PATH_DEPTH && parent->dev->bus->subordinate; i++) {
75 buf[i] = parent->dev->path.pci.devfn;
76 chain_len++;
77 parent = parent->dev->bus;
78 }
79 if (i == MAX_PATH_DEPTH) {
80 /* The path is deeper than MAX_PATH_DEPTH devices, error. */
81 printk(BIOS_ERR, "Too many bridges for %s\n", dev_path(dev));
82 return CB_ERR;
83 }
84 /*
85 * Now construct the mapping based on the device chain starting from
86 * root bridge device to the device itself.
87 */
88 mapping[0] = 1;
89 mapping[1] = chain_len;
90 for (i = 0; i < chain_len; i++)
91 mapping[i + 4] = buf[chain_len - i - 1];
92
93 /* Open main hwinfo block */
94 if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS)
95 return CB_ERR;
96 /* Now try to find a valid MAC address in hwinfo for this mapping.*/
97 for (i = 0; i < MAX_NUM_MAPPINGS; i++) {
98 if ((hwilib_get_field(XMac1Mapping + i, buf, 16) == 16) &&
99 !(memcmp(buf, mapping, chain_len + 4))) {
100 /* There is a matching mapping available, get MAC address. */
101 if ((hwilib_get_field(XMac1 + i, mac, 6) == 6) &&
102 (is_mac_adr_valid(mac))) {
103 return CB_SUCCESS;
104 } else {
105 return CB_ERR;
106 }
107 } else
108 continue;
109 }
110 /* No MAC address found for */
111 return CB_ERR;
112}
Mario Scheithauer092db952017-01-31 15:45:13 +0100113
Mario Scheithauer0af272c2018-04-10 12:40:11 +0200114/** \brief This function fixes an accuracy issue with IDT PMIC.
115 * The current reported system power consumption is higher than the
116 * actual consumption. With a correction of slope and offset for Vcc
117 * and Vnn, the issue is solved.
118 */
119static void config_pmic_imon(void)
120{
121 struct stopwatch sw;
122 uint32_t power_max;
123
124 printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - Start\n");
125
126 /* Calculate CPU TDP in mW */
127 power_max = cpu_get_power_max();
128 printk(BIOS_INFO, "PMIC: CPU TDP %d mW.\n", power_max);
129
130 /*
131 * Fix Vnn slope and offset value.
132 * slope = 0x4a4 # 2.32
133 * offset = 0xfa0d # -2.975
134 */
135 stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS);
136 /* Read P_CR_BIOS_MAILBOX_INTERFACE_0_0_0_MCHBAR and check RUN_BUSY. */
137 while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) {
138 if (stopwatch_expired(&sw)) {
139 printk(BIOS_ERR, "PMIC: Power consumption measurement "
140 "setup fails for Vnn.\n");
141 return;
142 }
143 }
144 /* Set Vnn values into P_CR_BIOS_MAILBOX_DATA_0_0_0_MCHBAR. */
145 MCHBAR32(BIOS_MAILBOX_DATA) = 0xfa0d04a4;
146 /* Set command, address and busy bit. */
147 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000011d;
148 printk(BIOS_DEBUG, "PMIC: Fix Vnn slope and offset value.\n");
149
150 /*
151 * Fix Vcc slope and offset value.
152 * Premium and High SKU:
153 * slope = 0x466 # 2.2
154 * offset = 0xe833 # -11.9
155 * Low and Intermediate SKU:
156 * slope = 0x3b3 # 1.85
157 * offset = 0xed33 # -9.4
158 */
159 stopwatch_init_msecs_expire(&sw, BIOS_MAILBOX_WAIT_MAX_MS);
160 while ((MCHBAR32(BIOS_MAILBOX_INTERFACE) & RUN_BUSY_STS)) {
161 if (stopwatch_expired(&sw)) {
162 printk(BIOS_ERR, "PMIC: Power consumption measurement "
163 "setup fails for Vcc.\n");
164 return;
165 }
166 }
167
168 /*
169 * CPU TDP limit between Premium/High and Low/Intermediate SKU
170 * is 9010 mW.
171 */
172 if (power_max > 9010) {
173 MCHBAR32(BIOS_MAILBOX_DATA) = 0xe8330466;
174 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d;
175 printk(BIOS_INFO, "PMIC: Fix Vcc for Premium SKU.\n");
176 } else {
177 MCHBAR32(BIOS_MAILBOX_DATA) = 0xed3303b3;
178 MCHBAR32(BIOS_MAILBOX_INTERFACE) = 0x8000001d;
179 printk(BIOS_INFO, "PMIC: Fix Vcc for Low SKU.\n");
180 }
181
182 printk(BIOS_DEBUG, "PMIC: Configure PMIC IMON - End\n");
183}
184
Mario Scheithauer092db952017-01-31 15:45:13 +0100185static void mainboard_init(void *chip_info)
186{
Mario Scheithauer2d981202017-03-27 13:25:57 +0200187 const struct pad_config *pads;
188 size_t num;
189
Mario Scheithauerd127be12018-04-23 10:55:39 +0200190 pads = variant_gpio_table(&num);
Mario Scheithauer2d981202017-03-27 13:25:57 +0200191 gpio_configure_pads(pads, num);
Mario Scheithauer0af272c2018-04-10 12:40:11 +0200192
193 config_pmic_imon();
Mario Scheithauer092db952017-01-31 15:45:13 +0100194}
195
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200196static void mainboard_final(void *chip_info)
197{
Mario Scheithauerb83858a2017-09-05 15:32:49 +0200198 uint16_t cmd = 0;
199 device_t dev = NULL;
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200200
Mario Scheithauer61413532018-04-25 14:05:09 +0200201 /* Do board specific things */
202 variant_mainboard_final();
Mario Scheithauerb83858a2017-09-05 15:32:49 +0200203
204 /* Set Master Enable for on-board PCI device. */
205 dev = dev_find_device(PCI_VENDOR_ID_SIEMENS, 0x403f, 0);
206 if (dev) {
207 cmd = pci_read_config16(dev, PCI_COMMAND);
208 cmd |= PCI_COMMAND_MASTER;
209 pci_write_config16(dev, PCI_COMMAND, cmd);
210 }
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200211}
212
Mario Scheithauer61413532018-04-25 14:05:09 +0200213/* The following function performs board specific things. */
214void __weak variant_mainboard_final(void)
Werner Zehefd0eb32017-09-12 08:58:44 +0200215{
Werner Zehefd0eb32017-09-12 08:58:44 +0200216}
217
Mario Scheithauer092db952017-01-31 15:45:13 +0100218struct chip_operations mainboard_ops = {
219 .init = mainboard_init,
Mario Scheithauer956a9f62017-03-29 17:09:37 +0200220 .final = mainboard_final,
Mario Scheithauer092db952017-01-31 15:45:13 +0100221};