sh_mmcif.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * include/linux/mmc/sh_mmcif.h
  3. *
  4. * platform data for eMMC driver
  5. *
  6. * Copyright (C) 2010 Renesas Solutions Corp.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License.
  11. *
  12. */
  13. #ifndef __SH_MMCIF_H__
  14. #define __SH_MMCIF_H__
  15. #include <linux/platform_device.h>
  16. #include <linux/io.h>
  17. /*
  18. * MMCIF : CE_CLK_CTRL [19:16]
  19. * 1000 : Peripheral clock / 512
  20. * 0111 : Peripheral clock / 256
  21. * 0110 : Peripheral clock / 128
  22. * 0101 : Peripheral clock / 64
  23. * 0100 : Peripheral clock / 32
  24. * 0011 : Peripheral clock / 16
  25. * 0010 : Peripheral clock / 8
  26. * 0001 : Peripheral clock / 4
  27. * 0000 : Peripheral clock / 2
  28. * 1111 : Peripheral clock (sup_pclk set '1')
  29. */
  30. struct sh_mmcif_plat_data {
  31. void (*set_pwr)(struct platform_device *pdev, int state);
  32. void (*down_pwr)(struct platform_device *pdev);
  33. int (*get_cd)(struct platform_device *pdef);
  34. u8 sup_pclk; /* 1 :SH7757, 0: SH7724/SH7372 */
  35. unsigned long caps;
  36. u32 ocr;
  37. };
  38. #define MMCIF_CE_CMD_SET 0x00000000
  39. #define MMCIF_CE_ARG 0x00000008
  40. #define MMCIF_CE_ARG_CMD12 0x0000000C
  41. #define MMCIF_CE_CMD_CTRL 0x00000010
  42. #define MMCIF_CE_BLOCK_SET 0x00000014
  43. #define MMCIF_CE_CLK_CTRL 0x00000018
  44. #define MMCIF_CE_BUF_ACC 0x0000001C
  45. #define MMCIF_CE_RESP3 0x00000020
  46. #define MMCIF_CE_RESP2 0x00000024
  47. #define MMCIF_CE_RESP1 0x00000028
  48. #define MMCIF_CE_RESP0 0x0000002C
  49. #define MMCIF_CE_RESP_CMD12 0x00000030
  50. #define MMCIF_CE_DATA 0x00000034
  51. #define MMCIF_CE_INT 0x00000040
  52. #define MMCIF_CE_INT_MASK 0x00000044
  53. #define MMCIF_CE_HOST_STS1 0x00000048
  54. #define MMCIF_CE_HOST_STS2 0x0000004C
  55. #define MMCIF_CE_VERSION 0x0000007C
  56. /* CE_BUF_ACC */
  57. #define BUF_ACC_DMAWEN (1 << 25)
  58. #define BUF_ACC_DMAREN (1 << 24)
  59. #define BUF_ACC_BUSW_32 (0 << 17)
  60. #define BUF_ACC_BUSW_16 (1 << 17)
  61. #define BUF_ACC_ATYP (1 << 16)
  62. /* CE_CLK_CTRL */
  63. #define CLK_ENABLE (1 << 24) /* 1: output mmc clock */
  64. #define CLK_CLEAR ((1 << 19) | (1 << 18) | (1 << 17) | (1 << 16))
  65. #define CLK_SUP_PCLK ((1 << 19) | (1 << 18) | (1 << 17) | (1 << 16))
  66. #define SRSPTO_256 ((1 << 13) | (0 << 12)) /* resp timeout */
  67. #define SRBSYTO_29 ((1 << 11) | (1 << 10) | \
  68. (1 << 9) | (1 << 8)) /* resp busy timeout */
  69. #define SRWDTO_29 ((1 << 7) | (1 << 6) | \
  70. (1 << 5) | (1 << 4)) /* read/write timeout */
  71. #define SCCSTO_29 ((1 << 3) | (1 << 2) | \
  72. (1 << 1) | (1 << 0)) /* ccs timeout */
  73. /* CE_VERSION */
  74. #define SOFT_RST_ON (1 << 31)
  75. #define SOFT_RST_OFF ~SOFT_RST_ON
  76. static inline u32 sh_mmcif_readl(void __iomem *addr, int reg)
  77. {
  78. return readl(addr + reg);
  79. }
  80. static inline void sh_mmcif_writel(void __iomem *addr, int reg, u32 val)
  81. {
  82. writel(val, addr + reg);
  83. }
  84. #define SH_MMCIF_BBS 512 /* boot block size */
  85. static inline void sh_mmcif_boot_cmd_send(void __iomem *base,
  86. unsigned long cmd, unsigned long arg)
  87. {
  88. sh_mmcif_writel(base, MMCIF_CE_INT, 0);
  89. sh_mmcif_writel(base, MMCIF_CE_ARG, arg);
  90. sh_mmcif_writel(base, MMCIF_CE_CMD_SET, cmd);
  91. }
  92. static inline int sh_mmcif_boot_cmd_poll(void __iomem *base, unsigned long mask)
  93. {
  94. unsigned long tmp;
  95. int cnt;
  96. for (cnt = 0; cnt < 1000000; cnt++) {
  97. tmp = sh_mmcif_readl(base, MMCIF_CE_INT);
  98. if (tmp & mask) {
  99. sh_mmcif_writel(base, MMCIF_CE_INT, tmp & ~mask);
  100. return 0;
  101. }
  102. }
  103. return -1;
  104. }
  105. static inline int sh_mmcif_boot_cmd(void __iomem *base,
  106. unsigned long cmd, unsigned long arg)
  107. {
  108. sh_mmcif_boot_cmd_send(base, cmd, arg);
  109. return sh_mmcif_boot_cmd_poll(base, 0x00010000);
  110. }
  111. static inline int sh_mmcif_boot_do_read_single(void __iomem *base,
  112. unsigned int block_nr,
  113. unsigned long *buf)
  114. {
  115. int k;
  116. /* CMD13 - Status */
  117. sh_mmcif_boot_cmd(base, 0x0d400000, 0x00010000);
  118. if (sh_mmcif_readl(base, MMCIF_CE_RESP0) != 0x0900)
  119. return -1;
  120. /* CMD17 - Read */
  121. sh_mmcif_boot_cmd(base, 0x11480000, block_nr * SH_MMCIF_BBS);
  122. if (sh_mmcif_boot_cmd_poll(base, 0x00100000) < 0)
  123. return -1;
  124. for (k = 0; k < (SH_MMCIF_BBS / 4); k++)
  125. buf[k] = sh_mmcif_readl(base, MMCIF_CE_DATA);
  126. return 0;
  127. }
  128. static inline int sh_mmcif_boot_do_read(void __iomem *base,
  129. unsigned long first_block,
  130. unsigned long nr_blocks,
  131. void *buf)
  132. {
  133. unsigned long k;
  134. int ret = 0;
  135. /* CMD16 - Set the block size */
  136. sh_mmcif_boot_cmd(base, 0x10400000, SH_MMCIF_BBS);
  137. for (k = 0; !ret && k < nr_blocks; k++)
  138. ret = sh_mmcif_boot_do_read_single(base, first_block + k,
  139. buf + (k * SH_MMCIF_BBS));
  140. return ret;
  141. }
  142. static inline void sh_mmcif_boot_init(void __iomem *base)
  143. {
  144. unsigned long tmp;
  145. /* reset */
  146. tmp = sh_mmcif_readl(base, MMCIF_CE_VERSION);
  147. sh_mmcif_writel(base, MMCIF_CE_VERSION, tmp | SOFT_RST_ON);
  148. sh_mmcif_writel(base, MMCIF_CE_VERSION, tmp & SOFT_RST_OFF);
  149. /* byte swap */
  150. sh_mmcif_writel(base, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
  151. /* Set block size in MMCIF hardware */
  152. sh_mmcif_writel(base, MMCIF_CE_BLOCK_SET, SH_MMCIF_BBS);
  153. /* Enable the clock, set it to Bus clock/256 (about 325Khz).
  154. * It is unclear where 0x70000 comes from or if it is even needed.
  155. * It is there for byte-compatibility with code that is known to
  156. * work.
  157. */
  158. sh_mmcif_writel(base, MMCIF_CE_CLK_CTRL,
  159. CLK_ENABLE | SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 |
  160. SCCSTO_29 | 0x70000);
  161. /* CMD0 */
  162. sh_mmcif_boot_cmd(base, 0x00000040, 0);
  163. /* CMD1 - Get OCR */
  164. do {
  165. sh_mmcif_boot_cmd(base, 0x01405040, 0x40300000); /* CMD1 */
  166. } while ((sh_mmcif_readl(base, MMCIF_CE_RESP0) & 0x80000000)
  167. != 0x80000000);
  168. /* CMD2 - Get CID */
  169. sh_mmcif_boot_cmd(base, 0x02806040, 0);
  170. /* CMD3 - Set card relative address */
  171. sh_mmcif_boot_cmd(base, 0x03400040, 0x00010000);
  172. }
  173. static inline void sh_mmcif_boot_slurp(void __iomem *base,
  174. unsigned char *buf,
  175. unsigned long no_bytes)
  176. {
  177. unsigned long tmp;
  178. /* In data transfer mode: Set clock to Bus clock/4 (about 20Mhz) */
  179. sh_mmcif_writel(base, MMCIF_CE_CLK_CTRL, 0x01012fff);
  180. /* CMD9 - Get CSD */
  181. sh_mmcif_boot_cmd(base, 0x09806000, 0x00010000);
  182. /* CMD7 - Select the card */
  183. sh_mmcif_boot_cmd(base, 0x07400000, 0x00010000);
  184. tmp = no_bytes / SH_MMCIF_BBS;
  185. tmp += (no_bytes % SH_MMCIF_BBS) ? 1 : 0;
  186. sh_mmcif_boot_do_read(base, 512, tmp, buf);
  187. }
  188. #endif /* __SH_MMCIF_H__ */