4 * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
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>
35 struct platform_device
*pdev
;
36 struct clocksource cs
;
38 unsigned long mapbase1
;
39 unsigned long mapbase2
;
41 unsigned int cs_enabled
;
44 static inline unsigned long read_tcnt32(struct tpu_priv
*p
)
48 tcnt
= ctrl_inw(p
->mapbase1
+ TCNT
) << 16;
49 tcnt
|= ctrl_inw(p
->mapbase2
+ TCNT
);
53 static int tpu_get_counter(struct tpu_priv
*p
, unsigned long long *val
)
55 unsigned long v1
, v2
, v3
;
58 o1
= ctrl_inb(p
->mapbase1
+ TSR
) & 0x10;
60 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
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
)));
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
);
83 unsigned long long value
;
85 raw_spin_lock_irqsave(&p
->lock
, flags
);
86 if (tpu_get_counter(p
, &value
))
88 raw_spin_unlock_irqrestore(&p
->lock
, flags
);
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;
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;
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
));
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");
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
;
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
);
158 static int tpu_probe(struct platform_device
*pdev
)
160 struct tpu_priv
*p
= platform_get_drvdata(pdev
);
163 dev_info(&pdev
->dev
, "kept as earlytimer\n");
167 p
= devm_kzalloc(&pdev
->dev
, sizeof(*p
), GFP_KERNEL
);
171 return tpu_setup(p
, pdev
);
174 static int tpu_remove(struct platform_device
*pdev
)
179 static const struct of_device_id tpu_of_table
[] = {
180 { .compatible
= "renesas,tpu" },
184 static struct platform_driver tpu_driver
= {
186 .remove
= tpu_remove
,
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");