blob: cef1f6a13aee00adc48b0d0781ad5273fa14c8ba [file] [log] [blame]
Michal Simek04b7e622015-01-15 10:01:51 +01001/*
2 * (C) Copyright 2014 - 2015 Xilinx, Inc.
3 * Michal Simek <michal.simek@xilinx.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
Michal Simekd54b1af2015-09-30 17:26:55 +02009#include <sata.h>
Michal Simekb216cc12015-07-23 13:27:40 +020010#include <ahci.h>
11#include <scsi.h>
Michal Simekecfb6dc2016-04-22 14:28:54 +020012#include <malloc.h>
Michal Simekc23d3f82015-11-05 08:34:35 +010013#include <asm/arch/clk.h>
Michal Simek04b7e622015-01-15 10:01:51 +010014#include <asm/arch/hardware.h>
15#include <asm/arch/sys_proto.h>
16#include <asm/io.h>
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +053017#include <usb.h>
18#include <dwc3-uboot.h>
Michal Simek8111aff2016-02-01 15:05:58 +010019#include <zynqmppl.h>
Michal Simekeec32f62016-04-22 11:48:49 +020020#include <i2c.h>
Michal Simek76d0a772016-09-01 11:16:40 +020021#include <g_dnl.h>
Michal Simek04b7e622015-01-15 10:01:51 +010022
23DECLARE_GLOBAL_DATA_PTR;
24
Michal Simek8111aff2016-02-01 15:05:58 +010025#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
26 !defined(CONFIG_SPL_BUILD)
27static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
28
29static const struct {
30 uint32_t id;
31 char *name;
32} zynqmp_devices[] = {
33 {
34 .id = 0x10,
35 .name = "3eg",
36 },
37 {
38 .id = 0x11,
39 .name = "2eg",
40 },
41 {
42 .id = 0x20,
43 .name = "5ev",
44 },
45 {
46 .id = 0x21,
47 .name = "4ev",
48 },
49 {
50 .id = 0x30,
51 .name = "7ev",
52 },
53 {
54 .id = 0x38,
55 .name = "9eg",
56 },
57 {
58 .id = 0x39,
59 .name = "6eg",
60 },
61 {
62 .id = 0x40,
63 .name = "11eg",
64 },
65 {
66 .id = 0x50,
67 .name = "15eg",
68 },
69 {
70 .id = 0x58,
71 .name = "19eg",
72 },
73 {
74 .id = 0x59,
75 .name = "17eg",
76 },
77};
78
79static int chip_id(void)
80{
81 struct pt_regs regs;
82 regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
83 regs.regs[1] = 0;
84 regs.regs[2] = 0;
85 regs.regs[3] = 0;
86
87 smc_call(&regs);
88
Soren Brinkmannd7696a52016-09-29 11:44:41 -070089 /*
90 * SMC returns:
91 * regs[0][31:0] = status of the operation
92 * regs[0][63:32] = CSU.IDCODE register
93 * regs[1][31:0] = CSU.version register
94 */
95 regs.regs[0] = upper_32_bits(regs.regs[0]);
96 regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
97 ZYNQMP_CSU_IDCODE_SVD_MASK;
98 regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
99
Michal Simek8111aff2016-02-01 15:05:58 +0100100 return regs.regs[0];
101}
102
103static char *zynqmp_get_silicon_idcode_name(void)
104{
105 uint32_t i, id;
106
107 id = chip_id();
108 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
109 if (zynqmp_devices[i].id == id)
110 return zynqmp_devices[i].name;
111 }
112 return "unknown";
113}
114#endif
115
116#define ZYNQMP_VERSION_SIZE 9
117
Michal Simek04b7e622015-01-15 10:01:51 +0100118int board_init(void)
119{
Michal Simekfb7242d2015-06-22 14:31:06 +0200120 printf("EL Level:\tEL%d\n", current_el());
121
Michal Simek8111aff2016-02-01 15:05:58 +0100122#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
123 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
124 defined(CONFIG_SPL_BUILD))
125 if (current_el() != 3) {
126 static char version[ZYNQMP_VERSION_SIZE];
127
128 strncat(version, "xczu", ZYNQMP_VERSION_SIZE);
129 zynqmppl.name = strncat(version,
130 zynqmp_get_silicon_idcode_name(),
131 ZYNQMP_VERSION_SIZE);
132 printf("Chip ID:\t%s\n", zynqmppl.name);
133 fpga_init();
134 fpga_add(fpga_xilinx, &zynqmppl);
135 }
136#endif
137
Michal Simek04b7e622015-01-15 10:01:51 +0100138 return 0;
139}
140
141int board_early_init_r(void)
142{
143 u32 val;
144
Michal Simekc23d3f82015-11-05 08:34:35 +0100145 if (current_el() == 3) {
146 val = readl(&crlapb_base->timestamp_ref_ctrl);
147 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
148 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek04b7e622015-01-15 10:01:51 +0100149
Michal Simekc23d3f82015-11-05 08:34:35 +0100150 /* Program freq register in System counter */
151 writel(zynqmp_get_system_timer_freq(),
152 &iou_scntr_secure->base_frequency_id_register);
153 /* And enable system counter */
154 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
155 &iou_scntr_secure->counter_control_register);
156 }
Michal Simek04b7e622015-01-15 10:01:51 +0100157 /* Program freq register in System counter and enable system counter */
158 writel(gd->cpu_clk, &iou_scntr->base_frequency_id_register);
159 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_HDBG |
160 ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
161 &iou_scntr->counter_control_register);
162
163 return 0;
164}
165
Michal Simekeec32f62016-04-22 11:48:49 +0200166int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
167{
168#if defined(CONFIG_ZYNQ_GEM_EEPROM_ADDR) && \
169 defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET) && \
170 defined(CONFIG_ZYNQ_EEPROM_BUS)
171 i2c_set_bus_num(CONFIG_ZYNQ_EEPROM_BUS);
172
173 if (eeprom_read(CONFIG_ZYNQ_GEM_EEPROM_ADDR,
174 CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET,
175 ethaddr, 6))
176 printf("I2C EEPROM MAC address read failed\n");
177#endif
178
179 return 0;
180}
181
Michal Simek8faa66a2016-02-08 09:34:53 +0100182#if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
Tom Riniedcfdbd2016-12-09 07:56:54 -0500183/*
184 * fdt_get_reg - Fill buffer by information from DT
185 */
186static phys_size_t fdt_get_reg(const void *fdt, int nodeoffset, void *buf,
187 const u32 *cell, int n)
Michal Simek8faa66a2016-02-08 09:34:53 +0100188{
Tom Riniedcfdbd2016-12-09 07:56:54 -0500189 int i = 0, b, banks;
190 int parent_offset = fdt_parent_offset(fdt, nodeoffset);
191 int address_cells = fdt_address_cells(fdt, parent_offset);
192 int size_cells = fdt_size_cells(fdt, parent_offset);
193 char *p = buf;
194 u64 val;
195 u64 vals;
Michal Simek8faa66a2016-02-08 09:34:53 +0100196
Tom Riniedcfdbd2016-12-09 07:56:54 -0500197 debug("%s: addr_cells=%x, size_cell=%x, buf=%p, cell=%p\n",
198 __func__, address_cells, size_cells, buf, cell);
Michal Simek8faa66a2016-02-08 09:34:53 +0100199
Tom Riniedcfdbd2016-12-09 07:56:54 -0500200 /* Check memory bank setup */
201 banks = n % (address_cells + size_cells);
202 if (banks)
203 panic("Incorrect memory setup cells=%d, ac=%d, sc=%d\n",
204 n, address_cells, size_cells);
Michal Simek8faa66a2016-02-08 09:34:53 +0100205
Tom Riniedcfdbd2016-12-09 07:56:54 -0500206 banks = n / (address_cells + size_cells);
Michal Simek8faa66a2016-02-08 09:34:53 +0100207
Tom Riniedcfdbd2016-12-09 07:56:54 -0500208 for (b = 0; b < banks; b++) {
209 debug("%s: Bank #%d:\n", __func__, b);
210 if (address_cells == 2) {
211 val = cell[i + 1];
212 val <<= 32;
213 val |= cell[i];
214 val = fdt64_to_cpu(val);
215 debug("%s: addr64=%llx, ptr=%p, cell=%p\n",
216 __func__, val, p, &cell[i]);
217 *(phys_addr_t *)p = val;
218 } else {
219 debug("%s: addr32=%x, ptr=%p\n",
220 __func__, fdt32_to_cpu(cell[i]), p);
221 *(phys_addr_t *)p = fdt32_to_cpu(cell[i]);
222 }
223 p += sizeof(phys_addr_t);
224 i += address_cells;
Michal Simek8faa66a2016-02-08 09:34:53 +0100225
Tom Riniedcfdbd2016-12-09 07:56:54 -0500226 debug("%s: pa=%p, i=%x, size=%zu\n", __func__, p, i,
227 sizeof(phys_addr_t));
Michal Simek8faa66a2016-02-08 09:34:53 +0100228
Tom Riniedcfdbd2016-12-09 07:56:54 -0500229 if (size_cells == 2) {
230 vals = cell[i + 1];
231 vals <<= 32;
232 vals |= cell[i];
233 vals = fdt64_to_cpu(vals);
Michal Simek8faa66a2016-02-08 09:34:53 +0100234
Tom Riniedcfdbd2016-12-09 07:56:54 -0500235 debug("%s: size64=%llx, ptr=%p, cell=%p\n",
236 __func__, vals, p, &cell[i]);
237 *(phys_size_t *)p = vals;
238 } else {
239 debug("%s: size32=%x, ptr=%p\n",
240 __func__, fdt32_to_cpu(cell[i]), p);
241 *(phys_size_t *)p = fdt32_to_cpu(cell[i]);
242 }
243 p += sizeof(phys_size_t);
244 i += size_cells;
Michal Simek8faa66a2016-02-08 09:34:53 +0100245
Tom Riniedcfdbd2016-12-09 07:56:54 -0500246 debug("%s: ps=%p, i=%x, size=%zu\n",
247 __func__, p, i, sizeof(phys_size_t));
248 }
Michal Simek8faa66a2016-02-08 09:34:53 +0100249
Tom Riniedcfdbd2016-12-09 07:56:54 -0500250 /* Return the first address size */
251 return *(phys_size_t *)((char *)buf + sizeof(phys_addr_t));
Michal Simek8faa66a2016-02-08 09:34:53 +0100252}
253
Tom Riniedcfdbd2016-12-09 07:56:54 -0500254#define FDT_REG_SIZE sizeof(u32)
255/* Temp location for sharing data for storing */
256/* Up to 64-bit address + 64-bit size */
257static u8 tmp[CONFIG_NR_DRAM_BANKS * 16];
258
Michal Simek8faa66a2016-02-08 09:34:53 +0100259void dram_init_banksize(void)
260{
Tom Riniedcfdbd2016-12-09 07:56:54 -0500261 int bank;
Michal Simek8faa66a2016-02-08 09:34:53 +0100262
Tom Riniedcfdbd2016-12-09 07:56:54 -0500263 memcpy(&gd->bd->bi_dram[0], &tmp, sizeof(tmp));
Michal Simek8faa66a2016-02-08 09:34:53 +0100264
Tom Riniedcfdbd2016-12-09 07:56:54 -0500265 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
266 debug("Bank #%d: start %llx\n", bank,
267 (unsigned long long)gd->bd->bi_dram[bank].start);
268 debug("Bank #%d: size %llx\n", bank,
269 (unsigned long long)gd->bd->bi_dram[bank].size);
Michal Simek8faa66a2016-02-08 09:34:53 +0100270 }
Tom Riniedcfdbd2016-12-09 07:56:54 -0500271}
Michal Simek8faa66a2016-02-08 09:34:53 +0100272
Tom Riniedcfdbd2016-12-09 07:56:54 -0500273int dram_init(void)
274{
275 int node, len;
276 const void *blob = gd->fdt_blob;
277 const u32 *cell;
Michal Simek8faa66a2016-02-08 09:34:53 +0100278
Tom Riniedcfdbd2016-12-09 07:56:54 -0500279 memset(&tmp, 0, sizeof(tmp));
Michal Simek8faa66a2016-02-08 09:34:53 +0100280
Tom Riniedcfdbd2016-12-09 07:56:54 -0500281 /* find or create "/memory" node. */
282 node = fdt_subnode_offset(blob, 0, "memory");
283 if (node < 0) {
284 printf("%s: Can't get memory node\n", __func__);
285 return node;
286 }
Michal Simek8faa66a2016-02-08 09:34:53 +0100287
Tom Riniedcfdbd2016-12-09 07:56:54 -0500288 /* Get pointer to cells and lenght of it */
289 cell = fdt_getprop(blob, node, "reg", &len);
290 if (!cell) {
291 printf("%s: Can't get reg property\n", __func__);
292 return -1;
Michal Simek8faa66a2016-02-08 09:34:53 +0100293 }
Tom Riniedcfdbd2016-12-09 07:56:54 -0500294
295 gd->ram_size = fdt_get_reg(blob, node, &tmp, cell, len / FDT_REG_SIZE);
296
297 debug("%s: Initial DRAM size %llx\n", __func__, (u64)gd->ram_size);
298
299 return 0;
Michal Simek8faa66a2016-02-08 09:34:53 +0100300}
301#else
Michal Simek04b7e622015-01-15 10:01:51 +0100302int dram_init(void)
303{
304 gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
305
306 return 0;
307}
Michal Simek8faa66a2016-02-08 09:34:53 +0100308#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100309
Michal Simek04b7e622015-01-15 10:01:51 +0100310void reset_cpu(ulong addr)
311{
312}
313
Tom Riniedcfdbd2016-12-09 07:56:54 -0500314#ifdef CONFIG_SCSI_AHCI_PLAT
315void scsi_init(void)
316{
317#if defined(CONFIG_SATA_CEVA)
318 init_sata(0);
319#endif
320 ahci_init((void __iomem *)ZYNQMP_SATA_BASEADDR);
321 scsi_scan(1);
322}
323#endif
324
Michal Simek04b7e622015-01-15 10:01:51 +0100325int board_late_init(void)
326{
327 u32 reg = 0;
328 u8 bootmode;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200329 const char *mode;
330 char *new_targets;
331
332 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
333 debug("Saved variables - Skipping\n");
334 return 0;
335 }
Michal Simek04b7e622015-01-15 10:01:51 +0100336
337 reg = readl(&crlapb_base->boot_mode);
Michal Simek833e0c42016-10-25 11:43:02 +0200338 if (reg >> BOOT_MODE_ALT_SHIFT)
339 reg >>= BOOT_MODE_ALT_SHIFT;
340
Michal Simek04b7e622015-01-15 10:01:51 +0100341 bootmode = reg & BOOT_MODES_MASK;
342
Michal Simekc5d95232015-09-20 17:20:42 +0200343 puts("Bootmode: ");
Michal Simek04b7e622015-01-15 10:01:51 +0100344 switch (bootmode) {
Michal Simek12398ea2016-08-19 14:14:52 +0200345 case USB_MODE:
346 puts("USB_MODE\n");
347 mode = "usb";
348 break;
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530349 case JTAG_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200350 puts("JTAG_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200351 mode = "pxe dhcp";
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530352 break;
353 case QSPI_MODE_24BIT:
354 case QSPI_MODE_32BIT:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200355 mode = "qspi0";
Michal Simekc5d95232015-09-20 17:20:42 +0200356 puts("QSPI_MODE\n");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530357 break;
Michal Simek02d66cd2015-04-15 15:02:28 +0200358 case EMMC_MODE:
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200359 puts("EMMC_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200360 mode = "mmc0";
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200361 break;
362 case SD_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200363 puts("SD_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200364 mode = "mmc0";
Michal Simek04b7e622015-01-15 10:01:51 +0100365 break;
Siva Durga Prasad Paladugu29a77d22016-09-21 11:45:05 +0530366 case SD1_LSHFT_MODE:
367 puts("LVL_SHFT_");
368 /* fall through */
Michal Simek108e1842015-10-05 10:51:12 +0200369 case SD_MODE1:
Michal Simekc5d95232015-09-20 17:20:42 +0200370 puts("SD_MODE1\n");
Michal Simek6d902452015-11-06 10:22:37 +0100371#if defined(CONFIG_ZYNQ_SDHCI0) && defined(CONFIG_ZYNQ_SDHCI1)
Michal Simekecfb6dc2016-04-22 14:28:54 +0200372 mode = "mmc1";
373#else
374 mode = "mmc0";
Michal Simek6d902452015-11-06 10:22:37 +0100375#endif
Michal Simek108e1842015-10-05 10:51:12 +0200376 break;
377 case NAND_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200378 puts("NAND_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200379 mode = "nand0";
Michal Simek108e1842015-10-05 10:51:12 +0200380 break;
Michal Simek04b7e622015-01-15 10:01:51 +0100381 default:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200382 mode = "";
Michal Simek04b7e622015-01-15 10:01:51 +0100383 printf("Invalid Boot Mode:0x%x\n", bootmode);
384 break;
385 }
386
Michal Simekecfb6dc2016-04-22 14:28:54 +0200387 /*
388 * One terminating char + one byte for space between mode
389 * and default boot_targets
390 */
391 new_targets = calloc(1, strlen(mode) +
392 strlen(getenv("boot_targets")) + 2);
393
394 sprintf(new_targets, "%s %s", mode, getenv("boot_targets"));
395 setenv("boot_targets", new_targets);
396
Michal Simek04b7e622015-01-15 10:01:51 +0100397 return 0;
398}
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530399
400int checkboard(void)
401{
Michal Simek47ce9362016-01-25 11:04:21 +0100402 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530403 return 0;
404}
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +0530405
406#ifdef CONFIG_USB_DWC3
Michal Simekea526be2016-08-08 10:11:26 +0200407static struct dwc3_device dwc3_device_data0 = {
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +0530408 .maximum_speed = USB_SPEED_HIGH,
409 .base = ZYNQMP_USB0_XHCI_BASEADDR,
410 .dr_mode = USB_DR_MODE_PERIPHERAL,
411 .index = 0,
412};
413
Michal Simekea526be2016-08-08 10:11:26 +0200414static struct dwc3_device dwc3_device_data1 = {
415 .maximum_speed = USB_SPEED_HIGH,
416 .base = ZYNQMP_USB1_XHCI_BASEADDR,
417 .dr_mode = USB_DR_MODE_PERIPHERAL,
418 .index = 1,
419};
420
Michal Simek76d0a772016-09-01 11:16:40 +0200421int usb_gadget_handle_interrupts(int index)
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +0530422{
Michal Simek76d0a772016-09-01 11:16:40 +0200423 dwc3_uboot_handle_interrupt(index);
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +0530424 return 0;
425}
426
427int board_usb_init(int index, enum usb_init_type init)
428{
Michal Simekea526be2016-08-08 10:11:26 +0200429 debug("%s: index %x\n", __func__, index);
430
Michal Simek7987d2a2016-09-01 11:27:32 +0200431#if defined(CONFIG_USB_GADGET_DOWNLOAD)
432 g_dnl_set_serialnumber(CONFIG_SYS_CONFIG_NAME);
433#endif
434
Michal Simekea526be2016-08-08 10:11:26 +0200435 switch (index) {
436 case 0:
437 return dwc3_uboot_init(&dwc3_device_data0);
438 case 1:
439 return dwc3_uboot_init(&dwc3_device_data1);
440 };
441
442 return -1;
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +0530443}
444
445int board_usb_cleanup(int index, enum usb_init_type init)
446{
447 dwc3_uboot_exit(index);
448 return 0;
449}
450#endif