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