qcom-pdc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. break;
  112. case IRQ_TYPE_LEVEL_HIGH:
  113. pdc_type = PDC_LEVEL_HIGH;
  114. break;
  115. case IRQ_TYPE_LEVEL_LOW:
  116. pdc_type = PDC_LEVEL_LOW;
  117. type = IRQ_TYPE_LEVEL_HIGH;
  118. break;
  119. default:
  120. WARN_ON(1);
  121. return -EINVAL;
  122. }
  123. pdc_reg_write(IRQ_i_CFG, pin_out, pdc_type);
  124. return irq_chip_set_type_parent(d, type);
  125. }
  126. static struct irq_chip qcom_pdc_gic_chip = {
  127. .name = "PDC",
  128. .irq_eoi = irq_chip_eoi_parent,
  129. .irq_mask = qcom_pdc_gic_mask,
  130. .irq_unmask = qcom_pdc_gic_unmask,
  131. .irq_retrigger = irq_chip_retrigger_hierarchy,
  132. .irq_set_type = qcom_pdc_gic_set_type,
  133. .flags = IRQCHIP_MASK_ON_SUSPEND |
  134. IRQCHIP_SET_TYPE_MASKED |
  135. IRQCHIP_SKIP_SET_WAKE,
  136. .irq_set_vcpu_affinity = irq_chip_set_vcpu_affinity_parent,
  137. .irq_set_affinity = irq_chip_set_affinity_parent,
  138. };
  139. static irq_hw_number_t get_parent_hwirq(int pin)
  140. {
  141. int i;
  142. struct pdc_pin_region *region;
  143. for (i = 0; i < pdc_region_cnt; i++) {
  144. region = &pdc_region[i];
  145. if (pin >= region->pin_base &&
  146. pin < region->pin_base + region->cnt)
  147. return (region->parent_base + pin - region->pin_base);
  148. }
  149. WARN_ON(1);
  150. return ~0UL;
  151. }
  152. static int qcom_pdc_translate(struct irq_domain *d, struct irq_fwspec *fwspec,
  153. unsigned long *hwirq, unsigned int *type)
  154. {
  155. if (is_of_node(fwspec->fwnode)) {
  156. if (fwspec->param_count != 2)
  157. return -EINVAL;
  158. *hwirq = fwspec->param[0];
  159. *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
  160. return 0;
  161. }
  162. return -EINVAL;
  163. }
  164. static int qcom_pdc_alloc(struct irq_domain *domain, unsigned int virq,
  165. unsigned int nr_irqs, void *data)
  166. {
  167. struct irq_fwspec *fwspec = data;
  168. struct irq_fwspec parent_fwspec;
  169. irq_hw_number_t hwirq, parent_hwirq;
  170. unsigned int type;
  171. int ret;
  172. ret = qcom_pdc_translate(domain, fwspec, &hwirq, &type);
  173. if (ret)
  174. return -EINVAL;
  175. parent_hwirq = get_parent_hwirq(hwirq);
  176. if (parent_hwirq == ~0UL)
  177. return -EINVAL;
  178. ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  179. &qcom_pdc_gic_chip, NULL);
  180. if (ret)
  181. return ret;
  182. if (type & IRQ_TYPE_EDGE_BOTH)
  183. type = IRQ_TYPE_EDGE_RISING;
  184. if (type & IRQ_TYPE_LEVEL_MASK)
  185. type = IRQ_TYPE_LEVEL_HIGH;
  186. parent_fwspec.fwnode = domain->parent->fwnode;
  187. parent_fwspec.param_count = 3;
  188. parent_fwspec.param[0] = 0;
  189. parent_fwspec.param[1] = parent_hwirq;
  190. parent_fwspec.param[2] = type;
  191. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
  192. &parent_fwspec);
  193. }
  194. static const struct irq_domain_ops qcom_pdc_ops = {
  195. .translate = qcom_pdc_translate,
  196. .alloc = qcom_pdc_alloc,
  197. .free = irq_domain_free_irqs_common,
  198. };
  199. static int pdc_setup_pin_mapping(struct device_node *np)
  200. {
  201. int ret, n;
  202. n = of_property_count_elems_of_size(np, "qcom,pdc-ranges", sizeof(u32));
  203. if (n <= 0 || n % 3)
  204. return -EINVAL;
  205. pdc_region_cnt = n / 3;
  206. pdc_region = kcalloc(pdc_region_cnt, sizeof(*pdc_region), GFP_KERNEL);
  207. if (!pdc_region) {
  208. pdc_region_cnt = 0;
  209. return -ENOMEM;
  210. }
  211. for (n = 0; n < pdc_region_cnt; n++) {
  212. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  213. n * 3 + 0,
  214. &pdc_region[n].pin_base);
  215. if (ret)
  216. return ret;
  217. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  218. n * 3 + 1,
  219. &pdc_region[n].parent_base);
  220. if (ret)
  221. return ret;
  222. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  223. n * 3 + 2,
  224. &pdc_region[n].cnt);
  225. if (ret)
  226. return ret;
  227. }
  228. return 0;
  229. }
  230. static int qcom_pdc_init(struct device_node *node, struct device_node *parent)
  231. {
  232. struct irq_domain *parent_domain, *pdc_domain;
  233. int ret;
  234. pdc_base = of_iomap(node, 0);
  235. if (!pdc_base) {
  236. pr_err("%pOF: unable to map PDC registers\n", node);
  237. return -ENXIO;
  238. }
  239. parent_domain = irq_find_host(parent);
  240. if (!parent_domain) {
  241. pr_err("%pOF: unable to find PDC's parent domain\n", node);
  242. ret = -ENXIO;
  243. goto fail;
  244. }
  245. ret = pdc_setup_pin_mapping(node);
  246. if (ret) {
  247. pr_err("%pOF: failed to init PDC pin-hwirq mapping\n", node);
  248. goto fail;
  249. }
  250. pdc_domain = irq_domain_create_hierarchy(parent_domain, 0, PDC_MAX_IRQS,
  251. of_fwnode_handle(node),
  252. &qcom_pdc_ops, NULL);
  253. if (!pdc_domain) {
  254. pr_err("%pOF: GIC domain add failed\n", node);
  255. ret = -ENOMEM;
  256. goto fail;
  257. }
  258. return 0;
  259. fail:
  260. kfree(pdc_region);
  261. iounmap(pdc_base);
  262. return ret;
  263. }
  264. IRQCHIP_DECLARE(pdc_sdm845, "qcom,sdm845-pdc", qcom_pdc_init);