2 * Address map functions for Marvell EBU SoCs (Kirkwood, Armada
3 * 370/XP, Dove, Orion5x and MV78xx0)
5 * This file is licensed under the terms of the GNU General Public
6 * License version 2. This program is licensed "as is" without any
7 * warranty of any kind, whether express or implied.
9 * The Marvell EBU SoCs have a configurable physical address space:
10 * the physical address at which certain devices (PCIe, NOR, NAND,
11 * etc.) sit can be configured. The configuration takes place through
12 * two sets of registers:
14 * - One to configure the access of the CPU to the devices. Depending
15 * on the families, there are between 8 and 20 configurable windows,
16 * each can be use to create a physical memory window that maps to a
17 * specific device. Devices are identified by a tuple (target,
20 * - One to configure the access to the CPU to the SDRAM. There are
21 * either 2 (for Dove) or 4 (for other families) windows to map the
22 * SDRAM into the physical address space.
26 * - Reads out the SDRAM address decoding windows at initialization
27 * time, and fills the mvebu_mbus_dram_info structure with these
28 * informations. The exported function mv_mbus_dram_info() allow
29 * device drivers to get those informations related to the SDRAM
30 * address decoding windows. This is because devices also have their
31 * own windows (configured through registers that are part of each
32 * device register space), and therefore the drivers for Marvell
33 * devices have to configure those device -> SDRAM windows to ensure
34 * that DMA works properly.
36 * - Provides an API for platform code or device drivers to
37 * dynamically add or remove address decoding windows for the CPU ->
38 * device accesses. This API is mvebu_mbus_add_window_by_id(),
39 * mvebu_mbus_add_window_remap_by_id() and
40 * mvebu_mbus_del_window().
42 * - Provides a debugfs interface in /sys/kernel/debug/mvebu-mbus/ to
43 * see the list of CPU -> SDRAM windows and their configuration
44 * (file 'sdram') and the list of CPU -> devices windows and their
45 * configuration (file 'devices').
48 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/mbus.h>
55 #include <linux/ioport.h>
57 #include <linux/of_address.h>
58 #include <linux/debugfs.h>
59 #include <linux/log2.h>
60 #include <linux/syscore_ops.h>
63 * DDR target is the same on all platforms.
68 * CPU Address Decode Windows registers
70 #define WIN_CTRL_OFF 0x0000
71 #define WIN_CTRL_ENABLE BIT(0)
72 #define WIN_CTRL_TGT_MASK 0xf0
73 #define WIN_CTRL_TGT_SHIFT 4
74 #define WIN_CTRL_ATTR_MASK 0xff00
75 #define WIN_CTRL_ATTR_SHIFT 8
76 #define WIN_CTRL_SIZE_MASK 0xffff0000
77 #define WIN_CTRL_SIZE_SHIFT 16
78 #define WIN_BASE_OFF 0x0004
79 #define WIN_BASE_LOW 0xffff0000
80 #define WIN_BASE_HIGH 0xf
81 #define WIN_REMAP_LO_OFF 0x0008
82 #define WIN_REMAP_LOW 0xffff0000
83 #define WIN_REMAP_HI_OFF 0x000c
85 #define ATTR_HW_COHERENCY (0x1 << 4)
87 #define DDR_BASE_CS_OFF(n) (0x0000 + ((n) << 3))
88 #define DDR_BASE_CS_HIGH_MASK 0xf
89 #define DDR_BASE_CS_LOW_MASK 0xff000000
90 #define DDR_SIZE_CS_OFF(n) (0x0004 + ((n) << 3))
91 #define DDR_SIZE_ENABLED BIT(0)
92 #define DDR_SIZE_CS_MASK 0x1c
93 #define DDR_SIZE_CS_SHIFT 2
94 #define DDR_SIZE_MASK 0xff000000
96 #define DOVE_DDR_BASE_CS_OFF(n) ((n) << 4)
98 /* Relative to mbusbridge_base */
99 #define MBUS_BRIDGE_CTRL_OFF 0x0
100 #define MBUS_BRIDGE_BASE_OFF 0x4
102 /* Maximum number of windows, for all known platforms */
103 #define MBUS_WINS_MAX 20
105 struct mvebu_mbus_state
;
107 struct mvebu_mbus_soc_data
{
108 unsigned int num_wins
;
109 unsigned int num_remappable_wins
;
110 bool has_mbus_bridge
;
111 unsigned int (*win_cfg_offset
)(const int win
);
112 void (*setup_cpu_target
)(struct mvebu_mbus_state
*s
);
113 int (*save_cpu_target
)(struct mvebu_mbus_state
*s
,
115 int (*show_cpu_target
)(struct mvebu_mbus_state
*s
,
116 struct seq_file
*seq
, void *v
);
120 * Used to store the state of one MBus window accross suspend/resume.
122 struct mvebu_mbus_win_data
{
129 struct mvebu_mbus_state
{
130 void __iomem
*mbuswins_base
;
131 void __iomem
*sdramwins_base
;
132 void __iomem
*mbusbridge_base
;
133 phys_addr_t sdramwins_phys_base
;
134 struct dentry
*debugfs_root
;
135 struct dentry
*debugfs_sdram
;
136 struct dentry
*debugfs_devs
;
137 struct resource pcie_mem_aperture
;
138 struct resource pcie_io_aperture
;
139 const struct mvebu_mbus_soc_data
*soc
;
142 /* Used during suspend/resume */
143 u32 mbus_bridge_ctrl
;
144 u32 mbus_bridge_base
;
145 struct mvebu_mbus_win_data wins
[MBUS_WINS_MAX
];
148 static struct mvebu_mbus_state mbus_state
;
150 static struct mbus_dram_target_info mvebu_mbus_dram_info
;
151 const struct mbus_dram_target_info
*mv_mbus_dram_info(void)
153 return &mvebu_mbus_dram_info
;
155 EXPORT_SYMBOL_GPL(mv_mbus_dram_info
);
158 * Functions to manipulate the address decoding windows
161 static void mvebu_mbus_read_window(struct mvebu_mbus_state
*mbus
,
162 int win
, int *enabled
, u64
*base
,
163 u32
*size
, u8
*target
, u8
*attr
,
166 void __iomem
*addr
= mbus
->mbuswins_base
+
167 mbus
->soc
->win_cfg_offset(win
);
168 u32 basereg
= readl(addr
+ WIN_BASE_OFF
);
169 u32 ctrlreg
= readl(addr
+ WIN_CTRL_OFF
);
171 if (!(ctrlreg
& WIN_CTRL_ENABLE
)) {
177 *base
= ((u64
)basereg
& WIN_BASE_HIGH
) << 32;
178 *base
|= (basereg
& WIN_BASE_LOW
);
179 *size
= (ctrlreg
| ~WIN_CTRL_SIZE_MASK
) + 1;
182 *target
= (ctrlreg
& WIN_CTRL_TGT_MASK
) >> WIN_CTRL_TGT_SHIFT
;
185 *attr
= (ctrlreg
& WIN_CTRL_ATTR_MASK
) >> WIN_CTRL_ATTR_SHIFT
;
188 if (win
< mbus
->soc
->num_remappable_wins
) {
189 u32 remap_low
= readl(addr
+ WIN_REMAP_LO_OFF
);
190 u32 remap_hi
= readl(addr
+ WIN_REMAP_HI_OFF
);
191 *remap
= ((u64
)remap_hi
<< 32) | remap_low
;
197 static void mvebu_mbus_disable_window(struct mvebu_mbus_state
*mbus
,
202 addr
= mbus
->mbuswins_base
+ mbus
->soc
->win_cfg_offset(win
);
204 writel(0, addr
+ WIN_BASE_OFF
);
205 writel(0, addr
+ WIN_CTRL_OFF
);
206 if (win
< mbus
->soc
->num_remappable_wins
) {
207 writel(0, addr
+ WIN_REMAP_LO_OFF
);
208 writel(0, addr
+ WIN_REMAP_HI_OFF
);
212 /* Checks whether the given window number is available */
213 static int mvebu_mbus_window_is_free(struct mvebu_mbus_state
*mbus
,
216 void __iomem
*addr
= mbus
->mbuswins_base
+
217 mbus
->soc
->win_cfg_offset(win
);
218 u32 ctrl
= readl(addr
+ WIN_CTRL_OFF
);
219 return !(ctrl
& WIN_CTRL_ENABLE
);
223 * Checks whether the given (base, base+size) area doesn't overlap an
226 static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state
*mbus
,
227 phys_addr_t base
, size_t size
,
230 u64 end
= (u64
)base
+ size
;
233 for (win
= 0; win
< mbus
->soc
->num_wins
; win
++) {
239 mvebu_mbus_read_window(mbus
, win
,
240 &enabled
, &wbase
, &wsize
,
241 &wtarget
, &wattr
, NULL
);
246 wend
= wbase
+ wsize
;
249 * Check if the current window overlaps with the
250 * proposed physical range
252 if ((u64
)base
< wend
&& end
> wbase
)
259 static int mvebu_mbus_find_window(struct mvebu_mbus_state
*mbus
,
260 phys_addr_t base
, size_t size
)
264 for (win
= 0; win
< mbus
->soc
->num_wins
; win
++) {
269 mvebu_mbus_read_window(mbus
, win
,
270 &enabled
, &wbase
, &wsize
,
276 if (base
== wbase
&& size
== wsize
)
283 static int mvebu_mbus_setup_window(struct mvebu_mbus_state
*mbus
,
284 int win
, phys_addr_t base
, size_t size
,
285 phys_addr_t remap
, u8 target
,
288 void __iomem
*addr
= mbus
->mbuswins_base
+
289 mbus
->soc
->win_cfg_offset(win
);
290 u32 ctrl
, remap_addr
;
292 if (!is_power_of_2(size
)) {
293 WARN(true, "Invalid MBus window size: 0x%zx\n", size
);
297 if ((base
& (phys_addr_t
)(size
- 1)) != 0) {
298 WARN(true, "Invalid MBus base/size: %pa len 0x%zx\n", &base
,
303 ctrl
= ((size
- 1) & WIN_CTRL_SIZE_MASK
) |
304 (attr
<< WIN_CTRL_ATTR_SHIFT
) |
305 (target
<< WIN_CTRL_TGT_SHIFT
) |
308 writel(base
& WIN_BASE_LOW
, addr
+ WIN_BASE_OFF
);
309 writel(ctrl
, addr
+ WIN_CTRL_OFF
);
310 if (win
< mbus
->soc
->num_remappable_wins
) {
311 if (remap
== MVEBU_MBUS_NO_REMAP
)
315 writel(remap_addr
& WIN_REMAP_LOW
, addr
+ WIN_REMAP_LO_OFF
);
316 writel(0, addr
+ WIN_REMAP_HI_OFF
);
322 static int mvebu_mbus_alloc_window(struct mvebu_mbus_state
*mbus
,
323 phys_addr_t base
, size_t size
,
324 phys_addr_t remap
, u8 target
,
329 if (remap
== MVEBU_MBUS_NO_REMAP
) {
330 for (win
= mbus
->soc
->num_remappable_wins
;
331 win
< mbus
->soc
->num_wins
; win
++)
332 if (mvebu_mbus_window_is_free(mbus
, win
))
333 return mvebu_mbus_setup_window(mbus
, win
, base
,
339 for (win
= 0; win
< mbus
->soc
->num_wins
; win
++)
340 if (mvebu_mbus_window_is_free(mbus
, win
))
341 return mvebu_mbus_setup_window(mbus
, win
, base
, size
,
342 remap
, target
, attr
);
351 /* Common function used for Dove, Kirkwood, Armada 370/XP and Orion 5x */
352 static int mvebu_sdram_debug_show_orion(struct mvebu_mbus_state
*mbus
,
353 struct seq_file
*seq
, void *v
)
357 for (i
= 0; i
< 4; i
++) {
358 u32 basereg
= readl(mbus
->sdramwins_base
+ DDR_BASE_CS_OFF(i
));
359 u32 sizereg
= readl(mbus
->sdramwins_base
+ DDR_SIZE_CS_OFF(i
));
363 if (!(sizereg
& DDR_SIZE_ENABLED
)) {
364 seq_printf(seq
, "[%d] disabled\n", i
);
368 base
= ((u64
)basereg
& DDR_BASE_CS_HIGH_MASK
) << 32;
369 base
|= basereg
& DDR_BASE_CS_LOW_MASK
;
370 size
= (sizereg
| ~DDR_SIZE_MASK
);
372 seq_printf(seq
, "[%d] %016llx - %016llx : cs%d\n",
373 i
, (unsigned long long)base
,
374 (unsigned long long)base
+ size
+ 1,
375 (sizereg
& DDR_SIZE_CS_MASK
) >> DDR_SIZE_CS_SHIFT
);
381 /* Special function for Dove */
382 static int mvebu_sdram_debug_show_dove(struct mvebu_mbus_state
*mbus
,
383 struct seq_file
*seq
, void *v
)
387 for (i
= 0; i
< 2; i
++) {
388 u32 map
= readl(mbus
->sdramwins_base
+ DOVE_DDR_BASE_CS_OFF(i
));
393 seq_printf(seq
, "[%d] disabled\n", i
);
397 base
= map
& 0xff800000;
398 size
= 0x100000 << (((map
& 0x000f0000) >> 16) - 4);
400 seq_printf(seq
, "[%d] %016llx - %016llx : cs%d\n",
401 i
, (unsigned long long)base
,
402 (unsigned long long)base
+ size
, i
);
408 static int mvebu_sdram_debug_show(struct seq_file
*seq
, void *v
)
410 struct mvebu_mbus_state
*mbus
= &mbus_state
;
411 return mbus
->soc
->show_cpu_target(mbus
, seq
, v
);
414 static int mvebu_sdram_debug_open(struct inode
*inode
, struct file
*file
)
416 return single_open(file
, mvebu_sdram_debug_show
, inode
->i_private
);
419 static const struct file_operations mvebu_sdram_debug_fops
= {
420 .open
= mvebu_sdram_debug_open
,
423 .release
= single_release
,
426 static int mvebu_devs_debug_show(struct seq_file
*seq
, void *v
)
428 struct mvebu_mbus_state
*mbus
= &mbus_state
;
431 for (win
= 0; win
< mbus
->soc
->num_wins
; win
++) {
437 mvebu_mbus_read_window(mbus
, win
,
438 &enabled
, &wbase
, &wsize
,
439 &wtarget
, &wattr
, &wremap
);
442 seq_printf(seq
, "[%02d] disabled\n", win
);
446 seq_printf(seq
, "[%02d] %016llx - %016llx : %04x:%04x",
447 win
, (unsigned long long)wbase
,
448 (unsigned long long)(wbase
+ wsize
), wtarget
, wattr
);
450 if (!is_power_of_2(wsize
) ||
451 ((wbase
& (u64
)(wsize
- 1)) != 0))
452 seq_puts(seq
, " (Invalid base/size!!)");
454 if (win
< mbus
->soc
->num_remappable_wins
) {
455 seq_printf(seq
, " (remap %016llx)\n",
456 (unsigned long long)wremap
);
458 seq_printf(seq
, "\n");
464 static int mvebu_devs_debug_open(struct inode
*inode
, struct file
*file
)
466 return single_open(file
, mvebu_devs_debug_show
, inode
->i_private
);
469 static const struct file_operations mvebu_devs_debug_fops
= {
470 .open
= mvebu_devs_debug_open
,
473 .release
= single_release
,
477 * SoC-specific functions and definitions
480 static unsigned int orion_mbus_win_offset(int win
)
485 static unsigned int armada_370_xp_mbus_win_offset(int win
)
487 /* The register layout is a bit annoying and the below code
488 * tries to cope with it.
489 * - At offset 0x0, there are the registers for the first 8
490 * windows, with 4 registers of 32 bits per window (ctrl,
491 * base, remap low, remap high)
492 * - Then at offset 0x80, there is a hole of 0x10 bytes for
493 * the internal registers base address and internal units
494 * sync barrier register.
495 * - Then at offset 0x90, there the registers for 12
496 * windows, with only 2 registers of 32 bits per window
502 return 0x90 + ((win
- 8) << 3);
505 static unsigned int mv78xx0_mbus_win_offset(int win
)
510 return 0x900 + ((win
- 8) << 4);
514 mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state
*mbus
)
519 mvebu_mbus_dram_info
.mbus_dram_target_id
= TARGET_DDR
;
521 for (i
= 0, cs
= 0; i
< 4; i
++) {
522 u32 base
= readl(mbus
->sdramwins_base
+ DDR_BASE_CS_OFF(i
));
523 u32 size
= readl(mbus
->sdramwins_base
+ DDR_SIZE_CS_OFF(i
));
526 * We only take care of entries for which the chip
527 * select is enabled, and that don't have high base
528 * address bits set (devices can only access the first
529 * 32 bits of the memory).
531 if ((size
& DDR_SIZE_ENABLED
) &&
532 !(base
& DDR_BASE_CS_HIGH_MASK
)) {
533 struct mbus_dram_window
*w
;
535 w
= &mvebu_mbus_dram_info
.cs
[cs
++];
537 w
->mbus_attr
= 0xf & ~(1 << i
);
538 if (mbus
->hw_io_coherency
)
539 w
->mbus_attr
|= ATTR_HW_COHERENCY
;
540 w
->base
= base
& DDR_BASE_CS_LOW_MASK
;
541 w
->size
= (size
| ~DDR_SIZE_MASK
) + 1;
544 mvebu_mbus_dram_info
.num_cs
= cs
;
548 mvebu_mbus_default_save_cpu_target(struct mvebu_mbus_state
*mbus
,
553 for (i
= 0; i
< 4; i
++) {
554 u32 base
= readl(mbus
->sdramwins_base
+ DDR_BASE_CS_OFF(i
));
555 u32 size
= readl(mbus
->sdramwins_base
+ DDR_SIZE_CS_OFF(i
));
557 writel(mbus
->sdramwins_phys_base
+ DDR_BASE_CS_OFF(i
),
559 writel(base
, store_addr
++);
560 writel(mbus
->sdramwins_phys_base
+ DDR_SIZE_CS_OFF(i
),
562 writel(size
, store_addr
++);
565 /* We've written 16 words to the store address */
570 mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state
*mbus
)
575 mvebu_mbus_dram_info
.mbus_dram_target_id
= TARGET_DDR
;
577 for (i
= 0, cs
= 0; i
< 2; i
++) {
578 u32 map
= readl(mbus
->sdramwins_base
+ DOVE_DDR_BASE_CS_OFF(i
));
581 * Chip select enabled?
584 struct mbus_dram_window
*w
;
586 w
= &mvebu_mbus_dram_info
.cs
[cs
++];
588 w
->mbus_attr
= 0; /* CS address decoding done inside */
589 /* the DDR controller, no need to */
590 /* provide attributes */
591 w
->base
= map
& 0xff800000;
592 w
->size
= 0x100000 << (((map
& 0x000f0000) >> 16) - 4);
596 mvebu_mbus_dram_info
.num_cs
= cs
;
600 mvebu_mbus_dove_save_cpu_target(struct mvebu_mbus_state
*mbus
,
605 for (i
= 0; i
< 2; i
++) {
606 u32 map
= readl(mbus
->sdramwins_base
+ DOVE_DDR_BASE_CS_OFF(i
));
608 writel(mbus
->sdramwins_phys_base
+ DOVE_DDR_BASE_CS_OFF(i
),
610 writel(map
, store_addr
++);
613 /* We've written 4 words to the store address */
617 int mvebu_mbus_save_cpu_target(u32
*store_addr
)
619 return mbus_state
.soc
->save_cpu_target(&mbus_state
, store_addr
);
622 static const struct mvebu_mbus_soc_data armada_370_xp_mbus_data
= {
624 .num_remappable_wins
= 8,
625 .has_mbus_bridge
= true,
626 .win_cfg_offset
= armada_370_xp_mbus_win_offset
,
627 .save_cpu_target
= mvebu_mbus_default_save_cpu_target
,
628 .setup_cpu_target
= mvebu_mbus_default_setup_cpu_target
,
629 .show_cpu_target
= mvebu_sdram_debug_show_orion
,
632 static const struct mvebu_mbus_soc_data kirkwood_mbus_data
= {
634 .num_remappable_wins
= 4,
635 .win_cfg_offset
= orion_mbus_win_offset
,
636 .save_cpu_target
= mvebu_mbus_default_save_cpu_target
,
637 .setup_cpu_target
= mvebu_mbus_default_setup_cpu_target
,
638 .show_cpu_target
= mvebu_sdram_debug_show_orion
,
641 static const struct mvebu_mbus_soc_data dove_mbus_data
= {
643 .num_remappable_wins
= 4,
644 .win_cfg_offset
= orion_mbus_win_offset
,
645 .save_cpu_target
= mvebu_mbus_dove_save_cpu_target
,
646 .setup_cpu_target
= mvebu_mbus_dove_setup_cpu_target
,
647 .show_cpu_target
= mvebu_sdram_debug_show_dove
,
651 * Some variants of Orion5x have 4 remappable windows, some other have
654 static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data
= {
656 .num_remappable_wins
= 4,
657 .win_cfg_offset
= orion_mbus_win_offset
,
658 .save_cpu_target
= mvebu_mbus_default_save_cpu_target
,
659 .setup_cpu_target
= mvebu_mbus_default_setup_cpu_target
,
660 .show_cpu_target
= mvebu_sdram_debug_show_orion
,
663 static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data
= {
665 .num_remappable_wins
= 2,
666 .win_cfg_offset
= orion_mbus_win_offset
,
667 .save_cpu_target
= mvebu_mbus_default_save_cpu_target
,
668 .setup_cpu_target
= mvebu_mbus_default_setup_cpu_target
,
669 .show_cpu_target
= mvebu_sdram_debug_show_orion
,
672 static const struct mvebu_mbus_soc_data mv78xx0_mbus_data
= {
674 .num_remappable_wins
= 8,
675 .win_cfg_offset
= mv78xx0_mbus_win_offset
,
676 .save_cpu_target
= mvebu_mbus_default_save_cpu_target
,
677 .setup_cpu_target
= mvebu_mbus_default_setup_cpu_target
,
678 .show_cpu_target
= mvebu_sdram_debug_show_orion
,
681 static const struct of_device_id of_mvebu_mbus_ids
[] = {
682 { .compatible
= "marvell,armada370-mbus",
683 .data
= &armada_370_xp_mbus_data
, },
684 { .compatible
= "marvell,armadaxp-mbus",
685 .data
= &armada_370_xp_mbus_data
, },
686 { .compatible
= "marvell,kirkwood-mbus",
687 .data
= &kirkwood_mbus_data
, },
688 { .compatible
= "marvell,dove-mbus",
689 .data
= &dove_mbus_data
, },
690 { .compatible
= "marvell,orion5x-88f5281-mbus",
691 .data
= &orion5x_4win_mbus_data
, },
692 { .compatible
= "marvell,orion5x-88f5182-mbus",
693 .data
= &orion5x_2win_mbus_data
, },
694 { .compatible
= "marvell,orion5x-88f5181-mbus",
695 .data
= &orion5x_2win_mbus_data
, },
696 { .compatible
= "marvell,orion5x-88f6183-mbus",
697 .data
= &orion5x_4win_mbus_data
, },
698 { .compatible
= "marvell,mv78xx0-mbus",
699 .data
= &mv78xx0_mbus_data
, },
704 * Public API of the driver
706 int mvebu_mbus_add_window_remap_by_id(unsigned int target
,
707 unsigned int attribute
,
708 phys_addr_t base
, size_t size
,
711 struct mvebu_mbus_state
*s
= &mbus_state
;
713 if (!mvebu_mbus_window_conflicts(s
, base
, size
, target
, attribute
)) {
714 pr_err("cannot add window '%x:%x', conflicts with another window\n",
719 return mvebu_mbus_alloc_window(s
, base
, size
, remap
, target
, attribute
);
722 int mvebu_mbus_add_window_by_id(unsigned int target
, unsigned int attribute
,
723 phys_addr_t base
, size_t size
)
725 return mvebu_mbus_add_window_remap_by_id(target
, attribute
, base
,
726 size
, MVEBU_MBUS_NO_REMAP
);
729 int mvebu_mbus_del_window(phys_addr_t base
, size_t size
)
733 win
= mvebu_mbus_find_window(&mbus_state
, base
, size
);
737 mvebu_mbus_disable_window(&mbus_state
, win
);
741 void mvebu_mbus_get_pcie_mem_aperture(struct resource
*res
)
745 *res
= mbus_state
.pcie_mem_aperture
;
748 void mvebu_mbus_get_pcie_io_aperture(struct resource
*res
)
752 *res
= mbus_state
.pcie_io_aperture
;
755 static __init
int mvebu_mbus_debugfs_init(void)
757 struct mvebu_mbus_state
*s
= &mbus_state
;
760 * If no base has been initialized, doesn't make sense to
761 * register the debugfs entries. We may be on a multiplatform
762 * kernel that isn't running a Marvell EBU SoC.
764 if (!s
->mbuswins_base
)
767 s
->debugfs_root
= debugfs_create_dir("mvebu-mbus", NULL
);
768 if (s
->debugfs_root
) {
769 s
->debugfs_sdram
= debugfs_create_file("sdram", S_IRUGO
,
770 s
->debugfs_root
, NULL
,
771 &mvebu_sdram_debug_fops
);
772 s
->debugfs_devs
= debugfs_create_file("devices", S_IRUGO
,
773 s
->debugfs_root
, NULL
,
774 &mvebu_devs_debug_fops
);
779 fs_initcall(mvebu_mbus_debugfs_init
);
781 static int mvebu_mbus_suspend(void)
783 struct mvebu_mbus_state
*s
= &mbus_state
;
786 if (!s
->mbusbridge_base
)
789 for (win
= 0; win
< s
->soc
->num_wins
; win
++) {
790 void __iomem
*addr
= s
->mbuswins_base
+
791 s
->soc
->win_cfg_offset(win
);
793 s
->wins
[win
].base
= readl(addr
+ WIN_BASE_OFF
);
794 s
->wins
[win
].ctrl
= readl(addr
+ WIN_CTRL_OFF
);
796 if (win
>= s
->soc
->num_remappable_wins
)
799 s
->wins
[win
].remap_lo
= readl(addr
+ WIN_REMAP_LO_OFF
);
800 s
->wins
[win
].remap_hi
= readl(addr
+ WIN_REMAP_HI_OFF
);
803 s
->mbus_bridge_ctrl
= readl(s
->mbusbridge_base
+
804 MBUS_BRIDGE_CTRL_OFF
);
805 s
->mbus_bridge_base
= readl(s
->mbusbridge_base
+
806 MBUS_BRIDGE_BASE_OFF
);
811 static void mvebu_mbus_resume(void)
813 struct mvebu_mbus_state
*s
= &mbus_state
;
816 writel(s
->mbus_bridge_ctrl
,
817 s
->mbusbridge_base
+ MBUS_BRIDGE_CTRL_OFF
);
818 writel(s
->mbus_bridge_base
,
819 s
->mbusbridge_base
+ MBUS_BRIDGE_BASE_OFF
);
821 for (win
= 0; win
< s
->soc
->num_wins
; win
++) {
822 void __iomem
*addr
= s
->mbuswins_base
+
823 s
->soc
->win_cfg_offset(win
);
825 writel(s
->wins
[win
].base
, addr
+ WIN_BASE_OFF
);
826 writel(s
->wins
[win
].ctrl
, addr
+ WIN_CTRL_OFF
);
828 if (win
>= s
->soc
->num_remappable_wins
)
831 writel(s
->wins
[win
].remap_lo
, addr
+ WIN_REMAP_LO_OFF
);
832 writel(s
->wins
[win
].remap_hi
, addr
+ WIN_REMAP_HI_OFF
);
836 struct syscore_ops mvebu_mbus_syscore_ops
= {
837 .suspend
= mvebu_mbus_suspend
,
838 .resume
= mvebu_mbus_resume
,
841 static int __init
mvebu_mbus_common_init(struct mvebu_mbus_state
*mbus
,
842 phys_addr_t mbuswins_phys_base
,
843 size_t mbuswins_size
,
844 phys_addr_t sdramwins_phys_base
,
845 size_t sdramwins_size
,
846 phys_addr_t mbusbridge_phys_base
,
847 size_t mbusbridge_size
)
851 mbus
->mbuswins_base
= ioremap(mbuswins_phys_base
, mbuswins_size
);
852 if (!mbus
->mbuswins_base
)
855 mbus
->sdramwins_base
= ioremap(sdramwins_phys_base
, sdramwins_size
);
856 if (!mbus
->sdramwins_base
) {
857 iounmap(mbus_state
.mbuswins_base
);
861 mbus
->sdramwins_phys_base
= sdramwins_phys_base
;
863 if (mbusbridge_phys_base
) {
864 mbus
->mbusbridge_base
= ioremap(mbusbridge_phys_base
,
866 if (!mbus
->mbusbridge_base
) {
867 iounmap(mbus
->sdramwins_base
);
868 iounmap(mbus
->mbuswins_base
);
872 mbus
->mbusbridge_base
= NULL
;
874 for (win
= 0; win
< mbus
->soc
->num_wins
; win
++)
875 mvebu_mbus_disable_window(mbus
, win
);
877 mbus
->soc
->setup_cpu_target(mbus
);
879 register_syscore_ops(&mvebu_mbus_syscore_ops
);
884 int __init
mvebu_mbus_init(const char *soc
, phys_addr_t mbuswins_phys_base
,
885 size_t mbuswins_size
,
886 phys_addr_t sdramwins_phys_base
,
887 size_t sdramwins_size
)
889 const struct of_device_id
*of_id
;
891 for (of_id
= of_mvebu_mbus_ids
; of_id
->compatible
[0]; of_id
++)
892 if (!strcmp(of_id
->compatible
, soc
))
895 if (!of_id
->compatible
[0]) {
896 pr_err("could not find a matching SoC family\n");
900 mbus_state
.soc
= of_id
->data
;
902 return mvebu_mbus_common_init(&mbus_state
,
906 sdramwins_size
, 0, 0);
911 * The window IDs in the ranges DT property have the following format:
912 * - bits 28 to 31: MBus custom field
913 * - bits 24 to 27: window target ID
914 * - bits 16 to 23: window attribute ID
915 * - bits 0 to 15: unused
917 #define CUSTOM(id) (((id) & 0xF0000000) >> 24)
918 #define TARGET(id) (((id) & 0x0F000000) >> 24)
919 #define ATTR(id) (((id) & 0x00FF0000) >> 16)
921 static int __init
mbus_dt_setup_win(struct mvebu_mbus_state
*mbus
,
925 if (!mvebu_mbus_window_conflicts(mbus
, base
, size
, target
, attr
)) {
926 pr_err("cannot add window '%04x:%04x', conflicts with another window\n",
931 if (mvebu_mbus_alloc_window(mbus
, base
, size
, MVEBU_MBUS_NO_REMAP
,
933 pr_err("cannot add window '%04x:%04x', too many windows\n",
941 mbus_parse_ranges(struct device_node
*node
,
942 int *addr_cells
, int *c_addr_cells
, int *c_size_cells
,
943 int *cell_count
, const __be32
**ranges_start
,
944 const __be32
**ranges_end
)
947 int ranges_len
, tuple_len
;
949 /* Allow a node with no 'ranges' property */
950 *ranges_start
= of_get_property(node
, "ranges", &ranges_len
);
951 if (*ranges_start
== NULL
) {
952 *addr_cells
= *c_addr_cells
= *c_size_cells
= *cell_count
= 0;
953 *ranges_start
= *ranges_end
= NULL
;
956 *ranges_end
= *ranges_start
+ ranges_len
/ sizeof(__be32
);
958 *addr_cells
= of_n_addr_cells(node
);
960 prop
= of_get_property(node
, "#address-cells", NULL
);
961 *c_addr_cells
= be32_to_cpup(prop
);
963 prop
= of_get_property(node
, "#size-cells", NULL
);
964 *c_size_cells
= be32_to_cpup(prop
);
966 *cell_count
= *addr_cells
+ *c_addr_cells
+ *c_size_cells
;
967 tuple_len
= (*cell_count
) * sizeof(__be32
);
969 if (ranges_len
% tuple_len
) {
970 pr_warn("malformed ranges entry '%s'\n", node
->name
);
976 static int __init
mbus_dt_setup(struct mvebu_mbus_state
*mbus
,
977 struct device_node
*np
)
979 int addr_cells
, c_addr_cells
, c_size_cells
;
980 int i
, ret
, cell_count
;
981 const __be32
*r
, *ranges_start
, *ranges_end
;
983 ret
= mbus_parse_ranges(np
, &addr_cells
, &c_addr_cells
,
984 &c_size_cells
, &cell_count
,
985 &ranges_start
, &ranges_end
);
989 for (i
= 0, r
= ranges_start
; r
< ranges_end
; r
+= cell_count
, i
++) {
990 u32 windowid
, base
, size
;
994 * An entry with a non-zero custom field do not
995 * correspond to a static window, so skip it.
997 windowid
= of_read_number(r
, 1);
998 if (CUSTOM(windowid
))
1001 target
= TARGET(windowid
);
1002 attr
= ATTR(windowid
);
1004 base
= of_read_number(r
+ c_addr_cells
, addr_cells
);
1005 size
= of_read_number(r
+ c_addr_cells
+ addr_cells
,
1007 ret
= mbus_dt_setup_win(mbus
, base
, size
, target
, attr
);
1014 static void __init
mvebu_mbus_get_pcie_resources(struct device_node
*np
,
1015 struct resource
*mem
,
1016 struct resource
*io
)
1022 * These are optional, so we make sure that resource_size(x) will
1025 memset(mem
, 0, sizeof(struct resource
));
1027 memset(io
, 0, sizeof(struct resource
));
1030 ret
= of_property_read_u32_array(np
, "pcie-mem-aperture", reg
, ARRAY_SIZE(reg
));
1032 mem
->start
= reg
[0];
1033 mem
->end
= mem
->start
+ reg
[1] - 1;
1034 mem
->flags
= IORESOURCE_MEM
;
1037 ret
= of_property_read_u32_array(np
, "pcie-io-aperture", reg
, ARRAY_SIZE(reg
));
1040 io
->end
= io
->start
+ reg
[1] - 1;
1041 io
->flags
= IORESOURCE_IO
;
1045 int __init
mvebu_mbus_dt_init(bool is_coherent
)
1047 struct resource mbuswins_res
, sdramwins_res
, mbusbridge_res
;
1048 struct device_node
*np
, *controller
;
1049 const struct of_device_id
*of_id
;
1053 np
= of_find_matching_node_and_match(NULL
, of_mvebu_mbus_ids
, &of_id
);
1055 pr_err("could not find a matching SoC family\n");
1059 mbus_state
.soc
= of_id
->data
;
1061 prop
= of_get_property(np
, "controller", NULL
);
1063 pr_err("required 'controller' property missing\n");
1067 controller
= of_find_node_by_phandle(be32_to_cpup(prop
));
1069 pr_err("could not find an 'mbus-controller' node\n");
1073 if (of_address_to_resource(controller
, 0, &mbuswins_res
)) {
1074 pr_err("cannot get MBUS register address\n");
1078 if (of_address_to_resource(controller
, 1, &sdramwins_res
)) {
1079 pr_err("cannot get SDRAM register address\n");
1084 * Set the resource to 0 so that it can be left unmapped by
1085 * mvebu_mbus_common_init() if the DT doesn't carry the
1086 * necessary information. This is needed to preserve backward
1089 memset(&mbusbridge_res
, 0, sizeof(mbusbridge_res
));
1091 if (mbus_state
.soc
->has_mbus_bridge
) {
1092 if (of_address_to_resource(controller
, 2, &mbusbridge_res
))
1093 pr_warn(FW_WARN
"deprecated mbus-mvebu Device Tree, suspend/resume will not work\n");
1096 mbus_state
.hw_io_coherency
= is_coherent
;
1098 /* Get optional pcie-{mem,io}-aperture properties */
1099 mvebu_mbus_get_pcie_resources(np
, &mbus_state
.pcie_mem_aperture
,
1100 &mbus_state
.pcie_io_aperture
);
1102 ret
= mvebu_mbus_common_init(&mbus_state
,
1104 resource_size(&mbuswins_res
),
1105 sdramwins_res
.start
,
1106 resource_size(&sdramwins_res
),
1107 mbusbridge_res
.start
,
1108 resource_size(&mbusbridge_res
));
1112 /* Setup statically declared windows in the DT */
1113 return mbus_dt_setup(&mbus_state
, np
);