tizen 2.4 release
[kernel/u-boot-tm1.git] / arch / arm / cpu / armv7 / sc8810 / 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 #include <asm/proc/system.h>
29
30 #define TIMEOUT_ADI     (300)//for 8810 fpga
31 #define CHIP_REG_OR(reg_addr, value)    (*(volatile unsigned int *)(reg_addr) |= (unsigned int)(value))
32 #define CHIP_REG_AND(reg_addr, value)   (*(volatile unsigned int *)(reg_addr) &= (unsigned int)(value))
33 #define CHIP_REG_GET(reg_addr)          (*(volatile unsigned int *)(reg_addr))
34 #define CHIP_REG_SET(reg_addr, value)   (*(volatile unsigned int *)(reg_addr)  = (unsigned int)(value))
35
36 #define SCI_ASSERT(condition) BUG_ON(!(condition))
37 #define SCI_PASSERT(condition, format...)  \
38         do {            \
39                 if(!(condition)) { \
40                         printf("function :%s\r\n", __FUNCTION__);\
41                         BUG();  \
42                 } \
43         }while(0)
44
45 #define ADI_PHYS        ADI_BASE
46 #define __adi_virt_to_phys(x) ((x) - SPRD_ADI_BASE + ADI_PHYS)
47
48 #ifdef CONFIG_SC7710G2
49 #define ADI_GSSI_CFG0           (ADI_BASE + 0x1C)
50 #define ADI_CLK_ALWAYS_ON       BIT_30
51 #endif
52
53 /*****************************************************************************
54  *  Description:    this function performs read operation to the analog die reg .   *
55  *                      it will disable all interrupt and polling the ready bit,        *
56  *              and return a half-word value after read complete.             *
57  *  Global Resource Dependence:                                              *
58  *  Author: Tim Luo                                                        *
59  *  Note:   return register value                                               *
60 ******************************************************************************/
61 unsigned short ADI_Analogdie_reg_read (unsigned int addr)
62
63 {
64         unsigned int adi_rd_data;
65         unsigned long flags;
66         int timeout = TIMEOUT_ADI;
67         local_irq_save(flags);
68    // SCI_DisableIRQ();
69    // SCI_DisableFIQ();
70
71     //SCI_ASSERT ( (addr>=ANA_REG_ADDR_START) && (addr<=ANA_REG_ADDR_END));
72
73     //Set read command
74    addr = __adi_virt_to_phys(addr);
75     CHIP_REG_SET (ADI_ARM_RD_CMD, addr);
76
77     //wait read operation complete, RD_data[31] will be cleared after the read operation complete
78         do {
79                 adi_rd_data = CHIP_REG_GET (ADI_RD_DATA);
80                 if (!timeout--)
81                         break;
82         } while (adi_rd_data & BIT_31);
83
84     //rd_data high part should be the address of the last read operation
85     //SCI_ASSERT ( (adi_rd_data & 0xFFFF0000) == ((addr) <<16));
86
87     //read operation complete
88     //SCI_RestoreFIQ();
89     //SCI_RestoreIRQ();
90         local_irq_restore(flags);
91
92     return ( (unsigned short) (adi_rd_data & 0x0000FFFF));
93
94 }
95
96 /*****************************************************************************
97  *  Description:    this function performs write operation to the analog die reg .   *
98  *                      it will write the analog die register if the fifo is not full       *
99  *              It will polling the fifo full status til it is not full                  *
100  *  Global Resource Dependence:                                              *
101  *  Author: Tim Luo                                                        *
102  *  Note:                                                                      *
103 ******************************************************************************/
104 void ADI_Analogdie_reg_write (unsigned int addr, unsigned short data)
105 {
106
107         int timeout = TIMEOUT_ADI;
108         do {////ADI_wait_fifo_empty
109                 if ( ( (CHIP_REG_GET (ADI_FIFO_STS) & ( (unsigned int) ADI_FIFO_EMPTY)) != 0))
110                 {
111                         break;
112                 }
113                 if (!timeout--)
114                         return;//NEED TO return ERROR.
115         } while (1);/*lint !e506*/
116
117         CHIP_REG_SET (addr, data);
118 }
119
120
121 /*****************************************************************************
122  *  Description:    this function is used to init analog to digital module.   *
123  *                      it will enable adi_acc and soft reset adi_module,        *
124  *              and then config the priority of each channel.             *
125  *  Global Resource Dependence:                                              *
126  *  Author: Tim Luo                                                        *
127  *  Note:                                                                                     *
128 ******************************************************************************/
129 void ADI_init (void)
130 {
131     // volatile ADI_CFG_REG_T *adi_handle = (ADI_CFG_REG_T *) ADI_BASE_ADDR;
132
133     //enable ADI_ACC to put the adi master to normal operation mode
134     CHIP_REG_OR (GR_GEN0, GEN0_ADI_EN);
135
136     //reset ADI module
137     CHIP_REG_OR (GR_SOFT_RST, ADI_SOFT_RST);
138     {
139         unsigned int wait = 50;
140
141         while (wait--);
142     }
143     CHIP_REG_AND (GR_SOFT_RST, (~ADI_SOFT_RST));
144
145     //Please refer to Section 5. Program guide, SC8800G Analog-Digital Interface module Implementation Specifications.doc
146     CHIP_REG_AND (ADI_CTL_REG, (~ARM_SERCLK_EN));
147
148     //config channel priority
149     CHIP_REG_SET (ADI_CHANNEL_PRI, ( (0<<INT_STEAL_PRI) | (1<<STC_WR_PRI) | (0<<ARM_WR_PRI)
150                                      | (0<<ARM_RD_PRI) | (0<<DSP_WR_PRI) | (0<<DSP_RD_PRI)
151                                      | (1<<RFT_WR_PRI) | (1<<PD_WR_PRI)));
152
153 }
154
155 #ifdef CONFIG_SC7710G2
156 void ADI_ClkAlwaysOn(unsigned long en)
157 {
158         if (en)
159                 CHIP_REG_OR(ADI_GSSI_CFG0, ADI_CLK_ALWAYS_ON);
160         else
161                 CHIP_REG_AND(ADI_GSSI_CFG0, (~ADI_CLK_ALWAYS_ON));
162 }
163 #endif