irq-mbigen.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (C) 2015 Hisilicon Limited, All Rights Reserved.
  3. * Author: Jun Ma <majun258@huawei.com>
  4. * Author: Yun Wu <wuyun.wu@huawei.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/interrupt.h>
  19. #include <linux/irqchip.h>
  20. #include <linux/module.h>
  21. #include <linux/msi.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. /* Interrupt numbers per mbigen node supported */
  28. #define IRQS_PER_MBIGEN_NODE 128
  29. /* 64 irqs (Pin0-pin63) are reserved for each mbigen chip */
  30. #define RESERVED_IRQ_PER_MBIGEN_CHIP 64
  31. /* The maximum IRQ pin number of mbigen chip(start from 0) */
  32. #define MAXIMUM_IRQ_PIN_NUM 1407
  33. /**
  34. * In mbigen vector register
  35. * bit[21:12]: event id value
  36. * bit[11:0]: device id
  37. */
  38. #define IRQ_EVENT_ID_SHIFT 12
  39. #define IRQ_EVENT_ID_MASK 0x3ff
  40. /* register range of each mbigen node */
  41. #define MBIGEN_NODE_OFFSET 0x1000
  42. /* offset of vector register in mbigen node */
  43. #define REG_MBIGEN_VEC_OFFSET 0x200
  44. /**
  45. * offset of clear register in mbigen node
  46. * This register is used to clear the status
  47. * of interrupt
  48. */
  49. #define REG_MBIGEN_CLEAR_OFFSET 0xa000
  50. /**
  51. * offset of interrupt type register
  52. * This register is used to configure interrupt
  53. * trigger type
  54. */
  55. #define REG_MBIGEN_TYPE_OFFSET 0x0
  56. /**
  57. * struct mbigen_device - holds the information of mbigen device.
  58. *
  59. * @pdev: pointer to the platform device structure of mbigen chip.
  60. * @base: mapped address of this mbigen chip.
  61. */
  62. struct mbigen_device {
  63. struct platform_device *pdev;
  64. void __iomem *base;
  65. };
  66. static inline unsigned int get_mbigen_vec_reg(irq_hw_number_t hwirq)
  67. {
  68. unsigned int nid, pin;
  69. hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP;
  70. nid = hwirq / IRQS_PER_MBIGEN_NODE + 1;
  71. pin = hwirq % IRQS_PER_MBIGEN_NODE;
  72. return pin * 4 + nid * MBIGEN_NODE_OFFSET
  73. + REG_MBIGEN_VEC_OFFSET;
  74. }
  75. static inline void get_mbigen_type_reg(irq_hw_number_t hwirq,
  76. u32 *mask, u32 *addr)
  77. {
  78. unsigned int nid, irq_ofst, ofst;
  79. hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP;
  80. nid = hwirq / IRQS_PER_MBIGEN_NODE + 1;
  81. irq_ofst = hwirq % IRQS_PER_MBIGEN_NODE;
  82. *mask = 1 << (irq_ofst % 32);
  83. ofst = irq_ofst / 32 * 4;
  84. *addr = ofst + nid * MBIGEN_NODE_OFFSET
  85. + REG_MBIGEN_TYPE_OFFSET;
  86. }
  87. static inline void get_mbigen_clear_reg(irq_hw_number_t hwirq,
  88. u32 *mask, u32 *addr)
  89. {
  90. unsigned int ofst;
  91. hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP;
  92. ofst = hwirq / 32 * 4;
  93. *mask = 1 << (hwirq % 32);
  94. *addr = ofst + REG_MBIGEN_CLEAR_OFFSET;
  95. }
  96. static void mbigen_eoi_irq(struct irq_data *data)
  97. {
  98. void __iomem *base = data->chip_data;
  99. u32 mask, addr;
  100. get_mbigen_clear_reg(data->hwirq, &mask, &addr);
  101. writel_relaxed(mask, base + addr);
  102. irq_chip_eoi_parent(data);
  103. }
  104. static int mbigen_set_type(struct irq_data *data, unsigned int type)
  105. {
  106. void __iomem *base = data->chip_data;
  107. u32 mask, addr, val;
  108. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  109. return -EINVAL;
  110. get_mbigen_type_reg(data->hwirq, &mask, &addr);
  111. val = readl_relaxed(base + addr);
  112. if (type == IRQ_TYPE_LEVEL_HIGH)
  113. val |= mask;
  114. else
  115. val &= ~mask;
  116. writel_relaxed(val, base + addr);
  117. return 0;
  118. }
  119. static struct irq_chip mbigen_irq_chip = {
  120. .name = "mbigen-v2",
  121. .irq_mask = irq_chip_mask_parent,
  122. .irq_unmask = irq_chip_unmask_parent,
  123. .irq_eoi = mbigen_eoi_irq,
  124. .irq_set_type = mbigen_set_type,
  125. .irq_set_affinity = irq_chip_set_affinity_parent,
  126. };
  127. static void mbigen_write_msg(struct msi_desc *desc, struct msi_msg *msg)
  128. {
  129. struct irq_data *d = irq_get_irq_data(desc->irq);
  130. void __iomem *base = d->chip_data;
  131. u32 val;
  132. base += get_mbigen_vec_reg(d->hwirq);
  133. val = readl_relaxed(base);
  134. val &= ~(IRQ_EVENT_ID_MASK << IRQ_EVENT_ID_SHIFT);
  135. val |= (msg->data << IRQ_EVENT_ID_SHIFT);
  136. /* The address of doorbell is encoded in mbigen register by default
  137. * So,we don't need to program the doorbell address at here
  138. */
  139. writel_relaxed(val, base);
  140. }
  141. static int mbigen_domain_translate(struct irq_domain *d,
  142. struct irq_fwspec *fwspec,
  143. unsigned long *hwirq,
  144. unsigned int *type)
  145. {
  146. if (is_of_node(fwspec->fwnode)) {
  147. if (fwspec->param_count != 2)
  148. return -EINVAL;
  149. if ((fwspec->param[0] > MAXIMUM_IRQ_PIN_NUM) ||
  150. (fwspec->param[0] < RESERVED_IRQ_PER_MBIGEN_CHIP))
  151. return -EINVAL;
  152. else
  153. *hwirq = fwspec->param[0];
  154. /* If there is no valid irq type, just use the default type */
  155. if ((fwspec->param[1] == IRQ_TYPE_EDGE_RISING) ||
  156. (fwspec->param[1] == IRQ_TYPE_LEVEL_HIGH))
  157. *type = fwspec->param[1];
  158. else
  159. return -EINVAL;
  160. return 0;
  161. }
  162. return -EINVAL;
  163. }
  164. static int mbigen_irq_domain_alloc(struct irq_domain *domain,
  165. unsigned int virq,
  166. unsigned int nr_irqs,
  167. void *args)
  168. {
  169. struct irq_fwspec *fwspec = args;
  170. irq_hw_number_t hwirq;
  171. unsigned int type;
  172. struct mbigen_device *mgn_chip;
  173. int i, err;
  174. err = mbigen_domain_translate(domain, fwspec, &hwirq, &type);
  175. if (err)
  176. return err;
  177. err = platform_msi_domain_alloc(domain, virq, nr_irqs);
  178. if (err)
  179. return err;
  180. mgn_chip = platform_msi_get_host_data(domain);
  181. for (i = 0; i < nr_irqs; i++)
  182. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  183. &mbigen_irq_chip, mgn_chip->base);
  184. return 0;
  185. }
  186. static struct irq_domain_ops mbigen_domain_ops = {
  187. .translate = mbigen_domain_translate,
  188. .alloc = mbigen_irq_domain_alloc,
  189. .free = irq_domain_free_irqs_common,
  190. };
  191. static int mbigen_device_probe(struct platform_device *pdev)
  192. {
  193. struct mbigen_device *mgn_chip;
  194. struct platform_device *child;
  195. struct irq_domain *domain;
  196. struct device_node *np;
  197. struct device *parent;
  198. struct resource *res;
  199. u32 num_pins;
  200. mgn_chip = devm_kzalloc(&pdev->dev, sizeof(*mgn_chip), GFP_KERNEL);
  201. if (!mgn_chip)
  202. return -ENOMEM;
  203. mgn_chip->pdev = pdev;
  204. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  205. mgn_chip->base = devm_ioremap_resource(&pdev->dev, res);
  206. if (IS_ERR(mgn_chip->base))
  207. return PTR_ERR(mgn_chip->base);
  208. for_each_child_of_node(pdev->dev.of_node, np) {
  209. if (!of_property_read_bool(np, "interrupt-controller"))
  210. continue;
  211. parent = platform_bus_type.dev_root;
  212. child = of_platform_device_create(np, NULL, parent);
  213. if (!child)
  214. return -ENOMEM;
  215. if (of_property_read_u32(child->dev.of_node, "num-pins",
  216. &num_pins) < 0) {
  217. dev_err(&pdev->dev, "No num-pins property\n");
  218. return -EINVAL;
  219. }
  220. domain = platform_msi_create_device_domain(&child->dev, num_pins,
  221. mbigen_write_msg,
  222. &mbigen_domain_ops,
  223. mgn_chip);
  224. if (!domain)
  225. return -ENOMEM;
  226. }
  227. platform_set_drvdata(pdev, mgn_chip);
  228. return 0;
  229. }
  230. static const struct of_device_id mbigen_of_match[] = {
  231. { .compatible = "hisilicon,mbigen-v2" },
  232. { /* END */ }
  233. };
  234. MODULE_DEVICE_TABLE(of, mbigen_of_match);
  235. static struct platform_driver mbigen_platform_driver = {
  236. .driver = {
  237. .name = "Hisilicon MBIGEN-V2",
  238. .owner = THIS_MODULE,
  239. .of_match_table = mbigen_of_match,
  240. },
  241. .probe = mbigen_device_probe,
  242. };
  243. module_platform_driver(mbigen_platform_driver);
  244. MODULE_AUTHOR("Jun Ma <majun258@huawei.com>");
  245. MODULE_AUTHOR("Yun Wu <wuyun.wu@huawei.com>");
  246. MODULE_LICENSE("GPL");
  247. MODULE_DESCRIPTION("Hisilicon MBI Generator driver");