reset-prcc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Reset controller portions for the U8500 PRCC
  4. * Copyright (C) 2021 Linus Walleij <[email protected]>
  5. */
  6. #include <linux/of.h>
  7. #include <linux/of_address.h>
  8. #include <linux/slab.h>
  9. #include <linux/io.h>
  10. #include <linux/err.h>
  11. #include <linux/types.h>
  12. #include <linux/reset-controller.h>
  13. #include <linux/bits.h>
  14. #include <linux/delay.h>
  15. #include "prcc.h"
  16. #include "reset-prcc.h"
  17. #define to_u8500_prcc_reset(p) container_of((p), struct u8500_prcc_reset, rcdev)
  18. /* This macro flattens the 2-dimensional PRCC numberspace */
  19. #define PRCC_RESET_LINE(prcc_num, bit) \
  20. (((prcc_num) * PRCC_PERIPHS_PER_CLUSTER) + (bit))
  21. /*
  22. * Reset registers in each PRCC - the reset lines are active low
  23. * so what you need to do is write a bit for the peripheral you
  24. * want to put into reset into the CLEAR register, this will assert
  25. * the reset by pulling the line low. SET take the device out of
  26. * reset. The status reflects the actual state of the line.
  27. */
  28. #define PRCC_K_SOFTRST_SET 0x018
  29. #define PRCC_K_SOFTRST_CLEAR 0x01c
  30. #define PRCC_K_RST_STATUS 0x020
  31. static int prcc_num_to_index(unsigned int num)
  32. {
  33. switch (num) {
  34. case 1:
  35. return CLKRST1_INDEX;
  36. case 2:
  37. return CLKRST2_INDEX;
  38. case 3:
  39. return CLKRST3_INDEX;
  40. case 5:
  41. return CLKRST5_INDEX;
  42. case 6:
  43. return CLKRST6_INDEX;
  44. }
  45. return -EINVAL;
  46. }
  47. static void __iomem *u8500_prcc_reset_base(struct u8500_prcc_reset *ur,
  48. unsigned long id)
  49. {
  50. unsigned int prcc_num, index;
  51. prcc_num = id / PRCC_PERIPHS_PER_CLUSTER;
  52. index = prcc_num_to_index(prcc_num);
  53. if (index >= ARRAY_SIZE(ur->base))
  54. return NULL;
  55. return ur->base[index];
  56. }
  57. static int u8500_prcc_reset(struct reset_controller_dev *rcdev,
  58. unsigned long id)
  59. {
  60. struct u8500_prcc_reset *ur = to_u8500_prcc_reset(rcdev);
  61. void __iomem *base = u8500_prcc_reset_base(ur, id);
  62. unsigned int bit = id % PRCC_PERIPHS_PER_CLUSTER;
  63. pr_debug("PRCC cycle reset id %lu, bit %u\n", id, bit);
  64. /*
  65. * Assert reset and then release it. The one microsecond
  66. * delay is found in the vendor reference code.
  67. */
  68. writel(BIT(bit), base + PRCC_K_SOFTRST_CLEAR);
  69. udelay(1);
  70. writel(BIT(bit), base + PRCC_K_SOFTRST_SET);
  71. udelay(1);
  72. return 0;
  73. }
  74. static int u8500_prcc_reset_assert(struct reset_controller_dev *rcdev,
  75. unsigned long id)
  76. {
  77. struct u8500_prcc_reset *ur = to_u8500_prcc_reset(rcdev);
  78. void __iomem *base = u8500_prcc_reset_base(ur, id);
  79. unsigned int bit = id % PRCC_PERIPHS_PER_CLUSTER;
  80. pr_debug("PRCC assert reset id %lu, bit %u\n", id, bit);
  81. writel(BIT(bit), base + PRCC_K_SOFTRST_CLEAR);
  82. return 0;
  83. }
  84. static int u8500_prcc_reset_deassert(struct reset_controller_dev *rcdev,
  85. unsigned long id)
  86. {
  87. struct u8500_prcc_reset *ur = to_u8500_prcc_reset(rcdev);
  88. void __iomem *base = u8500_prcc_reset_base(ur, id);
  89. unsigned int bit = id % PRCC_PERIPHS_PER_CLUSTER;
  90. pr_debug("PRCC deassert reset id %lu, bit %u\n", id, bit);
  91. writel(BIT(bit), base + PRCC_K_SOFTRST_SET);
  92. return 0;
  93. }
  94. static int u8500_prcc_reset_status(struct reset_controller_dev *rcdev,
  95. unsigned long id)
  96. {
  97. struct u8500_prcc_reset *ur = to_u8500_prcc_reset(rcdev);
  98. void __iomem *base = u8500_prcc_reset_base(ur, id);
  99. unsigned int bit = id % PRCC_PERIPHS_PER_CLUSTER;
  100. u32 val;
  101. pr_debug("PRCC check status on reset line id %lu, bit %u\n", id, bit);
  102. val = readl(base + PRCC_K_RST_STATUS);
  103. /* Active low so return the inverse value of the bit */
  104. return !(val & BIT(bit));
  105. }
  106. static const struct reset_control_ops u8500_prcc_reset_ops = {
  107. .reset = u8500_prcc_reset,
  108. .assert = u8500_prcc_reset_assert,
  109. .deassert = u8500_prcc_reset_deassert,
  110. .status = u8500_prcc_reset_status,
  111. };
  112. static int u8500_prcc_reset_xlate(struct reset_controller_dev *rcdev,
  113. const struct of_phandle_args *reset_spec)
  114. {
  115. unsigned int prcc_num, bit;
  116. if (reset_spec->args_count != 2)
  117. return -EINVAL;
  118. prcc_num = reset_spec->args[0];
  119. bit = reset_spec->args[1];
  120. if (prcc_num != 1 && prcc_num != 2 && prcc_num != 3 &&
  121. prcc_num != 5 && prcc_num != 6) {
  122. pr_err("%s: invalid PRCC %d\n", __func__, prcc_num);
  123. return -EINVAL;
  124. }
  125. pr_debug("located reset line %d at PRCC %d bit %d\n",
  126. PRCC_RESET_LINE(prcc_num, bit), prcc_num, bit);
  127. return PRCC_RESET_LINE(prcc_num, bit);
  128. }
  129. void u8500_prcc_reset_init(struct device_node *np, struct u8500_prcc_reset *ur)
  130. {
  131. struct reset_controller_dev *rcdev = &ur->rcdev;
  132. int ret;
  133. int i;
  134. for (i = 0; i < CLKRST_MAX; i++) {
  135. ur->base[i] = ioremap(ur->phy_base[i], SZ_4K);
  136. if (!ur->base[i])
  137. pr_err("PRCC failed to remap for reset base %d (%08x)\n",
  138. i, ur->phy_base[i]);
  139. }
  140. rcdev->owner = THIS_MODULE;
  141. rcdev->ops = &u8500_prcc_reset_ops;
  142. rcdev->of_node = np;
  143. rcdev->of_reset_n_cells = 2;
  144. rcdev->of_xlate = u8500_prcc_reset_xlate;
  145. ret = reset_controller_register(rcdev);
  146. if (ret)
  147. pr_err("PRCC failed to register reset controller\n");
  148. }