sdhci-omap.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SDHCI Controller driver for TI's OMAP SoCs
  4. *
  5. * Copyright (C) 2017 Texas Instruments
  6. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/mmc/mmc.h>
  10. #include <linux/mmc/slot-gpio.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/pm_wakeirq.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/sys_soc.h>
  20. #include <linux/thermal.h>
  21. #include "sdhci-pltfm.h"
  22. /*
  23. * Note that the register offsets used here are from omap_regs
  24. * base which is 0x100 for omap4 and later, and 0 for omap3 and
  25. * earlier.
  26. */
  27. #define SDHCI_OMAP_SYSCONFIG 0x10
  28. #define SDHCI_OMAP_CON 0x2c
  29. #define CON_DW8 BIT(5)
  30. #define CON_DMA_MASTER BIT(20)
  31. #define CON_DDR BIT(19)
  32. #define CON_CLKEXTFREE BIT(16)
  33. #define CON_PADEN BIT(15)
  34. #define CON_CTPL BIT(11)
  35. #define CON_INIT BIT(1)
  36. #define CON_OD BIT(0)
  37. #define SDHCI_OMAP_DLL 0x34
  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 0x10c
  44. #define SDHCI_OMAP_PSTATE 0x124
  45. #define PSTATE_DLEV_DAT0 BIT(20)
  46. #define PSTATE_DATI BIT(1)
  47. #define SDHCI_OMAP_HCTL 0x128
  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 0x12c
  55. #define SYSCTL_CEN BIT(2)
  56. #define SYSCTL_CLKD_SHIFT 6
  57. #define SYSCTL_CLKD_MASK 0x3ff
  58. #define SDHCI_OMAP_STAT 0x130
  59. #define SDHCI_OMAP_IE 0x134
  60. #define INT_CC_EN BIT(0)
  61. #define SDHCI_OMAP_ISE 0x138
  62. #define SDHCI_OMAP_AC12 0x13c
  63. #define AC12_V1V8_SIGEN BIT(19)
  64. #define AC12_SCLK_SEL BIT(23)
  65. #define SDHCI_OMAP_CAPA 0x140
  66. #define CAPA_VS33 BIT(24)
  67. #define CAPA_VS30 BIT(25)
  68. #define CAPA_VS18 BIT(26)
  69. #define SDHCI_OMAP_CAPA2 0x144
  70. #define CAPA2_TSDR50 BIT(13)
  71. #define SDHCI_OMAP_TIMEOUT 1 /* 1 msec */
  72. #define SYSCTL_CLKD_MAX 0x3FF
  73. #define IOV_1V8 1800000 /* 180000 uV */
  74. #define IOV_3V0 3000000 /* 300000 uV */
  75. #define IOV_3V3 3300000 /* 330000 uV */
  76. #define MAX_PHASE_DELAY 0x7C
  77. /* sdhci-omap controller flags */
  78. #define SDHCI_OMAP_REQUIRE_IODELAY BIT(0)
  79. #define SDHCI_OMAP_SPECIAL_RESET BIT(1)
  80. struct sdhci_omap_data {
  81. int omap_offset; /* Offset for omap regs from base */
  82. u32 offset; /* Offset for SDHCI regs from base */
  83. u8 flags;
  84. };
  85. struct sdhci_omap_host {
  86. char *version;
  87. void __iomem *base;
  88. struct device *dev;
  89. struct regulator *pbias;
  90. bool pbias_enabled;
  91. struct sdhci_host *host;
  92. u8 bus_mode;
  93. u8 power_mode;
  94. u8 timing;
  95. u8 flags;
  96. struct pinctrl *pinctrl;
  97. struct pinctrl_state **pinctrl_state;
  98. int wakeirq;
  99. bool is_tuning;
  100. /* Offset for omap specific registers from base */
  101. int omap_offset;
  102. /* Omap specific context save */
  103. u32 con;
  104. u32 hctl;
  105. u32 sysctl;
  106. u32 capa;
  107. u32 ie;
  108. u32 ise;
  109. };
  110. static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host);
  111. static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host);
  112. static inline u32 sdhci_omap_readl(struct sdhci_omap_host *host,
  113. unsigned int offset)
  114. {
  115. return readl(host->base + host->omap_offset + offset);
  116. }
  117. static inline void sdhci_omap_writel(struct sdhci_omap_host *host,
  118. unsigned int offset, u32 data)
  119. {
  120. writel(data, host->base + host->omap_offset + offset);
  121. }
  122. static int sdhci_omap_set_pbias(struct sdhci_omap_host *omap_host,
  123. bool power_on, unsigned int iov)
  124. {
  125. int ret;
  126. struct device *dev = omap_host->dev;
  127. if (IS_ERR(omap_host->pbias))
  128. return 0;
  129. if (power_on) {
  130. ret = regulator_set_voltage(omap_host->pbias, iov, iov);
  131. if (ret) {
  132. dev_err(dev, "pbias set voltage failed\n");
  133. return ret;
  134. }
  135. if (omap_host->pbias_enabled)
  136. return 0;
  137. ret = regulator_enable(omap_host->pbias);
  138. if (ret) {
  139. dev_err(dev, "pbias reg enable fail\n");
  140. return ret;
  141. }
  142. omap_host->pbias_enabled = true;
  143. } else {
  144. if (!omap_host->pbias_enabled)
  145. return 0;
  146. ret = regulator_disable(omap_host->pbias);
  147. if (ret) {
  148. dev_err(dev, "pbias reg disable fail\n");
  149. return ret;
  150. }
  151. omap_host->pbias_enabled = false;
  152. }
  153. return 0;
  154. }
  155. static int sdhci_omap_enable_iov(struct sdhci_omap_host *omap_host,
  156. unsigned int iov_pbias)
  157. {
  158. int ret;
  159. struct sdhci_host *host = omap_host->host;
  160. struct mmc_host *mmc = host->mmc;
  161. ret = sdhci_omap_set_pbias(omap_host, false, 0);
  162. if (ret)
  163. return ret;
  164. if (!IS_ERR(mmc->supply.vqmmc)) {
  165. /* Pick the right voltage to allow 3.0V for 3.3V nominal PBIAS */
  166. ret = mmc_regulator_set_vqmmc(mmc, &mmc->ios);
  167. if (ret < 0) {
  168. dev_err(mmc_dev(mmc), "vqmmc set voltage failed\n");
  169. return ret;
  170. }
  171. }
  172. ret = sdhci_omap_set_pbias(omap_host, true, iov_pbias);
  173. if (ret)
  174. return ret;
  175. return 0;
  176. }
  177. static void sdhci_omap_conf_bus_power(struct sdhci_omap_host *omap_host,
  178. unsigned char signal_voltage)
  179. {
  180. u32 reg, capa;
  181. ktime_t timeout;
  182. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL);
  183. reg &= ~HCTL_SDVS_MASK;
  184. switch (signal_voltage) {
  185. case MMC_SIGNAL_VOLTAGE_330:
  186. capa = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  187. if (capa & CAPA_VS33)
  188. reg |= HCTL_SDVS_33;
  189. else if (capa & CAPA_VS30)
  190. reg |= HCTL_SDVS_30;
  191. else
  192. dev_warn(omap_host->dev, "misconfigured CAPA: %08x\n",
  193. capa);
  194. break;
  195. case MMC_SIGNAL_VOLTAGE_180:
  196. default:
  197. reg |= HCTL_SDVS_18;
  198. break;
  199. }
  200. sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
  201. reg |= HCTL_SDBP;
  202. sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
  203. /* wait 1ms */
  204. timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
  205. while (1) {
  206. bool timedout = ktime_after(ktime_get(), timeout);
  207. if (sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL) & HCTL_SDBP)
  208. break;
  209. if (WARN_ON(timedout))
  210. return;
  211. usleep_range(5, 10);
  212. }
  213. }
  214. static void sdhci_omap_enable_sdio_irq(struct mmc_host *mmc, int enable)
  215. {
  216. struct sdhci_host *host = mmc_priv(mmc);
  217. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  218. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  219. u32 reg;
  220. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  221. if (enable)
  222. reg |= (CON_CTPL | CON_CLKEXTFREE);
  223. else
  224. reg &= ~(CON_CTPL | CON_CLKEXTFREE);
  225. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  226. sdhci_enable_sdio_irq(mmc, enable);
  227. }
  228. static inline void sdhci_omap_set_dll(struct sdhci_omap_host *omap_host,
  229. int count)
  230. {
  231. int i;
  232. u32 reg;
  233. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
  234. reg |= DLL_FORCE_VALUE;
  235. reg &= ~DLL_FORCE_SR_C_MASK;
  236. reg |= (count << DLL_FORCE_SR_C_SHIFT);
  237. sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
  238. reg |= DLL_CALIB;
  239. sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
  240. for (i = 0; i < 1000; i++) {
  241. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
  242. if (reg & DLL_CALIB)
  243. break;
  244. }
  245. reg &= ~DLL_CALIB;
  246. sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
  247. }
  248. static void sdhci_omap_disable_tuning(struct sdhci_omap_host *omap_host)
  249. {
  250. u32 reg;
  251. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  252. reg &= ~AC12_SCLK_SEL;
  253. sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
  254. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
  255. reg &= ~(DLL_FORCE_VALUE | DLL_SWT);
  256. sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
  257. }
  258. static int sdhci_omap_execute_tuning(struct mmc_host *mmc, u32 opcode)
  259. {
  260. struct sdhci_host *host = mmc_priv(mmc);
  261. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  262. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  263. struct thermal_zone_device *thermal_dev;
  264. struct device *dev = omap_host->dev;
  265. struct mmc_ios *ios = &mmc->ios;
  266. u32 start_window = 0, max_window = 0;
  267. bool single_point_failure = false;
  268. bool dcrc_was_enabled = false;
  269. u8 cur_match, prev_match = 0;
  270. u32 length = 0, max_len = 0;
  271. u32 phase_delay = 0;
  272. int temperature;
  273. int ret = 0;
  274. u32 reg;
  275. int i;
  276. /* clock tuning is not needed for upto 52MHz */
  277. if (ios->clock <= 52000000)
  278. return 0;
  279. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA2);
  280. if (ios->timing == MMC_TIMING_UHS_SDR50 && !(reg & CAPA2_TSDR50))
  281. return 0;
  282. thermal_dev = thermal_zone_get_zone_by_name("cpu_thermal");
  283. if (IS_ERR(thermal_dev)) {
  284. dev_err(dev, "Unable to get thermal zone for tuning\n");
  285. return PTR_ERR(thermal_dev);
  286. }
  287. ret = thermal_zone_get_temp(thermal_dev, &temperature);
  288. if (ret)
  289. return ret;
  290. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
  291. reg |= DLL_SWT;
  292. sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
  293. /*
  294. * OMAP5/DRA74X/DRA72x Errata i802:
  295. * DCRC error interrupts (MMCHS_STAT[21] DCRC=0x1) can occur
  296. * during the tuning procedure. So disable it during the
  297. * tuning procedure.
  298. */
  299. if (host->ier & SDHCI_INT_DATA_CRC) {
  300. host->ier &= ~SDHCI_INT_DATA_CRC;
  301. dcrc_was_enabled = true;
  302. }
  303. omap_host->is_tuning = true;
  304. /*
  305. * Stage 1: Search for a maximum pass window ignoring any
  306. * single point failures. If the tuning value ends up
  307. * near it, move away from it in stage 2 below
  308. */
  309. while (phase_delay <= MAX_PHASE_DELAY) {
  310. sdhci_omap_set_dll(omap_host, phase_delay);
  311. cur_match = !mmc_send_tuning(mmc, opcode, NULL);
  312. if (cur_match) {
  313. if (prev_match) {
  314. length++;
  315. } else if (single_point_failure) {
  316. /* ignore single point failure */
  317. length++;
  318. } else {
  319. start_window = phase_delay;
  320. length = 1;
  321. }
  322. } else {
  323. single_point_failure = prev_match;
  324. }
  325. if (length > max_len) {
  326. max_window = start_window;
  327. max_len = length;
  328. }
  329. prev_match = cur_match;
  330. phase_delay += 4;
  331. }
  332. if (!max_len) {
  333. dev_err(dev, "Unable to find match\n");
  334. ret = -EIO;
  335. goto tuning_error;
  336. }
  337. /*
  338. * Assign tuning value as a ratio of maximum pass window based
  339. * on temperature
  340. */
  341. if (temperature < -20000)
  342. phase_delay = min(max_window + 4 * (max_len - 1) - 24,
  343. max_window +
  344. DIV_ROUND_UP(13 * max_len, 16) * 4);
  345. else if (temperature < 20000)
  346. phase_delay = max_window + DIV_ROUND_UP(9 * max_len, 16) * 4;
  347. else if (temperature < 40000)
  348. phase_delay = max_window + DIV_ROUND_UP(8 * max_len, 16) * 4;
  349. else if (temperature < 70000)
  350. phase_delay = max_window + DIV_ROUND_UP(7 * max_len, 16) * 4;
  351. else if (temperature < 90000)
  352. phase_delay = max_window + DIV_ROUND_UP(5 * max_len, 16) * 4;
  353. else if (temperature < 120000)
  354. phase_delay = max_window + DIV_ROUND_UP(4 * max_len, 16) * 4;
  355. else
  356. phase_delay = max_window + DIV_ROUND_UP(3 * max_len, 16) * 4;
  357. /*
  358. * Stage 2: Search for a single point failure near the chosen tuning
  359. * value in two steps. First in the +3 to +10 range and then in the
  360. * +2 to -10 range. If found, move away from it in the appropriate
  361. * direction by the appropriate amount depending on the temperature.
  362. */
  363. for (i = 3; i <= 10; i++) {
  364. sdhci_omap_set_dll(omap_host, phase_delay + i);
  365. if (mmc_send_tuning(mmc, opcode, NULL)) {
  366. if (temperature < 10000)
  367. phase_delay += i + 6;
  368. else if (temperature < 20000)
  369. phase_delay += i - 12;
  370. else if (temperature < 70000)
  371. phase_delay += i - 8;
  372. else
  373. phase_delay += i - 6;
  374. goto single_failure_found;
  375. }
  376. }
  377. for (i = 2; i >= -10; i--) {
  378. sdhci_omap_set_dll(omap_host, phase_delay + i);
  379. if (mmc_send_tuning(mmc, opcode, NULL)) {
  380. if (temperature < 10000)
  381. phase_delay += i + 12;
  382. else if (temperature < 20000)
  383. phase_delay += i + 8;
  384. else if (temperature < 70000)
  385. phase_delay += i + 8;
  386. else if (temperature < 90000)
  387. phase_delay += i + 10;
  388. else
  389. phase_delay += i + 12;
  390. goto single_failure_found;
  391. }
  392. }
  393. single_failure_found:
  394. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  395. if (!(reg & AC12_SCLK_SEL)) {
  396. ret = -EIO;
  397. goto tuning_error;
  398. }
  399. sdhci_omap_set_dll(omap_host, phase_delay);
  400. omap_host->is_tuning = false;
  401. goto ret;
  402. tuning_error:
  403. omap_host->is_tuning = false;
  404. dev_err(dev, "Tuning failed\n");
  405. sdhci_omap_disable_tuning(omap_host);
  406. ret:
  407. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  408. /* Reenable forbidden interrupt */
  409. if (dcrc_was_enabled)
  410. host->ier |= SDHCI_INT_DATA_CRC;
  411. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  412. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  413. return ret;
  414. }
  415. static int sdhci_omap_card_busy(struct mmc_host *mmc)
  416. {
  417. u32 reg, ac12;
  418. int ret = false;
  419. struct sdhci_host *host = mmc_priv(mmc);
  420. struct sdhci_pltfm_host *pltfm_host;
  421. struct sdhci_omap_host *omap_host;
  422. u32 ier = host->ier;
  423. pltfm_host = sdhci_priv(host);
  424. omap_host = sdhci_pltfm_priv(pltfm_host);
  425. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  426. ac12 = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  427. reg &= ~CON_CLKEXTFREE;
  428. if (ac12 & AC12_V1V8_SIGEN)
  429. reg |= CON_CLKEXTFREE;
  430. reg |= CON_PADEN;
  431. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  432. disable_irq(host->irq);
  433. ier |= SDHCI_INT_CARD_INT;
  434. sdhci_writel(host, ier, SDHCI_INT_ENABLE);
  435. sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  436. /*
  437. * Delay is required for PSTATE to correctly reflect
  438. * DLEV/CLEV values after PADEN is set.
  439. */
  440. usleep_range(50, 100);
  441. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_PSTATE);
  442. if ((reg & PSTATE_DATI) || !(reg & PSTATE_DLEV_DAT0))
  443. ret = true;
  444. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  445. reg &= ~(CON_CLKEXTFREE | CON_PADEN);
  446. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  447. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  448. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  449. enable_irq(host->irq);
  450. return ret;
  451. }
  452. static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc,
  453. struct mmc_ios *ios)
  454. {
  455. u32 reg;
  456. int ret;
  457. unsigned int iov;
  458. struct sdhci_host *host = mmc_priv(mmc);
  459. struct sdhci_pltfm_host *pltfm_host;
  460. struct sdhci_omap_host *omap_host;
  461. struct device *dev;
  462. pltfm_host = sdhci_priv(host);
  463. omap_host = sdhci_pltfm_priv(pltfm_host);
  464. dev = omap_host->dev;
  465. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  466. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  467. if (!(reg & (CAPA_VS30 | CAPA_VS33)))
  468. return -EOPNOTSUPP;
  469. if (reg & CAPA_VS30)
  470. iov = IOV_3V0;
  471. else
  472. iov = IOV_3V3;
  473. sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
  474. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  475. reg &= ~AC12_V1V8_SIGEN;
  476. sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
  477. } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
  478. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  479. if (!(reg & CAPA_VS18))
  480. return -EOPNOTSUPP;
  481. iov = IOV_1V8;
  482. sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
  483. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
  484. reg |= AC12_V1V8_SIGEN;
  485. sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
  486. } else {
  487. return -EOPNOTSUPP;
  488. }
  489. ret = sdhci_omap_enable_iov(omap_host, iov);
  490. if (ret) {
  491. dev_err(dev, "failed to switch IO voltage to %dmV\n", iov);
  492. return ret;
  493. }
  494. dev_dbg(dev, "IO voltage switched to %dmV\n", iov);
  495. return 0;
  496. }
  497. static void sdhci_omap_set_timing(struct sdhci_omap_host *omap_host, u8 timing)
  498. {
  499. int ret;
  500. struct pinctrl_state *pinctrl_state;
  501. struct device *dev = omap_host->dev;
  502. if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
  503. return;
  504. if (omap_host->timing == timing)
  505. return;
  506. sdhci_omap_stop_clock(omap_host);
  507. pinctrl_state = omap_host->pinctrl_state[timing];
  508. ret = pinctrl_select_state(omap_host->pinctrl, pinctrl_state);
  509. if (ret) {
  510. dev_err(dev, "failed to select pinctrl state\n");
  511. return;
  512. }
  513. sdhci_omap_start_clock(omap_host);
  514. omap_host->timing = timing;
  515. }
  516. static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host,
  517. u8 power_mode)
  518. {
  519. if (omap_host->bus_mode == MMC_POWER_OFF)
  520. sdhci_omap_disable_tuning(omap_host);
  521. omap_host->power_mode = power_mode;
  522. }
  523. static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host,
  524. unsigned int mode)
  525. {
  526. u32 reg;
  527. if (omap_host->bus_mode == mode)
  528. return;
  529. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  530. if (mode == MMC_BUSMODE_OPENDRAIN)
  531. reg |= CON_OD;
  532. else
  533. reg &= ~CON_OD;
  534. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  535. omap_host->bus_mode = mode;
  536. }
  537. static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  538. {
  539. struct sdhci_host *host = mmc_priv(mmc);
  540. struct sdhci_pltfm_host *pltfm_host;
  541. struct sdhci_omap_host *omap_host;
  542. pltfm_host = sdhci_priv(host);
  543. omap_host = sdhci_pltfm_priv(pltfm_host);
  544. sdhci_omap_set_bus_mode(omap_host, ios->bus_mode);
  545. sdhci_omap_set_timing(omap_host, ios->timing);
  546. sdhci_set_ios(mmc, ios);
  547. sdhci_omap_set_power_mode(omap_host, ios->power_mode);
  548. }
  549. static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host,
  550. unsigned int clock)
  551. {
  552. u16 dsor;
  553. dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock);
  554. if (dsor > SYSCTL_CLKD_MAX)
  555. dsor = SYSCTL_CLKD_MAX;
  556. return dsor;
  557. }
  558. static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host)
  559. {
  560. u32 reg;
  561. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
  562. reg |= SYSCTL_CEN;
  563. sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
  564. }
  565. static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host)
  566. {
  567. u32 reg;
  568. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
  569. reg &= ~SYSCTL_CEN;
  570. sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
  571. }
  572. static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock)
  573. {
  574. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  575. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  576. unsigned long clkdiv;
  577. sdhci_omap_stop_clock(omap_host);
  578. if (!clock)
  579. return;
  580. clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock);
  581. clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT;
  582. sdhci_enable_clk(host, clkdiv);
  583. sdhci_omap_start_clock(omap_host);
  584. }
  585. static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode,
  586. unsigned short vdd)
  587. {
  588. struct mmc_host *mmc = host->mmc;
  589. if (!IS_ERR(mmc->supply.vmmc))
  590. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  591. }
  592. /*
  593. * MMCHS_HL_HWINFO has the MADMA_EN bit set if the controller instance
  594. * is connected to L3 interconnect and is bus master capable. Note that
  595. * the MMCHS_HL_HWINFO register is in the module registers before the
  596. * omap registers and sdhci registers. The offset can vary for omap
  597. * registers depending on the SoC. Do not use sdhci_omap_readl() here.
  598. */
  599. static bool sdhci_omap_has_adma(struct sdhci_omap_host *omap_host, int offset)
  600. {
  601. /* MMCHS_HL_HWINFO register is only available on omap4 and later */
  602. if (offset < 0x200)
  603. return false;
  604. return readl(omap_host->base + 4) & 1;
  605. }
  606. static int sdhci_omap_enable_dma(struct sdhci_host *host)
  607. {
  608. u32 reg;
  609. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  610. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  611. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  612. reg &= ~CON_DMA_MASTER;
  613. /* Switch to DMA slave mode when using external DMA */
  614. if (!host->use_external_dma)
  615. reg |= CON_DMA_MASTER;
  616. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  617. return 0;
  618. }
  619. static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host)
  620. {
  621. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  622. return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX;
  623. }
  624. static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width)
  625. {
  626. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  627. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  628. u32 reg;
  629. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  630. if (width == MMC_BUS_WIDTH_8)
  631. reg |= CON_DW8;
  632. else
  633. reg &= ~CON_DW8;
  634. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  635. sdhci_set_bus_width(host, width);
  636. }
  637. static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode)
  638. {
  639. u32 reg;
  640. ktime_t timeout;
  641. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  642. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  643. if (omap_host->power_mode == power_mode)
  644. return;
  645. if (power_mode != MMC_POWER_ON)
  646. return;
  647. disable_irq(host->irq);
  648. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  649. reg |= CON_INIT;
  650. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  651. sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0);
  652. /* wait 1ms */
  653. timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
  654. while (1) {
  655. bool timedout = ktime_after(ktime_get(), timeout);
  656. if (sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)
  657. break;
  658. if (WARN_ON(timedout))
  659. return;
  660. usleep_range(5, 10);
  661. }
  662. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  663. reg &= ~CON_INIT;
  664. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  665. sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN);
  666. enable_irq(host->irq);
  667. }
  668. static void sdhci_omap_set_uhs_signaling(struct sdhci_host *host,
  669. unsigned int timing)
  670. {
  671. u32 reg;
  672. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  673. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  674. sdhci_omap_stop_clock(omap_host);
  675. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  676. if (timing == MMC_TIMING_UHS_DDR50 || timing == MMC_TIMING_MMC_DDR52)
  677. reg |= CON_DDR;
  678. else
  679. reg &= ~CON_DDR;
  680. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
  681. sdhci_set_uhs_signaling(host, timing);
  682. sdhci_omap_start_clock(omap_host);
  683. }
  684. #define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
  685. static void sdhci_omap_reset(struct sdhci_host *host, u8 mask)
  686. {
  687. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  688. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  689. unsigned long limit = MMC_TIMEOUT_US;
  690. unsigned long i = 0;
  691. u32 sysc;
  692. /* Save target module sysconfig configured by SoC PM layer */
  693. if (mask & SDHCI_RESET_ALL)
  694. sysc = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCONFIG);
  695. /* Don't reset data lines during tuning operation */
  696. if (omap_host->is_tuning)
  697. mask &= ~SDHCI_RESET_DATA;
  698. if (omap_host->flags & SDHCI_OMAP_SPECIAL_RESET) {
  699. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  700. while ((!(sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask)) &&
  701. (i++ < limit))
  702. udelay(1);
  703. i = 0;
  704. while ((sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) &&
  705. (i++ < limit))
  706. udelay(1);
  707. if (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask)
  708. dev_err(mmc_dev(host->mmc),
  709. "Timeout waiting on controller reset in %s\n",
  710. __func__);
  711. goto restore_sysc;
  712. }
  713. sdhci_reset(host, mask);
  714. restore_sysc:
  715. if (mask & SDHCI_RESET_ALL)
  716. sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCONFIG, sysc);
  717. }
  718. #define CMD_ERR_MASK (SDHCI_INT_CRC | SDHCI_INT_END_BIT | SDHCI_INT_INDEX |\
  719. SDHCI_INT_TIMEOUT)
  720. #define CMD_MASK (CMD_ERR_MASK | SDHCI_INT_RESPONSE)
  721. static u32 sdhci_omap_irq(struct sdhci_host *host, u32 intmask)
  722. {
  723. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  724. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  725. if (omap_host->is_tuning && host->cmd && !host->data_early &&
  726. (intmask & CMD_ERR_MASK)) {
  727. /*
  728. * Since we are not resetting data lines during tuning
  729. * operation, data error or data complete interrupts
  730. * might still arrive. Mark this request as a failure
  731. * but still wait for the data interrupt
  732. */
  733. if (intmask & SDHCI_INT_TIMEOUT)
  734. host->cmd->error = -ETIMEDOUT;
  735. else
  736. host->cmd->error = -EILSEQ;
  737. host->cmd = NULL;
  738. /*
  739. * Sometimes command error interrupts and command complete
  740. * interrupt will arrive together. Clear all command related
  741. * interrupts here.
  742. */
  743. sdhci_writel(host, intmask & CMD_MASK, SDHCI_INT_STATUS);
  744. intmask &= ~CMD_MASK;
  745. }
  746. return intmask;
  747. }
  748. static void sdhci_omap_set_timeout(struct sdhci_host *host,
  749. struct mmc_command *cmd)
  750. {
  751. if (cmd->opcode == MMC_ERASE)
  752. sdhci_set_data_timeout_irq(host, false);
  753. __sdhci_set_timeout(host, cmd);
  754. }
  755. static const struct sdhci_ops sdhci_omap_ops = {
  756. .set_clock = sdhci_omap_set_clock,
  757. .set_power = sdhci_omap_set_power,
  758. .enable_dma = sdhci_omap_enable_dma,
  759. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  760. .get_min_clock = sdhci_omap_get_min_clock,
  761. .set_bus_width = sdhci_omap_set_bus_width,
  762. .platform_send_init_74_clocks = sdhci_omap_init_74_clocks,
  763. .reset = sdhci_omap_reset,
  764. .set_uhs_signaling = sdhci_omap_set_uhs_signaling,
  765. .irq = sdhci_omap_irq,
  766. .set_timeout = sdhci_omap_set_timeout,
  767. };
  768. static unsigned int sdhci_omap_regulator_get_caps(struct device *dev,
  769. const char *name)
  770. {
  771. struct regulator *reg;
  772. unsigned int caps = 0;
  773. reg = regulator_get(dev, name);
  774. if (IS_ERR(reg))
  775. return ~0U;
  776. if (regulator_is_supported_voltage(reg, 1700000, 1950000))
  777. caps |= SDHCI_CAN_VDD_180;
  778. if (regulator_is_supported_voltage(reg, 2700000, 3150000))
  779. caps |= SDHCI_CAN_VDD_300;
  780. if (regulator_is_supported_voltage(reg, 3150000, 3600000))
  781. caps |= SDHCI_CAN_VDD_330;
  782. regulator_put(reg);
  783. return caps;
  784. }
  785. static int sdhci_omap_set_capabilities(struct sdhci_host *host)
  786. {
  787. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  788. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  789. struct device *dev = omap_host->dev;
  790. const u32 mask = SDHCI_CAN_VDD_180 | SDHCI_CAN_VDD_300 | SDHCI_CAN_VDD_330;
  791. unsigned int pbias, vqmmc, caps = 0;
  792. u32 reg;
  793. pbias = sdhci_omap_regulator_get_caps(dev, "pbias");
  794. vqmmc = sdhci_omap_regulator_get_caps(dev, "vqmmc");
  795. caps = pbias & vqmmc;
  796. if (pbias != ~0U && vqmmc == ~0U)
  797. dev_warn(dev, "vqmmc regulator missing for pbias\n");
  798. else if (caps == ~0U)
  799. return 0;
  800. /*
  801. * Quirk handling to allow 3.0V vqmmc with a valid 3.3V PBIAS. This is
  802. * needed for 3.0V ldo9_reg on omap5 at least.
  803. */
  804. if (pbias != ~0U && (pbias & SDHCI_CAN_VDD_330) &&
  805. (vqmmc & SDHCI_CAN_VDD_300))
  806. caps |= SDHCI_CAN_VDD_330;
  807. /* voltage capabilities might be set by boot loader, clear it */
  808. reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  809. reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33);
  810. if (caps & SDHCI_CAN_VDD_180)
  811. reg |= CAPA_VS18;
  812. if (caps & SDHCI_CAN_VDD_300)
  813. reg |= CAPA_VS30;
  814. if (caps & SDHCI_CAN_VDD_330)
  815. reg |= CAPA_VS33;
  816. sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg);
  817. host->caps &= ~mask;
  818. host->caps |= caps;
  819. return 0;
  820. }
  821. static const struct sdhci_pltfm_data sdhci_omap_pdata = {
  822. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  823. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  824. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  825. SDHCI_QUIRK_NO_HISPD_BIT |
  826. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
  827. .quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN |
  828. SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  829. SDHCI_QUIRK2_RSP_136_HAS_CRC |
  830. SDHCI_QUIRK2_DISABLE_HW_TIMEOUT,
  831. .ops = &sdhci_omap_ops,
  832. };
  833. static const struct sdhci_omap_data omap2430_data = {
  834. .omap_offset = 0,
  835. .offset = 0x100,
  836. };
  837. static const struct sdhci_omap_data omap3_data = {
  838. .omap_offset = 0,
  839. .offset = 0x100,
  840. };
  841. static const struct sdhci_omap_data omap4_data = {
  842. .omap_offset = 0x100,
  843. .offset = 0x200,
  844. .flags = SDHCI_OMAP_SPECIAL_RESET,
  845. };
  846. static const struct sdhci_omap_data omap5_data = {
  847. .omap_offset = 0x100,
  848. .offset = 0x200,
  849. .flags = SDHCI_OMAP_SPECIAL_RESET,
  850. };
  851. static const struct sdhci_omap_data k2g_data = {
  852. .omap_offset = 0x100,
  853. .offset = 0x200,
  854. };
  855. static const struct sdhci_omap_data am335_data = {
  856. .omap_offset = 0x100,
  857. .offset = 0x200,
  858. .flags = SDHCI_OMAP_SPECIAL_RESET,
  859. };
  860. static const struct sdhci_omap_data am437_data = {
  861. .omap_offset = 0x100,
  862. .offset = 0x200,
  863. .flags = SDHCI_OMAP_SPECIAL_RESET,
  864. };
  865. static const struct sdhci_omap_data dra7_data = {
  866. .omap_offset = 0x100,
  867. .offset = 0x200,
  868. .flags = SDHCI_OMAP_REQUIRE_IODELAY,
  869. };
  870. static const struct of_device_id omap_sdhci_match[] = {
  871. { .compatible = "ti,omap2430-sdhci", .data = &omap2430_data },
  872. { .compatible = "ti,omap3-sdhci", .data = &omap3_data },
  873. { .compatible = "ti,omap4-sdhci", .data = &omap4_data },
  874. { .compatible = "ti,omap5-sdhci", .data = &omap5_data },
  875. { .compatible = "ti,dra7-sdhci", .data = &dra7_data },
  876. { .compatible = "ti,k2g-sdhci", .data = &k2g_data },
  877. { .compatible = "ti,am335-sdhci", .data = &am335_data },
  878. { .compatible = "ti,am437-sdhci", .data = &am437_data },
  879. {},
  880. };
  881. MODULE_DEVICE_TABLE(of, omap_sdhci_match);
  882. static struct pinctrl_state
  883. *sdhci_omap_iodelay_pinctrl_state(struct sdhci_omap_host *omap_host, char *mode,
  884. u32 *caps, u32 capmask)
  885. {
  886. struct device *dev = omap_host->dev;
  887. char *version = omap_host->version;
  888. struct pinctrl_state *pinctrl_state = ERR_PTR(-ENODEV);
  889. char str[20];
  890. if (!(*caps & capmask))
  891. goto ret;
  892. if (version) {
  893. snprintf(str, 20, "%s-%s", mode, version);
  894. pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, str);
  895. }
  896. if (IS_ERR(pinctrl_state))
  897. pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, mode);
  898. if (IS_ERR(pinctrl_state)) {
  899. dev_err(dev, "no pinctrl state for %s mode", mode);
  900. *caps &= ~capmask;
  901. }
  902. ret:
  903. return pinctrl_state;
  904. }
  905. static int sdhci_omap_config_iodelay_pinctrl_state(struct sdhci_omap_host
  906. *omap_host)
  907. {
  908. struct device *dev = omap_host->dev;
  909. struct sdhci_host *host = omap_host->host;
  910. struct mmc_host *mmc = host->mmc;
  911. u32 *caps = &mmc->caps;
  912. u32 *caps2 = &mmc->caps2;
  913. struct pinctrl_state *state;
  914. struct pinctrl_state **pinctrl_state;
  915. if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
  916. return 0;
  917. pinctrl_state = devm_kcalloc(dev,
  918. MMC_TIMING_MMC_HS200 + 1,
  919. sizeof(*pinctrl_state),
  920. GFP_KERNEL);
  921. if (!pinctrl_state)
  922. return -ENOMEM;
  923. omap_host->pinctrl = devm_pinctrl_get(omap_host->dev);
  924. if (IS_ERR(omap_host->pinctrl)) {
  925. dev_err(dev, "Cannot get pinctrl\n");
  926. return PTR_ERR(omap_host->pinctrl);
  927. }
  928. state = pinctrl_lookup_state(omap_host->pinctrl, "default");
  929. if (IS_ERR(state)) {
  930. dev_err(dev, "no pinctrl state for default mode\n");
  931. return PTR_ERR(state);
  932. }
  933. pinctrl_state[MMC_TIMING_LEGACY] = state;
  934. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr104", caps,
  935. MMC_CAP_UHS_SDR104);
  936. if (!IS_ERR(state))
  937. pinctrl_state[MMC_TIMING_UHS_SDR104] = state;
  938. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr50", caps,
  939. MMC_CAP_UHS_DDR50);
  940. if (!IS_ERR(state))
  941. pinctrl_state[MMC_TIMING_UHS_DDR50] = state;
  942. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr50", caps,
  943. MMC_CAP_UHS_SDR50);
  944. if (!IS_ERR(state))
  945. pinctrl_state[MMC_TIMING_UHS_SDR50] = state;
  946. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr25", caps,
  947. MMC_CAP_UHS_SDR25);
  948. if (!IS_ERR(state))
  949. pinctrl_state[MMC_TIMING_UHS_SDR25] = state;
  950. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr12", caps,
  951. MMC_CAP_UHS_SDR12);
  952. if (!IS_ERR(state))
  953. pinctrl_state[MMC_TIMING_UHS_SDR12] = state;
  954. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_1_8v", caps,
  955. MMC_CAP_1_8V_DDR);
  956. if (!IS_ERR(state)) {
  957. pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
  958. } else {
  959. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_3_3v",
  960. caps,
  961. MMC_CAP_3_3V_DDR);
  962. if (!IS_ERR(state))
  963. pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
  964. }
  965. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
  966. MMC_CAP_SD_HIGHSPEED);
  967. if (!IS_ERR(state))
  968. pinctrl_state[MMC_TIMING_SD_HS] = state;
  969. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
  970. MMC_CAP_MMC_HIGHSPEED);
  971. if (!IS_ERR(state))
  972. pinctrl_state[MMC_TIMING_MMC_HS] = state;
  973. state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs200_1_8v", caps2,
  974. MMC_CAP2_HS200_1_8V_SDR);
  975. if (!IS_ERR(state))
  976. pinctrl_state[MMC_TIMING_MMC_HS200] = state;
  977. omap_host->pinctrl_state = pinctrl_state;
  978. return 0;
  979. }
  980. static const struct soc_device_attribute sdhci_omap_soc_devices[] = {
  981. {
  982. .machine = "DRA7[45]*",
  983. .revision = "ES1.[01]",
  984. },
  985. {
  986. /* sentinel */
  987. }
  988. };
  989. static int sdhci_omap_probe(struct platform_device *pdev)
  990. {
  991. int ret;
  992. u32 offset;
  993. struct device *dev = &pdev->dev;
  994. struct sdhci_host *host;
  995. struct sdhci_pltfm_host *pltfm_host;
  996. struct sdhci_omap_host *omap_host;
  997. struct mmc_host *mmc;
  998. const struct sdhci_omap_data *data;
  999. const struct soc_device_attribute *soc;
  1000. struct resource *regs;
  1001. data = of_device_get_match_data(&pdev->dev);
  1002. if (!data) {
  1003. dev_err(dev, "no sdhci omap data\n");
  1004. return -EINVAL;
  1005. }
  1006. offset = data->offset;
  1007. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1008. if (!regs)
  1009. return -ENXIO;
  1010. host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata,
  1011. sizeof(*omap_host));
  1012. if (IS_ERR(host)) {
  1013. dev_err(dev, "Failed sdhci_pltfm_init\n");
  1014. return PTR_ERR(host);
  1015. }
  1016. pltfm_host = sdhci_priv(host);
  1017. omap_host = sdhci_pltfm_priv(pltfm_host);
  1018. omap_host->host = host;
  1019. omap_host->base = host->ioaddr;
  1020. omap_host->dev = dev;
  1021. omap_host->power_mode = MMC_POWER_UNDEFINED;
  1022. omap_host->timing = MMC_TIMING_LEGACY;
  1023. omap_host->flags = data->flags;
  1024. omap_host->omap_offset = data->omap_offset;
  1025. omap_host->con = -EINVAL; /* Prevent invalid restore on first resume */
  1026. host->ioaddr += offset;
  1027. host->mapbase = regs->start + offset;
  1028. mmc = host->mmc;
  1029. sdhci_get_of_property(pdev);
  1030. ret = mmc_of_parse(mmc);
  1031. if (ret)
  1032. return ret;
  1033. soc = soc_device_match(sdhci_omap_soc_devices);
  1034. if (soc) {
  1035. omap_host->version = "rev11";
  1036. if (!strcmp(dev_name(dev), "4809c000.mmc"))
  1037. mmc->f_max = 96000000;
  1038. if (!strcmp(dev_name(dev), "480b4000.mmc"))
  1039. mmc->f_max = 48000000;
  1040. if (!strcmp(dev_name(dev), "480ad000.mmc"))
  1041. mmc->f_max = 48000000;
  1042. }
  1043. if (!mmc_host_can_gpio_ro(mmc))
  1044. mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
  1045. pltfm_host->clk = devm_clk_get(dev, "fck");
  1046. if (IS_ERR(pltfm_host->clk))
  1047. return PTR_ERR(pltfm_host->clk);
  1048. ret = clk_set_rate(pltfm_host->clk, mmc->f_max);
  1049. if (ret)
  1050. return dev_err_probe(dev, ret,
  1051. "failed to set clock to %d\n", mmc->f_max);
  1052. omap_host->pbias = devm_regulator_get_optional(dev, "pbias");
  1053. if (IS_ERR(omap_host->pbias)) {
  1054. ret = PTR_ERR(omap_host->pbias);
  1055. if (ret != -ENODEV)
  1056. return ret;
  1057. dev_dbg(dev, "unable to get pbias regulator %d\n", ret);
  1058. }
  1059. omap_host->pbias_enabled = false;
  1060. /*
  1061. * omap_device_pm_domain has callbacks to enable the main
  1062. * functional clock, interface clock and also configure the
  1063. * SYSCONFIG register to clear any boot loader set voltage
  1064. * capabilities before calling sdhci_setup_host(). The
  1065. * callback will be invoked as part of pm_runtime_get_sync.
  1066. */
  1067. pm_runtime_use_autosuspend(dev);
  1068. pm_runtime_set_autosuspend_delay(dev, 50);
  1069. pm_runtime_enable(dev);
  1070. ret = pm_runtime_resume_and_get(dev);
  1071. if (ret) {
  1072. dev_err(dev, "pm_runtime_get_sync failed\n");
  1073. goto err_rpm_disable;
  1074. }
  1075. ret = sdhci_omap_set_capabilities(host);
  1076. if (ret) {
  1077. dev_err(dev, "failed to set system capabilities\n");
  1078. goto err_rpm_put;
  1079. }
  1080. host->mmc_host_ops.start_signal_voltage_switch =
  1081. sdhci_omap_start_signal_voltage_switch;
  1082. host->mmc_host_ops.set_ios = sdhci_omap_set_ios;
  1083. host->mmc_host_ops.card_busy = sdhci_omap_card_busy;
  1084. host->mmc_host_ops.execute_tuning = sdhci_omap_execute_tuning;
  1085. host->mmc_host_ops.enable_sdio_irq = sdhci_omap_enable_sdio_irq;
  1086. /*
  1087. * Switch to external DMA only if there is the "dmas" property and
  1088. * ADMA is not available on the controller instance.
  1089. */
  1090. if (device_property_present(dev, "dmas") &&
  1091. !sdhci_omap_has_adma(omap_host, offset))
  1092. sdhci_switch_external_dma(host, true);
  1093. if (device_property_read_bool(dev, "ti,non-removable")) {
  1094. dev_warn_once(dev, "using old ti,non-removable property\n");
  1095. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1096. }
  1097. /* R1B responses is required to properly manage HW busy detection. */
  1098. mmc->caps |= MMC_CAP_NEED_RSP_BUSY;
  1099. /* Enable SDIO card power off. */
  1100. mmc->caps |= MMC_CAP_POWER_OFF_CARD;
  1101. ret = sdhci_setup_host(host);
  1102. if (ret)
  1103. goto err_rpm_put;
  1104. ret = sdhci_omap_config_iodelay_pinctrl_state(omap_host);
  1105. if (ret)
  1106. goto err_cleanup_host;
  1107. ret = __sdhci_add_host(host);
  1108. if (ret)
  1109. goto err_cleanup_host;
  1110. /*
  1111. * SDIO devices can use the dat1 pin as a wake-up interrupt. Some
  1112. * devices like wl1xxx, use an out-of-band GPIO interrupt instead.
  1113. */
  1114. omap_host->wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
  1115. if (omap_host->wakeirq == -EPROBE_DEFER) {
  1116. ret = -EPROBE_DEFER;
  1117. goto err_cleanup_host;
  1118. }
  1119. if (omap_host->wakeirq > 0) {
  1120. device_init_wakeup(dev, true);
  1121. ret = dev_pm_set_dedicated_wake_irq(dev, omap_host->wakeirq);
  1122. if (ret) {
  1123. device_init_wakeup(dev, false);
  1124. goto err_cleanup_host;
  1125. }
  1126. host->mmc->pm_caps |= MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  1127. }
  1128. pm_runtime_put_autosuspend(dev);
  1129. return 0;
  1130. err_cleanup_host:
  1131. sdhci_cleanup_host(host);
  1132. err_rpm_put:
  1133. pm_runtime_put_autosuspend(dev);
  1134. err_rpm_disable:
  1135. pm_runtime_dont_use_autosuspend(dev);
  1136. pm_runtime_disable(dev);
  1137. return ret;
  1138. }
  1139. static void sdhci_omap_remove(struct platform_device *pdev)
  1140. {
  1141. struct device *dev = &pdev->dev;
  1142. struct sdhci_host *host = platform_get_drvdata(pdev);
  1143. pm_runtime_get_sync(dev);
  1144. sdhci_remove_host(host, true);
  1145. device_init_wakeup(dev, false);
  1146. dev_pm_clear_wake_irq(dev);
  1147. pm_runtime_dont_use_autosuspend(dev);
  1148. pm_runtime_put_sync(dev);
  1149. /* Ensure device gets disabled despite userspace sysfs config */
  1150. pm_runtime_force_suspend(dev);
  1151. }
  1152. static void sdhci_omap_context_save(struct sdhci_omap_host *omap_host)
  1153. {
  1154. omap_host->con = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
  1155. omap_host->hctl = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL);
  1156. omap_host->sysctl = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
  1157. omap_host->capa = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
  1158. omap_host->ie = sdhci_omap_readl(omap_host, SDHCI_OMAP_IE);
  1159. omap_host->ise = sdhci_omap_readl(omap_host, SDHCI_OMAP_ISE);
  1160. }
  1161. /* Order matters here, HCTL must be restored in two phases */
  1162. static void sdhci_omap_context_restore(struct sdhci_omap_host *omap_host)
  1163. {
  1164. sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, omap_host->hctl);
  1165. sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, omap_host->capa);
  1166. sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, omap_host->hctl);
  1167. sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, omap_host->sysctl);
  1168. sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, omap_host->con);
  1169. sdhci_omap_writel(omap_host, SDHCI_OMAP_IE, omap_host->ie);
  1170. sdhci_omap_writel(omap_host, SDHCI_OMAP_ISE, omap_host->ise);
  1171. }
  1172. static int sdhci_omap_runtime_suspend(struct device *dev)
  1173. {
  1174. struct sdhci_host *host = dev_get_drvdata(dev);
  1175. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1176. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  1177. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  1178. mmc_retune_needed(host->mmc);
  1179. if (omap_host->con != -EINVAL)
  1180. sdhci_runtime_suspend_host(host);
  1181. sdhci_omap_context_save(omap_host);
  1182. pinctrl_pm_select_idle_state(dev);
  1183. return 0;
  1184. }
  1185. static int sdhci_omap_runtime_resume(struct device *dev)
  1186. {
  1187. struct sdhci_host *host = dev_get_drvdata(dev);
  1188. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1189. struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
  1190. pinctrl_pm_select_default_state(dev);
  1191. if (omap_host->con != -EINVAL) {
  1192. sdhci_omap_context_restore(omap_host);
  1193. sdhci_runtime_resume_host(host, 0);
  1194. }
  1195. return 0;
  1196. }
  1197. static const struct dev_pm_ops sdhci_omap_dev_pm_ops = {
  1198. RUNTIME_PM_OPS(sdhci_omap_runtime_suspend, sdhci_omap_runtime_resume, NULL)
  1199. SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
  1200. };
  1201. static struct platform_driver sdhci_omap_driver = {
  1202. .probe = sdhci_omap_probe,
  1203. .remove = sdhci_omap_remove,
  1204. .driver = {
  1205. .name = "sdhci-omap",
  1206. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1207. .pm = pm_ptr(&sdhci_omap_dev_pm_ops),
  1208. .of_match_table = omap_sdhci_match,
  1209. },
  1210. };
  1211. module_platform_driver(sdhci_omap_driver);
  1212. MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
  1213. MODULE_AUTHOR("Texas Instruments Inc.");
  1214. MODULE_LICENSE("GPL v2");
  1215. MODULE_ALIAS("platform:sdhci_omap");