Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 4 | * Copyright 2013 Google Inc. |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 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 <types.h> |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 22 | |
| 23 | #include <armv7.h> |
| 24 | #include <cbfs.h> |
Stefan Reinauer | 80e6293 | 2013-07-29 15:52:23 -0700 | [diff] [blame] | 25 | #include <cbmem.h> |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 26 | |
| 27 | #include <arch/cache.h> |
Hung-Te Lin | 22d0ca0 | 2013-09-27 12:45:45 +0800 | [diff] [blame] | 28 | #include <soc/samsung/exynos5420/i2c.h> |
| 29 | #include <soc/samsung/exynos5420/clk.h> |
| 30 | #include <soc/samsung/exynos5420/cpu.h> |
| 31 | #include <soc/samsung/exynos5420/dmc.h> |
| 32 | #include <soc/samsung/exynos5420/gpio.h> |
| 33 | #include <soc/samsung/exynos5420/setup.h> |
| 34 | #include <soc/samsung/exynos5420/periph.h> |
| 35 | #include <soc/samsung/exynos5420/power.h> |
| 36 | #include <soc/samsung/exynos5420/trustzone.h> |
| 37 | #include <soc/samsung/exynos5420/wakeup.h> |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 38 | #include <console/console.h> |
| 39 | #include <arch/stages.h> |
| 40 | |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 41 | #include <drivers/maxim/max77802/max77802.h> |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 42 | #include <device/i2c.h> |
| 43 | |
David Hendricks | 77acf42 | 2013-08-05 21:04:16 -0700 | [diff] [blame] | 44 | #define PMIC_I2C_BUS 4 |
| 45 | |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 46 | struct pmic_write |
| 47 | { |
| 48 | int or_orig; // Whether to or in the original value. |
| 49 | uint8_t reg; // Register to write. |
| 50 | uint8_t val; // Value to write. |
| 51 | }; |
| 52 | |
| 53 | /* |
| 54 | * Use read-modify-write for MAX77802 control registers and clobber the |
| 55 | * output voltage setting (BUCK?DVS?) registers. |
| 56 | */ |
| 57 | struct pmic_write pmic_writes[] = |
| 58 | { |
| 59 | { 1, MAX77802_REG_PMIC_32KHZ, MAX77802_32KHCP_EN }, |
| 60 | { 0, MAX77802_REG_PMIC_BUCK1DVS1, MAX77802_BUCK1DVS1_1V }, |
| 61 | { 1, MAX77802_REG_PMIC_BUCK1CTRL, MAX77802_BUCK_TYPE1_ON | |
| 62 | MAX77802_BUCK_TYPE1_IGNORE_PWRREQ }, |
David Hendricks | 1f9f04e | 2013-08-01 18:57:52 -0700 | [diff] [blame] | 63 | { 0, MAX77802_REG_PMIC_BUCK2DVS1, MAX77802_BUCK2DVS1_1_2625V }, |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 64 | { 1, MAX77802_REG_PMIC_BUCK2CTRL1, MAX77802_BUCK_TYPE2_ON | |
| 65 | MAX77802_BUCK_TYPE2_IGNORE_PWRREQ }, |
| 66 | { 0, MAX77802_REG_PMIC_BUCK3DVS1, MAX77802_BUCK3DVS1_1V }, |
| 67 | { 1, MAX77802_REG_PMIC_BUCK3CTRL1, MAX77802_BUCK_TYPE2_ON | |
| 68 | MAX77802_BUCK_TYPE2_IGNORE_PWRREQ }, |
| 69 | { 0, MAX77802_REG_PMIC_BUCK4DVS1, MAX77802_BUCK4DVS1_1V }, |
| 70 | { 1, MAX77802_REG_PMIC_BUCK4CTRL1, MAX77802_BUCK_TYPE2_ON | |
| 71 | MAX77802_BUCK_TYPE2_IGNORE_PWRREQ }, |
| 72 | { 0, MAX77802_REG_PMIC_BUCK6DVS1, MAX77802_BUCK6DVS1_1V }, |
| 73 | { 1, MAX77802_REG_PMIC_BUCK6CTRL, MAX77802_BUCK_TYPE1_ON | |
Ronald G. Minnich | 88ac9b5 | 2013-06-26 17:28:52 -0700 | [diff] [blame] | 74 | MAX77802_BUCK_TYPE1_IGNORE_PWRREQ }, |
David Hendricks | 1f9f04e | 2013-08-01 18:57:52 -0700 | [diff] [blame] | 75 | /* Disable Boost(bypass) OUTPUT */ |
| 76 | { 0, MAX77802_REG_PMIC_BOOSTCTRL, MAX77802_BOOSTCTRL_OFF}, |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
David Hendricks | 77acf42 | 2013-08-05 21:04:16 -0700 | [diff] [blame] | 79 | static int setup_power(int is_resume) |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 80 | { |
| 81 | int error = 0; |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 82 | int i; |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 83 | |
| 84 | power_init(); |
| 85 | |
Hung-Te Lin | da7b8e4 | 2013-06-28 17:27:17 +0800 | [diff] [blame] | 86 | if (is_resume) { |
David Hendricks | 77acf42 | 2013-08-05 21:04:16 -0700 | [diff] [blame] | 87 | return 0; |
Hung-Te Lin | da7b8e4 | 2013-06-28 17:27:17 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 90 | /* Initialize I2C bus to configure PMIC. */ |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 91 | exynos_pinmux_i2c4(); |
David Hendricks | 77acf42 | 2013-08-05 21:04:16 -0700 | [diff] [blame] | 92 | i2c_init(PMIC_I2C_BUS, 1000000, 0x00); /* 1MHz */ |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 93 | |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 94 | for (i = 0; i < ARRAY_SIZE(pmic_writes); i++) { |
| 95 | uint8_t data = 0; |
| 96 | uint8_t reg = pmic_writes[i].reg; |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 97 | |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 98 | if (pmic_writes[i].or_orig) |
| 99 | error |= i2c_read(4, MAX77802_I2C_ADDR, |
| 100 | reg, sizeof(reg), |
| 101 | &data, sizeof(data)); |
| 102 | data |= pmic_writes[i].val; |
| 103 | error |= i2c_write(4, MAX77802_I2C_ADDR, |
| 104 | reg, sizeof(reg), |
| 105 | &data, sizeof(data)); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 106 | } |
David Hendricks | 1e3e2c5 | 2013-06-14 16:08:05 -0700 | [diff] [blame] | 107 | |
David Hendricks | 77acf42 | 2013-08-05 21:04:16 -0700 | [diff] [blame] | 108 | return error; |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Hung-Te Lin | c357aed | 2013-06-24 20:02:01 +0800 | [diff] [blame] | 111 | static void setup_ec(void) |
| 112 | { |
| 113 | /* SPI2 (EC) is slower and needs to work in half-duplex mode with |
| 114 | * single byte bus width. */ |
Gabe Black | 9801809 | 2013-07-24 06:18:20 -0700 | [diff] [blame] | 115 | clock_set_rate(PERIPH_ID_SPI2, 5000000); |
Hung-Te Lin | c357aed | 2013-06-24 20:02:01 +0800 | [diff] [blame] | 116 | exynos_pinmux_spi2(); |
| 117 | } |
| 118 | |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 119 | static void setup_gpio(void) |
| 120 | { |
Gabe Black | 63bb610 | 2013-06-19 03:29:45 -0700 | [diff] [blame] | 121 | gpio_direction_input(GPIO_X30); // WP_GPIO |
| 122 | gpio_set_pull(GPIO_X30, GPIO_PULL_NONE); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 123 | |
Gabe Black | 63bb610 | 2013-06-19 03:29:45 -0700 | [diff] [blame] | 124 | gpio_direction_input(GPIO_X07); // RECMODE_GPIO |
| 125 | gpio_set_pull(GPIO_X07, GPIO_PULL_NONE); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 126 | |
Gabe Black | 63bb610 | 2013-06-19 03:29:45 -0700 | [diff] [blame] | 127 | gpio_direction_input(GPIO_X34); // LID_GPIO |
| 128 | gpio_set_pull(GPIO_X34, GPIO_PULL_NONE); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 129 | |
Gabe Black | 63bb610 | 2013-06-19 03:29:45 -0700 | [diff] [blame] | 130 | gpio_direction_input(GPIO_X12); // POWER_GPIO |
| 131 | gpio_set_pull(GPIO_X12, GPIO_PULL_NONE); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void setup_memory(struct mem_timings *mem, int is_resume) |
| 135 | { |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 136 | printk(BIOS_SPEW, "manufacturer: 0x%x type: 0x%x, div: 0x%x, mhz: %d\n", |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 137 | mem->mem_manuf, |
| 138 | mem->mem_type, |
| 139 | mem->mpll_mdiv, |
| 140 | mem->frequency_mhz); |
| 141 | |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 142 | if (ddr3_mem_ctrl_init(mem, DMC_INTERLEAVE_SIZE, !is_resume)) { |
| 143 | die("Failed to initialize memory controller.\n"); |
| 144 | } |
| 145 | } |
| 146 | |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 147 | #define PRIMITIVE_MEM_TEST 0 |
| 148 | #if PRIMITIVE_MEM_TEST |
| 149 | static unsigned long primitive_mem_test(void) |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 150 | { |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 151 | unsigned long *l = (void *)0x40000000; |
| 152 | int bad = 0; |
| 153 | unsigned long i; |
| 154 | for(i = 0; i < 256*1048576; i++){ |
| 155 | if (! (i%1048576)) |
| 156 | printk(BIOS_SPEW, "%lu ...", i); |
| 157 | l[i] = 0xffffffff - i; |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 158 | } |
Gabe Black | 5420e09 | 2013-05-17 11:29:22 -0700 | [diff] [blame] | 159 | |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 160 | for(i = 0; i < 256*1048576; i++){ |
| 161 | if (! (i%1048576)) |
| 162 | printk(BIOS_SPEW, "%lu ...", i); |
| 163 | if (l[i] != (0xffffffff - i)){ |
| 164 | printk(BIOS_SPEW, "%p: want %08lx got %08lx\n", l, l[i], 0xffffffff - i); |
| 165 | bad++; |
| 166 | } |
| 167 | } |
Gabe Black | 5420e09 | 2013-05-17 11:29:22 -0700 | [diff] [blame] | 168 | |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 169 | printk(BIOS_SPEW, "%d errors\n", bad); |
| 170 | |
| 171 | return bad; |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 172 | } |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 173 | #else |
| 174 | #define primitive_mem_test() |
| 175 | #endif |
| 176 | |
| 177 | #define SIMPLE_SPI_TEST 0 |
| 178 | #if SIMPLE_SPI_TEST |
| 179 | /* here is a simple SPI debug test, known to fid trouble */ |
| 180 | static void simple_spi_test(void) |
| 181 | { |
| 182 | struct cbfs_media default_media, *media; |
| 183 | int i, amt = 4 * MiB, errors = 0; |
| 184 | //u32 *data = (void *)0x40000000; |
| 185 | u32 data[1024]; |
| 186 | u32 in; |
| 187 | |
| 188 | amt = sizeof(data); |
| 189 | media = &default_media; |
| 190 | if (init_default_cbfs_media(media) != 0) { |
| 191 | printk(BIOS_SPEW, "Failed to initialize default media.\n"); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | media->open(media); |
| 197 | if (media->read(media, data, (size_t) 0, amt) < amt){ |
| 198 | printk(BIOS_SPEW, "simple_spi_test fails\n"); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | for(i = 0; i < amt; i += 4){ |
| 204 | if (media->read(media, &in, (size_t) i, 4) < 1){ |
| 205 | printk(BIOS_SPEW, "simple_spi_test fails at %d\n", i); |
| 206 | return; |
| 207 | } |
| 208 | if (data[i/4] != in){ |
| 209 | errors++; |
| 210 | printk(BIOS_SPEW, "BAD at %d(%p):\nRAM %08lx\nSPI %08lx\n", |
| 211 | i, &data[i/4], (unsigned long)data[i/4], (unsigned long)in); |
| 212 | /* reread it to see which is wrong. */ |
| 213 | if (media->read(media, &in, (size_t) i, 4) < 1){ |
| 214 | printk(BIOS_SPEW, "simple_spi_test fails at %d\n", i); |
| 215 | return; |
| 216 | } |
| 217 | printk(BIOS_SPEW, "RTRY at %d(%p):\nRAM %08lx\nSPI %08lx\n", |
| 218 | i, &data[i/4], (unsigned long)data[i/4], (unsigned long)in); |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | printk(BIOS_SPEW, "%d errors\n", errors); |
| 223 | } |
| 224 | #else |
| 225 | #define simple_spi_test() |
| 226 | #endif |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 227 | |
| 228 | void main(void) |
| 229 | { |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 230 | |
| 231 | extern struct mem_timings mem_timings; |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 232 | void *entry; |
| 233 | int is_resume = (get_wakeup_state() != IS_NOT_WAKEUP); |
David Hendricks | 77acf42 | 2013-08-05 21:04:16 -0700 | [diff] [blame] | 234 | int power_init_failed; |
| 235 | |
Hung-Te Lin | 0682cfe | 2013-08-06 20:37:55 +0800 | [diff] [blame] | 236 | exynos5420_config_smp(); |
David Hendricks | 77acf42 | 2013-08-05 21:04:16 -0700 | [diff] [blame] | 237 | power_init_failed = setup_power(is_resume); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 238 | |
| 239 | /* Clock must be initialized before console_init, otherwise you may need |
| 240 | * to re-initialize serial console drivers again. */ |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 241 | system_clock_init(); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 242 | |
Gabe Black | 136e709 | 2013-08-09 00:31:09 -0700 | [diff] [blame] | 243 | exynos_pinmux_uart3(); |
Stefan Reinauer | 998ab0d | 2013-05-20 12:29:37 -0700 | [diff] [blame] | 244 | console_init(); |
| 245 | |
David Hendricks | 77acf42 | 2013-08-05 21:04:16 -0700 | [diff] [blame] | 246 | if (power_init_failed) |
| 247 | die("Failed to intialize power.\n"); |
| 248 | |
| 249 | /* re-initialize PMIC I2C channel after (re-)setting system clocks */ |
| 250 | i2c_init(PMIC_I2C_BUS, 1000000, 0x00); /* 1MHz */ |
| 251 | |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 252 | setup_memory(&mem_timings, is_resume); |
| 253 | |
| 254 | primitive_mem_test(); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 255 | |
Gabe Black | 8128a56 | 2013-09-18 05:48:37 -0700 | [diff] [blame] | 256 | trustzone_init(); |
| 257 | |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 258 | if (is_resume) { |
| 259 | wakeup(); |
| 260 | } |
| 261 | |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 262 | setup_gpio(); |
Hung-Te Lin | c357aed | 2013-06-24 20:02:01 +0800 | [diff] [blame] | 263 | setup_ec(); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 264 | |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 265 | simple_spi_test(); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 266 | /* Set SPI (primary CBFS media) clock to 50MHz. */ |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 267 | /* if this is uncommented SPI will not work correctly. */ |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 268 | clock_set_rate(PERIPH_ID_SPI1, 50000000); |
Julius Werner | 45d2ff3 | 2013-08-12 18:04:06 -0700 | [diff] [blame] | 269 | exynos_pinmux_spi1(); |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 270 | simple_spi_test(); |
Stefan Reinauer | 80e6293 | 2013-07-29 15:52:23 -0700 | [diff] [blame] | 271 | |
| 272 | cbmem_initialize_empty(); |
| 273 | |
Furquan Shaikh | 20f25dd | 2014-04-22 10:41:05 -0700 | [diff] [blame] | 274 | entry = cbfs_load_stage(CBFS_DEFAULT_MEDIA, "fallback/ramstage"); |
Ronald G. Minnich | e6af929 | 2013-06-03 13:03:50 -0700 | [diff] [blame] | 275 | simple_spi_test(); |
Gabe Black | d3163ab | 2013-05-16 05:53:40 -0700 | [diff] [blame] | 276 | stage_exit(entry); |
| 277 | } |