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