blob: 4057ffde523f332a1e3ffc9329b2c775867158ed [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>
Gaggery Tsai65623ef2017-09-29 11:15:23 +080036#include "chip.h"
Damien Zammite983f0c2016-05-21 02:24:19 +100037
38#define NIC_TIMEOUT 1000
39
40#define CMD_REG 0x37
41#define CMD_REG_RESET 0x10
Gaggery Tsai65623ef2017-09-29 11:15:23 +080042#define CMD_LED0_LED1 0x18
Damien Zammite983f0c2016-05-21 02:24:19 +100043
44#define CFG_9346 0x50
45#define CFG_9346_LOCK 0x00
46#define CFG_9346_UNLOCK 0xc0
47
Gaggery Tsai1f847042017-12-26 17:13:52 +080048#define DEVICE_INDEX_BYTE 12
49#define MAX_DEVICE_SUPPORT 10
Shelley Chen0528b612017-06-12 18:29:24 -070050/**
51 * search: Find first instance of string in a given region
52 * @param p string to find
53 * @param a start address of region to search
54 * @param lengthp length of string to search for
55 * @param lengtha length of region to search in
56 * @return offset offset from start address a where string was found.
57 * If string not found, return lengtha.
58 */
59static size_t search(const char *p, const u8 *a, size_t lengthp,
60 size_t lengtha)
61{
62 size_t i, j;
63
64 if (lengtha < lengthp)
65 return lengtha;
66 /* Searching */
67 for (j = 0; j <= lengtha - lengthp; j++) {
68 for (i = 0; i < lengthp && p[i] == a[i + j]; i++)
69 ;
David Wu9a0d9e02018-05-04 15:37:37 +080070 if (i >= lengthp && a[j - 1] == lengthp)
Shelley Chen0528b612017-06-12 18:29:24 -070071 return j;
72 }
73 return lengtha;
74}
75
Damien Zammitf5dd23f2016-12-01 23:29:34 +110076static u8 get_hex_digit(const u8 c)
Damien Zammite983f0c2016-05-21 02:24:19 +100077{
Damien Zammitf5dd23f2016-12-01 23:29:34 +110078 u8 ret = 0;
Damien Zammite983f0c2016-05-21 02:24:19 +100079
Damien Zammitf5dd23f2016-12-01 23:29:34 +110080 ret = c - '0';
81 if (ret > 0x09) {
82 ret = c - 'A' + 0x0a;
83 if (ret > 0x0f)
84 ret = c - 'a' + 0x0a;
85 }
86 if (ret > 0x0f) {
Shelley Chen0528b612017-06-12 18:29:24 -070087 printk(BIOS_ERR, "Error: Invalid hex digit found: "
88 "%c - 0x%02x\n", (char)c, c);
Damien Zammitf5dd23f2016-12-01 23:29:34 +110089 ret = 0;
90 }
91 return ret;
92}
Damien Zammite983f0c2016-05-21 02:24:19 +100093
Shelley Chen0528b612017-06-12 18:29:24 -070094#define MACLEN 17
95
Gaggery Tsai1f847042017-12-26 17:13:52 +080096static enum cb_err fetch_mac_string_vpd(u8 *macstrbuf, const u8 device_index)
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;
Gaggery Tsai1f847042017-12-26 17:13:52 +0800102 char key[] = "ethernet_mac "; /* Leave a space at tail to stuff an index */
103
104 /*
105 * The device_index 0 is treated as an special case matching to
106 * "ethernet_mac" with single NIC on DUT. When there are mulitple
107 * NICs on DUT, they are mapping to "ethernet_macN", where
108 * N is [0-9].
109 */
110 if (device_index == 0)
111 key[DEVICE_INDEX_BYTE] = '\0';
112 else
113 /* Translate index number from integer to ascii */
114 key[DEVICE_INDEX_BYTE] = (device_index - 1) + '0';
Shelley Chen0528b612017-06-12 18:29:24 -0700115
116 if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) {
117 printk(BIOS_ERR, "Error: Couldn't find RO_VPD region.");
118 return CB_ERR;
119 }
120 search_address = rdev_mmap_full(&rdev);
121 if (search_address == NULL) {
122 printk(BIOS_ERR, "LAN: VPD not found.\n");
123 return CB_ERR;
124 }
125
126 search_length = region_device_sz(&rdev);
127 offset = search(key, search_address, strlen(key),
128 search_length);
Gaggery Tsai1f847042017-12-26 17:13:52 +0800129
Shelley Chen0528b612017-06-12 18:29:24 -0700130 if (offset == search_length) {
131 printk(BIOS_ERR,
132 "Error: Could not locate '%s' in VPD\n", key);
133 return CB_ERR;
134 }
135 printk(BIOS_DEBUG, "Located '%s' in VPD\n", key);
136
Gaggery Tsai1f847042017-12-26 17:13:52 +0800137 offset += strlen(key) + 1; /* move to next character */
Shelley Chen0528b612017-06-12 18:29:24 -0700138
139 if (offset + MACLEN > search_length) {
140 printk(BIOS_ERR, "Search result too small!\n");
141 return CB_ERR;
142 }
143 memcpy(macstrbuf, search_address + offset, MACLEN);
Shelley Chen0528b612017-06-12 18:29:24 -0700144 return CB_SUCCESS;
145}
146
147static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
148{
149 struct cbfsf fh;
150 uint32_t matchraw = CBFS_TYPE_RAW;
151
152 if (!cbfs_boot_locate(&fh, "rt8168-macaddress", &matchraw)) {
153 /* check the cbfs for the mac address */
154 if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
155 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
156 return CB_ERR;
157 }
158 return CB_SUCCESS;
159 }
160 return CB_ERR;
161}
162
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100163static void get_mac_address(u8 *macaddr, const u8 *strbuf)
164{
165 size_t offset = 0;
166 int i;
167
Shelley Chen0528b612017-06-12 18:29:24 -0700168 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
169 (strbuf[8] != ':') || (strbuf[11] != ':') ||
170 (strbuf[14] != ':')) {
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100171 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
172 return;
173 }
174
175 for (i = 0; i < 6; i++) {
176 macaddr[i] = 0;
177 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
178 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
179 offset += 3;
180 }
181}
182
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100183static void program_mac_address(struct device *dev, u16 io_base)
184{
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100185 u8 macstrbuf[MACLEN] = { 0 };
186 int i = 0;
Shelley Chen0528b612017-06-12 18:29:24 -0700187 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100188 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
Gaggery Tsai1f847042017-12-26 17:13:52 +0800189 struct drivers_net_config *config = dev->chip_info;
David Wu9a0d9e02018-05-04 15:37:37 +0800190 bool mac_found = false;
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100191
Shelley Chen0528b612017-06-12 18:29:24 -0700192 /* check the VPD for the mac address */
193 if (IS_ENABLED(CONFIG_RT8168_GET_MAC_FROM_VPD)) {
Gaggery Tsai1f847042017-12-26 17:13:52 +0800194 /* Current implementation is up to 10 NIC cards */
195 if (config && config->device_index <= MAX_DEVICE_SUPPORT) {
David Wu9a0d9e02018-05-04 15:37:37 +0800196 /* check "ethernet_mac" first when the device index is 1 */
197 if (config->device_index == 1 &&
198 fetch_mac_string_vpd(macstrbuf, 0) == CB_SUCCESS)
199 mac_found = true;
200 if (!mac_found && fetch_mac_string_vpd(macstrbuf,
201 config->device_index) != CB_SUCCESS)
Gaggery Tsai1f847042017-12-26 17:13:52 +0800202 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
203 " using default 00:e0:4c:00:c0:b0\n");
204 } else {
205 printk(BIOS_ERR, "r8168: the maximum device_index should be"
206 " less then %d\n. Using default 00:e0:4c:00:c0:b0\n",
207 MAX_DEVICE_SUPPORT);
208 }
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100209 } else {
Shelley Chen0528b612017-06-12 18:29:24 -0700210 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS)
211 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS,"
Gaggery Tsai1f847042017-12-26 17:13:52 +0800212 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100213 }
Shelley Chen0528b612017-06-12 18:29:24 -0700214 get_mac_address(mac, macstrbuf);
Damien Zammite983f0c2016-05-21 02:24:19 +1000215
216 /* Reset NIC */
217 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100218 outb(CMD_REG_RESET, io_base + CMD_REG);
Damien Zammite983f0c2016-05-21 02:24:19 +1000219
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100220 /* Poll for reset, with 1sec timeout */
221 while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) {
Damien Zammite983f0c2016-05-21 02:24:19 +1000222 udelay(1000);
223 if (++i >= NIC_TIMEOUT)
Shelley Chen0528b612017-06-12 18:29:24 -0700224 printk(BIOS_ERR, "timeout waiting for nic to reset\n");
Damien Zammite983f0c2016-05-21 02:24:19 +1000225 }
226 if (i < NIC_TIMEOUT)
227 printk(BIOS_DEBUG, "done\n");
228
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100229 printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
Damien Zammite983f0c2016-05-21 02:24:19 +1000230
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100231 /* Disable register protection */
232 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
Damien Zammite983f0c2016-05-21 02:24:19 +1000233
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100234 /* Set MAC address: only 4-byte write accesses allowed */
235 outl(mac[4] | mac[5] << 8, io_base + 4);
236 inl(io_base + 4);
237 outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
238 io_base);
239 inl(io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000240 /* Lock config regs */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100241 outb(CFG_9346_LOCK, io_base + CFG_9346);
242
243 printk(BIOS_DEBUG, "done\n");
244}
245
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800246static void r8168_set_customized_led(struct device *dev, u16 io_base)
247{
248 struct drivers_net_config *config = dev->chip_info;
249
250 if (!config)
251 return;
252
253 /* Read the customized LED setting from devicetree */
254 printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds);
255
256 /*
257 * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration
258 * Starting from offset 0x18
259 * Bit[15:12] LED Feature Control(FC)
260 * Bit[11:08] LED Select for PINLED2
261 * Bit[07:04] LED Select for PINLED1
262 * Bit[03:00] LED Select for PINLED0
263 *
264 * Speed Link10M Link100M Link1000M ACT/Full
265 * LED0 Bit0 Bit1 Bit2 Bit3
266 * LED1 Bit4 Bit5 Bit6 Bit7
267 * LED2 Bit8 Bit9 Bit10 Bit11
268 * FC Bit12 Bit13 Bit14 Bit15
269 */
270
271 /* Set customized LED registers */
272 outw(config->customized_leds, io_base + CMD_LED0_LED1);
273 printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n",
274 inw(io_base + CMD_LED0_LED1));
275}
276
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100277static void r8168_init(struct device *dev)
278{
279 /* Get the resource of the NIC mmio */
280 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
281 u16 io_base = (u16)nic_res->base;
282
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800283 /* Check if the base is invalid */
284 if (!io_base) {
285 printk(BIOS_ERR, "r8168: Error cant find IO resource\n");
286 return;
287 }
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100288 /* Enable but do not set bus master */
289 pci_write_config16(dev, PCI_COMMAND,
290 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
291
292 /* Program MAC address based on CBFS "macaddress" containing
293 * a string AA:BB:CC:DD:EE:FF */
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800294 program_mac_address(dev, io_base);
295
296 /* Program customized LED mode */
297 if (IS_ENABLED(CONFIG_RT8168_SET_LED_MODE))
298 r8168_set_customized_led(dev, io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000299}
300
Gaggery Tsai6919a932017-11-16 17:21:23 +0800301#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
302#define R8168_ACPI_HID "R8168"
303static void r8168_net_fill_ssdt(struct device *dev)
304{
305 struct drivers_net_config *config = dev->chip_info;
306 const char *path = acpi_device_path(dev->bus->dev);
307 u32 address;
308
309 if (!path || !config)
310 return;
311
312 /* Device */
313 acpigen_write_scope(path);
314 acpigen_write_device(acpi_device_name(dev));
315 acpigen_write_name_string("_HID", R8168_ACPI_HID);
316 acpigen_write_name_integer("_UID", 0);
317 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,
Gaggery Tsai6919a932017-11-16 17:21:23 +0800349#if IS_ENABLED(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};