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