1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * This driver sets the MAC address of an Atheros AR8121/AR8113/AR8114
7 #include <device/mmio.h>
8 #include <device/device.h>
10 #include <console/console.h>
11 #include <device/pci.h>
12 #include <device/pci_ops.h>
15 #define REG_SPI_FLASH_CTRL 0x200
16 #define SPI_FLASH_CTRL_EN_VPD 0x2000
18 #define REG_PCIE_CAP_LIST 0x58
20 #define REG_MAC_STA_ADDR 0x1488
22 static u8
get_hex_digit(const u8 c
)
33 printk(BIOS_ERR
, "Invalid hex digit found: "
34 "%c - 0x%02x\n", (char)c
, c
);
42 static enum cb_err
fetch_mac_string_cbfs(u8
*macstrbuf
)
44 if (!cbfs_load("atl1e-macaddress", macstrbuf
, MACLEN
)) {
45 printk(BIOS_ERR
, "atl1e: Error reading MAC from CBFS\n");
51 static void get_mac_address(u8
*macaddr
, const u8
*strbuf
)
56 if ((strbuf
[2] != ':') || (strbuf
[5] != ':') ||
57 (strbuf
[8] != ':') || (strbuf
[11] != ':') ||
58 (strbuf
[14] != ':')) {
59 printk(BIOS_ERR
, "atl1e: ignore invalid MAC address in cbfs\n");
63 for (i
= 0; i
< 6; i
++) {
65 macaddr
[i
] |= get_hex_digit(strbuf
[offset
]) << 4;
66 macaddr
[i
] |= get_hex_digit(strbuf
[offset
+ 1]);
71 static void program_mac_address(u32 mem_base
)
73 u8 macstrbuf
[MACLEN
] = { 0 };
74 /* Default MAC Address of 90:e6:ba:24:f9:d2 */
75 u8 mac
[6] = { 0x90, 0xe6, 0xba, 0x24, 0xf9, 0xd2 };
78 if (fetch_mac_string_cbfs(macstrbuf
) != CB_SUCCESS
) {
79 printk(BIOS_ERR
, "atl1e: Error reading MAC from CBFS,"
80 " using default 90:e6:ba:24:f9:d2\n");
82 get_mac_address(mac
, macstrbuf
);
85 printk(BIOS_DEBUG
, "atl1e: Programming MAC Address...");
87 value
= (mac
[2] << 24) | (mac
[3] << 16) | (mac
[4] << 8) | (mac
[5] << 0);
88 write32p(mem_base
+ REG_MAC_STA_ADDR
, value
);
89 value
= (mac
[0] << 8) | (mac
[1] << 0);
90 write32p(mem_base
+ REG_MAC_STA_ADDR
+ 4, value
);
92 printk(BIOS_DEBUG
, "done\n");
95 static int atl1e_eeprom_exist(u32 mem_base
)
97 u32 value
= read32p(mem_base
+ REG_SPI_FLASH_CTRL
);
98 if (value
& SPI_FLASH_CTRL_EN_VPD
) {
99 value
&= ~SPI_FLASH_CTRL_EN_VPD
;
100 write32p(mem_base
+ REG_SPI_FLASH_CTRL
, value
);
102 value
= read32p(mem_base
+ REG_PCIE_CAP_LIST
);
103 return ((value
& 0xff00) == 0x6c00) ? 1 : 0;
106 static void atl1e_init(struct device
*dev
)
108 /* Get the resource of the NIC mmio */
109 struct resource
*nic_res
= probe_resource(dev
, PCI_BASE_ADDRESS_0
);
111 if (nic_res
== NULL
) {
112 printk(BIOS_ERR
, "atl1e: resource not found\n");
116 u32 mem_base
= nic_res
->base
;
119 printk(BIOS_ERR
, "atl1e: resource not assigned\n");
123 if (atl1e_eeprom_exist(mem_base
)) {
124 printk(BIOS_INFO
, "atl1e NIC has SPI eeprom, not setting MAC\n");
128 /* Check if the base is invalid */
130 printk(BIOS_ERR
, "atl1e: Error can't find MEM resource\n");
133 /* Enable but do not set bus master */
134 pci_write_config16(dev
, PCI_COMMAND
,
135 PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
);
137 /* Program MAC address based on CBFS "macaddress" containing
138 * a string AA:BB:CC:DD:EE:FF */
139 program_mac_address(mem_base
);
142 static struct device_operations atl1e_ops
= {
143 .read_resources
= pci_dev_read_resources
,
144 .set_resources
= pci_dev_set_resources
,
145 .enable_resources
= pci_dev_enable_resources
,
149 static const struct pci_driver atl1e_driver __pci_driver
= {
155 struct chip_operations drivers_net_ops
= {
156 .name
= "Atheros AR8121/AR8113/AR8114",