Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 2 | #include <linux/init.h> |
| 3 | #include <linux/clocksource.h> |
| 4 | #include <linux/clockchips.h> |
| 5 | #include <linux/interrupt.h> |
| 6 | #include <linux/irq.h> |
| 7 | |
| 8 | #include <linux/clk.h> |
Alexandre Belloni | 1ce861c | 2019-08-13 15:30:50 +0200 | [diff] [blame] | 9 | #include <linux/delay.h> |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 10 | #include <linux/err.h> |
| 11 | #include <linux/ioport.h> |
| 12 | #include <linux/io.h> |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 13 | #include <linux/of_address.h> |
| 14 | #include <linux/of_irq.h> |
Alexandre Belloni | f712a1e | 2019-04-26 23:47:12 +0200 | [diff] [blame] | 15 | #include <linux/sched_clock.h> |
Alexandre Belloni | 2a515e5 | 2017-05-12 20:22:51 +0200 | [diff] [blame] | 16 | #include <linux/syscore_ops.h> |
Alexandre Belloni | c2c9136 | 2019-04-26 23:47:10 +0200 | [diff] [blame] | 17 | #include <soc/at91/atmel_tcb.h> |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 18 | |
| 19 | |
| 20 | /* |
| 21 | * We're configured to use a specific TC block, one that's not hooked |
| 22 | * up to external hardware, to provide a time solution: |
| 23 | * |
| 24 | * - Two channels combine to create a free-running 32 bit counter |
| 25 | * with a base rate of 5+ MHz, packaged as a clocksource (with |
| 26 | * resolution better than 200 nsec). |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 27 | * - Some chips support 32 bit counter. A single channel is used for |
| 28 | * this 32 bit free-running counter. the second channel is not used. |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 29 | * |
| 30 | * - The third channel may be used to provide a 16-bit clockevent |
| 31 | * source, used in either periodic or oneshot mode. This runs |
| 32 | * at 32 KiHZ, and can handle delays of up to two seconds. |
| 33 | * |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 34 | * REVISIT behavior during system suspend states... we should disable |
| 35 | * all clocks and save the power. Easily done for clockevent devices, |
| 36 | * but clocksources won't necessarily get the needed notifications. |
| 37 | * For deeper system sleep states, this will be mandatory... |
| 38 | */ |
| 39 | |
| 40 | static void __iomem *tcaddr; |
Alexandre Belloni | 2a515e5 | 2017-05-12 20:22:51 +0200 | [diff] [blame] | 41 | static struct |
| 42 | { |
| 43 | u32 cmr; |
| 44 | u32 imr; |
| 45 | u32 rc; |
| 46 | bool clken; |
| 47 | } tcb_cache[3]; |
| 48 | static u32 bmr_cache; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 49 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 50 | static u64 tc_get_cycles(struct clocksource *cs) |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 51 | { |
| 52 | unsigned long flags; |
| 53 | u32 lower, upper; |
| 54 | |
| 55 | raw_local_irq_save(flags); |
| 56 | do { |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 57 | upper = readl_relaxed(tcaddr + ATMEL_TC_REG(1, CV)); |
| 58 | lower = readl_relaxed(tcaddr + ATMEL_TC_REG(0, CV)); |
| 59 | } while (upper != readl_relaxed(tcaddr + ATMEL_TC_REG(1, CV))); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 60 | |
| 61 | raw_local_irq_restore(flags); |
| 62 | return (upper << 16) | lower; |
| 63 | } |
| 64 | |
David Engraf | 7b9f1d1 | 2017-01-11 14:50:59 +0100 | [diff] [blame] | 65 | static u64 tc_get_cycles32(struct clocksource *cs) |
| 66 | { |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 67 | return readl_relaxed(tcaddr + ATMEL_TC_REG(0, CV)); |
David Engraf | 7b9f1d1 | 2017-01-11 14:50:59 +0100 | [diff] [blame] | 68 | } |
| 69 | |
kbuild test robot | 7ebe681 | 2019-04-26 23:47:17 +0200 | [diff] [blame] | 70 | static void tc_clksrc_suspend(struct clocksource *cs) |
Alexandre Belloni | 2a515e5 | 2017-05-12 20:22:51 +0200 | [diff] [blame] | 71 | { |
| 72 | int i; |
| 73 | |
| 74 | for (i = 0; i < ARRAY_SIZE(tcb_cache); i++) { |
| 75 | tcb_cache[i].cmr = readl(tcaddr + ATMEL_TC_REG(i, CMR)); |
| 76 | tcb_cache[i].imr = readl(tcaddr + ATMEL_TC_REG(i, IMR)); |
| 77 | tcb_cache[i].rc = readl(tcaddr + ATMEL_TC_REG(i, RC)); |
| 78 | tcb_cache[i].clken = !!(readl(tcaddr + ATMEL_TC_REG(i, SR)) & |
| 79 | ATMEL_TC_CLKSTA); |
| 80 | } |
| 81 | |
| 82 | bmr_cache = readl(tcaddr + ATMEL_TC_BMR); |
| 83 | } |
| 84 | |
kbuild test robot | 7ebe681 | 2019-04-26 23:47:17 +0200 | [diff] [blame] | 85 | static void tc_clksrc_resume(struct clocksource *cs) |
Alexandre Belloni | 2a515e5 | 2017-05-12 20:22:51 +0200 | [diff] [blame] | 86 | { |
| 87 | int i; |
| 88 | |
| 89 | for (i = 0; i < ARRAY_SIZE(tcb_cache); i++) { |
| 90 | /* Restore registers for the channel, RA and RB are not used */ |
| 91 | writel(tcb_cache[i].cmr, tcaddr + ATMEL_TC_REG(i, CMR)); |
| 92 | writel(tcb_cache[i].rc, tcaddr + ATMEL_TC_REG(i, RC)); |
| 93 | writel(0, tcaddr + ATMEL_TC_REG(i, RA)); |
| 94 | writel(0, tcaddr + ATMEL_TC_REG(i, RB)); |
| 95 | /* Disable all the interrupts */ |
| 96 | writel(0xff, tcaddr + ATMEL_TC_REG(i, IDR)); |
| 97 | /* Reenable interrupts that were enabled before suspending */ |
| 98 | writel(tcb_cache[i].imr, tcaddr + ATMEL_TC_REG(i, IER)); |
| 99 | /* Start the clock if it was used */ |
| 100 | if (tcb_cache[i].clken) |
| 101 | writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(i, CCR)); |
| 102 | } |
| 103 | |
| 104 | /* Dual channel, chain channels */ |
| 105 | writel(bmr_cache, tcaddr + ATMEL_TC_BMR); |
| 106 | /* Finally, trigger all the channels*/ |
| 107 | writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR); |
| 108 | } |
| 109 | |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 110 | static struct clocksource clksrc = { |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 111 | .rating = 200, |
| 112 | .read = tc_get_cycles, |
| 113 | .mask = CLOCKSOURCE_MASK(32), |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 114 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
Alexandre Belloni | 2a515e5 | 2017-05-12 20:22:51 +0200 | [diff] [blame] | 115 | .suspend = tc_clksrc_suspend, |
| 116 | .resume = tc_clksrc_resume, |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
Alexandre Belloni | f712a1e | 2019-04-26 23:47:12 +0200 | [diff] [blame] | 119 | static u64 notrace tc_sched_clock_read(void) |
| 120 | { |
| 121 | return tc_get_cycles(&clksrc); |
| 122 | } |
| 123 | |
| 124 | static u64 notrace tc_sched_clock_read32(void) |
| 125 | { |
| 126 | return tc_get_cycles32(&clksrc); |
| 127 | } |
| 128 | |
Alexandre Belloni | 1ce861c | 2019-08-13 15:30:50 +0200 | [diff] [blame] | 129 | static struct delay_timer tc_delay_timer; |
| 130 | |
| 131 | static unsigned long tc_delay_timer_read(void) |
| 132 | { |
| 133 | return tc_get_cycles(&clksrc); |
| 134 | } |
| 135 | |
| 136 | static unsigned long notrace tc_delay_timer_read32(void) |
| 137 | { |
| 138 | return tc_get_cycles32(&clksrc); |
| 139 | } |
| 140 | |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 141 | #ifdef CONFIG_GENERIC_CLOCKEVENTS |
| 142 | |
| 143 | struct tc_clkevt_device { |
| 144 | struct clock_event_device clkevt; |
| 145 | struct clk *clk; |
| 146 | void __iomem *regs; |
| 147 | }; |
| 148 | |
| 149 | static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt) |
| 150 | { |
| 151 | return container_of(clkevt, struct tc_clkevt_device, clkevt); |
| 152 | } |
| 153 | |
| 154 | /* For now, we always use the 32K clock ... this optimizes for NO_HZ, |
| 155 | * because using one of the divided clocks would usually mean the |
| 156 | * tick rate can never be less than several dozen Hz (vs 0.5 Hz). |
| 157 | * |
| 158 | * A divided clock could be good for high resolution timers, since |
| 159 | * 30.5 usec resolution can seem "low". |
| 160 | */ |
| 161 | static u32 timer_clock; |
| 162 | |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 163 | static int tc_shutdown(struct clock_event_device *d) |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 164 | { |
| 165 | struct tc_clkevt_device *tcd = to_tc_clkevt(d); |
| 166 | void __iomem *regs = tcd->regs; |
| 167 | |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 168 | writel(0xff, regs + ATMEL_TC_REG(2, IDR)); |
| 169 | writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR)); |
Alexandre Belloni | f02b4b7 | 2016-01-15 11:34:21 +0100 | [diff] [blame] | 170 | if (!clockevent_state_detached(d)) |
| 171 | clk_disable(tcd->clk); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 172 | |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int tc_set_oneshot(struct clock_event_device *d) |
| 177 | { |
| 178 | struct tc_clkevt_device *tcd = to_tc_clkevt(d); |
| 179 | void __iomem *regs = tcd->regs; |
| 180 | |
| 181 | if (clockevent_state_oneshot(d) || clockevent_state_periodic(d)) |
| 182 | tc_shutdown(d); |
| 183 | |
| 184 | clk_enable(tcd->clk); |
| 185 | |
| 186 | /* slow clock, count up to RC, then irq and stop */ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 187 | writel(timer_clock | ATMEL_TC_CPCSTOP | ATMEL_TC_WAVE | |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 188 | ATMEL_TC_WAVESEL_UP_AUTO, regs + ATMEL_TC_REG(2, CMR)); |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 189 | writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER)); |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 190 | |
| 191 | /* set_next_event() configures and starts the timer */ |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int tc_set_periodic(struct clock_event_device *d) |
| 196 | { |
| 197 | struct tc_clkevt_device *tcd = to_tc_clkevt(d); |
| 198 | void __iomem *regs = tcd->regs; |
| 199 | |
| 200 | if (clockevent_state_oneshot(d) || clockevent_state_periodic(d)) |
| 201 | tc_shutdown(d); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 202 | |
| 203 | /* By not making the gentime core emulate periodic mode on top |
| 204 | * of oneshot, we get lower overhead and improved accuracy. |
| 205 | */ |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 206 | clk_enable(tcd->clk); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 207 | |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 208 | /* slow clock, count up to RC, then irq and restart */ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 209 | writel(timer_clock | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO, |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 210 | regs + ATMEL_TC_REG(2, CMR)); |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 211 | writel((32768 + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC)); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 212 | |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 213 | /* Enable clock and interrupts on RC compare */ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 214 | writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER)); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 215 | |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 216 | /* go go gadget! */ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 217 | writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG, regs + |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 218 | ATMEL_TC_REG(2, CCR)); |
| 219 | return 0; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static int tc_next_event(unsigned long delta, struct clock_event_device *d) |
| 223 | { |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 224 | writel_relaxed(delta, tcaddr + ATMEL_TC_REG(2, RC)); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 225 | |
| 226 | /* go go gadget! */ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 227 | writel_relaxed(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG, |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 228 | tcaddr + ATMEL_TC_REG(2, CCR)); |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static struct tc_clkevt_device clkevt = { |
| 233 | .clkevt = { |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 234 | .features = CLOCK_EVT_FEAT_PERIODIC | |
| 235 | CLOCK_EVT_FEAT_ONESHOT, |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 236 | /* Should be lower than at91rm9200's system timer */ |
Viresh Kumar | cf4541c | 2015-06-18 16:24:38 +0530 | [diff] [blame] | 237 | .rating = 125, |
| 238 | .set_next_event = tc_next_event, |
| 239 | .set_state_shutdown = tc_shutdown, |
| 240 | .set_state_periodic = tc_set_periodic, |
| 241 | .set_state_oneshot = tc_set_oneshot, |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 242 | }, |
| 243 | }; |
| 244 | |
| 245 | static irqreturn_t ch2_irq(int irq, void *handle) |
| 246 | { |
| 247 | struct tc_clkevt_device *dev = handle; |
| 248 | unsigned int sr; |
| 249 | |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 250 | sr = readl_relaxed(dev->regs + ATMEL_TC_REG(2, SR)); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 251 | if (sr & ATMEL_TC_CPCS) { |
| 252 | dev->clkevt.event_handler(&dev->clkevt); |
| 253 | return IRQ_HANDLED; |
| 254 | } |
| 255 | |
| 256 | return IRQ_NONE; |
| 257 | } |
| 258 | |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 259 | static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx) |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 260 | { |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 261 | int ret; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 262 | struct clk *t2_clk = tc->clk[2]; |
| 263 | int irq = tc->irq[2]; |
| 264 | |
Boris Brezillon | 7d8d05d | 2015-08-16 11:23:46 +0200 | [diff] [blame] | 265 | ret = clk_prepare_enable(tc->slow_clk); |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 266 | if (ret) |
| 267 | return ret; |
Boris Brezillon | 7d8d05d | 2015-08-16 11:23:46 +0200 | [diff] [blame] | 268 | |
| 269 | /* try to enable t2 clk to avoid future errors in mode change */ |
| 270 | ret = clk_prepare_enable(t2_clk); |
| 271 | if (ret) { |
| 272 | clk_disable_unprepare(tc->slow_clk); |
| 273 | return ret; |
| 274 | } |
| 275 | |
David Jander | acbf6d2 | 2014-05-08 12:06:25 +0200 | [diff] [blame] | 276 | clk_disable(t2_clk); |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 277 | |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 278 | clkevt.regs = tc->regs; |
| 279 | clkevt.clk = t2_clk; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 280 | |
| 281 | timer_clock = clk32k_divisor_idx; |
| 282 | |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 283 | clkevt.clkevt.cpumask = cpumask_of(0); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 284 | |
Gaël PORTAY | d07a1ec | 2014-09-06 19:52:37 +0200 | [diff] [blame] | 285 | ret = request_irq(irq, ch2_irq, IRQF_TIMER, "tc_clkevt", &clkevt); |
| 286 | if (ret) { |
Boris Brezillon | eed9fb9 | 2015-08-16 11:23:45 +0200 | [diff] [blame] | 287 | clk_unprepare(t2_clk); |
Boris Brezillon | 7d8d05d | 2015-08-16 11:23:46 +0200 | [diff] [blame] | 288 | clk_disable_unprepare(tc->slow_clk); |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 289 | return ret; |
Gaël PORTAY | d07a1ec | 2014-09-06 19:52:37 +0200 | [diff] [blame] | 290 | } |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 291 | |
Shawn Guo | 77cc982 | 2013-01-12 11:50:06 +0000 | [diff] [blame] | 292 | clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff); |
Voss, Nikolaus | 1817dc0 | 2011-01-25 15:07:29 -0800 | [diff] [blame] | 293 | |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 294 | return ret; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | #else /* !CONFIG_GENERIC_CLOCKEVENTS */ |
| 298 | |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 299 | static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx) |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 300 | { |
| 301 | /* NOTHING */ |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 302 | return 0; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | #endif |
| 306 | |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 307 | static void __init tcb_setup_dual_chan(struct atmel_tc *tc, int mck_divisor_idx) |
| 308 | { |
| 309 | /* channel 0: waveform mode, input mclk/8, clock TIOA0 on overflow */ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 310 | writel(mck_divisor_idx /* likely divide-by-8 */ |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 311 | | ATMEL_TC_WAVE |
| 312 | | ATMEL_TC_WAVESEL_UP /* free-run */ |
| 313 | | ATMEL_TC_ACPA_SET /* TIOA0 rises at 0 */ |
| 314 | | ATMEL_TC_ACPC_CLEAR, /* (duty cycle 50%) */ |
| 315 | tcaddr + ATMEL_TC_REG(0, CMR)); |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 316 | writel(0x0000, tcaddr + ATMEL_TC_REG(0, RA)); |
| 317 | writel(0x8000, tcaddr + ATMEL_TC_REG(0, RC)); |
| 318 | writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */ |
| 319 | writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR)); |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 320 | |
| 321 | /* channel 1: waveform mode, input TIOA0 */ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 322 | writel(ATMEL_TC_XC1 /* input: TIOA0 */ |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 323 | | ATMEL_TC_WAVE |
| 324 | | ATMEL_TC_WAVESEL_UP, /* free-run */ |
| 325 | tcaddr + ATMEL_TC_REG(1, CMR)); |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 326 | writel(0xff, tcaddr + ATMEL_TC_REG(1, IDR)); /* no irqs */ |
| 327 | writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(1, CCR)); |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 328 | |
| 329 | /* chain channel 0 to channel 1*/ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 330 | writel(ATMEL_TC_TC1XC1S_TIOA0, tcaddr + ATMEL_TC_BMR); |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 331 | /* then reset all the timers */ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 332 | writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR); |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static void __init tcb_setup_single_chan(struct atmel_tc *tc, int mck_divisor_idx) |
| 336 | { |
| 337 | /* channel 0: waveform mode, input mclk/8 */ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 338 | writel(mck_divisor_idx /* likely divide-by-8 */ |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 339 | | ATMEL_TC_WAVE |
| 340 | | ATMEL_TC_WAVESEL_UP, /* free-run */ |
| 341 | tcaddr + ATMEL_TC_REG(0, CMR)); |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 342 | writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */ |
| 343 | writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR)); |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 344 | |
| 345 | /* then reset all the timers */ |
Alexandre Belloni | 6ec8be2 | 2017-06-23 17:03:31 +0200 | [diff] [blame] | 346 | writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR); |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 349 | static const u8 atmel_tcb_divisors[5] = { 2, 8, 32, 128, 0, }; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 350 | |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 351 | static const struct of_device_id atmel_tcb_of_match[] = { |
| 352 | { .compatible = "atmel,at91rm9200-tcb", .data = (void *)16, }, |
| 353 | { .compatible = "atmel,at91sam9x5-tcb", .data = (void *)32, }, |
| 354 | { /* sentinel */ } |
| 355 | }; |
| 356 | |
| 357 | static int __init tcb_clksrc_init(struct device_node *node) |
| 358 | { |
| 359 | struct atmel_tc tc; |
David Brownell | 3ee08ae | 2008-03-13 09:44:48 -0800 | [diff] [blame] | 360 | struct clk *t0_clk; |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 361 | const struct of_device_id *match; |
Alexandre Belloni | f712a1e | 2019-04-26 23:47:12 +0200 | [diff] [blame] | 362 | u64 (*tc_sched_clock)(void); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 363 | u32 rate, divided_rate = 0; |
| 364 | int best_divisor_idx = -1; |
| 365 | int clk32k_divisor_idx = -1; |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 366 | int bits; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 367 | int i; |
Boris BREZILLON | 0e746ec | 2013-10-02 14:35:20 +0200 | [diff] [blame] | 368 | int ret; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 369 | |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 370 | /* Protect against multiple calls */ |
| 371 | if (tcaddr) |
| 372 | return 0; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 373 | |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 374 | tc.regs = of_iomap(node->parent, 0); |
| 375 | if (!tc.regs) |
| 376 | return -ENXIO; |
| 377 | |
| 378 | t0_clk = of_clk_get_by_name(node->parent, "t0_clk"); |
| 379 | if (IS_ERR(t0_clk)) |
| 380 | return PTR_ERR(t0_clk); |
| 381 | |
| 382 | tc.slow_clk = of_clk_get_by_name(node->parent, "slow_clk"); |
| 383 | if (IS_ERR(tc.slow_clk)) |
| 384 | return PTR_ERR(tc.slow_clk); |
| 385 | |
| 386 | tc.clk[0] = t0_clk; |
| 387 | tc.clk[1] = of_clk_get_by_name(node->parent, "t1_clk"); |
| 388 | if (IS_ERR(tc.clk[1])) |
| 389 | tc.clk[1] = t0_clk; |
| 390 | tc.clk[2] = of_clk_get_by_name(node->parent, "t2_clk"); |
| 391 | if (IS_ERR(tc.clk[2])) |
| 392 | tc.clk[2] = t0_clk; |
| 393 | |
| 394 | tc.irq[2] = of_irq_get(node->parent, 2); |
| 395 | if (tc.irq[2] <= 0) { |
| 396 | tc.irq[2] = of_irq_get(node->parent, 0); |
| 397 | if (tc.irq[2] <= 0) |
| 398 | return -EINVAL; |
| 399 | } |
| 400 | |
| 401 | match = of_match_node(atmel_tcb_of_match, node->parent); |
| 402 | bits = (uintptr_t)match->data; |
| 403 | |
| 404 | for (i = 0; i < ARRAY_SIZE(tc.irq); i++) |
| 405 | writel(ATMEL_TC_ALL_IRQ, tc.regs + ATMEL_TC_REG(i, IDR)); |
| 406 | |
Boris BREZILLON | 0e746ec | 2013-10-02 14:35:20 +0200 | [diff] [blame] | 407 | ret = clk_prepare_enable(t0_clk); |
| 408 | if (ret) { |
| 409 | pr_debug("can't enable T0 clk\n"); |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 410 | return ret; |
Boris BREZILLON | 0e746ec | 2013-10-02 14:35:20 +0200 | [diff] [blame] | 411 | } |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 412 | |
| 413 | /* How fast will we be counting? Pick something over 5 MHz. */ |
| 414 | rate = (u32) clk_get_rate(t0_clk); |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 415 | for (i = 0; i < ARRAY_SIZE(atmel_tcb_divisors); i++) { |
| 416 | unsigned divisor = atmel_tcb_divisors[i]; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 417 | unsigned tmp; |
| 418 | |
| 419 | /* remember 32 KiHz clock for later */ |
| 420 | if (!divisor) { |
| 421 | clk32k_divisor_idx = i; |
| 422 | continue; |
| 423 | } |
| 424 | |
| 425 | tmp = rate / divisor; |
| 426 | pr_debug("TC: %u / %-3u [%d] --> %u\n", rate, divisor, i, tmp); |
| 427 | if (best_divisor_idx > 0) { |
| 428 | if (tmp < 5 * 1000 * 1000) |
| 429 | continue; |
| 430 | } |
| 431 | divided_rate = tmp; |
| 432 | best_divisor_idx = i; |
| 433 | } |
| 434 | |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 435 | clksrc.name = kbasename(node->parent->full_name); |
| 436 | clkevt.clkevt.name = kbasename(node->parent->full_name); |
| 437 | pr_debug("%s at %d.%03d MHz\n", clksrc.name, divided_rate / 1000000, |
Romain Izard | 542f824 | 2018-01-08 14:28:43 +0100 | [diff] [blame] | 438 | ((divided_rate % 1000000) + 500) / 1000); |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 439 | |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 440 | tcaddr = tc.regs; |
| 441 | |
| 442 | if (bits == 32) { |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 443 | /* use apropriate function to read 32 bit counter */ |
| 444 | clksrc.read = tc_get_cycles32; |
| 445 | /* setup ony channel 0 */ |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 446 | tcb_setup_single_chan(&tc, best_divisor_idx); |
Alexandre Belloni | f712a1e | 2019-04-26 23:47:12 +0200 | [diff] [blame] | 447 | tc_sched_clock = tc_sched_clock_read32; |
Alexandre Belloni | 1ce861c | 2019-08-13 15:30:50 +0200 | [diff] [blame] | 448 | tc_delay_timer.read_current_timer = tc_delay_timer_read32; |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 449 | } else { |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 450 | /* we have three clocks no matter what the |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 451 | * underlying platform supports. |
| 452 | */ |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 453 | ret = clk_prepare_enable(tc.clk[1]); |
Boris BREZILLON | 0e746ec | 2013-10-02 14:35:20 +0200 | [diff] [blame] | 454 | if (ret) { |
| 455 | pr_debug("can't enable T1 clk\n"); |
| 456 | goto err_disable_t0; |
| 457 | } |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 458 | /* setup both channel 0 & 1 */ |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 459 | tcb_setup_dual_chan(&tc, best_divisor_idx); |
Alexandre Belloni | f712a1e | 2019-04-26 23:47:12 +0200 | [diff] [blame] | 460 | tc_sched_clock = tc_sched_clock_read; |
Alexandre Belloni | 1ce861c | 2019-08-13 15:30:50 +0200 | [diff] [blame] | 461 | tc_delay_timer.read_current_timer = tc_delay_timer_read; |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 462 | } |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 463 | |
| 464 | /* and away we go! */ |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 465 | ret = clocksource_register_hz(&clksrc, divided_rate); |
| 466 | if (ret) |
| 467 | goto err_disable_t1; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 468 | |
| 469 | /* channel 2: periodic and oneshot timer support */ |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 470 | ret = setup_clkevents(&tc, clk32k_divisor_idx); |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 471 | if (ret) |
| 472 | goto err_unregister_clksrc; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 473 | |
Alexandre Belloni | f712a1e | 2019-04-26 23:47:12 +0200 | [diff] [blame] | 474 | sched_clock_register(tc_sched_clock, 32, divided_rate); |
| 475 | |
Alexandre Belloni | 1ce861c | 2019-08-13 15:30:50 +0200 | [diff] [blame] | 476 | tc_delay_timer.freq = divided_rate; |
| 477 | register_current_timer_delay(&tc_delay_timer); |
| 478 | |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 479 | return 0; |
Boris BREZILLON | 0e746ec | 2013-10-02 14:35:20 +0200 | [diff] [blame] | 480 | |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 481 | err_unregister_clksrc: |
| 482 | clocksource_unregister(&clksrc); |
| 483 | |
| 484 | err_disable_t1: |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 485 | if (bits != 32) |
| 486 | clk_disable_unprepare(tc.clk[1]); |
Boris BREZILLON | 5b3c11d | 2013-10-02 14:35:41 +0200 | [diff] [blame] | 487 | |
Boris BREZILLON | 0e746ec | 2013-10-02 14:35:20 +0200 | [diff] [blame] | 488 | err_disable_t0: |
| 489 | clk_disable_unprepare(t0_clk); |
| 490 | |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 491 | tcaddr = NULL; |
| 492 | |
Boris BREZILLON | 0e746ec | 2013-10-02 14:35:20 +0200 | [diff] [blame] | 493 | return ret; |
David Brownell | 4d243f9 | 2008-02-22 17:28:37 -0800 | [diff] [blame] | 494 | } |
Alexandre Belloni | 86232bf | 2019-04-26 23:47:11 +0200 | [diff] [blame] | 495 | TIMER_OF_DECLARE(atmel_tcb_clksrc, "atmel,tcb-timer", tcb_clksrc_init); |