mainboards: align on using ACPI_Sx definitions
[coreboot.git] / src / mainboard / google / rikku / lan.c
blobe5676af18636561eaeaff145c67ddc2aabd42753
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.
16 #include <cbfs.h>
17 #include <string.h>
18 #include <types.h>
19 #include <arch/io.h>
20 #include <console/console.h>
21 #include <device/device.h>
22 #include <device/pci.h>
23 #include <fmap.h>
24 #include <southbridge/intel/bd82x6x/pch.h>
25 #include "onboard.h"
27 static unsigned int search(char *p, u8 *a, unsigned int lengthp,
28 unsigned int lengtha)
30 int i, j;
32 /* Searching */
33 for (j = 0; j <= lengtha - lengthp; j++) {
34 for (i = 0; i < lengthp && p[i] == a[i + j]; i++) ;
35 if (i >= lengthp)
36 return j;
38 return lengtha;
41 static unsigned char get_hex_digit(u8 *offset)
43 unsigned char retval = 0;
45 retval = *offset - '0';
46 if (retval > 0x09) {
47 retval = *offset - 'A' + 0x0A;
48 if (retval > 0x0F)
49 retval = *offset - 'a' + 0x0a;
51 if (retval > 0x0F) {
52 printk(BIOS_DEBUG, "Error: Invalid Hex digit found: %c - 0x%02x\n",
53 *offset, *offset);
54 retval = 0;
57 return retval;
60 static int get_mac_address(u32 *high_dword, u32 *low_dword,
61 u8 *search_address, u32 search_length)
63 char key[] = "ethernet_mac";
64 unsigned int offset;
65 int i;
67 offset = search(key, search_address, sizeof(key) - 1, search_length);
68 if (offset == search_length) {
69 printk(BIOS_DEBUG,
70 "Error: Could not locate '%s' in VPD\n", key);
71 return 0;
73 printk(BIOS_DEBUG, "Located '%s' in VPD\n", key);
75 offset += sizeof(key); /* move to next character */
76 *high_dword = 0;
78 /* Fetch the MAC address and put the octets in the correct order to
79 * be programmed.
81 * From RTL8105E_Series_EEPROM-Less_App_Note_1.1
82 * If the MAC address is 001122334455h:
83 * Write 33221100h to I/O register offset 0x00 via double word access
84 * Write 00005544h to I/O register offset 0x04 via double word access
87 for (i = 0; i < 4; i++) {
88 *high_dword |= (get_hex_digit(search_address + offset)
89 << (4 + (i * 8)));
90 *high_dword |= (get_hex_digit(search_address + offset + 1)
91 << (i * 8));
92 offset += 3;
95 *low_dword = 0;
96 for (i = 0; i < 2; i++) {
97 *low_dword |= (get_hex_digit(search_address + offset)
98 << (4 + (i * 8)));
99 *low_dword |= (get_hex_digit(search_address + offset + 1)
100 << (i * 8));
101 offset += 3;
104 return *high_dword | *low_dword;
107 static void program_mac_address(u16 io_base)
109 void *search_address = NULL;
110 size_t search_length = -1;
112 /* Default MAC Address of A0:00:BA:D0:0B:AD */
113 u32 high_dword = 0xD0BA00A0; /* high dword of mac address */
114 u32 low_dword = 0x0000AD0B; /* low word of mac address as a dword */
116 if (IS_ENABLED(CONFIG_CHROMEOS)) {
117 struct region_device rdev;
119 if (fmap_locate_area_as_rdev("RO_VPD", &rdev) == 0) {
120 search_address = rdev_mmap_full(&rdev);
122 if (search_address != NULL)
123 search_length = region_device_sz(&rdev);
125 } else {
126 search_address = cbfs_boot_map_with_leak("vpd.bin",
127 CBFS_TYPE_RAW,
128 &search_length);
131 if (search_address == NULL)
132 printk(BIOS_ERR, "LAN: VPD not found.\n");
133 else
134 get_mac_address(&high_dword, &low_dword, search_address,
135 search_length);
137 if (io_base) {
138 printk(BIOS_DEBUG, "Realtek NIC io_base = 0x%04x\n", io_base);
139 printk(BIOS_DEBUG, "Programming MAC Address\n");
141 /* Disable register protection */
142 outb(0xc0, io_base + 0x50);
143 outl(high_dword, io_base);
144 outl(low_dword, io_base + 0x04);
145 outb(0x60, io_base + 54);
146 /* Enable register protection again */
147 outb(0x00, io_base + 0x50);
151 void lan_init(void)
153 u16 io_base = 0;
154 struct device *ethernet_dev = NULL;
156 /* Get NIC's IO base address */
157 ethernet_dev = dev_find_device(RIKKU_NIC_VENDOR_ID,
158 RIKKU_NIC_DEVICE_ID, 0);
159 if (ethernet_dev != NULL) {
160 io_base = pci_read_config16(ethernet_dev, 0x10) & 0xfffe;
163 * Battery life time - LAN PCIe should enter ASPM L1 to save
164 * power when LAN connection is idle.
165 * enable CLKREQ: LAN pci config space 0x81h=01
167 pci_write_config8(ethernet_dev, 0x81, 0x01);
170 if (io_base) {
171 /* Program MAC address based on VPD data */
172 program_mac_address(io_base);
175 * Program NIC LEDS
177 * RTL8105E Series EEPROM-Less Application Note,
178 * Section 5.6 LED Mode Configuration
180 * Step1: Write C0h to I/O register 0x50 via byte access to
181 * disable 'register protection'
182 * Step2: Write xx001111b to I/O register 0x52 via byte access
183 * (bit7 is LEDS1 and bit6 is LEDS0)
184 * Step3: Write 0x00 to I/O register 0x50 via byte access to
185 * enable 'register protection'
187 outb(0xc0, io_base + 0x50); /* Disable protection */
188 outb((RIKKU_NIC_LED_MODE << 6) | 0x0f, io_base + 0x52);
189 outb(0x00, io_base + 0x50); /* Enable register protection */