tmio_mmc.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Driver for the MMC / SD / SDIO cell found in:
  4. *
  5. * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
  6. *
  7. * Copyright (C) 2015-19 Renesas Electronics Corporation
  8. * Copyright (C) 2016-19 Sang Engineering, Wolfram Sang
  9. * Copyright (C) 2016-17 Horms Solutions, Simon Horman
  10. * Copyright (C) 2007 Ian Molton
  11. * Copyright (C) 2004 Ian Molton
  12. */
  13. #ifndef TMIO_MMC_H
  14. #define TMIO_MMC_H
  15. #include <linux/dmaengine.h>
  16. #include <linux/highmem.h>
  17. #include <linux/mutex.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/workqueue.h>
  23. #define CTL_SD_CMD 0x00
  24. #define CTL_ARG_REG 0x04
  25. #define CTL_STOP_INTERNAL_ACTION 0x08
  26. #define CTL_XFER_BLK_COUNT 0xa
  27. #define CTL_RESPONSE 0x0c
  28. /* driver merges STATUS and following STATUS2 */
  29. #define CTL_STATUS 0x1c
  30. /* driver merges IRQ_MASK and following IRQ_MASK2 */
  31. #define CTL_IRQ_MASK 0x20
  32. #define CTL_SD_CARD_CLK_CTL 0x24
  33. #define CTL_SD_XFER_LEN 0x26
  34. #define CTL_SD_MEM_CARD_OPT 0x28
  35. #define CTL_SD_ERROR_DETAIL_STATUS 0x2c
  36. #define CTL_SD_DATA_PORT 0x30
  37. #define CTL_TRANSACTION_CTL 0x34
  38. #define CTL_SDIO_STATUS 0x36
  39. #define CTL_SDIO_IRQ_MASK 0x38
  40. #define CTL_DMA_ENABLE 0xd8
  41. #define CTL_RESET_SD 0xe0
  42. #define CTL_VERSION 0xe2
  43. #define CTL_SDIF_MODE 0xe6 /* only known on R-Car 2+ */
  44. /* Definitions for values the CTL_STOP_INTERNAL_ACTION register can take */
  45. #define TMIO_STOP_STP BIT(0)
  46. #define TMIO_STOP_SEC BIT(8)
  47. /* Definitions for values the CTL_STATUS register can take */
  48. #define TMIO_STAT_CMDRESPEND BIT(0)
  49. #define TMIO_STAT_DATAEND BIT(2)
  50. #define TMIO_STAT_CARD_REMOVE BIT(3)
  51. #define TMIO_STAT_CARD_INSERT BIT(4)
  52. #define TMIO_STAT_SIGSTATE BIT(5)
  53. #define TMIO_STAT_WRPROTECT BIT(7)
  54. #define TMIO_STAT_CARD_REMOVE_A BIT(8)
  55. #define TMIO_STAT_CARD_INSERT_A BIT(9)
  56. #define TMIO_STAT_SIGSTATE_A BIT(10)
  57. /* These belong technically to CTL_STATUS2, but the driver merges them */
  58. #define TMIO_STAT_CMD_IDX_ERR BIT(16)
  59. #define TMIO_STAT_CRCFAIL BIT(17)
  60. #define TMIO_STAT_STOPBIT_ERR BIT(18)
  61. #define TMIO_STAT_DATATIMEOUT BIT(19)
  62. #define TMIO_STAT_RXOVERFLOW BIT(20)
  63. #define TMIO_STAT_TXUNDERRUN BIT(21)
  64. #define TMIO_STAT_CMDTIMEOUT BIT(22)
  65. #define TMIO_STAT_DAT0 BIT(23) /* only known on R-Car so far */
  66. #define TMIO_STAT_RXRDY BIT(24)
  67. #define TMIO_STAT_TXRQ BIT(25)
  68. #define TMIO_STAT_ALWAYS_SET_27 BIT(27) /* only known on R-Car 2+ so far */
  69. #define TMIO_STAT_ILL_FUNC BIT(29) /* only when !TMIO_MMC_HAS_IDLE_WAIT */
  70. #define TMIO_STAT_SCLKDIVEN BIT(29) /* only when TMIO_MMC_HAS_IDLE_WAIT */
  71. #define TMIO_STAT_CMD_BUSY BIT(30)
  72. #define TMIO_STAT_ILL_ACCESS BIT(31)
  73. /* Definitions for values the CTL_SD_CARD_CLK_CTL register can take */
  74. #define CLK_CTL_DIV_MASK 0xff
  75. #define CLK_CTL_SCLKEN BIT(8)
  76. /* Definitions for values the CTL_SD_MEM_CARD_OPT register can take */
  77. #define CARD_OPT_TOP_MASK 0xf0
  78. #define CARD_OPT_TOP_SHIFT 4
  79. #define CARD_OPT_EXTOP BIT(9) /* first appeared on R-Car Gen3 SDHI */
  80. #define CARD_OPT_WIDTH8 BIT(13)
  81. #define CARD_OPT_ALWAYS1 BIT(14)
  82. #define CARD_OPT_WIDTH BIT(15)
  83. /* Definitions for values the CTL_SDIO_STATUS register can take */
  84. #define TMIO_SDIO_STAT_IOIRQ 0x0001
  85. #define TMIO_SDIO_STAT_EXPUB52 0x4000
  86. #define TMIO_SDIO_STAT_EXWT 0x8000
  87. #define TMIO_SDIO_MASK_ALL 0xc007
  88. #define TMIO_SDIO_SETBITS_MASK 0x0006
  89. /* Definitions for values the CTL_DMA_ENABLE register can take */
  90. #define DMA_ENABLE_DMASDRW BIT(1)
  91. /* Definitions for values the CTL_SDIF_MODE register can take */
  92. #define SDIF_MODE_HS400 BIT(0) /* only known on R-Car 2+ */
  93. /* Define some IRQ masks */
  94. /* This is the mask used at reset by the chip */
  95. #define TMIO_MASK_ALL 0x837f031d
  96. #define TMIO_MASK_ALL_RCAR2 0x8b7f031d
  97. #define TMIO_MASK_READOP (TMIO_STAT_RXRDY | TMIO_STAT_DATAEND)
  98. #define TMIO_MASK_WRITEOP (TMIO_STAT_TXRQ | TMIO_STAT_DATAEND)
  99. #define TMIO_MASK_CMD (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT | \
  100. TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT)
  101. #define TMIO_MASK_IRQ (TMIO_MASK_READOP | TMIO_MASK_WRITEOP | TMIO_MASK_CMD)
  102. #define TMIO_MAX_BLK_SIZE 512
  103. struct tmio_mmc_data;
  104. struct tmio_mmc_host;
  105. struct tmio_mmc_dma_ops {
  106. void (*start)(struct tmio_mmc_host *host, struct mmc_data *data);
  107. void (*enable)(struct tmio_mmc_host *host, bool enable);
  108. void (*request)(struct tmio_mmc_host *host,
  109. struct tmio_mmc_data *pdata);
  110. void (*release)(struct tmio_mmc_host *host);
  111. void (*abort)(struct tmio_mmc_host *host);
  112. void (*dataend)(struct tmio_mmc_host *host);
  113. /* optional */
  114. void (*end)(struct tmio_mmc_host *host); /* held host->lock */
  115. bool (*dma_irq)(struct tmio_mmc_host *host);
  116. };
  117. struct tmio_mmc_host {
  118. void __iomem *ctl;
  119. struct mmc_command *cmd;
  120. struct mmc_request *mrq;
  121. struct mmc_data *data;
  122. struct mmc_host *mmc;
  123. struct mmc_host_ops ops;
  124. /* pio related stuff */
  125. struct scatterlist *sg_ptr;
  126. struct scatterlist *sg_orig;
  127. unsigned int sg_len;
  128. unsigned int sg_off;
  129. unsigned int bus_shift;
  130. struct platform_device *pdev;
  131. struct tmio_mmc_data *pdata;
  132. /* DMA support */
  133. bool dma_on;
  134. struct dma_chan *chan_rx;
  135. struct dma_chan *chan_tx;
  136. struct work_struct dma_issue;
  137. struct scatterlist bounce_sg;
  138. u8 *bounce_buf;
  139. /* Track lost interrupts */
  140. struct delayed_work delayed_reset_work;
  141. struct work_struct done;
  142. /* Cache */
  143. u32 sdcard_irq_mask;
  144. u32 sdio_irq_mask;
  145. unsigned int clk_cache;
  146. u32 sdcard_irq_setbit_mask;
  147. u32 sdcard_irq_mask_all;
  148. spinlock_t lock; /* protect host private data */
  149. unsigned long last_req_ts;
  150. struct mutex ios_lock; /* protect set_ios() context */
  151. bool native_hotplug;
  152. bool sdio_irq_enabled;
  153. /* Mandatory callback */
  154. int (*clk_enable)(struct tmio_mmc_host *host);
  155. void (*set_clock)(struct tmio_mmc_host *host, unsigned int clock);
  156. /* Optional callbacks */
  157. void (*clk_disable)(struct tmio_mmc_host *host);
  158. int (*multi_io_quirk)(struct mmc_card *card,
  159. unsigned int direction, int blk_size);
  160. int (*write16_hook)(struct tmio_mmc_host *host, int addr);
  161. void (*reset)(struct tmio_mmc_host *host, bool preserve);
  162. bool (*check_retune)(struct tmio_mmc_host *host, struct mmc_request *mrq);
  163. void (*fixup_request)(struct tmio_mmc_host *host, struct mmc_request *mrq);
  164. unsigned int (*get_timeout_cycles)(struct tmio_mmc_host *host);
  165. const struct tmio_mmc_dma_ops *dma_ops;
  166. };
  167. struct tmio_mmc_host *tmio_mmc_host_alloc(struct platform_device *pdev,
  168. struct tmio_mmc_data *pdata);
  169. void tmio_mmc_host_free(struct tmio_mmc_host *host);
  170. int tmio_mmc_host_probe(struct tmio_mmc_host *host);
  171. void tmio_mmc_host_remove(struct tmio_mmc_host *host);
  172. void tmio_mmc_do_data_irq(struct tmio_mmc_host *host);
  173. void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i);
  174. void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i);
  175. irqreturn_t tmio_mmc_irq(int irq, void *devid);
  176. #ifdef CONFIG_PM
  177. int tmio_mmc_host_runtime_suspend(struct device *dev);
  178. int tmio_mmc_host_runtime_resume(struct device *dev);
  179. #endif
  180. static inline u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr)
  181. {
  182. return ioread16(host->ctl + (addr << host->bus_shift));
  183. }
  184. static inline void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr,
  185. u16 *buf, int count)
  186. {
  187. ioread16_rep(host->ctl + (addr << host->bus_shift), buf, count);
  188. }
  189. static inline u32 sd_ctrl_read16_and_16_as_32(struct tmio_mmc_host *host,
  190. int addr)
  191. {
  192. return ioread16(host->ctl + (addr << host->bus_shift)) |
  193. ioread16(host->ctl + ((addr + 2) << host->bus_shift)) << 16;
  194. }
  195. static inline void sd_ctrl_read32_rep(struct tmio_mmc_host *host, int addr,
  196. u32 *buf, int count)
  197. {
  198. ioread32_rep(host->ctl + (addr << host->bus_shift), buf, count);
  199. }
  200. static inline void sd_ctrl_write16(struct tmio_mmc_host *host, int addr,
  201. u16 val)
  202. {
  203. /* If there is a hook and it returns non-zero then there
  204. * is an error and the write should be skipped
  205. */
  206. if (host->write16_hook && host->write16_hook(host, addr))
  207. return;
  208. iowrite16(val, host->ctl + (addr << host->bus_shift));
  209. }
  210. static inline void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr,
  211. u16 *buf, int count)
  212. {
  213. iowrite16_rep(host->ctl + (addr << host->bus_shift), buf, count);
  214. }
  215. static inline void sd_ctrl_write32_as_16_and_16(struct tmio_mmc_host *host,
  216. int addr, u32 val)
  217. {
  218. if (addr == CTL_IRQ_MASK || addr == CTL_STATUS)
  219. val |= host->sdcard_irq_setbit_mask;
  220. iowrite16(val & 0xffff, host->ctl + (addr << host->bus_shift));
  221. iowrite16(val >> 16, host->ctl + ((addr + 2) << host->bus_shift));
  222. }
  223. static inline void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val)
  224. {
  225. iowrite32(val, host->ctl + (addr << host->bus_shift));
  226. }
  227. static inline void sd_ctrl_write32_rep(struct tmio_mmc_host *host, int addr,
  228. const u32 *buf, int count)
  229. {
  230. iowrite32_rep(host->ctl + (addr << host->bus_shift), buf, count);
  231. }
  232. #endif