[coreboot-gerrit] Change in coreboot[master]: drivers/net/atl1e: Add driver

Arthur Heymans (Code Review) gerrit at coreboot.org
Wed Aug 22 08:56:14 CEST 2018


Arthur Heymans has uploaded this change for review. ( https://review.coreboot.org/28265


Change subject: drivers/net/atl1e: Add driver
......................................................................

drivers/net/atl1e: Add driver

Change-Id: I5c32df00e25453c350a45e7f1ee6834b89c4289f
Signed-off-by: Arthur Heymans <arthur at aheymans.xyz>
---
M src/drivers/net/Kconfig
M src/drivers/net/Makefile.inc
A src/drivers/net/atl1e.c
M src/mainboard/asus/p5qc/Kconfig
4 files changed, 196 insertions(+), 0 deletions(-)



  git pull ssh://review.coreboot.org:29418/coreboot refs/changes/65/28265/1

diff --git a/src/drivers/net/Kconfig b/src/drivers/net/Kconfig
index bd6f09a..83d88c7 100644
--- a/src/drivers/net/Kconfig
+++ b/src/drivers/net/Kconfig
@@ -29,3 +29,19 @@
 	  Please refer to RTL811x datasheet section 7.2 Customizable LED
 	  Configuration for details. With this flag enabled, the
 	  customized_leds variable will be read from devicetree setting.
+
+config ATHEROS_ATL1E_SETMAC
+	bool
+	help
+	  This sets the MAC address on boards featuring the atheros 1968:1026
+	  card which lack an eeprom to store it.
+
+config ATHEROS_ATL1E_MACADDRESS
+	string "Atheros AR8121/AR8113/AR8114 mac address"
+	depends on ATHEROS_ATL1E_SETMAC
+	default "90:e6:ba:24:f9:d2"
+	help
+	  This is a string to set the mac address on an Atheros atl1e card.
+	  It must be in the form  of "xx:xx:xx:xx:xx:xx", where x is a
+	  hexadecimal number for it to be valid. Failing to do so will
+	  result in the default macaddress being used.
diff --git a/src/drivers/net/Makefile.inc b/src/drivers/net/Makefile.inc
index 20dbe50..33c8211 100644
--- a/src/drivers/net/Makefile.inc
+++ b/src/drivers/net/Makefile.inc
@@ -1,6 +1,7 @@
 romstage-$(CONFIG_CONSOLE_NE2K) += ne2k.c
 ramstage-$(CONFIG_CONSOLE_NE2K) += ne2k.c
 ramstage-$(CONFIG_REALTEK_8168_RESET) += r8168.c
+ramstage-$(CONFIG_ATHEROS_ATL1E_SETMAC) += atl1e.c
 
 ifneq ($(CONFIG_REALTEK_8168_MACADDRESS),"")
 $(obj)/rt8168-macaddress: $(DOTCONFIG)
@@ -11,3 +12,13 @@
 rt8168-macaddress-file := $(obj)/rt8168-macaddress
 rt8168-macaddress-type := raw
 endif
+
+ifneq ($(CONFIG_ATHEROS_ATL1E_MACADDRESS),"")
+$(obj)/atl1e-macaddress: $(DOTCONFIG)
+	echo "    Creating a file holding the atl1e macaddress"
+	printf %s $(CONFIG_ATHEROS_ATL1E_MACADDRESS) > $@
+
+cbfs-files-$(CONFIG_ATHEROS_ATL1E_SETMAC) += atl1e-macaddress
+atl1e-macaddress-file := $(obj)/atl1e-macaddress
+atl1e-macaddress-type := raw
+endif
diff --git a/src/drivers/net/atl1e.c b/src/drivers/net/atl1e.c
new file mode 100644
index 0000000..65fc511
--- /dev/null
+++ b/src/drivers/net/atl1e.c
@@ -0,0 +1,168 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2007 Atheros Corporation. All rights reserved.
+ * Copyright (C) 2012 Google Inc.
+ * Copyright (C) 2016 Damien Zammit <damien at zamaudio.com>
+ * Copyright (C) 2018 Arthur Heymans <arthur at aheymans.xyz>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * This driver sets the macaddress of a Atheros AR8121/AR8113/AR8114
+ */
+
+#include <device/device.h>
+#include <cbfs.h>
+#include <string.h>
+#include <console/console.h>
+#include <device/pci.h>
+
+#define REG_SPI_FLASH_CTRL		0x200
+#define SPI_FLASH_CTRL_EN_VPD		0x2000
+
+#define REG_PCIE_CAP_LIST		0x58
+
+#define REG_MAC_STA_ADDR	0x1488
+
+static u8 get_hex_digit(const u8 c)
+{
+	u8 ret = 0;
+
+	ret = c - '0';
+	if (ret > 0x09) {
+		ret = c - 'A' + 0x0a;
+		if (ret > 0x0f)
+			ret = c - 'a' + 0x0a;
+	}
+	if (ret > 0x0f) {
+		printk(BIOS_ERR, "Error: Invalid hex digit found: "
+				 "%c - 0x%02x\n", (char)c, c);
+		ret = 0;
+	}
+	return ret;
+}
+
+#define MACLEN 17
+
+static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
+{
+	struct cbfsf fh;
+	uint32_t matchraw = CBFS_TYPE_RAW;
+
+	if (!cbfs_boot_locate(&fh, "atl1e-macaddress", &matchraw)) {
+		/* check the cbfs for the mac address */
+		if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
+			printk(BIOS_ERR, "atl1e: Error reading MAC from CBFS\n");
+			return CB_ERR;
+		}
+		return CB_SUCCESS;
+	}
+	return CB_ERR;
+}
+
+static void get_mac_address(u8 *macaddr, const u8 *strbuf)
+{
+	size_t offset = 0;
+	int i;
+
+	if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
+	    (strbuf[8] != ':') || (strbuf[11] != ':') ||
+	    (strbuf[14] != ':')) {
+		printk(BIOS_ERR, "atl1e: ignore invalid MAC address in cbfs\n");
+		return;
+	}
+
+	for (i = 0; i < 6; i++) {
+		macaddr[i] = 0;
+		macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
+		macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
+		offset += 3;
+	}
+}
+
+static void program_mac_address(struct device *dev, u32 mem_base)
+{
+	u8 macstrbuf[MACLEN] = { 0 };
+	/* Default MAC Address of 90:e6:ba:24:f9:d2 */
+	u8 mac[6] = { 0x90, 0xe6, 0xba, 0x24, 0xf9, 0xd2 };
+	u32 value;
+
+	if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS) {
+		printk(BIOS_ERR, "atl1e: Error reading MAC from CBFS,"
+		       " using default 90:e6:ba:24:f9:d2\n");
+	} else {
+		get_mac_address(mac, macstrbuf);
+	}
+
+	printk(BIOS_DEBUG, "atl1e: Programming MAC Address...");
+
+	value = (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | (mac[5] << 0);
+	write32((void *)mem_base + REG_MAC_STA_ADDR, value);
+	value = (mac[0] << 8) | (mac[1] << 0);
+	write32((void *)mem_base + REG_MAC_STA_ADDR + 4, value);
+
+	printk(BIOS_DEBUG, "done\n");
+}
+
+static int atl1e_eeprom_exist(u32 mem_base)
+{
+	u32 value = read32((void *)mem_base + REG_SPI_FLASH_CTRL);
+	if (value & SPI_FLASH_CTRL_EN_VPD) {
+		value &= ~SPI_FLASH_CTRL_EN_VPD;
+		write32((void *)mem_base + REG_SPI_FLASH_CTRL, value);
+	}
+	value = read32((void *)mem_base + REG_PCIE_CAP_LIST);
+	return ((value & 0xff00) == 0x6c00) ? 1 : 0;
+}
+
+static void atl1e_init(struct device *dev)
+{
+	/* Get the resource of the NIC mmio */
+	struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
+	u32 mem_base = nic_res->base;
+
+	if(atl1e_eeprom_exist(mem_base)) {
+		printk(BIOS_INFO, "atl1e NIC has SPI eeprom, not setting MAC\n");
+		return;
+	}
+
+	/* Check if the base is invalid */
+	if (!mem_base) {
+		printk(BIOS_ERR, "atl1e: Error cant find MEM resource\n");
+		return;
+	}
+	/* Enable but do not set bus master */
+	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 */
+	program_mac_address(dev, mem_base);
+}
+
+static struct device_operations atl1e_ops  = {
+	.read_resources   = pci_dev_read_resources,
+	.set_resources    = pci_dev_set_resources,
+	.enable_resources = pci_dev_enable_resources,
+	.init             = atl1e_init,
+	.scan_bus         = 0,
+};
+
+static const struct pci_driver atl1e_driver __pci_driver = {
+	.ops    = &atl1e_ops,
+	.vendor = 0x1969,
+	.device = 0x1026,
+};
+
+struct chip_operations drivers_net_ops = {
+	CHIP_NAME("Atheros AR8121/AR8113/AR8114")
+};
diff --git a/src/mainboard/asus/p5qc/Kconfig b/src/mainboard/asus/p5qc/Kconfig
index 605dfd7..cf71b64 100644
--- a/src/mainboard/asus/p5qc/Kconfig
+++ b/src/mainboard/asus/p5qc/Kconfig
@@ -28,6 +28,7 @@
 	select HAVE_OPTION_TABLE
 	select HAVE_CMOS_DEFAULT
 	select HAVE_ACPI_RESUME
+	select ATHEROS_ATL1E_SETMAC
 
 config MAINBOARD_DIR
 	string

-- 
To view, visit https://review.coreboot.org/28265
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I5c32df00e25453c350a45e7f1ee6834b89c4289f
Gerrit-Change-Number: 28265
Gerrit-PatchSet: 1
Gerrit-Owner: Arthur Heymans <arthur at aheymans.xyz>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.coreboot.org/pipermail/coreboot-gerrit/attachments/20180822/7fbd9e3a/attachment-0001.html>


More information about the coreboot-gerrit mailing list