irq-mbigen.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 = (hwirq / 32) * 4;
  91. *mask = 1 << (hwirq % 32);
  92. *addr = ofst + REG_MBIGEN_CLEAR_OFFSET;
  93. }
  94. static void mbigen_eoi_irq(struct irq_data *data)
  95. {
  96. void __iomem *base = data->chip_data;
  97. u32 mask, addr;
  98. get_mbigen_clear_reg(data->hwirq, &mask, &addr);
  99. writel_relaxed(mask, base + addr);
  100. irq_chip_eoi_parent(data);
  101. }
  102. static int mbigen_set_type(struct irq_data *data, unsigned int type)
  103. {
  104. void __iomem *base = data->chip_data;
  105. u32 mask, addr, val;
  106. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  107. return -EINVAL;
  108. get_mbigen_type_reg(data->hwirq, &mask, &addr);
  109. val = readl_relaxed(base + addr);
  110. if (type == IRQ_TYPE_LEVEL_HIGH)
  111. val |= mask;
  112. else
  113. val &= ~mask;
  114. writel_relaxed(val, base + addr);
  115. return 0;
  116. }
  117. static struct irq_chip mbigen_irq_chip = {
  118. .name = "mbigen-v2",
  119. .irq_mask = irq_chip_mask_parent,
  120. .irq_unmask = irq_chip_unmask_parent,
  121. .irq_eoi = mbigen_eoi_irq,
  122. .irq_set_type = mbigen_set_type,
  123. .irq_set_affinity = irq_chip_set_affinity_parent,
  124. };
  125. static void mbigen_write_msg(struct msi_desc *desc, struct msi_msg *msg)
  126. {
  127. struct irq_data *d = irq_get_irq_data(desc->irq);
  128. void __iomem *base = d->chip_data;
  129. u32 val;
  130. base += get_mbigen_vec_reg(d->hwirq);
  131. val = readl_relaxed(base);
  132. val &= ~(IRQ_EVENT_ID_MASK << IRQ_EVENT_ID_SHIFT);
  133. val |= (msg->data << IRQ_EVENT_ID_SHIFT);
  134. /* The address of doorbell is encoded in mbigen register by default
  135. * So,we don't need to program the doorbell address at here
  136. */
  137. writel_relaxed(val, base);
  138. }
  139. static int mbigen_domain_translate(struct irq_domain *d,
  140. struct irq_fwspec *fwspec,
  141. unsigned long *hwirq,
  142. unsigned int *type)
  143. {
  144. if (is_of_node(fwspec->fwnode)) {
  145. if (fwspec->param_count != 2)
  146. return -EINVAL;
  147. if ((fwspec->param[0] > MAXIMUM_IRQ_PIN_NUM) ||
  148. (fwspec->param[0] < RESERVED_IRQ_PER_MBIGEN_CHIP))
  149. return -EINVAL;
  150. else
  151. *hwirq = fwspec->param[0];
  152. /* If there is no valid irq type, just use the default type */
  153. if ((fwspec->param[1] == IRQ_TYPE_EDGE_RISING) ||
  154. (fwspec->param[1] == IRQ_TYPE_LEVEL_HIGH))
  155. *type = fwspec->param[1];
  156. else
  157. return -EINVAL;
  158. return 0;
  159. }
  160. return -EINVAL;
  161. }
  162. static int mbigen_irq_domain_alloc(struct irq_domain *domain,
  163. unsigned int virq,
  164. unsigned int nr_irqs,
  165. void *args)
  166. {
  167. struct irq_fwspec *fwspec = args;
  168. irq_hw_number_t hwirq;
  169. unsigned int type;
  170. struct mbigen_device *mgn_chip;
  171. int i, err;
  172. err = mbigen_domain_translate(domain, fwspec, &hwirq, &type);
  173. if (err)
  174. return err;
  175. err = platform_msi_domain_alloc(domain, virq, nr_irqs);
  176. if (err)
  177. return err;
  178. mgn_chip = platform_msi_get_host_data(domain);
  179. for (i = 0; i < nr_irqs; i++)
  180. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  181. &mbigen_irq_chip, mgn_chip->base);
  182. return 0;
  183. }
  184. static struct irq_domain_ops mbigen_domain_ops = {
  185. .translate = mbigen_domain_translate,
  186. .alloc = mbigen_irq_domain_alloc,
  187. .free = irq_domain_free_irqs_common,
  188. };
  189. static int mbigen_device_probe(struct platform_device *pdev)
  190. {
  191. struct mbigen_device *mgn_chip;
  192. struct platform_device *child;
  193. struct irq_domain *domain;
  194. struct device_node *np;
  195. struct device *parent;
  196. struct resource *res;
  197. u32 num_pins;
  198. mgn_chip = devm_kzalloc(&pdev->dev, sizeof(*mgn_chip), GFP_KERNEL);
  199. if (!mgn_chip)
  200. return -ENOMEM;
  201. mgn_chip->pdev = pdev;
  202. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  203. mgn_chip->base = devm_ioremap_resource(&pdev->dev, res);
  204. if (IS_ERR(mgn_chip->base))
  205. return PTR_ERR(mgn_chip->base);
  206. for_each_child_of_node(pdev->dev.of_node, np) {
  207. if (!of_property_read_bool(np, "interrupt-controller"))
  208. continue;
  209. parent = platform_bus_type.dev_root;
  210. child = of_platform_device_create(np, NULL, parent);
  211. if (!child)
  212. return -ENOMEM;
  213. if (of_property_read_u32(child->dev.of_node, "num-pins",
  214. &num_pins) < 0) {
  215. dev_err(&pdev->dev, "No num-pins property\n");
  216. return -EINVAL;
  217. }
  218. domain = platform_msi_create_device_domain(&child->dev, num_pins,
  219. mbigen_write_msg,
  220. &mbigen_domain_ops,
  221. mgn_chip);
  222. if (!domain)
  223. return -ENOMEM;
  224. }
  225. platform_set_drvdata(pdev, mgn_chip);
  226. return 0;
  227. }
  228. static const struct of_device_id mbigen_of_match[] = {
  229. { .compatible = "hisilicon,mbigen-v2" },
  230. { /* END */ }
  231. };
  232. MODULE_DEVICE_TABLE(of, mbigen_of_match);
  233. static struct platform_driver mbigen_platform_driver = {
  234. .driver = {
  235. .name = "Hisilicon MBIGEN-V2",
  236. .owner = THIS_MODULE,
  237. .of_match_table = mbigen_of_match,
  238. },
  239. .probe = mbigen_device_probe,
  240. };
  241. module_platform_driver(mbigen_platform_driver);
  242. MODULE_AUTHOR("Jun Ma <majun258@huawei.com>");
  243. MODULE_AUTHOR("Yun Wu <wuyun.wu@huawei.com>");
  244. MODULE_LICENSE("GPL");
  245. MODULE_DESCRIPTION("Hisilicon MBI Generator driver");