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