sh_mobile_sdhi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * SuperH Mobile SDHI
  3. *
  4. * Copyright (C) 2009 Magnus Damm
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Based on "Compaq ASIC3 support":
  11. *
  12. * Copyright 2001 Compaq Computer Corporation.
  13. * Copyright 2004-2005 Phil Blundell
  14. * Copyright 2007-2008 OpenedHand Ltd.
  15. *
  16. * Authors: Phil Blundell <pb@handhelds.org>,
  17. * Samuel Ortiz <sameo@openedhand.com>
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/clk.h>
  22. #include <linux/slab.h>
  23. #include <linux/mod_devicetable.h>
  24. #include <linux/module.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mmc/host.h>
  28. #include <linux/mmc/sh_mobile_sdhi.h>
  29. #include <linux/mfd/tmio.h>
  30. #include <linux/sh_dma.h>
  31. #include <linux/delay.h>
  32. #include "tmio_mmc.h"
  33. #define EXT_ACC 0xe4
  34. struct sh_mobile_sdhi_of_data {
  35. unsigned long tmio_flags;
  36. unsigned long capabilities;
  37. unsigned long capabilities2;
  38. };
  39. static const struct sh_mobile_sdhi_of_data sh_mobile_sdhi_of_cfg[] = {
  40. {
  41. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT,
  42. },
  43. };
  44. static const struct sh_mobile_sdhi_of_data of_rcar_gen1_compatible = {
  45. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE,
  46. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
  47. };
  48. static const struct sh_mobile_sdhi_of_data of_rcar_gen2_compatible = {
  49. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE,
  50. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
  51. .capabilities2 = MMC_CAP2_NO_MULTI_READ,
  52. };
  53. static const struct of_device_id sh_mobile_sdhi_of_match[] = {
  54. { .compatible = "renesas,sdhi-shmobile" },
  55. { .compatible = "renesas,sdhi-sh7372" },
  56. { .compatible = "renesas,sdhi-sh73a0", .data = &sh_mobile_sdhi_of_cfg[0], },
  57. { .compatible = "renesas,sdhi-r8a73a4", .data = &sh_mobile_sdhi_of_cfg[0], },
  58. { .compatible = "renesas,sdhi-r8a7740", .data = &sh_mobile_sdhi_of_cfg[0], },
  59. { .compatible = "renesas,sdhi-r8a7778", .data = &of_rcar_gen1_compatible, },
  60. { .compatible = "renesas,sdhi-r8a7779", .data = &of_rcar_gen1_compatible, },
  61. { .compatible = "renesas,sdhi-r8a7790", .data = &of_rcar_gen2_compatible, },
  62. { .compatible = "renesas,sdhi-r8a7791", .data = &of_rcar_gen2_compatible, },
  63. {},
  64. };
  65. MODULE_DEVICE_TABLE(of, sh_mobile_sdhi_of_match);
  66. struct sh_mobile_sdhi {
  67. struct clk *clk;
  68. struct tmio_mmc_data mmc_data;
  69. struct tmio_mmc_dma dma_priv;
  70. };
  71. static int sh_mobile_sdhi_clk_enable(struct platform_device *pdev, unsigned int *f)
  72. {
  73. struct mmc_host *mmc = platform_get_drvdata(pdev);
  74. struct tmio_mmc_host *host = mmc_priv(mmc);
  75. struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data);
  76. int ret = clk_prepare_enable(priv->clk);
  77. if (ret < 0)
  78. return ret;
  79. *f = clk_get_rate(priv->clk);
  80. return 0;
  81. }
  82. static void sh_mobile_sdhi_clk_disable(struct platform_device *pdev)
  83. {
  84. struct mmc_host *mmc = platform_get_drvdata(pdev);
  85. struct tmio_mmc_host *host = mmc_priv(mmc);
  86. struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data);
  87. clk_disable_unprepare(priv->clk);
  88. }
  89. static int sh_mobile_sdhi_wait_idle(struct tmio_mmc_host *host)
  90. {
  91. int timeout = 1000;
  92. while (--timeout && !(sd_ctrl_read16(host, CTL_STATUS2) & (1 << 13)))
  93. udelay(1);
  94. if (!timeout) {
  95. dev_warn(host->pdata->dev, "timeout waiting for SD bus idle\n");
  96. return -EBUSY;
  97. }
  98. return 0;
  99. }
  100. static int sh_mobile_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
  101. {
  102. switch (addr)
  103. {
  104. case CTL_SD_CMD:
  105. case CTL_STOP_INTERNAL_ACTION:
  106. case CTL_XFER_BLK_COUNT:
  107. case CTL_SD_CARD_CLK_CTL:
  108. case CTL_SD_XFER_LEN:
  109. case CTL_SD_MEM_CARD_OPT:
  110. case CTL_TRANSACTION_CTL:
  111. case CTL_DMA_ENABLE:
  112. return sh_mobile_sdhi_wait_idle(host);
  113. }
  114. return 0;
  115. }
  116. static void sh_mobile_sdhi_cd_wakeup(const struct platform_device *pdev)
  117. {
  118. mmc_detect_change(platform_get_drvdata(pdev), msecs_to_jiffies(100));
  119. }
  120. static const struct sh_mobile_sdhi_ops sdhi_ops = {
  121. .cd_wakeup = sh_mobile_sdhi_cd_wakeup,
  122. };
  123. static int sh_mobile_sdhi_probe(struct platform_device *pdev)
  124. {
  125. const struct of_device_id *of_id =
  126. of_match_device(sh_mobile_sdhi_of_match, &pdev->dev);
  127. struct sh_mobile_sdhi *priv;
  128. struct tmio_mmc_data *mmc_data;
  129. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  130. struct tmio_mmc_host *host;
  131. struct resource *res;
  132. int irq, ret, i = 0;
  133. bool multiplexed_isr = true;
  134. struct tmio_mmc_dma *dma_priv;
  135. u16 ver;
  136. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  137. if (!res)
  138. return -EINVAL;
  139. priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  140. if (priv == NULL) {
  141. dev_err(&pdev->dev, "kzalloc failed\n");
  142. return -ENOMEM;
  143. }
  144. mmc_data = &priv->mmc_data;
  145. dma_priv = &priv->dma_priv;
  146. if (p) {
  147. if (p->init) {
  148. ret = p->init(pdev, &sdhi_ops);
  149. if (ret)
  150. return ret;
  151. }
  152. }
  153. priv->clk = devm_clk_get(&pdev->dev, NULL);
  154. if (IS_ERR(priv->clk)) {
  155. ret = PTR_ERR(priv->clk);
  156. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  157. goto eclkget;
  158. }
  159. mmc_data->clk_enable = sh_mobile_sdhi_clk_enable;
  160. mmc_data->clk_disable = sh_mobile_sdhi_clk_disable;
  161. mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED;
  162. mmc_data->write16_hook = sh_mobile_sdhi_write16_hook;
  163. if (p) {
  164. mmc_data->flags = p->tmio_flags;
  165. mmc_data->ocr_mask = p->tmio_ocr_mask;
  166. mmc_data->capabilities |= p->tmio_caps;
  167. mmc_data->capabilities2 |= p->tmio_caps2;
  168. mmc_data->cd_gpio = p->cd_gpio;
  169. if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0) {
  170. /*
  171. * Yes, we have to provide slave IDs twice to TMIO:
  172. * once as a filter parameter and once for channel
  173. * configuration as an explicit slave ID
  174. */
  175. dma_priv->chan_priv_tx = (void *)p->dma_slave_tx;
  176. dma_priv->chan_priv_rx = (void *)p->dma_slave_rx;
  177. dma_priv->slave_id_tx = p->dma_slave_tx;
  178. dma_priv->slave_id_rx = p->dma_slave_rx;
  179. }
  180. }
  181. dma_priv->alignment_shift = 1; /* 2-byte alignment */
  182. dma_priv->filter = shdma_chan_filter;
  183. mmc_data->dma = dma_priv;
  184. /*
  185. * All SDHI blocks support 2-byte and larger block sizes in 4-bit
  186. * bus width mode.
  187. */
  188. mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
  189. /*
  190. * All SDHI blocks support SDIO IRQ signalling.
  191. */
  192. mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
  193. if (of_id && of_id->data) {
  194. const struct sh_mobile_sdhi_of_data *of_data = of_id->data;
  195. mmc_data->flags |= of_data->tmio_flags;
  196. mmc_data->capabilities |= of_data->capabilities;
  197. mmc_data->capabilities2 |= of_data->capabilities2;
  198. }
  199. /* SD control register space size is 0x100, 0x200 for bus_shift=1 */
  200. mmc_data->bus_shift = resource_size(res) >> 9;
  201. ret = tmio_mmc_host_probe(&host, pdev, mmc_data);
  202. if (ret < 0)
  203. goto eprobe;
  204. /*
  205. * FIXME:
  206. * this Workaround can be more clever method
  207. */
  208. ver = sd_ctrl_read16(host, CTL_VERSION);
  209. if (ver == 0xCB0D)
  210. sd_ctrl_write16(host, EXT_ACC, 1);
  211. /*
  212. * Allow one or more specific (named) ISRs or
  213. * one or more multiplexed (un-named) ISRs.
  214. */
  215. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_CARD_DETECT);
  216. if (irq >= 0) {
  217. multiplexed_isr = false;
  218. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_card_detect_irq, 0,
  219. dev_name(&pdev->dev), host);
  220. if (ret)
  221. goto eirq;
  222. }
  223. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDIO);
  224. if (irq >= 0) {
  225. multiplexed_isr = false;
  226. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdio_irq, 0,
  227. dev_name(&pdev->dev), host);
  228. if (ret)
  229. goto eirq;
  230. }
  231. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDCARD);
  232. if (irq >= 0) {
  233. multiplexed_isr = false;
  234. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdcard_irq, 0,
  235. dev_name(&pdev->dev), host);
  236. if (ret)
  237. goto eirq;
  238. } else if (!multiplexed_isr) {
  239. dev_err(&pdev->dev,
  240. "Principal SD-card IRQ is missing among named interrupts\n");
  241. ret = irq;
  242. goto eirq;
  243. }
  244. if (multiplexed_isr) {
  245. while (1) {
  246. irq = platform_get_irq(pdev, i);
  247. if (irq < 0)
  248. break;
  249. i++;
  250. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
  251. dev_name(&pdev->dev), host);
  252. if (ret)
  253. goto eirq;
  254. }
  255. /* There must be at least one IRQ source */
  256. if (!i) {
  257. ret = irq;
  258. goto eirq;
  259. }
  260. }
  261. dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n",
  262. mmc_hostname(host->mmc), (unsigned long)
  263. (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
  264. host->mmc->f_max / 1000000);
  265. return ret;
  266. eirq:
  267. tmio_mmc_host_remove(host);
  268. eprobe:
  269. eclkget:
  270. if (p && p->cleanup)
  271. p->cleanup(pdev);
  272. return ret;
  273. }
  274. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  275. {
  276. struct mmc_host *mmc = platform_get_drvdata(pdev);
  277. struct tmio_mmc_host *host = mmc_priv(mmc);
  278. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  279. tmio_mmc_host_remove(host);
  280. if (p && p->cleanup)
  281. p->cleanup(pdev);
  282. return 0;
  283. }
  284. static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
  285. SET_SYSTEM_SLEEP_PM_OPS(tmio_mmc_host_suspend, tmio_mmc_host_resume)
  286. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  287. tmio_mmc_host_runtime_resume,
  288. NULL)
  289. };
  290. static struct platform_driver sh_mobile_sdhi_driver = {
  291. .driver = {
  292. .name = "sh_mobile_sdhi",
  293. .owner = THIS_MODULE,
  294. .pm = &tmio_mmc_dev_pm_ops,
  295. .of_match_table = sh_mobile_sdhi_of_match,
  296. },
  297. .probe = sh_mobile_sdhi_probe,
  298. .remove = sh_mobile_sdhi_remove,
  299. };
  300. module_platform_driver(sh_mobile_sdhi_driver);
  301. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  302. MODULE_AUTHOR("Magnus Damm");
  303. MODULE_LICENSE("GPL v2");
  304. MODULE_ALIAS("platform:sh_mobile_sdhi");