18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * JZ47xx SoCs TCU IRQ driver
48c2ecf20Sopenharmony_ci * Copyright (C) 2019 Paul Cercueil <paul@crapouillou.net>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
98c2ecf20Sopenharmony_ci#include <linux/irqchip.h>
108c2ecf20Sopenharmony_ci#include <linux/irqchip/chained_irq.h>
118c2ecf20Sopenharmony_ci#include <linux/mfd/ingenic-tcu.h>
128c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
138c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
148c2ecf20Sopenharmony_ci#include <linux/regmap.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistruct ingenic_tcu {
178c2ecf20Sopenharmony_ci	struct regmap *map;
188c2ecf20Sopenharmony_ci	struct clk *clk;
198c2ecf20Sopenharmony_ci	struct irq_domain *domain;
208c2ecf20Sopenharmony_ci	unsigned int nb_parent_irqs;
218c2ecf20Sopenharmony_ci	u32 parent_irqs[3];
228c2ecf20Sopenharmony_ci};
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic void ingenic_tcu_intc_cascade(struct irq_desc *desc)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	struct irq_chip *irq_chip = irq_data_get_irq_chip(&desc->irq_data);
278c2ecf20Sopenharmony_ci	struct irq_domain *domain = irq_desc_get_handler_data(desc);
288c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_get_domain_generic_chip(domain, 0);
298c2ecf20Sopenharmony_ci	struct regmap *map = gc->private;
308c2ecf20Sopenharmony_ci	uint32_t irq_reg, irq_mask;
318c2ecf20Sopenharmony_ci	unsigned int i;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	regmap_read(map, TCU_REG_TFR, &irq_reg);
348c2ecf20Sopenharmony_ci	regmap_read(map, TCU_REG_TMR, &irq_mask);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	chained_irq_enter(irq_chip, desc);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	irq_reg &= ~irq_mask;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	for_each_set_bit(i, (unsigned long *)&irq_reg, 32)
418c2ecf20Sopenharmony_ci		generic_handle_irq(irq_linear_revmap(domain, i));
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	chained_irq_exit(irq_chip, desc);
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic void ingenic_tcu_gc_unmask_enable_reg(struct irq_data *d)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
498c2ecf20Sopenharmony_ci	struct irq_chip_type *ct = irq_data_get_chip_type(d);
508c2ecf20Sopenharmony_ci	struct regmap *map = gc->private;
518c2ecf20Sopenharmony_ci	u32 mask = d->mask;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	irq_gc_lock(gc);
548c2ecf20Sopenharmony_ci	regmap_write(map, ct->regs.ack, mask);
558c2ecf20Sopenharmony_ci	regmap_write(map, ct->regs.enable, mask);
568c2ecf20Sopenharmony_ci	*ct->mask_cache |= mask;
578c2ecf20Sopenharmony_ci	irq_gc_unlock(gc);
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic void ingenic_tcu_gc_mask_disable_reg(struct irq_data *d)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
638c2ecf20Sopenharmony_ci	struct irq_chip_type *ct = irq_data_get_chip_type(d);
648c2ecf20Sopenharmony_ci	struct regmap *map = gc->private;
658c2ecf20Sopenharmony_ci	u32 mask = d->mask;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	irq_gc_lock(gc);
688c2ecf20Sopenharmony_ci	regmap_write(map, ct->regs.disable, mask);
698c2ecf20Sopenharmony_ci	*ct->mask_cache &= ~mask;
708c2ecf20Sopenharmony_ci	irq_gc_unlock(gc);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic void ingenic_tcu_gc_mask_disable_reg_and_ack(struct irq_data *d)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
768c2ecf20Sopenharmony_ci	struct irq_chip_type *ct = irq_data_get_chip_type(d);
778c2ecf20Sopenharmony_ci	struct regmap *map = gc->private;
788c2ecf20Sopenharmony_ci	u32 mask = d->mask;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	irq_gc_lock(gc);
818c2ecf20Sopenharmony_ci	regmap_write(map, ct->regs.ack, mask);
828c2ecf20Sopenharmony_ci	regmap_write(map, ct->regs.disable, mask);
838c2ecf20Sopenharmony_ci	irq_gc_unlock(gc);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int __init ingenic_tcu_irq_init(struct device_node *np,
878c2ecf20Sopenharmony_ci				       struct device_node *parent)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc;
908c2ecf20Sopenharmony_ci	struct irq_chip_type *ct;
918c2ecf20Sopenharmony_ci	struct ingenic_tcu *tcu;
928c2ecf20Sopenharmony_ci	struct regmap *map;
938c2ecf20Sopenharmony_ci	unsigned int i;
948c2ecf20Sopenharmony_ci	int ret, irqs;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	map = device_node_to_regmap(np);
978c2ecf20Sopenharmony_ci	if (IS_ERR(map))
988c2ecf20Sopenharmony_ci		return PTR_ERR(map);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	tcu = kzalloc(sizeof(*tcu), GFP_KERNEL);
1018c2ecf20Sopenharmony_ci	if (!tcu)
1028c2ecf20Sopenharmony_ci		return -ENOMEM;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	tcu->map = map;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	irqs = of_property_count_elems_of_size(np, "interrupts", sizeof(u32));
1078c2ecf20Sopenharmony_ci	if (irqs < 0 || irqs > ARRAY_SIZE(tcu->parent_irqs)) {
1088c2ecf20Sopenharmony_ci		pr_crit("%s: Invalid 'interrupts' property\n", __func__);
1098c2ecf20Sopenharmony_ci		ret = -EINVAL;
1108c2ecf20Sopenharmony_ci		goto err_free_tcu;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	tcu->nb_parent_irqs = irqs;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	tcu->domain = irq_domain_add_linear(np, 32, &irq_generic_chip_ops,
1168c2ecf20Sopenharmony_ci					    NULL);
1178c2ecf20Sopenharmony_ci	if (!tcu->domain) {
1188c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1198c2ecf20Sopenharmony_ci		goto err_free_tcu;
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	ret = irq_alloc_domain_generic_chips(tcu->domain, 32, 1, "TCU",
1238c2ecf20Sopenharmony_ci					     handle_level_irq, 0,
1248c2ecf20Sopenharmony_ci					     IRQ_NOPROBE | IRQ_LEVEL, 0);
1258c2ecf20Sopenharmony_ci	if (ret) {
1268c2ecf20Sopenharmony_ci		pr_crit("%s: Invalid 'interrupts' property\n", __func__);
1278c2ecf20Sopenharmony_ci		goto out_domain_remove;
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	gc = irq_get_domain_generic_chip(tcu->domain, 0);
1318c2ecf20Sopenharmony_ci	ct = gc->chip_types;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	gc->wake_enabled = IRQ_MSK(32);
1348c2ecf20Sopenharmony_ci	gc->private = tcu->map;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	ct->regs.disable = TCU_REG_TMSR;
1378c2ecf20Sopenharmony_ci	ct->regs.enable = TCU_REG_TMCR;
1388c2ecf20Sopenharmony_ci	ct->regs.ack = TCU_REG_TFCR;
1398c2ecf20Sopenharmony_ci	ct->chip.irq_unmask = ingenic_tcu_gc_unmask_enable_reg;
1408c2ecf20Sopenharmony_ci	ct->chip.irq_mask = ingenic_tcu_gc_mask_disable_reg;
1418c2ecf20Sopenharmony_ci	ct->chip.irq_mask_ack = ingenic_tcu_gc_mask_disable_reg_and_ack;
1428c2ecf20Sopenharmony_ci	ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* Mask all IRQs by default */
1458c2ecf20Sopenharmony_ci	regmap_write(tcu->map, TCU_REG_TMSR, IRQ_MSK(32));
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/*
1488c2ecf20Sopenharmony_ci	 * On JZ4740, timer 0 and timer 1 have their own interrupt line;
1498c2ecf20Sopenharmony_ci	 * timers 2-7 share one interrupt.
1508c2ecf20Sopenharmony_ci	 * On SoCs >= JZ4770, timer 5 has its own interrupt line;
1518c2ecf20Sopenharmony_ci	 * timers 0-4 and 6-7 share one single interrupt.
1528c2ecf20Sopenharmony_ci	 *
1538c2ecf20Sopenharmony_ci	 * To keep things simple, we just register the same handler to
1548c2ecf20Sopenharmony_ci	 * all parent interrupts. The handler will properly detect which
1558c2ecf20Sopenharmony_ci	 * channel fired the interrupt.
1568c2ecf20Sopenharmony_ci	 */
1578c2ecf20Sopenharmony_ci	for (i = 0; i < irqs; i++) {
1588c2ecf20Sopenharmony_ci		tcu->parent_irqs[i] = irq_of_parse_and_map(np, i);
1598c2ecf20Sopenharmony_ci		if (!tcu->parent_irqs[i]) {
1608c2ecf20Sopenharmony_ci			ret = -EINVAL;
1618c2ecf20Sopenharmony_ci			goto out_unmap_irqs;
1628c2ecf20Sopenharmony_ci		}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		irq_set_chained_handler_and_data(tcu->parent_irqs[i],
1658c2ecf20Sopenharmony_ci						 ingenic_tcu_intc_cascade,
1668c2ecf20Sopenharmony_ci						 tcu->domain);
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return 0;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ciout_unmap_irqs:
1728c2ecf20Sopenharmony_ci	for (; i > 0; i--)
1738c2ecf20Sopenharmony_ci		irq_dispose_mapping(tcu->parent_irqs[i - 1]);
1748c2ecf20Sopenharmony_ciout_domain_remove:
1758c2ecf20Sopenharmony_ci	irq_domain_remove(tcu->domain);
1768c2ecf20Sopenharmony_cierr_free_tcu:
1778c2ecf20Sopenharmony_ci	kfree(tcu);
1788c2ecf20Sopenharmony_ci	return ret;
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(jz4740_tcu_irq, "ingenic,jz4740-tcu", ingenic_tcu_irq_init);
1818c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(jz4725b_tcu_irq, "ingenic,jz4725b-tcu", ingenic_tcu_irq_init);
1828c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(jz4760_tcu_irq, "ingenic,jz4760-tcu", ingenic_tcu_irq_init);
1838c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(jz4770_tcu_irq, "ingenic,jz4770-tcu", ingenic_tcu_irq_init);
1848c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(x1000_tcu_irq, "ingenic,x1000-tcu", ingenic_tcu_irq_init);
185