h8300_timer16.c 4.0 KB
Newer Older
Y
Yoshinori Sato 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 *  H8/300 16bit Timer driver
 *
 *  Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
 */

#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/clocksource.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/of.h>
13 14
#include <linux/of_address.h>
#include <linux/of_irq.h>
Y
Yoshinori Sato 已提交
15 16 17 18 19 20 21

#define TSTR	0
#define TISRC	6

#define TCR	0
#define TCNT	2

22 23 24
#define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
#define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))

Y
Yoshinori Sato 已提交
25 26 27
struct timer16_priv {
	struct clocksource cs;
	unsigned long total_cycles;
28 29
	void __iomem *mapbase;
	void __iomem *mapcommon;
Y
Yoshinori Sato 已提交
30 31 32
	unsigned short cs_enabled;
	unsigned char enb;
	unsigned char ovf;
33
	unsigned char ovie;
Y
Yoshinori Sato 已提交
34 35 36 37
};

static unsigned long timer16_get_counter(struct timer16_priv *p)
{
38 39
	unsigned short v1, v2, v3;
	unsigned char  o1, o2;
Y
Yoshinori Sato 已提交
40

41
	o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
Y
Yoshinori Sato 已提交
42 43 44 45

	/* Make sure the timer value is stable. Stolen from acpi_pm.c */
	do {
		o2 = o1;
46 47 48 49
		v1 = ioread16be(p->mapbase + TCNT);
		v2 = ioread16be(p->mapbase + TCNT);
		v3 = ioread16be(p->mapbase + TCNT);
		o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
Y
Yoshinori Sato 已提交
50 51 52
	} while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
			  || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));

53 54 55 56
	if (likely(!o1))
		return v2;
	else
		return v2 + 0x10000;
Y
Yoshinori Sato 已提交
57 58 59 60 61 62 63
}


static irqreturn_t timer16_interrupt(int irq, void *dev_id)
{
	struct timer16_priv *p = (struct timer16_priv *)dev_id;

64
	bclr(p->ovf, p->mapcommon + TISRC);
Y
Yoshinori Sato 已提交
65 66 67 68 69 70 71 72 73 74
	p->total_cycles += 0x10000;

	return IRQ_HANDLED;
}

static inline struct timer16_priv *cs_to_priv(struct clocksource *cs)
{
	return container_of(cs, struct timer16_priv, cs);
}

75
static u64 timer16_clocksource_read(struct clocksource *cs)
Y
Yoshinori Sato 已提交
76 77
{
	struct timer16_priv *p = cs_to_priv(cs);
78
	unsigned long raw, value;
Y
Yoshinori Sato 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92

	value = p->total_cycles;
	raw = timer16_get_counter(p);

	return value + raw;
}

static int timer16_enable(struct clocksource *cs)
{
	struct timer16_priv *p = cs_to_priv(cs);

	WARN_ON(p->cs_enabled);

	p->total_cycles = 0;
93 94 95 96
	iowrite16be(0x0000, p->mapbase + TCNT);
	iowrite8(0x83, p->mapbase + TCR);
	bset(p->ovie, p->mapcommon + TISRC);
	bset(p->enb, p->mapcommon + TSTR);
Y
Yoshinori Sato 已提交
97 98 99 100 101 102 103 104 105 106 107

	p->cs_enabled = true;
	return 0;
}

static void timer16_disable(struct clocksource *cs)
{
	struct timer16_priv *p = cs_to_priv(cs);

	WARN_ON(!p->cs_enabled);

108 109
	bclr(p->ovie, p->mapcommon + TISRC);
	bclr(p->enb, p->mapcommon + TSTR);
Y
Yoshinori Sato 已提交
110 111 112 113

	p->cs_enabled = false;
}

114 115 116 117 118 119 120 121 122 123 124 125
static struct timer16_priv timer16_priv = {
	.cs = {
		.name = "h8300_16timer",
		.rating = 200,
		.read = timer16_clocksource_read,
		.enable = timer16_enable,
		.disable = timer16_disable,
		.mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8),
		.flags = CLOCK_SOURCE_IS_CONTINUOUS,
	},
};

Y
Yoshinori Sato 已提交
126 127 128
#define REG_CH   0
#define REG_COMM 1

129
static int __init h8300_16timer_init(struct device_node *node)
Y
Yoshinori Sato 已提交
130
{
131
	void __iomem *base[2];
Y
Yoshinori Sato 已提交
132 133
	int ret, irq;
	unsigned int ch;
134
	struct clk *clk;
Y
Yoshinori Sato 已提交
135

136 137 138
	clk = of_clk_get(node, 0);
	if (IS_ERR(clk)) {
		pr_err("failed to get clock for clocksource\n");
139
		return PTR_ERR(clk);
Y
Yoshinori Sato 已提交
140 141
	}

142
	ret = -ENXIO;
143 144 145 146
	base[REG_CH] = of_iomap(node, 0);
	if (!base[REG_CH]) {
		pr_err("failed to map registers for clocksource\n");
		goto free_clk;
Y
Yoshinori Sato 已提交
147 148
	}

149 150 151 152
	base[REG_COMM] = of_iomap(node, 1);
	if (!base[REG_COMM]) {
		pr_err("failed to map registers for clocksource\n");
		goto unmap_ch;
Y
Yoshinori Sato 已提交
153 154
	}

155
	ret = -EINVAL;
156
	irq = irq_of_parse_and_map(node, 0);
157
	if (!irq) {
158 159
		pr_err("failed to get irq for clockevent\n");
		goto unmap_comm;
Y
Yoshinori Sato 已提交
160 161
	}

162
	of_property_read_u32(node, "renesas,channel", &ch);
Y
Yoshinori Sato 已提交
163

164 165
	timer16_priv.mapbase = base[REG_CH];
	timer16_priv.mapcommon = base[REG_COMM];
166 167 168
	timer16_priv.enb = ch;
	timer16_priv.ovf = ch;
	timer16_priv.ovie = 4 + ch;
Y
Yoshinori Sato 已提交
169

170 171 172 173 174
	ret = request_irq(irq, timer16_interrupt,
			  IRQF_TIMER, timer16_priv.cs.name, &timer16_priv);
	if (ret < 0) {
		pr_err("failed to request irq %d of clocksource\n", irq);
		goto unmap_comm;
Y
Yoshinori Sato 已提交
175 176
	}

177
	clocksource_register_hz(&timer16_priv.cs,
178
				clk_get_rate(clk) / 8);
179
	return 0;
Y
Yoshinori Sato 已提交
180

181 182 183 184 185 186
unmap_comm:
	iounmap(base[REG_COMM]);
unmap_ch:
	iounmap(base[REG_CH]);
free_clk:
	clk_put(clk);
187
	return ret;
Y
Yoshinori Sato 已提交
188 189
}

190
TIMER_OF_DECLARE(h8300_16bit, "renesas,16bit-timer",
191
			   h8300_16timer_init);