command: Remove the cmd_tbl_t typedef
[oweals/u-boot.git] / board / solidrun / clearfog / clearfog.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Stefan Roese <sr@denx.de>
4  */
5
6 #include <common.h>
7 #include <env.h>
8 #include <i2c.h>
9 #include <init.h>
10 #include <miiphy.h>
11 #include <net.h>
12 #include <netdev.h>
13 #include <asm/io.h>
14 #include <asm/arch/cpu.h>
15 #include <asm/arch/soc.h>
16 #include "../common/tlv_data.h"
17
18 #include "../drivers/ddr/marvell/a38x/ddr3_init.h"
19 #include <../serdes/a38x/high_speed_env_spec.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 /*
24  * Those values and defines are taken from the Marvell U-Boot version
25  * "u-boot-2013.01-15t1-clearfog"
26  */
27 #define BOARD_GPP_OUT_ENA_LOW   0xffffffff
28 #define BOARD_GPP_OUT_ENA_MID   0xffffffff
29
30 #define BOARD_GPP_OUT_VAL_LOW   0x0
31 #define BOARD_GPP_OUT_VAL_MID   0x0
32 #define BOARD_GPP_POL_LOW       0x0
33 #define BOARD_GPP_POL_MID       0x0
34
35 static struct tlv_data cf_tlv_data;
36
37 static void cf_read_tlv_data(void)
38 {
39         static bool read_once;
40
41         if (read_once)
42                 return;
43         read_once = true;
44
45         read_tlv_data(&cf_tlv_data);
46 }
47
48 /* The starting board_serdes_map reflects original Clearfog Pro usage */
49 static struct serdes_map board_serdes_map[] = {
50         {SATA0, SERDES_SPEED_3_GBPS, SERDES_DEFAULT_MODE, 0, 0},
51         {SGMII1, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0},
52         {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
53         {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
54         {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
55         {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0},
56 };
57
58 void config_cfbase_serdes_map(void)
59 {
60         board_serdes_map[4].serdes_type = USB3_HOST0;
61         board_serdes_map[4].serdes_speed = SERDES_SPEED_5_GBPS;
62         board_serdes_map[4].serdes_mode = SERDES_DEFAULT_MODE;
63 }
64
65 int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
66 {
67         cf_read_tlv_data();
68
69         /* Apply build configuration options before runtime configuration */
70         if (IS_ENABLED(CONFIG_CLEARFOG_SFP_25GB))
71                 board_serdes_map[5].serdes_speed = SERDES_SPEED_3_125_GBPS;
72
73         if (IS_ENABLED(CONFIG_CLEARFOG_CON2_SATA)) {
74                 board_serdes_map[4].serdes_type = SATA2;
75                 board_serdes_map[4].serdes_speed = SERDES_SPEED_3_GBPS;
76                 board_serdes_map[4].serdes_mode = SERDES_DEFAULT_MODE;
77                 board_serdes_map[4].swap_rx = 1;
78         }
79
80         if (IS_ENABLED(CONFIG_CLEARFOG_CON3_SATA)) {
81                 board_serdes_map[2].serdes_type = SATA1;
82                 board_serdes_map[2].serdes_speed = SERDES_SPEED_3_GBPS;
83                 board_serdes_map[2].serdes_mode = SERDES_DEFAULT_MODE;
84                 board_serdes_map[2].swap_rx = 1;
85         }
86
87         /* Apply runtime detection changes */
88         if (sr_product_is(&cf_tlv_data, "Clearfog GTR")) {
89                 board_serdes_map[0].serdes_type = PEX0;
90                 board_serdes_map[0].serdes_speed = SERDES_SPEED_5_GBPS;
91                 board_serdes_map[0].serdes_mode = PEX_ROOT_COMPLEX_X1;
92         } else if (sr_product_is(&cf_tlv_data, "Clearfog Pro")) {
93                 /* handle recognized product as noop, no adjustment required */
94         } else if (sr_product_is(&cf_tlv_data, "Clearfog Base")) {
95                 config_cfbase_serdes_map();
96         } else {
97                 /*
98                  * Fallback to static default. EEPROM TLV support is not
99                  * enabled, runtime detection failed, hardware support is not
100                  * present, EEPROM is corrupt, or an unrecognized product name
101                  * is present.
102                  */
103                 if (IS_ENABLED(CONFIG_SPL_CMD_TLV_EEPROM))
104                         puts("EEPROM TLV detection failed: ");
105                 puts("Using static config for ");
106                 if (IS_ENABLED(CONFIG_TARGET_CLEARFOG_BASE)) {
107                         puts("Clearfog Base.\n");
108                         config_cfbase_serdes_map();
109                 } else {
110                         puts("Clearfog Pro.\n");
111                 }
112         }
113
114         *serdes_map_array = board_serdes_map;
115         *count = ARRAY_SIZE(board_serdes_map);
116         return 0;
117 }
118
119 /*
120  * Define the DDR layout / topology here in the board file. This will
121  * be used by the DDR3 init code in the SPL U-Boot version to configure
122  * the DDR3 controller.
123  */
124 static struct mv_ddr_topology_map board_topology_map = {
125         DEBUG_LEVEL_ERROR,
126         0x1, /* active interfaces */
127         /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
128         { { { {0x1, 0, 0, 0},
129               {0x1, 0, 0, 0},
130               {0x1, 0, 0, 0},
131               {0x1, 0, 0, 0},
132               {0x1, 0, 0, 0} },
133             SPEED_BIN_DDR_1600K,        /* speed_bin */
134             MV_DDR_DEV_WIDTH_16BIT,     /* memory_width */
135             MV_DDR_DIE_CAP_4GBIT,       /* mem_size */
136             MV_DDR_FREQ_800,            /* frequency */
137             0, 0,                       /* cas_wl cas_l */
138             MV_DDR_TEMP_LOW,            /* temperature */
139             MV_DDR_TIM_DEFAULT} },      /* timing */
140         BUS_MASK_32BIT,                 /* Busses mask */
141         MV_DDR_CFG_DEFAULT,             /* ddr configuration data source */
142         { {0} },                        /* raw spd data */
143         {0},                            /* timing parameters */
144         { {0} },                        /* electrical configuration */
145         {0,},                           /* electrical parameters */
146         0x3,                            /* clock enable mask */
147 };
148
149 struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
150 {
151         struct if_params *ifp = &board_topology_map.interface_params[0];
152
153         cf_read_tlv_data();
154
155         switch (cf_tlv_data.ram_size) {
156         case 4:
157         default:
158                 ifp->memory_size = MV_DDR_DIE_CAP_4GBIT;
159                 break;
160         case 8:
161                 ifp->memory_size = MV_DDR_DIE_CAP_8GBIT;
162                 break;
163         }
164
165         /* Return the board topology as defined in the board code */
166         return &board_topology_map;
167 }
168
169 int board_early_init_f(void)
170 {
171         /* Configure MPP */
172         writel(0x11111111, MVEBU_MPP_BASE + 0x00);
173         writel(0x11111111, MVEBU_MPP_BASE + 0x04);
174         writel(0x10400011, MVEBU_MPP_BASE + 0x08);
175         writel(0x22043333, MVEBU_MPP_BASE + 0x0c);
176         writel(0x44400002, MVEBU_MPP_BASE + 0x10);
177         writel(0x41144004, MVEBU_MPP_BASE + 0x14);
178         writel(0x40333333, MVEBU_MPP_BASE + 0x18);
179         writel(0x00004444, MVEBU_MPP_BASE + 0x1c);
180
181         /* Set GPP Out value */
182         writel(BOARD_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
183         writel(BOARD_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
184
185         /* Set GPP Polarity */
186         writel(BOARD_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
187         writel(BOARD_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
188
189         /* Set GPP Out Enable */
190         writel(BOARD_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
191         writel(BOARD_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
192
193         return 0;
194 }
195
196 int board_init(void)
197 {
198         /* Address of boot parameters */
199         gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
200
201         /* Toggle GPIO41 to reset onboard switch and phy */
202         clrbits_le32(MVEBU_GPIO1_BASE + 0x0, BIT(9));
203         clrbits_le32(MVEBU_GPIO1_BASE + 0x4, BIT(9));
204         /* GPIO 19 on ClearFog rev 2.1 controls the uSOM onboard phy reset */
205         clrbits_le32(MVEBU_GPIO0_BASE + 0x0, BIT(19));
206         clrbits_le32(MVEBU_GPIO0_BASE + 0x4, BIT(19));
207         mdelay(1);
208         setbits_le32(MVEBU_GPIO1_BASE + 0x0, BIT(9));
209         setbits_le32(MVEBU_GPIO0_BASE + 0x0, BIT(19));
210         mdelay(10);
211
212         return 0;
213 }
214
215 int checkboard(void)
216 {
217         char *board = "Clearfog Pro";
218         if (IS_ENABLED(CONFIG_TARGET_CLEARFOG_BASE))
219                 board = "Clearfog Base";
220
221         cf_read_tlv_data();
222         if (strlen(cf_tlv_data.tlv_product_name[0]) > 0)
223                 board = cf_tlv_data.tlv_product_name[0];
224
225         printf("Board: SolidRun %s", board);
226         if (strlen(cf_tlv_data.tlv_product_name[1]) > 0)
227                 printf(", %s", cf_tlv_data.tlv_product_name[1]);
228         puts("\n");
229
230         return 0;
231 }
232
233 int board_eth_init(bd_t *bis)
234 {
235         cpu_eth_init(bis); /* Built in controller(s) come first */
236         return pci_eth_init(bis);
237 }
238
239 int board_late_init(void)
240 {
241         cf_read_tlv_data();
242
243         if (sr_product_is(&cf_tlv_data, "Clearfog Base"))
244                 env_set("fdtfile", "armada-388-clearfog-base.dtb");
245         else if (sr_product_is(&cf_tlv_data, "Clearfog GTR S4"))
246                 env_set("fdtfile", "armada-385-clearfog-gtr-s4.dtb");
247         else if (sr_product_is(&cf_tlv_data, "Clearfog GTR L8"))
248                 env_set("fdtfile", "armada-385-clearfog-gtr-l8.dtb");
249         else if (IS_ENABLED(CONFIG_TARGET_CLEARFOG_BASE))
250                 env_set("fdtfile", "armada-388-clearfog-base.dtb");
251         else
252                 env_set("fdtfile", "armada-388-clearfog-pro.dtb");
253
254         return 0;
255 }