1 // SPDX-License-Identifier: GPL-2.0 2 3 #define pr_fmt(fmt) "irq-ls-extirq: " fmt 4 5 #include <linux/irq.h> 6 #include <linux/irqchip.h> 7 #include <linux/irqdomain.h> 8 #include <linux/of.h> 9 #include <linux/of_address.h> 10 #include <linux/slab.h> 11 12 #include <dt-bindings/interrupt-controller/arm-gic.h> 13 14 #define MAXIRQ 12 15 #define LS1021A_SCFGREVCR 0x200 16 17 struct ls_extirq_data { 18 void __iomem *intpcr; 19 raw_spinlock_t lock; 20 bool big_endian; 21 bool is_ls1021a_or_ls1043a; 22 u32 nirq; 23 struct irq_fwspec map[MAXIRQ]; 24 }; 25 26 static void ls_extirq_intpcr_rmw(struct ls_extirq_data *priv, u32 mask, 27 u32 value) 28 { 29 u32 intpcr; 30 31 /* 32 * Serialize concurrent calls to ls_extirq_set_type() from multiple 33 * IRQ descriptors, making sure the read-modify-write is atomic. 34 */ 35 raw_spin_lock(&priv->lock); 36 37 if (priv->big_endian) 38 intpcr = ioread32be(priv->intpcr); 39 else 40 intpcr = ioread32(priv->intpcr); 41 42 intpcr &= ~mask; 43 intpcr |= value; 44 45 if (priv->big_endian) 46 iowrite32be(intpcr, priv->intpcr); 47 else 48 iowrite32(intpcr, priv->intpcr); 49 50 raw_spin_unlock(&priv->lock); 51 } 52 53 static int 54 ls_extirq_set_type(struct irq_data *data, unsigned int type) 55 { 56 struct ls_extirq_data *priv = data->chip_data; 57 irq_hw_number_t hwirq = data->hwirq; 58 u32 value, mask; 59 60 if (priv->is_ls1021a_or_ls1043a) 61 mask = 1U << (31 - hwirq); 62 else 63 mask = 1U << hwirq; 64 65 switch (type) { 66 case IRQ_TYPE_LEVEL_LOW: 67 type = IRQ_TYPE_LEVEL_HIGH; 68 value = mask; 69 break; 70 case IRQ_TYPE_EDGE_FALLING: 71 type = IRQ_TYPE_EDGE_RISING; 72 value = mask; 73 break; 74 case IRQ_TYPE_LEVEL_HIGH: 75 case IRQ_TYPE_EDGE_RISING: 76 value = 0; 77 break; 78 default: 79 return -EINVAL; 80 } 81 82 ls_extirq_intpcr_rmw(priv, mask, value); 83 84 return irq_chip_set_type_parent(data, type); 85 } 86 87 static struct irq_chip ls_extirq_chip = { 88 .name = "ls-extirq", 89 .irq_mask = irq_chip_mask_parent, 90 .irq_unmask = irq_chip_unmask_parent, 91 .irq_eoi = irq_chip_eoi_parent, 92 .irq_set_type = ls_extirq_set_type, 93 .irq_retrigger = irq_chip_retrigger_hierarchy, 94 .irq_set_affinity = irq_chip_set_affinity_parent, 95 .flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_SKIP_SET_WAKE, 96 }; 97 98 static int 99 ls_extirq_domain_alloc(struct irq_domain *domain, unsigned int virq, 100 unsigned int nr_irqs, void *arg) 101 { 102 struct ls_extirq_data *priv = domain->host_data; 103 struct irq_fwspec *fwspec = arg; 104 irq_hw_number_t hwirq; 105 106 if (fwspec->param_count != 2) 107 return -EINVAL; 108 109 hwirq = fwspec->param[0]; 110 if (hwirq >= priv->nirq) 111 return -EINVAL; 112 113 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, &ls_extirq_chip, 114 priv); 115 116 return irq_domain_alloc_irqs_parent(domain, virq, 1, &priv->map[hwirq]); 117 } 118 119 static const struct irq_domain_ops extirq_domain_ops = { 120 .xlate = irq_domain_xlate_twocell, 121 .alloc = ls_extirq_domain_alloc, 122 .free = irq_domain_free_irqs_common, 123 }; 124 125 static int 126 ls_extirq_parse_map(struct ls_extirq_data *priv, struct device_node *node) 127 { 128 struct of_imap_parser imap_parser; 129 struct of_imap_item imap_item; 130 int ret; 131 132 ret = of_imap_parser_init(&imap_parser, node, &imap_item); 133 if (ret) 134 return ret; 135 136 for_each_of_imap_item(&imap_parser, &imap_item) { 137 struct device_node *ipar; 138 u32 hwirq; 139 int i; 140 141 hwirq = imap_item.child_imap[0]; 142 if (hwirq >= MAXIRQ) { 143 of_node_put(imap_item.parent_args.np); 144 return -EINVAL; 145 } 146 priv->nirq = max(priv->nirq, hwirq + 1); 147 148 ipar = of_node_get(imap_item.parent_args.np); 149 priv->map[hwirq].fwnode = of_fwnode_handle(ipar); 150 151 priv->map[hwirq].param_count = imap_item.parent_args.args_count; 152 for (i = 0; i < priv->map[hwirq].param_count; i++) 153 priv->map[hwirq].param[i] = imap_item.parent_args.args[i]; 154 } 155 return 0; 156 } 157 158 static int ls_extirq_probe(struct platform_device *pdev) 159 { 160 struct irq_domain *domain, *parent_domain; 161 struct device_node *node, *parent; 162 struct device *dev = &pdev->dev; 163 struct ls_extirq_data *priv; 164 int ret; 165 166 node = dev->of_node; 167 parent = of_irq_find_parent(node); 168 if (!parent) 169 return dev_err_probe(dev, -ENODEV, "Failed to get IRQ parent node\n"); 170 171 parent_domain = irq_find_host(parent); 172 if (!parent_domain) 173 return dev_err_probe(dev, -EPROBE_DEFER, "Cannot find parent domain\n"); 174 175 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 176 if (!priv) 177 return dev_err_probe(dev, -ENOMEM, "Failed to allocate memory\n"); 178 179 priv->intpcr = devm_of_iomap(dev, node, 0, NULL); 180 if (!priv->intpcr) 181 return dev_err_probe(dev, -ENOMEM, "Cannot ioremap OF node %pOF\n", node); 182 183 ret = ls_extirq_parse_map(priv, node); 184 if (ret) 185 return dev_err_probe(dev, ret, "Failed to parse IRQ map\n"); 186 187 priv->big_endian = of_device_is_big_endian(node->parent); 188 priv->is_ls1021a_or_ls1043a = of_device_is_compatible(node, "fsl,ls1021a-extirq") || 189 of_device_is_compatible(node, "fsl,ls1043a-extirq"); 190 raw_spin_lock_init(&priv->lock); 191 192 domain = irq_domain_create_hierarchy(parent_domain, 0, priv->nirq, of_fwnode_handle(node), 193 &extirq_domain_ops, priv); 194 if (!domain) 195 return dev_err_probe(dev, -ENOMEM, "Failed to add IRQ domain\n"); 196 197 return 0; 198 } 199 200 static const struct of_device_id ls_extirq_dt_ids[] = { 201 { .compatible = "fsl,ls1021a-extirq" }, 202 { .compatible = "fsl,ls1043a-extirq" }, 203 { .compatible = "fsl,ls1088a-extirq" }, 204 {} 205 }; 206 MODULE_DEVICE_TABLE(of, ls_extirq_dt_ids); 207 208 static struct platform_driver ls_extirq_driver = { 209 .probe = ls_extirq_probe, 210 .driver = { 211 .name = "ls-extirq", 212 .of_match_table = ls_extirq_dt_ids, 213 } 214 }; 215 216 builtin_platform_driver(ls_extirq_driver); 217