blob: e64dbda54fecd35705d2337694181e2697f5277a [file] [log] [blame]
Werner Zehe5a1fc72021-07-01 13:40:11 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <baseboard/variants.h>
Werner Zeh4cec1a22021-06-11 06:55:50 +02004#include <bootstate.h>
Werner Zehc2f85c62021-07-23 11:39:04 +02005#include <console/console.h>
Werner Zehe5a1fc72021-07-01 13:40:11 +02006#include <device/device.h>
Werner Zeh78ec7502021-06-11 07:05:06 +02007#include <device/pci_def.h>
8#include <device/pci_ops.h>
9#include <device/pci_ids.h>
Werner Zehc2f85c62021-07-23 11:39:04 +020010#include <hwilib.h>
11#include <i210.h>
Werner Zehe5a1fc72021-07-01 13:40:11 +020012#include <soc/gpio.h>
Werner Zehc2f85c62021-07-23 11:39:04 +020013#include <string.h>
Werner Zeh4cec1a22021-06-11 06:55:50 +020014#include <timer.h>
15#include <timestamp.h>
Werner Zehc2f85c62021-07-23 11:39:04 +020016
17#define MAX_PATH_DEPTH 12
18#define MAX_NUM_MAPPINGS 10
19
20/** \brief This function can decide if a given MAC address is valid or not.
21 * Currently, addresses filled with 0xff or 0x00 are not valid.
22 * @param mac Buffer to the MAC address to check
23 * @return 0 if address is not valid, otherwise 1
24 */
25static uint8_t is_mac_adr_valid(uint8_t mac[MAC_ADDR_LEN])
26{
27 for (size_t i = 0; i < MAC_ADDR_LEN; i++) {
28 if (mac[i] != 0x00 && mac[i] != 0xff)
29 return 1;
30 if (mac[i] != mac[0])
31 return 1;
32 }
33 return 0;
34}
35
36/** \brief This function will search for a MAC address which can be assigned
37 * to a MACPHY.
38 * @param dev pointer to PCI device
39 * @param mac buffer where to store the MAC address
40 * @return cb_err CB_ERR or CB_SUCCESS
41 */
42enum cb_err mainboard_get_mac_address(struct device *dev, uint8_t mac[MAC_ADDR_LEN])
43{
44 struct bus *parent = dev->bus;
45 uint8_t buf[16], mapping[16], i = 0, chain_len = 0;
46
47 memset(buf, 0, sizeof(buf));
48 memset(mapping, 0, sizeof(mapping));
49
50 /* The first entry in the tree is the device itself. */
51 buf[0] = dev->path.pci.devfn;
52 chain_len = 1;
53 for (i = 1; i < MAX_PATH_DEPTH && parent->dev->bus->subordinate; i++) {
54 buf[i] = parent->dev->path.pci.devfn;
55 chain_len++;
56 parent = parent->dev->bus;
57 }
58 if (i == MAX_PATH_DEPTH) {
59 /* The path is deeper than MAX_PATH_DEPTH devices, error. */
60 printk(BIOS_ERR, "Too many bridges for %s\n", dev_path(dev));
61 return CB_ERR;
62 }
63 /*
64 * Now construct the mapping based on the device chain starting from
65 * root bridge device to the device itself.
66 */
67 mapping[0] = 1;
68 mapping[1] = chain_len;
69 for (i = 0; i < chain_len; i++)
70 mapping[i + 4] = buf[chain_len - i - 1];
71
72 /* Open main hwinfo block */
73 if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS)
74 return CB_ERR;
75 /* Now try to find a valid MAC address in hwinfo for this mapping. */
76 for (i = 0; i < MAX_NUM_MAPPINGS; i++) {
77 if (hwilib_get_field(XMac1Mapping + i, buf, 16) != 16)
78 continue;
79 if (memcmp(buf, mapping, chain_len + 4))
80 continue;
81 /* There is a matching mapping available, get MAC address. */
82 if (hwilib_get_field(XMac1 + i, mac, MAC_ADDR_LEN) == MAC_ADDR_LEN) {
83 if (is_mac_adr_valid(mac))
84 return CB_SUCCESS;
85 }
86 return CB_ERR;
87 }
88 /* No MAC address found for */
89 return CB_ERR;
90}
Werner Zehe5a1fc72021-07-01 13:40:11 +020091
Werner Zeh4cec1a22021-06-11 06:55:50 +020092static void wait_for_legacy_dev(void *unused)
93{
94 uint32_t legacy_delay, us_since_boot;
95 struct stopwatch sw;
96
97 /* Open main hwinfo block. */
98 if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS)
99 return;
100
101 /* Get legacy delay parameter from hwinfo. */
102 if (hwilib_get_field(LegacyDelay, (uint8_t *) &legacy_delay,
103 sizeof(legacy_delay)) != sizeof(legacy_delay))
104 return;
105
106 us_since_boot = get_us_since_boot();
107 /* No need to wait if the time since boot is already long enough.*/
108 if (us_since_boot > legacy_delay)
109 return;
110 stopwatch_init_msecs_expire(&sw, (legacy_delay - us_since_boot) / 1000);
111 printk(BIOS_NOTICE, "Wait remaining %d of %d us for legacy devices...",
112 legacy_delay - us_since_boot, legacy_delay);
113 stopwatch_wait_until_expired(&sw);
114 printk(BIOS_NOTICE, "done!\n");
115}
116
Werner Zehe5a1fc72021-07-01 13:40:11 +0200117static void mainboard_init(void *chip_info)
118{
119 const struct pad_config *pads;
120 size_t num;
121
122 pads = variant_gpio_table(&num);
123 gpio_configure_pads(pads, num);
124}
125
Werner Zeh78ec7502021-06-11 07:05:06 +0200126static void mainboard_final(void *chip_info)
127{
128 struct device *dev;
129
Mario Scheithauerd7f45ea2021-08-24 12:43:45 +0200130 /* Do board specific things */
131 variant_mainboard_final();
132
Werner Zeh78ec7502021-06-11 07:05:06 +0200133 if (CONFIG(PCI_ALLOW_BUS_MASTER_ANY_DEVICE)) {
134 /* Set Master Enable for on-board PCI devices if allowed. */
135 dev = dev_find_device(PCI_VENDOR_ID_SIEMENS, 0x403e, 0);
136 if (dev)
137 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
138
139 dev = dev_find_device(PCI_VENDOR_ID_SIEMENS, 0x403f, 0);
140 if (dev)
141 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
142 }
143}
144
Mario Scheithauerd7f45ea2021-08-24 12:43:45 +0200145/* The following function performs board specific things. */
146void __weak variant_mainboard_final(void)
147{
148}
149
Werner Zehe5a1fc72021-07-01 13:40:11 +0200150struct chip_operations mainboard_ops = {
Werner Zeh78ec7502021-06-11 07:05:06 +0200151 .init = mainboard_init,
152 .final = mainboard_final
Werner Zehe5a1fc72021-07-01 13:40:11 +0200153};
Werner Zeh4cec1a22021-06-11 06:55:50 +0200154
155BOOT_STATE_INIT_ENTRY(BS_DEV_ENUMERATE, BS_ON_ENTRY, wait_for_legacy_dev, NULL);