meson_gx_mmc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2016 Carlo Caione <carlo@caione.org>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <fdtdec.h>
  8. #include <malloc.h>
  9. #include <mmc.h>
  10. #include <asm/io.h>
  11. #include <asm/arch/sd_emmc.h>
  12. #include <linux/log2.h>
  13. static inline void *get_regbase(const struct mmc *mmc)
  14. {
  15. struct meson_mmc_platdata *pdata = mmc->priv;
  16. return pdata->regbase;
  17. }
  18. static inline uint32_t meson_read(struct mmc *mmc, int offset)
  19. {
  20. return readl(get_regbase(mmc) + offset);
  21. }
  22. static inline void meson_write(struct mmc *mmc, uint32_t val, int offset)
  23. {
  24. writel(val, get_regbase(mmc) + offset);
  25. }
  26. static void meson_mmc_config_clock(struct mmc *mmc)
  27. {
  28. uint32_t meson_mmc_clk = 0;
  29. unsigned int clk, clk_src, clk_div;
  30. if (!mmc->clock)
  31. return;
  32. /* 1GHz / CLK_MAX_DIV = 15,9 MHz */
  33. if (mmc->clock > 16000000) {
  34. clk = SD_EMMC_CLKSRC_DIV2;
  35. clk_src = CLK_SRC_DIV2;
  36. } else {
  37. clk = SD_EMMC_CLKSRC_24M;
  38. clk_src = CLK_SRC_24M;
  39. }
  40. clk_div = DIV_ROUND_UP(clk, mmc->clock);
  41. /* 180 phase core clock */
  42. meson_mmc_clk |= CLK_CO_PHASE_180;
  43. /* 180 phase tx clock */
  44. meson_mmc_clk |= CLK_TX_PHASE_000;
  45. /* clock settings */
  46. meson_mmc_clk |= clk_src;
  47. meson_mmc_clk |= clk_div;
  48. meson_write(mmc, meson_mmc_clk, MESON_SD_EMMC_CLOCK);
  49. }
  50. static int meson_dm_mmc_set_ios(struct udevice *dev)
  51. {
  52. struct mmc *mmc = mmc_get_mmc_dev(dev);
  53. uint32_t meson_mmc_cfg;
  54. meson_mmc_config_clock(mmc);
  55. meson_mmc_cfg = meson_read(mmc, MESON_SD_EMMC_CFG);
  56. meson_mmc_cfg &= ~CFG_BUS_WIDTH_MASK;
  57. if (mmc->bus_width == 1)
  58. meson_mmc_cfg |= CFG_BUS_WIDTH_1;
  59. else if (mmc->bus_width == 4)
  60. meson_mmc_cfg |= CFG_BUS_WIDTH_4;
  61. else if (mmc->bus_width == 8)
  62. meson_mmc_cfg |= CFG_BUS_WIDTH_8;
  63. else
  64. return -EINVAL;
  65. /* 512 bytes block length */
  66. meson_mmc_cfg &= ~CFG_BL_LEN_MASK;
  67. meson_mmc_cfg |= CFG_BL_LEN_512;
  68. /* Response timeout 256 clk */
  69. meson_mmc_cfg &= ~CFG_RESP_TIMEOUT_MASK;
  70. meson_mmc_cfg |= CFG_RESP_TIMEOUT_256;
  71. /* Command-command gap 16 clk */
  72. meson_mmc_cfg &= ~CFG_RC_CC_MASK;
  73. meson_mmc_cfg |= CFG_RC_CC_16;
  74. meson_write(mmc, meson_mmc_cfg, MESON_SD_EMMC_CFG);
  75. return 0;
  76. }
  77. static void meson_mmc_setup_cmd(struct mmc *mmc, struct mmc_data *data,
  78. struct mmc_cmd *cmd)
  79. {
  80. uint32_t meson_mmc_cmd = 0, cfg;
  81. meson_mmc_cmd |= cmd->cmdidx << CMD_CFG_CMD_INDEX_SHIFT;
  82. if (cmd->resp_type & MMC_RSP_PRESENT) {
  83. if (cmd->resp_type & MMC_RSP_136)
  84. meson_mmc_cmd |= CMD_CFG_RESP_128;
  85. if (cmd->resp_type & MMC_RSP_BUSY)
  86. meson_mmc_cmd |= CMD_CFG_R1B;
  87. if (!(cmd->resp_type & MMC_RSP_CRC))
  88. meson_mmc_cmd |= CMD_CFG_RESP_NOCRC;
  89. } else {
  90. meson_mmc_cmd |= CMD_CFG_NO_RESP;
  91. }
  92. if (data) {
  93. cfg = meson_read(mmc, MESON_SD_EMMC_CFG);
  94. cfg &= ~CFG_BL_LEN_MASK;
  95. cfg |= ilog2(data->blocksize) << CFG_BL_LEN_SHIFT;
  96. meson_write(mmc, cfg, MESON_SD_EMMC_CFG);
  97. if (data->flags == MMC_DATA_WRITE)
  98. meson_mmc_cmd |= CMD_CFG_DATA_WR;
  99. meson_mmc_cmd |= CMD_CFG_DATA_IO | CMD_CFG_BLOCK_MODE |
  100. data->blocks;
  101. }
  102. meson_mmc_cmd |= CMD_CFG_TIMEOUT_4S | CMD_CFG_OWNER |
  103. CMD_CFG_END_OF_CHAIN;
  104. meson_write(mmc, meson_mmc_cmd, MESON_SD_EMMC_CMD_CFG);
  105. }
  106. static void meson_mmc_setup_addr(struct mmc *mmc, struct mmc_data *data)
  107. {
  108. struct meson_mmc_platdata *pdata = mmc->priv;
  109. unsigned int data_size;
  110. uint32_t data_addr = 0;
  111. if (data) {
  112. data_size = data->blocks * data->blocksize;
  113. if (data->flags == MMC_DATA_READ) {
  114. data_addr = (ulong) data->dest;
  115. invalidate_dcache_range(data_addr,
  116. data_addr + data_size);
  117. } else {
  118. pdata->w_buf = calloc(data_size, sizeof(char));
  119. data_addr = (ulong) pdata->w_buf;
  120. memcpy(pdata->w_buf, data->src, data_size);
  121. flush_dcache_range(data_addr, data_addr + data_size);
  122. }
  123. }
  124. meson_write(mmc, data_addr, MESON_SD_EMMC_CMD_DAT);
  125. }
  126. static void meson_mmc_read_response(struct mmc *mmc, struct mmc_cmd *cmd)
  127. {
  128. if (cmd->resp_type & MMC_RSP_136) {
  129. cmd->response[0] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP3);
  130. cmd->response[1] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP2);
  131. cmd->response[2] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP1);
  132. cmd->response[3] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP);
  133. } else {
  134. cmd->response[0] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP);
  135. }
  136. }
  137. static int meson_dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  138. struct mmc_data *data)
  139. {
  140. struct mmc *mmc = mmc_get_mmc_dev(dev);
  141. struct meson_mmc_platdata *pdata = mmc->priv;
  142. uint32_t status;
  143. ulong start;
  144. int ret = 0;
  145. /* max block size supported by chip is 512 byte */
  146. if (data && data->blocksize > 512)
  147. return -EINVAL;
  148. meson_mmc_setup_cmd(mmc, data, cmd);
  149. meson_mmc_setup_addr(mmc, data);
  150. meson_write(mmc, cmd->cmdarg, MESON_SD_EMMC_CMD_ARG);
  151. /* use 10s timeout */
  152. start = get_timer(0);
  153. do {
  154. status = meson_read(mmc, MESON_SD_EMMC_STATUS);
  155. } while(!(status & STATUS_END_OF_CHAIN) && get_timer(start) < 10000);
  156. if (!(status & STATUS_END_OF_CHAIN))
  157. ret = -ETIMEDOUT;
  158. else if (status & STATUS_RESP_TIMEOUT)
  159. ret = -ETIMEDOUT;
  160. else if (status & STATUS_ERR_MASK)
  161. ret = -EIO;
  162. meson_mmc_read_response(mmc, cmd);
  163. if (data && data->flags == MMC_DATA_WRITE)
  164. free(pdata->w_buf);
  165. /* reset status bits */
  166. meson_write(mmc, STATUS_MASK, MESON_SD_EMMC_STATUS);
  167. return ret;
  168. }
  169. static const struct dm_mmc_ops meson_dm_mmc_ops = {
  170. .send_cmd = meson_dm_mmc_send_cmd,
  171. .set_ios = meson_dm_mmc_set_ios,
  172. };
  173. static int meson_mmc_ofdata_to_platdata(struct udevice *dev)
  174. {
  175. struct meson_mmc_platdata *pdata = dev_get_platdata(dev);
  176. fdt_addr_t addr;
  177. addr = devfdt_get_addr(dev);
  178. if (addr == FDT_ADDR_T_NONE)
  179. return -EINVAL;
  180. pdata->regbase = (void *)addr;
  181. return 0;
  182. }
  183. static int meson_mmc_probe(struct udevice *dev)
  184. {
  185. struct meson_mmc_platdata *pdata = dev_get_platdata(dev);
  186. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  187. struct mmc *mmc = &pdata->mmc;
  188. struct mmc_config *cfg = &pdata->cfg;
  189. uint32_t val;
  190. cfg->voltages = MMC_VDD_33_34 | MMC_VDD_32_33 |
  191. MMC_VDD_31_32 | MMC_VDD_165_195;
  192. cfg->host_caps = MMC_MODE_8BIT | MMC_MODE_4BIT |
  193. MMC_MODE_HS_52MHz | MMC_MODE_HS;
  194. cfg->f_min = DIV_ROUND_UP(SD_EMMC_CLKSRC_24M, CLK_MAX_DIV);
  195. cfg->f_max = 100000000; /* 100 MHz */
  196. cfg->b_max = 511; /* max 512 - 1 blocks */
  197. cfg->name = dev->name;
  198. mmc->priv = pdata;
  199. upriv->mmc = mmc;
  200. mmc_set_clock(mmc, cfg->f_min, MMC_CLK_ENABLE);
  201. /* reset all status bits */
  202. meson_write(mmc, STATUS_MASK, MESON_SD_EMMC_STATUS);
  203. /* disable interrupts */
  204. meson_write(mmc, 0, MESON_SD_EMMC_IRQ_EN);
  205. /* enable auto clock mode */
  206. val = meson_read(mmc, MESON_SD_EMMC_CFG);
  207. val &= ~CFG_SDCLK_ALWAYS_ON;
  208. val |= CFG_AUTO_CLK;
  209. meson_write(mmc, val, MESON_SD_EMMC_CFG);
  210. return 0;
  211. }
  212. int meson_mmc_bind(struct udevice *dev)
  213. {
  214. struct meson_mmc_platdata *pdata = dev_get_platdata(dev);
  215. return mmc_bind(dev, &pdata->mmc, &pdata->cfg);
  216. }
  217. static const struct udevice_id meson_mmc_match[] = {
  218. { .compatible = "amlogic,meson-gx-mmc" },
  219. { /* sentinel */ }
  220. };
  221. U_BOOT_DRIVER(meson_mmc) = {
  222. .name = "meson_gx_mmc",
  223. .id = UCLASS_MMC,
  224. .of_match = meson_mmc_match,
  225. .ops = &meson_dm_mmc_ops,
  226. .probe = meson_mmc_probe,
  227. .bind = meson_mmc_bind,
  228. .ofdata_to_platdata = meson_mmc_ofdata_to_platdata,
  229. .platdata_auto_alloc_size = sizeof(struct meson_mmc_platdata),
  230. };