Blame view

drivers/clocksource/timer-pistachio.c 5.59 KB
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  /*
   * Pistachio clocksource based on general-purpose timers
   *
   * Copyright (C) 2015 Imagination Technologies
   *
   * This file is subject to the terms and conditions of the GNU General Public
   * License. See the file "COPYING" in the main directory of this archive
   * for more details.
   */
  
  #define pr_fmt(fmt) "%s: " fmt, __func__
  
  #include <linux/clk.h>
  #include <linux/clocksource.h>
  #include <linux/clockchips.h>
  #include <linux/delay.h>
  #include <linux/err.h>
  #include <linux/init.h>
  #include <linux/spinlock.h>
  #include <linux/mfd/syscon.h>
  #include <linux/of.h>
  #include <linux/of_address.h>
  #include <linux/platform_device.h>
  #include <linux/regmap.h>
  #include <linux/sched_clock.h>
  #include <linux/time.h>
  
  /* Top level reg */
  #define CR_TIMER_CTRL_CFG		0x00
  #define TIMER_ME_GLOBAL			BIT(0)
  #define CR_TIMER_REV			0x10
  
  /* Timer specific registers */
  #define TIMER_CFG			0x20
  #define TIMER_ME_LOCAL			BIT(0)
  #define TIMER_RELOAD_VALUE		0x24
  #define TIMER_CURRENT_VALUE		0x28
  #define TIMER_CURRENT_OVERFLOW_VALUE	0x2C
  #define TIMER_IRQ_STATUS		0x30
  #define TIMER_IRQ_CLEAR			0x34
  #define TIMER_IRQ_MASK			0x38
  
  #define PERIP_TIMER_CONTROL		0x90
  
  /* Timer specific configuration Values */
  #define RELOAD_VALUE			0xffffffff
  
  struct pistachio_clocksource {
  	void __iomem *base;
  	raw_spinlock_t lock;
  	struct clocksource cs;
  };
  
  static struct pistachio_clocksource pcs_gpt;
  
  #define to_pistachio_clocksource(cs)	\
  	container_of(cs, struct pistachio_clocksource, cs)
  
  static inline u32 gpt_readl(void __iomem *base, u32 offset, u32 gpt_id)
  {
  	return readl(base + 0x20 * gpt_id + offset);
  }
  
  static inline void gpt_writel(void __iomem *base, u32 value, u32 offset,
  		u32 gpt_id)
  {
  	writel(value, base + 0x20 * gpt_id + offset);
  }
a5a1d1c29   Thomas Gleixner   clocksource: Use ...
69
  static u64 notrace
f8af0e9c6   Jisheng Zhang   clocksource/drive...
70
  pistachio_clocksource_read_cycles(struct clocksource *cs)
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  {
  	struct pistachio_clocksource *pcs = to_pistachio_clocksource(cs);
  	u32 counter, overflw;
  	unsigned long flags;
  
  	/*
  	 * The counter value is only refreshed after the overflow value is read.
  	 * And they must be read in strict order, hence raw spin lock added.
  	 */
  
  	raw_spin_lock_irqsave(&pcs->lock, flags);
  	overflw = gpt_readl(pcs->base, TIMER_CURRENT_OVERFLOW_VALUE, 0);
  	counter = gpt_readl(pcs->base, TIMER_CURRENT_VALUE, 0);
  	raw_spin_unlock_irqrestore(&pcs->lock, flags);
a5a1d1c29   Thomas Gleixner   clocksource: Use ...
85
  	return (u64)~counter;
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  }
  
  static u64 notrace pistachio_read_sched_clock(void)
  {
  	return pistachio_clocksource_read_cycles(&pcs_gpt.cs);
  }
  
  static void pistachio_clksrc_set_mode(struct clocksource *cs, int timeridx,
  			int enable)
  {
  	struct pistachio_clocksource *pcs = to_pistachio_clocksource(cs);
  	u32 val;
  
  	val = gpt_readl(pcs->base, TIMER_CFG, timeridx);
  	if (enable)
  		val |= TIMER_ME_LOCAL;
  	else
  		val &= ~TIMER_ME_LOCAL;
  
  	gpt_writel(pcs->base, val, TIMER_CFG, timeridx);
  }
  
  static void pistachio_clksrc_enable(struct clocksource *cs, int timeridx)
  {
  	struct pistachio_clocksource *pcs = to_pistachio_clocksource(cs);
  
  	/* Disable GPT local before loading reload value */
  	pistachio_clksrc_set_mode(cs, timeridx, false);
  	gpt_writel(pcs->base, RELOAD_VALUE, TIMER_RELOAD_VALUE, timeridx);
  	pistachio_clksrc_set_mode(cs, timeridx, true);
  }
  
  static void pistachio_clksrc_disable(struct clocksource *cs, int timeridx)
  {
  	/* Disable GPT local */
  	pistachio_clksrc_set_mode(cs, timeridx, false);
  }
  
  static int pistachio_clocksource_enable(struct clocksource *cs)
  {
  	pistachio_clksrc_enable(cs, 0);
  	return 0;
  }
  
  static void pistachio_clocksource_disable(struct clocksource *cs)
  {
  	pistachio_clksrc_disable(cs, 0);
  }
  
  /* Desirable clock source for pistachio platform */
  static struct pistachio_clocksource pcs_gpt = {
  	.cs =	{
  		.name		= "gptimer",
  		.rating		= 300,
  		.enable		= pistachio_clocksource_enable,
  		.disable	= pistachio_clocksource_disable,
  		.read		= pistachio_clocksource_read_cycles,
  		.mask		= CLOCKSOURCE_MASK(32),
  		.flags		= CLOCK_SOURCE_IS_CONTINUOUS |
  				  CLOCK_SOURCE_SUSPEND_NONSTOP,
  		},
  };
