sdhci-pxav3.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (C) 2010 Marvell International Ltd.
  3. * Zhangfei Gao <zhangfei.gao@marvell.com>
  4. * Kevin Wang <dwang4@marvell.com>
  5. * Mingwei Wang <mwwang@marvell.com>
  6. * Philip Rakity <prakity@marvell.com>
  7. * Mark Brown <markb@marvell.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/gpio.h>
  25. #include <linux/mmc/card.h>
  26. #include <linux/mmc/host.h>
  27. #include <linux/mmc/slot-gpio.h>
  28. #include <linux/platform_data/pxa_sdhci.h>
  29. #include <linux/slab.h>
  30. #include <linux/delay.h>
  31. #include <linux/module.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/of_gpio.h>
  35. #include "sdhci.h"
  36. #include "sdhci-pltfm.h"
  37. #define SD_CLOCK_BURST_SIZE_SETUP 0x10A
  38. #define SDCLK_SEL 0x100
  39. #define SDCLK_DELAY_SHIFT 9
  40. #define SDCLK_DELAY_MASK 0x1f
  41. #define SD_CFG_FIFO_PARAM 0x100
  42. #define SDCFG_GEN_PAD_CLK_ON (1<<6)
  43. #define SDCFG_GEN_PAD_CLK_CNT_MASK 0xFF
  44. #define SDCFG_GEN_PAD_CLK_CNT_SHIFT 24
  45. #define SD_SPI_MODE 0x108
  46. #define SD_CE_ATA_1 0x10C
  47. #define SD_CE_ATA_2 0x10E
  48. #define SDCE_MISC_INT (1<<2)
  49. #define SDCE_MISC_INT_EN (1<<1)
  50. static void pxav3_set_private_registers(struct sdhci_host *host, u8 mask)
  51. {
  52. struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  53. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  54. if (mask == SDHCI_RESET_ALL) {
  55. /*
  56. * tune timing of read data/command when crc error happen
  57. * no performance impact
  58. */
  59. if (pdata && 0 != pdata->clk_delay_cycles) {
  60. u16 tmp;
  61. tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  62. tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
  63. << SDCLK_DELAY_SHIFT;
  64. tmp |= SDCLK_SEL;
  65. writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  66. }
  67. }
  68. }
  69. #define MAX_WAIT_COUNT 5
  70. static void pxav3_gen_init_74_clocks(struct sdhci_host *host, u8 power_mode)
  71. {
  72. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  73. struct sdhci_pxa *pxa = pltfm_host->priv;
  74. u16 tmp;
  75. int count;
  76. if (pxa->power_mode == MMC_POWER_UP
  77. && power_mode == MMC_POWER_ON) {
  78. dev_dbg(mmc_dev(host->mmc),
  79. "%s: slot->power_mode = %d,"
  80. "ios->power_mode = %d\n",
  81. __func__,
  82. pxa->power_mode,
  83. power_mode);
  84. /* set we want notice of when 74 clocks are sent */
  85. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  86. tmp |= SDCE_MISC_INT_EN;
  87. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  88. /* start sending the 74 clocks */
  89. tmp = readw(host->ioaddr + SD_CFG_FIFO_PARAM);
  90. tmp |= SDCFG_GEN_PAD_CLK_ON;
  91. writew(tmp, host->ioaddr + SD_CFG_FIFO_PARAM);
  92. /* slowest speed is about 100KHz or 10usec per clock */
  93. udelay(740);
  94. count = 0;
  95. while (count++ < MAX_WAIT_COUNT) {
  96. if ((readw(host->ioaddr + SD_CE_ATA_2)
  97. & SDCE_MISC_INT) == 0)
  98. break;
  99. udelay(10);
  100. }
  101. if (count == MAX_WAIT_COUNT)
  102. dev_warn(mmc_dev(host->mmc), "74 clock interrupt not cleared\n");
  103. /* clear the interrupt bit if posted */
  104. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  105. tmp |= SDCE_MISC_INT;
  106. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  107. }
  108. pxa->power_mode = power_mode;
  109. }
  110. static int pxav3_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
  111. {
  112. u16 ctrl_2;
  113. /*
  114. * Set V18_EN -- UHS modes do not work without this.
  115. * does not change signaling voltage
  116. */
  117. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  118. /* Select Bus Speed Mode for host */
  119. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  120. switch (uhs) {
  121. case MMC_TIMING_UHS_SDR12:
  122. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  123. break;
  124. case MMC_TIMING_UHS_SDR25:
  125. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  126. break;
  127. case MMC_TIMING_UHS_SDR50:
  128. ctrl_2 |= SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180;
  129. break;
  130. case MMC_TIMING_UHS_SDR104:
  131. ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_VDD_180;
  132. break;
  133. case MMC_TIMING_UHS_DDR50:
  134. ctrl_2 |= SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180;
  135. break;
  136. }
  137. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  138. dev_dbg(mmc_dev(host->mmc),
  139. "%s uhs = %d, ctrl_2 = %04X\n",
  140. __func__, uhs, ctrl_2);
  141. return 0;
  142. }
  143. static u32 pxav3_get_max_clock(struct sdhci_host *host)
  144. {
  145. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  146. return clk_get_rate(pltfm_host->clk);
  147. }
  148. static struct sdhci_ops pxav3_sdhci_ops = {
  149. .platform_reset_exit = pxav3_set_private_registers,
  150. .set_uhs_signaling = pxav3_set_uhs_signaling,
  151. .platform_send_init_74_clocks = pxav3_gen_init_74_clocks,
  152. .get_max_clock = pxav3_get_max_clock,
  153. };
  154. #ifdef CONFIG_OF
  155. static const struct of_device_id sdhci_pxav3_of_match[] = {
  156. {
  157. .compatible = "mrvl,pxav3-mmc",
  158. },
  159. {},
  160. };
  161. MODULE_DEVICE_TABLE(of, sdhci_pxav3_of_match);
  162. static struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
  163. {
  164. struct sdhci_pxa_platdata *pdata;
  165. struct device_node *np = dev->of_node;
  166. u32 bus_width;
  167. u32 clk_delay_cycles;
  168. enum of_gpio_flags gpio_flags;
  169. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  170. if (!pdata)
  171. return NULL;
  172. if (of_find_property(np, "non-removable", NULL))
  173. pdata->flags |= PXA_FLAG_CARD_PERMANENT;
  174. of_property_read_u32(np, "bus-width", &bus_width);
  175. if (bus_width == 8)
  176. pdata->flags |= PXA_FLAG_SD_8_BIT_CAPABLE_SLOT;
  177. of_property_read_u32(np, "mrvl,clk-delay-cycles", &clk_delay_cycles);
  178. if (clk_delay_cycles > 0)
  179. pdata->clk_delay_cycles = clk_delay_cycles;
  180. pdata->ext_cd_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &gpio_flags);
  181. if (gpio_flags != OF_GPIO_ACTIVE_LOW)
  182. pdata->host_caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  183. return pdata;
  184. }
  185. #else
  186. static inline struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
  187. {
  188. return NULL;
  189. }
  190. #endif
  191. static int sdhci_pxav3_probe(struct platform_device *pdev)
  192. {
  193. struct sdhci_pltfm_host *pltfm_host;
  194. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  195. struct device *dev = &pdev->dev;
  196. struct sdhci_host *host = NULL;
  197. struct sdhci_pxa *pxa = NULL;
  198. const struct of_device_id *match;
  199. int ret;
  200. struct clk *clk;
  201. pxa = kzalloc(sizeof(struct sdhci_pxa), GFP_KERNEL);
  202. if (!pxa)
  203. return -ENOMEM;
  204. host = sdhci_pltfm_init(pdev, NULL);
  205. if (IS_ERR(host)) {
  206. kfree(pxa);
  207. return PTR_ERR(host);
  208. }
  209. pltfm_host = sdhci_priv(host);
  210. pltfm_host->priv = pxa;
  211. clk = clk_get(dev, NULL);
  212. if (IS_ERR(clk)) {
  213. dev_err(dev, "failed to get io clock\n");
  214. ret = PTR_ERR(clk);
  215. goto err_clk_get;
  216. }
  217. pltfm_host->clk = clk;
  218. clk_prepare_enable(clk);
  219. host->quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
  220. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  221. | SDHCI_QUIRK_32BIT_ADMA_SIZE
  222. | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
  223. /* enable 1/8V DDR capable */
  224. host->mmc->caps |= MMC_CAP_1_8V_DDR;
  225. match = of_match_device(of_match_ptr(sdhci_pxav3_of_match), &pdev->dev);
  226. if (match)
  227. pdata = pxav3_get_mmc_pdata(dev);
  228. if (pdata) {
  229. if (pdata->flags & PXA_FLAG_CARD_PERMANENT) {
  230. /* on-chip device */
  231. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  232. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  233. }
  234. /* If slot design supports 8 bit data, indicate this to MMC. */
  235. if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
  236. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  237. if (pdata->quirks)
  238. host->quirks |= pdata->quirks;
  239. if (pdata->quirks2)
  240. host->quirks2 |= pdata->quirks2;
  241. if (pdata->host_caps)
  242. host->mmc->caps |= pdata->host_caps;
  243. if (pdata->host_caps2)
  244. host->mmc->caps2 |= pdata->host_caps2;
  245. if (pdata->pm_caps)
  246. host->mmc->pm_caps |= pdata->pm_caps;
  247. if (gpio_is_valid(pdata->ext_cd_gpio)) {
  248. ret = mmc_gpio_request_cd(host->mmc, pdata->ext_cd_gpio);
  249. if (ret) {
  250. dev_err(mmc_dev(host->mmc),
  251. "failed to allocate card detect gpio\n");
  252. goto err_cd_req;
  253. }
  254. }
  255. }
  256. host->ops = &pxav3_sdhci_ops;
  257. sdhci_get_of_property(pdev);
  258. ret = sdhci_add_host(host);
  259. if (ret) {
  260. dev_err(&pdev->dev, "failed to add host\n");
  261. goto err_add_host;
  262. }
  263. platform_set_drvdata(pdev, host);
  264. return 0;
  265. err_add_host:
  266. clk_disable_unprepare(clk);
  267. clk_put(clk);
  268. mmc_gpio_free_cd(host->mmc);
  269. err_cd_req:
  270. err_clk_get:
  271. sdhci_pltfm_free(pdev);
  272. kfree(pxa);
  273. return ret;
  274. }
  275. static int sdhci_pxav3_remove(struct platform_device *pdev)
  276. {
  277. struct sdhci_host *host = platform_get_drvdata(pdev);
  278. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  279. struct sdhci_pxa *pxa = pltfm_host->priv;
  280. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  281. sdhci_remove_host(host, 1);
  282. clk_disable_unprepare(pltfm_host->clk);
  283. clk_put(pltfm_host->clk);
  284. if (gpio_is_valid(pdata->ext_cd_gpio))
  285. mmc_gpio_free_cd(host->mmc);
  286. sdhci_pltfm_free(pdev);
  287. kfree(pxa);
  288. platform_set_drvdata(pdev, NULL);
  289. return 0;
  290. }
  291. static struct platform_driver sdhci_pxav3_driver = {
  292. .driver = {
  293. .name = "sdhci-pxav3",
  294. #ifdef CONFIG_OF
  295. .of_match_table = sdhci_pxav3_of_match,
  296. #endif
  297. .owner = THIS_MODULE,
  298. .pm = SDHCI_PLTFM_PMOPS,
  299. },
  300. .probe = sdhci_pxav3_probe,
  301. .remove = sdhci_pxav3_remove,
  302. };
  303. module_platform_driver(sdhci_pxav3_driver);
  304. MODULE_DESCRIPTION("SDHCI driver for pxav3");
  305. MODULE_AUTHOR("Marvell International Ltd.");
  306. MODULE_LICENSE("GPL v2");