omap_hwmod.c 42 KB

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