Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Texas Instruments' K3 Interrupt Router irqchip driver
0004  *
0005  * Copyright (C) 2018-2019 Texas Instruments Incorporated - https://www.ti.com/
0006  *  Lokesh Vutla <lokeshvutla@ti.com>
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  * struct ti_sci_intr_irq_domain - Structure representing a TISCI based
0022  *                 Interrupt Router IRQ domain.
0023  * @sci:    Pointer to TISCI handle
0024  * @out_irqs:   TISCI resource pointer representing INTR irqs.
0025  * @dev:    Struct device pointer.
0026  * @ti_sci_id:  TI-SCI device identifier
0027  * @type:   Specifies the trigger type supported by this Interrupt Router
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  * ti_sci_intr_irq_domain_translate() - Retrieve hwirq and type from
0049  *                  IRQ firmware specific handler.
0050  * @domain: Pointer to IRQ domain
0051  * @fwspec: Pointer to IRQ specific firmware structure
0052  * @hwirq:  IRQ number identified by hardware
0053  * @type:   IRQ type
0054  *
0055  * Return 0 if all went ok else appropriate error.
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  * ti_sci_intr_xlate_irq() - Translate hwirq to parent's hwirq.
0075  * @intr:   IRQ domain corresponding to Interrupt Router
0076  * @irq:    Hardware irq corresponding to the above irq domain
0077  *
0078  * Return parent irq number if translation is available else -ENOENT.
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  * ti_sci_intr_irq_domain_free() - Free the specified IRQs from the domain.
0104  * @domain: Domain to which the irqs belong
0105  * @virq:   Linux virtual IRQ to be freed.
0106  * @nr_irqs:    Number of continuous irqs to be freed
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  * ti_sci_intr_alloc_parent_irq() - Allocate parent IRQ
0128  * @domain: Pointer to the interrupt router IRQ domain
0129  * @virq:   Corresponding Linux virtual IRQ number
0130  * @hwirq:  Corresponding hwirq for the IRQ within this IRQ domain
0131  *
0132  * Returns intr output irq if all went well else appropriate error pointer.
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         /* Parent is GIC */
0156         fwspec.param_count = 3;
0157         fwspec.param[0] = 0;    /* SPI */
0158         fwspec.param[1] = p_hwirq - 32; /* SPI offset */
0159         fwspec.param[2] = intr->type;
0160     } else {
0161         /* Parent is Interrupt Router */
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  * ti_sci_intr_irq_domain_alloc() - Allocate Interrupt router IRQs
0187  * @domain: Point to the interrupt router IRQ domain
0188  * @virq:   Corresponding Linux virtual IRQ number
0189  * @nr_irqs:    Continuous irqs to be allocated
0190  * @data:   Pointer to firmware specifier
0191  *
0192  * Return 0 if all went well else appropriate error value.
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     { /* sentinel */ },
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");