mt_pmic_wrap_init.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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 void _pwrap_enable(void);
  129. static void _pwrap_starve_set(void);
  130. static signed int _pwrap_wacs2_nochk(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata);
  131. static signed int pwrap_wacs2_hal(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata);
  132. /*********************test API************************************************/
  133. static inline void pwrap_dump_ap_register(void);
  134. static unsigned int pwrap_write_test(void);
  135. static unsigned int pwrap_read_test(void);
  136. static signed int pwrap_reset_pattern(void);
  137. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata);
  138. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata);
  139. static unsigned int si_sample_ctrl = 0;
  140. static struct pwrap_rc_info {
  141. unsigned int wacs2_cmd;
  142. unsigned int dcxo_en;
  143. unsigned int dcxo_conn_adr0;
  144. unsigned int dcxo_conn_wdata0;
  145. unsigned int dcxo_conn_adr1;
  146. unsigned int dcxo_conn_wdata1;
  147. unsigned int dcxo_nfc_adr0;
  148. unsigned int dcxo_nfc_wdata0;
  149. unsigned int dcxo_nfc_adr1;
  150. unsigned int dcxo_nfc_wdata1;
  151. };
  152. /************* end--internal API**********************************************/
  153. /*********************** external API for pmic_wrap user ************************/
  154. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata)
  155. {
  156. pwrap_wacs2(0, adr, 0, rdata);
  157. return 0;
  158. }
  159. /* Provide PMIC write API */
  160. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata)
  161. {
  162. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  163. unsigned int flag;
  164. flag = WRITE_CMD | (1 << WRITE_PMIC);
  165. pwrap_wacs2_ipi(adr, wdata, flag);
  166. #else
  167. pwrap_wacs2(1, adr, wdata, 0);
  168. #endif
  169. return 0;
  170. }
  171. signed int pwrap_read(unsigned int adr, unsigned int *rdata)
  172. {
  173. return pwrap_wacs2(0,adr,0,rdata);
  174. }
  175. signed int pwrap_write(unsigned int adr, unsigned int wdata)
  176. {
  177. return pwrap_wacs2(1,adr,wdata,0);
  178. }
  179. /******************************************************************************
  180. *wrapper timeout
  181. *****************************************************************************/
  182. /*use the same API name with kernel driver
  183. *however,the timeout API in uboot use tick instead of ns
  184. */
  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. /* Check IDLE & INIT_DONE in advance */
  379. return_value =
  380. wait_for_state_idle(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA,
  381. PMIC_WRAP_WACS2_VLDCLR, 0);
  382. if (return_value != 0) {
  383. PWRAPERR("fsm_idle fail,ret=%d\n", return_value);
  384. goto FAIL;
  385. }
  386. RETRY:
  387. wacs_write = write << 31;
  388. wacs_adr = (adr >> 1) << 16;
  389. wacs_cmd = wacs_write | wacs_adr | wdata;
  390. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  391. if (write == 0) {
  392. if (rdata == NULL) {
  393. PWRAPERR("rdata NULL\n");
  394. return_value = E_PWR_INVALID_ARG;
  395. goto FAIL;
  396. }
  397. return_value =
  398. wait_for_state_ready(wait_for_fsm_vldclr, TIMEOUT_READ, PMIC_WRAP_WACS2_RDATA,
  399. &reg_rdata);
  400. if (return_value != 0) {
  401. PWRAPERR("fsm_vldclr fail,ret=%d\n", return_value);
  402. return_value += 1;
  403. goto FAIL;
  404. }
  405. *rdata = GET_WACS2_RDATA(reg_rdata);
  406. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  407. }
  408. FAIL:
  409. if (return_value != 0) {
  410. PWRAPERR("pwrap_wacs2 fail, ret=%d\n", return_value);
  411. pwrap_ret = pwrap_reset_pattern();
  412. if (pwrap_ret != 0) {
  413. PWRAPERR("pwrap_reset_pattern fail, ret=%x\n", pwrap_ret);
  414. pwrap_dump_ap_register();
  415. }
  416. else {
  417. dprintf(CRITICAL, PWRAPTAG "retry cmd: 0x%x, 0x%x, 0x%x\n", wacs_cmd, write, adr);
  418. goto RETRY;
  419. }
  420. }
  421. return return_value;
  422. }
  423. /*********************internal API for pwrap_init***************************/
  424. /**********************************
  425. * Function : _pwrap_wacs2_nochk()
  426. * Description :
  427. * Parameter :
  428. * Return :
  429. ***********************************/
  430. signed int pwrap_read_nochk(unsigned int adr, unsigned int *rdata)
  431. {
  432. return _pwrap_wacs2_nochk(0, adr, 0, rdata);
  433. }
  434. signed int pwrap_write_nochk(unsigned int adr, unsigned int wdata)
  435. {
  436. return _pwrap_wacs2_nochk(1, adr, wdata, 0);
  437. }
  438. static signed int _pwrap_wacs2_nochk(unsigned int write, unsigned int adr, unsigned int wdata, unsigned int *rdata)
  439. {
  440. unsigned int reg_rdata = 0x0;
  441. unsigned int wacs_write = 0x0;
  442. unsigned int wacs_adr = 0x0;
  443. unsigned int wacs_cmd = 0x0;
  444. unsigned int return_value = 0x0;
  445. /* Check argument validation */
  446. if ((write & ~(0x1)) != 0)
  447. return E_PWR_INVALID_RW;
  448. if ((adr & ~(0xffff)) != 0)
  449. return E_PWR_INVALID_ADDR;
  450. if ((wdata & ~(0xffff)) != 0)
  451. return E_PWR_INVALID_WDAT;
  452. /* Check IDLE */
  453. return_value =
  454. wait_for_state_ready_init(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  455. if (return_value != 0) {
  456. PWRAPERR("write fail,ret=%x\n", return_value);
  457. return return_value;
  458. }
  459. wacs_write = write << 31;
  460. wacs_adr = (adr >> 1) << 16;
  461. wacs_cmd = wacs_write | wacs_adr | wdata;
  462. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  463. if (write == 0) {
  464. if (rdata == NULL) {
  465. PWRAPERR("rdata NULL\n");
  466. return_value = E_PWR_INVALID_ARG;
  467. return return_value;
  468. }
  469. return_value =
  470. wait_for_state_ready_init(wait_for_fsm_vldclr, TIMEOUT_READ,
  471. PMIC_WRAP_WACS2_RDATA, &reg_rdata);
  472. if (return_value != 0) {
  473. PWRAPERR("fsm_vldclr fail,ret=%d\n", return_value);
  474. return_value += 1;
  475. return return_value;
  476. }
  477. *rdata = GET_WACS2_RDATA(reg_rdata);
  478. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  479. }
  480. return 0;
  481. }
  482. static void __pwrap_soft_reset(void)
  483. {
  484. PWRAPLOG("start reset wrapper\n");
  485. WRAP_WR32(INFRA_GLOBALCON_RST2_SET, 0x1);
  486. WRAP_WR32(INFRA_GLOBALCON_RST2_CLR, 0x1);
  487. }
  488. static void __pwrap_spi_clk_set(void)
  489. {
  490. PWRAPLOG("pwrap_spictl reset ok\n");
  491. /* sys_ck cg enable, turn off clock */
  492. WRAP_WR32(MODULE_SW_CG_0_SET, 0x0000000f);
  493. /* turn off clock */
  494. WRAP_WR32(MODULE_SW_CG_2_SET, 0x00000100);
  495. /* Select ULPOSC/16 Clock as SYS, TMR and SPI Clocks in SODI-3.0 and Suspend Modes */
  496. #if !defined(CONFIG_MTK_FPGA)
  497. WRAP_WR32(CLK_CFG_7_CLR, 0x00000097);
  498. WRAP_WR32(CLK_CFG_7_SET, 0x00000003);
  499. WRAP_WR32(CLK_CFG_UPDATE, (0x1 << 28));
  500. #endif
  501. /* Disable Clock Source Control By SPM */
  502. PWRAPLOG("=====PMICW_CLOCK_CTRL===== (Write before): %x\n", WRAP_RD32(PMICW_CLOCK_CTRL));
  503. WRAP_WR32(PMICW_CLOCK_CTRL, (WRAP_RD32(PMICW_CLOCK_CTRL) & ~(0x1 << 2)));
  504. PWRAPLOG("=====PMICW_CLOCK_CTRL===== (Write after): %x\n", WRAP_RD32(PMICW_CLOCK_CTRL));
  505. /* toggle PMIC_WRAP and pwrap_spictl reset */
  506. __pwrap_soft_reset();
  507. /*sys_ck cg enable, turn on clock*/
  508. WRAP_WR32(MODULE_SW_CG_0_CLR, 0x0000000f);
  509. /* turn on clock*/
  510. WRAP_WR32(MODULE_SW_CG_2_CLR, 0x00000100);
  511. PWRAPLOG("spi clk set ....\n");
  512. }
  513. static signed int _pwrap_init_dio_reset(unsigned int dio_en)
  514. {
  515. unsigned int rdata = 0x0;
  516. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x4); /* ONLY WACS2 */
  517. /* wait for WRAP_FSM idle */
  518. do {
  519. rdata = WRAP_RD32(PMIC_WRAP_WRAP_STA);
  520. } while((GET_WRAP_FSM(rdata) != 0x0) || (GET_WRAP_CH_DLE_RESTCNT(rdata) != 0x0));
  521. do {
  522. rdata = WRAP_RD32(PMIC_WRAP_WACS2_RDATA);
  523. } while ((GET_WACS2_FSM(rdata) != WACS_FSM_IDLE) || (GET_SYNC_IDLE2(rdata) != WACS_SYNC_IDLE));
  524. #ifndef DUAL_PMICS
  525. WRAP_WR32(PMIC_WRAP_DIO_EN, dio_en);
  526. #else
  527. WRAP_WR32(PMIC_WRAP_DIO_EN, 0x2 | dio_en);
  528. #endif
  529. return 0;
  530. }
  531. static void _pwrap_InitStaUpd(void)
  532. {
  533. #ifndef DUAL_PMICS
  534. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, 0xf5);
  535. #else
  536. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, 0xff);
  537. #endif
  538. #ifdef PMIC_WRAP_CRC_SUPPORT
  539. /* CRC */
  540. #ifndef DUAL_PMICS
  541. pwrap_write_nochk(PMIC_DEW_CRC_EN_ADDR, 0x1);
  542. WRAP_WR32(PMIC_WRAP_CRC_EN, 0x1);
  543. WRAP_WR32(PMIC_WRAP_SIG_ADR, PMIC_DEW_CRC_VAL_ADDR);
  544. #else
  545. pwrap_write_nochk(PMIC_DEW_CRC_EN_ADDR, 0x1);
  546. pwrap_write_nochk(EXT_DEW_CRC_EN, 0x1);
  547. WRAP_WR32(PMIC_WRAP_CRC_EN, 0x1);
  548. WRAP_WR32(PMIC_WRAP_SIG_ADR, (PMIC_EXT_DEW_CRC_VAL_ADDR << 16 | PMIC_DEW_CRC_VAL_ADDR));
  549. #endif
  550. #else
  551. /* Signature */
  552. #ifndef DUAL_PMICS
  553. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x1);
  554. WRAP_WR32(PMIC_WRAP_SIG_ADR, PMIC_DEW_CRC_VAL_ADDR);
  555. WRAP_WR32(PMIC_WRAP_SIG_VALUE, 0x83);
  556. #else
  557. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x3);
  558. WRAP_WR32(PMIC_WRAP_SIG_ADR, (PMIC_EXT_DEW_CRC_VAL_ADDR << 16) | PMIC_DEW_CRC_VAL_ADDR);
  559. WRAP_WR32(PMIC_WRAP_SIG_VALUE, (0x83 << 16) | 0x83);
  560. #endif
  561. #endif /* end of crc */
  562. WRAP_WR32(PMIC_WRAP_EINT_STA0_ADR, PMIC_CPU_INT_STA_ADDR);
  563. #ifdef DUAL_PMICS
  564. WRAP_WR32(PMIC_WRAP_EINT_STA1_ADR, EXT_INT_STA);
  565. #endif
  566. /* MD ADC Interface */
  567. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_LATEST_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  568. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_WP_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  569. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_0_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  570. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_1_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  571. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_2_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  572. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_3_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  573. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_4_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  574. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_5_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  575. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_6_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  576. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_7_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  577. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_8_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  578. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_9_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  579. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_10_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  580. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_11_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  581. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_12_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  582. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_13_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  583. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_14_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  584. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_15_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  585. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_16_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  586. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_17_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  587. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_18_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  588. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_19_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  589. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_20_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  590. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_21_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  591. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_22_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  592. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_23_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  593. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_24_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  594. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_25_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  595. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_26_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  596. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_27_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  597. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_28_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  598. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_29_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  599. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_30_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  600. WRAP_WR32(PMIC_WRAP_MD_AUXADC_RDATA_31_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_MDRT_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  601. WRAP_WR32(PMIC_WRAP_INT_GPS_AUXADC_CMD_ADDR, (PMIC_AUXADC_RQST_DCXO_BY_GPS_ADDR << 16) + PMIC_AUXADC_RQST_CH7_ADDR);
  602. WRAP_WR32(PMIC_WRAP_INT_GPS_AUXADC_CMD, (0x0400 << 16) + 0x0080);
  603. 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);
  604. WRAP_WR32(PMIC_WRAP_EXT_GPS_AUXADC_RDATA_ADDR, PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  605. }
  606. static void _pwrap_starve_set(void)
  607. {
  608. WRAP_WR32(PMIC_WRAP_HARB_HPRIO, 0xf);
  609. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_0, 0x400);
  610. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_1, 0x402);
  611. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_2, 0x402);
  612. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_3, 0x40e);
  613. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_4, 0x402);
  614. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_5, 0x427);
  615. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_6, 0x427);
  616. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_7, 0x4a4);
  617. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_8, 0x413);
  618. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_9, 0x417);
  619. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_10, 0x417);
  620. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_11, 0x47b);
  621. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_12, 0x47b);
  622. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_13, 0x45b);
  623. }
  624. static void _pwrap_enable(void)
  625. {
  626. #if (MTK_PLATFORM_MT6357)
  627. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x3fd35);
  628. #endif
  629. WRAP_WR32(PMIC_WRAP_WACS0_EN, 0x1);
  630. WRAP_WR32(PMIC_WRAP_WACS2_EN, 0x1);
  631. WRAP_WR32(PMIC_WRAP_WACS_P2P_EN, 0x1);
  632. WRAP_WR32(PMIC_WRAP_WACS_MD32_EN, 0x1);
  633. WRAP_WR32(PMIC_WRAP_STAUPD_CTRL, 0x5); /* 100us */
  634. WRAP_WR32(PMIC_WRAP_WDT_UNIT, 0xf);
  635. WRAP_WR32(PMIC_WRAP_WDT_SRC_EN_0, 0xffffffff);
  636. WRAP_WR32(PMIC_WRAP_WDT_SRC_EN_1, 0xffffffff);
  637. WRAP_WR32(PMIC_WRAP_TIMER_CTRL, 0x3);
  638. WRAP_WR32(PMIC_WRAP_INT0_EN, 0x00000007);
  639. WRAP_WR32(PMIC_WRAP_INT1_EN, 0xffffd800); /* disable Matching interrupt for bit 13 */
  640. }
  641. static signed int _pwrap_init_reg_clock_reset(unsigned int regck_sel)
  642. {
  643. unsigned int rdata;
  644. WRAP_WR32(PMIC_WRAP_EXT_CK_WRITE, 0x1);
  645. #ifndef DUAL_PMICS
  646. /* Set Read Dummy Cycle Number (Slave Clock is 18MHz) */
  647. WRAP_WR32(PMIC_WRAP_RDDMY, 0x8);
  648. PWRAPLOG("NO_SLV_CLK_1M Set Read Dummy Cycle\n");
  649. #else
  650. WRAP_WR32(PMIC_WRAP_RDDMY, 0x0808);
  651. PWRAPLOG("NO_SLV_CLK_1M Set Read Dummy Cycle dual_pmics\n");
  652. #endif
  653. /* Config SPI Waveform according to reg clk */
  654. if (regck_sel == 1) { /* Slave Clock is 18MHz */
  655. /* wait data written into register => 4T_PMIC:
  656. * CSHEXT_WRITE_START+EXT_CK+CSHEXT_WRITE_END+CSLEXT_START
  657. */
  658. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x0);
  659. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0x0);
  660. WRAP_WR32(PMIC_WRAP_CSLEXT_WRITE, 0x0);
  661. WRAP_WR32(PMIC_WRAP_CSLEXT_READ, 0x0200);
  662. } else { /*Safe Mode*/
  663. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x0f0f);
  664. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0x0f0f);
  665. WRAP_WR32(PMIC_WRAP_CSLEXT_WRITE, 0x0f0f);
  666. WRAP_WR32(PMIC_WRAP_CSLEXT_READ, 0x0f0f);
  667. }
  668. return 0;
  669. }
  670. static unsigned int pwrap_read_test(void)
  671. {
  672. unsigned int rdata = 0;
  673. unsigned int return_value = 0;
  674. /* Read Test */
  675. return_value = pwrap_wacs2_read(PMIC_DEW_READ_TEST_ADDR, &rdata);
  676. if (rdata != DEFAULT_VALUE_READ_TEST) {
  677. PWRAPERR("Error: r_rdata=0x%x, exp=0x5aa5,ret=0x%x\n", rdata, return_value);
  678. return E_PWR_READ_TEST_FAIL;
  679. }
  680. PWRAPLOG("Read Test pass,return_value=%d\n", return_value);
  681. return 0;
  682. }
  683. static unsigned int pwrap_write_test(void)
  684. {
  685. unsigned int rdata = 0;
  686. unsigned int sub_return = 0;
  687. unsigned int sub_return1 = 0;
  688. /* Write test using WACS2 */
  689. PWRAPLOG("start pwrap_write\n");
  690. sub_return = pwrap_wacs2_write(PMIC_DEW_WRITE_TEST_ADDR, DEFAULT_VALUE_READ_TEST);
  691. PWRAPLOG("after pwrap_write\n");
  692. sub_return1 = pwrap_wacs2_read(PMIC_DEW_WRITE_TEST_ADDR, &rdata);
  693. if ((rdata != DEFAULT_VALUE_READ_TEST) || (sub_return != 0) || (sub_return1 != 0)) {
  694. PWRAPERR("Err:,w_rdata=0x%x,exp=0xa55a,sub_return=0x%x,sub_return1=0x%x\n", rdata, sub_return,
  695. sub_return1);
  696. return E_PWR_INIT_WRITE_TEST;
  697. }
  698. PWRAPLOG("write Test pass\n");
  699. return 0;
  700. }
  701. static void pwrap_ut(unsigned int ut_test)
  702. {
  703. unsigned int sub_return = 0;
  704. switch (ut_test) {
  705. case 1:
  706. pwrap_write_test();
  707. break;
  708. case 2:
  709. pwrap_read_test();
  710. break;
  711. case 3:
  712. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  713. pwrap_wacs2_ipi(0x10010000 + 0xD8, 0xffffffff, (WRITE_CMD | WRITE_PMIC_WRAP));
  714. break;
  715. #endif
  716. case 4:
  717. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0x1234);
  718. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0x4321);
  719. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0xF0F0);
  720. break;
  721. default:
  722. PWRAPLOG("default test.\n");
  723. break;
  724. }
  725. }
  726. /*-------------------pwrap debug---------------------*/
  727. static inline void pwrap_dump_ap_register(void)
  728. {
  729. unsigned int i = 0, offset = 0;
  730. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  731. unsigned int *reg_addr;
  732. #else
  733. unsigned int reg_addr;
  734. #endif
  735. unsigned int reg_value = 0;
  736. PWRAPERR("dump reg\n");
  737. for (i = 0; i <= PMIC_WRAP_REG_RANGE; i++) {
  738. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  739. reg_addr = (unsigned int *) (PMIC_WRAP_BASE + i * 4);
  740. reg_value = WRAP_RD32(((unsigned int *) (PMIC_WRAP_BASE + i * 4)));
  741. PWRAPERR("addr:0x%p = 0x%x\n", reg_addr, reg_value);
  742. #else
  743. reg_addr = (PMIC_WRAP_BASE + i * 4);
  744. reg_value = WRAP_RD32(reg_addr);
  745. PWRAPERR("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  746. #endif
  747. }
  748. for (i = 0; i <= 14; i++) {
  749. offset = 0xc00 + i * 4;
  750. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  751. reg_addr = (unsigned int *) (PMIC_WRAP_BASE + offset);
  752. reg_value = WRAP_RD32(((unsigned int *) (PMIC_WRAP_BASE + offset)));
  753. PWRAPERR("addr:0x%p = 0x%x\n", reg_addr, reg_value);
  754. #else
  755. reg_addr = (PMIC_WRAP_BASE + offset);
  756. reg_value = WRAP_RD32(reg_addr);
  757. PWRAPERR("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  758. #endif
  759. }
  760. }
  761. void pwrap_dump_all_register(void)
  762. {
  763. pwrap_dump_ap_register();
  764. }
  765. static int is_pwrap_init_done(void)
  766. {
  767. int ret = 0;
  768. ret = WRAP_RD32(PMIC_WRAP_INIT_DONE2);
  769. PWRAPLOG("is_pwrap_init_done %d\n", ret);
  770. if ((ret & 0x1) == 1)
  771. return 0;
  772. return -1;
  773. }
  774. signed int pwrap_init_lk(void)
  775. {
  776. PWRAPFUC();
  777. if (0 == is_pwrap_init_done()) {
  778. PWRAPLOG("[PMIC_WRAP] pwrap_init already init, do nothing\n");
  779. return 0;
  780. }
  781. return 0;
  782. }
  783. static S32 _pwrap_init_sistrobe_reset(int dual_si_sample_settings)
  784. {
  785. WRAP_WR32(PMIC_WRAP_SI_SAMPLE_CTRL, si_sample_ctrl);
  786. return 0;
  787. }
  788. static signed int _pwrap_reset_pattern(void)
  789. {
  790. signed int sub_return = 0;
  791. struct pwrap_rc_info info;
  792. PWRAPLOG("pwrap_init_reset start!!!!!!!!!!!!!\n");
  793. #if 0
  794. /* Set SoC SPI IO Driving Strength to 4 mA */
  795. WRAP_WR32(IOCFG_LM_DRV_CFG1_CLR, 0x7 | (0x7 << 3));
  796. WRAP_WR32(IOCFG_LM_DRV_CFG1_SET, 0x1 | (0x1 << 3));
  797. /* Set SPI MO and MI IOs to Pull Down */
  798. WRAP_WR32(IOCFG_LM_PU_CFG0_CLR, (0x1 << 12) | (0x1 << 13));
  799. WRAP_WR32(IOCFG_LM_PD_CFG0_SET, (0x1 << 12) | (0x1 << 13));
  800. #endif
  801. /* Backup PMIC Wrap key register before reset */
  802. si_sample_ctrl = WRAP_RD32(PMIC_WRAP_SI_SAMPLE_CTRL);
  803. info.wacs2_cmd = WRAP_RD32(PMIC_WRAP_WACS2_CMD);
  804. info.dcxo_en = WRAP_RD32(PMIC_WRAP_DCXO_ENABLE);
  805. info.dcxo_conn_adr0 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_ADR0);
  806. info.dcxo_conn_wdata0 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_WDATA0);
  807. info.dcxo_conn_adr1 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_ADR1);
  808. info.dcxo_conn_wdata1 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_WDATA1);
  809. info.dcxo_nfc_adr0 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_ADR0);
  810. info.dcxo_nfc_wdata0 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_WDATA0);
  811. info.dcxo_nfc_adr1 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_ADR1);
  812. info.dcxo_nfc_wdata1 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_WDATA1);
  813. PWRAPLOG("Backup pwrap key register ok\n");
  814. __pwrap_spi_clk_set();
  815. PWRAPLOG("__pwrap_spi_clk_set ok\n");
  816. /* Enable DCM */
  817. PWRAPLOG("No need to enable DCM\n");
  818. /* Reset SPISLV */
  819. PWRAPLOG("No need to reset SPISLV\n");
  820. /* Enable WRAP */
  821. WRAP_WR32(PMIC_WRAP_WRAP_EN, 0x1);
  822. /* Enable WACS2 */
  823. WRAP_WR32(PMIC_WRAP_WACS2_EN, 0x1);
  824. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x4); /* ONLY WACS2 */
  825. PWRAPLOG("Enable WACS2 ok\n");
  826. /* SPI Waveform Configuration. 0:safe mode, 1:18MHz */
  827. sub_return = _pwrap_init_reg_clock_reset(1);
  828. if (sub_return != 0) {
  829. PWRAPERR("init_reg_clock fail, sub_return=%x\n", sub_return);
  830. return E_PWR_INIT_REG_CLOCK;
  831. }
  832. PWRAPLOG("_pwrap_init_reg_clock_reset ok\n");
  833. /* SPI Slave Configuration */
  834. PWRAPLOG("No need to init SPISLV\n");
  835. /* Enable DIO mode */
  836. sub_return = _pwrap_init_dio_reset(1);
  837. if (sub_return != 0) {
  838. PWRAPERR("init_dio fail, sub_return=%x\n", sub_return);
  839. return E_PWR_INIT_DIO;
  840. }
  841. PWRAPLOG("_pwrap_init_dio_reset ok\n");
  842. /* Input data calibration flow */
  843. sub_return = _pwrap_init_sistrobe_reset(0);
  844. if (sub_return != 0) {
  845. PWRAPERR("InitSiStrobe fail, sub_return=%x\n", sub_return);
  846. return E_PWR_INIT_SIDLY;
  847. }
  848. PWRAPLOG("_pwrap_init_sistrobe_reset ok\n");
  849. /* Status update function initialization
  850. * 1. Signature Checking using CRC (CRC 0 only)
  851. * 2. EINT update
  852. * 3. Read back Auxadc thermal data for GPS
  853. */
  854. _pwrap_InitStaUpd();
  855. PWRAPLOG("_pwrap_InitStaUpd ok\n");
  856. /* PMIC_WRAP starvation setting */
  857. _pwrap_starve_set();
  858. PWRAPLOG("_pwrap_starve_set ok\n");
  859. /* PMIC_WRAP enables */
  860. _pwrap_enable();
  861. PWRAPLOG("_pwrap_enable ok\n");
  862. /* Initialization Done */
  863. WRAP_WR32(PMIC_WRAP_INIT_DONE0, 0x1);
  864. WRAP_WR32(PMIC_WRAP_INIT_DONE2, 0x1);
  865. WRAP_WR32(PMIC_WRAP_INIT_DONE_P2P, 0x1);
  866. WRAP_WR32(PMIC_WRAP_INIT_DONE_MD32, 0x1);
  867. /* restore key register after reset */
  868. WRAP_WR32(PMIC_WRAP_WACS2_CMD, info.wacs2_cmd);
  869. WRAP_WR32(PMIC_WRAP_DCXO_CONN_ADR0, info.dcxo_conn_adr0);
  870. WRAP_WR32(PMIC_WRAP_DCXO_CONN_WDATA0, info.dcxo_conn_wdata0);
  871. WRAP_WR32(PMIC_WRAP_DCXO_CONN_ADR1, info.dcxo_conn_adr1);
  872. WRAP_WR32(PMIC_WRAP_DCXO_CONN_WDATA1, info.dcxo_conn_wdata1);
  873. WRAP_WR32(PMIC_WRAP_DCXO_NFC_ADR0, info.dcxo_nfc_adr0);
  874. WRAP_WR32(PMIC_WRAP_DCXO_NFC_WDATA0, info.dcxo_nfc_wdata0);
  875. WRAP_WR32(PMIC_WRAP_DCXO_NFC_ADR1, info.dcxo_nfc_adr1);
  876. WRAP_WR32(PMIC_WRAP_DCXO_NFC_WDATA1, info.dcxo_nfc_wdata1);
  877. WRAP_WR32(PMIC_WRAP_DCXO_ENABLE, info.dcxo_en);
  878. PWRAPLOG("Restore pwrap key register ok\n");
  879. PWRAPLOG("pwrap_init_reset Done!!!!!!!!!\n");
  880. return 0;
  881. }
  882. static signed int pwrap_reset_pattern(void)
  883. {
  884. /* SPI & WRAP Reset Pattern */
  885. unsigned int ret;
  886. ret = _pwrap_reset_pattern();
  887. if (ret != 0) {
  888. PWRAPERR("_pwrap_reset_pattern fail, ret=%x\n", ret);
  889. return E_PWR_INIT_RESET_SPI;
  890. }
  891. return 0;
  892. }
  893. #endif /*endif PMIC_WRAP_NO_PMIC */