cm_common.c 10 KB

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