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