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 <asm/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 static struct resource realview_flash_resource = {
113 .start = REALVIEW_FLASH_BASE,
114 .end = REALVIEW_FLASH_BASE + REALVIEW_FLASH_SIZE,
115 .flags = IORESOURCE_MEM,
118 struct platform_device realview_flash_device = {
122 .platform_data = &realview_flash_data,
125 .resource = &realview_flash_resource,
128 static struct resource realview_i2c_resource = {
129 .start = REALVIEW_I2C_BASE,
130 .end = REALVIEW_I2C_BASE + SZ_4K - 1,
131 .flags = IORESOURCE_MEM,
134 struct platform_device realview_i2c_device = {
135 .name = "versatile-i2c",
138 .resource = &realview_i2c_resource,
141 #define REALVIEW_SYSMCI (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_MCI_OFFSET)
143 static unsigned int realview_mmc_status(struct device *dev)
145 struct amba_device *adev = container_of(dev, struct amba_device, dev);
148 if (adev->res.start == REALVIEW_MMCI0_BASE)
153 return readl(REALVIEW_SYSMCI) & mask;
156 struct mmc_platform_data realview_mmc0_plat_data = {
157 .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
158 .status = realview_mmc_status,
161 struct mmc_platform_data realview_mmc1_plat_data = {
162 .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
163 .status = realview_mmc_status,
169 static const struct icst307_params realview_oscvco_params = {
178 static void realview_oscvco_set(struct clk *clk, struct icst307_vco vco)
180 void __iomem *sys_lock = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LOCK_OFFSET;
181 void __iomem *sys_osc = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_OSC4_OFFSET;
184 val = readl(sys_osc) & ~0x7ffff;
185 val |= vco.v | (vco.r << 9) | (vco.s << 16);
187 writel(0xa05f, sys_lock);
188 writel(val, sys_osc);
192 struct clk realview_clcd_clk = {
194 .params = &realview_oscvco_params,
195 .setvco = realview_oscvco_set,
201 #define SYS_CLCD_NLCDIOON (1 << 2)
202 #define SYS_CLCD_VDDPOSSWITCH (1 << 3)
203 #define SYS_CLCD_PWR3V5SWITCH (1 << 4)
204 #define SYS_CLCD_ID_MASK (0x1f << 8)
205 #define SYS_CLCD_ID_SANYO_3_8 (0x00 << 8)
206 #define SYS_CLCD_ID_UNKNOWN_8_4 (0x01 << 8)
207 #define SYS_CLCD_ID_EPSON_2_2 (0x02 << 8)
208 #define SYS_CLCD_ID_SANYO_2_5 (0x07 << 8)
209 #define SYS_CLCD_ID_VGA (0x1f << 8)
211 static struct clcd_panel vga = {
225 .vmode = FB_VMODE_NONINTERLACED,
229 .tim2 = TIM2_BCD | TIM2_IPC,
230 .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
234 static struct clcd_panel sanyo_3_8_in = {
236 .name = "Sanyo QVGA",
248 .vmode = FB_VMODE_NONINTERLACED,
253 .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
257 static struct clcd_panel sanyo_2_5_in = {
259 .name = "Sanyo QVGA Portrait",
270 .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
271 .vmode = FB_VMODE_NONINTERLACED,
275 .tim2 = TIM2_IVS | TIM2_IHS | TIM2_IPC,
276 .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
280 static struct clcd_panel epson_2_2_in = {
282 .name = "Epson QCIF",
294 .vmode = FB_VMODE_NONINTERLACED,
298 .tim2 = TIM2_BCD | TIM2_IPC,
299 .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
304 * Detect which LCD panel is connected, and return the appropriate
305 * clcd_panel structure. Note: we do not have any information on
306 * the required timings for the 8.4in panel, so we presently assume
309 static struct clcd_panel *realview_clcd_panel(void)
311 void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
312 struct clcd_panel *panel = &vga;
315 val = readl(sys_clcd) & SYS_CLCD_ID_MASK;
316 if (val == SYS_CLCD_ID_SANYO_3_8)
317 panel = &sanyo_3_8_in;
318 else if (val == SYS_CLCD_ID_SANYO_2_5)
319 panel = &sanyo_2_5_in;
320 else if (val == SYS_CLCD_ID_EPSON_2_2)
321 panel = &epson_2_2_in;
322 else if (val == SYS_CLCD_ID_VGA)
325 printk(KERN_ERR "CLCD: unknown LCD panel ID 0x%08x, using VGA\n",
334 * Disable all display connectors on the interface module.
336 static void realview_clcd_disable(struct clcd_fb *fb)
338 void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
341 val = readl(sys_clcd);
342 val &= ~SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
343 writel(val, sys_clcd);
347 * Enable the relevant connector on the interface module.
349 static void realview_clcd_enable(struct clcd_fb *fb)
351 void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
357 val = readl(sys_clcd);
358 val |= SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
359 writel(val, sys_clcd);
362 static unsigned long framesize = SZ_1M;
364 static int realview_clcd_setup(struct clcd_fb *fb)
368 fb->panel = realview_clcd_panel();
370 fb->fb.screen_base = dma_alloc_writecombine(&fb->dev->dev, framesize,
372 if (!fb->fb.screen_base) {
373 printk(KERN_ERR "CLCD: unable to map framebuffer\n");
377 fb->fb.fix.smem_start = dma;
378 fb->fb.fix.smem_len = framesize;
383 static int realview_clcd_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
385 return dma_mmap_writecombine(&fb->dev->dev, vma,
387 fb->fb.fix.smem_start,
388 fb->fb.fix.smem_len);
391 static void realview_clcd_remove(struct clcd_fb *fb)
393 dma_free_writecombine(&fb->dev->dev, fb->fb.fix.smem_len,
394 fb->fb.screen_base, fb->fb.fix.smem_start);
397 struct clcd_board clcd_plat_data = {
399 .check = clcdfb_check,
400 .decode = clcdfb_decode,
401 .disable = realview_clcd_disable,
402 .enable = realview_clcd_enable,
403 .setup = realview_clcd_setup,
404 .mmap = realview_clcd_mmap,
405 .remove = realview_clcd_remove,
409 #define VA_LEDS_BASE (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LED_OFFSET)
411 void realview_leds_event(led_event_t ledevt)
416 local_irq_save(flags);
417 val = readl(VA_LEDS_BASE);
421 val = val & ~REALVIEW_SYS_LED0;
425 val = val | REALVIEW_SYS_LED0;
429 val = val ^ REALVIEW_SYS_LED1;
440 writel(val, VA_LEDS_BASE);
441 local_irq_restore(flags);
443 #endif /* CONFIG_LEDS */
446 * Where is the timer (VA)?
448 #define TIMER0_VA_BASE __io_address(REALVIEW_TIMER0_1_BASE)
449 #define TIMER1_VA_BASE (__io_address(REALVIEW_TIMER0_1_BASE) + 0x20)
450 #define TIMER2_VA_BASE __io_address(REALVIEW_TIMER2_3_BASE)
451 #define TIMER3_VA_BASE (__io_address(REALVIEW_TIMER2_3_BASE) + 0x20)
454 * How long is the timer interval?
456 #define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_10)
457 #if TIMER_INTERVAL >= 0x100000
458 #define TIMER_RELOAD (TIMER_INTERVAL >> 8)
459 #define TIMER_DIVISOR (TIMER_CTRL_DIV256)
460 #define TICKS2USECS(x) (256 * (x) / TICKS_PER_uSEC)
461 #elif TIMER_INTERVAL >= 0x10000
462 #define TIMER_RELOAD (TIMER_INTERVAL >> 4) /* Divide by 16 */
463 #define TIMER_DIVISOR (TIMER_CTRL_DIV16)
464 #define TICKS2USECS(x) (16 * (x) / TICKS_PER_uSEC)
466 #define TIMER_RELOAD (TIMER_INTERVAL)
467 #define TIMER_DIVISOR (TIMER_CTRL_DIV1)
468 #define TICKS2USECS(x) ((x) / TICKS_PER_uSEC)
471 static void timer_set_mode(enum clock_event_mode mode,
472 struct clock_event_device *clk)
477 case CLOCK_EVT_MODE_PERIODIC:
478 writel(TIMER_RELOAD, TIMER0_VA_BASE + TIMER_LOAD);
480 ctrl = TIMER_CTRL_PERIODIC;
481 ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_IE | TIMER_CTRL_ENABLE;
483 case CLOCK_EVT_MODE_ONESHOT:
484 /* period set, and timer enabled in 'next_event' hook */
485 ctrl = TIMER_CTRL_ONESHOT;
486 ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_IE;
488 case CLOCK_EVT_MODE_UNUSED:
489 case CLOCK_EVT_MODE_SHUTDOWN:
494 writel(ctrl, TIMER0_VA_BASE + TIMER_CTRL);
497 static int timer_set_next_event(unsigned long evt,
498 struct clock_event_device *unused)
500 unsigned long ctrl = readl(TIMER0_VA_BASE + TIMER_CTRL);
502 writel(evt, TIMER0_VA_BASE + TIMER_LOAD);
503 writel(ctrl | TIMER_CTRL_ENABLE, TIMER0_VA_BASE + TIMER_CTRL);
508 static struct clock_event_device timer0_clockevent = {
511 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
512 .set_mode = timer_set_mode,
513 .set_next_event = timer_set_next_event,
515 .cpumask = CPU_MASK_ALL,
518 static void __init realview_clockevents_init(unsigned int timer_irq)
520 timer0_clockevent.irq = timer_irq;
521 timer0_clockevent.mult =
522 div_sc(1000000, NSEC_PER_SEC, timer0_clockevent.shift);
523 timer0_clockevent.max_delta_ns =
524 clockevent_delta2ns(0xffffffff, &timer0_clockevent);
525 timer0_clockevent.min_delta_ns =
526 clockevent_delta2ns(0xf, &timer0_clockevent);
528 clockevents_register_device(&timer0_clockevent);
532 * IRQ handler for the timer
534 static irqreturn_t realview_timer_interrupt(int irq, void *dev_id)
536 struct clock_event_device *evt = &timer0_clockevent;
538 /* clear the interrupt */
539 writel(1, TIMER0_VA_BASE + TIMER_INTCLR);
541 evt->event_handler(evt);
546 static struct irqaction realview_timer_irq = {
547 .name = "RealView Timer Tick",
548 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
549 .handler = realview_timer_interrupt,
552 static cycle_t realview_get_cycles(void)
554 return ~readl(TIMER3_VA_BASE + TIMER_VALUE);
557 static struct clocksource clocksource_realview = {
560 .read = realview_get_cycles,
561 .mask = CLOCKSOURCE_MASK(32),
563 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
566 static void __init realview_clocksource_init(void)
568 /* setup timer 0 as free-running clocksource */
569 writel(0, TIMER3_VA_BASE + TIMER_CTRL);
570 writel(0xffffffff, TIMER3_VA_BASE + TIMER_LOAD);
571 writel(0xffffffff, TIMER3_VA_BASE + TIMER_VALUE);
572 writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
573 TIMER3_VA_BASE + TIMER_CTRL);
575 clocksource_realview.mult =
576 clocksource_khz2mult(1000, clocksource_realview.shift);
577 clocksource_register(&clocksource_realview);
581 * Set up the clock source and clock events devices
583 void __init realview_timer_init(unsigned int timer_irq)
587 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
589 * The dummy clock device has to be registered before the main device
590 * so that the latter will broadcast the clock events
592 local_timer_setup(smp_processor_id());
596 * set clock frequency:
597 * REALVIEW_REFCLK is 32KHz
598 * REALVIEW_TIMCLK is 1MHz
600 val = readl(__io_address(REALVIEW_SCTL_BASE));
601 writel((REALVIEW_TIMCLK << REALVIEW_TIMER1_EnSel) |
602 (REALVIEW_TIMCLK << REALVIEW_TIMER2_EnSel) |
603 (REALVIEW_TIMCLK << REALVIEW_TIMER3_EnSel) |
604 (REALVIEW_TIMCLK << REALVIEW_TIMER4_EnSel) | val,
605 __io_address(REALVIEW_SCTL_BASE));
608 * Initialise to a known state (all timers off)
610 writel(0, TIMER0_VA_BASE + TIMER_CTRL);
611 writel(0, TIMER1_VA_BASE + TIMER_CTRL);
612 writel(0, TIMER2_VA_BASE + TIMER_CTRL);
613 writel(0, TIMER3_VA_BASE + TIMER_CTRL);
616 * Make irqs happen for the system timer
618 setup_irq(timer_irq, &realview_timer_irq);
620 realview_clocksource_init();
621 realview_clockevents_init(timer_irq);