blob: 4bd5357f0773262367495a466fe83c37a3687b1a [file] [log] [blame]
Channagoud Kadabi24146af2014-01-24 17:22:08 -08001/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Channagoud Kadabiafb8e172013-05-23 13:55:47 -07002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <platform/iomap.h>
30#include <platform/irqs.h>
31#include <platform/interrupts.h>
32#include <platform/timer.h>
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -070033#include <sys/types.h>
Channagoud Kadabiafb8e172013-05-23 13:55:47 -070034#include <target.h>
35#include <string.h>
36#include <stdlib.h>
37#include <bits.h>
38#include <debug.h>
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -070039#include <mmc.h>
Channagoud Kadabiafb8e172013-05-23 13:55:47 -070040#include <sdhci.h>
41#include <sdhci_msm.h>
42
Channagoud Kadabie106d1f2014-04-25 18:26:26 -070043
44#define MX_DRV_SUPPORTED_HS200 3
45
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -070046/* Known data stored in the card & read during tuning
47 * process. 64 bytes for 4bit bus width & 128 bytes
48 * of data for 8 bit bus width.
49 * These values are derived from HPG
50 */
51static const uint32_t tuning_block_64[] = {
52 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
53 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
54 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
55 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
56};
57
58static const uint32_t tuning_block_128[] = {
59 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
60 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
61 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
62 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
63 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
64 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
65 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
66 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
67};
Channagoud Kadabiafb8e172013-05-23 13:55:47 -070068
69/*
70 * Function: sdhci int handler
71 * Arg : MSM specific data for sdhci
72 * Return : 0
73 * Flow: : 1. Read the power control mask register
74 * 2. Check if bus is ON
75 * 3. Write success to ack regiser
76 * Details : This is power control interrupt handler.
77 * Once we receive the interrupt, we will ack the power control
78 * register that we have successfully completed pmic transactions
79 */
80static enum handler_return sdhci_int_handler(struct sdhci_msm_data *data)
81{
82 uint32_t ack;
83 uint32_t status;
84
85 /*
86 * Read the mask register to check if BUS & IO level
87 * interrupts are enabled
88 */
89 status = readl(data->pwrctl_base + SDCC_HC_PWRCTL_MASK_REG);
90
91 if (status & (SDCC_HC_BUS_ON | SDCC_HC_BUS_OFF))
92 ack = SDCC_HC_BUS_ON_OFF_SUCC;
93 if (status & (SDCC_HC_IO_SIG_LOW | SDCC_HC_IO_SIG_HIGH))
94 ack |= SDCC_HC_IO_SIG_SUCC;
95
96 /* Write success to power control register */
97 writel(ack, (data->pwrctl_base + SDCC_HC_PWRCTL_CTL_REG));
98
99 event_signal(data->sdhc_event, false);
100
101 return 0;
102}
103
104/*
105 * Function: sdhci clear pending interrupts
106 * Arg : MSM specific data for sdhci
107 * Return : None
108 * Flow: : Clear pending interrupts
109 */
110static void sdhci_clear_power_ctrl_irq(struct sdhci_msm_data *data)
111{
112 uint32_t irq_ctl;
113 uint32_t irq_stat;
114
115 /*
116 * Read the power control status register to know
117 * the status of BUS & IO_HIGH_V
118 */
119 irq_stat = readl(data->pwrctl_base + SDCC_HC_PWRCTL_STATUS_REG);
120
121 /* Clear the power control status */
122 writel(irq_stat, (data->pwrctl_base + SDCC_HC_PWRCTL_CLEAR_REG));
123
124 /*
125 * Handle the pending irq by ack'ing the bus & IO switch
126 */
127 irq_ctl = readl(data->pwrctl_base + SDCC_HC_PWRCTL_CTL_REG);
128
129 if (irq_stat & (SDCC_HC_BUS_ON | SDCC_HC_BUS_OFF))
130 irq_ctl |= SDCC_HC_BUS_ON_OFF_SUCC;
131 if (irq_stat & (SDCC_HC_IO_SIG_LOW | SDCC_HC_IO_SIG_HIGH))
132 irq_ctl |= SDCC_HC_IO_SIG_SUCC;
133
134 writel(irq_ctl, (data->pwrctl_base + SDCC_HC_PWRCTL_CTL_REG));
135}
136
137/*
138 * Function: sdhci msm init
139 * Arg : MSM specific config data for sdhci
140 * Return : None
141 * Flow: : Enable sdhci mode & do msm specific init
142 */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700143void sdhci_msm_init(struct sdhci_host *host, struct sdhci_msm_data *config)
Channagoud Kadabiafb8e172013-05-23 13:55:47 -0700144{
Channagoud Kadabid10f6182013-12-30 11:51:53 -0800145 /* Disable HC mode */
146 RMWREG32((config->pwrctl_base + SDCC_MCI_HC_MODE), SDHCI_HC_START_BIT, SDHCI_HC_WIDTH, 0);
147
148 /* Core power reset */
149 RMWREG32((config->pwrctl_base + SDCC_MCI_POWER), CORE_SW_RST_START, CORE_SW_RST_WIDTH, 1);
150
151 /* Wait for the core power reset to complete*/
152 mdelay(1);
153
Channagoud Kadabiafb8e172013-05-23 13:55:47 -0700154 /* Enable sdhc mode */
Channagoud Kadabi946848d2013-10-02 12:09:37 -0700155 RMWREG32((config->pwrctl_base + SDCC_MCI_HC_MODE), SDHCI_HC_START_BIT, SDHCI_HC_WIDTH, SDHCI_HC_MODE_EN);
Channagoud Kadabiafb8e172013-05-23 13:55:47 -0700156
Channagoud Kadabie632e252014-03-31 15:26:00 -0700157 /* Set the FF_CLK_SW_RST_DIS to 1 */
158 RMWREG32((config->pwrctl_base + SDCC_MCI_HC_MODE), FF_CLK_SW_RST_DIS_START, FF_CLK_SW_RST_DIS_WIDTH, 1);
159
Channagoud Kadabiafb8e172013-05-23 13:55:47 -0700160 /*
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700161 * Reset the controller
162 */
163 sdhci_reset(host, SDHCI_SOFT_RESET);
164
165 /*
Channagoud Kadabiafb8e172013-05-23 13:55:47 -0700166 * CORE_SW_RST may trigger power irq if previous status of PWRCTL
167 * was either BUS_ON or IO_HIGH. So before we enable the power irq
168 * interrupt in GIC (by registering the interrupt handler), we need to
169 * ensure that any pending power irq interrupt status is acknowledged
170 * otherwise power irq interrupt handler would be fired prematurely.
171 */
172 sdhci_clear_power_ctrl_irq(config);
173
174 /*
175 * Register the interrupt handler for pwr irq
176 */
177 register_int_handler(config->pwr_irq, sdhci_int_handler, (void *)config);
178
179 unmask_interrupt(config->pwr_irq);
180
181 /* Enable pwr control interrupt */
182 writel(SDCC_HC_PWR_CTRL_INT, (config->pwrctl_base + SDCC_HC_PWRCTL_MASK_REG));
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700183
184 config->tuning_done = false;
185 config->calibration_done = false;
186 host->tuning_in_progress = false;
187}
188
189/*
190 * Function: sdhci msm set mci clk
191 * Arg : Host structure
192 * Return : None
193 * Flow: : Set HC_SELECT & HC_SELECT_EN for hs400
194 */
195void sdhci_msm_set_mci_clk(struct sdhci_host *host)
196{
197 struct sdhci_msm_data *msm_host;
198
199 msm_host = host->msm_host;
200
201 if (host->timing == MMC_HS400_TIMING)
202 {
203 /*
204 * If the current tuning mode is HS400 then we should set the MCLK to run
205 * the clock @ MCLK/2. Also set HS400 mode in SELECT_IN of vendor specific
206 * register
207 */
208 REG_RMW32(host, SDCC_VENDOR_SPECIFIC_FUNC, SDCC_HC_MCLK_HS400_START, SDCC_HC_MCLK_HS400_WIDTH, SDCC_HC_MCLK_SEL_HS400);
209
210 /* Enable HS400 mode from HC_SELECT_IN bit of VENDOR_SPEC register
211 * As the SDCC spec does not have matching mode for HS400
212 */
213 if (msm_host->tuning_done && !msm_host->calibration_done)
214 {
215 REG_RMW32(host, SDCC_VENDOR_SPECIFIC_FUNC, SDCC_HC_MCLK_SEL_IN_START, SDCC_HC_MCLK_SEL_IN_WIDTH, SDCC_HC_MCLK_SEL_IN_HS400);
216 REG_RMW32(host, SDCC_VENDOR_SPECIFIC_FUNC, SDCC_HC_MCLK_SEL_IN_EN_START, SDCC_HC_MCLK_SEL_IN_EN_WIDTH, SDCC_HC_MCLK_SEL_IN_EN);
217 }
218 }
219 else
220 {
221 /*
222 * Set 0x0 mode in SELECT_IN of vendor specific register so that the
223 * host control2 register settings from sdhc spec are used for
224 * speed mode
225 */
226 REG_RMW32(host, SDCC_VENDOR_SPECIFIC_FUNC, SDCC_HC_MCLK_SEL_IN_START, SDCC_HC_MCLK_SEL_IN_WIDTH, 0x0);
227 REG_RMW32(host, SDCC_VENDOR_SPECIFIC_FUNC, SDCC_HC_MCLK_SEL_IN_EN_START, SDCC_HC_MCLK_SEL_IN_EN_WIDTH, 0x0);
228 }
229}
230
231/*
232 * Set the value based on sdcc clock frequency
233 */
234static void msm_set_dll_freq(struct sdhci_host *host)
235{
236 uint32_t reg_val = 0;
237
238 /* Set clock freq value based on clock range */
239 if (host->cur_clk_rate <= 112000000)
240 reg_val = 0x0;
241 else if (host->cur_clk_rate <= 125000000)
242 reg_val = 0x1;
243 else if (host->cur_clk_rate <= 137000000)
244 reg_val = 0x2;
245 else if (host->cur_clk_rate <= 150000000)
246 reg_val = 0x3;
247 else if (host->cur_clk_rate <= 162000000)
248 reg_val = 0x4;
249 else if (host->cur_clk_rate <= 175000000)
250 reg_val = 0x5;
251 else if (host->cur_clk_rate <= 187000000)
252 reg_val = 0x6;
253 else if (host->cur_clk_rate <= 200000000)
254 reg_val = 0x7;
255
Channagoud Kadabie632e252014-03-31 15:26:00 -0700256 DBG("\n %s: DLL freq: 0x%08x\n", __func__, reg_val);
257
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700258 REG_RMW32(host, SDCC_DLL_CONFIG_REG, SDCC_DLL_CONFIG_MCLK_START, SDCC_DLL_CONFIG_MCLK_WIDTH, reg_val);
259}
260
261/* Initialize DLL (Programmable Delay Line) */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700262static uint32_t sdhci_msm_init_dll(struct sdhci_host *host)
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700263{
264 uint32_t pwr_save = 0;
Channagoud Kadabie632e252014-03-31 15:26:00 -0700265 uint32_t timeout = SDHCI_DLL_TIMEOUT;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700266
267 pwr_save = REG_READ32(host, SDCC_VENDOR_SPECIFIC_FUNC) & SDCC_DLL_PWR_SAVE_EN;
268
269 /* PWR SAVE to 0 */
270 if (pwr_save)
271 REG_WRITE32(host, (REG_READ32(host, SDCC_VENDOR_SPECIFIC_FUNC) & ~SDCC_DLL_PWR_SAVE_EN), SDCC_VENDOR_SPECIFIC_FUNC);
272 /* Set DLL_RST to 1 */
273 REG_WRITE32(host, (REG_READ32(host, SDCC_DLL_CONFIG_REG) | SDCC_DLL_RESET_EN), SDCC_DLL_CONFIG_REG);
274 /* Set DLL_PDN to 1 */
275 REG_WRITE32(host, (REG_READ32(host, SDCC_DLL_CONFIG_REG) | SDCC_DLL_PDN_EN), SDCC_DLL_CONFIG_REG);
276
277 /* Set frequency field in DLL_CONFIG */
278 msm_set_dll_freq(host);
279
280 /* Write 0 to DLL_RST */
281 REG_WRITE32(host, (REG_READ32(host, SDCC_DLL_CONFIG_REG) & ~SDCC_DLL_RESET_EN), SDCC_DLL_CONFIG_REG);
282 /* Write 0 to DLL_PDN */
283 REG_WRITE32(host, (REG_READ32(host, SDCC_DLL_CONFIG_REG) & ~SDCC_DLL_PDN_EN), SDCC_DLL_CONFIG_REG);
284 /* Write 1 to DLL_EN */
285 REG_WRITE32(host, (REG_READ32(host, SDCC_DLL_CONFIG_REG) | SDCC_DLL_EN), SDCC_DLL_CONFIG_REG);
286 /* Write 1 to CLK_OUT_EN */
287 REG_WRITE32(host, (REG_READ32(host, SDCC_DLL_CONFIG_REG) | SDCC_DLL_CLK_OUT_EN), SDCC_DLL_CONFIG_REG);
Channagoud Kadabie632e252014-03-31 15:26:00 -0700288 /* Wait for DLL_LOCK in DLL_STATUS register, wait time 50us */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700289 while(!((REG_READ32(host, SDCC_REG_DLL_STATUS)) & SDCC_DLL_LOCK_STAT));
Channagoud Kadabie632e252014-03-31 15:26:00 -0700290 {
291 udelay(1);
292 timeout--;
293 if (!timeout)
294 {
295 dprintf(CRITICAL, "%s: Failed to get DLL lock: 0x%08x\n", __func__, REG_READ32(host, SDCC_REG_DLL_STATUS));
296 return 1;
297 }
298 }
299
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700300 /* Set the powersave back on */
301 if (pwr_save)
Channagoud Kadabi7bcf6252014-05-14 18:28:13 -0700302 REG_WRITE32(host, (REG_READ32(host, SDCC_VENDOR_SPECIFIC_FUNC) | SDCC_DLL_PWR_SAVE_EN), SDCC_VENDOR_SPECIFIC_FUNC);
Channagoud Kadabie632e252014-03-31 15:26:00 -0700303
304 return 0;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700305}
306
307/* Configure DLL with delay value based on 'phase' */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700308static uint32_t sdhci_msm_config_dll(struct sdhci_host *host, uint32_t phase)
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700309{
310 uint32_t core_cfg = 0;
Channagoud Kadabie632e252014-03-31 15:26:00 -0700311 uint32_t timeout = SDHCI_DLL_TIMEOUT;
312
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700313 /* Gray code values from SWI */
314 uint32_t gray_code [] = { 0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9, 0x8 };
315
316 /* set CDR_EN & CLK_OUT_EN to 0 and
317 * CDR_EXT_EN & DLL_EN to 1*/
318 core_cfg = REG_READ32(host, SDCC_DLL_CONFIG_REG);
319 core_cfg &= ~(SDCC_DLL_CDR_EN | SDCC_DLL_CLK_OUT_EN);
320 core_cfg |= (SDCC_DLL_CDR_EXT_EN | SDCC_DLL_EN);
321 REG_WRITE32(host, core_cfg, SDCC_DLL_CONFIG_REG);
322
323 /* Wait until CLK_OUT_EN is 0 */
324 while(REG_READ32(host, SDCC_DLL_CONFIG_REG) & SDCC_DLL_CLK_OUT_EN);
325
326 REG_RMW32(host, SDCC_DLL_CONFIG_REG, SDCC_DLL_GRAY_CODE_START, SDCC_DLL_GRAY_CODE_WIDTH, gray_code[phase]);
327
328 REG_WRITE32(host, (REG_READ32(host, SDCC_DLL_CONFIG_REG) | SDCC_DLL_CLK_OUT_EN), SDCC_DLL_CONFIG_REG);
329
Channagoud Kadabie632e252014-03-31 15:26:00 -0700330 /* Wait until CLK_OUT_EN is 1, wait time 50us */
331 while(!(REG_READ32(host, SDCC_DLL_CONFIG_REG) & SDCC_DLL_CLK_OUT_EN))
332 {
333 timeout--;
334 udelay(1);
335 if (!timeout)
336 {
337 dprintf(CRITICAL, "%s: clk_out_en timed out: %08x\n", __func__, REG_READ32(host, SDCC_DLL_CONFIG_REG));
338 return 1;
339 }
340 }
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700341
342 core_cfg = REG_READ32(host, SDCC_DLL_CONFIG_REG);
343
344 core_cfg |= SDCC_DLL_CDR_EN;
345 core_cfg &= ~SDCC_DLL_CDR_EXT_EN;
346
347 REG_WRITE32(host, core_cfg, SDCC_DLL_CONFIG_REG);
348
Channagoud Kadabie632e252014-03-31 15:26:00 -0700349 return 0;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700350}
351
352/*
353 * Find the right tuning delay, this function finds the largest
354 * consecutive sequence of phases & then selects the 3/4 th of
355 * the range which has max entries
356 * For eg: If we get the following sequence in phase_table[]
357 * (A) phase_table[] = 0x1, 0x2, 0x3, 0x4 , 0x5
358 * (B) phase_table[] = 0xA, 0xB, 0xC
359 * In the above case the (A) has maximum consecutive entries with '5' entries
360 * So delay would be phase_table[(0x5 * 3) / 4] = 0x3
361 */
362static int sdhci_msm_find_appropriate_phase(struct sdhci_host *host,
363 uint32_t *phase_table,
364 uint32_t total_phases)
365{
366 int sub_phases[MAX_PHASES][MAX_PHASES]={{0}};
367 int phases_per_row[MAX_PHASES] = {0};
368 uint32_t i,j;
369 int selected_phase = 0;
370 uint32_t row_index = 0;
371 uint32_t col_index = 0;
372 uint32_t phase_15_row_idx = 0;
373 uint32_t phases_0_row_idx = 0;
374 uint32_t max_phases_3_4_idx = 0;
375 uint32_t max_phases = 0;
376 uint32_t max_phases_row = 0;
377 bool found_loop = false;
378
379 if (!phase_table[0] && phase_table[total_phases - 1] == (MAX_PHASES - 1))
380 found_loop = true;
381
382 for (i = 0; i < total_phases; i++)
383 {
384 /* Break the phase table entries into different sub arrays based
385 * on the consecutive entries. Each row will have one sub array
386 * of consecutive entries.
387 * for eg: phase_table [] = { 0x0, 0x1, 0x2, 0xA, 0xB}
388 * sub_phases [0][] = { 0x0, 0x1, 0x2}
389 * sub_phases [1][] = { 0xA, 0xB}
390 */
391 sub_phases[row_index][col_index] = phase_table[i];
392 phases_per_row[row_index]++;
393 col_index++;
394
395 /* If we are at the last phase no need to check further */
396 if ((i + 1) == total_phases)
397 break;
398
399 /* If phase_table does not have consecutive entries, move to next entry */
400 if (phase_table[i]+1 != phase_table[i+1])
401 {
402 row_index++;
403 col_index = 0;
404 }
405 }
406
407 if (found_loop && total_phases < MAX_PHASES)
408 {
409 /* For consecutive entries we need to consider loops.
410 * If the phase_table contains 0x0 & 0xF then we have
411 * a loop, the number after 0xF in the sequence would be
412 * 0x0.
413 * for eg:
414 * phase_table = { 0x0, 0x1, 0x2, 0xD, 0xE, 0xF }
415 * then
416 * sub_phase [0][] = { 0x0, 0x1, 0x2 }
417 * sub_phase [1][] = { 0xD, 0xE, 0xF }
418 * Since we have a loop here, we need to merge the sub arrays as:
419 * sub_phase [1][] = { 0xD, 0xE, 0xF, 0x0, 0x1, 0x2 }
420 */
421
422 /* The entry 0xF will always be in the last row
423 * and entry 0x0 will always be in the first row
424 */
425 phase_15_row_idx = row_index;
426 j = 0;
427 for (i = phases_per_row[phase_15_row_idx] ; i < MAX_PHASES ; i++)
428 {
429 sub_phases[phase_15_row_idx][i] = sub_phases[phases_0_row_idx][j];
430 if (++j >= phases_per_row[phases_0_row_idx])
431 break;
432 }
433
434 /* Update the number of entries for the sub_phase after the merger */
435 phases_per_row[phase_15_row_idx] = phases_per_row[phase_15_row_idx] + phases_per_row[phases_0_row_idx];
436 phases_per_row[phases_0_row_idx] = 0;
437 }
438
439 for (i = 0 ; i <= row_index; i++)
440 {
441 if (phases_per_row[i] > max_phases)
442 {
443 max_phases = phases_per_row[i];
444 max_phases_row = i;
445 }
446 }
447
448 max_phases_3_4_idx = (max_phases * 3) / 4;
449 if (max_phases_3_4_idx)
450 max_phases_3_4_idx--;
451
452 selected_phase = sub_phases[max_phases_row][max_phases_3_4_idx];
453
454 return selected_phase;
455}
456
457static uint32_t sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
458{
459 uint32_t timeout;
460 uint32_t cdc_err;
461
Channagoud Kadabie632e252014-03-31 15:26:00 -0700462 DBG("\n CDCLP533 Calibration Start\n");
463
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700464 /* Reset & Initialize the DLL block */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700465 if (sdhci_msm_init_dll(host))
466 return 1;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700467
468 /* Write the save phase */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700469 if (sdhci_msm_config_dll(host, host->msm_host->saved_phase))
470 return 1;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700471
472 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
473 REG_WRITE32(host, (REG_READ32(host, SDCC_DLL_CONFIG_REG) | CMD_DAT_TRACK_SEL), SDCC_DLL_CONFIG_REG);
474
475 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
476 REG_WRITE32(host, (REG_READ32(host, SDCC_CDC_DDR200_CFG) & ~CDC_T4_DLY_SEL), SDCC_CDC_DDR200_CFG);
477
478 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
479 REG_WRITE32(host, (REG_READ32(host, SDCC_CDC_DDR200_CFG) & ~START_CDC_TRAFFIC), SDCC_CDC_DDR200_CFG);
480
481 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CSR_CDC_GEN_CFG */
482 REG_WRITE32(host, (REG_READ32(host, SDCC_VENDOR_SPEC_CSR_CDC_CFG) & ~CDC_SWITCH_BYPASS_OFF), SDCC_VENDOR_SPEC_CSR_CDC_CFG);
483
484 /* Write 1 to CDC_SWITCH_RC_EN field in CSR_CDC_GEN_CFG */
485 REG_WRITE32(host, (REG_READ32(host, SDCC_VENDOR_SPEC_CSR_CDC_CFG) | CDC_SWITCH_RC_EN), SDCC_VENDOR_SPEC_CSR_CDC_CFG);
486
487 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
488 REG_WRITE32(host, (REG_READ32(host, SDCC_CDC_DDR200_CFG) & ~START_CDC_TRAFFIC), SDCC_CDC_DDR200_CFG);
489
490 /* Perform CDCLP533 initialization sequence
491 * SDCC_CSR_CDC_CTRL_CFG0 --> 0x11800EC
492 * SDCC_CSR_CDC_CTRL_CFG1 --> 0x3011111
493 * SDCC_CSR_CDC_CAL_TIMER_CFG0 --> 0x1201000
494 * SDCC_CSR_CDC_CAL_TIMER_CFG1 --> 0x4
495 * SDCC_CSR_CDC_REFCOUNT_CFG --> 0xCB732020
496 * SDCC_CSR_CDC_COARSE_CAL_CFG --> 0xB19
Channagoud Kadabi86afb382014-06-23 11:15:46 -0700497 * SDCC_CSR_CDC_DELAY_CFG --> 0x4E2
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700498 * SDCC_CDC_OFFSET_CFG --> 0x0
499 * SDCC_CDC_SLAVE_DDA_CFG --> 0x16334
500 */
501
502 REG_WRITE32(host, 0x11800EC, SDCC_CSR_CDC_CTRL_CFG0);
503 REG_WRITE32(host, 0x3011111, SDCC_CSR_CDC_CTRL_CFG1);
504 REG_WRITE32(host, 0x1201000, SDCC_CSR_CDC_CAL_TIMER_CFG0);
505 REG_WRITE32(host, 0x4, SDCC_CSR_CDC_CAL_TIMER_CFG1);
506 REG_WRITE32(host, 0xCB732020, SDCC_CSR_CDC_REFCOUNT_CFG);
507 REG_WRITE32(host, 0xB19, SDCC_CSR_CDC_COARSE_CAL_CFG);
Channagoud Kadabi86afb382014-06-23 11:15:46 -0700508 REG_WRITE32(host, 0x4E2, SDCC_CSR_CDC_DELAY_CFG);
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700509 REG_WRITE32(host, 0x0, SDCC_CDC_OFFSET_CFG);
510 REG_WRITE32(host, 0x16334, SDCC_CDC_SLAVE_DDA_CFG);
511
512 /* Write 1 to SW_TRIGGER_FULL_CALIB */
513 REG_WRITE32(host, (REG_READ32(host, SDCC_CSR_CDC_CTRL_CFG0) | CDC_SW_TRIGGER_FULL_CALIB), SDCC_CSR_CDC_CTRL_CFG0);
514
515 /* Write 0 to SW_TRIGGER_FULL_CALIB */
516 REG_WRITE32(host, (REG_READ32(host, SDCC_CSR_CDC_CTRL_CFG0) & ~CDC_SW_TRIGGER_FULL_CALIB), SDCC_CSR_CDC_CTRL_CFG0);
517
518 /* Write 1 to HW_AUTO_CAL_EN */
519 REG_WRITE32(host, (REG_READ32(host, SDCC_CSR_CDC_CTRL_CFG0) | CDC_HW_AUTO_CAL_EN), SDCC_CSR_CDC_CTRL_CFG0);
520
521 /* Write 1 to TIMER_ENA */
522 REG_WRITE32(host, (REG_READ32(host, SDCC_CSR_CDC_CAL_TIMER_CFG0) | CDC_TIMER_EN), SDCC_CSR_CDC_CAL_TIMER_CFG0);
523
524 /* Wait for CALIBRATION_DONE in CDC_STATUS */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700525 timeout = CDC_STATUS_TIMEOUT;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700526 while (!(REG_READ32(host, SDCC_CSR_CDC_STATUS0) & BIT(0)))
527 {
528 timeout--;
529 mdelay(1);
530 if (!timeout)
531 {
532 dprintf(CRITICAL, "Error: Calibration done in CDC status not set\n");
533 return 1;
534 }
535 }
536
537 cdc_err = REG_READ32(host, SDCC_CSR_CDC_STATUS0) & CSR_CDC_ERROR_MASK;
538 if (cdc_err)
539 {
540 dprintf(CRITICAL, "CDC error set during calibration: %x\n", cdc_err);
541 return 1;
542 }
543 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
544 REG_WRITE32(host, (REG_READ32(host, SDCC_CDC_DDR200_CFG) | START_CDC_TRAFFIC), SDCC_CDC_DDR200_CFG);
545
Channagoud Kadabie632e252014-03-31 15:26:00 -0700546 DBG("\n CDCLP533 Calibration Done\n");
547
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700548 return 0;
549}
550
551/*
552 * Function: sdhci msm execute tuning
553 * Arg : Host structure & bus width
554 * Return : 0 on Success, 1 on Failure
555 * Flow: : Execute Tuning sequence for HS200
556 */
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700557uint32_t sdhci_msm_execute_tuning(struct sdhci_host *host, struct mmc_card *card, uint32_t bus_width)
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700558{
559 uint32_t *tuning_block;
560 uint32_t *tuning_data;
561 uint32_t tuned_phases[MAX_PHASES] = {{0}};
562 uint32_t size;
563 uint32_t phase = 0;
564 uint32_t tuned_phase_cnt = 0;
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700565 uint8_t drv_type = 0;
566 bool drv_type_changed = false;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700567 int ret = 0;
Channagoud Kadabie632e252014-03-31 15:26:00 -0700568 int i;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700569 struct sdhci_msm_data *msm_host;
570
571 msm_host = host->msm_host;
572
573 /* In Tuning mode */
574 host->tuning_in_progress = true;
575
576 /* Calibration for CDCLP533 needed for HS400 mode */
577 if (msm_host->tuning_done && !msm_host->calibration_done && host->timing == MMC_HS400_TIMING)
578 {
579 ret = sdhci_msm_cdclp533_calibration(host);
580 if (!ret)
581 msm_host->calibration_done = true;
582 goto out;
583 }
584
585 if (bus_width == DATA_BUS_WIDTH_8BIT)
586 {
587 tuning_block = tuning_block_128;
588 size = sizeof(tuning_block_128);
589 }
590 else
591 {
592 tuning_block = tuning_block_64;
593 size = sizeof(tuning_block_64);
594 }
595
596 tuning_data = (uint32_t *) memalign(CACHE_LINE, ROUNDUP(size, CACHE_LINE));
597
598 ASSERT(tuning_data);
599
600 /* Reset & Initialize the DLL block */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700601 if (sdhci_msm_init_dll(host))
602 {
603 ret = 1;
604 goto free;
605 }
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700606
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700607retry_tuning:
608 tuned_phase_cnt = 0;
609 phase = 0;
610
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700611 while (phase < MAX_PHASES)
612 {
613 struct mmc_command cmd = {0};
614
615 /* configure dll to set phase delay */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700616 if (sdhci_msm_config_dll(host, phase))
617 {
618 ret = 1;
619 goto free;
620 }
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700621
622 cmd.cmd_index = CMD21_SEND_TUNING_BLOCK;
623 cmd.argument = 0x0;
624 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
625 cmd.resp_type = SDHCI_CMD_RESP_R1;
626 cmd.trans_mode = SDHCI_MMC_READ;
627 cmd.data_present = 0x1;
628 cmd.data.data_ptr = tuning_data;
629 cmd.data.blk_sz = size;
630 cmd.data.num_blocks = 0x1;
631
632 /* send command */
633 if (!sdhci_send_command(host, &cmd) && !memcmp(tuning_data, tuning_block, size))
634 tuned_phases[tuned_phase_cnt++] = phase;
635
636 phase++;
637 }
638
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700639 /*
640 * Check if all the tuning phases passed */
641 if (tuned_phase_cnt == MAX_PHASES)
642 {
643 /* Change the driver type & rerun tuning */
644 while(++drv_type < MX_DRV_SUPPORTED_HS200)
645 {
646 drv_type_changed = mmc_set_drv_type(host, card, drv_type);
647 if (drv_type_changed)
648 {
649 goto retry_tuning;
650 }
651 }
652 }
653
654 /* Restore the driver strength to default value */
655 if (drv_type_changed)
656 mmc_set_drv_type(host, card, 0);
657
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700658 /* Find the appropriate tuned phase */
659 if (tuned_phase_cnt)
660 {
Channagoud Kadabie632e252014-03-31 15:26:00 -0700661 DBG("\n Tuned phase\n");
662 for (i = 0 ; i < tuned_phase_cnt ; i++)
663 {
664 DBG("%d\t", tuned_phases[i]);
665 }
666
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700667 ret = sdhci_msm_find_appropriate_phase(host, tuned_phases, tuned_phase_cnt);
668
669 if (ret < 0)
670 {
671 dprintf(CRITICAL, "Failed in selecting the tuning phase\n");
672 ret = 1;
673 goto free;
674 }
675
676 phase = (uint32_t) ret;
677 ret = 0;
678
Channagoud Kadabie632e252014-03-31 15:26:00 -0700679 DBG("\n: %s: Tuned Phase: 0x%08x\n", __func__, phase);
680
681 if (sdhci_msm_config_dll(host, phase))
682 goto free;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700683
684 /* Save the tuned phase */
685 host->msm_host->saved_phase = phase;
686 }
687 else
688 {
689 dprintf(CRITICAL, "Failed to get tuned phase\n");
690 ret = 1;
691 }
692
693free:
694 free(tuning_data);
695out:
696 /* Tuning done */
697 host->tuning_in_progress = false;
698 host->msm_host->tuning_done = true;
699 return ret;
Channagoud Kadabiafb8e172013-05-23 13:55:47 -0700700}
Channagoud Kadabi24146af2014-01-24 17:22:08 -0800701
702/*
703 * API to disable HC mode
704 */
705void sdhci_mode_disable(struct sdhci_host *host)
706{
707 /* Disable HC mode */
708 RMWREG32((host->msm_host->pwrctl_base + SDCC_MCI_HC_MODE), SDHCI_HC_START_BIT, SDHCI_HC_WIDTH, 0);
709}
710