sh_mobile_sdhi.c 8.0 KB

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