Angel Pons | 58c0d32 | 2020-04-05 13:20:46 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Matt DeVillier | 1b25f1b | 2018-09-14 21:39:00 -0500 | [diff] [blame] | 2 | |
| 3 | #include <cbfs.h> |
| 4 | #include <fmap.h> |
| 5 | #include <types.h> |
Matt DeVillier | 1b25f1b | 2018-09-14 21:39:00 -0500 | [diff] [blame] | 6 | #include <arch/io.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 7 | #include <device/pci_ops.h> |
Matt DeVillier | 1b25f1b | 2018-09-14 21:39:00 -0500 | [diff] [blame] | 8 | #include <console/console.h> |
| 9 | #include <device/device.h> |
Matt DeVillier | 1b25f1b | 2018-09-14 21:39:00 -0500 | [diff] [blame] | 10 | #include <smbios.h> |
| 11 | #include <soc/pch.h> |
| 12 | #include <variant/onboard.h> |
| 13 | #include <mainboard/google/auron/variant.h> |
| 14 | |
Elyes HAOUAS | 38f1d13 | 2018-09-17 08:44:18 +0200 | [diff] [blame] | 15 | int variant_smbios_data(struct device *dev, int *handle, unsigned long *current) |
Matt DeVillier | 1b25f1b | 2018-09-14 21:39:00 -0500 | [diff] [blame] | 16 | { |
| 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 Walter | e6afab1 | 2019-05-21 17:22:49 +0200 | [diff] [blame] | 26 | 0, /* function */ |
| 27 | SMBIOS_DEVICE_TYPE_OTHER); /* device type */ |
Matt DeVillier | 1b25f1b | 2018-09-14 21:39:00 -0500 | [diff] [blame] | 28 | |
| 29 | return len; |
| 30 | } |
| 31 | |
| 32 | static 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 | |
| 47 | static 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) { |
| 58 | printk(BIOS_DEBUG, "Error: Invalid Hex digit found: %c - 0x%02x\n", |
| 59 | *offset, *offset); |
| 60 | retval = 0; |
| 61 | } |
| 62 | |
| 63 | return retval; |
| 64 | } |
| 65 | |
| 66 | static int get_mac_address(u32 *high_dword, u32 *low_dword, |
| 67 | u8 *search_address, u32 search_length) |
| 68 | { |
| 69 | char key[] = "ethernet_mac"; |
| 70 | unsigned int offset; |
| 71 | int i; |
| 72 | |
| 73 | offset = search(key, search_address, sizeof(key) - 1, search_length); |
| 74 | if (offset == search_length) { |
| 75 | printk(BIOS_DEBUG, |
| 76 | "Error: Could not locate '%s' in VPD\n", key); |
| 77 | return 0; |
| 78 | } |
| 79 | printk(BIOS_DEBUG, "Located '%s' in VPD\n", key); |
| 80 | |
| 81 | offset += sizeof(key); /* move to next character */ |
| 82 | *high_dword = 0; |
| 83 | |
| 84 | /* Fetch the MAC address and put the octets in the correct order to |
| 85 | * be programmed. |
| 86 | * |
| 87 | * From RTL8105E_Series_EEPROM-Less_App_Note_1.1 |
| 88 | * If the MAC address is 001122334455h: |
| 89 | * Write 33221100h to I/O register offset 0x00 via double word access |
| 90 | * Write 00005544h to I/O register offset 0x04 via double word access |
| 91 | */ |
| 92 | |
| 93 | for (i = 0; i < 4; i++) { |
| 94 | *high_dword |= (get_hex_digit(search_address + offset) |
| 95 | << (4 + (i * 8))); |
| 96 | *high_dword |= (get_hex_digit(search_address + offset + 1) |
| 97 | << (i * 8)); |
| 98 | offset += 3; |
| 99 | } |
| 100 | |
| 101 | *low_dword = 0; |
| 102 | for (i = 0; i < 2; i++) { |
| 103 | *low_dword |= (get_hex_digit(search_address + offset) |
| 104 | << (4 + (i * 8))); |
| 105 | *low_dword |= (get_hex_digit(search_address + offset + 1) |
| 106 | << (i * 8)); |
| 107 | offset += 3; |
| 108 | } |
| 109 | |
| 110 | return *high_dword | *low_dword; |
| 111 | } |
| 112 | |
| 113 | static void program_mac_address(u16 io_base) |
| 114 | { |
| 115 | void *search_address = NULL; |
| 116 | size_t search_length = -1; |
| 117 | |
| 118 | /* Default MAC Address of A0:00:BA:D0:0B:AD */ |
| 119 | u32 high_dword = 0xD0BA00A0; /* high dword of mac address */ |
| 120 | u32 low_dword = 0x0000AD0B; /* low word of mac address as a dword */ |
| 121 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 122 | if (CONFIG(CHROMEOS)) { |
Matt DeVillier | 1b25f1b | 2018-09-14 21:39:00 -0500 | [diff] [blame] | 123 | struct region_device rdev; |
| 124 | |
| 125 | if (fmap_locate_area_as_rdev("RO_VPD", &rdev) == 0) { |
| 126 | search_address = rdev_mmap_full(&rdev); |
| 127 | |
| 128 | if (search_address != NULL) |
| 129 | search_length = region_device_sz(&rdev); |
| 130 | } |
| 131 | } else { |
Julius Werner | 834b3ec | 2020-03-04 16:52:08 -0800 | [diff] [blame^] | 132 | search_address = cbfs_map("vpd.bin", &search_length); |
Matt DeVillier | 1b25f1b | 2018-09-14 21:39:00 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if (search_address == NULL) |
| 136 | printk(BIOS_ERR, "LAN: VPD not found.\n"); |
| 137 | else |
| 138 | get_mac_address(&high_dword, &low_dword, search_address, |
| 139 | search_length); |
| 140 | |
| 141 | if (io_base) { |
| 142 | printk(BIOS_DEBUG, "Realtek NIC io_base = 0x%04x\n", io_base); |
| 143 | printk(BIOS_DEBUG, "Programming MAC Address\n"); |
| 144 | |
| 145 | /* Disable register protection */ |
| 146 | outb(0xc0, io_base + 0x50); |
| 147 | outl(high_dword, io_base); |
| 148 | outl(low_dword, io_base + 0x04); |
| 149 | outb(0x60, io_base + 54); |
| 150 | /* Enable register protection again */ |
| 151 | outb(0x00, io_base + 0x50); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void lan_init(void) |
| 156 | { |
| 157 | u16 io_base = 0; |
| 158 | struct device *ethernet_dev = NULL; |
| 159 | |
| 160 | /* Get NIC's IO base address */ |
| 161 | ethernet_dev = dev_find_device(BUDDY_NIC_VENDOR_ID, |
| 162 | BUDDY_NIC_DEVICE_ID, 0); |
| 163 | if (ethernet_dev != NULL) { |
| 164 | io_base = pci_read_config16(ethernet_dev, 0x10) & 0xfffe; |
| 165 | |
| 166 | /* |
| 167 | * Battery life time - LAN PCIe should enter ASPM L1 to save |
| 168 | * power when LAN connection is idle. |
Elyes HAOUAS | 22f8ee0 | 2020-02-22 14:13:59 +0100 | [diff] [blame] | 169 | * enable CLKREQ: LAN PCI config space 0x81h=01 |
Matt DeVillier | 1b25f1b | 2018-09-14 21:39:00 -0500 | [diff] [blame] | 170 | */ |
| 171 | pci_write_config8(ethernet_dev, 0x81, 0x01); |
| 172 | } |
| 173 | |
| 174 | if (io_base) { |
| 175 | /* Program MAC address based on VPD data */ |
| 176 | program_mac_address(io_base); |
| 177 | |
| 178 | /* |
| 179 | * Program NIC LEDS |
| 180 | * |
| 181 | * RTL8105E Series EEPROM-Less Application Note, |
| 182 | * Section 5.6 LED Mode Configuration |
| 183 | * |
| 184 | * Step1: Write C0h to I/O register 0x50 via byte access to |
| 185 | * disable 'register protection' |
| 186 | * Step2: Write xx001111b to I/O register 0x52 via byte access |
| 187 | * (bit7 is LEDS1 and bit6 is LEDS0) |
| 188 | * Step3: Write 0x00 to I/O register 0x50 via byte access to |
| 189 | * enable 'register protection' |
| 190 | */ |
| 191 | outb(0xc0, io_base + 0x50); /* Disable protection */ |
| 192 | outb((BUDDY_NIC_LED_MODE << 6) | 0x0f, io_base + 0x52); |
| 193 | outb(0x00, io_base + 0x50); /* Enable register protection */ |
| 194 | } |
| 195 | } |