mt_pmic_wrap_init.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. #include <kernel/thread.h>
  48. #if (PMIC_WRAP_PRELOADER)
  49. #elif (PMIC_WRAP_LK)
  50. #elif (PMIC_WRAP_KERNEL)
  51. #elif (PMIC_WRAP_CTP)
  52. #include <gpio.h>
  53. #include <upmu_hw.h>
  54. #else
  55. ### Compile error, check SW ENV define
  56. #endif
  57. /************* marco ******************************************************/
  58. #if (PMIC_WRAP_PRELOADER)
  59. #elif (PMIC_WRAP_LK)
  60. #elif (PMIC_WRAP_KERNEL)
  61. #elif (PMIC_WRAP_SCP)
  62. #elif (PMIC_WRAP_CTP)
  63. #else
  64. ### Compile error, check SW ENV define
  65. #endif
  66. #ifdef PMIC_WRAP_NO_PMIC
  67. #if !(PMIC_WRAP_KERNEL)
  68. signed int pwrap_wacs2(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata)
  69. {
  70. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  71. return 0;
  72. }
  73. signed int pwrap_read(unsigned int adr, unsigned int *rdata)
  74. {
  75. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  76. return 0;
  77. }
  78. signed int pwrap_write(unsigned int adr, unsigned int wdata)
  79. {
  80. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  81. return 0;
  82. }
  83. #endif
  84. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata)
  85. {
  86. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  87. return 0;
  88. }
  89. /* Provide PMIC write API */
  90. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata)
  91. {
  92. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  93. return 0;
  94. }
  95. signed int pwrap_read_nochk(unsigned int adr, unsigned int *rdata)
  96. {
  97. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  98. return 0;
  99. }
  100. signed int pwrap_write_nochk(unsigned int adr, unsigned int wdata)
  101. {
  102. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  103. return 0;
  104. }
  105. /*
  106. *pmic_wrap init,init wrap interface
  107. *
  108. */
  109. signed int pwrap_init(void)
  110. {
  111. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  112. return 0;
  113. }
  114. signed int pwrap_init_preloader(void)
  115. {
  116. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  117. return 0;
  118. }
  119. signed int pwrap_init_lk(void)
  120. {
  121. PWRAPLOG("[PMIC_WRAP]There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  122. return 0;
  123. }
  124. #else /* #ifdef PMIC_WRAP_NO_PMIC */
  125. /*********************start ---internal API***********************************/
  126. static int _pwrap_timeout_ns(unsigned long long start_time_ns, unsigned long long timeout_time_ns);
  127. static unsigned long long _pwrap_get_current_time(void);
  128. static unsigned long long _pwrap_time2ns(unsigned long long time_us);
  129. static void _pwrap_enable(void);
  130. static void _pwrap_starve_set(void);
  131. static signed int _pwrap_wacs2_nochk(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata);
  132. static signed int pwrap_wacs2_hal(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata);
  133. /*********************test API************************************************/
  134. static inline void pwrap_dump_ap_register(void);
  135. static unsigned int pwrap_write_test(void);
  136. static unsigned int pwrap_read_test(void);
  137. static signed int pwrap_reset_pattern(void);
  138. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata);
  139. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata);
  140. static unsigned int si_sample_ctrl = 0;
  141. static struct pwrap_rc_info {
  142. unsigned int wacs2_cmd;
  143. unsigned int dcxo_en;
  144. unsigned int dcxo_conn_adr0;
  145. unsigned int dcxo_conn_wdata0;
  146. unsigned int dcxo_conn_adr1;
  147. unsigned int dcxo_conn_wdata1;
  148. unsigned int dcxo_nfc_adr0;
  149. unsigned int dcxo_nfc_wdata0;
  150. unsigned int dcxo_nfc_adr1;
  151. unsigned int dcxo_nfc_wdata1;
  152. };
  153. /************* end--internal API**********************************************/
  154. /*********************** external API for pmic_wrap user ************************/
  155. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata)
  156. {
  157. pwrap_wacs2(0, adr, 0, rdata);
  158. return 0;
  159. }
  160. /* Provide PMIC write API */
  161. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata)
  162. {
  163. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  164. unsigned int flag;
  165. flag = WRITE_CMD | (1 << WRITE_PMIC);
  166. pwrap_wacs2_ipi(adr, wdata, flag);
  167. #else
  168. pwrap_wacs2(1, adr, wdata, 0);
  169. #endif
  170. return 0;
  171. }
  172. signed int pwrap_read(unsigned int adr, unsigned int *rdata)
  173. {
  174. return pwrap_wacs2(0,adr,0,rdata);
  175. }
  176. signed int pwrap_write(unsigned int adr, unsigned int wdata)
  177. {
  178. return pwrap_wacs2(1,adr,wdata,0);
  179. }
  180. /******************************************************************************
  181. *wrapper timeout
  182. *****************************************************************************/
  183. /*use the same API name with kernel driver
  184. *however,the timeout API in uboot use tick instead of ns
  185. */
  186. #ifdef PWRAP_TIMEOUT
  187. static unsigned long long _pwrap_get_current_time(void)
  188. {
  189. return gpt4_get_current_tick();
  190. }
  191. static int _pwrap_timeout_ns(unsigned long long start_time_ns, unsigned long long timeout_time_ns)
  192. {
  193. return gpt4_timeout_tick(start_time_ns, timeout_time_ns);
  194. }
  195. static unsigned long long _pwrap_time2ns(unsigned long long time_us)
  196. {
  197. return gpt4_time2tick_us(time_us);
  198. }
  199. #else
  200. static unsigned long long _pwrap_get_current_time(void)
  201. {
  202. return 0;
  203. }
  204. static int _pwrap_timeout_ns(unsigned long long start_time_ns, unsigned long long elapse_time)
  205. {
  206. return 0;
  207. }
  208. static unsigned long long _pwrap_time2ns(unsigned long long time_us)
  209. {
  210. return 0;
  211. }
  212. #endif
  213. /* ##################################################################### */
  214. /* define macro and inline function (for do while loop) */
  215. /* ##################################################################### */
  216. typedef unsigned int(*loop_condition_fp) (unsigned int); /* define a function pointer */
  217. static inline unsigned int wait_for_fsm_idle(unsigned int x)
  218. {
  219. return GET_WACS2_FSM(x) != WACS_FSM_IDLE;
  220. }
  221. static inline unsigned int wait_for_fsm_vldclr(unsigned int x)
  222. {
  223. return GET_WACS2_FSM(x) != WACS_FSM_WFVLDCLR;
  224. }
  225. static inline unsigned int wait_for_sync(unsigned int x)
  226. {
  227. return GET_SYNC_IDLE2(x) != WACS_SYNC_IDLE;
  228. }
  229. static inline unsigned int wait_for_idle_and_sync(unsigned int x)
  230. {
  231. return (GET_WACS2_FSM(x) != WACS_FSM_IDLE) || (GET_SYNC_IDLE2(x) != WACS_SYNC_IDLE);
  232. }
  233. static inline unsigned int wait_for_wrap_idle(unsigned int x)
  234. {
  235. return (GET_WRAP_FSM(x) != 0x0) || (GET_WRAP_CH_DLE_RESTCNT(x) != 0x0);
  236. }
  237. static inline unsigned int wait_for_wrap_state_idle(unsigned int x)
  238. {
  239. return GET_WRAP_AG_DLE_RESTCNT(x) != 0;
  240. }
  241. static inline unsigned int wait_for_man_idle_and_noreq(unsigned int x)
  242. {
  243. return (GET_MAN_REQ(x) != MAN_FSM_NO_REQ) || (GET_MAN_FSM(x) != MAN_FSM_IDLE);
  244. }
  245. static inline unsigned int wait_for_man_vldclr(unsigned int x)
  246. {
  247. return GET_MAN_FSM(x) != MAN_FSM_WFVLDCLR;
  248. }
  249. static inline unsigned int wait_for_cipher_ready(unsigned int x)
  250. {
  251. return x != 3;
  252. }
  253. static inline unsigned int wait_for_stdupd_idle(unsigned int x)
  254. {
  255. return GET_STAUPD_FSM(x) != 0x0;
  256. }
  257. /**************used at _pwrap_wacs2_nochk*************************************/
  258. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  259. static inline unsigned int wait_for_state_ready_init(loop_condition_fp fp, unsigned int timeout_us,
  260. void *wacs_register, unsigned int *read_reg)
  261. #else
  262. static inline unsigned int wait_for_state_ready_init(loop_condition_fp fp, unsigned int timeout_us,
  263. volatile unsigned int *wacs_register, unsigned int *read_reg)
  264. #endif
  265. {
  266. unsigned long long start_time_ns = 0, timeout_ns = 0;
  267. unsigned int reg_rdata = 0x0;
  268. start_time_ns = _pwrap_get_current_time();
  269. timeout_ns = _pwrap_time2ns(timeout_us);
  270. do {
  271. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  272. PWRAPERR("ready_init timeout\n");
  273. pwrap_dump_ap_register();
  274. return E_PWR_WAIT_IDLE_TIMEOUT;
  275. }
  276. reg_rdata = WRAP_RD32(wacs_register);
  277. } while (fp(reg_rdata));
  278. if (read_reg)
  279. *read_reg = reg_rdata;
  280. return 0;
  281. }
  282. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  283. static inline unsigned int wait_for_state_idle(loop_condition_fp fp, unsigned int timeout_us, void *wacs_register,
  284. void *wacs_vldclr_register, unsigned int *read_reg)
  285. #else
  286. static inline unsigned int wait_for_state_idle(loop_condition_fp fp, unsigned int timeout_us,
  287. volatile unsigned int *wacs_register, volatile unsigned int *wacs_vldclr_register, unsigned int *read_reg)
  288. #endif
  289. {
  290. unsigned long long start_time_ns = 0, timeout_ns = 0;
  291. unsigned int reg_rdata;
  292. start_time_ns = _pwrap_get_current_time();
  293. timeout_ns = _pwrap_time2ns(timeout_us);
  294. do {
  295. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  296. PWRAPERR("state_idle timeout\n");
  297. pwrap_dump_ap_register();
  298. return E_PWR_WAIT_IDLE_TIMEOUT;
  299. }
  300. reg_rdata = WRAP_RD32(wacs_register);
  301. if (GET_WACS2_INIT_DONE2(reg_rdata) != WACS_INIT_DONE) {
  302. PWRAPERR("init isn't finished\n");
  303. pwrap_dump_ap_register();
  304. return E_PWR_NOT_INIT_DONE;
  305. }
  306. switch (GET_WACS2_FSM(reg_rdata)) {
  307. case WACS_FSM_WFVLDCLR:
  308. WRAP_WR32(wacs_vldclr_register, 1);
  309. PWRAPERR("WACS_FSM = VLDCLR\n");
  310. pwrap_dump_ap_register();
  311. break;
  312. case WACS_FSM_WFDLE:
  313. PWRAPERR("WACS_FSM = WFDLE\n");
  314. pwrap_dump_ap_register();
  315. break;
  316. case WACS_FSM_REQ:
  317. PWRAPERR("WACS_FSM = REQ\n");
  318. pwrap_dump_ap_register();
  319. break;
  320. default:
  321. break;
  322. }
  323. } while (fp(reg_rdata));
  324. if (read_reg)
  325. *read_reg = reg_rdata;
  326. return 0;
  327. }
  328. /**************used at pwrap_wacs2********************************************/
  329. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  330. static inline unsigned int wait_for_state_ready(loop_condition_fp fp, unsigned int timeout_us, void *wacs_register,
  331. unsigned int *read_reg)
  332. #else
  333. static inline unsigned int wait_for_state_ready(loop_condition_fp fp, unsigned int timeout_us,
  334. volatile unsigned int *wacs_register, unsigned int *read_reg)
  335. #endif
  336. {
  337. unsigned long long start_time_ns = 0, timeout_ns = 0;
  338. unsigned int reg_rdata;
  339. start_time_ns = _pwrap_get_current_time();
  340. timeout_ns = _pwrap_time2ns(timeout_us);
  341. do {
  342. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  343. PWRAPERR("state_ready timeout\n");
  344. pwrap_dump_ap_register();
  345. return E_PWR_WAIT_IDLE_TIMEOUT;
  346. }
  347. reg_rdata = WRAP_RD32(wacs_register);
  348. if (GET_WACS2_INIT_DONE2(reg_rdata) != WACS_INIT_DONE) {
  349. PWRAPERR("init isn't finished\n");
  350. pwrap_dump_ap_register();
  351. return E_PWR_NOT_INIT_DONE;
  352. }
  353. } while (fp(reg_rdata));
  354. if (read_reg)
  355. *read_reg = reg_rdata;
  356. return 0;
  357. }
  358. /******************************************************
  359. * Function : pwrap_wacs2()
  360. * Description :
  361. * Parameter :
  362. * Return :
  363. ******************************************************/
  364. signed int pwrap_wacs2(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata)
  365. {
  366. unsigned int reg_rdata = 0;
  367. unsigned int wacs_write = 0;
  368. unsigned int wacs_adr = 0;
  369. unsigned int wacs_cmd = 0;
  370. unsigned int return_value = 0;
  371. signed int pwrap_ret = 0;
  372. /* Check argument validation */
  373. if ((write & ~(0x1)) != 0)
  374. return E_PWR_INVALID_RW;
  375. if ((adr & ~(0xffff)) != 0)
  376. return E_PWR_INVALID_ADDR;
  377. if ((wdata & ~(0xffff)) != 0)
  378. return E_PWR_INVALID_WDAT;
  379. enter_critical_section();
  380. /* Check IDLE & INIT_DONE in advance */
  381. return_value =
  382. wait_for_state_idle(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA,
  383. PMIC_WRAP_WACS2_VLDCLR, 0);
  384. if (return_value != 0) {
  385. PWRAPERR("fsm_idle fail,ret=%d\n", return_value);
  386. goto FAIL;
  387. }
  388. RETRY:
  389. wacs_write = write << 31;
  390. wacs_adr = (adr >> 1) << 16;
  391. wacs_cmd = wacs_write | wacs_adr | wdata;
  392. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  393. if (write == 0) {
  394. if (rdata == NULL) {
  395. PWRAPERR("rdata NULL\n");
  396. return_value = E_PWR_INVALID_ARG;
  397. goto FAIL;
  398. }
  399. return_value =
  400. wait_for_state_ready(wait_for_fsm_vldclr, TIMEOUT_READ, PMIC_WRAP_WACS2_RDATA,
  401. &reg_rdata);
  402. if (return_value != 0) {
  403. PWRAPERR("fsm_vldclr fail,ret=%d\n", return_value);
  404. return_value += 1;
  405. goto FAIL;
  406. }
  407. *rdata = GET_WACS2_RDATA(reg_rdata);
  408. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  409. }
  410. FAIL:
  411. if (return_value != 0) {
  412. PWRAPERR("pwrap_wacs2 fail, ret=%d\n", return_value);
  413. pwrap_ret = pwrap_reset_pattern();
  414. if (pwrap_ret != 0) {
  415. PWRAPERR("pwrap_reset_pattern fail, ret=%x\n", pwrap_ret);
  416. pwrap_dump_ap_register();
  417. }
  418. else {
  419. dprintf(CRITICAL, PWRAPTAG "retry cmd: 0x%x, 0x%x, 0x%x\n", wacs_cmd, write, adr);
  420. goto RETRY;
  421. }
  422. }
  423. exit_critical_section();
  424. return return_value;
  425. }
  426. /*********************internal API for pwrap_init***************************/
  427. /**********************************
  428. * Function : _pwrap_wacs2_nochk()
  429. * Description :
  430. * Parameter :
  431. * Return :
  432. ***********************************/
  433. signed int pwrap_read_nochk(unsigned int adr, unsigned int *rdata)
  434. {
  435. return _pwrap_wacs2_nochk(0, adr, 0, rdata);
  436. }
  437. signed int pwrap_write_nochk(unsigned int adr, unsigned int wdata)
  438. {
  439. return _pwrap_wacs2_nochk(1, adr, wdata, 0);
  440. }
  441. static signed int _pwrap_wacs2_nochk(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata)
  442. {
  443. unsigned int reg_rdata = 0x0;
  444. unsigned int wacs_write = 0x0;
  445. unsigned int wacs_adr = 0x0;
  446. unsigned int wacs_cmd = 0x0;
  447. unsigned int return_value = 0x0;
  448. /* Check argument validation */
  449. if ((write & ~(0x1)) != 0)
  450. return E_PWR_INVALID_RW;
  451. if ((adr & ~(0xffff)) != 0)
  452. return E_PWR_INVALID_ADDR;
  453. if ((wdata & ~(0xffff)) != 0)
  454. return E_PWR_INVALID_WDAT;
  455. enter_critical_section();
  456. /* Check IDLE */
  457. return_value =
  458. wait_for_state_ready_init(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  459. if (return_value != 0) {
  460. PWRAPERR("write fail,ret=%x\n", return_value);
  461. exit_critical_section();
  462. return return_value;
  463. }
  464. wacs_write = write << 31;
  465. wacs_adr = (adr >> 1) << 16;
  466. wacs_cmd = wacs_write | wacs_adr | wdata;
  467. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  468. if (write == 0) {
  469. if (rdata == NULL) {
  470. PWRAPERR("rdata NULL\n");
  471. return_value = E_PWR_INVALID_ARG;
  472. exit_critical_section();
  473. return return_value;
  474. }
  475. return_value =
  476. wait_for_state_ready_init(wait_for_fsm_vldclr, TIMEOUT_READ,
  477. PMIC_WRAP_WACS2_RDATA, &reg_rdata);
  478. if (return_value != 0) {
  479. PWRAPERR("fsm_vldclr fail,ret=%d\n", return_value);
  480. return_value += 1;
  481. exit_critical_section();
  482. return return_value;
  483. }
  484. *rdata = GET_WACS2_RDATA(reg_rdata);
  485. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  486. }
  487. exit_critical_section();
  488. return 0;
  489. }
  490. static void __pwrap_soft_reset(void)
  491. {
  492. PWRAPLOG("start reset wrapper\n");
  493. WRAP_WR32(INFRA_GLOBALCON_RST2_SET, 0x1);
  494. WRAP_WR32(INFRA_GLOBALCON_RST2_CLR, 0x1);
  495. }
  496. static void __pwrap_spi_clk_set(void)
  497. {
  498. PWRAPLOG("pwrap_spictl reset ok\n");
  499. /* sys_ck cg enable, turn off clock */
  500. WRAP_WR32(MODULE_SW_CG_0_SET, 0x0000000f);
  501. /* turn off clock */
  502. WRAP_WR32(MODULE_SW_CG_2_SET, 0x00000100);
  503. /* Select ULPOSC/16 Clock as SYS, TMR and SPI Clocks in SODI-3.0 and Suspend Modes */
  504. #if !defined(CONFIG_MTK_FPGA)
  505. WRAP_WR32(CLK_CFG_7_CLR, 0x00000097);
  506. WRAP_WR32(CLK_CFG_7_SET, 0x00000003);
  507. WRAP_WR32(CLK_CFG_UPDATE, (0x1 << 28));
  508. #endif
  509. /* Disable Clock Source Control By SPM */
  510. PWRAPLOG("=====PMICW_CLOCK_CTRL===== (Write before): %x\n", WRAP_RD32(PMICW_CLOCK_CTRL));
  511. WRAP_WR32(PMICW_CLOCK_CTRL, (WRAP_RD32(PMICW_CLOCK_CTRL) & ~(0x1 << 2)));
  512. PWRAPLOG("=====PMICW_CLOCK_CTRL===== (Write after): %x\n", WRAP_RD32(PMICW_CLOCK_CTRL));
  513. /* toggle PMIC_WRAP and pwrap_spictl reset */
  514. __pwrap_soft_reset();
  515. /*sys_ck cg enable, turn on clock*/
  516. WRAP_WR32(MODULE_SW_CG_0_CLR, 0x0000000f);
  517. /* turn on clock*/
  518. WRAP_WR32(MODULE_SW_CG_2_CLR, 0x00000100);
  519. PWRAPLOG("spi clk set ....\n");
  520. }
  521. static signed int _pwrap_init_dio_reset(unsigned int dio_en)
  522. {
  523. unsigned int rdata = 0x0;
  524. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x4); /* ONLY WACS2 */
  525. /* wait for WRAP_FSM idle */
  526. do {
  527. rdata = WRAP_RD32(PMIC_WRAP_WRAP_STA);
  528. } while((GET_WRAP_FSM(rdata) != 0x0) || (GET_WRAP_CH_DLE_RESTCNT(rdata) != 0x0));
  529. do {
  530. rdata = WRAP_RD32(PMIC_WRAP_WACS2_RDATA);
  531. } while ((GET_WACS2_FSM(rdata) != WACS_FSM_IDLE) || (GET_SYNC_IDLE2(rdata) != WACS_SYNC_IDLE));
  532. #ifndef DUAL_PMICS
  533. WRAP_WR32(PMIC_WRAP_DIO_EN, dio_en);
  534. #else
  535. WRAP_WR32(PMIC_WRAP_DIO_EN, 0x2 | dio_en);
  536. #endif
  537. return 0;
  538. }
  539. static void _pwrap_InitStaUpd(void)
  540. {
  541. #ifndef DUAL_PMICS
  542. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, 0xf5);
  543. #else
  544. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, 0xff);
  545. #endif
  546. #ifdef PMIC_WRAP_CRC_SUPPORT
  547. /* CRC */
  548. #ifndef DUAL_PMICS
  549. pwrap_write_nochk(PMIC_DEW_CRC_EN_ADDR, 0x1);
  550. WRAP_WR32(PMIC_WRAP_CRC_EN, 0x1);
  551. WRAP_WR32(PMIC_WRAP_SIG_ADR, PMIC_DEW_CRC_VAL_ADDR);
  552. #else
  553. pwrap_write_nochk(PMIC_DEW_CRC_EN_ADDR, 0x1);
  554. pwrap_write_nochk(EXT_DEW_CRC_EN, 0x1);
  555. WRAP_WR32(PMIC_WRAP_CRC_EN, 0x1);
  556. WRAP_WR32(PMIC_WRAP_SIG_ADR, (PMIC_EXT_DEW_CRC_VAL_ADDR << 16 | PMIC_DEW_CRC_VAL_ADDR));
  557. #endif
  558. #else
  559. /* Signature */
  560. #ifndef DUAL_PMICS
  561. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x1);
  562. WRAP_WR32(PMIC_WRAP_SIG_ADR, PMIC_DEW_CRC_VAL_ADDR);
  563. WRAP_WR32(PMIC_WRAP_SIG_VALUE, 0x83);
  564. #else
  565. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x3);
  566. WRAP_WR32(PMIC_WRAP_SIG_ADR, (PMIC_EXT_DEW_CRC_VAL_ADDR << 16) | PMIC_DEW_CRC_VAL_ADDR);
  567. WRAP_WR32(PMIC_WRAP_SIG_VALUE, (0x83 << 16) | 0x83);
  568. #endif
  569. #endif /* end of crc */
  570. WRAP_WR32(PMIC_WRAP_EINT_STA0_ADR, PMIC_CPU_INT_STA_ADDR);
  571. #ifdef DUAL_PMICS
  572. WRAP_WR32(PMIC_WRAP_EINT_STA1_ADR, EXT_INT_STA);
  573. #endif
  574. /* MD ADC Interface */
  575. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_LATEST_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  576. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_WP_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  577. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_0_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  578. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_1_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  579. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_2_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  580. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_3_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  581. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_4_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  582. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_5_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  583. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_6_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  584. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_7_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  585. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_8_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  586. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_9_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  587. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_10_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  588. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_11_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  589. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_12_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  590. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_13_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  591. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_14_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  592. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_15_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  593. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_16_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  594. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_17_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  595. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_18_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  596. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_19_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  597. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_20_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  598. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_21_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  599. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_22_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  600. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_23_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  601. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_24_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  602. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_25_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  603. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_26_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  604. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_27_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  605. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_28_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  606. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_29_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  607. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_30_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  608. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_31_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  609. WRAP_WR32(PMIC_WRAP_INT_GPS_AUXADC_CMD_ADDR, (PMIC_AUXADC_RQST_DCXO_BY_GPS_ADDR << 16) + PMIC_AUXADC_RQST_CH7_ADDR);
  610. WRAP_WR32(PMIC_WRAP_INT_GPS_AUXADC_CMD, (0x0400 << 16) + 0x0080);
  611. WRAP_WR32(PMIC_WRAP_INT_GPS_AUXADC_RDATA_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_GPS_ADDR << 16) + PMIC_AUXADC_ADC_OUT_CH7_BY_AP_ADDR);
  612. WRAP_WR32(PMIC_WRAP_EXT_GPS_AUXADC_RDATA_ADDR, PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  613. }
  614. static void _pwrap_starve_set(void)
  615. {
  616. WRAP_WR32(PMIC_WRAP_HARB_HPRIO, 0xf);
  617. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_0, 0x400);
  618. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_1, 0x402);
  619. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_2, 0x402);
  620. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_3, 0x40e);
  621. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_4, 0x402);
  622. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_5, 0x427);
  623. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_6, 0x427);
  624. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_7, 0x4a4);
  625. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_8, 0x413);
  626. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_9, 0x417);
  627. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_10, 0x417);
  628. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_11, 0x47b);
  629. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_12, 0x47b);
  630. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_13, 0x45b);
  631. }
  632. static void _pwrap_enable(void)
  633. {
  634. #if (MTK_PLATFORM_MT6357)
  635. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x3fd35);
  636. #endif
  637. WRAP_WR32(PMIC_WRAP_WACS0_EN, 0x1);
  638. WRAP_WR32(PMIC_WRAP_WACS2_EN, 0x1);
  639. WRAP_WR32(PMIC_WRAP_WACS_P2P_EN, 0x1);
  640. WRAP_WR32(PMIC_WRAP_WACS_MD32_EN, 0x1);
  641. WRAP_WR32(PMIC_WRAP_STAUPD_CTRL, 0x5); /* 100us */
  642. WRAP_WR32(PMIC_WRAP_WDT_UNIT, 0xf);
  643. WRAP_WR32(PMIC_WRAP_WDT_SRC_EN_0, 0xffffffff);
  644. WRAP_WR32(PMIC_WRAP_WDT_SRC_EN_1, 0xffffffff);
  645. WRAP_WR32(PMIC_WRAP_TIMER_CTRL, 0x3);
  646. WRAP_WR32(PMIC_WRAP_INT0_EN, 0x00000007);
  647. WRAP_WR32(PMIC_WRAP_INT1_EN, 0xffffd800); /* disable Matching interrupt for bit 13 */
  648. }
  649. static signed int _pwrap_init_reg_clock_reset(unsigned int regck_sel)
  650. {
  651. unsigned int rdata;
  652. WRAP_WR32(PMIC_WRAP_EXT_CK_WRITE, 0x1);
  653. #ifndef DUAL_PMICS
  654. /* Set Read Dummy Cycle Number (Slave Clock is 18MHz) */
  655. WRAP_WR32(PMIC_WRAP_RDDMY, 0x8);
  656. PWRAPLOG("NO_SLV_CLK_1M Set Read Dummy Cycle\n");
  657. #else
  658. WRAP_WR32(PMIC_WRAP_RDDMY, 0x0808);
  659. PWRAPLOG("NO_SLV_CLK_1M Set Read Dummy Cycle dual_pmics\n");
  660. #endif
  661. /* Config SPI Waveform according to reg clk */
  662. if (regck_sel == 1) { /* Slave Clock is 18MHz */
  663. /* wait data written into register => 4T_PMIC:
  664. * CSHEXT_WRITE_START+EXT_CK+CSHEXT_WRITE_END+CSLEXT_START
  665. */
  666. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x0);
  667. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0x0);
  668. WRAP_WR32(PMIC_WRAP_CSLEXT_WRITE, 0x0);
  669. WRAP_WR32(PMIC_WRAP_CSLEXT_READ, 0x0200);
  670. } else { /*Safe Mode*/
  671. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x0f0f);
  672. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0x0f0f);
  673. WRAP_WR32(PMIC_WRAP_CSLEXT_WRITE, 0x0f0f);
  674. WRAP_WR32(PMIC_WRAP_CSLEXT_READ, 0x0f0f);
  675. }
  676. return 0;
  677. }
  678. static unsigned int pwrap_read_test(void)
  679. {
  680. unsigned int rdata = 0;
  681. unsigned int return_value = 0;
  682. /* Read Test */
  683. return_value = pwrap_wacs2_read(PMIC_DEW_READ_TEST_ADDR, &rdata);
  684. if (rdata != DEFAULT_VALUE_READ_TEST) {
  685. PWRAPERR("Error: r_rdata=0x%x, exp=0x5aa5,ret=0x%x\n", rdata, return_value);
  686. return E_PWR_READ_TEST_FAIL;
  687. }
  688. PWRAPLOG("Read Test pass,return_value=%d\n", return_value);
  689. return 0;
  690. }
  691. static unsigned int pwrap_write_test(void)
  692. {
  693. unsigned int rdata = 0;
  694. unsigned int sub_return = 0;
  695. unsigned int sub_return1 = 0;
  696. /* Write test using WACS2 */
  697. PWRAPLOG("start pwrap_write\n");
  698. sub_return = pwrap_wacs2_write(PMIC_DEW_WRITE_TEST_ADDR, DEFAULT_VALUE_READ_TEST);
  699. PWRAPLOG("after pwrap_write\n");
  700. sub_return1 = pwrap_wacs2_read(PMIC_DEW_WRITE_TEST_ADDR, &rdata);
  701. if ((rdata != DEFAULT_VALUE_READ_TEST) || (sub_return != 0) || (sub_return1 != 0)) {
  702. PWRAPERR("Err:,w_rdata=0x%x,exp=0xa55a,sub_return=0x%x,sub_return1=0x%x\n", rdata, sub_return,
  703. sub_return1);
  704. return E_PWR_INIT_WRITE_TEST;
  705. }
  706. PWRAPLOG("write Test pass\n");
  707. return 0;
  708. }
  709. static void pwrap_ut(unsigned int ut_test)
  710. {
  711. unsigned int sub_return = 0;
  712. switch (ut_test) {
  713. case 1:
  714. pwrap_write_test();
  715. break;
  716. case 2:
  717. pwrap_read_test();
  718. break;
  719. case 3:
  720. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  721. pwrap_wacs2_ipi(0x10010000 + 0xD8, 0xffffffff, (WRITE_CMD | WRITE_PMIC_WRAP));
  722. break;
  723. #endif
  724. case 4:
  725. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0x1234);
  726. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0x4321);
  727. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0xF0F0);
  728. break;
  729. default:
  730. PWRAPLOG("default test.\n");
  731. break;
  732. }
  733. }
  734. /*-------------------pwrap debug---------------------*/
  735. static inline void pwrap_dump_ap_register(void)
  736. {
  737. unsigned int i = 0, offset = 0;
  738. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  739. unsigned int *reg_addr;
  740. #else
  741. unsigned int reg_addr;
  742. #endif
  743. unsigned int reg_value = 0;
  744. PWRAPERR("dump reg\n");
  745. for (i = 0; i <= PMIC_WRAP_REG_RANGE; i++) {
  746. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  747. reg_addr = (unsigned int *) (PMIC_WRAP_BASE + i * 4);
  748. reg_value = WRAP_RD32(((unsigned int *) (PMIC_WRAP_BASE + i * 4)));
  749. PWRAPERR("addr:0x%p = 0x%x\n", reg_addr, reg_value);
  750. #else
  751. reg_addr = (PMIC_WRAP_BASE + i * 4);
  752. reg_value = WRAP_RD32(reg_addr);
  753. PWRAPERR("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  754. #endif
  755. }
  756. for (i = 0; i <= 14; i++) {
  757. offset = 0xc00 + i * 4;
  758. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  759. reg_addr = (unsigned int *) (PMIC_WRAP_BASE + offset);
  760. reg_value = WRAP_RD32(((unsigned int *) (PMIC_WRAP_BASE + offset)));
  761. PWRAPERR("addr:0x%p = 0x%x\n", reg_addr, reg_value);
  762. #else
  763. reg_addr = (PMIC_WRAP_BASE + offset);
  764. reg_value = WRAP_RD32(reg_addr);
  765. PWRAPERR("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  766. #endif
  767. }
  768. }
  769. void pwrap_dump_all_register(void)
  770. {
  771. pwrap_dump_ap_register();
  772. }
  773. static int is_pwrap_init_done(void)
  774. {
  775. int ret = 0;
  776. ret = WRAP_RD32(PMIC_WRAP_INIT_DONE2);
  777. PWRAPLOG("is_pwrap_init_done %d\n", ret);
  778. if ((ret & 0x1) == 1)
  779. return 0;
  780. return -1;
  781. }
  782. signed int pwrap_init_lk(void)
  783. {
  784. PWRAPFUC();
  785. if (0 == is_pwrap_init_done()) {
  786. PWRAPLOG("[PMIC_WRAP] pwrap_init already init, do nothing\n");
  787. return 0;
  788. }
  789. return 0;
  790. }
  791. static S32 _pwrap_init_sistrobe_reset(int dual_si_sample_settings)
  792. {
  793. WRAP_WR32(PMIC_WRAP_SI_SAMPLE_CTRL, si_sample_ctrl);
  794. return 0;
  795. }
  796. static signed int _pwrap_reset_pattern(void)
  797. {
  798. signed int sub_return = 0;
  799. struct pwrap_rc_info info;
  800. PWRAPLOG("pwrap_init_reset start!!!!!!!!!!!!!\n");
  801. #if 0
  802. /* Set SoC SPI IO Driving Strength to 4 mA */
  803. WRAP_WR32(IOCFG_LM_DRV_CFG1_CLR, 0x7 | (0x7 << 3));
  804. WRAP_WR32(IOCFG_LM_DRV_CFG1_SET, 0x1 | (0x1 << 3));
  805. /* Set SPI MO and MI IOs to Pull Down */
  806. WRAP_WR32(IOCFG_LM_PU_CFG0_CLR, (0x1 << 12) | (0x1 << 13));
  807. WRAP_WR32(IOCFG_LM_PD_CFG0_SET, (0x1 << 12) | (0x1 << 13));
  808. #endif
  809. /* Backup PMIC Wrap key register before reset */
  810. si_sample_ctrl = WRAP_RD32(PMIC_WRAP_SI_SAMPLE_CTRL);
  811. info.wacs2_cmd = WRAP_RD32(PMIC_WRAP_WACS2_CMD);
  812. info.dcxo_en = WRAP_RD32(PMIC_WRAP_DCXO_ENABLE);
  813. info.dcxo_conn_adr0 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_ADR0);
  814. info.dcxo_conn_wdata0 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_WDATA0);
  815. info.dcxo_conn_adr1 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_ADR1);
  816. info.dcxo_conn_wdata1 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_WDATA1);
  817. info.dcxo_nfc_adr0 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_ADR0);
  818. info.dcxo_nfc_wdata0 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_WDATA0);
  819. info.dcxo_nfc_adr1 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_ADR1);
  820. info.dcxo_nfc_wdata1 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_WDATA1);
  821. PWRAPLOG("Backup pwrap key register ok\n");
  822. __pwrap_spi_clk_set();
  823. PWRAPLOG("__pwrap_spi_clk_set ok\n");
  824. /* Enable DCM */
  825. PWRAPLOG("No need to enable DCM\n");
  826. /* Reset SPISLV */
  827. PWRAPLOG("No need to reset SPISLV\n");
  828. /* Enable WRAP */
  829. WRAP_WR32(PMIC_WRAP_WRAP_EN, 0x1);
  830. /* Enable WACS2 */
  831. WRAP_WR32(PMIC_WRAP_WACS2_EN, 0x1);
  832. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x4); /* ONLY WACS2 */
  833. PWRAPLOG("Enable WACS2 ok\n");
  834. /* SPI Waveform Configuration. 0:safe mode, 1:18MHz */
  835. sub_return = _pwrap_init_reg_clock_reset(1);
  836. if (sub_return != 0) {
  837. PWRAPERR("init_reg_clock fail, sub_return=%x\n", sub_return);
  838. return E_PWR_INIT_REG_CLOCK;
  839. }
  840. PWRAPLOG("_pwrap_init_reg_clock_reset ok\n");
  841. /* SPI Slave Configuration */
  842. PWRAPLOG("No need to init SPISLV\n");
  843. /* Enable DIO mode */
  844. sub_return = _pwrap_init_dio_reset(1);
  845. if (sub_return != 0) {
  846. PWRAPERR("init_dio fail, sub_return=%x\n", sub_return);
  847. return E_PWR_INIT_DIO;
  848. }
  849. PWRAPLOG("_pwrap_init_dio_reset ok\n");
  850. /* Input data calibration flow */
  851. sub_return = _pwrap_init_sistrobe_reset(0);
  852. if (sub_return != 0) {
  853. PWRAPERR("InitSiStrobe fail, sub_return=%x\n", sub_return);
  854. return E_PWR_INIT_SIDLY;
  855. }
  856. PWRAPLOG("_pwrap_init_sistrobe_reset ok\n");
  857. /* Status update function initialization
  858. * 1. Signature Checking using CRC (CRC 0 only)
  859. * 2. EINT update
  860. * 3. Read back Auxadc thermal data for GPS
  861. */
  862. _pwrap_InitStaUpd();
  863. PWRAPLOG("_pwrap_InitStaUpd ok\n");
  864. /* PMIC_WRAP starvation setting */
  865. _pwrap_starve_set();
  866. PWRAPLOG("_pwrap_starve_set ok\n");
  867. /* PMIC_WRAP enables */
  868. _pwrap_enable();
  869. PWRAPLOG("_pwrap_enable ok\n");
  870. /* Initialization Done */
  871. WRAP_WR32(PMIC_WRAP_INIT_DONE0, 0x1);
  872. WRAP_WR32(PMIC_WRAP_INIT_DONE2, 0x1);
  873. WRAP_WR32(PMIC_WRAP_INIT_DONE_P2P, 0x1);
  874. WRAP_WR32(PMIC_WRAP_INIT_DONE_MD32, 0x1);
  875. /* restore key register after reset */
  876. WRAP_WR32(PMIC_WRAP_WACS2_CMD, info.wacs2_cmd);
  877. WRAP_WR32(PMIC_WRAP_DCXO_CONN_ADR0, info.dcxo_conn_adr0);
  878. WRAP_WR32(PMIC_WRAP_DCXO_CONN_WDATA0, info.dcxo_conn_wdata0);
  879. WRAP_WR32(PMIC_WRAP_DCXO_CONN_ADR1, info.dcxo_conn_adr1);
  880. WRAP_WR32(PMIC_WRAP_DCXO_CONN_WDATA1, info.dcxo_conn_wdata1);
  881. WRAP_WR32(PMIC_WRAP_DCXO_NFC_ADR0, info.dcxo_nfc_adr0);
  882. WRAP_WR32(PMIC_WRAP_DCXO_NFC_WDATA0, info.dcxo_nfc_wdata0);
  883. WRAP_WR32(PMIC_WRAP_DCXO_NFC_ADR1, info.dcxo_nfc_adr1);
  884. WRAP_WR32(PMIC_WRAP_DCXO_NFC_WDATA1, info.dcxo_nfc_wdata1);
  885. WRAP_WR32(PMIC_WRAP_DCXO_ENABLE, info.dcxo_en);
  886. PWRAPLOG("Restore pwrap key register ok\n");
  887. PWRAPLOG("pwrap_init_reset Done!!!!!!!!!\n");
  888. return 0;
  889. }
  890. static signed int pwrap_reset_pattern(void)
  891. {
  892. /* SPI & WRAP Reset Pattern */
  893. unsigned int ret;
  894. ret = _pwrap_reset_pattern();
  895. if (ret != 0) {
  896. PWRAPERR("_pwrap_reset_pattern fail, ret=%x\n", ret);
  897. return E_PWR_INIT_RESET_SPI;
  898. }
  899. return 0;
  900. }
  901. void pwrap_disable(void) {
  902. PWRAPLOG("pmic wrap disable\n");
  903. WRAP_WR32(PMIC_WRAP_WRAP_EN, 0x0);
  904. udelay(10);
  905. }
  906. #endif /*endif PMIC_WRAP_NO_PMIC */