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>
23 struct clocksource cs
;
24 unsigned long total_cycles
;
25 void __iomem
*mapbase
;
26 void __iomem
*mapcommon
;
27 unsigned short cs_enabled
;
34 static unsigned long timer16_get_counter(struct timer16_priv
*p
)
36 unsigned long v1
, v2
, v3
;
39 o1
= readb(p
->mapcommon
+ TISRC
) & p
->ovf
;
41 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
44 v1
= readw(p
->mapbase
+ TCNT
);
45 v2
= readw(p
->mapbase
+ TCNT
);
46 v3
= readw(p
->mapbase
+ TCNT
);
47 o1
= readb(p
->mapcommon
+ TISRC
) & p
->ovf
;
48 } while (unlikely((o1
!= o2
) || (v1
> v2
&& v1
< v3
)
49 || (v2
> v3
&& v2
< v1
) || (v3
> v1
&& v3
< v2
)));
56 static irqreturn_t
timer16_interrupt(int irq
, void *dev_id
)
58 struct timer16_priv
*p
= (struct timer16_priv
*)dev_id
;
60 writeb(readb(p
->mapcommon
+ TISRC
) & ~p
->ovf
,
61 p
->mapcommon
+ TISRC
);
62 p
->total_cycles
+= 0x10000;
67 static inline struct timer16_priv
*cs_to_priv(struct clocksource
*cs
)
69 return container_of(cs
, struct timer16_priv
, cs
);
72 static cycle_t
timer16_clocksource_read(struct clocksource
*cs
)
74 struct timer16_priv
*p
= cs_to_priv(cs
);
75 unsigned long raw
, value
;
77 value
= p
->total_cycles
;
78 raw
= timer16_get_counter(p
);
83 static int timer16_enable(struct clocksource
*cs
)
85 struct timer16_priv
*p
= cs_to_priv(cs
);
87 WARN_ON(p
->cs_enabled
);
90 writew(0x0000, p
->mapbase
+ TCNT
);
91 writeb(0x83, p
->mapbase
+ TCR
);
92 writeb(readb(p
->mapcommon
+ TSTR
) | p
->enb
,
94 writeb(readb(p
->mapcommon
+ TISRC
) | p
->ovie
,
101 static void timer16_disable(struct clocksource
*cs
)
103 struct timer16_priv
*p
= cs_to_priv(cs
);
105 WARN_ON(!p
->cs_enabled
);
107 writeb(readb(p
->mapcommon
+ TSTR
) & ~p
->enb
,
108 p
->mapcommon
+ TSTR
);
110 p
->cs_enabled
= false;
113 static struct timer16_priv timer16_priv
= {
115 .name
= "h8300_16timer",
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
,
128 static void __init
h8300_16timer_init(struct device_node
*node
)
130 void __iomem
*base
[2];
135 clk
= of_clk_get(node
, 0);
137 pr_err("failed to get clock for clocksource\n");
141 base
[REG_CH
] = of_iomap(node
, 0);
143 pr_err("failed to map registers for clocksource\n");
147 base
[REG_COMM
] = of_iomap(node
, 1);
148 if (!base
[REG_COMM
]) {
149 pr_err("failed to map registers for clocksource\n");
153 irq
= irq_of_parse_and_map(node
, 0);
155 pr_err("failed to get irq for clockevent\n");
159 of_property_read_u32(node
, "renesas,channel", &ch
);
161 timer16_priv
.mapbase
= base
[REG_CH
];
162 timer16_priv
.mapcommon
= base
[REG_COMM
];
163 timer16_priv
.enb
= 1 << ch
;
164 timer16_priv
.ovf
= 1 << ch
;
165 timer16_priv
.ovie
= 1 << (4 + ch
);
167 ret
= request_irq(irq
, timer16_interrupt
,
168 IRQF_TIMER
, timer16_priv
.cs
.name
, &timer16_priv
);
170 pr_err("failed to request irq %d of clocksource\n", irq
);
174 clocksource_register_hz(&timer16_priv
.cs
,
175 clk_get_rate(timer16_priv
.clk
) / 8);
179 iounmap(base
[REG_COMM
]);
181 iounmap(base
[REG_CH
]);
186 CLOCKSOURCE_OF_DECLARE(h8300_16bit
, "renesas,16bit-timer", h8300_16timer_init
);