blob: 4399c60ceddeb4f05891052c6b931d1b46f5401c [file] [log] [blame]
Channagoud Kadabi74ed8352013-03-11 13:12:05 -07001/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
2 *
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>
40
41
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 */
48static void sdhci_reset(struct sdhci_host *host, uint8_t mask)
49{
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
105 if (clk > host->caps.base_clk_rate) {
106 dprintf(CRITICAL, "Error: Requested clk freq is more than supported\n");
107 return 1;
108 }
109
110 if (clk == host->caps.base_clk_rate)
111 goto clk_ctrl;
112
113 /* As per the sd spec div should be a multiplier of 2 */
114 for (div = 2; div < SDHCI_CLK_MAX_DIV; div += 2) {
115 freq = host->caps.base_clk_rate / div;
116 if (freq <= clk)
117 break;
118 }
119
120 div >>= 1;
121
122clk_ctrl:
123 /* As per the sdhci spec 3.0, bits 6-7 of the clock
124 * control registers will be mapped to bit 8-9, to
125 * support a 10 bit divider value.
126 * This is needed when the divider value overflows
127 * the 8 bit range.
128 */
129 clk_val = ((div & SDHCI_SDCLK_FREQ_MASK) << SDHCI_SDCLK_FREQ_SEL);
130 clk_val |= ((div & SDHC_SDCLK_UP_BIT_MASK) >> SDHCI_SDCLK_FREQ_SEL)
131 << SDHCI_SDCLK_UP_BIT_SEL;
132
133 clk_val |= SDHCI_INT_CLK_EN;
134 REG_WRITE16(host, clk_val, SDHCI_CLK_CTRL_REG);
135
136 /* Check for clock stable */
137 while (!(REG_READ16(host, SDHCI_CLK_CTRL_REG) & SDHCI_CLK_STABLE));
138
139 /* Now clock is stable, enable it */
140 clk_val = REG_READ16(host, SDHCI_CLK_CTRL_REG);
141 clk_val |= SDHCI_CLK_EN;
142 REG_WRITE16(host, clk_val, SDHCI_CLK_CTRL_REG);
143
144 host->cur_clk_rate = freq;
145
146 return 0;
147}
148
149/*
150 * Function: sdhci stop sdcc clock
151 * Arg : Host structure
152 * Return : 0 on Success, 1 on Failure
153 * Flow: : 1. Stop the clock
154 */
155static uint32_t sdhci_stop_sdcc_clk(struct sdhci_host *host)
156{
157 uint32_t reg;
158
159 reg = REG_READ32(host, SDHCI_PRESENT_STATE_REG);
160
161 if (reg & (SDHCI_CMD_ACT | SDHCI_DAT_ACT)) {
162 dprintf(CRITICAL, "Error: SDCC command & data line are active\n");
163 return 1;
164 }
165
166 REG_WRITE16(host, SDHCI_CLK_DIS, SDHCI_CLK_CTRL_REG);
167
168 return 0;
169}
170
171/*
172 * Function: sdhci change frequency
173 * Arg : Host structure & clock value
174 * Return : 0 on Success, 1 on Failure
175 * Flow: : 1. Stop the clock
176 * 2. Star the clock with new frequency
177 */
178static uint32_t sdhci_change_freq_clk(struct sdhci_host *host, uint32_t clk)
179{
180 if (sdhci_stop_sdcc_clk(host)) {
181 dprintf(CRITICAL, "Error: Card is busy, cannot change frequency\n");
182 return 1;
183 }
184
185 if (sdhci_clk_supply(host, clk)) {
186 dprintf(CRITICAL, "Error: cannot change frequency\n");
187 return 1;
188 }
189
190 return 0;
191}
192
193/*
194 * Function: sdhci set bus power
195 * Arg : Host structure
196 * Return : None
197 * Flow: : 1. Set the voltage
198 * 2. Set the sd power control register
199 */
200static void sdhci_set_bus_power_on(struct sdhci_host *host)
201{
202 uint8_t voltage;
203
204 voltage = host->caps.voltage;
205
206 voltage <<= SDHCI_BUS_VOL_SEL;
Channagoud Kadabi89902512013-05-14 13:22:06 -0700207 REG_WRITE8(host, voltage, SDHCI_PWR_CTRL_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700208
209 voltage |= SDHCI_BUS_PWR_EN;
210
211 REG_WRITE8(host, voltage, SDHCI_PWR_CTRL_REG);
212
213}
214
215/*
216 * Function: sdhci set SDR mode
217 * Arg : Host structure
218 * Return : None
219 * Flow: : 1. Disable the clock
220 * 2. Enable sdr mode
221 * 3. Enable the clock
222 * Details : SDR50/SDR104 mode is nothing but HS200
223 * mode SDCC spec refers to it as SDR mode
224 * & emmc spec refers as HS200 mode.
225 */
226void sdhci_set_sdr_mode(struct sdhci_host *host)
227{
228 uint16_t clk;
229 uint16_t ctrl = 0;
230
231 /* Disable the clock */
232 clk = REG_READ16(host, SDHCI_CLK_CTRL_REG);
233 clk &= ~SDHCI_CLK_EN;
234 REG_WRITE16(host, clk, SDHCI_CLK_CTRL_REG);
235
236 /* Enable SDR50 mode:
237 * Right now we support only SDR50 mode which runs at
238 * 100 MHZ sdcc clock, we dont need tuning with SDR50
239 * mode
240 */
241 ctrl = REG_READ16(host, SDHCI_HOST_CTRL2_REG);
242
243 /* Enable SDR50/SDR104 mode based on the controller
244 * capabilities.
245 */
246 if (host->caps.sdr50_support)
247 ctrl |= SDHCI_SDR50_MODE_EN;
248
249 REG_WRITE16(host, ctrl, SDHCI_HOST_CTRL2_REG);
250
251 /* Run the clock back */
252 sdhci_clk_supply(host, SDHCI_CLK_100MHZ);
253}
254
255/*
256 * Function: sdhci set ddr mode
257 * Arg : Host structure
258 * Return : None
259 * Flow: : 1. Disable the clock
260 * 2. Enable DDR mode
261 * 3. Enable the clock
262 */
263void sdhci_set_ddr_mode(struct sdhci_host *host)
264{
265 uint16_t clk;
266 uint16_t ctrl = 0;
267
268 /* Disable the clock */
269 clk = REG_READ16(host, SDHCI_CLK_CTRL_REG);
270 clk &= ~SDHCI_CLK_EN;
271 REG_WRITE16(host, clk, SDHCI_CLK_CTRL_REG);
272
273 ctrl = REG_READ16(host, SDHCI_HOST_CTRL2_REG);
274 ctrl |= SDHCI_DDR_MODE_EN;
275
276 /* Enalbe DDR mode */
277 REG_WRITE16(host, ctrl, SDHCI_HOST_CTRL2_REG);
278
279 /* Run the clock back */
280 sdhci_clk_supply(host, host->cur_clk_rate);
281}
282
283/*
284 * Function: sdhci set adma mode
285 * Arg : Host structure
286 * Return : None
287 * Flow: : Set adma mode
288 */
289static void sdhci_set_adma_mode(struct sdhci_host *host)
290{
291 /* Select 32 Bit ADMA2 type */
292 REG_WRITE8(host, SDHCI_ADMA_32BIT, SDHCI_HOST_CTRL1_REG);
293}
294
295/*
296 * Function: sdhci set bus width
297 * Arg : Host & width
298 * Return : 0 on Sucess, 1 on Failure
299 * Flow: : Set the bus width for controller
300 */
301uint8_t sdhci_set_bus_width(struct sdhci_host *host, uint16_t width)
302{
303 uint16_t reg = 0;
304
305 reg = REG_READ8(host, SDHCI_HOST_CTRL1_REG);
306
307 switch(width) {
308 case DATA_BUS_WIDTH_8BIT:
309 width = SDHCI_BUS_WITDH_8BIT;
310 break;
311 case DATA_BUS_WIDTH_4BIT:
312 width = SDHCI_BUS_WITDH_4BIT;
313 break;
314 case DATA_BUS_WIDTH_1BIT:
315 width = SDHCI_BUS_WITDH_1BIT;
316 break;
317 default:
318 dprintf(CRITICAL, "Bus width is invalid: %u\n", width);
319 return 1;
320 }
321
322 REG_WRITE8(host, (reg | width), SDHCI_HOST_CTRL1_REG);
323
324 return 0;
325}
326
327/*
328 * Function: sdhci command err status
329 * Arg : Host structure
330 * Return : 0 on Sucess, 1 on Failure
331 * Flow: : Look for error status
332 */
333static uint8_t sdhci_cmd_err_status(struct sdhci_host *host)
334{
335 uint32_t err;
336
337 err = REG_READ16(host, SDHCI_ERR_INT_STS_REG);
338
339 if (err & SDHCI_CMD_TIMEOUT_MASK) {
340 dprintf(CRITICAL, "Error: Command timeout error\n");
341 return 1;
342 } else if (err & SDHCI_CMD_CRC_MASK) {
343 dprintf(CRITICAL, "Error: Command CRC error\n");
344 return 1;
345 } else if (err & SDHCI_CMD_END_BIT_MASK) {
346 dprintf(CRITICAL, "Error: CMD end bit error\n");
347 return 1;
348 } else if (err & SDHCI_CMD_IDX_MASK) {
349 dprintf(CRITICAL, "Error: Command Index error\n");
350 return 1;
351 } else if (err & SDHCI_DAT_TIMEOUT_MASK) {
352 dprintf(CRITICAL, "Error: DATA time out error\n");
353 return 1;
354 } else if (err & SDHCI_DAT_CRC_MASK) {
355 dprintf(CRITICAL, "Error: DATA CRC error\n");
356 return 1;
357 } else if (err & SDHCI_DAT_END_BIT_MASK) {
358 dprintf(CRITICAL, "Error: DATA end bit error\n");
359 return 1;
360 } else if (err & SDHCI_CUR_LIM_MASK) {
361 dprintf(CRITICAL, "Error: Current limit error\n");
362 return 1;
363 } else if (err & SDHCI_AUTO_CMD12_MASK) {
364 dprintf(CRITICAL, "Error: Auto CMD12 error\n");
365 return 1;
366 } else if (err & SDHCI_ADMA_MASK) {
367 dprintf(CRITICAL, "Error: ADMA error\n");
368 return 1;
369 }
370
371 return 0;
372}
373
374/*
375 * Function: sdhci command complete
376 * Arg : Host & command structure
377 * Return : 0 on Sucess, 1 on Failure
378 * Flow: : 1. Check for command complete
379 * 2. Check for transfer complete
380 * 3. Get the command response
381 * 4. Check for errors
382 */
383static uint8_t sdhci_cmd_complete(struct sdhci_host *host, struct mmc_command *cmd)
384{
385 uint8_t i;
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700386 uint8_t ret = 0;
387 uint8_t need_reset = 0;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700388 uint32_t retry = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700389 uint32_t int_status;
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700390 uint32_t trans_complete = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700391
392 do {
393 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
394 int_status &= SDHCI_INT_STS_CMD_COMPLETE;
395
396 if (int_status == SDHCI_INT_STS_CMD_COMPLETE)
397 break;
398
399 retry++;
400 udelay(500);
401 if (retry == SDHCI_MAX_CMD_RETRY) {
402 dprintf(CRITICAL, "Error: Command never completed\n");
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700403 ret = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700404 goto err;
405 }
406 } while(1);
407
408 /* Command is complete, clear the interrupt bit */
409 REG_WRITE16(host, SDHCI_INT_STS_CMD_COMPLETE, SDHCI_NRML_INT_STS_REG);
410
411 /* Copy the command response,
412 * The valid bits for R2 response are 0-119, & but the actual response
413 * is stored in bits 8-128. We need to move 8 bits of MSB of each
414 * response to register 8 bits of LSB of next response register.
415 * As:
416 * MSB 8 bits of RESP0 --> LSB 8 bits of RESP1
417 * MSB 8 bits of RESP1 --> LSB 8 bits of RESP2
418 * MSB 8 bits of RESP2 --> LSB 8 bits of RESP3
419 */
420 if (cmd->resp_type == SDHCI_CMD_RESP_R2) {
421 for (i = 0; i < 4; i++) {
422 cmd->resp[i] = REG_READ32(host, SDHCI_RESP_REG + (i * 4));
423 cmd->resp[i] <<= SDHCI_RESP_LSHIFT;
424
425 if (i != 0)
426 cmd->resp[i] |= (REG_READ32(host, SDHCI_RESP_REG + ((i-1) * 4)) >> SDHCI_RESP_RSHIFT);
427 }
428 } else
429 cmd->resp[0] = REG_READ32(host, SDHCI_RESP_REG);
430
431 retry = 0;
432
433 /*
434 * Clear the transfer complete interrupt
435 */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700436 if (cmd->data_present || cmd->resp_type == SDHCI_CMD_RESP_R1B) {
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700437 do {
438 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
439 int_status &= SDHCI_INT_STS_TRANS_COMPLETE;
440
441 if (int_status & SDHCI_INT_STS_TRANS_COMPLETE)
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700442 {
443 trans_complete = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700444 break;
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700445 }
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700446
447 retry++;
448 udelay(1000);
449 if (retry == SDHCI_MAX_TRANS_RETRY) {
450 dprintf(CRITICAL, "Error: Transfer never completed\n");
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700451 ret = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700452 goto err;
453 }
454 } while(1);
455
456 /* Transfer is complete, clear the interrupt bit */
457 REG_WRITE16(host, SDHCI_INT_STS_TRANS_COMPLETE, SDHCI_NRML_INT_STS_REG);
458 }
459
460err:
461 /* Look for errors */
462 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700463
464 if (int_status & SDHCI_ERR_INT_STAT_MASK)
465 {
466 /*
467 * As per SDHC spec transfer complete has higher priority than data timeout
468 * If both transfer complete & data timeout are set then we should ignore
469 * data timeout error.
470 * ---------------------------------------------------------------------------
471 * | Transfer complete | Data timeout error | Meaning of the Status |
472 * |--------------------------------------------------------------------------|
473 * | 0 | 0 | Interrupted by another factor |
474 * |--------------------------------------------------------------------------|
475 * | 0 | 1 | Time out occured during transfer|
476 * |--------------------------------------------------------------------------|
477 * | 1 | Don't Care | Command execution complete |
478 * --------------------------------------------------------------------------
479 */
480 if ((REG_READ16(host, SDHCI_ERR_INT_STS_REG) & SDHCI_DAT_TIMEOUT_MASK) && trans_complete)
481 {
482 ret = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700483 }
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700484 else if (sdhci_cmd_err_status(host))
485 {
486 dprintf(CRITICAL, "Error: Command completed with errors\n");
487 ret = 1;
488 }
489 /* Reset Command & Dat lines on error */
490 need_reset = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700491 }
492
493 /* Reset data & command line */
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700494 if (cmd->data_present || need_reset)
Channagoud Kadabi7ad70ea2013-08-08 13:51:04 -0700495 sdhci_reset(host, (SOFT_RESET_CMD | SOFT_RESET_DATA));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700496
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700497 return ret;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700498}
499
500/*
501 * Function: sdhci prep desc table
502 * Arg : Pointer data & length
503 * Return : Pointer to desc table
504 * Flow: : Prepare the adma table as per the sd spec v 3.0
505 */
506static struct desc_entry *sdhci_prep_desc_table(void *data, uint32_t len)
507{
508 struct desc_entry *sg_list;
509 uint32_t sg_len = 0;
510 uint32_t remain = 0;
511 uint32_t i;
512 uint32_t table_len = 0;
513
514 if (len <= SDHCI_ADMA_DESC_LINE_SZ) {
515 /* Allocate only one descriptor */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700516 sg_list = (struct desc_entry *) memalign(lcm(4, CACHE_LINE), ROUNDUP(sizeof(struct desc_entry), CACHE_LINE));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700517
518 if (!sg_list) {
519 dprintf(CRITICAL, "Error allocating memory\n");
520 ASSERT(0);
521 }
522
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700523 sg_list[0].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700524 sg_list[0].len = (len < SDHCI_ADMA_DESC_LINE_SZ) ? len : (SDHCI_ADMA_DESC_LINE_SZ & 0xffff);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700525 sg_list[0].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA
526 | SDHCI_ADMA_TRANS_END;
527
528 arch_clean_invalidate_cache_range((addr_t)sg_list, sizeof(struct desc_entry));
529 } else {
530 /* Calculate the number of entries in desc table */
531 sg_len = len / SDHCI_ADMA_DESC_LINE_SZ;
532 remain = len - (sg_len * SDHCI_ADMA_DESC_LINE_SZ);
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700533
534 /* Allocate sg_len + 1 entries if there are remaining bytes at the end */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700535 if (remain)
536 sg_len++;
537
538 table_len = (sg_len * sizeof(struct desc_entry));
539
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700540 sg_list = (struct desc_entry *) memalign(lcm(4, CACHE_LINE), ROUNDUP(table_len, CACHE_LINE));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700541
542 if (!sg_list) {
543 dprintf(CRITICAL, "Error allocating memory\n");
544 ASSERT(0);
545 }
546
547 memset((void *) sg_list, 0, table_len);
548
549 /*
550 * Prepare sglist in the format:
551 * ___________________________________________________
552 * |Transfer Len | Transfer ATTR | Data Address |
553 * | (16 bit) | (16 bit) | (32 bit) |
554 * |_____________|_______________|_____________________|
555 */
556 for (i = 0; i < (sg_len - 1); i++) {
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700557 sg_list[i].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700558 /*
559 * Length attribute is 16 bit value & max transfer size for one
560 * descriptor line is 65536 bytes, As per SD Spec3.0 'len = 0'
561 * implies 65536 bytes. Truncate the length to limit to 16 bit
562 * range.
563 */
564 sg_list[i].len = (SDHCI_ADMA_DESC_LINE_SZ & 0xffff);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700565 sg_list[i].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA;
566 data += SDHCI_ADMA_DESC_LINE_SZ;
567 len -= SDHCI_ADMA_DESC_LINE_SZ;
568 }
569
570 /* Fill the last entry of the table with Valid & End
571 * attributes
572 */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700573 sg_list[sg_len - 1].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700574 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 -0700575 sg_list[sg_len - 1].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA |
576 SDHCI_ADMA_TRANS_END;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700577 }
578
579 arch_clean_invalidate_cache_range((addr_t)sg_list, table_len);
580
581 return sg_list;
582}
583
584/*
585 * Function: sdhci adma transfer
586 * Arg : Host structure & command stucture
587 * Return : Pointer to desc table
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700588 * Flow : 1. Prepare descriptor table
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700589 * 2. Write adma register
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700590 * 3. Write block size & block count register
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700591 */
592static struct desc_entry *sdhci_adma_transfer(struct sdhci_host *host,
593 struct mmc_command *cmd)
594{
595 uint32_t num_blks = 0;
596 uint32_t sz;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700597 void *data;
598 struct desc_entry *adma_addr;
599
600
601 num_blks = cmd->data.num_blocks;
602 data = cmd->data.data_ptr;
603
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700604 /*
605 * Some commands send data on DAT lines which is less
606 * than SDHCI_MMC_BLK_SZ, in that case trying to read
607 * more than the data sent by the card results in data
608 * CRC errors. To avoid such errors allow data to pass
609 * the required block size, if the block size is not
610 * passed use the default value
611 */
612 if (cmd->data.blk_sz)
613 sz = num_blks * cmd->data.blk_sz;
614 else
615 sz = num_blks * SDHCI_MMC_BLK_SZ;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700616
617 /* Prepare adma descriptor table */
618 adma_addr = sdhci_prep_desc_table(data, sz);
619
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700620 /* Write adma address to adma register */
621 REG_WRITE32(host, (uint32_t) adma_addr, SDHCI_ADM_ADDR_REG);
622
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700623 /* Write the block size */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700624 if (cmd->data.blk_sz)
625 REG_WRITE16(host, cmd->data.blk_sz, SDHCI_BLKSZ_REG);
626 else
627 REG_WRITE16(host, SDHCI_MMC_BLK_SZ, SDHCI_BLKSZ_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700628
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700629 /*
630 * Set block count in block count register
631 */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700632 REG_WRITE16(host, num_blks, SDHCI_BLK_CNT_REG);
633
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700634 return adma_addr;
635}
636
637/*
638 * Function: sdhci send command
639 * Arg : Host structure & command stucture
640 * Return : 0 on Success, 1 on Failure
641 * Flow: : 1. Prepare the command register
642 * 2. If data is present, prepare adma table
643 * 3. Run the command
644 * 4. Check for command results & take action
645 */
646uint32_t sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
647{
648 uint8_t retry = 0;
649 uint32_t resp_type = 0;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700650 uint16_t trans_mode = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700651 uint16_t present_state;
652 uint32_t flags;
653 struct desc_entry *sg_list = NULL;
654
655 if (cmd->data_present)
656 ASSERT(cmd->data.data_ptr);
657
658 /*
659 * Assert if the data buffer is not aligned to cache
660 * line size for read operations.
661 * For write operations this function assumes that
662 * the cache is already flushed by the caller. As
663 * the data buffer we receive for write operation
664 * may not be aligned to cache boundary due to
665 * certain image formats like sparse image.
666 */
667 if (cmd->trans_mode == SDHCI_READ_MODE)
668 ASSERT(IS_CACHE_LINE_ALIGNED(cmd->data.data_ptr));
669
670 do {
671 present_state = REG_READ32(host, SDHCI_PRESENT_STATE_REG);
672 /* check if CMD & DAT lines are free */
673 present_state &= SDHCI_STATE_CMD_DAT_MASK;
674
675 if (!present_state)
676 break;
677 udelay(1000);
678 retry++;
679 if (retry == 10) {
680 dprintf(CRITICAL, "Error: CMD or DAT lines were never freed\n");
681 return 1;
682 }
683 } while(1);
684
685 switch(cmd->resp_type) {
686 case SDHCI_CMD_RESP_R1:
687 case SDHCI_CMD_RESP_R3:
688 case SDHCI_CMD_RESP_R6:
689 case SDHCI_CMD_RESP_R7:
690 /* Response of length 48 have 32 bits
691 * of response data stored in RESP0[0:31]
692 */
693 resp_type = SDHCI_CMD_RESP_48;
694 break;
695
696 case SDHCI_CMD_RESP_R2:
697 /* Response of length 136 have 120 bits
698 * of response data stored in RESP0[0:119]
699 */
700 resp_type = SDHCI_CMD_RESP_136;
701 break;
702
703 case SDHCI_CMD_RESP_R1B:
704 /* Response of length 48 have 32 bits
705 * of response data stored in RESP0[0:31]
706 * & set CARD_BUSY status if card is busy
707 */
708 resp_type = SDHCI_CMD_RESP_48_BUSY;
709 break;
710
711 case SDHCI_CMD_RESP_NONE:
712 resp_type = SDHCI_CMD_RESP_NONE;
713 break;
714
715 default:
716 dprintf(CRITICAL, "Invalid response type for the command\n");
717 return 1;
718 };
719
720 flags = (resp_type << SDHCI_CMD_RESP_TYPE_SEL_BIT);
721 flags |= (cmd->data_present << SDHCI_CMD_DATA_PRESENT_BIT);
722 flags |= (cmd->cmd_type << SDHCI_CMD_CMD_TYPE_BIT);
723
724 /* Set the timeout value */
725 REG_WRITE8(host, SDHCI_CMD_TIMEOUT, SDHCI_TIMEOUT_REG);
726
727 /* Check if data needs to be processed */
728 if (cmd->data_present)
729 sg_list = sdhci_adma_transfer(host, cmd);
730
731 /* Write the argument 1 */
732 REG_WRITE32(host, cmd->argument, SDHCI_ARGUMENT_REG);
733
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700734 /* Set the Transfer mode */
735 if (cmd->data_present)
736 {
737 /* Enable DMA */
738 trans_mode |= SDHCI_DMA_EN;
739
740 if (cmd->trans_mode == SDHCI_MMC_READ)
741 trans_mode |= SDHCI_READ_MODE;
742
Channagoud Kadabi89902512013-05-14 13:22:06 -0700743 /* Enable auto cmd23 or cmd12 for multi block transfer
744 * based on what command card supports
745 */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700746 if (cmd->data.num_blocks > 1) {
Channagoud Kadabi89902512013-05-14 13:22:06 -0700747 if (cmd->cmd23_support) {
748 trans_mode |= SDHCI_TRANS_MULTI | SDHCI_AUTO_CMD23_EN | SDHCI_BLK_CNT_EN;
749 REG_WRITE32(host, cmd->data.num_blocks, SDHCI_ARG2_REG);
750 }
751 else
752 trans_mode |= SDHCI_TRANS_MULTI | SDHCI_AUTO_CMD12_EN | SDHCI_BLK_CNT_EN;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700753 }
754 }
755
756 /* Write to transfer mode register */
757 REG_WRITE16(host, trans_mode, SDHCI_TRANS_MODE_REG);
758
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700759 /* Write the command register */
760 REG_WRITE16(host, SDHCI_PREP_CMD(cmd->cmd_index, flags), SDHCI_CMD_REG);
761
762 /* Command complete sequence */
763 if (sdhci_cmd_complete(host, cmd))
764 return 1;
765
766 /* Invalidate the cache only for read operations */
767 if (cmd->trans_mode == SDHCI_MMC_READ)
768 arch_invalidate_cache_range((addr_t)cmd->data.data_ptr, (cmd->data.num_blocks * SDHCI_MMC_BLK_SZ));
769
770 /* Free the scatter/gather list */
771 if (sg_list)
772 free(sg_list);
773
774 return 0;
775}
776
777/*
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700778 * Function: sdhci init
779 * Arg : Host structure
780 * Return : None
781 * Flow: : 1. Reset the controller
782 * 2. Read the capabilities register & populate the host
783 * controller capabilities for use by other functions
784 * 3. Enable the power control
785 * 4. Set initial bus width
786 * 5. Set Adma mode
787 * 6. Enable the error status
788 */
789void sdhci_init(struct sdhci_host *host)
790{
791 uint32_t caps[2];
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700792
793 /*
794 * Reset the controller
795 */
Channagoud Kadabi7ad70ea2013-08-08 13:51:04 -0700796 sdhci_reset(host, SDHCI_SOFT_RESET);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700797
798 /* Read the capabilities register & store the info */
799 caps[0] = REG_READ32(host, SDHCI_CAPS_REG1);
800 caps[1] = REG_READ32(host, SDHCI_CAPS_REG2);
801
802 host->caps.base_clk_rate = (caps[0] & SDHCI_CLK_RATE_MASK) >> SDHCI_CLK_RATE_BIT;
803 host->caps.base_clk_rate *= 1000000;
804
805 /* Get the max block length for mmc */
806 host->caps.max_blk_len = (caps[0] & SDHCI_BLK_LEN_MASK) >> SDHCI_BLK_LEN_BIT;
807
808 /* 8 bit Bus width */
809 if (caps[0] & SDHCI_8BIT_WIDTH_MASK)
810 host->caps.bus_width_8bit = 1;
811
812 /* Adma support */
813 if (caps[0] & SDHCI_BLK_ADMA_MASK)
814 host->caps.adma_support = 1;
815
816 /* Supported voltage */
817 if (caps[0] & SDHCI_3_3_VOL_MASK)
818 host->caps.voltage = SDHCI_VOL_3_3;
819 else if (caps[0] & SDHCI_3_0_VOL_MASK)
820 host->caps.voltage = SDHCI_VOL_3_0;
821 else if (caps[0] & SDHCI_1_8_VOL_MASK)
822 host->caps.voltage = SDHCI_VOL_1_8;
823
824 /* DDR mode support */
825 host->caps.ddr_support = (caps[1] & SDHCI_DDR_MODE_MASK) ? 1 : 0;
826
827 /* SDR50 mode support */
828 host->caps.sdr50_support = (caps[1] & SDHCI_SDR50_MODE_MASK) ? 1 : 0;
829
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700830 /* Set bus power on */
831 sdhci_set_bus_power_on(host);
832
833 /* Wait for power interrupt to be handled */
Channagoud Kadabi89902512013-05-14 13:22:06 -0700834 event_wait(host->sdhc_event);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700835
836 /* Set bus width */
837 sdhci_set_bus_width(host, SDHCI_BUS_WITDH_1BIT);
838
839 /* Set Adma mode */
840 sdhci_set_adma_mode(host);
841
842 /*
843 * Enable error status
844 */
845 sdhci_error_status_enable(host);
846}