blob: 1fd6eddbb3209af2073674caf4e759860f43e464 [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>
20#include <delay.h>
Shelley Chen0528b612017-06-12 18:29:24 -070021#include <fmap.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020022#include <types.h>
23
Gaggery Tsai65623ef2017-09-29 11:15:23 +080024#include "chip.h"
Damien Zammite983f0c2016-05-21 02:24:19 +100025
26#define NIC_TIMEOUT 1000
27
28#define CMD_REG 0x37
29#define CMD_REG_RESET 0x10
Gaggery Tsai65623ef2017-09-29 11:15:23 +080030#define CMD_LED0_LED1 0x18
Damien Zammite983f0c2016-05-21 02:24:19 +100031
32#define CFG_9346 0x50
33#define CFG_9346_LOCK 0x00
34#define CFG_9346_UNLOCK 0xc0
35
Gaggery Tsai1f847042017-12-26 17:13:52 +080036#define DEVICE_INDEX_BYTE 12
37#define MAX_DEVICE_SUPPORT 10
Shelley Chen0528b612017-06-12 18:29:24 -070038/**
39 * search: Find first instance of string in a given region
40 * @param p string to find
41 * @param a start address of region to search
42 * @param lengthp length of string to search for
43 * @param lengtha length of region to search in
44 * @return offset offset from start address a where string was found.
45 * If string not found, return lengtha.
46 */
47static size_t search(const char *p, const u8 *a, size_t lengthp,
48 size_t lengtha)
49{
50 size_t i, j;
51
52 if (lengtha < lengthp)
53 return lengtha;
54 /* Searching */
55 for (j = 0; j <= lengtha - lengthp; j++) {
56 for (i = 0; i < lengthp && p[i] == a[i + j]; i++)
57 ;
David Wu9a0d9e02018-05-04 15:37:37 +080058 if (i >= lengthp && a[j - 1] == lengthp)
Shelley Chen0528b612017-06-12 18:29:24 -070059 return j;
60 }
61 return lengtha;
62}
63
Damien Zammitf5dd23f2016-12-01 23:29:34 +110064static u8 get_hex_digit(const u8 c)
Damien Zammite983f0c2016-05-21 02:24:19 +100065{
Damien Zammitf5dd23f2016-12-01 23:29:34 +110066 u8 ret = 0;
Damien Zammite983f0c2016-05-21 02:24:19 +100067
Damien Zammitf5dd23f2016-12-01 23:29:34 +110068 ret = c - '0';
69 if (ret > 0x09) {
70 ret = c - 'A' + 0x0a;
71 if (ret > 0x0f)
72 ret = c - 'a' + 0x0a;
73 }
74 if (ret > 0x0f) {
Shelley Chen0528b612017-06-12 18:29:24 -070075 printk(BIOS_ERR, "Error: Invalid hex digit found: "
76 "%c - 0x%02x\n", (char)c, c);
Damien Zammitf5dd23f2016-12-01 23:29:34 +110077 ret = 0;
78 }
79 return ret;
80}
Damien Zammite983f0c2016-05-21 02:24:19 +100081
Shelley Chen0528b612017-06-12 18:29:24 -070082#define MACLEN 17
83
Edward O'Callaghan2a82f742020-03-25 12:52:50 +110084/* Returns MAC address based on the key that is passed in. */
85static enum cb_err fetch_mac_vpd_key(u8 *macstrbuf, const char *vpd_key)
Shelley Chen0528b612017-06-12 18:29:24 -070086{
87 struct region_device rdev;
88 void *search_address;
89 size_t search_length;
90 size_t offset;
Edward O'Callaghan2a82f742020-03-25 12:52:50 +110091
92 if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) {
93 printk(BIOS_ERR, "Error: Couldn't find RO_VPD region.");
94 return CB_ERR;
95 }
96 search_address = rdev_mmap_full(&rdev);
97 if (search_address == NULL) {
98 printk(BIOS_ERR, "LAN: VPD not found.\n");
99 return CB_ERR;
100 }
101
102 search_length = region_device_sz(&rdev);
103 offset = search(vpd_key, search_address, strlen(vpd_key),
104 search_length);
105
106 if (offset == search_length) {
107 printk(BIOS_ERR,
108 "Error: Could not locate '%s' in VPD\n", vpd_key);
Edward O'Callaghand08bc5a2020-03-26 14:37:37 +1100109 rdev_munmap(&rdev, search_address);
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100110 return CB_ERR;
111 }
112 printk(BIOS_DEBUG, "Located '%s' in VPD\n", vpd_key);
113
114 offset += strlen(vpd_key) + 1; /* move to next character */
115
116 if (offset + MACLEN > search_length) {
Edward O'Callaghand08bc5a2020-03-26 14:37:37 +1100117 rdev_munmap(&rdev, search_address);
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100118 printk(BIOS_ERR, "Search result too small!\n");
119 return CB_ERR;
120 }
121 memcpy(macstrbuf, search_address + offset, MACLEN);
Edward O'Callaghand08bc5a2020-03-26 14:37:37 +1100122 rdev_munmap(&rdev, search_address);
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100123
124 return CB_SUCCESS;
125}
126
127/* Prepares vpd_key by concatenating ethernet_mac with device_index */
128static enum cb_err fetch_mac_vpd_dev_idx(u8 *macstrbuf, u8 device_index)
129{
Gaggery Tsai1f847042017-12-26 17:13:52 +0800130 char key[] = "ethernet_mac "; /* Leave a space at tail to stuff an index */
131
132 /*
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100133 * Map each NIC on the DUT to "ethernet_macN", where N is [0-9].
134 * Translate index number from integer to ascii by adding '0' char.
Gaggery Tsai1f847042017-12-26 17:13:52 +0800135 */
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100136 key[DEVICE_INDEX_BYTE] = device_index + '0';
Shelley Chen0528b612017-06-12 18:29:24 -0700137
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100138 return fetch_mac_vpd_key(macstrbuf, key);
139}
140
141static void fetch_mac_string_vpd(struct drivers_net_config *config, u8 *macstrbuf)
142{
143 if (!config)
144 return;
145
146 /* Current implementation is up to 10 NIC cards */
147 if (config->device_index > MAX_DEVICE_SUPPORT) {
148 printk(BIOS_ERR, "r8168: the maximum device_index should be less then %d\n."
149 " Using default 00:e0:4c:00:c0:b0\n", MAX_DEVICE_SUPPORT);
150 return;
Shelley Chen0528b612017-06-12 18:29:24 -0700151 }
152
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100153 if (fetch_mac_vpd_dev_idx(macstrbuf, config->device_index) == CB_SUCCESS)
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100154 return;
Gaggery Tsai1f847042017-12-26 17:13:52 +0800155
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100156 if (!CONFIG(RT8168_SUPPORT_LEGACY_VPD_MAC)) {
157 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
158 " using default 00:e0:4c:00:c0:b0\n");
159 return;
160 }
161
Edward O'Callaghan2a0a02f2020-03-26 16:47:55 +1100162 if (fetch_mac_vpd_key(macstrbuf, "ethernet_mac") != CB_SUCCESS)
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100163 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
164 " using default 00:e0:4c:00:c0:b0\n");
Shelley Chen0528b612017-06-12 18:29:24 -0700165}
166
167static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
168{
Julius Werner77639e42021-02-05 16:51:25 -0800169 if (!cbfs_load("rt8168-macaddress", macstrbuf, MACLEN)) {
170 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
171 return CB_ERR;
Shelley Chen0528b612017-06-12 18:29:24 -0700172 }
Julius Werner77639e42021-02-05 16:51:25 -0800173 return CB_SUCCESS;
Shelley Chen0528b612017-06-12 18:29:24 -0700174}
175
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100176static void get_mac_address(u8 *macaddr, const u8 *strbuf)
177{
178 size_t offset = 0;
179 int i;
180
Shelley Chen0528b612017-06-12 18:29:24 -0700181 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
182 (strbuf[8] != ':') || (strbuf[11] != ':') ||
183 (strbuf[14] != ':')) {
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100184 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
185 return;
186 }
187
188 for (i = 0; i < 6; i++) {
189 macaddr[i] = 0;
190 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
191 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
192 offset += 3;
193 }
194}
195
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100196static void program_mac_address(struct device *dev, u16 io_base)
197{
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100198 u8 macstrbuf[MACLEN] = { 0 };
199 int i = 0;
Shelley Chen0528b612017-06-12 18:29:24 -0700200 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100201 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
Gaggery Tsai1f847042017-12-26 17:13:52 +0800202 struct drivers_net_config *config = dev->chip_info;
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100203
Shelley Chen0528b612017-06-12 18:29:24 -0700204 /* check the VPD for the mac address */
Julius Wernercd49cce2019-03-05 16:53:33 -0800205 if (CONFIG(RT8168_GET_MAC_FROM_VPD)) {
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100206 fetch_mac_string_vpd(config, macstrbuf);
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100207 } else {
Shelley Chen0528b612017-06-12 18:29:24 -0700208 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS)
209 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS,"
Gaggery Tsai1f847042017-12-26 17:13:52 +0800210 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100211 }
Shelley Chen0528b612017-06-12 18:29:24 -0700212 get_mac_address(mac, macstrbuf);
Damien Zammite983f0c2016-05-21 02:24:19 +1000213
214 /* Reset NIC */
215 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100216 outb(CMD_REG_RESET, io_base + CMD_REG);
Damien Zammite983f0c2016-05-21 02:24:19 +1000217
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100218 /* Poll for reset, with 1sec timeout */
219 while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) {
Damien Zammite983f0c2016-05-21 02:24:19 +1000220 udelay(1000);
221 if (++i >= NIC_TIMEOUT)
Shelley Chen0528b612017-06-12 18:29:24 -0700222 printk(BIOS_ERR, "timeout waiting for nic to reset\n");
Damien Zammite983f0c2016-05-21 02:24:19 +1000223 }
224 if (i < NIC_TIMEOUT)
225 printk(BIOS_DEBUG, "done\n");
226
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100227 printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
Damien Zammite983f0c2016-05-21 02:24:19 +1000228
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100229 /* Disable register protection */
230 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
Damien Zammite983f0c2016-05-21 02:24:19 +1000231
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100232 /* Set MAC address: only 4-byte write accesses allowed */
233 outl(mac[4] | mac[5] << 8, io_base + 4);
234 inl(io_base + 4);
235 outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
236 io_base);
237 inl(io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000238 /* Lock config regs */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100239 outb(CFG_9346_LOCK, io_base + CFG_9346);
240
241 printk(BIOS_DEBUG, "done\n");
242}
243
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800244static void r8168_set_customized_led(struct device *dev, u16 io_base)
245{
246 struct drivers_net_config *config = dev->chip_info;
247
248 if (!config)
249 return;
250
251 /* Read the customized LED setting from devicetree */
252 printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds);
253
254 /*
255 * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration
256 * Starting from offset 0x18
257 * Bit[15:12] LED Feature Control(FC)
258 * Bit[11:08] LED Select for PINLED2
259 * Bit[07:04] LED Select for PINLED1
260 * Bit[03:00] LED Select for PINLED0
261 *
262 * Speed Link10M Link100M Link1000M ACT/Full
263 * LED0 Bit0 Bit1 Bit2 Bit3
264 * LED1 Bit4 Bit5 Bit6 Bit7
265 * LED2 Bit8 Bit9 Bit10 Bit11
266 * FC Bit12 Bit13 Bit14 Bit15
267 */
268
269 /* Set customized LED registers */
270 outw(config->customized_leds, io_base + CMD_LED0_LED1);
271 printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n",
272 inw(io_base + CMD_LED0_LED1));
273}
274
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100275static void r8168_init(struct device *dev)
276{
277 /* Get the resource of the NIC mmio */
278 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
279 u16 io_base = (u16)nic_res->base;
280
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800281 /* Check if the base is invalid */
282 if (!io_base) {
Martin Roth0949e732021-10-01 14:28:22 -0600283 printk(BIOS_ERR, "r8168: Error can't find IO resource\n");
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800284 return;
285 }
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100286 /* Enable but do not set bus master */
287 pci_write_config16(dev, PCI_COMMAND,
288 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
289
290 /* Program MAC address based on CBFS "macaddress" containing
291 * a string AA:BB:CC:DD:EE:FF */
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800292 program_mac_address(dev, io_base);
293
294 /* Program customized LED mode */
Julius Wernercd49cce2019-03-05 16:53:33 -0800295 if (CONFIG(RT8168_SET_LED_MODE))
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800296 r8168_set_customized_led(dev, io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000297}
298
Julius Wernercd49cce2019-03-05 16:53:33 -0800299#if CONFIG(HAVE_ACPI_TABLES)
Gaggery Tsai6919a932017-11-16 17:21:23 +0800300#define R8168_ACPI_HID "R8168"
Furquan Shaikh7536a392020-04-24 21:59:21 -0700301static void r8168_net_fill_ssdt(const struct device *dev)
Gaggery Tsai6919a932017-11-16 17:21:23 +0800302{
303 struct drivers_net_config *config = dev->chip_info;
304 const char *path = acpi_device_path(dev->bus->dev);
305 u32 address;
306
307 if (!path || !config)
308 return;
309
310 /* Device */
311 acpigen_write_scope(path);
312 acpigen_write_device(acpi_device_name(dev));
313 acpigen_write_name_string("_HID", R8168_ACPI_HID);
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100314 acpi_device_write_uid(dev);
315
Gaggery Tsai6919a932017-11-16 17:21:23 +0800316 if (dev->chip_ops)
317 acpigen_write_name_string("_DDN", dev->chip_ops->name);
318
Edward O'Callaghanb765fa62020-01-21 21:01:32 +1100319 /* Power Resource */
320 if (config->has_power_resource) {
321 const struct acpi_power_res_params power_res_params = {
322 .stop_gpio = &config->stop_gpio,
323 .stop_delay_ms = config->stop_delay_ms,
324 .stop_off_delay_ms = config->stop_off_delay_ms
325 };
326 acpi_device_add_power_res(&power_res_params);
327 }
328
Gaggery Tsai6919a932017-11-16 17:21:23 +0800329 /* Address */
330 address = PCI_SLOT(dev->path.pci.devfn) & 0xffff;
331 address <<= 16;
332 address |= PCI_FUNC(dev->path.pci.devfn) & 0xffff;
333 acpigen_write_name_dword("_ADR", address);
334
335 /* Wake capabilities */
336 if (config->wake)
337 acpigen_write_PRW(config->wake, 3);
338
339 acpigen_pop_len(); /* Device */
340 acpigen_pop_len(); /* Scope */
341
342 printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
343 dev->chip_ops ? dev->chip_ops->name : "", dev_path(dev));
344}
345
346static const char *r8168_net_acpi_name(const struct device *dev)
347{
348 return "RLTK";
349}
350#endif
351
Damien Zammite983f0c2016-05-21 02:24:19 +1000352static struct device_operations r8168_ops = {
Martin Rothb9810a42017-07-23 20:00:04 -0600353 .read_resources = pci_dev_read_resources,
354 .set_resources = pci_dev_set_resources,
355 .enable_resources = pci_dev_enable_resources,
356 .init = r8168_init,
Julius Wernercd49cce2019-03-05 16:53:33 -0800357#if CONFIG(HAVE_ACPI_TABLES)
Nico Huber68680dd2020-03-31 17:34:52 +0200358 .acpi_name = r8168_net_acpi_name,
359 .acpi_fill_ssdt = r8168_net_fill_ssdt,
Gaggery Tsai6919a932017-11-16 17:21:23 +0800360#endif
Damien Zammite983f0c2016-05-21 02:24:19 +1000361};
362
363static const struct pci_driver r8168_driver __pci_driver = {
Martin Rothb9810a42017-07-23 20:00:04 -0600364 .ops = &r8168_ops,
365 .vendor = 0x10ec,
366 .device = 0x8168,
Damien Zammite983f0c2016-05-21 02:24:19 +1000367};
Gaggery Tsai6919a932017-11-16 17:21:23 +0800368
369struct chip_operations drivers_net_ops = {
370 CHIP_NAME("Realtek r8168")
371};