qcom-pdc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/err.h>
  7. #include <linux/init.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/irq.h>
  10. #include <linux/irqchip.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_device.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/soc/qcom/irq.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/slab.h>
  22. #include <linux/types.h>
  23. #include <linux/qcom_scm.h>
  24. #include <linux/ipc_logging.h>
  25. #define PDC_IPC_LOG_SZ 2
  26. #define PDC_MAX_GPIO_IRQS 256
  27. #define IRQ_ENABLE_BANK 0x10
  28. #define IRQ_i_CFG 0x110
  29. #define IRQ_i_CFG_IRQ_ENABLE 3
  30. #define IRQ_i_CFG_TYPE_MASK 0x7
  31. #define VERSION 0x1000
  32. #define MAJOR_VER_MASK 0xFF
  33. #define MAJOR_VER_SHIFT 16
  34. #define MINOR_VER_MASK 0xFF
  35. #define MINOR_VER_SHIFT 8
  36. struct pdc_pin_region {
  37. u32 pin_base;
  38. u32 parent_base;
  39. u32 cnt;
  40. };
  41. struct spi_cfg_regs {
  42. union {
  43. u64 start;
  44. void __iomem *base;
  45. };
  46. resource_size_t size;
  47. bool scm_io;
  48. };
  49. #define pin_to_hwirq(r, p) ((r)->parent_base + (p) - (r)->pin_base)
  50. static DEFINE_RAW_SPINLOCK(pdc_lock);
  51. static void __iomem *pdc_base;
  52. static struct pdc_pin_region *pdc_region;
  53. static int pdc_region_cnt;
  54. static struct spi_cfg_regs *spi_cfg;
  55. static void *pdc_ipc_log;
  56. static bool enable_in_cfg;
  57. static u32 __spi_pin_read(unsigned int pin)
  58. {
  59. void __iomem *cfg_reg = spi_cfg->base + pin * 4;
  60. u64 scm_cfg_reg = spi_cfg->start + pin * 4;
  61. if (spi_cfg->scm_io) {
  62. unsigned int val;
  63. qcom_scm_io_readl(scm_cfg_reg, &val);
  64. return val;
  65. } else {
  66. return readl(cfg_reg);
  67. }
  68. }
  69. static void __spi_pin_write(unsigned int pin, unsigned int val)
  70. {
  71. void __iomem *cfg_reg = spi_cfg->base + pin * 4;
  72. u64 scm_cfg_reg = spi_cfg->start + pin * 4;
  73. if (spi_cfg->scm_io)
  74. qcom_scm_io_writel(scm_cfg_reg, val);
  75. else
  76. writel(val, cfg_reg);
  77. }
  78. static int spi_configure_type(irq_hw_number_t hwirq, unsigned int type)
  79. {
  80. int spi = hwirq - 32;
  81. u32 pin = spi / 32;
  82. u32 mask = BIT(spi % 32);
  83. u32 val;
  84. unsigned long flags;
  85. if (!spi_cfg)
  86. return 0;
  87. if (pin * 4 > spi_cfg->size)
  88. return -EFAULT;
  89. raw_spin_lock_irqsave(&pdc_lock, flags);
  90. val = __spi_pin_read(pin);
  91. val &= ~mask;
  92. if (type & IRQ_TYPE_LEVEL_MASK)
  93. val |= mask;
  94. __spi_pin_write(pin, val);
  95. ipc_log_string(pdc_ipc_log,
  96. "SPI config: GIC-SPI=%d (reg=%d,bit=%d) val=%d",
  97. spi, pin, spi % 32, type & IRQ_TYPE_LEVEL_MASK);
  98. raw_spin_unlock_irqrestore(&pdc_lock, flags);
  99. return 0;
  100. }
  101. static void pdc_reg_write(int reg, u32 i, u32 val)
  102. {
  103. writel_relaxed(val, pdc_base + reg + i * sizeof(u32));
  104. }
  105. static u32 pdc_reg_read(int reg, u32 i)
  106. {
  107. return readl_relaxed(pdc_base + reg + i * sizeof(u32));
  108. }
  109. static void pdc_enable_intr(struct irq_data *d, bool on)
  110. {
  111. int pin_out = d->hwirq;
  112. unsigned long enable;
  113. unsigned long flags;
  114. u32 index, mask;
  115. raw_spin_lock_irqsave(&pdc_lock, flags);
  116. if (!enable_in_cfg) {
  117. index = pin_out / 32;
  118. mask = pin_out % 32;
  119. enable = pdc_reg_read(IRQ_ENABLE_BANK, index);
  120. __assign_bit(mask, &enable, on);
  121. pdc_reg_write(IRQ_ENABLE_BANK, index, enable);
  122. } else {
  123. index = d->hwirq;
  124. enable = pdc_reg_read(IRQ_i_CFG, index);
  125. __assign_bit(IRQ_i_CFG_IRQ_ENABLE, &enable, on);
  126. pdc_reg_write(IRQ_i_CFG, index, enable);
  127. }
  128. raw_spin_unlock_irqrestore(&pdc_lock, flags);
  129. ipc_log_string(pdc_ipc_log, "PIN=%lu enable=%d", d->hwirq, on);
  130. }
  131. static void qcom_pdc_gic_disable(struct irq_data *d)
  132. {
  133. pdc_enable_intr(d, false);
  134. irq_chip_disable_parent(d);
  135. }
  136. static void qcom_pdc_gic_enable(struct irq_data *d)
  137. {
  138. pdc_enable_intr(d, true);
  139. irq_chip_enable_parent(d);
  140. }
  141. /*
  142. * GIC does not handle falling edge or active low. To allow falling edge and
  143. * active low interrupts to be handled at GIC, PDC has an inverter that inverts
  144. * falling edge into a rising edge and active low into an active high.
  145. * For the inverter to work, the polarity bit in the IRQ_CONFIG register has to
  146. * set as per the table below.
  147. * Level sensitive active low LOW
  148. * Rising edge sensitive NOT USED
  149. * Falling edge sensitive LOW
  150. * Dual Edge sensitive NOT USED
  151. * Level sensitive active High HIGH
  152. * Falling Edge sensitive NOT USED
  153. * Rising edge sensitive HIGH
  154. * Dual Edge sensitive HIGH
  155. */
  156. enum pdc_irq_config_bits {
  157. PDC_LEVEL_LOW = 0b000,
  158. PDC_EDGE_FALLING = 0b010,
  159. PDC_LEVEL_HIGH = 0b100,
  160. PDC_EDGE_RISING = 0b110,
  161. PDC_EDGE_DUAL = 0b111,
  162. };
  163. /**
  164. * qcom_pdc_gic_set_type: Configure PDC for the interrupt
  165. *
  166. * @d: the interrupt data
  167. * @type: the interrupt type
  168. *
  169. * If @type is edge triggered, forward that as Rising edge as PDC
  170. * takes care of converting falling edge to rising edge signal
  171. * If @type is level, then forward that as level high as PDC
  172. * takes care of converting falling edge to rising edge signal
  173. */
  174. static int qcom_pdc_gic_set_type(struct irq_data *d, unsigned int type)
  175. {
  176. int parent_hwirq = d->parent_data->hwirq;
  177. enum pdc_irq_config_bits pdc_type;
  178. enum pdc_irq_config_bits old_pdc_type;
  179. int ret;
  180. switch (type) {
  181. case IRQ_TYPE_EDGE_RISING:
  182. pdc_type = PDC_EDGE_RISING;
  183. break;
  184. case IRQ_TYPE_EDGE_FALLING:
  185. pdc_type = PDC_EDGE_FALLING;
  186. type = IRQ_TYPE_EDGE_RISING;
  187. break;
  188. case IRQ_TYPE_EDGE_BOTH:
  189. pdc_type = PDC_EDGE_DUAL;
  190. type = IRQ_TYPE_EDGE_RISING;
  191. break;
  192. case IRQ_TYPE_LEVEL_HIGH:
  193. pdc_type = PDC_LEVEL_HIGH;
  194. break;
  195. case IRQ_TYPE_LEVEL_LOW:
  196. pdc_type = PDC_LEVEL_LOW;
  197. type = IRQ_TYPE_LEVEL_HIGH;
  198. break;
  199. default:
  200. WARN_ON(1);
  201. return -EINVAL;
  202. }
  203. old_pdc_type = pdc_reg_read(IRQ_i_CFG, d->hwirq);
  204. pdc_type |= (old_pdc_type & ~IRQ_i_CFG_TYPE_MASK);
  205. pdc_reg_write(IRQ_i_CFG, d->hwirq, pdc_type);
  206. ipc_log_string(pdc_ipc_log, "Set type: PIN=%lu pdc_type=%d gic_type=%d",
  207. d->hwirq, pdc_type, type);
  208. /* Additionally, configure (only) the GPIO in the f/w */
  209. ret = spi_configure_type(parent_hwirq, type);
  210. if (ret)
  211. return ret;
  212. ret = irq_chip_set_type_parent(d, type);
  213. if (ret)
  214. return ret;
  215. /*
  216. * When we change types the PDC can give a phantom interrupt.
  217. * Clear it. Specifically the phantom shows up when reconfiguring
  218. * polarity of interrupt without changing the state of the signal
  219. * but let's be consistent and clear it always.
  220. *
  221. * Doing this works because we have IRQCHIP_SET_TYPE_MASKED so the
  222. * interrupt will be cleared before the rest of the system sees it.
  223. */
  224. if (old_pdc_type != pdc_type)
  225. irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
  226. return 0;
  227. }
  228. static struct irq_chip qcom_pdc_gic_chip = {
  229. .name = "PDC",
  230. .irq_eoi = irq_chip_eoi_parent,
  231. .irq_mask = irq_chip_mask_parent,
  232. .irq_unmask = irq_chip_unmask_parent,
  233. .irq_disable = qcom_pdc_gic_disable,
  234. .irq_enable = qcom_pdc_gic_enable,
  235. .irq_get_irqchip_state = irq_chip_get_parent_state,
  236. .irq_set_irqchip_state = irq_chip_set_parent_state,
  237. .irq_retrigger = irq_chip_retrigger_hierarchy,
  238. .irq_set_type = qcom_pdc_gic_set_type,
  239. .flags = IRQCHIP_MASK_ON_SUSPEND |
  240. IRQCHIP_SET_TYPE_MASKED |
  241. IRQCHIP_SKIP_SET_WAKE |
  242. IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND,
  243. .irq_set_vcpu_affinity = irq_chip_set_vcpu_affinity_parent,
  244. .irq_set_affinity = irq_chip_set_affinity_parent,
  245. };
  246. static struct pdc_pin_region *get_pin_region(int pin)
  247. {
  248. int i;
  249. for (i = 0; i < pdc_region_cnt; i++) {
  250. if (pin >= pdc_region[i].pin_base &&
  251. pin < pdc_region[i].pin_base + pdc_region[i].cnt)
  252. return &pdc_region[i];
  253. }
  254. return NULL;
  255. }
  256. static int qcom_pdc_alloc(struct irq_domain *domain, unsigned int virq,
  257. unsigned int nr_irqs, void *data)
  258. {
  259. struct irq_fwspec *fwspec = data;
  260. struct irq_fwspec parent_fwspec;
  261. struct pdc_pin_region *region;
  262. irq_hw_number_t hwirq;
  263. unsigned int type;
  264. int ret;
  265. ret = irq_domain_translate_twocell(domain, fwspec, &hwirq, &type);
  266. if (ret)
  267. return ret;
  268. if (hwirq == GPIO_NO_WAKE_IRQ)
  269. return irq_domain_disconnect_hierarchy(domain, virq);
  270. ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  271. &qcom_pdc_gic_chip, NULL);
  272. if (ret)
  273. return ret;
  274. region = get_pin_region(hwirq);
  275. if (!region)
  276. return irq_domain_disconnect_hierarchy(domain->parent, virq);
  277. if (type & IRQ_TYPE_EDGE_BOTH)
  278. type = IRQ_TYPE_EDGE_RISING;
  279. if (type & IRQ_TYPE_LEVEL_MASK)
  280. type = IRQ_TYPE_LEVEL_HIGH;
  281. parent_fwspec.fwnode = domain->parent->fwnode;
  282. parent_fwspec.param_count = 3;
  283. parent_fwspec.param[0] = 0;
  284. parent_fwspec.param[1] = pin_to_hwirq(region, hwirq);
  285. parent_fwspec.param[2] = type;
  286. ipc_log_string(pdc_ipc_log, "Alloc: PIN=%lu", hwirq);
  287. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
  288. &parent_fwspec);
  289. }
  290. static const struct irq_domain_ops qcom_pdc_ops = {
  291. .translate = irq_domain_translate_twocell,
  292. .alloc = qcom_pdc_alloc,
  293. .free = irq_domain_free_irqs_common,
  294. };
  295. static int pdc_setup_pin_mapping(struct device_node *np)
  296. {
  297. int ret, n, i, last_region;
  298. u32 irq_index, reg_index, val, max_irq;
  299. n = of_property_count_elems_of_size(np, "qcom,pdc-ranges", sizeof(u32));
  300. if (n <= 0 || n % 3)
  301. return -EINVAL;
  302. pdc_region_cnt = n / 3;
  303. pdc_region = kcalloc(pdc_region_cnt, sizeof(*pdc_region), GFP_KERNEL);
  304. if (!pdc_region) {
  305. pdc_region_cnt = 0;
  306. return -ENOMEM;
  307. }
  308. for (n = 0; n < pdc_region_cnt; n++) {
  309. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  310. n * 3 + 0,
  311. &pdc_region[n].pin_base);
  312. if (ret)
  313. return ret;
  314. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  315. n * 3 + 1,
  316. &pdc_region[n].parent_base);
  317. if (ret)
  318. return ret;
  319. ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
  320. n * 3 + 2,
  321. &pdc_region[n].cnt);
  322. if (ret)
  323. return ret;
  324. }
  325. last_region = pdc_region_cnt - 1;
  326. max_irq = pdc_region[last_region].pin_base + pdc_region[last_region].cnt;
  327. if (!enable_in_cfg) {
  328. for (i = 0; i < max_irq; i++) {
  329. reg_index = (i + pdc_region[n].pin_base) >> 5;
  330. irq_index = (i + pdc_region[n].pin_base) & 0x1f;
  331. val = pdc_reg_read(IRQ_ENABLE_BANK, reg_index);
  332. val &= ~BIT(irq_index);
  333. pdc_reg_write(IRQ_ENABLE_BANK, reg_index, val);
  334. }
  335. } else {
  336. for (i = 0; i < max_irq; i++) {
  337. val = pdc_reg_read(IRQ_i_CFG, i);
  338. val &= ~BIT(IRQ_i_CFG_IRQ_ENABLE);
  339. pdc_reg_write(IRQ_i_CFG, i, val);
  340. }
  341. }
  342. return 0;
  343. }
  344. static int qcom_pdc_init(struct device_node *node, struct device_node *parent)
  345. {
  346. struct irq_domain *parent_domain, *pdc_domain;
  347. struct resource res;
  348. int ret;
  349. u32 version, major_ver, minor_ver;
  350. pdc_base = of_iomap(node, 0);
  351. if (!pdc_base) {
  352. pr_err("%pOF: unable to map PDC registers\n", node);
  353. return -ENXIO;
  354. }
  355. version = pdc_reg_read(VERSION, 0);
  356. major_ver = version & (MAJOR_VER_MASK << MAJOR_VER_SHIFT);
  357. major_ver >>= MAJOR_VER_SHIFT;
  358. minor_ver = version & (MINOR_VER_MASK << MINOR_VER_SHIFT);
  359. minor_ver >>= MINOR_VER_SHIFT;
  360. if (major_ver >= 3 && minor_ver > 1)
  361. enable_in_cfg = true;
  362. else
  363. enable_in_cfg = false;
  364. parent_domain = irq_find_host(parent);
  365. if (!parent_domain) {
  366. pr_err("%pOF: unable to find PDC's parent domain\n", node);
  367. ret = -ENXIO;
  368. goto fail;
  369. }
  370. ret = pdc_setup_pin_mapping(node);
  371. if (ret) {
  372. pr_err("%pOF: failed to init PDC pin-hwirq mapping\n", node);
  373. goto fail;
  374. }
  375. ret = of_address_to_resource(node, 1, &res);
  376. if (!ret) {
  377. spi_cfg = kcalloc(1, sizeof(*spi_cfg), GFP_KERNEL);
  378. if (!spi_cfg) {
  379. ret = -ENOMEM;
  380. goto fail;
  381. }
  382. spi_cfg->scm_io = of_find_property(node,
  383. "qcom,scm-spi-cfg", NULL);
  384. spi_cfg->size = resource_size(&res);
  385. if (spi_cfg->scm_io) {
  386. spi_cfg->start = res.start;
  387. } else {
  388. spi_cfg->base = ioremap(res.start, spi_cfg->size);
  389. if (!spi_cfg->base) {
  390. ret = -ENOMEM;
  391. goto fail;
  392. }
  393. }
  394. }
  395. pdc_domain = irq_domain_create_hierarchy(parent_domain,
  396. IRQ_DOMAIN_FLAG_QCOM_PDC_WAKEUP,
  397. PDC_MAX_GPIO_IRQS,
  398. of_fwnode_handle(node),
  399. &qcom_pdc_ops, NULL);
  400. if (!pdc_domain) {
  401. pr_err("%pOF: PDC domain add failed\n", node);
  402. ret = -ENOMEM;
  403. if (spi_cfg && spi_cfg->base)
  404. iounmap(spi_cfg->base);
  405. goto fail;
  406. }
  407. irq_domain_update_bus_token(pdc_domain, DOMAIN_BUS_WAKEUP);
  408. pdc_ipc_log = ipc_log_context_create(PDC_IPC_LOG_SZ, "pdc", 0);
  409. return 0;
  410. fail:
  411. if (spi_cfg)
  412. kfree(spi_cfg);
  413. if (pdc_region)
  414. kfree(pdc_region);
  415. iounmap(pdc_base);
  416. return ret;
  417. }
  418. IRQCHIP_PLATFORM_DRIVER_BEGIN(qcom_pdc)
  419. IRQCHIP_MATCH("qcom,pdc", qcom_pdc_init)
  420. IRQCHIP_PLATFORM_DRIVER_END(qcom_pdc)
  421. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Power Domain Controller");
  422. MODULE_LICENSE("GPL v2");