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