blob: 92cb9b54f2159e1597a92b324141b0f0d0709828 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Damien Zammite983f0c2016-05-21 02:24:19 +10003
4/*
Damien Zammitf5dd23f2016-12-01 23:29:34 +11005 * This driver resets the 10ec:8168 NIC then tries to read
6 * "macaddress" string XX:XX:XX:XX:XX:XX from CBFS.
7 * If no MAC is found, it programs a default MAC address in the device
Damien Zammite983f0c2016-05-21 02:24:19 +10008 * so that if the EEPROM/efuse is unconfigured it still has a default MAC.
9 */
10
Damien Zammitf5dd23f2016-12-01 23:29:34 +110011#include <cbfs.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070012#include <acpi/acpi_device.h>
13#include <acpi/acpigen.h>
Damien Zammitf5dd23f2016-12-01 23:29:34 +110014#include <string.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100015#include <arch/io.h>
Shelley Chen0528b612017-06-12 18:29:24 -070016#include <console/console.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100017#include <device/device.h>
18#include <device/pci.h>
19#include <device/pci_ops.h>
20#include <device/pci_def.h>
21#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{
170 struct cbfsf fh;
171 uint32_t matchraw = CBFS_TYPE_RAW;
172
173 if (!cbfs_boot_locate(&fh, "rt8168-macaddress", &matchraw)) {
174 /* check the cbfs for the mac address */
175 if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
176 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
177 return CB_ERR;
178 }
179 return CB_SUCCESS;
180 }
181 return CB_ERR;
182}
183
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100184static void get_mac_address(u8 *macaddr, const u8 *strbuf)
185{
186 size_t offset = 0;
187 int i;
188
Shelley Chen0528b612017-06-12 18:29:24 -0700189 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
190 (strbuf[8] != ':') || (strbuf[11] != ':') ||
191 (strbuf[14] != ':')) {
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100192 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
193 return;
194 }
195
196 for (i = 0; i < 6; i++) {
197 macaddr[i] = 0;
198 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
199 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
200 offset += 3;
201 }
202}
203
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100204static void program_mac_address(struct device *dev, u16 io_base)
205{
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100206 u8 macstrbuf[MACLEN] = { 0 };
207 int i = 0;
Shelley Chen0528b612017-06-12 18:29:24 -0700208 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100209 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
Gaggery Tsai1f847042017-12-26 17:13:52 +0800210 struct drivers_net_config *config = dev->chip_info;
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100211
Shelley Chen0528b612017-06-12 18:29:24 -0700212 /* check the VPD for the mac address */
Julius Wernercd49cce2019-03-05 16:53:33 -0800213 if (CONFIG(RT8168_GET_MAC_FROM_VPD)) {
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100214 fetch_mac_string_vpd(config, macstrbuf);
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100215 } else {
Shelley Chen0528b612017-06-12 18:29:24 -0700216 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS)
217 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS,"
Gaggery Tsai1f847042017-12-26 17:13:52 +0800218 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100219 }
Shelley Chen0528b612017-06-12 18:29:24 -0700220 get_mac_address(mac, macstrbuf);
Damien Zammite983f0c2016-05-21 02:24:19 +1000221
222 /* Reset NIC */
223 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100224 outb(CMD_REG_RESET, io_base + CMD_REG);
Damien Zammite983f0c2016-05-21 02:24:19 +1000225
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100226 /* Poll for reset, with 1sec timeout */
227 while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) {
Damien Zammite983f0c2016-05-21 02:24:19 +1000228 udelay(1000);
229 if (++i >= NIC_TIMEOUT)
Shelley Chen0528b612017-06-12 18:29:24 -0700230 printk(BIOS_ERR, "timeout waiting for nic to reset\n");
Damien Zammite983f0c2016-05-21 02:24:19 +1000231 }
232 if (i < NIC_TIMEOUT)
233 printk(BIOS_DEBUG, "done\n");
234
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100235 printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
Damien Zammite983f0c2016-05-21 02:24:19 +1000236
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100237 /* Disable register protection */
238 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
Damien Zammite983f0c2016-05-21 02:24:19 +1000239
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100240 /* Set MAC address: only 4-byte write accesses allowed */
241 outl(mac[4] | mac[5] << 8, io_base + 4);
242 inl(io_base + 4);
243 outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
244 io_base);
245 inl(io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000246 /* Lock config regs */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100247 outb(CFG_9346_LOCK, io_base + CFG_9346);
248
249 printk(BIOS_DEBUG, "done\n");
250}
251
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800252static void r8168_set_customized_led(struct device *dev, u16 io_base)
253{
254 struct drivers_net_config *config = dev->chip_info;
255
256 if (!config)
257 return;
258
259 /* Read the customized LED setting from devicetree */
260 printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds);
261
262 /*
263 * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration
264 * Starting from offset 0x18
265 * Bit[15:12] LED Feature Control(FC)
266 * Bit[11:08] LED Select for PINLED2
267 * Bit[07:04] LED Select for PINLED1
268 * Bit[03:00] LED Select for PINLED0
269 *
270 * Speed Link10M Link100M Link1000M ACT/Full
271 * LED0 Bit0 Bit1 Bit2 Bit3
272 * LED1 Bit4 Bit5 Bit6 Bit7
273 * LED2 Bit8 Bit9 Bit10 Bit11
274 * FC Bit12 Bit13 Bit14 Bit15
275 */
276
277 /* Set customized LED registers */
278 outw(config->customized_leds, io_base + CMD_LED0_LED1);
279 printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n",
280 inw(io_base + CMD_LED0_LED1));
281}
282
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100283static void r8168_init(struct device *dev)
284{
285 /* Get the resource of the NIC mmio */
286 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
287 u16 io_base = (u16)nic_res->base;
288
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800289 /* Check if the base is invalid */
290 if (!io_base) {
291 printk(BIOS_ERR, "r8168: Error cant find IO resource\n");
292 return;
293 }
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100294 /* Enable but do not set bus master */
295 pci_write_config16(dev, PCI_COMMAND,
296 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
297
298 /* Program MAC address based on CBFS "macaddress" containing
299 * a string AA:BB:CC:DD:EE:FF */
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800300 program_mac_address(dev, io_base);
301
302 /* Program customized LED mode */
Julius Wernercd49cce2019-03-05 16:53:33 -0800303 if (CONFIG(RT8168_SET_LED_MODE))
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800304 r8168_set_customized_led(dev, io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000305}
306
Julius Wernercd49cce2019-03-05 16:53:33 -0800307#if CONFIG(HAVE_ACPI_TABLES)
Gaggery Tsai6919a932017-11-16 17:21:23 +0800308#define R8168_ACPI_HID "R8168"
Furquan Shaikh7536a392020-04-24 21:59:21 -0700309static void r8168_net_fill_ssdt(const struct device *dev)
Gaggery Tsai6919a932017-11-16 17:21:23 +0800310{
311 struct drivers_net_config *config = dev->chip_info;
312 const char *path = acpi_device_path(dev->bus->dev);
313 u32 address;
314
315 if (!path || !config)
316 return;
317
318 /* Device */
319 acpigen_write_scope(path);
320 acpigen_write_device(acpi_device_name(dev));
321 acpigen_write_name_string("_HID", R8168_ACPI_HID);
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100322 acpi_device_write_uid(dev);
323
Gaggery Tsai6919a932017-11-16 17:21:23 +0800324 if (dev->chip_ops)
325 acpigen_write_name_string("_DDN", dev->chip_ops->name);
326
Edward O'Callaghanb765fa62020-01-21 21:01:32 +1100327 /* Power Resource */
328 if (config->has_power_resource) {
329 const struct acpi_power_res_params power_res_params = {
330 .stop_gpio = &config->stop_gpio,
331 .stop_delay_ms = config->stop_delay_ms,
332 .stop_off_delay_ms = config->stop_off_delay_ms
333 };
334 acpi_device_add_power_res(&power_res_params);
335 }
336
Gaggery Tsai6919a932017-11-16 17:21:23 +0800337 /* Address */
338 address = PCI_SLOT(dev->path.pci.devfn) & 0xffff;
339 address <<= 16;
340 address |= PCI_FUNC(dev->path.pci.devfn) & 0xffff;
341 acpigen_write_name_dword("_ADR", address);
342
343 /* Wake capabilities */
344 if (config->wake)
345 acpigen_write_PRW(config->wake, 3);
346
347 acpigen_pop_len(); /* Device */
348 acpigen_pop_len(); /* Scope */
349
350 printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
351 dev->chip_ops ? dev->chip_ops->name : "", dev_path(dev));
352}
353
354static const char *r8168_net_acpi_name(const struct device *dev)
355{
356 return "RLTK";
357}
358#endif
359
Damien Zammite983f0c2016-05-21 02:24:19 +1000360static struct device_operations r8168_ops = {
Martin Rothb9810a42017-07-23 20:00:04 -0600361 .read_resources = pci_dev_read_resources,
362 .set_resources = pci_dev_set_resources,
363 .enable_resources = pci_dev_enable_resources,
364 .init = r8168_init,
Julius Wernercd49cce2019-03-05 16:53:33 -0800365#if CONFIG(HAVE_ACPI_TABLES)
Nico Huber68680dd2020-03-31 17:34:52 +0200366 .acpi_name = r8168_net_acpi_name,
367 .acpi_fill_ssdt = r8168_net_fill_ssdt,
Gaggery Tsai6919a932017-11-16 17:21:23 +0800368#endif
Damien Zammite983f0c2016-05-21 02:24:19 +1000369};
370
371static const struct pci_driver r8168_driver __pci_driver = {
Martin Rothb9810a42017-07-23 20:00:04 -0600372 .ops = &r8168_ops,
373 .vendor = 0x10ec,
374 .device = 0x8168,
Damien Zammite983f0c2016-05-21 02:24:19 +1000375};
Gaggery Tsai6919a932017-11-16 17:21:23 +0800376
377struct chip_operations drivers_net_ops = {
378 CHIP_NAME("Realtek r8168")
379};