cm_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OMAP2+ common Clock Management (CM) IP block functions
  4. *
  5. * Copyright (C) 2012 Texas Instruments, Inc.
  6. * Paul Walmsley
  7. *
  8. * XXX This code should eventually be moved to a CM driver.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/bug.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include "cm2xxx.h"
  17. #include "cm3xxx.h"
  18. #include "cm33xx.h"
  19. #include "cm44xx.h"
  20. #include "clock.h"
  21. /*
  22. * cm_ll_data: function pointers to SoC-specific implementations of
  23. * common CM functions
  24. */
  25. static struct cm_ll_data null_cm_ll_data;
  26. static const struct cm_ll_data *cm_ll_data = &null_cm_ll_data;
  27. /* cm_base: base virtual address of the CM IP block */
  28. struct omap_domain_base cm_base;
  29. /* cm2_base: base virtual address of the CM2 IP block (OMAP44xx only) */
  30. struct omap_domain_base cm2_base;
  31. #define CM_NO_CLOCKS 0x1
  32. #define CM_SINGLE_INSTANCE 0x2
  33. /**
  34. * cm_split_idlest_reg - split CM_IDLEST reg addr into its components
  35. * @idlest_reg: CM_IDLEST* virtual address
  36. * @prcm_inst: pointer to an s16 to return the PRCM instance offset
  37. * @idlest_reg_id: pointer to a u8 to return the CM_IDLESTx register ID
  38. *
  39. * Given an absolute CM_IDLEST register address @idlest_reg, passes
  40. * the PRCM instance offset and IDLEST register ID back to the caller
  41. * via the @prcm_inst and @idlest_reg_id. Returns -EINVAL upon error,
  42. * or 0 upon success. XXX This function is only needed until absolute
  43. * register addresses are removed from the OMAP struct clk records.
  44. */
  45. int cm_split_idlest_reg(struct clk_omap_reg *idlest_reg, s16 *prcm_inst,
  46. u8 *idlest_reg_id)
  47. {
  48. int ret;
  49. if (!cm_ll_data->split_idlest_reg) {
  50. WARN_ONCE(1, "cm: %s: no low-level function defined\n",
  51. __func__);
  52. return -EINVAL;
  53. }
  54. ret = cm_ll_data->split_idlest_reg(idlest_reg, prcm_inst,
  55. idlest_reg_id);
  56. *prcm_inst -= cm_base.offset;
  57. return ret;
  58. }
  59. /**
  60. * omap_cm_wait_module_ready - wait for a module to leave idle or standby
  61. * @part: PRCM partition
  62. * @prcm_mod: PRCM module offset
  63. * @idlest_reg: CM_IDLESTx register
  64. * @idlest_shift: shift of the bit in the CM_IDLEST* register to check
  65. *
  66. * Wait for the PRCM to indicate that the module identified by
  67. * (@prcm_mod, @idlest_id, @idlest_shift) is clocked. Return 0 upon
  68. * success, -EBUSY if the module doesn't enable in time, or -EINVAL if
  69. * no per-SoC wait_module_ready() function pointer has been registered
  70. * or if the idlest register is unknown on the SoC.
  71. */
  72. int omap_cm_wait_module_ready(u8 part, s16 prcm_mod, u16 idlest_reg,
  73. u8 idlest_shift)
  74. {
  75. if (!cm_ll_data->wait_module_ready) {
  76. WARN_ONCE(1, "cm: %s: no low-level function defined\n",
  77. __func__);
  78. return -EINVAL;
  79. }
  80. return cm_ll_data->wait_module_ready(part, prcm_mod, idlest_reg,
  81. idlest_shift);
  82. }
  83. /**
  84. * omap_cm_wait_module_idle - wait for a module to enter idle or standby
  85. * @part: PRCM partition
  86. * @prcm_mod: PRCM module offset
  87. * @idlest_reg: CM_IDLESTx register
  88. * @idlest_shift: shift of the bit in the CM_IDLEST* register to check
  89. *
  90. * Wait for the PRCM to indicate that the module identified by
  91. * (@prcm_mod, @idlest_id, @idlest_shift) is no longer clocked. Return
  92. * 0 upon success, -EBUSY if the module doesn't enable in time, or
  93. * -EINVAL if no per-SoC wait_module_idle() function pointer has been
  94. * registered or if the idlest register is unknown on the SoC.
  95. */
  96. int omap_cm_wait_module_idle(u8 part, s16 prcm_mod, u16 idlest_reg,
  97. u8 idlest_shift)
  98. {
  99. if (!cm_ll_data->wait_module_idle) {
  100. WARN_ONCE(1, "cm: %s: no low-level function defined\n",
  101. __func__);
  102. return -EINVAL;
  103. }
  104. return cm_ll_data->wait_module_idle(part, prcm_mod, idlest_reg,
  105. idlest_shift);
  106. }
  107. /**
  108. * omap_cm_module_enable - enable a module
  109. * @mode: target mode for the module
  110. * @part: PRCM partition
  111. * @inst: PRCM instance
  112. * @clkctrl_offs: CM_CLKCTRL register offset for the module
  113. *
  114. * Enables clocks for a module identified by (@part, @inst, @clkctrl_offs)
  115. * making its IO space accessible. Return 0 upon success, -EINVAL if no
  116. * per-SoC module_enable() function pointer has been registered.
  117. */
  118. int omap_cm_module_enable(u8 mode, u8 part, u16 inst, u16 clkctrl_offs)
  119. {
  120. if (!cm_ll_data->module_enable) {
  121. WARN_ONCE(1, "cm: %s: no low-level function defined\n",
  122. __func__);
  123. return -EINVAL;
  124. }
  125. cm_ll_data->module_enable(mode, part, inst, clkctrl_offs);
  126. return 0;
  127. }
  128. /**
  129. * omap_cm_module_disable - disable a module
  130. * @part: PRCM partition
  131. * @inst: PRCM instance
  132. * @clkctrl_offs: CM_CLKCTRL register offset for the module
  133. *
  134. * Disables clocks for a module identified by (@part, @inst, @clkctrl_offs)
  135. * makings its IO space inaccessible. Return 0 upon success, -EINVAL if
  136. * no per-SoC module_disable() function pointer has been registered.
  137. */
  138. int omap_cm_module_disable(u8 part, u16 inst, u16 clkctrl_offs)
  139. {
  140. if (!cm_ll_data->module_disable) {
  141. WARN_ONCE(1, "cm: %s: no low-level function defined\n",
  142. __func__);
  143. return -EINVAL;
  144. }
  145. cm_ll_data->module_disable(part, inst, clkctrl_offs);
  146. return 0;
  147. }
  148. u32 omap_cm_xlate_clkctrl(u8 part, u16 inst, u16 clkctrl_offs)
  149. {
  150. if (!cm_ll_data->xlate_clkctrl) {
  151. WARN_ONCE(1, "cm: %s: no low-level function defined\n",
  152. __func__);
  153. return 0;
  154. }
  155. return cm_ll_data->xlate_clkctrl(part, inst, clkctrl_offs);
  156. }
  157. /**
  158. * cm_register - register per-SoC low-level data with the CM
  159. * @cld: low-level per-SoC OMAP CM data & function pointers to register
  160. *
  161. * Register per-SoC low-level OMAP CM data and function pointers with
  162. * the OMAP CM common interface. The caller must keep the data
  163. * pointed to by @cld valid until it calls cm_unregister() and
  164. * it returns successfully. Returns 0 upon success, -EINVAL if @cld
  165. * is NULL, or -EEXIST if cm_register() has already been called
  166. * without an intervening cm_unregister().
  167. */
  168. int cm_register(const struct cm_ll_data *cld)
  169. {
  170. if (!cld)
  171. return -EINVAL;
  172. if (cm_ll_data != &null_cm_ll_data)
  173. return -EEXIST;
  174. cm_ll_data = cld;
  175. return 0;
  176. }
  177. /**
  178. * cm_unregister - unregister per-SoC low-level data & function pointers
  179. * @cld: low-level per-SoC OMAP CM data & function pointers to unregister
  180. *
  181. * Unregister per-SoC low-level OMAP CM data and function pointers
  182. * that were previously registered with cm_register(). The
  183. * caller may not destroy any of the data pointed to by @cld until
  184. * this function returns successfully. Returns 0 upon success, or
  185. * -EINVAL if @cld is NULL or if @cld does not match the struct
  186. * cm_ll_data * previously registered by cm_register().
  187. */
  188. int cm_unregister(const struct cm_ll_data *cld)
  189. {
  190. if (!cld || cm_ll_data != cld)
  191. return -EINVAL;
  192. cm_ll_data = &null_cm_ll_data;
  193. return 0;
  194. }
  195. #if defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_SOC_OMAP5) || \
  196. defined(CONFIG_SOC_DRA7XX)
  197. static struct omap_prcm_init_data cm_data __initdata = {
  198. .index = TI_CLKM_CM,
  199. .init = omap4_cm_init,
  200. };
  201. static struct omap_prcm_init_data cm2_data __initdata = {
  202. .index = TI_CLKM_CM2,
  203. .init = omap4_cm_init,
  204. };
  205. #endif
  206. #ifdef CONFIG_ARCH_OMAP2
  207. static struct omap_prcm_init_data omap2_prcm_data __initdata = {
  208. .index = TI_CLKM_CM,
  209. .init = omap2xxx_cm_init,
  210. .flags = CM_NO_CLOCKS | CM_SINGLE_INSTANCE,
  211. };
  212. #endif
  213. #ifdef CONFIG_ARCH_OMAP3
  214. static struct omap_prcm_init_data omap3_cm_data __initdata = {
  215. .index = TI_CLKM_CM,
  216. .init = omap3xxx_cm_init,
  217. .flags = CM_SINGLE_INSTANCE,
  218. /*
  219. * IVA2 offset is a negative value, must offset the cm_base address
  220. * by this to get it to positive side on the iomap
  221. */
  222. .offset = -OMAP3430_IVA2_MOD,
  223. };
  224. #endif
  225. #if defined(CONFIG_SOC_AM33XX) || defined(CONFIG_SOC_TI81XX)
  226. static struct omap_prcm_init_data am3_prcm_data __initdata = {
  227. .index = TI_CLKM_CM,
  228. .flags = CM_NO_CLOCKS | CM_SINGLE_INSTANCE,
  229. .init = am33xx_cm_init,
  230. };
  231. #endif
  232. #ifdef CONFIG_SOC_AM43XX
  233. static struct omap_prcm_init_data am4_prcm_data __initdata = {
  234. .index = TI_CLKM_CM,
  235. .flags = CM_NO_CLOCKS | CM_SINGLE_INSTANCE,
  236. .init = omap4_cm_init,
  237. };
  238. #endif
  239. static const struct of_device_id omap_cm_dt_match_table[] __initconst = {
  240. #ifdef CONFIG_ARCH_OMAP2
  241. { .compatible = "ti,omap2-prcm", .data = &omap2_prcm_data },
  242. #endif
  243. #ifdef CONFIG_ARCH_OMAP3
  244. { .compatible = "ti,omap3-cm", .data = &omap3_cm_data },
  245. #endif
  246. #ifdef CONFIG_ARCH_OMAP4
  247. { .compatible = "ti,omap4-cm1", .data = &cm_data },
  248. { .compatible = "ti,omap4-cm2", .data = &cm2_data },
  249. #endif
  250. #ifdef CONFIG_SOC_OMAP5
  251. { .compatible = "ti,omap5-cm-core-aon", .data = &cm_data },
  252. { .compatible = "ti,omap5-cm-core", .data = &cm2_data },
  253. #endif
  254. #ifdef CONFIG_SOC_DRA7XX
  255. { .compatible = "ti,dra7-cm-core-aon", .data = &cm_data },
  256. { .compatible = "ti,dra7-cm-core", .data = &cm2_data },
  257. #endif
  258. #ifdef CONFIG_SOC_AM33XX
  259. { .compatible = "ti,am3-prcm", .data = &am3_prcm_data },
  260. #endif
  261. #ifdef CONFIG_SOC_AM43XX
  262. { .compatible = "ti,am4-prcm", .data = &am4_prcm_data },
  263. #endif
  264. #ifdef CONFIG_SOC_TI81XX
  265. { .compatible = "ti,dm814-prcm", .data = &am3_prcm_data },
  266. { .compatible = "ti,dm816-prcm", .data = &am3_prcm_data },
  267. #endif
  268. { }
  269. };
  270. /**
  271. * omap2_cm_base_init - initialize iomappings for the CM drivers
  272. *
  273. * Detects and initializes the iomappings for the CM driver, based
  274. * on the DT data. Returns 0 in success, negative error value
  275. * otherwise.
  276. */
  277. int __init omap2_cm_base_init(void)
  278. {
  279. struct device_node *np;
  280. const struct of_device_id *match;
  281. struct omap_prcm_init_data *data;
  282. struct resource res;
  283. int ret;
  284. struct omap_domain_base *mem = NULL;
  285. for_each_matching_node_and_match(np, omap_cm_dt_match_table, &match) {
  286. data = (struct omap_prcm_init_data *)match->data;
  287. ret = of_address_to_resource(np, 0, &res);
  288. if (ret) {
  289. of_node_put(np);
  290. return ret;
  291. }
  292. if (data->index == TI_CLKM_CM)
  293. mem = &cm_base;
  294. if (data->index == TI_CLKM_CM2)
  295. mem = &cm2_base;
  296. data->mem = ioremap(res.start, resource_size(&res));
  297. if (mem) {
  298. mem->pa = res.start + data->offset;
  299. mem->va = data->mem + data->offset;
  300. mem->offset = data->offset;
  301. }
  302. data->np = np;
  303. if (data->init && (data->flags & CM_SINGLE_INSTANCE ||
  304. (cm_base.va && cm2_base.va)))
  305. data->init(data);
  306. }
  307. return 0;
  308. }
  309. /**
  310. * omap_cm_init - low level init for the CM drivers
  311. *
  312. * Initializes the low level clock infrastructure for CM drivers.
  313. * Returns 0 in success, negative error value in failure.
  314. */
  315. int __init omap_cm_init(void)
  316. {
  317. struct device_node *np;
  318. const struct of_device_id *match;
  319. const struct omap_prcm_init_data *data;
  320. int ret;
  321. for_each_matching_node_and_match(np, omap_cm_dt_match_table, &match) {
  322. data = match->data;
  323. if (data->flags & CM_NO_CLOCKS)
  324. continue;
  325. ret = omap2_clk_provider_init(np, data->index, NULL, data->mem);
  326. if (ret) {
  327. of_node_put(np);
  328. return ret;
  329. }
  330. }
  331. return 0;
  332. }