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