irq-qcom-mpm.c 13 KB

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