sdhci-tegra.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "sdhci-pltfm.h"
  28. /* Tegra SDHOST controller vendor register definitions */
  29. #define SDHCI_TEGRA_VENDOR_MISC_CTRL 0x120
  30. #define SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300 0x20
  31. #define NVQUIRK_FORCE_SDHCI_SPEC_200 BIT(0)
  32. #define NVQUIRK_ENABLE_BLOCK_GAP_DET BIT(1)
  33. #define NVQUIRK_ENABLE_SDHCI_SPEC_300 BIT(2)
  34. struct sdhci_tegra_soc_data {
  35. struct sdhci_pltfm_data *pdata;
  36. u32 nvquirks;
  37. };
  38. struct sdhci_tegra {
  39. const struct sdhci_tegra_soc_data *soc_data;
  40. int cd_gpio;
  41. int wp_gpio;
  42. int power_gpio;
  43. int is_8bit;
  44. };
  45. static u32 tegra_sdhci_readl(struct sdhci_host *host, int reg)
  46. {
  47. u32 val;
  48. if (unlikely(reg == SDHCI_PRESENT_STATE)) {
  49. /* Use wp_gpio here instead? */
  50. val = readl(host->ioaddr + reg);
  51. return val | SDHCI_WRITE_PROTECT;
  52. }
  53. return readl(host->ioaddr + reg);
  54. }
  55. static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
  56. {
  57. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  58. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  59. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  60. if (unlikely((soc_data->nvquirks & NVQUIRK_FORCE_SDHCI_SPEC_200) &&
  61. (reg == SDHCI_HOST_VERSION))) {
  62. /* Erratum: Version register is invalid in HW. */
  63. return SDHCI_SPEC_200;
  64. }
  65. return readw(host->ioaddr + reg);
  66. }
  67. static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
  68. {
  69. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  70. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  71. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  72. /* Seems like we're getting spurious timeout and crc errors, so
  73. * disable signalling of them. In case of real errors software
  74. * timers should take care of eventually detecting them.
  75. */
  76. if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
  77. val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
  78. writel(val, host->ioaddr + reg);
  79. if (unlikely((soc_data->nvquirks & NVQUIRK_ENABLE_BLOCK_GAP_DET) &&
  80. (reg == SDHCI_INT_ENABLE))) {
  81. /* Erratum: Must enable block gap interrupt detection */
  82. u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  83. if (val & SDHCI_INT_CARD_INT)
  84. gap_ctrl |= 0x8;
  85. else
  86. gap_ctrl &= ~0x8;
  87. writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  88. }
  89. }
  90. static unsigned int tegra_sdhci_get_ro(struct sdhci_host *host)
  91. {
  92. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  93. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  94. if (!gpio_is_valid(tegra_host->wp_gpio))
  95. return -1;
  96. return gpio_get_value(tegra_host->wp_gpio);
  97. }
  98. static irqreturn_t carddetect_irq(int irq, void *data)
  99. {
  100. struct sdhci_host *sdhost = (struct sdhci_host *)data;
  101. tasklet_schedule(&sdhost->card_tasklet);
  102. return IRQ_HANDLED;
  103. };
  104. static void tegra_sdhci_reset_exit(struct sdhci_host *host, u8 mask)
  105. {
  106. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  107. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  108. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  109. if (!(mask & SDHCI_RESET_ALL))
  110. return;
  111. /* Erratum: Enable SDHCI spec v3.00 support */
  112. if (soc_data->nvquirks & NVQUIRK_ENABLE_SDHCI_SPEC_300) {
  113. u32 misc_ctrl;
  114. misc_ctrl = sdhci_readb(host, SDHCI_TEGRA_VENDOR_MISC_CTRL);
  115. misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300;
  116. sdhci_writeb(host, misc_ctrl, SDHCI_TEGRA_VENDOR_MISC_CTRL);
  117. }
  118. }
  119. static int tegra_sdhci_buswidth(struct sdhci_host *host, int bus_width)
  120. {
  121. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  122. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  123. u32 ctrl;
  124. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  125. if (tegra_host->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_bus_width = tegra_sdhci_buswidth,
  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 void sdhci_tegra_parse_dt(struct device *dev,
  185. struct sdhci_tegra *tegra_host)
  186. {
  187. struct device_node *np = dev->of_node;
  188. u32 bus_width;
  189. tegra_host->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  190. tegra_host->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
  191. tegra_host->power_gpio = of_get_named_gpio(np, "power-gpios", 0);
  192. if (of_property_read_u32(np, "bus-width", &bus_width) == 0 &&
  193. bus_width == 8)
  194. tegra_host->is_8bit = 1;
  195. }
  196. static int sdhci_tegra_probe(struct platform_device *pdev)
  197. {
  198. const struct of_device_id *match;
  199. const struct sdhci_tegra_soc_data *soc_data;
  200. struct sdhci_host *host;
  201. struct sdhci_pltfm_host *pltfm_host;
  202. struct sdhci_tegra *tegra_host;
  203. struct clk *clk;
  204. int rc;
  205. match = of_match_device(sdhci_tegra_dt_match, &pdev->dev);
  206. if (!match)
  207. return -EINVAL;
  208. soc_data = match->data;
  209. host = sdhci_pltfm_init(pdev, soc_data->pdata);
  210. if (IS_ERR(host))
  211. return PTR_ERR(host);
  212. pltfm_host = sdhci_priv(host);
  213. tegra_host = devm_kzalloc(&pdev->dev, sizeof(*tegra_host), GFP_KERNEL);
  214. if (!tegra_host) {
  215. dev_err(mmc_dev(host->mmc), "failed to allocate tegra_host\n");
  216. rc = -ENOMEM;
  217. goto err_alloc_tegra_host;
  218. }
  219. tegra_host->soc_data = soc_data;
  220. pltfm_host->priv = tegra_host;
  221. sdhci_tegra_parse_dt(&pdev->dev, tegra_host);
  222. if (gpio_is_valid(tegra_host->power_gpio)) {
  223. rc = gpio_request(tegra_host->power_gpio, "sdhci_power");
  224. if (rc) {
  225. dev_err(mmc_dev(host->mmc),
  226. "failed to allocate power gpio\n");
  227. goto err_power_req;
  228. }
  229. gpio_direction_output(tegra_host->power_gpio, 1);
  230. }
  231. if (gpio_is_valid(tegra_host->cd_gpio)) {
  232. rc = gpio_request(tegra_host->cd_gpio, "sdhci_cd");
  233. if (rc) {
  234. dev_err(mmc_dev(host->mmc),
  235. "failed to allocate cd gpio\n");
  236. goto err_cd_req;
  237. }
  238. gpio_direction_input(tegra_host->cd_gpio);
  239. rc = request_irq(gpio_to_irq(tegra_host->cd_gpio),
  240. carddetect_irq,
  241. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  242. mmc_hostname(host->mmc), host);
  243. if (rc) {
  244. dev_err(mmc_dev(host->mmc), "request irq error\n");
  245. goto err_cd_irq_req;
  246. }
  247. }
  248. if (gpio_is_valid(tegra_host->wp_gpio)) {
  249. rc = gpio_request(tegra_host->wp_gpio, "sdhci_wp");
  250. if (rc) {
  251. dev_err(mmc_dev(host->mmc),
  252. "failed to allocate wp gpio\n");
  253. goto err_wp_req;
  254. }
  255. gpio_direction_input(tegra_host->wp_gpio);
  256. }
  257. clk = clk_get(mmc_dev(host->mmc), NULL);
  258. if (IS_ERR(clk)) {
  259. dev_err(mmc_dev(host->mmc), "clk err\n");
  260. rc = PTR_ERR(clk);
  261. goto err_clk_get;
  262. }
  263. clk_prepare_enable(clk);
  264. pltfm_host->clk = clk;
  265. if (tegra_host->is_8bit)
  266. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  267. rc = sdhci_add_host(host);
  268. if (rc)
  269. goto err_add_host;
  270. return 0;
  271. err_add_host:
  272. clk_disable_unprepare(pltfm_host->clk);
  273. clk_put(pltfm_host->clk);
  274. err_clk_get:
  275. if (gpio_is_valid(tegra_host->wp_gpio))
  276. gpio_free(tegra_host->wp_gpio);
  277. err_wp_req:
  278. if (gpio_is_valid(tegra_host->cd_gpio))
  279. free_irq(gpio_to_irq(tegra_host->cd_gpio), host);
  280. err_cd_irq_req:
  281. if (gpio_is_valid(tegra_host->cd_gpio))
  282. gpio_free(tegra_host->cd_gpio);
  283. err_cd_req:
  284. if (gpio_is_valid(tegra_host->power_gpio))
  285. gpio_free(tegra_host->power_gpio);
  286. err_power_req:
  287. err_alloc_tegra_host:
  288. sdhci_pltfm_free(pdev);
  289. return rc;
  290. }
  291. static int sdhci_tegra_remove(struct platform_device *pdev)
  292. {
  293. struct sdhci_host *host = platform_get_drvdata(pdev);
  294. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  295. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  296. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  297. sdhci_remove_host(host, dead);
  298. if (gpio_is_valid(tegra_host->wp_gpio))
  299. gpio_free(tegra_host->wp_gpio);
  300. if (gpio_is_valid(tegra_host->cd_gpio)) {
  301. free_irq(gpio_to_irq(tegra_host->cd_gpio), host);
  302. gpio_free(tegra_host->cd_gpio);
  303. }
  304. if (gpio_is_valid(tegra_host->power_gpio))
  305. gpio_free(tegra_host->power_gpio);
  306. clk_disable_unprepare(pltfm_host->clk);
  307. clk_put(pltfm_host->clk);
  308. sdhci_pltfm_free(pdev);
  309. return 0;
  310. }
  311. static struct platform_driver sdhci_tegra_driver = {
  312. .driver = {
  313. .name = "sdhci-tegra",
  314. .owner = THIS_MODULE,
  315. .of_match_table = sdhci_tegra_dt_match,
  316. .pm = SDHCI_PLTFM_PMOPS,
  317. },
  318. .probe = sdhci_tegra_probe,
  319. .remove = sdhci_tegra_remove,
  320. };
  321. module_platform_driver(sdhci_tegra_driver);
  322. MODULE_DESCRIPTION("SDHCI driver for Tegra");
  323. MODULE_AUTHOR("Google, Inc.");
  324. MODULE_LICENSE("GPL v2");