blob: 350dc373223ed5ec0b070a03ff9cc8e3e812905c [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>
25#include <string.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100026#include <arch/io.h>
Shelley Chen0528b612017-06-12 18:29:24 -070027#include <console/console.h>
Damien Zammite983f0c2016-05-21 02:24:19 +100028#include <device/device.h>
29#include <device/pci.h>
30#include <device/pci_ops.h>
31#include <device/pci_def.h>
32#include <delay.h>
Shelley Chen0528b612017-06-12 18:29:24 -070033#include <fmap.h>
Gaggery Tsai65623ef2017-09-29 11:15:23 +080034#include "chip.h"
Damien Zammite983f0c2016-05-21 02:24:19 +100035
36#define NIC_TIMEOUT 1000
37
38#define CMD_REG 0x37
39#define CMD_REG_RESET 0x10
Gaggery Tsai65623ef2017-09-29 11:15:23 +080040#define CMD_LED0_LED1 0x18
Damien Zammite983f0c2016-05-21 02:24:19 +100041
42#define CFG_9346 0x50
43#define CFG_9346_LOCK 0x00
44#define CFG_9346_UNLOCK 0xc0
45
Shelley Chen0528b612017-06-12 18:29:24 -070046/**
47 * search: Find first instance of string in a given region
48 * @param p string to find
49 * @param a start address of region to search
50 * @param lengthp length of string to search for
51 * @param lengtha length of region to search in
52 * @return offset offset from start address a where string was found.
53 * If string not found, return lengtha.
54 */
55static size_t search(const char *p, const u8 *a, size_t lengthp,
56 size_t lengtha)
57{
58 size_t i, j;
59
60 if (lengtha < lengthp)
61 return lengtha;
62 /* Searching */
63 for (j = 0; j <= lengtha - lengthp; j++) {
64 for (i = 0; i < lengthp && p[i] == a[i + j]; i++)
65 ;
66 if (i >= lengthp)
67 return j;
68 }
69 return lengtha;
70}
71
Damien Zammitf5dd23f2016-12-01 23:29:34 +110072static u8 get_hex_digit(const u8 c)
Damien Zammite983f0c2016-05-21 02:24:19 +100073{
Damien Zammitf5dd23f2016-12-01 23:29:34 +110074 u8 ret = 0;
Damien Zammite983f0c2016-05-21 02:24:19 +100075
Damien Zammitf5dd23f2016-12-01 23:29:34 +110076 ret = c - '0';
77 if (ret > 0x09) {
78 ret = c - 'A' + 0x0a;
79 if (ret > 0x0f)
80 ret = c - 'a' + 0x0a;
81 }
82 if (ret > 0x0f) {
Shelley Chen0528b612017-06-12 18:29:24 -070083 printk(BIOS_ERR, "Error: Invalid hex digit found: "
84 "%c - 0x%02x\n", (char)c, c);
Damien Zammitf5dd23f2016-12-01 23:29:34 +110085 ret = 0;
86 }
87 return ret;
88}
Damien Zammite983f0c2016-05-21 02:24:19 +100089
Shelley Chen0528b612017-06-12 18:29:24 -070090#define MACLEN 17
91
92static enum cb_err fetch_mac_string_vpd(u8 *macstrbuf)
93{
94 struct region_device rdev;
95 void *search_address;
96 size_t search_length;
97 size_t offset;
98 char key[] = "ethernet_mac";
99
100 if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) {
101 printk(BIOS_ERR, "Error: Couldn't find RO_VPD region.");
102 return CB_ERR;
103 }
104 search_address = rdev_mmap_full(&rdev);
105 if (search_address == NULL) {
106 printk(BIOS_ERR, "LAN: VPD not found.\n");
107 return CB_ERR;
108 }
109
110 search_length = region_device_sz(&rdev);
111 offset = search(key, search_address, strlen(key),
112 search_length);
113 if (offset == search_length) {
114 printk(BIOS_ERR,
115 "Error: Could not locate '%s' in VPD\n", key);
116 return CB_ERR;
117 }
118 printk(BIOS_DEBUG, "Located '%s' in VPD\n", key);
119
120 offset += sizeof(key); /* move to next character */
121
122 if (offset + MACLEN > search_length) {
123 printk(BIOS_ERR, "Search result too small!\n");
124 return CB_ERR;
125 }
126 memcpy(macstrbuf, search_address + offset, MACLEN);
127
128 return CB_SUCCESS;
129}
130
131static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
132{
133 struct cbfsf fh;
134 uint32_t matchraw = CBFS_TYPE_RAW;
135
136 if (!cbfs_boot_locate(&fh, "rt8168-macaddress", &matchraw)) {
137 /* check the cbfs for the mac address */
138 if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
139 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
140 return CB_ERR;
141 }
142 return CB_SUCCESS;
143 }
144 return CB_ERR;
145}
146
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100147static void get_mac_address(u8 *macaddr, const u8 *strbuf)
148{
149 size_t offset = 0;
150 int i;
151
Shelley Chen0528b612017-06-12 18:29:24 -0700152 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
153 (strbuf[8] != ':') || (strbuf[11] != ':') ||
154 (strbuf[14] != ':')) {
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100155 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
156 return;
157 }
158
159 for (i = 0; i < 6; i++) {
160 macaddr[i] = 0;
161 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
162 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
163 offset += 3;
164 }
165}
166
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100167static void program_mac_address(struct device *dev, u16 io_base)
168{
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100169 u8 macstrbuf[MACLEN] = { 0 };
170 int i = 0;
Shelley Chen0528b612017-06-12 18:29:24 -0700171 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100172 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
173
Shelley Chen0528b612017-06-12 18:29:24 -0700174 /* check the VPD for the mac address */
175 if (IS_ENABLED(CONFIG_RT8168_GET_MAC_FROM_VPD)) {
176 if (fetch_mac_string_vpd(macstrbuf) != CB_SUCCESS)
177 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
178 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100179 } else {
Shelley Chen0528b612017-06-12 18:29:24 -0700180 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS)
181 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS,"
182 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100183 }
Shelley Chen0528b612017-06-12 18:29:24 -0700184 get_mac_address(mac, macstrbuf);
Damien Zammite983f0c2016-05-21 02:24:19 +1000185
186 /* Reset NIC */
187 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100188 outb(CMD_REG_RESET, io_base + CMD_REG);
Damien Zammite983f0c2016-05-21 02:24:19 +1000189
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100190 /* Poll for reset, with 1sec timeout */
191 while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) {
Damien Zammite983f0c2016-05-21 02:24:19 +1000192 udelay(1000);
193 if (++i >= NIC_TIMEOUT)
Shelley Chen0528b612017-06-12 18:29:24 -0700194 printk(BIOS_ERR, "timeout waiting for nic to reset\n");
Damien Zammite983f0c2016-05-21 02:24:19 +1000195 }
196 if (i < NIC_TIMEOUT)
197 printk(BIOS_DEBUG, "done\n");
198
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100199 printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
Damien Zammite983f0c2016-05-21 02:24:19 +1000200
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100201 /* Disable register protection */
202 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
Damien Zammite983f0c2016-05-21 02:24:19 +1000203
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100204 /* Set MAC address: only 4-byte write accesses allowed */
205 outl(mac[4] | mac[5] << 8, io_base + 4);
206 inl(io_base + 4);
207 outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
208 io_base);
209 inl(io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000210 /* Lock config regs */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100211 outb(CFG_9346_LOCK, io_base + CFG_9346);
212
213 printk(BIOS_DEBUG, "done\n");
214}
215
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800216static void r8168_set_customized_led(struct device *dev, u16 io_base)
217{
218 struct drivers_net_config *config = dev->chip_info;
219
220 if (!config)
221 return;
222
223 /* Read the customized LED setting from devicetree */
224 printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds);
225
226 /*
227 * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration
228 * Starting from offset 0x18
229 * Bit[15:12] LED Feature Control(FC)
230 * Bit[11:08] LED Select for PINLED2
231 * Bit[07:04] LED Select for PINLED1
232 * Bit[03:00] LED Select for PINLED0
233 *
234 * Speed Link10M Link100M Link1000M ACT/Full
235 * LED0 Bit0 Bit1 Bit2 Bit3
236 * LED1 Bit4 Bit5 Bit6 Bit7
237 * LED2 Bit8 Bit9 Bit10 Bit11
238 * FC Bit12 Bit13 Bit14 Bit15
239 */
240
241 /* Set customized LED registers */
242 outw(config->customized_leds, io_base + CMD_LED0_LED1);
243 printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n",
244 inw(io_base + CMD_LED0_LED1));
245}
246
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100247static void r8168_init(struct device *dev)
248{
249 /* Get the resource of the NIC mmio */
250 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
251 u16 io_base = (u16)nic_res->base;
252
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800253 /* Check if the base is invalid */
254 if (!io_base) {
255 printk(BIOS_ERR, "r8168: Error cant find IO resource\n");
256 return;
257 }
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100258 /* Enable but do not set bus master */
259 pci_write_config16(dev, PCI_COMMAND,
260 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
261
262 /* Program MAC address based on CBFS "macaddress" containing
263 * a string AA:BB:CC:DD:EE:FF */
Gaggery Tsai65623ef2017-09-29 11:15:23 +0800264 program_mac_address(dev, io_base);
265
266 /* Program customized LED mode */
267 if (IS_ENABLED(CONFIG_RT8168_SET_LED_MODE))
268 r8168_set_customized_led(dev, io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000269}
270
271static struct device_operations r8168_ops = {
Martin Rothb9810a42017-07-23 20:00:04 -0600272 .read_resources = pci_dev_read_resources,
273 .set_resources = pci_dev_set_resources,
274 .enable_resources = pci_dev_enable_resources,
275 .init = r8168_init,
276 .scan_bus = 0,
Damien Zammite983f0c2016-05-21 02:24:19 +1000277};
278
279static const struct pci_driver r8168_driver __pci_driver = {
Martin Rothb9810a42017-07-23 20:00:04 -0600280 .ops = &r8168_ops,
281 .vendor = 0x10ec,
282 .device = 0x8168,
Damien Zammite983f0c2016-05-21 02:24:19 +1000283};