mt_pmic_wrap_init.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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) 2019. 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 2018 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,
  68. unsigned int wdata, unsigned int *rdata)
  69. {
  70. PWRAPLOG("PMIC_WRAP do Nothing.\n");
  71. return 0;
  72. }
  73. signed int pwrap_read(unsigned int adr, unsigned int *rdata)
  74. {
  75. PWRAPLOG("PMIC_WRAP do Nothing.\n");
  76. return 0;
  77. }
  78. signed int pwrap_write(unsigned int adr, unsigned int wdata)
  79. {
  80. PWRAPLOG("PMIC_WRAP do Nothing.\n");
  81. return 0;
  82. }
  83. #endif
  84. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata)
  85. {
  86. PWRAPLOG("PMIC_WRAP do Nothing.\n");
  87. return 0;
  88. }
  89. /* Provide PMIC write API */
  90. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata)
  91. {
  92. PWRAPLOG("PMIC_WRAP do Nothing.\n");
  93. return 0;
  94. }
  95. signed int pwrap_read_nochk(unsigned int adr, unsigned int *rdata)
  96. {
  97. PWRAPLOG("PMIC_WRAP do Nothing.\n");
  98. return 0;
  99. }
  100. signed int pwrap_write_nochk(unsigned int adr, unsigned int wdata)
  101. {
  102. PWRAPLOG("PMIC_WRAP do Nothing.\n");
  103. return 0;
  104. }
  105. /*
  106. * pmic_wrap init, init wrap interface
  107. */
  108. signed int pwrap_init(void)
  109. {
  110. PWRAPLOG("PMIC_WRAP do Nothing.\n");
  111. return 0;
  112. }
  113. signed int pwrap_init_preloader(void)
  114. {
  115. PWRAPLOG("PMIC_WRAP do Nothing.\n");
  116. return 0;
  117. }
  118. signed int pwrap_init_lk(void)
  119. {
  120. PWRAPLOG("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,
  126. unsigned long long timeout_time_ns);
  127. static unsigned long long _pwrap_get_current_time(void);
  128. static unsigned long long _pwrap_time2ns(unsigned long long time_us);
  129. static signed int _pwrap_wacs2_nochk(unsigned int write, unsigned int adr,
  130. unsigned int wdata, unsigned int *rdata);
  131. static signed int _pwrap_swinf_acc_nochk(unsigned int swinf_no, unsigned int cmd,
  132. unsigned int write, unsigned int pmifid, unsigned int slvid, unsigned int addr, unsigned int bytecnt,
  133. unsigned int wdata, unsigned int *rdata);
  134. static signed int _pwrap_swinf_acc(unsigned int swinf_no, unsigned int cmd,
  135. unsigned int write, unsigned int pmifid, unsigned int slvid, unsigned int addr, unsigned int bytecnt,
  136. unsigned int wdata, unsigned int *rdata);
  137. /*********************test API************************************************/
  138. static inline void pwrap_dump_ap_register(void);
  139. static unsigned int pwrap_write_test(void);
  140. static unsigned int pwrap_read_test(void);
  141. static signed int pwrap_reset_pattern(void);
  142. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata);
  143. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata);
  144. static unsigned int si_sample_ctrl = 0;
  145. static struct pwrap_rc_info {
  146. unsigned int swinf_cmd;
  147. unsigned int other_inf_en;
  148. unsigned int spi_mode_ctrl;
  149. unsigned int sleep_protect_ctrl;
  150. unsigned int dcxo_cmd_adr0;
  151. unsigned int dcxo_cmd_wdata0;
  152. unsigned int dcxo_cmd_adr1;
  153. unsigned int dcxo_cmd_wdata1;
  154. unsigned int gps_auxadc_addr;
  155. unsigned int gps_auxadc_cmd;
  156. unsigned int gps_auxadc_rdata;
  157. };
  158. /************* end--internal API**********************************************/
  159. /*********************** external API for pmic_wrap user *********************/
  160. signed int pwrap_wacs2_read(unsigned int adr, unsigned int *rdata)
  161. {
  162. _pwrap_swinf_acc(PMIF_SPI_AP_SWINF_NO, DEFAULT_CMD, 0, PMIF_SPI_PMIFID,
  163. DEFAULT_SLVID, adr, DEFAULT_BYTECNT, 0x0, rdata);
  164. return 0;
  165. }
  166. /* Provide PMIC write API */
  167. signed int pwrap_wacs2_write(unsigned int adr, unsigned int wdata)
  168. {
  169. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  170. unsigned int flag;
  171. flag = WRITE_CMD | (1 << WRITE_PMIC);
  172. pwrap_wacs2_ipi(adr, wdata, flag);
  173. #else
  174. _pwrap_swinf_acc(PMIF_SPI_AP_SWINF_NO, DEFAULT_CMD, 1, PMIF_SPI_PMIFID,
  175. DEFAULT_SLVID, adr, DEFAULT_BYTECNT, wdata, 0x0);
  176. #endif
  177. return 0;
  178. }
  179. signed int pwrap_read(unsigned int adr, unsigned int *rdata)
  180. {
  181. return _pwrap_swinf_acc(PMIF_SPI_AP_SWINF_NO, DEFAULT_CMD, 0, PMIF_SPI_PMIFID,
  182. DEFAULT_SLVID, adr, DEFAULT_BYTECNT, 0x0, rdata);
  183. }
  184. signed int pwrap_write(unsigned int adr, unsigned int wdata)
  185. {
  186. return _pwrap_swinf_acc(PMIF_SPI_AP_SWINF_NO, DEFAULT_CMD, 1, PMIF_SPI_PMIFID,
  187. DEFAULT_SLVID, adr, DEFAULT_BYTECNT, wdata, 0x0);
  188. }
  189. /******************************************************************************
  190. * wrapper timeout
  191. *****************************************************************************/
  192. /* use the same API name with kernel driver
  193. * however,the timeout API in uboot use tick instead of ns
  194. */
  195. #ifdef PWRAP_TIMEOUT
  196. static unsigned long long _pwrap_get_current_time(void)
  197. {
  198. return gpt4_get_current_tick();
  199. }
  200. static int _pwrap_timeout_ns(unsigned long long start_time_ns,
  201. unsigned long long timeout_time_ns)
  202. {
  203. return gpt4_timeout_tick(start_time_ns, timeout_time_ns);
  204. }
  205. static unsigned long long _pwrap_time2ns(unsigned long long time_us)
  206. {
  207. return gpt4_time2tick_us(time_us);
  208. }
  209. #else
  210. static unsigned long long _pwrap_get_current_time(void)
  211. {
  212. return 0;
  213. }
  214. static int _pwrap_timeout_ns(unsigned long long start_time_ns,
  215. unsigned long long elapse_time)
  216. {
  217. return 0;
  218. }
  219. static unsigned long long _pwrap_time2ns(unsigned long long time_us)
  220. {
  221. return 0;
  222. }
  223. #endif /* #ifdef PWRAP_TIMEOUT */
  224. /* ##################################################################### */
  225. /* define macro and inline function (for do while loop) */
  226. /* ##################################################################### */
  227. typedef unsigned int(*loop_condition_fp) (unsigned int); /* define a function pointer */
  228. static inline unsigned int wait_for_fsm_idle(unsigned int x)
  229. {
  230. return GET_SWINF_2_FSM(x) != WACS_FSM_IDLE;
  231. }
  232. static inline unsigned int wait_for_fsm_vldclr(unsigned int x)
  233. {
  234. return GET_SWINF_2_FSM(x) != WACS_FSM_WFVLDCLR;
  235. }
  236. static inline unsigned int wait_for_sync(unsigned int x)
  237. {
  238. return GET_SYNC_IDLE2(x) != WACS_SYNC_IDLE;
  239. }
  240. static inline unsigned int wait_for_idle_and_sync(unsigned int x)
  241. {
  242. return (GET_WACS2_FSM(x) != WACS_FSM_IDLE) ||
  243. (GET_SYNC_IDLE2(x) != WACS_SYNC_IDLE);
  244. }
  245. static inline unsigned int wait_for_wrap_idle(unsigned int x)
  246. {
  247. return (GET_WRAP_FSM(x) != 0x0) || (GET_WRAP_CH_DLE_RESTCNT(x) != 0x0);
  248. }
  249. static inline unsigned int wait_for_wrap_state_idle(unsigned int x)
  250. {
  251. return GET_WRAP_AG_DLE_RESTCNT(x) != 0;
  252. }
  253. static inline unsigned int wait_for_man_idle_and_noreq(unsigned int x)
  254. {
  255. return (GET_MAN_REQ(x) != MAN_FSM_NO_REQ) ||
  256. (GET_MAN_FSM(x) != MAN_FSM_IDLE);
  257. }
  258. static inline unsigned int wait_for_man_vldclr(unsigned int x)
  259. {
  260. return GET_MAN_FSM(x) != MAN_FSM_WFVLDCLR;
  261. }
  262. static inline unsigned int wait_for_cipher_ready(unsigned int x)
  263. {
  264. return x != 3;
  265. }
  266. static inline unsigned int wait_for_stdupd_idle(unsigned int x)
  267. {
  268. return GET_STAUPD_FSM(x) != 0x0;
  269. }
  270. /**************used at _pwrap_wacs2_nochk*************************************/
  271. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  272. static inline unsigned int wait_for_state_ready_init(loop_condition_fp fp,
  273. unsigned int timeout_us, void *wacs_register, unsigned int *read_reg)
  274. #else
  275. static inline unsigned int wait_for_state_ready_init(loop_condition_fp fp,
  276. unsigned int timeout_us, unsigned int *wacs_register,
  277. unsigned int *read_reg)
  278. #endif
  279. {
  280. unsigned long long start_time_ns = 0, timeout_ns = 0;
  281. unsigned int reg_rdata = 0x0;
  282. start_time_ns = _pwrap_get_current_time();
  283. timeout_ns = _pwrap_time2ns(timeout_us);
  284. do {
  285. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  286. PWRAPERR("ready_init timeout\n");
  287. pwrap_dump_ap_register();
  288. return E_PWR_WAIT_IDLE_TIMEOUT;
  289. }
  290. reg_rdata = WRAP_RD32(wacs_register);
  291. } while (fp(reg_rdata));
  292. if (read_reg)
  293. *read_reg = reg_rdata;
  294. return 0;
  295. }
  296. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  297. static inline unsigned int wait_for_state_idle(loop_condition_fp fp,
  298. unsigned int timeout_us, void *wacs_register,
  299. void *wacs_vldclr_register, unsigned int *read_reg)
  300. #else
  301. static inline unsigned int wait_for_state_idle(loop_condition_fp fp,
  302. unsigned int timeout_us, unsigned int *wacs_register,
  303. unsigned int *wacs_vldclr_register, unsigned int *read_reg)
  304. #endif
  305. {
  306. unsigned long long start_time_ns = 0, timeout_ns = 0;
  307. unsigned int reg_rdata;
  308. start_time_ns = _pwrap_get_current_time();
  309. timeout_ns = _pwrap_time2ns(timeout_us);
  310. do {
  311. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  312. PWRAPERR("state_idle timeout\n");
  313. pwrap_dump_ap_register();
  314. return E_PWR_WAIT_IDLE_TIMEOUT;
  315. }
  316. reg_rdata = WRAP_RD32(wacs_register);
  317. if (GET_SWINF_2_INIT_DONE(reg_rdata) != WACS_INIT_DONE) {
  318. PWRAPERR("init isn't finished\n");
  319. pwrap_dump_ap_register();
  320. return E_PWR_NOT_INIT_DONE;
  321. }
  322. switch (GET_SWINF_2_FSM(reg_rdata)) {
  323. case WACS_FSM_WFVLDCLR:
  324. WRAP_WR32(wacs_vldclr_register, 1);
  325. PWRAPERR("WACS_FSM = VLDCLR\n");
  326. break;
  327. case WACS_FSM_WFDLE:
  328. PWRAPERR("WACS_FSM = WFDLE\n");
  329. break;
  330. case WACS_FSM_REQ:
  331. PWRAPERR("WACS_FSM = REQ\n");
  332. break;
  333. default:
  334. break;
  335. }
  336. } while (fp(reg_rdata));
  337. if (read_reg)
  338. *read_reg = reg_rdata;
  339. return 0;
  340. }
  341. /**************used at pwrap_wacs2********************************************/
  342. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  343. static inline unsigned int wait_for_state_ready(loop_condition_fp fp,
  344. unsigned int timeout_us, void *wacs_register, unsigned int *read_reg)
  345. #else
  346. static inline unsigned int wait_for_state_ready(loop_condition_fp fp,
  347. unsigned int timeout_us, unsigned int *wacs_register,
  348. unsigned int *read_reg)
  349. #endif
  350. {
  351. unsigned long long start_time_ns = 0, timeout_ns = 0;
  352. unsigned int reg_rdata;
  353. start_time_ns = _pwrap_get_current_time();
  354. timeout_ns = _pwrap_time2ns(timeout_us);
  355. do {
  356. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  357. PWRAPERR("state_ready timeout\n");
  358. pwrap_dump_ap_register();
  359. return E_PWR_WAIT_IDLE_TIMEOUT;
  360. }
  361. reg_rdata = WRAP_RD32(wacs_register);
  362. if (GET_SWINF_2_INIT_DONE(reg_rdata) != WACS_INIT_DONE) {
  363. PWRAPERR("init isn't finished\n");
  364. pwrap_dump_ap_register();
  365. return E_PWR_NOT_INIT_DONE;
  366. }
  367. } while (fp(reg_rdata));
  368. if (read_reg)
  369. *read_reg = reg_rdata;
  370. return 0;
  371. }
  372. /*********************internal API for pwrap_init***************************/
  373. signed int pwrap_read_nochk(unsigned int adr, unsigned int *rdata)
  374. {
  375. return _pwrap_swinf_acc_nochk(PMIF_SPI_AP_SWINF_NO, DEFAULT_CMD, 0, PMIF_SPI_PMIFID,
  376. DEFAULT_SLVID, adr, DEFAULT_BYTECNT, 0x0, rdata);
  377. }
  378. signed int pwrap_write_nochk(unsigned int adr, unsigned int wdata)
  379. {
  380. return _pwrap_swinf_acc_nochk(PMIF_SPI_AP_SWINF_NO, DEFAULT_CMD, 1, PMIF_SPI_PMIFID,
  381. DEFAULT_SLVID, adr, DEFAULT_BYTECNT, wdata, 0x0);
  382. }
  383. signed int _pwrap_swinf_acc(unsigned int swinf_no, unsigned int cmd,
  384. unsigned int write, unsigned int pmifid, unsigned int slvid, unsigned int addr, unsigned int bytecnt,
  385. unsigned int wdata, unsigned int *rdata)
  386. {
  387. unsigned int reg_rdata = 0x0;
  388. unsigned int wacs_write = 0x0;
  389. unsigned int wacs_adr = 0x0;
  390. unsigned int wacs_cmd = 0x0;
  391. unsigned int return_value = 0x0;
  392. /* Check argument validation */
  393. if ((swinf_no & ~(0x3)) != 0)
  394. return E_PWR_INVALID_SWINF;
  395. if ((cmd & ~(0x3)) != 0)
  396. return E_PWR_INVALID_CMD;
  397. if ((write & ~(0x1)) != 0)
  398. return E_PWR_INVALID_RW;
  399. if ((pmifid & ~(0x3)) != 0)
  400. return E_PWR_INVALID_PMIFID;
  401. if ((slvid & ~(0xf)) != 0)
  402. return E_PWR_INVALID_SLVID;
  403. if ((addr & ~(0xffff)) != 0)
  404. return E_PWR_INVALID_ADDR;
  405. if ((bytecnt & ~(0x1)) != 0)
  406. return E_PWR_INVALID_BYTECNT;
  407. if ((wdata & ~(0xffff)) != 0)
  408. return E_PWR_INVALID_WDAT;
  409. enter_critical_section();
  410. /* Check whether INIT_DONE is set */
  411. if (pmifid == 0)
  412. reg_rdata = WRAP_RD32(PMIF_SPI_PMIF_SWINF_0_STA + 0x40 * swinf_no);
  413. if (GET_SWINF_2_INIT_DONE(reg_rdata) != 0x1) {
  414. exit_critical_section();
  415. return E_PWR_NOT_INIT_DONE;
  416. }
  417. /* Wait for Software Interface FSM state to be IDLE */
  418. while (GET_SWINF_2_FSM(reg_rdata) != 0x0) {
  419. if (pmifid == 0)
  420. reg_rdata = WRAP_RD32(PMIF_SPI_PMIF_SWINF_0_STA + 0x40 * swinf_no);
  421. }
  422. /* Set the write data */
  423. if (write == 1) {
  424. if(pmifid == 0)
  425. WRAP_WR32(PMIF_SPI_PMIF_SWINF_0_WDATA_31_0 + 0x40 * swinf_no, wdata);
  426. }
  427. /* Send the command */
  428. if (pmifid == 0)
  429. WRAP_WR32(PMIF_SPI_PMIF_SWINF_0_ACC + 0x40 * swinf_no,
  430. (cmd << 30) | (write << 29) | (slvid << 24) | (bytecnt << 16) | addr);
  431. if (write == 0) {
  432. /* Wait for Software Interface FSM to be WFVLDCLR, read the data and clear the valid flag */
  433. do {
  434. if (pmifid == 0)
  435. reg_rdata = WRAP_RD32(PMIF_SPI_PMIF_SWINF_0_STA + 0x40 * swinf_no);
  436. } while (GET_SWINF_2_FSM(reg_rdata) != 0x6);
  437. if (pmifid == 0)
  438. *rdata = WRAP_RD32(PMIF_SPI_PMIF_SWINF_0_RDATA_31_0 + 0x40 * swinf_no);
  439. if (pmifid == 0)
  440. WRAP_WR32(PMIF_SPI_PMIF_SWINF_0_VLD_CLR + 0x40 * swinf_no, 0x1);
  441. }
  442. exit_critical_section();
  443. return 0;
  444. }
  445. static signed int _pwrap_swinf_acc_nochk(unsigned int swinf_no, unsigned int cmd,
  446. unsigned int write, unsigned int pmifid, unsigned int slvid, unsigned int addr, unsigned int bytecnt,
  447. unsigned int wdata, unsigned int *rdata)
  448. {
  449. unsigned int reg_rdata = 0x0;
  450. unsigned int wacs_write = 0x0;
  451. unsigned int wacs_adr = 0x0;
  452. unsigned int wacs_cmd = 0x0;
  453. unsigned int return_value = 0x0;
  454. /* Check argument validation */
  455. if ((swinf_no & ~(0x3)) != 0)
  456. return E_PWR_INVALID_SWINF;
  457. if ((cmd & ~(0x3)) != 0)
  458. return E_PWR_INVALID_CMD;
  459. if ((write & ~(0x1)) != 0)
  460. return E_PWR_INVALID_RW;
  461. if ((pmifid & ~(0x3)) != 0)
  462. return E_PWR_INVALID_PMIFID;
  463. if ((slvid & ~(0xf)) != 0)
  464. return E_PWR_INVALID_SLVID;
  465. if ((addr & ~(0xffff)) != 0)
  466. return E_PWR_INVALID_ADDR;
  467. if ((bytecnt & ~(0x1)) != 0)
  468. return E_PWR_INVALID_BYTECNT;
  469. if ((wdata & ~(0xffff)) != 0)
  470. return E_PWR_INVALID_WDAT;
  471. enter_critical_section();
  472. /* Wait for Software Interface FSM state to be IDLE */
  473. do {
  474. if (pmifid == 0)
  475. reg_rdata = WRAP_RD32(PMIF_SPI_PMIF_SWINF_0_STA + 0x40 * swinf_no);
  476. } while (GET_SWINF_2_FSM(reg_rdata) != 0x0);
  477. /* Set the write data */
  478. if (write == 1) {
  479. if(pmifid == 0)
  480. WRAP_WR32(PMIF_SPI_PMIF_SWINF_0_WDATA_31_0 + 0x40 * swinf_no, wdata);
  481. }
  482. /* Send the command */
  483. if (pmifid == 0)
  484. WRAP_WR32(PMIF_SPI_PMIF_SWINF_0_ACC + 0x40 * swinf_no,
  485. (cmd << 30) | (write << 29) | (slvid << 24) | (bytecnt << 16) | addr);
  486. if (write == 0) {
  487. /* Wait for Software Interface FSM to be WFVLDCLR, read the data and clear the valid flag */
  488. do {
  489. if (pmifid == 0)
  490. reg_rdata = WRAP_RD32(PMIF_SPI_PMIF_SWINF_0_STA + 0x40 * swinf_no);
  491. } while (GET_SWINF_2_FSM(reg_rdata) != 0x6);
  492. if (pmifid == 0)
  493. *rdata = WRAP_RD32(PMIF_SPI_PMIF_SWINF_0_RDATA_31_0 + 0x40 * swinf_no);
  494. if (pmifid == 0)
  495. WRAP_WR32(PMIF_SPI_PMIF_SWINF_0_VLD_CLR + 0x40 * swinf_no, 0x1);
  496. }
  497. exit_critical_section();
  498. return 0;
  499. }
  500. static void __pwrap_soft_reset(void)
  501. {
  502. PWRAPLOG("start reset wrapper\n");
  503. WRAP_WR32(INFRA_GLOBALCON_RST2_SET, 0x1);
  504. WRAP_WR32(INFRA_GLOBALCON_RST2_CLR, 0x1);
  505. }
  506. static void __pwrap_spi_clk_set(void)
  507. {
  508. /* Turn off SYS, TMR and SPI clocks */
  509. WRAP_WR32(MODULE_SW_CG_0_SET, 0x0000000f);
  510. PWRAPLOG("pwrap_spictl reset ok\n");
  511. #ifndef MACH_FPGA
  512. /* Set ULPOSC clock to ULPOSC1/10 */
  513. WRAP_WR32(CLK_CFG_8_CLR, (0x1 << 15) | (0x1 << 12) | (0x7 << 8));
  514. WRAP_WR32(CLK_CFG_UPDATE1, 0x1 << 2);
  515. #endif
  516. /* Disable Fixed 26M Clock Control by SPM */
  517. PWRAPLOG("PMICW_CLOCK_CTRL:0x%x(before)\n", WRAP_RD32(PMICW_CLOCK_CTRL));
  518. WRAP_WR32(PMICW_CLOCK_CTRL_CLR, 0xf);
  519. PWRAPLOG("PMICW_CLOCK_CTRL:0x%x(after)\n", WRAP_RD32(PMICW_CLOCK_CTRL));
  520. /* Toggle PMIC_WRAP and pwrap_spictl reset */
  521. __pwrap_soft_reset();
  522. /* Turn on SYS, TMR and SPI clocks */
  523. WRAP_WR32(MODULE_SW_CG_0_CLR, 0x0000000f);
  524. PWRAPLOG("spi clk set ....\n");
  525. }
  526. static void _pwrap_InitStaUpd(void)
  527. {
  528. /* Signature mode */
  529. WRAP_WR32(PMIF_SPI_PMIF_SIG_MODE, 0x1);
  530. WRAP_WR32(PMIF_SPI_PMIF_PMIC_SIG_VAL, 0x83);
  531. WRAP_WR32(PMIF_SPI_PMIF_PMIC_SIG_ADDR, PMIC_DEW_CRC_VAL_ADDR);
  532. /* Setup PMIC EINT */
  533. WRAP_WR32(PMIF_SPI_PMIF_PMIC_EINT_STA_ADDR, PMIC_CPU_INT_STA_ADDR);
  534. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_LATEST_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  535. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_WP_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  536. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_0_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  537. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_1_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  538. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_2_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  539. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_3_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  540. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_4_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  541. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_5_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  542. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_6_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  543. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_7_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  544. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_8_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  545. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_9_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  546. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_10_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  547. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_11_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  548. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_12_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  549. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_13_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  550. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_14_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  551. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_15_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  552. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_16_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  553. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_17_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  554. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_18_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  555. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_19_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  556. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_20_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  557. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_21_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  558. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_22_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  559. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_23_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  560. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_24_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  561. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_25_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  562. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_26_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  563. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_27_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  564. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_28_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  565. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_29_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  566. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_30_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  567. WRAP_WR32(PMIF_SPI_PMIF_MD_AUXADC_RDATA_31_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_AP_ADDR << 16) + PMIC_AUXADC_ADC_OUT_MDRT_ADDR);
  568. /* Internal GPS AUXADC Interface */
  569. WRAP_WR32(PMIF_SPI_PMIF_INT_GPS_AUXADC_CMD_ADDR, (PMIC_AUXADC_RQST_DCXO_BY_GPS_ADDR << 16) + PMIC_AUXADC_RQST_CH7_BY_GPS_ADDR);
  570. WRAP_WR32(PMIF_SPI_PMIF_INT_GPS_AUXADC_CMD, (0x0040 << 16) + 0x0010);
  571. WRAP_WR32(PMIF_SPI_PMIF_INT_GPS_AUXADC_RDATA_ADDR, (PMIC_AUXADC_ADC_OUT_DCXO_BY_GPS_ADDR << 16) + PMIC_AUXADC_ADC_OUT_CH7_BY_GPS_ADDR);
  572. /* Status Update Period and Items */
  573. WRAP_WR32(PMIF_SPI_PMIF_STAUPD_CTRL, (0x5 << 4) | 0x5);
  574. }
  575. static unsigned int pwrap_read_test(void)
  576. {
  577. unsigned int rdata = 0;
  578. unsigned int return_value = 0;
  579. /* Read Test */
  580. PWRAPLOG("start pwrap_read_test\n");
  581. return_value = pwrap_wacs2_read(PMIC_DEW_READ_TEST_ADDR, &rdata);
  582. PWRAPLOG("rdata=0x%x\n", rdata);
  583. if (rdata != DEFAULT_VALUE_READ_TEST) {
  584. PWRAPERR("Error: r_rdata = 0x%x, exp = 0x5aa5\n", rdata);
  585. PWRAPERR("Error: return_value = 0x%x\n", return_value);
  586. return E_PWR_READ_TEST_FAIL;
  587. }
  588. else
  589. PWRAPLOG("Read Test pass, return_value = 0x%x\n", return_value);
  590. return 0;
  591. }
  592. static unsigned int pwrap_write_test(void)
  593. {
  594. unsigned int rdata = 0;
  595. unsigned int sub_return = 0;
  596. unsigned int sub_return1 = 0;
  597. /* Write test using WACS2 */
  598. PWRAPLOG("start pwrap_write_test\n");
  599. sub_return = pwrap_wacs2_write(PMIC_DEW_WRITE_TEST_ADDR,
  600. DEFAULT_VALUE_WRITE_TEST);
  601. PWRAPLOG("after pwrap_write\n");
  602. sub_return1 = pwrap_wacs2_read(PMIC_DEW_WRITE_TEST_ADDR, &rdata);
  603. PWRAPLOG("rdata=0x%x (read back)\n", rdata);
  604. if ((rdata != DEFAULT_VALUE_WRITE_TEST) ||
  605. (sub_return != 0) || (sub_return1 != 0)) {
  606. PWRAPERR("Error: w_rdata = 0x%x, exp = 0xa55a\n", rdata);
  607. PWRAPERR("Error: sub_return = 0x%x\n", sub_return);
  608. PWRAPERR("Error: sub_return1 = 0x%x\n", sub_return1);
  609. return E_PWR_INIT_WRITE_TEST;
  610. }
  611. else
  612. PWRAPLOG("Write Test pass\n");
  613. return 0;
  614. }
  615. static void pwrap_ut(unsigned int ut_test)
  616. {
  617. unsigned int sub_return = 0;
  618. switch (ut_test) {
  619. case 1:
  620. pwrap_write_test();
  621. break;
  622. case 2:
  623. pwrap_read_test();
  624. break;
  625. case 3:
  626. #ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
  627. pwrap_wacs2_ipi(0x10010000 + 0xD8, 0xffffffff, (WRITE_CMD | WRITE_PMIC_WRAP));
  628. break;
  629. #endif
  630. case 4:
  631. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0x1234);
  632. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0x4321);
  633. sub_return = pwrap_write_nochk(PMIC_DEW_WRITE_TEST_ADDR, 0xF0F0);
  634. if (sub_return != 0) {
  635. PWRAPERR("w_t_fail, sub_return=%x\n", sub_return);
  636. }
  637. break;
  638. default:
  639. PWRAPLOG("default test\n");
  640. break;
  641. }
  642. }
  643. /*-------------------pwrap debug---------------------*/
  644. static inline void pwrap_dump_ap_register(void)
  645. {
  646. unsigned int i = 0, offset = 0;
  647. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  648. unsigned int *reg_addr;
  649. #else
  650. unsigned int reg_addr;
  651. #endif
  652. unsigned int reg_value = 0;
  653. PWRAPCRI("dump pmif reg\n");
  654. for (i = 0; i <= (PMIF_REG_RANGE/4); i++) {
  655. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  656. reg_addr = (unsigned int *) (PMIF_SPI_BASE + i * 4);
  657. reg_value = WRAP_RD32(reg_addr);
  658. PWRAPCRI("addr:0x%p = 0x%x\n", reg_addr, reg_value);
  659. #else
  660. reg_addr = (PMIF_SPI_BASE + i * 4);
  661. reg_value = WRAP_RD32(reg_addr);
  662. PWRAPCRI("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  663. #endif
  664. }
  665. for (i = 0; i <= 10; i++) {
  666. offset = 0xc00 + i * 4;
  667. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  668. reg_addr = (unsigned int *) (PMIF_SPI_BASE + offset);
  669. reg_value = WRAP_RD32(reg_addr);
  670. PWRAPCRI("addr:0x%p = 0x%x\n", reg_addr, reg_value);
  671. #else
  672. reg_addr = (PMIF_SPI_BASE + offset);
  673. reg_value = WRAP_RD32(reg_addr);
  674. PWRAPCRI("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  675. #endif
  676. }
  677. for (i = 0; i <= 10; i++) {
  678. offset = 0xc40 + i * 4;
  679. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  680. reg_addr = (unsigned int *) (PMIF_SPI_BASE + offset);
  681. reg_value = WRAP_RD32(reg_addr);
  682. PWRAPCRI("addr:0x%p = 0x%x\n", reg_addr, reg_value);
  683. #else
  684. reg_addr = (PMIF_SPI_BASE + offset);
  685. reg_value = WRAP_RD32(reg_addr);
  686. PWRAPCRI("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  687. #endif
  688. }
  689. for (i = 0; i <= 10; i++) {
  690. offset = 0xc80 + i * 4;
  691. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  692. reg_addr = (unsigned int *) (PMIF_SPI_BASE + offset);
  693. reg_value = WRAP_RD32(reg_addr);
  694. PWRAPCRI("addr:0x%p = 0x%x\n", reg_addr, reg_value);
  695. #else
  696. reg_addr = (PMIF_SPI_BASE + offset);
  697. reg_value = WRAP_RD32(reg_addr);
  698. PWRAPCRI("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  699. #endif
  700. }
  701. PWRAPCRI("dump pmicspi_mst reg\n");
  702. for (i = 0; i <= (PMICSPI_MST_REG_RANGE/4); i++) {
  703. #if (PMIC_WRAP_KERNEL) || (PMIC_WRAP_CTP)
  704. reg_addr = (unsigned int *) (PMICSPI_MST_BASE + i * 4);
  705. reg_value = WRAP_RD32(reg_addr);
  706. PWRAPCRI("addr:0x%p = 0x%x\n", reg_addr, reg_value);
  707. #else
  708. reg_addr = (PMICSPI_MST_BASE + i * 4);
  709. reg_value = WRAP_RD32(reg_addr);
  710. PWRAPCRI("addr:0x%x = 0x%x\n", reg_addr, reg_value);
  711. #endif
  712. }
  713. }
  714. void pwrap_dump_all_register(void)
  715. {
  716. pwrap_dump_ap_register();
  717. }
  718. static int is_pwrap_init_done(void)
  719. {
  720. int ret = 0;
  721. ret = WRAP_RD32(PMIF_SPI_PMIF_INIT_DONE);
  722. PWRAPLOG("is_pwrap_init_done %d\n", ret);
  723. if ((ret & 0x1) == 1)
  724. return 0;
  725. return E_PWR_NOT_INIT_DONE;
  726. }
  727. signed int pwrap_init_lk(void)
  728. {
  729. PWRAPFUC();
  730. if (0 == is_pwrap_init_done()) {
  731. PWRAPLOG("wrap_init already init, do nothing\n");
  732. return 0;
  733. }
  734. return 0;
  735. }
  736. static signed int _pwrap_reset_spislv(void)
  737. {
  738. unsigned int ret = 0;
  739. unsigned int rdata = 0;
  740. unsigned int return_value = 0;
  741. unsigned int pmicspi_mst_dio_en_backup = 0;
  742. WRAP_WR32(PMICSPI_MST_SPIWRAP_EN, 0x0);
  743. WRAP_WR32(PMICSPI_MST_SPIMUX_SEL, 0x1);
  744. WRAP_WR32(PMICSPI_MST_SPIMAN_EN, 0x1);
  745. pmicspi_mst_dio_en_backup = WRAP_RD32(PMICSPI_MST_DIO_EN);
  746. WRAP_WR32(PMICSPI_MST_DIO_EN, 0x0);
  747. WRAP_WR32(PMICSPI_MST_SPIMAN_ACC, (0x1 << 13) | (OP_CSL << 8));
  748. WRAP_WR32(PMICSPI_MST_SPIMAN_ACC, (0x1 << 13) | (OP_OUTS << 8)); //Reset the counter.
  749. WRAP_WR32(PMICSPI_MST_SPIMAN_ACC, (0x1 << 13) | (OP_CSH << 8));
  750. WRAP_WR32(PMICSPI_MST_SPIMAN_ACC, (0x1 << 13) | (OP_OUTS << 8));
  751. WRAP_WR32(PMICSPI_MST_SPIMAN_ACC, (0x1 << 13) | (OP_OUTS << 8));
  752. WRAP_WR32(PMICSPI_MST_SPIMAN_ACC, (0x1 << 13) | (OP_OUTS << 8));
  753. WRAP_WR32(PMICSPI_MST_SPIMAN_ACC, (0x1 << 13) | (OP_OUTS << 8));
  754. /* Wait for PMIC SPI Master to be idle */
  755. do {
  756. rdata = WRAP_RD32(PMICSPI_MST_OTHER_BUSY_STA_0);
  757. } while (GET_PMICSPI_BUSY(rdata) != 0x0);
  758. WRAP_WR32(PMICSPI_MST_SPIMAN_EN, 0x0);
  759. WRAP_WR32(PMICSPI_MST_SPIMUX_SEL, 0x0);
  760. WRAP_WR32(PMICSPI_MST_SPIWRAP_EN, 0x1);
  761. WRAP_WR32(PMICSPI_MST_DIO_EN, pmicspi_mst_dio_en_backup);
  762. return 0;
  763. }
  764. static signed int _pwrap_init_reg_clock_reset(unsigned int regck_sel)
  765. {
  766. unsigned int rdata;
  767. /* Configure SPI protocol */
  768. WRAP_WR32(PMICSPI_MST_EXT_CK_WRITE, 0x1);
  769. WRAP_WR32(PMICSPI_MST_EXT_CK_READ, 0x0);
  770. WRAP_WR32(PMICSPI_MST_CSHEXT_WRITE, 0x0);
  771. WRAP_WR32(PMICSPI_MST_CSHEXT_READ, 0x0);
  772. WRAP_WR32(PMICSPI_MST_CSLEXT_WRITE, 0x0);
  773. WRAP_WR32(PMICSPI_MST_CSLEXT_READ, 0x0100);
  774. /* Set Read Dummy Cycle Number (Slave Clock is 18MHz) */
  775. WRAP_WR32(PMICSPI_MST_RDDMY, 0x8);
  776. PWRAPLOG("Set Read Dummy Cycle ok\n");
  777. /* Wait for completion of sending the commands */
  778. do {
  779. rdata = WRAP_RD32(PMIF_SPI_PMIF_INF_BUSY_STA);
  780. } while ((rdata & (0x1 << PMIF_SPI_AP_SWINF_CHAN_NO)) != 0x0);
  781. do {
  782. rdata = WRAP_RD32(PMIF_SPI_PMIF_OTHER_BUSY_STA_0);
  783. } while (GET_CMDISSUE_BUSY(rdata) != 0x0);
  784. do {
  785. rdata = WRAP_RD32(PMICSPI_MST_OTHER_BUSY_STA_0);
  786. } while (GET_PMICSPI_BUSY(rdata) != 0x0);
  787. /* Enable DIO mode */
  788. WRAP_WR32(PMICSPI_MST_DIO_EN, 0x1);
  789. PWRAPLOG("_pwrap_init_dio ok\n");
  790. return 0;
  791. }
  792. static int _pwrap_lock_SPISLVReg(void)
  793. {
  794. pwrap_write_nochk(PMIC_SPISLV_KEY_ADDR, 0x0);
  795. return 0;
  796. }
  797. static int _pwrap_unlock_SPISLVReg(void)
  798. {
  799. pwrap_write_nochk(PMIC_SPISLV_KEY_ADDR, 0xbade);
  800. return 0;
  801. }
  802. static S32 _pwrap_init_sistrobe_reset(void)
  803. {
  804. WRAP_WR32(PMICSPI_MST_SI_SAMPLING_CTRL, si_sample_ctrl);
  805. return 0;
  806. }
  807. signed int PMICSPIMSTDrv_Reset(void)
  808. {
  809. signed int sub_return = 0;
  810. unsigned int rdata = 0x0;
  811. /* Reset SPI Slave */
  812. sub_return = _pwrap_reset_spislv();
  813. if (sub_return != 0) {
  814. PWRAPERR("reset_spislv fail, ret=%x\n", sub_return);
  815. return E_PWR_INIT_RESET_SPI;
  816. }
  817. PWRAPLOG("Reset SPISLV ok\n");
  818. /* Enable SPI Wrapper */
  819. WRAP_WR32(PMICSPI_MST_SPIWRAP_EN, 0x1);
  820. /* SPI Waveform Configuration. 0: Safe Mode, 1: SPISLV Clock is 18MHz */
  821. sub_return = _pwrap_init_reg_clock_reset(1);
  822. if (sub_return != 0) {
  823. PWRAPERR("_pwrap_init_reg_clock_reset fail, ret=%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. /* Input data calibration flow; */
  830. sub_return = _pwrap_init_sistrobe_reset();
  831. if (sub_return != 0) {
  832. PWRAPERR("InitSiStrobe fail, sub_return=%x\n", sub_return);
  833. return E_PWR_INIT_SIDLY;
  834. }
  835. PWRAPLOG("_pwrap_init_sistrobe_reset ok\n");
  836. /* Lock SPI Slave Registers */
  837. PWRAPLOG("No need to lock SPISLV\n");
  838. return 0;
  839. }
  840. static signed int _pwrap_reset_pattern(void)
  841. {
  842. signed int sub_return = 0;
  843. struct pwrap_rc_info info;
  844. PWRAPLOG("pwrap_init_reset start!!!!!!!!!!!!!\n");
  845. /* Backup PMIC Wrap key register before reset */
  846. si_sample_ctrl = WRAP_RD32(PMICSPI_MST_SI_SAMPLING_CTRL);
  847. info.swinf_cmd = WRAP_RD32(PMIF_SPI_PMIF_SWINF_0_ACC + 0x40 * PMIF_SPI_AP_SWINF_NO);
  848. info.other_inf_en = WRAP_RD32(PMIF_SPI_PMIF_OTHER_INF_EN);
  849. info.spi_mode_ctrl = WRAP_RD32(PMIF_SPI_PMIF_SPI_MODE_CTRL);
  850. info.sleep_protect_ctrl = WRAP_RD32(PMIF_SPI_PMIF_SLEEP_PROTECTION_CTRL);
  851. info.dcxo_cmd_adr0 = WRAP_RD32(PMIF_SPI_PMIF_DCXO_CMD_ADDR_0);
  852. info.dcxo_cmd_wdata0 = WRAP_RD32(PMIF_SPI_PMIF_DCXO_CMD_WDATA_0);
  853. info.dcxo_cmd_adr1 = WRAP_RD32(PMIF_SPI_PMIF_DCXO_CMD_ADDR_1);
  854. info.dcxo_cmd_wdata1 = WRAP_RD32(PMIF_SPI_PMIF_DCXO_CMD_WDATA_1);
  855. info.gps_auxadc_addr = WRAP_RD32(PMIF_SPI_PMIF_INT_GPS_AUXADC_CMD_ADDR);
  856. info.gps_auxadc_cmd = WRAP_RD32(PMIF_SPI_PMIF_INT_GPS_AUXADC_CMD);
  857. info.gps_auxadc_rdata = WRAP_RD32(PMIF_SPI_PMIF_INT_GPS_AUXADC_RDATA_ADDR);
  858. PWRAPLOG("Backup pwrap key register ok\n");
  859. __pwrap_spi_clk_set();
  860. PWRAPLOG("__pwrap_spi_clk_set ok\n");
  861. /* Enable SWINF for AP */
  862. WRAP_WR32(PMIF_SPI_PMIF_INF_EN, 0x1 << PMIF_SPI_AP_SWINF_CHAN_NO);
  863. /* Enable arbitration for SWINF for AP */
  864. WRAP_WR32(PMIF_SPI_PMIF_ARB_EN, 0x1 << PMIF_SPI_AP_SWINF_CHAN_NO);
  865. /* Enable PMIF_SPI Command Issue */
  866. WRAP_WR32(PMIF_SPI_PMIF_CMDISSUE_EN, 0x1);
  867. /* Initialize PMIC SPI Master */
  868. sub_return = PMICSPIMSTDrv_Reset();
  869. if (sub_return != 0) {
  870. PWRAPERR("PMICSPIMSTDrv_Reset Failed, sub_return = %x, exp = 0\n", sub_return);
  871. return sub_return;
  872. }
  873. /* Status update function initialization
  874. * 1. Signature Checking using CRC (CRC 0 only)
  875. * 2. EINT update
  876. * 3. Read back Auxadc thermal data for GPS
  877. */
  878. _pwrap_InitStaUpd();
  879. PWRAPLOG("_pwrap_InitStaUpd ok\n");
  880. /* Configure PMIF Timer */
  881. WRAP_WR32(PMIF_SPI_PMIF_TIMER_CTRL, 0x3);
  882. /* Enable interfaces and arbitration */
  883. WRAP_WR32(PMIF_SPI_PMIF_INF_EN, 0x307f | (0x1 << PMIF_SPI_MD_SWINF_CHAN_NO) |
  884. (0x1 << PMIF_SPI_AP_SECURE_SWINF_CHAN_NO) |
  885. (0x1 << PMIF_SPI_AP_SWINF_CHAN_NO));
  886. WRAP_WR32(PMIF_SPI_PMIF_ARB_EN, 0x707f | (0x1 << PMIF_SPI_MD_SWINF_CHAN_NO) |
  887. (0x1 << PMIF_SPI_AP_SECURE_SWINF_CHAN_NO) |
  888. (0x1 << PMIF_SPI_AP_SWINF_CHAN_NO));
  889. /* Set INIT_DONE */
  890. WRAP_WR32(PMIF_SPI_PMIF_INIT_DONE, 0x1);
  891. #ifndef MACH_FPGA
  892. /* Configure MD ADC Interface */
  893. udelay(100);
  894. PWRAPLOG("write MODEM_TEMP_SHARE_CTRL start\n");
  895. WRAP_WR32(MODEM_TEMP_SHARE_CTRL, 0xf0);
  896. PWRAPLOG("write MODEM_TEMP_SHARE_CTRL ok\n");
  897. PWRAPLOG("MODEM_TEMP_SHARE_CTRL:%x\n", WRAP_RD32(MODEM_TEMP_SHARE_CTRL));
  898. #endif
  899. /* Restore key register after reset */
  900. WRAP_WR32(PMIF_SPI_PMIF_SWINF_0_ACC + 0x40 * PMIF_SPI_AP_SWINF_NO, info.swinf_cmd);
  901. WRAP_WR32(PMIF_SPI_PMIF_DCXO_CMD_ADDR_0, info.dcxo_cmd_adr0);
  902. WRAP_WR32(PMIF_SPI_PMIF_DCXO_CMD_WDATA_0, info.dcxo_cmd_wdata0);
  903. WRAP_WR32(PMIF_SPI_PMIF_DCXO_CMD_ADDR_1, info.dcxo_cmd_adr1);
  904. WRAP_WR32(PMIF_SPI_PMIF_DCXO_CMD_WDATA_1, info.dcxo_cmd_wdata1);
  905. WRAP_WR32(PMIF_SPI_PMIF_INT_GPS_AUXADC_CMD_ADDR, info.gps_auxadc_addr);
  906. WRAP_WR32(PMIF_SPI_PMIF_INT_GPS_AUXADC_CMD, info.gps_auxadc_cmd);
  907. WRAP_WR32(PMIF_SPI_PMIF_INT_GPS_AUXADC_RDATA_ADDR, info.gps_auxadc_rdata);
  908. WRAP_WR32(PMIF_SPI_PMIF_SLEEP_PROTECTION_CTRL, info.sleep_protect_ctrl);
  909. WRAP_WR32(PMIF_SPI_PMIF_SPI_MODE_CTRL, info.spi_mode_ctrl);
  910. WRAP_WR32(PMIF_SPI_PMIF_OTHER_INF_EN, info.other_inf_en);
  911. PWRAPLOG("Restore pwrap key register ok\n");
  912. /* Write Test */
  913. sub_return = pwrap_write_test();
  914. if (sub_return != 0) {
  915. PWRAPERR("write test fail\n");
  916. return E_PWR_INIT_WRITE_TEST;
  917. }
  918. PWRAPLOG("pwrap_write_test ok\n");
  919. PWRAPLOG("pwrap_init_reset Done!!!!!!!!!\n");
  920. return 0;
  921. }
  922. static signed int pwrap_reset_pattern(void)
  923. {
  924. /* SPI & WRAP Reset Pattern */
  925. unsigned int ret;
  926. ret = _pwrap_reset_pattern();
  927. if (ret != 0) {
  928. PWRAPERR("_pwrap_reset_pattern fail, ret=%x\n", ret);
  929. return E_PWR_INIT_RESET_SPI;
  930. }
  931. return 0;
  932. }
  933. void pwrap_disable(void) {
  934. PWRAPLOG("pmic wrap disable\n");
  935. WRAP_WR32(PMICSPI_MST_SPIWRAP_EN, 0x0);
  936. udelay(10);
  937. }
  938. #endif /*endif PMIC_WRAP_NO_PMIC */