162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#include <linux/sh_intc.h>
362306a36Sopenharmony_ci#include <linux/irq.h>
462306a36Sopenharmony_ci#include <linux/irqdomain.h>
562306a36Sopenharmony_ci#include <linux/list.h>
662306a36Sopenharmony_ci#include <linux/kernel.h>
762306a36Sopenharmony_ci#include <linux/types.h>
862306a36Sopenharmony_ci#include <linux/radix-tree.h>
962306a36Sopenharmony_ci#include <linux/device.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
1262306a36Sopenharmony_ci	((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
1362306a36Sopenharmony_ci	 ((addr_e) << 16) | ((addr_d << 24)))
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#define _INTC_SHIFT(h)		(h & 0x1f)
1662306a36Sopenharmony_ci#define _INTC_WIDTH(h)		((h >> 5) & 0xf)
1762306a36Sopenharmony_ci#define _INTC_FN(h)		((h >> 9) & 0xf)
1862306a36Sopenharmony_ci#define _INTC_MODE(h)		((h >> 13) & 0x7)
1962306a36Sopenharmony_ci#define _INTC_ADDR_E(h)		((h >> 16) & 0xff)
2062306a36Sopenharmony_ci#define _INTC_ADDR_D(h)		((h >> 24) & 0xff)
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#ifdef CONFIG_SMP
2362306a36Sopenharmony_ci#define IS_SMP(x)		(x.smp)
2462306a36Sopenharmony_ci#define INTC_REG(d, x, c)	(d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
2562306a36Sopenharmony_ci#define SMP_NR(d, x)		((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
2662306a36Sopenharmony_ci#else
2762306a36Sopenharmony_ci#define IS_SMP(x)		0
2862306a36Sopenharmony_ci#define INTC_REG(d, x, c)	(d->reg[(x)])
2962306a36Sopenharmony_ci#define SMP_NR(d, x)		1
3062306a36Sopenharmony_ci#endif
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistruct intc_handle_int {
3362306a36Sopenharmony_ci	unsigned int irq;
3462306a36Sopenharmony_ci	unsigned long handle;
3562306a36Sopenharmony_ci};
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistruct intc_window {
3862306a36Sopenharmony_ci	phys_addr_t phys;
3962306a36Sopenharmony_ci	void __iomem *virt;
4062306a36Sopenharmony_ci	unsigned long size;
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistruct intc_map_entry {
4462306a36Sopenharmony_ci	intc_enum enum_id;
4562306a36Sopenharmony_ci	struct intc_desc_int *desc;
4662306a36Sopenharmony_ci};
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistruct intc_subgroup_entry {
4962306a36Sopenharmony_ci	unsigned int pirq;
5062306a36Sopenharmony_ci	intc_enum enum_id;
5162306a36Sopenharmony_ci	unsigned long handle;
5262306a36Sopenharmony_ci};
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistruct intc_desc_int {
5562306a36Sopenharmony_ci	struct list_head list;
5662306a36Sopenharmony_ci	struct device dev;
5762306a36Sopenharmony_ci	struct radix_tree_root tree;
5862306a36Sopenharmony_ci	raw_spinlock_t lock;
5962306a36Sopenharmony_ci	unsigned int index;
6062306a36Sopenharmony_ci	unsigned long *reg;
6162306a36Sopenharmony_ci#ifdef CONFIG_SMP
6262306a36Sopenharmony_ci	unsigned long *smp;
6362306a36Sopenharmony_ci#endif
6462306a36Sopenharmony_ci	unsigned int nr_reg;
6562306a36Sopenharmony_ci	struct intc_handle_int *prio;
6662306a36Sopenharmony_ci	unsigned int nr_prio;
6762306a36Sopenharmony_ci	struct intc_handle_int *sense;
6862306a36Sopenharmony_ci	unsigned int nr_sense;
6962306a36Sopenharmony_ci	struct intc_window *window;
7062306a36Sopenharmony_ci	unsigned int nr_windows;
7162306a36Sopenharmony_ci	struct irq_domain *domain;
7262306a36Sopenharmony_ci	struct irq_chip chip;
7362306a36Sopenharmony_ci	bool skip_suspend;
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_cienum {
7862306a36Sopenharmony_ci	REG_FN_ERR = 0,
7962306a36Sopenharmony_ci	REG_FN_TEST_BASE = 1,
8062306a36Sopenharmony_ci	REG_FN_WRITE_BASE = 5,
8162306a36Sopenharmony_ci	REG_FN_MODIFY_BASE = 9
8262306a36Sopenharmony_ci};
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cienum {	MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
8562306a36Sopenharmony_ci	MODE_MASK_REG,       /* Bit(s) set -> interrupt disabled */
8662306a36Sopenharmony_ci	MODE_DUAL_REG,       /* Two registers, set bit to enable / disable */
8762306a36Sopenharmony_ci	MODE_PRIO_REG,       /* Priority value written to enable interrupt */
8862306a36Sopenharmony_ci	MODE_PCLR_REG,       /* Above plus all bits set to disable interrupt */
8962306a36Sopenharmony_ci};
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_cistatic inline struct intc_desc_int *get_intc_desc(unsigned int irq)
9262306a36Sopenharmony_ci{
9362306a36Sopenharmony_ci	struct irq_chip *chip = irq_get_chip(irq);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	return container_of(chip, struct intc_desc_int, chip);
9662306a36Sopenharmony_ci}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci/*
9962306a36Sopenharmony_ci * Grumble.
10062306a36Sopenharmony_ci */
10162306a36Sopenharmony_cistatic inline void activate_irq(int irq)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	irq_modify_status(irq, IRQ_NOREQUEST, IRQ_NOPROBE);
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic inline int intc_handle_int_cmp(const void *a, const void *b)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	const struct intc_handle_int *_a = a;
10962306a36Sopenharmony_ci	const struct intc_handle_int *_b = b;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	return _a->irq - _b->irq;
11262306a36Sopenharmony_ci}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci/* access.c */
11562306a36Sopenharmony_ciextern unsigned long
11662306a36Sopenharmony_ci(*intc_reg_fns[])(unsigned long addr, unsigned long h, unsigned long data);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciextern unsigned long
11962306a36Sopenharmony_ci(*intc_enable_fns[])(unsigned long addr, unsigned long handle,
12062306a36Sopenharmony_ci		     unsigned long (*fn)(unsigned long,
12162306a36Sopenharmony_ci				unsigned long, unsigned long),
12262306a36Sopenharmony_ci		     unsigned int irq);
12362306a36Sopenharmony_ciextern unsigned long
12462306a36Sopenharmony_ci(*intc_disable_fns[])(unsigned long addr, unsigned long handle,
12562306a36Sopenharmony_ci		      unsigned long (*fn)(unsigned long,
12662306a36Sopenharmony_ci				unsigned long, unsigned long),
12762306a36Sopenharmony_ci		      unsigned int irq);
12862306a36Sopenharmony_ciextern unsigned long
12962306a36Sopenharmony_ci(*intc_enable_noprio_fns[])(unsigned long addr, unsigned long handle,
13062306a36Sopenharmony_ci		            unsigned long (*fn)(unsigned long,
13162306a36Sopenharmony_ci				unsigned long, unsigned long),
13262306a36Sopenharmony_ci			    unsigned int irq);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciunsigned long intc_phys_to_virt(struct intc_desc_int *d, unsigned long address);
13562306a36Sopenharmony_ciunsigned int intc_get_reg(struct intc_desc_int *d, unsigned long address);
13662306a36Sopenharmony_ciunsigned int intc_set_field_from_handle(unsigned int value,
13762306a36Sopenharmony_ci			    unsigned int field_value,
13862306a36Sopenharmony_ci			    unsigned int handle);
13962306a36Sopenharmony_ciunsigned long intc_get_field_from_handle(unsigned int value,
14062306a36Sopenharmony_ci					 unsigned int handle);
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci/* balancing.c */
14362306a36Sopenharmony_ci#ifdef CONFIG_INTC_BALANCING
14462306a36Sopenharmony_civoid intc_balancing_enable(unsigned int irq);
14562306a36Sopenharmony_civoid intc_balancing_disable(unsigned int irq);
14662306a36Sopenharmony_civoid intc_set_dist_handle(unsigned int irq, struct intc_desc *desc,
14762306a36Sopenharmony_ci			  struct intc_desc_int *d, intc_enum id);
14862306a36Sopenharmony_ci#else
14962306a36Sopenharmony_cistatic inline void intc_balancing_enable(unsigned int irq) { }
15062306a36Sopenharmony_cistatic inline void intc_balancing_disable(unsigned int irq) { }
15162306a36Sopenharmony_cistatic inline void
15262306a36Sopenharmony_ciintc_set_dist_handle(unsigned int irq, struct intc_desc *desc,
15362306a36Sopenharmony_ci		     struct intc_desc_int *d, intc_enum id) { }
15462306a36Sopenharmony_ci#endif
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci/* chip.c */
15762306a36Sopenharmony_ciextern struct irq_chip intc_irq_chip;
15862306a36Sopenharmony_civoid _intc_enable(struct irq_data *data, unsigned long handle);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci/* core.c */
16162306a36Sopenharmony_ciextern struct list_head intc_list;
16262306a36Sopenharmony_ciextern raw_spinlock_t intc_big_lock;
16362306a36Sopenharmony_ciextern struct bus_type intc_subsys;
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciunsigned int intc_get_dfl_prio_level(void);
16662306a36Sopenharmony_ciunsigned int intc_get_prio_level(unsigned int irq);
16762306a36Sopenharmony_civoid intc_set_prio_level(unsigned int irq, unsigned int level);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci/* handle.c */
17062306a36Sopenharmony_ciunsigned int intc_get_mask_handle(struct intc_desc *desc,
17162306a36Sopenharmony_ci				  struct intc_desc_int *d,
17262306a36Sopenharmony_ci				  intc_enum enum_id, int do_grps);
17362306a36Sopenharmony_ciunsigned int intc_get_prio_handle(struct intc_desc *desc,
17462306a36Sopenharmony_ci				  struct intc_desc_int *d,
17562306a36Sopenharmony_ci				  intc_enum enum_id, int do_grps);
17662306a36Sopenharmony_ciunsigned int intc_get_sense_handle(struct intc_desc *desc,
17762306a36Sopenharmony_ci				   struct intc_desc_int *d,
17862306a36Sopenharmony_ci				   intc_enum enum_id);
17962306a36Sopenharmony_civoid intc_set_ack_handle(unsigned int irq, struct intc_desc *desc,
18062306a36Sopenharmony_ci			 struct intc_desc_int *d, intc_enum id);
18162306a36Sopenharmony_ciunsigned long intc_get_ack_handle(unsigned int irq);
18262306a36Sopenharmony_civoid intc_enable_disable_enum(struct intc_desc *desc, struct intc_desc_int *d,
18362306a36Sopenharmony_ci			      intc_enum enum_id, int enable);
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci/* irqdomain.c */
18662306a36Sopenharmony_civoid intc_irq_domain_init(struct intc_desc_int *d, struct intc_hw_desc *hw);
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci/* virq.c */
18962306a36Sopenharmony_civoid intc_subgroup_init(struct intc_desc *desc, struct intc_desc_int *d);
19062306a36Sopenharmony_civoid intc_irq_xlate_set(unsigned int irq, intc_enum id, struct intc_desc_int *d);
19162306a36Sopenharmony_cistruct intc_map_entry *intc_irq_xlate_get(unsigned int irq);
192