omap_hwmod.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  1. /*
  2. * omap_hwmod implementation for OMAP2/3/4
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Paul Walmsley
  6. * With fixes and testing from Kevin Hilman
  7. *
  8. * Created in collaboration with (alphabetical order): Benoit Cousson,
  9. * Kevin Hilman, Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari
  10. * Poussa, Anand Sawant, Santosh Shilimkar, Richard Woodruff
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. * This code manages "OMAP modules" (on-chip devices) and their
  17. * integration with Linux device driver and bus code.
  18. *
  19. * References:
  20. * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
  21. * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
  22. * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
  23. * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
  24. * - Open Core Protocol Specification 2.2
  25. *
  26. * To do:
  27. * - pin mux handling
  28. * - handle IO mapping
  29. * - bus throughput & module latency measurement code
  30. *
  31. * XXX add tests at the beginning of each function to ensure the hwmod is
  32. * in the appropriate state
  33. * XXX error return values should be checked to ensure that they are
  34. * appropriate
  35. */
  36. #undef DEBUG
  37. #include <linux/kernel.h>
  38. #include <linux/errno.h>
  39. #include <linux/io.h>
  40. #include <linux/clk.h>
  41. #include <linux/delay.h>
  42. #include <linux/err.h>
  43. #include <linux/list.h>
  44. #include <linux/mutex.h>
  45. #include <linux/bootmem.h>
  46. #include <plat/common.h>
  47. #include <plat/cpu.h>
  48. #include <plat/clockdomain.h>
  49. #include <plat/powerdomain.h>
  50. #include <plat/clock.h>
  51. #include <plat/omap_hwmod.h>
  52. #include "cm.h"
  53. /* Maximum microseconds to wait for OMAP module to reset */
  54. #define MAX_MODULE_RESET_WAIT 10000
  55. /* Name of the OMAP hwmod for the MPU */
  56. #define MPU_INITIATOR_NAME "mpu_hwmod"
  57. /* omap_hwmod_list contains all registered struct omap_hwmods */
  58. static LIST_HEAD(omap_hwmod_list);
  59. static DEFINE_MUTEX(omap_hwmod_mutex);
  60. /* mpu_oh: used to add/remove MPU initiator from sleepdep list */
  61. static struct omap_hwmod *mpu_oh;
  62. /* inited: 0 if omap_hwmod_init() has not yet been called; 1 otherwise */
  63. static u8 inited;
  64. /* Private functions */
  65. /**
  66. * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
  67. * @oh: struct omap_hwmod *
  68. *
  69. * Load the current value of the hwmod OCP_SYSCONFIG register into the
  70. * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
  71. * OCP_SYSCONFIG register or 0 upon success.
  72. */
  73. static int _update_sysc_cache(struct omap_hwmod *oh)
  74. {
  75. if (!oh->sysconfig) {
  76. WARN(!oh->sysconfig, "omap_hwmod: %s: cannot read "
  77. "OCP_SYSCONFIG: not defined on hwmod\n", oh->name);
  78. return -EINVAL;
  79. }
  80. /* XXX ensure module interface clock is up */
  81. oh->_sysc_cache = omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
  82. oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
  83. return 0;
  84. }
  85. /**
  86. * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
  87. * @v: OCP_SYSCONFIG value to write
  88. * @oh: struct omap_hwmod *
  89. *
  90. * Write @v into the module OCP_SYSCONFIG register, if it has one. No
  91. * return value.
  92. */
  93. static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
  94. {
  95. if (!oh->sysconfig) {
  96. WARN(!oh->sysconfig, "omap_hwmod: %s: cannot write "
  97. "OCP_SYSCONFIG: not defined on hwmod\n", oh->name);
  98. return;
  99. }
  100. /* XXX ensure module interface clock is up */
  101. if (oh->_sysc_cache != v) {
  102. oh->_sysc_cache = v;
  103. omap_hwmod_writel(v, oh, oh->sysconfig->sysc_offs);
  104. }
  105. }
  106. /**
  107. * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
  108. * @oh: struct omap_hwmod *
  109. * @standbymode: MIDLEMODE field bits
  110. * @v: pointer to register contents to modify
  111. *
  112. * Update the master standby mode bits in @v to be @standbymode for
  113. * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
  114. * upon error or 0 upon success.
  115. */
  116. static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
  117. u32 *v)
  118. {
  119. if (!oh->sysconfig ||
  120. !(oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE))
  121. return -EINVAL;
  122. *v &= ~SYSC_MIDLEMODE_MASK;
  123. *v |= __ffs(standbymode) << SYSC_MIDLEMODE_SHIFT;
  124. return 0;
  125. }
  126. /**
  127. * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
  128. * @oh: struct omap_hwmod *
  129. * @idlemode: SIDLEMODE field bits
  130. * @v: pointer to register contents to modify
  131. *
  132. * Update the slave idle mode bits in @v to be @idlemode for the @oh
  133. * hwmod. Does not write to the hardware. Returns -EINVAL upon error
  134. * or 0 upon success.
  135. */
  136. static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
  137. {
  138. if (!oh->sysconfig ||
  139. !(oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE))
  140. return -EINVAL;
  141. *v &= ~SYSC_SIDLEMODE_MASK;
  142. *v |= __ffs(idlemode) << SYSC_SIDLEMODE_SHIFT;
  143. return 0;
  144. }
  145. /**
  146. * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
  147. * @oh: struct omap_hwmod *
  148. * @clockact: CLOCKACTIVITY field bits
  149. * @v: pointer to register contents to modify
  150. *
  151. * Update the clockactivity mode bits in @v to be @clockact for the
  152. * @oh hwmod. Used for additional powersaving on some modules. Does
  153. * not write to the hardware. Returns -EINVAL upon error or 0 upon
  154. * success.
  155. */
  156. static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
  157. {
  158. if (!oh->sysconfig ||
  159. !(oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
  160. return -EINVAL;
  161. *v &= ~SYSC_CLOCKACTIVITY_MASK;
  162. *v |= clockact << SYSC_CLOCKACTIVITY_SHIFT;
  163. return 0;
  164. }
  165. /**
  166. * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
  167. * @oh: struct omap_hwmod *
  168. * @v: pointer to register contents to modify
  169. *
  170. * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
  171. * error or 0 upon success.
  172. */
  173. static int _set_softreset(struct omap_hwmod *oh, u32 *v)
  174. {
  175. if (!oh->sysconfig ||
  176. !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET))
  177. return -EINVAL;
  178. *v |= SYSC_SOFTRESET_MASK;
  179. return 0;
  180. }
  181. /**
  182. * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
  183. * @oh: struct omap_hwmod *
  184. * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
  185. * @v: pointer to register contents to modify
  186. *
  187. * Update the module autoidle bit in @v to be @autoidle for the @oh
  188. * hwmod. The autoidle bit controls whether the module can gate
  189. * internal clocks automatically when it isn't doing anything; the
  190. * exact function of this bit varies on a per-module basis. This
  191. * function does not write to the hardware. Returns -EINVAL upon
  192. * error or 0 upon success.
  193. */
  194. static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
  195. u32 *v)
  196. {
  197. if (!oh->sysconfig ||
  198. !(oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE))
  199. return -EINVAL;
  200. *v &= ~SYSC_AUTOIDLE_MASK;
  201. *v |= autoidle << SYSC_AUTOIDLE_SHIFT;
  202. return 0;
  203. }
  204. /**
  205. * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
  206. * @oh: struct omap_hwmod *
  207. *
  208. * Allow the hardware module @oh to send wakeups. Returns -EINVAL
  209. * upon error or 0 upon success.
  210. */
  211. static int _enable_wakeup(struct omap_hwmod *oh)
  212. {
  213. u32 v;
  214. if (!oh->sysconfig ||
  215. !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
  216. return -EINVAL;
  217. v = oh->_sysc_cache;
  218. v |= SYSC_ENAWAKEUP_MASK;
  219. _write_sysconfig(v, oh);
  220. /* XXX test pwrdm_get_wken for this hwmod's subsystem */
  221. oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
  222. return 0;
  223. }
  224. /**
  225. * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
  226. * @oh: struct omap_hwmod *
  227. *
  228. * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
  229. * upon error or 0 upon success.
  230. */
  231. static int _disable_wakeup(struct omap_hwmod *oh)
  232. {
  233. u32 v;
  234. if (!oh->sysconfig ||
  235. !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
  236. return -EINVAL;
  237. v = oh->_sysc_cache;
  238. v &= ~SYSC_ENAWAKEUP_MASK;
  239. _write_sysconfig(v, oh);
  240. /* XXX test pwrdm_get_wken for this hwmod's subsystem */
  241. oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
  242. return 0;
  243. }
  244. /**
  245. * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
  246. * @oh: struct omap_hwmod *
  247. *
  248. * Prevent the hardware module @oh from entering idle while the
  249. * hardare module initiator @init_oh is active. Useful when a module
  250. * will be accessed by a particular initiator (e.g., if a module will
  251. * be accessed by the IVA, there should be a sleepdep between the IVA
  252. * initiator and the module). Only applies to modules in smart-idle
  253. * mode. Returns -EINVAL upon error or passes along
  254. * pwrdm_add_sleepdep() value upon success.
  255. */
  256. static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
  257. {
  258. if (!oh->_clk)
  259. return -EINVAL;
  260. return pwrdm_add_sleepdep(oh->_clk->clkdm->pwrdm.ptr,
  261. init_oh->_clk->clkdm->pwrdm.ptr);
  262. }
  263. /**
  264. * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
  265. * @oh: struct omap_hwmod *
  266. *
  267. * Allow the hardware module @oh to enter idle while the hardare
  268. * module initiator @init_oh is active. Useful when a module will not
  269. * be accessed by a particular initiator (e.g., if a module will not
  270. * be accessed by the IVA, there should be no sleepdep between the IVA
  271. * initiator and the module). Only applies to modules in smart-idle
  272. * mode. Returns -EINVAL upon error or passes along
  273. * pwrdm_add_sleepdep() value upon success.
  274. */
  275. static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
  276. {
  277. if (!oh->_clk)
  278. return -EINVAL;
  279. return pwrdm_del_sleepdep(oh->_clk->clkdm->pwrdm.ptr,
  280. init_oh->_clk->clkdm->pwrdm.ptr);
  281. }
  282. /**
  283. * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
  284. * @oh: struct omap_hwmod *
  285. *
  286. * Called from _init_clocks(). Populates the @oh _clk (main
  287. * functional clock pointer) if a main_clk is present. Returns 0 on
  288. * success or -EINVAL on error.
  289. */
  290. static int _init_main_clk(struct omap_hwmod *oh)
  291. {
  292. struct clk *c;
  293. int ret = 0;
  294. if (!oh->clkdev_con_id)
  295. return 0;
  296. c = clk_get_sys(oh->clkdev_dev_id, oh->clkdev_con_id);
  297. WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get main_clk %s.%s\n",
  298. oh->name, oh->clkdev_dev_id, oh->clkdev_con_id);
  299. if (IS_ERR(c))
  300. ret = -EINVAL;
  301. oh->_clk = c;
  302. return ret;
  303. }
  304. /**
  305. * _init_interface_clk - get a struct clk * for the the hwmod's interface clks
  306. * @oh: struct omap_hwmod *
  307. *
  308. * Called from _init_clocks(). Populates the @oh OCP slave interface
  309. * clock pointers. Returns 0 on success or -EINVAL on error.
  310. */
  311. static int _init_interface_clks(struct omap_hwmod *oh)
  312. {
  313. struct omap_hwmod_ocp_if *os;
  314. struct clk *c;
  315. int i;
  316. int ret = 0;
  317. if (oh->slaves_cnt == 0)
  318. return 0;
  319. for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
  320. if (!os->clkdev_con_id)
  321. continue;
  322. c = clk_get_sys(os->clkdev_dev_id, os->clkdev_con_id);
  323. WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get "
  324. "interface_clk %s.%s\n", oh->name,
  325. os->clkdev_dev_id, os->clkdev_con_id);
  326. if (IS_ERR(c))
  327. ret = -EINVAL;
  328. os->_clk = c;
  329. }
  330. return ret;
  331. }
  332. /**
  333. * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
  334. * @oh: struct omap_hwmod *
  335. *
  336. * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
  337. * clock pointers. Returns 0 on success or -EINVAL on error.
  338. */
  339. static int _init_opt_clks(struct omap_hwmod *oh)
  340. {
  341. struct omap_hwmod_opt_clk *oc;
  342. struct clk *c;
  343. int i;
  344. int ret = 0;
  345. for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
  346. c = clk_get_sys(oc->clkdev_dev_id, oc->clkdev_con_id);
  347. WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get opt_clk "
  348. "%s.%s\n", oh->name, oc->clkdev_dev_id,
  349. oc->clkdev_con_id);
  350. if (IS_ERR(c))
  351. ret = -EINVAL;
  352. oc->_clk = c;
  353. }
  354. return ret;
  355. }
  356. /**
  357. * _enable_clocks - enable hwmod main clock and interface clocks
  358. * @oh: struct omap_hwmod *
  359. *
  360. * Enables all clocks necessary for register reads and writes to succeed
  361. * on the hwmod @oh. Returns 0.
  362. */
  363. static int _enable_clocks(struct omap_hwmod *oh)
  364. {
  365. struct omap_hwmod_ocp_if *os;
  366. int i;
  367. pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
  368. if (oh->_clk && !IS_ERR(oh->_clk))
  369. clk_enable(oh->_clk);
  370. if (oh->slaves_cnt > 0) {
  371. for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
  372. struct clk *c = os->_clk;
  373. if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
  374. clk_enable(c);
  375. }
  376. }
  377. /* The opt clocks are controlled by the device driver. */
  378. return 0;
  379. }
  380. /**
  381. * _disable_clocks - disable hwmod main clock and interface clocks
  382. * @oh: struct omap_hwmod *
  383. *
  384. * Disables the hwmod @oh main functional and interface clocks. Returns 0.
  385. */
  386. static int _disable_clocks(struct omap_hwmod *oh)
  387. {
  388. struct omap_hwmod_ocp_if *os;
  389. int i;
  390. pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
  391. if (oh->_clk && !IS_ERR(oh->_clk))
  392. clk_disable(oh->_clk);
  393. if (oh->slaves_cnt > 0) {
  394. for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
  395. struct clk *c = os->_clk;
  396. if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
  397. clk_disable(c);
  398. }
  399. }
  400. /* The opt clocks are controlled by the device driver. */
  401. return 0;
  402. }
  403. /**
  404. * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
  405. * @oh: struct omap_hwmod *
  406. *
  407. * Returns the array index of the OCP slave port that the MPU
  408. * addresses the device on, or -EINVAL upon error or not found.
  409. */
  410. static int _find_mpu_port_index(struct omap_hwmod *oh)
  411. {
  412. struct omap_hwmod_ocp_if *os;
  413. int i;
  414. int found = 0;
  415. if (!oh || oh->slaves_cnt == 0)
  416. return -EINVAL;
  417. for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
  418. if (os->user & OCP_USER_MPU) {
  419. found = 1;
  420. break;
  421. }
  422. }
  423. if (found)
  424. pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
  425. oh->name, i);
  426. else
  427. pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
  428. oh->name);
  429. return (found) ? i : -EINVAL;
  430. }
  431. /**
  432. * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
  433. * @oh: struct omap_hwmod *
  434. *
  435. * Return the virtual address of the base of the register target of
  436. * device @oh, or NULL on error.
  437. */
  438. static void __iomem *_find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
  439. {
  440. struct omap_hwmod_ocp_if *os;
  441. struct omap_hwmod_addr_space *mem;
  442. int i;
  443. int found = 0;
  444. void __iomem *va_start;
  445. if (!oh || oh->slaves_cnt == 0)
  446. return NULL;
  447. os = *oh->slaves + index;
  448. for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
  449. if (mem->flags & ADDR_TYPE_RT) {
  450. found = 1;
  451. break;
  452. }
  453. }
  454. if (found) {
  455. va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
  456. if (!va_start) {
  457. pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
  458. return NULL;
  459. }
  460. pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
  461. oh->name, va_start);
  462. } else {
  463. pr_debug("omap_hwmod: %s: no MPU register target found\n",
  464. oh->name);
  465. }
  466. return (found) ? va_start : NULL;
  467. }
  468. /**
  469. * _sysc_enable - try to bring a module out of idle via OCP_SYSCONFIG
  470. * @oh: struct omap_hwmod *
  471. *
  472. * If module is marked as SWSUP_SIDLE, force the module out of slave
  473. * idle; otherwise, configure it for smart-idle. If module is marked
  474. * as SWSUP_MSUSPEND, force the module out of master standby;
  475. * otherwise, configure it for smart-standby. No return value.
  476. */
  477. static void _sysc_enable(struct omap_hwmod *oh)
  478. {
  479. u8 idlemode;
  480. u32 v;
  481. if (!oh->sysconfig)
  482. return;
  483. v = oh->_sysc_cache;
  484. if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
  485. idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
  486. HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
  487. _set_slave_idlemode(oh, idlemode, &v);
  488. }
  489. if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
  490. idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
  491. HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
  492. _set_master_standbymode(oh, idlemode, &v);
  493. }
  494. if (oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE) {
  495. idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
  496. 0 : 1;
  497. _set_module_autoidle(oh, idlemode, &v);
  498. }
  499. /* XXX OCP ENAWAKEUP bit? */
  500. if (oh->flags & HWMOD_SET_DEFAULT_CLOCKACT &&
  501. oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY)
  502. _set_clockactivity(oh, oh->sysconfig->clockact, &v);
  503. _write_sysconfig(v, oh);
  504. }
  505. /**
  506. * _sysc_idle - try to put a module into idle via OCP_SYSCONFIG
  507. * @oh: struct omap_hwmod *
  508. *
  509. * If module is marked as SWSUP_SIDLE, force the module into slave
  510. * idle; otherwise, configure it for smart-idle. If module is marked
  511. * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
  512. * configure it for smart-standby. No return value.
  513. */
  514. static void _sysc_idle(struct omap_hwmod *oh)
  515. {
  516. u8 idlemode;
  517. u32 v;
  518. if (!oh->sysconfig)
  519. return;
  520. v = oh->_sysc_cache;
  521. if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
  522. idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
  523. HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
  524. _set_slave_idlemode(oh, idlemode, &v);
  525. }
  526. if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
  527. idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
  528. HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
  529. _set_master_standbymode(oh, idlemode, &v);
  530. }
  531. _write_sysconfig(v, oh);
  532. }
  533. /**
  534. * _sysc_shutdown - force a module into idle via OCP_SYSCONFIG
  535. * @oh: struct omap_hwmod *
  536. *
  537. * Force the module into slave idle and master suspend. No return
  538. * value.
  539. */
  540. static void _sysc_shutdown(struct omap_hwmod *oh)
  541. {
  542. u32 v;
  543. if (!oh->sysconfig)
  544. return;
  545. v = oh->_sysc_cache;
  546. if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE)
  547. _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
  548. if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE)
  549. _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
  550. if (oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE)
  551. _set_module_autoidle(oh, 1, &v);
  552. _write_sysconfig(v, oh);
  553. }
  554. /**
  555. * _lookup - find an omap_hwmod by name
  556. * @name: find an omap_hwmod by name
  557. *
  558. * Return a pointer to an omap_hwmod by name, or NULL if not found.
  559. * Caller must hold omap_hwmod_mutex.
  560. */
  561. static struct omap_hwmod *_lookup(const char *name)
  562. {
  563. struct omap_hwmod *oh, *temp_oh;
  564. oh = NULL;
  565. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  566. if (!strcmp(name, temp_oh->name)) {
  567. oh = temp_oh;
  568. break;
  569. }
  570. }
  571. return oh;
  572. }
  573. /**
  574. * _init_clocks - clk_get() all clocks associated with this hwmod
  575. * @oh: struct omap_hwmod *
  576. *
  577. * Called by omap_hwmod_late_init() (after omap2_clk_init()).
  578. * Resolves all clock names embedded in the hwmod. Must be called
  579. * with omap_hwmod_mutex held. Returns -EINVAL if the omap_hwmod
  580. * has not yet been registered or if the clocks have already been
  581. * initialized, 0 on success, or a non-zero error on failure.
  582. */
  583. static int _init_clocks(struct omap_hwmod *oh)
  584. {
  585. int ret = 0;
  586. if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
  587. return -EINVAL;
  588. pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
  589. ret |= _init_main_clk(oh);
  590. ret |= _init_interface_clks(oh);
  591. ret |= _init_opt_clks(oh);
  592. oh->_state = _HWMOD_STATE_CLKS_INITED;
  593. return ret;
  594. }
  595. /**
  596. * _wait_target_ready - wait for a module to leave slave idle
  597. * @oh: struct omap_hwmod *
  598. *
  599. * Wait for a module @oh to leave slave idle. Returns 0 if the module
  600. * does not have an IDLEST bit or if the module successfully leaves
  601. * slave idle; otherwise, pass along the return value of the
  602. * appropriate *_cm_wait_module_ready() function.
  603. */
  604. static int _wait_target_ready(struct omap_hwmod *oh)
  605. {
  606. struct omap_hwmod_ocp_if *os;
  607. int ret;
  608. if (!oh)
  609. return -EINVAL;
  610. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  611. return 0;
  612. os = *oh->slaves + oh->_mpu_port_index;
  613. if (!(os->flags & OCPIF_HAS_IDLEST))
  614. return 0;
  615. /* XXX check module SIDLEMODE */
  616. /* XXX check clock enable states */
  617. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  618. ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
  619. oh->prcm.omap2.idlest_reg_id,
  620. oh->prcm.omap2.idlest_idle_bit);
  621. #if 0
  622. } else if (cpu_is_omap44xx()) {
  623. ret = omap4_cm_wait_module_ready(oh->prcm.omap4.module_offs,
  624. oh->prcm.omap4.device_offs);
  625. #endif
  626. } else {
  627. BUG();
  628. };
  629. return ret;
  630. }
  631. /**
  632. * _reset - reset an omap_hwmod
  633. * @oh: struct omap_hwmod *
  634. *
  635. * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
  636. * enabled for this to work. Must be called with omap_hwmod_mutex
  637. * held. Returns -EINVAL if the hwmod cannot be reset this way or if
  638. * the hwmod is in the wrong state, -ETIMEDOUT if the module did not
  639. * reset in time, or 0 upon success.
  640. */
  641. static int _reset(struct omap_hwmod *oh)
  642. {
  643. u32 r, v;
  644. int c = 0;
  645. if (!oh->sysconfig ||
  646. !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET) ||
  647. (oh->sysconfig->sysc_flags & SYSS_MISSING))
  648. return -EINVAL;
  649. /* clocks must be on for this operation */
  650. if (oh->_state != _HWMOD_STATE_ENABLED) {
  651. WARN(1, "omap_hwmod: %s: reset can only be entered from "
  652. "enabled state\n", oh->name);
  653. return -EINVAL;
  654. }
  655. pr_debug("omap_hwmod: %s: resetting\n", oh->name);
  656. v = oh->_sysc_cache;
  657. r = _set_softreset(oh, &v);
  658. if (r)
  659. return r;
  660. _write_sysconfig(v, oh);
  661. omap_test_timeout((omap_hwmod_readl(oh, oh->sysconfig->syss_offs) &
  662. SYSS_RESETDONE_MASK),
  663. MAX_MODULE_RESET_WAIT, c);
  664. if (c == MAX_MODULE_RESET_WAIT)
  665. WARN(1, "omap_hwmod: %s: failed to reset in %d usec\n",
  666. oh->name, MAX_MODULE_RESET_WAIT);
  667. else
  668. pr_debug("omap_hwmod: %s: reset in %d usec\n", oh->name, c);
  669. /*
  670. * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
  671. * _wait_target_ready() or _reset()
  672. */
  673. return (c == MAX_MODULE_RESET_WAIT) ? -ETIMEDOUT : 0;
  674. }
  675. /**
  676. * _enable - enable an omap_hwmod
  677. * @oh: struct omap_hwmod *
  678. *
  679. * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
  680. * register target. Must be called with omap_hwmod_mutex held.
  681. * Returns -EINVAL if the hwmod is in the wrong state or passes along
  682. * the return value of _wait_target_ready().
  683. */
  684. static int _enable(struct omap_hwmod *oh)
  685. {
  686. int r;
  687. if (oh->_state != _HWMOD_STATE_INITIALIZED &&
  688. oh->_state != _HWMOD_STATE_IDLE &&
  689. oh->_state != _HWMOD_STATE_DISABLED) {
  690. WARN(1, "omap_hwmod: %s: enabled state can only be entered "
  691. "from initialized, idle, or disabled state\n", oh->name);
  692. return -EINVAL;
  693. }
  694. pr_debug("omap_hwmod: %s: enabling\n", oh->name);
  695. /* XXX mux balls */
  696. _add_initiator_dep(oh, mpu_oh);
  697. _enable_clocks(oh);
  698. if (oh->sysconfig) {
  699. if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
  700. _update_sysc_cache(oh);
  701. _sysc_enable(oh);
  702. }
  703. r = _wait_target_ready(oh);
  704. if (!r)
  705. oh->_state = _HWMOD_STATE_ENABLED;
  706. return r;
  707. }
  708. /**
  709. * _idle - idle an omap_hwmod
  710. * @oh: struct omap_hwmod *
  711. *
  712. * Idles an omap_hwmod @oh. This should be called once the hwmod has
  713. * no further work. Returns -EINVAL if the hwmod is in the wrong
  714. * state or returns 0.
  715. */
  716. static int _idle(struct omap_hwmod *oh)
  717. {
  718. if (oh->_state != _HWMOD_STATE_ENABLED) {
  719. WARN(1, "omap_hwmod: %s: idle state can only be entered from "
  720. "enabled state\n", oh->name);
  721. return -EINVAL;
  722. }
  723. pr_debug("omap_hwmod: %s: idling\n", oh->name);
  724. if (oh->sysconfig)
  725. _sysc_idle(oh);
  726. _del_initiator_dep(oh, mpu_oh);
  727. _disable_clocks(oh);
  728. oh->_state = _HWMOD_STATE_IDLE;
  729. return 0;
  730. }
  731. /**
  732. * _shutdown - shutdown an omap_hwmod
  733. * @oh: struct omap_hwmod *
  734. *
  735. * Shut down an omap_hwmod @oh. This should be called when the driver
  736. * used for the hwmod is removed or unloaded or if the driver is not
  737. * used by the system. Returns -EINVAL if the hwmod is in the wrong
  738. * state or returns 0.
  739. */
  740. static int _shutdown(struct omap_hwmod *oh)
  741. {
  742. if (oh->_state != _HWMOD_STATE_IDLE &&
  743. oh->_state != _HWMOD_STATE_ENABLED) {
  744. WARN(1, "omap_hwmod: %s: disabled state can only be entered "
  745. "from idle, or enabled state\n", oh->name);
  746. return -EINVAL;
  747. }
  748. pr_debug("omap_hwmod: %s: disabling\n", oh->name);
  749. if (oh->sysconfig)
  750. _sysc_shutdown(oh);
  751. _del_initiator_dep(oh, mpu_oh);
  752. /* XXX what about the other system initiators here? DMA, tesla, d2d */
  753. _disable_clocks(oh);
  754. /* XXX Should this code also force-disable the optional clocks? */
  755. /* XXX mux any associated balls to safe mode */
  756. oh->_state = _HWMOD_STATE_DISABLED;
  757. return 0;
  758. }
  759. /**
  760. * _write_clockact_lock - set the module's clockactivity bits
  761. * @oh: struct omap_hwmod *
  762. * @clockact: CLOCKACTIVITY field bits
  763. *
  764. * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
  765. * OCP_SYSCONFIG register. Returns -EINVAL if the hwmod is in the
  766. * wrong state or returns 0.
  767. */
  768. static int _write_clockact_lock(struct omap_hwmod *oh, u8 clockact)
  769. {
  770. u32 v;
  771. if (!oh->sysconfig ||
  772. !(oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
  773. return -EINVAL;
  774. mutex_lock(&omap_hwmod_mutex);
  775. v = oh->_sysc_cache;
  776. _set_clockactivity(oh, clockact, &v);
  777. _write_sysconfig(v, oh);
  778. mutex_unlock(&omap_hwmod_mutex);
  779. return 0;
  780. }
  781. /**
  782. * _setup - do initial configuration of omap_hwmod
  783. * @oh: struct omap_hwmod *
  784. *
  785. * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
  786. * OCP_SYSCONFIG register. Must be called with omap_hwmod_mutex
  787. * held. Returns -EINVAL if the hwmod is in the wrong state or returns
  788. * 0.
  789. */
  790. static int _setup(struct omap_hwmod *oh)
  791. {
  792. struct omap_hwmod_ocp_if *os;
  793. int i;
  794. if (!oh)
  795. return -EINVAL;
  796. /* Set iclk autoidle mode */
  797. if (oh->slaves_cnt > 0) {
  798. for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
  799. struct clk *c = os->_clk;
  800. if (!c || IS_ERR(c))
  801. continue;
  802. if (os->flags & OCPIF_SWSUP_IDLE) {
  803. /* XXX omap_iclk_deny_idle(c); */
  804. } else {
  805. /* XXX omap_iclk_allow_idle(c); */
  806. clk_enable(c);
  807. }
  808. }
  809. }
  810. oh->_state = _HWMOD_STATE_INITIALIZED;
  811. _enable(oh);
  812. if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
  813. /*
  814. * XXX Do the OCP_SYSCONFIG bits need to be
  815. * reprogrammed after a reset? If not, then this can
  816. * be removed. If they do, then probably the
  817. * _enable() function should be split to avoid the
  818. * rewrite of the OCP_SYSCONFIG register.
  819. */
  820. if (oh->sysconfig) {
  821. _update_sysc_cache(oh);
  822. _sysc_enable(oh);
  823. }
  824. }
  825. if (!(oh->flags & HWMOD_INIT_NO_IDLE))
  826. _idle(oh);
  827. return 0;
  828. }
  829. /* Public functions */
  830. u32 omap_hwmod_readl(struct omap_hwmod *oh, u16 reg_offs)
  831. {
  832. return __raw_readl(oh->_rt_va + reg_offs);
  833. }
  834. void omap_hwmod_writel(u32 v, struct omap_hwmod *oh, u16 reg_offs)
  835. {
  836. __raw_writel(v, oh->_rt_va + reg_offs);
  837. }
  838. /**
  839. * omap_hwmod_register - register a struct omap_hwmod
  840. * @oh: struct omap_hwmod *
  841. *
  842. * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod already
  843. * has been registered by the same name; -EINVAL if the omap_hwmod is in the
  844. * wrong state, or 0 on success.
  845. *
  846. * XXX The data should be copied into bootmem, so the original data
  847. * should be marked __initdata and freed after init. This would allow
  848. * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
  849. * that the copy process would be relatively complex due to the large number
  850. * of substructures.
  851. */
  852. int omap_hwmod_register(struct omap_hwmod *oh)
  853. {
  854. int ret, ms_id;
  855. if (!oh || (oh->_state != _HWMOD_STATE_UNKNOWN))
  856. return -EINVAL;
  857. mutex_lock(&omap_hwmod_mutex);
  858. pr_debug("omap_hwmod: %s: registering\n", oh->name);
  859. if (_lookup(oh->name)) {
  860. ret = -EEXIST;
  861. goto ohr_unlock;
  862. }
  863. ms_id = _find_mpu_port_index(oh);
  864. if (!IS_ERR_VALUE(ms_id)) {
  865. oh->_mpu_port_index = ms_id;
  866. oh->_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
  867. } else {
  868. oh->_int_flags |= _HWMOD_NO_MPU_PORT;
  869. }
  870. list_add_tail(&oh->node, &omap_hwmod_list);
  871. oh->_state = _HWMOD_STATE_REGISTERED;
  872. ret = 0;
  873. ohr_unlock:
  874. mutex_unlock(&omap_hwmod_mutex);
  875. return ret;
  876. }
  877. /**
  878. * omap_hwmod_lookup - look up a registered omap_hwmod by name
  879. * @name: name of the omap_hwmod to look up
  880. *
  881. * Given a @name of an omap_hwmod, return a pointer to the registered
  882. * struct omap_hwmod *, or NULL upon error.
  883. */
  884. struct omap_hwmod *omap_hwmod_lookup(const char *name)
  885. {
  886. struct omap_hwmod *oh;
  887. if (!name)
  888. return NULL;
  889. mutex_lock(&omap_hwmod_mutex);
  890. oh = _lookup(name);
  891. mutex_unlock(&omap_hwmod_mutex);
  892. return oh;
  893. }
  894. /**
  895. * omap_hwmod_for_each - call function for each registered omap_hwmod
  896. * @fn: pointer to a callback function
  897. *
  898. * Call @fn for each registered omap_hwmod, passing @data to each
  899. * function. @fn must return 0 for success or any other value for
  900. * failure. If @fn returns non-zero, the iteration across omap_hwmods
  901. * will stop and the non-zero return value will be passed to the
  902. * caller of omap_hwmod_for_each(). @fn is called with
  903. * omap_hwmod_for_each() held.
  904. */
  905. int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh))
  906. {
  907. struct omap_hwmod *temp_oh;
  908. int ret;
  909. if (!fn)
  910. return -EINVAL;
  911. mutex_lock(&omap_hwmod_mutex);
  912. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  913. ret = (*fn)(temp_oh);
  914. if (ret)
  915. break;
  916. }
  917. mutex_unlock(&omap_hwmod_mutex);
  918. return ret;
  919. }
  920. /**
  921. * omap_hwmod_init - init omap_hwmod code and register hwmods
  922. * @ohs: pointer to an array of omap_hwmods to register
  923. *
  924. * Intended to be called early in boot before the clock framework is
  925. * initialized. If @ohs is not null, will register all omap_hwmods
  926. * listed in @ohs that are valid for this chip. Returns -EINVAL if
  927. * omap_hwmod_init() has already been called or 0 otherwise.
  928. */
  929. int omap_hwmod_init(struct omap_hwmod **ohs)
  930. {
  931. struct omap_hwmod *oh;
  932. int r;
  933. if (inited)
  934. return -EINVAL;
  935. inited = 1;
  936. if (!ohs)
  937. return 0;
  938. oh = *ohs;
  939. while (oh) {
  940. if (omap_chip_is(oh->omap_chip)) {
  941. r = omap_hwmod_register(oh);
  942. WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
  943. "%d\n", oh->name, r);
  944. }
  945. oh = *++ohs;
  946. }
  947. return 0;
  948. }
  949. /**
  950. * omap_hwmod_late_init - do some post-clock framework initialization
  951. *
  952. * Must be called after omap2_clk_init(). Resolves the struct clk names
  953. * to struct clk pointers for each registered omap_hwmod. Also calls
  954. * _setup() on each hwmod. Returns 0.
  955. */
  956. int omap_hwmod_late_init(void)
  957. {
  958. int r;
  959. /* XXX check return value */
  960. r = omap_hwmod_for_each(_init_clocks);
  961. WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
  962. mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
  963. WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
  964. MPU_INITIATOR_NAME);
  965. omap_hwmod_for_each(_setup);
  966. return 0;
  967. }
  968. /**
  969. * omap_hwmod_unregister - unregister an omap_hwmod
  970. * @oh: struct omap_hwmod *
  971. *
  972. * Unregisters a previously-registered omap_hwmod @oh. There's probably
  973. * no use case for this, so it is likely to be removed in a later version.
  974. *
  975. * XXX Free all of the bootmem-allocated structures here when that is
  976. * implemented. Make it clear that core code is the only code that is
  977. * expected to unregister modules.
  978. */
  979. int omap_hwmod_unregister(struct omap_hwmod *oh)
  980. {
  981. if (!oh)
  982. return -EINVAL;
  983. pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
  984. mutex_lock(&omap_hwmod_mutex);
  985. iounmap(oh->_rt_va);
  986. list_del(&oh->node);
  987. mutex_unlock(&omap_hwmod_mutex);
  988. return 0;
  989. }
  990. /**
  991. * omap_hwmod_enable - enable an omap_hwmod
  992. * @oh: struct omap_hwmod *
  993. *
  994. * Enable an omap_hwomd @oh. Intended to be called by omap_device_enable().
  995. * Returns -EINVAL on error or passes along the return value from _enable().
  996. */
  997. int omap_hwmod_enable(struct omap_hwmod *oh)
  998. {
  999. int r;
  1000. if (!oh)
  1001. return -EINVAL;
  1002. mutex_lock(&omap_hwmod_mutex);
  1003. r = _enable(oh);
  1004. mutex_unlock(&omap_hwmod_mutex);
  1005. return r;
  1006. }
  1007. /**
  1008. * omap_hwmod_idle - idle an omap_hwmod
  1009. * @oh: struct omap_hwmod *
  1010. *
  1011. * Idle an omap_hwomd @oh. Intended to be called by omap_device_idle().
  1012. * Returns -EINVAL on error or passes along the return value from _idle().
  1013. */
  1014. int omap_hwmod_idle(struct omap_hwmod *oh)
  1015. {
  1016. if (!oh)
  1017. return -EINVAL;
  1018. mutex_lock(&omap_hwmod_mutex);
  1019. _idle(oh);
  1020. mutex_unlock(&omap_hwmod_mutex);
  1021. return 0;
  1022. }
  1023. /**
  1024. * omap_hwmod_shutdown - shutdown an omap_hwmod
  1025. * @oh: struct omap_hwmod *
  1026. *
  1027. * Shutdown an omap_hwomd @oh. Intended to be called by
  1028. * omap_device_shutdown(). Returns -EINVAL on error or passes along
  1029. * the return value from _shutdown().
  1030. */
  1031. int omap_hwmod_shutdown(struct omap_hwmod *oh)
  1032. {
  1033. if (!oh)
  1034. return -EINVAL;
  1035. mutex_lock(&omap_hwmod_mutex);
  1036. _shutdown(oh);
  1037. mutex_unlock(&omap_hwmod_mutex);
  1038. return 0;
  1039. }
  1040. /**
  1041. * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
  1042. * @oh: struct omap_hwmod *oh
  1043. *
  1044. * Intended to be called by the omap_device code.
  1045. */
  1046. int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
  1047. {
  1048. mutex_lock(&omap_hwmod_mutex);
  1049. _enable_clocks(oh);
  1050. mutex_unlock(&omap_hwmod_mutex);
  1051. return 0;
  1052. }
  1053. /**
  1054. * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
  1055. * @oh: struct omap_hwmod *oh
  1056. *
  1057. * Intended to be called by the omap_device code.
  1058. */
  1059. int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
  1060. {
  1061. mutex_lock(&omap_hwmod_mutex);
  1062. _disable_clocks(oh);
  1063. mutex_unlock(&omap_hwmod_mutex);
  1064. return 0;
  1065. }
  1066. /**
  1067. * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
  1068. * @oh: struct omap_hwmod *oh
  1069. *
  1070. * Intended to be called by drivers and core code when all posted
  1071. * writes to a device must complete before continuing further
  1072. * execution (for example, after clearing some device IRQSTATUS
  1073. * register bits)
  1074. *
  1075. * XXX what about targets with multiple OCP threads?
  1076. */
  1077. void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
  1078. {
  1079. BUG_ON(!oh);
  1080. if (!oh->sysconfig || !oh->sysconfig->sysc_flags) {
  1081. WARN(1, "omap_device: %s: OCP barrier impossible due to "
  1082. "device configuration\n", oh->name);
  1083. return;
  1084. }
  1085. /*
  1086. * Forces posted writes to complete on the OCP thread handling
  1087. * register writes
  1088. */
  1089. omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
  1090. }
  1091. /**
  1092. * omap_hwmod_reset - reset the hwmod
  1093. * @oh: struct omap_hwmod *
  1094. *
  1095. * Under some conditions, a driver may wish to reset the entire device.
  1096. * Called from omap_device code. Returns -EINVAL on error or passes along
  1097. * the return value from _reset()/_enable().
  1098. */
  1099. int omap_hwmod_reset(struct omap_hwmod *oh)
  1100. {
  1101. int r;
  1102. if (!oh || !(oh->_state & _HWMOD_STATE_ENABLED))
  1103. return -EINVAL;
  1104. mutex_lock(&omap_hwmod_mutex);
  1105. r = _reset(oh);
  1106. if (!r)
  1107. r = _enable(oh);
  1108. mutex_unlock(&omap_hwmod_mutex);
  1109. return r;
  1110. }
  1111. /**
  1112. * omap_hwmod_count_resources - count number of struct resources needed by hwmod
  1113. * @oh: struct omap_hwmod *
  1114. * @res: pointer to the first element of an array of struct resource to fill
  1115. *
  1116. * Count the number of struct resource array elements necessary to
  1117. * contain omap_hwmod @oh resources. Intended to be called by code
  1118. * that registers omap_devices. Intended to be used to determine the
  1119. * size of a dynamically-allocated struct resource array, before
  1120. * calling omap_hwmod_fill_resources(). Returns the number of struct
  1121. * resource array elements needed.
  1122. *
  1123. * XXX This code is not optimized. It could attempt to merge adjacent
  1124. * resource IDs.
  1125. *
  1126. */
  1127. int omap_hwmod_count_resources(struct omap_hwmod *oh)
  1128. {
  1129. int ret, i;
  1130. ret = oh->mpu_irqs_cnt + oh->sdma_chs_cnt;
  1131. for (i = 0; i < oh->slaves_cnt; i++)
  1132. ret += (*oh->slaves + i)->addr_cnt;
  1133. return ret;
  1134. }
  1135. /**
  1136. * omap_hwmod_fill_resources - fill struct resource array with hwmod data
  1137. * @oh: struct omap_hwmod *
  1138. * @res: pointer to the first element of an array of struct resource to fill
  1139. *
  1140. * Fill the struct resource array @res with resource data from the
  1141. * omap_hwmod @oh. Intended to be called by code that registers
  1142. * omap_devices. See also omap_hwmod_count_resources(). Returns the
  1143. * number of array elements filled.
  1144. */
  1145. int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
  1146. {
  1147. int i, j;
  1148. int r = 0;
  1149. /* For each IRQ, DMA, memory area, fill in array.*/
  1150. for (i = 0; i < oh->mpu_irqs_cnt; i++) {
  1151. (res + r)->start = *(oh->mpu_irqs + i);
  1152. (res + r)->end = *(oh->mpu_irqs + i);
  1153. (res + r)->flags = IORESOURCE_IRQ;
  1154. r++;
  1155. }
  1156. for (i = 0; i < oh->sdma_chs_cnt; i++) {
  1157. (res + r)->name = (oh->sdma_chs + i)->name;
  1158. (res + r)->start = (oh->sdma_chs + i)->dma_ch;
  1159. (res + r)->end = (oh->sdma_chs + i)->dma_ch;
  1160. (res + r)->flags = IORESOURCE_DMA;
  1161. r++;
  1162. }
  1163. for (i = 0; i < oh->slaves_cnt; i++) {
  1164. struct omap_hwmod_ocp_if *os;
  1165. os = *oh->slaves + i;
  1166. for (j = 0; j < os->addr_cnt; j++) {
  1167. (res + r)->start = (os->addr + j)->pa_start;
  1168. (res + r)->end = (os->addr + j)->pa_end;
  1169. (res + r)->flags = IORESOURCE_MEM;
  1170. r++;
  1171. }
  1172. }
  1173. return r;
  1174. }
  1175. /**
  1176. * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
  1177. * @oh: struct omap_hwmod *
  1178. *
  1179. * Return the powerdomain pointer associated with the OMAP module
  1180. * @oh's main clock. If @oh does not have a main clk, return the
  1181. * powerdomain associated with the interface clock associated with the
  1182. * module's MPU port. (XXX Perhaps this should use the SDMA port
  1183. * instead?) Returns NULL on error, or a struct powerdomain * on
  1184. * success.
  1185. */
  1186. struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
  1187. {
  1188. struct clk *c;
  1189. if (!oh)
  1190. return NULL;
  1191. if (oh->_clk) {
  1192. c = oh->_clk;
  1193. } else {
  1194. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  1195. return NULL;
  1196. c = oh->slaves[oh->_mpu_port_index]->_clk;
  1197. }
  1198. return c->clkdm->pwrdm.ptr;
  1199. }
  1200. /**
  1201. * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
  1202. * @oh: struct omap_hwmod *
  1203. * @init_oh: struct omap_hwmod * (initiator)
  1204. *
  1205. * Add a sleep dependency between the initiator @init_oh and @oh.
  1206. * Intended to be called by DSP/Bridge code via platform_data for the
  1207. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  1208. * code needs to add/del initiator dependencies dynamically
  1209. * before/after accessing a device. Returns the return value from
  1210. * _add_initiator_dep().
  1211. *
  1212. * XXX Keep a usecount in the clockdomain code
  1213. */
  1214. int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
  1215. struct omap_hwmod *init_oh)
  1216. {
  1217. return _add_initiator_dep(oh, init_oh);
  1218. }
  1219. /*
  1220. * XXX what about functions for drivers to save/restore ocp_sysconfig
  1221. * for context save/restore operations?
  1222. */
  1223. /**
  1224. * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
  1225. * @oh: struct omap_hwmod *
  1226. * @init_oh: struct omap_hwmod * (initiator)
  1227. *
  1228. * Remove a sleep dependency between the initiator @init_oh and @oh.
  1229. * Intended to be called by DSP/Bridge code via platform_data for the
  1230. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  1231. * code needs to add/del initiator dependencies dynamically
  1232. * before/after accessing a device. Returns the return value from
  1233. * _del_initiator_dep().
  1234. *
  1235. * XXX Keep a usecount in the clockdomain code
  1236. */
  1237. int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
  1238. struct omap_hwmod *init_oh)
  1239. {
  1240. return _del_initiator_dep(oh, init_oh);
  1241. }
  1242. /**
  1243. * omap_hwmod_set_clockact_none - set clockactivity test to BOTH
  1244. * @oh: struct omap_hwmod *
  1245. *
  1246. * On some modules, this function can affect the wakeup latency vs.
  1247. * power consumption balance. Intended to be called by the
  1248. * omap_device layer. Passes along the return value from
  1249. * _write_clockact_lock().
  1250. */
  1251. int omap_hwmod_set_clockact_both(struct omap_hwmod *oh)
  1252. {
  1253. return _write_clockact_lock(oh, CLOCKACT_TEST_BOTH);
  1254. }
  1255. /**
  1256. * omap_hwmod_set_clockact_none - set clockactivity test to MAIN
  1257. * @oh: struct omap_hwmod *
  1258. *
  1259. * On some modules, this function can affect the wakeup latency vs.
  1260. * power consumption balance. Intended to be called by the
  1261. * omap_device layer. Passes along the return value from
  1262. * _write_clockact_lock().
  1263. */
  1264. int omap_hwmod_set_clockact_main(struct omap_hwmod *oh)
  1265. {
  1266. return _write_clockact_lock(oh, CLOCKACT_TEST_MAIN);
  1267. }
  1268. /**
  1269. * omap_hwmod_set_clockact_none - set clockactivity test to ICLK
  1270. * @oh: struct omap_hwmod *
  1271. *
  1272. * On some modules, this function can affect the wakeup latency vs.
  1273. * power consumption balance. Intended to be called by the
  1274. * omap_device layer. Passes along the return value from
  1275. * _write_clockact_lock().
  1276. */
  1277. int omap_hwmod_set_clockact_iclk(struct omap_hwmod *oh)
  1278. {
  1279. return _write_clockact_lock(oh, CLOCKACT_TEST_ICLK);
  1280. }
  1281. /**
  1282. * omap_hwmod_set_clockact_none - set clockactivity test to NONE
  1283. * @oh: struct omap_hwmod *
  1284. *
  1285. * On some modules, this function can affect the wakeup latency vs.
  1286. * power consumption balance. Intended to be called by the
  1287. * omap_device layer. Passes along the return value from
  1288. * _write_clockact_lock().
  1289. */
  1290. int omap_hwmod_set_clockact_none(struct omap_hwmod *oh)
  1291. {
  1292. return _write_clockact_lock(oh, CLOCKACT_TEST_NONE);
  1293. }
  1294. /**
  1295. * omap_hwmod_enable_wakeup - allow device to wake up the system
  1296. * @oh: struct omap_hwmod *
  1297. *
  1298. * Sets the module OCP socket ENAWAKEUP bit to allow the module to
  1299. * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
  1300. * registers to cause the PRCM to receive wakeup events from the
  1301. * module. Does not set any wakeup routing registers beyond this
  1302. * point - if the module is to wake up any other module or subsystem,
  1303. * that must be set separately. Called by omap_device code. Returns
  1304. * -EINVAL on error or 0 upon success.
  1305. */
  1306. int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
  1307. {
  1308. if (!oh->sysconfig ||
  1309. !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
  1310. return -EINVAL;
  1311. mutex_lock(&omap_hwmod_mutex);
  1312. _enable_wakeup(oh);
  1313. mutex_unlock(&omap_hwmod_mutex);
  1314. return 0;
  1315. }
  1316. /**
  1317. * omap_hwmod_disable_wakeup - prevent device from waking the system
  1318. * @oh: struct omap_hwmod *
  1319. *
  1320. * Clears the module OCP socket ENAWAKEUP bit to prevent the module
  1321. * from sending wakeups to the PRCM. Eventually this should clear
  1322. * PRCM wakeup registers to cause the PRCM to ignore wakeup events
  1323. * from the module. Does not set any wakeup routing registers beyond
  1324. * this point - if the module is to wake up any other module or
  1325. * subsystem, that must be set separately. Called by omap_device
  1326. * code. Returns -EINVAL on error or 0 upon success.
  1327. */
  1328. int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
  1329. {
  1330. if (!oh->sysconfig ||
  1331. !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
  1332. return -EINVAL;
  1333. mutex_lock(&omap_hwmod_mutex);
  1334. _disable_wakeup(oh);
  1335. mutex_unlock(&omap_hwmod_mutex);
  1336. return 0;
  1337. }