Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Amlogic Meson6 SoCs timer handling. |
| 3 | * |
| 4 | * Copyright (C) 2014 Carlo Caione <carlo@caione.org> |
| 5 | * |
| 6 | * Based on code from Amlogic, Inc |
| 7 | * |
| 8 | * This file is licensed under the terms of the GNU General Public |
| 9 | * License version 2. This program is licensed "as is" without any |
| 10 | * warranty of any kind, whether express or implied. |
| 11 | */ |
| 12 | |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 13 | #include <linux/bitfield.h> |
| 14 | #include <linux/bitops.h> |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 15 | #include <linux/clk.h> |
| 16 | #include <linux/clockchips.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/irq.h> |
| 19 | #include <linux/irqreturn.h> |
| 20 | #include <linux/sched_clock.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/of_address.h> |
| 23 | #include <linux/of_irq.h> |
| 24 | |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 25 | #define MESON_ISA_TIMER_MUX 0x00 |
| 26 | #define MESON_ISA_TIMER_MUX_TIMERD_EN BIT(19) |
| 27 | #define MESON_ISA_TIMER_MUX_TIMERC_EN BIT(18) |
| 28 | #define MESON_ISA_TIMER_MUX_TIMERB_EN BIT(17) |
| 29 | #define MESON_ISA_TIMER_MUX_TIMERA_EN BIT(16) |
| 30 | #define MESON_ISA_TIMER_MUX_TIMERD_MODE BIT(15) |
| 31 | #define MESON_ISA_TIMER_MUX_TIMERC_MODE BIT(14) |
| 32 | #define MESON_ISA_TIMER_MUX_TIMERB_MODE BIT(13) |
| 33 | #define MESON_ISA_TIMER_MUX_TIMERA_MODE BIT(12) |
| 34 | #define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK GENMASK(10, 8) |
| 35 | #define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_SYSTEM_CLOCK 0x0 |
| 36 | #define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_1US 0x1 |
| 37 | #define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_10US 0x2 |
| 38 | #define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_100US 0x3 |
| 39 | #define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_1MS 0x4 |
| 40 | #define MESON_ISA_TIMER_MUX_TIMERD_INPUT_CLOCK_MASK GENMASK(7, 6) |
| 41 | #define MESON_ISA_TIMER_MUX_TIMERC_INPUT_CLOCK_MASK GENMASK(5, 4) |
| 42 | #define MESON_ISA_TIMER_MUX_TIMERB_INPUT_CLOCK_MASK GENMASK(3, 2) |
| 43 | #define MESON_ISA_TIMER_MUX_TIMERA_INPUT_CLOCK_MASK GENMASK(1, 0) |
| 44 | #define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1US 0x0 |
| 45 | #define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_10US 0x1 |
| 46 | #define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_100US 0x0 |
| 47 | #define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1MS 0x3 |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 48 | |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 49 | #define MESON_ISA_TIMERA 0x04 |
| 50 | #define MESON_ISA_TIMERB 0x08 |
| 51 | #define MESON_ISA_TIMERC 0x0c |
| 52 | #define MESON_ISA_TIMERD 0x10 |
| 53 | #define MESON_ISA_TIMERE 0x14 |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 54 | |
| 55 | static void __iomem *timer_base; |
| 56 | |
| 57 | static u64 notrace meson6_timer_sched_read(void) |
| 58 | { |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 59 | return (u64)readl(timer_base + MESON_ISA_TIMERE); |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 62 | static void meson6_clkevt_time_stop(void) |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 63 | { |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 64 | u32 val = readl(timer_base + MESON_ISA_TIMER_MUX); |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 65 | |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 66 | writel(val & ~MESON_ISA_TIMER_MUX_TIMERA_EN, |
| 67 | timer_base + MESON_ISA_TIMER_MUX); |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 70 | static void meson6_clkevt_time_setup(unsigned long delay) |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 71 | { |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 72 | writel(delay, timer_base + MESON_ISA_TIMERA); |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 75 | static void meson6_clkevt_time_start(bool periodic) |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 76 | { |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 77 | u32 val = readl(timer_base + MESON_ISA_TIMER_MUX); |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 78 | |
| 79 | if (periodic) |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 80 | val |= MESON_ISA_TIMER_MUX_TIMERA_MODE; |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 81 | else |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 82 | val &= ~MESON_ISA_TIMER_MUX_TIMERA_MODE; |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 83 | |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 84 | writel(val | MESON_ISA_TIMER_MUX_TIMERA_EN, |
| 85 | timer_base + MESON_ISA_TIMER_MUX); |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Viresh Kumar | 40117bd | 2015-06-18 16:24:23 +0530 | [diff] [blame] | 88 | static int meson6_shutdown(struct clock_event_device *evt) |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 89 | { |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 90 | meson6_clkevt_time_stop(); |
Viresh Kumar | 40117bd | 2015-06-18 16:24:23 +0530 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int meson6_set_oneshot(struct clock_event_device *evt) |
| 95 | { |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 96 | meson6_clkevt_time_stop(); |
| 97 | meson6_clkevt_time_start(false); |
Viresh Kumar | 40117bd | 2015-06-18 16:24:23 +0530 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int meson6_set_periodic(struct clock_event_device *evt) |
| 102 | { |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 103 | meson6_clkevt_time_stop(); |
| 104 | meson6_clkevt_time_setup(USEC_PER_SEC / HZ - 1); |
| 105 | meson6_clkevt_time_start(true); |
Viresh Kumar | 40117bd | 2015-06-18 16:24:23 +0530 | [diff] [blame] | 106 | return 0; |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int meson6_clkevt_next_event(unsigned long evt, |
| 110 | struct clock_event_device *unused) |
| 111 | { |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 112 | meson6_clkevt_time_stop(); |
| 113 | meson6_clkevt_time_setup(evt); |
| 114 | meson6_clkevt_time_start(false); |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static struct clock_event_device meson6_clockevent = { |
Viresh Kumar | 40117bd | 2015-06-18 16:24:23 +0530 | [diff] [blame] | 120 | .name = "meson6_tick", |
| 121 | .rating = 400, |
| 122 | .features = CLOCK_EVT_FEAT_PERIODIC | |
| 123 | CLOCK_EVT_FEAT_ONESHOT, |
| 124 | .set_state_shutdown = meson6_shutdown, |
| 125 | .set_state_periodic = meson6_set_periodic, |
| 126 | .set_state_oneshot = meson6_set_oneshot, |
| 127 | .tick_resume = meson6_shutdown, |
| 128 | .set_next_event = meson6_clkevt_next_event, |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static irqreturn_t meson6_timer_interrupt(int irq, void *dev_id) |
| 132 | { |
| 133 | struct clock_event_device *evt = (struct clock_event_device *)dev_id; |
| 134 | |
| 135 | evt->event_handler(evt); |
| 136 | |
| 137 | return IRQ_HANDLED; |
| 138 | } |
| 139 | |
| 140 | static struct irqaction meson6_timer_irq = { |
| 141 | .name = "meson6_timer", |
| 142 | .flags = IRQF_TIMER | IRQF_IRQPOLL, |
| 143 | .handler = meson6_timer_interrupt, |
| 144 | .dev_id = &meson6_clockevent, |
| 145 | }; |
| 146 | |
Daniel Lezcano | ca46acb | 2016-06-06 17:57:07 +0200 | [diff] [blame] | 147 | static int __init meson6_timer_init(struct device_node *node) |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 148 | { |
| 149 | u32 val; |
| 150 | int ret, irq; |
| 151 | |
| 152 | timer_base = of_io_request_and_map(node, 0, "meson6-timer"); |
Daniel Lezcano | ca46acb | 2016-06-06 17:57:07 +0200 | [diff] [blame] | 153 | if (IS_ERR(timer_base)) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 154 | pr_err("Can't map registers\n"); |
Daniel Lezcano | ca46acb | 2016-06-06 17:57:07 +0200 | [diff] [blame] | 155 | return -ENXIO; |
| 156 | } |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 157 | |
| 158 | irq = irq_of_parse_and_map(node, 0); |
Daniel Lezcano | ca46acb | 2016-06-06 17:57:07 +0200 | [diff] [blame] | 159 | if (irq <= 0) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 160 | pr_err("Can't parse IRQ\n"); |
Daniel Lezcano | ca46acb | 2016-06-06 17:57:07 +0200 | [diff] [blame] | 161 | return -EINVAL; |
| 162 | } |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 163 | |
| 164 | /* Set 1us for timer E */ |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 165 | val = readl(timer_base + MESON_ISA_TIMER_MUX); |
| 166 | val &= ~MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK; |
| 167 | val |= FIELD_PREP(MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK, |
| 168 | MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_1US); |
| 169 | writel(val, timer_base + MESON_ISA_TIMER_MUX); |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 170 | |
| 171 | sched_clock_register(meson6_timer_sched_read, 32, USEC_PER_SEC); |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 172 | clocksource_mmio_init(timer_base + MESON_ISA_TIMERE, node->name, |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 173 | 1000 * 1000, 300, 32, clocksource_mmio_readl_up); |
| 174 | |
| 175 | /* Timer A base 1us */ |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 176 | val &= ~MESON_ISA_TIMER_MUX_TIMERA_INPUT_CLOCK_MASK; |
| 177 | val |= FIELD_PREP(MESON_ISA_TIMER_MUX_TIMERA_INPUT_CLOCK_MASK, |
| 178 | MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1US); |
| 179 | writel(val, timer_base + MESON_ISA_TIMER_MUX); |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 180 | |
| 181 | /* Stop the timer A */ |
Martin Blumenstingl | bed8fc1 | 2018-11-15 23:46:56 +0100 | [diff] [blame^] | 182 | meson6_clkevt_time_stop(); |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 183 | |
| 184 | ret = setup_irq(irq, &meson6_timer_irq); |
Daniel Lezcano | ca46acb | 2016-06-06 17:57:07 +0200 | [diff] [blame] | 185 | if (ret) { |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 186 | pr_warn("failed to setup irq %d\n", irq); |
Daniel Lezcano | ca46acb | 2016-06-06 17:57:07 +0200 | [diff] [blame] | 187 | return ret; |
| 188 | } |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 189 | |
| 190 | meson6_clockevent.cpumask = cpu_possible_mask; |
| 191 | meson6_clockevent.irq = irq; |
| 192 | |
| 193 | clockevents_config_and_register(&meson6_clockevent, USEC_PER_SEC, |
| 194 | 1, 0xfffe); |
Daniel Lezcano | ca46acb | 2016-06-06 17:57:07 +0200 | [diff] [blame] | 195 | return 0; |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 196 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 197 | TIMER_OF_DECLARE(meson6, "amlogic,meson6-timer", |
Carlo Caione | e4a6b37 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 198 | meson6_timer_init); |