xref: /openbmc/linux/drivers/sh/intc/chip.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12be6bb0cSPaul Mundt /*
22be6bb0cSPaul Mundt  * IRQ chip definitions for INTC IRQs.
32be6bb0cSPaul Mundt  *
42be6bb0cSPaul Mundt  * Copyright (C) 2007, 2008 Magnus Damm
5b59f9f97SPaul Mundt  * Copyright (C) 2009 - 2012 Paul Mundt
62be6bb0cSPaul Mundt  *
72be6bb0cSPaul Mundt  * This file is subject to the terms and conditions of the GNU General Public
82be6bb0cSPaul Mundt  * License.  See the file "COPYING" in the main directory of this archive
92be6bb0cSPaul Mundt  * for more details.
102be6bb0cSPaul Mundt  */
112be6bb0cSPaul Mundt #include <linux/cpumask.h>
12b59f9f97SPaul Mundt #include <linux/bsearch.h>
132be6bb0cSPaul Mundt #include <linux/io.h>
142be6bb0cSPaul Mundt #include "internals.h"
152be6bb0cSPaul Mundt 
_intc_enable(struct irq_data * data,unsigned long handle)1626599a94SPaul Mundt void _intc_enable(struct irq_data *data, unsigned long handle)
172be6bb0cSPaul Mundt {
1826599a94SPaul Mundt 	unsigned int irq = data->irq;
192be6bb0cSPaul Mundt 	struct intc_desc_int *d = get_intc_desc(irq);
202be6bb0cSPaul Mundt 	unsigned long addr;
212be6bb0cSPaul Mundt 	unsigned int cpu;
222be6bb0cSPaul Mundt 
232be6bb0cSPaul Mundt 	for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
242be6bb0cSPaul Mundt #ifdef CONFIG_SMP
258b8149dfSThomas Gleixner 		if (!cpumask_test_cpu(cpu, irq_data_get_affinity_mask(data)))
262be6bb0cSPaul Mundt 			continue;
272be6bb0cSPaul Mundt #endif
282be6bb0cSPaul Mundt 		addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
292be6bb0cSPaul Mundt 		intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
302be6bb0cSPaul Mundt 						    [_INTC_FN(handle)], irq);
312be6bb0cSPaul Mundt 	}
322be6bb0cSPaul Mundt 
332be6bb0cSPaul Mundt 	intc_balancing_enable(irq);
342be6bb0cSPaul Mundt }
352be6bb0cSPaul Mundt 
intc_enable(struct irq_data * data)3626599a94SPaul Mundt static void intc_enable(struct irq_data *data)
372be6bb0cSPaul Mundt {
3826599a94SPaul Mundt 	_intc_enable(data, (unsigned long)irq_data_get_irq_chip_data(data));
392be6bb0cSPaul Mundt }
402be6bb0cSPaul Mundt 
intc_disable(struct irq_data * data)4126599a94SPaul Mundt static void intc_disable(struct irq_data *data)
422be6bb0cSPaul Mundt {
4326599a94SPaul Mundt 	unsigned int irq = data->irq;
442be6bb0cSPaul Mundt 	struct intc_desc_int *d = get_intc_desc(irq);
4526599a94SPaul Mundt 	unsigned long handle = (unsigned long)irq_data_get_irq_chip_data(data);
462be6bb0cSPaul Mundt 	unsigned long addr;
472be6bb0cSPaul Mundt 	unsigned int cpu;
482be6bb0cSPaul Mundt 
492be6bb0cSPaul Mundt 	intc_balancing_disable(irq);
502be6bb0cSPaul Mundt 
512be6bb0cSPaul Mundt 	for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
522be6bb0cSPaul Mundt #ifdef CONFIG_SMP
538b8149dfSThomas Gleixner 		if (!cpumask_test_cpu(cpu, irq_data_get_affinity_mask(data)))
542be6bb0cSPaul Mundt 			continue;
552be6bb0cSPaul Mundt #endif
562be6bb0cSPaul Mundt 		addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
572be6bb0cSPaul Mundt 		intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
582be6bb0cSPaul Mundt 						     [_INTC_FN(handle)], irq);
592be6bb0cSPaul Mundt 	}
602be6bb0cSPaul Mundt }
612be6bb0cSPaul Mundt 
622be6bb0cSPaul Mundt #ifdef CONFIG_SMP
632be6bb0cSPaul Mundt /*
642be6bb0cSPaul Mundt  * This is held with the irq desc lock held, so we don't require any
652be6bb0cSPaul Mundt  * additional locking here at the intc desc level. The affinity mask is
662be6bb0cSPaul Mundt  * later tested in the enable/disable paths.
672be6bb0cSPaul Mundt  */
intc_set_affinity(struct irq_data * data,const struct cpumask * cpumask,bool force)6826599a94SPaul Mundt static int intc_set_affinity(struct irq_data *data,
6926599a94SPaul Mundt 			     const struct cpumask *cpumask,
7026599a94SPaul Mundt 			     bool force)
712be6bb0cSPaul Mundt {
722be6bb0cSPaul Mundt 	if (!cpumask_intersects(cpumask, cpu_online_mask))
732be6bb0cSPaul Mundt 		return -1;
742be6bb0cSPaul Mundt 
75*073352e9SSamuel Holland 	irq_data_update_affinity(data, cpumask);
762be6bb0cSPaul Mundt 
7730377642SPaul Mundt 	return IRQ_SET_MASK_OK_NOCOPY;
782be6bb0cSPaul Mundt }
792be6bb0cSPaul Mundt #endif
802be6bb0cSPaul Mundt 
intc_mask_ack(struct irq_data * data)8126599a94SPaul Mundt static void intc_mask_ack(struct irq_data *data)
822be6bb0cSPaul Mundt {
8326599a94SPaul Mundt 	unsigned int irq = data->irq;
842be6bb0cSPaul Mundt 	struct intc_desc_int *d = get_intc_desc(irq);
852be6bb0cSPaul Mundt 	unsigned long handle = intc_get_ack_handle(irq);
860dd4d5cbSPaul Mundt 	void __iomem *addr;
872be6bb0cSPaul Mundt 
8826599a94SPaul Mundt 	intc_disable(data);
892be6bb0cSPaul Mundt 
902be6bb0cSPaul Mundt 	/* read register and write zero only to the associated bit */
912be6bb0cSPaul Mundt 	if (handle) {
922be6bb0cSPaul Mundt 		unsigned int value;
932be6bb0cSPaul Mundt 
940dd4d5cbSPaul Mundt 		addr = (void __iomem *)INTC_REG(d, _INTC_ADDR_D(handle), 0);
952be6bb0cSPaul Mundt 		value = intc_set_field_from_handle(0, 1, handle);
962be6bb0cSPaul Mundt 
972be6bb0cSPaul Mundt 		switch (_INTC_FN(handle)) {
982be6bb0cSPaul Mundt 		case REG_FN_MODIFY_BASE + 0:	/* 8bit */
992be6bb0cSPaul Mundt 			__raw_readb(addr);
1002be6bb0cSPaul Mundt 			__raw_writeb(0xff ^ value, addr);
1012be6bb0cSPaul Mundt 			break;
1022be6bb0cSPaul Mundt 		case REG_FN_MODIFY_BASE + 1:	/* 16bit */
1032be6bb0cSPaul Mundt 			__raw_readw(addr);
1042be6bb0cSPaul Mundt 			__raw_writew(0xffff ^ value, addr);
1052be6bb0cSPaul Mundt 			break;
1062be6bb0cSPaul Mundt 		case REG_FN_MODIFY_BASE + 3:	/* 32bit */
1072be6bb0cSPaul Mundt 			__raw_readl(addr);
1082be6bb0cSPaul Mundt 			__raw_writel(0xffffffff ^ value, addr);
1092be6bb0cSPaul Mundt 			break;
1102be6bb0cSPaul Mundt 		default:
1112be6bb0cSPaul Mundt 			BUG();
1122be6bb0cSPaul Mundt 			break;
1132be6bb0cSPaul Mundt 		}
1142be6bb0cSPaul Mundt 	}
1152be6bb0cSPaul Mundt }
1162be6bb0cSPaul Mundt 
intc_find_irq(struct intc_handle_int * hp,unsigned int nr_hp,unsigned int irq)1172be6bb0cSPaul Mundt static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
1182be6bb0cSPaul Mundt 					     unsigned int nr_hp,
1192be6bb0cSPaul Mundt 					     unsigned int irq)
1202be6bb0cSPaul Mundt {
121b59f9f97SPaul Mundt 	struct intc_handle_int key;
1222be6bb0cSPaul Mundt 
123b59f9f97SPaul Mundt 	key.irq = irq;
124b59f9f97SPaul Mundt 	key.handle = 0;
1252be6bb0cSPaul Mundt 
126b59f9f97SPaul Mundt 	return bsearch(&key, hp, nr_hp, sizeof(*hp), intc_handle_int_cmp);
1272be6bb0cSPaul Mundt }
1282be6bb0cSPaul Mundt 
intc_set_priority(unsigned int irq,unsigned int prio)1292be6bb0cSPaul Mundt int intc_set_priority(unsigned int irq, unsigned int prio)
1302be6bb0cSPaul Mundt {
1312be6bb0cSPaul Mundt 	struct intc_desc_int *d = get_intc_desc(irq);
13226599a94SPaul Mundt 	struct irq_data *data = irq_get_irq_data(irq);
1332be6bb0cSPaul Mundt 	struct intc_handle_int *ihp;
1342be6bb0cSPaul Mundt 
1352be6bb0cSPaul Mundt 	if (!intc_get_prio_level(irq) || prio <= 1)
1362be6bb0cSPaul Mundt 		return -EINVAL;
1372be6bb0cSPaul Mundt 
1382be6bb0cSPaul Mundt 	ihp = intc_find_irq(d->prio, d->nr_prio, irq);
1392be6bb0cSPaul Mundt 	if (ihp) {
1402be6bb0cSPaul Mundt 		if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
1412be6bb0cSPaul Mundt 			return -EINVAL;
1422be6bb0cSPaul Mundt 
1432be6bb0cSPaul Mundt 		intc_set_prio_level(irq, prio);
1442be6bb0cSPaul Mundt 
1452be6bb0cSPaul Mundt 		/*
1462be6bb0cSPaul Mundt 		 * only set secondary masking method directly
1472be6bb0cSPaul Mundt 		 * primary masking method is using intc_prio_level[irq]
1482be6bb0cSPaul Mundt 		 * priority level will be set during next enable()
1492be6bb0cSPaul Mundt 		 */
1502be6bb0cSPaul Mundt 		if (_INTC_FN(ihp->handle) != REG_FN_ERR)
15126599a94SPaul Mundt 			_intc_enable(data, ihp->handle);
1522be6bb0cSPaul Mundt 	}
1532be6bb0cSPaul Mundt 	return 0;
1542be6bb0cSPaul Mundt }
1552be6bb0cSPaul Mundt 
1568a5a7786SMagnus Damm #define SENSE_VALID_FLAG 0x80
1578a5a7786SMagnus Damm #define VALID(x) (x | SENSE_VALID_FLAG)
1582be6bb0cSPaul Mundt 
1592be6bb0cSPaul Mundt static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
1602be6bb0cSPaul Mundt 	[IRQ_TYPE_EDGE_FALLING] = VALID(0),
1612be6bb0cSPaul Mundt 	[IRQ_TYPE_EDGE_RISING] = VALID(1),
1622be6bb0cSPaul Mundt 	[IRQ_TYPE_LEVEL_LOW] = VALID(2),
1632be6bb0cSPaul Mundt 	/* SH7706, SH7707 and SH7709 do not support high level triggered */
1642be6bb0cSPaul Mundt #if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
1652be6bb0cSPaul Mundt     !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
1662be6bb0cSPaul Mundt     !defined(CONFIG_CPU_SUBTYPE_SH7709)
1672be6bb0cSPaul Mundt 	[IRQ_TYPE_LEVEL_HIGH] = VALID(3),
1682be6bb0cSPaul Mundt #endif
1697d377b17SMagnus Damm #if defined(CONFIG_ARM) /* all recent SH-Mobile / R-Mobile ARM support this */
1709a14a92cSMagnus Damm 	[IRQ_TYPE_EDGE_BOTH] = VALID(4),
1719a14a92cSMagnus Damm #endif
1722be6bb0cSPaul Mundt };
1732be6bb0cSPaul Mundt 
intc_set_type(struct irq_data * data,unsigned int type)17426599a94SPaul Mundt static int intc_set_type(struct irq_data *data, unsigned int type)
1752be6bb0cSPaul Mundt {
17626599a94SPaul Mundt 	unsigned int irq = data->irq;
1772be6bb0cSPaul Mundt 	struct intc_desc_int *d = get_intc_desc(irq);
1782be6bb0cSPaul Mundt 	unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
1792be6bb0cSPaul Mundt 	struct intc_handle_int *ihp;
1802be6bb0cSPaul Mundt 	unsigned long addr;
1812be6bb0cSPaul Mundt 
1822be6bb0cSPaul Mundt 	if (!value)
1832be6bb0cSPaul Mundt 		return -EINVAL;
1842be6bb0cSPaul Mundt 
18552e3124fSMagnus Damm 	value &= ~SENSE_VALID_FLAG;
18652e3124fSMagnus Damm 
1872be6bb0cSPaul Mundt 	ihp = intc_find_irq(d->sense, d->nr_sense, irq);
1882be6bb0cSPaul Mundt 	if (ihp) {
18952e3124fSMagnus Damm 		/* PINT has 2-bit sense registers, should fail on EDGE_BOTH */
19052e3124fSMagnus Damm 		if (value >= (1 << _INTC_WIDTH(ihp->handle)))
19152e3124fSMagnus Damm 			return -EINVAL;
19252e3124fSMagnus Damm 
1932be6bb0cSPaul Mundt 		addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
19452e3124fSMagnus Damm 		intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
1952be6bb0cSPaul Mundt 	}
1962be6bb0cSPaul Mundt 
1972be6bb0cSPaul Mundt 	return 0;
1982be6bb0cSPaul Mundt }
1992be6bb0cSPaul Mundt 
2002be6bb0cSPaul Mundt struct irq_chip intc_irq_chip	= {
20126599a94SPaul Mundt 	.irq_mask		= intc_disable,
20226599a94SPaul Mundt 	.irq_unmask		= intc_enable,
20326599a94SPaul Mundt 	.irq_mask_ack		= intc_mask_ack,
20426599a94SPaul Mundt 	.irq_enable		= intc_enable,
20526599a94SPaul Mundt 	.irq_disable		= intc_disable,
20626599a94SPaul Mundt 	.irq_set_type		= intc_set_type,
2072be6bb0cSPaul Mundt #ifdef CONFIG_SMP
20826599a94SPaul Mundt 	.irq_set_affinity	= intc_set_affinity,
2092be6bb0cSPaul Mundt #endif
2105bbda4e4SPaul Mundt 	.flags			= IRQCHIP_SKIP_SET_WAKE,
2112be6bb0cSPaul Mundt };
212