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