blob: 3b1b9f73ebf6c017f2262c76ec65e54281894de3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roese916711c2014-10-22 12:13:09 +02002/*
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 Roese916711c2014-10-22 12:13:09 +020015 * 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
49#include <common.h>
Simon Glass9bc15642020-02-03 07:36:16 -070050#include <malloc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060051#include <linux/bitops.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090052#include <linux/errno.h>
Stefan Roese916711c2014-10-22 12:13:09 +020053#include <asm/io.h>
54#include <asm/arch/cpu.h>
55#include <asm/arch/soc.h>
Fabio Estevam0297d1e2015-11-05 12:43:39 -020056#include <linux/log2.h>
Stefan Roese916711c2014-10-22 12:13:09 +020057#include <linux/mbus.h>
58
Stefan Roese916711c2014-10-22 12:13:09 +020059/* 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
91struct mvebu_mbus_state;
92
93struct mvebu_mbus_soc_data {
94 unsigned int num_wins;
95 unsigned int num_remappable_wins;
96 unsigned int (*win_cfg_offset)(const int win);
97 void (*setup_cpu_target)(struct mvebu_mbus_state *s);
98};
99
100struct mvebu_mbus_state mbus_state
Marek BehĂșn4bebdd32021-05-20 13:23:52 +0200101 __section(".data");
Stefan Roese916711c2014-10-22 12:13:09 +0200102static struct mbus_dram_target_info mbus_dram_info
Marek BehĂșn4bebdd32021-05-20 13:23:52 +0200103 __section(".data");
Stefan Roese916711c2014-10-22 12:13:09 +0200104
105/*
106 * Functions to manipulate the address decoding windows
107 */
108
109static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
110 int win, int *enabled, u64 *base,
111 u32 *size, u8 *target, u8 *attr,
112 u64 *remap)
113{
114 void __iomem *addr = mbus->mbuswins_base +
115 mbus->soc->win_cfg_offset(win);
116 u32 basereg = readl(addr + WIN_BASE_OFF);
117 u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
118
119 if (!(ctrlreg & WIN_CTRL_ENABLE)) {
120 *enabled = 0;
121 return;
122 }
123
124 *enabled = 1;
125 *base = ((u64)basereg & WIN_BASE_HIGH) << 32;
126 *base |= (basereg & WIN_BASE_LOW);
127 *size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
128
129 if (target)
130 *target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
131
132 if (attr)
133 *attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
134
135 if (remap) {
136 if (win < mbus->soc->num_remappable_wins) {
137 u32 remap_low = readl(addr + WIN_REMAP_LO_OFF);
138 u32 remap_hi = readl(addr + WIN_REMAP_HI_OFF);
139 *remap = ((u64)remap_hi << 32) | remap_low;
140 } else {
141 *remap = 0;
142 }
143 }
144}
145
146static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
147 int win)
148{
149 void __iomem *addr;
150
151 addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
152
153 writel(0, addr + WIN_BASE_OFF);
154 writel(0, addr + WIN_CTRL_OFF);
155 if (win < mbus->soc->num_remappable_wins) {
156 writel(0, addr + WIN_REMAP_LO_OFF);
157 writel(0, addr + WIN_REMAP_HI_OFF);
158 }
159}
160
161/* Checks whether the given window number is available */
162static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
163 const int win)
164{
165 void __iomem *addr = mbus->mbuswins_base +
166 mbus->soc->win_cfg_offset(win);
167 u32 ctrl = readl(addr + WIN_CTRL_OFF);
168 return !(ctrl & WIN_CTRL_ENABLE);
169}
170
171/*
172 * Checks whether the given (base, base+size) area doesn't overlap an
173 * existing region
174 */
175static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
176 phys_addr_t base, size_t size,
177 u8 target, u8 attr)
178{
179 u64 end = (u64)base + size;
180 int win;
181
182 for (win = 0; win < mbus->soc->num_wins; win++) {
183 u64 wbase, wend;
184 u32 wsize;
185 u8 wtarget, wattr;
186 int enabled;
187
188 mvebu_mbus_read_window(mbus, win,
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
214static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
215 phys_addr_t base, size_t size)
216{
217 int win;
218
219 for (win = 0; win < mbus->soc->num_wins; win++) {
220 u64 wbase;
221 u32 wsize;
222 int enabled;
223
224 mvebu_mbus_read_window(mbus, win,
225 &enabled, &wbase, &wsize,
226 NULL, NULL, NULL);
227
228 if (!enabled)
229 continue;
230
231 if (base == wbase && size == wsize)
232 return win;
233 }
234
235 return -ENODEV;
236}
237
238static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
239 int win, phys_addr_t base, size_t size,
240 phys_addr_t remap, u8 target,
241 u8 attr)
242{
243 void __iomem *addr = mbus->mbuswins_base +
244 mbus->soc->win_cfg_offset(win);
245 u32 ctrl, remap_addr;
246
247 ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
248 (attr << WIN_CTRL_ATTR_SHIFT) |
249 (target << WIN_CTRL_TGT_SHIFT) |
250 WIN_CTRL_ENABLE;
251
252 writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
253 writel(ctrl, addr + WIN_CTRL_OFF);
254 if (win < mbus->soc->num_remappable_wins) {
255 if (remap == MVEBU_MBUS_NO_REMAP)
256 remap_addr = base;
257 else
258 remap_addr = remap;
259 writel(remap_addr & WIN_REMAP_LOW, addr + WIN_REMAP_LO_OFF);
260 writel(0, addr + WIN_REMAP_HI_OFF);
261 }
262
263 return 0;
264}
265
266static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
267 phys_addr_t base, size_t size,
268 phys_addr_t remap, u8 target,
269 u8 attr)
270{
271 int win;
272
273 if (remap == MVEBU_MBUS_NO_REMAP) {
274 for (win = mbus->soc->num_remappable_wins;
275 win < mbus->soc->num_wins; win++)
276 if (mvebu_mbus_window_is_free(mbus, win))
277 return mvebu_mbus_setup_window(mbus, win, base,
278 size, remap,
279 target, attr);
280 }
281
282
283 for (win = 0; win < mbus->soc->num_wins; win++)
284 if (mvebu_mbus_window_is_free(mbus, win))
285 return mvebu_mbus_setup_window(mbus, win, base, size,
286 remap, target, attr);
287
288 return -ENOMEM;
289}
290
291/*
292 * SoC-specific functions and definitions
293 */
294
295static unsigned int armada_370_xp_mbus_win_offset(int win)
296{
297 /* The register layout is a bit annoying and the below code
298 * tries to cope with it.
299 * - At offset 0x0, there are the registers for the first 8
300 * windows, with 4 registers of 32 bits per window (ctrl,
301 * base, remap low, remap high)
302 * - Then at offset 0x80, there is a hole of 0x10 bytes for
303 * the internal registers base address and internal units
304 * sync barrier register.
305 * - Then at offset 0x90, there the registers for 12
306 * windows, with only 2 registers of 32 bits per window
307 * (ctrl, base).
308 */
309 if (win < 8)
310 return win << 4;
311 else
312 return 0x90 + ((win - 8) << 3);
313}
314
315static unsigned int orion5x_mbus_win_offset(int win)
316{
317 return win << 4;
318}
319
320static void mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
321{
322 int i;
323 int cs;
324
325 mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
326
327 for (i = 0, cs = 0; i < 4; i++) {
328 u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
329 u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
330
331 /*
332 * We only take care of entries for which the chip
333 * select is enabled, and that don't have high base
334 * address bits set (devices can only access the first
335 * 32 bits of the memory).
336 */
337 if ((size & DDR_SIZE_ENABLED) &&
338 !(base & DDR_BASE_CS_HIGH_MASK)) {
339 struct mbus_dram_window *w;
340
341 w = &mbus_dram_info.cs[cs++];
342 w->cs_index = i;
343 w->mbus_attr = 0xf & ~(1 << i);
Stefan Roese916711c2014-10-22 12:13:09 +0200344 w->base = base & DDR_BASE_CS_LOW_MASK;
345 w->size = (size | ~DDR_SIZE_MASK) + 1;
346 }
347 }
348 mbus_dram_info.num_cs = cs;
Chris Packhama8f845e2019-04-11 22:22:50 +1200349
350#if defined(CONFIG_ARMADA_MSYS)
351 /* Disable MBUS Err Prop - in order to avoid data aborts */
352 clrbits_le32(mbus->mbuswins_base + 0x200, BIT(8));
353#endif
Stefan Roese916711c2014-10-22 12:13:09 +0200354}
355
356static const struct mvebu_mbus_soc_data
357armada_370_xp_mbus_data __maybe_unused = {
358 .num_wins = 20,
359 .num_remappable_wins = 8,
360 .win_cfg_offset = armada_370_xp_mbus_win_offset,
361 .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
362};
363
364static const struct mvebu_mbus_soc_data
365kirkwood_mbus_data __maybe_unused = {
366 .num_wins = 8,
367 .num_remappable_wins = 4,
368 .win_cfg_offset = orion5x_mbus_win_offset,
369 .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
370};
371
372/*
373 * Public API of the driver
374 */
375const struct mbus_dram_target_info *mvebu_mbus_dram_info(void)
376{
377 return &mbus_dram_info;
378}
379
380int mvebu_mbus_add_window_remap_by_id(unsigned int target,
381 unsigned int attribute,
382 phys_addr_t base, size_t size,
383 phys_addr_t remap)
384{
385 struct mvebu_mbus_state *s = &mbus_state;
386
387 if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
388 printf("Cannot add window '%x:%x', conflicts with another window\n",
389 target, attribute);
390 return -EINVAL;
391 }
392
393 return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
394}
395
396int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
397 phys_addr_t base, size_t size)
398{
399 return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
400 size, MVEBU_MBUS_NO_REMAP);
401}
402
403int mvebu_mbus_del_window(phys_addr_t base, size_t size)
404{
405 int win;
406
407 win = mvebu_mbus_find_window(&mbus_state, base, size);
408 if (win < 0)
409 return win;
410
411 mvebu_mbus_disable_window(&mbus_state, win);
412 return 0;
413}
414
Trevor Woernerbb7ab072020-05-06 08:02:40 -0400415#ifndef CONFIG_ARCH_KIRKWOOD
Stefan Roesec049ca02015-07-01 12:44:51 +0200416static void mvebu_mbus_get_lowest_base(struct mvebu_mbus_state *mbus,
417 phys_addr_t *base)
418{
419 int win;
420 *base = 0xffffffff;
421
422 for (win = 0; win < mbus->soc->num_wins; win++) {
423 u64 wbase;
424 u32 wsize;
425 u8 wtarget, wattr;
426 int enabled;
427
428 mvebu_mbus_read_window(mbus, win,
429 &enabled, &wbase, &wsize,
430 &wtarget, &wattr, NULL);
431
432 if (!enabled)
433 continue;
434
435 if (wbase < *base)
436 *base = wbase;
437 }
438}
439
440static void mvebu_config_mbus_bridge(struct mvebu_mbus_state *mbus)
441{
442 phys_addr_t base;
443 u32 val;
444 u32 size;
445
446 /* Set MBUS bridge base/ctrl */
447 mvebu_mbus_get_lowest_base(&mbus_state, &base);
448
449 size = 0xffffffff - base + 1;
450 if (!is_power_of_2(size)) {
451 /* Round up to next power of 2 */
452 size = 1 << (ffs(base) + 1);
453 base = 0xffffffff - size + 1;
454 }
455
456 /* Now write base and size */
457 writel(base, MBUS_BRIDGE_WIN_BASE_REG);
458 /* Align window size to 64KiB */
459 val = (size / (64 << 10)) - 1;
460 writel((val << 16) | 0x1, MBUS_BRIDGE_WIN_CTRL_REG);
461}
Chris Packham968856c2019-03-13 20:47:03 +1300462#endif
Stefan Roesec049ca02015-07-01 12:44:51 +0200463
Stefan Roese916711c2014-10-22 12:13:09 +0200464int mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
465 u32 base, u32 size, u8 target, u8 attr)
466{
467 if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
468 printf("Cannot add window '%04x:%04x', conflicts with another window\n",
469 target, attr);
470 return -EBUSY;
471 }
472
473 /*
474 * In U-Boot we first try to add the mbus window to the remap windows.
475 * If this fails, lets try to add the windows to the non-remap windows.
476 */
477 if (mvebu_mbus_alloc_window(mbus, base, size, base, target, attr)) {
478 if (mvebu_mbus_alloc_window(mbus, base, size,
479 MVEBU_MBUS_NO_REMAP, target, attr))
480 return -ENOMEM;
481 }
482
Trevor Woernerbb7ab072020-05-06 08:02:40 -0400483#ifndef CONFIG_ARCH_KIRKWOOD
Stefan Roesec049ca02015-07-01 12:44:51 +0200484 /*
485 * Re-configure the mbus bridge registers each time this function
486 * is called. Since it may get called from the board code in
487 * later boot stages as well.
488 */
489 mvebu_config_mbus_bridge(mbus);
Chris Packham968856c2019-03-13 20:47:03 +1300490#endif
Stefan Roesec049ca02015-07-01 12:44:51 +0200491
Stefan Roese916711c2014-10-22 12:13:09 +0200492 return 0;
493}
494
495int mvebu_mbus_probe(struct mbus_win windows[], int count)
496{
497 int win;
498 int ret;
499 int i;
500
Trevor Woernerbb7ab072020-05-06 08:02:40 -0400501#if defined(CONFIG_ARCH_KIRKWOOD)
Stefan Roese916711c2014-10-22 12:13:09 +0200502 mbus_state.soc = &kirkwood_mbus_data;
503#endif
Stefan Roeseeb083e52015-12-21 13:56:33 +0100504#if defined(CONFIG_ARCH_MVEBU)
Stefan Roese916711c2014-10-22 12:13:09 +0200505 mbus_state.soc = &armada_370_xp_mbus_data;
506#endif
507
508 mbus_state.mbuswins_base = (void __iomem *)MVEBU_CPU_WIN_BASE;
509 mbus_state.sdramwins_base = (void __iomem *)MVEBU_SDRAM_BASE;
510
511 for (win = 0; win < mbus_state.soc->num_wins; win++)
512 mvebu_mbus_disable_window(&mbus_state, win);
513
514 mbus_state.soc->setup_cpu_target(&mbus_state);
515
516 /* Setup statically declared windows in the DT */
517 for (i = 0; i < count; i++) {
518 u32 base, size;
519 u8 target, attr;
520
521 target = windows[i].target;
522 attr = windows[i].attr;
523 base = windows[i].base;
524 size = windows[i].size;
525 ret = mbus_dt_setup_win(&mbus_state, base, size, target, attr);
526 if (ret < 0)
527 return ret;
528 }
529
530 return 0;
531}