card.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the
  12. * distribution.
  13. * 3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef _DRIVER_CHIP_SDMMC_CARD_H_
  30. #define _DRIVER_CHIP_SDMMC_CARD_H_
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #include "osal/os_mutex.h"
  35. #define CONFIG_SDIO_USE_FUNS
  36. //#define CONFIG_USE_SDIO_COMBO
  37. //#define SD_SUPPORT_VERSION3 /* not support for not support 1V8 */ #error !!
  38. //#define CONFIG_USE_MMC_QUIRK /* not support now */ #error !!
  39. //#define CONFIG_SDIO_USE_FUNS /* close to save code. and not support now */ #error !!
  40. #define SYSTEM_SIMULATION
  41. #ifdef CONFIG_USE_SDIO
  42. struct sdio_func;
  43. typedef void (sdio_irq_handler_t)(struct sdio_func *);
  44. /*
  45. * SDIO function devices
  46. */
  47. struct sdio_func {
  48. struct mmc_card *card; /* the card this device belongs to */
  49. sdio_irq_handler_t *irq_handler; /* IRQ callback */
  50. uint32_t num; /* function number */
  51. unsigned char class; /* standard interface class */
  52. unsigned short vendor; /* vendor id */
  53. unsigned short device; /* device id */
  54. uint32_t max_blksize; /* maximum block size */
  55. uint32_t cur_blksize; /* current block size */
  56. uint32_t enable_timeout; /* max enable timeout in msec */
  57. uint32_t state; /* function state */
  58. #define SDIO_STATE_PRESENT (1<<0) /* present in sysfs */
  59. uint8_t tmpbuf[4]; /* DMA:able scratch buffer */
  60. unsigned num_info; /* number of info strings */
  61. const char **info; /* info strings */
  62. struct sdio_func_tuple *tuples;
  63. /*for rtl*/
  64. void *drv_prv;
  65. };
  66. #define sdio_func_present(f) ((f)->state & SDIO_STATE_PRESENT)
  67. #define sdio_func_set_present(f) ((f)->state |= SDIO_STATE_PRESENT)
  68. #endif
  69. struct mmc_ocr {
  70. union {
  71. uint32_t vol_window : 24,
  72. to_1v8_acpt : 1,
  73. : 5,
  74. high_capacity : 1,
  75. : 1;
  76. uint32_t ocr;
  77. };
  78. };
  79. struct mmc_cid {
  80. uint16_t oemid;
  81. uint8_t manfid;
  82. uint32_t serial;
  83. uint16_t year;
  84. uint8_t month;
  85. uint8_t hwrev;
  86. uint8_t fwrev;
  87. uint8_t prod_name[6];
  88. };
  89. struct mmc_csd {
  90. uint8_t csd_ver;
  91. //uint8_t c_size_mult;
  92. //uint16_t c_size;
  93. uint32_t max_dtr; /* max transfer speed */
  94. uint16_t read_blk_len;
  95. uint16_t cmdclass;
  96. uint32_t capacity;
  97. };
  98. struct sd_scr {
  99. uint8_t sda_vsn;
  100. uint8_t sda_spec3;
  101. uint8_t sda_spec4;
  102. uint8_t sda_spec5;
  103. uint8_t bus_widths;
  104. uint8_t security_sup;
  105. #define SD_SCR_BUS_WIDTH_1 (1<<0)
  106. #define SD_SCR_BUS_WIDTH_4 (1<<2)
  107. uint8_t cmds;
  108. #define SD_SCR_CMD20_SUPPORT (1<<0)
  109. #define SD_SCR_CMD23_SUPPORT (1<<1)
  110. };
  111. struct sd_ssr {
  112. uint32_t au; /* In sectors */
  113. uint32_t erase_timeout; /* In milliseconds */
  114. uint32_t erase_offset; /* In milliseconds */
  115. };
  116. struct sd_switch_caps {
  117. uint32_t hs_max_dtr;
  118. uint32_t uhs_max_dtr;
  119. #define HIGH_SPEED_MAX_DTR 50000000
  120. #define UHS_SDR104_MAX_DTR 208000000
  121. #define UHS_SDR50_MAX_DTR 100000000
  122. #define UHS_DDR50_MAX_DTR 50000000
  123. #define UHS_SDR25_MAX_DTR UHS_DDR50_MAX_DTR
  124. #define UHS_SDR12_MAX_DTR 25000000
  125. uint32_t sd3_bus_mode;
  126. #define UHS_SDR12_BUS_SPEED 0
  127. #define HIGH_SPEED_BUS_SPEED 1
  128. #define UHS_SDR25_BUS_SPEED 1
  129. #define UHS_SDR50_BUS_SPEED 2
  130. #define UHS_SDR104_BUS_SPEED 3
  131. #define UHS_DDR50_BUS_SPEED 4
  132. #define SD_MODE_HIGH_SPEED (1 << HIGH_SPEED_BUS_SPEED)
  133. #define SD_MODE_UHS_SDR12 (1 << UHS_SDR12_BUS_SPEED)
  134. #define SD_MODE_UHS_SDR25 (1 << UHS_SDR25_BUS_SPEED)
  135. #define SD_MODE_UHS_SDR50 (1 << UHS_SDR50_BUS_SPEED)
  136. #define SD_MODE_UHS_SDR104 (1 << UHS_SDR104_BUS_SPEED)
  137. #define SD_MODE_UHS_DDR50 (1 << UHS_DDR50_BUS_SPEED)
  138. uint32_t sd3_drv_type;
  139. #define SD_DRIVER_TYPE_B 0x01
  140. #define SD_DRIVER_TYPE_A 0x02
  141. #define SD_DRIVER_TYPE_C 0x04
  142. #define SD_DRIVER_TYPE_D 0x08
  143. uint32_t sd3_curr_limit;
  144. #define SD_SET_CURRENT_LIMIT_200 0
  145. #define SD_SET_CURRENT_LIMIT_400 1
  146. #define SD_SET_CURRENT_LIMIT_600 2
  147. #define SD_SET_CURRENT_LIMIT_800 3
  148. #define SD_MAX_CURRENT_200 (1 << SD_SET_CURRENT_LIMIT_200)
  149. #define SD_MAX_CURRENT_400 (1 << SD_SET_CURRENT_LIMIT_400)
  150. #define SD_MAX_CURRENT_600 (1 << SD_SET_CURRENT_LIMIT_600)
  151. #define SD_MAX_CURRENT_800 (1 << SD_SET_CURRENT_LIMIT_800)
  152. };
  153. struct mmc_ext_csd {
  154. uint8_t version;
  155. uint8_t card_type;
  156. uint8_t csd_struc;
  157. uint8_t hs_timing;
  158. uint8_t bus_width;
  159. uint8_t part_config;
  160. uint8_t boot_bus_cond;
  161. };
  162. struct sdio_cccr {
  163. uint32_t sdio_vsn;
  164. uint32_t sd_vsn;
  165. uint32_t multi_block:1,
  166. low_speed:1,
  167. wide_bus:1,
  168. high_power:1,
  169. high_speed:1,
  170. disable_cd:1;
  171. };
  172. struct sdio_cis {
  173. uint16_t vendor;
  174. uint16_t device;
  175. uint16_t blksize;
  176. uint32_t max_dtr;
  177. };
  178. struct mmc_host;
  179. #define SDIO_MAX_FUNCS 7
  180. /** @bried SD Card Init Structure definition. */
  181. typedef struct {
  182. uint16_t debug_mask;
  183. uint16_t type; /* set card type if we know to speed up scan card, MMC_TYPE_xx */
  184. } SDCard_InitTypeDef;
  185. struct mmc_card {
  186. uint16_t debug_mask;
  187. uint16_t suspend;
  188. //#if ((defined CONFIG_USE_SD) || (defined CONFIG_USE_MMC))
  189. /* register info. */
  190. struct mmc_cid cid;
  191. struct mmc_csd csd;
  192. struct sd_scr scr;
  193. struct sd_ssr ssr;
  194. struct mmc_ext_csd extcsd;
  195. struct sd_switch_caps sw_caps; /* switch (CMD6) caps */
  196. //#endif
  197. /* card information */
  198. uint32_t id;
  199. uint32_t type; /* card type */
  200. #define MMC_TYPE_MMC 1 /* MMC card */
  201. #define MMC_TYPE_SD 2 /* SD card */
  202. #define MMC_TYPE_SDIO 3 /* SDIO card */
  203. #define MMC_TYPE_SD_COMBO 4 /* SD combo (IO+mem) card */
  204. #define MMC_TYPE_MAX 5
  205. uint32_t sd_bus_speed; /* Bus Speed Mode set for the card */
  206. uint32_t state; /* (our) card state */
  207. #define MMC_STATE_PRESENT (1 << 0) /* present */
  208. #define MMC_STATE_READONLY (1 << 1) /* card is read-only */
  209. #define MMC_STATE_HIGHSPEED (1 << 2) /* card is in high speed mode */
  210. #define MMC_STATE_BLOCKADDR (1 << 3) /* card uses block-addressing */
  211. #define MMC_STATE_HIGHSPEED_DDR (1 << 4) /* card is in high speed mode */
  212. #define MMC_STATE_ULTRAHIGHSPEED (1<<5) /* card is in ultra high speed mode */
  213. #define MMC_CARD_SDXC (1<<6) /* card is SDXC */
  214. #define MMC_CARD_REMOVED (1<<7) /* card has been removed */
  215. #define MMC_STATE_HIGHSPEED_200 (1<<8) /* card is in HS200 mode */
  216. #define MMC_STATE_SLEEP (1<<9) /* card is in sleep state */
  217. //#ifdef CONFIG_USE_MMC_QUIRK
  218. uint32_t quirks; /* card quirks */
  219. #define MMC_QUIRK_LENIENT_FN0 (1 << 0) /* allow SDIO FN0 writes outside of the VS CCCR range */
  220. #define MMC_QUIRK_BLKSZ_FOR_BYTE_MODE (1 << 1) /* use func->cur_blksize */
  221. /* for byte mode */
  222. #define MMC_QUIRK_NONSTD_SDIO (1 << 2) /* non-standard SDIO card attached */
  223. /* (missing CIA registers) */
  224. #define MMC_QUIRK_BROKEN_CLK_GATING (1<<3) /* clock gating the sdio bus will make card fail */
  225. #define MMC_QUIRK_NONSTD_FUNC_IF (1<<4) /* SDIO card has nonstd function interfaces */
  226. #define MMC_QUIRK_DISABLE_CD (1<<5) /* disconnect CD/DAT[3] resistor */
  227. #define MMC_QUIRK_INAND_CMD38 (1<<6) /* iNAND devices have broken CMD38 */
  228. #define MMC_QUIRK_BLK_NO_CMD23 (1<<7) /* Avoid CMD23 for regular multiblock */
  229. #define MMC_QUIRK_BROKEN_BYTE_MODE_512 (1<<8) /* Avoid sending 512 bytes in */
  230. #define MMC_QUIRK_LONG_READ_TIME (1<<9) /* Data read time > CSD says */
  231. #define MMC_QUIRK_SEC_ERASE_TRIM_BROKEN (1<<10) /* Skip secure for erase/trim */
  232. //#endif
  233. /* missing CIA registers */
  234. //#ifdef CONFIG_SDIO_USE_FUNS
  235. uint32_t erase_size; /* erase size in sectors */
  236. uint32_t erase_shift; /* if erase unit is power 2 */
  237. uint32_t pref_erase; /* in sectors */
  238. uint8_t erased_byte; /* value of erased bytes */
  239. uint32_t raw_cid[4]; /* raw card CID */
  240. uint32_t raw_csd[4]; /* raw card CSD */
  241. uint32_t raw_scr[2]; /* raw card SCR */
  242. uint32_t sdio_funcs; /* number of SDIO functions */
  243. struct sdio_func *sdio_func[SDIO_MAX_FUNCS]; /* SDIO functions (devices) */
  244. uint32_t num_info; /* number of info strings */
  245. const int8_t **info; /* info strings */
  246. //#ifdef CONFIG_SDIO_IRQ_SUPPORT
  247. // sdio_irq_handler_t *sdio_single_irq; /* IRQ callback, all funs only support one for efficiency! */
  248. struct sdio_func *sdio_single_irq; /* SDIO function when only one IRQ active */
  249. //#endif
  250. //#endif
  251. #ifdef CONFIG_USE_SDIO
  252. uint32_t manfid;
  253. uint32_t cisptr[8];
  254. uint16_t fn_bsize[8];
  255. void *mem_info_p; //if with memory, to store information about memory portion
  256. //struct sdio_func *sdio_func[SDIO_MAX_FUNCS]; /* SDIO functions (devices) */
  257. struct sdio_cccr cccr; /* common card info */
  258. struct sdio_cis cis; /* common tuple info */
  259. struct sdio_func_tuple *tuples; /* unknown common tuples */
  260. #endif
  261. uint8_t bus_width;
  262. uint8_t speed_class;
  263. uint16_t ref;
  264. OS_Mutex_t mutex;
  265. uint32_t cidno[4];
  266. uint32_t rca; /* relative card address of device */
  267. struct mmc_ocr ocr;
  268. struct mmc_host *host; /* the host this device belongs to */
  269. };
  270. struct mmc_card_info {
  271. struct mmc_card card;
  272. struct sdio_func sdio_func[SDIO_MAX_FUNCS]; /* SDIO functions (devices) */
  273. //const int8_t **info; /* info strings */
  274. //struct sdio_func *sdio_single_irq; /* SDIO function when only one IRQ active */
  275. //void *mem_info_p; //if with memory, to store information about memory portion
  276. #ifdef CONFIG_USE_SDIO
  277. //struct sdio_func_tuple *tuples; /* unknown common tuples */
  278. #endif
  279. uint32_t sdc_id;
  280. };
  281. #define mmc_card_mmc(c) ((c)->type == MMC_TYPE_MMC)
  282. #define mmc_card_sd(c) ((c)->type == MMC_TYPE_SD)
  283. #define mmc_card_sdio(c) ((c)->type == MMC_TYPE_SDIO)
  284. #define mmc_card_sd_combo(c) ((c)->type == MMC_TYPE_SD_COMBO)
  285. #define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT)
  286. #define mmc_card_readonly(c) ((c)->state & MMC_STATE_READONLY)
  287. #define mmc_card_highspeed(c) ((c)->state & MMC_STATE_HIGHSPEED)
  288. #define mmc_card_hs200(c) ((c)->state & MMC_STATE_HIGHSPEED_200)
  289. #define mmc_card_blockaddr(c) ((c)->state & MMC_STATE_BLOCKADDR)
  290. #define mmc_card_ddr_mode(c) ((c)->state & MMC_STATE_HIGHSPEED_DDR)
  291. #define mmc_card_uhs(c) ((c)->state & MMC_STATE_ULTRAHIGHSPEED)
  292. #define mmc_sd_card_uhs(c) ((c)->state & MMC_STATE_ULTRAHIGHSPEED)
  293. #define mmc_card_ext_capacity(c) ((c)->state & MMC_CARD_SDXC)
  294. #define mmc_card_removed(c) ((c) && ((c)->state & MMC_CARD_REMOVED))
  295. #define mmc_card_is_sleep(c) ((c)->state & MMC_STATE_SLEEP)
  296. #define mmc_card_set_present(c) ((c)->state |= MMC_STATE_PRESENT)
  297. #define mmc_card_set_readonly(c) ((c)->state |= MMC_STATE_READONLY)
  298. #define mmc_card_set_highspeed(c) ((c)->state |= MMC_STATE_HIGHSPEED)
  299. #define mmc_card_set_hs200(c) ((c)->state |= MMC_STATE_HIGHSPEED_200)
  300. #define mmc_card_set_blockaddr(c) ((c)->state |= MMC_STATE_BLOCKADDR)
  301. #define mmc_card_set_ddr_mode(c) ((c)->state |= MMC_STATE_HIGHSPEED_DDR)
  302. #define mmc_card_set_uhs(c) ((c)->state |= MMC_STATE_ULTRAHIGHSPEED)
  303. #define mmc_sd_card_set_uhs(c) ((c)->state |= MMC_STATE_ULTRAHIGHSPEED)
  304. #define mmc_card_set_ext_capacity(c) ((c)->state |= MMC_CARD_SDXC)
  305. #define mmc_card_set_removed(c) ((c)->state |= MMC_CARD_REMOVED)
  306. #define mmc_card_set_sleep(c) ((c)->state |= MMC_STATE_SLEEP)
  307. #define mmc_card_clr_sleep(c) ((c)->state &= ~MMC_STATE_SLEEP)
  308. #ifdef __cplusplus
  309. }
  310. #endif
  311. #endif /* _DRIVER_CHIP_SDMMC_CARD_H_ */