Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 2 | |
| 3 | /* |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 4 | * This driver resets the 10ec:8168 NIC then tries to read |
| 5 | * "macaddress" string XX:XX:XX:XX:XX:XX from CBFS. |
| 6 | * 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] | 7 | * so that if the EEPROM/efuse is unconfigured it still has a default MAC. |
| 8 | */ |
| 9 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 10 | #include <cbfs.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 11 | #include <acpi/acpi_device.h> |
| 12 | #include <acpi/acpigen.h> |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 13 | #include <string.h> |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 14 | #include <arch/io.h> |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 15 | #include <console/console.h> |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 16 | #include <device/device.h> |
| 17 | #include <device/pci.h> |
| 18 | #include <device/pci_ops.h> |
| 19 | #include <device/pci_def.h> |
Alan Huang | 769994c | 2021-11-10 10:59:13 +0800 | [diff] [blame] | 20 | #include <device/pci_ids.h> |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 21 | #include <delay.h> |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 22 | #include <fmap.h> |
Elyes HAOUAS | bd1683d | 2019-05-15 21:05:37 +0200 | [diff] [blame] | 23 | #include <types.h> |
| 24 | |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame] | 25 | #include "chip.h" |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 26 | |
| 27 | #define NIC_TIMEOUT 1000 |
| 28 | |
| 29 | #define CMD_REG 0x37 |
| 30 | #define CMD_REG_RESET 0x10 |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame] | 31 | #define CMD_LED0_LED1 0x18 |
Rory Liu | 2b1e737 | 2021-11-22 10:42:25 +0800 | [diff] [blame] | 32 | #define CMD_LED_FEATURE 0x94 |
| 33 | #define CMD_LEDSEL0 0x18 |
| 34 | #define CMD_LEDSEL2 0x84 |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 35 | |
| 36 | #define CFG_9346 0x50 |
| 37 | #define CFG_9346_LOCK 0x00 |
| 38 | #define CFG_9346_UNLOCK 0xc0 |
| 39 | |
Gaggery Tsai | 1f84704 | 2017-12-26 17:13:52 +0800 | [diff] [blame] | 40 | #define DEVICE_INDEX_BYTE 12 |
| 41 | #define MAX_DEVICE_SUPPORT 10 |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 42 | /** |
| 43 | * search: Find first instance of string in a given region |
| 44 | * @param p string to find |
| 45 | * @param a start address of region to search |
| 46 | * @param lengthp length of string to search for |
| 47 | * @param lengtha length of region to search in |
| 48 | * @return offset offset from start address a where string was found. |
| 49 | * If string not found, return lengtha. |
| 50 | */ |
| 51 | static size_t search(const char *p, const u8 *a, size_t lengthp, |
| 52 | size_t lengtha) |
| 53 | { |
| 54 | size_t i, j; |
| 55 | |
| 56 | if (lengtha < lengthp) |
| 57 | return lengtha; |
| 58 | /* Searching */ |
| 59 | for (j = 0; j <= lengtha - lengthp; j++) { |
| 60 | for (i = 0; i < lengthp && p[i] == a[i + j]; i++) |
| 61 | ; |
David Wu | 9a0d9e0 | 2018-05-04 15:37:37 +0800 | [diff] [blame] | 62 | if (i >= lengthp && a[j - 1] == lengthp) |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 63 | return j; |
| 64 | } |
| 65 | return lengtha; |
| 66 | } |
| 67 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 68 | static u8 get_hex_digit(const u8 c) |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 69 | { |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 70 | u8 ret = 0; |
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 | ret = c - '0'; |
| 73 | if (ret > 0x09) { |
| 74 | ret = c - 'A' + 0x0a; |
| 75 | if (ret > 0x0f) |
| 76 | ret = c - 'a' + 0x0a; |
| 77 | } |
| 78 | if (ret > 0x0f) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 79 | printk(BIOS_ERR, "Invalid hex digit found: " |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 80 | "%c - 0x%02x\n", (char)c, c); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 81 | ret = 0; |
| 82 | } |
| 83 | return ret; |
| 84 | } |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 85 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 86 | #define MACLEN 17 |
| 87 | |
Edward O'Callaghan | 2a82f74 | 2020-03-25 12:52:50 +1100 | [diff] [blame] | 88 | /* Returns MAC address based on the key that is passed in. */ |
| 89 | static enum cb_err fetch_mac_vpd_key(u8 *macstrbuf, const char *vpd_key) |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 90 | { |
| 91 | struct region_device rdev; |
| 92 | void *search_address; |
| 93 | size_t search_length; |
| 94 | size_t offset; |
Edward O'Callaghan | 2a82f74 | 2020-03-25 12:52:50 +1100 | [diff] [blame] | 95 | |
| 96 | if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 97 | printk(BIOS_ERR, "Couldn't find RO_VPD region."); |
Edward O'Callaghan | 2a82f74 | 2020-03-25 12:52:50 +1100 | [diff] [blame] | 98 | return CB_ERR; |
| 99 | } |
| 100 | search_address = rdev_mmap_full(&rdev); |
| 101 | if (search_address == NULL) { |
| 102 | printk(BIOS_ERR, "LAN: VPD not found.\n"); |
| 103 | return CB_ERR; |
| 104 | } |
| 105 | |
| 106 | search_length = region_device_sz(&rdev); |
| 107 | offset = search(vpd_key, search_address, strlen(vpd_key), |
| 108 | search_length); |
| 109 | |
| 110 | if (offset == search_length) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 111 | printk(BIOS_ERR, "Could not locate '%s' in VPD\n", vpd_key); |
Edward O'Callaghan | d08bc5a | 2020-03-26 14:37:37 +1100 | [diff] [blame] | 112 | rdev_munmap(&rdev, search_address); |
Edward O'Callaghan | 2a82f74 | 2020-03-25 12:52:50 +1100 | [diff] [blame] | 113 | return CB_ERR; |
| 114 | } |
| 115 | printk(BIOS_DEBUG, "Located '%s' in VPD\n", vpd_key); |
| 116 | |
| 117 | offset += strlen(vpd_key) + 1; /* move to next character */ |
| 118 | |
| 119 | if (offset + MACLEN > search_length) { |
Edward O'Callaghan | d08bc5a | 2020-03-26 14:37:37 +1100 | [diff] [blame] | 120 | rdev_munmap(&rdev, search_address); |
Edward O'Callaghan | 2a82f74 | 2020-03-25 12:52:50 +1100 | [diff] [blame] | 121 | printk(BIOS_ERR, "Search result too small!\n"); |
| 122 | return CB_ERR; |
| 123 | } |
| 124 | memcpy(macstrbuf, search_address + offset, MACLEN); |
Edward O'Callaghan | d08bc5a | 2020-03-26 14:37:37 +1100 | [diff] [blame] | 125 | rdev_munmap(&rdev, search_address); |
Edward O'Callaghan | 2a82f74 | 2020-03-25 12:52:50 +1100 | [diff] [blame] | 126 | |
| 127 | return CB_SUCCESS; |
| 128 | } |
| 129 | |
| 130 | /* Prepares vpd_key by concatenating ethernet_mac with device_index */ |
| 131 | static enum cb_err fetch_mac_vpd_dev_idx(u8 *macstrbuf, u8 device_index) |
| 132 | { |
Gaggery Tsai | 1f84704 | 2017-12-26 17:13:52 +0800 | [diff] [blame] | 133 | char key[] = "ethernet_mac "; /* Leave a space at tail to stuff an index */ |
| 134 | |
| 135 | /* |
Edward O'Callaghan | 0e13806 | 2020-03-23 13:06:42 +1100 | [diff] [blame] | 136 | * Map each NIC on the DUT to "ethernet_macN", where N is [0-9]. |
| 137 | * Translate index number from integer to ascii by adding '0' char. |
Gaggery Tsai | 1f84704 | 2017-12-26 17:13:52 +0800 | [diff] [blame] | 138 | */ |
Edward O'Callaghan | 0e13806 | 2020-03-23 13:06:42 +1100 | [diff] [blame] | 139 | key[DEVICE_INDEX_BYTE] = device_index + '0'; |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 140 | |
Edward O'Callaghan | 2a82f74 | 2020-03-25 12:52:50 +1100 | [diff] [blame] | 141 | return fetch_mac_vpd_key(macstrbuf, key); |
| 142 | } |
| 143 | |
| 144 | static void fetch_mac_string_vpd(struct drivers_net_config *config, u8 *macstrbuf) |
| 145 | { |
| 146 | if (!config) |
| 147 | return; |
| 148 | |
| 149 | /* Current implementation is up to 10 NIC cards */ |
| 150 | if (config->device_index > MAX_DEVICE_SUPPORT) { |
| 151 | printk(BIOS_ERR, "r8168: the maximum device_index should be less then %d\n." |
| 152 | " Using default 00:e0:4c:00:c0:b0\n", MAX_DEVICE_SUPPORT); |
| 153 | return; |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Edward O'Callaghan | 0e13806 | 2020-03-23 13:06:42 +1100 | [diff] [blame] | 156 | if (fetch_mac_vpd_dev_idx(macstrbuf, config->device_index) == CB_SUCCESS) |
Edward O'Callaghan | 2a82f74 | 2020-03-25 12:52:50 +1100 | [diff] [blame] | 157 | return; |
Gaggery Tsai | 1f84704 | 2017-12-26 17:13:52 +0800 | [diff] [blame] | 158 | |
Edward O'Callaghan | 0e13806 | 2020-03-23 13:06:42 +1100 | [diff] [blame] | 159 | if (!CONFIG(RT8168_SUPPORT_LEGACY_VPD_MAC)) { |
| 160 | printk(BIOS_ERR, "r8168: mac address not found in VPD," |
| 161 | " using default 00:e0:4c:00:c0:b0\n"); |
| 162 | return; |
| 163 | } |
| 164 | |
Edward O'Callaghan | 2a0a02f | 2020-03-26 16:47:55 +1100 | [diff] [blame] | 165 | if (fetch_mac_vpd_key(macstrbuf, "ethernet_mac") != CB_SUCCESS) |
Edward O'Callaghan | 2a82f74 | 2020-03-25 12:52:50 +1100 | [diff] [blame] | 166 | printk(BIOS_ERR, "r8168: mac address not found in VPD," |
| 167 | " using default 00:e0:4c:00:c0:b0\n"); |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf) |
| 171 | { |
Julius Werner | 77639e4 | 2021-02-05 16:51:25 -0800 | [diff] [blame] | 172 | if (!cbfs_load("rt8168-macaddress", macstrbuf, MACLEN)) { |
| 173 | printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n"); |
| 174 | return CB_ERR; |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 175 | } |
Julius Werner | 77639e4 | 2021-02-05 16:51:25 -0800 | [diff] [blame] | 176 | return CB_SUCCESS; |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 179 | static void get_mac_address(u8 *macaddr, const u8 *strbuf) |
| 180 | { |
| 181 | size_t offset = 0; |
| 182 | int i; |
| 183 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 184 | if ((strbuf[2] != ':') || (strbuf[5] != ':') || |
| 185 | (strbuf[8] != ':') || (strbuf[11] != ':') || |
| 186 | (strbuf[14] != ':')) { |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 187 | printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n"); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | for (i = 0; i < 6; i++) { |
| 192 | macaddr[i] = 0; |
| 193 | macaddr[i] |= get_hex_digit(strbuf[offset]) << 4; |
| 194 | macaddr[i] |= get_hex_digit(strbuf[offset + 1]); |
| 195 | offset += 3; |
| 196 | } |
| 197 | } |
| 198 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 199 | static void program_mac_address(struct device *dev, u16 io_base) |
| 200 | { |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 201 | u8 macstrbuf[MACLEN] = { 0 }; |
| 202 | int i = 0; |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 203 | /* Default MAC Address of 00:E0:4C:00:C0:B0 */ |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 204 | u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 }; |
Gaggery Tsai | 1f84704 | 2017-12-26 17:13:52 +0800 | [diff] [blame] | 205 | struct drivers_net_config *config = dev->chip_info; |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 206 | |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 207 | /* check the VPD for the mac address */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 208 | if (CONFIG(RT8168_GET_MAC_FROM_VPD)) { |
Edward O'Callaghan | 2a82f74 | 2020-03-25 12:52:50 +1100 | [diff] [blame] | 209 | fetch_mac_string_vpd(config, macstrbuf); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 210 | } else { |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 211 | if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS) |
| 212 | printk(BIOS_ERR, "r8168: Error reading MAC from CBFS," |
Gaggery Tsai | 1f84704 | 2017-12-26 17:13:52 +0800 | [diff] [blame] | 213 | " using default 00:e0:4c:00:c0:b0\n"); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 214 | } |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 215 | get_mac_address(mac, macstrbuf); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 216 | |
| 217 | /* Reset NIC */ |
| 218 | printk(BIOS_DEBUG, "r8168: Resetting NIC..."); |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 219 | outb(CMD_REG_RESET, io_base + CMD_REG); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 220 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 221 | /* Poll for reset, with 1sec timeout */ |
| 222 | while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) { |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 223 | udelay(1000); |
| 224 | if (++i >= NIC_TIMEOUT) |
Shelley Chen | 0528b61 | 2017-06-12 18:29:24 -0700 | [diff] [blame] | 225 | printk(BIOS_ERR, "timeout waiting for nic to reset\n"); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 226 | } |
| 227 | if (i < NIC_TIMEOUT) |
| 228 | printk(BIOS_DEBUG, "done\n"); |
| 229 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 230 | printk(BIOS_DEBUG, "r8168: Programming MAC Address..."); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 231 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 232 | /* Disable register protection */ |
| 233 | outb(CFG_9346_UNLOCK, io_base + CFG_9346); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 234 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 235 | /* Set MAC address: only 4-byte write accesses allowed */ |
| 236 | outl(mac[4] | mac[5] << 8, io_base + 4); |
| 237 | inl(io_base + 4); |
| 238 | outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24, |
| 239 | io_base); |
| 240 | inl(io_base); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 241 | /* Lock config regs */ |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 242 | outb(CFG_9346_LOCK, io_base + CFG_9346); |
| 243 | |
| 244 | printk(BIOS_DEBUG, "done\n"); |
| 245 | } |
| 246 | |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame] | 247 | static void r8168_set_customized_led(struct device *dev, u16 io_base) |
| 248 | { |
| 249 | struct drivers_net_config *config = dev->chip_info; |
| 250 | |
| 251 | if (!config) |
| 252 | return; |
| 253 | |
Rory Liu | 2b1e737 | 2021-11-22 10:42:25 +0800 | [diff] [blame] | 254 | if (dev->device == PCI_DEVICE_ID_REALTEK_8125) { |
| 255 | /* Set LED global Feature register */ |
| 256 | outb(config->led_feature, io_base + CMD_LED_FEATURE); |
| 257 | printk(BIOS_DEBUG, "r8125: read back LED global feature setting as 0x%x\n", |
| 258 | inb(io_base + CMD_LED_FEATURE)); |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame] | 259 | |
Rory Liu | 2b1e737 | 2021-11-22 10:42:25 +0800 | [diff] [blame] | 260 | /* |
| 261 | * Refer to RTL8125 datasheet 5.Customizable LED Configuration |
| 262 | * Register Name IO Address |
| 263 | * LEDSEL0 0x18 |
| 264 | * LEDSEL2 0x84 |
| 265 | * LEDFEATURE 0x94 |
| 266 | * |
| 267 | * LEDSEL Bit[] Description |
| 268 | * Bit0 Link10M |
| 269 | * Bit1 Link100M |
| 270 | * Bit3 Link1000M |
| 271 | * Bit5 Link2.5G |
| 272 | * Bit9 ACT |
| 273 | * Bit10 preboot enable |
| 274 | * Bit11 lp enable |
| 275 | * Bit12 active low/high |
| 276 | * |
| 277 | * LEDFEATURE Description |
| 278 | * Bit0 LED Table V1/V2 |
| 279 | * Bit1~3 Reserved |
| 280 | * Bit4~5 LED Blinking Duty Cycle 12.5%/ 25%/ 50%/ 75% |
| 281 | * Bit6~7 LED Blinking Freq. 240ms/160ms/80ms/Link-Speed-Dependent |
| 282 | */ |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame] | 283 | |
Rory Liu | 2b1e737 | 2021-11-22 10:42:25 +0800 | [diff] [blame] | 284 | /* Set customized LED0 register */ |
| 285 | outw(config->customized_led0, io_base + CMD_LEDSEL0); |
| 286 | printk(BIOS_DEBUG, "r8125: read back LED0 setting as 0x%x\n", |
| 287 | inw(io_base + CMD_LEDSEL0)); |
| 288 | |
| 289 | /* Set customized LED2 register */ |
| 290 | outw(config->customized_led2, io_base + CMD_LEDSEL2); |
| 291 | printk(BIOS_DEBUG, "r8125: read back LED2 setting as 0x%x\n", |
| 292 | inw(io_base + CMD_LEDSEL2)); |
| 293 | } else { |
| 294 | /* Read the customized LED setting from devicetree */ |
| 295 | printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds); |
| 296 | |
| 297 | /* |
| 298 | * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration |
| 299 | * Starting from offset 0x18 |
| 300 | * Bit[15:12] LED Feature Control(FC) |
| 301 | * Bit[11:08] LED Select for PINLED2 |
| 302 | * Bit[07:04] LED Select for PINLED1 |
| 303 | * Bit[03:00] LED Select for PINLED0 |
| 304 | * |
| 305 | * Speed Link10M Link100M Link1000M ACT/Full |
| 306 | * LED0 Bit0 Bit1 Bit2 Bit3 |
| 307 | * LED1 Bit4 Bit5 Bit6 Bit7 |
| 308 | * LED2 Bit8 Bit9 Bit10 Bit11 |
| 309 | * FC Bit12 Bit13 Bit14 Bit15 |
| 310 | */ |
| 311 | |
| 312 | /* Set customized LED registers */ |
| 313 | outw(config->customized_leds, io_base + CMD_LED0_LED1); |
| 314 | printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n", |
| 315 | inw(io_base + CMD_LED0_LED1)); |
| 316 | } |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame] | 317 | } |
| 318 | |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 319 | static void r8168_init(struct device *dev) |
| 320 | { |
| 321 | /* Get the resource of the NIC mmio */ |
| 322 | struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 323 | u16 io_base = (u16)nic_res->base; |
| 324 | |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame] | 325 | /* Check if the base is invalid */ |
| 326 | if (!io_base) { |
Martin Roth | 0949e73 | 2021-10-01 14:28:22 -0600 | [diff] [blame] | 327 | printk(BIOS_ERR, "r8168: Error can't find IO resource\n"); |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame] | 328 | return; |
| 329 | } |
Damien Zammit | f5dd23f | 2016-12-01 23:29:34 +1100 | [diff] [blame] | 330 | /* Enable but do not set bus master */ |
| 331 | pci_write_config16(dev, PCI_COMMAND, |
| 332 | PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
| 333 | |
| 334 | /* Program MAC address based on CBFS "macaddress" containing |
| 335 | * a string AA:BB:CC:DD:EE:FF */ |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame] | 336 | program_mac_address(dev, io_base); |
| 337 | |
| 338 | /* Program customized LED mode */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 339 | if (CONFIG(RT8168_SET_LED_MODE)) |
Gaggery Tsai | 65623ef | 2017-09-29 11:15:23 +0800 | [diff] [blame] | 340 | r8168_set_customized_led(dev, io_base); |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 341 | } |
| 342 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 343 | #if CONFIG(HAVE_ACPI_TABLES) |
Gaggery Tsai | 6919a93 | 2017-11-16 17:21:23 +0800 | [diff] [blame] | 344 | #define R8168_ACPI_HID "R8168" |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 345 | static void r8168_net_fill_ssdt(const struct device *dev) |
Gaggery Tsai | 6919a93 | 2017-11-16 17:21:23 +0800 | [diff] [blame] | 346 | { |
| 347 | struct drivers_net_config *config = dev->chip_info; |
| 348 | const char *path = acpi_device_path(dev->bus->dev); |
| 349 | u32 address; |
| 350 | |
| 351 | if (!path || !config) |
| 352 | return; |
| 353 | |
| 354 | /* Device */ |
| 355 | acpigen_write_scope(path); |
| 356 | acpigen_write_device(acpi_device_name(dev)); |
| 357 | acpigen_write_name_string("_HID", R8168_ACPI_HID); |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 358 | acpi_device_write_uid(dev); |
| 359 | |
Gaggery Tsai | 6919a93 | 2017-11-16 17:21:23 +0800 | [diff] [blame] | 360 | if (dev->chip_ops) |
| 361 | acpigen_write_name_string("_DDN", dev->chip_ops->name); |
| 362 | |
Edward O'Callaghan | b765fa6 | 2020-01-21 21:01:32 +1100 | [diff] [blame] | 363 | /* Power Resource */ |
Arthur Heymans | 59a348b | 2021-10-27 18:27:58 +0200 | [diff] [blame] | 364 | if (CONFIG(RT8168_GEN_ACPI_POWER_RESOURCE) && config->has_power_resource) { |
Edward O'Callaghan | b765fa6 | 2020-01-21 21:01:32 +1100 | [diff] [blame] | 365 | const struct acpi_power_res_params power_res_params = { |
| 366 | .stop_gpio = &config->stop_gpio, |
| 367 | .stop_delay_ms = config->stop_delay_ms, |
| 368 | .stop_off_delay_ms = config->stop_off_delay_ms |
| 369 | }; |
| 370 | acpi_device_add_power_res(&power_res_params); |
| 371 | } |
| 372 | |
Gaggery Tsai | 6919a93 | 2017-11-16 17:21:23 +0800 | [diff] [blame] | 373 | /* Address */ |
| 374 | address = PCI_SLOT(dev->path.pci.devfn) & 0xffff; |
| 375 | address <<= 16; |
| 376 | address |= PCI_FUNC(dev->path.pci.devfn) & 0xffff; |
| 377 | acpigen_write_name_dword("_ADR", address); |
| 378 | |
| 379 | /* Wake capabilities */ |
| 380 | if (config->wake) |
| 381 | acpigen_write_PRW(config->wake, 3); |
| 382 | |
| 383 | acpigen_pop_len(); /* Device */ |
| 384 | acpigen_pop_len(); /* Scope */ |
| 385 | |
| 386 | printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev), |
| 387 | dev->chip_ops ? dev->chip_ops->name : "", dev_path(dev)); |
| 388 | } |
| 389 | |
| 390 | static const char *r8168_net_acpi_name(const struct device *dev) |
| 391 | { |
| 392 | return "RLTK"; |
| 393 | } |
| 394 | #endif |
| 395 | |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 396 | static struct device_operations r8168_ops = { |
Martin Roth | b9810a4 | 2017-07-23 20:00:04 -0600 | [diff] [blame] | 397 | .read_resources = pci_dev_read_resources, |
| 398 | .set_resources = pci_dev_set_resources, |
| 399 | .enable_resources = pci_dev_enable_resources, |
| 400 | .init = r8168_init, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 401 | #if CONFIG(HAVE_ACPI_TABLES) |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 402 | .acpi_name = r8168_net_acpi_name, |
| 403 | .acpi_fill_ssdt = r8168_net_fill_ssdt, |
Gaggery Tsai | 6919a93 | 2017-11-16 17:21:23 +0800 | [diff] [blame] | 404 | #endif |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 405 | }; |
| 406 | |
Alan Huang | 769994c | 2021-11-10 10:59:13 +0800 | [diff] [blame] | 407 | static const unsigned short pci_device_ids[] = { |
| 408 | PCI_DEVICE_ID_REALTEK_8168, |
| 409 | PCI_DEVICE_ID_REALTEK_8125, |
| 410 | 0 |
| 411 | }; |
| 412 | |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 413 | static const struct pci_driver r8168_driver __pci_driver = { |
Martin Roth | b9810a4 | 2017-07-23 20:00:04 -0600 | [diff] [blame] | 414 | .ops = &r8168_ops, |
Alan Huang | 769994c | 2021-11-10 10:59:13 +0800 | [diff] [blame] | 415 | .vendor = PCI_VENDOR_ID_REALTEK, |
| 416 | .devices = pci_device_ids, |
Damien Zammit | e983f0c | 2016-05-21 02:24:19 +1000 | [diff] [blame] | 417 | }; |
Gaggery Tsai | 6919a93 | 2017-11-16 17:21:23 +0800 | [diff] [blame] | 418 | |
| 419 | struct chip_operations drivers_net_ops = { |
| 420 | CHIP_NAME("Realtek r8168") |
| 421 | }; |