41505a208   Daniel Lezcano   clocksource/drive...
148
  static int __init pistachio_clksrc_of_init(struct device_node *node)
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
149
150
151
152
153
154
155
156
157
158
  {
  	struct clk *sys_clk, *fast_clk;
  	struct regmap *periph_regs;
  	unsigned long rate;
  	int ret;
  
  	pcs_gpt.base = of_iomap(node, 0);
  	if (!pcs_gpt.base) {
  		pr_err("cannot iomap
  ");
41505a208   Daniel Lezcano   clocksource/drive...
159
  		return -ENXIO;
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
160
161
162
163
  	}
  
  	periph_regs = syscon_regmap_lookup_by_phandle(node, "img,cr-periph");
  	if (IS_ERR(periph_regs)) {
b7c8b4aac   Vladimir Zapolskiy   clocksource/drive...
164
165
  		pr_err("cannot get peripheral regmap (%ld)
  ",
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
166
  		       PTR_ERR(periph_regs));
41505a208   Daniel Lezcano   clocksource/drive...
167
  		return PTR_ERR(periph_regs);
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
168
169
170
171
172
173
  	}
  
  	/* Switch to using the fast counter clock */
  	ret = regmap_update_bits(periph_regs, PERIP_TIMER_CONTROL,
  				 0xf, 0x0);
  	if (ret)
41505a208   Daniel Lezcano   clocksource/drive...
174
  		return ret;
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
175
176
177
  
  	sys_clk = of_clk_get_by_name(node, "sys");
  	if (IS_ERR(sys_clk)) {
b7c8b4aac   Vladimir Zapolskiy   clocksource/drive...
178
179
  		pr_err("clock get failed (%ld)
  ", PTR_ERR(sys_clk));
41505a208   Daniel Lezcano   clocksource/drive...
180
  		return PTR_ERR(sys_clk);
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
181
182
183
184
185
186
  	}
  
  	fast_clk = of_clk_get_by_name(node, "fast");
  	if (IS_ERR(fast_clk)) {
  		pr_err("clock get failed (%lu)
  ", PTR_ERR(fast_clk));
41505a208   Daniel Lezcano   clocksource/drive...
187
  		return PTR_ERR(fast_clk);
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
188
189
190
191
192
193
  	}
  
  	ret = clk_prepare_enable(sys_clk);
  	if (ret < 0) {
  		pr_err("failed to enable clock (%d)
  ", ret);
41505a208   Daniel Lezcano   clocksource/drive...
194
  		return ret;
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
195
196
197
198
199
200
201
  	}
  
  	ret = clk_prepare_enable(fast_clk);
  	if (ret < 0) {
  		pr_err("failed to enable clock (%d)
  ", ret);
  		clk_disable_unprepare(sys_clk);
41505a208   Daniel Lezcano   clocksource/drive...
202
  		return ret;
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
203
204
205
206
207
  	}
  
  	rate = clk_get_rate(fast_clk);
  
  	/* Disable irq's for clocksource usage */
4d0e70165   Marcin Nowakowski   drivers/clocksour...
208
209
210
211
  	gpt_writel(pcs_gpt.base, 0, TIMER_IRQ_MASK, 0);
  	gpt_writel(pcs_gpt.base, 0, TIMER_IRQ_MASK, 1);
  	gpt_writel(pcs_gpt.base, 0, TIMER_IRQ_MASK, 2);
  	gpt_writel(pcs_gpt.base, 0, TIMER_IRQ_MASK, 3);
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
212
213
214
215
216
217
  
  	/* Enable timer block */
  	writel(TIMER_ME_GLOBAL, pcs_gpt.base);
  
  	raw_spin_lock_init(&pcs_gpt.lock);
  	sched_clock_register(pistachio_read_sched_clock, 32, rate);
41505a208   Daniel Lezcano   clocksource/drive...
218
  	return clocksource_register_hz(&pcs_gpt.cs, rate);
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
219
  }
172733959   Daniel Lezcano   clocksource/drive...
220
  TIMER_OF_DECLARE(pistachio_gptimer, "img,pistachio-gptimer",
84583983c   Ezequiel Garcia   CLOCKSOURCE: Add ...
221
  		       pistachio_clksrc_of_init);