blob: 7e02e810b0827f57156ba45bc81794655988b724 [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
23#include <device/device.h>
24#include <cbfs.h>
25#include <string.h>
26#include <console/console.h>
27#include <device/pci.h>
28
29#define REG_SPI_FLASH_CTRL 0x200
30#define SPI_FLASH_CTRL_EN_VPD 0x2000
31
32#define REG_PCIE_CAP_LIST 0x58
33
34#define REG_MAC_STA_ADDR 0x1488
35
36static u8 get_hex_digit(const u8 c)
37{
38 u8 ret = 0;
39
40 ret = c - '0';
41 if (ret > 0x09) {
42 ret = c - 'A' + 0x0a;
43 if (ret > 0x0f)
44 ret = c - 'a' + 0x0a;
45 }
46 if (ret > 0x0f) {
47 printk(BIOS_ERR, "Error: Invalid hex digit found: "
48 "%c - 0x%02x\n", (char)c, c);
49 ret = 0;
50 }
51 return ret;
52}
53
54#define MACLEN 17
55
56static enum cb_err fetch_mac_string_cbfs(u8 *macstrbuf)
57{
58 struct cbfsf fh;
59 uint32_t matchraw = CBFS_TYPE_RAW;
60
61 if (!cbfs_boot_locate(&fh, "atl1e-macaddress", &matchraw)) {
62 /* check the cbfs for the mac address */
63 if (rdev_readat(&fh.data, macstrbuf, 0, MACLEN) != MACLEN) {
64 printk(BIOS_ERR, "atl1e: Error reading MAC from CBFS\n");
65 return CB_ERR;
66 }
67 return CB_SUCCESS;
68 }
69 return CB_ERR;
70}
71
72static void get_mac_address(u8 *macaddr, const u8 *strbuf)
73{
74 size_t offset = 0;
75 int i;
76
77 if ((strbuf[2] != ':') || (strbuf[5] != ':') ||
78 (strbuf[8] != ':') || (strbuf[11] != ':') ||
79 (strbuf[14] != ':')) {
80 printk(BIOS_ERR, "atl1e: ignore invalid MAC address in cbfs\n");
81 return;
82 }
83
84 for (i = 0; i < 6; i++) {
85 macaddr[i] = 0;
86 macaddr[i] |= get_hex_digit(strbuf[offset]) << 4;
87 macaddr[i] |= get_hex_digit(strbuf[offset + 1]);
88 offset += 3;
89 }
90}
91
92static void program_mac_address(u32 mem_base)
93{
94 u8 macstrbuf[MACLEN] = { 0 };
95 /* Default MAC Address of 90:e6:ba:24:f9:d2 */
96 u8 mac[6] = { 0x90, 0xe6, 0xba, 0x24, 0xf9, 0xd2 };
97 u32 value;
98
99 if (fetch_mac_string_cbfs(macstrbuf) != CB_SUCCESS) {
100 printk(BIOS_ERR, "atl1e: Error reading MAC from CBFS,"
101 " using default 90:e6:ba:24:f9:d2\n");
102 } else {
103 get_mac_address(mac, macstrbuf);
104 }
105
106 printk(BIOS_DEBUG, "atl1e: Programming MAC Address...");
107
108 value = (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | (mac[5] << 0);
109 write32((void *)mem_base + REG_MAC_STA_ADDR, value);
110 value = (mac[0] << 8) | (mac[1] << 0);
111 write32((void *)mem_base + REG_MAC_STA_ADDR + 4, value);
112
113 printk(BIOS_DEBUG, "done\n");
114}
115
116static int atl1e_eeprom_exist(u32 mem_base)
117{
118 u32 value = read32((void *)mem_base + REG_SPI_FLASH_CTRL);
119 if (value & SPI_FLASH_CTRL_EN_VPD) {
120 value &= ~SPI_FLASH_CTRL_EN_VPD;
121 write32((void *)mem_base + REG_SPI_FLASH_CTRL, value);
122 }
123 value = read32((void *)mem_base + REG_PCIE_CAP_LIST);
124 return ((value & 0xff00) == 0x6c00) ? 1 : 0;
125}
126
127static void atl1e_init(struct device *dev)
128{
129 /* Get the resource of the NIC mmio */
130 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
131
132 if (nic_res == NULL) {
133 printk(BIOS_ERR, "atl1e: resource not found\n");
134 return;
135 }
136
137 u32 mem_base = nic_res->base;
138
139 if (!mem_base) {
140 printk(BIOS_ERR, "atl1e: resource not assigned\n");
141 return;
142 }
143
144 if (atl1e_eeprom_exist(mem_base)) {
145 printk(BIOS_INFO, "atl1e NIC has SPI eeprom, not setting MAC\n");
146 return;
147 }
148
149 /* Check if the base is invalid */
150 if (!mem_base) {
151 printk(BIOS_ERR, "atl1e: Error cant find MEM resource\n");
152 return;
153 }
154 /* Enable but do not set bus master */
155 pci_write_config16(dev, PCI_COMMAND,
156 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
157
158 /* Program MAC address based on CBFS "macaddress" containing
159 * a string AA:BB:CC:DD:EE:FF */
160 program_mac_address(mem_base);
161}
162
163static struct device_operations atl1e_ops = {
164 .read_resources = pci_dev_read_resources,
165 .set_resources = pci_dev_set_resources,
166 .enable_resources = pci_dev_enable_resources,
167 .init = atl1e_init,
168 .scan_bus = 0,
169};
170
171static const struct pci_driver atl1e_driver __pci_driver = {
172 .ops = &atl1e_ops,
173 .vendor = 0x1969,
174 .device = 0x1026,
175};
176
177struct chip_operations drivers_net_ops = {
178 CHIP_NAME("Atheros AR8121/AR8113/AR8114")
179};