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