xref: /linux/arch/arm/mach-omap2/prm_common.c (revision 4ba7c3c3c6567210bf46b1ab3d089134170c2762)
10a84a91cSTero Kristo /*
20a84a91cSTero Kristo  * OMAP2+ common Power & Reset Management (PRM) IP block functions
30a84a91cSTero Kristo  *
40a84a91cSTero Kristo  * Copyright (C) 2011 Texas Instruments, Inc.
50a84a91cSTero Kristo  * Tero Kristo <t-kristo@ti.com>
60a84a91cSTero Kristo  *
70a84a91cSTero Kristo  * This program is free software; you can redistribute it and/or modify
80a84a91cSTero Kristo  * it under the terms of the GNU General Public License version 2 as
90a84a91cSTero Kristo  * published by the Free Software Foundation.
100a84a91cSTero Kristo  *
110a84a91cSTero Kristo  *
120a84a91cSTero Kristo  * For historical purposes, the API used to configure the PRM
130a84a91cSTero Kristo  * interrupt handler refers to it as the "PRCM interrupt."  The
140a84a91cSTero Kristo  * underlying registers are located in the PRM on OMAP3/4.
150a84a91cSTero Kristo  *
160a84a91cSTero Kristo  * XXX This code should eventually be moved to a PRM driver.
170a84a91cSTero Kristo  */
180a84a91cSTero Kristo 
190a84a91cSTero Kristo #include <linux/kernel.h>
200a84a91cSTero Kristo #include <linux/module.h>
210a84a91cSTero Kristo #include <linux/init.h>
220a84a91cSTero Kristo #include <linux/io.h>
230a84a91cSTero Kristo #include <linux/irq.h>
240a84a91cSTero Kristo #include <linux/interrupt.h>
250a84a91cSTero Kristo #include <linux/slab.h>
260a84a91cSTero Kristo 
270a84a91cSTero Kristo #include <mach/system.h>
280a84a91cSTero Kristo #include <plat/common.h>
290a84a91cSTero Kristo #include <plat/prcm.h>
300a84a91cSTero Kristo #include <plat/irqs.h>
310a84a91cSTero Kristo 
320a84a91cSTero Kristo #include "prm2xxx_3xxx.h"
330a84a91cSTero Kristo #include "prm44xx.h"
340a84a91cSTero Kristo 
350a84a91cSTero Kristo /*
360a84a91cSTero Kristo  * OMAP_PRCM_MAX_NR_PENDING_REG: maximum number of PRM_IRQ*_MPU regs
370a84a91cSTero Kristo  * XXX this is technically not needed, since
380a84a91cSTero Kristo  * omap_prcm_register_chain_handler() could allocate this based on the
390a84a91cSTero Kristo  * actual amount of memory needed for the SoC
400a84a91cSTero Kristo  */
410a84a91cSTero Kristo #define OMAP_PRCM_MAX_NR_PENDING_REG		2
420a84a91cSTero Kristo 
430a84a91cSTero Kristo /*
440a84a91cSTero Kristo  * prcm_irq_chips: an array of all of the "generic IRQ chips" in use
450a84a91cSTero Kristo  * by the PRCM interrupt handler code.  There will be one 'chip' per
460a84a91cSTero Kristo  * PRM_{IRQSTATUS,IRQENABLE}_MPU register pair.  (So OMAP3 will have
470a84a91cSTero Kristo  * one "chip" and OMAP4 will have two.)
480a84a91cSTero Kristo  */
490a84a91cSTero Kristo static struct irq_chip_generic **prcm_irq_chips;
500a84a91cSTero Kristo 
510a84a91cSTero Kristo /*
520a84a91cSTero Kristo  * prcm_irq_setup: the PRCM IRQ parameters for the hardware the code
530a84a91cSTero Kristo  * is currently running on.  Defined and passed by initialization code
540a84a91cSTero Kristo  * that calls omap_prcm_register_chain_handler().
550a84a91cSTero Kristo  */
560a84a91cSTero Kristo static struct omap_prcm_irq_setup *prcm_irq_setup;
570a84a91cSTero Kristo 
580a84a91cSTero Kristo /* Private functions */
590a84a91cSTero Kristo 
600a84a91cSTero Kristo /*
610a84a91cSTero Kristo  * Move priority events from events to priority_events array
620a84a91cSTero Kristo  */
630a84a91cSTero Kristo static void omap_prcm_events_filter_priority(unsigned long *events,
640a84a91cSTero Kristo 	unsigned long *priority_events)
650a84a91cSTero Kristo {
660a84a91cSTero Kristo 	int i;
670a84a91cSTero Kristo 
680a84a91cSTero Kristo 	for (i = 0; i < prcm_irq_setup->nr_regs; i++) {
690a84a91cSTero Kristo 		priority_events[i] =
700a84a91cSTero Kristo 			events[i] & prcm_irq_setup->priority_mask[i];
710a84a91cSTero Kristo 		events[i] ^= priority_events[i];
720a84a91cSTero Kristo 	}
730a84a91cSTero Kristo }
740a84a91cSTero Kristo 
750a84a91cSTero Kristo /*
760a84a91cSTero Kristo  * PRCM Interrupt Handler
770a84a91cSTero Kristo  *
780a84a91cSTero Kristo  * This is a common handler for the OMAP PRCM interrupts. Pending
790a84a91cSTero Kristo  * interrupts are detected by a call to prcm_pending_events and
800a84a91cSTero Kristo  * dispatched accordingly. Clearing of the wakeup events should be
810a84a91cSTero Kristo  * done by the SoC specific individual handlers.
820a84a91cSTero Kristo  */
830a84a91cSTero Kristo static void omap_prcm_irq_handler(unsigned int irq, struct irq_desc *desc)
840a84a91cSTero Kristo {
850a84a91cSTero Kristo 	unsigned long pending[OMAP_PRCM_MAX_NR_PENDING_REG];
860a84a91cSTero Kristo 	unsigned long priority_pending[OMAP_PRCM_MAX_NR_PENDING_REG];
870a84a91cSTero Kristo 	struct irq_chip *chip = irq_desc_get_chip(desc);
880a84a91cSTero Kristo 	unsigned int virtirq;
890a84a91cSTero Kristo 	int nr_irqs = prcm_irq_setup->nr_regs * 32;
900a84a91cSTero Kristo 
910a84a91cSTero Kristo 	/*
9291285b6fSTero Kristo 	 * If we are suspended, mask all interrupts from PRCM level,
9391285b6fSTero Kristo 	 * this does not ack them, and they will be pending until we
9491285b6fSTero Kristo 	 * re-enable the interrupts, at which point the
9591285b6fSTero Kristo 	 * omap_prcm_irq_handler will be executed again.  The
9691285b6fSTero Kristo 	 * _save_and_clear_irqen() function must ensure that the PRM
9791285b6fSTero Kristo 	 * write to disable all IRQs has reached the PRM before
9891285b6fSTero Kristo 	 * returning, or spurious PRCM interrupts may occur during
9991285b6fSTero Kristo 	 * suspend.
10091285b6fSTero Kristo 	 */
10191285b6fSTero Kristo 	if (prcm_irq_setup->suspended) {
10291285b6fSTero Kristo 		prcm_irq_setup->save_and_clear_irqen(prcm_irq_setup->saved_mask);
10391285b6fSTero Kristo 		prcm_irq_setup->suspend_save_flag = true;
10491285b6fSTero Kristo 	}
10591285b6fSTero Kristo 
10691285b6fSTero Kristo 	/*
1070a84a91cSTero Kristo 	 * Loop until all pending irqs are handled, since
1080a84a91cSTero Kristo 	 * generic_handle_irq() can cause new irqs to come
1090a84a91cSTero Kristo 	 */
11091285b6fSTero Kristo 	while (!prcm_irq_setup->suspended) {
1110a84a91cSTero Kristo 		prcm_irq_setup->read_pending_irqs(pending);
1120a84a91cSTero Kristo 
1130a84a91cSTero Kristo 		/* No bit set, then all IRQs are handled */
1140a84a91cSTero Kristo 		if (find_first_bit(pending, nr_irqs) >= nr_irqs)
1150a84a91cSTero Kristo 			break;
1160a84a91cSTero Kristo 
1170a84a91cSTero Kristo 		omap_prcm_events_filter_priority(pending, priority_pending);
1180a84a91cSTero Kristo 
1190a84a91cSTero Kristo 		/*
1200a84a91cSTero Kristo 		 * Loop on all currently pending irqs so that new irqs
1210a84a91cSTero Kristo 		 * cannot starve previously pending irqs
1220a84a91cSTero Kristo 		 */
1230a84a91cSTero Kristo 
1240a84a91cSTero Kristo 		/* Serve priority events first */
1250a84a91cSTero Kristo 		for_each_set_bit(virtirq, priority_pending, nr_irqs)
1260a84a91cSTero Kristo 			generic_handle_irq(prcm_irq_setup->base_irq + virtirq);
1270a84a91cSTero Kristo 
1280a84a91cSTero Kristo 		/* Serve normal events next */
1290a84a91cSTero Kristo 		for_each_set_bit(virtirq, pending, nr_irqs)
1300a84a91cSTero Kristo 			generic_handle_irq(prcm_irq_setup->base_irq + virtirq);
1310a84a91cSTero Kristo 	}
1320a84a91cSTero Kristo 	if (chip->irq_ack)
1330a84a91cSTero Kristo 		chip->irq_ack(&desc->irq_data);
1340a84a91cSTero Kristo 	if (chip->irq_eoi)
1350a84a91cSTero Kristo 		chip->irq_eoi(&desc->irq_data);
1360a84a91cSTero Kristo 	chip->irq_unmask(&desc->irq_data);
1370a84a91cSTero Kristo 
1380a84a91cSTero Kristo 	prcm_irq_setup->ocp_barrier(); /* avoid spurious IRQs */
1390a84a91cSTero Kristo }
1400a84a91cSTero Kristo 
1410a84a91cSTero Kristo /* Public functions */
1420a84a91cSTero Kristo 
1430a84a91cSTero Kristo /**
1440a84a91cSTero Kristo  * omap_prcm_event_to_irq - given a PRCM event name, returns the
1450a84a91cSTero Kristo  * corresponding IRQ on which the handler should be registered
1460a84a91cSTero Kristo  * @name: name of the PRCM interrupt bit to look up - see struct omap_prcm_irq
1470a84a91cSTero Kristo  *
1480a84a91cSTero Kristo  * Returns the Linux internal IRQ ID corresponding to @name upon success,
1490a84a91cSTero Kristo  * or -ENOENT upon failure.
1500a84a91cSTero Kristo  */
1510a84a91cSTero Kristo int omap_prcm_event_to_irq(const char *name)
1520a84a91cSTero Kristo {
1530a84a91cSTero Kristo 	int i;
1540a84a91cSTero Kristo 
1550a84a91cSTero Kristo 	if (!prcm_irq_setup || !name)
1560a84a91cSTero Kristo 		return -ENOENT;
1570a84a91cSTero Kristo 
1580a84a91cSTero Kristo 	for (i = 0; i < prcm_irq_setup->nr_irqs; i++)
1590a84a91cSTero Kristo 		if (!strcmp(prcm_irq_setup->irqs[i].name, name))
1600a84a91cSTero Kristo 			return prcm_irq_setup->base_irq +
1610a84a91cSTero Kristo 				prcm_irq_setup->irqs[i].offset;
1620a84a91cSTero Kristo 
1630a84a91cSTero Kristo 	return -ENOENT;
1640a84a91cSTero Kristo }
1650a84a91cSTero Kristo 
1660a84a91cSTero Kristo /**
1670a84a91cSTero Kristo  * omap_prcm_irq_cleanup - reverses memory allocated and other steps
1680a84a91cSTero Kristo  * done by omap_prcm_register_chain_handler()
1690a84a91cSTero Kristo  *
1700a84a91cSTero Kristo  * No return value.
1710a84a91cSTero Kristo  */
1720a84a91cSTero Kristo void omap_prcm_irq_cleanup(void)
1730a84a91cSTero Kristo {
1740a84a91cSTero Kristo 	int i;
1750a84a91cSTero Kristo 
1760a84a91cSTero Kristo 	if (!prcm_irq_setup) {
1770a84a91cSTero Kristo 		pr_err("PRCM: IRQ handler not initialized; cannot cleanup\n");
1780a84a91cSTero Kristo 		return;
1790a84a91cSTero Kristo 	}
1800a84a91cSTero Kristo 
1810a84a91cSTero Kristo 	if (prcm_irq_chips) {
1820a84a91cSTero Kristo 		for (i = 0; i < prcm_irq_setup->nr_regs; i++) {
1830a84a91cSTero Kristo 			if (prcm_irq_chips[i])
1840a84a91cSTero Kristo 				irq_remove_generic_chip(prcm_irq_chips[i],
1850a84a91cSTero Kristo 					0xffffffff, 0, 0);
1860a84a91cSTero Kristo 			prcm_irq_chips[i] = NULL;
1870a84a91cSTero Kristo 		}
1880a84a91cSTero Kristo 		kfree(prcm_irq_chips);
1890a84a91cSTero Kristo 		prcm_irq_chips = NULL;
1900a84a91cSTero Kristo 	}
1910a84a91cSTero Kristo 
19291285b6fSTero Kristo 	kfree(prcm_irq_setup->saved_mask);
19391285b6fSTero Kristo 	prcm_irq_setup->saved_mask = NULL;
19491285b6fSTero Kristo 
1950a84a91cSTero Kristo 	kfree(prcm_irq_setup->priority_mask);
1960a84a91cSTero Kristo 	prcm_irq_setup->priority_mask = NULL;
1970a84a91cSTero Kristo 
1980a84a91cSTero Kristo 	irq_set_chained_handler(prcm_irq_setup->irq, NULL);
1990a84a91cSTero Kristo 
2000a84a91cSTero Kristo 	if (prcm_irq_setup->base_irq > 0)
2010a84a91cSTero Kristo 		irq_free_descs(prcm_irq_setup->base_irq,
2020a84a91cSTero Kristo 			prcm_irq_setup->nr_regs * 32);
2030a84a91cSTero Kristo 	prcm_irq_setup->base_irq = 0;
2040a84a91cSTero Kristo }
2050a84a91cSTero Kristo 
20691285b6fSTero Kristo void omap_prcm_irq_prepare(void)
20791285b6fSTero Kristo {
20891285b6fSTero Kristo 	prcm_irq_setup->suspended = true;
20991285b6fSTero Kristo }
21091285b6fSTero Kristo 
21191285b6fSTero Kristo void omap_prcm_irq_complete(void)
21291285b6fSTero Kristo {
21391285b6fSTero Kristo 	prcm_irq_setup->suspended = false;
21491285b6fSTero Kristo 
21591285b6fSTero Kristo 	/* If we have not saved the masks, do not attempt to restore */
21691285b6fSTero Kristo 	if (!prcm_irq_setup->suspend_save_flag)
21791285b6fSTero Kristo 		return;
21891285b6fSTero Kristo 
21991285b6fSTero Kristo 	prcm_irq_setup->suspend_save_flag = false;
22091285b6fSTero Kristo 
22191285b6fSTero Kristo 	/*
22291285b6fSTero Kristo 	 * Re-enable all masked PRCM irq sources, this causes the PRCM
22391285b6fSTero Kristo 	 * interrupt to fire immediately if the events were masked
22491285b6fSTero Kristo 	 * previously in the chain handler
22591285b6fSTero Kristo 	 */
22691285b6fSTero Kristo 	prcm_irq_setup->restore_irqen(prcm_irq_setup->saved_mask);
22791285b6fSTero Kristo }
22891285b6fSTero Kristo 
2290a84a91cSTero Kristo /**
2300a84a91cSTero Kristo  * omap_prcm_register_chain_handler - initializes the prcm chained interrupt
2310a84a91cSTero Kristo  * handler based on provided parameters
2320a84a91cSTero Kristo  * @irq_setup: hardware data about the underlying PRM/PRCM
2330a84a91cSTero Kristo  *
2340a84a91cSTero Kristo  * Set up the PRCM chained interrupt handler on the PRCM IRQ.  Sets up
2350a84a91cSTero Kristo  * one generic IRQ chip per PRM interrupt status/enable register pair.
2360a84a91cSTero Kristo  * Returns 0 upon success, -EINVAL if called twice or if invalid
2370a84a91cSTero Kristo  * arguments are passed, or -ENOMEM on any other error.
2380a84a91cSTero Kristo  */
2390a84a91cSTero Kristo int omap_prcm_register_chain_handler(struct omap_prcm_irq_setup *irq_setup)
2400a84a91cSTero Kristo {
2410a84a91cSTero Kristo 	int nr_regs = irq_setup->nr_regs;
2420a84a91cSTero Kristo 	u32 mask[OMAP_PRCM_MAX_NR_PENDING_REG];
2430a84a91cSTero Kristo 	int offset, i;
2440a84a91cSTero Kristo 	struct irq_chip_generic *gc;
2450a84a91cSTero Kristo 	struct irq_chip_type *ct;
2460a84a91cSTero Kristo 
2470a84a91cSTero Kristo 	if (!irq_setup)
2480a84a91cSTero Kristo 		return -EINVAL;
2490a84a91cSTero Kristo 
2500a84a91cSTero Kristo 	if (prcm_irq_setup) {
2510a84a91cSTero Kristo 		pr_err("PRCM: already initialized; won't reinitialize\n");
2520a84a91cSTero Kristo 		return -EINVAL;
2530a84a91cSTero Kristo 	}
2540a84a91cSTero Kristo 
2550a84a91cSTero Kristo 	if (nr_regs > OMAP_PRCM_MAX_NR_PENDING_REG) {
2560a84a91cSTero Kristo 		pr_err("PRCM: nr_regs too large\n");
2570a84a91cSTero Kristo 		return -EINVAL;
2580a84a91cSTero Kristo 	}
2590a84a91cSTero Kristo 
2600a84a91cSTero Kristo 	prcm_irq_setup = irq_setup;
2610a84a91cSTero Kristo 
2620a84a91cSTero Kristo 	prcm_irq_chips = kzalloc(sizeof(void *) * nr_regs, GFP_KERNEL);
26391285b6fSTero Kristo 	prcm_irq_setup->saved_mask = kzalloc(sizeof(u32) * nr_regs, GFP_KERNEL);
2640a84a91cSTero Kristo 	prcm_irq_setup->priority_mask = kzalloc(sizeof(u32) * nr_regs,
2650a84a91cSTero Kristo 		GFP_KERNEL);
2660a84a91cSTero Kristo 
26791285b6fSTero Kristo 	if (!prcm_irq_chips || !prcm_irq_setup->saved_mask ||
26891285b6fSTero Kristo 	    !prcm_irq_setup->priority_mask) {
2690a84a91cSTero Kristo 		pr_err("PRCM: kzalloc failed\n");
2700a84a91cSTero Kristo 		goto err;
2710a84a91cSTero Kristo 	}
2720a84a91cSTero Kristo 
2730a84a91cSTero Kristo 	memset(mask, 0, sizeof(mask));
2740a84a91cSTero Kristo 
2750a84a91cSTero Kristo 	for (i = 0; i < irq_setup->nr_irqs; i++) {
2760a84a91cSTero Kristo 		offset = irq_setup->irqs[i].offset;
2770a84a91cSTero Kristo 		mask[offset >> 5] |= 1 << (offset & 0x1f);
2780a84a91cSTero Kristo 		if (irq_setup->irqs[i].priority)
2790a84a91cSTero Kristo 			irq_setup->priority_mask[offset >> 5] |=
2800a84a91cSTero Kristo 				1 << (offset & 0x1f);
2810a84a91cSTero Kristo 	}
2820a84a91cSTero Kristo 
2830a84a91cSTero Kristo 	irq_set_chained_handler(irq_setup->irq, omap_prcm_irq_handler);
2840a84a91cSTero Kristo 
2850a84a91cSTero Kristo 	irq_setup->base_irq = irq_alloc_descs(-1, 0, irq_setup->nr_regs * 32,
2860a84a91cSTero Kristo 		0);
2870a84a91cSTero Kristo 
2880a84a91cSTero Kristo 	if (irq_setup->base_irq < 0) {
2890a84a91cSTero Kristo 		pr_err("PRCM: failed to allocate irq descs: %d\n",
2900a84a91cSTero Kristo 			irq_setup->base_irq);
2910a84a91cSTero Kristo 		goto err;
2920a84a91cSTero Kristo 	}
2930a84a91cSTero Kristo 
294*4ba7c3c3SMing Lei 	for (i = 0; i < irq_setup->nr_regs; i++) {
2950a84a91cSTero Kristo 		gc = irq_alloc_generic_chip("PRCM", 1,
2960a84a91cSTero Kristo 			irq_setup->base_irq + i * 32, prm_base,
2970a84a91cSTero Kristo 			handle_level_irq);
2980a84a91cSTero Kristo 
2990a84a91cSTero Kristo 		if (!gc) {
3000a84a91cSTero Kristo 			pr_err("PRCM: failed to allocate generic chip\n");
3010a84a91cSTero Kristo 			goto err;
3020a84a91cSTero Kristo 		}
3030a84a91cSTero Kristo 		ct = gc->chip_types;
3040a84a91cSTero Kristo 		ct->chip.irq_ack = irq_gc_ack_set_bit;
3050a84a91cSTero Kristo 		ct->chip.irq_mask = irq_gc_mask_clr_bit;
3060a84a91cSTero Kristo 		ct->chip.irq_unmask = irq_gc_mask_set_bit;
3070a84a91cSTero Kristo 
3080a84a91cSTero Kristo 		ct->regs.ack = irq_setup->ack + i * 4;
3090a84a91cSTero Kristo 		ct->regs.mask = irq_setup->mask + i * 4;
3100a84a91cSTero Kristo 
3110a84a91cSTero Kristo 		irq_setup_generic_chip(gc, mask[i], 0, IRQ_NOREQUEST, 0);
3120a84a91cSTero Kristo 		prcm_irq_chips[i] = gc;
3130a84a91cSTero Kristo 	}
3140a84a91cSTero Kristo 
3150a84a91cSTero Kristo 	return 0;
3160a84a91cSTero Kristo 
3170a84a91cSTero Kristo err:
3180a84a91cSTero Kristo 	omap_prcm_irq_cleanup();
3190a84a91cSTero Kristo 	return -ENOMEM;
3200a84a91cSTero Kristo }
321