Merge branch '2022-08-04-assorted-fixed'
[platform/kernel/u-boot.git] / board / keymile / km83xx / km83xx.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2006 Freescale Semiconductor, Inc.
4  *                    Dave Liu <daveliu@freescale.com>
5  *
6  * Copyright (C) 2007 Logic Product Development, Inc.
7  *                    Peter Barada <peterb@logicpd.com>
8  *
9  * Copyright (C) 2007 MontaVista Software, Inc.
10  *                    Anton Vorontsov <avorontsov@ru.mvista.com>
11  *
12  * (C) Copyright 2008 - 2010
13  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
14  */
15
16 #include <common.h>
17 #include <env.h>
18 #include <fdt_support.h>
19 #include <init.h>
20 #include <ioports.h>
21 #include <log.h>
22 #include <mpc83xx.h>
23 #include <i2c.h>
24 #include <miiphy.h>
25 #include <asm/global_data.h>
26 #include <asm/io.h>
27 #include <asm/mmu.h>
28 #include <asm/processor.h>
29 #include <pci.h>
30 #include <linux/delay.h>
31 #include <linux/libfdt.h>
32 #include <post.h>
33
34 #include "../common/common.h"
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 static uchar ivm_content[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
39
40 static int piggy_present(void)
41 {
42         struct km_bec_fpga __iomem *base =
43                 (struct km_bec_fpga __iomem *)CONFIG_SYS_KMBEC_FPGA_BASE;
44
45         return in_8(&base->bprth) & PIGGY_PRESENT;
46 }
47
48 int ethernet_present(void)
49 {
50         return piggy_present();
51 }
52
53 int board_early_init_r(void)
54 {
55         struct km_bec_fpga *base =
56                 (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
57
58 #if defined(CONFIG_ARCH_MPC8360)
59         unsigned short  svid;
60         /*
61          * Because of errata in the UCCs, we have to write to the reserved
62          * registers to slow the clocks down.
63          */
64         svid =  SVR_REV(mfspr(SVR));
65         switch (svid) {
66         case 0x0020:
67                 /*
68                  * MPC8360ECE.pdf QE_ENET10 table 4:
69                  * IMMR + 0x14A8[4:5] = 11 (clk delay for UCC 2)
70                  * IMMR + 0x14A8[18:19] = 11 (clk delay for UCC 1)
71                  */
72                 setbits_be32((void *)(CONFIG_SYS_IMMR + 0x14a8), 0x0c003000);
73                 break;
74         case 0x0021:
75                 /*
76                  * MPC8360ECE.pdf QE_ENET10 table 4:
77                  * IMMR + 0x14AC[24:27] = 1010
78                  */
79                 clrsetbits_be32((void *)(CONFIG_SYS_IMMR + 0x14ac),
80                         0x00000050, 0x000000a0);
81                 break;
82         }
83 #endif
84
85         /* enable the PHY on the PIGGY */
86         setbits_8(&base->pgy_eth, 0x01);
87         /* enable the Unit LED (green) */
88         setbits_8(&base->oprth, WRL_BOOT);
89         /* enable Application Buffer */
90         setbits_8(&base->oprtl, OPRTL_XBUFENA);
91
92         return 0;
93 }
94
95 int misc_init_r(void)
96 {
97         ivm_read_eeprom(ivm_content, CONFIG_SYS_IVM_EEPROM_MAX_LEN,
98                         CONFIG_PIGGY_MAC_ADDRESS_OFFSET);
99         return 0;
100 }
101
102 int last_stage_init(void)
103 {
104 #if defined(CONFIG_TARGET_KMCOGE5NE)
105         /*
106          * BFTIC3 on the local bus CS4
107          */
108         struct bfticu_iomap *base = (struct bfticu_iomap *)0xB0000000;
109         u8 dip_switch = in_8((u8 *)&(base->mswitch)) & BFTICU_DIPSWITCH_MASK;
110
111         if (dip_switch != 0) {
112                 /* start bootloader */
113                 puts("DIP:   Enabled\n");
114                 env_set("actual_bank", "0");
115         }
116 #endif
117         set_km_env();
118         return 0;
119 }
120
121 static int fixed_sdram(void)
122 {
123         immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
124         u32 msize = 0;
125         u32 ddr_size;
126         u32 ddr_size_log2;
127
128         out_be32(&im->sysconf.ddrlaw[0].ar, (LAWAR_EN | 0x1e));
129         out_be32(&im->ddr.csbnds[0].csbnds, (CONFIG_SYS_DDR_CS0_BNDS) | 0x7f);
130         out_be32(&im->ddr.cs_config[0], CONFIG_SYS_DDR_CS0_CONFIG);
131         out_be32(&im->ddr.timing_cfg_0, CONFIG_SYS_DDR_TIMING_0);
132         out_be32(&im->ddr.timing_cfg_1, CONFIG_SYS_DDR_TIMING_1);
133         out_be32(&im->ddr.timing_cfg_2, CONFIG_SYS_DDR_TIMING_2);
134         out_be32(&im->ddr.timing_cfg_3, CONFIG_SYS_DDR_TIMING_3);
135         out_be32(&im->ddr.sdram_cfg, CONFIG_SYS_DDR_SDRAM_CFG);
136         out_be32(&im->ddr.sdram_cfg2, CONFIG_SYS_DDR_SDRAM_CFG2);
137         out_be32(&im->ddr.sdram_mode, CONFIG_SYS_DDR_MODE);
138         out_be32(&im->ddr.sdram_mode2, CONFIG_SYS_DDR_MODE2);
139         out_be32(&im->ddr.sdram_interval, CONFIG_SYS_DDR_INTERVAL);
140         out_be32(&im->ddr.sdram_clk_cntl, CONFIG_SYS_DDR_CLK_CNTL);
141         udelay(200);
142         setbits_be32(&im->ddr.sdram_cfg, SDRAM_CFG_MEM_EN);
143
144         msize = CONFIG_SYS_DDR_SIZE << 20;
145         disable_addr_trans();
146         msize = get_ram_size(CONFIG_SYS_SDRAM_BASE, msize);
147         enable_addr_trans();
148         msize /= (1024 * 1024);
149         if (CONFIG_SYS_DDR_SIZE != msize) {
150                 for (ddr_size = msize << 20, ddr_size_log2 = 0;
151                         (ddr_size > 1);
152                         ddr_size = ddr_size >> 1, ddr_size_log2++)
153                         if (ddr_size & 1)
154                                 return -1;
155                 out_be32(&im->sysconf.ddrlaw[0].ar,
156                         (LAWAR_EN | ((ddr_size_log2 - 1) & LAWAR_SIZE)));
157                 out_be32(&im->ddr.csbnds[0].csbnds,
158                         (((msize / 16) - 1) & 0xff));
159         }
160
161         return msize;
162 }
163
164 int dram_init(void)
165 {
166         immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
167         u32 msize = 0;
168
169         if ((in_be32(&im->sysconf.immrbar) & IMMRBAR_BASE_ADDR) != (u32)im)
170                 return -ENXIO;
171
172         out_be32(&im->sysconf.ddrlaw[0].bar,
173                 CONFIG_SYS_SDRAM_BASE & LAWBAR_BAR);
174         msize = fixed_sdram();
175
176 #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
177         /*
178          * Initialize DDR ECC byte
179          */
180         ddr_enable_ecc(msize * 1024 * 1024);
181 #endif
182
183         /* return total bus SDRAM size(bytes)  -- DDR */
184         gd->ram_size = msize * 1024 * 1024;
185
186         return 0;
187 }
188
189 int checkboard(void)
190 {
191         puts("Board: Hitachi " CONFIG_SYS_CONFIG_NAME);
192
193         if (piggy_present())
194                 puts(" with PIGGY.");
195         puts("\n");
196         return 0;
197 }
198
199 int ft_board_setup(void *blob, struct bd_info *bd)
200 {
201         ft_cpu_setup(blob, bd);
202
203         return 0;
204 }
205
206 #if defined(CONFIG_HUSH_INIT_VAR)
207 int hush_init_var(void)
208 {
209         ivm_analyze_eeprom(ivm_content, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
210         return 0;
211 }
212 #endif
213
214 #if defined(CONFIG_POST)
215 int post_hotkeys_pressed(void)
216 {
217         int testpin = 0;
218         struct km_bec_fpga *base =
219                 (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
220         int testpin_reg = in_8(&base->CONFIG_TESTPIN_REG);
221         testpin = (testpin_reg & CONFIG_TESTPIN_MASK) != 0;
222         debug("post_hotkeys_pressed: %d\n", !testpin);
223         return testpin;
224 }
225
226 ulong post_word_load(void)
227 {
228         void* addr = (ulong *) (CPM_POST_WORD_ADDR);
229         debug("post_word_load 0x%08lX:  0x%08X\n", (ulong)addr, in_le32(addr));
230         return in_le32(addr);
231
232 }
233 void post_word_store(ulong value)
234 {
235         void* addr = (ulong *) (CPM_POST_WORD_ADDR);
236         debug("post_word_store 0x%08lX: 0x%08lX\n", (ulong)addr, value);
237         out_le32(addr, value);
238 }
239
240 int arch_memory_test_prepare(u32 *vstart, u32 *size, phys_addr_t *phys_offset)
241 {
242         *vstart = CONFIG_SYS_MEMTEST_START;
243         *size = CONFIG_SYS_MEMTEST_END - CONFIG_SYS_MEMTEST_START;
244         debug("arch_memory_test_prepare 0x%08X 0x%08X\n", *vstart, *size);
245
246         return 0;
247 }
248 #endif