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