blob: 99d472ce8e53a1038235dcd285ef6f32f31fdf9f [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 /*
144 * The device_index 0 is treated as an special case matching to
145 * "ethernet_mac" with single NIC on DUT. When there are mulitple
146 * NICs on DUT, they are mapping to "ethernet_macN", where
147 * N is [0-9].
148 */
149 if (device_index == 0)
150 key[DEVICE_INDEX_BYTE] = '\0';
151 else
152 /* Translate index number from integer to ascii */
153 key[DEVICE_INDEX_BYTE] = (device_index - 1) + '0';
Shelley Chen0528b612017-06-12 18:29:24 -0700154
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100155 return fetch_mac_vpd_key(macstrbuf, key);
156}
157
158static void fetch_mac_string_vpd(struct drivers_net_config *config, u8 *macstrbuf)
159{
160 if (!config)
161 return;
162
163 /* Current implementation is up to 10 NIC cards */
164 if (config->device_index > MAX_DEVICE_SUPPORT) {
165 printk(BIOS_ERR, "r8168: the maximum device_index should be less then %d\n."
166 " Using default 00:e0:4c:00:c0:b0\n", MAX_DEVICE_SUPPORT);
167 return;
Shelley Chen0528b612017-06-12 18:29:24 -0700168 }
169
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100170 /* check "ethernet_mac" first when the device index is 1 */
171 if (config->device_index == 1 &&
172 fetch_mac_vpd_dev_idx(macstrbuf, 0) == CB_SUCCESS)
173 return;
Gaggery Tsai1f847042017-12-26 17:13:52 +0800174
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100175 if (fetch_mac_vpd_dev_idx(macstrbuf, config->device_index) != CB_SUCCESS)
176 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
177 " using default 00:e0:4c:00:c0:b0\n");
Shelley Chen0528b612017-06-12 18:29:24 -0700178}
179
180static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
181{
182 struct cbfsf fh;
183 uint32_t matchraw = CBFS_TYPE_RAW;
184
185 if (!cbfs_boot_locate(&fh, "rt8168-macaddress", &matchraw)) {
186 /* check the cbfs for the mac address */
187 if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
188 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
189 return CB_ERR;
190 }
191 return CB_SUCCESS;
192 }
193 return CB_ERR;
194}
195
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100196static void get_mac_address(u8 *macaddr, const u8 *strbuf)
197{
198 size_t offset = 0;
199 int i;
200
Shelley Chen0528b612017-06-12 18:29:24 -0700201 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
202 (strbuf[8] != ':') || (strbuf[11] != ':') ||
203 (strbuf[14] != ':')) {
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100204 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
205 return;
206 }
207
208 for (i = 0; i < 6; i++) {
209 macaddr[i] = 0;
210 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
211 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
212 offset += 3;
213 }
214}
215
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100216static void program_mac_address(struct device *dev, u16 io_base)
217{
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100218 u8 macstrbuf[MACLEN] = { 0 };
219 int i = 0;
Shelley Chen0528b612017-06-12 18:29:24 -0700220 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100221 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
Gaggery Tsai1f847042017-12-26 17:13:52 +0800222 struct drivers_net_config *config = dev->chip_info;
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100223
Shelley Chen0528b612017-06-12 18:29:24 -0700224 /* check the VPD for the mac address */
Julius Wernercd49cce2019-03-05 16:53:33 -0800225 if (CONFIG(RT8168_GET_MAC_FROM_VPD)) {
Edward O'Callaghan2a82f742020-03-25 12:52:50 +1100226 fetch_mac_string_vpd(config, macstrbuf);
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100227 } else {
Shelley Chen0528b612017-06-12 18:29:24 -0700228 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS)
229 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS,"
Gaggery Tsai1f847042017-12-26 17:13:52 +0800230 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100231 }
Shelley Chen0528b612017-06-12 18:29:24 -0700232 get_mac_address(mac, macstrbuf);
Damien Zammite983f0c2016-05-21 02:24:19 +1000233
234 /* Reset NIC */
235 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100236 outb(CMD_REG_RESET, io_base + CMD_REG);
Damien Zammite983f0c2016-05-21 02:24:19 +1000237
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100238 /* Poll for reset, with 1sec timeout */
239 while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) {
Damien Zammite983f0c2016-05-21 02:24:19 +1000240 udelay(1000);
241 if (++i >= NIC_TIMEOUT)
Shelley Chen0528b612017-06-12 18:29:24 -0700242 printk(BIOS_ERR, "timeout waiting for nic to reset\n");
Damien Zammite983f0c2016-05-21 02:24:19 +1000243 }
244 if (i < NIC_TIMEOUT)
245 printk(BIOS_DEBUG, "done\n");
246
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100247 printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
Damien Zammite983f0c2016-05-21 02:24:19 +1000248
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100249 /* Disable register protection */
250 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
Damien Zammite983f0c2016-05-21 02:24:19 +1000251
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100252 /* Set MAC address: only 4-byte write accesses allowed */
253 outl(mac[4] | mac[5] << 8, io_base + 4);
254 inl(io_base + 4);
255 outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
256 io_base);
257 inl(io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000258 /* Lock config regs */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100259 outb(CFG_9346_LOCK, io_base + CFG_9346);
260
261 printk(BIOS_DEBUG, "done\n");
262}
263
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800264static void r8168_set_customized_led(struct device *dev, u16 io_base)
265{
266 struct drivers_net_config *config = dev->chip_info;
267
268 if (!config)
269 return;
270
271 /* Read the customized LED setting from devicetree */
272 printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds);
273
274 /*
275 * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration
276 * Starting from offset 0x18
277 * Bit[15:12] LED Feature Control(FC)
278 * Bit[11:08] LED Select for PINLED2
279 * Bit[07:04] LED Select for PINLED1
280 * Bit[03:00] LED Select for PINLED0
281 *
282 * Speed Link10M Link100M Link1000M ACT/Full
283 * LED0 Bit0 Bit1 Bit2 Bit3
284 * LED1 Bit4 Bit5 Bit6 Bit7
285 * LED2 Bit8 Bit9 Bit10 Bit11
286 * FC Bit12 Bit13 Bit14 Bit15
287 */
288
289 /* Set customized LED registers */
290 outw(config->customized_leds, io_base + CMD_LED0_LED1);
291 printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n",
292 inw(io_base + CMD_LED0_LED1));
293}
294
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100295static void r8168_init(struct device *dev)
296{
297 /* Get the resource of the NIC mmio */
298 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
299 u16 io_base = (u16)nic_res->base;
300
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800301 /* Check if the base is invalid */
302 if (!io_base) {
303 printk(BIOS_ERR, "r8168: Error cant find IO resource\n");
304 return;
305 }
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100306 /* Enable but do not set bus master */
307 pci_write_config16(dev, PCI_COMMAND,
308 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
309
310 /* Program MAC address based on CBFS "macaddress" containing
311 * a string AA:BB:CC:DD:EE:FF */
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800312 program_mac_address(dev, io_base);
313
314 /* Program customized LED mode */
Julius Wernercd49cce2019-03-05 16:53:33 -0800315 if (CONFIG(RT8168_SET_LED_MODE))
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800316 r8168_set_customized_led(dev, io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000317}
318
Julius Wernercd49cce2019-03-05 16:53:33 -0800319#if CONFIG(HAVE_ACPI_TABLES)
Gaggery Tsai6919a932017-11-16 17:21:23 +0800320#define R8168_ACPI_HID "R8168"
321static void r8168_net_fill_ssdt(struct device *dev)
322{
323 struct drivers_net_config *config = dev->chip_info;
324 const char *path = acpi_device_path(dev->bus->dev);
325 u32 address;
326
327 if (!path || !config)
328 return;
329
330 /* Device */
331 acpigen_write_scope(path);
332 acpigen_write_device(acpi_device_name(dev));
333 acpigen_write_name_string("_HID", R8168_ACPI_HID);
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100334 acpi_device_write_uid(dev);
335
Gaggery Tsai6919a932017-11-16 17:21:23 +0800336 if (dev->chip_ops)
337 acpigen_write_name_string("_DDN", dev->chip_ops->name);
338
Edward O'Callaghanb765fa62020-01-21 21:01:32 +1100339 /* Power Resource */
340 if (config->has_power_resource) {
341 const struct acpi_power_res_params power_res_params = {
342 .stop_gpio = &config->stop_gpio,
343 .stop_delay_ms = config->stop_delay_ms,
344 .stop_off_delay_ms = config->stop_off_delay_ms
345 };
346 acpi_device_add_power_res(&power_res_params);
347 }
348
Gaggery Tsai6919a932017-11-16 17:21:23 +0800349 /* Address */
350 address = PCI_SLOT(dev->path.pci.devfn) & 0xffff;
351 address <<= 16;
352 address |= PCI_FUNC(dev->path.pci.devfn) & 0xffff;
353 acpigen_write_name_dword("_ADR", address);
354
355 /* Wake capabilities */
356 if (config->wake)
357 acpigen_write_PRW(config->wake, 3);
358
359 acpigen_pop_len(); /* Device */
360 acpigen_pop_len(); /* Scope */
361
362 printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
363 dev->chip_ops ? dev->chip_ops->name : "", dev_path(dev));
364}
365
366static const char *r8168_net_acpi_name(const struct device *dev)
367{
368 return "RLTK";
369}
370#endif
371
Damien Zammite983f0c2016-05-21 02:24:19 +1000372static struct device_operations r8168_ops = {
Martin Rothb9810a42017-07-23 20:00:04 -0600373 .read_resources = pci_dev_read_resources,
374 .set_resources = pci_dev_set_resources,
375 .enable_resources = pci_dev_enable_resources,
376 .init = r8168_init,
377 .scan_bus = 0,
Julius Wernercd49cce2019-03-05 16:53:33 -0800378#if CONFIG(HAVE_ACPI_TABLES)
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100379 .acpi_name = r8168_net_acpi_name,
380 .acpi_fill_ssdt_generator = r8168_net_fill_ssdt,
Gaggery Tsai6919a932017-11-16 17:21:23 +0800381#endif
Damien Zammite983f0c2016-05-21 02:24:19 +1000382};
383
384static const struct pci_driver r8168_driver __pci_driver = {
Martin Rothb9810a42017-07-23 20:00:04 -0600385 .ops = &r8168_ops,
386 .vendor = 0x10ec,
387 .device = 0x8168,
Damien Zammite983f0c2016-05-21 02:24:19 +1000388};
Gaggery Tsai6919a932017-11-16 17:21:23 +0800389
390struct chip_operations drivers_net_ops = {
391 CHIP_NAME("Realtek r8168")
392};