rcpm.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // rcpm.c - Freescale QorIQ RCPM driver
  4. //
  5. // Copyright 2019-2020 NXP
  6. //
  7. // Author: Ran Wang <ran.wang_1@nxp.com>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/of_address.h>
  12. #include <linux/slab.h>
  13. #include <linux/suspend.h>
  14. #include <linux/kernel.h>
  15. #include <linux/acpi.h>
  16. #define RCPM_WAKEUP_CELL_MAX_SIZE 7
  17. struct rcpm {
  18. unsigned int wakeup_cells;
  19. void __iomem *ippdexpcr_base;
  20. bool little_endian;
  21. };
  22. #define SCFG_SPARECR8 0x051c
  23. static void copy_ippdexpcr1_setting(u32 val)
  24. {
  25. struct device_node *np;
  26. void __iomem *regs;
  27. u32 reg_val;
  28. np = of_find_compatible_node(NULL, NULL, "fsl,ls1021a-scfg");
  29. if (!np)
  30. return;
  31. regs = of_iomap(np, 0);
  32. of_node_put(np);
  33. if (!regs)
  34. return;
  35. reg_val = ioread32be(regs + SCFG_SPARECR8);
  36. iowrite32be(val | reg_val, regs + SCFG_SPARECR8);
  37. iounmap(regs);
  38. }
  39. /**
  40. * rcpm_pm_prepare - performs device-level tasks associated with power
  41. * management, such as programming related to the wakeup source control.
  42. * @dev: Device to handle.
  43. *
  44. */
  45. static int rcpm_pm_prepare(struct device *dev)
  46. {
  47. int i, ret, idx;
  48. void __iomem *base;
  49. struct wakeup_source *ws;
  50. struct rcpm *rcpm;
  51. struct device_node *np = dev->of_node;
  52. u32 value[RCPM_WAKEUP_CELL_MAX_SIZE + 1];
  53. u32 setting[RCPM_WAKEUP_CELL_MAX_SIZE] = {0};
  54. rcpm = dev_get_drvdata(dev);
  55. if (!rcpm)
  56. return -EINVAL;
  57. base = rcpm->ippdexpcr_base;
  58. idx = wakeup_sources_read_lock();
  59. /* Begin with first registered wakeup source */
  60. for_each_wakeup_source(ws) {
  61. /* skip object which is not attached to device */
  62. if (!ws->dev || !ws->dev->parent)
  63. continue;
  64. ret = device_property_read_u32_array(ws->dev->parent,
  65. "fsl,rcpm-wakeup", value,
  66. rcpm->wakeup_cells + 1);
  67. if (ret)
  68. continue;
  69. /*
  70. * For DT mode, would handle devices with "fsl,rcpm-wakeup"
  71. * pointing to the current RCPM node.
  72. *
  73. * For ACPI mode, currently we assume there is only one
  74. * RCPM controller existing.
  75. */
  76. if (is_of_node(dev->fwnode))
  77. if (np->phandle != value[0])
  78. continue;
  79. /* Property "#fsl,rcpm-wakeup-cells" of rcpm node defines the
  80. * number of IPPDEXPCR register cells, and "fsl,rcpm-wakeup"
  81. * of wakeup source IP contains an integer array: <phandle to
  82. * RCPM node, IPPDEXPCR0 setting, IPPDEXPCR1 setting,
  83. * IPPDEXPCR2 setting, etc>.
  84. *
  85. * So we will go thought them to collect setting data.
  86. */
  87. for (i = 0; i < rcpm->wakeup_cells; i++)
  88. setting[i] |= value[i + 1];
  89. }
  90. wakeup_sources_read_unlock(idx);
  91. /* Program all IPPDEXPCRn once */
  92. for (i = 0; i < rcpm->wakeup_cells; i++) {
  93. u32 tmp = setting[i];
  94. void __iomem *address = base + i * 4;
  95. if (!tmp)
  96. continue;
  97. /* We can only OR related bits */
  98. if (rcpm->little_endian) {
  99. tmp |= ioread32(address);
  100. iowrite32(tmp, address);
  101. } else {
  102. tmp |= ioread32be(address);
  103. iowrite32be(tmp, address);
  104. }
  105. /*
  106. * Workaround of errata A-008646 on SoC LS1021A:
  107. * There is a bug of register ippdexpcr1.
  108. * Reading configuration register RCPM_IPPDEXPCR1
  109. * always return zero. So save ippdexpcr1's value
  110. * to register SCFG_SPARECR8.And the value of
  111. * ippdexpcr1 will be read from SCFG_SPARECR8.
  112. */
  113. if (dev_of_node(dev) && (i == 1))
  114. if (of_device_is_compatible(np, "fsl,ls1021a-rcpm"))
  115. copy_ippdexpcr1_setting(tmp);
  116. }
  117. return 0;
  118. }
  119. static const struct dev_pm_ops rcpm_pm_ops = {
  120. .prepare = rcpm_pm_prepare,
  121. };
  122. static int rcpm_probe(struct platform_device *pdev)
  123. {
  124. struct device *dev = &pdev->dev;
  125. struct rcpm *rcpm;
  126. int ret;
  127. rcpm = devm_kzalloc(dev, sizeof(*rcpm), GFP_KERNEL);
  128. if (!rcpm)
  129. return -ENOMEM;
  130. rcpm->ippdexpcr_base = devm_platform_ioremap_resource(pdev, 0);
  131. if (IS_ERR(rcpm->ippdexpcr_base)) {
  132. ret = PTR_ERR(rcpm->ippdexpcr_base);
  133. return ret;
  134. }
  135. rcpm->little_endian = device_property_read_bool(
  136. &pdev->dev, "little-endian");
  137. ret = device_property_read_u32(&pdev->dev,
  138. "#fsl,rcpm-wakeup-cells", &rcpm->wakeup_cells);
  139. if (ret)
  140. return ret;
  141. dev_set_drvdata(&pdev->dev, rcpm);
  142. return 0;
  143. }
  144. static const struct of_device_id rcpm_of_match[] = {
  145. { .compatible = "fsl,qoriq-rcpm-2.1+", },
  146. {}
  147. };
  148. MODULE_DEVICE_TABLE(of, rcpm_of_match);
  149. #ifdef CONFIG_ACPI
  150. static const struct acpi_device_id rcpm_acpi_ids[] = {
  151. {"NXP0015",},
  152. { }
  153. };
  154. MODULE_DEVICE_TABLE(acpi, rcpm_acpi_ids);
  155. #endif
  156. static struct platform_driver rcpm_driver = {
  157. .driver = {
  158. .name = "rcpm",
  159. .of_match_table = rcpm_of_match,
  160. .acpi_match_table = ACPI_PTR(rcpm_acpi_ids),
  161. .pm = &rcpm_pm_ops,
  162. },
  163. .probe = rcpm_probe,
  164. };
  165. module_platform_driver(rcpm_driver);