mt_pmic_wrap_init.c 45 KB

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