Damien Zammit (damien@zamaudio.com) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/17672
-gerrit
commit f2bd9f182e88d1dfeadf93cd84b8782fb6a782b4 Author: Damien Zammit damien@zamaudio.com Date: Thu Dec 1 23:29:34 2016 +1100
drivers/r8168: Read default MAC address from CBFS
This driver applies to 10ec:8168
Previously, this driver resetted the nic and set a hardcoded MAC address. Now the driver reads a default MAC address from CBFS in the form of a string: echo -n "xx:xx:xx:xx:xx:xx" > macaddress and store the macaddress file in CBFS with the same name.
Change-Id: If1af91120fa3efca3f1406334a83ed1e59fbdaf9 Signed-off-by: Damien Zammit damien@zamaudio.com --- src/drivers/net/r8168.c | 115 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 88 insertions(+), 27 deletions(-)
diff --git a/src/drivers/net/r8168.c b/src/drivers/net/r8168.c index 1e6db57..4768b86 100644 --- a/src/drivers/net/r8168.c +++ b/src/drivers/net/r8168.c @@ -1,6 +1,7 @@ /* * This file is part of the coreboot project. * + * Copyright (C) 2012 Google Inc. * Copyright (C) 2016 Damien Zammit damien@zamaudio.com * * This program is free software; you can redistribute it and/or modify @@ -14,11 +15,14 @@ */
/* - * This driver forces the 10ec:8168 device to reset so that it goes - * into a proper power state, also programs a default MAC address - * so that if the EEPROM/efuse is unconfigured it still has a default MAC. + * This driver resets the 10ec:8168 NIC then tries to read + * "macaddress" string XX:XX:XX:XX:XX:XX from CBFS. + * If no MAC is found, it programs a default MAC address in the device + * so that if the EEPROM/efuse is unconfigured it still has a default MAC */
+#include <cbfs.h> +#include <string.h> #include <arch/io.h> #include <device/device.h> #include <device/pci.h> @@ -36,26 +40,64 @@ #define CFG_9346_LOCK 0x00 #define CFG_9346_UNLOCK 0xc0
-static void r8168_init(struct device *dev) +static unsigned char get_hex_digit(u8 *offset) { - u32 i; - const u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 }; + unsigned char retval = 0;
- /* Get the resource of the NIC mmio */ - struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0); - u16 nic_port = (u16)nic_res->base; + retval = *offset - '0'; + if (retval > 0x09) { + retval = *offset - 'A' + 0x0A; + if (retval > 0x0F) + retval = *offset - 'a' + 0x0a; + } + if (retval > 0x0F) { + printk(BIOS_DEBUG, "Error: Invalid Hex digit found: %c - 0x%02x\n", + *offset, *offset); + retval = 0; + }
- /* Ensble but do not set bus master. That's dangerous on a NIC. */ - pci_write_config16(dev, PCI_COMMAND, - PCI_COMMAND_MEMORY | PCI_COMMAND_IO); + return retval; +} + +static void get_mac_address(u8 *macaddr, u8 *cbfs_address) +{ + unsigned int offset = 0; + int i; + + for (i = 0; i < 6; i++) { + macaddr[i] = 0; + macaddr[i] |= (get_hex_digit(cbfs_address + offset) + << (4 + (i * 8))); + macaddr[i] |= (get_hex_digit(cbfs_address + offset + 1) + << (i * 8)); + offset += 3; + } +} + +static void program_mac_address(struct device *dev, u16 io_base) +{ + void *cbfs_address = NULL; + size_t search_length = -1; + int i = 0; + u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 }; + + cbfs_address = cbfs_boot_map_with_leak("macaddress", + CBFS_TYPE_RAW, + &search_length); + + if (cbfs_address == NULL) { + printk(BIOS_ERR, "r8168: `macaddress` not found in CBFS, " + "using default 00:e0:4c:00:c0:b0\n"); + } else { + get_mac_address(mac, cbfs_address); + }
/* Reset NIC */ printk(BIOS_DEBUG, "r8168: Resetting NIC..."); - outb(CMD_REG_RESET, nic_port + CMD_REG); + outb(CMD_REG_RESET, io_base + CMD_REG);
- i = 0; - /* Poll for reset, with 1s timeout */ - while (i < NIC_TIMEOUT && (inb(nic_port + CMD_REG) & CMD_REG_RESET)) { + /* Poll for reset, with 1sec timeout */ + while (i < NIC_TIMEOUT && (inb(io_base + CMD_REG) & CMD_REG_RESET)) { udelay(1000); if (++i >= NIC_TIMEOUT) printk(BIOS_DEBUG, "timeout waiting for nic to reset\n"); @@ -63,20 +105,39 @@ static void r8168_init(struct device *dev) if (i < NIC_TIMEOUT) printk(BIOS_DEBUG, "done\n");
- /* Unlock config regs */ - outb(CFG_9346_UNLOCK, nic_port + CFG_9346); - - /* Set MAC address 00:e0:4c:00:c0:b0 - * NB: only 4-byte write accesses allowed - */ - outl(mac[4] | mac[5] << 8, nic_port + 4); - inl(nic_port + 4); + printk(BIOS_DEBUG, "r8168: Programming MAC Address...");
- outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24, nic_port); - inl(nic_port); + /* Disable register protection */ + outb(CFG_9346_UNLOCK, io_base + CFG_9346);
+ /* Set MAC address: only 4-byte write accesses allowed */ + outl(mac[4] | mac[5] << 8, io_base + 4); + inl(io_base + 4); + outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24, + io_base); + inl(io_base); /* Lock config regs */ - outb(CFG_9346_LOCK, nic_port + CFG_9346); + outb(CFG_9346_LOCK, io_base + CFG_9346); + + printk(BIOS_DEBUG, "done\n"); +} + +static void r8168_init(struct device *dev) +{ + /* Get the resource of the NIC mmio */ + struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0); + u16 io_base = (u16)nic_res->base; + + /* Enable but do not set bus master. That's dangerous on a NIC. */ + pci_write_config16(dev, PCI_COMMAND, + PCI_COMMAND_MEMORY | PCI_COMMAND_IO); + + /* Program MAC address based on CBFS "macaddress" containing + * a string AA:BB:CC:DD:EE:FF */ + if (io_base) + program_mac_address(dev, io_base); + else + printk(BIOS_DEBUG, "r8168: Error cant find MMIO resource\n"); }
static struct device_operations r8168_ops = {