blob: b005874632ac62af3b5f56a2501ff0dcec809578 [file] [log] [blame]
Haojian Zhuang5f281b32017-05-24 08:45:05 +08001/*
2 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch_helpers.h>
8#include <assert.h>
9#include <bl_common.h>
10#include <console.h>
11#include <debug.h>
12#include <dw_mmc.h>
13#include <emmc.h>
14#include <errno.h>
15#include <gpio.h>
16#include <hi6220.h>
17#include <hi6553.h>
18#include <mmio.h>
19#include <pl061_gpio.h>
20#include <platform.h>
21#include <platform_def.h>
22#include <sp804_delay_timer.h>
23#include <string.h>
24#include <tbbr/tbbr_img_desc.h>
25
26#include "../../bl1/bl1_private.h"
27#include "hikey_def.h"
28#include "hikey_private.h"
29
30/*
31 * Declarations of linker defined symbols which will help us find the layout
32 * of trusted RAM
33 */
34extern unsigned long __COHERENT_RAM_START__;
35extern unsigned long __COHERENT_RAM_END__;
36
37/*
38 * The next 2 constants identify the extents of the coherent memory region.
39 * These addresses are used by the MMU setup code and therefore they must be
40 * page-aligned. It is the responsibility of the linker script to ensure that
41 * __COHERENT_RAM_START__ and __COHERENT_RAM_END__ linker symbols refer to
42 * page-aligned addresses.
43 */
44#define BL1_COHERENT_RAM_BASE (unsigned long)(&__COHERENT_RAM_START__)
45#define BL1_COHERENT_RAM_LIMIT (unsigned long)(&__COHERENT_RAM_END__)
46
47/* Data structure which holds the extents of the trusted RAM for BL1 */
48static meminfo_t bl1_tzram_layout;
49
50enum {
51 BOOT_NORMAL = 0,
52 BOOT_USB_DOWNLOAD,
53 BOOT_UART_DOWNLOAD,
54};
55
56meminfo_t *bl1_plat_sec_mem_layout(void)
57{
58 return &bl1_tzram_layout;
59}
60
61/*
62 * Perform any BL1 specific platform actions.
63 */
64void bl1_early_platform_setup(void)
65{
66 const size_t bl1_size = BL1_RAM_LIMIT - BL1_RAM_BASE;
67
68 /* Initialize the console to provide early debug support */
69 console_init(CONSOLE_BASE, PL011_UART_CLK_IN_HZ, PL011_BAUDRATE);
70
71 /* Allow BL1 to see the whole Trusted RAM */
72 bl1_tzram_layout.total_base = BL1_RW_BASE;
73 bl1_tzram_layout.total_size = BL1_RW_SIZE;
74
75 /* Calculate how much RAM BL1 is using and how much remains free */
76 bl1_tzram_layout.free_base = BL1_RW_BASE;
77 bl1_tzram_layout.free_size = BL1_RW_SIZE;
78 reserve_mem(&bl1_tzram_layout.free_base,
79 &bl1_tzram_layout.free_size,
80 BL1_RAM_BASE,
81 bl1_size);
82
83 INFO("BL1: 0x%lx - 0x%lx [size = %lu]\n", BL1_RAM_BASE, BL1_RAM_LIMIT,
84 bl1_size);
85}
86
87/*
88 * Perform the very early platform specific architecture setup here. At the
89 * moment this only does basic initialization. Later architectural setup
90 * (bl1_arch_setup()) does not do anything platform specific.
91 */
92void bl1_plat_arch_setup(void)
93{
94 hikey_init_mmu_el3(bl1_tzram_layout.total_base,
95 bl1_tzram_layout.total_size,
96 BL1_RO_BASE,
97 BL1_RO_LIMIT,
98 BL1_COHERENT_RAM_BASE,
99 BL1_COHERENT_RAM_LIMIT);
100}
101
102static void hikey_sp804_init(void)
103{
104 uint32_t data;
105
106 /* select the clock of dual timer0 */
107 data = mmio_read_32(AO_SC_TIMER_EN0);
108 while (data & 3) {
109 data &= ~3;
110 data |= 3 << 16;
111 mmio_write_32(AO_SC_TIMER_EN0, data);
112 data = mmio_read_32(AO_SC_TIMER_EN0);
113 }
114 /* enable the pclk of dual timer0 */
115 data = mmio_read_32(AO_SC_PERIPH_CLKSTAT4);
116 while (!(data & PCLK_TIMER1) || !(data & PCLK_TIMER0)) {
117 mmio_write_32(AO_SC_PERIPH_CLKEN4, PCLK_TIMER1 | PCLK_TIMER0);
118 data = mmio_read_32(AO_SC_PERIPH_CLKSTAT4);
119 }
120 /* reset dual timer0 */
121 data = mmio_read_32(AO_SC_PERIPH_RSTSTAT4);
122 mmio_write_32(AO_SC_PERIPH_RSTEN4, PCLK_TIMER1 | PCLK_TIMER0);
123 do {
124 data = mmio_read_32(AO_SC_PERIPH_RSTSTAT4);
125 } while (!(data & PCLK_TIMER1) || !(data & PCLK_TIMER0));
126 /* unreset dual timer0 */
127 mmio_write_32(AO_SC_PERIPH_RSTDIS4, PCLK_TIMER1 | PCLK_TIMER0);
128 do {
129 data = mmio_read_32(AO_SC_PERIPH_RSTSTAT4);
130 } while ((data & PCLK_TIMER1) || (data & PCLK_TIMER0));
131
132 sp804_timer_init(SP804_TIMER0_BASE, 10, 192);
133}
134
135static void hikey_gpio_init(void)
136{
137 pl061_gpio_init();
138 pl061_gpio_register(GPIO0_BASE, 0);
139 pl061_gpio_register(GPIO1_BASE, 1);
140 pl061_gpio_register(GPIO2_BASE, 2);
141 pl061_gpio_register(GPIO3_BASE, 3);
142 pl061_gpio_register(GPIO4_BASE, 4);
143 pl061_gpio_register(GPIO5_BASE, 5);
144 pl061_gpio_register(GPIO6_BASE, 6);
145 pl061_gpio_register(GPIO7_BASE, 7);
146 pl061_gpio_register(GPIO8_BASE, 8);
147 pl061_gpio_register(GPIO9_BASE, 9);
148 pl061_gpio_register(GPIO10_BASE, 10);
149 pl061_gpio_register(GPIO11_BASE, 11);
150 pl061_gpio_register(GPIO12_BASE, 12);
151 pl061_gpio_register(GPIO13_BASE, 13);
152 pl061_gpio_register(GPIO14_BASE, 14);
153 pl061_gpio_register(GPIO15_BASE, 15);
154 pl061_gpio_register(GPIO16_BASE, 16);
155 pl061_gpio_register(GPIO17_BASE, 17);
156 pl061_gpio_register(GPIO18_BASE, 18);
157 pl061_gpio_register(GPIO19_BASE, 19);
158
159 /* Power on indicator LED (USER_LED1). */
160 gpio_set_direction(32, GPIO_DIR_OUT); /* LED1 */
161 gpio_set_value(32, GPIO_LEVEL_HIGH);
162 gpio_set_direction(33, GPIO_DIR_OUT); /* LED2 */
163 gpio_set_value(33, GPIO_LEVEL_LOW);
164 gpio_set_direction(34, GPIO_DIR_OUT); /* LED3 */
165 gpio_set_direction(35, GPIO_DIR_OUT); /* LED4 */
166}
167
168static void hikey_pmussi_init(void)
169{
170 uint32_t data;
171
172 /* Initialize PWR_HOLD GPIO */
173 gpio_set_direction(0, GPIO_DIR_OUT);
174 gpio_set_value(0, GPIO_LEVEL_LOW);
175
176 /*
177 * After reset, PMUSSI stays in reset mode.
178 * Now make it out of reset.
179 */
180 mmio_write_32(AO_SC_PERIPH_RSTDIS4,
181 AO_SC_PERIPH_RSTDIS4_PRESET_PMUSSI_N);
182 do {
183 data = mmio_read_32(AO_SC_PERIPH_RSTSTAT4);
184 } while (data & AO_SC_PERIPH_RSTDIS4_PRESET_PMUSSI_N);
185
186 /* Set PMUSSI clock latency for read operation. */
187 data = mmio_read_32(AO_SC_MCU_SUBSYS_CTRL3);
188 data &= ~AO_SC_MCU_SUBSYS_CTRL3_RCLK_MASK;
189 data |= AO_SC_MCU_SUBSYS_CTRL3_RCLK_3;
190 mmio_write_32(AO_SC_MCU_SUBSYS_CTRL3, data);
191
192 /* enable PMUSSI clock */
193 data = AO_SC_PERIPH_CLKEN5_PCLK_PMUSSI_CCPU |
194 AO_SC_PERIPH_CLKEN5_PCLK_PMUSSI_MCU;
195 mmio_write_32(AO_SC_PERIPH_CLKEN5, data);
196 data = AO_SC_PERIPH_CLKEN4_PCLK_PMUSSI;
197 mmio_write_32(AO_SC_PERIPH_CLKEN4, data);
198
199 gpio_set_value(0, GPIO_LEVEL_HIGH);
200}
201
202static void hikey_hi6553_init(void)
203{
204 uint8_t data;
205
206 mmio_write_8(HI6553_PERI_EN_MARK, 0x1e);
207 mmio_write_8(HI6553_NP_REG_ADJ1, 0);
208 data = DISABLE6_XO_CLK_CONN | DISABLE6_XO_CLK_NFC |
209 DISABLE6_XO_CLK_RF1 | DISABLE6_XO_CLK_RF2;
210 mmio_write_8(HI6553_DISABLE6_XO_CLK, data);
211
212 /* configure BUCK0 & BUCK1 */
213 mmio_write_8(HI6553_BUCK01_CTRL2, 0x5e);
214 mmio_write_8(HI6553_BUCK0_CTRL7, 0x10);
215 mmio_write_8(HI6553_BUCK1_CTRL7, 0x10);
216 mmio_write_8(HI6553_BUCK0_CTRL5, 0x1e);
217 mmio_write_8(HI6553_BUCK1_CTRL5, 0x1e);
218 mmio_write_8(HI6553_BUCK0_CTRL1, 0xfc);
219 mmio_write_8(HI6553_BUCK1_CTRL1, 0xfc);
220
221 /* configure BUCK2 */
222 mmio_write_8(HI6553_BUCK2_REG1, 0x4f);
223 mmio_write_8(HI6553_BUCK2_REG5, 0x99);
224 mmio_write_8(HI6553_BUCK2_REG6, 0x45);
225 mdelay(1);
226 mmio_write_8(HI6553_VSET_BUCK2_ADJ, 0x22);
227 mdelay(1);
228
229 /* configure BUCK3 */
230 mmio_write_8(HI6553_BUCK3_REG3, 0x02);
231 mmio_write_8(HI6553_BUCK3_REG5, 0x99);
232 mmio_write_8(HI6553_BUCK3_REG6, 0x41);
233 mmio_write_8(HI6553_VSET_BUCK3_ADJ, 0x02);
234 mdelay(1);
235
236 /* configure BUCK4 */
237 mmio_write_8(HI6553_BUCK4_REG2, 0x9a);
238 mmio_write_8(HI6553_BUCK4_REG5, 0x99);
239 mmio_write_8(HI6553_BUCK4_REG6, 0x45);
240
241 /* configure LDO20 */
242 mmio_write_8(HI6553_LDO20_REG_ADJ, 0x50);
243
244 mmio_write_8(HI6553_NP_REG_CHG, 0x0f);
245 mmio_write_8(HI6553_CLK_TOP0, 0x06);
246 mmio_write_8(HI6553_CLK_TOP3, 0xc0);
247 mmio_write_8(HI6553_CLK_TOP4, 0x00);
248
249 /* configure LDO7 & LDO10 for SD slot */
250 /* enable LDO7 */
251 data = mmio_read_8(HI6553_LDO7_REG_ADJ);
252 data = (data & 0xf8) | 0x2;
253 mmio_write_8(HI6553_LDO7_REG_ADJ, data);
254 mdelay(5);
255 mmio_write_8(HI6553_ENABLE2_LDO1_8, 1 << 6);
256 mdelay(5);
257 /* enable LDO10 */
258 data = mmio_read_8(HI6553_LDO10_REG_ADJ);
259 data = (data & 0xf8) | 0x5;
260 mmio_write_8(HI6553_LDO10_REG_ADJ, data);
261 mdelay(5);
262 mmio_write_8(HI6553_ENABLE3_LDO9_16, 1 << 1);
263 mdelay(5);
264 /* enable LDO15 */
265 data = mmio_read_8(HI6553_LDO15_REG_ADJ);
266 data = (data & 0xf8) | 0x4;
267 mmio_write_8(HI6553_LDO15_REG_ADJ, data);
268 mmio_write_8(HI6553_ENABLE3_LDO9_16, 1 << 6);
269 mdelay(5);
270 /* enable LDO19 */
271 data = mmio_read_8(HI6553_LDO19_REG_ADJ);
272 data |= 0x7;
273 mmio_write_8(HI6553_LDO19_REG_ADJ, data);
274 mmio_write_8(HI6553_ENABLE4_LDO17_22, 1 << 2);
275 mdelay(5);
276 /* enable LDO21 */
277 data = mmio_read_8(HI6553_LDO21_REG_ADJ);
278 data = (data & 0xf8) | 0x3;
279 mmio_write_8(HI6553_LDO21_REG_ADJ, data);
280 mmio_write_8(HI6553_ENABLE4_LDO17_22, 1 << 4);
281 mdelay(5);
282 /* enable LDO22 */
283 data = mmio_read_8(HI6553_LDO22_REG_ADJ);
284 data = (data & 0xf8) | 0x7;
285 mmio_write_8(HI6553_LDO22_REG_ADJ, data);
286 mmio_write_8(HI6553_ENABLE4_LDO17_22, 1 << 5);
287 mdelay(5);
288
289 /* select 32.764KHz */
290 mmio_write_8(HI6553_CLK19M2_600_586_EN, 0x01);
291}
292
293static void init_mmc0_pll(void)
294{
295 unsigned int data;
296
297 /* select SYSPLL as the source of MMC0 */
298 /* select SYSPLL as the source of MUX1 (SC_CLK_SEL0) */
299 mmio_write_32(PERI_SC_CLK_SEL0, 1 << 5 | 1 << 21);
300 do {
301 data = mmio_read_32(PERI_SC_CLK_SEL0);
302 } while (!(data & (1 << 5)));
303 /* select MUX1 as the source of MUX2 (SC_CLK_SEL0) */
304 mmio_write_32(PERI_SC_CLK_SEL0, 1 << 29);
305 do {
306 data = mmio_read_32(PERI_SC_CLK_SEL0);
307 } while (data & (1 << 13));
308
309 mmio_write_32(PERI_SC_PERIPH_CLKEN0, (1 << 0));
310 do {
311 data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
312 } while (!(data & (1 << 0)));
313
314 data = mmio_read_32(PERI_SC_PERIPH_CLKEN12);
315 data |= 1 << 1;
316 mmio_write_32(PERI_SC_PERIPH_CLKEN12, data);
317
318 do {
319 mmio_write_32(PERI_SC_CLKCFG8BIT1, (1 << 7) | 0xb);
320 data = mmio_read_32(PERI_SC_CLKCFG8BIT1);
321 } while ((data & 0xb) != 0xb);
322}
323
324static void reset_mmc0_clk(void)
325{
326 unsigned int data;
327
328 /* disable mmc0 bus clock */
329 mmio_write_32(PERI_SC_PERIPH_CLKDIS0, PERI_CLK0_MMC0);
330 do {
331 data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
332 } while (data & PERI_CLK0_MMC0);
333 /* enable mmc0 bus clock */
334 mmio_write_32(PERI_SC_PERIPH_CLKEN0, PERI_CLK0_MMC0);
335 do {
336 data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
337 } while (!(data & PERI_CLK0_MMC0));
338 /* reset mmc0 clock domain */
339 mmio_write_32(PERI_SC_PERIPH_RSTEN0, PERI_RST0_MMC0);
340
341 /* bypass mmc0 clock phase */
342 data = mmio_read_32(PERI_SC_PERIPH_CTRL2);
343 data |= 3;
344 mmio_write_32(PERI_SC_PERIPH_CTRL2, data);
345
346 /* disable low power */
347 data = mmio_read_32(PERI_SC_PERIPH_CTRL13);
348 data |= 1 << 3;
349 mmio_write_32(PERI_SC_PERIPH_CTRL13, data);
350 do {
351 data = mmio_read_32(PERI_SC_PERIPH_RSTSTAT0);
352 } while (!(data & PERI_RST0_MMC0));
353
354 /* unreset mmc0 clock domain */
355 mmio_write_32(PERI_SC_PERIPH_RSTDIS0, PERI_RST0_MMC0);
356 do {
357 data = mmio_read_32(PERI_SC_PERIPH_RSTSTAT0);
358 } while (data & PERI_RST0_MMC0);
359}
360
361static void init_media_clk(void)
362{
363 unsigned int data, value;
364
365 data = mmio_read_32(PMCTRL_MEDPLLCTRL);
366 data |= 1;
367 mmio_write_32(PMCTRL_MEDPLLCTRL, data);
368
369 for (;;) {
370 data = mmio_read_32(PMCTRL_MEDPLLCTRL);
371 value = 1 << 28;
372 if ((data & value) == value)
373 break;
374 }
375
376 data = mmio_read_32(PERI_SC_PERIPH_CLKEN12);
377 data = 1 << 10;
378 mmio_write_32(PERI_SC_PERIPH_CLKEN12, data);
379}
380
381static void init_mmc1_pll(void)
382{
383 uint32_t data;
384
385 /* select SYSPLL as the source of MMC1 */
386 /* select SYSPLL as the source of MUX1 (SC_CLK_SEL0) */
387 mmio_write_32(PERI_SC_CLK_SEL0, 1 << 11 | 1 << 27);
388 do {
389 data = mmio_read_32(PERI_SC_CLK_SEL0);
390 } while (!(data & (1 << 11)));
391 /* select MUX1 as the source of MUX2 (SC_CLK_SEL0) */
392 mmio_write_32(PERI_SC_CLK_SEL0, 1 << 30);
393 do {
394 data = mmio_read_32(PERI_SC_CLK_SEL0);
395 } while (data & (1 << 14));
396
397 mmio_write_32(PERI_SC_PERIPH_CLKEN0, (1 << 1));
398 do {
399 data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
400 } while (!(data & (1 << 1)));
401
402 data = mmio_read_32(PERI_SC_PERIPH_CLKEN12);
403 data |= 1 << 2;
404 mmio_write_32(PERI_SC_PERIPH_CLKEN12, data);
405
406 do {
407 /* 1.2GHz / 50 = 24MHz */
408 mmio_write_32(PERI_SC_CLKCFG8BIT2, 0x31 | (1 << 7));
409 data = mmio_read_32(PERI_SC_CLKCFG8BIT2);
410 } while ((data & 0x31) != 0x31);
411}
412
413static void reset_mmc1_clk(void)
414{
415 unsigned int data;
416
417 /* disable mmc1 bus clock */
418 mmio_write_32(PERI_SC_PERIPH_CLKDIS0, PERI_CLK0_MMC1);
419 do {
420 data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
421 } while (data & PERI_CLK0_MMC1);
422 /* enable mmc1 bus clock */
423 mmio_write_32(PERI_SC_PERIPH_CLKEN0, PERI_CLK0_MMC1);
424 do {
425 data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
426 } while (!(data & PERI_CLK0_MMC1));
427 /* reset mmc1 clock domain */
428 mmio_write_32(PERI_SC_PERIPH_RSTEN0, PERI_RST0_MMC1);
429
430 /* bypass mmc1 clock phase */
431 data = mmio_read_32(PERI_SC_PERIPH_CTRL2);
432 data |= 3 << 2;
433 mmio_write_32(PERI_SC_PERIPH_CTRL2, data);
434
435 /* disable low power */
436 data = mmio_read_32(PERI_SC_PERIPH_CTRL13);
437 data |= 1 << 4;
438 mmio_write_32(PERI_SC_PERIPH_CTRL13, data);
439 do {
440 data = mmio_read_32(PERI_SC_PERIPH_RSTSTAT0);
441 } while (!(data & PERI_RST0_MMC1));
442
443 /* unreset mmc0 clock domain */
444 mmio_write_32(PERI_SC_PERIPH_RSTDIS0, PERI_RST0_MMC1);
445 do {
446 data = mmio_read_32(PERI_SC_PERIPH_RSTSTAT0);
447 } while (data & PERI_RST0_MMC1);
448}
449
450/* Initialize PLL of both eMMC and SD controllers. */
451static void hikey_mmc_pll_init(void)
452{
453 init_mmc0_pll();
454 reset_mmc0_clk();
455 init_media_clk();
456
457 dsb();
458
459 init_mmc1_pll();
460 reset_mmc1_clk();
461}
462
463/*
464 * Function which will perform any remaining platform-specific setup that can
465 * occur after the MMU and data cache have been enabled.
466 */
467void bl1_platform_setup(void)
468{
469 dw_mmc_params_t params;
470
471 assert((HIKEY_BL1_MMC_DESC_BASE >= SRAM_BASE) &&
472 ((SRAM_BASE + SRAM_SIZE) >=
473 (HIKEY_BL1_MMC_DATA_BASE + HIKEY_BL1_MMC_DATA_SIZE)));
474 hikey_sp804_init();
475 hikey_gpio_init();
476 hikey_pmussi_init();
477 hikey_hi6553_init();
478
479 hikey_mmc_pll_init();
480
481 memset(&params, 0, sizeof(dw_mmc_params_t));
482 params.reg_base = DWMMC0_BASE;
483 params.desc_base = HIKEY_BL1_MMC_DESC_BASE;
484 params.desc_size = 1 << 20;
485 params.clk_rate = 24 * 1000 * 1000;
486 params.bus_width = EMMC_BUS_WIDTH_8;
487 params.flags = EMMC_FLAG_CMD23;
488 dw_mmc_init(&params);
489
490 hikey_io_setup();
491}
492
493/*
494 * The following function checks if Firmware update is needed,
495 * by checking if TOC in FIP image is valid or not.
496 */
497unsigned int bl1_plat_get_next_image_id(void)
498{
499 int32_t boot_mode;
500 unsigned int ret;
501
502 boot_mode = mmio_read_32(ONCHIPROM_PARAM_BASE);
503 switch (boot_mode) {
504 case BOOT_NORMAL:
505 ret = BL2_IMAGE_ID;
506 break;
507 case BOOT_USB_DOWNLOAD:
508 case BOOT_UART_DOWNLOAD:
509 ret = NS_BL1U_IMAGE_ID;
510 break;
511 default:
512 WARN("Invalid boot mode is found:%d\n", boot_mode);
513 panic();
514 }
515 return ret;
516}
517
518image_desc_t *bl1_plat_get_image_desc(unsigned int image_id)
519{
520 unsigned int index = 0;
521
522 while (bl1_tbbr_image_descs[index].image_id != INVALID_IMAGE_ID) {
523 if (bl1_tbbr_image_descs[index].image_id == image_id)
524 return &bl1_tbbr_image_descs[index];
525
526 index++;
527 }
528
529 return NULL;
530}
531
532void bl1_plat_set_ep_info(unsigned int image_id,
533 entry_point_info_t *ep_info)
534{
535 unsigned int data = 0;
536
537 if (image_id == BL2_IMAGE_ID)
538 return;
539 inv_dcache_range(NS_BL1U_BASE, NS_BL1U_SIZE);
540 __asm__ volatile ("mrs %0, cpacr_el1" : "=r"(data));
541 do {
542 data |= 3 << 20;
543 __asm__ volatile ("msr cpacr_el1, %0" : : "r"(data));
544 __asm__ volatile ("mrs %0, cpacr_el1" : "=r"(data));
545 } while ((data & (3 << 20)) != (3 << 20));
546 INFO("cpacr_el1:0x%x\n", data);
547
548 ep_info->args.arg0 = 0xffff & read_mpidr();
549 ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
550 DISABLE_ALL_EXCEPTIONS);
551}