blob: 87df672c670cd8a34ce095422ba4a04ed5d93a26 [file] [log] [blame]
Matt DeVilliera87fcab2015-10-20 19:36:09 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2015 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <cbfs.h>
17#include <string.h>
18#include <types.h>
19#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020020#include <device/pci_ops.h>
Matt DeVilliera87fcab2015-10-20 19:36:09 -050021#include <console/console.h>
22#include <device/device.h>
23#include <device/pci.h>
24#include <fmap.h>
Matt DeVillierce0a5642017-01-12 12:19:21 -060025#include <variant/onboard.h>
Matt DeVilliera87fcab2015-10-20 19:36:09 -050026
27static unsigned int search(char *p, u8 *a, unsigned int lengthp,
28 unsigned int lengtha)
29{
30 int i, j;
31
32 /* Searching */
33 for (j = 0; j <= lengtha - lengthp; j++) {
34 for (i = 0; i < lengthp && p[i] == a[i + j]; i++)
35 ;
36 if (i >= lengthp)
37 return j;
38 }
39 return lengtha;
40}
41
42static unsigned char get_hex_digit(u8 *offset)
43{
44 unsigned char retval = 0;
45
46 retval = *offset - '0';
47 if (retval > 0x09) {
48 retval = *offset - 'A' + 0x0A;
49 if (retval > 0x0F)
50 retval = *offset - 'a' + 0x0a;
51 }
52 if (retval > 0x0F) {
53 printk(BIOS_DEBUG, "Error: Invalid Hex digit found: %c - 0x%02x\n",
54 *offset, *offset);
55 retval = 0;
56 }
57
58 return retval;
59}
60
61static int get_mac_address(u32 *high_dword, u32 *low_dword,
62 u8 *search_address, u32 search_length)
63{
64 char key[] = "ethernet_mac";
65 unsigned int offset;
66 int i;
67
68 offset = search(key, search_address, sizeof(key) - 1, search_length);
69 if (offset == search_length) {
70 printk(BIOS_DEBUG,
71 "Error: Could not locate '%s' in VPD\n", key);
72 return 0;
73 }
74 printk(BIOS_DEBUG, "Located '%s' in VPD\n", key);
75
76 offset += sizeof(key); /* move to next character */
77 *high_dword = 0;
78
79 /* Fetch the MAC address and put the octets in the correct order to
80 * be programmed.
81 *
82 * From RTL8105E_Series_EEPROM-Less_App_Note_1.1
83 * If the MAC address is 001122334455h:
84 * Write 33221100h to I/O register offset 0x00 via double word access
85 * Write 00005544h to I/O register offset 0x04 via double word access
86 */
87
88 for (i = 0; i < 4; i++) {
89 *high_dword |= (get_hex_digit(search_address + offset)
90 << (4 + (i * 8)));
91 *high_dword |= (get_hex_digit(search_address + offset + 1)
92 << (i * 8));
93 offset += 3;
94 }
95
96 *low_dword = 0;
97 for (i = 0; i < 2; i++) {
98 *low_dword |= (get_hex_digit(search_address + offset)
99 << (4 + (i * 8)));
100 *low_dword |= (get_hex_digit(search_address + offset + 1)
101 << (i * 8));
102 offset += 3;
103 }
104
105 return *high_dword | *low_dword;
106}
107
108static void program_mac_address(u16 io_base)
109{
110 void *search_address = NULL;
111 size_t search_length = -1;
112
113 /* Default MAC Address of A0:00:BA:D0:0B:AD */
114 u32 high_dword = 0xD0BA00A0; /* high dword of mac address */
115 u32 low_dword = 0x0000AD0B; /* low word of mac address as a dword */
116
117 if (IS_ENABLED(CONFIG_CHROMEOS)) {
118 struct region_device rdev;
119
120 if (fmap_locate_area_as_rdev("RO_VPD", &rdev) == 0) {
121 search_address = rdev_mmap_full(&rdev);
122
123 if (search_address != NULL)
124 search_length = region_device_sz(&rdev);
125 }
126 } else {
127 search_address = cbfs_boot_map_with_leak("vpd.bin",
128 CBFS_TYPE_RAW,
129 &search_length);
130 }
131
132 if (search_address == NULL)
133 printk(BIOS_ERR, "LAN: VPD not found.\n");
134 else
135 get_mac_address(&high_dword, &low_dword, search_address,
136 search_length);
137
138 if (io_base) {
139 printk(BIOS_DEBUG, "Realtek NIC io_base = 0x%04x\n", io_base);
140 printk(BIOS_DEBUG, "Programming MAC Address\n");
141
142 /* Disable register protection */
143 outb(0xc0, io_base + 0x50);
144 outl(high_dword, io_base);
145 outl(low_dword, io_base + 0x04);
146 outb(0x60, io_base + 54);
147 /* Enable register protection again */
148 outb(0x00, io_base + 0x50);
149 }
150}
151
152void lan_init(void)
153{
154 u16 io_base = 0;
155 struct device *ethernet_dev = NULL;
156
157 /* Get NIC's IO base address */
158 ethernet_dev = dev_find_device(NINJA_NIC_VENDOR_ID,
159 NINJA_NIC_DEVICE_ID, 0);
160 if (ethernet_dev != NULL) {
161 io_base = pci_read_config16(ethernet_dev, 0x10) & 0xfffe;
162
163 /*
164 * Battery life time - LAN PCIe should enter ASPM L1 to save
165 * power when LAN connection is idle.
166 * enable CLKREQ: LAN pci config space 0x81h=01
167 */
168 pci_write_config8(ethernet_dev, 0x81, 0x01);
169 }
170
171 if (io_base) {
172 /* Program MAC address based on VPD data */
173 program_mac_address(io_base);
174
175 /*
176 * Program NIC LEDS
177 *
178 * RTL8105E Series EEPROM-Less Application Note,
179 * Section 5.6 LED Mode Configuration
180 *
181 * Step1: Write C0h to I/O register 0x50 via byte access to
182 * disable 'register protection'
183 * Step2: Write xx001111b to I/O register 0x52 via byte access
184 * (bit7 is LEDS1 and bit6 is LEDS0)
185 * Step3: Write 0x00 to I/O register 0x50 via byte access to
186 * enable 'register protection'
187 */
188 outb(0xc0, io_base + 0x50); /* Disable protection */
189 outb((NINJA_NIC_LED_MODE << 6) | 0x0f, io_base + 0x52);
190 outb(0x00, io_base + 0x50); /* Enable register protection */
191 }
192}