uniphier-sd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * Copyright (C) 2016 Socionext Inc.
  3. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <clk.h>
  9. #include <fdtdec.h>
  10. #include <mmc.h>
  11. #include <dm.h>
  12. #include <linux/compat.h>
  13. #include <linux/dma-direction.h>
  14. #include <linux/io.h>
  15. #include <linux/sizes.h>
  16. #include <power/regulator.h>
  17. #include <asm/unaligned.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. #define UNIPHIER_SD_CMD 0x000 /* command */
  20. #define UNIPHIER_SD_CMD_NOSTOP BIT(14) /* No automatic CMD12 issue */
  21. #define UNIPHIER_SD_CMD_MULTI BIT(13) /* multiple block transfer */
  22. #define UNIPHIER_SD_CMD_RD BIT(12) /* 1: read, 0: write */
  23. #define UNIPHIER_SD_CMD_DATA BIT(11) /* data transfer */
  24. #define UNIPHIER_SD_CMD_APP BIT(6) /* ACMD preceded by CMD55 */
  25. #define UNIPHIER_SD_CMD_NORMAL (0 << 8)/* auto-detect of resp-type */
  26. #define UNIPHIER_SD_CMD_RSP_NONE (3 << 8)/* response: none */
  27. #define UNIPHIER_SD_CMD_RSP_R1 (4 << 8)/* response: R1, R5, R6, R7 */
  28. #define UNIPHIER_SD_CMD_RSP_R1B (5 << 8)/* response: R1b, R5b */
  29. #define UNIPHIER_SD_CMD_RSP_R2 (6 << 8)/* response: R2 */
  30. #define UNIPHIER_SD_CMD_RSP_R3 (7 << 8)/* response: R3, R4 */
  31. #define UNIPHIER_SD_ARG 0x008 /* command argument */
  32. #define UNIPHIER_SD_STOP 0x010 /* stop action control */
  33. #define UNIPHIER_SD_STOP_SEC BIT(8) /* use sector count */
  34. #define UNIPHIER_SD_STOP_STP BIT(0) /* issue CMD12 */
  35. #define UNIPHIER_SD_SECCNT 0x014 /* sector counter */
  36. #define UNIPHIER_SD_RSP10 0x018 /* response[39:8] */
  37. #define UNIPHIER_SD_RSP32 0x020 /* response[71:40] */
  38. #define UNIPHIER_SD_RSP54 0x028 /* response[103:72] */
  39. #define UNIPHIER_SD_RSP76 0x030 /* response[127:104] */
  40. #define UNIPHIER_SD_INFO1 0x038 /* IRQ status 1 */
  41. #define UNIPHIER_SD_INFO1_CD BIT(5) /* state of card detect */
  42. #define UNIPHIER_SD_INFO1_INSERT BIT(4) /* card inserted */
  43. #define UNIPHIER_SD_INFO1_REMOVE BIT(3) /* card removed */
  44. #define UNIPHIER_SD_INFO1_CMP BIT(2) /* data complete */
  45. #define UNIPHIER_SD_INFO1_RSP BIT(0) /* response complete */
  46. #define UNIPHIER_SD_INFO2 0x03c /* IRQ status 2 */
  47. #define UNIPHIER_SD_INFO2_ERR_ILA BIT(15) /* illegal access err */
  48. #define UNIPHIER_SD_INFO2_CBSY BIT(14) /* command busy */
  49. #define UNIPHIER_SD_INFO2_BWE BIT(9) /* write buffer ready */
  50. #define UNIPHIER_SD_INFO2_BRE BIT(8) /* read buffer ready */
  51. #define UNIPHIER_SD_INFO2_DAT0 BIT(7) /* SDDAT0 */
  52. #define UNIPHIER_SD_INFO2_ERR_RTO BIT(6) /* response time out */
  53. #define UNIPHIER_SD_INFO2_ERR_ILR BIT(5) /* illegal read err */
  54. #define UNIPHIER_SD_INFO2_ERR_ILW BIT(4) /* illegal write err */
  55. #define UNIPHIER_SD_INFO2_ERR_TO BIT(3) /* time out error */
  56. #define UNIPHIER_SD_INFO2_ERR_END BIT(2) /* END bit error */
  57. #define UNIPHIER_SD_INFO2_ERR_CRC BIT(1) /* CRC error */
  58. #define UNIPHIER_SD_INFO2_ERR_IDX BIT(0) /* cmd index error */
  59. #define UNIPHIER_SD_INFO1_MASK 0x040
  60. #define UNIPHIER_SD_INFO2_MASK 0x044
  61. #define UNIPHIER_SD_CLKCTL 0x048 /* clock divisor */
  62. #define UNIPHIER_SD_CLKCTL_DIV_MASK 0x104ff
  63. #define UNIPHIER_SD_CLKCTL_DIV1024 BIT(16) /* SDCLK = CLK / 1024 */
  64. #define UNIPHIER_SD_CLKCTL_DIV512 BIT(7) /* SDCLK = CLK / 512 */
  65. #define UNIPHIER_SD_CLKCTL_DIV256 BIT(6) /* SDCLK = CLK / 256 */
  66. #define UNIPHIER_SD_CLKCTL_DIV128 BIT(5) /* SDCLK = CLK / 128 */
  67. #define UNIPHIER_SD_CLKCTL_DIV64 BIT(4) /* SDCLK = CLK / 64 */
  68. #define UNIPHIER_SD_CLKCTL_DIV32 BIT(3) /* SDCLK = CLK / 32 */
  69. #define UNIPHIER_SD_CLKCTL_DIV16 BIT(2) /* SDCLK = CLK / 16 */
  70. #define UNIPHIER_SD_CLKCTL_DIV8 BIT(1) /* SDCLK = CLK / 8 */
  71. #define UNIPHIER_SD_CLKCTL_DIV4 BIT(0) /* SDCLK = CLK / 4 */
  72. #define UNIPHIER_SD_CLKCTL_DIV2 0 /* SDCLK = CLK / 2 */
  73. #define UNIPHIER_SD_CLKCTL_DIV1 BIT(10) /* SDCLK = CLK */
  74. #define UNIPHIER_SD_CLKCTL_OFFEN BIT(9) /* stop SDCLK when unused */
  75. #define UNIPHIER_SD_CLKCTL_SCLKEN BIT(8) /* SDCLK output enable */
  76. #define UNIPHIER_SD_SIZE 0x04c /* block size */
  77. #define UNIPHIER_SD_OPTION 0x050
  78. #define UNIPHIER_SD_OPTION_WIDTH_MASK (5 << 13)
  79. #define UNIPHIER_SD_OPTION_WIDTH_1 (4 << 13)
  80. #define UNIPHIER_SD_OPTION_WIDTH_4 (0 << 13)
  81. #define UNIPHIER_SD_OPTION_WIDTH_8 (1 << 13)
  82. #define UNIPHIER_SD_BUF 0x060 /* read/write buffer */
  83. #define UNIPHIER_SD_EXTMODE 0x1b0
  84. #define UNIPHIER_SD_EXTMODE_DMA_EN BIT(1) /* transfer 1: DMA, 0: pio */
  85. #define UNIPHIER_SD_SOFT_RST 0x1c0
  86. #define UNIPHIER_SD_SOFT_RST_RSTX BIT(0) /* reset deassert */
  87. #define UNIPHIER_SD_VERSION 0x1c4 /* version register */
  88. #define UNIPHIER_SD_VERSION_IP 0xff /* IP version */
  89. #define UNIPHIER_SD_HOST_MODE 0x1c8
  90. #define UNIPHIER_SD_IF_MODE 0x1cc
  91. #define UNIPHIER_SD_IF_MODE_DDR BIT(0) /* DDR mode */
  92. #define UNIPHIER_SD_VOLT 0x1e4 /* voltage switch */
  93. #define UNIPHIER_SD_VOLT_MASK (3 << 0)
  94. #define UNIPHIER_SD_VOLT_OFF (0 << 0)
  95. #define UNIPHIER_SD_VOLT_330 (1 << 0)/* 3.3V signal */
  96. #define UNIPHIER_SD_VOLT_180 (2 << 0)/* 1.8V signal */
  97. #define UNIPHIER_SD_DMA_MODE 0x410
  98. #define UNIPHIER_SD_DMA_MODE_DIR_RD BIT(16) /* 1: from device, 0: to dev */
  99. #define UNIPHIER_SD_DMA_MODE_ADDR_INC BIT(0) /* 1: address inc, 0: fixed */
  100. #define UNIPHIER_SD_DMA_CTL 0x414
  101. #define UNIPHIER_SD_DMA_CTL_START BIT(0) /* start DMA (auto cleared) */
  102. #define UNIPHIER_SD_DMA_RST 0x418
  103. #define UNIPHIER_SD_DMA_RST_RD BIT(9)
  104. #define UNIPHIER_SD_DMA_RST_WR BIT(8)
  105. #define UNIPHIER_SD_DMA_INFO1 0x420
  106. #define UNIPHIER_SD_DMA_INFO1_END_RD2 BIT(20) /* DMA from device is complete*/
  107. #define UNIPHIER_SD_DMA_INFO1_END_RD BIT(17) /* Don't use! Hardware bug */
  108. #define UNIPHIER_SD_DMA_INFO1_END_WR BIT(16) /* DMA to device is complete */
  109. #define UNIPHIER_SD_DMA_INFO1_MASK 0x424
  110. #define UNIPHIER_SD_DMA_INFO2 0x428
  111. #define UNIPHIER_SD_DMA_INFO2_ERR_RD BIT(17)
  112. #define UNIPHIER_SD_DMA_INFO2_ERR_WR BIT(16)
  113. #define UNIPHIER_SD_DMA_INFO2_MASK 0x42c
  114. #define UNIPHIER_SD_DMA_ADDR_L 0x440
  115. #define UNIPHIER_SD_DMA_ADDR_H 0x444
  116. /* alignment required by the DMA engine of this controller */
  117. #define UNIPHIER_SD_DMA_MINALIGN 0x10
  118. struct uniphier_sd_plat {
  119. struct mmc_config cfg;
  120. struct mmc mmc;
  121. };
  122. struct uniphier_sd_priv {
  123. void __iomem *regbase;
  124. unsigned long mclk;
  125. unsigned int version;
  126. u32 caps;
  127. #define UNIPHIER_SD_CAP_NONREMOVABLE BIT(0) /* Nonremovable e.g. eMMC */
  128. #define UNIPHIER_SD_CAP_DMA_INTERNAL BIT(1) /* have internal DMA engine */
  129. #define UNIPHIER_SD_CAP_DIV1024 BIT(2) /* divisor 1024 is available */
  130. #define UNIPHIER_SD_CAP_64BIT BIT(3) /* Controller is 64bit */
  131. };
  132. static u64 uniphier_sd_readq(struct uniphier_sd_priv *priv, unsigned int reg)
  133. {
  134. if (priv->caps & UNIPHIER_SD_CAP_64BIT)
  135. return readq(priv->regbase + (reg << 1));
  136. else
  137. return readq(priv->regbase + reg);
  138. }
  139. static void uniphier_sd_writeq(struct uniphier_sd_priv *priv,
  140. u64 val, unsigned int reg)
  141. {
  142. if (priv->caps & UNIPHIER_SD_CAP_64BIT)
  143. writeq(val, priv->regbase + (reg << 1));
  144. else
  145. writeq(val, priv->regbase + reg);
  146. }
  147. static u32 uniphier_sd_readl(struct uniphier_sd_priv *priv, unsigned int reg)
  148. {
  149. if (priv->caps & UNIPHIER_SD_CAP_64BIT)
  150. return readl(priv->regbase + (reg << 1));
  151. else
  152. return readl(priv->regbase + reg);
  153. }
  154. static void uniphier_sd_writel(struct uniphier_sd_priv *priv,
  155. u32 val, unsigned int reg)
  156. {
  157. if (priv->caps & UNIPHIER_SD_CAP_64BIT)
  158. writel(val, priv->regbase + (reg << 1));
  159. else
  160. writel(val, priv->regbase + reg);
  161. }
  162. static dma_addr_t __dma_map_single(void *ptr, size_t size,
  163. enum dma_data_direction dir)
  164. {
  165. unsigned long addr = (unsigned long)ptr;
  166. if (dir == DMA_FROM_DEVICE)
  167. invalidate_dcache_range(addr, addr + size);
  168. else
  169. flush_dcache_range(addr, addr + size);
  170. return addr;
  171. }
  172. static void __dma_unmap_single(dma_addr_t addr, size_t size,
  173. enum dma_data_direction dir)
  174. {
  175. if (dir != DMA_TO_DEVICE)
  176. invalidate_dcache_range(addr, addr + size);
  177. }
  178. static int uniphier_sd_check_error(struct udevice *dev)
  179. {
  180. struct uniphier_sd_priv *priv = dev_get_priv(dev);
  181. u32 info2 = uniphier_sd_readl(priv, UNIPHIER_SD_INFO2);
  182. if (info2 & UNIPHIER_SD_INFO2_ERR_RTO) {
  183. /*
  184. * TIMEOUT must be returned for unsupported command. Do not
  185. * display error log since this might be a part of sequence to
  186. * distinguish between SD and MMC.
  187. */
  188. return -ETIMEDOUT;
  189. }
  190. if (info2 & UNIPHIER_SD_INFO2_ERR_TO) {
  191. dev_err(dev, "timeout error\n");
  192. return -ETIMEDOUT;
  193. }
  194. if (info2 & (UNIPHIER_SD_INFO2_ERR_END | UNIPHIER_SD_INFO2_ERR_CRC |
  195. UNIPHIER_SD_INFO2_ERR_IDX)) {
  196. dev_err(dev, "communication out of sync\n");
  197. return -EILSEQ;
  198. }
  199. if (info2 & (UNIPHIER_SD_INFO2_ERR_ILA | UNIPHIER_SD_INFO2_ERR_ILR |
  200. UNIPHIER_SD_INFO2_ERR_ILW)) {
  201. dev_err(dev, "illegal access\n");
  202. return -EIO;
  203. }
  204. return 0;
  205. }
  206. static int uniphier_sd_wait_for_irq(struct udevice *dev, unsigned int reg,
  207. u32 flag)
  208. {
  209. struct uniphier_sd_priv *priv = dev_get_priv(dev);
  210. long wait = 1000000;
  211. int ret;
  212. while (!(uniphier_sd_readl(priv, reg) & flag)) {
  213. if (wait-- < 0) {
  214. dev_err(dev, "timeout\n");
  215. return -ETIMEDOUT;
  216. }
  217. ret = uniphier_sd_check_error(dev);
  218. if (ret)
  219. return ret;
  220. udelay(1);
  221. }
  222. return 0;
  223. }
  224. static int uniphier_sd_pio_read_one_block(struct udevice *dev, char *pbuf,
  225. uint blocksize)
  226. {
  227. struct uniphier_sd_priv *priv = dev_get_priv(dev);
  228. int i, ret;
  229. /* wait until the buffer is filled with data */
  230. ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO2,
  231. UNIPHIER_SD_INFO2_BRE);
  232. if (ret)
  233. return ret;
  234. /*
  235. * Clear the status flag _before_ read the buffer out because
  236. * UNIPHIER_SD_INFO2_BRE is edge-triggered, not level-triggered.
  237. */
  238. uniphier_sd_writel(priv, 0, UNIPHIER_SD_INFO2);
  239. if (priv->caps & UNIPHIER_SD_CAP_64BIT) {
  240. u64 *buf = (u64 *)pbuf;
  241. if (likely(IS_ALIGNED((uintptr_t)buf, 8))) {
  242. for (i = 0; i < blocksize / 8; i++) {
  243. *buf++ = uniphier_sd_readq(priv,
  244. UNIPHIER_SD_BUF);
  245. }
  246. } else {
  247. for (i = 0; i < blocksize / 8; i++) {
  248. u64 data;
  249. data = uniphier_sd_readq(priv,
  250. UNIPHIER_SD_BUF);
  251. put_unaligned(data, buf++);
  252. }
  253. }
  254. } else {
  255. u32 *buf = (u32 *)pbuf;
  256. if (likely(IS_ALIGNED((uintptr_t)buf, 4))) {
  257. for (i = 0; i < blocksize / 4; i++) {
  258. *buf++ = uniphier_sd_readl(priv,
  259. UNIPHIER_SD_BUF);
  260. }
  261. } else {
  262. for (i = 0; i < blocksize / 4; i++) {
  263. u32 data;
  264. data = uniphier_sd_readl(priv, UNIPHIER_SD_BUF);
  265. put_unaligned(data, buf++);
  266. }
  267. }
  268. }
  269. return 0;
  270. }
  271. static int uniphier_sd_pio_write_one_block(struct udevice *dev,
  272. const char *pbuf, uint blocksize)
  273. {
  274. struct uniphier_sd_priv *priv = dev_get_priv(dev);
  275. int i, ret;
  276. /* wait until the buffer becomes empty */
  277. ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO2,
  278. UNIPHIER_SD_INFO2_BWE);
  279. if (ret)
  280. return ret;
  281. uniphier_sd_writel(priv, 0, UNIPHIER_SD_INFO2);
  282. if (priv->caps & UNIPHIER_SD_CAP_64BIT) {
  283. const u64 *buf = (const u64 *)pbuf;
  284. if (likely(IS_ALIGNED((uintptr_t)buf, 8))) {
  285. for (i = 0; i < blocksize / 8; i++) {
  286. uniphier_sd_writeq(priv, *buf++,
  287. UNIPHIER_SD_BUF);
  288. }
  289. } else {
  290. for (i = 0; i < blocksize / 8; i++) {
  291. u64 data = get_unaligned(buf++);
  292. uniphier_sd_writeq(priv, data,
  293. UNIPHIER_SD_BUF);
  294. }
  295. }
  296. } else {
  297. const u32 *buf = (const u32 *)pbuf;
  298. if (likely(IS_ALIGNED((uintptr_t)buf, 4))) {
  299. for (i = 0; i < blocksize / 4; i++) {
  300. uniphier_sd_writel(priv, *buf++,
  301. UNIPHIER_SD_BUF);
  302. }
  303. } else {
  304. for (i = 0; i < blocksize / 4; i++) {
  305. u32 data = get_unaligned(buf++);
  306. uniphier_sd_writel(priv, data,
  307. UNIPHIER_SD_BUF);
  308. }
  309. }
  310. }
  311. return 0;
  312. }
  313. static int uniphier_sd_pio_xfer(struct udevice *dev, struct mmc_data *data)
  314. {
  315. const char *src = data->src;
  316. char *dest = data->dest;
  317. int i, ret;
  318. for (i = 0; i < data->blocks; i++) {
  319. if (data->flags & MMC_DATA_READ)
  320. ret = uniphier_sd_pio_read_one_block(dev, dest,
  321. data->blocksize);
  322. else
  323. ret = uniphier_sd_pio_write_one_block(dev, src,
  324. data->blocksize);
  325. if (ret)
  326. return ret;
  327. if (data->flags & MMC_DATA_READ)
  328. dest += data->blocksize;
  329. else
  330. src += data->blocksize;
  331. }
  332. return 0;
  333. }
  334. static void uniphier_sd_dma_start(struct uniphier_sd_priv *priv,
  335. dma_addr_t dma_addr)
  336. {
  337. u32 tmp;
  338. uniphier_sd_writel(priv, 0, UNIPHIER_SD_DMA_INFO1);
  339. uniphier_sd_writel(priv, 0, UNIPHIER_SD_DMA_INFO2);
  340. /* enable DMA */
  341. tmp = uniphier_sd_readl(priv, UNIPHIER_SD_EXTMODE);
  342. tmp |= UNIPHIER_SD_EXTMODE_DMA_EN;
  343. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_EXTMODE);
  344. uniphier_sd_writel(priv, dma_addr & U32_MAX, UNIPHIER_SD_DMA_ADDR_L);
  345. /* suppress the warning "right shift count >= width of type" */
  346. dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr));
  347. uniphier_sd_writel(priv, dma_addr & U32_MAX, UNIPHIER_SD_DMA_ADDR_H);
  348. uniphier_sd_writel(priv, UNIPHIER_SD_DMA_CTL_START, UNIPHIER_SD_DMA_CTL);
  349. }
  350. static int uniphier_sd_dma_wait_for_irq(struct udevice *dev, u32 flag,
  351. unsigned int blocks)
  352. {
  353. struct uniphier_sd_priv *priv = dev_get_priv(dev);
  354. long wait = 1000000 + 10 * blocks;
  355. while (!(uniphier_sd_readl(priv, UNIPHIER_SD_DMA_INFO1) & flag)) {
  356. if (wait-- < 0) {
  357. dev_err(dev, "timeout during DMA\n");
  358. return -ETIMEDOUT;
  359. }
  360. udelay(10);
  361. }
  362. if (uniphier_sd_readl(priv, UNIPHIER_SD_DMA_INFO2)) {
  363. dev_err(dev, "error during DMA\n");
  364. return -EIO;
  365. }
  366. return 0;
  367. }
  368. static int uniphier_sd_dma_xfer(struct udevice *dev, struct mmc_data *data)
  369. {
  370. struct uniphier_sd_priv *priv = dev_get_priv(dev);
  371. size_t len = data->blocks * data->blocksize;
  372. void *buf;
  373. enum dma_data_direction dir;
  374. dma_addr_t dma_addr;
  375. u32 poll_flag, tmp;
  376. int ret;
  377. tmp = uniphier_sd_readl(priv, UNIPHIER_SD_DMA_MODE);
  378. if (data->flags & MMC_DATA_READ) {
  379. buf = data->dest;
  380. dir = DMA_FROM_DEVICE;
  381. poll_flag = UNIPHIER_SD_DMA_INFO1_END_RD2;
  382. tmp |= UNIPHIER_SD_DMA_MODE_DIR_RD;
  383. } else {
  384. buf = (void *)data->src;
  385. dir = DMA_TO_DEVICE;
  386. poll_flag = UNIPHIER_SD_DMA_INFO1_END_WR;
  387. tmp &= ~UNIPHIER_SD_DMA_MODE_DIR_RD;
  388. }
  389. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_DMA_MODE);
  390. dma_addr = __dma_map_single(buf, len, dir);
  391. uniphier_sd_dma_start(priv, dma_addr);
  392. ret = uniphier_sd_dma_wait_for_irq(dev, poll_flag, data->blocks);
  393. __dma_unmap_single(dma_addr, len, dir);
  394. return ret;
  395. }
  396. /* check if the address is DMA'able */
  397. static bool uniphier_sd_addr_is_dmaable(unsigned long addr)
  398. {
  399. if (!IS_ALIGNED(addr, UNIPHIER_SD_DMA_MINALIGN))
  400. return false;
  401. #if defined(CONFIG_ARCH_UNIPHIER) && !defined(CONFIG_ARM64) && \
  402. defined(CONFIG_SPL_BUILD)
  403. /*
  404. * For UniPhier ARMv7 SoCs, the stack is allocated in the locked ways
  405. * of L2, which is unreachable from the DMA engine.
  406. */
  407. if (addr < CONFIG_SPL_STACK)
  408. return false;
  409. #endif
  410. return true;
  411. }
  412. static int uniphier_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  413. struct mmc_data *data)
  414. {
  415. struct uniphier_sd_priv *priv = dev_get_priv(dev);
  416. int ret;
  417. u32 tmp;
  418. if (uniphier_sd_readl(priv, UNIPHIER_SD_INFO2) & UNIPHIER_SD_INFO2_CBSY) {
  419. dev_err(dev, "command busy\n");
  420. return -EBUSY;
  421. }
  422. /* clear all status flags */
  423. uniphier_sd_writel(priv, 0, UNIPHIER_SD_INFO1);
  424. uniphier_sd_writel(priv, 0, UNIPHIER_SD_INFO2);
  425. /* disable DMA once */
  426. tmp = uniphier_sd_readl(priv, UNIPHIER_SD_EXTMODE);
  427. tmp &= ~UNIPHIER_SD_EXTMODE_DMA_EN;
  428. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_EXTMODE);
  429. uniphier_sd_writel(priv, cmd->cmdarg, UNIPHIER_SD_ARG);
  430. tmp = cmd->cmdidx;
  431. if (data) {
  432. uniphier_sd_writel(priv, data->blocksize, UNIPHIER_SD_SIZE);
  433. uniphier_sd_writel(priv, data->blocks, UNIPHIER_SD_SECCNT);
  434. /* Do not send CMD12 automatically */
  435. tmp |= UNIPHIER_SD_CMD_NOSTOP | UNIPHIER_SD_CMD_DATA;
  436. if (data->blocks > 1)
  437. tmp |= UNIPHIER_SD_CMD_MULTI;
  438. if (data->flags & MMC_DATA_READ)
  439. tmp |= UNIPHIER_SD_CMD_RD;
  440. }
  441. /*
  442. * Do not use the response type auto-detection on this hardware.
  443. * CMD8, for example, has different response types on SD and eMMC,
  444. * while this controller always assumes the response type for SD.
  445. * Set the response type manually.
  446. */
  447. switch (cmd->resp_type) {
  448. case MMC_RSP_NONE:
  449. tmp |= UNIPHIER_SD_CMD_RSP_NONE;
  450. break;
  451. case MMC_RSP_R1:
  452. tmp |= UNIPHIER_SD_CMD_RSP_R1;
  453. break;
  454. case MMC_RSP_R1b:
  455. tmp |= UNIPHIER_SD_CMD_RSP_R1B;
  456. break;
  457. case MMC_RSP_R2:
  458. tmp |= UNIPHIER_SD_CMD_RSP_R2;
  459. break;
  460. case MMC_RSP_R3:
  461. tmp |= UNIPHIER_SD_CMD_RSP_R3;
  462. break;
  463. default:
  464. dev_err(dev, "unknown response type\n");
  465. return -EINVAL;
  466. }
  467. dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n",
  468. cmd->cmdidx, tmp, cmd->cmdarg);
  469. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_CMD);
  470. ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO1,
  471. UNIPHIER_SD_INFO1_RSP);
  472. if (ret)
  473. return ret;
  474. if (cmd->resp_type & MMC_RSP_136) {
  475. u32 rsp_127_104 = uniphier_sd_readl(priv, UNIPHIER_SD_RSP76);
  476. u32 rsp_103_72 = uniphier_sd_readl(priv, UNIPHIER_SD_RSP54);
  477. u32 rsp_71_40 = uniphier_sd_readl(priv, UNIPHIER_SD_RSP32);
  478. u32 rsp_39_8 = uniphier_sd_readl(priv, UNIPHIER_SD_RSP10);
  479. cmd->response[0] = ((rsp_127_104 & 0x00ffffff) << 8) |
  480. ((rsp_103_72 & 0xff000000) >> 24);
  481. cmd->response[1] = ((rsp_103_72 & 0x00ffffff) << 8) |
  482. ((rsp_71_40 & 0xff000000) >> 24);
  483. cmd->response[2] = ((rsp_71_40 & 0x00ffffff) << 8) |
  484. ((rsp_39_8 & 0xff000000) >> 24);
  485. cmd->response[3] = (rsp_39_8 & 0xffffff) << 8;
  486. } else {
  487. /* bit 39-8 */
  488. cmd->response[0] = uniphier_sd_readl(priv, UNIPHIER_SD_RSP10);
  489. }
  490. if (data) {
  491. /* use DMA if the HW supports it and the buffer is aligned */
  492. if (priv->caps & UNIPHIER_SD_CAP_DMA_INTERNAL &&
  493. uniphier_sd_addr_is_dmaable((long)data->src))
  494. ret = uniphier_sd_dma_xfer(dev, data);
  495. else
  496. ret = uniphier_sd_pio_xfer(dev, data);
  497. ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO1,
  498. UNIPHIER_SD_INFO1_CMP);
  499. if (ret)
  500. return ret;
  501. }
  502. return ret;
  503. }
  504. static int uniphier_sd_set_bus_width(struct uniphier_sd_priv *priv,
  505. struct mmc *mmc)
  506. {
  507. u32 val, tmp;
  508. switch (mmc->bus_width) {
  509. case 1:
  510. val = UNIPHIER_SD_OPTION_WIDTH_1;
  511. break;
  512. case 4:
  513. val = UNIPHIER_SD_OPTION_WIDTH_4;
  514. break;
  515. case 8:
  516. val = UNIPHIER_SD_OPTION_WIDTH_8;
  517. break;
  518. default:
  519. return -EINVAL;
  520. }
  521. tmp = uniphier_sd_readl(priv, UNIPHIER_SD_OPTION);
  522. tmp &= ~UNIPHIER_SD_OPTION_WIDTH_MASK;
  523. tmp |= val;
  524. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_OPTION);
  525. return 0;
  526. }
  527. static void uniphier_sd_set_ddr_mode(struct uniphier_sd_priv *priv,
  528. struct mmc *mmc)
  529. {
  530. u32 tmp;
  531. tmp = uniphier_sd_readl(priv, UNIPHIER_SD_IF_MODE);
  532. if (mmc->ddr_mode)
  533. tmp |= UNIPHIER_SD_IF_MODE_DDR;
  534. else
  535. tmp &= ~UNIPHIER_SD_IF_MODE_DDR;
  536. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_IF_MODE);
  537. }
  538. static void uniphier_sd_set_clk_rate(struct uniphier_sd_priv *priv,
  539. struct mmc *mmc)
  540. {
  541. unsigned int divisor;
  542. u32 val, tmp;
  543. if (!mmc->clock)
  544. return;
  545. divisor = DIV_ROUND_UP(priv->mclk, mmc->clock);
  546. if (divisor <= 1)
  547. val = UNIPHIER_SD_CLKCTL_DIV1;
  548. else if (divisor <= 2)
  549. val = UNIPHIER_SD_CLKCTL_DIV2;
  550. else if (divisor <= 4)
  551. val = UNIPHIER_SD_CLKCTL_DIV4;
  552. else if (divisor <= 8)
  553. val = UNIPHIER_SD_CLKCTL_DIV8;
  554. else if (divisor <= 16)
  555. val = UNIPHIER_SD_CLKCTL_DIV16;
  556. else if (divisor <= 32)
  557. val = UNIPHIER_SD_CLKCTL_DIV32;
  558. else if (divisor <= 64)
  559. val = UNIPHIER_SD_CLKCTL_DIV64;
  560. else if (divisor <= 128)
  561. val = UNIPHIER_SD_CLKCTL_DIV128;
  562. else if (divisor <= 256)
  563. val = UNIPHIER_SD_CLKCTL_DIV256;
  564. else if (divisor <= 512 || !(priv->caps & UNIPHIER_SD_CAP_DIV1024))
  565. val = UNIPHIER_SD_CLKCTL_DIV512;
  566. else
  567. val = UNIPHIER_SD_CLKCTL_DIV1024;
  568. tmp = uniphier_sd_readl(priv, UNIPHIER_SD_CLKCTL);
  569. if (tmp & UNIPHIER_SD_CLKCTL_SCLKEN &&
  570. (tmp & UNIPHIER_SD_CLKCTL_DIV_MASK) == val)
  571. return;
  572. /* stop the clock before changing its rate to avoid a glitch signal */
  573. tmp &= ~UNIPHIER_SD_CLKCTL_SCLKEN;
  574. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_CLKCTL);
  575. tmp &= ~UNIPHIER_SD_CLKCTL_DIV_MASK;
  576. tmp |= val | UNIPHIER_SD_CLKCTL_OFFEN;
  577. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_CLKCTL);
  578. tmp |= UNIPHIER_SD_CLKCTL_SCLKEN;
  579. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_CLKCTL);
  580. udelay(1000);
  581. }
  582. static int uniphier_sd_set_ios(struct udevice *dev)
  583. {
  584. struct uniphier_sd_priv *priv = dev_get_priv(dev);
  585. struct mmc *mmc = mmc_get_mmc_dev(dev);
  586. int ret;
  587. dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n",
  588. mmc->clock, mmc->ddr_mode, mmc->bus_width);
  589. ret = uniphier_sd_set_bus_width(priv, mmc);
  590. if (ret)
  591. return ret;
  592. uniphier_sd_set_ddr_mode(priv, mmc);
  593. uniphier_sd_set_clk_rate(priv, mmc);
  594. return 0;
  595. }
  596. static int uniphier_sd_get_cd(struct udevice *dev)
  597. {
  598. struct uniphier_sd_priv *priv = dev_get_priv(dev);
  599. if (priv->caps & UNIPHIER_SD_CAP_NONREMOVABLE)
  600. return 1;
  601. return !!(uniphier_sd_readl(priv, UNIPHIER_SD_INFO1) &
  602. UNIPHIER_SD_INFO1_CD);
  603. }
  604. static const struct dm_mmc_ops uniphier_sd_ops = {
  605. .send_cmd = uniphier_sd_send_cmd,
  606. .set_ios = uniphier_sd_set_ios,
  607. .get_cd = uniphier_sd_get_cd,
  608. };
  609. static void uniphier_sd_host_init(struct uniphier_sd_priv *priv)
  610. {
  611. u32 tmp;
  612. /* soft reset of the host */
  613. tmp = uniphier_sd_readl(priv, UNIPHIER_SD_SOFT_RST);
  614. tmp &= ~UNIPHIER_SD_SOFT_RST_RSTX;
  615. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_SOFT_RST);
  616. tmp |= UNIPHIER_SD_SOFT_RST_RSTX;
  617. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_SOFT_RST);
  618. /* FIXME: implement eMMC hw_reset */
  619. uniphier_sd_writel(priv, UNIPHIER_SD_STOP_SEC, UNIPHIER_SD_STOP);
  620. /*
  621. * Connected to 32bit AXI.
  622. * This register dropped backward compatibility at version 0x10.
  623. * Write an appropriate value depending on the IP version.
  624. */
  625. uniphier_sd_writel(priv, priv->version >= 0x10 ? 0x00000101 : 0x00000000,
  626. UNIPHIER_SD_HOST_MODE);
  627. if (priv->caps & UNIPHIER_SD_CAP_DMA_INTERNAL) {
  628. tmp = uniphier_sd_readl(priv, UNIPHIER_SD_DMA_MODE);
  629. tmp |= UNIPHIER_SD_DMA_MODE_ADDR_INC;
  630. uniphier_sd_writel(priv, tmp, UNIPHIER_SD_DMA_MODE);
  631. }
  632. }
  633. static int uniphier_sd_bind(struct udevice *dev)
  634. {
  635. struct uniphier_sd_plat *plat = dev_get_platdata(dev);
  636. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  637. }
  638. static int uniphier_sd_probe(struct udevice *dev)
  639. {
  640. struct uniphier_sd_plat *plat = dev_get_platdata(dev);
  641. struct uniphier_sd_priv *priv = dev_get_priv(dev);
  642. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  643. const u32 quirks = dev_get_driver_data(dev);
  644. fdt_addr_t base;
  645. struct clk clk;
  646. int ret;
  647. #ifdef CONFIG_DM_REGULATOR
  648. struct udevice *vqmmc_dev;
  649. #endif
  650. base = devfdt_get_addr(dev);
  651. if (base == FDT_ADDR_T_NONE)
  652. return -EINVAL;
  653. priv->regbase = devm_ioremap(dev, base, SZ_2K);
  654. if (!priv->regbase)
  655. return -ENOMEM;
  656. #ifdef CONFIG_DM_REGULATOR
  657. ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
  658. if (!ret) {
  659. /* Set the regulator to 3.3V until we support 1.8V modes */
  660. regulator_set_value(vqmmc_dev, 3300000);
  661. regulator_set_enable(vqmmc_dev, true);
  662. }
  663. #endif
  664. ret = clk_get_by_index(dev, 0, &clk);
  665. if (ret < 0) {
  666. dev_err(dev, "failed to get host clock\n");
  667. return ret;
  668. }
  669. /* set to max rate */
  670. priv->mclk = clk_set_rate(&clk, ULONG_MAX);
  671. if (IS_ERR_VALUE(priv->mclk)) {
  672. dev_err(dev, "failed to set rate for host clock\n");
  673. clk_free(&clk);
  674. return priv->mclk;
  675. }
  676. ret = clk_enable(&clk);
  677. clk_free(&clk);
  678. if (ret) {
  679. dev_err(dev, "failed to enable host clock\n");
  680. return ret;
  681. }
  682. plat->cfg.name = dev->name;
  683. plat->cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
  684. switch (fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "bus-width",
  685. 1)) {
  686. case 8:
  687. plat->cfg.host_caps |= MMC_MODE_8BIT;
  688. break;
  689. case 4:
  690. plat->cfg.host_caps |= MMC_MODE_4BIT;
  691. break;
  692. case 1:
  693. break;
  694. default:
  695. dev_err(dev, "Invalid \"bus-width\" value\n");
  696. return -EINVAL;
  697. }
  698. if (quirks) {
  699. priv->caps = quirks;
  700. } else {
  701. priv->version = uniphier_sd_readl(priv, UNIPHIER_SD_VERSION) &
  702. UNIPHIER_SD_VERSION_IP;
  703. dev_dbg(dev, "version %x\n", priv->version);
  704. if (priv->version >= 0x10) {
  705. priv->caps |= UNIPHIER_SD_CAP_DMA_INTERNAL;
  706. priv->caps |= UNIPHIER_SD_CAP_DIV1024;
  707. }
  708. }
  709. if (fdt_get_property(gd->fdt_blob, dev_of_offset(dev), "non-removable",
  710. NULL))
  711. priv->caps |= UNIPHIER_SD_CAP_NONREMOVABLE;
  712. uniphier_sd_host_init(priv);
  713. plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
  714. plat->cfg.f_min = priv->mclk /
  715. (priv->caps & UNIPHIER_SD_CAP_DIV1024 ? 1024 : 512);
  716. plat->cfg.f_max = priv->mclk;
  717. plat->cfg.b_max = U32_MAX; /* max value of UNIPHIER_SD_SECCNT */
  718. upriv->mmc = &plat->mmc;
  719. return 0;
  720. }
  721. static const struct udevice_id uniphier_sd_match[] = {
  722. { .compatible = "renesas,sdhi-r8a7790", .data = 0 },
  723. { .compatible = "renesas,sdhi-r8a7791", .data = 0 },
  724. { .compatible = "renesas,sdhi-r8a7792", .data = 0 },
  725. { .compatible = "renesas,sdhi-r8a7793", .data = 0 },
  726. { .compatible = "renesas,sdhi-r8a7794", .data = 0 },
  727. { .compatible = "renesas,sdhi-r8a7795", .data = UNIPHIER_SD_CAP_64BIT },
  728. { .compatible = "renesas,sdhi-r8a7796", .data = UNIPHIER_SD_CAP_64BIT },
  729. { .compatible = "renesas,sdhi-r8a77965", .data = UNIPHIER_SD_CAP_64BIT },
  730. { .compatible = "renesas,sdhi-r8a77970", .data = UNIPHIER_SD_CAP_64BIT },
  731. { .compatible = "renesas,sdhi-r8a77995", .data = UNIPHIER_SD_CAP_64BIT },
  732. { .compatible = "socionext,uniphier-sdhc", .data = 0 },
  733. { /* sentinel */ }
  734. };
  735. U_BOOT_DRIVER(uniphier_mmc) = {
  736. .name = "uniphier-mmc",
  737. .id = UCLASS_MMC,
  738. .of_match = uniphier_sd_match,
  739. .bind = uniphier_sd_bind,
  740. .probe = uniphier_sd_probe,
  741. .priv_auto_alloc_size = sizeof(struct uniphier_sd_priv),
  742. .platdata_auto_alloc_size = sizeof(struct uniphier_sd_plat),
  743. .ops = &uniphier_sd_ops,
  744. };