1 /* SPDX-License-Identifier: GPL-2.0-only */
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
7 * so that if the EEPROM/efuse is unconfigured it still has a default MAC.
11 #include <acpi/acpi_device.h>
12 #include <acpi/acpigen.h>
15 #include <console/console.h>
16 #include <device/device.h>
17 #include <device/pci.h>
18 #include <device/pci_ops.h>
19 #include <device/pci_ids.h>
26 #define NIC_TIMEOUT 1000
29 #define CMD_REG_RESET 0x10
30 #define CMD_LED0_LED1 0x18
31 #define CMD_LED_FEATURE 0x94
32 #define CMD_LEDSEL0 0x18
33 #define CMD_LEDSEL2 0x84
36 #define CFG_9346_LOCK 0x00
37 #define CFG_9346_UNLOCK 0xc0
38 #define CMD_REG_ASPM 0xb0
39 #define ASPM_L1_2_MASK 0xe059000f
41 #define DEVICE_INDEX_BYTE 12
42 #define MAX_DEVICE_SUPPORT 10
44 * search: Find first instance of string in a given region
45 * @param p string to find
46 * @param a start address of region to search
47 * @param lengthp length of string to search for
48 * @param lengtha length of region to search in
49 * @return offset offset from start address a where string was found.
50 * If string not found, return lengtha.
52 static size_t search(const char *p
, const u8
*a
, size_t lengthp
,
57 if (lengtha
< lengthp
)
60 for (j
= 0; j
<= lengtha
- lengthp
; j
++) {
61 for (i
= 0; i
< lengthp
&& p
[i
] == a
[i
+ j
]; i
++)
63 if (i
>= lengthp
&& a
[j
- 1] == lengthp
)
69 static u8
get_hex_digit(const u8 c
)
80 printk(BIOS_ERR
, "Invalid hex digit found: "
81 "%c - 0x%02x\n", (char)c
, c
);
89 /* Returns MAC address based on the key that is passed in. */
90 static enum cb_err
fetch_mac_vpd_key(u8
*macstrbuf
, const char *vpd_key
)
92 struct region_device rdev
;
97 if (fmap_locate_area_as_rdev("RO_VPD", &rdev
)) {
98 printk(BIOS_ERR
, "Couldn't find RO_VPD region.");
101 search_address
= rdev_mmap_full(&rdev
);
102 if (search_address
== NULL
) {
103 printk(BIOS_ERR
, "LAN: VPD not found.\n");
107 search_length
= region_device_sz(&rdev
);
108 offset
= search(vpd_key
, search_address
, strlen(vpd_key
),
111 if (offset
== search_length
) {
112 printk(BIOS_WARNING
, "Could not locate '%s' in VPD\n", vpd_key
);
113 rdev_munmap(&rdev
, search_address
);
116 printk(BIOS_DEBUG
, "Located '%s' in VPD\n", vpd_key
);
118 offset
+= strlen(vpd_key
) + 1; /* move to next character */
120 if (offset
+ MACLEN
> search_length
) {
121 rdev_munmap(&rdev
, search_address
);
122 printk(BIOS_ERR
, "Search result too small!\n");
125 memcpy(macstrbuf
, search_address
+ offset
, MACLEN
);
126 rdev_munmap(&rdev
, search_address
);
131 /* Prepares vpd_key by concatenating ethernet_mac with device_index */
132 static enum cb_err
fetch_mac_vpd_dev_idx(u8
*macstrbuf
, u8 device_index
)
134 char key
[] = "ethernet_mac "; /* Leave a space at tail to stuff an index */
137 * Map each NIC on the DUT to "ethernet_macN", where N is [0-9].
138 * Translate index number from integer to ascii by adding '0' char.
140 key
[DEVICE_INDEX_BYTE
] = device_index
+ '0';
142 return fetch_mac_vpd_key(macstrbuf
, key
);
145 static void fetch_mac_string_vpd(struct drivers_net_config
*config
, u8
*macstrbuf
)
150 /* Current implementation is up to 10 NIC cards */
151 if (config
->device_index
> MAX_DEVICE_SUPPORT
) {
152 printk(BIOS_ERR
, "r8168: the maximum device_index should be less then %d\n."
153 " Using default 00:e0:4c:00:c0:b0\n", MAX_DEVICE_SUPPORT
);
157 if (fetch_mac_vpd_dev_idx(macstrbuf
, config
->device_index
) == CB_SUCCESS
)
160 if (!CONFIG(RT8168_SUPPORT_LEGACY_VPD_MAC
)) {
161 printk(BIOS_ERR
, "r8168: mac address not found in VPD,"
162 " using default 00:e0:4c:00:c0:b0\n");
166 if (fetch_mac_vpd_key(macstrbuf
, "ethernet_mac") != CB_SUCCESS
)
167 printk(BIOS_ERR
, "r8168: mac address not found in VPD,"
168 " using default 00:e0:4c:00:c0:b0\n");
171 static enum cb_err
fetch_mac_string_cbfs(u8
*macstrbuf
)
173 if (!cbfs_load("rt8168-macaddress", macstrbuf
, MACLEN
)) {
174 printk(BIOS_ERR
, "r8168: Error reading MAC from CBFS\n");
180 static void get_mac_address(u8
*macaddr
, const u8
*strbuf
)
185 if ((strbuf
[2] != ':') || (strbuf
[5] != ':') ||
186 (strbuf
[8] != ':') || (strbuf
[11] != ':') ||
187 (strbuf
[14] != ':')) {
188 printk(BIOS_ERR
, "r8168: ignore invalid MAC address in cbfs\n");
192 for (i
= 0; i
< 6; i
++) {
194 macaddr
[i
] |= get_hex_digit(strbuf
[offset
]) << 4;
195 macaddr
[i
] |= get_hex_digit(strbuf
[offset
+ 1]);
200 static void program_mac_address(struct device
*dev
, u16 io_base
)
202 u8 macstrbuf
[MACLEN
] = { 0 };
204 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
205 u8 mac
[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
206 struct drivers_net_config
*config
= dev
->chip_info
;
208 /* check the VPD for the mac address */
209 if (CONFIG(RT8168_GET_MAC_FROM_VPD
)) {
210 fetch_mac_string_vpd(config
, macstrbuf
);
212 if (fetch_mac_string_cbfs(macstrbuf
) != CB_SUCCESS
)
213 printk(BIOS_ERR
, "r8168: Error reading MAC from CBFS,"
214 " using default 00:e0:4c:00:c0:b0\n");
216 get_mac_address(mac
, macstrbuf
);
219 printk(BIOS_DEBUG
, "r8168: Resetting NIC...");
220 outb(CMD_REG_RESET
, io_base
+ CMD_REG
);
222 /* Poll for reset, with 1sec timeout */
223 while (i
< NIC_TIMEOUT
&& (inb(io_base
+ CMD_REG
) & CMD_REG_RESET
)) {
225 if (++i
>= NIC_TIMEOUT
)
226 printk(BIOS_ERR
, "timeout waiting for nic to reset\n");
229 printk(BIOS_DEBUG
, "done\n");
231 printk(BIOS_DEBUG
, "r8168: Programming MAC Address...");
233 /* Disable register protection */
234 outb(CFG_9346_UNLOCK
, io_base
+ CFG_9346
);
236 /* Set MAC address: only 4-byte write accesses allowed */
237 outl(mac
[4] | mac
[5] << 8, io_base
+ 4);
239 outl(mac
[0] | mac
[1] << 8 | mac
[2] << 16 | mac
[3] << 24,
242 /* Lock config regs */
243 outb(CFG_9346_LOCK
, io_base
+ CFG_9346
);
245 printk(BIOS_DEBUG
, "done\n");
248 static void enable_aspm_l1_2(u16 io_base
)
250 printk(BIOS_INFO
, "rtl: Enable ASPM L1.2\n");
252 /* Disable register protection */
253 outb(CFG_9346_UNLOCK
, io_base
+ CFG_9346
);
255 /* Enable ASPM_L1.2 */
256 outl(ASPM_L1_2_MASK
, io_base
+ CMD_REG_ASPM
);
258 /* Lock config regs */
259 outb(CFG_9346_LOCK
, io_base
+ CFG_9346
);
262 static void r8168_set_customized_led(struct device
*dev
, u16 io_base
)
264 struct drivers_net_config
*config
= dev
->chip_info
;
269 if (dev
->device
== PCI_DID_REALTEK_8125
) {
270 /* Set LED global Feature register */
271 outb(config
->led_feature
, io_base
+ CMD_LED_FEATURE
);
272 printk(BIOS_DEBUG
, "r8125: read back LED global feature setting as 0x%x\n",
273 inb(io_base
+ CMD_LED_FEATURE
));
276 * Refer to RTL8125 datasheet 5.Customizable LED Configuration
277 * Register Name IO Address
282 * LEDSEL Bit[] Description
288 * Bit10 preboot enable
290 * Bit12 active low/high
292 * LEDFEATURE Description
293 * Bit0 LED Table V1/V2
295 * Bit4~5 LED Blinking Duty Cycle 12.5%/ 25%/ 50%/ 75%
296 * Bit6~7 LED Blinking Freq. 240ms/160ms/80ms/Link-Speed-Dependent
299 /* Set customized LED0 register */
300 outw(config
->customized_led0
, io_base
+ CMD_LEDSEL0
);
301 printk(BIOS_DEBUG
, "r8125: read back LED0 setting as 0x%x\n",
302 inw(io_base
+ CMD_LEDSEL0
));
304 /* Set customized LED2 register */
305 outw(config
->customized_led2
, io_base
+ CMD_LEDSEL2
);
306 printk(BIOS_DEBUG
, "r8125: read back LED2 setting as 0x%x\n",
307 inw(io_base
+ CMD_LEDSEL2
));
309 /* Read the customized LED setting from devicetree */
310 printk(BIOS_DEBUG
, "r8168: Customized LED 0x%x\n", config
->customized_leds
);
313 * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration
314 * Starting from offset 0x18
315 * Bit[15:12] LED Feature Control(FC)
316 * Bit[11:08] LED Select for PINLED2
317 * Bit[07:04] LED Select for PINLED1
318 * Bit[03:00] LED Select for PINLED0
320 * Speed Link10M Link100M Link1000M ACT/Full
321 * LED0 Bit0 Bit1 Bit2 Bit3
322 * LED1 Bit4 Bit5 Bit6 Bit7
323 * LED2 Bit8 Bit9 Bit10 Bit11
324 * FC Bit12 Bit13 Bit14 Bit15
327 /* Set customized LED registers */
328 outw(config
->customized_leds
, io_base
+ CMD_LED0_LED1
);
329 printk(BIOS_DEBUG
, "r8168: read back LED setting as 0x%x\n",
330 inw(io_base
+ CMD_LED0_LED1
));
334 static void r8168_init(struct device
*dev
)
336 /* Get the resource of the NIC mmio */
337 struct resource
*nic_res
= find_resource(dev
, PCI_BASE_ADDRESS_0
);
338 u16 io_base
= (u16
)nic_res
->base
;
340 /* Check if the base is invalid */
342 printk(BIOS_ERR
, "r8168: Error can't find IO resource\n");
345 /* Enable but do not set bus master */
346 pci_write_config16(dev
, PCI_COMMAND
,
347 PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
);
349 /* Program MAC address based on CBFS "macaddress" containing
350 * a string AA:BB:CC:DD:EE:FF */
351 program_mac_address(dev
, io_base
);
353 /* Program customized LED mode */
354 if (CONFIG(RT8168_SET_LED_MODE
))
355 r8168_set_customized_led(dev
, io_base
);
357 struct drivers_net_config
*config
= dev
->chip_info
;
358 if (CONFIG(PCIEXP_ASPM
) && config
->enable_aspm_l1_2
)
359 enable_aspm_l1_2(io_base
);
362 #if CONFIG(HAVE_ACPI_TABLES)
363 #define R8168_ACPI_HID "R8168"
364 static void r8168_net_fill_ssdt(const struct device
*dev
)
366 struct drivers_net_config
*config
= dev
->chip_info
;
367 const char *path
= acpi_device_path(dev
->bus
->dev
);
370 if (!path
|| !config
)
374 acpigen_write_scope(path
);
375 acpigen_write_device(acpi_device_name(dev
));
376 acpigen_write_name_string("_HID", R8168_ACPI_HID
);
377 acpi_device_write_uid(dev
);
380 acpigen_write_name_string("_DDN", dev
->chip_ops
->name
);
383 if (CONFIG(RT8168_GEN_ACPI_POWER_RESOURCE
) && config
->has_power_resource
) {
384 const struct acpi_power_res_params power_res_params
= {
385 .stop_gpio
= &config
->stop_gpio
,
386 .stop_delay_ms
= config
->stop_delay_ms
,
387 .stop_off_delay_ms
= config
->stop_off_delay_ms
389 acpi_device_add_power_res(&power_res_params
);
393 address
= PCI_SLOT(dev
->path
.pci
.devfn
) & 0xffff;
395 address
|= PCI_FUNC(dev
->path
.pci
.devfn
) & 0xffff;
396 acpigen_write_name_dword("_ADR", address
);
398 /* Wake capabilities */
400 acpigen_write_PRW(config
->wake
, 3);
402 if (config
->add_acpi_dma_property
)
403 acpi_device_add_dma_property(NULL
);
405 acpigen_pop_len(); /* Device */
406 acpigen_pop_len(); /* Scope */
408 printk(BIOS_INFO
, "%s.%s: %s %s\n", path
, acpi_device_name(dev
),
409 dev
->chip_ops
? dev
->chip_ops
->name
: "", dev_path(dev
));
412 static const char *r8168_net_acpi_name(const struct device
*dev
)
418 static struct device_operations r8168_ops
= {
419 .read_resources
= pci_dev_read_resources
,
420 .set_resources
= pci_dev_set_resources
,
421 .enable_resources
= pci_dev_enable_resources
,
423 #if CONFIG(HAVE_ACPI_TABLES)
424 .acpi_name
= r8168_net_acpi_name
,
425 .acpi_fill_ssdt
= r8168_net_fill_ssdt
,
429 static const unsigned short pci_device_ids
[] = {
430 PCI_DID_REALTEK_8168
,
431 PCI_DID_REALTEK_8125
,
432 PCI_DID_REALTEK_8111
,
436 static const struct pci_driver r8168_driver __pci_driver
= {
438 .vendor
= PCI_VID_REALTEK
,
439 .devices
= pci_device_ids
,
442 struct chip_operations drivers_net_ops
= {
443 CHIP_NAME("Realtek r8168")