Blame view

drivers/clocksource/timer-vf-pit.c 4.88 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
c19672492   Jingchang Lu   clocksource: Add ...
2
3
  /*
   * Copyright 2012-2013 Freescale Semiconductor, Inc.
c19672492   Jingchang Lu   clocksource: Add ...
4
5
6
7
8
9
10
   */
  
  #include <linux/interrupt.h>
  #include <linux/clockchips.h>
  #include <linux/clk.h>
  #include <linux/of_address.h>
  #include <linux/of_irq.h>
269933936   Fabio Estevam   clocksource: vf_p...
11
  #include <linux/sched_clock.h>
c19672492   Jingchang Lu   clocksource: Add ...
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
  
  /*
   * Each pit takes 0x10 Bytes register space
   */
  #define PITMCR		0x00
  #define PIT0_OFFSET	0x100
  #define PITn_OFFSET(n)	(PIT0_OFFSET + 0x10 * (n))
  #define PITLDVAL	0x00
  #define PITCVAL		0x04
  #define PITTCTRL	0x08
  #define PITTFLG		0x0c
  
  #define PITMCR_MDIS	(0x1 << 1)
  
  #define PITTCTRL_TEN	(0x1 << 0)
  #define PITTCTRL_TIE	(0x1 << 1)
  #define PITCTRL_CHN	(0x1 << 2)
  
  #define PITTFLG_TIF	0x1
  
  static void __iomem *clksrc_base;
  static void __iomem *clkevt_base;
  static unsigned long cycle_per_jiffy;
  
  static inline void pit_timer_enable(void)
  {
  	__raw_writel(PITTCTRL_TEN | PITTCTRL_TIE, clkevt_base + PITTCTRL);
  }
  
  static inline void pit_timer_disable(void)
  {
  	__raw_writel(0, clkevt_base + PITTCTRL);
  }
  
  static inline void pit_irq_acknowledge(void)
  {
  	__raw_writel(PITTFLG_TIF, clkevt_base + PITTFLG);
  }
36361abc8   Jisheng Zhang   clocksource/drive...
50
  static u64 notrace pit_read_sched_clock(void)
c19672492   Jingchang Lu   clocksource: Add ...
51
  {
224aa3ed4   Stefan Agner   clocksource: vf_p...
52
  	return ~__raw_readl(clksrc_base + PITCVAL);
c19672492   Jingchang Lu   clocksource: Add ...
53
54
55
56
57
58
59
60
  }
  
  static int __init pit_clocksource_init(unsigned long rate)
  {
  	/* set the max load value and start the clock source counter */
  	__raw_writel(0, clksrc_base + PITTCTRL);
  	__raw_writel(~0UL, clksrc_base + PITLDVAL);
  	__raw_writel(PITTCTRL_TEN, clksrc_base + PITTCTRL);
f4e6e1ea1   Stephen Boyd   clocksource: vf_p...
61
  	sched_clock_register(pit_read_sched_clock, 32, rate);
c19672492   Jingchang Lu   clocksource: Add ...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  	return clocksource_mmio_init(clksrc_base + PITCVAL, "vf-pit", rate,
  			300, 32, clocksource_mmio_readl_down);
  }
  
  static int pit_set_next_event(unsigned long delta,
  				struct clock_event_device *unused)
  {
  	/*
  	 * set a new value to PITLDVAL register will not restart the timer,
  	 * to abort the current cycle and start a timer period with the new
  	 * value, the timer must be disabled and enabled again.
  	 * and the PITLAVAL should be set to delta minus one according to pit
  	 * hardware requirement.
  	 */
  	pit_timer_disable();
  	__raw_writel(delta - 1, clkevt_base + PITLDVAL);
  	pit_timer_enable();
  
  	return 0;
  }
9552a6af8   Viresh Kumar   clockevents/drive...
82
  static int pit_shutdown(struct clock_event_device *evt)
c19672492   Jingchang Lu   clocksource: Add ...
83
  {
9552a6af8   Viresh Kumar   clockevents/drive...
84
85
86
87
88
89
90
91
  	pit_timer_disable();
  	return 0;
  }
  
  static int pit_set_periodic(struct clock_event_device *evt)
  {
  	pit_set_next_event(cycle_per_jiffy, evt);
  	return 0;
c19672492   Jingchang Lu   clocksource: Add ...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  }
  
  static irqreturn_t pit_timer_interrupt(int irq, void *dev_id)
  {
  	struct clock_event_device *evt = dev_id;
  
  	pit_irq_acknowledge();
  
  	/*
  	 * pit hardware doesn't support oneshot, it will generate an interrupt
  	 * and reload the counter value from PITLDVAL when PITCVAL reach zero,
  	 * and start the counter again. So software need to disable the timer
  	 * to stop the counter loop in ONESHOT mode.
  	 */
9552a6af8   Viresh Kumar   clockevents/drive...
106
  	if (likely(clockevent_state_oneshot(evt)))
c19672492   Jingchang Lu   clocksource: Add ...
107
108
109
110
111
112
113
114
115
116
  		pit_timer_disable();
  
  	evt->event_handler(evt);
  
  	return IRQ_HANDLED;
  }
  
  static struct clock_event_device clockevent_pit = {
  	.name		= "VF pit timer",
  	.features	= CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
9552a6af8   Viresh Kumar   clockevents/drive...
117
118
  	.set_state_shutdown = pit_shutdown,
  	.set_state_periodic = pit_set_periodic,
c19672492   Jingchang Lu   clocksource: Add ...
119
120
121
  	.set_next_event	= pit_set_next_event,
  	.rating		= 300,
  };
