sh_mobile_sdhi.c 10 KB

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