db8500-prcmu.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014
  1. /*
  2. * Copyright (C) STMicroelectronics 2009
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * License Terms: GNU General Public License v2
  6. * Author: Kumar Sanghvi <kumar.sanghvi@stericsson.com>
  7. * Author: Sundar Iyer <sundar.iyer@stericsson.com>
  8. * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com>
  9. *
  10. * U8500 PRCM Unit interface driver
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/delay.h>
  16. #include <linux/errno.h>
  17. #include <linux/err.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/io.h>
  20. #include <linux/slab.h>
  21. #include <linux/mutex.h>
  22. #include <linux/completion.h>
  23. #include <linux/irq.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/bitops.h>
  26. #include <linux/fs.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/mfd/core.h>
  30. #include <linux/mfd/dbx500-prcmu.h>
  31. #include <linux/regulator/db8500-prcmu.h>
  32. #include <linux/regulator/machine.h>
  33. #include <asm/hardware/gic.h>
  34. #include <mach/hardware.h>
  35. #include <mach/irqs.h>
  36. #include <mach/db8500-regs.h>
  37. #include <mach/id.h>
  38. #include "dbx500-prcmu-regs.h"
  39. /* Offset for the firmware version within the TCPM */
  40. #define PRCMU_FW_VERSION_OFFSET 0xA4
  41. /* Index of different voltages to be used when accessing AVSData */
  42. #define PRCM_AVS_BASE 0x2FC
  43. #define PRCM_AVS_VBB_RET (PRCM_AVS_BASE + 0x0)
  44. #define PRCM_AVS_VBB_MAX_OPP (PRCM_AVS_BASE + 0x1)
  45. #define PRCM_AVS_VBB_100_OPP (PRCM_AVS_BASE + 0x2)
  46. #define PRCM_AVS_VBB_50_OPP (PRCM_AVS_BASE + 0x3)
  47. #define PRCM_AVS_VARM_MAX_OPP (PRCM_AVS_BASE + 0x4)
  48. #define PRCM_AVS_VARM_100_OPP (PRCM_AVS_BASE + 0x5)
  49. #define PRCM_AVS_VARM_50_OPP (PRCM_AVS_BASE + 0x6)
  50. #define PRCM_AVS_VARM_RET (PRCM_AVS_BASE + 0x7)
  51. #define PRCM_AVS_VAPE_100_OPP (PRCM_AVS_BASE + 0x8)
  52. #define PRCM_AVS_VAPE_50_OPP (PRCM_AVS_BASE + 0x9)
  53. #define PRCM_AVS_VMOD_100_OPP (PRCM_AVS_BASE + 0xA)
  54. #define PRCM_AVS_VMOD_50_OPP (PRCM_AVS_BASE + 0xB)
  55. #define PRCM_AVS_VSAFE (PRCM_AVS_BASE + 0xC)
  56. #define PRCM_AVS_VOLTAGE 0
  57. #define PRCM_AVS_VOLTAGE_MASK 0x3f
  58. #define PRCM_AVS_ISSLOWSTARTUP 6
  59. #define PRCM_AVS_ISSLOWSTARTUP_MASK (1 << PRCM_AVS_ISSLOWSTARTUP)
  60. #define PRCM_AVS_ISMODEENABLE 7
  61. #define PRCM_AVS_ISMODEENABLE_MASK (1 << PRCM_AVS_ISMODEENABLE)
  62. #define PRCM_BOOT_STATUS 0xFFF
  63. #define PRCM_ROMCODE_A2P 0xFFE
  64. #define PRCM_ROMCODE_P2A 0xFFD
  65. #define PRCM_XP70_CUR_PWR_STATE 0xFFC /* 4 BYTES */
  66. #define PRCM_SW_RST_REASON 0xFF8 /* 2 bytes */
  67. #define _PRCM_MBOX_HEADER 0xFE8 /* 16 bytes */
  68. #define PRCM_MBOX_HEADER_REQ_MB0 (_PRCM_MBOX_HEADER + 0x0)
  69. #define PRCM_MBOX_HEADER_REQ_MB1 (_PRCM_MBOX_HEADER + 0x1)
  70. #define PRCM_MBOX_HEADER_REQ_MB2 (_PRCM_MBOX_HEADER + 0x2)
  71. #define PRCM_MBOX_HEADER_REQ_MB3 (_PRCM_MBOX_HEADER + 0x3)
  72. #define PRCM_MBOX_HEADER_REQ_MB4 (_PRCM_MBOX_HEADER + 0x4)
  73. #define PRCM_MBOX_HEADER_REQ_MB5 (_PRCM_MBOX_HEADER + 0x5)
  74. #define PRCM_MBOX_HEADER_ACK_MB0 (_PRCM_MBOX_HEADER + 0x8)
  75. /* Req Mailboxes */
  76. #define PRCM_REQ_MB0 0xFDC /* 12 bytes */
  77. #define PRCM_REQ_MB1 0xFD0 /* 12 bytes */
  78. #define PRCM_REQ_MB2 0xFC0 /* 16 bytes */
  79. #define PRCM_REQ_MB3 0xE4C /* 372 bytes */
  80. #define PRCM_REQ_MB4 0xE48 /* 4 bytes */
  81. #define PRCM_REQ_MB5 0xE44 /* 4 bytes */
  82. /* Ack Mailboxes */
  83. #define PRCM_ACK_MB0 0xE08 /* 52 bytes */
  84. #define PRCM_ACK_MB1 0xE04 /* 4 bytes */
  85. #define PRCM_ACK_MB2 0xE00 /* 4 bytes */
  86. #define PRCM_ACK_MB3 0xDFC /* 4 bytes */
  87. #define PRCM_ACK_MB4 0xDF8 /* 4 bytes */
  88. #define PRCM_ACK_MB5 0xDF4 /* 4 bytes */
  89. /* Mailbox 0 headers */
  90. #define MB0H_POWER_STATE_TRANS 0
  91. #define MB0H_CONFIG_WAKEUPS_EXE 1
  92. #define MB0H_READ_WAKEUP_ACK 3
  93. #define MB0H_CONFIG_WAKEUPS_SLEEP 4
  94. #define MB0H_WAKEUP_EXE 2
  95. #define MB0H_WAKEUP_SLEEP 5
  96. /* Mailbox 0 REQs */
  97. #define PRCM_REQ_MB0_AP_POWER_STATE (PRCM_REQ_MB0 + 0x0)
  98. #define PRCM_REQ_MB0_AP_PLL_STATE (PRCM_REQ_MB0 + 0x1)
  99. #define PRCM_REQ_MB0_ULP_CLOCK_STATE (PRCM_REQ_MB0 + 0x2)
  100. #define PRCM_REQ_MB0_DO_NOT_WFI (PRCM_REQ_MB0 + 0x3)
  101. #define PRCM_REQ_MB0_WAKEUP_8500 (PRCM_REQ_MB0 + 0x4)
  102. #define PRCM_REQ_MB0_WAKEUP_4500 (PRCM_REQ_MB0 + 0x8)
  103. /* Mailbox 0 ACKs */
  104. #define PRCM_ACK_MB0_AP_PWRSTTR_STATUS (PRCM_ACK_MB0 + 0x0)
  105. #define PRCM_ACK_MB0_READ_POINTER (PRCM_ACK_MB0 + 0x1)
  106. #define PRCM_ACK_MB0_WAKEUP_0_8500 (PRCM_ACK_MB0 + 0x4)
  107. #define PRCM_ACK_MB0_WAKEUP_0_4500 (PRCM_ACK_MB0 + 0x8)
  108. #define PRCM_ACK_MB0_WAKEUP_1_8500 (PRCM_ACK_MB0 + 0x1C)
  109. #define PRCM_ACK_MB0_WAKEUP_1_4500 (PRCM_ACK_MB0 + 0x20)
  110. #define PRCM_ACK_MB0_EVENT_4500_NUMBERS 20
  111. /* Mailbox 1 headers */
  112. #define MB1H_ARM_APE_OPP 0x0
  113. #define MB1H_RESET_MODEM 0x2
  114. #define MB1H_REQUEST_APE_OPP_100_VOLT 0x3
  115. #define MB1H_RELEASE_APE_OPP_100_VOLT 0x4
  116. #define MB1H_RELEASE_USB_WAKEUP 0x5
  117. #define MB1H_PLL_ON_OFF 0x6
  118. /* Mailbox 1 Requests */
  119. #define PRCM_REQ_MB1_ARM_OPP (PRCM_REQ_MB1 + 0x0)
  120. #define PRCM_REQ_MB1_APE_OPP (PRCM_REQ_MB1 + 0x1)
  121. #define PRCM_REQ_MB1_PLL_ON_OFF (PRCM_REQ_MB1 + 0x4)
  122. #define PLL_SOC0_OFF 0x1
  123. #define PLL_SOC0_ON 0x2
  124. #define PLL_SOC1_OFF 0x4
  125. #define PLL_SOC1_ON 0x8
  126. /* Mailbox 1 ACKs */
  127. #define PRCM_ACK_MB1_CURRENT_ARM_OPP (PRCM_ACK_MB1 + 0x0)
  128. #define PRCM_ACK_MB1_CURRENT_APE_OPP (PRCM_ACK_MB1 + 0x1)
  129. #define PRCM_ACK_MB1_APE_VOLTAGE_STATUS (PRCM_ACK_MB1 + 0x2)
  130. #define PRCM_ACK_MB1_DVFS_STATUS (PRCM_ACK_MB1 + 0x3)
  131. /* Mailbox 2 headers */
  132. #define MB2H_DPS 0x0
  133. #define MB2H_AUTO_PWR 0x1
  134. /* Mailbox 2 REQs */
  135. #define PRCM_REQ_MB2_SVA_MMDSP (PRCM_REQ_MB2 + 0x0)
  136. #define PRCM_REQ_MB2_SVA_PIPE (PRCM_REQ_MB2 + 0x1)
  137. #define PRCM_REQ_MB2_SIA_MMDSP (PRCM_REQ_MB2 + 0x2)
  138. #define PRCM_REQ_MB2_SIA_PIPE (PRCM_REQ_MB2 + 0x3)
  139. #define PRCM_REQ_MB2_SGA (PRCM_REQ_MB2 + 0x4)
  140. #define PRCM_REQ_MB2_B2R2_MCDE (PRCM_REQ_MB2 + 0x5)
  141. #define PRCM_REQ_MB2_ESRAM12 (PRCM_REQ_MB2 + 0x6)
  142. #define PRCM_REQ_MB2_ESRAM34 (PRCM_REQ_MB2 + 0x7)
  143. #define PRCM_REQ_MB2_AUTO_PM_SLEEP (PRCM_REQ_MB2 + 0x8)
  144. #define PRCM_REQ_MB2_AUTO_PM_IDLE (PRCM_REQ_MB2 + 0xC)
  145. /* Mailbox 2 ACKs */
  146. #define PRCM_ACK_MB2_DPS_STATUS (PRCM_ACK_MB2 + 0x0)
  147. #define HWACC_PWR_ST_OK 0xFE
  148. /* Mailbox 3 headers */
  149. #define MB3H_ANC 0x0
  150. #define MB3H_SIDETONE 0x1
  151. #define MB3H_SYSCLK 0xE
  152. /* Mailbox 3 Requests */
  153. #define PRCM_REQ_MB3_ANC_FIR_COEFF (PRCM_REQ_MB3 + 0x0)
  154. #define PRCM_REQ_MB3_ANC_IIR_COEFF (PRCM_REQ_MB3 + 0x20)
  155. #define PRCM_REQ_MB3_ANC_SHIFTER (PRCM_REQ_MB3 + 0x60)
  156. #define PRCM_REQ_MB3_ANC_WARP (PRCM_REQ_MB3 + 0x64)
  157. #define PRCM_REQ_MB3_SIDETONE_FIR_GAIN (PRCM_REQ_MB3 + 0x68)
  158. #define PRCM_REQ_MB3_SIDETONE_FIR_COEFF (PRCM_REQ_MB3 + 0x6C)
  159. #define PRCM_REQ_MB3_SYSCLK_MGT (PRCM_REQ_MB3 + 0x16C)
  160. /* Mailbox 4 headers */
  161. #define MB4H_DDR_INIT 0x0
  162. #define MB4H_MEM_ST 0x1
  163. #define MB4H_HOTDOG 0x12
  164. #define MB4H_HOTMON 0x13
  165. #define MB4H_HOT_PERIOD 0x14
  166. #define MB4H_A9WDOG_CONF 0x16
  167. #define MB4H_A9WDOG_EN 0x17
  168. #define MB4H_A9WDOG_DIS 0x18
  169. #define MB4H_A9WDOG_LOAD 0x19
  170. #define MB4H_A9WDOG_KICK 0x20
  171. /* Mailbox 4 Requests */
  172. #define PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE (PRCM_REQ_MB4 + 0x0)
  173. #define PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE (PRCM_REQ_MB4 + 0x1)
  174. #define PRCM_REQ_MB4_ESRAM0_ST (PRCM_REQ_MB4 + 0x3)
  175. #define PRCM_REQ_MB4_HOTDOG_THRESHOLD (PRCM_REQ_MB4 + 0x0)
  176. #define PRCM_REQ_MB4_HOTMON_LOW (PRCM_REQ_MB4 + 0x0)
  177. #define PRCM_REQ_MB4_HOTMON_HIGH (PRCM_REQ_MB4 + 0x1)
  178. #define PRCM_REQ_MB4_HOTMON_CONFIG (PRCM_REQ_MB4 + 0x2)
  179. #define PRCM_REQ_MB4_HOT_PERIOD (PRCM_REQ_MB4 + 0x0)
  180. #define HOTMON_CONFIG_LOW BIT(0)
  181. #define HOTMON_CONFIG_HIGH BIT(1)
  182. #define PRCM_REQ_MB4_A9WDOG_0 (PRCM_REQ_MB4 + 0x0)
  183. #define PRCM_REQ_MB4_A9WDOG_1 (PRCM_REQ_MB4 + 0x1)
  184. #define PRCM_REQ_MB4_A9WDOG_2 (PRCM_REQ_MB4 + 0x2)
  185. #define PRCM_REQ_MB4_A9WDOG_3 (PRCM_REQ_MB4 + 0x3)
  186. #define A9WDOG_AUTO_OFF_EN BIT(7)
  187. #define A9WDOG_AUTO_OFF_DIS 0
  188. #define A9WDOG_ID_MASK 0xf
  189. /* Mailbox 5 Requests */
  190. #define PRCM_REQ_MB5_I2C_SLAVE_OP (PRCM_REQ_MB5 + 0x0)
  191. #define PRCM_REQ_MB5_I2C_HW_BITS (PRCM_REQ_MB5 + 0x1)
  192. #define PRCM_REQ_MB5_I2C_REG (PRCM_REQ_MB5 + 0x2)
  193. #define PRCM_REQ_MB5_I2C_VAL (PRCM_REQ_MB5 + 0x3)
  194. #define PRCMU_I2C_WRITE(slave) \
  195. (((slave) << 1) | (cpu_is_u8500v2() ? BIT(6) : 0))
  196. #define PRCMU_I2C_READ(slave) \
  197. (((slave) << 1) | BIT(0) | (cpu_is_u8500v2() ? BIT(6) : 0))
  198. #define PRCMU_I2C_STOP_EN BIT(3)
  199. /* Mailbox 5 ACKs */
  200. #define PRCM_ACK_MB5_I2C_STATUS (PRCM_ACK_MB5 + 0x1)
  201. #define PRCM_ACK_MB5_I2C_VAL (PRCM_ACK_MB5 + 0x3)
  202. #define I2C_WR_OK 0x1
  203. #define I2C_RD_OK 0x2
  204. #define NUM_MB 8
  205. #define MBOX_BIT BIT
  206. #define ALL_MBOX_BITS (MBOX_BIT(NUM_MB) - 1)
  207. /*
  208. * Wakeups/IRQs
  209. */
  210. #define WAKEUP_BIT_RTC BIT(0)
  211. #define WAKEUP_BIT_RTT0 BIT(1)
  212. #define WAKEUP_BIT_RTT1 BIT(2)
  213. #define WAKEUP_BIT_HSI0 BIT(3)
  214. #define WAKEUP_BIT_HSI1 BIT(4)
  215. #define WAKEUP_BIT_CA_WAKE BIT(5)
  216. #define WAKEUP_BIT_USB BIT(6)
  217. #define WAKEUP_BIT_ABB BIT(7)
  218. #define WAKEUP_BIT_ABB_FIFO BIT(8)
  219. #define WAKEUP_BIT_SYSCLK_OK BIT(9)
  220. #define WAKEUP_BIT_CA_SLEEP BIT(10)
  221. #define WAKEUP_BIT_AC_WAKE_ACK BIT(11)
  222. #define WAKEUP_BIT_SIDE_TONE_OK BIT(12)
  223. #define WAKEUP_BIT_ANC_OK BIT(13)
  224. #define WAKEUP_BIT_SW_ERROR BIT(14)
  225. #define WAKEUP_BIT_AC_SLEEP_ACK BIT(15)
  226. #define WAKEUP_BIT_ARM BIT(17)
  227. #define WAKEUP_BIT_HOTMON_LOW BIT(18)
  228. #define WAKEUP_BIT_HOTMON_HIGH BIT(19)
  229. #define WAKEUP_BIT_MODEM_SW_RESET_REQ BIT(20)
  230. #define WAKEUP_BIT_GPIO0 BIT(23)
  231. #define WAKEUP_BIT_GPIO1 BIT(24)
  232. #define WAKEUP_BIT_GPIO2 BIT(25)
  233. #define WAKEUP_BIT_GPIO3 BIT(26)
  234. #define WAKEUP_BIT_GPIO4 BIT(27)
  235. #define WAKEUP_BIT_GPIO5 BIT(28)
  236. #define WAKEUP_BIT_GPIO6 BIT(29)
  237. #define WAKEUP_BIT_GPIO7 BIT(30)
  238. #define WAKEUP_BIT_GPIO8 BIT(31)
  239. static struct {
  240. bool valid;
  241. struct prcmu_fw_version version;
  242. } fw_info;
  243. /*
  244. * This vector maps irq numbers to the bits in the bit field used in
  245. * communication with the PRCMU firmware.
  246. *
  247. * The reason for having this is to keep the irq numbers contiguous even though
  248. * the bits in the bit field are not. (The bits also have a tendency to move
  249. * around, to further complicate matters.)
  250. */
  251. #define IRQ_INDEX(_name) ((IRQ_PRCMU_##_name) - IRQ_PRCMU_BASE)
  252. #define IRQ_ENTRY(_name)[IRQ_INDEX(_name)] = (WAKEUP_BIT_##_name)
  253. static u32 prcmu_irq_bit[NUM_PRCMU_WAKEUPS] = {
  254. IRQ_ENTRY(RTC),
  255. IRQ_ENTRY(RTT0),
  256. IRQ_ENTRY(RTT1),
  257. IRQ_ENTRY(HSI0),
  258. IRQ_ENTRY(HSI1),
  259. IRQ_ENTRY(CA_WAKE),
  260. IRQ_ENTRY(USB),
  261. IRQ_ENTRY(ABB),
  262. IRQ_ENTRY(ABB_FIFO),
  263. IRQ_ENTRY(CA_SLEEP),
  264. IRQ_ENTRY(ARM),
  265. IRQ_ENTRY(HOTMON_LOW),
  266. IRQ_ENTRY(HOTMON_HIGH),
  267. IRQ_ENTRY(MODEM_SW_RESET_REQ),
  268. IRQ_ENTRY(GPIO0),
  269. IRQ_ENTRY(GPIO1),
  270. IRQ_ENTRY(GPIO2),
  271. IRQ_ENTRY(GPIO3),
  272. IRQ_ENTRY(GPIO4),
  273. IRQ_ENTRY(GPIO5),
  274. IRQ_ENTRY(GPIO6),
  275. IRQ_ENTRY(GPIO7),
  276. IRQ_ENTRY(GPIO8)
  277. };
  278. #define VALID_WAKEUPS (BIT(NUM_PRCMU_WAKEUP_INDICES) - 1)
  279. #define WAKEUP_ENTRY(_name)[PRCMU_WAKEUP_INDEX_##_name] = (WAKEUP_BIT_##_name)
  280. static u32 prcmu_wakeup_bit[NUM_PRCMU_WAKEUP_INDICES] = {
  281. WAKEUP_ENTRY(RTC),
  282. WAKEUP_ENTRY(RTT0),
  283. WAKEUP_ENTRY(RTT1),
  284. WAKEUP_ENTRY(HSI0),
  285. WAKEUP_ENTRY(HSI1),
  286. WAKEUP_ENTRY(USB),
  287. WAKEUP_ENTRY(ABB),
  288. WAKEUP_ENTRY(ABB_FIFO),
  289. WAKEUP_ENTRY(ARM)
  290. };
  291. /*
  292. * mb0_transfer - state needed for mailbox 0 communication.
  293. * @lock: The transaction lock.
  294. * @dbb_events_lock: A lock used to handle concurrent access to (parts of)
  295. * the request data.
  296. * @mask_work: Work structure used for (un)masking wakeup interrupts.
  297. * @req: Request data that need to persist between requests.
  298. */
  299. static struct {
  300. spinlock_t lock;
  301. spinlock_t dbb_irqs_lock;
  302. struct work_struct mask_work;
  303. struct mutex ac_wake_lock;
  304. struct completion ac_wake_work;
  305. struct {
  306. u32 dbb_irqs;
  307. u32 dbb_wakeups;
  308. u32 abb_events;
  309. } req;
  310. } mb0_transfer;
  311. /*
  312. * mb1_transfer - state needed for mailbox 1 communication.
  313. * @lock: The transaction lock.
  314. * @work: The transaction completion structure.
  315. * @ape_opp: The current APE OPP.
  316. * @ack: Reply ("acknowledge") data.
  317. */
  318. static struct {
  319. struct mutex lock;
  320. struct completion work;
  321. u8 ape_opp;
  322. struct {
  323. u8 header;
  324. u8 arm_opp;
  325. u8 ape_opp;
  326. u8 ape_voltage_status;
  327. } ack;
  328. } mb1_transfer;
  329. /*
  330. * mb2_transfer - state needed for mailbox 2 communication.
  331. * @lock: The transaction lock.
  332. * @work: The transaction completion structure.
  333. * @auto_pm_lock: The autonomous power management configuration lock.
  334. * @auto_pm_enabled: A flag indicating whether autonomous PM is enabled.
  335. * @req: Request data that need to persist between requests.
  336. * @ack: Reply ("acknowledge") data.
  337. */
  338. static struct {
  339. struct mutex lock;
  340. struct completion work;
  341. spinlock_t auto_pm_lock;
  342. bool auto_pm_enabled;
  343. struct {
  344. u8 status;
  345. } ack;
  346. } mb2_transfer;
  347. /*
  348. * mb3_transfer - state needed for mailbox 3 communication.
  349. * @lock: The request lock.
  350. * @sysclk_lock: A lock used to handle concurrent sysclk requests.
  351. * @sysclk_work: Work structure used for sysclk requests.
  352. */
  353. static struct {
  354. spinlock_t lock;
  355. struct mutex sysclk_lock;
  356. struct completion sysclk_work;
  357. } mb3_transfer;
  358. /*
  359. * mb4_transfer - state needed for mailbox 4 communication.
  360. * @lock: The transaction lock.
  361. * @work: The transaction completion structure.
  362. */
  363. static struct {
  364. struct mutex lock;
  365. struct completion work;
  366. } mb4_transfer;
  367. /*
  368. * mb5_transfer - state needed for mailbox 5 communication.
  369. * @lock: The transaction lock.
  370. * @work: The transaction completion structure.
  371. * @ack: Reply ("acknowledge") data.
  372. */
  373. static struct {
  374. struct mutex lock;
  375. struct completion work;
  376. struct {
  377. u8 status;
  378. u8 value;
  379. } ack;
  380. } mb5_transfer;
  381. static atomic_t ac_wake_req_state = ATOMIC_INIT(0);
  382. /* Spinlocks */
  383. static DEFINE_SPINLOCK(prcmu_lock);
  384. static DEFINE_SPINLOCK(clkout_lock);
  385. /* Global var to runtime determine TCDM base for v2 or v1 */
  386. static __iomem void *tcdm_base;
  387. struct clk_mgt {
  388. void __iomem *reg;
  389. u32 pllsw;
  390. int branch;
  391. bool clk38div;
  392. };
  393. enum {
  394. PLL_RAW,
  395. PLL_FIX,
  396. PLL_DIV
  397. };
  398. static DEFINE_SPINLOCK(clk_mgt_lock);
  399. #define CLK_MGT_ENTRY(_name, _branch, _clk38div)[PRCMU_##_name] = \
  400. { (PRCM_##_name##_MGT), 0 , _branch, _clk38div}
  401. struct clk_mgt clk_mgt[PRCMU_NUM_REG_CLOCKS] = {
  402. CLK_MGT_ENTRY(SGACLK, PLL_DIV, false),
  403. CLK_MGT_ENTRY(UARTCLK, PLL_FIX, true),
  404. CLK_MGT_ENTRY(MSP02CLK, PLL_FIX, true),
  405. CLK_MGT_ENTRY(MSP1CLK, PLL_FIX, true),
  406. CLK_MGT_ENTRY(I2CCLK, PLL_FIX, true),
  407. CLK_MGT_ENTRY(SDMMCCLK, PLL_DIV, true),
  408. CLK_MGT_ENTRY(SLIMCLK, PLL_FIX, true),
  409. CLK_MGT_ENTRY(PER1CLK, PLL_DIV, true),
  410. CLK_MGT_ENTRY(PER2CLK, PLL_DIV, true),
  411. CLK_MGT_ENTRY(PER3CLK, PLL_DIV, true),
  412. CLK_MGT_ENTRY(PER5CLK, PLL_DIV, true),
  413. CLK_MGT_ENTRY(PER6CLK, PLL_DIV, true),
  414. CLK_MGT_ENTRY(PER7CLK, PLL_DIV, true),
  415. CLK_MGT_ENTRY(LCDCLK, PLL_FIX, true),
  416. CLK_MGT_ENTRY(BMLCLK, PLL_DIV, true),
  417. CLK_MGT_ENTRY(HSITXCLK, PLL_DIV, true),
  418. CLK_MGT_ENTRY(HSIRXCLK, PLL_DIV, true),
  419. CLK_MGT_ENTRY(HDMICLK, PLL_FIX, false),
  420. CLK_MGT_ENTRY(APEATCLK, PLL_DIV, true),
  421. CLK_MGT_ENTRY(APETRACECLK, PLL_DIV, true),
  422. CLK_MGT_ENTRY(MCDECLK, PLL_DIV, true),
  423. CLK_MGT_ENTRY(IPI2CCLK, PLL_FIX, true),
  424. CLK_MGT_ENTRY(DSIALTCLK, PLL_FIX, false),
  425. CLK_MGT_ENTRY(DMACLK, PLL_DIV, true),
  426. CLK_MGT_ENTRY(B2R2CLK, PLL_DIV, true),
  427. CLK_MGT_ENTRY(TVCLK, PLL_FIX, true),
  428. CLK_MGT_ENTRY(SSPCLK, PLL_FIX, true),
  429. CLK_MGT_ENTRY(RNGCLK, PLL_FIX, true),
  430. CLK_MGT_ENTRY(UICCCLK, PLL_FIX, false),
  431. };
  432. struct dsiclk {
  433. u32 divsel_mask;
  434. u32 divsel_shift;
  435. u32 divsel;
  436. };
  437. static struct dsiclk dsiclk[2] = {
  438. {
  439. .divsel_mask = PRCM_DSI_PLLOUT_SEL_DSI0_PLLOUT_DIVSEL_MASK,
  440. .divsel_shift = PRCM_DSI_PLLOUT_SEL_DSI0_PLLOUT_DIVSEL_SHIFT,
  441. .divsel = PRCM_DSI_PLLOUT_SEL_PHI,
  442. },
  443. {
  444. .divsel_mask = PRCM_DSI_PLLOUT_SEL_DSI1_PLLOUT_DIVSEL_MASK,
  445. .divsel_shift = PRCM_DSI_PLLOUT_SEL_DSI1_PLLOUT_DIVSEL_SHIFT,
  446. .divsel = PRCM_DSI_PLLOUT_SEL_PHI,
  447. }
  448. };
  449. struct dsiescclk {
  450. u32 en;
  451. u32 div_mask;
  452. u32 div_shift;
  453. };
  454. static struct dsiescclk dsiescclk[3] = {
  455. {
  456. .en = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_EN,
  457. .div_mask = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_DIV_MASK,
  458. .div_shift = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_DIV_SHIFT,
  459. },
  460. {
  461. .en = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_EN,
  462. .div_mask = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_DIV_MASK,
  463. .div_shift = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_DIV_SHIFT,
  464. },
  465. {
  466. .en = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_EN,
  467. .div_mask = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_DIV_MASK,
  468. .div_shift = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_DIV_SHIFT,
  469. }
  470. };
  471. /*
  472. * Used by MCDE to setup all necessary PRCMU registers
  473. */
  474. #define PRCMU_RESET_DSIPLL 0x00004000
  475. #define PRCMU_UNCLAMP_DSIPLL 0x00400800
  476. #define PRCMU_CLK_PLL_DIV_SHIFT 0
  477. #define PRCMU_CLK_PLL_SW_SHIFT 5
  478. #define PRCMU_CLK_38 (1 << 9)
  479. #define PRCMU_CLK_38_SRC (1 << 10)
  480. #define PRCMU_CLK_38_DIV (1 << 11)
  481. /* PLLDIV=12, PLLSW=4 (PLLDDR) */
  482. #define PRCMU_DSI_CLOCK_SETTING 0x0000008C
  483. /* DPI 50000000 Hz */
  484. #define PRCMU_DPI_CLOCK_SETTING ((1 << PRCMU_CLK_PLL_SW_SHIFT) | \
  485. (16 << PRCMU_CLK_PLL_DIV_SHIFT))
  486. #define PRCMU_DSI_LP_CLOCK_SETTING 0x00000E00
  487. /* D=101, N=1, R=4, SELDIV2=0 */
  488. #define PRCMU_PLLDSI_FREQ_SETTING 0x00040165
  489. #define PRCMU_ENABLE_PLLDSI 0x00000001
  490. #define PRCMU_DISABLE_PLLDSI 0x00000000
  491. #define PRCMU_RELEASE_RESET_DSS 0x0000400C
  492. #define PRCMU_DSI_PLLOUT_SEL_SETTING 0x00000202
  493. /* ESC clk, div0=1, div1=1, div2=3 */
  494. #define PRCMU_ENABLE_ESCAPE_CLOCK_DIV 0x07030101
  495. #define PRCMU_DISABLE_ESCAPE_CLOCK_DIV 0x00030101
  496. #define PRCMU_DSI_RESET_SW 0x00000007
  497. #define PRCMU_PLLDSI_LOCKP_LOCKED 0x3
  498. int db8500_prcmu_enable_dsipll(void)
  499. {
  500. int i;
  501. /* Clear DSIPLL_RESETN */
  502. writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_CLR);
  503. /* Unclamp DSIPLL in/out */
  504. writel(PRCMU_UNCLAMP_DSIPLL, PRCM_MMIP_LS_CLAMP_CLR);
  505. /* Set DSI PLL FREQ */
  506. writel(PRCMU_PLLDSI_FREQ_SETTING, PRCM_PLLDSI_FREQ);
  507. writel(PRCMU_DSI_PLLOUT_SEL_SETTING, PRCM_DSI_PLLOUT_SEL);
  508. /* Enable Escape clocks */
  509. writel(PRCMU_ENABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV);
  510. /* Start DSI PLL */
  511. writel(PRCMU_ENABLE_PLLDSI, PRCM_PLLDSI_ENABLE);
  512. /* Reset DSI PLL */
  513. writel(PRCMU_DSI_RESET_SW, PRCM_DSI_SW_RESET);
  514. for (i = 0; i < 10; i++) {
  515. if ((readl(PRCM_PLLDSI_LOCKP) & PRCMU_PLLDSI_LOCKP_LOCKED)
  516. == PRCMU_PLLDSI_LOCKP_LOCKED)
  517. break;
  518. udelay(100);
  519. }
  520. /* Set DSIPLL_RESETN */
  521. writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_SET);
  522. return 0;
  523. }
  524. int db8500_prcmu_disable_dsipll(void)
  525. {
  526. /* Disable dsi pll */
  527. writel(PRCMU_DISABLE_PLLDSI, PRCM_PLLDSI_ENABLE);
  528. /* Disable escapeclock */
  529. writel(PRCMU_DISABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV);
  530. return 0;
  531. }
  532. int db8500_prcmu_set_display_clocks(void)
  533. {
  534. unsigned long flags;
  535. spin_lock_irqsave(&clk_mgt_lock, flags);
  536. /* Grab the HW semaphore. */
  537. while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
  538. cpu_relax();
  539. writel(PRCMU_DSI_CLOCK_SETTING, PRCM_HDMICLK_MGT);
  540. writel(PRCMU_DSI_LP_CLOCK_SETTING, PRCM_TVCLK_MGT);
  541. writel(PRCMU_DPI_CLOCK_SETTING, PRCM_LCDCLK_MGT);
  542. /* Release the HW semaphore. */
  543. writel(0, PRCM_SEM);
  544. spin_unlock_irqrestore(&clk_mgt_lock, flags);
  545. return 0;
  546. }
  547. u32 db8500_prcmu_read(unsigned int reg)
  548. {
  549. return readl(_PRCMU_BASE + reg);
  550. }
  551. void db8500_prcmu_write(unsigned int reg, u32 value)
  552. {
  553. unsigned long flags;
  554. spin_lock_irqsave(&prcmu_lock, flags);
  555. writel(value, (_PRCMU_BASE + reg));
  556. spin_unlock_irqrestore(&prcmu_lock, flags);
  557. }
  558. void db8500_prcmu_write_masked(unsigned int reg, u32 mask, u32 value)
  559. {
  560. u32 val;
  561. unsigned long flags;
  562. spin_lock_irqsave(&prcmu_lock, flags);
  563. val = readl(_PRCMU_BASE + reg);
  564. val = ((val & ~mask) | (value & mask));
  565. writel(val, (_PRCMU_BASE + reg));
  566. spin_unlock_irqrestore(&prcmu_lock, flags);
  567. }
  568. struct prcmu_fw_version *prcmu_get_fw_version(void)
  569. {
  570. return fw_info.valid ? &fw_info.version : NULL;
  571. }
  572. bool prcmu_has_arm_maxopp(void)
  573. {
  574. return (readb(tcdm_base + PRCM_AVS_VARM_MAX_OPP) &
  575. PRCM_AVS_ISMODEENABLE_MASK) == PRCM_AVS_ISMODEENABLE_MASK;
  576. }
  577. /**
  578. * prcmu_get_boot_status - PRCMU boot status checking
  579. * Returns: the current PRCMU boot status
  580. */
  581. int prcmu_get_boot_status(void)
  582. {
  583. return readb(tcdm_base + PRCM_BOOT_STATUS);
  584. }
  585. /**
  586. * prcmu_set_rc_a2p - This function is used to run few power state sequences
  587. * @val: Value to be set, i.e. transition requested
  588. * Returns: 0 on success, -EINVAL on invalid argument
  589. *
  590. * This function is used to run the following power state sequences -
  591. * any state to ApReset, ApDeepSleep to ApExecute, ApExecute to ApDeepSleep
  592. */
  593. int prcmu_set_rc_a2p(enum romcode_write val)
  594. {
  595. if (val < RDY_2_DS || val > RDY_2_XP70_RST)
  596. return -EINVAL;
  597. writeb(val, (tcdm_base + PRCM_ROMCODE_A2P));
  598. return 0;
  599. }
  600. /**
  601. * prcmu_get_rc_p2a - This function is used to get power state sequences
  602. * Returns: the power transition that has last happened
  603. *
  604. * This function can return the following transitions-
  605. * any state to ApReset, ApDeepSleep to ApExecute, ApExecute to ApDeepSleep
  606. */
  607. enum romcode_read prcmu_get_rc_p2a(void)
  608. {
  609. return readb(tcdm_base + PRCM_ROMCODE_P2A);
  610. }
  611. /**
  612. * prcmu_get_current_mode - Return the current XP70 power mode
  613. * Returns: Returns the current AP(ARM) power mode: init,
  614. * apBoot, apExecute, apDeepSleep, apSleep, apIdle, apReset
  615. */
  616. enum ap_pwrst prcmu_get_xp70_current_state(void)
  617. {
  618. return readb(tcdm_base + PRCM_XP70_CUR_PWR_STATE);
  619. }
  620. /**
  621. * prcmu_config_clkout - Configure one of the programmable clock outputs.
  622. * @clkout: The CLKOUT number (0 or 1).
  623. * @source: The clock to be used (one of the PRCMU_CLKSRC_*).
  624. * @div: The divider to be applied.
  625. *
  626. * Configures one of the programmable clock outputs (CLKOUTs).
  627. * @div should be in the range [1,63] to request a configuration, or 0 to
  628. * inform that the configuration is no longer requested.
  629. */
  630. int prcmu_config_clkout(u8 clkout, u8 source, u8 div)
  631. {
  632. static int requests[2];
  633. int r = 0;
  634. unsigned long flags;
  635. u32 val;
  636. u32 bits;
  637. u32 mask;
  638. u32 div_mask;
  639. BUG_ON(clkout > 1);
  640. BUG_ON(div > 63);
  641. BUG_ON((clkout == 0) && (source > PRCMU_CLKSRC_CLK009));
  642. if (!div && !requests[clkout])
  643. return -EINVAL;
  644. switch (clkout) {
  645. case 0:
  646. div_mask = PRCM_CLKOCR_CLKODIV0_MASK;
  647. mask = (PRCM_CLKOCR_CLKODIV0_MASK | PRCM_CLKOCR_CLKOSEL0_MASK);
  648. bits = ((source << PRCM_CLKOCR_CLKOSEL0_SHIFT) |
  649. (div << PRCM_CLKOCR_CLKODIV0_SHIFT));
  650. break;
  651. case 1:
  652. div_mask = PRCM_CLKOCR_CLKODIV1_MASK;
  653. mask = (PRCM_CLKOCR_CLKODIV1_MASK | PRCM_CLKOCR_CLKOSEL1_MASK |
  654. PRCM_CLKOCR_CLK1TYPE);
  655. bits = ((source << PRCM_CLKOCR_CLKOSEL1_SHIFT) |
  656. (div << PRCM_CLKOCR_CLKODIV1_SHIFT));
  657. break;
  658. }
  659. bits &= mask;
  660. spin_lock_irqsave(&clkout_lock, flags);
  661. val = readl(PRCM_CLKOCR);
  662. if (val & div_mask) {
  663. if (div) {
  664. if ((val & mask) != bits) {
  665. r = -EBUSY;
  666. goto unlock_and_return;
  667. }
  668. } else {
  669. if ((val & mask & ~div_mask) != bits) {
  670. r = -EINVAL;
  671. goto unlock_and_return;
  672. }
  673. }
  674. }
  675. writel((bits | (val & ~mask)), PRCM_CLKOCR);
  676. requests[clkout] += (div ? 1 : -1);
  677. unlock_and_return:
  678. spin_unlock_irqrestore(&clkout_lock, flags);
  679. return r;
  680. }
  681. int db8500_prcmu_set_power_state(u8 state, bool keep_ulp_clk, bool keep_ap_pll)
  682. {
  683. unsigned long flags;
  684. BUG_ON((state < PRCMU_AP_SLEEP) || (PRCMU_AP_DEEP_IDLE < state));
  685. spin_lock_irqsave(&mb0_transfer.lock, flags);
  686. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
  687. cpu_relax();
  688. writeb(MB0H_POWER_STATE_TRANS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
  689. writeb(state, (tcdm_base + PRCM_REQ_MB0_AP_POWER_STATE));
  690. writeb((keep_ap_pll ? 1 : 0), (tcdm_base + PRCM_REQ_MB0_AP_PLL_STATE));
  691. writeb((keep_ulp_clk ? 1 : 0),
  692. (tcdm_base + PRCM_REQ_MB0_ULP_CLOCK_STATE));
  693. writeb(0, (tcdm_base + PRCM_REQ_MB0_DO_NOT_WFI));
  694. writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
  695. spin_unlock_irqrestore(&mb0_transfer.lock, flags);
  696. return 0;
  697. }
  698. u8 db8500_prcmu_get_power_state_result(void)
  699. {
  700. return readb(tcdm_base + PRCM_ACK_MB0_AP_PWRSTTR_STATUS);
  701. }
  702. /* This function decouple the gic from the prcmu */
  703. int db8500_prcmu_gic_decouple(void)
  704. {
  705. u32 val = readl(PRCM_A9_MASK_REQ);
  706. /* Set bit 0 register value to 1 */
  707. writel(val | PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ,
  708. PRCM_A9_MASK_REQ);
  709. /* Make sure the register is updated */
  710. readl(PRCM_A9_MASK_REQ);
  711. /* Wait a few cycles for the gic mask completion */
  712. udelay(1);
  713. return 0;
  714. }
  715. /* This function recouple the gic with the prcmu */
  716. int db8500_prcmu_gic_recouple(void)
  717. {
  718. u32 val = readl(PRCM_A9_MASK_REQ);
  719. /* Set bit 0 register value to 0 */
  720. writel(val & ~PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ, PRCM_A9_MASK_REQ);
  721. return 0;
  722. }
  723. #define PRCMU_GIC_NUMBER_REGS 5
  724. /*
  725. * This function checks if there are pending irq on the gic. It only
  726. * makes sense if the gic has been decoupled before with the
  727. * db8500_prcmu_gic_decouple function. Disabling an interrupt only
  728. * disables the forwarding of the interrupt to any CPU interface. It
  729. * does not prevent the interrupt from changing state, for example
  730. * becoming pending, or active and pending if it is already
  731. * active. Hence, we have to check the interrupt is pending *and* is
  732. * active.
  733. */
  734. bool db8500_prcmu_gic_pending_irq(void)
  735. {
  736. u32 pr; /* Pending register */
  737. u32 er; /* Enable register */
  738. void __iomem *dist_base = __io_address(U8500_GIC_DIST_BASE);
  739. int i;
  740. /* 5 registers. STI & PPI not skipped */
  741. for (i = 0; i < PRCMU_GIC_NUMBER_REGS; i++) {
  742. pr = readl_relaxed(dist_base + GIC_DIST_PENDING_SET + i * 4);
  743. er = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  744. if (pr & er)
  745. return true; /* There is a pending interrupt */
  746. }
  747. return false;
  748. }
  749. /*
  750. * This function checks if there are pending interrupt on the
  751. * prcmu which has been delegated to monitor the irqs with the
  752. * db8500_prcmu_copy_gic_settings function.
  753. */
  754. bool db8500_prcmu_pending_irq(void)
  755. {
  756. u32 it, im;
  757. int i;
  758. for (i = 0; i < PRCMU_GIC_NUMBER_REGS - 1; i++) {
  759. it = readl(PRCM_ARMITVAL31TO0 + i * 4);
  760. im = readl(PRCM_ARMITMSK31TO0 + i * 4);
  761. if (it & im)
  762. return true; /* There is a pending interrupt */
  763. }
  764. return false;
  765. }
  766. /*
  767. * This function checks if the specified cpu is in in WFI. It's usage
  768. * makes sense only if the gic is decoupled with the db8500_prcmu_gic_decouple
  769. * function. Of course passing smp_processor_id() to this function will
  770. * always return false...
  771. */
  772. bool db8500_prcmu_is_cpu_in_wfi(int cpu)
  773. {
  774. return readl(PRCM_ARM_WFI_STANDBY) & cpu ? PRCM_ARM_WFI_STANDBY_WFI1 :
  775. PRCM_ARM_WFI_STANDBY_WFI0;
  776. }
  777. /*
  778. * This function copies the gic SPI settings to the prcmu in order to
  779. * monitor them and abort/finish the retention/off sequence or state.
  780. */
  781. int db8500_prcmu_copy_gic_settings(void)
  782. {
  783. u32 er; /* Enable register */
  784. void __iomem *dist_base = __io_address(U8500_GIC_DIST_BASE);
  785. int i;
  786. /* We skip the STI and PPI */
  787. for (i = 0; i < PRCMU_GIC_NUMBER_REGS - 1; i++) {
  788. er = readl_relaxed(dist_base +
  789. GIC_DIST_ENABLE_SET + (i + 1) * 4);
  790. writel(er, PRCM_ARMITMSK31TO0 + i * 4);
  791. }
  792. return 0;
  793. }
  794. /* This function should only be called while mb0_transfer.lock is held. */
  795. static void config_wakeups(void)
  796. {
  797. const u8 header[2] = {
  798. MB0H_CONFIG_WAKEUPS_EXE,
  799. MB0H_CONFIG_WAKEUPS_SLEEP
  800. };
  801. static u32 last_dbb_events;
  802. static u32 last_abb_events;
  803. u32 dbb_events;
  804. u32 abb_events;
  805. unsigned int i;
  806. dbb_events = mb0_transfer.req.dbb_irqs | mb0_transfer.req.dbb_wakeups;
  807. dbb_events |= (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK);
  808. abb_events = mb0_transfer.req.abb_events;
  809. if ((dbb_events == last_dbb_events) && (abb_events == last_abb_events))
  810. return;
  811. for (i = 0; i < 2; i++) {
  812. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
  813. cpu_relax();
  814. writel(dbb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_8500));
  815. writel(abb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_4500));
  816. writeb(header[i], (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
  817. writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
  818. }
  819. last_dbb_events = dbb_events;
  820. last_abb_events = abb_events;
  821. }
  822. void db8500_prcmu_enable_wakeups(u32 wakeups)
  823. {
  824. unsigned long flags;
  825. u32 bits;
  826. int i;
  827. BUG_ON(wakeups != (wakeups & VALID_WAKEUPS));
  828. for (i = 0, bits = 0; i < NUM_PRCMU_WAKEUP_INDICES; i++) {
  829. if (wakeups & BIT(i))
  830. bits |= prcmu_wakeup_bit[i];
  831. }
  832. spin_lock_irqsave(&mb0_transfer.lock, flags);
  833. mb0_transfer.req.dbb_wakeups = bits;
  834. config_wakeups();
  835. spin_unlock_irqrestore(&mb0_transfer.lock, flags);
  836. }
  837. void db8500_prcmu_config_abb_event_readout(u32 abb_events)
  838. {
  839. unsigned long flags;
  840. spin_lock_irqsave(&mb0_transfer.lock, flags);
  841. mb0_transfer.req.abb_events = abb_events;
  842. config_wakeups();
  843. spin_unlock_irqrestore(&mb0_transfer.lock, flags);
  844. }
  845. void db8500_prcmu_get_abb_event_buffer(void __iomem **buf)
  846. {
  847. if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
  848. *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_1_4500);
  849. else
  850. *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_0_4500);
  851. }
  852. /**
  853. * db8500_prcmu_set_arm_opp - set the appropriate ARM OPP
  854. * @opp: The new ARM operating point to which transition is to be made
  855. * Returns: 0 on success, non-zero on failure
  856. *
  857. * This function sets the the operating point of the ARM.
  858. */
  859. int db8500_prcmu_set_arm_opp(u8 opp)
  860. {
  861. int r;
  862. if (opp < ARM_NO_CHANGE || opp > ARM_EXTCLK)
  863. return -EINVAL;
  864. r = 0;
  865. mutex_lock(&mb1_transfer.lock);
  866. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
  867. cpu_relax();
  868. writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
  869. writeb(opp, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
  870. writeb(APE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_APE_OPP));
  871. writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
  872. wait_for_completion(&mb1_transfer.work);
  873. if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
  874. (mb1_transfer.ack.arm_opp != opp))
  875. r = -EIO;
  876. mutex_unlock(&mb1_transfer.lock);
  877. return r;
  878. }
  879. /**
  880. * db8500_prcmu_get_arm_opp - get the current ARM OPP
  881. *
  882. * Returns: the current ARM OPP
  883. */
  884. int db8500_prcmu_get_arm_opp(void)
  885. {
  886. return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_ARM_OPP);
  887. }
  888. /**
  889. * db8500_prcmu_get_ddr_opp - get the current DDR OPP
  890. *
  891. * Returns: the current DDR OPP
  892. */
  893. int db8500_prcmu_get_ddr_opp(void)
  894. {
  895. return readb(PRCM_DDR_SUBSYS_APE_MINBW);
  896. }
  897. /**
  898. * db8500_set_ddr_opp - set the appropriate DDR OPP
  899. * @opp: The new DDR operating point to which transition is to be made
  900. * Returns: 0 on success, non-zero on failure
  901. *
  902. * This function sets the operating point of the DDR.
  903. */
  904. int db8500_prcmu_set_ddr_opp(u8 opp)
  905. {
  906. if (opp < DDR_100_OPP || opp > DDR_25_OPP)
  907. return -EINVAL;
  908. /* Changing the DDR OPP can hang the hardware pre-v21 */
  909. if (cpu_is_u8500v20_or_later() && !cpu_is_u8500v20())
  910. writeb(opp, PRCM_DDR_SUBSYS_APE_MINBW);
  911. return 0;
  912. }
  913. /* Divide the frequency of certain clocks by 2 for APE_50_PARTLY_25_OPP. */
  914. static void request_even_slower_clocks(bool enable)
  915. {
  916. void __iomem *clock_reg[] = {
  917. PRCM_ACLK_MGT,
  918. PRCM_DMACLK_MGT
  919. };
  920. unsigned long flags;
  921. unsigned int i;
  922. spin_lock_irqsave(&clk_mgt_lock, flags);
  923. /* Grab the HW semaphore. */
  924. while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
  925. cpu_relax();
  926. for (i = 0; i < ARRAY_SIZE(clock_reg); i++) {
  927. u32 val;
  928. u32 div;
  929. val = readl(clock_reg[i]);
  930. div = (val & PRCM_CLK_MGT_CLKPLLDIV_MASK);
  931. if (enable) {
  932. if ((div <= 1) || (div > 15)) {
  933. pr_err("prcmu: Bad clock divider %d in %s\n",
  934. div, __func__);
  935. goto unlock_and_return;
  936. }
  937. div <<= 1;
  938. } else {
  939. if (div <= 2)
  940. goto unlock_and_return;
  941. div >>= 1;
  942. }
  943. val = ((val & ~PRCM_CLK_MGT_CLKPLLDIV_MASK) |
  944. (div & PRCM_CLK_MGT_CLKPLLDIV_MASK));
  945. writel(val, clock_reg[i]);
  946. }
  947. unlock_and_return:
  948. /* Release the HW semaphore. */
  949. writel(0, PRCM_SEM);
  950. spin_unlock_irqrestore(&clk_mgt_lock, flags);
  951. }
  952. /**
  953. * db8500_set_ape_opp - set the appropriate APE OPP
  954. * @opp: The new APE operating point to which transition is to be made
  955. * Returns: 0 on success, non-zero on failure
  956. *
  957. * This function sets the operating point of the APE.
  958. */
  959. int db8500_prcmu_set_ape_opp(u8 opp)
  960. {
  961. int r = 0;
  962. if (opp == mb1_transfer.ape_opp)
  963. return 0;
  964. mutex_lock(&mb1_transfer.lock);
  965. if (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)
  966. request_even_slower_clocks(false);
  967. if ((opp != APE_100_OPP) && (mb1_transfer.ape_opp != APE_100_OPP))
  968. goto skip_message;
  969. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
  970. cpu_relax();
  971. writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
  972. writeb(ARM_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
  973. writeb(((opp == APE_50_PARTLY_25_OPP) ? APE_50_OPP : opp),
  974. (tcdm_base + PRCM_REQ_MB1_APE_OPP));
  975. writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
  976. wait_for_completion(&mb1_transfer.work);
  977. if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
  978. (mb1_transfer.ack.ape_opp != opp))
  979. r = -EIO;
  980. skip_message:
  981. if ((!r && (opp == APE_50_PARTLY_25_OPP)) ||
  982. (r && (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)))
  983. request_even_slower_clocks(true);
  984. if (!r)
  985. mb1_transfer.ape_opp = opp;
  986. mutex_unlock(&mb1_transfer.lock);
  987. return r;
  988. }
  989. /**
  990. * db8500_prcmu_get_ape_opp - get the current APE OPP
  991. *
  992. * Returns: the current APE OPP
  993. */
  994. int db8500_prcmu_get_ape_opp(void)
  995. {
  996. return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_APE_OPP);
  997. }
  998. /**
  999. * prcmu_request_ape_opp_100_voltage - Request APE OPP 100% voltage
  1000. * @enable: true to request the higher voltage, false to drop a request.
  1001. *
  1002. * Calls to this function to enable and disable requests must be balanced.
  1003. */
  1004. int prcmu_request_ape_opp_100_voltage(bool enable)
  1005. {
  1006. int r = 0;
  1007. u8 header;
  1008. static unsigned int requests;
  1009. mutex_lock(&mb1_transfer.lock);
  1010. if (enable) {
  1011. if (0 != requests++)
  1012. goto unlock_and_return;
  1013. header = MB1H_REQUEST_APE_OPP_100_VOLT;
  1014. } else {
  1015. if (requests == 0) {
  1016. r = -EIO;
  1017. goto unlock_and_return;
  1018. } else if (1 != requests--) {
  1019. goto unlock_and_return;
  1020. }
  1021. header = MB1H_RELEASE_APE_OPP_100_VOLT;
  1022. }
  1023. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
  1024. cpu_relax();
  1025. writeb(header, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
  1026. writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
  1027. wait_for_completion(&mb1_transfer.work);
  1028. if ((mb1_transfer.ack.header != header) ||
  1029. ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
  1030. r = -EIO;
  1031. unlock_and_return:
  1032. mutex_unlock(&mb1_transfer.lock);
  1033. return r;
  1034. }
  1035. /**
  1036. * prcmu_release_usb_wakeup_state - release the state required by a USB wakeup
  1037. *
  1038. * This function releases the power state requirements of a USB wakeup.
  1039. */
  1040. int prcmu_release_usb_wakeup_state(void)
  1041. {
  1042. int r = 0;
  1043. mutex_lock(&mb1_transfer.lock);
  1044. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
  1045. cpu_relax();
  1046. writeb(MB1H_RELEASE_USB_WAKEUP,
  1047. (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
  1048. writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
  1049. wait_for_completion(&mb1_transfer.work);
  1050. if ((mb1_transfer.ack.header != MB1H_RELEASE_USB_WAKEUP) ||
  1051. ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
  1052. r = -EIO;
  1053. mutex_unlock(&mb1_transfer.lock);
  1054. return r;
  1055. }
  1056. static int request_pll(u8 clock, bool enable)
  1057. {
  1058. int r = 0;
  1059. if (clock == PRCMU_PLLSOC0)
  1060. clock = (enable ? PLL_SOC0_ON : PLL_SOC0_OFF);
  1061. else if (clock == PRCMU_PLLSOC1)
  1062. clock = (enable ? PLL_SOC1_ON : PLL_SOC1_OFF);
  1063. else
  1064. return -EINVAL;
  1065. mutex_lock(&mb1_transfer.lock);
  1066. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
  1067. cpu_relax();
  1068. writeb(MB1H_PLL_ON_OFF, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
  1069. writeb(clock, (tcdm_base + PRCM_REQ_MB1_PLL_ON_OFF));
  1070. writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
  1071. wait_for_completion(&mb1_transfer.work);
  1072. if (mb1_transfer.ack.header != MB1H_PLL_ON_OFF)
  1073. r = -EIO;
  1074. mutex_unlock(&mb1_transfer.lock);
  1075. return r;
  1076. }
  1077. /**
  1078. * db8500_prcmu_set_epod - set the state of a EPOD (power domain)
  1079. * @epod_id: The EPOD to set
  1080. * @epod_state: The new EPOD state
  1081. *
  1082. * This function sets the state of a EPOD (power domain). It may not be called
  1083. * from interrupt context.
  1084. */
  1085. int db8500_prcmu_set_epod(u16 epod_id, u8 epod_state)
  1086. {
  1087. int r = 0;
  1088. bool ram_retention = false;
  1089. int i;
  1090. /* check argument */
  1091. BUG_ON(epod_id >= NUM_EPOD_ID);
  1092. /* set flag if retention is possible */
  1093. switch (epod_id) {
  1094. case EPOD_ID_SVAMMDSP:
  1095. case EPOD_ID_SIAMMDSP:
  1096. case EPOD_ID_ESRAM12:
  1097. case EPOD_ID_ESRAM34:
  1098. ram_retention = true;
  1099. break;
  1100. }
  1101. /* check argument */
  1102. BUG_ON(epod_state > EPOD_STATE_ON);
  1103. BUG_ON(epod_state == EPOD_STATE_RAMRET && !ram_retention);
  1104. /* get lock */
  1105. mutex_lock(&mb2_transfer.lock);
  1106. /* wait for mailbox */
  1107. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(2))
  1108. cpu_relax();
  1109. /* fill in mailbox */
  1110. for (i = 0; i < NUM_EPOD_ID; i++)
  1111. writeb(EPOD_STATE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB2 + i));
  1112. writeb(epod_state, (tcdm_base + PRCM_REQ_MB2 + epod_id));
  1113. writeb(MB2H_DPS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB2));
  1114. writel(MBOX_BIT(2), PRCM_MBOX_CPU_SET);
  1115. /*
  1116. * The current firmware version does not handle errors correctly,
  1117. * and we cannot recover if there is an error.
  1118. * This is expected to change when the firmware is updated.
  1119. */
  1120. if (!wait_for_completion_timeout(&mb2_transfer.work,
  1121. msecs_to_jiffies(20000))) {
  1122. pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
  1123. __func__);
  1124. r = -EIO;
  1125. goto unlock_and_return;
  1126. }
  1127. if (mb2_transfer.ack.status != HWACC_PWR_ST_OK)
  1128. r = -EIO;
  1129. unlock_and_return:
  1130. mutex_unlock(&mb2_transfer.lock);
  1131. return r;
  1132. }
  1133. /**
  1134. * prcmu_configure_auto_pm - Configure autonomous power management.
  1135. * @sleep: Configuration for ApSleep.
  1136. * @idle: Configuration for ApIdle.
  1137. */
  1138. void prcmu_configure_auto_pm(struct prcmu_auto_pm_config *sleep,
  1139. struct prcmu_auto_pm_config *idle)
  1140. {
  1141. u32 sleep_cfg;
  1142. u32 idle_cfg;
  1143. unsigned long flags;
  1144. BUG_ON((sleep == NULL) || (idle == NULL));
  1145. sleep_cfg = (sleep->sva_auto_pm_enable & 0xF);
  1146. sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_auto_pm_enable & 0xF));
  1147. sleep_cfg = ((sleep_cfg << 8) | (sleep->sva_power_on & 0xFF));
  1148. sleep_cfg = ((sleep_cfg << 8) | (sleep->sia_power_on & 0xFF));
  1149. sleep_cfg = ((sleep_cfg << 4) | (sleep->sva_policy & 0xF));
  1150. sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_policy & 0xF));
  1151. idle_cfg = (idle->sva_auto_pm_enable & 0xF);
  1152. idle_cfg = ((idle_cfg << 4) | (idle->sia_auto_pm_enable & 0xF));
  1153. idle_cfg = ((idle_cfg << 8) | (idle->sva_power_on & 0xFF));
  1154. idle_cfg = ((idle_cfg << 8) | (idle->sia_power_on & 0xFF));
  1155. idle_cfg = ((idle_cfg << 4) | (idle->sva_policy & 0xF));
  1156. idle_cfg = ((idle_cfg << 4) | (idle->sia_policy & 0xF));
  1157. spin_lock_irqsave(&mb2_transfer.auto_pm_lock, flags);
  1158. /*
  1159. * The autonomous power management configuration is done through
  1160. * fields in mailbox 2, but these fields are only used as shared
  1161. * variables - i.e. there is no need to send a message.
  1162. */
  1163. writel(sleep_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_SLEEP));
  1164. writel(idle_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_IDLE));
  1165. mb2_transfer.auto_pm_enabled =
  1166. ((sleep->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
  1167. (sleep->sia_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
  1168. (idle->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
  1169. (idle->sia_auto_pm_enable == PRCMU_AUTO_PM_ON));
  1170. spin_unlock_irqrestore(&mb2_transfer.auto_pm_lock, flags);
  1171. }
  1172. EXPORT_SYMBOL(prcmu_configure_auto_pm);
  1173. bool prcmu_is_auto_pm_enabled(void)
  1174. {
  1175. return mb2_transfer.auto_pm_enabled;
  1176. }
  1177. static int request_sysclk(bool enable)
  1178. {
  1179. int r;
  1180. unsigned long flags;
  1181. r = 0;
  1182. mutex_lock(&mb3_transfer.sysclk_lock);
  1183. spin_lock_irqsave(&mb3_transfer.lock, flags);
  1184. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(3))
  1185. cpu_relax();
  1186. writeb((enable ? ON : OFF), (tcdm_base + PRCM_REQ_MB3_SYSCLK_MGT));
  1187. writeb(MB3H_SYSCLK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB3));
  1188. writel(MBOX_BIT(3), PRCM_MBOX_CPU_SET);
  1189. spin_unlock_irqrestore(&mb3_transfer.lock, flags);
  1190. /*
  1191. * The firmware only sends an ACK if we want to enable the
  1192. * SysClk, and it succeeds.
  1193. */
  1194. if (enable && !wait_for_completion_timeout(&mb3_transfer.sysclk_work,
  1195. msecs_to_jiffies(20000))) {
  1196. pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
  1197. __func__);
  1198. r = -EIO;
  1199. }
  1200. mutex_unlock(&mb3_transfer.sysclk_lock);
  1201. return r;
  1202. }
  1203. static int request_timclk(bool enable)
  1204. {
  1205. u32 val = (PRCM_TCR_DOZE_MODE | PRCM_TCR_TENSEL_MASK);
  1206. if (!enable)
  1207. val |= PRCM_TCR_STOP_TIMERS;
  1208. writel(val, PRCM_TCR);
  1209. return 0;
  1210. }
  1211. static int request_clock(u8 clock, bool enable)
  1212. {
  1213. u32 val;
  1214. unsigned long flags;
  1215. spin_lock_irqsave(&clk_mgt_lock, flags);
  1216. /* Grab the HW semaphore. */
  1217. while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
  1218. cpu_relax();
  1219. val = readl(clk_mgt[clock].reg);
  1220. if (enable) {
  1221. val |= (PRCM_CLK_MGT_CLKEN | clk_mgt[clock].pllsw);
  1222. } else {
  1223. clk_mgt[clock].pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
  1224. val &= ~(PRCM_CLK_MGT_CLKEN | PRCM_CLK_MGT_CLKPLLSW_MASK);
  1225. }
  1226. writel(val, clk_mgt[clock].reg);
  1227. /* Release the HW semaphore. */
  1228. writel(0, PRCM_SEM);
  1229. spin_unlock_irqrestore(&clk_mgt_lock, flags);
  1230. return 0;
  1231. }
  1232. static int request_sga_clock(u8 clock, bool enable)
  1233. {
  1234. u32 val;
  1235. int ret;
  1236. if (enable) {
  1237. val = readl(PRCM_CGATING_BYPASS);
  1238. writel(val | PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
  1239. }
  1240. ret = request_clock(clock, enable);
  1241. if (!ret && !enable) {
  1242. val = readl(PRCM_CGATING_BYPASS);
  1243. writel(val & ~PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
  1244. }
  1245. return ret;
  1246. }
  1247. static inline bool plldsi_locked(void)
  1248. {
  1249. return (readl(PRCM_PLLDSI_LOCKP) &
  1250. (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
  1251. PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3)) ==
  1252. (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
  1253. PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3);
  1254. }
  1255. static int request_plldsi(bool enable)
  1256. {
  1257. int r = 0;
  1258. u32 val;
  1259. writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
  1260. PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI), (enable ?
  1261. PRCM_MMIP_LS_CLAMP_CLR : PRCM_MMIP_LS_CLAMP_SET));
  1262. val = readl(PRCM_PLLDSI_ENABLE);
  1263. if (enable)
  1264. val |= PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
  1265. else
  1266. val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
  1267. writel(val, PRCM_PLLDSI_ENABLE);
  1268. if (enable) {
  1269. unsigned int i;
  1270. bool locked = plldsi_locked();
  1271. for (i = 10; !locked && (i > 0); --i) {
  1272. udelay(100);
  1273. locked = plldsi_locked();
  1274. }
  1275. if (locked) {
  1276. writel(PRCM_APE_RESETN_DSIPLL_RESETN,
  1277. PRCM_APE_RESETN_SET);
  1278. } else {
  1279. writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
  1280. PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI),
  1281. PRCM_MMIP_LS_CLAMP_SET);
  1282. val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
  1283. writel(val, PRCM_PLLDSI_ENABLE);
  1284. r = -EAGAIN;
  1285. }
  1286. } else {
  1287. writel(PRCM_APE_RESETN_DSIPLL_RESETN, PRCM_APE_RESETN_CLR);
  1288. }
  1289. return r;
  1290. }
  1291. static int request_dsiclk(u8 n, bool enable)
  1292. {
  1293. u32 val;
  1294. val = readl(PRCM_DSI_PLLOUT_SEL);
  1295. val &= ~dsiclk[n].divsel_mask;
  1296. val |= ((enable ? dsiclk[n].divsel : PRCM_DSI_PLLOUT_SEL_OFF) <<
  1297. dsiclk[n].divsel_shift);
  1298. writel(val, PRCM_DSI_PLLOUT_SEL);
  1299. return 0;
  1300. }
  1301. static int request_dsiescclk(u8 n, bool enable)
  1302. {
  1303. u32 val;
  1304. val = readl(PRCM_DSITVCLK_DIV);
  1305. enable ? (val |= dsiescclk[n].en) : (val &= ~dsiescclk[n].en);
  1306. writel(val, PRCM_DSITVCLK_DIV);
  1307. return 0;
  1308. }
  1309. /**
  1310. * db8500_prcmu_request_clock() - Request for a clock to be enabled or disabled.
  1311. * @clock: The clock for which the request is made.
  1312. * @enable: Whether the clock should be enabled (true) or disabled (false).
  1313. *
  1314. * This function should only be used by the clock implementation.
  1315. * Do not use it from any other place!
  1316. */
  1317. int db8500_prcmu_request_clock(u8 clock, bool enable)
  1318. {
  1319. if (clock == PRCMU_SGACLK)
  1320. return request_sga_clock(clock, enable);
  1321. else if (clock < PRCMU_NUM_REG_CLOCKS)
  1322. return request_clock(clock, enable);
  1323. else if (clock == PRCMU_TIMCLK)
  1324. return request_timclk(enable);
  1325. else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
  1326. return request_dsiclk((clock - PRCMU_DSI0CLK), enable);
  1327. else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
  1328. return request_dsiescclk((clock - PRCMU_DSI0ESCCLK), enable);
  1329. else if (clock == PRCMU_PLLDSI)
  1330. return request_plldsi(enable);
  1331. else if (clock == PRCMU_SYSCLK)
  1332. return request_sysclk(enable);
  1333. else if ((clock == PRCMU_PLLSOC0) || (clock == PRCMU_PLLSOC1))
  1334. return request_pll(clock, enable);
  1335. else
  1336. return -EINVAL;
  1337. }
  1338. static unsigned long pll_rate(void __iomem *reg, unsigned long src_rate,
  1339. int branch)
  1340. {
  1341. u64 rate;
  1342. u32 val;
  1343. u32 d;
  1344. u32 div = 1;
  1345. val = readl(reg);
  1346. rate = src_rate;
  1347. rate *= ((val & PRCM_PLL_FREQ_D_MASK) >> PRCM_PLL_FREQ_D_SHIFT);
  1348. d = ((val & PRCM_PLL_FREQ_N_MASK) >> PRCM_PLL_FREQ_N_SHIFT);
  1349. if (d > 1)
  1350. div *= d;
  1351. d = ((val & PRCM_PLL_FREQ_R_MASK) >> PRCM_PLL_FREQ_R_SHIFT);
  1352. if (d > 1)
  1353. div *= d;
  1354. if (val & PRCM_PLL_FREQ_SELDIV2)
  1355. div *= 2;
  1356. if ((branch == PLL_FIX) || ((branch == PLL_DIV) &&
  1357. (val & PRCM_PLL_FREQ_DIV2EN) &&
  1358. ((reg == PRCM_PLLSOC0_FREQ) ||
  1359. (reg == PRCM_PLLDDR_FREQ))))
  1360. div *= 2;
  1361. (void)do_div(rate, div);
  1362. return (unsigned long)rate;
  1363. }
  1364. #define ROOT_CLOCK_RATE 38400000
  1365. static unsigned long clock_rate(u8 clock)
  1366. {
  1367. u32 val;
  1368. u32 pllsw;
  1369. unsigned long rate = ROOT_CLOCK_RATE;
  1370. val = readl(clk_mgt[clock].reg);
  1371. if (val & PRCM_CLK_MGT_CLK38) {
  1372. if (clk_mgt[clock].clk38div && (val & PRCM_CLK_MGT_CLK38DIV))
  1373. rate /= 2;
  1374. return rate;
  1375. }
  1376. val |= clk_mgt[clock].pllsw;
  1377. pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
  1378. if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC0)
  1379. rate = pll_rate(PRCM_PLLSOC0_FREQ, rate, clk_mgt[clock].branch);
  1380. else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC1)
  1381. rate = pll_rate(PRCM_PLLSOC1_FREQ, rate, clk_mgt[clock].branch);
  1382. else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_DDR)
  1383. rate = pll_rate(PRCM_PLLDDR_FREQ, rate, clk_mgt[clock].branch);
  1384. else
  1385. return 0;
  1386. if ((clock == PRCMU_SGACLK) &&
  1387. (val & PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN)) {
  1388. u64 r = (rate * 10);
  1389. (void)do_div(r, 25);
  1390. return (unsigned long)r;
  1391. }
  1392. val &= PRCM_CLK_MGT_CLKPLLDIV_MASK;
  1393. if (val)
  1394. return rate / val;
  1395. else
  1396. return 0;
  1397. }
  1398. static unsigned long dsiclk_rate(u8 n)
  1399. {
  1400. u32 divsel;
  1401. u32 div = 1;
  1402. divsel = readl(PRCM_DSI_PLLOUT_SEL);
  1403. divsel = ((divsel & dsiclk[n].divsel_mask) >> dsiclk[n].divsel_shift);
  1404. if (divsel == PRCM_DSI_PLLOUT_SEL_OFF)
  1405. divsel = dsiclk[n].divsel;
  1406. switch (divsel) {
  1407. case PRCM_DSI_PLLOUT_SEL_PHI_4:
  1408. div *= 2;
  1409. case PRCM_DSI_PLLOUT_SEL_PHI_2:
  1410. div *= 2;
  1411. case PRCM_DSI_PLLOUT_SEL_PHI:
  1412. return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
  1413. PLL_RAW) / div;
  1414. default:
  1415. return 0;
  1416. }
  1417. }
  1418. static unsigned long dsiescclk_rate(u8 n)
  1419. {
  1420. u32 div;
  1421. div = readl(PRCM_DSITVCLK_DIV);
  1422. div = ((div & dsiescclk[n].div_mask) >> (dsiescclk[n].div_shift));
  1423. return clock_rate(PRCMU_TVCLK) / max((u32)1, div);
  1424. }
  1425. unsigned long prcmu_clock_rate(u8 clock)
  1426. {
  1427. if (clock < PRCMU_NUM_REG_CLOCKS)
  1428. return clock_rate(clock);
  1429. else if (clock == PRCMU_TIMCLK)
  1430. return ROOT_CLOCK_RATE / 16;
  1431. else if (clock == PRCMU_SYSCLK)
  1432. return ROOT_CLOCK_RATE;
  1433. else if (clock == PRCMU_PLLSOC0)
  1434. return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
  1435. else if (clock == PRCMU_PLLSOC1)
  1436. return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
  1437. else if (clock == PRCMU_PLLDDR)
  1438. return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
  1439. else if (clock == PRCMU_PLLDSI)
  1440. return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
  1441. PLL_RAW);
  1442. else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
  1443. return dsiclk_rate(clock - PRCMU_DSI0CLK);
  1444. else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
  1445. return dsiescclk_rate(clock - PRCMU_DSI0ESCCLK);
  1446. else
  1447. return 0;
  1448. }
  1449. static unsigned long clock_source_rate(u32 clk_mgt_val, int branch)
  1450. {
  1451. if (clk_mgt_val & PRCM_CLK_MGT_CLK38)
  1452. return ROOT_CLOCK_RATE;
  1453. clk_mgt_val &= PRCM_CLK_MGT_CLKPLLSW_MASK;
  1454. if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC0)
  1455. return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, branch);
  1456. else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC1)
  1457. return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, branch);
  1458. else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_DDR)
  1459. return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, branch);
  1460. else
  1461. return 0;
  1462. }
  1463. static u32 clock_divider(unsigned long src_rate, unsigned long rate)
  1464. {
  1465. u32 div;
  1466. div = (src_rate / rate);
  1467. if (div == 0)
  1468. return 1;
  1469. if (rate < (src_rate / div))
  1470. div++;
  1471. return div;
  1472. }
  1473. static long round_clock_rate(u8 clock, unsigned long rate)
  1474. {
  1475. u32 val;
  1476. u32 div;
  1477. unsigned long src_rate;
  1478. long rounded_rate;
  1479. val = readl(clk_mgt[clock].reg);
  1480. src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
  1481. clk_mgt[clock].branch);
  1482. div = clock_divider(src_rate, rate);
  1483. if (val & PRCM_CLK_MGT_CLK38) {
  1484. if (clk_mgt[clock].clk38div) {
  1485. if (div > 2)
  1486. div = 2;
  1487. } else {
  1488. div = 1;
  1489. }
  1490. } else if ((clock == PRCMU_SGACLK) && (div == 3)) {
  1491. u64 r = (src_rate * 10);
  1492. (void)do_div(r, 25);
  1493. if (r <= rate)
  1494. return (unsigned long)r;
  1495. }
  1496. rounded_rate = (src_rate / min(div, (u32)31));
  1497. return rounded_rate;
  1498. }
  1499. #define MIN_PLL_VCO_RATE 600000000ULL
  1500. #define MAX_PLL_VCO_RATE 1680640000ULL
  1501. static long round_plldsi_rate(unsigned long rate)
  1502. {
  1503. long rounded_rate = 0;
  1504. unsigned long src_rate;
  1505. unsigned long rem;
  1506. u32 r;
  1507. src_rate = clock_rate(PRCMU_HDMICLK);
  1508. rem = rate;
  1509. for (r = 7; (rem > 0) && (r > 0); r--) {
  1510. u64 d;
  1511. d = (r * rate);
  1512. (void)do_div(d, src_rate);
  1513. if (d < 6)
  1514. d = 6;
  1515. else if (d > 255)
  1516. d = 255;
  1517. d *= src_rate;
  1518. if (((2 * d) < (r * MIN_PLL_VCO_RATE)) ||
  1519. ((r * MAX_PLL_VCO_RATE) < (2 * d)))
  1520. continue;
  1521. (void)do_div(d, r);
  1522. if (rate < d) {
  1523. if (rounded_rate == 0)
  1524. rounded_rate = (long)d;
  1525. break;
  1526. }
  1527. if ((rate - d) < rem) {
  1528. rem = (rate - d);
  1529. rounded_rate = (long)d;
  1530. }
  1531. }
  1532. return rounded_rate;
  1533. }
  1534. static long round_dsiclk_rate(unsigned long rate)
  1535. {
  1536. u32 div;
  1537. unsigned long src_rate;
  1538. long rounded_rate;
  1539. src_rate = pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
  1540. PLL_RAW);
  1541. div = clock_divider(src_rate, rate);
  1542. rounded_rate = (src_rate / ((div > 2) ? 4 : div));
  1543. return rounded_rate;
  1544. }
  1545. static long round_dsiescclk_rate(unsigned long rate)
  1546. {
  1547. u32 div;
  1548. unsigned long src_rate;
  1549. long rounded_rate;
  1550. src_rate = clock_rate(PRCMU_TVCLK);
  1551. div = clock_divider(src_rate, rate);
  1552. rounded_rate = (src_rate / min(div, (u32)255));
  1553. return rounded_rate;
  1554. }
  1555. long prcmu_round_clock_rate(u8 clock, unsigned long rate)
  1556. {
  1557. if (clock < PRCMU_NUM_REG_CLOCKS)
  1558. return round_clock_rate(clock, rate);
  1559. else if (clock == PRCMU_PLLDSI)
  1560. return round_plldsi_rate(rate);
  1561. else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
  1562. return round_dsiclk_rate(rate);
  1563. else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
  1564. return round_dsiescclk_rate(rate);
  1565. else
  1566. return (long)prcmu_clock_rate(clock);
  1567. }
  1568. static void set_clock_rate(u8 clock, unsigned long rate)
  1569. {
  1570. u32 val;
  1571. u32 div;
  1572. unsigned long src_rate;
  1573. unsigned long flags;
  1574. spin_lock_irqsave(&clk_mgt_lock, flags);
  1575. /* Grab the HW semaphore. */
  1576. while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
  1577. cpu_relax();
  1578. val = readl(clk_mgt[clock].reg);
  1579. src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
  1580. clk_mgt[clock].branch);
  1581. div = clock_divider(src_rate, rate);
  1582. if (val & PRCM_CLK_MGT_CLK38) {
  1583. if (clk_mgt[clock].clk38div) {
  1584. if (div > 1)
  1585. val |= PRCM_CLK_MGT_CLK38DIV;
  1586. else
  1587. val &= ~PRCM_CLK_MGT_CLK38DIV;
  1588. }
  1589. } else if (clock == PRCMU_SGACLK) {
  1590. val &= ~(PRCM_CLK_MGT_CLKPLLDIV_MASK |
  1591. PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN);
  1592. if (div == 3) {
  1593. u64 r = (src_rate * 10);
  1594. (void)do_div(r, 25);
  1595. if (r <= rate) {
  1596. val |= PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN;
  1597. div = 0;
  1598. }
  1599. }
  1600. val |= min(div, (u32)31);
  1601. } else {
  1602. val &= ~PRCM_CLK_MGT_CLKPLLDIV_MASK;
  1603. val |= min(div, (u32)31);
  1604. }
  1605. writel(val, clk_mgt[clock].reg);
  1606. /* Release the HW semaphore. */
  1607. writel(0, PRCM_SEM);
  1608. spin_unlock_irqrestore(&clk_mgt_lock, flags);
  1609. }
  1610. static int set_plldsi_rate(unsigned long rate)
  1611. {
  1612. unsigned long src_rate;
  1613. unsigned long rem;
  1614. u32 pll_freq = 0;
  1615. u32 r;
  1616. src_rate = clock_rate(PRCMU_HDMICLK);
  1617. rem = rate;
  1618. for (r = 7; (rem > 0) && (r > 0); r--) {
  1619. u64 d;
  1620. u64 hwrate;
  1621. d = (r * rate);
  1622. (void)do_div(d, src_rate);
  1623. if (d < 6)
  1624. d = 6;
  1625. else if (d > 255)
  1626. d = 255;
  1627. hwrate = (d * src_rate);
  1628. if (((2 * hwrate) < (r * MIN_PLL_VCO_RATE)) ||
  1629. ((r * MAX_PLL_VCO_RATE) < (2 * hwrate)))
  1630. continue;
  1631. (void)do_div(hwrate, r);
  1632. if (rate < hwrate) {
  1633. if (pll_freq == 0)
  1634. pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
  1635. (r << PRCM_PLL_FREQ_R_SHIFT));
  1636. break;
  1637. }
  1638. if ((rate - hwrate) < rem) {
  1639. rem = (rate - hwrate);
  1640. pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
  1641. (r << PRCM_PLL_FREQ_R_SHIFT));
  1642. }
  1643. }
  1644. if (pll_freq == 0)
  1645. return -EINVAL;
  1646. pll_freq |= (1 << PRCM_PLL_FREQ_N_SHIFT);
  1647. writel(pll_freq, PRCM_PLLDSI_FREQ);
  1648. return 0;
  1649. }
  1650. static void set_dsiclk_rate(u8 n, unsigned long rate)
  1651. {
  1652. u32 val;
  1653. u32 div;
  1654. div = clock_divider(pll_rate(PRCM_PLLDSI_FREQ,
  1655. clock_rate(PRCMU_HDMICLK), PLL_RAW), rate);
  1656. dsiclk[n].divsel = (div == 1) ? PRCM_DSI_PLLOUT_SEL_PHI :
  1657. (div == 2) ? PRCM_DSI_PLLOUT_SEL_PHI_2 :
  1658. /* else */ PRCM_DSI_PLLOUT_SEL_PHI_4;
  1659. val = readl(PRCM_DSI_PLLOUT_SEL);
  1660. val &= ~dsiclk[n].divsel_mask;
  1661. val |= (dsiclk[n].divsel << dsiclk[n].divsel_shift);
  1662. writel(val, PRCM_DSI_PLLOUT_SEL);
  1663. }
  1664. static void set_dsiescclk_rate(u8 n, unsigned long rate)
  1665. {
  1666. u32 val;
  1667. u32 div;
  1668. div = clock_divider(clock_rate(PRCMU_TVCLK), rate);
  1669. val = readl(PRCM_DSITVCLK_DIV);
  1670. val &= ~dsiescclk[n].div_mask;
  1671. val |= (min(div, (u32)255) << dsiescclk[n].div_shift);
  1672. writel(val, PRCM_DSITVCLK_DIV);
  1673. }
  1674. int prcmu_set_clock_rate(u8 clock, unsigned long rate)
  1675. {
  1676. if (clock < PRCMU_NUM_REG_CLOCKS)
  1677. set_clock_rate(clock, rate);
  1678. else if (clock == PRCMU_PLLDSI)
  1679. return set_plldsi_rate(rate);
  1680. else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
  1681. set_dsiclk_rate((clock - PRCMU_DSI0CLK), rate);
  1682. else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
  1683. set_dsiescclk_rate((clock - PRCMU_DSI0ESCCLK), rate);
  1684. return 0;
  1685. }
  1686. int db8500_prcmu_config_esram0_deep_sleep(u8 state)
  1687. {
  1688. if ((state > ESRAM0_DEEP_SLEEP_STATE_RET) ||
  1689. (state < ESRAM0_DEEP_SLEEP_STATE_OFF))
  1690. return -EINVAL;
  1691. mutex_lock(&mb4_transfer.lock);
  1692. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
  1693. cpu_relax();
  1694. writeb(MB4H_MEM_ST, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
  1695. writeb(((DDR_PWR_STATE_OFFHIGHLAT << 4) | DDR_PWR_STATE_ON),
  1696. (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE));
  1697. writeb(DDR_PWR_STATE_ON,
  1698. (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE));
  1699. writeb(state, (tcdm_base + PRCM_REQ_MB4_ESRAM0_ST));
  1700. writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
  1701. wait_for_completion(&mb4_transfer.work);
  1702. mutex_unlock(&mb4_transfer.lock);
  1703. return 0;
  1704. }
  1705. int db8500_prcmu_config_hotdog(u8 threshold)
  1706. {
  1707. mutex_lock(&mb4_transfer.lock);
  1708. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
  1709. cpu_relax();
  1710. writeb(threshold, (tcdm_base + PRCM_REQ_MB4_HOTDOG_THRESHOLD));
  1711. writeb(MB4H_HOTDOG, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
  1712. writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
  1713. wait_for_completion(&mb4_transfer.work);
  1714. mutex_unlock(&mb4_transfer.lock);
  1715. return 0;
  1716. }
  1717. int db8500_prcmu_config_hotmon(u8 low, u8 high)
  1718. {
  1719. mutex_lock(&mb4_transfer.lock);
  1720. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
  1721. cpu_relax();
  1722. writeb(low, (tcdm_base + PRCM_REQ_MB4_HOTMON_LOW));
  1723. writeb(high, (tcdm_base + PRCM_REQ_MB4_HOTMON_HIGH));
  1724. writeb((HOTMON_CONFIG_LOW | HOTMON_CONFIG_HIGH),
  1725. (tcdm_base + PRCM_REQ_MB4_HOTMON_CONFIG));
  1726. writeb(MB4H_HOTMON, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
  1727. writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
  1728. wait_for_completion(&mb4_transfer.work);
  1729. mutex_unlock(&mb4_transfer.lock);
  1730. return 0;
  1731. }
  1732. static int config_hot_period(u16 val)
  1733. {
  1734. mutex_lock(&mb4_transfer.lock);
  1735. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
  1736. cpu_relax();
  1737. writew(val, (tcdm_base + PRCM_REQ_MB4_HOT_PERIOD));
  1738. writeb(MB4H_HOT_PERIOD, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
  1739. writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
  1740. wait_for_completion(&mb4_transfer.work);
  1741. mutex_unlock(&mb4_transfer.lock);
  1742. return 0;
  1743. }
  1744. int db8500_prcmu_start_temp_sense(u16 cycles32k)
  1745. {
  1746. if (cycles32k == 0xFFFF)
  1747. return -EINVAL;
  1748. return config_hot_period(cycles32k);
  1749. }
  1750. int db8500_prcmu_stop_temp_sense(void)
  1751. {
  1752. return config_hot_period(0xFFFF);
  1753. }
  1754. static int prcmu_a9wdog(u8 cmd, u8 d0, u8 d1, u8 d2, u8 d3)
  1755. {
  1756. mutex_lock(&mb4_transfer.lock);
  1757. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
  1758. cpu_relax();
  1759. writeb(d0, (tcdm_base + PRCM_REQ_MB4_A9WDOG_0));
  1760. writeb(d1, (tcdm_base + PRCM_REQ_MB4_A9WDOG_1));
  1761. writeb(d2, (tcdm_base + PRCM_REQ_MB4_A9WDOG_2));
  1762. writeb(d3, (tcdm_base + PRCM_REQ_MB4_A9WDOG_3));
  1763. writeb(cmd, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
  1764. writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
  1765. wait_for_completion(&mb4_transfer.work);
  1766. mutex_unlock(&mb4_transfer.lock);
  1767. return 0;
  1768. }
  1769. int db8500_prcmu_config_a9wdog(u8 num, bool sleep_auto_off)
  1770. {
  1771. BUG_ON(num == 0 || num > 0xf);
  1772. return prcmu_a9wdog(MB4H_A9WDOG_CONF, num, 0, 0,
  1773. sleep_auto_off ? A9WDOG_AUTO_OFF_EN :
  1774. A9WDOG_AUTO_OFF_DIS);
  1775. }
  1776. int db8500_prcmu_enable_a9wdog(u8 id)
  1777. {
  1778. return prcmu_a9wdog(MB4H_A9WDOG_EN, id, 0, 0, 0);
  1779. }
  1780. int db8500_prcmu_disable_a9wdog(u8 id)
  1781. {
  1782. return prcmu_a9wdog(MB4H_A9WDOG_DIS, id, 0, 0, 0);
  1783. }
  1784. int db8500_prcmu_kick_a9wdog(u8 id)
  1785. {
  1786. return prcmu_a9wdog(MB4H_A9WDOG_KICK, id, 0, 0, 0);
  1787. }
  1788. /*
  1789. * timeout is 28 bit, in ms.
  1790. */
  1791. int db8500_prcmu_load_a9wdog(u8 id, u32 timeout)
  1792. {
  1793. return prcmu_a9wdog(MB4H_A9WDOG_LOAD,
  1794. (id & A9WDOG_ID_MASK) |
  1795. /*
  1796. * Put the lowest 28 bits of timeout at
  1797. * offset 4. Four first bits are used for id.
  1798. */
  1799. (u8)((timeout << 4) & 0xf0),
  1800. (u8)((timeout >> 4) & 0xff),
  1801. (u8)((timeout >> 12) & 0xff),
  1802. (u8)((timeout >> 20) & 0xff));
  1803. }
  1804. /**
  1805. * prcmu_abb_read() - Read register value(s) from the ABB.
  1806. * @slave: The I2C slave address.
  1807. * @reg: The (start) register address.
  1808. * @value: The read out value(s).
  1809. * @size: The number of registers to read.
  1810. *
  1811. * Reads register value(s) from the ABB.
  1812. * @size has to be 1 for the current firmware version.
  1813. */
  1814. int prcmu_abb_read(u8 slave, u8 reg, u8 *value, u8 size)
  1815. {
  1816. int r;
  1817. if (size != 1)
  1818. return -EINVAL;
  1819. mutex_lock(&mb5_transfer.lock);
  1820. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
  1821. cpu_relax();
  1822. writeb(0, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB5));
  1823. writeb(PRCMU_I2C_READ(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
  1824. writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
  1825. writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
  1826. writeb(0, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
  1827. writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
  1828. if (!wait_for_completion_timeout(&mb5_transfer.work,
  1829. msecs_to_jiffies(20000))) {
  1830. pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
  1831. __func__);
  1832. r = -EIO;
  1833. } else {
  1834. r = ((mb5_transfer.ack.status == I2C_RD_OK) ? 0 : -EIO);
  1835. }
  1836. if (!r)
  1837. *value = mb5_transfer.ack.value;
  1838. mutex_unlock(&mb5_transfer.lock);
  1839. return r;
  1840. }
  1841. /**
  1842. * prcmu_abb_write_masked() - Write masked register value(s) to the ABB.
  1843. * @slave: The I2C slave address.
  1844. * @reg: The (start) register address.
  1845. * @value: The value(s) to write.
  1846. * @mask: The mask(s) to use.
  1847. * @size: The number of registers to write.
  1848. *
  1849. * Writes masked register value(s) to the ABB.
  1850. * For each @value, only the bits set to 1 in the corresponding @mask
  1851. * will be written. The other bits are not changed.
  1852. * @size has to be 1 for the current firmware version.
  1853. */
  1854. int prcmu_abb_write_masked(u8 slave, u8 reg, u8 *value, u8 *mask, u8 size)
  1855. {
  1856. int r;
  1857. if (size != 1)
  1858. return -EINVAL;
  1859. mutex_lock(&mb5_transfer.lock);
  1860. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
  1861. cpu_relax();
  1862. writeb(~*mask, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB5));
  1863. writeb(PRCMU_I2C_WRITE(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
  1864. writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
  1865. writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
  1866. writeb(*value, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
  1867. writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
  1868. if (!wait_for_completion_timeout(&mb5_transfer.work,
  1869. msecs_to_jiffies(20000))) {
  1870. pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
  1871. __func__);
  1872. r = -EIO;
  1873. } else {
  1874. r = ((mb5_transfer.ack.status == I2C_WR_OK) ? 0 : -EIO);
  1875. }
  1876. mutex_unlock(&mb5_transfer.lock);
  1877. return r;
  1878. }
  1879. /**
  1880. * prcmu_abb_write() - Write register value(s) to the ABB.
  1881. * @slave: The I2C slave address.
  1882. * @reg: The (start) register address.
  1883. * @value: The value(s) to write.
  1884. * @size: The number of registers to write.
  1885. *
  1886. * Writes register value(s) to the ABB.
  1887. * @size has to be 1 for the current firmware version.
  1888. */
  1889. int prcmu_abb_write(u8 slave, u8 reg, u8 *value, u8 size)
  1890. {
  1891. u8 mask = ~0;
  1892. return prcmu_abb_write_masked(slave, reg, value, &mask, size);
  1893. }
  1894. /**
  1895. * prcmu_ac_wake_req - should be called whenever ARM wants to wakeup Modem
  1896. */
  1897. void prcmu_ac_wake_req(void)
  1898. {
  1899. u32 val;
  1900. u32 status;
  1901. mutex_lock(&mb0_transfer.ac_wake_lock);
  1902. val = readl(PRCM_HOSTACCESS_REQ);
  1903. if (val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ)
  1904. goto unlock_and_return;
  1905. atomic_set(&ac_wake_req_state, 1);
  1906. retry:
  1907. writel((val | PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ), PRCM_HOSTACCESS_REQ);
  1908. if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
  1909. msecs_to_jiffies(5000))) {
  1910. pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
  1911. __func__);
  1912. goto unlock_and_return;
  1913. }
  1914. /*
  1915. * The modem can generate an AC_WAKE_ACK, and then still go to sleep.
  1916. * As a workaround, we wait, and then check that the modem is indeed
  1917. * awake (in terms of the value of the PRCM_MOD_AWAKE_STATUS
  1918. * register, which may not be the whole truth).
  1919. */
  1920. udelay(400);
  1921. status = (readl(PRCM_MOD_AWAKE_STATUS) & BITS(0, 2));
  1922. if (status != (PRCM_MOD_AWAKE_STATUS_PRCM_MOD_AAPD_AWAKE |
  1923. PRCM_MOD_AWAKE_STATUS_PRCM_MOD_COREPD_AWAKE)) {
  1924. pr_err("prcmu: %s received ack, but modem not awake (0x%X).\n",
  1925. __func__, status);
  1926. udelay(1200);
  1927. writel(val, PRCM_HOSTACCESS_REQ);
  1928. if (wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
  1929. msecs_to_jiffies(5000)))
  1930. goto retry;
  1931. pr_crit("prcmu: %s timed out (5 s) waiting for AC_SLEEP_ACK.\n",
  1932. __func__);
  1933. }
  1934. unlock_and_return:
  1935. mutex_unlock(&mb0_transfer.ac_wake_lock);
  1936. }
  1937. /**
  1938. * prcmu_ac_sleep_req - called when ARM no longer needs to talk to modem
  1939. */
  1940. void prcmu_ac_sleep_req()
  1941. {
  1942. u32 val;
  1943. mutex_lock(&mb0_transfer.ac_wake_lock);
  1944. val = readl(PRCM_HOSTACCESS_REQ);
  1945. if (!(val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ))
  1946. goto unlock_and_return;
  1947. writel((val & ~PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ),
  1948. PRCM_HOSTACCESS_REQ);
  1949. if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
  1950. msecs_to_jiffies(5000))) {
  1951. pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
  1952. __func__);
  1953. }
  1954. atomic_set(&ac_wake_req_state, 0);
  1955. unlock_and_return:
  1956. mutex_unlock(&mb0_transfer.ac_wake_lock);
  1957. }
  1958. bool db8500_prcmu_is_ac_wake_requested(void)
  1959. {
  1960. return (atomic_read(&ac_wake_req_state) != 0);
  1961. }
  1962. /**
  1963. * db8500_prcmu_system_reset - System reset
  1964. *
  1965. * Saves the reset reason code and then sets the APE_SOFTRST register which
  1966. * fires interrupt to fw
  1967. */
  1968. void db8500_prcmu_system_reset(u16 reset_code)
  1969. {
  1970. writew(reset_code, (tcdm_base + PRCM_SW_RST_REASON));
  1971. writel(1, PRCM_APE_SOFTRST);
  1972. }
  1973. /**
  1974. * db8500_prcmu_get_reset_code - Retrieve SW reset reason code
  1975. *
  1976. * Retrieves the reset reason code stored by prcmu_system_reset() before
  1977. * last restart.
  1978. */
  1979. u16 db8500_prcmu_get_reset_code(void)
  1980. {
  1981. return readw(tcdm_base + PRCM_SW_RST_REASON);
  1982. }
  1983. /**
  1984. * db8500_prcmu_reset_modem - ask the PRCMU to reset modem
  1985. */
  1986. void db8500_prcmu_modem_reset(void)
  1987. {
  1988. mutex_lock(&mb1_transfer.lock);
  1989. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
  1990. cpu_relax();
  1991. writeb(MB1H_RESET_MODEM, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
  1992. writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
  1993. wait_for_completion(&mb1_transfer.work);
  1994. /*
  1995. * No need to check return from PRCMU as modem should go in reset state
  1996. * This state is already managed by upper layer
  1997. */
  1998. mutex_unlock(&mb1_transfer.lock);
  1999. }
  2000. static void ack_dbb_wakeup(void)
  2001. {
  2002. unsigned long flags;
  2003. spin_lock_irqsave(&mb0_transfer.lock, flags);
  2004. while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
  2005. cpu_relax();
  2006. writeb(MB0H_READ_WAKEUP_ACK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
  2007. writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
  2008. spin_unlock_irqrestore(&mb0_transfer.lock, flags);
  2009. }
  2010. static inline void print_unknown_header_warning(u8 n, u8 header)
  2011. {
  2012. pr_warning("prcmu: Unknown message header (%d) in mailbox %d.\n",
  2013. header, n);
  2014. }
  2015. static bool read_mailbox_0(void)
  2016. {
  2017. bool r;
  2018. u32 ev;
  2019. unsigned int n;
  2020. u8 header;
  2021. header = readb(tcdm_base + PRCM_MBOX_HEADER_ACK_MB0);
  2022. switch (header) {
  2023. case MB0H_WAKEUP_EXE:
  2024. case MB0H_WAKEUP_SLEEP:
  2025. if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
  2026. ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_1_8500);
  2027. else
  2028. ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_0_8500);
  2029. if (ev & (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK))
  2030. complete(&mb0_transfer.ac_wake_work);
  2031. if (ev & WAKEUP_BIT_SYSCLK_OK)
  2032. complete(&mb3_transfer.sysclk_work);
  2033. ev &= mb0_transfer.req.dbb_irqs;
  2034. for (n = 0; n < NUM_PRCMU_WAKEUPS; n++) {
  2035. if (ev & prcmu_irq_bit[n])
  2036. generic_handle_irq(IRQ_PRCMU_BASE + n);
  2037. }
  2038. r = true;
  2039. break;
  2040. default:
  2041. print_unknown_header_warning(0, header);
  2042. r = false;
  2043. break;
  2044. }
  2045. writel(MBOX_BIT(0), PRCM_ARM_IT1_CLR);
  2046. return r;
  2047. }
  2048. static bool read_mailbox_1(void)
  2049. {
  2050. mb1_transfer.ack.header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB1);
  2051. mb1_transfer.ack.arm_opp = readb(tcdm_base +
  2052. PRCM_ACK_MB1_CURRENT_ARM_OPP);
  2053. mb1_transfer.ack.ape_opp = readb(tcdm_base +
  2054. PRCM_ACK_MB1_CURRENT_APE_OPP);
  2055. mb1_transfer.ack.ape_voltage_status = readb(tcdm_base +
  2056. PRCM_ACK_MB1_APE_VOLTAGE_STATUS);
  2057. writel(MBOX_BIT(1), PRCM_ARM_IT1_CLR);
  2058. complete(&mb1_transfer.work);
  2059. return false;
  2060. }
  2061. static bool read_mailbox_2(void)
  2062. {
  2063. mb2_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB2_DPS_STATUS);
  2064. writel(MBOX_BIT(2), PRCM_ARM_IT1_CLR);
  2065. complete(&mb2_transfer.work);
  2066. return false;
  2067. }
  2068. static bool read_mailbox_3(void)
  2069. {
  2070. writel(MBOX_BIT(3), PRCM_ARM_IT1_CLR);
  2071. return false;
  2072. }
  2073. static bool read_mailbox_4(void)
  2074. {
  2075. u8 header;
  2076. bool do_complete = true;
  2077. header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB4);
  2078. switch (header) {
  2079. case MB4H_MEM_ST:
  2080. case MB4H_HOTDOG:
  2081. case MB4H_HOTMON:
  2082. case MB4H_HOT_PERIOD:
  2083. case MB4H_A9WDOG_CONF:
  2084. case MB4H_A9WDOG_EN:
  2085. case MB4H_A9WDOG_DIS:
  2086. case MB4H_A9WDOG_LOAD:
  2087. case MB4H_A9WDOG_KICK:
  2088. break;
  2089. default:
  2090. print_unknown_header_warning(4, header);
  2091. do_complete = false;
  2092. break;
  2093. }
  2094. writel(MBOX_BIT(4), PRCM_ARM_IT1_CLR);
  2095. if (do_complete)
  2096. complete(&mb4_transfer.work);
  2097. return false;
  2098. }
  2099. static bool read_mailbox_5(void)
  2100. {
  2101. mb5_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB5_I2C_STATUS);
  2102. mb5_transfer.ack.value = readb(tcdm_base + PRCM_ACK_MB5_I2C_VAL);
  2103. writel(MBOX_BIT(5), PRCM_ARM_IT1_CLR);
  2104. complete(&mb5_transfer.work);
  2105. return false;
  2106. }
  2107. static bool read_mailbox_6(void)
  2108. {
  2109. writel(MBOX_BIT(6), PRCM_ARM_IT1_CLR);
  2110. return false;
  2111. }
  2112. static bool read_mailbox_7(void)
  2113. {
  2114. writel(MBOX_BIT(7), PRCM_ARM_IT1_CLR);
  2115. return false;
  2116. }
  2117. static bool (* const read_mailbox[NUM_MB])(void) = {
  2118. read_mailbox_0,
  2119. read_mailbox_1,
  2120. read_mailbox_2,
  2121. read_mailbox_3,
  2122. read_mailbox_4,
  2123. read_mailbox_5,
  2124. read_mailbox_6,
  2125. read_mailbox_7
  2126. };
  2127. static irqreturn_t prcmu_irq_handler(int irq, void *data)
  2128. {
  2129. u32 bits;
  2130. u8 n;
  2131. irqreturn_t r;
  2132. bits = (readl(PRCM_ARM_IT1_VAL) & ALL_MBOX_BITS);
  2133. if (unlikely(!bits))
  2134. return IRQ_NONE;
  2135. r = IRQ_HANDLED;
  2136. for (n = 0; bits; n++) {
  2137. if (bits & MBOX_BIT(n)) {
  2138. bits -= MBOX_BIT(n);
  2139. if (read_mailbox[n]())
  2140. r = IRQ_WAKE_THREAD;
  2141. }
  2142. }
  2143. return r;
  2144. }
  2145. static irqreturn_t prcmu_irq_thread_fn(int irq, void *data)
  2146. {
  2147. ack_dbb_wakeup();
  2148. return IRQ_HANDLED;
  2149. }
  2150. static void prcmu_mask_work(struct work_struct *work)
  2151. {
  2152. unsigned long flags;
  2153. spin_lock_irqsave(&mb0_transfer.lock, flags);
  2154. config_wakeups();
  2155. spin_unlock_irqrestore(&mb0_transfer.lock, flags);
  2156. }
  2157. static void prcmu_irq_mask(struct irq_data *d)
  2158. {
  2159. unsigned long flags;
  2160. spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
  2161. mb0_transfer.req.dbb_irqs &= ~prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
  2162. spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
  2163. if (d->irq != IRQ_PRCMU_CA_SLEEP)
  2164. schedule_work(&mb0_transfer.mask_work);
  2165. }
  2166. static void prcmu_irq_unmask(struct irq_data *d)
  2167. {
  2168. unsigned long flags;
  2169. spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
  2170. mb0_transfer.req.dbb_irqs |= prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
  2171. spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
  2172. if (d->irq != IRQ_PRCMU_CA_SLEEP)
  2173. schedule_work(&mb0_transfer.mask_work);
  2174. }
  2175. static void noop(struct irq_data *d)
  2176. {
  2177. }
  2178. static struct irq_chip prcmu_irq_chip = {
  2179. .name = "prcmu",
  2180. .irq_disable = prcmu_irq_mask,
  2181. .irq_ack = noop,
  2182. .irq_mask = prcmu_irq_mask,
  2183. .irq_unmask = prcmu_irq_unmask,
  2184. };
  2185. static char *fw_project_name(u8 project)
  2186. {
  2187. switch (project) {
  2188. case PRCMU_FW_PROJECT_U8500:
  2189. return "U8500";
  2190. case PRCMU_FW_PROJECT_U8500_C2:
  2191. return "U8500 C2";
  2192. case PRCMU_FW_PROJECT_U9500:
  2193. return "U9500";
  2194. case PRCMU_FW_PROJECT_U9500_C2:
  2195. return "U9500 C2";
  2196. case PRCMU_FW_PROJECT_U8520:
  2197. return "U8520";
  2198. case PRCMU_FW_PROJECT_U8420:
  2199. return "U8420";
  2200. default:
  2201. return "Unknown";
  2202. }
  2203. }
  2204. void __init db8500_prcmu_early_init(void)
  2205. {
  2206. unsigned int i;
  2207. if (cpu_is_u8500v2()) {
  2208. void *tcpm_base = ioremap_nocache(U8500_PRCMU_TCPM_BASE, SZ_4K);
  2209. if (tcpm_base != NULL) {
  2210. u32 version;
  2211. version = readl(tcpm_base + PRCMU_FW_VERSION_OFFSET);
  2212. fw_info.version.project = version & 0xFF;
  2213. fw_info.version.api_version = (version >> 8) & 0xFF;
  2214. fw_info.version.func_version = (version >> 16) & 0xFF;
  2215. fw_info.version.errata = (version >> 24) & 0xFF;
  2216. fw_info.valid = true;
  2217. pr_info("PRCMU firmware: %s, version %d.%d.%d\n",
  2218. fw_project_name(fw_info.version.project),
  2219. (version >> 8) & 0xFF, (version >> 16) & 0xFF,
  2220. (version >> 24) & 0xFF);
  2221. iounmap(tcpm_base);
  2222. }
  2223. tcdm_base = __io_address(U8500_PRCMU_TCDM_BASE);
  2224. } else {
  2225. pr_err("prcmu: Unsupported chip version\n");
  2226. BUG();
  2227. }
  2228. spin_lock_init(&mb0_transfer.lock);
  2229. spin_lock_init(&mb0_transfer.dbb_irqs_lock);
  2230. mutex_init(&mb0_transfer.ac_wake_lock);
  2231. init_completion(&mb0_transfer.ac_wake_work);
  2232. mutex_init(&mb1_transfer.lock);
  2233. init_completion(&mb1_transfer.work);
  2234. mb1_transfer.ape_opp = APE_NO_CHANGE;
  2235. mutex_init(&mb2_transfer.lock);
  2236. init_completion(&mb2_transfer.work);
  2237. spin_lock_init(&mb2_transfer.auto_pm_lock);
  2238. spin_lock_init(&mb3_transfer.lock);
  2239. mutex_init(&mb3_transfer.sysclk_lock);
  2240. init_completion(&mb3_transfer.sysclk_work);
  2241. mutex_init(&mb4_transfer.lock);
  2242. init_completion(&mb4_transfer.work);
  2243. mutex_init(&mb5_transfer.lock);
  2244. init_completion(&mb5_transfer.work);
  2245. INIT_WORK(&mb0_transfer.mask_work, prcmu_mask_work);
  2246. /* Initalize irqs. */
  2247. for (i = 0; i < NUM_PRCMU_WAKEUPS; i++) {
  2248. unsigned int irq;
  2249. irq = IRQ_PRCMU_BASE + i;
  2250. irq_set_chip_and_handler(irq, &prcmu_irq_chip,
  2251. handle_simple_irq);
  2252. set_irq_flags(irq, IRQF_VALID);
  2253. }
  2254. }
  2255. static void __init init_prcm_registers(void)
  2256. {
  2257. u32 val;
  2258. val = readl(PRCM_A9PL_FORCE_CLKEN);
  2259. val &= ~(PRCM_A9PL_FORCE_CLKEN_PRCM_A9PL_FORCE_CLKEN |
  2260. PRCM_A9PL_FORCE_CLKEN_PRCM_A9AXI_FORCE_CLKEN);
  2261. writel(val, (PRCM_A9PL_FORCE_CLKEN));
  2262. }
  2263. /*
  2264. * Power domain switches (ePODs) modeled as regulators for the DB8500 SoC
  2265. */
  2266. static struct regulator_consumer_supply db8500_vape_consumers[] = {
  2267. REGULATOR_SUPPLY("v-ape", NULL),
  2268. REGULATOR_SUPPLY("v-i2c", "nmk-i2c.0"),
  2269. REGULATOR_SUPPLY("v-i2c", "nmk-i2c.1"),
  2270. REGULATOR_SUPPLY("v-i2c", "nmk-i2c.2"),
  2271. REGULATOR_SUPPLY("v-i2c", "nmk-i2c.3"),
  2272. /* "v-mmc" changed to "vcore" in the mainline kernel */
  2273. REGULATOR_SUPPLY("vcore", "sdi0"),
  2274. REGULATOR_SUPPLY("vcore", "sdi1"),
  2275. REGULATOR_SUPPLY("vcore", "sdi2"),
  2276. REGULATOR_SUPPLY("vcore", "sdi3"),
  2277. REGULATOR_SUPPLY("vcore", "sdi4"),
  2278. REGULATOR_SUPPLY("v-dma", "dma40.0"),
  2279. REGULATOR_SUPPLY("v-ape", "ab8500-usb.0"),
  2280. /* "v-uart" changed to "vcore" in the mainline kernel */
  2281. REGULATOR_SUPPLY("vcore", "uart0"),
  2282. REGULATOR_SUPPLY("vcore", "uart1"),
  2283. REGULATOR_SUPPLY("vcore", "uart2"),
  2284. REGULATOR_SUPPLY("v-ape", "nmk-ske-keypad.0"),
  2285. REGULATOR_SUPPLY("v-hsi", "ste_hsi.0"),
  2286. };
  2287. static struct regulator_consumer_supply db8500_vsmps2_consumers[] = {
  2288. REGULATOR_SUPPLY("musb_1v8", "ab8500-usb.0"),
  2289. /* AV8100 regulator */
  2290. REGULATOR_SUPPLY("hdmi_1v8", "0-0070"),
  2291. };
  2292. static struct regulator_consumer_supply db8500_b2r2_mcde_consumers[] = {
  2293. REGULATOR_SUPPLY("vsupply", "b2r2_bus"),
  2294. REGULATOR_SUPPLY("vsupply", "mcde"),
  2295. };
  2296. /* SVA MMDSP regulator switch */
  2297. static struct regulator_consumer_supply db8500_svammdsp_consumers[] = {
  2298. REGULATOR_SUPPLY("sva-mmdsp", "cm_control"),
  2299. };
  2300. /* SVA pipe regulator switch */
  2301. static struct regulator_consumer_supply db8500_svapipe_consumers[] = {
  2302. REGULATOR_SUPPLY("sva-pipe", "cm_control"),
  2303. };
  2304. /* SIA MMDSP regulator switch */
  2305. static struct regulator_consumer_supply db8500_siammdsp_consumers[] = {
  2306. REGULATOR_SUPPLY("sia-mmdsp", "cm_control"),
  2307. };
  2308. /* SIA pipe regulator switch */
  2309. static struct regulator_consumer_supply db8500_siapipe_consumers[] = {
  2310. REGULATOR_SUPPLY("sia-pipe", "cm_control"),
  2311. };
  2312. static struct regulator_consumer_supply db8500_sga_consumers[] = {
  2313. REGULATOR_SUPPLY("v-mali", NULL),
  2314. };
  2315. /* ESRAM1 and 2 regulator switch */
  2316. static struct regulator_consumer_supply db8500_esram12_consumers[] = {
  2317. REGULATOR_SUPPLY("esram12", "cm_control"),
  2318. };
  2319. /* ESRAM3 and 4 regulator switch */
  2320. static struct regulator_consumer_supply db8500_esram34_consumers[] = {
  2321. REGULATOR_SUPPLY("v-esram34", "mcde"),
  2322. REGULATOR_SUPPLY("esram34", "cm_control"),
  2323. REGULATOR_SUPPLY("lcla_esram", "dma40.0"),
  2324. };
  2325. static struct regulator_init_data db8500_regulators[DB8500_NUM_REGULATORS] = {
  2326. [DB8500_REGULATOR_VAPE] = {
  2327. .constraints = {
  2328. .name = "db8500-vape",
  2329. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2330. .always_on = true,
  2331. },
  2332. .consumer_supplies = db8500_vape_consumers,
  2333. .num_consumer_supplies = ARRAY_SIZE(db8500_vape_consumers),
  2334. },
  2335. [DB8500_REGULATOR_VARM] = {
  2336. .constraints = {
  2337. .name = "db8500-varm",
  2338. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2339. },
  2340. },
  2341. [DB8500_REGULATOR_VMODEM] = {
  2342. .constraints = {
  2343. .name = "db8500-vmodem",
  2344. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2345. },
  2346. },
  2347. [DB8500_REGULATOR_VPLL] = {
  2348. .constraints = {
  2349. .name = "db8500-vpll",
  2350. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2351. },
  2352. },
  2353. [DB8500_REGULATOR_VSMPS1] = {
  2354. .constraints = {
  2355. .name = "db8500-vsmps1",
  2356. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2357. },
  2358. },
  2359. [DB8500_REGULATOR_VSMPS2] = {
  2360. .constraints = {
  2361. .name = "db8500-vsmps2",
  2362. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2363. },
  2364. .consumer_supplies = db8500_vsmps2_consumers,
  2365. .num_consumer_supplies = ARRAY_SIZE(db8500_vsmps2_consumers),
  2366. },
  2367. [DB8500_REGULATOR_VSMPS3] = {
  2368. .constraints = {
  2369. .name = "db8500-vsmps3",
  2370. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2371. },
  2372. },
  2373. [DB8500_REGULATOR_VRF1] = {
  2374. .constraints = {
  2375. .name = "db8500-vrf1",
  2376. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2377. },
  2378. },
  2379. [DB8500_REGULATOR_SWITCH_SVAMMDSP] = {
  2380. /* dependency to u8500-vape is handled outside regulator framework */
  2381. .constraints = {
  2382. .name = "db8500-sva-mmdsp",
  2383. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2384. },
  2385. .consumer_supplies = db8500_svammdsp_consumers,
  2386. .num_consumer_supplies = ARRAY_SIZE(db8500_svammdsp_consumers),
  2387. },
  2388. [DB8500_REGULATOR_SWITCH_SVAMMDSPRET] = {
  2389. .constraints = {
  2390. /* "ret" means "retention" */
  2391. .name = "db8500-sva-mmdsp-ret",
  2392. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2393. },
  2394. },
  2395. [DB8500_REGULATOR_SWITCH_SVAPIPE] = {
  2396. /* dependency to u8500-vape is handled outside regulator framework */
  2397. .constraints = {
  2398. .name = "db8500-sva-pipe",
  2399. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2400. },
  2401. .consumer_supplies = db8500_svapipe_consumers,
  2402. .num_consumer_supplies = ARRAY_SIZE(db8500_svapipe_consumers),
  2403. },
  2404. [DB8500_REGULATOR_SWITCH_SIAMMDSP] = {
  2405. /* dependency to u8500-vape is handled outside regulator framework */
  2406. .constraints = {
  2407. .name = "db8500-sia-mmdsp",
  2408. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2409. },
  2410. .consumer_supplies = db8500_siammdsp_consumers,
  2411. .num_consumer_supplies = ARRAY_SIZE(db8500_siammdsp_consumers),
  2412. },
  2413. [DB8500_REGULATOR_SWITCH_SIAMMDSPRET] = {
  2414. .constraints = {
  2415. .name = "db8500-sia-mmdsp-ret",
  2416. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2417. },
  2418. },
  2419. [DB8500_REGULATOR_SWITCH_SIAPIPE] = {
  2420. /* dependency to u8500-vape is handled outside regulator framework */
  2421. .constraints = {
  2422. .name = "db8500-sia-pipe",
  2423. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2424. },
  2425. .consumer_supplies = db8500_siapipe_consumers,
  2426. .num_consumer_supplies = ARRAY_SIZE(db8500_siapipe_consumers),
  2427. },
  2428. [DB8500_REGULATOR_SWITCH_SGA] = {
  2429. .supply_regulator = "db8500-vape",
  2430. .constraints = {
  2431. .name = "db8500-sga",
  2432. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2433. },
  2434. .consumer_supplies = db8500_sga_consumers,
  2435. .num_consumer_supplies = ARRAY_SIZE(db8500_sga_consumers),
  2436. },
  2437. [DB8500_REGULATOR_SWITCH_B2R2_MCDE] = {
  2438. .supply_regulator = "db8500-vape",
  2439. .constraints = {
  2440. .name = "db8500-b2r2-mcde",
  2441. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2442. },
  2443. .consumer_supplies = db8500_b2r2_mcde_consumers,
  2444. .num_consumer_supplies = ARRAY_SIZE(db8500_b2r2_mcde_consumers),
  2445. },
  2446. [DB8500_REGULATOR_SWITCH_ESRAM12] = {
  2447. /*
  2448. * esram12 is set in retention and supplied by Vsafe when Vape is off,
  2449. * no need to hold Vape
  2450. */
  2451. .constraints = {
  2452. .name = "db8500-esram12",
  2453. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2454. },
  2455. .consumer_supplies = db8500_esram12_consumers,
  2456. .num_consumer_supplies = ARRAY_SIZE(db8500_esram12_consumers),
  2457. },
  2458. [DB8500_REGULATOR_SWITCH_ESRAM12RET] = {
  2459. .constraints = {
  2460. .name = "db8500-esram12-ret",
  2461. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2462. },
  2463. },
  2464. [DB8500_REGULATOR_SWITCH_ESRAM34] = {
  2465. /*
  2466. * esram34 is set in retention and supplied by Vsafe when Vape is off,
  2467. * no need to hold Vape
  2468. */
  2469. .constraints = {
  2470. .name = "db8500-esram34",
  2471. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2472. },
  2473. .consumer_supplies = db8500_esram34_consumers,
  2474. .num_consumer_supplies = ARRAY_SIZE(db8500_esram34_consumers),
  2475. },
  2476. [DB8500_REGULATOR_SWITCH_ESRAM34RET] = {
  2477. .constraints = {
  2478. .name = "db8500-esram34-ret",
  2479. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  2480. },
  2481. },
  2482. };
  2483. static struct mfd_cell db8500_prcmu_devs[] = {
  2484. {
  2485. .name = "db8500-prcmu-regulators",
  2486. .platform_data = &db8500_regulators,
  2487. .pdata_size = sizeof(db8500_regulators),
  2488. },
  2489. {
  2490. .name = "cpufreq-u8500",
  2491. },
  2492. };
  2493. /**
  2494. * prcmu_fw_init - arch init call for the Linux PRCMU fw init logic
  2495. *
  2496. */
  2497. static int __init db8500_prcmu_probe(struct platform_device *pdev)
  2498. {
  2499. int err = 0;
  2500. if (ux500_is_svp())
  2501. return -ENODEV;
  2502. init_prcm_registers();
  2503. /* Clean up the mailbox interrupts after pre-kernel code. */
  2504. writel(ALL_MBOX_BITS, PRCM_ARM_IT1_CLR);
  2505. err = request_threaded_irq(IRQ_DB8500_PRCMU1, prcmu_irq_handler,
  2506. prcmu_irq_thread_fn, IRQF_NO_SUSPEND, "prcmu", NULL);
  2507. if (err < 0) {
  2508. pr_err("prcmu: Failed to allocate IRQ_DB8500_PRCMU1.\n");
  2509. err = -EBUSY;
  2510. goto no_irq_return;
  2511. }
  2512. if (cpu_is_u8500v20_or_later())
  2513. prcmu_config_esram0_deep_sleep(ESRAM0_DEEP_SLEEP_STATE_RET);
  2514. err = mfd_add_devices(&pdev->dev, 0, db8500_prcmu_devs,
  2515. ARRAY_SIZE(db8500_prcmu_devs), NULL,
  2516. 0);
  2517. if (err)
  2518. pr_err("prcmu: Failed to add subdevices\n");
  2519. else
  2520. pr_info("DB8500 PRCMU initialized\n");
  2521. no_irq_return:
  2522. return err;
  2523. }
  2524. static struct platform_driver db8500_prcmu_driver = {
  2525. .driver = {
  2526. .name = "db8500-prcmu",
  2527. .owner = THIS_MODULE,
  2528. },
  2529. };
  2530. static int __init db8500_prcmu_init(void)
  2531. {
  2532. return platform_driver_probe(&db8500_prcmu_driver, db8500_prcmu_probe);
  2533. }
  2534. arch_initcall(db8500_prcmu_init);
  2535. MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com>");
  2536. MODULE_DESCRIPTION("DB8500 PRCM Unit driver");
  2537. MODULE_LICENSE("GPL v2");