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