Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 2 | /* |
| 3 | * linux/arch/h8300/kernel/cpu/timer/timer8.c |
| 4 | * |
| 5 | * Yoshinori Sato <ysato@users.sourcefoge.jp> |
| 6 | * |
| 7 | * 8bit Timer driver |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/errno.h> |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/init.h> |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 15 | #include <linux/clockchips.h> |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 16 | #include <linux/clk.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/of.h> |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 19 | #include <linux/of_address.h> |
| 20 | #include <linux/of_irq.h> |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 21 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 22 | #define _8TCR 0 |
| 23 | #define _8TCSR 2 |
| 24 | #define TCORA 4 |
| 25 | #define TCORB 6 |
| 26 | #define _8TCNT 8 |
| 27 | |
Yoshinori Sato | d33f250 | 2015-12-05 02:48:18 +0900 | [diff] [blame] | 28 | #define CMIEA 6 |
| 29 | #define CMFA 6 |
| 30 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 31 | #define FLAG_STARTED (1 << 3) |
| 32 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 33 | #define SCALE 64 |
| 34 | |
Yoshinori Sato | d33f250 | 2015-12-05 02:48:18 +0900 | [diff] [blame] | 35 | #define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a)) |
| 36 | #define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a)) |
| 37 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 38 | struct timer8_priv { |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 39 | struct clock_event_device ced; |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame] | 40 | void __iomem *mapbase; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 41 | unsigned long flags; |
| 42 | unsigned int rate; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 43 | }; |
| 44 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 45 | static irqreturn_t timer8_interrupt(int irq, void *dev_id) |
| 46 | { |
| 47 | struct timer8_priv *p = dev_id; |
| 48 | |
Daniel Lezcano | 7053fda | 2015-11-08 18:07:38 +0100 | [diff] [blame] | 49 | if (clockevent_state_oneshot(&p->ced)) |
Yoshinori Sato | d33f250 | 2015-12-05 02:48:18 +0900 | [diff] [blame] | 50 | iowrite16be(0x0000, p->mapbase + _8TCR); |
Daniel Lezcano | 7053fda | 2015-11-08 18:07:38 +0100 | [diff] [blame] | 51 | |
| 52 | p->ced.event_handler(&p->ced); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 53 | |
Yoshinori Sato | d33f250 | 2015-12-05 02:48:18 +0900 | [diff] [blame] | 54 | bclr(CMFA, p->mapbase + _8TCSR); |
Yoshinori Sato | f37632d | 2015-12-05 02:48:16 +0900 | [diff] [blame] | 55 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 56 | return IRQ_HANDLED; |
| 57 | } |
| 58 | |
| 59 | static void timer8_set_next(struct timer8_priv *p, unsigned long delta) |
| 60 | { |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 61 | if (delta >= 0x10000) |
Daniel Lezcano | 8c09b7d | 2015-11-09 09:02:38 +0100 | [diff] [blame] | 62 | pr_warn("delta out of range\n"); |
Yoshinori Sato | d33f250 | 2015-12-05 02:48:18 +0900 | [diff] [blame] | 63 | bclr(CMIEA, p->mapbase + _8TCR); |
| 64 | iowrite16be(delta, p->mapbase + TCORA); |
| 65 | iowrite16be(0x0000, p->mapbase + _8TCNT); |
| 66 | bclr(CMFA, p->mapbase + _8TCSR); |
| 67 | bset(CMIEA, p->mapbase + _8TCR); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static int timer8_enable(struct timer8_priv *p) |
| 71 | { |
Yoshinori Sato | d33f250 | 2015-12-05 02:48:18 +0900 | [diff] [blame] | 72 | iowrite16be(0xffff, p->mapbase + TCORA); |
| 73 | iowrite16be(0x0000, p->mapbase + _8TCNT); |
| 74 | iowrite16be(0x0c02, p->mapbase + _8TCR); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int timer8_start(struct timer8_priv *p) |
| 80 | { |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 81 | int ret; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 82 | |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 83 | if ((p->flags & FLAG_STARTED)) |
| 84 | return 0; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 85 | |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 86 | ret = timer8_enable(p); |
| 87 | if (!ret) |
| 88 | p->flags |= FLAG_STARTED; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 89 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static void timer8_stop(struct timer8_priv *p) |
| 94 | { |
Yoshinori Sato | d33f250 | 2015-12-05 02:48:18 +0900 | [diff] [blame] | 95 | iowrite16be(0x0000, p->mapbase + _8TCR); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static inline struct timer8_priv *ced_to_priv(struct clock_event_device *ced) |
| 99 | { |
| 100 | return container_of(ced, struct timer8_priv, ced); |
| 101 | } |
| 102 | |
Daniel Lezcano | 1f058d5 | 2015-11-08 17:46:54 +0100 | [diff] [blame] | 103 | static void timer8_clock_event_start(struct timer8_priv *p, unsigned long delta) |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 104 | { |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 105 | timer8_start(p); |
Daniel Lezcano | 1f058d5 | 2015-11-08 17:46:54 +0100 | [diff] [blame] | 106 | timer8_set_next(p, delta); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 107 | } |
| 108 | |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 109 | static int timer8_clock_event_shutdown(struct clock_event_device *ced) |
| 110 | { |
| 111 | timer8_stop(ced_to_priv(ced)); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int timer8_clock_event_periodic(struct clock_event_device *ced) |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 116 | { |
| 117 | struct timer8_priv *p = ced_to_priv(ced); |
| 118 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 119 | pr_info("%s: used for periodic clock events\n", ced->name); |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 120 | timer8_stop(p); |
Daniel Lezcano | 1f058d5 | 2015-11-08 17:46:54 +0100 | [diff] [blame] | 121 | timer8_clock_event_start(p, (p->rate + HZ/2) / HZ); |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int timer8_clock_event_oneshot(struct clock_event_device *ced) |
| 127 | { |
| 128 | struct timer8_priv *p = ced_to_priv(ced); |
| 129 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 130 | pr_info("%s: used for oneshot clock events\n", ced->name); |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 131 | timer8_stop(p); |
Daniel Lezcano | 1f058d5 | 2015-11-08 17:46:54 +0100 | [diff] [blame] | 132 | timer8_clock_event_start(p, 0x10000); |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 133 | |
| 134 | return 0; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static int timer8_clock_event_next(unsigned long delta, |
| 138 | struct clock_event_device *ced) |
| 139 | { |
| 140 | struct timer8_priv *p = ced_to_priv(ced); |
| 141 | |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 142 | BUG_ON(!clockevent_state_oneshot(ced)); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 143 | timer8_set_next(p, delta - 1); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 148 | static struct timer8_priv timer8_priv = { |
| 149 | .ced = { |
| 150 | .name = "h8300_8timer", |
| 151 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, |
| 152 | .rating = 200, |
| 153 | .set_next_event = timer8_clock_event_next, |
| 154 | .set_state_shutdown = timer8_clock_event_shutdown, |
| 155 | .set_state_periodic = timer8_clock_event_periodic, |
| 156 | .set_state_oneshot = timer8_clock_event_oneshot, |
| 157 | }, |
| 158 | }; |
| 159 | |
Daniel Lezcano | 691f8f8 | 2016-06-06 17:56:37 +0200 | [diff] [blame] | 160 | static int __init h8300_8timer_init(struct device_node *node) |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 161 | { |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 162 | void __iomem *base; |
Daniel Lezcano | 691f8f8 | 2016-06-06 17:56:37 +0200 | [diff] [blame] | 163 | int irq, ret; |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 164 | struct clk *clk; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 165 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 166 | clk = of_clk_get(node, 0); |
| 167 | if (IS_ERR(clk)) { |
| 168 | pr_err("failed to get clock for clockevent\n"); |
Daniel Lezcano | 691f8f8 | 2016-06-06 17:56:37 +0200 | [diff] [blame] | 169 | return PTR_ERR(clk); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 170 | } |
| 171 | |
Tianjia Zhang | 400d033 | 2020-08-02 19:15:41 +0800 | [diff] [blame] | 172 | ret = -ENXIO; |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 173 | base = of_iomap(node, 0); |
| 174 | if (!base) { |
| 175 | pr_err("failed to map registers for clockevent\n"); |
| 176 | goto free_clk; |
| 177 | } |
| 178 | |
Daniel Lezcano | 691f8f8 | 2016-06-06 17:56:37 +0200 | [diff] [blame] | 179 | ret = -EINVAL; |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 180 | irq = irq_of_parse_and_map(node, 0); |
Daniel Lezcano | 54a0cd5 | 2015-11-08 17:56:18 +0100 | [diff] [blame] | 181 | if (!irq) { |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 182 | pr_err("failed to get irq for clockevent\n"); |
| 183 | goto unmap_reg; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 184 | } |
| 185 | |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame] | 186 | timer8_priv.mapbase = base; |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 187 | |
Yoshinori Sato | 6f2b611 | 2015-12-05 02:48:17 +0900 | [diff] [blame] | 188 | timer8_priv.rate = clk_get_rate(clk) / SCALE; |
| 189 | if (!timer8_priv.rate) { |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 190 | pr_err("Failed to get rate for the clocksource\n"); |
| 191 | goto unmap_reg; |
| 192 | } |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 193 | |
Yoshinori Sato | 6f2b611 | 2015-12-05 02:48:17 +0900 | [diff] [blame] | 194 | if (request_irq(irq, timer8_interrupt, IRQF_TIMER, |
| 195 | timer8_priv.ced.name, &timer8_priv) < 0) { |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 196 | pr_err("failed to request irq %d for clockevent\n", irq); |
| 197 | goto unmap_reg; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 198 | } |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 199 | |
Yoshinori Sato | 6f2b611 | 2015-12-05 02:48:17 +0900 | [diff] [blame] | 200 | clockevents_config_and_register(&timer8_priv.ced, |
| 201 | timer8_priv.rate, 1, 0x0000ffff); |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 202 | |
Daniel Lezcano | 691f8f8 | 2016-06-06 17:56:37 +0200 | [diff] [blame] | 203 | return 0; |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 204 | unmap_reg: |
| 205 | iounmap(base); |
| 206 | free_clk: |
| 207 | clk_put(clk); |
Daniel Lezcano | 691f8f8 | 2016-06-06 17:56:37 +0200 | [diff] [blame] | 208 | return ret; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 209 | } |
| 210 | |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 211 | TIMER_OF_DECLARE(h8300_8bit, "renesas,8bit-timer", h8300_8timer_init); |