| .. | .. |
|---|
| 8 | 8 | #include <linux/clocksource.h> |
|---|
| 9 | 9 | #include <linux/delay.h> |
|---|
| 10 | 10 | #include <linux/interrupt.h> |
|---|
| 11 | | -#include <linux/of_address.h> |
|---|
| 12 | | -#include <linux/of_irq.h> |
|---|
| 13 | 11 | #include <linux/sched_clock.h> |
|---|
| 12 | + |
|---|
| 13 | +#include "timer-of.h" |
|---|
| 14 | 14 | |
|---|
| 15 | 15 | #define TPM_PARAM 0x4 |
|---|
| 16 | 16 | #define TPM_PARAM_WIDTH_SHIFT 16 |
|---|
| .. | .. |
|---|
| 33 | 33 | #define TPM_C0V 0x24 |
|---|
| 34 | 34 | |
|---|
| 35 | 35 | static int counter_width; |
|---|
| 36 | | -static int rating; |
|---|
| 37 | 36 | static void __iomem *timer_base; |
|---|
| 38 | | -static struct clock_event_device clockevent_tpm; |
|---|
| 39 | 37 | |
|---|
| 40 | 38 | static inline void tpm_timer_disable(void) |
|---|
| 41 | 39 | { |
|---|
| .. | .. |
|---|
| 63 | 61 | writel(TPM_STATUS_CH0F, timer_base + TPM_STATUS); |
|---|
| 64 | 62 | } |
|---|
| 65 | 63 | |
|---|
| 66 | | -static struct delay_timer tpm_delay_timer; |
|---|
| 67 | | - |
|---|
| 68 | 64 | static inline unsigned long tpm_read_counter(void) |
|---|
| 69 | 65 | { |
|---|
| 70 | 66 | return readl(timer_base + TPM_CNT); |
|---|
| 71 | 67 | } |
|---|
| 72 | 68 | |
|---|
| 69 | +#if defined(CONFIG_ARM) |
|---|
| 70 | +static struct delay_timer tpm_delay_timer; |
|---|
| 71 | + |
|---|
| 73 | 72 | static unsigned long tpm_read_current_timer(void) |
|---|
| 74 | 73 | { |
|---|
| 75 | 74 | return tpm_read_counter(); |
|---|
| 76 | 75 | } |
|---|
| 76 | +#endif |
|---|
| 77 | 77 | |
|---|
| 78 | 78 | static u64 notrace tpm_read_sched_clock(void) |
|---|
| 79 | 79 | { |
|---|
| 80 | 80 | return tpm_read_counter(); |
|---|
| 81 | | -} |
|---|
| 82 | | - |
|---|
| 83 | | -static int __init tpm_clocksource_init(unsigned long rate) |
|---|
| 84 | | -{ |
|---|
| 85 | | - tpm_delay_timer.read_current_timer = &tpm_read_current_timer; |
|---|
| 86 | | - tpm_delay_timer.freq = rate; |
|---|
| 87 | | - register_current_timer_delay(&tpm_delay_timer); |
|---|
| 88 | | - |
|---|
| 89 | | - sched_clock_register(tpm_read_sched_clock, counter_width, rate); |
|---|
| 90 | | - |
|---|
| 91 | | - return clocksource_mmio_init(timer_base + TPM_CNT, "imx-tpm", |
|---|
| 92 | | - rate, rating, counter_width, |
|---|
| 93 | | - clocksource_mmio_readl_up); |
|---|
| 94 | 81 | } |
|---|
| 95 | 82 | |
|---|
| 96 | 83 | static int tpm_set_next_event(unsigned long delta, |
|---|
| .. | .. |
|---|
| 137 | 124 | return IRQ_HANDLED; |
|---|
| 138 | 125 | } |
|---|
| 139 | 126 | |
|---|
| 140 | | -static struct clock_event_device clockevent_tpm = { |
|---|
| 141 | | - .name = "i.MX7ULP TPM Timer", |
|---|
| 142 | | - .features = CLOCK_EVT_FEAT_ONESHOT, |
|---|
| 143 | | - .set_state_oneshot = tpm_set_state_oneshot, |
|---|
| 144 | | - .set_next_event = tpm_set_next_event, |
|---|
| 145 | | - .set_state_shutdown = tpm_set_state_shutdown, |
|---|
| 127 | +static struct timer_of to_tpm = { |
|---|
| 128 | + .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK, |
|---|
| 129 | + .clkevt = { |
|---|
| 130 | + .name = "i.MX7ULP TPM Timer", |
|---|
| 131 | + .rating = 200, |
|---|
| 132 | + .features = CLOCK_EVT_FEAT_ONESHOT, |
|---|
| 133 | + .set_state_shutdown = tpm_set_state_shutdown, |
|---|
| 134 | + .set_state_oneshot = tpm_set_state_oneshot, |
|---|
| 135 | + .set_next_event = tpm_set_next_event, |
|---|
| 136 | + .cpumask = cpu_possible_mask, |
|---|
| 137 | + }, |
|---|
| 138 | + .of_irq = { |
|---|
| 139 | + .handler = tpm_timer_interrupt, |
|---|
| 140 | + .flags = IRQF_TIMER | IRQF_IRQPOLL, |
|---|
| 141 | + }, |
|---|
| 142 | + .of_clk = { |
|---|
| 143 | + .name = "per", |
|---|
| 144 | + }, |
|---|
| 146 | 145 | }; |
|---|
| 147 | 146 | |
|---|
| 148 | | -static int __init tpm_clockevent_init(unsigned long rate, int irq) |
|---|
| 147 | +static int __init tpm_clocksource_init(void) |
|---|
| 149 | 148 | { |
|---|
| 150 | | - int ret; |
|---|
| 149 | +#if defined(CONFIG_ARM) |
|---|
| 150 | + tpm_delay_timer.read_current_timer = &tpm_read_current_timer; |
|---|
| 151 | + tpm_delay_timer.freq = timer_of_rate(&to_tpm) >> 3; |
|---|
| 152 | + register_current_timer_delay(&tpm_delay_timer); |
|---|
| 153 | +#endif |
|---|
| 151 | 154 | |
|---|
| 152 | | - ret = request_irq(irq, tpm_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL, |
|---|
| 153 | | - "i.MX7ULP TPM Timer", &clockevent_tpm); |
|---|
| 155 | + sched_clock_register(tpm_read_sched_clock, counter_width, |
|---|
| 156 | + timer_of_rate(&to_tpm) >> 3); |
|---|
| 154 | 157 | |
|---|
| 155 | | - clockevent_tpm.rating = rating; |
|---|
| 156 | | - clockevent_tpm.cpumask = cpumask_of(0); |
|---|
| 157 | | - clockevent_tpm.irq = irq; |
|---|
| 158 | | - clockevents_config_and_register(&clockevent_tpm, rate, 300, |
|---|
| 159 | | - GENMASK(counter_width - 1, 1)); |
|---|
| 158 | + return clocksource_mmio_init(timer_base + TPM_CNT, |
|---|
| 159 | + "imx-tpm", |
|---|
| 160 | + timer_of_rate(&to_tpm) >> 3, |
|---|
| 161 | + to_tpm.clkevt.rating, |
|---|
| 162 | + counter_width, |
|---|
| 163 | + clocksource_mmio_readl_up); |
|---|
| 164 | +} |
|---|
| 160 | 165 | |
|---|
| 161 | | - return ret; |
|---|
| 166 | +static void __init tpm_clockevent_init(void) |
|---|
| 167 | +{ |
|---|
| 168 | + clockevents_config_and_register(&to_tpm.clkevt, |
|---|
| 169 | + timer_of_rate(&to_tpm) >> 3, |
|---|
| 170 | + 300, |
|---|
| 171 | + GENMASK(counter_width - 1, |
|---|
| 172 | + 1)); |
|---|
| 162 | 173 | } |
|---|
| 163 | 174 | |
|---|
| 164 | 175 | static int __init tpm_timer_init(struct device_node *np) |
|---|
| 165 | 176 | { |
|---|
| 166 | | - struct clk *ipg, *per; |
|---|
| 167 | | - int irq, ret; |
|---|
| 168 | | - u32 rate; |
|---|
| 169 | | - |
|---|
| 170 | | - timer_base = of_iomap(np, 0); |
|---|
| 171 | | - if (!timer_base) { |
|---|
| 172 | | - pr_err("tpm: failed to get base address\n"); |
|---|
| 173 | | - return -ENXIO; |
|---|
| 174 | | - } |
|---|
| 175 | | - |
|---|
| 176 | | - irq = irq_of_parse_and_map(np, 0); |
|---|
| 177 | | - if (!irq) { |
|---|
| 178 | | - pr_err("tpm: failed to get irq\n"); |
|---|
| 179 | | - ret = -ENOENT; |
|---|
| 180 | | - goto err_iomap; |
|---|
| 181 | | - } |
|---|
| 177 | + struct clk *ipg; |
|---|
| 178 | + int ret; |
|---|
| 182 | 179 | |
|---|
| 183 | 180 | ipg = of_clk_get_by_name(np, "ipg"); |
|---|
| 184 | | - per = of_clk_get_by_name(np, "per"); |
|---|
| 185 | | - if (IS_ERR(ipg) || IS_ERR(per)) { |
|---|
| 186 | | - pr_err("tpm: failed to get ipg or per clk\n"); |
|---|
| 187 | | - ret = -ENODEV; |
|---|
| 188 | | - goto err_clk_get; |
|---|
| 181 | + if (IS_ERR(ipg)) { |
|---|
| 182 | + pr_err("tpm: failed to get ipg clk\n"); |
|---|
| 183 | + return -ENODEV; |
|---|
| 189 | 184 | } |
|---|
| 190 | | - |
|---|
| 191 | 185 | /* enable clk before accessing registers */ |
|---|
| 192 | 186 | ret = clk_prepare_enable(ipg); |
|---|
| 193 | 187 | if (ret) { |
|---|
| 194 | 188 | pr_err("tpm: ipg clock enable failed (%d)\n", ret); |
|---|
| 195 | | - goto err_clk_get; |
|---|
| 189 | + clk_put(ipg); |
|---|
| 190 | + return ret; |
|---|
| 196 | 191 | } |
|---|
| 197 | 192 | |
|---|
| 198 | | - ret = clk_prepare_enable(per); |
|---|
| 199 | | - if (ret) { |
|---|
| 200 | | - pr_err("tpm: per clock enable failed (%d)\n", ret); |
|---|
| 201 | | - goto err_per_clk_enable; |
|---|
| 202 | | - } |
|---|
| 193 | + ret = timer_of_init(np, &to_tpm); |
|---|
| 194 | + if (ret) |
|---|
| 195 | + return ret; |
|---|
| 203 | 196 | |
|---|
| 204 | | - counter_width = (readl(timer_base + TPM_PARAM) & TPM_PARAM_WIDTH_MASK) |
|---|
| 205 | | - >> TPM_PARAM_WIDTH_SHIFT; |
|---|
| 197 | + timer_base = timer_of_base(&to_tpm); |
|---|
| 198 | + |
|---|
| 199 | + counter_width = (readl(timer_base + TPM_PARAM) |
|---|
| 200 | + & TPM_PARAM_WIDTH_MASK) >> TPM_PARAM_WIDTH_SHIFT; |
|---|
| 206 | 201 | /* use rating 200 for 32-bit counter and 150 for 16-bit counter */ |
|---|
| 207 | | - rating = counter_width == 0x20 ? 200 : 150; |
|---|
| 202 | + to_tpm.clkevt.rating = counter_width == 0x20 ? 200 : 150; |
|---|
| 208 | 203 | |
|---|
| 209 | 204 | /* |
|---|
| 210 | 205 | * Initialize tpm module to a known state |
|---|
| .. | .. |
|---|
| 229 | 224 | writel(TPM_SC_CMOD_INC_PER_CNT | |
|---|
| 230 | 225 | (counter_width == 0x20 ? |
|---|
| 231 | 226 | TPM_SC_CMOD_DIV_DEFAULT : TPM_SC_CMOD_DIV_MAX), |
|---|
| 232 | | - timer_base + TPM_SC); |
|---|
| 227 | + timer_base + TPM_SC); |
|---|
| 233 | 228 | |
|---|
| 234 | 229 | /* set MOD register to maximum for free running mode */ |
|---|
| 235 | 230 | writel(GENMASK(counter_width - 1, 0), timer_base + TPM_MOD); |
|---|
| 236 | 231 | |
|---|
| 237 | | - rate = clk_get_rate(per) >> 3; |
|---|
| 238 | | - ret = tpm_clocksource_init(rate); |
|---|
| 239 | | - if (ret) |
|---|
| 240 | | - goto err_per_clk_enable; |
|---|
| 232 | + tpm_clockevent_init(); |
|---|
| 241 | 233 | |
|---|
| 242 | | - ret = tpm_clockevent_init(rate, irq); |
|---|
| 243 | | - if (ret) |
|---|
| 244 | | - goto err_per_clk_enable; |
|---|
| 245 | | - |
|---|
| 246 | | - return 0; |
|---|
| 247 | | - |
|---|
| 248 | | -err_per_clk_enable: |
|---|
| 249 | | - clk_disable_unprepare(ipg); |
|---|
| 250 | | -err_clk_get: |
|---|
| 251 | | - clk_put(per); |
|---|
| 252 | | - clk_put(ipg); |
|---|
| 253 | | -err_iomap: |
|---|
| 254 | | - iounmap(timer_base); |
|---|
| 255 | | - return ret; |
|---|
| 234 | + return tpm_clocksource_init(); |
|---|
| 256 | 235 | } |
|---|
| 257 | 236 | TIMER_OF_DECLARE(imx7ulp, "fsl,imx7ulp-tpm", tpm_timer_init); |
|---|