sdio.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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_SDIO_H_
  30. #define _DRIVER_CHIP_SDMMC_SDIO_H_
  31. #include "card.h"
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #undef readl
  36. #undef writel
  37. #ifndef printk
  38. #define printk rt_kprintf
  39. #endif
  40. #define readl hal_readl
  41. #define writel hal_writel
  42. #ifdef CONFIG_USE_SDIO
  43. /*
  44. * SDIO function CIS tuple (unknown to the core)
  45. */
  46. struct sdio_func_tuple {
  47. struct sdio_func_tuple *next;
  48. unsigned char code;
  49. unsigned char size;
  50. unsigned char data[0];
  51. };
  52. //#define SDIO_DEBUG
  53. #define SDIO_EXCLUSIVE_HOST
  54. /* SDIO commands type argument response */
  55. #define SD_IO_SEND_OP_COND 5 /* bcr [23:0] OCR R4 */
  56. #define SD_IO_RW_DIRECT 52 /* ac [31:0] See below R5 */
  57. #define SD_IO_RW_EXTENDED 53 /* adtc [31:0] See below R5 */
  58. /*
  59. * SD_IO_RW_DIRECT argument format:
  60. *
  61. * [31] R/W flag
  62. * [30:28] Function number
  63. * [27] RAW flag
  64. * [25:9] Register address
  65. * [7:0] Data
  66. */
  67. /*
  68. * SD_IO_RW_EXTENDED argument format:
  69. *
  70. * [31] R/W flag
  71. * [30:28] Function number
  72. * [27] Block mode
  73. * [26] Increment address
  74. * [25:9] Register address
  75. * [8:0] Byte/block count
  76. */
  77. #define R4_18V_PRESENT (1<<24)
  78. #define R4_MEMORY_PRESENT (1 << 27)
  79. //#define CONFIG_SDIO_IRQ_SUPPORT
  80. /*
  81. SDIO status in R5
  82. Type
  83. e : error bit
  84. s : status bit
  85. r : detected and set for the actual command response
  86. x : detected and set during command execution. the host must poll
  87. the card by sending status command in order to read these bits.
  88. Clear condition
  89. a : according to the card state
  90. b : always related to the previous command. Reception of
  91. a valid command will clear it (with a delay of one command)
  92. c : clear by read
  93. */
  94. #define R5_COM_CRC_ERROR (1 << 15) /* er, b */
  95. #define R5_ILLEGAL_COMMAND (1 << 14) /* er, b */
  96. #define R5_ERROR (1 << 11) /* erx, c */
  97. #define R5_FUNCTION_NUMBER (1 << 9) /* er, c */
  98. #define R5_OUT_OF_RANGE (1 << 8) /* er, c */
  99. #define R5_STATUS(x) (x & 0xCB00)
  100. #define R5_IO_CURRENT_STATE(x) ((x & 0x3000) >> 12) /* s, b */
  101. /*
  102. * Card Common Control Registers (CCCR)
  103. */
  104. #define SDIO_CCCR_CCCR 0x00
  105. #define SDIO_CCCR_REV_1_00 0 /* CCCR/FBR Version 1.00 */
  106. #define SDIO_CCCR_REV_1_10 1 /* CCCR/FBR Version 1.10 */
  107. #define SDIO_CCCR_REV_1_20 2 /* CCCR/FBR Version 1.20 */
  108. #define SDIO_CCCR_REV_3_00 3 /* CCCR/FBR Version 3.00 */
  109. #define SDIO_SDIO_REV_1_00 0 /* SDIO Spec Version 1.00 */
  110. #define SDIO_SDIO_REV_1_10 1 /* SDIO Spec Version 1.10 */
  111. #define SDIO_SDIO_REV_1_20 2 /* SDIO Spec Version 1.20 */
  112. #define SDIO_SDIO_REV_2_00 3 /* SDIO Spec Version 2.00 */
  113. #define SDIO_SDIO_REV_3_00 4 /* SDIO Spec Version 3.00 */
  114. #define SDIO_CCCR_SD 0x01
  115. #define SDIO_SD_REV_1_01 0 /* SD Physical Spec Version 1.01 */
  116. #define SDIO_SD_REV_1_10 1 /* SD Physical Spec Version 1.10 */
  117. #define SDIO_SD_REV_2_00 2 /* SD Physical Spec Version 2.00 */
  118. #define SDIO_SD_REV_3_00 3 /* SD Physical Spev Version 3.00 */
  119. #define SDIO_CCCR_IOEx 0x02
  120. #define SDIO_CCCR_IORx 0x03
  121. #define SDIO_CCCR_IENx 0x04 /* Function/Master Interrupt Enable */
  122. #define SDIO_CCCR_INTx 0x05 /* Function Interrupt Pending */
  123. #define SDIO_CCCR_ABORT 0x06 /* function abort/card reset */
  124. #define SDIO_CCCR_IF 0x07 /* bus interface controls */
  125. #define SDIO_BUS_WIDTH_1BIT 0x00
  126. #define SDIO_BUS_WIDTH_4BIT 0x02
  127. #define SDIO_BUS_ECSI 0x20 /* Enable continuous SPI interrupt */
  128. #define SDIO_BUS_SCSI 0x40 /* Support continuous SPI interrupt */
  129. #define SDIO_BUS_ASYNC_INT 0x20
  130. #define SDIO_BUS_CD_DISABLE 0x80 /* disable pull-up on DAT3 (pin 1) */
  131. #define SDIO_CCCR_CAPS 0x08
  132. #define SDIO_CCCR_CAP_SDC 0x01 /* can do CMD52 while data transfer */
  133. #define SDIO_CCCR_CAP_SMB 0x02 /* can do multi-block xfers (CMD53) */
  134. #define SDIO_CCCR_CAP_SRW 0x04 /* supports read-wait protocol */
  135. #define SDIO_CCCR_CAP_SBS 0x08 /* supports suspend/resume */
  136. #define SDIO_CCCR_CAP_S4MI 0x10 /* interrupt during 4-bit CMD53 */
  137. #define SDIO_CCCR_CAP_E4MI 0x20 /* enable ints during 4-bit CMD53 */
  138. #define SDIO_CCCR_CAP_LSC 0x40 /* low speed card */
  139. #define SDIO_CCCR_CAP_4BLS 0x80 /* 4 bit low speed card */
  140. #define SDIO_CCCR_CIS 0x09 /* common CIS pointer (3 bytes) */
  141. /* Following 4 regs are valid only if SBS is set */
  142. #define SDIO_CCCR_SUSPEND 0x0c
  143. #define SDIO_CCCR_SELx 0x0d
  144. #define SDIO_CCCR_EXECx 0x0e
  145. #define SDIO_CCCR_READYx 0x0f
  146. #define SDIO_CCCR_BLKSIZE 0x10
  147. #define SDIO_CCCR_POWER 0x12
  148. #define SDIO_POWER_SMPC 0x01 /* Supports Master Power Control */
  149. #define SDIO_POWER_EMPC 0x02 /* Enable Master Power Control */
  150. #define SDIO_CCCR_SPEED 0x13
  151. #define SDIO_SPEED_SHS 0x01 /* Supports High-Speed mode */
  152. #define SDIO_SPEED_BSS_SHIFT 1
  153. #define SDIO_SPEED_BSS_MASK (7<<SDIO_SPEED_BSS_SHIFT)
  154. #define SDIO_SPEED_SDR12 (0<<SDIO_SPEED_BSS_SHIFT)
  155. #define SDIO_SPEED_SDR25 (1<<SDIO_SPEED_BSS_SHIFT)
  156. #define SDIO_SPEED_SDR50 (2<<SDIO_SPEED_BSS_SHIFT)
  157. #define SDIO_SPEED_SDR104 (3<<SDIO_SPEED_BSS_SHIFT)
  158. #define SDIO_SPEED_DDR50 (4<<SDIO_SPEED_BSS_SHIFT)
  159. #define SDIO_SPEED_EHS SDIO_SPEED_SDR25 /* Enable High-Speed */
  160. #define SDIO_CCCR_UHS 0x14
  161. #define SDIO_UHS_SDR50 0x01
  162. #define SDIO_UHS_SDR104 0x02
  163. #define SDIO_UHS_DDR50 0x04
  164. #define SDIO_CCCR_DRIVE_STRENGTH 0x15
  165. #define SDIO_SDTx_MASK 0x07
  166. #define SDIO_DRIVE_SDTA (1<<0)
  167. #define SDIO_DRIVE_SDTC (1<<1)
  168. #define SDIO_DRIVE_SDTD (1<<2)
  169. #define SDIO_DRIVE_DTSx_MASK 0x03
  170. #define SDIO_DRIVE_DTSx_SHIFT 4
  171. #define SDIO_DTSx_SET_TYPE_B (0 << SDIO_DRIVE_DTSx_SHIFT)
  172. #define SDIO_DTSx_SET_TYPE_A (1 << SDIO_DRIVE_DTSx_SHIFT)
  173. #define SDIO_DTSx_SET_TYPE_C (2 << SDIO_DRIVE_DTSx_SHIFT)
  174. #define SDIO_DTSx_SET_TYPE_D (3 << SDIO_DRIVE_DTSx_SHIFT)
  175. /*
  176. * Function Basic Registers (FBR)
  177. */
  178. #define SDIO_FBR_BASE(f) ((f) * 0x100) /* base of function f's FBRs */
  179. #define SDIO_FBR_STD_IF 0x00
  180. #define SDIO_FBR_SUPPORTS_CSA 0x40 /* supports Code Storage Area */
  181. #define SDIO_FBR_ENABLE_CSA 0x80 /* enable Code Storage Area */
  182. #define SDIO_FBR_STD_IF_EXT 0x01
  183. #define SDIO_FBR_POWER 0x02
  184. #define SDIO_FBR_POWER_SPS 0x01 /* Supports Power Selection */
  185. #define SDIO_FBR_POWER_EPS 0x02 /* Enable (low) Power Selection */
  186. #define SDIO_FBR_CIS 0x09 /* CIS pointer (3 bytes) */
  187. #define SDIO_FBR_CSA 0x0C /* CSA pointer (3 bytes) */
  188. #define SDIO_FBR_CSA_DATA 0x0F
  189. #define SDIO_FBR_BLKSIZE 0x10 /* block size (2 bytes) */
  190. /* Misc. helper definitions */
  191. #define FN(x) (x)
  192. #define FN0 FN(0)
  193. #define FN1 FN(1)
  194. #define FN2 FN(2)
  195. #define FN3 FN(3)
  196. #define FN4 FN(4)
  197. #define FN5 FN(5)
  198. #define FN6 FN(6)
  199. #define FN7 FN(7)
  200. struct mmc_card;
  201. typedef struct mmc_card sdio_t;
  202. /**
  203. * sdio_readb - read a single byte from a SDIO function
  204. * @card: SDIO to access
  205. * @addr: address to read
  206. * @err_ret: optional status value from transfer
  207. *
  208. * Reads a single byte from the address space of a given SDIO
  209. * function. If there is a problem reading the address, 0xff
  210. * is returned and @err_ret will contain the error code.
  211. */
  212. extern uint8_t
  213. sdio_readb(struct mmc_card *card, uint32_t func_num, uint32_t addr,
  214. int32_t *err_ret);
  215. /**
  216. * sdio_writeb - write a single byte to a SDIO function
  217. * @card: SDIO to access
  218. * @b: byte to write
  219. * @addr: address to write to
  220. * @err_ret: optional status value from transfer
  221. *
  222. * Writes a single byte to the address space of a given SDIO
  223. * function. @err_ret will contain the status of the actual
  224. * transfer.
  225. */
  226. extern void
  227. sdio_writeb(struct mmc_card *card, uint32_t func_num, const uint8_t b,
  228. uint32_t addr, int32_t *err_ret);
  229. /**
  230. * sdio_readw - read a 16 bit integer from a SDIO function
  231. * @func: SDIO function to access
  232. * @addr: address to read
  233. * @err_ret: optional status value from transfer
  234. *
  235. * Reads a 16 bit integer from the address space of a given SDIO
  236. * function. If there is a problem reading the address, 0xffff
  237. * is returned and @err_ret will contain the error code.
  238. */
  239. extern uint16_t sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret);
  240. /**
  241. * sdio_writew - write a 16 bit integer to a SDIO function
  242. * @func: SDIO function to access
  243. * @b: integer to write
  244. * @addr: address to write to
  245. * @err_ret: optional status value from transfer
  246. *
  247. * Writes a 16 bit integer to the address space of a given SDIO
  248. * function. @err_ret will contain the status of the actual
  249. * transfer.
  250. */
  251. extern void sdio_writew(struct sdio_func *func, uint16_t b, unsigned int addr, int *err_ret);
  252. /**
  253. * sdio_readl - read a 32 bit integer from a SDIO function
  254. * @func: SDIO function to access
  255. * @addr: address to read
  256. * @err_ret: optional status value from transfer
  257. *
  258. * Reads a 32 bit integer from the address space of a given SDIO
  259. * function. If there is a problem reading the address,
  260. * 0xffffffff is returned and @err_ret will contain the error
  261. * code.
  262. */
  263. extern uint32_t sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret);
  264. /**
  265. * sdio_writel - write a 32 bit integer to a SDIO function
  266. * @func: SDIO function to access
  267. * @b: integer to write
  268. * @addr: address to write to
  269. * @err_ret: optional status value from transfer
  270. *
  271. * Writes a 32 bit integer to the address space of a given SDIO
  272. * function. @err_ret will contain the status of the actual
  273. * transfer.
  274. */
  275. extern void sdio_writel(struct sdio_func *func, uint32_t b, unsigned int addr, int *err_ret);
  276. /**
  277. * memcpy_fromio - read a chunk of memory from a SDIO function
  278. * @dst: buffer to store the data
  279. * @addr: address to begin reading from
  280. * @count: number of bytes to read
  281. *
  282. * Reads from the address space of a given SDIO function. Return
  283. * value indicates if the transfer succeeded or not.
  284. */
  285. extern int
  286. sdio_memcpy_fromio(struct mmc_card *card, unsigned int func_num, void *dst,
  287. unsigned int addr, int count);
  288. /**
  289. * memcpy_toio - write a chunk of memory to a SDIO function
  290. * @addr: address to start writing to
  291. * @src: buffer that contains the data to write
  292. * @count: number of bytes to write
  293. *
  294. * Writes to the address space of a given SDIO function. Return
  295. * value indicates if the transfer succeeded or not.
  296. */
  297. extern int
  298. sdio_memcpy_toio(struct mmc_card *card, unsigned int func_num, unsigned int addr,
  299. const void *src, int count);
  300. /**
  301. * sdio_claim_irq - claim the IRQ for a SDIO function
  302. * @card: SDIO card
  303. * @func_num: function num
  304. * @handler: IRQ handler callback
  305. *
  306. * Claim and activate the IRQ for the given SDIO function. The provided
  307. * handler will be called when that IRQ is asserted. The host is always
  308. * claimed already when the handler is called so the handler must not
  309. * call sdio_claim_host() nor sdio_release_host().
  310. */
  311. //extern int sdio_claim_irq(struct mmc_card *card, unsigned int func_num,
  312. // sdio_irq_handler_t *handler);
  313. extern int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler);
  314. /**
  315. * sdio_release_irq - release the IRQ for a SDIO function
  316. * @card: SDIO card
  317. * @func_num: function num
  318. *
  319. * Disable and release the IRQ for the given SDIO function.
  320. */
  321. //extern int sdio_release_irq(struct mmc_card *card, unsigned int func_num);
  322. extern int sdio_release_irq(struct sdio_func *func);
  323. /**
  324. * sdio_align_size - pads a transfer size to a more optimal value
  325. * @func: SDIO function
  326. * @sz: original transfer size
  327. *
  328. * Pads the original data size with a number of extra bytes in
  329. * order to avoid controller bugs and/or performance hits
  330. * (e.g. some controllers revert to PIO for certain sizes).
  331. *
  332. * If possible, it will also adjust the size so that it can be
  333. * handled in just a single request.
  334. *
  335. * Returns the improved size, which might be unmodified.
  336. */
  337. //unsigned int sdio_align_size(struct mmc_card *card, unsigned int sz);
  338. unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz);
  339. /**
  340. * claim_host - exclusively claim a bus for a certain SDIO function
  341. * @func: SDIO function that will be accessed
  342. *
  343. * Claim a bus for a set of operations. The SDIO function given
  344. * is used to figure out which bus is relevant.
  345. */
  346. #ifdef SDIO_EXCLUSIVE_HOST
  347. extern void sdio_claim_host(struct mmc_card *card);
  348. #else
  349. static inline void sdio_claim_host(struct mmc_card *card) {
  350. #ifdef SDIO_DEBUG
  351. if (card->_bus_refs)
  352. BUG_ON(1); /* check when need debug */
  353. card->_bus_refs++;
  354. #endif
  355. }
  356. #endif
  357. /**
  358. * release_host - release a bus for a certain SDIO function
  359. * @func: SDIO function that was accessed
  360. *
  361. * Release a bus, allowing others to claim the bus for their
  362. * operations.
  363. */
  364. #ifdef SDIO_EXCLUSIVE_HOST
  365. extern void sdio_release_host(struct mmc_card *card);
  366. #else
  367. static inline void sdio_release_host(struct mmc_card *card) {
  368. #ifdef SDIO_DEBUG
  369. card->_bus_refs--;
  370. if (card->_bus_refs)
  371. BUG_ON(1); /* check when need debug */
  372. #endif
  373. }
  374. #endif
  375. static inline void sdio_lock(struct mmc_card *card) {
  376. sdio_claim_host(card);
  377. }
  378. static inline void sdio_unlock(struct mmc_card *card) {
  379. sdio_release_host(card);
  380. }
  381. extern int32_t sdio_set_block_size(struct mmc_card *card, uint32_t fn_num,
  382. uint32_t blksz);
  383. extern int32_t sdio_enable_func(struct mmc_card *card, uint32_t func_num);
  384. extern int32_t sdio_disable_func(struct mmc_card *card, uint32_t func_num);
  385. extern int32_t sdio_pm(sdio_t *card, int32_t suspend);
  386. extern void sdio_test(void);
  387. extern int mmc_io_rw_direct(struct mmc_card *card, int32_t write, uint32_t fn, uint32_t addr, uint8_t in, uint8_t *out);
  388. extern struct sdio_func ** get_mmc_card_func(uint8_t card_id);
  389. #endif /* CONFIG_USE_SDIO */
  390. #ifdef __cplusplus
  391. }
  392. #endif
  393. #endif /* _DRIVER_CHIP_SDMMC_SDIO_H_ */