blob: 3727ccdd0fbab64349308a9fb637b6471eb7ee9d [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>
Damien Zammite983f0c2016-05-21 02:24:19 +100034
35#define NIC_TIMEOUT 1000
36
37#define CMD_REG 0x37
38#define CMD_REG_RESET 0x10
39
40#define CFG_9346 0x50
41#define CFG_9346_LOCK 0x00
42#define CFG_9346_UNLOCK 0xc0
43
Shelley Chen0528b612017-06-12 18:29:24 -070044/**
45 * search: Find first instance of string in a given region
46 * @param p string to find
47 * @param a start address of region to search
48 * @param lengthp length of string to search for
49 * @param lengtha length of region to search in
50 * @return offset offset from start address a where string was found.
51 * If string not found, return lengtha.
52 */
53static size_t search(const char *p, const u8 *a, size_t lengthp,
54 size_t lengtha)
55{
56 size_t i, j;
57
58 if (lengtha < lengthp)
59 return lengtha;
60 /* Searching */
61 for (j = 0; j <= lengtha - lengthp; j++) {
62 for (i = 0; i < lengthp && p[i] == a[i + j]; i++)
63 ;
64 if (i >= lengthp)
65 return j;
66 }
67 return lengtha;
68}
69
Damien Zammitf5dd23f2016-12-01 23:29:34 +110070static u8 get_hex_digit(const u8 c)
Damien Zammite983f0c2016-05-21 02:24:19 +100071{
Damien Zammitf5dd23f2016-12-01 23:29:34 +110072 u8 ret = 0;
Damien Zammite983f0c2016-05-21 02:24:19 +100073
Damien Zammitf5dd23f2016-12-01 23:29:34 +110074 ret = c - '0';
75 if (ret > 0x09) {
76 ret = c - 'A' + 0x0a;
77 if (ret > 0x0f)
78 ret = c - 'a' + 0x0a;
79 }
80 if (ret > 0x0f) {
Shelley Chen0528b612017-06-12 18:29:24 -070081 printk(BIOS_ERR, "Error: Invalid hex digit found: "
82 "%c - 0x%02x\n", (char)c, c);
Damien Zammitf5dd23f2016-12-01 23:29:34 +110083 ret = 0;
84 }
85 return ret;
86}
Damien Zammite983f0c2016-05-21 02:24:19 +100087
Shelley Chen0528b612017-06-12 18:29:24 -070088#define MACLEN 17
89
90static enum cb_err fetch_mac_string_vpd(u8 *macstrbuf)
91{
92 struct region_device rdev;
93 void *search_address;
94 size_t search_length;
95 size_t offset;
96 char key[] = "ethernet_mac";
97
98 if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) {
99 printk(BIOS_ERR, "Error: Couldn't find RO_VPD region.");
100 return CB_ERR;
101 }
102 search_address = rdev_mmap_full(&rdev);
103 if (search_address == NULL) {
104 printk(BIOS_ERR, "LAN: VPD not found.\n");
105 return CB_ERR;
106 }
107
108 search_length = region_device_sz(&rdev);
109 offset = search(key, search_address, strlen(key),
110 search_length);
111 if (offset == search_length) {
112 printk(BIOS_ERR,
113 "Error: Could not locate '%s' in VPD\n", key);
114 return CB_ERR;
115 }
116 printk(BIOS_DEBUG, "Located '%s' in VPD\n", key);
117
118 offset += sizeof(key); /* move to next character */
119
120 if (offset + MACLEN > search_length) {
121 printk(BIOS_ERR, "Search result too small!\n");
122 return CB_ERR;
123 }
124 memcpy(macstrbuf, search_address + offset, MACLEN);
125
126 return CB_SUCCESS;
127}
128
129static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
130{
131 struct cbfsf fh;
132 uint32_t matchraw = CBFS_TYPE_RAW;
133
134 if (!cbfs_boot_locate(&fh, "rt8168-macaddress", &matchraw)) {
135 /* check the cbfs for the mac address */
136 if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
137 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
138 return CB_ERR;
139 }
140 return CB_SUCCESS;
141 }
142 return CB_ERR;
143}
144
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100145static void get_mac_address(u8 *macaddr, const u8 *strbuf)
146{
147 size_t offset = 0;
148 int i;
149
Shelley Chen0528b612017-06-12 18:29:24 -0700150 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
151 (strbuf[8] != ':') || (strbuf[11] != ':') ||
152 (strbuf[14] != ':')) {
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100153 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
154 return;
155 }
156
157 for (i = 0; i < 6; i++) {
158 macaddr[i] = 0;
159 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
160 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
161 offset += 3;
162 }
163}
164
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100165static void program_mac_address(struct device *dev, u16 io_base)
166{
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100167 u8 macstrbuf[MACLEN] = { 0 };
168 int i = 0;
Shelley Chen0528b612017-06-12 18:29:24 -0700169 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100170 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
171
Shelley Chen0528b612017-06-12 18:29:24 -0700172 /* check the VPD for the mac address */
173 if (IS_ENABLED(CONFIG_RT8168_GET_MAC_FROM_VPD)) {
174 if (fetch_mac_string_vpd(macstrbuf) != CB_SUCCESS)
175 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
176 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100177 } else {
Shelley Chen0528b612017-06-12 18:29:24 -0700178 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS)
179 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS,"
180 " using default 00:e0:4c:00:c0:b0\n");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100181 }
Shelley Chen0528b612017-06-12 18:29:24 -0700182 get_mac_address(mac, macstrbuf);
Damien Zammite983f0c2016-05-21 02:24:19 +1000183
184 /* Reset NIC */
185 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100186 outb(CMD_REG_RESET, io_base + CMD_REG);
Damien Zammite983f0c2016-05-21 02:24:19 +1000187
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100188 /* Poll for reset, with 1sec timeout */
189 while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) {
Damien Zammite983f0c2016-05-21 02:24:19 +1000190 udelay(1000);
191 if (++i >= NIC_TIMEOUT)
Shelley Chen0528b612017-06-12 18:29:24 -0700192 printk(BIOS_ERR, "timeout waiting for nic to reset\n");
Damien Zammite983f0c2016-05-21 02:24:19 +1000193 }
194 if (i < NIC_TIMEOUT)
195 printk(BIOS_DEBUG, "done\n");
196
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100197 printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
Damien Zammite983f0c2016-05-21 02:24:19 +1000198
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100199 /* Disable register protection */
200 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
Damien Zammite983f0c2016-05-21 02:24:19 +1000201
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100202 /* Set MAC address: only 4-byte write accesses allowed */
203 outl(mac[4] | mac[5] << 8, io_base + 4);
204 inl(io_base + 4);
205 outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
206 io_base);
207 inl(io_base);
Damien Zammite983f0c2016-05-21 02:24:19 +1000208 /* Lock config regs */
Damien Zammitf5dd23f2016-12-01 23:29:34 +1100209 outb(CFG_9346_LOCK, io_base + CFG_9346);
210
211 printk(BIOS_DEBUG, "done\n");
212}
213
214static void r8168_init(struct device *dev)
215{
216 /* Get the resource of the NIC mmio */
217 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
218 u16 io_base = (u16)nic_res->base;
219
220 /* Enable but do not set bus master */
221 pci_write_config16(dev, PCI_COMMAND,
222 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
223
224 /* Program MAC address based on CBFS "macaddress" containing
225 * a string AA:BB:CC:DD:EE:FF */
226 if (io_base)
227 program_mac_address(dev, io_base);
228 else
Shelley Chen0528b612017-06-12 18:29:24 -0700229 printk(BIOS_ERR, "r8168: Error cant find MMIO resource\n");
Damien Zammite983f0c2016-05-21 02:24:19 +1000230}
231
232static struct device_operations r8168_ops = {
Martin Rothb9810a42017-07-23 20:00:04 -0600233 .read_resources = pci_dev_read_resources,
234 .set_resources = pci_dev_set_resources,
235 .enable_resources = pci_dev_enable_resources,
236 .init = r8168_init,
237 .scan_bus = 0,
Damien Zammite983f0c2016-05-21 02:24:19 +1000238};
239
240static const struct pci_driver r8168_driver __pci_driver = {
Martin Rothb9810a42017-07-23 20:00:04 -0600241 .ops = &r8168_ops,
242 .vendor = 0x10ec,
243 .device = 0x8168,
Damien Zammite983f0c2016-05-21 02:24:19 +1000244};