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> |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame^] | 34 | #include "chip.h" |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 35 | |
| 36 | #define NIC_TIMEOUT 1000 |
| 37 | |
| 38 | #define CMD_REG 0x37 |
| 39 | #define CMD_REG_RESET 0x10 |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame^] | 40 | #define CMD_LED0_LED1 0x18 |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 41 | |
| 42 | #define CFG_9346 0x50 |
| 43 | #define CFG_9346_LOCK 0x00 |
| 44 | #define CFG_9346_UNLOCK 0xc0 |
| 45 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 46 | /** |
| 47 | * search: Find first instance of string in a given region |
| 48 | * @param p string to find |
| 49 | * @param a start address of region to search |
| 50 | * @param lengthp length of string to search for |
| 51 | * @param lengtha length of region to search in |
| 52 | * @return offset offset from start address a where string was found. |
| 53 | * If string not found, return lengtha. |
| 54 | */ |
| 55 | static size_t search(const char *p, const u8 *a, size_t lengthp, |
| 56 | size_t lengtha) |
| 57 | { |
| 58 | size_t i, j; |
| 59 | |
| 60 | if (lengtha < lengthp) |
| 61 | return lengtha; |
| 62 | /* Searching */ |
| 63 | for (j = 0; j <= lengtha - lengthp; j++) { |
| 64 | for (i = 0; i < lengthp && p[i] == a[i + j]; i++) |
| 65 | ; |
| 66 | if (i >= lengthp) |
| 67 | return j; |
| 68 | } |
| 69 | return lengtha; |
| 70 | } |
| 71 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 72 | static u8 get_hex_digit(const u8 c) |
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 | u8 ret = 0; |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 75 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 76 | ret = c - '0'; |
| 77 | if (ret > 0x09) { |
| 78 | ret = c - 'A' + 0x0a; |
| 79 | if (ret > 0x0f) |
| 80 | ret = c - 'a' + 0x0a; |
| 81 | } |
| 82 | if (ret > 0x0f) { |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 83 | printk(BIOS_ERR, "Error: Invalid hex digit found: " |
| 84 | "%c - 0x%02x\n", (char)c, c); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 85 | ret = 0; |
| 86 | } |
| 87 | return ret; |
| 88 | } |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 89 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 90 | #define MACLEN 17 |
| 91 | |
| 92 | static enum cb_err fetch_mac_string_vpd(u8 *macstrbuf) |
| 93 | { |
| 94 | struct region_device rdev; |
| 95 | void *search_address; |
| 96 | size_t search_length; |
| 97 | size_t offset; |
| 98 | char key[] = "ethernet_mac"; |
| 99 | |
| 100 | if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) { |
| 101 | printk(BIOS_ERR, "Error: Couldn't find RO_VPD region."); |
| 102 | return CB_ERR; |
| 103 | } |
| 104 | search_address = rdev_mmap_full(&rdev); |
| 105 | if (search_address == NULL) { |
| 106 | printk(BIOS_ERR, "LAN: VPD not found.\n"); |
| 107 | return CB_ERR; |
| 108 | } |
| 109 | |
| 110 | search_length = region_device_sz(&rdev); |
| 111 | offset = search(key, search_address, strlen(key), |
| 112 | search_length); |
| 113 | if (offset == search_length) { |
| 114 | printk(BIOS_ERR, |
| 115 | "Error: Could not locate '%s' in VPD\n", key); |
| 116 | return CB_ERR; |
| 117 | } |
| 118 | printk(BIOS_DEBUG, "Located '%s' in VPD\n", key); |
| 119 | |
| 120 | offset += sizeof(key); /* move to next character */ |
| 121 | |
| 122 | if (offset + MACLEN > search_length) { |
| 123 | printk(BIOS_ERR, "Search result too small!\n"); |
| 124 | return CB_ERR; |
| 125 | } |
| 126 | memcpy(macstrbuf, search_address + offset, MACLEN); |
| 127 | |
| 128 | return CB_SUCCESS; |
| 129 | } |
| 130 | |
| 131 | static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf) |
| 132 | { |
| 133 | struct cbfsf fh; |
| 134 | uint32_t matchraw = CBFS_TYPE_RAW; |
| 135 | |
| 136 | if (!cbfs_boot_locate(&fh, "rt8168-macaddress", &matchraw)) { |
| 137 | /* check the cbfs for the mac address */ |
| 138 | if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) { |
| 139 | printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n"); |
| 140 | return CB_ERR; |
| 141 | } |
| 142 | return CB_SUCCESS; |
| 143 | } |
| 144 | return CB_ERR; |
| 145 | } |
| 146 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 147 | static void get_mac_address(u8 *macaddr, const u8 *strbuf) |
| 148 | { |
| 149 | size_t offset = 0; |
| 150 | int i; |
| 151 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 152 | if ((strbuf[2] != ':') || (strbuf[5] != ':') || |
| 153 | (strbuf[8] != ':') || (strbuf[11] != ':') || |
| 154 | (strbuf[14] != ':')) { |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 155 | printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n"); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | for (i = 0; i < 6; i++) { |
| 160 | macaddr[i] = 0; |
| 161 | macaddr[i] |= get_hex_digit(strbuf[offset]) << 4; |
| 162 | macaddr[i] |= get_hex_digit(strbuf[offset + 1]); |
| 163 | offset += 3; |
| 164 | } |
| 165 | } |
| 166 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 167 | static void program_mac_address(struct device *dev, u16 io_base) |
| 168 | { |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 169 | u8 macstrbuf[MACLEN] = { 0 }; |
| 170 | int i = 0; |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 171 | /* Default MAC Address of 00:E0:4C:00:C0:B0 */ |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 172 | u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 }; |
| 173 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 174 | /* check the VPD for the mac address */ |
| 175 | if (IS_ENABLED(CONFIG_RT8168_GET_MAC_FROM_VPD)) { |
| 176 | if (fetch_mac_string_vpd(macstrbuf) != CB_SUCCESS) |
| 177 | printk(BIOS_ERR, "r8168: mac address not found in VPD," |
| 178 | " using default 00:e0:4c:00:c0:b0\n"); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 179 | } else { |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 180 | if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS) |
| 181 | printk(BIOS_ERR, "r8168: Error reading MAC from CBFS," |
| 182 | " using default 00:e0:4c:00:c0:b0\n"); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 183 | } |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 184 | get_mac_address(mac, macstrbuf); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 185 | |
| 186 | /* Reset NIC */ |
| 187 | printk(BIOS_DEBUG, "r8168: Resetting NIC..."); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 188 | outb(CMD_REG_RESET, io_base + CMD_REG); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 189 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 190 | /* Poll for reset, with 1sec timeout */ |
| 191 | while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) { |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 192 | udelay(1000); |
| 193 | if (++i >= NIC_TIMEOUT) |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 194 | printk(BIOS_ERR, "timeout waiting for nic to reset\n"); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 195 | } |
| 196 | if (i < NIC_TIMEOUT) |
| 197 | printk(BIOS_DEBUG, "done\n"); |
| 198 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 199 | printk(BIOS_DEBUG, "r8168: Programming MAC Address..."); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 200 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 201 | /* Disable register protection */ |
| 202 | outb(CFG_9346_UNLOCK, io_base + CFG_9346); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 203 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 204 | /* Set MAC address: only 4-byte write accesses allowed */ |
| 205 | outl(mac[4] | mac[5] << 8, io_base + 4); |
| 206 | inl(io_base + 4); |
| 207 | outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24, |
| 208 | io_base); |
| 209 | inl(io_base); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 210 | /* Lock config regs */ |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 211 | outb(CFG_9346_LOCK, io_base + CFG_9346); |
| 212 | |
| 213 | printk(BIOS_DEBUG, "done\n"); |
| 214 | } |
| 215 | |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame^] | 216 | static void r8168_set_customized_led(struct device *dev, u16 io_base) |
| 217 | { |
| 218 | struct drivers_net_config *config = dev->chip_info; |
| 219 | |
| 220 | if (!config) |
| 221 | return; |
| 222 | |
| 223 | /* Read the customized LED setting from devicetree */ |
| 224 | printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds); |
| 225 | |
| 226 | /* |
| 227 | * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration |
| 228 | * Starting from offset 0x18 |
| 229 | * Bit[15:12] LED Feature Control(FC) |
| 230 | * Bit[11:08] LED Select for PINLED2 |
| 231 | * Bit[07:04] LED Select for PINLED1 |
| 232 | * Bit[03:00] LED Select for PINLED0 |
| 233 | * |
| 234 | * Speed Link10M Link100M Link1000M ACT/Full |
| 235 | * LED0 Bit0 Bit1 Bit2 Bit3 |
| 236 | * LED1 Bit4 Bit5 Bit6 Bit7 |
| 237 | * LED2 Bit8 Bit9 Bit10 Bit11 |
| 238 | * FC Bit12 Bit13 Bit14 Bit15 |
| 239 | */ |
| 240 | |
| 241 | /* Set customized LED registers */ |
| 242 | outw(config->customized_leds, io_base + CMD_LED0_LED1); |
| 243 | printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n", |
| 244 | inw(io_base + CMD_LED0_LED1)); |
| 245 | } |
| 246 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 247 | static void r8168_init(struct device *dev) |
| 248 | { |
| 249 | /* Get the resource of the NIC mmio */ |
| 250 | struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 251 | u16 io_base = (u16)nic_res->base; |
| 252 | |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame^] | 253 | /* Check if the base is invalid */ |
| 254 | if (!io_base) { |
| 255 | printk(BIOS_ERR, "r8168: Error cant find IO resource\n"); |
| 256 | return; |
| 257 | } |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 258 | /* Enable but do not set bus master */ |
| 259 | pci_write_config16(dev, PCI_COMMAND, |
| 260 | PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
| 261 | |
| 262 | /* Program MAC address based on CBFS "macaddress" containing |
| 263 | * a string AA:BB:CC:DD:EE:FF */ |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame^] | 264 | program_mac_address(dev, io_base); |
| 265 | |
| 266 | /* Program customized LED mode */ |
| 267 | if (IS_ENABLED(CONFIG_RT8168_SET_LED_MODE)) |
| 268 | r8168_set_customized_led(dev, io_base); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static struct device_operations r8168_ops = { |
Martin Roth | b9810a4 | 2017-07-23 20:00:04 -0600 | [diff] [blame] | 272 | .read_resources = pci_dev_read_resources, |
| 273 | .set_resources = pci_dev_set_resources, |
| 274 | .enable_resources = pci_dev_enable_resources, |
| 275 | .init = r8168_init, |
| 276 | .scan_bus = 0, |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | static const struct pci_driver r8168_driver __pci_driver = { |
Martin Roth | b9810a4 | 2017-07-23 20:00:04 -0600 | [diff] [blame] | 280 | .ops = &r8168_ops, |
| 281 | .vendor = 0x10ec, |
| 282 | .device = 0x8168, |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 283 | }; |