1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Stefan Roese <sr@denx.de>
4  * Copyright (C) 2016 Mario Six <mario.six@gdsys.cc>
5  */
6 
7 #include <common.h>
8 #include <command.h>
9 #include <dm.h>
10 #include <init.h>
11 #include <miiphy.h>
12 #include <net.h>
13 #include <tpm-v1.h>
14 #include <asm/global_data.h>
15 #include <asm/io.h>
16 #include <asm/arch/cpu.h>
17 #include <asm-generic/gpio.h>
18 #include <linux/delay.h>
19 
20 #include "../drivers/ddr/marvell/a38x/ddr3_init.h"
21 #include "../arch/arm/mach-mvebu/serdes/a38x/high_speed_env_spec.h"
22 
23 #include "keyprogram.h"
24 #include "dt_helpers.h"
25 #include "hydra.h"
26 #include "ihs_phys.h"
27 
28 DECLARE_GLOBAL_DATA_PTR;
29 
30 #define DB_GP_88F68XX_GPP_OUT_ENA_LOW	0x7fffffff
31 #define DB_GP_88F68XX_GPP_OUT_ENA_MID	0xffffefff
32 
33 #define DB_GP_88F68XX_GPP_OUT_VAL_LOW	0x0
34 #define DB_GP_88F68XX_GPP_OUT_VAL_MID	0x00001000
35 #define DB_GP_88F68XX_GPP_POL_LOW	0x0
36 #define DB_GP_88F68XX_GPP_POL_MID	0x0
37 
get_tpm(struct udevice ** devp)38 static int get_tpm(struct udevice **devp)
39 {
40 	int rc;
41 
42 	rc = uclass_first_device_err(UCLASS_TPM, devp);
43 	if (rc) {
44 		printf("Could not find TPM (ret=%d)\n", rc);
45 		return CMD_RET_FAILURE;
46 	}
47 
48 	return 0;
49 }
50 
51 /*
52  * Define the DDR layout / topology here in the board file. This will
53  * be used by the DDR3 init code in the SPL U-Boot version to configure
54  * the DDR3 controller.
55  */
56 static struct mv_ddr_topology_map ddr_topology_map = {
57 	DEBUG_LEVEL_ERROR,
58 	0x1, /* active interfaces */
59 	/* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
60 	{ { { {0x1, 0, 0, 0},
61 	      {0x1, 0, 0, 0},
62 	      {0x1, 0, 0, 0},
63 	      {0x1, 0, 0, 0},
64 	      {0x1, 0, 0, 0} },
65 	    SPEED_BIN_DDR_1600K,	/* speed_bin */
66 	    MV_DDR_DEV_WIDTH_16BIT,	/* memory_width */
67 	    MV_DDR_DIE_CAP_4GBIT,	/* mem_size */
68 	    MV_DDR_FREQ_533,		/* frequency */
69 	    0, 0,			/* cas_wl cas_l */
70 	    MV_DDR_TEMP_LOW,		/* temperature */
71 	    MV_DDR_TIM_DEFAULT} },	/* timing */
72 	BUS_MASK_32BIT,			/* Busses mask */
73 	MV_DDR_CFG_DEFAULT,		/* ddr configuration data source */
74 	NOT_COMBINED,			/* ddr twin-die combined */
75 	{ {0} },			/* raw spd data */
76 	{0}				/* timing parameters */
77 
78 };
79 
80 static struct serdes_map serdes_topology_map[] = {
81 	{SGMII0, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0},
82 	{USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
83 	/* SATA tx polarity is inverted */
84 	{SATA1, SERDES_SPEED_3_GBPS, SERDES_DEFAULT_MODE, 0, 1},
85 	{SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0},
86 	{DEFAULT_SERDES, SERDES_SPEED_3_GBPS, SERDES_DEFAULT_MODE, 0, 0},
87 	{PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0}
88 };
89 
hws_board_topology_load(struct serdes_map ** serdes_map_array,u8 * count)90 int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
91 {
92 	*serdes_map_array = serdes_topology_map;
93 	*count = ARRAY_SIZE(serdes_topology_map);
94 	return 0;
95 }
96 
board_pex_config(void)97 void board_pex_config(void)
98 {
99 #ifdef CONFIG_SPL_BUILD
100 	uint k;
101 	struct gpio_desc gpio = {};
102 
103 	if (!request_gpio_by_name(&gpio, "pca9698@22", 31, "fpga-program-gpio")) {
104 		/* prepare FPGA reconfiguration */
105 		dm_gpio_set_dir_flags(&gpio, GPIOD_IS_OUT);
106 		dm_gpio_set_value(&gpio, 0);
107 
108 		/* give lunatic PCIe clock some time to stabilize */
109 		mdelay(500);
110 
111 		/* start FPGA reconfiguration */
112 		dm_gpio_set_dir_flags(&gpio, GPIOD_IS_IN);
113 	}
114 
115 	/* wait for FPGA done */
116 	if (!request_gpio_by_name(&gpio, "pca9698@22", 19, "fpga-done-gpio")) {
117 		for (k = 0; k < 20; ++k) {
118 			if (dm_gpio_get_value(&gpio)) {
119 				printf("FPGA done after %u rounds\n", k);
120 				break;
121 			}
122 			mdelay(100);
123 		}
124 	}
125 
126 	/* disable FPGA reset */
127 	if (!request_gpio_by_name(&gpio, "gpio@18100", 6, "cpu-to-fpga-reset")) {
128 		dm_gpio_set_dir_flags(&gpio, GPIOD_IS_OUT);
129 		dm_gpio_set_value(&gpio, 1);
130 	}
131 
132 	/* wait for FPGA ready */
133 	if (!request_gpio_by_name(&gpio, "pca9698@22", 27, "fpga-ready-gpio")) {
134 		for (k = 0; k < 2; ++k) {
135 			if (!dm_gpio_get_value(&gpio))
136 				break;
137 			mdelay(100);
138 		}
139 	}
140 #endif
141 }
142 
mv_ddr_topology_map_get(void)143 struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
144 {
145 	return &ddr_topology_map;
146 }
147 
board_early_init_f(void)148 int board_early_init_f(void)
149 {
150 #ifdef CONFIG_SPL_BUILD
151 	/* Configure MPP */
152 	writel(0x00111111, MVEBU_MPP_BASE + 0x00);
153 	writel(0x40040000, MVEBU_MPP_BASE + 0x04);
154 	writel(0x00466444, MVEBU_MPP_BASE + 0x08);
155 	writel(0x00043300, MVEBU_MPP_BASE + 0x0c);
156 	writel(0x44400000, MVEBU_MPP_BASE + 0x10);
157 	writel(0x20000334, MVEBU_MPP_BASE + 0x14);
158 	writel(0x40000000, MVEBU_MPP_BASE + 0x18);
159 	writel(0x00004444, MVEBU_MPP_BASE + 0x1c);
160 
161 	/* Set GPP Out value */
162 	writel(DB_GP_88F68XX_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
163 	writel(DB_GP_88F68XX_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
164 
165 	/* Set GPP Polarity */
166 	writel(DB_GP_88F68XX_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
167 	writel(DB_GP_88F68XX_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
168 
169 	/* Set GPP Out Enable */
170 	writel(DB_GP_88F68XX_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
171 	writel(DB_GP_88F68XX_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
172 #endif
173 
174 	return 0;
175 }
176 
board_init(void)177 int board_init(void)
178 {
179 	/* Address of boot parameters */
180 	gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
181 
182 	return 0;
183 }
184 
185 #ifndef CONFIG_SPL_BUILD
init_host_phys(struct mii_dev * bus)186 void init_host_phys(struct mii_dev *bus)
187 {
188 	uint k;
189 
190 	for (k = 0; k < 2; ++k) {
191 		struct phy_device *phydev;
192 
193 		phydev = phy_find_by_mask(bus, 1 << k,
194 					  PHY_INTERFACE_MODE_SGMII);
195 
196 		if (phydev)
197 			phy_config(phydev);
198 	}
199 }
200 
ccdc_eth_init(void)201 int ccdc_eth_init(void)
202 {
203 	uint k;
204 	uint octo_phy_mask = 0;
205 	int ret;
206 	struct mii_dev *bus;
207 
208 	/* Init SoC's phys */
209 	bus = miiphy_get_dev_by_name("ethernet@34000");
210 
211 	if (bus)
212 		init_host_phys(bus);
213 
214 	bus = miiphy_get_dev_by_name("ethernet@70000");
215 
216 	if (bus)
217 		init_host_phys(bus);
218 
219 	/* Init octo phys */
220 	octo_phy_mask = calculate_octo_phy_mask();
221 
222 	printf("IHS PHYS: %08x", octo_phy_mask);
223 
224 	ret = init_octo_phys(octo_phy_mask);
225 
226 	if (ret)
227 		return ret;
228 
229 	printf("\n");
230 
231 	if (!get_fpga()) {
232 		puts("fpga was NULL\n");
233 		return 1;
234 	}
235 
236 	/* reset all FPGA-QSGMII instances */
237 	for (k = 0; k < 80; ++k)
238 		writel(1 << 31, get_fpga()->qsgmii_port_state[k]);
239 
240 	udelay(100);
241 
242 	for (k = 0; k < 80; ++k)
243 		writel(0, get_fpga()->qsgmii_port_state[k]);
244 	return 0;
245 }
246 
247 #endif
248 
board_late_init(void)249 int board_late_init(void)
250 {
251 #ifndef CONFIG_SPL_BUILD
252 	hydra_initialize();
253 #endif
254 	return 0;
255 }
256 
board_fix_fdt(void * rw_fdt_blob)257 int board_fix_fdt(void *rw_fdt_blob)
258 {
259 	struct udevice *bus = NULL;
260 	uint k;
261 	char name[64];
262 	int err;
263 
264 	err = uclass_get_device_by_name(UCLASS_I2C, "i2c@11000", &bus);
265 
266 	if (err) {
267 		printf("Could not get I2C bus.\n");
268 		return err;
269 	}
270 
271 	for (k = 0x21; k <= 0x26; k++) {
272 		snprintf(name, 64,
273 			 "/soc/internal-regs/i2c@11000/pca9698@%02x", k);
274 
275 		if (!dm_i2c_simple_probe(bus, k))
276 			fdt_disable_by_ofname(rw_fdt_blob, name);
277 	}
278 
279 	return 0;
280 }
281 
last_stage_init(void)282 int last_stage_init(void)
283 {
284 	struct udevice *tpm;
285 	int ret;
286 
287 #ifndef CONFIG_SPL_BUILD
288 	ccdc_eth_init();
289 #endif
290 	ret = get_tpm(&tpm);
291 	if (ret || tpm_init(tpm) || tpm_startup(tpm, TPM_ST_CLEAR) ||
292 	    tpm_continue_self_test(tpm)) {
293 		return 1;
294 	}
295 
296 	mdelay(37);
297 
298 	flush_keys(tpm);
299 	load_and_run_keyprog(tpm);
300 
301 	return 0;
302 }
303