meson8b-clkc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * AmLogic S805 / Meson8b Clock Controller Driver
  3. *
  4. * Copyright (c) 2015 Endless Mobile, Inc.
  5. * Author: Carlo Caione <carlo@endlessm.com>
  6. *
  7. * Copyright (c) 2016 BayLibre, Inc.
  8. * Michael Turquette <mturquette@baylibre.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/clk-provider.h>
  24. #include <linux/of_address.h>
  25. #include <dt-bindings/clock/meson8b-clkc.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/init.h>
  28. #include "clkc.h"
  29. /*
  30. * Clock controller register offsets
  31. *
  32. * Register offsets from the HardKernel[0] data sheet are listed in comment
  33. * blocks below. Those offsets must be multiplied by 4 before adding them to
  34. * the base address to get the right value
  35. *
  36. * [0] http://dn.odroid.com/S805/Datasheet/S805_Datasheet%20V0.8%2020150126.pdf
  37. */
  38. #define MESON8B_REG_SYS_CPU_CNTL1 0x015c /* 0x57 offset in data sheet */
  39. #define MESON8B_REG_HHI_MPEG 0x0174 /* 0x5d offset in data sheet */
  40. #define MESON8B_REG_MALI 0x01b0 /* 0x6c offset in data sheet */
  41. #define MESON8B_REG_PLL_FIXED 0x0280
  42. #define MESON8B_REG_PLL_SYS 0x0300
  43. #define MESON8B_REG_PLL_VID 0x0320
  44. static DEFINE_SPINLOCK(clk_lock);
  45. static const struct pll_rate_table sys_pll_rate_table[] = {
  46. PLL_RATE(312000000, 52, 1, 2),
  47. PLL_RATE(336000000, 56, 1, 2),
  48. PLL_RATE(360000000, 60, 1, 2),
  49. PLL_RATE(384000000, 64, 1, 2),
  50. PLL_RATE(408000000, 68, 1, 2),
  51. PLL_RATE(432000000, 72, 1, 2),
  52. PLL_RATE(456000000, 76, 1, 2),
  53. PLL_RATE(480000000, 80, 1, 2),
  54. PLL_RATE(504000000, 84, 1, 2),
  55. PLL_RATE(528000000, 88, 1, 2),
  56. PLL_RATE(552000000, 92, 1, 2),
  57. PLL_RATE(576000000, 96, 1, 2),
  58. PLL_RATE(600000000, 50, 1, 1),
  59. PLL_RATE(624000000, 52, 1, 1),
  60. PLL_RATE(648000000, 54, 1, 1),
  61. PLL_RATE(672000000, 56, 1, 1),
  62. PLL_RATE(696000000, 58, 1, 1),
  63. PLL_RATE(720000000, 60, 1, 1),
  64. PLL_RATE(744000000, 62, 1, 1),
  65. PLL_RATE(768000000, 64, 1, 1),
  66. PLL_RATE(792000000, 66, 1, 1),
  67. PLL_RATE(816000000, 68, 1, 1),
  68. PLL_RATE(840000000, 70, 1, 1),
  69. PLL_RATE(864000000, 72, 1, 1),
  70. PLL_RATE(888000000, 74, 1, 1),
  71. PLL_RATE(912000000, 76, 1, 1),
  72. PLL_RATE(936000000, 78, 1, 1),
  73. PLL_RATE(960000000, 80, 1, 1),
  74. PLL_RATE(984000000, 82, 1, 1),
  75. PLL_RATE(1008000000, 84, 1, 1),
  76. PLL_RATE(1032000000, 86, 1, 1),
  77. PLL_RATE(1056000000, 88, 1, 1),
  78. PLL_RATE(1080000000, 90, 1, 1),
  79. PLL_RATE(1104000000, 92, 1, 1),
  80. PLL_RATE(1128000000, 94, 1, 1),
  81. PLL_RATE(1152000000, 96, 1, 1),
  82. PLL_RATE(1176000000, 98, 1, 1),
  83. PLL_RATE(1200000000, 50, 1, 0),
  84. PLL_RATE(1224000000, 51, 1, 0),
  85. PLL_RATE(1248000000, 52, 1, 0),
  86. PLL_RATE(1272000000, 53, 1, 0),
  87. PLL_RATE(1296000000, 54, 1, 0),
  88. PLL_RATE(1320000000, 55, 1, 0),
  89. PLL_RATE(1344000000, 56, 1, 0),
  90. PLL_RATE(1368000000, 57, 1, 0),
  91. PLL_RATE(1392000000, 58, 1, 0),
  92. PLL_RATE(1416000000, 59, 1, 0),
  93. PLL_RATE(1440000000, 60, 1, 0),
  94. PLL_RATE(1464000000, 61, 1, 0),
  95. PLL_RATE(1488000000, 62, 1, 0),
  96. PLL_RATE(1512000000, 63, 1, 0),
  97. PLL_RATE(1536000000, 64, 1, 0),
  98. { /* sentinel */ },
  99. };
  100. static const struct clk_div_table cpu_div_table[] = {
  101. { .val = 1, .div = 1 },
  102. { .val = 2, .div = 2 },
  103. { .val = 3, .div = 3 },
  104. { .val = 2, .div = 4 },
  105. { .val = 3, .div = 6 },
  106. { .val = 4, .div = 8 },
  107. { .val = 5, .div = 10 },
  108. { .val = 6, .div = 12 },
  109. { .val = 7, .div = 14 },
  110. { .val = 8, .div = 16 },
  111. { /* sentinel */ },
  112. };
  113. static struct clk_fixed_rate meson8b_xtal = {
  114. .fixed_rate = 24000000,
  115. .hw.init = &(struct clk_init_data){
  116. .name = "xtal",
  117. .num_parents = 0,
  118. .ops = &clk_fixed_rate_ops,
  119. },
  120. };
  121. static struct meson_clk_pll meson8b_fixed_pll = {
  122. .m = {
  123. .reg_off = MESON8B_REG_PLL_FIXED,
  124. .shift = 0,
  125. .width = 9,
  126. },
  127. .n = {
  128. .reg_off = MESON8B_REG_PLL_FIXED,
  129. .shift = 9,
  130. .width = 5,
  131. },
  132. .od = {
  133. .reg_off = MESON8B_REG_PLL_FIXED,
  134. .shift = 16,
  135. .width = 2,
  136. },
  137. .lock = &clk_lock,
  138. .hw.init = &(struct clk_init_data){
  139. .name = "fixed_pll",
  140. .ops = &meson_clk_pll_ro_ops,
  141. .parent_names = (const char *[]){ "xtal" },
  142. .num_parents = 1,
  143. .flags = CLK_GET_RATE_NOCACHE,
  144. },
  145. };
  146. static struct meson_clk_pll meson8b_vid_pll = {
  147. .m = {
  148. .reg_off = MESON8B_REG_PLL_VID,
  149. .shift = 0,
  150. .width = 9,
  151. },
  152. .n = {
  153. .reg_off = MESON8B_REG_PLL_VID,
  154. .shift = 9,
  155. .width = 5,
  156. },
  157. .od = {
  158. .reg_off = MESON8B_REG_PLL_VID,
  159. .shift = 16,
  160. .width = 2,
  161. },
  162. .lock = &clk_lock,
  163. .hw.init = &(struct clk_init_data){
  164. .name = "vid_pll",
  165. .ops = &meson_clk_pll_ro_ops,
  166. .parent_names = (const char *[]){ "xtal" },
  167. .num_parents = 1,
  168. .flags = CLK_GET_RATE_NOCACHE,
  169. },
  170. };
  171. static struct meson_clk_pll meson8b_sys_pll = {
  172. .m = {
  173. .reg_off = MESON8B_REG_PLL_SYS,
  174. .shift = 0,
  175. .width = 9,
  176. },
  177. .n = {
  178. .reg_off = MESON8B_REG_PLL_SYS,
  179. .shift = 9,
  180. .width = 5,
  181. },
  182. .od = {
  183. .reg_off = MESON8B_REG_PLL_SYS,
  184. .shift = 16,
  185. .width = 2,
  186. },
  187. .rate_table = sys_pll_rate_table,
  188. .rate_count = ARRAY_SIZE(sys_pll_rate_table),
  189. .lock = &clk_lock,
  190. .hw.init = &(struct clk_init_data){
  191. .name = "sys_pll",
  192. .ops = &meson_clk_pll_ops,
  193. .parent_names = (const char *[]){ "xtal" },
  194. .num_parents = 1,
  195. .flags = CLK_GET_RATE_NOCACHE,
  196. },
  197. };
  198. static struct clk_fixed_factor meson8b_fclk_div2 = {
  199. .mult = 1,
  200. .div = 2,
  201. .hw.init = &(struct clk_init_data){
  202. .name = "fclk_div2",
  203. .ops = &clk_fixed_factor_ops,
  204. .parent_names = (const char *[]){ "fixed_pll" },
  205. .num_parents = 1,
  206. },
  207. };
  208. static struct clk_fixed_factor meson8b_fclk_div3 = {
  209. .mult = 1,
  210. .div = 3,
  211. .hw.init = &(struct clk_init_data){
  212. .name = "fclk_div3",
  213. .ops = &clk_fixed_factor_ops,
  214. .parent_names = (const char *[]){ "fixed_pll" },
  215. .num_parents = 1,
  216. },
  217. };
  218. static struct clk_fixed_factor meson8b_fclk_div4 = {
  219. .mult = 1,
  220. .div = 4,
  221. .hw.init = &(struct clk_init_data){
  222. .name = "fclk_div4",
  223. .ops = &clk_fixed_factor_ops,
  224. .parent_names = (const char *[]){ "fixed_pll" },
  225. .num_parents = 1,
  226. },
  227. };
  228. static struct clk_fixed_factor meson8b_fclk_div5 = {
  229. .mult = 1,
  230. .div = 5,
  231. .hw.init = &(struct clk_init_data){
  232. .name = "fclk_div5",
  233. .ops = &clk_fixed_factor_ops,
  234. .parent_names = (const char *[]){ "fixed_pll" },
  235. .num_parents = 1,
  236. },
  237. };
  238. static struct clk_fixed_factor meson8b_fclk_div7 = {
  239. .mult = 1,
  240. .div = 7,
  241. .hw.init = &(struct clk_init_data){
  242. .name = "fclk_div7",
  243. .ops = &clk_fixed_factor_ops,
  244. .parent_names = (const char *[]){ "fixed_pll" },
  245. .num_parents = 1,
  246. },
  247. };
  248. /*
  249. * FIXME cpu clocks and the legacy composite clocks (e.g. clk81) are both PLL
  250. * post-dividers and should be modeled with their respective PLLs via the
  251. * forthcoming coordinated clock rates feature
  252. */
  253. static struct meson_clk_cpu meson8b_cpu_clk = {
  254. .reg_off = MESON8B_REG_SYS_CPU_CNTL1,
  255. .div_table = cpu_div_table,
  256. .clk_nb.notifier_call = meson_clk_cpu_notifier_cb,
  257. .hw.init = &(struct clk_init_data){
  258. .name = "cpu_clk",
  259. .ops = &meson_clk_cpu_ops,
  260. .parent_names = (const char *[]){ "sys_pll" },
  261. .num_parents = 1,
  262. },
  263. };
  264. static u32 mux_table_clk81[] = { 6, 5, 7 };
  265. struct clk_mux meson8b_mpeg_clk_sel = {
  266. .reg = (void *)MESON8B_REG_HHI_MPEG,
  267. .mask = 0x7,
  268. .shift = 12,
  269. .flags = CLK_MUX_READ_ONLY,
  270. .table = mux_table_clk81,
  271. .lock = &clk_lock,
  272. .hw.init = &(struct clk_init_data){
  273. .name = "mpeg_clk_sel",
  274. .ops = &clk_mux_ro_ops,
  275. /*
  276. * FIXME bits 14:12 selects from 8 possible parents:
  277. * xtal, 1'b0 (wtf), fclk_div7, mpll_clkout1, mpll_clkout2,
  278. * fclk_div4, fclk_div3, fclk_div5
  279. */
  280. .parent_names = (const char *[]){ "fclk_div3", "fclk_div4",
  281. "fclk_div5" },
  282. .num_parents = 3,
  283. .flags = (CLK_SET_RATE_NO_REPARENT | CLK_IGNORE_UNUSED),
  284. },
  285. };
  286. struct clk_divider meson8b_mpeg_clk_div = {
  287. .reg = (void *)MESON8B_REG_HHI_MPEG,
  288. .shift = 0,
  289. .width = 7,
  290. .lock = &clk_lock,
  291. .hw.init = &(struct clk_init_data){
  292. .name = "mpeg_clk_div",
  293. .ops = &clk_divider_ops,
  294. .parent_names = (const char *[]){ "mpeg_clk_sel" },
  295. .num_parents = 1,
  296. .flags = (CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED),
  297. },
  298. };
  299. struct clk_gate meson8b_clk81 = {
  300. .reg = (void *)MESON8B_REG_HHI_MPEG,
  301. .bit_idx = 7,
  302. .lock = &clk_lock,
  303. .hw.init = &(struct clk_init_data){
  304. .name = "clk81",
  305. .ops = &clk_gate_ops,
  306. .parent_names = (const char *[]){ "mpeg_clk_div" },
  307. .num_parents = 1,
  308. .flags = (CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED),
  309. },
  310. };
  311. static struct clk_hw_onecell_data meson8b_hw_onecell_data = {
  312. .hws = {
  313. [CLKID_XTAL] = &meson8b_xtal.hw,
  314. [CLKID_PLL_FIXED] = &meson8b_fixed_pll.hw,
  315. [CLKID_PLL_VID] = &meson8b_vid_pll.hw,
  316. [CLKID_PLL_SYS] = &meson8b_sys_pll.hw,
  317. [CLKID_FCLK_DIV2] = &meson8b_fclk_div2.hw,
  318. [CLKID_FCLK_DIV3] = &meson8b_fclk_div3.hw,
  319. [CLKID_FCLK_DIV4] = &meson8b_fclk_div4.hw,
  320. [CLKID_FCLK_DIV5] = &meson8b_fclk_div5.hw,
  321. [CLKID_FCLK_DIV7] = &meson8b_fclk_div7.hw,
  322. [CLKID_CPUCLK] = &meson8b_cpu_clk.hw,
  323. [CLKID_MPEG_SEL] = &meson8b_mpeg_clk_sel.hw,
  324. [CLKID_MPEG_DIV] = &meson8b_mpeg_clk_div.hw,
  325. [CLKID_CLK81] = &meson8b_clk81.hw,
  326. },
  327. .num = CLK_NR_CLKS,
  328. };
  329. static struct meson_clk_pll *const meson8b_clk_plls[] = {
  330. &meson8b_fixed_pll,
  331. &meson8b_vid_pll,
  332. &meson8b_sys_pll,
  333. };
  334. static int meson8b_clkc_probe(struct platform_device *pdev)
  335. {
  336. void __iomem *clk_base;
  337. int ret, clkid, i;
  338. struct clk_hw *parent_hw;
  339. struct clk *parent_clk;
  340. struct device *dev = &pdev->dev;
  341. /* Generic clocks and PLLs */
  342. clk_base = of_iomap(dev->of_node, 1);
  343. if (!clk_base) {
  344. pr_err("%s: Unable to map clk base\n", __func__);
  345. return -ENXIO;
  346. }
  347. /* Populate base address for PLLs */
  348. for (i = 0; i < ARRAY_SIZE(meson8b_clk_plls); i++)
  349. meson8b_clk_plls[i]->base = clk_base;
  350. /* Populate the base address for CPU clk */
  351. meson8b_cpu_clk.base = clk_base;
  352. /* Populate the base address for the MPEG clks */
  353. meson8b_mpeg_clk_sel.reg = clk_base + (u32)meson8b_mpeg_clk_sel.reg;
  354. meson8b_mpeg_clk_div.reg = clk_base + (u32)meson8b_mpeg_clk_div.reg;
  355. meson8b_clk81.reg = clk_base + (u32)meson8b_clk81.reg;
  356. /*
  357. * register all clks
  358. * CLKID_UNUSED = 0, so skip it and start with CLKID_XTAL = 1
  359. */
  360. for (clkid = CLKID_XTAL; clkid < CLK_NR_CLKS; clkid++) {
  361. /* array might be sparse */
  362. if (!meson8b_hw_onecell_data.hws[clkid])
  363. continue;
  364. /* FIXME convert to devm_clk_register */
  365. ret = devm_clk_hw_register(dev, meson8b_hw_onecell_data.hws[clkid]);
  366. if (ret)
  367. goto iounmap;
  368. }
  369. /*
  370. * Register CPU clk notifier
  371. *
  372. * FIXME this is wrong for a lot of reasons. First, the muxes should be
  373. * struct clk_hw objects. Second, we shouldn't program the muxes in
  374. * notifier handlers. The tricky programming sequence will be handled
  375. * by the forthcoming coordinated clock rates mechanism once that
  376. * feature is released.
  377. *
  378. * Furthermore, looking up the parent this way is terrible. At some
  379. * point we will stop allocating a default struct clk when registering
  380. * a new clk_hw, and this hack will no longer work. Releasing the ccr
  381. * feature before that time solves the problem :-)
  382. */
  383. parent_hw = clk_hw_get_parent(&meson8b_cpu_clk.hw);
  384. parent_clk = parent_hw->clk;
  385. ret = clk_notifier_register(parent_clk, &meson8b_cpu_clk.clk_nb);
  386. if (ret) {
  387. pr_err("%s: failed to register clock notifier for cpu_clk\n",
  388. __func__);
  389. goto iounmap;
  390. }
  391. return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
  392. &meson8b_hw_onecell_data);
  393. iounmap:
  394. iounmap(clk_base);
  395. return ret;
  396. }
  397. static const struct of_device_id meson8b_clkc_match_table[] = {
  398. { .compatible = "amlogic,meson8b-clkc" },
  399. { }
  400. };
  401. static struct platform_driver meson8b_driver = {
  402. .probe = meson8b_clkc_probe,
  403. .driver = {
  404. .name = "meson8b-clkc",
  405. .of_match_table = meson8b_clkc_match_table,
  406. },
  407. };
  408. static int __init meson8b_clkc_init(void)
  409. {
  410. return platform_driver_register(&meson8b_driver);
  411. }
  412. device_initcall(meson8b_clkc_init);