blob: 3245eb0c602d24c7aaccbe915a1da518ab58b98b [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Dinh Nguyencfda5902012-07-11 15:13:16 -05002/*
3 * Copyright (C) 2012 Altera Corporation
4 * Copyright (c) 2011 Picochip Ltd., Jamie Iles
5 *
6 * Modified from mach-picoxcell/time.c
Dinh Nguyencfda5902012-07-11 15:13:16 -05007 */
Jisheng Zhang9115df82015-11-05 10:32:06 +08008#include <linux/delay.h>
Dinh Nguyencfda5902012-07-11 15:13:16 -05009#include <linux/dw_apb_timer.h>
10#include <linux/of.h>
11#include <linux/of_address.h>
12#include <linux/of_irq.h>
Heiko Stuebnera8b447f2013-06-04 11:37:36 +020013#include <linux/clk.h>
Dinh Nguyen1f174a12018-09-17 09:52:14 -050014#include <linux/reset.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070015#include <linux/sched_clock.h>
Dinh Nguyencfda5902012-07-11 15:13:16 -050016
Dinh Nguyen5d9814d2020-12-05 04:52:23 -060017static int __init timer_get_base_and_rate(struct device_node *np,
Dinh Nguyencfda5902012-07-11 15:13:16 -050018 void __iomem **base, u32 *rate)
19{
Heiko Stuebnera8b447f2013-06-04 11:37:36 +020020 struct clk *timer_clk;
21 struct clk *pclk;
Dinh Nguyen1f174a12018-09-17 09:52:14 -050022 struct reset_control *rstc;
Dinh Nguyen5d9814d2020-12-05 04:52:23 -060023 int ret;
Heiko Stuebnera8b447f2013-06-04 11:37:36 +020024
Dinh Nguyencfda5902012-07-11 15:13:16 -050025 *base = of_iomap(np, 0);
26
27 if (!*base)
Rob Herring2a4849d2018-08-27 20:52:14 -050028 panic("Unable to map regs for %pOFn", np);
Dinh Nguyencfda5902012-07-11 15:13:16 -050029
Heiko Stuebnera8b447f2013-06-04 11:37:36 +020030 /*
Dinh Nguyen1f174a12018-09-17 09:52:14 -050031 * Reset the timer if the reset control is available, wiping
32 * out the state the firmware may have left it
33 */
34 rstc = of_reset_control_get(np, NULL);
35 if (!IS_ERR(rstc)) {
36 reset_control_assert(rstc);
37 reset_control_deassert(rstc);
38 }
39
40 /*
Ingo Molnar4bf07f62021-03-22 22:39:03 +010041 * Not all implementations use a peripheral clock, so don't panic
Heiko Stuebnera8b447f2013-06-04 11:37:36 +020042 * if it's not present
43 */
44 pclk = of_clk_get_by_name(np, "pclk");
45 if (!IS_ERR(pclk))
46 if (clk_prepare_enable(pclk))
Rob Herring2a4849d2018-08-27 20:52:14 -050047 pr_warn("pclk for %pOFn is present, but could not be activated\n",
48 np);
Heiko Stuebnera8b447f2013-06-04 11:37:36 +020049
Alexey Sheplyakova663bd12021-11-09 19:34:02 +040050 if (!of_property_read_u32(np, "clock-freq", rate) ||
Dinh Nguyen5d9814d2020-12-05 04:52:23 -060051 !of_property_read_u32(np, "clock-frequency", rate))
52 return 0;
53
Heiko Stuebnera8b447f2013-06-04 11:37:36 +020054 timer_clk = of_clk_get_by_name(np, "timer");
Dinh Nguyen397dc6f2021-03-22 07:18:44 -050055 if (IS_ERR(timer_clk)) {
56 ret = PTR_ERR(timer_clk);
57 goto out_pclk_disable;
58 }
Heiko Stuebnera8b447f2013-06-04 11:37:36 +020059
Dinh Nguyen5d9814d2020-12-05 04:52:23 -060060 ret = clk_prepare_enable(timer_clk);
61 if (ret)
Dinh Nguyen397dc6f2021-03-22 07:18:44 -050062 goto out_timer_clk_put;
Heiko Stuebnera8b447f2013-06-04 11:37:36 +020063
Dinh Nguyen5d9814d2020-12-05 04:52:23 -060064 *rate = clk_get_rate(timer_clk);
Dinh Nguyen397dc6f2021-03-22 07:18:44 -050065 if (!(*rate)) {
66 ret = -EINVAL;
67 goto out_timer_clk_disable;
68 }
Dinh Nguyen5d9814d2020-12-05 04:52:23 -060069
70 return 0;
Dinh Nguyen397dc6f2021-03-22 07:18:44 -050071
72out_timer_clk_disable:
73 clk_disable_unprepare(timer_clk);
74out_timer_clk_put:
75 clk_put(timer_clk);
76out_pclk_disable:
77 if (!IS_ERR(pclk)) {
78 clk_disable_unprepare(pclk);
79 clk_put(pclk);
80 }
81 iounmap(*base);
82 return ret;
Dinh Nguyencfda5902012-07-11 15:13:16 -050083}
84
Dinh Nguyen5d9814d2020-12-05 04:52:23 -060085static int __init add_clockevent(struct device_node *event_timer)
Dinh Nguyencfda5902012-07-11 15:13:16 -050086{
87 void __iomem *iobase;
88 struct dw_apb_clock_event_device *ced;
89 u32 irq, rate;
Dinh Nguyen5d9814d2020-12-05 04:52:23 -060090 int ret = 0;
Dinh Nguyencfda5902012-07-11 15:13:16 -050091
92 irq = irq_of_parse_and_map(event_timer, 0);
Baruch Siach1a33bd22013-05-29 10:11:17 +020093 if (irq == 0)
Dinh Nguyencfda5902012-07-11 15:13:16 -050094 panic("No IRQ for clock event timer");
95
Dinh Nguyen5d9814d2020-12-05 04:52:23 -060096 ret = timer_get_base_and_rate(event_timer, &iobase, &rate);
97 if (ret)
98 return ret;
Dinh Nguyencfda5902012-07-11 15:13:16 -050099
Serge Semin65e0f872020-05-21 23:48:14 +0300100 ced = dw_apb_clockevent_init(-1, event_timer->name, 300, iobase, irq,
Dinh Nguyencfda5902012-07-11 15:13:16 -0500101 rate);
102 if (!ced)
Dinh Nguyen5d9814d2020-12-05 04:52:23 -0600103 return -EINVAL;
Dinh Nguyencfda5902012-07-11 15:13:16 -0500104
105 dw_apb_clockevent_register(ced);
Dinh Nguyen5d9814d2020-12-05 04:52:23 -0600106
107 return 0;
Dinh Nguyencfda5902012-07-11 15:13:16 -0500108}
109
Heiko Stuebnera1198f82013-06-04 11:37:02 +0200110static void __iomem *sched_io_base;
111static u32 sched_rate;
112
Dinh Nguyen5d9814d2020-12-05 04:52:23 -0600113static int __init add_clocksource(struct device_node *source_timer)
Dinh Nguyencfda5902012-07-11 15:13:16 -0500114{
115 void __iomem *iobase;
116 struct dw_apb_clocksource *cs;
117 u32 rate;
Dinh Nguyen5d9814d2020-12-05 04:52:23 -0600118 int ret;
Dinh Nguyencfda5902012-07-11 15:13:16 -0500119
Dinh Nguyen5d9814d2020-12-05 04:52:23 -0600120 ret = timer_get_base_and_rate(source_timer, &iobase, &rate);
121 if (ret)
122 return ret;
Dinh Nguyencfda5902012-07-11 15:13:16 -0500123
124 cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
125 if (!cs)
Dinh Nguyen5d9814d2020-12-05 04:52:23 -0600126 return -EINVAL;
Dinh Nguyencfda5902012-07-11 15:13:16 -0500127
128 dw_apb_clocksource_start(cs);
129 dw_apb_clocksource_register(cs);
Dinh Nguyencfda5902012-07-11 15:13:16 -0500130
Heiko Stuebnera1198f82013-06-04 11:37:02 +0200131 /*
132 * Fallback to use the clocksource as sched_clock if no separate
133 * timer is found. sched_io_base then points to the current_value
134 * register of the clocksource timer.
135 */
136 sched_io_base = iobase + 0x04;
137 sched_rate = rate;
Dinh Nguyen5d9814d2020-12-05 04:52:23 -0600138
139 return 0;
Heiko Stuebnera1198f82013-06-04 11:37:02 +0200140}
Dinh Nguyencfda5902012-07-11 15:13:16 -0500141
Yang Wei0d24d1f2014-05-13 11:10:08 +0800142static u64 notrace read_sched_clock(void)
Dinh Nguyencfda5902012-07-11 15:13:16 -0500143{
Ben Dooks3a100132015-03-30 22:17:12 +0200144 return ~readl_relaxed(sched_io_base);
Dinh Nguyencfda5902012-07-11 15:13:16 -0500145}
146
147static const struct of_device_id sptimer_ids[] __initconst = {
148 { .compatible = "picochip,pc3x2-rtc" },
Dinh Nguyencfda5902012-07-11 15:13:16 -0500149 { /* Sentinel */ },
150};
151
Uwe Kleine-König1cf02032013-10-01 10:38:12 +0200152static void __init init_sched_clock(void)
Dinh Nguyencfda5902012-07-11 15:13:16 -0500153{
154 struct device_node *sched_timer;
Dinh Nguyencfda5902012-07-11 15:13:16 -0500155
156 sched_timer = of_find_matching_node(NULL, sptimer_ids);
Heiko Stuebnera1198f82013-06-04 11:37:02 +0200157 if (sched_timer) {
158 timer_get_base_and_rate(sched_timer, &sched_io_base,
159 &sched_rate);
160 of_node_put(sched_timer);
161 }
Dinh Nguyencfda5902012-07-11 15:13:16 -0500162
Stephen Boydfa8296a2013-07-18 16:21:22 -0700163 sched_clock_register(read_sched_clock, 32, sched_rate);
Dinh Nguyencfda5902012-07-11 15:13:16 -0500164}
165
Jisheng Zhang9115df82015-11-05 10:32:06 +0800166#ifdef CONFIG_ARM
167static unsigned long dw_apb_delay_timer_read(void)
168{
169 return ~readl_relaxed(sched_io_base);
170}
171
172static struct delay_timer dw_apb_delay_timer = {
173 .read_current_timer = dw_apb_delay_timer_read,
174};
175#endif
176
Heiko Stuebner10021482013-06-04 11:38:42 +0200177static int num_called;
Daniel Lezcano2e1773f2016-06-01 08:55:46 +0200178static int __init dw_apb_timer_init(struct device_node *timer)
Dinh Nguyencfda5902012-07-11 15:13:16 -0500179{
Dinh Nguyen5d9814d2020-12-05 04:52:23 -0600180 int ret = 0;
181
Heiko Stuebner10021482013-06-04 11:38:42 +0200182 switch (num_called) {
Heiko Stuebner10021482013-06-04 11:38:42 +0200183 case 1:
184 pr_debug("%s: found clocksource timer\n", __func__);
Dinh Nguyen5d9814d2020-12-05 04:52:23 -0600185 ret = add_clocksource(timer);
186 if (ret)
187 return ret;
Heiko Stuebner10021482013-06-04 11:38:42 +0200188 init_sched_clock();
Jisheng Zhang9115df82015-11-05 10:32:06 +0800189#ifdef CONFIG_ARM
190 dw_apb_delay_timer.freq = sched_rate;
191 register_current_timer_delay(&dw_apb_delay_timer);
192#endif
Heiko Stuebner10021482013-06-04 11:38:42 +0200193 break;
194 default:
Serge Semin6d2e16a2020-05-21 23:48:15 +0300195 pr_debug("%s: found clockevent timer\n", __func__);
Dinh Nguyen5d9814d2020-12-05 04:52:23 -0600196 ret = add_clockevent(timer);
197 if (ret)
198 return ret;
Heiko Stuebner10021482013-06-04 11:38:42 +0200199 break;
200 }
Dinh Nguyencfda5902012-07-11 15:13:16 -0500201
Heiko Stuebner10021482013-06-04 11:38:42 +0200202 num_called++;
Daniel Lezcano2e1773f2016-06-01 08:55:46 +0200203
204 return 0;
Dinh Nguyencfda5902012-07-11 15:13:16 -0500205}
Daniel Lezcano17273392017-05-26 16:56:11 +0200206TIMER_OF_DECLARE(pc3x2_timer, "picochip,pc3x2-timer", dw_apb_timer_init);
207TIMER_OF_DECLARE(apb_timer_osc, "snps,dw-apb-timer-osc", dw_apb_timer_init);
208TIMER_OF_DECLARE(apb_timer_sp, "snps,dw-apb-timer-sp", dw_apb_timer_init);
209TIMER_OF_DECLARE(apb_timer, "snps,dw-apb-timer", dw_apb_timer_init);