pmic_wrap_init.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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. /*-----start-- Driver Config-------------------------------------------------*/
  10. /* #define PMIC_WRAP_PRELOADER_DRIVER */
  11. #define PMIC_WRAP_LK_DRIVER
  12. /* #define PMIC_WRAP_KERNEL_DRIVER */
  13. /*-----end -- Driver Config-------------------------------------------------*/
  14. #ifdef PMIC_WRAP_PRELOADER_DRIVER
  15. #include <timer.h>
  16. #include <typedefs.h>
  17. #include <gpio.h>
  18. #include "pmic_wrap_init.h"
  19. #endif
  20. #ifdef PMIC_WRAP_LK_DRIVER
  21. #include <platform/pmic_wrap_init.h>
  22. #endif
  23. #ifdef PMIC_WRAP_KERNEL_DRIVER
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/interrupt.h>
  27. #ifdef CONFIG_OF
  28. #include <linux/of.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_irq.h>
  31. #endif
  32. #include <linux/sched.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/timer.h>
  35. #include <mt-plat/mt_pmic_wrap.h>
  36. /* #include <mach/mt_typedefs.h> */
  37. #include "pwrap_hal.h"
  38. #define PMIC_WRAP_DEVICE "pmic_wrap"
  39. /*-----start-- global variable-----------------------------------------------*/
  40. #ifdef CONFIG_OF
  41. void __iomem *pwrap_base;
  42. static void __iomem *topckgen_reg_base;
  43. static void __iomem *infracfg_ao_reg_base;
  44. static void __iomem *gpio_reg_base;
  45. static void __iomem *sleep_reg_base;
  46. #endif
  47. static struct mt_pmic_wrap_driver *mt_wrp;
  48. static DEFINE_SPINLOCK(wrp_lock);
  49. static DEFINE_SPINLOCK(wrp_lock_isr);
  50. #endif /* End of #ifdef PMIC_WRAP_KERNEL_DRIVER */
  51. /*-----start--Internal API---------------------------------------------------*/
  52. static s32 _pwrap_init_dio(u32 dio_en);
  53. static s32 _pwrap_init_cipher(void);
  54. static s32 _pwrap_init_reg_clock(u32 regck_sel);
  55. static s32 _pwrap_wacs2_nochk(u32 write, u32 adr, u32 wdata, u32 *rdata);
  56. static s32 _pwrap_reset_spislv(void);
  57. /*-----end --Internal API---------------------------------------------------*/
  58. #ifdef PMIC_WRAP_KERNEL_DRIVER
  59. #ifdef CONFIG_OF
  60. static int pwrap_of_iomap(void);
  61. static void pwrap_of_iounmap(void);
  62. #endif
  63. #endif /* End of #ifdef PMIC_WRAP_KERNEL_DRIVER */
  64. #ifdef PMIC_WRAP_NO_PMIC
  65. /*-pwrap debug--------------------------------------------------------------------------*/
  66. static inline void pwrap_dump_all_register(void)
  67. {
  68. return;
  69. }
  70. #ifndef PMIC_WRAP_KERNEL_DRIVER
  71. s32 pwrap_wacs2(u32 write, u32 adr, u32 wdata, u32 *rdata)
  72. {
  73. PWRAPERR("there is no PMIC real chip,PMIC_WRAP do Nothing\n");
  74. return 0;
  75. }
  76. #endif
  77. /*********************************************************************************/
  78. /* extern API for PMIC driver, INT related control, this INT is for PMIC chip to AP */
  79. /*********************************************************************************/
  80. #ifndef PMIC_WRAP_KERNEL_DRIVER
  81. s32 pwrap_read(u32 adr, u32 *rdata)
  82. {
  83. return pwrap_wacs2(0, adr, 0, rdata);
  84. }
  85. s32 pwrap_write(u32 adr, u32 wdata)
  86. {
  87. return pwrap_wacs2(1, adr, wdata, 0);
  88. }
  89. #else
  90. u32 mt_pmic_wrap_eint_status(void)
  91. {
  92. PWRAPLOG("[PMIC2]first of all PMIC_WRAP_STAUPD_GRPEN=0x%x\n",
  93. WRAP_RD32(PMIC_WRAP_STAUPD_GRPEN));
  94. PWRAPLOG("[PMIC2]first of all PMIC_WRAP_EINT_STA=0x%x\n", WRAP_RD32(PMIC_WRAP_EINT_STA));
  95. return WRAP_RD32(PMIC_WRAP_EINT_STA);
  96. }
  97. void mt_pmic_wrap_eint_clr(int offset)
  98. {
  99. if ((offset < 0) || (offset > 3))
  100. PWRAPERR("clear EINT flag error, only 0-3 bit\n");
  101. else
  102. WRAP_WR32(PMIC_WRAP_EINT_CLR, (1 << offset));
  103. }
  104. /* -------------------------------------------------------- */
  105. /* Function : pwrap_wacs2_hal() */
  106. /* Description : */
  107. /* Parameter : */
  108. /* Return : */
  109. /* -------------------------------------------------------- */
  110. static s32 pwrap_wacs2_hal(u32 write, u32 adr, u32 wdata, u32 *rdata)
  111. {
  112. PWRAPERR("there is no PMIC real chip,PMIC_WRAP do Nothing\n");
  113. return 0;
  114. }
  115. #endif /* End of #ifndef PMIC_WRAP_KERNEL_DRIVER */
  116. /*
  117. *pmic_wrap init,init wrap interface
  118. *
  119. */
  120. s32 pwrap_init(void)
  121. {
  122. PWRAPERR("There is no PMIC real chip, PMIC_WRAP do Nothing.\n");
  123. return 0;
  124. }
  125. s32 pwrap_read_nochk(u32 adr, u32 *rdata)
  126. {
  127. return pwrap_wacs2_hal(0, adr, 0, rdata);
  128. }
  129. s32 pwrap_write_nochk(u32 adr, u32 wdata)
  130. {
  131. return pwrap_wacs2_hal(1, adr, wdata, 0);
  132. }
  133. #ifdef PMIC_WRAP_PRELOADER_DRIVER
  134. s32 pwrap_init_preloader(void)
  135. {
  136. u32 pwrap_ret = 0, i = 0;
  137. PWRAPFUC();
  138. for (i = 0; i < 3; i++) {
  139. pwrap_ret = pwrap_init();
  140. if (pwrap_ret != 0) {
  141. printf("[PMIC_WRAP]wrap_init fail,the return value=%x.\n", pwrap_ret);
  142. } else {
  143. printf("[PMIC_WRAP]wrap_init pass,the return value=%x.\n", pwrap_ret);
  144. break; /* init pass */
  145. }
  146. }
  147. #ifdef PWRAP_PRELOADER_PORTING
  148. /* pwrap_init_for_early_porting(); */
  149. #endif
  150. return 0;
  151. }
  152. #ifdef PWRAP_PRELOADER_PORTING
  153. /* -------------------------------------------------------- */
  154. /* Function : _pwrap_status_update_test() */
  155. /* Description :only for early porting */
  156. /* Parameter : */
  157. /* Return : */
  158. /* -------------------------------------------------------- */
  159. static s32 _pwrap_status_update_test_porting(void)
  160. {
  161. return 0;
  162. }
  163. void pwrap_init_for_early_porting(void)
  164. {
  165. }
  166. #endif /* PWRAP_PRELOADER_PORTING */
  167. #endif /* End of #ifdef PMIC_WRAP_PRELOADER_DRIVER */
  168. #ifdef PMIC_WRAP_KERNEL_DRIVER
  169. /*Interrupt handler function*/
  170. static irqreturn_t mt_pmic_wrap_irq(int irqno, void *dev_id)
  171. {
  172. unsigned long flags = 0;
  173. PWRAPFUC();
  174. PWRAPREG("dump pwrap register\n");
  175. spin_lock_irqsave(&wrp_lock, flags);
  176. /* *----------------------------------------------------------------------- */
  177. pwrap_dump_all_register();
  178. /* raise the priority of WACS2 for AP */
  179. WRAP_WR32(PMIC_WRAP_HARB_HPRIO, WACS2);
  180. /* *----------------------------------------------------------------------- */
  181. /* clear interrupt flag */
  182. WRAP_WR32(PMIC_WRAP_INT0_CLR, 0xffffffff);
  183. WRAP_WR32(PMIC_WRAP_INT1_CLR, 0xffffffff);
  184. PWRAPREG("INT flag 0x%x\n", WRAP_RD32(PMIC_WRAP_INT0_FLG_RAW));
  185. PWRAPREG("INT flag 0x%x\n", WRAP_RD32(PMIC_WRAP_INT1_FLG_RAW));
  186. /* BUG_ON(1); */
  187. spin_unlock_irqrestore(&wrp_lock, flags);
  188. return IRQ_HANDLED;
  189. }
  190. static u32 pwrap_read_test(void)
  191. {
  192. return 0;
  193. }
  194. static u32 pwrap_write_test(void)
  195. {
  196. return 0;
  197. }
  198. static void pwrap_ut(u32 ut_test)
  199. {
  200. switch (ut_test) {
  201. case 1:
  202. /* pwrap_wacs2_para_test(); */
  203. pwrap_write_test();
  204. break;
  205. case 2:
  206. /* pwrap_wacs2_para_test(); */
  207. pwrap_read_test();
  208. break;
  209. default:
  210. PWRAPREG("default test.\n");
  211. break;
  212. }
  213. }
  214. /*---------------------------------------------------------------------------*/
  215. static s32 mt_pwrap_show_hal(char *buf)
  216. {
  217. PWRAPFUC();
  218. return snprintf(buf, PAGE_SIZE, "%s\n", "no implement");
  219. }
  220. /*---------------------------------------------------------------------------*/
  221. static s32 mt_pwrap_store_hal(const char *buf, size_t count)
  222. {
  223. u32 reg_value = 0;
  224. u32 reg_addr = 0;
  225. u32 return_value = 0;
  226. u32 ut_test = 0;
  227. if (!strncmp(buf, "-h", 2)) {
  228. PWRAPREG
  229. ("PWRAP debug: [-dump_reg][-trace_wacs2][-init][-rdpmic][-wrpmic][-readtest][-writetest]\n");
  230. PWRAPREG("PWRAP UT: [1][2]\n");
  231. }
  232. /* ----------pwrap debug---------- */
  233. else if (!strncmp(buf, "-dump_reg", 9)) {
  234. pwrap_dump_all_register();
  235. } else if (!strncmp(buf, "-trace_wacs2", 12)) {
  236. /* pwrap_trace_wacs2(); */
  237. } else if (!strncmp(buf, "-init", 5)) {
  238. return_value = pwrap_init();
  239. if (return_value == 0)
  240. PWRAPREG("pwrap_init pass,return_value=%d\n", return_value);
  241. else
  242. PWRAPREG("pwrap_init fail,return_value=%d\n", return_value);
  243. } else if (!strncmp(buf, "-rdpmic", 7) && (1 == sscanf(buf + 7, "%x", &reg_addr))) {
  244. /* pwrap_read_reg_on_pmic(reg_addr); */
  245. } else if (!strncmp(buf, "-wrpmic", 7)
  246. && (2 == sscanf(buf + 7, "%x %x", &reg_addr, &reg_value))) {
  247. /* pwrap_write_reg_on_pmic(reg_addr,reg_value); */
  248. } else if (!strncmp(buf, "-readtest", 9)) {
  249. pwrap_read_test();
  250. } else if (!strncmp(buf, "-writetest", 10)) {
  251. pwrap_write_test();
  252. }
  253. /* ----------pwrap UT---------- */
  254. else if (!strncmp(buf, "-ut", 3) && (1 == sscanf(buf + 3, "%d", &ut_test))) {
  255. pwrap_ut(ut_test);
  256. } else {
  257. PWRAPREG("wrong parameter\n");
  258. }
  259. return count;
  260. }
  261. #endif /* End of #ifdef PMIC_WRAP_KERNEL_DRIVER */
  262. /*---------------------------------------------------------------------------*/
  263. #else /* Else of #ifdef PMIC_WRAP_NO_PMIC */
  264. /*-pwrap debug--------------------------------------------------------------------------*/
  265. static inline void pwrap_dump_ap_register(void)
  266. {
  267. u32 i = 0;
  268. PWRAPREG("dump pwrap register, base=0x%x\n", PMIC_WRAP_BASE);
  269. PWRAPREG("address : 3 2 1 0 7 6 5 4 B A 9 8 F E D C\n");
  270. for (i = 0; i <= PMIC_WRAP_REG_RANGE; i += 4) {
  271. PWRAPREG("offset 0x%.3x:0x%.8x 0x%.8x 0x%.8x 0x%.8x\n", i * 4,
  272. WRAP_RD32(PMIC_WRAP_BASE + (i * 4) + 0),
  273. WRAP_RD32(PMIC_WRAP_BASE + (i * 4) + 0x4),
  274. WRAP_RD32(PMIC_WRAP_BASE + (i * 4) + 0x8),
  275. WRAP_RD32(PMIC_WRAP_BASE + (i * 4) + 0xC));
  276. }
  277. }
  278. static inline void pwrap_dump_pmic_register(void)
  279. {
  280. #if 0
  281. u32 i = 0;
  282. u32 reg_addr = 0;
  283. u32 reg_value = 0;
  284. PWRAPREG("dump dewrap register\n");
  285. for (i = 0; i <= 14; i++) {
  286. reg_addr = (DEW_BASE + i * 4);
  287. reg_value = pwrap_read_nochk(reg_addr, &reg_value);
  288. PWRAPREG("0x%x=0x%x\n", reg_addr, reg_value);
  289. }
  290. #endif
  291. }
  292. static inline void pwrap_dump_all_register(void)
  293. {
  294. pwrap_dump_ap_register();
  295. pwrap_dump_pmic_register();
  296. }
  297. /******************************************************************************
  298. pmic wrapper timeout
  299. ******************************************************************************/
  300. #define PWRAP_TIMEOUT
  301. /* use the same API name with kernel driver */
  302. /* however,the timeout API in preloader/lk use tick instead of ns */
  303. #ifdef PWRAP_TIMEOUT
  304. static u64 _pwrap_get_current_time(void)
  305. {
  306. #ifdef PMIC_WRAP_KERNEL_DRIVER
  307. return sched_clock(); /* /TODO: fix me */
  308. #else
  309. return gpt4_get_current_tick();
  310. #endif
  311. }
  312. static bool _pwrap_timeout_ns(u64 start_time_ns, u64 timeout_time_ns)
  313. {
  314. #ifdef PMIC_WRAP_KERNEL_DRIVER
  315. u64 cur_time = 0;
  316. u64 elapse_time = 0;
  317. /* get current tick */
  318. cur_time = _pwrap_get_current_time(); /* ns */
  319. /* avoid timer over flow exiting in FPGA env */
  320. if (cur_time < start_time_ns) {
  321. PWRAPERR("@@@@Timer overflow! start%lld cur timer%lld\n", start_time_ns, cur_time);
  322. start_time_ns = cur_time;
  323. timeout_time_ns = 10000 * 1000; /* 10000us */
  324. PWRAPERR("@@@@reset timer! start%lld setting%lld\n", start_time_ns,
  325. timeout_time_ns);
  326. }
  327. elapse_time = cur_time - start_time_ns;
  328. /* check if timeout */
  329. if (timeout_time_ns <= elapse_time) {
  330. /* timeout */
  331. PWRAPERR
  332. ("@@@@Timeout: elapse time%lld, start%lld , current%lld, setting timer%lld\n",
  333. elapse_time, start_time_ns, cur_time, timeout_time_ns);
  334. return true;
  335. }
  336. return false;
  337. #else
  338. return gpt4_timeout_tick(start_time_ns, timeout_time_ns);
  339. #endif
  340. }
  341. static u64 _pwrap_time2ns(u64 time_us)
  342. {
  343. #ifdef PMIC_WRAP_KERNEL_DRIVER
  344. return time_us * 1000;
  345. #else
  346. return gpt4_time2tick_us(time_us);
  347. #endif
  348. }
  349. #else
  350. static u64 _pwrap_get_current_time(void)
  351. {
  352. return 0;
  353. }
  354. static BOOL _pwrap_timeout_ns(u64 start_time_ns, u64 elapse_time)
  355. {
  356. return false;
  357. }
  358. static u64 _pwrap_time2ns(u64 time_us)
  359. {
  360. return 0;
  361. }
  362. #endif /* End of #ifdef PWRAP_TIMEOUT */
  363. /* ##################################################################### */
  364. /* define macro and inline function (for do while loop) */
  365. /* ##################################################################### */
  366. typedef u32(*loop_condition_fp) (u32); /* define a function pointer */
  367. static inline u32 wait_for_fsm_idle(u32 x)
  368. {
  369. return GET_WACS0_FSM(x) != WACS_FSM_IDLE;
  370. }
  371. static inline u32 wait_for_fsm_vldclr(u32 x)
  372. {
  373. return GET_WACS0_FSM(x) != WACS_FSM_WFVLDCLR;
  374. }
  375. static inline u32 wait_for_sync(u32 x)
  376. {
  377. return GET_SYNC_IDLE0(x) != WACS_SYNC_IDLE;
  378. }
  379. static inline u32 wait_for_idle_and_sync(u32 x)
  380. {
  381. return (GET_WACS2_FSM(x) != WACS_FSM_IDLE) || (GET_SYNC_IDLE2(x) != WACS_SYNC_IDLE);
  382. }
  383. static inline u32 wait_for_wrap_idle(u32 x)
  384. {
  385. return (GET_WRAP_FSM(x) != 0x0) || (GET_WRAP_CH_DLE_RESTCNT(x) != 0x0);
  386. }
  387. static inline u32 wait_for_wrap_state_idle(u32 x)
  388. {
  389. return GET_WRAP_AG_DLE_RESTCNT(x) != 0;
  390. }
  391. static inline u32 wait_for_man_idle_and_noreq(u32 x)
  392. {
  393. return (GET_MAN_REQ(x) != MAN_FSM_NO_REQ) || (GET_MAN_FSM(x) != MAN_FSM_IDLE);
  394. }
  395. static inline u32 wait_for_man_vldclr(u32 x)
  396. {
  397. return GET_MAN_FSM(x) != MAN_FSM_WFVLDCLR;
  398. }
  399. static inline u32 wait_for_cipher_ready(u32 x)
  400. {
  401. return x != 3;
  402. }
  403. static inline u32 wait_for_stdupd_idle(u32 x)
  404. {
  405. return GET_STAUPD_FSM(x) != 0x0;
  406. }
  407. #ifdef PMIC_WRAP_KERNEL_DRIVER
  408. static inline u32 wait_for_state_ready_init(loop_condition_fp fp, u32 timeout_us,
  409. void *wacs_register, u32 *read_reg)
  410. #else
  411. static inline u32 wait_for_state_ready_init(loop_condition_fp fp, u32 timeout_us, u32 wacs_register,
  412. u32 *read_reg)
  413. #endif
  414. {
  415. u64 start_time_ns = 0, timeout_ns = 0;
  416. u32 reg_rdata = 0x0;
  417. start_time_ns = _pwrap_get_current_time();
  418. timeout_ns = _pwrap_time2ns(timeout_us);
  419. do {
  420. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  421. PWRAPLOG("wait_for_state_ready_init timeout when waiting for idle\n");
  422. return E_PWR_WAIT_IDLE_TIMEOUT;
  423. }
  424. reg_rdata = WRAP_RD32(wacs_register);
  425. } while (fp(reg_rdata)); /* IDLE State */
  426. if (read_reg)
  427. *read_reg = reg_rdata;
  428. return 0;
  429. }
  430. #ifdef PMIC_WRAP_KERNEL_DRIVER
  431. static inline u32 wait_for_state_idle_init(loop_condition_fp fp, u32 timeout_us,
  432. void *wacs_register, void *wacs_vldclr_register,
  433. u32 *read_reg)
  434. #else
  435. static inline u32 wait_for_state_idle_init(loop_condition_fp fp, u32 timeout_us, u32 wacs_register,
  436. u32 wacs_vldclr_register, u32 *read_reg)
  437. #endif
  438. {
  439. u64 start_time_ns = 0, timeout_ns = 0;
  440. u32 reg_rdata;
  441. start_time_ns = _pwrap_get_current_time();
  442. timeout_ns = _pwrap_time2ns(timeout_us);
  443. do {
  444. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  445. PWRAPLOG("wait_for_state_idle_init timeout when waiting for idle\n");
  446. pwrap_dump_ap_register();
  447. /* pwrap_trace_wacs2(); */
  448. /* BUG_ON(1); */
  449. return E_PWR_WAIT_IDLE_TIMEOUT;
  450. }
  451. reg_rdata = WRAP_RD32(wacs_register);
  452. /* if last read command timeout,clear vldclr bit */
  453. /* read command state machine:FSM_REQ-->wfdle-->WFVLDCLR;write:FSM_REQ-->idle */
  454. switch (GET_WACS0_FSM(reg_rdata)) {
  455. case WACS_FSM_WFVLDCLR:
  456. WRAP_WR32(wacs_vldclr_register, 1);
  457. PWRAPLOG("WACS_FSM = PMIC_WRAP_WACS_VLDCLR\n");
  458. break;
  459. case WACS_FSM_WFDLE:
  460. PWRAPLOG("WACS_FSM = WACS_FSM_WFDLE\n");
  461. break;
  462. case WACS_FSM_REQ:
  463. PWRAPLOG("WACS_FSM = WACS_FSM_REQ\n");
  464. break;
  465. default:
  466. break;
  467. }
  468. } while (fp(reg_rdata)); /* IDLE State */
  469. if (read_reg)
  470. *read_reg = reg_rdata;
  471. return 0;
  472. }
  473. #ifdef PMIC_WRAP_KERNEL_DRIVER
  474. static inline u32 wait_for_state_idle(loop_condition_fp fp, u32 timeout_us, void *wacs_register,
  475. void *wacs_vldclr_register, u32 *read_reg)
  476. #else
  477. static inline u32 wait_for_state_idle(loop_condition_fp fp, u32 timeout_us, u32 wacs_register,
  478. u32 wacs_vldclr_register, u32 *read_reg)
  479. #endif
  480. {
  481. u64 start_time_ns = 0, timeout_ns = 0;
  482. u64 loop_counter = 0;
  483. u32 reg_rdata;
  484. start_time_ns = _pwrap_get_current_time();
  485. timeout_ns = _pwrap_time2ns(timeout_us);
  486. do {
  487. loop_counter++;
  488. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  489. PWRAPLOG
  490. ("wait_for_state_idle timeout when waiting for idle, loop_counter = %lld\n",
  491. loop_counter);
  492. pwrap_dump_ap_register();
  493. /* pwrap_trace_wacs2(); */
  494. /* BUG_ON(1); */
  495. return E_PWR_WAIT_IDLE_TIMEOUT;
  496. }
  497. reg_rdata = WRAP_RD32(wacs_register);
  498. if (GET_INIT_DONE0(reg_rdata) != WACS_INIT_DONE) {
  499. PWRAPLOG("initialization isn't finished\n");
  500. return E_PWR_NOT_INIT_DONE;
  501. }
  502. /* if last read command timeout,clear vldclr bit */
  503. /* read command state machine:FSM_REQ-->wfdle-->WFVLDCLR;write:FSM_REQ-->idle */
  504. switch (GET_WACS0_FSM(reg_rdata)) {
  505. case WACS_FSM_WFVLDCLR:
  506. WRAP_WR32(wacs_vldclr_register, 1);
  507. PWRAPLOG("WACS_FSM = PMIC_WRAP_WACS_VLDCLR\n");
  508. break;
  509. case WACS_FSM_WFDLE:
  510. PWRAPLOG("WACS_FSM = WACS_FSM_WFDLE\n");
  511. break;
  512. case WACS_FSM_REQ:
  513. PWRAPLOG("WACS_FSM = WACS_FSM_REQ\n");
  514. break;
  515. default:
  516. break;
  517. }
  518. } while (fp(reg_rdata)); /* IDLE State */
  519. if (read_reg)
  520. *read_reg = reg_rdata;
  521. return 0;
  522. }
  523. #ifdef PMIC_WRAP_KERNEL_DRIVER
  524. static inline u32 wait_for_state_ready(loop_condition_fp fp, u32 timeout_us, void *wacs_register,
  525. u32 *read_reg)
  526. #else
  527. static inline u32 wait_for_state_ready(loop_condition_fp fp, u32 timeout_us, u32 wacs_register,
  528. u32 *read_reg)
  529. #endif
  530. {
  531. u64 start_time_ns = 0, timeout_ns = 0;
  532. u64 loop_count = 0;
  533. u32 reg_rdata;
  534. start_time_ns = _pwrap_get_current_time();
  535. timeout_ns = _pwrap_time2ns(timeout_us);
  536. do {
  537. loop_count++;
  538. if (_pwrap_timeout_ns(start_time_ns, timeout_ns)) {
  539. PWRAPLOG("timeout when waiting for idle, loop_count = %lld\n", loop_count);
  540. pwrap_dump_ap_register();
  541. return E_PWR_WAIT_IDLE_TIMEOUT;
  542. }
  543. reg_rdata = WRAP_RD32(wacs_register);
  544. if (GET_INIT_DONE0(reg_rdata) != WACS_INIT_DONE) {
  545. PWRAPLOG("initialization isn't finished\n");
  546. return E_PWR_NOT_INIT_DONE;
  547. }
  548. } while (fp(reg_rdata)); /* IDLE State */
  549. if (read_reg)
  550. *read_reg = reg_rdata;
  551. return 0;
  552. }
  553. #ifndef PMIC_WRAP_KERNEL_DRIVER
  554. /* ****************************************************************************** */
  555. /* --external API for pmic_wrap user------------------------------------------------- */
  556. /* ****************************************************************************** */
  557. s32 pwrap_read(u32 adr, u32 *rdata)
  558. {
  559. return pwrap_wacs2(0, adr, 0, rdata);
  560. }
  561. s32 pwrap_write(u32 adr, u32 wdata)
  562. {
  563. return pwrap_wacs2(1, adr, wdata, 0);
  564. }
  565. #endif
  566. #ifdef PMIC_WRAP_KERNEL_DRIVER
  567. /********************************************************************************************/
  568. /* extern API for PMIC driver, INT related control, this INT is for PMIC chip to AP */
  569. /********************************************************************************************/
  570. u32 mt_pmic_wrap_eint_status(void)
  571. {
  572. return WRAP_RD32(PMIC_WRAP_EINT_STA);
  573. }
  574. void mt_pmic_wrap_eint_clr(int offset)
  575. {
  576. if ((offset < 0) || (offset > 3))
  577. PWRAPERR("clear EINT flag error, only 0-3 bit\n");
  578. else
  579. WRAP_WR32(PMIC_WRAP_EINT_CLR, (1 << offset));
  580. PWRAPREG("clear EINT flag mt_pmic_wrap_eint_status=0x%x\n", WRAP_RD32(PMIC_WRAP_EINT_STA));
  581. }
  582. #endif
  583. /* -------------------------------------------------------- */
  584. /* Function : pwrap_wacs2() */
  585. /* Description : */
  586. /* Parameter : */
  587. /* Return : */
  588. /* -------------------------------------------------------- */
  589. #ifdef PMIC_WRAP_KERNEL_DRIVER
  590. static s32 pwrap_wacs2_hal(u32 write, u32 adr, u32 wdata, u32 *rdata)
  591. #else
  592. s32 pwrap_wacs2(u32 write, u32 adr, u32 wdata, u32 *rdata)
  593. #endif
  594. {
  595. /* u64 wrap_access_time=0x0; */
  596. u32 reg_rdata = 0;
  597. u32 wacs_write = 0;
  598. u32 wacs_adr = 0;
  599. u32 wacs_cmd = 0;
  600. u32 return_value = 0;
  601. #ifdef PMIC_WRAP_KERNEL_DRIVER
  602. unsigned long flags = 0;
  603. #endif
  604. /* Check argument validation */
  605. if ((write & ~(0x1)) != 0)
  606. return E_PWR_INVALID_RW;
  607. if ((adr & ~(0xffff)) != 0)
  608. return E_PWR_INVALID_ADDR;
  609. if ((wdata & ~(0xffff)) != 0)
  610. return E_PWR_INVALID_WDAT;
  611. #ifdef PMIC_WRAP_KERNEL_DRIVER
  612. spin_lock_irqsave(&wrp_lock, flags);
  613. #endif
  614. /* Check IDLE & INIT_DONE in advance */
  615. return_value =
  616. wait_for_state_idle(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA,
  617. PMIC_WRAP_WACS2_VLDCLR, 0);
  618. if (return_value != 0) {
  619. PWRAPLOG("wait_for_fsm_idle fail,return_value=%d\n", return_value);
  620. goto FAIL;
  621. }
  622. wacs_write = write << 31;
  623. wacs_adr = (adr >> 1) << 16;
  624. wacs_cmd = wacs_write | wacs_adr | wdata;
  625. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  626. if (write == 0) {
  627. if (NULL == rdata) {
  628. PWRAPLOG("rdata is a NULL pointer\n");
  629. return_value = E_PWR_INVALID_ARG;
  630. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  631. goto FAIL;
  632. }
  633. return_value =
  634. wait_for_state_ready(wait_for_fsm_vldclr, TIMEOUT_READ, PMIC_WRAP_WACS2_RDATA,
  635. &reg_rdata);
  636. if (return_value != 0) {
  637. PWRAPLOG("wait_for_fsm_vldclr fail,return_value=%d\n", return_value);
  638. return_value += 1; /* E_PWR_NOT_INIT_DONE_READ or E_PWR_WAIT_IDLE_TIMEOUT_READ */
  639. goto FAIL;
  640. }
  641. *rdata = GET_WACS0_RDATA(reg_rdata);
  642. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  643. }
  644. FAIL:
  645. #ifdef PMIC_WRAP_KERNEL_DRIVER
  646. spin_unlock_irqrestore(&wrp_lock, flags);
  647. #endif
  648. if (return_value != 0) {
  649. PWRAPLOG("pwrap_wacs2 fail,return_value=%d\n", return_value);
  650. PWRAPLOG("timeout:BUG_ON here\n");
  651. }
  652. return return_value;
  653. }
  654. /* ****************************************************************************** */
  655. /* --internal API for pwrap_init------------------------------------------------- */
  656. /* ****************************************************************************** */
  657. /* -------------------------------------------------------- */
  658. /* Function : _pwrap_wacs2_nochk() */
  659. /* Description : */
  660. /* Parameter : */
  661. /* Return : */
  662. /* -------------------------------------------------------- */
  663. s32 pwrap_read_nochk(u32 adr, u32 *rdata)
  664. {
  665. return _pwrap_wacs2_nochk(0, adr, 0, rdata);
  666. }
  667. s32 pwrap_write_nochk(u32 adr, u32 wdata)
  668. {
  669. return _pwrap_wacs2_nochk(1, adr, wdata, 0);
  670. }
  671. static s32 _pwrap_wacs2_nochk(u32 write, u32 adr, u32 wdata, u32 *rdata)
  672. {
  673. u32 reg_rdata = 0x0;
  674. u32 wacs_write = 0x0;
  675. u32 wacs_adr = 0x0;
  676. u32 wacs_cmd = 0x0;
  677. u32 return_value = 0x0;
  678. /* PWRAPFUC(); */
  679. /* Check argument validation */
  680. if ((write & ~(0x1)) != 0)
  681. return E_PWR_INVALID_RW;
  682. if ((adr & ~(0xffff)) != 0)
  683. return E_PWR_INVALID_ADDR;
  684. if ((wdata & ~(0xffff)) != 0)
  685. return E_PWR_INVALID_WDAT;
  686. /* Check IDLE */
  687. return_value =
  688. wait_for_state_ready_init(wait_for_fsm_idle, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA,
  689. 0);
  690. if (return_value != 0) {
  691. PWRAPLOG("_pwrap_wacs2_nochk write command fail,return_value=%x\n", return_value);
  692. return return_value;
  693. }
  694. wacs_write = write << 31;
  695. wacs_adr = (adr >> 1) << 16;
  696. wacs_cmd = wacs_write | wacs_adr | wdata;
  697. WRAP_WR32(PMIC_WRAP_WACS2_CMD, wacs_cmd);
  698. if (write == 0) {
  699. if (NULL == rdata) {
  700. PWRAPLOG("rdata is a NULL pointer\n");
  701. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  702. return E_PWR_INVALID_ARG;
  703. }
  704. /* wait for read data ready */
  705. return_value =
  706. wait_for_state_ready_init(wait_for_fsm_vldclr, TIMEOUT_READ,
  707. PMIC_WRAP_WACS2_RDATA, &reg_rdata);
  708. if (return_value != 0) {
  709. PWRAPLOG("wait_for_fsm_vldclr fail, return_value=%d\n", return_value);
  710. return_value += 1; /* E_PWR_NOT_INIT_DONE_READ or E_PWR_WAIT_IDLE_TIMEOUT_READ */
  711. return return_value;
  712. }
  713. *rdata = GET_WACS0_RDATA(reg_rdata);
  714. WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR, 1);
  715. }
  716. return 0;
  717. }
  718. /* -------------------------------------------------------- */
  719. /* Function : _pwrap_init_dio() */
  720. /* Description :call it in pwrap_init,mustn't check init done */
  721. /* Parameter : */
  722. /* Return : */
  723. /* -------------------------------------------------------- */
  724. static s32 _pwrap_init_dio(u32 dio_en)
  725. {
  726. u32 arb_en_backup = 0x0;
  727. u32 rdata = 0x0;
  728. u32 return_value = 0;
  729. /* PWRAPFUC(); */
  730. arb_en_backup = WRAP_RD32(PMIC_WRAP_HIPRIO_ARB_EN);
  731. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, WACS2); /* only WACS2 */
  732. #ifdef SLV_6351
  733. pwrap_write_nochk(MT6351_DEW_DIO_EN, dio_en);
  734. #endif
  735. /* Check IDLE & INIT_DONE in advance */
  736. return_value =
  737. wait_for_state_ready_init(wait_for_idle_and_sync, TIMEOUT_WAIT_IDLE,
  738. PMIC_WRAP_WACS2_RDATA, 0);
  739. if (return_value != 0) {
  740. PWRAPLOG("_pwrap_init_dio fail,return_value=%x\n", return_value);
  741. return return_value;
  742. }
  743. /* Enable AP DIO mode */
  744. WRAP_WR32(PMIC_WRAP_DIO_EN, dio_en);
  745. /* Read Test */
  746. #ifdef SLV_6351
  747. pwrap_read_nochk(MT6351_DEW_READ_TEST, &rdata);
  748. if (rdata != MT6351_DEFAULT_VALUE_READ_TEST) {
  749. PWRAPLOG("[Dio_mode][Read Test] fail,dio_en = %x, READ_TEST rdata=%x, exp=0x5aa5\n",
  750. dio_en, rdata);
  751. return E_PWR_READ_TEST_FAIL;
  752. }
  753. #endif
  754. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, arb_en_backup);
  755. return 0;
  756. }
  757. /* -------------------------------------------------------- */
  758. /* Function : _pwrap_init_cipher() */
  759. /* Description : */
  760. /* Parameter : */
  761. /* Return : */
  762. /* -------------------------------------------------------- */
  763. static s32 _pwrap_init_cipher(void)
  764. {
  765. u32 arb_en_backup = 0;
  766. u32 rdata = 0;
  767. u32 return_value = 0;
  768. u32 start_time_ns = 0, timeout_ns = 0;
  769. /* PWRAPFUC(); */
  770. arb_en_backup = WRAP_RD32(PMIC_WRAP_HIPRIO_ARB_EN);
  771. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, WACS2); /* only WACS0 */
  772. WRAP_WR32(PMIC_WRAP_CIPHER_SWRST, 1);
  773. WRAP_WR32(PMIC_WRAP_CIPHER_SWRST, 0);
  774. WRAP_WR32(PMIC_WRAP_CIPHER_KEY_SEL, 1);
  775. WRAP_WR32(PMIC_WRAP_CIPHER_IV_SEL, 2);
  776. WRAP_WR32(PMIC_WRAP_CIPHER_EN, 1);
  777. /* Config CIPHER @ PMIC */
  778. #ifdef SLV_6351
  779. pwrap_write_nochk(MT6351_DEW_CIPHER_SWRST, 0x1);
  780. pwrap_write_nochk(MT6351_DEW_CIPHER_SWRST, 0x0);
  781. pwrap_write_nochk(MT6351_DEW_CIPHER_KEY_SEL, 0x1);
  782. pwrap_write_nochk(MT6351_DEW_CIPHER_IV_SEL, 0x2);
  783. pwrap_write_nochk(MT6351_DEW_CIPHER_EN, 0x1);
  784. #endif
  785. /* wait for cipher data ready@AP */
  786. #ifndef CONFIG_MTK_FPGA
  787. return_value =
  788. wait_for_state_ready_init(wait_for_cipher_ready, TIMEOUT_WAIT_IDLE,
  789. PMIC_WRAP_CIPHER_RDY, 0);
  790. #else
  791. return_value =
  792. wait_for_state_ready_init(wait_for_cipher_ready, 0xFFFFFFFF, PMIC_WRAP_CIPHER_RDY, 0);
  793. #endif
  794. if (return_value != 0) {
  795. PWRAPLOG("wait for cipher data ready@AP fail,return_value=%x\n", return_value);
  796. return return_value;
  797. }
  798. /* wait for cipher data ready@PMIC */
  799. #ifdef SLV_6351
  800. start_time_ns = _pwrap_get_current_time();
  801. timeout_ns = _pwrap_time2ns(0xFFFFFF);
  802. do {
  803. if (_pwrap_timeout_ns(start_time_ns, timeout_ns))
  804. PWRAPLOG("wait for cipher data ready@PMIC\n");
  805. pwrap_read_nochk(MT6351_DEW_CIPHER_RDY, &rdata);
  806. } while (rdata != 0x1); /* cipher_ready */
  807. pwrap_write_nochk(MT6351_DEW_CIPHER_MODE, 0x1);
  808. #endif
  809. /* wait for cipher mode idle */
  810. return_value =
  811. wait_for_state_ready_init(wait_for_idle_and_sync, TIMEOUT_WAIT_IDLE,
  812. PMIC_WRAP_WACS2_RDATA, 0);
  813. if (return_value != 0) {
  814. PWRAPLOG("wait for cipher mode idle fail,return_value=%x\n", return_value);
  815. return return_value;
  816. }
  817. WRAP_WR32(PMIC_WRAP_CIPHER_MODE, 1);
  818. /* Read Test */
  819. #ifdef SLV_6351
  820. pwrap_read_nochk(MT6351_DEW_READ_TEST, &rdata);
  821. if (rdata != MT6351_DEFAULT_VALUE_READ_TEST) {
  822. PWRAPLOG("_pwrap_init_cipher,read test error,error code=%x, rdata=%x\n", 1, rdata);
  823. return E_PWR_READ_TEST_FAIL;
  824. }
  825. #endif
  826. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, arb_en_backup);
  827. return 0;
  828. }
  829. /* -------------------------------------------------------- */
  830. /* Function : _pwrap_init_sistrobe() */
  831. /* Description : Initialize SI_CK_CON and SIDLY */
  832. /* Parameter : */
  833. /* Return : */
  834. /* -------------------------------------------------------- */
  835. static s32 _pwrap_init_sistrobe(void)
  836. {
  837. u32 arb_en_backup = 0;
  838. u32 rdata = 0;
  839. s32 ind = 0;
  840. u32 result_faulty = 0;
  841. u64 result = 0, tmp1 = 0, tmp2 = 0;
  842. s32 leading_one = 0;
  843. s32 tailing_one = 0;
  844. #ifdef ULPOSC
  845. u64 inds = 0, indf = 0;
  846. #endif
  847. arb_en_backup = WRAP_RD32(PMIC_WRAP_HIPRIO_ARB_EN);
  848. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, WACS2); /* only WACS2 */
  849. #ifdef ULPOSC
  850. /* --------------------------------------------------------------------- */
  851. /* Scan all possible input strobe by READ_TEST */
  852. /* --------------------------------------------------------------------- */
  853. result = 0;
  854. result_faulty = 0;
  855. inds = 0;
  856. indf = 0;
  857. for (ind = 0; ind < 44; ind++) { /* 44 sampling clock edge */
  858. WRAP_WR32(PMIC_WRAP_SI_CK_CON, (ind >> 2) & 0xf);
  859. WRAP_WR32(PMIC_WRAP_SIDLY, 0x3 - (ind & 0x3));
  860. #ifdef SLV_6351
  861. _pwrap_wacs2_nochk(0, MT6351_DEW_READ_TEST, 0, &rdata);
  862. if (rdata == MT6351_DEFAULT_VALUE_READ_TEST) {
  863. PWRAPERR
  864. ("_pwrap_init_sistrobe [Read Test of MT6351] pass,index=%d rdata=%x\n",
  865. ind, rdata);
  866. result |= (0x1 << ind);
  867. } else {
  868. PWRAPERR
  869. ("_pwrap_init_sistrobe [Read Test of MT6351] tuning,index=%d rdata=%x\n",
  870. ind, rdata);
  871. }
  872. #endif
  873. }
  874. /* --------------------------------------------------------------------- */
  875. /* Locate the leading one and trailing one */
  876. /* --------------------------------------------------------------------- */
  877. #if 0
  878. for (ind = 43; ind >= 0; ind--) {
  879. /* if( result & (0x1 << ind) ) break; */
  880. if (ind > 20) {
  881. indf = (0x1 << 20);
  882. /* indf = (indf << ((ind - 20)%32)); */
  883. } else {
  884. indf = (0x1 << (ind % 32));
  885. }
  886. inds = result & indf;
  887. if (inds)
  888. break;
  889. }
  890. leading_one = ind;
  891. for (ind = 0; ind < 44; ind++) {
  892. /* if( result & (0x1 << ind) ) break; */
  893. if (ind > 20) {
  894. indf = (0x1 << 20);
  895. /* indf = (indf << ((ind - 20)%32)); */
  896. } else {
  897. indf = (0x1 << (ind % 32));
  898. }
  899. inds = result & indf;
  900. if (inds)
  901. break;
  902. }
  903. tailing_one = ind;
  904. #else
  905. for (ind = 43; ind >= 0; ind--) {
  906. if (result & (0x1 << ind))
  907. break;
  908. }
  909. leading_one = ind;
  910. for (ind = 0; ind < 44; ind++) {
  911. if (result & (0x1 << ind))
  912. break;
  913. }
  914. tailing_one = ind;
  915. #endif
  916. /* --------------------------------------------------------------------- */
  917. /* Check the continuity of pass range */
  918. /* --------------------------------------------------------------------- */
  919. tmp1 = (0x1 << (leading_one + 1)) - 1;
  920. tmp2 = (0x1 << tailing_one) - 1;
  921. if ((tmp1 - tmp2) != result) {
  922. /*TERR = "[DrvPWRAP_InitSiStrobe] Fail, tmp1:%d, tmp2:%d", tmp1, tmp2 */
  923. PWRAPERR
  924. ("_pwrap_init_sistrobe Fail, result = %llx, leading_one:%d, tailing_one:%d\n",
  925. result, leading_one, tailing_one);
  926. result_faulty = 0x1;
  927. }
  928. /* --------------------------------------------------------------------- */
  929. /* Config SICK and SIDLY to the middle point of pass range */
  930. /* --------------------------------------------------------------------- */
  931. ind = (leading_one + tailing_one) / 2;
  932. WRAP_WR32(PMIC_WRAP_SI_CK_CON, (ind >> 2) & 0xf);
  933. WRAP_WR32(PMIC_WRAP_SIDLY, 0x3 - (ind & 0x3));
  934. #else /* Else of #ifdef ULPOSC */
  935. /* --------------------------------------------------------------------- */
  936. /* Scan all possible input strobe by READ_TEST */
  937. /* --------------------------------------------------------------------- */
  938. for (ind = 0; ind < 24; ind++) { /* 24 sampling clock edge */
  939. WRAP_WR32(PMIC_WRAP_SI_CK_CON, (ind >> 2) & 0x7);
  940. WRAP_WR32(PMIC_WRAP_SIDLY, 0x3 - (ind & 0x3));
  941. #ifdef SLV_6351
  942. _pwrap_wacs2_nochk(0, MT6351_DEW_READ_TEST, 0, &rdata);
  943. if (rdata == MT6351_DEFAULT_VALUE_READ_TEST) {
  944. PWRAPERR
  945. ("_pwrap_init_sistrobe [Read Test of MT6351] pass,index=%d rdata=%x\n",
  946. ind, rdata);
  947. result |= (0x1 << ind);
  948. } else {
  949. PWRAPERR
  950. ("_pwrap_init_sistrobe [Read Test of MT6351] tuning,index=%d rdata=%x\n",
  951. ind, rdata);
  952. }
  953. #endif
  954. }
  955. /* --------------------------------------------------------------------- */
  956. /* Locate the leading one and trailing one of PMIC 1/2 */
  957. /* --------------------------------------------------------------------- */
  958. for (ind = 23; ind >= 0; ind--) {
  959. if (result & (0x1 << ind))
  960. break;
  961. }
  962. leading_one = ind;
  963. for (ind = 0; ind < 24; ind++) {
  964. if (result & (0x1 << ind))
  965. break;
  966. }
  967. tailing_one = ind;
  968. /* --------------------------------------------------------------------- */
  969. /* Check the continuity of pass range */
  970. /* --------------------------------------------------------------------- */
  971. tmp1 = (0x1 << (leading_one + 1)) - 1;
  972. tmp2 = (0x1 << tailing_one) - 1;
  973. if ((tmp1 - tmp2) != result) {
  974. /*TERR = "[PWRAP] Check Continuity Fail at PMIC %d, result = %x, leading_one:%d, tailing_one:%d"
  975. , i+1, result[i], leading_one[i], tailing_one[i] */
  976. PWRAPERR
  977. ("_pwrap_init_sistrobe Fail, result = %llx, leading_one:%d, tailing_one:%d\n",
  978. result, leading_one, tailing_one);
  979. result_faulty = 0x1;
  980. }
  981. /* --------------------------------------------------------------------- */
  982. /* Config SICK and SIDLY to the middle point of pass range */
  983. /* --------------------------------------------------------------------- */
  984. if (result_faulty == 0) {
  985. ind = (leading_one + tailing_one) / 2;
  986. WRAP_WR32(PMIC_WRAP_SI_CK_CON, (ind >> 2) & 0x7);
  987. WRAP_WR32(PMIC_WRAP_SIDLY, 0x3 - (ind & 0x3));
  988. /* --------------------------------------------------------------------- */
  989. /* Restore */
  990. /* --------------------------------------------------------------------- */
  991. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, arb_en_backup);
  992. return 0;
  993. } else {
  994. PWRAPERR("_pwrap_init_sistrobe Fail,result_faulty=%x\n", result_faulty);
  995. return result_faulty;
  996. }
  997. #endif /* End of #ifdef ULPOSC */
  998. }
  999. /* -------------------------------------------------------- */
  1000. /* Function : _pwrap_reset_spislv() */
  1001. /* Description : */
  1002. /* Parameter : */
  1003. /* Return : */
  1004. /* -------------------------------------------------------- */
  1005. static s32 _pwrap_reset_spislv(void)
  1006. {
  1007. u32 ret = 0;
  1008. u32 return_value = 0;
  1009. /* PWRAPFUC(); */
  1010. /* This driver does not using _pwrap_switch_mux */
  1011. /* because the remaining requests are expected to fail anyway */
  1012. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, DISABLE_ALL);
  1013. WRAP_WR32(PMIC_WRAP_WRAP_EN, DISABLE);
  1014. WRAP_WR32(PMIC_WRAP_MUX_SEL, MANUAL_MODE);
  1015. WRAP_WR32(PMIC_WRAP_MAN_EN, ENABLE);
  1016. WRAP_WR32(PMIC_WRAP_DIO_EN, DISABLE);
  1017. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_CSL << 8)); /* 0x2100 */
  1018. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_OUTS << 8)); /* 0x2800//to reset counter */
  1019. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_CSH << 8)); /* 0x2000 */
  1020. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_OUTS << 8));
  1021. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_OUTS << 8));
  1022. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_OUTS << 8));
  1023. WRAP_WR32(PMIC_WRAP_MAN_CMD, (OP_WR << 13) | (OP_OUTS << 8));
  1024. return_value =
  1025. wait_for_state_ready_init(wait_for_sync, TIMEOUT_WAIT_IDLE, PMIC_WRAP_WACS2_RDATA, 0);
  1026. if (return_value != 0) {
  1027. PWRAPLOG("_pwrap_reset_spislv fail,return_value=%x\n", return_value);
  1028. ret = E_PWR_TIMEOUT;
  1029. goto timeout;
  1030. }
  1031. WRAP_WR32(PMIC_WRAP_MAN_EN, DISABLE);
  1032. WRAP_WR32(PMIC_WRAP_MUX_SEL, WRAPPER_MODE);
  1033. timeout:
  1034. WRAP_WR32(PMIC_WRAP_MAN_EN, DISABLE);
  1035. WRAP_WR32(PMIC_WRAP_MUX_SEL, WRAPPER_MODE);
  1036. return ret;
  1037. }
  1038. #if 0
  1039. static void __pwrap_soft_reset(void)
  1040. {
  1041. PWRAPLOG("start reset wrapper\n");
  1042. PWRAP_SOFT_RESET;
  1043. PWRAPLOG("the reset register =%x\n", WRAP_RD32(INFRA_GLOBALCON_RST0));
  1044. PWRAPLOG("PMIC_WRAP_STAUPD_GRPEN =0x%x,it should be equal to 0xc\n",
  1045. WRAP_RD32(PMIC_WRAP_STAUPD_GRPEN));
  1046. /* clear reset bit */
  1047. PWRAP_CLEAR_SOFT_RESET_BIT;
  1048. }
  1049. #endif
  1050. static s32 _pwrap_init_reg_clock(u32 regck_sel)
  1051. {
  1052. PWRAPFUC();
  1053. #ifdef SLV_6351
  1054. pwrap_write_nochk(MT6351_DEW_RDDMY_NO, 0x8);
  1055. WRAP_WR32(PMIC_WRAP_RDDMY, 0x08);
  1056. #endif
  1057. /* Config SPI Waveform according to reg clk */
  1058. if (regck_sel == 1) { /* 18MHz in 6328, 26Mhz in BBCHip */
  1059. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0x0);
  1060. /* Wait data written into register =>
  1061. 4T_PMIC: consists of CSHEXT_WRITE_START+EXT_CK+CSHEXT_WRITE_END+CSLEXT_START */
  1062. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0x33);
  1063. WRAP_WR32(PMIC_WRAP_CSLEXT_START, 0x0);
  1064. WRAP_WR32(PMIC_WRAP_CSLEXT_END, 0x0); /* for PMIC site synchronizer use */
  1065. } else { /* Safe mode */
  1066. WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE, 0xff);
  1067. WRAP_WR32(PMIC_WRAP_CSHEXT_READ, 0xff);
  1068. WRAP_WR32(PMIC_WRAP_CSLEXT_START, 0xf);
  1069. WRAP_WR32(PMIC_WRAP_CSLEXT_END, 0xf);
  1070. }
  1071. return 0;
  1072. }
  1073. /*
  1074. * pmic_wrap init,init wrap interface
  1075. */
  1076. s32 pwrap_init(void)
  1077. {
  1078. s32 sub_return = 0;
  1079. s32 sub_return1 = 0;
  1080. /* s32 ret=0; */
  1081. u32 rdata = 0x0;
  1082. /* u32 timeout=0; */
  1083. PWRAPFUC();
  1084. #ifdef PMIC_WRAP_KERNEL_DRIVER
  1085. #ifdef CONFIG_OF
  1086. sub_return = pwrap_of_iomap();
  1087. if (sub_return)
  1088. return sub_return;
  1089. #endif
  1090. #endif
  1091. #ifndef CONFIG_MTK_FPGA
  1092. /* ############################### */
  1093. /* toggle PMIC_WRAP and pwrap_spictl reset */
  1094. /* ############################### */
  1095. WRAP_WR32(INFRA_GLOBALCON_RST2_SET, 0x1); /* Write 1 set */
  1096. WRAP_WR32(INFRA_GLOBALCON_RST2_CLR, 0x1); /* Write 1 clr */
  1097. /* ############################### */
  1098. /* Switch GPIO to PWRAP mode => modes of PMIC_WRAP's IOs are PMIC_WRAP in default setting */
  1099. /* ############################### */
  1100. /* SET_GPIO_MODE (40, 1); // MI */
  1101. /* SET_GPIO_MODE (41, 1); // MO */
  1102. /* SET_GPIO_MODE (38, 1); // CK */
  1103. /* SET_GPIO_MODE (39, 1); // CS */
  1104. /* ############################### */
  1105. /* Set SPI_CK_freq 52MHz or 26MHz */
  1106. /* ############################### */
  1107. #ifdef ULPOSC
  1108. WRAP_WR32(ULPOSC_CON, 0x0001); /* Enable ulposc */
  1109. udelay(1);
  1110. WRAP_WR32(ULPOSC_CON, 0x0003); /* Reset ulposc */
  1111. udelay(30);
  1112. WRAP_WR32(ULPOSC_CON, 0x0001); /* Release reset ulposc */
  1113. udelay(130);
  1114. WRAP_WR32(ULPOSC_CON, 0x0005);
  1115. WRAP_WR32(CLK_CFG_5_SET, 0x0002); /* 52MHz [1:0] */
  1116. udelay(20);
  1117. /* WRAP_WR32(GPIO_DUMMY, 0x80000000); remove by ECO */
  1118. WRAP_WR32(PMICW_CLOCK_CTRL, 0x0000); /* pmicw_sw_en */
  1119. WRAP_WR32(CLK_CFG_UPDATE, 0x00080000); /* Clock configure update */
  1120. WRAP_WR32(MODULE_SW_CG_0_SET, 0x0000000F); /* sys_ck cg enable */
  1121. WRAP_WR32(MODULE_SW_CG_0_CLR, 0x0000000F); /* sys_ck cg disable */
  1122. #else
  1123. WRAP_WR32(CLK_CFG_5_CLR, 0x0001); /* 26MHz [1:0] */
  1124. WRAP_WR32(MODULE_SW_CG_0_SET, 0x0000000F); /* sys_ck cg enable */
  1125. WRAP_WR32(MODULE_SW_CG_0_CLR, 0x0000000F); /* sys_ck cg disable */
  1126. #endif
  1127. #endif /* End of #ifndef CONFIG_MTK_FPGA */
  1128. /* ############################### */
  1129. /* Enable DCM */
  1130. /* ############################### */
  1131. WRAP_WR32(PMIC_WRAP_DCM_EN, 3); /* enable CRC DCM and PWRAP DCM */
  1132. WRAP_WR32(PMIC_WRAP_DCM_DBC_PRD, DISABLE); /* No debounce */
  1133. /* ############################### */
  1134. /* Reset all SPISLV */
  1135. /* ############################### */
  1136. sub_return = _pwrap_reset_spislv();
  1137. if (sub_return != 0) {
  1138. PWRAPLOG("error,_pwrap_reset_spislv fail,sub_return=%x\n", sub_return);
  1139. return E_PWR_INIT_RESET_SPI;
  1140. }
  1141. /* ############################### */
  1142. /* Enable WACS2 */
  1143. /* ############################### */
  1144. WRAP_WR32(PMIC_WRAP_WRAP_EN, ENABLE); /* enable wrap */
  1145. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, WACS2); /* Only WACS2 */
  1146. WRAP_WR32(PMIC_WRAP_WACS2_EN, ENABLE);
  1147. /* ############################### */
  1148. /* Input data calibration flow */
  1149. /* ############################### */
  1150. sub_return = _pwrap_init_sistrobe();
  1151. if (sub_return != 0) {
  1152. PWRAPLOG("error,DrvPWRAP_InitSiStrobe fail,sub_return=%x\n", sub_return);
  1153. return E_PWR_INIT_SIDLY;
  1154. }
  1155. /* ############################### */
  1156. /* SPI Waveform Configuration */
  1157. /* ############################### */
  1158. /* 0:safe mode, 1:18MHz */
  1159. sub_return = _pwrap_init_reg_clock(1);
  1160. if (sub_return != 0) {
  1161. PWRAPLOG("error,_pwrap_init_reg_clock fail,sub_return=%x\n", sub_return);
  1162. return E_PWR_INIT_REG_CLOCK;
  1163. }
  1164. /* ############################### */
  1165. /* Enable DIO mode */
  1166. /* ############################### */
  1167. sub_return = _pwrap_init_dio(1);
  1168. if (sub_return != 0) {
  1169. PWRAPLOG("_pwrap_init_dio test error,error code=%x, sub_return=%x\n", 0x11,
  1170. sub_return);
  1171. return E_PWR_INIT_DIO;
  1172. }
  1173. /* ############################### */
  1174. /* Enable Encryption */
  1175. /* ############################### */
  1176. sub_return = _pwrap_init_cipher();
  1177. if (sub_return != 0) {
  1178. PWRAPLOG("Enable Encryption fail, return=%x\n", sub_return);
  1179. return E_PWR_INIT_CIPHER;
  1180. }
  1181. /* ############################### */
  1182. /* Write test using WACS2 */
  1183. /* ############################### */
  1184. #ifdef SLV_6351
  1185. sub_return = pwrap_write_nochk(MT6351_DEW_WRITE_TEST, MT6351_WRITE_TEST_VALUE);
  1186. sub_return1 = pwrap_read_nochk(MT6351_DEW_WRITE_TEST, &rdata);
  1187. if (rdata != MT6351_WRITE_TEST_VALUE) {
  1188. PWRAPLOG
  1189. ("write test error,rdata=0x%x,exp=0xa55a,sub_return=0x%x,sub_return1=0x%x\n",
  1190. rdata, sub_return, sub_return1);
  1191. return E_PWR_INIT_WRITE_TEST;
  1192. }
  1193. #endif
  1194. /* ############################### */
  1195. /* Status update function initialization */
  1196. /* 1. Signature Checking using CRC (CRC 0 only) */
  1197. /* 2. EINT update */
  1198. /* 3. Read back Auxadc thermal data for GPD */
  1199. /* 4. Read back Auxadc thermal data for LTE */
  1200. /* ############################### */
  1201. #if 0
  1202. sub_return = pwrap_write_nochk(MT6351_DEW_CRC_EN, ENABLE);
  1203. if (sub_return != 0) {
  1204. PWRAPLOG("Enable CRC fail, return=%x\n", sub_return);
  1205. return E_PWR_INIT_ENABLE_CRC;
  1206. }
  1207. WRAP_WR32(PMIC_WRAP_WACS2_EN, MT6351_DEW_CRC_VAL);
  1208. #endif
  1209. WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN, 0x75);
  1210. #if 0
  1211. WRAP_WR32(PMIC_WRAP_CRC_EN, ENABLE);
  1212. #endif
  1213. WRAP_WR32(PMIC_WRAP_EINT_STA0_ADR, MT6351_INT_STA);
  1214. WRAP_WR32(PMIC_WRAP_ADC_CMD_ADDR, MT6351_AUXADC_RQST1_SET);
  1215. WRAP_WR32(PMIC_WRAP_PWRAP_ADC_CMD, 0x0100);
  1216. WRAP_WR32(PMIC_WRAP_ADC_RDATA_ADDR, MT6351_AUXADC_ADC16);
  1217. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR_LATEST, MT6351_AUXADC_ADC32);
  1218. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR_WP, MT6351_AUXADC_MDBG_1);
  1219. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR0, MT6351_AUXADC_BUF0);
  1220. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR1, MT6351_AUXADC_BUF1);
  1221. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR2, MT6351_AUXADC_BUF2);
  1222. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR3, MT6351_AUXADC_BUF3);
  1223. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR4, MT6351_AUXADC_BUF4);
  1224. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR5, MT6351_AUXADC_BUF5);
  1225. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR6, MT6351_AUXADC_BUF6);
  1226. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR7, MT6351_AUXADC_BUF7);
  1227. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR8, MT6351_AUXADC_BUF8);
  1228. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR9, MT6351_AUXADC_BUF9);
  1229. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR10, MT6351_AUXADC_BUF10);
  1230. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR11, MT6351_AUXADC_BUF11);
  1231. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR12, MT6351_AUXADC_BUF12);
  1232. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR13, MT6351_AUXADC_BUF13);
  1233. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR14, MT6351_AUXADC_BUF14);
  1234. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR15, MT6351_AUXADC_BUF15);
  1235. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR16, MT6351_AUXADC_BUF16);
  1236. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR17, MT6351_AUXADC_BUF17);
  1237. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR18, MT6351_AUXADC_BUF18);
  1238. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR19, MT6351_AUXADC_BUF19);
  1239. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR20, MT6351_AUXADC_BUF20);
  1240. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR21, MT6351_AUXADC_BUF21);
  1241. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR22, MT6351_AUXADC_BUF22);
  1242. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR23, MT6351_AUXADC_BUF23);
  1243. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR24, MT6351_AUXADC_BUF24);
  1244. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR25, MT6351_AUXADC_BUF25);
  1245. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR26, MT6351_AUXADC_BUF26);
  1246. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR27, MT6351_AUXADC_BUF27);
  1247. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR28, MT6351_AUXADC_BUF28);
  1248. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR29, MT6351_AUXADC_BUF29);
  1249. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR30, MT6351_AUXADC_BUF30);
  1250. WRAP_WR32(PMIC_WRAP_MD_ADC_RDATA_ADDR31, MT6351_AUXADC_BUF31);
  1251. /* ############################### */
  1252. /* PMIC_WRAP starvation setting */
  1253. /* ############################### */
  1254. /* latency is smaller than 10us, pop up to group"1" first */
  1255. WRAP_WR32(PMIC_WRAP_HARB_HPRIO, 0x0007); /* [0]:MD_HW [1]:C2K_HW [2]:MD_DVFS _HW */
  1256. /* starvation enable */
  1257. /* [0]=MD_HW, latency= 2.6us, target= (2.6/1.2)-1=1 */
  1258. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_0, 0x0402); /* [10]: enable starvation, [9:0]: target setting */
  1259. /* [1]=C2K_HW, latency= 2.6us, target= (2.6/1.2)-1=1 */
  1260. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_1, 0x0402); /* [10]: enable starvation, [9:0]: target setting */
  1261. /* [2]=MD_DVFS_HW,latency= 5.2us, target= (5.2/1.2)-1=3 */
  1262. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_2, 0x0403); /* [10]: enable starvation, [9:0]: target setting */
  1263. /* [3]=WACS0(MD_SW0), latency=40us, target= (40/1.2)-1=32 */
  1264. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_3, 0x0420); /* [10]: enable starvation, [9:0]: target setting */
  1265. /* [4]=SPM_HW, latency=40us, target= (40/1.2)-1=32 */
  1266. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_4, 0x0420); /* [10]: enable starvation, [9:0]: target setting */
  1267. /* [5]=WACS3(C2K_SW), latency=40us, target= (40/1.2)-1=32 */
  1268. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_5, 0x0420); /* [10]: enable starvation, [9:0]: target setting */
  1269. /* [6]=DCXO_CONNINF, latency=50us, target= (50/1.2)-1=40 */
  1270. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_6, 0x0428); /* [10]: enable starvation, [9:0]: target setting */
  1271. /* [7]=DCXO_NFCINF, latency=50us, target= (50/1.2)-1=40 */
  1272. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_7, 0x0428); /* [10]: enable starvation, [9:0]: target setting */
  1273. /* [8]=MD_ADCINF, latency=29us, target= (29/1.2)-1= 23 */
  1274. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_8, 0x0417); /* [10]: enable starvation, [9:0]: target setting */
  1275. /* [9]=STAUPD, latency=428us, target= (428/1.2)-1=355 */
  1276. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_9, 0x0563); /* [10]: enable starvation, [9:0]: target setting */
  1277. /* [10]=GPS_HW, latency=150us, target= (150/1.2)-1=124 */
  1278. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_10, 0x047C); /* [10]: enable starvation, [9:0]: target setting */
  1279. /* [11]=WACS2(AP_SW), latency=1000us, target= (1000/1.2)-1=832 */
  1280. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_11, 0x0740); /* [10]: enable starvation, [9:0]: target setting */
  1281. /* [12]=WACS1(MD_SW1), latency=1000us, target= (1000/1.2)-1=832 */
  1282. WRAP_WR32(PMIC_WRAP_STARV_COUNTER_12, 0x0740); /* [10]: enable starvation, [9:0]: target setting */
  1283. /* ############################### */
  1284. /* PMIC_WRAP enables */
  1285. /* ############################### */
  1286. WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN, 0x01fff);
  1287. WRAP_WR32(PMIC_WRAP_WACS0_EN, ENABLE);
  1288. WRAP_WR32(PMIC_WRAP_WACS1_EN, ENABLE);
  1289. WRAP_WR32(PMIC_WRAP_WACS3_EN, ENABLE);
  1290. WRAP_WR32(PMIC_WRAP_STAUPD_PRD, 0x5); /* 100us */
  1291. WRAP_WR32(PMIC_WRAP_WDT_UNIT, 0xf);
  1292. WRAP_WR32(PMIC_WRAP_WDT_SRC_EN, 0xffffffff);
  1293. WRAP_WR32(PMIC_WRAP_TIMER_EN, 0x1);
  1294. /* WRAP_WR32(PMIC_WRAP_INT0_EN, 0xffffffff); */
  1295. WRAP_WR32(PMIC_WRAP_INT0_EN, 0xfffffffD); /* FIX ME */
  1296. WRAP_WR32(PMIC_WRAP_INT1_EN, 0x0001ffff);
  1297. /* ############################### */
  1298. /* Initialization Done */
  1299. /* ############################### */
  1300. WRAP_WR32(PMIC_WRAP_INIT_DONE0, ENABLE); /* MD SW0 */
  1301. WRAP_WR32(PMIC_WRAP_INIT_DONE1, ENABLE); /* MD SW1 */
  1302. WRAP_WR32(PMIC_WRAP_INIT_DONE2, ENABLE); /* AP SW */
  1303. WRAP_WR32(PMIC_WRAP_INIT_DONE3, ENABLE); /* C2K SW */
  1304. #ifdef PMIC_WRAP_KERNEL_DRIVER
  1305. #ifdef CONFIG_OF
  1306. pwrap_of_iounmap();
  1307. #endif
  1308. #endif
  1309. return 0;
  1310. }
  1311. #ifdef PMIC_WRAP_KERNEL_DRIVER
  1312. static irqreturn_t mt_pmic_wrap_irq(int irqno, void *dev_id)
  1313. {
  1314. unsigned long flags = 0;
  1315. PWRAPFUC();
  1316. PWRAPREG("dump pwrap register\n");
  1317. spin_lock_irqsave(&wrp_lock_isr, flags);
  1318. /* *----------------------------------------------------------------------- */
  1319. pwrap_dump_all_register();
  1320. /* raise the priority of WACS2 for AP */
  1321. WRAP_WR32(PMIC_WRAP_HARB_HPRIO, WACS2);
  1322. /* *----------------------------------------------------------------------- */
  1323. PWRAPREG("INT flag 0x%x\n", WRAP_RD32(PMIC_WRAP_INT0_FLG_RAW));
  1324. PWRAPREG("INT flag 0x%x\n", WRAP_RD32(PMIC_WRAP_INT1_FLG_RAW));
  1325. /* clear interrupt flag */
  1326. WRAP_WR32(PMIC_WRAP_INT0_CLR, 0xffffffff);
  1327. WRAP_WR32(PMIC_WRAP_INT1_CLR, 0xffffffff);
  1328. /*BUG_ON(1);*/
  1329. spin_unlock_irqrestore(&wrp_lock_isr, flags);
  1330. return IRQ_HANDLED;
  1331. }
  1332. static u32 pwrap_read_test(void)
  1333. {
  1334. u32 rdata = 0;
  1335. u32 return_value = 0;
  1336. /* Read Test */
  1337. #ifdef SLV_6351
  1338. return_value = pwrap_read(MT6351_DEW_READ_TEST, &rdata);
  1339. if (rdata != MT6351_DEFAULT_VALUE_READ_TEST) {
  1340. PWRAPREG("Read Test fail,rdata=0x%x, exp=0x5aa5,return_value=0x%x\n", rdata,
  1341. return_value);
  1342. return E_PWR_READ_TEST_FAIL;
  1343. } else {
  1344. PWRAPREG("Read Test pass,return_value=%d\n", return_value);
  1345. /* return 0; */
  1346. }
  1347. #endif
  1348. return 0;
  1349. }
  1350. static u32 pwrap_write_test(void)
  1351. {
  1352. u32 rdata = 0;
  1353. u32 sub_return = 0;
  1354. u32 sub_return1 = 0;
  1355. /* ############################### */
  1356. /* Write test using WACS2 */
  1357. /* ############################### */
  1358. #ifdef SLV_6351
  1359. sub_return = pwrap_write(MT6351_DEW_WRITE_TEST, MT6351_WRITE_TEST_VALUE);
  1360. PWRAPREG("after MT6351 pwrap_write\n");
  1361. sub_return1 = pwrap_read(MT6351_DEW_WRITE_TEST, &rdata);
  1362. if ((rdata != MT6351_WRITE_TEST_VALUE) || (sub_return != 0) || (sub_return1 != 0)) {
  1363. PWRAPREG
  1364. ("write test error,rdata=0x%x,exp=0xa55a,sub_return=0x%x,sub_return1=0x%x\n",
  1365. rdata, sub_return, sub_return1);
  1366. return E_PWR_INIT_WRITE_TEST;
  1367. } else {
  1368. PWRAPREG("write MT6351 Test pass\n");
  1369. /* return 0; */
  1370. }
  1371. #endif
  1372. return 0;
  1373. }
  1374. static void pwrap_int_test(void)
  1375. {
  1376. u32 rdata1 = 0;
  1377. u32 rdata2 = 0;
  1378. while (1) {
  1379. #ifdef SLV_6351
  1380. rdata1 = WRAP_RD32(PMIC_WRAP_EINT_STA);
  1381. pwrap_read(MT6351_INT_STA, &rdata2);
  1382. PWRAPREG
  1383. ("Pwrap INT status check,PMIC_WRAP_EINT_STA=0x%x,MT6351_INT_STA[0x01B4]=0x%x\n",
  1384. rdata1, rdata2);
  1385. #endif
  1386. msleep(500);
  1387. }
  1388. }
  1389. #ifndef USER_BUILD_KERNEL
  1390. static void pwrap_read_reg_on_pmic(u32 reg_addr)
  1391. {
  1392. u32 reg_value = 0;
  1393. u32 return_value = 0;
  1394. /* PWRAPFUC(); */
  1395. return_value = pwrap_read(reg_addr, &reg_value);
  1396. PWRAPREG("0x%x=0x%x,return_value=%x\n", reg_addr, reg_value, return_value);
  1397. }
  1398. static void pwrap_write_reg_on_pmic(u32 reg_addr, u32 reg_value)
  1399. {
  1400. u32 return_value = 0;
  1401. PWRAPREG("write 0x%x to register 0x%x\n", reg_value, reg_addr);
  1402. return_value = pwrap_write(reg_addr, reg_value);
  1403. return_value = pwrap_read(reg_addr, &reg_value);
  1404. /* PWRAPFUC(); */
  1405. PWRAPREG("the result:0x%x=0x%x,return_value=%x\n", reg_addr, reg_value, return_value);
  1406. }
  1407. #endif
  1408. static void pwrap_ut(u32 ut_test)
  1409. {
  1410. switch (ut_test) {
  1411. case 1:
  1412. /* pwrap_wacs2_para_test(); */
  1413. pwrap_write_test();
  1414. break;
  1415. case 2:
  1416. /* pwrap_wacs2_para_test(); */
  1417. pwrap_read_test();
  1418. break;
  1419. default:
  1420. PWRAPREG("default test.\n");
  1421. break;
  1422. }
  1423. return;
  1424. }
  1425. /*---------------------------------------------------------------------------*/
  1426. static s32 mt_pwrap_show_hal(char *buf)
  1427. {
  1428. PWRAPFUC();
  1429. return snprintf(buf, PAGE_SIZE, "%s\n", "no implement");
  1430. }
  1431. /*---------------------------------------------------------------------------*/
  1432. static s32 mt_pwrap_store_hal(const char *buf, size_t count)
  1433. {
  1434. #ifndef USER_BUILD_KERNEL
  1435. u32 reg_value = 0;
  1436. u32 reg_addr = 0;
  1437. #endif
  1438. u32 return_value = 0;
  1439. u32 ut_test = 0;
  1440. if (!strncmp(buf, "-h", 2)) {
  1441. PWRAPREG
  1442. ("PWRAP debug: [-dump_reg][-trace_wacs2][-init][-rdpmic][-wrpmic][-readtest][-writetest]\n");
  1443. PWRAPREG("PWRAP UT: [1][2]\n");
  1444. }
  1445. /* ----------pwrap debug---------- */
  1446. else if (!strncmp(buf, "-dump_reg", 9)) {
  1447. pwrap_dump_all_register();
  1448. } else if (!strncmp(buf, "-trace_wacs2", 12)) {
  1449. /* pwrap_trace_wacs2(); */
  1450. } else if (!strncmp(buf, "-init", 5)) {
  1451. return_value = pwrap_init();
  1452. if (return_value == 0)
  1453. PWRAPREG("pwrap_init pass,return_value=%d\n", return_value);
  1454. else
  1455. PWRAPREG("pwrap_init fail,return_value=%d\n", return_value);
  1456. }
  1457. #ifndef USER_BUILD_KERNEL
  1458. else if (!strncmp(buf, "-rdpmic", 7) && (1 == sscanf(buf + 7, "%x", &reg_addr))) {
  1459. pwrap_read_reg_on_pmic(reg_addr);
  1460. } else if (!strncmp(buf, "-wrpmic", 7)
  1461. && (2 == sscanf(buf + 7, "%x %x", &reg_addr, &reg_value))) {
  1462. pwrap_write_reg_on_pmic(reg_addr, reg_value);
  1463. }
  1464. #endif
  1465. else if (!strncmp(buf, "-readtest", 9)) {
  1466. pwrap_read_test();
  1467. } else if (!strncmp(buf, "-writetest", 10)) {
  1468. pwrap_write_test();
  1469. } else if (!strncmp(buf, "-int", 4)) {
  1470. pwrap_int_test();
  1471. }
  1472. /* ----------pwrap UT---------- */
  1473. else if (!strncmp(buf, "-ut", 3) && (1 == sscanf(buf + 3, "%d", &ut_test))) {
  1474. pwrap_ut(ut_test);
  1475. } else {
  1476. PWRAPREG("wrong parameter\n");
  1477. }
  1478. return count;
  1479. }
  1480. /*---------------------------------------------------------------------------*/
  1481. #endif /* End of #endif PMIC_WRAP_KERNEL_DRIVER */
  1482. #ifdef PMIC_WRAP_PRELOADER_DRIVER
  1483. s32 pwrap_init_preloader(void)
  1484. {
  1485. u32 pwrap_ret = 0, i = 0;
  1486. PWRAPFUC();
  1487. for (i = 0; i < 3; i++) {
  1488. pwrap_ret = pwrap_init();
  1489. if (pwrap_ret != 0) {
  1490. printf("[PMIC_WRAP]wrap_init fail,the return value=%x.\n", pwrap_ret);
  1491. } else {
  1492. printf("[PMIC_WRAP]wrap_init pass,the return value=%x.\n", pwrap_ret);
  1493. break; /* init pass */
  1494. }
  1495. }
  1496. #ifdef PWRAP_PRELOADER_PORTING
  1497. /* pwrap_init_for_early_porting(); */
  1498. #endif
  1499. return 0;
  1500. }
  1501. #ifdef PWRAP_PRELOADER_PORTING
  1502. /* -------------------------------------------------------- */
  1503. /* Function : _pwrap_status_update_test() */
  1504. /* Description :only for early porting */
  1505. /* Parameter : */
  1506. /* Return : */
  1507. /* -------------------------------------------------------- */
  1508. static s32 _pwrap_status_update_test_porting(void)
  1509. {
  1510. /* u32 i, j; */
  1511. u32 rdata;
  1512. volatile u32 delay = 1000 * 1000 * 1;
  1513. PWRAPFUC();
  1514. /* disable signature interrupt */
  1515. WRAP_WR32(PMIC_WRAP_INT_EN, 0x0);
  1516. #ifdef SLV_6351
  1517. pwrap_write(MT6351_DEW_WRITE_TEST, MT6351_WRITE_TEST_VALUE);
  1518. WRAP_WR32(PMIC_WRAP_SIG_ADR, MT6351_DEW_WRITE_TEST);
  1519. WRAP_WR32(PMIC_WRAP_SIG_VALUE, 0xAA55);
  1520. #endif
  1521. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x1);
  1522. while (delay--)
  1523. ;
  1524. rdata = WRAP_RD32(PMIC_WRAP_SIG_ERRVAL);
  1525. #ifdef SLV_6351
  1526. if ((rdata & 0xFFFF) != MT6351_WRITE_TEST_VALUE) {
  1527. PWRAPLOG("MT6351 _pwrap_status_update_test error,error code=%x, rdata=%x\n", 1,
  1528. (rdata & 0xFFFF));
  1529. /* return 1; */
  1530. }
  1531. #endif
  1532. #ifdef SLV_6351
  1533. WRAP_WR32(PMIC_WRAP_SIG_VALUE, MT6351_WRITE_TEST_VALUE); /* tha same as write test */
  1534. #endif
  1535. /* clear sig_error interrupt flag bit */
  1536. WRAP_WR32(PMIC_WRAP_INT_CLR, 1 << 1);
  1537. /* enable signature interrupt */
  1538. WRAP_WR32(PMIC_WRAP_INT_EN, 0x7ffffff9);
  1539. WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x0);
  1540. #ifdef SLV_6351
  1541. WRAP_WR32(PMIC_WRAP_SIG_ADR, MT6351_DEW_CRC_VAL);
  1542. #endif
  1543. return 0;
  1544. }
  1545. void pwrap_init_for_early_porting(void)
  1546. {
  1547. int ret = 0;
  1548. u32 res = 0;
  1549. PWRAPFUC();
  1550. ret = _pwrap_status_update_test_porting();
  1551. if (ret == 0) {
  1552. PWRAPLOG("wrapper_StatusUpdateTest pass.\n");
  1553. } else {
  1554. PWRAPLOG("error:wrapper_StatusUpdateTest fail.\n");
  1555. res += 1;
  1556. }
  1557. }
  1558. #endif /* PWRAP_PRELOADER_PORTING */
  1559. #endif /* End of #ifdef PMIC_WRAP_PRELOADER_DRIVER */
  1560. #ifdef PMIC_WRAP_LK_DRIVER
  1561. s32 pwrap_init_lk(void)
  1562. {
  1563. u32 pwrap_ret = 0, i = 0;
  1564. PWRAPFUC();
  1565. for (i = 0; i < 3; i++) {
  1566. pwrap_ret = pwrap_init();
  1567. if (pwrap_ret != 0) {
  1568. printf("[PMIC_WRAP]wrap_init fail,the return value=%x.\n", pwrap_ret);
  1569. } else {
  1570. printf("[PMIC_WRAP]wrap_init pass,the return value=%x.\n", pwrap_ret);
  1571. break; /* init pass */
  1572. }
  1573. }
  1574. #ifdef PWRAP_PRELOADER_PORTING
  1575. /* pwrap_init_for_early_porting(); */
  1576. #endif
  1577. return 0;
  1578. }
  1579. #endif
  1580. #endif /* End of #if PMIC_WRAP_NO_PMIC */
  1581. #ifdef PMIC_WRAP_KERNEL_DRIVER
  1582. #ifdef CONFIG_OF
  1583. static int pwrap_of_iomap(void)
  1584. {
  1585. /*
  1586. * Map the address of the following register base:
  1587. * TOPCKGEN, INFRACFG_AO, GPIO, SLEEP
  1588. */
  1589. struct device_node *topckgen_node;
  1590. struct device_node *infracfg_ao_node;
  1591. struct device_node *gpio_node;
  1592. struct device_node *sleep_node;
  1593. topckgen_node = of_find_compatible_node(NULL, NULL, "mediatek,TOPCKGEN");
  1594. if (!topckgen_node) {
  1595. pr_info("get TOPCKGEN failed\n");
  1596. return -ENODEV;
  1597. }
  1598. topckgen_reg_base = of_iomap(topckgen_node, 0);
  1599. if (!topckgen_reg_base) {
  1600. pr_info("TOPCKGEN iomap failed\n");
  1601. return -ENOMEM;
  1602. }
  1603. infracfg_ao_node = of_find_compatible_node(NULL, NULL, "mediatek,INFRACFG_AO");
  1604. if (!infracfg_ao_node) {
  1605. pr_info("get INFRACFG_AO failed\n");
  1606. return -ENODEV;
  1607. }
  1608. infracfg_ao_reg_base = of_iomap(infracfg_ao_node, 0);
  1609. if (!infracfg_ao_reg_base) {
  1610. pr_info("INFRACFG_AO iomap failed\n");
  1611. return -ENOMEM;
  1612. }
  1613. gpio_node = of_find_compatible_node(NULL, NULL, "mediatek,GPIO");
  1614. if (!gpio_node) {
  1615. pr_info("get GPIO failed\n");
  1616. return -ENODEV;
  1617. }
  1618. gpio_reg_base = of_iomap(gpio_node, 0);
  1619. if (!gpio_reg_base) {
  1620. pr_info("GPIO iomap failed\n");
  1621. return -ENOMEM;
  1622. }
  1623. sleep_node = of_find_compatible_node(NULL, NULL, "mediatek,SLEEP");
  1624. if (!sleep_node) {
  1625. pr_info("get SLEEP failed\n");
  1626. return -ENODEV;
  1627. }
  1628. sleep_reg_base = of_iomap(sleep_node, 0);
  1629. if (!sleep_reg_base) {
  1630. pr_info("SLEEp iomap failed\n");
  1631. return -ENOMEM;
  1632. }
  1633. pr_info("TOPCKGEN reg: 0x%p\n", topckgen_reg_base);
  1634. pr_info("INFRACFG_AO reg: 0x%p\n", infracfg_ao_reg_base);
  1635. pr_info("GPIO reg: 0x%p\n", gpio_reg_base);
  1636. pr_info("SLEEP reg: 0x%p\n", sleep_reg_base);
  1637. return 0;
  1638. }
  1639. static void pwrap_of_iounmap(void)
  1640. {
  1641. iounmap(topckgen_reg_base);
  1642. iounmap(infracfg_ao_reg_base);
  1643. iounmap(gpio_reg_base);
  1644. iounmap(sleep_reg_base);
  1645. }
  1646. #endif
  1647. #define VERSION "Revision"
  1648. static int is_pwrap_init_done(void)
  1649. {
  1650. int ret = 0;
  1651. ret = WRAP_RD32(PMIC_WRAP_INIT_DONE2);
  1652. if (ret != 0)
  1653. return 0;
  1654. ret = pwrap_init();
  1655. if (ret != 0) {
  1656. PWRAPERR("init error (%d)\n", ret);
  1657. pwrap_dump_all_register();
  1658. return ret;
  1659. }
  1660. PWRAPLOG("init successfully done (%d)\n\n", ret);
  1661. return ret;
  1662. }
  1663. static int __init pwrap_hal_init(void)
  1664. {
  1665. s32 ret = 0;
  1666. #ifdef CONFIG_OF
  1667. u32 pwrap_irq;
  1668. struct device_node *pwrap_node;
  1669. pwrap_node = of_find_compatible_node(NULL, NULL, "mediatek,PWRAP");
  1670. if (!pwrap_node) {
  1671. pr_info("PWRAP get node failed\n");
  1672. return -ENODEV;
  1673. }
  1674. pwrap_base = of_iomap(pwrap_node, 0);
  1675. if (!pwrap_base) {
  1676. pr_info("PWRAP iomap failed\n");
  1677. return -ENOMEM;
  1678. }
  1679. pwrap_irq = irq_of_parse_and_map(pwrap_node, 0);
  1680. if (!pwrap_irq) {
  1681. pr_info("PWRAP get irq fail\n");
  1682. return -ENODEV;
  1683. }
  1684. pr_info("PWRAP reg: 0x%p, irq: %d\n", pwrap_base, pwrap_irq);
  1685. #endif
  1686. PWRAPLOG("HAL init: version %s\n", VERSION);
  1687. mt_wrp = get_mt_pmic_wrap_drv();
  1688. mt_wrp->store_hal = mt_pwrap_store_hal;
  1689. mt_wrp->show_hal = mt_pwrap_show_hal;
  1690. mt_wrp->wacs2_hal = pwrap_wacs2_hal;
  1691. if (is_pwrap_init_done() == 0) {
  1692. #ifdef PMIC_WRAP_NO_PMIC
  1693. #else
  1694. ret =
  1695. request_irq(MT_PMIC_WRAP_IRQ_ID, mt_pmic_wrap_irq, IRQF_TRIGGER_HIGH,
  1696. PMIC_WRAP_DEVICE, 0);
  1697. #endif
  1698. if (ret) {
  1699. PWRAPERR("register IRQ failed (%d)\n", ret);
  1700. return ret;
  1701. }
  1702. } else {
  1703. PWRAPERR("not init (%d)\n", ret);
  1704. }
  1705. /* PWRAPERR("not init (%x)\n", is_pwrap_init_done); */
  1706. return ret;
  1707. }
  1708. postcore_initcall(pwrap_hal_init);
  1709. #endif /* End of #ifdef PMIC_WRAP_KERNEL_DRIVER */