clk.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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_reset.h>
  17. struct clk {
  18. void (*set)(struct clk *, int);
  19. unsigned int rate;
  20. unsigned int usage;
  21. int id;
  22. };
  23. static DEFINE_MUTEX(clocks_mutex);
  24. static void clk_enable_unlocked(struct clk *clk)
  25. {
  26. if (clk->set && (clk->usage++) == 0)
  27. clk->set(clk, 1);
  28. }
  29. static void clk_disable_unlocked(struct clk *clk)
  30. {
  31. if (clk->set && (--clk->usage) == 0)
  32. clk->set(clk, 0);
  33. }
  34. static void bcm_hwclock_set(u32 mask, int enable)
  35. {
  36. u32 reg;
  37. reg = bcm_perf_readl(PERF_CKCTL_REG);
  38. if (enable)
  39. reg |= mask;
  40. else
  41. reg &= ~mask;
  42. bcm_perf_writel(reg, PERF_CKCTL_REG);
  43. }
  44. /*
  45. * Ethernet MAC "misc" clock: dma clocks and main clock on 6348
  46. */
  47. static void enet_misc_set(struct clk *clk, int enable)
  48. {
  49. u32 mask;
  50. if (BCMCPU_IS_6338())
  51. mask = CKCTL_6338_ENET_EN;
  52. else if (BCMCPU_IS_6345())
  53. mask = CKCTL_6345_ENET_EN;
  54. else if (BCMCPU_IS_6348())
  55. mask = CKCTL_6348_ENET_EN;
  56. else
  57. /* BCMCPU_IS_6358 */
  58. mask = CKCTL_6358_EMUSB_EN;
  59. bcm_hwclock_set(mask, enable);
  60. }
  61. static struct clk clk_enet_misc = {
  62. .set = enet_misc_set,
  63. };
  64. /*
  65. * Ethernet MAC clocks: only revelant on 6358, silently enable misc
  66. * clocks
  67. */
  68. static void enetx_set(struct clk *clk, int enable)
  69. {
  70. if (enable)
  71. clk_enable_unlocked(&clk_enet_misc);
  72. else
  73. clk_disable_unlocked(&clk_enet_misc);
  74. if (BCMCPU_IS_3368() || BCMCPU_IS_6358()) {
  75. u32 mask;
  76. if (clk->id == 0)
  77. mask = CKCTL_6358_ENET0_EN;
  78. else
  79. mask = CKCTL_6358_ENET1_EN;
  80. bcm_hwclock_set(mask, enable);
  81. }
  82. }
  83. static struct clk clk_enet0 = {
  84. .id = 0,
  85. .set = enetx_set,
  86. };
  87. static struct clk clk_enet1 = {
  88. .id = 1,
  89. .set = enetx_set,
  90. };
  91. /*
  92. * Ethernet PHY clock
  93. */
  94. static void ephy_set(struct clk *clk, int enable)
  95. {
  96. if (BCMCPU_IS_3368() || BCMCPU_IS_6358())
  97. bcm_hwclock_set(CKCTL_6358_EPHY_EN, enable);
  98. }
  99. static struct clk clk_ephy = {
  100. .set = ephy_set,
  101. };
  102. /*
  103. * Ethernet switch clock
  104. */
  105. static void enetsw_set(struct clk *clk, int enable)
  106. {
  107. if (BCMCPU_IS_6328())
  108. bcm_hwclock_set(CKCTL_6328_ROBOSW_EN, enable);
  109. else if (BCMCPU_IS_6362())
  110. bcm_hwclock_set(CKCTL_6362_ROBOSW_EN, enable);
  111. else if (BCMCPU_IS_6368())
  112. bcm_hwclock_set(CKCTL_6368_ROBOSW_EN |
  113. CKCTL_6368_SWPKT_USB_EN |
  114. CKCTL_6368_SWPKT_SAR_EN,
  115. enable);
  116. else
  117. return;
  118. if (enable) {
  119. /* reset switch core afer clock change */
  120. bcm63xx_core_set_reset(BCM63XX_RESET_ENETSW, 1);
  121. msleep(10);
  122. bcm63xx_core_set_reset(BCM63XX_RESET_ENETSW, 0);
  123. msleep(10);
  124. }
  125. }
  126. static struct clk clk_enetsw = {
  127. .set = enetsw_set,
  128. };
  129. /*
  130. * PCM clock
  131. */
  132. static void pcm_set(struct clk *clk, int enable)
  133. {
  134. if (BCMCPU_IS_3368())
  135. bcm_hwclock_set(CKCTL_3368_PCM_EN, enable);
  136. if (BCMCPU_IS_6358())
  137. bcm_hwclock_set(CKCTL_6358_PCM_EN, enable);
  138. }
  139. static struct clk clk_pcm = {
  140. .set = pcm_set,
  141. };
  142. /*
  143. * USB host clock
  144. */
  145. static void usbh_set(struct clk *clk, int enable)
  146. {
  147. if (BCMCPU_IS_6328())
  148. bcm_hwclock_set(CKCTL_6328_USBH_EN, enable);
  149. else if (BCMCPU_IS_6348())
  150. bcm_hwclock_set(CKCTL_6348_USBH_EN, enable);
  151. else if (BCMCPU_IS_6362())
  152. bcm_hwclock_set(CKCTL_6362_USBH_EN, enable);
  153. else if (BCMCPU_IS_6368())
  154. bcm_hwclock_set(CKCTL_6368_USBH_EN, enable);
  155. }
  156. static struct clk clk_usbh = {
  157. .set = usbh_set,
  158. };
  159. /*
  160. * USB device clock
  161. */
  162. static void usbd_set(struct clk *clk, int enable)
  163. {
  164. if (BCMCPU_IS_6328())
  165. bcm_hwclock_set(CKCTL_6328_USBD_EN, enable);
  166. else if (BCMCPU_IS_6362())
  167. bcm_hwclock_set(CKCTL_6362_USBD_EN, enable);
  168. else if (BCMCPU_IS_6368())
  169. bcm_hwclock_set(CKCTL_6368_USBD_EN, enable);
  170. }
  171. static struct clk clk_usbd = {
  172. .set = usbd_set,
  173. };
  174. /*
  175. * SPI clock
  176. */
  177. static void spi_set(struct clk *clk, int enable)
  178. {
  179. u32 mask;
  180. if (BCMCPU_IS_6338())
  181. mask = CKCTL_6338_SPI_EN;
  182. else if (BCMCPU_IS_6348())
  183. mask = CKCTL_6348_SPI_EN;
  184. else if (BCMCPU_IS_3368() || BCMCPU_IS_6358())
  185. mask = CKCTL_6358_SPI_EN;
  186. else if (BCMCPU_IS_6362())
  187. mask = CKCTL_6362_SPI_EN;
  188. else
  189. /* BCMCPU_IS_6368 */
  190. mask = CKCTL_6368_SPI_EN;
  191. bcm_hwclock_set(mask, enable);
  192. }
  193. static struct clk clk_spi = {
  194. .set = spi_set,
  195. };
  196. /*
  197. * HSSPI clock
  198. */
  199. static void hsspi_set(struct clk *clk, int enable)
  200. {
  201. u32 mask;
  202. if (BCMCPU_IS_6328())
  203. mask = CKCTL_6328_HSSPI_EN;
  204. else if (BCMCPU_IS_6362())
  205. mask = CKCTL_6362_HSSPI_EN;
  206. else
  207. return;
  208. bcm_hwclock_set(mask, enable);
  209. }
  210. static struct clk clk_hsspi = {
  211. .set = hsspi_set,
  212. };
  213. /*
  214. * XTM clock
  215. */
  216. static void xtm_set(struct clk *clk, int enable)
  217. {
  218. if (!BCMCPU_IS_6368())
  219. return;
  220. bcm_hwclock_set(CKCTL_6368_SAR_EN |
  221. CKCTL_6368_SWPKT_SAR_EN, enable);
  222. if (enable) {
  223. /* reset sar core afer clock change */
  224. bcm63xx_core_set_reset(BCM63XX_RESET_SAR, 1);
  225. mdelay(1);
  226. bcm63xx_core_set_reset(BCM63XX_RESET_SAR, 0);
  227. mdelay(1);
  228. }
  229. }
  230. static struct clk clk_xtm = {
  231. .set = xtm_set,
  232. };
  233. /*
  234. * IPsec clock
  235. */
  236. static void ipsec_set(struct clk *clk, int enable)
  237. {
  238. if (BCMCPU_IS_6362())
  239. bcm_hwclock_set(CKCTL_6362_IPSEC_EN, enable);
  240. else if (BCMCPU_IS_6368())
  241. bcm_hwclock_set(CKCTL_6368_IPSEC_EN, enable);
  242. }
  243. static struct clk clk_ipsec = {
  244. .set = ipsec_set,
  245. };
  246. /*
  247. * PCIe clock
  248. */
  249. static void pcie_set(struct clk *clk, int enable)
  250. {
  251. if (BCMCPU_IS_6328())
  252. bcm_hwclock_set(CKCTL_6328_PCIE_EN, enable);
  253. else if (BCMCPU_IS_6362())
  254. bcm_hwclock_set(CKCTL_6362_PCIE_EN, enable);
  255. }
  256. static struct clk clk_pcie = {
  257. .set = pcie_set,
  258. };
  259. /*
  260. * Internal peripheral clock
  261. */
  262. static struct clk clk_periph = {
  263. .rate = (50 * 1000 * 1000),
  264. };
  265. /*
  266. * Linux clock API implementation
  267. */
  268. int clk_enable(struct clk *clk)
  269. {
  270. mutex_lock(&clocks_mutex);
  271. clk_enable_unlocked(clk);
  272. mutex_unlock(&clocks_mutex);
  273. return 0;
  274. }
  275. EXPORT_SYMBOL(clk_enable);
  276. void clk_disable(struct clk *clk)
  277. {
  278. if (!clk)
  279. return;
  280. mutex_lock(&clocks_mutex);
  281. clk_disable_unlocked(clk);
  282. mutex_unlock(&clocks_mutex);
  283. }
  284. EXPORT_SYMBOL(clk_disable);
  285. unsigned long clk_get_rate(struct clk *clk)
  286. {
  287. return clk->rate;
  288. }
  289. EXPORT_SYMBOL(clk_get_rate);
  290. int clk_set_rate(struct clk *clk, unsigned long rate)
  291. {
  292. return 0;
  293. }
  294. EXPORT_SYMBOL_GPL(clk_set_rate);
  295. long clk_round_rate(struct clk *clk, unsigned long rate)
  296. {
  297. return 0;
  298. }
  299. EXPORT_SYMBOL_GPL(clk_round_rate);
  300. struct clk *clk_get(struct device *dev, const char *id)
  301. {
  302. if (!strcmp(id, "enet0"))
  303. return &clk_enet0;
  304. if (!strcmp(id, "enet1"))
  305. return &clk_enet1;
  306. if (!strcmp(id, "enetsw"))
  307. return &clk_enetsw;
  308. if (!strcmp(id, "ephy"))
  309. return &clk_ephy;
  310. if (!strcmp(id, "usbh"))
  311. return &clk_usbh;
  312. if (!strcmp(id, "usbd"))
  313. return &clk_usbd;
  314. if (!strcmp(id, "spi"))
  315. return &clk_spi;
  316. if (!strcmp(id, "hsspi"))
  317. return &clk_hsspi;
  318. if (!strcmp(id, "xtm"))
  319. return &clk_xtm;
  320. if (!strcmp(id, "periph"))
  321. return &clk_periph;
  322. if ((BCMCPU_IS_3368() || BCMCPU_IS_6358()) && !strcmp(id, "pcm"))
  323. return &clk_pcm;
  324. if ((BCMCPU_IS_6362() || BCMCPU_IS_6368()) && !strcmp(id, "ipsec"))
  325. return &clk_ipsec;
  326. if ((BCMCPU_IS_6328() || BCMCPU_IS_6362()) && !strcmp(id, "pcie"))
  327. return &clk_pcie;
  328. return ERR_PTR(-ENOENT);
  329. }
  330. EXPORT_SYMBOL(clk_get);
  331. void clk_put(struct clk *clk)
  332. {
  333. }
  334. EXPORT_SYMBOL(clk_put);
  335. #define HSSPI_PLL_HZ_6328 133333333
  336. #define HSSPI_PLL_HZ_6362 400000000
  337. static int __init bcm63xx_clk_init(void)
  338. {
  339. switch (bcm63xx_get_cpu_id()) {
  340. case BCM6328_CPU_ID:
  341. clk_hsspi.rate = HSSPI_PLL_HZ_6328;
  342. break;
  343. case BCM6362_CPU_ID:
  344. clk_hsspi.rate = HSSPI_PLL_HZ_6362;
  345. break;
  346. }
  347. return 0;
  348. }
  349. arch_initcall(bcm63xx_clk_init);