0048-mmc-sdhci-msm-Add-platform_execute_tuning-implementa.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. From c2a237b3e467c8bb349c4624b71ec400abaf8ad1 Mon Sep 17 00:00:00 2001
  2. From: Georgi Djakov <[email protected]>
  3. Date: Mon, 10 Mar 2014 17:37:13 +0200
  4. Subject: [PATCH 048/182] mmc: sdhci-msm: Add platform_execute_tuning
  5. implementation
  6. This patch adds implementation for platform specific tuning in order
  7. to support HS200 bus speed mode on Qualcomm SDHCI controller.
  8. Signed-off-by: Asutosh Das <[email protected]>
  9. Signed-off-by: Venkat Gopalakrishnan <[email protected]>
  10. Signed-off-by: Georgi Djakov <[email protected]>
  11. Acked-by: Ulf Hansson <[email protected]>
  12. Signed-off-by: Chris Ball <[email protected]>
  13. ---
  14. drivers/mmc/host/sdhci-msm.c | 420 +++++++++++++++++++++++++++++++++++++++++-
  15. 1 file changed, 415 insertions(+), 5 deletions(-)
  16. --- a/drivers/mmc/host/sdhci-msm.c
  17. +++ b/drivers/mmc/host/sdhci-msm.c
  18. @@ -18,6 +18,8 @@
  19. #include <linux/of_device.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/delay.h>
  22. +#include <linux/mmc/mmc.h>
  23. +#include <linux/slab.h>
  24. #include "sdhci-pltfm.h"
  25. @@ -26,6 +28,42 @@
  26. #define CORE_POWER 0x0
  27. #define CORE_SW_RST BIT(7)
  28. +#define MAX_PHASES 16
  29. +#define CORE_DLL_LOCK BIT(7)
  30. +#define CORE_DLL_EN BIT(16)
  31. +#define CORE_CDR_EN BIT(17)
  32. +#define CORE_CK_OUT_EN BIT(18)
  33. +#define CORE_CDR_EXT_EN BIT(19)
  34. +#define CORE_DLL_PDN BIT(29)
  35. +#define CORE_DLL_RST BIT(30)
  36. +#define CORE_DLL_CONFIG 0x100
  37. +#define CORE_DLL_STATUS 0x108
  38. +
  39. +#define CORE_VENDOR_SPEC 0x10c
  40. +#define CORE_CLK_PWRSAVE BIT(1)
  41. +
  42. +#define CDR_SELEXT_SHIFT 20
  43. +#define CDR_SELEXT_MASK (0xf << CDR_SELEXT_SHIFT)
  44. +#define CMUX_SHIFT_PHASE_SHIFT 24
  45. +#define CMUX_SHIFT_PHASE_MASK (7 << CMUX_SHIFT_PHASE_SHIFT)
  46. +
  47. +static const u32 tuning_block_64[] = {
  48. + 0x00ff0fff, 0xccc3ccff, 0xffcc3cc3, 0xeffefffe,
  49. + 0xddffdfff, 0xfbfffbff, 0xff7fffbf, 0xefbdf777,
  50. + 0xf0fff0ff, 0x3cccfc0f, 0xcfcc33cc, 0xeeffefff,
  51. + 0xfdfffdff, 0xffbfffdf, 0xfff7ffbb, 0xde7b7ff7
  52. +};
  53. +
  54. +static const u32 tuning_block_128[] = {
  55. + 0xff00ffff, 0x0000ffff, 0xccccffff, 0xcccc33cc,
  56. + 0xcc3333cc, 0xffffcccc, 0xffffeeff, 0xffeeeeff,
  57. + 0xffddffff, 0xddddffff, 0xbbffffff, 0xbbffffff,
  58. + 0xffffffbb, 0xffffff77, 0x77ff7777, 0xffeeddbb,
  59. + 0x00ffffff, 0x00ffffff, 0xccffff00, 0xcc33cccc,
  60. + 0x3333cccc, 0xffcccccc, 0xffeeffff, 0xeeeeffff,
  61. + 0xddffffff, 0xddffffff, 0xffffffdd, 0xffffffbb,
  62. + 0xffffbbbb, 0xffff77ff, 0xff7777ff, 0xeeddbb77
  63. +};
  64. struct sdhci_msm_host {
  65. struct platform_device *pdev;
  66. @@ -38,17 +76,389 @@ struct sdhci_msm_host {
  67. };
  68. /* Platform specific tuning */
  69. -static int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
  70. +static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host, u8 poll)
  71. +{
  72. + u32 wait_cnt = 50;
  73. + u8 ck_out_en;
  74. + struct mmc_host *mmc = host->mmc;
  75. +
  76. + /* Poll for CK_OUT_EN bit. max. poll time = 50us */
  77. + ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
  78. + CORE_CK_OUT_EN);
  79. +
  80. + while (ck_out_en != poll) {
  81. + if (--wait_cnt == 0) {
  82. + dev_err(mmc_dev(mmc), "%s: CK_OUT_EN bit is not %d\n",
  83. + mmc_hostname(mmc), poll);
  84. + return -ETIMEDOUT;
  85. + }
  86. + udelay(1);
  87. +
  88. + ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
  89. + CORE_CK_OUT_EN);
  90. + }
  91. +
  92. + return 0;
  93. +}
  94. +
  95. +static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
  96. +{
  97. + int rc;
  98. + static const u8 grey_coded_phase_table[] = {
  99. + 0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
  100. + 0xc, 0xd, 0xf, 0xe, 0xa, 0xb, 0x9, 0x8
  101. + };
  102. + unsigned long flags;
  103. + u32 config;
  104. + struct mmc_host *mmc = host->mmc;
  105. +
  106. + spin_lock_irqsave(&host->lock, flags);
  107. +
  108. + config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  109. + config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
  110. + config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
  111. + writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  112. +
  113. + /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
  114. + rc = msm_dll_poll_ck_out_en(host, 0);
  115. + if (rc)
  116. + goto err_out;
  117. +
  118. + /*
  119. + * Write the selected DLL clock output phase (0 ... 15)
  120. + * to CDR_SELEXT bit field of DLL_CONFIG register.
  121. + */
  122. + config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  123. + config &= ~CDR_SELEXT_MASK;
  124. + config |= grey_coded_phase_table[phase] << CDR_SELEXT_SHIFT;
  125. + writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  126. +
  127. + /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
  128. + writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  129. + | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
  130. +
  131. + /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
  132. + rc = msm_dll_poll_ck_out_en(host, 1);
  133. + if (rc)
  134. + goto err_out;
  135. +
  136. + config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  137. + config |= CORE_CDR_EN;
  138. + config &= ~CORE_CDR_EXT_EN;
  139. + writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  140. + goto out;
  141. +
  142. +err_out:
  143. + dev_err(mmc_dev(mmc), "%s: Failed to set DLL phase: %d\n",
  144. + mmc_hostname(mmc), phase);
  145. +out:
  146. + spin_unlock_irqrestore(&host->lock, flags);
  147. + return rc;
  148. +}
  149. +
  150. +/*
  151. + * Find out the greatest range of consecuitive selected
  152. + * DLL clock output phases that can be used as sampling
  153. + * setting for SD3.0 UHS-I card read operation (in SDR104
  154. + * timing mode) or for eMMC4.5 card read operation (in HS200
  155. + * timing mode).
  156. + * Select the 3/4 of the range and configure the DLL with the
  157. + * selected DLL clock output phase.
  158. + */
  159. +
  160. +static int msm_find_most_appropriate_phase(struct sdhci_host *host,
  161. + u8 *phase_table, u8 total_phases)
  162. +{
  163. + int ret;
  164. + u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
  165. + u8 phases_per_row[MAX_PHASES] = { 0 };
  166. + int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
  167. + int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
  168. + bool phase_0_found = false, phase_15_found = false;
  169. + struct mmc_host *mmc = host->mmc;
  170. +
  171. + if (!total_phases || (total_phases > MAX_PHASES)) {
  172. + dev_err(mmc_dev(mmc), "%s: Invalid argument: total_phases=%d\n",
  173. + mmc_hostname(mmc), total_phases);
  174. + return -EINVAL;
  175. + }
  176. +
  177. + for (cnt = 0; cnt < total_phases; cnt++) {
  178. + ranges[row_index][col_index] = phase_table[cnt];
  179. + phases_per_row[row_index] += 1;
  180. + col_index++;
  181. +
  182. + if ((cnt + 1) == total_phases) {
  183. + continue;
  184. + /* check if next phase in phase_table is consecutive or not */
  185. + } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
  186. + row_index++;
  187. + col_index = 0;
  188. + }
  189. + }
  190. +
  191. + if (row_index >= MAX_PHASES)
  192. + return -EINVAL;
  193. +
  194. + /* Check if phase-0 is present in first valid window? */
  195. + if (!ranges[0][0]) {
  196. + phase_0_found = true;
  197. + phase_0_raw_index = 0;
  198. + /* Check if cycle exist between 2 valid windows */
  199. + for (cnt = 1; cnt <= row_index; cnt++) {
  200. + if (phases_per_row[cnt]) {
  201. + for (i = 0; i < phases_per_row[cnt]; i++) {
  202. + if (ranges[cnt][i] == 15) {
  203. + phase_15_found = true;
  204. + phase_15_raw_index = cnt;
  205. + break;
  206. + }
  207. + }
  208. + }
  209. + }
  210. + }
  211. +
  212. + /* If 2 valid windows form cycle then merge them as single window */
  213. + if (phase_0_found && phase_15_found) {
  214. + /* number of phases in raw where phase 0 is present */
  215. + u8 phases_0 = phases_per_row[phase_0_raw_index];
  216. + /* number of phases in raw where phase 15 is present */
  217. + u8 phases_15 = phases_per_row[phase_15_raw_index];
  218. +
  219. + if (phases_0 + phases_15 >= MAX_PHASES)
  220. + /*
  221. + * If there are more than 1 phase windows then total
  222. + * number of phases in both the windows should not be
  223. + * more than or equal to MAX_PHASES.
  224. + */
  225. + return -EINVAL;
  226. +
  227. + /* Merge 2 cyclic windows */
  228. + i = phases_15;
  229. + for (cnt = 0; cnt < phases_0; cnt++) {
  230. + ranges[phase_15_raw_index][i] =
  231. + ranges[phase_0_raw_index][cnt];
  232. + if (++i >= MAX_PHASES)
  233. + break;
  234. + }
  235. +
  236. + phases_per_row[phase_0_raw_index] = 0;
  237. + phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
  238. + }
  239. +
  240. + for (cnt = 0; cnt <= row_index; cnt++) {
  241. + if (phases_per_row[cnt] > curr_max) {
  242. + curr_max = phases_per_row[cnt];
  243. + selected_row_index = cnt;
  244. + }
  245. + }
  246. +
  247. + i = (curr_max * 3) / 4;
  248. + if (i)
  249. + i--;
  250. +
  251. + ret = ranges[selected_row_index][i];
  252. +
  253. + if (ret >= MAX_PHASES) {
  254. + ret = -EINVAL;
  255. + dev_err(mmc_dev(mmc), "%s: Invalid phase selected=%d\n",
  256. + mmc_hostname(mmc), ret);
  257. + }
  258. +
  259. + return ret;
  260. +}
  261. +
  262. +static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
  263. +{
  264. + u32 mclk_freq = 0, config;
  265. +
  266. + /* Program the MCLK value to MCLK_FREQ bit field */
  267. + if (host->clock <= 112000000)
  268. + mclk_freq = 0;
  269. + else if (host->clock <= 125000000)
  270. + mclk_freq = 1;
  271. + else if (host->clock <= 137000000)
  272. + mclk_freq = 2;
  273. + else if (host->clock <= 150000000)
  274. + mclk_freq = 3;
  275. + else if (host->clock <= 162000000)
  276. + mclk_freq = 4;
  277. + else if (host->clock <= 175000000)
  278. + mclk_freq = 5;
  279. + else if (host->clock <= 187000000)
  280. + mclk_freq = 6;
  281. + else if (host->clock <= 200000000)
  282. + mclk_freq = 7;
  283. +
  284. + config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  285. + config &= ~CMUX_SHIFT_PHASE_MASK;
  286. + config |= mclk_freq << CMUX_SHIFT_PHASE_SHIFT;
  287. + writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  288. +}
  289. +
  290. +/* Initialize the DLL (Programmable Delay Line) */
  291. +static int msm_init_cm_dll(struct sdhci_host *host)
  292. {
  293. + struct mmc_host *mmc = host->mmc;
  294. + int wait_cnt = 50;
  295. + unsigned long flags;
  296. +
  297. + spin_lock_irqsave(&host->lock, flags);
  298. +
  299. /*
  300. - * Tuning is required for SDR104, HS200 and HS400 cards and if the clock
  301. - * frequency greater than 100MHz in those modes. The standard tuning
  302. - * procedure should not be executed, but a custom implementation will be
  303. - * added here instead.
  304. + * Make sure that clock is always enabled when DLL
  305. + * tuning is in progress. Keeping PWRSAVE ON may
  306. + * turn off the clock.
  307. */
  308. + writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  309. + & ~CORE_CLK_PWRSAVE), host->ioaddr + CORE_VENDOR_SPEC);
  310. +
  311. + /* Write 1 to DLL_RST bit of DLL_CONFIG register */
  312. + writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  313. + | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
  314. +
  315. + /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
  316. + writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  317. + | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
  318. + msm_cm_dll_set_freq(host);
  319. +
  320. + /* Write 0 to DLL_RST bit of DLL_CONFIG register */
  321. + writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  322. + & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
  323. +
  324. + /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
  325. + writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  326. + & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
  327. +
  328. + /* Set DLL_EN bit to 1. */
  329. + writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  330. + | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
  331. +
  332. + /* Set CK_OUT_EN bit to 1. */
  333. + writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  334. + | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
  335. +
  336. + /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
  337. + while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
  338. + CORE_DLL_LOCK)) {
  339. + /* max. wait for 50us sec for LOCK bit to be set */
  340. + if (--wait_cnt == 0) {
  341. + dev_err(mmc_dev(mmc), "%s: DLL failed to LOCK\n",
  342. + mmc_hostname(mmc));
  343. + spin_unlock_irqrestore(&host->lock, flags);
  344. + return -ETIMEDOUT;
  345. + }
  346. + udelay(1);
  347. + }
  348. +
  349. + spin_unlock_irqrestore(&host->lock, flags);
  350. return 0;
  351. }
  352. +static int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
  353. +{
  354. + int tuning_seq_cnt = 3;
  355. + u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
  356. + const u32 *tuning_block_pattern = tuning_block_64;
  357. + int size = sizeof(tuning_block_64); /* Pattern size in bytes */
  358. + int rc;
  359. + struct mmc_host *mmc = host->mmc;
  360. + struct mmc_ios ios = host->mmc->ios;
  361. +
  362. + /*
  363. + * Tuning is required for SDR104, HS200 and HS400 cards and
  364. + * if clock frequency is greater than 100MHz in these modes.
  365. + */
  366. + if (host->clock <= 100 * 1000 * 1000 ||
  367. + !((ios.timing == MMC_TIMING_MMC_HS200) ||
  368. + (ios.timing == MMC_TIMING_UHS_SDR104)))
  369. + return 0;
  370. +
  371. + if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
  372. + (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
  373. + tuning_block_pattern = tuning_block_128;
  374. + size = sizeof(tuning_block_128);
  375. + }
  376. +
  377. + data_buf = kmalloc(size, GFP_KERNEL);
  378. + if (!data_buf)
  379. + return -ENOMEM;
  380. +
  381. +retry:
  382. + /* First of all reset the tuning block */
  383. + rc = msm_init_cm_dll(host);
  384. + if (rc)
  385. + goto out;
  386. +
  387. + phase = 0;
  388. + do {
  389. + struct mmc_command cmd = { 0 };
  390. + struct mmc_data data = { 0 };
  391. + struct mmc_request mrq = {
  392. + .cmd = &cmd,
  393. + .data = &data
  394. + };
  395. + struct scatterlist sg;
  396. +
  397. + /* Set the phase in delay line hw block */
  398. + rc = msm_config_cm_dll_phase(host, phase);
  399. + if (rc)
  400. + goto out;
  401. +
  402. + cmd.opcode = opcode;
  403. + cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  404. +
  405. + data.blksz = size;
  406. + data.blocks = 1;
  407. + data.flags = MMC_DATA_READ;
  408. + data.timeout_ns = NSEC_PER_SEC; /* 1 second */
  409. +
  410. + data.sg = &sg;
  411. + data.sg_len = 1;
  412. + sg_init_one(&sg, data_buf, size);
  413. + memset(data_buf, 0, size);
  414. + mmc_wait_for_req(mmc, &mrq);
  415. +
  416. + if (!cmd.error && !data.error &&
  417. + !memcmp(data_buf, tuning_block_pattern, size)) {
  418. + /* Tuning is successful at this tuning point */
  419. + tuned_phases[tuned_phase_cnt++] = phase;
  420. + dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
  421. + mmc_hostname(mmc), phase);
  422. + }
  423. + } while (++phase < ARRAY_SIZE(tuned_phases));
  424. +
  425. + if (tuned_phase_cnt) {
  426. + rc = msm_find_most_appropriate_phase(host, tuned_phases,
  427. + tuned_phase_cnt);
  428. + if (rc < 0)
  429. + goto out;
  430. + else
  431. + phase = rc;
  432. +
  433. + /*
  434. + * Finally set the selected phase in delay
  435. + * line hw block.
  436. + */
  437. + rc = msm_config_cm_dll_phase(host, phase);
  438. + if (rc)
  439. + goto out;
  440. + dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
  441. + mmc_hostname(mmc), phase);
  442. + } else {
  443. + if (--tuning_seq_cnt)
  444. + goto retry;
  445. + /* Tuning failed */
  446. + dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
  447. + mmc_hostname(mmc));
  448. + rc = -EIO;
  449. + }
  450. +
  451. +out:
  452. + kfree(data_buf);
  453. + return rc;
  454. +}
  455. +
  456. static const struct of_device_id sdhci_msm_dt_match[] = {
  457. { .compatible = "qcom,sdhci-msm-v4" },
  458. {},