SDMMC: select SDcard module configuration in kernel-3.10 project
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / host / rk29_sdmmc.h
1 /* drivers/mmc/host/rk29_sdmmc.h
2  *
3  * Copyright (C) 2011 ROCKCHIP, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #ifndef __RK2918_SDMMC_H
17 #define __RK2918_SDMMC_H
18
19 #include <linux/bitops.h>
20
21 #define MAX_SG_CHN      2
22
23
24 #define SDMMC_CTRL            (0x000)   //SDMMC Control register
25 #define SDMMC_PWREN           (0x004)   //Power enable register
26 #define SDMMC_CLKDIV          (0x008)   //Clock divider register
27 #define SDMMC_CLKSRC          (0x00c)   //Clock source register
28 #define SDMMC_CLKENA          (0x010)   //Clock enable register
29 #define SDMMC_TMOUT           (0x014)   //Time out register
30 #define SDMMC_CTYPE           (0x018)   //Card type register
31 #define SDMMC_BLKSIZ          (0x01c)   //Block size register
32 #define SDMMC_BYTCNT          (0x020)   //Byte count register
33 #define SDMMC_INTMASK         (0x024)   //Interrupt mask register
34 #define SDMMC_CMDARG          (0x028)   //Command argument register
35 #define SDMMC_CMD             (0x02c)   //Command register
36 #define SDMMC_RESP0           (0x030)   //Response 0 register
37 #define SDMMC_RESP1           (0x034)   //Response 1 register
38 #define SDMMC_RESP2           (0x038)   //Response 2 register
39 #define SDMMC_RESP3           (0x03c)   //Response 3 register
40 #define SDMMC_MINTSTS         (0x040)   //Masked interrupt status register
41 #define SDMMC_RINTSTS         (0x044)   //Raw interrupt status register
42 #define SDMMC_STATUS          (0x048)   //Status register
43 #define SDMMC_FIFOTH          (0x04c)   //FIFO threshold register
44 #define SDMMC_CDETECT         (0x050)   //Card detect register
45 #define SDMMC_WRTPRT          (0x054)   //Write protect register
46 #define SDMMC_TCBCNT          (0x05c)   //Transferred CIU card byte count
47 #define SDMMC_TBBCNT          (0x060)   //Transferred host/DMA to/from BIU_FIFO byte count
48 #define SDMMC_DEBNCE          (0x064)   //Card detect debounce register
49 #define SDMMC_USRID           (0x068)   //User ID register
50
51 #if defined(CONFIG_ARCH_RK29)
52 #define SDMMC_DATA            (0x100)   //FIFO data read write
53 #else
54 #define SDMMC_VERID           (0x06c)   //Version ID register
55 #define SDMMC_UHS_REG         (0x074)   //UHS-I register
56 #define SDMMC_RST_n           (0x078)   //Hardware reset register
57 #define SDMMC_BMOD                    (0x080)   //Bus mode register, control IMAC
58 #define SDMMC_PLDMND              (0x084)   //Poll Demand Register
59 #define SDMMC_DBADDR              (0x088)   //Descriptor List Base Address Register for 32-bit.
60 #define SDMMC_IDSTS                   (0x08c)   //Internal DMAC Status register
61 #define SDMMC_IDINTEN             (0x090)   //Internal DMAC Interrupt Enable Register
62 #define SDMMC_DSCADDR             (0x094)   //Current Host Descriptor Address Register for 32-bit
63 #define SDMMC_BUFADDR             (0x098)   //Current Buffer Descriptor Address Register for 32-bit
64 #define SDMMC_CARDTHRCTL      (0x100)   //Card Read Threshold Enable
65 #define SDMMC_BACK_END_POWER  (0x104)   //Back-end Power
66 #define SDMMC_FIFO_BASE       (0x200)   //FIFO data read write
67
68 #define SDMMC_DATA            SDMMC_FIFO_BASE
69 #endif
70
71 struct sdmmc_reg
72 {
73   u32      addr;
74   char   * name;
75 };
76
77 static struct sdmmc_reg rk_sdmmc_regs[] =
78 {
79   { 0x0000, "      CTRL" },
80   { 0x0004, "     PWREN" },
81   { 0x0008, "    CLKDIV" },
82   { 0x000C, "    CLKSRC" },
83   { 0x0010, "    CLKENA" },
84   { 0x0014, "     TMOUT" },
85   { 0x0018, "     CTYPE" },
86   { 0x001C, "    BLKSIZ" },
87   { 0x0020, "    BYTCNT" },
88   { 0x0024, "    INTMSK" },
89   { 0x0028, "    CMDARG" },
90   { 0x002C, "       CMD" },
91   { 0x0030, "     RESP0" },
92   { 0x0034, "     RESP1" },
93   { 0x0038, "     RESP2" },
94   { 0x003C, "     RESP3" },
95   { 0x0040, "    MINSTS" },
96   { 0x0044, "   RINTSTS" },
97   { 0x0048, "    STATUS" },
98   { 0x004C, "    FIFOTH" },
99   { 0x0050, "   CDETECT" },
100   { 0x0054, "    WRTPRT" },
101   { 0x0058, "      GPIO" },
102   { 0x005C, "    TCBCNT" },
103   { 0x0060, "    TBBCNT" },
104   { 0x0064, "    DEBNCE" },
105   { 0x0068, "     USRID" },
106 #if !defined(CONFIG_ARCH_RK29)  
107   { 0x006C, "     VERID" },
108   { 0x0070, "      HCON" },
109   { 0x0074, "   UHS_REG" },
110   { 0x0078, "     RST_n" },
111   { 0x0080, "      BMOD" },
112   { 0x0084, "    PLDMND" },
113   { 0x0088, "    DBADDR" },
114   { 0x008C, "     IDSTS" },
115   { 0x0090, "   IDINTEN" },
116   { 0x0094, "   DSCADDR" },
117   { 0x0098, "   BUFADDR" },
118   { 0x0100, "CARDTHRCTL" },
119   { 0x0104, "BackEndPwr" },
120 #endif  
121   { 0, 0 }
122 };
123
124 #define BIT(n)                          (1<<(n))
125 #define RK_CLEAR_BIT(n)                 (0<<(n))
126
127
128 /* Control register defines (base+ 0x00)*/
129 #define SDMMC_CTRL_USE_IDMAC        BIT(25)
130 #define SDMMC_CTRL_OD_PULLUP        BIT(24)
131 #define SDMMC_CTRL_CEATA_INT_EN     BIT(11)
132 #define SDMMC_CTRL_SEND_AS_CCSD     BIT(10)
133 #define SDMMC_CTRL_SEND_CCSD            BIT(9)
134 #define SDMMC_CTRL_ABRT_READ_DATA       BIT(8)
135 #define SDMMC_CTRL_SEND_IRQ_RESP        BIT(7)
136 #define SDMMC_CTRL_READ_WAIT            BIT(6)
137 #define SDMMC_CTRL_DMA_ENABLE       BIT(5)
138 #define SDMMC_CTRL_INT_ENABLE       BIT(4)
139 #define SDMMC_CTRL_DMA_RESET        BIT(2)
140 #define SDMMC_CTRL_FIFO_RESET       BIT(1)
141 #define SDMMC_CTRL_RESET            BIT(0)
142
143 /* Power Enable Register(base+ 0x04) */
144 #define POWER_ENABLE             BIT(0)             //Power enable
145 #define POWER_DISABLE            RK_CLEAR_BIT(0)    //Power off
146
147 /* SDMMC Clock source Register(base+ 0x0C) */
148 #define CLK_DIV_SRC_0         (0x0)    //clock divider 0 selected
149 #define CLK_DIV_SRC_1         (0x1)    //clock divider 1 selected
150 #define CLK_DIV_SRC_2         (0x2)    //clock divider 2 selected
151 #define CLK_DIV_SRC_3         (0x3)    //clock divider 3 selected
152
153
154 /* Clock Enable register defines(base+0x10) */
155 #define SDMMC_CLKEN_LOW_PWR      BIT(16)
156 #define SDMMC_CLKEN_NO_LOW_PWR   RK_CLEAR_BIT(16)   //low-power mode disabled
157 #define SDMMC_CLKEN_ENABLE       BIT(0)
158 #define SDMMC_CLKEN_DISABLE      RK_CLEAR_BIT(0)   //clock disabled
159
160 /* time-out register defines(base+0x14) */
161 #define SDMMC_TMOUT_DATA(n)      _SBF(8, (n))
162 #define SDMMC_TMOUT_DATA_MSK     0xFFFFFF00
163 #define SDMMC_TMOUT_RESP(n)      ((n) & 0xFF)
164 #define SDMMC_TMOUT_RESP_MSK     0xFF
165
166 /* card-type register defines(base+0x18) */
167 #define SDMMC_CTYPE_8BIT         BIT(16)
168 #define SDMMC_CTYPE_4BIT         BIT(0)
169 #define SDMMC_CTYPE_1BIT         RK_CLEAR_BIT(0)
170
171 /* Interrupt status & mask register defines(base+0x24) */
172 #if defined(CONFIG_ARCH_RK29)
173 #define SDMMC_INT_SDIO          BIT(16)      //SDIO interrupt
174 #else
175 #define SDMMC_INT_SDIO          BIT(24)      //SDIO interrupt
176 #define SDMMC_INT_UNBUSY        BIT(16)      //data no busy interrupt
177 #endif
178
179 #define SDMMC_INT_EBE           BIT(15)      //End Bit Error(read)/Write no CRC
180 #define SDMMC_INT_ACD           BIT(14)      //Auto Command Done
181 #define SDMMC_INT_SBE           BIT(13)      //Start Bit Error
182 #define SDMMC_INT_HLE           BIT(12)      //Hardware Locked Write Error
183 #define SDMMC_INT_FRUN          BIT(11)      //FIFO Underrun/Overrun Error
184 #define SDMMC_INT_HTO           BIT(10)      //Data Starvation by Host Timeout
185 #define SDMMC_INT_VSI           SDMMC_INT_HTO   // VSI => Voltage Switch Interrupt,Volt_Switch_int
186 #define SDMMC_INT_DRTO          BIT(9)       //Data Read TimeOut
187 #define SDMMC_INT_RTO           BIT(8)       //Response TimeOut
188 #define SDMMC_INT_DCRC          BIT(7)       //Data CRC Error
189 #define SDMMC_INT_RCRC          BIT(6)       //Response CRC Error
190 #define SDMMC_INT_RXDR          BIT(5)       //Receive FIFO Data Request
191 #define SDMMC_INT_TXDR          BIT(4)       //Transmit FIFO Data Request
192 #define SDMMC_INT_DTO                   BIT(3)       //Data Transfer Over
193 #define SDMMC_INT_CMD_DONE              BIT(2)       //Command Done
194 #define SDMMC_INT_RE            BIT(1)       //Response Error
195 #define SDMMC_INT_CD            BIT(0)       //Card Detect
196
197 /* Command register defines(base+0x2C) */
198 #define SDMMC_CMD_START         BIT(31)      //start command
199 #if !defined(CONFIG_ARCH_RK29)
200 #define SDMMC_CMD_USE_HOLD_REG      BIT(29)      //Use hold register.
201 #define SDMMC_CMD_VOLT_SWITCH       BIT(28)      //Voltage switch bit
202 #define SDMMC_CMD_BOOT_MODE         BIT(27)      //set boot mode.
203 #define SDMMC_CMD_DISABLE_BOOT      BIT(26)      //disable boot.
204 #define SDMMC_CMD_EXPECT_BOOT_ACK   BIT(25)      //Expect Boot Acknowledge.
205 #define SDMMC_CMD_ENABLE_BOOT       BIT(24)      //be set only for mandatory boot mode.
206 #define SDMMC_CMD_CCS_EXP                   BIT(23)      //expect Command Completion Signal(CCS) from the CE-ATA device.
207 #define SDMMC_CMD_CEATA_RD                  BIT(22)      //software should set this bit to indicate that CE-ATA device
208 #endif
209 #define SDMMC_CMD_UPD_CLK           BIT(21)             //update clock register only
210 #define SDMMC_CMD_INIT              BIT(15)             //send initialization sequence
211 #define SDMMC_CMD_STOP              BIT(14)             //stop abort command
212 #define SDMMC_CMD_PRV_DAT_NO_WAIT   RK_CLEAR_BIT(13)    //not wait previous data transfer complete, send command at once
213 #define SDMMC_CMD_PRV_DAT_WAIT      BIT(13)             //wait previous data transfer complete
214 #define SDMMC_CMD_SEND_STOP         BIT(12)             //send auto stop command at end of data transfer
215 #define SDMMC_CMD_BLOCK_MODE        RK_CLEAR_BIT(11)    //block data transfer command
216 #define SDMMC_CMD_STRM_MODE         BIT(11)             //stream data transfer command
217 #define SDMMC_CMD_DAT_READ          RK_CLEAR_BIT(10)    //read from card
218 #define SDMMC_CMD_DAT_WRITE         BIT(10)             //write to card; 
219 #define SDMMC_CMD_DAT_WR            BIT(10)             //write to card;
220 #define SDMMC_CMD_DAT_NO_EXP        RK_CLEAR_BIT(9)     //no data transfer expected
221 #define SDMMC_CMD_DAT_EXP           BIT(9)              //data transfer expected
222 #define SDMMC_CMD_RESP_NO_CRC       RK_CLEAR_BIT(8)     //do not check response crc
223 #define SDMMC_CMD_RESP_CRC          BIT(8)              //check response crc
224 #define SDMMC_CMD_RESP_CRC_NOCARE   SDMMC_CMD_RESP_CRC  //not care response crc
225 #define SDMMC_CMD_RESP_SHORT        RK_CLEAR_BIT(7)         //short response expected from card
226 #define SDMMC_CMD_RESP_LONG         BIT(7)                  //long response expected from card;
227 #define SDMMC_CMD_RESP_NOCARE       SDMMC_CMD_RESP_SHORT    //not care response length
228 #define SDMMC_CMD_RESP_NO_EXP       RK_CLEAR_BIT(6)         //no response expected from card
229 #define SDMMC_CMD_RESP_EXP          BIT(6)                  //response expected from card
230 #define SDMMC_CMD_INDX(n)           ((n) & 0x1F)
231
232
233 /* Status register defines (base+0x48)*/
234 #define SDMMC_STAUTS_MC_BUSY        BIT(10)
235 #define SDMMC_STAUTS_DATA_BUSY      BIT(9)          //Card busy
236 #define SDMMC_CMD_FSM_MASK                  (0x0F << 4)     //Command FSM status mask
237 #define SDMMC_CMD_FSM_IDLE          (0x00)                      //CMD FSM is IDLE
238 #define SDMMC_STAUTS_FIFO_FULL      BIT(3)          //FIFO is full status
239 #define SDMMC_STAUTS_FIFO_EMPTY     BIT(2)          //FIFO is empty status
240
241 /* Status register defines */
242 #define SDMMC_GET_FCNT(x)           (((x)>>17) & 0x1FFF)//fifo_count, numbers of filled locations in FIFO
243 #define SDMMC_FIFO_SZ               32
244 #define PIO_DATA_SHIFT              2
245
246
247 /* FIFO Register (base + 0x4c)*/
248 #define SD_MSIZE_1        (0x0 << 28)  //DW_DMA_Multiple_Transaction_Size
249 #define SD_MSIZE_4        (0x1 << 28)
250 #define SD_MSIZE_8        (0x1 << 28)
251 #define SD_MSIZE_16       (0x3 << 28)
252 #define SD_MSIZE_32       (0x4 << 28)
253 #define SD_MSIZE_64       (0x5 << 28)
254 #define SD_MSIZE_128      (0x6 << 28)
255 #define SD_MSIZE_256      (0x7 << 28)
256
257
258 #if defined(CONFIG_ARCH_RK29)
259 #define FIFO_DEPTH        (0x20)       //FIFO depth = 32 word
260 #define RX_WMARK_SHIFT    (16)
261 #define TX_WMARK_SHIFT    (0)
262
263 /* FIFO watermark */
264 #define RX_WMARK          (0xF)        //RX watermark level set to 15
265 #define TX_WMARK          (0x10)       //TX watermark level set to 16
266
267 #else
268 #define FIFO_DEPTH        (0x100)       //FIFO depth = 256 word
269 #define RX_WMARK_SHIFT    (16)
270 #define TX_WMARK_SHIFT    (0)
271
272 /* FIFO watermark */
273 #define RX_WMARK          (FIFO_DEPTH/2-1)     //RX watermark level set to 127
274 #define TX_WMARK          (FIFO_DEPTH/2)       //TX watermark level set to  128
275 #endif
276
277 #define FIFO_THRESHOLD_WATERMASK    (SD_MSIZE_16 |(RX_WMARK << RX_WMARK_SHIFT)|(TX_WMARK << TX_WMARK_SHIFT))
278
279 /* CDETECT register defines (base+0x50)*/
280 #define SDMMC_CARD_DETECT_N             BIT(0)      //0--represents presence of card.
281
282 /* WRIPRT register defines (base+0x54)*/
283 #define SDMMC_WRITE_PROTECT             BIT(0)      // 1--represents write protect
284
285 /* Control SDMMC_UHS_REG defines (base+ 0x74)*/
286 #define SDMMC_UHS_DDR_MODE      BIT(16)     // 0--Non DDR Mode; 1--DDR mode 
287 #define SDMMC_UHS_VOLT_REG_18   BIT(0)      // 0--3.3v; 1--1.8V
288
289
290 // #ifdef IDMAC_SUPPORT
291
292 /* Bus Mode Register (base + 0x80) */
293 #define  BMOD_SWR                   BIT(0)          // Software Reset: Auto cleared after one clock cycle                                0
294 #define  BMOD_FB                    BIT(1)          // Fixed Burst Length: when set SINGLE/INCR/INCR4/INCR8/INCR16 used at the start     1 
295 #define  BMOD_DE                    BIT(7)          // Idmac Enable: When set IDMAC is enabled                                           7
296 #define  BMOD_DSL_MSK           0x0000007C      // Descriptor Skip length: In Number of Words                                      6:2 
297 #define  BMOD_DSL_Shift         2               // Descriptor Skip length Shift value
298 #define  BMOD_DSL_ZERO      0x00000000  // No Gap between Descriptors
299 #define  BMOD_DSL_TWO       0x00000008  // 2 Words Gap between Descriptors
300 #define  BMOD_PBL                   0x00000400  // MSIZE in FIFOTH Register 
301
302 /* Internal DMAC Status Register(base + 0x8c)*/
303 /* Internal DMAC Interrupt Enable Register Bit Definitions */
304 #define  IDMAC_AI                           0x00000200   // Abnormal Interrupt Summary Enable/ Status                                       9
305 #define  IDMAC_NI                       0x00000100   // Normal Interrupt Summary Enable/ Status                                         8
306 #define  IDMAC_CES                              0x00000020   // Card Error Summary Interrupt Enable/ status                                     5
307 #define  IDMAC_DU                               0x00000010   // Descriptor Unavailabe Interrupt Enable /Status                                  4
308 #define  IDMAC_FBE                              0x00000004   // Fata Bus Error Enable/ Status                                                   2
309 #define  IDMAC_RI                               0x00000002   // Rx Interrupt Enable/ Status                                                     1
310 #define  IDMAC_TI                               0x00000001   // Tx Interrupt Enable/ Status                                                     0
311
312 #define  IDMAC_EN_INT_ALL       0x00000337   // Enables all interrupts 
313
314 #define IDMAC_HOST_ABORT_TX     0x00000400   // Host Abort received during Transmission                                     12:10 
315 #define IDMAC_HOST_ABORT_RX     0x00000800   // Host Abort received during Reception                                        12:10 
316
317 /* IDMAC FSM States */
318 #define IDMAC_DMA_IDLE          0x00000000   // DMA is in IDLE state                                                        
319 #define IDMAC_DMA_SUSPEND       0x00002000   // DMA is in SUSPEND state                                                     
320 #define IDMAC_DESC_RD           0x00004000   // DMA is in DESC READ or FETCH State                                          
321 #define IDMAC_DESC_CHK          0x00006000   // DMA is checking the Descriptor for Correctness                              
322 #define IDMAC_DMA_RD_REQ_WAIT   0x00008000   // DMA is in this state till dma_req is asserted (Read operation)              
323 #define IDMAC_DMA_WR_REQ_WAIT   0x0000A000   // DMA is in this state till dma_req is asserted (Write operation)             
324 #define IDMAC_DMA_RD            0x0000C000   // DMA is in Read mode                                                         
325 #define IDMAC_DMA_WR            0x0000E000   // DMA is in Write mode                                                         
326 #define IDMAC_DESC_CLOSE        0x00010000   // DMA is closing the Descriptor                                               
327
328 #define FIFOTH_MSIZE_1          0x00000000   // Multiple Trans. Size is 1
329 #define FIFOTH_MSIZE_4          0x10000000   // Multiple Trans. Size is 4
330 #define FIFOTH_MSIZE_8          0x20000000   // Multiple Trans. Size is 8
331 #define FIFOTH_MSIZE_16         0x30000000   // Multiple Trans. Size is 16
332 #define FIFOTH_MSIZE_32         0x40000000   // Multiple Trans. Size is 32
333 #define FIFOTH_MSIZE_64         0x50000000   // Multiple Trans. Size is 64
334 #define FIFOTH_MSIZE_128        0x60000000   // Multiple Trans. Size is 128
335 #define FIFOTH_MSIZE_256        0x70000000   // Multiple Trans. Size is 256
336 // #endif //#endif --#ifdef IDMAC_SUPPORT
337
338
339 /**********************************************************************
340 **  Misc Defines 
341 **********************************************************************/
342 #define SDMMC_MAX_BUFF_SIZE_IDMAC       8192
343 #define SDMMC_DEFAULT_DEBNCE_VAL        0x0FFFFFF
344
345 /* Specifies how often in millisecs to poll for card removal-insertion changes
346  * when the timer switch is open */
347 #define RK_SDMMC0_SWITCH_POLL_DELAY 35
348
349 /* SDMMC progress  return value */
350 #define SDM_SUCCESS              (0)                    
351 #define SDM_FALSE                (1)             
352 #define SDM_PARAM_ERROR          (2)              
353 #define SDM_RESP_ERROR           (3)             
354 #define SDM_RESP_TIMEOUT         (4)              
355 #define SDM_DATA_CRC_ERROR       (5)              
356 #define SDM_DATA_READ_TIMEOUT    (6)              
357 #define SDM_END_BIT_ERROR        (7)              
358 #define SDM_START_BIT_ERROR      (8)              
359 #define SDM_BUSY_TIMEOUT         (9)            
360 #define SDM_ERROR                (10)             //SDMMC host controller error 
361 #define SDM_START_CMD_FAIL       (11)  
362 #define SDM_WAIT_FOR_CMDSTART_TIMEOUT   (12)  
363 #define SDM_WAIT_FOR_FIFORESET_TIMEOUT  (13)  
364
365 #define FALSE                   0
366 #define TRUE                    1
367
368 #define DEBOUNCE_TIME         (25)     //uint is ms, recommend 5--25ms
369
370 #if defined(CONFIG_ARCH_RK29)
371 #define SDMMC_USE_INT_UNBUSY     0
372 #else
373 #define SDMMC_USE_INT_UNBUSY     0///1 
374 #endif
375
376 /*
377 ** You can set the macro to true, if some module wants to use this feature, which is about SDIO suspend-resume.
378 ** As the following example.
379 ** added by xbw at 2013-05-08
380 */
381 #if defined(CONFIG_MTK_COMBO_DRIVER_VERSION_JB2) || defined(CONFIG_ESP8089)
382 #define RK_SDMMC_USE_SDIO_SUSPEND_RESUME    1
383 #else
384 #define RK_SDMMC_USE_SDIO_SUSPEND_RESUME    0
385 #endif
386
387 #define RK29_SDMMC_ERROR_FLAGS          (SDMMC_INT_FRUN | SDMMC_INT_HLE )
388
389 #if defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
390     #if SDMMC_USE_INT_UNBUSY
391     #define RK29_SDMMC_INTMASK_USEDMA   (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | SDMMC_INT_UNBUSY |RK29_SDMMC_ERROR_FLAGS )
392     #define RK29_SDMMC_INTMASK_USEIO    (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | SDMMC_INT_UNBUSY |RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_TXDR | SDMMC_INT_RXDR )
393     #else
394     #define RK29_SDMMC_INTMASK_USEDMA   (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS )
395     #define RK29_SDMMC_INTMASK_USEIO    (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_TXDR | SDMMC_INT_RXDR )
396     #endif
397 #else
398     #if SDMMC_USE_INT_UNBUSY
399     #define RK29_SDMMC_INTMASK_USEDMA   (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | SDMMC_INT_UNBUSY |RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD)
400     #define RK29_SDMMC_INTMASK_USEIO    (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | SDMMC_INT_UNBUSY |RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD| SDMMC_INT_TXDR | SDMMC_INT_RXDR )
401     #else
402     #define RK29_SDMMC_INTMASK_USEDMA   (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD)
403     #define RK29_SDMMC_INTMASK_USEIO    (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD| SDMMC_INT_TXDR | SDMMC_INT_RXDR )
404     #endif
405 #endif
406
407 #define RK29_SDMMC_SEND_START_TIMEOUT   3000  //The time interval from the time SEND_CMD to START_CMD_BIT cleared.
408 #define RK29_ERROR_PRINTK_INTERVAL      200   //The time interval between the two printk for the same error. 
409 #define RK29_SDMMC_WAIT_DTO_INTERNVAL   4500  //The time interval from the CMD_DONE_INT to DTO_INT
410 #define RK29_SDMMC_REMOVAL_DELAY        2000  //The time interval from the CD_INT to detect_timer react.
411
412 //#define RK29_SDMMC_VERSION "Ver.6.00 The last modify date is 2013-08-02"
413
414 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)  
415 #define RK29_CTRL_SDMMC_ID   0  //mainly used by SDMMC
416 #define RK29_CTRL_SDIO1_ID   1  //mainly used by sdio-wifi
417 #define RK29_CTRL_SDIO2_ID   2  //mainly used by sdio-card
418 #else
419 #define RK29_CTRL_SDMMC_ID   5  
420 #define RK29_CTRL_SDIO1_ID   1  
421 #define RK29_CTRL_SDIO2_ID   2  
422 #endif
423
424 #define SDMMC_CLOCK_TEST     0
425
426 #define RK29_SDMMC_NOTIFY_REMOVE_INSERTION /* use sysfs to notify the removal or insertion of sd-card*/
427 //#define RK29_SDMMC_LIST_QUEUE            /* use list-queue for multi-card*/
428
429 //support Internal DMA 
430 #if 0 //Sometime in the future to enable
431 #define DRIVER_SDMMC_USE_IDMA 1
432 #else
433 #define DRIVER_SDMMC_USE_IDMA 0
434 #endif
435
436
437 enum {
438         EVENT_CMD_COMPLETE = 0,
439         EVENT_DATA_COMPLETE,
440         EVENT_DATA_UNBUSY,
441         EVENT_DATA_ERROR,
442         EVENT_XFER_ERROR
443 };
444
445 enum rk29_sdmmc_state {
446         STATE_IDLE = 0,
447         STATE_SENDING_CMD,
448         STATE_DATA_BUSY,
449         STATE_DATA_UNBUSY,
450         STATE_DATA_END,
451         STATE_SENDING_STOP,
452 };
453
454 struct rk29_sdmmc_dma_info {
455         enum dma_ch chn;
456         char *name;
457         struct rk29_dma_client client;
458 };
459
460 static struct rk29_sdmmc_dma_info rk29_sdmmc_dma_infos[]= {
461         {
462                 .chn = DMACH_SDMMC,
463                 .client = {
464                         .name = "rk29-dma-sdmmc0",
465                 }
466         },
467         {
468                 .chn = DMACH_SDIO,
469                 .client = {
470                         .name = "rk29-dma-sdio1",
471                 }
472         },
473
474         {
475                 .chn = DMACH_EMMC,
476                 .client = {
477                         .name = "rk29-dma-sdio2",
478                 }
479         },
480 };
481
482
483 /* Interrupt Information */
484 typedef struct TagSDC_INT_INFO
485 {
486     u32     transLen;               //the length of data sent.
487     u32     desLen;                 //the total length of the all data.
488     u32    *pBuf;                   //the data buffer for interrupt read or write.
489 }SDC_INT_INFO_T;
490
491
492 struct rk29_sdmmc {
493         spinlock_t              lock;
494         void __iomem    *regs;
495         struct clk              *clk;
496
497         struct mmc_request      *mrq;
498         struct mmc_request      *new_mrq;
499         struct mmc_command      *cmd;
500         struct mmc_data         *data;  
501         struct scatterlist      *sg;
502         unsigned int            pio_offset;
503
504         dma_addr_t              dma_addr;;
505         unsigned int    use_dma:1;
506         char                    dma_name[8];
507         u32                     cmd_status;
508         u32                     data_status;
509         u32                     stop_cmdr;
510
511     u32         old_div;
512         u32                     cmdr;   //the value setted into command-register
513         u32                     dodma;  //sign the DMA used for transfer.
514         u32         errorstep;//record the error point.
515         int         timeout_times;  //use to force close the sdmmc0 when the timeout_times exceeds the limit.
516         u32         *pbuf;
517         SDC_INT_INFO_T    intInfo; 
518     struct rk29_sdmmc_dma_info  dma_info;
519     int         irq;
520         int error_times;
521         u32 old_cmd;
522         
523         struct tasklet_struct   tasklet;
524         unsigned long           pending_events;
525         unsigned long           completed_events;
526         enum rk29_sdmmc_state   state;
527
528 #ifdef RK29_SDMMC_LIST_QUEUE
529         struct list_head        queue;
530         struct list_head        queue_node;
531 #endif
532
533         u32                     bus_hz;
534         struct platform_device  *pdev;
535         struct mmc_host         *mmc;
536         u32                     ctype;
537         unsigned int            clock;
538         unsigned long           flags;
539         
540 #define RK29_SDMMC_CARD_PRESENT 0
541
542         int                     id;
543
544         struct timer_list       detect_timer; 
545         struct timer_list       request_timer; //the timer for INT_CMD_DONE
546         struct timer_list       DTO_timer;     //the timer for INT_DTO
547         struct mmc_command      stopcmd;
548     struct rksdmmc_gpio det_pin;
549
550         /* flag for current bus settings */
551     u32 bus_mode;
552
553     unsigned int            oldstatus;
554     unsigned int            complete_done;
555     unsigned int            retryfunc;
556     
557     int gpio_irq;
558         int gpio_power_en;
559         int gpio_power_en_level;
560         struct delayed_work             work;
561
562 #ifdef CONFIG_RK29_SDIO_IRQ_FROM_GPIO
563     unsigned int sdio_INT_gpio;
564     unsigned int sdio_irq;
565     unsigned long trigger_level;
566 #endif
567
568 #if defined(CONFIG_SDMMC0_RK29_WRITE_PROTECT) || defined(CONFIG_SDMMC1_RK29_WRITE_PROTECT)
569     int write_protect;
570     int protect_level;
571 #endif
572
573         bool                    irq_state;
574     void (*set_iomux)(int device_id, unsigned int bus_width);
575
576     /* FIFO push and pull */
577     int         data_shift;
578     void (*push_data)(struct rk29_sdmmc *host, void *buf, int cnt);
579     void (*pull_data)(struct rk29_sdmmc *host, void *buf, int cnt);
580 };
581
582
583 #ifdef RK29_SDMMC_NOTIFY_REMOVE_INSERTION
584 static struct rk29_sdmmc    *globalSDhost[3];
585 #endif
586
587 #define rk29_sdmmc_test_and_clear_pending(host, event)          \
588         test_and_clear_bit(event, &host->pending_events)
589 #define rk29_sdmmc_test_pending(host, event)            \
590         test_bit(event, &host->pending_events)
591 #define rk29_sdmmc_test_completed(host, event)           \
592         test_bit(event, &host->completed_events)       
593 #define rk29_sdmmc_set_completed(host, event)                   \
594         set_bit(event, &host->completed_events)
595 #define rk29_sdmmc_set_pending(host, event)                             \
596         set_bit(event, &host->pending_events)
597
598
599
600 #endif