blob: d6e7a841b100e3f714c792f7a65cdb7745445bf8 [file] [log] [blame]
Arthur Heymans7f922b02018-08-22 02:14:04 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2007 Atheros Corporation. All rights reserved.
5 * Copyright (C) 2012 Google Inc.
6 * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
7 * Copyright (C) 2018 Arthur Heymans <arthur@aheymans.xyz>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19/*
20 * This driver sets the macaddress of a Atheros AR8121/AR8113/AR8114
21 */
22
Kyösti Mälkki13f66502019-03-03 08:01:05 +020023#include <device/mmio.h>
Arthur Heymans7f922b02018-08-22 02:14:04 +020024#include <device/device.h>
25#include <cbfs.h>
26#include <string.h>
27#include <console/console.h>
28#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020029#include <device/pci_ops.h>
Arthur Heymans7f922b02018-08-22 02:14:04 +020030
31#define REG_SPI_FLASH_CTRL 0x200
32#define SPI_FLASH_CTRL_EN_VPD 0x2000
33
34#define REG_PCIE_CAP_LIST 0x58
35
36#define REG_MAC_STA_ADDR 0x1488
37
38static u8 get_hex_digit(const u8 c)
39{
40 u8 ret = 0;
41
42 ret = c - '0';
43 if (ret > 0x09) {
44 ret = c - 'A' + 0x0a;
45 if (ret > 0x0f)
46 ret = c - 'a' + 0x0a;
47 }
48 if (ret > 0x0f) {
49 printk(BIOS_ERR, "Error: Invalid hex digit found: "
50 "%c - 0x%02x\n", (char)c, c);
51 ret = 0;
52 }
53 return ret;
54}
55
56#define MACLEN 17
57
58static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
59{
60 struct cbfsf fh;
61 uint32_t matchraw = CBFS_TYPE_RAW;
62
63 if (!cbfs_boot_locate(&fh, "atl1e-macaddress", &matchraw)) {
64 /* check the cbfs for the mac address */
65 if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
66 printk(BIOS_ERR, "atl1e: Error reading MAC from CBFS\n");
67 return CB_ERR;
68 }
69 return CB_SUCCESS;
70 }
71 return CB_ERR;
72}
73
74static void get_mac_address(u8 *macaddr, const u8 *strbuf)
75{
76 size_t offset = 0;
77 int i;
78
79 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
80 (strbuf[8] != ':') || (strbuf[11] != ':') ||
81 (strbuf[14] != ':')) {
82 printk(BIOS_ERR, "atl1e: ignore invalid MAC address in cbfs\n");
83 return;
84 }
85
86 for (i = 0; i < 6; i++) {
87 macaddr[i] = 0;
88 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
89 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
90 offset += 3;
91 }
92}
93
94static void program_mac_address(u32 mem_base)
95{
96 u8 macstrbuf[MACLEN] = { 0 };
97 /* Default MAC Address of 90:e6:ba:24:f9:d2 */
98 u8 mac[6] = { 0x90, 0xe6, 0xba, 0x24, 0xf9, 0xd2 };
99 u32 value;
100
101 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS) {
102 printk(BIOS_ERR, "atl1e: Error reading MAC from CBFS,"
103 " using default 90:e6:ba:24:f9:d2\n");
104 } else {
105 get_mac_address(mac, macstrbuf);
106 }
107
108 printk(BIOS_DEBUG, "atl1e: Programming MAC Address...");
109
110 value = (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | (mac[5] << 0);
111 write32((void *)mem_base + REG_MAC_STA_ADDR, value);
112 value = (mac[0] << 8) | (mac[1] << 0);
113 write32((void *)mem_base + REG_MAC_STA_ADDR + 4, value);
114
115 printk(BIOS_DEBUG, "done\n");
116}
117
118static int atl1e_eeprom_exist(u32 mem_base)
119{
120 u32 value = read32((void *)mem_base + REG_SPI_FLASH_CTRL);
121 if (value & SPI_FLASH_CTRL_EN_VPD) {
122 value &= ~SPI_FLASH_CTRL_EN_VPD;
123 write32((void *)mem_base + REG_SPI_FLASH_CTRL, value);
124 }
125 value = read32((void *)mem_base + REG_PCIE_CAP_LIST);
126 return ((value & 0xff00) == 0x6c00) ? 1 : 0;
127}
128
129static void atl1e_init(struct device *dev)
130{
131 /* Get the resource of the NIC mmio */
132 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
133
134 if (nic_res == NULL) {
135 printk(BIOS_ERR, "atl1e: resource not found\n");
136 return;
137 }
138
139 u32 mem_base = nic_res->base;
140
141 if (!mem_base) {
142 printk(BIOS_ERR, "atl1e: resource not assigned\n");
143 return;
144 }
145
146 if (atl1e_eeprom_exist(mem_base)) {
147 printk(BIOS_INFO, "atl1e NIC has SPI eeprom, not setting MAC\n");
148 return;
149 }
150
151 /* Check if the base is invalid */
152 if (!mem_base) {
153 printk(BIOS_ERR, "atl1e: Error cant find MEM resource\n");
154 return;
155 }
156 /* Enable but do not set bus master */
157 pci_write_config16(dev, PCI_COMMAND,
158 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
159
160 /* Program MAC address based on CBFS "macaddress" containing
161 * a string AA:BB:CC:DD:EE:FF */
162 program_mac_address(mem_base);
163}
164
165static struct device_operations atl1e_ops = {
166 .read_resources = pci_dev_read_resources,
167 .set_resources = pci_dev_set_resources,
168 .enable_resources = pci_dev_enable_resources,
169 .init = atl1e_init,
170 .scan_bus = 0,
171};
172
173static const struct pci_driver atl1e_driver __pci_driver = {
174 .ops = &atl1e_ops,
175 .vendor = 0x1969,
176 .device = 0x1026,
177};
178
179struct chip_operations drivers_net_ops = {
180 CHIP_NAME("Atheros AR8121/AR8113/AR8114")
181};