Werner Zeh | e5a1fc7 | 2021-07-01 13:40:11 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <baseboard/variants.h> |
Werner Zeh | 4cec1a2 | 2021-06-11 06:55:50 +0200 | [diff] [blame] | 4 | #include <bootstate.h> |
Werner Zeh | c2f85c6 | 2021-07-23 11:39:04 +0200 | [diff] [blame] | 5 | #include <console/console.h> |
Werner Zeh | e5a1fc7 | 2021-07-01 13:40:11 +0200 | [diff] [blame] | 6 | #include <device/device.h> |
Werner Zeh | 78ec750 | 2021-06-11 07:05:06 +0200 | [diff] [blame^] | 7 | #include <device/pci_def.h> |
| 8 | #include <device/pci_ops.h> |
| 9 | #include <device/pci_ids.h> |
Werner Zeh | c2f85c6 | 2021-07-23 11:39:04 +0200 | [diff] [blame] | 10 | #include <hwilib.h> |
| 11 | #include <i210.h> |
Werner Zeh | e5a1fc7 | 2021-07-01 13:40:11 +0200 | [diff] [blame] | 12 | #include <soc/gpio.h> |
Werner Zeh | c2f85c6 | 2021-07-23 11:39:04 +0200 | [diff] [blame] | 13 | #include <string.h> |
Werner Zeh | 4cec1a2 | 2021-06-11 06:55:50 +0200 | [diff] [blame] | 14 | #include <timer.h> |
| 15 | #include <timestamp.h> |
Werner Zeh | c2f85c6 | 2021-07-23 11:39:04 +0200 | [diff] [blame] | 16 | |
| 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 | */ |
| 25 | static 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 | */ |
| 42 | enum 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 Zeh | e5a1fc7 | 2021-07-01 13:40:11 +0200 | [diff] [blame] | 91 | |
Werner Zeh | 4cec1a2 | 2021-06-11 06:55:50 +0200 | [diff] [blame] | 92 | static 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 Zeh | e5a1fc7 | 2021-07-01 13:40:11 +0200 | [diff] [blame] | 117 | static 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 Zeh | 78ec750 | 2021-06-11 07:05:06 +0200 | [diff] [blame^] | 126 | static void mainboard_final(void *chip_info) |
| 127 | { |
| 128 | struct device *dev; |
| 129 | |
| 130 | if (CONFIG(PCI_ALLOW_BUS_MASTER_ANY_DEVICE)) { |
| 131 | /* Set Master Enable for on-board PCI devices if allowed. */ |
| 132 | dev = dev_find_device(PCI_VENDOR_ID_SIEMENS, 0x403e, 0); |
| 133 | if (dev) |
| 134 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER); |
| 135 | |
| 136 | dev = dev_find_device(PCI_VENDOR_ID_SIEMENS, 0x403f, 0); |
| 137 | if (dev) |
| 138 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER); |
| 139 | } |
| 140 | } |
| 141 | |
Werner Zeh | e5a1fc7 | 2021-07-01 13:40:11 +0200 | [diff] [blame] | 142 | struct chip_operations mainboard_ops = { |
Werner Zeh | 78ec750 | 2021-06-11 07:05:06 +0200 | [diff] [blame^] | 143 | .init = mainboard_init, |
| 144 | .final = mainboard_final |
Werner Zeh | e5a1fc7 | 2021-07-01 13:40:11 +0200 | [diff] [blame] | 145 | }; |
Werner Zeh | 4cec1a2 | 2021-06-11 06:55:50 +0200 | [diff] [blame] | 146 | |
| 147 | BOOT_STATE_INIT_ENTRY(BS_DEV_ENUMERATE, BS_ON_ENTRY, wait_for_legacy_dev, NULL); |