blob: 7d6443cd3e59cef06e5d40ab0191674a4d72015c [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
Channagoud Kadabidd8a7342014-07-09 10:35:01 -0700307void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
308{
309 uint32_t core_cfg;
310
311 core_cfg = REG_READ32(host, SDCC_DLL_CONFIG_REG);
312
313 if (enable)
314 {
315 core_cfg |= SDCC_DLL_CDR_EN;
316 }
317 else
318 {
319 core_cfg &= ~SDCC_DLL_CDR_EN;
320 }
321
322 REG_WRITE32(host, core_cfg, SDCC_DLL_CONFIG_REG);
323}
324
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700325/* Configure DLL with delay value based on 'phase' */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700326static uint32_t sdhci_msm_config_dll(struct sdhci_host *host, uint32_t phase)
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700327{
328 uint32_t core_cfg = 0;
Channagoud Kadabie632e252014-03-31 15:26:00 -0700329 uint32_t timeout = SDHCI_DLL_TIMEOUT;
330
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700331 /* Gray code values from SWI */
332 uint32_t gray_code [] = { 0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9, 0x8 };
333
334 /* set CDR_EN & CLK_OUT_EN to 0 and
335 * CDR_EXT_EN & DLL_EN to 1*/
336 core_cfg = REG_READ32(host, SDCC_DLL_CONFIG_REG);
337 core_cfg &= ~(SDCC_DLL_CDR_EN | SDCC_DLL_CLK_OUT_EN);
338 core_cfg |= (SDCC_DLL_CDR_EXT_EN | SDCC_DLL_EN);
339 REG_WRITE32(host, core_cfg, SDCC_DLL_CONFIG_REG);
340
341 /* Wait until CLK_OUT_EN is 0 */
342 while(REG_READ32(host, SDCC_DLL_CONFIG_REG) & SDCC_DLL_CLK_OUT_EN);
343
344 REG_RMW32(host, SDCC_DLL_CONFIG_REG, SDCC_DLL_GRAY_CODE_START, SDCC_DLL_GRAY_CODE_WIDTH, gray_code[phase]);
345
346 REG_WRITE32(host, (REG_READ32(host, SDCC_DLL_CONFIG_REG) | SDCC_DLL_CLK_OUT_EN), SDCC_DLL_CONFIG_REG);
347
Channagoud Kadabie632e252014-03-31 15:26:00 -0700348 /* Wait until CLK_OUT_EN is 1, wait time 50us */
349 while(!(REG_READ32(host, SDCC_DLL_CONFIG_REG) & SDCC_DLL_CLK_OUT_EN))
350 {
351 timeout--;
352 udelay(1);
353 if (!timeout)
354 {
355 dprintf(CRITICAL, "%s: clk_out_en timed out: %08x\n", __func__, REG_READ32(host, SDCC_DLL_CONFIG_REG));
356 return 1;
357 }
358 }
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700359
360 core_cfg = REG_READ32(host, SDCC_DLL_CONFIG_REG);
361
362 core_cfg |= SDCC_DLL_CDR_EN;
363 core_cfg &= ~SDCC_DLL_CDR_EXT_EN;
364
365 REG_WRITE32(host, core_cfg, SDCC_DLL_CONFIG_REG);
366
Channagoud Kadabie632e252014-03-31 15:26:00 -0700367 return 0;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700368}
369
370/*
371 * Find the right tuning delay, this function finds the largest
372 * consecutive sequence of phases & then selects the 3/4 th of
373 * the range which has max entries
374 * For eg: If we get the following sequence in phase_table[]
375 * (A) phase_table[] = 0x1, 0x2, 0x3, 0x4 , 0x5
376 * (B) phase_table[] = 0xA, 0xB, 0xC
377 * In the above case the (A) has maximum consecutive entries with '5' entries
378 * So delay would be phase_table[(0x5 * 3) / 4] = 0x3
379 */
380static int sdhci_msm_find_appropriate_phase(struct sdhci_host *host,
381 uint32_t *phase_table,
382 uint32_t total_phases)
383{
384 int sub_phases[MAX_PHASES][MAX_PHASES]={{0}};
385 int phases_per_row[MAX_PHASES] = {0};
386 uint32_t i,j;
387 int selected_phase = 0;
388 uint32_t row_index = 0;
389 uint32_t col_index = 0;
390 uint32_t phase_15_row_idx = 0;
391 uint32_t phases_0_row_idx = 0;
392 uint32_t max_phases_3_4_idx = 0;
393 uint32_t max_phases = 0;
394 uint32_t max_phases_row = 0;
395 bool found_loop = false;
396
397 if (!phase_table[0] && phase_table[total_phases - 1] == (MAX_PHASES - 1))
398 found_loop = true;
399
400 for (i = 0; i < total_phases; i++)
401 {
402 /* Break the phase table entries into different sub arrays based
403 * on the consecutive entries. Each row will have one sub array
404 * of consecutive entries.
405 * for eg: phase_table [] = { 0x0, 0x1, 0x2, 0xA, 0xB}
406 * sub_phases [0][] = { 0x0, 0x1, 0x2}
407 * sub_phases [1][] = { 0xA, 0xB}
408 */
409 sub_phases[row_index][col_index] = phase_table[i];
410 phases_per_row[row_index]++;
411 col_index++;
412
413 /* If we are at the last phase no need to check further */
414 if ((i + 1) == total_phases)
415 break;
416
417 /* If phase_table does not have consecutive entries, move to next entry */
418 if (phase_table[i]+1 != phase_table[i+1])
419 {
420 row_index++;
421 col_index = 0;
422 }
423 }
424
425 if (found_loop && total_phases < MAX_PHASES)
426 {
427 /* For consecutive entries we need to consider loops.
428 * If the phase_table contains 0x0 & 0xF then we have
429 * a loop, the number after 0xF in the sequence would be
430 * 0x0.
431 * for eg:
432 * phase_table = { 0x0, 0x1, 0x2, 0xD, 0xE, 0xF }
433 * then
434 * sub_phase [0][] = { 0x0, 0x1, 0x2 }
435 * sub_phase [1][] = { 0xD, 0xE, 0xF }
436 * Since we have a loop here, we need to merge the sub arrays as:
437 * sub_phase [1][] = { 0xD, 0xE, 0xF, 0x0, 0x1, 0x2 }
438 */
439
440 /* The entry 0xF will always be in the last row
441 * and entry 0x0 will always be in the first row
442 */
443 phase_15_row_idx = row_index;
444 j = 0;
445 for (i = phases_per_row[phase_15_row_idx] ; i < MAX_PHASES ; i++)
446 {
447 sub_phases[phase_15_row_idx][i] = sub_phases[phases_0_row_idx][j];
448 if (++j >= phases_per_row[phases_0_row_idx])
449 break;
450 }
451
452 /* Update the number of entries for the sub_phase after the merger */
453 phases_per_row[phase_15_row_idx] = phases_per_row[phase_15_row_idx] + phases_per_row[phases_0_row_idx];
454 phases_per_row[phases_0_row_idx] = 0;
455 }
456
457 for (i = 0 ; i <= row_index; i++)
458 {
459 if (phases_per_row[i] > max_phases)
460 {
461 max_phases = phases_per_row[i];
462 max_phases_row = i;
463 }
464 }
465
466 max_phases_3_4_idx = (max_phases * 3) / 4;
467 if (max_phases_3_4_idx)
468 max_phases_3_4_idx--;
469
470 selected_phase = sub_phases[max_phases_row][max_phases_3_4_idx];
471
472 return selected_phase;
473}
474
Channagoud Kadabi756e1e32014-06-05 13:00:55 -0700475static uint32_t sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
476{
477 uint32_t timeout = 0;
478
479 DBG("\n CM_DLL_SDC4 Calibration Start\n");
480
481 /*1.Write the default value to SDCC_HC_REG_DDR_CONFIG register*/
482 REG_WRITE32(host, DDR_CONFIG_VAL, SDCC_HC_REG_DDR_CONFIG);
483
484 /*2. Write DDR_CAL_EN to '1' */
485 REG_WRITE32(host, (REG_READ32(host, SDCC_HC_REG_DLL_CONFIG_2) | DDR_CAL_EN), SDCC_HC_REG_DLL_CONFIG_2);
486
487 /*3. Wait for DLL_LOCK for hs400 to be set */
488 timeout = DDR_CAL_TIMEOUT_MAX;
489 while (!(REG_READ32(host, SDCC_REG_DLL_STATUS) & DDR_DLL_LOCK_JDR))
490 {
491 timeout--;
492 mdelay(1);
493 if (!timeout)
494 {
495 dprintf(CRITICAL, "Error: DLL lock for hs400 operation is not set\n");
496 return 1;
497 }
498 }
499
500 /*4. Set powersave dll */
501 REG_WRITE32(host, (REG_READ32(host, SDCC_HC_VENDOR_SPECIFIC_FUNC3) | PWRSAVE_DLL), SDCC_HC_VENDOR_SPECIFIC_FUNC3);
502
503 DBG("\n CM_DLL_SDC4 Calibration Done\n");
504
505 return 0;
506}
507
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700508static uint32_t sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
509{
510 uint32_t timeout;
511 uint32_t cdc_err;
512
Channagoud Kadabie632e252014-03-31 15:26:00 -0700513 DBG("\n CDCLP533 Calibration Start\n");
514
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700515 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
516 REG_WRITE32(host, (REG_READ32(host, SDCC_CDC_DDR200_CFG) & ~CDC_T4_DLY_SEL), SDCC_CDC_DDR200_CFG);
517
518 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
519 REG_WRITE32(host, (REG_READ32(host, SDCC_CDC_DDR200_CFG) & ~START_CDC_TRAFFIC), SDCC_CDC_DDR200_CFG);
520
521 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CSR_CDC_GEN_CFG */
522 REG_WRITE32(host, (REG_READ32(host, SDCC_VENDOR_SPEC_CSR_CDC_CFG) & ~CDC_SWITCH_BYPASS_OFF), SDCC_VENDOR_SPEC_CSR_CDC_CFG);
523
524 /* Write 1 to CDC_SWITCH_RC_EN field in CSR_CDC_GEN_CFG */
525 REG_WRITE32(host, (REG_READ32(host, SDCC_VENDOR_SPEC_CSR_CDC_CFG) | CDC_SWITCH_RC_EN), SDCC_VENDOR_SPEC_CSR_CDC_CFG);
526
527 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
528 REG_WRITE32(host, (REG_READ32(host, SDCC_CDC_DDR200_CFG) & ~START_CDC_TRAFFIC), SDCC_CDC_DDR200_CFG);
529
530 /* Perform CDCLP533 initialization sequence
531 * SDCC_CSR_CDC_CTRL_CFG0 --> 0x11800EC
532 * SDCC_CSR_CDC_CTRL_CFG1 --> 0x3011111
533 * SDCC_CSR_CDC_CAL_TIMER_CFG0 --> 0x1201000
534 * SDCC_CSR_CDC_CAL_TIMER_CFG1 --> 0x4
535 * SDCC_CSR_CDC_REFCOUNT_CFG --> 0xCB732020
536 * SDCC_CSR_CDC_COARSE_CAL_CFG --> 0xB19
Channagoud Kadabi86afb382014-06-23 11:15:46 -0700537 * SDCC_CSR_CDC_DELAY_CFG --> 0x4E2
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700538 * SDCC_CDC_OFFSET_CFG --> 0x0
539 * SDCC_CDC_SLAVE_DDA_CFG --> 0x16334
540 */
541
542 REG_WRITE32(host, 0x11800EC, SDCC_CSR_CDC_CTRL_CFG0);
543 REG_WRITE32(host, 0x3011111, SDCC_CSR_CDC_CTRL_CFG1);
544 REG_WRITE32(host, 0x1201000, SDCC_CSR_CDC_CAL_TIMER_CFG0);
545 REG_WRITE32(host, 0x4, SDCC_CSR_CDC_CAL_TIMER_CFG1);
546 REG_WRITE32(host, 0xCB732020, SDCC_CSR_CDC_REFCOUNT_CFG);
547 REG_WRITE32(host, 0xB19, SDCC_CSR_CDC_COARSE_CAL_CFG);
Channagoud Kadabi86afb382014-06-23 11:15:46 -0700548 REG_WRITE32(host, 0x4E2, SDCC_CSR_CDC_DELAY_CFG);
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700549 REG_WRITE32(host, 0x0, SDCC_CDC_OFFSET_CFG);
550 REG_WRITE32(host, 0x16334, SDCC_CDC_SLAVE_DDA_CFG);
551
552 /* Write 1 to SW_TRIGGER_FULL_CALIB */
553 REG_WRITE32(host, (REG_READ32(host, SDCC_CSR_CDC_CTRL_CFG0) | CDC_SW_TRIGGER_FULL_CALIB), SDCC_CSR_CDC_CTRL_CFG0);
554
555 /* Write 0 to SW_TRIGGER_FULL_CALIB */
556 REG_WRITE32(host, (REG_READ32(host, SDCC_CSR_CDC_CTRL_CFG0) & ~CDC_SW_TRIGGER_FULL_CALIB), SDCC_CSR_CDC_CTRL_CFG0);
557
558 /* Write 1 to HW_AUTO_CAL_EN */
559 REG_WRITE32(host, (REG_READ32(host, SDCC_CSR_CDC_CTRL_CFG0) | CDC_HW_AUTO_CAL_EN), SDCC_CSR_CDC_CTRL_CFG0);
560
561 /* Write 1 to TIMER_ENA */
562 REG_WRITE32(host, (REG_READ32(host, SDCC_CSR_CDC_CAL_TIMER_CFG0) | CDC_TIMER_EN), SDCC_CSR_CDC_CAL_TIMER_CFG0);
563
564 /* Wait for CALIBRATION_DONE in CDC_STATUS */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700565 timeout = CDC_STATUS_TIMEOUT;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700566 while (!(REG_READ32(host, SDCC_CSR_CDC_STATUS0) & BIT(0)))
567 {
568 timeout--;
569 mdelay(1);
570 if (!timeout)
571 {
572 dprintf(CRITICAL, "Error: Calibration done in CDC status not set\n");
573 return 1;
574 }
575 }
576
577 cdc_err = REG_READ32(host, SDCC_CSR_CDC_STATUS0) & CSR_CDC_ERROR_MASK;
578 if (cdc_err)
579 {
580 dprintf(CRITICAL, "CDC error set during calibration: %x\n", cdc_err);
581 return 1;
582 }
583 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
584 REG_WRITE32(host, (REG_READ32(host, SDCC_CDC_DDR200_CFG) | START_CDC_TRAFFIC), SDCC_CDC_DDR200_CFG);
585
Channagoud Kadabie632e252014-03-31 15:26:00 -0700586 DBG("\n CDCLP533 Calibration Done\n");
587
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700588 return 0;
589}
590
Channagoud Kadabi756e1e32014-06-05 13:00:55 -0700591
592static uint32_t sdhci_msm_hs400_calibration(struct sdhci_host *host)
593{
594 DBG("\n HS400 Calibration Start\n");
595
596 /* Reset & Initialize the DLL block */
597 if (sdhci_msm_init_dll(host))
598 return 1;
599
600 /* Write the save phase */
601 if (sdhci_msm_config_dll(host, host->msm_host->saved_phase))
602 return 1;
603
604 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
605 REG_WRITE32(host, (REG_READ32(host, SDCC_DLL_CONFIG_REG) | CMD_DAT_TRACK_SEL), SDCC_DLL_CONFIG_REG);
606
607 if (host->use_cdclp533)
608 return sdhci_msm_cdclp533_calibration(host);
609 else
610 return sdhci_msm_cm_dll_sdc4_calibration(host);
611
612 DBG("\n HS400 Calibration Done\n");
613
614 return 0;
615}
616
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700617/*
618 * Function: sdhci msm execute tuning
619 * Arg : Host structure & bus width
620 * Return : 0 on Success, 1 on Failure
Channagoud Kadabi756e1e32014-06-05 13:00:55 -0700621 * Flow: : Execute Tuning sequence for HS200 and calibration for hs400
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700622 */
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700623uint32_t sdhci_msm_execute_tuning(struct sdhci_host *host, struct mmc_card *card, uint32_t bus_width)
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700624{
625 uint32_t *tuning_block;
626 uint32_t *tuning_data;
627 uint32_t tuned_phases[MAX_PHASES] = {{0}};
628 uint32_t size;
629 uint32_t phase = 0;
630 uint32_t tuned_phase_cnt = 0;
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700631 uint8_t drv_type = 0;
632 bool drv_type_changed = false;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700633 int ret = 0;
Channagoud Kadabie632e252014-03-31 15:26:00 -0700634 int i;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700635 struct sdhci_msm_data *msm_host;
636
637 msm_host = host->msm_host;
638
639 /* In Tuning mode */
640 host->tuning_in_progress = true;
641
642 /* Calibration for CDCLP533 needed for HS400 mode */
643 if (msm_host->tuning_done && !msm_host->calibration_done && host->timing == MMC_HS400_TIMING)
644 {
Channagoud Kadabi756e1e32014-06-05 13:00:55 -0700645 ret = sdhci_msm_hs400_calibration(host);
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700646 if (!ret)
647 msm_host->calibration_done = true;
648 goto out;
649 }
650
651 if (bus_width == DATA_BUS_WIDTH_8BIT)
652 {
653 tuning_block = tuning_block_128;
654 size = sizeof(tuning_block_128);
655 }
656 else
657 {
658 tuning_block = tuning_block_64;
659 size = sizeof(tuning_block_64);
660 }
661
662 tuning_data = (uint32_t *) memalign(CACHE_LINE, ROUNDUP(size, CACHE_LINE));
663
664 ASSERT(tuning_data);
665
666 /* Reset & Initialize the DLL block */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700667 if (sdhci_msm_init_dll(host))
668 {
669 ret = 1;
670 goto free;
671 }
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700672
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700673retry_tuning:
674 tuned_phase_cnt = 0;
675 phase = 0;
676
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700677 while (phase < MAX_PHASES)
678 {
679 struct mmc_command cmd = {0};
680
681 /* configure dll to set phase delay */
Channagoud Kadabie632e252014-03-31 15:26:00 -0700682 if (sdhci_msm_config_dll(host, phase))
683 {
684 ret = 1;
685 goto free;
686 }
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700687
688 cmd.cmd_index = CMD21_SEND_TUNING_BLOCK;
689 cmd.argument = 0x0;
690 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
691 cmd.resp_type = SDHCI_CMD_RESP_R1;
692 cmd.trans_mode = SDHCI_MMC_READ;
693 cmd.data_present = 0x1;
694 cmd.data.data_ptr = tuning_data;
695 cmd.data.blk_sz = size;
696 cmd.data.num_blocks = 0x1;
697
698 /* send command */
699 if (!sdhci_send_command(host, &cmd) && !memcmp(tuning_data, tuning_block, size))
700 tuned_phases[tuned_phase_cnt++] = phase;
701
702 phase++;
703 }
704
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700705 /*
706 * Check if all the tuning phases passed */
707 if (tuned_phase_cnt == MAX_PHASES)
708 {
709 /* Change the driver type & rerun tuning */
Channagoud Kadabie4cab762014-07-08 17:26:06 -0700710 while(++drv_type <= MX_DRV_SUPPORTED_HS200)
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700711 {
712 drv_type_changed = mmc_set_drv_type(host, card, drv_type);
713 if (drv_type_changed)
714 {
715 goto retry_tuning;
716 }
717 }
718 }
719
720 /* Restore the driver strength to default value */
721 if (drv_type_changed)
722 mmc_set_drv_type(host, card, 0);
723
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700724 /* Find the appropriate tuned phase */
725 if (tuned_phase_cnt)
726 {
Channagoud Kadabie632e252014-03-31 15:26:00 -0700727 DBG("\n Tuned phase\n");
728 for (i = 0 ; i < tuned_phase_cnt ; i++)
729 {
730 DBG("%d\t", tuned_phases[i]);
731 }
732
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700733 ret = sdhci_msm_find_appropriate_phase(host, tuned_phases, tuned_phase_cnt);
734
735 if (ret < 0)
736 {
737 dprintf(CRITICAL, "Failed in selecting the tuning phase\n");
738 ret = 1;
739 goto free;
740 }
741
742 phase = (uint32_t) ret;
743 ret = 0;
744
Channagoud Kadabie632e252014-03-31 15:26:00 -0700745 DBG("\n: %s: Tuned Phase: 0x%08x\n", __func__, phase);
746
747 if (sdhci_msm_config_dll(host, phase))
748 goto free;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700749
750 /* Save the tuned phase */
751 host->msm_host->saved_phase = phase;
752 }
753 else
754 {
755 dprintf(CRITICAL, "Failed to get tuned phase\n");
756 ret = 1;
757 }
758
759free:
760 free(tuning_data);
761out:
762 /* Tuning done */
763 host->tuning_in_progress = false;
764 host->msm_host->tuning_done = true;
765 return ret;
Channagoud Kadabiafb8e172013-05-23 13:55:47 -0700766}
Channagoud Kadabi24146af2014-01-24 17:22:08 -0800767
768/*
769 * API to disable HC mode
770 */
771void sdhci_mode_disable(struct sdhci_host *host)
772{
773 /* Disable HC mode */
774 RMWREG32((host->msm_host->pwrctl_base + SDCC_MCI_HC_MODE), SDHCI_HC_START_BIT, SDHCI_HC_WIDTH, 0);
775}
776