bus: mvebu-mbus: fix support of MBus window 13
[firefly-linux-kernel-4.4.55.git] / drivers / bus / mvebu-mbus.c
1 /*
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
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/mbus.h>
56 #include <linux/io.h>
57 #include <linux/ioport.h>
58 #include <linux/of.h>
59 #include <linux/of_address.h>
60 #include <linux/debugfs.h>
61
62 /*
63  * DDR target is the same on all platforms.
64  */
65 #define TARGET_DDR              0
66
67 /*
68  * CPU Address Decode Windows registers
69  */
70 #define WIN_CTRL_OFF            0x0000
71 #define   WIN_CTRL_ENABLE       BIT(0)
72 #define   WIN_CTRL_TGT_MASK     0xf0
73 #define   WIN_CTRL_TGT_SHIFT    4
74 #define   WIN_CTRL_ATTR_MASK    0xff00
75 #define   WIN_CTRL_ATTR_SHIFT   8
76 #define   WIN_CTRL_SIZE_MASK    0xffff0000
77 #define   WIN_CTRL_SIZE_SHIFT   16
78 #define WIN_BASE_OFF            0x0004
79 #define   WIN_BASE_LOW          0xffff0000
80 #define   WIN_BASE_HIGH         0xf
81 #define WIN_REMAP_LO_OFF        0x0008
82 #define   WIN_REMAP_LOW         0xffff0000
83 #define WIN_REMAP_HI_OFF        0x000c
84
85 #define ATTR_HW_COHERENCY       (0x1 << 4)
86
87 #define DDR_BASE_CS_OFF(n)      (0x0000 + ((n) << 3))
88 #define  DDR_BASE_CS_HIGH_MASK  0xf
89 #define  DDR_BASE_CS_LOW_MASK   0xff000000
90 #define DDR_SIZE_CS_OFF(n)      (0x0004 + ((n) << 3))
91 #define  DDR_SIZE_ENABLED       BIT(0)
92 #define  DDR_SIZE_CS_MASK       0x1c
93 #define  DDR_SIZE_CS_SHIFT      2
94 #define  DDR_SIZE_MASK          0xff000000
95
96 #define DOVE_DDR_BASE_CS_OFF(n) ((n) << 4)
97
98 struct mvebu_mbus_mapping {
99         const char *name;
100         u8 target;
101         u8 attr;
102         u8 attrmask;
103 };
104
105 /*
106  * Masks used for the 'attrmask' field of mvebu_mbus_mapping. They
107  * allow to get the real attribute value, discarding the special bits
108  * used to select a PCI MEM region or a PCI WA region. This allows the
109  * debugfs code to reverse-match the name of a device from its
110  * target/attr values.
111  *
112  * For all devices except PCI, all bits of 'attr' must be
113  * considered. For most SoCs, only bit 3 should be ignored (it allows
114  * to select between PCI MEM and PCI I/O). On Orion5x however, there
115  * is the special bit 5 to select a PCI WA region.
116  */
117 #define MAPDEF_NOMASK       0xff
118 #define MAPDEF_PCIMASK      0xf7
119 #define MAPDEF_ORIONPCIMASK 0xd7
120
121 /* Macro used to define one mvebu_mbus_mapping entry */
122 #define MAPDEF(__n, __t, __a, __m) \
123         { .name = __n, .target = __t, .attr = __a, .attrmask = __m }
124
125 struct mvebu_mbus_state;
126
127 struct mvebu_mbus_soc_data {
128         unsigned int num_wins;
129         unsigned int num_remappable_wins;
130         unsigned int (*win_cfg_offset)(const int win);
131         void (*setup_cpu_target)(struct mvebu_mbus_state *s);
132         int (*show_cpu_target)(struct mvebu_mbus_state *s,
133                                struct seq_file *seq, void *v);
134         const struct mvebu_mbus_mapping *map;
135 };
136
137 struct mvebu_mbus_state {
138         void __iomem *mbuswins_base;
139         void __iomem *sdramwins_base;
140         struct dentry *debugfs_root;
141         struct dentry *debugfs_sdram;
142         struct dentry *debugfs_devs;
143         const struct mvebu_mbus_soc_data *soc;
144         int hw_io_coherency;
145 };
146
147 static struct mvebu_mbus_state mbus_state;
148
149 static struct mbus_dram_target_info mvebu_mbus_dram_info;
150 const struct mbus_dram_target_info *mv_mbus_dram_info(void)
151 {
152         return &mvebu_mbus_dram_info;
153 }
154 EXPORT_SYMBOL_GPL(mv_mbus_dram_info);
155
156 /*
157  * Functions to manipulate the address decoding windows
158  */
159
160 static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
161                                    int win, int *enabled, u64 *base,
162                                    u32 *size, u8 *target, u8 *attr,
163                                    u64 *remap)
164 {
165         void __iomem *addr = mbus->mbuswins_base +
166                 mbus->soc->win_cfg_offset(win);
167         u32 basereg = readl(addr + WIN_BASE_OFF);
168         u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
169
170         if (!(ctrlreg & WIN_CTRL_ENABLE)) {
171                 *enabled = 0;
172                 return;
173         }
174
175         *enabled = 1;
176         *base = ((u64)basereg & WIN_BASE_HIGH) << 32;
177         *base |= (basereg & WIN_BASE_LOW);
178         *size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
179
180         if (target)
181                 *target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
182
183         if (attr)
184                 *attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
185
186         if (remap) {
187                 if (win < mbus->soc->num_remappable_wins) {
188                         u32 remap_low = readl(addr + WIN_REMAP_LO_OFF);
189                         u32 remap_hi  = readl(addr + WIN_REMAP_HI_OFF);
190                         *remap = ((u64)remap_hi << 32) | remap_low;
191                 } else
192                         *remap = 0;
193         }
194 }
195
196 static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
197                                       int win)
198 {
199         void __iomem *addr;
200
201         addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
202
203         writel(0, addr + WIN_BASE_OFF);
204         writel(0, addr + WIN_CTRL_OFF);
205         if (win < mbus->soc->num_remappable_wins) {
206                 writel(0, addr + WIN_REMAP_LO_OFF);
207                 writel(0, addr + WIN_REMAP_HI_OFF);
208         }
209 }
210
211 /* Checks whether the given window number is available */
212
213 /* On Armada XP, 375 and 38x the MBus window 13 has the remap
214  * capability, like windows 0 to 7. However, the mvebu-mbus driver
215  * isn't currently taking into account this special case, which means
216  * that when window 13 is actually used, the remap registers are left
217  * to 0, making the device using this MBus window unavailable. The
218  * quick fix for stable is to not use window 13. A follow up patch
219  * will correctly handle this window.
220 */
221 static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
222                                      const int win)
223 {
224         void __iomem *addr = mbus->mbuswins_base +
225                 mbus->soc->win_cfg_offset(win);
226         u32 ctrl = readl(addr + WIN_CTRL_OFF);
227
228         if (win == 13)
229                 return false;
230
231         return !(ctrl & WIN_CTRL_ENABLE);
232 }
233
234 /*
235  * Checks whether the given (base, base+size) area doesn't overlap an
236  * existing region
237  */
238 static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
239                                        phys_addr_t base, size_t size,
240                                        u8 target, u8 attr)
241 {
242         u64 end = (u64)base + size;
243         int win;
244
245         for (win = 0; win < mbus->soc->num_wins; win++) {
246                 u64 wbase, wend;
247                 u32 wsize;
248                 u8 wtarget, wattr;
249                 int enabled;
250
251                 mvebu_mbus_read_window(mbus, win,
252                                        &enabled, &wbase, &wsize,
253                                        &wtarget, &wattr, NULL);
254
255                 if (!enabled)
256                         continue;
257
258                 wend = wbase + wsize;
259
260                 /*
261                  * Check if the current window overlaps with the
262                  * proposed physical range
263                  */
264                 if ((u64)base < wend && end > wbase)
265                         return 0;
266         }
267
268         return 1;
269 }
270
271 static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
272                                   phys_addr_t base, size_t size)
273 {
274         int win;
275
276         for (win = 0; win < mbus->soc->num_wins; win++) {
277                 u64 wbase;
278                 u32 wsize;
279                 int enabled;
280
281                 mvebu_mbus_read_window(mbus, win,
282                                        &enabled, &wbase, &wsize,
283                                        NULL, NULL, NULL);
284
285                 if (!enabled)
286                         continue;
287
288                 if (base == wbase && size == wsize)
289                         return win;
290         }
291
292         return -ENODEV;
293 }
294
295 static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
296                                    int win, phys_addr_t base, size_t size,
297                                    phys_addr_t remap, u8 target,
298                                    u8 attr)
299 {
300         void __iomem *addr = mbus->mbuswins_base +
301                 mbus->soc->win_cfg_offset(win);
302         u32 ctrl, remap_addr;
303
304         ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
305                 (attr << WIN_CTRL_ATTR_SHIFT)    |
306                 (target << WIN_CTRL_TGT_SHIFT)   |
307                 WIN_CTRL_ENABLE;
308
309         writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
310         writel(ctrl, addr + WIN_CTRL_OFF);
311         if (win < mbus->soc->num_remappable_wins) {
312                 if (remap == MVEBU_MBUS_NO_REMAP)
313                         remap_addr = base;
314                 else
315                         remap_addr = remap;
316                 writel(remap_addr & WIN_REMAP_LOW, addr + WIN_REMAP_LO_OFF);
317                 writel(0, addr + WIN_REMAP_HI_OFF);
318         }
319
320         return 0;
321 }
322
323 static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
324                                    phys_addr_t base, size_t size,
325                                    phys_addr_t remap, u8 target,
326                                    u8 attr)
327 {
328         int win;
329
330         if (remap == MVEBU_MBUS_NO_REMAP) {
331                 for (win = mbus->soc->num_remappable_wins;
332                      win < mbus->soc->num_wins; win++)
333                         if (mvebu_mbus_window_is_free(mbus, win))
334                                 return mvebu_mbus_setup_window(mbus, win, base,
335                                                                size, remap,
336                                                                target, attr);
337         }
338
339
340         for (win = 0; win < mbus->soc->num_wins; win++)
341                 if (mvebu_mbus_window_is_free(mbus, win))
342                         return mvebu_mbus_setup_window(mbus, win, base, size,
343                                                        remap, target, attr);
344
345         return -ENOMEM;
346 }
347
348 /*
349  * Debugfs debugging
350  */
351
352 /* Common function used for Dove, Kirkwood, Armada 370/XP and Orion 5x */
353 static int mvebu_sdram_debug_show_orion(struct mvebu_mbus_state *mbus,
354                                         struct seq_file *seq, void *v)
355 {
356         int i;
357
358         for (i = 0; i < 4; i++) {
359                 u32 basereg = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
360                 u32 sizereg = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
361                 u64 base;
362                 u32 size;
363
364                 if (!(sizereg & DDR_SIZE_ENABLED)) {
365                         seq_printf(seq, "[%d] disabled\n", i);
366                         continue;
367                 }
368
369                 base = ((u64)basereg & DDR_BASE_CS_HIGH_MASK) << 32;
370                 base |= basereg & DDR_BASE_CS_LOW_MASK;
371                 size = (sizereg | ~DDR_SIZE_MASK);
372
373                 seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
374                            i, (unsigned long long)base,
375                            (unsigned long long)base + size + 1,
376                            (sizereg & DDR_SIZE_CS_MASK) >> DDR_SIZE_CS_SHIFT);
377         }
378
379         return 0;
380 }
381
382 /* Special function for Dove */
383 static int mvebu_sdram_debug_show_dove(struct mvebu_mbus_state *mbus,
384                                        struct seq_file *seq, void *v)
385 {
386         int i;
387
388         for (i = 0; i < 2; i++) {
389                 u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
390                 u64 base;
391                 u32 size;
392
393                 if (!(map & 1)) {
394                         seq_printf(seq, "[%d] disabled\n", i);
395                         continue;
396                 }
397
398                 base = map & 0xff800000;
399                 size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
400
401                 seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
402                            i, (unsigned long long)base,
403                            (unsigned long long)base + size, i);
404         }
405
406         return 0;
407 }
408
409 static int mvebu_sdram_debug_show(struct seq_file *seq, void *v)
410 {
411         struct mvebu_mbus_state *mbus = &mbus_state;
412         return mbus->soc->show_cpu_target(mbus, seq, v);
413 }
414
415 static int mvebu_sdram_debug_open(struct inode *inode, struct file *file)
416 {
417         return single_open(file, mvebu_sdram_debug_show, inode->i_private);
418 }
419
420 static const struct file_operations mvebu_sdram_debug_fops = {
421         .open = mvebu_sdram_debug_open,
422         .read = seq_read,
423         .llseek = seq_lseek,
424         .release = single_release,
425 };
426
427 static int mvebu_devs_debug_show(struct seq_file *seq, void *v)
428 {
429         struct mvebu_mbus_state *mbus = &mbus_state;
430         int win;
431
432         for (win = 0; win < mbus->soc->num_wins; win++) {
433                 u64 wbase, wremap;
434                 u32 wsize;
435                 u8 wtarget, wattr;
436                 int enabled, i;
437                 const char *name;
438
439                 mvebu_mbus_read_window(mbus, win,
440                                        &enabled, &wbase, &wsize,
441                                        &wtarget, &wattr, &wremap);
442
443                 if (!enabled) {
444                         seq_printf(seq, "[%02d] disabled\n", win);
445                         continue;
446                 }
447
448
449                 for (i = 0; mbus->soc->map[i].name; i++)
450                         if (mbus->soc->map[i].target == wtarget &&
451                             mbus->soc->map[i].attr ==
452                             (wattr & mbus->soc->map[i].attrmask))
453                                 break;
454
455                 name = mbus->soc->map[i].name ?: "unknown";
456
457                 seq_printf(seq, "[%02d] %016llx - %016llx : %s",
458                            win, (unsigned long long)wbase,
459                            (unsigned long long)(wbase + wsize), name);
460
461                 if (win < mbus->soc->num_remappable_wins) {
462                         seq_printf(seq, " (remap %016llx)\n",
463                                    (unsigned long long)wremap);
464                 } else
465                         seq_printf(seq, "\n");
466         }
467
468         return 0;
469 }
470
471 static int mvebu_devs_debug_open(struct inode *inode, struct file *file)
472 {
473         return single_open(file, mvebu_devs_debug_show, inode->i_private);
474 }
475
476 static const struct file_operations mvebu_devs_debug_fops = {
477         .open = mvebu_devs_debug_open,
478         .read = seq_read,
479         .llseek = seq_lseek,
480         .release = single_release,
481 };
482
483 /*
484  * SoC-specific functions and definitions
485  */
486
487 static unsigned int orion_mbus_win_offset(int win)
488 {
489         return win << 4;
490 }
491
492 static unsigned int armada_370_xp_mbus_win_offset(int win)
493 {
494         /* The register layout is a bit annoying and the below code
495          * tries to cope with it.
496          * - At offset 0x0, there are the registers for the first 8
497          *   windows, with 4 registers of 32 bits per window (ctrl,
498          *   base, remap low, remap high)
499          * - Then at offset 0x80, there is a hole of 0x10 bytes for
500          *   the internal registers base address and internal units
501          *   sync barrier register.
502          * - Then at offset 0x90, there the registers for 12
503          *   windows, with only 2 registers of 32 bits per window
504          *   (ctrl, base).
505          */
506         if (win < 8)
507                 return win << 4;
508         else
509                 return 0x90 + ((win - 8) << 3);
510 }
511
512 static unsigned int mv78xx0_mbus_win_offset(int win)
513 {
514         if (win < 8)
515                 return win << 4;
516         else
517                 return 0x900 + ((win - 8) << 4);
518 }
519
520 static void __init
521 mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
522 {
523         int i;
524         int cs;
525
526         mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
527
528         for (i = 0, cs = 0; i < 4; i++) {
529                 u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
530                 u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
531
532                 /*
533                  * We only take care of entries for which the chip
534                  * select is enabled, and that don't have high base
535                  * address bits set (devices can only access the first
536                  * 32 bits of the memory).
537                  */
538                 if ((size & DDR_SIZE_ENABLED) &&
539                     !(base & DDR_BASE_CS_HIGH_MASK)) {
540                         struct mbus_dram_window *w;
541
542                         w = &mvebu_mbus_dram_info.cs[cs++];
543                         w->cs_index = i;
544                         w->mbus_attr = 0xf & ~(1 << i);
545                         if (mbus->hw_io_coherency)
546                                 w->mbus_attr |= ATTR_HW_COHERENCY;
547                         w->base = base & DDR_BASE_CS_LOW_MASK;
548                         w->size = (size | ~DDR_SIZE_MASK) + 1;
549                 }
550         }
551         mvebu_mbus_dram_info.num_cs = cs;
552 }
553
554 static void __init
555 mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state *mbus)
556 {
557         int i;
558         int cs;
559
560         mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
561
562         for (i = 0, cs = 0; i < 2; i++) {
563                 u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
564
565                 /*
566                  * Chip select enabled?
567                  */
568                 if (map & 1) {
569                         struct mbus_dram_window *w;
570
571                         w = &mvebu_mbus_dram_info.cs[cs++];
572                         w->cs_index = i;
573                         w->mbus_attr = 0; /* CS address decoding done inside */
574                                           /* the DDR controller, no need to  */
575                                           /* provide attributes */
576                         w->base = map & 0xff800000;
577                         w->size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
578                 }
579         }
580
581         mvebu_mbus_dram_info.num_cs = cs;
582 }
583
584 static const struct mvebu_mbus_mapping armada_370_map[] = {
585         MAPDEF("bootrom",     1, 0xe0, MAPDEF_NOMASK),
586         MAPDEF("devbus-boot", 1, 0x2f, MAPDEF_NOMASK),
587         MAPDEF("devbus-cs0",  1, 0x3e, MAPDEF_NOMASK),
588         MAPDEF("devbus-cs1",  1, 0x3d, MAPDEF_NOMASK),
589         MAPDEF("devbus-cs2",  1, 0x3b, MAPDEF_NOMASK),
590         MAPDEF("devbus-cs3",  1, 0x37, MAPDEF_NOMASK),
591         MAPDEF("pcie0.0",     4, 0xe0, MAPDEF_PCIMASK),
592         MAPDEF("pcie1.0",     8, 0xe0, MAPDEF_PCIMASK),
593         {},
594 };
595
596 static const struct mvebu_mbus_soc_data armada_370_mbus_data = {
597         .num_wins            = 20,
598         .num_remappable_wins = 8,
599         .win_cfg_offset      = armada_370_xp_mbus_win_offset,
600         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
601         .show_cpu_target     = mvebu_sdram_debug_show_orion,
602         .map                 = armada_370_map,
603 };
604
605 static const struct mvebu_mbus_mapping armada_xp_map[] = {
606         MAPDEF("bootrom",     1, 0x1d, MAPDEF_NOMASK),
607         MAPDEF("devbus-boot", 1, 0x2f, MAPDEF_NOMASK),
608         MAPDEF("devbus-cs0",  1, 0x3e, MAPDEF_NOMASK),
609         MAPDEF("devbus-cs1",  1, 0x3d, MAPDEF_NOMASK),
610         MAPDEF("devbus-cs2",  1, 0x3b, MAPDEF_NOMASK),
611         MAPDEF("devbus-cs3",  1, 0x37, MAPDEF_NOMASK),
612         MAPDEF("pcie0.0",     4, 0xe0, MAPDEF_PCIMASK),
613         MAPDEF("pcie0.1",     4, 0xd0, MAPDEF_PCIMASK),
614         MAPDEF("pcie0.2",     4, 0xb0, MAPDEF_PCIMASK),
615         MAPDEF("pcie0.3",     4, 0x70, MAPDEF_PCIMASK),
616         MAPDEF("pcie1.0",     8, 0xe0, MAPDEF_PCIMASK),
617         MAPDEF("pcie1.1",     8, 0xd0, MAPDEF_PCIMASK),
618         MAPDEF("pcie1.2",     8, 0xb0, MAPDEF_PCIMASK),
619         MAPDEF("pcie1.3",     8, 0x70, MAPDEF_PCIMASK),
620         MAPDEF("pcie2.0",     4, 0xf0, MAPDEF_PCIMASK),
621         MAPDEF("pcie3.0",     8, 0xf0, MAPDEF_PCIMASK),
622         {},
623 };
624
625 static const struct mvebu_mbus_soc_data armada_xp_mbus_data = {
626         .num_wins            = 20,
627         .num_remappable_wins = 8,
628         .win_cfg_offset      = armada_370_xp_mbus_win_offset,
629         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
630         .show_cpu_target     = mvebu_sdram_debug_show_orion,
631         .map                 = armada_xp_map,
632 };
633
634 static const struct mvebu_mbus_mapping kirkwood_map[] = {
635         MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
636         MAPDEF("pcie1.0", 4, 0xd0, MAPDEF_PCIMASK),
637         MAPDEF("sram",    3, 0x01, MAPDEF_NOMASK),
638         MAPDEF("nand",    1, 0x2f, MAPDEF_NOMASK),
639         {},
640 };
641
642 static const struct mvebu_mbus_soc_data kirkwood_mbus_data = {
643         .num_wins            = 8,
644         .num_remappable_wins = 4,
645         .win_cfg_offset      = orion_mbus_win_offset,
646         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
647         .show_cpu_target     = mvebu_sdram_debug_show_orion,
648         .map                 = kirkwood_map,
649 };
650
651 static const struct mvebu_mbus_mapping dove_map[] = {
652         MAPDEF("pcie0.0",    0x4, 0xe0, MAPDEF_PCIMASK),
653         MAPDEF("pcie1.0",    0x8, 0xe0, MAPDEF_PCIMASK),
654         MAPDEF("cesa",       0x3, 0x01, MAPDEF_NOMASK),
655         MAPDEF("bootrom",    0x1, 0xfd, MAPDEF_NOMASK),
656         MAPDEF("scratchpad", 0xd, 0x0, MAPDEF_NOMASK),
657         {},
658 };
659
660 static const struct mvebu_mbus_soc_data dove_mbus_data = {
661         .num_wins            = 8,
662         .num_remappable_wins = 4,
663         .win_cfg_offset      = orion_mbus_win_offset,
664         .setup_cpu_target    = mvebu_mbus_dove_setup_cpu_target,
665         .show_cpu_target     = mvebu_sdram_debug_show_dove,
666         .map                 = dove_map,
667 };
668
669 static const struct mvebu_mbus_mapping orion5x_map[] = {
670         MAPDEF("pcie0.0",     4, 0x51, MAPDEF_ORIONPCIMASK),
671         MAPDEF("pci0.0",      3, 0x51, MAPDEF_ORIONPCIMASK),
672         MAPDEF("devbus-boot", 1, 0x0f, MAPDEF_NOMASK),
673         MAPDEF("devbus-cs0",  1, 0x1e, MAPDEF_NOMASK),
674         MAPDEF("devbus-cs1",  1, 0x1d, MAPDEF_NOMASK),
675         MAPDEF("devbus-cs2",  1, 0x1b, MAPDEF_NOMASK),
676         MAPDEF("sram",        0, 0x00, MAPDEF_NOMASK),
677         {},
678 };
679
680 /*
681  * Some variants of Orion5x have 4 remappable windows, some other have
682  * only two of them.
683  */
684 static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data = {
685         .num_wins            = 8,
686         .num_remappable_wins = 4,
687         .win_cfg_offset      = orion_mbus_win_offset,
688         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
689         .show_cpu_target     = mvebu_sdram_debug_show_orion,
690         .map                 = orion5x_map,
691 };
692
693 static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data = {
694         .num_wins            = 8,
695         .num_remappable_wins = 2,
696         .win_cfg_offset      = orion_mbus_win_offset,
697         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
698         .show_cpu_target     = mvebu_sdram_debug_show_orion,
699         .map                 = orion5x_map,
700 };
701
702 static const struct mvebu_mbus_mapping mv78xx0_map[] = {
703         MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
704         MAPDEF("pcie0.1", 4, 0xd0, MAPDEF_PCIMASK),
705         MAPDEF("pcie0.2", 4, 0xb0, MAPDEF_PCIMASK),
706         MAPDEF("pcie0.3", 4, 0x70, MAPDEF_PCIMASK),
707         MAPDEF("pcie1.0", 8, 0xe0, MAPDEF_PCIMASK),
708         MAPDEF("pcie1.1", 8, 0xd0, MAPDEF_PCIMASK),
709         MAPDEF("pcie1.2", 8, 0xb0, MAPDEF_PCIMASK),
710         MAPDEF("pcie1.3", 8, 0x70, MAPDEF_PCIMASK),
711         MAPDEF("pcie2.0", 4, 0xf0, MAPDEF_PCIMASK),
712         MAPDEF("pcie3.0", 8, 0xf0, MAPDEF_PCIMASK),
713         {},
714 };
715
716 static const struct mvebu_mbus_soc_data mv78xx0_mbus_data = {
717         .num_wins            = 14,
718         .num_remappable_wins = 8,
719         .win_cfg_offset      = mv78xx0_mbus_win_offset,
720         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
721         .show_cpu_target     = mvebu_sdram_debug_show_orion,
722         .map                 = mv78xx0_map,
723 };
724
725 /*
726  * The driver doesn't yet have a DT binding because the details of
727  * this DT binding still need to be sorted out. However, as a
728  * preparation, we already use of_device_id to match a SoC description
729  * string against the SoC specific details of this driver.
730  */
731 static const struct of_device_id of_mvebu_mbus_ids[] = {
732         { .compatible = "marvell,armada370-mbus",
733           .data = &armada_370_mbus_data, },
734         { .compatible = "marvell,armadaxp-mbus",
735           .data = &armada_xp_mbus_data, },
736         { .compatible = "marvell,kirkwood-mbus",
737           .data = &kirkwood_mbus_data, },
738         { .compatible = "marvell,dove-mbus",
739           .data = &dove_mbus_data, },
740         { .compatible = "marvell,orion5x-88f5281-mbus",
741           .data = &orion5x_4win_mbus_data, },
742         { .compatible = "marvell,orion5x-88f5182-mbus",
743           .data = &orion5x_2win_mbus_data, },
744         { .compatible = "marvell,orion5x-88f5181-mbus",
745           .data = &orion5x_2win_mbus_data, },
746         { .compatible = "marvell,orion5x-88f6183-mbus",
747           .data = &orion5x_4win_mbus_data, },
748         { .compatible = "marvell,mv78xx0-mbus",
749           .data = &mv78xx0_mbus_data, },
750         { },
751 };
752
753 /*
754  * Public API of the driver
755  */
756 int mvebu_mbus_add_window_remap_flags(const char *devname, phys_addr_t base,
757                                       size_t size, phys_addr_t remap,
758                                       unsigned int flags)
759 {
760         struct mvebu_mbus_state *s = &mbus_state;
761         u8 target, attr;
762         int i;
763
764         if (!s->soc->map)
765                 return -ENODEV;
766
767         for (i = 0; s->soc->map[i].name; i++)
768                 if (!strcmp(s->soc->map[i].name, devname))
769                         break;
770
771         if (!s->soc->map[i].name) {
772                 pr_err("mvebu-mbus: unknown device '%s'\n", devname);
773                 return -ENODEV;
774         }
775
776         target = s->soc->map[i].target;
777         attr   = s->soc->map[i].attr;
778
779         if (flags == MVEBU_MBUS_PCI_MEM)
780                 attr |= 0x8;
781         else if (flags == MVEBU_MBUS_PCI_WA)
782                 attr |= 0x28;
783
784         if (!mvebu_mbus_window_conflicts(s, base, size, target, attr)) {
785                 pr_err("mvebu-mbus: cannot add window '%s', conflicts with another window\n",
786                        devname);
787                 return -EINVAL;
788         }
789
790         return mvebu_mbus_alloc_window(s, base, size, remap, target, attr);
791
792 }
793
794 int mvebu_mbus_add_window(const char *devname, phys_addr_t base, size_t size)
795 {
796         return mvebu_mbus_add_window_remap_flags(devname, base, size,
797                                                  MVEBU_MBUS_NO_REMAP, 0);
798 }
799
800 int mvebu_mbus_del_window(phys_addr_t base, size_t size)
801 {
802         int win;
803
804         win = mvebu_mbus_find_window(&mbus_state, base, size);
805         if (win < 0)
806                 return win;
807
808         mvebu_mbus_disable_window(&mbus_state, win);
809         return 0;
810 }
811
812 static __init int mvebu_mbus_debugfs_init(void)
813 {
814         struct mvebu_mbus_state *s = &mbus_state;
815
816         /*
817          * If no base has been initialized, doesn't make sense to
818          * register the debugfs entries. We may be on a multiplatform
819          * kernel that isn't running a Marvell EBU SoC.
820          */
821         if (!s->mbuswins_base)
822                 return 0;
823
824         s->debugfs_root = debugfs_create_dir("mvebu-mbus", NULL);
825         if (s->debugfs_root) {
826                 s->debugfs_sdram = debugfs_create_file("sdram", S_IRUGO,
827                                                        s->debugfs_root, NULL,
828                                                        &mvebu_sdram_debug_fops);
829                 s->debugfs_devs = debugfs_create_file("devices", S_IRUGO,
830                                                       s->debugfs_root, NULL,
831                                                       &mvebu_devs_debug_fops);
832         }
833
834         return 0;
835 }
836 fs_initcall(mvebu_mbus_debugfs_init);
837
838 int __init mvebu_mbus_init(const char *soc, phys_addr_t mbuswins_phys_base,
839                            size_t mbuswins_size,
840                            phys_addr_t sdramwins_phys_base,
841                            size_t sdramwins_size)
842 {
843         struct mvebu_mbus_state *mbus = &mbus_state;
844         const struct of_device_id *of_id;
845         int win;
846
847         for (of_id = of_mvebu_mbus_ids; of_id->compatible; of_id++)
848                 if (!strcmp(of_id->compatible, soc))
849                         break;
850
851         if (!of_id->compatible) {
852                 pr_err("mvebu-mbus: could not find a matching SoC family\n");
853                 return -ENODEV;
854         }
855
856         mbus->soc = of_id->data;
857
858         mbus->mbuswins_base = ioremap(mbuswins_phys_base, mbuswins_size);
859         if (!mbus->mbuswins_base)
860                 return -ENOMEM;
861
862         mbus->sdramwins_base = ioremap(sdramwins_phys_base, sdramwins_size);
863         if (!mbus->sdramwins_base) {
864                 iounmap(mbus_state.mbuswins_base);
865                 return -ENOMEM;
866         }
867
868         if (of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric"))
869                 mbus->hw_io_coherency = 1;
870
871         for (win = 0; win < mbus->soc->num_wins; win++)
872                 mvebu_mbus_disable_window(mbus, win);
873
874         mbus->soc->setup_cpu_target(mbus);
875
876         return 0;
877 }