blob: a278deaaa5069cf4e184e95fa42d700916e3564f [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 Kadabie86a40b2014-03-12 17:48:51 -0700380 uint64_t max_trans_retry = (cmd->cmd_timeout ? cmd->cmd_timeout : SDHCI_MAX_TRANS_RETRY);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700381
382 do {
383 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
384 int_status &= SDHCI_INT_STS_CMD_COMPLETE;
385
386 if (int_status == SDHCI_INT_STS_CMD_COMPLETE)
387 break;
388
389 retry++;
390 udelay(500);
391 if (retry == SDHCI_MAX_CMD_RETRY) {
392 dprintf(CRITICAL, "Error: Command never completed\n");
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700393 ret = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700394 goto err;
395 }
396 } while(1);
397
398 /* Command is complete, clear the interrupt bit */
399 REG_WRITE16(host, SDHCI_INT_STS_CMD_COMPLETE, SDHCI_NRML_INT_STS_REG);
400
401 /* Copy the command response,
402 * The valid bits for R2 response are 0-119, & but the actual response
403 * is stored in bits 8-128. We need to move 8 bits of MSB of each
404 * response to register 8 bits of LSB of next response register.
405 * As:
406 * MSB 8 bits of RESP0 --> LSB 8 bits of RESP1
407 * MSB 8 bits of RESP1 --> LSB 8 bits of RESP2
408 * MSB 8 bits of RESP2 --> LSB 8 bits of RESP3
409 */
410 if (cmd->resp_type == SDHCI_CMD_RESP_R2) {
411 for (i = 0; i < 4; i++) {
412 cmd->resp[i] = REG_READ32(host, SDHCI_RESP_REG + (i * 4));
413 cmd->resp[i] <<= SDHCI_RESP_LSHIFT;
414
415 if (i != 0)
416 cmd->resp[i] |= (REG_READ32(host, SDHCI_RESP_REG + ((i-1) * 4)) >> SDHCI_RESP_RSHIFT);
417 }
418 } else
419 cmd->resp[0] = REG_READ32(host, SDHCI_RESP_REG);
420
421 retry = 0;
422
423 /*
424 * Clear the transfer complete interrupt
425 */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700426 if (cmd->data_present || cmd->resp_type == SDHCI_CMD_RESP_R1B) {
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700427 do {
428 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
429 int_status &= SDHCI_INT_STS_TRANS_COMPLETE;
430
431 if (int_status & SDHCI_INT_STS_TRANS_COMPLETE)
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700432 {
433 trans_complete = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700434 break;
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700435 }
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700436
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700437 /*
438 * If we are in tuning then we need to wait until Data timeout , Data end
439 * or Data CRC error
440 */
441 if (host->tuning_in_progress)
442 {
443 err_status = REG_READ16(host, SDHCI_ERR_INT_STS_REG);
444 if ((err_status & SDHCI_DAT_TIMEOUT_MASK) || (err_status & SDHCI_DAT_CRC_MASK))
445 {
446 sdhci_reset(host, (SOFT_RESET_CMD | SOFT_RESET_DATA));
447 return 0;
448 }
449 }
450
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700451 retry++;
452 udelay(1000);
Channagoud Kadabie86a40b2014-03-12 17:48:51 -0700453 if (retry == max_trans_retry) {
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700454 dprintf(CRITICAL, "Error: Transfer never completed\n");
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700455 ret = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700456 goto err;
457 }
458 } while(1);
459
460 /* Transfer is complete, clear the interrupt bit */
461 REG_WRITE16(host, SDHCI_INT_STS_TRANS_COMPLETE, SDHCI_NRML_INT_STS_REG);
462 }
463
464err:
465 /* Look for errors */
466 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700467
468 if (int_status & SDHCI_ERR_INT_STAT_MASK)
469 {
470 /*
471 * As per SDHC spec transfer complete has higher priority than data timeout
472 * If both transfer complete & data timeout are set then we should ignore
473 * data timeout error.
474 * ---------------------------------------------------------------------------
475 * | Transfer complete | Data timeout error | Meaning of the Status |
476 * |--------------------------------------------------------------------------|
477 * | 0 | 0 | Interrupted by another factor |
478 * |--------------------------------------------------------------------------|
479 * | 0 | 1 | Time out occured during transfer|
480 * |--------------------------------------------------------------------------|
481 * | 1 | Don't Care | Command execution complete |
482 * --------------------------------------------------------------------------
483 */
484 if ((REG_READ16(host, SDHCI_ERR_INT_STS_REG) & SDHCI_DAT_TIMEOUT_MASK) && trans_complete)
485 {
486 ret = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700487 }
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700488 else if (sdhci_cmd_err_status(host))
489 {
490 dprintf(CRITICAL, "Error: Command completed with errors\n");
491 ret = 1;
492 }
493 /* Reset Command & Dat lines on error */
494 need_reset = 1;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700495 }
496
497 /* Reset data & command line */
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700498 if (cmd->data_present || need_reset)
Channagoud Kadabi7ad70ea2013-08-08 13:51:04 -0700499 sdhci_reset(host, (SOFT_RESET_CMD | SOFT_RESET_DATA));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700500
Channagoud Kadabi6b649cd2013-09-19 13:19:49 -0700501 return ret;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700502}
503
504/*
505 * Function: sdhci prep desc table
506 * Arg : Pointer data & length
507 * Return : Pointer to desc table
508 * Flow: : Prepare the adma table as per the sd spec v 3.0
509 */
510static struct desc_entry *sdhci_prep_desc_table(void *data, uint32_t len)
511{
512 struct desc_entry *sg_list;
513 uint32_t sg_len = 0;
514 uint32_t remain = 0;
515 uint32_t i;
516 uint32_t table_len = 0;
517
518 if (len <= SDHCI_ADMA_DESC_LINE_SZ) {
519 /* Allocate only one descriptor */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700520 sg_list = (struct desc_entry *) memalign(lcm(4, CACHE_LINE), ROUNDUP(sizeof(struct desc_entry), CACHE_LINE));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700521
522 if (!sg_list) {
523 dprintf(CRITICAL, "Error allocating memory\n");
524 ASSERT(0);
525 }
526
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700527 sg_list[0].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700528 sg_list[0].len = (len < SDHCI_ADMA_DESC_LINE_SZ) ? len : (SDHCI_ADMA_DESC_LINE_SZ & 0xffff);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700529 sg_list[0].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA
530 | SDHCI_ADMA_TRANS_END;
531
532 arch_clean_invalidate_cache_range((addr_t)sg_list, sizeof(struct desc_entry));
533 } else {
534 /* Calculate the number of entries in desc table */
535 sg_len = len / SDHCI_ADMA_DESC_LINE_SZ;
536 remain = len - (sg_len * SDHCI_ADMA_DESC_LINE_SZ);
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700537
538 /* Allocate sg_len + 1 entries if there are remaining bytes at the end */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700539 if (remain)
540 sg_len++;
541
542 table_len = (sg_len * sizeof(struct desc_entry));
543
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700544 sg_list = (struct desc_entry *) memalign(lcm(4, CACHE_LINE), ROUNDUP(table_len, CACHE_LINE));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700545
546 if (!sg_list) {
547 dprintf(CRITICAL, "Error allocating memory\n");
548 ASSERT(0);
549 }
550
551 memset((void *) sg_list, 0, table_len);
552
553 /*
554 * Prepare sglist in the format:
555 * ___________________________________________________
556 * |Transfer Len | Transfer ATTR | Data Address |
557 * | (16 bit) | (16 bit) | (32 bit) |
558 * |_____________|_______________|_____________________|
559 */
560 for (i = 0; i < (sg_len - 1); i++) {
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700561 sg_list[i].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700562 /*
563 * Length attribute is 16 bit value & max transfer size for one
564 * descriptor line is 65536 bytes, As per SD Spec3.0 'len = 0'
565 * implies 65536 bytes. Truncate the length to limit to 16 bit
566 * range.
567 */
568 sg_list[i].len = (SDHCI_ADMA_DESC_LINE_SZ & 0xffff);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700569 sg_list[i].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA;
570 data += SDHCI_ADMA_DESC_LINE_SZ;
571 len -= SDHCI_ADMA_DESC_LINE_SZ;
572 }
573
574 /* Fill the last entry of the table with Valid & End
575 * attributes
576 */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700577 sg_list[sg_len - 1].addr = (uint32_t)data;
Channagoud Kadabi942a8df2013-06-20 14:30:49 -0700578 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 -0700579 sg_list[sg_len - 1].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA |
580 SDHCI_ADMA_TRANS_END;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700581 }
582
583 arch_clean_invalidate_cache_range((addr_t)sg_list, table_len);
584
585 return sg_list;
586}
587
588/*
589 * Function: sdhci adma transfer
590 * Arg : Host structure & command stucture
591 * Return : Pointer to desc table
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700592 * Flow : 1. Prepare descriptor table
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700593 * 2. Write adma register
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700594 * 3. Write block size & block count register
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700595 */
596static struct desc_entry *sdhci_adma_transfer(struct sdhci_host *host,
597 struct mmc_command *cmd)
598{
599 uint32_t num_blks = 0;
600 uint32_t sz;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700601 void *data;
602 struct desc_entry *adma_addr;
603
604
605 num_blks = cmd->data.num_blocks;
606 data = cmd->data.data_ptr;
607
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700608 /*
609 * Some commands send data on DAT lines which is less
610 * than SDHCI_MMC_BLK_SZ, in that case trying to read
611 * more than the data sent by the card results in data
612 * CRC errors. To avoid such errors allow data to pass
613 * the required block size, if the block size is not
614 * passed use the default value
615 */
616 if (cmd->data.blk_sz)
617 sz = num_blks * cmd->data.blk_sz;
618 else
619 sz = num_blks * SDHCI_MMC_BLK_SZ;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700620
621 /* Prepare adma descriptor table */
622 adma_addr = sdhci_prep_desc_table(data, sz);
623
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700624 /* Write adma address to adma register */
625 REG_WRITE32(host, (uint32_t) adma_addr, SDHCI_ADM_ADDR_REG);
626
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700627 /* Write the block size */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700628 if (cmd->data.blk_sz)
629 REG_WRITE16(host, cmd->data.blk_sz, SDHCI_BLKSZ_REG);
630 else
631 REG_WRITE16(host, SDHCI_MMC_BLK_SZ, SDHCI_BLKSZ_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700632
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700633 /*
634 * Set block count in block count register
635 */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700636 REG_WRITE16(host, num_blks, SDHCI_BLK_CNT_REG);
637
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700638 return adma_addr;
639}
640
641/*
642 * Function: sdhci send command
643 * Arg : Host structure & command stucture
644 * Return : 0 on Success, 1 on Failure
645 * Flow: : 1. Prepare the command register
646 * 2. If data is present, prepare adma table
647 * 3. Run the command
648 * 4. Check for command results & take action
649 */
650uint32_t sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
651{
652 uint8_t retry = 0;
653 uint32_t resp_type = 0;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700654 uint16_t trans_mode = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700655 uint16_t present_state;
656 uint32_t flags;
657 struct desc_entry *sg_list = NULL;
658
659 if (cmd->data_present)
660 ASSERT(cmd->data.data_ptr);
661
662 /*
663 * Assert if the data buffer is not aligned to cache
664 * line size for read operations.
665 * For write operations this function assumes that
666 * the cache is already flushed by the caller. As
667 * the data buffer we receive for write operation
668 * may not be aligned to cache boundary due to
669 * certain image formats like sparse image.
670 */
671 if (cmd->trans_mode == SDHCI_READ_MODE)
672 ASSERT(IS_CACHE_LINE_ALIGNED(cmd->data.data_ptr));
673
674 do {
675 present_state = REG_READ32(host, SDHCI_PRESENT_STATE_REG);
676 /* check if CMD & DAT lines are free */
677 present_state &= SDHCI_STATE_CMD_DAT_MASK;
678
679 if (!present_state)
680 break;
681 udelay(1000);
682 retry++;
683 if (retry == 10) {
684 dprintf(CRITICAL, "Error: CMD or DAT lines were never freed\n");
685 return 1;
686 }
687 } while(1);
688
689 switch(cmd->resp_type) {
690 case SDHCI_CMD_RESP_R1:
691 case SDHCI_CMD_RESP_R3:
692 case SDHCI_CMD_RESP_R6:
693 case SDHCI_CMD_RESP_R7:
694 /* Response of length 48 have 32 bits
695 * of response data stored in RESP0[0:31]
696 */
697 resp_type = SDHCI_CMD_RESP_48;
698 break;
699
700 case SDHCI_CMD_RESP_R2:
701 /* Response of length 136 have 120 bits
702 * of response data stored in RESP0[0:119]
703 */
704 resp_type = SDHCI_CMD_RESP_136;
705 break;
706
707 case SDHCI_CMD_RESP_R1B:
708 /* Response of length 48 have 32 bits
709 * of response data stored in RESP0[0:31]
710 * & set CARD_BUSY status if card is busy
711 */
712 resp_type = SDHCI_CMD_RESP_48_BUSY;
713 break;
714
715 case SDHCI_CMD_RESP_NONE:
716 resp_type = SDHCI_CMD_RESP_NONE;
717 break;
718
719 default:
720 dprintf(CRITICAL, "Invalid response type for the command\n");
721 return 1;
722 };
723
724 flags = (resp_type << SDHCI_CMD_RESP_TYPE_SEL_BIT);
725 flags |= (cmd->data_present << SDHCI_CMD_DATA_PRESENT_BIT);
726 flags |= (cmd->cmd_type << SDHCI_CMD_CMD_TYPE_BIT);
727
728 /* Set the timeout value */
729 REG_WRITE8(host, SDHCI_CMD_TIMEOUT, SDHCI_TIMEOUT_REG);
730
731 /* Check if data needs to be processed */
732 if (cmd->data_present)
733 sg_list = sdhci_adma_transfer(host, cmd);
734
735 /* Write the argument 1 */
736 REG_WRITE32(host, cmd->argument, SDHCI_ARGUMENT_REG);
737
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700738 /* Set the Transfer mode */
739 if (cmd->data_present)
740 {
741 /* Enable DMA */
742 trans_mode |= SDHCI_DMA_EN;
743
744 if (cmd->trans_mode == SDHCI_MMC_READ)
745 trans_mode |= SDHCI_READ_MODE;
746
Channagoud Kadabi89902512013-05-14 13:22:06 -0700747 /* Enable auto cmd23 or cmd12 for multi block transfer
748 * based on what command card supports
749 */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700750 if (cmd->data.num_blocks > 1) {
Channagoud Kadabi89902512013-05-14 13:22:06 -0700751 if (cmd->cmd23_support) {
752 trans_mode |= SDHCI_TRANS_MULTI | SDHCI_AUTO_CMD23_EN | SDHCI_BLK_CNT_EN;
753 REG_WRITE32(host, cmd->data.num_blocks, SDHCI_ARG2_REG);
754 }
755 else
756 trans_mode |= SDHCI_TRANS_MULTI | SDHCI_AUTO_CMD12_EN | SDHCI_BLK_CNT_EN;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700757 }
758 }
759
760 /* Write to transfer mode register */
761 REG_WRITE16(host, trans_mode, SDHCI_TRANS_MODE_REG);
762
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700763 /* Write the command register */
764 REG_WRITE16(host, SDHCI_PREP_CMD(cmd->cmd_index, flags), SDHCI_CMD_REG);
765
766 /* Command complete sequence */
767 if (sdhci_cmd_complete(host, cmd))
768 return 1;
769
770 /* Invalidate the cache only for read operations */
771 if (cmd->trans_mode == SDHCI_MMC_READ)
772 arch_invalidate_cache_range((addr_t)cmd->data.data_ptr, (cmd->data.num_blocks * SDHCI_MMC_BLK_SZ));
773
774 /* Free the scatter/gather list */
775 if (sg_list)
776 free(sg_list);
777
778 return 0;
779}
780
781/*
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700782 * Function: sdhci init
783 * Arg : Host structure
784 * Return : None
785 * Flow: : 1. Reset the controller
786 * 2. Read the capabilities register & populate the host
787 * controller capabilities for use by other functions
788 * 3. Enable the power control
789 * 4. Set initial bus width
790 * 5. Set Adma mode
791 * 6. Enable the error status
792 */
793void sdhci_init(struct sdhci_host *host)
794{
795 uint32_t caps[2];
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700796
797 /* Read the capabilities register & store the info */
798 caps[0] = REG_READ32(host, SDHCI_CAPS_REG1);
799 caps[1] = REG_READ32(host, SDHCI_CAPS_REG2);
800
801 host->caps.base_clk_rate = (caps[0] & SDHCI_CLK_RATE_MASK) >> SDHCI_CLK_RATE_BIT;
802 host->caps.base_clk_rate *= 1000000;
803
804 /* Get the max block length for mmc */
805 host->caps.max_blk_len = (caps[0] & SDHCI_BLK_LEN_MASK) >> SDHCI_BLK_LEN_BIT;
806
807 /* 8 bit Bus width */
808 if (caps[0] & SDHCI_8BIT_WIDTH_MASK)
809 host->caps.bus_width_8bit = 1;
810
811 /* Adma support */
812 if (caps[0] & SDHCI_BLK_ADMA_MASK)
813 host->caps.adma_support = 1;
814
815 /* Supported voltage */
816 if (caps[0] & SDHCI_3_3_VOL_MASK)
817 host->caps.voltage = SDHCI_VOL_3_3;
818 else if (caps[0] & SDHCI_3_0_VOL_MASK)
819 host->caps.voltage = SDHCI_VOL_3_0;
820 else if (caps[0] & SDHCI_1_8_VOL_MASK)
821 host->caps.voltage = SDHCI_VOL_1_8;
822
823 /* DDR mode support */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700824 host->caps.ddr_support = (caps[1] & SDHCI_DDR50_MODE_MASK) ? 1 : 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700825
826 /* SDR50 mode support */
827 host->caps.sdr50_support = (caps[1] & SDHCI_SDR50_MODE_MASK) ? 1 : 0;
828
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700829 /* SDR104 mode support */
830 host->caps.sdr104_support = (caps[1] & SDHCI_SDR104_MODE_MASK) ? 1 : 0;
831
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700832 /* Set bus power on */
833 sdhci_set_bus_power_on(host);
834
835 /* Wait for power interrupt to be handled */
Channagoud Kadabi89902512013-05-14 13:22:06 -0700836 event_wait(host->sdhc_event);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700837
838 /* Set bus width */
839 sdhci_set_bus_width(host, SDHCI_BUS_WITDH_1BIT);
840
841 /* Set Adma mode */
842 sdhci_set_adma_mode(host);
843
844 /*
845 * Enable error status
846 */
847 sdhci_error_status_enable(host);
848}