fh_sdio.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * This file is part of FH8620 BSP for RT-Thread distribution.
  3. *
  4. * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd.
  5. * All rights reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Visit http://www.fullhan.com to get contact with Fullhan.
  22. *
  23. * Change Logs:
  24. * Date Author Notes
  25. */
  26. #ifndef FH_SDIO_H_
  27. #define FH_SDIO_H_
  28. #include <rtthread.h>
  29. //#define __ASIC_BRANCH__
  30. enum {
  31. CTRL = 0x0, /** Control */
  32. PWREN = 0x4, /** Power-enable */
  33. CLKDIV = 0x8, /** Clock divider */
  34. CLKSRC = 0xC, /** Clock source */
  35. CLKENA = 0x10, /** Clock enable */
  36. TMOUT = 0x14, /** Timeout */
  37. CTYPE = 0x18, /** Card type */
  38. BLKSIZ = 0x1C, /** Block Size */
  39. BYTCNT = 0x20, /** Byte count */
  40. INTMSK = 0x24, /** Interrupt Mask */
  41. CMDARG = 0x28, /** Command Argument */
  42. CMD = 0x2C, /** Command */
  43. RESP0 = 0x30, /** Response 0 */
  44. RESP1 = 0x34, /** Response 1 */
  45. RESP2 = 0x38, /** Response 2 */
  46. RESP3 = 0x3C, /** Response 3 */
  47. MINTSTS = 0x40, /** Masked interrupt status */
  48. RINTSTS = 0x44, /** Raw interrupt status */
  49. STATUS = 0x48, /** Status */
  50. FIFOTH = 0x4C, /** FIFO threshold */
  51. CDETECT = 0x50, /** Card detect */
  52. WRTPRT = 0x54, /** Write protect */
  53. GPIO = 0x58, /** General Purpose IO */
  54. TCBCNT = 0x5C, /** Transferred CIU byte count */
  55. TBBCNT = 0x60, /** Transferred host/DMA to/from byte count */
  56. DEBNCE = 0x64, /** Card detect debounce */
  57. USRID = 0x68, /** User ID */
  58. VERID = 0x6C, /** Version ID */
  59. HCON = 0x70, /** Hardware Configuration */
  60. UHSREG = 0x74, /** Reserved */
  61. BMOD = 0x80, /** Bus mode Register */
  62. PLDMND = 0x84, /** Poll Demand */
  63. DBADDR = 0x88, /** Descriptor Base Address */
  64. IDSTS = 0x8C, /** Internal DMAC Status */
  65. IDINTEN = 0x90, /** Internal DMAC Interrupt Enable */
  66. DSCADDR = 0x94, /** Current Host Descriptor Address */
  67. BUFADDR = 0x98, /** Current Host Buffer Address */
  68. FIFODAT = 0x200, /** FIFO data read write */
  69. };
  70. /* Control register definitions */
  71. #define CTRL_RESET 0x00000001
  72. #define FIFO_RESET 0x00000002
  73. #define DMA_RESET 0x00000004
  74. #define INT_ENABLE 0x00000010
  75. #define READ_WAIT 0x00000040
  76. #define CTRL_USE_IDMAC 0x02000000
  77. /* Interrupt mask defines */
  78. #define INTMSK_CDETECT 0x00000001
  79. #define INTMSK_RESP_ERR 0x00000002
  80. #define INTMSK_CMD_DONE 0x00000004
  81. #define INTMSK_DAT_OVER 0x00000008
  82. #define INTMSK_TXDR 0x00000010
  83. #define INTMSK_RXDR 0x00000020
  84. #define INTMSK_RCRC 0x00000040
  85. #define INTMSK_DCRC 0x00000080
  86. #define INTMSK_RTO 0x00000100
  87. #define INTMSK_DTO 0x00000200
  88. #define INTMSK_HTO 0x00000400
  89. #define INTMSK_VSI INTMSK_HTO // VSI => Voltage Switch Interrupt
  90. #define INTMSK_FRUN 0x00000800
  91. #define INTMSK_HLE 0x00001000
  92. #define INTMSK_SBE 0x00002000
  93. #define INTMSK_ACD 0x00004000
  94. #define INTMSK_EBE 0x00008000
  95. #define INTMSK_SDIO 0x00010000
  96. #define INTMASK_ERROR (INTMSK_RESP_ERR|INTMSK_RCRC|INTMSK_DCRC|INTMSK_RTO|INTMSK_DTO|INTMSK_HTO|INTMSK_FRUN|INTMSK_HLE|INTMSK_SBE|INTMSK_EBE)
  97. /*BMOD register define */
  98. #define BMOD_SWR 0x00000001
  99. #define BMOD_DE 0x00000080
  100. /* for STATUS register */
  101. #define GET_FIFO_COUNT(x) (((x)&0x3ffe0000)>>17)
  102. #define GET_FIFO_DEPTH(x) ((((x)&0x0FFF0000)>>16)+1)
  103. /* for IDMA intr register */
  104. #define IDMAINTBITS 0x337
  105. /* PMU related registers */
  106. //#define PMU_REG_BASE 0xF0000000
  107. #define PMU_REG_RST 0xF0000100
  108. #define PMU_REG_CLK_DIV3 0xF0000030
  109. #define PMU_SDC0_RST_BIT 9
  110. #define PMU_SDC1_RST_BIT 10
  111. #define PMU_RST_MODULE(x) *((volatile unsigned int*)(PMU_REG_RST)) = (~((1<<(x))))
  112. /* Define Card status bits (R1 response) */
  113. #define R1CS_ADDRESS_OUT_OF_RANGE 0x80000000
  114. #define R1CS_ADDRESS_MISALIGN 0x40000000
  115. #define R1CS_BLOCK_LEN_ERR 0x20000000
  116. #define R1CS_ERASE_SEQ_ERR 0x10000000
  117. #define R1CS_ERASE_PARAM 0x08000000
  118. #define R1CS_WP_VIOLATION 0x04000000
  119. #define R1CS_CARD_IS_LOCKED 0x02000000
  120. #define R1CS_LCK_UNLCK_FAILED 0x01000000
  121. #define R1CS_COM_CRC_ERROR 0x00800000
  122. #define R1CS_ILLEGAL_COMMAND 0x00400000
  123. #define R1CS_CARD_ECC_FAILED 0x00200000
  124. #define R1CS_CC_ERROR 0x00100000
  125. #define R1CS_ERROR 0x00080000
  126. #define R1CS_UNDERRUN 0x00040000
  127. #define R1CS_OVERRUN 0x00020000
  128. #define R1CS_CSD_OVERWRITE 0x00010000
  129. #define R1CS_WP_ERASE_SKIP 0x00008000
  130. #define R1CS_RESERVED_0 0x00004000
  131. #define R1CS_ERASE_RESET 0x00002000
  132. #define R1CS_CURRENT_STATE_MASK 0x00001e00
  133. #define R1CS_READY_FOR_DATA 0x00000100
  134. #define R1CS_SWITCH_ERROR 0x00000080
  135. #define R1CS_RESERVED_1 0x00000040
  136. #define R1CS_APP_CMD 0x00000020
  137. #define R1CS_RESERVED_2 0x00000010
  138. #define R1CS_APP_SPECIFIC_MASK 0x0000000c
  139. #define R1CS_MANUFAC_TEST_MASK 0x00000003
  140. #define R1CS_ERROR_OCCURED_MAP 0xfdffa080
  141. #define R1CS_CURRENT_STATE(x) (((x)&R1CS_CURRENT_STATE_MASK)>>9)
  142. /* R5 response */
  143. #define R5_IO_CRC_ERR 0x00008000
  144. #define R5_IO_BAD_CMD 0x00004000
  145. #define R5_IO_GEN_ERR 0x00000800
  146. #define R5_IO_FUNC_ERR 0x00000200
  147. #define R5_IO_OUT_RANGE 0x00000100
  148. #define R5_IO_ERR_BITS 0x0000cb00
  149. enum {
  150. NONE_TYPE = 0,
  151. SD_TYPE,
  152. SD_2_0_TYPE,
  153. SDIO_TYPE,
  154. };
  155. enum {
  156. CARD_STATE_EMPTY = -1,
  157. CARD_STATE_IDLE = 0,
  158. CARD_STATE_READY = 1,
  159. CARD_STATE_IDENT = 2,
  160. CARD_STATE_STBY = 3,
  161. CARD_STATE_TRAN = 4,
  162. CARD_STATE_DATA = 5,
  163. CARD_STATE_RCV = 6,
  164. CARD_STATE_PRG = 7,
  165. CARD_STATE_DIS = 8,
  166. CARD_STATE_INA = 9
  167. };
  168. enum DmaDescriptorDES1 // Buffer's size field of Descriptor
  169. {
  170. DescBuf2SizMsk = 0x03FFE000, /* Mask for Buffer2 Size 25:13 */
  171. DescBuf2SizeShift = 13, /* Shift value for Buffer2 Size */
  172. DescBuf1SizMsk = 0x00001FFF, /* Mask for Buffer1 Size 12:0 */
  173. DescBuf1SizeShift = 0, /* Shift value for Buffer2 Size */
  174. };
  175. enum DmaDescriptorDES0 // Control and status word of DMA descriptor DES0
  176. {
  177. DescOwnByDma = 0x80000000, /* (OWN)Descriptor is owned by DMA engine 31 */
  178. DescCardErrSummary = 0x40000000, /* Indicates EBE/RTO/RCRC/SBE/DRTO/DCRC/RE 30 */
  179. DescEndOfRing = 0x00000020, /* A "1" indicates End of Ring for Ring Mode 05 */
  180. DescSecAddrChained = 0x00000010, /* A "1" indicates DES3 contains Next Desc Address 04 */
  181. DescFirstDesc = 0x00000008, /* A "1" indicates this Desc contains first 03
  182. buffer of the data */
  183. DescLastDesc = 0x00000004, /* A "1" indicates buffer pointed to by this this 02
  184. Desc contains last buffer of Data */
  185. DescDisInt = 0x00000002, /* A "1" in this field disables the RI/TI of IDSTS 01
  186. for data that ends in the buffer pointed to by
  187. this descriptor */
  188. };
  189. typedef struct DmaDescStruct {
  190. unsigned int desc0; /* control and status information of descriptor */
  191. unsigned int desc1; /* buffer sizes */
  192. unsigned int desc2; /* physical address of the buffer 1 */
  193. unsigned int desc3; /* physical address of the buffer 2 */
  194. }DmaDesc;
  195. typedef struct {
  196. unsigned int wkmod;
  197. volatile DmaDesc *pDmaDesc;
  198. unsigned int idma_support;
  199. unsigned int rca;
  200. unsigned int ip_base;
  201. unsigned int card_type;
  202. unsigned int fifo_depth;
  203. unsigned int fifo_threth;
  204. unsigned int sectors;
  205. unsigned int scr[2];
  206. unsigned int csd[4];
  207. unsigned int idsts;
  208. rt_sem_t sem;
  209. rt_sem_t mutex;
  210. void (*cb)(void);
  211. } sdc_t;
  212. #define ONE_BIT_MODE (0)
  213. #define FOUR_BIT_MODE (1)
  214. #define BE32_TO_CPU(x) ((unsigned int)( \
  215. (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
  216. (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) | \
  217. (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) | \
  218. (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24)))
  219. #define synopmob_set_bits(reg, bit_id) *((volatile unsigned int *)(reg)) |= ((unsigned int)(bit_id))
  220. #define synopmob_clear_bits(reg, bit_id) *((volatile unsigned int *)(reg)) &= (~((unsigned int)(bit_id)))
  221. #define synopmob_set_register(reg, val) *((volatile unsigned int*)(reg)) = (val)
  222. #define synopmob_read_register(reg) (*((volatile unsigned int*)(reg)))
  223. enum {
  224. ERRNOERROR = 0,
  225. // for raw interrupt status error
  226. ERRRESPRECEP, // 1
  227. ERRRESPCRC,
  228. ERRDCRC,
  229. ERRRESPTIMEOUT,
  230. ERRDRTIMEOUT,
  231. ERRUNDERWRITE,
  232. ERROVERREAD,
  233. ERRHLE,
  234. ERRSTARTBIT,
  235. ERRENDBITERR, // 10
  236. // for R1 response
  237. ERRADDRESSRANGE, // 11
  238. ERRADDRESSMISALIGN,
  239. ERRBLOCKLEN,
  240. ERRERASESEQERR,
  241. ERRERASEPARAM,
  242. ERRPROT,
  243. ERRCARDLOCKED,
  244. ERRCRC,
  245. ERRILLEGALCOMMAND,
  246. ERRECCFAILED,
  247. ERRCCERR,
  248. ERRUNKNOWN,
  249. ERRUNDERRUN,
  250. ERROVERRUN,
  251. ERRCSDOVERWRITE,
  252. ERRERASERESET,
  253. ERRFSMSTATE, // 27
  254. // for R5 response
  255. ERRBADFUNC, // 28
  256. // others
  257. ERRCARDNOTCONN, // 29
  258. ERRCARDWPROTECT,
  259. ERRCMDRETRIESOVER,
  260. ERRNOTSUPPORTED,
  261. ERRHARDWARE,
  262. ERRDATANOTREADY,
  263. ERRCARDINTERNAL,
  264. ERRACMD41TIMEOUT,
  265. ERRIDMA,
  266. ERRNORES,
  267. ERRNOTEQUAL,
  268. };
  269. #ifdef __ASIC_BRANCH__
  270. #define SDC_WHERE() *((volatile unsigned int*)0x9b800000) = ((unsigned int)(__LINE__))
  271. #define SDC_ERROR() do { *((volatile unsigned int*)0x9b800008)=ret; *((volatile unsigned int*)0x9a7ff000) = 0;}while(0)
  272. #define SDC_FINISH() *((volatile unsigned int*)0x9a7ff004) = 0;
  273. #else
  274. #define SDC_WHERE()
  275. #define SDC_ERROR()
  276. #define SDC_FINISH()
  277. #endif //__ASIC_BRANCH__
  278. #define SDC_WKMOD_25M_STAND_SPEED 0x0002 // 25M standard speed
  279. #define SDC_WKMOD_50M_HI_SPEED 0x0004 // 50M high speed
  280. #define SDC_WKMOD_1WIRE 0x0008 // 1 wire transfer mode
  281. #define SDC_WKMOD_4WIRE 0x0010 // 4 wire transfer mode
  282. #define SDC_RW_USE_DMA 0x80000000 // Use DMA transfer?
  283. typedef void* HSDC;
  284. // map to DmaDesc
  285. typedef struct _buf_chain {
  286. unsigned int csi; // desc0
  287. unsigned int size; // desc1
  288. void * buf; // desc2
  289. struct _buf_chain *next; // desc3
  290. }buf_chain_t;
  291. /*
  292. * for SD card
  293. */
  294. extern int sdc_is_connected(unsigned int which);
  295. extern int sdc_init(unsigned int which, unsigned int wkmod, unsigned int* dma_desc/*4Byte aligned,16 bytes space*/, HSDC* phandle);
  296. extern int sdc_read_block(HSDC handle, unsigned int blk, unsigned int num, unsigned char* buffer);
  297. extern int sdc_write_block(HSDC handle, unsigned int blk, unsigned int num, unsigned char* buffer);
  298. extern int sdc_erase_block(HSDC handle, unsigned int blk, unsigned int num);
  299. extern int sdc_get_sector_num(HSDC handle);
  300. extern int sdc_set_clk_divider(unsigned int divider);
  301. /*
  302. * for SDIO WIFI card
  303. */
  304. extern void fh_sdio0_init(void);
  305. extern void fh_sdio1_init(void);
  306. extern void fh_sdio_init(void);
  307. extern int sdio_init(unsigned int which, unsigned int wkmod, unsigned int* dma_desc/*4Byte aligned,16 bytes space*/, HSDC* phandle);
  308. extern int sdio_high_speed_mode(HSDC handle, int bitwidth, int freq);
  309. extern int sdio_enable_card_int(HSDC handle, int enable);
  310. extern int sdio_set_card_int_cb(HSDC handle, void (*cb)(void));
  311. extern int sdio_drv_read(HSDC handle, unsigned int addr, unsigned int fn, unsigned int bcnt, unsigned int bsize, unsigned char *buf);
  312. extern int sdio_drv_write(HSDC handle, unsigned int addr, unsigned int fn, unsigned int bcnt, unsigned int bsize, unsigned char *buf);
  313. extern int sdio_drv_creg_read(HSDC handle, int addr, int fn, unsigned int *resp);
  314. extern int sdio_drv_creg_write(HSDC handle, int addr, int fn, unsigned char data, unsigned int *resp);
  315. extern void inv_dcache_range(unsigned long start, unsigned long len);
  316. extern void flush_dcache_range(unsigned long start, unsigned long len);
  317. #endif //__sdcard_h__