wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mach-w90x900/time.c |
| 3 | * |
| 4 | * Based on linux/arch/arm/plat-s3c24xx/time.c by Ben Dooks |
| 5 | * |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 6 | * Copyright (c) 2009 Nuvoton technology corporation |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 7 | * All rights reserved. |
| 8 | * |
| 9 | * Wan ZongShun <mcuos.com@gmail.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/clk.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <linux/leds.h> |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 26 | #include <linux/clocksource.h> |
| 27 | #include <linux/clockchips.h> |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 28 | |
| 29 | #include <asm/mach-types.h> |
| 30 | #include <asm/mach/irq.h> |
| 31 | #include <asm/mach/time.h> |
| 32 | |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 33 | #include <mach/map.h> |
| 34 | #include <mach/regs-timer.h> |
| 35 | |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 36 | #define RESETINT 0x1f |
| 37 | #define PERIOD (0x01 << 27) |
| 38 | #define ONESHOT (0x00 << 27) |
| 39 | #define COUNTEN (0x01 << 30) |
| 40 | #define INTEN (0x01 << 29) |
| 41 | |
| 42 | #define TICKS_PER_SEC 100 |
| 43 | #define PRESCALE 0x63 /* Divider = prescale + 1 */ |
| 44 | |
Li Jie | 1368c51 | 2009-12-31 15:57:53 +0100 | [diff] [blame] | 45 | #define TDR_SHIFT 24 |
| 46 | #define TDR_MASK ((1 << TDR_SHIFT) - 1) |
| 47 | |
| 48 | static unsigned int timer0_load; |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 49 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 50 | static void nuc900_clockevent_setmode(enum clock_event_mode mode, |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 51 | struct clock_event_device *clk) |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 52 | { |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 53 | unsigned int val; |
| 54 | |
| 55 | val = __raw_readl(REG_TCSR0); |
| 56 | val &= ~(0x03 << 27); |
| 57 | |
| 58 | switch (mode) { |
| 59 | case CLOCK_EVT_MODE_PERIODIC: |
| 60 | __raw_writel(timer0_load, REG_TICR0); |
| 61 | val |= (PERIOD | COUNTEN | INTEN | PRESCALE); |
| 62 | break; |
| 63 | |
| 64 | case CLOCK_EVT_MODE_ONESHOT: |
| 65 | val |= (ONESHOT | COUNTEN | INTEN | PRESCALE); |
| 66 | break; |
| 67 | |
| 68 | case CLOCK_EVT_MODE_UNUSED: |
| 69 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 70 | case CLOCK_EVT_MODE_RESUME: |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | __raw_writel(val, REG_TCSR0); |
| 75 | } |
| 76 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 77 | static int nuc900_clockevent_setnextevent(unsigned long evt, |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 78 | struct clock_event_device *clk) |
| 79 | { |
| 80 | unsigned int val; |
| 81 | |
| 82 | __raw_writel(evt, REG_TICR0); |
| 83 | |
| 84 | val = __raw_readl(REG_TCSR0); |
| 85 | val |= (COUNTEN | INTEN | PRESCALE); |
| 86 | __raw_writel(val, REG_TCSR0); |
| 87 | |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 91 | static struct clock_event_device nuc900_clockevent_device = { |
| 92 | .name = "nuc900-timer0", |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 93 | .shift = 32, |
Li Jie | 1368c51 | 2009-12-31 15:57:53 +0100 | [diff] [blame] | 94 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 95 | .set_mode = nuc900_clockevent_setmode, |
| 96 | .set_next_event = nuc900_clockevent_setnextevent, |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 97 | .rating = 300, |
| 98 | }; |
| 99 | |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 100 | /*IRQ handler for the timer*/ |
| 101 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 102 | static irqreturn_t nuc900_timer0_interrupt(int irq, void *dev_id) |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 103 | { |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 104 | struct clock_event_device *evt = &nuc900_clockevent_device; |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 105 | |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 106 | __raw_writel(0x01, REG_TISR); /* clear TIF0 */ |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 107 | |
| 108 | evt->event_handler(evt); |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 109 | return IRQ_HANDLED; |
| 110 | } |
| 111 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 112 | static struct irqaction nuc900_timer0_irq = { |
| 113 | .name = "nuc900-timer0", |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 114 | .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL, |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 115 | .handler = nuc900_timer0_interrupt, |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 116 | }; |
| 117 | |
Li Jie | 1368c51 | 2009-12-31 15:57:53 +0100 | [diff] [blame] | 118 | static void __init nuc900_clockevents_init(void) |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 119 | { |
Li Jie | 1368c51 | 2009-12-31 15:57:53 +0100 | [diff] [blame] | 120 | unsigned int rate; |
| 121 | struct clk *clk = clk_get(NULL, "timer0"); |
| 122 | |
| 123 | BUG_ON(IS_ERR(clk)); |
| 124 | |
| 125 | __raw_writel(0x00, REG_TCSR0); |
| 126 | |
| 127 | clk_enable(clk); |
| 128 | rate = clk_get_rate(clk) / (PRESCALE + 1); |
| 129 | |
| 130 | timer0_load = (rate / TICKS_PER_SEC); |
| 131 | |
| 132 | __raw_writel(RESETINT, REG_TISR); |
| 133 | setup_irq(IRQ_TIMER0, &nuc900_timer0_irq); |
| 134 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 135 | nuc900_clockevent_device.mult = div_sc(rate, NSEC_PER_SEC, |
| 136 | nuc900_clockevent_device.shift); |
| 137 | nuc900_clockevent_device.max_delta_ns = clockevent_delta2ns(0xffffffff, |
| 138 | &nuc900_clockevent_device); |
| 139 | nuc900_clockevent_device.min_delta_ns = clockevent_delta2ns(0xf, |
| 140 | &nuc900_clockevent_device); |
| 141 | nuc900_clockevent_device.cpumask = cpumask_of(0); |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 142 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 143 | clockevents_register_device(&nuc900_clockevent_device); |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 144 | } |
| 145 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 146 | static cycle_t nuc900_get_cycles(struct clocksource *cs) |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 147 | { |
Li Jie | 1368c51 | 2009-12-31 15:57:53 +0100 | [diff] [blame] | 148 | return (~__raw_readl(REG_TDR1)) & TDR_MASK; |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 149 | } |
| 150 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 151 | static struct clocksource clocksource_nuc900 = { |
| 152 | .name = "nuc900-timer1", |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 153 | .rating = 200, |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 154 | .read = nuc900_get_cycles, |
Li Jie | 1368c51 | 2009-12-31 15:57:53 +0100 | [diff] [blame] | 155 | .mask = CLOCKSOURCE_MASK(TDR_SHIFT), |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 156 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 157 | }; |
| 158 | |
Li Jie | 1368c51 | 2009-12-31 15:57:53 +0100 | [diff] [blame] | 159 | static void __init nuc900_clocksource_init(void) |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 160 | { |
| 161 | unsigned int val; |
Li Jie | 1368c51 | 2009-12-31 15:57:53 +0100 | [diff] [blame] | 162 | unsigned int rate; |
| 163 | struct clk *clk = clk_get(NULL, "timer1"); |
| 164 | |
| 165 | BUG_ON(IS_ERR(clk)); |
| 166 | |
| 167 | __raw_writel(0x00, REG_TCSR1); |
| 168 | |
| 169 | clk_enable(clk); |
| 170 | rate = clk_get_rate(clk) / (PRESCALE + 1); |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 171 | |
| 172 | __raw_writel(0xffffffff, REG_TICR1); |
| 173 | |
| 174 | val = __raw_readl(REG_TCSR1); |
Li Jie | 1368c51 | 2009-12-31 15:57:53 +0100 | [diff] [blame] | 175 | val |= (COUNTEN | PERIOD | PRESCALE); |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 176 | __raw_writel(val, REG_TCSR1); |
| 177 | |
Russell King | 894cf56 | 2010-12-13 13:20:06 +0000 | [diff] [blame^] | 178 | clocksource_register_hz(&clocksource_nuc900, rate); |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 179 | } |
| 180 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 181 | static void __init nuc900_timer_init(void) |
wanzongshun | 58b5369 | 2009-08-14 15:36:44 +0100 | [diff] [blame] | 182 | { |
Li Jie | 1368c51 | 2009-12-31 15:57:53 +0100 | [diff] [blame] | 183 | nuc900_clocksource_init(); |
| 184 | nuc900_clockevents_init(); |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 185 | } |
| 186 | |
wanzongshun | 35c9221 | 2009-08-21 07:07:46 +0100 | [diff] [blame] | 187 | struct sys_timer nuc900_timer = { |
| 188 | .init = nuc900_timer_init, |
wanzongshun | 7ec80dd | 2008-12-03 03:55:38 +0100 | [diff] [blame] | 189 | }; |