clk-lpc18xx-cgu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * Clk driver for NXP LPC18xx/LPC43xx Clock Generation Unit (CGU)
  3. *
  4. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/delay.h>
  12. #include <linux/kernel.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <dt-bindings/clock/lpc18xx-cgu.h>
  16. /* Clock Generation Unit (CGU) registers */
  17. #define LPC18XX_CGU_XTAL_OSC_CTRL 0x018
  18. #define LPC18XX_CGU_PLL0USB_STAT 0x01c
  19. #define LPC18XX_CGU_PLL0USB_CTRL 0x020
  20. #define LPC18XX_CGU_PLL0USB_MDIV 0x024
  21. #define LPC18XX_CGU_PLL0USB_NP_DIV 0x028
  22. #define LPC18XX_CGU_PLL0AUDIO_STAT 0x02c
  23. #define LPC18XX_CGU_PLL0AUDIO_CTRL 0x030
  24. #define LPC18XX_CGU_PLL0AUDIO_MDIV 0x034
  25. #define LPC18XX_CGU_PLL0AUDIO_NP_DIV 0x038
  26. #define LPC18XX_CGU_PLL0AUDIO_FRAC 0x03c
  27. #define LPC18XX_CGU_PLL1_STAT 0x040
  28. #define LPC18XX_CGU_PLL1_CTRL 0x044
  29. #define LPC18XX_PLL1_CTRL_FBSEL BIT(6)
  30. #define LPC18XX_PLL1_CTRL_DIRECT BIT(7)
  31. #define LPC18XX_CGU_IDIV_CTRL(n) (0x048 + (n) * sizeof(u32))
  32. #define LPC18XX_CGU_BASE_CLK(id) (0x05c + (id) * sizeof(u32))
  33. #define LPC18XX_CGU_PLL_CTRL_OFFSET 0x4
  34. /* PLL0 bits common to both audio and USB PLL */
  35. #define LPC18XX_PLL0_STAT_LOCK BIT(0)
  36. #define LPC18XX_PLL0_CTRL_PD BIT(0)
  37. #define LPC18XX_PLL0_CTRL_BYPASS BIT(1)
  38. #define LPC18XX_PLL0_CTRL_DIRECTI BIT(2)
  39. #define LPC18XX_PLL0_CTRL_DIRECTO BIT(3)
  40. #define LPC18XX_PLL0_CTRL_CLKEN BIT(4)
  41. #define LPC18XX_PLL0_MDIV_MDEC_MASK 0x1ffff
  42. #define LPC18XX_PLL0_MDIV_SELP_SHIFT 17
  43. #define LPC18XX_PLL0_MDIV_SELI_SHIFT 22
  44. #define LPC18XX_PLL0_MSEL_MAX BIT(15)
  45. /* Register value that gives PLL0 post/pre dividers equal to 1 */
  46. #define LPC18XX_PLL0_NP_DIVS_1 0x00302062
  47. enum {
  48. CLK_SRC_OSC32,
  49. CLK_SRC_IRC,
  50. CLK_SRC_ENET_RX_CLK,
  51. CLK_SRC_ENET_TX_CLK,
  52. CLK_SRC_GP_CLKIN,
  53. CLK_SRC_RESERVED1,
  54. CLK_SRC_OSC,
  55. CLK_SRC_PLL0USB,
  56. CLK_SRC_PLL0AUDIO,
  57. CLK_SRC_PLL1,
  58. CLK_SRC_RESERVED2,
  59. CLK_SRC_RESERVED3,
  60. CLK_SRC_IDIVA,
  61. CLK_SRC_IDIVB,
  62. CLK_SRC_IDIVC,
  63. CLK_SRC_IDIVD,
  64. CLK_SRC_IDIVE,
  65. CLK_SRC_MAX
  66. };
  67. static const char *clk_src_names[CLK_SRC_MAX] = {
  68. [CLK_SRC_OSC32] = "osc32",
  69. [CLK_SRC_IRC] = "irc",
  70. [CLK_SRC_ENET_RX_CLK] = "enet_rx_clk",
  71. [CLK_SRC_ENET_TX_CLK] = "enet_tx_clk",
  72. [CLK_SRC_GP_CLKIN] = "gp_clkin",
  73. [CLK_SRC_OSC] = "osc",
  74. [CLK_SRC_PLL0USB] = "pll0usb",
  75. [CLK_SRC_PLL0AUDIO] = "pll0audio",
  76. [CLK_SRC_PLL1] = "pll1",
  77. [CLK_SRC_IDIVA] = "idiva",
  78. [CLK_SRC_IDIVB] = "idivb",
  79. [CLK_SRC_IDIVC] = "idivc",
  80. [CLK_SRC_IDIVD] = "idivd",
  81. [CLK_SRC_IDIVE] = "idive",
  82. };
  83. static const char *clk_base_names[BASE_CLK_MAX] = {
  84. [BASE_SAFE_CLK] = "base_safe_clk",
  85. [BASE_USB0_CLK] = "base_usb0_clk",
  86. [BASE_PERIPH_CLK] = "base_periph_clk",
  87. [BASE_USB1_CLK] = "base_usb1_clk",
  88. [BASE_CPU_CLK] = "base_cpu_clk",
  89. [BASE_SPIFI_CLK] = "base_spifi_clk",
  90. [BASE_SPI_CLK] = "base_spi_clk",
  91. [BASE_PHY_RX_CLK] = "base_phy_rx_clk",
  92. [BASE_PHY_TX_CLK] = "base_phy_tx_clk",
  93. [BASE_APB1_CLK] = "base_apb1_clk",
  94. [BASE_APB3_CLK] = "base_apb3_clk",
  95. [BASE_LCD_CLK] = "base_lcd_clk",
  96. [BASE_ADCHS_CLK] = "base_adchs_clk",
  97. [BASE_SDIO_CLK] = "base_sdio_clk",
  98. [BASE_SSP0_CLK] = "base_ssp0_clk",
  99. [BASE_SSP1_CLK] = "base_ssp1_clk",
  100. [BASE_UART0_CLK] = "base_uart0_clk",
  101. [BASE_UART1_CLK] = "base_uart1_clk",
  102. [BASE_UART2_CLK] = "base_uart2_clk",
  103. [BASE_UART3_CLK] = "base_uart3_clk",
  104. [BASE_OUT_CLK] = "base_out_clk",
  105. [BASE_AUDIO_CLK] = "base_audio_clk",
  106. [BASE_CGU_OUT0_CLK] = "base_cgu_out0_clk",
  107. [BASE_CGU_OUT1_CLK] = "base_cgu_out1_clk",
  108. };
  109. static u32 lpc18xx_cgu_pll0_src_ids[] = {
  110. CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
  111. CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
  112. CLK_SRC_PLL1, CLK_SRC_IDIVA, CLK_SRC_IDIVB, CLK_SRC_IDIVC,
  113. CLK_SRC_IDIVD, CLK_SRC_IDIVE,
  114. };
  115. static u32 lpc18xx_cgu_pll1_src_ids[] = {
  116. CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
  117. CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
  118. CLK_SRC_PLL0USB, CLK_SRC_PLL0AUDIO, CLK_SRC_IDIVA,
  119. CLK_SRC_IDIVB, CLK_SRC_IDIVC, CLK_SRC_IDIVD, CLK_SRC_IDIVE,
  120. };
  121. static u32 lpc18xx_cgu_idiva_src_ids[] = {
  122. CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
  123. CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
  124. CLK_SRC_PLL0USB, CLK_SRC_PLL0AUDIO, CLK_SRC_PLL1
  125. };
  126. static u32 lpc18xx_cgu_idivbcde_src_ids[] = {
  127. CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
  128. CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
  129. CLK_SRC_PLL0AUDIO, CLK_SRC_PLL1, CLK_SRC_IDIVA,
  130. };
  131. static u32 lpc18xx_cgu_base_irc_src_ids[] = {CLK_SRC_IRC};
  132. static u32 lpc18xx_cgu_base_usb0_src_ids[] = {CLK_SRC_PLL0USB};
  133. static u32 lpc18xx_cgu_base_common_src_ids[] = {
  134. CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
  135. CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
  136. CLK_SRC_PLL0AUDIO, CLK_SRC_PLL1, CLK_SRC_IDIVA,
  137. CLK_SRC_IDIVB, CLK_SRC_IDIVC, CLK_SRC_IDIVD, CLK_SRC_IDIVE,
  138. };
  139. static u32 lpc18xx_cgu_base_all_src_ids[] = {
  140. CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
  141. CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
  142. CLK_SRC_PLL0USB, CLK_SRC_PLL0AUDIO, CLK_SRC_PLL1,
  143. CLK_SRC_IDIVA, CLK_SRC_IDIVB, CLK_SRC_IDIVC,
  144. CLK_SRC_IDIVD, CLK_SRC_IDIVE,
  145. };
  146. struct lpc18xx_cgu_src_clk_div {
  147. u8 clk_id;
  148. u8 n_parents;
  149. struct clk_divider div;
  150. struct clk_mux mux;
  151. struct clk_gate gate;
  152. };
  153. #define LPC1XX_CGU_SRC_CLK_DIV(_id, _width, _table) \
  154. { \
  155. .clk_id = CLK_SRC_ ##_id, \
  156. .n_parents = ARRAY_SIZE(lpc18xx_cgu_ ##_table), \
  157. .div = { \
  158. .shift = 2, \
  159. .width = _width, \
  160. }, \
  161. .mux = { \
  162. .mask = 0x1f, \
  163. .shift = 24, \
  164. .table = lpc18xx_cgu_ ##_table, \
  165. }, \
  166. .gate = { \
  167. .bit_idx = 0, \
  168. .flags = CLK_GATE_SET_TO_DISABLE, \
  169. }, \
  170. }
  171. static struct lpc18xx_cgu_src_clk_div lpc18xx_cgu_src_clk_divs[] = {
  172. LPC1XX_CGU_SRC_CLK_DIV(IDIVA, 2, idiva_src_ids),
  173. LPC1XX_CGU_SRC_CLK_DIV(IDIVB, 4, idivbcde_src_ids),
  174. LPC1XX_CGU_SRC_CLK_DIV(IDIVC, 4, idivbcde_src_ids),
  175. LPC1XX_CGU_SRC_CLK_DIV(IDIVD, 4, idivbcde_src_ids),
  176. LPC1XX_CGU_SRC_CLK_DIV(IDIVE, 8, idivbcde_src_ids),
  177. };
  178. struct lpc18xx_cgu_base_clk {
  179. u8 clk_id;
  180. u8 n_parents;
  181. struct clk_mux mux;
  182. struct clk_gate gate;
  183. };
  184. #define LPC1XX_CGU_BASE_CLK(_id, _table, _flags) \
  185. { \
  186. .clk_id = BASE_ ##_id ##_CLK, \
  187. .n_parents = ARRAY_SIZE(lpc18xx_cgu_ ##_table), \
  188. .mux = { \
  189. .mask = 0x1f, \
  190. .shift = 24, \
  191. .table = lpc18xx_cgu_ ##_table, \
  192. .flags = _flags, \
  193. }, \
  194. .gate = { \
  195. .bit_idx = 0, \
  196. .flags = CLK_GATE_SET_TO_DISABLE, \
  197. }, \
  198. }
  199. static struct lpc18xx_cgu_base_clk lpc18xx_cgu_base_clks[] = {
  200. LPC1XX_CGU_BASE_CLK(SAFE, base_irc_src_ids, CLK_MUX_READ_ONLY),
  201. LPC1XX_CGU_BASE_CLK(USB0, base_usb0_src_ids, 0),
  202. LPC1XX_CGU_BASE_CLK(PERIPH, base_common_src_ids, 0),
  203. LPC1XX_CGU_BASE_CLK(USB1, base_all_src_ids, 0),
  204. LPC1XX_CGU_BASE_CLK(CPU, base_common_src_ids, 0),
  205. LPC1XX_CGU_BASE_CLK(SPIFI, base_common_src_ids, 0),
  206. LPC1XX_CGU_BASE_CLK(SPI, base_common_src_ids, 0),
  207. LPC1XX_CGU_BASE_CLK(PHY_RX, base_common_src_ids, 0),
  208. LPC1XX_CGU_BASE_CLK(PHY_TX, base_common_src_ids, 0),
  209. LPC1XX_CGU_BASE_CLK(APB1, base_common_src_ids, 0),
  210. LPC1XX_CGU_BASE_CLK(APB3, base_common_src_ids, 0),
  211. LPC1XX_CGU_BASE_CLK(LCD, base_common_src_ids, 0),
  212. LPC1XX_CGU_BASE_CLK(ADCHS, base_common_src_ids, 0),
  213. LPC1XX_CGU_BASE_CLK(SDIO, base_common_src_ids, 0),
  214. LPC1XX_CGU_BASE_CLK(SSP0, base_common_src_ids, 0),
  215. LPC1XX_CGU_BASE_CLK(SSP1, base_common_src_ids, 0),
  216. LPC1XX_CGU_BASE_CLK(UART0, base_common_src_ids, 0),
  217. LPC1XX_CGU_BASE_CLK(UART1, base_common_src_ids, 0),
  218. LPC1XX_CGU_BASE_CLK(UART2, base_common_src_ids, 0),
  219. LPC1XX_CGU_BASE_CLK(UART3, base_common_src_ids, 0),
  220. LPC1XX_CGU_BASE_CLK(OUT, base_all_src_ids, 0),
  221. { /* 21 reserved */ },
  222. { /* 22 reserved */ },
  223. { /* 23 reserved */ },
  224. { /* 24 reserved */ },
  225. LPC1XX_CGU_BASE_CLK(AUDIO, base_common_src_ids, 0),
  226. LPC1XX_CGU_BASE_CLK(CGU_OUT0, base_all_src_ids, 0),
  227. LPC1XX_CGU_BASE_CLK(CGU_OUT1, base_all_src_ids, 0),
  228. };
  229. struct lpc18xx_pll {
  230. struct clk_hw hw;
  231. void __iomem *reg;
  232. spinlock_t *lock;
  233. u8 flags;
  234. };
  235. #define to_lpc_pll(hw) container_of(hw, struct lpc18xx_pll, hw)
  236. struct lpc18xx_cgu_pll_clk {
  237. u8 clk_id;
  238. u8 n_parents;
  239. u8 reg_offset;
  240. struct clk_mux mux;
  241. struct clk_gate gate;
  242. struct lpc18xx_pll pll;
  243. const struct clk_ops *pll_ops;
  244. };
  245. #define LPC1XX_CGU_CLK_PLL(_id, _table, _pll_ops) \
  246. { \
  247. .clk_id = CLK_SRC_ ##_id, \
  248. .n_parents = ARRAY_SIZE(lpc18xx_cgu_ ##_table), \
  249. .reg_offset = LPC18XX_CGU_ ##_id ##_STAT, \
  250. .mux = { \
  251. .mask = 0x1f, \
  252. .shift = 24, \
  253. .table = lpc18xx_cgu_ ##_table, \
  254. }, \
  255. .gate = { \
  256. .bit_idx = 0, \
  257. .flags = CLK_GATE_SET_TO_DISABLE, \
  258. }, \
  259. .pll_ops = &lpc18xx_ ##_pll_ops, \
  260. }
  261. /*
  262. * PLL0 uses a special register value encoding. The compute functions below
  263. * are taken or derived from the LPC1850 user manual (section 12.6.3.3).
  264. */
  265. /* Compute PLL0 multiplier from decoded version */
  266. static u32 lpc18xx_pll0_mdec2msel(u32 x)
  267. {
  268. int i;
  269. switch (x) {
  270. case 0x18003: return 1;
  271. case 0x10003: return 2;
  272. default:
  273. for (i = LPC18XX_PLL0_MSEL_MAX + 1; x != 0x4000 && i > 0; i--)
  274. x = ((x ^ x >> 14) & 1) | (x << 1 & 0x7fff);
  275. return i;
  276. }
  277. }
  278. /* Compute PLL0 decoded multiplier from binary version */
  279. static u32 lpc18xx_pll0_msel2mdec(u32 msel)
  280. {
  281. u32 i, x = 0x4000;
  282. switch (msel) {
  283. case 0: return 0;
  284. case 1: return 0x18003;
  285. case 2: return 0x10003;
  286. default:
  287. for (i = msel; i <= LPC18XX_PLL0_MSEL_MAX; i++)
  288. x = ((x ^ x >> 1) & 1) << 14 | (x >> 1 & 0xffff);
  289. return x;
  290. }
  291. }
  292. /* Compute PLL0 bandwidth SELI reg from multiplier */
  293. static u32 lpc18xx_pll0_msel2seli(u32 msel)
  294. {
  295. u32 tmp;
  296. if (msel > 16384) return 1;
  297. if (msel > 8192) return 2;
  298. if (msel > 2048) return 4;
  299. if (msel >= 501) return 8;
  300. if (msel >= 60) {
  301. tmp = 1024 / (msel + 9);
  302. return ((1024 == (tmp * (msel + 9))) == 0) ? tmp * 4 : (tmp + 1) * 4;
  303. }
  304. return (msel & 0x3c) + 4;
  305. }
  306. /* Compute PLL0 bandwidth SELP reg from multiplier */
  307. static u32 lpc18xx_pll0_msel2selp(u32 msel)
  308. {
  309. if (msel < 60)
  310. return (msel >> 1) + 1;
  311. return 31;
  312. }
  313. static unsigned long lpc18xx_pll0_recalc_rate(struct clk_hw *hw,
  314. unsigned long parent_rate)
  315. {
  316. struct lpc18xx_pll *pll = to_lpc_pll(hw);
  317. u32 ctrl, mdiv, msel, npdiv;
  318. ctrl = clk_readl(pll->reg + LPC18XX_CGU_PLL0USB_CTRL);
  319. mdiv = clk_readl(pll->reg + LPC18XX_CGU_PLL0USB_MDIV);
  320. npdiv = clk_readl(pll->reg + LPC18XX_CGU_PLL0USB_NP_DIV);
  321. if (ctrl & LPC18XX_PLL0_CTRL_BYPASS)
  322. return parent_rate;
  323. if (npdiv != LPC18XX_PLL0_NP_DIVS_1) {
  324. pr_warn("%s: pre/post dividers not supported\n", __func__);
  325. return 0;
  326. }
  327. msel = lpc18xx_pll0_mdec2msel(mdiv & LPC18XX_PLL0_MDIV_MDEC_MASK);
  328. if (msel)
  329. return 2 * msel * parent_rate;
  330. pr_warn("%s: unable to calculate rate\n", __func__);
  331. return 0;
  332. }
  333. static long lpc18xx_pll0_round_rate(struct clk_hw *hw, unsigned long rate,
  334. unsigned long *prate)
  335. {
  336. unsigned long m;
  337. if (*prate < rate) {
  338. pr_warn("%s: pll dividers not supported\n", __func__);
  339. return -EINVAL;
  340. }
  341. m = DIV_ROUND_UP_ULL(*prate, rate * 2);
  342. if (m <= 0 && m > LPC18XX_PLL0_MSEL_MAX) {
  343. pr_warn("%s: unable to support rate %lu\n", __func__, rate);
  344. return -EINVAL;
  345. }
  346. return 2 * *prate * m;
  347. }
  348. static int lpc18xx_pll0_set_rate(struct clk_hw *hw, unsigned long rate,
  349. unsigned long parent_rate)
  350. {
  351. struct lpc18xx_pll *pll = to_lpc_pll(hw);
  352. u32 ctrl, stat, m;
  353. int retry = 3;
  354. if (parent_rate < rate) {
  355. pr_warn("%s: pll dividers not supported\n", __func__);
  356. return -EINVAL;
  357. }
  358. m = DIV_ROUND_UP_ULL(parent_rate, rate * 2);
  359. if (m <= 0 && m > LPC18XX_PLL0_MSEL_MAX) {
  360. pr_warn("%s: unable to support rate %lu\n", __func__, rate);
  361. return -EINVAL;
  362. }
  363. m = lpc18xx_pll0_msel2mdec(m);
  364. m |= lpc18xx_pll0_msel2selp(m) << LPC18XX_PLL0_MDIV_SELP_SHIFT;
  365. m |= lpc18xx_pll0_msel2seli(m) << LPC18XX_PLL0_MDIV_SELI_SHIFT;
  366. /* Power down PLL, disable clk output and dividers */
  367. ctrl = clk_readl(pll->reg + LPC18XX_CGU_PLL0USB_CTRL);
  368. ctrl |= LPC18XX_PLL0_CTRL_PD;
  369. ctrl &= ~(LPC18XX_PLL0_CTRL_BYPASS | LPC18XX_PLL0_CTRL_DIRECTI |
  370. LPC18XX_PLL0_CTRL_DIRECTO | LPC18XX_PLL0_CTRL_CLKEN);
  371. clk_writel(ctrl, pll->reg + LPC18XX_CGU_PLL0USB_CTRL);
  372. /* Configure new PLL settings */
  373. clk_writel(m, pll->reg + LPC18XX_CGU_PLL0USB_MDIV);
  374. clk_writel(LPC18XX_PLL0_NP_DIVS_1, pll->reg + LPC18XX_CGU_PLL0USB_NP_DIV);
  375. /* Power up PLL and wait for lock */
  376. ctrl &= ~LPC18XX_PLL0_CTRL_PD;
  377. clk_writel(ctrl, pll->reg + LPC18XX_CGU_PLL0USB_CTRL);
  378. do {
  379. udelay(10);
  380. stat = clk_readl(pll->reg + LPC18XX_CGU_PLL0USB_STAT);
  381. if (stat & LPC18XX_PLL0_STAT_LOCK) {
  382. ctrl |= LPC18XX_PLL0_CTRL_CLKEN;
  383. clk_writel(ctrl, pll->reg + LPC18XX_CGU_PLL0USB_CTRL);
  384. return 0;
  385. }
  386. } while (retry--);
  387. pr_warn("%s: unable to lock pll\n", __func__);
  388. return -EINVAL;
  389. }
  390. static const struct clk_ops lpc18xx_pll0_ops = {
  391. .recalc_rate = lpc18xx_pll0_recalc_rate,
  392. .round_rate = lpc18xx_pll0_round_rate,
  393. .set_rate = lpc18xx_pll0_set_rate,
  394. };
  395. static unsigned long lpc18xx_pll1_recalc_rate(struct clk_hw *hw,
  396. unsigned long parent_rate)
  397. {
  398. struct lpc18xx_pll *pll = to_lpc_pll(hw);
  399. u16 msel, nsel, psel;
  400. bool direct, fbsel;
  401. u32 stat, ctrl;
  402. stat = clk_readl(pll->reg + LPC18XX_CGU_PLL1_STAT);
  403. ctrl = clk_readl(pll->reg + LPC18XX_CGU_PLL1_CTRL);
  404. direct = (ctrl & LPC18XX_PLL1_CTRL_DIRECT) ? true : false;
  405. fbsel = (ctrl & LPC18XX_PLL1_CTRL_FBSEL) ? true : false;
  406. msel = ((ctrl >> 16) & 0xff) + 1;
  407. nsel = ((ctrl >> 12) & 0x3) + 1;
  408. if (direct || fbsel)
  409. return msel * (parent_rate / nsel);
  410. psel = (ctrl >> 8) & 0x3;
  411. psel = 1 << psel;
  412. return (msel / (2 * psel)) * (parent_rate / nsel);
  413. }
  414. static const struct clk_ops lpc18xx_pll1_ops = {
  415. .recalc_rate = lpc18xx_pll1_recalc_rate,
  416. };
  417. static int lpc18xx_cgu_gate_enable(struct clk_hw *hw)
  418. {
  419. return clk_gate_ops.enable(hw);
  420. }
  421. static void lpc18xx_cgu_gate_disable(struct clk_hw *hw)
  422. {
  423. clk_gate_ops.disable(hw);
  424. }
  425. static int lpc18xx_cgu_gate_is_enabled(struct clk_hw *hw)
  426. {
  427. const struct clk_hw *parent;
  428. /*
  429. * The consumer of base clocks needs know if the
  430. * base clock is really enabled before it can be
  431. * accessed. It is therefore necessary to verify
  432. * this all the way up.
  433. */
  434. parent = clk_hw_get_parent(hw);
  435. if (!parent)
  436. return 0;
  437. if (!clk_hw_is_enabled(parent))
  438. return 0;
  439. return clk_gate_ops.is_enabled(hw);
  440. }
  441. static const struct clk_ops lpc18xx_gate_ops = {
  442. .enable = lpc18xx_cgu_gate_enable,
  443. .disable = lpc18xx_cgu_gate_disable,
  444. .is_enabled = lpc18xx_cgu_gate_is_enabled,
  445. };
  446. static struct lpc18xx_cgu_pll_clk lpc18xx_cgu_src_clk_plls[] = {
  447. LPC1XX_CGU_CLK_PLL(PLL0USB, pll0_src_ids, pll0_ops),
  448. LPC1XX_CGU_CLK_PLL(PLL0AUDIO, pll0_src_ids, pll0_ops),
  449. LPC1XX_CGU_CLK_PLL(PLL1, pll1_src_ids, pll1_ops),
  450. };
  451. static void lpc18xx_fill_parent_names(const char **parent, u32 *id, int size)
  452. {
  453. int i;
  454. for (i = 0; i < size; i++)
  455. parent[i] = clk_src_names[id[i]];
  456. }
  457. static struct clk *lpc18xx_cgu_register_div(struct lpc18xx_cgu_src_clk_div *clk,
  458. void __iomem *base, int n)
  459. {
  460. void __iomem *reg = base + LPC18XX_CGU_IDIV_CTRL(n);
  461. const char *name = clk_src_names[clk->clk_id];
  462. const char *parents[CLK_SRC_MAX];
  463. clk->div.reg = reg;
  464. clk->mux.reg = reg;
  465. clk->gate.reg = reg;
  466. lpc18xx_fill_parent_names(parents, clk->mux.table, clk->n_parents);
  467. return clk_register_composite(NULL, name, parents, clk->n_parents,
  468. &clk->mux.hw, &clk_mux_ops,
  469. &clk->div.hw, &clk_divider_ops,
  470. &clk->gate.hw, &lpc18xx_gate_ops, 0);
  471. }
  472. static struct clk *lpc18xx_register_base_clk(struct lpc18xx_cgu_base_clk *clk,
  473. void __iomem *reg_base, int n)
  474. {
  475. void __iomem *reg = reg_base + LPC18XX_CGU_BASE_CLK(n);
  476. const char *name = clk_base_names[clk->clk_id];
  477. const char *parents[CLK_SRC_MAX];
  478. if (clk->n_parents == 0)
  479. return ERR_PTR(-ENOENT);
  480. clk->mux.reg = reg;
  481. clk->gate.reg = reg;
  482. lpc18xx_fill_parent_names(parents, clk->mux.table, clk->n_parents);
  483. /* SAFE_CLK can not be turned off */
  484. if (n == BASE_SAFE_CLK)
  485. return clk_register_composite(NULL, name, parents, clk->n_parents,
  486. &clk->mux.hw, &clk_mux_ops,
  487. NULL, NULL, NULL, NULL, 0);
  488. return clk_register_composite(NULL, name, parents, clk->n_parents,
  489. &clk->mux.hw, &clk_mux_ops,
  490. NULL, NULL,
  491. &clk->gate.hw, &lpc18xx_gate_ops, 0);
  492. }
  493. static struct clk *lpc18xx_cgu_register_pll(struct lpc18xx_cgu_pll_clk *clk,
  494. void __iomem *base)
  495. {
  496. const char *name = clk_src_names[clk->clk_id];
  497. const char *parents[CLK_SRC_MAX];
  498. clk->pll.reg = base;
  499. clk->mux.reg = base + clk->reg_offset + LPC18XX_CGU_PLL_CTRL_OFFSET;
  500. clk->gate.reg = base + clk->reg_offset + LPC18XX_CGU_PLL_CTRL_OFFSET;
  501. lpc18xx_fill_parent_names(parents, clk->mux.table, clk->n_parents);
  502. return clk_register_composite(NULL, name, parents, clk->n_parents,
  503. &clk->mux.hw, &clk_mux_ops,
  504. &clk->pll.hw, clk->pll_ops,
  505. &clk->gate.hw, &lpc18xx_gate_ops, 0);
  506. }
  507. static void __init lpc18xx_cgu_register_source_clks(struct device_node *np,
  508. void __iomem *base)
  509. {
  510. const char *parents[CLK_SRC_MAX];
  511. struct clk *clk;
  512. int i;
  513. /* Register the internal 12 MHz RC oscillator (IRC) */
  514. clk = clk_register_fixed_rate(NULL, clk_src_names[CLK_SRC_IRC],
  515. NULL, CLK_IS_ROOT, 12000000);
  516. if (IS_ERR(clk))
  517. pr_warn("%s: failed to register irc clk\n", __func__);
  518. /* Register crystal oscillator controlller */
  519. parents[0] = of_clk_get_parent_name(np, 0);
  520. clk = clk_register_gate(NULL, clk_src_names[CLK_SRC_OSC], parents[0],
  521. 0, base + LPC18XX_CGU_XTAL_OSC_CTRL,
  522. 0, CLK_GATE_SET_TO_DISABLE, NULL);
  523. if (IS_ERR(clk))
  524. pr_warn("%s: failed to register osc clk\n", __func__);
  525. /* Register all PLLs */
  526. for (i = 0; i < ARRAY_SIZE(lpc18xx_cgu_src_clk_plls); i++) {
  527. clk = lpc18xx_cgu_register_pll(&lpc18xx_cgu_src_clk_plls[i],
  528. base);
  529. if (IS_ERR(clk))
  530. pr_warn("%s: failed to register pll (%d)\n", __func__, i);
  531. }
  532. /* Register all clock dividers A-E */
  533. for (i = 0; i < ARRAY_SIZE(lpc18xx_cgu_src_clk_divs); i++) {
  534. clk = lpc18xx_cgu_register_div(&lpc18xx_cgu_src_clk_divs[i],
  535. base, i);
  536. if (IS_ERR(clk))
  537. pr_warn("%s: failed to register div %d\n", __func__, i);
  538. }
  539. }
  540. static struct clk *clk_base[BASE_CLK_MAX];
  541. static struct clk_onecell_data clk_base_data = {
  542. .clks = clk_base,
  543. .clk_num = BASE_CLK_MAX,
  544. };
  545. static void __init lpc18xx_cgu_register_base_clks(void __iomem *reg_base)
  546. {
  547. int i;
  548. for (i = BASE_SAFE_CLK; i < BASE_CLK_MAX; i++) {
  549. clk_base[i] = lpc18xx_register_base_clk(&lpc18xx_cgu_base_clks[i],
  550. reg_base, i);
  551. if (IS_ERR(clk_base[i]) && PTR_ERR(clk_base[i]) != -ENOENT)
  552. pr_warn("%s: register base clk %d failed\n", __func__, i);
  553. }
  554. }
  555. static void __init lpc18xx_cgu_init(struct device_node *np)
  556. {
  557. void __iomem *reg_base;
  558. reg_base = of_iomap(np, 0);
  559. if (!reg_base) {
  560. pr_warn("%s: failed to map address range\n", __func__);
  561. return;
  562. }
  563. lpc18xx_cgu_register_source_clks(np, reg_base);
  564. lpc18xx_cgu_register_base_clks(reg_base);
  565. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_base_data);
  566. }
  567. CLK_OF_DECLARE(lpc18xx_cgu, "nxp,lpc1850-cgu", lpc18xx_cgu_init);