blob: 5af2502e6592d487ce94fbb4a909ee4fd0ad3101 [file] [log] [blame]
Angel Pons58c0d322020-04-05 13:20:46 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Matt DeVillier1b25f1b2018-09-14 21:39:00 -05002
3#include <cbfs.h>
4#include <fmap.h>
5#include <types.h>
Matt DeVillier1b25f1b2018-09-14 21:39:00 -05006#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02007#include <device/pci_ops.h>
Matt DeVillier1b25f1b2018-09-14 21:39:00 -05008#include <console/console.h>
9#include <device/device.h>
Matt DeVillier1b25f1b2018-09-14 21:39:00 -050010#include <smbios.h>
11#include <soc/pch.h>
12#include <variant/onboard.h>
13#include <mainboard/google/auron/variant.h>
14
Elyes HAOUAS38f1d132018-09-17 08:44:18 +020015int variant_smbios_data(struct device *dev, int *handle, unsigned long *current)
Matt DeVillier1b25f1b2018-09-14 21:39:00 -050016{
17 int len = 0;
18
19 len += smbios_write_type41(
20 current, handle,
21 BOARD_TOUCHSCREEN_NAME, /* name */
22 BOARD_TOUCHSCREEN_IRQ, /* instance */
23 BOARD_TOUCHSCREEN_I2C_BUS, /* segment */
24 BOARD_TOUCHSCREEN_I2C_ADDR, /* bus */
25 0, /* device */
Christian Waltere6afab12019-05-21 17:22:49 +020026 0, /* function */
27 SMBIOS_DEVICE_TYPE_OTHER); /* device type */
Matt DeVillier1b25f1b2018-09-14 21:39:00 -050028
29 return len;
30}
31
32static unsigned int search(char *p, u8 *a, unsigned int lengthp,
33 unsigned int lengtha)
34{
35 int i, j;
36
37 /* Searching */
38 for (j = 0; j <= lengtha - lengthp; j++) {
39 for (i = 0; i < lengthp && p[i] == a[i + j]; i++)
40 ;
41 if (i >= lengthp)
42 return j;
43 }
44 return lengtha;
45}
46
47static unsigned char get_hex_digit(u8 *offset)
48{
49 unsigned char retval = 0;
50
51 retval = *offset - '0';
52 if (retval > 0x09) {
53 retval = *offset - 'A' + 0x0A;
54 if (retval > 0x0F)
55 retval = *offset - 'a' + 0x0a;
56 }
57 if (retval > 0x0F) {
Elyes Haouas0f864f62022-11-13 07:34:48 +010058 printk(BIOS_ERR, "Invalid Hex digit found: %c - 0x%02x\n", *offset, *offset);
Matt DeVillier1b25f1b2018-09-14 21:39:00 -050059 retval = 0;
60 }
61
62 return retval;
63}
64
65static int get_mac_address(u32 *high_dword, u32 *low_dword,
66 u8 *search_address, u32 search_length)
67{
68 char key[] = "ethernet_mac";
69 unsigned int offset;
70 int i;
71
72 offset = search(key, search_address, sizeof(key) - 1, search_length);
73 if (offset == search_length) {
Elyes Haouas0f864f62022-11-13 07:34:48 +010074 printk(BIOS_ERR, "Could not locate '%s' in VPD\n", key);
Matt DeVillier1b25f1b2018-09-14 21:39:00 -050075 return 0;
76 }
77 printk(BIOS_DEBUG, "Located '%s' in VPD\n", key);
78
79 offset += sizeof(key); /* move to next character */
80 *high_dword = 0;
81
82 /* Fetch the MAC address and put the octets in the correct order to
83 * be programmed.
84 *
85 * From RTL8105E_Series_EEPROM-Less_App_Note_1.1
86 * If the MAC address is 001122334455h:
87 * Write 33221100h to I/O register offset 0x00 via double word access
88 * Write 00005544h to I/O register offset 0x04 via double word access
89 */
90
91 for (i = 0; i < 4; i++) {
92 *high_dword |= (get_hex_digit(search_address + offset)
93 << (4 + (i * 8)));
94 *high_dword |= (get_hex_digit(search_address + offset + 1)
95 << (i * 8));
96 offset += 3;
97 }
98
99 *low_dword = 0;
100 for (i = 0; i < 2; i++) {
101 *low_dword |= (get_hex_digit(search_address + offset)
102 << (4 + (i * 8)));
103 *low_dword |= (get_hex_digit(search_address + offset + 1)
104 << (i * 8));
105 offset += 3;
106 }
107
108 return *high_dword | *low_dword;
109}
110
111static void program_mac_address(u16 io_base)
112{
113 void *search_address = NULL;
114 size_t search_length = -1;
115
116 /* Default MAC Address of A0:00:BA:D0:0B:AD */
117 u32 high_dword = 0xD0BA00A0; /* high dword of mac address */
118 u32 low_dword = 0x0000AD0B; /* low word of mac address as a dword */
119
Matt DeVillierd1b3f242021-04-19 13:14:24 -0500120 if (CONFIG(VPD)) {
Matt DeVillier1b25f1b2018-09-14 21:39:00 -0500121 struct region_device rdev;
122
123 if (fmap_locate_area_as_rdev("RO_VPD", &rdev) == 0) {
124 search_address = rdev_mmap_full(&rdev);
125
126 if (search_address != NULL)
127 search_length = region_device_sz(&rdev);
128 }
129 } else {
Julius Werner834b3ec2020-03-04 16:52:08 -0800130 search_address = cbfs_map("vpd.bin", &search_length);
Matt DeVillier1b25f1b2018-09-14 21:39:00 -0500131 }
132
133 if (search_address == NULL)
134 printk(BIOS_ERR, "LAN: VPD not found.\n");
135 else
136 get_mac_address(&high_dword, &low_dword, search_address,
137 search_length);
138
139 if (io_base) {
140 printk(BIOS_DEBUG, "Realtek NIC io_base = 0x%04x\n", io_base);
141 printk(BIOS_DEBUG, "Programming MAC Address\n");
142
143 /* Disable register protection */
144 outb(0xc0, io_base + 0x50);
145 outl(high_dword, io_base);
146 outl(low_dword, io_base + 0x04);
147 outb(0x60, io_base + 54);
148 /* Enable register protection again */
149 outb(0x00, io_base + 0x50);
150 }
151}
152
153void lan_init(void)
154{
155 u16 io_base = 0;
156 struct device *ethernet_dev = NULL;
157
158 /* Get NIC's IO base address */
159 ethernet_dev = dev_find_device(BUDDY_NIC_VENDOR_ID,
160 BUDDY_NIC_DEVICE_ID, 0);
161 if (ethernet_dev != NULL) {
162 io_base = pci_read_config16(ethernet_dev, 0x10) & 0xfffe;
163
164 /*
165 * Battery life time - LAN PCIe should enter ASPM L1 to save
166 * power when LAN connection is idle.
Elyes HAOUAS22f8ee02020-02-22 14:13:59 +0100167 * enable CLKREQ: LAN PCI config space 0x81h=01
Matt DeVillier1b25f1b2018-09-14 21:39:00 -0500168 */
169 pci_write_config8(ethernet_dev, 0x81, 0x01);
170 }
171
172 if (io_base) {
173 /* Program MAC address based on VPD data */
174 program_mac_address(io_base);
175
176 /*
177 * Program NIC LEDS
178 *
179 * RTL8105E Series EEPROM-Less Application Note,
180 * Section 5.6 LED Mode Configuration
181 *
182 * Step1: Write C0h to I/O register 0x50 via byte access to
183 * disable 'register protection'
184 * Step2: Write xx001111b to I/O register 0x52 via byte access
185 * (bit7 is LEDS1 and bit6 is LEDS0)
186 * Step3: Write 0x00 to I/O register 0x50 via byte access to
187 * enable 'register protection'
188 */
189 outb(0xc0, io_base + 0x50); /* Disable protection */
190 outb((BUDDY_NIC_LED_MODE << 6) | 0x0f, io_base + 0x52);
191 outb(0x00, io_base + 0x50); /* Enable register protection */
192 }
193}