dw_mmc-exynos.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Exynos Specific Extensions for Synopsys DW Multimedia Card Interface driver
  3. *
  4. * Copyright (C) 2012, Samsung Electronics Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/clk.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/dw_mmc.h>
  16. #include <linux/mmc/mmc.h>
  17. #include <linux/of.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/slab.h>
  20. #include "dw_mmc.h"
  21. #include "dw_mmc-pltfm.h"
  22. #define NUM_PINS(x) (x + 2)
  23. #define SDMMC_CLKSEL 0x09C
  24. #define SDMMC_CLKSEL_CCLK_SAMPLE(x) (((x) & 7) << 0)
  25. #define SDMMC_CLKSEL_CCLK_DRIVE(x) (((x) & 7) << 16)
  26. #define SDMMC_CLKSEL_CCLK_DIVIDER(x) (((x) & 7) << 24)
  27. #define SDMMC_CLKSEL_GET_DRV_WD3(x) (((x) >> 16) & 0x7)
  28. #define SDMMC_CLKSEL_TIMING(x, y, z) (SDMMC_CLKSEL_CCLK_SAMPLE(x) | \
  29. SDMMC_CLKSEL_CCLK_DRIVE(y) | \
  30. SDMMC_CLKSEL_CCLK_DIVIDER(z))
  31. #define SDMMC_CLKSEL_WAKEUP_INT BIT(11)
  32. #define EXYNOS4210_FIXED_CIU_CLK_DIV 2
  33. #define EXYNOS4412_FIXED_CIU_CLK_DIV 4
  34. /* Block number in eMMC */
  35. #define DWMCI_BLOCK_NUM 0xFFFFFFFF
  36. #define SDMMC_EMMCP_BASE 0x1000
  37. #define SDMMC_MPSECURITY (SDMMC_EMMCP_BASE + 0x0010)
  38. #define SDMMC_MPSBEGIN0 (SDMMC_EMMCP_BASE + 0x0200)
  39. #define SDMMC_MPSEND0 (SDMMC_EMMCP_BASE + 0x0204)
  40. #define SDMMC_MPSCTRL0 (SDMMC_EMMCP_BASE + 0x020C)
  41. /* SMU control bits */
  42. #define DWMCI_MPSCTRL_SECURE_READ_BIT BIT(7)
  43. #define DWMCI_MPSCTRL_SECURE_WRITE_BIT BIT(6)
  44. #define DWMCI_MPSCTRL_NON_SECURE_READ_BIT BIT(5)
  45. #define DWMCI_MPSCTRL_NON_SECURE_WRITE_BIT BIT(4)
  46. #define DWMCI_MPSCTRL_USE_FUSE_KEY BIT(3)
  47. #define DWMCI_MPSCTRL_ECB_MODE BIT(2)
  48. #define DWMCI_MPSCTRL_ENCRYPTION BIT(1)
  49. #define DWMCI_MPSCTRL_VALID BIT(0)
  50. #define EXYNOS_CCLKIN_MIN 50000000 /* unit: HZ */
  51. /* Variations in Exynos specific dw-mshc controller */
  52. enum dw_mci_exynos_type {
  53. DW_MCI_TYPE_EXYNOS4210,
  54. DW_MCI_TYPE_EXYNOS4412,
  55. DW_MCI_TYPE_EXYNOS5250,
  56. DW_MCI_TYPE_EXYNOS5420,
  57. DW_MCI_TYPE_EXYNOS5420_SMU,
  58. };
  59. /* Exynos implementation specific driver private data */
  60. struct dw_mci_exynos_priv_data {
  61. enum dw_mci_exynos_type ctrl_type;
  62. u8 ciu_div;
  63. u32 sdr_timing;
  64. u32 ddr_timing;
  65. u32 cur_speed;
  66. };
  67. static struct dw_mci_exynos_compatible {
  68. char *compatible;
  69. enum dw_mci_exynos_type ctrl_type;
  70. } exynos_compat[] = {
  71. {
  72. .compatible = "samsung,exynos4210-dw-mshc",
  73. .ctrl_type = DW_MCI_TYPE_EXYNOS4210,
  74. }, {
  75. .compatible = "samsung,exynos4412-dw-mshc",
  76. .ctrl_type = DW_MCI_TYPE_EXYNOS4412,
  77. }, {
  78. .compatible = "samsung,exynos5250-dw-mshc",
  79. .ctrl_type = DW_MCI_TYPE_EXYNOS5250,
  80. }, {
  81. .compatible = "samsung,exynos5420-dw-mshc",
  82. .ctrl_type = DW_MCI_TYPE_EXYNOS5420,
  83. }, {
  84. .compatible = "samsung,exynos5420-dw-mshc-smu",
  85. .ctrl_type = DW_MCI_TYPE_EXYNOS5420_SMU,
  86. },
  87. };
  88. static int dw_mci_exynos_priv_init(struct dw_mci *host)
  89. {
  90. struct dw_mci_exynos_priv_data *priv = host->priv;
  91. if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420_SMU) {
  92. mci_writel(host, MPSBEGIN0, 0);
  93. mci_writel(host, MPSEND0, DWMCI_BLOCK_NUM);
  94. mci_writel(host, MPSCTRL0, DWMCI_MPSCTRL_SECURE_WRITE_BIT |
  95. DWMCI_MPSCTRL_NON_SECURE_READ_BIT |
  96. DWMCI_MPSCTRL_VALID |
  97. DWMCI_MPSCTRL_NON_SECURE_WRITE_BIT);
  98. }
  99. return 0;
  100. }
  101. static int dw_mci_exynos_setup_clock(struct dw_mci *host)
  102. {
  103. struct dw_mci_exynos_priv_data *priv = host->priv;
  104. unsigned long rate = clk_get_rate(host->ciu_clk);
  105. host->bus_hz = rate / (priv->ciu_div + 1);
  106. return 0;
  107. }
  108. #ifdef CONFIG_PM_SLEEP
  109. static int dw_mci_exynos_suspend(struct device *dev)
  110. {
  111. struct dw_mci *host = dev_get_drvdata(dev);
  112. return dw_mci_suspend(host);
  113. }
  114. static int dw_mci_exynos_resume(struct device *dev)
  115. {
  116. struct dw_mci *host = dev_get_drvdata(dev);
  117. dw_mci_exynos_priv_init(host);
  118. return dw_mci_resume(host);
  119. }
  120. /**
  121. * dw_mci_exynos_resume_noirq - Exynos-specific resume code
  122. *
  123. * On exynos5420 there is a silicon errata that will sometimes leave the
  124. * WAKEUP_INT bit in the CLKSEL register asserted. This bit is 1 to indicate
  125. * that it fired and we can clear it by writing a 1 back. Clear it to prevent
  126. * interrupts from going off constantly.
  127. *
  128. * We run this code on all exynos variants because it doesn't hurt.
  129. */
  130. static int dw_mci_exynos_resume_noirq(struct device *dev)
  131. {
  132. struct dw_mci *host = dev_get_drvdata(dev);
  133. u32 clksel;
  134. clksel = mci_readl(host, CLKSEL);
  135. if (clksel & SDMMC_CLKSEL_WAKEUP_INT)
  136. mci_writel(host, CLKSEL, clksel);
  137. return 0;
  138. }
  139. #else
  140. #define dw_mci_exynos_suspend NULL
  141. #define dw_mci_exynos_resume NULL
  142. #define dw_mci_exynos_resume_noirq NULL
  143. #endif /* CONFIG_PM_SLEEP */
  144. static void dw_mci_exynos_prepare_command(struct dw_mci *host, u32 *cmdr)
  145. {
  146. /*
  147. * Exynos4412 and Exynos5250 extends the use of CMD register with the
  148. * use of bit 29 (which is reserved on standard MSHC controllers) for
  149. * optionally bypassing the HOLD register for command and data. The
  150. * HOLD register should be bypassed in case there is no phase shift
  151. * applied on CMD/DATA that is sent to the card.
  152. */
  153. if (SDMMC_CLKSEL_GET_DRV_WD3(mci_readl(host, CLKSEL)))
  154. *cmdr |= SDMMC_CMD_USE_HOLD_REG;
  155. }
  156. static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  157. {
  158. struct dw_mci_exynos_priv_data *priv = host->priv;
  159. unsigned int wanted = ios->clock;
  160. unsigned long actual;
  161. u8 div = priv->ciu_div + 1;
  162. if (ios->timing == MMC_TIMING_UHS_DDR50) {
  163. mci_writel(host, CLKSEL, priv->ddr_timing);
  164. /* Should be double rate for DDR mode */
  165. if (ios->bus_width == MMC_BUS_WIDTH_8)
  166. wanted <<= 1;
  167. } else {
  168. mci_writel(host, CLKSEL, priv->sdr_timing);
  169. }
  170. /* Don't care if wanted clock is zero */
  171. if (!wanted)
  172. return;
  173. /* Guaranteed minimum frequency for cclkin */
  174. if (wanted < EXYNOS_CCLKIN_MIN)
  175. wanted = EXYNOS_CCLKIN_MIN;
  176. if (wanted != priv->cur_speed) {
  177. int ret = clk_set_rate(host->ciu_clk, wanted * div);
  178. if (ret)
  179. dev_warn(host->dev,
  180. "failed to set clk-rate %u error: %d\n",
  181. wanted * div, ret);
  182. actual = clk_get_rate(host->ciu_clk);
  183. host->bus_hz = actual / div;
  184. priv->cur_speed = wanted;
  185. host->current_speed = 0;
  186. }
  187. }
  188. static int dw_mci_exynos_parse_dt(struct dw_mci *host)
  189. {
  190. struct dw_mci_exynos_priv_data *priv;
  191. struct device_node *np = host->dev->of_node;
  192. u32 timing[2];
  193. u32 div = 0;
  194. int idx;
  195. int ret;
  196. priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  197. if (!priv) {
  198. dev_err(host->dev, "mem alloc failed for private data\n");
  199. return -ENOMEM;
  200. }
  201. for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
  202. if (of_device_is_compatible(np, exynos_compat[idx].compatible))
  203. priv->ctrl_type = exynos_compat[idx].ctrl_type;
  204. }
  205. if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
  206. priv->ciu_div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1;
  207. else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
  208. priv->ciu_div = EXYNOS4210_FIXED_CIU_CLK_DIV - 1;
  209. else {
  210. of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
  211. priv->ciu_div = div;
  212. }
  213. ret = of_property_read_u32_array(np,
  214. "samsung,dw-mshc-sdr-timing", timing, 2);
  215. if (ret)
  216. return ret;
  217. priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
  218. ret = of_property_read_u32_array(np,
  219. "samsung,dw-mshc-ddr-timing", timing, 2);
  220. if (ret)
  221. return ret;
  222. priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
  223. host->priv = priv;
  224. return 0;
  225. }
  226. static inline u8 dw_mci_exynos_get_clksmpl(struct dw_mci *host)
  227. {
  228. return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL));
  229. }
  230. static inline void dw_mci_exynos_set_clksmpl(struct dw_mci *host, u8 sample)
  231. {
  232. u32 clksel;
  233. clksel = mci_readl(host, CLKSEL);
  234. clksel = (clksel & ~0x7) | SDMMC_CLKSEL_CCLK_SAMPLE(sample);
  235. mci_writel(host, CLKSEL, clksel);
  236. }
  237. static inline u8 dw_mci_exynos_move_next_clksmpl(struct dw_mci *host)
  238. {
  239. u32 clksel;
  240. u8 sample;
  241. clksel = mci_readl(host, CLKSEL);
  242. sample = (clksel + 1) & 0x7;
  243. clksel = (clksel & ~0x7) | sample;
  244. mci_writel(host, CLKSEL, clksel);
  245. return sample;
  246. }
  247. static s8 dw_mci_exynos_get_best_clksmpl(u8 candiates)
  248. {
  249. const u8 iter = 8;
  250. u8 __c;
  251. s8 i, loc = -1;
  252. for (i = 0; i < iter; i++) {
  253. __c = ror8(candiates, i);
  254. if ((__c & 0xc7) == 0xc7) {
  255. loc = i;
  256. goto out;
  257. }
  258. }
  259. for (i = 0; i < iter; i++) {
  260. __c = ror8(candiates, i);
  261. if ((__c & 0x83) == 0x83) {
  262. loc = i;
  263. goto out;
  264. }
  265. }
  266. out:
  267. return loc;
  268. }
  269. static int dw_mci_exynos_execute_tuning(struct dw_mci_slot *slot, u32 opcode,
  270. struct dw_mci_tuning_data *tuning_data)
  271. {
  272. struct dw_mci *host = slot->host;
  273. struct mmc_host *mmc = slot->mmc;
  274. const u8 *blk_pattern = tuning_data->blk_pattern;
  275. u8 *blk_test;
  276. unsigned int blksz = tuning_data->blksz;
  277. u8 start_smpl, smpl, candiates = 0;
  278. s8 found = -1;
  279. int ret = 0;
  280. blk_test = kmalloc(blksz, GFP_KERNEL);
  281. if (!blk_test)
  282. return -ENOMEM;
  283. start_smpl = dw_mci_exynos_get_clksmpl(host);
  284. do {
  285. struct mmc_request mrq = {NULL};
  286. struct mmc_command cmd = {0};
  287. struct mmc_command stop = {0};
  288. struct mmc_data data = {0};
  289. struct scatterlist sg;
  290. cmd.opcode = opcode;
  291. cmd.arg = 0;
  292. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  293. stop.opcode = MMC_STOP_TRANSMISSION;
  294. stop.arg = 0;
  295. stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
  296. data.blksz = blksz;
  297. data.blocks = 1;
  298. data.flags = MMC_DATA_READ;
  299. data.sg = &sg;
  300. data.sg_len = 1;
  301. sg_init_one(&sg, blk_test, blksz);
  302. mrq.cmd = &cmd;
  303. mrq.stop = &stop;
  304. mrq.data = &data;
  305. host->mrq = &mrq;
  306. mci_writel(host, TMOUT, ~0);
  307. smpl = dw_mci_exynos_move_next_clksmpl(host);
  308. mmc_wait_for_req(mmc, &mrq);
  309. if (!cmd.error && !data.error) {
  310. if (!memcmp(blk_pattern, blk_test, blksz))
  311. candiates |= (1 << smpl);
  312. } else {
  313. dev_dbg(host->dev,
  314. "Tuning error: cmd.error:%d, data.error:%d\n",
  315. cmd.error, data.error);
  316. }
  317. } while (start_smpl != smpl);
  318. found = dw_mci_exynos_get_best_clksmpl(candiates);
  319. if (found >= 0)
  320. dw_mci_exynos_set_clksmpl(host, found);
  321. else
  322. ret = -EIO;
  323. kfree(blk_test);
  324. return ret;
  325. }
  326. /* Common capabilities of Exynos4/Exynos5 SoC */
  327. static unsigned long exynos_dwmmc_caps[4] = {
  328. MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR |
  329. MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
  330. MMC_CAP_CMD23,
  331. MMC_CAP_CMD23,
  332. MMC_CAP_CMD23,
  333. };
  334. static const struct dw_mci_drv_data exynos_drv_data = {
  335. .caps = exynos_dwmmc_caps,
  336. .init = dw_mci_exynos_priv_init,
  337. .setup_clock = dw_mci_exynos_setup_clock,
  338. .prepare_command = dw_mci_exynos_prepare_command,
  339. .set_ios = dw_mci_exynos_set_ios,
  340. .parse_dt = dw_mci_exynos_parse_dt,
  341. .execute_tuning = dw_mci_exynos_execute_tuning,
  342. };
  343. static const struct of_device_id dw_mci_exynos_match[] = {
  344. { .compatible = "samsung,exynos4412-dw-mshc",
  345. .data = &exynos_drv_data, },
  346. { .compatible = "samsung,exynos5250-dw-mshc",
  347. .data = &exynos_drv_data, },
  348. { .compatible = "samsung,exynos5420-dw-mshc",
  349. .data = &exynos_drv_data, },
  350. { .compatible = "samsung,exynos5420-dw-mshc-smu",
  351. .data = &exynos_drv_data, },
  352. {},
  353. };
  354. MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
  355. static int dw_mci_exynos_probe(struct platform_device *pdev)
  356. {
  357. const struct dw_mci_drv_data *drv_data;
  358. const struct of_device_id *match;
  359. match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
  360. drv_data = match->data;
  361. return dw_mci_pltfm_register(pdev, drv_data);
  362. }
  363. const struct dev_pm_ops dw_mci_exynos_pmops = {
  364. SET_SYSTEM_SLEEP_PM_OPS(dw_mci_exynos_suspend, dw_mci_exynos_resume)
  365. .resume_noirq = dw_mci_exynos_resume_noirq,
  366. .thaw_noirq = dw_mci_exynos_resume_noirq,
  367. .restore_noirq = dw_mci_exynos_resume_noirq,
  368. };
  369. static struct platform_driver dw_mci_exynos_pltfm_driver = {
  370. .probe = dw_mci_exynos_probe,
  371. .remove = __exit_p(dw_mci_pltfm_remove),
  372. .driver = {
  373. .name = "dwmmc_exynos",
  374. .of_match_table = dw_mci_exynos_match,
  375. .pm = &dw_mci_exynos_pmops,
  376. },
  377. };
  378. module_platform_driver(dw_mci_exynos_pltfm_driver);
  379. MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
  380. MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
  381. MODULE_LICENSE("GPL v2");
  382. MODULE_ALIAS("platform:dwmmc-exynos");