omap_hwmod.c 39 KB

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