Blame view

drivers/clocksource/vt8500_timer.c 4.66 KB
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
1
  /*
83cc76909   Tony Prisk   vt8500: Fix heade...
2
   *  arch/arm/mach-vt8500/timer.c
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
3
   *
e9a91de76   Tony Prisk   arm: vt8500: Upda...
4
   *  Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   *  Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */
e9a91de76   Tony Prisk   arm: vt8500: Upda...
21
22
23
24
  /*
   * This file is copied and modified from the original timer.c provided by
   * Alexey Charkov. Minor changes have been made for Device Tree Support.
   */
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
25
26
27
28
29
30
  #include <linux/io.h>
  #include <linux/irq.h>
  #include <linux/interrupt.h>
  #include <linux/clocksource.h>
  #include <linux/clockchips.h>
  #include <linux/delay.h>
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
31

e9a91de76   Tony Prisk   arm: vt8500: Upda...
32
33
34
  #include <linux/of.h>
  #include <linux/of_address.h>
  #include <linux/of_irq.h>
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
35
36
  
  #define VT8500_TIMER_OFFSET	0x0100
e9a91de76   Tony Prisk   arm: vt8500: Upda...
37
  #define VT8500_TIMER_HZ		3000000
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
38
39
40
41
42
43
44
45
46
  #define TIMER_MATCH_VAL		0x0000
  #define TIMER_COUNT_VAL		0x0010
  #define TIMER_STATUS_VAL	0x0014
  #define TIMER_IER_VAL		0x001c		/* interrupt enable */
  #define TIMER_CTRL_VAL		0x0020
  #define TIMER_AS_VAL		0x0024		/* access status */
  #define TIMER_COUNT_R_ACTIVE	(1 << 5)	/* not ready for read */
  #define TIMER_COUNT_W_ACTIVE	(1 << 4)	/* not ready for write */
  #define TIMER_MATCH_W_ACTIVE	(1 << 0)	/* not ready for write */
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
47
48
  
  #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
f9eccf246   Roman Volkov   clocksource/drive...
49
  #define MIN_OSCR_DELTA		16
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
50
51
52
53
54
55
56
57
58
59
60
  static void __iomem *regbase;
  
  static cycle_t vt8500_timer_read(struct clocksource *cs)
  {
  	int loops = msecs_to_loops(10);
  	writel(3, regbase + TIMER_CTRL_VAL);
  	while ((readl((regbase + TIMER_AS_VAL)) & TIMER_COUNT_R_ACTIVE)
  						&& --loops)
  		cpu_relax();
  	return readl(regbase + TIMER_COUNT_VAL);
  }
e9a91de76   Tony Prisk   arm: vt8500: Upda...
61
  static struct clocksource clocksource = {
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  	.name           = "vt8500_timer",
  	.rating         = 200,
  	.read           = vt8500_timer_read,
  	.mask           = CLOCKSOURCE_MASK(32),
  	.flags          = CLOCK_SOURCE_IS_CONTINUOUS,
  };
  
  static int vt8500_timer_set_next_event(unsigned long cycles,
  				    struct clock_event_device *evt)
  {
  	int loops = msecs_to_loops(10);
  	cycle_t alarm = clocksource.read(&clocksource) + cycles;
  	while ((readl(regbase + TIMER_AS_VAL) & TIMER_MATCH_W_ACTIVE)
  						&& --loops)
  		cpu_relax();
  	writel((unsigned long)alarm, regbase + TIMER_MATCH_VAL);
f9eccf246   Roman Volkov   clocksource/drive...
78
  	if ((signed)(alarm - clocksource.read(&clocksource)) <= MIN_OSCR_DELTA)
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
79
80
81
82
83
84
  		return -ETIME;
  
  	writel(1, regbase + TIMER_IER_VAL);
  
  	return 0;
  }
214bc755b   Viresh Kumar   clockevents/drive...
85
  static int vt8500_shutdown(struct clock_event_device *evt)
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
86
  {
214bc755b   Viresh Kumar   clockevents/drive...
87
88
89
  	writel(readl(regbase + TIMER_CTRL_VAL) | 1, regbase + TIMER_CTRL_VAL);
  	writel(0, regbase + TIMER_IER_VAL);
  	return 0;
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
90
  }
