sdhci-tegra.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/iopoll.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/clk.h>
  21. #include <linux/io.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/reset.h>
  27. #include <linux/mmc/card.h>
  28. #include <linux/mmc/host.h>
  29. #include <linux/mmc/mmc.h>
  30. #include <linux/mmc/slot-gpio.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/ktime.h>
  33. #include "sdhci-pltfm.h"
  34. /* Tegra SDHOST controller vendor register definitions */
  35. #define SDHCI_TEGRA_VENDOR_CLOCK_CTRL 0x100
  36. #define SDHCI_CLOCK_CTRL_TAP_MASK 0x00ff0000
  37. #define SDHCI_CLOCK_CTRL_TAP_SHIFT 16
  38. #define SDHCI_CLOCK_CTRL_TRIM_MASK 0x1f000000
  39. #define SDHCI_CLOCK_CTRL_TRIM_SHIFT 24
  40. #define SDHCI_CLOCK_CTRL_SDR50_TUNING_OVERRIDE BIT(5)
  41. #define SDHCI_CLOCK_CTRL_PADPIPE_CLKEN_OVERRIDE BIT(3)
  42. #define SDHCI_CLOCK_CTRL_SPI_MODE_CLKEN_OVERRIDE BIT(2)
  43. #define SDHCI_TEGRA_VENDOR_SYS_SW_CTRL 0x104
  44. #define SDHCI_TEGRA_SYS_SW_CTRL_ENHANCED_STROBE BIT(31)
  45. #define SDHCI_TEGRA_VENDOR_CAP_OVERRIDES 0x10c
  46. #define SDHCI_TEGRA_CAP_OVERRIDES_DQS_TRIM_MASK 0x00003f00
  47. #define SDHCI_TEGRA_CAP_OVERRIDES_DQS_TRIM_SHIFT 8
  48. #define SDHCI_TEGRA_VENDOR_MISC_CTRL 0x120
  49. #define SDHCI_MISC_CTRL_ENABLE_SDR104 0x8
  50. #define SDHCI_MISC_CTRL_ENABLE_SDR50 0x10
  51. #define SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300 0x20
  52. #define SDHCI_MISC_CTRL_ENABLE_DDR50 0x200
  53. #define SDHCI_TEGRA_VENDOR_DLLCAL_CFG 0x1b0
  54. #define SDHCI_TEGRA_DLLCAL_CALIBRATE BIT(31)
  55. #define SDHCI_TEGRA_VENDOR_DLLCAL_STA 0x1bc
  56. #define SDHCI_TEGRA_DLLCAL_STA_ACTIVE BIT(31)
  57. #define SDHCI_VNDR_TUN_CTRL0_0 0x1c0
  58. #define SDHCI_VNDR_TUN_CTRL0_TUN_HW_TAP 0x20000
  59. #define SDHCI_TEGRA_AUTO_CAL_CONFIG 0x1e4
  60. #define SDHCI_AUTO_CAL_START BIT(31)
  61. #define SDHCI_AUTO_CAL_ENABLE BIT(29)
  62. #define SDHCI_AUTO_CAL_PDPU_OFFSET_MASK 0x0000ffff
  63. #define SDHCI_TEGRA_SDMEM_COMP_PADCTRL 0x1e0
  64. #define SDHCI_TEGRA_SDMEM_COMP_PADCTRL_VREF_SEL_MASK 0x0000000f
  65. #define SDHCI_TEGRA_SDMEM_COMP_PADCTRL_VREF_SEL_VAL 0x7
  66. #define SDHCI_TEGRA_SDMEM_COMP_PADCTRL_E_INPUT_E_PWRD BIT(31)
  67. #define SDHCI_TEGRA_AUTO_CAL_STATUS 0x1ec
  68. #define SDHCI_TEGRA_AUTO_CAL_ACTIVE BIT(31)
  69. #define NVQUIRK_FORCE_SDHCI_SPEC_200 BIT(0)
  70. #define NVQUIRK_ENABLE_BLOCK_GAP_DET BIT(1)
  71. #define NVQUIRK_ENABLE_SDHCI_SPEC_300 BIT(2)
  72. #define NVQUIRK_ENABLE_SDR50 BIT(3)
  73. #define NVQUIRK_ENABLE_SDR104 BIT(4)
  74. #define NVQUIRK_ENABLE_DDR50 BIT(5)
  75. #define NVQUIRK_HAS_PADCALIB BIT(6)
  76. #define NVQUIRK_NEEDS_PAD_CONTROL BIT(7)
  77. #define NVQUIRK_DIS_CARD_CLK_CONFIG_TAP BIT(8)
  78. struct sdhci_tegra_soc_data {
  79. const struct sdhci_pltfm_data *pdata;
  80. u32 nvquirks;
  81. };
  82. /* Magic pull up and pull down pad calibration offsets */
  83. struct sdhci_tegra_autocal_offsets {
  84. u32 pull_up_3v3;
  85. u32 pull_down_3v3;
  86. u32 pull_up_3v3_timeout;
  87. u32 pull_down_3v3_timeout;
  88. u32 pull_up_1v8;
  89. u32 pull_down_1v8;
  90. u32 pull_up_1v8_timeout;
  91. u32 pull_down_1v8_timeout;
  92. u32 pull_up_sdr104;
  93. u32 pull_down_sdr104;
  94. u32 pull_up_hs400;
  95. u32 pull_down_hs400;
  96. };
  97. struct sdhci_tegra {
  98. const struct sdhci_tegra_soc_data *soc_data;
  99. struct gpio_desc *power_gpio;
  100. bool ddr_signaling;
  101. bool pad_calib_required;
  102. bool pad_control_available;
  103. struct reset_control *rst;
  104. struct pinctrl *pinctrl_sdmmc;
  105. struct pinctrl_state *pinctrl_state_3v3;
  106. struct pinctrl_state *pinctrl_state_1v8;
  107. struct sdhci_tegra_autocal_offsets autocal_offsets;
  108. ktime_t last_calib;
  109. u32 default_tap;
  110. u32 default_trim;
  111. u32 dqs_trim;
  112. };
  113. static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
  114. {
  115. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  116. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  117. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  118. if (unlikely((soc_data->nvquirks & NVQUIRK_FORCE_SDHCI_SPEC_200) &&
  119. (reg == SDHCI_HOST_VERSION))) {
  120. /* Erratum: Version register is invalid in HW. */
  121. return SDHCI_SPEC_200;
  122. }
  123. return readw(host->ioaddr + reg);
  124. }
  125. static void tegra_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
  126. {
  127. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  128. switch (reg) {
  129. case SDHCI_TRANSFER_MODE:
  130. /*
  131. * Postpone this write, we must do it together with a
  132. * command write that is down below.
  133. */
  134. pltfm_host->xfer_mode_shadow = val;
  135. return;
  136. case SDHCI_COMMAND:
  137. writel((val << 16) | pltfm_host->xfer_mode_shadow,
  138. host->ioaddr + SDHCI_TRANSFER_MODE);
  139. return;
  140. }
  141. writew(val, host->ioaddr + reg);
  142. }
  143. static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
  144. {
  145. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  146. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  147. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  148. /* Seems like we're getting spurious timeout and crc errors, so
  149. * disable signalling of them. In case of real errors software
  150. * timers should take care of eventually detecting them.
  151. */
  152. if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
  153. val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
  154. writel(val, host->ioaddr + reg);
  155. if (unlikely((soc_data->nvquirks & NVQUIRK_ENABLE_BLOCK_GAP_DET) &&
  156. (reg == SDHCI_INT_ENABLE))) {
  157. /* Erratum: Must enable block gap interrupt detection */
  158. u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  159. if (val & SDHCI_INT_CARD_INT)
  160. gap_ctrl |= 0x8;
  161. else
  162. gap_ctrl &= ~0x8;
  163. writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  164. }
  165. }
  166. static bool tegra_sdhci_configure_card_clk(struct sdhci_host *host, bool enable)
  167. {
  168. bool status;
  169. u32 reg;
  170. reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  171. status = !!(reg & SDHCI_CLOCK_CARD_EN);
  172. if (status == enable)
  173. return status;
  174. if (enable)
  175. reg |= SDHCI_CLOCK_CARD_EN;
  176. else
  177. reg &= ~SDHCI_CLOCK_CARD_EN;
  178. sdhci_writew(host, reg, SDHCI_CLOCK_CONTROL);
  179. return status;
  180. }
  181. static void tegra210_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
  182. {
  183. bool is_tuning_cmd = 0;
  184. bool clk_enabled;
  185. u8 cmd;
  186. if (reg == SDHCI_COMMAND) {
  187. cmd = SDHCI_GET_CMD(val);
  188. is_tuning_cmd = cmd == MMC_SEND_TUNING_BLOCK ||
  189. cmd == MMC_SEND_TUNING_BLOCK_HS200;
  190. }
  191. if (is_tuning_cmd)
  192. clk_enabled = tegra_sdhci_configure_card_clk(host, 0);
  193. writew(val, host->ioaddr + reg);
  194. if (is_tuning_cmd) {
  195. udelay(1);
  196. tegra_sdhci_configure_card_clk(host, clk_enabled);
  197. }
  198. }
  199. static unsigned int tegra_sdhci_get_ro(struct sdhci_host *host)
  200. {
  201. return mmc_gpio_get_ro(host->mmc);
  202. }
  203. static bool tegra_sdhci_is_pad_and_regulator_valid(struct sdhci_host *host)
  204. {
  205. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  206. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  207. int has_1v8, has_3v3;
  208. /*
  209. * The SoCs which have NVQUIRK_NEEDS_PAD_CONTROL require software pad
  210. * voltage configuration in order to perform voltage switching. This
  211. * means that valid pinctrl info is required on SDHCI instances capable
  212. * of performing voltage switching. Whether or not an SDHCI instance is
  213. * capable of voltage switching is determined based on the regulator.
  214. */
  215. if (!(tegra_host->soc_data->nvquirks & NVQUIRK_NEEDS_PAD_CONTROL))
  216. return true;
  217. if (IS_ERR(host->mmc->supply.vqmmc))
  218. return false;
  219. has_1v8 = regulator_is_supported_voltage(host->mmc->supply.vqmmc,
  220. 1700000, 1950000);
  221. has_3v3 = regulator_is_supported_voltage(host->mmc->supply.vqmmc,
  222. 2700000, 3600000);
  223. if (has_1v8 == 1 && has_3v3 == 1)
  224. return tegra_host->pad_control_available;
  225. /* Fixed voltage, no pad control required. */
  226. return true;
  227. }
  228. static void tegra_sdhci_set_tap(struct sdhci_host *host, unsigned int tap)
  229. {
  230. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  231. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  232. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  233. bool card_clk_enabled = false;
  234. u32 reg;
  235. /*
  236. * Touching the tap values is a bit tricky on some SoC generations.
  237. * The quirk enables a workaround for a glitch that sometimes occurs if
  238. * the tap values are changed.
  239. */
  240. if (soc_data->nvquirks & NVQUIRK_DIS_CARD_CLK_CONFIG_TAP)
  241. card_clk_enabled = tegra_sdhci_configure_card_clk(host, false);
  242. reg = sdhci_readl(host, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
  243. reg &= ~SDHCI_CLOCK_CTRL_TAP_MASK;
  244. reg |= tap << SDHCI_CLOCK_CTRL_TAP_SHIFT;
  245. sdhci_writel(host, reg, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
  246. if (soc_data->nvquirks & NVQUIRK_DIS_CARD_CLK_CONFIG_TAP &&
  247. card_clk_enabled) {
  248. udelay(1);
  249. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  250. tegra_sdhci_configure_card_clk(host, card_clk_enabled);
  251. }
  252. }
  253. static void tegra_sdhci_hs400_enhanced_strobe(struct mmc_host *mmc,
  254. struct mmc_ios *ios)
  255. {
  256. struct sdhci_host *host = mmc_priv(mmc);
  257. u32 val;
  258. val = sdhci_readl(host, SDHCI_TEGRA_VENDOR_SYS_SW_CTRL);
  259. if (ios->enhanced_strobe)
  260. val |= SDHCI_TEGRA_SYS_SW_CTRL_ENHANCED_STROBE;
  261. else
  262. val &= ~SDHCI_TEGRA_SYS_SW_CTRL_ENHANCED_STROBE;
  263. sdhci_writel(host, val, SDHCI_TEGRA_VENDOR_SYS_SW_CTRL);
  264. }
  265. static void tegra_sdhci_reset(struct sdhci_host *host, u8 mask)
  266. {
  267. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  268. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  269. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  270. u32 misc_ctrl, clk_ctrl, pad_ctrl;
  271. sdhci_reset(host, mask);
  272. if (!(mask & SDHCI_RESET_ALL))
  273. return;
  274. tegra_sdhci_set_tap(host, tegra_host->default_tap);
  275. misc_ctrl = sdhci_readl(host, SDHCI_TEGRA_VENDOR_MISC_CTRL);
  276. clk_ctrl = sdhci_readl(host, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
  277. misc_ctrl &= ~(SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300 |
  278. SDHCI_MISC_CTRL_ENABLE_SDR50 |
  279. SDHCI_MISC_CTRL_ENABLE_DDR50 |
  280. SDHCI_MISC_CTRL_ENABLE_SDR104);
  281. clk_ctrl &= ~(SDHCI_CLOCK_CTRL_TRIM_MASK |
  282. SDHCI_CLOCK_CTRL_SPI_MODE_CLKEN_OVERRIDE);
  283. if (tegra_sdhci_is_pad_and_regulator_valid(host)) {
  284. /* Erratum: Enable SDHCI spec v3.00 support */
  285. if (soc_data->nvquirks & NVQUIRK_ENABLE_SDHCI_SPEC_300)
  286. misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300;
  287. /* Advertise UHS modes as supported by host */
  288. if (soc_data->nvquirks & NVQUIRK_ENABLE_SDR50)
  289. misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDR50;
  290. if (soc_data->nvquirks & NVQUIRK_ENABLE_DDR50)
  291. misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_DDR50;
  292. if (soc_data->nvquirks & NVQUIRK_ENABLE_SDR104)
  293. misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDR104;
  294. if (soc_data->nvquirks & SDHCI_MISC_CTRL_ENABLE_SDR50)
  295. clk_ctrl |= SDHCI_CLOCK_CTRL_SDR50_TUNING_OVERRIDE;
  296. }
  297. clk_ctrl |= tegra_host->default_trim << SDHCI_CLOCK_CTRL_TRIM_SHIFT;
  298. sdhci_writel(host, misc_ctrl, SDHCI_TEGRA_VENDOR_MISC_CTRL);
  299. sdhci_writel(host, clk_ctrl, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
  300. if (soc_data->nvquirks & NVQUIRK_HAS_PADCALIB) {
  301. pad_ctrl = sdhci_readl(host, SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
  302. pad_ctrl &= ~SDHCI_TEGRA_SDMEM_COMP_PADCTRL_VREF_SEL_MASK;
  303. pad_ctrl |= SDHCI_TEGRA_SDMEM_COMP_PADCTRL_VREF_SEL_VAL;
  304. sdhci_writel(host, pad_ctrl, SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
  305. tegra_host->pad_calib_required = true;
  306. }
  307. tegra_host->ddr_signaling = false;
  308. }
  309. static void tegra_sdhci_configure_cal_pad(struct sdhci_host *host, bool enable)
  310. {
  311. u32 val;
  312. /*
  313. * Enable or disable the additional I/O pad used by the drive strength
  314. * calibration process.
  315. */
  316. val = sdhci_readl(host, SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
  317. if (enable)
  318. val |= SDHCI_TEGRA_SDMEM_COMP_PADCTRL_E_INPUT_E_PWRD;
  319. else
  320. val &= ~SDHCI_TEGRA_SDMEM_COMP_PADCTRL_E_INPUT_E_PWRD;
  321. sdhci_writel(host, val, SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
  322. if (enable)
  323. usleep_range(1, 2);
  324. }
  325. static void tegra_sdhci_set_pad_autocal_offset(struct sdhci_host *host,
  326. u16 pdpu)
  327. {
  328. u32 reg;
  329. reg = sdhci_readl(host, SDHCI_TEGRA_AUTO_CAL_CONFIG);
  330. reg &= ~SDHCI_AUTO_CAL_PDPU_OFFSET_MASK;
  331. reg |= pdpu;
  332. sdhci_writel(host, reg, SDHCI_TEGRA_AUTO_CAL_CONFIG);
  333. }
  334. static void tegra_sdhci_pad_autocalib(struct sdhci_host *host)
  335. {
  336. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  337. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  338. struct sdhci_tegra_autocal_offsets offsets =
  339. tegra_host->autocal_offsets;
  340. struct mmc_ios *ios = &host->mmc->ios;
  341. bool card_clk_enabled;
  342. u16 pdpu;
  343. u32 reg;
  344. int ret;
  345. switch (ios->timing) {
  346. case MMC_TIMING_UHS_SDR104:
  347. pdpu = offsets.pull_down_sdr104 << 8 | offsets.pull_up_sdr104;
  348. break;
  349. case MMC_TIMING_MMC_HS400:
  350. pdpu = offsets.pull_down_hs400 << 8 | offsets.pull_up_hs400;
  351. break;
  352. default:
  353. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
  354. pdpu = offsets.pull_down_1v8 << 8 | offsets.pull_up_1v8;
  355. else
  356. pdpu = offsets.pull_down_3v3 << 8 | offsets.pull_up_3v3;
  357. }
  358. tegra_sdhci_set_pad_autocal_offset(host, pdpu);
  359. card_clk_enabled = tegra_sdhci_configure_card_clk(host, false);
  360. tegra_sdhci_configure_cal_pad(host, true);
  361. reg = sdhci_readl(host, SDHCI_TEGRA_AUTO_CAL_CONFIG);
  362. reg |= SDHCI_AUTO_CAL_ENABLE | SDHCI_AUTO_CAL_START;
  363. sdhci_writel(host, reg, SDHCI_TEGRA_AUTO_CAL_CONFIG);
  364. usleep_range(1, 2);
  365. /* 10 ms timeout */
  366. ret = readl_poll_timeout(host->ioaddr + SDHCI_TEGRA_AUTO_CAL_STATUS,
  367. reg, !(reg & SDHCI_TEGRA_AUTO_CAL_ACTIVE),
  368. 1000, 10000);
  369. tegra_sdhci_configure_cal_pad(host, false);
  370. tegra_sdhci_configure_card_clk(host, card_clk_enabled);
  371. if (ret) {
  372. dev_err(mmc_dev(host->mmc), "Pad autocal timed out\n");
  373. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
  374. pdpu = offsets.pull_down_1v8_timeout << 8 |
  375. offsets.pull_up_1v8_timeout;
  376. else
  377. pdpu = offsets.pull_down_3v3_timeout << 8 |
  378. offsets.pull_up_3v3_timeout;
  379. /* Disable automatic calibration and use fixed offsets */
  380. reg = sdhci_readl(host, SDHCI_TEGRA_AUTO_CAL_CONFIG);
  381. reg &= ~SDHCI_AUTO_CAL_ENABLE;
  382. sdhci_writel(host, reg, SDHCI_TEGRA_AUTO_CAL_CONFIG);
  383. tegra_sdhci_set_pad_autocal_offset(host, pdpu);
  384. }
  385. }
  386. static void tegra_sdhci_parse_pad_autocal_dt(struct sdhci_host *host)
  387. {
  388. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  389. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  390. struct sdhci_tegra_autocal_offsets *autocal =
  391. &tegra_host->autocal_offsets;
  392. int err;
  393. err = device_property_read_u32(host->mmc->parent,
  394. "nvidia,pad-autocal-pull-up-offset-3v3",
  395. &autocal->pull_up_3v3);
  396. if (err)
  397. autocal->pull_up_3v3 = 0;
  398. err = device_property_read_u32(host->mmc->parent,
  399. "nvidia,pad-autocal-pull-down-offset-3v3",
  400. &autocal->pull_down_3v3);
  401. if (err)
  402. autocal->pull_down_3v3 = 0;
  403. err = device_property_read_u32(host->mmc->parent,
  404. "nvidia,pad-autocal-pull-up-offset-1v8",
  405. &autocal->pull_up_1v8);
  406. if (err)
  407. autocal->pull_up_1v8 = 0;
  408. err = device_property_read_u32(host->mmc->parent,
  409. "nvidia,pad-autocal-pull-down-offset-1v8",
  410. &autocal->pull_down_1v8);
  411. if (err)
  412. autocal->pull_down_1v8 = 0;
  413. err = device_property_read_u32(host->mmc->parent,
  414. "nvidia,pad-autocal-pull-up-offset-3v3-timeout",
  415. &autocal->pull_up_3v3);
  416. if (err)
  417. autocal->pull_up_3v3_timeout = 0;
  418. err = device_property_read_u32(host->mmc->parent,
  419. "nvidia,pad-autocal-pull-down-offset-3v3-timeout",
  420. &autocal->pull_down_3v3);
  421. if (err)
  422. autocal->pull_down_3v3_timeout = 0;
  423. err = device_property_read_u32(host->mmc->parent,
  424. "nvidia,pad-autocal-pull-up-offset-1v8-timeout",
  425. &autocal->pull_up_1v8);
  426. if (err)
  427. autocal->pull_up_1v8_timeout = 0;
  428. err = device_property_read_u32(host->mmc->parent,
  429. "nvidia,pad-autocal-pull-down-offset-1v8-timeout",
  430. &autocal->pull_down_1v8);
  431. if (err)
  432. autocal->pull_down_1v8_timeout = 0;
  433. err = device_property_read_u32(host->mmc->parent,
  434. "nvidia,pad-autocal-pull-up-offset-sdr104",
  435. &autocal->pull_up_sdr104);
  436. if (err)
  437. autocal->pull_up_sdr104 = autocal->pull_up_1v8;
  438. err = device_property_read_u32(host->mmc->parent,
  439. "nvidia,pad-autocal-pull-down-offset-sdr104",
  440. &autocal->pull_down_sdr104);
  441. if (err)
  442. autocal->pull_down_sdr104 = autocal->pull_down_1v8;
  443. err = device_property_read_u32(host->mmc->parent,
  444. "nvidia,pad-autocal-pull-up-offset-hs400",
  445. &autocal->pull_up_hs400);
  446. if (err)
  447. autocal->pull_up_hs400 = autocal->pull_up_1v8;
  448. err = device_property_read_u32(host->mmc->parent,
  449. "nvidia,pad-autocal-pull-down-offset-hs400",
  450. &autocal->pull_down_hs400);
  451. if (err)
  452. autocal->pull_down_hs400 = autocal->pull_down_1v8;
  453. }
  454. static void tegra_sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  455. {
  456. struct sdhci_host *host = mmc_priv(mmc);
  457. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  458. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  459. ktime_t since_calib = ktime_sub(ktime_get(), tegra_host->last_calib);
  460. /* 100 ms calibration interval is specified in the TRM */
  461. if (ktime_to_ms(since_calib) > 100) {
  462. tegra_sdhci_pad_autocalib(host);
  463. tegra_host->last_calib = ktime_get();
  464. }
  465. sdhci_request(mmc, mrq);
  466. }
  467. static void tegra_sdhci_parse_tap_and_trim(struct sdhci_host *host)
  468. {
  469. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  470. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  471. int err;
  472. err = device_property_read_u32(host->mmc->parent, "nvidia,default-tap",
  473. &tegra_host->default_tap);
  474. if (err)
  475. tegra_host->default_tap = 0;
  476. err = device_property_read_u32(host->mmc->parent, "nvidia,default-trim",
  477. &tegra_host->default_trim);
  478. if (err)
  479. tegra_host->default_trim = 0;
  480. err = device_property_read_u32(host->mmc->parent, "nvidia,dqs-trim",
  481. &tegra_host->dqs_trim);
  482. if (err)
  483. tegra_host->dqs_trim = 0x11;
  484. }
  485. static void tegra_sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
  486. {
  487. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  488. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  489. unsigned long host_clk;
  490. if (!clock)
  491. return sdhci_set_clock(host, clock);
  492. /*
  493. * In DDR50/52 modes the Tegra SDHCI controllers require the SDHCI
  494. * divider to be configured to divided the host clock by two. The SDHCI
  495. * clock divider is calculated as part of sdhci_set_clock() by
  496. * sdhci_calc_clk(). The divider is calculated from host->max_clk and
  497. * the requested clock rate.
  498. *
  499. * By setting the host->max_clk to clock * 2 the divider calculation
  500. * will always result in the correct value for DDR50/52 modes,
  501. * regardless of clock rate rounding, which may happen if the value
  502. * from clk_get_rate() is used.
  503. */
  504. host_clk = tegra_host->ddr_signaling ? clock * 2 : clock;
  505. clk_set_rate(pltfm_host->clk, host_clk);
  506. if (tegra_host->ddr_signaling)
  507. host->max_clk = host_clk;
  508. else
  509. host->max_clk = clk_get_rate(pltfm_host->clk);
  510. sdhci_set_clock(host, clock);
  511. if (tegra_host->pad_calib_required) {
  512. tegra_sdhci_pad_autocalib(host);
  513. tegra_host->pad_calib_required = false;
  514. }
  515. }
  516. static unsigned int tegra_sdhci_get_max_clock(struct sdhci_host *host)
  517. {
  518. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  519. return clk_round_rate(pltfm_host->clk, UINT_MAX);
  520. }
  521. static void tegra_sdhci_set_dqs_trim(struct sdhci_host *host, u8 trim)
  522. {
  523. u32 val;
  524. val = sdhci_readl(host, SDHCI_TEGRA_VENDOR_CAP_OVERRIDES);
  525. val &= ~SDHCI_TEGRA_CAP_OVERRIDES_DQS_TRIM_MASK;
  526. val |= trim << SDHCI_TEGRA_CAP_OVERRIDES_DQS_TRIM_SHIFT;
  527. sdhci_writel(host, val, SDHCI_TEGRA_VENDOR_CAP_OVERRIDES);
  528. }
  529. static void tegra_sdhci_hs400_dll_cal(struct sdhci_host *host)
  530. {
  531. u32 reg;
  532. int err;
  533. reg = sdhci_readl(host, SDHCI_TEGRA_VENDOR_DLLCAL_CFG);
  534. reg |= SDHCI_TEGRA_DLLCAL_CALIBRATE;
  535. sdhci_writel(host, reg, SDHCI_TEGRA_VENDOR_DLLCAL_CFG);
  536. /* 1 ms sleep, 5 ms timeout */
  537. err = readl_poll_timeout(host->ioaddr + SDHCI_TEGRA_VENDOR_DLLCAL_STA,
  538. reg, !(reg & SDHCI_TEGRA_DLLCAL_STA_ACTIVE),
  539. 1000, 5000);
  540. if (err)
  541. dev_err(mmc_dev(host->mmc),
  542. "HS400 delay line calibration timed out\n");
  543. }
  544. static void tegra_sdhci_set_uhs_signaling(struct sdhci_host *host,
  545. unsigned timing)
  546. {
  547. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  548. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  549. bool set_default_tap = false;
  550. bool set_dqs_trim = false;
  551. bool do_hs400_dll_cal = false;
  552. switch (timing) {
  553. case MMC_TIMING_UHS_SDR50:
  554. case MMC_TIMING_UHS_SDR104:
  555. case MMC_TIMING_MMC_HS200:
  556. /* Don't set default tap on tunable modes. */
  557. break;
  558. case MMC_TIMING_MMC_HS400:
  559. set_dqs_trim = true;
  560. do_hs400_dll_cal = true;
  561. break;
  562. case MMC_TIMING_MMC_DDR52:
  563. case MMC_TIMING_UHS_DDR50:
  564. tegra_host->ddr_signaling = true;
  565. set_default_tap = true;
  566. break;
  567. default:
  568. set_default_tap = true;
  569. break;
  570. }
  571. sdhci_set_uhs_signaling(host, timing);
  572. tegra_sdhci_pad_autocalib(host);
  573. if (set_default_tap)
  574. tegra_sdhci_set_tap(host, tegra_host->default_tap);
  575. if (set_dqs_trim)
  576. tegra_sdhci_set_dqs_trim(host, tegra_host->dqs_trim);
  577. if (do_hs400_dll_cal)
  578. tegra_sdhci_hs400_dll_cal(host);
  579. }
  580. static int tegra_sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
  581. {
  582. unsigned int min, max;
  583. /*
  584. * Start search for minimum tap value at 10, as smaller values are
  585. * may wrongly be reported as working but fail at higher speeds,
  586. * according to the TRM.
  587. */
  588. min = 10;
  589. while (min < 255) {
  590. tegra_sdhci_set_tap(host, min);
  591. if (!mmc_send_tuning(host->mmc, opcode, NULL))
  592. break;
  593. min++;
  594. }
  595. /* Find the maximum tap value that still passes. */
  596. max = min + 1;
  597. while (max < 255) {
  598. tegra_sdhci_set_tap(host, max);
  599. if (mmc_send_tuning(host->mmc, opcode, NULL)) {
  600. max--;
  601. break;
  602. }
  603. max++;
  604. }
  605. /* The TRM states the ideal tap value is at 75% in the passing range. */
  606. tegra_sdhci_set_tap(host, min + ((max - min) * 3 / 4));
  607. return mmc_send_tuning(host->mmc, opcode, NULL);
  608. }
  609. static int tegra_sdhci_set_padctrl(struct sdhci_host *host, int voltage)
  610. {
  611. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  612. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  613. int ret;
  614. if (!tegra_host->pad_control_available)
  615. return 0;
  616. if (voltage == MMC_SIGNAL_VOLTAGE_180) {
  617. ret = pinctrl_select_state(tegra_host->pinctrl_sdmmc,
  618. tegra_host->pinctrl_state_1v8);
  619. if (ret < 0)
  620. dev_err(mmc_dev(host->mmc),
  621. "setting 1.8V failed, ret: %d\n", ret);
  622. } else {
  623. ret = pinctrl_select_state(tegra_host->pinctrl_sdmmc,
  624. tegra_host->pinctrl_state_3v3);
  625. if (ret < 0)
  626. dev_err(mmc_dev(host->mmc),
  627. "setting 3.3V failed, ret: %d\n", ret);
  628. }
  629. return ret;
  630. }
  631. static int sdhci_tegra_start_signal_voltage_switch(struct mmc_host *mmc,
  632. struct mmc_ios *ios)
  633. {
  634. struct sdhci_host *host = mmc_priv(mmc);
  635. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  636. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  637. int ret = 0;
  638. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  639. ret = tegra_sdhci_set_padctrl(host, ios->signal_voltage);
  640. if (ret < 0)
  641. return ret;
  642. ret = sdhci_start_signal_voltage_switch(mmc, ios);
  643. } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
  644. ret = sdhci_start_signal_voltage_switch(mmc, ios);
  645. if (ret < 0)
  646. return ret;
  647. ret = tegra_sdhci_set_padctrl(host, ios->signal_voltage);
  648. }
  649. if (tegra_host->pad_calib_required)
  650. tegra_sdhci_pad_autocalib(host);
  651. return ret;
  652. }
  653. static int tegra_sdhci_init_pinctrl_info(struct device *dev,
  654. struct sdhci_tegra *tegra_host)
  655. {
  656. tegra_host->pinctrl_sdmmc = devm_pinctrl_get(dev);
  657. if (IS_ERR(tegra_host->pinctrl_sdmmc)) {
  658. dev_dbg(dev, "No pinctrl info, err: %ld\n",
  659. PTR_ERR(tegra_host->pinctrl_sdmmc));
  660. return -1;
  661. }
  662. tegra_host->pinctrl_state_3v3 =
  663. pinctrl_lookup_state(tegra_host->pinctrl_sdmmc, "sdmmc-3v3");
  664. if (IS_ERR(tegra_host->pinctrl_state_3v3)) {
  665. dev_warn(dev, "Missing 3.3V pad state, err: %ld\n",
  666. PTR_ERR(tegra_host->pinctrl_state_3v3));
  667. return -1;
  668. }
  669. tegra_host->pinctrl_state_1v8 =
  670. pinctrl_lookup_state(tegra_host->pinctrl_sdmmc, "sdmmc-1v8");
  671. if (IS_ERR(tegra_host->pinctrl_state_1v8)) {
  672. dev_warn(dev, "Missing 1.8V pad state, err: %ld\n",
  673. PTR_ERR(tegra_host->pinctrl_state_1v8));
  674. return -1;
  675. }
  676. tegra_host->pad_control_available = true;
  677. return 0;
  678. }
  679. static void tegra_sdhci_voltage_switch(struct sdhci_host *host)
  680. {
  681. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  682. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  683. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  684. if (soc_data->nvquirks & NVQUIRK_HAS_PADCALIB)
  685. tegra_host->pad_calib_required = true;
  686. }
  687. static const struct sdhci_ops tegra_sdhci_ops = {
  688. .get_ro = tegra_sdhci_get_ro,
  689. .read_w = tegra_sdhci_readw,
  690. .write_l = tegra_sdhci_writel,
  691. .set_clock = tegra_sdhci_set_clock,
  692. .set_bus_width = sdhci_set_bus_width,
  693. .reset = tegra_sdhci_reset,
  694. .platform_execute_tuning = tegra_sdhci_execute_tuning,
  695. .set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
  696. .voltage_switch = tegra_sdhci_voltage_switch,
  697. .get_max_clock = tegra_sdhci_get_max_clock,
  698. };
  699. static const struct sdhci_pltfm_data sdhci_tegra20_pdata = {
  700. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  701. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  702. SDHCI_QUIRK_NO_HISPD_BIT |
  703. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
  704. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  705. .ops = &tegra_sdhci_ops,
  706. };
  707. static const struct sdhci_tegra_soc_data soc_data_tegra20 = {
  708. .pdata = &sdhci_tegra20_pdata,
  709. .nvquirks = NVQUIRK_FORCE_SDHCI_SPEC_200 |
  710. NVQUIRK_ENABLE_BLOCK_GAP_DET,
  711. };
  712. static const struct sdhci_pltfm_data sdhci_tegra30_pdata = {
  713. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  714. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  715. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  716. SDHCI_QUIRK_NO_HISPD_BIT |
  717. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
  718. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  719. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  720. SDHCI_QUIRK2_BROKEN_HS200 |
  721. /*
  722. * Auto-CMD23 leads to "Got command interrupt 0x00010000 even
  723. * though no command operation was in progress."
  724. *
  725. * The exact reason is unknown, as the same hardware seems
  726. * to support Auto CMD23 on a downstream 3.1 kernel.
  727. */
  728. SDHCI_QUIRK2_ACMD23_BROKEN,
  729. .ops = &tegra_sdhci_ops,
  730. };
  731. static const struct sdhci_tegra_soc_data soc_data_tegra30 = {
  732. .pdata = &sdhci_tegra30_pdata,
  733. .nvquirks = NVQUIRK_ENABLE_SDHCI_SPEC_300 |
  734. NVQUIRK_ENABLE_SDR50 |
  735. NVQUIRK_ENABLE_SDR104 |
  736. NVQUIRK_HAS_PADCALIB,
  737. };
  738. static const struct sdhci_ops tegra114_sdhci_ops = {
  739. .get_ro = tegra_sdhci_get_ro,
  740. .read_w = tegra_sdhci_readw,
  741. .write_w = tegra_sdhci_writew,
  742. .write_l = tegra_sdhci_writel,
  743. .set_clock = tegra_sdhci_set_clock,
  744. .set_bus_width = sdhci_set_bus_width,
  745. .reset = tegra_sdhci_reset,
  746. .platform_execute_tuning = tegra_sdhci_execute_tuning,
  747. .set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
  748. .voltage_switch = tegra_sdhci_voltage_switch,
  749. .get_max_clock = tegra_sdhci_get_max_clock,
  750. };
  751. static const struct sdhci_pltfm_data sdhci_tegra114_pdata = {
  752. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  753. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  754. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  755. SDHCI_QUIRK_NO_HISPD_BIT |
  756. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
  757. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  758. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  759. .ops = &tegra114_sdhci_ops,
  760. };
  761. static const struct sdhci_tegra_soc_data soc_data_tegra114 = {
  762. .pdata = &sdhci_tegra114_pdata,
  763. };
  764. static const struct sdhci_pltfm_data sdhci_tegra124_pdata = {
  765. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  766. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  767. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  768. SDHCI_QUIRK_NO_HISPD_BIT |
  769. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
  770. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  771. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  772. /*
  773. * The TRM states that the SD/MMC controller found on
  774. * Tegra124 can address 34 bits (the maximum supported by
  775. * the Tegra memory controller), but tests show that DMA
  776. * to or from above 4 GiB doesn't work. This is possibly
  777. * caused by missing programming, though it's not obvious
  778. * what sequence is required. Mark 64-bit DMA broken for
  779. * now to fix this for existing users (e.g. Nyan boards).
  780. */
  781. SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
  782. .ops = &tegra114_sdhci_ops,
  783. };
  784. static const struct sdhci_tegra_soc_data soc_data_tegra124 = {
  785. .pdata = &sdhci_tegra124_pdata,
  786. };
  787. static const struct sdhci_ops tegra210_sdhci_ops = {
  788. .get_ro = tegra_sdhci_get_ro,
  789. .read_w = tegra_sdhci_readw,
  790. .write_w = tegra210_sdhci_writew,
  791. .write_l = tegra_sdhci_writel,
  792. .set_clock = tegra_sdhci_set_clock,
  793. .set_bus_width = sdhci_set_bus_width,
  794. .reset = tegra_sdhci_reset,
  795. .set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
  796. .voltage_switch = tegra_sdhci_voltage_switch,
  797. .get_max_clock = tegra_sdhci_get_max_clock,
  798. };
  799. static const struct sdhci_pltfm_data sdhci_tegra210_pdata = {
  800. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  801. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  802. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  803. SDHCI_QUIRK_NO_HISPD_BIT |
  804. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
  805. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  806. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  807. .ops = &tegra210_sdhci_ops,
  808. };
  809. static const struct sdhci_tegra_soc_data soc_data_tegra210 = {
  810. .pdata = &sdhci_tegra210_pdata,
  811. .nvquirks = NVQUIRK_NEEDS_PAD_CONTROL |
  812. NVQUIRK_HAS_PADCALIB |
  813. NVQUIRK_DIS_CARD_CLK_CONFIG_TAP |
  814. NVQUIRK_ENABLE_SDR50 |
  815. NVQUIRK_ENABLE_SDR104,
  816. };
  817. static const struct sdhci_ops tegra186_sdhci_ops = {
  818. .get_ro = tegra_sdhci_get_ro,
  819. .read_w = tegra_sdhci_readw,
  820. .write_l = tegra_sdhci_writel,
  821. .set_clock = tegra_sdhci_set_clock,
  822. .set_bus_width = sdhci_set_bus_width,
  823. .reset = tegra_sdhci_reset,
  824. .set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
  825. .voltage_switch = tegra_sdhci_voltage_switch,
  826. .get_max_clock = tegra_sdhci_get_max_clock,
  827. };
  828. static const struct sdhci_pltfm_data sdhci_tegra186_pdata = {
  829. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  830. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  831. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  832. SDHCI_QUIRK_NO_HISPD_BIT |
  833. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
  834. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  835. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  836. /* SDHCI controllers on Tegra186 support 40-bit addressing.
  837. * IOVA addresses are 48-bit wide on Tegra186.
  838. * With 64-bit dma mask used for SDHCI, accesses can
  839. * be broken. Disable 64-bit dma, which would fall back
  840. * to 32-bit dma mask. Ideally 40-bit dma mask would work,
  841. * But it is not supported as of now.
  842. */
  843. SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
  844. .ops = &tegra186_sdhci_ops,
  845. };
  846. static const struct sdhci_tegra_soc_data soc_data_tegra186 = {
  847. .pdata = &sdhci_tegra186_pdata,
  848. .nvquirks = NVQUIRK_NEEDS_PAD_CONTROL |
  849. NVQUIRK_HAS_PADCALIB |
  850. NVQUIRK_DIS_CARD_CLK_CONFIG_TAP |
  851. NVQUIRK_ENABLE_SDR50 |
  852. NVQUIRK_ENABLE_SDR104,
  853. };
  854. static const struct of_device_id sdhci_tegra_dt_match[] = {
  855. { .compatible = "nvidia,tegra186-sdhci", .data = &soc_data_tegra186 },
  856. { .compatible = "nvidia,tegra210-sdhci", .data = &soc_data_tegra210 },
  857. { .compatible = "nvidia,tegra124-sdhci", .data = &soc_data_tegra124 },
  858. { .compatible = "nvidia,tegra114-sdhci", .data = &soc_data_tegra114 },
  859. { .compatible = "nvidia,tegra30-sdhci", .data = &soc_data_tegra30 },
  860. { .compatible = "nvidia,tegra20-sdhci", .data = &soc_data_tegra20 },
  861. {}
  862. };
  863. MODULE_DEVICE_TABLE(of, sdhci_tegra_dt_match);
  864. static int sdhci_tegra_probe(struct platform_device *pdev)
  865. {
  866. const struct of_device_id *match;
  867. const struct sdhci_tegra_soc_data *soc_data;
  868. struct sdhci_host *host;
  869. struct sdhci_pltfm_host *pltfm_host;
  870. struct sdhci_tegra *tegra_host;
  871. struct clk *clk;
  872. int rc;
  873. match = of_match_device(sdhci_tegra_dt_match, &pdev->dev);
  874. if (!match)
  875. return -EINVAL;
  876. soc_data = match->data;
  877. host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*tegra_host));
  878. if (IS_ERR(host))
  879. return PTR_ERR(host);
  880. pltfm_host = sdhci_priv(host);
  881. tegra_host = sdhci_pltfm_priv(pltfm_host);
  882. tegra_host->ddr_signaling = false;
  883. tegra_host->pad_calib_required = false;
  884. tegra_host->pad_control_available = false;
  885. tegra_host->soc_data = soc_data;
  886. if (soc_data->nvquirks & NVQUIRK_NEEDS_PAD_CONTROL) {
  887. rc = tegra_sdhci_init_pinctrl_info(&pdev->dev, tegra_host);
  888. if (rc == 0)
  889. host->mmc_host_ops.start_signal_voltage_switch =
  890. sdhci_tegra_start_signal_voltage_switch;
  891. }
  892. /* Hook to periodically rerun pad calibration */
  893. if (soc_data->nvquirks & NVQUIRK_HAS_PADCALIB)
  894. host->mmc_host_ops.request = tegra_sdhci_request;
  895. host->mmc_host_ops.hs400_enhanced_strobe =
  896. tegra_sdhci_hs400_enhanced_strobe;
  897. rc = mmc_of_parse(host->mmc);
  898. if (rc)
  899. goto err_parse_dt;
  900. if (tegra_host->soc_data->nvquirks & NVQUIRK_ENABLE_DDR50)
  901. host->mmc->caps |= MMC_CAP_1_8V_DDR;
  902. tegra_sdhci_parse_pad_autocal_dt(host);
  903. tegra_sdhci_parse_tap_and_trim(host);
  904. tegra_host->power_gpio = devm_gpiod_get_optional(&pdev->dev, "power",
  905. GPIOD_OUT_HIGH);
  906. if (IS_ERR(tegra_host->power_gpio)) {
  907. rc = PTR_ERR(tegra_host->power_gpio);
  908. goto err_power_req;
  909. }
  910. clk = devm_clk_get(mmc_dev(host->mmc), NULL);
  911. if (IS_ERR(clk)) {
  912. dev_err(mmc_dev(host->mmc), "clk err\n");
  913. rc = PTR_ERR(clk);
  914. goto err_clk_get;
  915. }
  916. clk_prepare_enable(clk);
  917. pltfm_host->clk = clk;
  918. tegra_host->rst = devm_reset_control_get_exclusive(&pdev->dev,
  919. "sdhci");
  920. if (IS_ERR(tegra_host->rst)) {
  921. rc = PTR_ERR(tegra_host->rst);
  922. dev_err(&pdev->dev, "failed to get reset control: %d\n", rc);
  923. goto err_rst_get;
  924. }
  925. rc = reset_control_assert(tegra_host->rst);
  926. if (rc)
  927. goto err_rst_get;
  928. usleep_range(2000, 4000);
  929. rc = reset_control_deassert(tegra_host->rst);
  930. if (rc)
  931. goto err_rst_get;
  932. usleep_range(2000, 4000);
  933. rc = sdhci_add_host(host);
  934. if (rc)
  935. goto err_add_host;
  936. return 0;
  937. err_add_host:
  938. reset_control_assert(tegra_host->rst);
  939. err_rst_get:
  940. clk_disable_unprepare(pltfm_host->clk);
  941. err_clk_get:
  942. err_power_req:
  943. err_parse_dt:
  944. sdhci_pltfm_free(pdev);
  945. return rc;
  946. }
  947. static int sdhci_tegra_remove(struct platform_device *pdev)
  948. {
  949. struct sdhci_host *host = platform_get_drvdata(pdev);
  950. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  951. struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
  952. sdhci_remove_host(host, 0);
  953. reset_control_assert(tegra_host->rst);
  954. usleep_range(2000, 4000);
  955. clk_disable_unprepare(pltfm_host->clk);
  956. sdhci_pltfm_free(pdev);
  957. return 0;
  958. }
  959. static struct platform_driver sdhci_tegra_driver = {
  960. .driver = {
  961. .name = "sdhci-tegra",
  962. .of_match_table = sdhci_tegra_dt_match,
  963. .pm = &sdhci_pltfm_pmops,
  964. },
  965. .probe = sdhci_tegra_probe,
  966. .remove = sdhci_tegra_remove,
  967. };
  968. module_platform_driver(sdhci_tegra_driver);
  969. MODULE_DESCRIPTION("SDHCI driver for Tegra");
  970. MODULE_AUTHOR("Google, Inc.");
  971. MODULE_LICENSE("GPL v2");