blob: 576dae6f2a9177572439b021ae60bb161606d124 [file] [log] [blame]
Yoshinori Sato618b9022015-01-28 02:52:42 +09001/*
Yoshinori Sato4633f4c2015-11-07 01:31:44 +09002 * H8S TPU Driver
Yoshinori Sato618b9022015-01-28 02:52:42 +09003 *
4 * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
5 *
6 */
7
8#include <linux/errno.h>
9#include <linux/sched.h>
10#include <linux/kernel.h>
11#include <linux/interrupt.h>
12#include <linux/init.h>
13#include <linux/platform_device.h>
14#include <linux/slab.h>
15#include <linux/clocksource.h>
16#include <linux/module.h>
17#include <linux/clk.h>
18#include <linux/io.h>
19#include <linux/of.h>
Yoshinori Sato4633f4c2015-11-07 01:31:44 +090020#include <linux/of_address.h>
21#include <linux/of_irq.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090022
Daniel Lezcano9471f1d2015-11-07 14:18:51 +010023#define TCR 0x0
24#define TSR 0x5
25#define TCNT 0x6
Yoshinori Sato618b9022015-01-28 02:52:42 +090026
27struct tpu_priv {
Yoshinori Sato618b9022015-01-28 02:52:42 +090028 struct clocksource cs;
Yoshinori Sato618b9022015-01-28 02:52:42 +090029 unsigned long mapbase1;
30 unsigned long mapbase2;
31 raw_spinlock_t lock;
32 unsigned int cs_enabled;
33};
34
35static inline unsigned long read_tcnt32(struct tpu_priv *p)
36{
37 unsigned long tcnt;
38
39 tcnt = ctrl_inw(p->mapbase1 + TCNT) << 16;
40 tcnt |= ctrl_inw(p->mapbase2 + TCNT);
41 return tcnt;
42}
43
44static int tpu_get_counter(struct tpu_priv *p, unsigned long long *val)
45{
46 unsigned long v1, v2, v3;
47 int o1, o2;
48
49 o1 = ctrl_inb(p->mapbase1 + TSR) & 0x10;
50
51 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
52 do {
53 o2 = o1;
54 v1 = read_tcnt32(p);
55 v2 = read_tcnt32(p);
56 v3 = read_tcnt32(p);
57 o1 = ctrl_inb(p->mapbase1 + TSR) & 0x10;
58 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
59 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
60
61 *val = v2;
62 return o1;
63}
64
65static inline struct tpu_priv *cs_to_priv(struct clocksource *cs)
66{
67 return container_of(cs, struct tpu_priv, cs);
68}
69
70static cycle_t tpu_clocksource_read(struct clocksource *cs)
71{
72 struct tpu_priv *p = cs_to_priv(cs);
73 unsigned long flags;
74 unsigned long long value;
75
76 raw_spin_lock_irqsave(&p->lock, flags);
77 if (tpu_get_counter(p, &value))
78 value += 0x100000000;
79 raw_spin_unlock_irqrestore(&p->lock, flags);
80
81 return value;
82}
83
84static int tpu_clocksource_enable(struct clocksource *cs)
85{
86 struct tpu_priv *p = cs_to_priv(cs);
87
88 WARN_ON(p->cs_enabled);
89
90 ctrl_outw(0, p->mapbase1 + TCNT);
91 ctrl_outw(0, p->mapbase2 + TCNT);
92 ctrl_outb(0x0f, p->mapbase1 + TCR);
93 ctrl_outb(0x03, p->mapbase2 + TCR);
94
95 p->cs_enabled = true;
96 return 0;
97}
98
99static void tpu_clocksource_disable(struct clocksource *cs)
100{
101 struct tpu_priv *p = cs_to_priv(cs);
102
103 WARN_ON(!p->cs_enabled);
104
105 ctrl_outb(0, p->mapbase1 + TCR);
106 ctrl_outb(0, p->mapbase2 + TCR);
107 p->cs_enabled = false;
108}
109
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900110static struct tpu_priv tpu_priv = {
111 .cs = {
112 .name = "H8S_TPU",
113 .rating = 200,
114 .read = tpu_clocksource_read,
115 .enable = tpu_clocksource_enable,
116 .disable = tpu_clocksource_disable,
117 .mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8),
118 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
119 },
120};
121
Yoshinori Sato618b9022015-01-28 02:52:42 +0900122#define CH_L 0
123#define CH_H 1
124
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900125static void __init h8300_tpu_init(struct device_node *node)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900126{
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900127 void __iomem *base[2];
128 struct clk *clk;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900129
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900130 clk = of_clk_get(node, 0);
131 if (IS_ERR(clk)) {
132 pr_err("failed to get clock for clocksource\n");
133 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900134 }
135
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900136 base[CH_L] = of_iomap(node, CH_L);
137 if (!base[CH_L]) {
138 pr_err("failed to map registers for clocksource\n");
139 goto free_clk;
140 }
141 base[CH_H] = of_iomap(node, CH_H);
142 if (!base[CH_H]) {
143 pr_err("failed to map registers for clocksource\n");
144 goto unmap_L;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900145 }
146
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900147 tpu_priv.mapbase1 = (unsigned long)base[CH_L];
148 tpu_priv.mapbase2 = (unsigned long)base[CH_H];
Yoshinori Sato618b9022015-01-28 02:52:42 +0900149
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900150 clocksource_register_hz(&tpu_priv.cs, clk_get_rate(clk) / 64);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900151
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900152 return;
153
154unmap_L:
155 iounmap(base[CH_H]);
156free_clk:
157 clk_put(clk);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900158}
159
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900160CLOCKSOURCE_OF_DECLARE(h8300_tpu, "renesas,tpu", h8300_tpu_init);