e9a91de76   Tony Prisk   arm: vt8500: Upda...
91
  static struct clock_event_device clockevent = {
214bc755b   Viresh Kumar   clockevents/drive...
92
93
94
95
96
97
  	.name			= "vt8500_timer",
  	.features		= CLOCK_EVT_FEAT_ONESHOT,
  	.rating			= 200,
  	.set_next_event		= vt8500_timer_set_next_event,
  	.set_state_shutdown	= vt8500_shutdown,
  	.set_state_oneshot	= vt8500_shutdown,
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
98
99
100
101
102
103
104
105
106
107
  };
  
  static irqreturn_t vt8500_timer_interrupt(int irq, void *dev_id)
  {
  	struct clock_event_device *evt = dev_id;
  	writel(0xf, regbase + TIMER_STATUS_VAL);
  	evt->event_handler(evt);
  
  	return IRQ_HANDLED;
  }
e9a91de76   Tony Prisk   arm: vt8500: Upda...
108
  static struct irqaction irq = {
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
109
  	.name    = "vt8500_timer",
39039eb31   Michael Opdenacker   clocksource: vt85...
110
  	.flags   = IRQF_TIMER | IRQF_IRQPOLL,
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
111
112
113
  	.handler = vt8500_timer_interrupt,
  	.dev_id  = &clockevent,
  };
979d7f28f   Daniel Lezcano   clocksource/drive...
114
  static int __init vt8500_timer_init(struct device_node *np)
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
115
  {
979d7f28f   Daniel Lezcano   clocksource/drive...
116
  	int timer_irq, ret;
e9a91de76   Tony Prisk   arm: vt8500: Upda...
117

e9a91de76   Tony Prisk   arm: vt8500: Upda...
118
119
120
121
122
  	regbase = of_iomap(np, 0);
  	if (!regbase) {
  		pr_err("%s: Missing iobase description in Device Tree
  ",
  								__func__);
979d7f28f   Daniel Lezcano   clocksource/drive...
123
  		return -ENXIO;
e9a91de76   Tony Prisk   arm: vt8500: Upda...
124
  	}
979d7f28f   Daniel Lezcano   clocksource/drive...
125

e9a91de76   Tony Prisk   arm: vt8500: Upda...
126
127
128
129
130
  	timer_irq = irq_of_parse_and_map(np, 0);
  	if (!timer_irq) {
  		pr_err("%s: Missing irq description in Device Tree
  ",
  								__func__);
979d7f28f   Daniel Lezcano   clocksource/drive...
131
  		return -EINVAL;
e9a91de76   Tony Prisk   arm: vt8500: Upda...
132
  	}
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
133
134
135
136
  
  	writel(1, regbase + TIMER_CTRL_VAL);
  	writel(0xf, regbase + TIMER_STATUS_VAL);
  	writel(~0, regbase + TIMER_MATCH_VAL);
979d7f28f   Daniel Lezcano   clocksource/drive...
137
138
  	ret = clocksource_register_hz(&clocksource, VT8500_TIMER_HZ);
  	if (ret) {
e9a91de76   Tony Prisk   arm: vt8500: Upda...
139
140
  		pr_err("%s: vt8500_timer_init: clocksource_register failed for %s
  ",
979d7f28f   Daniel Lezcano   clocksource/drive...
141
142
143
  		       __func__, clocksource.name);
  		return ret;
  	}
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
144

21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
145
  	clockevent.cpumask = cpumask_of(0);
979d7f28f   Daniel Lezcano   clocksource/drive...
146
147
  	ret = setup_irq(timer_irq, &irq);
  	if (ret) {
e9a91de76   Tony Prisk   arm: vt8500: Upda...
148
149
150
  		pr_err("%s: setup_irq failed for %s
  ", __func__,
  							clockevent.name);
979d7f28f   Daniel Lezcano   clocksource/drive...
151
152
  		return ret;
  	}
838a2ae80   Shawn Guo   ARM: use clockeve...
153
  	clockevents_config_and_register(&clockevent, VT8500_TIMER_HZ,
f9eccf246   Roman Volkov   clocksource/drive...
154
  					MIN_OSCR_DELTA * 2, 0xf0000000);
979d7f28f   Daniel Lezcano   clocksource/drive...
155
156
  
  	return 0;
21f47fbc5   Alexey Charkov   ARM: 6597/1: Add ...
157
  }
177cf6e52   Daniel Lezcano   clocksources: Swi...
158
  CLOCKSOURCE_OF_DECLARE(vt8500, "via,vt8500-timer", vt8500_timer_init);