sdhci-bcm-kona.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (C) 2013 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/highmem.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mmc/host.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio.h>
  21. #include <linux/clk.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/version.h>
  27. #include <linux/mmc/slot-gpio.h>
  28. #include "sdhci-pltfm.h"
  29. #include "sdhci.h"
  30. #define SDHCI_SOFT_RESET 0x01000000
  31. #define KONA_SDHOST_CORECTRL 0x8000
  32. #define KONA_SDHOST_CD_PINCTRL 0x00000008
  33. #define KONA_SDHOST_STOP_HCLK 0x00000004
  34. #define KONA_SDHOST_RESET 0x00000002
  35. #define KONA_SDHOST_EN 0x00000001
  36. #define KONA_SDHOST_CORESTAT 0x8004
  37. #define KONA_SDHOST_WP 0x00000002
  38. #define KONA_SDHOST_CD_SW 0x00000001
  39. #define KONA_SDHOST_COREIMR 0x8008
  40. #define KONA_SDHOST_IP 0x00000001
  41. #define KONA_SDHOST_COREISR 0x800C
  42. #define KONA_SDHOST_COREIMSR 0x8010
  43. #define KONA_SDHOST_COREDBG1 0x8014
  44. #define KONA_SDHOST_COREGPO_MASK 0x8018
  45. #define SD_DETECT_GPIO_DEBOUNCE_128MS 128
  46. #define KONA_MMC_AUTOSUSPEND_DELAY (50)
  47. struct sdhci_bcm_kona_dev {
  48. struct mutex write_lock; /* protect back to back writes */
  49. };
  50. static int sdhci_bcm_kona_sd_reset(struct sdhci_host *host)
  51. {
  52. unsigned int val;
  53. unsigned long timeout;
  54. /* This timeout should be sufficent for core to reset */
  55. timeout = jiffies + msecs_to_jiffies(100);
  56. /* reset the host using the top level reset */
  57. val = sdhci_readl(host, KONA_SDHOST_CORECTRL);
  58. val |= KONA_SDHOST_RESET;
  59. sdhci_writel(host, val, KONA_SDHOST_CORECTRL);
  60. while (!(sdhci_readl(host, KONA_SDHOST_CORECTRL) & KONA_SDHOST_RESET)) {
  61. if (time_is_before_jiffies(timeout)) {
  62. pr_err("Error: sd host is stuck in reset!!!\n");
  63. return -EFAULT;
  64. }
  65. }
  66. /* bring the host out of reset */
  67. val = sdhci_readl(host, KONA_SDHOST_CORECTRL);
  68. val &= ~KONA_SDHOST_RESET;
  69. /*
  70. * Back-to-Back register write needs a delay of 1ms at bootup (min 10uS)
  71. * Back-to-Back writes to same register needs delay when SD bus clock
  72. * is very low w.r.t AHB clock, mainly during boot-time and during card
  73. * insert-removal.
  74. */
  75. usleep_range(1000, 5000);
  76. sdhci_writel(host, val, KONA_SDHOST_CORECTRL);
  77. return 0;
  78. }
  79. static void sdhci_bcm_kona_sd_init(struct sdhci_host *host)
  80. {
  81. unsigned int val;
  82. /* enable the interrupt from the IP core */
  83. val = sdhci_readl(host, KONA_SDHOST_COREIMR);
  84. val |= KONA_SDHOST_IP;
  85. sdhci_writel(host, val, KONA_SDHOST_COREIMR);
  86. /* Enable the AHB clock gating module to the host */
  87. val = sdhci_readl(host, KONA_SDHOST_CORECTRL);
  88. val |= KONA_SDHOST_EN;
  89. /*
  90. * Back-to-Back register write needs a delay of 1ms at bootup (min 10uS)
  91. * Back-to-Back writes to same register needs delay when SD bus clock
  92. * is very low w.r.t AHB clock, mainly during boot-time and during card
  93. * insert-removal.
  94. */
  95. usleep_range(1000, 5000);
  96. sdhci_writel(host, val, KONA_SDHOST_CORECTRL);
  97. }
  98. /*
  99. * Software emulation of the SD card insertion/removal. Set insert=1 for insert
  100. * and insert=0 for removal. The card detection is done by GPIO. For Broadcom
  101. * IP to function properly the bit 0 of CORESTAT register needs to be set/reset
  102. * to generate the CD IRQ handled in sdhci.c which schedules card_tasklet.
  103. */
  104. static int sdhci_bcm_kona_sd_card_emulate(struct sdhci_host *host, int insert)
  105. {
  106. struct sdhci_pltfm_host *pltfm_priv = sdhci_priv(host);
  107. struct sdhci_bcm_kona_dev *kona_dev = sdhci_pltfm_priv(pltfm_priv);
  108. u32 val;
  109. /*
  110. * Back-to-Back register write needs a delay of min 10uS.
  111. * Back-to-Back writes to same register needs delay when SD bus clock
  112. * is very low w.r.t AHB clock, mainly during boot-time and during card
  113. * insert-removal.
  114. * We keep 20uS
  115. */
  116. mutex_lock(&kona_dev->write_lock);
  117. udelay(20);
  118. val = sdhci_readl(host, KONA_SDHOST_CORESTAT);
  119. if (insert) {
  120. int ret;
  121. ret = mmc_gpio_get_ro(host->mmc);
  122. if (ret >= 0)
  123. val = (val & ~KONA_SDHOST_WP) |
  124. ((ret) ? KONA_SDHOST_WP : 0);
  125. val |= KONA_SDHOST_CD_SW;
  126. sdhci_writel(host, val, KONA_SDHOST_CORESTAT);
  127. } else {
  128. val &= ~KONA_SDHOST_CD_SW;
  129. sdhci_writel(host, val, KONA_SDHOST_CORESTAT);
  130. }
  131. mutex_unlock(&kona_dev->write_lock);
  132. return 0;
  133. }
  134. /*
  135. * SD card interrupt event callback
  136. */
  137. void sdhci_bcm_kona_card_event(struct sdhci_host *host)
  138. {
  139. if (mmc_gpio_get_cd(host->mmc) > 0) {
  140. dev_dbg(mmc_dev(host->mmc),
  141. "card inserted\n");
  142. sdhci_bcm_kona_sd_card_emulate(host, 1);
  143. } else {
  144. dev_dbg(mmc_dev(host->mmc),
  145. "card removed\n");
  146. sdhci_bcm_kona_sd_card_emulate(host, 0);
  147. }
  148. }
  149. /*
  150. * Get the base clock. Use central clock source for now. Not sure if different
  151. * clock speed to each dev is allowed
  152. */
  153. static unsigned int sdhci_bcm_kona_get_max_clk(struct sdhci_host *host)
  154. {
  155. struct sdhci_bcm_kona_dev *kona_dev;
  156. struct sdhci_pltfm_host *pltfm_priv = sdhci_priv(host);
  157. kona_dev = sdhci_pltfm_priv(pltfm_priv);
  158. return host->mmc->f_max;
  159. }
  160. static unsigned int sdhci_bcm_kona_get_timeout_clock(struct sdhci_host *host)
  161. {
  162. return sdhci_bcm_kona_get_max_clk(host);
  163. }
  164. static void sdhci_bcm_kona_init_74_clocks(struct sdhci_host *host,
  165. u8 power_mode)
  166. {
  167. /*
  168. * JEDEC and SD spec specify supplying 74 continuous clocks to
  169. * device after power up. With minimum bus (100KHz) that
  170. * that translates to 740us
  171. */
  172. if (power_mode != MMC_POWER_OFF)
  173. udelay(740);
  174. }
  175. static struct sdhci_ops sdhci_bcm_kona_ops = {
  176. .get_max_clock = sdhci_bcm_kona_get_max_clk,
  177. .get_timeout_clock = sdhci_bcm_kona_get_timeout_clock,
  178. .platform_send_init_74_clocks = sdhci_bcm_kona_init_74_clocks,
  179. .card_event = sdhci_bcm_kona_card_event,
  180. };
  181. static struct sdhci_pltfm_data sdhci_pltfm_data_kona = {
  182. .ops = &sdhci_bcm_kona_ops,
  183. .quirks = SDHCI_QUIRK_NO_CARD_NO_RESET |
  184. SDHCI_QUIRK_BROKEN_TIMEOUT_VAL | SDHCI_QUIRK_32BIT_DMA_ADDR |
  185. SDHCI_QUIRK_32BIT_DMA_SIZE | SDHCI_QUIRK_32BIT_ADMA_SIZE |
  186. SDHCI_QUIRK_FORCE_BLK_SZ_2048 |
  187. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  188. };
  189. static const struct of_device_id sdhci_bcm_kona_of_match[] __initdata = {
  190. { .compatible = "bcm,kona-sdhci"},
  191. {}
  192. };
  193. MODULE_DEVICE_TABLE(of, sdhci_bcm_kona_of_match);
  194. static int __init sdhci_bcm_kona_probe(struct platform_device *pdev)
  195. {
  196. struct sdhci_bcm_kona_dev *kona_dev = NULL;
  197. struct sdhci_pltfm_host *pltfm_priv;
  198. struct device *dev = &pdev->dev;
  199. struct sdhci_host *host;
  200. int ret;
  201. ret = 0;
  202. host = sdhci_pltfm_init(pdev, &sdhci_pltfm_data_kona,
  203. sizeof(*kona_dev));
  204. if (IS_ERR(host))
  205. return PTR_ERR(host);
  206. dev_dbg(dev, "%s: inited. IOADDR=%p\n", __func__, host->ioaddr);
  207. pltfm_priv = sdhci_priv(host);
  208. kona_dev = sdhci_pltfm_priv(pltfm_priv);
  209. mutex_init(&kona_dev->write_lock);
  210. mmc_of_parse(host->mmc);
  211. if (!host->mmc->f_max) {
  212. dev_err(&pdev->dev, "Missing max-freq for SDHCI cfg\n");
  213. ret = -ENXIO;
  214. goto err_pltfm_free;
  215. }
  216. dev_dbg(dev, "non-removable=%c\n",
  217. (host->mmc->caps & MMC_CAP_NONREMOVABLE) ? 'Y' : 'N');
  218. dev_dbg(dev, "cd_gpio %c, wp_gpio %c\n",
  219. (mmc_gpio_get_cd(host->mmc) != -ENOSYS) ? 'Y' : 'N',
  220. (mmc_gpio_get_ro(host->mmc) != -ENOSYS) ? 'Y' : 'N');
  221. if (host->mmc->caps | MMC_CAP_NONREMOVABLE)
  222. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  223. dev_dbg(dev, "is_8bit=%c\n",
  224. (host->mmc->caps | MMC_CAP_8_BIT_DATA) ? 'Y' : 'N');
  225. ret = sdhci_bcm_kona_sd_reset(host);
  226. if (ret)
  227. goto err_pltfm_free;
  228. sdhci_bcm_kona_sd_init(host);
  229. ret = sdhci_add_host(host);
  230. if (ret) {
  231. dev_err(dev, "Failed sdhci_add_host\n");
  232. goto err_reset;
  233. }
  234. /* if device is eMMC, emulate card insert right here */
  235. if (host->mmc->caps | MMC_CAP_NONREMOVABLE) {
  236. ret = sdhci_bcm_kona_sd_card_emulate(host, 1);
  237. if (ret) {
  238. dev_err(dev,
  239. "unable to emulate card insertion\n");
  240. goto err_remove_host;
  241. }
  242. }
  243. /*
  244. * Since the card detection GPIO interrupt is configured to be
  245. * edge sensitive, check the initial GPIO value here, emulate
  246. * only if the card is present
  247. */
  248. if (mmc_gpio_get_cd(host->mmc) > 0)
  249. sdhci_bcm_kona_sd_card_emulate(host, 1);
  250. dev_dbg(dev, "initialized properly\n");
  251. return 0;
  252. err_remove_host:
  253. sdhci_remove_host(host, 0);
  254. err_reset:
  255. sdhci_bcm_kona_sd_reset(host);
  256. err_pltfm_free:
  257. sdhci_pltfm_free(pdev);
  258. dev_err(dev, "Probing of sdhci-pltfm failed: %d\n", ret);
  259. return ret;
  260. }
  261. static int __exit sdhci_bcm_kona_remove(struct platform_device *pdev)
  262. {
  263. struct sdhci_host *host = platform_get_drvdata(pdev);
  264. int dead;
  265. u32 scratch;
  266. dead = 0;
  267. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  268. if (scratch == (u32)-1)
  269. dead = 1;
  270. sdhci_remove_host(host, dead);
  271. sdhci_free_host(host);
  272. return 0;
  273. }
  274. static struct platform_driver sdhci_bcm_kona_driver = {
  275. .driver = {
  276. .name = "sdhci-kona",
  277. .owner = THIS_MODULE,
  278. .pm = SDHCI_PLTFM_PMOPS,
  279. .of_match_table = of_match_ptr(sdhci_bcm_kona_of_match),
  280. },
  281. .probe = sdhci_bcm_kona_probe,
  282. .remove = __exit_p(sdhci_bcm_kona_remove),
  283. };
  284. module_platform_driver(sdhci_bcm_kona_driver);
  285. MODULE_DESCRIPTION("SDHCI driver for Broadcom Kona platform");
  286. MODULE_AUTHOR("Broadcom");
  287. MODULE_LICENSE("GPL v2");