blob: 2b55410edaf65dbcb3739ac9305674b3441239ec [file] [log] [blame]
Maxime Coquelin4958ebb2015-05-09 09:53:46 +02001/*
2 * Copyright (C) Maxime Coquelin 2015
3 * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
4 * License terms: GNU General Public License (GPL), version 2
5 */
6
7#include <linux/kernel.h>
8#include <linux/clocksource.h>
9#include <linux/clockchips.h>
10#include <linux/of.h>
11#include <linux/of_address.h>
12#include <linux/clk.h>
13#include <linux/bitops.h>
14
15#define SYST_CSR 0x00
16#define SYST_RVR 0x04
17#define SYST_CVR 0x08
18#define SYST_CALIB 0x0c
19
20#define SYST_CSR_ENABLE BIT(0)
21
22#define SYSTICK_LOAD_RELOAD_MASK 0x00FFFFFF
23
Daniel Lezcano802fa492016-06-02 18:43:42 +020024static int __init system_timer_of_register(struct device_node *np)
Maxime Coquelin4958ebb2015-05-09 09:53:46 +020025{
26 struct clk *clk = NULL;
27 void __iomem *base;
28 u32 rate;
29 int ret;
30
31 base = of_iomap(np, 0);
32 if (!base) {
33 pr_warn("system-timer: invalid base address\n");
Daniel Lezcano802fa492016-06-02 18:43:42 +020034 return -ENXIO;
Maxime Coquelin4958ebb2015-05-09 09:53:46 +020035 }
36
37 ret = of_property_read_u32(np, "clock-frequency", &rate);
38 if (ret) {
39 clk = of_clk_get(np, 0);
Daniel Lezcano802fa492016-06-02 18:43:42 +020040 if (IS_ERR(clk)) {
41 ret = PTR_ERR(clk);
Maxime Coquelin4958ebb2015-05-09 09:53:46 +020042 goto out_unmap;
Daniel Lezcano802fa492016-06-02 18:43:42 +020043 }
Maxime Coquelin4958ebb2015-05-09 09:53:46 +020044
45 ret = clk_prepare_enable(clk);
46 if (ret)
47 goto out_clk_put;
48
49 rate = clk_get_rate(clk);
Daniel Lezcano802fa492016-06-02 18:43:42 +020050 if (!rate) {
51 ret = -EINVAL;
Maxime Coquelin4958ebb2015-05-09 09:53:46 +020052 goto out_clk_disable;
Daniel Lezcano802fa492016-06-02 18:43:42 +020053 }
Maxime Coquelin4958ebb2015-05-09 09:53:46 +020054 }
55
56 writel_relaxed(SYSTICK_LOAD_RELOAD_MASK, base + SYST_RVR);
57 writel_relaxed(SYST_CSR_ENABLE, base + SYST_CSR);
58
59 ret = clocksource_mmio_init(base + SYST_CVR, "arm_system_timer", rate,
60 200, 24, clocksource_mmio_readl_down);
61 if (ret) {
62 pr_err("failed to init clocksource (%d)\n", ret);
63 if (clk)
64 goto out_clk_disable;
65 else
66 goto out_unmap;
67 }
68
69 pr_info("ARM System timer initialized as clocksource\n");
70
Daniel Lezcano802fa492016-06-02 18:43:42 +020071 return 0;
Maxime Coquelin4958ebb2015-05-09 09:53:46 +020072
73out_clk_disable:
74 clk_disable_unprepare(clk);
75out_clk_put:
76 clk_put(clk);
77out_unmap:
78 iounmap(base);
79 pr_warn("ARM System timer register failed (%d)\n", ret);
Daniel Lezcano802fa492016-06-02 18:43:42 +020080
81 return ret;
Maxime Coquelin4958ebb2015-05-09 09:53:46 +020082}
83
Daniel Lezcano802fa492016-06-02 18:43:42 +020084CLOCKSOURCE_OF_DECLARE_RET(arm_systick, "arm,armv7m-systick",
Maxime Coquelin4958ebb2015-05-09 09:53:46 +020085 system_timer_of_register);