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 | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 27 | #define FLAG_STARTED (1 << 3) |
| 28 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 29 | #define SCALE 64 |
| 30 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 31 | struct timer8_priv { |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 32 | struct clock_event_device ced; |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 33 | void __iomem *mapbase; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 34 | unsigned long flags; |
| 35 | unsigned int rate; |
| 36 | unsigned int tcora; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static unsigned long timer8_get_counter(struct timer8_priv *p) |
| 40 | { |
| 41 | unsigned long v1, v2, v3; |
| 42 | int o1, o2; |
| 43 | |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 44 | o1 = readb(p->mapbase + _8TCSR) & 0x20; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 45 | |
| 46 | /* Make sure the timer value is stable. Stolen from acpi_pm.c */ |
| 47 | do { |
| 48 | o2 = o1; |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 49 | v1 = readw(p->mapbase + _8TCNT); |
| 50 | v2 = readw(p->mapbase + _8TCNT); |
| 51 | v3 = readw(p->mapbase + _8TCNT); |
| 52 | o1 = readb(p->mapbase + _8TCSR) & 0x20; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 53 | } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3) |
| 54 | || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2))); |
| 55 | |
| 56 | v2 |= o1 << 10; |
| 57 | return v2; |
| 58 | } |
| 59 | |
| 60 | static irqreturn_t timer8_interrupt(int irq, void *dev_id) |
| 61 | { |
| 62 | struct timer8_priv *p = dev_id; |
| 63 | |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 64 | writeb(readb(p->mapbase + _8TCSR) & ~0x40, |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 65 | p->mapbase + _8TCSR); |
Daniel Lezcano | 7053fda | 2015-11-08 18:07:38 +0100 | [diff] [blame] | 66 | |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 67 | writew(p->tcora, p->mapbase + TCORA); |
Daniel Lezcano | 7053fda | 2015-11-08 18:07:38 +0100 | [diff] [blame] | 68 | |
| 69 | if (clockevent_state_oneshot(&p->ced)) |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 70 | writew(0x0000, p->mapbase + _8TCR); |
Daniel Lezcano | 7053fda | 2015-11-08 18:07:38 +0100 | [diff] [blame] | 71 | |
| 72 | p->ced.event_handler(&p->ced); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 73 | |
| 74 | return IRQ_HANDLED; |
| 75 | } |
| 76 | |
| 77 | static void timer8_set_next(struct timer8_priv *p, unsigned long delta) |
| 78 | { |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 79 | unsigned long now; |
| 80 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 81 | if (delta >= 0x10000) |
Daniel Lezcano | 8c09b7d | 2015-11-09 09:02:38 +0100 | [diff] [blame] | 82 | pr_warn("delta out of range\n"); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 83 | now = timer8_get_counter(p); |
| 84 | p->tcora = delta; |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 85 | writeb(readb(p->mapbase + _8TCR) | 0x40, p->mapbase + _8TCR); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 86 | if (delta > now) |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 87 | writew(delta, p->mapbase + TCORA); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 88 | else |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 89 | writew(now + 1, p->mapbase + TCORA); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static int timer8_enable(struct timer8_priv *p) |
| 93 | { |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 94 | writew(0xffff, p->mapbase + TCORA); |
| 95 | writew(0x0000, p->mapbase + _8TCNT); |
| 96 | writew(0x0c02, p->mapbase + _8TCR); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int timer8_start(struct timer8_priv *p) |
| 102 | { |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 103 | int ret; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 104 | |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 105 | if ((p->flags & FLAG_STARTED)) |
| 106 | return 0; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 107 | |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 108 | ret = timer8_enable(p); |
| 109 | if (!ret) |
| 110 | p->flags |= FLAG_STARTED; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 111 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | static void timer8_stop(struct timer8_priv *p) |
| 116 | { |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 117 | writew(0x0000, p->mapbase + _8TCR); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static inline struct timer8_priv *ced_to_priv(struct clock_event_device *ced) |
| 121 | { |
| 122 | return container_of(ced, struct timer8_priv, ced); |
| 123 | } |
| 124 | |
Daniel Lezcano | 1f058d5 | 2015-11-08 17:46:54 +0100 | [diff] [blame] | 125 | 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] | 126 | { |
| 127 | struct clock_event_device *ced = &p->ced; |
| 128 | |
| 129 | timer8_start(p); |
| 130 | |
| 131 | ced->shift = 32; |
| 132 | ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift); |
| 133 | ced->max_delta_ns = clockevent_delta2ns(0xffff, ced); |
| 134 | ced->min_delta_ns = clockevent_delta2ns(0x0001, ced); |
| 135 | |
Daniel Lezcano | 1f058d5 | 2015-11-08 17:46:54 +0100 | [diff] [blame] | 136 | timer8_set_next(p, delta); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 137 | } |
| 138 | |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 139 | static int timer8_clock_event_shutdown(struct clock_event_device *ced) |
| 140 | { |
| 141 | timer8_stop(ced_to_priv(ced)); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int timer8_clock_event_periodic(struct clock_event_device *ced) |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 146 | { |
| 147 | struct timer8_priv *p = ced_to_priv(ced); |
| 148 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 149 | pr_info("%s: used for periodic clock events\n", ced->name); |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 150 | timer8_stop(p); |
Daniel Lezcano | 1f058d5 | 2015-11-08 17:46:54 +0100 | [diff] [blame] | 151 | timer8_clock_event_start(p, (p->rate + HZ/2) / HZ); |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int timer8_clock_event_oneshot(struct clock_event_device *ced) |
| 157 | { |
| 158 | struct timer8_priv *p = ced_to_priv(ced); |
| 159 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 160 | pr_info("%s: used for oneshot clock events\n", ced->name); |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 161 | timer8_stop(p); |
Daniel Lezcano | 1f058d5 | 2015-11-08 17:46:54 +0100 | [diff] [blame] | 162 | timer8_clock_event_start(p, 0x10000); |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 163 | |
| 164 | return 0; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int timer8_clock_event_next(unsigned long delta, |
| 168 | struct clock_event_device *ced) |
| 169 | { |
| 170 | struct timer8_priv *p = ced_to_priv(ced); |
| 171 | |
Viresh Kumar | fc2b2f5 | 2015-07-27 15:02:00 +0530 | [diff] [blame] | 172 | BUG_ON(!clockevent_state_oneshot(ced)); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 173 | timer8_set_next(p, delta - 1); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 178 | static struct timer8_priv timer8_priv = { |
| 179 | .ced = { |
| 180 | .name = "h8300_8timer", |
| 181 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, |
| 182 | .rating = 200, |
| 183 | .set_next_event = timer8_clock_event_next, |
| 184 | .set_state_shutdown = timer8_clock_event_shutdown, |
| 185 | .set_state_periodic = timer8_clock_event_periodic, |
| 186 | .set_state_oneshot = timer8_clock_event_oneshot, |
| 187 | }, |
| 188 | }; |
| 189 | |
| 190 | static void __init h8300_8timer_init(struct device_node *node) |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 191 | { |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 192 | void __iomem *base; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 193 | int irq; |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 194 | int ret = 0; |
| 195 | int rate; |
| 196 | struct clk *clk; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 197 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 198 | clk = of_clk_get(node, 0); |
| 199 | if (IS_ERR(clk)) { |
| 200 | pr_err("failed to get clock for clockevent\n"); |
| 201 | return; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 202 | } |
| 203 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 204 | base = of_iomap(node, 0); |
| 205 | if (!base) { |
| 206 | pr_err("failed to map registers for clockevent\n"); |
| 207 | goto free_clk; |
| 208 | } |
| 209 | |
| 210 | irq = irq_of_parse_and_map(node, 0); |
Daniel Lezcano | 54a0cd5 | 2015-11-08 17:56:18 +0100 | [diff] [blame] | 211 | if (!irq) { |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 212 | pr_err("failed to get irq for clockevent\n"); |
| 213 | goto unmap_reg; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 214 | } |
| 215 | |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame^] | 216 | timer8_priv.mapbase = base; |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 217 | |
| 218 | rate = clk_get_rate(clk) / SCALE; |
| 219 | if (!rate) { |
| 220 | pr_err("Failed to get rate for the clocksource\n"); |
| 221 | goto unmap_reg; |
| 222 | } |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 223 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 224 | ret = request_irq(irq, timer8_interrupt, |
| 225 | IRQF_TIMER, timer8_priv.ced.name, &timer8_priv); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 226 | if (ret < 0) { |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 227 | pr_err("failed to request irq %d for clockevent\n", irq); |
| 228 | goto unmap_reg; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 229 | } |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 230 | |
Daniel Lezcano | cce483e | 2015-11-08 23:24:28 +0100 | [diff] [blame] | 231 | clockevents_config_and_register(&timer8_priv.ced, rate, 1, 0x0000ffff); |
| 232 | |
| 233 | return; |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 234 | unmap_reg: |
| 235 | iounmap(base); |
| 236 | free_clk: |
| 237 | clk_put(clk); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 238 | } |
| 239 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 240 | CLOCKSOURCE_OF_DECLARE(h8300_8bit, "renesas,8bit-timer", h8300_8timer_init); |