owl_mmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020 Amit Singh Tomar <amittomer25@gmail.com>
  4. *
  5. * Driver for SD/MMC controller present on Actions Semi S700/S900 SoC, based
  6. * on Linux Driver "drivers/mmc/host/owl-mmc.c".
  7. *
  8. * Though, there is a bit (BSEL, BUS or DMA Special Channel Selection) that
  9. * controls the data transfer from SDx_DAT register either using CPU AHB Bus
  10. * or DMA channel, but seems like, it only works correctly using external DMA
  11. * channel, and those special bits used in this driver is picked from vendor
  12. * source exclusively for MMC/SD.
  13. */
  14. #include <common.h>
  15. #include <clk.h>
  16. #include <cpu_func.h>
  17. #include <dm.h>
  18. #include <errno.h>
  19. #include <log.h>
  20. #include <mmc.h>
  21. #include <asm/io.h>
  22. #include <linux/bitops.h>
  23. #include <linux/delay.h>
  24. #include <linux/err.h>
  25. #include <linux/iopoll.h>
  26. /*
  27. * SDC registers
  28. */
  29. #define OWL_REG_SD_EN 0x0000
  30. #define OWL_REG_SD_CTL 0x0004
  31. #define OWL_REG_SD_STATE 0x0008
  32. #define OWL_REG_SD_CMD 0x000c
  33. #define OWL_REG_SD_ARG 0x0010
  34. #define OWL_REG_SD_RSPBUF0 0x0014
  35. #define OWL_REG_SD_RSPBUF1 0x0018
  36. #define OWL_REG_SD_RSPBUF2 0x001c
  37. #define OWL_REG_SD_RSPBUF3 0x0020
  38. #define OWL_REG_SD_RSPBUF4 0x0024
  39. #define OWL_REG_SD_DAT 0x0028
  40. #define OWL_REG_SD_BLK_SIZE 0x002c
  41. #define OWL_REG_SD_BLK_NUM 0x0030
  42. #define OWL_REG_SD_BUF_SIZE 0x0034
  43. /* SD_EN Bits */
  44. #define OWL_SD_EN_RANE BIT(31)
  45. #define OWL_SD_EN_RESE BIT(10)
  46. #define OWL_SD_ENABLE BIT(7)
  47. #define OWL_SD_EN_BSEL BIT(6)
  48. #define OWL_SD_EN_DATAWID(x) (((x) & 0x3) << 0)
  49. #define OWL_SD_EN_DATAWID_MASK 0x03
  50. /* SD_CTL Bits */
  51. #define OWL_SD_CTL_TOUTEN BIT(31)
  52. #define OWL_SD_CTL_DELAY_MSK GENMASK(23, 16)
  53. #define OWL_SD_CTL_RDELAY(x) (((x) & 0xf) << 20)
  54. #define OWL_SD_CTL_WDELAY(x) (((x) & 0xf) << 16)
  55. #define OWL_SD_CTL_TS BIT(7)
  56. #define OWL_SD_CTL_LBE BIT(6)
  57. #define OWL_SD_CTL_TM(x) (((x) & 0xf) << 0)
  58. #define OWL_SD_DELAY_LOW_CLK 0x0f
  59. #define OWL_SD_DELAY_MID_CLK 0x0a
  60. #define OWL_SD_RDELAY_HIGH 0x08
  61. #define OWL_SD_WDELAY_HIGH 0x09
  62. /* SD_STATE Bits */
  63. #define OWL_SD_STATE_DAT0S BIT(7)
  64. #define OWL_SD_STATE_CLNR BIT(4)
  65. #define OWL_SD_STATE_CRC7ER BIT(0)
  66. #define OWL_MMC_OCR (MMC_VDD_32_33 | MMC_VDD_33_34 | \
  67. MMC_VDD_165_195)
  68. #define DATA_TRANSFER_TIMEOUT 3000000
  69. #define DMA_TRANSFER_TIMEOUT 5000000
  70. /*
  71. * Simple DMA transfer operations defines for MMC/SD card
  72. */
  73. #define SD_DMA_CHANNEL(base, channel) ((base) + 0x100 + 0x100 * (channel))
  74. #define DMA_MODE 0x0000
  75. #define DMA_SOURCE 0x0004
  76. #define DMA_DESTINATION 0x0008
  77. #define DMA_FRAME_LEN 0x000C
  78. #define DMA_FRAME_CNT 0x0010
  79. #define DMA_START 0x0024
  80. /* DMAx_MODE */
  81. #define DMA_MODE_ST(x) (((x) & 0x3) << 8)
  82. #define DMA_MODE_ST_DEV DMA_MODE_ST(0)
  83. #define DMA_MODE_DT(x) (((x) & 0x3) << 10)
  84. #define DMA_MODE_DT_DCU DMA_MODE_DT(2)
  85. #define DMA_MODE_SAM(x) (((x) & 0x3) << 16)
  86. #define DMA_MODE_SAM_CONST DMA_MODE_SAM(0)
  87. #define DMA_MODE_DAM(x) (((x) & 0x3) << 18)
  88. #define DMA_MODE_DAM_INC DMA_MODE_DAM(1)
  89. #define DMA_ENABLE 0x1
  90. struct owl_mmc_plat {
  91. struct mmc_config cfg;
  92. struct mmc mmc;
  93. };
  94. struct owl_mmc_priv {
  95. void *reg_base;
  96. void *dma_channel;
  97. struct clk clk;
  98. unsigned int clock; /* Current clock */
  99. unsigned int dma_drq; /* Trigger Source */
  100. };
  101. static void owl_dma_config(struct owl_mmc_priv *priv, unsigned int src,
  102. unsigned int dst, unsigned int len)
  103. {
  104. unsigned int mode = priv->dma_drq;
  105. /* Set Source and Destination adderess mode */
  106. mode |= (DMA_MODE_ST_DEV | DMA_MODE_SAM_CONST | DMA_MODE_DT_DCU |
  107. DMA_MODE_DAM_INC);
  108. writel(mode, SD_DMA_CHANNEL(priv->dma_channel, 0) + DMA_MODE);
  109. writel(src, SD_DMA_CHANNEL(priv->dma_channel, 0) + DMA_SOURCE);
  110. writel(dst, SD_DMA_CHANNEL(priv->dma_channel, 0) + DMA_DESTINATION);
  111. writel(len, SD_DMA_CHANNEL(priv->dma_channel, 0) + DMA_FRAME_LEN);
  112. writel(0x1, SD_DMA_CHANNEL(priv->dma_channel, 0) + DMA_FRAME_CNT);
  113. }
  114. static void owl_mmc_prepare_data(struct owl_mmc_priv *priv,
  115. struct mmc_data *data)
  116. {
  117. unsigned int total;
  118. u32 buf = 0;
  119. setbits_le32(priv->reg_base + OWL_REG_SD_EN, OWL_SD_EN_BSEL);
  120. writel(data->blocks, priv->reg_base + OWL_REG_SD_BLK_NUM);
  121. writel(data->blocksize, priv->reg_base + OWL_REG_SD_BLK_SIZE);
  122. total = data->blocksize * data->blocks;
  123. if (total < 512)
  124. writel(total, priv->reg_base + OWL_REG_SD_BUF_SIZE);
  125. else
  126. writel(512, priv->reg_base + OWL_REG_SD_BUF_SIZE);
  127. /* DMA STOP */
  128. writel(0x0, SD_DMA_CHANNEL(priv->dma_channel, 0) + DMA_START);
  129. if (data) {
  130. if (data->flags == MMC_DATA_READ) {
  131. buf = (ulong) (data->dest);
  132. owl_dma_config(priv, (ulong) priv->reg_base +
  133. OWL_REG_SD_DAT, buf, total);
  134. invalidate_dcache_range(buf, buf + total);
  135. } else {
  136. buf = (ulong) (data->src);
  137. owl_dma_config(priv, buf, (ulong) priv->reg_base +
  138. OWL_REG_SD_DAT, total);
  139. flush_dcache_range(buf, buf + total);
  140. }
  141. /* DMA START */
  142. writel(0x1, SD_DMA_CHANNEL(priv->dma_channel, 0) + DMA_START);
  143. }
  144. }
  145. static int owl_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  146. struct mmc_data *data)
  147. {
  148. struct owl_mmc_priv *priv = dev_get_priv(dev);
  149. unsigned int cmd_rsp_mask, mode, reg;
  150. int ret;
  151. setbits_le32(priv->reg_base + OWL_REG_SD_EN, OWL_SD_ENABLE);
  152. /* setup response */
  153. mode = 0;
  154. if (cmd->resp_type != MMC_RSP_NONE)
  155. cmd_rsp_mask = OWL_SD_STATE_CLNR | OWL_SD_STATE_CRC7ER;
  156. if (cmd->resp_type == MMC_RSP_R1) {
  157. if (data) {
  158. if (data->flags == MMC_DATA_READ)
  159. mode |= OWL_SD_CTL_TM(4);
  160. else
  161. mode |= OWL_SD_CTL_TM(5);
  162. } else
  163. mode |= OWL_SD_CTL_TM(1);
  164. } else if (cmd->resp_type == MMC_RSP_R2) {
  165. mode = OWL_SD_CTL_TM(2);
  166. } else if (cmd->resp_type == MMC_RSP_R1b) {
  167. mode = OWL_SD_CTL_TM(3);
  168. } else if (cmd->resp_type == MMC_RSP_R3) {
  169. cmd_rsp_mask = OWL_SD_STATE_CLNR;
  170. mode = OWL_SD_CTL_TM(1);
  171. }
  172. mode |= (readl(priv->reg_base + OWL_REG_SD_CTL) & (0xff << 16));
  173. /* setup command */
  174. writel(cmd->cmdidx, priv->reg_base + OWL_REG_SD_CMD);
  175. writel(cmd->cmdarg, priv->reg_base + OWL_REG_SD_ARG);
  176. /* Set LBE to send clk at the end of last read block */
  177. if (data)
  178. mode |= (OWL_SD_CTL_TS | OWL_SD_CTL_LBE | 0xE4000000);
  179. else
  180. mode |= OWL_SD_CTL_TS;
  181. if (data)
  182. owl_mmc_prepare_data(priv, data);
  183. /* Start transfer */
  184. writel(mode, priv->reg_base + OWL_REG_SD_CTL);
  185. ret = readl_poll_timeout(priv->reg_base + OWL_REG_SD_CTL, reg,
  186. !(reg & OWL_SD_CTL_TS), DATA_TRANSFER_TIMEOUT);
  187. if (ret == -ETIMEDOUT) {
  188. debug("error: transferred data timeout\n");
  189. return ret;
  190. }
  191. reg = readl(priv->reg_base + OWL_REG_SD_STATE) & cmd_rsp_mask;
  192. if (cmd->resp_type & MMC_RSP_PRESENT) {
  193. if (reg & OWL_SD_STATE_CLNR) {
  194. printf("Error CMD_NO_RSP\n");
  195. return -1;
  196. }
  197. if (reg & OWL_SD_STATE_CRC7ER) {
  198. printf("Error CMD_RSP_CRC\n");
  199. return -1;
  200. }
  201. if (cmd->resp_type & MMC_RSP_136) {
  202. cmd->response[3] = readl(priv->reg_base + OWL_REG_SD_RSPBUF0);
  203. cmd->response[2] = readl(priv->reg_base + OWL_REG_SD_RSPBUF1);
  204. cmd->response[1] = readl(priv->reg_base + OWL_REG_SD_RSPBUF2);
  205. cmd->response[0] = readl(priv->reg_base + OWL_REG_SD_RSPBUF3);
  206. } else {
  207. u32 rsp[2];
  208. rsp[0] = readl(priv->reg_base + OWL_REG_SD_RSPBUF0);
  209. rsp[1] = readl(priv->reg_base + OWL_REG_SD_RSPBUF1);
  210. cmd->response[0] = rsp[1] << 24 | rsp[0] >> 8;
  211. cmd->response[1] = rsp[1] >> 8;
  212. }
  213. }
  214. if (data) {
  215. ret = readl_poll_timeout(SD_DMA_CHANNEL(priv->dma_channel, 0) + DMA_START,
  216. reg, !(reg & DMA_ENABLE), DMA_TRANSFER_TIMEOUT);
  217. if (ret == -ETIMEDOUT) {
  218. debug("error: DMA transfer timeout\n");
  219. return ret;
  220. }
  221. /* DMA STOP */
  222. writel(0x0, SD_DMA_CHANNEL(priv->dma_channel, 0) + DMA_START);
  223. /* Transmission STOP */
  224. while (readl(priv->reg_base + OWL_REG_SD_CTL) & OWL_SD_CTL_TS)
  225. clrbits_le32(priv->reg_base + OWL_REG_SD_CTL,
  226. OWL_SD_CTL_TS);
  227. }
  228. return 0;
  229. }
  230. static int owl_mmc_clk_set(struct owl_mmc_priv *priv, int rate)
  231. {
  232. u32 reg, wdelay, rdelay;
  233. reg = readl(priv->reg_base + OWL_REG_SD_CTL);
  234. reg &= ~OWL_SD_CTL_DELAY_MSK;
  235. /* Set RDELAY and WDELAY based on the clock */
  236. if (rate <= 1000000)
  237. rdelay = wdelay = OWL_SD_DELAY_LOW_CLK;
  238. else if ((rate > 1000000) && (rate <= 26000000))
  239. rdelay = wdelay = OWL_SD_DELAY_MID_CLK;
  240. else if ((rate > 26000000) && (rate <= 52000000)) {
  241. rdelay = OWL_SD_RDELAY_HIGH;
  242. wdelay = OWL_SD_WDELAY_HIGH;
  243. } else {
  244. debug("SD clock rate not supported\n");
  245. return -EINVAL;
  246. }
  247. writel(reg | OWL_SD_CTL_RDELAY(rdelay) | OWL_SD_CTL_WDELAY(wdelay),
  248. priv->reg_base + OWL_REG_SD_CTL);
  249. return 0;
  250. }
  251. static int owl_mmc_set_ios(struct udevice *dev)
  252. {
  253. struct owl_mmc_priv *priv = dev_get_priv(dev);
  254. struct owl_mmc_plat *plat = dev_get_plat(dev);
  255. struct mmc *mmc = &plat->mmc;
  256. u32 reg, ret;
  257. if (mmc->clock != priv->clock) {
  258. priv->clock = mmc->clock;
  259. ret = owl_mmc_clk_set(priv, mmc->clock);
  260. if (IS_ERR_VALUE(ret))
  261. return ret;
  262. ret = clk_set_rate(&priv->clk, mmc->clock);
  263. if (IS_ERR_VALUE(ret))
  264. return ret;
  265. }
  266. if (mmc->clk_disable)
  267. ret = clk_disable(&priv->clk);
  268. else
  269. ret = clk_enable(&priv->clk);
  270. if (ret)
  271. return ret;
  272. /* Set the Bus width */
  273. reg = readl(priv->reg_base + OWL_REG_SD_EN);
  274. reg &= ~OWL_SD_EN_DATAWID_MASK;
  275. if (mmc->bus_width == 8)
  276. reg |= OWL_SD_EN_DATAWID(2);
  277. else if (mmc->bus_width == 4)
  278. reg |= OWL_SD_EN_DATAWID(1);
  279. writel(reg, priv->reg_base + OWL_REG_SD_EN);
  280. return 0;
  281. }
  282. static const struct dm_mmc_ops owl_mmc_ops = {
  283. .send_cmd = owl_mmc_send_cmd,
  284. .set_ios = owl_mmc_set_ios,
  285. };
  286. static int owl_mmc_probe(struct udevice *dev)
  287. {
  288. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  289. struct owl_mmc_plat *plat = dev_get_plat(dev);
  290. struct owl_mmc_priv *priv = dev_get_priv(dev);
  291. struct mmc_config *cfg = &plat->cfg;
  292. struct ofnode_phandle_args args;
  293. int ret;
  294. fdt_addr_t addr;
  295. cfg->name = dev->name;
  296. cfg->voltages = OWL_MMC_OCR;
  297. cfg->f_min = 400000;
  298. cfg->f_max = 52000000;
  299. cfg->b_max = 512;
  300. cfg->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz;
  301. ret = mmc_of_parse(dev, cfg);
  302. if (ret)
  303. return ret;
  304. addr = dev_read_addr(dev);
  305. if (addr == FDT_ADDR_T_NONE)
  306. return -EINVAL;
  307. priv->reg_base = (void *)addr;
  308. ret = dev_read_phandle_with_args(dev, "dmas", "#dma-cells", 0, 0,
  309. &args);
  310. if (ret)
  311. return ret;
  312. priv->dma_channel = (void *)ofnode_get_addr(args.node);
  313. priv->dma_drq = args.args[0];
  314. ret = clk_get_by_index(dev, 0, &priv->clk);
  315. if (ret) {
  316. debug("clk_get_by_index() failed: %d\n", ret);
  317. return ret;
  318. }
  319. upriv->mmc = &plat->mmc;
  320. return 0;
  321. }
  322. static int owl_mmc_bind(struct udevice *dev)
  323. {
  324. struct owl_mmc_plat *plat = dev_get_plat(dev);
  325. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  326. }
  327. static const struct udevice_id owl_mmc_ids[] = {
  328. { .compatible = "actions,s700-mmc" },
  329. { .compatible = "actions,owl-mmc" },
  330. { }
  331. };
  332. U_BOOT_DRIVER(owl_mmc_drv) = {
  333. .name = "owl_mmc",
  334. .id = UCLASS_MMC,
  335. .of_match = owl_mmc_ids,
  336. .bind = owl_mmc_bind,
  337. .probe = owl_mmc_probe,
  338. .ops = &owl_mmc_ops,
  339. .plat_auto = sizeof(struct owl_mmc_plat),
  340. .priv_auto = sizeof(struct owl_mmc_priv),
  341. };