Merge branch 'viafb-next' of git://github.com/schandinat/linux-2.6
[pandora-kernel.git] / arch / powerpc / platforms / 86xx / mpc8610_hpcd.c
1 /*
2  * MPC8610 HPCD board specific routines
3  *
4  * Initial author: Xianghua Xiao <x.xiao@freescale.com>
5  * Recode: Jason Jin <jason.jin@freescale.com>
6  *         York Sun <yorksun@freescale.com>
7  *
8  * Rewrite the interrupt routing. remove the 8259PIC support,
9  * All the integrated device in ULI use sideband interrupt.
10  *
11  * Copyright 2008 Freescale Semiconductor Inc.
12  *
13  * This program is free software; you can redistribute  it and/or modify it
14  * under  the terms of  the GNU General  Public License as published by the
15  * Free Software Foundation;  either version 2 of the  License, or (at your
16  * option) any later version.
17  */
18
19 #include <linux/stddef.h>
20 #include <linux/kernel.h>
21 #include <linux/pci.h>
22 #include <linux/interrupt.h>
23 #include <linux/kdev_t.h>
24 #include <linux/delay.h>
25 #include <linux/seq_file.h>
26 #include <linux/of.h>
27
28 #include <asm/system.h>
29 #include <asm/time.h>
30 #include <asm/machdep.h>
31 #include <asm/pci-bridge.h>
32 #include <asm/prom.h>
33 #include <mm/mmu_decl.h>
34 #include <asm/udbg.h>
35
36 #include <asm/mpic.h>
37
38 #include <linux/of_platform.h>
39 #include <sysdev/fsl_pci.h>
40 #include <sysdev/fsl_soc.h>
41 #include <sysdev/simple_gpio.h>
42
43 #include "mpc86xx.h"
44
45 static struct device_node *pixis_node;
46 static unsigned char *pixis_bdcfg0, *pixis_arch;
47
48 #ifdef CONFIG_SUSPEND
49 static irqreturn_t mpc8610_sw9_irq(int irq, void *data)
50 {
51         pr_debug("%s: PIXIS' event (sw9/wakeup) IRQ handled\n", __func__);
52         return IRQ_HANDLED;
53 }
54
55 static void __init mpc8610_suspend_init(void)
56 {
57         int irq;
58         int ret;
59
60         if (!pixis_node)
61                 return;
62
63         irq = irq_of_parse_and_map(pixis_node, 0);
64         if (!irq) {
65                 pr_err("%s: can't map pixis event IRQ.\n", __func__);
66                 return;
67         }
68
69         ret = request_irq(irq, mpc8610_sw9_irq, 0, "sw9:wakeup", NULL);
70         if (ret) {
71                 pr_err("%s: can't request pixis event IRQ: %d\n",
72                        __func__, ret);
73                 irq_dispose_mapping(irq);
74         }
75
76         enable_irq_wake(irq);
77 }
78 #else
79 static inline void mpc8610_suspend_init(void) { }
80 #endif /* CONFIG_SUSPEND */
81
82 static struct of_device_id __initdata mpc8610_ids[] = {
83         { .compatible = "fsl,mpc8610-immr", },
84         { .compatible = "fsl,mpc8610-guts", },
85         { .compatible = "simple-bus", },
86         /* So that the DMA channel nodes can be probed individually: */
87         { .compatible = "fsl,eloplus-dma", },
88         {}
89 };
90
91 static int __init mpc8610_declare_of_platform_devices(void)
92 {
93         /* Firstly, register PIXIS GPIOs. */
94         simple_gpiochip_init("fsl,fpga-pixis-gpio-bank");
95
96         /* Enable wakeup on PIXIS' event IRQ. */
97         mpc8610_suspend_init();
98
99         /* Without this call, the SSI device driver won't get probed. */
100         of_platform_bus_probe(NULL, mpc8610_ids, NULL);
101
102         return 0;
103 }
104 machine_device_initcall(mpc86xx_hpcd, mpc8610_declare_of_platform_devices);
105
106 #if defined(CONFIG_FB_FSL_DIU) || defined(CONFIG_FB_FSL_DIU_MODULE)
107
108 /*
109  * DIU Area Descriptor
110  *
111  * The MPC8610 reference manual shows the bits of the AD register in
112  * little-endian order, which causes the BLUE_C field to be split into two
113  * parts. To simplify the definition of the MAKE_AD() macro, we define the
114  * fields in big-endian order and byte-swap the result.
115  *
116  * So even though the registers don't look like they're in the
117  * same bit positions as they are on the P1022, the same value is written to
118  * the AD register on the MPC8610 and on the P1022.
119  */
120 #define AD_BYTE_F               0x10000000
121 #define AD_ALPHA_C_MASK         0x0E000000
122 #define AD_ALPHA_C_SHIFT        25
123 #define AD_BLUE_C_MASK          0x01800000
124 #define AD_BLUE_C_SHIFT         23
125 #define AD_GREEN_C_MASK         0x00600000
126 #define AD_GREEN_C_SHIFT        21
127 #define AD_RED_C_MASK           0x00180000
128 #define AD_RED_C_SHIFT          19
129 #define AD_PALETTE              0x00040000
130 #define AD_PIXEL_S_MASK         0x00030000
131 #define AD_PIXEL_S_SHIFT        16
132 #define AD_COMP_3_MASK          0x0000F000
133 #define AD_COMP_3_SHIFT         12
134 #define AD_COMP_2_MASK          0x00000F00
135 #define AD_COMP_2_SHIFT         8
136 #define AD_COMP_1_MASK          0x000000F0
137 #define AD_COMP_1_SHIFT         4
138 #define AD_COMP_0_MASK          0x0000000F
139 #define AD_COMP_0_SHIFT         0
140
141 #define MAKE_AD(alpha, red, blue, green, size, c0, c1, c2, c3) \
142         cpu_to_le32(AD_BYTE_F | (alpha << AD_ALPHA_C_SHIFT) | \
143         (blue << AD_BLUE_C_SHIFT) | (green << AD_GREEN_C_SHIFT) | \
144         (red << AD_RED_C_SHIFT) | (c3 << AD_COMP_3_SHIFT) | \
145         (c2 << AD_COMP_2_SHIFT) | (c1 << AD_COMP_1_SHIFT) | \
146         (c0 << AD_COMP_0_SHIFT) | (size << AD_PIXEL_S_SHIFT))
147
148 u32 mpc8610hpcd_get_pixel_format(enum fsl_diu_monitor_port port,
149                                  unsigned int bits_per_pixel)
150 {
151         static const u32 pixelformat[][3] = {
152                 {
153                         MAKE_AD(3, 0, 2, 1, 3, 8, 8, 8, 8),
154                         MAKE_AD(4, 2, 0, 1, 2, 8, 8, 8, 0),
155                         MAKE_AD(4, 0, 2, 1, 1, 5, 6, 5, 0)
156                 },
157                 {
158                         MAKE_AD(3, 2, 0, 1, 3, 8, 8, 8, 8),
159                         MAKE_AD(4, 0, 2, 1, 2, 8, 8, 8, 0),
160                         MAKE_AD(4, 2, 0, 1, 1, 5, 6, 5, 0)
161                 },
162         };
163         unsigned int arch_monitor;
164
165         /* The DVI port is mis-wired on revision 1 of this board. */
166         arch_monitor =
167                 ((*pixis_arch == 0x01) && (port == FSL_DIU_PORT_DVI)) ? 0 : 1;
168
169         switch (bits_per_pixel) {
170         case 32:
171                 return pixelformat[arch_monitor][0];
172         case 24:
173                 return pixelformat[arch_monitor][1];
174         case 16:
175                 return pixelformat[arch_monitor][2];
176         default:
177                 pr_err("fsl-diu: unsupported pixel depth %u\n", bits_per_pixel);
178                 return 0;
179         }
180 }
181
182 void mpc8610hpcd_set_gamma_table(enum fsl_diu_monitor_port port,
183                                  char *gamma_table_base)
184 {
185         int i;
186         if (port == FSL_DIU_PORT_DLVDS) {
187                 for (i = 0; i < 256*3; i++)
188                         gamma_table_base[i] = (gamma_table_base[i] << 2) |
189                                          ((gamma_table_base[i] >> 6) & 0x03);
190         }
191 }
192
193 #define PX_BRDCFG0_DVISEL       (1 << 3)
194 #define PX_BRDCFG0_DLINK        (1 << 4)
195 #define PX_BRDCFG0_DIU_MASK     (PX_BRDCFG0_DVISEL | PX_BRDCFG0_DLINK)
196
197 void mpc8610hpcd_set_monitor_port(enum fsl_diu_monitor_port port)
198 {
199         switch (port) {
200         case FSL_DIU_PORT_DVI:
201                 clrsetbits_8(pixis_bdcfg0, PX_BRDCFG0_DIU_MASK,
202                              PX_BRDCFG0_DVISEL | PX_BRDCFG0_DLINK);
203                 break;
204         case FSL_DIU_PORT_LVDS:
205                 clrsetbits_8(pixis_bdcfg0, PX_BRDCFG0_DIU_MASK,
206                              PX_BRDCFG0_DLINK);
207                 break;
208         case FSL_DIU_PORT_DLVDS:
209                 clrbits8(pixis_bdcfg0, PX_BRDCFG0_DIU_MASK);
210                 break;
211         }
212 }
213
214 void mpc8610hpcd_set_pixel_clock(unsigned int pixclock)
215 {
216         u32 __iomem *clkdvdr;
217         u32 temp;
218         /* variables for pixel clock calcs */
219         ulong  bestval, bestfreq, speed_ccb, minpixclock, maxpixclock;
220         ulong pixval;
221         long err;
222         int i;
223
224         clkdvdr = ioremap(get_immrbase() + 0xe0800, sizeof(u32));
225         if (!clkdvdr) {
226                 printk(KERN_ERR "Err: can't map clock divider register!\n");
227                 return;
228         }
229
230         /* Pixel Clock configuration */
231         speed_ccb = fsl_get_sys_freq();
232
233         /* Calculate the pixel clock with the smallest error */
234         /* calculate the following in steps to avoid overflow */
235         pr_debug("DIU pixclock in ps - %d\n", pixclock);
236         temp = 1000000000/pixclock;
237         temp *= 1000;
238         pixclock = temp;
239         pr_debug("DIU pixclock freq - %u\n", pixclock);
240
241         temp = pixclock * 5 / 100;
242         pr_debug("deviation = %d\n", temp);
243         minpixclock = pixclock - temp;
244         maxpixclock = pixclock + temp;
245         pr_debug("DIU minpixclock - %lu\n", minpixclock);
246         pr_debug("DIU maxpixclock - %lu\n", maxpixclock);
247         pixval = speed_ccb/pixclock;
248         pr_debug("DIU pixval = %lu\n", pixval);
249
250         err = 100000000;
251         bestval = pixval;
252         pr_debug("DIU bestval = %lu\n", bestval);
253
254         bestfreq = 0;
255         for (i = -1; i <= 1; i++) {
256                 temp = speed_ccb / ((pixval+i) + 1);
257                 pr_debug("DIU test pixval i= %d, pixval=%lu, temp freq. = %u\n",
258                                                         i, pixval, temp);
259                 if ((temp < minpixclock) || (temp > maxpixclock))
260                         pr_debug("DIU exceeds monitor range (%lu to %lu)\n",
261                                 minpixclock, maxpixclock);
262                 else if (abs(temp - pixclock) < err) {
263                   pr_debug("Entered the else if block %d\n", i);
264                         err = abs(temp - pixclock);
265                         bestval = pixval+i;
266                         bestfreq = temp;
267                 }
268         }
269
270         pr_debug("DIU chose = %lx\n", bestval);
271         pr_debug("DIU error = %ld\n NomPixClk ", err);
272         pr_debug("DIU: Best Freq = %lx\n", bestfreq);
273         /* Modify PXCLK in GUTS CLKDVDR */
274         pr_debug("DIU: Current value of CLKDVDR = 0x%08x\n", (*clkdvdr));
275         temp = (*clkdvdr) & 0x2000FFFF;
276         *clkdvdr = temp;                /* turn off clock */
277         *clkdvdr = temp | 0x80000000 | (((bestval) & 0x1F) << 16);
278         pr_debug("DIU: Modified value of CLKDVDR = 0x%08x\n", (*clkdvdr));
279         iounmap(clkdvdr);
280 }
281
282 enum fsl_diu_monitor_port
283 mpc8610hpcd_valid_monitor_port(enum fsl_diu_monitor_port port)
284 {
285         return port;
286 }
287
288 #endif
289
290 static void __init mpc86xx_hpcd_setup_arch(void)
291 {
292         struct resource r;
293         struct device_node *np;
294         unsigned char *pixis;
295
296         if (ppc_md.progress)
297                 ppc_md.progress("mpc86xx_hpcd_setup_arch()", 0);
298
299 #ifdef CONFIG_PCI
300         for_each_node_by_type(np, "pci") {
301                 if (of_device_is_compatible(np, "fsl,mpc8610-pci")
302                     || of_device_is_compatible(np, "fsl,mpc8641-pcie")) {
303                         struct resource rsrc;
304                         of_address_to_resource(np, 0, &rsrc);
305                         if ((rsrc.start & 0xfffff) == 0xa000)
306                                 fsl_add_bridge(np, 1);
307                         else
308                                 fsl_add_bridge(np, 0);
309                 }
310         }
311 #endif
312 #if defined(CONFIG_FB_FSL_DIU) || defined(CONFIG_FB_FSL_DIU_MODULE)
313         diu_ops.get_pixel_format        = mpc8610hpcd_get_pixel_format;
314         diu_ops.set_gamma_table         = mpc8610hpcd_set_gamma_table;
315         diu_ops.set_monitor_port        = mpc8610hpcd_set_monitor_port;
316         diu_ops.set_pixel_clock         = mpc8610hpcd_set_pixel_clock;
317         diu_ops.valid_monitor_port      = mpc8610hpcd_valid_monitor_port;
318 #endif
319
320         pixis_node = of_find_compatible_node(NULL, NULL, "fsl,fpga-pixis");
321         if (pixis_node) {
322                 of_address_to_resource(pixis_node, 0, &r);
323                 of_node_put(pixis_node);
324                 pixis = ioremap(r.start, 32);
325                 if (!pixis) {
326                         printk(KERN_ERR "Err: can't map FPGA cfg register!\n");
327                         return;
328                 }
329                 pixis_bdcfg0 = pixis + 8;
330                 pixis_arch = pixis + 1;
331         } else
332                 printk(KERN_ERR "Err: "
333                                 "can't find device node 'fsl,fpga-pixis'\n");
334
335         printk("MPC86xx HPCD board from Freescale Semiconductor\n");
336 }
337
338 /*
339  * Called very early, device-tree isn't unflattened
340  */
341 static int __init mpc86xx_hpcd_probe(void)
342 {
343         unsigned long root = of_get_flat_dt_root();
344
345         if (of_flat_dt_is_compatible(root, "fsl,MPC8610HPCD"))
346                 return 1;       /* Looks good */
347
348         return 0;
349 }
350
351 static long __init mpc86xx_time_init(void)
352 {
353         unsigned int temp;
354
355         /* Set the time base to zero */
356         mtspr(SPRN_TBWL, 0);
357         mtspr(SPRN_TBWU, 0);
358
359         temp = mfspr(SPRN_HID0);
360         temp |= HID0_TBEN;
361         mtspr(SPRN_HID0, temp);
362         asm volatile("isync");
363
364         return 0;
365 }
366
367 define_machine(mpc86xx_hpcd) {
368         .name                   = "MPC86xx HPCD",
369         .probe                  = mpc86xx_hpcd_probe,
370         .setup_arch             = mpc86xx_hpcd_setup_arch,
371         .init_IRQ               = mpc86xx_init_irq,
372         .get_irq                = mpic_get_irq,
373         .restart                = fsl_rstcr_restart,
374         .time_init              = mpc86xx_time_init,
375         .calibrate_decr         = generic_calibrate_decr,
376         .progress               = udbg_progress,
377         .pcibios_fixup_bus      = fsl_pcibios_fixup_bus,
378 };