qcom-pdc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/err.h>
  6. #include <linux/init.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/irq.h>
  9. #include <linux/irqchip.h>
  10. #include <linux/irqdomain.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/soc/qcom/irq.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/slab.h>
  20. #include <linux/types.h>
  21. #define PDC_MAX_GPIO_IRQS 256
  22. #define PDC_DRV_OFFSET 0x10000
  23. /* Valid only on HW version < 3.2 */
  24. #define IRQ_ENABLE_BANK 0x10
  25. #define IRQ_ENABLE_BANK_MAX (IRQ_ENABLE_BANK + BITS_TO_BYTES(PDC_MAX_GPIO_IRQS))
  26. #define IRQ_i_CFG 0x110
  27. /* Valid only on HW version >= 3.2 */
  28. #define IRQ_i_CFG_IRQ_ENABLE 3
  29. #define IRQ_i_CFG_TYPE_MASK GENMASK(2, 0)
  30. #define PDC_VERSION_REG 0x1000
  31. /* Notable PDC versions */
  32. #define PDC_VERSION_3_2 0x30200
  33. struct pdc_pin_region {
  34. u32 pin_base;
  35. u32 parent_base;
  36. u32 cnt;
  37. };
  38. #define pin_to_hwirq(r, p) ((r)->parent_base + (p) - (r)->pin_base)
  39. static DEFINE_RAW_SPINLOCK(pdc_lock);
  40. static void __iomem *pdc_base;
  41. static void __iomem *pdc_prev_base;
  42. static struct pdc_pin_region *pdc_region;
  43. static int pdc_region_cnt;
  44. static unsigned int pdc_version;
  45. static bool pdc_x1e_quirk;
  46. static void pdc_base_reg_write(void __iomem *base, int reg, u32 i, u32 val)
  47. {
  48. writel_relaxed(val, base + reg + i * sizeof(u32));
  49. }
  50. static void pdc_reg_write(int reg, u32 i, u32 val)
  51. {
  52. pdc_base_reg_write(pdc_base, reg, i, val);
  53. }
  54. static u32 pdc_reg_read(int reg, u32 i)
  55. {
  56. return readl_relaxed(pdc_base + reg + i * sizeof(u32));
  57. }
  58. static void pdc_x1e_irq_enable_write(u32 bank, u32 enable)
  59. {
  60. void __iomem *base;
  61. /* Remap the write access to work around a hardware bug on X1E */
  62. switch (bank) {
  63. case 0 ... 1:
  64. /* Use previous DRV (client) region and shift to bank 3-4 */
  65. base = pdc_prev_base;
  66. bank += 3;
  67. break;
  68. case 2 ... 4:
  69. /* Use our own region and shift to bank 0-2 */
  70. base = pdc_base;
  71. bank -= 2;
  72. break;
  73. case 5:
  74. /* No fixup required for bank 5 */
  75. base = pdc_base;
  76. break;
  77. default:
  78. WARN_ON(1);
  79. return;
  80. }
  81. pdc_base_reg_write(base, IRQ_ENABLE_BANK, bank, enable);
  82. }
  83. static void __pdc_enable_intr(int pin_out, bool on)
  84. {
  85. unsigned long enable;
  86. if (pdc_version < PDC_VERSION_3_2) {
  87. u32 index, mask;
  88. index = pin_out / 32;
  89. mask = pin_out % 32;
  90. enable = pdc_reg_read(IRQ_ENABLE_BANK, index);
  91. __assign_bit(mask, &enable, on);
  92. if (pdc_x1e_quirk)
  93. pdc_x1e_irq_enable_write(index, enable);
  94. else
  95. pdc_reg_write(IRQ_ENABLE_BANK, index, enable);
  96. } else {
  97. enable = pdc_reg_read(IRQ_i_CFG, pin_out);
  98. __assign_bit(IRQ_i_CFG_IRQ_ENABLE, &enable, on);
  99. pdc_reg_write(IRQ_i_CFG, pin_out, enable);
  100. }
  101. }
  102. static void pdc_enable_intr(struct irq_data *d, bool on)
  103. {
  104. unsigned long flags;
  105. raw_spin_lock_irqsave(&pdc_lock, flags);
  106. __pdc_enable_intr(d->hwirq, on);
  107. raw_spin_unlock_irqrestore(&pdc_lock, flags);
  108. }
  109. static void qcom_pdc_gic_disable(struct irq_data *d)
  110. {
  111. pdc_enable_intr(d, false);
  112. irq_chip_disable_parent(d);
  113. }
  114. static void qcom_pdc_gic_enable(struct irq_data *d)
  115. {
  116. pdc_enable_intr(d, true);
  117. irq_chip_enable_parent(d);
  118. }
  119. /*
  120. * GIC does not handle falling edge or active low. To allow falling edge and
  121. * active low interrupts to be handled at GIC, PDC has an inverter that inverts
  122. * falling edge into a rising edge and active low into an active high.
  123. * For the inverter to work, the polarity bit in the IRQ_CONFIG register has to
  124. * set as per the table below.
  125. * Level sensitive active low LOW
  126. * Rising edge sensitive NOT USED
  127. * Falling edge sensitive LOW
  128. * Dual Edge sensitive NOT USED
  129. * Level sensitive active High HIGH
  130. * Falling Edge sensitive NOT USED
  131. * Rising edge sensitive HIGH
  132. * Dual Edge sensitive HIGH
  133. */
  134. enum pdc_irq_config_bits {
  135. PDC_LEVEL_LOW = 0b000,
  136. PDC_EDGE_FALLING = 0b010,
  137. PDC_LEVEL_HIGH = 0b100,
  138. PDC_EDGE_RISING = 0b110,
  139. PDC_EDGE_DUAL = 0b111,
  140. };
  141. /**
  142. * qcom_pdc_gic_set_type: Configure PDC for the interrupt
  143. *
  144. * @d: the interrupt data
  145. * @type: the interrupt type
  146. *
  147. * If @type is edge triggered, forward that as Rising edge as PDC
  148. * takes care of converting falling edge to rising edge signal
  149. * If @type is level, then forward that as level high as PDC
  150. * takes care of converting falling edge to rising edge signal
  151. */
  152. static int qcom_pdc_gic_set_type(struct irq_data *d, unsigned int type)
  153. {
  154. enum pdc_irq_config_bits pdc_type;
  155. enum pdc_irq_config_bits old_pdc_type;
  156. int ret;
  157. switch (type) {
  158. case IRQ_TYPE_EDGE_RISING:
  159. pdc_type = PDC_EDGE_RISING;
  160. break;
  161. case IRQ_TYPE_EDGE_FALLING:
  162. pdc_type = PDC_EDGE_FALLING;
  163. type = IRQ_TYPE_EDGE_RISING;
  164. break;
  165. case IRQ_TYPE_EDGE_BOTH:
  166. pdc_type = PDC_EDGE_DUAL;
  167. type = IRQ_TYPE_EDGE_RISING;
  168. break;
  169. case IRQ_TYPE_LEVEL_HIGH:
  170. pdc_type = PDC_LEVEL_HIGH;
  171. break;
  172. case IRQ_TYPE_LEVEL_LOW:
  173. pdc_type = PDC_LEVEL_LOW;
  174. type = IRQ_TYPE_LEVEL_HIGH;
  175. break;
  176. default:
  177. WARN_ON(1);
  178. return -EINVAL;
  179. }
  180. old_pdc_type = pdc_reg_read(IRQ_i_CFG, d->hwirq);
  181. pdc_type |= (old_pdc_type & ~IRQ_i_CFG_TYPE_MASK);
  182. pdc_reg_write(IRQ_i_CFG, d->hwirq, pdc_type);
  183. ret = irq_chip_set_type_parent(d, type);
  184. if (ret)
  185. return ret;
  186. /*
  187. * When we change types the PDC can give a phantom interrupt.
  188. * Clear it. Specifically the phantom shows up when reconfiguring
  189. * polarity of interrupt without changing the state of the signal
  190. * but let's be consistent and clear it always.
  191. *
  192. * Doing this works because we have IRQCHIP_SET_TYPE_MASKED so the
  193. * interrupt will be cleared before the rest of the system sees it.
  194. */
  195. if (old_pdc_type != pdc_type)
  196. irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
  197. return 0;
  198. }
  199. static struct irq_chip qcom_pdc_gic_chip = {
  200. .name = "PDC",
  201. .irq_eoi = irq_chip_eoi_parent,
  202. .irq_mask = irq_chip_mask_parent,
  203. .irq_unmask = irq_chip_unmask_parent,
  204. .irq_disable = qcom_pdc_gic_disable,
  205. .irq_enable = qcom_pdc_gic_enable,
  206. .irq_get_irqchip_state = irq_chip_get_parent_state,
  207. .irq_set_irqchip_state = irq_chip_set_parent_state,
  208. .irq_retrigger = irq_chip_retrigger_hierarchy,
  209. .irq_set_type = qcom_pdc_gic_set_type,
  210. .flags = IRQCHIP_MASK_ON_SUSPEND |
  211. IRQCHIP_SET_TYPE_MASKED |
  212. IRQCHIP_SKIP_SET_WAKE |
  213. IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND,
  214. .irq_set_vcpu_affinity = irq_chip_set_vcpu_affinity_parent,
  215. .irq_set_affinity = irq_chip_set_affinity_parent,
  216. };
  217. static struct pdc_pin_region *get_pin_region(int pin)
  218. {
  219. int i;
  220. for (i = 0; i < pdc_region_cnt; i++) {
  221. if (pin >= pdc_region[i].pin_base &&
  222. pin < pdc_region[i].pin_base + pdc_region[i].cnt)
  223. return &pdc_region[i];
  224. }
  225. return NULL;
  226. }
  227. static int qcom_pdc_alloc(struct irq_domain *domain, unsigned int virq,
  228. unsigned int nr_irqs, void *data)
  229. {
  230. struct irq_fwspec *fwspec = data;
  231. struct irq_fwspec parent_fwspec;
  232. struct pdc_pin_region *region;
  233. irq_hw_number_t hwirq;
  234. unsigned int type;
  235. int ret;
  236. ret = irq_domain_translate_twocell(domain, fwspec, &hwirq, &type);
  237. if (ret)
  238. return ret;
  239. if (hwirq == GPIO_NO_WAKE_IRQ)
  240. return irq_domain_disconnect_hierarchy(domain, virq);
  241. ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  242. &qcom_pdc_gic_chip, NULL);
  243. if (ret)
  244. return ret;
  245. region = get_pin_region(hwirq);
  246. if (!region)
  247. return irq_domain_disconnect_hierarchy(domain->parent, virq);
  248. if (type & IRQ_TYPE_EDGE_BOTH)
  249. type = IRQ_TYPE_EDGE_RISING;
  250. if (type & IRQ_TYPE_LEVEL_MASK)
  251. type = IRQ_TYPE_LEVEL_HIGH;
  252. parent_fwspec.fwnode = domain->parent->fwnode;
  253. parent_fwspec.param_count = 3;
  254. parent_fwspec.param[0] = 0;
  255. parent_fwspec.param[1] = pin_to_hwirq(region, hwirq);
  256. parent_fwspec.param[2] = type;
  257. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
  258. &parent_fwspec);
  259. }
  260. static const struct irq_domain_ops qcom_pdc_ops = {
  261. .translate = irq_domain_translate_twocell,
  262. .alloc = qcom_pdc_alloc,
  263. .free = irq_domain_free_irqs_common,
  264. };
  265. static int pdc_setup_pin_mapping(struct device_node *np)
  266. {
  267. int ret, n, i;
  268. n = of_property_count_elems_of_size(np, "qcom,pdc-ranges", sizeof(u32));
  269. if (n <= 0 || n % 3)
  270. return -EINVAL;
  271. pdc_region_cnt = n / 3;
  272. pdc_region = kzalloc_objs(*pdc_region, pdc_region_cnt);
  273. if (!pdc_region) {
  274. pdc_region_cnt = 0;
  275. return -ENOMEM;
  276. }
  277. for (n = 0; n < pdc_region_cnt; n++) {
  278. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  279. n * 3 + 0,
  280. &pdc_region[n].pin_base);
  281. if (ret)
  282. return ret;
  283. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  284. n * 3 + 1,
  285. &pdc_region[n].parent_base);
  286. if (ret)
  287. return ret;
  288. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  289. n * 3 + 2,
  290. &pdc_region[n].cnt);
  291. if (ret)
  292. return ret;
  293. for (i = 0; i < pdc_region[n].cnt; i++)
  294. __pdc_enable_intr(i + pdc_region[n].pin_base, 0);
  295. }
  296. return 0;
  297. }
  298. #define QCOM_PDC_SIZE 0x30000
  299. static int qcom_pdc_probe(struct platform_device *pdev, struct device_node *parent)
  300. {
  301. struct irq_domain *parent_domain, *pdc_domain;
  302. struct device_node *node = pdev->dev.of_node;
  303. resource_size_t res_size;
  304. struct resource res;
  305. int ret;
  306. /* compat with old sm8150 DT which had very small region for PDC */
  307. if (of_address_to_resource(node, 0, &res))
  308. return -EINVAL;
  309. res_size = max_t(resource_size_t, resource_size(&res), QCOM_PDC_SIZE);
  310. if (res_size > resource_size(&res))
  311. pr_warn("%pOF: invalid reg size, please fix DT\n", node);
  312. /*
  313. * PDC has multiple DRV regions, each one provides the same set of
  314. * registers for a particular client in the system. Due to a hardware
  315. * bug on X1E, some writes to the IRQ_ENABLE_BANK register must be
  316. * issued inside the previous region. This region belongs to
  317. * a different client and is not described in the device tree. Map the
  318. * region with the expected offset to preserve support for old DTs.
  319. */
  320. if (of_device_is_compatible(node, "qcom,x1e80100-pdc")) {
  321. pdc_prev_base = ioremap(res.start - PDC_DRV_OFFSET, IRQ_ENABLE_BANK_MAX);
  322. if (!pdc_prev_base) {
  323. pr_err("%pOF: unable to map previous PDC DRV region\n", node);
  324. return -ENXIO;
  325. }
  326. pdc_x1e_quirk = true;
  327. }
  328. pdc_base = ioremap(res.start, res_size);
  329. if (!pdc_base) {
  330. pr_err("%pOF: unable to map PDC registers\n", node);
  331. ret = -ENXIO;
  332. goto fail;
  333. }
  334. pdc_version = pdc_reg_read(PDC_VERSION_REG, 0);
  335. parent_domain = irq_find_host(parent);
  336. if (!parent_domain) {
  337. pr_err("%pOF: unable to find PDC's parent domain\n", node);
  338. ret = -ENXIO;
  339. goto fail;
  340. }
  341. ret = pdc_setup_pin_mapping(node);
  342. if (ret) {
  343. pr_err("%pOF: failed to init PDC pin-hwirq mapping\n", node);
  344. goto fail;
  345. }
  346. pdc_domain = irq_domain_create_hierarchy(parent_domain,
  347. IRQ_DOMAIN_FLAG_QCOM_PDC_WAKEUP,
  348. PDC_MAX_GPIO_IRQS,
  349. of_fwnode_handle(node),
  350. &qcom_pdc_ops, NULL);
  351. if (!pdc_domain) {
  352. pr_err("%pOF: PDC domain add failed\n", node);
  353. ret = -ENOMEM;
  354. goto fail;
  355. }
  356. irq_domain_update_bus_token(pdc_domain, DOMAIN_BUS_WAKEUP);
  357. return 0;
  358. fail:
  359. kfree(pdc_region);
  360. iounmap(pdc_base);
  361. iounmap(pdc_prev_base);
  362. return ret;
  363. }
  364. IRQCHIP_PLATFORM_DRIVER_BEGIN(qcom_pdc)
  365. IRQCHIP_MATCH("qcom,pdc", qcom_pdc_probe)
  366. IRQCHIP_PLATFORM_DRIVER_END(qcom_pdc)
  367. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Power Domain Controller");
  368. MODULE_LICENSE("GPL v2");