upower_api.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /**
  3. * Copyright 2019-2024 NXP
  4. *
  5. * KEYWORDS: micro-power uPower driver API
  6. */
  7. #include <string.h>
  8. #include "upower_api.h"
  9. #include "upower_soc_defs.h"
  10. /* ---------------------------------------------------------------
  11. * Common Macros
  12. * ---------------------------------------------------------------
  13. */
  14. /* tests Service Group busy */
  15. #define UPWR_SG_BUSY(sg) ((sg_busy & (1U << (sg))) == 1U)
  16. /* install user callback for the Service Group */
  17. #define UPWR_USR_CALLB(sg, cb) { user_callback[(sg)] = (cb); }
  18. /* fills up common message header info */
  19. #define UPWR_MSG_HDR(hdr, sg, fn) { \
  20. (hdr).domain = (uint32_t)pwr_domain; \
  21. (hdr).srvgrp = (sg); \
  22. (hdr).function = (fn); }
  23. /* ---------------------------------------------------------------
  24. * Common Data Structures
  25. * ---------------------------------------------------------------
  26. */
  27. static soc_domain_t pwr_domain;
  28. static upwr_code_vers_t fw_rom_version;
  29. static upwr_code_vers_t fw_ram_version;
  30. static uint32_t fw_launch_option;
  31. /* shared memory buffers */
  32. #define UPWR_API_BUFFER_SIZE (MAX_SG_EXCEPT_MEM_SIZE + \
  33. MAX_SG_PWRMGMT_MEM_SIZE + MAX_SG_VOLTM_MEM_SIZE)
  34. /* service group shared mem buffer pointers */
  35. static void *sh_buffer[UPWR_SG_COUNT];
  36. /* Callbacks registered for each service group :
  37. *
  38. * NULL means no callback is registered;
  39. * for sgrp_callback, it also means the service group is
  40. * free to receive a new request.
  41. */
  42. static upwr_callb user_callback[UPWR_SG_COUNT];
  43. static UPWR_RX_CALLB_FUNC_T sgrp_callback[UPWR_SG_COUNT];
  44. /* request data structures for each service group */
  45. /* message waiting for TX */
  46. static upwr_down_max_msg sg_req_msg[UPWR_SG_COUNT];
  47. /* waiting message size */
  48. static unsigned int sg_req_siz[UPWR_SG_COUNT];
  49. /* response msg */
  50. static upwr_up_max_msg sg_rsp_msg[UPWR_SG_COUNT];
  51. /* response msg size */
  52. static unsigned int sg_rsp_siz[UPWR_SG_COUNT];
  53. /* tx pending status for each (1 bit per service group) */
  54. static volatile uint32_t sg_tx_pend;
  55. /* serv.group of current ongoing Tx, if any */
  56. static volatile upwr_sg_t sg_tx_curr;
  57. /* service group busy status, only for this domain (MU index 0) */
  58. /* SG bit = 1 if group is busy with a request */
  59. static volatile uint32_t sg_busy;
  60. /* OS-dependent memory allocation function */
  61. static upwr_malloc_ptr_t os_malloc;
  62. /* OS-dependent pointer->physical address conversion function */
  63. static upwr_phyadr_ptr_t os_ptr2phy;
  64. /* OS-dependent function to lock critical code */
  65. static upwr_lock_ptr_t os_lock;
  66. /* pointer to MU structure */
  67. static struct MU_t *mu;
  68. /*
  69. * indicates that a transmission was done and is pending; this
  70. * bit is necessary because the Tx and Rx interrupts are ORed
  71. * together, and there is no way of telling if only Rx interrupt
  72. * or both occurred just by looking at the MU status registers
  73. */
  74. static uint32_t mu_tx_pend;
  75. static UPWR_TX_CALLB_FUNC_T mu_tx_callb;
  76. static UPWR_RX_CALLB_FUNC_T mu_rx_callb;
  77. #define UPWR_API_INIT_WAIT (0U) /* waiting for ROM firmware initialization */
  78. #define UPWR_API_INITLZED (1U) /* ROM firmware initialized */
  79. #define UPWR_API_START_WAIT (2U) /* waiting for start services */
  80. #define UPWR_API_SHUTDOWN_WAIT (3U) /* waiting for shutdown */
  81. #define UPWR_API_READY (4U) /* ready to receive service requests */
  82. volatile upwr_api_state_t api_state;
  83. /* default pointer->physical address conversion, returns the same address */
  84. static void *ptr2phys(const void *ptr)
  85. {
  86. return (void *)ptr;
  87. }
  88. /* ---------------------------------------------------------------
  89. * SHARED MEMORY MANAGEMENT
  90. * --------------------------------------------------------------
  91. */
  92. /*
  93. * upwr_ptr2offset() - converts a pointer (casted to uint64_t) to an
  94. * address offset from the shared memory start. If it does not point
  95. * to a shared memory location, the structure pointed is copied to a
  96. * buffer in the shared memory, and the buffer offset is returned.
  97. * The 2nd argument is the service group to which the buffer belongs;
  98. * The 3rd argument is the size of structure to be copied. The 4th argument
  99. * is an offset to apply to the copy destination address. The 5th argument
  100. * is ptr before the conversion to physical address. 2nd, 3rd. 4th and 5th
  101. * arguments are not used if the 1st one points to a location inside the
  102. * shared memory.
  103. */
  104. static uint32_t upwr_ptr2offset(unsigned long ptr,
  105. upwr_sg_t sg,
  106. size_t siz,
  107. size_t offset,
  108. const void *vptr)
  109. {
  110. if ((ptr >= UPWR_DRAM_SHARED_BASE_ADDR) &&
  111. ((ptr - UPWR_DRAM_SHARED_BASE_ADDR) < UPWR_DRAM_SHARED_SIZE)) {
  112. return (uint32_t)(ptr - UPWR_DRAM_SHARED_BASE_ADDR);
  113. }
  114. /* pointer is outside the shared memory, copy the struct to buffer */
  115. (void)memcpy((void *)(offset + (char *)sh_buffer[sg]), (void *)vptr, siz);
  116. return (uint32_t)((unsigned long)sh_buffer[sg] + offset - UPWR_DRAM_SHARED_BASE_ADDR);
  117. }
  118. /*
  119. * ---------------------------------------------------------------
  120. * INTERRUPTS AND CALLBACKS
  121. * Service-group specific callbacks are in their own sections
  122. * --------------------------------------------------------------
  123. */
  124. /*
  125. * upwr_lock()- locks (lock=1) or unlocks (lock=0) a critical code section;
  126. * for now it only needs to protect a portion of the code from being
  127. * interrupted by the MU.
  128. */
  129. static void upwr_lock(int lock)
  130. {
  131. if (os_lock != NULL) {
  132. os_lock(lock);
  133. }
  134. }
  135. /* upwr_exp_isr()- handles the exception interrupt from uPower */
  136. static void upwr_exp_isr(void)
  137. {
  138. }
  139. /* upwr_copy2tr prototype; function definition in auxiliary function section */
  140. void upwr_copy2tr(struct MU_t *local_mu, const uint32_t *msg, unsigned int size);
  141. #define UPWR_MU_TSR_EMPTY ((uint32_t)((1UL << UPWR_MU_MSG_SIZE) - 1UL))
  142. /* upwr_txrx_isr()- handles both the Tx and Rx MU interrupts */
  143. void upwr_txrx_isr(void)
  144. {
  145. /* Tx pending and TX register empty */
  146. if ((mu_tx_pend != 0UL) && (mu->TSR.R == UPWR_MU_TSR_EMPTY)) {
  147. mu_tx_pend = 0UL;
  148. /* disable the tx interrupts */
  149. mu->TCR.R = 0U;
  150. /* urgency flag off, in case it was set */
  151. mu->FCR.B.F0 = 0U;
  152. if (mu_tx_callb != NULL) {
  153. mu_tx_callb();
  154. }
  155. }
  156. /* RX ISR occurred */
  157. if (mu->RSR.R != 0UL) {
  158. /* disable the interrupt until data is read */
  159. mu->RCR.R = 0U;
  160. if (mu_rx_callb != NULL) {
  161. mu_rx_callb();
  162. }
  163. }
  164. }
  165. /**
  166. * upwr_next_req() - sends the next pending service request message, if any.
  167. *
  168. * Called upon MU Tx interrupts, it checks if there is any service request
  169. * pending amongst the service groups, and sends the request if needed.
  170. *
  171. * Context: no sleep, no locks taken/released.
  172. * Return: none (void).
  173. */
  174. static void upwr_next_req(void)
  175. {
  176. upwr_sg_t sg = (upwr_sg_t)0U;
  177. /* no lock needed here, this is called from an MU ISR */
  178. sg_tx_pend &= ~((uint32_t)1UL << sg_tx_curr); /* no longer pending */
  179. if (sg_tx_pend == 0U) {
  180. return; /* no other pending */
  181. }
  182. /* find the next one pending */
  183. for (uint32_t mask = 1UL; mask < (1UL << UPWR_SG_COUNT); mask = mask << 1UL) {
  184. if ((sg_tx_pend & mask) != 0U) {
  185. break;
  186. }
  187. sg = (upwr_sg_t)(sg + 1U);
  188. }
  189. sg_tx_curr = sg;
  190. if (upwr_tx((uint32_t *)&sg_req_msg[sg], sg_req_siz[sg], upwr_next_req) < 0) {
  191. return; /* leave the Tx pending */
  192. }
  193. }
  194. /**
  195. * upwr_mu_int_callback() - general MU interrupt callback.
  196. *
  197. * Called upon MU Rx interrupts, it calls the Service Group-specific callback,
  198. * if any registered, based on the service group field in the received message.
  199. * Otherwise, calls the user callback, if any registered.
  200. *
  201. * Context: no sleep, no locks taken/released.
  202. * Return: none (void).
  203. */
  204. static void upwr_mu_int_callback(void)
  205. {
  206. upwr_sg_t sg; /* service group number */
  207. UPWR_RX_CALLB_FUNC_T sg_callb; /* service group callback */
  208. upwr_up_max_msg rxmsg = {0};
  209. unsigned int size; /* in words */
  210. if (upwr_rx((char *)&rxmsg, &size) < 0) {
  211. return;
  212. }
  213. sg = (upwr_sg_t)rxmsg.hdr.srvgrp;
  214. /* copy msg to the service group buffer */
  215. msg_copy((char *)&sg_rsp_msg[sg], (char *)&rxmsg, size);
  216. sg_rsp_siz[sg] = size;
  217. /* clear the service group busy status */
  218. sg_busy &= ~(1UL << sg); /* no lock needed here, we're in the MU ISR */
  219. sg_callb = sgrp_callback[sg];
  220. if (sg_callb == NULL) {
  221. upwr_callb user_callb = user_callback[sg];
  222. /* no service group callback; call the user callback if any */
  223. if (user_callb == NULL) {
  224. goto done; /* no user callback */
  225. }
  226. /* make the user callback */
  227. user_callb(sg, rxmsg.hdr.function,
  228. (upwr_resp_t)rxmsg.hdr.errcode,
  229. (size == 2U) ? rxmsg.word2 : rxmsg.hdr.ret);
  230. goto done;
  231. }
  232. /*
  233. * finally make the group callback. don't uninstall the group
  234. * callback, it is permanent.
  235. */
  236. sg_callb();
  237. done:
  238. if (rxmsg.hdr.errcode == UPWR_RESP_SHUTDOWN) { /* shutdown error: */
  239. /*
  240. * change the API state automatically. so new requests
  241. * are rejected by the API immediately
  242. */
  243. api_state = UPWR_API_INITLZED;
  244. }
  245. }
  246. /**
  247. * upwr_srv_req() - sends a service request message.
  248. * @sg: message service group.
  249. * @msg: pointer to the message
  250. * @size: message size in 32-bit words.
  251. *
  252. * The message is sent right away if possible, or gets pending to be sent later.
  253. * If pending, the message is stored in sg_req_msg and will be sent when the
  254. * MU transmission buffer is clear and there are no other pending messages
  255. * from higher priority service groups.
  256. *
  257. * This is an auxiliary function used by the rest of the API calls.
  258. * It is normally not called by the driver code, unless maybe for test purposes.
  259. *
  260. * Context: no sleep, no locks taken/released.
  261. * Return: none (void)
  262. */
  263. static void upwr_srv_req(upwr_sg_t sg,
  264. uint32_t *msg,
  265. unsigned int size)
  266. {
  267. int rc;
  268. upwr_lock(1);
  269. sg_busy |= (uint32_t)1U << sg;
  270. upwr_lock(0);
  271. rc = upwr_tx(msg, size, upwr_next_req);
  272. if (rc < 0) {
  273. /* queue full, make the transmission pending */
  274. msg_copy((char *)&sg_req_msg[sg], (char *)msg, size);
  275. sg_req_siz[sg] = size;
  276. upwr_lock(1);
  277. sg_tx_curr = sg;
  278. sg_tx_pend |= (uint32_t)1U << sg;
  279. upwr_lock(0);
  280. return;
  281. }
  282. }
  283. /**---------------------------------------------------------------
  284. * INITIALIZATION, CONFIGURATION
  285. *
  286. * A reference uPower initialization sequence goes as follows:
  287. *
  288. * 1. host CPU calls upwr_init.
  289. * 2. (optional) host checks the ROM version and SoC code calling upwr_vers(...)
  290. * and optionally performs any configuration or workaround accordingly.
  291. * 3. host CPU calls upwr_start to start the uPower services, passing a
  292. * service option number.
  293. * If no RAM code is loaded or it has no service options, the launch option
  294. * number passed must be 0, which will start the services available in ROM.
  295. * upwr_start also receives a pointer to a callback called by the API
  296. * when the firmware is ready to receive service requests.
  297. * The callback may be replaced by polling, calling upwr_req_status in a loop
  298. * or upwr_poll_req_status; in this case the callback pointer may be NULL.
  299. * A host may call upwr_start even if the services were already started by
  300. * any host: if the launch option is the same, the response will be ok,
  301. * but will indicate error if the services were already started with a
  302. * different launch option.
  303. * 4. host waits for the callback calling, or polling finishing;
  304. * if no error is returned, it can start making service calls using the API.
  305. *
  306. * Variations on that reference sequence are possible:
  307. * - the uPower services can be started using the ROM code only, which includes
  308. * the basic Power Management services, among others, with launch option
  309. * number = 0.
  310. * The code RAM can be loaded while these services are running and,
  311. * when the loading is done, the services can be re-started with these 2
  312. * requests executed in order: upwr_xcp_shutdown and upwr_start,
  313. * using the newly loaded RAM code (launch option > 0).
  314. *
  315. * NOTE: the initialization call upwr_init is not effective and
  316. * returns error when called after the uPower services are started.
  317. */
  318. /**
  319. * upwr_start_callb() - internal callback for the Rx message from uPower
  320. * that indicates the firmware is ready to receive the start commands.
  321. * It calls the user callbacks registered in the upwr_start_boot and upwr_start
  322. * call.
  323. */
  324. void upwr_start_callb(void)
  325. {
  326. switch (api_state) {
  327. case UPWR_API_START_WAIT: {
  328. upwr_rdy_callb start_callb = (upwr_rdy_callb)user_callback[UPWR_SG_EXCEPT];
  329. upwr_ready_msg *msg = (upwr_ready_msg *)&sg_rsp_msg[UPWR_SG_EXCEPT];
  330. fw_ram_version.soc_id = fw_rom_version.soc_id;
  331. fw_ram_version.vmajor = msg->args.vmajor;
  332. fw_ram_version.vminor = msg->args.vminor;
  333. fw_ram_version.vfixes = msg->args.vfixes;
  334. /*
  335. * vmajor == vminor == vfixes == 0 indicates start error
  336. * in this case, go back to the INITLZED state
  337. */
  338. if ((fw_ram_version.vmajor != 0U) ||
  339. (fw_ram_version.vminor != 0U) ||
  340. (fw_ram_version.vfixes != 0U)) {
  341. api_state = UPWR_API_READY;
  342. /*
  343. * initialization is over:
  344. * uninstall the user callback just in case
  345. */
  346. UPWR_USR_CALLB(UPWR_SG_EXCEPT, NULL);
  347. if (fw_launch_option == 0U) {
  348. /*
  349. * launched ROM firmware:
  350. * RAM fw versions must be all 0s
  351. */
  352. fw_ram_version.vmajor = 0U;
  353. fw_ram_version.vminor = 0U;
  354. fw_ram_version.vfixes = 0U;
  355. }
  356. } else {
  357. api_state = UPWR_API_INITLZED;
  358. }
  359. start_callb(msg->args.vmajor, msg->args.vminor, msg->args.vfixes);
  360. }
  361. break;
  362. case UPWR_API_SHUTDOWN_WAIT: {
  363. upwr_callb user_callb = (upwr_callb)user_callback[UPWR_SG_EXCEPT];
  364. upwr_shutdown_msg *msg = (upwr_shutdown_msg *)&sg_rsp_msg[UPWR_SG_EXCEPT];
  365. if ((upwr_resp_t)msg->hdr.errcode == UPWR_RESP_OK) {
  366. api_state = UPWR_API_INITLZED;
  367. }
  368. if (user_callb != NULL) {
  369. user_callb(UPWR_SG_EXCEPT, UPWR_XCP_SHUTDOWN,
  370. (upwr_resp_t)msg->hdr.errcode, 0U);
  371. }
  372. }
  373. break;
  374. case UPWR_API_READY:
  375. {
  376. upwr_callb user_callb = (upwr_callb)user_callback[UPWR_SG_EXCEPT];
  377. upwr_up_max_msg *msg = (upwr_up_max_msg *)&sg_rsp_msg[UPWR_SG_EXCEPT];
  378. if (user_callb != NULL) {
  379. user_callb(UPWR_SG_EXCEPT, msg->hdr.function,
  380. (upwr_resp_t)msg->hdr.errcode,
  381. (int)((sg_rsp_siz[UPWR_SG_EXCEPT] == 2U) ?
  382. msg->word2 : msg->hdr.ret));
  383. }
  384. }
  385. break;
  386. default:
  387. break;
  388. }
  389. }
  390. /**
  391. * upwr_init() - API initialization; must be the first API call after reset.
  392. * @domain: SoC-dependent CPU domain id; identifier used by the firmware in
  393. * many services. Defined by SoC-dependent type soc_domain_t found in
  394. * upower_soc_defs.h.
  395. * @muptr: pointer to the MU instance.
  396. * @mallocptr: pointer to the memory allocation function
  397. * @physaddrptr: pointer to the function to convert pointers to
  398. * physical addresses. If NULL, no conversion is made (pointer=physical address)
  399. * @isrinstptr: pointer to the function to install the uPower ISR callbacks;
  400. * the function receives the pointers to the MU tx/rx and Exception ISRs
  401. * callbacks, which must be called from the actual system ISRs.
  402. * The function pointed by isrinstptr must also enable the interrupt at the
  403. * core/interrupt controller, but must not enable the interrupt at the MU IP.
  404. * The system ISRs are responsible for dealing with the interrupt controller,
  405. * performing any other context save/restore, and any other housekeeping.
  406. * @lockptr: pointer to a function that prevents MU interrupts (if argrument=1)
  407. * or allows it (if argument=0). The API calls this function to make small
  408. * specific code portions thread safe. Only MU interrupts must be avoided,
  409. * the code may be suspended for other reasons.
  410. * If no MU interrupts can happen during the execution of an API call or
  411. * callback, even if enabled, for some other reason (e.g. interrupt priority),
  412. * then this argument may be NULL.
  413. *
  414. * Context: no sleep, no locks taken/released.
  415. * Return: 0 if ok,
  416. * -1 if failed to allocate memory, or use some other resource.
  417. * -2 if any argument is invalid.
  418. * -3 if failed to send the ping message.
  419. * -4 if failed to receive the initialization message, or was invalid
  420. */
  421. int upwr_init(soc_domain_t domain, struct MU_t *muptr,
  422. const upwr_malloc_ptr_t mallocptr,
  423. const upwr_phyadr_ptr_t phyadrptr,
  424. const upwr_inst_isr_ptr_t isrinstptr,
  425. const upwr_lock_ptr_t lockptr)
  426. {
  427. uint32_t j;
  428. upwr_sg_t sg; /* service group number */
  429. unsigned int size;
  430. unsigned long dom_buffer_base = (domain == RTD_DOMAIN) ? UPWR_API_BUFFER_BASE :
  431. ((UPWR_API_BUFFER_ENDPLUS + UPWR_API_BUFFER_BASE) / 2U);
  432. upwr_init_msg *msg = (upwr_init_msg *)&sg_rsp_msg[UPWR_SG_EXCEPT];
  433. mu = muptr;
  434. /*
  435. * Disable tx and rx interrupts in case not called
  436. * 1st time after reset
  437. */
  438. mu->TCR.R = mu->RCR.R = 0U;
  439. os_malloc = mallocptr;
  440. os_ptr2phy = (phyadrptr == (upwr_phyadr_ptr_t)NULL) ? ptr2phys : phyadrptr;
  441. os_lock = lockptr;
  442. api_state = UPWR_API_INIT_WAIT;
  443. sg_busy = 0UL;
  444. pwr_domain = domain;
  445. /* initialize the versions, in case they are polled */
  446. fw_rom_version.soc_id = 0U;
  447. fw_rom_version.vmajor = 0U;
  448. fw_rom_version.vminor = 0U;
  449. fw_rom_version.vfixes = 0U;
  450. fw_ram_version.soc_id = 0U;
  451. fw_ram_version.vmajor = 0U;
  452. fw_ram_version.vminor = 0U;
  453. fw_ram_version.vfixes = 0U;
  454. mu_tx_pend = (uint32_t)0U;
  455. sg_tx_pend = (uint32_t)0U;
  456. sg_tx_curr = UPWR_SG_COUNT; /* means none here */
  457. sh_buffer[UPWR_SG_EXCEPT] = (void *)(unsigned long)dom_buffer_base;
  458. sh_buffer[UPWR_SG_PWRMGMT] = (void *)(unsigned long)(dom_buffer_base +
  459. MAX_SG_EXCEPT_MEM_SIZE);
  460. sh_buffer[UPWR_SG_DELAYM] = NULL;
  461. sh_buffer[UPWR_SG_VOLTM] = (void *)(unsigned long)(dom_buffer_base +
  462. MAX_SG_EXCEPT_MEM_SIZE + MAX_SG_PWRMGMT_MEM_SIZE);
  463. sh_buffer[UPWR_SG_CURRM] = NULL;
  464. sh_buffer[UPWR_SG_TEMPM] = NULL;
  465. sh_buffer[UPWR_SG_DIAG] = NULL;
  466. /* (no buffers service groups other than xcp and pwm for now) */
  467. for (j = 0; j < UPWR_SG_COUNT; j++) {
  468. user_callback[j] = NULL;
  469. /* service group Exception gets the initialization callbacks */
  470. sgrp_callback[j] = (j == UPWR_SG_EXCEPT) ? upwr_start_callb : NULL;
  471. /* response messages with an initial consistent content */
  472. sg_rsp_msg[j].hdr.errcode = UPWR_RESP_SHUTDOWN;
  473. }
  474. /* init message already received, assume takss are running on upower */
  475. if (mu->FSR.B.F0 != 0U) {
  476. /* send a ping message down to get the ROM version back */
  477. upwr_xcp_ping_msg ping_msg = {0};
  478. ping_msg.hdr.domain = pwr_domain;
  479. ping_msg.hdr.srvgrp = UPWR_SG_EXCEPT;
  480. ping_msg.hdr.function = UPWR_XCP_PING;
  481. if (mu->RSR.B.RF0 != 0U) { /* first clean any Rx message left over */
  482. (void)upwr_rx((char *)msg, &size);
  483. }
  484. /* wait any TX left over to be sent */
  485. while (mu->TSR.R != UPWR_MU_TSR_EMPTY) {
  486. }
  487. /*
  488. * now send the ping message;
  489. * do not use upwr_tx, which needs API initialized;
  490. * just write to the MU TR register(s)
  491. */
  492. mu->FCR.B.F0 = 1U; /* flag urgency status */
  493. upwr_copy2tr(mu, (uint32_t *)&ping_msg, sizeof(ping_msg) / 4U);
  494. }
  495. do {
  496. /*
  497. * poll for the MU Rx status: wait for an init message, either
  498. * 1st sent from uPower after reset or as a response to a ping
  499. */
  500. while (mu->RSR.B.RF0 == 0U) {
  501. }
  502. /* urgency status off, in case it was set */
  503. mu->FCR.B.F0 = 0U;
  504. if (upwr_rx((char *)msg, &size) < 0) {
  505. return -4;
  506. }
  507. if (size != (sizeof(upwr_init_msg) / 4U)) {
  508. if (mu->FSR.B.F0 != 0U) {
  509. continue; /* discard left over msg */
  510. } else {
  511. return -4;
  512. }
  513. }
  514. sg = (upwr_sg_t)msg->hdr.srvgrp;
  515. if (sg != UPWR_SG_EXCEPT) {
  516. if (mu->FSR.B.F0 != 0U) {
  517. continue; /* discard left over msg */
  518. } else {
  519. return -4;
  520. }
  521. }
  522. if ((upwr_xcp_f_t)msg->hdr.function != UPWR_XCP_INIT) {
  523. if (mu->FSR.B.F0 != 0U) {
  524. continue; /* discard left over msg */
  525. } else {
  526. return -4;
  527. }
  528. }
  529. break;
  530. } while (true);
  531. fw_rom_version.soc_id = msg->args.soc;
  532. fw_rom_version.vmajor = msg->args.vmajor;
  533. fw_rom_version.vminor = msg->args.vminor;
  534. fw_rom_version.vfixes = msg->args.vfixes;
  535. if (upwr_rx_callback(upwr_mu_int_callback) < 0) {
  536. /* catastrophic error, but is it possible to happen? */
  537. return -1;
  538. }
  539. mu_tx_callb = NULL; /* assigned on upwr_tx */
  540. /* install the ISRs and enable the interrupts */
  541. isrinstptr(upwr_txrx_isr, upwr_exp_isr);
  542. /* enable only RR[0] receive interrupt */
  543. mu->RCR.R = 1U;
  544. api_state = UPWR_API_INITLZED;
  545. return 0;
  546. }
  547. /**
  548. * upwr_start() - Starts the uPower services.
  549. * @launchopt: a number to select between multiple launch options,
  550. * that may define, among other things, which services will be started,
  551. * or which services implementations, features etc.
  552. * launchopt = 0 selects a subset of services implemented in ROM;
  553. * any other number selects service sets implemented in RAM, launched
  554. * by the firmware function ram_launch; if an invalid launchopt value is passed,
  555. * no services are started, and the callback returns error (see below).
  556. * @rdycallb: pointer to the callback to be called when the uPower is ready
  557. * to receive service requests. NULL if no callback needed.
  558. * The callback receives as arguments the RAM firmware version numbers.
  559. * If all 3 numbers (vmajor, vminor, vfixes) are 0, that means the
  560. * service launching failed.
  561. * Firmware version numbers will be the same as ROM if launchopt = 0,
  562. * selecting the ROM services.
  563. *
  564. * upwr_start can be called by any domain even if the services are already
  565. * started: it has no effect, returning success, if the launch option is the
  566. * same as the one that actually started the service, and returns error if
  567. * called with a different option.
  568. *
  569. * A callback can be optionally registered, and will be called upon the arrival
  570. * of the request response from the uPower firmware, telling if it succeeded or
  571. * not.
  572. * A callback may not be registered (NULL pointer), in which case polling has
  573. * to be used to check the response, by calling upwr_req_status or
  574. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  575. *
  576. * Context: no sleep, no locks taken/released.
  577. * Return: 0 if ok,
  578. * -1 if a resource failed,
  579. * -2 if the domain passed is the same as the caller,
  580. * -3 if called in an invalid API state
  581. */
  582. int upwr_start(uint32_t launchopt, const upwr_rdy_callb rdycallb)
  583. {
  584. upwr_start_msg txmsg = {0};
  585. if (api_state != UPWR_API_INITLZED) {
  586. return -3;
  587. }
  588. UPWR_USR_CALLB(UPWR_SG_EXCEPT, (upwr_callb)rdycallb);
  589. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_START);
  590. txmsg.hdr.arg = fw_launch_option = launchopt;
  591. if (upwr_tx((uint32_t *)&txmsg, sizeof(txmsg) / 4U, NULL) < 0) {
  592. /* catastrophic error, but is it possible to happen? */
  593. return -1;
  594. }
  595. api_state = UPWR_API_START_WAIT;
  596. return 0;
  597. }
  598. /**---------------------------------------------------------------
  599. * EXCEPTION SERVICE GROUP
  600. */
  601. /**
  602. * upwr_xcp_config() - Applies general uPower configurations.
  603. * @config: pointer to the uPower SoC-dependent configuration struct
  604. * upwr_xcp_config_t defined in upower_soc_defs.h. NULL may be passed, meaning
  605. * a request to read the configuration, in which case it appears in the callback
  606. * argument ret, or can be pointed by argument retptr in the upwr_req_status and
  607. * upwr_poll_req_status calls, casted to upwr_xcp_config_t.
  608. * @callb: pointer to the callback to be called when the uPower has finished
  609. * the configuration, or NULL if no callback needed (polling used instead).
  610. *
  611. * Some configurations are targeted for a specific domain (see the struct
  612. * upwr_xcp_config_t definition in upower_soc_defs.h); this call has implicit
  613. * domain target (the same domain from which is called).
  614. *
  615. * The return value is always the current configuration value, either in a
  616. * read-only request (config = NULL) or after setting a new configuration
  617. * (non-NULL config).
  618. *
  619. * A callback can be optionally registered, and will be called upon the arrival
  620. * of the request response from the uPower firmware, telling if it succeeded or
  621. * not.
  622. * A callback may not be registered (NULL pointer), in which case polling has
  623. * to be used to check the response, by calling upwr_req_status or
  624. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  625. *
  626. * Context: no sleep, no locks taken/released.
  627. * Return: 0 if ok,
  628. * -1 if service group is busy,
  629. * -3 if called in an invalid API state
  630. */
  631. int upwr_xcp_config(const upwr_xcp_config_t *config, const upwr_callb callb)
  632. {
  633. upwr_xcp_config_msg txmsg = {0};
  634. if (api_state != UPWR_API_READY) {
  635. return -3;
  636. }
  637. if (UPWR_SG_BUSY(UPWR_SG_EXCEPT)) {
  638. return -1;
  639. }
  640. UPWR_USR_CALLB(UPWR_SG_EXCEPT, callb);
  641. if (config == NULL) {
  642. txmsg.hdr.arg = 1U; /* 1= read, txmsg.word2 ignored */
  643. } else {
  644. txmsg.hdr.arg = 0U; /* 1= write */
  645. txmsg.word2 = config->R;
  646. }
  647. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_CONFIG);
  648. upwr_srv_req(UPWR_SG_EXCEPT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  649. return 0;
  650. }
  651. /**
  652. * upwr_xcp_sw_alarm() - Makes uPower issue an alarm interrupt to given domain.
  653. * @domain: identifier of the domain to alarm. Defined by SoC-dependent type
  654. * soc_domain_t found in upower_soc_defs.h.
  655. * @code: alarm code. Defined by SoC-dependent type upwr_alarm_t found in
  656. * upower_soc_defs.h.
  657. * @callb: pointer to the callback to be called when the uPower has finished
  658. * the alarm, or NULL if no callback needed (polling used instead).
  659. *
  660. * The function requests the uPower to issue an alarm of the given code as if
  661. * it had originated internally. This service is useful mainly to test the
  662. * system response to such alarms, or to make the system handle a similar alarm
  663. * situation detected externally to uPower.
  664. *
  665. * The system ISR/code handling the alarm may retrieve the alarm code by calling
  666. * the auxiliary function upwr_alarm_code.
  667. *
  668. * A callback can be optionally registered, and will be called upon the arrival
  669. * of the request response from the uPower firmware, telling if it succeeded or
  670. * not.
  671. * A callback may not be registered (NULL pointer), in which case polling has
  672. * to be used to check the response, by calling upwr_req_status or
  673. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  674. *
  675. * Context: no sleep, no locks taken/released.
  676. * Return: 0 if ok,
  677. * -1 if service group is busy,
  678. * -3 if called in an invalid API state
  679. */
  680. int upwr_xcp_sw_alarm(soc_domain_t domain,
  681. upwr_alarm_t code,
  682. const upwr_callb callb)
  683. {
  684. upwr_xcp_swalarm_msg txmsg = {0};
  685. if (api_state != UPWR_API_READY) {
  686. return -3;
  687. }
  688. if (UPWR_SG_BUSY(UPWR_SG_EXCEPT)) {
  689. return -1;
  690. }
  691. UPWR_USR_CALLB(UPWR_SG_EXCEPT, callb);
  692. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_SW_ALARM);
  693. txmsg.hdr.domain = (uint32_t)domain;
  694. txmsg.hdr.arg = (uint32_t)code;
  695. upwr_srv_req(UPWR_SG_EXCEPT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  696. return 0;
  697. }
  698. /**
  699. * upwr_xcp_set_ddr_retention() - M33/A35 can use this API to set/clear ddr retention
  700. * @domain: identifier of the caller domain.
  701. * soc_domain_t found in upower_soc_defs.h.
  702. * @enable: true, means that set ddr retention, false clear ddr retention.
  703. * @callb: NULL
  704. *
  705. * A callback may not be registered (NULL pointer), in which case polling has
  706. * to be used to check the response, by calling upwr_req_status or
  707. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  708. *
  709. * Context: no sleep, no locks taken/released.
  710. * Return: 0 if ok,
  711. * -1 if service group is busy,
  712. * -3 if called in an invalid API state
  713. */
  714. int upwr_xcp_set_ddr_retention(soc_domain_t domain,
  715. uint32_t enable,
  716. const upwr_callb callb)
  717. {
  718. upwr_xcp_ddr_retn_msg txmsg = {0};
  719. if (api_state != UPWR_API_READY) {
  720. return -3;
  721. }
  722. if (UPWR_SG_BUSY(UPWR_SG_EXCEPT)) {
  723. return -1;
  724. }
  725. UPWR_USR_CALLB(UPWR_SG_EXCEPT, callb);
  726. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_SET_DDR_RETN);
  727. txmsg.hdr.domain = (uint32_t)domain;
  728. txmsg.hdr.arg = (uint32_t)enable;
  729. upwr_srv_req(UPWR_SG_EXCEPT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  730. return 0;
  731. }
  732. /**
  733. * upwr_xcp_set_mipi_dsi_ena() - M33/A35 can use this API to set/clear mipi dsi ena
  734. * @domain: identifier of the caller domain.
  735. * soc_domain_t found in upower_soc_defs.h.
  736. * @enable: true, means that set ddr retention, false clear ddr retention.
  737. * @callb: NULL
  738. *
  739. * A callback may not be registered (NULL pointer), in which case polling has
  740. * to be used to check the response, by calling upwr_req_status or
  741. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  742. *
  743. * Context: no sleep, no locks taken/released.
  744. * Return: 0 if ok,
  745. * -1 if service group is busy,
  746. * -3 if called in an invalid API state
  747. */
  748. int upwr_xcp_set_mipi_dsi_ena(soc_domain_t domain,
  749. uint32_t enable,
  750. const upwr_callb callb)
  751. {
  752. upwr_xcp_set_mipi_dsi_ena_msg txmsg = {0};
  753. if (api_state != UPWR_API_READY) {
  754. return -3;
  755. }
  756. if (UPWR_SG_BUSY(UPWR_SG_EXCEPT)) {
  757. return -1;
  758. }
  759. UPWR_USR_CALLB(UPWR_SG_EXCEPT, callb);
  760. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_SET_MIPI_DSI_ENA);
  761. txmsg.hdr.domain = (uint32_t)domain;
  762. txmsg.hdr.arg = (uint32_t)enable;
  763. upwr_srv_req(UPWR_SG_EXCEPT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  764. return 0;
  765. }
  766. /**
  767. * upwr_xcp_get_mipi_dsi_ena() - M33/A35 can use this API to get mipi dsi ena status
  768. * @domain: identifier of the caller domain.
  769. * soc_domain_t found in upower_soc_defs.h.
  770. * @callb: NULL
  771. *
  772. * A callback may not be registered (NULL pointer), in which case polling has
  773. * to be used to check the response, by calling upwr_req_status or
  774. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  775. *
  776. * Context: no sleep, no locks taken/released.
  777. * Return: 0 if ok,
  778. * -1 if service group is busy,
  779. * -3 if called in an invalid API state
  780. */
  781. int upwr_xcp_get_mipi_dsi_ena(soc_domain_t domain, const upwr_callb callb)
  782. {
  783. upwr_xcp_get_mipi_dsi_ena_msg txmsg = {0};
  784. if (api_state != UPWR_API_READY) {
  785. return -3;
  786. }
  787. if (UPWR_SG_BUSY(UPWR_SG_EXCEPT)) {
  788. return -1;
  789. }
  790. UPWR_USR_CALLB(UPWR_SG_EXCEPT, callb);
  791. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_GET_MIPI_DSI_ENA);
  792. txmsg.hdr.domain = (uint32_t)domain;
  793. upwr_srv_req(UPWR_SG_EXCEPT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  794. return 0;
  795. }
  796. /**
  797. * upwr_xcp_set_osc_mode() - M33/A35 can use this API to set uPower OSC mode
  798. * @domain: identifier of the caller domain.
  799. * soc_domain_t found in upower_soc_defs.h.
  800. * @osc_mode, 0 means low frequency, not 0 means high frequency.
  801. * @callb: NULL
  802. *
  803. * A callback may not be registered (NULL pointer), in which case polling has
  804. * to be used to check the response, by calling upwr_req_status or
  805. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  806. *
  807. * Context: no sleep, no locks taken/released.
  808. * Return: 0 if ok,
  809. * -1 if service group is busy,
  810. * -3 if called in an invalid API state
  811. */
  812. int upwr_xcp_set_osc_mode(soc_domain_t domain,
  813. uint32_t osc_mode,
  814. const upwr_callb callb)
  815. {
  816. upwr_xcp_set_osc_mode_msg txmsg = {0};
  817. if (api_state != UPWR_API_READY) {
  818. return -3;
  819. }
  820. if (UPWR_SG_BUSY(UPWR_SG_EXCEPT)) {
  821. return -1;
  822. }
  823. UPWR_USR_CALLB(UPWR_SG_EXCEPT, callb);
  824. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_SET_OSC_MODE);
  825. txmsg.hdr.domain = (uint32_t)domain;
  826. txmsg.hdr.arg = (uint32_t)osc_mode;
  827. upwr_srv_req(UPWR_SG_EXCEPT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  828. return 0;
  829. }
  830. /**
  831. * upwr_xcp_set_rtd_use_ddr() - M33 call this API to inform uPower, M33 is using ddr
  832. * @domain: identifier of the caller domain.
  833. * soc_domain_t found in upower_soc_defs.h.
  834. * @is_use_ddr: not 0, true, means that RTD is using ddr. 0, false, means that, RTD
  835. * is not using ddr.
  836. * @callb: NULL
  837. *
  838. * A callback may not be registered (NULL pointer), in which case polling has
  839. * to be used to check the response, by calling upwr_req_status or
  840. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  841. *
  842. * Context: no sleep, no locks taken/released.
  843. * Return: 0 if ok,
  844. * -1 if service group is busy,
  845. * -3 if called in an invalid API state
  846. */
  847. int upwr_xcp_set_rtd_use_ddr(soc_domain_t domain,
  848. uint32_t is_use_ddr,
  849. const upwr_callb callb)
  850. {
  851. upwr_xcp_rtd_use_ddr_msg txmsg = {0};
  852. if (api_state != UPWR_API_READY) {
  853. return -3;
  854. }
  855. if (UPWR_SG_BUSY(UPWR_SG_EXCEPT)) {
  856. return -1;
  857. }
  858. UPWR_USR_CALLB(UPWR_SG_EXCEPT, callb);
  859. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_SET_RTD_USE_DDR);
  860. txmsg.hdr.domain = (uint32_t)domain;
  861. txmsg.hdr.arg = (uint32_t)is_use_ddr;
  862. upwr_srv_req(UPWR_SG_EXCEPT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  863. return 0;
  864. }
  865. /**
  866. * upwr_xcp_set_rtd_apd_llwu() - M33/A35 can use this API to set/clear rtd_llwu apd_llwu
  867. * @domain: set which domain (RTD_DOMAIN, APD_DOMAIN) LLWU.
  868. * soc_domain_t found in upower_soc_defs.h.
  869. * @enable: true, means that set rtd_llwu or apd_llwu, false clear rtd_llwu or apd_llwu.
  870. * @callb: NULL
  871. *
  872. * A callback may not be registered (NULL pointer), in which case polling has
  873. * to be used to check the response, by calling upwr_req_status or
  874. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  875. *
  876. * Context: no sleep, no locks taken/released.
  877. * Return: 0 if ok,
  878. * -1 if service group is busy,
  879. * -3 if called in an invalid API state
  880. */
  881. int upwr_xcp_set_rtd_apd_llwu(soc_domain_t domain,
  882. uint32_t enable,
  883. const upwr_callb callb)
  884. {
  885. upwr_xcp_rtd_apd_llwu_msg txmsg = {0};
  886. if (api_state != UPWR_API_READY) {
  887. return -3;
  888. }
  889. if (UPWR_SG_BUSY(UPWR_SG_EXCEPT)) {
  890. return -1;
  891. }
  892. UPWR_USR_CALLB(UPWR_SG_EXCEPT, callb);
  893. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_SET_RTD_APD_LLWU);
  894. txmsg.hdr.domain = (uint32_t)domain;
  895. txmsg.hdr.arg = (uint32_t)enable;
  896. upwr_srv_req(UPWR_SG_EXCEPT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  897. return 0;
  898. }
  899. /**
  900. * upwr_xcp_shutdown() - Shuts down all uPower services and power mode tasks.
  901. * @callb: pointer to the callback to be called when the uPower has finished
  902. * the shutdown, or NULL if no callback needed
  903. * (polling used instead).
  904. *
  905. * A callback can be optionally registered, and will be called upon the arrival
  906. * of the request response from the uPower firmware, telling if it succeeded or
  907. * not.
  908. * A callback may not be registered (NULL pointer), in which case polling has
  909. * to be used to check the response, by calling upwr_req_status or
  910. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  911. *
  912. * At the callback the uPower/API is back to initialization/start-up phase,
  913. * so service request calls return error.
  914. *
  915. * Context: no sleep, no locks taken/released.
  916. * Return: 0 if ok,
  917. * -1 if service group is busy,
  918. * -3 if called in an invalid API state
  919. */
  920. int upwr_xcp_shutdown(const upwr_callb callb)
  921. {
  922. upwr_xcp_shutdown_msg txmsg = {0};
  923. if (api_state != UPWR_API_READY) {
  924. return -3;
  925. }
  926. if (UPWR_SG_BUSY(UPWR_SG_EXCEPT)) {
  927. return -1;
  928. }
  929. UPWR_USR_CALLB(UPWR_SG_EXCEPT, callb);
  930. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_SHUTDOWN);
  931. upwr_srv_req(UPWR_SG_EXCEPT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  932. api_state = UPWR_API_SHUTDOWN_WAIT;
  933. return 0;
  934. }
  935. /**
  936. * upwr_xcp_i2c_access() - Performs an access through the uPower I2C interface.
  937. * @addr: I2C slave address, up to 10 bits.
  938. * @data_size: determines the access direction and data size in bytes, up to 4;
  939. * negetive data_size determines a read access with size -data_size;
  940. * positive data_size determines a write access with size data_size;
  941. * data_size=0 is invalid, making the service return error UPWR_RESP_BAD_REQ.
  942. * @subaddr_size: size of the sub-address in bytes, up to 4; if subaddr_size=0,
  943. * no subaddress is used.
  944. * @subaddr: sub-address, only used if subaddr_size > 0.
  945. * @wdata: write data, up to 4 bytes; ignored if data_size < 0 (read)
  946. * @callb: pointer to the callback to be called when the uPower has finished
  947. * the access, or NULL if no callback needed
  948. * (polling used instead).
  949. *
  950. * A callback can be optionally registered, and will be called upon the arrival
  951. * of the request response from the uPower firmware, telling if it succeeded or
  952. * not.
  953. * A callback may not be registered (NULL pointer), in which case polling has
  954. * to be used to check the response, by calling upwr_req_status or
  955. * upwr_poll_req_status, using UPWR_SG_EXCEPT as the service group argument.
  956. *
  957. * The service performs a read (data_size < 0) or a write (data_size > 0) of
  958. * up to 4 bytes on the uPower I2C interface. The data read from I2C comes via
  959. * the callback argument ret, or written to the variable pointed by retptr,
  960. * if polling is used (calls upwr_req_status or upwr_poll_req_status).
  961. * ret (or *retptr) also returns the data written on writes.
  962. *
  963. * Sub-addressing is supported, with sub-address size determined by the argument
  964. * subaddr_size, up to 4 bytes. Sub-addressing is not used if subaddr_size=0.
  965. *
  966. * Context: no sleep, no locks taken/released.
  967. * Return: 0 if ok,
  968. * -1 if service group is busy,
  969. * -3 if called in an invalid API state
  970. */
  971. int upwr_xcp_i2c_access(uint16_t addr,
  972. int8_t data_size,
  973. uint8_t subaddr_size,
  974. uint32_t subaddr,
  975. uint32_t wdata,
  976. const upwr_callb callb)
  977. {
  978. unsigned long ptrval = (unsigned long)sh_buffer[UPWR_SG_EXCEPT];
  979. upwr_i2c_access *i2c_acc_ptr = (upwr_i2c_access *)ptrval;
  980. upwr_pwm_pmiccfg_msg txmsg = {0};
  981. if (api_state != UPWR_API_READY) {
  982. return -3;
  983. }
  984. if (UPWR_SG_BUSY(UPWR_SG_EXCEPT)) {
  985. return -1;
  986. }
  987. UPWR_USR_CALLB(UPWR_SG_EXCEPT, callb);
  988. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_EXCEPT, UPWR_XCP_I2C);
  989. i2c_acc_ptr->addr = addr;
  990. i2c_acc_ptr->subaddr = subaddr;
  991. i2c_acc_ptr->subaddr_size = subaddr_size;
  992. i2c_acc_ptr->data = wdata;
  993. i2c_acc_ptr->data_size = data_size;
  994. txmsg.ptr = upwr_ptr2offset(ptrval,
  995. UPWR_SG_EXCEPT,
  996. (size_t)sizeof(upwr_i2c_access),
  997. 0U,
  998. i2c_acc_ptr);
  999. upwr_srv_req(UPWR_SG_EXCEPT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1000. return 0;
  1001. }
  1002. /**---------------------------------------------------------------
  1003. * VOLTAGE MANAGERMENT SERVICE GROUP
  1004. */
  1005. /**
  1006. * upwr_vtm_pmic_cold_reset() -request cold reset the pmic.
  1007. * pmic will power cycle all the regulators
  1008. * @callb: response callback pointer; NULL if no callback needed.
  1009. *
  1010. * The function requests uPower to cold reset the pmic.
  1011. * The request is executed if arguments are within range, with no protections
  1012. * regarding the adequate voltage value for the given domain process,
  1013. * temperature and frequency.
  1014. *
  1015. * A callback can be optionally registered, and will be called upon the arrival
  1016. * of the request response from the uPower firmware, telling if it succeeded
  1017. * or not.
  1018. *
  1019. * A callback may not be registered (NULL pointer), in which case polling has
  1020. * to be used to check the response, by calling upwr_req_status or
  1021. * upwr_poll_req_status, using UPWR_SG_VOLTM as the service group argument.
  1022. *
  1023. * Context: no sleep, no locks taken/released.
  1024. * Return: 0 if ok,
  1025. * -1 if service group is busy,
  1026. * -3 if called in an invalid API state
  1027. * Note that this is not the error response from the request itself:
  1028. * it only tells if the request was successfully sent to the uPower.
  1029. */
  1030. int upwr_vtm_pmic_cold_reset(upwr_callb callb)
  1031. {
  1032. upwr_volt_pmic_cold_reset_msg txmsg = {0};
  1033. if (api_state != UPWR_API_READY) {
  1034. return -3;
  1035. }
  1036. if (UPWR_SG_BUSY(UPWR_SG_VOLTM)) {
  1037. return -1;
  1038. }
  1039. UPWR_USR_CALLB(UPWR_SG_VOLTM, callb);
  1040. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_VOLTM, UPWR_VTM_PMIC_COLD_RESET);
  1041. upwr_srv_req(UPWR_SG_VOLTM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1042. return 0;
  1043. }
  1044. /**
  1045. * upwr_vtm_set_pmic_mode() -request uPower set pmic mode
  1046. * @pmic_mode: the target mode need to be set
  1047. * @callb: response callback pointer; NULL if no callback needed.
  1048. *
  1049. * The function requests uPower to set pmic mode
  1050. * The request is executed if arguments are within range, with no protections
  1051. * regarding the adequate voltage value for the given domain process,
  1052. * temperature and frequency.
  1053. *
  1054. * A callback can be optionally registered, and will be called upon the arrival
  1055. * of the request response from the uPower firmware, telling if it succeeded
  1056. * or not.
  1057. *
  1058. * A callback may not be registered (NULL pointer), in which case polling has
  1059. * to be used to check the response, by calling upwr_req_status or
  1060. * upwr_poll_req_status, using UPWR_SG_VOLTM as the service group argument.
  1061. *
  1062. * Context: no sleep, no locks taken/released.
  1063. * Return: 0 if ok,
  1064. * -1 if service group is busy,
  1065. * -3 if called in an invalid API state
  1066. * Note that this is not the error response from the request itself:
  1067. * it only tells if the request was successfully sent to the uPower.
  1068. */
  1069. int upwr_vtm_set_pmic_mode(uint32_t pmic_mode, upwr_callb callb)
  1070. {
  1071. upwr_volt_pmic_set_mode_msg txmsg = {0};
  1072. if (api_state != UPWR_API_READY) {
  1073. return -3;
  1074. }
  1075. if (UPWR_SG_BUSY(UPWR_SG_VOLTM)) {
  1076. return -1;
  1077. }
  1078. UPWR_USR_CALLB(UPWR_SG_VOLTM, callb);
  1079. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_VOLTM, UPWR_VTM_SET_PMIC_MODE);
  1080. txmsg.hdr.arg = pmic_mode;
  1081. upwr_srv_req(UPWR_SG_VOLTM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1082. return 0;
  1083. }
  1084. /**
  1085. * upwr_vtm_chng_pmic_voltage() - Changes the voltage of a given rail.
  1086. * @rail: pmic rail id.
  1087. * @volt: the target voltage of the given rail, accurate to uV
  1088. * If pass volt value 0, means that power off this rail.
  1089. * @callb: response callback pointer; NULL if no callback needed.
  1090. *
  1091. * The function requests uPower to change the voltage of the given rail.
  1092. * The request is executed if arguments are within range, with no protections
  1093. * regarding the adequate voltage value for the given domain process,
  1094. * temperature and frequency.
  1095. *
  1096. * A callback can be optionally registered, and will be called upon the arrival
  1097. * of the request response from the uPower firmware, telling if it succeeded
  1098. * or not.
  1099. *
  1100. * A callback may not be registered (NULL pointer), in which case polling has
  1101. * to be used to check the response, by calling upwr_req_status or
  1102. * upwr_poll_req_status, using UPWR_SG_VOLTM as the service group argument.
  1103. *
  1104. * Context: no sleep, no locks taken/released.
  1105. * Return: 0 if ok,
  1106. * -1 if service group is busy,
  1107. * -3 if called in an invalid API state
  1108. * Note that this is not the error response from the request itself:
  1109. * it only tells if the request was successfully sent to the uPower.
  1110. */
  1111. int upwr_vtm_chng_pmic_voltage(uint32_t rail, uint32_t volt, upwr_callb callb)
  1112. {
  1113. upwr_volt_pmic_set_volt_msg txmsg = {0};
  1114. if (api_state != UPWR_API_READY) {
  1115. return -3;
  1116. }
  1117. if (UPWR_SG_BUSY(UPWR_SG_VOLTM)) {
  1118. return -1;
  1119. }
  1120. UPWR_USR_CALLB(UPWR_SG_VOLTM, callb);
  1121. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_VOLTM, UPWR_VTM_CHNG_PMIC_RAIL_VOLT);
  1122. txmsg.args.rail = rail;
  1123. txmsg.args.volt = (volt + PMIC_VOLTAGE_MIN_STEP - 1U) / PMIC_VOLTAGE_MIN_STEP;
  1124. upwr_srv_req(UPWR_SG_VOLTM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1125. return 0;
  1126. }
  1127. /**
  1128. * upwr_vtm_get_pmic_voltage() - Get the voltage of a given rail.
  1129. * @rail: pmic rail id.
  1130. * @callb: response callback pointer; NULL if no callback needed.
  1131. * (polling used instead)
  1132. *
  1133. * The function requests uPower to get the voltage of the given rail.
  1134. * The request is executed if arguments are within range, with no protections
  1135. * regarding the adequate voltage value for the given domain process,
  1136. * temperature and frequency.
  1137. *
  1138. * A callback can be optionally registered, and will be called upon the arrival
  1139. * of the request response from the uPower firmware, telling if it succeeded
  1140. * or not.
  1141. *
  1142. * A callback may not be registered (NULL pointer), in which case polling has
  1143. * to be used to check the response, by calling upwr_req_status or
  1144. * upwr_poll_req_status, using UPWR_SG_VOLTM as the service group argument.
  1145. *
  1146. * The voltage data read from uPower via
  1147. * the callback argument ret, or written to the variable pointed by retptr,
  1148. * if polling is used (calls upwr_req_status or upwr_poll_req_status).
  1149. * ret (or *retptr) also returns the data written on writes.
  1150. *
  1151. * Context: no sleep, no locks taken/released.
  1152. * Return: 0 if ok,
  1153. * -1 if service group is busy,
  1154. * -3 if called in an invalid API state
  1155. * Note that this is not the error response from the request itself:
  1156. * it only tells if the request was successfully sent to the uPower.
  1157. */
  1158. int upwr_vtm_get_pmic_voltage(uint32_t rail, upwr_callb callb)
  1159. {
  1160. upwr_volt_pmic_get_volt_msg txmsg = {0};
  1161. if (api_state != UPWR_API_READY) {
  1162. return -3;
  1163. }
  1164. if (UPWR_SG_BUSY(UPWR_SG_VOLTM)) {
  1165. return -1;
  1166. }
  1167. UPWR_USR_CALLB(UPWR_SG_VOLTM, callb);
  1168. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_VOLTM, UPWR_VTM_GET_PMIC_RAIL_VOLT);
  1169. txmsg.args.rail = rail;
  1170. upwr_srv_req(UPWR_SG_VOLTM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1171. return 0;
  1172. }
  1173. /**
  1174. * upwr_vtm_power_measure() - request uPower to measure power consumption
  1175. * @ssel: This field determines which power switches will have their currents
  1176. * sampled to be accounted for a
  1177. * current/power measurement. Support 0~7
  1178. * SSEL bit # Power Switch
  1179. * 0 M33 core complex/platform/peripherals
  1180. * 1 Fusion Core and Peripherals
  1181. * 2 A35[0] core complex
  1182. * 3 A35[1] core complex
  1183. * 4 3DGPU
  1184. * 5 HiFi4
  1185. * 6 DDR Controller (PHY and PLL NOT included)
  1186. * 7 PXP, EPDC
  1187. *
  1188. * @callb: response callback pointer; NULL if no callback needed.
  1189. * (polling used instead)
  1190. *
  1191. * The function requests uPower to measure power consumption
  1192. * The request is executed if arguments are within range, with no protections
  1193. * regarding the adequate voltage value for the given domain process,
  1194. * temperature and frequency.
  1195. *
  1196. * A callback can be optionally registered, and will be called upon the arrival
  1197. * of the request response from the uPower firmware, telling if it succeeded
  1198. * or not.
  1199. *
  1200. * A callback may not be registered (NULL pointer), in which case polling has
  1201. * to be used to check the response, by calling upwr_req_status or
  1202. * upwr_poll_req_status, using UPWR_SG_VOLTM as the service group argument.
  1203. *
  1204. * The power consumption data read from uPower via
  1205. * the callback argument ret, or written to the variable pointed by retptr,
  1206. * if polling is used (calls upwr_req_status or upwr_poll_req_status).
  1207. * ret (or *retptr) also returns the data written on writes.
  1208. * upower fw needs support cocurrent request from M33 and A35.
  1209. *
  1210. * Accurate to uA
  1211. *
  1212. * Context: no sleep, no locks taken/released.
  1213. * Return: 0 if ok,
  1214. * -1 if service group is busy,
  1215. * -3 if called in an invalid API state
  1216. * Note that this is not the error response from the request itself:
  1217. * it only tells if the request was successfully sent to the uPower.
  1218. */
  1219. int upwr_vtm_power_measure(uint32_t ssel, upwr_callb callb)
  1220. {
  1221. upwr_volt_pmeter_meas_msg txmsg = {0};
  1222. if (api_state != UPWR_API_READY) {
  1223. return -3;
  1224. }
  1225. if (UPWR_SG_BUSY(UPWR_SG_VOLTM)) {
  1226. return -1;
  1227. }
  1228. UPWR_USR_CALLB(UPWR_SG_VOLTM, callb);
  1229. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_VOLTM, UPWR_VTM_PMETER_MEAS);
  1230. txmsg.hdr.arg = ssel;
  1231. upwr_srv_req(UPWR_SG_VOLTM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1232. return 0;
  1233. }
  1234. /**
  1235. * upwr_vtm_vmeter_measure() - request uPower to measure voltage
  1236. * @vdetsel: Voltage Detector Selector, support 0~3
  1237. * 00b - RTD sense point
  1238. 01b - LDO output
  1239. 10b - APD domain sense point
  1240. 11b - AVD domain sense point
  1241. Refer to upower_defs.h
  1242. * @callb: response callback pointer; NULL if no callback needed.
  1243. * (polling used instead)
  1244. *
  1245. * The function requests uPower to use vmeter to measure voltage
  1246. * The request is executed if arguments are within range, with no protections
  1247. * regarding the adequate voltage value for the given domain process,
  1248. * temperature and frequency.
  1249. *
  1250. * A callback can be optionally registered, and will be called upon the arrival
  1251. * of the request response from the uPower firmware, telling if it succeeded
  1252. * or not.
  1253. *
  1254. * A callback may not be registered (NULL pointer), in which case polling has
  1255. * to be used to check the response, by calling upwr_req_status or
  1256. * upwr_poll_req_status, using UPWR_SG_VOLTM as the service group argument.
  1257. *
  1258. * The voltage data read from uPower via
  1259. * the callback argument ret, or written to the variable pointed by retptr,
  1260. * if polling is used (calls upwr_req_status or upwr_poll_req_status).
  1261. * ret (or *retptr) also returns the data written on writes.
  1262. * upower fw needs support cocurrent request from M33 and A35.
  1263. *
  1264. * Refer to RM COREREGVL (Core Regulator Voltage Level)
  1265. * uPower return VDETLVL to user, user can calculate the real voltage:
  1266. *
  1267. 0b000000(0x00) - 0.595833V
  1268. 0b100110(0x26) - 1.007498V
  1269. <value> - 0.595833V + <value>x10.8333mV
  1270. 0b110010(0x32) - 1.138V
  1271. *
  1272. * Context: no sleep, no locks taken/released.
  1273. * Return: 0 if ok,
  1274. * -1 if service group is busy,
  1275. * -3 if called in an invalid API state
  1276. * Note that this is not the error response from the request itself:
  1277. * it only tells if the request was successfully sent to the uPower.
  1278. */
  1279. int upwr_vtm_vmeter_measure(uint32_t vdetsel, upwr_callb callb)
  1280. {
  1281. upwr_volt_vmeter_meas_msg txmsg = {0};
  1282. if (api_state != UPWR_API_READY) {
  1283. return -3;
  1284. }
  1285. if (UPWR_SG_BUSY(UPWR_SG_VOLTM)) {
  1286. return -1;
  1287. }
  1288. UPWR_USR_CALLB(UPWR_SG_VOLTM, callb);
  1289. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_VOLTM, UPWR_VTM_VMETER_MEAS);
  1290. txmsg.hdr.arg = vdetsel;
  1291. upwr_srv_req(UPWR_SG_VOLTM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1292. return 0;
  1293. }
  1294. /**
  1295. * upwr_vtm_pmic_config() - Configures the SoC PMIC (Power Management IC).
  1296. * @config: pointer to a PMIC-dependent struct defining the PMIC configuration.
  1297. * @size: size of the struct pointed by config, in bytes.
  1298. * @callb: pointer to the callback called when configurations are applied.
  1299. * NULL if no callback is required.
  1300. *
  1301. * The function requests uPower to change/define the PMIC configuration.
  1302. *
  1303. * A callback can be optionally registered, and will be called upon the arrival
  1304. * of the request response from the uPower firmware, telling if it succeeded
  1305. * or not.
  1306. *
  1307. * A callback may not be registered (NULL pointer), in which case polling has
  1308. * to be used to check the response, by calling upwr_req_status or
  1309. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  1310. *
  1311. * Context: no sleep, no locks taken/released.
  1312. * Return: 0 if ok, -1 if service group is busy,
  1313. * -2 if the pointer conversion to physical address failed,
  1314. * -3 if called in an invalid API state.
  1315. * Note that this is not the error response from the request itself:
  1316. * it only tells if the request was successfully sent to the uPower.
  1317. */
  1318. int upwr_vtm_pmic_config(const void *config, uint32_t size, upwr_callb callb)
  1319. {
  1320. upwr_pwm_pmiccfg_msg txmsg = {0};
  1321. unsigned long ptrval = 0UL; /* needed for X86, ARM64 */
  1322. if (api_state != UPWR_API_READY) {
  1323. return -3;
  1324. }
  1325. if (UPWR_SG_BUSY(UPWR_SG_VOLTM)) {
  1326. return -1;
  1327. }
  1328. UPWR_USR_CALLB(UPWR_SG_VOLTM, callb);
  1329. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_VOLTM, UPWR_VTM_PMIC_CONFIG);
  1330. ptrval = (unsigned long)os_ptr2phy(config);
  1331. if (ptrval == 0UL) {
  1332. return -2; /* pointer conversion failed */
  1333. }
  1334. txmsg.ptr = upwr_ptr2offset(ptrval,
  1335. UPWR_SG_VOLTM,
  1336. (size_t)size,
  1337. 0U,
  1338. config);
  1339. upwr_srv_req(UPWR_SG_VOLTM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1340. return 0;
  1341. }
  1342. /**---------------------------------------------------------------
  1343. * TEMPERATURE MANAGEMENT SERVICE GROUP
  1344. */
  1345. /**
  1346. * upwr_tpm_get_temperature() - request uPower to get temperature of one temperature sensor
  1347. * @sensor_id: temperature sensor ID, support 0~2
  1348. * @callb: response callback pointer; NULL if no callback needed.
  1349. * (polling used instead)
  1350. *
  1351. * The function requests uPower to measure temperature
  1352. * The request is executed if arguments are within range, with no protections
  1353. * regarding the adequate voltage value for the given domain process,
  1354. * temperature and frequency.
  1355. *
  1356. * A callback can be optionally registered, and will be called upon the arrival
  1357. * of the request response from the uPower firmware, telling if it succeeded
  1358. * or not.
  1359. *
  1360. * A callback may not be registered (NULL pointer), in which case polling has
  1361. * to be used to check the response, by calling upwr_req_status or
  1362. * upwr_poll_req_status, using UPWR_SG_TEMPM as the service group argument.
  1363. *
  1364. * The temperature data read from uPower via
  1365. * the callback argument ret, or written to the variable pointed by retptr,
  1366. * if polling is used (calls upwr_req_status or upwr_poll_req_status).
  1367. * ret (or *retptr) also returns the data written on writes.
  1368. *
  1369. * uPower return TSEL to the caller (M33 or A35), caller calculate the real temperature
  1370. * Tsh = 0.000002673049*TSEL[7:0]^3 + 0.0003734262*TSEL[7:0]^2 +
  1371. 0.4487042*TSEL[7:0] - 46.98694
  1372. *
  1373. * upower fw needs support cocurrent request from M33 and A35.
  1374. *
  1375. * Context: no sleep, no locks taken/released.
  1376. * Return: 0 if ok,
  1377. * -1 if service group is busy,
  1378. * -3 if called in an invalid API state
  1379. * Note that this is not the error response from the request itself:
  1380. * it only tells if the request was successfully sent to the uPower.
  1381. */
  1382. int upwr_tpm_get_temperature(uint32_t sensor_id, upwr_callb callb)
  1383. {
  1384. upwr_temp_get_cur_temp_msg txmsg = {0};
  1385. if (api_state != UPWR_API_READY) {
  1386. return -3;
  1387. }
  1388. if (UPWR_SG_BUSY(UPWR_SG_TEMPM)) {
  1389. return -1;
  1390. }
  1391. UPWR_USR_CALLB(UPWR_SG_TEMPM, callb);
  1392. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_TEMPM, UPWR_TEMP_GET_CUR_TEMP);
  1393. txmsg.args.sensor_id = sensor_id;
  1394. upwr_srv_req(UPWR_SG_TEMPM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1395. return 0;
  1396. }
  1397. /**---------------------------------------------------------------
  1398. * DELAY MANAGEMENT SERVICE GROUP
  1399. */
  1400. /**
  1401. * upwr_dlm_get_delay_margin() - request uPower to get delay margin
  1402. * @path: The critical path
  1403. * @index: Use whitch delay meter
  1404. * @callb: response callback pointer; NULL if no callback needed.
  1405. * (polling used instead)
  1406. *
  1407. * The function requests uPower to get delay margin
  1408. * The request is executed if arguments are within range, with no protections
  1409. * regarding the adequate voltage value for the given domain process,
  1410. * temperature and frequency.
  1411. *
  1412. * A callback can be optionally registered, and will be called upon the arrival
  1413. * of the request response from the uPower firmware, telling if it succeeded
  1414. * or not.
  1415. *
  1416. * A callback may not be registered (NULL pointer), in which case polling has
  1417. * to be used to check the response, by calling upwr_req_status or
  1418. * upwr_poll_req_status, using UPWR_SG_DELAYM as the service group argument.
  1419. *
  1420. * The delay margin data read from uPower via
  1421. * the callback argument ret, or written to the variable pointed by retptr,
  1422. * if polling is used (calls upwr_req_status or upwr_poll_req_status).
  1423. * ret (or *retptr) also returns the data written on writes.
  1424. * upower fw needs support cocurrent request from M33 and A35.
  1425. *
  1426. * Context: no sleep, no locks taken/released.
  1427. * Return: 0 if ok,
  1428. * -1 if service group is busy,
  1429. * -3 if called in an invalid API state
  1430. * Note that this is not the error response from the request itself:
  1431. * it only tells if the request was successfully sent to the uPower.
  1432. */
  1433. int upwr_dlm_get_delay_margin(uint32_t path, uint32_t index, upwr_callb callb)
  1434. {
  1435. upwr_dmeter_get_delay_margin_msg txmsg = {0};
  1436. if (api_state != UPWR_API_READY) {
  1437. return -3;
  1438. }
  1439. if (UPWR_SG_BUSY(UPWR_SG_DELAYM)) {
  1440. return -1;
  1441. }
  1442. UPWR_USR_CALLB(UPWR_SG_DELAYM, callb);
  1443. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_DELAYM, UPWR_DMETER_GET_DELAY_MARGIN);
  1444. txmsg.args.path = path;
  1445. txmsg.args.index = index;
  1446. upwr_srv_req(UPWR_SG_DELAYM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1447. return 0;
  1448. }
  1449. /**
  1450. * upwr_dlm_set_delay_margin() - request uPower to set delay margin
  1451. * @path: The critical path
  1452. * @index: Use whitch delay meter
  1453. * @delay_margin: the value of delay margin
  1454. * @callb: response callback pointer; NULL if no callback needed.
  1455. * (polling used instead)
  1456. *
  1457. * The function requests uPower to set delay margin
  1458. * The request is executed if arguments are within range, with no protections
  1459. * regarding the adequate voltage value for the given domain process,
  1460. * temperature and frequency.
  1461. *
  1462. * A callback can be optionally registered, and will be called upon the arrival
  1463. * of the request response from the uPower firmware, telling if it succeeded
  1464. * or not.
  1465. *
  1466. * A callback may not be registered (NULL pointer), in which case polling has
  1467. * to be used to check the response, by calling upwr_req_status or
  1468. * upwr_poll_req_status, using UPWR_SG_DELAYM as the service group argument.
  1469. *
  1470. * The result of the corresponding critical path, failed or not read from uPower via
  1471. * the callback argument ret, or written to the variable pointed by retptr,
  1472. * if polling is used (calls upwr_req_status or upwr_poll_req_status).
  1473. * ret (or *retptr) also returns the data written on writes.
  1474. * upower fw needs support cocurrent request from M33 and A35.
  1475. *
  1476. * Context: no sleep, no locks taken/released.
  1477. * Return: 0 if ok,
  1478. * -1 if service group is busy,
  1479. * -3 if called in an invalid API state
  1480. * Note that this is not the error response from the request itself:
  1481. * it only tells if the request was successfully sent to the uPower.
  1482. */
  1483. int upwr_dlm_set_delay_margin(uint32_t path, uint32_t index, uint32_t delay_margin,
  1484. upwr_callb callb)
  1485. {
  1486. upwr_dmeter_set_delay_margin_msg txmsg = {0};
  1487. if (api_state != UPWR_API_READY) {
  1488. return -3;
  1489. }
  1490. if (UPWR_SG_BUSY(UPWR_SG_DELAYM)) {
  1491. return -1;
  1492. }
  1493. UPWR_USR_CALLB(UPWR_SG_DELAYM, callb);
  1494. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_DELAYM, UPWR_DMETER_SET_DELAY_MARGIN);
  1495. txmsg.args.path = path;
  1496. txmsg.args.index = index;
  1497. txmsg.args.dm = delay_margin;
  1498. upwr_srv_req(UPWR_SG_DELAYM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1499. return 0;
  1500. }
  1501. /**
  1502. * upwr_dlm_process_monitor() - request uPower to do process monitor
  1503. * @chain_sel: Chain Cell Type Selection
  1504. * Select the chain to be used for the clock signal generation.
  1505. * Support two types chain cell, 0~1
  1506. 0b - P4 type delay cells selected
  1507. 1b - P16 type delay cells selected
  1508. * @callb: response callback pointer; NULL if no callback needed.
  1509. * (polling used instead)
  1510. *
  1511. * The function requests uPower to do process monitor
  1512. * The request is executed if arguments are within range, with no protections
  1513. * regarding the adequate voltage value for the given domain process,
  1514. * temperature and frequency.
  1515. *
  1516. * A callback can be optionally registered, and will be called upon the arrival
  1517. * of the request response from the uPower firmware, telling if it succeeded
  1518. * or not.
  1519. *
  1520. * A callback may not be registered (NULL pointer), in which case polling has
  1521. * to be used to check the response, by calling upwr_req_status or
  1522. * upwr_poll_req_status, using UPWR_SG_DELAYM as the service group argument.
  1523. *
  1524. * The result of process monitor, failed or not read from uPower via
  1525. * the callback argument ret, or written to the variable pointed by retptr,
  1526. * if polling is used (calls upwr_req_status or upwr_poll_req_status).
  1527. * ret (or *retptr) also returns the data written on writes.
  1528. * upower fw needs support cocurrent request from M33 and A35.
  1529. *
  1530. * Context: no sleep, no locks taken/released.
  1531. * Return: 0 if ok,
  1532. * -1 if service group is busy,
  1533. * -3 if called in an invalid API state
  1534. * Note that this is not the error response from the request itself:
  1535. * it only tells if the request was successfully sent to the uPower.
  1536. */
  1537. int upwr_dlm_process_monitor(uint32_t chain_sel, upwr_callb callb)
  1538. {
  1539. upwr_pmon_msg txmsg = {0};
  1540. if (api_state != UPWR_API_READY) {
  1541. return -3;
  1542. }
  1543. if (UPWR_SG_BUSY(UPWR_SG_DELAYM)) {
  1544. return -1;
  1545. }
  1546. UPWR_USR_CALLB(UPWR_SG_DELAYM, callb);
  1547. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_DELAYM, UPWR_PMON_REQ);
  1548. txmsg.args.chain_sel = chain_sel;
  1549. upwr_srv_req(UPWR_SG_DELAYM, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1550. return 0;
  1551. }
  1552. /**---------------------------------------------------------------
  1553. * POWER MANAGEMENT SERVICE GROUP
  1554. */
  1555. /**
  1556. * upwr_pwm_dom_power_on() - Commands uPower to power on the platform of other
  1557. * domain (not necessarily its core(s)); does not release the core reset.
  1558. * @domain: identifier of the domain to power on. Defined by SoC-dependent type
  1559. * soc_domain_t found in upower_soc_defs.h.
  1560. * @boot_start: must be 1 to start the domain core(s) boot(s), releasing
  1561. * its (their) resets, or 0 otherwise.
  1562. * @pwroncallb: pointer to the callback to be called when the uPower has
  1563. * finished the power on procedure, or NULL if no callback needed
  1564. * (polling used instead).
  1565. *
  1566. * A callback can be optionally registered, and will be called upon the arrival
  1567. * of the request response from the uPower firmware, telling if it succeeded or
  1568. * not.
  1569. * A callback may not be registered (NULL pointer), in which case polling has
  1570. * to be used to check the response, by calling upwr_req_status or
  1571. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  1572. *
  1573. * Context: no sleep, no locks taken/released.
  1574. * Return: 0 if ok,
  1575. * -1 if service group is busy,
  1576. * -2 if the domain passed is the same as the caller,
  1577. * -3 if called in an invalid API state
  1578. */
  1579. int upwr_pwm_dom_power_on(soc_domain_t domain,
  1580. int boot_start,
  1581. const upwr_callb pwroncallb)
  1582. {
  1583. upwr_pwm_dom_pwron_msg txmsg = {0};
  1584. if (pwr_domain == domain) {
  1585. return -2;
  1586. }
  1587. if (api_state != UPWR_API_READY) {
  1588. return -3;
  1589. }
  1590. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  1591. return -1;
  1592. }
  1593. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, (upwr_callb)pwroncallb);
  1594. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_DOM_PWRON);
  1595. txmsg.hdr.domain = (uint32_t)domain;
  1596. txmsg.hdr.arg = (uint32_t)boot_start;
  1597. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1598. return 0;
  1599. }
  1600. /**
  1601. * upwr_pwm_boot_start() - Commands uPower to release the reset of other CPU(s),
  1602. * starting their boots.
  1603. * @domain: identifier of the domain to release the reset. Defined by
  1604. * SoC-dependent type soc_domain_t found in upower_soc_defs.h.
  1605. * @bootcallb: pointer to the callback to be called when the uPower has finished
  1606. * the boot start procedure, or NULL if no callback needed
  1607. * (polling used instead).
  1608. *
  1609. * A callback can be optionally registered, and will be called upon the arrival
  1610. * of the request response from the uPower firmware, telling if it succeeded or
  1611. * not.
  1612. * A callback may not be registered (NULL pointer), in which case polling has
  1613. * to be used to check the response, by calling upwr_req_status or
  1614. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  1615. *
  1616. * The callback calling doesn't mean the CPUs boots have finished:
  1617. * it only indicates that uPower released the CPUs resets, and can receive
  1618. * other power management service group requests.
  1619. *
  1620. * Context: no sleep, no locks taken/released.
  1621. * Return: 0 if ok,
  1622. * -1 if service group is busy,
  1623. * -2 if the domain passed is the same as the caller,
  1624. * -3 if called in an invalid API state
  1625. */
  1626. int upwr_pwm_boot_start(soc_domain_t domain, const upwr_callb bootcallb)
  1627. {
  1628. upwr_pwm_boot_start_msg txmsg = {0};
  1629. if (pwr_domain == domain) {
  1630. return -2;
  1631. }
  1632. if (api_state != UPWR_API_READY) {
  1633. return -3;
  1634. }
  1635. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  1636. return -1;
  1637. }
  1638. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, (upwr_callb)bootcallb);
  1639. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_BOOT);
  1640. txmsg.hdr.domain = (uint32_t)domain;
  1641. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1642. return 0;
  1643. }
  1644. /**
  1645. * upwr_pwm_param() - Changes Power Management parameters.
  1646. * @param: pointer to a parameter structure upwr_pwm_param_t, SoC-dependent,
  1647. * defined in upwr_soc_defines.h. NULL may be passed, meaning
  1648. * a request to read the parameter set, in which case it appears in the callback
  1649. * argument ret, or can be pointed by argument retptr in the upwr_req_status and
  1650. * upwr_poll_req_status calls, casted to upwr_pwm_param_t.
  1651. * @callb: response callback pointer; NULL if no callback needed.
  1652. *
  1653. * The return value is always the current parameter set value, either in a
  1654. * read-only request (param = NULL) or after setting a new parameter
  1655. * (non-NULL param).
  1656. *
  1657. * Some parameters may be targeted for a specific domain (see the struct
  1658. * upwr_pwm_param_t definition in upower_soc_defs.h); this call has implicit
  1659. * domain target (the same domain from which is called).
  1660. *
  1661. * A callback can be optionally registered, and will be called upon the arrival
  1662. * of the request response from the uPower firmware, telling if it succeeded or
  1663. * not.
  1664. * A callback may not be registered (NULL pointer), in which case polling has
  1665. * to be used to check the response, by calling upwr_req_status or
  1666. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  1667. *
  1668. * Context: no sleep, no locks taken/released.
  1669. * Return: 0 if ok,
  1670. * -1 if service group is busy,
  1671. * -3 if called in an invalid API state
  1672. */
  1673. int upwr_pwm_param(upwr_pwm_param_t *param, const upwr_callb callb)
  1674. {
  1675. upwr_pwm_param_msg txmsg = {0};
  1676. if (api_state != UPWR_API_READY) {
  1677. return -3;
  1678. }
  1679. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  1680. return -1;
  1681. }
  1682. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  1683. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_PARAM);
  1684. if (param == NULL) {
  1685. txmsg.hdr.arg = 1U; /* 1= read, txmsg.word2 ignored */
  1686. } else {
  1687. txmsg.hdr.arg = 0U; /* 1= write */
  1688. txmsg.word2 = param->R; /* just 1 word, so that's ok */
  1689. }
  1690. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1691. return 0;
  1692. }
  1693. /**
  1694. * upwr_pwm_chng_reg_voltage() - Changes the voltage at a given regulator.
  1695. * @reg: regulator id.
  1696. * @volt: voltage value; value unit is SoC-dependent, converted from mV by the
  1697. * macro UPWR_VTM_MILIV, or from micro-Volts by the macro UPWR_VTM_MICROV,
  1698. * both macros in upower_soc_defs.h
  1699. * @callb: response callback pointer; NULL if no callback needed.
  1700. *
  1701. * The function requests uPower to change the voltage of the given regulator.
  1702. * The request is executed if arguments are within range, with no protections
  1703. * regarding the adequate voltage value for the given domain process,
  1704. * temperature and frequency.
  1705. *
  1706. * A callback can be optionally registered, and will be called upon the arrival
  1707. * of the request response from the uPower firmware, telling if it succeeded
  1708. * or not.
  1709. *
  1710. * A callback may not be registered (NULL pointer), in which case polling has
  1711. * to be used to check the response, by calling upwr_req_status or
  1712. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  1713. *
  1714. * Context: no sleep, no locks taken/released.
  1715. * Return: 0 if ok,
  1716. * -1 if service group is busy,
  1717. * -3 if called in an invalid API state
  1718. * Note that this is not the error response from the request itself:
  1719. * it only tells if the request was successfully sent to the uPower.
  1720. */
  1721. int upwr_pwm_chng_reg_voltage(uint32_t reg, uint32_t volt, upwr_callb callb)
  1722. {
  1723. upwr_pwm_volt_msg txmsg = {0};
  1724. if (api_state != UPWR_API_READY) {
  1725. return -3;
  1726. }
  1727. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  1728. return -1;
  1729. }
  1730. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  1731. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_VOLT);
  1732. txmsg.args.reg = reg;
  1733. txmsg.args.volt = volt;
  1734. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1735. return 0;
  1736. }
  1737. /**
  1738. * upwr_pwm_freq_setup() - Determines the next frequency target for a given
  1739. * domain and current frequency.
  1740. * @domain: identifier of the domain to change frequency. Defined by
  1741. * SoC-dependent type soc_domain_t found in upower_soc_defs.h.
  1742. * @rail: the pmic regulator number for the target domain.
  1743. * @stage: DVA adjust stage
  1744. * refer to upower_defs.h "DVA adjust stage"
  1745. * @target_freq: the target adjust frequency, accurate to MHz
  1746. *
  1747. * refer to upower_defs.h structure definition upwr_pwm_freq_msg
  1748. *
  1749. * @callb: response callback pointer; NULL if no callback needed.
  1750. *
  1751. * The DVA algorithm is broken down into two phases.
  1752. * The first phase uses a look up table to get a safe operating voltage
  1753. * for the requested frequency.
  1754. * This voltage is guaranteed to work over process and temperature.
  1755. *
  1756. * The second step of the second phase is to measure the temperature
  1757. * using the uPower Temperature Sensor module.
  1758. * This is accomplished by doing a binary search of the TSEL bit field
  1759. * in the Temperature Measurement Register (TMR).
  1760. * The search is repeated until the THIGH bit fields in the same register change value.
  1761. * There are 3 temperature sensors in 8ULP (APD, AVD, and RTD).
  1762. *
  1763. *
  1764. * The second phase is the fine adjust of the voltage.
  1765. * This stage is entered only when the new frequency requested
  1766. * by application was already set as well as the voltage for that frequency.
  1767. * The first step of the fine adjust is to find what is the current margins
  1768. * for the monitored critical paths, or, in other words,
  1769. * how many delay cells will be necessary to generate a setup-timing violation.
  1770. * The function informs uPower that the given domain frequency has changed or
  1771. * will change to the given value. uPower firmware will then adjust voltage and
  1772. * bias to cope with the new frequency (if decreasing) or prepare for it
  1773. * (if increasing). The function must be called after decreasing the frequency,
  1774. * and before increasing it. The actual increase in frequency must not occur
  1775. * before the service returns its response.
  1776. *
  1777. * So, for increase clock frequency case, user need to call this API twice,
  1778. * the first stage gross adjust and the second stage fine adjust.
  1779. *
  1780. * for reduce clock frequency case, user can only call this API once,
  1781. * full stage (combine gross stage and fine adjust)
  1782. *
  1783. * The request is executed if arguments are within range.
  1784. *
  1785. * A callback can be optionally registered, and will be called upon the arrival
  1786. * of the request response from the uPower firmware, telling if it succeeded
  1787. * or not.
  1788. *
  1789. * A callback may not be registered (NULL pointer), in which case polling has
  1790. * to be used to check the response, by calling upwr_req_status or
  1791. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  1792. *
  1793. * Context: no sleep, no locks taken/released.
  1794. * Return: 0 if ok,
  1795. * -1 if service group is busy,
  1796. * -3 if called in an invalid API state
  1797. * Note that this is not the error response from the request itself:
  1798. * it only tells if the request was successfully sent to the uPower.
  1799. */
  1800. int upwr_pwm_freq_setup(soc_domain_t domain, uint32_t rail, uint32_t stage, uint32_t target_freq,
  1801. upwr_callb callb)
  1802. {
  1803. upwr_pwm_freq_msg txmsg = {0};
  1804. if (api_state != UPWR_API_READY) {
  1805. return -3;
  1806. }
  1807. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  1808. return -1;
  1809. }
  1810. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  1811. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_FREQ);
  1812. txmsg.hdr.domain = (uint32_t)domain;
  1813. txmsg.args.rail = rail;
  1814. txmsg.args.stage = stage;
  1815. txmsg.args.target_freq = target_freq;
  1816. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1817. return 0;
  1818. }
  1819. /**
  1820. * upwr_pwm_power_on()- Powers on (not off) one or more switches and ROM/RAMs.
  1821. * @swton: pointer to an array of words that tells which power switches to
  1822. * turn on. Each word in the array has 1 bit for each switch.
  1823. * A bit=1 means the respective switch must be turned on,
  1824. * bit = 0 means it will stay unchanged (on or off).
  1825. * The pointer may be set to NULL, in which case no switch will be changed,
  1826. * unless a memory that it feeds must be turned on.
  1827. * WARNING: swton must not point to the first shared memory address.
  1828. * @memon: pointer to an array of words that tells which memories to turn on.
  1829. * Each word in the array has 1 bit for each switch.
  1830. * A bit=1 means the respective memory must be turned on, both array and
  1831. * periphery logic;
  1832. * bit = 0 means it will stay unchanged (on or off).
  1833. * The pointer may be set to NULL, in which case no memory will be changed.
  1834. * WARNING: memon must not point to the first shared memory address.
  1835. * @callb: pointer to the callback called when configurations are applyed.
  1836. * NULL if no callback is required.
  1837. *
  1838. * The function requests uPower to turn on the PMC and memory array/peripheral
  1839. * switches that control their power, as specified above.
  1840. * The request is executed if arguments are within range, with no protections
  1841. * regarding the adequate memory power state related to overall system state.
  1842. *
  1843. * If a memory is requested to turn on, but the power switch that feeds that
  1844. * memory is not, the power switch will be turned on anyway, if the pwron
  1845. * array is not provided (that is, if pwron is NULL).
  1846. *
  1847. * A callback can be optionally registered, and will be called upon the arrival
  1848. * of the request response from the uPower firmware, telling if it succeeded
  1849. * or not.
  1850. *
  1851. * A callback may not be registered (NULL pointer), in which case polling has
  1852. * to be used to check the response, by calling upwr_req_status or
  1853. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  1854. *
  1855. * Callback or polling may return error if the service contends for a resource
  1856. * already being used by a power mode transition or an ongoing service in
  1857. * another domain.
  1858. *
  1859. * Context: no sleep, no locks taken/released.
  1860. * Return: 0 if ok, -1 if service group is busy,
  1861. * -2 if a pointer conversion to physical address failed,
  1862. * -3 if called in an invalid API state.
  1863. * Note that this is not the error response from the request itself:
  1864. * it only tells if the request was successfully sent to the uPower.
  1865. */
  1866. int upwr_pwm_power_on(const uint32_t swton[],
  1867. const uint32_t memon[],
  1868. upwr_callb callb)
  1869. {
  1870. upwr_pwm_pwron_msg txmsg = {0};
  1871. unsigned long ptrval = 0UL; /* needed for X86, ARM64 */
  1872. size_t stsize = 0U;
  1873. if (api_state != UPWR_API_READY) {
  1874. return -3;
  1875. }
  1876. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  1877. return -1;
  1878. }
  1879. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  1880. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_PWR_ON);
  1881. ptrval = (unsigned long)os_ptr2phy((void *)swton);
  1882. if (swton == NULL) {
  1883. txmsg.ptrs.ptr0 = 0; /* NULL pointer -> 0 offset */
  1884. } else if (ptrval == 0U) {
  1885. return -2; /* pointer conversion failed */
  1886. } else {
  1887. txmsg.ptrs.ptr0 = upwr_ptr2offset(ptrval,
  1888. UPWR_SG_PWRMGMT,
  1889. (stsize = UPWR_PMC_SWT_WORDS * 4U),
  1890. 0U,
  1891. swton);
  1892. }
  1893. ptrval = (unsigned long)os_ptr2phy((void *)memon);
  1894. if (memon == NULL) {
  1895. txmsg.ptrs.ptr1 = 0; /* NULL pointer -> 0 offset */
  1896. } else if (ptrval == 0U) {
  1897. return -2; /* pointer conversion failed */
  1898. } else {
  1899. txmsg.ptrs.ptr1 = upwr_ptr2offset(ptrval,
  1900. UPWR_SG_PWRMGMT,
  1901. UPWR_PMC_MEM_WORDS * 4U,
  1902. stsize,
  1903. memon);
  1904. }
  1905. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1906. return 0;
  1907. }
  1908. /**
  1909. * upwr_pwm_power_off()- Powers off (not on) one or more switches and ROM/RAMs.
  1910. * @swtoff: pointer to an array of words that tells which power switches to
  1911. * turn off. Each word in the array has 1 bit for each switch.
  1912. * A bit=1 means the respective switch must be turned off,
  1913. * bit = 0 means it will stay unchanged (on or off).
  1914. * The pointer may be set to NULL, in which case no switch will be changed.
  1915. * WARNING: swtoff must not point to the first shared memory address.
  1916. * @memoff: pointer to an array of words that tells which memories to turn off.
  1917. * Each word in the array has 1 bit for each switch.
  1918. * A bit=1 means the respective memory must be turned off, both array and
  1919. * periphery logic;
  1920. * bit = 0 means it will stay unchanged (on or off).
  1921. * The pointer may be set to NULL, in which case no memory will be changed,
  1922. * but notice it may be turned off if the switch that feeds it is powered off.
  1923. * WARNING: memoff must not point to the first shared memory address.
  1924. * @callb: pointer to the callback called when configurations are applyed.
  1925. * NULL if no callback is required.
  1926. *
  1927. * The function requests uPower to turn off the PMC and memory array/peripheral
  1928. * switches that control their power, as specified above.
  1929. * The request is executed if arguments are within range, with no protections
  1930. * regarding the adequate memory power state related to overall system state.
  1931. *
  1932. * A callback can be optionally registered, and will be called upon the arrival
  1933. * of the request response from the uPower firmware, telling if it succeeded
  1934. * or not.
  1935. *
  1936. * A callback may not be registered (NULL pointer), in which case polling has
  1937. * to be used to check the response, by calling upwr_req_status or
  1938. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  1939. *
  1940. * Callback or polling may return error if the service contends for a resource
  1941. * already being used by a power mode transition or an ongoing service in
  1942. * another domain.
  1943. *
  1944. * Context: no sleep, no locks taken/released.
  1945. * Return: 0 if ok, -1 if service group is busy,
  1946. * -2 if a pointer conversion to physical address failed,
  1947. * -3 if called in an invalid API state.
  1948. * Note that this is not the error response from the request itself:
  1949. * it only tells if the request was successfully sent to the uPower.
  1950. */
  1951. int upwr_pwm_power_off(const uint32_t swtoff[],
  1952. const uint32_t memoff[],
  1953. upwr_callb callb)
  1954. {
  1955. upwr_pwm_pwroff_msg txmsg = {0};
  1956. unsigned long ptrval = 0UL; /* needed for X86, ARM64 */
  1957. size_t stsize = 0;
  1958. if (api_state != UPWR_API_READY) {
  1959. return -3;
  1960. }
  1961. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  1962. return -1;
  1963. }
  1964. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  1965. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_PWR_OFF);
  1966. ptrval = (unsigned long)os_ptr2phy((void *)swtoff);
  1967. if (swtoff == NULL) {
  1968. txmsg.ptrs.ptr0 = 0; /* NULL pointer -> 0 offset */
  1969. } else if (ptrval == 0U) {
  1970. return -2; /* pointer conversion failed */
  1971. } else {
  1972. txmsg.ptrs.ptr0 = upwr_ptr2offset(ptrval,
  1973. UPWR_SG_PWRMGMT,
  1974. (stsize = UPWR_PMC_SWT_WORDS * 4U),
  1975. 0U,
  1976. swtoff);
  1977. }
  1978. ptrval = (unsigned long)os_ptr2phy((void *)memoff);
  1979. if (memoff == NULL) {
  1980. txmsg.ptrs.ptr1 = 0; /* NULL pointer -> 0 offset */
  1981. } else if (ptrval == 0U) {
  1982. return -2; /* pointer conversion failed */
  1983. } else {
  1984. txmsg.ptrs.ptr1 = upwr_ptr2offset(ptrval,
  1985. UPWR_SG_PWRMGMT,
  1986. UPWR_PMC_MEM_WORDS * 4U,
  1987. stsize,
  1988. memoff);
  1989. }
  1990. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  1991. return 0;
  1992. }
  1993. /**
  1994. * upwr_pwm_mem_retain()- Configures one or more memory power switches to
  1995. * retain its contents, having the power array on, while its peripheral logic
  1996. * is turned off.
  1997. * @mem: pointer to an array of words that tells which memories to put in a
  1998. * retention state. Each word in the array has 1 bit for each memory.
  1999. * A bit=1 means the respective memory must be put in retention state,
  2000. * bit = 0 means it will stay unchanged (retention, fully on or off).
  2001. * @callb: pointer to the callback called when configurations are applyed.
  2002. * NULL if no callback is required.
  2003. *
  2004. * The function requests uPower to turn off the memory peripheral and leave
  2005. * its array on, as specified above.
  2006. * The request is executed if arguments are within range.
  2007. *
  2008. * A callback can be optionally registered, and will be called upon the arrival
  2009. * of the request response from the uPower firmware, telling if it succeeded
  2010. * or not.
  2011. *
  2012. * A callback may not be registered (NULL pointer), in which case polling has
  2013. * to be used to check the response, by calling upwr_req_status or
  2014. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  2015. *
  2016. * Callback or polling may return error if the service contends for a resource
  2017. * already being used by a power mode transition or an ongoing service in
  2018. * another domain.
  2019. *
  2020. * Context: no sleep, no locks taken/released.
  2021. * Return: 0 if ok, -1 if service group is busy,
  2022. * -2 if a pointer conversion to physical address failed,
  2023. * -3 if called in an invalid API state.
  2024. * Note that this is not the error response from the request itself:
  2025. * it only tells if the request was successfully sent to the uPower.
  2026. */
  2027. int upwr_pwm_mem_retain(const uint32_t mem[], upwr_callb callb)
  2028. {
  2029. upwr_pwm_retain_msg txmsg = {0};
  2030. unsigned long ptrval = 0UL; /* needed for X86, ARM64 */
  2031. if (api_state != UPWR_API_READY) {
  2032. return -3;
  2033. }
  2034. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  2035. return -1;
  2036. }
  2037. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  2038. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_RETAIN);
  2039. ptrval = (unsigned long)os_ptr2phy((void *)mem);
  2040. if (ptrval == 0U) {
  2041. return -2; /* pointer conversion failed */
  2042. }
  2043. txmsg.ptr = upwr_ptr2offset(ptrval,
  2044. UPWR_SG_PWRMGMT,
  2045. UPWR_PMC_MEM_WORDS * 4U,
  2046. 0U,
  2047. mem);
  2048. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  2049. return 0;
  2050. }
  2051. /**
  2052. * upwr_pwm_chng_switch_mem() - Turns on/off power on one or more PMC switches
  2053. * and memories, including their array and peripheral logic.
  2054. * @swt: pointer to a list of PMC switches to be opened/closed.
  2055. * The list is structured as an array of struct upwr_switch_board_t
  2056. * (see upower_defs.h), each one containing a word for up to 32 switches,
  2057. * one per bit. A bit = 1 means switch closed, bit = 0 means switch open.
  2058. * struct upwr_switch_board_t also specifies a mask with 1 bit for each
  2059. * respective switch: mask bit = 1 means the open/close action is applied,
  2060. * mask bit = 0 means the switch stays unchanged.
  2061. * The pointer may be set to NULL, in which case no switch will be changed,
  2062. * unless a memory that it feeds must be turned on.
  2063. * WARNING: swt must not point to the first shared memory address.
  2064. * @mem: pointer to a list of switches to be turned on/off.
  2065. * The list is structured as an array of struct upwr_mem_switches_t
  2066. * (see upower_defs.h), each one containing 2 word for up to 32 switches,
  2067. * one per bit, one word for the RAM array power switch, other for the
  2068. * RAM peripheral logic power switch. A bit = 1 means switch closed,
  2069. * bit = 0 means switch open.
  2070. * struct upwr_mem_switches_t also specifies a mask with 1 bit for each
  2071. * respective switch: mask bit = 1 means the open/close action is applied,
  2072. * mask bit = 0 means the switch stays unchanged.
  2073. * The pointer may be set to NULL, in which case no memory switch will be
  2074. * changed, but notice it may be turned off if the switch that feeds it is
  2075. * powered off.
  2076. * WARNING: mem must not point to the first shared memory address.
  2077. * @callb: pointer to the callback called when the configurations are applied.
  2078. * NULL if no callback is required.
  2079. *
  2080. * The function requests uPower to change the PMC switches and/or memory power
  2081. * as specified above.
  2082. * The request is executed if arguments are within range, with no protections
  2083. * regarding the adequate switch combinations and overall system state.
  2084. *
  2085. * If a memory is requested to turn on, but the power switch that feeds that
  2086. * memory is not, the power switch will be turned on anyway, if the swt
  2087. * array is not provided (that is, if swt is NULL).
  2088. *
  2089. * A callback can be optionally registered, and will be called upon the arrival
  2090. * of the request response from the uPower firmware, telling if it succeeded
  2091. * or not.
  2092. *
  2093. * A callback may not be registered (NULL pointer), in which case polling has
  2094. * to be used to check the response, by calling upwr_req_status or
  2095. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  2096. *
  2097. * Callback or polling may return error if the service contends for a resource
  2098. * already being used by a power mode transition or an ongoing service in
  2099. * another domain.
  2100. *
  2101. * Context: no sleep, no locks taken/released.
  2102. * Return: 0 if ok, -1 if service group is busy.
  2103. * -2 if a pointer conversion to physical address failed,
  2104. * -3 if called in an invalid API state.
  2105. * Note that this is not the error response from the request itself:
  2106. * it only tells if the request was successfully sent to the uPower.
  2107. */
  2108. int upwr_pwm_chng_switch_mem(const struct upwr_switch_board_t swt[],
  2109. const struct upwr_mem_switches_t mem[],
  2110. upwr_callb callb)
  2111. {
  2112. upwr_pwm_switch_msg txmsg = {0};
  2113. unsigned long ptrval = 0UL; /* needed for X86, ARM64 */
  2114. size_t stsize = 0U;
  2115. if (api_state != UPWR_API_READY) {
  2116. return -3;
  2117. }
  2118. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  2119. return -1;
  2120. }
  2121. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  2122. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_SWITCH);
  2123. ptrval = (unsigned long)os_ptr2phy((void *)swt);
  2124. if (swt == NULL) {
  2125. txmsg.ptrs.ptr0 = 0; /* NULL pointer -> 0 offset */
  2126. } else if (ptrval == 0U) {
  2127. return -2; /* pointer conversion failed */
  2128. } else {
  2129. txmsg.ptrs.ptr0 = upwr_ptr2offset(ptrval,
  2130. UPWR_SG_PWRMGMT,
  2131. (stsize = UPWR_PMC_SWT_WORDS * sizeof(struct upwr_switch_board_t)),
  2132. 0U,
  2133. swt);
  2134. }
  2135. ptrval = (unsigned long)os_ptr2phy((void *)mem);
  2136. if (mem == NULL) {
  2137. txmsg.ptrs.ptr1 = 0; /* NULL pointer -> 0 offset */
  2138. } else if (ptrval == 0U) {
  2139. return -2; /* pointer conversion failed */
  2140. } else {
  2141. txmsg.ptrs.ptr1 = upwr_ptr2offset(ptrval,
  2142. UPWR_SG_PWRMGMT,
  2143. UPWR_PMC_MEM_WORDS * sizeof(struct upwr_mem_switches_t),
  2144. stsize,
  2145. mem);
  2146. }
  2147. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  2148. return 0;
  2149. }
  2150. /**
  2151. * upwr_pwm_pmode_config() - Configures a given power mode in a given domain.
  2152. * @domain: identifier of the domain to which the power mode belongs.
  2153. * Defined by SoC-dependent type soc_domain_t found in upower_soc_defs.h.
  2154. * @pmode: SoC-dependent power mode identifier defined by type abs_pwr_mode_t
  2155. * found in upower_soc_defs.h.
  2156. * @config: pointer to an SoC-dependent struct defining the power mode
  2157. * configuration, found in upower_soc_defs.h.
  2158. * @callb: pointer to the callback called when configurations are applied.
  2159. * NULL if no callback is required.
  2160. *
  2161. * The function requests uPower to change the power mode configuration as
  2162. * specified above. The request is executed if arguments are within range,
  2163. * and complies with SoC-dependent restrictions on value combinations.
  2164. *
  2165. * A callback can be optionally registered, and will be called upon the arrival
  2166. * of the request response from the uPower firmware, telling if it succeeded
  2167. * or not.
  2168. *
  2169. * A callback may not be registered (NULL pointer), in which case polling has
  2170. * to be used to check the response, by calling upwr_req_status or
  2171. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  2172. *
  2173. * Context: no sleep, no locks taken/released.
  2174. * Return: 0 if ok, -1 if service group is busy,
  2175. * -2 if the pointer conversion to physical address failed,
  2176. * -3 if called in an invalid API state.
  2177. * Note that this is not the error response from the request itself:
  2178. * it only tells if the request was successfully sent to the uPower.
  2179. */
  2180. int upwr_pwm_pmode_config(soc_domain_t domain,
  2181. abs_pwr_mode_t pmode,
  2182. const void *config,
  2183. upwr_callb callb)
  2184. {
  2185. upwr_pwm_pmode_cfg_msg txmsg = {0};
  2186. unsigned long ptrval = 0UL; /* needed for X86, ARM64 */
  2187. if (api_state != UPWR_API_READY) {
  2188. return -3;
  2189. }
  2190. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  2191. return -1;
  2192. }
  2193. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  2194. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_CONFIG);
  2195. txmsg.hdr.domain = (uint32_t)domain;
  2196. txmsg.hdr.arg = pmode;
  2197. ptrval = (unsigned long)os_ptr2phy(config);
  2198. if (ptrval == 0U) {
  2199. return -2; /* pointer conversion failed */
  2200. }
  2201. /*
  2202. * upwr_pwm_pmode_config is an exception: use the pointer
  2203. * (physical addr) as is
  2204. */
  2205. txmsg.ptr = (uint32_t)ptrval;
  2206. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  2207. return 0;
  2208. }
  2209. /**
  2210. * upwr_pwm_reg_config() - Configures the uPower internal regulators.
  2211. * @config: pointer to the struct defining the regulator configuration;
  2212. * the struct upwr_reg_config_t is defined in the file upower_defs.h.
  2213. * @callb: pointer to the callback called when configurations are applied.
  2214. * NULL if no callback is required.
  2215. *
  2216. * The function requests uPower to change/define the configurations of the
  2217. * internal regulators.
  2218. *
  2219. * A callback can be optionally registered, and will be called upon the arrival
  2220. * of the request response from the uPower firmware, telling if it succeeded
  2221. * or not.
  2222. *
  2223. * A callback may not be registered (NULL pointer), in which case polling has
  2224. * to be used to check the response, by calling upwr_req_status or
  2225. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  2226. *
  2227. * The service may fail with error UPWR_RESP_RESOURCE if a power mode transition
  2228. * or the same service (called from another domain) is executing simultaneously.
  2229. * This error should be interpreted as a "try later" response, as the service
  2230. * will succeed once those concurrent executions are done, and no other is
  2231. * started.
  2232. *
  2233. * Context: no sleep, no locks taken/released.
  2234. * Return: 0 if ok, -1 if service group is busy,
  2235. * -2 if the pointer conversion to physical address failed,
  2236. * -3 if called in an invalid API state.
  2237. * Note that this is not the error response from the request itself:
  2238. * it only tells if the request was successfully sent to the uPower.
  2239. */
  2240. int upwr_pwm_reg_config(const struct upwr_reg_config_t *config,
  2241. upwr_callb callb)
  2242. {
  2243. upwr_pwm_regcfg_msg txmsg = {0};
  2244. unsigned long ptrval = 0UL; /* needed for X86, ARM64 */
  2245. if (api_state != UPWR_API_READY) {
  2246. return -3;
  2247. }
  2248. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  2249. return -1;
  2250. }
  2251. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  2252. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_REGCFG);
  2253. ptrval = (unsigned long)os_ptr2phy(config);
  2254. if (ptrval == 0U) {
  2255. return -2; /* pointer conversion failed */
  2256. }
  2257. txmsg.ptr = upwr_ptr2offset(ptrval,
  2258. UPWR_SG_PWRMGMT,
  2259. sizeof(struct upwr_reg_config_t),
  2260. 0U,
  2261. config);
  2262. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  2263. return 0;
  2264. }
  2265. /**
  2266. * upwr_pwm_chng_dom_bias() - Changes the domain bias.
  2267. * @bias: pointer to a domain bias configuration struct (see upower_soc_defs.h).
  2268. * @callb: pointer to the callback called when configurations are applied.
  2269. * NULL if no callback is required.
  2270. *
  2271. * The function requests uPower to change the domain bias configuration as
  2272. * specified above. The request is executed if arguments are within range,
  2273. * with no protections regarding the adequate value combinations and
  2274. * overall system state.
  2275. *
  2276. * A callback can be optionally registered, and will be called upon the arrival
  2277. * of the request response from the uPower firmware, telling if it succeeded
  2278. * or not.
  2279. *
  2280. * A callback may not be registered (NULL pointer), in which case polling has
  2281. * to be used to check the response, by calling upwr_req_status or
  2282. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  2283. *
  2284. * Context: no sleep, no locks taken/released.
  2285. * Return: 0 if ok, -1 if service group is busy,
  2286. * -3 if called in an invalid API state.
  2287. * Note that this is not the error response from the request itself:
  2288. * it only tells if the request was successfully sent to the uPower.
  2289. */
  2290. int upwr_pwm_chng_dom_bias(const struct upwr_dom_bias_cfg_t *bias,
  2291. upwr_callb callb)
  2292. {
  2293. upwr_pwm_dom_bias_msg txmsg = {0};
  2294. if (api_state != UPWR_API_READY) {
  2295. return -3;
  2296. }
  2297. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  2298. return -1;
  2299. }
  2300. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  2301. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_DOM_BIAS);
  2302. /* SoC-dependent argument filling, defined in upower_soc_defs.h */
  2303. UPWR_FILL_DOMBIAS_ARGS(txmsg.hdr.domain, bias, txmsg.args);
  2304. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  2305. return 0;
  2306. }
  2307. /**
  2308. * upwr_pwm_chng_mem_bias()- Changes a ROM/RAM power bias.
  2309. * @domain: identifier of the domain upon which the bias is applied.
  2310. * Defined by SoC-dependent type soc_domain_t found in upower_soc_defs.h.
  2311. * @bias: pointer to a memory bias configuration struct (see upower_soc_defs.h).
  2312. * @callb: pointer to the callback called when configurations are applied.
  2313. * NULL if no callback is required.
  2314. *
  2315. * The function requests uPower to change the memory bias configuration as
  2316. * specified above. The request is executed if arguments are within range,
  2317. * with no protections regarding the adequate value combinations and
  2318. * overall system state.
  2319. *
  2320. * A callback can be optionally registered, and will be called upon the arrival
  2321. * of the request response from the uPower firmware, telling if it succeeded
  2322. * or not.
  2323. *
  2324. * A callback may not be registered (NULL pointer), in which case polling has
  2325. * to be used to check the response, by calling upwr_req_status or
  2326. * upwr_poll_req_status, using UPWR_SG_PWRMGMT as the service group argument.
  2327. *
  2328. * Context: no sleep, no locks taken/released.
  2329. * Return: 0 if ok, -1 if service group is busy,
  2330. * -3 if called in an invalid API state.
  2331. * Note that this is not the error response from the request itself:
  2332. * it only tells if the request was successfully sent to the uPower.
  2333. */
  2334. int upwr_pwm_chng_mem_bias(soc_domain_t domain,
  2335. const struct upwr_mem_bias_cfg_t *bias,
  2336. upwr_callb callb)
  2337. {
  2338. upwr_pwm_mem_bias_msg txmsg = {0};
  2339. if (api_state != UPWR_API_READY) {
  2340. return -3;
  2341. }
  2342. if (UPWR_SG_BUSY(UPWR_SG_PWRMGMT)) {
  2343. return -1;
  2344. }
  2345. UPWR_USR_CALLB(UPWR_SG_PWRMGMT, callb);
  2346. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_PWRMGMT, UPWR_PWM_MEM_BIAS);
  2347. txmsg.hdr.domain = (uint32_t)domain;
  2348. /* SoC-dependent argument filling, defined in upower_soc_defs.h */
  2349. UPWR_FILL_MEMBIAS_ARGS(bias, txmsg.args);
  2350. upwr_srv_req(UPWR_SG_PWRMGMT, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  2351. return 0;
  2352. }
  2353. /**---------------------------------------------------------------
  2354. * DIAGNOSE SERVICE GROUP
  2355. */
  2356. /**
  2357. * upwr_dgn_mode() - Sets the diagnostic mode.
  2358. * @mode: diagnostic mode, which can be:
  2359. * - UPWR_DGN_NONE: no diagnostic recorded
  2360. * - UPWR_DGN_TRACE: warnings, errors, service, internal activity recorded
  2361. * - UPWR_DGN_SRVREQ: warnings, errors, service activity recorded
  2362. * - UPWR_DGN_WARN: warnings and errors recorded
  2363. * - UPWR_DGN_ALL: trace, service, warnings, errors, task state recorded
  2364. * - UPWR_DGN_ERROR: only errors recorded
  2365. * - UPWR_DGN_ALL2ERR: record all until an error occurs,
  2366. * freeze recording on error
  2367. * - UPWR_DGN_ALL2HLT: record all until an error occurs,
  2368. * executes an ebreak on error, which halts the core if enabled through
  2369. * the debug interface
  2370. * @callb: pointer to the callback called when mode is changed.
  2371. * NULL if no callback is required.
  2372. *
  2373. * Context: no sleep, no locks taken/released.
  2374. * Return: 0 if ok,
  2375. * -1 if service group is busy,
  2376. * -3 if called in an invalid API state
  2377. */
  2378. int upwr_dgn_mode(upwr_dgn_mode_t mode, const upwr_callb callb)
  2379. {
  2380. upwr_dgn_mode_msg txmsg = {0};
  2381. if (UPWR_SG_BUSY(UPWR_SG_DIAG)) {
  2382. return -1;
  2383. }
  2384. UPWR_USR_CALLB(UPWR_SG_DIAG, callb);
  2385. UPWR_MSG_HDR(txmsg.hdr, UPWR_SG_DIAG, UPWR_DGN_MODE);
  2386. txmsg.hdr.arg = mode;
  2387. upwr_srv_req(UPWR_SG_DIAG, (uint32_t *)&txmsg, sizeof(txmsg) / 4U);
  2388. return 0;
  2389. }
  2390. /**---------------------------------------------------------------
  2391. * AUXILIARY CALLS
  2392. */
  2393. /**
  2394. * upwr_rom_version() - informs the ROM firwmware version.
  2395. * @vmajor: pointer to the variable to get the firmware major version number.
  2396. * @vminor: pointer to the variable to get the firmware minor version number.
  2397. * @vfixes: pointer to the variable to get the firmware fixes number.
  2398. *
  2399. * Context: no sleep, no locks taken/released.
  2400. * Return: SoC id.
  2401. */
  2402. uint32_t upwr_rom_version(uint32_t *vmajor, uint32_t *vminor, uint32_t *vfixes)
  2403. {
  2404. uint32_t soc;
  2405. upwr_lock(1);
  2406. soc = fw_rom_version.soc_id;
  2407. *vmajor = fw_rom_version.vmajor;
  2408. *vminor = fw_rom_version.vminor;
  2409. *vfixes = fw_rom_version.vfixes;
  2410. upwr_lock(0);
  2411. return soc;
  2412. }
  2413. /**
  2414. * upwr_ram_version() - informs the RAM firwmware version.
  2415. * @vminor: pointer to the variable to get the firmware minor version number.
  2416. * @vfixes: pointer to the variable to get the firmware fixes number.
  2417. *
  2418. * The 3 values returned are 0 if no RAM firmwmare was loaded and initialized.
  2419. *
  2420. * Context: no sleep, no locks taken/released.
  2421. * Return: firmware major version number.
  2422. */
  2423. uint32_t upwr_ram_version(uint32_t *vminor, uint32_t *vfixes)
  2424. {
  2425. uint32_t vmajor;
  2426. upwr_lock(1);
  2427. vmajor = fw_ram_version.vmajor;
  2428. *vminor = fw_ram_version.vminor;
  2429. *vfixes = fw_ram_version.vfixes;
  2430. upwr_lock(0);
  2431. return vmajor;
  2432. }
  2433. /**
  2434. * upwr_req_status() - tells the status of the service group request, and
  2435. * returns a request return value, if any.
  2436. * @sg: service group of the request
  2437. * @sgfptr: pointer to the variable that will hold the function id of
  2438. * the last request completed; can be NULL, in which case it is not used.
  2439. * @errptr: pointer to the variable that will hold the error code;
  2440. * can be NULL, in which case it is not used.
  2441. * @retptr: pointer to the variable that will hold the value returned
  2442. * by the last request completed (invalid if the last request completed didn't
  2443. * return any value); can be NULL, in which case it is not used.
  2444. * Note that a request may return a value even if service error is returned
  2445. * (*errptr != UPWR_RESP_OK): that is dependent on the specific service.
  2446. *
  2447. * This call can be used in a poll loop of a service request completion in case
  2448. * a callback was not registered.
  2449. *
  2450. * Context: no sleep, no locks taken/released.
  2451. * Return: service request status: succeeded, failed, or ongoing (busy)
  2452. */
  2453. upwr_req_status_t upwr_req_status(upwr_sg_t sg,
  2454. uint32_t *sgfptr,
  2455. upwr_resp_t *errptr,
  2456. int *retptr)
  2457. {
  2458. upwr_req_status_t status;
  2459. upwr_lock(1);
  2460. if (sgfptr != NULL) {
  2461. *sgfptr = (uint32_t)sg_rsp_msg[sg].hdr.function;
  2462. }
  2463. if (errptr != NULL) {
  2464. *errptr = (upwr_resp_t)sg_rsp_msg[sg].hdr.errcode;
  2465. }
  2466. if (retptr != NULL) {
  2467. *retptr = (int)((sg_rsp_siz[sg] == 2U) ?
  2468. sg_rsp_msg[sg].word2 : sg_rsp_msg[sg].hdr.ret);
  2469. }
  2470. status = ((sg_busy & (1UL << sg)) == 1U) ? UPWR_REQ_BUSY :
  2471. (sg_rsp_msg[sg].hdr.errcode == UPWR_RESP_OK) ? UPWR_REQ_OK :
  2472. UPWR_REQ_ERR;
  2473. upwr_lock(0);
  2474. return status;
  2475. }
  2476. /**
  2477. * upwr_poll_req_status() - polls the status of the service group request, and
  2478. * returns a request return value, if any.
  2479. * @sg: service group of the request
  2480. * @sgfptr: pointer to the variable that will hold the function id of
  2481. * the last request completed; can be NULL, in which case it is not used.
  2482. * @errptr: pointer to the variable that will hold the error code;
  2483. * can be NULL, in which case it is not used.
  2484. * @retptr: pointer to the variable that will hold the value returned
  2485. * by the last request completed (invalid if the last request completed didn't
  2486. * return any value); can be NULL, in which case it is not used.
  2487. * Note that a request may return a value even if service error is returned
  2488. * (*errptr != UPWR_RESP_OK): that is dependent on the specific service.
  2489. * @attempts: maximum number of polling attempts; if attempts > 0 and is
  2490. * reached with no service response received, upwr_poll_req_status returns
  2491. * UPWR_REQ_BUSY and variables pointed by sgfptr, retptr and errptr are not
  2492. * updated; if attempts = 0, upwr_poll_req_status waits "forever".
  2493. *
  2494. * This call can be used to poll a service request completion in case a
  2495. * callback was not registered.
  2496. *
  2497. * Context: no sleep, no locks taken/released.
  2498. * Return: service request status: succeeded, failed, or ongoing (busy)
  2499. */
  2500. upwr_req_status_t upwr_poll_req_status(upwr_sg_t sg,
  2501. uint32_t *sgfptr,
  2502. upwr_resp_t *errptr,
  2503. int *retptr,
  2504. uint32_t attempts)
  2505. {
  2506. uint32_t i;
  2507. upwr_req_status_t ret;
  2508. if (attempts == 0U) {
  2509. while ((ret = upwr_req_status(sg, sgfptr, errptr, retptr)) == UPWR_REQ_BUSY) {
  2510. };
  2511. return ret;
  2512. }
  2513. for (i = 0U; i < attempts; i++) {
  2514. ret = upwr_req_status(sg, sgfptr, errptr, retptr);
  2515. if (ret != UPWR_REQ_BUSY) {
  2516. break;
  2517. }
  2518. }
  2519. return ret;
  2520. }
  2521. /**
  2522. * upwr_alarm_code() - returns the alarm code of the last alarm occurrence.
  2523. *
  2524. * The value returned is not meaningful if no alarm was issued by uPower.
  2525. *
  2526. * Context: no sleep, no locks taken/released.
  2527. * Return: alarm code, as defined by the type upwr_alarm_t in upwr_soc_defines.h
  2528. */
  2529. upwr_alarm_t upwr_alarm_code(void)
  2530. {
  2531. return (upwr_alarm_t)(3U & (mu->FSR.R >> 1U)); /* FSR[2:1] */
  2532. }
  2533. /**---------------------------------------------------------------
  2534. * TRANSMIT/RECEIVE PRIMITIVES
  2535. * ---------------------------------------------------------------
  2536. */
  2537. /*
  2538. * upwr_copy2tr() - copies a message to the MU TR registers;
  2539. * fill the TR registers before writing TIEN to avoid early interrupts;
  2540. * also, fill them from the higher index to the lowest, so the receive
  2541. * interrupt flag RF[0] will be the last to set, regardless of message size;
  2542. */
  2543. void upwr_copy2tr(struct MU_t *local_mu, const uint32_t *msg, unsigned int size)
  2544. {
  2545. for (int i = (int)size - 1; i > -1; i--) {
  2546. local_mu->TR[i].R = msg[i];
  2547. }
  2548. }
  2549. /**
  2550. * upwr_tx() - queues a message for transmission.
  2551. * @msg : pointer to the message sent.
  2552. * @size: message size in 32-bit words
  2553. * @callback: pointer to a function to be called when transmission done;
  2554. * can be NULL, in which case no callback is done.
  2555. *
  2556. * This is an auxiliary function used by the rest of the API calls.
  2557. * It is normally not called by the driver code, unless maybe for test purposes.
  2558. *
  2559. * Context: no sleep, no locks taken/released.
  2560. * Return: number of vacant positions left in the transmission queue, or
  2561. * -1 if the queue was already full when upwr_tx was called, or
  2562. * -2 if any argument is invalid (like size off-range)
  2563. */
  2564. int upwr_tx(const uint32_t *msg,
  2565. unsigned int size,
  2566. UPWR_TX_CALLB_FUNC_T callback)
  2567. {
  2568. if (size > UPWR_MU_MSG_SIZE) {
  2569. return -2;
  2570. }
  2571. if (size == 0U) {
  2572. return -2;
  2573. }
  2574. if (mu->TSR.R != UPWR_MU_TSR_EMPTY) {
  2575. return -1; /* not all TE bits in 1: some data to send still */
  2576. }
  2577. mu_tx_callb = callback;
  2578. upwr_copy2tr(mu, msg, size);
  2579. mu->TCR.R = 1UL << (size - 1UL);
  2580. mu_tx_pend = 1UL;
  2581. return 0;
  2582. }
  2583. /**
  2584. * upwr_rx() - unqueues a received message from the reception queue.
  2585. * @msg: pointer to the message destination buffer.
  2586. * @size: pointer to variable to hold message size in 32-bit words.
  2587. *
  2588. * This is an auxiliary function used by the rest of the API calls.
  2589. * It is normally not called by the driver code, unless maybe for test purposes.
  2590. *
  2591. * Context: no sleep, no locks taken/released.
  2592. * Return: number of messages remaining in the reception queue, or
  2593. * -1 if the queue was already empty when upwr_rx was called, or
  2594. * -2 if any argument is invalid (like mu off-range)
  2595. */
  2596. int upwr_rx(char *msg, unsigned int *size)
  2597. {
  2598. unsigned int len = mu->RSR.R;
  2599. len = (len == 0x0U) ? 0U :
  2600. (len == 0x1U) ? 1U :
  2601. #if UPWR_MU_MSG_SIZE > 1
  2602. (len == 0x3U) ? 2U :
  2603. #if UPWR_MU_MSG_SIZE > 2
  2604. (len == 0x7U) ? 3U :
  2605. #if UPWR_MU_MSG_SIZE > 3
  2606. (len == 0xFU) ? 4U :
  2607. #endif
  2608. #endif
  2609. #endif
  2610. 0xFFFFFFFFU; /* something wrong */
  2611. if (len == 0xFFFFFFFFU) {
  2612. return -3;
  2613. }
  2614. if (len == 0U) {
  2615. return -1;
  2616. }
  2617. *size = len;
  2618. /*
  2619. * copy the received message to the rx queue,
  2620. * so the interrupts are cleared.
  2621. */
  2622. msg_copy(msg, (char *)&mu->RR[0], len);
  2623. mu->RCR.R = 1U; /* enable only RR[0] receive interrupt */
  2624. return 0;
  2625. }
  2626. /**
  2627. * upwr_rx_callback() - sets up a callback for a message receiving event.
  2628. * @callback: pointer to a function to be called when a message arrives;
  2629. * can be NULL, in which case no callback is done.
  2630. *
  2631. * This is an auxiliary function used by the rest of the API calls.
  2632. * It is normally not called by the driver code, unless maybe for test purposes.
  2633. *
  2634. * Context: no sleep, no locks taken/released.
  2635. * Return: 0 if ok; -2 if any argument is invalid (mu off-range).
  2636. */
  2637. int upwr_rx_callback(UPWR_RX_CALLB_FUNC_T callback)
  2638. {
  2639. mu_rx_callb = callback;
  2640. return 0;
  2641. }
  2642. /**
  2643. * msg_copy() - copies a message.
  2644. * @dest: pointer to the destination message.
  2645. * @src : pointer to the source message.
  2646. * @size: message size in words.
  2647. *
  2648. * This is an auxiliary function used by the rest of the API calls.
  2649. * It is normally not called by the driver code, unless maybe for test purposes.
  2650. *
  2651. * Context: no sleep, no locks taken/released.
  2652. * Return: none (void)
  2653. */
  2654. void msg_copy(char *dest, char *src, unsigned int size)
  2655. {
  2656. for (uint32_t i = 0U; i < size * sizeof(uint32_t); i++) {
  2657. dest[i] = src[i];
  2658. }
  2659. }