cminst44xx.c 15 KB

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