tizen 2.4 release
[kernel/u-boot-tm1.git] / arch / arm / cpu / arm926ejs / sc8800g / adi_drv.c
1 /******************************************************************************
2  ** File Name:      adi_phy_v3.c                                                 *
3  ** Author:         tim.luo                                             *
4  ** DATE:           2/25/2010                                                *
5  ** Copyright:      2010 Spreatrum, Incoporated. All Rights Reserved.         *
6  ** Description:    This file defines the basic operation interfaces of       *
7  **                 Analog to Digital Module.                                       *
8  **                                                                                             *
9  ******************************************************************************
10
11  ******************************************************************************
12  **                        Edit History                                       *
13  ** ------------------------------------------------------------------------- *
14  ** DATE           NAME             DESCRIPTION                               *
15  ** 2/25/2010     Tim Luo      Create.                                   *
16  **                                                                                                *
17  ******************************************************************************/
18
19
20 /**---------------------------------------------------------------------------*
21  **                         Dependencies                                      *
22  **---------------------------------------------------------------------------*/
23 #include <common.h>
24 #include <asm/io.h>
25
26 #include <asm/arch/regs_adi.h>
27 #include <asm/arch/adi_hal_internal.h>
28
29 #define CHIP_REG_OR(reg_addr, value)    (*(volatile unsigned int *)(reg_addr) |= (unsigned int)(value))
30 #define CHIP_REG_AND(reg_addr, value)   (*(volatile unsigned int *)(reg_addr) &= (unsigned int)(value))
31 #define CHIP_REG_GET(reg_addr)          (*(volatile unsigned int *)(reg_addr))
32 #define CHIP_REG_SET(reg_addr, value)   (*(volatile unsigned int *)(reg_addr)  = (unsigned int)(value))
33
34 #define SCI_ASSERT(condition) BUG_ON(!(condition))  
35 #define SCI_PASSERT(condition, format...)  \
36         do {            \
37                 if(!(condition)) { \
38                         printf("function :%s\r\n", __FUNCTION__);\
39                         BUG();  \
40                 } \
41         }while(0)
42         
43 #define ADI_PHYS        ADI_BASE
44 #define __adi_virt_to_phys(x) ((x) - SPRD_ADI_BASE + ADI_PHYS)
45
46 /*****************************************************************************
47  *  Description:    this function performs read operation to the analog die reg .   *
48  *                      it will disable all interrupt and polling the ready bit,        *
49  *              and return a half-word value after read complete.             *
50  *  Global Resource Dependence:                                              *
51  *  Author: Tim Luo                                                        *
52  *  Note:   return register value                                               *
53 ******************************************************************************/
54 unsigned short ADI_Analogdie_reg_read (unsigned int addr)
55
56 {
57     unsigned int adi_rd_data;
58         unsigned long flags;
59
60         local_irq_save(flags);
61    // SCI_DisableIRQ();
62    // SCI_DisableFIQ();
63
64     //SCI_ASSERT ( (addr>=ANA_REG_ADDR_START) && (addr<=ANA_REG_ADDR_END));
65
66     //Set read command
67    addr = __adi_virt_to_phys(addr);
68     CHIP_REG_SET (ADI_ARM_RD_CMD, addr);
69
70     //wait read operation complete, RD_data[31] will be cleared after the read operation complete
71     do
72     {
73         adi_rd_data = CHIP_REG_GET (ADI_RD_DATA);
74     }
75     while (adi_rd_data & BIT_31);
76         
77     //rd_data high part should be the address of the last read operation
78     //SCI_ASSERT ( (adi_rd_data & 0xFFFF0000) == ((addr) <<16));
79
80     //read operation complete
81     //SCI_RestoreFIQ();
82     //SCI_RestoreIRQ();
83         local_irq_restore(flags);
84         
85     return ( (unsigned short) (adi_rd_data & 0x0000FFFF));
86
87 }
88 /*****************************************************************************
89  *  Description:    this function performs write operation to the analog die reg .   *
90  *                      it will write the analog die register if the fifo is not full       *
91  *              It will polling the fifo full status til it is not full                  *
92  *  Global Resource Dependence:                                              *
93  *  Author: Tim Luo                                                        *
94  *  Note:                                                                      *
95 ******************************************************************************/
96 void ADI_Analogdie_reg_write (unsigned int addr, unsigned short data)
97
98 {
99
100     do              ////ADI_wait_fifo_empty
101     {
102         if ( ( (CHIP_REG_GET (ADI_FIFO_STS) & ( (unsigned int) ADI_FIFO_EMPTY)) != 0))
103         {
104             break;
105         }
106     }
107     while (1);/*lint !e506*/
108
109     CHIP_REG_SET (addr, data);
110 }
111
112
113 /*****************************************************************************
114  *  Description:    this function is used to init analog to digital module.   *
115  *                      it will enable adi_acc and soft reset adi_module,        *
116  *              and then config the priority of each channel.             *
117  *  Global Resource Dependence:                                              *
118  *  Author: Tim Luo                                                        *
119  *  Note:                                                                                     *
120 ******************************************************************************/
121 void ADI_init (void)
122 {
123     // volatile ADI_CFG_REG_T *adi_handle = (ADI_CFG_REG_T *) ADI_BASE_ADDR;
124
125     //enable ADI_ACC to put the adi master to normal operation mode
126     CHIP_REG_OR (GR_GEN0, GEN0_ADI_EN);
127
128     //reset ADI module
129     CHIP_REG_OR (GR_SOFT_RST, ADI_SOFT_RST);
130     {
131         unsigned int wait = 50;
132
133         while (wait--);
134     }
135     CHIP_REG_AND (GR_SOFT_RST, (~ADI_SOFT_RST));
136
137     //Please refer to Section 5. Program guide, SC8800G Analog-Digital Interface module Implementation Specifications.doc
138     CHIP_REG_AND (ADI_CTL_REG, (~ARM_SERCLK_EN));
139
140     //config channel priority
141     CHIP_REG_SET (ADI_CHANNEL_PRI, ( (0<<INT_STEAL_PRI) | (1<<STC_WR_PRI) | (0<<ARM_WR_PRI)
142                                      | (0<<ARM_RD_PRI) | (0<<DSP_WR_PRI) | (0<<DSP_RD_PRI)
143                                      | (1<<RFT_WR_PRI) | (1<<PD_WR_PRI)));
144
145 }