blob: aca1a9dbdbb5cac03f7d3cc6c1ec31ad0397f7ce [file] [log] [blame]
Parth Dixitb27f0f52016-02-19 17:01:15 +05301/* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Channagoud Kadabi74ed8352013-03-11 13:12:05 -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>
33#include <kernel/event.h>
34#include <target.h>
35#include <string.h>
36#include <stdlib.h>
37#include <bits.h>
38#include <debug.h>
39#include <sdhci.h>
Channagoud Kadabie9168e82014-01-28 21:33:34 -080040#include <sdhci_msm.h>
Channagoud Kadabi74ed8352013-03-11 13:12:05 -070041
Channagoud Kadabie632e252014-03-31 15:26:00 -070042static void sdhci_dumpregs(struct sdhci_host *host)
43{
44 DBG("****************** SDHC REG DUMP START ********************\n");
45
46 DBG("Version: 0x%08x\n", REG_READ32(host, SDHCI_ARG2_REG));
47 DBG("Arg2: 0x%08x\t Blk Cnt: 0x%08x\n",
48 REG_READ32(host, SDHCI_ARG2_REG),
49 REG_READ16(host, SDHCI_BLK_CNT_REG));
50 DBG("Arg1: 0x%08x\t Blk Sz : 0x%08x\n",
51 REG_READ32(host, SDHCI_ARGUMENT_REG),
52 REG_READ16(host, SDHCI_BLKSZ_REG));
53 DBG("Command: 0x%08x\t Trans mode: 0x%08x\n",
54 REG_READ16(host, SDHCI_CMD_REG),
55 REG_READ16(host, SDHCI_TRANS_MODE_REG));
56 DBG("Resp0: 0x%08x\t Resp1: 0x%08x\n",
57 REG_READ32(host, SDHCI_RESP_REG),
58 REG_READ32(host, SDHCI_RESP_REG + 0x4));
59 DBG("Resp2: 0x%08x\t Resp3: 0x%08x\n",
60 REG_READ32(host, SDHCI_RESP_REG + 0x8),
61 REG_READ32(host, SDHCI_RESP_REG + 0xC));
62 DBG("Prsnt State: 0x%08x\t Host Ctrl1: 0x%08x\n",
63 REG_READ32(host, SDHCI_PRESENT_STATE_REG),
64 REG_READ8(host, SDHCI_HOST_CTRL1_REG));
65 DBG("Timeout ctrl: 0x%08x\t Power Ctrl: 0x%08x\n",
66 REG_READ8(host, SDHCI_TIMEOUT_REG),
67 REG_READ8(host, SDHCI_PWR_CTRL_REG));
68 DBG("Error stat: 0x%08x\t Int Status: 0x%08x\n",
Channagoud Kadabif1746d42014-05-14 18:22:36 -070069 REG_READ16(host, SDHCI_ERR_INT_STS_REG),
70 REG_READ16(host, SDHCI_NRML_INT_STS_REG));
Channagoud Kadabie632e252014-03-31 15:26:00 -070071 DBG("Host Ctrl2: 0x%08x\t Clock ctrl: 0x%08x\n",
Channagoud Kadabif1746d42014-05-14 18:22:36 -070072 REG_READ16(host, SDHCI_HOST_CTRL2_REG),
73 REG_READ16(host, SDHCI_CLK_CTRL_REG));
Channagoud Kadabie632e252014-03-31 15:26:00 -070074 DBG("Caps1: 0x%08x\t Caps2: 0x%08x\n",
75 REG_READ32(host, SDHCI_CAPS_REG1),
76 REG_READ32(host, SDHCI_CAPS_REG1));
77 DBG("Adma Err: 0x%08x\t Auto Cmd err: 0x%08x\n",
78 REG_READ8(host, SDHCI_ADM_ERR_REG),
79 REG_READ16(host, SDHCI_AUTO_CMD_ERR));
80 DBG("Adma addr1: 0x%08x\t Adma addr2: 0x%08x\n",
81 REG_READ32(host, SDHCI_ADM_ADDR_REG),
82 REG_READ32(host, SDHCI_ADM_ADDR_REG + 0x4));
83
84 DBG("****************** SDHC REG DUMP END ********************\n");
85
86 DBG("************* SDHC VENDOR REG DUMPS START ***************\n");
87 DBG("SDCC_DLL_CONFIG_REG: 0x%08x\n", REG_READ32(host, SDCC_DLL_CONFIG_REG));
88 DBG("SDCC_VENDOR_SPECIFIC_FUNC: 0x%08x\n", REG_READ32(host, SDCC_VENDOR_SPECIFIC_FUNC));
89 DBG("SDCC_REG_DLL_STATUS: 0x%08x\n", REG_READ32(host, SDCC_REG_DLL_STATUS));
90 DBG("************* SDHC VENDOR REG DUMPS END ***************\n");
91}
92
Channagoud Kadabi74ed8352013-03-11 13:12:05 -070093/*
Channagoud Kadabi7ad70ea2013-08-08 13:51:04 -070094 * Function: sdhci reset
95 * Arg : Host structure & mask to write to reset register
96 * Return : None
97 * Flow: : Reset the host controller
98 */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -070099void sdhci_reset(struct sdhci_host *host, uint8_t mask)
Channagoud Kadabi7ad70ea2013-08-08 13:51:04 -0700100{
101 uint32_t reg;
102 uint32_t timeout = SDHCI_RESET_MAX_TIMEOUT;
103
104 REG_WRITE8(host, mask, SDHCI_RESET_REG);
105
106 /* Wait for the reset to complete */
107 do {
108 reg = REG_READ8(host, SDHCI_RESET_REG);
109 reg &= mask;
110
111 if (!reg)
112 break;
113 if (!timeout)
114 {
115 dprintf(CRITICAL, "Error: sdhci reset failed for: %x\n", mask);
116 break;
117 }
118
119 timeout--;
120 mdelay(1);
121
122 } while(1);
123}
124
125/*
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700126 * Function: sdhci error status enable
127 * Arg : Host structure
128 * Return : None
129 * Flow: : Enable command error status
130 */
131static void sdhci_error_status_enable(struct sdhci_host *host)
132{
133 /* Enable all interrupt status */
134 REG_WRITE16(host, SDHCI_NRML_INT_STS_EN, SDHCI_NRML_INT_STS_EN_REG);
135 REG_WRITE16(host, SDHCI_ERR_INT_STS_EN, SDHCI_ERR_INT_STS_EN_REG);
136 /* Enable all interrupt signal */
137 REG_WRITE16(host, SDHCI_NRML_INT_SIG_EN, SDHCI_NRML_INT_SIG_EN_REG);
138 REG_WRITE16(host, SDHCI_ERR_INT_SIG_EN, SDHCI_ERR_INT_SIG_EN_REG);
139}
140
141/*
142 * Function: sdhci clock supply
143 * Arg : Host structure
144 * Return : 0 on Success, 1 on Failure
145 * Flow: : 1. Calculate the clock divider
146 * 2. Set the clock divider
147 * 3. Check if clock stable
148 * 4. Enable Clock
149 */
150uint32_t sdhci_clk_supply(struct sdhci_host *host, uint32_t clk)
151{
152 uint32_t div = 0;
153 uint32_t freq = 0;
154 uint16_t clk_val = 0;
155
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700156 if (clk >= host->caps.base_clk_rate)
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700157 goto clk_ctrl;
158
159 /* As per the sd spec div should be a multiplier of 2 */
160 for (div = 2; div < SDHCI_CLK_MAX_DIV; div += 2) {
161 freq = host->caps.base_clk_rate / div;
162 if (freq <= clk)
163 break;
164 }
165
166 div >>= 1;
167
168clk_ctrl:
169 /* As per the sdhci spec 3.0, bits 6-7 of the clock
170 * control registers will be mapped to bit 8-9, to
171 * support a 10 bit divider value.
172 * This is needed when the divider value overflows
173 * the 8 bit range.
174 */
175 clk_val = ((div & SDHCI_SDCLK_FREQ_MASK) << SDHCI_SDCLK_FREQ_SEL);
176 clk_val |= ((div & SDHC_SDCLK_UP_BIT_MASK) >> SDHCI_SDCLK_FREQ_SEL)
177 << SDHCI_SDCLK_UP_BIT_SEL;
178
179 clk_val |= SDHCI_INT_CLK_EN;
180 REG_WRITE16(host, clk_val, SDHCI_CLK_CTRL_REG);
181
182 /* Check for clock stable */
183 while (!(REG_READ16(host, SDHCI_CLK_CTRL_REG) & SDHCI_CLK_STABLE));
184
185 /* Now clock is stable, enable it */
186 clk_val = REG_READ16(host, SDHCI_CLK_CTRL_REG);
187 clk_val |= SDHCI_CLK_EN;
188 REG_WRITE16(host, clk_val, SDHCI_CLK_CTRL_REG);
189
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700190 host->cur_clk_rate = clk;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700191
Channagoud Kadabie632e252014-03-31 15:26:00 -0700192 DBG("\n %s: clock_rate: %d clock_div:0x%08x\n", __func__, clk, div);
193
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700194 return 0;
195}
196
197/*
198 * Function: sdhci stop sdcc clock
199 * Arg : Host structure
200 * Return : 0 on Success, 1 on Failure
201 * Flow: : 1. Stop the clock
202 */
203static uint32_t sdhci_stop_sdcc_clk(struct sdhci_host *host)
204{
205 uint32_t reg;
206
207 reg = REG_READ32(host, SDHCI_PRESENT_STATE_REG);
208
209 if (reg & (SDHCI_CMD_ACT | SDHCI_DAT_ACT)) {
210 dprintf(CRITICAL, "Error: SDCC command & data line are active\n");
211 return 1;
212 }
213
214 REG_WRITE16(host, SDHCI_CLK_DIS, SDHCI_CLK_CTRL_REG);
215
216 return 0;
217}
218
219/*
220 * Function: sdhci change frequency
221 * Arg : Host structure & clock value
222 * Return : 0 on Success, 1 on Failure
223 * Flow: : 1. Stop the clock
224 * 2. Star the clock with new frequency
225 */
226static uint32_t sdhci_change_freq_clk(struct sdhci_host *host, uint32_t clk)
227{
228 if (sdhci_stop_sdcc_clk(host)) {
229 dprintf(CRITICAL, "Error: Card is busy, cannot change frequency\n");
230 return 1;
231 }
232
233 if (sdhci_clk_supply(host, clk)) {
234 dprintf(CRITICAL, "Error: cannot change frequency\n");
235 return 1;
236 }
237
238 return 0;
239}
240
241/*
242 * Function: sdhci set bus power
243 * Arg : Host structure
244 * Return : None
245 * Flow: : 1. Set the voltage
246 * 2. Set the sd power control register
247 */
248static void sdhci_set_bus_power_on(struct sdhci_host *host)
249{
250 uint8_t voltage;
251
252 voltage = host->caps.voltage;
253
254 voltage <<= SDHCI_BUS_VOL_SEL;
Channagoud Kadabi89902512013-05-14 13:22:06 -0700255 REG_WRITE8(host, voltage, SDHCI_PWR_CTRL_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700256
257 voltage |= SDHCI_BUS_PWR_EN;
258
Channagoud Kadabie632e252014-03-31 15:26:00 -0700259 DBG("\n %s: voltage: 0x%02x\n", __func__, voltage);
260
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700261 REG_WRITE8(host, voltage, SDHCI_PWR_CTRL_REG);
262
263}
264
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700265
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700266/*
267 * Function: sdhci set SDR mode
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700268 * Arg : Host structure, UHS mode
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700269 * Return : None
270 * Flow: : 1. Disable the clock
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700271 * 2. Enable UHS mode
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700272 * 3. Enable the clock
273 * Details : SDR50/SDR104 mode is nothing but HS200
274 * mode SDCC spec refers to it as SDR mode
275 * & emmc spec refers as HS200 mode.
276 */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700277void sdhci_set_uhs_mode(struct sdhci_host *host, uint32_t mode)
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700278{
279 uint16_t clk;
280 uint16_t ctrl = 0;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700281 uint32_t clk_val = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700282
283 /* Disable the clock */
284 clk = REG_READ16(host, SDHCI_CLK_CTRL_REG);
285 clk &= ~SDHCI_CLK_EN;
286 REG_WRITE16(host, clk, SDHCI_CLK_CTRL_REG);
287
288 ctrl = REG_READ16(host, SDHCI_HOST_CTRL2_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700289
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700290 ctrl &= ~SDHCI_UHS_MODE_MASK;
291
292 /* Enable SDR50/SDR104/DDR50 mode */
293 switch (mode)
294 {
295 case SDHCI_SDR104_MODE:
296 ctrl |= SDHCI_SDR104_MODE_EN;
297 clk_val = SDHCI_CLK_200MHZ;
298 break;
299 case SDHCI_SDR50_MODE:
300 ctrl |= SDHCI_SDR50_MODE_EN;
301 clk_val = SDHCI_CLK_100MHZ;
302 break;
303 case SDHCI_DDR50_MODE:
304 ctrl |= SDHCI_DDR50_MODE_EN;
305 clk_val = SDHCI_CLK_50MHZ;
306 break;
307 case SDHCI_SDR25_MODE:
308 ctrl |= SDHCI_SDR25_MODE_EN;
309 clk_val = SDHCI_CLK_50MHZ;
310 break;
311 case SDHCI_SDR12_MODE_EN:
312 ctrl |= SDHCI_SDR12_MODE_EN;
313 clk_val = SDHCI_CLK_25MHZ;
314 break;
315 default:
316 dprintf(CRITICAL, "Error: Invalid UHS mode: %x\n", mode);
317 ASSERT(0);
318 };
319
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700320 REG_WRITE16(host, ctrl, SDHCI_HOST_CTRL2_REG);
321
322 /* Run the clock back */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700323 sdhci_clk_supply(host, clk_val);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700324}
325
326/*
327 * Function: sdhci set adma mode
328 * Arg : Host structure
329 * Return : None
330 * Flow: : Set adma mode
331 */
332static void sdhci_set_adma_mode(struct sdhci_host *host)
333{
334 /* Select 32 Bit ADMA2 type */
335 REG_WRITE8(host, SDHCI_ADMA_32BIT, SDHCI_HOST_CTRL1_REG);
336}
337
338/*
339 * Function: sdhci set bus width
340 * Arg : Host & width
341 * Return : 0 on Sucess, 1 on Failure
342 * Flow: : Set the bus width for controller
343 */
344uint8_t sdhci_set_bus_width(struct sdhci_host *host, uint16_t width)
345{
346 uint16_t reg = 0;
347
348 reg = REG_READ8(host, SDHCI_HOST_CTRL1_REG);
349
350 switch(width) {
351 case DATA_BUS_WIDTH_8BIT:
352 width = SDHCI_BUS_WITDH_8BIT;
353 break;
354 case DATA_BUS_WIDTH_4BIT:
355 width = SDHCI_BUS_WITDH_4BIT;
356 break;
357 case DATA_BUS_WIDTH_1BIT:
358 width = SDHCI_BUS_WITDH_1BIT;
359 break;
360 default:
361 dprintf(CRITICAL, "Bus width is invalid: %u\n", width);
362 return 1;
363 }
364
Channagoud Kadabie632e252014-03-31 15:26:00 -0700365 DBG("\n %s: bus width:0x%04x\n", __func__, width);
366
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700367 REG_WRITE8(host, (reg | width), SDHCI_HOST_CTRL1_REG);
368
369 return 0;
370}
371
372/*
373 * Function: sdhci command err status
374 * Arg : Host structure
375 * Return : 0 on Sucess, 1 on Failure
376 * Flow: : Look for error status
377 */
378static uint8_t sdhci_cmd_err_status(struct sdhci_host *host)
379{
380 uint32_t err;
381
382 err = REG_READ16(host, SDHCI_ERR_INT_STS_REG);
383
384 if (err & SDHCI_CMD_TIMEOUT_MASK) {
385 dprintf(CRITICAL, "Error: Command timeout error\n");
386 return 1;
387 } else if (err & SDHCI_CMD_CRC_MASK) {
388 dprintf(CRITICAL, "Error: Command CRC error\n");
389 return 1;
390 } else if (err & SDHCI_CMD_END_BIT_MASK) {
391 dprintf(CRITICAL, "Error: CMD end bit error\n");
392 return 1;
393 } else if (err & SDHCI_CMD_IDX_MASK) {
394 dprintf(CRITICAL, "Error: Command Index error\n");
395 return 1;
396 } else if (err & SDHCI_DAT_TIMEOUT_MASK) {
397 dprintf(CRITICAL, "Error: DATA time out error\n");
398 return 1;
399 } else if (err & SDHCI_DAT_CRC_MASK) {
400 dprintf(CRITICAL, "Error: DATA CRC error\n");
401 return 1;
402 } else if (err & SDHCI_DAT_END_BIT_MASK) {
403 dprintf(CRITICAL, "Error: DATA end bit error\n");
404 return 1;
405 } else if (err & SDHCI_CUR_LIM_MASK) {
406 dprintf(CRITICAL, "Error: Current limit error\n");
407 return 1;
408 } else if (err & SDHCI_AUTO_CMD12_MASK) {
409 dprintf(CRITICAL, "Error: Auto CMD12 error\n");
410 return 1;
411 } else if (err & SDHCI_ADMA_MASK) {
412 dprintf(CRITICAL, "Error: ADMA error\n");
413 return 1;
414 }
415
416 return 0;
417}
418
419/*
420 * Function: sdhci command complete
421 * Arg : Host & command structure
422 * Return : 0 on Sucess, 1 on Failure
423 * Flow: : 1. Check for command complete
424 * 2. Check for transfer complete
425 * 3. Get the command response
426 * 4. Check for errors
427 */
428static uint8_t sdhci_cmd_complete(struct sdhci_host *host, struct mmc_command *cmd)
429{
430 uint8_t i;
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700431 uint8_t ret = 0;
432 uint8_t need_reset = 0;
Channagoud Kadabi7491e6c2014-12-02 15:42:11 -0800433 uint64_t retry = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700434 uint32_t int_status;
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700435 uint32_t trans_complete = 0;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700436 uint32_t err_status;
Channagoud Kadabie86a40b2014-03-12 17:48:51 -0700437 uint64_t max_trans_retry = (cmd->cmd_timeout ? cmd->cmd_timeout : SDHCI_MAX_TRANS_RETRY);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700438
439 do {
Gaurav Nebhwani77e21612016-04-27 15:00:07 +0530440
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700441 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700442
Gaurav Nebhwani77e21612016-04-27 15:00:07 +0530443 if((int_status & SDHCI_INT_STS_CMD_COMPLETE) &&
444 !(REG_READ16(host, SDHCI_ERR_INT_STS_REG) & SDHCI_CMD_TIMEOUT_MASK))
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700445 break;
vijay kumar6c602922015-08-11 13:12:55 +0530446 /*
447 * Some controllers set the data timout first on issuing an erase & take time
448 * to set data complete interrupt. We need to wait hoping the controller would
449 * set data complete
450 */
451 else if (int_status & SDHCI_ERR_INT_STAT_MASK && !host->tuning_in_progress &&
452 !((REG_READ16(host, SDHCI_ERR_INT_STS_REG) & SDHCI_DAT_TIMEOUT_MASK)))
Channagoud Kadabi57f37a82014-08-25 13:33:49 -0700453 goto err;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700454
Channagoud Kadabie632e252014-03-31 15:26:00 -0700455 /*
Channagoud Kadabi18748bb2014-06-04 17:58:19 -0700456 * If Tuning is in progress ignore cmd crc, cmd timeout & cmd end bit errors
Channagoud Kadabie632e252014-03-31 15:26:00 -0700457 */
458 if (host->tuning_in_progress)
459 {
460 err_status = REG_READ16(host, SDHCI_ERR_INT_STS_REG);
Channagoud Kadabi756e1e32014-06-05 13:00:55 -0700461 if ((err_status & SDHCI_CMD_CRC_MASK) || (err_status & SDHCI_CMD_END_BIT_MASK)
Parth Dixit6dda8d42015-06-10 22:39:34 +0530462 || (err_status & SDHCI_CMD_TIMEOUT_MASK)
463 || (err_status & SDHCI_CMD_IDX_MASK))
Channagoud Kadabie632e252014-03-31 15:26:00 -0700464 {
465 sdhci_reset(host, (SOFT_RESET_CMD | SOFT_RESET_DATA));
Parth Dixit6dda8d42015-06-10 22:39:34 +0530466 return 1;
Channagoud Kadabie632e252014-03-31 15:26:00 -0700467 }
468 }
469
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700470 retry++;
Channagoud Kadabi96622212014-07-30 12:13:28 -0700471 udelay(1);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700472 if (retry == SDHCI_MAX_CMD_RETRY) {
473 dprintf(CRITICAL, "Error: Command never completed\n");
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700474 ret = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700475 goto err;
476 }
477 } while(1);
478
479 /* Command is complete, clear the interrupt bit */
480 REG_WRITE16(host, SDHCI_INT_STS_CMD_COMPLETE, SDHCI_NRML_INT_STS_REG);
481
482 /* Copy the command response,
483 * The valid bits for R2 response are 0-119, & but the actual response
484 * is stored in bits 8-128. We need to move 8 bits of MSB of each
485 * response to register 8 bits of LSB of next response register.
486 * As:
487 * MSB 8 bits of RESP0 --> LSB 8 bits of RESP1
488 * MSB 8 bits of RESP1 --> LSB 8 bits of RESP2
489 * MSB 8 bits of RESP2 --> LSB 8 bits of RESP3
490 */
491 if (cmd->resp_type == SDHCI_CMD_RESP_R2) {
492 for (i = 0; i < 4; i++) {
493 cmd->resp[i] = REG_READ32(host, SDHCI_RESP_REG + (i * 4));
494 cmd->resp[i] <<= SDHCI_RESP_LSHIFT;
495
496 if (i != 0)
497 cmd->resp[i] |= (REG_READ32(host, SDHCI_RESP_REG + ((i-1) * 4)) >> SDHCI_RESP_RSHIFT);
498 }
499 } else
500 cmd->resp[0] = REG_READ32(host, SDHCI_RESP_REG);
501
502 retry = 0;
503
504 /*
505 * Clear the transfer complete interrupt
506 */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700507 if (cmd->data_present || cmd->resp_type == SDHCI_CMD_RESP_R1B) {
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700508 do {
509 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700510
511 if (int_status & SDHCI_INT_STS_TRANS_COMPLETE)
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700512 {
513 trans_complete = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700514 break;
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700515 }
Channagoud Kadabidb43aee2015-01-09 11:57:19 -0800516 /*
517 * Some controllers set the data timout first on issuing an erase & take time
518 * to set data complete interrupt. We need to wait hoping the controller would
519 * set data complete
520 */
521 else if (int_status & SDHCI_ERR_INT_STAT_MASK && !host->tuning_in_progress &&
522 !((REG_READ16(host, SDHCI_ERR_INT_STS_REG) & SDHCI_DAT_TIMEOUT_MASK)))
Channagoud Kadabi57f37a82014-08-25 13:33:49 -0700523 goto err;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700524
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700525 /*
526 * If we are in tuning then we need to wait until Data timeout , Data end
527 * or Data CRC error
528 */
529 if (host->tuning_in_progress)
530 {
531 err_status = REG_READ16(host, SDHCI_ERR_INT_STS_REG);
532 if ((err_status & SDHCI_DAT_TIMEOUT_MASK) || (err_status & SDHCI_DAT_CRC_MASK))
533 {
534 sdhci_reset(host, (SOFT_RESET_CMD | SOFT_RESET_DATA));
Parth Dixit6dda8d42015-06-10 22:39:34 +0530535 return 1;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700536 }
537 }
538
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700539 retry++;
Channagoud Kadabi96622212014-07-30 12:13:28 -0700540 udelay(1);
Channagoud Kadabie86a40b2014-03-12 17:48:51 -0700541 if (retry == max_trans_retry) {
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700542 dprintf(CRITICAL, "Error: Transfer never completed\n");
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700543 ret = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700544 goto err;
545 }
546 } while(1);
547
548 /* Transfer is complete, clear the interrupt bit */
549 REG_WRITE16(host, SDHCI_INT_STS_TRANS_COMPLETE, SDHCI_NRML_INT_STS_REG);
550 }
551
552err:
553 /* Look for errors */
554 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700555
556 if (int_status & SDHCI_ERR_INT_STAT_MASK)
557 {
558 /*
559 * As per SDHC spec transfer complete has higher priority than data timeout
560 * If both transfer complete & data timeout are set then we should ignore
561 * data timeout error.
562 * ---------------------------------------------------------------------------
563 * | Transfer complete | Data timeout error | Meaning of the Status |
564 * |--------------------------------------------------------------------------|
565 * | 0 | 0 | Interrupted by another factor |
566 * |--------------------------------------------------------------------------|
567 * | 0 | 1 | Time out occured during transfer|
568 * |--------------------------------------------------------------------------|
569 * | 1 | Don't Care | Command execution complete |
570 * --------------------------------------------------------------------------
571 */
572 if ((REG_READ16(host, SDHCI_ERR_INT_STS_REG) & SDHCI_DAT_TIMEOUT_MASK) && trans_complete)
573 {
574 ret = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700575 }
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700576 else if (sdhci_cmd_err_status(host))
577 {
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700578 ret = 1;
Channagoud Kadabie632e252014-03-31 15:26:00 -0700579 /* Dump sdhc registers on error */
580 sdhci_dumpregs(host);
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700581 }
582 /* Reset Command & Dat lines on error */
583 need_reset = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700584 }
585
586 /* Reset data & command line */
Channagoud Kadabie4cab762014-07-08 17:26:06 -0700587 if (need_reset)
Channagoud Kadabi7ad70ea2013-08-08 13:51:04 -0700588 sdhci_reset(host, (SOFT_RESET_CMD | SOFT_RESET_DATA));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700589
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700590 return ret;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700591}
592
593/*
594 * Function: sdhci prep desc table
595 * Arg : Pointer data & length
596 * Return : Pointer to desc table
597 * Flow: : Prepare the adma table as per the sd spec v 3.0
598 */
599static struct desc_entry *sdhci_prep_desc_table(void *data, uint32_t len)
600{
601 struct desc_entry *sg_list;
602 uint32_t sg_len = 0;
603 uint32_t remain = 0;
604 uint32_t i;
605 uint32_t table_len = 0;
606
607 if (len <= SDHCI_ADMA_DESC_LINE_SZ) {
608 /* Allocate only one descriptor */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700609 sg_list = (struct desc_entry *) memalign(lcm(4, CACHE_LINE), ROUNDUP(sizeof(struct desc_entry), CACHE_LINE));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700610
611 if (!sg_list) {
612 dprintf(CRITICAL, "Error allocating memory\n");
613 ASSERT(0);
614 }
615
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700616 sg_list[0].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700617 sg_list[0].len = (len < SDHCI_ADMA_DESC_LINE_SZ) ? len : (SDHCI_ADMA_DESC_LINE_SZ & 0xffff);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700618 sg_list[0].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA
619 | SDHCI_ADMA_TRANS_END;
620
Channagoud Kadabie632e252014-03-31 15:26:00 -0700621 sg_len = 1;
622 table_len = sizeof(struct desc_entry);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700623 } else {
624 /* Calculate the number of entries in desc table */
625 sg_len = len / SDHCI_ADMA_DESC_LINE_SZ;
626 remain = len - (sg_len * SDHCI_ADMA_DESC_LINE_SZ);
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700627
628 /* Allocate sg_len + 1 entries if there are remaining bytes at the end */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700629 if (remain)
630 sg_len++;
631
632 table_len = (sg_len * sizeof(struct desc_entry));
633
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700634 sg_list = (struct desc_entry *) memalign(lcm(4, CACHE_LINE), ROUNDUP(table_len, CACHE_LINE));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700635
636 if (!sg_list) {
637 dprintf(CRITICAL, "Error allocating memory\n");
638 ASSERT(0);
639 }
640
641 memset((void *) sg_list, 0, table_len);
642
643 /*
644 * Prepare sglist in the format:
645 * ___________________________________________________
646 * |Transfer Len | Transfer ATTR | Data Address |
647 * | (16 bit) | (16 bit) | (32 bit) |
648 * |_____________|_______________|_____________________|
649 */
650 for (i = 0; i < (sg_len - 1); i++) {
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700651 sg_list[i].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700652 /*
653 * Length attribute is 16 bit value & max transfer size for one
654 * descriptor line is 65536 bytes, As per SD Spec3.0 'len = 0'
655 * implies 65536 bytes. Truncate the length to limit to 16 bit
656 * range.
657 */
658 sg_list[i].len = (SDHCI_ADMA_DESC_LINE_SZ & 0xffff);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700659 sg_list[i].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA;
660 data += SDHCI_ADMA_DESC_LINE_SZ;
661 len -= SDHCI_ADMA_DESC_LINE_SZ;
662 }
663
664 /* Fill the last entry of the table with Valid & End
665 * attributes
666 */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700667 sg_list[sg_len - 1].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700668 sg_list[sg_len - 1].len = (len < SDHCI_ADMA_DESC_LINE_SZ) ? len : (SDHCI_ADMA_DESC_LINE_SZ & 0xffff);
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700669 sg_list[sg_len - 1].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA |
670 SDHCI_ADMA_TRANS_END;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700671 }
672
673 arch_clean_invalidate_cache_range((addr_t)sg_list, table_len);
674
Channagoud Kadabie632e252014-03-31 15:26:00 -0700675 for (i = 0; i < sg_len; i++)
676 {
677 DBG("\n %s: sg_list: addr: 0x%08x len: 0x%04x attr: 0x%04x\n", __func__, sg_list[i].addr,
678 (sg_list[i].len ? sg_list[i].len : SDHCI_ADMA_DESC_LINE_SZ), sg_list[i].tran_att);
679 }
680
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700681 return sg_list;
682}
683
684/*
685 * Function: sdhci adma transfer
686 * Arg : Host structure & command stucture
687 * Return : Pointer to desc table
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700688 * Flow : 1. Prepare descriptor table
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700689 * 2. Write adma register
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700690 * 3. Write block size & block count register
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700691 */
692static struct desc_entry *sdhci_adma_transfer(struct sdhci_host *host,
693 struct mmc_command *cmd)
694{
695 uint32_t num_blks = 0;
696 uint32_t sz;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700697 void *data;
698 struct desc_entry *adma_addr;
699
700
701 num_blks = cmd->data.num_blocks;
702 data = cmd->data.data_ptr;
703
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700704 /*
705 * Some commands send data on DAT lines which is less
706 * than SDHCI_MMC_BLK_SZ, in that case trying to read
707 * more than the data sent by the card results in data
708 * CRC errors. To avoid such errors allow data to pass
709 * the required block size, if the block size is not
710 * passed use the default value
711 */
712 if (cmd->data.blk_sz)
713 sz = num_blks * cmd->data.blk_sz;
714 else
715 sz = num_blks * SDHCI_MMC_BLK_SZ;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700716
717 /* Prepare adma descriptor table */
718 adma_addr = sdhci_prep_desc_table(data, sz);
719
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700720 /* Write adma address to adma register */
721 REG_WRITE32(host, (uint32_t) adma_addr, SDHCI_ADM_ADDR_REG);
722
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700723 /* Write the block size */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700724 if (cmd->data.blk_sz)
725 REG_WRITE16(host, cmd->data.blk_sz, SDHCI_BLKSZ_REG);
726 else
727 REG_WRITE16(host, SDHCI_MMC_BLK_SZ, SDHCI_BLKSZ_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700728
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700729 /*
730 * Set block count in block count register
731 */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700732 REG_WRITE16(host, num_blks, SDHCI_BLK_CNT_REG);
733
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700734 return adma_addr;
735}
736
737/*
738 * Function: sdhci send command
739 * Arg : Host structure & command stucture
740 * Return : 0 on Success, 1 on Failure
741 * Flow: : 1. Prepare the command register
742 * 2. If data is present, prepare adma table
743 * 3. Run the command
744 * 4. Check for command results & take action
745 */
746uint32_t sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
747{
Channagoud Kadabid26eacc2014-06-09 11:41:55 -0700748 uint32_t ret = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700749 uint8_t retry = 0;
750 uint32_t resp_type = 0;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700751 uint16_t trans_mode = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700752 uint16_t present_state;
753 uint32_t flags;
754 struct desc_entry *sg_list = NULL;
755
Channagoud Kadabif1746d42014-05-14 18:22:36 -0700756 DBG("\n %s: START: cmd:%04d, arg:0x%08x, resp_type:0x%04x, data_present:%d\n",
Channagoud Kadabie632e252014-03-31 15:26:00 -0700757 __func__, cmd->cmd_index, cmd->argument, cmd->resp_type, cmd->data_present);
758
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700759 if (cmd->data_present)
760 ASSERT(cmd->data.data_ptr);
761
762 /*
763 * Assert if the data buffer is not aligned to cache
764 * line size for read operations.
765 * For write operations this function assumes that
766 * the cache is already flushed by the caller. As
767 * the data buffer we receive for write operation
768 * may not be aligned to cache boundary due to
769 * certain image formats like sparse image.
770 */
Parth Dixit4abeb262016-04-18 08:09:30 -0700771 if (cmd->trans_mode == SDHCI_READ_MODE)
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700772 ASSERT(IS_CACHE_LINE_ALIGNED(cmd->data.data_ptr));
773
774 do {
775 present_state = REG_READ32(host, SDHCI_PRESENT_STATE_REG);
776 /* check if CMD & DAT lines are free */
777 present_state &= SDHCI_STATE_CMD_DAT_MASK;
778
779 if (!present_state)
780 break;
781 udelay(1000);
782 retry++;
783 if (retry == 10) {
784 dprintf(CRITICAL, "Error: CMD or DAT lines were never freed\n");
785 return 1;
786 }
787 } while(1);
788
789 switch(cmd->resp_type) {
790 case SDHCI_CMD_RESP_R1:
791 case SDHCI_CMD_RESP_R3:
792 case SDHCI_CMD_RESP_R6:
793 case SDHCI_CMD_RESP_R7:
794 /* Response of length 48 have 32 bits
795 * of response data stored in RESP0[0:31]
796 */
797 resp_type = SDHCI_CMD_RESP_48;
798 break;
799
800 case SDHCI_CMD_RESP_R2:
801 /* Response of length 136 have 120 bits
802 * of response data stored in RESP0[0:119]
803 */
804 resp_type = SDHCI_CMD_RESP_136;
805 break;
806
807 case SDHCI_CMD_RESP_R1B:
808 /* Response of length 48 have 32 bits
809 * of response data stored in RESP0[0:31]
810 * & set CARD_BUSY status if card is busy
811 */
812 resp_type = SDHCI_CMD_RESP_48_BUSY;
813 break;
814
815 case SDHCI_CMD_RESP_NONE:
816 resp_type = SDHCI_CMD_RESP_NONE;
817 break;
818
819 default:
820 dprintf(CRITICAL, "Invalid response type for the command\n");
821 return 1;
822 };
823
824 flags = (resp_type << SDHCI_CMD_RESP_TYPE_SEL_BIT);
825 flags |= (cmd->data_present << SDHCI_CMD_DATA_PRESENT_BIT);
826 flags |= (cmd->cmd_type << SDHCI_CMD_CMD_TYPE_BIT);
827
Channagoud Kadabie632e252014-03-31 15:26:00 -0700828 /* Enable Command CRC & Index check for commands with response
829 * R1, R6, R7 & R1B. Also only CRC check for R2 response
830 */
831 switch(cmd->resp_type) {
832 case SDHCI_CMD_RESP_R1:
833 case SDHCI_CMD_RESP_R6:
834 case SDHCI_CMD_RESP_R7:
835 case SDHCI_CMD_RESP_R1B:
836 flags |= (1 << SDHCI_CMD_CRC_CHECK_BIT) | (1 << SDHCI_CMD_IDX_CHECK_BIT);
837 break;
838 case SDHCI_CMD_RESP_R2:
839 flags |= (1 << SDHCI_CMD_CRC_CHECK_BIT);
840 break;
841 default:
842 break;
843 };
844
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700845 /* Set the timeout value */
846 REG_WRITE8(host, SDHCI_CMD_TIMEOUT, SDHCI_TIMEOUT_REG);
847
848 /* Check if data needs to be processed */
849 if (cmd->data_present)
850 sg_list = sdhci_adma_transfer(host, cmd);
851
852 /* Write the argument 1 */
853 REG_WRITE32(host, cmd->argument, SDHCI_ARGUMENT_REG);
854
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700855 /* Set the Transfer mode */
856 if (cmd->data_present)
857 {
858 /* Enable DMA */
859 trans_mode |= SDHCI_DMA_EN;
860
861 if (cmd->trans_mode == SDHCI_MMC_READ)
Channagoud Kadabidd8a7342014-07-09 10:35:01 -0700862 {
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700863 trans_mode |= SDHCI_READ_MODE;
Parth Dixitb27f0f52016-02-19 17:01:15 +0530864 if(cmd->cmd_index == CMD21_SEND_TUNING_BLOCK)
865 sdhci_msm_toggle_cdr(host, false);
866 else
867 sdhci_msm_toggle_cdr(host, true);
Channagoud Kadabidd8a7342014-07-09 10:35:01 -0700868 }
869 else
870 {
871 sdhci_msm_toggle_cdr(host, false);
872 }
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700873
Channagoud Kadabi89902512013-05-14 13:22:06 -0700874 /* Enable auto cmd23 or cmd12 for multi block transfer
875 * based on what command card supports
876 */
Sridhar Parasuramc97e0542015-06-26 16:14:58 -0700877 if ((cmd->data.num_blocks > 1) && !cmd->rel_write) {
Channagoud Kadabi89902512013-05-14 13:22:06 -0700878 if (cmd->cmd23_support) {
879 trans_mode |= SDHCI_TRANS_MULTI | SDHCI_AUTO_CMD23_EN | SDHCI_BLK_CNT_EN;
880 REG_WRITE32(host, cmd->data.num_blocks, SDHCI_ARG2_REG);
881 }
882 else
883 trans_mode |= SDHCI_TRANS_MULTI | SDHCI_AUTO_CMD12_EN | SDHCI_BLK_CNT_EN;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700884 }
Sridhar Parasuramc97e0542015-06-26 16:14:58 -0700885 else if ((cmd->data.num_blocks > 1) && cmd->rel_write) {
886 trans_mode |= SDHCI_TRANS_MULTI | SDHCI_BLK_CNT_EN;
887 }
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700888 }
889
890 /* Write to transfer mode register */
891 REG_WRITE16(host, trans_mode, SDHCI_TRANS_MODE_REG);
892
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700893 /* Write the command register */
894 REG_WRITE16(host, SDHCI_PREP_CMD(cmd->cmd_index, flags), SDHCI_CMD_REG);
895
896 /* Command complete sequence */
897 if (sdhci_cmd_complete(host, cmd))
Channagoud Kadabid26eacc2014-06-09 11:41:55 -0700898 {
899 ret = 1;
900 goto err;
901 }
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700902
903 /* Invalidate the cache only for read operations */
904 if (cmd->trans_mode == SDHCI_MMC_READ)
Channagoud Kadabi597c6dd2015-04-03 10:51:50 -0700905 {
906 /* Read can be performed on block size < SDHCI_MMC_BLK_SZ, make sure to flush
907 * the data only for the read size instead
908 */
909 arch_invalidate_cache_range((addr_t)cmd->data.data_ptr, (cmd->data.blk_sz) ? \
910 (cmd->data.num_blocks * cmd->data.blk_sz) : \
911 (cmd->data.num_blocks * SDHCI_MMC_BLK_SZ));
912 }
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700913
Channagoud Kadabid26eacc2014-06-09 11:41:55 -0700914 DBG("\n %s: END: cmd:%04d, arg:0x%08x, resp:0x%08x 0x%08x 0x%08x 0x%08x\n",
915 __func__, cmd->cmd_index, cmd->argument, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
916err:
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700917 /* Free the scatter/gather list */
918 if (sg_list)
919 free(sg_list);
920
Channagoud Kadabid26eacc2014-06-09 11:41:55 -0700921 return ret;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700922}
923
924/*
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700925 * Function: sdhci init
926 * Arg : Host structure
927 * Return : None
928 * Flow: : 1. Reset the controller
929 * 2. Read the capabilities register & populate the host
930 * controller capabilities for use by other functions
931 * 3. Enable the power control
932 * 4. Set initial bus width
933 * 5. Set Adma mode
934 * 6. Enable the error status
935 */
936void sdhci_init(struct sdhci_host *host)
937{
938 uint32_t caps[2];
Channagoud Kadabi756e1e32014-06-05 13:00:55 -0700939 uint32_t version;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700940
941 /* Read the capabilities register & store the info */
942 caps[0] = REG_READ32(host, SDHCI_CAPS_REG1);
943 caps[1] = REG_READ32(host, SDHCI_CAPS_REG2);
944
Channagoud Kadabie632e252014-03-31 15:26:00 -0700945
946 DBG("\n %s: Host capability: cap1:0x%08x, cap2: 0x%08x\n", __func__, caps[0], caps[1]);
947
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700948 host->caps.base_clk_rate = (caps[0] & SDHCI_CLK_RATE_MASK) >> SDHCI_CLK_RATE_BIT;
949 host->caps.base_clk_rate *= 1000000;
950
951 /* Get the max block length for mmc */
952 host->caps.max_blk_len = (caps[0] & SDHCI_BLK_LEN_MASK) >> SDHCI_BLK_LEN_BIT;
953
954 /* 8 bit Bus width */
955 if (caps[0] & SDHCI_8BIT_WIDTH_MASK)
956 host->caps.bus_width_8bit = 1;
957
958 /* Adma support */
959 if (caps[0] & SDHCI_BLK_ADMA_MASK)
960 host->caps.adma_support = 1;
961
962 /* Supported voltage */
963 if (caps[0] & SDHCI_3_3_VOL_MASK)
964 host->caps.voltage = SDHCI_VOL_3_3;
965 else if (caps[0] & SDHCI_3_0_VOL_MASK)
966 host->caps.voltage = SDHCI_VOL_3_0;
967 else if (caps[0] & SDHCI_1_8_VOL_MASK)
968 host->caps.voltage = SDHCI_VOL_1_8;
969
970 /* DDR mode support */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700971 host->caps.ddr_support = (caps[1] & SDHCI_DDR50_MODE_MASK) ? 1 : 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700972
973 /* SDR50 mode support */
974 host->caps.sdr50_support = (caps[1] & SDHCI_SDR50_MODE_MASK) ? 1 : 0;
975
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700976 /* SDR104 mode support */
977 host->caps.sdr104_support = (caps[1] & SDHCI_SDR104_MODE_MASK) ? 1 : 0;
978
Channagoud Kadabi756e1e32014-06-05 13:00:55 -0700979 version = readl(host->msm_host->pwrctl_base + MCI_VERSION);
980
981 host->major = (version & CORE_VERSION_MAJOR_MASK) >> CORE_VERSION_MAJOR_SHIFT;
982 host->minor = (version & CORE_VERSION_MINOR_MASK);
983
984 if (host->major == 0x1 && host->minor < 0x34)
985 host->use_cdclp533 = true;
986 else
987 host->use_cdclp533 = false;
988
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700989 /* Set bus power on */
990 sdhci_set_bus_power_on(host);
991
992 /* Wait for power interrupt to be handled */
Channagoud Kadabi89902512013-05-14 13:22:06 -0700993 event_wait(host->sdhc_event);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700994
995 /* Set bus width */
996 sdhci_set_bus_width(host, SDHCI_BUS_WITDH_1BIT);
997
998 /* Set Adma mode */
999 sdhci_set_adma_mode(host);
1000
1001 /*
1002 * Enable error status
1003 */
1004 sdhci_error_status_enable(host);
1005}