sh_mobile_sdhi.c 8.2 KB

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