xref: /rk3399_ARM-atf/drivers/marvell/ccu.c (revision 28623c102d6fec0ba0271be64951679bf20681ba)
1c0474d58SKonstantin Porotchkin /*
2c0474d58SKonstantin Porotchkin  * Copyright (C) 2018 Marvell International Ltd.
3c0474d58SKonstantin Porotchkin  *
4c0474d58SKonstantin Porotchkin  * SPDX-License-Identifier:     BSD-3-Clause
5c0474d58SKonstantin Porotchkin  * https://spdx.org/licenses
6c0474d58SKonstantin Porotchkin  */
7c0474d58SKonstantin Porotchkin 
8c0474d58SKonstantin Porotchkin /* CCU unit device driver for Marvell AP807, AP807 and AP810 SoCs */
9c0474d58SKonstantin Porotchkin 
10*4ce3e99aSScott Branden #include <inttypes.h>
11*4ce3e99aSScott Branden #include <stdint.h>
12*4ce3e99aSScott Branden 
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
1409d40e0eSAntonio Nino Diaz #include <drivers/marvell/ccu.h>
1509d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1609d40e0eSAntonio Nino Diaz 
1794d6dd67SKonstantin Porotchkin #include <armada_common.h>
18c0474d58SKonstantin Porotchkin #include <mvebu.h>
19c0474d58SKonstantin Porotchkin #include <mvebu_def.h>
20c0474d58SKonstantin Porotchkin 
21c0474d58SKonstantin Porotchkin #if LOG_LEVEL >= LOG_LEVEL_INFO
22c0474d58SKonstantin Porotchkin #define DEBUG_ADDR_MAP
23c0474d58SKonstantin Porotchkin #endif
24c0474d58SKonstantin Porotchkin 
25c0474d58SKonstantin Porotchkin /* common defines */
26c0474d58SKonstantin Porotchkin #define WIN_ENABLE_BIT			(0x1)
2739b6cc66SAntonio Nino Diaz /* Physical address of the base of the window = {AddrLow[19:0],20'h0} */
28c0474d58SKonstantin Porotchkin #define ADDRESS_SHIFT			(20 - 4)
29c0474d58SKonstantin Porotchkin #define ADDRESS_MASK			(0xFFFFFFF0)
30c0474d58SKonstantin Porotchkin #define CCU_WIN_ALIGNMENT		(0x100000)
31c0474d58SKonstantin Porotchkin 
32a9688f07SAlex Leibovich /*
33a9688f07SAlex Leibovich  * Physical address of the highest address of window bits[31:19] = 0x6FF
34a9688f07SAlex Leibovich  * Physical address of the lowest address of window bits[18:6] = 0x6E0
35a9688f07SAlex Leibovich  * Unit Id bits [5:2] = 2
36a9688f07SAlex Leibovich  * RGF Window Enable bit[0] = 1
37a9688f07SAlex Leibovich  * 0x37f9b809 - 11011111111 0011011100000 0010 0 1
38a9688f07SAlex Leibovich  */
39a9688f07SAlex Leibovich #define ERRATA_WA_CCU_WIN4	0x37f9b809U
40a9688f07SAlex Leibovich 
41a9688f07SAlex Leibovich /*
42a9688f07SAlex Leibovich  * Physical address of the highest address of window bits[31:19] = 0xFFF
43a9688f07SAlex Leibovich  * Physical address of the lowest address of window bits[18:6] = 0x800
44a9688f07SAlex Leibovich  * Unit Id bits [5:2] = 2
45a9688f07SAlex Leibovich  * RGF Window Enable bit[0] = 1
46a9688f07SAlex Leibovich  * 0x7ffa0009 - 111111111111 0100000000000 0010 0 1
47a9688f07SAlex Leibovich  */
48a9688f07SAlex Leibovich #define ERRATA_WA_CCU_WIN5	0x7ffa0009U
49a9688f07SAlex Leibovich 
50a9688f07SAlex Leibovich /*
51a9688f07SAlex Leibovich  * Physical address of the highest address of window bits[31:19] = 0x1FFF
52a9688f07SAlex Leibovich  * Physical address of the lowest address of window bits[18:6] = 0x1000
53a9688f07SAlex Leibovich  * Unit Id bits [5:2] = 2
54a9688f07SAlex Leibovich  * RGF Window Enable bit[0] = 1
55a9688f07SAlex Leibovich  * 0xfffc000d - 1111111111111 1000000000000 0011 0 1
56a9688f07SAlex Leibovich  */
57a9688f07SAlex Leibovich #define ERRATA_WA_CCU_WIN6	0xfffc000dU
58a9688f07SAlex Leibovich 
59c0474d58SKonstantin Porotchkin #define IS_DRAM_TARGET(tgt)		((((tgt) == DRAM_0_TID) || \
60c0474d58SKonstantin Porotchkin 					((tgt) == DRAM_1_TID) || \
61c0474d58SKonstantin Porotchkin 					((tgt) == RAR_TID)) ? 1 : 0)
62c0474d58SKonstantin Porotchkin 
635e4c97d0SStefan Chulski #define CCU_RGF(win)			(MVEBU_CCU_BASE(MVEBU_AP0) + \
645e4c97d0SStefan Chulski 					 0x90 + 4 * (win))
655e4c97d0SStefan Chulski 
66c0474d58SKonstantin Porotchkin /* For storage of CR, SCR, ALR, AHR abd GCR */
67c0474d58SKonstantin Porotchkin static uint32_t ccu_regs_save[MVEBU_CCU_MAX_WINS * 4 + 1];
68c0474d58SKonstantin Porotchkin 
69c0474d58SKonstantin Porotchkin #ifdef DEBUG_ADDR_MAP
dump_ccu(int ap_index)70c0474d58SKonstantin Porotchkin static void dump_ccu(int ap_index)
71c0474d58SKonstantin Porotchkin {
72c0474d58SKonstantin Porotchkin 	uint32_t win_id, win_cr, alr, ahr;
73c0474d58SKonstantin Porotchkin 	uint8_t target_id;
74c0474d58SKonstantin Porotchkin 	uint64_t start, end;
75c0474d58SKonstantin Porotchkin 
76c0474d58SKonstantin Porotchkin 	/* Dump all AP windows */
7739b6cc66SAntonio Nino Diaz 	printf("\tbank  target     start              end\n");
7839b6cc66SAntonio Nino Diaz 	printf("\t----------------------------------------------------\n");
79c0474d58SKonstantin Porotchkin 	for (win_id = 0; win_id < MVEBU_CCU_MAX_WINS; win_id++) {
80c0474d58SKonstantin Porotchkin 		win_cr = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
81c0474d58SKonstantin Porotchkin 		if (win_cr & WIN_ENABLE_BIT) {
82c0474d58SKonstantin Porotchkin 			target_id = (win_cr >> CCU_TARGET_ID_OFFSET) &
83c0474d58SKonstantin Porotchkin 				     CCU_TARGET_ID_MASK;
84c0474d58SKonstantin Porotchkin 			alr = mmio_read_32(CCU_WIN_ALR_OFFSET(ap_index,
85c0474d58SKonstantin Porotchkin 							      win_id));
86c0474d58SKonstantin Porotchkin 			ahr = mmio_read_32(CCU_WIN_AHR_OFFSET(ap_index,
87c0474d58SKonstantin Porotchkin 							      win_id));
88c0474d58SKonstantin Porotchkin 			start = ((uint64_t)alr << ADDRESS_SHIFT);
89c0474d58SKonstantin Porotchkin 			end = (((uint64_t)ahr + 0x10) << ADDRESS_SHIFT);
90*4ce3e99aSScott Branden 			printf("\tccu%d    %02x     0x%016" PRIx64 " 0x%016" PRIx64 "\n",
91957a5addSKonstantin Porotchkin 			       win_id, target_id, start, end);
92c0474d58SKonstantin Porotchkin 		}
93c0474d58SKonstantin Porotchkin 	}
94c0474d58SKonstantin Porotchkin 	win_cr = mmio_read_32(CCU_WIN_GCR_OFFSET(ap_index));
95c0474d58SKonstantin Porotchkin 	target_id = (win_cr >> CCU_GCR_TARGET_OFFSET) & CCU_GCR_TARGET_MASK;
9639b6cc66SAntonio Nino Diaz 	printf("\tccu   GCR %d - all other transactions\n", target_id);
97c0474d58SKonstantin Porotchkin }
98c0474d58SKonstantin Porotchkin #endif
99c0474d58SKonstantin Porotchkin 
ccu_win_check(struct addr_map_win * win)100c0474d58SKonstantin Porotchkin void ccu_win_check(struct addr_map_win *win)
101c0474d58SKonstantin Porotchkin {
102c0474d58SKonstantin Porotchkin 	/* check if address is aligned to 1M */
103c0474d58SKonstantin Porotchkin 	if (IS_NOT_ALIGN(win->base_addr, CCU_WIN_ALIGNMENT)) {
104c0474d58SKonstantin Porotchkin 		win->base_addr = ALIGN_UP(win->base_addr, CCU_WIN_ALIGNMENT);
105*4ce3e99aSScott Branden 		NOTICE("%s: Align up the base address to 0x%" PRIx64 "\n",
106c0474d58SKonstantin Porotchkin 		       __func__, win->base_addr);
107c0474d58SKonstantin Porotchkin 	}
108c0474d58SKonstantin Porotchkin 
109c0474d58SKonstantin Porotchkin 	/* size parameter validity check */
110c0474d58SKonstantin Porotchkin 	if (IS_NOT_ALIGN(win->win_size, CCU_WIN_ALIGNMENT)) {
111c0474d58SKonstantin Porotchkin 		win->win_size = ALIGN_UP(win->win_size, CCU_WIN_ALIGNMENT);
112*4ce3e99aSScott Branden 		NOTICE("%s: Aligning size to 0x%" PRIx64 "\n",
113c0474d58SKonstantin Porotchkin 		       __func__, win->win_size);
114c0474d58SKonstantin Porotchkin 	}
115c0474d58SKonstantin Porotchkin }
116c0474d58SKonstantin Porotchkin 
ccu_is_win_enabled(int ap_index,uint32_t win_id)117957a5addSKonstantin Porotchkin int ccu_is_win_enabled(int ap_index, uint32_t win_id)
118957a5addSKonstantin Porotchkin {
119957a5addSKonstantin Porotchkin 	return mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id)) &
120957a5addSKonstantin Porotchkin 			    WIN_ENABLE_BIT;
121957a5addSKonstantin Porotchkin }
122957a5addSKonstantin Porotchkin 
ccu_enable_win(int ap_index,struct addr_map_win * win,uint32_t win_id)123c0474d58SKonstantin Porotchkin void ccu_enable_win(int ap_index, struct addr_map_win *win, uint32_t win_id)
124c0474d58SKonstantin Porotchkin {
125c0474d58SKonstantin Porotchkin 	uint32_t ccu_win_reg;
126c0474d58SKonstantin Porotchkin 	uint32_t alr, ahr;
127c0474d58SKonstantin Porotchkin 	uint64_t end_addr;
128c0474d58SKonstantin Porotchkin 
129c0474d58SKonstantin Porotchkin 	if ((win_id == 0) || (win_id > MVEBU_CCU_MAX_WINS)) {
130c0474d58SKonstantin Porotchkin 		ERROR("Enabling wrong CCU window %d!\n", win_id);
131c0474d58SKonstantin Porotchkin 		return;
132c0474d58SKonstantin Porotchkin 	}
133c0474d58SKonstantin Porotchkin 
134c0474d58SKonstantin Porotchkin 	end_addr = (win->base_addr + win->win_size - 1);
135c0474d58SKonstantin Porotchkin 	alr = (uint32_t)((win->base_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
136c0474d58SKonstantin Porotchkin 	ahr = (uint32_t)((end_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
137c0474d58SKonstantin Porotchkin 
138c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_ALR_OFFSET(ap_index, win_id), alr);
139c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_AHR_OFFSET(ap_index, win_id), ahr);
140c0474d58SKonstantin Porotchkin 
141c0474d58SKonstantin Porotchkin 	ccu_win_reg = WIN_ENABLE_BIT;
142c0474d58SKonstantin Porotchkin 	ccu_win_reg |= (win->target_id & CCU_TARGET_ID_MASK)
143c0474d58SKonstantin Porotchkin 			<< CCU_TARGET_ID_OFFSET;
144c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_CR_OFFSET(ap_index, win_id), ccu_win_reg);
145c0474d58SKonstantin Porotchkin }
146c0474d58SKonstantin Porotchkin 
ccu_disable_win(int ap_index,uint32_t win_id)147c0474d58SKonstantin Porotchkin static void ccu_disable_win(int ap_index, uint32_t win_id)
148c0474d58SKonstantin Porotchkin {
149c0474d58SKonstantin Porotchkin 	uint32_t win_reg;
150c0474d58SKonstantin Porotchkin 
151c0474d58SKonstantin Porotchkin 	if ((win_id == 0) || (win_id > MVEBU_CCU_MAX_WINS)) {
152c0474d58SKonstantin Porotchkin 		ERROR("Disabling wrong CCU window %d!\n", win_id);
153c0474d58SKonstantin Porotchkin 		return;
154c0474d58SKonstantin Porotchkin 	}
155c0474d58SKonstantin Porotchkin 
156c0474d58SKonstantin Porotchkin 	win_reg = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
157c0474d58SKonstantin Porotchkin 	win_reg &= ~WIN_ENABLE_BIT;
158c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_CR_OFFSET(ap_index, win_id), win_reg);
159c0474d58SKonstantin Porotchkin }
160c0474d58SKonstantin Porotchkin 
161c0474d58SKonstantin Porotchkin /* Insert/Remove temporary window for using the out-of reset default
162c0474d58SKonstantin Porotchkin  * CPx base address to access the CP configuration space prior to
163c0474d58SKonstantin Porotchkin  * the further base address update in accordance with address mapping
164c0474d58SKonstantin Porotchkin  * design.
165c0474d58SKonstantin Porotchkin  *
166c0474d58SKonstantin Porotchkin  * NOTE: Use the same window array for insertion and removal of
167c0474d58SKonstantin Porotchkin  *       temporary windows.
168c0474d58SKonstantin Porotchkin  */
ccu_temp_win_insert(int ap_index,struct addr_map_win * win,int size)169c0474d58SKonstantin Porotchkin void ccu_temp_win_insert(int ap_index, struct addr_map_win *win, int size)
170c0474d58SKonstantin Porotchkin {
171c0474d58SKonstantin Porotchkin 	uint32_t win_id;
172c0474d58SKonstantin Porotchkin 
173c0474d58SKonstantin Porotchkin 	for (int i = 0; i < size; i++) {
174c0474d58SKonstantin Porotchkin 		win_id = MVEBU_CCU_MAX_WINS - 1 - i;
175c0474d58SKonstantin Porotchkin 		ccu_win_check(win);
176c0474d58SKonstantin Porotchkin 		ccu_enable_win(ap_index, win, win_id);
177c0474d58SKonstantin Porotchkin 		win++;
178c0474d58SKonstantin Porotchkin 	}
179c0474d58SKonstantin Porotchkin }
180c0474d58SKonstantin Porotchkin 
181c0474d58SKonstantin Porotchkin /*
182c0474d58SKonstantin Porotchkin  * NOTE: Use the same window array for insertion and removal of
183c0474d58SKonstantin Porotchkin  *       temporary windows.
184c0474d58SKonstantin Porotchkin  */
ccu_temp_win_remove(int ap_index,struct addr_map_win * win,int size)185c0474d58SKonstantin Porotchkin void ccu_temp_win_remove(int ap_index, struct addr_map_win *win, int size)
186c0474d58SKonstantin Porotchkin {
187c0474d58SKonstantin Porotchkin 	uint32_t win_id;
188c0474d58SKonstantin Porotchkin 
189c0474d58SKonstantin Porotchkin 	for (int i = 0; i < size; i++) {
190c0474d58SKonstantin Porotchkin 		uint64_t base;
191c0474d58SKonstantin Porotchkin 		uint32_t target;
192c0474d58SKonstantin Porotchkin 
193c0474d58SKonstantin Porotchkin 		win_id = MVEBU_CCU_MAX_WINS - 1 - i;
194c0474d58SKonstantin Porotchkin 
195c0474d58SKonstantin Porotchkin 		target = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
196c0474d58SKonstantin Porotchkin 		target >>= CCU_TARGET_ID_OFFSET;
197c0474d58SKonstantin Porotchkin 		target &= CCU_TARGET_ID_MASK;
198c0474d58SKonstantin Porotchkin 
199c0474d58SKonstantin Porotchkin 		base = mmio_read_32(CCU_WIN_ALR_OFFSET(ap_index, win_id));
200c0474d58SKonstantin Porotchkin 		base <<= ADDRESS_SHIFT;
201c0474d58SKonstantin Porotchkin 
202c0474d58SKonstantin Porotchkin 		if ((win->target_id != target) || (win->base_addr != base)) {
203c0474d58SKonstantin Porotchkin 			ERROR("%s: Trying to remove bad window-%d!\n",
204c0474d58SKonstantin Porotchkin 			      __func__, win_id);
205c0474d58SKonstantin Porotchkin 			continue;
206c0474d58SKonstantin Porotchkin 		}
207c0474d58SKonstantin Porotchkin 		ccu_disable_win(ap_index, win_id);
208c0474d58SKonstantin Porotchkin 		win++;
209c0474d58SKonstantin Porotchkin 	}
210c0474d58SKonstantin Porotchkin }
211c0474d58SKonstantin Porotchkin 
212c0474d58SKonstantin Porotchkin /* Returns current DRAM window target (DRAM_0_TID, DRAM_1_TID, RAR_TID)
213c0474d58SKonstantin Porotchkin  * NOTE: Call only once for each AP.
214c0474d58SKonstantin Porotchkin  * The AP0 DRAM window is located at index 2 only at the BL31 execution start.
215c0474d58SKonstantin Porotchkin  * Then it relocated to index 1 for matching the rest of APs DRAM settings.
216c0474d58SKonstantin Porotchkin  * Calling this function after relocation will produce wrong results on AP0
217c0474d58SKonstantin Porotchkin  */
ccu_dram_target_get(int ap_index)218c0474d58SKonstantin Porotchkin static uint32_t ccu_dram_target_get(int ap_index)
219c0474d58SKonstantin Porotchkin {
220c0474d58SKonstantin Porotchkin 	/* On BLE stage the AP0 DRAM window is opened by the BootROM at index 2.
221c0474d58SKonstantin Porotchkin 	 * All the rest of detected APs will use window at index 1.
222c0474d58SKonstantin Porotchkin 	 * The AP0 DRAM window is moved from index 2 to 1 during
223c0474d58SKonstantin Porotchkin 	 * init_ccu() execution.
224c0474d58SKonstantin Porotchkin 	 */
225c0474d58SKonstantin Porotchkin 	const uint32_t win_id = (ap_index == 0) ? 2 : 1;
226c0474d58SKonstantin Porotchkin 	uint32_t target;
227c0474d58SKonstantin Porotchkin 
228c0474d58SKonstantin Porotchkin 	target = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
229c0474d58SKonstantin Porotchkin 	target >>= CCU_TARGET_ID_OFFSET;
230c0474d58SKonstantin Porotchkin 	target &= CCU_TARGET_ID_MASK;
231c0474d58SKonstantin Porotchkin 
232c0474d58SKonstantin Porotchkin 	return target;
233c0474d58SKonstantin Porotchkin }
234c0474d58SKonstantin Porotchkin 
ccu_dram_target_set(int ap_index,uint32_t target)235c0474d58SKonstantin Porotchkin void ccu_dram_target_set(int ap_index, uint32_t target)
236c0474d58SKonstantin Porotchkin {
237c0474d58SKonstantin Porotchkin 	/* On BLE stage the AP0 DRAM window is opened by the BootROM at index 2.
238c0474d58SKonstantin Porotchkin 	 * All the rest of detected APs will use window at index 1.
239c0474d58SKonstantin Porotchkin 	 * The AP0 DRAM window is moved from index 2 to 1
240c0474d58SKonstantin Porotchkin 	 * during init_ccu() execution.
241c0474d58SKonstantin Porotchkin 	 */
242c0474d58SKonstantin Porotchkin 	const uint32_t win_id = (ap_index == 0) ? 2 : 1;
243c0474d58SKonstantin Porotchkin 	uint32_t dram_cr;
244c0474d58SKonstantin Porotchkin 
245c0474d58SKonstantin Porotchkin 	dram_cr = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
246c0474d58SKonstantin Porotchkin 	dram_cr &= ~(CCU_TARGET_ID_MASK << CCU_TARGET_ID_OFFSET);
247c0474d58SKonstantin Porotchkin 	dram_cr |= (target & CCU_TARGET_ID_MASK) << CCU_TARGET_ID_OFFSET;
248c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_CR_OFFSET(ap_index, win_id), dram_cr);
249c0474d58SKonstantin Porotchkin }
250c0474d58SKonstantin Porotchkin 
251c0474d58SKonstantin Porotchkin /* Setup CCU DRAM window and enable it */
ccu_dram_win_config(int ap_index,struct addr_map_win * win)252c0474d58SKonstantin Porotchkin void ccu_dram_win_config(int ap_index, struct addr_map_win *win)
253c0474d58SKonstantin Porotchkin {
254c0474d58SKonstantin Porotchkin #if IMAGE_BLE /* BLE */
255c0474d58SKonstantin Porotchkin 	/* On BLE stage the AP0 DRAM window is opened by the BootROM at index 2.
256c0474d58SKonstantin Porotchkin 	 * Since the BootROM is not accessing DRAM at BLE stage,
257c0474d58SKonstantin Porotchkin 	 * the DRAM window can be temporarely disabled.
258c0474d58SKonstantin Porotchkin 	 */
259c0474d58SKonstantin Porotchkin 	const uint32_t win_id = (ap_index == 0) ? 2 : 1;
260c0474d58SKonstantin Porotchkin #else /* end of BLE */
261c0474d58SKonstantin Porotchkin 	/* At the ccu_init() execution stage, DRAM windows of all APs
262c0474d58SKonstantin Porotchkin 	 * are arranged at index 1.
263c0474d58SKonstantin Porotchkin 	 * The AP0 still has the old window BootROM DRAM at index 2, so
264c0474d58SKonstantin Porotchkin 	 * the window-1 can be safely disabled without breaking the DRAM access.
265c0474d58SKonstantin Porotchkin 	 */
266c0474d58SKonstantin Porotchkin 	const uint32_t win_id = 1;
267c0474d58SKonstantin Porotchkin #endif
268c0474d58SKonstantin Porotchkin 
269c0474d58SKonstantin Porotchkin 	ccu_disable_win(ap_index, win_id);
270c0474d58SKonstantin Porotchkin 	/* enable write secure (and clear read secure) */
271c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_SCR_OFFSET(ap_index, win_id),
272c0474d58SKonstantin Porotchkin 		      CCU_WIN_ENA_WRITE_SECURE);
273c0474d58SKonstantin Porotchkin 	ccu_win_check(win);
274c0474d58SKonstantin Porotchkin 	ccu_enable_win(ap_index, win, win_id);
275c0474d58SKonstantin Porotchkin }
276c0474d58SKonstantin Porotchkin 
277c0474d58SKonstantin Porotchkin /* Save content of CCU window + GCR */
ccu_save_win_range(int ap_id,int win_first,int win_last,uint32_t * buffer)278c0474d58SKonstantin Porotchkin static void ccu_save_win_range(int ap_id, int win_first,
279c0474d58SKonstantin Porotchkin 			       int win_last, uint32_t *buffer)
280c0474d58SKonstantin Porotchkin {
281c0474d58SKonstantin Porotchkin 	int win_id, idx;
282c0474d58SKonstantin Porotchkin 	/* Save CCU */
283c0474d58SKonstantin Porotchkin 	for (idx = 0, win_id = win_first; win_id <= win_last; win_id++) {
284c0474d58SKonstantin Porotchkin 		buffer[idx++] = mmio_read_32(CCU_WIN_CR_OFFSET(ap_id, win_id));
285c0474d58SKonstantin Porotchkin 		buffer[idx++] = mmio_read_32(CCU_WIN_SCR_OFFSET(ap_id, win_id));
286c0474d58SKonstantin Porotchkin 		buffer[idx++] = mmio_read_32(CCU_WIN_ALR_OFFSET(ap_id, win_id));
287c0474d58SKonstantin Porotchkin 		buffer[idx++] = mmio_read_32(CCU_WIN_AHR_OFFSET(ap_id, win_id));
288c0474d58SKonstantin Porotchkin 	}
289c0474d58SKonstantin Porotchkin 	buffer[idx] = mmio_read_32(CCU_WIN_GCR_OFFSET(ap_id));
290c0474d58SKonstantin Porotchkin }
291c0474d58SKonstantin Porotchkin 
292c0474d58SKonstantin Porotchkin /* Restore content of CCU window + GCR */
ccu_restore_win_range(int ap_id,int win_first,int win_last,uint32_t * buffer)293c0474d58SKonstantin Porotchkin static void ccu_restore_win_range(int ap_id, int win_first,
294c0474d58SKonstantin Porotchkin 				  int win_last, uint32_t *buffer)
295c0474d58SKonstantin Porotchkin {
296c0474d58SKonstantin Porotchkin 	int win_id, idx;
297c0474d58SKonstantin Porotchkin 	/* Restore CCU */
298c0474d58SKonstantin Porotchkin 	for (idx = 0, win_id = win_first; win_id <= win_last; win_id++) {
299c0474d58SKonstantin Porotchkin 		mmio_write_32(CCU_WIN_CR_OFFSET(ap_id, win_id),  buffer[idx++]);
300c0474d58SKonstantin Porotchkin 		mmio_write_32(CCU_WIN_SCR_OFFSET(ap_id, win_id), buffer[idx++]);
301c0474d58SKonstantin Porotchkin 		mmio_write_32(CCU_WIN_ALR_OFFSET(ap_id, win_id), buffer[idx++]);
302c0474d58SKonstantin Porotchkin 		mmio_write_32(CCU_WIN_AHR_OFFSET(ap_id, win_id), buffer[idx++]);
303c0474d58SKonstantin Porotchkin 	}
304c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_GCR_OFFSET(ap_id), buffer[idx]);
305c0474d58SKonstantin Porotchkin }
306c0474d58SKonstantin Porotchkin 
ccu_save_win_all(int ap_id)307c0474d58SKonstantin Porotchkin void ccu_save_win_all(int ap_id)
308c0474d58SKonstantin Porotchkin {
309c0474d58SKonstantin Porotchkin 	ccu_save_win_range(ap_id, 0, MVEBU_CCU_MAX_WINS - 1, ccu_regs_save);
310c0474d58SKonstantin Porotchkin }
311c0474d58SKonstantin Porotchkin 
ccu_restore_win_all(int ap_id)312c0474d58SKonstantin Porotchkin void ccu_restore_win_all(int ap_id)
313c0474d58SKonstantin Porotchkin {
314c0474d58SKonstantin Porotchkin 	ccu_restore_win_range(ap_id, 0, MVEBU_CCU_MAX_WINS - 1, ccu_regs_save);
315c0474d58SKonstantin Porotchkin }
316c0474d58SKonstantin Porotchkin 
init_ccu(int ap_index)317c0474d58SKonstantin Porotchkin int init_ccu(int ap_index)
318c0474d58SKonstantin Porotchkin {
319c0474d58SKonstantin Porotchkin 	struct addr_map_win *win, *dram_win;
320c0474d58SKonstantin Porotchkin 	uint32_t win_id, win_reg;
321c0474d58SKonstantin Porotchkin 	uint32_t win_count, array_id;
322c0474d58SKonstantin Porotchkin 	uint32_t dram_target;
323c0474d58SKonstantin Porotchkin #if IMAGE_BLE
324c0474d58SKonstantin Porotchkin 	/* In BootROM context CCU Window-1
325c0474d58SKonstantin Porotchkin 	 * has SRAM_TID target and should not be disabled
326c0474d58SKonstantin Porotchkin 	 */
327c0474d58SKonstantin Porotchkin 	const uint32_t win_start = 2;
328c0474d58SKonstantin Porotchkin #else
329c0474d58SKonstantin Porotchkin 	const uint32_t win_start = 1;
330c0474d58SKonstantin Porotchkin #endif
331c0474d58SKonstantin Porotchkin 
332c0474d58SKonstantin Porotchkin 	INFO("Initializing CCU Address decoding\n");
333c0474d58SKonstantin Porotchkin 
334c0474d58SKonstantin Porotchkin 	/* Get the array of the windows and fill the map data */
335c0474d58SKonstantin Porotchkin 	marvell_get_ccu_memory_map(ap_index, &win, &win_count);
336c0474d58SKonstantin Porotchkin 	if (win_count <= 0) {
337c0474d58SKonstantin Porotchkin 		INFO("No windows configurations found\n");
338c0474d58SKonstantin Porotchkin 	} else if (win_count > (MVEBU_CCU_MAX_WINS - 1)) {
339c0474d58SKonstantin Porotchkin 		ERROR("CCU mem map array > than max available windows (%d)\n",
340c0474d58SKonstantin Porotchkin 		      MVEBU_CCU_MAX_WINS);
341c0474d58SKonstantin Porotchkin 		win_count = MVEBU_CCU_MAX_WINS;
342c0474d58SKonstantin Porotchkin 	}
343c0474d58SKonstantin Porotchkin 
344c0474d58SKonstantin Porotchkin 	/* Need to set GCR to DRAM before all CCU windows are disabled for
345c0474d58SKonstantin Porotchkin 	 * securing the normal access to DRAM location, which the ATF is running
346c0474d58SKonstantin Porotchkin 	 * from. Once all CCU windows are set, which have to include the
347c0474d58SKonstantin Porotchkin 	 * dedicated DRAM window as well, the GCR can be switched to the target
348c0474d58SKonstantin Porotchkin 	 * defined by the platform configuration.
349c0474d58SKonstantin Porotchkin 	 */
350c0474d58SKonstantin Porotchkin 	dram_target = ccu_dram_target_get(ap_index);
351c0474d58SKonstantin Porotchkin 	win_reg = (dram_target & CCU_GCR_TARGET_MASK) << CCU_GCR_TARGET_OFFSET;
352c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_GCR_OFFSET(ap_index), win_reg);
353c0474d58SKonstantin Porotchkin 
354c0474d58SKonstantin Porotchkin 	/* If the DRAM window was already configured at the BLE stage,
355c0474d58SKonstantin Porotchkin 	 * only the window target considered valid, the address range should be
356c0474d58SKonstantin Porotchkin 	 * updated according to the platform configuration.
357c0474d58SKonstantin Porotchkin 	 */
358c0474d58SKonstantin Porotchkin 	for (dram_win = win, array_id = 0; array_id < win_count;
359c0474d58SKonstantin Porotchkin 	     array_id++, dram_win++) {
360c0474d58SKonstantin Porotchkin 		if (IS_DRAM_TARGET(dram_win->target_id)) {
361c0474d58SKonstantin Porotchkin 			dram_win->target_id = dram_target;
362c0474d58SKonstantin Porotchkin 			break;
363c0474d58SKonstantin Porotchkin 		}
364c0474d58SKonstantin Porotchkin 	}
365c0474d58SKonstantin Porotchkin 
366c0474d58SKonstantin Porotchkin 	/* Disable all AP CCU windows
367c0474d58SKonstantin Porotchkin 	 * Window-0 is always bypassed since it already contains
368c0474d58SKonstantin Porotchkin 	 * data allowing the internal configuration space access
369c0474d58SKonstantin Porotchkin 	 */
370c0474d58SKonstantin Porotchkin 	for (win_id = win_start; win_id < MVEBU_CCU_MAX_WINS; win_id++) {
371c0474d58SKonstantin Porotchkin 		ccu_disable_win(ap_index, win_id);
372c0474d58SKonstantin Porotchkin 		/* enable write secure (and clear read secure) */
373c0474d58SKonstantin Porotchkin 		mmio_write_32(CCU_WIN_SCR_OFFSET(ap_index, win_id),
374c0474d58SKonstantin Porotchkin 			      CCU_WIN_ENA_WRITE_SECURE);
375c0474d58SKonstantin Porotchkin 	}
376c0474d58SKonstantin Porotchkin 
377c0474d58SKonstantin Porotchkin 	/* win_id is the index of the current ccu window
378c0474d58SKonstantin Porotchkin 	 * array_id is the index of the current memory map window entry
379c0474d58SKonstantin Porotchkin 	 */
380c0474d58SKonstantin Porotchkin 	for (win_id = win_start, array_id = 0;
381c0474d58SKonstantin Porotchkin 	    ((win_id < MVEBU_CCU_MAX_WINS) && (array_id < win_count));
382c0474d58SKonstantin Porotchkin 	    win_id++) {
383c0474d58SKonstantin Porotchkin 		ccu_win_check(win);
384c0474d58SKonstantin Porotchkin 		ccu_enable_win(ap_index, win, win_id);
385c0474d58SKonstantin Porotchkin 		win++;
386c0474d58SKonstantin Porotchkin 		array_id++;
387c0474d58SKonstantin Porotchkin 	}
388c0474d58SKonstantin Porotchkin 
389c0474d58SKonstantin Porotchkin 	/* Get & set the default target according to board topology */
390c0474d58SKonstantin Porotchkin 	win_reg = (marvell_get_ccu_gcr_target(ap_index) & CCU_GCR_TARGET_MASK)
391c0474d58SKonstantin Porotchkin 		   << CCU_GCR_TARGET_OFFSET;
392c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_GCR_OFFSET(ap_index), win_reg);
393c0474d58SKonstantin Porotchkin 
394c0474d58SKonstantin Porotchkin #ifdef DEBUG_ADDR_MAP
395c0474d58SKonstantin Porotchkin 	dump_ccu(ap_index);
396c0474d58SKonstantin Porotchkin #endif
397c0474d58SKonstantin Porotchkin 
398c0474d58SKonstantin Porotchkin 	INFO("Done CCU Address decoding Initializing\n");
399c0474d58SKonstantin Porotchkin 
400c0474d58SKonstantin Porotchkin 	return 0;
401c0474d58SKonstantin Porotchkin }
4025e4c97d0SStefan Chulski 
errata_wa_init(void)4035e4c97d0SStefan Chulski void errata_wa_init(void)
4045e4c97d0SStefan Chulski {
4055e4c97d0SStefan Chulski 	/*
4065e4c97d0SStefan Chulski 	 * EERATA ID: RES-3033912 - Internal Address Space Init state causes
4075e4c97d0SStefan Chulski 	 * a hang upon accesses to [0xf070_0000, 0xf07f_ffff]
4085e4c97d0SStefan Chulski 	 * Workaround: Boot Firmware (ATF) should configure CCU_RGF_WIN(4) to
409a9688f07SAlex Leibovich 	 * split [0x6e_0000, 0x1ff_ffff] to values [0x6e_0000, 0x6f_ffff] and
410a9688f07SAlex Leibovich 	 * [0x80_0000, 0xff_ffff] and [0x100_0000, 0x1ff_ffff],that cause
411a9688f07SAlex Leibovich 	 * accesses to the segment of [0xf070_0000, 0xf1ff_ffff]
412a9688f07SAlex Leibovich 	 * to act as RAZWI.
4135e4c97d0SStefan Chulski 	 */
414a9688f07SAlex Leibovich 	mmio_write_32(CCU_RGF(4), ERRATA_WA_CCU_WIN4);
415a9688f07SAlex Leibovich 	mmio_write_32(CCU_RGF(5), ERRATA_WA_CCU_WIN5);
416a9688f07SAlex Leibovich 	mmio_write_32(CCU_RGF(6), ERRATA_WA_CCU_WIN6);
4175e4c97d0SStefan Chulski }
418