Merge tag 'gvt-fixes-2018-11-26' of https://github.com/intel/gvt-linux into drm-intel...
[sfrench/cifs-2.6.git] / arch / arm / mach-versatile / versatile_dt.c
1 /*
2  * Versatile board support using the device tree
3  *
4  *  Copyright (C) 2010 Secret Lab Technologies Ltd.
5  *  Copyright (C) 2009 Jeremy Kerr <jeremy.kerr@canonical.com>
6  *  Copyright (C) 2004 ARM Limited
7  *  Copyright (C) 2000 Deep Blue Solutions Ltd
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <linux/init.h>
25 #include <linux/io.h>
26 #include <linux/of.h>
27 #include <linux/of_address.h>
28 #include <linux/of_irq.h>
29 #include <linux/of_platform.h>
30 #include <linux/slab.h>
31 #include <linux/amba/bus.h>
32 #include <linux/amba/clcd.h>
33 #include <linux/platform_data/video-clcd-versatile.h>
34 #include <linux/amba/mmci.h>
35 #include <asm/mach-types.h>
36 #include <asm/mach/arch.h>
37 #include <asm/mach/map.h>
38
39 /* macro to get at MMIO space when running virtually */
40 #define IO_ADDRESS(x)           (((x) & 0x0fffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
41 #define __io_address(n)         ((void __iomem __force *)IO_ADDRESS(n))
42
43 /*
44  * ------------------------------------------------------------------------
45  *  Versatile Registers
46  * ------------------------------------------------------------------------
47  */
48 #define VERSATILE_SYS_PCICTL_OFFSET           0x44
49 #define VERSATILE_SYS_MCI_OFFSET              0x48
50 #define VERSATILE_SYS_CLCD_OFFSET             0x50
51
52 /*
53  * VERSATILE peripheral addresses
54  */
55 #define VERSATILE_MMCI0_BASE           0x10005000       /* MMC interface */
56 #define VERSATILE_MMCI1_BASE           0x1000B000       /* MMC Interface */
57 #define VERSATILE_CLCD_BASE            0x10120000       /* CLCD */
58 #define VERSATILE_SCTL_BASE            0x101E0000       /* System controller */
59 #define VERSATILE_IB2_BASE             0x24000000       /* IB2 module */
60 #define VERSATILE_IB2_CTL_BASE          (VERSATILE_IB2_BASE + 0x03000000)
61
62 /*
63  * System controller bit assignment
64  */
65 #define VERSATILE_REFCLK        0
66 #define VERSATILE_TIMCLK        1
67
68 #define VERSATILE_TIMER1_EnSel  15
69 #define VERSATILE_TIMER2_EnSel  17
70 #define VERSATILE_TIMER3_EnSel  19
71 #define VERSATILE_TIMER4_EnSel  21
72
73 static void __iomem *versatile_sys_base;
74 static void __iomem *versatile_ib2_ctrl;
75
76 unsigned int mmc_status(struct device *dev)
77 {
78         struct amba_device *adev = container_of(dev, struct amba_device, dev);
79         u32 mask;
80
81         if (adev->res.start == VERSATILE_MMCI0_BASE)
82                 mask = 1;
83         else
84                 mask = 2;
85
86         return readl(versatile_sys_base + VERSATILE_SYS_MCI_OFFSET) & mask;
87 }
88
89 static struct mmci_platform_data mmc0_plat_data = {
90         .ocr_mask       = MMC_VDD_32_33|MMC_VDD_33_34,
91         .status         = mmc_status,
92 };
93
94 static struct mmci_platform_data mmc1_plat_data = {
95         .ocr_mask       = MMC_VDD_32_33|MMC_VDD_33_34,
96         .status         = mmc_status,
97 };
98
99 /*
100  * CLCD support.
101  */
102 #define SYS_CLCD_MODE_MASK      (3 << 0)
103 #define SYS_CLCD_MODE_888       (0 << 0)
104 #define SYS_CLCD_MODE_5551      (1 << 0)
105 #define SYS_CLCD_MODE_565_RLSB  (2 << 0)
106 #define SYS_CLCD_MODE_565_BLSB  (3 << 0)
107 #define SYS_CLCD_NLCDIOON       (1 << 2)
108 #define SYS_CLCD_VDDPOSSWITCH   (1 << 3)
109 #define SYS_CLCD_PWR3V5SWITCH   (1 << 4)
110 #define SYS_CLCD_ID_MASK        (0x1f << 8)
111 #define SYS_CLCD_ID_SANYO_3_8   (0x00 << 8)
112 #define SYS_CLCD_ID_UNKNOWN_8_4 (0x01 << 8)
113 #define SYS_CLCD_ID_EPSON_2_2   (0x02 << 8)
114 #define SYS_CLCD_ID_SANYO_2_5   (0x07 << 8)
115 #define SYS_CLCD_ID_VGA         (0x1f << 8)
116
117 static bool is_sanyo_2_5_lcd;
118
119 /*
120  * Disable all display connectors on the interface module.
121  */
122 static void versatile_clcd_disable(struct clcd_fb *fb)
123 {
124         void __iomem *sys_clcd = versatile_sys_base + VERSATILE_SYS_CLCD_OFFSET;
125         u32 val;
126
127         val = readl(sys_clcd);
128         val &= ~SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
129         writel(val, sys_clcd);
130
131         /*
132          * If the LCD is Sanyo 2x5 in on the IB2 board, turn the back-light off
133          */
134         if (of_machine_is_compatible("arm,versatile-ab") && is_sanyo_2_5_lcd) {
135                 unsigned long ctrl;
136
137                 ctrl = readl(versatile_ib2_ctrl);
138                 ctrl &= ~0x01;
139                 writel(ctrl, versatile_ib2_ctrl);
140         }
141 }
142
143 /*
144  * Enable the relevant connector on the interface module.
145  */
146 static void versatile_clcd_enable(struct clcd_fb *fb)
147 {
148         struct fb_var_screeninfo *var = &fb->fb.var;
149         void __iomem *sys_clcd = versatile_sys_base + VERSATILE_SYS_CLCD_OFFSET;
150         u32 val;
151
152         val = readl(sys_clcd);
153         val &= ~SYS_CLCD_MODE_MASK;
154
155         switch (var->green.length) {
156         case 5:
157                 val |= SYS_CLCD_MODE_5551;
158                 break;
159         case 6:
160                 if (var->red.offset == 0)
161                         val |= SYS_CLCD_MODE_565_RLSB;
162                 else
163                         val |= SYS_CLCD_MODE_565_BLSB;
164                 break;
165         case 8:
166                 val |= SYS_CLCD_MODE_888;
167                 break;
168         }
169
170         /*
171          * Set the MUX
172          */
173         writel(val, sys_clcd);
174
175         /*
176          * And now enable the PSUs
177          */
178         val |= SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
179         writel(val, sys_clcd);
180
181         /*
182          * If the LCD is Sanyo 2x5 in on the IB2 board, turn the back-light on
183          */
184         if (of_machine_is_compatible("arm,versatile-ab") && is_sanyo_2_5_lcd) {
185                 unsigned long ctrl;
186
187                 ctrl = readl(versatile_ib2_ctrl);
188                 ctrl |= 0x01;
189                 writel(ctrl, versatile_ib2_ctrl);
190         }
191 }
192
193 /*
194  * Detect which LCD panel is connected, and return the appropriate
195  * clcd_panel structure.  Note: we do not have any information on
196  * the required timings for the 8.4in panel, so we presently assume
197  * VGA timings.
198  */
199 static int versatile_clcd_setup(struct clcd_fb *fb)
200 {
201         void __iomem *sys_clcd = versatile_sys_base + VERSATILE_SYS_CLCD_OFFSET;
202         const char *panel_name;
203         u32 val;
204
205         is_sanyo_2_5_lcd = false;
206
207         val = readl(sys_clcd) & SYS_CLCD_ID_MASK;
208         if (val == SYS_CLCD_ID_SANYO_3_8)
209                 panel_name = "Sanyo TM38QV67A02A";
210         else if (val == SYS_CLCD_ID_SANYO_2_5) {
211                 panel_name = "Sanyo QVGA Portrait";
212                 is_sanyo_2_5_lcd = true;
213         } else if (val == SYS_CLCD_ID_EPSON_2_2)
214                 panel_name = "Epson L2F50113T00";
215         else if (val == SYS_CLCD_ID_VGA)
216                 panel_name = "VGA";
217         else {
218                 printk(KERN_ERR "CLCD: unknown LCD panel ID 0x%08x, using VGA\n",
219                         val);
220                 panel_name = "VGA";
221         }
222
223         fb->panel = versatile_clcd_get_panel(panel_name);
224         if (!fb->panel)
225                 return -EINVAL;
226
227         return versatile_clcd_setup_dma(fb, SZ_1M);
228 }
229
230 static void versatile_clcd_decode(struct clcd_fb *fb, struct clcd_regs *regs)
231 {
232         clcdfb_decode(fb, regs);
233
234         /* Always clear BGR for RGB565: we do the routing externally */
235         if (fb->fb.var.green.length == 6)
236                 regs->cntl &= ~CNTL_BGR;
237 }
238
239 static struct clcd_board clcd_plat_data = {
240         .name           = "Versatile",
241         .caps           = CLCD_CAP_5551 | CLCD_CAP_565 | CLCD_CAP_888,
242         .check          = clcdfb_check,
243         .decode         = versatile_clcd_decode,
244         .disable        = versatile_clcd_disable,
245         .enable         = versatile_clcd_enable,
246         .setup          = versatile_clcd_setup,
247         .mmap           = versatile_clcd_mmap_dma,
248         .remove         = versatile_clcd_remove_dma,
249 };
250
251 /*
252  * Lookup table for attaching a specific name and platform_data pointer to
253  * devices as they get created by of_platform_populate().  Ideally this table
254  * would not exist, but the current clock implementation depends on some devices
255  * having a specific name.
256  */
257 struct of_dev_auxdata versatile_auxdata_lookup[] __initdata = {
258         OF_DEV_AUXDATA("arm,primecell", VERSATILE_MMCI0_BASE, "fpga:05", &mmc0_plat_data),
259         OF_DEV_AUXDATA("arm,primecell", VERSATILE_MMCI1_BASE, "fpga:0b", &mmc1_plat_data),
260         OF_DEV_AUXDATA("arm,primecell", VERSATILE_CLCD_BASE, "dev:20", &clcd_plat_data),
261         {}
262 };
263
264 static struct map_desc versatile_io_desc[] __initdata __maybe_unused = {
265         {
266                 .virtual        =  IO_ADDRESS(VERSATILE_SCTL_BASE),
267                 .pfn            = __phys_to_pfn(VERSATILE_SCTL_BASE),
268                 .length         = SZ_4K * 9,
269                 .type           = MT_DEVICE
270         }
271 };
272
273 static void __init versatile_map_io(void)
274 {
275         debug_ll_io_init();
276         iotable_init(versatile_io_desc, ARRAY_SIZE(versatile_io_desc));
277 }
278
279 static void __init versatile_init_early(void)
280 {
281         u32 val;
282
283         /*
284          * set clock frequency:
285          *      VERSATILE_REFCLK is 32KHz
286          *      VERSATILE_TIMCLK is 1MHz
287          */
288         val = readl(__io_address(VERSATILE_SCTL_BASE));
289         writel((VERSATILE_TIMCLK << VERSATILE_TIMER1_EnSel) |
290                (VERSATILE_TIMCLK << VERSATILE_TIMER2_EnSel) |
291                (VERSATILE_TIMCLK << VERSATILE_TIMER3_EnSel) |
292                (VERSATILE_TIMCLK << VERSATILE_TIMER4_EnSel) | val,
293                __io_address(VERSATILE_SCTL_BASE));
294 }
295
296 static void __init versatile_dt_pci_init(void)
297 {
298         u32 val;
299         struct device_node *np;
300         struct property *newprop;
301
302         np = of_find_compatible_node(NULL, NULL, "arm,versatile-pci");
303         if (!np)
304                 return;
305
306         /* Check if PCI backplane is detected */
307         val = readl(versatile_sys_base + VERSATILE_SYS_PCICTL_OFFSET);
308         if (val & 1) {
309                 /*
310                  * Enable PCI accesses. Note that the documentaton is
311                  * inconsistent whether or not this is needed, but the old
312                  * driver had it so we will keep it.
313                  */
314                 writel(1, versatile_sys_base + VERSATILE_SYS_PCICTL_OFFSET);
315                 return;
316         }
317
318         newprop = kzalloc(sizeof(*newprop), GFP_KERNEL);
319         if (!newprop)
320                 return;
321
322         newprop->name = kstrdup("status", GFP_KERNEL);
323         newprop->value = kstrdup("disabled", GFP_KERNEL);
324         newprop->length = sizeof("disabled");
325         of_update_property(np, newprop);
326
327         pr_info("Not plugged into PCI backplane!\n");
328 }
329
330 static void __init versatile_dt_init(void)
331 {
332         struct device_node *np;
333
334         np = of_find_compatible_node(NULL, NULL, "arm,core-module-versatile");
335         if (np)
336                 versatile_sys_base = of_iomap(np, 0);
337         WARN_ON(!versatile_sys_base);
338
339         versatile_ib2_ctrl = ioremap(VERSATILE_IB2_CTL_BASE, SZ_4K);
340
341         versatile_dt_pci_init();
342
343         of_platform_default_populate(NULL, versatile_auxdata_lookup, NULL);
344 }
345
346 static const char *const versatile_dt_match[] __initconst = {
347         "arm,versatile-ab",
348         "arm,versatile-pb",
349         NULL,
350 };
351
352 DT_MACHINE_START(VERSATILE_PB, "ARM-Versatile (Device Tree Support)")
353         .map_io         = versatile_map_io,
354         .init_early     = versatile_init_early,
355         .init_machine   = versatile_dt_init,
356         .dt_compat      = versatile_dt_match,
357 MACHINE_END