blob: afee0f731111488fc45a9c7e0ba5b19039fb7538 [file] [log] [blame]
Thomas Petazzonifddddb52013-03-21 17:59:14 +01001/*
2 * Address map functions for Marvell EBU SoCs (Kirkwood, Armada
3 * 370/XP, Dove, Orion5x and MV78xx0)
4 *
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.
8 *
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:
13 *
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,
18 * attribute).
19 *
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.
23 *
24 * This driver:
25 *
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.
35 *
36 * - Provides an API for platform code or device drivers to
37 * dynamically add or remove address decoding windows for the CPU ->
Thomas Petazzoni6275afe2013-07-26 10:17:52 -030038 * 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().
Thomas Petazzonifddddb52013-03-21 17:59:14 +010041 *
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').
46 */
47
Ezequiel Garciab15d0b52013-06-07 13:47:38 -030048#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49
Thomas Petazzonifddddb52013-03-21 17:59:14 +010050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/mbus.h>
54#include <linux/io.h>
55#include <linux/ioport.h>
56#include <linux/of.h>
57#include <linux/of_address.h>
58#include <linux/debugfs.h>
Jason Gunthorpe09752a12014-04-18 14:19:51 +020059#include <linux/log2.h>
Thomas Petazzonifddddb52013-03-21 17:59:14 +010060
61/*
62 * DDR target is the same on all platforms.
63 */
64#define TARGET_DDR 0
65
66/*
67 * CPU Address Decode Windows registers
68 */
69#define WIN_CTRL_OFF 0x0000
70#define WIN_CTRL_ENABLE BIT(0)
71#define WIN_CTRL_TGT_MASK 0xf0
72#define WIN_CTRL_TGT_SHIFT 4
73#define WIN_CTRL_ATTR_MASK 0xff00
74#define WIN_CTRL_ATTR_SHIFT 8
75#define WIN_CTRL_SIZE_MASK 0xffff0000
76#define WIN_CTRL_SIZE_SHIFT 16
77#define WIN_BASE_OFF 0x0004
78#define WIN_BASE_LOW 0xffff0000
79#define WIN_BASE_HIGH 0xf
80#define WIN_REMAP_LO_OFF 0x0008
81#define WIN_REMAP_LOW 0xffff0000
82#define WIN_REMAP_HI_OFF 0x000c
83
84#define ATTR_HW_COHERENCY (0x1 << 4)
85
86#define DDR_BASE_CS_OFF(n) (0x0000 + ((n) << 3))
87#define DDR_BASE_CS_HIGH_MASK 0xf
88#define DDR_BASE_CS_LOW_MASK 0xff000000
89#define DDR_SIZE_CS_OFF(n) (0x0004 + ((n) << 3))
90#define DDR_SIZE_ENABLED BIT(0)
91#define DDR_SIZE_CS_MASK 0x1c
92#define DDR_SIZE_CS_SHIFT 2
93#define DDR_SIZE_MASK 0xff000000
94
95#define DOVE_DDR_BASE_CS_OFF(n) ((n) << 4)
96
Thomas Petazzonifddddb52013-03-21 17:59:14 +010097struct mvebu_mbus_state;
98
99struct mvebu_mbus_soc_data {
100 unsigned int num_wins;
101 unsigned int num_remappable_wins;
102 unsigned int (*win_cfg_offset)(const int win);
103 void (*setup_cpu_target)(struct mvebu_mbus_state *s);
104 int (*show_cpu_target)(struct mvebu_mbus_state *s,
105 struct seq_file *seq, void *v);
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100106};
107
108struct mvebu_mbus_state {
109 void __iomem *mbuswins_base;
110 void __iomem *sdramwins_base;
111 struct dentry *debugfs_root;
112 struct dentry *debugfs_sdram;
113 struct dentry *debugfs_devs;
Ezequiel Garcia79d94682013-07-26 10:17:47 -0300114 struct resource pcie_mem_aperture;
115 struct resource pcie_io_aperture;
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100116 const struct mvebu_mbus_soc_data *soc;
117 int hw_io_coherency;
118};
119
120static struct mvebu_mbus_state mbus_state;
121
122static struct mbus_dram_target_info mvebu_mbus_dram_info;
123const struct mbus_dram_target_info *mv_mbus_dram_info(void)
124{
125 return &mvebu_mbus_dram_info;
126}
127EXPORT_SYMBOL_GPL(mv_mbus_dram_info);
128
129/*
130 * Functions to manipulate the address decoding windows
131 */
132
133static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
134 int win, int *enabled, u64 *base,
135 u32 *size, u8 *target, u8 *attr,
136 u64 *remap)
137{
138 void __iomem *addr = mbus->mbuswins_base +
139 mbus->soc->win_cfg_offset(win);
140 u32 basereg = readl(addr + WIN_BASE_OFF);
141 u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
142
143 if (!(ctrlreg & WIN_CTRL_ENABLE)) {
144 *enabled = 0;
145 return;
146 }
147
148 *enabled = 1;
149 *base = ((u64)basereg & WIN_BASE_HIGH) << 32;
150 *base |= (basereg & WIN_BASE_LOW);
151 *size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
152
153 if (target)
154 *target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
155
156 if (attr)
157 *attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
158
159 if (remap) {
160 if (win < mbus->soc->num_remappable_wins) {
161 u32 remap_low = readl(addr + WIN_REMAP_LO_OFF);
162 u32 remap_hi = readl(addr + WIN_REMAP_HI_OFF);
163 *remap = ((u64)remap_hi << 32) | remap_low;
164 } else
165 *remap = 0;
166 }
167}
168
169static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
170 int win)
171{
172 void __iomem *addr;
173
174 addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
175
176 writel(0, addr + WIN_BASE_OFF);
177 writel(0, addr + WIN_CTRL_OFF);
178 if (win < mbus->soc->num_remappable_wins) {
179 writel(0, addr + WIN_REMAP_LO_OFF);
180 writel(0, addr + WIN_REMAP_HI_OFF);
181 }
182}
183
184/* Checks whether the given window number is available */
185static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
186 const int win)
187{
188 void __iomem *addr = mbus->mbuswins_base +
189 mbus->soc->win_cfg_offset(win);
190 u32 ctrl = readl(addr + WIN_CTRL_OFF);
191 return !(ctrl & WIN_CTRL_ENABLE);
192}
193
194/*
195 * Checks whether the given (base, base+size) area doesn't overlap an
196 * existing region
197 */
198static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
199 phys_addr_t base, size_t size,
200 u8 target, u8 attr)
201{
202 u64 end = (u64)base + size;
203 int win;
204
205 for (win = 0; win < mbus->soc->num_wins; win++) {
206 u64 wbase, wend;
207 u32 wsize;
208 u8 wtarget, wattr;
209 int enabled;
210
211 mvebu_mbus_read_window(mbus, win,
212 &enabled, &wbase, &wsize,
213 &wtarget, &wattr, NULL);
214
215 if (!enabled)
216 continue;
217
218 wend = wbase + wsize;
219
220 /*
221 * Check if the current window overlaps with the
222 * proposed physical range
223 */
224 if ((u64)base < wend && end > wbase)
225 return 0;
226
227 /*
228 * Check if target/attribute conflicts
229 */
230 if (target == wtarget && attr == wattr)
231 return 0;
232 }
233
234 return 1;
235}
236
237static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
238 phys_addr_t base, size_t size)
239{
240 int win;
241
242 for (win = 0; win < mbus->soc->num_wins; win++) {
243 u64 wbase;
244 u32 wsize;
245 int enabled;
246
247 mvebu_mbus_read_window(mbus, win,
248 &enabled, &wbase, &wsize,
249 NULL, NULL, NULL);
250
251 if (!enabled)
252 continue;
253
254 if (base == wbase && size == wsize)
255 return win;
256 }
257
258 return -ENODEV;
259}
260
261static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
262 int win, phys_addr_t base, size_t size,
263 phys_addr_t remap, u8 target,
264 u8 attr)
265{
266 void __iomem *addr = mbus->mbuswins_base +
267 mbus->soc->win_cfg_offset(win);
268 u32 ctrl, remap_addr;
269
Jason Gunthorpe09752a12014-04-18 14:19:51 +0200270 if (!is_power_of_2(size)) {
271 WARN(true, "Invalid MBus window size: 0x%zx\n", size);
272 return -EINVAL;
273 }
274
275 if ((base & (phys_addr_t)(size - 1)) != 0) {
276 WARN(true, "Invalid MBus base/size: %pa len 0x%zx\n", &base,
277 size);
278 return -EINVAL;
279 }
280
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100281 ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
282 (attr << WIN_CTRL_ATTR_SHIFT) |
283 (target << WIN_CTRL_TGT_SHIFT) |
284 WIN_CTRL_ENABLE;
285
286 writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
287 writel(ctrl, addr + WIN_CTRL_OFF);
288 if (win < mbus->soc->num_remappable_wins) {
289 if (remap == MVEBU_MBUS_NO_REMAP)
290 remap_addr = base;
291 else
292 remap_addr = remap;
293 writel(remap_addr & WIN_REMAP_LOW, addr + WIN_REMAP_LO_OFF);
294 writel(0, addr + WIN_REMAP_HI_OFF);
295 }
296
297 return 0;
298}
299
300static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
301 phys_addr_t base, size_t size,
302 phys_addr_t remap, u8 target,
303 u8 attr)
304{
305 int win;
306
307 if (remap == MVEBU_MBUS_NO_REMAP) {
308 for (win = mbus->soc->num_remappable_wins;
309 win < mbus->soc->num_wins; win++)
310 if (mvebu_mbus_window_is_free(mbus, win))
311 return mvebu_mbus_setup_window(mbus, win, base,
312 size, remap,
313 target, attr);
314 }
315
316
317 for (win = 0; win < mbus->soc->num_wins; win++)
318 if (mvebu_mbus_window_is_free(mbus, win))
319 return mvebu_mbus_setup_window(mbus, win, base, size,
320 remap, target, attr);
321
322 return -ENOMEM;
323}
324
325/*
326 * Debugfs debugging
327 */
328
329/* Common function used for Dove, Kirkwood, Armada 370/XP and Orion 5x */
330static int mvebu_sdram_debug_show_orion(struct mvebu_mbus_state *mbus,
331 struct seq_file *seq, void *v)
332{
333 int i;
334
335 for (i = 0; i < 4; i++) {
336 u32 basereg = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
337 u32 sizereg = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
338 u64 base;
339 u32 size;
340
341 if (!(sizereg & DDR_SIZE_ENABLED)) {
342 seq_printf(seq, "[%d] disabled\n", i);
343 continue;
344 }
345
346 base = ((u64)basereg & DDR_BASE_CS_HIGH_MASK) << 32;
347 base |= basereg & DDR_BASE_CS_LOW_MASK;
348 size = (sizereg | ~DDR_SIZE_MASK);
349
350 seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
351 i, (unsigned long long)base,
352 (unsigned long long)base + size + 1,
353 (sizereg & DDR_SIZE_CS_MASK) >> DDR_SIZE_CS_SHIFT);
354 }
355
356 return 0;
357}
358
359/* Special function for Dove */
360static int mvebu_sdram_debug_show_dove(struct mvebu_mbus_state *mbus,
361 struct seq_file *seq, void *v)
362{
363 int i;
364
365 for (i = 0; i < 2; i++) {
366 u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
367 u64 base;
368 u32 size;
369
370 if (!(map & 1)) {
371 seq_printf(seq, "[%d] disabled\n", i);
372 continue;
373 }
374
375 base = map & 0xff800000;
376 size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
377
378 seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
379 i, (unsigned long long)base,
380 (unsigned long long)base + size, i);
381 }
382
383 return 0;
384}
385
386static int mvebu_sdram_debug_show(struct seq_file *seq, void *v)
387{
388 struct mvebu_mbus_state *mbus = &mbus_state;
389 return mbus->soc->show_cpu_target(mbus, seq, v);
390}
391
392static int mvebu_sdram_debug_open(struct inode *inode, struct file *file)
393{
394 return single_open(file, mvebu_sdram_debug_show, inode->i_private);
395}
396
397static const struct file_operations mvebu_sdram_debug_fops = {
398 .open = mvebu_sdram_debug_open,
399 .read = seq_read,
400 .llseek = seq_lseek,
401 .release = single_release,
402};
403
404static int mvebu_devs_debug_show(struct seq_file *seq, void *v)
405{
406 struct mvebu_mbus_state *mbus = &mbus_state;
407 int win;
408
409 for (win = 0; win < mbus->soc->num_wins; win++) {
410 u64 wbase, wremap;
411 u32 wsize;
412 u8 wtarget, wattr;
Thomas Petazzonied843a72013-07-26 10:17:51 -0300413 int enabled;
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100414
415 mvebu_mbus_read_window(mbus, win,
416 &enabled, &wbase, &wsize,
417 &wtarget, &wattr, &wremap);
418
419 if (!enabled) {
420 seq_printf(seq, "[%02d] disabled\n", win);
421 continue;
422 }
423
Thomas Petazzonied843a72013-07-26 10:17:51 -0300424 seq_printf(seq, "[%02d] %016llx - %016llx : %04x:%04x",
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100425 win, (unsigned long long)wbase,
Thomas Petazzonied843a72013-07-26 10:17:51 -0300426 (unsigned long long)(wbase + wsize), wtarget, wattr);
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100427
Jason Gunthorpe09752a12014-04-18 14:19:51 +0200428 if (!is_power_of_2(wsize) ||
429 ((wbase & (u64)(wsize - 1)) != 0))
430 seq_puts(seq, " (Invalid base/size!!)");
431
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100432 if (win < mbus->soc->num_remappable_wins) {
433 seq_printf(seq, " (remap %016llx)\n",
434 (unsigned long long)wremap);
435 } else
436 seq_printf(seq, "\n");
437 }
438
439 return 0;
440}
441
442static int mvebu_devs_debug_open(struct inode *inode, struct file *file)
443{
444 return single_open(file, mvebu_devs_debug_show, inode->i_private);
445}
446
447static const struct file_operations mvebu_devs_debug_fops = {
448 .open = mvebu_devs_debug_open,
449 .read = seq_read,
450 .llseek = seq_lseek,
451 .release = single_release,
452};
453
454/*
455 * SoC-specific functions and definitions
456 */
457
458static unsigned int orion_mbus_win_offset(int win)
459{
460 return win << 4;
461}
462
463static unsigned int armada_370_xp_mbus_win_offset(int win)
464{
465 /* The register layout is a bit annoying and the below code
466 * tries to cope with it.
467 * - At offset 0x0, there are the registers for the first 8
468 * windows, with 4 registers of 32 bits per window (ctrl,
469 * base, remap low, remap high)
470 * - Then at offset 0x80, there is a hole of 0x10 bytes for
471 * the internal registers base address and internal units
472 * sync barrier register.
473 * - Then at offset 0x90, there the registers for 12
474 * windows, with only 2 registers of 32 bits per window
475 * (ctrl, base).
476 */
477 if (win < 8)
478 return win << 4;
479 else
480 return 0x90 + ((win - 8) << 3);
481}
482
483static unsigned int mv78xx0_mbus_win_offset(int win)
484{
485 if (win < 8)
486 return win << 4;
487 else
488 return 0x900 + ((win - 8) << 4);
489}
490
491static void __init
492mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
493{
494 int i;
495 int cs;
496
497 mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
498
499 for (i = 0, cs = 0; i < 4; i++) {
500 u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
501 u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
502
503 /*
504 * We only take care of entries for which the chip
505 * select is enabled, and that don't have high base
506 * address bits set (devices can only access the first
507 * 32 bits of the memory).
508 */
509 if ((size & DDR_SIZE_ENABLED) &&
510 !(base & DDR_BASE_CS_HIGH_MASK)) {
511 struct mbus_dram_window *w;
512
513 w = &mvebu_mbus_dram_info.cs[cs++];
514 w->cs_index = i;
515 w->mbus_attr = 0xf & ~(1 << i);
516 if (mbus->hw_io_coherency)
517 w->mbus_attr |= ATTR_HW_COHERENCY;
518 w->base = base & DDR_BASE_CS_LOW_MASK;
519 w->size = (size | ~DDR_SIZE_MASK) + 1;
520 }
521 }
522 mvebu_mbus_dram_info.num_cs = cs;
523}
524
525static void __init
526mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state *mbus)
527{
528 int i;
529 int cs;
530
531 mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
532
533 for (i = 0, cs = 0; i < 2; i++) {
534 u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
535
536 /*
537 * Chip select enabled?
538 */
539 if (map & 1) {
540 struct mbus_dram_window *w;
541
542 w = &mvebu_mbus_dram_info.cs[cs++];
543 w->cs_index = i;
544 w->mbus_attr = 0; /* CS address decoding done inside */
545 /* the DDR controller, no need to */
546 /* provide attributes */
547 w->base = map & 0xff800000;
548 w->size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
549 }
550 }
551
552 mvebu_mbus_dram_info.num_cs = cs;
553}
554
Thomas Petazzoni59cb2fc2013-07-26 10:17:53 -0300555static const struct mvebu_mbus_soc_data armada_370_xp_mbus_data = {
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100556 .num_wins = 20,
557 .num_remappable_wins = 8,
558 .win_cfg_offset = armada_370_xp_mbus_win_offset,
559 .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
560 .show_cpu_target = mvebu_sdram_debug_show_orion,
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100561};
562
563static const struct mvebu_mbus_soc_data kirkwood_mbus_data = {
564 .num_wins = 8,
565 .num_remappable_wins = 4,
566 .win_cfg_offset = orion_mbus_win_offset,
567 .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
568 .show_cpu_target = mvebu_sdram_debug_show_orion,
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100569};
570
571static const struct mvebu_mbus_soc_data dove_mbus_data = {
572 .num_wins = 8,
573 .num_remappable_wins = 4,
574 .win_cfg_offset = orion_mbus_win_offset,
575 .setup_cpu_target = mvebu_mbus_dove_setup_cpu_target,
576 .show_cpu_target = mvebu_sdram_debug_show_dove,
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100577};
578
579/*
580 * Some variants of Orion5x have 4 remappable windows, some other have
581 * only two of them.
582 */
583static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data = {
584 .num_wins = 8,
585 .num_remappable_wins = 4,
586 .win_cfg_offset = orion_mbus_win_offset,
587 .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
588 .show_cpu_target = mvebu_sdram_debug_show_orion,
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100589};
590
591static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data = {
592 .num_wins = 8,
593 .num_remappable_wins = 2,
594 .win_cfg_offset = orion_mbus_win_offset,
595 .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
596 .show_cpu_target = mvebu_sdram_debug_show_orion,
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100597};
598
599static const struct mvebu_mbus_soc_data mv78xx0_mbus_data = {
600 .num_wins = 14,
601 .num_remappable_wins = 8,
602 .win_cfg_offset = mv78xx0_mbus_win_offset,
603 .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
604 .show_cpu_target = mvebu_sdram_debug_show_orion,
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100605};
606
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100607static const struct of_device_id of_mvebu_mbus_ids[] = {
608 { .compatible = "marvell,armada370-mbus",
Thomas Petazzoni59cb2fc2013-07-26 10:17:53 -0300609 .data = &armada_370_xp_mbus_data, },
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100610 { .compatible = "marvell,armadaxp-mbus",
Thomas Petazzoni59cb2fc2013-07-26 10:17:53 -0300611 .data = &armada_370_xp_mbus_data, },
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100612 { .compatible = "marvell,kirkwood-mbus",
613 .data = &kirkwood_mbus_data, },
614 { .compatible = "marvell,dove-mbus",
615 .data = &dove_mbus_data, },
616 { .compatible = "marvell,orion5x-88f5281-mbus",
617 .data = &orion5x_4win_mbus_data, },
618 { .compatible = "marvell,orion5x-88f5182-mbus",
619 .data = &orion5x_2win_mbus_data, },
620 { .compatible = "marvell,orion5x-88f5181-mbus",
621 .data = &orion5x_2win_mbus_data, },
622 { .compatible = "marvell,orion5x-88f6183-mbus",
623 .data = &orion5x_4win_mbus_data, },
624 { .compatible = "marvell,mv78xx0-mbus",
625 .data = &mv78xx0_mbus_data, },
626 { },
627};
628
629/*
630 * Public API of the driver
631 */
Thomas Petazzoni6a63b092013-07-26 10:17:39 -0300632int mvebu_mbus_add_window_remap_by_id(unsigned int target,
633 unsigned int attribute,
634 phys_addr_t base, size_t size,
635 phys_addr_t remap)
636{
637 struct mvebu_mbus_state *s = &mbus_state;
638
639 if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
640 pr_err("cannot add window '%x:%x', conflicts with another window\n",
641 target, attribute);
642 return -EINVAL;
643 }
644
645 return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
646}
647
Thomas Petazzoni6a63b092013-07-26 10:17:39 -0300648int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
649 phys_addr_t base, size_t size)
650{
651 return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
652 size, MVEBU_MBUS_NO_REMAP);
653}
654
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100655int mvebu_mbus_del_window(phys_addr_t base, size_t size)
656{
657 int win;
658
659 win = mvebu_mbus_find_window(&mbus_state, base, size);
660 if (win < 0)
661 return win;
662
663 mvebu_mbus_disable_window(&mbus_state, win);
664 return 0;
665}
666
Ezequiel Garcia79d94682013-07-26 10:17:47 -0300667void mvebu_mbus_get_pcie_mem_aperture(struct resource *res)
668{
669 if (!res)
670 return;
671 *res = mbus_state.pcie_mem_aperture;
672}
673
674void mvebu_mbus_get_pcie_io_aperture(struct resource *res)
675{
676 if (!res)
677 return;
678 *res = mbus_state.pcie_io_aperture;
679}
680
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100681static __init int mvebu_mbus_debugfs_init(void)
682{
683 struct mvebu_mbus_state *s = &mbus_state;
684
685 /*
686 * If no base has been initialized, doesn't make sense to
687 * register the debugfs entries. We may be on a multiplatform
688 * kernel that isn't running a Marvell EBU SoC.
689 */
690 if (!s->mbuswins_base)
691 return 0;
692
693 s->debugfs_root = debugfs_create_dir("mvebu-mbus", NULL);
694 if (s->debugfs_root) {
695 s->debugfs_sdram = debugfs_create_file("sdram", S_IRUGO,
696 s->debugfs_root, NULL,
697 &mvebu_sdram_debug_fops);
698 s->debugfs_devs = debugfs_create_file("devices", S_IRUGO,
699 s->debugfs_root, NULL,
700 &mvebu_devs_debug_fops);
701 }
702
703 return 0;
704}
705fs_initcall(mvebu_mbus_debugfs_init);
706
Ezequiel Garcia6bd6b3c2013-07-26 10:17:44 -0300707static int __init mvebu_mbus_common_init(struct mvebu_mbus_state *mbus,
708 phys_addr_t mbuswins_phys_base,
709 size_t mbuswins_size,
710 phys_addr_t sdramwins_phys_base,
711 size_t sdramwins_size)
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100712{
Jisheng Zhang4ec7fc42013-08-27 12:41:15 +0800713 struct device_node *np;
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100714 int win;
715
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100716 mbus->mbuswins_base = ioremap(mbuswins_phys_base, mbuswins_size);
717 if (!mbus->mbuswins_base)
718 return -ENOMEM;
719
720 mbus->sdramwins_base = ioremap(sdramwins_phys_base, sdramwins_size);
721 if (!mbus->sdramwins_base) {
722 iounmap(mbus_state.mbuswins_base);
723 return -ENOMEM;
724 }
725
Jisheng Zhang4ec7fc42013-08-27 12:41:15 +0800726 np = of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric");
727 if (np) {
Neil Greatorexfe0cd962013-03-30 20:41:20 +0000728 mbus->hw_io_coherency = 1;
Jisheng Zhang4ec7fc42013-08-27 12:41:15 +0800729 of_node_put(np);
730 }
Neil Greatorexfe0cd962013-03-30 20:41:20 +0000731
Thomas Petazzonifddddb52013-03-21 17:59:14 +0100732 for (win = 0; win < mbus->soc->num_wins; win++)
733 mvebu_mbus_disable_window(mbus, win);
734
735 mbus->soc->setup_cpu_target(mbus);
736
737 return 0;
738}
Ezequiel Garcia6bd6b3c2013-07-26 10:17:44 -0300739
740int __init mvebu_mbus_init(const char *soc, phys_addr_t mbuswins_phys_base,
741 size_t mbuswins_size,
742 phys_addr_t sdramwins_phys_base,
743 size_t sdramwins_size)
744{
745 const struct of_device_id *of_id;
746
Dan Carpenter7663cfd2013-11-13 10:50:24 +0300747 for (of_id = of_mvebu_mbus_ids; of_id->compatible[0]; of_id++)
Ezequiel Garcia6bd6b3c2013-07-26 10:17:44 -0300748 if (!strcmp(of_id->compatible, soc))
749 break;
750
Dan Carpenter7663cfd2013-11-13 10:50:24 +0300751 if (!of_id->compatible[0]) {
Ezequiel Garcia6bd6b3c2013-07-26 10:17:44 -0300752 pr_err("could not find a matching SoC family\n");
753 return -ENODEV;
754 }
755
756 mbus_state.soc = of_id->data;
757
758 return mvebu_mbus_common_init(&mbus_state,
759 mbuswins_phys_base,
760 mbuswins_size,
761 sdramwins_phys_base,
762 sdramwins_size);
763}
Ezequiel Garcia6839cfa2013-07-26 10:17:45 -0300764
765#ifdef CONFIG_OF
Ezequiel Garciabb24cab2013-07-26 10:17:46 -0300766/*
767 * The window IDs in the ranges DT property have the following format:
768 * - bits 28 to 31: MBus custom field
769 * - bits 24 to 27: window target ID
770 * - bits 16 to 23: window attribute ID
771 * - bits 0 to 15: unused
772 */
773#define CUSTOM(id) (((id) & 0xF0000000) >> 24)
774#define TARGET(id) (((id) & 0x0F000000) >> 24)
775#define ATTR(id) (((id) & 0x00FF0000) >> 16)
776
777static int __init mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
778 u32 base, u32 size,
779 u8 target, u8 attr)
780{
Ezequiel Garciabb24cab2013-07-26 10:17:46 -0300781 if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
Thomas Petazzonied843a72013-07-26 10:17:51 -0300782 pr_err("cannot add window '%04x:%04x', conflicts with another window\n",
783 target, attr);
Ezequiel Garciabb24cab2013-07-26 10:17:46 -0300784 return -EBUSY;
785 }
786
787 if (mvebu_mbus_alloc_window(mbus, base, size, MVEBU_MBUS_NO_REMAP,
788 target, attr)) {
Thomas Petazzonied843a72013-07-26 10:17:51 -0300789 pr_err("cannot add window '%04x:%04x', too many windows\n",
790 target, attr);
Ezequiel Garciabb24cab2013-07-26 10:17:46 -0300791 return -ENOMEM;
792 }
793 return 0;
794}
795
796static int __init
797mbus_parse_ranges(struct device_node *node,
798 int *addr_cells, int *c_addr_cells, int *c_size_cells,
799 int *cell_count, const __be32 **ranges_start,
800 const __be32 **ranges_end)
801{
802 const __be32 *prop;
803 int ranges_len, tuple_len;
804
805 /* Allow a node with no 'ranges' property */
806 *ranges_start = of_get_property(node, "ranges", &ranges_len);
807 if (*ranges_start == NULL) {
808 *addr_cells = *c_addr_cells = *c_size_cells = *cell_count = 0;
809 *ranges_start = *ranges_end = NULL;
810 return 0;
811 }
812 *ranges_end = *ranges_start + ranges_len / sizeof(__be32);
813
814 *addr_cells = of_n_addr_cells(node);
815
816 prop = of_get_property(node, "#address-cells", NULL);
817 *c_addr_cells = be32_to_cpup(prop);
818
819 prop = of_get_property(node, "#size-cells", NULL);
820 *c_size_cells = be32_to_cpup(prop);
821
822 *cell_count = *addr_cells + *c_addr_cells + *c_size_cells;
823 tuple_len = (*cell_count) * sizeof(__be32);
824
825 if (ranges_len % tuple_len) {
826 pr_warn("malformed ranges entry '%s'\n", node->name);
827 return -EINVAL;
828 }
829 return 0;
830}
831
832static int __init mbus_dt_setup(struct mvebu_mbus_state *mbus,
833 struct device_node *np)
834{
835 int addr_cells, c_addr_cells, c_size_cells;
836 int i, ret, cell_count;
837 const __be32 *r, *ranges_start, *ranges_end;
838
839 ret = mbus_parse_ranges(np, &addr_cells, &c_addr_cells,
840 &c_size_cells, &cell_count,
841 &ranges_start, &ranges_end);
842 if (ret < 0)
843 return ret;
844
845 for (i = 0, r = ranges_start; r < ranges_end; r += cell_count, i++) {
846 u32 windowid, base, size;
847 u8 target, attr;
848
849 /*
850 * An entry with a non-zero custom field do not
851 * correspond to a static window, so skip it.
852 */
853 windowid = of_read_number(r, 1);
854 if (CUSTOM(windowid))
855 continue;
856
857 target = TARGET(windowid);
858 attr = ATTR(windowid);
859
860 base = of_read_number(r + c_addr_cells, addr_cells);
861 size = of_read_number(r + c_addr_cells + addr_cells,
862 c_size_cells);
863 ret = mbus_dt_setup_win(mbus, base, size, target, attr);
864 if (ret < 0)
865 return ret;
866 }
867 return 0;
868}
869
Ezequiel Garcia79d94682013-07-26 10:17:47 -0300870static void __init mvebu_mbus_get_pcie_resources(struct device_node *np,
871 struct resource *mem,
872 struct resource *io)
873{
874 u32 reg[2];
875 int ret;
876
877 /*
Jason Gunthorpe8553bca2013-09-17 14:11:04 -0600878 * These are optional, so we make sure that resource_size(x) will
879 * return 0.
Ezequiel Garcia79d94682013-07-26 10:17:47 -0300880 */
881 memset(mem, 0, sizeof(struct resource));
Jason Gunthorpe8553bca2013-09-17 14:11:04 -0600882 mem->end = -1;
Ezequiel Garcia79d94682013-07-26 10:17:47 -0300883 memset(io, 0, sizeof(struct resource));
Jason Gunthorpe8553bca2013-09-17 14:11:04 -0600884 io->end = -1;
Ezequiel Garcia79d94682013-07-26 10:17:47 -0300885
886 ret = of_property_read_u32_array(np, "pcie-mem-aperture", reg, ARRAY_SIZE(reg));
887 if (!ret) {
888 mem->start = reg[0];
Jason Gunthorpea723e752014-02-12 15:57:07 -0700889 mem->end = mem->start + reg[1] - 1;
Ezequiel Garcia79d94682013-07-26 10:17:47 -0300890 mem->flags = IORESOURCE_MEM;
891 }
892
893 ret = of_property_read_u32_array(np, "pcie-io-aperture", reg, ARRAY_SIZE(reg));
894 if (!ret) {
895 io->start = reg[0];
Jason Gunthorpea723e752014-02-12 15:57:07 -0700896 io->end = io->start + reg[1] - 1;
Ezequiel Garcia79d94682013-07-26 10:17:47 -0300897 io->flags = IORESOURCE_IO;
898 }
899}
900
Ezequiel Garcia6839cfa2013-07-26 10:17:45 -0300901int __init mvebu_mbus_dt_init(void)
902{
903 struct resource mbuswins_res, sdramwins_res;
904 struct device_node *np, *controller;
905 const struct of_device_id *of_id;
906 const __be32 *prop;
907 int ret;
908
Josh Cartwright087a4ab2014-02-11 10:24:00 -0600909 np = of_find_matching_node_and_match(NULL, of_mvebu_mbus_ids, &of_id);
Ezequiel Garcia6839cfa2013-07-26 10:17:45 -0300910 if (!np) {
911 pr_err("could not find a matching SoC family\n");
912 return -ENODEV;
913 }
914
Ezequiel Garcia6839cfa2013-07-26 10:17:45 -0300915 mbus_state.soc = of_id->data;
916
917 prop = of_get_property(np, "controller", NULL);
918 if (!prop) {
919 pr_err("required 'controller' property missing\n");
920 return -EINVAL;
921 }
922
923 controller = of_find_node_by_phandle(be32_to_cpup(prop));
924 if (!controller) {
925 pr_err("could not find an 'mbus-controller' node\n");
926 return -ENODEV;
927 }
928
929 if (of_address_to_resource(controller, 0, &mbuswins_res)) {
930 pr_err("cannot get MBUS register address\n");
931 return -EINVAL;
932 }
933
934 if (of_address_to_resource(controller, 1, &sdramwins_res)) {
935 pr_err("cannot get SDRAM register address\n");
936 return -EINVAL;
937 }
938
Ezequiel Garcia79d94682013-07-26 10:17:47 -0300939 /* Get optional pcie-{mem,io}-aperture properties */
940 mvebu_mbus_get_pcie_resources(np, &mbus_state.pcie_mem_aperture,
941 &mbus_state.pcie_io_aperture);
942
Ezequiel Garcia6839cfa2013-07-26 10:17:45 -0300943 ret = mvebu_mbus_common_init(&mbus_state,
944 mbuswins_res.start,
945 resource_size(&mbuswins_res),
946 sdramwins_res.start,
947 resource_size(&sdramwins_res));
Ezequiel Garciabb24cab2013-07-26 10:17:46 -0300948 if (ret)
949 return ret;
950
951 /* Setup statically declared windows in the DT */
952 return mbus_dt_setup(&mbus_state, np);
Ezequiel Garcia6839cfa2013-07-26 10:17:45 -0300953}
954#endif