blob: c11f712e65598aff602bcc8784a763d583a65390 [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>
28#include <cpu/samsung/exynos5420/i2c.h>
29#include <cpu/samsung/exynos5420/clk.h>
30#include <cpu/samsung/exynos5420/cpu.h>
31#include <cpu/samsung/exynos5420/dmc.h>
32#include <cpu/samsung/exynos5420/gpio.h>
33#include <cpu/samsung/exynos5420/setup.h>
34#include <cpu/samsung/exynos5420/periph.h>
35#include <cpu/samsung/exynos5420/power.h>
36#include <cpu/samsung/exynos5420/wakeup.h>
37#include <console/console.h>
38#include <arch/stages.h>
39
David Hendricks1e3e2c52013-06-14 16:08:05 -070040#include <drivers/maxim/max77802/max77802.h>
Gabe Blackd3163ab2013-05-16 05:53:40 -070041#include <device/i2c.h>
42
Gabe Blackd3163ab2013-05-16 05:53:40 -070043#define MMC0_GPIO_PIN (58)
44
David Hendricks1e3e2c52013-06-14 16:08:05 -070045struct pmic_write
46{
47 int or_orig; // Whether to or in the original value.
48 uint8_t reg; // Register to write.
49 uint8_t val; // Value to write.
50};
51
52/*
53 * Use read-modify-write for MAX77802 control registers and clobber the
54 * output voltage setting (BUCK?DVS?) registers.
55 */
56struct pmic_write pmic_writes[] =
57{
58 { 1, MAX77802_REG_PMIC_32KHZ, MAX77802_32KHCP_EN },
59 { 0, MAX77802_REG_PMIC_BUCK1DVS1, MAX77802_BUCK1DVS1_1V },
60 { 1, MAX77802_REG_PMIC_BUCK1CTRL, MAX77802_BUCK_TYPE1_ON |
61 MAX77802_BUCK_TYPE1_IGNORE_PWRREQ },
62 { 0, MAX77802_REG_PMIC_BUCK2DVS1, MAX77802_BUCK2DVS1_1V },
63 { 1, MAX77802_REG_PMIC_BUCK2CTRL1, MAX77802_BUCK_TYPE2_ON |
64 MAX77802_BUCK_TYPE2_IGNORE_PWRREQ },
65 { 0, MAX77802_REG_PMIC_BUCK3DVS1, MAX77802_BUCK3DVS1_1V },
66 { 1, MAX77802_REG_PMIC_BUCK3CTRL1, MAX77802_BUCK_TYPE2_ON |
67 MAX77802_BUCK_TYPE2_IGNORE_PWRREQ },
68 { 0, MAX77802_REG_PMIC_BUCK4DVS1, MAX77802_BUCK4DVS1_1V },
69 { 1, MAX77802_REG_PMIC_BUCK4CTRL1, MAX77802_BUCK_TYPE2_ON |
70 MAX77802_BUCK_TYPE2_IGNORE_PWRREQ },
71 { 0, MAX77802_REG_PMIC_BUCK6DVS1, MAX77802_BUCK6DVS1_1V },
72 { 1, MAX77802_REG_PMIC_BUCK6CTRL, MAX77802_BUCK_TYPE1_ON |
Ronald G. Minnich88ac9b52013-06-26 17:28:52 -070073 MAX77802_BUCK_TYPE1_IGNORE_PWRREQ },
74 { 1, MAX77802_REG_PMIC_LDO35CTRL1, MAX77802_LDO35CTRL1_1_2V },
David Hendricks1e3e2c52013-06-14 16:08:05 -070075};
76
Hung-Te Linda7b8e42013-06-28 17:27:17 +080077static void setup_power(int is_resume)
Gabe Blackd3163ab2013-05-16 05:53:40 -070078{
79 int error = 0;
David Hendricks1e3e2c52013-06-14 16:08:05 -070080 int i;
Gabe Blackd3163ab2013-05-16 05:53:40 -070081
82 power_init();
83
Hung-Te Linda7b8e42013-06-28 17:27:17 +080084 if (is_resume) {
85 return;
86 }
87
Gabe Blackd3163ab2013-05-16 05:53:40 -070088 /* Initialize I2C bus to configure PMIC. */
David Hendricks1e3e2c52013-06-14 16:08:05 -070089 exynos_pinmux_i2c4();
Gabe Black925ad2e2013-07-31 23:21:34 -070090 i2c_init(4, 1000000, 0x00); /* 1MHz */
Gabe Blackd3163ab2013-05-16 05:53:40 -070091
92 printk(BIOS_DEBUG, "%s: Setting up PMIC...\n", __func__);
Gabe Blackd3163ab2013-05-16 05:53:40 -070093
David Hendricks1e3e2c52013-06-14 16:08:05 -070094 for (i = 0; i < ARRAY_SIZE(pmic_writes); i++) {
95 uint8_t data = 0;
96 uint8_t reg = pmic_writes[i].reg;
Gabe Blackd3163ab2013-05-16 05:53:40 -070097
David Hendricks1e3e2c52013-06-14 16:08:05 -070098 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 Blackd3163ab2013-05-16 05:53:40 -0700106 }
David Hendricks1e3e2c52013-06-14 16:08:05 -0700107
108 if (error)
109 die("Failed to intialize PMIC.\n");
Gabe Blackd3163ab2013-05-16 05:53:40 -0700110}
111
112static void setup_storage(void)
113{
114 /* MMC0: Fixed, 8 bit mode, connected with GPIO. */
Hung-Te Linf6d6e622013-07-03 19:07:21 +0800115 if (clock_set_dwmci(PERIPH_ID_SDMMC0))
Gabe Blackd3163ab2013-05-16 05:53:40 -0700116 printk(BIOS_CRIT, "%s: Failed to set MMC0 clock.\n", __func__);
Gabe Blacke6a44eb2013-06-15 23:40:26 -0700117 exynos_pinmux_sdmmc0();
Gabe Blackd3163ab2013-05-16 05:53:40 -0700118
119 /* MMC2: Removable, 4 bit mode, no GPIO. */
Hung-Te Linf6d6e622013-07-03 19:07:21 +0800120 clock_set_dwmci(PERIPH_ID_SDMMC2);
Gabe Blacke6a44eb2013-06-15 23:40:26 -0700121 exynos_pinmux_sdmmc2();
Gabe Blackd3163ab2013-05-16 05:53:40 -0700122}
123
Hung-Te Linc357aed2013-06-24 20:02:01 +0800124static void setup_ec(void)
125{
126 /* SPI2 (EC) is slower and needs to work in half-duplex mode with
127 * single byte bus width. */
Gabe Black98018092013-07-24 06:18:20 -0700128 clock_set_rate(PERIPH_ID_SPI2, 5000000);
Hung-Te Linc357aed2013-06-24 20:02:01 +0800129 exynos_pinmux_spi2();
130}
131
Gabe Blackd3163ab2013-05-16 05:53:40 -0700132static void setup_gpio(void)
133{
Gabe Black63bb6102013-06-19 03:29:45 -0700134 gpio_direction_input(GPIO_X30); // WP_GPIO
135 gpio_set_pull(GPIO_X30, GPIO_PULL_NONE);
Gabe Blackd3163ab2013-05-16 05:53:40 -0700136
Gabe Black63bb6102013-06-19 03:29:45 -0700137 gpio_direction_input(GPIO_X07); // RECMODE_GPIO
138 gpio_set_pull(GPIO_X07, GPIO_PULL_NONE);
Gabe Blackd3163ab2013-05-16 05:53:40 -0700139
Gabe Black63bb6102013-06-19 03:29:45 -0700140 gpio_direction_input(GPIO_X34); // LID_GPIO
141 gpio_set_pull(GPIO_X34, GPIO_PULL_NONE);
Gabe Blackd3163ab2013-05-16 05:53:40 -0700142
Gabe Black63bb6102013-06-19 03:29:45 -0700143 gpio_direction_input(GPIO_X12); // POWER_GPIO
144 gpio_set_pull(GPIO_X12, GPIO_PULL_NONE);
Gabe Blackd3163ab2013-05-16 05:53:40 -0700145}
146
147static void setup_memory(struct mem_timings *mem, int is_resume)
148{
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700149 printk(BIOS_SPEW, "manufacturer: 0x%x type: 0x%x, div: 0x%x, mhz: %d\n",
Gabe Blackd3163ab2013-05-16 05:53:40 -0700150 mem->mem_manuf,
151 mem->mem_type,
152 mem->mpll_mdiv,
153 mem->frequency_mhz);
154
Gabe Blackd3163ab2013-05-16 05:53:40 -0700155 if (ddr3_mem_ctrl_init(mem, DMC_INTERLEAVE_SIZE, !is_resume)) {
156 die("Failed to initialize memory controller.\n");
157 }
158}
159
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700160#define PRIMITIVE_MEM_TEST 0
161#if PRIMITIVE_MEM_TEST
162static unsigned long primitive_mem_test(void)
Gabe Blackd3163ab2013-05-16 05:53:40 -0700163{
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700164 unsigned long *l = (void *)0x40000000;
165 int bad = 0;
166 unsigned long i;
167 for(i = 0; i < 256*1048576; i++){
168 if (! (i%1048576))
169 printk(BIOS_SPEW, "%lu ...", i);
170 l[i] = 0xffffffff - i;
Gabe Blackd3163ab2013-05-16 05:53:40 -0700171 }
Gabe Black5420e092013-05-17 11:29:22 -0700172
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700173 for(i = 0; i < 256*1048576; i++){
174 if (! (i%1048576))
175 printk(BIOS_SPEW, "%lu ...", i);
176 if (l[i] != (0xffffffff - i)){
177 printk(BIOS_SPEW, "%p: want %08lx got %08lx\n", l, l[i], 0xffffffff - i);
178 bad++;
179 }
180 }
Gabe Black5420e092013-05-17 11:29:22 -0700181
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700182 printk(BIOS_SPEW, "%d errors\n", bad);
183
184 return bad;
Gabe Blackd3163ab2013-05-16 05:53:40 -0700185}
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700186#else
187#define primitive_mem_test()
188#endif
189
190#define SIMPLE_SPI_TEST 0
191#if SIMPLE_SPI_TEST
192/* here is a simple SPI debug test, known to fid trouble */
193static void simple_spi_test(void)
194{
195 struct cbfs_media default_media, *media;
196 int i, amt = 4 * MiB, errors = 0;
197 //u32 *data = (void *)0x40000000;
198 u32 data[1024];
199 u32 in;
200
201 amt = sizeof(data);
202 media = &default_media;
203 if (init_default_cbfs_media(media) != 0) {
204 printk(BIOS_SPEW, "Failed to initialize default media.\n");
205 return;
206 }
207
208
209 media->open(media);
210 if (media->read(media, data, (size_t) 0, amt) < amt){
211 printk(BIOS_SPEW, "simple_spi_test fails\n");
212 return;
213 }
214
215
216 for(i = 0; i < amt; i += 4){
217 if (media->read(media, &in, (size_t) i, 4) < 1){
218 printk(BIOS_SPEW, "simple_spi_test fails at %d\n", i);
219 return;
220 }
221 if (data[i/4] != in){
222 errors++;
223 printk(BIOS_SPEW, "BAD at %d(%p):\nRAM %08lx\nSPI %08lx\n",
224 i, &data[i/4], (unsigned long)data[i/4], (unsigned long)in);
225 /* reread it to see which is wrong. */
226 if (media->read(media, &in, (size_t) i, 4) < 1){
227 printk(BIOS_SPEW, "simple_spi_test fails at %d\n", i);
228 return;
229 }
230 printk(BIOS_SPEW, "RTRY at %d(%p):\nRAM %08lx\nSPI %08lx\n",
231 i, &data[i/4], (unsigned long)data[i/4], (unsigned long)in);
232 }
233
234 }
235 printk(BIOS_SPEW, "%d errors\n", errors);
236}
237#else
238#define simple_spi_test()
239#endif
Gabe Blackd3163ab2013-05-16 05:53:40 -0700240
241void main(void)
242{
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700243
244 extern struct mem_timings mem_timings;
Gabe Blackd3163ab2013-05-16 05:53:40 -0700245 void *entry;
246 int is_resume = (get_wakeup_state() != IS_NOT_WAKEUP);
247
248 /* Clock must be initialized before console_init, otherwise you may need
249 * to re-initialize serial console drivers again. */
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700250 system_clock_init();
Gabe Blackd3163ab2013-05-16 05:53:40 -0700251
Stefan Reinauer998ab0d2013-05-20 12:29:37 -0700252 console_init();
253
Hung-Te Linda7b8e42013-06-28 17:27:17 +0800254 setup_power(is_resume);
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700255 setup_memory(&mem_timings, is_resume);
256
257 primitive_mem_test();
Gabe Blackd3163ab2013-05-16 05:53:40 -0700258
259 if (is_resume) {
260 wakeup();
261 }
262
263 setup_storage();
264 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);
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700271 simple_spi_test();
Stefan Reinauer80e62932013-07-29 15:52:23 -0700272
273 cbmem_initialize_empty();
274
Gabe Blackd3163ab2013-05-16 05:53:40 -0700275 entry = cbfs_load_stage(CBFS_DEFAULT_MEDIA, "fallback/coreboot_ram");
Ronald G. Minniche6af9292013-06-03 13:03:50 -0700276 simple_spi_test();
Gabe Blackd3163ab2013-05-16 05:53:40 -0700277 stage_exit(entry);
278}