1717c3dbcSMa Jun /* 2717c3dbcSMa Jun * Copyright (C) 2015 Hisilicon Limited, All Rights Reserved. 3717c3dbcSMa Jun * Author: Jun Ma <majun258@huawei.com> 4717c3dbcSMa Jun * Author: Yun Wu <wuyun.wu@huawei.com> 5717c3dbcSMa Jun * 6717c3dbcSMa Jun * This program is free software; you can redistribute it and/or modify 7717c3dbcSMa Jun * it under the terms of the GNU General Public License version 2 as 8717c3dbcSMa Jun * published by the Free Software Foundation. 9717c3dbcSMa Jun * 10717c3dbcSMa Jun * This program is distributed in the hope that it will be useful, 11717c3dbcSMa Jun * but WITHOUT ANY WARRANTY; without even the implied warranty of 12717c3dbcSMa Jun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13717c3dbcSMa Jun * GNU General Public License for more details. 14717c3dbcSMa Jun * 15717c3dbcSMa Jun * You should have received a copy of the GNU General Public License 16717c3dbcSMa Jun * along with this program. If not, see <http://www.gnu.org/licenses/>. 17717c3dbcSMa Jun */ 18717c3dbcSMa Jun 19f907c515SHanjun Guo #include <linux/acpi.h> 209650c60eSMa Jun #include <linux/interrupt.h> 219650c60eSMa Jun #include <linux/irqchip.h> 22717c3dbcSMa Jun #include <linux/module.h> 239650c60eSMa Jun #include <linux/msi.h> 24717c3dbcSMa Jun #include <linux/of_address.h> 25717c3dbcSMa Jun #include <linux/of_irq.h> 26717c3dbcSMa Jun #include <linux/of_platform.h> 27717c3dbcSMa Jun #include <linux/platform_device.h> 28717c3dbcSMa Jun #include <linux/slab.h> 29717c3dbcSMa Jun 309650c60eSMa Jun /* Interrupt numbers per mbigen node supported */ 319650c60eSMa Jun #define IRQS_PER_MBIGEN_NODE 128 329650c60eSMa Jun 339650c60eSMa Jun /* 64 irqs (Pin0-pin63) are reserved for each mbigen chip */ 349650c60eSMa Jun #define RESERVED_IRQ_PER_MBIGEN_CHIP 64 359650c60eSMa Jun 369650c60eSMa Jun /* The maximum IRQ pin number of mbigen chip(start from 0) */ 379650c60eSMa Jun #define MAXIMUM_IRQ_PIN_NUM 1407 389650c60eSMa Jun 399650c60eSMa Jun /** 409650c60eSMa Jun * In mbigen vector register 419650c60eSMa Jun * bit[21:12]: event id value 429650c60eSMa Jun * bit[11:0]: device id 439650c60eSMa Jun */ 449650c60eSMa Jun #define IRQ_EVENT_ID_SHIFT 12 459650c60eSMa Jun #define IRQ_EVENT_ID_MASK 0x3ff 469650c60eSMa Jun 479650c60eSMa Jun /* register range of each mbigen node */ 489650c60eSMa Jun #define MBIGEN_NODE_OFFSET 0x1000 499650c60eSMa Jun 509650c60eSMa Jun /* offset of vector register in mbigen node */ 519650c60eSMa Jun #define REG_MBIGEN_VEC_OFFSET 0x200 529650c60eSMa Jun 53717c3dbcSMa Jun /** 54a6c2f87bSMa Jun * offset of clear register in mbigen node 55a6c2f87bSMa Jun * This register is used to clear the status 56a6c2f87bSMa Jun * of interrupt 57a6c2f87bSMa Jun */ 58a6c2f87bSMa Jun #define REG_MBIGEN_CLEAR_OFFSET 0xa000 59a6c2f87bSMa Jun 60a6c2f87bSMa Jun /** 61a6c2f87bSMa Jun * offset of interrupt type register 62a6c2f87bSMa Jun * This register is used to configure interrupt 63a6c2f87bSMa Jun * trigger type 64a6c2f87bSMa Jun */ 65a6c2f87bSMa Jun #define REG_MBIGEN_TYPE_OFFSET 0x0 66a6c2f87bSMa Jun 67a6c2f87bSMa Jun /** 68717c3dbcSMa Jun * struct mbigen_device - holds the information of mbigen device. 69717c3dbcSMa Jun * 70717c3dbcSMa Jun * @pdev: pointer to the platform device structure of mbigen chip. 71717c3dbcSMa Jun * @base: mapped address of this mbigen chip. 72717c3dbcSMa Jun */ 73717c3dbcSMa Jun struct mbigen_device { 74717c3dbcSMa Jun struct platform_device *pdev; 75717c3dbcSMa Jun void __iomem *base; 76717c3dbcSMa Jun }; 77717c3dbcSMa Jun 789650c60eSMa Jun static inline unsigned int get_mbigen_vec_reg(irq_hw_number_t hwirq) 799650c60eSMa Jun { 809650c60eSMa Jun unsigned int nid, pin; 819650c60eSMa Jun 829650c60eSMa Jun hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP; 839650c60eSMa Jun nid = hwirq / IRQS_PER_MBIGEN_NODE + 1; 849650c60eSMa Jun pin = hwirq % IRQS_PER_MBIGEN_NODE; 859650c60eSMa Jun 869650c60eSMa Jun return pin * 4 + nid * MBIGEN_NODE_OFFSET 879650c60eSMa Jun + REG_MBIGEN_VEC_OFFSET; 889650c60eSMa Jun } 899650c60eSMa Jun 90a6c2f87bSMa Jun static inline void get_mbigen_type_reg(irq_hw_number_t hwirq, 91a6c2f87bSMa Jun u32 *mask, u32 *addr) 92a6c2f87bSMa Jun { 93a6c2f87bSMa Jun unsigned int nid, irq_ofst, ofst; 94a6c2f87bSMa Jun 95a6c2f87bSMa Jun hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP; 96a6c2f87bSMa Jun nid = hwirq / IRQS_PER_MBIGEN_NODE + 1; 97a6c2f87bSMa Jun irq_ofst = hwirq % IRQS_PER_MBIGEN_NODE; 98a6c2f87bSMa Jun 99a6c2f87bSMa Jun *mask = 1 << (irq_ofst % 32); 100a6c2f87bSMa Jun ofst = irq_ofst / 32 * 4; 101a6c2f87bSMa Jun 102a6c2f87bSMa Jun *addr = ofst + nid * MBIGEN_NODE_OFFSET 103a6c2f87bSMa Jun + REG_MBIGEN_TYPE_OFFSET; 104a6c2f87bSMa Jun } 105a6c2f87bSMa Jun 106a6c2f87bSMa Jun static inline void get_mbigen_clear_reg(irq_hw_number_t hwirq, 107a6c2f87bSMa Jun u32 *mask, u32 *addr) 108a6c2f87bSMa Jun { 109a6c2f87bSMa Jun unsigned int ofst; 110a6c2f87bSMa Jun 111a6c2f87bSMa Jun hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP; 112a6c2f87bSMa Jun ofst = hwirq / 32 * 4; 113a6c2f87bSMa Jun 114a6c2f87bSMa Jun *mask = 1 << (hwirq % 32); 115a6c2f87bSMa Jun *addr = ofst + REG_MBIGEN_CLEAR_OFFSET; 116a6c2f87bSMa Jun } 117a6c2f87bSMa Jun 118a6c2f87bSMa Jun static void mbigen_eoi_irq(struct irq_data *data) 119a6c2f87bSMa Jun { 120a6c2f87bSMa Jun void __iomem *base = data->chip_data; 121a6c2f87bSMa Jun u32 mask, addr; 122a6c2f87bSMa Jun 123a6c2f87bSMa Jun get_mbigen_clear_reg(data->hwirq, &mask, &addr); 124a6c2f87bSMa Jun 125a6c2f87bSMa Jun writel_relaxed(mask, base + addr); 126a6c2f87bSMa Jun 127a6c2f87bSMa Jun irq_chip_eoi_parent(data); 128a6c2f87bSMa Jun } 129a6c2f87bSMa Jun 130a6c2f87bSMa Jun static int mbigen_set_type(struct irq_data *data, unsigned int type) 131a6c2f87bSMa Jun { 132a6c2f87bSMa Jun void __iomem *base = data->chip_data; 133a6c2f87bSMa Jun u32 mask, addr, val; 134a6c2f87bSMa Jun 135a6c2f87bSMa Jun if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING) 136a6c2f87bSMa Jun return -EINVAL; 137a6c2f87bSMa Jun 138a6c2f87bSMa Jun get_mbigen_type_reg(data->hwirq, &mask, &addr); 139a6c2f87bSMa Jun 140a6c2f87bSMa Jun val = readl_relaxed(base + addr); 141a6c2f87bSMa Jun 142a6c2f87bSMa Jun if (type == IRQ_TYPE_LEVEL_HIGH) 143a6c2f87bSMa Jun val |= mask; 144a6c2f87bSMa Jun else 145a6c2f87bSMa Jun val &= ~mask; 146a6c2f87bSMa Jun 147a6c2f87bSMa Jun writel_relaxed(val, base + addr); 148a6c2f87bSMa Jun 149a6c2f87bSMa Jun return 0; 150a6c2f87bSMa Jun } 151a6c2f87bSMa Jun 1529650c60eSMa Jun static struct irq_chip mbigen_irq_chip = { 1539650c60eSMa Jun .name = "mbigen-v2", 154a6c2f87bSMa Jun .irq_mask = irq_chip_mask_parent, 155a6c2f87bSMa Jun .irq_unmask = irq_chip_unmask_parent, 156a6c2f87bSMa Jun .irq_eoi = mbigen_eoi_irq, 157a6c2f87bSMa Jun .irq_set_type = mbigen_set_type, 158a6c2f87bSMa Jun .irq_set_affinity = irq_chip_set_affinity_parent, 1599650c60eSMa Jun }; 1609650c60eSMa Jun 1619650c60eSMa Jun static void mbigen_write_msg(struct msi_desc *desc, struct msi_msg *msg) 1629650c60eSMa Jun { 1639650c60eSMa Jun struct irq_data *d = irq_get_irq_data(desc->irq); 1649650c60eSMa Jun void __iomem *base = d->chip_data; 1659650c60eSMa Jun u32 val; 1669650c60eSMa Jun 1679650c60eSMa Jun base += get_mbigen_vec_reg(d->hwirq); 1689650c60eSMa Jun val = readl_relaxed(base); 1699650c60eSMa Jun 1709650c60eSMa Jun val &= ~(IRQ_EVENT_ID_MASK << IRQ_EVENT_ID_SHIFT); 1719650c60eSMa Jun val |= (msg->data << IRQ_EVENT_ID_SHIFT); 1729650c60eSMa Jun 1739650c60eSMa Jun /* The address of doorbell is encoded in mbigen register by default 1749650c60eSMa Jun * So,we don't need to program the doorbell address at here 1759650c60eSMa Jun */ 1769650c60eSMa Jun writel_relaxed(val, base); 1779650c60eSMa Jun } 1789650c60eSMa Jun 1799650c60eSMa Jun static int mbigen_domain_translate(struct irq_domain *d, 1809650c60eSMa Jun struct irq_fwspec *fwspec, 1819650c60eSMa Jun unsigned long *hwirq, 1829650c60eSMa Jun unsigned int *type) 1839650c60eSMa Jun { 184f907c515SHanjun Guo if (is_of_node(fwspec->fwnode) || is_acpi_device_node(fwspec->fwnode)) { 1859650c60eSMa Jun if (fwspec->param_count != 2) 1869650c60eSMa Jun return -EINVAL; 1879650c60eSMa Jun 1889650c60eSMa Jun if ((fwspec->param[0] > MAXIMUM_IRQ_PIN_NUM) || 1899650c60eSMa Jun (fwspec->param[0] < RESERVED_IRQ_PER_MBIGEN_CHIP)) 1909650c60eSMa Jun return -EINVAL; 1919650c60eSMa Jun else 1929650c60eSMa Jun *hwirq = fwspec->param[0]; 1939650c60eSMa Jun 1949650c60eSMa Jun /* If there is no valid irq type, just use the default type */ 1959650c60eSMa Jun if ((fwspec->param[1] == IRQ_TYPE_EDGE_RISING) || 1969650c60eSMa Jun (fwspec->param[1] == IRQ_TYPE_LEVEL_HIGH)) 1979650c60eSMa Jun *type = fwspec->param[1]; 1989650c60eSMa Jun else 1999650c60eSMa Jun return -EINVAL; 2009650c60eSMa Jun 2019650c60eSMa Jun return 0; 2029650c60eSMa Jun } 2039650c60eSMa Jun return -EINVAL; 2049650c60eSMa Jun } 2059650c60eSMa Jun 2069650c60eSMa Jun static int mbigen_irq_domain_alloc(struct irq_domain *domain, 2079650c60eSMa Jun unsigned int virq, 2089650c60eSMa Jun unsigned int nr_irqs, 2099650c60eSMa Jun void *args) 2109650c60eSMa Jun { 2119650c60eSMa Jun struct irq_fwspec *fwspec = args; 2129650c60eSMa Jun irq_hw_number_t hwirq; 2139650c60eSMa Jun unsigned int type; 2149650c60eSMa Jun struct mbigen_device *mgn_chip; 2159650c60eSMa Jun int i, err; 2169650c60eSMa Jun 2179650c60eSMa Jun err = mbigen_domain_translate(domain, fwspec, &hwirq, &type); 2189650c60eSMa Jun if (err) 2199650c60eSMa Jun return err; 2209650c60eSMa Jun 2219650c60eSMa Jun err = platform_msi_domain_alloc(domain, virq, nr_irqs); 2229650c60eSMa Jun if (err) 2239650c60eSMa Jun return err; 2249650c60eSMa Jun 2259650c60eSMa Jun mgn_chip = platform_msi_get_host_data(domain); 2269650c60eSMa Jun 2279650c60eSMa Jun for (i = 0; i < nr_irqs; i++) 2289650c60eSMa Jun irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i, 2299650c60eSMa Jun &mbigen_irq_chip, mgn_chip->base); 2309650c60eSMa Jun 2319650c60eSMa Jun return 0; 2329650c60eSMa Jun } 2339650c60eSMa Jun 2349650c60eSMa Jun static struct irq_domain_ops mbigen_domain_ops = { 2359650c60eSMa Jun .translate = mbigen_domain_translate, 2369650c60eSMa Jun .alloc = mbigen_irq_domain_alloc, 2379650c60eSMa Jun .free = irq_domain_free_irqs_common, 2389650c60eSMa Jun }; 2399650c60eSMa Jun 24076e1f77fSKefeng Wang static int mbigen_of_create_domain(struct platform_device *pdev, 24176e1f77fSKefeng Wang struct mbigen_device *mgn_chip) 242717c3dbcSMa Jun { 24376e1f77fSKefeng Wang struct device *parent; 244ed2a1002SMaJun struct platform_device *child; 2459650c60eSMa Jun struct irq_domain *domain; 246ed2a1002SMaJun struct device_node *np; 2479650c60eSMa Jun u32 num_pins; 248717c3dbcSMa Jun 249ed2a1002SMaJun for_each_child_of_node(pdev->dev.of_node, np) { 250ed2a1002SMaJun if (!of_property_read_bool(np, "interrupt-controller")) 251ed2a1002SMaJun continue; 252ed2a1002SMaJun 253ed2a1002SMaJun parent = platform_bus_type.dev_root; 254ed2a1002SMaJun child = of_platform_device_create(np, NULL, parent); 255086eec2dSDan Carpenter if (!child) 256086eec2dSDan Carpenter return -ENOMEM; 257ed2a1002SMaJun 258ed2a1002SMaJun if (of_property_read_u32(child->dev.of_node, "num-pins", 259ed2a1002SMaJun &num_pins) < 0) { 2609650c60eSMa Jun dev_err(&pdev->dev, "No num-pins property\n"); 2619650c60eSMa Jun return -EINVAL; 2629650c60eSMa Jun } 2639650c60eSMa Jun 264ed2a1002SMaJun domain = platform_msi_create_device_domain(&child->dev, num_pins, 2659650c60eSMa Jun mbigen_write_msg, 2669650c60eSMa Jun &mbigen_domain_ops, 2679650c60eSMa Jun mgn_chip); 2689650c60eSMa Jun if (!domain) 2699650c60eSMa Jun return -ENOMEM; 270ed2a1002SMaJun } 2719650c60eSMa Jun 27276e1f77fSKefeng Wang return 0; 27376e1f77fSKefeng Wang } 27476e1f77fSKefeng Wang 275f907c515SHanjun Guo #ifdef CONFIG_ACPI 276f907c515SHanjun Guo static int mbigen_acpi_create_domain(struct platform_device *pdev, 277f907c515SHanjun Guo struct mbigen_device *mgn_chip) 278f907c515SHanjun Guo { 279f907c515SHanjun Guo struct irq_domain *domain; 280f907c515SHanjun Guo u32 num_pins = 0; 281f907c515SHanjun Guo int ret; 282f907c515SHanjun Guo 283f907c515SHanjun Guo /* 284f907c515SHanjun Guo * "num-pins" is the total number of interrupt pins implemented in 285f907c515SHanjun Guo * this mbigen instance, and mbigen is an interrupt controller 286f907c515SHanjun Guo * connected to ITS converting wired interrupts into MSI, so we 287f907c515SHanjun Guo * use "num-pins" to alloc MSI vectors which are needed by client 288f907c515SHanjun Guo * devices connected to it. 289f907c515SHanjun Guo * 290f907c515SHanjun Guo * Here is the DSDT device node used for mbigen in firmware: 291f907c515SHanjun Guo * Device(MBI0) { 292f907c515SHanjun Guo * Name(_HID, "HISI0152") 293f907c515SHanjun Guo * Name(_UID, Zero) 294f907c515SHanjun Guo * Name(_CRS, ResourceTemplate() { 295f907c515SHanjun Guo * Memory32Fixed(ReadWrite, 0xa0080000, 0x10000) 296f907c515SHanjun Guo * }) 297f907c515SHanjun Guo * 298f907c515SHanjun Guo * Name(_DSD, Package () { 299f907c515SHanjun Guo * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), 300f907c515SHanjun Guo * Package () { 301f907c515SHanjun Guo * Package () {"num-pins", 378} 302f907c515SHanjun Guo * } 303f907c515SHanjun Guo * }) 304f907c515SHanjun Guo * } 305f907c515SHanjun Guo */ 306f907c515SHanjun Guo ret = device_property_read_u32(&pdev->dev, "num-pins", &num_pins); 307f907c515SHanjun Guo if (ret || num_pins == 0) 308f907c515SHanjun Guo return -EINVAL; 309f907c515SHanjun Guo 310f907c515SHanjun Guo domain = platform_msi_create_device_domain(&pdev->dev, num_pins, 311f907c515SHanjun Guo mbigen_write_msg, 312f907c515SHanjun Guo &mbigen_domain_ops, 313f907c515SHanjun Guo mgn_chip); 314f907c515SHanjun Guo if (!domain) 315f907c515SHanjun Guo return -ENOMEM; 316f907c515SHanjun Guo 317f907c515SHanjun Guo return 0; 318f907c515SHanjun Guo } 319f907c515SHanjun Guo #else 320f907c515SHanjun Guo static inline int mbigen_acpi_create_domain(struct platform_device *pdev, 321f907c515SHanjun Guo struct mbigen_device *mgn_chip) 322f907c515SHanjun Guo { 323f907c515SHanjun Guo return -ENODEV; 324f907c515SHanjun Guo } 325f907c515SHanjun Guo #endif 326f907c515SHanjun Guo 32776e1f77fSKefeng Wang static int mbigen_device_probe(struct platform_device *pdev) 32876e1f77fSKefeng Wang { 32976e1f77fSKefeng Wang struct mbigen_device *mgn_chip; 33076e1f77fSKefeng Wang struct resource *res; 33176e1f77fSKefeng Wang int err; 33276e1f77fSKefeng Wang 33376e1f77fSKefeng Wang mgn_chip = devm_kzalloc(&pdev->dev, sizeof(*mgn_chip), GFP_KERNEL); 33476e1f77fSKefeng Wang if (!mgn_chip) 33576e1f77fSKefeng Wang return -ENOMEM; 33676e1f77fSKefeng Wang 33776e1f77fSKefeng Wang mgn_chip->pdev = pdev; 33876e1f77fSKefeng Wang 33976e1f77fSKefeng Wang res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 340*5ba9b0a1SHanjun Guo mgn_chip->base = devm_ioremap(&pdev->dev, res->start, 341*5ba9b0a1SHanjun Guo resource_size(res)); 342*5ba9b0a1SHanjun Guo if (!mgn_chip->base) { 343*5ba9b0a1SHanjun Guo dev_err(&pdev->dev, "failed to ioremap %pR\n", res); 344*5ba9b0a1SHanjun Guo return -ENOMEM; 345*5ba9b0a1SHanjun Guo } 34676e1f77fSKefeng Wang 347f907c515SHanjun Guo if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) 34876e1f77fSKefeng Wang err = mbigen_of_create_domain(pdev, mgn_chip); 349f907c515SHanjun Guo else if (ACPI_COMPANION(&pdev->dev)) 350f907c515SHanjun Guo err = mbigen_acpi_create_domain(pdev, mgn_chip); 351f907c515SHanjun Guo else 352f907c515SHanjun Guo err = -EINVAL; 353f907c515SHanjun Guo 354f907c515SHanjun Guo if (err) { 355f907c515SHanjun Guo dev_err(&pdev->dev, "Failed to create mbi-gen@%p irqdomain", 356f907c515SHanjun Guo mgn_chip->base); 35776e1f77fSKefeng Wang return err; 358f907c515SHanjun Guo } 35976e1f77fSKefeng Wang 360717c3dbcSMa Jun platform_set_drvdata(pdev, mgn_chip); 361717c3dbcSMa Jun return 0; 362717c3dbcSMa Jun } 363717c3dbcSMa Jun 364717c3dbcSMa Jun static const struct of_device_id mbigen_of_match[] = { 365717c3dbcSMa Jun { .compatible = "hisilicon,mbigen-v2" }, 366717c3dbcSMa Jun { /* END */ } 367717c3dbcSMa Jun }; 368717c3dbcSMa Jun MODULE_DEVICE_TABLE(of, mbigen_of_match); 369717c3dbcSMa Jun 370f907c515SHanjun Guo static const struct acpi_device_id mbigen_acpi_match[] = { 371f907c515SHanjun Guo { "HISI0152", 0 }, 372f907c515SHanjun Guo {} 373f907c515SHanjun Guo }; 374f907c515SHanjun Guo MODULE_DEVICE_TABLE(acpi, mbigen_acpi_match); 375f907c515SHanjun Guo 376717c3dbcSMa Jun static struct platform_driver mbigen_platform_driver = { 377717c3dbcSMa Jun .driver = { 378717c3dbcSMa Jun .name = "Hisilicon MBIGEN-V2", 379717c3dbcSMa Jun .of_match_table = mbigen_of_match, 380f907c515SHanjun Guo .acpi_match_table = ACPI_PTR(mbigen_acpi_match), 381717c3dbcSMa Jun }, 382717c3dbcSMa Jun .probe = mbigen_device_probe, 383717c3dbcSMa Jun }; 384717c3dbcSMa Jun 385717c3dbcSMa Jun module_platform_driver(mbigen_platform_driver); 386717c3dbcSMa Jun 387717c3dbcSMa Jun MODULE_AUTHOR("Jun Ma <majun258@huawei.com>"); 388717c3dbcSMa Jun MODULE_AUTHOR("Yun Wu <wuyun.wu@huawei.com>"); 389717c3dbcSMa Jun MODULE_LICENSE("GPL"); 390717c3dbcSMa Jun MODULE_DESCRIPTION("Hisilicon MBI Generator driver"); 391