2 * linux/arch/arm/mach-realview/core.c
4 * Copyright (C) 1999 - 2003 ARM Limited
5 * Copyright (C) 2000 Deep Blue Solutions Ltd
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.
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.
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
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/clocksource.h>
29 #include <linux/clockchips.h>
31 #include <asm/system.h>
32 #include <mach/hardware.h>
36 #include <asm/hardware/arm_timer.h>
37 #include <asm/hardware/icst307.h>
39 #include <asm/mach/arch.h>
40 #include <asm/mach/flash.h>
41 #include <asm/mach/irq.h>
42 #include <asm/mach/map.h>
43 #include <asm/mach/mmc.h>
45 #include <asm/hardware/gic.h>
50 #define REALVIEW_REFCOUNTER (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_24MHz_OFFSET)
52 /* used by entry-macro.S */
53 void __iomem *gic_cpu_base_addr;
56 * This is the RealView sched_clock implementation. This has
57 * a resolution of 41.7ns, and a maximum value of about 179s.
59 unsigned long long sched_clock(void)
63 v = (unsigned long long)readl(REALVIEW_REFCOUNTER) * 125;
70 #define REALVIEW_FLASHCTRL (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_FLASH_OFFSET)
72 static int realview_flash_init(void)
76 val = __raw_readl(REALVIEW_FLASHCTRL);
77 val &= ~REALVIEW_FLASHPROG_FLVPPEN;
78 __raw_writel(val, REALVIEW_FLASHCTRL);
83 static void realview_flash_exit(void)
87 val = __raw_readl(REALVIEW_FLASHCTRL);
88 val &= ~REALVIEW_FLASHPROG_FLVPPEN;
89 __raw_writel(val, REALVIEW_FLASHCTRL);
92 static void realview_flash_set_vpp(int on)
96 val = __raw_readl(REALVIEW_FLASHCTRL);
98 val |= REALVIEW_FLASHPROG_FLVPPEN;
100 val &= ~REALVIEW_FLASHPROG_FLVPPEN;
101 __raw_writel(val, REALVIEW_FLASHCTRL);
104 static struct flash_platform_data realview_flash_data = {
105 .map_name = "cfi_probe",
107 .init = realview_flash_init,
108 .exit = realview_flash_exit,
109 .set_vpp = realview_flash_set_vpp,
112 struct platform_device realview_flash_device = {
116 .platform_data = &realview_flash_data,
120 int realview_flash_register(struct resource *res, u32 num)
122 realview_flash_device.resource = res;
123 realview_flash_device.num_resources = num;
124 return platform_device_register(&realview_flash_device);
127 static struct resource realview_i2c_resource = {
128 .start = REALVIEW_I2C_BASE,
129 .end = REALVIEW_I2C_BASE + SZ_4K - 1,
130 .flags = IORESOURCE_MEM,
133 struct platform_device realview_i2c_device = {
134 .name = "versatile-i2c",
137 .resource = &realview_i2c_resource,
140 #define REALVIEW_SYSMCI (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_MCI_OFFSET)
142 static unsigned int realview_mmc_status(struct device *dev)
144 struct amba_device *adev = container_of(dev, struct amba_device, dev);
147 if (adev->res.start == REALVIEW_MMCI0_BASE)
152 return readl(REALVIEW_SYSMCI) & mask;
155 struct mmc_platform_data realview_mmc0_plat_data = {
156 .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
157 .status = realview_mmc_status,
160 struct mmc_platform_data realview_mmc1_plat_data = {
161 .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
162 .status = realview_mmc_status,
168 static const struct icst307_params realview_oscvco_params = {
177 static void realview_oscvco_set(struct clk *clk, struct icst307_vco vco)
179 void __iomem *sys_lock = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LOCK_OFFSET;
180 void __iomem *sys_osc = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_OSC4_OFFSET;
183 val = readl(sys_osc) & ~0x7ffff;
184 val |= vco.v | (vco.r << 9) | (vco.s << 16);
186 writel(0xa05f, sys_lock);
187 writel(val, sys_osc);
191 struct clk realview_clcd_clk = {
193 .params = &realview_oscvco_params,
194 .setvco = realview_oscvco_set,
200 #define SYS_CLCD_NLCDIOON (1 << 2)
201 #define SYS_CLCD_VDDPOSSWITCH (1 << 3)
202 #define SYS_CLCD_PWR3V5SWITCH (1 << 4)
203 #define SYS_CLCD_ID_MASK (0x1f << 8)
204 #define SYS_CLCD_ID_SANYO_3_8 (0x00 << 8)
205 #define SYS_CLCD_ID_UNKNOWN_8_4 (0x01 << 8)
206 #define SYS_CLCD_ID_EPSON_2_2 (0x02 << 8)
207 #define SYS_CLCD_ID_SANYO_2_5 (0x07 << 8)
208 #define SYS_CLCD_ID_VGA (0x1f << 8)
210 static struct clcd_panel vga = {
224 .vmode = FB_VMODE_NONINTERLACED,
228 .tim2 = TIM2_BCD | TIM2_IPC,
229 .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
233 static struct clcd_panel sanyo_3_8_in = {
235 .name = "Sanyo QVGA",
247 .vmode = FB_VMODE_NONINTERLACED,
252 .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
256 static struct clcd_panel sanyo_2_5_in = {
258 .name = "Sanyo QVGA Portrait",
269 .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
270 .vmode = FB_VMODE_NONINTERLACED,
274 .tim2 = TIM2_IVS | TIM2_IHS | TIM2_IPC,
275 .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
279 static struct clcd_panel epson_2_2_in = {
281 .name = "Epson QCIF",
293 .vmode = FB_VMODE_NONINTERLACED,
297 .tim2 = TIM2_BCD | TIM2_IPC,
298 .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
303 * Detect which LCD panel is connected, and return the appropriate
304 * clcd_panel structure. Note: we do not have any information on
305 * the required timings for the 8.4in panel, so we presently assume
308 static struct clcd_panel *realview_clcd_panel(void)
310 void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
311 struct clcd_panel *panel = &vga;
314 val = readl(sys_clcd) & SYS_CLCD_ID_MASK;
315 if (val == SYS_CLCD_ID_SANYO_3_8)
316 panel = &sanyo_3_8_in;
317 else if (val == SYS_CLCD_ID_SANYO_2_5)
318 panel = &sanyo_2_5_in;
319 else if (val == SYS_CLCD_ID_EPSON_2_2)
320 panel = &epson_2_2_in;
321 else if (val == SYS_CLCD_ID_VGA)
324 printk(KERN_ERR "CLCD: unknown LCD panel ID 0x%08x, using VGA\n",
333 * Disable all display connectors on the interface module.
335 static void realview_clcd_disable(struct clcd_fb *fb)
337 void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
340 val = readl(sys_clcd);
341 val &= ~SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
342 writel(val, sys_clcd);
346 * Enable the relevant connector on the interface module.
348 static void realview_clcd_enable(struct clcd_fb *fb)
350 void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
356 val = readl(sys_clcd);
357 val |= SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
358 writel(val, sys_clcd);
361 static unsigned long framesize = SZ_1M;
363 static int realview_clcd_setup(struct clcd_fb *fb)
367 fb->panel = realview_clcd_panel();
369 fb->fb.screen_base = dma_alloc_writecombine(&fb->dev->dev, framesize,
371 if (!fb->fb.screen_base) {
372 printk(KERN_ERR "CLCD: unable to map framebuffer\n");
376 fb->fb.fix.smem_start = dma;
377 fb->fb.fix.smem_len = framesize;
382 static int realview_clcd_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
384 return dma_mmap_writecombine(&fb->dev->dev, vma,
386 fb->fb.fix.smem_start,
387 fb->fb.fix.smem_len);
390 static void realview_clcd_remove(struct clcd_fb *fb)
392 dma_free_writecombine(&fb->dev->dev, fb->fb.fix.smem_len,
393 fb->fb.screen_base, fb->fb.fix.smem_start);
396 struct clcd_board clcd_plat_data = {
398 .check = clcdfb_check,
399 .decode = clcdfb_decode,
400 .disable = realview_clcd_disable,
401 .enable = realview_clcd_enable,
402 .setup = realview_clcd_setup,
403 .mmap = realview_clcd_mmap,
404 .remove = realview_clcd_remove,
408 #define VA_LEDS_BASE (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LED_OFFSET)
410 void realview_leds_event(led_event_t ledevt)
415 local_irq_save(flags);
416 val = readl(VA_LEDS_BASE);
420 val = val & ~REALVIEW_SYS_LED0;
424 val = val | REALVIEW_SYS_LED0;
428 val = val ^ REALVIEW_SYS_LED1;
439 writel(val, VA_LEDS_BASE);
440 local_irq_restore(flags);
442 #endif /* CONFIG_LEDS */
445 * Where is the timer (VA)?
447 void __iomem *timer0_va_base;
448 void __iomem *timer1_va_base;
449 void __iomem *timer2_va_base;
450 void __iomem *timer3_va_base;
453 * How long is the timer interval?
455 #define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_10)
456 #if TIMER_INTERVAL >= 0x100000
457 #define TIMER_RELOAD (TIMER_INTERVAL >> 8)
458 #define TIMER_DIVISOR (TIMER_CTRL_DIV256)
459 #define TICKS2USECS(x) (256 * (x) / TICKS_PER_uSEC)
460 #elif TIMER_INTERVAL >= 0x10000
461 #define TIMER_RELOAD (TIMER_INTERVAL >> 4) /* Divide by 16 */
462 #define TIMER_DIVISOR (TIMER_CTRL_DIV16)
463 #define TICKS2USECS(x) (16 * (x) / TICKS_PER_uSEC)
465 #define TIMER_RELOAD (TIMER_INTERVAL)
466 #define TIMER_DIVISOR (TIMER_CTRL_DIV1)
467 #define TICKS2USECS(x) ((x) / TICKS_PER_uSEC)
470 static void timer_set_mode(enum clock_event_mode mode,
471 struct clock_event_device *clk)
476 case CLOCK_EVT_MODE_PERIODIC:
477 writel(TIMER_RELOAD, timer0_va_base + TIMER_LOAD);
479 ctrl = TIMER_CTRL_PERIODIC;
480 ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_IE | TIMER_CTRL_ENABLE;
482 case CLOCK_EVT_MODE_ONESHOT:
483 /* period set, and timer enabled in 'next_event' hook */
484 ctrl = TIMER_CTRL_ONESHOT;
485 ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_IE;
487 case CLOCK_EVT_MODE_UNUSED:
488 case CLOCK_EVT_MODE_SHUTDOWN:
493 writel(ctrl, timer0_va_base + TIMER_CTRL);
496 static int timer_set_next_event(unsigned long evt,
497 struct clock_event_device *unused)
499 unsigned long ctrl = readl(timer0_va_base + TIMER_CTRL);
501 writel(evt, timer0_va_base + TIMER_LOAD);
502 writel(ctrl | TIMER_CTRL_ENABLE, timer0_va_base + TIMER_CTRL);
507 static struct clock_event_device timer0_clockevent = {
510 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
511 .set_mode = timer_set_mode,
512 .set_next_event = timer_set_next_event,
514 .cpumask = CPU_MASK_ALL,
517 static void __init realview_clockevents_init(unsigned int timer_irq)
519 timer0_clockevent.irq = timer_irq;
520 timer0_clockevent.mult =
521 div_sc(1000000, NSEC_PER_SEC, timer0_clockevent.shift);
522 timer0_clockevent.max_delta_ns =
523 clockevent_delta2ns(0xffffffff, &timer0_clockevent);
524 timer0_clockevent.min_delta_ns =
525 clockevent_delta2ns(0xf, &timer0_clockevent);
527 clockevents_register_device(&timer0_clockevent);
531 * IRQ handler for the timer
533 static irqreturn_t realview_timer_interrupt(int irq, void *dev_id)
535 struct clock_event_device *evt = &timer0_clockevent;
537 /* clear the interrupt */
538 writel(1, timer0_va_base + TIMER_INTCLR);
540 evt->event_handler(evt);
545 static struct irqaction realview_timer_irq = {
546 .name = "RealView Timer Tick",
547 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
548 .handler = realview_timer_interrupt,
551 static cycle_t realview_get_cycles(void)
553 return ~readl(timer3_va_base + TIMER_VALUE);
556 static struct clocksource clocksource_realview = {
559 .read = realview_get_cycles,
560 .mask = CLOCKSOURCE_MASK(32),
562 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
565 static void __init realview_clocksource_init(void)
567 /* setup timer 0 as free-running clocksource */
568 writel(0, timer3_va_base + TIMER_CTRL);
569 writel(0xffffffff, timer3_va_base + TIMER_LOAD);
570 writel(0xffffffff, timer3_va_base + TIMER_VALUE);
571 writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
572 timer3_va_base + TIMER_CTRL);
574 clocksource_realview.mult =
575 clocksource_khz2mult(1000, clocksource_realview.shift);
576 clocksource_register(&clocksource_realview);
580 * Set up the clock source and clock events devices
582 void __init realview_timer_init(unsigned int timer_irq)
586 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
588 * The dummy clock device has to be registered before the main device
589 * so that the latter will broadcast the clock events
591 local_timer_setup(smp_processor_id());
595 * set clock frequency:
596 * REALVIEW_REFCLK is 32KHz
597 * REALVIEW_TIMCLK is 1MHz
599 val = readl(__io_address(REALVIEW_SCTL_BASE));
600 writel((REALVIEW_TIMCLK << REALVIEW_TIMER1_EnSel) |
601 (REALVIEW_TIMCLK << REALVIEW_TIMER2_EnSel) |
602 (REALVIEW_TIMCLK << REALVIEW_TIMER3_EnSel) |
603 (REALVIEW_TIMCLK << REALVIEW_TIMER4_EnSel) | val,
604 __io_address(REALVIEW_SCTL_BASE));
607 * Initialise to a known state (all timers off)
609 writel(0, timer0_va_base + TIMER_CTRL);
610 writel(0, timer1_va_base + TIMER_CTRL);
611 writel(0, timer2_va_base + TIMER_CTRL);
612 writel(0, timer3_va_base + TIMER_CTRL);
615 * Make irqs happen for the system timer
617 setup_irq(timer_irq, &realview_timer_irq);
619 realview_clocksource_init();
620 realview_clockevents_init(timer_irq);