Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 2 | /* |
Linus Walleij | f5bf0ee | 2017-03-24 22:32:34 +0100 | [diff] [blame] | 3 | * Faraday Technology FTTMR010 timer driver |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 4 | * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org> |
| 5 | * |
| 6 | * Based on a rewrite of arch/arm/mach-gemini/timer.c: |
| 7 | * Copyright (C) 2001-2006 Storlink, Corp. |
| 8 | * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt> |
| 9 | */ |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/of.h> |
| 13 | #include <linux/of_address.h> |
| 14 | #include <linux/of_irq.h> |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 15 | #include <linux/clockchips.h> |
| 16 | #include <linux/clocksource.h> |
| 17 | #include <linux/sched_clock.h> |
Linus Walleij | 28e71e2 | 2017-03-24 22:32:35 +0100 | [diff] [blame] | 18 | #include <linux/clk.h> |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 19 | #include <linux/slab.h> |
Linus Walleij | d0d76d5 | 2017-05-18 22:17:02 +0200 | [diff] [blame] | 20 | #include <linux/bitops.h> |
Linus Walleij | 385c98f | 2017-06-11 23:26:17 +0200 | [diff] [blame] | 21 | #include <linux/delay.h> |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Register definitions for the timers |
| 25 | */ |
| 26 | #define TIMER1_COUNT (0x00) |
| 27 | #define TIMER1_LOAD (0x04) |
| 28 | #define TIMER1_MATCH1 (0x08) |
| 29 | #define TIMER1_MATCH2 (0x0c) |
| 30 | #define TIMER2_COUNT (0x10) |
| 31 | #define TIMER2_LOAD (0x14) |
| 32 | #define TIMER2_MATCH1 (0x18) |
| 33 | #define TIMER2_MATCH2 (0x1c) |
| 34 | #define TIMER3_COUNT (0x20) |
| 35 | #define TIMER3_LOAD (0x24) |
| 36 | #define TIMER3_MATCH1 (0x28) |
| 37 | #define TIMER3_MATCH2 (0x2c) |
| 38 | #define TIMER_CR (0x30) |
| 39 | #define TIMER_INTR_STATE (0x34) |
| 40 | #define TIMER_INTR_MASK (0x38) |
| 41 | |
Linus Walleij | d0d76d5 | 2017-05-18 22:17:02 +0200 | [diff] [blame] | 42 | #define TIMER_1_CR_ENABLE BIT(0) |
| 43 | #define TIMER_1_CR_CLOCK BIT(1) |
| 44 | #define TIMER_1_CR_INT BIT(2) |
| 45 | #define TIMER_2_CR_ENABLE BIT(3) |
| 46 | #define TIMER_2_CR_CLOCK BIT(4) |
| 47 | #define TIMER_2_CR_INT BIT(5) |
| 48 | #define TIMER_3_CR_ENABLE BIT(6) |
| 49 | #define TIMER_3_CR_CLOCK BIT(7) |
| 50 | #define TIMER_3_CR_INT BIT(8) |
| 51 | #define TIMER_1_CR_UPDOWN BIT(9) |
| 52 | #define TIMER_2_CR_UPDOWN BIT(10) |
| 53 | #define TIMER_3_CR_UPDOWN BIT(11) |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 54 | |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 55 | /* |
| 56 | * The Aspeed AST2400 moves bits around in the control register |
| 57 | * and lacks bits for setting the timer to count upwards. |
| 58 | */ |
| 59 | #define TIMER_1_CR_ASPEED_ENABLE BIT(0) |
| 60 | #define TIMER_1_CR_ASPEED_CLOCK BIT(1) |
| 61 | #define TIMER_1_CR_ASPEED_INT BIT(2) |
| 62 | #define TIMER_2_CR_ASPEED_ENABLE BIT(4) |
| 63 | #define TIMER_2_CR_ASPEED_CLOCK BIT(5) |
| 64 | #define TIMER_2_CR_ASPEED_INT BIT(6) |
| 65 | #define TIMER_3_CR_ASPEED_ENABLE BIT(8) |
| 66 | #define TIMER_3_CR_ASPEED_CLOCK BIT(9) |
| 67 | #define TIMER_3_CR_ASPEED_INT BIT(10) |
| 68 | |
Linus Walleij | d0d76d5 | 2017-05-18 22:17:02 +0200 | [diff] [blame] | 69 | #define TIMER_1_INT_MATCH1 BIT(0) |
| 70 | #define TIMER_1_INT_MATCH2 BIT(1) |
| 71 | #define TIMER_1_INT_OVERFLOW BIT(2) |
| 72 | #define TIMER_2_INT_MATCH1 BIT(3) |
| 73 | #define TIMER_2_INT_MATCH2 BIT(4) |
| 74 | #define TIMER_2_INT_OVERFLOW BIT(5) |
| 75 | #define TIMER_3_INT_MATCH1 BIT(6) |
| 76 | #define TIMER_3_INT_MATCH2 BIT(7) |
| 77 | #define TIMER_3_INT_OVERFLOW BIT(8) |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 78 | #define TIMER_INT_ALL_MASK 0x1ff |
| 79 | |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 80 | struct fttmr010 { |
| 81 | void __iomem *base; |
| 82 | unsigned int tick_rate; |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 83 | bool count_down; |
| 84 | u32 t1_enable_val; |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 85 | struct clock_event_device clkevt; |
Linus Walleij | 385c98f | 2017-06-11 23:26:17 +0200 | [diff] [blame] | 86 | #ifdef CONFIG_ARM |
| 87 | struct delay_timer delay_timer; |
| 88 | #endif |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
Linus Walleij | 385c98f | 2017-06-11 23:26:17 +0200 | [diff] [blame] | 91 | /* |
| 92 | * A local singleton used by sched_clock and delay timer reads, which are |
| 93 | * fast and stateless |
| 94 | */ |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 95 | static struct fttmr010 *local_fttmr; |
| 96 | |
| 97 | static inline struct fttmr010 *to_fttmr010(struct clock_event_device *evt) |
| 98 | { |
| 99 | return container_of(evt, struct fttmr010, clkevt); |
| 100 | } |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 101 | |
Linus Walleij | 385c98f | 2017-06-11 23:26:17 +0200 | [diff] [blame] | 102 | static unsigned long fttmr010_read_current_timer_up(void) |
| 103 | { |
| 104 | return readl(local_fttmr->base + TIMER2_COUNT); |
| 105 | } |
| 106 | |
| 107 | static unsigned long fttmr010_read_current_timer_down(void) |
| 108 | { |
| 109 | return ~readl(local_fttmr->base + TIMER2_COUNT); |
| 110 | } |
| 111 | |
Linus Walleij | c477990 | 2017-06-13 23:48:13 +0200 | [diff] [blame] | 112 | static u64 notrace fttmr010_read_sched_clock_up(void) |
| 113 | { |
| 114 | return fttmr010_read_current_timer_up(); |
| 115 | } |
| 116 | |
| 117 | static u64 notrace fttmr010_read_sched_clock_down(void) |
| 118 | { |
| 119 | return fttmr010_read_current_timer_down(); |
| 120 | } |
Linus Walleij | 385c98f | 2017-06-11 23:26:17 +0200 | [diff] [blame] | 121 | |
Linus Walleij | f5bf0ee | 2017-03-24 22:32:34 +0100 | [diff] [blame] | 122 | static int fttmr010_timer_set_next_event(unsigned long cycles, |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 123 | struct clock_event_device *evt) |
| 124 | { |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 125 | struct fttmr010 *fttmr010 = to_fttmr010(evt); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 126 | u32 cr; |
| 127 | |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 128 | /* Stop */ |
| 129 | cr = readl(fttmr010->base + TIMER_CR); |
| 130 | cr &= ~fttmr010->t1_enable_val; |
| 131 | writel(cr, fttmr010->base + TIMER_CR); |
| 132 | |
Tao Ren | 4451d3f | 2018-09-19 15:13:31 -0700 | [diff] [blame] | 133 | if (fttmr010->count_down) { |
| 134 | /* |
| 135 | * ASPEED Timer Controller will load TIMER1_LOAD register |
| 136 | * into TIMER1_COUNT register when the timer is re-enabled. |
| 137 | */ |
| 138 | writel(cycles, fttmr010->base + TIMER1_LOAD); |
| 139 | } else { |
| 140 | /* Setup the match register forward in time */ |
| 141 | cr = readl(fttmr010->base + TIMER1_COUNT); |
| 142 | writel(cr + cycles, fttmr010->base + TIMER1_MATCH1); |
| 143 | } |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 144 | |
| 145 | /* Start */ |
| 146 | cr = readl(fttmr010->base + TIMER_CR); |
| 147 | cr |= fttmr010->t1_enable_val; |
| 148 | writel(cr, fttmr010->base + TIMER_CR); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Linus Walleij | f5bf0ee | 2017-03-24 22:32:34 +0100 | [diff] [blame] | 153 | static int fttmr010_timer_shutdown(struct clock_event_device *evt) |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 154 | { |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 155 | struct fttmr010 *fttmr010 = to_fttmr010(evt); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 156 | u32 cr; |
| 157 | |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 158 | /* Stop */ |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 159 | cr = readl(fttmr010->base + TIMER_CR); |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 160 | cr &= ~fttmr010->t1_enable_val; |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 161 | writel(cr, fttmr010->base + TIMER_CR); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int fttmr010_timer_set_oneshot(struct clock_event_device *evt) |
| 167 | { |
| 168 | struct fttmr010 *fttmr010 = to_fttmr010(evt); |
| 169 | u32 cr; |
| 170 | |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 171 | /* Stop */ |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 172 | cr = readl(fttmr010->base + TIMER_CR); |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 173 | cr &= ~fttmr010->t1_enable_val; |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 174 | writel(cr, fttmr010->base + TIMER_CR); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 175 | |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 176 | /* Setup counter start from 0 or ~0 */ |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 177 | writel(0, fttmr010->base + TIMER1_COUNT); |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 178 | if (fttmr010->count_down) |
| 179 | writel(~0, fttmr010->base + TIMER1_LOAD); |
| 180 | else |
| 181 | writel(0, fttmr010->base + TIMER1_LOAD); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 182 | |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 183 | /* Enable interrupt */ |
| 184 | cr = readl(fttmr010->base + TIMER_INTR_MASK); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 185 | cr &= ~(TIMER_1_INT_OVERFLOW | TIMER_1_INT_MATCH2); |
| 186 | cr |= TIMER_1_INT_MATCH1; |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 187 | writel(cr, fttmr010->base + TIMER_INTR_MASK); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 188 | |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
Linus Walleij | f5bf0ee | 2017-03-24 22:32:34 +0100 | [diff] [blame] | 192 | static int fttmr010_timer_set_periodic(struct clock_event_device *evt) |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 193 | { |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 194 | struct fttmr010 *fttmr010 = to_fttmr010(evt); |
| 195 | u32 period = DIV_ROUND_CLOSEST(fttmr010->tick_rate, HZ); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 196 | u32 cr; |
| 197 | |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 198 | /* Stop */ |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 199 | cr = readl(fttmr010->base + TIMER_CR); |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 200 | cr &= ~fttmr010->t1_enable_val; |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 201 | writel(cr, fttmr010->base + TIMER_CR); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 202 | |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 203 | /* Setup timer to fire at 1/HZ intervals. */ |
| 204 | if (fttmr010->count_down) { |
| 205 | writel(period, fttmr010->base + TIMER1_LOAD); |
| 206 | writel(0, fttmr010->base + TIMER1_MATCH1); |
| 207 | } else { |
| 208 | cr = 0xffffffff - (period - 1); |
| 209 | writel(cr, fttmr010->base + TIMER1_COUNT); |
| 210 | writel(cr, fttmr010->base + TIMER1_LOAD); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 211 | |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 212 | /* Enable interrupt on overflow */ |
| 213 | cr = readl(fttmr010->base + TIMER_INTR_MASK); |
| 214 | cr &= ~(TIMER_1_INT_MATCH1 | TIMER_1_INT_MATCH2); |
| 215 | cr |= TIMER_1_INT_OVERFLOW; |
| 216 | writel(cr, fttmr010->base + TIMER_INTR_MASK); |
| 217 | } |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 218 | |
| 219 | /* Start the timer */ |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 220 | cr = readl(fttmr010->base + TIMER_CR); |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 221 | cr |= fttmr010->t1_enable_val; |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 222 | writel(cr, fttmr010->base + TIMER_CR); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 227 | /* |
| 228 | * IRQ handler for the timer |
| 229 | */ |
Linus Walleij | f5bf0ee | 2017-03-24 22:32:34 +0100 | [diff] [blame] | 230 | static irqreturn_t fttmr010_timer_interrupt(int irq, void *dev_id) |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 231 | { |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 232 | struct clock_event_device *evt = dev_id; |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 233 | |
| 234 | evt->event_handler(evt); |
| 235 | return IRQ_HANDLED; |
| 236 | } |
| 237 | |
Daniel Lezcano | ef89718 | 2017-05-26 10:38:07 +0200 | [diff] [blame] | 238 | static int __init fttmr010_common_init(struct device_node *np, bool is_aspeed) |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 239 | { |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 240 | struct fttmr010 *fttmr010; |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 241 | int irq; |
Linus Walleij | dd98442 | 2017-05-18 22:17:00 +0200 | [diff] [blame] | 242 | struct clk *clk; |
| 243 | int ret; |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 244 | u32 val; |
Linus Walleij | dd98442 | 2017-05-18 22:17:00 +0200 | [diff] [blame] | 245 | |
| 246 | /* |
| 247 | * These implementations require a clock reference. |
| 248 | * FIXME: we currently only support clocking using PCLK |
| 249 | * and using EXTCLK is not supported in the driver. |
| 250 | */ |
| 251 | clk = of_clk_get_by_name(np, "PCLK"); |
| 252 | if (IS_ERR(clk)) { |
| 253 | pr_err("could not get PCLK\n"); |
| 254 | return PTR_ERR(clk); |
| 255 | } |
| 256 | ret = clk_prepare_enable(clk); |
| 257 | if (ret) { |
| 258 | pr_err("failed to enable PCLK\n"); |
| 259 | return ret; |
| 260 | } |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 261 | |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 262 | fttmr010 = kzalloc(sizeof(*fttmr010), GFP_KERNEL); |
| 263 | if (!fttmr010) { |
| 264 | ret = -ENOMEM; |
| 265 | goto out_disable_clock; |
| 266 | } |
| 267 | fttmr010->tick_rate = clk_get_rate(clk); |
| 268 | |
| 269 | fttmr010->base = of_iomap(np, 0); |
| 270 | if (!fttmr010->base) { |
Arvind Yadav | 1893428 | 2017-09-25 13:46:39 +0530 | [diff] [blame] | 271 | pr_err("Can't remap registers\n"); |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 272 | ret = -ENXIO; |
| 273 | goto out_free; |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 274 | } |
| 275 | /* IRQ for timer 1 */ |
| 276 | irq = irq_of_parse_and_map(np, 0); |
| 277 | if (irq <= 0) { |
Arvind Yadav | 1893428 | 2017-09-25 13:46:39 +0530 | [diff] [blame] | 278 | pr_err("Can't parse IRQ\n"); |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 279 | ret = -EINVAL; |
| 280 | goto out_unmap; |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 283 | /* |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 284 | * The Aspeed AST2400 moves bits around in the control register, |
| 285 | * otherwise it works the same. |
| 286 | */ |
Daniel Lezcano | ef89718 | 2017-05-26 10:38:07 +0200 | [diff] [blame] | 287 | if (is_aspeed) { |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 288 | fttmr010->t1_enable_val = TIMER_1_CR_ASPEED_ENABLE | |
| 289 | TIMER_1_CR_ASPEED_INT; |
| 290 | /* Downward not available */ |
| 291 | fttmr010->count_down = true; |
| 292 | } else { |
| 293 | fttmr010->t1_enable_val = TIMER_1_CR_ENABLE | TIMER_1_CR_INT; |
| 294 | } |
| 295 | |
| 296 | /* |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 297 | * Reset the interrupt mask and status |
| 298 | */ |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 299 | writel(TIMER_INT_ALL_MASK, fttmr010->base + TIMER_INTR_MASK); |
| 300 | writel(0, fttmr010->base + TIMER_INTR_STATE); |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Enable timer 1 count up, timer 2 count up, except on Aspeed, |
| 304 | * where everything just counts down. |
| 305 | */ |
Daniel Lezcano | ef89718 | 2017-05-26 10:38:07 +0200 | [diff] [blame] | 306 | if (is_aspeed) |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 307 | val = TIMER_2_CR_ASPEED_ENABLE; |
| 308 | else { |
| 309 | val = TIMER_2_CR_ENABLE; |
| 310 | if (!fttmr010->count_down) |
| 311 | val |= TIMER_1_CR_UPDOWN | TIMER_2_CR_UPDOWN; |
| 312 | } |
| 313 | writel(val, fttmr010->base + TIMER_CR); |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 314 | |
| 315 | /* |
| 316 | * Setup free-running clocksource timer (interrupts |
| 317 | * disabled.) |
| 318 | */ |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 319 | local_fttmr = fttmr010; |
Linus Walleij | b589da8 | 2017-05-18 22:17:03 +0200 | [diff] [blame] | 320 | writel(0, fttmr010->base + TIMER2_COUNT); |
Linus Walleij | b589da8 | 2017-05-18 22:17:03 +0200 | [diff] [blame] | 321 | writel(0, fttmr010->base + TIMER2_MATCH1); |
| 322 | writel(0, fttmr010->base + TIMER2_MATCH2); |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 323 | |
| 324 | if (fttmr010->count_down) { |
| 325 | writel(~0, fttmr010->base + TIMER2_LOAD); |
| 326 | clocksource_mmio_init(fttmr010->base + TIMER2_COUNT, |
| 327 | "FTTMR010-TIMER2", |
| 328 | fttmr010->tick_rate, |
| 329 | 300, 32, clocksource_mmio_readl_down); |
Linus Walleij | 740e237 | 2017-06-11 23:26:16 +0200 | [diff] [blame] | 330 | sched_clock_register(fttmr010_read_sched_clock_down, 32, |
| 331 | fttmr010->tick_rate); |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 332 | } else { |
| 333 | writel(0, fttmr010->base + TIMER2_LOAD); |
| 334 | clocksource_mmio_init(fttmr010->base + TIMER2_COUNT, |
| 335 | "FTTMR010-TIMER2", |
| 336 | fttmr010->tick_rate, |
| 337 | 300, 32, clocksource_mmio_readl_up); |
Linus Walleij | 740e237 | 2017-06-11 23:26:16 +0200 | [diff] [blame] | 338 | sched_clock_register(fttmr010_read_sched_clock_up, 32, |
| 339 | fttmr010->tick_rate); |
Linus Walleij | ec14ba1 | 2017-05-18 22:17:04 +0200 | [diff] [blame] | 340 | } |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 341 | |
| 342 | /* |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 343 | * Setup clockevent timer (interrupt-driven) on timer 1. |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 344 | */ |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 345 | writel(0, fttmr010->base + TIMER1_COUNT); |
| 346 | writel(0, fttmr010->base + TIMER1_LOAD); |
| 347 | writel(0, fttmr010->base + TIMER1_MATCH1); |
| 348 | writel(0, fttmr010->base + TIMER1_MATCH2); |
| 349 | ret = request_irq(irq, fttmr010_timer_interrupt, IRQF_TIMER, |
| 350 | "FTTMR010-TIMER1", &fttmr010->clkevt); |
| 351 | if (ret) { |
| 352 | pr_err("FTTMR010-TIMER1 no IRQ\n"); |
| 353 | goto out_unmap; |
| 354 | } |
| 355 | |
| 356 | fttmr010->clkevt.name = "FTTMR010-TIMER1"; |
| 357 | /* Reasonably fast and accurate clock event */ |
| 358 | fttmr010->clkevt.rating = 300; |
| 359 | fttmr010->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | |
| 360 | CLOCK_EVT_FEAT_ONESHOT; |
| 361 | fttmr010->clkevt.set_next_event = fttmr010_timer_set_next_event; |
| 362 | fttmr010->clkevt.set_state_shutdown = fttmr010_timer_shutdown; |
| 363 | fttmr010->clkevt.set_state_periodic = fttmr010_timer_set_periodic; |
| 364 | fttmr010->clkevt.set_state_oneshot = fttmr010_timer_set_oneshot; |
| 365 | fttmr010->clkevt.tick_resume = fttmr010_timer_shutdown; |
| 366 | fttmr010->clkevt.cpumask = cpumask_of(0); |
| 367 | fttmr010->clkevt.irq = irq; |
| 368 | clockevents_config_and_register(&fttmr010->clkevt, |
| 369 | fttmr010->tick_rate, |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 370 | 1, 0xffffffff); |
| 371 | |
Linus Walleij | 385c98f | 2017-06-11 23:26:17 +0200 | [diff] [blame] | 372 | #ifdef CONFIG_ARM |
| 373 | /* Also use this timer for delays */ |
| 374 | if (fttmr010->count_down) |
| 375 | fttmr010->delay_timer.read_current_timer = |
| 376 | fttmr010_read_current_timer_down; |
| 377 | else |
| 378 | fttmr010->delay_timer.read_current_timer = |
| 379 | fttmr010_read_current_timer_up; |
| 380 | fttmr010->delay_timer.freq = fttmr010->tick_rate; |
| 381 | register_current_timer_delay(&fttmr010->delay_timer); |
| 382 | #endif |
| 383 | |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 384 | return 0; |
Linus Walleij | e7bad21 | 2017-05-18 22:17:01 +0200 | [diff] [blame] | 385 | |
| 386 | out_unmap: |
| 387 | iounmap(fttmr010->base); |
| 388 | out_free: |
| 389 | kfree(fttmr010); |
| 390 | out_disable_clock: |
| 391 | clk_disable_unprepare(clk); |
| 392 | |
| 393 | return ret; |
Linus Walleij | 4750535 | 2017-01-22 13:17:17 +0100 | [diff] [blame] | 394 | } |
Daniel Lezcano | ef89718 | 2017-05-26 10:38:07 +0200 | [diff] [blame] | 395 | |
| 396 | static __init int aspeed_timer_init(struct device_node *np) |
| 397 | { |
| 398 | return fttmr010_common_init(np, true); |
| 399 | } |
| 400 | |
| 401 | static __init int fttmr010_timer_init(struct device_node *np) |
| 402 | { |
| 403 | return fttmr010_common_init(np, false); |
| 404 | } |
| 405 | |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 406 | TIMER_OF_DECLARE(fttmr010, "faraday,fttmr010", fttmr010_timer_init); |
| 407 | TIMER_OF_DECLARE(gemini, "cortina,gemini-timer", fttmr010_timer_init); |
| 408 | TIMER_OF_DECLARE(moxart, "moxa,moxart-timer", fttmr010_timer_init); |
| 409 | TIMER_OF_DECLARE(ast2400, "aspeed,ast2400-timer", aspeed_timer_init); |
| 410 | TIMER_OF_DECLARE(ast2500, "aspeed,ast2500-timer", aspeed_timer_init); |