cminst44xx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * OMAP4 CM instance functions
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Copyright (C) 2008-2011 Texas Instruments, Inc.
  6. * Paul Walmsley
  7. * Rajendra Nayak <rnayak@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This is needed since CM instances can be in the PRM, PRCM_MPU, CM1,
  14. * or CM2 hardware modules. For example, the EMU_CM CM instance is in
  15. * the PRM hardware module. What a mess...
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include "clockdomain.h"
  23. #include "cm.h"
  24. #include "cm1_44xx.h"
  25. #include "cm2_44xx.h"
  26. #include "cm44xx.h"
  27. #include "cm-regbits-34xx.h"
  28. #include "prcm44xx.h"
  29. #include "prm44xx.h"
  30. #include "prcm_mpu44xx.h"
  31. #include "prcm-common.h"
  32. #define OMAP4430_IDLEST_SHIFT 16
  33. #define OMAP4430_IDLEST_MASK (0x3 << 16)
  34. #define OMAP4430_CLKTRCTRL_SHIFT 0
  35. #define OMAP4430_CLKTRCTRL_MASK (0x3 << 0)
  36. #define OMAP4430_MODULEMODE_SHIFT 0
  37. #define OMAP4430_MODULEMODE_MASK (0x3 << 0)
  38. /*
  39. * CLKCTRL_IDLEST_*: possible values for the CM_*_CLKCTRL.IDLEST bitfield:
  40. *
  41. * 0x0 func: Module is fully functional, including OCP
  42. * 0x1 trans: Module is performing transition: wakeup, or sleep, or sleep
  43. * abortion
  44. * 0x2 idle: Module is in Idle mode (only OCP part). It is functional if
  45. * using separate functional clock
  46. * 0x3 disabled: Module is disabled and cannot be accessed
  47. *
  48. */
  49. #define CLKCTRL_IDLEST_FUNCTIONAL 0x0
  50. #define CLKCTRL_IDLEST_INTRANSITION 0x1
  51. #define CLKCTRL_IDLEST_INTERFACE_IDLE 0x2
  52. #define CLKCTRL_IDLEST_DISABLED 0x3
  53. static void __iomem *_cm_bases[OMAP4_MAX_PRCM_PARTITIONS];
  54. /**
  55. * omap_cm_base_init - Populates the cm partitions
  56. *
  57. * Populates the base addresses of the _cm_bases
  58. * array used for read/write of cm module registers.
  59. */
  60. static void omap_cm_base_init(void)
  61. {
  62. _cm_bases[OMAP4430_PRM_PARTITION] = prm_base;
  63. _cm_bases[OMAP4430_CM1_PARTITION] = cm_base;
  64. _cm_bases[OMAP4430_CM2_PARTITION] = cm2_base;
  65. _cm_bases[OMAP4430_PRCM_MPU_PARTITION] = prcm_mpu_base;
  66. }
  67. /* Private functions */
  68. static u32 omap4_cminst_read_inst_reg(u8 part, u16 inst, u16 idx);
  69. /**
  70. * _clkctrl_idlest - read a CM_*_CLKCTRL register; mask & shift IDLEST bitfield
  71. * @part: PRCM partition ID that the CM_CLKCTRL register exists in
  72. * @inst: CM instance register offset (*_INST macro)
  73. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  74. *
  75. * Return the IDLEST bitfield of a CM_*_CLKCTRL register, shifted down to
  76. * bit 0.
  77. */
  78. static u32 _clkctrl_idlest(u8 part, u16 inst, u16 clkctrl_offs)
  79. {
  80. u32 v = omap4_cminst_read_inst_reg(part, inst, clkctrl_offs);
  81. v &= OMAP4430_IDLEST_MASK;
  82. v >>= OMAP4430_IDLEST_SHIFT;
  83. return v;
  84. }
  85. /**
  86. * _is_module_ready - can module registers be accessed without causing an abort?
  87. * @part: PRCM partition ID that the CM_CLKCTRL register exists in
  88. * @inst: CM instance register offset (*_INST macro)
  89. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  90. *
  91. * Returns true if the module's CM_*_CLKCTRL.IDLEST bitfield is either
  92. * *FUNCTIONAL or *INTERFACE_IDLE; false otherwise.
  93. */
  94. static bool _is_module_ready(u8 part, u16 inst, u16 clkctrl_offs)
  95. {
  96. u32 v;
  97. v = _clkctrl_idlest(part, inst, clkctrl_offs);
  98. return (v == CLKCTRL_IDLEST_FUNCTIONAL ||
  99. v == CLKCTRL_IDLEST_INTERFACE_IDLE) ? true : false;
  100. }
  101. /* Read a register in a CM instance */
  102. static u32 omap4_cminst_read_inst_reg(u8 part, u16 inst, u16 idx)
  103. {
  104. BUG_ON(part >= OMAP4_MAX_PRCM_PARTITIONS ||
  105. part == OMAP4430_INVALID_PRCM_PARTITION ||
  106. !_cm_bases[part]);
  107. return readl_relaxed(_cm_bases[part] + inst + idx);
  108. }
  109. /* Write into a register in a CM instance */
  110. static void omap4_cminst_write_inst_reg(u32 val, u8 part, u16 inst, u16 idx)
  111. {
  112. BUG_ON(part >= OMAP4_MAX_PRCM_PARTITIONS ||
  113. part == OMAP4430_INVALID_PRCM_PARTITION ||
  114. !_cm_bases[part]);
  115. writel_relaxed(val, _cm_bases[part] + inst + idx);
  116. }
  117. /* Read-modify-write a register in CM1. Caller must lock */
  118. static u32 omap4_cminst_rmw_inst_reg_bits(u32 mask, u32 bits, u8 part, u16 inst,
  119. s16 idx)
  120. {
  121. u32 v;
  122. v = omap4_cminst_read_inst_reg(part, inst, idx);
  123. v &= ~mask;
  124. v |= bits;
  125. omap4_cminst_write_inst_reg(v, part, inst, idx);
  126. return v;
  127. }
  128. static u32 omap4_cminst_set_inst_reg_bits(u32 bits, u8 part, u16 inst, s16 idx)
  129. {
  130. return omap4_cminst_rmw_inst_reg_bits(bits, bits, part, inst, idx);
  131. }
  132. static u32 omap4_cminst_clear_inst_reg_bits(u32 bits, u8 part, u16 inst,
  133. s16 idx)
  134. {
  135. return omap4_cminst_rmw_inst_reg_bits(bits, 0x0, part, inst, idx);
  136. }
  137. static u32 omap4_cminst_read_inst_reg_bits(u8 part, u16 inst, s16 idx, u32 mask)
  138. {
  139. u32 v;
  140. v = omap4_cminst_read_inst_reg(part, inst, idx);
  141. v &= mask;
  142. v >>= __ffs(mask);
  143. return v;
  144. }
  145. /*
  146. *
  147. */
  148. /**
  149. * _clktrctrl_write - write @c to a CM_CLKSTCTRL.CLKTRCTRL register bitfield
  150. * @c: CLKTRCTRL register bitfield (LSB = bit 0, i.e., unshifted)
  151. * @part: PRCM partition ID that the CM_CLKSTCTRL register exists in
  152. * @inst: CM instance register offset (*_INST macro)
  153. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  154. *
  155. * @c must be the unshifted value for CLKTRCTRL - i.e., this function
  156. * will handle the shift itself.
  157. */
  158. static void _clktrctrl_write(u8 c, u8 part, u16 inst, u16 cdoffs)
  159. {
  160. u32 v;
  161. v = omap4_cminst_read_inst_reg(part, inst, cdoffs + OMAP4_CM_CLKSTCTRL);
  162. v &= ~OMAP4430_CLKTRCTRL_MASK;
  163. v |= c << OMAP4430_CLKTRCTRL_SHIFT;
  164. omap4_cminst_write_inst_reg(v, part, inst, cdoffs + OMAP4_CM_CLKSTCTRL);
  165. }
  166. /**
  167. * omap4_cminst_is_clkdm_in_hwsup - is a clockdomain in hwsup idle mode?
  168. * @part: PRCM partition ID that the CM_CLKSTCTRL register exists in
  169. * @inst: CM instance register offset (*_INST macro)
  170. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  171. *
  172. * Returns true if the clockdomain referred to by (@part, @inst, @cdoffs)
  173. * is in hardware-supervised idle mode, or 0 otherwise.
  174. */
  175. static bool omap4_cminst_is_clkdm_in_hwsup(u8 part, u16 inst, u16 cdoffs)
  176. {
  177. u32 v;
  178. v = omap4_cminst_read_inst_reg(part, inst, cdoffs + OMAP4_CM_CLKSTCTRL);
  179. v &= OMAP4430_CLKTRCTRL_MASK;
  180. v >>= OMAP4430_CLKTRCTRL_SHIFT;
  181. return (v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ? true : false;
  182. }
  183. /**
  184. * omap4_cminst_clkdm_enable_hwsup - put a clockdomain in hwsup-idle mode
  185. * @part: PRCM partition ID that the clockdomain registers exist in
  186. * @inst: CM instance register offset (*_INST macro)
  187. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  188. *
  189. * Put a clockdomain referred to by (@part, @inst, @cdoffs) into
  190. * hardware-supervised idle mode. No return value.
  191. */
  192. static void omap4_cminst_clkdm_enable_hwsup(u8 part, u16 inst, u16 cdoffs)
  193. {
  194. _clktrctrl_write(OMAP34XX_CLKSTCTRL_ENABLE_AUTO, part, inst, cdoffs);
  195. }
  196. /**
  197. * omap4_cminst_clkdm_disable_hwsup - put a clockdomain in swsup-idle mode
  198. * @part: PRCM partition ID that the clockdomain registers exist in
  199. * @inst: CM instance register offset (*_INST macro)
  200. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  201. *
  202. * Put a clockdomain referred to by (@part, @inst, @cdoffs) into
  203. * software-supervised idle mode, i.e., controlled manually by the
  204. * Linux OMAP clockdomain code. No return value.
  205. */
  206. static void omap4_cminst_clkdm_disable_hwsup(u8 part, u16 inst, u16 cdoffs)
  207. {
  208. _clktrctrl_write(OMAP34XX_CLKSTCTRL_DISABLE_AUTO, part, inst, cdoffs);
  209. }
  210. /**
  211. * omap4_cminst_clkdm_force_sleep - try to take a clockdomain out of idle
  212. * @part: PRCM partition ID that the clockdomain registers exist in
  213. * @inst: CM instance register offset (*_INST macro)
  214. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  215. *
  216. * Take a clockdomain referred to by (@part, @inst, @cdoffs) out of idle,
  217. * waking it up. No return value.
  218. */
  219. static void omap4_cminst_clkdm_force_wakeup(u8 part, u16 inst, u16 cdoffs)
  220. {
  221. _clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_WAKEUP, part, inst, cdoffs);
  222. }
  223. /*
  224. *
  225. */
  226. static void omap4_cminst_clkdm_force_sleep(u8 part, u16 inst, u16 cdoffs)
  227. {
  228. _clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_SLEEP, part, inst, cdoffs);
  229. }
  230. /**
  231. * omap4_cminst_wait_module_ready - wait for a module to be in 'func' state
  232. * @part: PRCM partition ID that the CM_CLKCTRL register exists in
  233. * @inst: CM instance register offset (*_INST macro)
  234. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  235. * @bit_shift: bit shift for the register, ignored for OMAP4+
  236. *
  237. * Wait for the module IDLEST to be functional. If the idle state is in any
  238. * the non functional state (trans, idle or disabled), module and thus the
  239. * sysconfig cannot be accessed and will probably lead to an "imprecise
  240. * external abort"
  241. */
  242. static int omap4_cminst_wait_module_ready(u8 part, s16 inst, u16 clkctrl_offs,
  243. u8 bit_shift)
  244. {
  245. int i = 0;
  246. omap_test_timeout(_is_module_ready(part, inst, clkctrl_offs),
  247. MAX_MODULE_READY_TIME, i);
  248. return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;
  249. }
  250. /**
  251. * omap4_cminst_wait_module_idle - wait for a module to be in 'disabled'
  252. * state
  253. * @part: PRCM partition ID that the CM_CLKCTRL register exists in
  254. * @inst: CM instance register offset (*_INST macro)
  255. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  256. * @bit_shift: Bit shift for the register, ignored for OMAP4+
  257. *
  258. * Wait for the module IDLEST to be disabled. Some PRCM transition,
  259. * like reset assertion or parent clock de-activation must wait the
  260. * module to be fully disabled.
  261. */
  262. static int omap4_cminst_wait_module_idle(u8 part, s16 inst, u16 clkctrl_offs,
  263. u8 bit_shift)
  264. {
  265. int i = 0;
  266. omap_test_timeout((_clkctrl_idlest(part, inst, clkctrl_offs) ==
  267. CLKCTRL_IDLEST_DISABLED),
  268. MAX_MODULE_DISABLE_TIME, i);
  269. return (i < MAX_MODULE_DISABLE_TIME) ? 0 : -EBUSY;
  270. }
  271. /**
  272. * omap4_cminst_module_enable - Enable the modulemode inside CLKCTRL
  273. * @mode: Module mode (SW or HW)
  274. * @part: PRCM partition ID that the CM_CLKCTRL register exists in
  275. * @inst: CM instance register offset (*_INST macro)
  276. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  277. *
  278. * No return value.
  279. */
  280. static void omap4_cminst_module_enable(u8 mode, u8 part, u16 inst,
  281. u16 clkctrl_offs)
  282. {
  283. u32 v;
  284. v = omap4_cminst_read_inst_reg(part, inst, clkctrl_offs);
  285. v &= ~OMAP4430_MODULEMODE_MASK;
  286. v |= mode << OMAP4430_MODULEMODE_SHIFT;
  287. omap4_cminst_write_inst_reg(v, part, inst, clkctrl_offs);
  288. }
  289. /**
  290. * omap4_cminst_module_disable - Disable the module inside CLKCTRL
  291. * @part: PRCM partition ID that the CM_CLKCTRL register exists in
  292. * @inst: CM instance register offset (*_INST macro)
  293. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  294. *
  295. * No return value.
  296. */
  297. static void omap4_cminst_module_disable(u8 part, u16 inst, u16 clkctrl_offs)
  298. {
  299. u32 v;
  300. v = omap4_cminst_read_inst_reg(part, inst, clkctrl_offs);
  301. v &= ~OMAP4430_MODULEMODE_MASK;
  302. omap4_cminst_write_inst_reg(v, part, inst, clkctrl_offs);
  303. }
  304. /*
  305. * Clockdomain low-level functions
  306. */
  307. static int omap4_clkdm_add_wkup_sleep_dep(struct clockdomain *clkdm1,
  308. struct clockdomain *clkdm2)
  309. {
  310. omap4_cminst_set_inst_reg_bits((1 << clkdm2->dep_bit),
  311. clkdm1->prcm_partition,
  312. clkdm1->cm_inst, clkdm1->clkdm_offs +
  313. OMAP4_CM_STATICDEP);
  314. return 0;
  315. }
  316. static int omap4_clkdm_del_wkup_sleep_dep(struct clockdomain *clkdm1,
  317. struct clockdomain *clkdm2)
  318. {
  319. omap4_cminst_clear_inst_reg_bits((1 << clkdm2->dep_bit),
  320. clkdm1->prcm_partition,
  321. clkdm1->cm_inst, clkdm1->clkdm_offs +
  322. OMAP4_CM_STATICDEP);
  323. return 0;
  324. }
  325. static int omap4_clkdm_read_wkup_sleep_dep(struct clockdomain *clkdm1,
  326. struct clockdomain *clkdm2)
  327. {
  328. return omap4_cminst_read_inst_reg_bits(clkdm1->prcm_partition,
  329. clkdm1->cm_inst,
  330. clkdm1->clkdm_offs +
  331. OMAP4_CM_STATICDEP,
  332. (1 << clkdm2->dep_bit));
  333. }
  334. static int omap4_clkdm_clear_all_wkup_sleep_deps(struct clockdomain *clkdm)
  335. {
  336. struct clkdm_dep *cd;
  337. u32 mask = 0;
  338. if (!clkdm->prcm_partition)
  339. return 0;
  340. for (cd = clkdm->wkdep_srcs; cd && cd->clkdm_name; cd++) {
  341. if (!cd->clkdm)
  342. continue; /* only happens if data is erroneous */
  343. mask |= 1 << cd->clkdm->dep_bit;
  344. cd->wkdep_usecount = 0;
  345. }
  346. omap4_cminst_clear_inst_reg_bits(mask, clkdm->prcm_partition,
  347. clkdm->cm_inst, clkdm->clkdm_offs +
  348. OMAP4_CM_STATICDEP);
  349. return 0;
  350. }
  351. static int omap4_clkdm_sleep(struct clockdomain *clkdm)
  352. {
  353. if (clkdm->flags & CLKDM_CAN_HWSUP)
  354. omap4_cminst_clkdm_enable_hwsup(clkdm->prcm_partition,
  355. clkdm->cm_inst,
  356. clkdm->clkdm_offs);
  357. else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP)
  358. omap4_cminst_clkdm_force_sleep(clkdm->prcm_partition,
  359. clkdm->cm_inst,
  360. clkdm->clkdm_offs);
  361. else
  362. return -EINVAL;
  363. return 0;
  364. }
  365. static int omap4_clkdm_wakeup(struct clockdomain *clkdm)
  366. {
  367. omap4_cminst_clkdm_force_wakeup(clkdm->prcm_partition,
  368. clkdm->cm_inst, clkdm->clkdm_offs);
  369. return 0;
  370. }
  371. static void omap4_clkdm_allow_idle(struct clockdomain *clkdm)
  372. {
  373. omap4_cminst_clkdm_enable_hwsup(clkdm->prcm_partition,
  374. clkdm->cm_inst, clkdm->clkdm_offs);
  375. }
  376. static void omap4_clkdm_deny_idle(struct clockdomain *clkdm)
  377. {
  378. if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
  379. omap4_clkdm_wakeup(clkdm);
  380. else
  381. omap4_cminst_clkdm_disable_hwsup(clkdm->prcm_partition,
  382. clkdm->cm_inst,
  383. clkdm->clkdm_offs);
  384. }
  385. static int omap4_clkdm_clk_enable(struct clockdomain *clkdm)
  386. {
  387. if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
  388. return omap4_clkdm_wakeup(clkdm);
  389. return 0;
  390. }
  391. static int omap4_clkdm_clk_disable(struct clockdomain *clkdm)
  392. {
  393. bool hwsup = false;
  394. if (!clkdm->prcm_partition)
  395. return 0;
  396. /*
  397. * The CLKDM_MISSING_IDLE_REPORTING flag documentation has
  398. * more details on the unpleasant problem this is working
  399. * around
  400. */
  401. if (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING &&
  402. !(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
  403. omap4_clkdm_allow_idle(clkdm);
  404. return 0;
  405. }
  406. hwsup = omap4_cminst_is_clkdm_in_hwsup(clkdm->prcm_partition,
  407. clkdm->cm_inst, clkdm->clkdm_offs);
  408. if (!hwsup && (clkdm->flags & CLKDM_CAN_FORCE_SLEEP))
  409. omap4_clkdm_sleep(clkdm);
  410. return 0;
  411. }
  412. struct clkdm_ops omap4_clkdm_operations = {
  413. .clkdm_add_wkdep = omap4_clkdm_add_wkup_sleep_dep,
  414. .clkdm_del_wkdep = omap4_clkdm_del_wkup_sleep_dep,
  415. .clkdm_read_wkdep = omap4_clkdm_read_wkup_sleep_dep,
  416. .clkdm_clear_all_wkdeps = omap4_clkdm_clear_all_wkup_sleep_deps,
  417. .clkdm_add_sleepdep = omap4_clkdm_add_wkup_sleep_dep,
  418. .clkdm_del_sleepdep = omap4_clkdm_del_wkup_sleep_dep,
  419. .clkdm_read_sleepdep = omap4_clkdm_read_wkup_sleep_dep,
  420. .clkdm_clear_all_sleepdeps = omap4_clkdm_clear_all_wkup_sleep_deps,
  421. .clkdm_sleep = omap4_clkdm_sleep,
  422. .clkdm_wakeup = omap4_clkdm_wakeup,
  423. .clkdm_allow_idle = omap4_clkdm_allow_idle,
  424. .clkdm_deny_idle = omap4_clkdm_deny_idle,
  425. .clkdm_clk_enable = omap4_clkdm_clk_enable,
  426. .clkdm_clk_disable = omap4_clkdm_clk_disable,
  427. };
  428. struct clkdm_ops am43xx_clkdm_operations = {
  429. .clkdm_sleep = omap4_clkdm_sleep,
  430. .clkdm_wakeup = omap4_clkdm_wakeup,
  431. .clkdm_allow_idle = omap4_clkdm_allow_idle,
  432. .clkdm_deny_idle = omap4_clkdm_deny_idle,
  433. .clkdm_clk_enable = omap4_clkdm_clk_enable,
  434. .clkdm_clk_disable = omap4_clkdm_clk_disable,
  435. };
  436. static struct cm_ll_data omap4xxx_cm_ll_data = {
  437. .wait_module_ready = &omap4_cminst_wait_module_ready,
  438. .wait_module_idle = &omap4_cminst_wait_module_idle,
  439. .module_enable = &omap4_cminst_module_enable,
  440. .module_disable = &omap4_cminst_module_disable,
  441. };
  442. int __init omap4_cm_init(const struct omap_prcm_init_data *data)
  443. {
  444. omap_cm_base_init();
  445. return cm_register(&omap4xxx_cm_ll_data);
  446. }
  447. static void __exit omap4_cm_exit(void)
  448. {
  449. cm_unregister(&omap4xxx_cm_ll_data);
  450. }
  451. __exitcall(omap4_cm_exit);