]>
Commit | Line | Data |
---|---|---|
618b902d YS |
1 | /* |
2 | * H8/300 16bit Timer driver | |
3 | * | |
4 | * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp> | |
5 | */ | |
6 | ||
618b902d YS |
7 | #include <linux/interrupt.h> |
8 | #include <linux/init.h> | |
618b902d | 9 | #include <linux/clocksource.h> |
618b902d YS |
10 | #include <linux/clk.h> |
11 | #include <linux/io.h> | |
12 | #include <linux/of.h> | |
4633f4ca YS |
13 | #include <linux/of_address.h> |
14 | #include <linux/of_irq.h> | |
618b902d | 15 | |
618b902d | 16 | #define TSTR 0 |
618b902d | 17 | #define TISRA 4 |
618b902d YS |
18 | #define TISRC 6 |
19 | ||
20 | #define TCR 0 | |
618b902d | 21 | #define TCNT 2 |
618b902d YS |
22 | |
23 | struct timer16_priv { | |
618b902d | 24 | struct clocksource cs; |
618b902d YS |
25 | unsigned long total_cycles; |
26 | unsigned long mapbase; | |
27 | unsigned long mapcommon; | |
618b902d YS |
28 | unsigned short cs_enabled; |
29 | unsigned char enb; | |
30 | unsigned char imfa; | |
31 | unsigned char imiea; | |
32 | unsigned char ovf; | |
618b902d YS |
33 | struct clk *clk; |
34 | }; | |
35 | ||
36 | static unsigned long timer16_get_counter(struct timer16_priv *p) | |
37 | { | |
38 | unsigned long v1, v2, v3; | |
39 | int o1, o2; | |
40 | ||
41 | o1 = ctrl_inb(p->mapcommon + TISRC) & p->ovf; | |
42 | ||
43 | /* Make sure the timer value is stable. Stolen from acpi_pm.c */ | |
44 | do { | |
45 | o2 = o1; | |
46 | v1 = ctrl_inw(p->mapbase + TCNT); | |
47 | v2 = ctrl_inw(p->mapbase + TCNT); | |
48 | v3 = ctrl_inw(p->mapbase + TCNT); | |
49 | o1 = ctrl_inb(p->mapcommon + TISRC) & p->ovf; | |
50 | } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3) | |
51 | || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2))); | |
52 | ||
53 | v2 |= 0x10000; | |
54 | return v2; | |
55 | } | |
56 | ||
57 | ||
58 | static irqreturn_t timer16_interrupt(int irq, void *dev_id) | |
59 | { | |
60 | struct timer16_priv *p = (struct timer16_priv *)dev_id; | |
61 | ||
62 | ctrl_outb(ctrl_inb(p->mapcommon + TISRA) & ~p->imfa, | |
63 | p->mapcommon + TISRA); | |
64 | p->total_cycles += 0x10000; | |
65 | ||
66 | return IRQ_HANDLED; | |
67 | } | |
68 | ||
69 | static inline struct timer16_priv *cs_to_priv(struct clocksource *cs) | |
70 | { | |
71 | return container_of(cs, struct timer16_priv, cs); | |
72 | } | |
73 | ||
74 | static cycle_t timer16_clocksource_read(struct clocksource *cs) | |
75 | { | |
76 | struct timer16_priv *p = cs_to_priv(cs); | |
05de7ed6 | 77 | unsigned long raw, value; |
618b902d | 78 | |
618b902d YS |
79 | value = p->total_cycles; |
80 | raw = timer16_get_counter(p); | |
618b902d YS |
81 | |
82 | return value + raw; | |
83 | } | |
84 | ||
85 | static int timer16_enable(struct clocksource *cs) | |
86 | { | |
87 | struct timer16_priv *p = cs_to_priv(cs); | |
88 | ||
89 | WARN_ON(p->cs_enabled); | |
90 | ||
91 | p->total_cycles = 0; | |
92 | ctrl_outw(0x0000, p->mapbase + TCNT); | |
93 | ctrl_outb(0x83, p->mapbase + TCR); | |
94 | ctrl_outb(ctrl_inb(p->mapcommon + TSTR) | p->enb, | |
95 | p->mapcommon + TSTR); | |
96 | ||
97 | p->cs_enabled = true; | |
98 | return 0; | |
99 | } | |
100 | ||
101 | static void timer16_disable(struct clocksource *cs) | |
102 | { | |
103 | struct timer16_priv *p = cs_to_priv(cs); | |
104 | ||
105 | WARN_ON(!p->cs_enabled); | |
106 | ||
107 | ctrl_outb(ctrl_inb(p->mapcommon + TSTR) & ~p->enb, | |
108 | p->mapcommon + TSTR); | |
109 | ||
110 | p->cs_enabled = false; | |
111 | } | |
112 | ||
4633f4ca YS |
113 | static struct timer16_priv timer16_priv = { |
114 | .cs = { | |
115 | .name = "h8300_16timer", | |
116 | .rating = 200, | |
117 | .read = timer16_clocksource_read, | |
118 | .enable = timer16_enable, | |
119 | .disable = timer16_disable, | |
120 | .mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8), | |
121 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, | |
122 | }, | |
123 | }; | |
124 | ||
618b902d YS |
125 | #define REG_CH 0 |
126 | #define REG_COMM 1 | |
127 | ||
4633f4ca | 128 | static void __init h8300_16timer_init(struct device_node *node) |
618b902d | 129 | { |
4633f4ca | 130 | void __iomem *base[2]; |
618b902d YS |
131 | int ret, irq; |
132 | unsigned int ch; | |
4633f4ca | 133 | struct clk *clk; |
618b902d | 134 | |
4633f4ca YS |
135 | clk = of_clk_get(node, 0); |
136 | if (IS_ERR(clk)) { | |
137 | pr_err("failed to get clock for clocksource\n"); | |
138 | return; | |
618b902d YS |
139 | } |
140 | ||
4633f4ca YS |
141 | base[REG_CH] = of_iomap(node, 0); |
142 | if (!base[REG_CH]) { | |
143 | pr_err("failed to map registers for clocksource\n"); | |
144 | goto free_clk; | |
618b902d | 145 | } |
618b902d | 146 | |
4633f4ca YS |
147 | base[REG_COMM] = of_iomap(node, 1); |
148 | if (!base[REG_COMM]) { | |
149 | pr_err("failed to map registers for clocksource\n"); | |
150 | goto unmap_ch; | |
618b902d YS |
151 | } |
152 | ||
4633f4ca | 153 | irq = irq_of_parse_and_map(node, 0); |
5019c902 | 154 | if (!irq) { |
4633f4ca YS |
155 | pr_err("failed to get irq for clockevent\n"); |
156 | goto unmap_comm; | |
618b902d YS |
157 | } |
158 | ||
4633f4ca | 159 | of_property_read_u32(node, "renesas,channel", &ch); |
618b902d | 160 | |
4633f4ca YS |
161 | timer16_priv.mapbase = (unsigned long)base[REG_CH]; |
162 | timer16_priv.mapcommon = (unsigned long)base[REG_COMM]; | |
163 | timer16_priv.enb = 1 << ch; | |
164 | timer16_priv.imfa = 1 << ch; | |
165 | timer16_priv.imiea = 1 << (4 + ch); | |
618b902d | 166 | |
4633f4ca YS |
167 | ret = request_irq(irq, timer16_interrupt, |
168 | IRQF_TIMER, timer16_priv.cs.name, &timer16_priv); | |
169 | if (ret < 0) { | |
170 | pr_err("failed to request irq %d of clocksource\n", irq); | |
171 | goto unmap_comm; | |
618b902d | 172 | } |
618b902d | 173 | |
4633f4ca YS |
174 | clocksource_register_hz(&timer16_priv.cs, |
175 | clk_get_rate(timer16_priv.clk) / 8); | |
176 | return; | |
618b902d | 177 | |
4633f4ca YS |
178 | unmap_comm: |
179 | iounmap(base[REG_COMM]); | |
180 | unmap_ch: | |
181 | iounmap(base[REG_CH]); | |
182 | free_clk: | |
183 | clk_put(clk); | |
618b902d YS |
184 | } |
185 | ||
4633f4ca | 186 | CLOCKSOURCE_OF_DECLARE(h8300_16bit, "renesas,16bit-timer", h8300_16timer_init); |