0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/err.h>
0010 #include <linux/module.h>
0011 #include <linux/moduleparam.h>
0012 #include <linux/io.h>
0013 #include <linux/irqchip.h>
0014 #include <linux/irqdomain.h>
0015 #include <linux/of_platform.h>
0016 #include <linux/of_address.h>
0017 #include <linux/of_irq.h>
0018 #include <linux/soc/ti/ti_sci_protocol.h>
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 struct ti_sci_intr_irq_domain {
0030 const struct ti_sci_handle *sci;
0031 struct ti_sci_resource *out_irqs;
0032 struct device *dev;
0033 u32 ti_sci_id;
0034 u32 type;
0035 };
0036
0037 static struct irq_chip ti_sci_intr_irq_chip = {
0038 .name = "INTR",
0039 .irq_eoi = irq_chip_eoi_parent,
0040 .irq_mask = irq_chip_mask_parent,
0041 .irq_unmask = irq_chip_unmask_parent,
0042 .irq_set_type = irq_chip_set_type_parent,
0043 .irq_retrigger = irq_chip_retrigger_hierarchy,
0044 .irq_set_affinity = irq_chip_set_affinity_parent,
0045 };
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 static int ti_sci_intr_irq_domain_translate(struct irq_domain *domain,
0058 struct irq_fwspec *fwspec,
0059 unsigned long *hwirq,
0060 unsigned int *type)
0061 {
0062 struct ti_sci_intr_irq_domain *intr = domain->host_data;
0063
0064 if (fwspec->param_count != 1)
0065 return -EINVAL;
0066
0067 *hwirq = fwspec->param[0];
0068 *type = intr->type;
0069
0070 return 0;
0071 }
0072
0073
0074
0075
0076
0077
0078
0079
0080 static int ti_sci_intr_xlate_irq(struct ti_sci_intr_irq_domain *intr, u32 irq)
0081 {
0082 struct device_node *np = dev_of_node(intr->dev);
0083 u32 base, pbase, size, len;
0084 const __be32 *range;
0085
0086 range = of_get_property(np, "ti,interrupt-ranges", &len);
0087 if (!range)
0088 return irq;
0089
0090 for (len /= sizeof(*range); len >= 3; len -= 3) {
0091 base = be32_to_cpu(*range++);
0092 pbase = be32_to_cpu(*range++);
0093 size = be32_to_cpu(*range++);
0094
0095 if (base <= irq && irq < base + size)
0096 return irq - base + pbase;
0097 }
0098
0099 return -ENOENT;
0100 }
0101
0102
0103
0104
0105
0106
0107
0108 static void ti_sci_intr_irq_domain_free(struct irq_domain *domain,
0109 unsigned int virq, unsigned int nr_irqs)
0110 {
0111 struct ti_sci_intr_irq_domain *intr = domain->host_data;
0112 struct irq_data *data;
0113 int out_irq;
0114
0115 data = irq_domain_get_irq_data(domain, virq);
0116 out_irq = (uintptr_t)data->chip_data;
0117
0118 intr->sci->ops.rm_irq_ops.free_irq(intr->sci,
0119 intr->ti_sci_id, data->hwirq,
0120 intr->ti_sci_id, out_irq);
0121 ti_sci_release_resource(intr->out_irqs, out_irq);
0122 irq_domain_free_irqs_parent(domain, virq, 1);
0123 irq_domain_reset_irq_data(data);
0124 }
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134 static int ti_sci_intr_alloc_parent_irq(struct irq_domain *domain,
0135 unsigned int virq, u32 hwirq)
0136 {
0137 struct ti_sci_intr_irq_domain *intr = domain->host_data;
0138 struct device_node *parent_node;
0139 struct irq_fwspec fwspec;
0140 int p_hwirq, err = 0;
0141 u16 out_irq;
0142
0143 out_irq = ti_sci_get_free_resource(intr->out_irqs);
0144 if (out_irq == TI_SCI_RESOURCE_NULL)
0145 return -EINVAL;
0146
0147 p_hwirq = ti_sci_intr_xlate_irq(intr, out_irq);
0148 if (p_hwirq < 0)
0149 goto err_irqs;
0150
0151 parent_node = of_irq_find_parent(dev_of_node(intr->dev));
0152 fwspec.fwnode = of_node_to_fwnode(parent_node);
0153
0154 if (of_device_is_compatible(parent_node, "arm,gic-v3")) {
0155
0156 fwspec.param_count = 3;
0157 fwspec.param[0] = 0;
0158 fwspec.param[1] = p_hwirq - 32;
0159 fwspec.param[2] = intr->type;
0160 } else {
0161
0162 fwspec.param_count = 1;
0163 fwspec.param[0] = p_hwirq;
0164 }
0165
0166 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
0167 if (err)
0168 goto err_irqs;
0169
0170 err = intr->sci->ops.rm_irq_ops.set_irq(intr->sci,
0171 intr->ti_sci_id, hwirq,
0172 intr->ti_sci_id, out_irq);
0173 if (err)
0174 goto err_msg;
0175
0176 return out_irq;
0177
0178 err_msg:
0179 irq_domain_free_irqs_parent(domain, virq, 1);
0180 err_irqs:
0181 ti_sci_release_resource(intr->out_irqs, out_irq);
0182 return err;
0183 }
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194 static int ti_sci_intr_irq_domain_alloc(struct irq_domain *domain,
0195 unsigned int virq, unsigned int nr_irqs,
0196 void *data)
0197 {
0198 struct irq_fwspec *fwspec = data;
0199 unsigned long hwirq;
0200 unsigned int flags;
0201 int err, out_irq;
0202
0203 err = ti_sci_intr_irq_domain_translate(domain, fwspec, &hwirq, &flags);
0204 if (err)
0205 return err;
0206
0207 out_irq = ti_sci_intr_alloc_parent_irq(domain, virq, hwirq);
0208 if (out_irq < 0)
0209 return out_irq;
0210
0211 irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
0212 &ti_sci_intr_irq_chip,
0213 (void *)(uintptr_t)out_irq);
0214
0215 return 0;
0216 }
0217
0218 static const struct irq_domain_ops ti_sci_intr_irq_domain_ops = {
0219 .free = ti_sci_intr_irq_domain_free,
0220 .alloc = ti_sci_intr_irq_domain_alloc,
0221 .translate = ti_sci_intr_irq_domain_translate,
0222 };
0223
0224 static int ti_sci_intr_irq_domain_probe(struct platform_device *pdev)
0225 {
0226 struct irq_domain *parent_domain, *domain;
0227 struct ti_sci_intr_irq_domain *intr;
0228 struct device_node *parent_node;
0229 struct device *dev = &pdev->dev;
0230 int ret;
0231
0232 parent_node = of_irq_find_parent(dev_of_node(dev));
0233 if (!parent_node) {
0234 dev_err(dev, "Failed to get IRQ parent node\n");
0235 return -ENODEV;
0236 }
0237
0238 parent_domain = irq_find_host(parent_node);
0239 if (!parent_domain) {
0240 dev_err(dev, "Failed to find IRQ parent domain\n");
0241 return -ENODEV;
0242 }
0243
0244 intr = devm_kzalloc(dev, sizeof(*intr), GFP_KERNEL);
0245 if (!intr)
0246 return -ENOMEM;
0247
0248 intr->dev = dev;
0249 ret = of_property_read_u32(dev_of_node(dev), "ti,intr-trigger-type",
0250 &intr->type);
0251 if (ret) {
0252 dev_err(dev, "missing ti,intr-trigger-type property\n");
0253 return -EINVAL;
0254 }
0255
0256 intr->sci = devm_ti_sci_get_by_phandle(dev, "ti,sci");
0257 if (IS_ERR(intr->sci))
0258 return dev_err_probe(dev, PTR_ERR(intr->sci),
0259 "ti,sci read fail\n");
0260
0261 ret = of_property_read_u32(dev_of_node(dev), "ti,sci-dev-id",
0262 &intr->ti_sci_id);
0263 if (ret) {
0264 dev_err(dev, "missing 'ti,sci-dev-id' property\n");
0265 return -EINVAL;
0266 }
0267
0268 intr->out_irqs = devm_ti_sci_get_resource(intr->sci, dev,
0269 intr->ti_sci_id,
0270 TI_SCI_RESASG_SUBTYPE_IR_OUTPUT);
0271 if (IS_ERR(intr->out_irqs)) {
0272 dev_err(dev, "Destination irq resource allocation failed\n");
0273 return PTR_ERR(intr->out_irqs);
0274 }
0275
0276 domain = irq_domain_add_hierarchy(parent_domain, 0, 0, dev_of_node(dev),
0277 &ti_sci_intr_irq_domain_ops, intr);
0278 if (!domain) {
0279 dev_err(dev, "Failed to allocate IRQ domain\n");
0280 return -ENOMEM;
0281 }
0282
0283 dev_info(dev, "Interrupt Router %d domain created\n", intr->ti_sci_id);
0284
0285 return 0;
0286 }
0287
0288 static const struct of_device_id ti_sci_intr_irq_domain_of_match[] = {
0289 { .compatible = "ti,sci-intr", },
0290 { },
0291 };
0292 MODULE_DEVICE_TABLE(of, ti_sci_intr_irq_domain_of_match);
0293
0294 static struct platform_driver ti_sci_intr_irq_domain_driver = {
0295 .probe = ti_sci_intr_irq_domain_probe,
0296 .driver = {
0297 .name = "ti-sci-intr",
0298 .of_match_table = ti_sci_intr_irq_domain_of_match,
0299 },
0300 };
0301 module_platform_driver(ti_sci_intr_irq_domain_driver);
0302
0303 MODULE_AUTHOR("Lokesh Vutla <lokeshvutla@ticom>");
0304 MODULE_DESCRIPTION("K3 Interrupt Router driver over TI SCI protocol");
0305 MODULE_LICENSE("GPL v2");