sh_mobile_sdhi.c 8.6 KB

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