irq-qcom-mpm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, Linaro Limited
  4. * Copyright (c) 2010-2020, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/err.h>
  8. #include <linux/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/irqchip.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/mailbox_client.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_domain.h>
  19. #include <linux/slab.h>
  20. #include <linux/soc/qcom/irq.h>
  21. #include <linux/spinlock.h>
  22. /*
  23. * This is the driver for Qualcomm MPM (MSM Power Manager) interrupt controller,
  24. * which is commonly found on Qualcomm SoCs built on the RPM architecture.
  25. * Sitting in always-on domain, MPM monitors the wakeup interrupts when SoC is
  26. * asleep, and wakes up the AP when one of those interrupts occurs. This driver
  27. * doesn't directly access physical MPM registers though. Instead, the access
  28. * is bridged via a piece of internal memory (SRAM) that is accessible to both
  29. * AP and RPM. This piece of memory is called 'vMPM' in the driver.
  30. *
  31. * When SoC is awake, the vMPM is owned by AP and the register setup by this
  32. * driver all happens on vMPM. When AP is about to get power collapsed, the
  33. * driver sends a mailbox notification to RPM, which will take over the vMPM
  34. * ownership and dump vMPM into physical MPM registers. On wakeup, AP is woken
  35. * up by a MPM pin/interrupt, and RPM will copy STATUS registers into vMPM.
  36. * Then AP start owning vMPM again.
  37. *
  38. * vMPM register map:
  39. *
  40. * 31 0
  41. * +--------------------------------+
  42. * | TIMER0 | 0x00
  43. * +--------------------------------+
  44. * | TIMER1 | 0x04
  45. * +--------------------------------+
  46. * | ENABLE0 | 0x08
  47. * +--------------------------------+
  48. * | ... | ...
  49. * +--------------------------------+
  50. * | ENABLEn |
  51. * +--------------------------------+
  52. * | FALLING_EDGE0 |
  53. * +--------------------------------+
  54. * | ... |
  55. * +--------------------------------+
  56. * | STATUSn |
  57. * +--------------------------------+
  58. *
  59. * n = DIV_ROUND_UP(pin_cnt, 32)
  60. *
  61. */
  62. #define MPM_REG_ENABLE 0
  63. #define MPM_REG_FALLING_EDGE 1
  64. #define MPM_REG_RISING_EDGE 2
  65. #define MPM_REG_POLARITY 3
  66. #define MPM_REG_STATUS 4
  67. /* MPM pin map to GIC hwirq */
  68. struct mpm_gic_map {
  69. int pin;
  70. irq_hw_number_t hwirq;
  71. };
  72. struct qcom_mpm_priv {
  73. void __iomem *base;
  74. raw_spinlock_t lock;
  75. struct mbox_client mbox_client;
  76. struct mbox_chan *mbox_chan;
  77. struct mpm_gic_map *maps;
  78. unsigned int map_cnt;
  79. unsigned int reg_stride;
  80. struct irq_domain *domain;
  81. struct generic_pm_domain genpd;
  82. };
  83. static u32 qcom_mpm_read(struct qcom_mpm_priv *priv, unsigned int reg,
  84. unsigned int index)
  85. {
  86. unsigned int offset = (reg * priv->reg_stride + index + 2) * 4;
  87. return readl_relaxed(priv->base + offset);
  88. }
  89. static void qcom_mpm_write(struct qcom_mpm_priv *priv, unsigned int reg,
  90. unsigned int index, u32 val)
  91. {
  92. unsigned int offset = (reg * priv->reg_stride + index + 2) * 4;
  93. writel_relaxed(val, priv->base + offset);
  94. /* Ensure the write is completed */
  95. wmb();
  96. }
  97. static void qcom_mpm_enable_irq(struct irq_data *d, bool en)
  98. {
  99. struct qcom_mpm_priv *priv = d->chip_data;
  100. int pin = d->hwirq;
  101. unsigned int index = pin / 32;
  102. unsigned int shift = pin % 32;
  103. unsigned long flags, val;
  104. raw_spin_lock_irqsave(&priv->lock, flags);
  105. val = qcom_mpm_read(priv, MPM_REG_ENABLE, index);
  106. __assign_bit(shift, &val, en);
  107. qcom_mpm_write(priv, MPM_REG_ENABLE, index, val);
  108. raw_spin_unlock_irqrestore(&priv->lock, flags);
  109. }
  110. static void qcom_mpm_mask(struct irq_data *d)
  111. {
  112. qcom_mpm_enable_irq(d, false);
  113. if (d->parent_data)
  114. irq_chip_mask_parent(d);
  115. }
  116. static void qcom_mpm_unmask(struct irq_data *d)
  117. {
  118. qcom_mpm_enable_irq(d, true);
  119. if (d->parent_data)
  120. irq_chip_unmask_parent(d);
  121. }
  122. static void mpm_set_type(struct qcom_mpm_priv *priv, bool set, unsigned int reg,
  123. unsigned int index, unsigned int shift)
  124. {
  125. unsigned long flags, val;
  126. raw_spin_lock_irqsave(&priv->lock, flags);
  127. val = qcom_mpm_read(priv, reg, index);
  128. __assign_bit(shift, &val, set);
  129. qcom_mpm_write(priv, reg, index, val);
  130. raw_spin_unlock_irqrestore(&priv->lock, flags);
  131. }
  132. static int qcom_mpm_set_type(struct irq_data *d, unsigned int type)
  133. {
  134. struct qcom_mpm_priv *priv = d->chip_data;
  135. int pin = d->hwirq;
  136. unsigned int index = pin / 32;
  137. unsigned int shift = pin % 32;
  138. if (type & IRQ_TYPE_EDGE_RISING)
  139. mpm_set_type(priv, true, MPM_REG_RISING_EDGE, index, shift);
  140. else
  141. mpm_set_type(priv, false, MPM_REG_RISING_EDGE, index, shift);
  142. if (type & IRQ_TYPE_EDGE_FALLING)
  143. mpm_set_type(priv, true, MPM_REG_FALLING_EDGE, index, shift);
  144. else
  145. mpm_set_type(priv, false, MPM_REG_FALLING_EDGE, index, shift);
  146. if (type & IRQ_TYPE_LEVEL_HIGH)
  147. mpm_set_type(priv, true, MPM_REG_POLARITY, index, shift);
  148. else
  149. mpm_set_type(priv, false, MPM_REG_POLARITY, index, shift);
  150. if (!d->parent_data)
  151. return 0;
  152. if (type & IRQ_TYPE_EDGE_BOTH)
  153. type = IRQ_TYPE_EDGE_RISING;
  154. if (type & IRQ_TYPE_LEVEL_MASK)
  155. type = IRQ_TYPE_LEVEL_HIGH;
  156. return irq_chip_set_type_parent(d, type);
  157. }
  158. static struct irq_chip qcom_mpm_chip = {
  159. .name = "mpm",
  160. .irq_eoi = irq_chip_eoi_parent,
  161. .irq_mask = qcom_mpm_mask,
  162. .irq_unmask = qcom_mpm_unmask,
  163. .irq_retrigger = irq_chip_retrigger_hierarchy,
  164. .irq_set_type = qcom_mpm_set_type,
  165. .irq_set_affinity = irq_chip_set_affinity_parent,
  166. .flags = IRQCHIP_MASK_ON_SUSPEND |
  167. IRQCHIP_SKIP_SET_WAKE,
  168. };
  169. static struct mpm_gic_map *get_mpm_gic_map(struct qcom_mpm_priv *priv, int pin)
  170. {
  171. struct mpm_gic_map *maps = priv->maps;
  172. int i;
  173. for (i = 0; i < priv->map_cnt; i++) {
  174. if (maps[i].pin == pin)
  175. return &maps[i];
  176. }
  177. return NULL;
  178. }
  179. static int qcom_mpm_alloc(struct irq_domain *domain, unsigned int virq,
  180. unsigned int nr_irqs, void *data)
  181. {
  182. struct qcom_mpm_priv *priv = domain->host_data;
  183. struct irq_fwspec *fwspec = data;
  184. struct irq_fwspec parent_fwspec;
  185. struct mpm_gic_map *map;
  186. irq_hw_number_t pin;
  187. unsigned int type;
  188. int ret;
  189. ret = irq_domain_translate_twocell(domain, fwspec, &pin, &type);
  190. if (ret)
  191. return ret;
  192. ret = irq_domain_set_hwirq_and_chip(domain, virq, pin,
  193. &qcom_mpm_chip, priv);
  194. if (ret)
  195. return ret;
  196. map = get_mpm_gic_map(priv, pin);
  197. if (map == NULL)
  198. return irq_domain_disconnect_hierarchy(domain->parent, virq);
  199. if (type & IRQ_TYPE_EDGE_BOTH)
  200. type = IRQ_TYPE_EDGE_RISING;
  201. if (type & IRQ_TYPE_LEVEL_MASK)
  202. type = IRQ_TYPE_LEVEL_HIGH;
  203. parent_fwspec.fwnode = domain->parent->fwnode;
  204. parent_fwspec.param_count = 3;
  205. parent_fwspec.param[0] = 0;
  206. parent_fwspec.param[1] = map->hwirq;
  207. parent_fwspec.param[2] = type;
  208. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
  209. &parent_fwspec);
  210. }
  211. static const struct irq_domain_ops qcom_mpm_ops = {
  212. .alloc = qcom_mpm_alloc,
  213. .free = irq_domain_free_irqs_common,
  214. .translate = irq_domain_translate_twocell,
  215. };
  216. /* Triggered by RPM when system resumes from deep sleep */
  217. static irqreturn_t qcom_mpm_handler(int irq, void *dev_id)
  218. {
  219. struct qcom_mpm_priv *priv = dev_id;
  220. unsigned long enable, pending;
  221. irqreturn_t ret = IRQ_NONE;
  222. unsigned long flags;
  223. int i, j;
  224. for (i = 0; i < priv->reg_stride; i++) {
  225. raw_spin_lock_irqsave(&priv->lock, flags);
  226. enable = qcom_mpm_read(priv, MPM_REG_ENABLE, i);
  227. pending = qcom_mpm_read(priv, MPM_REG_STATUS, i);
  228. pending &= enable;
  229. raw_spin_unlock_irqrestore(&priv->lock, flags);
  230. for_each_set_bit(j, &pending, 32) {
  231. unsigned int pin = 32 * i + j;
  232. struct irq_desc *desc = irq_resolve_mapping(priv->domain, pin);
  233. struct irq_data *d = &desc->irq_data;
  234. if (!irqd_is_level_type(d))
  235. irq_set_irqchip_state(d->irq,
  236. IRQCHIP_STATE_PENDING, true);
  237. ret = IRQ_HANDLED;
  238. }
  239. }
  240. return ret;
  241. }
  242. static int mpm_pd_power_off(struct generic_pm_domain *genpd)
  243. {
  244. struct qcom_mpm_priv *priv = container_of(genpd, struct qcom_mpm_priv,
  245. genpd);
  246. int i, ret;
  247. for (i = 0; i < priv->reg_stride; i++)
  248. qcom_mpm_write(priv, MPM_REG_STATUS, i, 0);
  249. /* Notify RPM to write vMPM into HW */
  250. ret = mbox_send_message(priv->mbox_chan, NULL);
  251. if (ret < 0)
  252. return ret;
  253. return 0;
  254. }
  255. static bool gic_hwirq_is_mapped(struct mpm_gic_map *maps, int cnt, u32 hwirq)
  256. {
  257. int i;
  258. for (i = 0; i < cnt; i++)
  259. if (maps[i].hwirq == hwirq)
  260. return true;
  261. return false;
  262. }
  263. static int qcom_mpm_init(struct device_node *np, struct device_node *parent)
  264. {
  265. struct platform_device *pdev = of_find_device_by_node(np);
  266. struct device *dev = &pdev->dev;
  267. struct irq_domain *parent_domain;
  268. struct generic_pm_domain *genpd;
  269. struct qcom_mpm_priv *priv;
  270. unsigned int pin_cnt;
  271. int i, irq;
  272. int ret;
  273. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  274. if (!priv)
  275. return -ENOMEM;
  276. ret = of_property_read_u32(np, "qcom,mpm-pin-count", &pin_cnt);
  277. if (ret) {
  278. dev_err(dev, "failed to read qcom,mpm-pin-count: %d\n", ret);
  279. return ret;
  280. }
  281. priv->reg_stride = DIV_ROUND_UP(pin_cnt, 32);
  282. ret = of_property_count_u32_elems(np, "qcom,mpm-pin-map");
  283. if (ret < 0) {
  284. dev_err(dev, "failed to read qcom,mpm-pin-map: %d\n", ret);
  285. return ret;
  286. }
  287. if (ret % 2) {
  288. dev_err(dev, "invalid qcom,mpm-pin-map\n");
  289. return -EINVAL;
  290. }
  291. priv->map_cnt = ret / 2;
  292. priv->maps = devm_kcalloc(dev, priv->map_cnt, sizeof(*priv->maps),
  293. GFP_KERNEL);
  294. if (!priv->maps)
  295. return -ENOMEM;
  296. for (i = 0; i < priv->map_cnt; i++) {
  297. u32 pin, hwirq;
  298. of_property_read_u32_index(np, "qcom,mpm-pin-map", i * 2, &pin);
  299. of_property_read_u32_index(np, "qcom,mpm-pin-map", i * 2 + 1, &hwirq);
  300. if (gic_hwirq_is_mapped(priv->maps, i, hwirq)) {
  301. dev_warn(dev, "failed to map pin %d as GIC hwirq %d is already mapped\n",
  302. pin, hwirq);
  303. continue;
  304. }
  305. priv->maps[i].pin = pin;
  306. priv->maps[i].hwirq = hwirq;
  307. }
  308. raw_spin_lock_init(&priv->lock);
  309. priv->base = devm_platform_ioremap_resource(pdev, 0);
  310. if (IS_ERR(priv->base))
  311. return PTR_ERR(priv->base);
  312. for (i = 0; i < priv->reg_stride; i++) {
  313. qcom_mpm_write(priv, MPM_REG_ENABLE, i, 0);
  314. qcom_mpm_write(priv, MPM_REG_FALLING_EDGE, i, 0);
  315. qcom_mpm_write(priv, MPM_REG_RISING_EDGE, i, 0);
  316. qcom_mpm_write(priv, MPM_REG_POLARITY, i, 0);
  317. qcom_mpm_write(priv, MPM_REG_STATUS, i, 0);
  318. }
  319. irq = platform_get_irq(pdev, 0);
  320. if (irq < 0)
  321. return irq;
  322. genpd = &priv->genpd;
  323. genpd->flags = GENPD_FLAG_IRQ_SAFE;
  324. genpd->power_off = mpm_pd_power_off;
  325. genpd->name = devm_kasprintf(dev, GFP_KERNEL, "%s", dev_name(dev));
  326. if (!genpd->name)
  327. return -ENOMEM;
  328. ret = pm_genpd_init(genpd, NULL, false);
  329. if (ret) {
  330. dev_err(dev, "failed to init genpd: %d\n", ret);
  331. return ret;
  332. }
  333. ret = of_genpd_add_provider_simple(np, genpd);
  334. if (ret) {
  335. dev_err(dev, "failed to add genpd provider: %d\n", ret);
  336. goto remove_genpd;
  337. }
  338. priv->mbox_client.dev = dev;
  339. priv->mbox_chan = mbox_request_channel(&priv->mbox_client, 0);
  340. if (IS_ERR(priv->mbox_chan)) {
  341. ret = PTR_ERR(priv->mbox_chan);
  342. dev_err(dev, "failed to acquire IPC channel: %d\n", ret);
  343. return ret;
  344. }
  345. parent_domain = irq_find_host(parent);
  346. if (!parent_domain) {
  347. dev_err(dev, "failed to find MPM parent domain\n");
  348. ret = -ENXIO;
  349. goto free_mbox;
  350. }
  351. priv->domain = irq_domain_create_hierarchy(parent_domain,
  352. IRQ_DOMAIN_FLAG_QCOM_MPM_WAKEUP, pin_cnt,
  353. of_node_to_fwnode(np), &qcom_mpm_ops, priv);
  354. if (!priv->domain) {
  355. dev_err(dev, "failed to create MPM domain\n");
  356. ret = -ENOMEM;
  357. goto free_mbox;
  358. }
  359. irq_domain_update_bus_token(priv->domain, DOMAIN_BUS_WAKEUP);
  360. ret = devm_request_irq(dev, irq, qcom_mpm_handler, IRQF_NO_SUSPEND,
  361. "qcom_mpm", priv);
  362. if (ret) {
  363. dev_err(dev, "failed to request irq: %d\n", ret);
  364. goto remove_domain;
  365. }
  366. return 0;
  367. remove_domain:
  368. irq_domain_remove(priv->domain);
  369. free_mbox:
  370. mbox_free_channel(priv->mbox_chan);
  371. remove_genpd:
  372. pm_genpd_remove(genpd);
  373. return ret;
  374. }
  375. IRQCHIP_PLATFORM_DRIVER_BEGIN(qcom_mpm)
  376. IRQCHIP_MATCH("qcom,mpm", qcom_mpm_init)
  377. IRQCHIP_PLATFORM_DRIVER_END(qcom_mpm)
  378. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. MSM Power Manager");
  379. MODULE_LICENSE("GPL v2");