Merge branch 'next'
[platform/kernel/u-boot.git] / board / keymile / kmcent2 / kmcent2.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016 Keymile AG
4  * Rainer Boschung <rainer.boschung@keymile.com>
5  *
6  * Copyright 2013 Freescale Semiconductor, Inc.
7  */
8
9 #include <asm/cache.h>
10 #include <asm/fsl_fdt.h>
11 #include <asm/fsl_law.h>
12 #include <asm/fsl_liodn.h>
13 #include <asm/fsl_portals.h>
14 #include <asm/fsl_serdes.h>
15 #include <asm/immap_85xx.h>
16 #include <asm/mmu.h>
17 #include <asm/processor.h>
18 #include <fdt_support.h>
19 #include <fm_eth.h>
20 #include <hwconfig.h>
21 #include <image.h>
22 #include <linux/compiler.h>
23 #include <net.h>
24 #include <netdev.h>
25 #include <vsc9953.h>
26
27 #include "../common/common.h"
28 #include "../common/qrio.h"
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 static uchar ivm_content[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
33
34 int checkboard(void)
35 {
36         printf("Board: Hitachi Power Grids %s\n", KM_BOARD_NAME);
37
38         return 0;
39 }
40
41 #define RSTRQSR1_WDT_RR 0x00200000
42 #define RSTRQSR1_SW_RR  0x00100000
43
44 int board_early_init_f(void)
45 {
46         struct fsl_ifc ifc = {(void *)CONFIG_SYS_IFC_ADDR, (void *)NULL};
47         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
48         bool cpuwd_flag = false;
49
50         /* board specific IFC configuration: increased bus turnaround time */
51         setbits_be32(&ifc.gregs->ifc_gcr, 8 << IFC_GCR_TBCTL_TRN_TIME_SHIFT);
52
53         /* configure mode for uP reset request */
54         qrio_uprstreq(UPREQ_CORE_RST);
55
56         /* board only uses the DDR_MCK0, so disable the DDR_MCK1 */
57         setbits_be32(&gur->ddrclkdr, 0x40000000);
58
59         /* set reset reason according CPU register */
60         if ((gur->rstrqsr1 & (RSTRQSR1_WDT_RR | RSTRQSR1_SW_RR)) ==
61             RSTRQSR1_WDT_RR)
62                 cpuwd_flag = true;
63
64         qrio_cpuwd_flag(cpuwd_flag);
65         /* clear CPU bits by writing 1 */
66         setbits_be32(&gur->rstrqsr1, RSTRQSR1_WDT_RR | RSTRQSR1_SW_RR);
67
68         /* configure PRST lines for the application: */
69         /*
70          * ETHSW_DDR_RST:
71          * reset at power-up and unit reset only and enable WD on it
72          */
73         qrio_prstcfg(KM_ETHSW_DDR_RST, PRSTCFG_POWUP_UNIT_RST);
74         qrio_wdmask(KM_ETHSW_DDR_RST, true);
75         /*
76          * XES_PHY_RST:
77          * reset at power-up and unit reset only and enable WD on it
78          */
79         qrio_prstcfg(KM_XES_PHY_RST, PRSTCFG_POWUP_UNIT_RST);
80         qrio_wdmask(KM_XES_PHY_RST, true);
81         /*
82          * ES_PHY_RST:
83          * reset at power-up and unit reset only and enable WD on it
84          */
85         qrio_prstcfg(KM_ES_PHY_RST, PRSTCFG_POWUP_UNIT_RST);
86         qrio_wdmask(KM_ES_PHY_RST, true);
87         /*
88          * EFE_RST:
89          * reset at power-up and unit reset only and enable WD on it
90          */
91         qrio_prstcfg(KM_EFE_RST, PRSTCFG_POWUP_UNIT_RST);
92         qrio_wdmask(KM_EFE_RST, true);
93         /*
94          * BFTIC4_RST:
95          * reset at power-up and unit reset only and enable WD on it
96          */
97         qrio_prstcfg(KM_BFTIC4_RST, PRSTCFG_POWUP_UNIT_RST);
98         qrio_wdmask(KM_BFTIC4_RST, true);
99         /*
100          * DPAXE_RST:
101          * reset at power-up and unit reset only and enable WD on it
102          */
103         qrio_prstcfg(KM_DPAXE_RST, PRSTCFG_POWUP_UNIT_RST);
104         qrio_wdmask(KM_DPAXE_RST, true);
105         /*
106          * PEXSW_RST:
107          * reset at power-up and unit reset only, deassert reset w/o WD
108          */
109         qrio_prstcfg(KM_PEXSW_RST, PRSTCFG_POWUP_UNIT_RST);
110         qrio_prst(KM_PEXSW_RST, false, false);
111         /*
112          * PEXSW_NT_RST:
113          * reset at power-up and unit reset only, deassert reset w/o WD
114          */
115         qrio_prstcfg(KM_PEXSW_NT_RST, PRSTCFG_POWUP_UNIT_RST);
116         qrio_prst(KM_PEXSW_NT_RST, false, false);
117         /*
118          * BOBCAT_RST:
119          * reset at power-up and unit reset only, deassert reset w/o WD
120          */
121         qrio_prstcfg(KM_BOBCAT_RST, PRSTCFG_POWUP_UNIT_RST);
122         qrio_prst(KM_BOBCAT_RST, false, false);
123         /*
124          * FEMT_RST:
125          * reset at power-up and unit reset only and enable WD
126          */
127         qrio_prstcfg(KM_FEMT_RST, PRSTCFG_POWUP_UNIT_RST);
128         qrio_wdmask(KM_FEMT_RST, true);
129         /*
130          * FOAM_RST:
131          * reset at power-up and unit reset only and enable WD
132          */
133         qrio_prstcfg(KM_FOAM_RST, PRSTCFG_POWUP_UNIT_RST);
134         qrio_wdmask(KM_FOAM_RST, true);
135
136         return 0;
137 }
138
139 int board_early_init_r(void)
140 {
141         int ret = 0;
142
143         const unsigned int flashbase = CONFIG_SYS_FLASH_BASE;
144         int flash_esel = find_tlb_idx((void *)flashbase, 1);
145
146         /*
147          * Remap Boot flash region to caching-inhibited
148          * so that flash can be erased properly.
149          */
150
151         /* Flush d-cache and invalidate i-cache of any FLASH data */
152         flush_dcache();
153         invalidate_icache();
154
155         if (flash_esel == -1) {
156                 /* very unlikely unless something is messed up */
157                 puts("Error: Could not find TLB for FLASH BASE\n");
158                 flash_esel = 2; /* give our best effort to continue */
159         } else {
160                 /* invalidate existing TLB entry for flash */
161                 disable_tlb(flash_esel);
162         }
163
164         set_tlb(1, flashbase, CONFIG_SYS_FLASH_BASE_PHYS,
165                 MAS3_SX | MAS3_SW | MAS3_SR, MAS2_I | MAS2_G,
166                 0, flash_esel, BOOKE_PAGESZ_256M, 1);
167
168         set_liodns();
169         setup_qbman_portals();
170
171         qrio_set_leds();
172
173         /* enable Application Buffer */
174         qrio_enable_app_buffer();
175
176         return ret;
177 }
178
179 unsigned long get_serial_clock(unsigned long dummy)
180 {
181         return (gd->bus_clk / 2);
182 }
183
184 int misc_init_f(void)
185 {
186         /* configure QRIO pis for i2c deblocking */
187         i2c_deblock_gpio_cfg();
188
189         /*
190          * CFE_RST (front phy):
191          * reset at power-up, unit and core reset, deasset reset w/o WD
192          */
193         qrio_prstcfg(KM_CFE_RST, PRSTCFG_POWUP_UNIT_CORE_RST);
194         qrio_prst(KM_CFE_RST, false, false);
195
196         /*
197          * ZL30158_RST (PTP clock generator):
198          * reset at power-up only, deassert reset and enable WD on it
199          */
200         qrio_prstcfg(KM_ZL30158_RST, PRSTCFG_POWUP_RST);
201         qrio_prst(KM_ZL30158_RST, false, false);
202
203         /*
204          * ZL30364_RST (EEC generator):
205          * reset at power-up only, deassert reset and enable WD on it
206          */
207         qrio_prstcfg(KM_ZL30364_RST, PRSTCFG_POWUP_RST);
208         qrio_prst(KM_ZL30364_RST, false, false);
209
210         return 0;
211 }
212
213 #define USED_SRDS_BANK 0
214 #define EXPECTED_SRDS_RFCK SRDS_PLLCR0_RFCK_SEL_100
215
216 #define BRG01_IOCLK12   0x02000000
217 #define EC2_GTX_CLK125  0x08000000
218
219 int misc_init_r(void)
220 {
221         serdes_corenet_t *regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
222         struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_MPC85xx_SCFG;
223         ccsr_gur_t __iomem *gur = (ccsr_gur_t __iomem *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
224
225         /* check SERDES bank 0 reference clock */
226         u32 actual = in_be32(&regs->bank[USED_SRDS_BANK].pllcr0);
227
228         if (actual & SRDS_PLLCR0_POFF)
229                 printf("Warning: SERDES bank %u pll is off\n", USED_SRDS_BANK);
230         if ((actual & SRDS_PLLCR0_RFCK_SEL_MASK) != EXPECTED_SRDS_RFCK) {
231                 printf("Warning: SERDES bank %u expects %sMHz clock, is %sMHz\n",
232                        USED_SRDS_BANK,
233                        serdes_clock_to_string(EXPECTED_SRDS_RFCK),
234                        serdes_clock_to_string(actual));
235         }
236
237         /* QE IO clk : BRG01 is used over clk12 for HDLC clk (20 MhZ) */
238         out_be32(&scfg->qeioclkcr,
239                  in_be32(&scfg->qeioclkcr) | BRG01_IOCLK12);
240
241         ivm_read_eeprom(ivm_content, CONFIG_SYS_IVM_EEPROM_MAX_LEN,
242                         CONFIG_PIGGY_MAC_ADDRESS_OFFSET);
243
244         /* Fix polarity of Card Detect and Write Protect */
245         out_be32(&gur->sdhcpcr, 0xFFFFFFFF);
246
247         /*
248          * EC1 is disabled in our design, so we must explicitly set GTXCLKSEL
249          * to EC2
250          */
251         out_be32(&scfg->emiiocr, in_be32(&scfg->emiiocr) | EC2_GTX_CLK125);
252
253         return 0;
254 }
255
256 int hush_init_var(void)
257 {
258         ivm_analyze_eeprom(ivm_content, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
259         return 0;
260 }
261
262 int last_stage_init(void)
263 {
264         const char *kmem;
265         /* DIP switch support on BFTIC */
266         struct bfticu_iomap *bftic4 =
267                 (struct bfticu_iomap *)SYS_BFTIC_BASE;
268         u8 dip_switch = in_8((u8 *)&bftic4->mswitch) & BFTICU_DIPSWITCH_MASK;
269
270         if (dip_switch != 0) {
271                 /* start bootloader */
272                 puts("DIP:   Enabled\n");
273                 env_set("actual_bank", "0");
274         }
275
276         set_km_env();
277
278         /*
279          * bootm_size is used to fixup the FDT memory node
280          * set it to kernelmem that has the same value
281          */
282         kmem = env_get("kernelmem");
283         if (kmem)
284                 env_set("bootm_size", kmem);
285
286         return 0;
287 }
288
289 void fdt_fixup_fman_mac_addresses(void *blob)
290 {
291         int node, ret;
292         char path[24];
293         unsigned char mac_addr[6];
294
295         /*
296          * Just the fm1-mac5 must be set by us, u-boot handle the 2 others,
297          * get the mac addr from env
298          */
299         if (!eth_env_get_enetaddr_by_index("eth", 4, mac_addr)) {
300                 printf("eth4addr env variable not defined\n");
301                 return;
302         }
303
304         /* local management port */
305         strcpy(path, "/soc/fman/ethernet@e8000");
306         node = fdt_path_offset(blob, path);
307         if (node < 0) {
308                 printf("no %s\n", path);
309                 return;
310         }
311
312         ret = fdt_setprop(blob, node, "local-mac-address", mac_addr, 6);
313         if (ret) {
314                 printf("%s\n\terror setting local-mac-address property\n",
315                        path);
316         }
317 }
318
319 int ft_board_setup(void *blob, struct bd_info *bd)
320 {
321         phys_addr_t base;
322         phys_size_t size;
323
324         ft_cpu_setup(blob, bd);
325
326         base = env_get_bootm_low();
327         size = env_get_bootm_size();
328
329         fdt_fixup_memory(blob, (u64)base, (u64)size);
330
331         fdt_fixup_liodn(blob);
332
333         fdt_fixup_fman_mac_addresses(blob);
334
335         if (hwconfig("qe-tdm"))
336                 fdt_del_diu(blob);
337         return 0;
338 }
339
340 /* DIC26_SELFTEST GPIO used to start factory test sw */
341 #define SELFTEST_PORT   QRIO_GPIO_A
342 #define SELFTEST_PIN    0
343
344 int post_hotkeys_pressed(void)
345 {
346         qrio_gpio_direction_input(SELFTEST_PORT, SELFTEST_PIN);
347         return qrio_get_gpio(SELFTEST_PORT, SELFTEST_PIN);
348 }