c19672492   Jingchang Lu   clocksource: Add ...
122
123
124
125
  static int __init pit_clockevent_init(unsigned long rate, int irq)
  {
  	__raw_writel(0, clkevt_base + PITTCTRL);
  	__raw_writel(PITTFLG_TIF, clkevt_base + PITTFLG);
cc2550b42   afzal mohammed   clocksource: Repl...
126
  	BUG_ON(request_irq(irq, pit_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
760a53768   afzal mohammed   clocksource/drive...
127
  			   "VF pit timer", &clockevent_pit));
c19672492   Jingchang Lu   clocksource: Add ...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  
  	clockevent_pit.cpumask = cpumask_of(0);
  	clockevent_pit.irq = irq;
  	/*
  	 * The value for the LDVAL register trigger is calculated as:
  	 * LDVAL trigger = (period / clock period) - 1
  	 * The pit is a 32-bit down count timer, when the conter value
  	 * reaches 0, it will generate an interrupt, thus the minimal
  	 * LDVAL trigger value is 1. And then the min_delta is
  	 * minimal LDVAL trigger value + 1, and the max_delta is full 32-bit.
  	 */
  	clockevents_config_and_register(&clockevent_pit, rate, 2, 0xffffffff);
  
  	return 0;
  }
b71306e62   Daniel Lezcano   clocksource/drive...
143
  static int __init pit_timer_init(struct device_node *np)
c19672492   Jingchang Lu   clocksource: Add ...
144
145
146
147
  {
  	struct clk *pit_clk;
  	void __iomem *timer_base;
  	unsigned long clk_rate;
b71306e62   Daniel Lezcano   clocksource/drive...
148
  	int irq, ret;
c19672492   Jingchang Lu   clocksource: Add ...
149
150
  
  	timer_base = of_iomap(np, 0);
b71306e62   Daniel Lezcano   clocksource/drive...
151
  	if (!timer_base) {
ac9ce6d1a   Rafał Miłecki   clocksource: Add ...
152
153
  		pr_err("Failed to iomap
  ");
b71306e62   Daniel Lezcano   clocksource/drive...
154
155
  		return -ENXIO;
  	}
c19672492   Jingchang Lu   clocksource: Add ...
156
157
158
159
160
161
162
163
164
165
  
  	/*
  	 * PIT0 and PIT1 can be chained to build a 64-bit timer,
  	 * so choose PIT2 as clocksource, PIT3 as clockevent device,
  	 * and leave PIT0 and PIT1 unused for anyone else who needs them.
  	 */
  	clksrc_base = timer_base + PITn_OFFSET(2);
  	clkevt_base = timer_base + PITn_OFFSET(3);
  
  	irq = irq_of_parse_and_map(np, 0);
b71306e62   Daniel Lezcano   clocksource/drive...
166
167
  	if (irq <= 0)
  		return -EINVAL;
c19672492   Jingchang Lu   clocksource: Add ...
168
169
  
  	pit_clk = of_clk_get(np, 0);
b71306e62   Daniel Lezcano   clocksource/drive...
170
171
  	if (IS_ERR(pit_clk))
  		return PTR_ERR(pit_clk);
c19672492   Jingchang Lu   clocksource: Add ...
172

b71306e62   Daniel Lezcano   clocksource/drive...
173
174
175
  	ret = clk_prepare_enable(pit_clk);
  	if (ret)
  		return ret;
c19672492   Jingchang Lu   clocksource: Add ...
176
177
178
179
180
181
  
  	clk_rate = clk_get_rate(pit_clk);
  	cycle_per_jiffy = clk_rate / (HZ);
  
  	/* enable the pit module */
  	__raw_writel(~PITMCR_MDIS, timer_base + PITMCR);
b71306e62   Daniel Lezcano   clocksource/drive...
182
183
184
  	ret = pit_clocksource_init(clk_rate);
  	if (ret)
  		return ret;
c19672492   Jingchang Lu   clocksource: Add ...
185

b71306e62   Daniel Lezcano   clocksource/drive...
186
  	return pit_clockevent_init(clk_rate, irq);
c19672492   Jingchang Lu   clocksource: Add ...
187
  }
172733959   Daniel Lezcano   clocksource/drive...
188
  TIMER_OF_DECLARE(vf610, "fsl,vf610-pit", pit_timer_init);