blob: c764b433b62a2224187936687cb196339072a619 [file] [log] [blame]
Damien Zammite983f0c2016-05-21 02:24:19 +10001/*
2 * This file is part of the coreboot project.
3 *
Damien Zammite983f0c2016-05-21 02:24:19 +10004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14/*
Damien Zammitf5dd23f2016-12-01 23:29:34 +110015 * This driver resets the 10ec:8168 NIC then tries to read
16 * "macaddress" string XX:XX:XX:XX:XX:XX from CBFS.
17 * If no MAC is found, it programs a default MAC address in the device
Damien Zammite983f0c2016-05-21 02:24:19 +100018 * so that if the EEPROM/efuse is unconfigured it still has a default MAC.
19 */
20
Damien Zammitf5dd23f2016-12-01 23:29:34 +110021#include <cbfs.h>
Gaggery Tsai6919a932017-11-16 17:21:23 +080022#include <arch/acpi_device.h>
23#include <arch/acpigen.h>
Damien Zammitf5dd23f2016-12-01 23:29:34 +110024#include <string.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100025#include <arch/io.h>
Shelley Chen0528b612017-06-12 18:29:24 -070026#include <console/console.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100027#include <device/device.h>
28#include <device/pci.h>
29#include <device/pci_ops.h>
30#include <device/pci_def.h>
31#include <delay.h>
Shelley Chen0528b612017-06-12 18:29:24 -070032#include <fmap.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020033#include <types.h>
34
Gaggery Tsai65623ef2017-09-29 11:15:23 +080035#include "chip.h"
Damien Zammite983f0c2016-05-21 02:24:19 +100036
37#define NIC_TIMEOUT 1000
38
39#define CMD_REG 0x37
40#define CMD_REG_RESET 0x10
Gaggery Tsai65623ef2017-09-29 11:15:23 +080041#define CMD_LED0_LED1 0x18
Damien Zammite983f0c2016-05-21 02:24:19 +100042
43#define CFG_9346 0x50
44#define CFG_9346_LOCK 0x00
45#define CFG_9346_UNLOCK 0xc0
46
Gaggery Tsai1f847042017-12-26 17:13:52 +080047#define DEVICE_INDEX_BYTE 12
48#define MAX_DEVICE_SUPPORT 10
Shelley Chen0528b612017-06-12 18:29:24 -070049/**
50 * search: Find first instance of string in a given region
51 * @param p string to find
52 * @param a start address of region to search
53 * @param lengthp length of string to search for
54 * @param lengtha length of region to search in
55 * @return offset offset from start address a where string was found.
56 * If string not found, return lengtha.
57 */
58static size_t search(const char *p, const u8 *a, size_t lengthp,
59 size_t lengtha)
60{
61 size_t i, j;
62
63 if (lengtha < lengthp)
64 return lengtha;
65 /* Searching */
66 for (j = 0; j <= lengtha - lengthp; j++) {
67 for (i = 0; i < lengthp && p[i] == a[i + j]; i++)
68 ;
David Wu9a0d9e02018-05-04 15:37:37 +080069 if (i >= lengthp && a[j - 1] == lengthp)
Shelley Chen0528b612017-06-12 18:29:24 -070070 return j;
71 }
72 return lengtha;
73}
74
Damien Zammitf5dd23f2016-12-01 23:29:34 +110075static u8 get_hex_digit(const u8 c)
Damien Zammite983f0c2016-05-21 02:24:19 +100076{
Damien Zammitf5dd23f2016-12-01 23:29:34 +110077 u8 ret = 0;
Damien Zammite983f0c2016-05-21 02:24:19 +100078
Damien Zammitf5dd23f2016-12-01 23:29:34 +110079 ret = c - '0';
80 if (ret > 0x09) {
81 ret = c - 'A' + 0x0a;
82 if (ret > 0x0f)
83 ret = c - 'a' + 0x0a;
84 }
85 if (ret > 0x0f) {
Shelley Chen0528b612017-06-12 18:29:24 -070086 printk(BIOS_ERR, "Error: Invalid hex digit found: "
87 "%c - 0x%02x\n", (char)c, c);
Damien Zammitf5dd23f2016-12-01 23:29:34 +110088 ret = 0;
89 }
90 return ret;
91}
Damien Zammite983f0c2016-05-21 02:24:19 +100092
Shelley Chen0528b612017-06-12 18:29:24 -070093#define MACLEN 17
94
Edward O'Callaghan2a82f742020-03-25 12:52:50 +110095/* Returns MAC address based on the key that is passed in. */
96static enum cb_err fetch_mac_vpd_key(u8 *macstrbuf, const char *vpd_key)
Shelley Chen0528b612017-06-12 18:29:24 -070097{
98 struct region_device rdev;
99 void *search_address;
100 size_t search_length;
101 size_t offset;
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100102
103 if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) {
104 printk(BIOS_ERR, "Error: Couldn't find RO_VPD region.");
105 return CB_ERR;
106 }
107 search_address = rdev_mmap_full(&rdev);
108 if (search_address == NULL) {
109 printk(BIOS_ERR, "LAN: VPD not found.\n");
110 return CB_ERR;
111 }
112
113 search_length = region_device_sz(&rdev);
114 offset = search(vpd_key, search_address, strlen(vpd_key),
115 search_length);
116
117 if (offset == search_length) {
118 printk(BIOS_ERR,
119 "Error: Could not locate '%s' in VPD\n", vpd_key);
Edward O'Callaghand08bc5a2020-03-26 14:37:37 +1100120 rdev_munmap(&rdev, search_address);
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100121 return CB_ERR;
122 }
123 printk(BIOS_DEBUG, "Located '%s' in VPD\n", vpd_key);
124
125 offset += strlen(vpd_key) + 1; /* move to next character */
126
127 if (offset + MACLEN > search_length) {
Edward O'Callaghand08bc5a2020-03-26 14:37:37 +1100128 rdev_munmap(&rdev, search_address);
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100129 printk(BIOS_ERR, "Search result too small!\n");
130 return CB_ERR;
131 }
132 memcpy(macstrbuf, search_address + offset, MACLEN);
Edward O'Callaghand08bc5a2020-03-26 14:37:37 +1100133 rdev_munmap(&rdev, search_address);
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100134
135 return CB_SUCCESS;
136}
137
138/* Prepares vpd_key by concatenating ethernet_mac with device_index */
139static enum cb_err fetch_mac_vpd_dev_idx(u8 *macstrbuf, u8 device_index)
140{
Gaggery Tsai1f847042017-12-26 17:13:52 +0800141 char key[] = "ethernet_mac "; /* Leave a space at tail to stuff an index */
142
143 /*
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100144 * Map each NIC on the DUT to "ethernet_macN", where N is [0-9].
145 * Translate index number from integer to ascii by adding '0' char.
Gaggery Tsai1f847042017-12-26 17:13:52 +0800146 */
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100147 key[DEVICE_INDEX_BYTE] = device_index + '0';
Shelley Chen0528b612017-06-12 18:29:24 -0700148
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100149 return fetch_mac_vpd_key(macstrbuf, key);
150}
151
152static void fetch_mac_string_vpd(struct drivers_net_config *config, u8 *macstrbuf)
153{
154 if (!config)
155 return;
156
157 /* Current implementation is up to 10 NIC cards */
158 if (config->device_index > MAX_DEVICE_SUPPORT) {
159 printk(BIOS_ERR, "r8168: the maximum device_index should be less then %d\n."
160 " Using default 00:e0:4c:00:c0:b0\n", MAX_DEVICE_SUPPORT);
161 return;
Shelley Chen0528b612017-06-12 18:29:24 -0700162 }
163
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100164 if (fetch_mac_vpd_dev_idx(macstrbuf, config->device_index) == CB_SUCCESS)
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100165 return;
Gaggery Tsai1f847042017-12-26 17:13:52 +0800166
Edward O'Callaghan0e138062020-03-23 13:06:42 +1100167 if (!CONFIG(RT8168_SUPPORT_LEGACY_VPD_MAC)) {
168 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
169 " using default 00:e0:4c:00:c0:b0\n");
170 return;
171 }
172
Edward O'Callaghan2a0a02f2020-03-26 16:47:55 +1100173 if (fetch_mac_vpd_key(macstrbuf, "ethernet_mac") != CB_SUCCESS)
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100174 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
175 " using default 00:e0:4c:00:c0:b0\n");
Shelley Chen0528b612017-06-12 18:29:24 -0700176}
177
178static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
179{
180 struct cbfsf fh;
181 uint32_t matchraw = CBFS_TYPE_RAW;
182
183 if (!cbfs_boot_locate(&fh, "rt8168-macaddress", &matchraw)) {
184 /* check the cbfs for the mac address */
185 if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
186 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
187 return CB_ERR;
188 }
189 return CB_SUCCESS;
190 }
191 return CB_ERR;
192}
193
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100194static void get_mac_address(u8 *macaddr, const u8 *strbuf)
195{
196 size_t offset = 0;
197 int i;
198
Shelley Chen0528b612017-06-12 18:29:24 -0700199 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
200 (strbuf[8] != ':') || (strbuf[11] != ':') ||
201 (strbuf[14] != ':')) {
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100202 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
203 return;
204 }
205
206 for (i = 0; i < 6; i++) {
207 macaddr[i] = 0;
208 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
209 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
210 offset += 3;
211 }
212}
213
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100214static void program_mac_address(struct device *dev, u16 io_base)
215{
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100216 u8 macstrbuf[MACLEN] = { 0 };
217 int i = 0;
Shelley Chen0528b612017-06-12 18:29:24 -0700218 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100219 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
Gaggery Tsai1f847042017-12-26 17:13:52 +0800220 struct drivers_net_config *config = dev->chip_info;
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100221
Shelley Chen0528b612017-06-12 18:29:24 -0700222 /* check the VPD for the mac address */
Julius Wernercd49cce2019-03-05 16:53:33 -0800223 if (CONFIG(RT8168_GET_MAC_FROM_VPD)) {
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100224 fetch_mac_string_vpd(config, macstrbuf);
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100225 } else {
Shelley Chen0528b612017-06-12 18:29:24 -0700226 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS)
227 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS,"
Gaggery Tsai1f847042017-12-26 17:13:52 +0800228 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100229 }
Shelley Chen0528b612017-06-12 18:29:24 -0700230 get_mac_address(mac, macstrbuf);
Damien Zammite983f0c2016-05-21 02:24:19 +1000231
232 /* Reset NIC */
233 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100234 outb(CMD_REG_RESET, io_base + CMD_REG);
Damien Zammite983f0c2016-05-21 02:24:19 +1000235
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100236 /* Poll for reset, with 1sec timeout */
237 while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) {
Damien Zammite983f0c2016-05-21 02:24:19 +1000238 udelay(1000);
239 if (++i >= NIC_TIMEOUT)
Shelley Chen0528b612017-06-12 18:29:24 -0700240 printk(BIOS_ERR, "timeout waiting for nic to reset\n");
Damien Zammite983f0c2016-05-21 02:24:19 +1000241 }
242 if (i < NIC_TIMEOUT)
243 printk(BIOS_DEBUG, "done\n");
244
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100245 printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
Damien Zammite983f0c2016-05-21 02:24:19 +1000246
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100247 /* Disable register protection */
248 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
Damien Zammite983f0c2016-05-21 02:24:19 +1000249
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100250 /* Set MAC address: only 4-byte write accesses allowed */
251 outl(mac[4] | mac[5] << 8, io_base + 4);
252 inl(io_base + 4);
253 outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
254 io_base);
255 inl(io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000256 /* Lock config regs */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100257 outb(CFG_9346_LOCK, io_base + CFG_9346);
258
259 printk(BIOS_DEBUG, "done\n");
260}
261
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800262static void r8168_set_customized_led(struct device *dev, u16 io_base)
263{
264 struct drivers_net_config *config = dev->chip_info;
265
266 if (!config)
267 return;
268
269 /* Read the customized LED setting from devicetree */
270 printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds);
271
272 /*
273 * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration
274 * Starting from offset 0x18
275 * Bit[15:12] LED Feature Control(FC)
276 * Bit[11:08] LED Select for PINLED2
277 * Bit[07:04] LED Select for PINLED1
278 * Bit[03:00] LED Select for PINLED0
279 *
280 * Speed Link10M Link100M Link1000M ACT/Full
281 * LED0 Bit0 Bit1 Bit2 Bit3
282 * LED1 Bit4 Bit5 Bit6 Bit7
283 * LED2 Bit8 Bit9 Bit10 Bit11
284 * FC Bit12 Bit13 Bit14 Bit15
285 */
286
287 /* Set customized LED registers */
288 outw(config->customized_leds, io_base + CMD_LED0_LED1);
289 printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n",
290 inw(io_base + CMD_LED0_LED1));
291}
292
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100293static void r8168_init(struct device *dev)
294{
295 /* Get the resource of the NIC mmio */
296 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
297 u16 io_base = (u16)nic_res->base;
298
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800299 /* Check if the base is invalid */
300 if (!io_base) {
301 printk(BIOS_ERR, "r8168: Error cant find IO resource\n");
302 return;
303 }
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100304 /* Enable but do not set bus master */
305 pci_write_config16(dev, PCI_COMMAND,
306 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
307
308 /* Program MAC address based on CBFS "macaddress" containing
309 * a string AA:BB:CC:DD:EE:FF */
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800310 program_mac_address(dev, io_base);
311
312 /* Program customized LED mode */
Julius Wernercd49cce2019-03-05 16:53:33 -0800313 if (CONFIG(RT8168_SET_LED_MODE))
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800314 r8168_set_customized_led(dev, io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000315}
316
Julius Wernercd49cce2019-03-05 16:53:33 -0800317#if CONFIG(HAVE_ACPI_TABLES)
Gaggery Tsai6919a932017-11-16 17:21:23 +0800318#define R8168_ACPI_HID "R8168"
319static void r8168_net_fill_ssdt(struct device *dev)
320{
321 struct drivers_net_config *config = dev->chip_info;
322 const char *path = acpi_device_path(dev->bus->dev);
323 u32 address;
324
325 if (!path || !config)
326 return;
327
328 /* Device */
329 acpigen_write_scope(path);
330 acpigen_write_device(acpi_device_name(dev));
331 acpigen_write_name_string("_HID", R8168_ACPI_HID);
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100332 acpi_device_write_uid(dev);
333
Gaggery Tsai6919a932017-11-16 17:21:23 +0800334 if (dev->chip_ops)
335 acpigen_write_name_string("_DDN", dev->chip_ops->name);
336
Edward O'Callaghanb765fa62020-01-21 21:01:32 +1100337 /* Power Resource */
338 if (config->has_power_resource) {
339 const struct acpi_power_res_params power_res_params = {
340 .stop_gpio = &config->stop_gpio,
341 .stop_delay_ms = config->stop_delay_ms,
342 .stop_off_delay_ms = config->stop_off_delay_ms
343 };
344 acpi_device_add_power_res(&power_res_params);
345 }
346
Gaggery Tsai6919a932017-11-16 17:21:23 +0800347 /* Address */
348 address = PCI_SLOT(dev->path.pci.devfn) & 0xffff;
349 address <<= 16;
350 address |= PCI_FUNC(dev->path.pci.devfn) & 0xffff;
351 acpigen_write_name_dword("_ADR", address);
352
353 /* Wake capabilities */
354 if (config->wake)
355 acpigen_write_PRW(config->wake, 3);
356
357 acpigen_pop_len(); /* Device */
358 acpigen_pop_len(); /* Scope */
359
360 printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
361 dev->chip_ops ? dev->chip_ops->name : "", dev_path(dev));
362}
363
364static const char *r8168_net_acpi_name(const struct device *dev)
365{
366 return "RLTK";
367}
368#endif
369
Damien Zammite983f0c2016-05-21 02:24:19 +1000370static struct device_operations r8168_ops = {
Martin Rothb9810a42017-07-23 20:00:04 -0600371 .read_resources = pci_dev_read_resources,
372 .set_resources = pci_dev_set_resources,
373 .enable_resources = pci_dev_enable_resources,
374 .init = r8168_init,
375 .scan_bus = 0,
Julius Wernercd49cce2019-03-05 16:53:33 -0800376#if CONFIG(HAVE_ACPI_TABLES)
Nico Huber68680dd2020-03-31 17:34:52 +0200377 .acpi_name = r8168_net_acpi_name,
378 .acpi_fill_ssdt = r8168_net_fill_ssdt,
Gaggery Tsai6919a932017-11-16 17:21:23 +0800379#endif
Damien Zammite983f0c2016-05-21 02:24:19 +1000380};
381
382static const struct pci_driver r8168_driver __pci_driver = {
Martin Rothb9810a42017-07-23 20:00:04 -0600383 .ops = &r8168_ops,
384 .vendor = 0x10ec,
385 .device = 0x8168,
Damien Zammite983f0c2016-05-21 02:24:19 +1000386};
Gaggery Tsai6919a932017-11-16 17:21:23 +0800387
388struct chip_operations drivers_net_ops = {
389 CHIP_NAME("Realtek r8168")
390};