2 * H8/300 16bit Timer driver
4 * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
7 #include <linux/interrupt.h>
8 #include <linux/init.h>
9 #include <linux/clocksource.h>
10 #include <linux/clk.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
22 #define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
23 #define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
26 struct clocksource cs
;
27 unsigned long total_cycles
;
28 void __iomem
*mapbase
;
29 void __iomem
*mapcommon
;
30 unsigned short cs_enabled
;
36 static unsigned long timer16_get_counter(struct timer16_priv
*p
)
38 unsigned short v1
, v2
, v3
;
41 o1
= ioread8(p
->mapcommon
+ TISRC
) & p
->ovf
;
43 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
46 v1
= ioread16be(p
->mapbase
+ TCNT
);
47 v2
= ioread16be(p
->mapbase
+ TCNT
);
48 v3
= ioread16be(p
->mapbase
+ TCNT
);
49 o1
= ioread8(p
->mapcommon
+ TISRC
) & p
->ovf
;
50 } while (unlikely((o1
!= o2
) || (v1
> v2
&& v1
< v3
)
51 || (v2
> v3
&& v2
< v1
) || (v3
> v1
&& v3
< v2
)));
60 static irqreturn_t
timer16_interrupt(int irq
, void *dev_id
)
62 struct timer16_priv
*p
= (struct timer16_priv
*)dev_id
;
64 bclr(p
->ovf
, p
->mapcommon
+ TISRC
);
65 p
->total_cycles
+= 0x10000;
70 static inline struct timer16_priv
*cs_to_priv(struct clocksource
*cs
)
72 return container_of(cs
, struct timer16_priv
, cs
);
75 static cycle_t
timer16_clocksource_read(struct clocksource
*cs
)
77 struct timer16_priv
*p
= cs_to_priv(cs
);
78 unsigned long raw
, value
;
80 value
= p
->total_cycles
;
81 raw
= timer16_get_counter(p
);
86 static int timer16_enable(struct clocksource
*cs
)
88 struct timer16_priv
*p
= cs_to_priv(cs
);
90 WARN_ON(p
->cs_enabled
);
93 iowrite16be(0x0000, p
->mapbase
+ TCNT
);
94 iowrite8(0x83, p
->mapbase
+ TCR
);
95 bset(p
->ovie
, p
->mapcommon
+ TISRC
);
96 bset(p
->enb
, p
->mapcommon
+ TSTR
);
102 static void timer16_disable(struct clocksource
*cs
)
104 struct timer16_priv
*p
= cs_to_priv(cs
);
106 WARN_ON(!p
->cs_enabled
);
108 bclr(p
->ovie
, p
->mapcommon
+ TISRC
);
109 bclr(p
->enb
, p
->mapcommon
+ TSTR
);
111 p
->cs_enabled
= false;
114 static struct timer16_priv timer16_priv
= {
116 .name
= "h8300_16timer",
118 .read
= timer16_clocksource_read
,
119 .enable
= timer16_enable
,
120 .disable
= timer16_disable
,
121 .mask
= CLOCKSOURCE_MASK(sizeof(unsigned long) * 8),
122 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
129 static int __init
h8300_16timer_init(struct device_node
*node
)
131 void __iomem
*base
[2];
136 clk
= of_clk_get(node
, 0);
138 pr_err("failed to get clock for clocksource\n");
143 base
[REG_CH
] = of_iomap(node
, 0);
145 pr_err("failed to map registers for clocksource\n");
149 base
[REG_COMM
] = of_iomap(node
, 1);
150 if (!base
[REG_COMM
]) {
151 pr_err("failed to map registers for clocksource\n");
156 irq
= irq_of_parse_and_map(node
, 0);
158 pr_err("failed to get irq for clockevent\n");
162 of_property_read_u32(node
, "renesas,channel", &ch
);
164 timer16_priv
.mapbase
= base
[REG_CH
];
165 timer16_priv
.mapcommon
= base
[REG_COMM
];
166 timer16_priv
.enb
= ch
;
167 timer16_priv
.ovf
= ch
;
168 timer16_priv
.ovie
= 4 + ch
;
170 ret
= request_irq(irq
, timer16_interrupt
,
171 IRQF_TIMER
, timer16_priv
.cs
.name
, &timer16_priv
);
173 pr_err("failed to request irq %d of clocksource\n", irq
);
177 clocksource_register_hz(&timer16_priv
.cs
,
178 clk_get_rate(clk
) / 8);
182 iounmap(base
[REG_COMM
]);
184 iounmap(base
[REG_CH
]);
190 CLOCKSOURCE_OF_DECLARE(h8300_16bit
, "renesas,16bit-timer",