b240c7649ced3a11b24a3da393761fe56f9fcbd5
[oweals/u-boot.git] / board / mscc / jr2 / jr2.c
1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3  * Copyright (c) 2018 Microsemi Corporation
4  */
5
6 #include <common.h>
7 #include <image.h>
8 #include <init.h>
9 #include <asm/io.h>
10 #include <led.h>
11 #include <miiphy.h>
12
13 enum {
14         BOARD_TYPE_PCB110 = 0xAABBCE00,
15         BOARD_TYPE_PCB111,
16         BOARD_TYPE_PCB112,
17 };
18
19 int board_early_init_r(void)
20 {
21         /* Prepare SPI controller to be used in master mode */
22         writel(0, BASE_CFG + ICPU_SW_MODE);
23         clrsetbits_le32(BASE_CFG + ICPU_GENERAL_CTRL,
24                         ICPU_GENERAL_CTRL_IF_SI_OWNER_M,
25                         ICPU_GENERAL_CTRL_IF_SI_OWNER(2));
26
27         /* Address of boot parameters */
28         gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE;
29
30         /* LED setup */
31         if (IS_ENABLED(CONFIG_LED))
32                 led_default_state();
33
34         return 0;
35 }
36
37 static void vcoreiii_gpio_set_alternate(int gpio, int mode)
38 {
39         u32 mask;
40         u32 val0, val1;
41         void __iomem *reg0, *reg1;
42
43         if (gpio < 32) {
44                 mask = BIT(gpio);
45                 reg0 = BASE_DEVCPU_GCB + GPIO_GPIO_ALT(0);
46                 reg1 = BASE_DEVCPU_GCB + GPIO_GPIO_ALT(1);
47         } else {
48                 gpio -= 32;
49                 mask = BIT(gpio);
50                 reg0 = BASE_DEVCPU_GCB + GPIO_GPIO_ALT1(0);
51                 reg1 = BASE_DEVCPU_GCB + GPIO_GPIO_ALT1(1);
52         }
53         val0 = readl(reg0);
54         val1 = readl(reg1);
55         if (mode == 1) {
56                 writel(val0 | mask, reg0);
57                 writel(val1 & ~mask, reg1);
58         } else if (mode == 2) {
59                 writel(val0 & ~mask, reg0);
60                 writel(val1 | mask, reg1);
61         } else if (mode == 3) {
62                 writel(val0 | mask, reg0);
63                 writel(val1 | mask, reg1);
64         } else {
65                 writel(val0 & ~mask, reg0);
66                 writel(val1 & ~mask, reg1);
67         }
68 }
69
70 int board_phy_config(struct phy_device *phydev)
71 {
72         if (gd->board_type == BOARD_TYPE_PCB110 ||
73             gd->board_type == BOARD_TYPE_PCB112) {
74                 phy_write(phydev, 0, 31, 0x10);
75                 phy_write(phydev, 0, 18, 0x80F0);
76                 while (phy_read(phydev, 0, 18) & 0x8000)
77                         ;
78                 phy_write(phydev, 0, 31, 0);
79         }
80         if (gd->board_type == BOARD_TYPE_PCB111) {
81                 phy_write(phydev, 0, 31, 0x10);
82                 phy_write(phydev, 0, 18, 0x80A0);
83                 while (phy_read(phydev, 0, 18) & 0x8000)
84                         ;
85                 phy_write(phydev, 0, 14, 0x800);
86                 phy_write(phydev, 0, 31, 0);
87         }
88
89         return 0;
90 }
91
92 void board_debug_uart_init(void)
93 {
94         /* too early for the pinctrl driver, so configure the UART pins here */
95         vcoreiii_gpio_set_alternate(10, 1);
96         vcoreiii_gpio_set_alternate(11, 1);
97 }
98
99 static void do_board_detect(void)
100 {
101         int i;
102         u16 pval;
103
104         /* MIIM 1 + 2  MDC/MDIO */
105         for (i = 56; i < 60; i++)
106                 vcoreiii_gpio_set_alternate(i, 1);
107
108         /* small delay for settling the pins */
109         mdelay(30);
110
111         if (mscc_phy_rd(0, 0x10, 0x3, &pval) == 0 &&
112             ((pval >> 4) & 0x3F) == 0x3c) {
113                 gd->board_type = BOARD_TYPE_PCB112; /* Serval2-NID */
114         } else if (mscc_phy_rd(1, 0x0, 0x3, &pval) == 0 &&
115                    ((pval >> 4) & 0x3F) == 0x3c) {
116                 gd->board_type = BOARD_TYPE_PCB110; /* Jr2-24 */
117         } else {
118                 /* Fall-back */
119                 gd->board_type = BOARD_TYPE_PCB111; /* Jr2-48 */
120         }
121 }
122
123 #if defined(CONFIG_MULTI_DTB_FIT)
124 int board_fit_config_name_match(const char *name)
125 {
126         if (gd->board_type == BOARD_TYPE_PCB110 &&
127             strcmp(name, "jr2_pcb110") == 0)
128                 return 0;
129
130         if (gd->board_type == BOARD_TYPE_PCB111 &&
131             strcmp(name, "jr2_pcb111") == 0)
132                 return 0;
133
134         if (gd->board_type == BOARD_TYPE_PCB112 &&
135             strcmp(name, "serval2_pcb112") == 0)
136                 return 0;
137
138         return -1;
139 }
140 #endif
141
142 #if defined(CONFIG_DTB_RESELECT)
143 int embedded_dtb_select(void)
144 {
145         do_board_detect();
146         fdtdec_setup();
147
148         return 0;
149 }
150 #endif