Angel Pons | 54c5472 | 2020-04-05 13:20:54 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 2 | |
| 3 | #include <types.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 4 | #include <cbfs.h> |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 5 | #include <device/device.h> |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 6 | #include <device/pci_ops.h> |
| 7 | #include <console/console.h> |
Vladimir Serbinenko | a2a906e | 2014-09-01 01:41:37 +0200 | [diff] [blame] | 8 | #include <drivers/intel/gma/int15.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 9 | #include <fmap.h> |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 10 | #include <arch/io.h> |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 11 | #include "onboard.h" |
| 12 | #include "ec.h" |
| 13 | #include <southbridge/intel/bd82x6x/pch.h> |
| 14 | #include <smbios.h> |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 15 | #include <ec/quanta/ene_kb3940q/ec.h> |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 16 | |
| 17 | static unsigned int search(char *p, char *a, unsigned int lengthp, |
| 18 | unsigned int lengtha) |
| 19 | { |
| 20 | int i, j; |
| 21 | |
| 22 | /* Searching */ |
| 23 | for (j = 0; j <= lengtha - lengthp; j++) { |
Elyes HAOUAS | f2fcf22 | 2016-10-02 10:09:11 +0200 | [diff] [blame] | 24 | for (i = 0; i < lengthp && p[i] == a[i + j]; i++) |
| 25 | ; |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 26 | if (i >= lengthp) |
| 27 | return j; |
| 28 | } |
| 29 | return lengtha; |
| 30 | } |
| 31 | |
| 32 | static unsigned char get_hex_digit(char *offset) |
| 33 | { |
| 34 | unsigned char retval = 0; |
| 35 | |
| 36 | retval = *offset - '0'; |
| 37 | if (retval > 0x09) { |
| 38 | retval = *offset - 'A' + 0x0A; |
| 39 | if (retval > 0x0F) |
| 40 | retval = *offset - 'a' + 0x0a; |
| 41 | } |
| 42 | if (retval > 0x0F) { |
Elyes Haouas | 0f864f6 | 2022-11-13 07:34:48 +0100 | [diff] [blame] | 43 | printk(BIOS_ERR, "Invalid Hex digit found: %c - 0x%02x\n", |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 44 | *offset, (unsigned char)*offset); |
| 45 | retval = 0; |
| 46 | } |
| 47 | |
| 48 | return retval; |
| 49 | } |
| 50 | |
| 51 | static int get_mac_address(u32 *high_dword, u32 *low_dword, |
| 52 | u32 search_address, u32 search_length) |
| 53 | { |
| 54 | char key[] = "ethernet_mac"; |
| 55 | unsigned int offset; |
| 56 | int i; |
| 57 | |
| 58 | offset = search(key, (char *)search_address, |
| 59 | sizeof(key) - 1, search_length); |
| 60 | if (offset == search_length) { |
Elyes Haouas | 0f864f6 | 2022-11-13 07:34:48 +0100 | [diff] [blame] | 61 | printk(BIOS_ERR, "Could not locate '%s' in VPD\n", key); |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | printk(BIOS_DEBUG, "Located '%s' in VPD\n", key); |
| 65 | |
| 66 | offset += sizeof(key); /* move to next character */ |
| 67 | *high_dword = 0; |
| 68 | |
| 69 | /* Fetch the MAC address and put the octets in the correct order to |
| 70 | * be programmed. |
| 71 | * |
| 72 | * From RTL8105E_Series_EEPROM-Less_App_Note_1.1 |
| 73 | * If the MAC address is 001122334455h: |
| 74 | * Write 33221100h to I/O register offset 0x00 via double word access |
| 75 | * Write 00005544h to I/O register offset 0x04 via double word access |
| 76 | */ |
| 77 | |
| 78 | for (i = 0; i < 4; i++) { |
| 79 | *high_dword |= (get_hex_digit((char *)(search_address + offset)) |
| 80 | << (4 + (i * 8))); |
| 81 | *high_dword |= (get_hex_digit((char *)(search_address + offset + 1)) |
| 82 | << (i * 8)); |
| 83 | offset += 3; |
| 84 | } |
| 85 | |
| 86 | *low_dword = 0; |
| 87 | for (i = 0; i < 2; i++) { |
| 88 | *low_dword |= (get_hex_digit((char *)(search_address + offset)) |
| 89 | << (4 + (i * 8))); |
| 90 | *low_dword |= (get_hex_digit((char *)(search_address + offset + 1)) |
| 91 | << (i * 8)); |
| 92 | offset += 3; |
| 93 | } |
| 94 | |
| 95 | return *high_dword | *low_dword; |
| 96 | } |
| 97 | |
| 98 | static void program_mac_address(u16 io_base, u32 search_address, |
| 99 | u32 search_length) |
| 100 | { |
| 101 | /* Default MAC Address of A0:00:BA:D0:0B:AD */ |
| 102 | u32 high_dword = 0xD0BA00A0; /* high dword of mac address */ |
| 103 | u32 low_dword = 0x0000AD0B; /* low word of mac address as a dword */ |
| 104 | |
| 105 | if (search_length != -1) |
| 106 | get_mac_address(&high_dword, &low_dword, search_address, |
| 107 | search_length); |
| 108 | |
| 109 | if (io_base) { |
| 110 | printk(BIOS_DEBUG, "Realtek NIC io_base = 0x%04x\n", io_base); |
| 111 | printk(BIOS_DEBUG, "Programming MAC Address\n"); |
| 112 | |
| 113 | outb(0xc0, io_base + 0x50); /* Disable register protection */ |
| 114 | outl(high_dword, io_base); |
| 115 | outl(low_dword, io_base + 0x04); |
| 116 | outb(0x60, io_base + 54); |
| 117 | outb(0x00, io_base + 0x50); /* Enable register protection again */ |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static void program_keyboard_type(u32 search_address, u32 search_length) |
| 122 | { |
| 123 | char key[] = "keyboard_layout"; |
| 124 | char kbd_jpn[] = "xkb:jp::jpn"; |
| 125 | unsigned int offset; |
| 126 | char kbd_type = EC_KBD_EN; /* Default keyboard type is English */ |
| 127 | |
| 128 | if (search_length != -1) { |
| 129 | |
| 130 | /* |
| 131 | * Search for keyboard_layout identifier |
| 132 | * The only options in the EC are Japanese or English. |
| 133 | * The English keyboard layout is actually used for multiple |
| 134 | * different languages - English, Spanish, French... Because |
| 135 | * of this the code only searches for Japanese, and sets the |
| 136 | * keyboard type to English if Japanese is not found. |
| 137 | */ |
| 138 | offset = search(key, (char *)search_address, sizeof(key) - 1, |
| 139 | search_length); |
| 140 | if (offset != search_length) { |
| 141 | printk(BIOS_DEBUG, "Located '%s' in VPD\n", key); |
| 142 | |
| 143 | offset += sizeof(key); /* move to next character */ |
| 144 | search_length = sizeof(kbd_jpn); |
| 145 | offset = search(kbd_jpn, (char *)(search_address + offset), |
| 146 | sizeof(kbd_jpn) - 1, search_length); |
| 147 | if (offset != search_length) |
| 148 | kbd_type = EC_KBD_JP; |
| 149 | } |
Angel Pons | 1f4e78f | 2021-08-26 09:53:14 +0200 | [diff] [blame] | 150 | } else { |
Elyes Haouas | 0f864f6 | 2022-11-13 07:34:48 +0100 | [diff] [blame] | 151 | printk(BIOS_ERR, "Could not locate VPD area\n"); |
Angel Pons | 1f4e78f | 2021-08-26 09:53:14 +0200 | [diff] [blame] | 152 | } |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 153 | |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 154 | printk(BIOS_DEBUG, "Setting Keyboard type in EC to "); |
| 155 | printk(BIOS_DEBUG, (kbd_type == EC_KBD_JP) ? "Japanese" : "English"); |
| 156 | printk(BIOS_DEBUG, ".\n"); |
| 157 | |
| 158 | ec_mem_write(EC_KBID_REG, kbd_type); |
| 159 | } |
| 160 | |
Elyes HAOUAS | d129d43 | 2018-05-04 20:23:33 +0200 | [diff] [blame] | 161 | static void mainboard_init(struct device *dev) |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 162 | { |
Kyösti Mälkki | fab0c9f | 2013-11-28 18:10:03 +0200 | [diff] [blame] | 163 | u32 search_address = 0x0; |
Vladimir Serbinenko | 1287416 | 2014-01-12 14:12:15 +0100 | [diff] [blame] | 164 | size_t search_length = -1; |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 165 | u16 io_base = 0; |
| 166 | struct device *ethernet_dev = NULL; |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 167 | void *vpd_file; |
| 168 | |
Matt DeVillier | d1b3f24 | 2021-04-19 13:14:24 -0500 | [diff] [blame] | 169 | if (CONFIG(VPD)) { |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 170 | struct region_device rdev; |
| 171 | |
| 172 | if (fmap_locate_area_as_rdev("RO_VPD", &rdev) == 0) { |
| 173 | vpd_file = rdev_mmap_full(&rdev); |
| 174 | |
| 175 | if (vpd_file != NULL) { |
| 176 | search_length = region_device_sz(&rdev); |
| 177 | search_address = (uintptr_t)vpd_file; |
| 178 | } |
| 179 | } |
Vladimir Serbinenko | 1287416 | 2014-01-12 14:12:15 +0100 | [diff] [blame] | 180 | } else { |
Julius Werner | 834b3ec | 2020-03-04 16:52:08 -0800 | [diff] [blame] | 181 | vpd_file = cbfs_map("vpd.bin", &search_length); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 182 | if (vpd_file) { |
| 183 | search_address = (unsigned long)vpd_file; |
| 184 | } else { |
| 185 | search_length = -1; |
| 186 | search_address = 0; |
| 187 | } |
Kyösti Mälkki | fab0c9f | 2013-11-28 18:10:03 +0200 | [diff] [blame] | 188 | } |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 189 | |
| 190 | /* Initialize the Embedded Controller */ |
| 191 | butterfly_ec_init(); |
| 192 | |
| 193 | /* Program EC Keyboard locale based on VPD data */ |
| 194 | program_keyboard_type(search_address, search_length); |
| 195 | |
| 196 | /* Get NIC's IO base address */ |
| 197 | ethernet_dev = dev_find_device(BUTTERFLY_NIC_VENDOR_ID, |
| 198 | BUTTERFLY_NIC_DEVICE_ID, dev); |
| 199 | if (ethernet_dev != NULL) { |
| 200 | io_base = pci_read_config16(ethernet_dev, 0x10) & 0xfffe; |
| 201 | |
| 202 | /* |
| 203 | * Battery life time - LAN PCIe should enter ASPM L1 to save |
| 204 | * power when LAN connection is idle. |
| 205 | * enable CLKREQ: LAN pci config space 0x81h=01 |
| 206 | */ |
| 207 | pci_write_config8(ethernet_dev, 0x81, 0x01); |
| 208 | } |
| 209 | |
| 210 | if (io_base) { |
| 211 | /* Program MAC address based on VPD data */ |
| 212 | program_mac_address(io_base, search_address, search_length); |
| 213 | |
| 214 | /* |
| 215 | * Program NIC LEDS |
| 216 | * |
| 217 | * RTL8105E Series EEPROM-Less Application Note, |
| 218 | * Section 5.6 LED Mode Configuration |
| 219 | * |
| 220 | * Step1: Write C0h to I/O register 0x50 via byte access to |
| 221 | * disable 'register protection' |
| 222 | * Step2: Write xx001111b to I/O register 0x52 via byte access |
| 223 | * (bit7 is LEDS1 and bit6 is LEDS0) |
| 224 | * Step3: Write 0x00 to I/O register 0x50 via byte access to |
| 225 | * enable 'register protection' |
| 226 | */ |
| 227 | outb(0xc0, io_base + 0x50); /* Disable protection */ |
| 228 | outb((BUTTERFLY_NIC_LED_MODE << 6) | 0x0f, io_base + 0x52); |
| 229 | outb(0x00, io_base + 0x50); /* Enable register protection */ |
| 230 | } |
| 231 | } |
| 232 | |
Elyes HAOUAS | d129d43 | 2018-05-04 20:23:33 +0200 | [diff] [blame] | 233 | static int butterfly_onboard_smbios_data(struct device *dev, int *handle, |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 234 | unsigned long *current) |
| 235 | { |
| 236 | int len = 0; |
| 237 | |
Duncan Laurie | 21a7870 | 2013-05-23 14:17:05 -0700 | [diff] [blame] | 238 | len += smbios_write_type41( |
| 239 | current, handle, |
Kyösti Mälkki | b9cd5ec | 2015-04-24 16:05:58 +0300 | [diff] [blame] | 240 | BOARD_TRACKPAD_NAME, /* name */ |
| 241 | BOARD_TRACKPAD_IRQ, /* instance */ |
Duncan Laurie | 21a7870 | 2013-05-23 14:17:05 -0700 | [diff] [blame] | 242 | 0, /* segment */ |
Kyösti Mälkki | b9cd5ec | 2015-04-24 16:05:58 +0300 | [diff] [blame] | 243 | BOARD_TRACKPAD_I2C_ADDR, /* bus */ |
Duncan Laurie | 21a7870 | 2013-05-23 14:17:05 -0700 | [diff] [blame] | 244 | 0, /* device */ |
Christian Walter | e6afab1 | 2019-05-21 17:22:49 +0200 | [diff] [blame] | 245 | 0, /* function */ |
| 246 | SMBIOS_DEVICE_TYPE_OTHER); /* device type */ |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 247 | |
| 248 | return len; |
| 249 | } |
| 250 | |
| 251 | // mainboard_enable is executed as first thing after |
| 252 | // enumerate_buses(). |
| 253 | |
Elyes HAOUAS | d129d43 | 2018-05-04 20:23:33 +0200 | [diff] [blame] | 254 | static void mainboard_enable(struct device *dev) |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 255 | { |
| 256 | dev->ops->init = mainboard_init; |
| 257 | dev->ops->get_smbios_data = butterfly_onboard_smbios_data; |
Vladimir Serbinenko | a2a906e | 2014-09-01 01:41:37 +0200 | [diff] [blame] | 258 | install_intel_vga_int15_handler(GMA_INT15_ACTIVE_LFP_INT_LVDS, GMA_INT15_PANEL_FIT_DEFAULT, GMA_INT15_BOOT_DISPLAY_DEFAULT, 0); |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | struct chip_operations mainboard_ops = { |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 262 | .enable_dev = mainboard_enable, |
| 263 | }; |