blob: 4cc5490f2a39d2d162b838e86fb8ab5c39c35482 [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 Zehc2f85c62021-07-23 11:39:04 +02007#include <hwilib.h>
8#include <i210.h>
Werner Zehe5a1fc72021-07-01 13:40:11 +02009#include <soc/gpio.h>
Werner Zehc2f85c62021-07-23 11:39:04 +020010#include <string.h>
Werner Zeh4cec1a22021-06-11 06:55:50 +020011#include <timer.h>
12#include <timestamp.h>
Werner Zehc2f85c62021-07-23 11:39:04 +020013
14#define MAX_PATH_DEPTH 12
15#define MAX_NUM_MAPPINGS 10
16
17/** \brief This function can decide if a given MAC address is valid or not.
18 * Currently, addresses filled with 0xff or 0x00 are not valid.
19 * @param mac Buffer to the MAC address to check
20 * @return 0 if address is not valid, otherwise 1
21 */
22static uint8_t is_mac_adr_valid(uint8_t mac[MAC_ADDR_LEN])
23{
24 for (size_t i = 0; i < MAC_ADDR_LEN; i++) {
25 if (mac[i] != 0x00 && mac[i] != 0xff)
26 return 1;
27 if (mac[i] != mac[0])
28 return 1;
29 }
30 return 0;
31}
32
33/** \brief This function will search for a MAC address which can be assigned
34 * to a MACPHY.
35 * @param dev pointer to PCI device
36 * @param mac buffer where to store the MAC address
37 * @return cb_err CB_ERR or CB_SUCCESS
38 */
39enum cb_err mainboard_get_mac_address(struct device *dev, uint8_t mac[MAC_ADDR_LEN])
40{
41 struct bus *parent = dev->bus;
42 uint8_t buf[16], mapping[16], i = 0, chain_len = 0;
43
44 memset(buf, 0, sizeof(buf));
45 memset(mapping, 0, sizeof(mapping));
46
47 /* The first entry in the tree is the device itself. */
48 buf[0] = dev->path.pci.devfn;
49 chain_len = 1;
50 for (i = 1; i < MAX_PATH_DEPTH && parent->dev->bus->subordinate; i++) {
51 buf[i] = parent->dev->path.pci.devfn;
52 chain_len++;
53 parent = parent->dev->bus;
54 }
55 if (i == MAX_PATH_DEPTH) {
56 /* The path is deeper than MAX_PATH_DEPTH devices, error. */
57 printk(BIOS_ERR, "Too many bridges for %s\n", dev_path(dev));
58 return CB_ERR;
59 }
60 /*
61 * Now construct the mapping based on the device chain starting from
62 * root bridge device to the device itself.
63 */
64 mapping[0] = 1;
65 mapping[1] = chain_len;
66 for (i = 0; i < chain_len; i++)
67 mapping[i + 4] = buf[chain_len - i - 1];
68
69 /* Open main hwinfo block */
70 if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS)
71 return CB_ERR;
72 /* Now try to find a valid MAC address in hwinfo for this mapping. */
73 for (i = 0; i < MAX_NUM_MAPPINGS; i++) {
74 if (hwilib_get_field(XMac1Mapping + i, buf, 16) != 16)
75 continue;
76 if (memcmp(buf, mapping, chain_len + 4))
77 continue;
78 /* There is a matching mapping available, get MAC address. */
79 if (hwilib_get_field(XMac1 + i, mac, MAC_ADDR_LEN) == MAC_ADDR_LEN) {
80 if (is_mac_adr_valid(mac))
81 return CB_SUCCESS;
82 }
83 return CB_ERR;
84 }
85 /* No MAC address found for */
86 return CB_ERR;
87}
Werner Zehe5a1fc72021-07-01 13:40:11 +020088
Werner Zeh4cec1a22021-06-11 06:55:50 +020089static void wait_for_legacy_dev(void *unused)
90{
91 uint32_t legacy_delay, us_since_boot;
92 struct stopwatch sw;
93
94 /* Open main hwinfo block. */
95 if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS)
96 return;
97
98 /* Get legacy delay parameter from hwinfo. */
99 if (hwilib_get_field(LegacyDelay, (uint8_t *) &legacy_delay,
100 sizeof(legacy_delay)) != sizeof(legacy_delay))
101 return;
102
103 us_since_boot = get_us_since_boot();
104 /* No need to wait if the time since boot is already long enough.*/
105 if (us_since_boot > legacy_delay)
106 return;
107 stopwatch_init_msecs_expire(&sw, (legacy_delay - us_since_boot) / 1000);
108 printk(BIOS_NOTICE, "Wait remaining %d of %d us for legacy devices...",
109 legacy_delay - us_since_boot, legacy_delay);
110 stopwatch_wait_until_expired(&sw);
111 printk(BIOS_NOTICE, "done!\n");
112}
113
Werner Zehe5a1fc72021-07-01 13:40:11 +0200114static void mainboard_init(void *chip_info)
115{
116 const struct pad_config *pads;
117 size_t num;
118
119 pads = variant_gpio_table(&num);
120 gpio_configure_pads(pads, num);
121}
122
123struct chip_operations mainboard_ops = {
124 .init = mainboard_init,
125};
Werner Zeh4cec1a22021-06-11 06:55:50 +0200126
127BOOT_STATE_INIT_ENTRY(BS_DEV_ENUMERATE, BS_ON_ENTRY, wait_for_legacy_dev, NULL);