cm_common.c 11 KB

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