Blame view

drivers/clocksource/clksrc_st_lpc.c 2.81 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
70bef01c0   Lee Jones   clocksource: sti:...
2
3
4
5
6
7
8
  /*
   * Clocksource using the Low Power Timer found in the Low Power Controller (LPC)
   *
   * Copyright (C) 2015 STMicroelectronics – All Rights Reserved
   *
   * Author(s): Francesco Virlinzi <francesco.virlinzi@st.com>
   *	      Ajit Pal Singh <ajitpal.singh@st.com>
70bef01c0   Lee Jones   clocksource: sti:...
9
10
11
12
13
14
   */
  
  #include <linux/clk.h>
  #include <linux/clocksource.h>
  #include <linux/init.h>
  #include <linux/of_address.h>
ff45d8dd8   Lee Jones   clocksource: sti:...
15
  #include <linux/sched_clock.h>
70bef01c0   Lee Jones   clocksource: sti:...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #include <linux/slab.h>
  
  #include <dt-bindings/mfd/st-lpc.h>
  
  /* Low Power Timer */
  #define LPC_LPT_LSB_OFF		0x400
  #define LPC_LPT_MSB_OFF		0x404
  #define LPC_LPT_START_OFF	0x408
  
  static struct st_clksrc_ddata {
  	struct clk		*clk;
  	void __iomem		*base;
  } ddata;
  
  static void __init st_clksrc_reset(void)
  {
  	writel_relaxed(0, ddata.base + LPC_LPT_START_OFF);
  	writel_relaxed(0, ddata.base + LPC_LPT_MSB_OFF);
  	writel_relaxed(0, ddata.base + LPC_LPT_LSB_OFF);
  	writel_relaxed(1, ddata.base + LPC_LPT_START_OFF);
  }
ff45d8dd8   Lee Jones   clocksource: sti:...
37
38
39
40
  static u64 notrace st_clksrc_sched_clock_read(void)
  {
  	return (u64)readl_relaxed(ddata.base + LPC_LPT_LSB_OFF);
  }
70bef01c0   Lee Jones   clocksource: sti:...
41
42
43
44
45
46
47
48
  static int __init st_clksrc_init(void)
  {
  	unsigned long rate;
  	int ret;
  
  	st_clksrc_reset();
  
  	rate = clk_get_rate(ddata.clk);
ff45d8dd8   Lee Jones   clocksource: sti:...
49
  	sched_clock_register(st_clksrc_sched_clock_read, 32, rate);
70bef01c0   Lee Jones   clocksource: sti:...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  	ret = clocksource_mmio_init(ddata.base + LPC_LPT_LSB_OFF,
  				    "clksrc-st-lpc", rate, 300, 32,
  				    clocksource_mmio_readl_up);
  	if (ret) {
  		pr_err("clksrc-st-lpc: Failed to register clocksource
  ");
  		return ret;
  	}
  
  	return 0;
  }
  
  static int __init st_clksrc_setup_clk(struct device_node *np)
  {
  	struct clk *clk;
  
  	clk = of_clk_get(np, 0);
  	if (IS_ERR(clk)) {
  		pr_err("clksrc-st-lpc: Failed to get LPC clock
  ");
  		return PTR_ERR(clk);
  	}
  
  	if (clk_prepare_enable(clk)) {
  		pr_err("clksrc-st-lpc: Failed to enable LPC clock
  ");
  		return -EINVAL;
  	}
  
  	if (!clk_get_rate(clk)) {
  		pr_err("clksrc-st-lpc: Failed to get LPC clock rate
  ");
  		clk_disable_unprepare(clk);
  		return -EINVAL;
  	}
  
  	ddata.clk = clk;
  
  	return 0;
  }
84309e0ab   Daniel Lezcano   clocksource/drive...
90
  static int __init st_clksrc_of_register(struct device_node *np)
70bef01c0   Lee Jones   clocksource: sti:...
91
92
93
94
95
96
97
98
  {
  	int ret;
  	uint32_t mode;
  
  	ret = of_property_read_u32(np, "st,lpc-mode", &mode);
  	if (ret) {
  		pr_err("clksrc-st-lpc: An LPC mode must be provided
  ");
84309e0ab   Daniel Lezcano   clocksource/drive...
99
  		return ret;
70bef01c0   Lee Jones   clocksource: sti:...
100
101
102
103
  	}
  
  	/* LPC can either run as a Clocksource or in RTC or WDT mode */
  	if (mode != ST_LPC_MODE_CLKSRC)
84309e0ab   Daniel Lezcano   clocksource/drive...
104
  		return 0;
70bef01c0   Lee Jones   clocksource: sti:...
105
106
107
108
109
  
  	ddata.base = of_iomap(np, 0);
  	if (!ddata.base) {
  		pr_err("clksrc-st-lpc: Unable to map iomem
  ");
84309e0ab   Daniel Lezcano   clocksource/drive...
110
  		return -ENXIO;
70bef01c0   Lee Jones   clocksource: sti:...
111
  	}
84309e0ab   Daniel Lezcano   clocksource/drive...
112
113
  	ret = st_clksrc_setup_clk(np);
  	if (ret) {
70bef01c0   Lee Jones   clocksource: sti:...
114
  		iounmap(ddata.base);
84309e0ab   Daniel Lezcano   clocksource/drive...
115
  		return ret;
70bef01c0   Lee Jones   clocksource: sti:...
116
  	}
84309e0ab   Daniel Lezcano   clocksource/drive...
117
118
  	ret = st_clksrc_init();
  	if (ret) {
70bef01c0   Lee Jones   clocksource: sti:...
119
120
121
  		clk_disable_unprepare(ddata.clk);
  		clk_put(ddata.clk);
  		iounmap(ddata.base);
84309e0ab   Daniel Lezcano   clocksource/drive...
122
  		return ret;
70bef01c0   Lee Jones   clocksource: sti:...
123
124
125
126
127
  	}
  
  	pr_info("clksrc-st-lpc: clocksource initialised - running @ %luHz
  ",
  		clk_get_rate(ddata.clk));
84309e0ab   Daniel Lezcano   clocksource/drive...
128
129
  
  	return ret;
70bef01c0   Lee Jones   clocksource: sti:...
130
  }
172733959   Daniel Lezcano   clocksource/drive...
131
  TIMER_OF_DECLARE(ddata, "st,stih407-lpc", st_clksrc_of_register);