clk.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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_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_6328())
  140. bcm_hwclock_set(CKCTL_6328_USBH_EN, enable);
  141. else if (BCMCPU_IS_6348())
  142. bcm_hwclock_set(CKCTL_6348_USBH_EN, enable);
  143. else if (BCMCPU_IS_6368())
  144. bcm_hwclock_set(CKCTL_6368_USBH_EN, enable);
  145. }
  146. static struct clk clk_usbh = {
  147. .set = usbh_set,
  148. };
  149. /*
  150. * USB device clock
  151. */
  152. static void usbd_set(struct clk *clk, int enable)
  153. {
  154. if (BCMCPU_IS_6328())
  155. bcm_hwclock_set(CKCTL_6328_USBD_EN, enable);
  156. else if (BCMCPU_IS_6368())
  157. bcm_hwclock_set(CKCTL_6368_USBD_EN, enable);
  158. }
  159. static struct clk clk_usbd = {
  160. .set = usbd_set,
  161. };
  162. /*
  163. * SPI clock
  164. */
  165. static void spi_set(struct clk *clk, int enable)
  166. {
  167. u32 mask;
  168. if (BCMCPU_IS_6338())
  169. mask = CKCTL_6338_SPI_EN;
  170. else if (BCMCPU_IS_6348())
  171. mask = CKCTL_6348_SPI_EN;
  172. else if (BCMCPU_IS_6358())
  173. mask = CKCTL_6358_SPI_EN;
  174. else
  175. /* BCMCPU_IS_6368 */
  176. mask = CKCTL_6368_SPI_EN;
  177. bcm_hwclock_set(mask, enable);
  178. }
  179. static struct clk clk_spi = {
  180. .set = spi_set,
  181. };
  182. /*
  183. * XTM clock
  184. */
  185. static void xtm_set(struct clk *clk, int enable)
  186. {
  187. if (!BCMCPU_IS_6368())
  188. return;
  189. bcm_hwclock_set(CKCTL_6368_SAR_EN |
  190. CKCTL_6368_SWPKT_SAR_EN, enable);
  191. if (enable) {
  192. u32 val;
  193. /* reset sar core afer clock change */
  194. val = bcm_perf_readl(PERF_SOFTRESET_6368_REG);
  195. val &= ~SOFTRESET_6368_SAR_MASK;
  196. bcm_perf_writel(val, PERF_SOFTRESET_6368_REG);
  197. mdelay(1);
  198. val |= SOFTRESET_6368_SAR_MASK;
  199. bcm_perf_writel(val, PERF_SOFTRESET_6368_REG);
  200. mdelay(1);
  201. }
  202. }
  203. static struct clk clk_xtm = {
  204. .set = xtm_set,
  205. };
  206. /*
  207. * IPsec clock
  208. */
  209. static void ipsec_set(struct clk *clk, int enable)
  210. {
  211. bcm_hwclock_set(CKCTL_6368_IPSEC_EN, enable);
  212. }
  213. static struct clk clk_ipsec = {
  214. .set = ipsec_set,
  215. };
  216. /*
  217. * Internal peripheral clock
  218. */
  219. static struct clk clk_periph = {
  220. .rate = (50 * 1000 * 1000),
  221. };
  222. /*
  223. * Linux clock API implementation
  224. */
  225. int clk_enable(struct clk *clk)
  226. {
  227. mutex_lock(&clocks_mutex);
  228. clk_enable_unlocked(clk);
  229. mutex_unlock(&clocks_mutex);
  230. return 0;
  231. }
  232. EXPORT_SYMBOL(clk_enable);
  233. void clk_disable(struct clk *clk)
  234. {
  235. mutex_lock(&clocks_mutex);
  236. clk_disable_unlocked(clk);
  237. mutex_unlock(&clocks_mutex);
  238. }
  239. EXPORT_SYMBOL(clk_disable);
  240. unsigned long clk_get_rate(struct clk *clk)
  241. {
  242. return clk->rate;
  243. }
  244. EXPORT_SYMBOL(clk_get_rate);
  245. struct clk *clk_get(struct device *dev, const char *id)
  246. {
  247. if (!strcmp(id, "enet0"))
  248. return &clk_enet0;
  249. if (!strcmp(id, "enet1"))
  250. return &clk_enet1;
  251. if (!strcmp(id, "enetsw"))
  252. return &clk_enetsw;
  253. if (!strcmp(id, "ephy"))
  254. return &clk_ephy;
  255. if (!strcmp(id, "usbh"))
  256. return &clk_usbh;
  257. if (!strcmp(id, "usbd"))
  258. return &clk_usbd;
  259. if (!strcmp(id, "spi"))
  260. return &clk_spi;
  261. if (!strcmp(id, "xtm"))
  262. return &clk_xtm;
  263. if (!strcmp(id, "periph"))
  264. return &clk_periph;
  265. if (BCMCPU_IS_6358() && !strcmp(id, "pcm"))
  266. return &clk_pcm;
  267. if (BCMCPU_IS_6368() && !strcmp(id, "ipsec"))
  268. return &clk_ipsec;
  269. return ERR_PTR(-ENOENT);
  270. }
  271. EXPORT_SYMBOL(clk_get);
  272. void clk_put(struct clk *clk)
  273. {
  274. }
  275. EXPORT_SYMBOL(clk_put);