Merge branch 'devel-pmu' into devel
[safe/jmp/linux-2.6] / arch / arm / mach-realview / core.c
1 /*
2  *  linux/arch/arm/mach-realview/core.c
3  *
4  *  Copyright (C) 1999 - 2003 ARM Limited
5  *  Copyright (C) 2000 Deep Blue Solutions Ltd
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/sysdev.h>
25 #include <linux/interrupt.h>
26 #include <linux/amba/bus.h>
27 #include <linux/amba/clcd.h>
28 #include <linux/io.h>
29 #include <linux/smsc911x.h>
30 #include <linux/ata_platform.h>
31 #include <linux/amba/mmci.h>
32
33 #include <asm/clkdev.h>
34 #include <asm/system.h>
35 #include <mach/hardware.h>
36 #include <asm/irq.h>
37 #include <asm/leds.h>
38 #include <asm/mach-types.h>
39 #include <asm/hardware/arm_timer.h>
40 #include <asm/hardware/icst.h>
41
42 #include <asm/mach/arch.h>
43 #include <asm/mach/flash.h>
44 #include <asm/mach/irq.h>
45 #include <asm/mach/map.h>
46
47 #include <asm/hardware/gic.h>
48
49 #include <mach/clkdev.h>
50 #include <mach/platform.h>
51 #include <mach/irqs.h>
52 #include <plat/timer-sp.h>
53
54 #include "core.h"
55
56 /* used by entry-macro.S and platsmp.c */
57 void __iomem *gic_cpu_base_addr;
58
59 #ifdef CONFIG_ZONE_DMA
60 /*
61  * Adjust the zones if there are restrictions for DMA access.
62  */
63 void __init realview_adjust_zones(int node, unsigned long *size,
64                                   unsigned long *hole)
65 {
66         unsigned long dma_size = SZ_256M >> PAGE_SHIFT;
67
68         if (!machine_is_realview_pbx() || node || (size[0] <= dma_size))
69                 return;
70
71         size[ZONE_NORMAL] = size[0] - dma_size;
72         size[ZONE_DMA] = dma_size;
73         hole[ZONE_NORMAL] = hole[0];
74         hole[ZONE_DMA] = 0;
75 }
76 #endif
77
78
79 #define REALVIEW_FLASHCTRL    (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_FLASH_OFFSET)
80
81 static int realview_flash_init(void)
82 {
83         u32 val;
84
85         val = __raw_readl(REALVIEW_FLASHCTRL);
86         val &= ~REALVIEW_FLASHPROG_FLVPPEN;
87         __raw_writel(val, REALVIEW_FLASHCTRL);
88
89         return 0;
90 }
91
92 static void realview_flash_exit(void)
93 {
94         u32 val;
95
96         val = __raw_readl(REALVIEW_FLASHCTRL);
97         val &= ~REALVIEW_FLASHPROG_FLVPPEN;
98         __raw_writel(val, REALVIEW_FLASHCTRL);
99 }
100
101 static void realview_flash_set_vpp(int on)
102 {
103         u32 val;
104
105         val = __raw_readl(REALVIEW_FLASHCTRL);
106         if (on)
107                 val |= REALVIEW_FLASHPROG_FLVPPEN;
108         else
109                 val &= ~REALVIEW_FLASHPROG_FLVPPEN;
110         __raw_writel(val, REALVIEW_FLASHCTRL);
111 }
112
113 static struct flash_platform_data realview_flash_data = {
114         .map_name               = "cfi_probe",
115         .width                  = 4,
116         .init                   = realview_flash_init,
117         .exit                   = realview_flash_exit,
118         .set_vpp                = realview_flash_set_vpp,
119 };
120
121 struct platform_device realview_flash_device = {
122         .name                   = "armflash",
123         .id                     = 0,
124         .dev                    = {
125                 .platform_data  = &realview_flash_data,
126         },
127 };
128
129 int realview_flash_register(struct resource *res, u32 num)
130 {
131         realview_flash_device.resource = res;
132         realview_flash_device.num_resources = num;
133         return platform_device_register(&realview_flash_device);
134 }
135
136 static struct smsc911x_platform_config smsc911x_config = {
137         .flags          = SMSC911X_USE_32BIT,
138         .irq_polarity   = SMSC911X_IRQ_POLARITY_ACTIVE_HIGH,
139         .irq_type       = SMSC911X_IRQ_TYPE_PUSH_PULL,
140         .phy_interface  = PHY_INTERFACE_MODE_MII,
141 };
142
143 static struct platform_device realview_eth_device = {
144         .name           = "smsc911x",
145         .id             = 0,
146         .num_resources  = 2,
147 };
148
149 int realview_eth_register(const char *name, struct resource *res)
150 {
151         if (name)
152                 realview_eth_device.name = name;
153         realview_eth_device.resource = res;
154         if (strcmp(realview_eth_device.name, "smsc911x") == 0)
155                 realview_eth_device.dev.platform_data = &smsc911x_config;
156
157         return platform_device_register(&realview_eth_device);
158 }
159
160 struct platform_device realview_usb_device = {
161         .name                   = "isp1760",
162         .num_resources          = 2,
163 };
164
165 int realview_usb_register(struct resource *res)
166 {
167         realview_usb_device.resource = res;
168         return platform_device_register(&realview_usb_device);
169 }
170
171 static struct pata_platform_info pata_platform_data = {
172         .ioport_shift           = 1,
173 };
174
175 static struct resource pata_resources[] = {
176         [0] = {
177                 .start          = REALVIEW_CF_BASE,
178                 .end            = REALVIEW_CF_BASE + 0xff,
179                 .flags          = IORESOURCE_MEM,
180         },
181         [1] = {
182                 .start          = REALVIEW_CF_BASE + 0x100,
183                 .end            = REALVIEW_CF_BASE + SZ_4K - 1,
184                 .flags          = IORESOURCE_MEM,
185         },
186 };
187
188 struct platform_device realview_cf_device = {
189         .name                   = "pata_platform",
190         .id                     = -1,
191         .num_resources          = ARRAY_SIZE(pata_resources),
192         .resource               = pata_resources,
193         .dev                    = {
194                 .platform_data  = &pata_platform_data,
195         },
196 };
197
198 static struct resource realview_i2c_resource = {
199         .start          = REALVIEW_I2C_BASE,
200         .end            = REALVIEW_I2C_BASE + SZ_4K - 1,
201         .flags          = IORESOURCE_MEM,
202 };
203
204 struct platform_device realview_i2c_device = {
205         .name           = "versatile-i2c",
206         .id             = 0,
207         .num_resources  = 1,
208         .resource       = &realview_i2c_resource,
209 };
210
211 static struct i2c_board_info realview_i2c_board_info[] = {
212         {
213                 I2C_BOARD_INFO("ds1338", 0xd0 >> 1),
214         },
215 };
216
217 static int __init realview_i2c_init(void)
218 {
219         return i2c_register_board_info(0, realview_i2c_board_info,
220                                        ARRAY_SIZE(realview_i2c_board_info));
221 }
222 arch_initcall(realview_i2c_init);
223
224 #define REALVIEW_SYSMCI (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_MCI_OFFSET)
225
226 /*
227  * This is only used if GPIOLIB support is disabled
228  */
229 static unsigned int realview_mmc_status(struct device *dev)
230 {
231         struct amba_device *adev = container_of(dev, struct amba_device, dev);
232         u32 mask;
233
234         if (adev->res.start == REALVIEW_MMCI0_BASE)
235                 mask = 1;
236         else
237                 mask = 2;
238
239         return readl(REALVIEW_SYSMCI) & mask;
240 }
241
242 struct mmci_platform_data realview_mmc0_plat_data = {
243         .ocr_mask       = MMC_VDD_32_33|MMC_VDD_33_34,
244         .status         = realview_mmc_status,
245         .gpio_wp        = 17,
246         .gpio_cd        = 16,
247 };
248
249 struct mmci_platform_data realview_mmc1_plat_data = {
250         .ocr_mask       = MMC_VDD_32_33|MMC_VDD_33_34,
251         .status         = realview_mmc_status,
252         .gpio_wp        = 19,
253         .gpio_cd        = 18,
254 };
255
256 /*
257  * Clock handling
258  */
259 static const struct icst_params realview_oscvco_params = {
260         .ref            = 24000000,
261         .vco_max        = ICST307_VCO_MAX,
262         .vco_min        = ICST307_VCO_MIN,
263         .vd_min         = 4 + 8,
264         .vd_max         = 511 + 8,
265         .rd_min         = 1 + 2,
266         .rd_max         = 127 + 2,
267         .s2div          = icst307_s2div,
268         .idx2s          = icst307_idx2s,
269 };
270
271 static void realview_oscvco_set(struct clk *clk, struct icst_vco vco)
272 {
273         void __iomem *sys_lock = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LOCK_OFFSET;
274         u32 val;
275
276         val = readl(clk->vcoreg) & ~0x7ffff;
277         val |= vco.v | (vco.r << 9) | (vco.s << 16);
278
279         writel(0xa05f, sys_lock);
280         writel(val, clk->vcoreg);
281         writel(0, sys_lock);
282 }
283
284 static const struct clk_ops oscvco_clk_ops = {
285         .round  = icst_clk_round,
286         .set    = icst_clk_set,
287         .setvco = realview_oscvco_set,
288 };
289
290 static struct clk oscvco_clk = {
291         .ops    = &oscvco_clk_ops,
292         .params = &realview_oscvco_params,
293 };
294
295 /*
296  * These are fixed clocks.
297  */
298 static struct clk ref24_clk = {
299         .rate   = 24000000,
300 };
301
302 static struct clk_lookup lookups[] = {
303         {       /* UART0 */
304                 .dev_id         = "dev:uart0",
305                 .clk            = &ref24_clk,
306         }, {    /* UART1 */
307                 .dev_id         = "dev:uart1",
308                 .clk            = &ref24_clk,
309         }, {    /* UART2 */
310                 .dev_id         = "dev:uart2",
311                 .clk            = &ref24_clk,
312         }, {    /* UART3 */
313                 .dev_id         = "fpga:uart3",
314                 .clk            = &ref24_clk,
315         }, {    /* KMI0 */
316                 .dev_id         = "fpga:kmi0",
317                 .clk            = &ref24_clk,
318         }, {    /* KMI1 */
319                 .dev_id         = "fpga:kmi1",
320                 .clk            = &ref24_clk,
321         }, {    /* MMC0 */
322                 .dev_id         = "fpga:mmc0",
323                 .clk            = &ref24_clk,
324         }, {    /* EB:CLCD */
325                 .dev_id         = "dev:clcd",
326                 .clk            = &oscvco_clk,
327         }, {    /* PB:CLCD */
328                 .dev_id         = "issp:clcd",
329                 .clk            = &oscvco_clk,
330         }
331 };
332
333 static int __init clk_init(void)
334 {
335         if (machine_is_realview_pb1176())
336                 oscvco_clk.vcoreg = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_OSC0_OFFSET;
337         else
338                 oscvco_clk.vcoreg = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_OSC4_OFFSET;
339
340         clkdev_add_table(lookups, ARRAY_SIZE(lookups));
341
342         return 0;
343 }
344 arch_initcall(clk_init);
345
346 /*
347  * CLCD support.
348  */
349 #define SYS_CLCD_NLCDIOON       (1 << 2)
350 #define SYS_CLCD_VDDPOSSWITCH   (1 << 3)
351 #define SYS_CLCD_PWR3V5SWITCH   (1 << 4)
352 #define SYS_CLCD_ID_MASK        (0x1f << 8)
353 #define SYS_CLCD_ID_SANYO_3_8   (0x00 << 8)
354 #define SYS_CLCD_ID_UNKNOWN_8_4 (0x01 << 8)
355 #define SYS_CLCD_ID_EPSON_2_2   (0x02 << 8)
356 #define SYS_CLCD_ID_SANYO_2_5   (0x07 << 8)
357 #define SYS_CLCD_ID_VGA         (0x1f << 8)
358
359 static struct clcd_panel vga = {
360         .mode           = {
361                 .name           = "VGA",
362                 .refresh        = 60,
363                 .xres           = 640,
364                 .yres           = 480,
365                 .pixclock       = 39721,
366                 .left_margin    = 40,
367                 .right_margin   = 24,
368                 .upper_margin   = 32,
369                 .lower_margin   = 11,
370                 .hsync_len      = 96,
371                 .vsync_len      = 2,
372                 .sync           = 0,
373                 .vmode          = FB_VMODE_NONINTERLACED,
374         },
375         .width          = -1,
376         .height         = -1,
377         .tim2           = TIM2_BCD | TIM2_IPC,
378         .cntl           = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
379         .bpp            = 16,
380 };
381
382 static struct clcd_panel xvga = {
383         .mode           = {
384                 .name           = "XVGA",
385                 .refresh        = 60,
386                 .xres           = 1024,
387                 .yres           = 768,
388                 .pixclock       = 15748,
389                 .left_margin    = 152,
390                 .right_margin   = 48,
391                 .upper_margin   = 23,
392                 .lower_margin   = 3,
393                 .hsync_len      = 104,
394                 .vsync_len      = 4,
395                 .sync           = 0,
396                 .vmode          = FB_VMODE_NONINTERLACED,
397         },
398         .width          = -1,
399         .height         = -1,
400         .tim2           = TIM2_BCD | TIM2_IPC,
401         .cntl           = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
402         .bpp            = 16,
403 };
404
405 static struct clcd_panel sanyo_3_8_in = {
406         .mode           = {
407                 .name           = "Sanyo QVGA",
408                 .refresh        = 116,
409                 .xres           = 320,
410                 .yres           = 240,
411                 .pixclock       = 100000,
412                 .left_margin    = 6,
413                 .right_margin   = 6,
414                 .upper_margin   = 5,
415                 .lower_margin   = 5,
416                 .hsync_len      = 6,
417                 .vsync_len      = 6,
418                 .sync           = 0,
419                 .vmode          = FB_VMODE_NONINTERLACED,
420         },
421         .width          = -1,
422         .height         = -1,
423         .tim2           = TIM2_BCD,
424         .cntl           = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
425         .bpp            = 16,
426 };
427
428 static struct clcd_panel sanyo_2_5_in = {
429         .mode           = {
430                 .name           = "Sanyo QVGA Portrait",
431                 .refresh        = 116,
432                 .xres           = 240,
433                 .yres           = 320,
434                 .pixclock       = 100000,
435                 .left_margin    = 20,
436                 .right_margin   = 10,
437                 .upper_margin   = 2,
438                 .lower_margin   = 2,
439                 .hsync_len      = 10,
440                 .vsync_len      = 2,
441                 .sync           = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
442                 .vmode          = FB_VMODE_NONINTERLACED,
443         },
444         .width          = -1,
445         .height         = -1,
446         .tim2           = TIM2_IVS | TIM2_IHS | TIM2_IPC,
447         .cntl           = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
448         .bpp            = 16,
449 };
450
451 static struct clcd_panel epson_2_2_in = {
452         .mode           = {
453                 .name           = "Epson QCIF",
454                 .refresh        = 390,
455                 .xres           = 176,
456                 .yres           = 220,
457                 .pixclock       = 62500,
458                 .left_margin    = 3,
459                 .right_margin   = 2,
460                 .upper_margin   = 1,
461                 .lower_margin   = 0,
462                 .hsync_len      = 3,
463                 .vsync_len      = 2,
464                 .sync           = 0,
465                 .vmode          = FB_VMODE_NONINTERLACED,
466         },
467         .width          = -1,
468         .height         = -1,
469         .tim2           = TIM2_BCD | TIM2_IPC,
470         .cntl           = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
471         .bpp            = 16,
472 };
473
474 /*
475  * Detect which LCD panel is connected, and return the appropriate
476  * clcd_panel structure.  Note: we do not have any information on
477  * the required timings for the 8.4in panel, so we presently assume
478  * VGA timings.
479  */
480 static struct clcd_panel *realview_clcd_panel(void)
481 {
482         void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
483         struct clcd_panel *vga_panel;
484         struct clcd_panel *panel;
485         u32 val;
486
487         if (machine_is_realview_eb())
488                 vga_panel = &vga;
489         else
490                 vga_panel = &xvga;
491
492         val = readl(sys_clcd) & SYS_CLCD_ID_MASK;
493         if (val == SYS_CLCD_ID_SANYO_3_8)
494                 panel = &sanyo_3_8_in;
495         else if (val == SYS_CLCD_ID_SANYO_2_5)
496                 panel = &sanyo_2_5_in;
497         else if (val == SYS_CLCD_ID_EPSON_2_2)
498                 panel = &epson_2_2_in;
499         else if (val == SYS_CLCD_ID_VGA)
500                 panel = vga_panel;
501         else {
502                 printk(KERN_ERR "CLCD: unknown LCD panel ID 0x%08x, using VGA\n",
503                         val);
504                 panel = vga_panel;
505         }
506
507         return panel;
508 }
509
510 /*
511  * Disable all display connectors on the interface module.
512  */
513 static void realview_clcd_disable(struct clcd_fb *fb)
514 {
515         void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
516         u32 val;
517
518         val = readl(sys_clcd);
519         val &= ~SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
520         writel(val, sys_clcd);
521 }
522
523 /*
524  * Enable the relevant connector on the interface module.
525  */
526 static void realview_clcd_enable(struct clcd_fb *fb)
527 {
528         void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
529         u32 val;
530
531         /*
532          * Enable the PSUs
533          */
534         val = readl(sys_clcd);
535         val |= SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
536         writel(val, sys_clcd);
537 }
538
539 static int realview_clcd_setup(struct clcd_fb *fb)
540 {
541         unsigned long framesize;
542         dma_addr_t dma;
543
544         if (machine_is_realview_eb())
545                 /* VGA, 16bpp */
546                 framesize = 640 * 480 * 2;
547         else
548                 /* XVGA, 16bpp */
549                 framesize = 1024 * 768 * 2;
550
551         fb->panel               = realview_clcd_panel();
552
553         fb->fb.screen_base = dma_alloc_writecombine(&fb->dev->dev, framesize,
554                                                     &dma, GFP_KERNEL | GFP_DMA);
555         if (!fb->fb.screen_base) {
556                 printk(KERN_ERR "CLCD: unable to map framebuffer\n");
557                 return -ENOMEM;
558         }
559
560         fb->fb.fix.smem_start   = dma;
561         fb->fb.fix.smem_len     = framesize;
562
563         return 0;
564 }
565
566 static int realview_clcd_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
567 {
568         return dma_mmap_writecombine(&fb->dev->dev, vma,
569                                      fb->fb.screen_base,
570                                      fb->fb.fix.smem_start,
571                                      fb->fb.fix.smem_len);
572 }
573
574 static void realview_clcd_remove(struct clcd_fb *fb)
575 {
576         dma_free_writecombine(&fb->dev->dev, fb->fb.fix.smem_len,
577                               fb->fb.screen_base, fb->fb.fix.smem_start);
578 }
579
580 struct clcd_board clcd_plat_data = {
581         .name           = "RealView",
582         .check          = clcdfb_check,
583         .decode         = clcdfb_decode,
584         .disable        = realview_clcd_disable,
585         .enable         = realview_clcd_enable,
586         .setup          = realview_clcd_setup,
587         .mmap           = realview_clcd_mmap,
588         .remove         = realview_clcd_remove,
589 };
590
591 #ifdef CONFIG_LEDS
592 #define VA_LEDS_BASE (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LED_OFFSET)
593
594 void realview_leds_event(led_event_t ledevt)
595 {
596         unsigned long flags;
597         u32 val;
598         u32 led = 1 << smp_processor_id();
599
600         local_irq_save(flags);
601         val = readl(VA_LEDS_BASE);
602
603         switch (ledevt) {
604         case led_idle_start:
605                 val = val & ~led;
606                 break;
607
608         case led_idle_end:
609                 val = val | led;
610                 break;
611
612         case led_timer:
613                 val = val ^ REALVIEW_SYS_LED7;
614                 break;
615
616         case led_halted:
617                 val = 0;
618                 break;
619
620         default:
621                 break;
622         }
623
624         writel(val, VA_LEDS_BASE);
625         local_irq_restore(flags);
626 }
627 #endif  /* CONFIG_LEDS */
628
629 /*
630  * Where is the timer (VA)?
631  */
632 void __iomem *timer0_va_base;
633 void __iomem *timer1_va_base;
634 void __iomem *timer2_va_base;
635 void __iomem *timer3_va_base;
636
637 /*
638  * Set up the clock source and clock events devices
639  */
640 void __init realview_timer_init(unsigned int timer_irq)
641 {
642         u32 val;
643
644         /* 
645          * set clock frequency: 
646          *      REALVIEW_REFCLK is 32KHz
647          *      REALVIEW_TIMCLK is 1MHz
648          */
649         val = readl(__io_address(REALVIEW_SCTL_BASE));
650         writel((REALVIEW_TIMCLK << REALVIEW_TIMER1_EnSel) |
651                (REALVIEW_TIMCLK << REALVIEW_TIMER2_EnSel) | 
652                (REALVIEW_TIMCLK << REALVIEW_TIMER3_EnSel) |
653                (REALVIEW_TIMCLK << REALVIEW_TIMER4_EnSel) | val,
654                __io_address(REALVIEW_SCTL_BASE));
655
656         /*
657          * Initialise to a known state (all timers off)
658          */
659         writel(0, timer0_va_base + TIMER_CTRL);
660         writel(0, timer1_va_base + TIMER_CTRL);
661         writel(0, timer2_va_base + TIMER_CTRL);
662         writel(0, timer3_va_base + TIMER_CTRL);
663
664         sp804_clocksource_init(timer3_va_base);
665         sp804_clockevents_init(timer0_va_base, timer_irq);
666 }
667
668 /*
669  * Setup the memory banks.
670  */
671 void realview_fixup(struct machine_desc *mdesc, struct tag *tags, char **from,
672                     struct meminfo *meminfo)
673 {
674         /*
675          * Most RealView platforms have 512MB contiguous RAM at 0x70000000.
676          * Half of this is mirrored at 0.
677          */
678 #ifdef CONFIG_REALVIEW_HIGH_PHYS_OFFSET
679         meminfo->bank[0].start = 0x70000000;
680         meminfo->bank[0].size = SZ_512M;
681         meminfo->nr_banks = 1;
682 #else
683         meminfo->bank[0].start = 0;
684         meminfo->bank[0].size = SZ_256M;
685         meminfo->nr_banks = 1;
686 #endif
687 }