mt_pmic_wrap_init.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. #ifdef PWRAP_TIMEOUT
  186. static unsigned long long _pwrap_get_current_time(void)
  187. {
  188. return gpt4_get_current_tick();
  189. }
  190. static int _pwrap_timeout_ns(unsigned long long start_time_ns, unsigned long long timeout_time_ns)
  191. {
  192. return gpt4_timeout_tick(start_time_ns, timeout_time_ns);
  193. }
  194. static unsigned long long _pwrap_time2ns(unsigned long long time_us)
  195. {
  196. return gpt4_time2tick_us(time_us);
  197. }
  198. #else
  199. static unsigned long long _pwrap_get_current_time(void)
  200. {
  201. return 0;
  202. }
  203. static int _pwrap_timeout_ns(unsigned long long start_time_ns, unsigned long long elapse_time)
  204. {
  205. return 0;
  206. }
  207. static unsigned long long _pwrap_time2ns(unsigned long long time_us)
  208. {
  209. return 0;
  210. }
  211. #endif
  212. /* ##################################################################### */
  213. /* define macro and inline function (for do while loop) */
  214. /* ##################################################################### */
  215. typedef unsigned int(*loop_condition_fp) (unsigned int); /* define a function pointer */
  216. static inline unsigned int wait_for_fsm_idle(unsigned int x)
  217. {
  218. return GET_WACS2_FSM(x) != WACS_FSM_IDLE;
  219. }
  220. static inline unsigned int wait_for_fsm_vldclr(unsigned int x)
  221. {
  222. return GET_WACS2_FSM(x) != WACS_FSM_WFVLDCLR;
  223. }
  224. static inline unsigned int wait_for_sync(unsigned int x)
  225. {
  226. return GET_SYNC_IDLE2(x) != WACS_SYNC_IDLE;
  227. }
  228. static inline unsigned int wait_for_idle_and_sync(unsigned int x)
  229. {
  230. return (GET_WACS2_FSM(x) != WACS_FSM_IDLE) || (GET_SYNC_IDLE2(x) != WACS_SYNC_IDLE);
  231. }
  232. static inline unsigned int wait_for_wrap_idle(unsigned int x)
  233. {
  234. return (GET_WRAP_FSM(x) != 0x0) || (GET_WRAP_CH_DLE_RESTCNT(x) != 0x0);
  235. }
  236. static inline unsigned int wait_for_wrap_state_idle(unsigned int x)
  237. {
  238. return GET_WRAP_AG_DLE_RESTCNT(x) != 0;
  239. }
  240. static inline unsigned int wait_for_man_idle_and_noreq(unsigned int x)
  241. {
  242. return (GET_MAN_REQ(x) != MAN_FSM_NO_REQ) || (GET_MAN_FSM(x) != MAN_FSM_IDLE);
  243. }
  244. static inline unsigned int wait_for_man_vldclr(unsigned int x)
  245. {
  246. return GET_MAN_FSM(x) != MAN_FSM_WFVLDCLR;
  247. }
  248. static inline unsigned int wait_for_cipher_ready(unsigned int x)
  249. {
  250. return x != 3;
  251. }
  252. static inline unsigned int wait_for_stdupd_idle(unsigned int x)
  253. {
  254. return GET_STAUPD_FSM(x) != 0x0;
  255. }
  256. /**************used at _pwrap_wacs2_nochk*************************************/
  257. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  258. static inline unsigned int wait_for_state_ready_init(loop_condition_fp fp, unsigned int timeout_us,
  259. void *wacs_register, unsigned int *read_reg)
  260. #else
  261. static inline unsigned int wait_for_state_ready_init(loop_condition_fp fp, unsigned int timeout_us,
  262. volatile unsigned int *wacs_register, unsigned int *read_reg)
  263. #endif
  264. {
  265. unsigned long long start_time_ns = 0, timeout_ns = 0;
  266. unsigned int reg_rdata = 0x0;
  267. start_time_ns = _pwrap_get_current_time();
  268. timeout_ns = _pwrap_time2ns(timeout_us);
  269. do {
  270. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  271. PWRAPERR("ready_init timeout\n");
  272. pwrap_dump_ap_register();
  273. return E_PWR_WAIT_IDLE_TIMEOUT;
  274. }
  275. reg_rdata = WRAP_RD32(wacs_register);
  276. } while (fp(reg_rdata));
  277. if (read_reg)
  278. *read_reg = reg_rdata;
  279. return 0;
  280. }
  281. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  282. static inline unsigned int wait_for_state_idle(loop_condition_fp fp, unsigned int timeout_us, void *wacs_register,
  283. void *wacs_vldclr_register, unsigned int *read_reg)
  284. #else
  285. static inline unsigned int wait_for_state_idle(loop_condition_fp fp, unsigned int timeout_us,
  286. volatile unsigned int *wacs_register, volatile unsigned int *wacs_vldclr_register, unsigned int *read_reg)
  287. #endif
  288. {
  289. unsigned long long start_time_ns = 0, timeout_ns = 0;
  290. unsigned int reg_rdata;
  291. start_time_ns = _pwrap_get_current_time();
  292. timeout_ns = _pwrap_time2ns(timeout_us);
  293. do {
  294. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  295. PWRAPERR("state_idle timeout\n");
  296. pwrap_dump_ap_register();
  297. return E_PWR_WAIT_IDLE_TIMEOUT;
  298. }
  299. reg_rdata = WRAP_RD32(wacs_register);
  300. if (GET_WACS2_INIT_DONE2(reg_rdata) != WACS_INIT_DONE) {
  301. PWRAPERR("init isn't finished\n");
  302. pwrap_dump_ap_register();
  303. return E_PWR_NOT_INIT_DONE;
  304. }
  305. switch (GET_WACS2_FSM(reg_rdata)) {
  306. case WACS_FSM_WFVLDCLR:
  307. WRAP_WR32(wacs_vldclr_register, 1);
  308. PWRAPERR("WACS_FSM = VLDCLR\n");
  309. pwrap_dump_ap_register();
  310. break;
  311. case WACS_FSM_WFDLE:
  312. PWRAPERR("WACS_FSM = WFDLE\n");
  313. pwrap_dump_ap_register();
  314. break;
  315. case WACS_FSM_REQ:
  316. PWRAPERR("WACS_FSM = REQ\n");
  317. pwrap_dump_ap_register();
  318. break;
  319. default:
  320. break;
  321. }
  322. } while (fp(reg_rdata));
  323. if (read_reg)
  324. *read_reg = reg_rdata;
  325. return 0;
  326. }
  327. /**************used at pwrap_wacs2********************************************/
  328. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  329. static inline unsigned int wait_for_state_ready(loop_condition_fp fp, unsigned int timeout_us, void *wacs_register,
  330. unsigned int *read_reg)
  331. #else
  332. static inline unsigned int wait_for_state_ready(loop_condition_fp fp, unsigned int timeout_us,
  333. volatile unsigned int *wacs_register, unsigned int *read_reg)
  334. #endif
  335. {
  336. unsigned long long start_time_ns = 0, timeout_ns = 0;
  337. unsigned int reg_rdata;
  338. start_time_ns = _pwrap_get_current_time();
  339. timeout_ns = _pwrap_time2ns(timeout_us);
  340. do {
  341. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  342. PWRAPERR("state_ready timeout\n");
  343. pwrap_dump_ap_register();
  344. return E_PWR_WAIT_IDLE_TIMEOUT;
  345. }
  346. reg_rdata = WRAP_RD32(wacs_register);
  347. if (GET_WACS2_INIT_DONE2(reg_rdata) != WACS_INIT_DONE) {
  348. PWRAPERR("init isn't finished\n");
  349. pwrap_dump_ap_register();
  350. return E_PWR_NOT_INIT_DONE;
  351. }
  352. } while (fp(reg_rdata));
  353. if (read_reg)
  354. *read_reg = reg_rdata;
  355. return 0;
  356. }
  357. /******************************************************
  358. * Function : pwrap_wacs2()
  359. * Description :
  360. * Parameter :
  361. * Return :
  362. ******************************************************/
  363. signed int pwrap_wacs2(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata)
  364. {
  365. unsigned int reg_rdata = 0;
  366. unsigned int wacs_write = 0;
  367. unsigned int wacs_adr = 0;
  368. unsigned int wacs_cmd = 0;
  369. unsigned int return_value = 0;
  370. signed int pwrap_ret = 0;
  371. /* Check argument validation */
  372. if ((write & ~(0x1)) != 0)
  373. return E_PWR_INVALID_RW;
  374. if ((adr & ~(0xffff)) != 0)
  375. return E_PWR_INVALID_ADDR;
  376. if ((wdata & ~(0xffff)) != 0)
  377. return E_PWR_INVALID_WDAT;
  378. enter_critical_section();
  379. /* Check IDLE & INIT_DONE in advance */
  380. return_value =
  381. wait_for_state_idle(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA,
  382. PMIC_WRAP_WACS2_VLDCLR, 0);
  383. if (return_value != 0) {
  384. PWRAPERR("fsm_idle fail,ret=%d\n", return_value);
  385. goto FAIL;
  386. }
  387. RETRY:
  388. wacs_write = write << 31;
  389. wacs_adr = (adr >> 1) << 16;
  390. wacs_cmd = wacs_write | wacs_adr | wdata;
  391. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  392. if (write == 0) {
  393. if (rdata == NULL) {
  394. PWRAPERR("rdata NULL\n");
  395. return_value = E_PWR_INVALID_ARG;
  396. goto FAIL;
  397. }
  398. return_value =
  399. wait_for_state_ready(wait_for_fsm_vldclr, TIMEOUT_READ, PMIC_WRAP_WACS2_RDATA,
  400. &reg_rdata);
  401. if (return_value != 0) {
  402. PWRAPERR("fsm_vldclr fail,ret=%d\n", return_value);
  403. return_value += 1;
  404. goto FAIL;
  405. }
  406. *rdata = GET_WACS2_RDATA(reg_rdata);
  407. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  408. }
  409. FAIL:
  410. if (return_value != 0) {
  411. PWRAPERR("pwrap_wacs2_hal fail,ret=%d\n", return_value);
  412. pwrap_ret = pwrap_reset_pattern();
  413. if (pwrap_ret != 0) {
  414. PWRAPERR("init fail, ret=%x.\n",pwrap_ret);
  415. pwrap_dump_ap_register();
  416. }
  417. else {
  418. dprintf(CRITICAL, PWRAPTAG "retry cmd:0x%x,0x%x,0x%x\n", wacs_cmd, write, adr);
  419. goto RETRY;
  420. }
  421. }
  422. exit_critical_section();
  423. return return_value;
  424. }
  425. /*********************internal API for pwrap_init***************************/
  426. /**********************************
  427. * Function : _pwrap_wacs2_nochk()
  428. * Description :
  429. * Parameter :
  430. * Return :
  431. ***********************************/
  432. signed int pwrap_read_nochk(unsigned int adr, unsigned int *rdata)
  433. {
  434. return _pwrap_wacs2_nochk(0, adr, 0, rdata);
  435. }
  436. signed int pwrap_write_nochk(unsigned int adr, unsigned int wdata)
  437. {
  438. return _pwrap_wacs2_nochk(1, adr, wdata, 0);
  439. }
  440. static signed int _pwrap_wacs2_nochk(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata)
  441. {
  442. unsigned int reg_rdata = 0x0;
  443. unsigned int wacs_write = 0x0;
  444. unsigned int wacs_adr = 0x0;
  445. unsigned int wacs_cmd = 0x0;
  446. unsigned int return_value = 0x0;
  447. /* Check argument validation */
  448. if ((write & ~(0x1)) != 0)
  449. return E_PWR_INVALID_RW;
  450. if ((adr & ~(0xffff)) != 0)
  451. return E_PWR_INVALID_ADDR;
  452. if ((wdata & ~(0xffff)) != 0)
  453. return E_PWR_INVALID_WDAT;
  454. enter_critical_section();
  455. /* Check IDLE */
  456. return_value =
  457. wait_for_state_ready_init(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  458. if (return_value != 0) {
  459. PWRAPERR("write fail,ret=%x\n", return_value);
  460. exit_critical_section();
  461. return return_value;
  462. }
  463. wacs_write = write << 31;
  464. wacs_adr = (adr >> 1) << 16;
  465. wacs_cmd = wacs_write | wacs_adr | wdata;
  466. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  467. if (write == 0) {
  468. if (rdata == NULL) {
  469. PWRAPERR("rdata NULL\n");
  470. return_value = E_PWR_INVALID_ARG;
  471. exit_critical_section();
  472. return return_value;
  473. }
  474. return_value =
  475. wait_for_state_ready_init(wait_for_fsm_vldclr, TIMEOUT_READ,
  476. PMIC_WRAP_WACS2_RDATA, &reg_rdata);
  477. if (return_value != 0) {
  478. PWRAPERR("fsm_vldclr fail,ret=%d\n", return_value);
  479. return_value += 1;
  480. exit_critical_section();
  481. return return_value;
  482. }
  483. *rdata = GET_WACS2_RDATA(reg_rdata);
  484. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  485. }
  486. exit_critical_section();
  487. return 0;
  488. }
  489. static void __pwrap_soft_reset(void)
  490. {
  491. PWRAPLOG("start reset wrapper\n");
  492. WRAP_WR32(INFRA_GLOBALCON_RST2_SET, 0x1);
  493. WRAP_WR32(INFRA_GLOBALCON_RST2_CLR, 0x1);
  494. }
  495. static void __pwrap_spi_clk_set(void)
  496. {
  497. PWRAPLOG("pwrap_spictl reset ok\n");
  498. #if !defined(CONFIG_FPGA_EARLY_PORTING)
  499. WRAP_WR32(CLK_CFG_5_CLR, 0x00000093);
  500. WRAP_WR32(CLK_CFG_5_SET, CLK_SPI_CK_26M);
  501. #endif
  502. /*sys_ck cg enable, turn off clock*/
  503. WRAP_WR32(MODULE_SW_CG_0_SET, 0x0000000F);
  504. /* turn off clock*/
  505. WRAP_WR32(MODULE_SW_CG_2_SET, 0x00000100);
  506. /* toggle PMIC_WRAP and pwrap_spictl reset */
  507. __pwrap_soft_reset();
  508. /*sys_ck cg enable, turn on clock*/
  509. WRAP_WR32(MODULE_SW_CG_0_CLR, 0x0000000F);
  510. /* turn on clock*/
  511. WRAP_WR32(MODULE_SW_CG_2_CLR, 0x00000100);
  512. PWRAPLOG("spi clk set ....\n");
  513. }
  514. static void _pwrap_InitStaUpd(void)
  515. {
  516. #ifndef DUAL_PMICS
  517. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, 0xf4);
  518. #else
  519. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, 0xfc);
  520. #endif
  521. #if 0
  522. /* CRC */
  523. #ifdef DUAL_PMICS
  524. pwrap_write_nochk(PMIC_DEW_CRC_EN_ADDR, 0x1);
  525. WRAP_WR32(PMIC_WRAP_CRC_EN, 0x1);
  526. WRAP_WR32(PMIC_WRAP_SIG_ADR, DEW_CRC_VAL);
  527. #else
  528. pwrap_write_nochk(PMIC_DEW_CRC_EN_ADDR, 0x1);
  529. pwrap_write_nochk(EXT_DEW_CRC_EN, 0x1);
  530. WRAP_WR32(PMIC_WRAP_CRC_EN, 0x1);
  531. WRAP_WR32(PMIC_WRAP_SIG_ADR, (EXT_DEW_CRC_VAL << 16 | DEW_CRC_VAL));
  532. #endif
  533. #endif
  534. /* Signature */
  535. #ifndef DUAL_PMICS
  536. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x1);
  537. WRAP_WR32(PMIC_WRAP_SIG_ADR, PMIC_DEW_CRC_VAL_ADDR);
  538. WRAP_WR32(PMIC_WRAP_SIG_VALUE, 0x83);
  539. #else
  540. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x3);
  541. WRAP_WR32(PMIC_WRAP_SIG_ADR, (EXT_DEW_CRC_VAL << 16) | DEW_CRC_VAL);
  542. WRAP_WR32(PMIC_WRAP_SIG_VALUE, (0x83 << 16) | 0x83);
  543. #endif
  544. WRAP_WR32(PMIC_WRAP_EINT_STA0_ADR, PMIC_CPU_INT_STA_ADDR);
  545. #ifdef DUAL_PMICS
  546. WRAP_WR32(PMIC_WRAP_EINT_STA1_ADR, EXT_INT_STA);
  547. #endif
  548. /* MD ADC Interface */
  549. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_LATEST_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  550. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_WP_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  551. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_0_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  552. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_1_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  553. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_2_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  554. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_3_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  555. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_4_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  556. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_5_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  557. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_6_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  558. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_7_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  559. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_8_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  560. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_9_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  561. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_10_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  562. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_11_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  563. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_12_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  564. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_13_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  565. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_14_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  566. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_15_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  567. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_16_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  568. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_17_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  569. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_18_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  570. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_19_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  571. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_20_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  572. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_21_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  573. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_22_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  574. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_23_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  575. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_24_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  576. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_25_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  577. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_26_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  578. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_27_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  579. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_28_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  580. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_29_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  581. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_30_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  582. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_31_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  583. WRAP_WR32(PMIC_WRAP_INT_GPS_AUXADC_CMD_ADDR, (PMIC_AUXADC_RQST_DCXO_BY_GPS_ADDR << 16) + PMIC_AUXADC_RQST_CH7_ADDR);
  584. WRAP_WR32(PMIC_WRAP_INT_GPS_AUXADC_CMD, (0x0040 << 16) + 0x0080);
  585. 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);
  586. WRAP_WR32(PMIC_WRAP_EXT_GPS_AUXADC_RDATA_ADDR, PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  587. }
  588. static void _pwrap_starve_set(void)
  589. {
  590. WRAP_WR32(PMIC_WRAP_HARB_HPRIO, 0xf);
  591. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_0, 0x402);
  592. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_1, 0x403);
  593. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_2, 0x403);
  594. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_3, 0x403);
  595. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_4, 0x40f);
  596. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_5, 0x420);
  597. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_6, 0x428);
  598. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_7, 0x428);
  599. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_8, 0x413);
  600. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_9, 0x417);
  601. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_10, 0x417);
  602. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_11, 0x47c);
  603. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_12, 0x47c);
  604. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_13, 0x740);
  605. }
  606. static void _pwrap_enable(void)
  607. {
  608. #if (MTK_PLATFORM_MT6358)
  609. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x3fa75);
  610. #endif
  611. WRAP_WR32(PMIC_WRAP_WACS0_EN, 0x1);
  612. WRAP_WR32(PMIC_WRAP_WACS2_EN, 0x1);
  613. WRAP_WR32(PMIC_WRAP_WACS_P2P_EN, 0x1);
  614. WRAP_WR32(PMIC_WRAP_WACS_MD32_EN, 0x1);
  615. WRAP_WR32(PMIC_WRAP_STAUPD_CTRL, 0x5); /* 100us */
  616. WRAP_WR32(PMIC_WRAP_WDT_UNIT, 0xf);
  617. WRAP_WR32(PMIC_WRAP_WDT_SRC_EN_0, 0xffffffff);
  618. WRAP_WR32(PMIC_WRAP_WDT_SRC_EN_1, 0xffffffff);
  619. WRAP_WR32(PMIC_WRAP_TIMER_EN, 0x1);
  620. WRAP_WR32(PMIC_WRAP_INT0_EN, 0xffffffff);
  621. WRAP_WR32(PMIC_WRAP_INT1_EN, 0xffffffff);
  622. }
  623. static unsigned int pwrap_read_test(void)
  624. {
  625. unsigned int rdata = 0;
  626. unsigned int return_value = 0;
  627. /* Read Test */
  628. return_value = pwrap_wacs2_read(PMIC_DEW_READ_TEST_ADDR, &rdata);
  629. if (rdata != DEFAULT_VALUE_READ_TEST) {
  630. PWRAPERR("Error: r_rdata=0x%x, exp=0x5aa5,ret=0x%x\n", rdata, return_value);
  631. return E_PWR_READ_TEST_FAIL;
  632. }
  633. PWRAPLOG("Read Test pass,return_value=%d\n", return_value);
  634. return 0;
  635. }
  636. static unsigned int pwrap_write_test(void)
  637. {
  638. unsigned int rdata = 0;
  639. unsigned int sub_return = 0;
  640. unsigned int sub_return1 = 0;
  641. /* Write test using WACS2 */
  642. PWRAPLOG("start pwrap_write\n");
  643. sub_return = pwrap_wacs2_write(PMIC_DEW_WRITE_TEST_ADDR, DEFAULT_VALUE_READ_TEST);
  644. PWRAPLOG("after pwrap_write\n");
  645. sub_return1 = pwrap_wacs2_read(PMIC_DEW_WRITE_TEST_ADDR, &rdata);
  646. if ((rdata != DEFAULT_VALUE_READ_TEST) || (sub_return != 0) || (sub_return1 != 0)) {
  647. PWRAPERR("Err:,w_rdata=0x%x,exp=0xa55a,sub_return=0x%x,sub_return1=0x%x\n", rdata, sub_return,
  648. sub_return1);
  649. return E_PWR_INIT_WRITE_TEST;
  650. }
  651. PWRAPLOG("write Test pass\n");
  652. return 0;
  653. }
  654. static void pwrap_ut(unsigned int ut_test)
  655. {
  656. unsigned int sub_return = 0;
  657. switch (ut_test) {
  658. case 1:
  659. pwrap_write_test();
  660. break;
  661. case 2:
  662. pwrap_read_test();
  663. break;
  664. case 3:
  665. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  666. pwrap_wacs2_ipi(0x10010000 + 0xD8, 0xffffffff, (WRITE_CMD | WRITE_PMIC_WRAP));
  667. break;
  668. #endif
  669. case 4:
  670. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0x1234);
  671. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0x4321);
  672. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0xF0F0);
  673. if (sub_return != 0) {
  674. PWRAPERR("w_t_fail, sub_return=%x\n", sub_return);
  675. }
  676. default:
  677. PWRAPLOG("default test.\n");
  678. break;
  679. }
  680. }
  681. /*-------------------pwrap debug---------------------*/
  682. static inline void pwrap_dump_ap_register(void)
  683. {
  684. unsigned int i = 0, offset = 0;
  685. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  686. unsigned int *reg_addr;
  687. #else
  688. unsigned int reg_addr;
  689. #endif
  690. unsigned int reg_value = 0;
  691. PWRAPERR("dump reg\n");
  692. for (i = 0; i <= PMIC_WRAP_REG_RANGE; i++) {
  693. reg_addr = (PMIC_WRAP_BASE + i * 4);
  694. #if (PMIC_WRAP_KERNEL)
  695. reg_value = WRAP_RD32(((unsigned int *) (PMIC_WRAP_BASE + i * 4)));
  696. #else
  697. reg_value = WRAP_RD32(reg_addr);
  698. #endif
  699. PWRAPERR("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  700. }
  701. for (i = 0; i <= 14; i++) {
  702. offset = 0xc00 + i * 4;
  703. reg_addr = (PMIC_WRAP_BASE + offset);
  704. #if (PMIC_WRAP_KERNEL)
  705. reg_value = WRAP_RD32(((unsigned int *) (PMIC_WRAP_BASE + offset)));
  706. #else
  707. reg_value = WRAP_RD32(reg_addr);
  708. #endif
  709. PWRAPERR("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  710. }
  711. PWRAPERR("CG_0_STA:0x%x, CG_2_STA:0x%x, PMICW_CLOCK_CTRL:0x%x\n", WRAP_RD32(0x10001090), WRAP_RD32(0x100010ac), WRAP_RD32(0x10001108));
  712. PWRAPERR("INFRA_MISC2:0x%x, CFG_5:0x%x, SCP_CFG_1:0x%x\n", WRAP_RD32(0x10001f0c), WRAP_RD32(0x10000090), WRAP_RD32(0x10000204));
  713. }
  714. void pwrap_dump_all_register(void)
  715. {
  716. pwrap_dump_ap_register();
  717. }
  718. static int is_pwrap_init_done(void)
  719. {
  720. int ret = 0;
  721. ret = WRAP_RD32(PMIC_WRAP_INIT_DONE2);
  722. PWRAPLOG("is_pwrap_init_done %d\n", ret);
  723. if ((ret & 0x1) == 1)
  724. return 0;
  725. return -1;
  726. }
  727. signed int pwrap_init_lk(void)
  728. {
  729. PWRAPFUC();
  730. if (0 == is_pwrap_init_done()) {
  731. PWRAPLOG("[PMIC_WRAP]wrap_init already init, do nothing\n");
  732. return 0;
  733. }
  734. return 0;
  735. }
  736. static signed int _pwrap_init_reg_clock_reset(unsigned int regck_sel)
  737. {
  738. WRAP_WR32(PMIC_WRAP_EXT_CK_WRITE, 0x1);
  739. WRAP_WR32(PMIC_WRAP_RDDMY, 0x8);
  740. /* Config SPI Waveform according to reg clk */
  741. if (regck_sel == 1) { /* Slave Clock is 18MHz */
  742. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x0);
  743. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0x0);
  744. WRAP_WR32(PMIC_WRAP_CSLEXT_WRITE, 0x0);
  745. WRAP_WR32(PMIC_WRAP_CSLEXT_READ, 0x0);
  746. } else { /*Safe Mode*/
  747. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x0f0f);
  748. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0x0f0f);
  749. WRAP_WR32(PMIC_WRAP_CSLEXT_WRITE, 0x0f0f);
  750. WRAP_WR32(PMIC_WRAP_CSLEXT_READ, 0x0f0f);
  751. }
  752. return 0;
  753. }
  754. static signed int _pwrap_init_dio_reset(unsigned int dio_en)
  755. {
  756. #ifndef DUAL_PMICS
  757. WRAP_WR32(PMIC_WRAP_DIO_EN, 0x1);
  758. #else
  759. WRAP_WR32(PMIC_WRAP_DIO_EN, 0x3);
  760. #endif
  761. return 0;
  762. }
  763. static S32 _pwrap_init_sistrobe_reset(int dual_si_sample_settings)
  764. {
  765. WRAP_WR32(PMIC_WRAP_SI_SAMPLE_CTRL, si_sample_ctrl);
  766. return 0;
  767. }
  768. static signed int _pwrap_reset_pattern(void)
  769. {
  770. signed int sub_return = 0;
  771. struct pwrap_rc_info info;
  772. /* backup key register before reset */
  773. si_sample_ctrl = WRAP_RD32(PMIC_WRAP_SI_SAMPLE_CTRL);
  774. info.wacs2_cmd = WRAP_RD32(PMIC_WRAP_WACS2_CMD);
  775. info.dcxo_en = WRAP_RD32(PMIC_WRAP_DCXO_ENABLE);
  776. info.dcxo_conn_adr0 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_ADR0);
  777. info.dcxo_conn_wdata0 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_WDATA0);
  778. info.dcxo_conn_adr1 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_ADR1);
  779. info.dcxo_conn_wdata1 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_WDATA1);
  780. info.dcxo_nfc_adr0 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_ADR0);
  781. info.dcxo_nfc_wdata0 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_WDATA0);
  782. info.dcxo_nfc_adr1 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_ADR1);
  783. info.dcxo_nfc_wdata1 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_WDATA1);
  784. __pwrap_spi_clk_set();
  785. /* Enable WRAP */
  786. WRAP_WR32(PMIC_WRAP_WRAP_EN, 0x1);
  787. WRAP_WR32(PMIC_WRAP_WACS2_EN, 0x1);
  788. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x4); /* ONLY WACS2 */
  789. /* SPI Waveform Configuration. 0:safe mode, 1:18MHz */
  790. sub_return = _pwrap_init_reg_clock_reset(1);
  791. if (sub_return != 0) {
  792. PWRAPERR("init_reg_clock fail,sub_return=%x\n", sub_return);
  793. return E_PWR_INIT_REG_CLOCK;
  794. }
  795. /* Enable DIO mode */
  796. sub_return = _pwrap_init_dio_reset(1);
  797. if (sub_return != 0) {
  798. PWRAPERR("init_dio fail, sub_return=%x\n", sub_return);
  799. return E_PWR_INIT_DIO;
  800. }
  801. /* Input data calibration flow; */
  802. sub_return = _pwrap_init_sistrobe_reset(1);
  803. if (sub_return != 0) {
  804. PWRAPERR("InitSiStrobe fail,sub_return=%x\n", sub_return);
  805. return E_PWR_INIT_SIDLY;
  806. }
  807. /* Status update function initialization
  808. * 1. Signature Checking using CRC (CRC 0 only)
  809. * 2. EINT update
  810. * 3. Read back Auxadc thermal data for GPS
  811. */
  812. _pwrap_InitStaUpd();
  813. /* PMIC WRAP priority adjust */
  814. WRAP_WR32(PMIC_WRAP_PRIORITY_USER_SEL_2, 0x0b09080a);
  815. WRAP_WR32(PMIC_WRAP_ARBITER_OUT_SEL_2, 0x0b080a09);
  816. /* PMIC_WRAP starvation setting */
  817. _pwrap_starve_set();
  818. /* PMIC_WRAP enables */
  819. _pwrap_enable();
  820. /* Initialization Done */
  821. WRAP_WR32(PMIC_WRAP_INIT_DONE0, 0x1);
  822. WRAP_WR32(PMIC_WRAP_INIT_DONE2, 0x1);
  823. WRAP_WR32(PMIC_WRAP_INIT_DONE_P2P, 0x1);
  824. WRAP_WR32(PMIC_WRAP_INIT_DONE_MD32, 0x1);
  825. /* restore key register after reset */
  826. WRAP_WR32(PMIC_WRAP_WACS2_CMD, info.wacs2_cmd);
  827. WRAP_WR32(PMIC_WRAP_DCXO_CONN_ADR0, info.dcxo_conn_adr0);
  828. WRAP_WR32(PMIC_WRAP_DCXO_CONN_WDATA0, info.dcxo_conn_wdata0);
  829. WRAP_WR32(PMIC_WRAP_DCXO_CONN_ADR1, info.dcxo_conn_adr1);
  830. WRAP_WR32(PMIC_WRAP_DCXO_CONN_WDATA1, info.dcxo_conn_wdata1);
  831. WRAP_WR32(PMIC_WRAP_DCXO_NFC_ADR0, info.dcxo_nfc_adr0);
  832. WRAP_WR32(PMIC_WRAP_DCXO_NFC_WDATA0, info.dcxo_nfc_wdata0);
  833. WRAP_WR32(PMIC_WRAP_DCXO_NFC_ADR1, info.dcxo_nfc_adr1);
  834. WRAP_WR32(PMIC_WRAP_DCXO_NFC_WDATA1, info.dcxo_nfc_wdata1);
  835. WRAP_WR32(PMIC_WRAP_DCXO_ENABLE, info.dcxo_en);
  836. PWRAPLOG("pwrap_init_reset Done!!!!!!!!!\n");
  837. return 0;
  838. }
  839. static signed int pwrap_reset_pattern(void)
  840. {
  841. /* SPI & WRAP Reset Pattern */
  842. unsigned int ret;
  843. ret = _pwrap_reset_pattern();
  844. if (ret != 0) {
  845. PWRAPERR("pwrap_reset_pattern fail, ret=%x\n", ret);
  846. return E_PWR_INIT_RESET_SPI;
  847. }
  848. return 0;
  849. }
  850. void pwrap_disable(void) {
  851. PWRAPLOG("pmic wrap disable\n");
  852. WRAP_WR32(PMIC_WRAP_WRAP_EN, 0x0);
  853. udelay(10);
  854. }
  855. #endif /*endif PMIC_WRAP_NO_PMIC */