blob: eeb47fc2b9633cab0f98f36156e3cce6a6fd823c [file] [log] [blame]
Channagoud Kadabie9168e82014-01-28 21:33:34 -08001/* Copyright (c) 2013-2014, 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
42/*
Channagoud Kadabi7ad70ea2013-08-08 13:51:04 -070043 * Function: sdhci reset
44 * Arg : Host structure & mask to write to reset register
45 * Return : None
46 * Flow: : Reset the host controller
47 */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -070048void sdhci_reset(struct sdhci_host *host, uint8_t mask)
Channagoud Kadabi7ad70ea2013-08-08 13:51:04 -070049{
50 uint32_t reg;
51 uint32_t timeout = SDHCI_RESET_MAX_TIMEOUT;
52
53 REG_WRITE8(host, mask, SDHCI_RESET_REG);
54
55 /* Wait for the reset to complete */
56 do {
57 reg = REG_READ8(host, SDHCI_RESET_REG);
58 reg &= mask;
59
60 if (!reg)
61 break;
62 if (!timeout)
63 {
64 dprintf(CRITICAL, "Error: sdhci reset failed for: %x\n", mask);
65 break;
66 }
67
68 timeout--;
69 mdelay(1);
70
71 } while(1);
72}
73
74/*
Channagoud Kadabi74ed8352013-03-11 13:12:05 -070075 * Function: sdhci error status enable
76 * Arg : Host structure
77 * Return : None
78 * Flow: : Enable command error status
79 */
80static void sdhci_error_status_enable(struct sdhci_host *host)
81{
82 /* Enable all interrupt status */
83 REG_WRITE16(host, SDHCI_NRML_INT_STS_EN, SDHCI_NRML_INT_STS_EN_REG);
84 REG_WRITE16(host, SDHCI_ERR_INT_STS_EN, SDHCI_ERR_INT_STS_EN_REG);
85 /* Enable all interrupt signal */
86 REG_WRITE16(host, SDHCI_NRML_INT_SIG_EN, SDHCI_NRML_INT_SIG_EN_REG);
87 REG_WRITE16(host, SDHCI_ERR_INT_SIG_EN, SDHCI_ERR_INT_SIG_EN_REG);
88}
89
90/*
91 * Function: sdhci clock supply
92 * Arg : Host structure
93 * Return : 0 on Success, 1 on Failure
94 * Flow: : 1. Calculate the clock divider
95 * 2. Set the clock divider
96 * 3. Check if clock stable
97 * 4. Enable Clock
98 */
99uint32_t sdhci_clk_supply(struct sdhci_host *host, uint32_t clk)
100{
101 uint32_t div = 0;
102 uint32_t freq = 0;
103 uint16_t clk_val = 0;
104
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700105 if (clk >= host->caps.base_clk_rate)
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700106 goto clk_ctrl;
107
108 /* As per the sd spec div should be a multiplier of 2 */
109 for (div = 2; div < SDHCI_CLK_MAX_DIV; div += 2) {
110 freq = host->caps.base_clk_rate / div;
111 if (freq <= clk)
112 break;
113 }
114
115 div >>= 1;
116
117clk_ctrl:
118 /* As per the sdhci spec 3.0, bits 6-7 of the clock
119 * control registers will be mapped to bit 8-9, to
120 * support a 10 bit divider value.
121 * This is needed when the divider value overflows
122 * the 8 bit range.
123 */
124 clk_val = ((div & SDHCI_SDCLK_FREQ_MASK) << SDHCI_SDCLK_FREQ_SEL);
125 clk_val |= ((div & SDHC_SDCLK_UP_BIT_MASK) >> SDHCI_SDCLK_FREQ_SEL)
126 << SDHCI_SDCLK_UP_BIT_SEL;
127
128 clk_val |= SDHCI_INT_CLK_EN;
129 REG_WRITE16(host, clk_val, SDHCI_CLK_CTRL_REG);
130
131 /* Check for clock stable */
132 while (!(REG_READ16(host, SDHCI_CLK_CTRL_REG) & SDHCI_CLK_STABLE));
133
134 /* Now clock is stable, enable it */
135 clk_val = REG_READ16(host, SDHCI_CLK_CTRL_REG);
136 clk_val |= SDHCI_CLK_EN;
137 REG_WRITE16(host, clk_val, SDHCI_CLK_CTRL_REG);
138
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700139 host->cur_clk_rate = clk;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700140
141 return 0;
142}
143
144/*
145 * Function: sdhci stop sdcc clock
146 * Arg : Host structure
147 * Return : 0 on Success, 1 on Failure
148 * Flow: : 1. Stop the clock
149 */
150static uint32_t sdhci_stop_sdcc_clk(struct sdhci_host *host)
151{
152 uint32_t reg;
153
154 reg = REG_READ32(host, SDHCI_PRESENT_STATE_REG);
155
156 if (reg & (SDHCI_CMD_ACT | SDHCI_DAT_ACT)) {
157 dprintf(CRITICAL, "Error: SDCC command & data line are active\n");
158 return 1;
159 }
160
161 REG_WRITE16(host, SDHCI_CLK_DIS, SDHCI_CLK_CTRL_REG);
162
163 return 0;
164}
165
166/*
167 * Function: sdhci change frequency
168 * Arg : Host structure & clock value
169 * Return : 0 on Success, 1 on Failure
170 * Flow: : 1. Stop the clock
171 * 2. Star the clock with new frequency
172 */
173static uint32_t sdhci_change_freq_clk(struct sdhci_host *host, uint32_t clk)
174{
175 if (sdhci_stop_sdcc_clk(host)) {
176 dprintf(CRITICAL, "Error: Card is busy, cannot change frequency\n");
177 return 1;
178 }
179
180 if (sdhci_clk_supply(host, clk)) {
181 dprintf(CRITICAL, "Error: cannot change frequency\n");
182 return 1;
183 }
184
185 return 0;
186}
187
188/*
189 * Function: sdhci set bus power
190 * Arg : Host structure
191 * Return : None
192 * Flow: : 1. Set the voltage
193 * 2. Set the sd power control register
194 */
195static void sdhci_set_bus_power_on(struct sdhci_host *host)
196{
197 uint8_t voltage;
198
199 voltage = host->caps.voltage;
200
201 voltage <<= SDHCI_BUS_VOL_SEL;
Channagoud Kadabi89902512013-05-14 13:22:06 -0700202 REG_WRITE8(host, voltage, SDHCI_PWR_CTRL_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700203
204 voltage |= SDHCI_BUS_PWR_EN;
205
206 REG_WRITE8(host, voltage, SDHCI_PWR_CTRL_REG);
207
208}
209
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700210
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700211/*
212 * Function: sdhci set SDR mode
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700213 * Arg : Host structure, UHS mode
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700214 * Return : None
215 * Flow: : 1. Disable the clock
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700216 * 2. Enable UHS mode
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700217 * 3. Enable the clock
218 * Details : SDR50/SDR104 mode is nothing but HS200
219 * mode SDCC spec refers to it as SDR mode
220 * & emmc spec refers as HS200 mode.
221 */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700222void sdhci_set_uhs_mode(struct sdhci_host *host, uint32_t mode)
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700223{
224 uint16_t clk;
225 uint16_t ctrl = 0;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700226 uint32_t clk_val = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700227
228 /* Disable the clock */
229 clk = REG_READ16(host, SDHCI_CLK_CTRL_REG);
230 clk &= ~SDHCI_CLK_EN;
231 REG_WRITE16(host, clk, SDHCI_CLK_CTRL_REG);
232
233 ctrl = REG_READ16(host, SDHCI_HOST_CTRL2_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700234
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700235 ctrl &= ~SDHCI_UHS_MODE_MASK;
236
237 /* Enable SDR50/SDR104/DDR50 mode */
238 switch (mode)
239 {
240 case SDHCI_SDR104_MODE:
241 ctrl |= SDHCI_SDR104_MODE_EN;
242 clk_val = SDHCI_CLK_200MHZ;
243 break;
244 case SDHCI_SDR50_MODE:
245 ctrl |= SDHCI_SDR50_MODE_EN;
246 clk_val = SDHCI_CLK_100MHZ;
247 break;
248 case SDHCI_DDR50_MODE:
249 ctrl |= SDHCI_DDR50_MODE_EN;
250 clk_val = SDHCI_CLK_50MHZ;
251 break;
252 case SDHCI_SDR25_MODE:
253 ctrl |= SDHCI_SDR25_MODE_EN;
254 clk_val = SDHCI_CLK_50MHZ;
255 break;
256 case SDHCI_SDR12_MODE_EN:
257 ctrl |= SDHCI_SDR12_MODE_EN;
258 clk_val = SDHCI_CLK_25MHZ;
259 break;
260 default:
261 dprintf(CRITICAL, "Error: Invalid UHS mode: %x\n", mode);
262 ASSERT(0);
263 };
264
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700265 REG_WRITE16(host, ctrl, SDHCI_HOST_CTRL2_REG);
266
267 /* Run the clock back */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700268 sdhci_clk_supply(host, clk_val);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700269}
270
271/*
272 * Function: sdhci set adma mode
273 * Arg : Host structure
274 * Return : None
275 * Flow: : Set adma mode
276 */
277static void sdhci_set_adma_mode(struct sdhci_host *host)
278{
279 /* Select 32 Bit ADMA2 type */
280 REG_WRITE8(host, SDHCI_ADMA_32BIT, SDHCI_HOST_CTRL1_REG);
281}
282
283/*
284 * Function: sdhci set bus width
285 * Arg : Host & width
286 * Return : 0 on Sucess, 1 on Failure
287 * Flow: : Set the bus width for controller
288 */
289uint8_t sdhci_set_bus_width(struct sdhci_host *host, uint16_t width)
290{
291 uint16_t reg = 0;
292
293 reg = REG_READ8(host, SDHCI_HOST_CTRL1_REG);
294
295 switch(width) {
296 case DATA_BUS_WIDTH_8BIT:
297 width = SDHCI_BUS_WITDH_8BIT;
298 break;
299 case DATA_BUS_WIDTH_4BIT:
300 width = SDHCI_BUS_WITDH_4BIT;
301 break;
302 case DATA_BUS_WIDTH_1BIT:
303 width = SDHCI_BUS_WITDH_1BIT;
304 break;
305 default:
306 dprintf(CRITICAL, "Bus width is invalid: %u\n", width);
307 return 1;
308 }
309
310 REG_WRITE8(host, (reg | width), SDHCI_HOST_CTRL1_REG);
311
312 return 0;
313}
314
315/*
316 * Function: sdhci command err status
317 * Arg : Host structure
318 * Return : 0 on Sucess, 1 on Failure
319 * Flow: : Look for error status
320 */
321static uint8_t sdhci_cmd_err_status(struct sdhci_host *host)
322{
323 uint32_t err;
324
325 err = REG_READ16(host, SDHCI_ERR_INT_STS_REG);
326
327 if (err & SDHCI_CMD_TIMEOUT_MASK) {
328 dprintf(CRITICAL, "Error: Command timeout error\n");
329 return 1;
330 } else if (err & SDHCI_CMD_CRC_MASK) {
331 dprintf(CRITICAL, "Error: Command CRC error\n");
332 return 1;
333 } else if (err & SDHCI_CMD_END_BIT_MASK) {
334 dprintf(CRITICAL, "Error: CMD end bit error\n");
335 return 1;
336 } else if (err & SDHCI_CMD_IDX_MASK) {
337 dprintf(CRITICAL, "Error: Command Index error\n");
338 return 1;
339 } else if (err & SDHCI_DAT_TIMEOUT_MASK) {
340 dprintf(CRITICAL, "Error: DATA time out error\n");
341 return 1;
342 } else if (err & SDHCI_DAT_CRC_MASK) {
343 dprintf(CRITICAL, "Error: DATA CRC error\n");
344 return 1;
345 } else if (err & SDHCI_DAT_END_BIT_MASK) {
346 dprintf(CRITICAL, "Error: DATA end bit error\n");
347 return 1;
348 } else if (err & SDHCI_CUR_LIM_MASK) {
349 dprintf(CRITICAL, "Error: Current limit error\n");
350 return 1;
351 } else if (err & SDHCI_AUTO_CMD12_MASK) {
352 dprintf(CRITICAL, "Error: Auto CMD12 error\n");
353 return 1;
354 } else if (err & SDHCI_ADMA_MASK) {
355 dprintf(CRITICAL, "Error: ADMA error\n");
356 return 1;
357 }
358
359 return 0;
360}
361
362/*
363 * Function: sdhci command complete
364 * Arg : Host & command structure
365 * Return : 0 on Sucess, 1 on Failure
366 * Flow: : 1. Check for command complete
367 * 2. Check for transfer complete
368 * 3. Get the command response
369 * 4. Check for errors
370 */
371static uint8_t sdhci_cmd_complete(struct sdhci_host *host, struct mmc_command *cmd)
372{
373 uint8_t i;
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700374 uint8_t ret = 0;
375 uint8_t need_reset = 0;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700376 uint32_t retry = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700377 uint32_t int_status;
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700378 uint32_t trans_complete = 0;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700379 uint32_t err_status;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700380
381 do {
382 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
383 int_status &= SDHCI_INT_STS_CMD_COMPLETE;
384
385 if (int_status == SDHCI_INT_STS_CMD_COMPLETE)
386 break;
387
388 retry++;
389 udelay(500);
390 if (retry == SDHCI_MAX_CMD_RETRY) {
391 dprintf(CRITICAL, "Error: Command never completed\n");
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700392 ret = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700393 goto err;
394 }
395 } while(1);
396
397 /* Command is complete, clear the interrupt bit */
398 REG_WRITE16(host, SDHCI_INT_STS_CMD_COMPLETE, SDHCI_NRML_INT_STS_REG);
399
400 /* Copy the command response,
401 * The valid bits for R2 response are 0-119, & but the actual response
402 * is stored in bits 8-128. We need to move 8 bits of MSB of each
403 * response to register 8 bits of LSB of next response register.
404 * As:
405 * MSB 8 bits of RESP0 --> LSB 8 bits of RESP1
406 * MSB 8 bits of RESP1 --> LSB 8 bits of RESP2
407 * MSB 8 bits of RESP2 --> LSB 8 bits of RESP3
408 */
409 if (cmd->resp_type == SDHCI_CMD_RESP_R2) {
410 for (i = 0; i < 4; i++) {
411 cmd->resp[i] = REG_READ32(host, SDHCI_RESP_REG + (i * 4));
412 cmd->resp[i] <<= SDHCI_RESP_LSHIFT;
413
414 if (i != 0)
415 cmd->resp[i] |= (REG_READ32(host, SDHCI_RESP_REG + ((i-1) * 4)) >> SDHCI_RESP_RSHIFT);
416 }
417 } else
418 cmd->resp[0] = REG_READ32(host, SDHCI_RESP_REG);
419
420 retry = 0;
421
422 /*
423 * Clear the transfer complete interrupt
424 */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700425 if (cmd->data_present || cmd->resp_type == SDHCI_CMD_RESP_R1B) {
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700426 do {
427 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
428 int_status &= SDHCI_INT_STS_TRANS_COMPLETE;
429
430 if (int_status & SDHCI_INT_STS_TRANS_COMPLETE)
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700431 {
432 trans_complete = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700433 break;
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700434 }
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700435
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700436 /*
437 * If we are in tuning then we need to wait until Data timeout , Data end
438 * or Data CRC error
439 */
440 if (host->tuning_in_progress)
441 {
442 err_status = REG_READ16(host, SDHCI_ERR_INT_STS_REG);
443 if ((err_status & SDHCI_DAT_TIMEOUT_MASK) || (err_status & SDHCI_DAT_CRC_MASK))
444 {
445 sdhci_reset(host, (SOFT_RESET_CMD | SOFT_RESET_DATA));
446 return 0;
447 }
448 }
449
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700450 retry++;
451 udelay(1000);
452 if (retry == SDHCI_MAX_TRANS_RETRY) {
453 dprintf(CRITICAL, "Error: Transfer never completed\n");
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700454 ret = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700455 goto err;
456 }
457 } while(1);
458
459 /* Transfer is complete, clear the interrupt bit */
460 REG_WRITE16(host, SDHCI_INT_STS_TRANS_COMPLETE, SDHCI_NRML_INT_STS_REG);
461 }
462
463err:
464 /* Look for errors */
465 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700466
467 if (int_status & SDHCI_ERR_INT_STAT_MASK)
468 {
469 /*
470 * As per SDHC spec transfer complete has higher priority than data timeout
471 * If both transfer complete & data timeout are set then we should ignore
472 * data timeout error.
473 * ---------------------------------------------------------------------------
474 * | Transfer complete | Data timeout error | Meaning of the Status |
475 * |--------------------------------------------------------------------------|
476 * | 0 | 0 | Interrupted by another factor |
477 * |--------------------------------------------------------------------------|
478 * | 0 | 1 | Time out occured during transfer|
479 * |--------------------------------------------------------------------------|
480 * | 1 | Don't Care | Command execution complete |
481 * --------------------------------------------------------------------------
482 */
483 if ((REG_READ16(host, SDHCI_ERR_INT_STS_REG) & SDHCI_DAT_TIMEOUT_MASK) && trans_complete)
484 {
485 ret = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700486 }
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700487 else if (sdhci_cmd_err_status(host))
488 {
489 dprintf(CRITICAL, "Error: Command completed with errors\n");
490 ret = 1;
491 }
492 /* Reset Command & Dat lines on error */
493 need_reset = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700494 }
495
496 /* Reset data & command line */
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700497 if (cmd->data_present || need_reset)
Channagoud Kadabi7ad70ea2013-08-08 13:51:04 -0700498 sdhci_reset(host, (SOFT_RESET_CMD | SOFT_RESET_DATA));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700499
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700500 return ret;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700501}
502
503/*
504 * Function: sdhci prep desc table
505 * Arg : Pointer data & length
506 * Return : Pointer to desc table
507 * Flow: : Prepare the adma table as per the sd spec v 3.0
508 */
509static struct desc_entry *sdhci_prep_desc_table(void *data, uint32_t len)
510{
511 struct desc_entry *sg_list;
512 uint32_t sg_len = 0;
513 uint32_t remain = 0;
514 uint32_t i;
515 uint32_t table_len = 0;
516
517 if (len <= SDHCI_ADMA_DESC_LINE_SZ) {
518 /* Allocate only one descriptor */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700519 sg_list = (struct desc_entry *) memalign(lcm(4, CACHE_LINE), ROUNDUP(sizeof(struct desc_entry), CACHE_LINE));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700520
521 if (!sg_list) {
522 dprintf(CRITICAL, "Error allocating memory\n");
523 ASSERT(0);
524 }
525
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700526 sg_list[0].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700527 sg_list[0].len = (len < SDHCI_ADMA_DESC_LINE_SZ) ? len : (SDHCI_ADMA_DESC_LINE_SZ & 0xffff);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700528 sg_list[0].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA
529 | SDHCI_ADMA_TRANS_END;
530
531 arch_clean_invalidate_cache_range((addr_t)sg_list, sizeof(struct desc_entry));
532 } else {
533 /* Calculate the number of entries in desc table */
534 sg_len = len / SDHCI_ADMA_DESC_LINE_SZ;
535 remain = len - (sg_len * SDHCI_ADMA_DESC_LINE_SZ);
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700536
537 /* Allocate sg_len + 1 entries if there are remaining bytes at the end */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700538 if (remain)
539 sg_len++;
540
541 table_len = (sg_len * sizeof(struct desc_entry));
542
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700543 sg_list = (struct desc_entry *) memalign(lcm(4, CACHE_LINE), ROUNDUP(table_len, CACHE_LINE));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700544
545 if (!sg_list) {
546 dprintf(CRITICAL, "Error allocating memory\n");
547 ASSERT(0);
548 }
549
550 memset((void *) sg_list, 0, table_len);
551
552 /*
553 * Prepare sglist in the format:
554 * ___________________________________________________
555 * |Transfer Len | Transfer ATTR | Data Address |
556 * | (16 bit) | (16 bit) | (32 bit) |
557 * |_____________|_______________|_____________________|
558 */
559 for (i = 0; i < (sg_len - 1); i++) {
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700560 sg_list[i].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700561 /*
562 * Length attribute is 16 bit value & max transfer size for one
563 * descriptor line is 65536 bytes, As per SD Spec3.0 'len = 0'
564 * implies 65536 bytes. Truncate the length to limit to 16 bit
565 * range.
566 */
567 sg_list[i].len = (SDHCI_ADMA_DESC_LINE_SZ & 0xffff);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700568 sg_list[i].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA;
569 data += SDHCI_ADMA_DESC_LINE_SZ;
570 len -= SDHCI_ADMA_DESC_LINE_SZ;
571 }
572
573 /* Fill the last entry of the table with Valid & End
574 * attributes
575 */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700576 sg_list[sg_len - 1].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700577 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 -0700578 sg_list[sg_len - 1].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA |
579 SDHCI_ADMA_TRANS_END;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700580 }
581
582 arch_clean_invalidate_cache_range((addr_t)sg_list, table_len);
583
584 return sg_list;
585}
586
587/*
588 * Function: sdhci adma transfer
589 * Arg : Host structure & command stucture
590 * Return : Pointer to desc table
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700591 * Flow : 1. Prepare descriptor table
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700592 * 2. Write adma register
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700593 * 3. Write block size & block count register
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700594 */
595static struct desc_entry *sdhci_adma_transfer(struct sdhci_host *host,
596 struct mmc_command *cmd)
597{
598 uint32_t num_blks = 0;
599 uint32_t sz;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700600 void *data;
601 struct desc_entry *adma_addr;
602
603
604 num_blks = cmd->data.num_blocks;
605 data = cmd->data.data_ptr;
606
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700607 /*
608 * Some commands send data on DAT lines which is less
609 * than SDHCI_MMC_BLK_SZ, in that case trying to read
610 * more than the data sent by the card results in data
611 * CRC errors. To avoid such errors allow data to pass
612 * the required block size, if the block size is not
613 * passed use the default value
614 */
615 if (cmd->data.blk_sz)
616 sz = num_blks * cmd->data.blk_sz;
617 else
618 sz = num_blks * SDHCI_MMC_BLK_SZ;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700619
620 /* Prepare adma descriptor table */
621 adma_addr = sdhci_prep_desc_table(data, sz);
622
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700623 /* Write adma address to adma register */
624 REG_WRITE32(host, (uint32_t) adma_addr, SDHCI_ADM_ADDR_REG);
625
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700626 /* Write the block size */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700627 if (cmd->data.blk_sz)
628 REG_WRITE16(host, cmd->data.blk_sz, SDHCI_BLKSZ_REG);
629 else
630 REG_WRITE16(host, SDHCI_MMC_BLK_SZ, SDHCI_BLKSZ_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700631
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700632 /*
633 * Set block count in block count register
634 */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700635 REG_WRITE16(host, num_blks, SDHCI_BLK_CNT_REG);
636
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700637 return adma_addr;
638}
639
640/*
641 * Function: sdhci send command
642 * Arg : Host structure & command stucture
643 * Return : 0 on Success, 1 on Failure
644 * Flow: : 1. Prepare the command register
645 * 2. If data is present, prepare adma table
646 * 3. Run the command
647 * 4. Check for command results & take action
648 */
649uint32_t sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
650{
651 uint8_t retry = 0;
652 uint32_t resp_type = 0;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700653 uint16_t trans_mode = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700654 uint16_t present_state;
655 uint32_t flags;
656 struct desc_entry *sg_list = NULL;
657
658 if (cmd->data_present)
659 ASSERT(cmd->data.data_ptr);
660
661 /*
662 * Assert if the data buffer is not aligned to cache
663 * line size for read operations.
664 * For write operations this function assumes that
665 * the cache is already flushed by the caller. As
666 * the data buffer we receive for write operation
667 * may not be aligned to cache boundary due to
668 * certain image formats like sparse image.
669 */
670 if (cmd->trans_mode == SDHCI_READ_MODE)
671 ASSERT(IS_CACHE_LINE_ALIGNED(cmd->data.data_ptr));
672
673 do {
674 present_state = REG_READ32(host, SDHCI_PRESENT_STATE_REG);
675 /* check if CMD & DAT lines are free */
676 present_state &= SDHCI_STATE_CMD_DAT_MASK;
677
678 if (!present_state)
679 break;
680 udelay(1000);
681 retry++;
682 if (retry == 10) {
683 dprintf(CRITICAL, "Error: CMD or DAT lines were never freed\n");
684 return 1;
685 }
686 } while(1);
687
688 switch(cmd->resp_type) {
689 case SDHCI_CMD_RESP_R1:
690 case SDHCI_CMD_RESP_R3:
691 case SDHCI_CMD_RESP_R6:
692 case SDHCI_CMD_RESP_R7:
693 /* Response of length 48 have 32 bits
694 * of response data stored in RESP0[0:31]
695 */
696 resp_type = SDHCI_CMD_RESP_48;
697 break;
698
699 case SDHCI_CMD_RESP_R2:
700 /* Response of length 136 have 120 bits
701 * of response data stored in RESP0[0:119]
702 */
703 resp_type = SDHCI_CMD_RESP_136;
704 break;
705
706 case SDHCI_CMD_RESP_R1B:
707 /* Response of length 48 have 32 bits
708 * of response data stored in RESP0[0:31]
709 * & set CARD_BUSY status if card is busy
710 */
711 resp_type = SDHCI_CMD_RESP_48_BUSY;
712 break;
713
714 case SDHCI_CMD_RESP_NONE:
715 resp_type = SDHCI_CMD_RESP_NONE;
716 break;
717
718 default:
719 dprintf(CRITICAL, "Invalid response type for the command\n");
720 return 1;
721 };
722
723 flags = (resp_type << SDHCI_CMD_RESP_TYPE_SEL_BIT);
724 flags |= (cmd->data_present << SDHCI_CMD_DATA_PRESENT_BIT);
725 flags |= (cmd->cmd_type << SDHCI_CMD_CMD_TYPE_BIT);
726
727 /* Set the timeout value */
728 REG_WRITE8(host, SDHCI_CMD_TIMEOUT, SDHCI_TIMEOUT_REG);
729
730 /* Check if data needs to be processed */
731 if (cmd->data_present)
732 sg_list = sdhci_adma_transfer(host, cmd);
733
734 /* Write the argument 1 */
735 REG_WRITE32(host, cmd->argument, SDHCI_ARGUMENT_REG);
736
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700737 /* Set the Transfer mode */
738 if (cmd->data_present)
739 {
740 /* Enable DMA */
741 trans_mode |= SDHCI_DMA_EN;
742
743 if (cmd->trans_mode == SDHCI_MMC_READ)
744 trans_mode |= SDHCI_READ_MODE;
745
Channagoud Kadabi89902512013-05-14 13:22:06 -0700746 /* Enable auto cmd23 or cmd12 for multi block transfer
747 * based on what command card supports
748 */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700749 if (cmd->data.num_blocks > 1) {
Channagoud Kadabi89902512013-05-14 13:22:06 -0700750 if (cmd->cmd23_support) {
751 trans_mode |= SDHCI_TRANS_MULTI | SDHCI_AUTO_CMD23_EN | SDHCI_BLK_CNT_EN;
752 REG_WRITE32(host, cmd->data.num_blocks, SDHCI_ARG2_REG);
753 }
754 else
755 trans_mode |= SDHCI_TRANS_MULTI | SDHCI_AUTO_CMD12_EN | SDHCI_BLK_CNT_EN;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700756 }
757 }
758
759 /* Write to transfer mode register */
760 REG_WRITE16(host, trans_mode, SDHCI_TRANS_MODE_REG);
761
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700762 /* Write the command register */
763 REG_WRITE16(host, SDHCI_PREP_CMD(cmd->cmd_index, flags), SDHCI_CMD_REG);
764
765 /* Command complete sequence */
766 if (sdhci_cmd_complete(host, cmd))
767 return 1;
768
769 /* Invalidate the cache only for read operations */
770 if (cmd->trans_mode == SDHCI_MMC_READ)
771 arch_invalidate_cache_range((addr_t)cmd->data.data_ptr, (cmd->data.num_blocks * SDHCI_MMC_BLK_SZ));
772
773 /* Free the scatter/gather list */
774 if (sg_list)
775 free(sg_list);
776
777 return 0;
778}
779
780/*
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700781 * Function: sdhci init
782 * Arg : Host structure
783 * Return : None
784 * Flow: : 1. Reset the controller
785 * 2. Read the capabilities register & populate the host
786 * controller capabilities for use by other functions
787 * 3. Enable the power control
788 * 4. Set initial bus width
789 * 5. Set Adma mode
790 * 6. Enable the error status
791 */
792void sdhci_init(struct sdhci_host *host)
793{
794 uint32_t caps[2];
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700795
796 /* Read the capabilities register & store the info */
797 caps[0] = REG_READ32(host, SDHCI_CAPS_REG1);
798 caps[1] = REG_READ32(host, SDHCI_CAPS_REG2);
799
800 host->caps.base_clk_rate = (caps[0] & SDHCI_CLK_RATE_MASK) >> SDHCI_CLK_RATE_BIT;
801 host->caps.base_clk_rate *= 1000000;
802
803 /* Get the max block length for mmc */
804 host->caps.max_blk_len = (caps[0] & SDHCI_BLK_LEN_MASK) >> SDHCI_BLK_LEN_BIT;
805
806 /* 8 bit Bus width */
807 if (caps[0] & SDHCI_8BIT_WIDTH_MASK)
808 host->caps.bus_width_8bit = 1;
809
810 /* Adma support */
811 if (caps[0] & SDHCI_BLK_ADMA_MASK)
812 host->caps.adma_support = 1;
813
814 /* Supported voltage */
815 if (caps[0] & SDHCI_3_3_VOL_MASK)
816 host->caps.voltage = SDHCI_VOL_3_3;
817 else if (caps[0] & SDHCI_3_0_VOL_MASK)
818 host->caps.voltage = SDHCI_VOL_3_0;
819 else if (caps[0] & SDHCI_1_8_VOL_MASK)
820 host->caps.voltage = SDHCI_VOL_1_8;
821
822 /* DDR mode support */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700823 host->caps.ddr_support = (caps[1] & SDHCI_DDR50_MODE_MASK) ? 1 : 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700824
825 /* SDR50 mode support */
826 host->caps.sdr50_support = (caps[1] & SDHCI_SDR50_MODE_MASK) ? 1 : 0;
827
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700828 /* SDR104 mode support */
829 host->caps.sdr104_support = (caps[1] & SDHCI_SDR104_MODE_MASK) ? 1 : 0;
830
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700831 /* Set bus power on */
832 sdhci_set_bus_power_on(host);
833
834 /* Wait for power interrupt to be handled */
Channagoud Kadabi89902512013-05-14 13:22:06 -0700835 event_wait(host->sdhc_event);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700836
837 /* Set bus width */
838 sdhci_set_bus_width(host, SDHCI_BUS_WITDH_1BIT);
839
840 /* Set Adma mode */
841 sdhci_set_adma_mode(host);
842
843 /*
844 * Enable error status
845 */
846 sdhci_error_status_enable(host);
847}