mt_pmic_wrap_init.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. 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, 0x1);
  526. #else
  527. WRAP_WR32(PMIC_WRAP_DIO_EN, 0x3);
  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, 0xffffffff);
  639. WRAP_WR32(PMIC_WRAP_INT1_EN, 0xffffdfff); /* disable Matching interrupt for bit 13 */
  640. }
  641. static signed int _pwrap_init_reg_clock_reset(unsigned int regck_sel)
  642. {
  643. WRAP_WR32(PMIC_WRAP_EXT_CK_WRITE, 0x1);
  644. #ifndef DUAL_PMICS
  645. /* Set Read Dummy Cycle Number (Slave Clock is 18MHz) */
  646. WRAP_WR32(PMIC_WRAP_RDDMY, 0x8);
  647. PWRAPLOG("NO_SLV_CLK_1M Set Read Dummy Cycle\n");
  648. #else
  649. WRAP_WR32(PMIC_WRAP_RDDMY, 0x0808);
  650. PWRAPLOG("NO_SLV_CLK_1M Set Read Dummy Cycle dual_pmics\n");
  651. #endif
  652. /* Config SPI Waveform according to reg clk */
  653. if (regck_sel == 1) { /* Slave Clock is 18MHz */
  654. /* wait data written into register => 4T_PMIC:
  655. * CSHEXT_WRITE_START+EXT_CK+CSHEXT_WRITE_END+CSLEXT_START
  656. */
  657. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x0);
  658. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0x0);
  659. WRAP_WR32(PMIC_WRAP_CSLEXT_WRITE, 0x0);
  660. WRAP_WR32(PMIC_WRAP_CSLEXT_READ, 0x0200);
  661. } else { /*Safe Mode*/
  662. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x0f0f);
  663. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0x0f0f);
  664. WRAP_WR32(PMIC_WRAP_CSLEXT_WRITE, 0x0f0f);
  665. WRAP_WR32(PMIC_WRAP_CSLEXT_READ, 0x0f0f);
  666. }
  667. return 0;
  668. }
  669. static unsigned int pwrap_read_test(void)
  670. {
  671. unsigned int rdata = 0;
  672. unsigned int return_value = 0;
  673. /* Read Test */
  674. return_value = pwrap_wacs2_read(PMIC_DEW_READ_TEST_ADDR, &rdata);
  675. if (rdata != DEFAULT_VALUE_READ_TEST) {
  676. PWRAPERR("Error: r_rdata=0x%x, exp=0x5aa5,ret=0x%x\n", rdata, return_value);
  677. return E_PWR_READ_TEST_FAIL;
  678. }
  679. PWRAPLOG("Read Test pass,return_value=%d\n", return_value);
  680. return 0;
  681. }
  682. static unsigned int pwrap_write_test(void)
  683. {
  684. unsigned int rdata = 0;
  685. unsigned int sub_return = 0;
  686. unsigned int sub_return1 = 0;
  687. /* Write test using WACS2 */
  688. PWRAPLOG("start pwrap_write\n");
  689. sub_return = pwrap_wacs2_write(PMIC_DEW_WRITE_TEST_ADDR, DEFAULT_VALUE_READ_TEST);
  690. PWRAPLOG("after pwrap_write\n");
  691. sub_return1 = pwrap_wacs2_read(PMIC_DEW_WRITE_TEST_ADDR, &rdata);
  692. if ((rdata != DEFAULT_VALUE_READ_TEST) || (sub_return != 0) || (sub_return1 != 0)) {
  693. PWRAPERR("Err:,w_rdata=0x%x,exp=0xa55a,sub_return=0x%x,sub_return1=0x%x\n", rdata, sub_return,
  694. sub_return1);
  695. return E_PWR_INIT_WRITE_TEST;
  696. }
  697. PWRAPLOG("write Test pass\n");
  698. return 0;
  699. }
  700. static void pwrap_ut(unsigned int ut_test)
  701. {
  702. switch (ut_test) {
  703. case 1:
  704. pwrap_write_test();
  705. break;
  706. case 2:
  707. pwrap_read_test();
  708. break;
  709. case 3:
  710. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  711. pwrap_wacs2_ipi(0x10010000 + 0xD8, 0xffffffff, (WRITE_CMD | WRITE_PMIC_WRAP));
  712. break;
  713. #endif
  714. case 4:
  715. pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0x1234);
  716. pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0x4321);
  717. pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0xF0F0);
  718. break;
  719. default:
  720. PWRAPLOG("default test.\n");
  721. break;
  722. }
  723. }
  724. /*-------------------pwrap debug---------------------*/
  725. static inline void pwrap_dump_ap_register(void)
  726. {
  727. unsigned int i = 0, offset = 0;
  728. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  729. unsigned int *reg_addr;
  730. #else
  731. unsigned int reg_addr;
  732. #endif
  733. unsigned int reg_value = 0;
  734. PWRAPERR("dump reg\n");
  735. for (i = 0; i <= PMIC_WRAP_REG_RANGE; i++) {
  736. reg_addr = (PMIC_WRAP_BASE + i * 4);
  737. #if (PMIC_WRAP_KERNEL)
  738. reg_value = WRAP_RD32(((unsigned int *) (PMIC_WRAP_BASE + i * 4)));
  739. #else
  740. reg_value = WRAP_RD32(reg_addr);
  741. #endif
  742. PWRAPERR("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  743. }
  744. for (i = 0; i <= 14; i++) {
  745. offset = 0xc00 + i * 4;
  746. reg_addr = (PMIC_WRAP_BASE + offset);
  747. #if (PMIC_WRAP_KERNEL)
  748. reg_value = WRAP_RD32(((unsigned int *) (PMIC_WRAP_BASE + offset)));
  749. #else
  750. reg_value = WRAP_RD32(reg_addr);
  751. #endif
  752. PWRAPERR("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  753. }
  754. }
  755. void pwrap_dump_all_register(void)
  756. {
  757. pwrap_dump_ap_register();
  758. }
  759. static int is_pwrap_init_done(void)
  760. {
  761. int ret = 0;
  762. ret = WRAP_RD32(PMIC_WRAP_INIT_DONE2);
  763. PWRAPLOG("is_pwrap_init_done %d\n", ret);
  764. if ((ret & 0x1) == 1)
  765. return 0;
  766. return -1;
  767. }
  768. signed int pwrap_init_lk(void)
  769. {
  770. PWRAPFUC();
  771. if (0 == is_pwrap_init_done()) {
  772. PWRAPLOG("[PMIC_WRAP] pwrap_init already init, do nothing\n");
  773. return 0;
  774. }
  775. return 0;
  776. }
  777. static S32 _pwrap_init_sistrobe_reset(int dual_si_sample_settings)
  778. {
  779. WRAP_WR32(PMIC_WRAP_SI_SAMPLE_CTRL, si_sample_ctrl);
  780. return 0;
  781. }
  782. static signed int _pwrap_reset_pattern(void)
  783. {
  784. signed int sub_return = 0;
  785. struct pwrap_rc_info info;
  786. PWRAPLOG("pwrap_init_reset start!!!!!!!!!!!!!\n");
  787. #if 0
  788. /* Set SoC SPI IO Driving Strength to 4 mA */
  789. WRAP_WR32(IOCFG_LM_DRV_CFG1_CLR, 0x3f);
  790. WRAP_WR32(IOCFG_LM_DRV_CFG1_SET, 0x9);
  791. /* Set SPI MI IO to Pull Down */
  792. WRAP_WR32(IOCFG_LM_PU_CFG0_CLR, (0x1 << 12));
  793. WRAP_WR32(IOCFG_LM_PD_CFG0_SET, (0x1 << 12));
  794. #endif
  795. /* Backup PMIC Wrap key register before reset */
  796. si_sample_ctrl = WRAP_RD32(PMIC_WRAP_SI_SAMPLE_CTRL);
  797. info.wacs2_cmd = WRAP_RD32(PMIC_WRAP_WACS2_CMD);
  798. info.dcxo_en = WRAP_RD32(PMIC_WRAP_DCXO_ENABLE);
  799. info.dcxo_conn_adr0 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_ADR0);
  800. info.dcxo_conn_wdata0 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_WDATA0);
  801. info.dcxo_conn_adr1 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_ADR1);
  802. info.dcxo_conn_wdata1 = WRAP_RD32(PMIC_WRAP_DCXO_CONN_WDATA1);
  803. info.dcxo_nfc_adr0 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_ADR0);
  804. info.dcxo_nfc_wdata0 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_WDATA0);
  805. info.dcxo_nfc_adr1 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_ADR1);
  806. info.dcxo_nfc_wdata1 = WRAP_RD32(PMIC_WRAP_DCXO_NFC_WDATA1);
  807. PWRAPLOG("Backup pwrap key register ok\n");
  808. __pwrap_spi_clk_set();
  809. PWRAPLOG("__pwrap_spi_clk_set ok\n");
  810. /* Enable DCM */
  811. PWRAPLOG("No need to enable DCM\n");
  812. /* Reset SPISLV */
  813. PWRAPLOG("No need to reset SPISLV\n");
  814. /* Enable WRAP */
  815. WRAP_WR32(PMIC_WRAP_WRAP_EN, 0x1);
  816. /* Enable WACS2 */
  817. WRAP_WR32(PMIC_WRAP_WACS2_EN, 0x1);
  818. WRAP_WR32(PMIC_WRAP_HPRIO_ARB_EN, 0x4); /* ONLY WACS2 */
  819. PWRAPLOG("Enable WACS2 ok\n");
  820. /* SPI Waveform Configuration. 0:safe mode, 1:18MHz */
  821. sub_return = _pwrap_init_reg_clock_reset(1);
  822. if (sub_return != 0) {
  823. PWRAPERR("init_reg_clock fail, sub_return=%x\n", sub_return);
  824. return E_PWR_INIT_REG_CLOCK;
  825. }
  826. PWRAPLOG("_pwrap_init_reg_clock_reset ok\n");
  827. /* SPI Slave Configuration */
  828. PWRAPLOG("No need to init SPISLV\n");
  829. /* Enable DIO mode */
  830. sub_return = _pwrap_init_dio_reset(1);
  831. if (sub_return != 0) {
  832. PWRAPERR("init_dio fail, sub_return=%x\n", sub_return);
  833. return E_PWR_INIT_DIO;
  834. }
  835. PWRAPLOG("_pwrap_init_dio_reset ok\n");
  836. /* Input data calibration flow */
  837. sub_return = _pwrap_init_sistrobe_reset(0);
  838. if (sub_return != 0) {
  839. PWRAPERR("InitSiStrobe fail, sub_return=%x\n", sub_return);
  840. return E_PWR_INIT_SIDLY;
  841. }
  842. PWRAPLOG("_pwrap_init_sistrobe_reset ok\n");
  843. /* Status update function initialization
  844. * 1. Signature Checking using CRC (CRC 0 only)
  845. * 2. EINT update
  846. * 3. Read back Auxadc thermal data for GPS
  847. */
  848. _pwrap_InitStaUpd();
  849. PWRAPLOG("_pwrap_InitStaUpd ok\n");
  850. /* PMIC_WRAP starvation setting */
  851. _pwrap_starve_set();
  852. PWRAPLOG("_pwrap_starve_set ok\n");
  853. /* PMIC_WRAP enables */
  854. _pwrap_enable();
  855. PWRAPLOG("_pwrap_enable ok\n");
  856. /* Initialization Done */
  857. WRAP_WR32(PMIC_WRAP_INIT_DONE0, 0x1);
  858. WRAP_WR32(PMIC_WRAP_INIT_DONE2, 0x1);
  859. WRAP_WR32(PMIC_WRAP_INIT_DONE_P2P, 0x1);
  860. WRAP_WR32(PMIC_WRAP_INIT_DONE_MD32, 0x1);
  861. /* restore key register after reset */
  862. WRAP_WR32(PMIC_WRAP_WACS2_CMD, info.wacs2_cmd);
  863. WRAP_WR32(PMIC_WRAP_DCXO_CONN_ADR0, info.dcxo_conn_adr0);
  864. WRAP_WR32(PMIC_WRAP_DCXO_CONN_WDATA0, info.dcxo_conn_wdata0);
  865. WRAP_WR32(PMIC_WRAP_DCXO_CONN_ADR1, info.dcxo_conn_adr1);
  866. WRAP_WR32(PMIC_WRAP_DCXO_CONN_WDATA1, info.dcxo_conn_wdata1);
  867. WRAP_WR32(PMIC_WRAP_DCXO_NFC_ADR0, info.dcxo_nfc_adr0);
  868. WRAP_WR32(PMIC_WRAP_DCXO_NFC_WDATA0, info.dcxo_nfc_wdata0);
  869. WRAP_WR32(PMIC_WRAP_DCXO_NFC_ADR1, info.dcxo_nfc_adr1);
  870. WRAP_WR32(PMIC_WRAP_DCXO_NFC_WDATA1, info.dcxo_nfc_wdata1);
  871. WRAP_WR32(PMIC_WRAP_DCXO_ENABLE, info.dcxo_en);
  872. PWRAPLOG("Restore pwrap key register ok\n");
  873. PWRAPLOG("pwrap_init_reset Done!!!!!!!!!\n");
  874. return 0;
  875. }
  876. static signed int pwrap_reset_pattern(void)
  877. {
  878. /* SPI & WRAP Reset Pattern */
  879. unsigned int ret;
  880. ret = _pwrap_reset_pattern();
  881. if (ret != 0) {
  882. PWRAPERR("_pwrap_reset_pattern fail, ret=%x\n", ret);
  883. return E_PWR_INIT_RESET_SPI;
  884. }
  885. return 0;
  886. }
  887. #endif /*endif PMIC_WRAP_NO_PMIC */