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