1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Clocksource using the Low Power Timer found in the Low Power Controller (LPC)
5 * Copyright (C) 2015 STMicroelectronics – All Rights Reserved
7 * Author(s): Francesco Virlinzi <francesco.virlinzi@st.com>
8 * Ajit Pal Singh <ajitpal.singh@st.com>
11 #include <linux/clk.h>
12 #include <linux/clocksource.h>
13 #include <linux/init.h>
14 #include <linux/of_address.h>
15 #include <linux/sched_clock.h>
16 #include <linux/slab.h>
18 #include <dt-bindings/mfd/st-lpc.h>
21 #define LPC_LPT_LSB_OFF 0x400
22 #define LPC_LPT_MSB_OFF 0x404
23 #define LPC_LPT_START_OFF 0x408
25 static struct st_clksrc_ddata {
30 static void __init st_clksrc_reset(void)
32 writel_relaxed(0, ddata.base + LPC_LPT_START_OFF);
33 writel_relaxed(0, ddata.base + LPC_LPT_MSB_OFF);
34 writel_relaxed(0, ddata.base + LPC_LPT_LSB_OFF);
35 writel_relaxed(1, ddata.base + LPC_LPT_START_OFF);
38 static u64 notrace st_clksrc_sched_clock_read(void)
40 return (u64)readl_relaxed(ddata.base + LPC_LPT_LSB_OFF);
43 static int __init st_clksrc_init(void)
50 rate = clk_get_rate(ddata.clk);
52 sched_clock_register(st_clksrc_sched_clock_read, 32, rate);
54 ret = clocksource_mmio_init(ddata.base + LPC_LPT_LSB_OFF,
55 "clksrc-st-lpc", rate, 300, 32,
56 clocksource_mmio_readl_up);
58 pr_err("clksrc-st-lpc: Failed to register clocksource\n");
65 static int __init st_clksrc_setup_clk(struct device_node *np)
69 clk = of_clk_get(np, 0);
71 pr_err("clksrc-st-lpc: Failed to get LPC clock\n");
75 if (clk_prepare_enable(clk)) {
76 pr_err("clksrc-st-lpc: Failed to enable LPC clock\n");
80 if (!clk_get_rate(clk)) {
81 pr_err("clksrc-st-lpc: Failed to get LPC clock rate\n");
82 clk_disable_unprepare(clk);
91 static int __init st_clksrc_of_register(struct device_node *np)
96 ret = of_property_read_u32(np, "st,lpc-mode", &mode);
98 pr_err("clksrc-st-lpc: An LPC mode must be provided\n");
102 /* LPC can either run as a Clocksource or in RTC or WDT mode */
103 if (mode != ST_LPC_MODE_CLKSRC)
106 ddata.base = of_iomap(np, 0);
108 pr_err("clksrc-st-lpc: Unable to map iomem\n");
112 ret = st_clksrc_setup_clk(np);
118 ret = st_clksrc_init();
120 clk_disable_unprepare(ddata.clk);
126 pr_info("clksrc-st-lpc: clocksource initialised - running @ %luHz\n",
127 clk_get_rate(ddata.clk));
131 TIMER_OF_DECLARE(ddata, "st,stih407-lpc", st_clksrc_of_register);