blob: fe85b8348cdf95cf125b4693e5781d12ffed0b1a [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Arthur Heymans7f922b02018-08-22 02:14:04 +02002
3/*
Martin Roth3e25f852023-09-04 15:37:07 -06004 * This driver sets the MAC address of an Atheros AR8121/AR8113/AR8114
Arthur Heymans7f922b02018-08-22 02:14:04 +02005 */
6
Kyösti Mälkki13f66502019-03-03 08:01:05 +02007#include <device/mmio.h>
Arthur Heymans7f922b02018-08-22 02:14:04 +02008#include <device/device.h>
9#include <cbfs.h>
Arthur Heymans7f922b02018-08-22 02:14:04 +020010#include <console/console.h>
11#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020012#include <device/pci_ops.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020013#include <types.h>
Arthur Heymans7f922b02018-08-22 02:14:04 +020014
15#define REG_SPI_FLASH_CTRL 0x200
16#define SPI_FLASH_CTRL_EN_VPD 0x2000
17
18#define REG_PCIE_CAP_LIST 0x58
19
20#define REG_MAC_STA_ADDR 0x1488
21
22static u8 get_hex_digit(const u8 c)
23{
24 u8 ret = 0;
25
26 ret = c - '0';
27 if (ret > 0x09) {
28 ret = c - 'A' + 0x0a;
29 if (ret > 0x0f)
30 ret = c - 'a' + 0x0a;
31 }
32 if (ret > 0x0f) {
Julius Wernere9665952022-01-21 17:06:20 -080033 printk(BIOS_ERR, "Invalid hex digit found: "
Arthur Heymans7f922b02018-08-22 02:14:04 +020034 "%c - 0x%02x\n", (char)c, c);
35 ret = 0;
36 }
37 return ret;
38}
39
40#define MACLEN 17
41
42static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
43{
Julius Werner77639e42021-02-05 16:51:25 -080044 if (!cbfs_load("atl1e-macaddress", macstrbuf, MACLEN)) {
45 printk(BIOS_ERR, "atl1e: Error reading MAC from CBFS\n");
46 return CB_ERR;
Arthur Heymans7f922b02018-08-22 02:14:04 +020047 }
Julius Werner77639e42021-02-05 16:51:25 -080048 return CB_SUCCESS;
Arthur Heymans7f922b02018-08-22 02:14:04 +020049}
50
51static void get_mac_address(u8 *macaddr, const u8 *strbuf)
52{
53 size_t offset = 0;
54 int i;
55
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");
60 return;
61 }
62
63 for (i = 0; i < 6; i++) {
64 macaddr[i] = 0;
65 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
66 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
67 offset += 3;
68 }
69}
70
71static void program_mac_address(u32 mem_base)
72{
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 };
76 u32 value;
77
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");
81 } else {
82 get_mac_address(mac, macstrbuf);
83 }
84
85 printk(BIOS_DEBUG, "atl1e: Programming MAC Address...");
86
87 value = (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | (mac[5] << 0);
Elyes Haouas60803c12022-12-08 08:48:17 +010088 write32p(mem_base + REG_MAC_STA_ADDR, value);
Arthur Heymans7f922b02018-08-22 02:14:04 +020089 value = (mac[0] << 8) | (mac[1] << 0);
Elyes Haouas60803c12022-12-08 08:48:17 +010090 write32p(mem_base + REG_MAC_STA_ADDR + 4, value);
Arthur Heymans7f922b02018-08-22 02:14:04 +020091
92 printk(BIOS_DEBUG, "done\n");
93}
94
95static int atl1e_eeprom_exist(u32 mem_base)
96{
Elyes Haouas60803c12022-12-08 08:48:17 +010097 u32 value = read32p(mem_base + REG_SPI_FLASH_CTRL);
Arthur Heymans7f922b02018-08-22 02:14:04 +020098 if (value & SPI_FLASH_CTRL_EN_VPD) {
99 value &= ~SPI_FLASH_CTRL_EN_VPD;
Elyes Haouas60803c12022-12-08 08:48:17 +0100100 write32p(mem_base + REG_SPI_FLASH_CTRL, value);
Arthur Heymans7f922b02018-08-22 02:14:04 +0200101 }
Elyes Haouas60803c12022-12-08 08:48:17 +0100102 value = read32p(mem_base + REG_PCIE_CAP_LIST);
Arthur Heymans7f922b02018-08-22 02:14:04 +0200103 return ((value & 0xff00) == 0x6c00) ? 1 : 0;
104}
105
106static void atl1e_init(struct device *dev)
107{
108 /* Get the resource of the NIC mmio */
Angel Ponse0588412021-11-03 13:22:22 +0100109 struct resource *nic_res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Arthur Heymans7f922b02018-08-22 02:14:04 +0200110
111 if (nic_res == NULL) {
112 printk(BIOS_ERR, "atl1e: resource not found\n");
113 return;
114 }
115
116 u32 mem_base = nic_res->base;
117
118 if (!mem_base) {
119 printk(BIOS_ERR, "atl1e: resource not assigned\n");
120 return;
121 }
122
123 if (atl1e_eeprom_exist(mem_base)) {
124 printk(BIOS_INFO, "atl1e NIC has SPI eeprom, not setting MAC\n");
125 return;
126 }
127
128 /* Check if the base is invalid */
129 if (!mem_base) {
Martin Roth0949e732021-10-01 14:28:22 -0600130 printk(BIOS_ERR, "atl1e: Error can't find MEM resource\n");
Arthur Heymans7f922b02018-08-22 02:14:04 +0200131 return;
132 }
133 /* Enable but do not set bus master */
134 pci_write_config16(dev, PCI_COMMAND,
135 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
136
137 /* Program MAC address based on CBFS "macaddress" containing
138 * a string AA:BB:CC:DD:EE:FF */
139 program_mac_address(mem_base);
140}
141
142static 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,
146 .init = atl1e_init,
Arthur Heymans7f922b02018-08-22 02:14:04 +0200147};
148
149static const struct pci_driver atl1e_driver __pci_driver = {
150 .ops = &atl1e_ops,
151 .vendor = 0x1969,
152 .device = 0x1026,
153};
154
155struct chip_operations drivers_net_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900156 .name = "Atheros AR8121/AR8113/AR8114",
Arthur Heymans7f922b02018-08-22 02:14:04 +0200157};