sdhci-tegra.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (C) 2010 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/gpio.h>
  24. #include <linux/mmc/card.h>
  25. #include <linux/mmc/host.h>
  26. #include <asm/gpio.h>
  27. #include <linux/platform_data/mmc-sdhci-tegra.h>
  28. #include "sdhci-pltfm.h"
  29. /* Tegra SDHOST controller vendor register definitions */
  30. #define SDHCI_TEGRA_VENDOR_MISC_CTRL 0x120
  31. #define SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300 0x20
  32. #define NVQUIRK_FORCE_SDHCI_SPEC_200 BIT(0)
  33. #define NVQUIRK_ENABLE_BLOCK_GAP_DET BIT(1)
  34. #define NVQUIRK_ENABLE_SDHCI_SPEC_300 BIT(2)
  35. struct sdhci_tegra_soc_data {
  36. struct sdhci_pltfm_data *pdata;
  37. u32 nvquirks;
  38. };
  39. struct sdhci_tegra {
  40. const struct tegra_sdhci_platform_data *plat;
  41. const struct sdhci_tegra_soc_data *soc_data;
  42. };
  43. static u32 tegra_sdhci_readl(struct sdhci_host *host, int reg)
  44. {
  45. u32 val;
  46. if (unlikely(reg == SDHCI_PRESENT_STATE)) {
  47. /* Use wp_gpio here instead? */
  48. val = readl(host->ioaddr + reg);
  49. return val | SDHCI_WRITE_PROTECT;
  50. }
  51. return readl(host->ioaddr + reg);
  52. }
  53. static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
  54. {
  55. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  56. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  57. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  58. if (unlikely((soc_data->nvquirks & NVQUIRK_FORCE_SDHCI_SPEC_200) &&
  59. (reg == SDHCI_HOST_VERSION))) {
  60. /* Erratum: Version register is invalid in HW. */
  61. return SDHCI_SPEC_200;
  62. }
  63. return readw(host->ioaddr + reg);
  64. }
  65. static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
  66. {
  67. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  68. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  69. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  70. /* Seems like we're getting spurious timeout and crc errors, so
  71. * disable signalling of them. In case of real errors software
  72. * timers should take care of eventually detecting them.
  73. */
  74. if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
  75. val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
  76. writel(val, host->ioaddr + reg);
  77. if (unlikely((soc_data->nvquirks & NVQUIRK_ENABLE_BLOCK_GAP_DET) &&
  78. (reg == SDHCI_INT_ENABLE))) {
  79. /* Erratum: Must enable block gap interrupt detection */
  80. u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  81. if (val & SDHCI_INT_CARD_INT)
  82. gap_ctrl |= 0x8;
  83. else
  84. gap_ctrl &= ~0x8;
  85. writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  86. }
  87. }
  88. static unsigned int tegra_sdhci_get_ro(struct sdhci_host *host)
  89. {
  90. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  91. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  92. const struct tegra_sdhci_platform_data *plat = tegra_host->plat;
  93. if (!gpio_is_valid(plat->wp_gpio))
  94. return -1;
  95. return gpio_get_value(plat->wp_gpio);
  96. }
  97. static irqreturn_t carddetect_irq(int irq, void *data)
  98. {
  99. struct sdhci_host *sdhost = (struct sdhci_host *)data;
  100. tasklet_schedule(&sdhost->card_tasklet);
  101. return IRQ_HANDLED;
  102. };
  103. static void tegra_sdhci_reset_exit(struct sdhci_host *host, u8 mask)
  104. {
  105. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  106. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  107. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  108. if (!(mask & SDHCI_RESET_ALL))
  109. return;
  110. /* Erratum: Enable SDHCI spec v3.00 support */
  111. if (soc_data->nvquirks & NVQUIRK_ENABLE_SDHCI_SPEC_300) {
  112. u32 misc_ctrl;
  113. misc_ctrl = sdhci_readb(host, SDHCI_TEGRA_VENDOR_MISC_CTRL);
  114. misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300;
  115. sdhci_writeb(host, misc_ctrl, SDHCI_TEGRA_VENDOR_MISC_CTRL);
  116. }
  117. }
  118. static int tegra_sdhci_8bit(struct sdhci_host *host, int bus_width)
  119. {
  120. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  121. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  122. const struct tegra_sdhci_platform_data *plat = tegra_host->plat;
  123. u32 ctrl;
  124. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  125. if (plat->is_8bit && bus_width == MMC_BUS_WIDTH_8) {
  126. ctrl &= ~SDHCI_CTRL_4BITBUS;
  127. ctrl |= SDHCI_CTRL_8BITBUS;
  128. } else {
  129. ctrl &= ~SDHCI_CTRL_8BITBUS;
  130. if (bus_width == MMC_BUS_WIDTH_4)
  131. ctrl |= SDHCI_CTRL_4BITBUS;
  132. else
  133. ctrl &= ~SDHCI_CTRL_4BITBUS;
  134. }
  135. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  136. return 0;
  137. }
  138. static struct sdhci_ops tegra_sdhci_ops = {
  139. .get_ro = tegra_sdhci_get_ro,
  140. .read_l = tegra_sdhci_readl,
  141. .read_w = tegra_sdhci_readw,
  142. .write_l = tegra_sdhci_writel,
  143. .platform_8bit_width = tegra_sdhci_8bit,
  144. .platform_reset_exit = tegra_sdhci_reset_exit,
  145. };
  146. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  147. static struct sdhci_pltfm_data sdhci_tegra20_pdata = {
  148. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  149. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  150. SDHCI_QUIRK_NO_HISPD_BIT |
  151. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
  152. .ops = &tegra_sdhci_ops,
  153. };
  154. static struct sdhci_tegra_soc_data soc_data_tegra20 = {
  155. .pdata = &sdhci_tegra20_pdata,
  156. .nvquirks = NVQUIRK_FORCE_SDHCI_SPEC_200 |
  157. NVQUIRK_ENABLE_BLOCK_GAP_DET,
  158. };
  159. #endif
  160. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  161. static struct sdhci_pltfm_data sdhci_tegra30_pdata = {
  162. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  163. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  164. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  165. SDHCI_QUIRK_NO_HISPD_BIT |
  166. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
  167. .ops = &tegra_sdhci_ops,
  168. };
  169. static struct sdhci_tegra_soc_data soc_data_tegra30 = {
  170. .pdata = &sdhci_tegra30_pdata,
  171. .nvquirks = NVQUIRK_ENABLE_SDHCI_SPEC_300,
  172. };
  173. #endif
  174. static const struct of_device_id sdhci_tegra_dt_match[] = {
  175. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  176. { .compatible = "nvidia,tegra30-sdhci", .data = &soc_data_tegra30 },
  177. #endif
  178. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  179. { .compatible = "nvidia,tegra20-sdhci", .data = &soc_data_tegra20 },
  180. #endif
  181. {}
  182. };
  183. MODULE_DEVICE_TABLE(of, sdhci_dt_ids);
  184. static struct tegra_sdhci_platform_data *sdhci_tegra_dt_parse_pdata(
  185. struct platform_device *pdev)
  186. {
  187. struct tegra_sdhci_platform_data *plat;
  188. struct device_node *np = pdev->dev.of_node;
  189. u32 bus_width;
  190. if (!np)
  191. return NULL;
  192. plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
  193. if (!plat) {
  194. dev_err(&pdev->dev, "Can't allocate platform data\n");
  195. return NULL;
  196. }
  197. plat->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  198. plat->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
  199. plat->power_gpio = of_get_named_gpio(np, "power-gpios", 0);
  200. if (of_property_read_u32(np, "bus-width", &bus_width) == 0 &&
  201. bus_width == 8)
  202. plat->is_8bit = 1;
  203. return plat;
  204. }
  205. static int sdhci_tegra_probe(struct platform_device *pdev)
  206. {
  207. const struct of_device_id *match;
  208. const struct sdhci_tegra_soc_data *soc_data;
  209. struct sdhci_host *host;
  210. struct sdhci_pltfm_host *pltfm_host;
  211. struct tegra_sdhci_platform_data *plat;
  212. struct sdhci_tegra *tegra_host;
  213. struct clk *clk;
  214. int rc;
  215. match = of_match_device(sdhci_tegra_dt_match, &pdev->dev);
  216. if (!match)
  217. return -EINVAL;
  218. soc_data = match->data;
  219. host = sdhci_pltfm_init(pdev, soc_data->pdata);
  220. if (IS_ERR(host))
  221. return PTR_ERR(host);
  222. pltfm_host = sdhci_priv(host);
  223. plat = pdev->dev.platform_data;
  224. if (plat == NULL)
  225. plat = sdhci_tegra_dt_parse_pdata(pdev);
  226. if (plat == NULL) {
  227. dev_err(mmc_dev(host->mmc), "missing platform data\n");
  228. rc = -ENXIO;
  229. goto err_no_plat;
  230. }
  231. tegra_host = devm_kzalloc(&pdev->dev, sizeof(*tegra_host), GFP_KERNEL);
  232. if (!tegra_host) {
  233. dev_err(mmc_dev(host->mmc), "failed to allocate tegra_host\n");
  234. rc = -ENOMEM;
  235. goto err_no_plat;
  236. }
  237. tegra_host->plat = plat;
  238. tegra_host->soc_data = soc_data;
  239. pltfm_host->priv = tegra_host;
  240. if (gpio_is_valid(plat->power_gpio)) {
  241. rc = gpio_request(plat->power_gpio, "sdhci_power");
  242. if (rc) {
  243. dev_err(mmc_dev(host->mmc),
  244. "failed to allocate power gpio\n");
  245. goto err_power_req;
  246. }
  247. gpio_direction_output(plat->power_gpio, 1);
  248. }
  249. if (gpio_is_valid(plat->cd_gpio)) {
  250. rc = gpio_request(plat->cd_gpio, "sdhci_cd");
  251. if (rc) {
  252. dev_err(mmc_dev(host->mmc),
  253. "failed to allocate cd gpio\n");
  254. goto err_cd_req;
  255. }
  256. gpio_direction_input(plat->cd_gpio);
  257. rc = request_irq(gpio_to_irq(plat->cd_gpio), carddetect_irq,
  258. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  259. mmc_hostname(host->mmc), host);
  260. if (rc) {
  261. dev_err(mmc_dev(host->mmc), "request irq error\n");
  262. goto err_cd_irq_req;
  263. }
  264. }
  265. if (gpio_is_valid(plat->wp_gpio)) {
  266. rc = gpio_request(plat->wp_gpio, "sdhci_wp");
  267. if (rc) {
  268. dev_err(mmc_dev(host->mmc),
  269. "failed to allocate wp gpio\n");
  270. goto err_wp_req;
  271. }
  272. gpio_direction_input(plat->wp_gpio);
  273. }
  274. clk = clk_get(mmc_dev(host->mmc), NULL);
  275. if (IS_ERR(clk)) {
  276. dev_err(mmc_dev(host->mmc), "clk err\n");
  277. rc = PTR_ERR(clk);
  278. goto err_clk_get;
  279. }
  280. clk_prepare_enable(clk);
  281. pltfm_host->clk = clk;
  282. host->mmc->pm_caps = plat->pm_flags;
  283. if (plat->is_8bit)
  284. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  285. rc = sdhci_add_host(host);
  286. if (rc)
  287. goto err_add_host;
  288. return 0;
  289. err_add_host:
  290. clk_disable_unprepare(pltfm_host->clk);
  291. clk_put(pltfm_host->clk);
  292. err_clk_get:
  293. if (gpio_is_valid(plat->wp_gpio))
  294. gpio_free(plat->wp_gpio);
  295. err_wp_req:
  296. if (gpio_is_valid(plat->cd_gpio))
  297. free_irq(gpio_to_irq(plat->cd_gpio), host);
  298. err_cd_irq_req:
  299. if (gpio_is_valid(plat->cd_gpio))
  300. gpio_free(plat->cd_gpio);
  301. err_cd_req:
  302. if (gpio_is_valid(plat->power_gpio))
  303. gpio_free(plat->power_gpio);
  304. err_power_req:
  305. err_no_plat:
  306. sdhci_pltfm_free(pdev);
  307. return rc;
  308. }
  309. static int sdhci_tegra_remove(struct platform_device *pdev)
  310. {
  311. struct sdhci_host *host = platform_get_drvdata(pdev);
  312. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  313. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  314. const struct tegra_sdhci_platform_data *plat = tegra_host->plat;
  315. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  316. sdhci_remove_host(host, dead);
  317. if (gpio_is_valid(plat->wp_gpio))
  318. gpio_free(plat->wp_gpio);
  319. if (gpio_is_valid(plat->cd_gpio)) {
  320. free_irq(gpio_to_irq(plat->cd_gpio), host);
  321. gpio_free(plat->cd_gpio);
  322. }
  323. if (gpio_is_valid(plat->power_gpio))
  324. gpio_free(plat->power_gpio);
  325. clk_disable_unprepare(pltfm_host->clk);
  326. clk_put(pltfm_host->clk);
  327. sdhci_pltfm_free(pdev);
  328. return 0;
  329. }
  330. static struct platform_driver sdhci_tegra_driver = {
  331. .driver = {
  332. .name = "sdhci-tegra",
  333. .owner = THIS_MODULE,
  334. .of_match_table = sdhci_tegra_dt_match,
  335. .pm = SDHCI_PLTFM_PMOPS,
  336. },
  337. .probe = sdhci_tegra_probe,
  338. .remove = sdhci_tegra_remove,
  339. };
  340. module_platform_driver(sdhci_tegra_driver);
  341. MODULE_DESCRIPTION("SDHCI driver for Tegra");
  342. MODULE_AUTHOR("Google, Inc.");
  343. MODULE_LICENSE("GPL v2");