common: Drop linux/delay.h from common header
[oweals/u-boot.git] / board / freescale / mpc837xemds / mpc837xemds.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2007,2010 Freescale Semiconductor, Inc.
4  * Dave Liu <daveliu@freescale.com>
5  */
6
7 #include <common.h>
8 #include <hwconfig.h>
9 #include <i2c.h>
10 #include <init.h>
11 #include <net.h>
12 #include <asm/io.h>
13 #include <asm/fsl_mpc83xx_serdes.h>
14 #include <spd_sdram.h>
15 #include <tsec.h>
16 #include <linux/delay.h>
17 #include <linux/libfdt.h>
18 #include <fdt_support.h>
19 #include <fsl_esdhc.h>
20 #include <fsl_mdio.h>
21 #include <phy.h>
22 #include "pci.h"
23 #include "../common/pq-mds-pib.h"
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 int board_early_init_f(void)
28 {
29         u8 *bcsr = (u8 *)CONFIG_SYS_BCSR;
30
31         /* Enable flash write */
32         bcsr[0x9] &= ~0x04;
33         /* Clear all of the interrupt of BCSR */
34         bcsr[0xe] = 0xff;
35
36 #ifdef CONFIG_FSL_SERDES
37         immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
38         u32 spridr = in_be32(&immr->sysconf.spridr);
39
40         /* we check only part num, and don't look for CPU revisions */
41         switch (PARTID_NO_E(spridr)) {
42         case SPR_8377:
43                 fsl_setup_serdes(CONFIG_FSL_SERDES1, FSL_SERDES_PROTO_SATA,
44                                 FSL_SERDES_CLK_100, FSL_SERDES_VDD_1V);
45                 break;
46         case SPR_8378:
47                 fsl_setup_serdes(CONFIG_FSL_SERDES1, FSL_SERDES_PROTO_SGMII,
48                                 FSL_SERDES_CLK_125, FSL_SERDES_VDD_1V);
49                 break;
50         case SPR_8379:
51                 fsl_setup_serdes(CONFIG_FSL_SERDES1, FSL_SERDES_PROTO_SATA,
52                                 FSL_SERDES_CLK_100, FSL_SERDES_VDD_1V);
53                 fsl_setup_serdes(CONFIG_FSL_SERDES2, FSL_SERDES_PROTO_SATA,
54                                 FSL_SERDES_CLK_100, FSL_SERDES_VDD_1V);
55                 break;
56         default:
57                 printf("serdes not configured: unknown CPU part number: "
58                                 "%04x\n", spridr >> 16);
59                 break;
60         }
61 #endif /* CONFIG_FSL_SERDES */
62         return 0;
63 }
64
65 #ifdef CONFIG_FSL_ESDHC
66 int board_mmc_init(bd_t *bd)
67 {
68         struct immap __iomem *im = (struct immap __iomem *)CONFIG_SYS_IMMR;
69         u8 *bcsr = (u8 *)CONFIG_SYS_BCSR;
70
71         if (!hwconfig("esdhc"))
72                 return 0;
73
74         /* Set SPI_SD, SER_SD, and IRQ4_WP so that SD signals go through */
75         bcsr[0xc] |= 0x4c;
76
77         /* Set proper bits in SICR to allow SD signals through */
78         clrsetbits_be32(&im->sysconf.sicrl, SICRL_USB_B, SICRL_USB_B_SD);
79         clrsetbits_be32(&im->sysconf.sicrh, SICRH_GPIO2_E | SICRH_SPI,
80                         SICRH_GPIO2_E_SD | SICRH_SPI_SD);
81
82         return fsl_esdhc_mmc_init(bd);
83 }
84 #endif
85
86 #if defined(CONFIG_TSEC1) || defined(CONFIG_TSEC2)
87 int board_eth_init(bd_t *bd)
88 {
89         struct fsl_pq_mdio_info mdio_info;
90         struct tsec_info_struct tsec_info[2];
91         struct immap __iomem *im = (struct immap __iomem *)CONFIG_SYS_IMMR;
92         u32 rcwh = in_be32(&im->reset.rcwh);
93         u32 tsec_mode;
94         int num = 0;
95
96         /* New line after Net: */
97         printf("\n");
98
99 #ifdef CONFIG_TSEC1
100         SET_STD_TSEC_INFO(tsec_info[num], 1);
101
102         printf(CONFIG_TSEC1_NAME ": ");
103
104         tsec_mode = rcwh & HRCWH_TSEC1M_MASK;
105         if (tsec_mode == HRCWH_TSEC1M_IN_RGMII) {
106                 printf("RGMII\n");
107                 /* this is default, no need to fixup */
108         } else if (tsec_mode == HRCWH_TSEC1M_IN_SGMII) {
109                 printf("SGMII\n");
110                 tsec_info[num].phyaddr = TSEC1_PHY_ADDR_SGMII;
111                 tsec_info[num].flags = TSEC_GIGABIT;
112         } else {
113                 printf("unsupported PHY type\n");
114         }
115         num++;
116 #endif
117 #ifdef CONFIG_TSEC2
118         SET_STD_TSEC_INFO(tsec_info[num], 2);
119
120         printf(CONFIG_TSEC2_NAME ": ");
121
122         tsec_mode = rcwh & HRCWH_TSEC2M_MASK;
123         if (tsec_mode == HRCWH_TSEC2M_IN_RGMII) {
124                 printf("RGMII\n");
125                 /* this is default, no need to fixup */
126         } else if (tsec_mode == HRCWH_TSEC2M_IN_SGMII) {
127                 printf("SGMII\n");
128                 tsec_info[num].phyaddr = TSEC2_PHY_ADDR_SGMII;
129                 tsec_info[num].flags = TSEC_GIGABIT;
130         } else {
131                 printf("unsupported PHY type\n");
132         }
133         num++;
134 #endif
135
136         mdio_info.regs = (struct tsec_mii_mng *)CONFIG_SYS_MDIO_BASE_ADDR;
137         mdio_info.name = DEFAULT_MII_NAME;
138         fsl_pq_mdio_init(bd, &mdio_info);
139
140         return tsec_eth_init(bd, tsec_info, num);
141 }
142
143 static void __ft_tsec_fixup(void *blob, bd_t *bd, const char *alias,
144                             int phy_addr)
145 {
146         const u32 *ph;
147         int off;
148         int err;
149
150         off = fdt_path_offset(blob, alias);
151         if (off < 0) {
152                 printf("WARNING: could not find %s alias: %s.\n", alias,
153                         fdt_strerror(off));
154                 return;
155         }
156
157         err = fdt_fixup_phy_connection(blob, off, PHY_INTERFACE_MODE_SGMII);
158
159         if (err) {
160                 printf("WARNING: could not set phy-connection-type for %s: "
161                         "%s.\n", alias, fdt_strerror(err));
162                 return;
163         }
164
165         ph = (u32 *)fdt_getprop(blob, off, "phy-handle", 0);
166         if (!ph) {
167                 printf("WARNING: could not get phy-handle for %s.\n",
168                         alias);
169                 return;
170         }
171
172         off = fdt_node_offset_by_phandle(blob, *ph);
173         if (off < 0) {
174                 printf("WARNING: could not get phy node for %s: %s\n", alias,
175                         fdt_strerror(off));
176                 return;
177         }
178
179         phy_addr = cpu_to_fdt32(phy_addr);
180         err = fdt_setprop(blob, off, "reg", &phy_addr, sizeof(phy_addr));
181         if (err < 0) {
182                 printf("WARNING: could not set phy node's reg for %s: "
183                         "%s.\n", alias, fdt_strerror(err));
184                 return;
185         }
186 }
187
188 static void ft_tsec_fixup(void *blob, bd_t *bd)
189 {
190         struct immap __iomem *im = (struct immap __iomem *)CONFIG_SYS_IMMR;
191         u32 rcwh = in_be32(&im->reset.rcwh);
192         u32 tsec_mode;
193
194 #ifdef CONFIG_TSEC1
195         tsec_mode = rcwh & HRCWH_TSEC1M_MASK;
196         if (tsec_mode == HRCWH_TSEC1M_IN_SGMII)
197                 __ft_tsec_fixup(blob, bd, "ethernet0", TSEC1_PHY_ADDR_SGMII);
198 #endif
199
200 #ifdef CONFIG_TSEC2
201         tsec_mode = rcwh & HRCWH_TSEC2M_MASK;
202         if (tsec_mode == HRCWH_TSEC2M_IN_SGMII)
203                 __ft_tsec_fixup(blob, bd, "ethernet1", TSEC2_PHY_ADDR_SGMII);
204 #endif
205 }
206 #else
207 static inline void ft_tsec_fixup(void *blob, bd_t *bd) {}
208 #endif /* defined(CONFIG_TSEC1) || defined(CONFIG_TSEC2) */
209
210 int board_early_init_r(void)
211 {
212 #ifdef CONFIG_PQ_MDS_PIB
213         pib_init();
214 #endif
215         return 0;
216 }
217
218 #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
219 extern void ddr_enable_ecc(unsigned int dram_size);
220 #endif
221 int fixed_sdram(void);
222
223 int dram_init(void)
224 {
225         volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
226         u32 msize = 0;
227
228         if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32) im)
229                 return -ENXIO;
230
231 #if defined(CONFIG_SPD_EEPROM)
232         msize = spd_sdram();
233 #else
234         msize = fixed_sdram();
235 #endif
236
237 #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
238         /* Initialize DDR ECC byte */
239         ddr_enable_ecc(msize * 1024 * 1024);
240 #endif
241
242         /* return total bus DDR size(bytes) */
243         gd->ram_size = msize * 1024 * 1024;
244
245         return 0;
246 }
247
248 #if !defined(CONFIG_SPD_EEPROM)
249 /*************************************************************************
250  *  fixed sdram init -- doesn't use serial presence detect.
251  ************************************************************************/
252 int fixed_sdram(void)
253 {
254         volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
255         u32 msize = CONFIG_SYS_DDR_SIZE * 1024 * 1024;
256         u32 msize_log2 = __ilog2(msize);
257
258         im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE & 0xfffff000;
259         im->sysconf.ddrlaw[0].ar = LBLAWAR_EN | (msize_log2 - 1);
260
261 #if (CONFIG_SYS_DDR_SIZE != 512)
262 #warning Currenly any ddr size other than 512 is not supported
263 #endif
264         im->sysconf.ddrcdr = CONFIG_SYS_DDRCDR_VALUE;
265         udelay(50000);
266
267         im->ddr.sdram_clk_cntl = CONFIG_SYS_DDR_SDRAM_CLK_CNTL;
268         udelay(1000);
269
270         im->ddr.csbnds[0].csbnds = CONFIG_SYS_DDR_CS0_BNDS;
271         im->ddr.cs_config[0] = CONFIG_SYS_DDR_CS0_CONFIG;
272         udelay(1000);
273
274         im->ddr.timing_cfg_0 = CONFIG_SYS_DDR_TIMING_0;
275         im->ddr.timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1;
276         im->ddr.timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2;
277         im->ddr.timing_cfg_3 = CONFIG_SYS_DDR_TIMING_3;
278         im->ddr.sdram_cfg = CONFIG_SYS_DDR_SDRAM_CFG;
279         im->ddr.sdram_cfg2 = CONFIG_SYS_DDR_SDRAM_CFG2;
280         im->ddr.sdram_mode = CONFIG_SYS_DDR_MODE;
281         im->ddr.sdram_mode2 = CONFIG_SYS_DDR_MODE2;
282         im->ddr.sdram_interval = CONFIG_SYS_DDR_INTERVAL;
283         __asm__ __volatile__("sync");
284         udelay(1000);
285
286         im->ddr.sdram_cfg |= SDRAM_CFG_MEM_EN;
287         udelay(2000);
288         return CONFIG_SYS_DDR_SIZE;
289 }
290 #endif /*!CONFIG_SYS_SPD_EEPROM */
291
292 int checkboard(void)
293 {
294         puts("Board: Freescale MPC837xEMDS\n");
295         return 0;
296 }
297
298 #ifdef CONFIG_PCI
299 int board_pci_host_broken(void)
300 {
301         struct immap __iomem *im = (struct immap __iomem *)CONFIG_SYS_IMMR;
302         const u32 rcw_mask = HRCWH_PCI1_ARBITER_ENABLE | HRCWH_PCI_HOST;
303
304         /* It's always OK in case of external arbiter. */
305         if (hwconfig_subarg_cmp("pci", "arbiter", "external"))
306                 return 0;
307
308         if ((in_be32(&im->reset.rcwh) & rcw_mask) != rcw_mask)
309                 return 1;
310
311         return 0;
312 }
313
314 static void ft_pci_fixup(void *blob, bd_t *bd)
315 {
316         const char *status = "broken (no arbiter)";
317         int off;
318         int err;
319
320         off = fdt_path_offset(blob, "pci0");
321         if (off < 0) {
322                 printf("WARNING: could not find pci0 alias: %s.\n",
323                         fdt_strerror(off));
324                 return;
325         }
326
327         err = fdt_setprop(blob, off, "status", status, strlen(status) + 1);
328         if (err) {
329                 printf("WARNING: could not set status for pci0: %s.\n",
330                         fdt_strerror(err));
331                 return;
332         }
333 }
334 #endif
335
336 #if defined(CONFIG_OF_BOARD_SETUP)
337 int ft_board_setup(void *blob, bd_t *bd)
338 {
339         ft_cpu_setup(blob, bd);
340         ft_tsec_fixup(blob, bd);
341         fsl_fdt_fixup_dr_usb(blob, bd);
342         fdt_fixup_esdhc(blob, bd);
343 #ifdef CONFIG_PCI
344         ft_pci_setup(blob, bd);
345         if (board_pci_host_broken())
346                 ft_pci_fixup(blob, bd);
347         ft_pcie_fixup(blob, bd);
348 #endif
349
350         return 0;
351 }
352 #endif /* CONFIG_OF_BOARD_SETUP */