clk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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/init.h>
  9. #include <linux/export.h>
  10. #include <linux/mutex.h>
  11. #include <linux/err.h>
  12. #include <linux/clk.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/delay.h>
  15. #include <bcm63xx_cpu.h>
  16. #include <bcm63xx_io.h>
  17. #include <bcm63xx_regs.h>
  18. #include <bcm63xx_reset.h>
  19. struct clk {
  20. void (*set)(struct clk *, int);
  21. unsigned int rate;
  22. unsigned int usage;
  23. int id;
  24. };
  25. static DEFINE_MUTEX(clocks_mutex);
  26. static void clk_enable_unlocked(struct clk *clk)
  27. {
  28. if (clk->set && (clk->usage++) == 0)
  29. clk->set(clk, 1);
  30. }
  31. static void clk_disable_unlocked(struct clk *clk)
  32. {
  33. if (clk->set && (--clk->usage) == 0)
  34. clk->set(clk, 0);
  35. }
  36. static void bcm_hwclock_set(u32 mask, int enable)
  37. {
  38. u32 reg;
  39. reg = bcm_perf_readl(PERF_CKCTL_REG);
  40. if (enable)
  41. reg |= mask;
  42. else
  43. reg &= ~mask;
  44. bcm_perf_writel(reg, PERF_CKCTL_REG);
  45. }
  46. /*
  47. * Ethernet MAC "misc" clock: dma clocks and main clock on 6348
  48. */
  49. static void enet_misc_set(struct clk *clk, int enable)
  50. {
  51. u32 mask;
  52. if (BCMCPU_IS_6338())
  53. mask = CKCTL_6338_ENET_EN;
  54. else if (BCMCPU_IS_6345())
  55. mask = CKCTL_6345_ENET_EN;
  56. else if (BCMCPU_IS_6348())
  57. mask = CKCTL_6348_ENET_EN;
  58. else
  59. /* BCMCPU_IS_6358 */
  60. mask = CKCTL_6358_EMUSB_EN;
  61. bcm_hwclock_set(mask, enable);
  62. }
  63. static struct clk clk_enet_misc = {
  64. .set = enet_misc_set,
  65. };
  66. /*
  67. * Ethernet MAC clocks: only revelant on 6358, silently enable misc
  68. * clocks
  69. */
  70. static void enetx_set(struct clk *clk, int enable)
  71. {
  72. if (enable)
  73. clk_enable_unlocked(&clk_enet_misc);
  74. else
  75. clk_disable_unlocked(&clk_enet_misc);
  76. if (BCMCPU_IS_3368() || BCMCPU_IS_6358()) {
  77. u32 mask;
  78. if (clk->id == 0)
  79. mask = CKCTL_6358_ENET0_EN;
  80. else
  81. mask = CKCTL_6358_ENET1_EN;
  82. bcm_hwclock_set(mask, enable);
  83. }
  84. }
  85. static struct clk clk_enet0 = {
  86. .id = 0,
  87. .set = enetx_set,
  88. };
  89. static struct clk clk_enet1 = {
  90. .id = 1,
  91. .set = enetx_set,
  92. };
  93. /*
  94. * Ethernet PHY clock
  95. */
  96. static void ephy_set(struct clk *clk, int enable)
  97. {
  98. if (BCMCPU_IS_3368() || BCMCPU_IS_6358())
  99. bcm_hwclock_set(CKCTL_6358_EPHY_EN, enable);
  100. }
  101. static struct clk clk_ephy = {
  102. .set = ephy_set,
  103. };
  104. /*
  105. * Ethernet switch clock
  106. */
  107. static void enetsw_set(struct clk *clk, int enable)
  108. {
  109. if (BCMCPU_IS_6328())
  110. bcm_hwclock_set(CKCTL_6328_ROBOSW_EN, enable);
  111. else if (BCMCPU_IS_6362())
  112. bcm_hwclock_set(CKCTL_6362_ROBOSW_EN, enable);
  113. else if (BCMCPU_IS_6368())
  114. bcm_hwclock_set(CKCTL_6368_ROBOSW_EN |
  115. CKCTL_6368_SWPKT_USB_EN |
  116. CKCTL_6368_SWPKT_SAR_EN,
  117. enable);
  118. else
  119. return;
  120. if (enable) {
  121. /* reset switch core afer clock change */
  122. bcm63xx_core_set_reset(BCM63XX_RESET_ENETSW, 1);
  123. msleep(10);
  124. bcm63xx_core_set_reset(BCM63XX_RESET_ENETSW, 0);
  125. msleep(10);
  126. }
  127. }
  128. static struct clk clk_enetsw = {
  129. .set = enetsw_set,
  130. };
  131. /*
  132. * PCM clock
  133. */
  134. static void pcm_set(struct clk *clk, int enable)
  135. {
  136. if (BCMCPU_IS_3368())
  137. bcm_hwclock_set(CKCTL_3368_PCM_EN, enable);
  138. if (BCMCPU_IS_6358())
  139. bcm_hwclock_set(CKCTL_6358_PCM_EN, enable);
  140. }
  141. static struct clk clk_pcm = {
  142. .set = pcm_set,
  143. };
  144. /*
  145. * USB host clock
  146. */
  147. static void usbh_set(struct clk *clk, int enable)
  148. {
  149. if (BCMCPU_IS_6328())
  150. bcm_hwclock_set(CKCTL_6328_USBH_EN, enable);
  151. else if (BCMCPU_IS_6348())
  152. bcm_hwclock_set(CKCTL_6348_USBH_EN, enable);
  153. else if (BCMCPU_IS_6362())
  154. bcm_hwclock_set(CKCTL_6362_USBH_EN, enable);
  155. else if (BCMCPU_IS_6368())
  156. bcm_hwclock_set(CKCTL_6368_USBH_EN, enable);
  157. }
  158. static struct clk clk_usbh = {
  159. .set = usbh_set,
  160. };
  161. /*
  162. * USB device clock
  163. */
  164. static void usbd_set(struct clk *clk, int enable)
  165. {
  166. if (BCMCPU_IS_6328())
  167. bcm_hwclock_set(CKCTL_6328_USBD_EN, enable);
  168. else if (BCMCPU_IS_6362())
  169. bcm_hwclock_set(CKCTL_6362_USBD_EN, enable);
  170. else if (BCMCPU_IS_6368())
  171. bcm_hwclock_set(CKCTL_6368_USBD_EN, enable);
  172. }
  173. static struct clk clk_usbd = {
  174. .set = usbd_set,
  175. };
  176. /*
  177. * SPI clock
  178. */
  179. static void spi_set(struct clk *clk, int enable)
  180. {
  181. u32 mask;
  182. if (BCMCPU_IS_6338())
  183. mask = CKCTL_6338_SPI_EN;
  184. else if (BCMCPU_IS_6348())
  185. mask = CKCTL_6348_SPI_EN;
  186. else if (BCMCPU_IS_3368() || BCMCPU_IS_6358())
  187. mask = CKCTL_6358_SPI_EN;
  188. else if (BCMCPU_IS_6362())
  189. mask = CKCTL_6362_SPI_EN;
  190. else
  191. /* BCMCPU_IS_6368 */
  192. mask = CKCTL_6368_SPI_EN;
  193. bcm_hwclock_set(mask, enable);
  194. }
  195. static struct clk clk_spi = {
  196. .set = spi_set,
  197. };
  198. /*
  199. * HSSPI clock
  200. */
  201. static void hsspi_set(struct clk *clk, int enable)
  202. {
  203. u32 mask;
  204. if (BCMCPU_IS_6328())
  205. mask = CKCTL_6328_HSSPI_EN;
  206. else if (BCMCPU_IS_6362())
  207. mask = CKCTL_6362_HSSPI_EN;
  208. else
  209. return;
  210. bcm_hwclock_set(mask, enable);
  211. }
  212. static struct clk clk_hsspi = {
  213. .set = hsspi_set,
  214. };
  215. /*
  216. * HSSPI PLL
  217. */
  218. static struct clk clk_hsspi_pll;
  219. /*
  220. * XTM clock
  221. */
  222. static void xtm_set(struct clk *clk, int enable)
  223. {
  224. if (!BCMCPU_IS_6368())
  225. return;
  226. bcm_hwclock_set(CKCTL_6368_SAR_EN |
  227. CKCTL_6368_SWPKT_SAR_EN, enable);
  228. if (enable) {
  229. /* reset sar core afer clock change */
  230. bcm63xx_core_set_reset(BCM63XX_RESET_SAR, 1);
  231. mdelay(1);
  232. bcm63xx_core_set_reset(BCM63XX_RESET_SAR, 0);
  233. mdelay(1);
  234. }
  235. }
  236. static struct clk clk_xtm = {
  237. .set = xtm_set,
  238. };
  239. /*
  240. * IPsec clock
  241. */
  242. static void ipsec_set(struct clk *clk, int enable)
  243. {
  244. if (BCMCPU_IS_6362())
  245. bcm_hwclock_set(CKCTL_6362_IPSEC_EN, enable);
  246. else if (BCMCPU_IS_6368())
  247. bcm_hwclock_set(CKCTL_6368_IPSEC_EN, enable);
  248. }
  249. static struct clk clk_ipsec = {
  250. .set = ipsec_set,
  251. };
  252. /*
  253. * PCIe clock
  254. */
  255. static void pcie_set(struct clk *clk, int enable)
  256. {
  257. if (BCMCPU_IS_6328())
  258. bcm_hwclock_set(CKCTL_6328_PCIE_EN, enable);
  259. else if (BCMCPU_IS_6362())
  260. bcm_hwclock_set(CKCTL_6362_PCIE_EN, enable);
  261. }
  262. static struct clk clk_pcie = {
  263. .set = pcie_set,
  264. };
  265. /*
  266. * Internal peripheral clock
  267. */
  268. static struct clk clk_periph = {
  269. .rate = (50 * 1000 * 1000),
  270. };
  271. /*
  272. * Linux clock API implementation
  273. */
  274. int clk_enable(struct clk *clk)
  275. {
  276. mutex_lock(&clocks_mutex);
  277. clk_enable_unlocked(clk);
  278. mutex_unlock(&clocks_mutex);
  279. return 0;
  280. }
  281. EXPORT_SYMBOL(clk_enable);
  282. void clk_disable(struct clk *clk)
  283. {
  284. if (!clk)
  285. return;
  286. mutex_lock(&clocks_mutex);
  287. clk_disable_unlocked(clk);
  288. mutex_unlock(&clocks_mutex);
  289. }
  290. EXPORT_SYMBOL(clk_disable);
  291. unsigned long clk_get_rate(struct clk *clk)
  292. {
  293. if (!clk)
  294. return 0;
  295. return clk->rate;
  296. }
  297. EXPORT_SYMBOL(clk_get_rate);
  298. int clk_set_rate(struct clk *clk, unsigned long rate)
  299. {
  300. return 0;
  301. }
  302. EXPORT_SYMBOL_GPL(clk_set_rate);
  303. long clk_round_rate(struct clk *clk, unsigned long rate)
  304. {
  305. return 0;
  306. }
  307. EXPORT_SYMBOL_GPL(clk_round_rate);
  308. static struct clk_lookup bcm3368_clks[] = {
  309. /* fixed rate clocks */
  310. CLKDEV_INIT(NULL, "periph", &clk_periph),
  311. CLKDEV_INIT("bcm63xx_uart.0", "refclk", &clk_periph),
  312. CLKDEV_INIT("bcm63xx_uart.1", "refclk", &clk_periph),
  313. /* gated clocks */
  314. CLKDEV_INIT(NULL, "enet0", &clk_enet0),
  315. CLKDEV_INIT(NULL, "enet1", &clk_enet1),
  316. CLKDEV_INIT(NULL, "ephy", &clk_ephy),
  317. CLKDEV_INIT(NULL, "usbh", &clk_usbh),
  318. CLKDEV_INIT(NULL, "usbd", &clk_usbd),
  319. CLKDEV_INIT(NULL, "spi", &clk_spi),
  320. CLKDEV_INIT(NULL, "pcm", &clk_pcm),
  321. };
  322. static struct clk_lookup bcm6328_clks[] = {
  323. /* fixed rate clocks */
  324. CLKDEV_INIT(NULL, "periph", &clk_periph),
  325. CLKDEV_INIT("bcm63xx_uart.0", "refclk", &clk_periph),
  326. CLKDEV_INIT("bcm63xx_uart.1", "refclk", &clk_periph),
  327. CLKDEV_INIT("bcm63xx-hsspi.0", "pll", &clk_hsspi_pll),
  328. /* gated clocks */
  329. CLKDEV_INIT(NULL, "enetsw", &clk_enetsw),
  330. CLKDEV_INIT(NULL, "usbh", &clk_usbh),
  331. CLKDEV_INIT(NULL, "usbd", &clk_usbd),
  332. CLKDEV_INIT(NULL, "hsspi", &clk_hsspi),
  333. CLKDEV_INIT(NULL, "pcie", &clk_pcie),
  334. };
  335. static struct clk_lookup bcm6338_clks[] = {
  336. /* fixed rate clocks */
  337. CLKDEV_INIT(NULL, "periph", &clk_periph),
  338. CLKDEV_INIT("bcm63xx_uart.0", "refclk", &clk_periph),
  339. /* gated clocks */
  340. CLKDEV_INIT(NULL, "enet0", &clk_enet0),
  341. CLKDEV_INIT(NULL, "enet1", &clk_enet1),
  342. CLKDEV_INIT(NULL, "ephy", &clk_ephy),
  343. CLKDEV_INIT(NULL, "usbh", &clk_usbh),
  344. CLKDEV_INIT(NULL, "usbd", &clk_usbd),
  345. CLKDEV_INIT(NULL, "spi", &clk_spi),
  346. };
  347. static struct clk_lookup bcm6345_clks[] = {
  348. /* fixed rate clocks */
  349. CLKDEV_INIT(NULL, "periph", &clk_periph),
  350. CLKDEV_INIT("bcm63xx_uart.0", "refclk", &clk_periph),
  351. /* gated clocks */
  352. CLKDEV_INIT(NULL, "enet0", &clk_enet0),
  353. CLKDEV_INIT(NULL, "enet1", &clk_enet1),
  354. CLKDEV_INIT(NULL, "ephy", &clk_ephy),
  355. CLKDEV_INIT(NULL, "usbh", &clk_usbh),
  356. CLKDEV_INIT(NULL, "usbd", &clk_usbd),
  357. CLKDEV_INIT(NULL, "spi", &clk_spi),
  358. };
  359. static struct clk_lookup bcm6348_clks[] = {
  360. /* fixed rate clocks */
  361. CLKDEV_INIT(NULL, "periph", &clk_periph),
  362. CLKDEV_INIT("bcm63xx_uart.0", "refclk", &clk_periph),
  363. /* gated clocks */
  364. CLKDEV_INIT(NULL, "enet0", &clk_enet0),
  365. CLKDEV_INIT(NULL, "enet1", &clk_enet1),
  366. CLKDEV_INIT(NULL, "ephy", &clk_ephy),
  367. CLKDEV_INIT(NULL, "usbh", &clk_usbh),
  368. CLKDEV_INIT(NULL, "usbd", &clk_usbd),
  369. CLKDEV_INIT(NULL, "spi", &clk_spi),
  370. };
  371. static struct clk_lookup bcm6358_clks[] = {
  372. /* fixed rate clocks */
  373. CLKDEV_INIT(NULL, "periph", &clk_periph),
  374. CLKDEV_INIT("bcm63xx_uart.0", "refclk", &clk_periph),
  375. CLKDEV_INIT("bcm63xx_uart.1", "refclk", &clk_periph),
  376. /* gated clocks */
  377. CLKDEV_INIT(NULL, "enet0", &clk_enet0),
  378. CLKDEV_INIT(NULL, "enet1", &clk_enet1),
  379. CLKDEV_INIT(NULL, "ephy", &clk_ephy),
  380. CLKDEV_INIT(NULL, "usbh", &clk_usbh),
  381. CLKDEV_INIT(NULL, "usbd", &clk_usbd),
  382. CLKDEV_INIT(NULL, "spi", &clk_spi),
  383. CLKDEV_INIT(NULL, "pcm", &clk_pcm),
  384. };
  385. static struct clk_lookup bcm6362_clks[] = {
  386. /* fixed rate clocks */
  387. CLKDEV_INIT(NULL, "periph", &clk_periph),
  388. CLKDEV_INIT("bcm63xx_uart.0", "refclk", &clk_periph),
  389. CLKDEV_INIT("bcm63xx_uart.1", "refclk", &clk_periph),
  390. CLKDEV_INIT("bcm63xx-hsspi.0", "pll", &clk_hsspi_pll),
  391. /* gated clocks */
  392. CLKDEV_INIT(NULL, "enetsw", &clk_enetsw),
  393. CLKDEV_INIT(NULL, "usbh", &clk_usbh),
  394. CLKDEV_INIT(NULL, "usbd", &clk_usbd),
  395. CLKDEV_INIT(NULL, "spi", &clk_spi),
  396. CLKDEV_INIT(NULL, "hsspi", &clk_hsspi),
  397. CLKDEV_INIT(NULL, "pcie", &clk_pcie),
  398. CLKDEV_INIT(NULL, "ipsec", &clk_ipsec),
  399. };
  400. static struct clk_lookup bcm6368_clks[] = {
  401. /* fixed rate clocks */
  402. CLKDEV_INIT(NULL, "periph", &clk_periph),
  403. CLKDEV_INIT("bcm63xx_uart.0", "refclk", &clk_periph),
  404. CLKDEV_INIT("bcm63xx_uart.1", "refclk", &clk_periph),
  405. /* gated clocks */
  406. CLKDEV_INIT(NULL, "enetsw", &clk_enetsw),
  407. CLKDEV_INIT(NULL, "usbh", &clk_usbh),
  408. CLKDEV_INIT(NULL, "usbd", &clk_usbd),
  409. CLKDEV_INIT(NULL, "spi", &clk_spi),
  410. CLKDEV_INIT(NULL, "xtm", &clk_xtm),
  411. CLKDEV_INIT(NULL, "ipsec", &clk_ipsec),
  412. };
  413. #define HSSPI_PLL_HZ_6328 133333333
  414. #define HSSPI_PLL_HZ_6362 400000000
  415. static int __init bcm63xx_clk_init(void)
  416. {
  417. switch (bcm63xx_get_cpu_id()) {
  418. case BCM3368_CPU_ID:
  419. clkdev_add_table(bcm3368_clks, ARRAY_SIZE(bcm3368_clks));
  420. break;
  421. case BCM6328_CPU_ID:
  422. clk_hsspi_pll.rate = HSSPI_PLL_HZ_6328;
  423. clkdev_add_table(bcm6328_clks, ARRAY_SIZE(bcm6328_clks));
  424. break;
  425. case BCM6338_CPU_ID:
  426. clkdev_add_table(bcm6338_clks, ARRAY_SIZE(bcm6338_clks));
  427. break;
  428. case BCM6345_CPU_ID:
  429. clkdev_add_table(bcm6345_clks, ARRAY_SIZE(bcm6345_clks));
  430. break;
  431. case BCM6348_CPU_ID:
  432. clkdev_add_table(bcm6348_clks, ARRAY_SIZE(bcm6348_clks));
  433. break;
  434. case BCM6358_CPU_ID:
  435. clkdev_add_table(bcm6358_clks, ARRAY_SIZE(bcm6358_clks));
  436. break;
  437. case BCM6362_CPU_ID:
  438. clk_hsspi_pll.rate = HSSPI_PLL_HZ_6362;
  439. clkdev_add_table(bcm6362_clks, ARRAY_SIZE(bcm6362_clks));
  440. break;
  441. case BCM6368_CPU_ID:
  442. clkdev_add_table(bcm6368_clks, ARRAY_SIZE(bcm6368_clks));
  443. break;
  444. }
  445. return 0;
  446. }
  447. arch_initcall(bcm63xx_clk_init);