Remove address from GPLv2 headers
[coreboot.git] / src / mainboard / google / panther / lan.c
blob9fd325f5d2e885bd093b2522a80c88a482cc6f92
1 /*
2 * This file is part of the coreboot project.
4 * Copyright (C) 2012 Google Inc.
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc.
20 #include <cbfs.h>
21 #include <string.h>
22 #include <types.h>
23 #include <arch/io.h>
24 #include <console/console.h>
25 #include <device/device.h>
26 #include <device/pci.h>
27 #include <southbridge/intel/bd82x6x/pch.h>
28 #include <vendorcode/google/chromeos/fmap.h>
29 #include "onboard.h"
31 static unsigned int search(char *p, u8 *a, unsigned int lengthp,
32 unsigned int lengtha)
34 int i, j;
36 /* Searching */
37 for (j = 0; j <= lengtha - lengthp; j++) {
38 for (i = 0; i < lengthp && p[i] == a[i + j]; i++) ;
39 if (i >= lengthp)
40 return j;
42 return lengtha;
45 static unsigned char get_hex_digit(u8 *offset)
47 unsigned char retval = 0;
49 retval = *offset - '0';
50 if (retval > 0x09) {
51 retval = *offset - 'A' + 0x0A;
52 if (retval > 0x0F)
53 retval = *offset - 'a' + 0x0a;
55 if (retval > 0x0F) {
56 printk(BIOS_DEBUG, "Error: Invalid Hex digit found: %c - 0x%02x\n",
57 *offset, *offset);
58 retval = 0;
61 return retval;
64 static int get_mac_address(u32 *high_dword, u32 *low_dword,
65 u8 *search_address, u32 search_length)
67 char key[] = "ethernet_mac";
68 unsigned int offset;
69 int i;
71 offset = search(key, search_address, sizeof(key) - 1, search_length);
72 if (offset == search_length) {
73 printk(BIOS_DEBUG,
74 "Error: Could not locate '%s' in VPD\n", key);
75 return 0;
77 printk(BIOS_DEBUG, "Located '%s' in VPD\n", key);
79 offset += sizeof(key); /* move to next character */
80 *high_dword = 0;
82 /* Fetch the MAC address and put the octets in the correct order to
83 * be programmed.
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
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;
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;
108 return *high_dword | *low_dword;
111 static void program_mac_address(u16 io_base)
113 void *search_address = NULL;
114 size_t search_length = -1;
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 */
120 #if CONFIG_CHROMEOS
121 search_length = find_fmap_entry("RO_VPD", &search_address);
122 #else
123 search_address = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "vpd.bin",
124 CBFS_TYPE_RAW, &search_length);
125 #endif
127 if (search_length <= 0)
128 printk(BIOS_ERR, "LAN: find_fmap_entry returned -1.\n");
129 else
130 get_mac_address(&high_dword, &low_dword, search_address,
131 search_length);
133 if (io_base) {
134 printk(BIOS_DEBUG, "Realtek NIC io_base = 0x%04x\n", io_base);
135 printk(BIOS_DEBUG, "Programming MAC Address\n");
137 /* Disable register protection */
138 outb(0xc0, io_base + 0x50);
139 outl(high_dword, io_base);
140 outl(low_dword, io_base + 0x04);
141 outb(0x60, io_base + 54);
142 /* Enable register protection again */
143 outb(0x00, io_base + 0x50);
147 void lan_init(void)
149 u16 io_base = 0;
150 struct device *ethernet_dev = NULL;
152 /* Get NIC's IO base address */
153 ethernet_dev = dev_find_device(PANTHER_NIC_VENDOR_ID,
154 PANTHER_NIC_DEVICE_ID, 0);
155 if (ethernet_dev != NULL) {
156 io_base = pci_read_config16(ethernet_dev, 0x10) & 0xfffe;
159 * Battery life time - LAN PCIe should enter ASPM L1 to save
160 * power when LAN connection is idle.
161 * enable CLKREQ: LAN pci config space 0x81h=01
163 pci_write_config8(ethernet_dev, 0x81, 0x01);
166 if (io_base) {
167 /* Program MAC address based on VPD data */
168 program_mac_address(io_base);
171 * Program NIC LEDS
173 * RTL8105E Series EEPROM-Less Application Note,
174 * Section 5.6 LED Mode Configuration
176 * Step1: Write C0h to I/O register 0x50 via byte access to
177 * disable 'register protection'
178 * Step2: Write xx001111b to I/O register 0x52 via byte access
179 * (bit7 is LEDS1 and bit6 is LEDS0)
180 * Step3: Write 0x00 to I/O register 0x50 via byte access to
181 * enable 'register protection'
183 outb(0xc0, io_base + 0x50); /* Disable protection */
184 outb((PANTHER_NIC_LED_MODE << 6) | 0x0f, io_base + 0x52);
185 outb(0x00, io_base + 0x50); /* Enable register protection */