rcpm.c 4.5 KB

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