xref: /arm-trusted-firmware/drivers/marvell/mg_conf_cm3/mg_conf_cm3.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (C) 2019 Marvell International Ltd.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier:     BSD-3-Clause
5*91f16700Schasinglulu  * https://spdx.org/licenses
6*91f16700Schasinglulu  */
7*91f16700Schasinglulu 
8*91f16700Schasinglulu #include <a8k_plat_def.h>
9*91f16700Schasinglulu #include <arch_helpers.h>
10*91f16700Schasinglulu #include <common/debug.h>
11*91f16700Schasinglulu #include <lib/mmio.h>
12*91f16700Schasinglulu #include <mss_scp_bl2_format.h>
13*91f16700Schasinglulu 
14*91f16700Schasinglulu /* CONFI REGISTERS */
15*91f16700Schasinglulu #define MG_CM3_CONFI_BASE(CP)		(MVEBU_CP_REGS_BASE(CP) + 0x100000)
16*91f16700Schasinglulu #define MG_CM3_SRAM_BASE(CP)		MG_CM3_CONFI_BASE(CP)
17*91f16700Schasinglulu #define MG_CM3_CONFI_GLOB_CFG_REG(CP)	(MG_CM3_CONFI_BASE(CP) + 0x2B500)
18*91f16700Schasinglulu #define CM3_CPU_EN_BIT			BIT(28)
19*91f16700Schasinglulu #define MG_CM3_MG_INT_MFX_REG(CP)	(MG_CM3_CONFI_BASE(CP) + 0x2B054)
20*91f16700Schasinglulu #define CM3_SYS_RESET_BIT		BIT(0)
21*91f16700Schasinglulu 
22*91f16700Schasinglulu #define MG_CM3_SHARED_MEM_BASE(CP)	(MG_CM3_SRAM_BASE(CP) + 0x1FC00ULL)
23*91f16700Schasinglulu 
24*91f16700Schasinglulu #define MG_SRAM_SIZE	0x20000 /* 128KB */
25*91f16700Schasinglulu 
26*91f16700Schasinglulu #define MG_ACK_TIMEOUT 10
27*91f16700Schasinglulu 
28*91f16700Schasinglulu /**
29*91f16700Schasinglulu  * struct ap_sharedmem_ctrl - used to pass information between the HOST and CM3
30*91f16700Schasinglulu  * @init_done:	Set by CM3 when ap_proces initialzied. Host check if CM3 set
31*91f16700Schasinglulu  *		this flag to confirm that the process is running
32*91f16700Schasinglulu  * @lane_nr:	Set by Host to mark which comphy lane should be configure. E.g.:
33*91f16700Schasinglulu  *		- A8K development board uses comphy lane 2 for eth0
34*91f16700Schasinglulu  *		- CN913x development board uses comphy lane 4 for eth0
35*91f16700Schasinglulu  */
36*91f16700Schasinglulu struct ap_sharedmem_ctrl {
37*91f16700Schasinglulu 	uint32_t init_done;
38*91f16700Schasinglulu 	uint32_t lane_nr;
39*91f16700Schasinglulu };
40*91f16700Schasinglulu 
41*91f16700Schasinglulu int mg_image_load(uintptr_t src_addr, uint32_t size, int cp_index)
42*91f16700Schasinglulu {
43*91f16700Schasinglulu 	uintptr_t mg_regs = MG_CM3_SRAM_BASE(cp_index);
44*91f16700Schasinglulu 
45*91f16700Schasinglulu 	if (size > MG_SRAM_SIZE) {
46*91f16700Schasinglulu 		ERROR("image is too big to fit into MG CM3 memory\n");
47*91f16700Schasinglulu 		return 1;
48*91f16700Schasinglulu 	}
49*91f16700Schasinglulu 
50*91f16700Schasinglulu 	NOTICE("Loading MG image from address 0x%lx Size 0x%x to MG at 0x%lx\n",
51*91f16700Schasinglulu 	       src_addr, size, mg_regs);
52*91f16700Schasinglulu 
53*91f16700Schasinglulu 	/* Copy image to MG CM3 SRAM */
54*91f16700Schasinglulu 	memcpy((void *)mg_regs, (void *)src_addr, size);
55*91f16700Schasinglulu 
56*91f16700Schasinglulu 	/* Don't release MG CM3 from reset - it will be done by next step
57*91f16700Schasinglulu 	 * bootloader (e.g. U-Boot), when appriopriate device-tree setup (which
58*91f16700Schasinglulu 	 * has enabeld 802.3. auto-neg) will be chosen.
59*91f16700Schasinglulu 	 */
60*91f16700Schasinglulu 
61*91f16700Schasinglulu 	return 0;
62*91f16700Schasinglulu }
63*91f16700Schasinglulu 
64*91f16700Schasinglulu void mg_start_ap_fw(int cp_nr, uint8_t comphy_index)
65*91f16700Schasinglulu {
66*91f16700Schasinglulu 	volatile struct ap_sharedmem_ctrl *ap_shared_ctrl =
67*91f16700Schasinglulu 					(void *)MG_CM3_SHARED_MEM_BASE(cp_nr);
68*91f16700Schasinglulu 	int timeout = MG_ACK_TIMEOUT;
69*91f16700Schasinglulu 
70*91f16700Schasinglulu 	if (mmio_read_32(MG_CM3_CONFI_GLOB_CFG_REG(cp_nr)) & CM3_CPU_EN_BIT) {
71*91f16700Schasinglulu 		VERBOSE("cm3 already running\n");
72*91f16700Schasinglulu 		return;  /* cm3 already running */
73*91f16700Schasinglulu 	}
74*91f16700Schasinglulu 
75*91f16700Schasinglulu 	/*
76*91f16700Schasinglulu 	 * Mark which comphy lane should be used - it will be read via shared
77*91f16700Schasinglulu 	 * mem by ap process
78*91f16700Schasinglulu 	 */
79*91f16700Schasinglulu 	ap_shared_ctrl->lane_nr = comphy_index;
80*91f16700Schasinglulu 	/* Make sure it took place before enabling cm3 */
81*91f16700Schasinglulu 	dmbst();
82*91f16700Schasinglulu 
83*91f16700Schasinglulu 	mmio_setbits_32(MG_CM3_CONFI_GLOB_CFG_REG(cp_nr), CM3_CPU_EN_BIT);
84*91f16700Schasinglulu 	mmio_setbits_32(MG_CM3_MG_INT_MFX_REG(cp_nr), CM3_SYS_RESET_BIT);
85*91f16700Schasinglulu 
86*91f16700Schasinglulu 	/* Check for ap process initialization by fw */
87*91f16700Schasinglulu 	while (ap_shared_ctrl->init_done != 1 && timeout--)
88*91f16700Schasinglulu 		VERBOSE("Waiting for ap process ack, timeout %d\n", timeout);
89*91f16700Schasinglulu 
90*91f16700Schasinglulu 	if (timeout == 0) {
91*91f16700Schasinglulu 		ERROR("AP process failed, disabling cm3\n");
92*91f16700Schasinglulu 		mmio_clrbits_32(MG_CM3_MG_INT_MFX_REG(cp_nr),
93*91f16700Schasinglulu 				CM3_SYS_RESET_BIT);
94*91f16700Schasinglulu 		mmio_clrbits_32(MG_CM3_CONFI_GLOB_CFG_REG(cp_nr),
95*91f16700Schasinglulu 				CM3_CPU_EN_BIT);
96*91f16700Schasinglulu 	}
97*91f16700Schasinglulu }
98