pwrap_hal.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. /******************************************************************************
  2. * pwrap_hal.c - Linux pmic_wrapper Driver,hardware_dependent driver
  3. *
  4. *
  5. * DESCRIPTION:
  6. * This file provid the other drivers PMIC wrapper relative functions
  7. *
  8. ******************************************************************************/
  9. #include <linux/spinlock.h>
  10. #include <linux/delay.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/sched.h>
  13. #include <linux/timer.h>
  14. #include <linux/io.h>
  15. #ifdef CONFIG_OF
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_address.h>
  19. #endif
  20. #include <mach/mt_pmic_wrap.h>
  21. #include "pwrap_hal.h"
  22. #define PMIC_WRAP_DEVICE "pmic_wrap"
  23. /*-----start-- global variable-------------------------------------------------*/
  24. #ifdef CONFIG_OF
  25. void __iomem *pwrap_base;
  26. static void __iomem *topckgen_base;
  27. static void __iomem *infracfg_ao_base;
  28. #endif
  29. static struct mt_pmic_wrap_driver *mt_wrp;
  30. static spinlock_t wrp_lock = __SPIN_LOCK_UNLOCKED(lock);
  31. /* spinlock_t wrp_lock = __SPIN_LOCK_UNLOCKED(lock); */
  32. /* ----------interral API ------------------------ */
  33. static s32 _pwrap_init_dio(u32 dio_en);
  34. static s32 _pwrap_init_cipher(void);
  35. static s32 _pwrap_init_reg_clock(u32 regck_sel);
  36. static s32 _pwrap_wacs2_nochk(u32 write, u32 adr, u32 wdata, u32 *rdata);
  37. #ifdef CONFIG_OF
  38. static int pwrap_of_iomap(void);
  39. static void pwrap_of_iounmap(void);
  40. #endif
  41. #ifdef PMIC_WRAP_NO_PMIC
  42. /*-pwrap debug--------------------------------------------------------------------------*/
  43. static inline void pwrap_dump_all_register(void)
  44. {
  45. }
  46. /********************************************************************************************/
  47. /* extern API for PMIC driver, INT related control, this INT is for PMIC chip to AP */
  48. /********************************************************************************************/
  49. u32 mt_pmic_wrap_eint_status(void)
  50. {
  51. PWRAPLOG("[PMIC2]first of all PMIC_WRAP_STAUPD_GRPEN=0x%x\n", WRAP_RD32(PMIC_WRAP_STAUPD_GRPEN));
  52. PWRAPLOG("[PMIC2]first of all PMIC_WRAP_EINT_STA=0x%x\n", WRAP_RD32(PMIC_WRAP_EINT_STA));
  53. return WRAP_RD32(PMIC_WRAP_EINT_STA);
  54. }
  55. void mt_pmic_wrap_eint_clr(int offset)
  56. {
  57. if ((offset < 0) || (offset > 3))
  58. PWRAPERR("clear EINT flag error, only 0-3 bit\n");
  59. else
  60. WRAP_WR32(PMIC_WRAP_EINT_CLR, (1<<offset));
  61. }
  62. /* -------------------------------------------------------- */
  63. /* Function : pwrap_wacs2_hal() */
  64. /* Description : */
  65. /* Parameter : */
  66. /* Return : */
  67. /* -------------------------------------------------------- */
  68. static s32 pwrap_wacs2_hal(u32 write, u32 adr, u32 wdata, u32 *rdata)
  69. {
  70. PWRAPERR("there is no PMIC real chip,PMIC_WRAP do Nothing\n");
  71. return 0;
  72. }
  73. /*
  74. *pmic_wrap init,init wrap interface
  75. *
  76. */
  77. s32 pwrap_init(void)
  78. {
  79. return 0;
  80. }
  81. /* EXPORT_SYMBOL(pwrap_init); */
  82. /*Interrupt handler function*/
  83. static irqreturn_t mt_pmic_wrap_irq(int irqno, void *dev_id)
  84. {
  85. unsigned long flags = 0;
  86. PWRAPFUC();
  87. PWRAPREG("dump pwrap register\n");
  88. spin_lock_irqsave(&wrp_lock, flags);
  89. /* *----------------------------------------------------------------------- */
  90. pwrap_dump_all_register();
  91. /* raise the priority of WACS2 for AP */
  92. WRAP_WR32(PMIC_WRAP_HARB_HPRIO, 1<<3);
  93. /* *----------------------------------------------------------------------- */
  94. /* clear interrupt flag */
  95. WRAP_WR32(PMIC_WRAP_INT_CLR, 0xffffffff);
  96. PWRAPREG("INT flag 0x%x\n", WRAP_RD32(PMIC_WRAP_INT_EN));
  97. /* BUG_ON(1); */
  98. spin_unlock_irqrestore(&wrp_lock, flags);
  99. return IRQ_HANDLED;
  100. }
  101. static u32 pwrap_read_test(void)
  102. {
  103. return 0;
  104. }
  105. static u32 pwrap_write_test(void)
  106. {
  107. return 0;
  108. }
  109. static void pwrap_ut(u32 ut_test)
  110. {
  111. switch (ut_test) {
  112. case 1:
  113. /* pwrap_wacs2_para_test(); */
  114. pwrap_write_test();
  115. break;
  116. case 2:
  117. /* pwrap_wacs2_para_test(); */
  118. pwrap_read_test();
  119. break;
  120. default:
  121. PWRAPREG("default test.\n");
  122. break;
  123. }
  124. }
  125. /*---------------------------------------------------------------------------*/
  126. static s32 mt_pwrap_show_hal(char *buf)
  127. {
  128. PWRAPFUC();
  129. return snprintf(buf, PAGE_SIZE, "%s\n", "no implement");
  130. }
  131. /*---------------------------------------------------------------------------*/
  132. static s32 mt_pwrap_store_hal(const char *buf, size_t count)
  133. {
  134. u32 reg_value = 0;
  135. u32 reg_addr = 0;
  136. u32 return_value = 0;
  137. u32 ut_test = 0;
  138. if (!strncmp(buf, "-h", 2)) {
  139. PWRAPREG("PWRAP debug: [-dump_reg]"
  140. \"[-trace_wacs2][-init]"
  141. \"[-rdap][-wrap][-rdpmic][-wrpmic]"
  142. \"[-readtest][-writetest]\n");
  143. PWRAPREG("PWRAP UT: [1][2]\n");
  144. }
  145. /* -------pwrap debug----------- */
  146. else if (!strncmp(buf, "-dump_reg", 9))
  147. pwrap_dump_all_register();
  148. else if (!strncmp(buf, "-trace_wacs2", 12))
  149. ;/* pwrap_trace_wacs2(); */
  150. else if (!strncmp(buf, "-init", 5)) {
  151. return_value = pwrap_init();
  152. if (return_value == 0)
  153. PWRAPREG("pwrap_init pass,return_value=%d\n", return_value);
  154. else
  155. PWRAPREG("pwrap_init fail,return_value=%d\n", return_value);
  156. } else if (!strncmp(buf, "-rdap", 5) && (1 == sscanf(buf+5, "%x", &reg_addr))) {
  157. /* pwrap_read_reg_on_ap(reg_addr); */
  158. } else if (!strncmp(buf, "-wrap", 5) && (2 == sscanf(buf+5, "%x %x", &reg_addr, &reg_value))) {
  159. /* pwrap_write_reg_on_ap(reg_addr,reg_value); */
  160. } else if (!strncmp(buf, "-rdpmic", 7) && (1 == sscanf(buf+7, "%x", &reg_addr))) {
  161. /* pwrap_read_reg_on_pmic(reg_addr); */
  162. } else if (!strncmp(buf, "-wrpmic", 7) && (2 == sscanf(buf+7, "%x %x", &reg_addr, &reg_value))) {
  163. /* pwrap_write_reg_on_pmic(reg_addr,reg_value); */
  164. } else if (!strncmp(buf, "-readtest", 9)) {
  165. pwrap_read_test();
  166. } else if (!strncmp(buf, "-writetest", 10)) {
  167. pwrap_write_test();
  168. }
  169. /* ----------------pwrap UT---------- */
  170. else if (!strncmp(buf, "-ut", 3) && (1 == sscanf(buf+3, "%d", &ut_test))) {
  171. pwrap_ut(ut_test);
  172. } else{
  173. PWRAPREG("wrong parameter\n");
  174. }
  175. return count;
  176. }
  177. /*---------------------------------------------------------------------------*/
  178. #else
  179. /*-pwrap debug--------------------------------------------------------------------------*/
  180. static inline void pwrap_dump_ap_register(void)
  181. {
  182. u32 i = 0;
  183. PWRAPREG("dump pwrap register, base=0x%p\n", PMIC_WRAP_BASE);
  184. PWRAPREG("address : 3 2 1 0 7 6 5 4 B A 9 8 F E D C\n");
  185. #if defined(CONFIG_ARCH_MT6735M)
  186. for (i = 0; i <= 0x234; i += 16) {
  187. PWRAPREG("offset 0x%.3x:0x%.8x 0x%.8x 0x%.8x 0x%.8x\n", i,
  188. WRAP_RD32(PMIC_WRAP_BASE+i+0),
  189. WRAP_RD32(PMIC_WRAP_BASE+i+4),
  190. WRAP_RD32(PMIC_WRAP_BASE+i+8),
  191. WRAP_RD32(PMIC_WRAP_BASE+i+12));
  192. }
  193. i = 0x234;
  194. PWRAPREG("offset 0x%.3x:0x%.8x 0x%.8x 0x%.8x 0x%.8x\n", i ,
  195. WRAP_RD32(PMIC_WRAP_BASE+i+0),
  196. WRAP_RD32(PMIC_WRAP_BASE+i+4),
  197. WRAP_RD32(PMIC_WRAP_BASE+i+8),
  198. WRAP_RD32(PMIC_WRAP_BASE+i+12));
  199. #else
  200. for (i = 0; i <= 0x248; i += 16) {
  201. PWRAPREG("offset 0x%.3x:0x%.8x 0x%.8x 0x%.8x 0x%.8x\n", i,
  202. WRAP_RD32(PMIC_WRAP_BASE+i+0),
  203. WRAP_RD32(PMIC_WRAP_BASE+i+4),
  204. WRAP_RD32(PMIC_WRAP_BASE+i+8),
  205. WRAP_RD32(PMIC_WRAP_BASE+i+12));
  206. }
  207. PWRAPREG("infra clock1 0x10000048 =0x%x\n", WRAP_RD32(infracfg_ao_base+0x48));
  208. PWRAPREG("infra clock2 0x10210080 =0x%x\n", WRAP_RD32(topckgen_base+0x80));
  209. #endif
  210. }
  211. static inline void pwrap_dump_pmic_register(void)
  212. {
  213. /* u32 i=0; */
  214. /* u32 reg_addr=0; */
  215. /* u32 reg_value=0; */
  216. /* */
  217. /* PWRAPREG("dump dewrap register\n"); */
  218. /* for(i=0;i<=14;i++) */
  219. /* { */
  220. /* reg_addr=(DEW_BASE+i*4); */
  221. /* reg_value=pwrap_read_nochk(reg_addr,&reg_value); */
  222. /* PWRAPREG("0x%x=0x%x\n",reg_addr,reg_value); */
  223. /* } */
  224. }
  225. static inline void pwrap_dump_all_register(void)
  226. {
  227. pwrap_dump_ap_register();
  228. pwrap_dump_pmic_register();
  229. }
  230. static void __pwrap_soft_reset(void)
  231. {
  232. PWRAPLOG("start reset wrapper\n");
  233. /* PWRAP_SOFT_RESET; */
  234. WRAP_WR32(INFRA_GLOBALCON_RST0, 0x80);
  235. PWRAPLOG("the reset register =%x\n", WRAP_RD32(INFRA_GLOBALCON_RST0));
  236. PWRAPLOG("PMIC_WRAP_STAUPD_GRPEN =0x%x,it should be equal to 0xc\n", WRAP_RD32(PMIC_WRAP_STAUPD_GRPEN));
  237. /* clear reset bit */
  238. /* PWRAP_CLEAR_SOFT_RESET_BIT; */
  239. WRAP_WR32(INFRA_GLOBALCON_RST1, 0x80);
  240. }
  241. /******************************************************************************
  242. wrapper timeout
  243. ******************************************************************************/
  244. #define PWRAP_TIMEOUT
  245. #ifdef PWRAP_TIMEOUT
  246. static u64 _pwrap_get_current_time(void)
  247. {
  248. return sched_clock(); /* /TODO: fix me */
  249. }
  250. /* u64 elapse_time=0; */
  251. static bool _pwrap_timeout_ns(u64 start_time_ns, u64 timeout_time_ns)
  252. {
  253. u64 cur_time = 0;
  254. u64 elapse_time = 0;
  255. /* get current tick */
  256. cur_time = _pwrap_get_current_time();/* ns */
  257. /* avoid timer over flow exiting in FPGA env */
  258. if (cur_time < start_time_ns) {
  259. PWRAPERR("@@@@Timer overflow! start%lld cur timer%lld\n", start_time_ns, cur_time);
  260. start_time_ns = cur_time;
  261. timeout_time_ns = 2000*1000;
  262. PWRAPERR("@@@@reset timer! start%lld setting%lld\n", start_time_ns, timeout_time_ns);
  263. }
  264. elapse_time = cur_time-start_time_ns;
  265. /* check if timeout */
  266. if (timeout_time_ns <= elapse_time) {
  267. /* timeout */
  268. PWRAPERR("@@@@Timeout: elapse time%lld,start%lld setting timer%lld\n",
  269. elapse_time, start_time_ns, timeout_time_ns);
  270. return true;
  271. }
  272. return false;
  273. }
  274. static u64 _pwrap_time2ns(u64 time_us)
  275. {
  276. return time_us*1000;
  277. }
  278. #else
  279. static u64 _pwrap_get_current_time(void)
  280. {
  281. return 0;
  282. }
  283. static bool _pwrap_timeout_ns(u64 start_time_ns, u64 elapse_time)/* ,u64 timeout_ns) */
  284. {
  285. return false;
  286. }
  287. static u64 _pwrap_time2ns(u64 time_us)
  288. {
  289. return 0;
  290. }
  291. #endif
  292. /* ##################################################################### */
  293. /* define macro and inline function (for do while loop) */
  294. /* ##################################################################### */
  295. typedef u32 (*loop_condition_fp)(u32);/* define a function pointer */
  296. static inline u32 wait_for_fsm_idle(u32 x)
  297. {
  298. return GET_WACS0_FSM(x) != WACS_FSM_IDLE;
  299. }
  300. static inline u32 wait_for_fsm_vldclr(u32 x)
  301. {
  302. return GET_WACS0_FSM(x) != WACS_FSM_WFVLDCLR;
  303. }
  304. static inline u32 wait_for_sync(u32 x)
  305. {
  306. return GET_SYNC_IDLE0(x) != WACS_SYNC_IDLE;
  307. }
  308. static inline u32 wait_for_idle_and_sync(u32 x)
  309. {
  310. return (GET_WACS2_FSM(x) != WACS_FSM_IDLE) || (GET_SYNC_IDLE2(x) != WACS_SYNC_IDLE);
  311. }
  312. static inline u32 wait_for_wrap_idle(u32 x)
  313. {
  314. return (GET_WRAP_FSM(x) != 0x0) || (GET_WRAP_CH_DLE_RESTCNT(x) != 0x0);
  315. }
  316. static inline u32 wait_for_wrap_state_idle(u32 x)
  317. {
  318. return GET_WRAP_AG_DLE_RESTCNT(x) != 0;
  319. }
  320. static inline u32 wait_for_man_idle_and_noreq(u32 x)
  321. {
  322. return (GET_MAN_REQ(x) != MAN_FSM_NO_REQ) || (GET_MAN_FSM(x) != MAN_FSM_IDLE);
  323. }
  324. static inline u32 wait_for_man_vldclr(u32 x)
  325. {
  326. return GET_MAN_FSM(x) != MAN_FSM_WFVLDCLR;
  327. }
  328. static inline u32 wait_for_cipher_ready(u32 x)
  329. {
  330. return x != 3;
  331. }
  332. static inline u32 wait_for_stdupd_idle(u32 x)
  333. {
  334. return GET_STAUPD_FSM(x) != 0x0;
  335. }
  336. static inline u32 wait_for_state_ready_init(loop_condition_fp fp, u32 timeout_us, void *wacs_register, u32 *read_reg)
  337. {
  338. u64 start_time_ns = 0, timeout_ns = 0;
  339. u32 reg_rdata = 0x0;
  340. start_time_ns = _pwrap_get_current_time();
  341. timeout_ns = _pwrap_time2ns(timeout_us);
  342. do {
  343. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  344. PWRAPERR("wait_for_state_ready_init timeout when waiting for idle\n");
  345. return E_PWR_WAIT_IDLE_TIMEOUT;
  346. }
  347. reg_rdata = WRAP_RD32(wacs_register);
  348. } while (fp(reg_rdata)); /* IDLE State */
  349. if (read_reg)
  350. *read_reg = reg_rdata;
  351. return 0;
  352. }
  353. static inline u32 wait_for_state_idle_init(loop_condition_fp fp, u32 timeout_us,
  354. void *wacs_register, void *wacs_vldclr_register, u32 *read_reg)
  355. {
  356. u64 start_time_ns = 0, timeout_ns = 0;
  357. u32 reg_rdata;
  358. start_time_ns = _pwrap_get_current_time();
  359. timeout_ns = _pwrap_time2ns(timeout_us);
  360. do {
  361. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  362. PWRAPERR("wait_for_state_idle_init timeout when waiting for idle\n");
  363. pwrap_dump_ap_register();
  364. /* pwrap_trace_wacs2(); */
  365. /* BUG_ON(1); */
  366. return E_PWR_WAIT_IDLE_TIMEOUT;
  367. }
  368. reg_rdata = WRAP_RD32(wacs_register);
  369. /* if last read command timeout,clear vldclr bit */
  370. /* read command state machine:FSM_REQ-->wfdle-->WFVLDCLR;write:FSM_REQ-->idle */
  371. switch (GET_WACS0_FSM(reg_rdata)) {
  372. case WACS_FSM_WFVLDCLR:
  373. WRAP_WR32(wacs_vldclr_register , 1);
  374. PWRAPERR("WACS_FSM = PMIC_WRAP_WACS_VLDCLR\n");
  375. break;
  376. case WACS_FSM_WFDLE:
  377. PWRAPERR("WACS_FSM = WACS_FSM_WFDLE\n");
  378. break;
  379. case WACS_FSM_REQ:
  380. PWRAPERR("WACS_FSM = WACS_FSM_REQ\n");
  381. break;
  382. default:
  383. break;
  384. }
  385. } while (fp(reg_rdata)); /* IDLE State */
  386. if (read_reg)
  387. *read_reg = reg_rdata;
  388. return 0;
  389. }
  390. static inline u32 wait_for_state_idle(loop_condition_fp fp, u32 timeout_us,
  391. void *wacs_register, void *wacs_vldclr_register, u32 *read_reg)
  392. {
  393. u64 start_time_ns = 0, timeout_ns = 0;
  394. u32 reg_rdata;
  395. start_time_ns = _pwrap_get_current_time();
  396. timeout_ns = _pwrap_time2ns(timeout_us);
  397. do {
  398. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  399. PWRAPERR("wait_for_state_idle timeout when waiting for idle\n");
  400. pwrap_dump_ap_register();
  401. /* pwrap_trace_wacs2(); */
  402. /* BUG_ON(1); */
  403. return E_PWR_WAIT_IDLE_TIMEOUT;
  404. }
  405. reg_rdata = WRAP_RD32(wacs_register);
  406. if (GET_INIT_DONE0(reg_rdata) != WACS_INIT_DONE) {
  407. PWRAPERR("initialization isn't finished\n");
  408. return E_PWR_NOT_INIT_DONE;
  409. }
  410. /* if last read command timeout,clear vldclr bit */
  411. /* read command state machine:FSM_REQ-->wfdle-->WFVLDCLR;write:FSM_REQ-->idle */
  412. switch (GET_WACS0_FSM(reg_rdata)) {
  413. case WACS_FSM_WFVLDCLR:
  414. WRAP_WR32(wacs_vldclr_register , 1);
  415. PWRAPERR("WACS_FSM = PMIC_WRAP_WACS_VLDCLR\n");
  416. break;
  417. case WACS_FSM_WFDLE:
  418. PWRAPERR("WACS_FSM = WACS_FSM_WFDLE\n");
  419. break;
  420. case WACS_FSM_REQ:
  421. PWRAPERR("WACS_FSM = WACS_FSM_REQ\n");
  422. break;
  423. default:
  424. break;
  425. }
  426. } while (fp(reg_rdata)); /* IDLE State */
  427. if (read_reg)
  428. *read_reg = reg_rdata;
  429. return 0;
  430. }
  431. /********************************************************************************************/
  432. /* extern API for PMIC driver, INT related control, this INT is for PMIC chip to AP */
  433. /********************************************************************************************/
  434. u32 mt_pmic_wrap_eint_status(void)
  435. {
  436. return WRAP_RD32(PMIC_WRAP_EINT_STA);
  437. }
  438. void mt_pmic_wrap_eint_clr(int offset)
  439. {
  440. if (offset < 0 || offset > 3)
  441. PWRAPERR("clear EINT flag error, only 0-3 bit\n");
  442. else
  443. WRAP_WR32(PMIC_WRAP_EINT_CLR, (1<<offset));
  444. PWRAPREG("clear EINT flag mt_pmic_wrap_eint_status=0x%x\n", WRAP_RD32(PMIC_WRAP_EINT_STA));
  445. }
  446. static inline u32 wait_for_state_ready(loop_condition_fp fp, u32 timeout_us, void *wacs_register, u32 *read_reg)
  447. {
  448. u64 start_time_ns = 0, timeout_ns = 0;
  449. u32 reg_rdata;
  450. start_time_ns = _pwrap_get_current_time();
  451. timeout_ns = _pwrap_time2ns(timeout_us);
  452. do {
  453. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  454. PWRAPERR("timeout when waiting for idle\n");
  455. pwrap_dump_ap_register();
  456. /* pwrap_trace_wacs2(); */
  457. return E_PWR_WAIT_IDLE_TIMEOUT;
  458. }
  459. reg_rdata = WRAP_RD32(wacs_register);
  460. if (GET_INIT_DONE0(reg_rdata) != WACS_INIT_DONE) {
  461. PWRAPERR("initialization isn't finished\n");
  462. return E_PWR_NOT_INIT_DONE;
  463. }
  464. } while (fp(reg_rdata)); /* IDLE State */
  465. if (read_reg)
  466. *read_reg = reg_rdata;
  467. return 0;
  468. }
  469. /* -------------------------------------------------------- */
  470. /* Function : pwrap_wacs2_hal() */
  471. /* Description : */
  472. /* Parameter : */
  473. /* Return : */
  474. /* -------------------------------------------------------- */
  475. static s32 pwrap_wacs2_hal(u32 write, u32 adr, u32 wdata, u32 *rdata)
  476. {
  477. /* u64 wrap_access_time=0x0; */
  478. u32 reg_rdata = 0;
  479. u32 wacs_write = 0;
  480. u32 wacs_adr = 0;
  481. u32 wacs_cmd = 0;
  482. u32 return_value = 0;
  483. unsigned long flags = 0;
  484. /* PWRAPFUC(); */
  485. /* #ifndef CONFIG_MTK_LDVT_PMIC_WRAP */
  486. /* PWRAPLOG("wrapper access,write=%x,add=%x,wdata=%x,rdata=%x\n",write,adr,wdata,rdata); */
  487. /* #endif */
  488. /* Check argument validation */
  489. if ((write & ~(0x1)) != 0)
  490. return E_PWR_INVALID_RW;
  491. if ((adr & ~(0xffff)) != 0)
  492. return E_PWR_INVALID_ADDR;
  493. if ((wdata & ~(0xffff)) != 0)
  494. return E_PWR_INVALID_WDAT;
  495. spin_lock_irqsave(&wrp_lock, flags);
  496. /* check pmicaddr 0xa bit11 & bit10 ,bit 11 only can write1 bit 10 only can write 0 request by Wy Chuang */
  497. if (0 != write && 0xa == adr) {
  498. if (0 == (wdata & (1<<11)) || 1 == (wdata & (1<<10))) {
  499. PWRAPERR(" pwrap_wacs2_hal check 0xa err pid=%d, wdata=0x%x\n", current->pid, wdata);
  500. BUG_ON(1);
  501. }
  502. }
  503. /* Check IDLE & INIT_DONE in advance */
  504. return_value = wait_for_state_idle(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE,
  505. PMIC_WRAP_WACS2_RDATA, PMIC_WRAP_WACS2_VLDCLR, 0);
  506. if (return_value != 0) {
  507. PWRAPERR("wait_for_fsm_idle fail,return_value=%d\n", return_value);
  508. goto FAIL;
  509. }
  510. wacs_write = write << 31;
  511. wacs_adr = (adr >> 1) << 16;
  512. wacs_cmd = wacs_write | wacs_adr | wdata;
  513. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  514. if (write == 0) {
  515. if (NULL == rdata) {
  516. PWRAPERR("rdata is a NULL pointer\n");
  517. return_value = E_PWR_INVALID_ARG;
  518. goto FAIL;
  519. }
  520. return_value = wait_for_state_ready(wait_for_fsm_vldclr,
  521. TIMEOUT_READ, PMIC_WRAP_WACS2_RDATA, &reg_rdata);
  522. if (return_value != 0) {
  523. PWRAPERR("wait_for_fsm_vldclr fail,return_value=%d\n", return_value);
  524. return_value += 1;
  525. goto FAIL;
  526. }
  527. *rdata = GET_WACS0_RDATA(reg_rdata);
  528. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR , 1);
  529. }
  530. /* spin_unlock_irqrestore(&wrp_lock,flags); */
  531. FAIL:
  532. spin_unlock_irqrestore(&wrp_lock, flags);
  533. if (return_value != 0) {
  534. PWRAPERR("pwrap_wacs2_hal fail,return_value=%d\n", return_value);
  535. PWRAPERR("timeout:BUG_ON here\n");
  536. /* BUG_ON(1); */
  537. }
  538. /* wrap_access_time=sched_clock(); */
  539. /* pwrap_trace(wrap_access_time,return_value,write, adr, wdata,(u32)rdata); */
  540. return return_value;
  541. }
  542. /* s32 pwrap_wacs2( u32 write, u32 adr, u32 wdata, u32 *rdata ) */
  543. /* { */
  544. /* return pwrap_wacs2_hal(write, adr,wdata,rdata ); */
  545. /* } */
  546. /* EXPORT_SYMBOL(pwrap_wacs2); */
  547. /* s32 pwrap_read( u32 adr, u32 *rdata ) */
  548. /* { */
  549. /* return pwrap_wacs2( PWRAP_READ, adr,0,rdata ); */
  550. /* } */
  551. /* EXPORT_SYMBOL(pwrap_read); */
  552. /* */
  553. /* s32 pwrap_write( u32 adr, u32 wdata ) */
  554. /* { */
  555. /* return pwrap_wacs2( PWRAP_WRITE, adr,wdata,0 ); */
  556. /* } */
  557. /* EXPORT_SYMBOL(pwrap_write); */
  558. /* ****************************************************************************** */
  559. /* --internal API for pwrap_init------------------------------------------------- */
  560. /* ****************************************************************************** */
  561. /* -------------------------------------------------------- */
  562. /* Function : _pwrap_wacs2_nochk() */
  563. /* Description : */
  564. /* Parameter : */
  565. /* Return : */
  566. /* -------------------------------------------------------- */
  567. /* static s32 pwrap_read_nochk( u32 adr, u32 *rdata ) */
  568. s32 pwrap_read_nochk(u32 adr, u32 *rdata)
  569. {
  570. return _pwrap_wacs2_nochk(0, adr, 0, rdata);
  571. }
  572. /*EXPORT_SYMBOL(pwrap_read_nochk);*/
  573. s32 pwrap_write_nochk(u32 adr, u32 wdata)
  574. {
  575. return _pwrap_wacs2_nochk(1, adr, wdata, 0);
  576. }
  577. /*EXPORT_SYMBOL(pwrap_write_nochk);*/
  578. static s32 _pwrap_wacs2_nochk(u32 write, u32 adr, u32 wdata, u32 *rdata)
  579. {
  580. u32 reg_rdata = 0x0;
  581. u32 wacs_write = 0x0;
  582. u32 wacs_adr = 0x0;
  583. u32 wacs_cmd = 0x0;
  584. u32 return_value = 0x0;
  585. /* PWRAPFUC(); */
  586. /* Check argument validation */
  587. if (0 != write && 0xa == adr) {
  588. if (0 == (wdata & (1<<11)) || 1 == (wdata & (1<<10))) {
  589. PWRAPERR("_pwrap_wacs2_nochk check 0xa err pid=%d, wdata=0x%x\n", current->pid, wdata);
  590. BUG_ON(1);
  591. }
  592. }
  593. if ((write & ~(0x1)) != 0)
  594. return E_PWR_INVALID_RW;
  595. if ((adr & ~(0xffff)) != 0)
  596. return E_PWR_INVALID_ADDR;
  597. if ((wdata & ~(0xffff)) != 0)
  598. return E_PWR_INVALID_WDAT;
  599. /* Check IDLE */
  600. return_value = wait_for_state_ready_init(wait_for_fsm_idle,
  601. TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  602. if (return_value != 0) {
  603. PWRAPERR("_pwrap_wacs2_nochk write command fail,return_value=%x\n", return_value);
  604. return return_value;
  605. }
  606. wacs_write = write << 31;
  607. wacs_adr = (adr >> 1) << 16;
  608. wacs_cmd = wacs_write | wacs_adr | wdata;
  609. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  610. if (write == 0) {
  611. if (NULL == rdata)
  612. return E_PWR_INVALID_ARG;
  613. /* wait for read data ready */
  614. return_value = wait_for_state_ready_init(wait_for_fsm_vldclr,
  615. TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, &reg_rdata);
  616. if (return_value != 0) {
  617. PWRAPERR("_pwrap_wacs2_nochk read fail,return_value=%x\n", return_value);
  618. return return_value;
  619. }
  620. *rdata = GET_WACS0_RDATA(reg_rdata);
  621. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR , 1);
  622. }
  623. return 0;
  624. }
  625. /* -------------------------------------------------------- */
  626. /* Function : _pwrap_init_dio() */
  627. /* Description :call it in pwrap_init,mustn't check init done */
  628. /* Parameter : */
  629. /* Return : */
  630. /* -------------------------------------------------------- */
  631. static s32 _pwrap_init_dio(u32 dio_en)
  632. {
  633. u32 arb_en_backup = 0x0;
  634. u32 rdata = 0x0;
  635. u32 return_value = 0;
  636. /* PWRAPFUC(); */
  637. arb_en_backup = WRAP_RD32(PMIC_WRAP_HIPRIO_ARB_EN);
  638. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN , WACS2); /* only WACS2 */
  639. #ifdef SLV_6328
  640. pwrap_write_nochk(MT6328_DEW_DIO_EN, (dio_en));
  641. #endif
  642. #ifdef SLV_6332
  643. pwrap_write_nochk(MT6332_DEW_DIO_EN, (dio_en>>1));
  644. #endif
  645. /* Check IDLE & INIT_DONE in advance */
  646. return_value = wait_for_state_ready_init(wait_for_idle_and_sync,
  647. TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  648. if (return_value != 0) {
  649. PWRAPERR("_pwrap_init_dio fail,return_value=%x\n", return_value);
  650. return return_value;
  651. }
  652. /* enable AP DIO mode */
  653. WRAP_WR32(PMIC_WRAP_DIO_EN , dio_en);
  654. /* Read Test */
  655. #ifdef SLV_6328
  656. pwrap_read_nochk(MT6328_DEW_READ_TEST, &rdata);
  657. if (rdata != MT6328_DEFAULT_VALUE_READ_TEST) {
  658. PWRAPERR("[Dio_mode][Read Test] fail,dio_en = %x", dio_en);
  659. PWRAPERR("READ_TEST rdata=%x, exp=0x5aa5\n", rdata);
  660. return E_PWR_READ_TEST_FAIL;
  661. }
  662. #endif
  663. #ifdef SLV_6332
  664. pwrap_read_nochk(MT6332_DEW_READ_TEST, &rdata);
  665. if (rdata != MT6332_DEFAULT_VALUE_READ_TEST) {
  666. PWRAPERR("[Dio_mode][Read Test] fail,dio_en = %x, READ_TEST rdata=%x, exp=0xa55a\n", dio_en, rdata);
  667. return E_PWR_READ_TEST_FAIL;
  668. }
  669. #endif
  670. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN , arb_en_backup);
  671. return 0;
  672. }
  673. /* -------------------------------------------------------- */
  674. /* Function : _pwrap_init_cipher() */
  675. /* Description : */
  676. /* Parameter : */
  677. /* Return : */
  678. /* -------------------------------------------------------- */
  679. static s32 _pwrap_init_cipher(void)
  680. {
  681. u32 arb_en_backup = 0;
  682. u32 rdata = 0;
  683. u32 return_value = 0;
  684. u32 start_time_ns = 0, timeout_ns = 0;
  685. /* PWRAPFUC(); */
  686. arb_en_backup = WRAP_RD32(PMIC_WRAP_HIPRIO_ARB_EN);
  687. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN , WACS2);
  688. WRAP_WR32(PMIC_WRAP_CIPHER_SWRST , 1);
  689. WRAP_WR32(PMIC_WRAP_CIPHER_SWRST , 0);
  690. WRAP_WR32(PMIC_WRAP_CIPHER_KEY_SEL , 1);
  691. WRAP_WR32(PMIC_WRAP_CIPHER_IV_SEL , 2);
  692. WRAP_WR32(PMIC_WRAP_CIPHER_EN , 1);
  693. /* Config CIPHER @ PMIC */
  694. #ifdef SLV_6328
  695. pwrap_write_nochk(MT6328_DEW_CIPHER_SWRST, 0x1);
  696. pwrap_write_nochk(MT6328_DEW_CIPHER_SWRST, 0x0);
  697. pwrap_write_nochk(MT6328_DEW_CIPHER_KEY_SEL, 0x1);
  698. pwrap_write_nochk(MT6328_DEW_CIPHER_IV_SEL, 0x2);
  699. pwrap_write_nochk(MT6328_DEW_CIPHER_EN, 0x1);
  700. #endif
  701. #ifdef SLV_6332
  702. pwrap_write_nochk(MT6332_DEW_CIPHER_SWRST, 0x1);
  703. pwrap_write_nochk(MT6332_DEW_CIPHER_SWRST, 0x0);
  704. pwrap_write_nochk(MT6332_DEW_CIPHER_KEY_SEL, 0x1);
  705. pwrap_write_nochk(MT6332_DEW_CIPHER_IV_SEL, 0x2);
  706. pwrap_write_nochk(MT6332_DEW_CIPHER_EN, 0x1);
  707. #endif
  708. PWRAPLOG("mt_pwrap_init---- debug7\n");
  709. /* wait for cipher data ready@AP */
  710. return_value = wait_for_state_ready_init(wait_for_cipher_ready, TIMEOUT_WAIT_IDLE, PMIC_WRAP_CIPHER_RDY, 0);
  711. if (return_value != 0) {
  712. PWRAPERR("wait for cipher data ready@AP fail,return_value=%x\n", return_value);
  713. return return_value;
  714. }
  715. PWRAPLOG("mt_pwrap_init---- debug8\n");
  716. /* wait for cipher data ready@PMIC */
  717. #ifdef SLV_6328
  718. start_time_ns = _pwrap_get_current_time();
  719. timeout_ns = _pwrap_time2ns(0xFFFFFF);
  720. do {
  721. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  722. PWRAPERR("wait for cipher data ready@PMIC\n");
  723. /* pwrap_dump_all_register(); */
  724. /* return E_PWR_WAIT_IDLE_TIMEOUT; */
  725. }
  726. pwrap_read_nochk(MT6328_DEW_CIPHER_RDY, &rdata);
  727. } while (rdata != 0x1); /* cipher_ready */
  728. pwrap_write_nochk(MT6328_DEW_CIPHER_MODE, 0x1);
  729. PWRAPLOG("mt_pwrap_init---- debug9\n");
  730. #endif
  731. #ifdef SLV_6332
  732. start_time_ns = _pwrap_get_current_time();
  733. timeout_ns = _pwrap_time2ns(0xFFFFFF);
  734. do {
  735. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  736. PWRAPERR("wait for cipher data ready@PMIC\n");
  737. /* pwrap_dump_all_register(); */
  738. /* return E_PWR_WAIT_IDLE_TIMEOUT; */
  739. }
  740. pwrap_read_nochk(MT6332_DEW_CIPHER_RDY, &rdata);
  741. } while (rdata != 0x1); /* cipher_ready */
  742. pwrap_write_nochk(MT6332_DEW_CIPHER_MODE, 0x1);
  743. #endif
  744. /* wait for cipher mode idle */
  745. return_value = wait_for_state_ready_init(wait_for_idle_and_sync, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  746. if (return_value != 0) {
  747. PWRAPERR("wait for cipher mode idle fail,return_value=%x\n", return_value);
  748. return return_value;
  749. }
  750. WRAP_WR32(PMIC_WRAP_CIPHER_MODE , 1);
  751. /* Read Test */
  752. #ifdef SLV_6328
  753. pwrap_read_nochk(MT6328_DEW_READ_TEST, &rdata);
  754. if (rdata != MT6328_DEFAULT_VALUE_READ_TEST) {
  755. PWRAPERR("_pwrap_init_cipher,read test error,error code=%x, rdata=%x\n", 1, rdata);
  756. return E_PWR_READ_TEST_FAIL;
  757. }
  758. #endif
  759. #ifdef SLV_6332
  760. pwrap_read_nochk(MT6332_DEW_READ_TEST, &rdata);
  761. if (rdata != MT6332_DEFAULT_VALUE_READ_TEST) {
  762. PWRAPERR("_pwrap_init_cipher,read test error,error code=%x, rdata=%x\n", 1, rdata);
  763. return E_PWR_READ_TEST_FAIL;
  764. }
  765. #endif
  766. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN , arb_en_backup);
  767. return 0;
  768. }
  769. /* -------------------------------------------------------- */
  770. /* Function : _pwrap_init_sistrobe() */
  771. /* Description : Initialize SI_CK_CON and SIDLY */
  772. /* Parameter : */
  773. /* Return : */
  774. /* -------------------------------------------------------- */
  775. static s32 _pwrap_init_sistrobe(void)
  776. {
  777. u32 arb_en_backup = 0;
  778. u32 rdata = 0;
  779. u32 i = 0;
  780. s32 ind = 0;
  781. u32 tmp1 = 0;
  782. u32 tmp2 = 0;
  783. u32 result_faulty = 0;
  784. u32 result[2] = {0, 0};
  785. s32 leading_one[2] = {-1, -1};
  786. s32 tailing_one[2] = {-1, -1};
  787. arb_en_backup = WRAP_RD32(PMIC_WRAP_HIPRIO_ARB_EN);
  788. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN , WACS2);
  789. /* --------------------------------------------------------------------- */
  790. /* Scan all possible input strobe by READ_TEST */
  791. /* --------------------------------------------------------------------- */
  792. for (ind = 0; ind < 24; ind++) {
  793. WRAP_WR32(PMIC_WRAP_SI_CK_CON , (ind >> 2) & 0x7);
  794. WRAP_WR32(PMIC_WRAP_SIDLY , 0x3 - (ind & 0x3));
  795. #ifdef SLV_6328
  796. _pwrap_wacs2_nochk(0, MT6328_DEW_READ_TEST, 0, &rdata);
  797. if (rdata == MT6328_DEFAULT_VALUE_READ_TEST) {
  798. PWRAPLOG("_pwrap_init_sistrobe [Read Test of MT6328] pass");
  799. PWRAPLOG("index=%d rdata=%x\n", ind, rdata);
  800. result[0] |= (0x1 << ind);
  801. } else{
  802. PWRAPLOG("_pwrap_init_sistrobe [Read Test of MT6328]");
  803. PWRAPLOG("tuning,index=%d rdata=%x\n", ind, rdata);
  804. }
  805. #endif
  806. #ifdef SLV_6332
  807. _pwrap_wacs2_nochk(0, MT6332_DEW_READ_TEST, 0, &rdata);
  808. if (rdata == MT6332_DEFAULT_VALUE_READ_TEST) {
  809. PWRAPLOG("_pwrap_init_sistrobe [Read Test of MT6332] pass");
  810. PWRAPLOG("index=%d rdata=%x\n", ind, rdata);
  811. result[1] |= (0x1 << ind);
  812. } else {
  813. PWRAPLOG("_pwrap_init_sistrobe");
  814. PWRAPLOG("[Read Test of MT6332] tuning,index=%d rdata=%x\n", ind, rdata);
  815. }
  816. #endif
  817. }
  818. #ifndef SLV_6328
  819. result[0] = result[1];
  820. #endif
  821. #ifndef SLV_6332
  822. result[1] = result[0];
  823. #endif
  824. /* --------------------------------------------------------------------- */
  825. /* Locate the leading one and trailing one of PMIC 1/2 */
  826. /* --------------------------------------------------------------------- */
  827. for (ind = 23; ind >= 0; ind--) {
  828. if ((result[0] & (0x1 << ind)) && leading_one[0] == -1)
  829. leading_one[0] = ind;
  830. if (leading_one[0] > 0)
  831. break;
  832. }
  833. for (ind = 23; ind >= 0; ind--) {
  834. if ((result[1] & (0x1 << ind)) && leading_one[1] == -1)
  835. leading_one[1] = ind;
  836. if (leading_one[1] > 0)
  837. break;
  838. }
  839. for (ind = 0; ind < 24; ind++) {
  840. if ((result[0] & (0x1 << ind)) && tailing_one[0] == -1)
  841. tailing_one[0] = ind;
  842. if (tailing_one[0] > 0)
  843. break;
  844. }
  845. for (ind = 0; ind < 24; ind++) {
  846. if ((result[1] & (0x1 << ind)) && tailing_one[1] == -1)
  847. tailing_one[1] = ind;
  848. if (tailing_one[1] > 0)
  849. break;
  850. }
  851. /* --------------------------------------------------------------------- */
  852. /* Check the continuity of pass range */
  853. /* --------------------------------------------------------------------- */
  854. for (i = 0; i < 2; i++) {
  855. tmp1 = (0x1 << (leading_one[i]+1)) - 1;
  856. tmp2 = (0x1 << tailing_one[i]) - 1;
  857. if ((tmp1 - tmp2) != result[i]) {
  858. PWRAPERR("_pwrap_init_sistrobe Fail at PMIC %d, result = %x", i+1, result[i]);
  859. PWRAPERR("leading_one:%d, tailing_one:%d\n", leading_one[i], tailing_one[i]);
  860. result_faulty = 0x1;
  861. }
  862. }
  863. /* --------------------------------------------------------------------- */
  864. /* Config SICK and SIDLY to the middle point of pass range */
  865. /* --------------------------------------------------------------------- */
  866. if (result_faulty == 0) {
  867. /* choose the best point in the interaction of PMIC1's pass range and PMIC2's pass range */
  868. ind = ((leading_one[0] + tailing_one[0])/2 + (leading_one[1] + tailing_one[1])/2)/2;
  869. /*TINFO = "The best point in the interaction area is %d, ind"*/
  870. WRAP_WR32(PMIC_WRAP_SI_CK_CON , (ind >> 2) & 0x7);
  871. WRAP_WR32(PMIC_WRAP_SIDLY , 0x3 - (ind & 0x3));
  872. /* --------------------------------------------------------------------- */
  873. /* Restore */
  874. /* --------------------------------------------------------------------- */
  875. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN , arb_en_backup);
  876. return 0;
  877. }
  878. return 0;
  879. }
  880. /* -------------------------------------------------------- */
  881. /* Function : _pwrap_reset_spislv() */
  882. /* Description : */
  883. /* Parameter : */
  884. /* Return : */
  885. /* -------------------------------------------------------- */
  886. static s32 _pwrap_reset_spislv(void)
  887. {
  888. u32 ret = 0;
  889. u32 return_value = 0;
  890. /* PWRAPFUC(); */
  891. /* This driver does not using _pwrap_switch_mux */
  892. /* because the remaining requests are expected to fail anyway */
  893. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN , DISABLE_ALL);
  894. WRAP_WR32(PMIC_WRAP_WRAP_EN , DISABLE);
  895. WRAP_WR32(PMIC_WRAP_MUX_SEL , MANUAL_MODE);
  896. WRAP_WR32(PMIC_WRAP_MAN_EN , ENABLE);
  897. WRAP_WR32(PMIC_WRAP_DIO_EN , DISABLE);
  898. WRAP_WR32(PMIC_WRAP_MAN_CMD , (OP_WR << 13) | (OP_CSL << 8));/* 0x2100 */
  899. WRAP_WR32(PMIC_WRAP_MAN_CMD , (OP_WR << 13) | (OP_OUTS << 8)); /* 0x2800//to reset counter */
  900. WRAP_WR32(PMIC_WRAP_MAN_CMD , (OP_WR << 13) | (OP_CSH << 8));/* 0x2000 */
  901. WRAP_WR32(PMIC_WRAP_MAN_CMD , (OP_WR << 13) | (OP_OUTS << 8));
  902. WRAP_WR32(PMIC_WRAP_MAN_CMD , (OP_WR << 13) | (OP_OUTS << 8));
  903. WRAP_WR32(PMIC_WRAP_MAN_CMD , (OP_WR << 13) | (OP_OUTS << 8));
  904. WRAP_WR32(PMIC_WRAP_MAN_CMD , (OP_WR << 13) | (OP_OUTS << 8));
  905. return_value = wait_for_state_ready_init(wait_for_sync, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  906. if (return_value != 0) {
  907. PWRAPERR("_pwrap_reset_spislv fail,return_value=%x\n", return_value);
  908. ret = E_PWR_TIMEOUT;
  909. goto timeout;
  910. }
  911. WRAP_WR32(PMIC_WRAP_MAN_EN , DISABLE);
  912. WRAP_WR32(PMIC_WRAP_MUX_SEL , WRAPPER_MODE);
  913. timeout:
  914. WRAP_WR32(PMIC_WRAP_MAN_EN , DISABLE);
  915. WRAP_WR32(PMIC_WRAP_MUX_SEL , WRAPPER_MODE);
  916. return ret;
  917. }
  918. static s32 _pwrap_init_reg_clock(u32 regck_sel)
  919. {
  920. /* u32 wdata=0; */
  921. /* u32 rdata=0; */
  922. PWRAPFUC();
  923. /* Set Dummy cycle 6328 and 6332 (assume 12MHz) */
  924. #ifdef SLV_6328
  925. pwrap_write_nochk(MT6328_DEW_RDDMY_NO, 0x8);
  926. #endif
  927. #ifdef SLV_6332
  928. pwrap_write_nochk(MT6332_DEW_RDDMY_NO, 0x8);
  929. #endif
  930. WRAP_WR32(PMIC_WRAP_RDDMY , 0x88);
  931. /* Config SPI Waveform according to reg clk */
  932. if (regck_sel == 1) { /* 6MHz in 6323 => no support ; 18MHz in 6320 */
  933. /* for 6320, slave need enough time (4T of PMIC reg_ck) to back idle state */
  934. WRAP_WR32(PMIC_WRAP_CSHEXT_READ , 0x0);
  935. /* wait data written into register => 3T_PMIC: consists of CSLEXT_END(1T) + CSHEXT(6T) */
  936. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE , 0x6);
  937. WRAP_WR32(PMIC_WRAP_CSLEXT_START , 0x0);
  938. WRAP_WR32(PMIC_WRAP_CSLEXT_END , 0x0);
  939. } else { /* Safe mode */
  940. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE , 0xff);
  941. WRAP_WR32(PMIC_WRAP_CSHEXT_READ , 0xff);
  942. WRAP_WR32(PMIC_WRAP_CSLEXT_START , 0xf);
  943. WRAP_WR32(PMIC_WRAP_CSLEXT_END , 0xf);
  944. }
  945. return 0;
  946. }
  947. /* -------------------------------------------------------- */
  948. /* Function : DrvPWRAP_Switch_Strobe_Setting() */
  949. /* Description : used to switch input data calibration setting before system sleep or after system wakeup */
  950. /* no use since SPI_CK (26MHz) is always kept unchanged */
  951. /* Parameter : */
  952. /* Return : */
  953. /* -------------------------------------------------------- */
  954. /* void DrvPWRAP_Switch_Strobe_Setting (int si_ck_con, int sidly) */
  955. /* { */
  956. /* int reg_rdata; */
  957. /* // turn off spi_wrap */
  958. /* *PMIC_WRAP_WRAP_EN = 0; */
  959. /* // wait for WRAP to be in idle state */
  960. /* // and no remaining rdata to be received */
  961. /* do */
  962. /* { */
  963. /* reg_rdata = *PMIC_WRAP_WRAP_STA; */
  964. /* } while ( (GET_WRAP_FSM(reg_rdata) != 0) || */
  965. /* (GET_WRAP_CH_DLE_RESTCNT(reg_rdata)) != 0 ); */
  966. /* */
  967. /* *PMIC_WRAP_SI_CK_CON = si_ck_con; */
  968. /* *PMIC_WRAP_SIDLY = sidly; */
  969. /* */
  970. /* // turn on spi_wrap */
  971. /* *PMIC_WRAP_WRAP_EN = 1; */
  972. /* } */
  973. #if 0
  974. static s32 _pwrap_init_signature(U8 path)
  975. {
  976. int ret;
  977. u32 rdata = 0x0;
  978. PWRAPFUC();
  979. if (path == 1) {
  980. /* ############################### */
  981. /* Signature Checking - Using Write Test Register */
  982. /* should be the last to modify WRITE_TEST */
  983. /* ############################### */
  984. _pwrap_wacs2_nochk(1, MT6328_DEW_WRITE_TEST, 0x5678, &rdata);
  985. WRAP_WR32(PMIC_WRAP_SIG_ADR, MT6328_DEW_WRITE_TEST);
  986. WRAP_WR32(PMIC_WRAP_SIG_VALUE, 0x5678);
  987. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x1);
  988. } else{
  989. /* ############################### */
  990. /* Signature Checking using CRC and EINT update */
  991. /* should be the last to modify WRITE_TEST */
  992. /* ############################### */
  993. #ifdef SLV_6328
  994. ret = pwrap_write_nochk(MT6328_DEW_CRC_EN, ENABLE);
  995. if (ret != 0) {
  996. PWRAPERR("MT6328 enable CRC fail,ret=%x\n", ret);
  997. return E_PWR_INIT_ENABLE_CRC;
  998. }
  999. WRAP_WR32(PMIC_WRAP_SIG_ADR, WRAP_RD32(PMIC_WRAP_SIG_ADR)|MT6328_DEW_CRC_VAL);
  1000. /* WRAP_WR32(PMIC_WRAP_EINT_STA0_ADR,MT6328_INT_STA); */
  1001. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, WRAP_RD32(PMIC_WRAP_STAUPD_GRPEN)|0x35);
  1002. #endif
  1003. #ifdef SLV_6332
  1004. ret = pwrap_write_nochk(MT6332_DEW_CRC_EN, ENABLE);
  1005. if (ret != 0) {
  1006. PWRAPERR("MT6332 enable CRC fail,ret=%x\n", ret);
  1007. return E_PWR_INIT_ENABLE_CRC;
  1008. }
  1009. WRAP_WR32(PMIC_WRAP_SIG_ADR, WRAP_RD32(PMIC_WRAP_SIG_ADR)|(MT6332_DEW_CRC_VAL<<16));
  1010. WRAP_WR32(PMIC_WRAP_EINT_STA1_ADR, MT6332_INT_STA);
  1011. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, WRAP_RD32(PMIC_WRAP_STAUPD_GRPEN)|0xa);
  1012. #endif
  1013. }
  1014. WRAP_WR32(PMIC_WRAP_CRC_EN, ENABLE);
  1015. return 0;
  1016. }
  1017. #endif
  1018. /*
  1019. *pmic_wrap init,init wrap interface
  1020. *
  1021. */
  1022. s32 pwrap_init(void)
  1023. {
  1024. s32 sub_return = 0;
  1025. s32 sub_return1 = 0;
  1026. /* s32 ret=0; */
  1027. u32 rdata = 0x0;
  1028. /* u32 regValue; */
  1029. /* u32 timeout=0; */
  1030. /* u32 cg_mask = 0; */
  1031. /* u32 backup = 0; */
  1032. PWRAPFUC();
  1033. #ifdef CONFIG_OF
  1034. sub_return = pwrap_of_iomap();
  1035. if (sub_return)
  1036. return sub_return;
  1037. #endif
  1038. PWRAPLOG("mt_pwrap_init---- debug2\n");
  1039. /* ############################### */
  1040. /* toggle PMIC_WRAP and pwrap_spictl reset */
  1041. /* ############################### */
  1042. /* WRAP_SET_BIT(0x80,INFRA_GLOBALCON_RST0); */
  1043. /* WRAP_CLR_BIT(0x80,INFRA_GLOBALCON_RST0); */
  1044. __pwrap_soft_reset();
  1045. PWRAPLOG("mt_pwrap_init---- debug3\n");
  1046. /* ############################### */
  1047. /* Set SPI_CK_freq = 26MHz */
  1048. /* ############################### */
  1049. WRAP_WR32(CLK_CFG_4_CLR, CLK_SPI_CK_26M);
  1050. /* ############################### */
  1051. /* toggle PERI_PWRAP_BRIDGE reset */
  1052. /* ############################### */
  1053. /* WRAP_SET_BIT(0x04,PERI_GLOBALCON_RST1); */
  1054. /* WRAP_CLR_BIT(0x04,PERI_GLOBALCON_RST1); */
  1055. /* ############################### */
  1056. /* Enable DCM */
  1057. /* ############################### */
  1058. /* WRAP_WR32(PMIC_WRAP_DCM_EN , ENABLE); */
  1059. WRAP_WR32(PMIC_WRAP_DCM_EN , 3);/* enable CRC DCM and Pwrap DCM */
  1060. WRAP_WR32(PMIC_WRAP_DCM_DBC_PRD , DISABLE);
  1061. PWRAPLOG("mt_pwrap_init---- debug4\n");
  1062. /* ############################### */
  1063. /* Reset SPISLV */
  1064. /* ############################### */
  1065. sub_return = _pwrap_reset_spislv();
  1066. if (sub_return != 0) {
  1067. PWRAPERR("error,_pwrap_reset_spislv fail,sub_return=%x\n", sub_return);
  1068. return E_PWR_INIT_RESET_SPI;
  1069. }
  1070. /* ############################### */
  1071. /* Enable WACS2 */
  1072. /* ############################### */
  1073. WRAP_WR32(PMIC_WRAP_WRAP_EN, ENABLE);
  1074. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, WACS2);
  1075. WRAP_WR32(PMIC_WRAP_WACS2_EN, ENABLE);
  1076. PWRAPLOG("mt_pwrap_init---- debug5\n");
  1077. /* ############################### */
  1078. /* Input data calibration flow; */
  1079. /* ############################### */
  1080. sub_return = _pwrap_init_sistrobe();
  1081. if (sub_return != 0) {
  1082. PWRAPERR("error,DrvPWRAP_InitSiStrobe fail,sub_return=%x\n", sub_return);
  1083. return E_PWR_INIT_SIDLY;
  1084. }
  1085. PWRAPLOG("mt_pwrap_init---- debug6\n");
  1086. /* ############################### */
  1087. /* SPI Waveform Configuration */
  1088. /* ############################### */
  1089. /* 0:safe mode, 1:18MHz */
  1090. sub_return = _pwrap_init_reg_clock(1);
  1091. if (sub_return != 0) {
  1092. PWRAPERR("error,_pwrap_init_reg_clock fail,sub_return=%x\n", sub_return);
  1093. return E_PWR_INIT_REG_CLOCK;
  1094. }
  1095. /* ############################### */
  1096. /* Enable DIO mode */
  1097. /* ############################### */
  1098. /* PMIC2 dual io not ready */
  1099. /* /TODO: Fix me //for early porting */
  1100. #if 1
  1101. sub_return = _pwrap_init_dio(1);
  1102. if (sub_return != 0) {
  1103. PWRAPERR("_pwrap_init_dio test error,error code=%x, sub_return=%x\n", 0x11, sub_return);
  1104. return E_PWR_INIT_DIO;
  1105. }
  1106. #endif
  1107. /* ############################### */
  1108. /* Enable Encryption */
  1109. /* ############################### */
  1110. PWRAPLOG("have cipher\n");
  1111. sub_return = _pwrap_init_cipher();
  1112. if (sub_return != 0) {
  1113. PWRAPERR("Enable Encryption fail, return=%x\n", sub_return);
  1114. return E_PWR_INIT_CIPHER;
  1115. }
  1116. /* ############################### */
  1117. /* Write test using WACS2 */
  1118. /* ############################### */
  1119. /* check Wtiet test default value */
  1120. #ifdef SLV_6328
  1121. sub_return = pwrap_write_nochk(MT6328_DEW_WRITE_TEST, MT6328_WRITE_TEST_VALUE);
  1122. sub_return1 = pwrap_read_nochk(MT6328_DEW_WRITE_TEST, &rdata);
  1123. if (rdata != MT6328_WRITE_TEST_VALUE) {
  1124. PWRAPERR("write test error,rdata=0x%x,exp=0xa55a", rdata);
  1125. PWRAPERR("sub_return=0x%x,sub_return1=0x%x\n", sub_return, sub_return1);
  1126. return E_PWR_INIT_WRITE_TEST;
  1127. }
  1128. #endif
  1129. #ifdef SLV_6332
  1130. sub_return = pwrap_write_nochk(MT6332_DEW_WRITE_TEST, MT6332_WRITE_TEST_VALUE);
  1131. sub_return1 = pwrap_read_nochk(MT6332_DEW_WRITE_TEST, &rdata);
  1132. if (rdata != MT6332_WRITE_TEST_VALUE) {
  1133. PWRAPERR("write test error,rdata=0x%x,exp=0xa55a", rdata);
  1134. PWRAPERR("sub_return=0x%x,sub_return1=0x%x\n", sub_return, sub_return1);
  1135. return E_PWR_INIT_WRITE_TEST;
  1136. }
  1137. #endif
  1138. /* ############################### */
  1139. /* Signature Checking - Using CRC */
  1140. /* should be the last to modify WRITE_TEST */
  1141. /* ############################### */
  1142. PWRAPLOG("mt_pwrap_init---- debug10\n");
  1143. /* /TODO: Fix me //for early porting really no need? */
  1144. #if 0
  1145. sub_return = _pwrap_init_signature(0);
  1146. if (sub_return != 0) {
  1147. PWRAPERR("Enable CRC fail, return=%x\n", sub_return);
  1148. return E_PWR_INIT_ENABLE_CRC;
  1149. }
  1150. #endif
  1151. #if 1
  1152. /* ADC init */
  1153. WRAP_WR32(PMIC_WRAP_ADC_CMD_ADDR, MT6328_AUXADC_RQST1_SET);
  1154. WRAP_WR32(PMIC_WRAP_PWRAP_ADC_CMD, 0x0100);
  1155. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR_LATEST , MT6328_AUXADC_ADC32);
  1156. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR_WP , MT6328_AUXADC_MDBG_1);
  1157. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR0 , MT6328_AUXADC_BUF0);
  1158. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR1 , MT6328_AUXADC_BUF1);
  1159. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR2 , MT6328_AUXADC_BUF2);
  1160. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR3 , MT6328_AUXADC_BUF3);
  1161. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR4 , MT6328_AUXADC_BUF4);
  1162. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR5 , MT6328_AUXADC_BUF5);
  1163. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR6 , MT6328_AUXADC_BUF6);
  1164. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR7 , MT6328_AUXADC_BUF7);
  1165. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR8 , MT6328_AUXADC_BUF8);
  1166. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR9 , MT6328_AUXADC_BUF9);
  1167. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR10 , MT6328_AUXADC_BUF10);
  1168. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR11 , MT6328_AUXADC_BUF11);
  1169. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR12 , MT6328_AUXADC_BUF12);
  1170. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR13 , MT6328_AUXADC_BUF13);
  1171. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR14 , MT6328_AUXADC_BUF14);
  1172. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR15 , MT6328_AUXADC_BUF15);
  1173. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR16 , MT6328_AUXADC_BUF16);
  1174. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR17 , MT6328_AUXADC_BUF17);
  1175. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR18 , MT6328_AUXADC_BUF18);
  1176. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR19 , MT6328_AUXADC_BUF19);
  1177. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR20 , MT6328_AUXADC_BUF20);
  1178. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR21 , MT6328_AUXADC_BUF21);
  1179. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR22 , MT6328_AUXADC_BUF22);
  1180. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR23 , MT6328_AUXADC_BUF23);
  1181. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR24 , MT6328_AUXADC_BUF24);
  1182. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR25 , MT6328_AUXADC_BUF25);
  1183. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR26 , MT6328_AUXADC_BUF26);
  1184. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR27 , MT6328_AUXADC_BUF27);
  1185. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR28 , MT6328_AUXADC_BUF28);
  1186. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR29 , MT6328_AUXADC_BUF29);
  1187. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR30 , MT6328_AUXADC_BUF30);
  1188. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR31 , MT6328_AUXADC_BUF31);
  1189. #endif
  1190. /* adc 6328 setting */
  1191. pwrap_write_nochk(MT6328_AUXADC_MDBG_0, (0x40|(1<<15)));
  1192. pwrap_write_nochk(MT6328_AUXADC_MDRT_0, (0x40|(1<<15)));
  1193. pwrap_write_nochk(MT6328_AUXADC_MDRT_2, 0x04);
  1194. PWRAPLOG("mt_pwrap_init---- debug11 with adc &6328 adc\n");
  1195. /* ############################### */
  1196. /* PMIC_WRAP enables */
  1197. /* ############################### */
  1198. #if defined(CONFIG_ARCH_MT6735M)
  1199. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, 0xff);
  1200. PWRAPLOG("mt_pwrap_init---- use D2\n");
  1201. #else
  1202. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, 0x3ff);
  1203. #endif
  1204. WRAP_WR32(PMIC_WRAP_WACS0_EN, ENABLE);
  1205. WRAP_WR32(PMIC_WRAP_WACS1_EN, ENABLE);
  1206. #if defined(CONFIG_ARCH_MT6735M)
  1207. /* not enable wacs3 D2 change */
  1208. #else
  1209. WRAP_WR32(PMIC_WRAP_WACS3_EN, ENABLE);
  1210. #endif
  1211. PWRAPLOG("mt_pwrap_init---- debug12 ok\n");
  1212. WRAP_WR32(PMIC_WRAP_STAUPD_PRD, 0x5); /* 0x1:20us,for concurrence test,MP:0x5; //100us */
  1213. WRAP_WR32(PMIC_WRAP_WDT_UNIT, 0xf);
  1214. WRAP_WR32(PMIC_WRAP_WDT_SRC_EN, 0xfffffbff);
  1215. WRAP_WR32(PMIC_WRAP_TIMER_EN, 0x1);
  1216. WRAP_WR32(PMIC_WRAP_INT_EN, 0xfffffbff);
  1217. PWRAPLOG("mt_pwrap_init---- debug12++\n");
  1218. /* no ok */
  1219. /* ############################### */
  1220. /* Initialization Done */
  1221. /* ############################### */
  1222. WRAP_WR32(PMIC_WRAP_INIT_DONE0 , ENABLE);
  1223. WRAP_WR32(PMIC_WRAP_INIT_DONE2 , ENABLE);
  1224. PWRAPLOG("mt_pwrap_init---- debug13\n");
  1225. #if defined(CONFIG_ARCH_MT6735M)
  1226. /* not enable wacs3 D2 change */
  1227. #else
  1228. WRAP_WR32(PMIC_WRAP_INIT_DONE3 , ENABLE);
  1229. #endif
  1230. /* WRAP_WR32(PMIC_WRAP_INIT_DONE1 , ENABLE); */
  1231. PWRAPLOG("mt_pwrap_init---- debug14\n");
  1232. WRAP_WR32(PMIC_WRAP_EINT_STA0_ADR, MT6328_INT_STA);
  1233. #ifdef CONFIG_OF
  1234. pwrap_of_iounmap();
  1235. #endif
  1236. PWRAPLOG("mt_pwrap_init---- debug15,%x\n", WRAP_RD32(PMIC_WRAP_EINT_STA0_ADR));
  1237. return 0;
  1238. }
  1239. /* EXPORT_SYMBOL(pwrap_init); */
  1240. /*Interrupt handler function*/
  1241. static int g_wrap_wdt_irq_count;
  1242. static int g_case_flag;
  1243. static irqreturn_t mt_pmic_wrap_irq(int irqno, void *dev_id)
  1244. {
  1245. unsigned long flags = 0;
  1246. PWRAPFUC();
  1247. PWRAPREG("dump pwrap register\n");
  1248. if ((WRAP_RD32(PMIC_WRAP_INT_FLG)&0x01) == 0x01) {
  1249. g_wrap_wdt_irq_count++;
  1250. g_case_flag = 0;
  1251. PWRAPREG("g_wrap_wdt_irq_count=%d\n", g_wrap_wdt_irq_count);
  1252. } else {
  1253. g_case_flag = 1;
  1254. }
  1255. spin_lock_irqsave(&wrp_lock, flags);
  1256. /* *----------------------------------------------------------------------- */
  1257. PWRAPREG("infra clock1=0x%x\n", WRAP_RD32(infracfg_ao_base+0x48));
  1258. pwrap_dump_all_register();
  1259. /* raise the priority of WACS2 for AP */
  1260. WRAP_WR32(PMIC_WRAP_HARB_HPRIO, 1<<3);
  1261. /* *----------------------------------------------------------------------- */
  1262. /* clear interrupt flag */
  1263. WRAP_WR32(PMIC_WRAP_INT_CLR, 0xffffffff);
  1264. PWRAPREG("INT flag 0x%x\n", WRAP_RD32(PMIC_WRAP_INT_EN));
  1265. PWRAPREG("infra clock2=0x%x\n", WRAP_RD32(infracfg_ao_base+0x48));
  1266. if (10 == g_wrap_wdt_irq_count || 1 == g_case_flag)
  1267. BUG_ON(1);
  1268. spin_unlock_irqrestore(&wrp_lock, flags);
  1269. return IRQ_HANDLED;
  1270. }
  1271. static u32 pwrap_read_test(void)
  1272. {
  1273. u32 rdata = 0;
  1274. u32 return_value = 0;
  1275. /* Read Test */
  1276. #ifdef SLV_6328
  1277. return_value = pwrap_read(MT6328_DEW_READ_TEST, &rdata);
  1278. if (rdata != MT6328_DEFAULT_VALUE_READ_TEST) {
  1279. PWRAPREG("Read Test fail,rdata=0x%x, exp=0x5aa5,return_value=0x%x\n", rdata, return_value);
  1280. return E_PWR_READ_TEST_FAIL;
  1281. }
  1282. #endif
  1283. #ifdef SLV_6332
  1284. return_value = pwrap_read(MT6332_DEW_READ_TEST, &rdata);
  1285. if (rdata != MT6332_DEFAULT_VALUE_READ_TEST) {
  1286. PWRAPREG("Read Test fail,rdata=0x%x, exp=0x5aa5,return_value=0x%x\n", rdata, return_value);
  1287. return E_PWR_READ_TEST_FAIL;
  1288. }
  1289. #endif
  1290. return 0;
  1291. }
  1292. static u32 pwrap_write_test(void)
  1293. {
  1294. u32 rdata = 0;
  1295. u32 sub_return = 0;
  1296. u32 sub_return1 = 0;
  1297. /* ############################### */
  1298. /* Write test using WACS2 */
  1299. /* ############################### */
  1300. #ifdef SLV_6328
  1301. sub_return = pwrap_write(MT6328_DEW_WRITE_TEST, MT6328_WRITE_TEST_VALUE);
  1302. PWRAPREG("after MT6328 pwrap_write\n");
  1303. sub_return1 = pwrap_read(MT6328_DEW_WRITE_TEST, &rdata);
  1304. if ((rdata != MT6328_WRITE_TEST_VALUE) || (sub_return != 0) || (sub_return1 != 0)) {
  1305. PWRAPREG("write test error,rdata=0x%x,exp=0xa55a", rdata);
  1306. PWRAPREG("sub_return=0x%x,sub_return1=0x%x\n", sub_return, sub_return1);
  1307. return E_PWR_INIT_WRITE_TEST;
  1308. }
  1309. #endif
  1310. #ifdef SLV_6332
  1311. sub_return = pwrap_write(MT6332_DEW_WRITE_TEST, MT6332_WRITE_TEST_VALUE);
  1312. PWRAPREG("after MT6332 pwrap_write\n");
  1313. sub_return1 = pwrap_read(MT6332_DEW_WRITE_TEST, &rdata);
  1314. if ((rdata != MT6332_WRITE_TEST_VALUE) || (sub_return != 0) || (sub_return1 != 0)) {
  1315. PWRAPREG("write test error,rdata=0x%x", rdata);
  1316. PWRAPREG("exp=0xa55a,sub_return=0x%x,sub_return1=0x%x\n", sub_return, sub_return1);
  1317. return E_PWR_INIT_WRITE_TEST;
  1318. }
  1319. #endif
  1320. return 0;
  1321. }
  1322. static void pwrap_int_test(void)
  1323. {
  1324. u32 rdata1 = 0;
  1325. u32 rdata2 = 0;
  1326. while (1) {
  1327. #ifdef SLV_6328
  1328. rdata1 = WRAP_RD32(PMIC_WRAP_EINT_STA);
  1329. pwrap_read(MT6328_INT_STA, &rdata2);
  1330. PWRAPREG("Pwrap INT status check,PMIC_WRAP_EINT_STA=0x%x", rdata1);
  1331. PWRAPREG("MT6328_INT_STA[0x01B4]=0x%x\n", rdata2);
  1332. #endif
  1333. #ifdef SLV_6332
  1334. rdata1 = WRAP_RD32(PMIC_WRAP_EINT_STA);
  1335. pwrap_read(MT6332_INT_STA, &rdata2);
  1336. PWRAPREG("Pwrap INT status check,PMIC_WRAP_EINT_STA=0x%x", rdata1);
  1337. PWRAPREG("MT6332_INT_STA[0x8112]=0x%x\n", rdata2);
  1338. #endif
  1339. msleep(500);
  1340. }
  1341. }
  1342. static void pwrap_ut(u32 ut_test)
  1343. {
  1344. switch (ut_test) {
  1345. case 1:
  1346. /* pwrap_wacs2_para_test(); */
  1347. pwrap_write_test();
  1348. break;
  1349. case 2:
  1350. /* pwrap_wacs2_para_test(); */
  1351. pwrap_read_test();
  1352. break;
  1353. default:
  1354. PWRAPREG("default test.\n");
  1355. break;
  1356. }
  1357. }
  1358. /*---------------------------------------------------------------------------*/
  1359. static s32 mt_pwrap_show_hal(char *buf)
  1360. {
  1361. PWRAPFUC();
  1362. return snprintf(buf, PAGE_SIZE, "%s\n", "no implement");
  1363. }
  1364. /*---------------------------------------------------------------------------*/
  1365. static s32 mt_pwrap_store_hal(const char *buf, size_t count)
  1366. {
  1367. u32 reg_value = 0;
  1368. u32 reg_addr = 0;
  1369. u32 return_value = 0;
  1370. u32 ut_test = 0;
  1371. if (!strncmp(buf, "-h", 2)) {
  1372. PWRAPREG("PWRAP debug: [-dump_reg][-trace_wacs2][-init]");
  1373. PWRAPREG("[-rdap][-wrap][-rdpmic][-wrpmic][-readtest][-writetest]\n");
  1374. PWRAPREG("PWRAP UT: [1][2]\n");
  1375. }
  1376. /* ------------pwrap debug-------------------- */
  1377. else if (!strncmp(buf, "-dump_reg", 9))
  1378. pwrap_dump_all_register();
  1379. else if (!strncmp(buf, "-trace_wacs2", 12))
  1380. ;/* pwrap_trace_wacs2(); */
  1381. else if (!strncmp(buf, "-init", 5)) {
  1382. return_value = pwrap_init();
  1383. if (return_value == 0)
  1384. PWRAPREG("pwrap_init pass,return_value=%d\n", return_value);
  1385. else
  1386. PWRAPREG("pwrap_init fail,return_value=%d\n", return_value);
  1387. } else if (!strncmp(buf, "-rdap", 5) && (1 == sscanf(buf+5, "%x", &reg_addr)))
  1388. ;/* pwrap_read_reg_on_ap(reg_addr); */
  1389. else if (!strncmp(buf, "-wrap", 5) && (2 == sscanf(buf+5, "%x %x", &reg_addr, &reg_value)))
  1390. ;/* pwrap_write_reg_on_ap(reg_addr,reg_value); */
  1391. else if (!strncmp(buf, "-rdpmic", 7) && (1 == sscanf(buf+7, "%x", &reg_addr)))
  1392. ;/* pwrap_read_reg_on_pmic(reg_addr); */
  1393. else if (!strncmp(buf, "-wrpmic", 7) && (2 == sscanf(buf+7, "%x %x", &reg_addr, &reg_value)))
  1394. ;/* pwrap_write_reg_on_pmic(reg_addr,reg_value); */
  1395. else if (!strncmp(buf, "-readtest", 9))
  1396. pwrap_read_test();
  1397. else if (!strncmp(buf, "-writetest", 10))
  1398. pwrap_write_test();
  1399. else if (!strncmp(buf, "-int", 4))
  1400. pwrap_int_test();
  1401. /* ----------pwrap UT---------------- */
  1402. else if (!strncmp(buf, "-ut", 3) && (1 == sscanf(buf+3, "%d", &ut_test)))
  1403. pwrap_ut(ut_test);
  1404. else
  1405. PWRAPREG("wrong parameter\n");
  1406. return count;
  1407. }
  1408. /*---------------------------------------------------------------------------*/
  1409. #endif /* endif PMIC_WRAP_NO_PMIC */
  1410. #ifdef CONFIG_OF
  1411. static int pwrap_of_iomap(void)
  1412. {
  1413. /*
  1414. * Map the address of the following register base:
  1415. * INFRACFG_AO, TOPCKGEN, SCP_CLK_CTRL, SCP_PMICWP2P
  1416. */
  1417. struct device_node *infracfg_ao_node;
  1418. struct device_node *topckgen_node;
  1419. infracfg_ao_node =
  1420. of_find_compatible_node(NULL, NULL, "mediatek,INFRACFG_AO");
  1421. if (!infracfg_ao_node) {
  1422. pr_warn("get INFRACFG_AO failed\n");
  1423. return -ENODEV;
  1424. }
  1425. infracfg_ao_base = of_iomap(infracfg_ao_node, 0);
  1426. if (!infracfg_ao_base) {
  1427. pr_warn("INFRACFG_AO iomap failed\n");
  1428. return -ENOMEM;
  1429. }
  1430. topckgen_node = of_find_compatible_node(NULL, NULL, "mediatek,CKSYS");
  1431. if (!topckgen_node) {
  1432. pr_warn("get TOPCKGEN failed\n");
  1433. return -ENODEV;
  1434. }
  1435. topckgen_base = of_iomap(topckgen_node, 0);
  1436. if (!topckgen_base) {
  1437. pr_warn("TOPCKGEN iomap failed\n");
  1438. return -ENOMEM;
  1439. }
  1440. return 0;
  1441. }
  1442. static void pwrap_of_iounmap(void)
  1443. {
  1444. /* iounmap(infracfg_ao_base); */
  1445. iounmap(topckgen_base);
  1446. }
  1447. #endif
  1448. #define VERSION "Revision"
  1449. static int is_pwrap_init_done(void)
  1450. {
  1451. int ret = 0;
  1452. ret = WRAP_RD32(PMIC_WRAP_INIT_DONE2);
  1453. PWRAPLOG("is_pwrap_init_done %d\n", ret);
  1454. if (ret != 0)
  1455. return 0;
  1456. ret = pwrap_init();
  1457. if (ret != 0) {
  1458. PWRAPERR("init error (%d)\n", ret);
  1459. pwrap_dump_all_register();
  1460. return ret;
  1461. }
  1462. PWRAPLOG("init successfully done (%d)\n\n", ret);
  1463. return ret;
  1464. }
  1465. static int __init pwrap_hal_init(void)
  1466. {
  1467. s32 ret = 0;
  1468. #ifdef CONFIG_OF
  1469. u32 pwrap_irq;
  1470. struct device_node *pwrap_node;
  1471. PWRAPLOG("mt_pwrap_init\n");
  1472. g_wrap_wdt_irq_count = 0;
  1473. g_case_flag = 0;
  1474. pwrap_node = of_find_compatible_node(NULL, NULL, "mediatek,PWRAP");
  1475. if (!pwrap_node) {
  1476. pr_warn("PWRAP get node failed\n");
  1477. return -ENODEV;
  1478. }
  1479. pwrap_base = of_iomap(pwrap_node, 0);
  1480. if (!pwrap_base) {
  1481. pr_warn("PWRAP iomap failed\n");
  1482. return -ENOMEM;
  1483. }
  1484. pwrap_irq = irq_of_parse_and_map(pwrap_node, 0);
  1485. if (!pwrap_irq) {
  1486. pr_warn("PWRAP get irq fail\n");
  1487. return -ENODEV;
  1488. }
  1489. pr_warn("PWRAP reg: 0x%p, irq: %d\n", pwrap_base, pwrap_irq);
  1490. #endif
  1491. PWRAPLOG("real init: version %s\n", VERSION);
  1492. mt_wrp = get_mt_pmic_wrap_drv();
  1493. mt_wrp->store_hal = mt_pwrap_store_hal;
  1494. mt_wrp->show_hal = mt_pwrap_show_hal;
  1495. mt_wrp->wacs2_hal = pwrap_wacs2_hal;
  1496. PWRAPLOG("mt_pwrap_init---- debug1\n");
  1497. pwrap_of_iomap();
  1498. if (is_pwrap_init_done() == 0) {
  1499. #ifdef PMIC_WRAP_NO_PMIC
  1500. #else
  1501. ret = request_irq(MT_PMIC_WRAP_IRQ_ID, mt_pmic_wrap_irq, IRQF_TRIGGER_HIGH, PMIC_WRAP_DEVICE, 0);
  1502. #endif
  1503. if (ret) {
  1504. PWRAPERR("register IRQ failed (%d)\n", ret);
  1505. return ret;
  1506. }
  1507. } else{
  1508. PWRAPERR("not init (%d)\n", ret);
  1509. }
  1510. pwrap_ut(1);
  1511. pwrap_ut(2);
  1512. PWRAPLOG("mt_pwrap_init----\n");
  1513. return ret;
  1514. }
  1515. postcore_initcall(pwrap_hal_init);