Merge tag 'omap-for-v3.8/dt-signed' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-omap2 / cm33xx.c
1 /*
2  * AM33XX CM functions
3  *
4  * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
5  * Vaibhav Hiremath <hvaibhav@ti.com>
6  *
7  * Reference taken from from OMAP4 cminst44xx.c
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation version 2.
12  *
13  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
14  * kind, whether express or implied; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/io.h>
24
25 #include "cm.h"
26 #include "cm33xx.h"
27 #include "cm-regbits-34xx.h"
28 #include "cm-regbits-33xx.h"
29 #include "prm33xx.h"
30
31 /*
32  * CLKCTRL_IDLEST_*: possible values for the CM_*_CLKCTRL.IDLEST bitfield:
33  *
34  *   0x0 func:     Module is fully functional, including OCP
35  *   0x1 trans:    Module is performing transition: wakeup, or sleep, or sleep
36  *                 abortion
37  *   0x2 idle:     Module is in Idle mode (only OCP part). It is functional if
38  *                 using separate functional clock
39  *   0x3 disabled: Module is disabled and cannot be accessed
40  *
41  */
42 #define CLKCTRL_IDLEST_FUNCTIONAL               0x0
43 #define CLKCTRL_IDLEST_INTRANSITION             0x1
44 #define CLKCTRL_IDLEST_INTERFACE_IDLE           0x2
45 #define CLKCTRL_IDLEST_DISABLED                 0x3
46
47 /* Private functions */
48
49 /* Read a register in a CM instance */
50 static inline u32 am33xx_cm_read_reg(s16 inst, u16 idx)
51 {
52         return __raw_readl(cm_base + inst + idx);
53 }
54
55 /* Write into a register in a CM */
56 static inline void am33xx_cm_write_reg(u32 val, s16 inst, u16 idx)
57 {
58         __raw_writel(val, cm_base + inst + idx);
59 }
60
61 /* Read-modify-write a register in CM */
62 static inline u32 am33xx_cm_rmw_reg_bits(u32 mask, u32 bits, s16 inst, s16 idx)
63 {
64         u32 v;
65
66         v = am33xx_cm_read_reg(inst, idx);
67         v &= ~mask;
68         v |= bits;
69         am33xx_cm_write_reg(v, inst, idx);
70
71         return v;
72 }
73
74 static inline u32 am33xx_cm_set_reg_bits(u32 bits, s16 inst, s16 idx)
75 {
76         return am33xx_cm_rmw_reg_bits(bits, bits, inst, idx);
77 }
78
79 static inline u32 am33xx_cm_clear_reg_bits(u32 bits, s16 inst, s16 idx)
80 {
81         return am33xx_cm_rmw_reg_bits(bits, 0x0, inst, idx);
82 }
83
84 static inline u32 am33xx_cm_read_reg_bits(u16 inst, s16 idx, u32 mask)
85 {
86         u32 v;
87
88         v = am33xx_cm_read_reg(inst, idx);
89         v &= mask;
90         v >>= __ffs(mask);
91
92         return v;
93 }
94
95 /**
96  * _clkctrl_idlest - read a CM_*_CLKCTRL register; mask & shift IDLEST bitfield
97  * @inst: CM instance register offset (*_INST macro)
98  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
99  * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
100  *
101  * Return the IDLEST bitfield of a CM_*_CLKCTRL register, shifted down to
102  * bit 0.
103  */
104 static u32 _clkctrl_idlest(u16 inst, s16 cdoffs, u16 clkctrl_offs)
105 {
106         u32 v = am33xx_cm_read_reg(inst, clkctrl_offs);
107         v &= AM33XX_IDLEST_MASK;
108         v >>= AM33XX_IDLEST_SHIFT;
109         return v;
110 }
111
112 /**
113  * _is_module_ready - can module registers be accessed without causing an abort?
114  * @inst: CM instance register offset (*_INST macro)
115  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
116  * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
117  *
118  * Returns true if the module's CM_*_CLKCTRL.IDLEST bitfield is either
119  * *FUNCTIONAL or *INTERFACE_IDLE; false otherwise.
120  */
121 static bool _is_module_ready(u16 inst, s16 cdoffs, u16 clkctrl_offs)
122 {
123         u32 v;
124
125         v = _clkctrl_idlest(inst, cdoffs, clkctrl_offs);
126
127         return (v == CLKCTRL_IDLEST_FUNCTIONAL ||
128                 v == CLKCTRL_IDLEST_INTERFACE_IDLE) ? true : false;
129 }
130
131 /**
132  * _clktrctrl_write - write @c to a CM_CLKSTCTRL.CLKTRCTRL register bitfield
133  * @c: CLKTRCTRL register bitfield (LSB = bit 0, i.e., unshifted)
134  * @inst: CM instance register offset (*_INST macro)
135  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
136  *
137  * @c must be the unshifted value for CLKTRCTRL - i.e., this function
138  * will handle the shift itself.
139  */
140 static void _clktrctrl_write(u8 c, s16 inst, u16 cdoffs)
141 {
142         u32 v;
143
144         v = am33xx_cm_read_reg(inst, cdoffs);
145         v &= ~AM33XX_CLKTRCTRL_MASK;
146         v |= c << AM33XX_CLKTRCTRL_SHIFT;
147         am33xx_cm_write_reg(v, inst, cdoffs);
148 }
149
150 /* Public functions */
151
152 /**
153  * am33xx_cm_is_clkdm_in_hwsup - is a clockdomain in hwsup idle mode?
154  * @inst: CM instance register offset (*_INST macro)
155  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
156  *
157  * Returns true if the clockdomain referred to by (@inst, @cdoffs)
158  * is in hardware-supervised idle mode, or 0 otherwise.
159  */
160 bool am33xx_cm_is_clkdm_in_hwsup(s16 inst, u16 cdoffs)
161 {
162         u32 v;
163
164         v = am33xx_cm_read_reg(inst, cdoffs);
165         v &= AM33XX_CLKTRCTRL_MASK;
166         v >>= AM33XX_CLKTRCTRL_SHIFT;
167
168         return (v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ? true : false;
169 }
170
171 /**
172  * am33xx_cm_clkdm_enable_hwsup - put a clockdomain in hwsup-idle mode
173  * @inst: CM instance register offset (*_INST macro)
174  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
175  *
176  * Put a clockdomain referred to by (@inst, @cdoffs) into
177  * hardware-supervised idle mode.  No return value.
178  */
179 void am33xx_cm_clkdm_enable_hwsup(s16 inst, u16 cdoffs)
180 {
181         _clktrctrl_write(OMAP34XX_CLKSTCTRL_ENABLE_AUTO, inst, cdoffs);
182 }
183
184 /**
185  * am33xx_cm_clkdm_disable_hwsup - put a clockdomain in swsup-idle mode
186  * @inst: CM instance register offset (*_INST macro)
187  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
188  *
189  * Put a clockdomain referred to by (@inst, @cdoffs) into
190  * software-supervised idle mode, i.e., controlled manually by the
191  * Linux OMAP clockdomain code.  No return value.
192  */
193 void am33xx_cm_clkdm_disable_hwsup(s16 inst, u16 cdoffs)
194 {
195         _clktrctrl_write(OMAP34XX_CLKSTCTRL_DISABLE_AUTO, inst, cdoffs);
196 }
197
198 /**
199  * am33xx_cm_clkdm_force_sleep - try to put a clockdomain into idle
200  * @inst: CM instance register offset (*_INST macro)
201  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
202  *
203  * Put a clockdomain referred to by (@inst, @cdoffs) into idle
204  * No return value.
205  */
206 void am33xx_cm_clkdm_force_sleep(s16 inst, u16 cdoffs)
207 {
208         _clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_SLEEP, inst, cdoffs);
209 }
210
211 /**
212  * am33xx_cm_clkdm_force_wakeup - try to take a clockdomain out of idle
213  * @inst: CM instance register offset (*_INST macro)
214  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
215  *
216  * Take a clockdomain referred to by (@inst, @cdoffs) out of idle,
217  * waking it up.  No return value.
218  */
219 void am33xx_cm_clkdm_force_wakeup(s16 inst, u16 cdoffs)
220 {
221         _clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_WAKEUP, inst, cdoffs);
222 }
223
224 /*
225  *
226  */
227
228 /**
229  * am33xx_cm_wait_module_ready - wait for a module to be in 'func' state
230  * @inst: CM instance register offset (*_INST macro)
231  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
232  * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
233  *
234  * Wait for the module IDLEST to be functional. If the idle state is in any
235  * the non functional state (trans, idle or disabled), module and thus the
236  * sysconfig cannot be accessed and will probably lead to an "imprecise
237  * external abort"
238  */
239 int am33xx_cm_wait_module_ready(u16 inst, s16 cdoffs, u16 clkctrl_offs)
240 {
241         int i = 0;
242
243         if (!clkctrl_offs)
244                 return 0;
245
246         omap_test_timeout(_is_module_ready(inst, cdoffs, clkctrl_offs),
247                           MAX_MODULE_READY_TIME, i);
248
249         return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;
250 }
251
252 /**
253  * am33xx_cm_wait_module_idle - wait for a module to be in 'disabled'
254  * state
255  * @inst: CM instance register offset (*_INST macro)
256  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
257  * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
258  *
259  * Wait for the module IDLEST to be disabled. Some PRCM transition,
260  * like reset assertion or parent clock de-activation must wait the
261  * module to be fully disabled.
262  */
263 int am33xx_cm_wait_module_idle(u16 inst, s16 cdoffs, u16 clkctrl_offs)
264 {
265         int i = 0;
266
267         if (!clkctrl_offs)
268                 return 0;
269
270         omap_test_timeout((_clkctrl_idlest(inst, cdoffs, clkctrl_offs) ==
271                                 CLKCTRL_IDLEST_DISABLED),
272                                 MAX_MODULE_READY_TIME, i);
273
274         return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;
275 }
276
277 /**
278  * am33xx_cm_module_enable - Enable the modulemode inside CLKCTRL
279  * @mode: Module mode (SW or HW)
280  * @inst: CM instance register offset (*_INST macro)
281  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
282  * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
283  *
284  * No return value.
285  */
286 void am33xx_cm_module_enable(u8 mode, u16 inst, s16 cdoffs, u16 clkctrl_offs)
287 {
288         u32 v;
289
290         v = am33xx_cm_read_reg(inst, clkctrl_offs);
291         v &= ~AM33XX_MODULEMODE_MASK;
292         v |= mode << AM33XX_MODULEMODE_SHIFT;
293         am33xx_cm_write_reg(v, inst, clkctrl_offs);
294 }
295
296 /**
297  * am33xx_cm_module_disable - Disable the module inside CLKCTRL
298  * @inst: CM instance register offset (*_INST macro)
299  * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
300  * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
301  *
302  * No return value.
303  */
304 void am33xx_cm_module_disable(u16 inst, s16 cdoffs, u16 clkctrl_offs)
305 {
306         u32 v;
307
308         v = am33xx_cm_read_reg(inst, clkctrl_offs);
309         v &= ~AM33XX_MODULEMODE_MASK;
310         am33xx_cm_write_reg(v, inst, clkctrl_offs);
311 }