Matt DeVillier | 81ae67a | 2016-11-08 15:04:30 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 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älkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 20 | #include <device/pci_ops.h> |
Matt DeVillier | 81ae67a | 2016-11-08 15:04:30 -0600 | [diff] [blame] | 21 | #include <console/console.h> |
| 22 | #include <device/device.h> |
| 23 | #include <device/pci.h> |
| 24 | #include <fmap.h> |
| 25 | #include <southbridge/intel/bd82x6x/pch.h> |
| 26 | #include "onboard.h" |
| 27 | |
| 28 | static unsigned int search(char *p, u8 *a, unsigned int lengthp, |
| 29 | unsigned int lengtha) |
| 30 | { |
| 31 | int i, j; |
| 32 | |
| 33 | /* Searching */ |
| 34 | for (j = 0; j <= lengtha - lengthp; j++) { |
| 35 | for (i = 0; i < lengthp && p[i] == a[i + j]; i++) |
| 36 | ; |
| 37 | if (i >= lengthp) |
| 38 | return j; |
| 39 | } |
| 40 | return lengtha; |
| 41 | } |
| 42 | |
| 43 | static unsigned char get_hex_digit(u8 *offset) |
| 44 | { |
| 45 | unsigned char retval = 0; |
| 46 | |
| 47 | retval = *offset - '0'; |
| 48 | if (retval > 0x09) { |
| 49 | retval = *offset - 'A' + 0x0A; |
| 50 | if (retval > 0x0F) |
| 51 | retval = *offset - 'a' + 0x0a; |
| 52 | } |
| 53 | if (retval > 0x0F) { |
| 54 | printk(BIOS_DEBUG, "Error: Invalid Hex digit found: %c - 0x%02x\n", |
| 55 | *offset, *offset); |
| 56 | retval = 0; |
| 57 | } |
| 58 | |
| 59 | return retval; |
| 60 | } |
| 61 | |
| 62 | static int get_mac_address(u32 *high_dword, u32 *low_dword, |
| 63 | u8 *search_address, u32 search_length) |
| 64 | { |
| 65 | char key[] = "ethernet_mac"; |
| 66 | unsigned int offset; |
| 67 | int i; |
| 68 | |
| 69 | offset = search(key, search_address, sizeof(key) - 1, search_length); |
| 70 | if (offset == search_length) { |
| 71 | printk(BIOS_DEBUG, |
| 72 | "Error: Could not locate '%s' in VPD\n", key); |
| 73 | return 0; |
| 74 | } |
| 75 | printk(BIOS_DEBUG, "Located '%s' in VPD\n", key); |
| 76 | |
| 77 | offset += sizeof(key); /* move to next character */ |
| 78 | *high_dword = 0; |
| 79 | |
| 80 | /* Fetch the MAC address and put the octets in the correct order to |
| 81 | * be programmed. |
| 82 | * |
| 83 | * From RTL8105E_Series_EEPROM-Less_App_Note_1.1 |
| 84 | * If the MAC address is 001122334455h: |
| 85 | * Write 33221100h to I/O register offset 0x00 via double word access |
| 86 | * Write 00005544h to I/O register offset 0x04 via double word access |
| 87 | */ |
| 88 | |
| 89 | for (i = 0; i < 4; i++) { |
| 90 | *high_dword |= (get_hex_digit(search_address + offset) |
| 91 | << (4 + (i * 8))); |
| 92 | *high_dword |= (get_hex_digit(search_address + offset + 1) |
| 93 | << (i * 8)); |
| 94 | offset += 3; |
| 95 | } |
| 96 | |
| 97 | *low_dword = 0; |
| 98 | for (i = 0; i < 2; i++) { |
| 99 | *low_dword |= (get_hex_digit(search_address + offset) |
| 100 | << (4 + (i * 8))); |
| 101 | *low_dword |= (get_hex_digit(search_address + offset + 1) |
| 102 | << (i * 8)); |
| 103 | offset += 3; |
| 104 | } |
| 105 | |
| 106 | return *high_dword | *low_dword; |
| 107 | } |
| 108 | |
| 109 | static void program_mac_address(u16 io_base) |
| 110 | { |
| 111 | void *search_address = NULL; |
| 112 | size_t search_length = -1; |
| 113 | |
| 114 | /* Default MAC Address of A0:00:BA:D0:0B:AD */ |
| 115 | u32 high_dword = 0xD0BA00A0; /* high dword of mac address */ |
| 116 | u32 low_dword = 0x0000AD0B; /* low word of mac address as a dword */ |
| 117 | |
| 118 | if (IS_ENABLED(CONFIG_CHROMEOS)) { |
| 119 | struct region_device rdev; |
| 120 | |
| 121 | if (fmap_locate_area_as_rdev("RO_VPD", &rdev) == 0) { |
| 122 | search_address = rdev_mmap_full(&rdev); |
| 123 | |
| 124 | if (search_address != NULL) |
| 125 | search_length = region_device_sz(&rdev); |
| 126 | } |
| 127 | } else { |
| 128 | search_address = cbfs_boot_map_with_leak("vpd.bin", |
| 129 | CBFS_TYPE_RAW, |
| 130 | &search_length); |
| 131 | } |
| 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 | |
| 153 | void 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(NIC_VENDOR_ID, |
| 160 | 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. |
| 167 | * enable CLKREQ: LAN pci config space 0x81h=01 |
| 168 | */ |
| 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((NIC_LED_MODE << 6) | 0x0f, io_base + 0x52); |
| 191 | outb(0x00, io_base + 0x50); /* Enable register protection */ |
| 192 | } |
| 193 | } |