]> Pileus Git - ~andy/linux/blob - arch/arm/mach-omap2/prm_common.c
Merge tag 'headers' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[~andy/linux] / arch / arm / mach-omap2 / prm_common.c
1 /*
2  * OMAP2+ common Power & Reset Management (PRM) IP block functions
3  *
4  * Copyright (C) 2011 Texas Instruments, Inc.
5  * Tero Kristo <t-kristo@ti.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  *
12  * For historical purposes, the API used to configure the PRM
13  * interrupt handler refers to it as the "PRCM interrupt."  The
14  * underlying registers are located in the PRM on OMAP3/4.
15  *
16  * XXX This code should eventually be moved to a PRM driver.
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/io.h>
23 #include <linux/irq.h>
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
26
27 #include <plat/prcm.h>
28
29 #include "prm2xxx_3xxx.h"
30 #include "prm2xxx.h"
31 #include "prm3xxx.h"
32 #include "prm44xx.h"
33
34 /*
35  * OMAP_PRCM_MAX_NR_PENDING_REG: maximum number of PRM_IRQ*_MPU regs
36  * XXX this is technically not needed, since
37  * omap_prcm_register_chain_handler() could allocate this based on the
38  * actual amount of memory needed for the SoC
39  */
40 #define OMAP_PRCM_MAX_NR_PENDING_REG            2
41
42 /*
43  * prcm_irq_chips: an array of all of the "generic IRQ chips" in use
44  * by the PRCM interrupt handler code.  There will be one 'chip' per
45  * PRM_{IRQSTATUS,IRQENABLE}_MPU register pair.  (So OMAP3 will have
46  * one "chip" and OMAP4 will have two.)
47  */
48 static struct irq_chip_generic **prcm_irq_chips;
49
50 /*
51  * prcm_irq_setup: the PRCM IRQ parameters for the hardware the code
52  * is currently running on.  Defined and passed by initialization code
53  * that calls omap_prcm_register_chain_handler().
54  */
55 static struct omap_prcm_irq_setup *prcm_irq_setup;
56
57 /*
58  * prm_ll_data: function pointers to SoC-specific implementations of
59  * common PRM functions
60  */
61 static struct prm_ll_data null_prm_ll_data;
62 static struct prm_ll_data *prm_ll_data = &null_prm_ll_data;
63
64 /* Private functions */
65
66 /*
67  * Move priority events from events to priority_events array
68  */
69 static void omap_prcm_events_filter_priority(unsigned long *events,
70         unsigned long *priority_events)
71 {
72         int i;
73
74         for (i = 0; i < prcm_irq_setup->nr_regs; i++) {
75                 priority_events[i] =
76                         events[i] & prcm_irq_setup->priority_mask[i];
77                 events[i] ^= priority_events[i];
78         }
79 }
80
81 /*
82  * PRCM Interrupt Handler
83  *
84  * This is a common handler for the OMAP PRCM interrupts. Pending
85  * interrupts are detected by a call to prcm_pending_events and
86  * dispatched accordingly. Clearing of the wakeup events should be
87  * done by the SoC specific individual handlers.
88  */
89 static void omap_prcm_irq_handler(unsigned int irq, struct irq_desc *desc)
90 {
91         unsigned long pending[OMAP_PRCM_MAX_NR_PENDING_REG];
92         unsigned long priority_pending[OMAP_PRCM_MAX_NR_PENDING_REG];
93         struct irq_chip *chip = irq_desc_get_chip(desc);
94         unsigned int virtirq;
95         int nr_irq = prcm_irq_setup->nr_regs * 32;
96
97         /*
98          * If we are suspended, mask all interrupts from PRCM level,
99          * this does not ack them, and they will be pending until we
100          * re-enable the interrupts, at which point the
101          * omap_prcm_irq_handler will be executed again.  The
102          * _save_and_clear_irqen() function must ensure that the PRM
103          * write to disable all IRQs has reached the PRM before
104          * returning, or spurious PRCM interrupts may occur during
105          * suspend.
106          */
107         if (prcm_irq_setup->suspended) {
108                 prcm_irq_setup->save_and_clear_irqen(prcm_irq_setup->saved_mask);
109                 prcm_irq_setup->suspend_save_flag = true;
110         }
111
112         /*
113          * Loop until all pending irqs are handled, since
114          * generic_handle_irq() can cause new irqs to come
115          */
116         while (!prcm_irq_setup->suspended) {
117                 prcm_irq_setup->read_pending_irqs(pending);
118
119                 /* No bit set, then all IRQs are handled */
120                 if (find_first_bit(pending, nr_irq) >= nr_irq)
121                         break;
122
123                 omap_prcm_events_filter_priority(pending, priority_pending);
124
125                 /*
126                  * Loop on all currently pending irqs so that new irqs
127                  * cannot starve previously pending irqs
128                  */
129
130                 /* Serve priority events first */
131                 for_each_set_bit(virtirq, priority_pending, nr_irq)
132                         generic_handle_irq(prcm_irq_setup->base_irq + virtirq);
133
134                 /* Serve normal events next */
135                 for_each_set_bit(virtirq, pending, nr_irq)
136                         generic_handle_irq(prcm_irq_setup->base_irq + virtirq);
137         }
138         if (chip->irq_ack)
139                 chip->irq_ack(&desc->irq_data);
140         if (chip->irq_eoi)
141                 chip->irq_eoi(&desc->irq_data);
142         chip->irq_unmask(&desc->irq_data);
143
144         prcm_irq_setup->ocp_barrier(); /* avoid spurious IRQs */
145 }
146
147 /* Public functions */
148
149 /**
150  * omap_prcm_event_to_irq - given a PRCM event name, returns the
151  * corresponding IRQ on which the handler should be registered
152  * @name: name of the PRCM interrupt bit to look up - see struct omap_prcm_irq
153  *
154  * Returns the Linux internal IRQ ID corresponding to @name upon success,
155  * or -ENOENT upon failure.
156  */
157 int omap_prcm_event_to_irq(const char *name)
158 {
159         int i;
160
161         if (!prcm_irq_setup || !name)
162                 return -ENOENT;
163
164         for (i = 0; i < prcm_irq_setup->nr_irqs; i++)
165                 if (!strcmp(prcm_irq_setup->irqs[i].name, name))
166                         return prcm_irq_setup->base_irq +
167                                 prcm_irq_setup->irqs[i].offset;
168
169         return -ENOENT;
170 }
171
172 /**
173  * omap_prcm_irq_cleanup - reverses memory allocated and other steps
174  * done by omap_prcm_register_chain_handler()
175  *
176  * No return value.
177  */
178 void omap_prcm_irq_cleanup(void)
179 {
180         int i;
181
182         if (!prcm_irq_setup) {
183                 pr_err("PRCM: IRQ handler not initialized; cannot cleanup\n");
184                 return;
185         }
186
187         if (prcm_irq_chips) {
188                 for (i = 0; i < prcm_irq_setup->nr_regs; i++) {
189                         if (prcm_irq_chips[i])
190                                 irq_remove_generic_chip(prcm_irq_chips[i],
191                                         0xffffffff, 0, 0);
192                         prcm_irq_chips[i] = NULL;
193                 }
194                 kfree(prcm_irq_chips);
195                 prcm_irq_chips = NULL;
196         }
197
198         kfree(prcm_irq_setup->saved_mask);
199         prcm_irq_setup->saved_mask = NULL;
200
201         kfree(prcm_irq_setup->priority_mask);
202         prcm_irq_setup->priority_mask = NULL;
203
204         irq_set_chained_handler(prcm_irq_setup->irq, NULL);
205
206         if (prcm_irq_setup->base_irq > 0)
207                 irq_free_descs(prcm_irq_setup->base_irq,
208                         prcm_irq_setup->nr_regs * 32);
209         prcm_irq_setup->base_irq = 0;
210 }
211
212 void omap_prcm_irq_prepare(void)
213 {
214         prcm_irq_setup->suspended = true;
215 }
216
217 void omap_prcm_irq_complete(void)
218 {
219         prcm_irq_setup->suspended = false;
220
221         /* If we have not saved the masks, do not attempt to restore */
222         if (!prcm_irq_setup->suspend_save_flag)
223                 return;
224
225         prcm_irq_setup->suspend_save_flag = false;
226
227         /*
228          * Re-enable all masked PRCM irq sources, this causes the PRCM
229          * interrupt to fire immediately if the events were masked
230          * previously in the chain handler
231          */
232         prcm_irq_setup->restore_irqen(prcm_irq_setup->saved_mask);
233 }
234
235 /**
236  * omap_prcm_register_chain_handler - initializes the prcm chained interrupt
237  * handler based on provided parameters
238  * @irq_setup: hardware data about the underlying PRM/PRCM
239  *
240  * Set up the PRCM chained interrupt handler on the PRCM IRQ.  Sets up
241  * one generic IRQ chip per PRM interrupt status/enable register pair.
242  * Returns 0 upon success, -EINVAL if called twice or if invalid
243  * arguments are passed, or -ENOMEM on any other error.
244  */
245 int omap_prcm_register_chain_handler(struct omap_prcm_irq_setup *irq_setup)
246 {
247         int nr_regs;
248         u32 mask[OMAP_PRCM_MAX_NR_PENDING_REG];
249         int offset, i;
250         struct irq_chip_generic *gc;
251         struct irq_chip_type *ct;
252
253         if (!irq_setup)
254                 return -EINVAL;
255
256         nr_regs = irq_setup->nr_regs;
257
258         if (prcm_irq_setup) {
259                 pr_err("PRCM: already initialized; won't reinitialize\n");
260                 return -EINVAL;
261         }
262
263         if (nr_regs > OMAP_PRCM_MAX_NR_PENDING_REG) {
264                 pr_err("PRCM: nr_regs too large\n");
265                 return -EINVAL;
266         }
267
268         prcm_irq_setup = irq_setup;
269
270         prcm_irq_chips = kzalloc(sizeof(void *) * nr_regs, GFP_KERNEL);
271         prcm_irq_setup->saved_mask = kzalloc(sizeof(u32) * nr_regs, GFP_KERNEL);
272         prcm_irq_setup->priority_mask = kzalloc(sizeof(u32) * nr_regs,
273                 GFP_KERNEL);
274
275         if (!prcm_irq_chips || !prcm_irq_setup->saved_mask ||
276             !prcm_irq_setup->priority_mask) {
277                 pr_err("PRCM: kzalloc failed\n");
278                 goto err;
279         }
280
281         memset(mask, 0, sizeof(mask));
282
283         for (i = 0; i < irq_setup->nr_irqs; i++) {
284                 offset = irq_setup->irqs[i].offset;
285                 mask[offset >> 5] |= 1 << (offset & 0x1f);
286                 if (irq_setup->irqs[i].priority)
287                         irq_setup->priority_mask[offset >> 5] |=
288                                 1 << (offset & 0x1f);
289         }
290
291         irq_set_chained_handler(irq_setup->irq, omap_prcm_irq_handler);
292
293         irq_setup->base_irq = irq_alloc_descs(-1, 0, irq_setup->nr_regs * 32,
294                 0);
295
296         if (irq_setup->base_irq < 0) {
297                 pr_err("PRCM: failed to allocate irq descs: %d\n",
298                         irq_setup->base_irq);
299                 goto err;
300         }
301
302         for (i = 0; i < irq_setup->nr_regs; i++) {
303                 gc = irq_alloc_generic_chip("PRCM", 1,
304                         irq_setup->base_irq + i * 32, prm_base,
305                         handle_level_irq);
306
307                 if (!gc) {
308                         pr_err("PRCM: failed to allocate generic chip\n");
309                         goto err;
310                 }
311                 ct = gc->chip_types;
312                 ct->chip.irq_ack = irq_gc_ack_set_bit;
313                 ct->chip.irq_mask = irq_gc_mask_clr_bit;
314                 ct->chip.irq_unmask = irq_gc_mask_set_bit;
315
316                 ct->regs.ack = irq_setup->ack + i * 4;
317                 ct->regs.mask = irq_setup->mask + i * 4;
318
319                 irq_setup_generic_chip(gc, mask[i], 0, IRQ_NOREQUEST, 0);
320                 prcm_irq_chips[i] = gc;
321         }
322
323         return 0;
324
325 err:
326         omap_prcm_irq_cleanup();
327         return -ENOMEM;
328 }
329
330 /**
331  * prm_read_reset_sources - return the sources of the SoC's last reset
332  *
333  * Return a u32 bitmask representing the reset sources that caused the
334  * SoC to reset.  The low-level per-SoC functions called by this
335  * function remap the SoC-specific reset source bits into an
336  * OMAP-common set of reset source bits, defined in
337  * arch/arm/mach-omap2/prm.h.  Returns the standardized reset source
338  * u32 bitmask from the hardware upon success, or returns (1 <<
339  * OMAP_UNKNOWN_RST_SRC_ID_SHIFT) if no low-level read_reset_sources()
340  * function was registered.
341  */
342 u32 prm_read_reset_sources(void)
343 {
344         u32 ret = 1 << OMAP_UNKNOWN_RST_SRC_ID_SHIFT;
345
346         if (prm_ll_data->read_reset_sources)
347                 ret = prm_ll_data->read_reset_sources();
348         else
349                 WARN_ONCE(1, "prm: %s: no mapping function defined for reset sources\n", __func__);
350
351         return ret;
352 }
353
354 /**
355  * prm_register - register per-SoC low-level data with the PRM
356  * @pld: low-level per-SoC OMAP PRM data & function pointers to register
357  *
358  * Register per-SoC low-level OMAP PRM data and function pointers with
359  * the OMAP PRM common interface.  The caller must keep the data
360  * pointed to by @pld valid until it calls prm_unregister() and
361  * it returns successfully.  Returns 0 upon success, -EINVAL if @pld
362  * is NULL, or -EEXIST if prm_register() has already been called
363  * without an intervening prm_unregister().
364  */
365 int prm_register(struct prm_ll_data *pld)
366 {
367         if (!pld)
368                 return -EINVAL;
369
370         if (prm_ll_data != &null_prm_ll_data)
371                 return -EEXIST;
372
373         prm_ll_data = pld;
374
375         return 0;
376 }
377
378 /**
379  * prm_unregister - unregister per-SoC low-level data & function pointers
380  * @pld: low-level per-SoC OMAP PRM data & function pointers to unregister
381  *
382  * Unregister per-SoC low-level OMAP PRM data and function pointers
383  * that were previously registered with prm_register().  The
384  * caller may not destroy any of the data pointed to by @pld until
385  * this function returns successfully.  Returns 0 upon success, or
386  * -EINVAL if @pld is NULL or if @pld does not match the struct
387  * prm_ll_data * previously registered by prm_register().
388  */
389 int prm_unregister(struct prm_ll_data *pld)
390 {
391         if (!pld || prm_ll_data != pld)
392                 return -EINVAL;
393
394         prm_ll_data = &null_prm_ll_data;
395
396         return 0;
397 }