sdhci-omap.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. u8 cur_match, prev_match = 0;
  239. u32 length = 0, max_len = 0;
  240. u32 ier = host->ier;
  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. ier &= ~SDHCI_INT_DATA_CRC;
  263. sdhci_writel(host, ier, SDHCI_INT_ENABLE);
  264. sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  265. while (phase_delay <= MAX_PHASE_DELAY) {
  266. sdhci_omap_set_dll(omap_host, phase_delay);
  267. cur_match = !mmc_send_tuning(mmc, opcode, NULL);
  268. if (cur_match) {
  269. if (prev_match) {
  270. length++;
  271. } else {
  272. start_window = phase_delay;
  273. length = 1;
  274. }
  275. }
  276. if (length > max_len) {
  277. max_window = start_window;
  278. max_len = length;
  279. }
  280. prev_match = cur_match;
  281. phase_delay += 4;
  282. }
  283. if (!max_len) {
  284. dev_err(dev, "Unable to find match\n");
  285. ret = -EIO;
  286. goto tuning_error;
  287. }
  288. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  289. if (!(reg & AC12_SCLK_SEL)) {
  290. ret = -EIO;
  291. goto tuning_error;
  292. }
  293. phase_delay = max_window + 4 * (max_len >> 1);
  294. sdhci_omap_set_dll(omap_host, phase_delay);
  295. goto ret;
  296. tuning_error:
  297. dev_err(dev, "Tuning failed\n");
  298. sdhci_omap_disable_tuning(omap_host);
  299. ret:
  300. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  301. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  302. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  303. return ret;
  304. }
  305. static int sdhci_omap_card_busy(struct mmc_host *mmc)
  306. {
  307. u32 reg, ac12;
  308. int ret = false;
  309. struct sdhci_host *host = mmc_priv(mmc);
  310. struct sdhci_pltfm_host *pltfm_host;
  311. struct sdhci_omap_host *omap_host;
  312. u32 ier = host->ier;
  313. pltfm_host = sdhci_priv(host);
  314. omap_host = sdhci_pltfm_priv(pltfm_host);
  315. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  316. ac12 = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  317. reg &= ~CON_CLKEXTFREE;
  318. if (ac12 & AC12_V1V8_SIGEN)
  319. reg |= CON_CLKEXTFREE;
  320. reg |= CON_PADEN;
  321. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  322. disable_irq(host->irq);
  323. ier |= SDHCI_INT_CARD_INT;
  324. sdhci_writel(host, ier, SDHCI_INT_ENABLE);
  325. sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  326. /*
  327. * Delay is required for PSTATE to correctly reflect
  328. * DLEV/CLEV values after PADEN is set.
  329. */
  330. usleep_range(50, 100);
  331. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_PSTATE);
  332. if ((reg & PSTATE_DATI) || !(reg & PSTATE_DLEV_DAT0))
  333. ret = true;
  334. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  335. reg &= ~(CON_CLKEXTFREE | CON_PADEN);
  336. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  337. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  338. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  339. enable_irq(host->irq);
  340. return ret;
  341. }
  342. static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc,
  343. struct mmc_ios *ios)
  344. {
  345. u32 reg;
  346. int ret;
  347. unsigned int iov;
  348. struct sdhci_host *host = mmc_priv(mmc);
  349. struct sdhci_pltfm_host *pltfm_host;
  350. struct sdhci_omap_host *omap_host;
  351. struct device *dev;
  352. pltfm_host = sdhci_priv(host);
  353. omap_host = sdhci_pltfm_priv(pltfm_host);
  354. dev = omap_host->dev;
  355. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  356. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  357. if (!(reg & CAPA_VS33))
  358. return -EOPNOTSUPP;
  359. sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
  360. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  361. reg &= ~AC12_V1V8_SIGEN;
  362. sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
  363. iov = IOV_3V3;
  364. } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
  365. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  366. if (!(reg & CAPA_VS18))
  367. return -EOPNOTSUPP;
  368. sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
  369. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  370. reg |= AC12_V1V8_SIGEN;
  371. sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
  372. iov = IOV_1V8;
  373. } else {
  374. return -EOPNOTSUPP;
  375. }
  376. ret = sdhci_omap_enable_iov(omap_host, iov);
  377. if (ret) {
  378. dev_err(dev, "failed to switch IO voltage to %dmV\n", iov);
  379. return ret;
  380. }
  381. dev_dbg(dev, "IO voltage switched to %dmV\n", iov);
  382. return 0;
  383. }
  384. static void sdhci_omap_set_timing(struct sdhci_omap_host *omap_host, u8 timing)
  385. {
  386. int ret;
  387. struct pinctrl_state *pinctrl_state;
  388. struct device *dev = omap_host->dev;
  389. if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
  390. return;
  391. if (omap_host->timing == timing)
  392. return;
  393. sdhci_omap_stop_clock(omap_host);
  394. pinctrl_state = omap_host->pinctrl_state[timing];
  395. ret = pinctrl_select_state(omap_host->pinctrl, pinctrl_state);
  396. if (ret) {
  397. dev_err(dev, "failed to select pinctrl state\n");
  398. return;
  399. }
  400. sdhci_omap_start_clock(omap_host);
  401. omap_host->timing = timing;
  402. }
  403. static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host,
  404. u8 power_mode)
  405. {
  406. if (omap_host->bus_mode == MMC_POWER_OFF)
  407. sdhci_omap_disable_tuning(omap_host);
  408. omap_host->power_mode = power_mode;
  409. }
  410. static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host,
  411. unsigned int mode)
  412. {
  413. u32 reg;
  414. if (omap_host->bus_mode == mode)
  415. return;
  416. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  417. if (mode == MMC_BUSMODE_OPENDRAIN)
  418. reg |= CON_OD;
  419. else
  420. reg &= ~CON_OD;
  421. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  422. omap_host->bus_mode = mode;
  423. }
  424. static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  425. {
  426. struct sdhci_host *host = mmc_priv(mmc);
  427. struct sdhci_pltfm_host *pltfm_host;
  428. struct sdhci_omap_host *omap_host;
  429. pltfm_host = sdhci_priv(host);
  430. omap_host = sdhci_pltfm_priv(pltfm_host);
  431. sdhci_omap_set_bus_mode(omap_host, ios->bus_mode);
  432. sdhci_omap_set_timing(omap_host, ios->timing);
  433. sdhci_set_ios(mmc, ios);
  434. sdhci_omap_set_power_mode(omap_host, ios->power_mode);
  435. }
  436. static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host,
  437. unsigned int clock)
  438. {
  439. u16 dsor;
  440. dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock);
  441. if (dsor > SYSCTL_CLKD_MAX)
  442. dsor = SYSCTL_CLKD_MAX;
  443. return dsor;
  444. }
  445. static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host)
  446. {
  447. u32 reg;
  448. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
  449. reg |= SYSCTL_CEN;
  450. sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
  451. }
  452. static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host)
  453. {
  454. u32 reg;
  455. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
  456. reg &= ~SYSCTL_CEN;
  457. sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
  458. }
  459. static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock)
  460. {
  461. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  462. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  463. unsigned long clkdiv;
  464. sdhci_omap_stop_clock(omap_host);
  465. if (!clock)
  466. return;
  467. clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock);
  468. clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT;
  469. sdhci_enable_clk(host, clkdiv);
  470. sdhci_omap_start_clock(omap_host);
  471. }
  472. static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode,
  473. unsigned short vdd)
  474. {
  475. struct mmc_host *mmc = host->mmc;
  476. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  477. }
  478. static int sdhci_omap_enable_dma(struct sdhci_host *host)
  479. {
  480. u32 reg;
  481. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  482. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  483. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  484. reg |= CON_DMA_MASTER;
  485. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  486. return 0;
  487. }
  488. static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host)
  489. {
  490. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  491. return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX;
  492. }
  493. static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width)
  494. {
  495. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  496. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  497. u32 reg;
  498. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  499. if (width == MMC_BUS_WIDTH_8)
  500. reg |= CON_DW8;
  501. else
  502. reg &= ~CON_DW8;
  503. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  504. sdhci_set_bus_width(host, width);
  505. }
  506. static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode)
  507. {
  508. u32 reg;
  509. ktime_t timeout;
  510. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  511. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  512. if (omap_host->power_mode == power_mode)
  513. return;
  514. if (power_mode != MMC_POWER_ON)
  515. return;
  516. disable_irq(host->irq);
  517. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  518. reg |= CON_INIT;
  519. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  520. sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0);
  521. /* wait 1ms */
  522. timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
  523. while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)) {
  524. if (WARN_ON(ktime_after(ktime_get(), timeout)))
  525. return;
  526. usleep_range(5, 10);
  527. }
  528. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  529. reg &= ~CON_INIT;
  530. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  531. sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN);
  532. enable_irq(host->irq);
  533. }
  534. static void sdhci_omap_set_uhs_signaling(struct sdhci_host *host,
  535. unsigned int timing)
  536. {
  537. u32 reg;
  538. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  539. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  540. sdhci_omap_stop_clock(omap_host);
  541. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  542. if (timing == MMC_TIMING_UHS_DDR50 || timing == MMC_TIMING_MMC_DDR52)
  543. reg |= CON_DDR;
  544. else
  545. reg &= ~CON_DDR;
  546. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  547. sdhci_set_uhs_signaling(host, timing);
  548. sdhci_omap_start_clock(omap_host);
  549. }
  550. static struct sdhci_ops sdhci_omap_ops = {
  551. .set_clock = sdhci_omap_set_clock,
  552. .set_power = sdhci_omap_set_power,
  553. .enable_dma = sdhci_omap_enable_dma,
  554. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  555. .get_min_clock = sdhci_omap_get_min_clock,
  556. .set_bus_width = sdhci_omap_set_bus_width,
  557. .platform_send_init_74_clocks = sdhci_omap_init_74_clocks,
  558. .reset = sdhci_reset,
  559. .set_uhs_signaling = sdhci_omap_set_uhs_signaling,
  560. };
  561. static int sdhci_omap_set_capabilities(struct sdhci_omap_host *omap_host)
  562. {
  563. u32 reg;
  564. int ret = 0;
  565. struct device *dev = omap_host->dev;
  566. struct regulator *vqmmc;
  567. vqmmc = regulator_get(dev, "vqmmc");
  568. if (IS_ERR(vqmmc)) {
  569. ret = PTR_ERR(vqmmc);
  570. goto reg_put;
  571. }
  572. /* voltage capabilities might be set by boot loader, clear it */
  573. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  574. reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33);
  575. if (regulator_is_supported_voltage(vqmmc, IOV_3V3, IOV_3V3))
  576. reg |= CAPA_VS33;
  577. if (regulator_is_supported_voltage(vqmmc, IOV_1V8, IOV_1V8))
  578. reg |= CAPA_VS18;
  579. sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg);
  580. reg_put:
  581. regulator_put(vqmmc);
  582. return ret;
  583. }
  584. static const struct sdhci_pltfm_data sdhci_omap_pdata = {
  585. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  586. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  587. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  588. SDHCI_QUIRK_NO_HISPD_BIT |
  589. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
  590. .quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN |
  591. SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  592. SDHCI_QUIRK2_RSP_136_HAS_CRC |
  593. SDHCI_QUIRK2_DISABLE_HW_TIMEOUT,
  594. .ops = &sdhci_omap_ops,
  595. };
  596. static const struct sdhci_omap_data k2g_data = {
  597. .offset = 0x200,
  598. };
  599. static const struct sdhci_omap_data dra7_data = {
  600. .offset = 0x200,
  601. .flags = SDHCI_OMAP_REQUIRE_IODELAY,
  602. };
  603. static const struct of_device_id omap_sdhci_match[] = {
  604. { .compatible = "ti,dra7-sdhci", .data = &dra7_data },
  605. { .compatible = "ti,k2g-sdhci", .data = &k2g_data },
  606. {},
  607. };
  608. MODULE_DEVICE_TABLE(of, omap_sdhci_match);
  609. static struct pinctrl_state
  610. *sdhci_omap_iodelay_pinctrl_state(struct sdhci_omap_host *omap_host, char *mode,
  611. u32 *caps, u32 capmask)
  612. {
  613. struct device *dev = omap_host->dev;
  614. char *version = omap_host->version;
  615. struct pinctrl_state *pinctrl_state = ERR_PTR(-ENODEV);
  616. char str[20];
  617. if (!(*caps & capmask))
  618. goto ret;
  619. if (version) {
  620. snprintf(str, 20, "%s-%s", mode, version);
  621. pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, str);
  622. }
  623. if (IS_ERR(pinctrl_state))
  624. pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, mode);
  625. if (IS_ERR(pinctrl_state)) {
  626. dev_err(dev, "no pinctrl state for %s mode", mode);
  627. *caps &= ~capmask;
  628. }
  629. ret:
  630. return pinctrl_state;
  631. }
  632. static int sdhci_omap_config_iodelay_pinctrl_state(struct sdhci_omap_host
  633. *omap_host)
  634. {
  635. struct device *dev = omap_host->dev;
  636. struct sdhci_host *host = omap_host->host;
  637. struct mmc_host *mmc = host->mmc;
  638. u32 *caps = &mmc->caps;
  639. u32 *caps2 = &mmc->caps2;
  640. struct pinctrl_state *state;
  641. struct pinctrl_state **pinctrl_state;
  642. if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
  643. return 0;
  644. pinctrl_state = devm_kcalloc(dev,
  645. MMC_TIMING_MMC_HS200 + 1,
  646. sizeof(*pinctrl_state),
  647. GFP_KERNEL);
  648. if (!pinctrl_state)
  649. return -ENOMEM;
  650. omap_host->pinctrl = devm_pinctrl_get(omap_host->dev);
  651. if (IS_ERR(omap_host->pinctrl)) {
  652. dev_err(dev, "Cannot get pinctrl\n");
  653. return PTR_ERR(omap_host->pinctrl);
  654. }
  655. state = pinctrl_lookup_state(omap_host->pinctrl, "default");
  656. if (IS_ERR(state)) {
  657. dev_err(dev, "no pinctrl state for default mode\n");
  658. return PTR_ERR(state);
  659. }
  660. pinctrl_state[MMC_TIMING_LEGACY] = state;
  661. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr104", caps,
  662. MMC_CAP_UHS_SDR104);
  663. if (!IS_ERR(state))
  664. pinctrl_state[MMC_TIMING_UHS_SDR104] = state;
  665. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr50", caps,
  666. MMC_CAP_UHS_DDR50);
  667. if (!IS_ERR(state))
  668. pinctrl_state[MMC_TIMING_UHS_DDR50] = state;
  669. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr50", caps,
  670. MMC_CAP_UHS_SDR50);
  671. if (!IS_ERR(state))
  672. pinctrl_state[MMC_TIMING_UHS_SDR50] = state;
  673. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr25", caps,
  674. MMC_CAP_UHS_SDR25);
  675. if (!IS_ERR(state))
  676. pinctrl_state[MMC_TIMING_UHS_SDR25] = state;
  677. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr12", caps,
  678. MMC_CAP_UHS_SDR12);
  679. if (!IS_ERR(state))
  680. pinctrl_state[MMC_TIMING_UHS_SDR12] = state;
  681. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_1_8v", caps,
  682. MMC_CAP_1_8V_DDR);
  683. if (!IS_ERR(state)) {
  684. pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
  685. } else {
  686. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_3_3v",
  687. caps,
  688. MMC_CAP_3_3V_DDR);
  689. if (!IS_ERR(state))
  690. pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
  691. }
  692. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
  693. MMC_CAP_SD_HIGHSPEED);
  694. if (!IS_ERR(state))
  695. pinctrl_state[MMC_TIMING_SD_HS] = state;
  696. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
  697. MMC_CAP_MMC_HIGHSPEED);
  698. if (!IS_ERR(state))
  699. pinctrl_state[MMC_TIMING_MMC_HS] = state;
  700. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs200_1_8v", caps2,
  701. MMC_CAP2_HS200_1_8V_SDR);
  702. if (!IS_ERR(state))
  703. pinctrl_state[MMC_TIMING_MMC_HS200] = state;
  704. omap_host->pinctrl_state = pinctrl_state;
  705. return 0;
  706. }
  707. static const struct soc_device_attribute sdhci_omap_soc_devices[] = {
  708. {
  709. .machine = "DRA7[45]*",
  710. .revision = "ES1.[01]",
  711. },
  712. {
  713. /* sentinel */
  714. }
  715. };
  716. static int sdhci_omap_probe(struct platform_device *pdev)
  717. {
  718. int ret;
  719. u32 offset;
  720. struct device *dev = &pdev->dev;
  721. struct sdhci_host *host;
  722. struct sdhci_pltfm_host *pltfm_host;
  723. struct sdhci_omap_host *omap_host;
  724. struct mmc_host *mmc;
  725. const struct of_device_id *match;
  726. struct sdhci_omap_data *data;
  727. const struct soc_device_attribute *soc;
  728. match = of_match_device(omap_sdhci_match, dev);
  729. if (!match)
  730. return -EINVAL;
  731. data = (struct sdhci_omap_data *)match->data;
  732. if (!data) {
  733. dev_err(dev, "no sdhci omap data\n");
  734. return -EINVAL;
  735. }
  736. offset = data->offset;
  737. host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata,
  738. sizeof(*omap_host));
  739. if (IS_ERR(host)) {
  740. dev_err(dev, "Failed sdhci_pltfm_init\n");
  741. return PTR_ERR(host);
  742. }
  743. pltfm_host = sdhci_priv(host);
  744. omap_host = sdhci_pltfm_priv(pltfm_host);
  745. omap_host->host = host;
  746. omap_host->base = host->ioaddr;
  747. omap_host->dev = dev;
  748. omap_host->power_mode = MMC_POWER_UNDEFINED;
  749. omap_host->timing = MMC_TIMING_LEGACY;
  750. omap_host->flags = data->flags;
  751. host->ioaddr += offset;
  752. mmc = host->mmc;
  753. sdhci_get_of_property(pdev);
  754. ret = mmc_of_parse(mmc);
  755. if (ret)
  756. goto err_pltfm_free;
  757. soc = soc_device_match(sdhci_omap_soc_devices);
  758. if (soc) {
  759. omap_host->version = "rev11";
  760. if (!strcmp(dev_name(dev), "4809c000.mmc"))
  761. mmc->f_max = 96000000;
  762. if (!strcmp(dev_name(dev), "480b4000.mmc"))
  763. mmc->f_max = 48000000;
  764. if (!strcmp(dev_name(dev), "480ad000.mmc"))
  765. mmc->f_max = 48000000;
  766. }
  767. pltfm_host->clk = devm_clk_get(dev, "fck");
  768. if (IS_ERR(pltfm_host->clk)) {
  769. ret = PTR_ERR(pltfm_host->clk);
  770. goto err_pltfm_free;
  771. }
  772. ret = clk_set_rate(pltfm_host->clk, mmc->f_max);
  773. if (ret) {
  774. dev_err(dev, "failed to set clock to %d\n", mmc->f_max);
  775. goto err_pltfm_free;
  776. }
  777. omap_host->pbias = devm_regulator_get_optional(dev, "pbias");
  778. if (IS_ERR(omap_host->pbias)) {
  779. ret = PTR_ERR(omap_host->pbias);
  780. if (ret != -ENODEV)
  781. goto err_pltfm_free;
  782. dev_dbg(dev, "unable to get pbias regulator %d\n", ret);
  783. }
  784. omap_host->pbias_enabled = false;
  785. /*
  786. * omap_device_pm_domain has callbacks to enable the main
  787. * functional clock, interface clock and also configure the
  788. * SYSCONFIG register of omap devices. The callback will be invoked
  789. * as part of pm_runtime_get_sync.
  790. */
  791. pm_runtime_enable(dev);
  792. ret = pm_runtime_get_sync(dev);
  793. if (ret < 0) {
  794. dev_err(dev, "pm_runtime_get_sync failed\n");
  795. pm_runtime_put_noidle(dev);
  796. goto err_rpm_disable;
  797. }
  798. ret = sdhci_omap_set_capabilities(omap_host);
  799. if (ret) {
  800. dev_err(dev, "failed to set system capabilities\n");
  801. goto err_put_sync;
  802. }
  803. host->mmc_host_ops.get_ro = mmc_gpio_get_ro;
  804. host->mmc_host_ops.start_signal_voltage_switch =
  805. sdhci_omap_start_signal_voltage_switch;
  806. host->mmc_host_ops.set_ios = sdhci_omap_set_ios;
  807. host->mmc_host_ops.card_busy = sdhci_omap_card_busy;
  808. host->mmc_host_ops.execute_tuning = sdhci_omap_execute_tuning;
  809. host->mmc_host_ops.enable_sdio_irq = sdhci_omap_enable_sdio_irq;
  810. ret = sdhci_setup_host(host);
  811. if (ret)
  812. goto err_put_sync;
  813. ret = sdhci_omap_config_iodelay_pinctrl_state(omap_host);
  814. if (ret)
  815. goto err_cleanup_host;
  816. ret = __sdhci_add_host(host);
  817. if (ret)
  818. goto err_cleanup_host;
  819. return 0;
  820. err_cleanup_host:
  821. sdhci_cleanup_host(host);
  822. err_put_sync:
  823. pm_runtime_put_sync(dev);
  824. err_rpm_disable:
  825. pm_runtime_disable(dev);
  826. err_pltfm_free:
  827. sdhci_pltfm_free(pdev);
  828. return ret;
  829. }
  830. static int sdhci_omap_remove(struct platform_device *pdev)
  831. {
  832. struct device *dev = &pdev->dev;
  833. struct sdhci_host *host = platform_get_drvdata(pdev);
  834. sdhci_remove_host(host, true);
  835. pm_runtime_put_sync(dev);
  836. pm_runtime_disable(dev);
  837. sdhci_pltfm_free(pdev);
  838. return 0;
  839. }
  840. static struct platform_driver sdhci_omap_driver = {
  841. .probe = sdhci_omap_probe,
  842. .remove = sdhci_omap_remove,
  843. .driver = {
  844. .name = "sdhci-omap",
  845. .of_match_table = omap_sdhci_match,
  846. },
  847. };
  848. module_platform_driver(sdhci_omap_driver);
  849. MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
  850. MODULE_AUTHOR("Texas Instruments Inc.");
  851. MODULE_LICENSE("GPL v2");
  852. MODULE_ALIAS("platform:sdhci_omap");