xref: /openbmc/u-boot/arch/arm/mach-mvebu/mbus.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0
2d0787656SStefan Roese /*
3d0787656SStefan Roese  * Address map functions for Marvell EBU SoCs (Kirkwood, Armada
4d0787656SStefan Roese  * 370/XP, Dove, Orion5x and MV78xx0)
5d0787656SStefan Roese  *
6d0787656SStefan Roese  * Ported from the Barebox version to U-Boot by:
7d0787656SStefan Roese  * Stefan Roese <sr@denx.de>
8d0787656SStefan Roese  *
9d0787656SStefan Roese  * The Barebox version is:
10d0787656SStefan Roese  * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
11d0787656SStefan Roese  *
12d0787656SStefan Roese  * based on mbus driver from Linux
13d0787656SStefan Roese  *   (C) Copyright 2008 Marvell Semiconductor
14d0787656SStefan Roese  *
15d0787656SStefan Roese  * The Marvell EBU SoCs have a configurable physical address space:
16d0787656SStefan Roese  * the physical address at which certain devices (PCIe, NOR, NAND,
17d0787656SStefan Roese  * etc.) sit can be configured. The configuration takes place through
18d0787656SStefan Roese  * two sets of registers:
19d0787656SStefan Roese  *
20d0787656SStefan Roese  * - One to configure the access of the CPU to the devices. Depending
21d0787656SStefan Roese  *   on the families, there are between 8 and 20 configurable windows,
22d0787656SStefan Roese  *   each can be use to create a physical memory window that maps to a
23d0787656SStefan Roese  *   specific device. Devices are identified by a tuple (target,
24d0787656SStefan Roese  *   attribute).
25d0787656SStefan Roese  *
26d0787656SStefan Roese  * - One to configure the access to the CPU to the SDRAM. There are
27d0787656SStefan Roese  *   either 2 (for Dove) or 4 (for other families) windows to map the
28d0787656SStefan Roese  *   SDRAM into the physical address space.
29d0787656SStefan Roese  *
30d0787656SStefan Roese  * This driver:
31d0787656SStefan Roese  *
32d0787656SStefan Roese  * - Reads out the SDRAM address decoding windows at initialization
33d0787656SStefan Roese  *   time, and fills the mbus_dram_info structure with these
34d0787656SStefan Roese  *   informations. The exported function mv_mbus_dram_info() allow
35d0787656SStefan Roese  *   device drivers to get those informations related to the SDRAM
36d0787656SStefan Roese  *   address decoding windows. This is because devices also have their
37d0787656SStefan Roese  *   own windows (configured through registers that are part of each
38d0787656SStefan Roese  *   device register space), and therefore the drivers for Marvell
39d0787656SStefan Roese  *   devices have to configure those device -> SDRAM windows to ensure
40d0787656SStefan Roese  *   that DMA works properly.
41d0787656SStefan Roese  *
42d0787656SStefan Roese  * - Provides an API for platform code or device drivers to
43d0787656SStefan Roese  *   dynamically add or remove address decoding windows for the CPU ->
44d0787656SStefan Roese  *   device accesses. This API is mvebu_mbus_add_window_by_id(),
45d0787656SStefan Roese  *   mvebu_mbus_add_window_remap_by_id() and
46d0787656SStefan Roese  *   mvebu_mbus_del_window().
47d0787656SStefan Roese  */
48d0787656SStefan Roese 
49d0787656SStefan Roese #include <common.h>
501221ce45SMasahiro Yamada #include <linux/errno.h>
51d0787656SStefan Roese #include <asm/io.h>
52d0787656SStefan Roese #include <asm/arch/cpu.h>
53d0787656SStefan Roese #include <asm/arch/soc.h>
54f8fdb81fSFabio Estevam #include <linux/log2.h>
55d0787656SStefan Roese #include <linux/mbus.h>
56d0787656SStefan Roese 
57d0787656SStefan Roese /* DDR target is the same on all platforms */
58d0787656SStefan Roese #define TARGET_DDR		0
59d0787656SStefan Roese 
60d0787656SStefan Roese /* CPU Address Decode Windows registers */
61d0787656SStefan Roese #define WIN_CTRL_OFF		0x0000
62d0787656SStefan Roese #define   WIN_CTRL_ENABLE       BIT(0)
63d0787656SStefan Roese #define   WIN_CTRL_TGT_MASK     0xf0
64d0787656SStefan Roese #define   WIN_CTRL_TGT_SHIFT    4
65d0787656SStefan Roese #define   WIN_CTRL_ATTR_MASK    0xff00
66d0787656SStefan Roese #define   WIN_CTRL_ATTR_SHIFT   8
67d0787656SStefan Roese #define   WIN_CTRL_SIZE_MASK    0xffff0000
68d0787656SStefan Roese #define   WIN_CTRL_SIZE_SHIFT   16
69d0787656SStefan Roese #define WIN_BASE_OFF		0x0004
70d0787656SStefan Roese #define   WIN_BASE_LOW          0xffff0000
71d0787656SStefan Roese #define   WIN_BASE_HIGH         0xf
72d0787656SStefan Roese #define WIN_REMAP_LO_OFF	0x0008
73d0787656SStefan Roese #define   WIN_REMAP_LOW         0xffff0000
74d0787656SStefan Roese #define WIN_REMAP_HI_OFF	0x000c
75d0787656SStefan Roese 
76d0787656SStefan Roese #define ATTR_HW_COHERENCY	(0x1 << 4)
77d0787656SStefan Roese 
78d0787656SStefan Roese #define DDR_BASE_CS_OFF(n)	(0x0000 + ((n) << 3))
79d0787656SStefan Roese #define  DDR_BASE_CS_HIGH_MASK  0xf
80d0787656SStefan Roese #define  DDR_BASE_CS_LOW_MASK   0xff000000
81d0787656SStefan Roese #define DDR_SIZE_CS_OFF(n)	(0x0004 + ((n) << 3))
82d0787656SStefan Roese #define  DDR_SIZE_ENABLED       BIT(0)
83d0787656SStefan Roese #define  DDR_SIZE_CS_MASK       0x1c
84d0787656SStefan Roese #define  DDR_SIZE_CS_SHIFT      2
85d0787656SStefan Roese #define  DDR_SIZE_MASK          0xff000000
86d0787656SStefan Roese 
87d0787656SStefan Roese #define DOVE_DDR_BASE_CS_OFF(n) ((n) << 4)
88d0787656SStefan Roese 
89d0787656SStefan Roese struct mvebu_mbus_state;
90d0787656SStefan Roese 
91d0787656SStefan Roese struct mvebu_mbus_soc_data {
92d0787656SStefan Roese 	unsigned int num_wins;
93d0787656SStefan Roese 	unsigned int num_remappable_wins;
94d0787656SStefan Roese 	unsigned int (*win_cfg_offset)(const int win);
95d0787656SStefan Roese 	void (*setup_cpu_target)(struct mvebu_mbus_state *s);
96d0787656SStefan Roese };
97d0787656SStefan Roese 
98d0787656SStefan Roese struct mvebu_mbus_state mbus_state
99d0787656SStefan Roese 	__attribute__ ((section(".data")));
100d0787656SStefan Roese static struct mbus_dram_target_info mbus_dram_info
101d0787656SStefan Roese 	__attribute__ ((section(".data")));
102d0787656SStefan Roese 
103d0787656SStefan Roese /*
104d0787656SStefan Roese  * Functions to manipulate the address decoding windows
105d0787656SStefan Roese  */
106d0787656SStefan Roese 
mvebu_mbus_read_window(struct mvebu_mbus_state * mbus,int win,int * enabled,u64 * base,u32 * size,u8 * target,u8 * attr,u64 * remap)107d0787656SStefan Roese static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
108d0787656SStefan Roese 				   int win, int *enabled, u64 *base,
109d0787656SStefan Roese 				   u32 *size, u8 *target, u8 *attr,
110d0787656SStefan Roese 				   u64 *remap)
111d0787656SStefan Roese {
112d0787656SStefan Roese 	void __iomem *addr = mbus->mbuswins_base +
113d0787656SStefan Roese 		mbus->soc->win_cfg_offset(win);
114d0787656SStefan Roese 	u32 basereg = readl(addr + WIN_BASE_OFF);
115d0787656SStefan Roese 	u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
116d0787656SStefan Roese 
117d0787656SStefan Roese 	if (!(ctrlreg & WIN_CTRL_ENABLE)) {
118d0787656SStefan Roese 		*enabled = 0;
119d0787656SStefan Roese 		return;
120d0787656SStefan Roese 	}
121d0787656SStefan Roese 
122d0787656SStefan Roese 	*enabled = 1;
123d0787656SStefan Roese 	*base = ((u64)basereg & WIN_BASE_HIGH) << 32;
124d0787656SStefan Roese 	*base |= (basereg & WIN_BASE_LOW);
125d0787656SStefan Roese 	*size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
126d0787656SStefan Roese 
127d0787656SStefan Roese 	if (target)
128d0787656SStefan Roese 		*target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
129d0787656SStefan Roese 
130d0787656SStefan Roese 	if (attr)
131d0787656SStefan Roese 		*attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
132d0787656SStefan Roese 
133d0787656SStefan Roese 	if (remap) {
134d0787656SStefan Roese 		if (win < mbus->soc->num_remappable_wins) {
135d0787656SStefan Roese 			u32 remap_low = readl(addr + WIN_REMAP_LO_OFF);
136d0787656SStefan Roese 			u32 remap_hi  = readl(addr + WIN_REMAP_HI_OFF);
137d0787656SStefan Roese 			*remap = ((u64)remap_hi << 32) | remap_low;
138d0787656SStefan Roese 		} else {
139d0787656SStefan Roese 			*remap = 0;
140d0787656SStefan Roese 		}
141d0787656SStefan Roese 	}
142d0787656SStefan Roese }
143d0787656SStefan Roese 
mvebu_mbus_disable_window(struct mvebu_mbus_state * mbus,int win)144d0787656SStefan Roese static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
145d0787656SStefan Roese 				      int win)
146d0787656SStefan Roese {
147d0787656SStefan Roese 	void __iomem *addr;
148d0787656SStefan Roese 
149d0787656SStefan Roese 	addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
150d0787656SStefan Roese 
151d0787656SStefan Roese 	writel(0, addr + WIN_BASE_OFF);
152d0787656SStefan Roese 	writel(0, addr + WIN_CTRL_OFF);
153d0787656SStefan Roese 	if (win < mbus->soc->num_remappable_wins) {
154d0787656SStefan Roese 		writel(0, addr + WIN_REMAP_LO_OFF);
155d0787656SStefan Roese 		writel(0, addr + WIN_REMAP_HI_OFF);
156d0787656SStefan Roese 	}
157d0787656SStefan Roese }
158d0787656SStefan Roese 
159d0787656SStefan Roese /* Checks whether the given window number is available */
mvebu_mbus_window_is_free(struct mvebu_mbus_state * mbus,const int win)160d0787656SStefan Roese static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
161d0787656SStefan Roese 				     const int win)
162d0787656SStefan Roese {
163d0787656SStefan Roese 	void __iomem *addr = mbus->mbuswins_base +
164d0787656SStefan Roese 		mbus->soc->win_cfg_offset(win);
165d0787656SStefan Roese 	u32 ctrl = readl(addr + WIN_CTRL_OFF);
166d0787656SStefan Roese 	return !(ctrl & WIN_CTRL_ENABLE);
167d0787656SStefan Roese }
168d0787656SStefan Roese 
169d0787656SStefan Roese /*
170d0787656SStefan Roese  * Checks whether the given (base, base+size) area doesn't overlap an
171d0787656SStefan Roese  * existing region
172d0787656SStefan Roese  */
mvebu_mbus_window_conflicts(struct mvebu_mbus_state * mbus,phys_addr_t base,size_t size,u8 target,u8 attr)173d0787656SStefan Roese static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
174d0787656SStefan Roese 				       phys_addr_t base, size_t size,
175d0787656SStefan Roese 				       u8 target, u8 attr)
176d0787656SStefan Roese {
177d0787656SStefan Roese 	u64 end = (u64)base + size;
178d0787656SStefan Roese 	int win;
179d0787656SStefan Roese 
180d0787656SStefan Roese 	for (win = 0; win < mbus->soc->num_wins; win++) {
181d0787656SStefan Roese 		u64 wbase, wend;
182d0787656SStefan Roese 		u32 wsize;
183d0787656SStefan Roese 		u8 wtarget, wattr;
184d0787656SStefan Roese 		int enabled;
185d0787656SStefan Roese 
186d0787656SStefan Roese 		mvebu_mbus_read_window(mbus, win,
187d0787656SStefan Roese 				       &enabled, &wbase, &wsize,
188d0787656SStefan Roese 				       &wtarget, &wattr, NULL);
189d0787656SStefan Roese 
190d0787656SStefan Roese 		if (!enabled)
191d0787656SStefan Roese 			continue;
192d0787656SStefan Roese 
193d0787656SStefan Roese 		wend = wbase + wsize;
194d0787656SStefan Roese 
195d0787656SStefan Roese 		/*
196d0787656SStefan Roese 		 * Check if the current window overlaps with the
197d0787656SStefan Roese 		 * proposed physical range
198d0787656SStefan Roese 		 */
199d0787656SStefan Roese 		if ((u64)base < wend && end > wbase)
200d0787656SStefan Roese 			return 0;
201d0787656SStefan Roese 
202d0787656SStefan Roese 		/*
203d0787656SStefan Roese 		 * Check if target/attribute conflicts
204d0787656SStefan Roese 		 */
205d0787656SStefan Roese 		if (target == wtarget && attr == wattr)
206d0787656SStefan Roese 			return 0;
207d0787656SStefan Roese 	}
208d0787656SStefan Roese 
209d0787656SStefan Roese 	return 1;
210d0787656SStefan Roese }
211d0787656SStefan Roese 
mvebu_mbus_find_window(struct mvebu_mbus_state * mbus,phys_addr_t base,size_t size)212d0787656SStefan Roese static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
213d0787656SStefan Roese 				  phys_addr_t base, size_t size)
214d0787656SStefan Roese {
215d0787656SStefan Roese 	int win;
216d0787656SStefan Roese 
217d0787656SStefan Roese 	for (win = 0; win < mbus->soc->num_wins; win++) {
218d0787656SStefan Roese 		u64 wbase;
219d0787656SStefan Roese 		u32 wsize;
220d0787656SStefan Roese 		int enabled;
221d0787656SStefan Roese 
222d0787656SStefan Roese 		mvebu_mbus_read_window(mbus, win,
223d0787656SStefan Roese 				       &enabled, &wbase, &wsize,
224d0787656SStefan Roese 				       NULL, NULL, NULL);
225d0787656SStefan Roese 
226d0787656SStefan Roese 		if (!enabled)
227d0787656SStefan Roese 			continue;
228d0787656SStefan Roese 
229d0787656SStefan Roese 		if (base == wbase && size == wsize)
230d0787656SStefan Roese 			return win;
231d0787656SStefan Roese 	}
232d0787656SStefan Roese 
233d0787656SStefan Roese 	return -ENODEV;
234d0787656SStefan Roese }
235d0787656SStefan Roese 
mvebu_mbus_setup_window(struct mvebu_mbus_state * mbus,int win,phys_addr_t base,size_t size,phys_addr_t remap,u8 target,u8 attr)236d0787656SStefan Roese static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
237d0787656SStefan Roese 				   int win, phys_addr_t base, size_t size,
238d0787656SStefan Roese 				   phys_addr_t remap, u8 target,
239d0787656SStefan Roese 				   u8 attr)
240d0787656SStefan Roese {
241d0787656SStefan Roese 	void __iomem *addr = mbus->mbuswins_base +
242d0787656SStefan Roese 		mbus->soc->win_cfg_offset(win);
243d0787656SStefan Roese 	u32 ctrl, remap_addr;
244d0787656SStefan Roese 
245d0787656SStefan Roese 	ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
246d0787656SStefan Roese 		(attr << WIN_CTRL_ATTR_SHIFT)    |
247d0787656SStefan Roese 		(target << WIN_CTRL_TGT_SHIFT)   |
248d0787656SStefan Roese 		WIN_CTRL_ENABLE;
249d0787656SStefan Roese 
250d0787656SStefan Roese 	writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
251d0787656SStefan Roese 	writel(ctrl, addr + WIN_CTRL_OFF);
252d0787656SStefan Roese 	if (win < mbus->soc->num_remappable_wins) {
253d0787656SStefan Roese 		if (remap == MVEBU_MBUS_NO_REMAP)
254d0787656SStefan Roese 			remap_addr = base;
255d0787656SStefan Roese 		else
256d0787656SStefan Roese 			remap_addr = remap;
257d0787656SStefan Roese 		writel(remap_addr & WIN_REMAP_LOW, addr + WIN_REMAP_LO_OFF);
258d0787656SStefan Roese 		writel(0, addr + WIN_REMAP_HI_OFF);
259d0787656SStefan Roese 	}
260d0787656SStefan Roese 
261d0787656SStefan Roese 	return 0;
262d0787656SStefan Roese }
263d0787656SStefan Roese 
mvebu_mbus_alloc_window(struct mvebu_mbus_state * mbus,phys_addr_t base,size_t size,phys_addr_t remap,u8 target,u8 attr)264d0787656SStefan Roese static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
265d0787656SStefan Roese 				   phys_addr_t base, size_t size,
266d0787656SStefan Roese 				   phys_addr_t remap, u8 target,
267d0787656SStefan Roese 				   u8 attr)
268d0787656SStefan Roese {
269d0787656SStefan Roese 	int win;
270d0787656SStefan Roese 
271d0787656SStefan Roese 	if (remap == MVEBU_MBUS_NO_REMAP) {
272d0787656SStefan Roese 		for (win = mbus->soc->num_remappable_wins;
273d0787656SStefan Roese 		     win < mbus->soc->num_wins; win++)
274d0787656SStefan Roese 			if (mvebu_mbus_window_is_free(mbus, win))
275d0787656SStefan Roese 				return mvebu_mbus_setup_window(mbus, win, base,
276d0787656SStefan Roese 							       size, remap,
277d0787656SStefan Roese 							       target, attr);
278d0787656SStefan Roese 	}
279d0787656SStefan Roese 
280d0787656SStefan Roese 
281d0787656SStefan Roese 	for (win = 0; win < mbus->soc->num_wins; win++)
282d0787656SStefan Roese 		if (mvebu_mbus_window_is_free(mbus, win))
283d0787656SStefan Roese 			return mvebu_mbus_setup_window(mbus, win, base, size,
284d0787656SStefan Roese 						       remap, target, attr);
285d0787656SStefan Roese 
286d0787656SStefan Roese 	return -ENOMEM;
287d0787656SStefan Roese }
288d0787656SStefan Roese 
289d0787656SStefan Roese /*
290d0787656SStefan Roese  * SoC-specific functions and definitions
291d0787656SStefan Roese  */
292d0787656SStefan Roese 
armada_370_xp_mbus_win_offset(int win)293d0787656SStefan Roese static unsigned int armada_370_xp_mbus_win_offset(int win)
294d0787656SStefan Roese {
295d0787656SStefan Roese 	/* The register layout is a bit annoying and the below code
296d0787656SStefan Roese 	 * tries to cope with it.
297d0787656SStefan Roese 	 * - At offset 0x0, there are the registers for the first 8
298d0787656SStefan Roese 	 *   windows, with 4 registers of 32 bits per window (ctrl,
299d0787656SStefan Roese 	 *   base, remap low, remap high)
300d0787656SStefan Roese 	 * - Then at offset 0x80, there is a hole of 0x10 bytes for
301d0787656SStefan Roese 	 *   the internal registers base address and internal units
302d0787656SStefan Roese 	 *   sync barrier register.
303d0787656SStefan Roese 	 * - Then at offset 0x90, there the registers for 12
304d0787656SStefan Roese 	 *   windows, with only 2 registers of 32 bits per window
305d0787656SStefan Roese 	 *   (ctrl, base).
306d0787656SStefan Roese 	 */
307d0787656SStefan Roese 	if (win < 8)
308d0787656SStefan Roese 		return win << 4;
309d0787656SStefan Roese 	else
310d0787656SStefan Roese 		return 0x90 + ((win - 8) << 3);
311d0787656SStefan Roese }
312d0787656SStefan Roese 
orion5x_mbus_win_offset(int win)313d0787656SStefan Roese static unsigned int orion5x_mbus_win_offset(int win)
314d0787656SStefan Roese {
315d0787656SStefan Roese 	return win << 4;
316d0787656SStefan Roese }
317d0787656SStefan Roese 
mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state * mbus)318d0787656SStefan Roese static void mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
319d0787656SStefan Roese {
320d0787656SStefan Roese 	int i;
321d0787656SStefan Roese 	int cs;
322d0787656SStefan Roese 
323d0787656SStefan Roese 	mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
324d0787656SStefan Roese 
325d0787656SStefan Roese 	for (i = 0, cs = 0; i < 4; i++) {
326d0787656SStefan Roese 		u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
327d0787656SStefan Roese 		u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
328d0787656SStefan Roese 
329d0787656SStefan Roese 		/*
330d0787656SStefan Roese 		 * We only take care of entries for which the chip
331d0787656SStefan Roese 		 * select is enabled, and that don't have high base
332d0787656SStefan Roese 		 * address bits set (devices can only access the first
333d0787656SStefan Roese 		 * 32 bits of the memory).
334d0787656SStefan Roese 		 */
335d0787656SStefan Roese 		if ((size & DDR_SIZE_ENABLED) &&
336d0787656SStefan Roese 		    !(base & DDR_BASE_CS_HIGH_MASK)) {
337d0787656SStefan Roese 			struct mbus_dram_window *w;
338d0787656SStefan Roese 
339d0787656SStefan Roese 			w = &mbus_dram_info.cs[cs++];
340d0787656SStefan Roese 			w->cs_index = i;
341d0787656SStefan Roese 			w->mbus_attr = 0xf & ~(1 << i);
342d0787656SStefan Roese 			w->base = base & DDR_BASE_CS_LOW_MASK;
343d0787656SStefan Roese 			w->size = (size | ~DDR_SIZE_MASK) + 1;
344d0787656SStefan Roese 		}
345d0787656SStefan Roese 	}
346d0787656SStefan Roese 	mbus_dram_info.num_cs = cs;
347d0787656SStefan Roese }
348d0787656SStefan Roese 
349d0787656SStefan Roese static const struct mvebu_mbus_soc_data
350d0787656SStefan Roese armada_370_xp_mbus_data __maybe_unused = {
351d0787656SStefan Roese 	.num_wins            = 20,
352d0787656SStefan Roese 	.num_remappable_wins = 8,
353d0787656SStefan Roese 	.win_cfg_offset      = armada_370_xp_mbus_win_offset,
354d0787656SStefan Roese 	.setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
355d0787656SStefan Roese };
356d0787656SStefan Roese 
357d0787656SStefan Roese static const struct mvebu_mbus_soc_data
358d0787656SStefan Roese kirkwood_mbus_data __maybe_unused = {
359d0787656SStefan Roese 	.num_wins            = 8,
360d0787656SStefan Roese 	.num_remappable_wins = 4,
361d0787656SStefan Roese 	.win_cfg_offset      = orion5x_mbus_win_offset,
362d0787656SStefan Roese 	.setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
363d0787656SStefan Roese };
364d0787656SStefan Roese 
365d0787656SStefan Roese /*
366d0787656SStefan Roese  * Public API of the driver
367d0787656SStefan Roese  */
mvebu_mbus_dram_info(void)368d0787656SStefan Roese const struct mbus_dram_target_info *mvebu_mbus_dram_info(void)
369d0787656SStefan Roese {
370d0787656SStefan Roese 	return &mbus_dram_info;
371d0787656SStefan Roese }
372d0787656SStefan Roese 
mvebu_mbus_add_window_remap_by_id(unsigned int target,unsigned int attribute,phys_addr_t base,size_t size,phys_addr_t remap)373d0787656SStefan Roese int mvebu_mbus_add_window_remap_by_id(unsigned int target,
374d0787656SStefan Roese 				      unsigned int attribute,
375d0787656SStefan Roese 				      phys_addr_t base, size_t size,
376d0787656SStefan Roese 				      phys_addr_t remap)
377d0787656SStefan Roese {
378d0787656SStefan Roese 	struct mvebu_mbus_state *s = &mbus_state;
379d0787656SStefan Roese 
380d0787656SStefan Roese 	if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
381d0787656SStefan Roese 		printf("Cannot add window '%x:%x', conflicts with another window\n",
382d0787656SStefan Roese 		       target, attribute);
383d0787656SStefan Roese 		return -EINVAL;
384d0787656SStefan Roese 	}
385d0787656SStefan Roese 
386d0787656SStefan Roese 	return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
387d0787656SStefan Roese }
388d0787656SStefan Roese 
mvebu_mbus_add_window_by_id(unsigned int target,unsigned int attribute,phys_addr_t base,size_t size)389d0787656SStefan Roese int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
390d0787656SStefan Roese 				phys_addr_t base, size_t size)
391d0787656SStefan Roese {
392d0787656SStefan Roese 	return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
393d0787656SStefan Roese 						 size, MVEBU_MBUS_NO_REMAP);
394d0787656SStefan Roese }
395d0787656SStefan Roese 
mvebu_mbus_del_window(phys_addr_t base,size_t size)396d0787656SStefan Roese int mvebu_mbus_del_window(phys_addr_t base, size_t size)
397d0787656SStefan Roese {
398d0787656SStefan Roese 	int win;
399d0787656SStefan Roese 
400d0787656SStefan Roese 	win = mvebu_mbus_find_window(&mbus_state, base, size);
401d0787656SStefan Roese 	if (win < 0)
402d0787656SStefan Roese 		return win;
403d0787656SStefan Roese 
404d0787656SStefan Roese 	mvebu_mbus_disable_window(&mbus_state, win);
405d0787656SStefan Roese 	return 0;
406d0787656SStefan Roese }
407d0787656SStefan Roese 
mvebu_mbus_get_lowest_base(struct mvebu_mbus_state * mbus,phys_addr_t * base)4085b72dbfcSStefan Roese static void mvebu_mbus_get_lowest_base(struct mvebu_mbus_state *mbus,
4095b72dbfcSStefan Roese 				       phys_addr_t *base)
4105b72dbfcSStefan Roese {
4115b72dbfcSStefan Roese 	int win;
4125b72dbfcSStefan Roese 	*base = 0xffffffff;
4135b72dbfcSStefan Roese 
4145b72dbfcSStefan Roese 	for (win = 0; win < mbus->soc->num_wins; win++) {
4155b72dbfcSStefan Roese 		u64 wbase;
4165b72dbfcSStefan Roese 		u32 wsize;
4175b72dbfcSStefan Roese 		u8 wtarget, wattr;
4185b72dbfcSStefan Roese 		int enabled;
4195b72dbfcSStefan Roese 
4205b72dbfcSStefan Roese 		mvebu_mbus_read_window(mbus, win,
4215b72dbfcSStefan Roese 				       &enabled, &wbase, &wsize,
4225b72dbfcSStefan Roese 				       &wtarget, &wattr, NULL);
4235b72dbfcSStefan Roese 
4245b72dbfcSStefan Roese 		if (!enabled)
4255b72dbfcSStefan Roese 			continue;
4265b72dbfcSStefan Roese 
4275b72dbfcSStefan Roese 		if (wbase < *base)
4285b72dbfcSStefan Roese 			*base = wbase;
4295b72dbfcSStefan Roese 	}
4305b72dbfcSStefan Roese }
4315b72dbfcSStefan Roese 
mvebu_config_mbus_bridge(struct mvebu_mbus_state * mbus)4325b72dbfcSStefan Roese static void mvebu_config_mbus_bridge(struct mvebu_mbus_state *mbus)
4335b72dbfcSStefan Roese {
4345b72dbfcSStefan Roese 	phys_addr_t base;
4355b72dbfcSStefan Roese 	u32 val;
4365b72dbfcSStefan Roese 	u32 size;
4375b72dbfcSStefan Roese 
4385b72dbfcSStefan Roese 	/* Set MBUS bridge base/ctrl */
4395b72dbfcSStefan Roese 	mvebu_mbus_get_lowest_base(&mbus_state, &base);
4405b72dbfcSStefan Roese 
4415b72dbfcSStefan Roese 	size = 0xffffffff - base + 1;
4425b72dbfcSStefan Roese 	if (!is_power_of_2(size)) {
4435b72dbfcSStefan Roese 		/* Round up to next power of 2 */
4445b72dbfcSStefan Roese 		size = 1 << (ffs(base) + 1);
4455b72dbfcSStefan Roese 		base = 0xffffffff - size + 1;
4465b72dbfcSStefan Roese 	}
4475b72dbfcSStefan Roese 
4485b72dbfcSStefan Roese 	/* Now write base and size */
4495b72dbfcSStefan Roese 	writel(base, MBUS_BRIDGE_WIN_BASE_REG);
4505b72dbfcSStefan Roese 	/* Align window size to 64KiB */
4515b72dbfcSStefan Roese 	val = (size / (64 << 10)) - 1;
4525b72dbfcSStefan Roese 	writel((val << 16) | 0x1, MBUS_BRIDGE_WIN_CTRL_REG);
4535b72dbfcSStefan Roese }
4545b72dbfcSStefan Roese 
mbus_dt_setup_win(struct mvebu_mbus_state * mbus,u32 base,u32 size,u8 target,u8 attr)455d0787656SStefan Roese int mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
456d0787656SStefan Roese 		      u32 base, u32 size, u8 target, u8 attr)
457d0787656SStefan Roese {
458d0787656SStefan Roese 	if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
459d0787656SStefan Roese 		printf("Cannot add window '%04x:%04x', conflicts with another window\n",
460d0787656SStefan Roese 		       target, attr);
461d0787656SStefan Roese 		return -EBUSY;
462d0787656SStefan Roese 	}
463d0787656SStefan Roese 
464d0787656SStefan Roese 	/*
465d0787656SStefan Roese 	 * In U-Boot we first try to add the mbus window to the remap windows.
466d0787656SStefan Roese 	 * If this fails, lets try to add the windows to the non-remap windows.
467d0787656SStefan Roese 	 */
468d0787656SStefan Roese 	if (mvebu_mbus_alloc_window(mbus, base, size, base, target, attr)) {
469d0787656SStefan Roese 		if (mvebu_mbus_alloc_window(mbus, base, size,
470d0787656SStefan Roese 					    MVEBU_MBUS_NO_REMAP, target, attr))
471d0787656SStefan Roese 			return -ENOMEM;
472d0787656SStefan Roese 	}
473d0787656SStefan Roese 
4745b72dbfcSStefan Roese 	/*
4755b72dbfcSStefan Roese 	 * Re-configure the mbus bridge registers each time this function
4765b72dbfcSStefan Roese 	 * is called. Since it may get called from the board code in
4775b72dbfcSStefan Roese 	 * later boot stages as well.
4785b72dbfcSStefan Roese 	 */
4795b72dbfcSStefan Roese 	mvebu_config_mbus_bridge(mbus);
4805b72dbfcSStefan Roese 
481d0787656SStefan Roese 	return 0;
482d0787656SStefan Roese }
483d0787656SStefan Roese 
mvebu_mbus_probe(struct mbus_win windows[],int count)484d0787656SStefan Roese int mvebu_mbus_probe(struct mbus_win windows[], int count)
485d0787656SStefan Roese {
486d0787656SStefan Roese 	int win;
487d0787656SStefan Roese 	int ret;
488d0787656SStefan Roese 	int i;
489d0787656SStefan Roese 
490d0787656SStefan Roese #if defined(CONFIG_KIRKWOOD)
491d0787656SStefan Roese 	mbus_state.soc = &kirkwood_mbus_data;
492d0787656SStefan Roese #endif
49381e33f4bSStefan Roese #if defined(CONFIG_ARCH_MVEBU)
494d0787656SStefan Roese 	mbus_state.soc = &armada_370_xp_mbus_data;
495d0787656SStefan Roese #endif
496d0787656SStefan Roese 
497d0787656SStefan Roese 	mbus_state.mbuswins_base = (void __iomem *)MVEBU_CPU_WIN_BASE;
498d0787656SStefan Roese 	mbus_state.sdramwins_base = (void __iomem *)MVEBU_SDRAM_BASE;
499d0787656SStefan Roese 
500d0787656SStefan Roese 	for (win = 0; win < mbus_state.soc->num_wins; win++)
501d0787656SStefan Roese 		mvebu_mbus_disable_window(&mbus_state, win);
502d0787656SStefan Roese 
503d0787656SStefan Roese 	mbus_state.soc->setup_cpu_target(&mbus_state);
504d0787656SStefan Roese 
505d0787656SStefan Roese 	/* Setup statically declared windows in the DT */
506d0787656SStefan Roese 	for (i = 0; i < count; i++) {
507d0787656SStefan Roese 		u32 base, size;
508d0787656SStefan Roese 		u8 target, attr;
509d0787656SStefan Roese 
510d0787656SStefan Roese 		target = windows[i].target;
511d0787656SStefan Roese 		attr = windows[i].attr;
512d0787656SStefan Roese 		base = windows[i].base;
513d0787656SStefan Roese 		size = windows[i].size;
514d0787656SStefan Roese 		ret = mbus_dt_setup_win(&mbus_state, base, size, target, attr);
515d0787656SStefan Roese 		if (ret < 0)
516d0787656SStefan Roese 			return ret;
517d0787656SStefan Roese 	}
518d0787656SStefan Roese 
519d0787656SStefan Roese 	return 0;
520d0787656SStefan Roese }
521