sdhci-uhs2.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/drivers/mmc/host/sdhci_uhs2.c - Secure Digital Host Controller
  4. * Interface driver
  5. *
  6. * Copyright (C) 2014 Intel Corp, All Rights Reserved.
  7. * Copyright (C) 2020 Genesys Logic, Inc.
  8. * Authors: Ben Chuang <ben.chuang@genesyslogic.com.tw>
  9. * Copyright (C) 2020 Linaro Limited
  10. * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/module.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/bitfield.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/mmc/mmc.h>
  18. #include <linux/mmc/host.h>
  19. #include "sdhci.h"
  20. #include "sdhci-uhs2.h"
  21. #define DRIVER_NAME "sdhci_uhs2"
  22. #define DBG(f, x...) \
  23. pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
  24. #define SDHCI_UHS2_DUMP(f, x...) \
  25. pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  26. #define UHS2_RESET_TIMEOUT_100MS 100000
  27. #define UHS2_CHECK_DORMANT_TIMEOUT_100MS 100000
  28. #define UHS2_INTERFACE_DETECT_TIMEOUT_100MS 100000
  29. #define UHS2_LANE_SYNC_TIMEOUT_150MS 150000
  30. #define UHS2_ARG_IOADR_MASK 0xfff
  31. void sdhci_uhs2_dump_regs(struct sdhci_host *host)
  32. {
  33. if (!(mmc_card_uhs2(host->mmc)))
  34. return;
  35. SDHCI_UHS2_DUMP("==================== UHS2 ==================\n");
  36. SDHCI_UHS2_DUMP("Blk Size: 0x%08x | Blk Cnt: 0x%08x\n",
  37. sdhci_readw(host, SDHCI_UHS2_BLOCK_SIZE),
  38. sdhci_readl(host, SDHCI_UHS2_BLOCK_COUNT));
  39. SDHCI_UHS2_DUMP("Cmd: 0x%08x | Trn mode: 0x%08x\n",
  40. sdhci_readw(host, SDHCI_UHS2_CMD),
  41. sdhci_readw(host, SDHCI_UHS2_TRANS_MODE));
  42. SDHCI_UHS2_DUMP("Int Stat: 0x%08x | Dev Sel : 0x%08x\n",
  43. sdhci_readw(host, SDHCI_UHS2_DEV_INT_STATUS),
  44. sdhci_readb(host, SDHCI_UHS2_DEV_SELECT));
  45. SDHCI_UHS2_DUMP("Dev Int Code: 0x%08x\n",
  46. sdhci_readb(host, SDHCI_UHS2_DEV_INT_CODE));
  47. SDHCI_UHS2_DUMP("Reset: 0x%08x | Timer: 0x%08x\n",
  48. sdhci_readw(host, SDHCI_UHS2_SW_RESET),
  49. sdhci_readw(host, SDHCI_UHS2_TIMER_CTRL));
  50. SDHCI_UHS2_DUMP("ErrInt: 0x%08x | ErrIntEn: 0x%08x\n",
  51. sdhci_readl(host, SDHCI_UHS2_INT_STATUS),
  52. sdhci_readl(host, SDHCI_UHS2_INT_STATUS_ENABLE));
  53. SDHCI_UHS2_DUMP("ErrSigEn: 0x%08x\n",
  54. sdhci_readl(host, SDHCI_UHS2_INT_SIGNAL_ENABLE));
  55. }
  56. EXPORT_SYMBOL_GPL(sdhci_uhs2_dump_regs);
  57. /*****************************************************************************\
  58. * *
  59. * Low level functions *
  60. * *
  61. \*****************************************************************************/
  62. static inline u16 uhs2_dev_cmd(struct mmc_command *cmd)
  63. {
  64. return be16_to_cpu((__force __be16)cmd->uhs2_cmd->arg) & UHS2_ARG_IOADR_MASK;
  65. }
  66. static inline int mmc_opt_regulator_set_ocr(struct mmc_host *mmc,
  67. struct regulator *supply,
  68. unsigned short vdd_bit)
  69. {
  70. return IS_ERR_OR_NULL(supply) ? 0 : mmc_regulator_set_ocr(mmc, supply, vdd_bit);
  71. }
  72. /**
  73. * sdhci_uhs2_reset - invoke SW reset
  74. * @host: SDHCI host
  75. * @mask: Control mask
  76. *
  77. * Invoke SW reset, depending on a bit in @mask and wait for completion.
  78. */
  79. void sdhci_uhs2_reset(struct sdhci_host *host, u16 mask)
  80. {
  81. u32 val;
  82. sdhci_writew(host, mask, SDHCI_UHS2_SW_RESET);
  83. if (mask & SDHCI_UHS2_SW_RESET_FULL)
  84. host->clock = 0;
  85. /* hw clears the bit when it's done */
  86. if (read_poll_timeout_atomic(sdhci_readw, val, !(val & mask), 10,
  87. UHS2_RESET_TIMEOUT_100MS, true, host, SDHCI_UHS2_SW_RESET)) {
  88. pr_debug("%s: %s: Reset 0x%x never completed. %s: clean reset bit.\n", __func__,
  89. mmc_hostname(host->mmc), (int)mask, mmc_hostname(host->mmc));
  90. sdhci_writeb(host, 0, SDHCI_UHS2_SW_RESET);
  91. return;
  92. }
  93. }
  94. EXPORT_SYMBOL_GPL(sdhci_uhs2_reset);
  95. static void sdhci_uhs2_reset_cmd_data(struct sdhci_host *host)
  96. {
  97. sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  98. if (host->mmc->uhs2_sd_tran) {
  99. sdhci_uhs2_reset(host, SDHCI_UHS2_SW_RESET_SD);
  100. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  101. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  102. sdhci_uhs2_clear_set_irqs(host, SDHCI_INT_ALL_MASK, SDHCI_UHS2_INT_ERROR_MASK);
  103. }
  104. }
  105. void sdhci_uhs2_set_power(struct sdhci_host *host, unsigned char mode, unsigned short vdd)
  106. {
  107. struct mmc_host *mmc = host->mmc;
  108. u8 pwr = 0;
  109. if (mode != MMC_POWER_OFF) {
  110. pwr = sdhci_get_vdd_value(vdd);
  111. if (!pwr)
  112. WARN(1, "%s: Invalid vdd %#x\n",
  113. mmc_hostname(host->mmc), vdd);
  114. pwr |= SDHCI_VDD2_POWER_180;
  115. }
  116. if (host->pwr == pwr)
  117. return;
  118. host->pwr = pwr;
  119. if (pwr == 0) {
  120. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  121. mmc_opt_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  122. mmc_regulator_set_vqmmc2(mmc, &mmc->ios);
  123. } else {
  124. mmc_opt_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  125. /* support 1.8v only for now */
  126. mmc_regulator_set_vqmmc2(mmc, &mmc->ios);
  127. /* Clear the power reg before setting a new value */
  128. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  129. /* vdd first */
  130. pwr |= SDHCI_POWER_ON;
  131. sdhci_writeb(host, pwr & 0xf, SDHCI_POWER_CONTROL);
  132. mdelay(5);
  133. pwr |= SDHCI_VDD2_POWER_ON;
  134. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  135. mdelay(5);
  136. }
  137. }
  138. EXPORT_SYMBOL_GPL(sdhci_uhs2_set_power);
  139. static u8 sdhci_calc_timeout_uhs2(struct sdhci_host *host, u8 *cmd_res, u8 *dead_lock)
  140. {
  141. /* timeout in us */
  142. unsigned int dead_lock_timeout = 1 * 1000 * 1000;
  143. unsigned int cmd_res_timeout = 5 * 1000;
  144. unsigned int current_timeout;
  145. u8 count;
  146. /*
  147. * Figure out needed cycles.
  148. * We do this in steps in order to fit inside a 32 bit int.
  149. * The first step is the minimum timeout, which will have a
  150. * minimum resolution of 6 bits:
  151. * (1) 2^13*1000 > 2^22,
  152. * (2) host->timeout_clk < 2^16
  153. * =>
  154. * (1) / (2) > 2^6
  155. */
  156. count = 0;
  157. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  158. while (current_timeout < cmd_res_timeout) {
  159. count++;
  160. current_timeout <<= 1;
  161. if (count >= 0xF)
  162. break;
  163. }
  164. if (count >= 0xF) {
  165. DBG("%s: Too large timeout 0x%x requested for CMD_RES!\n",
  166. mmc_hostname(host->mmc), count);
  167. count = 0xE;
  168. }
  169. *cmd_res = count;
  170. count = 0;
  171. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  172. while (current_timeout < dead_lock_timeout) {
  173. count++;
  174. current_timeout <<= 1;
  175. if (count >= 0xF)
  176. break;
  177. }
  178. if (count >= 0xF) {
  179. DBG("%s: Too large timeout 0x%x requested for DEADLOCK!\n",
  180. mmc_hostname(host->mmc), count);
  181. count = 0xE;
  182. }
  183. *dead_lock = count;
  184. return count;
  185. }
  186. static void __sdhci_uhs2_set_timeout(struct sdhci_host *host)
  187. {
  188. u8 cmd_res, dead_lock;
  189. sdhci_calc_timeout_uhs2(host, &cmd_res, &dead_lock);
  190. cmd_res |= FIELD_PREP(SDHCI_UHS2_TIMER_CTRL_DEADLOCK_MASK, dead_lock);
  191. sdhci_writeb(host, cmd_res, SDHCI_UHS2_TIMER_CTRL);
  192. }
  193. void sdhci_uhs2_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  194. {
  195. __sdhci_set_timeout(host, cmd);
  196. if (mmc_card_uhs2(host->mmc))
  197. __sdhci_uhs2_set_timeout(host);
  198. }
  199. EXPORT_SYMBOL_GPL(sdhci_uhs2_set_timeout);
  200. /**
  201. * sdhci_uhs2_clear_set_irqs - set Error Interrupt Status Enable register
  202. * @host: SDHCI host
  203. * @clear: bit-wise clear mask
  204. * @set: bit-wise set mask
  205. *
  206. * Set/unset bits in UHS-II Error Interrupt Status Enable register
  207. */
  208. void sdhci_uhs2_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
  209. {
  210. u32 ier;
  211. ier = sdhci_readl(host, SDHCI_UHS2_INT_STATUS_ENABLE);
  212. ier &= ~clear;
  213. ier |= set;
  214. sdhci_writel(host, ier, SDHCI_UHS2_INT_STATUS_ENABLE);
  215. sdhci_writel(host, ier, SDHCI_UHS2_INT_SIGNAL_ENABLE);
  216. }
  217. EXPORT_SYMBOL_GPL(sdhci_uhs2_clear_set_irqs);
  218. static void __sdhci_uhs2_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  219. {
  220. struct sdhci_host *host = mmc_priv(mmc);
  221. u8 cmd_res, dead_lock;
  222. u16 ctrl_2;
  223. /* UHS2 Timeout Control */
  224. sdhci_calc_timeout_uhs2(host, &cmd_res, &dead_lock);
  225. /* change to use calculate value */
  226. cmd_res |= FIELD_PREP(SDHCI_UHS2_TIMER_CTRL_DEADLOCK_MASK, dead_lock);
  227. sdhci_uhs2_clear_set_irqs(host,
  228. SDHCI_UHS2_INT_CMD_TIMEOUT |
  229. SDHCI_UHS2_INT_DEADLOCK_TIMEOUT,
  230. 0);
  231. sdhci_writeb(host, cmd_res, SDHCI_UHS2_TIMER_CTRL);
  232. sdhci_uhs2_clear_set_irqs(host, 0,
  233. SDHCI_UHS2_INT_CMD_TIMEOUT |
  234. SDHCI_UHS2_INT_DEADLOCK_TIMEOUT);
  235. /* UHS2 timing. Note, UHS2 timing is disabled when powering off */
  236. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  237. if (ios->power_mode != MMC_POWER_OFF &&
  238. (ios->timing == MMC_TIMING_UHS2_SPEED_A ||
  239. ios->timing == MMC_TIMING_UHS2_SPEED_A_HD ||
  240. ios->timing == MMC_TIMING_UHS2_SPEED_B ||
  241. ios->timing == MMC_TIMING_UHS2_SPEED_B_HD))
  242. ctrl_2 |= SDHCI_CTRL_UHS2 | SDHCI_CTRL_UHS2_ENABLE;
  243. else
  244. ctrl_2 &= ~(SDHCI_CTRL_UHS2 | SDHCI_CTRL_UHS2_ENABLE);
  245. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  246. host->timing = ios->timing;
  247. if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
  248. sdhci_enable_preset_value(host, true);
  249. if (host->ops->set_power)
  250. host->ops->set_power(host, ios->power_mode, ios->vdd);
  251. else
  252. sdhci_uhs2_set_power(host, ios->power_mode, ios->vdd);
  253. host->ops->set_clock(host, ios->clock);
  254. host->clock = ios->clock;
  255. }
  256. static int sdhci_uhs2_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  257. {
  258. struct sdhci_host *host = mmc_priv(mmc);
  259. pr_debug("%s: clock %uHz powermode %u Vdd %u timing %u\n",
  260. mmc_hostname(mmc), ios->clock, ios->power_mode, ios->vdd, ios->timing);
  261. if (!mmc_card_uhs2(mmc)) {
  262. sdhci_set_ios(mmc, ios);
  263. return 0;
  264. }
  265. if (ios->power_mode == MMC_POWER_UNDEFINED)
  266. return 0;
  267. if (host->flags & SDHCI_DEVICE_DEAD) {
  268. if (ios->power_mode == MMC_POWER_OFF) {
  269. mmc_opt_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  270. mmc_regulator_set_vqmmc2(mmc, ios);
  271. }
  272. return -1;
  273. }
  274. sdhci_set_ios_common(mmc, ios);
  275. __sdhci_uhs2_set_ios(mmc, ios);
  276. return 0;
  277. }
  278. static int sdhci_uhs2_interface_detect(struct sdhci_host *host)
  279. {
  280. u32 val;
  281. if (read_poll_timeout(sdhci_readl, val, (val & SDHCI_UHS2_IF_DETECT),
  282. 100, UHS2_INTERFACE_DETECT_TIMEOUT_100MS, true,
  283. host, SDHCI_PRESENT_STATE)) {
  284. pr_debug("%s: not detect UHS2 interface in 100ms.\n", mmc_hostname(host->mmc));
  285. sdhci_dbg_dumpregs(host, "UHS2 interface detect timeout in 100ms");
  286. return -EIO;
  287. }
  288. /* Enable UHS2 error interrupts */
  289. sdhci_uhs2_clear_set_irqs(host, SDHCI_INT_ALL_MASK, SDHCI_UHS2_INT_ERROR_MASK);
  290. if (read_poll_timeout(sdhci_readl, val, (val & SDHCI_UHS2_LANE_SYNC),
  291. 100, UHS2_LANE_SYNC_TIMEOUT_150MS, true, host, SDHCI_PRESENT_STATE)) {
  292. pr_debug("%s: UHS2 Lane sync fail in 150ms.\n", mmc_hostname(host->mmc));
  293. sdhci_dbg_dumpregs(host, "UHS2 Lane sync fail in 150ms");
  294. return -EIO;
  295. }
  296. DBG("%s: UHS2 Lane synchronized in UHS2 mode, PHY is initialized.\n",
  297. mmc_hostname(host->mmc));
  298. return 0;
  299. }
  300. static int sdhci_uhs2_init(struct sdhci_host *host)
  301. {
  302. u16 caps_ptr = 0;
  303. u32 caps_gen = 0;
  304. u32 caps_phy = 0;
  305. u32 caps_tran[2] = {0, 0};
  306. struct mmc_host *mmc = host->mmc;
  307. caps_ptr = sdhci_readw(host, SDHCI_UHS2_CAPS_PTR);
  308. if (caps_ptr < 0x100 || caps_ptr > 0x1FF) {
  309. pr_err("%s: SDHCI_UHS2_CAPS_PTR(%d) is wrong.\n",
  310. mmc_hostname(mmc), caps_ptr);
  311. return -ENODEV;
  312. }
  313. caps_gen = sdhci_readl(host, caps_ptr + SDHCI_UHS2_CAPS_OFFSET);
  314. caps_phy = sdhci_readl(host, caps_ptr + SDHCI_UHS2_CAPS_PHY_OFFSET);
  315. caps_tran[0] = sdhci_readl(host, caps_ptr + SDHCI_UHS2_CAPS_TRAN_OFFSET);
  316. caps_tran[1] = sdhci_readl(host, caps_ptr + SDHCI_UHS2_CAPS_TRAN_1_OFFSET);
  317. /* General Caps */
  318. mmc->uhs2_caps.dap = caps_gen & SDHCI_UHS2_CAPS_DAP_MASK;
  319. mmc->uhs2_caps.gap = FIELD_GET(SDHCI_UHS2_CAPS_GAP_MASK, caps_gen);
  320. mmc->uhs2_caps.n_lanes = FIELD_GET(SDHCI_UHS2_CAPS_LANE_MASK, caps_gen);
  321. mmc->uhs2_caps.addr64 = (caps_gen & SDHCI_UHS2_CAPS_ADDR_64) ? 1 : 0;
  322. mmc->uhs2_caps.card_type = FIELD_GET(SDHCI_UHS2_CAPS_DEV_TYPE_MASK, caps_gen);
  323. /* PHY Caps */
  324. mmc->uhs2_caps.phy_rev = caps_phy & SDHCI_UHS2_CAPS_PHY_REV_MASK;
  325. mmc->uhs2_caps.speed_range = FIELD_GET(SDHCI_UHS2_CAPS_PHY_RANGE_MASK, caps_phy);
  326. mmc->uhs2_caps.n_lss_sync = FIELD_GET(SDHCI_UHS2_CAPS_PHY_N_LSS_SYN_MASK, caps_phy);
  327. mmc->uhs2_caps.n_lss_dir = FIELD_GET(SDHCI_UHS2_CAPS_PHY_N_LSS_DIR_MASK, caps_phy);
  328. if (mmc->uhs2_caps.n_lss_sync == 0)
  329. mmc->uhs2_caps.n_lss_sync = 16 << 2;
  330. else
  331. mmc->uhs2_caps.n_lss_sync <<= 2;
  332. if (mmc->uhs2_caps.n_lss_dir == 0)
  333. mmc->uhs2_caps.n_lss_dir = 16 << 3;
  334. else
  335. mmc->uhs2_caps.n_lss_dir <<= 3;
  336. /* LINK/TRAN Caps */
  337. mmc->uhs2_caps.link_rev = caps_tran[0] & SDHCI_UHS2_CAPS_TRAN_LINK_REV_MASK;
  338. mmc->uhs2_caps.n_fcu = FIELD_GET(SDHCI_UHS2_CAPS_TRAN_N_FCU_MASK, caps_tran[0]);
  339. if (mmc->uhs2_caps.n_fcu == 0)
  340. mmc->uhs2_caps.n_fcu = 256;
  341. mmc->uhs2_caps.host_type = FIELD_GET(SDHCI_UHS2_CAPS_TRAN_HOST_TYPE_MASK, caps_tran[0]);
  342. mmc->uhs2_caps.maxblk_len = FIELD_GET(SDHCI_UHS2_CAPS_TRAN_BLK_LEN_MASK, caps_tran[0]);
  343. mmc->uhs2_caps.n_data_gap = caps_tran[1] & SDHCI_UHS2_CAPS_TRAN_1_N_DATA_GAP_MASK;
  344. return 0;
  345. }
  346. static int sdhci_uhs2_do_detect_init(struct mmc_host *mmc)
  347. {
  348. struct sdhci_host *host = mmc_priv(mmc);
  349. DBG("Begin do uhs2 detect init.\n");
  350. if (host->ops->uhs2_pre_detect_init)
  351. host->ops->uhs2_pre_detect_init(host);
  352. if (sdhci_uhs2_interface_detect(host)) {
  353. pr_debug("%s: cannot detect UHS2 interface.\n", mmc_hostname(host->mmc));
  354. return -EIO;
  355. }
  356. if (sdhci_uhs2_init(host)) {
  357. pr_debug("%s: UHS2 init fail.\n", mmc_hostname(host->mmc));
  358. return -EIO;
  359. }
  360. /* Init complete, do soft reset and enable UHS2 error irqs. */
  361. sdhci_uhs2_reset(host, SDHCI_UHS2_SW_RESET_SD);
  362. sdhci_uhs2_clear_set_irqs(host, SDHCI_INT_ALL_MASK, SDHCI_UHS2_INT_ERROR_MASK);
  363. /*
  364. * N.B SDHCI_INT_ENABLE and SDHCI_SIGNAL_ENABLE was cleared
  365. * by SDHCI_UHS2_SW_RESET_SD
  366. */
  367. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  368. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  369. return 0;
  370. }
  371. static int sdhci_uhs2_disable_clk(struct mmc_host *mmc)
  372. {
  373. struct sdhci_host *host = mmc_priv(mmc);
  374. u16 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  375. clk &= ~SDHCI_CLOCK_CARD_EN;
  376. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  377. return 0;
  378. }
  379. static int sdhci_uhs2_enable_clk(struct mmc_host *mmc)
  380. {
  381. struct sdhci_host *host = mmc_priv(mmc);
  382. u16 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  383. int timeout_us = 20000; /* 20ms */
  384. u32 val;
  385. clk |= SDHCI_CLOCK_CARD_EN;
  386. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  387. if (read_poll_timeout(sdhci_readw, val, (val & SDHCI_CLOCK_INT_STABLE),
  388. 10, timeout_us, true, host, SDHCI_CLOCK_CONTROL)) {
  389. pr_err("%s: Internal clock never stabilised.\n", mmc_hostname(host->mmc));
  390. sdhci_dumpregs(host);
  391. return -EIO;
  392. }
  393. return 0;
  394. }
  395. static void sdhci_uhs2_set_config(struct sdhci_host *host)
  396. {
  397. u32 value;
  398. u16 sdhci_uhs2_set_ptr = sdhci_readw(host, SDHCI_UHS2_SETTINGS_PTR);
  399. u16 sdhci_uhs2_gen_set_reg = sdhci_uhs2_set_ptr;
  400. u16 sdhci_uhs2_phy_set_reg = sdhci_uhs2_set_ptr + 4;
  401. u16 sdhci_uhs2_tran_set_reg = sdhci_uhs2_set_ptr + 8;
  402. u16 sdhci_uhs2_tran_set_1_reg = sdhci_uhs2_set_ptr + 12;
  403. /* Set Gen Settings */
  404. value = FIELD_PREP(SDHCI_UHS2_GEN_SETTINGS_N_LANES_MASK, host->mmc->uhs2_caps.n_lanes_set);
  405. sdhci_writel(host, value, sdhci_uhs2_gen_set_reg);
  406. /* Set PHY Settings */
  407. value = FIELD_PREP(SDHCI_UHS2_PHY_N_LSS_DIR_MASK, host->mmc->uhs2_caps.n_lss_dir_set) |
  408. FIELD_PREP(SDHCI_UHS2_PHY_N_LSS_SYN_MASK, host->mmc->uhs2_caps.n_lss_sync_set);
  409. if (host->mmc->ios.timing == MMC_TIMING_UHS2_SPEED_B ||
  410. host->mmc->ios.timing == MMC_TIMING_UHS2_SPEED_B_HD)
  411. value |= SDHCI_UHS2_PHY_SET_SPEED_B;
  412. sdhci_writel(host, value, sdhci_uhs2_phy_set_reg);
  413. /* Set LINK-TRAN Settings */
  414. value = FIELD_PREP(SDHCI_UHS2_TRAN_RETRY_CNT_MASK, host->mmc->uhs2_caps.max_retry_set) |
  415. FIELD_PREP(SDHCI_UHS2_TRAN_N_FCU_MASK, host->mmc->uhs2_caps.n_fcu_set);
  416. sdhci_writel(host, value, sdhci_uhs2_tran_set_reg);
  417. sdhci_writel(host, host->mmc->uhs2_caps.n_data_gap_set, sdhci_uhs2_tran_set_1_reg);
  418. }
  419. static int sdhci_uhs2_check_dormant(struct sdhci_host *host)
  420. {
  421. u32 val;
  422. if (read_poll_timeout(sdhci_readl, val, (val & SDHCI_UHS2_IN_DORMANT_STATE),
  423. 100, UHS2_CHECK_DORMANT_TIMEOUT_100MS, true, host,
  424. SDHCI_PRESENT_STATE)) {
  425. pr_debug("%s: UHS2 IN_DORMANT fail in 100ms.\n", mmc_hostname(host->mmc));
  426. sdhci_dbg_dumpregs(host, "UHS2 IN_DORMANT fail in 100ms");
  427. return -EIO;
  428. }
  429. return 0;
  430. }
  431. static int sdhci_uhs2_control(struct mmc_host *mmc, enum sd_uhs2_operation op)
  432. {
  433. struct sdhci_host *host = mmc_priv(mmc);
  434. struct mmc_ios *ios = &mmc->ios;
  435. int err = 0;
  436. DBG("Begin uhs2 control, act %d.\n", op);
  437. switch (op) {
  438. case UHS2_PHY_INIT:
  439. err = sdhci_uhs2_do_detect_init(mmc);
  440. break;
  441. case UHS2_SET_CONFIG:
  442. sdhci_uhs2_set_config(host);
  443. break;
  444. case UHS2_ENABLE_INT:
  445. sdhci_uhs2_clear_set_irqs(host, 0, SDHCI_INT_CARD_INT);
  446. break;
  447. case UHS2_DISABLE_INT:
  448. sdhci_uhs2_clear_set_irqs(host, SDHCI_INT_CARD_INT, 0);
  449. break;
  450. case UHS2_CHECK_DORMANT:
  451. err = sdhci_uhs2_check_dormant(host);
  452. break;
  453. case UHS2_DISABLE_CLK:
  454. err = sdhci_uhs2_disable_clk(mmc);
  455. break;
  456. case UHS2_ENABLE_CLK:
  457. err = sdhci_uhs2_enable_clk(mmc);
  458. break;
  459. case UHS2_SET_IOS:
  460. err = sdhci_uhs2_set_ios(mmc, ios);
  461. break;
  462. default:
  463. pr_err("%s: input sd uhs2 operation %d is wrong!\n",
  464. mmc_hostname(host->mmc), op);
  465. err = -EIO;
  466. break;
  467. }
  468. return err;
  469. }
  470. /*****************************************************************************\
  471. * *
  472. * Core functions *
  473. * *
  474. \*****************************************************************************/
  475. static void sdhci_uhs2_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
  476. {
  477. struct mmc_data *data = cmd->data;
  478. sdhci_initialize_data(host, data);
  479. sdhci_prepare_dma(host, data);
  480. sdhci_writew(host, data->blksz, SDHCI_UHS2_BLOCK_SIZE);
  481. sdhci_writew(host, data->blocks, SDHCI_UHS2_BLOCK_COUNT);
  482. }
  483. static void sdhci_uhs2_finish_data(struct sdhci_host *host)
  484. {
  485. struct mmc_data *data = host->data;
  486. __sdhci_finish_data_common(host, true);
  487. __sdhci_finish_mrq(host, data->mrq);
  488. }
  489. static void sdhci_uhs2_set_transfer_mode(struct sdhci_host *host, struct mmc_command *cmd)
  490. {
  491. u16 mode;
  492. struct mmc_data *data = cmd->data;
  493. if (!data) {
  494. /* clear Auto CMD settings for no data CMDs */
  495. if (uhs2_dev_cmd(cmd) == UHS2_DEV_CMD_TRANS_ABORT) {
  496. mode = 0;
  497. } else {
  498. mode = sdhci_readw(host, SDHCI_UHS2_TRANS_MODE);
  499. if (cmd->opcode == MMC_STOP_TRANSMISSION || cmd->opcode == MMC_ERASE)
  500. mode |= SDHCI_UHS2_TRNS_WAIT_EBSY;
  501. else
  502. /* send status mode */
  503. if (cmd->opcode == MMC_SEND_STATUS)
  504. mode = 0;
  505. }
  506. DBG("UHS2 no data trans mode is 0x%x.\n", mode);
  507. sdhci_writew(host, mode, SDHCI_UHS2_TRANS_MODE);
  508. return;
  509. }
  510. WARN_ON(!host->data);
  511. mode = SDHCI_UHS2_TRNS_BLK_CNT_EN | SDHCI_UHS2_TRNS_WAIT_EBSY;
  512. if (data->flags & MMC_DATA_WRITE)
  513. mode |= SDHCI_UHS2_TRNS_DATA_TRNS_WRT;
  514. if (data->blocks == 1 &&
  515. data->blksz != 512 &&
  516. cmd->opcode != MMC_READ_SINGLE_BLOCK &&
  517. cmd->opcode != MMC_WRITE_BLOCK) {
  518. mode &= ~SDHCI_UHS2_TRNS_BLK_CNT_EN;
  519. mode |= SDHCI_UHS2_TRNS_BLK_BYTE_MODE;
  520. }
  521. if (host->flags & SDHCI_REQ_USE_DMA)
  522. mode |= SDHCI_UHS2_TRNS_DMA;
  523. if (cmd->uhs2_cmd->tmode_half_duplex)
  524. mode |= SDHCI_UHS2_TRNS_2L_HD;
  525. sdhci_writew(host, mode, SDHCI_UHS2_TRANS_MODE);
  526. DBG("UHS2 trans mode is 0x%x.\n", mode);
  527. }
  528. static void __sdhci_uhs2_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  529. {
  530. int i, j;
  531. int cmd_reg;
  532. i = 0;
  533. sdhci_writel(host,
  534. ((u32)cmd->uhs2_cmd->arg << 16) |
  535. (u32)cmd->uhs2_cmd->header,
  536. SDHCI_UHS2_CMD_PACKET + i);
  537. i += 4;
  538. /*
  539. * Per spec, payload (config) should be MSB before sending out.
  540. * But we don't need convert here because had set payload as
  541. * MSB when preparing config read/write commands.
  542. */
  543. for (j = 0; j < cmd->uhs2_cmd->payload_len / sizeof(u32); j++) {
  544. sdhci_writel(host, *(__force u32 *)(cmd->uhs2_cmd->payload + j),
  545. SDHCI_UHS2_CMD_PACKET + i);
  546. i += 4;
  547. }
  548. for ( ; i < SDHCI_UHS2_CMD_PACK_MAX_LEN; i += 4)
  549. sdhci_writel(host, 0, SDHCI_UHS2_CMD_PACKET + i);
  550. DBG("UHS2 CMD packet_len = %d.\n", cmd->uhs2_cmd->packet_len);
  551. for (i = 0; i < cmd->uhs2_cmd->packet_len; i++)
  552. DBG("UHS2 CMD_PACKET[%d] = 0x%x.\n", i,
  553. sdhci_readb(host, SDHCI_UHS2_CMD_PACKET + i));
  554. cmd_reg = FIELD_PREP(SDHCI_UHS2_CMD_PACK_LEN_MASK, cmd->uhs2_cmd->packet_len);
  555. if ((cmd->flags & MMC_CMD_MASK) == MMC_CMD_ADTC)
  556. cmd_reg |= SDHCI_UHS2_CMD_DATA;
  557. if (cmd->opcode == MMC_STOP_TRANSMISSION)
  558. cmd_reg |= SDHCI_UHS2_CMD_CMD12;
  559. /* UHS2 Native ABORT */
  560. if ((cmd->uhs2_cmd->header & UHS2_NATIVE_PACKET) &&
  561. (uhs2_dev_cmd(cmd) == UHS2_DEV_CMD_TRANS_ABORT))
  562. cmd_reg |= SDHCI_UHS2_CMD_TRNS_ABORT;
  563. /* UHS2 Native DORMANT */
  564. if ((cmd->uhs2_cmd->header & UHS2_NATIVE_PACKET) &&
  565. (uhs2_dev_cmd(cmd) == UHS2_DEV_CMD_GO_DORMANT_STATE))
  566. cmd_reg |= SDHCI_UHS2_CMD_DORMANT;
  567. DBG("0x%x is set to UHS2 CMD register.\n", cmd_reg);
  568. sdhci_writew(host, cmd_reg, SDHCI_UHS2_CMD);
  569. }
  570. static bool sdhci_uhs2_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  571. {
  572. u32 mask;
  573. unsigned long timeout;
  574. WARN_ON(host->cmd);
  575. /* Initially, a command has no error */
  576. cmd->error = 0;
  577. if (cmd->opcode == MMC_STOP_TRANSMISSION)
  578. cmd->flags |= MMC_RSP_BUSY;
  579. mask = SDHCI_CMD_INHIBIT;
  580. if (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask)
  581. return false;
  582. host->cmd = cmd;
  583. host->data_timeout = 0;
  584. if (sdhci_data_line_cmd(cmd)) {
  585. WARN_ON(host->data_cmd);
  586. host->data_cmd = cmd;
  587. __sdhci_uhs2_set_timeout(host);
  588. }
  589. if (cmd->data)
  590. sdhci_uhs2_prepare_data(host, cmd);
  591. sdhci_uhs2_set_transfer_mode(host, cmd);
  592. timeout = jiffies;
  593. if (host->data_timeout)
  594. timeout += nsecs_to_jiffies(host->data_timeout);
  595. else if (!cmd->data && cmd->busy_timeout > 9000)
  596. timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  597. else
  598. timeout += 10 * HZ;
  599. sdhci_mod_timer(host, cmd->mrq, timeout);
  600. __sdhci_uhs2_send_command(host, cmd);
  601. return true;
  602. }
  603. static bool sdhci_uhs2_send_command_retry(struct sdhci_host *host,
  604. struct mmc_command *cmd,
  605. unsigned long flags)
  606. __releases(host->lock)
  607. __acquires(host->lock)
  608. {
  609. struct mmc_command *deferred_cmd = host->deferred_cmd;
  610. int timeout = 10; /* Approx. 10 ms */
  611. bool present;
  612. while (!sdhci_uhs2_send_command(host, cmd)) {
  613. if (!timeout--) {
  614. pr_err("%s: Controller never released inhibit bit(s).\n",
  615. mmc_hostname(host->mmc));
  616. sdhci_dumpregs(host);
  617. cmd->error = -EIO;
  618. return false;
  619. }
  620. spin_unlock_irqrestore(&host->lock, flags);
  621. usleep_range(1000, 1250);
  622. present = host->mmc->ops->get_cd(host->mmc);
  623. spin_lock_irqsave(&host->lock, flags);
  624. /* A deferred command might disappear, handle that */
  625. if (cmd == deferred_cmd && cmd != host->deferred_cmd)
  626. return true;
  627. if (sdhci_present_error(host, cmd, present))
  628. return false;
  629. }
  630. if (cmd == host->deferred_cmd)
  631. host->deferred_cmd = NULL;
  632. return true;
  633. }
  634. static void __sdhci_uhs2_finish_command(struct sdhci_host *host)
  635. {
  636. struct mmc_command *cmd = host->cmd;
  637. u8 resp;
  638. u8 error_code;
  639. bool breada0 = 0;
  640. int i;
  641. if (host->mmc->uhs2_sd_tran) {
  642. resp = sdhci_readb(host, SDHCI_UHS2_RESPONSE + 2);
  643. if (resp & UHS2_RES_NACK_MASK) {
  644. error_code = (resp >> UHS2_RES_ECODE_POS) & UHS2_RES_ECODE_MASK;
  645. pr_err("%s: NACK response, ECODE=0x%x.\n",
  646. mmc_hostname(host->mmc), error_code);
  647. }
  648. breada0 = 1;
  649. }
  650. if (cmd->uhs2_cmd->uhs2_resp_len) {
  651. int len = min_t(int, cmd->uhs2_cmd->uhs2_resp_len, UHS2_MAX_RESP_LEN);
  652. /* Get whole response of some native CCMD, like
  653. * DEVICE_INIT, ENUMERATE.
  654. */
  655. for (i = 0; i < len; i++)
  656. cmd->uhs2_cmd->uhs2_resp[i] = sdhci_readb(host, SDHCI_UHS2_RESPONSE + i);
  657. } else {
  658. /* Get SD CMD response and Payload for some read
  659. * CCMD, like INQUIRY_CFG.
  660. */
  661. /* Per spec (p136), payload field is divided into
  662. * a unit of DWORD and transmission order within
  663. * a DWORD is big endian.
  664. */
  665. if (!breada0)
  666. sdhci_readl(host, SDHCI_UHS2_RESPONSE);
  667. for (i = 4; i < 20; i += 4) {
  668. cmd->resp[i / 4 - 1] =
  669. (sdhci_readb(host,
  670. SDHCI_UHS2_RESPONSE + i) << 24) |
  671. (sdhci_readb(host,
  672. SDHCI_UHS2_RESPONSE + i + 1)
  673. << 16) |
  674. (sdhci_readb(host,
  675. SDHCI_UHS2_RESPONSE + i + 2)
  676. << 8) |
  677. sdhci_readb(host, SDHCI_UHS2_RESPONSE + i + 3);
  678. }
  679. }
  680. }
  681. static void sdhci_uhs2_finish_command(struct sdhci_host *host)
  682. {
  683. struct mmc_command *cmd = host->cmd;
  684. __sdhci_uhs2_finish_command(host);
  685. host->cmd = NULL;
  686. if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
  687. mmc_command_done(host->mmc, cmd->mrq);
  688. /*
  689. * The host can send and interrupt when the busy state has
  690. * ended, allowing us to wait without wasting CPU cycles.
  691. * The busy signal uses DAT0 so this is similar to waiting
  692. * for data to complete.
  693. *
  694. * Note: The 1.0 specification is a bit ambiguous about this
  695. * feature so there might be some problems with older
  696. * controllers.
  697. */
  698. if (cmd->flags & MMC_RSP_BUSY) {
  699. if (cmd->data) {
  700. DBG("Cannot wait for busy signal when also doing a data transfer");
  701. } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
  702. cmd == host->data_cmd) {
  703. /* Command complete before busy is ended */
  704. return;
  705. }
  706. }
  707. /* Processed actual command. */
  708. if (host->data && host->data_early)
  709. sdhci_uhs2_finish_data(host);
  710. if (!cmd->data)
  711. __sdhci_finish_mrq(host, cmd->mrq);
  712. }
  713. static void sdhci_uhs2_request(struct mmc_host *mmc, struct mmc_request *mrq)
  714. {
  715. struct sdhci_host *host = mmc_priv(mmc);
  716. struct mmc_command *cmd;
  717. unsigned long flags;
  718. bool present;
  719. if (!(mmc_card_uhs2(mmc))) {
  720. sdhci_request(mmc, mrq);
  721. return;
  722. }
  723. mrq->stop = NULL;
  724. mrq->sbc = NULL;
  725. if (mrq->data)
  726. mrq->data->stop = NULL;
  727. /* Firstly check card presence */
  728. present = mmc->ops->get_cd(mmc);
  729. spin_lock_irqsave(&host->lock, flags);
  730. if (sdhci_present_error(host, mrq->cmd, present))
  731. goto out_finish;
  732. cmd = mrq->cmd;
  733. if (!sdhci_uhs2_send_command_retry(host, cmd, flags))
  734. goto out_finish;
  735. spin_unlock_irqrestore(&host->lock, flags);
  736. return;
  737. out_finish:
  738. sdhci_finish_mrq(host, mrq);
  739. spin_unlock_irqrestore(&host->lock, flags);
  740. }
  741. /*****************************************************************************\
  742. * *
  743. * Request done *
  744. * *
  745. \*****************************************************************************/
  746. static bool sdhci_uhs2_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
  747. {
  748. return sdhci_needs_reset(host, mrq) ||
  749. (!(host->flags & SDHCI_DEVICE_DEAD) && mrq->data && mrq->data->error);
  750. }
  751. static bool sdhci_uhs2_request_done(struct sdhci_host *host)
  752. {
  753. unsigned long flags;
  754. struct mmc_request *mrq;
  755. int i;
  756. spin_lock_irqsave(&host->lock, flags);
  757. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  758. mrq = host->mrqs_done[i];
  759. if (mrq)
  760. break;
  761. }
  762. if (!mrq) {
  763. spin_unlock_irqrestore(&host->lock, flags);
  764. return true;
  765. }
  766. /*
  767. * Always unmap the data buffers if they were mapped by
  768. * sdhci_prepare_data() whenever we finish with a request.
  769. * This avoids leaking DMA mappings on error.
  770. */
  771. if (host->flags & SDHCI_REQ_USE_DMA)
  772. sdhci_request_done_dma(host, mrq);
  773. /*
  774. * The controller needs a reset of internal state machines
  775. * upon error conditions.
  776. */
  777. if (sdhci_uhs2_needs_reset(host, mrq)) {
  778. /*
  779. * Do not finish until command and data lines are available for
  780. * reset. Note there can only be one other mrq, so it cannot
  781. * also be in mrqs_done, otherwise host->cmd and host->data_cmd
  782. * would both be null.
  783. */
  784. if (host->cmd || host->data_cmd) {
  785. spin_unlock_irqrestore(&host->lock, flags);
  786. return true;
  787. }
  788. if (mrq->cmd->error || mrq->data->error)
  789. sdhci_uhs2_reset_cmd_data(host);
  790. else
  791. sdhci_uhs2_reset(host, SDHCI_UHS2_SW_RESET_SD);
  792. host->pending_reset = false;
  793. }
  794. host->mrqs_done[i] = NULL;
  795. spin_unlock_irqrestore(&host->lock, flags);
  796. if (host->ops->request_done)
  797. host->ops->request_done(host, mrq);
  798. else
  799. mmc_request_done(host->mmc, mrq);
  800. return false;
  801. }
  802. static void sdhci_uhs2_complete_work(struct work_struct *work)
  803. {
  804. struct sdhci_host *host = container_of(work, struct sdhci_host,
  805. complete_work);
  806. if (!mmc_card_uhs2(host->mmc)) {
  807. sdhci_complete_work(work);
  808. return;
  809. }
  810. while (!sdhci_uhs2_request_done(host))
  811. ;
  812. }
  813. /*****************************************************************************\
  814. * *
  815. * Interrupt handling *
  816. * *
  817. \*****************************************************************************/
  818. static void __sdhci_uhs2_irq(struct sdhci_host *host, u32 uhs2mask)
  819. {
  820. struct mmc_command *cmd = host->cmd;
  821. DBG("*** %s got UHS2 error interrupt: 0x%08x\n",
  822. mmc_hostname(host->mmc), uhs2mask);
  823. if (uhs2mask & SDHCI_UHS2_INT_CMD_ERR_MASK) {
  824. if (!host->cmd) {
  825. pr_err("%s: Got cmd interrupt 0x%08x but no cmd.\n",
  826. mmc_hostname(host->mmc),
  827. (unsigned int)uhs2mask);
  828. sdhci_dumpregs(host);
  829. return;
  830. }
  831. host->cmd->error = -EILSEQ;
  832. if (uhs2mask & SDHCI_UHS2_INT_CMD_TIMEOUT)
  833. host->cmd->error = -ETIMEDOUT;
  834. }
  835. if (uhs2mask & SDHCI_UHS2_INT_DATA_ERR_MASK) {
  836. if (!host->data) {
  837. pr_err("%s: Got data interrupt 0x%08x but no data.\n",
  838. mmc_hostname(host->mmc),
  839. (unsigned int)uhs2mask);
  840. sdhci_dumpregs(host);
  841. return;
  842. }
  843. if (uhs2mask & SDHCI_UHS2_INT_DEADLOCK_TIMEOUT) {
  844. pr_err("%s: Got deadlock timeout interrupt 0x%08x\n",
  845. mmc_hostname(host->mmc),
  846. (unsigned int)uhs2mask);
  847. host->data->error = -ETIMEDOUT;
  848. } else if (uhs2mask & SDHCI_UHS2_INT_ADMA_ERROR) {
  849. pr_err("%s: ADMA error = 0x %x\n",
  850. mmc_hostname(host->mmc),
  851. sdhci_readb(host, SDHCI_ADMA_ERROR));
  852. host->data->error = -EIO;
  853. } else {
  854. host->data->error = -EILSEQ;
  855. }
  856. }
  857. if (host->data && host->data->error)
  858. sdhci_uhs2_finish_data(host);
  859. else
  860. sdhci_finish_mrq(host, cmd->mrq);
  861. }
  862. u32 sdhci_uhs2_irq(struct sdhci_host *host, u32 intmask)
  863. {
  864. u32 mask = intmask, uhs2mask;
  865. if (!mmc_card_uhs2(host->mmc))
  866. goto out;
  867. if (intmask & SDHCI_INT_ERROR) {
  868. uhs2mask = sdhci_readl(host, SDHCI_UHS2_INT_STATUS);
  869. if (!(uhs2mask & SDHCI_UHS2_INT_ERROR_MASK))
  870. goto cmd_irq;
  871. /* Clear error interrupts */
  872. sdhci_writel(host, uhs2mask & SDHCI_UHS2_INT_ERROR_MASK,
  873. SDHCI_UHS2_INT_STATUS);
  874. /* Handle error interrupts */
  875. __sdhci_uhs2_irq(host, uhs2mask);
  876. /* Caller, sdhci_irq(), doesn't have to care about UHS-2 errors */
  877. intmask &= ~SDHCI_INT_ERROR;
  878. mask &= SDHCI_INT_ERROR;
  879. }
  880. cmd_irq:
  881. if (intmask & SDHCI_INT_CMD_MASK) {
  882. /* Clear command interrupt */
  883. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK, SDHCI_INT_STATUS);
  884. /* Handle command interrupt */
  885. if (intmask & SDHCI_INT_RESPONSE)
  886. sdhci_uhs2_finish_command(host);
  887. /* Caller, sdhci_irq(), doesn't have to care about UHS-2 commands */
  888. intmask &= ~SDHCI_INT_CMD_MASK;
  889. mask &= SDHCI_INT_CMD_MASK;
  890. }
  891. /* Clear already-handled interrupts. */
  892. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  893. out:
  894. return intmask;
  895. }
  896. EXPORT_SYMBOL_GPL(sdhci_uhs2_irq);
  897. static irqreturn_t sdhci_uhs2_thread_irq(int irq, void *dev_id)
  898. {
  899. struct sdhci_host *host = dev_id;
  900. struct mmc_command *cmd;
  901. unsigned long flags;
  902. u32 isr;
  903. if (!mmc_card_uhs2(host->mmc))
  904. return sdhci_thread_irq(irq, dev_id);
  905. while (!sdhci_uhs2_request_done(host))
  906. ;
  907. spin_lock_irqsave(&host->lock, flags);
  908. isr = host->thread_isr;
  909. host->thread_isr = 0;
  910. cmd = host->deferred_cmd;
  911. if (cmd && !sdhci_uhs2_send_command_retry(host, cmd, flags))
  912. sdhci_finish_mrq(host, cmd->mrq);
  913. spin_unlock_irqrestore(&host->lock, flags);
  914. if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  915. struct mmc_host *mmc = host->mmc;
  916. mmc->ops->card_event(mmc);
  917. mmc_detect_change(mmc, msecs_to_jiffies(200));
  918. }
  919. return IRQ_HANDLED;
  920. }
  921. /*****************************************************************************\
  922. * *
  923. * Driver init/exit *
  924. * *
  925. \*****************************************************************************/
  926. static int sdhci_uhs2_host_ops_init(struct sdhci_host *host)
  927. {
  928. host->mmc_host_ops.uhs2_control = sdhci_uhs2_control;
  929. host->mmc_host_ops.request = sdhci_uhs2_request;
  930. return 0;
  931. }
  932. static int __init sdhci_uhs2_mod_init(void)
  933. {
  934. return 0;
  935. }
  936. module_init(sdhci_uhs2_mod_init);
  937. static void __exit sdhci_uhs2_mod_exit(void)
  938. {
  939. }
  940. module_exit(sdhci_uhs2_mod_exit);
  941. /*****************************************************************************\
  942. *
  943. * Device allocation/registration *
  944. * *
  945. \*****************************************************************************/
  946. static void __sdhci_uhs2_add_host_v4(struct sdhci_host *host, u32 caps1)
  947. {
  948. struct mmc_host *mmc;
  949. u32 max_current_caps2;
  950. mmc = host->mmc;
  951. /* Support UHS2 */
  952. if (caps1 & SDHCI_SUPPORT_UHS2)
  953. mmc->caps2 |= MMC_CAP2_SD_UHS2;
  954. max_current_caps2 = sdhci_readl(host, SDHCI_MAX_CURRENT_1);
  955. if ((caps1 & SDHCI_CAN_VDD2_180) &&
  956. !max_current_caps2 &&
  957. !IS_ERR(mmc->supply.vqmmc2)) {
  958. /* UHS2 - VDD2 */
  959. int curr = regulator_get_current_limit(mmc->supply.vqmmc2);
  960. if (curr > 0) {
  961. /* convert to SDHCI_MAX_CURRENT format */
  962. curr = curr / 1000; /* convert to mA */
  963. curr = curr / SDHCI_MAX_CURRENT_MULTIPLIER;
  964. curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
  965. max_current_caps2 = curr;
  966. }
  967. }
  968. if (!(caps1 & SDHCI_CAN_VDD2_180))
  969. mmc->caps2 &= ~MMC_CAP2_SD_UHS2;
  970. }
  971. static void __sdhci_uhs2_remove_host(struct sdhci_host *host, int dead)
  972. {
  973. if (!mmc_card_uhs2(host->mmc))
  974. return;
  975. if (!dead)
  976. sdhci_uhs2_reset(host, SDHCI_UHS2_SW_RESET_FULL);
  977. }
  978. int sdhci_uhs2_add_host(struct sdhci_host *host)
  979. {
  980. struct mmc_host *mmc = host->mmc;
  981. int ret;
  982. ret = sdhci_setup_host(host);
  983. if (ret)
  984. return ret;
  985. if (host->version >= SDHCI_SPEC_400)
  986. __sdhci_uhs2_add_host_v4(host, host->caps1);
  987. if ((mmc->caps2 & MMC_CAP2_SD_UHS2) && !host->v4_mode)
  988. /* host doesn't want to enable UHS2 support */
  989. mmc->caps2 &= ~MMC_CAP2_SD_UHS2;
  990. /* overwrite ops */
  991. if (mmc->caps2 & MMC_CAP2_SD_UHS2)
  992. sdhci_uhs2_host_ops_init(host);
  993. host->complete_work_fn = sdhci_uhs2_complete_work;
  994. host->thread_irq_fn = sdhci_uhs2_thread_irq;
  995. /* LED support not implemented for UHS2 */
  996. host->quirks |= SDHCI_QUIRK_NO_LED;
  997. ret = __sdhci_add_host(host);
  998. if (ret)
  999. goto cleanup;
  1000. return 0;
  1001. cleanup:
  1002. if (host->version >= SDHCI_SPEC_400)
  1003. __sdhci_uhs2_remove_host(host, 0);
  1004. sdhci_cleanup_host(host);
  1005. return ret;
  1006. }
  1007. EXPORT_SYMBOL_GPL(sdhci_uhs2_add_host);
  1008. void sdhci_uhs2_remove_host(struct sdhci_host *host, int dead)
  1009. {
  1010. __sdhci_uhs2_remove_host(host, dead);
  1011. sdhci_remove_host(host, dead);
  1012. }
  1013. EXPORT_SYMBOL_GPL(sdhci_uhs2_remove_host);
  1014. MODULE_AUTHOR("Intel, Genesys Logic, Linaro");
  1015. MODULE_DESCRIPTION("MMC UHS-II Support");
  1016. MODULE_LICENSE("GPL");