pmc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 Boris BREZILLON <[email protected]>
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/clkdev.h>
  8. #include <linux/clk/at91_pmc.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/syscore_ops.h>
  15. #include <asm/proc-fns.h>
  16. #include "pmc.h"
  17. #define PMC_MAX_IDS 128
  18. #define PMC_MAX_PCKS 8
  19. int of_at91_get_clk_range(struct device_node *np, const char *propname,
  20. struct clk_range *range)
  21. {
  22. u32 min, max;
  23. int ret;
  24. ret = of_property_read_u32_index(np, propname, 0, &min);
  25. if (ret)
  26. return ret;
  27. ret = of_property_read_u32_index(np, propname, 1, &max);
  28. if (ret)
  29. return ret;
  30. if (range) {
  31. range->min = min;
  32. range->max = max;
  33. }
  34. return 0;
  35. }
  36. EXPORT_SYMBOL_GPL(of_at91_get_clk_range);
  37. struct clk_hw *of_clk_hw_pmc_get(struct of_phandle_args *clkspec, void *data)
  38. {
  39. unsigned int type = clkspec->args[0];
  40. unsigned int idx = clkspec->args[1];
  41. struct pmc_data *pmc_data = data;
  42. switch (type) {
  43. case PMC_TYPE_CORE:
  44. if (idx < pmc_data->ncore)
  45. return pmc_data->chws[idx];
  46. break;
  47. case PMC_TYPE_SYSTEM:
  48. if (idx < pmc_data->nsystem)
  49. return pmc_data->shws[idx];
  50. break;
  51. case PMC_TYPE_PERIPHERAL:
  52. if (idx < pmc_data->nperiph)
  53. return pmc_data->phws[idx];
  54. break;
  55. case PMC_TYPE_GCK:
  56. if (idx < pmc_data->ngck)
  57. return pmc_data->ghws[idx];
  58. break;
  59. case PMC_TYPE_PROGRAMMABLE:
  60. if (idx < pmc_data->npck)
  61. return pmc_data->pchws[idx];
  62. break;
  63. default:
  64. break;
  65. }
  66. pr_err("%s: invalid type (%u) or index (%u)\n", __func__, type, idx);
  67. return ERR_PTR(-EINVAL);
  68. }
  69. struct pmc_data *pmc_data_allocate(unsigned int ncore, unsigned int nsystem,
  70. unsigned int nperiph, unsigned int ngck,
  71. unsigned int npck)
  72. {
  73. unsigned int num_clks = ncore + nsystem + nperiph + ngck + npck;
  74. struct pmc_data *pmc_data;
  75. pmc_data = kzalloc(struct_size(pmc_data, hwtable, num_clks),
  76. GFP_KERNEL);
  77. if (!pmc_data)
  78. return NULL;
  79. pmc_data->ncore = ncore;
  80. pmc_data->chws = pmc_data->hwtable;
  81. pmc_data->nsystem = nsystem;
  82. pmc_data->shws = pmc_data->chws + ncore;
  83. pmc_data->nperiph = nperiph;
  84. pmc_data->phws = pmc_data->shws + nsystem;
  85. pmc_data->ngck = ngck;
  86. pmc_data->ghws = pmc_data->phws + nperiph;
  87. pmc_data->npck = npck;
  88. pmc_data->pchws = pmc_data->ghws + ngck;
  89. return pmc_data;
  90. }
  91. #ifdef CONFIG_PM
  92. /* Address in SECURAM that say if we suspend to backup mode. */
  93. static void __iomem *at91_pmc_backup_suspend;
  94. static int at91_pmc_suspend(void)
  95. {
  96. unsigned int backup;
  97. if (!at91_pmc_backup_suspend)
  98. return 0;
  99. backup = readl_relaxed(at91_pmc_backup_suspend);
  100. if (!backup)
  101. return 0;
  102. return clk_save_context();
  103. }
  104. static void at91_pmc_resume(void)
  105. {
  106. unsigned int backup;
  107. if (!at91_pmc_backup_suspend)
  108. return;
  109. backup = readl_relaxed(at91_pmc_backup_suspend);
  110. if (!backup)
  111. return;
  112. clk_restore_context();
  113. }
  114. static struct syscore_ops pmc_syscore_ops = {
  115. .suspend = at91_pmc_suspend,
  116. .resume = at91_pmc_resume,
  117. };
  118. static const struct of_device_id pmc_dt_ids[] = {
  119. { .compatible = "atmel,sama5d2-pmc" },
  120. { .compatible = "microchip,sama7g5-pmc", },
  121. { /* sentinel */ }
  122. };
  123. static int __init pmc_register_ops(void)
  124. {
  125. struct device_node *np;
  126. np = of_find_matching_node(NULL, pmc_dt_ids);
  127. if (!np)
  128. return -ENODEV;
  129. if (!of_device_is_available(np)) {
  130. of_node_put(np);
  131. return -ENODEV;
  132. }
  133. of_node_put(np);
  134. np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-securam");
  135. if (!np)
  136. return -ENODEV;
  137. if (!of_device_is_available(np)) {
  138. of_node_put(np);
  139. return -ENODEV;
  140. }
  141. of_node_put(np);
  142. at91_pmc_backup_suspend = of_iomap(np, 0);
  143. if (!at91_pmc_backup_suspend) {
  144. pr_warn("%s(): unable to map securam\n", __func__);
  145. return -ENOMEM;
  146. }
  147. register_syscore_ops(&pmc_syscore_ops);
  148. return 0;
  149. }
  150. /* This has to happen before arch_initcall because of the tcb_clksrc driver */
  151. postcore_initcall(pmc_register_ops);
  152. #endif