common: Drop linux/delay.h from common header
[oweals/u-boot.git] / board / hisilicon / hikey / hikey.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015 Linaro
4  * Peter Griffin <peter.griffin@linaro.org>
5  */
6 #include <common.h>
7 #include <cpu_func.h>
8 #include <dm.h>
9 #include <fdt_support.h>
10 #include <init.h>
11 #include <log.h>
12 #include <dm/platform_data/serial_pl01x.h>
13 #include <errno.h>
14 #include <malloc.h>
15 #include <netdev.h>
16 #include <asm/io.h>
17 #include <usb.h>
18 #include <linux/delay.h>
19 #include <power/hi6553_pmic.h>
20 #include <asm-generic/gpio.h>
21 #include <asm/arch/dwmmc.h>
22 #include <asm/arch/gpio.h>
23 #include <asm/arch/periph.h>
24 #include <asm/arch/pinmux.h>
25 #include <asm/arch/hi6220.h>
26 #include <asm/armv8/mmu.h>
27
28 /*TODO drop this table in favour of device tree */
29 static const struct hikey_gpio_platdata hi6220_gpio[] = {
30         { 0, HI6220_GPIO_BASE(0)},
31         { 1, HI6220_GPIO_BASE(1)},
32         { 2, HI6220_GPIO_BASE(2)},
33         { 3, HI6220_GPIO_BASE(3)},
34         { 4, HI6220_GPIO_BASE(4)},
35         { 5, HI6220_GPIO_BASE(5)},
36         { 6, HI6220_GPIO_BASE(6)},
37         { 7, HI6220_GPIO_BASE(7)},
38         { 8, HI6220_GPIO_BASE(8)},
39         { 9, HI6220_GPIO_BASE(9)},
40         { 10, HI6220_GPIO_BASE(10)},
41         { 11, HI6220_GPIO_BASE(11)},
42         { 12, HI6220_GPIO_BASE(12)},
43         { 13, HI6220_GPIO_BASE(13)},
44         { 14, HI6220_GPIO_BASE(14)},
45         { 15, HI6220_GPIO_BASE(15)},
46         { 16, HI6220_GPIO_BASE(16)},
47         { 17, HI6220_GPIO_BASE(17)},
48         { 18, HI6220_GPIO_BASE(18)},
49         { 19, HI6220_GPIO_BASE(19)},
50
51 };
52
53 U_BOOT_DEVICES(hi6220_gpios) = {
54         { "gpio_hi6220", &hi6220_gpio[0] },
55         { "gpio_hi6220", &hi6220_gpio[1] },
56         { "gpio_hi6220", &hi6220_gpio[2] },
57         { "gpio_hi6220", &hi6220_gpio[3] },
58         { "gpio_hi6220", &hi6220_gpio[4] },
59         { "gpio_hi6220", &hi6220_gpio[5] },
60         { "gpio_hi6220", &hi6220_gpio[6] },
61         { "gpio_hi6220", &hi6220_gpio[7] },
62         { "gpio_hi6220", &hi6220_gpio[8] },
63         { "gpio_hi6220", &hi6220_gpio[9] },
64         { "gpio_hi6220", &hi6220_gpio[10] },
65         { "gpio_hi6220", &hi6220_gpio[11] },
66         { "gpio_hi6220", &hi6220_gpio[12] },
67         { "gpio_hi6220", &hi6220_gpio[13] },
68         { "gpio_hi6220", &hi6220_gpio[14] },
69         { "gpio_hi6220", &hi6220_gpio[15] },
70         { "gpio_hi6220", &hi6220_gpio[16] },
71         { "gpio_hi6220", &hi6220_gpio[17] },
72         { "gpio_hi6220", &hi6220_gpio[18] },
73         { "gpio_hi6220", &hi6220_gpio[19] },
74 };
75
76 DECLARE_GLOBAL_DATA_PTR;
77
78 #if !CONFIG_IS_ENABLED(OF_CONTROL)
79
80 static const struct pl01x_serial_platdata serial_platdata = {
81 #if CONFIG_CONS_INDEX == 1
82         .base = HI6220_UART0_BASE,
83 #elif CONFIG_CONS_INDEX == 4
84         .base = HI6220_UART3_BASE,
85 #else
86 #error "Unsupported console index value."
87 #endif
88         .type = TYPE_PL011,
89         .clock = 19200000
90 };
91
92 U_BOOT_DEVICE(hikey_seriala) = {
93         .name = "serial_pl01x",
94         .platdata = &serial_platdata,
95 };
96 #endif
97
98 static struct mm_region hikey_mem_map[] = {
99         {
100                 .virt = 0x0UL,
101                 .phys = 0x0UL,
102                 .size = 0x80000000UL,
103                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
104                          PTE_BLOCK_INNER_SHARE
105         }, {
106                 .virt = 0x80000000UL,
107                 .phys = 0x80000000UL,
108                 .size = 0x80000000UL,
109                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
110                          PTE_BLOCK_NON_SHARE |
111                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
112         }, {
113                 /* List terminator */
114                 0,
115         }
116 };
117
118 struct mm_region *mem_map = hikey_mem_map;
119
120 #ifdef CONFIG_BOARD_EARLY_INIT_F
121 int board_uart_init(void)
122 {
123         switch (CONFIG_CONS_INDEX) {
124         case 1:
125                 hi6220_pinmux_config(PERIPH_ID_UART0);
126                 break;
127         case 4:
128                 hi6220_pinmux_config(PERIPH_ID_UART3);
129                 break;
130         default:
131                 debug("%s: Unsupported UART selected\n", __func__);
132                 return -1;
133         }
134
135         return 0;
136 }
137
138 int board_early_init_f(void)
139 {
140         board_uart_init();
141         return 0;
142 }
143 #endif
144
145 struct peri_sc_periph_regs *peri_sc =
146         (struct peri_sc_periph_regs *)HI6220_PERI_BASE;
147
148 struct alwayson_sc_regs *ao_sc =
149         (struct alwayson_sc_regs *)ALWAYSON_CTRL_BASE;
150
151 /* status offset from enable reg */
152 #define STAT_EN_OFF 0x2
153
154 void hi6220_clk_enable(u32 bitfield, unsigned int *clk_base)
155 {
156         uint32_t data;
157
158         data = readl(clk_base);
159         data |= bitfield;
160
161         writel(bitfield, clk_base);
162         do {
163                 data = readl(clk_base + STAT_EN_OFF);
164         } while ((data & bitfield) == 0);
165 }
166
167 /* status offset from disable reg */
168 #define STAT_DIS_OFF 0x1
169
170 void hi6220_clk_disable(u32 bitfield, unsigned int *clk_base)
171 {
172         uint32_t data;
173
174         data = readl(clk_base);
175         data |= bitfield;
176
177         writel(data, clk_base);
178         do {
179                 data = readl(clk_base + STAT_DIS_OFF);
180         } while (data & bitfield);
181 }
182
183 #define EYE_PATTERN     0x70533483
184
185 int board_usb_init(int index, enum usb_init_type init)
186 {
187         unsigned int data;
188
189         /* enable USB clock */
190         hi6220_clk_enable(PERI_CLK0_USBOTG, &peri_sc->clk0_en);
191
192         /* take usb IPs out of reset */
193         writel(PERI_RST0_USBOTG_BUS | PERI_RST0_POR_PICOPHY |
194                 PERI_RST0_USBOTG | PERI_RST0_USBOTG_32K,
195                 &peri_sc->rst0_dis);
196         do {
197                 data = readl(&peri_sc->rst0_stat);
198                 data &= PERI_RST0_USBOTG_BUS | PERI_RST0_POR_PICOPHY |
199                         PERI_RST0_USBOTG | PERI_RST0_USBOTG_32K;
200         } while (data);
201
202         /*CTRL 5*/
203         data = readl(&peri_sc->ctrl5);
204         data &= ~PERI_CTRL5_PICOPHY_BC_MODE;
205         data |= PERI_CTRL5_USBOTG_RES_SEL | PERI_CTRL5_PICOPHY_ACAENB;
206         data |= 0x300;
207         writel(data, &peri_sc->ctrl5);
208
209         /*CTRL 4*/
210
211         /* configure USB PHY */
212         data = readl(&peri_sc->ctrl4);
213
214         /* make PHY out of low power mode */
215         data &= ~PERI_CTRL4_PICO_SIDDQ;
216         data &= ~PERI_CTRL4_PICO_OGDISABLE;
217         data |= PERI_CTRL4_PICO_VBUSVLDEXTSEL | PERI_CTRL4_PICO_VBUSVLDEXT;
218         writel(data, &peri_sc->ctrl4);
219
220         writel(EYE_PATTERN, &peri_sc->ctrl8);
221
222         mdelay(5);
223         return 0;
224 }
225
226 static int config_sd_carddetect(void)
227 {
228         int ret;
229
230         /* configure GPIO8 as nopull */
231         writel(0, 0xf8001830);
232
233         gpio_request(8, "SD CD");
234
235         gpio_direction_input(8);
236         ret = gpio_get_value(8);
237
238         if (!ret) {
239                 printf("%s: SD card present\n", __func__);
240                 return 1;
241         }
242
243         printf("%s: SD card not present\n", __func__);
244         return 0;
245 }
246
247
248 static void mmc1_init_pll(void)
249 {
250         uint32_t data;
251
252         /* select SYSPLL as the source of MMC1 */
253         /* select SYSPLL as the source of MUX1 (SC_CLK_SEL0) */
254         writel(1 << 11 | 1 << 27, &peri_sc->clk0_sel);
255         do {
256                 data = readl(&peri_sc->clk0_sel);
257         } while (!(data & (1 << 11)));
258
259         /* select MUX1 as the source of MUX2 (SC_CLK_SEL0) */
260         writel(1 << 30, &peri_sc->clk0_sel);
261         do {
262                 data = readl(&peri_sc->clk0_sel);
263         } while (data & (1 << 14));
264
265         hi6220_clk_enable(PERI_CLK0_MMC1, &peri_sc->clk0_en);
266
267         hi6220_clk_enable(PERI_CLK12_MMC1_SRC, &peri_sc->clk12_en);
268
269         do {
270                 /* 1.2GHz / 50 = 24MHz */
271                 writel(0x31 | (1 << 7), &peri_sc->clkcfg8bit2);
272                 data = readl(&peri_sc->clkcfg8bit2);
273         } while ((data & 0x31) != 0x31);
274 }
275
276 static void mmc1_reset_clk(void)
277 {
278         unsigned int data;
279
280         /* disable mmc1 bus clock */
281         hi6220_clk_disable(PERI_CLK0_MMC1, &peri_sc->clk0_dis);
282
283         /* enable mmc1 bus clock */
284         hi6220_clk_enable(PERI_CLK0_MMC1, &peri_sc->clk0_en);
285
286         /* reset mmc1 clock domain */
287         writel(PERI_RST0_MMC1, &peri_sc->rst0_en);
288
289         /* bypass mmc1 clock phase */
290         data = readl(&peri_sc->ctrl2);
291         data |= 3 << 2;
292         writel(data, &peri_sc->ctrl2);
293
294         /* disable low power */
295         data = readl(&peri_sc->ctrl13);
296         data |= 1 << 4;
297         writel(data, &peri_sc->ctrl13);
298         do {
299                 data = readl(&peri_sc->rst0_stat);
300         } while (!(data & PERI_RST0_MMC1));
301
302         /* unreset mmc1 clock domain */
303         writel(PERI_RST0_MMC1, &peri_sc->rst0_dis);
304         do {
305                 data = readl(&peri_sc->rst0_stat);
306         } while (data & PERI_RST0_MMC1);
307 }
308
309 static void mmc0_reset_clk(void)
310 {
311         unsigned int data;
312
313         /* disable mmc0 bus clock */
314         hi6220_clk_disable(PERI_CLK0_MMC0, &peri_sc->clk0_dis);
315
316         /* enable mmc0 bus clock */
317         hi6220_clk_enable(PERI_CLK0_MMC0, &peri_sc->clk0_en);
318
319         /* reset mmc0 clock domain */
320         writel(PERI_RST0_MMC0, &peri_sc->rst0_en);
321
322         /* bypass mmc0 clock phase */
323         data = readl(&peri_sc->ctrl2);
324         data |= 3;
325         writel(data, &peri_sc->ctrl2);
326
327         /* disable low power */
328         data = readl(&peri_sc->ctrl13);
329         data |= 1 << 3;
330         writel(data, &peri_sc->ctrl13);
331         do {
332                 data = readl(&peri_sc->rst0_stat);
333         } while (!(data & PERI_RST0_MMC0));
334
335         /* unreset mmc0 clock domain */
336         writel(PERI_RST0_MMC0, &peri_sc->rst0_dis);
337         do {
338                 data = readl(&peri_sc->rst0_stat);
339         } while (data & PERI_RST0_MMC0);
340 }
341
342
343 /* PMU SSI is the IP that maps the external PMU hi6553 registers as IO */
344 static void hi6220_pmussi_init(void)
345 {
346         uint32_t data;
347
348         /* Take PMUSSI out of reset */
349         writel(ALWAYSON_SC_PERIPH_RST4_DIS_PRESET_PMUSSI_N,
350                &ao_sc->rst4_dis);
351         do {
352                 data = readl(&ao_sc->rst4_stat);
353         } while (data & ALWAYSON_SC_PERIPH_RST4_DIS_PRESET_PMUSSI_N);
354
355         /* set PMU SSI clock latency for read operation */
356         data = readl(&ao_sc->mcu_subsys_ctrl3);
357         data &= ~ALWAYSON_SC_MCU_SUBSYS_CTRL3_RCLK_MASK;
358         data |= ALWAYSON_SC_MCU_SUBSYS_CTRL3_RCLK_3;
359         writel(data, &ao_sc->mcu_subsys_ctrl3);
360
361         /* enable PMUSSI clock */
362         data = ALWAYSON_SC_PERIPH_CLK5_EN_PCLK_PMUSSI_CCPU |
363                ALWAYSON_SC_PERIPH_CLK5_EN_PCLK_PMUSSI_MCU;
364
365         hi6220_clk_enable(data, &ao_sc->clk5_en);
366
367         /* Output high to PMIC on PWR_HOLD_GPIO0_0 */
368         gpio_request(0, "PWR_HOLD_GPIO0_0");
369         gpio_direction_output(0, 1);
370 }
371
372 int misc_init_r(void)
373 {
374         return 0;
375 }
376
377 int board_init(void)
378 {
379         return 0;
380 }
381
382 #ifdef CONFIG_MMC
383
384 static int init_dwmmc(void)
385 {
386         int ret = 0;
387
388 #ifdef CONFIG_MMC_DW
389
390         /* mmc0 pll is already configured by ATF */
391         mmc0_reset_clk();
392         ret = hi6220_pinmux_config(PERIPH_ID_SDMMC0);
393         if (ret)
394                 printf("%s: Error configuring pinmux for eMMC (%d)\n"
395                         , __func__, ret);
396
397         ret |= hi6220_dwmci_add_port(0, HI6220_MMC0_BASE, 8);
398         if (ret)
399                 printf("%s: Error adding eMMC port (%d)\n", __func__, ret);
400
401
402         /* take mmc1 (sd slot) out of reset, configure clocks and pinmuxing */
403         mmc1_init_pll();
404         mmc1_reset_clk();
405
406         ret |= hi6220_pinmux_config(PERIPH_ID_SDMMC1);
407         if (ret)
408                 printf("%s: Error configuring pinmux for eMMC (%d)\n"
409                         , __func__, ret);
410
411         config_sd_carddetect();
412
413         ret |= hi6220_dwmci_add_port(1, HI6220_MMC1_BASE, 4);
414         if (ret)
415                 printf("%s: Error adding SD port (%d)\n", __func__, ret);
416
417 #endif
418         return ret;
419 }
420
421 /* setup board specific PMIC */
422 int power_init_board(void)
423 {
424         /* init the hi6220 pmussi ip */
425         hi6220_pmussi_init();
426
427         power_hi6553_init((u8 *)HI6220_PMUSSI_BASE);
428
429         return 0;
430 }
431
432 int board_mmc_init(bd_t *bis)
433 {
434         int ret;
435
436         /* add the eMMC and sd ports */
437         ret = init_dwmmc();
438
439         if (ret)
440                 debug("init_dwmmc failed\n");
441
442         return ret;
443 }
444 #endif
445
446 int dram_init(void)
447 {
448         gd->ram_size = PHYS_SDRAM_1_SIZE;
449         return 0;
450 }
451
452 int dram_init_banksize(void)
453 {
454         /*
455          * Reserve regions below from DT memory node (which gets generated
456          * by U-Boot from the dram banks in arch_fixup_fdt() before booting
457          * the kernel. This will then match the kernel hikey dts memory node.
458          *
459          *  0x05e0,0000 - 0x05ef,ffff: MCU firmware runtime using
460          *  0x05f0,1000 - 0x05f0,1fff: Reboot reason
461          *  0x06df,f000 - 0x06df,ffff: Mailbox message data
462          *  0x0740,f000 - 0x0740,ffff: MCU firmware section
463          *  0x21f0,0000 - 0x21ff,ffff: pstore/ramoops buffer
464          *  0x3e00,0000 - 0x3fff,ffff: OP-TEE
465         */
466
467         gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
468         gd->bd->bi_dram[0].size = 0x05e00000;
469
470         gd->bd->bi_dram[1].start = 0x05f00000;
471         gd->bd->bi_dram[1].size = 0x00001000;
472
473         gd->bd->bi_dram[2].start = 0x05f02000;
474         gd->bd->bi_dram[2].size = 0x00efd000;
475
476         gd->bd->bi_dram[3].start = 0x06e00000;
477         gd->bd->bi_dram[3].size = 0x0060f000;
478
479         gd->bd->bi_dram[4].start = 0x07410000;
480         gd->bd->bi_dram[4].size = 0x1aaf0000;
481
482         gd->bd->bi_dram[5].start = 0x22000000;
483         gd->bd->bi_dram[5].size = 0x1c000000;
484
485         return 0;
486 }
487
488 void reset_cpu(ulong addr)
489 {
490         writel(0x48698284, &ao_sc->stat0);
491         wfi();
492 }