1 /******************************************************************************
2 * pwrap_hal.c - Linux pmic_wrapper Driver,hardware_dependent driver
6 * This file provid the other drivers PMIC wrapper relative functions
8 ******************************************************************************/
10 #include <linux/spinlock.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
13 #include <linux/sched.h>
14 #include <mach/mt_typedefs.h>
15 #include <linux/timer.h>
16 #include <mach/mt_pmic_wrap.h>
18 #include "pwrap_hal.h"
20 #define PMIC_WRAP_DEVICE "pmic_wrap"
22 static struct mt_pmic_wrap_driver
*mt_wrp
;
24 static spinlock_t wrp_lock
= __SPIN_LOCK_UNLOCKED(lock
);
25 //----------interral API ------------------------
26 static S32
_pwrap_init_dio( U32 dio_en
);
27 static S32
_pwrap_init_cipher( void );
28 static S32
_pwrap_init_reg_clock( U32 regck_sel
);
30 static S32
_pwrap_wacs2_nochk( U32 write
, U32 adr
, U32 wdata
, U32
*rdata
);
31 static S32
pwrap_write_nochk( U32 adr
, U32 wdata
);
32 static S32
pwrap_read_nochk( U32 adr
, U32
*rdata
);
34 /*-pwrap debug--------------------------------------------------------------------------*/
35 static inline void pwrap_dump_ap_register(void)
38 PWRAPREG("dump pwrap register, base=0x%x\n",PMIC_WRAP_BASE
);
39 PWRAPREG("address : 3 2 1 0 7 6 5 4 B A 9 8 F E D C \n");
40 for(i
=0;i
<=0x150;i
+=16)
42 PWRAPREG("offset 0x%.3x:0x%.8x 0x%.8x 0x%.8x 0x%.8x \n",i
,
43 WRAP_RD32(PMIC_WRAP_BASE
+i
+0),
44 WRAP_RD32(PMIC_WRAP_BASE
+i
+4),
45 WRAP_RD32(PMIC_WRAP_BASE
+i
+8),
46 WRAP_RD32(PMIC_WRAP_BASE
+i
+12));
48 //PWRAPREG("elapse_time=%llx(ns)\n",elapse_time);
51 static inline void pwrap_dump_pmic_register(void)
57 // PWRAPREG("dump dewrap register\n");
60 // reg_addr=(DEW_BASE+i*4);
61 // reg_value=pwrap_read_nochk(reg_addr,®_value);
62 // PWRAPREG("0x%x=0x%x\n",reg_addr,reg_value);
66 static inline void pwrap_dump_all_register(void)
68 pwrap_dump_ap_register();
69 pwrap_dump_pmic_register();
72 /******************************************************************************
74 ******************************************************************************/
77 static U64
_pwrap_get_current_time(void)
79 return sched_clock(); ///TODO: fix me
83 static BOOL
_pwrap_timeout_ns (U64 start_time_ns
, U64 timeout_time_ns
)
89 cur_time
= _pwrap_get_current_time();//ns
90 if(cur_time
< start_time_ns
){
91 PWRAPERR("@@@@Timer overflow! start%lld cur timer%lld\n",start_time_ns
,cur_time
);
92 start_time_ns
=cur_time
;
93 timeout_time_ns
=255*1000; //255us
94 PWRAPERR("@@@@reset timer! start%lld setting%lld\n",start_time_ns
,timeout_time_ns
);
96 elapse_time
=cur_time
-start_time_ns
;
99 if (timeout_time_ns
<= elapse_time
)
106 static U64
_pwrap_time2ns (U64 time_us
)
112 static U64
_pwrap_get_current_time(void)
116 static BOOL
_pwrap_timeout_ns (U64 start_time_ns
, U64 elapse_time
)//,U64 timeout_ns)
120 static U64
_pwrap_time2ns (U64 time_us
)
126 //#####################################################################
127 //define macro and inline function (for do while loop)
128 //#####################################################################
129 typedef U32 (*loop_condition_fp
)(U32
);//define a function pointer
131 static inline U32
wait_for_fsm_idle(U32 x
)
133 return (GET_WACS0_FSM( x
) != WACS_FSM_IDLE
);
135 static inline U32
wait_for_fsm_vldclr(U32 x
)
137 return (GET_WACS0_FSM( x
) != WACS_FSM_WFVLDCLR
);
139 static inline U32
wait_for_sync(U32 x
)
141 return (GET_SYNC_IDLE0(x
) != WACS_SYNC_IDLE
);
143 static inline U32
wait_for_idle_and_sync(U32 x
)
145 return ((GET_WACS0_FSM(x
) != WACS_FSM_IDLE
) || (GET_SYNC_IDLE0(x
) != WACS_SYNC_IDLE
)) ;
147 static inline U32
wait_for_wrap_idle(U32 x
)
149 return ((GET_WRAP_FSM(x
) != 0x0) || (GET_WRAP_CH_DLE_RESTCNT(x
) != 0x0));
151 static inline U32
wait_for_wrap_state_idle(U32 x
)
153 return ( GET_WRAP_AG_DLE_RESTCNT( x
) != 0 ) ;
155 static inline U32
wait_for_man_idle_and_noreq(U32 x
)
157 return ( (GET_MAN_REQ(x
) != MAN_FSM_NO_REQ
) || (GET_MAN_FSM(x
) != MAN_FSM_IDLE
) );
159 static inline U32
wait_for_man_vldclr(U32 x
)
161 return (GET_MAN_FSM( x
) != MAN_FSM_WFVLDCLR
) ;
163 static inline U32
wait_for_cipher_ready(U32 x
)
167 static inline U32
wait_for_stdupd_idle(U32 x
)
169 return ( GET_STAUPD_FSM(x
) != 0x0) ;
172 static inline U32
wait_for_state_ready_init(loop_condition_fp fp
,U32 timeout_us
,U32 wacs_register
,U32
*read_reg
)
175 U64 start_time_ns
=0, timeout_ns
=0;
177 start_time_ns
= _pwrap_get_current_time();
178 timeout_ns
= _pwrap_time2ns(timeout_us
);
181 if (_pwrap_timeout_ns(start_time_ns
, timeout_ns
))
183 PWRAPERR("wait_for_state_ready_init timeout when waiting for idle\n");
184 return E_PWR_WAIT_IDLE_TIMEOUT
;
186 reg_rdata
= WRAP_RD32(wacs_register
);
187 } while( fp(reg_rdata
)); //IDLE State
193 static inline U32
wait_for_state_idle_init(loop_condition_fp fp
,U32 timeout_us
,U32 wacs_register
,U32 wacs_vldclr_register
,U32
*read_reg
)
196 U64 start_time_ns
=0, timeout_ns
=0;
198 start_time_ns
= _pwrap_get_current_time();
199 timeout_ns
= _pwrap_time2ns(timeout_us
);
202 if (_pwrap_timeout_ns(start_time_ns
, timeout_ns
))
204 PWRAPERR("wait_for_state_idle_init timeout when waiting for idle\n");
205 pwrap_dump_ap_register();
206 //pwrap_trace_wacs2();
208 return E_PWR_WAIT_IDLE_TIMEOUT
;
210 reg_rdata
= WRAP_RD32(wacs_register
);
211 //if last read command timeout,clear vldclr bit
212 //read command state machine:FSM_REQ-->wfdle-->WFVLDCLR;write:FSM_REQ-->idle
213 switch ( GET_WACS0_FSM( reg_rdata
) )
215 case WACS_FSM_WFVLDCLR
:
216 WRAP_WR32(wacs_vldclr_register
, 1);
217 PWRAPERR("WACS_FSM = PMIC_WRAP_WACS_VLDCLR\n");
220 PWRAPERR("WACS_FSM = WACS_FSM_WFDLE\n");
223 PWRAPERR("WACS_FSM = WACS_FSM_REQ\n");
228 }while( fp(reg_rdata
)); //IDLE State
233 static inline U32
wait_for_state_idle(loop_condition_fp fp
,U32 timeout_us
,U32 wacs_register
,U32 wacs_vldclr_register
,U32
*read_reg
)
236 U64 start_time_ns
=0, timeout_ns
=0;
238 start_time_ns
= _pwrap_get_current_time();
239 timeout_ns
= _pwrap_time2ns(timeout_us
);
242 if (_pwrap_timeout_ns(start_time_ns
, timeout_ns
))
244 PWRAPERR("wait_for_state_idle timeout when waiting for idle\n");
245 pwrap_dump_ap_register();
246 //pwrap_trace_wacs2();
248 return E_PWR_WAIT_IDLE_TIMEOUT
;
250 reg_rdata
= WRAP_RD32(wacs_register
);
251 if( GET_INIT_DONE0( reg_rdata
) != WACS_INIT_DONE
)
253 PWRAPERR("initialization isn't finished \n");
254 return E_PWR_NOT_INIT_DONE
;
256 //if last read command timeout,clear vldclr bit
257 //read command state machine:FSM_REQ-->wfdle-->WFVLDCLR;write:FSM_REQ-->idle
258 switch ( GET_WACS0_FSM( reg_rdata
) )
260 case WACS_FSM_WFVLDCLR
:
261 WRAP_WR32(wacs_vldclr_register
, 1);
262 PWRAPERR("WACS_FSM = PMIC_WRAP_WACS_VLDCLR\n");
265 PWRAPERR("WACS_FSM = WACS_FSM_WFDLE\n");
268 PWRAPERR("WACS_FSM = WACS_FSM_REQ\n");
273 }while( fp(reg_rdata
)); //IDLE State
279 static inline U32
wait_for_state_ready(loop_condition_fp fp
,U32 timeout_us
,U32 wacs_register
,U32
*read_reg
)
282 U64 start_time_ns
=0, timeout_ns
=0;
284 start_time_ns
= _pwrap_get_current_time();
285 timeout_ns
= _pwrap_time2ns(timeout_us
);
288 if (_pwrap_timeout_ns(start_time_ns
, timeout_ns
))
290 PWRAPERR("timeout when waiting for idle\n");
291 pwrap_dump_ap_register();
292 //pwrap_trace_wacs2();
293 return E_PWR_WAIT_IDLE_TIMEOUT
;
295 reg_rdata
= WRAP_RD32(wacs_register
);
297 if( GET_INIT_DONE0( reg_rdata
) != WACS_INIT_DONE
)
299 PWRAPERR("initialization isn't finished \n");
300 return E_PWR_NOT_INIT_DONE
;
302 } while( fp(reg_rdata
)); //IDLE State
307 /********************************************************************************************/
308 //extern API for PMIC driver, INT related control, this INT is for PMIC chip to AP (72~92 no need this)
309 /********************************************************************************************/
310 U32
mt_pmic_wrap_eint_status(void)
315 void mt_pmic_wrap_eint_clr(int offset
)
320 //--------------------------------------------------------
321 // Function : pwrap_wacs2_hal()
325 //--------------------------------------------------------
326 static S32
pwrap_wacs2_hal( U32 write
, U32 adr
, U32 wdata
, U32
*rdata
)
328 //U64 wrap_access_time=0x0;
334 unsigned long flags
=0;
336 //#ifndef CONFIG_MTK_LDVT_PMIC_WRAP
337 //PWRAPLOG("wrapper access,write=%x,add=%x,wdata=%x,rdata=%x\n",write,adr,wdata,rdata);
339 // Check argument validation
340 if( (write
& ~(0x1)) != 0) return E_PWR_INVALID_RW
;
341 if( (adr
& ~(0xffff)) != 0) return E_PWR_INVALID_ADDR
;
342 if( (wdata
& ~(0xffff)) != 0) return E_PWR_INVALID_WDAT
;
344 spin_lock_irqsave(&wrp_lock
,flags
);
345 // Check IDLE & INIT_DONE in advance
346 return_value
=wait_for_state_idle(wait_for_fsm_idle
,TIMEOUT_WAIT_IDLE
,PMIC_WRAP_WACS2_RDATA
,PMIC_WRAP_WACS2_VLDCLR
,0);
349 PWRAPERR("wait_for_fsm_idle fail,return_value=%d\n",return_value
);
352 wacs_write
= write
<< 31;
353 wacs_adr
= (adr
>> 1) << 16;
354 wacs_cmd
= wacs_write
| wacs_adr
| wdata
;
356 WRAP_WR32(PMIC_WRAP_WACS2_CMD
,wacs_cmd
);
361 PWRAPERR("rdata is a NULL pointer\n");
362 return_value
= E_PWR_INVALID_ARG
;
365 return_value
=wait_for_state_ready(wait_for_fsm_vldclr
,TIMEOUT_READ
,PMIC_WRAP_WACS2_RDATA
,®_rdata
);
368 PWRAPERR("wait_for_fsm_vldclr fail,return_value=%d\n",return_value
);
369 return_value
+=1;//E_PWR_NOT_INIT_DONE_READ or E_PWR_WAIT_IDLE_TIMEOUT_READ
372 *rdata
= GET_WACS0_RDATA( reg_rdata
);
373 WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR
, 1);
376 spin_unlock_irqrestore(&wrp_lock
,flags
);
379 PWRAPERR("pwrap_wacs2_hal fail,return_value=%d\n",return_value
);
380 PWRAPERR("timeout:BUG_ON here\n");
383 //wrap_access_time=sched_clock();
384 //pwrap_trace(wrap_access_time,return_value,write, adr, wdata,(U32)rdata);
388 //S32 pwrap_wacs2( U32 write, U32 adr, U32 wdata, U32 *rdata )
390 // return pwrap_wacs2_hal(write, adr,wdata,rdata );
392 //EXPORT_SYMBOL(pwrap_wacs2);
393 //S32 pwrap_read( U32 adr, U32 *rdata )
395 // return pwrap_wacs2( PWRAP_READ, adr,0,rdata );
397 //EXPORT_SYMBOL(pwrap_read);
399 //S32 pwrap_write( U32 adr, U32 wdata )
401 // return pwrap_wacs2( PWRAP_WRITE, adr,wdata,0 );
403 //EXPORT_SYMBOL(pwrap_write);
404 //******************************************************************************
405 //--internal API for pwrap_init-------------------------------------------------
406 //******************************************************************************
407 //--------------------------------------------------------
408 // Function : _pwrap_wacs2_nochk()
412 //--------------------------------------------------------
414 //static S32 pwrap_read_nochk( U32 adr, U32 *rdata )
415 static S32
pwrap_read_nochk( U32 adr
, U32
*rdata
)
417 return _pwrap_wacs2_nochk( 0, adr
, 0, rdata
);
420 static S32
pwrap_write_nochk( U32 adr
, U32 wdata
)
422 return _pwrap_wacs2_nochk( 1, adr
,wdata
,0 );
425 static S32
_pwrap_wacs2_nochk( U32 write
, U32 adr
, U32 wdata
, U32
*rdata
)
431 U32 return_value
=0x0;
433 // Check argument validation
434 if( (write
& ~(0x1)) != 0) return E_PWR_INVALID_RW
;
435 if( (adr
& ~(0xffff)) != 0) return E_PWR_INVALID_ADDR
;
436 if( (wdata
& ~(0xffff)) != 0) return E_PWR_INVALID_WDAT
;
439 return_value
=wait_for_state_ready_init(wait_for_fsm_idle
,TIMEOUT_WAIT_IDLE
,PMIC_WRAP_WACS2_RDATA
,0);
442 PWRAPERR("_pwrap_wacs2_nochk write command fail,return_value=%x\n", return_value
);
446 wacs_write
= write
<< 31;
447 wacs_adr
= (adr
>> 1) << 16;
448 wacs_cmd
= wacs_write
| wacs_adr
| wdata
;
449 WRAP_WR32(PMIC_WRAP_WACS2_CMD
,wacs_cmd
);
454 return E_PWR_INVALID_ARG
;
455 // wait for read data ready
456 return_value
=wait_for_state_ready_init(wait_for_fsm_vldclr
,TIMEOUT_WAIT_IDLE
,PMIC_WRAP_WACS2_RDATA
,®_rdata
);
459 PWRAPERR("_pwrap_wacs2_nochk read fail,return_value=%x\n", return_value
);
462 *rdata
= GET_WACS0_RDATA( reg_rdata
);
463 WRAP_WR32(PMIC_WRAP_WACS2_VLDCLR
, 1);
467 //--------------------------------------------------------
468 // Function : _pwrap_init_dio()
469 // Description :call it in pwrap_init,mustn't check init done
472 //--------------------------------------------------------
473 static S32
_pwrap_init_dio( U32 dio_en
)
475 U32 arb_en_backup
=0x0;
480 arb_en_backup
= WRAP_RD32(PMIC_WRAP_HIPRIO_ARB_EN
);
481 WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN
,WACS2
); // only WACS2
482 pwrap_write_nochk(DEW_DIO_EN
, dio_en
);
484 // Check IDLE & INIT_DONE in advance
485 return_value
=wait_for_state_ready_init(wait_for_idle_and_sync
,TIMEOUT_WAIT_IDLE
,PMIC_WRAP_WACS2_RDATA
,0);
488 PWRAPERR("_pwrap_init_dio fail,return_value=%x\n", return_value
);
491 WRAP_WR32(PMIC_WRAP_DIO_EN
, dio_en
);
493 pwrap_read_nochk(DEW_READ_TEST
,&rdata
);
494 if( rdata
!= DEFAULT_VALUE_READ_TEST
)
496 PWRAPERR("[Dio_mode][Read Test] fail,dio_en = %x, READ_TEST rdata=%x, exp=0x5aa5\n", dio_en
, rdata
);
497 return E_PWR_READ_TEST_FAIL
;
499 WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN
, arb_en_backup
);
503 //--------------------------------------------------------
504 // Function : _pwrap_init_cipher()
508 //--------------------------------------------------------
509 static S32
_pwrap_init_cipher( void )
514 U32 start_time_ns
=0, timeout_ns
=0;
516 arb_en_backup
= WRAP_RD32(PMIC_WRAP_HIPRIO_ARB_EN
);
518 WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN
,WACS2
); // only WACS2
520 WRAP_WR32(PMIC_WRAP_CIPHER_SWRST
, 1);
521 WRAP_WR32(PMIC_WRAP_CIPHER_SWRST
, 0);
522 WRAP_WR32(PMIC_WRAP_CIPHER_KEY_SEL
, 1);
523 WRAP_WR32(PMIC_WRAP_CIPHER_IV_SEL
, 2);
524 WRAP_WR32(PMIC_WRAP_CIPHER_EN
, 1);
526 //Config CIPHER @ PMIC
527 pwrap_write_nochk(DEW_CIPHER_SWRST
, 0x1);
528 pwrap_write_nochk(DEW_CIPHER_SWRST
, 0x0);
529 pwrap_write_nochk(DEW_CIPHER_KEY_SEL
, 0x1);
530 pwrap_write_nochk(DEW_CIPHER_IV_SEL
, 0x2);
532 pwrap_write_nochk(DEW_CIPHER_LOAD
, 0x1);
533 pwrap_write_nochk(DEW_CIPHER_START
, 0x1);
534 #elif defined SLV_6323
535 pwrap_write_nochk(DEW_CIPHER_EN
, 0x1);
538 //wait for cipher data ready@AP
539 return_value
=wait_for_state_ready_init(wait_for_cipher_ready
,TIMEOUT_WAIT_IDLE
,PMIC_WRAP_CIPHER_RDY
,0);
542 PWRAPERR("wait for cipher data ready@AP fail,return_value=%x\n", return_value
);
546 //wait for cipher data ready@PMIC
547 start_time_ns
= _pwrap_get_current_time();
548 timeout_ns
= _pwrap_time2ns(0xFFFFFF);
551 pwrap_read_nochk(DEW_CIPHER_RDY
,&rdata
);
552 if (_pwrap_timeout_ns(start_time_ns
, timeout_ns
))
554 PWRAPERR("wait for cipher data ready@PMIC\n");
555 //return E_PWR_WAIT_IDLE_TIMEOUT;
557 } while( rdata
!= 0x1 ); //cipher_ready
559 pwrap_write_nochk(DEW_CIPHER_MODE
, 0x1);
560 //wait for cipher mode idle
561 return_value
=wait_for_state_ready_init(wait_for_idle_and_sync
,TIMEOUT_WAIT_IDLE
,PMIC_WRAP_WACS2_RDATA
,0);
564 PWRAPERR("wait for cipher mode idle fail,return_value=%x\n", return_value
);
567 WRAP_WR32(PMIC_WRAP_CIPHER_MODE
, 1);
570 pwrap_read_nochk(DEW_READ_TEST
,&rdata
);
571 if( rdata
!= DEFAULT_VALUE_READ_TEST
)
573 PWRAPERR("_pwrap_init_cipher,read test error,error code=%x, rdata=%x\n", 1, rdata
);
574 return E_PWR_READ_TEST_FAIL
;
576 WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN
, arb_en_backup
);
580 //--------------------------------------------------------
581 // Function : _pwrap_init_sistrobe()
582 // Description : Initialize SI_CK_CON and SIDLY
585 //--------------------------------------------------------
586 static S32
_pwrap_init_sistrobe( void )
593 U32 leading_one
, tailing_one
;
595 arb_en_backup
= WRAP_RD32(PMIC_WRAP_HIPRIO_ARB_EN
);
597 WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN
,WACS2
); // only WACS2
599 //---------------------------------------------------------------------
600 // Scan all possible input strobe by READ_TEST
601 //---------------------------------------------------------------------
604 for( ind
=0; ind
<24; ind
++) // 24 sampling clock edge
606 WRAP_WR32(PMIC_WRAP_SI_CK_CON
, (ind
>> 2) & 0x7);
607 WRAP_WR32(PMIC_WRAP_SIDLY
,0x3 - (ind
& 0x3));
608 _pwrap_wacs2_nochk(0, DEW_READ_TEST
, 0, &rdata
);
609 if( rdata
== DEFAULT_VALUE_READ_TEST
)
611 PWRAPLOG("_pwrap_init_sistrobe [Read Test] pass,index=%d rdata=%x\n", ind
,rdata
);
612 result
|= (0x1 << ind
);
615 PWRAPLOG("_pwrap_init_sistrobe [Read Test] fail,index=%d,rdata=%x\n", ind
,rdata
);
618 //---------------------------------------------------------------------
619 // Locate the leading one and trailing one
620 //---------------------------------------------------------------------
621 for( ind
=23 ; ind
>=0 ; ind
-- )
623 if( result
& (0x1 << ind
) ) break;
627 for( ind
=0 ; ind
<24 ; ind
++ )
629 if( result
& (0x1 << ind
) ) break;
633 //---------------------------------------------------------------------
634 // Check the continuity of pass range
635 //---------------------------------------------------------------------
636 tmp1
= (0x1 << (leading_one
+1)) - 1;
637 tmp2
= (0x1 << tailing_one
) - 1;
638 if( (tmp1
- tmp2
) != result
)
640 /*TERR = "[DrvPWRAP_InitSiStrobe] Fail, tmp1:%d, tmp2:%d", tmp1, tmp2*/
641 PWRAPERR("_pwrap_init_sistrobe Fail,tmp1=%x,tmp2=%x\n", tmp1
,tmp2
);
644 //---------------------------------------------------------------------
645 // Config SICK and SIDLY to the middle point of pass range
646 //---------------------------------------------------------------------
647 ind
= (leading_one
+ tailing_one
)/2;
648 WRAP_WR32(PMIC_WRAP_SI_CK_CON
, (ind
>> 2) & 0x7);
649 WRAP_WR32(PMIC_WRAP_SIDLY
, 0x3 - (ind
& 0x3));
651 //---------------------------------------------------------------------
653 //---------------------------------------------------------------------
654 WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN
, arb_en_backup
);
655 if( result_faulty
== 0 )
659 /*TERR = "[DrvPWRAP_InitSiStrobe] Fail, result = %x", result*/
660 PWRAPERR("_pwrap_init_sistrobe Fail,result=%x\n", result
);
661 return result_faulty
;
665 //--------------------------------------------------------
666 // Function : _pwrap_reset_spislv()
670 //--------------------------------------------------------
672 static S32
_pwrap_reset_spislv( void )
677 // This driver does not using _pwrap_switch_mux
678 // because the remaining requests are expected to fail anyway
680 WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN
, DISABLE_ALL
);
681 WRAP_WR32(PMIC_WRAP_WRAP_EN
, DISABLE
);
682 WRAP_WR32(PMIC_WRAP_MUX_SEL
, MANUAL_MODE
);
683 WRAP_WR32(PMIC_WRAP_MAN_EN
,ENABLE
);
684 WRAP_WR32(PMIC_WRAP_DIO_EN
,DISABLE
);
686 WRAP_WR32(PMIC_WRAP_MAN_CMD
, (OP_WR
<< 14) | ((OP_PMIC_SEL
<< 13)|(OP_CSL
<< 8)));
687 WRAP_WR32(PMIC_WRAP_MAN_CMD
, (OP_WR
<< 14) | ((OP_PMIC_SEL
<< 13)|(OP_OUTS
<< 8))); //to reset counter
688 WRAP_WR32(PMIC_WRAP_MAN_CMD
, (OP_WR
<< 14) | ((OP_PMIC_SEL
<< 13)|(OP_CSH
<< 8)));
689 WRAP_WR32(PMIC_WRAP_MAN_CMD
, (OP_WR
<< 14) | ((OP_PMIC_SEL
<< 13)|(OP_OUTS
<< 8)));
690 WRAP_WR32(PMIC_WRAP_MAN_CMD
, (OP_WR
<< 14) | ((OP_PMIC_SEL
<< 13)|(OP_OUTS
<< 8)));
691 WRAP_WR32(PMIC_WRAP_MAN_CMD
, (OP_WR
<< 14) | ((OP_PMIC_SEL
<< 13)|(OP_OUTS
<< 8)));
692 WRAP_WR32(PMIC_WRAP_MAN_CMD
, (OP_WR
<< 14) | ((OP_PMIC_SEL
<< 13)|(OP_OUTS
<< 8)));
694 return_value
=wait_for_state_ready_init(wait_for_sync
,TIMEOUT_WAIT_IDLE
,PMIC_WRAP_WACS2_RDATA
,0);
697 PWRAPERR("_pwrap_reset_spislv fail,return_value=%x\n", return_value
);
702 WRAP_WR32(PMIC_WRAP_MAN_EN
,DISABLE
);
703 WRAP_WR32(PMIC_WRAP_MUX_SEL
,WRAPPER_MODE
);
706 WRAP_WR32(PMIC_WRAP_MAN_EN
,DISABLE
);
707 WRAP_WR32(PMIC_WRAP_MUX_SEL
,WRAPPER_MODE
);
711 static S32
_pwrap_init_reg_clock( U32 regck_sel
)
719 pwrap_read_nochk(PMIC_TOP_CKCON2
,&rdata
);
724 wdata
= (rdata
& (~(0x3<<10))) | (0x1<<10);
726 wdata
= rdata
& ~(0x3 << 10);
727 #elif defined SLV_6323
728 if( regck_sel
== 1 ) return 0x10; // not support in 6323!!
733 pwrap_write_nochk(PMIC_TOP_CKCON2
, wdata
);
734 pwrap_read_nochk(PMIC_TOP_CKCON2
, &rdata
);
735 #elif defined SLV_6323
736 pwrap_write_nochk(TOP_CKCON1_CLR
, wdata
);
737 pwrap_read_nochk(TOP_CKCON1
, &rdata
);
743 PWRAPERR("_pwrap_init_reg_clock,PMIC_TOP_CKCON2 Write [15]=1 Fail, rdata=%x\n",rdata
);
744 return E_PWR_WRITE_TEST_FAIL
;
746 #elif defined SLV_6323
747 if( (rdata
& 0x3) != 0)
749 PWRAPERR("_pwrap_init_reg_clock,PMIC_TOP_CKCON2 Write [15]=1 Fail, rdata=%x\n",rdata
);
750 return E_PWR_WRITE_TEST_FAIL
;
754 // Set Dummy cycle for both 6397(assume 12MHz)/6323 (assume 12MHz)
756 WRAP_WR32(PMIC_WRAP_RDDMY
,0xC);
757 #elif defined SLV_6323
758 pwrap_write_nochk(DEW_RDDMY_NO
, 0x8);
759 WRAP_WR32(PMIC_WRAP_RDDMY
,0x8);
762 // Config SPI Waveform according to reg clk
764 { // 6MHz in 6323 => no support ; // 12MHz in 6397
766 WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE
, 0x0); // wait data written into register => 3T_PMIC
767 WRAP_WR32(PMIC_WRAP_CSHEXT_READ
, 0x4); // for 6320, slave need enough time (4T of PMIC reg_ck) to back idle state
768 WRAP_WR32(PMIC_WRAP_CSLEXT_START
, 0x0);
769 WRAP_WR32(PMIC_WRAP_CSLEXT_END
, 0x4);
771 WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE
, 0x4); // wait data written into register => 3T_PMIC
772 WRAP_WR32(PMIC_WRAP_CSHEXT_READ
, 0x5); // for 6320, slave need enough time (4T of PMIC reg_ck) to back idle state
773 WRAP_WR32(PMIC_WRAP_CSLEXT_START
, 0x0);
774 WRAP_WR32(PMIC_WRAP_CSLEXT_END
, 0x0);
776 } else if( regck_sel
== 2 )
777 { // 12MHz in 6323; 24MHz in 6397
779 WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE
, 0x4); // wait data written into register => 3T_PMIC: consists of CSLEXT_END(1T) + CSHEXT(6T)
780 WRAP_WR32(PMIC_WRAP_CSHEXT_READ
, 0x0);
781 WRAP_WR32(PMIC_WRAP_CSLEXT_START
, 0x2);
782 WRAP_WR32(PMIC_WRAP_CSLEXT_END
, 0x2);
784 WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE
, 0x5); // wait data written into register => 3T_PMIC: consists of CSLEXT_END(1T) + CSHEXT(6T)
785 WRAP_WR32(PMIC_WRAP_CSHEXT_READ
, 0x0);
786 WRAP_WR32(PMIC_WRAP_CSLEXT_START
, 0x2);
787 WRAP_WR32(PMIC_WRAP_CSLEXT_END
, 0x2);
791 WRAP_WR32(PMIC_WRAP_CSHEXT_WRITE
, 0xf);
792 WRAP_WR32(PMIC_WRAP_CSHEXT_READ
, 0xf);
793 WRAP_WR32(PMIC_WRAP_CSLEXT_START
, 0xf);
794 WRAP_WR32(PMIC_WRAP_CSLEXT_END
, 0xf);
800 //--------------------------------------------------------
801 // Function : DrvPWRAP_Switch_Strobe_Setting()
802 // Description : used to switch input data calibration setting before system sleep or after system wakeup
803 // no use since SPI_CK (26MHz) is always kept unchanged
806 //--------------------------------------------------------
807 //void DrvPWRAP_Switch_Strobe_Setting (int si_ck_con, int sidly)
810 // // turn off spi_wrap
811 // *PMIC_WRAP_WRAP_EN = 0;
812 // // wait for WRAP to be in idle state
813 // // and no remaining rdata to be received
816 // reg_rdata = *PMIC_WRAP_WRAP_STA;
817 // } while ( (GET_WRAP_FSM(reg_rdata) != 0) ||
818 // (GET_WRAP_CH_DLE_RESTCNT(reg_rdata)) != 0 );
820 // *PMIC_WRAP_SI_CK_CON = si_ck_con;
821 // *PMIC_WRAP_SIDLY = sidly;
823 // // turn on spi_wrap
824 // *PMIC_WRAP_WRAP_EN = 1;
829 *pmic_wrap init,init wrap interface
832 S32
pwrap_init ( void )
841 //###############################
842 //toggle PMIC_WRAP and pwrap_spictl reset
843 //###############################
845 regValue
=WRAP_RD32(INFRA_GLOBALCON_RST0
);
846 PWRAPLOG("the reset register0x%x = %x\n",INFRA_GLOBALCON_RST0
,regValue
);
847 regValue
=WRAP_RD32(PMIC_WRAP_STAUPD_GRPEN
);
848 PWRAPLOG("PMIC_WRAP_STAUPD_GRPEN =%x,it should be equal to 0x1\n",regValue
);
850 PWRAP_CLEAR_SOFT_RESET_BIT
;
852 //###############################
853 // Set SPI_CK_freq = 26MHz for both 6320/6323/6397
854 //###############################
855 WRAP_WR32(CLK_CFG_4_CLR
,CLK_SPI_CK_26M
);
857 //###############################
858 //toggle PERI_PWRAP_BRIDGE reset
859 //###############################
860 //WRAP_SET_BIT(0x04,PERI_GLOBALCON_RST1);
861 //WRAP_CLR_BIT(0x04,PERI_GLOBALCON_RST1);
863 //###############################
865 //###############################
866 WRAP_WR32(PMIC_WRAP_DCM_EN
, ENABLE
);
867 WRAP_WR32(PMIC_WRAP_DCM_DBC_PRD
,DISABLE
);
868 //###############################
870 //###############################
872 WRAP_WR32(PMIC_WRAP_OP_TYPE
,OP_TYPE_CK
);
873 WRAP_WR32(PMIC_WRAP_MSB_FIRST
, MSB
);
876 //###############################
878 //###############################
879 sub_return
=_pwrap_reset_spislv();
880 if( sub_return
!= 0 )
882 PWRAPERR("error,_pwrap_reset_spislv fail,sub_return=%x\n",sub_return
);
883 return E_PWR_INIT_RESET_SPI
;
885 //###############################
887 //###############################
888 WRAP_WR32(PMIC_WRAP_WRAP_EN
,ENABLE
);//enable wrap
889 WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN
,WACS2
); //Only WACS2
890 WRAP_WR32(PMIC_WRAP_WACS2_EN
,ENABLE
);
891 //###############################
892 // Set Dummy cycle to make the cycle is the same at both AP and PMIC sides
893 //###############################
894 // (default value of 6320 dummy cycle is already 0x8)
896 WRAP_WR32(PMIC_WRAP_RDDMY
, 0xF);
899 //###############################
900 // Input data calibration flow;
901 //###############################
902 sub_return
= _pwrap_init_sistrobe();
903 if( sub_return
!= 0 )
905 PWRAPERR("error,DrvPWRAP_InitSiStrobe fail,sub_return=%x\n",sub_return
);
906 return E_PWR_INIT_SIDLY
;
909 //###############################
910 // SPI Waveform Configuration
911 //###############################
912 //6397; 0:safe mode, 1:12MHz, 2:24MHz
913 //6323; 0:safe mode, 1:NA, 2:12MHz
914 sub_return
= _pwrap_init_reg_clock(2);
917 PWRAPERR("error,_pwrap_init_reg_clock fail,sub_return=%x\n",sub_return
);
918 return E_PWR_INIT_REG_CLOCK
;
921 //###############################
922 // Enable PMIC dewrapper (only for 6320)
923 // (May not be necessary, depending on S/W partition)
924 //###############################
926 sub_return
= pwrap_write_nochk(PMIC_WRP_CKPDN
, 0);//set dewrap clock bit
927 sub_return1
=pwrap_write_nochk(PMIC_WRP_RST_CON
, 0);//clear dewrap reset bit
928 if(( sub_return
!= 0 )||( sub_return1
!= 0 ))
930 PWRAPERR("Enable PMIC fail, sub_return=%x sub_return1=%x\n", sub_return
,sub_return1
);
931 return E_PWR_INIT_ENABLE_PMIC
;
935 //###############################
937 //###############################
938 sub_return
= _pwrap_init_dio(ENABLE
);
939 if( sub_return
!= 0 )
941 PWRAPERR("_pwrap_init_dio test error,error code=%x, sub_return=%x\n", 0x11, sub_return
);
942 return E_PWR_INIT_DIO
;
945 //###############################
947 //###############################
948 sub_return
= _pwrap_init_cipher();
949 if( sub_return
!= 0 )
951 PWRAPERR("Enable Encryption fail, return=%x\n", sub_return
);
952 return E_PWR_INIT_CIPHER
;
955 //###############################
956 // Write test using WACS2
957 //###############################
958 sub_return
= pwrap_write_nochk(DEW_WRITE_TEST
, WRITE_TEST_VALUE
);
959 sub_return1
= pwrap_read_nochk(DEW_WRITE_TEST
,&rdata
);
960 if(( rdata
!= WRITE_TEST_VALUE
)||( sub_return
!= 0 )||( sub_return1
!= 0 ))
962 PWRAPERR("write test error,rdata=0x%x,exp=0xa55a,sub_return=0x%x,sub_return1=0x%x\n", rdata
,sub_return
,sub_return1
);
963 return E_PWR_INIT_WRITE_TEST
;
966 //###############################
967 // Signature Checking - Using Write Test Register
968 // should be the last to modify WRITE_TEST
969 //###############################
970 //_pwrap_wacs2_nochk(1, DEW_WRITE_TEST, 0x5678, &rdata);
971 //WRAP_WR32(PMIC_WRAP_SIG_ADR,DEW_WRITE_TEST);
972 //WRAP_WR32(PMIC_WRAP_SIG_VALUE,0x5678);
973 //WRAP_WR32(PMIC_WRAP_SIG_MODE, 0x1);
975 //###############################
976 // Signature Checking - Using CRC
977 // should be the last to modify WRITE_TEST
978 //###############################
979 sub_return
=pwrap_write_nochk(DEW_CRC_EN
,ENABLE
);
980 if( sub_return
!= 0 )
982 PWRAPERR("enable CRC fail,sub_return=%x\n", sub_return
);
983 return E_PWR_INIT_ENABLE_CRC
;
985 WRAP_WR32(PMIC_WRAP_CRC_EN
,ENABLE
);
986 WRAP_WR32(PMIC_WRAP_SIG_MODE
, CHECK_CRC
);
987 WRAP_WR32(PMIC_WRAP_SIG_ADR
, DEW_CRC_VAL
);
990 //###############################
992 //###############################
993 WRAP_WR32(PMIC_WRAP_HIPRIO_ARB_EN
,0x1ff);
994 //WRAP_WR32(PMIC_WRAP_RRARB_EN ,0x7);
995 WRAP_WR32(PMIC_WRAP_WACS0_EN
,ENABLE
);
996 WRAP_WR32(PMIC_WRAP_WACS1_EN
,ENABLE
);
997 //WRAP_WR32(PMIC_WRAP_WACS2_EN,0x1);//already enabled
998 // WRAP_WR32(PMIC_WRAP_EVENT_IN_EN,0x1);
999 //WRAP_WR32(PMIC_WRAP_EVENT_DST_EN,0xffff);
1000 WRAP_WR32(PMIC_WRAP_STAUPD_PRD
, 0x5); //0x1:20us,for concurrence test,MP:0x5; //100us
1001 WRAP_WR32(PMIC_WRAP_STAUPD_GRPEN
,0xff);
1002 WRAP_WR32(PMIC_WRAP_WDT_UNIT
,0xf);
1003 WRAP_WR32(PMIC_WRAP_WDT_SRC_EN
,0xffffffff);
1004 WRAP_WR32(PMIC_WRAP_TIMER_EN
,0x1);
1005 WRAP_WR32(PMIC_WRAP_INT_EN
,0x7ffffffb); //except for [31] debug_int
1008 //###############################
1009 // GPS_INTF initialization
1010 //###############################
1012 WRAP_WR32(PMIC_WRAP_ADC_CMD_ADDR
, AUXADC_CON21
);
1013 WRAP_WR32(PMIC_WRAP_PWRAP_ADC_CMD
, 0x8000);
1014 WRAP_WR32(PMIC_WRAP_ADC_RDY_ADDR
, AUXADC_ADC12
);
1015 WRAP_WR32(PMIC_WRAP_ADC_RDATA_ADDR1
, AUXADC_ADC13
);
1016 WRAP_WR32(PMIC_WRAP_ADC_RDATA_ADDR2
, AUXADC_ADC14
);
1019 //###############################
1020 // Initialization Done
1021 //###############################
1022 WRAP_WR32(PMIC_WRAP_INIT_DONE2
, 0x1);
1024 //###############################
1025 //TBD: Should be configured by MD MCU
1026 //###############################
1027 #if 1 //CONFIG_MTK_LDVT_PMIC_WRAP
1028 WRAP_WR32(PMIC_WRAP_INIT_DONE0
,1);
1029 WRAP_WR32(PMIC_WRAP_INIT_DONE1
, 1);
1030 //WRAP_WR32(PERI_PWRAP_BRIDGE_INIT_DONE3 , 1);
1031 //WRAP_WR32(PERI_PWRAP_BRIDGE_INIT_DONE4 , 1);
1035 //EXPORT_SYMBOL(pwrap_init);
1037 /*Interrupt handler function*/
1038 static irqreturn_t
mt_pmic_wrap_irq(int irqno
, void *dev_id
)
1040 unsigned long flags
=0;
1043 PWRAPREG("dump pwrap register\n");
1044 spin_lock_irqsave(&wrp_lock
,flags
);
1045 //*-----------------------------------------------------------------------
1046 pwrap_dump_all_register();
1047 //raise the priority of WACS2 for AP
1048 WRAP_WR32(PMIC_WRAP_HARB_HPRIO
,1<<2);
1050 //*-----------------------------------------------------------------------
1051 //clear interrupt flag
1052 WRAP_WR32(PMIC_WRAP_INT_CLR
, 0xffffffff);
1054 spin_unlock_irqrestore(&wrp_lock
,flags
);
1058 static U32
pwrap_read_test(void)
1063 return_value
=pwrap_read(DEW_READ_TEST
,&rdata
);
1064 if( rdata
!= DEFAULT_VALUE_READ_TEST
)
1066 PWRAPREG("Read Test fail,rdata=0x%x, exp=0x5aa5,return_value=0x%x\n", rdata
,return_value
);
1067 return E_PWR_READ_TEST_FAIL
;
1071 PWRAPREG("Read Test pass,return_value=%d\n",return_value
);
1075 static U32
pwrap_write_test(void)
1080 //###############################
1081 // Write test using WACS2
1082 //###############################
1083 sub_return
= pwrap_write(DEW_WRITE_TEST
, WRITE_TEST_VALUE
);
1084 PWRAPREG("after pwrap_write\n");
1085 sub_return1
= pwrap_read(DEW_WRITE_TEST
,&rdata
);
1086 if(( rdata
!= WRITE_TEST_VALUE
)||( sub_return
!= 0 )||( sub_return1
!= 0 ))
1088 PWRAPREG("write test error,rdata=0x%x,exp=0xa55a,sub_return=0x%x,sub_return1=0x%x\n", rdata
,sub_return
,sub_return1
);
1089 return E_PWR_INIT_WRITE_TEST
;
1093 PWRAPREG("write Test pass\n");
1098 static void pwrap_ut(U32 ut_test
)
1103 //pwrap_wacs2_para_test();
1106 //pwrap_wacs2_para_test();
1110 PWRAPREG ( "default test.\n" );
1115 /*---------------------------------------------------------------------------*/
1116 static S32
mt_pwrap_show_hal(char *buf
)
1119 return snprintf(buf
, PAGE_SIZE
, "%s\n","no implement" );
1121 /*---------------------------------------------------------------------------*/
1122 static S32
mt_pwrap_store_hal(const char *buf
,size_t count
)
1128 if(!strncmp(buf
, "-h", 2))
1130 PWRAPREG("PWRAP debug: [-dump_reg][-trace_wacs2][-init][-rdap][-wrap][-rdpmic][-wrpmic][-readtest][-writetest]\n");
1131 PWRAPREG("PWRAP UT: [1][2]\n");
1133 //--------------------------------------pwrap debug-------------------------------------------------------------
1134 else if(!strncmp(buf
, "-dump_reg", 9))
1136 pwrap_dump_all_register();
1138 else if(!strncmp(buf
, "-trace_wacs2", 12))
1140 //pwrap_trace_wacs2();
1142 else if(!strncmp(buf
, "-init", 5))
1144 return_value
=pwrap_init();
1146 PWRAPREG("pwrap_init pass,return_value=%d\n",return_value
);
1148 PWRAPREG("pwrap_init fail,return_value=%d\n",return_value
);
1150 else if (!strncmp(buf
, "-rdap", 5) && (1 == sscanf(buf
+5, "%x", ®_addr
)))
1152 //pwrap_read_reg_on_ap(reg_addr);
1154 else if (!strncmp(buf
, "-wrap", 5) && (2 == sscanf(buf
+5, "%x %x", ®_addr
,®_value
)))
1156 //pwrap_write_reg_on_ap(reg_addr,reg_value);
1158 else if (!strncmp(buf
, "-rdpmic", 7) && (1 == sscanf(buf
+7, "%x", ®_addr
)))
1160 //pwrap_read_reg_on_pmic(reg_addr);
1162 else if (!strncmp(buf
, "-wrpmic", 7) && (2 == sscanf(buf
+7, "%x %x", ®_addr
,®_value
)))
1164 //pwrap_write_reg_on_pmic(reg_addr,reg_value);
1166 else if(!strncmp(buf
, "-readtest", 9))
1170 else if(!strncmp(buf
, "-writetest", 10))
1174 //--------------------------------------pwrap UT-------------------------------------------------------------
1175 else if (!strncmp(buf
, "-ut", 3) && (1 == sscanf(buf
+3, "%d", &ut_test
)))
1179 PWRAPREG("wrong parameter\n");
1183 /*---------------------------------------------------------------------------*/
1184 #define VERSION "$Revision$"
1185 static int __init
mt_pwrap_init(void)
1188 PWRAPLOG("HAL init: version %s\n", VERSION
);
1189 mt_wrp
= get_mt_pmic_wrap_drv();
1190 mt_wrp
->store_hal
= mt_pwrap_store_hal
;
1191 mt_wrp
->show_hal
= mt_pwrap_show_hal
;
1192 mt_wrp
->wacs2_hal
= pwrap_wacs2_hal
;
1193 ret
= request_irq(MT_PMIC_WRAP_IRQ_ID
, mt_pmic_wrap_irq
, IRQF_TRIGGER_HIGH
, PMIC_WRAP_DEVICE
,0);
1195 PWRAPERR("register IRQ failed (%d)\n", ret
);
1201 postcore_initcall(mt_pwrap_init
);