mt_pmic_wrap_init.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein is
  5. * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
  6. * the prior written permission of MediaTek inc. and/or its licensors, any
  7. * reproduction, modification, use or disclosure of MediaTek Software, and
  8. * information contained herein, in whole or in part, shall be strictly
  9. * prohibited.
  10. *
  11. * MediaTek Inc. (C) 2016. All rights reserved.
  12. *
  13. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  14. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  15. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
  16. * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
  17. * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  19. * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
  20. * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
  21. * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
  22. * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
  23. * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
  24. * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
  25. * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
  26. * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  27. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
  28. * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
  29. * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
  30. * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
  31. * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  32. *
  33. * The following software/firmware and/or related documentation ("MediaTek
  34. * Software") have been modified by MediaTek Inc. All revisions are subject to
  35. * any receiver's applicable license agreements with MediaTek Inc.
  36. */
  37. /******************************************************************************
  38. * MTK PMIC Wrapper Driver
  39. *
  40. * Copyright 2016 MediaTek Co.,Ltd.
  41. *
  42. * DESCRIPTION:
  43. * This file provides API for other drivers to access PMIC registers
  44. *
  45. ******************************************************************************/
  46. #include <mt_pmic_wrap_init.h>
  47. #if (PMIC_WRAP_PRELOADER)
  48. #elif (PMIC_WRAP_LK)
  49. #elif (PMIC_WRAP_KERNEL)
  50. #include <linux/spinlock.h>
  51. #include <linux/delay.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/sched.h>
  54. #include <linux/timer.h>
  55. #include <linux/io.h>
  56. #ifdef CONFIG_OF
  57. #include <linux/of.h>
  58. #include <linux/of_irq.h>
  59. #include <linux/of_address.h>
  60. #endif
  61. #include <mt-plat/mtk_gpio.h>
  62. #include <mach/mtk_pmic_wrap.h>
  63. #include "pwrap_hal.h"
  64. #undef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  65. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  66. #include "sspm_ipi.h"
  67. #endif
  68. #define PMIC_WRAP_DEVICE "pmic_wrap"
  69. #elif (PMIC_WRAP_CTP)
  70. #include <gpio.h>
  71. #include <upmu_hw.h>
  72. #else
  73. ### Compile error, check SW ENV define
  74. #endif
  75. /************* marco ******************************************************/
  76. #if (PMIC_WRAP_PRELOADER)
  77. #elif (PMIC_WRAP_LK)
  78. #elif (PMIC_WRAP_KERNEL)
  79. #ifdef CONFIG_OF
  80. void __iomem *pwrap_base;
  81. #ifndef PMIC_WRAP_NO_PMIC
  82. static void __iomem *topckgen_base;
  83. static void __iomem *infracfg_ao_base;
  84. #endif /* end of #ifndef PMIC_WRAP_NO_PMIC */
  85. #endif
  86. #ifndef PMIC_WRAP_NO_PMIC
  87. static struct mt_pmic_wrap_driver *mt_wrp;
  88. static spinlock_t wrp_lock = __SPIN_LOCK_UNLOCKED(lock);
  89. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  90. #define WRITE_CMD 1
  91. #define READ_CMD 0
  92. #define WRITE_PMIC 1
  93. #define WRITE_PMIC_WRAP 0
  94. static unsigned int pwrap_recv_data[4] = {0};
  95. static signed int pwrap_wacs2_ipi(unsigned int adr, unsigned int rdata, unsigned int flag);
  96. static int pwrap_ipi_register(void);
  97. #endif
  98. #ifdef CONFIG_OF
  99. static int pwrap_of_iomap(void);
  100. static void pwrap_of_iounmap(void);
  101. #endif
  102. #endif /* end of #ifndef PMIC_WRAP_NO_PMIC */
  103. #elif (PMIC_WRAP_SCP)
  104. #elif (PMIC_WRAP_CTP)
  105. #else
  106. ### Compile error, check SW ENV define
  107. #endif
  108. #ifdef PMIC_WRAP_NO_PMIC
  109. #if !(PMIC_WRAP_KERNEL)
  110. signed int pwrap_wacs2(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata)
  111. {
  112. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  113. return 0;
  114. }
  115. signed int pwrap_read(unsigned int adr, unsigned int *rdata)
  116. {
  117. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  118. return 0;
  119. }
  120. signed int pwrap_write(unsigned int adr, unsigned int wdata)
  121. {
  122. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  123. return 0;
  124. }
  125. #endif
  126. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata)
  127. {
  128. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  129. return 0;
  130. }
  131. /* Provide PMIC write API */
  132. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata)
  133. {
  134. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  135. return 0;
  136. }
  137. signed int pwrap_read_nochk(unsigned int adr, unsigned int *rdata)
  138. {
  139. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  140. return 0;
  141. }
  142. signed int pwrap_write_nochk(unsigned int adr, unsigned int wdata)
  143. {
  144. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  145. return 0;
  146. }
  147. /*
  148. *pmic_wrap init,init wrap interface
  149. *
  150. */
  151. signed int pwrap_init(void)
  152. {
  153. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  154. return 0;
  155. }
  156. signed int pwrap_init_preloader(void)
  157. {
  158. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  159. return 0;
  160. }
  161. signed int pwrap_init_lk(void)
  162. {
  163. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  164. return 0;
  165. }
  166. #else /* #ifdef PMIC_WRAP_NO_PMIC */
  167. /*********************start ---internal API***********************************/
  168. static int _pwrap_timeout_ns(unsigned long long start_time_ns, unsigned long long timeout_time_ns);
  169. static unsigned long long _pwrap_get_current_time(void);
  170. static unsigned long long _pwrap_time2ns(unsigned long long time_us);
  171. static signed int _pwrap_reset_spislv(void);
  172. static signed int _pwrap_init_dio(unsigned int dio_en);
  173. static signed int _pwrap_init_cipher(void);
  174. static signed int _pwrap_init_reg_clock(unsigned int regck_sel);
  175. static void _pwrap_enable(void);
  176. static void _pwrap_starve_set(void);
  177. static signed int _pwrap_wacs2_nochk(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata);
  178. static signed int pwrap_wacs2_hal(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata);
  179. /*********************test API************************************************/
  180. static inline void pwrap_dump_ap_register(void);
  181. static unsigned int pwrap_write_test(void);
  182. static unsigned int pwrap_read_test(void);
  183. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata);
  184. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata);
  185. /************* end--internal API**********************************************/
  186. /*********************** external API for pmic_wrap user ************************/
  187. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata)
  188. {
  189. pwrap_wacs2(0, adr, 0, rdata);
  190. return 0;
  191. }
  192. /* Provide PMIC write API */
  193. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata)
  194. {
  195. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  196. unsigned int flag;
  197. flag = WRITE_CMD | (1 << WRITE_PMIC);
  198. pwrap_wacs2_ipi(adr, wdata, flag);
  199. #else
  200. pwrap_wacs2(1, adr, wdata, 0);
  201. #endif
  202. return 0;
  203. }
  204. signed int pwrap_read(unsigned int adr, unsigned int *rdata)
  205. {
  206. return pwrap_wacs2(0,adr,0,rdata);
  207. }
  208. signed int pwrap_write(unsigned int adr, unsigned int wdata)
  209. {
  210. return pwrap_wacs2(1,adr,wdata,0);
  211. }
  212. /******************************************************************************
  213. wrapper timeout
  214. ******************************************************************************/
  215. /*use the same API name with kernel driver
  216. *however,the timeout API in uboot use tick instead of ns */
  217. #ifdef PWRAP_TIMEOUT
  218. static unsigned long long _pwrap_get_current_time(void)
  219. {
  220. return gpt4_get_current_tick();
  221. }
  222. static int _pwrap_timeout_ns(unsigned long long start_time_ns, unsigned long long timeout_time_ns)
  223. {
  224. return gpt4_timeout_tick(start_time_ns, timeout_time_ns);
  225. }
  226. static unsigned long long _pwrap_time2ns(unsigned long long time_us)
  227. {
  228. return gpt4_time2tick_us(time_us);
  229. }
  230. #else
  231. static unsigned long long _pwrap_get_current_time(void)
  232. {
  233. return 0;
  234. }
  235. static int _pwrap_timeout_ns(unsigned long long start_time_ns, unsigned long long elapse_time)
  236. {
  237. return 0;
  238. }
  239. static unsigned long long _pwrap_time2ns(unsigned long long time_us)
  240. {
  241. return 0;
  242. }
  243. #endif
  244. /* ##################################################################### */
  245. /* define macro and inline function (for do while loop) */
  246. /* ##################################################################### */
  247. typedef unsigned int(*loop_condition_fp) (unsigned int); /* define a function pointer */
  248. static inline unsigned int wait_for_fsm_idle(unsigned int x)
  249. {
  250. return GET_WACS2_FSM(x) != WACS_FSM_IDLE;
  251. }
  252. static inline unsigned int wait_for_fsm_vldclr(unsigned int x)
  253. {
  254. return GET_WACS2_FSM(x) != WACS_FSM_WFVLDCLR;
  255. }
  256. static inline unsigned int wait_for_sync(unsigned int x)
  257. {
  258. return GET_SYNC_IDLE2(x) != WACS_SYNC_IDLE;
  259. }
  260. static inline unsigned int wait_for_idle_and_sync(unsigned int x)
  261. {
  262. return (GET_WACS2_FSM(x) != WACS_FSM_IDLE) || (GET_SYNC_IDLE2(x) != WACS_SYNC_IDLE);
  263. }
  264. static inline unsigned int wait_for_wrap_idle(unsigned int x)
  265. {
  266. return (GET_WRAP_FSM(x) != 0x0) || (GET_WRAP_CH_DLE_RESTCNT(x) != 0x0);
  267. }
  268. static inline unsigned int wait_for_wrap_state_idle(unsigned int x)
  269. {
  270. return GET_WRAP_AG_DLE_RESTCNT(x) != 0;
  271. }
  272. static inline unsigned int wait_for_man_idle_and_noreq(unsigned int x)
  273. {
  274. return (GET_MAN_REQ(x) != MAN_FSM_NO_REQ) || (GET_MAN_FSM(x) != MAN_FSM_IDLE);
  275. }
  276. static inline unsigned int wait_for_man_vldclr(unsigned int x)
  277. {
  278. return GET_MAN_FSM(x) != MAN_FSM_WFVLDCLR;
  279. }
  280. static inline unsigned int wait_for_cipher_ready(unsigned int x)
  281. {
  282. return x != 3;
  283. }
  284. static inline unsigned int wait_for_stdupd_idle(unsigned int x)
  285. {
  286. return GET_STAUPD_FSM(x) != 0x0;
  287. }
  288. /**************used at _pwrap_wacs2_nochk*************************************/
  289. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  290. static inline unsigned int wait_for_state_ready_init(loop_condition_fp fp, unsigned int timeout_us,
  291. void *wacs_register, unsigned int *read_reg)
  292. #else
  293. static inline unsigned int wait_for_state_ready_init(loop_condition_fp fp, unsigned int timeout_us,
  294. volatile unsigned int *wacs_register, unsigned int *read_reg)
  295. #endif
  296. {
  297. unsigned long long start_time_ns = 0, timeout_ns = 0;
  298. unsigned int reg_rdata = 0x0;
  299. start_time_ns = _pwrap_get_current_time();
  300. timeout_ns = _pwrap_time2ns(timeout_us);
  301. do {
  302. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  303. PWRAPERR("ready_init timeout\n");
  304. return E_PWR_WAIT_IDLE_TIMEOUT;
  305. }
  306. reg_rdata = WRAP_RD32(wacs_register);
  307. } while (fp(reg_rdata));
  308. if (read_reg)
  309. *read_reg = reg_rdata;
  310. return 0;
  311. }
  312. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  313. static inline unsigned int wait_for_state_idle(loop_condition_fp fp, unsigned int timeout_us, void *wacs_register,
  314. void *wacs_vldclr_register, unsigned int *read_reg)
  315. #else
  316. static inline unsigned int wait_for_state_idle(loop_condition_fp fp, unsigned int timeout_us,
  317. volatile unsigned int *wacs_register, volatile unsigned int *wacs_vldclr_register, unsigned int *read_reg)
  318. #endif
  319. {
  320. unsigned long long start_time_ns = 0, timeout_ns = 0;
  321. unsigned int reg_rdata;
  322. start_time_ns = _pwrap_get_current_time();
  323. timeout_ns = _pwrap_time2ns(timeout_us);
  324. do {
  325. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  326. PWRAPERR("state_idle timeout\n");
  327. pwrap_dump_ap_register();
  328. return E_PWR_WAIT_IDLE_TIMEOUT;
  329. }
  330. reg_rdata = WRAP_RD32(wacs_register);
  331. if (GET_WACS2_INIT_DONE2(reg_rdata) != WACS_INIT_DONE) {
  332. PWRAPERR("init isn't finished\n");
  333. return E_PWR_NOT_INIT_DONE;
  334. }
  335. switch (GET_WACS2_FSM(reg_rdata)) {
  336. case WACS_FSM_WFVLDCLR:
  337. WRAP_WR32(wacs_vldclr_register, 1);
  338. PWRAPERR("WACS_FSM = VLDCLR\n");
  339. break;
  340. case WACS_FSM_WFDLE:
  341. PWRAPERR("WACS_FSM = WFDLE\n");
  342. break;
  343. case WACS_FSM_REQ:
  344. PWRAPERR("WACS_FSM = REQ\n");
  345. break;
  346. default:
  347. break;
  348. }
  349. } while (fp(reg_rdata));
  350. if (read_reg)
  351. *read_reg = reg_rdata;
  352. return 0;
  353. }
  354. /**************used at pwrap_wacs2********************************************/
  355. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  356. static inline unsigned int wait_for_state_ready(loop_condition_fp fp, unsigned int timeout_us, void *wacs_register,
  357. unsigned int *read_reg)
  358. #else
  359. static inline unsigned int wait_for_state_ready(loop_condition_fp fp, unsigned int timeout_us,
  360. volatile unsigned int *wacs_register, unsigned int *read_reg)
  361. #endif
  362. {
  363. unsigned long long start_time_ns = 0, timeout_ns = 0;
  364. unsigned int reg_rdata;
  365. start_time_ns = _pwrap_get_current_time();
  366. timeout_ns = _pwrap_time2ns(timeout_us);
  367. do {
  368. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  369. PWRAPERR("state_ready timeout\n");
  370. pwrap_dump_ap_register();
  371. return E_PWR_WAIT_IDLE_TIMEOUT;
  372. }
  373. reg_rdata = WRAP_RD32(wacs_register);
  374. if (GET_WACS2_INIT_DONE2(reg_rdata) != WACS_INIT_DONE) {
  375. PWRAPERR("init isn't finished\n");
  376. return E_PWR_NOT_INIT_DONE;
  377. }
  378. } while (fp(reg_rdata));
  379. if (read_reg)
  380. *read_reg = reg_rdata;
  381. return 0;
  382. }
  383. /******************************************************
  384. * Function : pwrap_wacs2()
  385. * Description :
  386. * Parameter :
  387. * Return :
  388. ******************************************************/
  389. signed int pwrap_wacs2(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata)
  390. {
  391. unsigned int reg_rdata = 0;
  392. unsigned int wacs_write = 0;
  393. unsigned int wacs_adr = 0;
  394. unsigned int wacs_cmd = 0;
  395. unsigned int return_value = 0;
  396. /* Check argument validation */
  397. if ((write & ~(0x1)) != 0)
  398. return E_PWR_INVALID_RW;
  399. if ((adr & ~(0xffff)) != 0)
  400. return E_PWR_INVALID_ADDR;
  401. if ((wdata & ~(0xffff)) != 0)
  402. return E_PWR_INVALID_WDAT;
  403. /* Check IDLE & INIT_DONE in advance */
  404. return_value =
  405. wait_for_state_idle(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA,
  406. PMIC_WRAP_WACS2_VLDCLR, 0);
  407. if (return_value != 0) {
  408. PWRAPERR("fsm_idle fail,ret=%d\n", return_value);
  409. goto FAIL;
  410. }
  411. wacs_write = write << 31;
  412. wacs_adr = (adr >> 1) << 16;
  413. wacs_cmd = wacs_write | wacs_adr | wdata;
  414. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  415. if (write == 0) {
  416. if (rdata == NULL) {
  417. PWRAPERR("rdata NULL\n");
  418. return_value = E_PWR_INVALID_ARG;
  419. goto FAIL;
  420. }
  421. return_value =
  422. wait_for_state_ready(wait_for_fsm_vldclr, TIMEOUT_READ, PMIC_WRAP_WACS2_RDATA,
  423. &reg_rdata);
  424. if (return_value != 0) {
  425. PWRAPERR("fsm_vldclr fail,ret=%d\n", return_value);
  426. return_value += 1;
  427. goto FAIL;
  428. }
  429. *rdata = GET_WACS2_RDATA(reg_rdata);
  430. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  431. }
  432. FAIL:
  433. if (return_value != 0) {
  434. PWRAPERR("pwrap_wacs2_hal fail,ret=%d\n", return_value);
  435. PWRAPERR("BUG_ON\n");
  436. }
  437. return return_value;
  438. }
  439. /*********************internal API for pwrap_init***************************/
  440. /**********************************
  441. * Function : _pwrap_wacs2_nochk()
  442. * Description :
  443. * Parameter :
  444. * Return :
  445. ***********************************/
  446. signed int pwrap_read_nochk(unsigned int adr, unsigned int *rdata)
  447. {
  448. return _pwrap_wacs2_nochk(0, adr, 0, rdata);
  449. }
  450. signed int pwrap_write_nochk(unsigned int adr, unsigned int wdata)
  451. {
  452. return _pwrap_wacs2_nochk(1, adr, wdata, 0);
  453. }
  454. static signed int _pwrap_wacs2_nochk(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata)
  455. {
  456. unsigned int reg_rdata = 0x0;
  457. unsigned int wacs_write = 0x0;
  458. unsigned int wacs_adr = 0x0;
  459. unsigned int wacs_cmd = 0x0;
  460. unsigned int return_value = 0x0;
  461. /* Check argument validation */
  462. if ((write & ~(0x1)) != 0)
  463. return E_PWR_INVALID_RW;
  464. if ((adr & ~(0xffff)) != 0)
  465. return E_PWR_INVALID_ADDR;
  466. if ((wdata & ~(0xffff)) != 0)
  467. return E_PWR_INVALID_WDAT;
  468. /* Check IDLE */
  469. return_value =
  470. wait_for_state_ready_init(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  471. if (return_value != 0) {
  472. PWRAPERR("write fail,ret=%x\n", return_value);
  473. return return_value;
  474. }
  475. wacs_write = write << 31;
  476. wacs_adr = (adr >> 1) << 16;
  477. wacs_cmd = wacs_write | wacs_adr | wdata;
  478. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  479. if (write == 0) {
  480. if (rdata == NULL) {
  481. PWRAPERR("rdata NULL\n");
  482. return_value = E_PWR_INVALID_ARG;
  483. return return_value;
  484. }
  485. return_value =
  486. wait_for_state_ready_init(wait_for_fsm_vldclr, TIMEOUT_READ,
  487. PMIC_WRAP_WACS2_RDATA, &reg_rdata);
  488. if (return_value != 0) {
  489. PWRAPERR("fsm_vldclr fail,ret=%d\n", return_value);
  490. return_value += 1;
  491. return return_value;
  492. }
  493. *rdata = GET_WACS2_RDATA(reg_rdata);
  494. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  495. }
  496. return 0;
  497. }
  498. static void __pwrap_soft_reset(void)
  499. {
  500. PWRAPLOG("start reset wrapper\n");
  501. WRAP_WR32(INFRA_GLOBALCON_RST2_SET, 0x1);
  502. WRAP_WR32(INFRA_GLOBALCON_RST2_CLR, 0x1);
  503. }
  504. static void __pwrap_spi_clk_set(void)
  505. {
  506. PWRAPLOG("pwrap_spictl reset ok\n");
  507. #if !defined(CONFIG_FPGA_EARLY_PORTING)
  508. WRAP_WR32(CLK_CFG_5_CLR, 0x00000093);
  509. WRAP_WR32(CLK_CFG_5_SET, CLK_SPI_CK_26M);
  510. #endif
  511. /*sys_ck cg enable, turn off clock*/
  512. WRAP_WR32(MODULE_SW_CG_0_SET, 0x0000000F);
  513. /* turn off clock*/
  514. WRAP_WR32(MODULE_SW_CG_2_SET, 0x00000100);
  515. /* toggle PMIC_WRAP and pwrap_spictl reset */
  516. __pwrap_soft_reset();
  517. /*sys_ck cg enable, turn on clock*/
  518. WRAP_WR32(MODULE_SW_CG_0_CLR, 0x0000000F);
  519. /* turn on clock*/
  520. WRAP_WR32(MODULE_SW_CG_2_CLR, 0x00000100);
  521. PWRAPLOG("spi clk set ....\n");
  522. }
  523. /************************************************
  524. * Function : _pwrap_init_dio()
  525. * Description :call it in pwrap_init,mustn't check init done
  526. * Parameter :
  527. * Return :
  528. ************************************************/
  529. static signed int _pwrap_init_dio(unsigned int dio_en)
  530. {
  531. unsigned int rdata = 0x0;
  532. pwrap_write_nochk(PMIC_DEW_DIO_EN_ADDR, dio_en);
  533. #ifdef DUAL_PMICS
  534. pwrap_write_nochk(EXT_DEW_DIO_EN, dio_en);
  535. #endif
  536. do {
  537. rdata = WRAP_RD32(PMIC_WRAP_WACS2_RDATA);
  538. } while ((GET_WACS2_FSM(rdata) != WACS_FSM_IDLE) || (GET_SYNC_IDLE2(rdata) != WACS_SYNC_IDLE));
  539. #ifndef DUAL_PMICS
  540. WRAP_WR32(PMIC_WRAP_DIO_EN, 0x1);
  541. #else
  542. WRAP_WR32(PMIC_WRAP_DIO_EN, 0x3);
  543. #endif
  544. return 0;
  545. }
  546. /***************************************************
  547. * Function : _pwrap_init_cipher()
  548. * Description :
  549. * Parameter :
  550. * Return :
  551. ****************************************************/
  552. static signed int _pwrap_init_cipher(void)
  553. {
  554. unsigned int rdata = 0;
  555. unsigned int return_value = 0;
  556. unsigned int start_time_ns = 0, timeout_ns = 0;
  557. WRAP_WR32(PMIC_WRAP_CIPHER_SWRST, 1);
  558. WRAP_WR32(PMIC_WRAP_CIPHER_SWRST, 0);
  559. WRAP_WR32(PMIC_WRAP_CIPHER_KEY_SEL, 1);
  560. WRAP_WR32(PMIC_WRAP_CIPHER_IV_SEL, 2);
  561. WRAP_WR32(PMIC_WRAP_CIPHER_EN, 1);
  562. /* Config CIPHER @ PMIC */
  563. pwrap_write_nochk(PMIC_DEW_CIPHER_SWRST_ADDR, 0x1);
  564. pwrap_write_nochk(PMIC_DEW_CIPHER_SWRST_ADDR, 0x0);
  565. pwrap_write_nochk(PMIC_DEW_CIPHER_KEY_SEL_ADDR, 0x1);
  566. pwrap_write_nochk(PMIC_DEW_CIPHER_IV_SEL_ADDR, 0x2);
  567. pwrap_write_nochk(PMIC_DEW_CIPHER_EN_ADDR, 0x1);
  568. PWRAPLOG("[_pwrap_init_cipher]Config CIPHER of PMIC 0 ok\n");
  569. #ifdef DUAL_PMICS
  570. /* Config CIPHER of PMIC 1 */
  571. pwrap_write_nochk(EXT_DEW_CIPHER_SWRST, 0x1);
  572. pwrap_write_nochk(EXT_DEW_CIPHER_SWRST, 0x0);
  573. pwrap_write_nochk(EXT_DEW_CIPHER_KEY_SEL, 0x1);
  574. pwrap_write_nochk(EXT_DEW_CIPHER_IV_SEL, 0x2);
  575. pwrap_write_nochk(EXT_DEW_CIPHER_EN, 0x1);
  576. PWRAPLOG("[_pwrap_init_cipher]Config CIPHER of PMIC 1 ok\n");
  577. #endif
  578. /*wait for cipher data ready@AP */
  579. return_value = wait_for_state_ready_init(wait_for_cipher_ready, TIMEOUT_WAIT_IDLE, PMIC_WRAP_CIPHER_RDY, 0);
  580. if (return_value != 0) {
  581. PWRAPERR("cipher fail,ret=%x\n", return_value);
  582. return return_value;
  583. }
  584. PWRAPLOG("wait for cipher 0 and 1 to be ready ok\n");
  585. /* wait for cipher 0 data ready@PMIC */
  586. start_time_ns = _pwrap_get_current_time();
  587. timeout_ns = _pwrap_time2ns(0xFFFFFF);
  588. do {
  589. if (_pwrap_timeout_ns(start_time_ns, timeout_ns))
  590. PWRAPERR("cipher 0 data\n");
  591. pwrap_read_nochk(PMIC_DEW_CIPHER_RDY_ADDR, &rdata);
  592. } while (rdata != 0x1); /* cipher_ready */
  593. return_value = pwrap_write_nochk(PMIC_DEW_CIPHER_MODE_ADDR, 0x1);
  594. if (return_value != 0) {
  595. PWRAPERR("CIPHER_MODE fail,ret=%x\n", return_value);
  596. return return_value;
  597. }
  598. PWRAPLOG("wait for cipher0 data ready ok\n");
  599. #ifdef DUAL_PMICS
  600. start_time_ns = _pwrap_get_current_time();
  601. timeout_ns = _pwrap_time2ns(0xFFFFFF);
  602. do {
  603. if (_pwrap_timeout_ns(start_time_ns, timeout_ns))
  604. PWRAPERR("cipher 1 data\n");
  605. pwrap_read_nochk(EXT_DEW_CIPHER_RDY, &rdata);
  606. } while (rdata != 0x1); /* cipher_ready */
  607. return_value = pwrap_write_nochk(EXT_DEW_CIPHER_MODE, 0x1);
  608. if (return_value != 0) {
  609. PWRAPERR("EXT_CIPHER_MODE fail,ret=%x\n", return_value);
  610. return return_value;
  611. }
  612. #endif
  613. PWRAPLOG("wait for cipher 1 data ready@PMIC ok\n");
  614. /* wait for cipher mode idle */
  615. return_value = wait_for_state_ready_init(wait_for_idle_and_sync, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  616. if (return_value != 0) {
  617. PWRAPERR("cipher mode idle fail,ret=%x\n", return_value);
  618. return return_value;
  619. }
  620. WRAP_WR32(PMIC_WRAP_CIPHER_MODE, 1);
  621. /* Read Test */
  622. pwrap_read_nochk(PMIC_DEW_READ_TEST_ADDR, &rdata);
  623. if (rdata != DEFAULT_VALUE_READ_TEST) {
  624. PWRAPERR("cipher,err=%x, rdata=%x\n", 1, rdata);
  625. return E_PWR_READ_TEST_FAIL;
  626. }
  627. #ifdef DUAL_PMICS
  628. pwrap_read_nochk(EXT_DEW_READ_TEST, &rdata);
  629. if (rdata != DEFAULT_VALUE_READ_TEST) {
  630. PWRAPERR("cipher,err=%x, rdata=%x\n", 1, rdata);
  631. return E_PWR_READ_TEST_FAIL;
  632. }
  633. #endif
  634. return 0;
  635. }
  636. static void _pwrap_InitStaUpd(void)
  637. {
  638. #ifndef DUAL_PMICS
  639. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, 0xf4);
  640. #else
  641. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, 0xfc);
  642. #endif
  643. #if 0
  644. /* CRC */
  645. #ifdef DUAL_PMICS
  646. pwrap_write_nochk(PMIC_DEW_CRC_EN_ADDR, 0x1);
  647. WRAP_WR32(PMIC_WRAP_CRC_EN, 0x1);
  648. WRAP_WR32(PMIC_WRAP_SIG_ADR, DEW_CRC_VAL);
  649. #else
  650. pwrap_write_nochk(PMIC_DEW_CRC_EN_ADDR, 0x1);
  651. pwrap_write_nochk(EXT_DEW_CRC_EN, 0x1);
  652. WRAP_WR32(PMIC_WRAP_CRC_EN, 0x1);
  653. WRAP_WR32(PMIC_WRAP_SIG_ADR, (EXT_DEW_CRC_VAL << 16 | DEW_CRC_VAL));
  654. #endif
  655. #endif
  656. /* Signature */
  657. #ifndef DUAL_PMICS
  658. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x1);
  659. WRAP_WR32(PMIC_WRAP_SIG_ADR, MT6356_DEW_CRC_VAL);
  660. WRAP_WR32(PMIC_WRAP_SIG_VALUE, 0x83);
  661. #else
  662. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x3);
  663. WRAP_WR32(PMIC_WRAP_SIG_ADR, (EXT_DEW_CRC_VAL << 16) | DEW_CRC_VAL);
  664. WRAP_WR32(PMIC_WRAP_SIG_VALUE, (0x83 << 16) | 0x83);
  665. #endif
  666. WRAP_WR32(PMIC_WRAP_EINT_STA0_ADR, PMIC_CPU_INT_STA_ADDR);
  667. #ifdef DUAL_PMICS
  668. WRAP_WR32(PMIC_WRAP_EINT_STA1_ADR, EXT_INT_STA);
  669. #endif
  670. /* MD ADC Interface */
  671. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_LATEST_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  672. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_WP_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  673. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_0_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  674. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_1_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  675. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_2_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  676. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_3_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  677. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_4_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  678. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_5_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  679. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_6_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  680. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_7_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  681. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_8_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  682. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_9_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  683. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_10_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  684. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_11_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  685. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_12_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  686. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_13_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  687. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_14_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  688. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_15_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  689. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_16_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  690. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_17_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  691. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_18_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  692. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_19_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  693. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_20_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  694. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_21_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  695. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_22_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  696. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_23_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  697. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_24_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  698. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_25_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  699. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_26_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  700. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_27_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  701. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_28_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  702. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_29_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  703. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_30_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  704. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_31_ADDR, (MT6356_AUXADC_ADC41 << 16) + MT6356_AUXADC_ADC36);
  705. WRAP_WR32(PMIC_WRAP_INT_GPS_AUXADC_CMD_ADDR, (MT6356_AUXADC_RQST1_SET << 16) + MT6356_AUXADC_RQST1_SET);
  706. WRAP_WR32(PMIC_WRAP_INT_GPS_AUXADC_CMD, (0x0400 << 16) + 0x0100);
  707. WRAP_WR32(PMIC_WRAP_INT_GPS_AUXADC_RDATA_ADDR, (MT6356_AUXADC_ADC39 << 16) + MT6356_AUXADC_ADC16);
  708. WRAP_WR32(PMIC_WRAP_EXT_GPS_AUXADC_RDATA_ADDR, MT6356_AUXADC_ADC36);
  709. }
  710. static void _pwrap_starve_set(void)
  711. {
  712. WRAP_WR32(PMIC_WRAP_HARB_HPRIO, 0xf);
  713. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_0, 0x402);
  714. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_1, 0x403);
  715. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_2, 0x403);
  716. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_3, 0x403);
  717. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_4, 0x40f);
  718. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_5, 0x420);
  719. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_6, 0x428);
  720. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_7, 0x428);
  721. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_8, 0x413);
  722. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_9, 0x417);
  723. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_10, 0x417);
  724. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_11, 0x47c);
  725. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_12, 0x47c);
  726. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_13, 0x740);
  727. }
  728. static void _pwrap_enable(void)
  729. {
  730. #if (MTK_PLATFORM_MT6356)
  731. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x3fa65);
  732. #endif
  733. WRAP_WR32(PMIC_WRAP_WACS0_EN, 0x1);
  734. WRAP_WR32(PMIC_WRAP_WACS2_EN, 0x1);
  735. WRAP_WR32(PMIC_WRAP_STAUPD_CTRL, 0x5); /* 100us */
  736. WRAP_WR32(PMIC_WRAP_WDT_UNIT, 0xf);
  737. WRAP_WR32(PMIC_WRAP_WDT_SRC_EN_0, 0xffffffff);
  738. WRAP_WR32(PMIC_WRAP_WDT_SRC_EN_1, 0xffffffff);
  739. WRAP_WR32(PMIC_WRAP_TIMER_CTRL, 0x1);
  740. WRAP_WR32(PMIC_WRAP_INT0_EN, 0xffffffff);
  741. WRAP_WR32(PMIC_WRAP_INT1_EN, 0xffffffff);
  742. }
  743. /************************************************
  744. * Function : _pwrap_init_sistrobe()
  745. * scription : Initialize SI_CK_CON and SIDLY
  746. * Parameter :
  747. * Return :
  748. ************************************************/
  749. static signed int _pwrap_init_sistrobe(int dual_si_sample_settings)
  750. {
  751. unsigned int rdata;
  752. int si_en_sel, si_ck_sel, si_dly, si_sample_ctrl, reserved = 0;
  753. char result_faulty = 0;
  754. char found;
  755. int test_data[30] = {0x6996, 0x9669, 0x6996, 0x9669, 0x6996, 0x9669, 0x6996, 0x9669, 0x6996, 0x9669,
  756. 0x5AA5, 0xA55A, 0x5AA5, 0xA55A, 0x5AA5, 0xA55A, 0x5AA5, 0xA55A, 0x5AA5, 0xA55A,
  757. 0x1B27, 0x1B27, 0x1B27, 0x1B27, 0x1B27, 0x1B27, 0x1B27, 0x1B27, 0x1B27, 0x1B27
  758. };
  759. int i;
  760. int error = 0;
  761. /* TINFO = "[DrvPWRAP_InitSiStrobe] SI Strobe Calibration For PMIC 0............" */
  762. /* TINFO = "[DrvPWRAP_InitSiStrobe] Scan For The First Valid Sampling Clock Edge......" */
  763. found = 0;
  764. for (si_en_sel = 0; si_en_sel < 8; si_en_sel++) {
  765. for (si_ck_sel = 0; si_ck_sel < 2; si_ck_sel++) {
  766. si_sample_ctrl = (si_en_sel << 6) | (si_ck_sel << 5);
  767. WRAP_WR32(PMIC_WRAP_SI_SAMPLE_CTRL, si_sample_ctrl);
  768. pwrap_read_nochk(MT6356_DEW_READ_TEST, &rdata);
  769. if (rdata == DEFAULT_VALUE_READ_TEST) {
  770. PWRAPLOG("[DrvPWRAP_InitSiStrobe]The First Valid Sampling Clock Edge Is Found !!!\n");
  771. PWRAPLOG("si_en_sel = %x, si_ck_sel = %x, si_sample_ctrl = %x, rdata = %x\n",
  772. si_en_sel, si_ck_sel, si_sample_ctrl, rdata);
  773. found = 1;
  774. break;
  775. }
  776. PWRAPERR("si_en_sel = %x, si_ck_sel = %x, *PMIC_WRAP_SI_SAMPLE_CTRL = %x, rdata = %x\n",
  777. si_en_sel, si_ck_sel, si_sample_ctrl, rdata);
  778. }
  779. if (found == 1)
  780. break;
  781. }
  782. if (found == 0) {
  783. result_faulty |= 0x1;
  784. PWRAPERR("result_faulty = %d\n", result_faulty);
  785. }
  786. if (si_en_sel == 8)
  787. result_faulty |= 0x2;
  788. /* TINFO = "[DrvPWRAP_InitSiStrobe] Search For The Data Boundary......" */
  789. for (si_dly = 0; si_dly < 10; si_dly++) {
  790. pwrap_write_nochk(MT6356_RG_SPI_CON2, si_dly);
  791. error = 0;
  792. #ifndef SPEED_UP_PWRAP_INIT
  793. for (i = 0; i < 30; i++)
  794. #else
  795. for (i = 0; i < 1; i++)
  796. #endif
  797. {
  798. pwrap_write_nochk(MT6356_DEW_WRITE_TEST, test_data[i]);
  799. pwrap_read_nochk(MT6356_DEW_WRITE_TEST, &rdata);
  800. if ((rdata & 0x7fff) != (test_data[i] & 0x7fff)) {
  801. PWRAPERR("InitSiStrobe (%x, %x, %x) Data Boundary Is Found !!\n",
  802. si_dly, reserved, rdata);
  803. PWRAPERR("InitSiStrobe (%x, %x, %x) Data Boundary Is Found !!\n",
  804. si_dly, si_dly, rdata);
  805. error = 1;
  806. break;
  807. }
  808. }
  809. if (error == 1)
  810. break;
  811. PWRAPLOG("si_dly = %x, *PMIC_WRAP_RESERVED = %x, rdata = %x\n",
  812. si_dly, reserved, rdata);
  813. PWRAPLOG("si_dly = %x, *RG_SPI_CON2 = %x, rdata = %x\n",
  814. si_dly, si_dly, rdata);
  815. }
  816. /* TINFO = "[DrvPWRAP_InitSiStrobe] Change The Sampling Clock Edge To The Next One." */
  817. /* si_sample_ctrl = (((si_en_sel << 1) | si_ck_sel) + 1) << 2;*/
  818. si_sample_ctrl = si_sample_ctrl + 0x20;
  819. WRAP_WR32(PMIC_WRAP_SI_SAMPLE_CTRL, si_sample_ctrl);
  820. if (si_dly == 10) {
  821. PWRAPLOG("SI Strobe Calibration For PMIC 0 Done, (%x, si_dly%x)\n", si_sample_ctrl, si_dly);
  822. si_dly--;
  823. }
  824. PWRAPLOG("SI Strobe Calibration For PMIC 0 Done, (%x, %x)\n", si_sample_ctrl, reserved);
  825. PWRAPLOG("SI Strobe Calibration For PMIC 0 Done, (%x, %x)\n", si_sample_ctrl, si_dly);
  826. #ifdef DUAL_PMICS
  827. if (dual_si_sample_settings == 1) {
  828. si_sample_ctrl = si_sample_ctrl | 0x40000;
  829. /* TINFO = "[DrvPWRAP_InitSiStrobe] SI Strobe Calibration For PMIC 1............" */
  830. /* TINFO = "[DrvPWRAP_InitSiStrobe] Scan For The First Valid Sampling Clock Edge." */
  831. found = 0;
  832. for (si_en_sel = 0; si_en_sel < 3; si_en_sel++) {
  833. for (si_ck_sel = 0; si_ck_sel < 2; si_ck_sel++) {
  834. /* si_sample_ctrl = (si_sample_ctrl & 0xfffffc1f) |
  835. *(((si_en_sel << 3) | (si_ck_sel << 2)) << 5);
  836. */
  837. si_sample_ctrl = (si_sample_ctrl & 0xfffc01ff) | (si_en_sel << 15) | (si_ck_sel << 14);
  838. WRAP_WR32(PMIC_WRAP_SI_SAMPLE_CTRL, si_sample_ctrl);
  839. pwrap_read_nochk(EXT_DEW_READ_TEST, &rdata);
  840. if (rdata == 0x5aa5) {
  841. PWRAPLOG("si_en_sel = %x, si_ck_sel = %x, *si_sample_ctrl = %x, rdata = %x\n",
  842. si_en_sel, si_ck_sel, si_sample_ctrl, rdata);
  843. found = 1;
  844. break;
  845. }
  846. PWRAPERR("si_en_sel = %x, si_ck_sel = %x, *si_sample_ctrl = %x, rdata = %x\n",
  847. si_en_sel, si_ck_sel, si_sample_ctrl, rdata);
  848. }
  849. if (found == 1)
  850. break;
  851. }
  852. if (found == 0) {
  853. result_faulty |= 0x2;
  854. PWRAPERR("2.result_faulty = %d\n", result_faulty);
  855. pwrap_dump_ap_register();
  856. }
  857. /* TINFO = "[DrvPWRAP_InitSiStrobe] Search For The Data Boundary." */
  858. for (si_dly = 0; si_dly < 8; si_dly++) {
  859. pwrap_write_nochk(EXT_RG_SPI_CON2, si_dly);
  860. error = 0;
  861. #ifndef SPEED_UP_PWRAP_INIT
  862. for (i = 0; i < 30; i++) {
  863. #else
  864. for (i = 0; i < 1; i++) {
  865. #endif
  866. pwrap_write_nochk(EXT_DEW_WRITE_TEST, test_data[i]);
  867. pwrap_read_nochk(EXT_DEW_WRITE_TEST, &rdata);
  868. if ((rdata & 0x7fff) != (test_data[i] & 0x7fff)) {
  869. PWRAPERR("si_dly = %x, *RESERVED = %x, rdata = %x\n",
  870. si_dly, reserved, rdata);
  871. PWRAPERR("si_dly = %x, *EXT_RG_SPI_CON2 = %x, rdata = %x\n",
  872. si_dly, si_dly, rdata);
  873. error = 1;
  874. break;
  875. }
  876. }
  877. if (error == 1)
  878. break;
  879. PWRAPLOG("si_dly = %x, *PMIC_WRAP_RESERVED = %x, rdata = %x\n", si_dly, reserved, rdata);
  880. PWRAPLOG("si_dly = %x, *EXT_RG_SPI_CON2 = %x, rdata = %x\n", si_dly, si_dly, rdata);
  881. }
  882. /* TINFO = "[DrvPWRAP_InitSiStrobe] Change The Sampling Clock Edge To The Next One." */
  883. /*si_sample_ctrl = (si_sample_ctrl & 0xfffffc1f) | ((((si_en_sel << 1) | si_ck_sel) + 1) << 7);*/
  884. si_sample_ctrl = si_sample_ctrl + 0x4000;
  885. WRAP_WR32(PMIC_WRAP_SI_SAMPLE_CTRL, si_sample_ctrl);
  886. if (si_dly == 8)
  887. si_dly--;
  888. PWRAPLOG("For PMIC 1 Done, *PMIC_WRAP_SI_SAMPLE_CTRL = %x, *PMIC_WRAP_RESERVED = %x\n",
  889. si_sample_ctrl, reserved);
  890. PWRAPLOG("For PMIC 1 Done, *PMIC_WRAP_SI_SAMPLE_CTRL = %x, *EXT_RG_SPI_CON2 = %x\n",
  891. si_sample_ctrl, si_dly);
  892. }
  893. #endif
  894. if (result_faulty != 0)
  895. return result_faulty;
  896. /* Read Test */
  897. pwrap_read_nochk(MT6356_DEW_READ_TEST, &rdata);
  898. if (rdata != DEFAULT_VALUE_READ_TEST) {
  899. PWRAPERR("dio Read Test Failed, rdata = %x, exp = 0x5aa5\n",
  900. rdata);
  901. return 0x10;
  902. }
  903. #ifdef DUAL_PMICS
  904. pwrap_read_nochk(EXT_DEW_READ_TEST, &rdata);
  905. if (rdata != DEFAULT_VALUE_READ_TEST) {
  906. PWRAPERR("dio Read Test Failed, EXT rdata = %x, exp = 0x5aa5\n",
  907. rdata);
  908. return 0x11;
  909. }
  910. #endif
  911. return 0;
  912. }
  913. static int __pwrap_InitSPISLV(void)
  914. {
  915. pwrap_write_nochk(MT6356_FILTER_CON0, 0xf0); /* turn on IO filter function */
  916. /* pwrap_write_nochk(MT6356_BM_TOP_CKHWEN_CON0_SET, 0x80); *//* turn on SPI slave DCM */
  917. pwrap_write_nochk(MT6356_SMT_CON1, 0xf); /* turn on IO SMT function to improve noise immunity */
  918. pwrap_write_nochk(MT6356_GPIO_PULLEN0_CLR, 0xf0); /* turn off IO pull function for power saving */
  919. pwrap_write_nochk(MT6356_RG_SPI_CON0, 0x1); /* turn off IO pull function for power saving */
  920. #ifdef DUAL_PMICS
  921. pwrap_write_nochk(EXT_FILTER_CON0, 0xf); /* turn on IO filter function */
  922. pwrap_write_nochk(EXT_TOP_CKHWEN_CON0_SET, 0x80); /* turn on SPI slave DCM */
  923. pwrap_write_nochk(EXT_SMT_CON1, 0xf); /* turn on IO SMT function to improve noise immunity */
  924. pwrap_write_nochk(EXT_RG_SPI_CON, 0x1); /* turn off IO pull function for power saving */
  925. #endif
  926. return 0;
  927. }
  928. /******************************************************
  929. * Function : _pwrap_reset_spislv()
  930. * Description :
  931. * Parameter :
  932. * Return :
  933. ******************************************************/
  934. static signed int _pwrap_reset_spislv(void)
  935. {
  936. unsigned int ret = 0;
  937. unsigned int return_value = 0;
  938. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, DISABLE_ALL);
  939. WRAP_WR32(PMIC_WRAP_WRAP_EN, 0x0);
  940. WRAP_WR32(PMIC_WRAP_MUX_SEL, MANUAL_MODE);
  941. WRAP_WR32(PMIC_WRAP_MAN_EN, 0x1);
  942. WRAP_WR32(PMIC_WRAP_DIO_EN, 0x0);
  943. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_CSL << 8));
  944. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_OUTS << 8));
  945. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_CSH << 8));
  946. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_OUTS << 8));
  947. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_OUTS << 8));
  948. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_OUTS << 8));
  949. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_OUTS << 8));
  950. return_value =
  951. wait_for_state_ready_init(wait_for_sync, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  952. if (return_value != 0) {
  953. PWRAPERR("reset_spislv fail,ret=%x\n", return_value);
  954. ret = E_PWR_TIMEOUT;
  955. goto timeout;
  956. }
  957. WRAP_WR32(PMIC_WRAP_MAN_EN, 0x0);
  958. WRAP_WR32(PMIC_WRAP_MUX_SEL, WRAPPER_MODE);
  959. timeout:
  960. WRAP_WR32(PMIC_WRAP_MAN_EN, 0x0);
  961. WRAP_WR32(PMIC_WRAP_MUX_SEL, WRAPPER_MODE);
  962. return ret;
  963. }
  964. static signed int _pwrap_init_reg_clock(unsigned int regck_sel)
  965. {
  966. unsigned int rdata;
  967. WRAP_WR32(PMIC_WRAP_EXT_CK_WRITE, 0x1);
  968. #ifndef SLV_CLK_1M
  969. #ifndef DUAL_PMICS
  970. /* Set Read Dummy Cycle Number (Slave Clock is 18MHz) */
  971. _pwrap_wacs2_nochk(1, MT6356_DEW_RDDMY_NO, 0x8, &rdata);
  972. WRAP_WR32(PMIC_WRAP_RDDMY, 0x8);
  973. PWRAPLOG("NO_SLV_CLK_1M Set Read Dummy Cycle\n");
  974. #else
  975. _pwrap_wacs2_nochk(1, PMIC_DEW_RDDMY_NO_ADDR, 0x8, &rdata);
  976. _pwrap_wacs2_nochk(1, EXT_DEW_RDDMY_NO, 0x8, &rdata);
  977. WRAP_WR32(PMIC_WRAP_RDDMY, 0x0808);
  978. PWRAPLOG("NO_SLV_CLK_1M Set Read Dummy Cycle dual_pmics\n");
  979. #endif
  980. #else
  981. #ifndef DUAL_PMICS
  982. /* Set Read Dummy Cycle Number (Slave Clock is 1MHz) */
  983. _pwrap_wacs2_nochk(1, PMIC_DEW_RDDMY_NO_ADDR, 0x68, &rdata);
  984. WRAP_WR32(PMIC_WRAP_RDDMY, 0x68);
  985. PWRAPLOG("SLV_CLK_1M Set Read Dummy Cycle\n");
  986. #else
  987. _pwrap_wacs2_nochk(1, PMIC_DEW_RDDMY_NO_ADDR, 0x68, &rdata);
  988. _pwrap_wacs2_nochk(1, EXT_DEW_RDDMY_NO, 0x68, &rdata);
  989. WRAP_WR32(PMIC_WRAP_RDDMY, 0x6868);
  990. PWRAPLOG("SLV_CLK_1M Set Read Dummy Cycle dual_pmics\n");
  991. #endif
  992. #endif
  993. /* Config SPI Waveform according to reg clk */
  994. if (regck_sel == 1) { /* Slave Clock is 18MHz */
  995. /* wait data written into register => 4T_PMIC:
  996. * CSHEXT_WRITE_START+EXT_CK+CSHEXT_WRITE_END+CSLEXT_START
  997. */
  998. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x0);
  999. WRAP_WR32(PMIC_WRAP_CSHEXT_READ , 0x0);
  1000. WRAP_WR32(PMIC_WRAP_CSLEXT_WRITE, 0x0);
  1001. WRAP_WR32(PMIC_WRAP_CSLEXT_READ, 0x0);
  1002. } else { /*Safe Mode*/
  1003. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x0f0f);
  1004. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0x0f0f);
  1005. WRAP_WR32(PMIC_WRAP_CSLEXT_WRITE, 0x0f0f);
  1006. WRAP_WR32(PMIC_WRAP_CSLEXT_READ, 0x0f0f);
  1007. }
  1008. return 0;
  1009. }
  1010. static int _pwrap_wacs2_write_test(int pmic_no)
  1011. {
  1012. unsigned int rdata;
  1013. if (pmic_no == 0) {
  1014. pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0xa55a);
  1015. pwrap_read_nochk(PMIC_DEW_WRITE_TEST_ADDR, &rdata);
  1016. if (rdata != 0xa55a) {
  1017. PWRAPERR("Error: w_rdata = %x, exp = 0xa55a\n", rdata);
  1018. return E_PWR_WRITE_TEST_FAIL;
  1019. }
  1020. }
  1021. #ifdef DUAL_PMICS
  1022. if (pmic_no == 1) {
  1023. pwrap_write_nochk(EXT_DEW_WRITE_TEST, 0xa55a);
  1024. pwrap_read_nochk(EXT_DEW_WRITE_TEST, &rdata);
  1025. if (rdata != 0xa55a) {
  1026. PWRAPERR("Error: ext_w_rdata = %x, exp = 0xa55a\n", rdata);
  1027. return E_PWR_WRITE_TEST_FAIL;
  1028. }
  1029. }
  1030. #endif
  1031. return 0;
  1032. }
  1033. static unsigned int pwrap_read_test(void)
  1034. {
  1035. unsigned int rdata = 0;
  1036. unsigned int return_value = 0;
  1037. /* Read Test */
  1038. return_value = pwrap_wacs2_read(PMIC_DEW_READ_TEST_ADDR, &rdata);
  1039. if (rdata != DEFAULT_VALUE_READ_TEST) {
  1040. PWRAPERR("Error: r_rdata=0x%x, exp=0x5aa5,ret=0x%x\n", rdata, return_value);
  1041. return E_PWR_READ_TEST_FAIL;
  1042. }
  1043. PWRAPLOG("Read Test pass,return_value=%d\n", return_value);
  1044. return 0;
  1045. }
  1046. static unsigned int pwrap_write_test(void)
  1047. {
  1048. unsigned int rdata = 0;
  1049. unsigned int sub_return = 0;
  1050. unsigned int sub_return1 = 0;
  1051. /* Write test using WACS2 */
  1052. PWRAPLOG("start pwrap_write\n");
  1053. sub_return = pwrap_wacs2_write(PMIC_DEW_WRITE_TEST_ADDR, DEFAULT_VALUE_READ_TEST);
  1054. PWRAPLOG("after pwrap_write\n");
  1055. sub_return1 = pwrap_wacs2_read(PMIC_DEW_WRITE_TEST_ADDR, &rdata);
  1056. if ((rdata != DEFAULT_VALUE_READ_TEST) || (sub_return != 0) || (sub_return1 != 0)) {
  1057. PWRAPERR("Err:,w_rdata=0x%x,exp=0xa55a,sub_return=0x%x,sub_return1=0x%x\n", rdata, sub_return,
  1058. sub_return1);
  1059. return E_PWR_INIT_WRITE_TEST;
  1060. }
  1061. PWRAPLOG("write Test pass\n");
  1062. return 0;
  1063. }
  1064. static void pwrap_ut(unsigned int ut_test)
  1065. {
  1066. switch (ut_test) {
  1067. case 1:
  1068. pwrap_write_test();
  1069. break;
  1070. case 2:
  1071. pwrap_read_test();
  1072. break;
  1073. case 3:
  1074. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  1075. pwrap_wacs2_ipi(0x10010000 + 0xD8, 0xffffffff, (WRITE_CMD | WRITE_PMIC_WRAP));
  1076. break;
  1077. #endif
  1078. default:
  1079. PWRAPLOG("default test.\n");
  1080. break;
  1081. }
  1082. }
  1083. static void _pwrap_set_DCM(void)
  1084. {
  1085. #if 0
  1086. #ifndef PWRAP_DCM_ALL_ON
  1087. WRAP_WR32(PMIC_WRAP_DCM_EN, 1);
  1088. #else
  1089. WRAP_WR32(PMIC_WRAP_DCM_EN, 0x3fff);
  1090. #endif
  1091. /* no debounce */
  1092. WRAP_WR32(PMIC_WRAP_DCM_DBC_PRD, DISABLE);
  1093. #endif
  1094. }
  1095. signed int pwrap_init(void)
  1096. {
  1097. signed int sub_return = 0;
  1098. unsigned int rdata = 0x0;
  1099. PWRAPLOG("pwrap_init start!!!!!!!!!!!!!\n");
  1100. #ifdef DUAL_PMICS
  1101. /* For dual pmic used */
  1102. #define PWRAP_GPIO_DRIVING_ADDR 0x11C100B0
  1103. WRAP_WR32(PWRAP_GPIO_DRIVING_ADDR, WRAP_RD32(PWRAP_GPIO_DRIVING_ADDR)|(0x1<<4));
  1104. PWRAPLOG("Preloader GPIO228-231 driving = 0x%x \n", WRAP_RD32(PWRAP_GPIO_DRIVING_ADDR));
  1105. #endif
  1106. __pwrap_spi_clk_set();
  1107. PWRAPLOG("__pwrap_spi_clk_set ok\n");
  1108. /* Enable DCM */
  1109. _pwrap_set_DCM();
  1110. PWRAPLOG("Enable DCM ok\n");
  1111. /* Reset SPISLV */
  1112. sub_return = _pwrap_reset_spislv();
  1113. if (sub_return != 0) {
  1114. PWRAPERR("reset_spislv fail,ret=%x\n", sub_return);
  1115. return E_PWR_INIT_RESET_SPI;
  1116. }
  1117. PWRAPLOG("Reset SPISLV ok\n");
  1118. /* Enable WRAP */
  1119. WRAP_WR32(PMIC_WRAP_WRAP_EN, 0x1);
  1120. PWRAPLOG("Enable WRAP ok\n");
  1121. #if MTK_PLATFORM_MT6356
  1122. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x4); /* ONLY WACS2 */
  1123. #else
  1124. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, WACS2); /* ONLY WACS2 */
  1125. #endif
  1126. /* Enable WACS2 */
  1127. WRAP_WR32(PMIC_WRAP_WACS2_EN, 0x1);
  1128. PWRAPLOG("Enable WACSW2 ok\n");
  1129. /* SPI Waveform Configuration. 0:safe mode, 1:18MHz */
  1130. sub_return = _pwrap_init_reg_clock(1);
  1131. if (sub_return != 0) {
  1132. PWRAPERR("init_reg_clock fail,ret=%x\n", sub_return);
  1133. return E_PWR_INIT_REG_CLOCK;
  1134. }
  1135. PWRAPLOG("_pwrap_init_reg_clock ok\n");
  1136. /* SPI Slave Configuration */
  1137. sub_return = __pwrap_InitSPISLV();
  1138. if (sub_return != 0) {
  1139. PWRAPERR("Error: DrvPWRAP_InitSPISLV Failed, sub_return = %x", sub_return);
  1140. return -1;
  1141. }
  1142. /* Enable DIO mode */
  1143. sub_return = _pwrap_init_dio(1);
  1144. if (sub_return != 0) {
  1145. PWRAPERR("dio test error,err=%x, ret=%x\n", 0x11, sub_return);
  1146. return E_PWR_INIT_DIO;
  1147. }
  1148. PWRAPLOG("_pwrap_init_dio ok\n");
  1149. /* Input data calibration flow; */
  1150. sub_return = _pwrap_init_sistrobe(0);
  1151. if (sub_return != 0) {
  1152. PWRAPERR("InitSiStrobe fail,ret=%x\n", sub_return);
  1153. return E_PWR_INIT_SIDLY;
  1154. }
  1155. PWRAPLOG("_pwrap_init_sistrobe ok\n");
  1156. #if 0
  1157. /* Enable Encryption */
  1158. sub_return = _pwrap_init_cipher();
  1159. if (sub_return != 0) {
  1160. PWRAPERR("Encryption fail, ret=%x\n", sub_return);
  1161. return E_PWR_INIT_CIPHER;
  1162. }
  1163. PWRAPLOG("_pwrap_init_cipher ok\n");
  1164. #endif
  1165. /* Write test using WACS2. check Wtiet test default value */
  1166. sub_return = _pwrap_wacs2_write_test(0);
  1167. if (rdata != 0) {
  1168. PWRAPERR("write test 0 fail\n");
  1169. return E_PWR_INIT_WRITE_TEST;
  1170. }
  1171. #ifdef DUAL_PMICS
  1172. sub_return = _pwrap_wacs2_write_test(1);
  1173. if (rdata != 0) {
  1174. PWRAPERR("write test 1 fail\n");
  1175. return E_PWR_INIT_WRITE_TEST;
  1176. }
  1177. #endif
  1178. PWRAPLOG("_pwrap_wacs2_write_test ok\n");
  1179. /* Status update function initialization
  1180. * 1. Signature Checking using CRC (CRC 0 only)
  1181. * 2. EINT update
  1182. * 3. Read back Auxadc thermal data for GPS
  1183. */
  1184. _pwrap_InitStaUpd();
  1185. PWRAPLOG("_pwrap_InitStaUpd ok\n");
  1186. #if (MTK_PLATFORM_MT6356)
  1187. /* PMIC WRAP priority adjust */
  1188. WRAP_WR32(PMIC_WRAP_PRIORITY_USER_SEL_2, 0x0b09080a);
  1189. WRAP_WR32(PMIC_WRAP_ARBITER_OUT_SEL_2, 0x0b080a09);
  1190. #endif
  1191. /* PMIC_WRAP starvation setting */
  1192. _pwrap_starve_set();
  1193. PWRAPLOG("_pwrap_starve_set ok\n");
  1194. /* PMIC_WRAP enables */
  1195. _pwrap_enable();
  1196. PWRAPLOG("_pwrap_enable ok\n");
  1197. /* Backward Compatible Settings */
  1198. /* WRAP_WR32(PMIC_WRAP_BWC_OPTIONS, WRAP_RD32(PMIC_WRAP_BWC_OPTIONS) | 0x8); */
  1199. /* Initialization Done */
  1200. WRAP_WR32(PMIC_WRAP_INIT_DONE0, 0x1);
  1201. WRAP_WR32(PMIC_WRAP_INIT_DONE2, 0x1);
  1202. PWRAPLOG("pwrap_init Done!!!!!!!!!\n");
  1203. /* WACS2 UT */
  1204. pwrap_ut(1);
  1205. pwrap_ut(2);
  1206. PWRAPLOG("channel pass \n\r ");
  1207. #if 0
  1208. unsigned int rgdata=0;
  1209. pwrap_read(MT6356_PPCCTL0, &rgdata);
  1210. rgdata |= 0x1;
  1211. pwrap_write(MT6356_PPCCTL0, rgdata);
  1212. pwrap_read(MT6356_PPCCTL0, &rgdata);
  1213. PWRAPLOG("PMIC_RG_PWRHOLD=%x(should be 1)\n", rgdata);
  1214. #endif
  1215. return 0;
  1216. }
  1217. /*-------------------pwrap debug---------------------*/
  1218. static inline void pwrap_dump_ap_register(void)
  1219. {
  1220. unsigned int i = 0;
  1221. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  1222. unsigned int *reg_addr;
  1223. #else
  1224. unsigned int reg_addr;
  1225. #endif
  1226. unsigned int reg_value = 0;
  1227. PWRAPERR("dump reg\n");
  1228. for (i = 0; i <= PMIC_WRAP_REG_RANGE; i++) {
  1229. reg_addr = (unsigned int *) (PMIC_WRAP_BASE + i * 4);
  1230. #if (PMIC_WRAP_KERNEL)
  1231. reg_value = WRAP_RD32(((unsigned int *) (PMIC_WRAP_BASE + i * 4)));
  1232. #else
  1233. reg_value = WRAP_RD32(reg_addr);
  1234. #endif
  1235. PWRAPERR("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  1236. }
  1237. }
  1238. void pwrap_dump_all_register(void)
  1239. {
  1240. pwrap_dump_ap_register();
  1241. }
  1242. static int is_pwrap_init_done(void)
  1243. {
  1244. int ret = 0;
  1245. ret = WRAP_RD32(PMIC_WRAP_INIT_DONE2);
  1246. PWRAPLOG("is_pwrap_init_done %d\n", ret);
  1247. if ((ret & 0x1) == 1)
  1248. return 0;
  1249. return -1;
  1250. }
  1251. signed int pwrap_init_lk(void)
  1252. {
  1253. unsigned int pwrap_ret = 0, i = 0;
  1254. PWRAPFUC();
  1255. if (0 == is_pwrap_init_done()) {
  1256. PWRAPLOG("[PMIC_WRAP]wrap_init already init, do nothing\n");
  1257. return 0;
  1258. }
  1259. for (i = 0; i < 3; i++) {
  1260. pwrap_ret = pwrap_init();
  1261. if (pwrap_ret != 0) {
  1262. PWRAPERR("init fail, ret=%x.\n",pwrap_ret);
  1263. if (i >= 2)
  1264. ASSERT(0);
  1265. } else {
  1266. PWRAPLOG("init pass, ret=%x.\n",pwrap_ret);
  1267. break;
  1268. }
  1269. }
  1270. return 0;
  1271. }
  1272. #endif /*endif PMIC_WRAP_NO_PMIC */