89ee02c54561d1802fd72a49afaedd88fc9e2368
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / platforms / 85xx / p1022_ds.c
1 /*
2  * P1022DS board specific routines
3  *
4  * Authors: Travis Wheatley <travis.wheatley@freescale.com>
5  *          Dave Liu <daveliu@freescale.com>
6  *          Timur Tabi <timur@freescale.com>
7  *
8  * Copyright 2010 Freescale Semiconductor, Inc.
9  *
10  * This file is taken from the Freescale P1022DS BSP, with modifications:
11  * 2) No AMP support
12  * 3) No PCI endpoint support
13  *
14  * This file is licensed under the terms of the GNU General Public License
15  * version 2.  This program is licensed "as is" without any warranty of any
16  * kind, whether express or implied.
17  */
18
19 #include <linux/pci.h>
20 #include <linux/of_platform.h>
21 #include <linux/memblock.h>
22 #include <asm/div64.h>
23 #include <asm/mpic.h>
24 #include <asm/swiotlb.h>
25
26 #include <sysdev/fsl_soc.h>
27 #include <sysdev/fsl_pci.h>
28 #include <asm/udbg.h>
29 #include <asm/fsl_guts.h>
30 #include <asm/fsl_lbc.h>
31 #include "smp.h"
32
33 #include "mpc85xx.h"
34
35 #if defined(CONFIG_FB_FSL_DIU) || defined(CONFIG_FB_FSL_DIU_MODULE)
36
37 #define PMUXCR_ELBCDIU_MASK     0xc0000000
38 #define PMUXCR_ELBCDIU_NOR16    0x80000000
39 #define PMUXCR_ELBCDIU_DIU      0x40000000
40
41 /*
42  * Board-specific initialization of the DIU.  This code should probably be
43  * executed when the DIU is opened, rather than in arch code, but the DIU
44  * driver does not have a mechanism for this (yet).
45  *
46  * This is especially problematic on the P1022DS because the local bus (eLBC)
47  * and the DIU video signals share the same pins, which means that enabling the
48  * DIU will disable access to NOR flash.
49  */
50
51 /* DIU Pixel Clock bits of the CLKDVDR Global Utilities register */
52 #define CLKDVDR_PXCKEN          0x80000000
53 #define CLKDVDR_PXCKINV         0x10000000
54 #define CLKDVDR_PXCKDLY         0x06000000
55 #define CLKDVDR_PXCLK_MASK      0x00FF0000
56
57 /* Some ngPIXIS register definitions */
58 #define PX_CTL          3
59 #define PX_BRDCFG0      8
60 #define PX_BRDCFG1      9
61
62 #define PX_BRDCFG0_ELBC_SPI_MASK        0xc0
63 #define PX_BRDCFG0_ELBC_SPI_ELBC        0x00
64 #define PX_BRDCFG0_ELBC_SPI_NULL        0xc0
65 #define PX_BRDCFG0_ELBC_DIU             0x02
66
67 #define PX_BRDCFG1_DVIEN        0x80
68 #define PX_BRDCFG1_DFPEN        0x40
69 #define PX_BRDCFG1_BACKLIGHT    0x20
70 #define PX_BRDCFG1_DDCEN        0x10
71
72 #define PX_CTL_ALTACC           0x80
73
74 /*
75  * DIU Area Descriptor
76  *
77  * Note that we need to byte-swap the value before it's written to the AD
78  * register.  So even though the registers don't look like they're in the same
79  * bit positions as they are on the MPC8610, the same value is written to the
80  * AD register on the MPC8610 and on the P1022.
81  */
82 #define AD_BYTE_F               0x10000000
83 #define AD_ALPHA_C_MASK         0x0E000000
84 #define AD_ALPHA_C_SHIFT        25
85 #define AD_BLUE_C_MASK          0x01800000
86 #define AD_BLUE_C_SHIFT         23
87 #define AD_GREEN_C_MASK         0x00600000
88 #define AD_GREEN_C_SHIFT        21
89 #define AD_RED_C_MASK           0x00180000
90 #define AD_RED_C_SHIFT          19
91 #define AD_PALETTE              0x00040000
92 #define AD_PIXEL_S_MASK         0x00030000
93 #define AD_PIXEL_S_SHIFT        16
94 #define AD_COMP_3_MASK          0x0000F000
95 #define AD_COMP_3_SHIFT         12
96 #define AD_COMP_2_MASK          0x00000F00
97 #define AD_COMP_2_SHIFT         8
98 #define AD_COMP_1_MASK          0x000000F0
99 #define AD_COMP_1_SHIFT         4
100 #define AD_COMP_0_MASK          0x0000000F
101 #define AD_COMP_0_SHIFT         0
102
103 #define MAKE_AD(alpha, red, blue, green, size, c0, c1, c2, c3) \
104         cpu_to_le32(AD_BYTE_F | (alpha << AD_ALPHA_C_SHIFT) | \
105         (blue << AD_BLUE_C_SHIFT) | (green << AD_GREEN_C_SHIFT) | \
106         (red << AD_RED_C_SHIFT) | (c3 << AD_COMP_3_SHIFT) | \
107         (c2 << AD_COMP_2_SHIFT) | (c1 << AD_COMP_1_SHIFT) | \
108         (c0 << AD_COMP_0_SHIFT) | (size << AD_PIXEL_S_SHIFT))
109
110 /**
111  * p1022ds_get_pixel_format: return the Area Descriptor for a given pixel depth
112  *
113  * The Area Descriptor is a 32-bit value that determine which bits in each
114  * pixel are to be used for each color.
115  */
116 static u32 p1022ds_get_pixel_format(enum fsl_diu_monitor_port port,
117                                     unsigned int bits_per_pixel)
118 {
119         switch (bits_per_pixel) {
120         case 32:
121                 /* 0x88883316 */
122                 return MAKE_AD(3, 2, 0, 1, 3, 8, 8, 8, 8);
123         case 24:
124                 /* 0x88082219 */
125                 return MAKE_AD(4, 0, 1, 2, 2, 0, 8, 8, 8);
126         case 16:
127                 /* 0x65053118 */
128                 return MAKE_AD(4, 2, 1, 0, 1, 5, 6, 5, 0);
129         default:
130                 pr_err("fsl-diu: unsupported pixel depth %u\n", bits_per_pixel);
131                 return 0;
132         }
133 }
134
135 /**
136  * p1022ds_set_gamma_table: update the gamma table, if necessary
137  *
138  * On some boards, the gamma table for some ports may need to be modified.
139  * This is not the case on the P1022DS, so we do nothing.
140 */
141 static void p1022ds_set_gamma_table(enum fsl_diu_monitor_port port,
142                                     char *gamma_table_base)
143 {
144 }
145
146 struct fsl_law {
147         u32     lawbar;
148         u32     reserved1;
149         u32     lawar;
150         u32     reserved[5];
151 };
152
153 #define LAWBAR_MASK     0x00F00000
154 #define LAWBAR_SHIFT    12
155
156 #define LAWAR_EN        0x80000000
157 #define LAWAR_TGT_MASK  0x01F00000
158 #define LAW_TRGT_IF_LBC (0x04 << 20)
159
160 #define LAWAR_MASK      (LAWAR_EN | LAWAR_TGT_MASK)
161 #define LAWAR_MATCH     (LAWAR_EN | LAW_TRGT_IF_LBC)
162
163 #define BR_BA           0xFFFF8000
164
165 /*
166  * Map a BRx value to a physical address
167  *
168  * The localbus BRx registers only store the lower 32 bits of the address.  To
169  * obtain the upper four bits, we need to scan the LAW table.  The entry which
170  * maps to the localbus will contain the upper four bits.
171  */
172 static phys_addr_t lbc_br_to_phys(const void *ecm, unsigned int count, u32 br)
173 {
174 #ifndef CONFIG_PHYS_64BIT
175         /*
176          * If we only have 32-bit addressing, then the BRx address *is* the
177          * physical address.
178          */
179         return br & BR_BA;
180 #else
181         const struct fsl_law *law = ecm + 0xc08;
182         unsigned int i;
183
184         for (i = 0; i < count; i++) {
185                 u64 lawbar = in_be32(&law[i].lawbar);
186                 u32 lawar = in_be32(&law[i].lawar);
187
188                 if ((lawar & LAWAR_MASK) == LAWAR_MATCH)
189                         /* Extract the upper four bits */
190                         return (br & BR_BA) | ((lawbar & LAWBAR_MASK) << 12);
191         }
192
193         return 0;
194 #endif
195 }
196
197 /**
198  * p1022ds_set_monitor_port: switch the output to a different monitor port
199  */
200 static void p1022ds_set_monitor_port(enum fsl_diu_monitor_port port)
201 {
202         struct device_node *guts_node;
203         struct device_node *lbc_node = NULL;
204         struct device_node *law_node = NULL;
205         struct ccsr_guts __iomem *guts;
206         struct fsl_lbc_regs *lbc = NULL;
207         void *ecm = NULL;
208         u8 __iomem *lbc_lcs0_ba = NULL;
209         u8 __iomem *lbc_lcs1_ba = NULL;
210         phys_addr_t cs0_addr, cs1_addr;
211         const __be32 *iprop;
212         unsigned int num_laws;
213         u8 b;
214
215         /* Map the global utilities registers. */
216         guts_node = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
217         if (!guts_node) {
218                 pr_err("p1022ds: missing global utilties device node\n");
219                 return;
220         }
221
222         guts = of_iomap(guts_node, 0);
223         if (!guts) {
224                 pr_err("p1022ds: could not map global utilties device\n");
225                 goto exit;
226         }
227
228         lbc_node = of_find_compatible_node(NULL, NULL, "fsl,p1022-elbc");
229         if (!lbc_node) {
230                 pr_err("p1022ds: missing localbus node\n");
231                 goto exit;
232         }
233
234         lbc = of_iomap(lbc_node, 0);
235         if (!lbc) {
236                 pr_err("p1022ds: could not map localbus node\n");
237                 goto exit;
238         }
239
240         law_node = of_find_compatible_node(NULL, NULL, "fsl,ecm-law");
241         if (!law_node) {
242                 pr_err("p1022ds: missing local access window node\n");
243                 goto exit;
244         }
245
246         ecm = of_iomap(law_node, 0);
247         if (!ecm) {
248                 pr_err("p1022ds: could not map local access window node\n");
249                 goto exit;
250         }
251
252         iprop = of_get_property(law_node, "fsl,num-laws", 0);
253         if (!iprop) {
254                 pr_err("p1022ds: LAW node is missing fsl,num-laws property\n");
255                 goto exit;
256         }
257         num_laws = be32_to_cpup(iprop);
258
259         cs0_addr = lbc_br_to_phys(ecm, num_laws, in_be32(&lbc->bank[0].br));
260         cs1_addr = lbc_br_to_phys(ecm, num_laws, in_be32(&lbc->bank[1].br));
261
262         lbc_lcs0_ba = ioremap(cs0_addr, 1);
263         lbc_lcs1_ba = ioremap(cs1_addr, 1);
264
265         /* Make sure we're in indirect mode first. */
266         if ((in_be32(&guts->pmuxcr) & PMUXCR_ELBCDIU_MASK) !=
267             PMUXCR_ELBCDIU_DIU) {
268                 struct device_node *pixis_node;
269                 void __iomem *pixis;
270
271                 pixis_node =
272                         of_find_compatible_node(NULL, NULL, "fsl,p1022ds-fpga");
273                 if (!pixis_node) {
274                         pr_err("p1022ds: missing pixis node\n");
275                         goto exit;
276                 }
277
278                 pixis = of_iomap(pixis_node, 0);
279                 of_node_put(pixis_node);
280                 if (!pixis) {
281                         pr_err("p1022ds: could not map pixis registers\n");
282                         goto exit;
283                 }
284
285                 /* Enable indirect PIXIS mode.  */
286                 setbits8(pixis + PX_CTL, PX_CTL_ALTACC);
287                 iounmap(pixis);
288
289                 /* Switch the board mux to the DIU */
290                 out_8(lbc_lcs0_ba, PX_BRDCFG0); /* BRDCFG0 */
291                 b = in_8(lbc_lcs1_ba);
292                 b |= PX_BRDCFG0_ELBC_DIU;
293                 out_8(lbc_lcs1_ba, b);
294
295                 /* Set the chip mux to DIU mode. */
296                 clrsetbits_be32(&guts->pmuxcr, PMUXCR_ELBCDIU_MASK,
297                                 PMUXCR_ELBCDIU_DIU);
298                 in_be32(&guts->pmuxcr);
299         }
300
301
302         switch (port) {
303         case FSL_DIU_PORT_DVI:
304                 /* Enable the DVI port, disable the DFP and the backlight */
305                 out_8(lbc_lcs0_ba, PX_BRDCFG1);
306                 b = in_8(lbc_lcs1_ba);
307                 b &= ~(PX_BRDCFG1_DFPEN | PX_BRDCFG1_BACKLIGHT);
308                 b |= PX_BRDCFG1_DVIEN;
309                 out_8(lbc_lcs1_ba, b);
310                 break;
311         case FSL_DIU_PORT_LVDS:
312                 /*
313                  * LVDS also needs backlight enabled, otherwise the display
314                  * will be blank.
315                  */
316                 /* Enable the DFP port, disable the DVI and the backlight */
317                 out_8(lbc_lcs0_ba, PX_BRDCFG1);
318                 b = in_8(lbc_lcs1_ba);
319                 b &= ~PX_BRDCFG1_DVIEN;
320                 b |= PX_BRDCFG1_DFPEN | PX_BRDCFG1_BACKLIGHT;
321                 out_8(lbc_lcs1_ba, b);
322                 break;
323         default:
324                 pr_err("p1022ds: unsupported monitor port %i\n", port);
325         }
326
327 exit:
328         if (lbc_lcs1_ba)
329                 iounmap(lbc_lcs1_ba);
330         if (lbc_lcs0_ba)
331                 iounmap(lbc_lcs0_ba);
332         if (lbc)
333                 iounmap(lbc);
334         if (ecm)
335                 iounmap(ecm);
336         if (guts)
337                 iounmap(guts);
338
339         of_node_put(law_node);
340         of_node_put(lbc_node);
341         of_node_put(guts_node);
342 }
343
344 /**
345  * p1022ds_set_pixel_clock: program the DIU's clock
346  *
347  * @pixclock: the wavelength, in picoseconds, of the clock
348  */
349 void p1022ds_set_pixel_clock(unsigned int pixclock)
350 {
351         struct device_node *guts_np = NULL;
352         struct ccsr_guts __iomem *guts;
353         unsigned long freq;
354         u64 temp;
355         u32 pxclk;
356
357         /* Map the global utilities registers. */
358         guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
359         if (!guts_np) {
360                 pr_err("p1022ds: missing global utilties device node\n");
361                 return;
362         }
363
364         guts = of_iomap(guts_np, 0);
365         of_node_put(guts_np);
366         if (!guts) {
367                 pr_err("p1022ds: could not map global utilties device\n");
368                 return;
369         }
370
371         /* Convert pixclock from a wavelength to a frequency */
372         temp = 1000000000000ULL;
373         do_div(temp, pixclock);
374         freq = temp;
375
376         /*
377          * 'pxclk' is the ratio of the platform clock to the pixel clock.
378          * This number is programmed into the CLKDVDR register, and the valid
379          * range of values is 2-255.
380          */
381         pxclk = DIV_ROUND_CLOSEST(fsl_get_sys_freq(), freq);
382         pxclk = clamp_t(u32, pxclk, 2, 255);
383
384         /* Disable the pixel clock, and set it to non-inverted and no delay */
385         clrbits32(&guts->clkdvdr,
386                   CLKDVDR_PXCKEN | CLKDVDR_PXCKDLY | CLKDVDR_PXCLK_MASK);
387
388         /* Enable the clock and set the pxclk */
389         setbits32(&guts->clkdvdr, CLKDVDR_PXCKEN | (pxclk << 16));
390
391         iounmap(guts);
392 }
393
394 /**
395  * p1022ds_valid_monitor_port: set the monitor port for sysfs
396  */
397 enum fsl_diu_monitor_port
398 p1022ds_valid_monitor_port(enum fsl_diu_monitor_port port)
399 {
400         switch (port) {
401         case FSL_DIU_PORT_DVI:
402         case FSL_DIU_PORT_LVDS:
403                 return port;
404         default:
405                 return FSL_DIU_PORT_DVI; /* Dual-link LVDS is not supported */
406         }
407 }
408
409 #endif
410
411 void __init p1022_ds_pic_init(void)
412 {
413         struct mpic *mpic = mpic_alloc(NULL, 0, MPIC_BIG_ENDIAN |
414                 MPIC_SINGLE_DEST_CPU,
415                 0, 256, " OpenPIC  ");
416         BUG_ON(mpic == NULL);
417         mpic_init(mpic);
418 }
419
420 #if defined(CONFIG_FB_FSL_DIU) || defined(CONFIG_FB_FSL_DIU_MODULE)
421
422 /*
423  * Disables a node in the device tree.
424  *
425  * This function is called before kmalloc() is available, so the 'new' object
426  * should be allocated in the global area.  The easiest way is to do that is
427  * to allocate one static local variable for each call to this function.
428  */
429 static void __init disable_one_node(struct device_node *np, struct property *new)
430 {
431         prom_update_property(np, new);
432 }
433
434 /* TRUE if there is a "video=fslfb" command-line parameter. */
435 static bool fslfb;
436
437 /*
438  * Search for a "video=fslfb" command-line parameter, and set 'fslfb' to
439  * true if we find it.
440  *
441  * We need to use early_param() instead of __setup() because the normal
442  * __setup() gets called to late.  However, early_param() gets called very
443  * early, before the device tree is unflattened, so all we can do now is set a
444  * global variable.  Later on, p1022_ds_setup_arch() will use that variable
445  * to determine if we need to update the device tree.
446  */
447 static int __init early_video_setup(char *options)
448 {
449         fslfb = (strncmp(options, "fslfb:", 6) == 0);
450
451         return 0;
452 }
453 early_param("video", early_video_setup);
454
455 #endif
456
457 /*
458  * Setup the architecture
459  */
460 static void __init p1022_ds_setup_arch(void)
461 {
462 #ifdef CONFIG_PCI
463         struct device_node *np;
464 #endif
465         dma_addr_t max = 0xffffffff;
466
467         if (ppc_md.progress)
468                 ppc_md.progress("p1022_ds_setup_arch()", 0);
469
470 #ifdef CONFIG_PCI
471         for_each_compatible_node(np, "pci", "fsl,p1022-pcie") {
472                 struct resource rsrc;
473                 struct pci_controller *hose;
474
475                 of_address_to_resource(np, 0, &rsrc);
476
477                 if ((rsrc.start & 0xfffff) == 0x8000)
478                         fsl_add_bridge(np, 1);
479                 else
480                         fsl_add_bridge(np, 0);
481
482                 hose = pci_find_hose_for_OF_device(np);
483                 max = min(max, hose->dma_window_base_cur +
484                           hose->dma_window_size);
485         }
486 #endif
487
488 #if defined(CONFIG_FB_FSL_DIU) || defined(CONFIG_FB_FSL_DIU_MODULE)
489         diu_ops.get_pixel_format        = p1022ds_get_pixel_format;
490         diu_ops.set_gamma_table         = p1022ds_set_gamma_table;
491         diu_ops.set_monitor_port        = p1022ds_set_monitor_port;
492         diu_ops.set_pixel_clock         = p1022ds_set_pixel_clock;
493         diu_ops.valid_monitor_port      = p1022ds_valid_monitor_port;
494
495         /*
496          * Disable the NOR flash node if there is video=fslfb... command-line
497          * parameter.  When the DIU is active, NOR flash is unavailable, so we
498          * have to disable the node before the MTD driver loads.
499          */
500         if (fslfb) {
501                 struct device_node *np =
502                         of_find_compatible_node(NULL, NULL, "fsl,p1022-elbc");
503
504                 if (np) {
505                         np = of_find_compatible_node(np, NULL, "cfi-flash");
506                         if (np) {
507                                 static struct property nor_status = {
508                                         .name = "status",
509                                         .value = "disabled",
510                                         .length = sizeof("disabled"),
511                                 };
512
513                                 pr_info("p1022ds: disabling %s node",
514                                         np->full_name);
515                                 disable_one_node(np, &nor_status);
516                                 of_node_put(np);
517                         }
518                 }
519
520         }
521
522 #endif
523
524         mpc85xx_smp_init();
525
526 #ifdef CONFIG_SWIOTLB
527         if ((memblock_end_of_DRAM() - 1) > max) {
528                 ppc_swiotlb_enable = 1;
529                 set_pci_dma_ops(&swiotlb_dma_ops);
530                 ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_swiotlb;
531         }
532 #endif
533
534         pr_info("Freescale P1022 DS reference board\n");
535 }
536
537 machine_device_initcall(p1022_ds, mpc85xx_common_publish_devices);
538
539 machine_arch_initcall(p1022_ds, swiotlb_setup_bus_notifier);
540
541 /*
542  * Called very early, device-tree isn't unflattened
543  */
544 static int __init p1022_ds_probe(void)
545 {
546         unsigned long root = of_get_flat_dt_root();
547
548         return of_flat_dt_is_compatible(root, "fsl,p1022ds");
549 }
550
551 define_machine(p1022_ds) {
552         .name                   = "P1022 DS",
553         .probe                  = p1022_ds_probe,
554         .setup_arch             = p1022_ds_setup_arch,
555         .init_IRQ               = p1022_ds_pic_init,
556 #ifdef CONFIG_PCI
557         .pcibios_fixup_bus      = fsl_pcibios_fixup_bus,
558 #endif
559         .get_irq                = mpic_get_irq,
560         .restart                = fsl_rstcr_restart,
561         .calibrate_decr         = generic_calibrate_decr,
562         .progress               = udbg_progress,
563 };