clk-mpfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PolarFire SoC MSS/core complex clock control
  4. *
  5. * Copyright (C) 2020-2022 Microchip Technology Inc. All rights reserved.
  6. */
  7. #include <linux/cleanup.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/io.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <dt-bindings/clock/microchip,mpfs-clock.h>
  15. #include <soc/microchip/mpfs.h>
  16. /* address offset of control registers */
  17. #define REG_MSSPLL_REF_CR 0x08u
  18. #define REG_MSSPLL_POSTDIV01_CR 0x10u
  19. #define REG_MSSPLL_POSTDIV23_CR 0x14u
  20. #define REG_MSSPLL_SSCG_2_CR 0x2Cu
  21. #define REG_CLOCK_CONFIG_CR 0x08u
  22. #define REG_RTC_CLOCK_CR 0x0Cu
  23. #define REG_SUBBLK_CLOCK_CR 0x84u
  24. #define REG_SUBBLK_RESET_CR 0x88u
  25. #define MSSPLL_FBDIV_SHIFT 0x00u
  26. #define MSSPLL_FBDIV_WIDTH 0x0Cu
  27. #define MSSPLL_REFDIV_SHIFT 0x08u
  28. #define MSSPLL_REFDIV_WIDTH 0x06u
  29. #define MSSPLL_POSTDIV02_SHIFT 0x08u
  30. #define MSSPLL_POSTDIV13_SHIFT 0x18u
  31. #define MSSPLL_POSTDIV_WIDTH 0x07u
  32. #define MSSPLL_FIXED_DIV 4u
  33. static const struct regmap_config mpfs_clk_regmap_config = {
  34. .reg_bits = 32,
  35. .reg_stride = 4,
  36. .val_bits = 32,
  37. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  38. .max_register = REG_SUBBLK_RESET_CR,
  39. };
  40. /*
  41. * This clock ID is defined here, rather than the binding headers, as it is an
  42. * internal clock only, and therefore has no consumers in other peripheral
  43. * blocks.
  44. */
  45. #define CLK_MSSPLL_INTERNAL 38u
  46. struct mpfs_clock_data {
  47. struct device *dev;
  48. struct regmap *regmap;
  49. void __iomem *base;
  50. void __iomem *msspll_base;
  51. struct clk_hw_onecell_data hw_data;
  52. };
  53. struct mpfs_msspll_hw_clock {
  54. void __iomem *base;
  55. struct clk_hw hw;
  56. struct clk_init_data init;
  57. unsigned int id;
  58. u32 reg_offset;
  59. u32 shift;
  60. u32 width;
  61. u32 flags;
  62. };
  63. #define to_mpfs_msspll_clk(_hw) container_of(_hw, struct mpfs_msspll_hw_clock, hw)
  64. struct mpfs_msspll_out_hw_clock {
  65. void __iomem *base;
  66. struct clk_divider output;
  67. struct clk_init_data init;
  68. unsigned int id;
  69. u32 reg_offset;
  70. };
  71. #define to_mpfs_msspll_out_clk(_hw) container_of(_hw, struct mpfs_msspll_out_hw_clock, hw)
  72. struct mpfs_cfg_clock {
  73. struct regmap *map;
  74. const struct clk_div_table *table;
  75. u8 map_offset;
  76. u8 shift;
  77. u8 width;
  78. u8 flags;
  79. };
  80. struct mpfs_cfg_hw_clock {
  81. struct clk_hw hw;
  82. struct mpfs_cfg_clock cfg;
  83. unsigned int id;
  84. };
  85. #define to_mpfs_cfg_clk(_hw) container_of(_hw, struct mpfs_cfg_hw_clock, hw)
  86. struct mpfs_periph_clock {
  87. struct regmap *map;
  88. u8 map_offset;
  89. u8 shift;
  90. };
  91. struct mpfs_periph_hw_clock {
  92. struct clk_hw hw;
  93. struct mpfs_periph_clock periph;
  94. unsigned int id;
  95. };
  96. #define to_mpfs_periph_clk(_hw) container_of(_hw, struct mpfs_periph_hw_clock, hw)
  97. /*
  98. * Protects MSSPLL outputs, since there's two to a register
  99. */
  100. static DEFINE_SPINLOCK(mpfs_clk_lock);
  101. static const struct clk_parent_data mpfs_ext_ref[] = {
  102. { .index = 0 },
  103. };
  104. static const struct clk_div_table mpfs_div_cpu_axi_table[] = {
  105. { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 },
  106. { 0, 0 }
  107. };
  108. static const struct clk_div_table mpfs_div_ahb_table[] = {
  109. { 1, 2 }, { 2, 4}, { 3, 8 },
  110. { 0, 0 }
  111. };
  112. /*
  113. * The only two supported reference clock frequencies for the PolarFire SoC are
  114. * 100 and 125 MHz, as the rtc reference is required to be 1 MHz.
  115. * It therefore only needs to have divider table entries corresponding to
  116. * divide by 100 and 125.
  117. */
  118. static const struct clk_div_table mpfs_div_rtcref_table[] = {
  119. { 100, 100 }, { 125, 125 },
  120. { 0, 0 }
  121. };
  122. /*
  123. * MSS PLL internal clock
  124. */
  125. static unsigned long mpfs_clk_msspll_recalc_rate(struct clk_hw *hw, unsigned long prate)
  126. {
  127. struct mpfs_msspll_hw_clock *msspll_hw = to_mpfs_msspll_clk(hw);
  128. void __iomem *mult_addr = msspll_hw->base + msspll_hw->reg_offset;
  129. void __iomem *ref_div_addr = msspll_hw->base + REG_MSSPLL_REF_CR;
  130. u32 mult, ref_div;
  131. mult = readl_relaxed(mult_addr) >> MSSPLL_FBDIV_SHIFT;
  132. mult &= clk_div_mask(MSSPLL_FBDIV_WIDTH);
  133. ref_div = readl_relaxed(ref_div_addr) >> MSSPLL_REFDIV_SHIFT;
  134. ref_div &= clk_div_mask(MSSPLL_REFDIV_WIDTH);
  135. return prate * mult / (ref_div * MSSPLL_FIXED_DIV);
  136. }
  137. static const struct clk_ops mpfs_clk_msspll_ops = {
  138. .recalc_rate = mpfs_clk_msspll_recalc_rate,
  139. };
  140. #define CLK_PLL(_id, _name, _parent, _shift, _width, _flags, _offset) { \
  141. .id = _id, \
  142. .flags = _flags, \
  143. .shift = _shift, \
  144. .width = _width, \
  145. .reg_offset = _offset, \
  146. .hw.init = CLK_HW_INIT_PARENTS_DATA(_name, _parent, &mpfs_clk_msspll_ops, 0), \
  147. }
  148. static struct mpfs_msspll_hw_clock mpfs_msspll_clks[] = {
  149. CLK_PLL(CLK_MSSPLL_INTERNAL, "clk_msspll_internal", mpfs_ext_ref, MSSPLL_FBDIV_SHIFT,
  150. MSSPLL_FBDIV_WIDTH, 0, REG_MSSPLL_SSCG_2_CR),
  151. };
  152. static int mpfs_clk_register_mssplls(struct device *dev, struct mpfs_msspll_hw_clock *msspll_hws,
  153. unsigned int num_clks, struct mpfs_clock_data *data)
  154. {
  155. unsigned int i;
  156. int ret;
  157. for (i = 0; i < num_clks; i++) {
  158. struct mpfs_msspll_hw_clock *msspll_hw = &msspll_hws[i];
  159. msspll_hw->base = data->msspll_base;
  160. ret = devm_clk_hw_register(dev, &msspll_hw->hw);
  161. if (ret)
  162. return dev_err_probe(dev, ret, "failed to register msspll id: %d\n",
  163. CLK_MSSPLL_INTERNAL);
  164. data->hw_data.hws[msspll_hw->id] = &msspll_hw->hw;
  165. }
  166. return 0;
  167. }
  168. /*
  169. * MSS PLL output clocks
  170. */
  171. #define CLK_PLL_OUT(_id, _name, _parent, _flags, _shift, _width, _offset) { \
  172. .id = _id, \
  173. .output.shift = _shift, \
  174. .output.width = _width, \
  175. .output.table = NULL, \
  176. .reg_offset = _offset, \
  177. .output.flags = _flags, \
  178. .output.hw.init = CLK_HW_INIT(_name, _parent, &clk_divider_ops, 0), \
  179. .output.lock = &mpfs_clk_lock, \
  180. }
  181. static struct mpfs_msspll_out_hw_clock mpfs_msspll_out_clks[] = {
  182. CLK_PLL_OUT(CLK_MSSPLL0, "clk_msspll", "clk_msspll_internal", CLK_DIVIDER_ONE_BASED,
  183. MSSPLL_POSTDIV02_SHIFT, MSSPLL_POSTDIV_WIDTH, REG_MSSPLL_POSTDIV01_CR),
  184. CLK_PLL_OUT(CLK_MSSPLL1, "clk_msspll1", "clk_msspll_internal", CLK_DIVIDER_ONE_BASED,
  185. MSSPLL_POSTDIV13_SHIFT, MSSPLL_POSTDIV_WIDTH, REG_MSSPLL_POSTDIV01_CR),
  186. CLK_PLL_OUT(CLK_MSSPLL2, "clk_msspll2", "clk_msspll_internal", CLK_DIVIDER_ONE_BASED,
  187. MSSPLL_POSTDIV02_SHIFT, MSSPLL_POSTDIV_WIDTH, REG_MSSPLL_POSTDIV23_CR),
  188. CLK_PLL_OUT(CLK_MSSPLL3, "clk_msspll3", "clk_msspll_internal", CLK_DIVIDER_ONE_BASED,
  189. MSSPLL_POSTDIV13_SHIFT, MSSPLL_POSTDIV_WIDTH, REG_MSSPLL_POSTDIV23_CR),
  190. };
  191. static int mpfs_clk_register_msspll_outs(struct device *dev,
  192. struct mpfs_msspll_out_hw_clock *msspll_out_hws,
  193. unsigned int num_clks, struct mpfs_clock_data *data)
  194. {
  195. unsigned int i;
  196. int ret;
  197. for (i = 0; i < num_clks; i++) {
  198. struct mpfs_msspll_out_hw_clock *msspll_out_hw = &msspll_out_hws[i];
  199. msspll_out_hw->output.reg = data->msspll_base + msspll_out_hw->reg_offset;
  200. ret = devm_clk_hw_register(dev, &msspll_out_hw->output.hw);
  201. if (ret)
  202. return dev_err_probe(dev, ret, "failed to register msspll out id: %d\n",
  203. msspll_out_hw->id);
  204. data->hw_data.hws[msspll_out_hw->id] = &msspll_out_hw->output.hw;
  205. }
  206. return 0;
  207. }
  208. /*
  209. * "CFG" clocks
  210. */
  211. static unsigned long mpfs_cfg_clk_recalc_rate(struct clk_hw *hw, unsigned long prate)
  212. {
  213. struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw);
  214. struct mpfs_cfg_clock *cfg = &cfg_hw->cfg;
  215. u32 val;
  216. regmap_read(cfg->map, cfg->map_offset, &val);
  217. val >>= cfg->shift;
  218. val &= clk_div_mask(cfg->width);
  219. return divider_recalc_rate(hw, prate, val, cfg->table, cfg->flags, cfg->width);
  220. }
  221. static int mpfs_cfg_clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  222. {
  223. struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw);
  224. struct mpfs_cfg_clock *cfg = &cfg_hw->cfg;
  225. return divider_determine_rate(hw, req, cfg->table, cfg->width, 0);
  226. }
  227. static int mpfs_cfg_clk_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long prate)
  228. {
  229. struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw);
  230. struct mpfs_cfg_clock *cfg = &cfg_hw->cfg;
  231. int divider_setting;
  232. u32 val;
  233. u32 mask;
  234. divider_setting = divider_get_val(rate, prate, cfg->table, cfg->width, 0);
  235. if (divider_setting < 0)
  236. return divider_setting;
  237. mask = clk_div_mask(cfg->width) << cfg->shift;
  238. val = divider_setting << cfg->shift;
  239. regmap_update_bits(cfg->map, cfg->map_offset, val, mask);
  240. return 0;
  241. }
  242. static const struct clk_ops mpfs_clk_cfg_ops = {
  243. .recalc_rate = mpfs_cfg_clk_recalc_rate,
  244. .determine_rate = mpfs_cfg_clk_determine_rate,
  245. .set_rate = mpfs_cfg_clk_set_rate,
  246. };
  247. #define CLK_CFG(_id, _name, _parent, _shift, _width, _table, _flags, _offset) { \
  248. .id = _id, \
  249. .cfg.shift = _shift, \
  250. .cfg.width = _width, \
  251. .cfg.table = _table, \
  252. .cfg.map_offset = _offset, \
  253. .cfg.flags = _flags, \
  254. .hw.init = CLK_HW_INIT(_name, _parent, &mpfs_clk_cfg_ops, 0), \
  255. }
  256. #define CLK_CPU_OFFSET 0u
  257. #define CLK_AXI_OFFSET 1u
  258. #define CLK_AHB_OFFSET 2u
  259. #define CLK_RTCREF_OFFSET 3u
  260. static struct mpfs_cfg_hw_clock mpfs_cfg_clks[] = {
  261. CLK_CFG(CLK_CPU, "clk_cpu", "clk_msspll", 0, 2, mpfs_div_cpu_axi_table, 0,
  262. REG_CLOCK_CONFIG_CR),
  263. CLK_CFG(CLK_AXI, "clk_axi", "clk_msspll", 2, 2, mpfs_div_cpu_axi_table, 0,
  264. REG_CLOCK_CONFIG_CR),
  265. CLK_CFG(CLK_AHB, "clk_ahb", "clk_msspll", 4, 2, mpfs_div_ahb_table, 0,
  266. REG_CLOCK_CONFIG_CR),
  267. {
  268. .id = CLK_RTCREF,
  269. .cfg.shift = 0,
  270. .cfg.width = 12,
  271. .cfg.table = mpfs_div_rtcref_table,
  272. .cfg.map_offset = REG_RTC_CLOCK_CR,
  273. .cfg.flags = CLK_DIVIDER_ONE_BASED,
  274. .hw.init =
  275. CLK_HW_INIT_PARENTS_DATA("clk_rtcref", mpfs_ext_ref, &mpfs_clk_cfg_ops, 0),
  276. }
  277. };
  278. static int mpfs_clk_register_cfgs(struct device *dev, struct mpfs_cfg_hw_clock *cfg_hws,
  279. unsigned int num_clks, struct mpfs_clock_data *data)
  280. {
  281. unsigned int i, id;
  282. int ret;
  283. for (i = 0; i < num_clks; i++) {
  284. struct mpfs_cfg_hw_clock *cfg_hw = &cfg_hws[i];
  285. cfg_hw->cfg.map = data->regmap;
  286. ret = devm_clk_hw_register(dev, &cfg_hw->hw);
  287. if (ret)
  288. return dev_err_probe(dev, ret, "failed to register clock id: %d\n",
  289. cfg_hw->id);
  290. id = cfg_hw->id;
  291. data->hw_data.hws[id] = &cfg_hw->hw;
  292. }
  293. return 0;
  294. }
  295. /*
  296. * peripheral clocks - devices connected to axi or ahb buses.
  297. */
  298. static int mpfs_periph_clk_enable(struct clk_hw *hw)
  299. {
  300. struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
  301. struct mpfs_periph_clock *periph = &periph_hw->periph;
  302. regmap_update_bits(periph->map, periph->map_offset,
  303. BIT(periph->shift), BIT(periph->shift));
  304. return 0;
  305. }
  306. static void mpfs_periph_clk_disable(struct clk_hw *hw)
  307. {
  308. struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
  309. struct mpfs_periph_clock *periph = &periph_hw->periph;
  310. regmap_update_bits(periph->map, periph->map_offset, BIT(periph->shift), 0);
  311. }
  312. static int mpfs_periph_clk_is_enabled(struct clk_hw *hw)
  313. {
  314. struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
  315. struct mpfs_periph_clock *periph = &periph_hw->periph;
  316. u32 val;
  317. regmap_read(periph->map, periph->map_offset, &val);
  318. return !!(val & BIT(periph->shift));
  319. }
  320. static const struct clk_ops mpfs_periph_clk_ops = {
  321. .enable = mpfs_periph_clk_enable,
  322. .disable = mpfs_periph_clk_disable,
  323. .is_enabled = mpfs_periph_clk_is_enabled,
  324. };
  325. #define CLK_PERIPH(_id, _name, _parent, _shift, _flags) { \
  326. .id = _id, \
  327. .periph.map_offset = REG_SUBBLK_CLOCK_CR, \
  328. .periph.shift = _shift, \
  329. .hw.init = CLK_HW_INIT_HW(_name, _parent, &mpfs_periph_clk_ops, _flags), \
  330. }
  331. #define PARENT_CLK(PARENT) (&mpfs_cfg_clks[CLK_##PARENT##_OFFSET].hw)
  332. /*
  333. * Critical clocks:
  334. * - CLK_ENVM: reserved by hart software services (hss) superloop monitor/m mode interrupt
  335. * trap handler
  336. * - CLK_MMUART0: reserved by the hss
  337. * - CLK_DDRC: provides clock to the ddr subsystem
  338. * - CLK_RTC: the onboard RTC's AHB bus clock must be kept running as the rtc will stop
  339. * if the AHB interface clock is disabled
  340. * - CLK_FICx: these provide the processor side clocks to the "FIC" (Fabric InterConnect)
  341. * clock domain crossers which provide the interface to the FPGA fabric. Disabling them
  342. * causes the FPGA fabric to go into reset.
  343. * - CLK_ATHENA: The athena clock is FIC4, which is reserved for the Athena TeraFire.
  344. */
  345. static struct mpfs_periph_hw_clock mpfs_periph_clks[] = {
  346. CLK_PERIPH(CLK_ENVM, "clk_periph_envm", PARENT_CLK(AHB), 0, CLK_IS_CRITICAL),
  347. CLK_PERIPH(CLK_MAC0, "clk_periph_mac0", PARENT_CLK(AHB), 1, 0),
  348. CLK_PERIPH(CLK_MAC1, "clk_periph_mac1", PARENT_CLK(AHB), 2, 0),
  349. CLK_PERIPH(CLK_MMC, "clk_periph_mmc", PARENT_CLK(AHB), 3, 0),
  350. CLK_PERIPH(CLK_TIMER, "clk_periph_timer", PARENT_CLK(RTCREF), 4, 0),
  351. CLK_PERIPH(CLK_MMUART0, "clk_periph_mmuart0", PARENT_CLK(AHB), 5, CLK_IS_CRITICAL),
  352. CLK_PERIPH(CLK_MMUART1, "clk_periph_mmuart1", PARENT_CLK(AHB), 6, 0),
  353. CLK_PERIPH(CLK_MMUART2, "clk_periph_mmuart2", PARENT_CLK(AHB), 7, 0),
  354. CLK_PERIPH(CLK_MMUART3, "clk_periph_mmuart3", PARENT_CLK(AHB), 8, 0),
  355. CLK_PERIPH(CLK_MMUART4, "clk_periph_mmuart4", PARENT_CLK(AHB), 9, 0),
  356. CLK_PERIPH(CLK_SPI0, "clk_periph_spi0", PARENT_CLK(AHB), 10, 0),
  357. CLK_PERIPH(CLK_SPI1, "clk_periph_spi1", PARENT_CLK(AHB), 11, 0),
  358. CLK_PERIPH(CLK_I2C0, "clk_periph_i2c0", PARENT_CLK(AHB), 12, 0),
  359. CLK_PERIPH(CLK_I2C1, "clk_periph_i2c1", PARENT_CLK(AHB), 13, 0),
  360. CLK_PERIPH(CLK_CAN0, "clk_periph_can0", PARENT_CLK(AHB), 14, 0),
  361. CLK_PERIPH(CLK_CAN1, "clk_periph_can1", PARENT_CLK(AHB), 15, 0),
  362. CLK_PERIPH(CLK_USB, "clk_periph_usb", PARENT_CLK(AHB), 16, 0),
  363. CLK_PERIPH(CLK_RTC, "clk_periph_rtc", PARENT_CLK(AHB), 18, CLK_IS_CRITICAL),
  364. CLK_PERIPH(CLK_QSPI, "clk_periph_qspi", PARENT_CLK(AHB), 19, 0),
  365. CLK_PERIPH(CLK_GPIO0, "clk_periph_gpio0", PARENT_CLK(AHB), 20, 0),
  366. CLK_PERIPH(CLK_GPIO1, "clk_periph_gpio1", PARENT_CLK(AHB), 21, 0),
  367. CLK_PERIPH(CLK_GPIO2, "clk_periph_gpio2", PARENT_CLK(AHB), 22, 0),
  368. CLK_PERIPH(CLK_DDRC, "clk_periph_ddrc", PARENT_CLK(AHB), 23, CLK_IS_CRITICAL),
  369. CLK_PERIPH(CLK_FIC0, "clk_periph_fic0", PARENT_CLK(AXI), 24, CLK_IS_CRITICAL),
  370. CLK_PERIPH(CLK_FIC1, "clk_periph_fic1", PARENT_CLK(AXI), 25, CLK_IS_CRITICAL),
  371. CLK_PERIPH(CLK_FIC2, "clk_periph_fic2", PARENT_CLK(AXI), 26, CLK_IS_CRITICAL),
  372. CLK_PERIPH(CLK_FIC3, "clk_periph_fic3", PARENT_CLK(AXI), 27, CLK_IS_CRITICAL),
  373. CLK_PERIPH(CLK_ATHENA, "clk_periph_athena", PARENT_CLK(AXI), 28, CLK_IS_CRITICAL),
  374. CLK_PERIPH(CLK_CFM, "clk_periph_cfm", PARENT_CLK(AHB), 29, 0),
  375. };
  376. static int mpfs_clk_register_periphs(struct device *dev, struct mpfs_periph_hw_clock *periph_hws,
  377. int num_clks, struct mpfs_clock_data *data)
  378. {
  379. unsigned int i, id;
  380. int ret;
  381. for (i = 0; i < num_clks; i++) {
  382. struct mpfs_periph_hw_clock *periph_hw = &periph_hws[i];
  383. periph_hw->periph.map = data->regmap;
  384. ret = devm_clk_hw_register(dev, &periph_hw->hw);
  385. if (ret)
  386. return dev_err_probe(dev, ret, "failed to register clock id: %d\n",
  387. periph_hw->id);
  388. id = periph_hws[i].id;
  389. data->hw_data.hws[id] = &periph_hw->hw;
  390. }
  391. return 0;
  392. }
  393. static inline int mpfs_clk_syscon_probe(struct mpfs_clock_data *clk_data,
  394. struct platform_device *pdev)
  395. {
  396. clk_data->regmap = syscon_regmap_lookup_by_compatible("microchip,mpfs-mss-top-sysreg");
  397. if (IS_ERR(clk_data->regmap))
  398. return PTR_ERR(clk_data->regmap);
  399. clk_data->msspll_base = devm_platform_ioremap_resource(pdev, 0);
  400. if (IS_ERR(clk_data->msspll_base))
  401. return PTR_ERR(clk_data->msspll_base);
  402. return 0;
  403. }
  404. static inline int mpfs_clk_old_format_probe(struct mpfs_clock_data *clk_data,
  405. struct platform_device *pdev)
  406. {
  407. struct device *dev = &pdev->dev;
  408. dev_warn(&pdev->dev, "falling back to old devicetree format");
  409. clk_data->base = devm_platform_ioremap_resource(pdev, 0);
  410. if (IS_ERR(clk_data->base))
  411. return PTR_ERR(clk_data->base);
  412. clk_data->msspll_base = devm_platform_ioremap_resource(pdev, 1);
  413. if (IS_ERR(clk_data->msspll_base))
  414. return PTR_ERR(clk_data->msspll_base);
  415. clk_data->regmap = devm_regmap_init_mmio(dev, clk_data->base, &mpfs_clk_regmap_config);
  416. if (IS_ERR(clk_data->regmap))
  417. return PTR_ERR(clk_data->regmap);
  418. return mpfs_reset_controller_register(dev, clk_data->regmap);
  419. }
  420. static int mpfs_clk_probe(struct platform_device *pdev)
  421. {
  422. struct device *dev = &pdev->dev;
  423. struct mpfs_clock_data *clk_data;
  424. unsigned int num_clks;
  425. int ret;
  426. /* CLK_RESERVED is not part of clock arrays, so add 1 */
  427. num_clks = ARRAY_SIZE(mpfs_msspll_clks) + ARRAY_SIZE(mpfs_msspll_out_clks)
  428. + ARRAY_SIZE(mpfs_cfg_clks) + ARRAY_SIZE(mpfs_periph_clks) + 1;
  429. clk_data = devm_kzalloc(dev, struct_size(clk_data, hw_data.hws, num_clks), GFP_KERNEL);
  430. if (!clk_data)
  431. return -ENOMEM;
  432. ret = mpfs_clk_syscon_probe(clk_data, pdev);
  433. if (ret) {
  434. ret = mpfs_clk_old_format_probe(clk_data, pdev);
  435. if (ret)
  436. return ret;
  437. }
  438. clk_data->hw_data.num = num_clks;
  439. clk_data->dev = dev;
  440. dev_set_drvdata(dev, clk_data);
  441. ret = mpfs_clk_register_mssplls(dev, mpfs_msspll_clks, ARRAY_SIZE(mpfs_msspll_clks),
  442. clk_data);
  443. if (ret)
  444. return ret;
  445. ret = mpfs_clk_register_msspll_outs(dev, mpfs_msspll_out_clks,
  446. ARRAY_SIZE(mpfs_msspll_out_clks),
  447. clk_data);
  448. if (ret)
  449. return ret;
  450. ret = mpfs_clk_register_cfgs(dev, mpfs_cfg_clks, ARRAY_SIZE(mpfs_cfg_clks), clk_data);
  451. if (ret)
  452. return ret;
  453. ret = mpfs_clk_register_periphs(dev, mpfs_periph_clks, ARRAY_SIZE(mpfs_periph_clks),
  454. clk_data);
  455. if (ret)
  456. return ret;
  457. return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, &clk_data->hw_data);
  458. }
  459. static const struct of_device_id mpfs_clk_of_match_table[] = {
  460. { .compatible = "microchip,mpfs-clkcfg", },
  461. {}
  462. };
  463. MODULE_DEVICE_TABLE(of, mpfs_clk_of_match_table);
  464. static struct platform_driver mpfs_clk_driver = {
  465. .probe = mpfs_clk_probe,
  466. .driver = {
  467. .name = "microchip-mpfs-clkcfg",
  468. .of_match_table = mpfs_clk_of_match_table,
  469. },
  470. };
  471. static int __init clk_mpfs_init(void)
  472. {
  473. return platform_driver_register(&mpfs_clk_driver);
  474. }
  475. core_initcall(clk_mpfs_init);
  476. static void __exit clk_mpfs_exit(void)
  477. {
  478. platform_driver_unregister(&mpfs_clk_driver);
  479. }
  480. module_exit(clk_mpfs_exit);
  481. MODULE_DESCRIPTION("Microchip PolarFire SoC Clock Driver");
  482. MODULE_AUTHOR("Padmarao Begari <padmarao.begari@microchip.com>");
  483. MODULE_AUTHOR("Daire McNamara <daire.mcnamara@microchip.com>");
  484. MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
  485. MODULE_IMPORT_NS("MCHP_CLK_MPFS");