sdhci-omap.c 24 KB

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