xref: /openbmc/linux/drivers/bus/mvebu-mbus.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
13bb16560SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fddddb52SThomas Petazzoni /*
3fddddb52SThomas Petazzoni  * Address map functions for Marvell EBU SoCs (Kirkwood, Armada
4fddddb52SThomas Petazzoni  * 370/XP, Dove, Orion5x and MV78xx0)
5fddddb52SThomas Petazzoni  *
6fddddb52SThomas Petazzoni  * The Marvell EBU SoCs have a configurable physical address space:
7fddddb52SThomas Petazzoni  * the physical address at which certain devices (PCIe, NOR, NAND,
8fddddb52SThomas Petazzoni  * etc.) sit can be configured. The configuration takes place through
9fddddb52SThomas Petazzoni  * two sets of registers:
10fddddb52SThomas Petazzoni  *
11fddddb52SThomas Petazzoni  * - One to configure the access of the CPU to the devices. Depending
12fddddb52SThomas Petazzoni  *   on the families, there are between 8 and 20 configurable windows,
13fddddb52SThomas Petazzoni  *   each can be use to create a physical memory window that maps to a
14fddddb52SThomas Petazzoni  *   specific device. Devices are identified by a tuple (target,
15fddddb52SThomas Petazzoni  *   attribute).
16fddddb52SThomas Petazzoni  *
17fddddb52SThomas Petazzoni  * - One to configure the access to the CPU to the SDRAM. There are
18fddddb52SThomas Petazzoni  *   either 2 (for Dove) or 4 (for other families) windows to map the
19fddddb52SThomas Petazzoni  *   SDRAM into the physical address space.
20fddddb52SThomas Petazzoni  *
21fddddb52SThomas Petazzoni  * This driver:
22fddddb52SThomas Petazzoni  *
23fddddb52SThomas Petazzoni  * - Reads out the SDRAM address decoding windows at initialization
24fddddb52SThomas Petazzoni  *   time, and fills the mvebu_mbus_dram_info structure with these
2585df4646SZhang Jiaming  *   information. The exported function mv_mbus_dram_info() allow
2685df4646SZhang Jiaming  *   device drivers to get those information related to the SDRAM
27fddddb52SThomas Petazzoni  *   address decoding windows. This is because devices also have their
28fddddb52SThomas Petazzoni  *   own windows (configured through registers that are part of each
29fddddb52SThomas Petazzoni  *   device register space), and therefore the drivers for Marvell
30fddddb52SThomas Petazzoni  *   devices have to configure those device -> SDRAM windows to ensure
31fddddb52SThomas Petazzoni  *   that DMA works properly.
32fddddb52SThomas Petazzoni  *
33fddddb52SThomas Petazzoni  * - Provides an API for platform code or device drivers to
34fddddb52SThomas Petazzoni  *   dynamically add or remove address decoding windows for the CPU ->
356275afefSThomas Petazzoni  *   device accesses. This API is mvebu_mbus_add_window_by_id(),
366275afefSThomas Petazzoni  *   mvebu_mbus_add_window_remap_by_id() and
376275afefSThomas Petazzoni  *   mvebu_mbus_del_window().
38fddddb52SThomas Petazzoni  *
39fddddb52SThomas Petazzoni  * - Provides a debugfs interface in /sys/kernel/debug/mvebu-mbus/ to
40fddddb52SThomas Petazzoni  *   see the list of CPU -> SDRAM windows and their configuration
41fddddb52SThomas Petazzoni  *   (file 'sdram') and the list of CPU -> devices windows and their
42fddddb52SThomas Petazzoni  *   configuration (file 'devices').
43fddddb52SThomas Petazzoni  */
44fddddb52SThomas Petazzoni 
45b15d0b52SEzequiel Garcia #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46b15d0b52SEzequiel Garcia 
47fddddb52SThomas Petazzoni #include <linux/kernel.h>
48fddddb52SThomas Petazzoni #include <linux/module.h>
49fddddb52SThomas Petazzoni #include <linux/init.h>
50fddddb52SThomas Petazzoni #include <linux/mbus.h>
51fddddb52SThomas Petazzoni #include <linux/io.h>
52fddddb52SThomas Petazzoni #include <linux/ioport.h>
53fddddb52SThomas Petazzoni #include <linux/of.h>
54fddddb52SThomas Petazzoni #include <linux/of_address.h>
55fddddb52SThomas Petazzoni #include <linux/debugfs.h>
5609752a12SJason Gunthorpe #include <linux/log2.h>
57bfa1ce5fSThomas Petazzoni #include <linux/memblock.h>
58a0e89c02SThomas Petazzoni #include <linux/syscore_ops.h>
59fddddb52SThomas Petazzoni 
60fddddb52SThomas Petazzoni /*
61fddddb52SThomas Petazzoni  * DDR target is the same on all platforms.
62fddddb52SThomas Petazzoni  */
63fddddb52SThomas Petazzoni #define TARGET_DDR		0
64fddddb52SThomas Petazzoni 
65fddddb52SThomas Petazzoni /*
66fddddb52SThomas Petazzoni  * CPU Address Decode Windows registers
67fddddb52SThomas Petazzoni  */
68fddddb52SThomas Petazzoni #define WIN_CTRL_OFF		0x0000
69fddddb52SThomas Petazzoni #define   WIN_CTRL_ENABLE       BIT(0)
708c9e06e6SNicolas Schichan /* Only on HW I/O coherency capable platforms */
71a0b5cd4aSThomas Petazzoni #define   WIN_CTRL_SYNCBARRIER  BIT(1)
72fddddb52SThomas Petazzoni #define   WIN_CTRL_TGT_MASK     0xf0
73fddddb52SThomas Petazzoni #define   WIN_CTRL_TGT_SHIFT    4
74fddddb52SThomas Petazzoni #define   WIN_CTRL_ATTR_MASK    0xff00
75fddddb52SThomas Petazzoni #define   WIN_CTRL_ATTR_SHIFT   8
76fddddb52SThomas Petazzoni #define   WIN_CTRL_SIZE_MASK    0xffff0000
77fddddb52SThomas Petazzoni #define   WIN_CTRL_SIZE_SHIFT   16
78fddddb52SThomas Petazzoni #define WIN_BASE_OFF		0x0004
79fddddb52SThomas Petazzoni #define   WIN_BASE_LOW          0xffff0000
80fddddb52SThomas Petazzoni #define   WIN_BASE_HIGH         0xf
81fddddb52SThomas Petazzoni #define WIN_REMAP_LO_OFF	0x0008
82fddddb52SThomas Petazzoni #define   WIN_REMAP_LOW         0xffff0000
83fddddb52SThomas Petazzoni #define WIN_REMAP_HI_OFF	0x000c
84fddddb52SThomas Petazzoni 
85a0b5cd4aSThomas Petazzoni #define UNIT_SYNC_BARRIER_OFF   0x84
86a0b5cd4aSThomas Petazzoni #define   UNIT_SYNC_BARRIER_ALL 0xFFFF
87a0b5cd4aSThomas Petazzoni 
88fddddb52SThomas Petazzoni #define ATTR_HW_COHERENCY	(0x1 << 4)
89fddddb52SThomas Petazzoni 
90fddddb52SThomas Petazzoni #define DDR_BASE_CS_OFF(n)	(0x0000 + ((n) << 3))
91fddddb52SThomas Petazzoni #define  DDR_BASE_CS_HIGH_MASK  0xf
92fddddb52SThomas Petazzoni #define  DDR_BASE_CS_LOW_MASK   0xff000000
93fddddb52SThomas Petazzoni #define DDR_SIZE_CS_OFF(n)	(0x0004 + ((n) << 3))
94fddddb52SThomas Petazzoni #define  DDR_SIZE_ENABLED       BIT(0)
95fddddb52SThomas Petazzoni #define  DDR_SIZE_CS_MASK       0x1c
96fddddb52SThomas Petazzoni #define  DDR_SIZE_CS_SHIFT      2
97fddddb52SThomas Petazzoni #define  DDR_SIZE_MASK          0xff000000
98fddddb52SThomas Petazzoni 
99fddddb52SThomas Petazzoni #define DOVE_DDR_BASE_CS_OFF(n) ((n) << 4)
100fddddb52SThomas Petazzoni 
101a0e89c02SThomas Petazzoni /* Relative to mbusbridge_base */
102a0e89c02SThomas Petazzoni #define MBUS_BRIDGE_CTRL_OFF	0x0
103a0e89c02SThomas Petazzoni #define MBUS_BRIDGE_BASE_OFF	0x4
104a0e89c02SThomas Petazzoni 
105a0e89c02SThomas Petazzoni /* Maximum number of windows, for all known platforms */
106a0e89c02SThomas Petazzoni #define MBUS_WINS_MAX           20
107a0e89c02SThomas Petazzoni 
108fddddb52SThomas Petazzoni struct mvebu_mbus_state;
109fddddb52SThomas Petazzoni 
110fddddb52SThomas Petazzoni struct mvebu_mbus_soc_data {
111fddddb52SThomas Petazzoni 	unsigned int num_wins;
112a0e89c02SThomas Petazzoni 	bool has_mbus_bridge;
113fddddb52SThomas Petazzoni 	unsigned int (*win_cfg_offset)(const int win);
1147fdf3d8aSMichal Mazur 	unsigned int (*win_remap_offset)(const int win);
115fddddb52SThomas Petazzoni 	void (*setup_cpu_target)(struct mvebu_mbus_state *s);
1164749c02bSThomas Petazzoni 	int (*save_cpu_target)(struct mvebu_mbus_state *s,
117fce7b5aeSBen Dooks 			       u32 __iomem *store_addr);
118fddddb52SThomas Petazzoni 	int (*show_cpu_target)(struct mvebu_mbus_state *s,
119fddddb52SThomas Petazzoni 			       struct seq_file *seq, void *v);
120fddddb52SThomas Petazzoni };
121fddddb52SThomas Petazzoni 
122a0e89c02SThomas Petazzoni /*
12385df4646SZhang Jiaming  * Used to store the state of one MBus window across suspend/resume.
124a0e89c02SThomas Petazzoni  */
125a0e89c02SThomas Petazzoni struct mvebu_mbus_win_data {
126a0e89c02SThomas Petazzoni 	u32 ctrl;
127a0e89c02SThomas Petazzoni 	u32 base;
128a0e89c02SThomas Petazzoni 	u32 remap_lo;
129a0e89c02SThomas Petazzoni 	u32 remap_hi;
130a0e89c02SThomas Petazzoni };
131a0e89c02SThomas Petazzoni 
132fddddb52SThomas Petazzoni struct mvebu_mbus_state {
133fddddb52SThomas Petazzoni 	void __iomem *mbuswins_base;
134fddddb52SThomas Petazzoni 	void __iomem *sdramwins_base;
135a0e89c02SThomas Petazzoni 	void __iomem *mbusbridge_base;
1364749c02bSThomas Petazzoni 	phys_addr_t sdramwins_phys_base;
137fddddb52SThomas Petazzoni 	struct dentry *debugfs_root;
138fddddb52SThomas Petazzoni 	struct dentry *debugfs_sdram;
139fddddb52SThomas Petazzoni 	struct dentry *debugfs_devs;
14079d94683SEzequiel Garcia 	struct resource pcie_mem_aperture;
14179d94683SEzequiel Garcia 	struct resource pcie_io_aperture;
142fddddb52SThomas Petazzoni 	const struct mvebu_mbus_soc_data *soc;
143fddddb52SThomas Petazzoni 	int hw_io_coherency;
144a0e89c02SThomas Petazzoni 
145a0e89c02SThomas Petazzoni 	/* Used during suspend/resume */
146a0e89c02SThomas Petazzoni 	u32 mbus_bridge_ctrl;
147a0e89c02SThomas Petazzoni 	u32 mbus_bridge_base;
148a0e89c02SThomas Petazzoni 	struct mvebu_mbus_win_data wins[MBUS_WINS_MAX];
149fddddb52SThomas Petazzoni };
150fddddb52SThomas Petazzoni 
151fddddb52SThomas Petazzoni static struct mvebu_mbus_state mbus_state;
152fddddb52SThomas Petazzoni 
153bfa1ce5fSThomas Petazzoni /*
154bfa1ce5fSThomas Petazzoni  * We provide two variants of the mv_mbus_dram_info() function:
155bfa1ce5fSThomas Petazzoni  *
156bfa1ce5fSThomas Petazzoni  * - The normal one, where the described DRAM ranges may overlap with
157bfa1ce5fSThomas Petazzoni  *   the I/O windows, but for which the DRAM ranges are guaranteed to
158bfa1ce5fSThomas Petazzoni  *   have a power of two size. Such ranges are suitable for the DMA
159bfa1ce5fSThomas Petazzoni  *   masters that only DMA between the RAM and the device, which is
160bfa1ce5fSThomas Petazzoni  *   actually all devices except the crypto engines.
161bfa1ce5fSThomas Petazzoni  *
162bfa1ce5fSThomas Petazzoni  * - The 'nooverlap' one, where the described DRAM ranges are
163bfa1ce5fSThomas Petazzoni  *   guaranteed to not overlap with the I/O windows, but for which the
164bfa1ce5fSThomas Petazzoni  *   DRAM ranges will not have power of two sizes. They will only be
165bfa1ce5fSThomas Petazzoni  *   aligned on a 64 KB boundary, and have a size multiple of 64
166bfa1ce5fSThomas Petazzoni  *   KB. Such ranges are suitable for the DMA masters that DMA between
167bfa1ce5fSThomas Petazzoni  *   the crypto SRAM (which is mapped through an I/O window) and a
168bfa1ce5fSThomas Petazzoni  *   device. This is the case for the crypto engines.
169bfa1ce5fSThomas Petazzoni  */
170bfa1ce5fSThomas Petazzoni 
171fddddb52SThomas Petazzoni static struct mbus_dram_target_info mvebu_mbus_dram_info;
172bfa1ce5fSThomas Petazzoni static struct mbus_dram_target_info mvebu_mbus_dram_info_nooverlap;
173bfa1ce5fSThomas Petazzoni 
mv_mbus_dram_info(void)174fddddb52SThomas Petazzoni const struct mbus_dram_target_info *mv_mbus_dram_info(void)
175fddddb52SThomas Petazzoni {
176fddddb52SThomas Petazzoni 	return &mvebu_mbus_dram_info;
177fddddb52SThomas Petazzoni }
178fddddb52SThomas Petazzoni EXPORT_SYMBOL_GPL(mv_mbus_dram_info);
179fddddb52SThomas Petazzoni 
mv_mbus_dram_info_nooverlap(void)180bfa1ce5fSThomas Petazzoni const struct mbus_dram_target_info *mv_mbus_dram_info_nooverlap(void)
181bfa1ce5fSThomas Petazzoni {
182bfa1ce5fSThomas Petazzoni 	return &mvebu_mbus_dram_info_nooverlap;
183bfa1ce5fSThomas Petazzoni }
184bfa1ce5fSThomas Petazzoni EXPORT_SYMBOL_GPL(mv_mbus_dram_info_nooverlap);
185bfa1ce5fSThomas Petazzoni 
1867fdf3d8aSMichal Mazur /* Checks whether the given window has remap capability */
mvebu_mbus_window_is_remappable(struct mvebu_mbus_state * mbus,const int win)1877fdf3d8aSMichal Mazur static bool mvebu_mbus_window_is_remappable(struct mvebu_mbus_state *mbus,
1887fdf3d8aSMichal Mazur 					    const int win)
1897fdf3d8aSMichal Mazur {
1907fdf3d8aSMichal Mazur 	return mbus->soc->win_remap_offset(win) != MVEBU_MBUS_NO_REMAP;
1917fdf3d8aSMichal Mazur }
1927fdf3d8aSMichal Mazur 
193fddddb52SThomas Petazzoni /*
194fddddb52SThomas Petazzoni  * Functions to manipulate the address decoding windows
195fddddb52SThomas Petazzoni  */
196fddddb52SThomas Petazzoni 
mvebu_mbus_read_window(struct mvebu_mbus_state * mbus,int win,int * enabled,u64 * base,u32 * size,u8 * target,u8 * attr,u64 * remap)197fddddb52SThomas Petazzoni static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
198fddddb52SThomas Petazzoni 				   int win, int *enabled, u64 *base,
199fddddb52SThomas Petazzoni 				   u32 *size, u8 *target, u8 *attr,
200fddddb52SThomas Petazzoni 				   u64 *remap)
201fddddb52SThomas Petazzoni {
202fddddb52SThomas Petazzoni 	void __iomem *addr = mbus->mbuswins_base +
203fddddb52SThomas Petazzoni 		mbus->soc->win_cfg_offset(win);
204fddddb52SThomas Petazzoni 	u32 basereg = readl(addr + WIN_BASE_OFF);
205fddddb52SThomas Petazzoni 	u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
206fddddb52SThomas Petazzoni 
207fddddb52SThomas Petazzoni 	if (!(ctrlreg & WIN_CTRL_ENABLE)) {
208fddddb52SThomas Petazzoni 		*enabled = 0;
209fddddb52SThomas Petazzoni 		return;
210fddddb52SThomas Petazzoni 	}
211fddddb52SThomas Petazzoni 
212fddddb52SThomas Petazzoni 	*enabled = 1;
213fddddb52SThomas Petazzoni 	*base = ((u64)basereg & WIN_BASE_HIGH) << 32;
214fddddb52SThomas Petazzoni 	*base |= (basereg & WIN_BASE_LOW);
215fddddb52SThomas Petazzoni 	*size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
216fddddb52SThomas Petazzoni 
217fddddb52SThomas Petazzoni 	if (target)
218fddddb52SThomas Petazzoni 		*target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
219fddddb52SThomas Petazzoni 
220fddddb52SThomas Petazzoni 	if (attr)
221fddddb52SThomas Petazzoni 		*attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
222fddddb52SThomas Petazzoni 
223fddddb52SThomas Petazzoni 	if (remap) {
2247fdf3d8aSMichal Mazur 		if (mvebu_mbus_window_is_remappable(mbus, win)) {
2257fdf3d8aSMichal Mazur 			u32 remap_low, remap_hi;
2267fdf3d8aSMichal Mazur 			void __iomem *addr_rmp = mbus->mbuswins_base +
2277fdf3d8aSMichal Mazur 				mbus->soc->win_remap_offset(win);
2287fdf3d8aSMichal Mazur 			remap_low = readl(addr_rmp + WIN_REMAP_LO_OFF);
2297fdf3d8aSMichal Mazur 			remap_hi  = readl(addr_rmp + WIN_REMAP_HI_OFF);
230fddddb52SThomas Petazzoni 			*remap = ((u64)remap_hi << 32) | remap_low;
231fddddb52SThomas Petazzoni 		} else
232fddddb52SThomas Petazzoni 			*remap = 0;
233fddddb52SThomas Petazzoni 	}
234fddddb52SThomas Petazzoni }
235fddddb52SThomas Petazzoni 
mvebu_mbus_disable_window(struct mvebu_mbus_state * mbus,int win)236fddddb52SThomas Petazzoni static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
237fddddb52SThomas Petazzoni 				      int win)
238fddddb52SThomas Petazzoni {
239fddddb52SThomas Petazzoni 	void __iomem *addr;
240fddddb52SThomas Petazzoni 
241fddddb52SThomas Petazzoni 	addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
242fddddb52SThomas Petazzoni 	writel(0, addr + WIN_BASE_OFF);
243fddddb52SThomas Petazzoni 	writel(0, addr + WIN_CTRL_OFF);
2447fdf3d8aSMichal Mazur 
2457fdf3d8aSMichal Mazur 	if (mvebu_mbus_window_is_remappable(mbus, win)) {
2467fdf3d8aSMichal Mazur 		addr = mbus->mbuswins_base + mbus->soc->win_remap_offset(win);
247fddddb52SThomas Petazzoni 		writel(0, addr + WIN_REMAP_LO_OFF);
248fddddb52SThomas Petazzoni 		writel(0, addr + WIN_REMAP_HI_OFF);
249fddddb52SThomas Petazzoni 	}
250fddddb52SThomas Petazzoni }
251fddddb52SThomas Petazzoni 
252fddddb52SThomas Petazzoni /* Checks whether the given window number is available */
25338bdf45fSAndrew Lunn 
mvebu_mbus_window_is_free(struct mvebu_mbus_state * mbus,const int win)254fddddb52SThomas Petazzoni static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
255fddddb52SThomas Petazzoni 				     const int win)
256fddddb52SThomas Petazzoni {
257fddddb52SThomas Petazzoni 	void __iomem *addr = mbus->mbuswins_base +
258fddddb52SThomas Petazzoni 		mbus->soc->win_cfg_offset(win);
259fddddb52SThomas Petazzoni 	u32 ctrl = readl(addr + WIN_CTRL_OFF);
26038bdf45fSAndrew Lunn 
261fddddb52SThomas Petazzoni 	return !(ctrl & WIN_CTRL_ENABLE);
262fddddb52SThomas Petazzoni }
263fddddb52SThomas Petazzoni 
264fddddb52SThomas Petazzoni /*
265fddddb52SThomas Petazzoni  * Checks whether the given (base, base+size) area doesn't overlap an
266fddddb52SThomas Petazzoni  * existing region
267fddddb52SThomas Petazzoni  */
mvebu_mbus_window_conflicts(struct mvebu_mbus_state * mbus,phys_addr_t base,size_t size,u8 target,u8 attr)268fddddb52SThomas Petazzoni static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
269fddddb52SThomas Petazzoni 				       phys_addr_t base, size_t size,
270fddddb52SThomas Petazzoni 				       u8 target, u8 attr)
271fddddb52SThomas Petazzoni {
272fddddb52SThomas Petazzoni 	u64 end = (u64)base + size;
273fddddb52SThomas Petazzoni 	int win;
274fddddb52SThomas Petazzoni 
275fddddb52SThomas Petazzoni 	for (win = 0; win < mbus->soc->num_wins; win++) {
276fddddb52SThomas Petazzoni 		u64 wbase, wend;
277fddddb52SThomas Petazzoni 		u32 wsize;
278fddddb52SThomas Petazzoni 		u8 wtarget, wattr;
279fddddb52SThomas Petazzoni 		int enabled;
280fddddb52SThomas Petazzoni 
281fddddb52SThomas Petazzoni 		mvebu_mbus_read_window(mbus, win,
282fddddb52SThomas Petazzoni 				       &enabled, &wbase, &wsize,
283fddddb52SThomas Petazzoni 				       &wtarget, &wattr, NULL);
284fddddb52SThomas Petazzoni 
285fddddb52SThomas Petazzoni 		if (!enabled)
286fddddb52SThomas Petazzoni 			continue;
287fddddb52SThomas Petazzoni 
288fddddb52SThomas Petazzoni 		wend = wbase + wsize;
289fddddb52SThomas Petazzoni 
290fddddb52SThomas Petazzoni 		/*
291fddddb52SThomas Petazzoni 		 * Check if the current window overlaps with the
292fddddb52SThomas Petazzoni 		 * proposed physical range
293fddddb52SThomas Petazzoni 		 */
294fddddb52SThomas Petazzoni 		if ((u64)base < wend && end > wbase)
295fddddb52SThomas Petazzoni 			return 0;
296fddddb52SThomas Petazzoni 	}
297fddddb52SThomas Petazzoni 
298fddddb52SThomas Petazzoni 	return 1;
299fddddb52SThomas Petazzoni }
300fddddb52SThomas Petazzoni 
mvebu_mbus_find_window(struct mvebu_mbus_state * mbus,phys_addr_t base,size_t size)301fddddb52SThomas Petazzoni static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
302fddddb52SThomas Petazzoni 				  phys_addr_t base, size_t size)
303fddddb52SThomas Petazzoni {
304fddddb52SThomas Petazzoni 	int win;
305fddddb52SThomas Petazzoni 
306fddddb52SThomas Petazzoni 	for (win = 0; win < mbus->soc->num_wins; win++) {
307fddddb52SThomas Petazzoni 		u64 wbase;
308fddddb52SThomas Petazzoni 		u32 wsize;
309fddddb52SThomas Petazzoni 		int enabled;
310fddddb52SThomas Petazzoni 
311fddddb52SThomas Petazzoni 		mvebu_mbus_read_window(mbus, win,
312fddddb52SThomas Petazzoni 				       &enabled, &wbase, &wsize,
313fddddb52SThomas Petazzoni 				       NULL, NULL, NULL);
314fddddb52SThomas Petazzoni 
315fddddb52SThomas Petazzoni 		if (!enabled)
316fddddb52SThomas Petazzoni 			continue;
317fddddb52SThomas Petazzoni 
318fddddb52SThomas Petazzoni 		if (base == wbase && size == wsize)
319fddddb52SThomas Petazzoni 			return win;
320fddddb52SThomas Petazzoni 	}
321fddddb52SThomas Petazzoni 
322fddddb52SThomas Petazzoni 	return -ENODEV;
323fddddb52SThomas Petazzoni }
324fddddb52SThomas Petazzoni 
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)325fddddb52SThomas Petazzoni static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
326fddddb52SThomas Petazzoni 				   int win, phys_addr_t base, size_t size,
327fddddb52SThomas Petazzoni 				   phys_addr_t remap, u8 target,
328fddddb52SThomas Petazzoni 				   u8 attr)
329fddddb52SThomas Petazzoni {
330fddddb52SThomas Petazzoni 	void __iomem *addr = mbus->mbuswins_base +
331fddddb52SThomas Petazzoni 		mbus->soc->win_cfg_offset(win);
332fddddb52SThomas Petazzoni 	u32 ctrl, remap_addr;
333fddddb52SThomas Petazzoni 
33409752a12SJason Gunthorpe 	if (!is_power_of_2(size)) {
33509752a12SJason Gunthorpe 		WARN(true, "Invalid MBus window size: 0x%zx\n", size);
33609752a12SJason Gunthorpe 		return -EINVAL;
33709752a12SJason Gunthorpe 	}
33809752a12SJason Gunthorpe 
33909752a12SJason Gunthorpe 	if ((base & (phys_addr_t)(size - 1)) != 0) {
34009752a12SJason Gunthorpe 		WARN(true, "Invalid MBus base/size: %pa len 0x%zx\n", &base,
34109752a12SJason Gunthorpe 		     size);
34209752a12SJason Gunthorpe 		return -EINVAL;
34309752a12SJason Gunthorpe 	}
34409752a12SJason Gunthorpe 
345fddddb52SThomas Petazzoni 	ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
346fddddb52SThomas Petazzoni 		(attr << WIN_CTRL_ATTR_SHIFT)    |
347fddddb52SThomas Petazzoni 		(target << WIN_CTRL_TGT_SHIFT)   |
348fddddb52SThomas Petazzoni 		WIN_CTRL_ENABLE;
3498c9e06e6SNicolas Schichan 	if (mbus->hw_io_coherency)
3508c9e06e6SNicolas Schichan 		ctrl |= WIN_CTRL_SYNCBARRIER;
351fddddb52SThomas Petazzoni 
352fddddb52SThomas Petazzoni 	writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
353fddddb52SThomas Petazzoni 	writel(ctrl, addr + WIN_CTRL_OFF);
3547fdf3d8aSMichal Mazur 
3557fdf3d8aSMichal Mazur 	if (mvebu_mbus_window_is_remappable(mbus, win)) {
3567fdf3d8aSMichal Mazur 		void __iomem *addr_rmp = mbus->mbuswins_base +
3577fdf3d8aSMichal Mazur 			mbus->soc->win_remap_offset(win);
3587fdf3d8aSMichal Mazur 
359fddddb52SThomas Petazzoni 		if (remap == MVEBU_MBUS_NO_REMAP)
360fddddb52SThomas Petazzoni 			remap_addr = base;
361fddddb52SThomas Petazzoni 		else
362fddddb52SThomas Petazzoni 			remap_addr = remap;
3637fdf3d8aSMichal Mazur 		writel(remap_addr & WIN_REMAP_LOW, addr_rmp + WIN_REMAP_LO_OFF);
3647fdf3d8aSMichal Mazur 		writel(0, addr_rmp + WIN_REMAP_HI_OFF);
365fddddb52SThomas Petazzoni 	}
366fddddb52SThomas Petazzoni 
367fddddb52SThomas Petazzoni 	return 0;
368fddddb52SThomas Petazzoni }
369fddddb52SThomas Petazzoni 
mvebu_mbus_alloc_window(struct mvebu_mbus_state * mbus,phys_addr_t base,size_t size,phys_addr_t remap,u8 target,u8 attr)370fddddb52SThomas Petazzoni static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
371fddddb52SThomas Petazzoni 				   phys_addr_t base, size_t size,
372fddddb52SThomas Petazzoni 				   phys_addr_t remap, u8 target,
373fddddb52SThomas Petazzoni 				   u8 attr)
374fddddb52SThomas Petazzoni {
375fddddb52SThomas Petazzoni 	int win;
376fddddb52SThomas Petazzoni 
377fddddb52SThomas Petazzoni 	if (remap == MVEBU_MBUS_NO_REMAP) {
3787fdf3d8aSMichal Mazur 		for (win = 0; win < mbus->soc->num_wins; win++) {
3797fdf3d8aSMichal Mazur 			if (mvebu_mbus_window_is_remappable(mbus, win))
3807fdf3d8aSMichal Mazur 				continue;
3817fdf3d8aSMichal Mazur 
382fddddb52SThomas Petazzoni 			if (mvebu_mbus_window_is_free(mbus, win))
383fddddb52SThomas Petazzoni 				return mvebu_mbus_setup_window(mbus, win, base,
384fddddb52SThomas Petazzoni 							       size, remap,
385fddddb52SThomas Petazzoni 							       target, attr);
386fddddb52SThomas Petazzoni 		}
3877fdf3d8aSMichal Mazur 	}
388fddddb52SThomas Petazzoni 
3897fdf3d8aSMichal Mazur 	for (win = 0; win < mbus->soc->num_wins; win++) {
3907fdf3d8aSMichal Mazur 		/* Skip window if need remap but is not supported */
3917fdf3d8aSMichal Mazur 		if ((remap != MVEBU_MBUS_NO_REMAP) &&
3927fdf3d8aSMichal Mazur 		    !mvebu_mbus_window_is_remappable(mbus, win))
3937fdf3d8aSMichal Mazur 			continue;
394fddddb52SThomas Petazzoni 
395fddddb52SThomas Petazzoni 		if (mvebu_mbus_window_is_free(mbus, win))
396fddddb52SThomas Petazzoni 			return mvebu_mbus_setup_window(mbus, win, base, size,
397fddddb52SThomas Petazzoni 						       remap, target, attr);
3987fdf3d8aSMichal Mazur 	}
399fddddb52SThomas Petazzoni 
400fddddb52SThomas Petazzoni 	return -ENOMEM;
401fddddb52SThomas Petazzoni }
402fddddb52SThomas Petazzoni 
403fddddb52SThomas Petazzoni /*
404fddddb52SThomas Petazzoni  * Debugfs debugging
405fddddb52SThomas Petazzoni  */
406fddddb52SThomas Petazzoni 
407fddddb52SThomas Petazzoni /* Common function used for Dove, Kirkwood, Armada 370/XP and Orion 5x */
mvebu_sdram_debug_show_orion(struct mvebu_mbus_state * mbus,struct seq_file * seq,void * v)408fddddb52SThomas Petazzoni static int mvebu_sdram_debug_show_orion(struct mvebu_mbus_state *mbus,
409fddddb52SThomas Petazzoni 					struct seq_file *seq, void *v)
410fddddb52SThomas Petazzoni {
411fddddb52SThomas Petazzoni 	int i;
412fddddb52SThomas Petazzoni 
413fddddb52SThomas Petazzoni 	for (i = 0; i < 4; i++) {
414fddddb52SThomas Petazzoni 		u32 basereg = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
415fddddb52SThomas Petazzoni 		u32 sizereg = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
416fddddb52SThomas Petazzoni 		u64 base;
417fddddb52SThomas Petazzoni 		u32 size;
418fddddb52SThomas Petazzoni 
419fddddb52SThomas Petazzoni 		if (!(sizereg & DDR_SIZE_ENABLED)) {
420fddddb52SThomas Petazzoni 			seq_printf(seq, "[%d] disabled\n", i);
421fddddb52SThomas Petazzoni 			continue;
422fddddb52SThomas Petazzoni 		}
423fddddb52SThomas Petazzoni 
424fddddb52SThomas Petazzoni 		base = ((u64)basereg & DDR_BASE_CS_HIGH_MASK) << 32;
425fddddb52SThomas Petazzoni 		base |= basereg & DDR_BASE_CS_LOW_MASK;
426fddddb52SThomas Petazzoni 		size = (sizereg | ~DDR_SIZE_MASK);
427fddddb52SThomas Petazzoni 
428fddddb52SThomas Petazzoni 		seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
429fddddb52SThomas Petazzoni 			   i, (unsigned long long)base,
430fddddb52SThomas Petazzoni 			   (unsigned long long)base + size + 1,
431fddddb52SThomas Petazzoni 			   (sizereg & DDR_SIZE_CS_MASK) >> DDR_SIZE_CS_SHIFT);
432fddddb52SThomas Petazzoni 	}
433fddddb52SThomas Petazzoni 
434fddddb52SThomas Petazzoni 	return 0;
435fddddb52SThomas Petazzoni }
436fddddb52SThomas Petazzoni 
437fddddb52SThomas Petazzoni /* Special function for Dove */
mvebu_sdram_debug_show_dove(struct mvebu_mbus_state * mbus,struct seq_file * seq,void * v)438fddddb52SThomas Petazzoni static int mvebu_sdram_debug_show_dove(struct mvebu_mbus_state *mbus,
439fddddb52SThomas Petazzoni 				       struct seq_file *seq, void *v)
440fddddb52SThomas Petazzoni {
441fddddb52SThomas Petazzoni 	int i;
442fddddb52SThomas Petazzoni 
443fddddb52SThomas Petazzoni 	for (i = 0; i < 2; i++) {
444fddddb52SThomas Petazzoni 		u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
445fddddb52SThomas Petazzoni 		u64 base;
446fddddb52SThomas Petazzoni 		u32 size;
447fddddb52SThomas Petazzoni 
448fddddb52SThomas Petazzoni 		if (!(map & 1)) {
449fddddb52SThomas Petazzoni 			seq_printf(seq, "[%d] disabled\n", i);
450fddddb52SThomas Petazzoni 			continue;
451fddddb52SThomas Petazzoni 		}
452fddddb52SThomas Petazzoni 
453fddddb52SThomas Petazzoni 		base = map & 0xff800000;
454fddddb52SThomas Petazzoni 		size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
455fddddb52SThomas Petazzoni 
456fddddb52SThomas Petazzoni 		seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
457fddddb52SThomas Petazzoni 			   i, (unsigned long long)base,
458fddddb52SThomas Petazzoni 			   (unsigned long long)base + size, i);
459fddddb52SThomas Petazzoni 	}
460fddddb52SThomas Petazzoni 
461fddddb52SThomas Petazzoni 	return 0;
462fddddb52SThomas Petazzoni }
463fddddb52SThomas Petazzoni 
mvebu_sdram_debug_show(struct seq_file * seq,void * v)464fddddb52SThomas Petazzoni static int mvebu_sdram_debug_show(struct seq_file *seq, void *v)
465fddddb52SThomas Petazzoni {
466fddddb52SThomas Petazzoni 	struct mvebu_mbus_state *mbus = &mbus_state;
467fddddb52SThomas Petazzoni 	return mbus->soc->show_cpu_target(mbus, seq, v);
468fddddb52SThomas Petazzoni }
469a5ccec12SLiu Shixin DEFINE_SHOW_ATTRIBUTE(mvebu_sdram_debug);
470fddddb52SThomas Petazzoni 
mvebu_devs_debug_show(struct seq_file * seq,void * v)471fddddb52SThomas Petazzoni static int mvebu_devs_debug_show(struct seq_file *seq, void *v)
472fddddb52SThomas Petazzoni {
473fddddb52SThomas Petazzoni 	struct mvebu_mbus_state *mbus = &mbus_state;
474fddddb52SThomas Petazzoni 	int win;
475fddddb52SThomas Petazzoni 
476fddddb52SThomas Petazzoni 	for (win = 0; win < mbus->soc->num_wins; win++) {
477fddddb52SThomas Petazzoni 		u64 wbase, wremap;
478fddddb52SThomas Petazzoni 		u32 wsize;
479fddddb52SThomas Petazzoni 		u8 wtarget, wattr;
480ed843a7dSThomas Petazzoni 		int enabled;
481fddddb52SThomas Petazzoni 
482fddddb52SThomas Petazzoni 		mvebu_mbus_read_window(mbus, win,
483fddddb52SThomas Petazzoni 				       &enabled, &wbase, &wsize,
484fddddb52SThomas Petazzoni 				       &wtarget, &wattr, &wremap);
485fddddb52SThomas Petazzoni 
486fddddb52SThomas Petazzoni 		if (!enabled) {
487fddddb52SThomas Petazzoni 			seq_printf(seq, "[%02d] disabled\n", win);
488fddddb52SThomas Petazzoni 			continue;
489fddddb52SThomas Petazzoni 		}
490fddddb52SThomas Petazzoni 
491ed843a7dSThomas Petazzoni 		seq_printf(seq, "[%02d] %016llx - %016llx : %04x:%04x",
492fddddb52SThomas Petazzoni 			   win, (unsigned long long)wbase,
493ed843a7dSThomas Petazzoni 			   (unsigned long long)(wbase + wsize), wtarget, wattr);
494fddddb52SThomas Petazzoni 
49509752a12SJason Gunthorpe 		if (!is_power_of_2(wsize) ||
49609752a12SJason Gunthorpe 		    ((wbase & (u64)(wsize - 1)) != 0))
49709752a12SJason Gunthorpe 			seq_puts(seq, " (Invalid base/size!!)");
49809752a12SJason Gunthorpe 
4997fdf3d8aSMichal Mazur 		if (mvebu_mbus_window_is_remappable(mbus, win)) {
500fddddb52SThomas Petazzoni 			seq_printf(seq, " (remap %016llx)\n",
501fddddb52SThomas Petazzoni 				   (unsigned long long)wremap);
502fddddb52SThomas Petazzoni 		} else
503fddddb52SThomas Petazzoni 			seq_printf(seq, "\n");
504fddddb52SThomas Petazzoni 	}
505fddddb52SThomas Petazzoni 
506fddddb52SThomas Petazzoni 	return 0;
507fddddb52SThomas Petazzoni }
508a5ccec12SLiu Shixin DEFINE_SHOW_ATTRIBUTE(mvebu_devs_debug);
509fddddb52SThomas Petazzoni 
510fddddb52SThomas Petazzoni /*
511fddddb52SThomas Petazzoni  * SoC-specific functions and definitions
512fddddb52SThomas Petazzoni  */
513fddddb52SThomas Petazzoni 
generic_mbus_win_cfg_offset(int win)5147fdf3d8aSMichal Mazur static unsigned int generic_mbus_win_cfg_offset(int win)
515fddddb52SThomas Petazzoni {
516fddddb52SThomas Petazzoni 	return win << 4;
517fddddb52SThomas Petazzoni }
518fddddb52SThomas Petazzoni 
armada_370_xp_mbus_win_cfg_offset(int win)5197fdf3d8aSMichal Mazur static unsigned int armada_370_xp_mbus_win_cfg_offset(int win)
520fddddb52SThomas Petazzoni {
521fddddb52SThomas Petazzoni 	/* The register layout is a bit annoying and the below code
522fddddb52SThomas Petazzoni 	 * tries to cope with it.
523fddddb52SThomas Petazzoni 	 * - At offset 0x0, there are the registers for the first 8
524fddddb52SThomas Petazzoni 	 *   windows, with 4 registers of 32 bits per window (ctrl,
525fddddb52SThomas Petazzoni 	 *   base, remap low, remap high)
526fddddb52SThomas Petazzoni 	 * - Then at offset 0x80, there is a hole of 0x10 bytes for
527fddddb52SThomas Petazzoni 	 *   the internal registers base address and internal units
528fddddb52SThomas Petazzoni 	 *   sync barrier register.
529fddddb52SThomas Petazzoni 	 * - Then at offset 0x90, there the registers for 12
530fddddb52SThomas Petazzoni 	 *   windows, with only 2 registers of 32 bits per window
531fddddb52SThomas Petazzoni 	 *   (ctrl, base).
532fddddb52SThomas Petazzoni 	 */
533fddddb52SThomas Petazzoni 	if (win < 8)
534fddddb52SThomas Petazzoni 		return win << 4;
535fddddb52SThomas Petazzoni 	else
536fddddb52SThomas Petazzoni 		return 0x90 + ((win - 8) << 3);
537fddddb52SThomas Petazzoni }
538fddddb52SThomas Petazzoni 
mv78xx0_mbus_win_cfg_offset(int win)5397fdf3d8aSMichal Mazur static unsigned int mv78xx0_mbus_win_cfg_offset(int win)
540fddddb52SThomas Petazzoni {
541fddddb52SThomas Petazzoni 	if (win < 8)
542fddddb52SThomas Petazzoni 		return win << 4;
543fddddb52SThomas Petazzoni 	else
544fddddb52SThomas Petazzoni 		return 0x900 + ((win - 8) << 4);
545fddddb52SThomas Petazzoni }
546fddddb52SThomas Petazzoni 
generic_mbus_win_remap_2_offset(int win)5477fdf3d8aSMichal Mazur static unsigned int generic_mbus_win_remap_2_offset(int win)
5487fdf3d8aSMichal Mazur {
5497fdf3d8aSMichal Mazur 	if (win < 2)
5507fdf3d8aSMichal Mazur 		return generic_mbus_win_cfg_offset(win);
5517fdf3d8aSMichal Mazur 	else
5527fdf3d8aSMichal Mazur 		return MVEBU_MBUS_NO_REMAP;
5537fdf3d8aSMichal Mazur }
5547fdf3d8aSMichal Mazur 
generic_mbus_win_remap_4_offset(int win)5557fdf3d8aSMichal Mazur static unsigned int generic_mbus_win_remap_4_offset(int win)
5567fdf3d8aSMichal Mazur {
5577fdf3d8aSMichal Mazur 	if (win < 4)
5587fdf3d8aSMichal Mazur 		return generic_mbus_win_cfg_offset(win);
5597fdf3d8aSMichal Mazur 	else
5607fdf3d8aSMichal Mazur 		return MVEBU_MBUS_NO_REMAP;
5617fdf3d8aSMichal Mazur }
5627fdf3d8aSMichal Mazur 
generic_mbus_win_remap_8_offset(int win)5637fdf3d8aSMichal Mazur static unsigned int generic_mbus_win_remap_8_offset(int win)
5647fdf3d8aSMichal Mazur {
5657fdf3d8aSMichal Mazur 	if (win < 8)
5667fdf3d8aSMichal Mazur 		return generic_mbus_win_cfg_offset(win);
5677fdf3d8aSMichal Mazur 	else
5687fdf3d8aSMichal Mazur 		return MVEBU_MBUS_NO_REMAP;
5697fdf3d8aSMichal Mazur }
5707fdf3d8aSMichal Mazur 
armada_xp_mbus_win_remap_offset(int win)5717fdf3d8aSMichal Mazur static unsigned int armada_xp_mbus_win_remap_offset(int win)
5727fdf3d8aSMichal Mazur {
5737fdf3d8aSMichal Mazur 	if (win < 8)
5747fdf3d8aSMichal Mazur 		return generic_mbus_win_cfg_offset(win);
5757fdf3d8aSMichal Mazur 	else if (win == 13)
5767fdf3d8aSMichal Mazur 		return 0xF0 - WIN_REMAP_LO_OFF;
5777fdf3d8aSMichal Mazur 	else
5787fdf3d8aSMichal Mazur 		return MVEBU_MBUS_NO_REMAP;
5797fdf3d8aSMichal Mazur }
5807fdf3d8aSMichal Mazur 
581bfa1ce5fSThomas Petazzoni /*
582bfa1ce5fSThomas Petazzoni  * Use the memblock information to find the MBus bridge hole in the
583bfa1ce5fSThomas Petazzoni  * physical address space.
584bfa1ce5fSThomas Petazzoni  */
585bfa1ce5fSThomas Petazzoni static void __init
mvebu_mbus_find_bridge_hole(uint64_t * start,uint64_t * end)586bfa1ce5fSThomas Petazzoni mvebu_mbus_find_bridge_hole(uint64_t *start, uint64_t *end)
587bfa1ce5fSThomas Petazzoni {
588b10d6bcaSMike Rapoport 	phys_addr_t reg_start, reg_end;
589b10d6bcaSMike Rapoport 	uint64_t i, s = 0;
590bfa1ce5fSThomas Petazzoni 
591b10d6bcaSMike Rapoport 	for_each_mem_range(i, &reg_start, &reg_end) {
592bfa1ce5fSThomas Petazzoni 		/*
593bfa1ce5fSThomas Petazzoni 		 * This part of the memory is above 4 GB, so we don't
594bfa1ce5fSThomas Petazzoni 		 * care for the MBus bridge hole.
595bfa1ce5fSThomas Petazzoni 		 */
596a2fa9e57SArnd Bergmann 		if ((u64)reg_start >= 0x100000000ULL)
597bfa1ce5fSThomas Petazzoni 			continue;
598bfa1ce5fSThomas Petazzoni 
599bfa1ce5fSThomas Petazzoni 		/*
600bfa1ce5fSThomas Petazzoni 		 * The MBus bridge hole is at the end of the RAM under
601bfa1ce5fSThomas Petazzoni 		 * the 4 GB limit.
602bfa1ce5fSThomas Petazzoni 		 */
603b10d6bcaSMike Rapoport 		if (reg_end > s)
604b10d6bcaSMike Rapoport 			s = reg_end;
605bfa1ce5fSThomas Petazzoni 	}
606bfa1ce5fSThomas Petazzoni 
607bfa1ce5fSThomas Petazzoni 	*start = s;
608bfa1ce5fSThomas Petazzoni 	*end = 0x100000000ULL;
609bfa1ce5fSThomas Petazzoni }
610bfa1ce5fSThomas Petazzoni 
611bfa1ce5fSThomas Petazzoni /*
612bfa1ce5fSThomas Petazzoni  * This function fills in the mvebu_mbus_dram_info_nooverlap data
613bfa1ce5fSThomas Petazzoni  * structure, by looking at the mvebu_mbus_dram_info data, and
614bfa1ce5fSThomas Petazzoni  * removing the parts of it that overlap with I/O windows.
615bfa1ce5fSThomas Petazzoni  */
616bfa1ce5fSThomas Petazzoni static void __init
mvebu_mbus_setup_cpu_target_nooverlap(struct mvebu_mbus_state * mbus)617bfa1ce5fSThomas Petazzoni mvebu_mbus_setup_cpu_target_nooverlap(struct mvebu_mbus_state *mbus)
618bfa1ce5fSThomas Petazzoni {
619bfa1ce5fSThomas Petazzoni 	uint64_t mbus_bridge_base, mbus_bridge_end;
620bfa1ce5fSThomas Petazzoni 	int cs_nooverlap = 0;
621bfa1ce5fSThomas Petazzoni 	int i;
622bfa1ce5fSThomas Petazzoni 
623bfa1ce5fSThomas Petazzoni 	mvebu_mbus_find_bridge_hole(&mbus_bridge_base, &mbus_bridge_end);
624bfa1ce5fSThomas Petazzoni 
625bfa1ce5fSThomas Petazzoni 	for (i = 0; i < mvebu_mbus_dram_info.num_cs; i++) {
626bfa1ce5fSThomas Petazzoni 		struct mbus_dram_window *w;
627bfa1ce5fSThomas Petazzoni 		u64 base, size, end;
628bfa1ce5fSThomas Petazzoni 
629bfa1ce5fSThomas Petazzoni 		w = &mvebu_mbus_dram_info.cs[i];
630bfa1ce5fSThomas Petazzoni 		base = w->base;
631bfa1ce5fSThomas Petazzoni 		size = w->size;
632bfa1ce5fSThomas Petazzoni 		end = base + size;
633bfa1ce5fSThomas Petazzoni 
634bfa1ce5fSThomas Petazzoni 		/*
635bfa1ce5fSThomas Petazzoni 		 * The CS is fully enclosed inside the MBus bridge
636bfa1ce5fSThomas Petazzoni 		 * area, so ignore it.
637bfa1ce5fSThomas Petazzoni 		 */
638bfa1ce5fSThomas Petazzoni 		if (base >= mbus_bridge_base && end <= mbus_bridge_end)
639bfa1ce5fSThomas Petazzoni 			continue;
640bfa1ce5fSThomas Petazzoni 
641bfa1ce5fSThomas Petazzoni 		/*
642bfa1ce5fSThomas Petazzoni 		 * Beginning of CS overlaps with end of MBus, raise CS
643bfa1ce5fSThomas Petazzoni 		 * base address, and shrink its size.
644bfa1ce5fSThomas Petazzoni 		 */
645bfa1ce5fSThomas Petazzoni 		if (base >= mbus_bridge_base && end > mbus_bridge_end) {
646bfa1ce5fSThomas Petazzoni 			size -= mbus_bridge_end - base;
647bfa1ce5fSThomas Petazzoni 			base = mbus_bridge_end;
648bfa1ce5fSThomas Petazzoni 		}
649bfa1ce5fSThomas Petazzoni 
650bfa1ce5fSThomas Petazzoni 		/*
651bfa1ce5fSThomas Petazzoni 		 * End of CS overlaps with beginning of MBus, shrink
652bfa1ce5fSThomas Petazzoni 		 * CS size.
653bfa1ce5fSThomas Petazzoni 		 */
654bfa1ce5fSThomas Petazzoni 		if (base < mbus_bridge_base && end > mbus_bridge_base)
655bfa1ce5fSThomas Petazzoni 			size -= end - mbus_bridge_base;
656bfa1ce5fSThomas Petazzoni 
657bfa1ce5fSThomas Petazzoni 		w = &mvebu_mbus_dram_info_nooverlap.cs[cs_nooverlap++];
658bfa1ce5fSThomas Petazzoni 		w->cs_index = i;
659bfa1ce5fSThomas Petazzoni 		w->mbus_attr = 0xf & ~(1 << i);
660bfa1ce5fSThomas Petazzoni 		if (mbus->hw_io_coherency)
661bfa1ce5fSThomas Petazzoni 			w->mbus_attr |= ATTR_HW_COHERENCY;
662bfa1ce5fSThomas Petazzoni 		w->base = base;
663bfa1ce5fSThomas Petazzoni 		w->size = size;
664bfa1ce5fSThomas Petazzoni 	}
665bfa1ce5fSThomas Petazzoni 
666bfa1ce5fSThomas Petazzoni 	mvebu_mbus_dram_info_nooverlap.mbus_dram_target_id = TARGET_DDR;
667bfa1ce5fSThomas Petazzoni 	mvebu_mbus_dram_info_nooverlap.num_cs = cs_nooverlap;
668bfa1ce5fSThomas Petazzoni }
669bfa1ce5fSThomas Petazzoni 
670fddddb52SThomas Petazzoni static void __init
mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state * mbus)671fddddb52SThomas Petazzoni mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
672fddddb52SThomas Petazzoni {
673fddddb52SThomas Petazzoni 	int i;
674fddddb52SThomas Petazzoni 	int cs;
675fddddb52SThomas Petazzoni 
676fddddb52SThomas Petazzoni 	mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
677fddddb52SThomas Petazzoni 
678fddddb52SThomas Petazzoni 	for (i = 0, cs = 0; i < 4; i++) {
679885dbd15SThomas Petazzoni 		u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
680885dbd15SThomas Petazzoni 		u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
681885dbd15SThomas Petazzoni 
682885dbd15SThomas Petazzoni 		/*
683885dbd15SThomas Petazzoni 		 * We only take care of entries for which the chip
684885dbd15SThomas Petazzoni 		 * select is enabled, and that don't have high base
685885dbd15SThomas Petazzoni 		 * address bits set (devices can only access the first
686885dbd15SThomas Petazzoni 		 * 32 bits of the memory).
687885dbd15SThomas Petazzoni 		 */
688885dbd15SThomas Petazzoni 		if ((size & DDR_SIZE_ENABLED) &&
689885dbd15SThomas Petazzoni 		    !(base & DDR_BASE_CS_HIGH_MASK)) {
6901737cac6SThomas Petazzoni 			struct mbus_dram_window *w;
6911737cac6SThomas Petazzoni 
692fddddb52SThomas Petazzoni 			w = &mvebu_mbus_dram_info.cs[cs++];
693fddddb52SThomas Petazzoni 			w->cs_index = i;
694fddddb52SThomas Petazzoni 			w->mbus_attr = 0xf & ~(1 << i);
695fddddb52SThomas Petazzoni 			if (mbus->hw_io_coherency)
696fddddb52SThomas Petazzoni 				w->mbus_attr |= ATTR_HW_COHERENCY;
697885dbd15SThomas Petazzoni 			w->base = base & DDR_BASE_CS_LOW_MASK;
6982bbbd963SJan Luebbe 			w->size = (u64)(size | ~DDR_SIZE_MASK) + 1;
699885dbd15SThomas Petazzoni 		}
700fddddb52SThomas Petazzoni 	}
701fddddb52SThomas Petazzoni 	mvebu_mbus_dram_info.num_cs = cs;
702fddddb52SThomas Petazzoni }
703fddddb52SThomas Petazzoni 
7044749c02bSThomas Petazzoni static int
mvebu_mbus_default_save_cpu_target(struct mvebu_mbus_state * mbus,u32 __iomem * store_addr)7054749c02bSThomas Petazzoni mvebu_mbus_default_save_cpu_target(struct mvebu_mbus_state *mbus,
706fce7b5aeSBen Dooks 				   u32 __iomem *store_addr)
7074749c02bSThomas Petazzoni {
7084749c02bSThomas Petazzoni 	int i;
7094749c02bSThomas Petazzoni 
7104749c02bSThomas Petazzoni 	for (i = 0; i < 4; i++) {
7114749c02bSThomas Petazzoni 		u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
7124749c02bSThomas Petazzoni 		u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
7134749c02bSThomas Petazzoni 
7144749c02bSThomas Petazzoni 		writel(mbus->sdramwins_phys_base + DDR_BASE_CS_OFF(i),
7154749c02bSThomas Petazzoni 		       store_addr++);
7164749c02bSThomas Petazzoni 		writel(base, store_addr++);
7174749c02bSThomas Petazzoni 		writel(mbus->sdramwins_phys_base + DDR_SIZE_CS_OFF(i),
7184749c02bSThomas Petazzoni 		       store_addr++);
7194749c02bSThomas Petazzoni 		writel(size, store_addr++);
7204749c02bSThomas Petazzoni 	}
7214749c02bSThomas Petazzoni 
7224749c02bSThomas Petazzoni 	/* We've written 16 words to the store address */
7234749c02bSThomas Petazzoni 	return 16;
7244749c02bSThomas Petazzoni }
7254749c02bSThomas Petazzoni 
726fddddb52SThomas Petazzoni static void __init
mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state * mbus)727fddddb52SThomas Petazzoni mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state *mbus)
728fddddb52SThomas Petazzoni {
729fddddb52SThomas Petazzoni 	int i;
730fddddb52SThomas Petazzoni 	int cs;
731fddddb52SThomas Petazzoni 
732fddddb52SThomas Petazzoni 	mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
733fddddb52SThomas Petazzoni 
734fddddb52SThomas Petazzoni 	for (i = 0, cs = 0; i < 2; i++) {
735fddddb52SThomas Petazzoni 		u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
736fddddb52SThomas Petazzoni 
737fddddb52SThomas Petazzoni 		/*
738fddddb52SThomas Petazzoni 		 * Chip select enabled?
739fddddb52SThomas Petazzoni 		 */
740fddddb52SThomas Petazzoni 		if (map & 1) {
741fddddb52SThomas Petazzoni 			struct mbus_dram_window *w;
742fddddb52SThomas Petazzoni 
743fddddb52SThomas Petazzoni 			w = &mvebu_mbus_dram_info.cs[cs++];
744fddddb52SThomas Petazzoni 			w->cs_index = i;
745fddddb52SThomas Petazzoni 			w->mbus_attr = 0; /* CS address decoding done inside */
746fddddb52SThomas Petazzoni 					  /* the DDR controller, no need to  */
747fddddb52SThomas Petazzoni 					  /* provide attributes */
748fddddb52SThomas Petazzoni 			w->base = map & 0xff800000;
749fddddb52SThomas Petazzoni 			w->size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
750fddddb52SThomas Petazzoni 		}
751fddddb52SThomas Petazzoni 	}
752fddddb52SThomas Petazzoni 
753fddddb52SThomas Petazzoni 	mvebu_mbus_dram_info.num_cs = cs;
754fddddb52SThomas Petazzoni }
755fddddb52SThomas Petazzoni 
7564749c02bSThomas Petazzoni static int
mvebu_mbus_dove_save_cpu_target(struct mvebu_mbus_state * mbus,u32 __iomem * store_addr)7574749c02bSThomas Petazzoni mvebu_mbus_dove_save_cpu_target(struct mvebu_mbus_state *mbus,
758fce7b5aeSBen Dooks 				u32 __iomem *store_addr)
7594749c02bSThomas Petazzoni {
7604749c02bSThomas Petazzoni 	int i;
7614749c02bSThomas Petazzoni 
7624749c02bSThomas Petazzoni 	for (i = 0; i < 2; i++) {
7634749c02bSThomas Petazzoni 		u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
7644749c02bSThomas Petazzoni 
7654749c02bSThomas Petazzoni 		writel(mbus->sdramwins_phys_base + DOVE_DDR_BASE_CS_OFF(i),
7664749c02bSThomas Petazzoni 		       store_addr++);
7674749c02bSThomas Petazzoni 		writel(map, store_addr++);
7684749c02bSThomas Petazzoni 	}
7694749c02bSThomas Petazzoni 
7704749c02bSThomas Petazzoni 	/* We've written 4 words to the store address */
7714749c02bSThomas Petazzoni 	return 4;
7724749c02bSThomas Petazzoni }
7734749c02bSThomas Petazzoni 
mvebu_mbus_save_cpu_target(u32 __iomem * store_addr)774fce7b5aeSBen Dooks int mvebu_mbus_save_cpu_target(u32 __iomem *store_addr)
7754749c02bSThomas Petazzoni {
7764749c02bSThomas Petazzoni 	return mbus_state.soc->save_cpu_target(&mbus_state, store_addr);
7774749c02bSThomas Petazzoni }
7784749c02bSThomas Petazzoni 
7797fdf3d8aSMichal Mazur static const struct mvebu_mbus_soc_data armada_370_mbus_data = {
780fddddb52SThomas Petazzoni 	.num_wins            = 20,
781a0e89c02SThomas Petazzoni 	.has_mbus_bridge     = true,
7827fdf3d8aSMichal Mazur 	.win_cfg_offset      = armada_370_xp_mbus_win_cfg_offset,
7837fdf3d8aSMichal Mazur 	.win_remap_offset    = generic_mbus_win_remap_8_offset,
784fddddb52SThomas Petazzoni 	.setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
785fddddb52SThomas Petazzoni 	.show_cpu_target     = mvebu_sdram_debug_show_orion,
7867fdf3d8aSMichal Mazur 	.save_cpu_target     = mvebu_mbus_default_save_cpu_target,
7877fdf3d8aSMichal Mazur };
7887fdf3d8aSMichal Mazur 
7897fdf3d8aSMichal Mazur static const struct mvebu_mbus_soc_data armada_xp_mbus_data = {
7907fdf3d8aSMichal Mazur 	.num_wins            = 20,
7917fdf3d8aSMichal Mazur 	.has_mbus_bridge     = true,
7927fdf3d8aSMichal Mazur 	.win_cfg_offset      = armada_370_xp_mbus_win_cfg_offset,
7937fdf3d8aSMichal Mazur 	.win_remap_offset    = armada_xp_mbus_win_remap_offset,
7947fdf3d8aSMichal Mazur 	.setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
7957fdf3d8aSMichal Mazur 	.show_cpu_target     = mvebu_sdram_debug_show_orion,
7967fdf3d8aSMichal Mazur 	.save_cpu_target     = mvebu_mbus_default_save_cpu_target,
797fddddb52SThomas Petazzoni };
798fddddb52SThomas Petazzoni 
799fddddb52SThomas Petazzoni static const struct mvebu_mbus_soc_data kirkwood_mbus_data = {
800fddddb52SThomas Petazzoni 	.num_wins            = 8,
8017fdf3d8aSMichal Mazur 	.win_cfg_offset      = generic_mbus_win_cfg_offset,
8024749c02bSThomas Petazzoni 	.save_cpu_target     = mvebu_mbus_default_save_cpu_target,
8037fdf3d8aSMichal Mazur 	.win_remap_offset    = generic_mbus_win_remap_4_offset,
804fddddb52SThomas Petazzoni 	.setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
805fddddb52SThomas Petazzoni 	.show_cpu_target     = mvebu_sdram_debug_show_orion,
806fddddb52SThomas Petazzoni };
807fddddb52SThomas Petazzoni 
808fddddb52SThomas Petazzoni static const struct mvebu_mbus_soc_data dove_mbus_data = {
809fddddb52SThomas Petazzoni 	.num_wins            = 8,
8107fdf3d8aSMichal Mazur 	.win_cfg_offset      = generic_mbus_win_cfg_offset,
8114749c02bSThomas Petazzoni 	.save_cpu_target     = mvebu_mbus_dove_save_cpu_target,
8127fdf3d8aSMichal Mazur 	.win_remap_offset    = generic_mbus_win_remap_4_offset,
813fddddb52SThomas Petazzoni 	.setup_cpu_target    = mvebu_mbus_dove_setup_cpu_target,
814fddddb52SThomas Petazzoni 	.show_cpu_target     = mvebu_sdram_debug_show_dove,
815fddddb52SThomas Petazzoni };
816fddddb52SThomas Petazzoni 
817fddddb52SThomas Petazzoni /*
818fddddb52SThomas Petazzoni  * Some variants of Orion5x have 4 remappable windows, some other have
819fddddb52SThomas Petazzoni  * only two of them.
820fddddb52SThomas Petazzoni  */
821fddddb52SThomas Petazzoni static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data = {
822fddddb52SThomas Petazzoni 	.num_wins            = 8,
8237fdf3d8aSMichal Mazur 	.win_cfg_offset      = generic_mbus_win_cfg_offset,
8244749c02bSThomas Petazzoni 	.save_cpu_target     = mvebu_mbus_default_save_cpu_target,
8257fdf3d8aSMichal Mazur 	.win_remap_offset    = generic_mbus_win_remap_4_offset,
826fddddb52SThomas Petazzoni 	.setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
827fddddb52SThomas Petazzoni 	.show_cpu_target     = mvebu_sdram_debug_show_orion,
828fddddb52SThomas Petazzoni };
829fddddb52SThomas Petazzoni 
830fddddb52SThomas Petazzoni static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data = {
831fddddb52SThomas Petazzoni 	.num_wins            = 8,
8327fdf3d8aSMichal Mazur 	.win_cfg_offset      = generic_mbus_win_cfg_offset,
8334749c02bSThomas Petazzoni 	.save_cpu_target     = mvebu_mbus_default_save_cpu_target,
8347fdf3d8aSMichal Mazur 	.win_remap_offset    = generic_mbus_win_remap_2_offset,
835fddddb52SThomas Petazzoni 	.setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
836fddddb52SThomas Petazzoni 	.show_cpu_target     = mvebu_sdram_debug_show_orion,
837fddddb52SThomas Petazzoni };
838fddddb52SThomas Petazzoni 
839fddddb52SThomas Petazzoni static const struct mvebu_mbus_soc_data mv78xx0_mbus_data = {
840fddddb52SThomas Petazzoni 	.num_wins            = 14,
8417fdf3d8aSMichal Mazur 	.win_cfg_offset      = mv78xx0_mbus_win_cfg_offset,
8424749c02bSThomas Petazzoni 	.save_cpu_target     = mvebu_mbus_default_save_cpu_target,
8437fdf3d8aSMichal Mazur 	.win_remap_offset    = generic_mbus_win_remap_8_offset,
844fddddb52SThomas Petazzoni 	.setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
845fddddb52SThomas Petazzoni 	.show_cpu_target     = mvebu_sdram_debug_show_orion,
846fddddb52SThomas Petazzoni };
847fddddb52SThomas Petazzoni 
848fddddb52SThomas Petazzoni static const struct of_device_id of_mvebu_mbus_ids[] = {
849fddddb52SThomas Petazzoni 	{ .compatible = "marvell,armada370-mbus",
8507fdf3d8aSMichal Mazur 	  .data = &armada_370_mbus_data, },
8517fdf3d8aSMichal Mazur 	{ .compatible = "marvell,armada375-mbus",
8527fdf3d8aSMichal Mazur 	  .data = &armada_xp_mbus_data, },
8537fdf3d8aSMichal Mazur 	{ .compatible = "marvell,armada380-mbus",
8547fdf3d8aSMichal Mazur 	  .data = &armada_xp_mbus_data, },
855fddddb52SThomas Petazzoni 	{ .compatible = "marvell,armadaxp-mbus",
8567fdf3d8aSMichal Mazur 	  .data = &armada_xp_mbus_data, },
857fddddb52SThomas Petazzoni 	{ .compatible = "marvell,kirkwood-mbus",
858fddddb52SThomas Petazzoni 	  .data = &kirkwood_mbus_data, },
859fddddb52SThomas Petazzoni 	{ .compatible = "marvell,dove-mbus",
860fddddb52SThomas Petazzoni 	  .data = &dove_mbus_data, },
861fddddb52SThomas Petazzoni 	{ .compatible = "marvell,orion5x-88f5281-mbus",
862fddddb52SThomas Petazzoni 	  .data = &orion5x_4win_mbus_data, },
863fddddb52SThomas Petazzoni 	{ .compatible = "marvell,orion5x-88f5182-mbus",
864fddddb52SThomas Petazzoni 	  .data = &orion5x_2win_mbus_data, },
865fddddb52SThomas Petazzoni 	{ .compatible = "marvell,orion5x-88f5181-mbus",
866fddddb52SThomas Petazzoni 	  .data = &orion5x_2win_mbus_data, },
867fddddb52SThomas Petazzoni 	{ .compatible = "marvell,orion5x-88f6183-mbus",
868fddddb52SThomas Petazzoni 	  .data = &orion5x_4win_mbus_data, },
869fddddb52SThomas Petazzoni 	{ .compatible = "marvell,mv78xx0-mbus",
870fddddb52SThomas Petazzoni 	  .data = &mv78xx0_mbus_data, },
871fddddb52SThomas Petazzoni 	{ },
872fddddb52SThomas Petazzoni };
873fddddb52SThomas Petazzoni 
874fddddb52SThomas Petazzoni /*
875fddddb52SThomas Petazzoni  * Public API of the driver
876fddddb52SThomas Petazzoni  */
mvebu_mbus_add_window_remap_by_id(unsigned int target,unsigned int attribute,phys_addr_t base,size_t size,phys_addr_t remap)8776a63b098SThomas Petazzoni int mvebu_mbus_add_window_remap_by_id(unsigned int target,
8786a63b098SThomas Petazzoni 				      unsigned int attribute,
8796a63b098SThomas Petazzoni 				      phys_addr_t base, size_t size,
8806a63b098SThomas Petazzoni 				      phys_addr_t remap)
8816a63b098SThomas Petazzoni {
8826a63b098SThomas Petazzoni 	struct mvebu_mbus_state *s = &mbus_state;
8836a63b098SThomas Petazzoni 
8846a63b098SThomas Petazzoni 	if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
8856a63b098SThomas Petazzoni 		pr_err("cannot add window '%x:%x', conflicts with another window\n",
8866a63b098SThomas Petazzoni 		       target, attribute);
8876a63b098SThomas Petazzoni 		return -EINVAL;
8886a63b098SThomas Petazzoni 	}
8896a63b098SThomas Petazzoni 
8906a63b098SThomas Petazzoni 	return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
8916a63b098SThomas Petazzoni }
892859186e2SPali Rohár EXPORT_SYMBOL_GPL(mvebu_mbus_add_window_remap_by_id);
8936a63b098SThomas Petazzoni 
mvebu_mbus_add_window_by_id(unsigned int target,unsigned int attribute,phys_addr_t base,size_t size)8946a63b098SThomas Petazzoni int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
8956a63b098SThomas Petazzoni 				phys_addr_t base, size_t size)
8966a63b098SThomas Petazzoni {
8976a63b098SThomas Petazzoni 	return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
8986a63b098SThomas Petazzoni 						 size, MVEBU_MBUS_NO_REMAP);
8996a63b098SThomas Petazzoni }
900859186e2SPali Rohár EXPORT_SYMBOL_GPL(mvebu_mbus_add_window_by_id);
9016a63b098SThomas Petazzoni 
mvebu_mbus_del_window(phys_addr_t base,size_t size)902fddddb52SThomas Petazzoni int mvebu_mbus_del_window(phys_addr_t base, size_t size)
903fddddb52SThomas Petazzoni {
904fddddb52SThomas Petazzoni 	int win;
905fddddb52SThomas Petazzoni 
906fddddb52SThomas Petazzoni 	win = mvebu_mbus_find_window(&mbus_state, base, size);
907fddddb52SThomas Petazzoni 	if (win < 0)
908fddddb52SThomas Petazzoni 		return win;
909fddddb52SThomas Petazzoni 
910fddddb52SThomas Petazzoni 	mvebu_mbus_disable_window(&mbus_state, win);
911fddddb52SThomas Petazzoni 	return 0;
912fddddb52SThomas Petazzoni }
913859186e2SPali Rohár EXPORT_SYMBOL_GPL(mvebu_mbus_del_window);
914fddddb52SThomas Petazzoni 
mvebu_mbus_get_pcie_mem_aperture(struct resource * res)91579d94683SEzequiel Garcia void mvebu_mbus_get_pcie_mem_aperture(struct resource *res)
91679d94683SEzequiel Garcia {
91779d94683SEzequiel Garcia 	if (!res)
91879d94683SEzequiel Garcia 		return;
91979d94683SEzequiel Garcia 	*res = mbus_state.pcie_mem_aperture;
92079d94683SEzequiel Garcia }
921859186e2SPali Rohár EXPORT_SYMBOL_GPL(mvebu_mbus_get_pcie_mem_aperture);
92279d94683SEzequiel Garcia 
mvebu_mbus_get_pcie_io_aperture(struct resource * res)92379d94683SEzequiel Garcia void mvebu_mbus_get_pcie_io_aperture(struct resource *res)
92479d94683SEzequiel Garcia {
92579d94683SEzequiel Garcia 	if (!res)
92679d94683SEzequiel Garcia 		return;
92779d94683SEzequiel Garcia 	*res = mbus_state.pcie_io_aperture;
92879d94683SEzequiel Garcia }
929859186e2SPali Rohár EXPORT_SYMBOL_GPL(mvebu_mbus_get_pcie_io_aperture);
93079d94683SEzequiel Garcia 
mvebu_mbus_get_dram_win_info(phys_addr_t phyaddr,u8 * target,u8 * attr)931f2900aceSMarcin Wojtas int mvebu_mbus_get_dram_win_info(phys_addr_t phyaddr, u8 *target, u8 *attr)
932f2900aceSMarcin Wojtas {
933f2900aceSMarcin Wojtas 	const struct mbus_dram_target_info *dram;
934f2900aceSMarcin Wojtas 	int i;
935f2900aceSMarcin Wojtas 
936f2900aceSMarcin Wojtas 	/* Get dram info */
937f2900aceSMarcin Wojtas 	dram = mv_mbus_dram_info();
938f2900aceSMarcin Wojtas 	if (!dram) {
939f2900aceSMarcin Wojtas 		pr_err("missing DRAM information\n");
940f2900aceSMarcin Wojtas 		return -ENODEV;
941f2900aceSMarcin Wojtas 	}
942f2900aceSMarcin Wojtas 
943f2900aceSMarcin Wojtas 	/* Try to find matching DRAM window for phyaddr */
944f2900aceSMarcin Wojtas 	for (i = 0; i < dram->num_cs; i++) {
945f2900aceSMarcin Wojtas 		const struct mbus_dram_window *cs = dram->cs + i;
946f2900aceSMarcin Wojtas 
947f2900aceSMarcin Wojtas 		if (cs->base <= phyaddr &&
948f2900aceSMarcin Wojtas 			phyaddr <= (cs->base + cs->size - 1)) {
949f2900aceSMarcin Wojtas 			*target = dram->mbus_dram_target_id;
950f2900aceSMarcin Wojtas 			*attr = cs->mbus_attr;
951f2900aceSMarcin Wojtas 			return 0;
952f2900aceSMarcin Wojtas 		}
953f2900aceSMarcin Wojtas 	}
954f2900aceSMarcin Wojtas 
95577644ad8SArnd Bergmann 	pr_err("invalid dram address %pa\n", &phyaddr);
956f2900aceSMarcin Wojtas 	return -EINVAL;
957f2900aceSMarcin Wojtas }
958f2900aceSMarcin Wojtas EXPORT_SYMBOL_GPL(mvebu_mbus_get_dram_win_info);
959f2900aceSMarcin Wojtas 
mvebu_mbus_get_io_win_info(phys_addr_t phyaddr,u32 * size,u8 * target,u8 * attr)960f2900aceSMarcin Wojtas int mvebu_mbus_get_io_win_info(phys_addr_t phyaddr, u32 *size, u8 *target,
961f2900aceSMarcin Wojtas 			       u8 *attr)
962f2900aceSMarcin Wojtas {
963f2900aceSMarcin Wojtas 	int win;
964f2900aceSMarcin Wojtas 
965f2900aceSMarcin Wojtas 	for (win = 0; win < mbus_state.soc->num_wins; win++) {
966f2900aceSMarcin Wojtas 		u64 wbase;
967f2900aceSMarcin Wojtas 		int enabled;
968f2900aceSMarcin Wojtas 
969f2900aceSMarcin Wojtas 		mvebu_mbus_read_window(&mbus_state, win, &enabled, &wbase,
970f2900aceSMarcin Wojtas 				       size, target, attr, NULL);
971f2900aceSMarcin Wojtas 
972f2900aceSMarcin Wojtas 		if (!enabled)
973f2900aceSMarcin Wojtas 			continue;
974f2900aceSMarcin Wojtas 
975f2900aceSMarcin Wojtas 		if (wbase <= phyaddr && phyaddr <= wbase + *size)
976f2900aceSMarcin Wojtas 			return win;
977f2900aceSMarcin Wojtas 	}
978f2900aceSMarcin Wojtas 
979f2900aceSMarcin Wojtas 	return -EINVAL;
980f2900aceSMarcin Wojtas }
981f2900aceSMarcin Wojtas EXPORT_SYMBOL_GPL(mvebu_mbus_get_io_win_info);
982f2900aceSMarcin Wojtas 
mvebu_mbus_debugfs_init(void)983fddddb52SThomas Petazzoni static __init int mvebu_mbus_debugfs_init(void)
984fddddb52SThomas Petazzoni {
985fddddb52SThomas Petazzoni 	struct mvebu_mbus_state *s = &mbus_state;
986fddddb52SThomas Petazzoni 
987fddddb52SThomas Petazzoni 	/*
988fddddb52SThomas Petazzoni 	 * If no base has been initialized, doesn't make sense to
989fddddb52SThomas Petazzoni 	 * register the debugfs entries. We may be on a multiplatform
990fddddb52SThomas Petazzoni 	 * kernel that isn't running a Marvell EBU SoC.
991fddddb52SThomas Petazzoni 	 */
992fddddb52SThomas Petazzoni 	if (!s->mbuswins_base)
993fddddb52SThomas Petazzoni 		return 0;
994fddddb52SThomas Petazzoni 
995fddddb52SThomas Petazzoni 	s->debugfs_root = debugfs_create_dir("mvebu-mbus", NULL);
996fddddb52SThomas Petazzoni 	if (s->debugfs_root) {
997fddddb52SThomas Petazzoni 		s->debugfs_sdram = debugfs_create_file("sdram", S_IRUGO,
998fddddb52SThomas Petazzoni 						       s->debugfs_root, NULL,
999fddddb52SThomas Petazzoni 						       &mvebu_sdram_debug_fops);
1000fddddb52SThomas Petazzoni 		s->debugfs_devs = debugfs_create_file("devices", S_IRUGO,
1001fddddb52SThomas Petazzoni 						      s->debugfs_root, NULL,
1002fddddb52SThomas Petazzoni 						      &mvebu_devs_debug_fops);
1003fddddb52SThomas Petazzoni 	}
1004fddddb52SThomas Petazzoni 
1005fddddb52SThomas Petazzoni 	return 0;
1006fddddb52SThomas Petazzoni }
1007fddddb52SThomas Petazzoni fs_initcall(mvebu_mbus_debugfs_init);
1008fddddb52SThomas Petazzoni 
mvebu_mbus_suspend(void)1009a0e89c02SThomas Petazzoni static int mvebu_mbus_suspend(void)
1010a0e89c02SThomas Petazzoni {
1011a0e89c02SThomas Petazzoni 	struct mvebu_mbus_state *s = &mbus_state;
1012a0e89c02SThomas Petazzoni 	int win;
1013a0e89c02SThomas Petazzoni 
1014a0e89c02SThomas Petazzoni 	if (!s->mbusbridge_base)
1015a0e89c02SThomas Petazzoni 		return -ENODEV;
1016a0e89c02SThomas Petazzoni 
1017a0e89c02SThomas Petazzoni 	for (win = 0; win < s->soc->num_wins; win++) {
1018a0e89c02SThomas Petazzoni 		void __iomem *addr = s->mbuswins_base +
1019a0e89c02SThomas Petazzoni 			s->soc->win_cfg_offset(win);
10207fdf3d8aSMichal Mazur 		void __iomem *addr_rmp;
1021a0e89c02SThomas Petazzoni 
1022a0e89c02SThomas Petazzoni 		s->wins[win].base = readl(addr + WIN_BASE_OFF);
1023a0e89c02SThomas Petazzoni 		s->wins[win].ctrl = readl(addr + WIN_CTRL_OFF);
1024a0e89c02SThomas Petazzoni 
10257fdf3d8aSMichal Mazur 		if (!mvebu_mbus_window_is_remappable(s, win))
1026a0e89c02SThomas Petazzoni 			continue;
1027a0e89c02SThomas Petazzoni 
10287fdf3d8aSMichal Mazur 		addr_rmp = s->mbuswins_base +
10297fdf3d8aSMichal Mazur 			s->soc->win_remap_offset(win);
10307fdf3d8aSMichal Mazur 
10317fdf3d8aSMichal Mazur 		s->wins[win].remap_lo = readl(addr_rmp + WIN_REMAP_LO_OFF);
10327fdf3d8aSMichal Mazur 		s->wins[win].remap_hi = readl(addr_rmp + WIN_REMAP_HI_OFF);
1033a0e89c02SThomas Petazzoni 	}
1034a0e89c02SThomas Petazzoni 
1035a0e89c02SThomas Petazzoni 	s->mbus_bridge_ctrl = readl(s->mbusbridge_base +
1036a0e89c02SThomas Petazzoni 				    MBUS_BRIDGE_CTRL_OFF);
1037a0e89c02SThomas Petazzoni 	s->mbus_bridge_base = readl(s->mbusbridge_base +
1038a0e89c02SThomas Petazzoni 				    MBUS_BRIDGE_BASE_OFF);
1039a0e89c02SThomas Petazzoni 
1040a0e89c02SThomas Petazzoni 	return 0;
1041a0e89c02SThomas Petazzoni }
1042a0e89c02SThomas Petazzoni 
mvebu_mbus_resume(void)1043a0e89c02SThomas Petazzoni static void mvebu_mbus_resume(void)
1044a0e89c02SThomas Petazzoni {
1045a0e89c02SThomas Petazzoni 	struct mvebu_mbus_state *s = &mbus_state;
1046a0e89c02SThomas Petazzoni 	int win;
1047a0e89c02SThomas Petazzoni 
1048a0e89c02SThomas Petazzoni 	writel(s->mbus_bridge_ctrl,
1049a0e89c02SThomas Petazzoni 	       s->mbusbridge_base + MBUS_BRIDGE_CTRL_OFF);
1050a0e89c02SThomas Petazzoni 	writel(s->mbus_bridge_base,
1051a0e89c02SThomas Petazzoni 	       s->mbusbridge_base + MBUS_BRIDGE_BASE_OFF);
1052a0e89c02SThomas Petazzoni 
1053a0e89c02SThomas Petazzoni 	for (win = 0; win < s->soc->num_wins; win++) {
1054a0e89c02SThomas Petazzoni 		void __iomem *addr = s->mbuswins_base +
1055a0e89c02SThomas Petazzoni 			s->soc->win_cfg_offset(win);
10567fdf3d8aSMichal Mazur 		void __iomem *addr_rmp;
1057a0e89c02SThomas Petazzoni 
1058a0e89c02SThomas Petazzoni 		writel(s->wins[win].base, addr + WIN_BASE_OFF);
1059a0e89c02SThomas Petazzoni 		writel(s->wins[win].ctrl, addr + WIN_CTRL_OFF);
1060a0e89c02SThomas Petazzoni 
10617fdf3d8aSMichal Mazur 		if (!mvebu_mbus_window_is_remappable(s, win))
1062a0e89c02SThomas Petazzoni 			continue;
1063a0e89c02SThomas Petazzoni 
10647fdf3d8aSMichal Mazur 		addr_rmp = s->mbuswins_base +
10657fdf3d8aSMichal Mazur 			s->soc->win_remap_offset(win);
10667fdf3d8aSMichal Mazur 
10677fdf3d8aSMichal Mazur 		writel(s->wins[win].remap_lo, addr_rmp + WIN_REMAP_LO_OFF);
10687fdf3d8aSMichal Mazur 		writel(s->wins[win].remap_hi, addr_rmp + WIN_REMAP_HI_OFF);
1069a0e89c02SThomas Petazzoni 	}
1070a0e89c02SThomas Petazzoni }
1071a0e89c02SThomas Petazzoni 
1072ac29abf3SBen Dooks static struct syscore_ops mvebu_mbus_syscore_ops = {
1073a0e89c02SThomas Petazzoni 	.suspend	= mvebu_mbus_suspend,
1074a0e89c02SThomas Petazzoni 	.resume		= mvebu_mbus_resume,
1075a0e89c02SThomas Petazzoni };
1076a0e89c02SThomas Petazzoni 
mvebu_mbus_common_init(struct mvebu_mbus_state * mbus,phys_addr_t mbuswins_phys_base,size_t mbuswins_size,phys_addr_t sdramwins_phys_base,size_t sdramwins_size,phys_addr_t mbusbridge_phys_base,size_t mbusbridge_size,bool is_coherent)10776bd6b3cbSEzequiel Garcia static int __init mvebu_mbus_common_init(struct mvebu_mbus_state *mbus,
10786bd6b3cbSEzequiel Garcia 					 phys_addr_t mbuswins_phys_base,
1079fddddb52SThomas Petazzoni 					 size_t mbuswins_size,
1080fddddb52SThomas Petazzoni 					 phys_addr_t sdramwins_phys_base,
1081a0e89c02SThomas Petazzoni 					 size_t sdramwins_size,
1082a0e89c02SThomas Petazzoni 					 phys_addr_t mbusbridge_phys_base,
1083a0b5cd4aSThomas Petazzoni 					 size_t mbusbridge_size,
1084a0b5cd4aSThomas Petazzoni 					 bool is_coherent)
1085fddddb52SThomas Petazzoni {
1086fddddb52SThomas Petazzoni 	int win;
1087fddddb52SThomas Petazzoni 
1088fddddb52SThomas Petazzoni 	mbus->mbuswins_base = ioremap(mbuswins_phys_base, mbuswins_size);
1089fddddb52SThomas Petazzoni 	if (!mbus->mbuswins_base)
1090fddddb52SThomas Petazzoni 		return -ENOMEM;
1091fddddb52SThomas Petazzoni 
1092fddddb52SThomas Petazzoni 	mbus->sdramwins_base = ioremap(sdramwins_phys_base, sdramwins_size);
1093fddddb52SThomas Petazzoni 	if (!mbus->sdramwins_base) {
10942241ed92SChris Packham 		iounmap(mbus->mbuswins_base);
1095fddddb52SThomas Petazzoni 		return -ENOMEM;
1096fddddb52SThomas Petazzoni 	}
1097fddddb52SThomas Petazzoni 
10984749c02bSThomas Petazzoni 	mbus->sdramwins_phys_base = sdramwins_phys_base;
10994749c02bSThomas Petazzoni 
1100a0e89c02SThomas Petazzoni 	if (mbusbridge_phys_base) {
1101a0e89c02SThomas Petazzoni 		mbus->mbusbridge_base = ioremap(mbusbridge_phys_base,
1102a0e89c02SThomas Petazzoni 						mbusbridge_size);
1103a0e89c02SThomas Petazzoni 		if (!mbus->mbusbridge_base) {
1104a0e89c02SThomas Petazzoni 			iounmap(mbus->sdramwins_base);
1105a0e89c02SThomas Petazzoni 			iounmap(mbus->mbuswins_base);
1106a0e89c02SThomas Petazzoni 			return -ENOMEM;
1107a0e89c02SThomas Petazzoni 		}
1108a0e89c02SThomas Petazzoni 	} else
1109a0e89c02SThomas Petazzoni 		mbus->mbusbridge_base = NULL;
1110a0e89c02SThomas Petazzoni 
1111fddddb52SThomas Petazzoni 	for (win = 0; win < mbus->soc->num_wins; win++)
1112fddddb52SThomas Petazzoni 		mvebu_mbus_disable_window(mbus, win);
1113fddddb52SThomas Petazzoni 
1114fddddb52SThomas Petazzoni 	mbus->soc->setup_cpu_target(mbus);
1115bfa1ce5fSThomas Petazzoni 	mvebu_mbus_setup_cpu_target_nooverlap(mbus);
1116fddddb52SThomas Petazzoni 
1117a0b5cd4aSThomas Petazzoni 	if (is_coherent)
1118a0b5cd4aSThomas Petazzoni 		writel(UNIT_SYNC_BARRIER_ALL,
1119a0b5cd4aSThomas Petazzoni 		       mbus->mbuswins_base + UNIT_SYNC_BARRIER_OFF);
1120a0b5cd4aSThomas Petazzoni 
1121a0e89c02SThomas Petazzoni 	register_syscore_ops(&mvebu_mbus_syscore_ops);
1122a0e89c02SThomas Petazzoni 
1123fddddb52SThomas Petazzoni 	return 0;
1124fddddb52SThomas Petazzoni }
11256bd6b3cbSEzequiel Garcia 
mvebu_mbus_init(const char * soc,phys_addr_t mbuswins_phys_base,size_t mbuswins_size,phys_addr_t sdramwins_phys_base,size_t sdramwins_size)11266bd6b3cbSEzequiel Garcia int __init mvebu_mbus_init(const char *soc, phys_addr_t mbuswins_phys_base,
11276bd6b3cbSEzequiel Garcia 			   size_t mbuswins_size,
11286bd6b3cbSEzequiel Garcia 			   phys_addr_t sdramwins_phys_base,
11296bd6b3cbSEzequiel Garcia 			   size_t sdramwins_size)
11306bd6b3cbSEzequiel Garcia {
11316bd6b3cbSEzequiel Garcia 	const struct of_device_id *of_id;
11326bd6b3cbSEzequiel Garcia 
11337663cfd3SDan Carpenter 	for (of_id = of_mvebu_mbus_ids; of_id->compatible[0]; of_id++)
11346bd6b3cbSEzequiel Garcia 		if (!strcmp(of_id->compatible, soc))
11356bd6b3cbSEzequiel Garcia 			break;
11366bd6b3cbSEzequiel Garcia 
11377663cfd3SDan Carpenter 	if (!of_id->compatible[0]) {
11386bd6b3cbSEzequiel Garcia 		pr_err("could not find a matching SoC family\n");
11396bd6b3cbSEzequiel Garcia 		return -ENODEV;
11406bd6b3cbSEzequiel Garcia 	}
11416bd6b3cbSEzequiel Garcia 
11426bd6b3cbSEzequiel Garcia 	mbus_state.soc = of_id->data;
11436bd6b3cbSEzequiel Garcia 
11446bd6b3cbSEzequiel Garcia 	return mvebu_mbus_common_init(&mbus_state,
11456bd6b3cbSEzequiel Garcia 			mbuswins_phys_base,
11466bd6b3cbSEzequiel Garcia 			mbuswins_size,
11476bd6b3cbSEzequiel Garcia 			sdramwins_phys_base,
1148a0b5cd4aSThomas Petazzoni 			sdramwins_size, 0, 0, false);
11496bd6b3cbSEzequiel Garcia }
11506839cfa8SEzequiel Garcia 
11516839cfa8SEzequiel Garcia #ifdef CONFIG_OF
1152bb24cab3SEzequiel Garcia /*
1153bb24cab3SEzequiel Garcia  * The window IDs in the ranges DT property have the following format:
1154bb24cab3SEzequiel Garcia  *  - bits 28 to 31: MBus custom field
1155bb24cab3SEzequiel Garcia  *  - bits 24 to 27: window target ID
1156bb24cab3SEzequiel Garcia  *  - bits 16 to 23: window attribute ID
1157bb24cab3SEzequiel Garcia  *  - bits  0 to 15: unused
1158bb24cab3SEzequiel Garcia  */
1159bb24cab3SEzequiel Garcia #define CUSTOM(id) (((id) & 0xF0000000) >> 24)
1160bb24cab3SEzequiel Garcia #define TARGET(id) (((id) & 0x0F000000) >> 24)
1161bb24cab3SEzequiel Garcia #define ATTR(id)   (((id) & 0x00FF0000) >> 16)
1162bb24cab3SEzequiel Garcia 
mbus_dt_setup_win(struct mvebu_mbus_state * mbus,u32 base,u32 size,u8 target,u8 attr)1163bb24cab3SEzequiel Garcia static int __init mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
1164bb24cab3SEzequiel Garcia 				    u32 base, u32 size,
1165bb24cab3SEzequiel Garcia 				    u8 target, u8 attr)
1166bb24cab3SEzequiel Garcia {
1167bb24cab3SEzequiel Garcia 	if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
1168ed843a7dSThomas Petazzoni 		pr_err("cannot add window '%04x:%04x', conflicts with another window\n",
1169ed843a7dSThomas Petazzoni 		       target, attr);
1170bb24cab3SEzequiel Garcia 		return -EBUSY;
1171bb24cab3SEzequiel Garcia 	}
1172bb24cab3SEzequiel Garcia 
1173bb24cab3SEzequiel Garcia 	if (mvebu_mbus_alloc_window(mbus, base, size, MVEBU_MBUS_NO_REMAP,
1174bb24cab3SEzequiel Garcia 				    target, attr)) {
1175ed843a7dSThomas Petazzoni 		pr_err("cannot add window '%04x:%04x', too many windows\n",
1176ed843a7dSThomas Petazzoni 		       target, attr);
1177bb24cab3SEzequiel Garcia 		return -ENOMEM;
1178bb24cab3SEzequiel Garcia 	}
1179bb24cab3SEzequiel Garcia 	return 0;
1180bb24cab3SEzequiel Garcia }
1181bb24cab3SEzequiel Garcia 
mbus_dt_setup(struct mvebu_mbus_state * mbus,struct device_node * np)1182bb24cab3SEzequiel Garcia static int __init mbus_dt_setup(struct mvebu_mbus_state *mbus,
1183bb24cab3SEzequiel Garcia 				struct device_node *np)
1184bb24cab3SEzequiel Garcia {
1185*6bb1504dSRob Herring 	int ret;
1186*6bb1504dSRob Herring 	struct of_range_parser parser;
1187*6bb1504dSRob Herring 	struct of_range range;
1188bb24cab3SEzequiel Garcia 
1189*6bb1504dSRob Herring 	ret = of_range_parser_init(&parser, np);
1190bb24cab3SEzequiel Garcia 	if (ret < 0)
1191*6bb1504dSRob Herring 		return 0;
1192bb24cab3SEzequiel Garcia 
1193*6bb1504dSRob Herring 	for_each_of_range(&parser, &range) {
1194*6bb1504dSRob Herring 		u32 windowid = upper_32_bits(range.bus_addr);
1195bb24cab3SEzequiel Garcia 		u8 target, attr;
1196bb24cab3SEzequiel Garcia 
1197bb24cab3SEzequiel Garcia 		/*
1198bb24cab3SEzequiel Garcia 		 * An entry with a non-zero custom field do not
1199bb24cab3SEzequiel Garcia 		 * correspond to a static window, so skip it.
1200bb24cab3SEzequiel Garcia 		 */
1201bb24cab3SEzequiel Garcia 		if (CUSTOM(windowid))
1202bb24cab3SEzequiel Garcia 			continue;
1203bb24cab3SEzequiel Garcia 
1204bb24cab3SEzequiel Garcia 		target = TARGET(windowid);
1205bb24cab3SEzequiel Garcia 		attr = ATTR(windowid);
1206bb24cab3SEzequiel Garcia 
1207*6bb1504dSRob Herring 		ret = mbus_dt_setup_win(mbus, range.cpu_addr, range.size, target, attr);
1208bb24cab3SEzequiel Garcia 		if (ret < 0)
1209bb24cab3SEzequiel Garcia 			return ret;
1210bb24cab3SEzequiel Garcia 	}
1211bb24cab3SEzequiel Garcia 	return 0;
1212bb24cab3SEzequiel Garcia }
1213bb24cab3SEzequiel Garcia 
mvebu_mbus_get_pcie_resources(struct device_node * np,struct resource * mem,struct resource * io)121479d94683SEzequiel Garcia static void __init mvebu_mbus_get_pcie_resources(struct device_node *np,
121579d94683SEzequiel Garcia 						 struct resource *mem,
121679d94683SEzequiel Garcia 						 struct resource *io)
121779d94683SEzequiel Garcia {
121879d94683SEzequiel Garcia 	u32 reg[2];
121979d94683SEzequiel Garcia 	int ret;
122079d94683SEzequiel Garcia 
122179d94683SEzequiel Garcia 	/*
12228553bcadSJason Gunthorpe 	 * These are optional, so we make sure that resource_size(x) will
12238553bcadSJason Gunthorpe 	 * return 0.
122479d94683SEzequiel Garcia 	 */
122579d94683SEzequiel Garcia 	memset(mem, 0, sizeof(struct resource));
12268553bcadSJason Gunthorpe 	mem->end = -1;
122779d94683SEzequiel Garcia 	memset(io, 0, sizeof(struct resource));
12288553bcadSJason Gunthorpe 	io->end = -1;
122979d94683SEzequiel Garcia 
123079d94683SEzequiel Garcia 	ret = of_property_read_u32_array(np, "pcie-mem-aperture", reg, ARRAY_SIZE(reg));
123179d94683SEzequiel Garcia 	if (!ret) {
123279d94683SEzequiel Garcia 		mem->start = reg[0];
1233a723e752SJason Gunthorpe 		mem->end = mem->start + reg[1] - 1;
123479d94683SEzequiel Garcia 		mem->flags = IORESOURCE_MEM;
123579d94683SEzequiel Garcia 	}
123679d94683SEzequiel Garcia 
123779d94683SEzequiel Garcia 	ret = of_property_read_u32_array(np, "pcie-io-aperture", reg, ARRAY_SIZE(reg));
123879d94683SEzequiel Garcia 	if (!ret) {
123979d94683SEzequiel Garcia 		io->start = reg[0];
1240a723e752SJason Gunthorpe 		io->end = io->start + reg[1] - 1;
124179d94683SEzequiel Garcia 		io->flags = IORESOURCE_IO;
124279d94683SEzequiel Garcia 	}
124379d94683SEzequiel Garcia }
124479d94683SEzequiel Garcia 
mvebu_mbus_dt_init(bool is_coherent)12455686a1e5SThomas Petazzoni int __init mvebu_mbus_dt_init(bool is_coherent)
12466839cfa8SEzequiel Garcia {
1247a0e89c02SThomas Petazzoni 	struct resource mbuswins_res, sdramwins_res, mbusbridge_res;
12486839cfa8SEzequiel Garcia 	struct device_node *np, *controller;
12496839cfa8SEzequiel Garcia 	const struct of_device_id *of_id;
12506839cfa8SEzequiel Garcia 	const __be32 *prop;
12516839cfa8SEzequiel Garcia 	int ret;
12526839cfa8SEzequiel Garcia 
1253087a4ab2SJosh Cartwright 	np = of_find_matching_node_and_match(NULL, of_mvebu_mbus_ids, &of_id);
12546839cfa8SEzequiel Garcia 	if (!np) {
12556839cfa8SEzequiel Garcia 		pr_err("could not find a matching SoC family\n");
12566839cfa8SEzequiel Garcia 		return -ENODEV;
12576839cfa8SEzequiel Garcia 	}
12586839cfa8SEzequiel Garcia 
12596839cfa8SEzequiel Garcia 	mbus_state.soc = of_id->data;
12606839cfa8SEzequiel Garcia 
12616839cfa8SEzequiel Garcia 	prop = of_get_property(np, "controller", NULL);
12626839cfa8SEzequiel Garcia 	if (!prop) {
12636839cfa8SEzequiel Garcia 		pr_err("required 'controller' property missing\n");
12646839cfa8SEzequiel Garcia 		return -EINVAL;
12656839cfa8SEzequiel Garcia 	}
12666839cfa8SEzequiel Garcia 
12676839cfa8SEzequiel Garcia 	controller = of_find_node_by_phandle(be32_to_cpup(prop));
12686839cfa8SEzequiel Garcia 	if (!controller) {
12696839cfa8SEzequiel Garcia 		pr_err("could not find an 'mbus-controller' node\n");
12706839cfa8SEzequiel Garcia 		return -ENODEV;
12716839cfa8SEzequiel Garcia 	}
12726839cfa8SEzequiel Garcia 
12736839cfa8SEzequiel Garcia 	if (of_address_to_resource(controller, 0, &mbuswins_res)) {
12746839cfa8SEzequiel Garcia 		pr_err("cannot get MBUS register address\n");
12756839cfa8SEzequiel Garcia 		return -EINVAL;
12766839cfa8SEzequiel Garcia 	}
12776839cfa8SEzequiel Garcia 
12786839cfa8SEzequiel Garcia 	if (of_address_to_resource(controller, 1, &sdramwins_res)) {
12796839cfa8SEzequiel Garcia 		pr_err("cannot get SDRAM register address\n");
12806839cfa8SEzequiel Garcia 		return -EINVAL;
12816839cfa8SEzequiel Garcia 	}
12826839cfa8SEzequiel Garcia 
1283a0e89c02SThomas Petazzoni 	/*
1284a0e89c02SThomas Petazzoni 	 * Set the resource to 0 so that it can be left unmapped by
1285a0e89c02SThomas Petazzoni 	 * mvebu_mbus_common_init() if the DT doesn't carry the
1286a0e89c02SThomas Petazzoni 	 * necessary information. This is needed to preserve backward
1287a0e89c02SThomas Petazzoni 	 * compatibility.
1288a0e89c02SThomas Petazzoni 	 */
1289a0e89c02SThomas Petazzoni 	memset(&mbusbridge_res, 0, sizeof(mbusbridge_res));
1290a0e89c02SThomas Petazzoni 
1291a0e89c02SThomas Petazzoni 	if (mbus_state.soc->has_mbus_bridge) {
1292a0e89c02SThomas Petazzoni 		if (of_address_to_resource(controller, 2, &mbusbridge_res))
1293a0e89c02SThomas Petazzoni 			pr_warn(FW_WARN "deprecated mbus-mvebu Device Tree, suspend/resume will not work\n");
1294a0e89c02SThomas Petazzoni 	}
1295a0e89c02SThomas Petazzoni 
12965686a1e5SThomas Petazzoni 	mbus_state.hw_io_coherency = is_coherent;
12975686a1e5SThomas Petazzoni 
129879d94683SEzequiel Garcia 	/* Get optional pcie-{mem,io}-aperture properties */
129979d94683SEzequiel Garcia 	mvebu_mbus_get_pcie_resources(np, &mbus_state.pcie_mem_aperture,
130079d94683SEzequiel Garcia 					  &mbus_state.pcie_io_aperture);
130179d94683SEzequiel Garcia 
13026839cfa8SEzequiel Garcia 	ret = mvebu_mbus_common_init(&mbus_state,
13036839cfa8SEzequiel Garcia 				     mbuswins_res.start,
13046839cfa8SEzequiel Garcia 				     resource_size(&mbuswins_res),
13056839cfa8SEzequiel Garcia 				     sdramwins_res.start,
1306a0e89c02SThomas Petazzoni 				     resource_size(&sdramwins_res),
1307a0e89c02SThomas Petazzoni 				     mbusbridge_res.start,
1308a0b5cd4aSThomas Petazzoni 				     resource_size(&mbusbridge_res),
1309a0b5cd4aSThomas Petazzoni 				     is_coherent);
1310bb24cab3SEzequiel Garcia 	if (ret)
13116839cfa8SEzequiel Garcia 		return ret;
1312bb24cab3SEzequiel Garcia 
1313bb24cab3SEzequiel Garcia 	/* Setup statically declared windows in the DT */
1314bb24cab3SEzequiel Garcia 	return mbus_dt_setup(&mbus_state, np);
13156839cfa8SEzequiel Garcia }
13166839cfa8SEzequiel Garcia #endif
1317