sh_mobile_sdhi.c 10 KB

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