clk.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. #include <linux/err.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <bcm63xx_cpu.h>
  14. #include <bcm63xx_io.h>
  15. #include <bcm63xx_regs.h>
  16. #include <bcm63xx_clk.h>
  17. static DEFINE_MUTEX(clocks_mutex);
  18. static void clk_enable_unlocked(struct clk *clk)
  19. {
  20. if (clk->set && (clk->usage++) == 0)
  21. clk->set(clk, 1);
  22. }
  23. static void clk_disable_unlocked(struct clk *clk)
  24. {
  25. if (clk->set && (--clk->usage) == 0)
  26. clk->set(clk, 0);
  27. }
  28. static void bcm_hwclock_set(u32 mask, int enable)
  29. {
  30. u32 reg;
  31. reg = bcm_perf_readl(PERF_CKCTL_REG);
  32. if (enable)
  33. reg |= mask;
  34. else
  35. reg &= ~mask;
  36. bcm_perf_writel(reg, PERF_CKCTL_REG);
  37. }
  38. /*
  39. * Ethernet MAC "misc" clock: dma clocks and main clock on 6348
  40. */
  41. static void enet_misc_set(struct clk *clk, int enable)
  42. {
  43. u32 mask;
  44. if (BCMCPU_IS_6338())
  45. mask = CKCTL_6338_ENET_EN;
  46. else if (BCMCPU_IS_6345())
  47. mask = CKCTL_6345_ENET_EN;
  48. else if (BCMCPU_IS_6348())
  49. mask = CKCTL_6348_ENET_EN;
  50. else
  51. /* BCMCPU_IS_6358 */
  52. mask = CKCTL_6358_EMUSB_EN;
  53. bcm_hwclock_set(mask, enable);
  54. }
  55. static struct clk clk_enet_misc = {
  56. .set = enet_misc_set,
  57. };
  58. /*
  59. * Ethernet MAC clocks: only revelant on 6358, silently enable misc
  60. * clocks
  61. */
  62. static void enetx_set(struct clk *clk, int enable)
  63. {
  64. if (enable)
  65. clk_enable_unlocked(&clk_enet_misc);
  66. else
  67. clk_disable_unlocked(&clk_enet_misc);
  68. if (BCMCPU_IS_6358()) {
  69. u32 mask;
  70. if (clk->id == 0)
  71. mask = CKCTL_6358_ENET0_EN;
  72. else
  73. mask = CKCTL_6358_ENET1_EN;
  74. bcm_hwclock_set(mask, enable);
  75. }
  76. }
  77. static struct clk clk_enet0 = {
  78. .id = 0,
  79. .set = enetx_set,
  80. };
  81. static struct clk clk_enet1 = {
  82. .id = 1,
  83. .set = enetx_set,
  84. };
  85. /*
  86. * Ethernet PHY clock
  87. */
  88. static void ephy_set(struct clk *clk, int enable)
  89. {
  90. if (!BCMCPU_IS_6358())
  91. return;
  92. bcm_hwclock_set(CKCTL_6358_EPHY_EN, enable);
  93. }
  94. static struct clk clk_ephy = {
  95. .set = ephy_set,
  96. };
  97. /*
  98. * Ethernet switch clock
  99. */
  100. static void enetsw_set(struct clk *clk, int enable)
  101. {
  102. if (!BCMCPU_IS_6368())
  103. return;
  104. bcm_hwclock_set(CKCTL_6368_ROBOSW_CLK_EN |
  105. CKCTL_6368_SWPKT_USB_EN |
  106. CKCTL_6368_SWPKT_SAR_EN, enable);
  107. if (enable) {
  108. u32 val;
  109. /* reset switch core afer clock change */
  110. val = bcm_perf_readl(PERF_SOFTRESET_6368_REG);
  111. val &= ~SOFTRESET_6368_ENETSW_MASK;
  112. bcm_perf_writel(val, PERF_SOFTRESET_6368_REG);
  113. msleep(10);
  114. val |= SOFTRESET_6368_ENETSW_MASK;
  115. bcm_perf_writel(val, PERF_SOFTRESET_6368_REG);
  116. msleep(10);
  117. }
  118. }
  119. static struct clk clk_enetsw = {
  120. .set = enetsw_set,
  121. };
  122. /*
  123. * PCM clock
  124. */
  125. static void pcm_set(struct clk *clk, int enable)
  126. {
  127. if (!BCMCPU_IS_6358())
  128. return;
  129. bcm_hwclock_set(CKCTL_6358_PCM_EN, enable);
  130. }
  131. static struct clk clk_pcm = {
  132. .set = pcm_set,
  133. };
  134. /*
  135. * USB host clock
  136. */
  137. static void usbh_set(struct clk *clk, int enable)
  138. {
  139. if (BCMCPU_IS_6348())
  140. bcm_hwclock_set(CKCTL_6348_USBH_EN, enable);
  141. else if (BCMCPU_IS_6368())
  142. bcm_hwclock_set(CKCTL_6368_USBH_CLK_EN, enable);
  143. }
  144. static struct clk clk_usbh = {
  145. .set = usbh_set,
  146. };
  147. /*
  148. * SPI clock
  149. */
  150. static void spi_set(struct clk *clk, int enable)
  151. {
  152. u32 mask;
  153. if (BCMCPU_IS_6338())
  154. mask = CKCTL_6338_SPI_EN;
  155. else if (BCMCPU_IS_6348())
  156. mask = CKCTL_6348_SPI_EN;
  157. else
  158. /* BCMCPU_IS_6358 */
  159. mask = CKCTL_6358_SPI_EN;
  160. bcm_hwclock_set(mask, enable);
  161. }
  162. static struct clk clk_spi = {
  163. .set = spi_set,
  164. };
  165. /*
  166. * XTM clock
  167. */
  168. static void xtm_set(struct clk *clk, int enable)
  169. {
  170. if (!BCMCPU_IS_6368())
  171. return;
  172. bcm_hwclock_set(CKCTL_6368_SAR_CLK_EN |
  173. CKCTL_6368_SWPKT_SAR_EN, enable);
  174. if (enable) {
  175. u32 val;
  176. /* reset sar core afer clock change */
  177. val = bcm_perf_readl(PERF_SOFTRESET_6368_REG);
  178. val &= ~SOFTRESET_6368_SAR_MASK;
  179. bcm_perf_writel(val, PERF_SOFTRESET_6368_REG);
  180. mdelay(1);
  181. val |= SOFTRESET_6368_SAR_MASK;
  182. bcm_perf_writel(val, PERF_SOFTRESET_6368_REG);
  183. mdelay(1);
  184. }
  185. }
  186. static struct clk clk_xtm = {
  187. .set = xtm_set,
  188. };
  189. /*
  190. * Internal peripheral clock
  191. */
  192. static struct clk clk_periph = {
  193. .rate = (50 * 1000 * 1000),
  194. };
  195. /*
  196. * Linux clock API implementation
  197. */
  198. int clk_enable(struct clk *clk)
  199. {
  200. mutex_lock(&clocks_mutex);
  201. clk_enable_unlocked(clk);
  202. mutex_unlock(&clocks_mutex);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(clk_enable);
  206. void clk_disable(struct clk *clk)
  207. {
  208. mutex_lock(&clocks_mutex);
  209. clk_disable_unlocked(clk);
  210. mutex_unlock(&clocks_mutex);
  211. }
  212. EXPORT_SYMBOL(clk_disable);
  213. unsigned long clk_get_rate(struct clk *clk)
  214. {
  215. return clk->rate;
  216. }
  217. EXPORT_SYMBOL(clk_get_rate);
  218. struct clk *clk_get(struct device *dev, const char *id)
  219. {
  220. if (!strcmp(id, "enet0"))
  221. return &clk_enet0;
  222. if (!strcmp(id, "enet1"))
  223. return &clk_enet1;
  224. if (!strcmp(id, "enetsw"))
  225. return &clk_enetsw;
  226. if (!strcmp(id, "ephy"))
  227. return &clk_ephy;
  228. if (!strcmp(id, "usbh"))
  229. return &clk_usbh;
  230. if (!strcmp(id, "spi"))
  231. return &clk_spi;
  232. if (!strcmp(id, "xtm"))
  233. return &clk_xtm;
  234. if (!strcmp(id, "periph"))
  235. return &clk_periph;
  236. if (BCMCPU_IS_6358() && !strcmp(id, "pcm"))
  237. return &clk_pcm;
  238. return ERR_PTR(-ENOENT);
  239. }
  240. EXPORT_SYMBOL(clk_get);
  241. void clk_put(struct clk *clk)
  242. {
  243. }
  244. EXPORT_SYMBOL(clk_put);