blob: 56068594890459a2b303b6c40a0172d7af1bf696 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Damien Zammite983f0c2016-05-21 02:24:19 +10002
3/*
Damien Zammitf5dd23f2016-12-01 23:29:34 +11004 * 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 Zammite983f0c2016-05-21 02:24:19 +10007 * so that if the EEPROM/efuse is unconfigured it still has a default MAC.
8 */
9
Damien Zammitf5dd23f2016-12-01 23:29:34 +110010#include <cbfs.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070011#include <acpi/acpi_device.h>
12#include <acpi/acpigen.h>
Damien Zammitf5dd23f2016-12-01 23:29:34 +110013#include <string.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100014#include <arch/io.h>
Shelley Chen0528b612017-06-12 18:29:24 -070015#include <console/console.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100016#include <device/device.h>
17#include <device/pci.h>
18#include <device/pci_ops.h>
19#include <device/pci_def.h>
Alan Huang769994c2021-11-10 10:59:13 +080020#include <device/pci_ids.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100021#include <delay.h>
Shelley Chen0528b612017-06-12 18:29:24 -070022#include <fmap.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020023#include <types.h>
24
Gaggery Tsai65623ef2017-09-29 11:15:23 +080025#include "chip.h"
Damien Zammite983f0c2016-05-21 02:24:19 +100026
27#define NIC_TIMEOUT 1000
28
29#define CMD_REG 0x37
30#define CMD_REG_RESET 0x10
Gaggery Tsai65623ef2017-09-29 11:15:23 +080031#define CMD_LED0_LED1 0x18
Damien Zammite983f0c2016-05-21 02:24:19 +100032
33#define CFG_9346 0x50
34#define CFG_9346_LOCK 0x00
35#define CFG_9346_UNLOCK 0xc0
36
Gaggery Tsai1f847042017-12-26 17:13:52 +080037#define DEVICE_INDEX_BYTE 12
38#define MAX_DEVICE_SUPPORT 10
Shelley Chen0528b612017-06-12 18:29:24 -070039/**
40 * search: Find first instance of string in a given region
41 * @param p string to find
42 * @param a start address of region to search
43 * @param lengthp length of string to search for
44 * @param lengtha length of region to search in
45 * @return offset offset from start address a where string was found.
46 * If string not found, return lengtha.
47 */
48static size_t search(const char *p, const u8 *a, size_t lengthp,
49 size_t lengtha)
50{
51 size_t i, j;
52
53 if (lengtha < lengthp)
54 return lengtha;
55 /* Searching */
56 for (j = 0; j <= lengtha - lengthp; j++) {
57 for (i = 0; i < lengthp && p[i] == a[i + j]; i++)
58 ;
David Wu9a0d9e02018-05-04 15:37:37 +080059 if (i >= lengthp && a[j - 1] == lengthp)
Shelley Chen0528b612017-06-12 18:29:24 -070060 return j;
61 }
62 return lengtha;
63}
64
Damien Zammitf5dd23f2016-12-01 23:29:34 +110065static u8 get_hex_digit(const u8 c)
Damien Zammite983f0c2016-05-21 02:24:19 +100066{
Damien Zammitf5dd23f2016-12-01 23:29:34 +110067 u8 ret = 0;
Damien Zammite983f0c2016-05-21 02:24:19 +100068
Damien Zammitf5dd23f2016-12-01 23:29:34 +110069 ret = c - '0';
70 if (ret > 0x09) {
71 ret = c - 'A' + 0x0a;
72 if (ret > 0x0f)
73 ret = c - 'a' + 0x0a;
74 }
75 if (ret > 0x0f) {
Shelley Chen0528b612017-06-12 18:29:24 -070076 printk(BIOS_ERR, "Error: Invalid hex digit found: "
77 "%c - 0x%02x\n", (char)c, c);
Damien Zammitf5dd23f2016-12-01 23:29:34 +110078 ret = 0;
79 }
80 return ret;
81}
Damien Zammite983f0c2016-05-21 02:24:19 +100082
Shelley Chen0528b612017-06-12 18:29:24 -070083#define MACLEN 17
84
Edward O'Callaghan2a82f742020-03-25 12:52:50 +110085/* Returns MAC address based on the key that is passed in. */
86static enum cb_err fetch_mac_vpd_key(u8 *macstrbuf, const char *vpd_key)
Shelley Chen0528b612017-06-12 18:29:24 -070087{
88 struct region_device rdev;
89 void *search_address;
90 size_t search_length;
91 size_t offset;
Edward O'Callaghan2a82f742020-03-25 12:52:50 +110092
93 if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) {
94 printk(BIOS_ERR, "Error: Couldn't find RO_VPD region.");
95 return CB_ERR;
96 }
97 search_address = rdev_mmap_full(&rdev);
98 if (search_address == NULL) {
99 printk(BIOS_ERR, "LAN: VPD not found.\n");
100 return CB_ERR;
101 }
102
103 search_length = region_device_sz(&rdev);
104 offset = search(vpd_key, search_address, strlen(vpd_key),
105 search_length);
106
107 if (offset == search_length) {
108 printk(BIOS_ERR,
109 "Error: Could not locate '%s' in VPD\n", vpd_key);
Edward O'Callaghand08bc5a2020-03-26 14:37:37 +1100110 rdev_munmap(&rdev, search_address);
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100111 return CB_ERR;
112 }
113 printk(BIOS_DEBUG, "Located '%s' in VPD\n", vpd_key);
114
115 offset += strlen(vpd_key) + 1; /* move to next character */
116
117 if (offset + MACLEN > search_length) {
Edward O'Callaghand08bc5a2020-03-26 14:37:37 +1100118 rdev_munmap(&rdev, search_address);
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100119 printk(BIOS_ERR, "Search result too small!\n");
120 return CB_ERR;
121 }
122 memcpy(macstrbuf, search_address + offset, MACLEN);
Edward O'Callaghand08bc5a2020-03-26 14:37:37 +1100123 rdev_munmap(&rdev, search_address);
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100124
125 return CB_SUCCESS;
126}
127
128/* Prepares vpd_key by concatenating ethernet_mac with device_index */
129static enum cb_err fetch_mac_vpd_dev_idx(u8 *macstrbuf, u8 device_index)
130{
Gaggery Tsai1f847042017-12-26 17:13:52 +0800131 char key[] = "ethernet_mac "; /* Leave a space at tail to stuff an index */
132
133 /*
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100134 * Map each NIC on the DUT to "ethernet_macN", where N is [0-9].
135 * Translate index number from integer to ascii by adding '0' char.
Gaggery Tsai1f847042017-12-26 17:13:52 +0800136 */
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100137 key[DEVICE_INDEX_BYTE] = device_index + '0';
Shelley Chen0528b612017-06-12 18:29:24 -0700138
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100139 return fetch_mac_vpd_key(macstrbuf, key);
140}
141
142static void fetch_mac_string_vpd(struct drivers_net_config *config, u8 *macstrbuf)
143{
144 if (!config)
145 return;
146
147 /* Current implementation is up to 10 NIC cards */
148 if (config->device_index > MAX_DEVICE_SUPPORT) {
149 printk(BIOS_ERR, "r8168: the maximum device_index should be less then %d\n."
150 " Using default 00:e0:4c:00:c0:b0\n", MAX_DEVICE_SUPPORT);
151 return;
Shelley Chen0528b612017-06-12 18:29:24 -0700152 }
153
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100154 if (fetch_mac_vpd_dev_idx(macstrbuf, config->device_index) == CB_SUCCESS)
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100155 return;
Gaggery Tsai1f847042017-12-26 17:13:52 +0800156
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100157 if (!CONFIG(RT8168_SUPPORT_LEGACY_VPD_MAC)) {
158 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
159 " using default 00:e0:4c:00:c0:b0\n");
160 return;
161 }
162
Edward O'Callaghan2a0a02f2020-03-26 16:47:55 +1100163 if (fetch_mac_vpd_key(macstrbuf, "ethernet_mac") != CB_SUCCESS)
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100164 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
165 " using default 00:e0:4c:00:c0:b0\n");
Shelley Chen0528b612017-06-12 18:29:24 -0700166}
167
168static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
169{
Julius Werner77639e42021-02-05 16:51:25 -0800170 if (!cbfs_load("rt8168-macaddress", macstrbuf, MACLEN)) {
171 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
172 return CB_ERR;
Shelley Chen0528b612017-06-12 18:29:24 -0700173 }
Julius Werner77639e42021-02-05 16:51:25 -0800174 return CB_SUCCESS;
Shelley Chen0528b612017-06-12 18:29:24 -0700175}
176
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100177static void get_mac_address(u8 *macaddr, const u8 *strbuf)
178{
179 size_t offset = 0;
180 int i;
181
Shelley Chen0528b612017-06-12 18:29:24 -0700182 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
183 (strbuf[8] != ':') || (strbuf[11] != ':') ||
184 (strbuf[14] != ':')) {
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100185 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
186 return;
187 }
188
189 for (i = 0; i < 6; i++) {
190 macaddr[i] = 0;
191 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
192 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
193 offset += 3;
194 }
195}
196
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100197static void program_mac_address(struct device *dev, u16 io_base)
198{
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100199 u8 macstrbuf[MACLEN] = { 0 };
200 int i = 0;
Shelley Chen0528b612017-06-12 18:29:24 -0700201 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100202 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
Gaggery Tsai1f847042017-12-26 17:13:52 +0800203 struct drivers_net_config *config = dev->chip_info;
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100204
Shelley Chen0528b612017-06-12 18:29:24 -0700205 /* check the VPD for the mac address */
Julius Wernercd49cce2019-03-05 16:53:33 -0800206 if (CONFIG(RT8168_GET_MAC_FROM_VPD)) {
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100207 fetch_mac_string_vpd(config, macstrbuf);
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100208 } else {
Shelley Chen0528b612017-06-12 18:29:24 -0700209 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS)
210 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS,"
Gaggery Tsai1f847042017-12-26 17:13:52 +0800211 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100212 }
Shelley Chen0528b612017-06-12 18:29:24 -0700213 get_mac_address(mac, macstrbuf);
Damien Zammite983f0c2016-05-21 02:24:19 +1000214
215 /* Reset NIC */
216 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100217 outb(CMD_REG_RESET, io_base + CMD_REG);
Damien Zammite983f0c2016-05-21 02:24:19 +1000218
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100219 /* Poll for reset, with 1sec timeout */
220 while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) {
Damien Zammite983f0c2016-05-21 02:24:19 +1000221 udelay(1000);
222 if (++i >= NIC_TIMEOUT)
Shelley Chen0528b612017-06-12 18:29:24 -0700223 printk(BIOS_ERR, "timeout waiting for nic to reset\n");
Damien Zammite983f0c2016-05-21 02:24:19 +1000224 }
225 if (i < NIC_TIMEOUT)
226 printk(BIOS_DEBUG, "done\n");
227
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100228 printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
Damien Zammite983f0c2016-05-21 02:24:19 +1000229
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100230 /* Disable register protection */
231 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
Damien Zammite983f0c2016-05-21 02:24:19 +1000232
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100233 /* Set MAC address: only 4-byte write accesses allowed */
234 outl(mac[4] | mac[5] << 8, io_base + 4);
235 inl(io_base + 4);
236 outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
237 io_base);
238 inl(io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000239 /* Lock config regs */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100240 outb(CFG_9346_LOCK, io_base + CFG_9346);
241
242 printk(BIOS_DEBUG, "done\n");
243}
244
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800245static void r8168_set_customized_led(struct device *dev, u16 io_base)
246{
247 struct drivers_net_config *config = dev->chip_info;
248
249 if (!config)
250 return;
251
252 /* Read the customized LED setting from devicetree */
253 printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds);
254
255 /*
256 * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration
257 * Starting from offset 0x18
258 * Bit[15:12] LED Feature Control(FC)
259 * Bit[11:08] LED Select for PINLED2
260 * Bit[07:04] LED Select for PINLED1
261 * Bit[03:00] LED Select for PINLED0
262 *
263 * Speed Link10M Link100M Link1000M ACT/Full
264 * LED0 Bit0 Bit1 Bit2 Bit3
265 * LED1 Bit4 Bit5 Bit6 Bit7
266 * LED2 Bit8 Bit9 Bit10 Bit11
267 * FC Bit12 Bit13 Bit14 Bit15
268 */
269
270 /* Set customized LED registers */
271 outw(config->customized_leds, io_base + CMD_LED0_LED1);
272 printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n",
273 inw(io_base + CMD_LED0_LED1));
274}
275
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100276static void r8168_init(struct device *dev)
277{
278 /* Get the resource of the NIC mmio */
279 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
280 u16 io_base = (u16)nic_res->base;
281
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800282 /* Check if the base is invalid */
283 if (!io_base) {
Martin Roth0949e732021-10-01 14:28:22 -0600284 printk(BIOS_ERR, "r8168: Error can't find IO resource\n");
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800285 return;
286 }
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100287 /* Enable but do not set bus master */
288 pci_write_config16(dev, PCI_COMMAND,
289 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
290
291 /* Program MAC address based on CBFS "macaddress" containing
292 * a string AA:BB:CC:DD:EE:FF */
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800293 program_mac_address(dev, io_base);
294
295 /* Program customized LED mode */
Julius Wernercd49cce2019-03-05 16:53:33 -0800296 if (CONFIG(RT8168_SET_LED_MODE))
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800297 r8168_set_customized_led(dev, io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000298}
299
Julius Wernercd49cce2019-03-05 16:53:33 -0800300#if CONFIG(HAVE_ACPI_TABLES)
Gaggery Tsai6919a932017-11-16 17:21:23 +0800301#define R8168_ACPI_HID "R8168"
Furquan Shaikh7536a392020-04-24 21:59:21 -0700302static void r8168_net_fill_ssdt(const struct device *dev)
Gaggery Tsai6919a932017-11-16 17:21:23 +0800303{
304 struct drivers_net_config *config = dev->chip_info;
305 const char *path = acpi_device_path(dev->bus->dev);
306 u32 address;
307
308 if (!path || !config)
309 return;
310
311 /* Device */
312 acpigen_write_scope(path);
313 acpigen_write_device(acpi_device_name(dev));
314 acpigen_write_name_string("_HID", R8168_ACPI_HID);
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100315 acpi_device_write_uid(dev);
316
Gaggery Tsai6919a932017-11-16 17:21:23 +0800317 if (dev->chip_ops)
318 acpigen_write_name_string("_DDN", dev->chip_ops->name);
319
Edward O'Callaghanb765fa62020-01-21 21:01:32 +1100320 /* Power Resource */
Arthur Heymans59a348b2021-10-27 18:27:58 +0200321 if (CONFIG(RT8168_GEN_ACPI_POWER_RESOURCE) && config->has_power_resource) {
Edward O'Callaghanb765fa62020-01-21 21:01:32 +1100322 const struct acpi_power_res_params power_res_params = {
323 .stop_gpio = &config->stop_gpio,
324 .stop_delay_ms = config->stop_delay_ms,
325 .stop_off_delay_ms = config->stop_off_delay_ms
326 };
327 acpi_device_add_power_res(&power_res_params);
328 }
329
Gaggery Tsai6919a932017-11-16 17:21:23 +0800330 /* Address */
331 address = PCI_SLOT(dev->path.pci.devfn) & 0xffff;
332 address <<= 16;
333 address |= PCI_FUNC(dev->path.pci.devfn) & 0xffff;
334 acpigen_write_name_dword("_ADR", address);
335
336 /* Wake capabilities */
337 if (config->wake)
338 acpigen_write_PRW(config->wake, 3);
339
340 acpigen_pop_len(); /* Device */
341 acpigen_pop_len(); /* Scope */
342
343 printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
344 dev->chip_ops ? dev->chip_ops->name : "", dev_path(dev));
345}
346
347static const char *r8168_net_acpi_name(const struct device *dev)
348{
349 return "RLTK";
350}
351#endif
352
Damien Zammite983f0c2016-05-21 02:24:19 +1000353static struct device_operations r8168_ops = {
Martin Rothb9810a42017-07-23 20:00:04 -0600354 .read_resources = pci_dev_read_resources,
355 .set_resources = pci_dev_set_resources,
356 .enable_resources = pci_dev_enable_resources,
357 .init = r8168_init,
Julius Wernercd49cce2019-03-05 16:53:33 -0800358#if CONFIG(HAVE_ACPI_TABLES)
Nico Huber68680dd2020-03-31 17:34:52 +0200359 .acpi_name = r8168_net_acpi_name,
360 .acpi_fill_ssdt = r8168_net_fill_ssdt,
Gaggery Tsai6919a932017-11-16 17:21:23 +0800361#endif
Damien Zammite983f0c2016-05-21 02:24:19 +1000362};
363
Alan Huang769994c2021-11-10 10:59:13 +0800364static const unsigned short pci_device_ids[] = {
365 PCI_DEVICE_ID_REALTEK_8168,
366 PCI_DEVICE_ID_REALTEK_8125,
367 0
368};
369
Damien Zammite983f0c2016-05-21 02:24:19 +1000370static const struct pci_driver r8168_driver __pci_driver = {
Martin Rothb9810a42017-07-23 20:00:04 -0600371 .ops = &r8168_ops,
Alan Huang769994c2021-11-10 10:59:13 +0800372 .vendor = PCI_VENDOR_ID_REALTEK,
373 .devices = pci_device_ids,
Damien Zammite983f0c2016-05-21 02:24:19 +1000374};
Gaggery Tsai6919a932017-11-16 17:21:23 +0800375
376struct chip_operations drivers_net_ops = {
377 CHIP_NAME("Realtek r8168")
378};