blob: 1bca8799aad8351cb28932f35a073df22cd98759 [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
Gaggery Tsai1f847042017-12-26 17:13:52 +080095static enum cb_err fetch_mac_string_vpd(u8 *macstrbuf, const u8 device_index)
Shelley Chen0528b612017-06-12 18:29:24 -070096{
97 struct region_device rdev;
98 void *search_address;
99 size_t search_length;
100 size_t offset;
Gaggery Tsai1f847042017-12-26 17:13:52 +0800101 char key[] = "ethernet_mac "; /* Leave a space at tail to stuff an index */
102
103 /*
104 * The device_index 0 is treated as an special case matching to
105 * "ethernet_mac" with single NIC on DUT. When there are mulitple
106 * NICs on DUT, they are mapping to "ethernet_macN", where
107 * N is [0-9].
108 */
109 if (device_index == 0)
110 key[DEVICE_INDEX_BYTE] = '\0';
111 else
112 /* Translate index number from integer to ascii */
113 key[DEVICE_INDEX_BYTE] = (device_index - 1) + '0';
Shelley Chen0528b612017-06-12 18:29:24 -0700114
115 if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) {
116 printk(BIOS_ERR, "Error: Couldn't find RO_VPD region.");
117 return CB_ERR;
118 }
119 search_address = rdev_mmap_full(&rdev);
120 if (search_address == NULL) {
121 printk(BIOS_ERR, "LAN: VPD not found.\n");
122 return CB_ERR;
123 }
124
125 search_length = region_device_sz(&rdev);
126 offset = search(key, search_address, strlen(key),
127 search_length);
Gaggery Tsai1f847042017-12-26 17:13:52 +0800128
Shelley Chen0528b612017-06-12 18:29:24 -0700129 if (offset == search_length) {
130 printk(BIOS_ERR,
131 "Error: Could not locate '%s' in VPD\n", key);
132 return CB_ERR;
133 }
134 printk(BIOS_DEBUG, "Located '%s' in VPD\n", key);
135
Gaggery Tsai1f847042017-12-26 17:13:52 +0800136 offset += strlen(key) + 1; /* move to next character */
Shelley Chen0528b612017-06-12 18:29:24 -0700137
138 if (offset + MACLEN > search_length) {
139 printk(BIOS_ERR, "Search result too small!\n");
140 return CB_ERR;
141 }
142 memcpy(macstrbuf, search_address + offset, MACLEN);
Shelley Chen0528b612017-06-12 18:29:24 -0700143 return CB_SUCCESS;
144}
145
146static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
147{
148 struct cbfsf fh;
149 uint32_t matchraw = CBFS_TYPE_RAW;
150
151 if (!cbfs_boot_locate(&fh, "rt8168-macaddress", &matchraw)) {
152 /* check the cbfs for the mac address */
153 if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
154 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
155 return CB_ERR;
156 }
157 return CB_SUCCESS;
158 }
159 return CB_ERR;
160}
161
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100162static void get_mac_address(u8 *macaddr, const u8 *strbuf)
163{
164 size_t offset = 0;
165 int i;
166
Shelley Chen0528b612017-06-12 18:29:24 -0700167 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
168 (strbuf[8] != ':') || (strbuf[11] != ':') ||
169 (strbuf[14] != ':')) {
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100170 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
171 return;
172 }
173
174 for (i = 0; i < 6; i++) {
175 macaddr[i] = 0;
176 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
177 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
178 offset += 3;
179 }
180}
181
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100182static void program_mac_address(struct device *dev, u16 io_base)
183{
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100184 u8 macstrbuf[MACLEN] = { 0 };
185 int i = 0;
Shelley Chen0528b612017-06-12 18:29:24 -0700186 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100187 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
Gaggery Tsai1f847042017-12-26 17:13:52 +0800188 struct drivers_net_config *config = dev->chip_info;
David Wu9a0d9e02018-05-04 15:37:37 +0800189 bool mac_found = false;
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100190
Shelley Chen0528b612017-06-12 18:29:24 -0700191 /* check the VPD for the mac address */
Julius Wernercd49cce2019-03-05 16:53:33 -0800192 if (CONFIG(RT8168_GET_MAC_FROM_VPD)) {
Gaggery Tsai1f847042017-12-26 17:13:52 +0800193 /* Current implementation is up to 10 NIC cards */
194 if (config && config->device_index <= MAX_DEVICE_SUPPORT) {
David Wu9a0d9e02018-05-04 15:37:37 +0800195 /* check "ethernet_mac" first when the device index is 1 */
196 if (config->device_index == 1 &&
197 fetch_mac_string_vpd(macstrbuf, 0) == CB_SUCCESS)
198 mac_found = true;
199 if (!mac_found && fetch_mac_string_vpd(macstrbuf,
200 config->device_index) != CB_SUCCESS)
Gaggery Tsai1f847042017-12-26 17:13:52 +0800201 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
202 " using default 00:e0:4c:00:c0:b0\n");
203 } else {
204 printk(BIOS_ERR, "r8168: the maximum device_index should be"
205 " less then %d\n. Using default 00:e0:4c:00:c0:b0\n",
206 MAX_DEVICE_SUPPORT);
207 }
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) {
284 printk(BIOS_ERR, "r8168: Error cant find IO resource\n");
285 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"
302static void r8168_net_fill_ssdt(struct device *dev)
303{
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
320 /* Address */
321 address = PCI_SLOT(dev->path.pci.devfn) & 0xffff;
322 address <<= 16;
323 address |= PCI_FUNC(dev->path.pci.devfn) & 0xffff;
324 acpigen_write_name_dword("_ADR", address);
325
326 /* Wake capabilities */
327 if (config->wake)
328 acpigen_write_PRW(config->wake, 3);
329
330 acpigen_pop_len(); /* Device */
331 acpigen_pop_len(); /* Scope */
332
333 printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
334 dev->chip_ops ? dev->chip_ops->name : "", dev_path(dev));
335}
336
337static const char *r8168_net_acpi_name(const struct device *dev)
338{
339 return "RLTK";
340}
341#endif
342
Damien Zammite983f0c2016-05-21 02:24:19 +1000343static struct device_operations r8168_ops = {
Martin Rothb9810a42017-07-23 20:00:04 -0600344 .read_resources = pci_dev_read_resources,
345 .set_resources = pci_dev_set_resources,
346 .enable_resources = pci_dev_enable_resources,
347 .init = r8168_init,
348 .scan_bus = 0,
Julius Wernercd49cce2019-03-05 16:53:33 -0800349#if CONFIG(HAVE_ACPI_TABLES)
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100350 .acpi_name = r8168_net_acpi_name,
351 .acpi_fill_ssdt_generator = r8168_net_fill_ssdt,
Gaggery Tsai6919a932017-11-16 17:21:23 +0800352#endif
Damien Zammite983f0c2016-05-21 02:24:19 +1000353};
354
355static const struct pci_driver r8168_driver __pci_driver = {
Martin Rothb9810a42017-07-23 20:00:04 -0600356 .ops = &r8168_ops,
357 .vendor = 0x10ec,
358 .device = 0x8168,
Damien Zammite983f0c2016-05-21 02:24:19 +1000359};
Gaggery Tsai6919a932017-11-16 17:21:23 +0800360
361struct chip_operations drivers_net_ops = {
362 CHIP_NAME("Realtek r8168")
363};