Debian Codegraph @

src/drivers/net/r8168.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 /*
4 * This driver resets the 10ec:8168 NIC then tries to read
5 * "macaddress" string XX:XX:XX:XX:XX:XX from CBFS.
6 * If no MAC is found, it programs a default MAC address in the device
7 * so that if the EEPROM/efuse is unconfigured it still has a default MAC.
8 */
9
10 #include <cbfs.h>
11 #include <acpi/acpi_device.h>
12 #include <acpi/acpigen.h>
13 #include <string.h>
14 #include <arch/io.h>
15 #include <console/console.h>
16 #include <device/device.h>
17 #include <device/pci.h>
18 #include <device/pci_ops.h>
19 #include <device/pci_ids.h>
20 #include <delay.h>
21 #include <fmap.h>
22 #include <types.h>
23
24 #include "chip.h"
25
26 #define NIC_TIMEOUT 1000
27
28 #define CMD_REG 0x37
29 #define CMD_REG_RESET 0x10
30 #define CMD_LED0_LED1 0x18
31 #define CMD_LED_FEATURE 0x94
32 #define CMD_LEDSEL0 0x18
33 #define CMD_LEDSEL2 0x84
34
35 #define CFG_9346 0x50
36 #define CFG_9346_LOCK 0x00
37 #define CFG_9346_UNLOCK 0xc0
38 #define CMD_REG_ASPM 0xb0
39 #define ASPM_L1_2_MASK 0xe059000f
40 #define ERIDR 0x70
41 #define ERIAR 0x74
42
43 #define DEVICE_INDEX_BYTE 12
44 #define MAX_DEVICE_SUPPORT 10
45 /**
46 * search: Find first instance of string in a given region
47 * @param p string to find
48 * @param a start address of region to search
49 * @param lengthp length of string to search for
50 * @param lengtha length of region to search in
51 * @return offset offset from start address a where string was found.
52 * If string not found, return lengtha.
53 */
54 static size_t search(const char *p, const u8 *a, size_t lengthp,
55 size_t lengtha)
56 {
57 size_t i, j;
58
59 if (lengtha < lengthp)
60 return lengtha;
61 /* Searching */
62 for (j = 0; j <= lengtha - lengthp; j++) {
63 for (i = 0; i < lengthp && p[i] == a[i + j]; i++)
64 ;
65 if (i >= lengthp && a[j - 1] == lengthp)
66 return j;
67 }
68 return lengtha;
69 }
70
71 static u8 get_hex_digit(const u8 c)
72 {
73 u8 ret = 0;
74
75 ret = c - '0';
76 if (ret > 0x09) {
77 ret = c - 'A' + 0x0a;
78 if (ret > 0x0f)
79 ret = c - 'a' + 0x0a;
80 }
81 if (ret > 0x0f) {
82 printk(BIOS_ERR, "Invalid hex digit found: "
83 "%c - 0x%02x\n", (char)c, c);
84 ret = 0;
85 }
86 return ret;
87 }
88
89 #define MACLEN 17
90
91 /* Returns MAC address based on the key that is passed in. */
92 static enum cb_err fetch_mac_vpd_key(u8 *macstrbuf, const char *vpd_key)
93 {
94 struct region_device rdev;
95 void *search_address;
96 size_t search_length;
97 size_t offset;
98
99 if (fmap_locate_area_as_rdev("RO_VPD", &rdev)) {
100 printk(BIOS_ERR, "Couldn't find RO_VPD region.");
101 return CB_ERR;
102 }
103 search_address = rdev_mmap_full(&rdev);
104 if (search_address == NULL) {
105 printk(BIOS_ERR, "LAN: VPD not found.\n");
106 return CB_ERR;
107 }
108
109 search_length = region_device_sz(&rdev);
110 offset = search(vpd_key, search_address, strlen(vpd_key),
111 search_length);
112
113 if (offset == search_length) {
114 printk(BIOS_WARNING, "Could not locate '%s' in VPD\n", vpd_key);
115 rdev_munmap(&rdev, search_address);
116 return CB_ERR;
117 }
118 printk(BIOS_DEBUG, "Located '%s' in VPD\n", vpd_key);
119
120 offset += strlen(vpd_key) + 1; /* move to next character */
121
122 if (offset + MACLEN > search_length) {
123 rdev_munmap(&rdev, search_address);
124 printk(BIOS_ERR, "Search result too small!\n");
125 return CB_ERR;
126 }
127 memcpy(macstrbuf, search_address + offset, MACLEN);
128 rdev_munmap(&rdev, search_address);
129
130 return CB_SUCCESS;
131 }
132
133 /* Prepares vpd_key by concatenating ethernet_mac with device_index */
134 static enum cb_err fetch_mac_vpd_dev_idx(u8 *macstrbuf, u8 device_index)
135 {
136 char key[] = "ethernet_mac "; /* Leave a space at tail to stuff an index */
137
138 /*
139 * Map each NIC on the DUT to "ethernet_macN", where N is [0-9].
140 * Translate index number from integer to ascii by adding '0' char.
141 */
142 key[DEVICE_INDEX_BYTE] = device_index + '0';
143
144 return fetch_mac_vpd_key(macstrbuf, key);
145 }
146
147 static void fetch_mac_string_vpd(struct drivers_net_config *config, u8 *macstrbuf)
148 {
149 if (!config)
150 return;
151
152 /* Current implementation is up to 10 NIC cards */
153 if (config->device_index > MAX_DEVICE_SUPPORT) {
154 printk(BIOS_ERR, "r8168: the maximum device_index should be less then %d\n."
155 " Using default 00:e0:4c:00:c0:b0\n", MAX_DEVICE_SUPPORT);
156 return;
157 }
158
159 if (fetch_mac_vpd_dev_idx(macstrbuf, config->device_index) == CB_SUCCESS)
160 return;
161
162 if (!CONFIG(RT8168_SUPPORT_LEGACY_VPD_MAC)) {
163 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
164 " using default 00:e0:4c:00:c0:b0\n");
165 return;
166 }
167
168 if (fetch_mac_vpd_key(macstrbuf, "ethernet_mac") != CB_SUCCESS)
169 printk(BIOS_ERR, "r8168: mac address not found in VPD,"
170 " using default 00:e0:4c:00:c0:b0\n");
171 }
172
173 static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
174 {
175 if (!cbfs_load("rt8168-macaddress", macstrbuf, MACLEN)) {
176 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS\n");
177 return CB_ERR;
178 }
179 return CB_SUCCESS;
180 }
181
182 static void get_mac_address(u8 *macaddr, const u8 *strbuf)
183 {
184 size_t offset = 0;
185 int i;
186
187 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
188 (strbuf[8] != ':') || (strbuf[11] != ':') ||
189 (strbuf[14] != ':')) {
190 printk(BIOS_ERR, "r8168: ignore invalid MAC address in cbfs\n");
191 return;
192 }
193
194 for (i = 0; i < 6; i++) {
195 macaddr[i] = 0;
196 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
197 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
198 offset += 3;
199 }
200 }
201
202 static void program_mac_address(struct device *dev, u16 io_base)
203 {
204 u8 macstrbuf[MACLEN] = { 0 };
205 u32 maclo, machi;
206 int i = 0;
207 /* Default MAC Address of 00:E0:4C:00:C0:B0 */
208 u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
209 struct drivers_net_config *config = dev->chip_info;
210
211 /* check the VPD for the mac address */
212 if (CONFIG(RT8168_GET_MAC_FROM_VPD)) {
213 fetch_mac_string_vpd(config, macstrbuf);
214 } else {
215 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS)
216 printk(BIOS_ERR, "r8168: Error reading MAC from CBFS,"
217 " using default 00:e0:4c:00:c0:b0\n");
218 }
219 get_mac_address(mac, macstrbuf);
220
221 /* Reset NIC */
222 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
223 outb(CMD_REG_RESET, io_base + CMD_REG);
224
225 /* Poll for reset, with 1sec timeout */
226 while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) {
227 udelay(1000);
228 if (++i >= NIC_TIMEOUT)
229 printk(BIOS_ERR, "timeout waiting for nic to reset\n");
230 }
231 if (i < NIC_TIMEOUT)
232 printk(BIOS_DEBUG, "done\n");
233
234 printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
235
236 /* Disable register protection */
237 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
238
239 /* Set MAC address: only 4-byte write accesses allowed */
240 maclo = mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24;
241 machi = mac[4] | mac[5] << 8;
242 outl(machi, io_base + 4);
243 inl(io_base + 4);
244 outl(maclo, io_base);
245 inl(io_base);
246 /* Some boards (e.g. asus/p8z77-v_le_plus) need the MAC address set here too */
247 if (CONFIG(RT8168_PUT_MAC_TO_ERI)) {
248 switch (pci_read_config8(dev, PCI_REVISION_ID)) {
249 case 6:
250 outl((maclo & 0xffff) << 16, io_base + ERIDR);
251 inl(io_base + ERIDR);
252 outl(0x8000f0f0, io_base + ERIAR);
253 inl(io_base + ERIAR);
254 outl((machi << 16 | maclo >> 16), io_base + ERIDR);
255 inl(io_base + ERIDR);
256 outl(0x8000f0f4, io_base + ERIAR);
257 break;
258 case 9:
259 outl(maclo, io_base + ERIDR);
260 inl(io_base + ERIDR);
261 outl(0x8000f0e0, io_base + ERIAR);
262 inl(io_base + ERIAR);
263 outl(machi, io_base + ERIDR);
264 inl(io_base + ERIDR);
265 outl(0x800030e4, io_base + ERIAR);
266 break;
267 }
268 udelay(1000);
269 }
270 /* Lock config regs */
271 outb(CFG_9346_LOCK, io_base + CFG_9346);
272
273 printk(BIOS_DEBUG, "done\n");
274 }
275
276 static void enable_aspm_l1_2(u16 io_base)
277 {
278 printk(BIOS_INFO, "rtl: Enable ASPM L1.2\n");
279
280 /* Disable register protection */
281 outb(CFG_9346_UNLOCK, io_base + CFG_9346);
282
283 /* Enable ASPM_L1.2 */
284 outl(ASPM_L1_2_MASK, io_base + CMD_REG_ASPM);
285
286 /* Lock config regs */
287 outb(CFG_9346_LOCK, io_base + CFG_9346);
288 }
289
290 static void r8168_set_customized_led(struct device *dev, u16 io_base)
291 {
292 struct drivers_net_config *config = dev->chip_info;
293
294 if (!config)
295 return;
296
297 if (dev->device == PCI_DID_REALTEK_8125) {
298 /* Set LED global Feature register */
299 outb(config->led_feature, io_base + CMD_LED_FEATURE);
300 printk(BIOS_DEBUG, "r8125: read back LED global feature setting as 0x%x\n",
301 inb(io_base + CMD_LED_FEATURE));
302
303 /*
304 * Refer to RTL8125 datasheet 5.Customizable LED Configuration
305 * Register Name IO Address
306 * LEDSEL0 0x18
307 * LEDSEL2 0x84
308 * LEDFEATURE 0x94
309 *
310 * LEDSEL Bit[] Description
311 * Bit0 Link10M
312 * Bit1 Link100M
313 * Bit3 Link1000M
314 * Bit5 Link2.5G
315 * Bit9 ACT
316 * Bit10 preboot enable
317 * Bit11 lp enable
318 * Bit12 active low/high
319 *
320 * LEDFEATURE Description
321 * Bit0 LED Table V1/V2
322 * Bit1~3 Reserved
323 * Bit4~5 LED Blinking Duty Cycle 12.5%/ 25%/ 50%/ 75%
324 * Bit6~7 LED Blinking Freq. 240ms/160ms/80ms/Link-Speed-Dependent
325 */
326
327 /* Set customized LED0 register */
328 outw(config->customized_led0, io_base + CMD_LEDSEL0);
329 printk(BIOS_DEBUG, "r8125: read back LED0 setting as 0x%x\n",
330 inw(io_base + CMD_LEDSEL0));
331
332 /* Set customized LED2 register */
333 outw(config->customized_led2, io_base + CMD_LEDSEL2);
334 printk(BIOS_DEBUG, "r8125: read back LED2 setting as 0x%x\n",
335 inw(io_base + CMD_LEDSEL2));
336 } else {
337 /* Read the customized LED setting from devicetree */
338 printk(BIOS_DEBUG, "r8168: Customized LED 0x%x\n", config->customized_leds);
339
340 /*
341 * Refer to RTL8111H datasheet 7.2 Customizable LED Configuration
342 * Starting from offset 0x18
343 * Bit[15:12] LED Feature Control(FC)
344 * Bit[11:08] LED Select for PINLED2
345 * Bit[07:04] LED Select for PINLED1
346 * Bit[03:00] LED Select for PINLED0
347 *
348 * Speed Link10M Link100M Link1000M ACT/Full
349 * LED0 Bit0 Bit1 Bit2 Bit3
350 * LED1 Bit4 Bit5 Bit6 Bit7
351 * LED2 Bit8 Bit9 Bit10 Bit11
352 * FC Bit12 Bit13 Bit14 Bit15
353 */
354
355 /* Set customized LED registers */
356 outw(config->customized_leds, io_base + CMD_LED0_LED1);
357 printk(BIOS_DEBUG, "r8168: read back LED setting as 0x%x\n",
358 inw(io_base + CMD_LED0_LED1));
359 }
360 }
361
362 static void r8168_init(struct device *dev)
363 {
364 /* Get the resource of the NIC mmio */
365 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
366 u16 io_base = (u16)nic_res->base;
367
368 /* Check if the base is invalid */
369 if (!io_base) {
370 printk(BIOS_ERR, "r8168: Error can't find IO resource\n");
371 return;
372 }
373 /* Enable but do not set bus master */
374 pci_write_config16(dev, PCI_COMMAND,
375 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
376
377 /* Program MAC address based on CBFS "macaddress" containing
378 * a string AA:BB:CC:DD:EE:FF */
379 program_mac_address(dev, io_base);
380
381 /* Program customized LED mode */
382 if (CONFIG(RT8168_SET_LED_MODE))
383 r8168_set_customized_led(dev, io_base);
384
385 struct drivers_net_config *config = dev->chip_info;
386 if (CONFIG(PCIEXP_ASPM) && config->enable_aspm_l1_2)
387 enable_aspm_l1_2(io_base);
388 }
389
390 #if CONFIG(HAVE_ACPI_TABLES)
391 #define R8168_ACPI_HID "R8168"
392 static void r8168_net_fill_ssdt(const struct device *dev)
393 {
394 struct drivers_net_config *config = dev->chip_info;
395 const char *path = acpi_device_path(dev->upstream->dev);
396 u32 address;
397
398 if (!path || !config)
399 return;
400
401 /* Device */
402 acpigen_write_scope(path);
403 acpigen_write_device(acpi_device_name(dev));
404 acpigen_write_name_string("_HID", R8168_ACPI_HID);
405 acpi_device_write_uid(dev);
406
407 if (dev->chip_ops)
408 acpigen_write_name_string("_DDN", dev->chip_ops->name);
409 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
410
411 /* Power Resource */
412 if (CONFIG(RT8168_GEN_ACPI_POWER_RESOURCE) && config->has_power_resource) {
413 const struct acpi_power_res_params power_res_params = {
414 .stop_gpio = &config->stop_gpio,
415 .stop_delay_ms = config->stop_delay_ms,
416 .stop_off_delay_ms = config->stop_off_delay_ms
417 };
418 acpi_device_add_power_res(&power_res_params);
419 }
420
421 /* Address */
422 address = PCI_SLOT(dev->path.pci.devfn) & 0xffff;
423 address <<= 16;
424 address |= PCI_FUNC(dev->path.pci.devfn) & 0xffff;
425 acpigen_write_name_dword("_ADR", address);
426
427 /* Wake capabilities */
428 if (config->wake)
429 acpigen_write_PRW(config->wake, 3);
430
431 if (config->add_acpi_dma_property)
432 acpi_device_add_dma_property(NULL);
433
434 acpigen_pop_len(); /* Device */
435 acpigen_pop_len(); /* Scope */
436
437 printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
438 dev->chip_ops ? dev->chip_ops->name : "", dev_path(dev));
439 }
440
441 static const char *r8168_net_acpi_name(const struct device *dev)
442 {
443 return "RLTK";
444 }
445 #endif
446
447 static struct device_operations r8168_ops = {
448 .read_resources = pci_dev_read_resources,
449 .set_resources = pci_dev_set_resources,
450 .enable_resources = pci_dev_enable_resources,
451 .init = r8168_init,
452 #if CONFIG(HAVE_ACPI_TABLES)
453 .acpi_name = r8168_net_acpi_name,
454 .acpi_fill_ssdt = r8168_net_fill_ssdt,
455 #endif
456 };
457
458 static const unsigned short pci_device_ids[] = {
459 PCI_DID_REALTEK_8168,
460 PCI_DID_REALTEK_8125,
461 PCI_DID_REALTEK_8111,
462 0
463 };
464
465 static const struct pci_driver r8168_driver __pci_driver = {
466 .ops = &r8168_ops,
467 .vendor = PCI_VID_REALTEK,
468 .devices = pci_device_ids,
469 };
470
471 struct chip_operations drivers_net_ops = {
472 .name = "Realtek r8168",
473 };