mt_pmic_wrap_init_v1.c 44 KB

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