clk.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2010 Thomas Langer <thomas.langer@lantiq.com>
  7. * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
  8. */
  9. #include <linux/io.h>
  10. #include <linux/export.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/list.h>
  17. #include <asm/time.h>
  18. #include <asm/irq.h>
  19. #include <asm/div64.h>
  20. #include <lantiq_soc.h>
  21. #include "clk.h"
  22. struct clk {
  23. const char *name;
  24. unsigned long rate;
  25. unsigned long (*get_rate) (void);
  26. };
  27. static struct clk *cpu_clk;
  28. static int cpu_clk_cnt;
  29. /* lantiq socs have 3 static clocks */
  30. static struct clk cpu_clk_generic[] = {
  31. {
  32. .name = "cpu",
  33. .get_rate = ltq_get_cpu_hz,
  34. }, {
  35. .name = "fpi",
  36. .get_rate = ltq_get_fpi_hz,
  37. }, {
  38. .name = "io",
  39. .get_rate = ltq_get_io_region_clock,
  40. },
  41. };
  42. static struct resource ltq_cgu_resource = {
  43. .name = "cgu",
  44. .start = LTQ_CGU_BASE_ADDR,
  45. .end = LTQ_CGU_BASE_ADDR + LTQ_CGU_SIZE - 1,
  46. .flags = IORESOURCE_MEM,
  47. };
  48. /* remapped clock register range */
  49. void __iomem *ltq_cgu_membase;
  50. void clk_init(void)
  51. {
  52. cpu_clk = cpu_clk_generic;
  53. cpu_clk_cnt = ARRAY_SIZE(cpu_clk_generic);
  54. }
  55. static inline int clk_good(struct clk *clk)
  56. {
  57. return clk && !IS_ERR(clk);
  58. }
  59. unsigned long clk_get_rate(struct clk *clk)
  60. {
  61. if (unlikely(!clk_good(clk)))
  62. return 0;
  63. if (clk->rate != 0)
  64. return clk->rate;
  65. if (clk->get_rate != NULL)
  66. return clk->get_rate();
  67. return 0;
  68. }
  69. EXPORT_SYMBOL(clk_get_rate);
  70. struct clk *clk_get(struct device *dev, const char *id)
  71. {
  72. int i;
  73. for (i = 0; i < cpu_clk_cnt; i++)
  74. if (!strcmp(id, cpu_clk[i].name))
  75. return &cpu_clk[i];
  76. BUG();
  77. return ERR_PTR(-ENOENT);
  78. }
  79. EXPORT_SYMBOL(clk_get);
  80. void clk_put(struct clk *clk)
  81. {
  82. /* not used */
  83. }
  84. EXPORT_SYMBOL(clk_put);
  85. int clk_enable(struct clk *clk)
  86. {
  87. /* not used */
  88. return 0;
  89. }
  90. EXPORT_SYMBOL(clk_enable);
  91. void clk_disable(struct clk *clk)
  92. {
  93. /* not used */
  94. }
  95. EXPORT_SYMBOL(clk_disable);
  96. static inline u32 ltq_get_counter_resolution(void)
  97. {
  98. u32 res;
  99. __asm__ __volatile__(
  100. ".set push\n"
  101. ".set mips32r2\n"
  102. "rdhwr %0, $3\n"
  103. ".set pop\n"
  104. : "=&r" (res)
  105. : /* no input */
  106. : "memory");
  107. return res;
  108. }
  109. void __init plat_time_init(void)
  110. {
  111. struct clk *clk;
  112. if (insert_resource(&iomem_resource, &ltq_cgu_resource) < 0)
  113. panic("Failed to insert cgu memory");
  114. if (request_mem_region(ltq_cgu_resource.start,
  115. resource_size(&ltq_cgu_resource), "cgu") < 0)
  116. panic("Failed to request cgu memory");
  117. ltq_cgu_membase = ioremap_nocache(ltq_cgu_resource.start,
  118. resource_size(&ltq_cgu_resource));
  119. if (!ltq_cgu_membase) {
  120. pr_err("Failed to remap cgu memory\n");
  121. unreachable();
  122. }
  123. clk = clk_get(0, "cpu");
  124. mips_hpt_frequency = clk_get_rate(clk) / ltq_get_counter_resolution();
  125. write_c0_compare(read_c0_count());
  126. clk_put(clk);
  127. }