Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google Inc. |
| 5 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stddef.h> |
| 21 | #include <arch/cpu.h> |
| 22 | #include <arch/io.h> |
| 23 | #include <arch/cbfs.h> |
| 24 | #include <arch/stages.h> |
| 25 | #include <console/console.h> |
| 26 | #include <cbmem.h> |
| 27 | #include <cpu/x86/mtrr.h> |
| 28 | #include <romstage_handoff.h> |
Aaron Durbin | 794bddf | 2013-09-27 11:38:36 -0500 | [diff] [blame] | 29 | #include <timestamp.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 30 | #include <baytrail/gpio.h> |
| 31 | #include <baytrail/iomap.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 32 | #include <baytrail/lpc.h> |
| 33 | #include <baytrail/pci_devs.h> |
| 34 | #include <baytrail/romstage.h> |
| 35 | |
Aaron Durbin | 794bddf | 2013-09-27 11:38:36 -0500 | [diff] [blame] | 36 | static inline uint64_t timestamp_get(void) |
| 37 | { |
| 38 | return rdtscll(); |
| 39 | } |
| 40 | |
| 41 | static inline tsc_t ts64_to_tsc(uint64_t ts) |
| 42 | { |
| 43 | tsc_t tsc = { |
| 44 | .lo = ts, |
| 45 | .hi = ts >> 32, |
| 46 | }; |
| 47 | return tsc; |
| 48 | } |
| 49 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 50 | /* The cache-as-ram assembly file calls romstage_main() after setting up |
| 51 | * cache-as-ram. romstage_main() will then call the mainboards's |
| 52 | * mainboard_romstage_entry() function. That function then calls |
| 53 | * romstage_common() below. The reason for the back and forth is to provide |
| 54 | * common entry point from cache-as-ram while still allowing for code sharing. |
| 55 | * Because we can't use global variables the stack is used for allocations -- |
| 56 | * thus the need to call back and forth. */ |
| 57 | |
| 58 | static void *setup_stack_and_mttrs(void); |
| 59 | |
| 60 | static void program_base_addresses(void) |
| 61 | { |
| 62 | uint32_t reg; |
| 63 | const uint32_t lpc_dev = PCI_DEV(0, LPC_DEV, LPC_FUNC); |
| 64 | |
| 65 | /* Memory Mapped IO registers. */ |
| 66 | reg = PMC_BASE_ADDRESS | 2; |
| 67 | pci_write_config32(lpc_dev, PBASE, reg); |
| 68 | reg = IO_BASE_ADDRESS | 2; |
| 69 | pci_write_config32(lpc_dev, IOBASE, reg); |
| 70 | reg = ILB_BASE_ADDRESS | 2; |
| 71 | pci_write_config32(lpc_dev, IBASE, reg); |
| 72 | reg = SPI_BASE_ADDRESS | 2; |
| 73 | pci_write_config32(lpc_dev, SBASE, reg); |
| 74 | reg = MPHY_BASE_ADDRESS | 2; |
| 75 | pci_write_config32(lpc_dev, MPBASE, reg); |
Aaron Durbin | a64ef62 | 2013-10-03 12:56:37 -0500 | [diff] [blame] | 76 | reg = PUNIT_BASE_ADDRESS | 2; |
| 77 | pci_write_config32(lpc_dev, PUBASE, reg); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 78 | reg = RCBA_BASE_ADDRESS | 1; |
| 79 | pci_write_config32(lpc_dev, RCBA, reg); |
| 80 | |
| 81 | /* IO Port Registers. */ |
| 82 | reg = ACPI_BASE_ADDRESS | 2; |
| 83 | pci_write_config32(lpc_dev, ABASE, reg); |
| 84 | reg = GPIO_BASE_ADDRESS | 2; |
| 85 | pci_write_config32(lpc_dev, GBASE, reg); |
| 86 | } |
| 87 | |
Aaron Durbin | 794bddf | 2013-09-27 11:38:36 -0500 | [diff] [blame] | 88 | static inline void mark_ts(struct romstage_params *rp, uint64_t ts) |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 89 | { |
Aaron Durbin | 794bddf | 2013-09-27 11:38:36 -0500 | [diff] [blame] | 90 | struct romstage_timestamps *rt = &rp->ts; |
| 91 | |
| 92 | rt->times[rt->count] = ts; |
| 93 | rt->count++; |
| 94 | } |
| 95 | |
| 96 | /* Entry from cache-as-ram.inc. */ |
| 97 | void * asmlinkage romstage_main(unsigned long bist, |
| 98 | uint32_t tsc_low, uint32_t tsc_hi) |
| 99 | { |
| 100 | struct romstage_params rp = { |
| 101 | .bist = bist, |
| 102 | .mrc_params = NULL, |
| 103 | }; |
| 104 | |
| 105 | /* Save initial timestamp from bootblock. */ |
| 106 | mark_ts(&rp, (((uint64_t)tsc_hi) << 32) | (uint64_t)tsc_low); |
| 107 | /* Save romstage begin */ |
| 108 | mark_ts(&rp, timestamp_get()); |
| 109 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 110 | /* Call into mainboard. */ |
Aaron Durbin | 794bddf | 2013-09-27 11:38:36 -0500 | [diff] [blame] | 111 | mainboard_romstage_entry(&rp); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 112 | |
| 113 | return setup_stack_and_mttrs(); |
| 114 | } |
| 115 | |
| 116 | /* Entry from the mainboard. */ |
Aaron Durbin | 794bddf | 2013-09-27 11:38:36 -0500 | [diff] [blame] | 117 | void romstage_common(struct romstage_params *params) |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 118 | { |
| 119 | struct romstage_handoff *handoff; |
| 120 | |
| 121 | program_base_addresses(); |
| 122 | |
Aaron Durbin | fd039f7 | 2013-10-04 11:11:52 -0500 | [diff] [blame] | 123 | tco_disable(); |
| 124 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 125 | byt_config_com1_and_enable(); |
| 126 | |
| 127 | console_init(); |
| 128 | |
Aaron Durbin | bb3ee83 | 2013-10-07 17:12:20 -0500 | [diff] [blame^] | 129 | set_max_freq(); |
| 130 | |
Aaron Durbin | 189aa3e | 2013-10-04 11:17:45 -0500 | [diff] [blame] | 131 | punit_init(); |
| 132 | |
Aaron Durbin | ecf9086 | 2013-09-24 12:36:14 -0500 | [diff] [blame] | 133 | gfx_init(); |
| 134 | |
Aaron Durbin | 794bddf | 2013-09-27 11:38:36 -0500 | [diff] [blame] | 135 | mark_ts(params, timestamp_get()); |
| 136 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 137 | /* Initialize RAM */ |
| 138 | raminit(params->mrc_params, 5); |
| 139 | |
Aaron Durbin | 794bddf | 2013-09-27 11:38:36 -0500 | [diff] [blame] | 140 | mark_ts(params, timestamp_get()); |
| 141 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 142 | handoff = romstage_handoff_find_or_add(); |
| 143 | if (handoff != NULL) |
| 144 | handoff->s3_resume = 0; |
| 145 | else |
| 146 | printk(BIOS_DEBUG, "Romstage handoff structure not added!\n"); |
| 147 | |
Aaron Durbin | 794bddf | 2013-09-27 11:38:36 -0500 | [diff] [blame] | 148 | /* Save timestamp information. */ |
| 149 | timestamp_init(ts64_to_tsc(params->ts.times[0])); |
| 150 | timestamp_add(TS_START_ROMSTAGE, ts64_to_tsc(params->ts.times[1])); |
| 151 | timestamp_add(TS_BEFORE_INITRAM, ts64_to_tsc(params->ts.times[2])); |
| 152 | timestamp_add(TS_AFTER_INITRAM, ts64_to_tsc(params->ts.times[3])); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void open_up_spi(void) |
| 156 | { |
| 157 | const uintptr_t sbase = SPI_BASE_ADDRESS; |
| 158 | |
| 159 | /* Disable generating SMI when setting WPD bit. */ |
| 160 | write32(sbase + 0xf8, read32(sbase + 0xf8) & ~(1 << 7)); |
| 161 | /* Disable the SMM-only BIOS write and set WPD bit. */ |
| 162 | write32(sbase + 0xfc, 1 | (read32(sbase + 0xfc) & ~(1 << 5))); |
| 163 | } |
| 164 | |
| 165 | void asmlinkage romstage_after_car(void) |
| 166 | { |
| 167 | /* Allow BIOS to program SPI part. */ |
| 168 | open_up_spi(); |
| 169 | |
Aaron Durbin | 794bddf | 2013-09-27 11:38:36 -0500 | [diff] [blame] | 170 | timestamp_add_now(TS_END_ROMSTAGE); |
| 171 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 172 | /* Load the ramstage. */ |
| 173 | copy_and_run(); |
| 174 | while (1); |
| 175 | } |
| 176 | |
| 177 | static inline uint32_t *stack_push(u32 *stack, u32 value) |
| 178 | { |
| 179 | stack = &stack[-1]; |
| 180 | *stack = value; |
| 181 | return stack; |
| 182 | } |
| 183 | |
| 184 | /* Romstage needs quite a bit of stack for decompressing images since the lzma |
| 185 | * lib keeps its state on the stack during romstage. */ |
| 186 | static unsigned long choose_top_of_stack(void) |
| 187 | { |
| 188 | unsigned long stack_top; |
| 189 | const unsigned long romstage_ram_stack_size = 0x5000; |
| 190 | |
| 191 | /* cbmem_add() does a find() before add(). */ |
| 192 | stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK, |
| 193 | romstage_ram_stack_size); |
| 194 | stack_top += romstage_ram_stack_size; |
| 195 | return stack_top; |
| 196 | } |
| 197 | |
| 198 | /* setup_stack_and_mttrs() determines the stack to use after |
| 199 | * cache-as-ram is torn down as well as the MTRR settings to use. */ |
| 200 | static void *setup_stack_and_mttrs(void) |
| 201 | { |
| 202 | unsigned long top_of_stack; |
| 203 | int num_mtrrs; |
| 204 | uint32_t *slot; |
| 205 | uint32_t mtrr_mask_upper; |
| 206 | uint32_t top_of_ram; |
| 207 | |
| 208 | /* Top of stack needs to be aligned to a 4-byte boundary. */ |
| 209 | top_of_stack = choose_top_of_stack() & ~3; |
| 210 | slot = (void *)top_of_stack; |
| 211 | num_mtrrs = 0; |
| 212 | |
| 213 | /* The upper bits of the MTRR mask need to set according to the number |
| 214 | * of physical address bits. */ |
| 215 | mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1; |
| 216 | |
| 217 | /* The order for each MTRR is value then base with upper 32-bits of |
| 218 | * each value coming before the lower 32-bits. The reasoning for |
| 219 | * this ordering is to create a stack layout like the following: |
| 220 | * +0: Number of MTRRs |
| 221 | * +4: MTRR base 0 31:0 |
| 222 | * +8: MTRR base 0 63:32 |
| 223 | * +12: MTRR mask 0 31:0 |
| 224 | * +16: MTRR mask 0 63:32 |
| 225 | * +20: MTRR base 1 31:0 |
| 226 | * +24: MTRR base 1 63:32 |
| 227 | * +28: MTRR mask 1 31:0 |
| 228 | * +32: MTRR mask 1 63:32 |
| 229 | */ |
| 230 | |
| 231 | /* Cache the ROM as WP just below 4GiB. */ |
| 232 | slot = stack_push(slot, mtrr_mask_upper); /* upper mask */ |
| 233 | slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid); |
| 234 | slot = stack_push(slot, 0); /* upper base */ |
| 235 | slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT); |
| 236 | num_mtrrs++; |
| 237 | |
| 238 | /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */ |
| 239 | slot = stack_push(slot, mtrr_mask_upper); /* upper mask */ |
| 240 | slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid); |
| 241 | slot = stack_push(slot, 0); /* upper base */ |
| 242 | slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK); |
| 243 | num_mtrrs++; |
| 244 | |
| 245 | top_of_ram = (uint32_t)cbmem_top(); |
| 246 | /* Cache 8MiB below the top of ram. The top of ram under 4GiB is the |
| 247 | * start of the TSEG region. It is required to be 8MiB aligned. Set |
| 248 | * this area as cacheable so it can be used later for ramstage before |
| 249 | * setting up the entire RAM as cacheable. */ |
| 250 | slot = stack_push(slot, mtrr_mask_upper); /* upper mask */ |
| 251 | slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid); |
| 252 | slot = stack_push(slot, 0); /* upper base */ |
| 253 | slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK); |
| 254 | num_mtrrs++; |
| 255 | |
| 256 | /* Cache 8MiB at the top of ram. Top of ram is where the TSEG |
| 257 | * region resides. However, it is not restricted to SMM mode until |
| 258 | * SMM has been relocated. By setting the region to cacheable it |
| 259 | * provides faster access when relocating the SMM handler as well |
| 260 | * as using the TSEG region for other purposes. */ |
| 261 | slot = stack_push(slot, mtrr_mask_upper); /* upper mask */ |
| 262 | slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid); |
| 263 | slot = stack_push(slot, 0); /* upper base */ |
| 264 | slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK); |
| 265 | num_mtrrs++; |
| 266 | |
| 267 | /* Save the number of MTRRs to setup. Return the stack location |
| 268 | * pointing to the number of MTRRs. */ |
| 269 | slot = stack_push(slot, num_mtrrs); |
| 270 | |
| 271 | return slot; |
| 272 | } |