Merge branch 'linus' into x86/gart
[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/kdev_t.h>
23 #include <linux/delay.h>
24 #include <linux/seq_file.h>
25 #include <linux/of.h>
26
27 #include <asm/system.h>
28 #include <asm/time.h>
29 #include <asm/machdep.h>
30 #include <asm/pci-bridge.h>
31 #include <asm/mpc86xx.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
42 static unsigned char *pixis_bdcfg0, *pixis_arch;
43
44 static struct of_device_id __initdata mpc8610_ids[] = {
45         { .compatible = "fsl,mpc8610-immr", },
46         { .compatible = "simple-bus", },
47         {}
48 };
49
50 static int __init mpc8610_declare_of_platform_devices(void)
51 {
52         /* Without this call, the SSI device driver won't get probed. */
53         of_platform_bus_probe(NULL, mpc8610_ids, NULL);
54
55         return 0;
56 }
57 machine_device_initcall(mpc86xx_hpcd, mpc8610_declare_of_platform_devices);
58
59 static void __init mpc86xx_hpcd_init_irq(void)
60 {
61         struct mpic *mpic1;
62         struct device_node *np;
63         struct resource res;
64
65         /* Determine PIC address. */
66         np = of_find_node_by_type(NULL, "open-pic");
67         if (np == NULL)
68                 return;
69         of_address_to_resource(np, 0, &res);
70
71         /* Alloc mpic structure and per isu has 16 INT entries. */
72         mpic1 = mpic_alloc(np, res.start,
73                         MPIC_PRIMARY | MPIC_WANTS_RESET | MPIC_BIG_ENDIAN,
74                         0, 256, " MPIC     ");
75         BUG_ON(mpic1 == NULL);
76
77         mpic_init(mpic1);
78 }
79
80 #ifdef CONFIG_PCI
81 static void __devinit quirk_uli1575(struct pci_dev *dev)
82 {
83         u32 temp32;
84
85         /* Disable INTx */
86         pci_read_config_dword(dev, 0x48, &temp32);
87         pci_write_config_dword(dev, 0x48, (temp32 | 1<<26));
88
89         /* Enable sideband interrupt */
90         pci_read_config_dword(dev, 0x90, &temp32);
91         pci_write_config_dword(dev, 0x90, (temp32 | 1<<22));
92 }
93
94 static void __devinit quirk_uli5288(struct pci_dev *dev)
95 {
96         unsigned char c;
97         unsigned short temp;
98
99         /* Interrupt Disable, Needed when SATA disabled */
100         pci_read_config_word(dev, PCI_COMMAND, &temp);
101         temp |= 1<<10;
102         pci_write_config_word(dev, PCI_COMMAND, temp);
103
104         pci_read_config_byte(dev, 0x83, &c);
105         c |= 0x80;
106         pci_write_config_byte(dev, 0x83, c);
107
108         pci_write_config_byte(dev, PCI_CLASS_PROG, 0x01);
109         pci_write_config_byte(dev, PCI_CLASS_DEVICE, 0x06);
110
111         pci_read_config_byte(dev, 0x83, &c);
112         c &= 0x7f;
113         pci_write_config_byte(dev, 0x83, c);
114 }
115
116 /*
117  * Since 8259PIC was disabled on the board, the IDE device can not
118  * use the legacy IRQ, we need to let the IDE device work under
119  * native mode and use the interrupt line like other PCI devices.
120  * IRQ14 is a sideband interrupt from IDE device to CPU and we use this
121  * as the interrupt for IDE device.
122  */
123 static void __devinit quirk_uli5229(struct pci_dev *dev)
124 {
125         unsigned char c;
126
127         pci_read_config_byte(dev, 0x4b, &c);
128         c |= 0x10;
129         pci_write_config_byte(dev, 0x4b, c);
130 }
131
132 /*
133  * SATA interrupt pin bug fix
134  * There's a chip bug for 5288, The interrupt pin should be 2,
135  * not the read only value 1, So it use INTB#, not INTA# which
136  * actually used by the IDE device 5229.
137  * As of this bug, during the PCI initialization, 5288 read the
138  * irq of IDE device from the device tree, this function fix this
139  * bug by re-assigning a correct irq to 5288.
140  *
141  */
142 static void __devinit final_uli5288(struct pci_dev *dev)
143 {
144         struct pci_controller *hose = pci_bus_to_host(dev->bus);
145         struct device_node *hosenode = hose ? hose->dn : NULL;
146         struct of_irq oirq;
147         int virq, pin = 2;
148         u32 laddr[3];
149
150         if (!hosenode)
151                 return;
152
153         laddr[0] = (hose->first_busno << 16) | (PCI_DEVFN(31, 0) << 8);
154         laddr[1] = laddr[2] = 0;
155         of_irq_map_raw(hosenode, &pin, 1, laddr, &oirq);
156         virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
157                                      oirq.size);
158         dev->irq = virq;
159 }
160
161 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AL, 0x1575, quirk_uli1575);
162 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AL, 0x5288, quirk_uli5288);
163 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AL, 0x5229, quirk_uli5229);
164 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AL, 0x5288, final_uli5288);
165 #endif /* CONFIG_PCI */
166
167 #if defined(CONFIG_FB_FSL_DIU) || defined(CONFIG_FB_FSL_DIU_MODULE)
168
169 static u32 get_busfreq(void)
170 {
171         struct device_node *node;
172
173         u32 fs_busfreq = 0;
174         node = of_find_node_by_type(NULL, "cpu");
175         if (node) {
176                 unsigned int size;
177                 const unsigned int *prop =
178                         of_get_property(node, "bus-frequency", &size);
179                 if (prop)
180                         fs_busfreq = *prop;
181                 of_node_put(node);
182         };
183         return fs_busfreq;
184 }
185
186 unsigned int mpc8610hpcd_get_pixel_format(unsigned int bits_per_pixel,
187                                                 int monitor_port)
188 {
189         static const unsigned long pixelformat[][3] = {
190                 {0x88882317, 0x88083218, 0x65052119},
191                 {0x88883316, 0x88082219, 0x65053118},
192         };
193         unsigned int pix_fmt, arch_monitor;
194
195         arch_monitor = ((*pixis_arch == 0x01) && (monitor_port == 0))? 0 : 1;
196                 /* DVI port for board version 0x01 */
197
198         if (bits_per_pixel == 32)
199                 pix_fmt = pixelformat[arch_monitor][0];
200         else if (bits_per_pixel == 24)
201                 pix_fmt = pixelformat[arch_monitor][1];
202         else if (bits_per_pixel == 16)
203                 pix_fmt = pixelformat[arch_monitor][2];
204         else
205                 pix_fmt = pixelformat[1][0];
206
207         return pix_fmt;
208 }
209
210 void mpc8610hpcd_set_gamma_table(int monitor_port, char *gamma_table_base)
211 {
212         int i;
213         if (monitor_port == 2) {                /* dual link LVDS */
214                 for (i = 0; i < 256*3; i++)
215                         gamma_table_base[i] = (gamma_table_base[i] << 2) |
216                                          ((gamma_table_base[i] >> 6) & 0x03);
217         }
218 }
219
220 #define PX_BRDCFG0_DVISEL       (1 << 3)
221 #define PX_BRDCFG0_DLINK        (1 << 4)
222 #define PX_BRDCFG0_DIU_MASK     (PX_BRDCFG0_DVISEL | PX_BRDCFG0_DLINK)
223
224 void mpc8610hpcd_set_monitor_port(int monitor_port)
225 {
226         static const u8 bdcfg[] = {
227                 PX_BRDCFG0_DVISEL | PX_BRDCFG0_DLINK,
228                 PX_BRDCFG0_DLINK,
229                 0,
230         };
231
232         if (monitor_port < 3)
233                 clrsetbits_8(pixis_bdcfg0, PX_BRDCFG0_DIU_MASK,
234                              bdcfg[monitor_port]);
235 }
236
237 void mpc8610hpcd_set_pixel_clock(unsigned int pixclock)
238 {
239         u32 __iomem *clkdvdr;
240         u32 temp;
241         /* variables for pixel clock calcs */
242         ulong  bestval, bestfreq, speed_ccb, minpixclock, maxpixclock;
243         ulong pixval;
244         long err;
245         int i;
246
247         clkdvdr = ioremap(get_immrbase() + 0xe0800, sizeof(u32));
248         if (!clkdvdr) {
249                 printk(KERN_ERR "Err: can't map clock divider register!\n");
250                 return;
251         }
252
253         /* Pixel Clock configuration */
254         pr_debug("DIU: Bus Frequency = %d\n", get_busfreq());
255         speed_ccb = get_busfreq();
256
257         /* Calculate the pixel clock with the smallest error */
258         /* calculate the following in steps to avoid overflow */
259         pr_debug("DIU pixclock in ps - %d\n", pixclock);
260         temp = 1000000000/pixclock;
261         temp *= 1000;
262         pixclock = temp;
263         pr_debug("DIU pixclock freq - %u\n", pixclock);
264
265         temp = pixclock * 5 / 100;
266         pr_debug("deviation = %d\n", temp);
267         minpixclock = pixclock - temp;
268         maxpixclock = pixclock + temp;
269         pr_debug("DIU minpixclock - %lu\n", minpixclock);
270         pr_debug("DIU maxpixclock - %lu\n", maxpixclock);
271         pixval = speed_ccb/pixclock;
272         pr_debug("DIU pixval = %lu\n", pixval);
273
274         err = 100000000;
275         bestval = pixval;
276         pr_debug("DIU bestval = %lu\n", bestval);
277
278         bestfreq = 0;
279         for (i = -1; i <= 1; i++) {
280                 temp = speed_ccb / ((pixval+i) + 1);
281                 pr_debug("DIU test pixval i= %d, pixval=%lu, temp freq. = %u\n",
282                                                         i, pixval, temp);
283                 if ((temp < minpixclock) || (temp > maxpixclock))
284                         pr_debug("DIU exceeds monitor range (%lu to %lu)\n",
285                                 minpixclock, maxpixclock);
286                 else if (abs(temp - pixclock) < err) {
287                   pr_debug("Entered the else if block %d\n", i);
288                         err = abs(temp - pixclock);
289                         bestval = pixval+i;
290                         bestfreq = temp;
291                 }
292         }
293
294         pr_debug("DIU chose = %lx\n", bestval);
295         pr_debug("DIU error = %ld\n NomPixClk ", err);
296         pr_debug("DIU: Best Freq = %lx\n", bestfreq);
297         /* Modify PXCLK in GUTS CLKDVDR */
298         pr_debug("DIU: Current value of CLKDVDR = 0x%08x\n", (*clkdvdr));
299         temp = (*clkdvdr) & 0x2000FFFF;
300         *clkdvdr = temp;                /* turn off clock */
301         *clkdvdr = temp | 0x80000000 | (((bestval) & 0x1F) << 16);
302         pr_debug("DIU: Modified value of CLKDVDR = 0x%08x\n", (*clkdvdr));
303         iounmap(clkdvdr);
304 }
305
306 ssize_t mpc8610hpcd_show_monitor_port(int monitor_port, char *buf)
307 {
308         return snprintf(buf, PAGE_SIZE,
309                         "%c0 - DVI\n"
310                         "%c1 - Single link LVDS\n"
311                         "%c2 - Dual link LVDS\n",
312                         monitor_port == 0 ? '*' : ' ',
313                         monitor_port == 1 ? '*' : ' ',
314                         monitor_port == 2 ? '*' : ' ');
315 }
316
317 int mpc8610hpcd_set_sysfs_monitor_port(int val)
318 {
319         return val < 3 ? val : 0;
320 }
321
322 #endif
323
324 static void __init mpc86xx_hpcd_setup_arch(void)
325 {
326         struct resource r;
327         struct device_node *np;
328         unsigned char *pixis;
329
330         if (ppc_md.progress)
331                 ppc_md.progress("mpc86xx_hpcd_setup_arch()", 0);
332
333 #ifdef CONFIG_PCI
334         for_each_node_by_type(np, "pci") {
335                 if (of_device_is_compatible(np, "fsl,mpc8610-pci")
336                     || of_device_is_compatible(np, "fsl,mpc8641-pcie")) {
337                         struct resource rsrc;
338                         of_address_to_resource(np, 0, &rsrc);
339                         if ((rsrc.start & 0xfffff) == 0xa000)
340                                 fsl_add_bridge(np, 1);
341                         else
342                                 fsl_add_bridge(np, 0);
343                 }
344         }
345 #endif
346 #if defined(CONFIG_FB_FSL_DIU) || defined(CONFIG_FB_FSL_DIU_MODULE)
347         preallocate_diu_videomemory();
348         diu_ops.get_pixel_format        = mpc8610hpcd_get_pixel_format;
349         diu_ops.set_gamma_table         = mpc8610hpcd_set_gamma_table;
350         diu_ops.set_monitor_port        = mpc8610hpcd_set_monitor_port;
351         diu_ops.set_pixel_clock         = mpc8610hpcd_set_pixel_clock;
352         diu_ops.show_monitor_port       = mpc8610hpcd_show_monitor_port;
353         diu_ops.set_sysfs_monitor_port  = mpc8610hpcd_set_sysfs_monitor_port;
354 #endif
355
356         np = of_find_compatible_node(NULL, NULL, "fsl,fpga-pixis");
357         if (np) {
358                 of_address_to_resource(np, 0, &r);
359                 of_node_put(np);
360                 pixis = ioremap(r.start, 32);
361                 if (!pixis) {
362                         printk(KERN_ERR "Err: can't map FPGA cfg register!\n");
363                         return;
364                 }
365                 pixis_bdcfg0 = pixis + 8;
366                 pixis_arch = pixis + 1;
367         } else
368                 printk(KERN_ERR "Err: "
369                                 "can't find device node 'fsl,fpga-pixis'\n");
370
371         printk("MPC86xx HPCD board from Freescale Semiconductor\n");
372 }
373
374 /*
375  * Called very early, device-tree isn't unflattened
376  */
377 static int __init mpc86xx_hpcd_probe(void)
378 {
379         unsigned long root = of_get_flat_dt_root();
380
381         if (of_flat_dt_is_compatible(root, "fsl,MPC8610HPCD"))
382                 return 1;       /* Looks good */
383
384         return 0;
385 }
386
387 static long __init mpc86xx_time_init(void)
388 {
389         unsigned int temp;
390
391         /* Set the time base to zero */
392         mtspr(SPRN_TBWL, 0);
393         mtspr(SPRN_TBWU, 0);
394
395         temp = mfspr(SPRN_HID0);
396         temp |= HID0_TBEN;
397         mtspr(SPRN_HID0, temp);
398         asm volatile("isync");
399
400         return 0;
401 }
402
403 define_machine(mpc86xx_hpcd) {
404         .name                   = "MPC86xx HPCD",
405         .probe                  = mpc86xx_hpcd_probe,
406         .setup_arch             = mpc86xx_hpcd_setup_arch,
407         .init_IRQ               = mpc86xx_hpcd_init_irq,
408         .get_irq                = mpic_get_irq,
409         .restart                = fsl_rstcr_restart,
410         .time_init              = mpc86xx_time_init,
411         .calibrate_decr         = generic_calibrate_decr,
412         .progress               = udbg_progress,
413         .pcibios_fixup_bus      = fsl_pcibios_fixup_bus,
414 };