xref: /rk3399_ARM-atf/drivers/marvell/ccu.c (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
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*09d40e0eSAntonio Nino Diaz #include <common/debug.h>
11*09d40e0eSAntonio Nino Diaz #include <drivers/marvell/ccu.h>
12*09d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
13*09d40e0eSAntonio Nino Diaz 
1494d6dd67SKonstantin Porotchkin #include <armada_common.h>
15c0474d58SKonstantin Porotchkin #include <mvebu.h>
16c0474d58SKonstantin Porotchkin #include <mvebu_def.h>
17c0474d58SKonstantin Porotchkin 
18c0474d58SKonstantin Porotchkin #if LOG_LEVEL >= LOG_LEVEL_INFO
19c0474d58SKonstantin Porotchkin #define DEBUG_ADDR_MAP
20c0474d58SKonstantin Porotchkin #endif
21c0474d58SKonstantin Porotchkin 
22c0474d58SKonstantin Porotchkin /* common defines */
23c0474d58SKonstantin Porotchkin #define WIN_ENABLE_BIT			(0x1)
2439b6cc66SAntonio Nino Diaz /* Physical address of the base of the window = {AddrLow[19:0],20'h0} */
25c0474d58SKonstantin Porotchkin #define ADDRESS_SHIFT			(20 - 4)
26c0474d58SKonstantin Porotchkin #define ADDRESS_MASK			(0xFFFFFFF0)
27c0474d58SKonstantin Porotchkin #define CCU_WIN_ALIGNMENT		(0x100000)
28c0474d58SKonstantin Porotchkin 
29c0474d58SKonstantin Porotchkin #define IS_DRAM_TARGET(tgt)		((((tgt) == DRAM_0_TID) || \
30c0474d58SKonstantin Porotchkin 					((tgt) == DRAM_1_TID) || \
31c0474d58SKonstantin Porotchkin 					((tgt) == RAR_TID)) ? 1 : 0)
32c0474d58SKonstantin Porotchkin 
33c0474d58SKonstantin Porotchkin /* For storage of CR, SCR, ALR, AHR abd GCR */
34c0474d58SKonstantin Porotchkin static uint32_t ccu_regs_save[MVEBU_CCU_MAX_WINS * 4 + 1];
35c0474d58SKonstantin Porotchkin 
36c0474d58SKonstantin Porotchkin #ifdef DEBUG_ADDR_MAP
37c0474d58SKonstantin Porotchkin static void dump_ccu(int ap_index)
38c0474d58SKonstantin Porotchkin {
39c0474d58SKonstantin Porotchkin 	uint32_t win_id, win_cr, alr, ahr;
40c0474d58SKonstantin Porotchkin 	uint8_t target_id;
41c0474d58SKonstantin Porotchkin 	uint64_t start, end;
42c0474d58SKonstantin Porotchkin 
43c0474d58SKonstantin Porotchkin 	/* Dump all AP windows */
4439b6cc66SAntonio Nino Diaz 	printf("\tbank  target     start              end\n");
4539b6cc66SAntonio Nino Diaz 	printf("\t----------------------------------------------------\n");
46c0474d58SKonstantin Porotchkin 	for (win_id = 0; win_id < MVEBU_CCU_MAX_WINS; win_id++) {
47c0474d58SKonstantin Porotchkin 		win_cr = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
48c0474d58SKonstantin Porotchkin 		if (win_cr & WIN_ENABLE_BIT) {
49c0474d58SKonstantin Porotchkin 			target_id = (win_cr >> CCU_TARGET_ID_OFFSET) &
50c0474d58SKonstantin Porotchkin 				     CCU_TARGET_ID_MASK;
51c0474d58SKonstantin Porotchkin 			alr = mmio_read_32(CCU_WIN_ALR_OFFSET(ap_index,
52c0474d58SKonstantin Porotchkin 							      win_id));
53c0474d58SKonstantin Porotchkin 			ahr = mmio_read_32(CCU_WIN_AHR_OFFSET(ap_index,
54c0474d58SKonstantin Porotchkin 							      win_id));
55c0474d58SKonstantin Porotchkin 			start = ((uint64_t)alr << ADDRESS_SHIFT);
56c0474d58SKonstantin Porotchkin 			end = (((uint64_t)ahr + 0x10) << ADDRESS_SHIFT);
5739b6cc66SAntonio Nino Diaz 			printf("\tccu    %02x     0x%016llx 0x%016llx\n",
58c0474d58SKonstantin Porotchkin 			       target_id, start, end);
59c0474d58SKonstantin Porotchkin 		}
60c0474d58SKonstantin Porotchkin 	}
61c0474d58SKonstantin Porotchkin 	win_cr = mmio_read_32(CCU_WIN_GCR_OFFSET(ap_index));
62c0474d58SKonstantin Porotchkin 	target_id = (win_cr >> CCU_GCR_TARGET_OFFSET) & CCU_GCR_TARGET_MASK;
6339b6cc66SAntonio Nino Diaz 	printf("\tccu   GCR %d - all other transactions\n", target_id);
64c0474d58SKonstantin Porotchkin }
65c0474d58SKonstantin Porotchkin #endif
66c0474d58SKonstantin Porotchkin 
67c0474d58SKonstantin Porotchkin void ccu_win_check(struct addr_map_win *win)
68c0474d58SKonstantin Porotchkin {
69c0474d58SKonstantin Porotchkin 	/* check if address is aligned to 1M */
70c0474d58SKonstantin Porotchkin 	if (IS_NOT_ALIGN(win->base_addr, CCU_WIN_ALIGNMENT)) {
71c0474d58SKonstantin Porotchkin 		win->base_addr = ALIGN_UP(win->base_addr, CCU_WIN_ALIGNMENT);
72c0474d58SKonstantin Porotchkin 		NOTICE("%s: Align up the base address to 0x%llx\n",
73c0474d58SKonstantin Porotchkin 		       __func__, win->base_addr);
74c0474d58SKonstantin Porotchkin 	}
75c0474d58SKonstantin Porotchkin 
76c0474d58SKonstantin Porotchkin 	/* size parameter validity check */
77c0474d58SKonstantin Porotchkin 	if (IS_NOT_ALIGN(win->win_size, CCU_WIN_ALIGNMENT)) {
78c0474d58SKonstantin Porotchkin 		win->win_size = ALIGN_UP(win->win_size, CCU_WIN_ALIGNMENT);
79c0474d58SKonstantin Porotchkin 		NOTICE("%s: Aligning size to 0x%llx\n",
80c0474d58SKonstantin Porotchkin 		       __func__, win->win_size);
81c0474d58SKonstantin Porotchkin 	}
82c0474d58SKonstantin Porotchkin }
83c0474d58SKonstantin Porotchkin 
84c0474d58SKonstantin Porotchkin void ccu_enable_win(int ap_index, struct addr_map_win *win, uint32_t win_id)
85c0474d58SKonstantin Porotchkin {
86c0474d58SKonstantin Porotchkin 	uint32_t ccu_win_reg;
87c0474d58SKonstantin Porotchkin 	uint32_t alr, ahr;
88c0474d58SKonstantin Porotchkin 	uint64_t end_addr;
89c0474d58SKonstantin Porotchkin 
90c0474d58SKonstantin Porotchkin 	if ((win_id == 0) || (win_id > MVEBU_CCU_MAX_WINS)) {
91c0474d58SKonstantin Porotchkin 		ERROR("Enabling wrong CCU window %d!\n", win_id);
92c0474d58SKonstantin Porotchkin 		return;
93c0474d58SKonstantin Porotchkin 	}
94c0474d58SKonstantin Porotchkin 
95c0474d58SKonstantin Porotchkin 	end_addr = (win->base_addr + win->win_size - 1);
96c0474d58SKonstantin Porotchkin 	alr = (uint32_t)((win->base_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
97c0474d58SKonstantin Porotchkin 	ahr = (uint32_t)((end_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
98c0474d58SKonstantin Porotchkin 
99c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_ALR_OFFSET(ap_index, win_id), alr);
100c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_AHR_OFFSET(ap_index, win_id), ahr);
101c0474d58SKonstantin Porotchkin 
102c0474d58SKonstantin Porotchkin 	ccu_win_reg = WIN_ENABLE_BIT;
103c0474d58SKonstantin Porotchkin 	ccu_win_reg |= (win->target_id & CCU_TARGET_ID_MASK)
104c0474d58SKonstantin Porotchkin 			<< CCU_TARGET_ID_OFFSET;
105c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_CR_OFFSET(ap_index, win_id), ccu_win_reg);
106c0474d58SKonstantin Porotchkin }
107c0474d58SKonstantin Porotchkin 
108c0474d58SKonstantin Porotchkin static void ccu_disable_win(int ap_index, uint32_t win_id)
109c0474d58SKonstantin Porotchkin {
110c0474d58SKonstantin Porotchkin 	uint32_t win_reg;
111c0474d58SKonstantin Porotchkin 
112c0474d58SKonstantin Porotchkin 	if ((win_id == 0) || (win_id > MVEBU_CCU_MAX_WINS)) {
113c0474d58SKonstantin Porotchkin 		ERROR("Disabling wrong CCU window %d!\n", win_id);
114c0474d58SKonstantin Porotchkin 		return;
115c0474d58SKonstantin Porotchkin 	}
116c0474d58SKonstantin Porotchkin 
117c0474d58SKonstantin Porotchkin 	win_reg = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
118c0474d58SKonstantin Porotchkin 	win_reg &= ~WIN_ENABLE_BIT;
119c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_CR_OFFSET(ap_index, win_id), win_reg);
120c0474d58SKonstantin Porotchkin }
121c0474d58SKonstantin Porotchkin 
122c0474d58SKonstantin Porotchkin /* Insert/Remove temporary window for using the out-of reset default
123c0474d58SKonstantin Porotchkin  * CPx base address to access the CP configuration space prior to
124c0474d58SKonstantin Porotchkin  * the further base address update in accordance with address mapping
125c0474d58SKonstantin Porotchkin  * design.
126c0474d58SKonstantin Porotchkin  *
127c0474d58SKonstantin Porotchkin  * NOTE: Use the same window array for insertion and removal of
128c0474d58SKonstantin Porotchkin  *       temporary windows.
129c0474d58SKonstantin Porotchkin  */
130c0474d58SKonstantin Porotchkin void ccu_temp_win_insert(int ap_index, struct addr_map_win *win, int size)
131c0474d58SKonstantin Porotchkin {
132c0474d58SKonstantin Porotchkin 	uint32_t win_id;
133c0474d58SKonstantin Porotchkin 
134c0474d58SKonstantin Porotchkin 	for (int i = 0; i < size; i++) {
135c0474d58SKonstantin Porotchkin 		win_id = MVEBU_CCU_MAX_WINS - 1 - i;
136c0474d58SKonstantin Porotchkin 		ccu_win_check(win);
137c0474d58SKonstantin Porotchkin 		ccu_enable_win(ap_index, win, win_id);
138c0474d58SKonstantin Porotchkin 		win++;
139c0474d58SKonstantin Porotchkin 	}
140c0474d58SKonstantin Porotchkin }
141c0474d58SKonstantin Porotchkin 
142c0474d58SKonstantin Porotchkin /*
143c0474d58SKonstantin Porotchkin  * NOTE: Use the same window array for insertion and removal of
144c0474d58SKonstantin Porotchkin  *       temporary windows.
145c0474d58SKonstantin Porotchkin  */
146c0474d58SKonstantin Porotchkin void ccu_temp_win_remove(int ap_index, struct addr_map_win *win, int size)
147c0474d58SKonstantin Porotchkin {
148c0474d58SKonstantin Porotchkin 	uint32_t win_id;
149c0474d58SKonstantin Porotchkin 
150c0474d58SKonstantin Porotchkin 	for (int i = 0; i < size; i++) {
151c0474d58SKonstantin Porotchkin 		uint64_t base;
152c0474d58SKonstantin Porotchkin 		uint32_t target;
153c0474d58SKonstantin Porotchkin 
154c0474d58SKonstantin Porotchkin 		win_id = MVEBU_CCU_MAX_WINS - 1 - i;
155c0474d58SKonstantin Porotchkin 
156c0474d58SKonstantin Porotchkin 		target = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
157c0474d58SKonstantin Porotchkin 		target >>= CCU_TARGET_ID_OFFSET;
158c0474d58SKonstantin Porotchkin 		target &= CCU_TARGET_ID_MASK;
159c0474d58SKonstantin Porotchkin 
160c0474d58SKonstantin Porotchkin 		base = mmio_read_32(CCU_WIN_ALR_OFFSET(ap_index, win_id));
161c0474d58SKonstantin Porotchkin 		base <<= ADDRESS_SHIFT;
162c0474d58SKonstantin Porotchkin 
163c0474d58SKonstantin Porotchkin 		if ((win->target_id != target) || (win->base_addr != base)) {
164c0474d58SKonstantin Porotchkin 			ERROR("%s: Trying to remove bad window-%d!\n",
165c0474d58SKonstantin Porotchkin 			      __func__, win_id);
166c0474d58SKonstantin Porotchkin 			continue;
167c0474d58SKonstantin Porotchkin 		}
168c0474d58SKonstantin Porotchkin 		ccu_disable_win(ap_index, win_id);
169c0474d58SKonstantin Porotchkin 		win++;
170c0474d58SKonstantin Porotchkin 	}
171c0474d58SKonstantin Porotchkin }
172c0474d58SKonstantin Porotchkin 
173c0474d58SKonstantin Porotchkin /* Returns current DRAM window target (DRAM_0_TID, DRAM_1_TID, RAR_TID)
174c0474d58SKonstantin Porotchkin  * NOTE: Call only once for each AP.
175c0474d58SKonstantin Porotchkin  * The AP0 DRAM window is located at index 2 only at the BL31 execution start.
176c0474d58SKonstantin Porotchkin  * Then it relocated to index 1 for matching the rest of APs DRAM settings.
177c0474d58SKonstantin Porotchkin  * Calling this function after relocation will produce wrong results on AP0
178c0474d58SKonstantin Porotchkin  */
179c0474d58SKonstantin Porotchkin static uint32_t ccu_dram_target_get(int ap_index)
180c0474d58SKonstantin Porotchkin {
181c0474d58SKonstantin Porotchkin 	/* On BLE stage the AP0 DRAM window is opened by the BootROM at index 2.
182c0474d58SKonstantin Porotchkin 	 * All the rest of detected APs will use window at index 1.
183c0474d58SKonstantin Porotchkin 	 * The AP0 DRAM window is moved from index 2 to 1 during
184c0474d58SKonstantin Porotchkin 	 * init_ccu() execution.
185c0474d58SKonstantin Porotchkin 	 */
186c0474d58SKonstantin Porotchkin 	const uint32_t win_id = (ap_index == 0) ? 2 : 1;
187c0474d58SKonstantin Porotchkin 	uint32_t target;
188c0474d58SKonstantin Porotchkin 
189c0474d58SKonstantin Porotchkin 	target = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
190c0474d58SKonstantin Porotchkin 	target >>= CCU_TARGET_ID_OFFSET;
191c0474d58SKonstantin Porotchkin 	target &= CCU_TARGET_ID_MASK;
192c0474d58SKonstantin Porotchkin 
193c0474d58SKonstantin Porotchkin 	return target;
194c0474d58SKonstantin Porotchkin }
195c0474d58SKonstantin Porotchkin 
196c0474d58SKonstantin Porotchkin void ccu_dram_target_set(int ap_index, uint32_t target)
197c0474d58SKonstantin Porotchkin {
198c0474d58SKonstantin Porotchkin 	/* On BLE stage the AP0 DRAM window is opened by the BootROM at index 2.
199c0474d58SKonstantin Porotchkin 	 * All the rest of detected APs will use window at index 1.
200c0474d58SKonstantin Porotchkin 	 * The AP0 DRAM window is moved from index 2 to 1
201c0474d58SKonstantin Porotchkin 	 * during init_ccu() execution.
202c0474d58SKonstantin Porotchkin 	 */
203c0474d58SKonstantin Porotchkin 	const uint32_t win_id = (ap_index == 0) ? 2 : 1;
204c0474d58SKonstantin Porotchkin 	uint32_t dram_cr;
205c0474d58SKonstantin Porotchkin 
206c0474d58SKonstantin Porotchkin 	dram_cr = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
207c0474d58SKonstantin Porotchkin 	dram_cr &= ~(CCU_TARGET_ID_MASK << CCU_TARGET_ID_OFFSET);
208c0474d58SKonstantin Porotchkin 	dram_cr |= (target & CCU_TARGET_ID_MASK) << CCU_TARGET_ID_OFFSET;
209c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_CR_OFFSET(ap_index, win_id), dram_cr);
210c0474d58SKonstantin Porotchkin }
211c0474d58SKonstantin Porotchkin 
212c0474d58SKonstantin Porotchkin /* Setup CCU DRAM window and enable it */
213c0474d58SKonstantin Porotchkin void ccu_dram_win_config(int ap_index, struct addr_map_win *win)
214c0474d58SKonstantin Porotchkin {
215c0474d58SKonstantin Porotchkin #if IMAGE_BLE /* BLE */
216c0474d58SKonstantin Porotchkin 	/* On BLE stage the AP0 DRAM window is opened by the BootROM at index 2.
217c0474d58SKonstantin Porotchkin 	 * Since the BootROM is not accessing DRAM at BLE stage,
218c0474d58SKonstantin Porotchkin 	 * the DRAM window can be temporarely disabled.
219c0474d58SKonstantin Porotchkin 	 */
220c0474d58SKonstantin Porotchkin 	const uint32_t win_id = (ap_index == 0) ? 2 : 1;
221c0474d58SKonstantin Porotchkin #else /* end of BLE */
222c0474d58SKonstantin Porotchkin 	/* At the ccu_init() execution stage, DRAM windows of all APs
223c0474d58SKonstantin Porotchkin 	 * are arranged at index 1.
224c0474d58SKonstantin Porotchkin 	 * The AP0 still has the old window BootROM DRAM at index 2, so
225c0474d58SKonstantin Porotchkin 	 * the window-1 can be safely disabled without breaking the DRAM access.
226c0474d58SKonstantin Porotchkin 	 */
227c0474d58SKonstantin Porotchkin 	const uint32_t win_id = 1;
228c0474d58SKonstantin Porotchkin #endif
229c0474d58SKonstantin Porotchkin 
230c0474d58SKonstantin Porotchkin 	ccu_disable_win(ap_index, win_id);
231c0474d58SKonstantin Porotchkin 	/* enable write secure (and clear read secure) */
232c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_SCR_OFFSET(ap_index, win_id),
233c0474d58SKonstantin Porotchkin 		      CCU_WIN_ENA_WRITE_SECURE);
234c0474d58SKonstantin Porotchkin 	ccu_win_check(win);
235c0474d58SKonstantin Porotchkin 	ccu_enable_win(ap_index, win, win_id);
236c0474d58SKonstantin Porotchkin }
237c0474d58SKonstantin Porotchkin 
238c0474d58SKonstantin Porotchkin /* Save content of CCU window + GCR */
239c0474d58SKonstantin Porotchkin static void ccu_save_win_range(int ap_id, int win_first,
240c0474d58SKonstantin Porotchkin 			       int win_last, uint32_t *buffer)
241c0474d58SKonstantin Porotchkin {
242c0474d58SKonstantin Porotchkin 	int win_id, idx;
243c0474d58SKonstantin Porotchkin 	/* Save CCU */
244c0474d58SKonstantin Porotchkin 	for (idx = 0, win_id = win_first; win_id <= win_last; win_id++) {
245c0474d58SKonstantin Porotchkin 		buffer[idx++] = mmio_read_32(CCU_WIN_CR_OFFSET(ap_id, win_id));
246c0474d58SKonstantin Porotchkin 		buffer[idx++] = mmio_read_32(CCU_WIN_SCR_OFFSET(ap_id, win_id));
247c0474d58SKonstantin Porotchkin 		buffer[idx++] = mmio_read_32(CCU_WIN_ALR_OFFSET(ap_id, win_id));
248c0474d58SKonstantin Porotchkin 		buffer[idx++] = mmio_read_32(CCU_WIN_AHR_OFFSET(ap_id, win_id));
249c0474d58SKonstantin Porotchkin 	}
250c0474d58SKonstantin Porotchkin 	buffer[idx] = mmio_read_32(CCU_WIN_GCR_OFFSET(ap_id));
251c0474d58SKonstantin Porotchkin }
252c0474d58SKonstantin Porotchkin 
253c0474d58SKonstantin Porotchkin /* Restore content of CCU window + GCR */
254c0474d58SKonstantin Porotchkin static void ccu_restore_win_range(int ap_id, int win_first,
255c0474d58SKonstantin Porotchkin 				  int win_last, uint32_t *buffer)
256c0474d58SKonstantin Porotchkin {
257c0474d58SKonstantin Porotchkin 	int win_id, idx;
258c0474d58SKonstantin Porotchkin 	/* Restore CCU */
259c0474d58SKonstantin Porotchkin 	for (idx = 0, win_id = win_first; win_id <= win_last; win_id++) {
260c0474d58SKonstantin Porotchkin 		mmio_write_32(CCU_WIN_CR_OFFSET(ap_id, win_id),  buffer[idx++]);
261c0474d58SKonstantin Porotchkin 		mmio_write_32(CCU_WIN_SCR_OFFSET(ap_id, win_id), buffer[idx++]);
262c0474d58SKonstantin Porotchkin 		mmio_write_32(CCU_WIN_ALR_OFFSET(ap_id, win_id), buffer[idx++]);
263c0474d58SKonstantin Porotchkin 		mmio_write_32(CCU_WIN_AHR_OFFSET(ap_id, win_id), buffer[idx++]);
264c0474d58SKonstantin Porotchkin 	}
265c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_GCR_OFFSET(ap_id), buffer[idx]);
266c0474d58SKonstantin Porotchkin }
267c0474d58SKonstantin Porotchkin 
268c0474d58SKonstantin Porotchkin void ccu_save_win_all(int ap_id)
269c0474d58SKonstantin Porotchkin {
270c0474d58SKonstantin Porotchkin 	ccu_save_win_range(ap_id, 0, MVEBU_CCU_MAX_WINS - 1, ccu_regs_save);
271c0474d58SKonstantin Porotchkin }
272c0474d58SKonstantin Porotchkin 
273c0474d58SKonstantin Porotchkin void ccu_restore_win_all(int ap_id)
274c0474d58SKonstantin Porotchkin {
275c0474d58SKonstantin Porotchkin 	ccu_restore_win_range(ap_id, 0, MVEBU_CCU_MAX_WINS - 1, ccu_regs_save);
276c0474d58SKonstantin Porotchkin }
277c0474d58SKonstantin Porotchkin 
278c0474d58SKonstantin Porotchkin int init_ccu(int ap_index)
279c0474d58SKonstantin Porotchkin {
280c0474d58SKonstantin Porotchkin 	struct addr_map_win *win, *dram_win;
281c0474d58SKonstantin Porotchkin 	uint32_t win_id, win_reg;
282c0474d58SKonstantin Porotchkin 	uint32_t win_count, array_id;
283c0474d58SKonstantin Porotchkin 	uint32_t dram_target;
284c0474d58SKonstantin Porotchkin #if IMAGE_BLE
285c0474d58SKonstantin Porotchkin 	/* In BootROM context CCU Window-1
286c0474d58SKonstantin Porotchkin 	 * has SRAM_TID target and should not be disabled
287c0474d58SKonstantin Porotchkin 	 */
288c0474d58SKonstantin Porotchkin 	const uint32_t win_start = 2;
289c0474d58SKonstantin Porotchkin #else
290c0474d58SKonstantin Porotchkin 	const uint32_t win_start = 1;
291c0474d58SKonstantin Porotchkin #endif
292c0474d58SKonstantin Porotchkin 
293c0474d58SKonstantin Porotchkin 	INFO("Initializing CCU Address decoding\n");
294c0474d58SKonstantin Porotchkin 
295c0474d58SKonstantin Porotchkin 	/* Get the array of the windows and fill the map data */
296c0474d58SKonstantin Porotchkin 	marvell_get_ccu_memory_map(ap_index, &win, &win_count);
297c0474d58SKonstantin Porotchkin 	if (win_count <= 0) {
298c0474d58SKonstantin Porotchkin 		INFO("No windows configurations found\n");
299c0474d58SKonstantin Porotchkin 	} else if (win_count > (MVEBU_CCU_MAX_WINS - 1)) {
300c0474d58SKonstantin Porotchkin 		ERROR("CCU mem map array > than max available windows (%d)\n",
301c0474d58SKonstantin Porotchkin 		      MVEBU_CCU_MAX_WINS);
302c0474d58SKonstantin Porotchkin 		win_count = MVEBU_CCU_MAX_WINS;
303c0474d58SKonstantin Porotchkin 	}
304c0474d58SKonstantin Porotchkin 
305c0474d58SKonstantin Porotchkin 	/* Need to set GCR to DRAM before all CCU windows are disabled for
306c0474d58SKonstantin Porotchkin 	 * securing the normal access to DRAM location, which the ATF is running
307c0474d58SKonstantin Porotchkin 	 * from. Once all CCU windows are set, which have to include the
308c0474d58SKonstantin Porotchkin 	 * dedicated DRAM window as well, the GCR can be switched to the target
309c0474d58SKonstantin Porotchkin 	 * defined by the platform configuration.
310c0474d58SKonstantin Porotchkin 	 */
311c0474d58SKonstantin Porotchkin 	dram_target = ccu_dram_target_get(ap_index);
312c0474d58SKonstantin Porotchkin 	win_reg = (dram_target & CCU_GCR_TARGET_MASK) << CCU_GCR_TARGET_OFFSET;
313c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_GCR_OFFSET(ap_index), win_reg);
314c0474d58SKonstantin Porotchkin 
315c0474d58SKonstantin Porotchkin 	/* If the DRAM window was already configured at the BLE stage,
316c0474d58SKonstantin Porotchkin 	 * only the window target considered valid, the address range should be
317c0474d58SKonstantin Porotchkin 	 * updated according to the platform configuration.
318c0474d58SKonstantin Porotchkin 	 */
319c0474d58SKonstantin Porotchkin 	for (dram_win = win, array_id = 0; array_id < win_count;
320c0474d58SKonstantin Porotchkin 	     array_id++, dram_win++) {
321c0474d58SKonstantin Porotchkin 		if (IS_DRAM_TARGET(dram_win->target_id)) {
322c0474d58SKonstantin Porotchkin 			dram_win->target_id = dram_target;
323c0474d58SKonstantin Porotchkin 			break;
324c0474d58SKonstantin Porotchkin 		}
325c0474d58SKonstantin Porotchkin 	}
326c0474d58SKonstantin Porotchkin 
327c0474d58SKonstantin Porotchkin 	/* Disable all AP CCU windows
328c0474d58SKonstantin Porotchkin 	 * Window-0 is always bypassed since it already contains
329c0474d58SKonstantin Porotchkin 	 * data allowing the internal configuration space access
330c0474d58SKonstantin Porotchkin 	 */
331c0474d58SKonstantin Porotchkin 	for (win_id = win_start; win_id < MVEBU_CCU_MAX_WINS; win_id++) {
332c0474d58SKonstantin Porotchkin 		ccu_disable_win(ap_index, win_id);
333c0474d58SKonstantin Porotchkin 		/* enable write secure (and clear read secure) */
334c0474d58SKonstantin Porotchkin 		mmio_write_32(CCU_WIN_SCR_OFFSET(ap_index, win_id),
335c0474d58SKonstantin Porotchkin 			      CCU_WIN_ENA_WRITE_SECURE);
336c0474d58SKonstantin Porotchkin 	}
337c0474d58SKonstantin Porotchkin 
338c0474d58SKonstantin Porotchkin 	/* win_id is the index of the current ccu window
339c0474d58SKonstantin Porotchkin 	 * array_id is the index of the current memory map window entry
340c0474d58SKonstantin Porotchkin 	 */
341c0474d58SKonstantin Porotchkin 	for (win_id = win_start, array_id = 0;
342c0474d58SKonstantin Porotchkin 	    ((win_id < MVEBU_CCU_MAX_WINS) && (array_id < win_count));
343c0474d58SKonstantin Porotchkin 	    win_id++) {
344c0474d58SKonstantin Porotchkin 		ccu_win_check(win);
345c0474d58SKonstantin Porotchkin 		ccu_enable_win(ap_index, win, win_id);
346c0474d58SKonstantin Porotchkin 		win++;
347c0474d58SKonstantin Porotchkin 		array_id++;
348c0474d58SKonstantin Porotchkin 	}
349c0474d58SKonstantin Porotchkin 
350c0474d58SKonstantin Porotchkin 	/* Get & set the default target according to board topology */
351c0474d58SKonstantin Porotchkin 	win_reg = (marvell_get_ccu_gcr_target(ap_index) & CCU_GCR_TARGET_MASK)
352c0474d58SKonstantin Porotchkin 		   << CCU_GCR_TARGET_OFFSET;
353c0474d58SKonstantin Porotchkin 	mmio_write_32(CCU_WIN_GCR_OFFSET(ap_index), win_reg);
354c0474d58SKonstantin Porotchkin 
355c0474d58SKonstantin Porotchkin #ifdef DEBUG_ADDR_MAP
356c0474d58SKonstantin Porotchkin 	dump_ccu(ap_index);
357c0474d58SKonstantin Porotchkin #endif
358c0474d58SKonstantin Porotchkin 
359c0474d58SKonstantin Porotchkin 	INFO("Done CCU Address decoding Initializing\n");
360c0474d58SKonstantin Porotchkin 
361c0474d58SKonstantin Porotchkin 	return 0;
362c0474d58SKonstantin Porotchkin }
363