blob: 16dc997e753b779623f023a95299601c1eeefb13 [file] [log] [blame]
Gabe Blackd3163ab2013-05-16 05:53:40 -07001/*
2 * This file is part of the coreboot project.
3 *
David Hendricks1e3e2c52013-06-14 16:08:05 -07004 * Copyright 2013 Google Inc.
Gabe Blackd3163ab2013-05-16 05:53:40 -07005 *
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 Hendricks1e3e2c52013-06-14 16:08:05 -070021#include <stdlib.h>
Gabe Blackd3163ab2013-05-16 05:53:40 -070022
23#include <armv7.h>
24#include <cbfs.h>
Stefan Reinauer80e62932013-07-29 15:52:23 -070025#include <cbmem.h>
Gabe Blackd3163ab2013-05-16 05:53:40 -070026
27#include <arch/cache.h>
Julius Werner85620db2013-11-13 18:22:15 -080028#include <arch/exception.h>
Hung-Te Lin22d0ca02013-09-27 12:45:45 +080029#include <soc/samsung/exynos5420/i2c.h>
30#include <soc/samsung/exynos5420/clk.h>
31#include <soc/samsung/exynos5420/cpu.h>
32#include <soc/samsung/exynos5420/dmc.h>
33#include <soc/samsung/exynos5420/gpio.h>
34#include <soc/samsung/exynos5420/setup.h>
35#include <soc/samsung/exynos5420/periph.h>
36#include <soc/samsung/exynos5420/power.h>
37#include <soc/samsung/exynos5420/trustzone.h>
38#include <soc/samsung/exynos5420/wakeup.h>
Gabe Blackd3163ab2013-05-16 05:53:40 -070039#include <console/console.h>
40#include <arch/stages.h>
41
David Hendricks1e3e2c52013-06-14 16:08:05 -070042#include <drivers/maxim/max77802/max77802.h>
Gabe Blackd3163ab2013-05-16 05:53:40 -070043#include <device/i2c.h>
44
David Hendricks77acf422013-08-05 21:04:16 -070045#define PMIC_I2C_BUS 4
46
David Hendricks1e3e2c52013-06-14 16:08:05 -070047struct pmic_write
48{
49 int or_orig; // Whether to or in the original value.
50 uint8_t reg; // Register to write.
51 uint8_t val; // Value to write.
52};
53
54/*
55 * Use read-modify-write for MAX77802 control registers and clobber the
56 * output voltage setting (BUCK?DVS?) registers.
57 */
58struct pmic_write pmic_writes[] =
59{
60 { 1, MAX77802_REG_PMIC_32KHZ, MAX77802_32KHCP_EN },
61 { 0, MAX77802_REG_PMIC_BUCK1DVS1, MAX77802_BUCK1DVS1_1V },
62 { 1, MAX77802_REG_PMIC_BUCK1CTRL, MAX77802_BUCK_TYPE1_ON |
63 MAX77802_BUCK_TYPE1_IGNORE_PWRREQ },
David Hendricks1f9f04e2013-08-01 18:57:52 -070064 { 0, MAX77802_REG_PMIC_BUCK2DVS1, MAX77802_BUCK2DVS1_1_2625V },
David Hendricks1e3e2c52013-06-14 16:08:05 -070065 { 1, MAX77802_REG_PMIC_BUCK2CTRL1, MAX77802_BUCK_TYPE2_ON |
66 MAX77802_BUCK_TYPE2_IGNORE_PWRREQ },
67 { 0, MAX77802_REG_PMIC_BUCK3DVS1, MAX77802_BUCK3DVS1_1V },
68 { 1, MAX77802_REG_PMIC_BUCK3CTRL1, MAX77802_BUCK_TYPE2_ON |
69 MAX77802_BUCK_TYPE2_IGNORE_PWRREQ },
70 { 0, MAX77802_REG_PMIC_BUCK4DVS1, MAX77802_BUCK4DVS1_1V },
71 { 1, MAX77802_REG_PMIC_BUCK4CTRL1, MAX77802_BUCK_TYPE2_ON |
72 MAX77802_BUCK_TYPE2_IGNORE_PWRREQ },
73 { 0, MAX77802_REG_PMIC_BUCK6DVS1, MAX77802_BUCK6DVS1_1V },
74 { 1, MAX77802_REG_PMIC_BUCK6CTRL, MAX77802_BUCK_TYPE1_ON |
Ronald G. Minnich88ac9b52013-06-26 17:28:52 -070075 MAX77802_BUCK_TYPE1_IGNORE_PWRREQ },
David Hendricks1f9f04e2013-08-01 18:57:52 -070076 /* Disable Boost(bypass) OUTPUT */
77 { 0, MAX77802_REG_PMIC_BOOSTCTRL, MAX77802_BOOSTCTRL_OFF},
David Hendricks1e3e2c52013-06-14 16:08:05 -070078};
79
David Hendricks77acf422013-08-05 21:04:16 -070080static int setup_power(int is_resume)
Gabe Blackd3163ab2013-05-16 05:53:40 -070081{
82 int error = 0;
David Hendricks1e3e2c52013-06-14 16:08:05 -070083 int i;
Gabe Blackd3163ab2013-05-16 05:53:40 -070084
85 power_init();
86
Hung-Te Linda7b8e42013-06-28 17:27:17 +080087 if (is_resume) {
David Hendricks77acf422013-08-05 21:04:16 -070088 return 0;
Hung-Te Linda7b8e42013-06-28 17:27:17 +080089 }
90
Gabe Blackd3163ab2013-05-16 05:53:40 -070091 /* Initialize I2C bus to configure PMIC. */
David Hendricks1e3e2c52013-06-14 16:08:05 -070092 exynos_pinmux_i2c4();
David Hendricks77acf422013-08-05 21:04:16 -070093 i2c_init(PMIC_I2C_BUS, 1000000, 0x00); /* 1MHz */
Gabe Blackd3163ab2013-05-16 05:53:40 -070094
David Hendricks1e3e2c52013-06-14 16:08:05 -070095 for (i = 0; i < ARRAY_SIZE(pmic_writes); i++) {
96 uint8_t data = 0;
97 uint8_t reg = pmic_writes[i].reg;
Gabe Blackd3163ab2013-05-16 05:53:40 -070098
David Hendricks1e3e2c52013-06-14 16:08:05 -070099 if (pmic_writes[i].or_orig)
100 error |= i2c_read(4, MAX77802_I2C_ADDR,
101 reg, sizeof(reg),
102 &data, sizeof(data));
103 data |= pmic_writes[i].val;
104 error |= i2c_write(4, MAX77802_I2C_ADDR,
105 reg, sizeof(reg),
106 &data, sizeof(data));
Gabe Blackd3163ab2013-05-16 05:53:40 -0700107 }
David Hendricks1e3e2c52013-06-14 16:08:05 -0700108
David Hendricks77acf422013-08-05 21:04:16 -0700109 return error;
Gabe Blackd3163ab2013-05-16 05:53:40 -0700110}
111
Hung-Te Linc357aed2013-06-24 20:02:01 +0800112static void setup_ec(void)
113{
114 /* SPI2 (EC) is slower and needs to work in half-duplex mode with
115 * single byte bus width. */
Gabe Black98018092013-07-24 06:18:20 -0700116 clock_set_rate(PERIPH_ID_SPI2, 5000000);
Hung-Te Linc357aed2013-06-24 20:02:01 +0800117 exynos_pinmux_spi2();
118}
119
Gabe Blackd3163ab2013-05-16 05:53:40 -0700120static void setup_gpio(void)
121{
Gabe Black63bb6102013-06-19 03:29:45 -0700122 gpio_direction_input(GPIO_X30); // WP_GPIO
123 gpio_set_pull(GPIO_X30, GPIO_PULL_NONE);
Gabe Blackd3163ab2013-05-16 05:53:40 -0700124
Gabe Black63bb6102013-06-19 03:29:45 -0700125 gpio_direction_input(GPIO_X07); // RECMODE_GPIO
126 gpio_set_pull(GPIO_X07, GPIO_PULL_NONE);
Gabe Blackd3163ab2013-05-16 05:53:40 -0700127
Gabe Black63bb6102013-06-19 03:29:45 -0700128 gpio_direction_input(GPIO_X34); // LID_GPIO
129 gpio_set_pull(GPIO_X34, GPIO_PULL_NONE);
Gabe Blackd3163ab2013-05-16 05:53:40 -0700130
Gabe Black63bb6102013-06-19 03:29:45 -0700131 gpio_direction_input(GPIO_X12); // POWER_GPIO
132 gpio_set_pull(GPIO_X12, GPIO_PULL_NONE);
Gabe Blackd3163ab2013-05-16 05:53:40 -0700133}
134
135static void setup_memory(struct mem_timings *mem, int is_resume)
136{
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700137 printk(BIOS_SPEW, "manufacturer: 0x%x type: 0x%x, div: 0x%x, mhz: %d\n",
Gabe Blackd3163ab2013-05-16 05:53:40 -0700138 mem->mem_manuf,
139 mem->mem_type,
140 mem->mpll_mdiv,
141 mem->frequency_mhz);
142
Gabe Blackd3163ab2013-05-16 05:53:40 -0700143 if (ddr3_mem_ctrl_init(mem, DMC_INTERLEAVE_SIZE, !is_resume)) {
144 die("Failed to initialize memory controller.\n");
145 }
146}
147
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700148#define PRIMITIVE_MEM_TEST 0
149#if PRIMITIVE_MEM_TEST
150static unsigned long primitive_mem_test(void)
Gabe Blackd3163ab2013-05-16 05:53:40 -0700151{
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700152 unsigned long *l = (void *)0x40000000;
153 int bad = 0;
154 unsigned long i;
155 for(i = 0; i < 256*1048576; i++){
156 if (! (i%1048576))
157 printk(BIOS_SPEW, "%lu ...", i);
158 l[i] = 0xffffffff - i;
Gabe Blackd3163ab2013-05-16 05:53:40 -0700159 }
Gabe Black5420e092013-05-17 11:29:22 -0700160
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700161 for(i = 0; i < 256*1048576; i++){
162 if (! (i%1048576))
163 printk(BIOS_SPEW, "%lu ...", i);
164 if (l[i] != (0xffffffff - i)){
165 printk(BIOS_SPEW, "%p: want %08lx got %08lx\n", l, l[i], 0xffffffff - i);
166 bad++;
167 }
168 }
Gabe Black5420e092013-05-17 11:29:22 -0700169
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700170 printk(BIOS_SPEW, "%d errors\n", bad);
171
172 return bad;
Gabe Blackd3163ab2013-05-16 05:53:40 -0700173}
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700174#else
175#define primitive_mem_test()
176#endif
177
178#define SIMPLE_SPI_TEST 0
179#if SIMPLE_SPI_TEST
180/* here is a simple SPI debug test, known to fid trouble */
181static void simple_spi_test(void)
182{
183 struct cbfs_media default_media, *media;
184 int i, amt = 4 * MiB, errors = 0;
185 //u32 *data = (void *)0x40000000;
186 u32 data[1024];
187 u32 in;
188
189 amt = sizeof(data);
190 media = &default_media;
191 if (init_default_cbfs_media(media) != 0) {
192 printk(BIOS_SPEW, "Failed to initialize default media.\n");
193 return;
194 }
195
196
197 media->open(media);
198 if (media->read(media, data, (size_t) 0, amt) < amt){
199 printk(BIOS_SPEW, "simple_spi_test fails\n");
200 return;
201 }
202
203
204 for(i = 0; i < amt; i += 4){
205 if (media->read(media, &in, (size_t) i, 4) < 1){
206 printk(BIOS_SPEW, "simple_spi_test fails at %d\n", i);
207 return;
208 }
209 if (data[i/4] != in){
210 errors++;
211 printk(BIOS_SPEW, "BAD at %d(%p):\nRAM %08lx\nSPI %08lx\n",
212 i, &data[i/4], (unsigned long)data[i/4], (unsigned long)in);
213 /* reread it to see which is wrong. */
214 if (media->read(media, &in, (size_t) i, 4) < 1){
215 printk(BIOS_SPEW, "simple_spi_test fails at %d\n", i);
216 return;
217 }
218 printk(BIOS_SPEW, "RTRY at %d(%p):\nRAM %08lx\nSPI %08lx\n",
219 i, &data[i/4], (unsigned long)data[i/4], (unsigned long)in);
220 }
221
222 }
223 printk(BIOS_SPEW, "%d errors\n", errors);
224}
225#else
226#define simple_spi_test()
227#endif
Gabe Blackd3163ab2013-05-16 05:53:40 -0700228
229void main(void)
230{
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700231
232 extern struct mem_timings mem_timings;
Gabe Blackd3163ab2013-05-16 05:53:40 -0700233 void *entry;
234 int is_resume = (get_wakeup_state() != IS_NOT_WAKEUP);
David Hendricks77acf422013-08-05 21:04:16 -0700235 int power_init_failed;
236
Hung-Te Lin0682cfe2013-08-06 20:37:55 +0800237 exynos5420_config_smp();
David Hendricks77acf422013-08-05 21:04:16 -0700238 power_init_failed = setup_power(is_resume);
Gabe Blackd3163ab2013-05-16 05:53:40 -0700239
240 /* Clock must be initialized before console_init, otherwise you may need
241 * to re-initialize serial console drivers again. */
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700242 system_clock_init();
Gabe Blackd3163ab2013-05-16 05:53:40 -0700243
Gabe Black136e7092013-08-09 00:31:09 -0700244 exynos_pinmux_uart3();
Stefan Reinauer998ab0d2013-05-20 12:29:37 -0700245 console_init();
Julius Werner85620db2013-11-13 18:22:15 -0800246 exception_init();
Stefan Reinauer998ab0d2013-05-20 12:29:37 -0700247
David Hendricks77acf422013-08-05 21:04:16 -0700248 if (power_init_failed)
249 die("Failed to intialize power.\n");
250
251 /* re-initialize PMIC I2C channel after (re-)setting system clocks */
252 i2c_init(PMIC_I2C_BUS, 1000000, 0x00); /* 1MHz */
253
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700254 setup_memory(&mem_timings, is_resume);
255
256 primitive_mem_test();
Gabe Blackd3163ab2013-05-16 05:53:40 -0700257
Gabe Black8128a562013-09-18 05:48:37 -0700258 trustzone_init();
259
Gabe Blackd3163ab2013-05-16 05:53:40 -0700260 if (is_resume) {
261 wakeup();
262 }
263
Gabe Blackd3163ab2013-05-16 05:53:40 -0700264 setup_gpio();
Hung-Te Linc357aed2013-06-24 20:02:01 +0800265 setup_ec();
Gabe Blackd3163ab2013-05-16 05:53:40 -0700266
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700267 simple_spi_test();
Gabe Blackd3163ab2013-05-16 05:53:40 -0700268 /* Set SPI (primary CBFS media) clock to 50MHz. */
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700269 /* if this is uncommented SPI will not work correctly. */
Gabe Blackd3163ab2013-05-16 05:53:40 -0700270 clock_set_rate(PERIPH_ID_SPI1, 50000000);
Julius Werner45d2ff32013-08-12 18:04:06 -0700271 exynos_pinmux_spi1();
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700272 simple_spi_test();
Stefan Reinauer80e62932013-07-29 15:52:23 -0700273
274 cbmem_initialize_empty();
275
Furquan Shaikh20f25dd2014-04-22 10:41:05 -0700276 entry = cbfs_load_stage(CBFS_DEFAULT_MEDIA, "fallback/ramstage");
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700277 simple_spi_test();
Gabe Blackd3163ab2013-05-16 05:53:40 -0700278 stage_exit(entry);
279}