ARM: OMAP2+: PRM: add generic API for deasserting hardware reset
[cascardo/linux.git] / arch / arm / mach-omap2 / prm33xx.c
1 /*
2  * AM33XX PRM functions
3  *
4  * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2.
9  *
10  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11  * kind, whether express or implied; without even the implied warranty
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/io.h>
21
22 #include "powerdomain.h"
23 #include "prm33xx.h"
24 #include "prm-regbits-33xx.h"
25
26 /* Read a register in a PRM instance */
27 u32 am33xx_prm_read_reg(s16 inst, u16 idx)
28 {
29         return readl_relaxed(prm_base + inst + idx);
30 }
31
32 /* Write into a register in a PRM instance */
33 void am33xx_prm_write_reg(u32 val, s16 inst, u16 idx)
34 {
35         writel_relaxed(val, prm_base + inst + idx);
36 }
37
38 /* Read-modify-write a register in PRM. Caller must lock */
39 u32 am33xx_prm_rmw_reg_bits(u32 mask, u32 bits, s16 inst, s16 idx)
40 {
41         u32 v;
42
43         v = am33xx_prm_read_reg(inst, idx);
44         v &= ~mask;
45         v |= bits;
46         am33xx_prm_write_reg(v, inst, idx);
47
48         return v;
49 }
50
51 /**
52  * am33xx_prm_is_hardreset_asserted - read the HW reset line state of
53  * submodules contained in the hwmod module
54  * @shift: register bit shift corresponding to the reset line to check
55  * @inst: CM instance register offset (*_INST macro)
56  * @rstctrl_offs: RM_RSTCTRL register address offset for this module
57  *
58  * Returns 1 if the (sub)module hardreset line is currently asserted,
59  * 0 if the (sub)module hardreset line is not currently asserted, or
60  * -EINVAL upon parameter error.
61  */
62 int am33xx_prm_is_hardreset_asserted(u8 shift, s16 inst, u16 rstctrl_offs)
63 {
64         u32 v;
65
66         v = am33xx_prm_read_reg(inst, rstctrl_offs);
67         v &= 1 << shift;
68         v >>= shift;
69
70         return v;
71 }
72
73 /**
74  * am33xx_prm_assert_hardreset - assert the HW reset line of a submodule
75  * @shift: register bit shift corresponding to the reset line to assert
76  * @part: CM partition, ignored for AM33xx
77  * @inst: CM instance register offset (*_INST macro)
78  * @rstctrl_reg: RM_RSTCTRL register address for this module
79  *
80  * Some IPs like dsp, ipu or iva contain processors that require an HW
81  * reset line to be asserted / deasserted in order to fully enable the
82  * IP.  These modules may have multiple hard-reset lines that reset
83  * different 'submodules' inside the IP block.  This function will
84  * place the submodule into reset.  Returns 0 upon success or -EINVAL
85  * upon an argument error.
86  */
87 static int am33xx_prm_assert_hardreset(u8 shift, u8 part, s16 inst,
88                                        u16 rstctrl_offs)
89 {
90         u32 mask = 1 << shift;
91
92         am33xx_prm_rmw_reg_bits(mask, mask, inst, rstctrl_offs);
93
94         return 0;
95 }
96
97 /**
98  * am33xx_prm_deassert_hardreset - deassert a submodule hardreset line and
99  * wait
100  * @shift: register bit shift corresponding to the reset line to deassert
101  * @st_shift: reset status register bit shift corresponding to the reset line
102  * @part: PRM partition, not used for AM33xx
103  * @inst: CM instance register offset (*_INST macro)
104  * @rstctrl_reg: RM_RSTCTRL register address for this module
105  * @rstst_reg: RM_RSTST register address for this module
106  *
107  * Some IPs like dsp, ipu or iva contain processors that require an HW
108  * reset line to be asserted / deasserted in order to fully enable the
109  * IP.  These modules may have multiple hard-reset lines that reset
110  * different 'submodules' inside the IP block.  This function will
111  * take the submodule out of reset and wait until the PRCM indicates
112  * that the reset has completed before returning.  Returns 0 upon success or
113  * -EINVAL upon an argument error, -EEXIST if the submodule was already out
114  * of reset, or -EBUSY if the submodule did not exit reset promptly.
115  */
116 static int am33xx_prm_deassert_hardreset(u8 shift, u8 st_shift, u8 part,
117                                          s16 inst, u16 rstctrl_offs,
118                                          u16 rstst_offs)
119 {
120         int c;
121         u32 mask = 1 << st_shift;
122
123         /* Check the current status to avoid  de-asserting the line twice */
124         if (am33xx_prm_is_hardreset_asserted(shift, inst, rstctrl_offs) == 0)
125                 return -EEXIST;
126
127         /* Clear the reset status by writing 1 to the status bit */
128         am33xx_prm_rmw_reg_bits(0xffffffff, mask, inst, rstst_offs);
129
130         /* de-assert the reset control line */
131         mask = 1 << shift;
132
133         am33xx_prm_rmw_reg_bits(mask, 0, inst, rstctrl_offs);
134
135         /* wait the status to be set */
136         omap_test_timeout(am33xx_prm_is_hardreset_asserted(st_shift, inst,
137                                                            rstst_offs),
138                           MAX_MODULE_HARDRESET_WAIT, c);
139
140         return (c == MAX_MODULE_HARDRESET_WAIT) ? -EBUSY : 0;
141 }
142
143 static int am33xx_pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
144 {
145         am33xx_prm_rmw_reg_bits(OMAP_POWERSTATE_MASK,
146                                 (pwrst << OMAP_POWERSTATE_SHIFT),
147                                 pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
148         return 0;
149 }
150
151 static int am33xx_pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
152 {
153         u32 v;
154
155         v = am33xx_prm_read_reg(pwrdm->prcm_offs,  pwrdm->pwrstctrl_offs);
156         v &= OMAP_POWERSTATE_MASK;
157         v >>= OMAP_POWERSTATE_SHIFT;
158
159         return v;
160 }
161
162 static int am33xx_pwrdm_read_pwrst(struct powerdomain *pwrdm)
163 {
164         u32 v;
165
166         v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstst_offs);
167         v &= OMAP_POWERSTATEST_MASK;
168         v >>= OMAP_POWERSTATEST_SHIFT;
169
170         return v;
171 }
172
173 static int am33xx_pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
174 {
175         u32 v;
176
177         v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstst_offs);
178         v &= AM33XX_LASTPOWERSTATEENTERED_MASK;
179         v >>= AM33XX_LASTPOWERSTATEENTERED_SHIFT;
180
181         return v;
182 }
183
184 static int am33xx_pwrdm_set_lowpwrstchange(struct powerdomain *pwrdm)
185 {
186         am33xx_prm_rmw_reg_bits(AM33XX_LOWPOWERSTATECHANGE_MASK,
187                                 (1 << AM33XX_LOWPOWERSTATECHANGE_SHIFT),
188                                 pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
189         return 0;
190 }
191
192 static int am33xx_pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
193 {
194         am33xx_prm_rmw_reg_bits(AM33XX_LASTPOWERSTATEENTERED_MASK,
195                                 AM33XX_LASTPOWERSTATEENTERED_MASK,
196                                 pwrdm->prcm_offs, pwrdm->pwrstst_offs);
197         return 0;
198 }
199
200 static int am33xx_pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
201 {
202         u32 m;
203
204         m = pwrdm->logicretstate_mask;
205         if (!m)
206                 return -EINVAL;
207
208         am33xx_prm_rmw_reg_bits(m, (pwrst << __ffs(m)),
209                                 pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
210
211         return 0;
212 }
213
214 static int am33xx_pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
215 {
216         u32 v;
217
218         v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstst_offs);
219         v &= AM33XX_LOGICSTATEST_MASK;
220         v >>= AM33XX_LOGICSTATEST_SHIFT;
221
222         return v;
223 }
224
225 static int am33xx_pwrdm_read_logic_retst(struct powerdomain *pwrdm)
226 {
227         u32 v, m;
228
229         m = pwrdm->logicretstate_mask;
230         if (!m)
231                 return -EINVAL;
232
233         v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
234         v &= m;
235         v >>= __ffs(m);
236
237         return v;
238 }
239
240 static int am33xx_pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank,
241                 u8 pwrst)
242 {
243         u32 m;
244
245         m = pwrdm->mem_on_mask[bank];
246         if (!m)
247                 return -EINVAL;
248
249         am33xx_prm_rmw_reg_bits(m, (pwrst << __ffs(m)),
250                                 pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
251
252         return 0;
253 }
254
255 static int am33xx_pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank,
256                                         u8 pwrst)
257 {
258         u32 m;
259
260         m = pwrdm->mem_ret_mask[bank];
261         if (!m)
262                 return -EINVAL;
263
264         am33xx_prm_rmw_reg_bits(m, (pwrst << __ffs(m)),
265                                 pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
266
267         return 0;
268 }
269
270 static int am33xx_pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
271 {
272         u32 m, v;
273
274         m = pwrdm->mem_pwrst_mask[bank];
275         if (!m)
276                 return -EINVAL;
277
278         v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstst_offs);
279         v &= m;
280         v >>= __ffs(m);
281
282         return v;
283 }
284
285 static int am33xx_pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
286 {
287         u32 m, v;
288
289         m = pwrdm->mem_retst_mask[bank];
290         if (!m)
291                 return -EINVAL;
292
293         v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
294         v &= m;
295         v >>= __ffs(m);
296
297         return v;
298 }
299
300 static int am33xx_pwrdm_wait_transition(struct powerdomain *pwrdm)
301 {
302         u32 c = 0;
303
304         /*
305          * REVISIT: pwrdm_wait_transition() may be better implemented
306          * via a callback and a periodic timer check -- how long do we expect
307          * powerdomain transitions to take?
308          */
309
310         /* XXX Is this udelay() value meaningful? */
311         while ((am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstst_offs)
312                         & OMAP_INTRANSITION_MASK) &&
313                         (c++ < PWRDM_TRANSITION_BAILOUT))
314                 udelay(1);
315
316         if (c > PWRDM_TRANSITION_BAILOUT) {
317                 pr_err("powerdomain: %s: waited too long to complete transition\n",
318                        pwrdm->name);
319                 return -EAGAIN;
320         }
321
322         pr_debug("powerdomain: completed transition in %d loops\n", c);
323
324         return 0;
325 }
326
327 static int am33xx_check_vcvp(void)
328 {
329         /* No VC/VP on am33xx devices */
330         return 0;
331 }
332
333 struct pwrdm_ops am33xx_pwrdm_operations = {
334         .pwrdm_set_next_pwrst           = am33xx_pwrdm_set_next_pwrst,
335         .pwrdm_read_next_pwrst          = am33xx_pwrdm_read_next_pwrst,
336         .pwrdm_read_pwrst               = am33xx_pwrdm_read_pwrst,
337         .pwrdm_read_prev_pwrst          = am33xx_pwrdm_read_prev_pwrst,
338         .pwrdm_set_logic_retst          = am33xx_pwrdm_set_logic_retst,
339         .pwrdm_read_logic_pwrst         = am33xx_pwrdm_read_logic_pwrst,
340         .pwrdm_read_logic_retst         = am33xx_pwrdm_read_logic_retst,
341         .pwrdm_clear_all_prev_pwrst     = am33xx_pwrdm_clear_all_prev_pwrst,
342         .pwrdm_set_lowpwrstchange       = am33xx_pwrdm_set_lowpwrstchange,
343         .pwrdm_read_mem_pwrst           = am33xx_pwrdm_read_mem_pwrst,
344         .pwrdm_read_mem_retst           = am33xx_pwrdm_read_mem_retst,
345         .pwrdm_set_mem_onst             = am33xx_pwrdm_set_mem_onst,
346         .pwrdm_set_mem_retst            = am33xx_pwrdm_set_mem_retst,
347         .pwrdm_wait_transition          = am33xx_pwrdm_wait_transition,
348         .pwrdm_has_voltdm               = am33xx_check_vcvp,
349 };
350
351 static struct prm_ll_data am33xx_prm_ll_data = {
352         .assert_hardreset               = am33xx_prm_assert_hardreset,
353         .deassert_hardreset             = am33xx_prm_deassert_hardreset,
354 };
355
356 int __init am33xx_prm_init(void)
357 {
358         return prm_register(&am33xx_prm_ll_data);
359 }
360
361 static void __exit am33xx_prm_exit(void)
362 {
363         prm_unregister(&am33xx_prm_ll_data);
364 }
365 __exitcall(am33xx_prm_exit);