xref: /rk3399_ARM-atf/drivers/marvell/iob.c (revision 39b6cc66d670be41d6b51b644beb675f386a4240)
1c0474d58SKonstantin Porotchkin /*
2c0474d58SKonstantin Porotchkin  * Copyright (C) 2016 - 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 /* IOW unit device driver for Marvell CP110 and CP115 SoCs */
9c0474d58SKonstantin Porotchkin 
10c0474d58SKonstantin Porotchkin #include <a8k_common.h>
11c0474d58SKonstantin Porotchkin #include <arch_helpers.h>
12c0474d58SKonstantin Porotchkin #include <debug.h>
13c0474d58SKonstantin Porotchkin #include <iob.h>
14c0474d58SKonstantin Porotchkin #include <mmio.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 #define MVEBU_IOB_OFFSET		(0x190000)
23c0474d58SKonstantin Porotchkin #define MVEBU_IOB_MAX_WINS		16
24c0474d58SKonstantin Porotchkin 
25c0474d58SKonstantin Porotchkin /* common defines */
26c0474d58SKonstantin Porotchkin #define WIN_ENABLE_BIT			(0x1)
27c0474d58SKonstantin Porotchkin /* 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 IOB_WIN_ALIGNMENT		(0x100000)
31c0474d58SKonstantin Porotchkin 
32c0474d58SKonstantin Porotchkin /* IOB registers */
33c0474d58SKonstantin Porotchkin #define IOB_WIN_CR_OFFSET(win)		(iob_base + 0x0 + (0x20 * win))
34c0474d58SKonstantin Porotchkin #define IOB_TARGET_ID_OFFSET		(8)
35c0474d58SKonstantin Porotchkin #define IOB_TARGET_ID_MASK		(0xF)
36c0474d58SKonstantin Porotchkin 
37c0474d58SKonstantin Porotchkin #define IOB_WIN_SCR_OFFSET(win)		(iob_base + 0x4 + (0x20 * win))
38c0474d58SKonstantin Porotchkin #define IOB_WIN_ENA_CTRL_WRITE_SECURE	(0x1)
39c0474d58SKonstantin Porotchkin #define IOB_WIN_ENA_CTRL_READ_SECURE	(0x2)
40c0474d58SKonstantin Porotchkin #define IOB_WIN_ENA_WRITE_SECURE	(0x4)
41c0474d58SKonstantin Porotchkin #define IOB_WIN_ENA_READ_SECURE		(0x8)
42c0474d58SKonstantin Porotchkin 
43c0474d58SKonstantin Porotchkin #define IOB_WIN_ALR_OFFSET(win)		(iob_base + 0x8 + (0x20 * win))
44c0474d58SKonstantin Porotchkin #define IOB_WIN_AHR_OFFSET(win)		(iob_base + 0xC + (0x20 * win))
45c0474d58SKonstantin Porotchkin 
46c0474d58SKonstantin Porotchkin uintptr_t iob_base;
47c0474d58SKonstantin Porotchkin 
48c0474d58SKonstantin Porotchkin static void iob_win_check(struct addr_map_win *win, uint32_t win_num)
49c0474d58SKonstantin Porotchkin {
50c0474d58SKonstantin Porotchkin 	/* check if address is aligned to the size */
51c0474d58SKonstantin Porotchkin 	if (IS_NOT_ALIGN(win->base_addr, IOB_WIN_ALIGNMENT)) {
52c0474d58SKonstantin Porotchkin 		win->base_addr = ALIGN_UP(win->base_addr, IOB_WIN_ALIGNMENT);
53c0474d58SKonstantin Porotchkin 		ERROR("Window %d: base address unaligned to 0x%x\n",
54c0474d58SKonstantin Porotchkin 		      win_num, IOB_WIN_ALIGNMENT);
55*39b6cc66SAntonio Nino Diaz 		printf("Align up the base address to 0x%llx\n",
56c0474d58SKonstantin Porotchkin 		       win->base_addr);
57c0474d58SKonstantin Porotchkin 	}
58c0474d58SKonstantin Porotchkin 
59c0474d58SKonstantin Porotchkin 	/* size parameter validity check */
60c0474d58SKonstantin Porotchkin 	if (IS_NOT_ALIGN(win->win_size, IOB_WIN_ALIGNMENT)) {
61c0474d58SKonstantin Porotchkin 		win->win_size = ALIGN_UP(win->win_size, IOB_WIN_ALIGNMENT);
62c0474d58SKonstantin Porotchkin 		ERROR("Window %d: window size unaligned to 0x%x\n", win_num,
63c0474d58SKonstantin Porotchkin 		      IOB_WIN_ALIGNMENT);
64*39b6cc66SAntonio Nino Diaz 		printf("Aligning size to 0x%llx\n", win->win_size);
65c0474d58SKonstantin Porotchkin 	}
66c0474d58SKonstantin Porotchkin }
67c0474d58SKonstantin Porotchkin 
68c0474d58SKonstantin Porotchkin static void iob_enable_win(struct addr_map_win *win, uint32_t win_id)
69c0474d58SKonstantin Porotchkin {
70c0474d58SKonstantin Porotchkin 	uint32_t iob_win_reg;
71c0474d58SKonstantin Porotchkin 	uint32_t alr, ahr;
72c0474d58SKonstantin Porotchkin 	uint64_t end_addr;
73c0474d58SKonstantin Porotchkin 
74c0474d58SKonstantin Porotchkin 	end_addr = (win->base_addr + win->win_size - 1);
75c0474d58SKonstantin Porotchkin 	alr = (uint32_t)((win->base_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
76c0474d58SKonstantin Porotchkin 	ahr = (uint32_t)((end_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
77c0474d58SKonstantin Porotchkin 
78c0474d58SKonstantin Porotchkin 	mmio_write_32(IOB_WIN_ALR_OFFSET(win_id), alr);
79c0474d58SKonstantin Porotchkin 	mmio_write_32(IOB_WIN_AHR_OFFSET(win_id), ahr);
80c0474d58SKonstantin Porotchkin 
81c0474d58SKonstantin Porotchkin 	iob_win_reg = WIN_ENABLE_BIT;
82c0474d58SKonstantin Porotchkin 	iob_win_reg |= (win->target_id & IOB_TARGET_ID_MASK)
83c0474d58SKonstantin Porotchkin 		       << IOB_TARGET_ID_OFFSET;
84c0474d58SKonstantin Porotchkin 	mmio_write_32(IOB_WIN_CR_OFFSET(win_id), iob_win_reg);
85c0474d58SKonstantin Porotchkin 
86c0474d58SKonstantin Porotchkin }
87c0474d58SKonstantin Porotchkin 
88c0474d58SKonstantin Porotchkin #ifdef DEBUG_ADDR_MAP
89c0474d58SKonstantin Porotchkin static void dump_iob(void)
90c0474d58SKonstantin Porotchkin {
91c0474d58SKonstantin Porotchkin 	uint32_t win_id, win_cr, alr, ahr;
92c0474d58SKonstantin Porotchkin 	uint8_t target_id;
93c0474d58SKonstantin Porotchkin 	uint64_t start, end;
94c0474d58SKonstantin Porotchkin 	char *iob_target_name[IOB_MAX_TID] = {
95c0474d58SKonstantin Porotchkin 		"CFG  ", "MCI0 ", "PEX1 ", "PEX2 ",
96c0474d58SKonstantin Porotchkin 		"PEX0 ", "NAND ", "RUNIT", "MCI1 " };
97c0474d58SKonstantin Porotchkin 
98c0474d58SKonstantin Porotchkin 	/* Dump all IOB windows */
99*39b6cc66SAntonio Nino Diaz 	printf("bank  id target  start              end\n");
100*39b6cc66SAntonio Nino Diaz 	printf("----------------------------------------------------\n");
101c0474d58SKonstantin Porotchkin 	for (win_id = 0; win_id < MVEBU_IOB_MAX_WINS; win_id++) {
102c0474d58SKonstantin Porotchkin 		win_cr = mmio_read_32(IOB_WIN_CR_OFFSET(win_id));
103c0474d58SKonstantin Porotchkin 		if (win_cr & WIN_ENABLE_BIT) {
104c0474d58SKonstantin Porotchkin 			target_id = (win_cr >> IOB_TARGET_ID_OFFSET) &
105c0474d58SKonstantin Porotchkin 				     IOB_TARGET_ID_MASK;
106c0474d58SKonstantin Porotchkin 			alr = mmio_read_32(IOB_WIN_ALR_OFFSET(win_id));
107c0474d58SKonstantin Porotchkin 			start = ((uint64_t)alr << ADDRESS_SHIFT);
108c0474d58SKonstantin Porotchkin 			if (win_id != 0) {
109c0474d58SKonstantin Porotchkin 				ahr = mmio_read_32(IOB_WIN_AHR_OFFSET(win_id));
110c0474d58SKonstantin Porotchkin 				end = (((uint64_t)ahr + 0x10) << ADDRESS_SHIFT);
111c0474d58SKonstantin Porotchkin 			} else {
112c0474d58SKonstantin Porotchkin 				/* Window #0 size is hardcoded to 16MB, as it's
113c0474d58SKonstantin Porotchkin 				 * reserved for CP configuration space.
114c0474d58SKonstantin Porotchkin 				 */
115c0474d58SKonstantin Porotchkin 				end = start + (16 << 20);
116c0474d58SKonstantin Porotchkin 			}
117*39b6cc66SAntonio Nino Diaz 			printf("iob   %02d %s   0x%016llx 0x%016llx\n",
118c0474d58SKonstantin Porotchkin 			       win_id, iob_target_name[target_id],
119c0474d58SKonstantin Porotchkin 			       start, end);
120c0474d58SKonstantin Porotchkin 		}
121c0474d58SKonstantin Porotchkin 	}
122c0474d58SKonstantin Porotchkin }
123c0474d58SKonstantin Porotchkin #endif
124c0474d58SKonstantin Porotchkin 
125c0474d58SKonstantin Porotchkin void iob_cfg_space_update(int ap_idx, int cp_idx, uintptr_t base,
126c0474d58SKonstantin Porotchkin 			  uintptr_t new_base)
127c0474d58SKonstantin Porotchkin {
128c0474d58SKonstantin Porotchkin 	debug_enter();
129c0474d58SKonstantin Porotchkin 
130c0474d58SKonstantin Porotchkin 	iob_base = base + MVEBU_IOB_OFFSET;
131c0474d58SKonstantin Porotchkin 
132c0474d58SKonstantin Porotchkin 	NOTICE("Change the base address of AP%d-CP%d to %lx\n",
133c0474d58SKonstantin Porotchkin 	       ap_idx, cp_idx, new_base);
134c0474d58SKonstantin Porotchkin 	mmio_write_32(IOB_WIN_ALR_OFFSET(0), new_base >> ADDRESS_SHIFT);
135c0474d58SKonstantin Porotchkin 
136c0474d58SKonstantin Porotchkin 	iob_base = new_base + MVEBU_IOB_OFFSET;
137c0474d58SKonstantin Porotchkin 
138c0474d58SKonstantin Porotchkin 	/* Make sure the address was configured by the CPU before
139c0474d58SKonstantin Porotchkin 	 * any possible access to the CP.
140c0474d58SKonstantin Porotchkin 	 */
141c0474d58SKonstantin Porotchkin 	dsb();
142c0474d58SKonstantin Porotchkin 
143c0474d58SKonstantin Porotchkin 	debug_exit();
144c0474d58SKonstantin Porotchkin }
145c0474d58SKonstantin Porotchkin 
146c0474d58SKonstantin Porotchkin int init_iob(uintptr_t base)
147c0474d58SKonstantin Porotchkin {
148c0474d58SKonstantin Porotchkin 	struct addr_map_win *win;
149c0474d58SKonstantin Porotchkin 	uint32_t win_id, win_reg;
150c0474d58SKonstantin Porotchkin 	uint32_t win_count;
151c0474d58SKonstantin Porotchkin 
152c0474d58SKonstantin Porotchkin 	INFO("Initializing IOB Address decoding\n");
153c0474d58SKonstantin Porotchkin 
154c0474d58SKonstantin Porotchkin 	/* Get the base address of the address decoding MBUS */
155c0474d58SKonstantin Porotchkin 	iob_base = base + MVEBU_IOB_OFFSET;
156c0474d58SKonstantin Porotchkin 
157c0474d58SKonstantin Porotchkin 	/* Get the array of the windows and fill the map data */
158c0474d58SKonstantin Porotchkin 	marvell_get_iob_memory_map(&win, &win_count, base);
159c0474d58SKonstantin Porotchkin 	if (win_count <= 0) {
160c0474d58SKonstantin Porotchkin 		INFO("no windows configurations found\n");
161c0474d58SKonstantin Porotchkin 		return 0;
162c0474d58SKonstantin Porotchkin 	} else if (win_count > (MVEBU_IOB_MAX_WINS - 1)) {
163c0474d58SKonstantin Porotchkin 		ERROR("IOB mem map array > than max available windows (%d)\n",
164c0474d58SKonstantin Porotchkin 		      MVEBU_IOB_MAX_WINS);
165c0474d58SKonstantin Porotchkin 		win_count = MVEBU_IOB_MAX_WINS;
166c0474d58SKonstantin Porotchkin 	}
167c0474d58SKonstantin Porotchkin 
168c0474d58SKonstantin Porotchkin 	/* disable all IOB windows, start from win_id = 1
169c0474d58SKonstantin Porotchkin 	 * because can't disable internal register window
170c0474d58SKonstantin Porotchkin 	 */
171c0474d58SKonstantin Porotchkin 	for (win_id = 1; win_id < MVEBU_IOB_MAX_WINS; win_id++) {
172c0474d58SKonstantin Porotchkin 		win_reg = mmio_read_32(IOB_WIN_CR_OFFSET(win_id));
173c0474d58SKonstantin Porotchkin 		win_reg &= ~WIN_ENABLE_BIT;
174c0474d58SKonstantin Porotchkin 		mmio_write_32(IOB_WIN_CR_OFFSET(win_id), win_reg);
175c0474d58SKonstantin Porotchkin 
176c0474d58SKonstantin Porotchkin 		win_reg = ~IOB_WIN_ENA_CTRL_WRITE_SECURE;
177c0474d58SKonstantin Porotchkin 		win_reg &= ~IOB_WIN_ENA_CTRL_READ_SECURE;
178c0474d58SKonstantin Porotchkin 		win_reg &= ~IOB_WIN_ENA_WRITE_SECURE;
179c0474d58SKonstantin Porotchkin 		win_reg &= ~IOB_WIN_ENA_READ_SECURE;
180c0474d58SKonstantin Porotchkin 		mmio_write_32(IOB_WIN_SCR_OFFSET(win_id), win_reg);
181c0474d58SKonstantin Porotchkin 	}
182c0474d58SKonstantin Porotchkin 
183c0474d58SKonstantin Porotchkin 	for (win_id = 1; win_id < win_count + 1; win_id++, win++) {
184c0474d58SKonstantin Porotchkin 		iob_win_check(win, win_id);
185c0474d58SKonstantin Porotchkin 		iob_enable_win(win, win_id);
186c0474d58SKonstantin Porotchkin 	}
187c0474d58SKonstantin Porotchkin 
188c0474d58SKonstantin Porotchkin #ifdef DEBUG_ADDR_MAP
189c0474d58SKonstantin Porotchkin 	dump_iob();
190c0474d58SKonstantin Porotchkin #endif
191c0474d58SKonstantin Porotchkin 
192c0474d58SKonstantin Porotchkin 	INFO("Done IOB Address decoding Initializing\n");
193c0474d58SKonstantin Porotchkin 
194c0474d58SKonstantin Porotchkin 	return 0;
195c0474d58SKonstantin Porotchkin }
196