sdhci-pxav2.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2010 Marvell International Ltd.
  4. * Zhangfei Gao <zhangfei.gao@marvell.com>
  5. * Kevin Wang <dwang4@marvell.com>
  6. * Jun Nie <njun@marvell.com>
  7. * Qiming Wu <wuqm@marvell.com>
  8. * Philip Rakity <prakity@marvell.com>
  9. */
  10. #include <linux/err.h>
  11. #include <linux/init.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/clk.h>
  14. #include <linux/module.h>
  15. #include <linux/io.h>
  16. #include <linux/mmc/card.h>
  17. #include <linux/mmc/host.h>
  18. #include <linux/platform_data/pxa_sdhci.h>
  19. #include <linux/slab.h>
  20. #include <linux/of.h>
  21. #include <linux/mmc/sdio.h>
  22. #include <linux/mmc/mmc.h>
  23. #include <linux/pinctrl/consumer.h>
  24. #include "sdhci.h"
  25. #include "sdhci-pltfm.h"
  26. #define SD_FIFO_PARAM 0xe0
  27. #define DIS_PAD_SD_CLK_GATE 0x0400 /* Turn on/off Dynamic SD Clock Gating */
  28. #define CLK_GATE_ON 0x0200 /* Disable/enable Clock Gate */
  29. #define CLK_GATE_CTL 0x0100 /* Clock Gate Control */
  30. #define CLK_GATE_SETTING_BITS (DIS_PAD_SD_CLK_GATE | \
  31. CLK_GATE_ON | CLK_GATE_CTL)
  32. #define SD_CLOCK_BURST_SIZE_SETUP 0xe6
  33. #define SDCLK_SEL_SHIFT 8
  34. #define SDCLK_SEL_MASK 0x3
  35. #define SDCLK_DELAY_SHIFT 10
  36. #define SDCLK_DELAY_MASK 0x3c
  37. #define SD_CE_ATA_2 0xea
  38. #define MMC_CARD 0x1000
  39. #define MMC_WIDTH 0x0100
  40. struct sdhci_pxav2_host {
  41. struct mmc_request *sdio_mrq;
  42. struct pinctrl *pinctrl;
  43. struct pinctrl_state *pins_default;
  44. struct pinctrl_state *pins_cmd_gpio;
  45. };
  46. static void pxav2_reset(struct sdhci_host *host, u8 mask)
  47. {
  48. struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  49. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  50. sdhci_reset(host, mask);
  51. if (mask == SDHCI_RESET_ALL) {
  52. u16 tmp = 0;
  53. /*
  54. * tune timing of read data/command when crc error happen
  55. * no performance impact
  56. */
  57. if (pdata && pdata->clk_delay_sel == 1) {
  58. tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  59. tmp &= ~(SDCLK_DELAY_MASK << SDCLK_DELAY_SHIFT);
  60. tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
  61. << SDCLK_DELAY_SHIFT;
  62. tmp &= ~(SDCLK_SEL_MASK << SDCLK_SEL_SHIFT);
  63. tmp |= (1 & SDCLK_SEL_MASK) << SDCLK_SEL_SHIFT;
  64. writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  65. }
  66. if (pdata && (pdata->flags & PXA_FLAG_ENABLE_CLOCK_GATING)) {
  67. tmp = readw(host->ioaddr + SD_FIFO_PARAM);
  68. tmp &= ~CLK_GATE_SETTING_BITS;
  69. writew(tmp, host->ioaddr + SD_FIFO_PARAM);
  70. } else {
  71. tmp = readw(host->ioaddr + SD_FIFO_PARAM);
  72. tmp &= ~CLK_GATE_SETTING_BITS;
  73. tmp |= CLK_GATE_SETTING_BITS;
  74. writew(tmp, host->ioaddr + SD_FIFO_PARAM);
  75. }
  76. }
  77. }
  78. static u16 pxav1_readw(struct sdhci_host *host, int reg)
  79. {
  80. /* Workaround for data abort exception on SDH2 and SDH4 on PXA168 */
  81. if (reg == SDHCI_HOST_VERSION)
  82. return readl(host->ioaddr + SDHCI_HOST_VERSION - 2) >> 16;
  83. return readw(host->ioaddr + reg);
  84. }
  85. static u32 pxav1_irq(struct sdhci_host *host, u32 intmask)
  86. {
  87. struct sdhci_pxav2_host *pxav2_host = sdhci_pltfm_priv(sdhci_priv(host));
  88. struct mmc_request *sdio_mrq;
  89. if (pxav2_host->sdio_mrq && (intmask & SDHCI_INT_CMD_MASK)) {
  90. /* The dummy CMD0 for the SDIO workaround just completed */
  91. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK, SDHCI_INT_STATUS);
  92. intmask &= ~SDHCI_INT_CMD_MASK;
  93. /* Restore MMC function to CMD pin */
  94. if (pxav2_host->pinctrl && pxav2_host->pins_default)
  95. pinctrl_select_state(pxav2_host->pinctrl, pxav2_host->pins_default);
  96. sdio_mrq = pxav2_host->sdio_mrq;
  97. pxav2_host->sdio_mrq = NULL;
  98. mmc_request_done(host->mmc, sdio_mrq);
  99. }
  100. return intmask;
  101. }
  102. static void pxav1_request_done(struct sdhci_host *host, struct mmc_request *mrq)
  103. {
  104. u16 tmp;
  105. struct sdhci_pxav2_host *pxav2_host;
  106. /* If this is an SDIO command, perform errata workaround for silicon bug */
  107. if (!mrq->cmd->error &&
  108. (mrq->cmd->opcode == SD_IO_RW_DIRECT ||
  109. mrq->cmd->opcode == SD_IO_RW_EXTENDED)) {
  110. /* Reset data port */
  111. tmp = readw(host->ioaddr + SDHCI_TIMEOUT_CONTROL);
  112. tmp |= 0x400;
  113. writew(tmp, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
  114. /* Clock is now stopped, so restart it by sending a dummy CMD0 */
  115. pxav2_host = sdhci_pltfm_priv(sdhci_priv(host));
  116. pxav2_host->sdio_mrq = mrq;
  117. /* Set CMD as high output rather than MMC function while we do CMD0 */
  118. if (pxav2_host->pinctrl && pxav2_host->pins_cmd_gpio)
  119. pinctrl_select_state(pxav2_host->pinctrl, pxav2_host->pins_cmd_gpio);
  120. sdhci_writel(host, 0, SDHCI_ARGUMENT);
  121. sdhci_writew(host, 0, SDHCI_TRANSFER_MODE);
  122. sdhci_writew(host, SDHCI_MAKE_CMD(MMC_GO_IDLE_STATE, SDHCI_CMD_RESP_NONE),
  123. SDHCI_COMMAND);
  124. /* Don't finish this request until the dummy CMD0 finishes */
  125. return;
  126. }
  127. mmc_request_done(host->mmc, mrq);
  128. }
  129. static void pxav2_mmc_set_bus_width(struct sdhci_host *host, int width)
  130. {
  131. u8 ctrl;
  132. u16 tmp;
  133. ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
  134. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  135. if (width == MMC_BUS_WIDTH_8) {
  136. ctrl &= ~SDHCI_CTRL_4BITBUS;
  137. tmp |= MMC_CARD | MMC_WIDTH;
  138. } else {
  139. tmp &= ~(MMC_CARD | MMC_WIDTH);
  140. if (width == MMC_BUS_WIDTH_4)
  141. ctrl |= SDHCI_CTRL_4BITBUS;
  142. else
  143. ctrl &= ~SDHCI_CTRL_4BITBUS;
  144. }
  145. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  146. writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
  147. }
  148. struct sdhci_pxa_variant {
  149. const struct sdhci_ops *ops;
  150. unsigned int extra_quirks;
  151. };
  152. static const struct sdhci_ops pxav1_sdhci_ops = {
  153. .read_w = pxav1_readw,
  154. .set_clock = sdhci_set_clock,
  155. .irq = pxav1_irq,
  156. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  157. .set_bus_width = pxav2_mmc_set_bus_width,
  158. .reset = pxav2_reset,
  159. .set_uhs_signaling = sdhci_set_uhs_signaling,
  160. .request_done = pxav1_request_done,
  161. };
  162. static const struct sdhci_pxa_variant __maybe_unused pxav1_variant = {
  163. .ops = &pxav1_sdhci_ops,
  164. .extra_quirks = SDHCI_QUIRK_NO_BUSY_IRQ | SDHCI_QUIRK_32BIT_DMA_SIZE,
  165. };
  166. static const struct sdhci_ops pxav2_sdhci_ops = {
  167. .set_clock = sdhci_set_clock,
  168. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  169. .set_bus_width = pxav2_mmc_set_bus_width,
  170. .reset = pxav2_reset,
  171. .set_uhs_signaling = sdhci_set_uhs_signaling,
  172. };
  173. static const struct sdhci_pxa_variant pxav2_variant = {
  174. .ops = &pxav2_sdhci_ops,
  175. };
  176. #ifdef CONFIG_OF
  177. static const struct of_device_id sdhci_pxav2_of_match[] = {
  178. { .compatible = "mrvl,pxav1-mmc", .data = &pxav1_variant, },
  179. { .compatible = "mrvl,pxav2-mmc", .data = &pxav2_variant, },
  180. {},
  181. };
  182. MODULE_DEVICE_TABLE(of, sdhci_pxav2_of_match);
  183. static struct sdhci_pxa_platdata *pxav2_get_mmc_pdata(struct device *dev)
  184. {
  185. struct sdhci_pxa_platdata *pdata;
  186. struct device_node *np = dev->of_node;
  187. u32 bus_width;
  188. u32 clk_delay_cycles;
  189. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  190. if (!pdata)
  191. return NULL;
  192. if (of_property_read_bool(np, "non-removable"))
  193. pdata->flags |= PXA_FLAG_CARD_PERMANENT;
  194. of_property_read_u32(np, "bus-width", &bus_width);
  195. if (bus_width == 8)
  196. pdata->flags |= PXA_FLAG_SD_8_BIT_CAPABLE_SLOT;
  197. of_property_read_u32(np, "mrvl,clk-delay-cycles", &clk_delay_cycles);
  198. if (clk_delay_cycles > 0) {
  199. pdata->clk_delay_sel = 1;
  200. pdata->clk_delay_cycles = clk_delay_cycles;
  201. }
  202. return pdata;
  203. }
  204. #else
  205. static inline struct sdhci_pxa_platdata *pxav2_get_mmc_pdata(struct device *dev)
  206. {
  207. return NULL;
  208. }
  209. #endif
  210. static int sdhci_pxav2_probe(struct platform_device *pdev)
  211. {
  212. struct sdhci_pltfm_host *pltfm_host;
  213. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  214. struct sdhci_pxav2_host *pxav2_host;
  215. struct device *dev = &pdev->dev;
  216. struct sdhci_host *host = NULL;
  217. const struct sdhci_pxa_variant *variant;
  218. struct clk *clk, *clk_core;
  219. host = sdhci_pltfm_init(pdev, NULL, sizeof(*pxav2_host));
  220. if (IS_ERR(host))
  221. return PTR_ERR(host);
  222. pltfm_host = sdhci_priv(host);
  223. pxav2_host = sdhci_pltfm_priv(pltfm_host);
  224. clk = devm_clk_get_optional_enabled(dev, "io");
  225. if (!clk)
  226. clk = devm_clk_get_enabled(dev, NULL);
  227. if (IS_ERR(clk))
  228. return dev_err_probe(dev, PTR_ERR(clk), "failed to get io clock\n");
  229. pltfm_host->clk = clk;
  230. clk_core = devm_clk_get_optional_enabled(dev, "core");
  231. if (IS_ERR(clk_core))
  232. return dev_err_probe(dev, PTR_ERR(clk_core),
  233. "failed to enable core clock\n");
  234. host->quirks = SDHCI_QUIRK_BROKEN_ADMA
  235. | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
  236. | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
  237. variant = of_device_get_match_data(dev);
  238. if (variant)
  239. pdata = pxav2_get_mmc_pdata(dev);
  240. else
  241. variant = &pxav2_variant;
  242. if (pdata) {
  243. if (pdata->flags & PXA_FLAG_CARD_PERMANENT) {
  244. /* on-chip device */
  245. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  246. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  247. }
  248. /* If slot design supports 8 bit data, indicate this to MMC. */
  249. if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
  250. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  251. if (pdata->quirks)
  252. host->quirks |= pdata->quirks;
  253. if (pdata->host_caps)
  254. host->mmc->caps |= pdata->host_caps;
  255. if (pdata->pm_caps)
  256. host->mmc->pm_caps |= pdata->pm_caps;
  257. }
  258. host->quirks |= variant->extra_quirks;
  259. host->ops = variant->ops;
  260. /* Set up optional pinctrl for PXA168 SDIO IRQ fix */
  261. pxav2_host->pinctrl = devm_pinctrl_get(dev);
  262. if (!IS_ERR(pxav2_host->pinctrl)) {
  263. pxav2_host->pins_cmd_gpio = pinctrl_lookup_state(pxav2_host->pinctrl,
  264. "state_cmd_gpio");
  265. if (IS_ERR(pxav2_host->pins_cmd_gpio))
  266. pxav2_host->pins_cmd_gpio = NULL;
  267. pxav2_host->pins_default = pinctrl_lookup_state(pxav2_host->pinctrl,
  268. "default");
  269. if (IS_ERR(pxav2_host->pins_default))
  270. pxav2_host->pins_default = NULL;
  271. } else {
  272. pxav2_host->pinctrl = NULL;
  273. }
  274. return sdhci_add_host(host);
  275. }
  276. static struct platform_driver sdhci_pxav2_driver = {
  277. .driver = {
  278. .name = "sdhci-pxav2",
  279. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  280. .of_match_table = of_match_ptr(sdhci_pxav2_of_match),
  281. .pm = &sdhci_pltfm_pmops,
  282. },
  283. .probe = sdhci_pxav2_probe,
  284. .remove = sdhci_pltfm_remove,
  285. };
  286. module_platform_driver(sdhci_pxav2_driver);
  287. MODULE_DESCRIPTION("SDHCI driver for pxav2");
  288. MODULE_AUTHOR("Marvell International Ltd.");
  289. MODULE_LICENSE("GPL v2");