common: Drop init.h from common header
[pandora-u-boot.git] / board / synopsys / iot_devkit / iot_devkit.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Synopsys, Inc. All rights reserved.
4  */
5
6 #include <common.h>
7 #include <cpu_func.h>
8 #include <init.h>
9 #include <malloc.h>
10 #include <dwmmc.h>
11 #include <linux/libfdt.h>
12 #include <fdtdec.h>
13
14 #include <asm/arcregs.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 #define SYSCON_BASE     0xf000a000
19 #define AHBCKDIV        (void *)(SYSCON_BASE + 0x04)
20 #define APBCKDIV        (void *)(SYSCON_BASE + 0x08)
21 #define APBCKEN         (void *)(SYSCON_BASE + 0x0C)
22 #define RESET_REG       (void *)(SYSCON_BASE + 0x18)
23 #define CLKSEL          (void *)(SYSCON_BASE + 0x24)
24 #define CLKSTAT         (void *)(SYSCON_BASE + 0x28)
25 #define PLLCON          (void *)(SYSCON_BASE + 0x2C)
26 #define APBCKSEL        (void *)(SYSCON_BASE + 0x30)
27 #define AHBCKEN         (void *)(SYSCON_BASE + 0x34)
28 #define USBPHY_PLL      (void *)(SYSCON_BASE + 0x78)
29 #define USBCFG          (void *)(SYSCON_BASE + 0x7c)
30
31 #define PLL_MASK_0      0xffcfffff
32 #define PLL_MASK_1      0xffcfff00
33 #define PLL_MASK_2      0xfbcfff00
34
35 #define CLKSEL_DEFAULT  0x5a690000
36
37 static int set_cpu_freq(unsigned int clk)
38 {
39         clk /= 1000000;
40
41         /* Set clk to ext Xtal (LSN value 0) */
42         writel(CLKSEL_DEFAULT, CLKSEL);
43
44         switch (clk) {
45         case 16:
46                 /* Bypass mode */
47                 return 0;
48
49         case 50:
50                 writel(readl(PLLCON) & PLL_MASK_0, PLLCON);
51                 /* pll_off=1, M=25, N=1, OD=3, PLL_OUT_CLK=50M */
52                 writel((readl(PLLCON) & PLL_MASK_1) | 0x300191, PLLCON);
53                 /* pll_off=0, M=25, N=1, OD=3, PLL_OUT_CLK=50M */
54                 writel((readl(PLLCON) & PLL_MASK_2) | 0x300191, PLLCON);
55                 break;
56
57         case 72:
58                 writel(readl(PLLCON) & PLL_MASK_0, PLLCON);
59                 /* pll_off=1, M=18, N=1, OD=2, PLL_OUT_CLK=72M */
60                 writel((readl(PLLCON) & PLL_MASK_1) | 0x200121, PLLCON);
61                 /* pll_off=0, M=18, N=1, OD=2, PLL_OUT_CLK=72M */
62                 writel((readl(PLLCON) & PLL_MASK_2) | 0x200121, PLLCON);
63                 break;
64
65         case 100:
66                 writel(readl(PLLCON) & PLL_MASK_0, PLLCON);
67                 /* pll_off=1,M=25, N=1, OD=2, PLL_OUT_CLK=100M */
68                 writel((readl(PLLCON) & PLL_MASK_1) | 0x200191, PLLCON);
69                 /* pll_off=0,M=25, N=1, OD=2, PLL_OUT_CLK=100M */
70                 writel((readl(PLLCON) & PLL_MASK_2) | 0x200191, PLLCON);
71                 break;
72
73         case 136:
74                 writel(readl(PLLCON) & PLL_MASK_0, PLLCON);
75                 /* pll_off=1, M=17, N=1, OD=1, PLL_OUT_CLK=136M */
76                 writel((readl(PLLCON) & PLL_MASK_1) | 0x100111, PLLCON);
77                 /* pll_off=0, M=17, N=1, OD=1, PLL_OUT_CLK=136M */
78                 writel((readl(PLLCON) & PLL_MASK_2) | 0x100111, PLLCON);
79                 break;
80
81         case 144:
82                 writel(readl(PLLCON) & PLL_MASK_0, PLLCON);
83                 /* pll_off=1, M=18, N=1, OD=1, PLL_OUT_CLK=144M */
84                 writel((readl(PLLCON) & PLL_MASK_1) | 0x100121, PLLCON);
85                 /* pll_off=0, M=18, N=1, OD=1, PLL_OUT_CLK=144M */
86                 writel((readl(PLLCON) & PLL_MASK_2) | 0x100121, PLLCON);
87                 break;
88
89         default:
90                 return -EINVAL;
91         }
92
93         while (!(readl(CLKSTAT) & 0x4))
94                 ;
95
96         /* Set clk from PLL on bus (LSN = 1) */
97         writel(CLKSEL_DEFAULT | BIT(0), CLKSEL);
98
99         return 0;
100 }
101
102 extern u8 __rom_end[];
103 extern u8 __ram_start[];
104 extern u8 __ram_end[];
105
106 /*
107  * Use mach_cpu_init() for .data section copy as board_early_init_f() will be
108  * too late: initf_dm() will use a value of "av_" variable from not yet
109  * initialized (by copy) area.
110  */
111 int mach_cpu_init(void)
112 {
113         int offset;
114
115         /* Don't relocate U-Boot */
116         gd->flags |= GD_FLG_SKIP_RELOC;
117
118         /* Copy data from ROM to RAM */
119         u8 *src = __rom_end;
120         u8 *dst = __ram_start;
121
122         while (dst < __ram_end)
123                 *dst++ = *src++;
124
125         /* Enable debug uart */
126 #define DEBUG_UART_BASE         0x80014000
127 #define DEBUG_UART_DLF_OFFSET   0xc0
128         write_aux_reg(DEBUG_UART_BASE + DEBUG_UART_DLF_OFFSET, 1);
129
130         offset = fdt_path_offset(gd->fdt_blob, "/cpu_card/core_clk");
131         if (offset < 0)
132                 return offset;
133
134         gd->cpu_clk = fdtdec_get_int(gd->fdt_blob, offset, "clock-frequency", 0);
135         if (!gd->cpu_clk)
136                 return -EINVAL;
137
138         /* If CPU freq > 100 MHz, divide eFLASH clock by 2 */
139         if (gd->cpu_clk > 100000000) {
140                 u32 reg = readl(AHBCKDIV);
141
142                 reg &= ~(0xF << 8);
143                 reg |= 2 << 8;
144                 writel(reg, AHBCKDIV);
145         }
146
147         return set_cpu_freq(gd->cpu_clk);
148 }
149
150 #define IOTDK_RESET_SEQ         0x55AA6699
151
152 void reset_cpu(ulong addr)
153 {
154         writel(IOTDK_RESET_SEQ, RESET_REG);
155 }
156
157 int checkboard(void)
158 {
159         puts("Board: Synopsys IoT Development Kit\n");
160         return 0;
161 };