Merge git://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending
[linux/fpc-iii.git] / drivers / clocksource / h8300_tpu.c
blob64195fdd78bf78e90a1f3c264a1e88716b6c7f25
1 /*
2 * H8/300 TPU Driver
4 * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
6 */
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>
21 #include <asm/irq.h>
23 #define TCR 0
24 #define TMDR 1
25 #define TIOR 2
26 #define TER 4
27 #define TSR 5
28 #define TCNT 6
29 #define TGRA 8
30 #define TGRB 10
31 #define TGRC 12
32 #define TGRD 14
34 struct tpu_priv {
35 struct platform_device *pdev;
36 struct clocksource cs;
37 struct clk *clk;
38 unsigned long mapbase1;
39 unsigned long mapbase2;
40 raw_spinlock_t lock;
41 unsigned int cs_enabled;
44 static inline unsigned long read_tcnt32(struct tpu_priv *p)
46 unsigned long tcnt;
48 tcnt = ctrl_inw(p->mapbase1 + TCNT) << 16;
49 tcnt |= ctrl_inw(p->mapbase2 + TCNT);
50 return tcnt;
53 static int tpu_get_counter(struct tpu_priv *p, unsigned long long *val)
55 unsigned long v1, v2, v3;
56 int o1, o2;
58 o1 = ctrl_inb(p->mapbase1 + TSR) & 0x10;
60 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
61 do {
62 o2 = o1;
63 v1 = read_tcnt32(p);
64 v2 = read_tcnt32(p);
65 v3 = read_tcnt32(p);
66 o1 = ctrl_inb(p->mapbase1 + TSR) & 0x10;
67 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
68 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
70 *val = v2;
71 return o1;
74 static inline struct tpu_priv *cs_to_priv(struct clocksource *cs)
76 return container_of(cs, struct tpu_priv, cs);
79 static cycle_t tpu_clocksource_read(struct clocksource *cs)
81 struct tpu_priv *p = cs_to_priv(cs);
82 unsigned long flags;
83 unsigned long long value;
85 raw_spin_lock_irqsave(&p->lock, flags);
86 if (tpu_get_counter(p, &value))
87 value += 0x100000000;
88 raw_spin_unlock_irqrestore(&p->lock, flags);
90 return value;
93 static int tpu_clocksource_enable(struct clocksource *cs)
95 struct tpu_priv *p = cs_to_priv(cs);
97 WARN_ON(p->cs_enabled);
99 ctrl_outw(0, p->mapbase1 + TCNT);
100 ctrl_outw(0, p->mapbase2 + TCNT);
101 ctrl_outb(0x0f, p->mapbase1 + TCR);
102 ctrl_outb(0x03, p->mapbase2 + TCR);
104 p->cs_enabled = true;
105 return 0;
108 static void tpu_clocksource_disable(struct clocksource *cs)
110 struct tpu_priv *p = cs_to_priv(cs);
112 WARN_ON(!p->cs_enabled);
114 ctrl_outb(0, p->mapbase1 + TCR);
115 ctrl_outb(0, p->mapbase2 + TCR);
116 p->cs_enabled = false;
119 #define CH_L 0
120 #define CH_H 1
122 static int __init tpu_setup(struct tpu_priv *p, struct platform_device *pdev)
124 struct resource *res[2];
126 memset(p, 0, sizeof(*p));
127 p->pdev = pdev;
129 res[CH_L] = platform_get_resource(p->pdev, IORESOURCE_MEM, CH_L);
130 res[CH_H] = platform_get_resource(p->pdev, IORESOURCE_MEM, CH_H);
131 if (!res[CH_L] || !res[CH_H]) {
132 dev_err(&p->pdev->dev, "failed to get I/O memory\n");
133 return -ENXIO;
136 p->clk = clk_get(&p->pdev->dev, "fck");
137 if (IS_ERR(p->clk)) {
138 dev_err(&p->pdev->dev, "can't get clk\n");
139 return PTR_ERR(p->clk);
142 p->mapbase1 = res[CH_L]->start;
143 p->mapbase2 = res[CH_H]->start;
145 p->cs.name = pdev->name;
146 p->cs.rating = 200;
147 p->cs.read = tpu_clocksource_read;
148 p->cs.enable = tpu_clocksource_enable;
149 p->cs.disable = tpu_clocksource_disable;
150 p->cs.mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
151 p->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
152 clocksource_register_hz(&p->cs, clk_get_rate(p->clk) / 64);
153 platform_set_drvdata(pdev, p);
155 return 0;
158 static int tpu_probe(struct platform_device *pdev)
160 struct tpu_priv *p = platform_get_drvdata(pdev);
162 if (p) {
163 dev_info(&pdev->dev, "kept as earlytimer\n");
164 return 0;
167 p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
168 if (!p)
169 return -ENOMEM;
171 return tpu_setup(p, pdev);
174 static int tpu_remove(struct platform_device *pdev)
176 return -EBUSY;
179 static const struct of_device_id tpu_of_table[] = {
180 { .compatible = "renesas,tpu" },
184 static struct platform_driver tpu_driver = {
185 .probe = tpu_probe,
186 .remove = tpu_remove,
187 .driver = {
188 .name = "h8s-tpu",
189 .of_match_table = of_match_ptr(tpu_of_table),
193 static int __init tpu_init(void)
195 return platform_driver_register(&tpu_driver);
198 static void __exit tpu_exit(void)
200 platform_driver_unregister(&tpu_driver);
203 subsys_initcall(tpu_init);
204 module_exit(tpu_exit);
205 MODULE_AUTHOR("Yoshinori Sato");
206 MODULE_DESCRIPTION("H8S Timer Pulse Unit Driver");
207 MODULE_LICENSE("GPL v2");