blob: 318877890038fbef9098535aec3152fa1eb93042 [file] [log] [blame]
Damien Zammite983f0c2016-05-21 02:24:19 +10001/*
2 * This file is part of the coreboot project.
3 *
Damien Zammitf5dd23f2016-12-01 23:29:34 +11004 * Copyright (C) 2012 Google Inc.
Damien Zammite983f0c2016-05-21 02:24:19 +10005 * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17/*
Damien Zammitf5dd23f2016-12-01 23:29:34 +110018 * This driver resets the 10ec:8168 NIC then tries to read
19 * "macaddress" string XX:XX:XX:XX:XX:XX from CBFS.
20 * If no MAC is found, it programs a default MAC address in the device
Damien Zammite983f0c2016-05-21 02:24:19 +100021 * so that if the EEPROM/efuse is unconfigured it still has a default MAC.
22 */
23
Damien Zammitf5dd23f2016-12-01 23:29:34 +110024#include <cbfs.h>
Gaggery Tsai6919a932017-11-16 17:21:23 +080025#include <arch/acpi_device.h>
26#include <arch/acpigen.h>
Damien Zammitf5dd23f2016-12-01 23:29:34 +110027#include <string.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100028#include <arch/io.h>
Shelley Chen0528b612017-06-12 18:29:24 -070029#include <console/console.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100030#include <device/device.h>
31#include <device/pci.h>
32#include <device/pci_ops.h>
33#include <device/pci_def.h>
34#include <delay.h>
Shelley Chen0528b612017-06-12 18:29:24 -070035#include <fmap.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020036#include <types.h>
37
Gaggery Tsai65623ef2017-09-29 11:15:23 +080038#include "chip.h"
Damien Zammite983f0c2016-05-21 02:24:19 +100039
40#define NIC_TIMEOUT 1000
41
42#define CMD_REG 0x37
43#define CMD_REG_RESET 0x10
Gaggery Tsai65623ef2017-09-29 11:15:23 +080044#define CMD_LED0_LED1 0x18
Damien Zammite983f0c2016-05-21 02:24:19 +100045
46#define CFG_9346 0x50
47#define CFG_9346_LOCK 0x00
48#define CFG_9346_UNLOCK 0xc0
49
Gaggery Tsai1f847042017-12-26 17:13:52 +080050#define DEVICE_INDEX_BYTE 12
51#define MAX_DEVICE_SUPPORT 10
Shelley Chen0528b612017-06-12 18:29:24 -070052/**
53 * search: Find first instance of string in a given region
54 * @param p string to find
55 * @param a start address of region to search
56 * @param lengthp length of string to search for
57 * @param lengtha length of region to search in
58 * @return offset offset from start address a where string was found.
59 * If string not found, return lengtha.
60 */
61static size_t search(const char *p, const u8 *a, size_t lengthp,
62 size_t lengtha)
63{
64 size_t i, j;
65
66 if (lengtha < lengthp)
67 return lengtha;
68 /* Searching */
69 for (j = 0; j <= lengtha - lengthp; j++) {
70 for (i = 0; i < lengthp && p[i] == a[i + j]; i++)
71 ;
David Wu9a0d9e02018-05-04 15:37:37 +080072 if (i >= lengthp && a[j - 1] == lengthp)
Shelley Chen0528b612017-06-12 18:29:24 -070073 return j;
74 }
75 return lengtha;
76}
77
Damien Zammitf5dd23f2016-12-01 23:29:34 +110078static u8 get_hex_digit(const u8 c)
Damien Zammite983f0c2016-05-21 02:24:19 +100079{
Damien Zammitf5dd23f2016-12-01 23:29:34 +110080 u8 ret = 0;
Damien Zammite983f0c2016-05-21 02:24:19 +100081
Damien Zammitf5dd23f2016-12-01 23:29:34 +110082 ret = c - '0';
83 if (ret > 0x09) {
84 ret = c - 'A' + 0x0a;
85 if (ret > 0x0f)
86 ret = c - 'a' + 0x0a;
87 }
88 if (ret > 0x0f) {
Shelley Chen0528b612017-06-12 18:29:24 -070089 printk(BIOS_ERR, "Error: Invalid hex digit found: "
90 "%c - 0x%02x\n", (char)c, c);
Damien Zammitf5dd23f2016-12-01 23:29:34 +110091 ret = 0;
92 }
93 return ret;
94}
Damien Zammite983f0c2016-05-21 02:24:19 +100095
Shelley Chen0528b612017-06-12 18:29:24 -070096#define MACLEN 17
97
Gaggery Tsai1f847042017-12-26 17:13:52 +080098static enum cb_err fetch_mac_string_vpd(u8 *macstrbuf, const u8 device_index)
Shelley Chen0528b612017-06-12 18:29:24 -070099{
100 struct region_device rdev;
101 void *search_address;
102 size_t search_length;
103 size_t offset;
Gaggery Tsai1f847042017-12-26 17:13:52 +0800104 char key[] = "ethernet_mac "; /* Leave a space at tail to stuff an index */
105
106 /*
107 * The device_index 0 is treated as an special case matching to
108 * "ethernet_mac" with single NIC on DUT. When there are mulitple
109 * NICs on DUT, they are mapping to "ethernet_macN", where
110 * N is [0-9].
111 */
112 if (device_index == 0)
113 key[DEVICE_INDEX_BYTE] = '\0';
114 else
115 /* Translate index number from integer to ascii */
116 key[DEVICE_INDEX_BYTE] = (device_index - 1) + '0';
Shelley Chen0528b612017-06-12 18:29:24 -0700117
118 if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) {
119 printk(BIOS_ERR, "Error: Couldn't find RO_VPD region.");
120 return CB_ERR;
121 }
122 search_address = rdev_mmap_full(&rdev);
123 if (search_address == NULL) {
124 printk(BIOS_ERR, "LAN: VPD not found.\n");
125 return CB_ERR;
126 }
127
128 search_length = region_device_sz(&rdev);
129 offset = search(key, search_address, strlen(key),
130 search_length);
Gaggery Tsai1f847042017-12-26 17:13:52 +0800131
Shelley Chen0528b612017-06-12 18:29:24 -0700132 if (offset == search_length) {
133 printk(BIOS_ERR,
134 "Error: Could not locate '%s' in VPD\n", key);
135 return CB_ERR;
136 }
137 printk(BIOS_DEBUG, "Located '%s' in VPD\n", key);
138
Gaggery Tsai1f847042017-12-26 17:13:52 +0800139 offset += strlen(key) + 1; /* move to next character */
Shelley Chen0528b612017-06-12 18:29:24 -0700140
141 if (offset + MACLEN > search_length) {
142 printk(BIOS_ERR, "Search result too small!\n");
143 return CB_ERR;
144 }
145 memcpy(macstrbuf, search_address + offset, MACLEN);
Shelley Chen0528b612017-06-12 18:29:24 -0700146 return CB_SUCCESS;
147}
148
149static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
150{
151 struct cbfsf fh;
152 uint32_t matchraw = CBFS_TYPE_RAW;
153
154 if (!cbfs_boot_locate(&fh, "rt8168-macaddress", &matchraw)) {
155 /* check the cbfs for the mac address */
156 if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
157 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
158 return CB_ERR;
159 }
160 return CB_SUCCESS;
161 }
162 return CB_ERR;
163}
164
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100165static void get_mac_address(u8 *macaddr, const u8 *strbuf)
166{
167 size_t offset = 0;
168 int i;
169
Shelley Chen0528b612017-06-12 18:29:24 -0700170 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
171 (strbuf[8] != ':') || (strbuf[11] != ':') ||
172 (strbuf[14] != ':')) {
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100173 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
174 return;
175 }
176
177 for (i = 0; i < 6; i++) {
178 macaddr[i] = 0;
179 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
180 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
181 offset += 3;
182 }
183}
184
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100185static void program_mac_address(struct device *dev, u16 io_base)
186{
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100187 u8 macstrbuf[MACLEN] = { 0 };
188 int i = 0;
Shelley Chen0528b612017-06-12 18:29:24 -0700189 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100190 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
Gaggery Tsai1f847042017-12-26 17:13:52 +0800191 struct drivers_net_config *config = dev->chip_info;
David Wu9a0d9e02018-05-04 15:37:37 +0800192 bool mac_found = false;
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100193
Shelley Chen0528b612017-06-12 18:29:24 -0700194 /* check the VPD for the mac address */
Julius Wernercd49cce2019-03-05 16:53:33 -0800195 if (CONFIG(RT8168_GET_MAC_FROM_VPD)) {
Gaggery Tsai1f847042017-12-26 17:13:52 +0800196 /* Current implementation is up to 10 NIC cards */
197 if (config && config->device_index <= MAX_DEVICE_SUPPORT) {
David Wu9a0d9e02018-05-04 15:37:37 +0800198 /* check "ethernet_mac" first when the device index is 1 */
199 if (config->device_index == 1 &&
200 fetch_mac_string_vpd(macstrbuf, 0) == CB_SUCCESS)
201 mac_found = true;
202 if (!mac_found && fetch_mac_string_vpd(macstrbuf,
203 config->device_index) != CB_SUCCESS)
Gaggery Tsai1f847042017-12-26 17:13:52 +0800204 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
205 " using default 00:e0:4c:00:c0:b0\n");
206 } else {
207 printk(BIOS_ERR, "r8168: the maximum device_index should be"
208 " less then %d\n. Using default 00:e0:4c:00:c0:b0\n",
209 MAX_DEVICE_SUPPORT);
210 }
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100211 } else {
Shelley Chen0528b612017-06-12 18:29:24 -0700212 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS)
213 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS,"
Gaggery Tsai1f847042017-12-26 17:13:52 +0800214 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100215 }
Shelley Chen0528b612017-06-12 18:29:24 -0700216 get_mac_address(mac, macstrbuf);
Damien Zammite983f0c2016-05-21 02:24:19 +1000217
218 /* Reset NIC */
219 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100220 outb(CMD_REG_RESET, io_base + CMD_REG);
Damien Zammite983f0c2016-05-21 02:24:19 +1000221
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100222 /* Poll for reset, with 1sec timeout */
223 while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) {
Damien Zammite983f0c2016-05-21 02:24:19 +1000224 udelay(1000);
225 if (++i >= NIC_TIMEOUT)
Shelley Chen0528b612017-06-12 18:29:24 -0700226 printk(BIOS_ERR, "timeout waiting for nic to reset\n");
Damien Zammite983f0c2016-05-21 02:24:19 +1000227 }
228 if (i < NIC_TIMEOUT)
229 printk(BIOS_DEBUG, "done\n");
230
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100231 printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
Damien Zammite983f0c2016-05-21 02:24:19 +1000232
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100233 /* Disable register protection */
234 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
Damien Zammite983f0c2016-05-21 02:24:19 +1000235
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100236 /* Set MAC address: only 4-byte write accesses allowed */
237 outl(mac[4] | mac[5] << 8, io_base + 4);
238 inl(io_base + 4);
239 outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
240 io_base);
241 inl(io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000242 /* Lock config regs */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100243 outb(CFG_9346_LOCK, io_base + CFG_9346);
244
245 printk(BIOS_DEBUG, "done\n");
246}
247
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800248static void r8168_set_customized_led(struct device *dev, u16 io_base)
249{
250 struct drivers_net_config *config = dev->chip_info;
251
252 if (!config)
253 return;
254
255 /* Read the customized LED setting from devicetree */
256 printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds);
257
258 /*
259 * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration
260 * Starting from offset 0x18
261 * Bit[15:12] LED Feature Control(FC)
262 * Bit[11:08] LED Select for PINLED2
263 * Bit[07:04] LED Select for PINLED1
264 * Bit[03:00] LED Select for PINLED0
265 *
266 * Speed Link10M Link100M Link1000M ACT/Full
267 * LED0 Bit0 Bit1 Bit2 Bit3
268 * LED1 Bit4 Bit5 Bit6 Bit7
269 * LED2 Bit8 Bit9 Bit10 Bit11
270 * FC Bit12 Bit13 Bit14 Bit15
271 */
272
273 /* Set customized LED registers */
274 outw(config->customized_leds, io_base + CMD_LED0_LED1);
275 printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n",
276 inw(io_base + CMD_LED0_LED1));
277}
278
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100279static void r8168_init(struct device *dev)
280{
281 /* Get the resource of the NIC mmio */
282 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
283 u16 io_base = (u16)nic_res->base;
284
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800285 /* Check if the base is invalid */
286 if (!io_base) {
287 printk(BIOS_ERR, "r8168: Error cant find IO resource\n");
288 return;
289 }
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100290 /* Enable but do not set bus master */
291 pci_write_config16(dev, PCI_COMMAND,
292 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
293
294 /* Program MAC address based on CBFS "macaddress" containing
295 * a string AA:BB:CC:DD:EE:FF */
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800296 program_mac_address(dev, io_base);
297
298 /* Program customized LED mode */
Julius Wernercd49cce2019-03-05 16:53:33 -0800299 if (CONFIG(RT8168_SET_LED_MODE))
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800300 r8168_set_customized_led(dev, io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000301}
302
Julius Wernercd49cce2019-03-05 16:53:33 -0800303#if CONFIG(HAVE_ACPI_TABLES)
Gaggery Tsai6919a932017-11-16 17:21:23 +0800304#define R8168_ACPI_HID "R8168"
305static void r8168_net_fill_ssdt(struct device *dev)
306{
307 struct drivers_net_config *config = dev->chip_info;
308 const char *path = acpi_device_path(dev->bus->dev);
309 u32 address;
310
311 if (!path || !config)
312 return;
313
314 /* Device */
315 acpigen_write_scope(path);
316 acpigen_write_device(acpi_device_name(dev));
317 acpigen_write_name_string("_HID", R8168_ACPI_HID);
318 acpigen_write_name_integer("_UID", 0);
319 if (dev->chip_ops)
320 acpigen_write_name_string("_DDN", dev->chip_ops->name);
321
322 /* Address */
323 address = PCI_SLOT(dev->path.pci.devfn) & 0xffff;
324 address <<= 16;
325 address |= PCI_FUNC(dev->path.pci.devfn) & 0xffff;
326 acpigen_write_name_dword("_ADR", address);
327
328 /* Wake capabilities */
329 if (config->wake)
330 acpigen_write_PRW(config->wake, 3);
331
332 acpigen_pop_len(); /* Device */
333 acpigen_pop_len(); /* Scope */
334
335 printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
336 dev->chip_ops ? dev->chip_ops->name : "", dev_path(dev));
337}
338
339static const char *r8168_net_acpi_name(const struct device *dev)
340{
341 return "RLTK";
342}
343#endif
344
Damien Zammite983f0c2016-05-21 02:24:19 +1000345static struct device_operations r8168_ops = {
Martin Rothb9810a42017-07-23 20:00:04 -0600346 .read_resources = pci_dev_read_resources,
347 .set_resources = pci_dev_set_resources,
348 .enable_resources = pci_dev_enable_resources,
349 .init = r8168_init,
350 .scan_bus = 0,
Julius Wernercd49cce2019-03-05 16:53:33 -0800351#if CONFIG(HAVE_ACPI_TABLES)
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100352 .acpi_name = r8168_net_acpi_name,
353 .acpi_fill_ssdt_generator = r8168_net_fill_ssdt,
Gaggery Tsai6919a932017-11-16 17:21:23 +0800354#endif
Damien Zammite983f0c2016-05-21 02:24:19 +1000355};
356
357static const struct pci_driver r8168_driver __pci_driver = {
Martin Rothb9810a42017-07-23 20:00:04 -0600358 .ops = &r8168_ops,
359 .vendor = 0x10ec,
360 .device = 0x8168,
Damien Zammite983f0c2016-05-21 02:24:19 +1000361};
Gaggery Tsai6919a932017-11-16 17:21:23 +0800362
363struct chip_operations drivers_net_ops = {
364 CHIP_NAME("Realtek r8168")
365};