Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 2 | /* |
Andrew Victor | 9d04126 | 2007-02-05 11:42:07 +0100 | [diff] [blame] | 3 | * linux/arch/arm/mach-at91/at91rm9200_time.c |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2003 SAN People |
| 6 | * Copyright (C) 2003 ATMEL |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 9 | #include <linux/kernel.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 10 | #include <linux/interrupt.h> |
Thomas Gleixner | 07d265d | 2006-07-01 23:01:50 +0100 | [diff] [blame] | 11 | #include <linux/irq.h> |
Alexandre Belloni | 216ab8f | 2015-08-16 11:23:44 +0200 | [diff] [blame] | 12 | #include <linux/clk.h> |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 13 | #include <linux/clockchips.h> |
Joachim Eastwood | 9fce85c | 2012-04-04 19:15:15 +0200 | [diff] [blame] | 14 | #include <linux/export.h> |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 15 | #include <linux/mfd/syscon.h> |
| 16 | #include <linux/mfd/syscon/atmel-st.h> |
Joachim Eastwood | 454c46d | 2012-10-28 18:31:07 +0000 | [diff] [blame] | 17 | #include <linux/of_irq.h> |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 18 | #include <linux/regmap.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 19 | |
Andrew Victor | 963151f | 2006-06-19 15:23:41 +0100 | [diff] [blame] | 20 | static unsigned long last_crtr; |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 21 | static u32 irqmask; |
| 22 | static struct clock_event_device clkevt; |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 23 | static struct regmap *regmap_st; |
Alexandre Belloni | 216ab8f | 2015-08-16 11:23:44 +0200 | [diff] [blame] | 24 | static int timer_latch; |
Jean-Christophe PLAGNIOL-VILLARD | 2f5893c | 2011-10-16 18:17:09 +0800 | [diff] [blame] | 25 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 26 | /* |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 27 | * The ST_CRTR is updated asynchronously to the master clock ... but |
| 28 | * the updates as seen by the CPU don't seem to be strictly monotonic. |
| 29 | * Waiting until we read the same value twice avoids glitching. |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 30 | */ |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 31 | static inline unsigned long read_CRTR(void) |
| 32 | { |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 33 | unsigned int x1, x2; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 34 | |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 35 | regmap_read(regmap_st, AT91_ST_CRTR, &x1); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 36 | do { |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 37 | regmap_read(regmap_st, AT91_ST_CRTR, &x2); |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 38 | if (x1 == x2) |
| 39 | break; |
| 40 | x1 = x2; |
| 41 | } while (1); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 42 | return x1; |
| 43 | } |
| 44 | |
| 45 | /* |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 46 | * IRQ handler for the timer. |
| 47 | */ |
Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 48 | static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 49 | { |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 50 | u32 sr; |
| 51 | |
| 52 | regmap_read(regmap_st, AT91_ST_SR, &sr); |
| 53 | sr &= irqmask; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 54 | |
Uwe Kleine-König | 501d703 | 2009-09-21 09:30:09 +0200 | [diff] [blame] | 55 | /* |
| 56 | * irqs should be disabled here, but as the irq is shared they are only |
| 57 | * guaranteed to be off if the timer irq is registered first. |
| 58 | */ |
| 59 | WARN_ON_ONCE(!irqs_disabled()); |
| 60 | |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 61 | /* simulate "oneshot" timer with alarm */ |
| 62 | if (sr & AT91_ST_ALMS) { |
| 63 | clkevt.event_handler(&clkevt); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 64 | return IRQ_HANDLED; |
| 65 | } |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 66 | |
| 67 | /* periodic mode should handle delayed ticks */ |
| 68 | if (sr & AT91_ST_PITS) { |
| 69 | u32 crtr = read_CRTR(); |
| 70 | |
Alexandre Belloni | 216ab8f | 2015-08-16 11:23:44 +0200 | [diff] [blame] | 71 | while (((crtr - last_crtr) & AT91_ST_CRTV) >= timer_latch) { |
| 72 | last_crtr += timer_latch; |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 73 | clkevt.event_handler(&clkevt); |
| 74 | } |
| 75 | return IRQ_HANDLED; |
| 76 | } |
| 77 | |
| 78 | /* this irq is shared ... */ |
| 79 | return IRQ_NONE; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 82 | static u64 read_clk32k(struct clocksource *cs) |
Andrew Victor | 2a6f990 | 2006-06-19 15:26:50 +0100 | [diff] [blame] | 83 | { |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 84 | return read_CRTR(); |
Andrew Victor | 2a6f990 | 2006-06-19 15:26:50 +0100 | [diff] [blame] | 85 | } |
| 86 | |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 87 | static struct clocksource clk32k = { |
| 88 | .name = "32k_counter", |
| 89 | .rating = 150, |
| 90 | .read = read_clk32k, |
| 91 | .mask = CLOCKSOURCE_MASK(20), |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 92 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 93 | }; |
| 94 | |
Viresh Kumar | 8ab2823 | 2015-06-18 16:24:45 +0530 | [diff] [blame] | 95 | static void clkdev32k_disable_and_flush_irq(void) |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 96 | { |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 97 | unsigned int val; |
| 98 | |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 99 | /* Disable and flush pending timer interrupts */ |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 100 | regmap_write(regmap_st, AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS); |
| 101 | regmap_read(regmap_st, AT91_ST_SR, &val); |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 102 | last_crtr = read_CRTR(); |
Viresh Kumar | 8ab2823 | 2015-06-18 16:24:45 +0530 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static int clkevt32k_shutdown(struct clock_event_device *evt) |
| 106 | { |
| 107 | clkdev32k_disable_and_flush_irq(); |
| 108 | irqmask = 0; |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 109 | regmap_write(regmap_st, AT91_ST_IER, irqmask); |
Viresh Kumar | 8ab2823 | 2015-06-18 16:24:45 +0530 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int clkevt32k_set_oneshot(struct clock_event_device *dev) |
| 114 | { |
| 115 | clkdev32k_disable_and_flush_irq(); |
| 116 | |
| 117 | /* |
| 118 | * ALM for oneshot irqs, set by next_event() |
| 119 | * before 32 seconds have passed. |
| 120 | */ |
| 121 | irqmask = AT91_ST_ALMS; |
| 122 | regmap_write(regmap_st, AT91_ST_RTAR, last_crtr); |
| 123 | regmap_write(regmap_st, AT91_ST_IER, irqmask); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int clkevt32k_set_periodic(struct clock_event_device *dev) |
| 128 | { |
| 129 | clkdev32k_disable_and_flush_irq(); |
| 130 | |
| 131 | /* PIT for periodic irqs; fixed rate of 1/HZ */ |
| 132 | irqmask = AT91_ST_PITS; |
Alexandre Belloni | 216ab8f | 2015-08-16 11:23:44 +0200 | [diff] [blame] | 133 | regmap_write(regmap_st, AT91_ST_PIMR, timer_latch); |
Viresh Kumar | 8ab2823 | 2015-06-18 16:24:45 +0530 | [diff] [blame] | 134 | regmap_write(regmap_st, AT91_ST_IER, irqmask); |
| 135 | return 0; |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static int |
| 139 | clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev) |
| 140 | { |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 141 | u32 alm; |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 142 | unsigned int val; |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 143 | |
| 144 | BUG_ON(delta < 2); |
| 145 | |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 146 | /* The alarm IRQ uses absolute time (now+delta), not the relative |
| 147 | * time (delta) in our calling convention. Like all clockevents |
| 148 | * using such "match" hardware, we have a race to defend against. |
| 149 | * |
| 150 | * Our defense here is to have set up the clockevent device so the |
| 151 | * delta is at least two. That way we never end up writing RTAR |
| 152 | * with the value then held in CRTR ... which would mean the match |
| 153 | * wouldn't trigger until 32 seconds later, after CRTR wraps. |
| 154 | */ |
| 155 | alm = read_CRTR(); |
| 156 | |
| 157 | /* Cancel any pending alarm; flush any pending IRQ */ |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 158 | regmap_write(regmap_st, AT91_ST_RTAR, alm); |
| 159 | regmap_read(regmap_st, AT91_ST_SR, &val); |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 160 | |
| 161 | /* Schedule alarm by writing RTAR. */ |
| 162 | alm += delta; |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 163 | regmap_write(regmap_st, AT91_ST_RTAR, alm); |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 164 | |
Jason Yan | 8c42c0f7 | 2020-04-14 20:02:38 +0800 | [diff] [blame] | 165 | return 0; |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static struct clock_event_device clkevt = { |
Viresh Kumar | 8ab2823 | 2015-06-18 16:24:45 +0530 | [diff] [blame] | 169 | .name = "at91_tick", |
| 170 | .features = CLOCK_EVT_FEAT_PERIODIC | |
| 171 | CLOCK_EVT_FEAT_ONESHOT, |
| 172 | .rating = 150, |
| 173 | .set_next_event = clkevt32k_next_event, |
| 174 | .set_state_shutdown = clkevt32k_shutdown, |
| 175 | .set_state_periodic = clkevt32k_set_periodic, |
| 176 | .set_state_oneshot = clkevt32k_set_oneshot, |
| 177 | .tick_resume = clkevt32k_shutdown, |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 178 | }; |
| 179 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 180 | /* |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 181 | * ST (system timer) module supports both clockevents and clocksource. |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 182 | */ |
Daniel Lezcano | adbaf52 | 2016-06-06 19:11:12 +0200 | [diff] [blame] | 183 | static int __init atmel_st_timer_init(struct device_node *node) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 184 | { |
Alexandre Belloni | 216ab8f | 2015-08-16 11:23:44 +0200 | [diff] [blame] | 185 | struct clk *sclk; |
| 186 | unsigned int sclk_rate, val; |
Alexandre Belloni | 0afb46b | 2015-03-13 11:54:37 +0100 | [diff] [blame] | 187 | int irq, ret; |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 188 | |
| 189 | regmap_st = syscon_node_to_regmap(node); |
Daniel Lezcano | adbaf52 | 2016-06-06 19:11:12 +0200 | [diff] [blame] | 190 | if (IS_ERR(regmap_st)) { |
| 191 | pr_err("Unable to get regmap\n"); |
| 192 | return PTR_ERR(regmap_st); |
| 193 | } |
Joachim Eastwood | 454c46d | 2012-10-28 18:31:07 +0000 | [diff] [blame] | 194 | |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 195 | /* Disable all timer interrupts, and clear any pending ones */ |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 196 | regmap_write(regmap_st, AT91_ST_IDR, |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 197 | AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS); |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 198 | regmap_read(regmap_st, AT91_ST_SR, &val); |
| 199 | |
| 200 | /* Get the interrupts property */ |
Alexandre Belloni | 0afb46b | 2015-03-13 11:54:37 +0100 | [diff] [blame] | 201 | irq = irq_of_parse_and_map(node, 0); |
Daniel Lezcano | adbaf52 | 2016-06-06 19:11:12 +0200 | [diff] [blame] | 202 | if (!irq) { |
| 203 | pr_err("Unable to get IRQ from DT\n"); |
| 204 | return -EINVAL; |
| 205 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 206 | |
Andrew Victor | 2a6f990 | 2006-06-19 15:26:50 +0100 | [diff] [blame] | 207 | /* Make IRQs happen for the system timer */ |
Alexandre Belloni | 0afb46b | 2015-03-13 11:54:37 +0100 | [diff] [blame] | 208 | ret = request_irq(irq, at91rm9200_timer_interrupt, |
| 209 | IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL, |
| 210 | "at91_tick", regmap_st); |
Daniel Lezcano | adbaf52 | 2016-06-06 19:11:12 +0200 | [diff] [blame] | 211 | if (ret) { |
| 212 | pr_err("Unable to setup IRQ\n"); |
| 213 | return ret; |
| 214 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 215 | |
Alexandre Belloni | 216ab8f | 2015-08-16 11:23:44 +0200 | [diff] [blame] | 216 | sclk = of_clk_get(node, 0); |
Daniel Lezcano | adbaf52 | 2016-06-06 19:11:12 +0200 | [diff] [blame] | 217 | if (IS_ERR(sclk)) { |
| 218 | pr_err("Unable to get slow clock\n"); |
| 219 | return PTR_ERR(sclk); |
| 220 | } |
Alexandre Belloni | 216ab8f | 2015-08-16 11:23:44 +0200 | [diff] [blame] | 221 | |
Daniel Lezcano | adbaf52 | 2016-06-06 19:11:12 +0200 | [diff] [blame] | 222 | ret = clk_prepare_enable(sclk); |
| 223 | if (ret) { |
| 224 | pr_err("Could not enable slow clock\n"); |
| 225 | return ret; |
| 226 | } |
Alexandre Belloni | 216ab8f | 2015-08-16 11:23:44 +0200 | [diff] [blame] | 227 | |
| 228 | sclk_rate = clk_get_rate(sclk); |
Daniel Lezcano | adbaf52 | 2016-06-06 19:11:12 +0200 | [diff] [blame] | 229 | if (!sclk_rate) { |
| 230 | pr_err("Invalid slow clock rate\n"); |
| 231 | return -EINVAL; |
| 232 | } |
Alexandre Belloni | 216ab8f | 2015-08-16 11:23:44 +0200 | [diff] [blame] | 233 | timer_latch = (sclk_rate + HZ / 2) / HZ; |
| 234 | |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 235 | /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used |
| 236 | * directly for the clocksource and all clockevents, after adjusting |
| 237 | * its prescaler from the 1 Hz default. |
| 238 | */ |
Alexandre Belloni | adf2edf | 2015-03-12 13:07:32 +0100 | [diff] [blame] | 239 | regmap_write(regmap_st, AT91_ST_RTMR, 1); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 240 | |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 241 | /* Setup timer clockevent, with minimum of two ticks (important!!) */ |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 242 | clkevt.cpumask = cpumask_of(0); |
Alexandre Belloni | 216ab8f | 2015-08-16 11:23:44 +0200 | [diff] [blame] | 243 | clockevents_config_and_register(&clkevt, sclk_rate, |
Uwe Kleine-König | 1c28353 | 2013-10-08 16:38:53 +0200 | [diff] [blame] | 244 | 2, AT91_ST_ALMV); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 245 | |
David Brownell | 5e802df | 2007-07-31 01:41:26 +0100 | [diff] [blame] | 246 | /* register clocksource */ |
Daniel Lezcano | adbaf52 | 2016-06-06 19:11:12 +0200 | [diff] [blame] | 247 | return clocksource_register_hz(&clk32k, sclk_rate); |
Andrew Victor | 2a6f990 | 2006-06-19 15:26:50 +0100 | [diff] [blame] | 248 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 249 | TIMER_OF_DECLARE(atmel_st_timer, "atmel,at91rm9200-st", |
Alexandre Belloni | bbfc97e | 2015-03-12 13:07:30 +0100 | [diff] [blame] | 250 | atmel_st_timer_init); |