common: Drop linux/delay.h from common header
[oweals/u-boot.git] / board / freescale / p1_p2_rdb_pc / p1_p2_rdb_pc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2010-2011, 2013 Freescale Semiconductor, Inc.
4  * Copyright 2020 NXP
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <env.h>
10 #include <hang.h>
11 #include <hwconfig.h>
12 #include <image.h>
13 #include <init.h>
14 #include <net.h>
15 #include <pci.h>
16 #include <i2c.h>
17 #include <asm/processor.h>
18 #include <asm/mmu.h>
19 #include <asm/cache.h>
20 #include <asm/immap_85xx.h>
21 #include <asm/fsl_pci.h>
22 #include <fsl_ddr_sdram.h>
23 #include <asm/io.h>
24 #include <asm/fsl_law.h>
25 #include <asm/fsl_lbc.h>
26 #include <asm/mp.h>
27 #include <miiphy.h>
28 #include <linux/delay.h>
29 #include <linux/libfdt.h>
30 #include <fdt_support.h>
31 #include <fsl_mdio.h>
32 #include <tsec.h>
33 #include <vsc7385.h>
34 #include <ioports.h>
35 #include <asm/fsl_serdes.h>
36 #include <netdev.h>
37
38 #ifdef CONFIG_QE
39
40 #define GPIO_GETH_SW_PORT       1
41 #define GPIO_GETH_SW_PIN        29
42 #define GPIO_GETH_SW_DATA       (1 << (31 - GPIO_GETH_SW_PIN))
43
44 #define GPIO_SLIC_PORT          1
45 #define GPIO_SLIC_PIN           30
46 #define GPIO_SLIC_DATA          (1 << (31 - GPIO_SLIC_PIN))
47
48 #if defined(CONFIG_TARGET_P1021RDB) && !defined(CONFIG_SYS_RAMBOOT)
49 #define GPIO_DDR_RST_PORT       1
50 #define GPIO_DDR_RST_PIN        8
51 #define GPIO_DDR_RST_DATA       (1 << (31 - GPIO_DDR_RST_PIN))
52
53 #define GPIO_2BIT_MASK          (0x3 << (32 - (GPIO_DDR_RST_PIN + 1) * 2))
54 #endif
55
56 #if defined(CONFIG_TARGET_P1025RDB) || defined(CONFIG_TARGET_P1021RDB)
57 #define PCA_IOPORT_I2C_ADDR             0x23
58 #define PCA_IOPORT_OUTPUT_CMD           0x2
59 #define PCA_IOPORT_CFG_CMD              0x6
60 #define PCA_IOPORT_QE_PIN_ENABLE        0xf8
61 #define PCA_IOPORT_QE_TDM_ENABLE        0xf6
62 #endif
63
64 const qe_iop_conf_t qe_iop_conf_tab[] = {
65         /* GPIO */
66         {1,   1, 2, 0, 0}, /* GPIO7/PB1   - LOAD_DEFAULT_N */
67 #if defined(CONFIG_TARGET_P1021RDB) && !defined(CONFIG_SYS_RAMBOOT)
68         {1,   8, 1, 1, 0}, /* GPIO10/PB8  - DDR_RST */
69 #endif
70         {0,  15, 1, 0, 0}, /* GPIO11/A15  - WDI */
71         {GPIO_GETH_SW_PORT, GPIO_GETH_SW_PIN, 1, 0, 0}, /* RST_GETH_SW_N */
72         {GPIO_SLIC_PORT, GPIO_SLIC_PIN, 1, 0, 0},       /* RST_SLIC_N */
73
74 #ifdef CONFIG_TARGET_P1025RDB
75         /* QE_MUX_MDC */
76         {1,  19, 1, 0, 1}, /* QE_MUX_MDC               */
77
78         /* QE_MUX_MDIO */
79         {1,  20, 3, 0, 1}, /* QE_MUX_MDIO              */
80
81         /* UCC_1_MII */
82         {0, 23, 2, 0, 2}, /* CLK12 */
83         {0, 24, 2, 0, 1}, /* CLK9 */
84         {0,  7, 1, 0, 2}, /* ENET1_TXD0_SER1_TXD0      */
85         {0,  9, 1, 0, 2}, /* ENET1_TXD1_SER1_TXD1      */
86         {0, 11, 1, 0, 2}, /* ENET1_TXD2_SER1_TXD2      */
87         {0, 12, 1, 0, 2}, /* ENET1_TXD3_SER1_TXD3      */
88         {0,  6, 2, 0, 2}, /* ENET1_RXD0_SER1_RXD0      */
89         {0, 10, 2, 0, 2}, /* ENET1_RXD1_SER1_RXD1      */
90         {0, 14, 2, 0, 2}, /* ENET1_RXD2_SER1_RXD2      */
91         {0, 15, 2, 0, 2}, /* ENET1_RXD3_SER1_RXD3      */
92         {0,  5, 1, 0, 2}, /* ENET1_TX_EN_SER1_RTS_B    */
93         {0, 13, 1, 0, 2}, /* ENET1_TX_ER               */
94         {0,  4, 2, 0, 2}, /* ENET1_RX_DV_SER1_CTS_B    */
95         {0,  8, 2, 0, 2}, /* ENET1_RX_ER_SER1_CD_B    */
96         {0, 17, 2, 0, 2}, /* ENET1_CRS    */
97         {0, 16, 2, 0, 2}, /* ENET1_COL    */
98
99         /* UCC_5_RMII */
100         {1, 11, 2, 0, 1}, /* CLK13 */
101         {1, 7,  1, 0, 2}, /* ENET5_TXD0_SER5_TXD0      */
102         {1, 10, 1, 0, 2}, /* ENET5_TXD1_SER5_TXD1      */
103         {1, 6, 2, 0, 2}, /* ENET5_RXD0_SER5_RXD0      */
104         {1, 9, 2, 0, 2}, /* ENET5_RXD1_SER5_RXD1      */
105         {1, 5, 1, 0, 2}, /* ENET5_TX_EN_SER5_RTS_B    */
106         {1, 4, 2, 0, 2}, /* ENET5_RX_DV_SER5_CTS_B    */
107         {1, 8, 2, 0, 2}, /* ENET5_RX_ER_SER5_CD_B    */
108 #endif
109
110         {0,  0, 0, 0, QE_IOP_TAB_END} /* END of table */
111 };
112 #endif
113
114 struct cpld_data {
115         u8 cpld_rev_major;
116         u8 pcba_rev;
117         u8 wd_cfg;
118         u8 rst_bps_sw;
119         u8 load_default_n;
120         u8 rst_bps_wd;
121         u8 bypass_enable;
122         u8 bps_led;
123         u8 status_led;                  /* offset: 0x8 */
124         u8 fxo_led;                     /* offset: 0x9 */
125         u8 fxs_led;                     /* offset: 0xa */
126         u8 rev4[2];
127         u8 system_rst;                  /* offset: 0xd */
128         u8 bps_out;
129         u8 rev5[3];
130         u8 cpld_rev_minor;
131 };
132
133 #define CPLD_WD_CFG     0x03
134 #define CPLD_RST_BSW    0x00
135 #define CPLD_RST_BWD    0x00
136 #define CPLD_BYPASS_EN  0x03
137 #define CPLD_STATUS_LED 0x01
138 #define CPLD_FXO_LED    0x01
139 #define CPLD_FXS_LED    0x0F
140 #define CPLD_SYS_RST    0x00
141
142 void board_cpld_init(void)
143 {
144         struct cpld_data *cpld_data = (void *)(CONFIG_SYS_CPLD_BASE);
145
146         out_8(&cpld_data->wd_cfg, CPLD_WD_CFG);
147         out_8(&cpld_data->status_led, CPLD_STATUS_LED);
148         out_8(&cpld_data->fxo_led, CPLD_FXO_LED);
149         out_8(&cpld_data->fxs_led, CPLD_FXS_LED);
150         out_8(&cpld_data->system_rst, CPLD_SYS_RST);
151 }
152
153 void board_gpio_init(void)
154 {
155 #ifdef CONFIG_QE
156         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
157         par_io_t *par_io = (par_io_t *) &(gur->qe_par_io);
158
159 #if defined(CONFIG_TARGET_P1021RDB) && !defined(CONFIG_SYS_RAMBOOT)
160         /* reset DDR3 */
161         setbits_be32(&par_io[GPIO_DDR_RST_PORT].cpdat, GPIO_DDR_RST_DATA);
162         udelay(1000);
163         clrbits_be32(&par_io[GPIO_DDR_RST_PORT].cpdat, GPIO_DDR_RST_DATA);
164         udelay(1000);
165         setbits_be32(&par_io[GPIO_DDR_RST_PORT].cpdat, GPIO_DDR_RST_DATA);
166         /* disable CE_PB8 */
167         clrbits_be32(&par_io[GPIO_DDR_RST_PORT].cpdir1, GPIO_2BIT_MASK);
168 #endif
169         /* Enable VSC7385 switch */
170         setbits_be32(&par_io[GPIO_GETH_SW_PORT].cpdat, GPIO_GETH_SW_DATA);
171
172         /* Enable SLIC */
173         setbits_be32(&par_io[GPIO_SLIC_PORT].cpdat, GPIO_SLIC_DATA);
174 #else
175
176         ccsr_gpio_t *pgpio = (void *)(CONFIG_SYS_MPC85xx_GPIO_ADDR);
177
178         /*
179          * GPIO10 DDR Reset, open drain
180          * GPIO7  LOAD_DEFAULT_N          Input
181          * GPIO11  WDI (watchdog input)
182          * GPIO12  Ethernet Switch Reset
183          * GPIO13  SLIC Reset
184          */
185
186         setbits_be32(&pgpio->gpdir, 0x02130000);
187 #if !defined(CONFIG_SYS_RAMBOOT) && !defined(CONFIG_SPL)
188         /* init DDR3 reset signal */
189         setbits_be32(&pgpio->gpdir, 0x00200000);
190         setbits_be32(&pgpio->gpodr, 0x00200000);
191         clrbits_be32(&pgpio->gpdat, 0x00200000);
192         udelay(1000);
193         setbits_be32(&pgpio->gpdat, 0x00200000);
194         udelay(1000);
195         clrbits_be32(&pgpio->gpdir, 0x00200000);
196 #endif
197
198 #ifdef CONFIG_VSC7385_ENET
199         /* reset VSC7385 Switch */
200         setbits_be32(&pgpio->gpdir, 0x00080000);
201         setbits_be32(&pgpio->gpdat, 0x00080000);
202 #endif
203
204 #ifdef CONFIG_SLIC
205         /* reset SLIC */
206         setbits_be32(&pgpio->gpdir, 0x00040000);
207         setbits_be32(&pgpio->gpdat, 0x00040000);
208 #endif
209 #endif
210 }
211
212 int board_early_init_f(void)
213 {
214         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
215
216         setbits_be32(&gur->pmuxcr,
217                         (MPC85xx_PMUXCR_SDHC_CD | MPC85xx_PMUXCR_SDHC_WP));
218         clrbits_be32(&gur->sdhcdcr, SDHCDCR_CD_INV);
219
220         clrbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_SD_DATA);
221         setbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_TDM_ENA);
222
223         board_gpio_init();
224         board_cpld_init();
225
226         return 0;
227 }
228
229 int checkboard(void)
230 {
231         struct cpld_data *cpld_data = (void *)(CONFIG_SYS_CPLD_BASE);
232         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
233         u8 in, out, io_config, val;
234         int bus_num = CONFIG_SYS_SPD_BUS_NUM;
235
236         printf("Board: %s CPLD: V%d.%d PCBA: V%d.0\n", CONFIG_BOARDNAME,
237                 in_8(&cpld_data->cpld_rev_major) & 0x0F,
238                 in_8(&cpld_data->cpld_rev_minor) & 0x0F,
239                 in_8(&cpld_data->pcba_rev) & 0x0F);
240
241         /* Initialize i2c early for rom_loc and flash bank information */
242         #if defined(CONFIG_DM_I2C)
243         struct udevice *dev;
244         int ret;
245
246         ret = i2c_get_chip_for_busnum(bus_num, CONFIG_SYS_I2C_PCA9557_ADDR,
247                                       1, &dev);
248         if (ret) {
249                 printf("%s: Cannot find udev for a bus %d\n", __func__,
250                        bus_num);
251                 return -ENXIO;
252         }
253
254         if (dm_i2c_read(dev, 0, &in, 1) < 0 ||
255             dm_i2c_read(dev, 1, &out, 1) < 0 ||
256             dm_i2c_read(dev, 3, &io_config, 1) < 0) {
257                 printf("Error reading i2c boot information!\n");
258                 return 0; /* Don't want to hang() on this error */
259         }
260         #else /* Non DM I2C support - will be removed */
261         i2c_set_bus_num(bus_num);
262
263         if (i2c_read(CONFIG_SYS_I2C_PCA9557_ADDR, 0, 1, &in, 1) < 0 ||
264             i2c_read(CONFIG_SYS_I2C_PCA9557_ADDR, 1, 1, &out, 1) < 0 ||
265             i2c_read(CONFIG_SYS_I2C_PCA9557_ADDR, 3, 1, &io_config, 1) < 0) {
266                 printf("Error reading i2c boot information!\n");
267                 return 0; /* Don't want to hang() on this error */
268         }
269         #endif
270
271         val = (in & io_config) | (out & (~io_config));
272
273         puts("rom_loc: ");
274         if ((val & (~__SW_BOOT_MASK)) == __SW_BOOT_SD) {
275                 puts("sd");
276 #ifdef __SW_BOOT_SPI
277         } else if ((val & (~__SW_BOOT_MASK)) == __SW_BOOT_SPI) {
278                 puts("spi");
279 #endif
280 #ifdef __SW_BOOT_NAND
281         } else if ((val & (~__SW_BOOT_MASK)) == __SW_BOOT_NAND) {
282                 puts("nand");
283 #endif
284 #ifdef __SW_BOOT_PCIE
285         } else if ((val & (~__SW_BOOT_MASK)) == __SW_BOOT_PCIE) {
286                 puts("pcie");
287 #endif
288         } else {
289                 if (val & 0x2)
290                         puts("nor lower bank");
291                 else
292                         puts("nor upper bank");
293         }
294         puts("\n");
295
296         if (val & 0x1) {
297                 setbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_SD_DATA);
298                 puts("SD/MMC : 8-bit Mode\n");
299                 puts("eSPI : Disabled\n");
300         } else {
301                 puts("SD/MMC : 4-bit Mode\n");
302                 puts("eSPI : Enabled\n");
303         }
304
305         return 0;
306 }
307
308 #if defined(CONFIG_PCI) && !defined(CONFIG_DM_PCI)
309 void pci_init_board(void)
310 {
311         fsl_pcie_init_board(0);
312 }
313 #endif
314
315 int board_early_init_r(void)
316 {
317         const unsigned int flashbase = CONFIG_SYS_FLASH_BASE;
318         int flash_esel = find_tlb_idx((void *)flashbase, 1);
319
320         /*
321          * Remap Boot flash region to caching-inhibited
322          * so that flash can be erased properly.
323          */
324
325         /* Flush d-cache and invalidate i-cache of any FLASH data */
326         flush_dcache();
327         invalidate_icache();
328
329         if (flash_esel == -1) {
330                 /* very unlikely unless something is messed up */
331                 puts("Error: Could not find TLB for FLASH BASE\n");
332                 flash_esel = 2; /* give our best effort to continue */
333         } else {
334                 /* invalidate existing TLB entry for flash */
335                 disable_tlb(flash_esel);
336         }
337
338         set_tlb(1, flashbase, CONFIG_SYS_FLASH_BASE_PHYS, /* tlb, epn, rpn */
339                 MAS3_SX|MAS3_SW|MAS3_SR, MAS2_I|MAS2_G,/* perms, wimge */
340                 0, flash_esel, BOOKE_PAGESZ_64M, 1);/* ts, esel, tsize, iprot */
341         return 0;
342 }
343
344 int board_eth_init(bd_t *bis)
345 {
346         struct fsl_pq_mdio_info mdio_info;
347         struct tsec_info_struct tsec_info[4];
348         ccsr_gur_t *gur __attribute__((unused)) =
349                 (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
350         int num = 0;
351 #ifdef CONFIG_VSC7385_ENET
352         char *tmp;
353         unsigned int vscfw_addr;
354 #endif
355
356 #ifdef CONFIG_TSEC1
357         SET_STD_TSEC_INFO(tsec_info[num], 1);
358         num++;
359 #endif
360 #ifdef CONFIG_TSEC2
361         SET_STD_TSEC_INFO(tsec_info[num], 2);
362         if (is_serdes_configured(SGMII_TSEC2)) {
363                 printf("eTSEC2 is in sgmii mode.\n");
364                 tsec_info[num].flags |= TSEC_SGMII;
365         }
366         num++;
367 #endif
368 #ifdef CONFIG_TSEC3
369         SET_STD_TSEC_INFO(tsec_info[num], 3);
370         num++;
371 #endif
372
373         if (!num) {
374                 printf("No TSECs initialized\n");
375                 return 0;
376         }
377
378 #ifdef CONFIG_VSC7385_ENET
379         /* If a VSC7385 microcode image is present, then upload it. */
380         tmp = env_get("vscfw_addr");
381         if (tmp) {
382                 vscfw_addr = simple_strtoul(tmp, NULL, 16);
383                 printf("uploading VSC7385 microcode from %x\n", vscfw_addr);
384                 if (vsc7385_upload_firmware((void *) vscfw_addr,
385                                         CONFIG_VSC7385_IMAGE_SIZE))
386                         puts("Failure uploading VSC7385 microcode.\n");
387         } else
388                 puts("No address specified for VSC7385 microcode.\n");
389 #endif
390
391         mdio_info.regs = TSEC_GET_MDIO_REGS_BASE(1);
392         mdio_info.name = DEFAULT_MII_NAME;
393
394         fsl_pq_mdio_init(bis, &mdio_info);
395
396         tsec_eth_init(bis, tsec_info, num);
397
398 #if defined(CONFIG_UEC_ETH)
399         /*  QE0 and QE3 need to be exposed for UCC1 and UCC5 Eth mode */
400         setbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_QE0);
401         setbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_QE3);
402
403         uec_standard_init(bis);
404 #endif
405
406         return pci_eth_init(bis);
407 }
408
409 #if defined(CONFIG_QE) && \
410         (defined(CONFIG_TARGET_P1025RDB) || defined(CONFIG_TARGET_P1021RDB))
411 static void fdt_board_fixup_qe_pins(void *blob)
412 {
413         unsigned int oldbus;
414         u8 val8;
415         int node;
416         fsl_lbc_t *lbc = LBC_BASE_ADDR;
417
418         if (hwconfig("qe")) {
419                 /* For QE and eLBC pins multiplexing,
420                  * there is a PCA9555 device on P1025RDB.
421                  * It control the multiplex pins' functions,
422                  * and setting the PCA9555 can switch the
423                  * function between QE and eLBC.
424                  */
425                 oldbus = i2c_get_bus_num();
426                 i2c_set_bus_num(0);
427                 if (hwconfig("tdm"))
428                         val8 = PCA_IOPORT_QE_TDM_ENABLE;
429                 else
430                         val8 = PCA_IOPORT_QE_PIN_ENABLE;
431                 i2c_write(PCA_IOPORT_I2C_ADDR, PCA_IOPORT_CFG_CMD,
432                                 1, &val8, 1);
433                 i2c_write(PCA_IOPORT_I2C_ADDR, PCA_IOPORT_OUTPUT_CMD,
434                                 1, &val8, 1);
435                 i2c_set_bus_num(oldbus);
436                 /* if run QE TDM, Set ABSWP to implement
437                  * conversion of addresses in the eLBC.
438                  */
439                 if (hwconfig("tdm")) {
440                         set_lbc_or(2, CONFIG_PMC_OR_PRELIM);
441                         set_lbc_br(2, CONFIG_PMC_BR_PRELIM);
442                         setbits_be32(&lbc->lbcr, CONFIG_SYS_LBC_LBCR);
443                 }
444         } else {
445                 node = fdt_path_offset(blob, "/qe");
446                 if (node >= 0)
447                         fdt_del_node(blob, node);
448         }
449
450         return;
451 }
452 #endif
453
454 #ifdef CONFIG_OF_BOARD_SETUP
455 int ft_board_setup(void *blob, bd_t *bd)
456 {
457         phys_addr_t base;
458         phys_size_t size;
459 #if defined(CONFIG_TARGET_P1020RDB_PD) || defined(CONFIG_TARGET_P1020RDB_PC)
460         const char *soc_usb_compat = "fsl-usb2-dr";
461         int usb_err, usb1_off, usb2_off;
462 #endif
463 #if defined(CONFIG_SDCARD) || defined(CONFIG_SPIFLASH)
464         int err;
465 #endif
466
467         ft_cpu_setup(blob, bd);
468
469         base = env_get_bootm_low();
470         size = env_get_bootm_size();
471
472         fdt_fixup_memory(blob, (u64)base, (u64)size);
473
474 #if !defined(CONFIG_DM_PCI)
475         FT_FSL_PCI_SETUP;
476 #endif
477
478 #ifdef CONFIG_QE
479         do_fixup_by_compat(blob, "fsl,qe", "status", "okay",
480                         sizeof("okay"), 0);
481 #if defined(CONFIG_TARGET_P1025RDB) || defined(CONFIG_TARGET_P1021RDB)
482         fdt_board_fixup_qe_pins(blob);
483 #endif
484 #endif
485
486 #if defined(CONFIG_HAS_FSL_DR_USB)
487         fsl_fdt_fixup_dr_usb(blob, bd);
488 #endif
489
490 #if defined(CONFIG_SDCARD) || defined(CONFIG_SPIFLASH)
491         /* Delete eLBC node as it is muxed with USB2 controller */
492         if (hwconfig("usb2")) {
493                 const char *soc_elbc_compat = "fsl,p1020-elbc";
494                 int off = fdt_node_offset_by_compatible(blob, -1,
495                                 soc_elbc_compat);
496                 if (off < 0) {
497                         printf("WARNING: could not find compatible node %s\n",
498                                soc_elbc_compat);
499                         return off;
500                 }
501                 err = fdt_del_node(blob, off);
502                 if (err < 0) {
503                         printf("WARNING: could not remove %s\n",
504                                soc_elbc_compat);
505                         return err;
506                 }
507                 return 0;
508         }
509 #endif
510
511 #if defined(CONFIG_TARGET_P1020RDB_PD) || defined(CONFIG_TARGET_P1020RDB_PC)
512 /* Delete USB2 node as it is muxed with eLBC */
513         usb1_off = fdt_node_offset_by_compatible(blob, -1,
514                 soc_usb_compat);
515         if (usb1_off < 0) {
516                 printf("WARNING: could not find compatible node %s\n",
517                        soc_usb_compat);
518                 return usb1_off;
519         }
520         usb2_off = fdt_node_offset_by_compatible(blob, usb1_off,
521                         soc_usb_compat);
522         if (usb2_off < 0) {
523                 printf("WARNING: could not find compatible node %s\n",
524                        soc_usb_compat);
525                 return usb2_off;
526         }
527         usb_err = fdt_del_node(blob, usb2_off);
528         if (usb_err < 0) {
529                 printf("WARNING: could not remove %s\n", soc_usb_compat);
530                 return usb_err;
531         }
532 #endif
533
534         return 0;
535 }
536 #endif