sdhci-omap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /**
  2. * SDHCI Controller driver for TI's OMAP SoCs
  3. *
  4. * Copyright (C) 2017 Texas Instruments
  5. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 of
  9. * the License as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/mmc/slot-gpio.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/pinctrl/consumer.h>
  28. #include <linux/sys_soc.h>
  29. #include "sdhci-pltfm.h"
  30. #define SDHCI_OMAP_CON 0x12c
  31. #define CON_DW8 BIT(5)
  32. #define CON_DMA_MASTER BIT(20)
  33. #define CON_DDR BIT(19)
  34. #define CON_CLKEXTFREE BIT(16)
  35. #define CON_PADEN BIT(15)
  36. #define CON_CTPL BIT(11)
  37. #define CON_INIT BIT(1)
  38. #define CON_OD BIT(0)
  39. #define SDHCI_OMAP_DLL 0x0134
  40. #define DLL_SWT BIT(20)
  41. #define DLL_FORCE_SR_C_SHIFT 13
  42. #define DLL_FORCE_SR_C_MASK (0x7f << DLL_FORCE_SR_C_SHIFT)
  43. #define DLL_FORCE_VALUE BIT(12)
  44. #define DLL_CALIB BIT(1)
  45. #define SDHCI_OMAP_CMD 0x20c
  46. #define SDHCI_OMAP_PSTATE 0x0224
  47. #define PSTATE_DLEV_DAT0 BIT(20)
  48. #define PSTATE_DATI BIT(1)
  49. #define SDHCI_OMAP_HCTL 0x228
  50. #define HCTL_SDBP BIT(8)
  51. #define HCTL_SDVS_SHIFT 9
  52. #define HCTL_SDVS_MASK (0x7 << HCTL_SDVS_SHIFT)
  53. #define HCTL_SDVS_33 (0x7 << HCTL_SDVS_SHIFT)
  54. #define HCTL_SDVS_30 (0x6 << HCTL_SDVS_SHIFT)
  55. #define HCTL_SDVS_18 (0x5 << HCTL_SDVS_SHIFT)
  56. #define SDHCI_OMAP_SYSCTL 0x22c
  57. #define SYSCTL_CEN BIT(2)
  58. #define SYSCTL_CLKD_SHIFT 6
  59. #define SYSCTL_CLKD_MASK 0x3ff
  60. #define SDHCI_OMAP_STAT 0x230
  61. #define SDHCI_OMAP_IE 0x234
  62. #define INT_CC_EN BIT(0)
  63. #define SDHCI_OMAP_AC12 0x23c
  64. #define AC12_V1V8_SIGEN BIT(19)
  65. #define AC12_SCLK_SEL BIT(23)
  66. #define SDHCI_OMAP_CAPA 0x240
  67. #define CAPA_VS33 BIT(24)
  68. #define CAPA_VS30 BIT(25)
  69. #define CAPA_VS18 BIT(26)
  70. #define SDHCI_OMAP_CAPA2 0x0244
  71. #define CAPA2_TSDR50 BIT(13)
  72. #define SDHCI_OMAP_TIMEOUT 1 /* 1 msec */
  73. #define SYSCTL_CLKD_MAX 0x3FF
  74. #define IOV_1V8 1800000 /* 180000 uV */
  75. #define IOV_3V0 3000000 /* 300000 uV */
  76. #define IOV_3V3 3300000 /* 330000 uV */
  77. #define MAX_PHASE_DELAY 0x7C
  78. /* sdhci-omap controller flags */
  79. #define SDHCI_OMAP_REQUIRE_IODELAY BIT(0)
  80. struct sdhci_omap_data {
  81. u32 offset;
  82. u8 flags;
  83. };
  84. struct sdhci_omap_host {
  85. char *version;
  86. void __iomem *base;
  87. struct device *dev;
  88. struct regulator *pbias;
  89. bool pbias_enabled;
  90. struct sdhci_host *host;
  91. u8 bus_mode;
  92. u8 power_mode;
  93. u8 timing;
  94. u8 flags;
  95. struct pinctrl *pinctrl;
  96. struct pinctrl_state **pinctrl_state;
  97. };
  98. static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host);
  99. static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host);
  100. static inline u32 sdhci_omap_readl(struct sdhci_omap_host *host,
  101. unsigned int offset)
  102. {
  103. return readl(host->base + offset);
  104. }
  105. static inline void sdhci_omap_writel(struct sdhci_omap_host *host,
  106. unsigned int offset, u32 data)
  107. {
  108. writel(data, host->base + offset);
  109. }
  110. static int sdhci_omap_set_pbias(struct sdhci_omap_host *omap_host,
  111. bool power_on, unsigned int iov)
  112. {
  113. int ret;
  114. struct device *dev = omap_host->dev;
  115. if (IS_ERR(omap_host->pbias))
  116. return 0;
  117. if (power_on) {
  118. ret = regulator_set_voltage(omap_host->pbias, iov, iov);
  119. if (ret) {
  120. dev_err(dev, "pbias set voltage failed\n");
  121. return ret;
  122. }
  123. if (omap_host->pbias_enabled)
  124. return 0;
  125. ret = regulator_enable(omap_host->pbias);
  126. if (ret) {
  127. dev_err(dev, "pbias reg enable fail\n");
  128. return ret;
  129. }
  130. omap_host->pbias_enabled = true;
  131. } else {
  132. if (!omap_host->pbias_enabled)
  133. return 0;
  134. ret = regulator_disable(omap_host->pbias);
  135. if (ret) {
  136. dev_err(dev, "pbias reg disable fail\n");
  137. return ret;
  138. }
  139. omap_host->pbias_enabled = false;
  140. }
  141. return 0;
  142. }
  143. static int sdhci_omap_enable_iov(struct sdhci_omap_host *omap_host,
  144. unsigned int iov)
  145. {
  146. int ret;
  147. struct sdhci_host *host = omap_host->host;
  148. struct mmc_host *mmc = host->mmc;
  149. ret = sdhci_omap_set_pbias(omap_host, false, 0);
  150. if (ret)
  151. return ret;
  152. if (!IS_ERR(mmc->supply.vqmmc)) {
  153. ret = regulator_set_voltage(mmc->supply.vqmmc, iov, iov);
  154. if (ret) {
  155. dev_err(mmc_dev(mmc), "vqmmc set voltage failed\n");
  156. return ret;
  157. }
  158. }
  159. ret = sdhci_omap_set_pbias(omap_host, true, iov);
  160. if (ret)
  161. return ret;
  162. return 0;
  163. }
  164. static void sdhci_omap_conf_bus_power(struct sdhci_omap_host *omap_host,
  165. unsigned char signal_voltage)
  166. {
  167. u32 reg;
  168. ktime_t timeout;
  169. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL);
  170. reg &= ~HCTL_SDVS_MASK;
  171. if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  172. reg |= HCTL_SDVS_33;
  173. else
  174. reg |= HCTL_SDVS_18;
  175. sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
  176. reg |= HCTL_SDBP;
  177. sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
  178. /* wait 1ms */
  179. timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
  180. while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL) & HCTL_SDBP)) {
  181. if (WARN_ON(ktime_after(ktime_get(), timeout)))
  182. return;
  183. usleep_range(5, 10);
  184. }
  185. }
  186. static void sdhci_omap_enable_sdio_irq(struct mmc_host *mmc, int enable)
  187. {
  188. struct sdhci_host *host = mmc_priv(mmc);
  189. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  190. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  191. u32 reg;
  192. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  193. if (enable)
  194. reg |= (CON_CTPL | CON_CLKEXTFREE);
  195. else
  196. reg &= ~(CON_CTPL | CON_CLKEXTFREE);
  197. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  198. sdhci_enable_sdio_irq(mmc, enable);
  199. }
  200. static inline void sdhci_omap_set_dll(struct sdhci_omap_host *omap_host,
  201. int count)
  202. {
  203. int i;
  204. u32 reg;
  205. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
  206. reg |= DLL_FORCE_VALUE;
  207. reg &= ~DLL_FORCE_SR_C_MASK;
  208. reg |= (count << DLL_FORCE_SR_C_SHIFT);
  209. sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
  210. reg |= DLL_CALIB;
  211. sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
  212. for (i = 0; i < 1000; i++) {
  213. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
  214. if (reg & DLL_CALIB)
  215. break;
  216. }
  217. reg &= ~DLL_CALIB;
  218. sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
  219. }
  220. static void sdhci_omap_disable_tuning(struct sdhci_omap_host *omap_host)
  221. {
  222. u32 reg;
  223. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  224. reg &= ~AC12_SCLK_SEL;
  225. sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
  226. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
  227. reg &= ~(DLL_FORCE_VALUE | DLL_SWT);
  228. sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
  229. }
  230. static int sdhci_omap_execute_tuning(struct mmc_host *mmc, u32 opcode)
  231. {
  232. struct sdhci_host *host = mmc_priv(mmc);
  233. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  234. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  235. struct device *dev = omap_host->dev;
  236. struct mmc_ios *ios = &mmc->ios;
  237. u32 start_window = 0, max_window = 0;
  238. bool dcrc_was_enabled = false;
  239. u8 cur_match, prev_match = 0;
  240. u32 length = 0, max_len = 0;
  241. u32 phase_delay = 0;
  242. int ret = 0;
  243. u32 reg;
  244. pltfm_host = sdhci_priv(host);
  245. omap_host = sdhci_pltfm_priv(pltfm_host);
  246. dev = omap_host->dev;
  247. /* clock tuning is not needed for upto 52MHz */
  248. if (ios->clock <= 52000000)
  249. return 0;
  250. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA2);
  251. if (ios->timing == MMC_TIMING_UHS_SDR50 && !(reg & CAPA2_TSDR50))
  252. return 0;
  253. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
  254. reg |= DLL_SWT;
  255. sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
  256. /*
  257. * OMAP5/DRA74X/DRA72x Errata i802:
  258. * DCRC error interrupts (MMCHS_STAT[21] DCRC=0x1) can occur
  259. * during the tuning procedure. So disable it during the
  260. * tuning procedure.
  261. */
  262. if (host->ier & SDHCI_INT_DATA_CRC) {
  263. host->ier &= ~SDHCI_INT_DATA_CRC;
  264. dcrc_was_enabled = true;
  265. }
  266. while (phase_delay <= MAX_PHASE_DELAY) {
  267. sdhci_omap_set_dll(omap_host, phase_delay);
  268. cur_match = !mmc_send_tuning(mmc, opcode, NULL);
  269. if (cur_match) {
  270. if (prev_match) {
  271. length++;
  272. } else {
  273. start_window = phase_delay;
  274. length = 1;
  275. }
  276. }
  277. if (length > max_len) {
  278. max_window = start_window;
  279. max_len = length;
  280. }
  281. prev_match = cur_match;
  282. phase_delay += 4;
  283. }
  284. if (!max_len) {
  285. dev_err(dev, "Unable to find match\n");
  286. ret = -EIO;
  287. goto tuning_error;
  288. }
  289. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  290. if (!(reg & AC12_SCLK_SEL)) {
  291. ret = -EIO;
  292. goto tuning_error;
  293. }
  294. phase_delay = max_window + 4 * (max_len >> 1);
  295. sdhci_omap_set_dll(omap_host, phase_delay);
  296. goto ret;
  297. tuning_error:
  298. dev_err(dev, "Tuning failed\n");
  299. sdhci_omap_disable_tuning(omap_host);
  300. ret:
  301. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  302. /* Reenable forbidden interrupt */
  303. if (dcrc_was_enabled)
  304. host->ier |= SDHCI_INT_DATA_CRC;
  305. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  306. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  307. return ret;
  308. }
  309. static int sdhci_omap_card_busy(struct mmc_host *mmc)
  310. {
  311. u32 reg, ac12;
  312. int ret = false;
  313. struct sdhci_host *host = mmc_priv(mmc);
  314. struct sdhci_pltfm_host *pltfm_host;
  315. struct sdhci_omap_host *omap_host;
  316. u32 ier = host->ier;
  317. pltfm_host = sdhci_priv(host);
  318. omap_host = sdhci_pltfm_priv(pltfm_host);
  319. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  320. ac12 = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  321. reg &= ~CON_CLKEXTFREE;
  322. if (ac12 & AC12_V1V8_SIGEN)
  323. reg |= CON_CLKEXTFREE;
  324. reg |= CON_PADEN;
  325. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  326. disable_irq(host->irq);
  327. ier |= SDHCI_INT_CARD_INT;
  328. sdhci_writel(host, ier, SDHCI_INT_ENABLE);
  329. sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  330. /*
  331. * Delay is required for PSTATE to correctly reflect
  332. * DLEV/CLEV values after PADEN is set.
  333. */
  334. usleep_range(50, 100);
  335. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_PSTATE);
  336. if ((reg & PSTATE_DATI) || !(reg & PSTATE_DLEV_DAT0))
  337. ret = true;
  338. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  339. reg &= ~(CON_CLKEXTFREE | CON_PADEN);
  340. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  341. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  342. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  343. enable_irq(host->irq);
  344. return ret;
  345. }
  346. static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc,
  347. struct mmc_ios *ios)
  348. {
  349. u32 reg;
  350. int ret;
  351. unsigned int iov;
  352. struct sdhci_host *host = mmc_priv(mmc);
  353. struct sdhci_pltfm_host *pltfm_host;
  354. struct sdhci_omap_host *omap_host;
  355. struct device *dev;
  356. pltfm_host = sdhci_priv(host);
  357. omap_host = sdhci_pltfm_priv(pltfm_host);
  358. dev = omap_host->dev;
  359. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  360. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  361. if (!(reg & CAPA_VS33))
  362. return -EOPNOTSUPP;
  363. sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
  364. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  365. reg &= ~AC12_V1V8_SIGEN;
  366. sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
  367. iov = IOV_3V3;
  368. } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
  369. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  370. if (!(reg & CAPA_VS18))
  371. return -EOPNOTSUPP;
  372. sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
  373. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  374. reg |= AC12_V1V8_SIGEN;
  375. sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
  376. iov = IOV_1V8;
  377. } else {
  378. return -EOPNOTSUPP;
  379. }
  380. ret = sdhci_omap_enable_iov(omap_host, iov);
  381. if (ret) {
  382. dev_err(dev, "failed to switch IO voltage to %dmV\n", iov);
  383. return ret;
  384. }
  385. dev_dbg(dev, "IO voltage switched to %dmV\n", iov);
  386. return 0;
  387. }
  388. static void sdhci_omap_set_timing(struct sdhci_omap_host *omap_host, u8 timing)
  389. {
  390. int ret;
  391. struct pinctrl_state *pinctrl_state;
  392. struct device *dev = omap_host->dev;
  393. if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
  394. return;
  395. if (omap_host->timing == timing)
  396. return;
  397. sdhci_omap_stop_clock(omap_host);
  398. pinctrl_state = omap_host->pinctrl_state[timing];
  399. ret = pinctrl_select_state(omap_host->pinctrl, pinctrl_state);
  400. if (ret) {
  401. dev_err(dev, "failed to select pinctrl state\n");
  402. return;
  403. }
  404. sdhci_omap_start_clock(omap_host);
  405. omap_host->timing = timing;
  406. }
  407. static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host,
  408. u8 power_mode)
  409. {
  410. if (omap_host->bus_mode == MMC_POWER_OFF)
  411. sdhci_omap_disable_tuning(omap_host);
  412. omap_host->power_mode = power_mode;
  413. }
  414. static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host,
  415. unsigned int mode)
  416. {
  417. u32 reg;
  418. if (omap_host->bus_mode == mode)
  419. return;
  420. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  421. if (mode == MMC_BUSMODE_OPENDRAIN)
  422. reg |= CON_OD;
  423. else
  424. reg &= ~CON_OD;
  425. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  426. omap_host->bus_mode = mode;
  427. }
  428. static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  429. {
  430. struct sdhci_host *host = mmc_priv(mmc);
  431. struct sdhci_pltfm_host *pltfm_host;
  432. struct sdhci_omap_host *omap_host;
  433. pltfm_host = sdhci_priv(host);
  434. omap_host = sdhci_pltfm_priv(pltfm_host);
  435. sdhci_omap_set_bus_mode(omap_host, ios->bus_mode);
  436. sdhci_omap_set_timing(omap_host, ios->timing);
  437. sdhci_set_ios(mmc, ios);
  438. sdhci_omap_set_power_mode(omap_host, ios->power_mode);
  439. }
  440. static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host,
  441. unsigned int clock)
  442. {
  443. u16 dsor;
  444. dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock);
  445. if (dsor > SYSCTL_CLKD_MAX)
  446. dsor = SYSCTL_CLKD_MAX;
  447. return dsor;
  448. }
  449. static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host)
  450. {
  451. u32 reg;
  452. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
  453. reg |= SYSCTL_CEN;
  454. sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
  455. }
  456. static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host)
  457. {
  458. u32 reg;
  459. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
  460. reg &= ~SYSCTL_CEN;
  461. sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
  462. }
  463. static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock)
  464. {
  465. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  466. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  467. unsigned long clkdiv;
  468. sdhci_omap_stop_clock(omap_host);
  469. if (!clock)
  470. return;
  471. clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock);
  472. clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT;
  473. sdhci_enable_clk(host, clkdiv);
  474. sdhci_omap_start_clock(omap_host);
  475. }
  476. static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode,
  477. unsigned short vdd)
  478. {
  479. struct mmc_host *mmc = host->mmc;
  480. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  481. }
  482. static int sdhci_omap_enable_dma(struct sdhci_host *host)
  483. {
  484. u32 reg;
  485. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  486. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  487. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  488. reg |= CON_DMA_MASTER;
  489. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  490. return 0;
  491. }
  492. static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host)
  493. {
  494. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  495. return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX;
  496. }
  497. static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width)
  498. {
  499. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  500. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  501. u32 reg;
  502. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  503. if (width == MMC_BUS_WIDTH_8)
  504. reg |= CON_DW8;
  505. else
  506. reg &= ~CON_DW8;
  507. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  508. sdhci_set_bus_width(host, width);
  509. }
  510. static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode)
  511. {
  512. u32 reg;
  513. ktime_t timeout;
  514. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  515. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  516. if (omap_host->power_mode == power_mode)
  517. return;
  518. if (power_mode != MMC_POWER_ON)
  519. return;
  520. disable_irq(host->irq);
  521. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  522. reg |= CON_INIT;
  523. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  524. sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0);
  525. /* wait 1ms */
  526. timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
  527. while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)) {
  528. if (WARN_ON(ktime_after(ktime_get(), timeout)))
  529. return;
  530. usleep_range(5, 10);
  531. }
  532. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  533. reg &= ~CON_INIT;
  534. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  535. sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN);
  536. enable_irq(host->irq);
  537. }
  538. static void sdhci_omap_set_uhs_signaling(struct sdhci_host *host,
  539. unsigned int timing)
  540. {
  541. u32 reg;
  542. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  543. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  544. sdhci_omap_stop_clock(omap_host);
  545. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  546. if (timing == MMC_TIMING_UHS_DDR50 || timing == MMC_TIMING_MMC_DDR52)
  547. reg |= CON_DDR;
  548. else
  549. reg &= ~CON_DDR;
  550. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  551. sdhci_set_uhs_signaling(host, timing);
  552. sdhci_omap_start_clock(omap_host);
  553. }
  554. static struct sdhci_ops sdhci_omap_ops = {
  555. .set_clock = sdhci_omap_set_clock,
  556. .set_power = sdhci_omap_set_power,
  557. .enable_dma = sdhci_omap_enable_dma,
  558. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  559. .get_min_clock = sdhci_omap_get_min_clock,
  560. .set_bus_width = sdhci_omap_set_bus_width,
  561. .platform_send_init_74_clocks = sdhci_omap_init_74_clocks,
  562. .reset = sdhci_reset,
  563. .set_uhs_signaling = sdhci_omap_set_uhs_signaling,
  564. };
  565. static int sdhci_omap_set_capabilities(struct sdhci_omap_host *omap_host)
  566. {
  567. u32 reg;
  568. int ret = 0;
  569. struct device *dev = omap_host->dev;
  570. struct regulator *vqmmc;
  571. vqmmc = regulator_get(dev, "vqmmc");
  572. if (IS_ERR(vqmmc)) {
  573. ret = PTR_ERR(vqmmc);
  574. goto reg_put;
  575. }
  576. /* voltage capabilities might be set by boot loader, clear it */
  577. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  578. reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33);
  579. if (regulator_is_supported_voltage(vqmmc, IOV_3V3, IOV_3V3))
  580. reg |= CAPA_VS33;
  581. if (regulator_is_supported_voltage(vqmmc, IOV_1V8, IOV_1V8))
  582. reg |= CAPA_VS18;
  583. sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg);
  584. reg_put:
  585. regulator_put(vqmmc);
  586. return ret;
  587. }
  588. static const struct sdhci_pltfm_data sdhci_omap_pdata = {
  589. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  590. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  591. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  592. SDHCI_QUIRK_NO_HISPD_BIT |
  593. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
  594. .quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN |
  595. SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  596. SDHCI_QUIRK2_RSP_136_HAS_CRC |
  597. SDHCI_QUIRK2_DISABLE_HW_TIMEOUT,
  598. .ops = &sdhci_omap_ops,
  599. };
  600. static const struct sdhci_omap_data k2g_data = {
  601. .offset = 0x200,
  602. };
  603. static const struct sdhci_omap_data dra7_data = {
  604. .offset = 0x200,
  605. .flags = SDHCI_OMAP_REQUIRE_IODELAY,
  606. };
  607. static const struct of_device_id omap_sdhci_match[] = {
  608. { .compatible = "ti,dra7-sdhci", .data = &dra7_data },
  609. { .compatible = "ti,k2g-sdhci", .data = &k2g_data },
  610. {},
  611. };
  612. MODULE_DEVICE_TABLE(of, omap_sdhci_match);
  613. static struct pinctrl_state
  614. *sdhci_omap_iodelay_pinctrl_state(struct sdhci_omap_host *omap_host, char *mode,
  615. u32 *caps, u32 capmask)
  616. {
  617. struct device *dev = omap_host->dev;
  618. char *version = omap_host->version;
  619. struct pinctrl_state *pinctrl_state = ERR_PTR(-ENODEV);
  620. char str[20];
  621. if (!(*caps & capmask))
  622. goto ret;
  623. if (version) {
  624. snprintf(str, 20, "%s-%s", mode, version);
  625. pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, str);
  626. }
  627. if (IS_ERR(pinctrl_state))
  628. pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, mode);
  629. if (IS_ERR(pinctrl_state)) {
  630. dev_err(dev, "no pinctrl state for %s mode", mode);
  631. *caps &= ~capmask;
  632. }
  633. ret:
  634. return pinctrl_state;
  635. }
  636. static int sdhci_omap_config_iodelay_pinctrl_state(struct sdhci_omap_host
  637. *omap_host)
  638. {
  639. struct device *dev = omap_host->dev;
  640. struct sdhci_host *host = omap_host->host;
  641. struct mmc_host *mmc = host->mmc;
  642. u32 *caps = &mmc->caps;
  643. u32 *caps2 = &mmc->caps2;
  644. struct pinctrl_state *state;
  645. struct pinctrl_state **pinctrl_state;
  646. if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
  647. return 0;
  648. pinctrl_state = devm_kcalloc(dev,
  649. MMC_TIMING_MMC_HS200 + 1,
  650. sizeof(*pinctrl_state),
  651. GFP_KERNEL);
  652. if (!pinctrl_state)
  653. return -ENOMEM;
  654. omap_host->pinctrl = devm_pinctrl_get(omap_host->dev);
  655. if (IS_ERR(omap_host->pinctrl)) {
  656. dev_err(dev, "Cannot get pinctrl\n");
  657. return PTR_ERR(omap_host->pinctrl);
  658. }
  659. state = pinctrl_lookup_state(omap_host->pinctrl, "default");
  660. if (IS_ERR(state)) {
  661. dev_err(dev, "no pinctrl state for default mode\n");
  662. return PTR_ERR(state);
  663. }
  664. pinctrl_state[MMC_TIMING_LEGACY] = state;
  665. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr104", caps,
  666. MMC_CAP_UHS_SDR104);
  667. if (!IS_ERR(state))
  668. pinctrl_state[MMC_TIMING_UHS_SDR104] = state;
  669. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr50", caps,
  670. MMC_CAP_UHS_DDR50);
  671. if (!IS_ERR(state))
  672. pinctrl_state[MMC_TIMING_UHS_DDR50] = state;
  673. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr50", caps,
  674. MMC_CAP_UHS_SDR50);
  675. if (!IS_ERR(state))
  676. pinctrl_state[MMC_TIMING_UHS_SDR50] = state;
  677. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr25", caps,
  678. MMC_CAP_UHS_SDR25);
  679. if (!IS_ERR(state))
  680. pinctrl_state[MMC_TIMING_UHS_SDR25] = state;
  681. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr12", caps,
  682. MMC_CAP_UHS_SDR12);
  683. if (!IS_ERR(state))
  684. pinctrl_state[MMC_TIMING_UHS_SDR12] = state;
  685. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_1_8v", caps,
  686. MMC_CAP_1_8V_DDR);
  687. if (!IS_ERR(state)) {
  688. pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
  689. } else {
  690. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_3_3v",
  691. caps,
  692. MMC_CAP_3_3V_DDR);
  693. if (!IS_ERR(state))
  694. pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
  695. }
  696. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
  697. MMC_CAP_SD_HIGHSPEED);
  698. if (!IS_ERR(state))
  699. pinctrl_state[MMC_TIMING_SD_HS] = state;
  700. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
  701. MMC_CAP_MMC_HIGHSPEED);
  702. if (!IS_ERR(state))
  703. pinctrl_state[MMC_TIMING_MMC_HS] = state;
  704. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs200_1_8v", caps2,
  705. MMC_CAP2_HS200_1_8V_SDR);
  706. if (!IS_ERR(state))
  707. pinctrl_state[MMC_TIMING_MMC_HS200] = state;
  708. omap_host->pinctrl_state = pinctrl_state;
  709. return 0;
  710. }
  711. static const struct soc_device_attribute sdhci_omap_soc_devices[] = {
  712. {
  713. .machine = "DRA7[45]*",
  714. .revision = "ES1.[01]",
  715. },
  716. {
  717. /* sentinel */
  718. }
  719. };
  720. static int sdhci_omap_probe(struct platform_device *pdev)
  721. {
  722. int ret;
  723. u32 offset;
  724. struct device *dev = &pdev->dev;
  725. struct sdhci_host *host;
  726. struct sdhci_pltfm_host *pltfm_host;
  727. struct sdhci_omap_host *omap_host;
  728. struct mmc_host *mmc;
  729. const struct of_device_id *match;
  730. struct sdhci_omap_data *data;
  731. const struct soc_device_attribute *soc;
  732. match = of_match_device(omap_sdhci_match, dev);
  733. if (!match)
  734. return -EINVAL;
  735. data = (struct sdhci_omap_data *)match->data;
  736. if (!data) {
  737. dev_err(dev, "no sdhci omap data\n");
  738. return -EINVAL;
  739. }
  740. offset = data->offset;
  741. host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata,
  742. sizeof(*omap_host));
  743. if (IS_ERR(host)) {
  744. dev_err(dev, "Failed sdhci_pltfm_init\n");
  745. return PTR_ERR(host);
  746. }
  747. pltfm_host = sdhci_priv(host);
  748. omap_host = sdhci_pltfm_priv(pltfm_host);
  749. omap_host->host = host;
  750. omap_host->base = host->ioaddr;
  751. omap_host->dev = dev;
  752. omap_host->power_mode = MMC_POWER_UNDEFINED;
  753. omap_host->timing = MMC_TIMING_LEGACY;
  754. omap_host->flags = data->flags;
  755. host->ioaddr += offset;
  756. mmc = host->mmc;
  757. sdhci_get_of_property(pdev);
  758. ret = mmc_of_parse(mmc);
  759. if (ret)
  760. goto err_pltfm_free;
  761. soc = soc_device_match(sdhci_omap_soc_devices);
  762. if (soc) {
  763. omap_host->version = "rev11";
  764. if (!strcmp(dev_name(dev), "4809c000.mmc"))
  765. mmc->f_max = 96000000;
  766. if (!strcmp(dev_name(dev), "480b4000.mmc"))
  767. mmc->f_max = 48000000;
  768. if (!strcmp(dev_name(dev), "480ad000.mmc"))
  769. mmc->f_max = 48000000;
  770. }
  771. pltfm_host->clk = devm_clk_get(dev, "fck");
  772. if (IS_ERR(pltfm_host->clk)) {
  773. ret = PTR_ERR(pltfm_host->clk);
  774. goto err_pltfm_free;
  775. }
  776. ret = clk_set_rate(pltfm_host->clk, mmc->f_max);
  777. if (ret) {
  778. dev_err(dev, "failed to set clock to %d\n", mmc->f_max);
  779. goto err_pltfm_free;
  780. }
  781. omap_host->pbias = devm_regulator_get_optional(dev, "pbias");
  782. if (IS_ERR(omap_host->pbias)) {
  783. ret = PTR_ERR(omap_host->pbias);
  784. if (ret != -ENODEV)
  785. goto err_pltfm_free;
  786. dev_dbg(dev, "unable to get pbias regulator %d\n", ret);
  787. }
  788. omap_host->pbias_enabled = false;
  789. /*
  790. * omap_device_pm_domain has callbacks to enable the main
  791. * functional clock, interface clock and also configure the
  792. * SYSCONFIG register of omap devices. The callback will be invoked
  793. * as part of pm_runtime_get_sync.
  794. */
  795. pm_runtime_enable(dev);
  796. ret = pm_runtime_get_sync(dev);
  797. if (ret < 0) {
  798. dev_err(dev, "pm_runtime_get_sync failed\n");
  799. pm_runtime_put_noidle(dev);
  800. goto err_rpm_disable;
  801. }
  802. ret = sdhci_omap_set_capabilities(omap_host);
  803. if (ret) {
  804. dev_err(dev, "failed to set system capabilities\n");
  805. goto err_put_sync;
  806. }
  807. host->mmc_host_ops.get_ro = mmc_gpio_get_ro;
  808. host->mmc_host_ops.start_signal_voltage_switch =
  809. sdhci_omap_start_signal_voltage_switch;
  810. host->mmc_host_ops.set_ios = sdhci_omap_set_ios;
  811. host->mmc_host_ops.card_busy = sdhci_omap_card_busy;
  812. host->mmc_host_ops.execute_tuning = sdhci_omap_execute_tuning;
  813. host->mmc_host_ops.enable_sdio_irq = sdhci_omap_enable_sdio_irq;
  814. ret = sdhci_setup_host(host);
  815. if (ret)
  816. goto err_put_sync;
  817. ret = sdhci_omap_config_iodelay_pinctrl_state(omap_host);
  818. if (ret)
  819. goto err_cleanup_host;
  820. ret = __sdhci_add_host(host);
  821. if (ret)
  822. goto err_cleanup_host;
  823. return 0;
  824. err_cleanup_host:
  825. sdhci_cleanup_host(host);
  826. err_put_sync:
  827. pm_runtime_put_sync(dev);
  828. err_rpm_disable:
  829. pm_runtime_disable(dev);
  830. err_pltfm_free:
  831. sdhci_pltfm_free(pdev);
  832. return ret;
  833. }
  834. static int sdhci_omap_remove(struct platform_device *pdev)
  835. {
  836. struct device *dev = &pdev->dev;
  837. struct sdhci_host *host = platform_get_drvdata(pdev);
  838. sdhci_remove_host(host, true);
  839. pm_runtime_put_sync(dev);
  840. pm_runtime_disable(dev);
  841. sdhci_pltfm_free(pdev);
  842. return 0;
  843. }
  844. static struct platform_driver sdhci_omap_driver = {
  845. .probe = sdhci_omap_probe,
  846. .remove = sdhci_omap_remove,
  847. .driver = {
  848. .name = "sdhci-omap",
  849. .of_match_table = omap_sdhci_match,
  850. },
  851. };
  852. module_platform_driver(sdhci_omap_driver);
  853. MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
  854. MODULE_AUTHOR("Texas Instruments Inc.");
  855. MODULE_LICENSE("GPL v2");
  856. MODULE_ALIAS("platform:sdhci_omap");