Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 4 | * Copyright (C) 2012 Google Inc. |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 5 | * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com> |
| 6 | * |
| 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 | |
| 17 | /* |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 18 | * This driver resets the 10ec:8168 NIC then tries to read |
| 19 | * "macaddress" string XX:XX:XX:XX:XX:XX from CBFS. |
| 20 | * If no MAC is found, it programs a default MAC address in the device |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 21 | * so that if the EEPROM/efuse is unconfigured it still has a default MAC. |
| 22 | */ |
| 23 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 24 | #include <cbfs.h> |
| 25 | #include <string.h> |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 26 | #include <arch/io.h> |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 27 | #include <console/console.h> |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 28 | #include <device/device.h> |
| 29 | #include <device/pci.h> |
| 30 | #include <device/pci_ops.h> |
| 31 | #include <device/pci_def.h> |
| 32 | #include <delay.h> |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 33 | #include <fmap.h> |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 34 | |
| 35 | #define NIC_TIMEOUT 1000 |
| 36 | |
| 37 | #define CMD_REG 0x37 |
| 38 | #define CMD_REG_RESET 0x10 |
| 39 | |
| 40 | #define CFG_9346 0x50 |
| 41 | #define CFG_9346_LOCK 0x00 |
| 42 | #define CFG_9346_UNLOCK 0xc0 |
| 43 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 44 | /** |
| 45 | * search: Find first instance of string in a given region |
| 46 | * @param p string to find |
| 47 | * @param a start address of region to search |
| 48 | * @param lengthp length of string to search for |
| 49 | * @param lengtha length of region to search in |
| 50 | * @return offset offset from start address a where string was found. |
| 51 | * If string not found, return lengtha. |
| 52 | */ |
| 53 | static size_t search(const char *p, const u8 *a, size_t lengthp, |
| 54 | size_t lengtha) |
| 55 | { |
| 56 | size_t i, j; |
| 57 | |
| 58 | if (lengtha < lengthp) |
| 59 | return lengtha; |
| 60 | /* Searching */ |
| 61 | for (j = 0; j <= lengtha - lengthp; j++) { |
| 62 | for (i = 0; i < lengthp && p[i] == a[i + j]; i++) |
| 63 | ; |
| 64 | if (i >= lengthp) |
| 65 | return j; |
| 66 | } |
| 67 | return lengtha; |
| 68 | } |
| 69 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 70 | static u8 get_hex_digit(const u8 c) |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 71 | { |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 72 | u8 ret = 0; |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 73 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 74 | ret = c - '0'; |
| 75 | if (ret > 0x09) { |
| 76 | ret = c - 'A' + 0x0a; |
| 77 | if (ret > 0x0f) |
| 78 | ret = c - 'a' + 0x0a; |
| 79 | } |
| 80 | if (ret > 0x0f) { |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 81 | printk(BIOS_ERR, "Error: Invalid hex digit found: " |
| 82 | "%c - 0x%02x\n", (char)c, c); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 83 | ret = 0; |
| 84 | } |
| 85 | return ret; |
| 86 | } |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 87 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 88 | #define MACLEN 17 |
| 89 | |
| 90 | static enum cb_err fetch_mac_string_vpd(u8 *macstrbuf) |
| 91 | { |
| 92 | struct region_device rdev; |
| 93 | void *search_address; |
| 94 | size_t search_length; |
| 95 | size_t offset; |
| 96 | char key[] = "ethernet_mac"; |
| 97 | |
| 98 | if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) { |
| 99 | printk(BIOS_ERR, "Error: Couldn't find RO_VPD region."); |
| 100 | return CB_ERR; |
| 101 | } |
| 102 | search_address = rdev_mmap_full(&rdev); |
| 103 | if (search_address == NULL) { |
| 104 | printk(BIOS_ERR, "LAN: VPD not found.\n"); |
| 105 | return CB_ERR; |
| 106 | } |
| 107 | |
| 108 | search_length = region_device_sz(&rdev); |
| 109 | offset = search(key, search_address, strlen(key), |
| 110 | search_length); |
| 111 | if (offset == search_length) { |
| 112 | printk(BIOS_ERR, |
| 113 | "Error: Could not locate '%s' in VPD\n", key); |
| 114 | return CB_ERR; |
| 115 | } |
| 116 | printk(BIOS_DEBUG, "Located '%s' in VPD\n", key); |
| 117 | |
| 118 | offset += sizeof(key); /* move to next character */ |
| 119 | |
| 120 | if (offset + MACLEN > search_length) { |
| 121 | printk(BIOS_ERR, "Search result too small!\n"); |
| 122 | return CB_ERR; |
| 123 | } |
| 124 | memcpy(macstrbuf, search_address + offset, MACLEN); |
| 125 | |
| 126 | return CB_SUCCESS; |
| 127 | } |
| 128 | |
| 129 | static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf) |
| 130 | { |
| 131 | struct cbfsf fh; |
| 132 | uint32_t matchraw = CBFS_TYPE_RAW; |
| 133 | |
| 134 | if (!cbfs_boot_locate(&fh, "rt8168-macaddress", &matchraw)) { |
| 135 | /* check the cbfs for the mac address */ |
| 136 | if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) { |
| 137 | printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n"); |
| 138 | return CB_ERR; |
| 139 | } |
| 140 | return CB_SUCCESS; |
| 141 | } |
| 142 | return CB_ERR; |
| 143 | } |
| 144 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 145 | static void get_mac_address(u8 *macaddr, const u8 *strbuf) |
| 146 | { |
| 147 | size_t offset = 0; |
| 148 | int i; |
| 149 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 150 | if ((strbuf[2] != ':') || (strbuf[5] != ':') || |
| 151 | (strbuf[8] != ':') || (strbuf[11] != ':') || |
| 152 | (strbuf[14] != ':')) { |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 153 | printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n"); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | for (i = 0; i < 6; i++) { |
| 158 | macaddr[i] = 0; |
| 159 | macaddr[i] |= get_hex_digit(strbuf[offset]) << 4; |
| 160 | macaddr[i] |= get_hex_digit(strbuf[offset + 1]); |
| 161 | offset += 3; |
| 162 | } |
| 163 | } |
| 164 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 165 | static void program_mac_address(struct device *dev, u16 io_base) |
| 166 | { |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 167 | u8 macstrbuf[MACLEN] = { 0 }; |
| 168 | int i = 0; |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 169 | /* Default MAC Address of 00:E0:4C:00:C0:B0 */ |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 170 | u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 }; |
| 171 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 172 | /* check the VPD for the mac address */ |
| 173 | if (IS_ENABLED(CONFIG_RT8168_GET_MAC_FROM_VPD)) { |
| 174 | if (fetch_mac_string_vpd(macstrbuf) != CB_SUCCESS) |
| 175 | printk(BIOS_ERR, "r8168: mac address not found in VPD," |
| 176 | " using default 00:e0:4c:00:c0:b0\n"); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 177 | } else { |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 178 | if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS) |
| 179 | printk(BIOS_ERR, "r8168: Error reading MAC from CBFS," |
| 180 | " using default 00:e0:4c:00:c0:b0\n"); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 181 | } |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 182 | get_mac_address(mac, macstrbuf); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 183 | |
| 184 | /* Reset NIC */ |
| 185 | printk(BIOS_DEBUG, "r8168: Resetting NIC..."); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 186 | outb(CMD_REG_RESET, io_base + CMD_REG); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 187 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 188 | /* Poll for reset, with 1sec timeout */ |
| 189 | while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) { |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 190 | udelay(1000); |
| 191 | if (++i >= NIC_TIMEOUT) |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 192 | printk(BIOS_ERR, "timeout waiting for nic to reset\n"); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 193 | } |
| 194 | if (i < NIC_TIMEOUT) |
| 195 | printk(BIOS_DEBUG, "done\n"); |
| 196 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 197 | printk(BIOS_DEBUG, "r8168: Programming MAC Address..."); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 198 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 199 | /* Disable register protection */ |
| 200 | outb(CFG_9346_UNLOCK, io_base + CFG_9346); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 201 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 202 | /* Set MAC address: only 4-byte write accesses allowed */ |
| 203 | outl(mac[4] | mac[5] << 8, io_base + 4); |
| 204 | inl(io_base + 4); |
| 205 | outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24, |
| 206 | io_base); |
| 207 | inl(io_base); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 208 | /* Lock config regs */ |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 209 | outb(CFG_9346_LOCK, io_base + CFG_9346); |
| 210 | |
| 211 | printk(BIOS_DEBUG, "done\n"); |
| 212 | } |
| 213 | |
| 214 | static void r8168_init(struct device *dev) |
| 215 | { |
| 216 | /* Get the resource of the NIC mmio */ |
| 217 | struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 218 | u16 io_base = (u16)nic_res->base; |
| 219 | |
| 220 | /* Enable but do not set bus master */ |
| 221 | pci_write_config16(dev, PCI_COMMAND, |
| 222 | PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
| 223 | |
| 224 | /* Program MAC address based on CBFS "macaddress" containing |
| 225 | * a string AA:BB:CC:DD:EE:FF */ |
| 226 | if (io_base) |
| 227 | program_mac_address(dev, io_base); |
| 228 | else |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 229 | printk(BIOS_ERR, "r8168: Error cant find MMIO resource\n"); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static struct device_operations r8168_ops = { |
Martin Roth | b9810a4 | 2017-07-23 20:00:04 -0600 | [diff] [blame] | 233 | .read_resources = pci_dev_read_resources, |
| 234 | .set_resources = pci_dev_set_resources, |
| 235 | .enable_resources = pci_dev_enable_resources, |
| 236 | .init = r8168_init, |
| 237 | .scan_bus = 0, |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | static const struct pci_driver r8168_driver __pci_driver = { |
Martin Roth | b9810a4 | 2017-07-23 20:00:04 -0600 | [diff] [blame] | 241 | .ops = &r8168_ops, |
| 242 | .vendor = 0x10ec, |
| 243 | .device = 0x8168, |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 244 | }; |