blob: 129dca02b3ee64cb48eaef6a06c0b2af2db4ef1d [file] [log] [blame]
Yoshinori Sato618b9022015-01-28 02:52:42 +09001/*
2 * H8/300 16bit Timer driver
3 *
4 * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
5 */
6
Yoshinori Sato618b9022015-01-28 02:52:42 +09007#include <linux/interrupt.h>
8#include <linux/init.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +09009#include <linux/clocksource.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090010#include <linux/clk.h>
11#include <linux/io.h>
12#include <linux/of.h>
Yoshinori Sato4633f4c2015-11-07 01:31:44 +090013#include <linux/of_address.h>
14#include <linux/of_irq.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090015
Yoshinori Sato618b9022015-01-28 02:52:42 +090016#define TSTR 0
Yoshinori Sato618b9022015-01-28 02:52:42 +090017#define TISRA 4
Yoshinori Sato618b9022015-01-28 02:52:42 +090018#define TISRC 6
19
20#define TCR 0
Yoshinori Sato618b9022015-01-28 02:52:42 +090021#define TCNT 2
Yoshinori Sato618b9022015-01-28 02:52:42 +090022
23struct timer16_priv {
Yoshinori Sato618b9022015-01-28 02:52:42 +090024 struct clocksource cs;
Yoshinori Sato618b9022015-01-28 02:52:42 +090025 unsigned long total_cycles;
26 unsigned long mapbase;
27 unsigned long mapcommon;
Yoshinori Sato618b9022015-01-28 02:52:42 +090028 unsigned short cs_enabled;
29 unsigned char enb;
30 unsigned char imfa;
31 unsigned char imiea;
32 unsigned char ovf;
33 raw_spinlock_t lock;
34 struct clk *clk;
35};
36
37static unsigned long timer16_get_counter(struct timer16_priv *p)
38{
39 unsigned long v1, v2, v3;
40 int o1, o2;
41
42 o1 = ctrl_inb(p->mapcommon + TISRC) & p->ovf;
43
44 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
45 do {
46 o2 = o1;
47 v1 = ctrl_inw(p->mapbase + TCNT);
48 v2 = ctrl_inw(p->mapbase + TCNT);
49 v3 = ctrl_inw(p->mapbase + TCNT);
50 o1 = ctrl_inb(p->mapcommon + TISRC) & p->ovf;
51 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
52 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
53
54 v2 |= 0x10000;
55 return v2;
56}
57
58
59static irqreturn_t timer16_interrupt(int irq, void *dev_id)
60{
61 struct timer16_priv *p = (struct timer16_priv *)dev_id;
62
63 ctrl_outb(ctrl_inb(p->mapcommon + TISRA) & ~p->imfa,
64 p->mapcommon + TISRA);
65 p->total_cycles += 0x10000;
66
67 return IRQ_HANDLED;
68}
69
70static inline struct timer16_priv *cs_to_priv(struct clocksource *cs)
71{
72 return container_of(cs, struct timer16_priv, cs);
73}
74
75static cycle_t timer16_clocksource_read(struct clocksource *cs)
76{
77 struct timer16_priv *p = cs_to_priv(cs);
78 unsigned long flags, raw;
79 unsigned long value;
80
81 raw_spin_lock_irqsave(&p->lock, flags);
82 value = p->total_cycles;
83 raw = timer16_get_counter(p);
84 raw_spin_unlock_irqrestore(&p->lock, flags);
85
86 return value + raw;
87}
88
89static int timer16_enable(struct clocksource *cs)
90{
91 struct timer16_priv *p = cs_to_priv(cs);
92
93 WARN_ON(p->cs_enabled);
94
95 p->total_cycles = 0;
96 ctrl_outw(0x0000, p->mapbase + TCNT);
97 ctrl_outb(0x83, p->mapbase + TCR);
98 ctrl_outb(ctrl_inb(p->mapcommon + TSTR) | p->enb,
99 p->mapcommon + TSTR);
100
101 p->cs_enabled = true;
102 return 0;
103}
104
105static void timer16_disable(struct clocksource *cs)
106{
107 struct timer16_priv *p = cs_to_priv(cs);
108
109 WARN_ON(!p->cs_enabled);
110
111 ctrl_outb(ctrl_inb(p->mapcommon + TSTR) & ~p->enb,
112 p->mapcommon + TSTR);
113
114 p->cs_enabled = false;
115}
116
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900117static struct timer16_priv timer16_priv = {
118 .cs = {
119 .name = "h8300_16timer",
120 .rating = 200,
121 .read = timer16_clocksource_read,
122 .enable = timer16_enable,
123 .disable = timer16_disable,
124 .mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8),
125 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
126 },
127};
128
Yoshinori Sato618b9022015-01-28 02:52:42 +0900129#define REG_CH 0
130#define REG_COMM 1
131
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900132static void __init h8300_16timer_init(struct device_node *node)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900133{
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900134 void __iomem *base[2];
Yoshinori Sato618b9022015-01-28 02:52:42 +0900135 int ret, irq;
136 unsigned int ch;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900137 struct clk *clk;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900138
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900139 clk = of_clk_get(node, 0);
140 if (IS_ERR(clk)) {
141 pr_err("failed to get clock for clocksource\n");
142 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900143 }
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900144
145 base[REG_CH] = of_iomap(node, 0);
146 if (!base[REG_CH]) {
147 pr_err("failed to map registers for clocksource\n");
148 goto free_clk;
149 }
150
151 base[REG_COMM] = of_iomap(node, 1);
152 if (!base[REG_COMM]) {
153 pr_err("failed to map registers for clocksource\n");
154 goto unmap_ch;
155 }
156
157 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcano5019c902015-11-09 10:52:35 +0100158 if (!irq) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900159 pr_err("failed to get irq for clockevent\n");
160 goto unmap_comm;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900161 }
162
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900163 of_property_read_u32(node, "renesas,channel", &ch);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900164
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900165 timer16_priv.mapbase = (unsigned long)base[REG_CH];
166 timer16_priv.mapcommon = (unsigned long)base[REG_COMM];
167 timer16_priv.enb = 1 << ch;
168 timer16_priv.imfa = 1 << ch;
169 timer16_priv.imiea = 1 << (4 + ch);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900170
171 ret = request_irq(irq, timer16_interrupt,
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900172 IRQF_TIMER, timer16_priv.cs.name, &timer16_priv);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900173 if (ret < 0) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900174 pr_err("failed to request irq %d of clocksource\n", irq);
175 goto unmap_comm;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900176 }
177
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900178 clocksource_register_hz(&timer16_priv.cs,
179 clk_get_rate(timer16_priv.clk) / 8);
180 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900181
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900182unmap_comm:
183 iounmap(base[REG_COMM]);
184unmap_ch:
185 iounmap(base[REG_CH]);
186free_clk:
187 clk_put(clk);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900188}
189
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900190CLOCKSOURCE_OF_DECLARE(h8300_16bit, "renesas,16bit-timer", h8300_16timer_init);