18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * IRQ chip definitions for INTC IRQs.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2007, 2008 Magnus Damm
58c2ecf20Sopenharmony_ci * Copyright (C) 2009 - 2012 Paul Mundt
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
88c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
98c2ecf20Sopenharmony_ci * for more details.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
128c2ecf20Sopenharmony_ci#include <linux/bsearch.h>
138c2ecf20Sopenharmony_ci#include <linux/io.h>
148c2ecf20Sopenharmony_ci#include "internals.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_civoid _intc_enable(struct irq_data *data, unsigned long handle)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	unsigned int irq = data->irq;
198c2ecf20Sopenharmony_ci	struct intc_desc_int *d = get_intc_desc(irq);
208c2ecf20Sopenharmony_ci	unsigned long addr;
218c2ecf20Sopenharmony_ci	unsigned int cpu;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
248c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
258c2ecf20Sopenharmony_ci		if (!cpumask_test_cpu(cpu, irq_data_get_affinity_mask(data)))
268c2ecf20Sopenharmony_ci			continue;
278c2ecf20Sopenharmony_ci#endif
288c2ecf20Sopenharmony_ci		addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
298c2ecf20Sopenharmony_ci		intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
308c2ecf20Sopenharmony_ci						    [_INTC_FN(handle)], irq);
318c2ecf20Sopenharmony_ci	}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	intc_balancing_enable(irq);
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic void intc_enable(struct irq_data *data)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	_intc_enable(data, (unsigned long)irq_data_get_irq_chip_data(data));
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic void intc_disable(struct irq_data *data)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	unsigned int irq = data->irq;
448c2ecf20Sopenharmony_ci	struct intc_desc_int *d = get_intc_desc(irq);
458c2ecf20Sopenharmony_ci	unsigned long handle = (unsigned long)irq_data_get_irq_chip_data(data);
468c2ecf20Sopenharmony_ci	unsigned long addr;
478c2ecf20Sopenharmony_ci	unsigned int cpu;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	intc_balancing_disable(irq);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
528c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
538c2ecf20Sopenharmony_ci		if (!cpumask_test_cpu(cpu, irq_data_get_affinity_mask(data)))
548c2ecf20Sopenharmony_ci			continue;
558c2ecf20Sopenharmony_ci#endif
568c2ecf20Sopenharmony_ci		addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
578c2ecf20Sopenharmony_ci		intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
588c2ecf20Sopenharmony_ci						     [_INTC_FN(handle)], irq);
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
638c2ecf20Sopenharmony_ci/*
648c2ecf20Sopenharmony_ci * This is held with the irq desc lock held, so we don't require any
658c2ecf20Sopenharmony_ci * additional locking here at the intc desc level. The affinity mask is
668c2ecf20Sopenharmony_ci * later tested in the enable/disable paths.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cistatic int intc_set_affinity(struct irq_data *data,
698c2ecf20Sopenharmony_ci			     const struct cpumask *cpumask,
708c2ecf20Sopenharmony_ci			     bool force)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	if (!cpumask_intersects(cpumask, cpu_online_mask))
738c2ecf20Sopenharmony_ci		return -1;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	cpumask_copy(irq_data_get_affinity_mask(data), cpumask);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return IRQ_SET_MASK_OK_NOCOPY;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci#endif
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic void intc_mask_ack(struct irq_data *data)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	unsigned int irq = data->irq;
848c2ecf20Sopenharmony_ci	struct intc_desc_int *d = get_intc_desc(irq);
858c2ecf20Sopenharmony_ci	unsigned long handle = intc_get_ack_handle(irq);
868c2ecf20Sopenharmony_ci	void __iomem *addr;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	intc_disable(data);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/* read register and write zero only to the associated bit */
918c2ecf20Sopenharmony_ci	if (handle) {
928c2ecf20Sopenharmony_ci		unsigned int value;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci		addr = (void __iomem *)INTC_REG(d, _INTC_ADDR_D(handle), 0);
958c2ecf20Sopenharmony_ci		value = intc_set_field_from_handle(0, 1, handle);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci		switch (_INTC_FN(handle)) {
988c2ecf20Sopenharmony_ci		case REG_FN_MODIFY_BASE + 0:	/* 8bit */
998c2ecf20Sopenharmony_ci			__raw_readb(addr);
1008c2ecf20Sopenharmony_ci			__raw_writeb(0xff ^ value, addr);
1018c2ecf20Sopenharmony_ci			break;
1028c2ecf20Sopenharmony_ci		case REG_FN_MODIFY_BASE + 1:	/* 16bit */
1038c2ecf20Sopenharmony_ci			__raw_readw(addr);
1048c2ecf20Sopenharmony_ci			__raw_writew(0xffff ^ value, addr);
1058c2ecf20Sopenharmony_ci			break;
1068c2ecf20Sopenharmony_ci		case REG_FN_MODIFY_BASE + 3:	/* 32bit */
1078c2ecf20Sopenharmony_ci			__raw_readl(addr);
1088c2ecf20Sopenharmony_ci			__raw_writel(0xffffffff ^ value, addr);
1098c2ecf20Sopenharmony_ci			break;
1108c2ecf20Sopenharmony_ci		default:
1118c2ecf20Sopenharmony_ci			BUG();
1128c2ecf20Sopenharmony_ci			break;
1138c2ecf20Sopenharmony_ci		}
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
1188c2ecf20Sopenharmony_ci					     unsigned int nr_hp,
1198c2ecf20Sopenharmony_ci					     unsigned int irq)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct intc_handle_int key;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	key.irq = irq;
1248c2ecf20Sopenharmony_ci	key.handle = 0;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	return bsearch(&key, hp, nr_hp, sizeof(*hp), intc_handle_int_cmp);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciint intc_set_priority(unsigned int irq, unsigned int prio)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	struct intc_desc_int *d = get_intc_desc(irq);
1328c2ecf20Sopenharmony_ci	struct irq_data *data = irq_get_irq_data(irq);
1338c2ecf20Sopenharmony_ci	struct intc_handle_int *ihp;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (!intc_get_prio_level(irq) || prio <= 1)
1368c2ecf20Sopenharmony_ci		return -EINVAL;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	ihp = intc_find_irq(d->prio, d->nr_prio, irq);
1398c2ecf20Sopenharmony_ci	if (ihp) {
1408c2ecf20Sopenharmony_ci		if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
1418c2ecf20Sopenharmony_ci			return -EINVAL;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci		intc_set_prio_level(irq, prio);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		/*
1468c2ecf20Sopenharmony_ci		 * only set secondary masking method directly
1478c2ecf20Sopenharmony_ci		 * primary masking method is using intc_prio_level[irq]
1488c2ecf20Sopenharmony_ci		 * priority level will be set during next enable()
1498c2ecf20Sopenharmony_ci		 */
1508c2ecf20Sopenharmony_ci		if (_INTC_FN(ihp->handle) != REG_FN_ERR)
1518c2ecf20Sopenharmony_ci			_intc_enable(data, ihp->handle);
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci	return 0;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci#define SENSE_VALID_FLAG 0x80
1578c2ecf20Sopenharmony_ci#define VALID(x) (x | SENSE_VALID_FLAG)
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
1608c2ecf20Sopenharmony_ci	[IRQ_TYPE_EDGE_FALLING] = VALID(0),
1618c2ecf20Sopenharmony_ci	[IRQ_TYPE_EDGE_RISING] = VALID(1),
1628c2ecf20Sopenharmony_ci	[IRQ_TYPE_LEVEL_LOW] = VALID(2),
1638c2ecf20Sopenharmony_ci	/* SH7706, SH7707 and SH7709 do not support high level triggered */
1648c2ecf20Sopenharmony_ci#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
1658c2ecf20Sopenharmony_ci    !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
1668c2ecf20Sopenharmony_ci    !defined(CONFIG_CPU_SUBTYPE_SH7709)
1678c2ecf20Sopenharmony_ci	[IRQ_TYPE_LEVEL_HIGH] = VALID(3),
1688c2ecf20Sopenharmony_ci#endif
1698c2ecf20Sopenharmony_ci#if defined(CONFIG_ARM) /* all recent SH-Mobile / R-Mobile ARM support this */
1708c2ecf20Sopenharmony_ci	[IRQ_TYPE_EDGE_BOTH] = VALID(4),
1718c2ecf20Sopenharmony_ci#endif
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic int intc_set_type(struct irq_data *data, unsigned int type)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	unsigned int irq = data->irq;
1778c2ecf20Sopenharmony_ci	struct intc_desc_int *d = get_intc_desc(irq);
1788c2ecf20Sopenharmony_ci	unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
1798c2ecf20Sopenharmony_ci	struct intc_handle_int *ihp;
1808c2ecf20Sopenharmony_ci	unsigned long addr;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	if (!value)
1838c2ecf20Sopenharmony_ci		return -EINVAL;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	value &= ~SENSE_VALID_FLAG;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	ihp = intc_find_irq(d->sense, d->nr_sense, irq);
1888c2ecf20Sopenharmony_ci	if (ihp) {
1898c2ecf20Sopenharmony_ci		/* PINT has 2-bit sense registers, should fail on EDGE_BOTH */
1908c2ecf20Sopenharmony_ci		if (value >= (1 << _INTC_WIDTH(ihp->handle)))
1918c2ecf20Sopenharmony_ci			return -EINVAL;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci		addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
1948c2ecf20Sopenharmony_ci		intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return 0;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistruct irq_chip intc_irq_chip	= {
2018c2ecf20Sopenharmony_ci	.irq_mask		= intc_disable,
2028c2ecf20Sopenharmony_ci	.irq_unmask		= intc_enable,
2038c2ecf20Sopenharmony_ci	.irq_mask_ack		= intc_mask_ack,
2048c2ecf20Sopenharmony_ci	.irq_enable		= intc_enable,
2058c2ecf20Sopenharmony_ci	.irq_disable		= intc_disable,
2068c2ecf20Sopenharmony_ci	.irq_set_type		= intc_set_type,
2078c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
2088c2ecf20Sopenharmony_ci	.irq_set_affinity	= intc_set_affinity,
2098c2ecf20Sopenharmony_ci#endif
2108c2ecf20Sopenharmony_ci	.flags			= IRQCHIP_SKIP_SET_WAKE,
2118c2ecf20Sopenharmony_ci};
212