0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/init.h>
0009 #include <linux/kernel.h>
0010 #include <linux/clk.h>
0011 #include <linux/cpu.h>
0012 #include <linux/delay.h>
0013 #include <linux/device.h>
0014 #include <linux/err.h>
0015 #include <linux/smp.h>
0016 #include <linux/jiffies.h>
0017 #include <linux/clockchips.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/io.h>
0020 #include <linux/of_irq.h>
0021 #include <linux/of_address.h>
0022
0023 #include <asm/smp_twd.h>
0024
0025
0026 static void __iomem *twd_base;
0027
0028 static struct clk *twd_clk;
0029 static unsigned long twd_timer_rate;
0030 static DEFINE_PER_CPU(bool, percpu_setup_called);
0031
0032 static struct clock_event_device __percpu *twd_evt;
0033 static unsigned int twd_features =
0034 CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
0035 static int twd_ppi;
0036
0037 static int twd_shutdown(struct clock_event_device *clk)
0038 {
0039 writel_relaxed(0, twd_base + TWD_TIMER_CONTROL);
0040 return 0;
0041 }
0042
0043 static int twd_set_oneshot(struct clock_event_device *clk)
0044 {
0045
0046 writel_relaxed(TWD_TIMER_CONTROL_IT_ENABLE | TWD_TIMER_CONTROL_ONESHOT,
0047 twd_base + TWD_TIMER_CONTROL);
0048 return 0;
0049 }
0050
0051 static int twd_set_periodic(struct clock_event_device *clk)
0052 {
0053 unsigned long ctrl = TWD_TIMER_CONTROL_ENABLE |
0054 TWD_TIMER_CONTROL_IT_ENABLE |
0055 TWD_TIMER_CONTROL_PERIODIC;
0056
0057 writel_relaxed(DIV_ROUND_CLOSEST(twd_timer_rate, HZ),
0058 twd_base + TWD_TIMER_LOAD);
0059 writel_relaxed(ctrl, twd_base + TWD_TIMER_CONTROL);
0060 return 0;
0061 }
0062
0063 static int twd_set_next_event(unsigned long evt,
0064 struct clock_event_device *unused)
0065 {
0066 unsigned long ctrl = readl_relaxed(twd_base + TWD_TIMER_CONTROL);
0067
0068 ctrl |= TWD_TIMER_CONTROL_ENABLE;
0069
0070 writel_relaxed(evt, twd_base + TWD_TIMER_COUNTER);
0071 writel_relaxed(ctrl, twd_base + TWD_TIMER_CONTROL);
0072
0073 return 0;
0074 }
0075
0076
0077
0078
0079
0080
0081
0082 static int twd_timer_ack(void)
0083 {
0084 if (readl_relaxed(twd_base + TWD_TIMER_INTSTAT)) {
0085 writel_relaxed(1, twd_base + TWD_TIMER_INTSTAT);
0086 return 1;
0087 }
0088
0089 return 0;
0090 }
0091
0092 static void twd_timer_stop(void)
0093 {
0094 struct clock_event_device *clk = raw_cpu_ptr(twd_evt);
0095
0096 twd_shutdown(clk);
0097 disable_percpu_irq(clk->irq);
0098 }
0099
0100
0101
0102
0103
0104 static void twd_update_frequency(void *new_rate)
0105 {
0106 twd_timer_rate = *((unsigned long *) new_rate);
0107
0108 clockevents_update_freq(raw_cpu_ptr(twd_evt), twd_timer_rate);
0109 }
0110
0111 static int twd_rate_change(struct notifier_block *nb,
0112 unsigned long flags, void *data)
0113 {
0114 struct clk_notifier_data *cnd = data;
0115
0116
0117
0118
0119
0120
0121 if (flags == POST_RATE_CHANGE)
0122 on_each_cpu(twd_update_frequency,
0123 (void *)&cnd->new_rate, 1);
0124
0125 return NOTIFY_OK;
0126 }
0127
0128 static struct notifier_block twd_clk_nb = {
0129 .notifier_call = twd_rate_change,
0130 };
0131
0132 static int twd_clk_init(void)
0133 {
0134 if (twd_evt && raw_cpu_ptr(twd_evt) && !IS_ERR(twd_clk))
0135 return clk_notifier_register(twd_clk, &twd_clk_nb);
0136
0137 return 0;
0138 }
0139 core_initcall(twd_clk_init);
0140
0141 static void twd_calibrate_rate(void)
0142 {
0143 unsigned long count;
0144 u64 waitjiffies;
0145
0146
0147
0148
0149
0150 if (twd_timer_rate == 0) {
0151 pr_info("Calibrating local timer... ");
0152
0153
0154 waitjiffies = get_jiffies_64() + 1;
0155
0156 while (get_jiffies_64() < waitjiffies)
0157 udelay(10);
0158
0159
0160 waitjiffies += 5;
0161
0162
0163 writel_relaxed(0x1, twd_base + TWD_TIMER_CONTROL);
0164
0165
0166 writel_relaxed(0xFFFFFFFFU, twd_base + TWD_TIMER_COUNTER);
0167
0168 while (get_jiffies_64() < waitjiffies)
0169 udelay(10);
0170
0171 count = readl_relaxed(twd_base + TWD_TIMER_COUNTER);
0172
0173 twd_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
0174
0175 pr_cont("%lu.%02luMHz.\n", twd_timer_rate / 1000000,
0176 (twd_timer_rate / 10000) % 100);
0177 }
0178 }
0179
0180 static irqreturn_t twd_handler(int irq, void *dev_id)
0181 {
0182 struct clock_event_device *evt = dev_id;
0183
0184 if (twd_timer_ack()) {
0185 evt->event_handler(evt);
0186 return IRQ_HANDLED;
0187 }
0188
0189 return IRQ_NONE;
0190 }
0191
0192 static void twd_get_clock(struct device_node *np)
0193 {
0194 int err;
0195
0196 if (np)
0197 twd_clk = of_clk_get(np, 0);
0198 else
0199 twd_clk = clk_get_sys("smp_twd", NULL);
0200
0201 if (IS_ERR(twd_clk)) {
0202 pr_err("smp_twd: clock not found %d\n", (int) PTR_ERR(twd_clk));
0203 return;
0204 }
0205
0206 err = clk_prepare_enable(twd_clk);
0207 if (err) {
0208 pr_err("smp_twd: clock failed to prepare+enable: %d\n", err);
0209 clk_put(twd_clk);
0210 return;
0211 }
0212
0213 twd_timer_rate = clk_get_rate(twd_clk);
0214 }
0215
0216
0217
0218
0219 static void twd_timer_setup(void)
0220 {
0221 struct clock_event_device *clk = raw_cpu_ptr(twd_evt);
0222 int cpu = smp_processor_id();
0223
0224
0225
0226
0227
0228 if (per_cpu(percpu_setup_called, cpu)) {
0229 writel_relaxed(0, twd_base + TWD_TIMER_CONTROL);
0230 clockevents_register_device(clk);
0231 enable_percpu_irq(clk->irq, 0);
0232 return;
0233 }
0234 per_cpu(percpu_setup_called, cpu) = true;
0235
0236 twd_calibrate_rate();
0237
0238
0239
0240
0241
0242 writel_relaxed(0, twd_base + TWD_TIMER_CONTROL);
0243
0244 clk->name = "local_timer";
0245 clk->features = twd_features;
0246 clk->rating = 350;
0247 clk->set_state_shutdown = twd_shutdown;
0248 clk->set_state_periodic = twd_set_periodic;
0249 clk->set_state_oneshot = twd_set_oneshot;
0250 clk->tick_resume = twd_shutdown;
0251 clk->set_next_event = twd_set_next_event;
0252 clk->irq = twd_ppi;
0253 clk->cpumask = cpumask_of(cpu);
0254
0255 clockevents_config_and_register(clk, twd_timer_rate,
0256 0xf, 0xffffffff);
0257 enable_percpu_irq(clk->irq, 0);
0258 }
0259
0260 static int twd_timer_starting_cpu(unsigned int cpu)
0261 {
0262 twd_timer_setup();
0263 return 0;
0264 }
0265
0266 static int twd_timer_dying_cpu(unsigned int cpu)
0267 {
0268 twd_timer_stop();
0269 return 0;
0270 }
0271
0272 static int __init twd_local_timer_common_register(struct device_node *np)
0273 {
0274 int err;
0275
0276 twd_evt = alloc_percpu(struct clock_event_device);
0277 if (!twd_evt) {
0278 err = -ENOMEM;
0279 goto out_free;
0280 }
0281
0282 err = request_percpu_irq(twd_ppi, twd_handler, "twd", twd_evt);
0283 if (err) {
0284 pr_err("twd: can't register interrupt %d (%d)\n", twd_ppi, err);
0285 goto out_free;
0286 }
0287
0288 cpuhp_setup_state_nocalls(CPUHP_AP_ARM_TWD_STARTING,
0289 "arm/timer/twd:starting",
0290 twd_timer_starting_cpu, twd_timer_dying_cpu);
0291
0292 twd_get_clock(np);
0293 if (!of_property_read_bool(np, "always-on"))
0294 twd_features |= CLOCK_EVT_FEAT_C3STOP;
0295
0296
0297
0298
0299
0300
0301 if (twd_timer_rate)
0302 twd_timer_setup();
0303 else
0304 late_time_init = twd_timer_setup;
0305
0306 return 0;
0307
0308 out_free:
0309 iounmap(twd_base);
0310 twd_base = NULL;
0311 free_percpu(twd_evt);
0312
0313 return err;
0314 }
0315
0316 static int __init twd_local_timer_of_register(struct device_node *np)
0317 {
0318 int err;
0319
0320 twd_ppi = irq_of_parse_and_map(np, 0);
0321 if (!twd_ppi) {
0322 err = -EINVAL;
0323 goto out;
0324 }
0325
0326 twd_base = of_iomap(np, 0);
0327 if (!twd_base) {
0328 err = -ENOMEM;
0329 goto out;
0330 }
0331
0332 err = twd_local_timer_common_register(np);
0333
0334 out:
0335 WARN(err, "twd_local_timer_of_register failed (%d)\n", err);
0336 return err;
0337 }
0338 TIMER_OF_DECLARE(arm_twd_a9, "arm,cortex-a9-twd-timer", twd_local_timer_of_register);
0339 TIMER_OF_DECLARE(arm_twd_a5, "arm,cortex-a5-twd-timer", twd_local_timer_of_register);
0340 TIMER_OF_DECLARE(arm_twd_11mp, "arm,arm11mp-twd-timer", twd_local_timer_of_register);