change source file mode to 0644 instead of 0755
[profile/mobile/platform/kernel/u-boot-tm1.git] / arch / arm / cpu / armv7 / tiger / 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 /*****************************************************************************
49  *  Description:    this function performs read operation to the analog die reg .   *
50  *                      it will disable all interrupt and polling the ready bit,        *
51  *              and return a half-word value after read complete.             *
52  *  Global Resource Dependence:                                              *
53  *  Author: Tim Luo                                                        *
54  *  Note:   return register value                                               *
55 ******************************************************************************/
56 unsigned short ADI_Analogdie_reg_read (unsigned int addr)
57
58 {
59         unsigned int adi_rd_data;
60         unsigned long flags;
61         int timeout = TIMEOUT_ADI;
62         local_irq_save(flags);
63    // SCI_DisableIRQ();
64    // SCI_DisableFIQ();
65
66     //SCI_ASSERT ( (addr>=ANA_REG_ADDR_START) && (addr<=ANA_REG_ADDR_END));
67
68     //Set read command
69    addr = __adi_virt_to_phys(addr);
70     CHIP_REG_SET (ADI_ARM_RD_CMD, addr);
71
72     //wait read operation complete, RD_data[31] will be cleared after the read operation complete
73         do {
74                 adi_rd_data = CHIP_REG_GET (ADI_RD_DATA);               
75                 if (!timeout--)
76                         break;
77         } while (adi_rd_data & BIT_31);
78         
79     //rd_data high part should be the address of the last read operation
80     //SCI_ASSERT ( (adi_rd_data & 0xFFFF0000) == ((addr) <<16));
81
82     //read operation complete
83     //SCI_RestoreFIQ();
84     //SCI_RestoreIRQ();
85         local_irq_restore(flags);
86         
87     return ( (unsigned short) (adi_rd_data & 0x0000FFFF));
88
89 }
90
91 /*****************************************************************************
92  *  Description:    this function performs write operation to the analog die reg .   *
93  *                      it will write the analog die register if the fifo is not full       *
94  *              It will polling the fifo full status til it is not full                  *
95  *  Global Resource Dependence:                                              *
96  *  Author: Tim Luo                                                        *
97  *  Note:                                                                      *
98 ******************************************************************************/
99 void ADI_Analogdie_reg_write (unsigned int addr, unsigned short data)
100 {
101         
102         int timeout = TIMEOUT_ADI;
103         do {////ADI_wait_fifo_empty
104                 if ( ( (CHIP_REG_GET (ADI_FIFO_STS) & ( (unsigned int) ADI_FIFO_EMPTY)) != 0))
105                 {
106                         break;
107                 }
108                 if (!timeout--)
109                         return;//NEED TO return ERROR.
110         } while (1);/*lint !e506*/
111
112         CHIP_REG_SET (addr, data);
113 }
114
115
116 /*****************************************************************************
117  *  Description:    this function is used to init analog to digital module.   *
118  *                      it will enable adi_acc and soft reset adi_module,        *
119  *              and then config the priority of each channel.             *
120  *  Global Resource Dependence:                                              *
121  *  Author: Tim Luo                                                        *
122  *  Note:                                                                                     *
123 ******************************************************************************/
124 void ADI_init (void)
125 {
126     // volatile ADI_CFG_REG_T *adi_handle = (ADI_CFG_REG_T *) ADI_BASE_ADDR;
127
128     //enable ADI_ACC to put the adi master to normal operation mode
129     CHIP_REG_OR (GR_GEN0, GEN0_ADI_EN);
130
131     //reset ADI module
132     CHIP_REG_OR (GR_SOFT_RST, ADI_SOFT_RST);
133     {
134         unsigned int wait = 50;
135
136         while (wait--);
137     }
138     CHIP_REG_AND (GR_SOFT_RST, (~ADI_SOFT_RST));
139
140     //Please refer to Section 5. Program guide, SC8800G Analog-Digital Interface module Implementation Specifications.doc
141     CHIP_REG_AND (ADI_CTL_REG, (~ARM_SERCLK_EN));
142
143     //config channel priority
144     CHIP_REG_SET (ADI_CHANNEL_PRI, ( (0<<INT_STEAL_PRI) | (1<<STC_WR_PRI) | (0<<ARM_WR_PRI)
145                                      | (0<<ARM_RD_PRI) | (0<<DSP_WR_PRI) | (0<<DSP_RD_PRI)
146                                      | (1<<RFT_WR_PRI) | (1<<PD_WR_PRI)));
147
148 }