summaryrefslogtreecommitdiff
path: root/arch/arm/mach-socfpga/misc_s10.c
blob: d8cdc35a6187f02f2b0887144ad5306c6ae9222c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Copyright (C) 2016-2018 Intel Corporation <www.intel.com>
 *
 * SPDX-License-Identifier:    GPL-2.0
 */

#include <common.h>
#include <asm/io.h>
#include <errno.h>
#include <fdtdec.h>
#include <libfdt.h>
#include <altera.h>
#include <miiphy.h>
#include <netdev.h>
#include <watchdog.h>
#include <asm/arch/reset_manager.h>
#include <asm/arch/sdram_s10.h>
#include <asm/arch/system_manager.h>
#include <asm/pl310.h>

#include <dt-bindings/reset/altr,rst-mgr-s10.h>

DECLARE_GLOBAL_DATA_PTR;

static struct socfpga_system_manager *sysmgr_regs =
	(struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
#if defined(CONFIG_SOCFPGA_SDRAM_SBE_ECC_CHECKING) || \
defined(CONFIG_SOCFPGA_SDRAM_DBE_ECC_CHECKING) || \
defined(CONFIG_SOCFPGA_SDRAM_DBE_ECC_CHECKING)
static const struct socfpga_ecc_hmc *socfpga_ecc_hmc_base =
		(void *)SOCFPGA_SDR_ADDRESS;
#endif

#ifdef CONFIG_SOCFPGA_SDRAM_SBE_ECC_CHECKING
void sdram_sbe_ecc_checking(void)
{
	u32 ecc_data, location, read_data_lo;
	u32 read_data_hi = 0;
	u64 data, sbe_data;
	u32 hmc_size = readl(&socfpga_ecc_hmc_base->ddrioctrl);

	/* DDIR IO x64 */
	if (hmc_size == 2) {
		ecc_data = 0x5C86E6E1;
		data = 0x1122334455667788ULL;
		location = 0x810000;
		sbe_data = 0x1122334455667789ULL;
	} else { /* DDR IO x32 */
		ecc_data = 0x6db7d7d3;
		data = 0x11223344;
		location = 0x800000;
		sbe_data = 0x11223345;
	}

	printf("***********************************************************\n");
	printf("SDRAM single bit error test starting . . .\n");
	printf("Enable SBE interrupt\n");
	setbits_le32(&socfpga_ecc_hmc_base->errinten,
		     DDR_HMC_ERRINTEN_SERRINTEN_EN_SET_MSK);
	setbits_le32(&socfpga_ecc_hmc_base->intmode,
		     (DDR_HMC_INTMODE_INTMODE_SET_MSK));
	printf("Writing data 0x%x%x into SDRAM address 0x%lx\n",
	       (u32)(data >> 32), (u32)data, (uintptr_t)location);

	/* DDIR IO x64 */
	if (hmc_size == 2) {
		writeq(data, (uintptr_t)location);
	} else { /* DDR IO x32 */
		writel(data, (uintptr_t)location);
	}

	printf("Reading data at SDRAM address 0x%lx\n", (uintptr_t)location);
	printf("0x%lx: 0x%x\n", (uintptr_t)location,
	       readl((uintptr_t)location));
	if (hmc_size == 2)
		printf("0x%lx: 0x%x\n", (uintptr_t)(location+4),
		       readl((uintptr_t)(location+4)));
	printf("Writing ecc data 0x%x into ecc_reg2wreccdatabus register\n",
	       ecc_data);
	writel(ecc_data, &socfpga_ecc_hmc_base->ecc_reg2wreccdatabus);
	printf("Enable eccdiagon AND wrdiagon at ecc_diagon\n");
	setbits_le32(&socfpga_ecc_hmc_base->ecc_diagon,
		     (DDR_HMC_ECC_DIAGON_ECCDIAGON_EN_SET_MSK |
		      DDR_HMC_ECC_DIAGON_WRDIAGON_EN_SET_MSK));
	printf("Injecting SBE data 0x%x%x into SDRAM address 0x%lx\n",
	       (u32)(sbe_data >> 32), (u32)sbe_data, (uintptr_t)location);

	/* DDIR IO x64 */
	if (hmc_size == 2) {
		writeq(sbe_data, (uintptr_t)location);
	} else { /* DDR IO x32 */
		writel(sbe_data, (uintptr_t)location);
	}

	read_data_lo = readl((uintptr_t)location);
	printf("Reading data at SDRAM address\n0x%lx: 0x%x\n",
	       (uintptr_t)location, read_data_lo);
	if (hmc_size == 2) {
		read_data_hi = readl((uintptr_t)(location+4));
		printf("0x%lx: 0x%x\n", (uintptr_t)(location+4),
		       read_data_hi);
	}

	if (readl(&socfpga_ecc_hmc_base->ecc_decstat) & 0x1) {
		printf("ECC decoder for data [63:0] has detected SBE\n");

		printf("SBE check Passed: Corrected SDRAM ECC @ 0x%08x\n",
		       readl(&socfpga_ecc_hmc_base->autowb_corraddr));

		if (readl(&socfpga_ecc_hmc_base->intstat) & 0x1) {
			printf("SBE interrupt is pending . . .\n");
			printf("Clearing the pending SBE\n");
			setbits_le32(&socfpga_ecc_hmc_base->intstat,
				     DDR_HMC_INTSTAT_SERRPENA_SET_MSK);
		}
	} else {
		if (read_data_lo != (u32)data) {
			printf("SBE Error: Did not read correct data 0x%x%x: ",
			       read_data_hi, read_data_lo);
			printf("expected 0x%x%x\n", (u32)(data >> 32),
			       (u32)data);
		} else {
			printf("SBE Error: Suspect ECC decoder failed to ");
			printf("detect data [63:0]\n");
		}
	}

	printf("Disable eccdiagon AND wrdiagon at ecc_diagon\n");
	printf("***********************************************************\n");
	clrbits_le32(&socfpga_ecc_hmc_base->ecc_diagon,
		     (DDR_HMC_ECC_DIAGON_ECCDIAGON_EN_SET_MSK |
		      DDR_HMC_ECC_DIAGON_WRDIAGON_EN_SET_MSK));
}
#endif

#ifdef CONFIG_SOCFPGA_SDRAM_DBE_ECC_CHECKING
void sdram_dbe_ecc_checking(void)
{
	u32 ecc_data, location;
	u64 data, dbe_data;
	u32 hmc_size = readl(&socfpga_ecc_hmc_base->ddrioctrl);

	/* DDIR IO x64 */
	if (hmc_size == 2) {
		ecc_data = 0x5C86E6E1;
		data = 0x1122334455667788ULL;
		location = 0x810000;
		dbe_data = 0x112233445566778BULL;
	} else { /* DDR IO x32 */
		ecc_data = 0x6db7d7d3;
		data = 0x11223344;
		location = 0x800000;
		dbe_data = 0x11003344;
	}

	printf("***********************************************************\n");
	printf("SDRAM double bit error test starting . . .\n");
	printf("Writing data 0x%x%x into SDRAM address 0x%lx\n",
	       (u32)(data >> 32), (u32)data, (uintptr_t)location);

	/* DDIR IO x64 */
	if (hmc_size == 2) {
		writeq(data, (uintptr_t)location);
	} else { /* DDR IO x32 */
		writel(data, (uintptr_t)location);
	}

	printf("Reading data at SDRAM address 0x%lx\n", (uintptr_t)location);
	printf("0x%lx: 0x%x\n", (uintptr_t)location,
	       readl((uintptr_t)location));
	if (hmc_size == 2)
		printf("0x%lx: 0x%x\n", (uintptr_t)(location+4),
		       readl((uintptr_t)(location+4)));

	printf("Writing ecc data 0x%x into ecc_reg2wreccdatabus register\n",
	       ecc_data);
	writel(ecc_data, &socfpga_ecc_hmc_base->ecc_reg2wreccdatabus);
	printf("Enable eccdiagon AND wrdiagon at ecc_diagon\n");
	setbits_le32(&socfpga_ecc_hmc_base->ecc_diagon,
		     (DDR_HMC_ECC_DIAGON_ECCDIAGON_EN_SET_MSK |
		      DDR_HMC_ECC_DIAGON_WRDIAGON_EN_SET_MSK));
	printf("Injecting DBE data 0x%x%x into SDRAM address 0x%lx\n",
	       (u32)(dbe_data >> 32), (u32)dbe_data, (uintptr_t)location);
	/* DDIR IO x64 */
	if (hmc_size == 2) {
		writeq(dbe_data, (uintptr_t)location);
	} else { /* DDR IO x32 */
		writel(dbe_data, (uintptr_t)location);
	}
	printf("***********************************************************\n");
}
#endif

#ifdef CONFIG_SOCFPGA_SDRAM_ADDR_MISMATCH_ECC_CHECKING
void sdram_addr_mismatch_ecc_checking(void)
{
	u32 ecc_data = 0x88523236;
	u32 location = 0x800000;
	u64 data;
	u32 hmc_size = readl(&socfpga_ecc_hmc_base->ddrioctrl);

	/* DDIR IO x64 */
	if (hmc_size == 2) {
		data = 0x1122334455667788ULL;
	} else { /* DDR IO x32 */
		data = 0x11223344;
	}

	printf("***********************************************************\n");
	printf("ECC address mismatch test starting . . .\n");
	printf("Writing data 0x%x%x into SDRAM address 0x%lx\n",
	       (u32)(data >> 32), (u32)data, (uintptr_t)location);

	/* DDIR IO x64 */
	if (hmc_size == 2) {
		writeq(data, (uintptr_t)location);
	} else { /* DDR IO x32 */
		writel(data, (uintptr_t)location);
	}

	printf("Reading data at SDRAM address 0x%lx\n", (uintptr_t)location);
	printf("0x%lx: 0x%x\n", (uintptr_t)location,
	       readl((uintptr_t)location));
	if (hmc_size == 2)
		printf("0x%lx: 0x%x\n", (uintptr_t)(location+4),
		       readl((uintptr_t)(location+4)));
	printf("Writing ecc data 0x%x into ecc_reg2wreccdatabus register\n",
	       ecc_data);
	writel(ecc_data, &socfpga_ecc_hmc_base->ecc_reg2wreccdatabus);
	printf("Enable eccdiagon AND wrdiagon at ecc_diagon\n");
	setbits_le32(&socfpga_ecc_hmc_base->ecc_diagon,
		     (DDR_HMC_ECC_DIAGON_ECCDIAGON_EN_SET_MSK |
		      DDR_HMC_ECC_DIAGON_WRDIAGON_EN_SET_MSK));
	printf("First write 0x%x%x into SDRAM address 0x%lx after ",
	       (u32)(data >> 32), (u32)data, (uintptr_t)location);
	printf("ecc_diagon is on\n");
	/* DDIR IO x64 */
	if (hmc_size == 2) {
		writeq(data, (uintptr_t)location);
	} else { /* DDR IO x32 */
		writel(data, (uintptr_t)location);
	}
	printf("Reading data at SDRAM address 0x%lx\n", (uintptr_t)location);
	printf("0x%lx: 0x%x\n", (uintptr_t)location,
	       readl((uintptr_t)location));
	if (hmc_size == 2)
		printf("0x%lx: 0x%x\n", (uintptr_t)(location+4),
		       readl((uintptr_t)(location+4)));
	printf("***********************************************************\n");
}
#endif

/*
 * DesignWare Ethernet initialization
 */
#ifdef CONFIG_ETH_DESIGNWARE
void dwmac_deassert_reset(const unsigned int of_reset_id,
				 const u32 phymode)
{
	/* Put the emac we're using into reset.
	 * This is required before configuring the PHY interface
	 */
	socfpga_emac_manage_reset(of_reset_id, 1);

	clrsetbits_le32(&sysmgr_regs->emac0 + (of_reset_id - EMAC0_RESET),
			SYSMGR_EMACGRP_CTRL_PHYSEL_MASK,
			phymode);

	socfpga_emac_manage_reset(of_reset_id, 0);
}

static u32 dwmac_phymode_to_modereg(const char *phymode, u32 *modereg)
{
	if (!phymode)
		return -EINVAL;

	if (!strcmp(phymode, "mii") || !strcmp(phymode, "gmii")) {
		*modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
		return 0;
	}

	if (!strcmp(phymode, "rgmii")) {
		*modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
		return 0;
	}

	if (!strcmp(phymode, "rmii")) {
		*modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII;
		return 0;
	}

	return -EINVAL;
}

static int socfpga_eth_reset(void)
{
	const void *fdt = gd->fdt_blob;
	struct fdtdec_phandle_args args;
	const char *phy_mode;
	u32 phy_modereg;
	int nodes[2];	/* Max. 3 GMACs */
	int ret, count;
	int i, node;

	count = fdtdec_find_aliases_for_id(fdt, "ethernet",
					   COMPAT_ALTERA_SOCFPGA_DWMAC,
					   nodes, ARRAY_SIZE(nodes));
	for (i = 0; i < count; i++) {
		node = nodes[i];
		if (node <= 0)
			continue;

		ret = fdtdec_parse_phandle_with_args(fdt, node, "resets",
						     "#reset-cells", 1, 0,
						     &args);
		if (ret || (args.args_count != 1)) {
			debug("GMAC%i: Failed to parse DT 'resets'!\n", i);
			continue;
		}

		phy_mode = fdt_getprop(fdt, node, "phy-mode", NULL);
		ret = dwmac_phymode_to_modereg(phy_mode, &phy_modereg);
		if (ret) {
			debug("GMAC%i: Failed to parse DT 'phy-mode'!\n", i);
			continue;
		}

		dwmac_deassert_reset(args.args[0], phy_modereg);
	}

	return 0;
}
#else
static int socfpga_eth_reset(void)
{
	return 0;
};
#endif

/*
 * Print CPU information
 */
#if defined(CONFIG_DISPLAY_CPUINFO)
int print_cpuinfo(void)
{
	puts("CPU:   Intel FPGA SoCFPGA Platform\n");
	puts("FPGA:  Intel FPGA Stratix 10\n");
	return 0;
}
#endif

#ifdef CONFIG_ARCH_MISC_INIT
int arch_misc_init(void)
{
	char qspi_string[13];

	sprintf(qspi_string, "<0x%08x>", cm_get_qspi_controller_clk_hz());
	env_set("qspi_clock", qspi_string);

	return socfpga_eth_reset();
}
#endif

#ifdef CONFIG_FPGA
/*
 * FPGA programming support for SoC FPGA Stratix 10
 */
static Altera_desc altera_fpga[] = {
	{
		/* Family */
		Intel_FPGA_Stratix10,
		/* Interface type */
		secure_device_manager_mailbox,
		/* No limitation as additional data will be ignored */
		-1,
		/* No device function table */
		NULL,
		/* Base interface address specified in driver */
		NULL,
		/* No cookie implementation */
		0
	},
};

/* add device descriptor to FPGA device table */
static void socfpga_fpga_add(void)
{
	int i;
	fpga_init();
	for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
		fpga_add(fpga_altera, &altera_fpga[i]);
}
#endif

int arch_early_init_r(void)
{
#ifdef CONFIG_FPGA
	socfpga_fpga_add();
#endif

	return 0;
}

int do_bridge(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	if (argc != 2)
		return CMD_RET_USAGE;

	argv++;

	switch (*argv[0]) {
	case 'e':	/* Enable */
		socfpga_bridges_reset(1);
		break;
	case 'd':	/* Disable */
		socfpga_bridges_reset(0);
		break;
	default:
		return CMD_RET_USAGE;
	}

	return 0;
}

U_BOOT_CMD(
	bridge, 2, 1, do_bridge,
	"SoCFPGA HPS FPGA bridge control",
	"enable  - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
	"bridge disable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
	""
);