meson-secure-pwrc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
  2. /*
  3. * Copyright (c) 2019 Amlogic, Inc.
  4. * Author: Jianxin Pan <[email protected]>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/io.h>
  8. #include <linux/of_device.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm_domain.h>
  11. #include <dt-bindings/power/meson-a1-power.h>
  12. #include <dt-bindings/power/meson-s4-power.h>
  13. #include <linux/arm-smccc.h>
  14. #include <linux/firmware/meson/meson_sm.h>
  15. #include <linux/module.h>
  16. #define PWRC_ON 1
  17. #define PWRC_OFF 0
  18. struct meson_secure_pwrc_domain {
  19. struct generic_pm_domain base;
  20. unsigned int index;
  21. struct meson_secure_pwrc *pwrc;
  22. };
  23. struct meson_secure_pwrc {
  24. struct meson_secure_pwrc_domain *domains;
  25. struct genpd_onecell_data xlate;
  26. struct meson_sm_firmware *fw;
  27. };
  28. struct meson_secure_pwrc_domain_desc {
  29. unsigned int index;
  30. unsigned int flags;
  31. char *name;
  32. bool (*is_off)(struct meson_secure_pwrc_domain *pwrc_domain);
  33. };
  34. struct meson_secure_pwrc_domain_data {
  35. unsigned int count;
  36. struct meson_secure_pwrc_domain_desc *domains;
  37. };
  38. static bool pwrc_secure_is_off(struct meson_secure_pwrc_domain *pwrc_domain)
  39. {
  40. int is_off = 1;
  41. if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_GET, &is_off,
  42. pwrc_domain->index, 0, 0, 0, 0) < 0)
  43. pr_err("failed to get power domain status\n");
  44. return is_off;
  45. }
  46. static int meson_secure_pwrc_off(struct generic_pm_domain *domain)
  47. {
  48. int ret = 0;
  49. struct meson_secure_pwrc_domain *pwrc_domain =
  50. container_of(domain, struct meson_secure_pwrc_domain, base);
  51. if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
  52. pwrc_domain->index, PWRC_OFF, 0, 0, 0) < 0) {
  53. pr_err("failed to set power domain off\n");
  54. ret = -EINVAL;
  55. }
  56. return ret;
  57. }
  58. static int meson_secure_pwrc_on(struct generic_pm_domain *domain)
  59. {
  60. int ret = 0;
  61. struct meson_secure_pwrc_domain *pwrc_domain =
  62. container_of(domain, struct meson_secure_pwrc_domain, base);
  63. if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
  64. pwrc_domain->index, PWRC_ON, 0, 0, 0) < 0) {
  65. pr_err("failed to set power domain on\n");
  66. ret = -EINVAL;
  67. }
  68. return ret;
  69. }
  70. #define SEC_PD(__name, __flag) \
  71. [PWRC_##__name##_ID] = \
  72. { \
  73. .name = #__name, \
  74. .index = PWRC_##__name##_ID, \
  75. .is_off = pwrc_secure_is_off, \
  76. .flags = __flag, \
  77. }
  78. static struct meson_secure_pwrc_domain_desc a1_pwrc_domains[] = {
  79. SEC_PD(DSPA, 0),
  80. SEC_PD(DSPB, 0),
  81. /* UART should keep working in ATF after suspend and before resume */
  82. SEC_PD(UART, GENPD_FLAG_ALWAYS_ON),
  83. /* DMC is for DDR PHY ana/dig and DMC, and should be always on */
  84. SEC_PD(DMC, GENPD_FLAG_ALWAYS_ON),
  85. SEC_PD(I2C, 0),
  86. SEC_PD(PSRAM, 0),
  87. SEC_PD(ACODEC, 0),
  88. SEC_PD(AUDIO, 0),
  89. SEC_PD(OTP, 0),
  90. SEC_PD(DMA, GENPD_FLAG_ALWAYS_ON | GENPD_FLAG_IRQ_SAFE),
  91. SEC_PD(SD_EMMC, 0),
  92. SEC_PD(RAMA, 0),
  93. /* SRAMB is used as ATF runtime memory, and should be always on */
  94. SEC_PD(RAMB, GENPD_FLAG_ALWAYS_ON),
  95. SEC_PD(IR, 0),
  96. SEC_PD(SPICC, 0),
  97. SEC_PD(SPIFC, 0),
  98. SEC_PD(USB, 0),
  99. /* NIC is for the Arm NIC-400 interconnect, and should be always on */
  100. SEC_PD(NIC, GENPD_FLAG_ALWAYS_ON),
  101. SEC_PD(PDMIN, 0),
  102. SEC_PD(RSA, 0),
  103. };
  104. static struct meson_secure_pwrc_domain_desc s4_pwrc_domains[] = {
  105. SEC_PD(S4_DOS_HEVC, 0),
  106. SEC_PD(S4_DOS_VDEC, 0),
  107. SEC_PD(S4_VPU_HDMI, 0),
  108. SEC_PD(S4_USB_COMB, 0),
  109. SEC_PD(S4_GE2D, 0),
  110. /* ETH is for ethernet online wakeup, and should be always on */
  111. SEC_PD(S4_ETH, GENPD_FLAG_ALWAYS_ON),
  112. SEC_PD(S4_DEMOD, 0),
  113. SEC_PD(S4_AUDIO, 0),
  114. };
  115. static int meson_secure_pwrc_probe(struct platform_device *pdev)
  116. {
  117. int i;
  118. struct device_node *sm_np;
  119. struct meson_secure_pwrc *pwrc;
  120. const struct meson_secure_pwrc_domain_data *match;
  121. match = of_device_get_match_data(&pdev->dev);
  122. if (!match) {
  123. dev_err(&pdev->dev, "failed to get match data\n");
  124. return -ENODEV;
  125. }
  126. sm_np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gxbb-sm");
  127. if (!sm_np) {
  128. dev_err(&pdev->dev, "no secure-monitor node\n");
  129. return -ENODEV;
  130. }
  131. pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
  132. if (!pwrc) {
  133. of_node_put(sm_np);
  134. return -ENOMEM;
  135. }
  136. pwrc->fw = meson_sm_get(sm_np);
  137. of_node_put(sm_np);
  138. if (!pwrc->fw)
  139. return -EPROBE_DEFER;
  140. pwrc->xlate.domains = devm_kcalloc(&pdev->dev, match->count,
  141. sizeof(*pwrc->xlate.domains),
  142. GFP_KERNEL);
  143. if (!pwrc->xlate.domains)
  144. return -ENOMEM;
  145. pwrc->domains = devm_kcalloc(&pdev->dev, match->count,
  146. sizeof(*pwrc->domains), GFP_KERNEL);
  147. if (!pwrc->domains)
  148. return -ENOMEM;
  149. pwrc->xlate.num_domains = match->count;
  150. platform_set_drvdata(pdev, pwrc);
  151. for (i = 0 ; i < match->count ; ++i) {
  152. struct meson_secure_pwrc_domain *dom = &pwrc->domains[i];
  153. if (!match->domains[i].index)
  154. continue;
  155. dom->pwrc = pwrc;
  156. dom->index = match->domains[i].index;
  157. dom->base.name = match->domains[i].name;
  158. dom->base.flags = match->domains[i].flags;
  159. dom->base.power_on = meson_secure_pwrc_on;
  160. dom->base.power_off = meson_secure_pwrc_off;
  161. pm_genpd_init(&dom->base, NULL, match->domains[i].is_off(dom));
  162. pwrc->xlate.domains[i] = &dom->base;
  163. }
  164. return of_genpd_add_provider_onecell(pdev->dev.of_node, &pwrc->xlate);
  165. }
  166. static struct meson_secure_pwrc_domain_data meson_secure_a1_pwrc_data = {
  167. .domains = a1_pwrc_domains,
  168. .count = ARRAY_SIZE(a1_pwrc_domains),
  169. };
  170. static struct meson_secure_pwrc_domain_data meson_secure_s4_pwrc_data = {
  171. .domains = s4_pwrc_domains,
  172. .count = ARRAY_SIZE(s4_pwrc_domains),
  173. };
  174. static const struct of_device_id meson_secure_pwrc_match_table[] = {
  175. {
  176. .compatible = "amlogic,meson-a1-pwrc",
  177. .data = &meson_secure_a1_pwrc_data,
  178. },
  179. {
  180. .compatible = "amlogic,meson-s4-pwrc",
  181. .data = &meson_secure_s4_pwrc_data,
  182. },
  183. { /* sentinel */ }
  184. };
  185. MODULE_DEVICE_TABLE(of, meson_secure_pwrc_match_table);
  186. static struct platform_driver meson_secure_pwrc_driver = {
  187. .probe = meson_secure_pwrc_probe,
  188. .driver = {
  189. .name = "meson_secure_pwrc",
  190. .of_match_table = meson_secure_pwrc_match_table,
  191. },
  192. };
  193. module_platform_driver(meson_secure_pwrc_driver);
  194. MODULE_LICENSE("Dual MIT/GPL");