Marc Gonzalez | ccd63ce | 2015-10-09 16:59:18 +0200 | [diff] [blame] | 1 | #include <linux/clocksource.h> |
| 2 | #include <linux/sched_clock.h> |
| 3 | #include <linux/of_address.h> |
| 4 | #include <linux/printk.h> |
| 5 | #include <linux/delay.h> |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/clk.h> |
| 8 | |
| 9 | static void __iomem *xtal_in_cnt; |
| 10 | static struct delay_timer delay_timer; |
| 11 | |
| 12 | static unsigned long notrace read_xtal_counter(void) |
| 13 | { |
| 14 | return readl_relaxed(xtal_in_cnt); |
| 15 | } |
| 16 | |
| 17 | static u64 notrace read_sched_clock(void) |
| 18 | { |
| 19 | return read_xtal_counter(); |
| 20 | } |
| 21 | |
Daniel Lezcano | 0683d50 | 2016-06-06 17:59:24 +0200 | [diff] [blame] | 22 | static int __init tango_clocksource_init(struct device_node *np) |
Marc Gonzalez | ccd63ce | 2015-10-09 16:59:18 +0200 | [diff] [blame] | 23 | { |
| 24 | struct clk *clk; |
| 25 | int xtal_freq, ret; |
| 26 | |
| 27 | xtal_in_cnt = of_iomap(np, 0); |
| 28 | if (xtal_in_cnt == NULL) { |
| 29 | pr_err("%s: invalid address\n", np->full_name); |
Daniel Lezcano | 0683d50 | 2016-06-06 17:59:24 +0200 | [diff] [blame] | 30 | return -ENXIO; |
Marc Gonzalez | ccd63ce | 2015-10-09 16:59:18 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | clk = of_clk_get(np, 0); |
| 34 | if (IS_ERR(clk)) { |
| 35 | pr_err("%s: invalid clock\n", np->full_name); |
Daniel Lezcano | 0683d50 | 2016-06-06 17:59:24 +0200 | [diff] [blame] | 36 | return PTR_ERR(clk); |
Marc Gonzalez | ccd63ce | 2015-10-09 16:59:18 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | xtal_freq = clk_get_rate(clk); |
| 40 | delay_timer.freq = xtal_freq; |
| 41 | delay_timer.read_current_timer = read_xtal_counter; |
| 42 | |
Daniel Lezcano | 0881841 | 2015-11-13 10:44:38 +0100 | [diff] [blame] | 43 | ret = clocksource_mmio_init(xtal_in_cnt, "tango-xtal", xtal_freq, 350, |
| 44 | 32, clocksource_mmio_readl_up); |
Daniel Lezcano | 16eeed7 | 2016-04-19 15:43:02 +0200 | [diff] [blame] | 45 | if (ret) { |
Marc Gonzalez | ccd63ce | 2015-10-09 16:59:18 +0200 | [diff] [blame] | 46 | pr_err("%s: registration failed\n", np->full_name); |
Daniel Lezcano | 0683d50 | 2016-06-06 17:59:24 +0200 | [diff] [blame] | 47 | return ret; |
Marc Gonzalez | ccd63ce | 2015-10-09 16:59:18 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | sched_clock_register(read_sched_clock, 32, xtal_freq); |
| 51 | register_current_timer_delay(&delay_timer); |
Daniel Lezcano | 0683d50 | 2016-06-06 17:59:24 +0200 | [diff] [blame] | 52 | |
| 53 | return 0; |
Marc Gonzalez | ccd63ce | 2015-10-09 16:59:18 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Daniel Lezcano | 177cf6e | 2016-06-07 00:27:44 +0200 | [diff] [blame] | 56 | CLOCKSOURCE_OF_DECLARE(tango, "sigma,tick-counter", tango_clocksource_init); |