1 /* SPDX-License-Identifier: GPL-2.0-only */
6 #include <device/pci_ops.h>
7 #include <console/console.h>
8 #include <device/device.h>
10 #include <variant/onboard.h>
12 static unsigned int search(char *p
, u8
*a
, unsigned int lengthp
,
18 for (j
= 0; j
<= lengtha
- lengthp
; j
++) {
19 for (i
= 0; i
< lengthp
&& p
[i
] == a
[i
+ j
]; i
++)
27 static unsigned char get_hex_digit(u8
*offset
)
29 unsigned char retval
= 0;
31 retval
= *offset
- '0';
33 retval
= *offset
- 'A' + 0x0A;
35 retval
= *offset
- 'a' + 0x0a;
38 printk(BIOS_ERR
, "Invalid Hex digit found: %c - 0x%02x\n", *offset
, *offset
);
45 static int get_mac_address(u32
*high_dword
, u32
*low_dword
,
46 u8
*search_address
, u32 search_length
)
48 char key
[] = "ethernet_mac";
52 offset
= search(key
, search_address
, sizeof(key
) - 1, search_length
);
53 if (offset
== search_length
) {
54 printk(BIOS_ERR
, "Could not locate '%s' in VPD\n", key
);
57 printk(BIOS_DEBUG
, "Located '%s' in VPD\n", key
);
59 offset
+= sizeof(key
); /* move to next character */
62 /* Fetch the MAC address and put the octets in the correct order to
65 * From RTL8105E_Series_EEPROM-Less_App_Note_1.1
66 * If the MAC address is 001122334455h:
67 * Write 33221100h to I/O register offset 0x00 via double word access
68 * Write 00005544h to I/O register offset 0x04 via double word access
71 for (i
= 0; i
< 4; i
++) {
72 *high_dword
|= (get_hex_digit(search_address
+ offset
)
74 *high_dword
|= (get_hex_digit(search_address
+ offset
+ 1)
80 for (i
= 0; i
< 2; i
++) {
81 *low_dword
|= (get_hex_digit(search_address
+ offset
)
83 *low_dword
|= (get_hex_digit(search_address
+ offset
+ 1)
88 return *high_dword
| *low_dword
;
91 static void program_mac_address(u16 io_base
)
93 void *search_address
= NULL
;
94 size_t search_length
= -1;
96 /* Default MAC Address of A0:00:BA:D0:0B:AD */
97 u32 high_dword
= 0xD0BA00A0; /* high dword of mac address */
98 u32 low_dword
= 0x0000AD0B; /* low word of mac address as a dword */
101 struct region_device rdev
;
103 if (fmap_locate_area_as_rdev("RO_VPD", &rdev
) == 0) {
104 search_address
= rdev_mmap_full(&rdev
);
106 if (search_address
!= NULL
)
107 search_length
= region_device_sz(&rdev
);
110 search_address
= cbfs_map("vpd.bin", &search_length
);
113 if (search_address
== NULL
)
114 printk(BIOS_ERR
, "LAN: VPD not found.\n");
116 get_mac_address(&high_dword
, &low_dword
, search_address
,
120 printk(BIOS_DEBUG
, "Realtek NIC io_base = 0x%04x\n", io_base
);
121 printk(BIOS_DEBUG
, "Programming MAC Address\n");
123 /* Disable register protection */
124 outb(0xc0, io_base
+ 0x50);
125 outl(high_dword
, io_base
);
126 outl(low_dword
, io_base
+ 0x04);
127 outb(0x60, io_base
+ 54);
128 /* Enable register protection again */
129 outb(0x00, io_base
+ 0x50);
136 struct device
*ethernet_dev
= NULL
;
138 /* Get NIC's IO base address */
139 ethernet_dev
= dev_find_device(NINJA_NIC_VENDOR_ID
,
140 NINJA_NIC_DEVICE_ID
, 0);
141 if (ethernet_dev
!= NULL
) {
142 io_base
= pci_read_config16(ethernet_dev
, 0x10) & 0xfffe;
145 * Battery life time - LAN PCIe should enter ASPM L1 to save
146 * power when LAN connection is idle.
147 * enable CLKREQ: LAN pci config space 0x81h=01
149 pci_write_config8(ethernet_dev
, 0x81, 0x01);
153 /* Program MAC address based on VPD data */
154 program_mac_address(io_base
);
159 * RTL8105E Series EEPROM-Less Application Note,
160 * Section 5.6 LED Mode Configuration
162 * Step1: Write C0h to I/O register 0x50 via byte access to
163 * disable 'register protection'
164 * Step2: Write xx001111b to I/O register 0x52 via byte access
165 * (bit7 is LEDS1 and bit6 is LEDS0)
166 * Step3: Write 0x00 to I/O register 0x50 via byte access to
167 * enable 'register protection'
169 outb(0xc0, io_base
+ 0x50); /* Disable protection */
170 outb((NINJA_NIC_LED_MODE
<< 6) | 0x0f, io_base
+ 0x52);
171 outb(0x00, io_base
+ 0x50); /* Enable register protection */