qcom-pdc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/err.h>
  6. #include <linux/init.h>
  7. #include <linux/irq.h>
  8. #include <linux/irqchip.h>
  9. #include <linux/irqdomain.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_device.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #define PDC_MAX_IRQS 126
  20. #define CLEAR_INTR(reg, intr) (reg & ~(1 << intr))
  21. #define ENABLE_INTR(reg, intr) (reg | (1 << intr))
  22. #define IRQ_ENABLE_BANK 0x10
  23. #define IRQ_i_CFG 0x110
  24. struct pdc_pin_region {
  25. u32 pin_base;
  26. u32 parent_base;
  27. u32 cnt;
  28. };
  29. static DEFINE_RAW_SPINLOCK(pdc_lock);
  30. static void __iomem *pdc_base;
  31. static struct pdc_pin_region *pdc_region;
  32. static int pdc_region_cnt;
  33. static void pdc_reg_write(int reg, u32 i, u32 val)
  34. {
  35. writel_relaxed(val, pdc_base + reg + i * sizeof(u32));
  36. }
  37. static u32 pdc_reg_read(int reg, u32 i)
  38. {
  39. return readl_relaxed(pdc_base + reg + i * sizeof(u32));
  40. }
  41. static void pdc_enable_intr(struct irq_data *d, bool on)
  42. {
  43. int pin_out = d->hwirq;
  44. u32 index, mask;
  45. u32 enable;
  46. index = pin_out / 32;
  47. mask = pin_out % 32;
  48. raw_spin_lock(&pdc_lock);
  49. enable = pdc_reg_read(IRQ_ENABLE_BANK, index);
  50. enable = on ? ENABLE_INTR(enable, mask) : CLEAR_INTR(enable, mask);
  51. pdc_reg_write(IRQ_ENABLE_BANK, index, enable);
  52. raw_spin_unlock(&pdc_lock);
  53. }
  54. static void qcom_pdc_gic_mask(struct irq_data *d)
  55. {
  56. pdc_enable_intr(d, false);
  57. irq_chip_mask_parent(d);
  58. }
  59. static void qcom_pdc_gic_unmask(struct irq_data *d)
  60. {
  61. pdc_enable_intr(d, true);
  62. irq_chip_unmask_parent(d);
  63. }
  64. /*
  65. * GIC does not handle falling edge or active low. To allow falling edge and
  66. * active low interrupts to be handled at GIC, PDC has an inverter that inverts
  67. * falling edge into a rising edge and active low into an active high.
  68. * For the inverter to work, the polarity bit in the IRQ_CONFIG register has to
  69. * set as per the table below.
  70. * Level sensitive active low LOW
  71. * Rising edge sensitive NOT USED
  72. * Falling edge sensitive LOW
  73. * Dual Edge sensitive NOT USED
  74. * Level sensitive active High HIGH
  75. * Falling Edge sensitive NOT USED
  76. * Rising edge sensitive HIGH
  77. * Dual Edge sensitive HIGH
  78. */
  79. enum pdc_irq_config_bits {
  80. PDC_LEVEL_LOW = 0b000,
  81. PDC_EDGE_FALLING = 0b010,
  82. PDC_LEVEL_HIGH = 0b100,
  83. PDC_EDGE_RISING = 0b110,
  84. PDC_EDGE_DUAL = 0b111,
  85. };
  86. /**
  87. * qcom_pdc_gic_set_type: Configure PDC for the interrupt
  88. *
  89. * @d: the interrupt data
  90. * @type: the interrupt type
  91. *
  92. * If @type is edge triggered, forward that as Rising edge as PDC
  93. * takes care of converting falling edge to rising edge signal
  94. * If @type is level, then forward that as level high as PDC
  95. * takes care of converting falling edge to rising edge signal
  96. */
  97. static int qcom_pdc_gic_set_type(struct irq_data *d, unsigned int type)
  98. {
  99. int pin_out = d->hwirq;
  100. enum pdc_irq_config_bits pdc_type;
  101. switch (type) {
  102. case IRQ_TYPE_EDGE_RISING:
  103. pdc_type = PDC_EDGE_RISING;
  104. break;
  105. case IRQ_TYPE_EDGE_FALLING:
  106. pdc_type = PDC_EDGE_FALLING;
  107. type = IRQ_TYPE_EDGE_RISING;
  108. break;
  109. case IRQ_TYPE_EDGE_BOTH:
  110. pdc_type = PDC_EDGE_DUAL;
  111. type = IRQ_TYPE_EDGE_RISING;
  112. break;
  113. case IRQ_TYPE_LEVEL_HIGH:
  114. pdc_type = PDC_LEVEL_HIGH;
  115. break;
  116. case IRQ_TYPE_LEVEL_LOW:
  117. pdc_type = PDC_LEVEL_LOW;
  118. type = IRQ_TYPE_LEVEL_HIGH;
  119. break;
  120. default:
  121. WARN_ON(1);
  122. return -EINVAL;
  123. }
  124. pdc_reg_write(IRQ_i_CFG, pin_out, pdc_type);
  125. return irq_chip_set_type_parent(d, type);
  126. }
  127. static struct irq_chip qcom_pdc_gic_chip = {
  128. .name = "PDC",
  129. .irq_eoi = irq_chip_eoi_parent,
  130. .irq_mask = qcom_pdc_gic_mask,
  131. .irq_unmask = qcom_pdc_gic_unmask,
  132. .irq_retrigger = irq_chip_retrigger_hierarchy,
  133. .irq_set_type = qcom_pdc_gic_set_type,
  134. .flags = IRQCHIP_MASK_ON_SUSPEND |
  135. IRQCHIP_SET_TYPE_MASKED |
  136. IRQCHIP_SKIP_SET_WAKE,
  137. .irq_set_vcpu_affinity = irq_chip_set_vcpu_affinity_parent,
  138. .irq_set_affinity = irq_chip_set_affinity_parent,
  139. };
  140. static irq_hw_number_t get_parent_hwirq(int pin)
  141. {
  142. int i;
  143. struct pdc_pin_region *region;
  144. for (i = 0; i < pdc_region_cnt; i++) {
  145. region = &pdc_region[i];
  146. if (pin >= region->pin_base &&
  147. pin < region->pin_base + region->cnt)
  148. return (region->parent_base + pin - region->pin_base);
  149. }
  150. WARN_ON(1);
  151. return ~0UL;
  152. }
  153. static int qcom_pdc_translate(struct irq_domain *d, struct irq_fwspec *fwspec,
  154. unsigned long *hwirq, unsigned int *type)
  155. {
  156. if (is_of_node(fwspec->fwnode)) {
  157. if (fwspec->param_count != 2)
  158. return -EINVAL;
  159. *hwirq = fwspec->param[0];
  160. *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
  161. return 0;
  162. }
  163. return -EINVAL;
  164. }
  165. static int qcom_pdc_alloc(struct irq_domain *domain, unsigned int virq,
  166. unsigned int nr_irqs, void *data)
  167. {
  168. struct irq_fwspec *fwspec = data;
  169. struct irq_fwspec parent_fwspec;
  170. irq_hw_number_t hwirq, parent_hwirq;
  171. unsigned int type;
  172. int ret;
  173. ret = qcom_pdc_translate(domain, fwspec, &hwirq, &type);
  174. if (ret)
  175. return -EINVAL;
  176. parent_hwirq = get_parent_hwirq(hwirq);
  177. if (parent_hwirq == ~0UL)
  178. return -EINVAL;
  179. ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  180. &qcom_pdc_gic_chip, NULL);
  181. if (ret)
  182. return ret;
  183. if (type & IRQ_TYPE_EDGE_BOTH)
  184. type = IRQ_TYPE_EDGE_RISING;
  185. if (type & IRQ_TYPE_LEVEL_MASK)
  186. type = IRQ_TYPE_LEVEL_HIGH;
  187. parent_fwspec.fwnode = domain->parent->fwnode;
  188. parent_fwspec.param_count = 3;
  189. parent_fwspec.param[0] = 0;
  190. parent_fwspec.param[1] = parent_hwirq;
  191. parent_fwspec.param[2] = type;
  192. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
  193. &parent_fwspec);
  194. }
  195. static const struct irq_domain_ops qcom_pdc_ops = {
  196. .translate = qcom_pdc_translate,
  197. .alloc = qcom_pdc_alloc,
  198. .free = irq_domain_free_irqs_common,
  199. };
  200. static int pdc_setup_pin_mapping(struct device_node *np)
  201. {
  202. int ret, n;
  203. n = of_property_count_elems_of_size(np, "qcom,pdc-ranges", sizeof(u32));
  204. if (n <= 0 || n % 3)
  205. return -EINVAL;
  206. pdc_region_cnt = n / 3;
  207. pdc_region = kcalloc(pdc_region_cnt, sizeof(*pdc_region), GFP_KERNEL);
  208. if (!pdc_region) {
  209. pdc_region_cnt = 0;
  210. return -ENOMEM;
  211. }
  212. for (n = 0; n < pdc_region_cnt; n++) {
  213. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  214. n * 3 + 0,
  215. &pdc_region[n].pin_base);
  216. if (ret)
  217. return ret;
  218. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  219. n * 3 + 1,
  220. &pdc_region[n].parent_base);
  221. if (ret)
  222. return ret;
  223. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  224. n * 3 + 2,
  225. &pdc_region[n].cnt);
  226. if (ret)
  227. return ret;
  228. }
  229. return 0;
  230. }
  231. static int qcom_pdc_init(struct device_node *node, struct device_node *parent)
  232. {
  233. struct irq_domain *parent_domain, *pdc_domain;
  234. int ret;
  235. pdc_base = of_iomap(node, 0);
  236. if (!pdc_base) {
  237. pr_err("%pOF: unable to map PDC registers\n", node);
  238. return -ENXIO;
  239. }
  240. parent_domain = irq_find_host(parent);
  241. if (!parent_domain) {
  242. pr_err("%pOF: unable to find PDC's parent domain\n", node);
  243. ret = -ENXIO;
  244. goto fail;
  245. }
  246. ret = pdc_setup_pin_mapping(node);
  247. if (ret) {
  248. pr_err("%pOF: failed to init PDC pin-hwirq mapping\n", node);
  249. goto fail;
  250. }
  251. pdc_domain = irq_domain_create_hierarchy(parent_domain, 0, PDC_MAX_IRQS,
  252. of_fwnode_handle(node),
  253. &qcom_pdc_ops, NULL);
  254. if (!pdc_domain) {
  255. pr_err("%pOF: GIC domain add failed\n", node);
  256. ret = -ENOMEM;
  257. goto fail;
  258. }
  259. return 0;
  260. fail:
  261. kfree(pdc_region);
  262. iounmap(pdc_base);
  263. return ret;
  264. }
  265. IRQCHIP_DECLARE(pdc_sdm845, "qcom,sdm845-pdc", qcom_pdc_init);