]> Pileus Git - ~andy/linux/blob - drivers/bus/mvebu-mbus.c
bus: mvebu-mbus: Remove the no longer used name-based API
[~andy/linux] / 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53
54 #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
100 struct mvebu_mbus_mapping {
101         const char *name;
102         u8 target;
103         u8 attr;
104         u8 attrmask;
105 };
106
107 /*
108  * Masks used for the 'attrmask' field of mvebu_mbus_mapping. They
109  * allow to get the real attribute value, discarding the special bits
110  * used to select a PCI MEM region or a PCI WA region. This allows the
111  * debugfs code to reverse-match the name of a device from its
112  * target/attr values.
113  *
114  * For all devices except PCI, all bits of 'attr' must be
115  * considered. For most SoCs, only bit 3 should be ignored (it allows
116  * to select between PCI MEM and PCI I/O). On Orion5x however, there
117  * is the special bit 5 to select a PCI WA region.
118  */
119 #define MAPDEF_NOMASK       0xff
120 #define MAPDEF_PCIMASK      0xf7
121 #define MAPDEF_ORIONPCIMASK 0xd7
122
123 /* Macro used to define one mvebu_mbus_mapping entry */
124 #define MAPDEF(__n, __t, __a, __m) \
125         { .name = __n, .target = __t, .attr = __a, .attrmask = __m }
126
127 struct mvebu_mbus_state;
128
129 struct mvebu_mbus_soc_data {
130         unsigned int num_wins;
131         unsigned int num_remappable_wins;
132         unsigned int (*win_cfg_offset)(const int win);
133         void (*setup_cpu_target)(struct mvebu_mbus_state *s);
134         int (*show_cpu_target)(struct mvebu_mbus_state *s,
135                                struct seq_file *seq, void *v);
136         const struct mvebu_mbus_mapping *map;
137 };
138
139 struct mvebu_mbus_state {
140         void __iomem *mbuswins_base;
141         void __iomem *sdramwins_base;
142         struct dentry *debugfs_root;
143         struct dentry *debugfs_sdram;
144         struct dentry *debugfs_devs;
145         struct resource pcie_mem_aperture;
146         struct resource pcie_io_aperture;
147         const struct mvebu_mbus_soc_data *soc;
148         int hw_io_coherency;
149 };
150
151 static struct mvebu_mbus_state mbus_state;
152
153 static struct mbus_dram_target_info mvebu_mbus_dram_info;
154 const struct mbus_dram_target_info *mv_mbus_dram_info(void)
155 {
156         return &mvebu_mbus_dram_info;
157 }
158 EXPORT_SYMBOL_GPL(mv_mbus_dram_info);
159
160 /*
161  * Functions to manipulate the address decoding windows
162  */
163
164 static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
165                                    int win, int *enabled, u64 *base,
166                                    u32 *size, u8 *target, u8 *attr,
167                                    u64 *remap)
168 {
169         void __iomem *addr = mbus->mbuswins_base +
170                 mbus->soc->win_cfg_offset(win);
171         u32 basereg = readl(addr + WIN_BASE_OFF);
172         u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
173
174         if (!(ctrlreg & WIN_CTRL_ENABLE)) {
175                 *enabled = 0;
176                 return;
177         }
178
179         *enabled = 1;
180         *base = ((u64)basereg & WIN_BASE_HIGH) << 32;
181         *base |= (basereg & WIN_BASE_LOW);
182         *size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
183
184         if (target)
185                 *target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
186
187         if (attr)
188                 *attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
189
190         if (remap) {
191                 if (win < mbus->soc->num_remappable_wins) {
192                         u32 remap_low = readl(addr + WIN_REMAP_LO_OFF);
193                         u32 remap_hi  = readl(addr + WIN_REMAP_HI_OFF);
194                         *remap = ((u64)remap_hi << 32) | remap_low;
195                 } else
196                         *remap = 0;
197         }
198 }
199
200 static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
201                                       int win)
202 {
203         void __iomem *addr;
204
205         addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
206
207         writel(0, addr + WIN_BASE_OFF);
208         writel(0, addr + WIN_CTRL_OFF);
209         if (win < mbus->soc->num_remappable_wins) {
210                 writel(0, addr + WIN_REMAP_LO_OFF);
211                 writel(0, addr + WIN_REMAP_HI_OFF);
212         }
213 }
214
215 /* Checks whether the given window number is available */
216 static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
217                                      const int win)
218 {
219         void __iomem *addr = mbus->mbuswins_base +
220                 mbus->soc->win_cfg_offset(win);
221         u32 ctrl = readl(addr + WIN_CTRL_OFF);
222         return !(ctrl & WIN_CTRL_ENABLE);
223 }
224
225 /*
226  * Checks whether the given (base, base+size) area doesn't overlap an
227  * existing region
228  */
229 static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
230                                        phys_addr_t base, size_t size,
231                                        u8 target, u8 attr)
232 {
233         u64 end = (u64)base + size;
234         int win;
235
236         for (win = 0; win < mbus->soc->num_wins; win++) {
237                 u64 wbase, wend;
238                 u32 wsize;
239                 u8 wtarget, wattr;
240                 int enabled;
241
242                 mvebu_mbus_read_window(mbus, win,
243                                        &enabled, &wbase, &wsize,
244                                        &wtarget, &wattr, NULL);
245
246                 if (!enabled)
247                         continue;
248
249                 wend = wbase + wsize;
250
251                 /*
252                  * Check if the current window overlaps with the
253                  * proposed physical range
254                  */
255                 if ((u64)base < wend && end > wbase)
256                         return 0;
257
258                 /*
259                  * Check if target/attribute conflicts
260                  */
261                 if (target == wtarget && attr == wattr)
262                         return 0;
263         }
264
265         return 1;
266 }
267
268 static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
269                                   phys_addr_t base, size_t size)
270 {
271         int win;
272
273         for (win = 0; win < mbus->soc->num_wins; win++) {
274                 u64 wbase;
275                 u32 wsize;
276                 int enabled;
277
278                 mvebu_mbus_read_window(mbus, win,
279                                        &enabled, &wbase, &wsize,
280                                        NULL, NULL, NULL);
281
282                 if (!enabled)
283                         continue;
284
285                 if (base == wbase && size == wsize)
286                         return win;
287         }
288
289         return -ENODEV;
290 }
291
292 static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
293                                    int win, phys_addr_t base, size_t size,
294                                    phys_addr_t remap, u8 target,
295                                    u8 attr)
296 {
297         void __iomem *addr = mbus->mbuswins_base +
298                 mbus->soc->win_cfg_offset(win);
299         u32 ctrl, remap_addr;
300
301         ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
302                 (attr << WIN_CTRL_ATTR_SHIFT)    |
303                 (target << WIN_CTRL_TGT_SHIFT)   |
304                 WIN_CTRL_ENABLE;
305
306         writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
307         writel(ctrl, addr + WIN_CTRL_OFF);
308         if (win < mbus->soc->num_remappable_wins) {
309                 if (remap == MVEBU_MBUS_NO_REMAP)
310                         remap_addr = base;
311                 else
312                         remap_addr = remap;
313                 writel(remap_addr & WIN_REMAP_LOW, addr + WIN_REMAP_LO_OFF);
314                 writel(0, addr + WIN_REMAP_HI_OFF);
315         }
316
317         return 0;
318 }
319
320 static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
321                                    phys_addr_t base, size_t size,
322                                    phys_addr_t remap, u8 target,
323                                    u8 attr)
324 {
325         int win;
326
327         if (remap == MVEBU_MBUS_NO_REMAP) {
328                 for (win = mbus->soc->num_remappable_wins;
329                      win < mbus->soc->num_wins; win++)
330                         if (mvebu_mbus_window_is_free(mbus, win))
331                                 return mvebu_mbus_setup_window(mbus, win, base,
332                                                                size, remap,
333                                                                target, attr);
334         }
335
336
337         for (win = 0; win < mbus->soc->num_wins; win++)
338                 if (mvebu_mbus_window_is_free(mbus, win))
339                         return mvebu_mbus_setup_window(mbus, win, base, size,
340                                                        remap, target, attr);
341
342         return -ENOMEM;
343 }
344
345 /*
346  * Debugfs debugging
347  */
348
349 /* Common function used for Dove, Kirkwood, Armada 370/XP and Orion 5x */
350 static int mvebu_sdram_debug_show_orion(struct mvebu_mbus_state *mbus,
351                                         struct seq_file *seq, void *v)
352 {
353         int i;
354
355         for (i = 0; i < 4; i++) {
356                 u32 basereg = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
357                 u32 sizereg = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
358                 u64 base;
359                 u32 size;
360
361                 if (!(sizereg & DDR_SIZE_ENABLED)) {
362                         seq_printf(seq, "[%d] disabled\n", i);
363                         continue;
364                 }
365
366                 base = ((u64)basereg & DDR_BASE_CS_HIGH_MASK) << 32;
367                 base |= basereg & DDR_BASE_CS_LOW_MASK;
368                 size = (sizereg | ~DDR_SIZE_MASK);
369
370                 seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
371                            i, (unsigned long long)base,
372                            (unsigned long long)base + size + 1,
373                            (sizereg & DDR_SIZE_CS_MASK) >> DDR_SIZE_CS_SHIFT);
374         }
375
376         return 0;
377 }
378
379 /* Special function for Dove */
380 static int mvebu_sdram_debug_show_dove(struct mvebu_mbus_state *mbus,
381                                        struct seq_file *seq, void *v)
382 {
383         int i;
384
385         for (i = 0; i < 2; i++) {
386                 u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
387                 u64 base;
388                 u32 size;
389
390                 if (!(map & 1)) {
391                         seq_printf(seq, "[%d] disabled\n", i);
392                         continue;
393                 }
394
395                 base = map & 0xff800000;
396                 size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
397
398                 seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
399                            i, (unsigned long long)base,
400                            (unsigned long long)base + size, i);
401         }
402
403         return 0;
404 }
405
406 static int mvebu_sdram_debug_show(struct seq_file *seq, void *v)
407 {
408         struct mvebu_mbus_state *mbus = &mbus_state;
409         return mbus->soc->show_cpu_target(mbus, seq, v);
410 }
411
412 static int mvebu_sdram_debug_open(struct inode *inode, struct file *file)
413 {
414         return single_open(file, mvebu_sdram_debug_show, inode->i_private);
415 }
416
417 static const struct file_operations mvebu_sdram_debug_fops = {
418         .open = mvebu_sdram_debug_open,
419         .read = seq_read,
420         .llseek = seq_lseek,
421         .release = single_release,
422 };
423
424 static int mvebu_devs_debug_show(struct seq_file *seq, void *v)
425 {
426         struct mvebu_mbus_state *mbus = &mbus_state;
427         int win;
428
429         for (win = 0; win < mbus->soc->num_wins; win++) {
430                 u64 wbase, wremap;
431                 u32 wsize;
432                 u8 wtarget, wattr;
433                 int enabled, i;
434                 const char *name;
435
436                 mvebu_mbus_read_window(mbus, win,
437                                        &enabled, &wbase, &wsize,
438                                        &wtarget, &wattr, &wremap);
439
440                 if (!enabled) {
441                         seq_printf(seq, "[%02d] disabled\n", win);
442                         continue;
443                 }
444
445
446                 for (i = 0; mbus->soc->map[i].name; i++)
447                         if (mbus->soc->map[i].target == wtarget &&
448                             mbus->soc->map[i].attr ==
449                             (wattr & mbus->soc->map[i].attrmask))
450                                 break;
451
452                 name = mbus->soc->map[i].name ?: "unknown";
453
454                 seq_printf(seq, "[%02d] %016llx - %016llx : %s",
455                            win, (unsigned long long)wbase,
456                            (unsigned long long)(wbase + wsize), name);
457
458                 if (win < mbus->soc->num_remappable_wins) {
459                         seq_printf(seq, " (remap %016llx)\n",
460                                    (unsigned long long)wremap);
461                 } else
462                         seq_printf(seq, "\n");
463         }
464
465         return 0;
466 }
467
468 static int mvebu_devs_debug_open(struct inode *inode, struct file *file)
469 {
470         return single_open(file, mvebu_devs_debug_show, inode->i_private);
471 }
472
473 static const struct file_operations mvebu_devs_debug_fops = {
474         .open = mvebu_devs_debug_open,
475         .read = seq_read,
476         .llseek = seq_lseek,
477         .release = single_release,
478 };
479
480 /*
481  * SoC-specific functions and definitions
482  */
483
484 static unsigned int orion_mbus_win_offset(int win)
485 {
486         return win << 4;
487 }
488
489 static unsigned int armada_370_xp_mbus_win_offset(int win)
490 {
491         /* The register layout is a bit annoying and the below code
492          * tries to cope with it.
493          * - At offset 0x0, there are the registers for the first 8
494          *   windows, with 4 registers of 32 bits per window (ctrl,
495          *   base, remap low, remap high)
496          * - Then at offset 0x80, there is a hole of 0x10 bytes for
497          *   the internal registers base address and internal units
498          *   sync barrier register.
499          * - Then at offset 0x90, there the registers for 12
500          *   windows, with only 2 registers of 32 bits per window
501          *   (ctrl, base).
502          */
503         if (win < 8)
504                 return win << 4;
505         else
506                 return 0x90 + ((win - 8) << 3);
507 }
508
509 static unsigned int mv78xx0_mbus_win_offset(int win)
510 {
511         if (win < 8)
512                 return win << 4;
513         else
514                 return 0x900 + ((win - 8) << 4);
515 }
516
517 static void __init
518 mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
519 {
520         int i;
521         int cs;
522
523         mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
524
525         for (i = 0, cs = 0; i < 4; i++) {
526                 u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
527                 u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
528
529                 /*
530                  * We only take care of entries for which the chip
531                  * select is enabled, and that don't have high base
532                  * address bits set (devices can only access the first
533                  * 32 bits of the memory).
534                  */
535                 if ((size & DDR_SIZE_ENABLED) &&
536                     !(base & DDR_BASE_CS_HIGH_MASK)) {
537                         struct mbus_dram_window *w;
538
539                         w = &mvebu_mbus_dram_info.cs[cs++];
540                         w->cs_index = i;
541                         w->mbus_attr = 0xf & ~(1 << i);
542                         if (mbus->hw_io_coherency)
543                                 w->mbus_attr |= ATTR_HW_COHERENCY;
544                         w->base = base & DDR_BASE_CS_LOW_MASK;
545                         w->size = (size | ~DDR_SIZE_MASK) + 1;
546                 }
547         }
548         mvebu_mbus_dram_info.num_cs = cs;
549 }
550
551 static void __init
552 mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state *mbus)
553 {
554         int i;
555         int cs;
556
557         mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
558
559         for (i = 0, cs = 0; i < 2; i++) {
560                 u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
561
562                 /*
563                  * Chip select enabled?
564                  */
565                 if (map & 1) {
566                         struct mbus_dram_window *w;
567
568                         w = &mvebu_mbus_dram_info.cs[cs++];
569                         w->cs_index = i;
570                         w->mbus_attr = 0; /* CS address decoding done inside */
571                                           /* the DDR controller, no need to  */
572                                           /* provide attributes */
573                         w->base = map & 0xff800000;
574                         w->size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
575                 }
576         }
577
578         mvebu_mbus_dram_info.num_cs = cs;
579 }
580
581 static const struct mvebu_mbus_mapping armada_370_map[] = {
582         MAPDEF("bootrom",     1, 0xe0, MAPDEF_NOMASK),
583         MAPDEF("devbus-boot", 1, 0x2f, MAPDEF_NOMASK),
584         MAPDEF("devbus-cs0",  1, 0x3e, MAPDEF_NOMASK),
585         MAPDEF("devbus-cs1",  1, 0x3d, MAPDEF_NOMASK),
586         MAPDEF("devbus-cs2",  1, 0x3b, MAPDEF_NOMASK),
587         MAPDEF("devbus-cs3",  1, 0x37, MAPDEF_NOMASK),
588         MAPDEF("pcie0.0",     4, 0xe0, MAPDEF_PCIMASK),
589         MAPDEF("pcie1.0",     8, 0xe0, MAPDEF_PCIMASK),
590         {},
591 };
592
593 static const struct mvebu_mbus_soc_data armada_370_mbus_data = {
594         .num_wins            = 20,
595         .num_remappable_wins = 8,
596         .win_cfg_offset      = armada_370_xp_mbus_win_offset,
597         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
598         .show_cpu_target     = mvebu_sdram_debug_show_orion,
599         .map                 = armada_370_map,
600 };
601
602 static const struct mvebu_mbus_mapping armada_xp_map[] = {
603         MAPDEF("bootrom",     1, 0x1d, MAPDEF_NOMASK),
604         MAPDEF("devbus-boot", 1, 0x2f, MAPDEF_NOMASK),
605         MAPDEF("devbus-cs0",  1, 0x3e, MAPDEF_NOMASK),
606         MAPDEF("devbus-cs1",  1, 0x3d, MAPDEF_NOMASK),
607         MAPDEF("devbus-cs2",  1, 0x3b, MAPDEF_NOMASK),
608         MAPDEF("devbus-cs3",  1, 0x37, MAPDEF_NOMASK),
609         MAPDEF("pcie0.0",     4, 0xe0, MAPDEF_PCIMASK),
610         MAPDEF("pcie0.1",     4, 0xd0, MAPDEF_PCIMASK),
611         MAPDEF("pcie0.2",     4, 0xb0, MAPDEF_PCIMASK),
612         MAPDEF("pcie0.3",     4, 0x70, MAPDEF_PCIMASK),
613         MAPDEF("pcie1.0",     8, 0xe0, MAPDEF_PCIMASK),
614         MAPDEF("pcie1.1",     8, 0xd0, MAPDEF_PCIMASK),
615         MAPDEF("pcie1.2",     8, 0xb0, MAPDEF_PCIMASK),
616         MAPDEF("pcie1.3",     8, 0x70, MAPDEF_PCIMASK),
617         MAPDEF("pcie2.0",     4, 0xf0, MAPDEF_PCIMASK),
618         MAPDEF("pcie3.0",     8, 0xf0, MAPDEF_PCIMASK),
619         {},
620 };
621
622 static const struct mvebu_mbus_soc_data armada_xp_mbus_data = {
623         .num_wins            = 20,
624         .num_remappable_wins = 8,
625         .win_cfg_offset      = armada_370_xp_mbus_win_offset,
626         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
627         .show_cpu_target     = mvebu_sdram_debug_show_orion,
628         .map                 = armada_xp_map,
629 };
630
631 static const struct mvebu_mbus_mapping kirkwood_map[] = {
632         MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
633         MAPDEF("pcie1.0", 4, 0xd0, MAPDEF_PCIMASK),
634         MAPDEF("sram",    3, 0x01, MAPDEF_NOMASK),
635         MAPDEF("nand",    1, 0x2f, MAPDEF_NOMASK),
636         {},
637 };
638
639 static const struct mvebu_mbus_soc_data kirkwood_mbus_data = {
640         .num_wins            = 8,
641         .num_remappable_wins = 4,
642         .win_cfg_offset      = orion_mbus_win_offset,
643         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
644         .show_cpu_target     = mvebu_sdram_debug_show_orion,
645         .map                 = kirkwood_map,
646 };
647
648 static const struct mvebu_mbus_mapping dove_map[] = {
649         MAPDEF("pcie0.0",    0x4, 0xe0, MAPDEF_PCIMASK),
650         MAPDEF("pcie1.0",    0x8, 0xe0, MAPDEF_PCIMASK),
651         MAPDEF("cesa",       0x3, 0x01, MAPDEF_NOMASK),
652         MAPDEF("bootrom",    0x1, 0xfd, MAPDEF_NOMASK),
653         MAPDEF("scratchpad", 0xd, 0x0, MAPDEF_NOMASK),
654         {},
655 };
656
657 static const struct mvebu_mbus_soc_data dove_mbus_data = {
658         .num_wins            = 8,
659         .num_remappable_wins = 4,
660         .win_cfg_offset      = orion_mbus_win_offset,
661         .setup_cpu_target    = mvebu_mbus_dove_setup_cpu_target,
662         .show_cpu_target     = mvebu_sdram_debug_show_dove,
663         .map                 = dove_map,
664 };
665
666 static const struct mvebu_mbus_mapping orion5x_map[] = {
667         MAPDEF("pcie0.0",     4, 0x51, MAPDEF_ORIONPCIMASK),
668         MAPDEF("pci0.0",      3, 0x51, MAPDEF_ORIONPCIMASK),
669         MAPDEF("devbus-boot", 1, 0x0f, MAPDEF_NOMASK),
670         MAPDEF("devbus-cs0",  1, 0x1e, MAPDEF_NOMASK),
671         MAPDEF("devbus-cs1",  1, 0x1d, MAPDEF_NOMASK),
672         MAPDEF("devbus-cs2",  1, 0x1b, MAPDEF_NOMASK),
673         MAPDEF("sram",        0, 0x00, MAPDEF_NOMASK),
674         {},
675 };
676
677 /*
678  * Some variants of Orion5x have 4 remappable windows, some other have
679  * only two of them.
680  */
681 static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data = {
682         .num_wins            = 8,
683         .num_remappable_wins = 4,
684         .win_cfg_offset      = orion_mbus_win_offset,
685         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
686         .show_cpu_target     = mvebu_sdram_debug_show_orion,
687         .map                 = orion5x_map,
688 };
689
690 static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data = {
691         .num_wins            = 8,
692         .num_remappable_wins = 2,
693         .win_cfg_offset      = orion_mbus_win_offset,
694         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
695         .show_cpu_target     = mvebu_sdram_debug_show_orion,
696         .map                 = orion5x_map,
697 };
698
699 static const struct mvebu_mbus_mapping mv78xx0_map[] = {
700         MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
701         MAPDEF("pcie0.1", 4, 0xd0, MAPDEF_PCIMASK),
702         MAPDEF("pcie0.2", 4, 0xb0, MAPDEF_PCIMASK),
703         MAPDEF("pcie0.3", 4, 0x70, MAPDEF_PCIMASK),
704         MAPDEF("pcie1.0", 8, 0xe0, MAPDEF_PCIMASK),
705         MAPDEF("pcie1.1", 8, 0xd0, MAPDEF_PCIMASK),
706         MAPDEF("pcie1.2", 8, 0xb0, MAPDEF_PCIMASK),
707         MAPDEF("pcie1.3", 8, 0x70, MAPDEF_PCIMASK),
708         MAPDEF("pcie2.0", 4, 0xf0, MAPDEF_PCIMASK),
709         MAPDEF("pcie3.0", 8, 0xf0, MAPDEF_PCIMASK),
710         {},
711 };
712
713 static const struct mvebu_mbus_soc_data mv78xx0_mbus_data = {
714         .num_wins            = 14,
715         .num_remappable_wins = 8,
716         .win_cfg_offset      = mv78xx0_mbus_win_offset,
717         .setup_cpu_target    = mvebu_mbus_default_setup_cpu_target,
718         .show_cpu_target     = mvebu_sdram_debug_show_orion,
719         .map                 = mv78xx0_map,
720 };
721
722 /*
723  * The driver doesn't yet have a DT binding because the details of
724  * this DT binding still need to be sorted out. However, as a
725  * preparation, we already use of_device_id to match a SoC description
726  * string against the SoC specific details of this driver.
727  */
728 static const struct of_device_id of_mvebu_mbus_ids[] = {
729         { .compatible = "marvell,armada370-mbus",
730           .data = &armada_370_mbus_data, },
731         { .compatible = "marvell,armadaxp-mbus",
732           .data = &armada_xp_mbus_data, },
733         { .compatible = "marvell,kirkwood-mbus",
734           .data = &kirkwood_mbus_data, },
735         { .compatible = "marvell,dove-mbus",
736           .data = &dove_mbus_data, },
737         { .compatible = "marvell,orion5x-88f5281-mbus",
738           .data = &orion5x_4win_mbus_data, },
739         { .compatible = "marvell,orion5x-88f5182-mbus",
740           .data = &orion5x_2win_mbus_data, },
741         { .compatible = "marvell,orion5x-88f5181-mbus",
742           .data = &orion5x_2win_mbus_data, },
743         { .compatible = "marvell,orion5x-88f6183-mbus",
744           .data = &orion5x_4win_mbus_data, },
745         { .compatible = "marvell,mv78xx0-mbus",
746           .data = &mv78xx0_mbus_data, },
747         { },
748 };
749
750 /*
751  * Public API of the driver
752  */
753 int mvebu_mbus_add_window_remap_by_id(unsigned int target,
754                                       unsigned int attribute,
755                                       phys_addr_t base, size_t size,
756                                       phys_addr_t remap)
757 {
758         struct mvebu_mbus_state *s = &mbus_state;
759
760         if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
761                 pr_err("cannot add window '%x:%x', conflicts with another window\n",
762                        target, attribute);
763                 return -EINVAL;
764         }
765
766         return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
767 }
768
769 int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
770                                 phys_addr_t base, size_t size)
771 {
772         return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
773                                                  size, MVEBU_MBUS_NO_REMAP);
774 }
775
776 int mvebu_mbus_del_window(phys_addr_t base, size_t size)
777 {
778         int win;
779
780         win = mvebu_mbus_find_window(&mbus_state, base, size);
781         if (win < 0)
782                 return win;
783
784         mvebu_mbus_disable_window(&mbus_state, win);
785         return 0;
786 }
787
788 void mvebu_mbus_get_pcie_mem_aperture(struct resource *res)
789 {
790         if (!res)
791                 return;
792         *res = mbus_state.pcie_mem_aperture;
793 }
794
795 void mvebu_mbus_get_pcie_io_aperture(struct resource *res)
796 {
797         if (!res)
798                 return;
799         *res = mbus_state.pcie_io_aperture;
800 }
801
802 static __init int mvebu_mbus_debugfs_init(void)
803 {
804         struct mvebu_mbus_state *s = &mbus_state;
805
806         /*
807          * If no base has been initialized, doesn't make sense to
808          * register the debugfs entries. We may be on a multiplatform
809          * kernel that isn't running a Marvell EBU SoC.
810          */
811         if (!s->mbuswins_base)
812                 return 0;
813
814         s->debugfs_root = debugfs_create_dir("mvebu-mbus", NULL);
815         if (s->debugfs_root) {
816                 s->debugfs_sdram = debugfs_create_file("sdram", S_IRUGO,
817                                                        s->debugfs_root, NULL,
818                                                        &mvebu_sdram_debug_fops);
819                 s->debugfs_devs = debugfs_create_file("devices", S_IRUGO,
820                                                       s->debugfs_root, NULL,
821                                                       &mvebu_devs_debug_fops);
822         }
823
824         return 0;
825 }
826 fs_initcall(mvebu_mbus_debugfs_init);
827
828 static int __init mvebu_mbus_common_init(struct mvebu_mbus_state *mbus,
829                                          phys_addr_t mbuswins_phys_base,
830                                          size_t mbuswins_size,
831                                          phys_addr_t sdramwins_phys_base,
832                                          size_t sdramwins_size)
833 {
834         int win;
835
836         mbus->mbuswins_base = ioremap(mbuswins_phys_base, mbuswins_size);
837         if (!mbus->mbuswins_base)
838                 return -ENOMEM;
839
840         mbus->sdramwins_base = ioremap(sdramwins_phys_base, sdramwins_size);
841         if (!mbus->sdramwins_base) {
842                 iounmap(mbus_state.mbuswins_base);
843                 return -ENOMEM;
844         }
845
846         if (of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric"))
847                 mbus->hw_io_coherency = 1;
848
849         for (win = 0; win < mbus->soc->num_wins; win++)
850                 mvebu_mbus_disable_window(mbus, win);
851
852         mbus->soc->setup_cpu_target(mbus);
853
854         return 0;
855 }
856
857 int __init mvebu_mbus_init(const char *soc, phys_addr_t mbuswins_phys_base,
858                            size_t mbuswins_size,
859                            phys_addr_t sdramwins_phys_base,
860                            size_t sdramwins_size)
861 {
862         const struct of_device_id *of_id;
863
864         for (of_id = of_mvebu_mbus_ids; of_id->compatible; of_id++)
865                 if (!strcmp(of_id->compatible, soc))
866                         break;
867
868         if (!of_id->compatible) {
869                 pr_err("could not find a matching SoC family\n");
870                 return -ENODEV;
871         }
872
873         mbus_state.soc = of_id->data;
874
875         return mvebu_mbus_common_init(&mbus_state,
876                         mbuswins_phys_base,
877                         mbuswins_size,
878                         sdramwins_phys_base,
879                         sdramwins_size);
880 }
881
882 #ifdef CONFIG_OF
883 /*
884  * The window IDs in the ranges DT property have the following format:
885  *  - bits 28 to 31: MBus custom field
886  *  - bits 24 to 27: window target ID
887  *  - bits 16 to 23: window attribute ID
888  *  - bits  0 to 15: unused
889  */
890 #define CUSTOM(id) (((id) & 0xF0000000) >> 24)
891 #define TARGET(id) (((id) & 0x0F000000) >> 24)
892 #define ATTR(id)   (((id) & 0x00FF0000) >> 16)
893
894 static int __init mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
895                                     u32 base, u32 size,
896                                     u8 target, u8 attr)
897 {
898         const struct mvebu_mbus_mapping *map = mbus->soc->map;
899         const char *name;
900         int i;
901
902         /* Search for a suitable window in the existing mappings */
903         for (i = 0; map[i].name; i++)
904                 if (map[i].target == target &&
905                     map[i].attr == (attr & map[i].attrmask))
906                         break;
907
908         name = map[i].name;
909         if (!name) {
910                 pr_err("window 0x%x:0x%x is unknown, skipping\n",
911                        target, attr);
912                 return -EINVAL;
913         }
914
915         if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
916                 pr_err("cannot add window '%s', conflicts with another window\n",
917                        name);
918                 return -EBUSY;
919         }
920
921         if (mvebu_mbus_alloc_window(mbus, base, size, MVEBU_MBUS_NO_REMAP,
922                                     target, attr)) {
923                 pr_err("cannot add window '%s', too many windows\n",
924                        name);
925                 return -ENOMEM;
926         }
927         return 0;
928 }
929
930 static int __init
931 mbus_parse_ranges(struct device_node *node,
932                   int *addr_cells, int *c_addr_cells, int *c_size_cells,
933                   int *cell_count, const __be32 **ranges_start,
934                   const __be32 **ranges_end)
935 {
936         const __be32 *prop;
937         int ranges_len, tuple_len;
938
939         /* Allow a node with no 'ranges' property */
940         *ranges_start = of_get_property(node, "ranges", &ranges_len);
941         if (*ranges_start == NULL) {
942                 *addr_cells = *c_addr_cells = *c_size_cells = *cell_count = 0;
943                 *ranges_start = *ranges_end = NULL;
944                 return 0;
945         }
946         *ranges_end = *ranges_start + ranges_len / sizeof(__be32);
947
948         *addr_cells = of_n_addr_cells(node);
949
950         prop = of_get_property(node, "#address-cells", NULL);
951         *c_addr_cells = be32_to_cpup(prop);
952
953         prop = of_get_property(node, "#size-cells", NULL);
954         *c_size_cells = be32_to_cpup(prop);
955
956         *cell_count = *addr_cells + *c_addr_cells + *c_size_cells;
957         tuple_len = (*cell_count) * sizeof(__be32);
958
959         if (ranges_len % tuple_len) {
960                 pr_warn("malformed ranges entry '%s'\n", node->name);
961                 return -EINVAL;
962         }
963         return 0;
964 }
965
966 static int __init mbus_dt_setup(struct mvebu_mbus_state *mbus,
967                                 struct device_node *np)
968 {
969         int addr_cells, c_addr_cells, c_size_cells;
970         int i, ret, cell_count;
971         const __be32 *r, *ranges_start, *ranges_end;
972
973         ret = mbus_parse_ranges(np, &addr_cells, &c_addr_cells,
974                                 &c_size_cells, &cell_count,
975                                 &ranges_start, &ranges_end);
976         if (ret < 0)
977                 return ret;
978
979         for (i = 0, r = ranges_start; r < ranges_end; r += cell_count, i++) {
980                 u32 windowid, base, size;
981                 u8 target, attr;
982
983                 /*
984                  * An entry with a non-zero custom field do not
985                  * correspond to a static window, so skip it.
986                  */
987                 windowid = of_read_number(r, 1);
988                 if (CUSTOM(windowid))
989                         continue;
990
991                 target = TARGET(windowid);
992                 attr = ATTR(windowid);
993
994                 base = of_read_number(r + c_addr_cells, addr_cells);
995                 size = of_read_number(r + c_addr_cells + addr_cells,
996                                       c_size_cells);
997                 ret = mbus_dt_setup_win(mbus, base, size, target, attr);
998                 if (ret < 0)
999                         return ret;
1000         }
1001         return 0;
1002 }
1003
1004 static void __init mvebu_mbus_get_pcie_resources(struct device_node *np,
1005                                                  struct resource *mem,
1006                                                  struct resource *io)
1007 {
1008         u32 reg[2];
1009         int ret;
1010
1011         /*
1012          * These are optional, so we clear them and they'll
1013          * be zero if they are missing from the DT.
1014          */
1015         memset(mem, 0, sizeof(struct resource));
1016         memset(io, 0, sizeof(struct resource));
1017
1018         ret = of_property_read_u32_array(np, "pcie-mem-aperture", reg, ARRAY_SIZE(reg));
1019         if (!ret) {
1020                 mem->start = reg[0];
1021                 mem->end = mem->start + reg[1];
1022                 mem->flags = IORESOURCE_MEM;
1023         }
1024
1025         ret = of_property_read_u32_array(np, "pcie-io-aperture", reg, ARRAY_SIZE(reg));
1026         if (!ret) {
1027                 io->start = reg[0];
1028                 io->end = io->start + reg[1];
1029                 io->flags = IORESOURCE_IO;
1030         }
1031 }
1032
1033 int __init mvebu_mbus_dt_init(void)
1034 {
1035         struct resource mbuswins_res, sdramwins_res;
1036         struct device_node *np, *controller;
1037         const struct of_device_id *of_id;
1038         const __be32 *prop;
1039         int ret;
1040
1041         np = of_find_matching_node(NULL, of_mvebu_mbus_ids);
1042         if (!np) {
1043                 pr_err("could not find a matching SoC family\n");
1044                 return -ENODEV;
1045         }
1046
1047         of_id = of_match_node(of_mvebu_mbus_ids, np);
1048         mbus_state.soc = of_id->data;
1049
1050         prop = of_get_property(np, "controller", NULL);
1051         if (!prop) {
1052                 pr_err("required 'controller' property missing\n");
1053                 return -EINVAL;
1054         }
1055
1056         controller = of_find_node_by_phandle(be32_to_cpup(prop));
1057         if (!controller) {
1058                 pr_err("could not find an 'mbus-controller' node\n");
1059                 return -ENODEV;
1060         }
1061
1062         if (of_address_to_resource(controller, 0, &mbuswins_res)) {
1063                 pr_err("cannot get MBUS register address\n");
1064                 return -EINVAL;
1065         }
1066
1067         if (of_address_to_resource(controller, 1, &sdramwins_res)) {
1068                 pr_err("cannot get SDRAM register address\n");
1069                 return -EINVAL;
1070         }
1071
1072         /* Get optional pcie-{mem,io}-aperture properties */
1073         mvebu_mbus_get_pcie_resources(np, &mbus_state.pcie_mem_aperture,
1074                                           &mbus_state.pcie_io_aperture);
1075
1076         ret = mvebu_mbus_common_init(&mbus_state,
1077                                      mbuswins_res.start,
1078                                      resource_size(&mbuswins_res),
1079                                      sdramwins_res.start,
1080                                      resource_size(&sdramwins_res));
1081         if (ret)
1082                 return ret;
1083
1084         /* Setup statically declared windows in the DT */
1085         return mbus_dt_setup(&mbus_state, np);
1086 }
1087 #endif