xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/spl_pcie_ep_boot.c (revision a4719b90cc2f09e5348b830d61f32ab6d991069a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2023 Rockchip Electronics Co., Ltd
4  */
5 
6 #include <common.h>
7 #include <spl.h>
8 #include <asm/io.h>
9 #include <asm/arch/cpu.h>
10 #include <asm/arch/hardware.h>
11 #include <asm/arch/ioc_rk3588.h>
12 #include <dt-bindings/clock/rk3588-cru.h>
13 #include <pci.h>
14 #include <asm/arch/rk_atags.h>
15 
16 #ifndef CONFIG_SPL_LOAD_FIT_ADDRESS
17 #error "SPL_LOAD_FIT_ADDRESS not defined!"
18 #endif
19 
20 #define printep(fmt, ...) \
21 		do { \
22 			printf("RKEP: %d - ", readl(CONFIG_ROCKCHIP_STIMER_BASE + 0x2c) / 24); \
23 			printf(fmt, ##__VA_ARGS__); \
24 		} while (0)
25 
26 #ifdef CONFIG_ROCKCHIP_RK3588
27 #define PCIE_SNPS_DBI_BASE	0xf5000000
28 #define PCIE_SNPS_APB_BASE	0xfe150000
29 #define PCIE_SNPS_IATU_BASE	0xa40300000
30 
31 #define PCI_RESBAR		0x2e8
32 #elif CONFIG_ROCKCHIP_RK3568
33 #define PCIE_SNPS_DBI_BASE	0xf6000000
34 #define PCIE_SNPS_APB_BASE	0xfe280000
35 #define PCIE_SNPS_IATU_BASE	0x3c0b00000
36 
37 #define PCI_RESBAR		0x2b8
38 #else
39 #error "this soc is not support pcie ep!"
40 #endif
41 
42 #define RKEP_BAR0_ADDR		0x3c000000
43 #define RKEP_BAR2_ADDR		CONFIG_SPL_LOAD_FIT_ADDRESS
44 #define RKEP_BAR0_CMD_ADDR	(RKEP_BAR0_ADDR + 0x400)
45 #define RKEP_BOOT_MAGIC		0x524b4550 /* RKEP */
46 #define RKEP_CMD_LOADER_RUN	0x524b4501
47 
48 #define PCI_EXP_LNKCAP		12	/* Link Capabilities */
49 #define PCI_EXP_LNKCTL2		48	/* Link Control 2 */
50 #define PCI_EXP_LNKCTL2_TLS		0x000f
51 #define PCI_EXP_LNKCAP_SLS		0x0000000f
52 
53 #define PCI_EXP_LNKCTL2_TLS_2_5GT	0x0001 /* Supported Speed 2.5GT/s */
54 #define PCI_EXP_LNKCTL2_TLS_5_0GT	0x0002 /* Supported Speed 5GT/s */
55 #define PCI_EXP_LNKCTL2_TLS_8_0GT	0x0003 /* Supported Speed 8GT/s */
56 
57 /* Synopsys-specific PCIe configuration registers */
58 #define PCIE_PORT_LINK_CONTROL		0x710
59 #define PORT_LINK_MODE_MASK		(0x3f << 16)
60 #define PORT_LINK_MODE_1_LANES		(0x1 << 16)
61 #define PORT_LINK_MODE_2_LANES		(0x3 << 16)
62 #define PORT_LINK_MODE_4_LANES		(0x7 << 16)
63 #define PORT_LINK_MODE_8_LANES		(0xf << 16)
64 
65 #define PCIE_LINK_WIDTH_SPEED_CONTROL	0x80C
66 #define PORT_LOGIC_SPEED_CHANGE		(0x1 << 17)
67 #define PORT_LOGIC_LINK_WIDTH_MASK	(0x1f << 8)
68 #define PORT_LOGIC_LINK_WIDTH_1_LANES	(0x1 << 8)
69 #define PORT_LOGIC_LINK_WIDTH_2_LANES	(0x2 << 8)
70 #define PORT_LOGIC_LINK_WIDTH_4_LANES	(0x4 << 8)
71 #define PORT_LOGIC_LINK_WIDTH_8_LANES	(0x8 << 8)
72 
73 #define PCIE_DIRECT_SPEED_CHANGE	(0x1 << 17)
74 
75 #define LINK_WAIT_IATU			10000
76 #define PCIE_ATU_ENABLE			(0x1 << 31)
77 #define PCIE_ATU_BAR_MODE_ENABLE	(0x1 << 30 | 1 << 19)
78 #define PCIE_ATU_UNR_REGION_CTRL1	0x00
79 #define PCIE_ATU_UNR_REGION_CTRL2	0x04
80 #define PCIE_ATU_CPU_ADDR_LOW		0x14
81 #define PCIE_ATU_CPU_ADDR_HIGH		0x18
82 
83 /* SRNS: Use Separate refclk(internal clock) instead of from RC */
84 // #define PCIE_ENABLE_SRNS_PLL_REFCLK
85 
86 struct rkpcie_cmd {
87 	u32 cmd;
88 	u32 size;
89 	u32 data[6];
90 };
91 
92 /* rkep device mode status definition */
93 #define RKEP_MODE_BOOTROM	1
94 #define RKEP_MODE_LOADER	2
95 #define RKEP_MODE_KERNEL	3
96 
97 /* Common status */
98 #define RKEP_SMODE_INIT		0
99 #define RKEP_SMODE_LNKRDY	1
100 #define RKEP_SMODE_LNKUP	2
101 #define RKEP_SMODE_ERR		0xff
102 /* Firmware download status */
103 #define RKEP_SMODE_FWDLRDY	0x10
104 #define RKEP_SMODE_FWDLDONE	0x11
105 /* Application status*/
106 #define RKEP_SMODE_APPRDY	0x20
107 
108 struct rkpcie_boot {
109 	/* magic: "RKEP" */
110 	u32 magic;
111 	u32 version;
112 	struct {
113 		u16 mode;
114 		u16 submode;
115 	} devmode;
116 	/* Size of ATAGS for cap */
117 	u32 cap_size;
118 	struct {
119 		u8 cmd;
120 		u8 status;
121 		/* Error code for current CMD */
122 		u16 opcode;
123 	} cmd_status;
124 	u32 reserved[2];
125 	/* RK ATAGS, for mem and other info */
126 	struct tag cap;
127 	/* offset 0x400 */
128 	struct rkpcie_cmd cmd;
129 };
130 
131 static void pcie_inbound_config(void)
132 {
133 	u64 base = PCIE_SNPS_IATU_BASE + 0x100;
134 	u32 val;
135 	char i;
136 
137 	/* BAR0: RKEP_BAR0_ADDR */
138 	writel(RKEP_BAR0_ADDR, base + PCIE_ATU_CPU_ADDR_LOW);
139 	writel(0, base + PCIE_ATU_CPU_ADDR_HIGH);
140 	writel(0, base + PCIE_ATU_UNR_REGION_CTRL1);
141 	/* PCIE_ATU_UNR_REGION_CTRL2 */
142 	writel(PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE | (0 << 8),
143 	       base + PCIE_ATU_UNR_REGION_CTRL2);
144 	for (i = 0; i < 5; i++) {
145 		val = readl(base + PCIE_ATU_UNR_REGION_CTRL2);
146 		if (val & PCIE_ATU_ENABLE)
147 			break;
148 		udelay(LINK_WAIT_IATU);
149 	}
150 	printep("BAR0: 0x%x\n", RKEP_BAR0_ADDR);
151 
152 	/* BAR2: RKEP_BAR2_ADDR */
153 	writel(RKEP_BAR2_ADDR, base + PCIE_ATU_CPU_ADDR_LOW + 0x200);
154 	writel(0, base + PCIE_ATU_CPU_ADDR_HIGH + 0x200);
155 	writel(0, base + PCIE_ATU_UNR_REGION_CTRL1 + 0x200);
156 	writel(PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE | (2 << 8),
157 	       base + PCIE_ATU_UNR_REGION_CTRL2 + 0x200);
158 	for (i = 0; i < 5; i++) {
159 		val = readl(base + PCIE_ATU_UNR_REGION_CTRL2 + 0x200);
160 		if (val & PCIE_ATU_ENABLE)
161 			break;
162 		udelay(LINK_WAIT_IATU);
163 	}
164 	printep("BAR2: 0x%x%x\n", 0, RKEP_BAR2_ADDR);
165 
166 	/* BAR4 is wired reg, no need iATU */
167 }
168 
169 static int rockchip_pcie_ep_set_bar_flag(void *dbi_base, u32 barno, int flags)
170 {
171 	u32 reg;
172 
173 	reg = PCI_BASE_ADDRESS_0 + (4 * barno);
174 
175 	/* Disabled the upper 32bits BAR to make a 64bits bar pair */
176 	if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
177 		writel(0, dbi_base + reg + 0x100000 + 4);
178 
179 	writel(flags, dbi_base + reg);
180 	if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
181 		writel(0, dbi_base + reg + 4);
182 
183 	return 0;
184 }
185 
186 static void pcie_bar_init(void *dbi_base)
187 {
188 	void *resbar_base;
189 	u32 val;
190 
191 	writel(0, dbi_base + 0x10);
192 	writel(0, dbi_base + 0x14);
193 	writel(0, dbi_base + 0x18);
194 	writel(0, dbi_base + 0x1c);
195 	writel(0, dbi_base + 0x20);
196 	writel(0, dbi_base + 0x24);
197 
198 	/* Disable ASPM */
199 	val = readl(dbi_base + 0x7c);
200 	val &= ~(3 << 10);
201 	writel(val, dbi_base + 0x7c);
202 
203 	/* Resize BAR0 to support 4M 32bits */
204 	resbar_base = dbi_base + PCI_RESBAR;
205 	writel(0x40, resbar_base + 0x4);
206 	writel(0x2c0, resbar_base + 0x8);
207 	/* BAR2: 64M 64bits */
208 	writel(0x400, resbar_base + 0x14);
209 	writel(0x6c0, resbar_base + 0x18);
210 	/* BAR4: Fixed for EP wired register, 1M 32bits */
211 	writel(0x10, resbar_base + 0x24);
212 	writel(0xc0, resbar_base + 0x28);
213 	/* Set flags */
214 	rockchip_pcie_ep_set_bar_flag(dbi_base, 0, PCI_BASE_ADDRESS_MEM_TYPE_32);
215 	rockchip_pcie_ep_set_bar_flag(dbi_base, 2,
216 				      PCI_BASE_ADDRESS_MEM_PREFETCH | PCI_BASE_ADDRESS_MEM_TYPE_64);
217 	rockchip_pcie_ep_set_bar_flag(dbi_base, 4, PCI_BASE_ADDRESS_MEM_TYPE_32);
218 
219 	/* Close bar1 bar5 */
220 	writel(0x0, dbi_base + 0x100000 + 0x14);
221 	//writel(0x0, dbi_base + 0x100000 + 0x18);
222 	//writel(0x0, dbi_base + 0x100000 + 0x1c);
223 	//writel(0x0, dbi_base + 0x100000 + 0x20);
224 	writel(0x0, dbi_base + 0x100000 + 0x24);
225 	/* Close ROM BAR */
226 	writel(0x0, dbi_base + 0x100000 + 0x30);
227 }
228 
229 static void pcie_bar0_header_init(void)
230 {
231 	struct rkpcie_boot *bh = (struct rkpcie_boot *)RKEP_BAR0_ADDR;
232 
233 	bh->magic = RKEP_BOOT_MAGIC;
234 	bh->version = 0x100;
235 	bh->devmode.mode = RKEP_MODE_LOADER;
236 	bh->devmode.submode = RKEP_SMODE_INIT;
237 	bh->cap_size = 0;
238 
239 	memset((char *)RKEP_BAR0_CMD_ADDR, 0, sizeof(struct rkpcie_cmd));
240 }
241 
242 static void pcie_link_set_max_speed(void *dbi_base, u32 link_gen)
243 {
244 	u32 cap, ctrl2, link_speed;
245 	u8 offset = 0x70;
246 
247 	cap = readl(dbi_base + offset + PCI_EXP_LNKCAP);
248 	ctrl2 = readl(dbi_base + offset + PCI_EXP_LNKCTL2);
249 	ctrl2 &= ~PCI_EXP_LNKCTL2_TLS;
250 
251 	link_speed = link_gen;
252 
253 	cap &= ~((u32)PCI_EXP_LNKCAP_SLS);
254 	writel(ctrl2 | link_speed, dbi_base + offset + PCI_EXP_LNKCTL2);
255 	writel(cap | link_speed, dbi_base + offset + PCI_EXP_LNKCAP);
256 }
257 
258 static void pcie_link_set_lanes(void *dbi_base, u32 lanes)
259 {
260 	u32 val;
261 
262 	/* Set the number of lanes */
263 	val = readl(dbi_base + PCIE_PORT_LINK_CONTROL);
264 	val &= ~PORT_LINK_MODE_MASK;
265 	switch (lanes) {
266 	case 1:
267 		val |= PORT_LINK_MODE_1_LANES;
268 		break;
269 	case 2:
270 		val |= PORT_LINK_MODE_2_LANES;
271 		break;
272 	case 4:
273 		val |= PORT_LINK_MODE_4_LANES;
274 		break;
275 	default:
276 		printf("RKEP: num-lanes %u: invalid value\n", lanes);
277 		return;
278 	}
279 	writel(val, dbi_base + PCIE_PORT_LINK_CONTROL);
280 
281 	/* Set link width speed control register */
282 	val = readl(dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
283 	val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
284 	switch (lanes) {
285 	case 1:
286 		val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
287 		break;
288 	case 2:
289 		val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
290 		break;
291 	case 4:
292 		val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
293 		break;
294 	}
295 
296 	val |= PCIE_DIRECT_SPEED_CHANGE;
297 
298 	writel(val, dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
299 }
300 
301 static void pcie_devmode_update(int mode, int submode)
302 {
303 	struct rkpcie_boot *bh = (struct rkpcie_boot *)RKEP_BAR0_ADDR;
304 
305 	bh->devmode.mode = mode;
306 	bh->devmode.submode = submode;
307 	flush_dcache_range(RKEP_BAR0_ADDR, RKEP_BAR0_ADDR + 64);
308 }
309 
310 #ifdef CONFIG_SPL_RAM_DEVICE
311 static void pcie_wait_for_fw(void)
312 {
313 	struct rkpcie_cmd *cmd = (struct rkpcie_cmd *)(RKEP_BAR0_CMD_ADDR);
314 	int val;
315 	int i = 0;
316 
317 	printep("Link ready! Waiting RC to download Firmware:\n");
318 	printep("Download uboot.img  to BAR2+0\n");
319 	printep("Download boot.img   to BAR2+0x400000\n");
320 	printep("Send CMD_LOADER_RUN to BAR0+0x400\n");
321 	while (1) {
322 		invalidate_dcache_range(RKEP_BAR0_CMD_ADDR,
323 					RKEP_BAR0_CMD_ADDR + 32);
324 		val = readl(&cmd->cmd);
325 		if (val == RKEP_CMD_LOADER_RUN)
326 			break;
327 		i++;
328 		if (!(i % 10))
329 			printep("Waiting for FW, CMD: %x\n", val);
330 		mdelay(100);
331 	}
332 	/* Invalidate Cache for firmware area: BAR2, 64MB */
333 	invalidate_dcache_range(RKEP_BAR2_ADDR, RKEP_BAR2_ADDR + 0x4000000);
334 	printep("Firmware Download complete!\n");
335 }
336 
337 static void pcie_update_atags(void)
338 {
339 	struct tag_ram_partition t_ram_part;
340 
341 	if (!atags_is_available()) {
342 		printep("RKEP: No ATAGS data found, create new!\n");
343 		atags_destroy();
344 	}
345 
346 	/* ram partition */
347 	memset(&t_ram_part, 0, sizeof(t_ram_part));
348 	t_ram_part.version = 0;
349 	t_ram_part.count = 1;
350 	strcpy(t_ram_part.part[0].name, "boot");
351 	t_ram_part.part[0].start = RKEP_BAR2_ADDR + 0x400000;	/* 4M offset */
352 	t_ram_part.part[0].size  = 0x3c00000;	/* 60M size */
353 	atags_set_tag(ATAG_RAM_PARTITION, &t_ram_part);
354 }
355 
356 void rockchip_pcie_ep_get_firmware(void)
357 {
358 	pcie_devmode_update(RKEP_MODE_LOADER, RKEP_SMODE_FWDLRDY);
359 	pcie_wait_for_fw();
360 	pcie_update_atags();
361 	pcie_devmode_update(RKEP_MODE_LOADER, RKEP_SMODE_FWDLDONE);
362 }
363 #endif
364 
365 #ifdef CONFIG_ROCKCHIP_RK3588
366 #define BUS_IOC_GPIO3D_IOMUX_SEL_H	0xfd5f807c
367 #define GPIO3_BASE			0xfec40000
368 #define GPIO3_SWPORT_DR_H		(GPIO3_BASE + 0x4)
369 #define GPIO3_SWPORT_DDR_H		(GPIO3_BASE + 0xc)
370 
371 static void pcie_board_init(void)
372 {
373 	/* Enable AU5426 buffer chip on EVB4v10 */
374 	/* Set GPIO3D4 to gpio output HIGH mode PCIE20_CLK_PWREN */
375 	writel(0xf << 16, BUS_IOC_GPIO3D_IOMUX_SEL_H);
376 	writel(0x10001000, GPIO3_SWPORT_DDR_H);
377 	writel(0x10001000, GPIO3_SWPORT_DR_H);
378 	udelay(100);
379 }
380 
381 #define PHY_MODE_PCIE_AGGREGATION 4	/* PCIe3x4 */
382 #define PHY_MODE_PCIE_NANBNB	0	/* P1:PCIe3x2  +  P0:PCIe3x2 */
383 #define PHY_MODE_PCIE_NANBBI	1	/* P1:PCIe3x2  +  P0:PCIe3x1*2 */
384 #define PHY_MODE_PCIE_NABINB	2	/* P1:PCIe3x1*2 + P0:PCIe3x2 */
385 #define PHY_MODE_PCIE_NABIBI	3	/* P1:PCIe3x1*2 + P0:PCIe3x1*2 */
386 
387 #define CRU_BASE_ADDR			0xfd7c0000
388 #define CRU_SOFTRST_CON32		(CRU_BASE_ADDR + 0x0a80)
389 #define CRU_SOFTRST_CON33		(CRU_BASE_ADDR + 0x0a84)
390 #define CRU_SOFTRST_CON34		(CRU_BASE_ADDR + 0x0a88)
391 #define CRU_GATE_CON32			(CRU_BASE_ADDR + 0x0880)
392 #define CRU_GATE_CON33			(CRU_BASE_ADDR + 0x0884)
393 #define CRU_GATE_CON34			(CRU_BASE_ADDR + 0x0888)
394 #define CRU_GATE_CON38			(CRU_BASE_ADDR + 0x0898)
395 #define CRU_GATE_CON39			(CRU_BASE_ADDR + 0x089c)
396 #define PHPTOPCRU_BASE_ADDR		0xfd7c8000
397 #define PHPTOPCRU_SOFTRST_CON00		(PHPTOPCRU_BASE_ADDR + 0x0a00)
398 #define PHPTOPCRU_GATE_CON00		(PHPTOPCRU_BASE_ADDR + 0x0800)
399 #define PCIE3PHY_GRF_BASE		0xfd5b8000
400 #define RK3588_PCIE3PHY_GRF_CMN_CON0	(PCIE3PHY_GRF_BASE + 0x0000)
401 #define PCIE3PHY_GRF_PHY0_CON6		(PCIE3PHY_GRF_BASE + 0x0118)
402 #define PCIE3PHY_GRF_PHY1_CON6		(PCIE3PHY_GRF_BASE + 0x0218)
403 #define PCIE3PHY_GRF_PHY0_LN0_CON1	(PCIE3PHY_GRF_BASE + 0x1004)
404 #define PCIE3PHY_GRF_PHY0_LN1_CON1	(PCIE3PHY_GRF_BASE + 0x1104)
405 #define PCIE3PHY_GRF_PHY1_LN0_CON1	(PCIE3PHY_GRF_BASE + 0x2004)
406 #define PCIE3PHY_GRF_PHY1_LN1_CON1	(PCIE3PHY_GRF_BASE + 0x2104)
407 #define FIREWALL_PCIE_MASTER_SEC	0xfe0300f0
408 #define FIREWALL_PCIE_ACCESS		0xfe586040
409 #define CRU_PHYREF_ALT_GATE_CON		(CRU_BASE_ADDR + 0x0c38)
410 #define PMU1_GRF_BASE			0xfd58a000
411 #define PMU_PWR_GATE_SFTCON1		0xfd8d8150
412 #define PMU1_IOC_BASE			0xfd5F0000
413 #define CRU_GLB_RST_CON_OFFSET		(0xC10U)
414 #define CRU_GLB_SRST_FST_VALUE_OFFSET	(0xC08U)
415 
416 void pcie_first_reset(void)
417 {
418 	printep("Fst Reset\n");
419 	mdelay(1);
420 
421 	writel(0xFFDF, CRU_BASE_ADDR + CRU_GLB_RST_CON_OFFSET);
422 	writel(0xffffffff, PMU1_GRF_BASE + 0x4); // reset width
423 	writel(0x30003000, PMU1_GRF_BASE + 0x1c); // pmu1_grf pmu1_ioc hiold
424 	writel(0x00f00020, PMU1_IOC_BASE + 0x0);   //select tsad_shut_m0 iomux
425 	writel(0xFDB9, CRU_BASE_ADDR + CRU_GLB_SRST_FST_VALUE_OFFSET);
426 
427 	while (1)
428 		;
429 }
430 
431 static void pcie_cru_init(void)
432 {
433 	u32 phy0_mplla, phy1_mplla, t0 = 0, t1 = 0;
434 	u32 i, timeout = 500;
435 
436 	/* Enable power domain: PD_PCIE & PD_PHP */
437 	writel(0x1 << 23 | 0x1 << 21, PMU_PWR_GATE_SFTCON1);
438 
439 	/* FixMe init 3.0 PHY */
440 	/* Phy mode: Aggregation NBNB */
441 	writel((0x7 << 16) | PHY_MODE_PCIE_AGGREGATION, RK3588_PCIE3PHY_GRF_CMN_CON0);
442 	printep("PHY Mode 0x%x\n", readl(RK3588_PCIE3PHY_GRF_CMN_CON0) & 7);
443 	/* Enable clock and sfreset for Controller and PHY */
444 	writel(0xffff0000, CRU_SOFTRST_CON32);
445 	writel(0xffff0000, CRU_SOFTRST_CON33);
446 	writel(0xffff0000, CRU_SOFTRST_CON34);
447 	writel(0xffff0000, CRU_GATE_CON32);
448 	writel(0xffff0000, CRU_GATE_CON33);
449 	writel(0xffff0000, CRU_GATE_CON34);
450 	writel(0xffff0000, CRU_GATE_CON38);
451 	writel(0xffff0000, CRU_GATE_CON39);
452 
453 	writel((0x1 << 24), PHPTOPCRU_SOFTRST_CON00);
454 	writel(0xffff0000, PHPTOPCRU_GATE_CON00);
455 
456 	/* PHY Reset */
457 	writel((0x1 << 10) | (0x1 << 26), PHPTOPCRU_SOFTRST_CON00);
458 
459 	udelay(1);
460 
461 #ifdef PCIE_ENABLE_SRNS_PLL_REFCLK
462 	writel(0x000f0000, CRU_PHYREF_ALT_GATE_CON);
463 
464 	/* PHY0 & PHY1  use internal clock */
465 	writel(0x0 | (0x1 << 18), PCIE3PHY_GRF_PHY0_CON6);
466 	writel(0x0 | (0x1 << 18), PCIE3PHY_GRF_PHY1_CON6);
467 
468 	/* phy0_rx0_cmn_refclk_mod */
469 	writel((0x0) | (0x1 << 23), PCIE3PHY_GRF_PHY0_LN0_CON1);
470 	/* phy1_rx0_cmn_refclk_mod */
471 	writel((0x0) | (0x1 << 23), PCIE3PHY_GRF_PHY0_LN1_CON1);
472 	/* phy0_rx0_cmn_refclk_mod */
473 	writel((0x0) | (0x1 << 23), PCIE3PHY_GRF_PHY1_LN0_CON1);
474 	/* phy1_rx0_cmn_refclk_mod */
475 	writel((0x0) | (0x1 << 23), PCIE3PHY_GRF_PHY1_LN1_CON1);
476 #endif
477 
478 	udelay(1000);
479 
480 	/* Deassert PCIe PMA output clamp mode */
481 	writel((0x1 << 8) | (0x1 << 24), RK3588_PCIE3PHY_GRF_CMN_CON0);
482 
483 	/* Deassert PHY Reset */
484 	writel((0x1 << 26), PHPTOPCRU_SOFTRST_CON00);
485 
486 	/* S-Phy: waiting for phy locked */
487 	for (i = 0; i < timeout; i++) {
488 		phy0_mplla = readl(PCIE3PHY_GRF_BASE + 0x904);
489 		phy1_mplla = readl(PCIE3PHY_GRF_BASE + 0xA04);
490 
491 		if (phy0_mplla != t0 || phy1_mplla != t1) {
492 			printep("RKEP: GRF:904=%x, a04=%x...\n", phy0_mplla, phy1_mplla);
493 
494 			t0 = phy0_mplla;
495 			t1 = phy1_mplla;
496 			if (phy0_mplla == 0xF && phy1_mplla == 0xF)
497 				break;
498 		}
499 
500 		udelay(10);
501 	}
502 
503 	if (i >= timeout) {
504 		printep("lock fail\n");
505 		mdelay(1);
506 		pcie_first_reset();
507 	}
508 
509 	/* PHY config: no config need for snps3.0phy */
510 }
511 
512 static void pcie_firewall_init(void)
513 {
514 	/* Enable PCIe Access in firewall and master secure mode */
515 	writel(0xffff0000, FIREWALL_PCIE_MASTER_SEC);
516 	writel(0x01800000, FIREWALL_PCIE_ACCESS);
517 }
518 #elif CONFIG_ROCKCHIP_RK3568
519 
520 static void pcie_board_init(void)
521 {
522 	/* to-do */
523 }
524 
525 static const u16 phy_fw[] = {
526 	#include "./../../../drivers/phy/phy-rockchip-snps-pcie3.fw"
527 };
528 
529 #define GRF_PCIE30PHY_RK3568_CON1 0x4
530 #define GRF_PCIE30PHY_RK3568_CON3 0xC
531 #define GRF_PCIE30PHY_RK3568_CON4 0x10
532 #define GRF_PCIE30PHY_RK3568_CON5 0x14
533 #define GRF_PCIE30PHY_RK3568_CON6 0x18
534 #define GRF_PCIE30PHY_RK3568_CON9 0x24
535 #define GRF_PCIE30PHY_RK3568_STATUS0 0x80
536 #define RK3568_SRAM_INIT_DONE(reg) ((reg) & BIT(14))
537 
538 #define PMUCRU_BASE			0xFDD00000
539 #define PMUCRU_PMUGATE_CON02		(PMUCRU_BASE + 0x188)
540 
541 #define CRU_BASE			0xFDD20000
542 #define CRU_GATE_CON12			(CRU_BASE + 0x330)
543 #define CRU_GATE_CON13			(CRU_BASE + 0x334)
544 #define CRU_GATE_CON33			(CRU_BASE + 0x384)
545 #define CRU_SOFTRST_CON12		(CRU_BASE + 0x430)
546 #define CRU_SOFTRST_CON27		(CRU_BASE + 0x46c)
547 #define CRU_GLB_SRST_FST_OFFSET		(0xD4U)
548 
549 #define PCIE30_PHY_GRF			0xFDCB8000
550 
551 #define SYS_GRF_BASE			0xFDC60000
552 
553 void pcie_first_reset(void)
554 {
555 	printep("Fst Reset\n");
556 	mdelay(1);
557 
558 	writel(0x00040004, CRU_BASE + 0x104);
559 	writel(0x00700010, CRU_BASE);
560 	writel(0x00100010, SYS_GRF_BASE + 0x508);
561 	writel(0xFDB9, CRU_BASE + CRU_GLB_SRST_FST_OFFSET);
562 
563 	while (1)
564 		;
565 }
566 
567 void pcie_cru_init(void)
568 {
569 	u32 i, reg, timeout = 500;
570 	void __iomem *mmio = (void __iomem *)0xFE8C0000;
571 	u32 phy0_status0, phy0_status1, t0 = 0, t1 = 0;
572 
573 	/* Enable phy and controoler clk */
574 	writel(0xffff0000, PMUCRU_PMUGATE_CON02);
575 	writel(0xffff0000, CRU_GATE_CON12);
576 	writel(0xffff0000, CRU_GATE_CON13);
577 	writel(0xffff0000, CRU_GATE_CON33);
578 	writel(0xffff0000, CRU_SOFTRST_CON27);
579 
580 	writel(0x40004000, CRU_SOFTRST_CON27);
581 	writel(0x80008000, PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_CON9);
582 
583 	writel((0x1 << 15) | (0x1 << 31),
584 	       PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_CON9); //map to access sram
585 
586 #ifdef PCIE_ENABLE_SRNS_PLL_REFCLK
587 	/* use internal clock */
588 	writel(0x0 | (0x1 << 31), PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_CON3);
589 
590 	/* rx0_cmn_refclk_mode disabled */
591 	writel((0x0) | (0x1 << 25), PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_CON5);
592 	/* rx1_cmn_refclk_mode disabled */
593 	writel((0x0) | (0x1 << 25), PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_CON6);
594 #endif
595 
596 	writel((0x0 << 14) | (0x1 << (14 + 16)),
597 	       PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_CON4); //sdram_ld_done
598 	writel((0x0 << 13) | (0x1 << (13 + 16)),
599 	       PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_CON4); //sdram_bypass
600 
601 	writel(0x40000000, CRU_SOFTRST_CON27);
602 
603 	udelay(5);
604 	printep("RKEP: sram initial\n");
605 	while (1) {
606 		reg = readl(PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_STATUS0);
607 		if (RK3568_SRAM_INIT_DONE(reg))
608 			break;
609 	}
610 	printep("RKEP: sram init done\n");
611 
612 	writel((0x3 << 8) | (0x3 << (8 + 16)),
613 	       PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_CON9); //map to access sram
614 	for (i = 0; i < ARRAY_SIZE(phy_fw); i++)
615 		writel(phy_fw[i], mmio + (i << 2));
616 
617 	printep("RKEP: snps pcie3phy FW update! size %ld\n", ARRAY_SIZE(phy_fw));
618 	writel((0x0 << 8) | (0x3 << (8 + 16)),
619 	       PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_CON9);
620 	writel((0x1 << 14) | (0x1 << (14 + 16)),
621 	       PCIE30_PHY_GRF + GRF_PCIE30PHY_RK3568_CON4); //sdram_ld_done
622 
623 	writel(0xffff0000, CRU_SOFTRST_CON12);
624 	writel(0x100010, PCIE_SNPS_APB_BASE + 0x180);
625 
626 	/* S-Phy: waiting for phy locked */
627 	for (i = 0; i < timeout; i++) {
628 		phy0_status0 = readl(PCIE30_PHY_GRF + 0x80);
629 		phy0_status1 = readl(PCIE30_PHY_GRF + 0x84);
630 
631 		if (phy0_status0 != t0 || phy0_status1 != t1) {
632 			printep("RKEP: GRF:0x80=%x, 0x84=%x...\n", phy0_status0, phy0_status1);
633 
634 			t0 = phy0_status0;
635 			t1 = phy0_status1;
636 			if (RK3568_SRAM_INIT_DONE(phy0_status0))
637 				break;
638 		}
639 
640 		udelay(10);
641 	}
642 
643 	if (i >= timeout) {
644 		printep("lock fail\n");
645 		mdelay(1);
646 		pcie_first_reset();
647 	}
648 
649 	udelay(1);
650 }
651 
652 static void pcie_firewall_init(void)
653 {
654 }
655 #endif
656 
657 static void pcie_ep_init(void)
658 {
659 	u32 val;
660 	void *dbi_base = (void *)PCIE_SNPS_DBI_BASE;
661 	u64 apb_base = PCIE_SNPS_APB_BASE;
662 	int i, retries = 0, phy_linkup;
663 
664 #ifdef PCIE_ENABLE_SRNS_PLL_REFCLK
665 	printep("RefClock in SRNS clock mode\n");
666 #else
667 	printep("RefClock in common clock_mode\n");
668 #endif
669 
670 	/*
671 	 * ltssm_enable enhance mode and enable delaying the link training
672 	 * after Hot Reset
673 	 */
674 	writel(0x120012, apb_base + 0x180);
675 
676 	/* Unmask pm_turnoff_int */
677 	writel(0x04000000, apb_base + 0x18);
678 
679 	/* PortLorgic DBI_RO_WR_EN */
680 	val = readl((dbi_base + 0x8bc));
681 	val |= 0x1;
682 	writel(val, dbi_base + 0x8bc);
683 
684 reinit:
685 	pcie_bar_init(dbi_base);
686 	pcie_inbound_config();
687 
688 	/* Device PID, DID */
689 	writel(0x1d87, dbi_base + 0x00);
690 	writel(0x356a, dbi_base + 0x02);
691 	/* Device Class: Processing accelerators */
692 	writel(0x1200, dbi_base + 0x0a);
693 
694 	pcie_link_set_max_speed(dbi_base, PCI_EXP_LNKCTL2_TLS_8_0GT);
695 
696 #ifdef	CONFIG_ROCKCHIP_RK3588
697 	pcie_link_set_lanes(dbi_base, 4);
698 #elif	CONFIG_ROCKCHIP_RK3568
699 	pcie_link_set_lanes(dbi_base, 2);
700 #endif
701 
702 	/* EP mode */
703 	writel(0xf00000, apb_base);
704 	udelay(100);
705 
706 	/* Enable EP mem/io access */
707 	val = readl(dbi_base + 0x4);
708 	writel(val | 0x6, dbi_base + 0x4);
709 
710 	val = readl(apb_base + 0x10);
711 	if (val & 0x4) {
712 		printep("Link is reset, int status misc=%x\n", val);
713 		retries++;
714 	}
715 
716 	if (retries)	/* Set app_dly2_done to enable app_ltssm_enable */
717 		writel(0x80008, apb_base + 0x180);
718 	else		/* Enable LTSSM */
719 		writel(0xc000c, apb_base);
720 	printep("init PCIe fast Link up\n");
721 	pcie_devmode_update(RKEP_MODE_LOADER, RKEP_SMODE_LNKRDY);
722 
723 	/* Waiting for Link up */
724 	while (1) {
725 		val = readl(apb_base + 0x300);
726 		if (((val & 0x3ffff) & ((0x3 << 16))) == 0x30000)
727 			break;
728 
729 		if (((val & 0x3ffff) & ((0x3 << 16))) == 0x10000)
730 			phy_linkup = 1;
731 
732 		if (val == 0 && phy_linkup)
733 			pcie_first_reset();
734 
735 		udelay(10);
736 	}
737 	printep("Link up %x\n", val);
738 	mdelay(3);
739 
740 	/* Wait for link stable */
741 	for (i = 0; i < 10000; i++) {
742 		val = readl(apb_base + 0x10);
743 		if (val & 0x4) {
744 			writel(0x4, apb_base + 0x10);
745 			printep("Link is reset, int status misc=%x\n", val);
746 			if (retries < 3) {
747 				retries++;
748 				goto reinit;
749 			} else {
750 				break;
751 			}
752 		}
753 
754 		/* L2 */
755 		val = readl(apb_base + 0x4);
756 		if (val & 0x400) {
757 			writel(0x4, apb_base + 0x10);
758 			pcie_first_reset();
759 		}
760 		udelay(1);
761 	}
762 	printep("Done\n");
763 	pcie_devmode_update(RKEP_MODE_LOADER, RKEP_SMODE_LNKUP);
764 }
765 
766 void rockchip_pcie_ep_init(void)
767 {
768 	u32 val;
769 
770 	printf("\nRKEP: Init PCIe EP\n");
771 	pcie_bar0_header_init();
772 
773 #ifdef CONFIG_ROCKCHIP_RK3588
774 	writel(0x1 << 23 | 0x1 << 21, PMU_PWR_GATE_SFTCON1);
775 	udelay(10);
776 #endif
777 
778 	pcie_firewall_init();
779 	/* Re-in pcie initial */
780 	val = readl(PCIE_SNPS_APB_BASE + 0x300);
781 	if (((val & 0x3ffff) & ((0x3 << 16))) == 0x30000) {
782 		printf("RKEP: already link up\n");
783 		pcie_devmode_update(RKEP_MODE_LOADER, RKEP_SMODE_LNKUP);
784 		return;
785 	}
786 
787 	pcie_board_init();
788 	/* CRU and PHY Init */
789 	pcie_cru_init();
790 
791 	pcie_ep_init();
792 }
793