renesas-cpg-mssr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Renesas RCar Gen3 CPG MSSR driver
  4. *
  5. * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
  6. *
  7. * Based on the following driver from Linux kernel:
  8. * r8a7796 Clock Pulse Generator / Module Standby and Software Reset
  9. *
  10. * Copyright (C) 2016 Glider bvba
  11. */
  12. #include <common.h>
  13. #include <clk-uclass.h>
  14. #include <dm.h>
  15. #include <errno.h>
  16. #include <wait_bit.h>
  17. #include <asm/io.h>
  18. #include <dt-bindings/clock/renesas-cpg-mssr.h>
  19. #include "renesas-cpg-mssr.h"
  20. /*
  21. * Module Standby and Software Reset register offets.
  22. *
  23. * If the registers exist, these are valid for SH-Mobile, R-Mobile,
  24. * R-Car Gen2, R-Car Gen3, and RZ/G1.
  25. * These are NOT valid for R-Car Gen1 and RZ/A1!
  26. */
  27. /*
  28. * Module Stop Status Register offsets
  29. */
  30. static const u16 mstpsr[] = {
  31. 0x030, 0x038, 0x040, 0x048, 0x04C, 0x03C, 0x1C0, 0x1C4,
  32. 0x9A0, 0x9A4, 0x9A8, 0x9AC,
  33. };
  34. #define MSTPSR(i) mstpsr[i]
  35. /*
  36. * System Module Stop Control Register offsets
  37. */
  38. static const u16 smstpcr[] = {
  39. 0x130, 0x134, 0x138, 0x13C, 0x140, 0x144, 0x148, 0x14C,
  40. 0x990, 0x994, 0x998, 0x99C,
  41. };
  42. #define SMSTPCR(i) smstpcr[i]
  43. /* Realtime Module Stop Control Register offsets */
  44. #define RMSTPCR(i) (smstpcr[i] - 0x20)
  45. /* Modem Module Stop Control Register offsets (r8a73a4) */
  46. #define MMSTPCR(i) (smstpcr[i] + 0x20)
  47. /* Software Reset Clearing Register offsets */
  48. #define SRSTCLR(i) (0x940 + (i) * 4)
  49. bool renesas_clk_is_mod(struct clk *clk)
  50. {
  51. return (clk->id >> 16) == CPG_MOD;
  52. }
  53. int renesas_clk_get_mod(struct clk *clk, struct cpg_mssr_info *info,
  54. const struct mssr_mod_clk **mssr)
  55. {
  56. const unsigned long clkid = clk->id & 0xffff;
  57. int i;
  58. for (i = 0; i < info->mod_clk_size; i++) {
  59. if (info->mod_clk[i].id !=
  60. (info->mod_clk_base + MOD_CLK_PACK(clkid)))
  61. continue;
  62. *mssr = &info->mod_clk[i];
  63. return 0;
  64. }
  65. return -ENODEV;
  66. }
  67. int renesas_clk_get_core(struct clk *clk, struct cpg_mssr_info *info,
  68. const struct cpg_core_clk **core)
  69. {
  70. const unsigned long clkid = clk->id & 0xffff;
  71. int i;
  72. for (i = 0; i < info->core_clk_size; i++) {
  73. if (info->core_clk[i].id != clkid)
  74. continue;
  75. *core = &info->core_clk[i];
  76. return 0;
  77. }
  78. return -ENODEV;
  79. }
  80. int renesas_clk_get_parent(struct clk *clk, struct cpg_mssr_info *info,
  81. struct clk *parent)
  82. {
  83. const struct cpg_core_clk *core;
  84. const struct mssr_mod_clk *mssr;
  85. int ret;
  86. if (renesas_clk_is_mod(clk)) {
  87. ret = renesas_clk_get_mod(clk, info, &mssr);
  88. if (ret)
  89. return ret;
  90. parent->id = mssr->parent;
  91. } else {
  92. ret = renesas_clk_get_core(clk, info, &core);
  93. if (ret)
  94. return ret;
  95. if (core->type == CLK_TYPE_IN)
  96. parent->id = ~0; /* Top-level clock */
  97. else
  98. parent->id = core->parent;
  99. }
  100. parent->dev = clk->dev;
  101. return 0;
  102. }
  103. int renesas_clk_endisable(struct clk *clk, void __iomem *base, bool enable)
  104. {
  105. const unsigned long clkid = clk->id & 0xffff;
  106. const unsigned int reg = clkid / 100;
  107. const unsigned int bit = clkid % 100;
  108. const u32 bitmask = BIT(bit);
  109. if (!renesas_clk_is_mod(clk))
  110. return -EINVAL;
  111. debug("%s[%i] MSTP %lu=%02u/%02u %s\n", __func__, __LINE__,
  112. clkid, reg, bit, enable ? "ON" : "OFF");
  113. if (enable) {
  114. clrbits_le32(base + SMSTPCR(reg), bitmask);
  115. return wait_for_bit_le32(base + MSTPSR(reg),
  116. bitmask, 0, 100, 0);
  117. } else {
  118. setbits_le32(base + SMSTPCR(reg), bitmask);
  119. return 0;
  120. }
  121. }
  122. int renesas_clk_remove(void __iomem *base, struct cpg_mssr_info *info)
  123. {
  124. unsigned int i;
  125. /* Stop TMU0 */
  126. clrbits_le32(TMU_BASE + TSTR0, TSTR0_STR0);
  127. /* Stop module clock */
  128. for (i = 0; i < info->mstp_table_size; i++) {
  129. clrsetbits_le32(base + SMSTPCR(i),
  130. info->mstp_table[i].sdis,
  131. info->mstp_table[i].sen);
  132. clrsetbits_le32(base + RMSTPCR(i),
  133. info->mstp_table[i].rdis,
  134. info->mstp_table[i].ren);
  135. }
  136. return 0;
  137. }