blob: 21e309752ed9906da9c24488094a44ac0fe6f76b [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/*
43 * Function: sdhci int handler
44 * Arg : Event argument
45 * Return : 0
46 * Flow: : 1. Read the power control mask register
47 * 2. Check if bus is ON
48 * 3. Write success to ack regiser
49 * Details : This is power control interrupt handler.
50 * Once we receive the interrupt, we will ack the power control
51 * register that we have successfully completed pmic transactions
52 */
53enum handler_return sdhci_int_handler(void *arg)
54{
55 uint32_t ack;
56 uint32_t status;
57
58 /*
59 * Read the mask register to check if BUS & IO level
60 * interrupts are enabled
61 */
62 status = readl(SDCC_HC_PWRCTL_MASK_REG);
63
64 if (status & (SDCC_HC_BUS_ON | SDCC_HC_BUS_OFF))
65 ack = SDCC_HC_BUS_ON_OFF_SUCC;
66 if (status & (SDCC_HC_IO_SIG_LOW | SDCC_HC_IO_SIG_HIGH))
67 ack |= SDCC_HC_IO_SIG_SUCC;
68
69 /* Write success to power control register */
70 writel(ack, SDCC_HC_PWRCTL_CTL_REG);
71
72 event_signal((event_t *)arg, false);
73
74 return 0;
75}
76
77/*
78 * Function: sdhci error status enable
79 * Arg : Host structure
80 * Return : None
81 * Flow: : Enable command error status
82 */
83static void sdhci_error_status_enable(struct sdhci_host *host)
84{
85 /* Enable all interrupt status */
86 REG_WRITE16(host, SDHCI_NRML_INT_STS_EN, SDHCI_NRML_INT_STS_EN_REG);
87 REG_WRITE16(host, SDHCI_ERR_INT_STS_EN, SDHCI_ERR_INT_STS_EN_REG);
88 /* Enable all interrupt signal */
89 REG_WRITE16(host, SDHCI_NRML_INT_SIG_EN, SDHCI_NRML_INT_SIG_EN_REG);
90 REG_WRITE16(host, SDHCI_ERR_INT_SIG_EN, SDHCI_ERR_INT_SIG_EN_REG);
91}
92
93/*
94 * Function: sdhci clock supply
95 * Arg : Host structure
96 * Return : 0 on Success, 1 on Failure
97 * Flow: : 1. Calculate the clock divider
98 * 2. Set the clock divider
99 * 3. Check if clock stable
100 * 4. Enable Clock
101 */
102uint32_t sdhci_clk_supply(struct sdhci_host *host, uint32_t clk)
103{
104 uint32_t div = 0;
105 uint32_t freq = 0;
106 uint16_t clk_val = 0;
107
108 if (clk > host->caps.base_clk_rate) {
109 dprintf(CRITICAL, "Error: Requested clk freq is more than supported\n");
110 return 1;
111 }
112
113 if (clk == host->caps.base_clk_rate)
114 goto clk_ctrl;
115
116 /* As per the sd spec div should be a multiplier of 2 */
117 for (div = 2; div < SDHCI_CLK_MAX_DIV; div += 2) {
118 freq = host->caps.base_clk_rate / div;
119 if (freq <= clk)
120 break;
121 }
122
123 div >>= 1;
124
125clk_ctrl:
126 /* As per the sdhci spec 3.0, bits 6-7 of the clock
127 * control registers will be mapped to bit 8-9, to
128 * support a 10 bit divider value.
129 * This is needed when the divider value overflows
130 * the 8 bit range.
131 */
132 clk_val = ((div & SDHCI_SDCLK_FREQ_MASK) << SDHCI_SDCLK_FREQ_SEL);
133 clk_val |= ((div & SDHC_SDCLK_UP_BIT_MASK) >> SDHCI_SDCLK_FREQ_SEL)
134 << SDHCI_SDCLK_UP_BIT_SEL;
135
136 clk_val |= SDHCI_INT_CLK_EN;
137 REG_WRITE16(host, clk_val, SDHCI_CLK_CTRL_REG);
138
139 /* Check for clock stable */
140 while (!(REG_READ16(host, SDHCI_CLK_CTRL_REG) & SDHCI_CLK_STABLE));
141
142 /* Now clock is stable, enable it */
143 clk_val = REG_READ16(host, SDHCI_CLK_CTRL_REG);
144 clk_val |= SDHCI_CLK_EN;
145 REG_WRITE16(host, clk_val, SDHCI_CLK_CTRL_REG);
146
147 host->cur_clk_rate = freq;
148
149 return 0;
150}
151
152/*
153 * Function: sdhci stop sdcc clock
154 * Arg : Host structure
155 * Return : 0 on Success, 1 on Failure
156 * Flow: : 1. Stop the clock
157 */
158static uint32_t sdhci_stop_sdcc_clk(struct sdhci_host *host)
159{
160 uint32_t reg;
161
162 reg = REG_READ32(host, SDHCI_PRESENT_STATE_REG);
163
164 if (reg & (SDHCI_CMD_ACT | SDHCI_DAT_ACT)) {
165 dprintf(CRITICAL, "Error: SDCC command & data line are active\n");
166 return 1;
167 }
168
169 REG_WRITE16(host, SDHCI_CLK_DIS, SDHCI_CLK_CTRL_REG);
170
171 return 0;
172}
173
174/*
175 * Function: sdhci change frequency
176 * Arg : Host structure & clock value
177 * Return : 0 on Success, 1 on Failure
178 * Flow: : 1. Stop the clock
179 * 2. Star the clock with new frequency
180 */
181static uint32_t sdhci_change_freq_clk(struct sdhci_host *host, uint32_t clk)
182{
183 if (sdhci_stop_sdcc_clk(host)) {
184 dprintf(CRITICAL, "Error: Card is busy, cannot change frequency\n");
185 return 1;
186 }
187
188 if (sdhci_clk_supply(host, clk)) {
189 dprintf(CRITICAL, "Error: cannot change frequency\n");
190 return 1;
191 }
192
193 return 0;
194}
195
196/*
197 * Function: sdhci set bus power
198 * Arg : Host structure
199 * Return : None
200 * Flow: : 1. Set the voltage
201 * 2. Set the sd power control register
202 */
203static void sdhci_set_bus_power_on(struct sdhci_host *host)
204{
205 uint8_t voltage;
206
207 voltage = host->caps.voltage;
208
209 voltage <<= SDHCI_BUS_VOL_SEL;
210 REG_WRITE8(host, voltage, SDHCI_BUS_PWR_EN);
211
212 voltage |= SDHCI_BUS_PWR_EN;
213
214 REG_WRITE8(host, voltage, SDHCI_PWR_CTRL_REG);
215
216}
217
218/*
219 * Function: sdhci set SDR mode
220 * Arg : Host structure
221 * Return : None
222 * Flow: : 1. Disable the clock
223 * 2. Enable sdr mode
224 * 3. Enable the clock
225 * Details : SDR50/SDR104 mode is nothing but HS200
226 * mode SDCC spec refers to it as SDR mode
227 * & emmc spec refers as HS200 mode.
228 */
229void sdhci_set_sdr_mode(struct sdhci_host *host)
230{
231 uint16_t clk;
232 uint16_t ctrl = 0;
233
234 /* Disable the clock */
235 clk = REG_READ16(host, SDHCI_CLK_CTRL_REG);
236 clk &= ~SDHCI_CLK_EN;
237 REG_WRITE16(host, clk, SDHCI_CLK_CTRL_REG);
238
239 /* Enable SDR50 mode:
240 * Right now we support only SDR50 mode which runs at
241 * 100 MHZ sdcc clock, we dont need tuning with SDR50
242 * mode
243 */
244 ctrl = REG_READ16(host, SDHCI_HOST_CTRL2_REG);
245
246 /* Enable SDR50/SDR104 mode based on the controller
247 * capabilities.
248 */
249 if (host->caps.sdr50_support)
250 ctrl |= SDHCI_SDR50_MODE_EN;
251
252 REG_WRITE16(host, ctrl, SDHCI_HOST_CTRL2_REG);
253
254 /* Run the clock back */
255 sdhci_clk_supply(host, SDHCI_CLK_100MHZ);
256}
257
258/*
259 * Function: sdhci set ddr mode
260 * Arg : Host structure
261 * Return : None
262 * Flow: : 1. Disable the clock
263 * 2. Enable DDR mode
264 * 3. Enable the clock
265 */
266void sdhci_set_ddr_mode(struct sdhci_host *host)
267{
268 uint16_t clk;
269 uint16_t ctrl = 0;
270
271 /* Disable the clock */
272 clk = REG_READ16(host, SDHCI_CLK_CTRL_REG);
273 clk &= ~SDHCI_CLK_EN;
274 REG_WRITE16(host, clk, SDHCI_CLK_CTRL_REG);
275
276 ctrl = REG_READ16(host, SDHCI_HOST_CTRL2_REG);
277 ctrl |= SDHCI_DDR_MODE_EN;
278
279 /* Enalbe DDR mode */
280 REG_WRITE16(host, ctrl, SDHCI_HOST_CTRL2_REG);
281
282 /* Run the clock back */
283 sdhci_clk_supply(host, host->cur_clk_rate);
284}
285
286/*
287 * Function: sdhci set adma mode
288 * Arg : Host structure
289 * Return : None
290 * Flow: : Set adma mode
291 */
292static void sdhci_set_adma_mode(struct sdhci_host *host)
293{
294 /* Select 32 Bit ADMA2 type */
295 REG_WRITE8(host, SDHCI_ADMA_32BIT, SDHCI_HOST_CTRL1_REG);
296}
297
298/*
299 * Function: sdhci set bus width
300 * Arg : Host & width
301 * Return : 0 on Sucess, 1 on Failure
302 * Flow: : Set the bus width for controller
303 */
304uint8_t sdhci_set_bus_width(struct sdhci_host *host, uint16_t width)
305{
306 uint16_t reg = 0;
307
308 reg = REG_READ8(host, SDHCI_HOST_CTRL1_REG);
309
310 switch(width) {
311 case DATA_BUS_WIDTH_8BIT:
312 width = SDHCI_BUS_WITDH_8BIT;
313 break;
314 case DATA_BUS_WIDTH_4BIT:
315 width = SDHCI_BUS_WITDH_4BIT;
316 break;
317 case DATA_BUS_WIDTH_1BIT:
318 width = SDHCI_BUS_WITDH_1BIT;
319 break;
320 default:
321 dprintf(CRITICAL, "Bus width is invalid: %u\n", width);
322 return 1;
323 }
324
325 REG_WRITE8(host, (reg | width), SDHCI_HOST_CTRL1_REG);
326
327 return 0;
328}
329
330/*
331 * Function: sdhci command err status
332 * Arg : Host structure
333 * Return : 0 on Sucess, 1 on Failure
334 * Flow: : Look for error status
335 */
336static uint8_t sdhci_cmd_err_status(struct sdhci_host *host)
337{
338 uint32_t err;
339
340 err = REG_READ16(host, SDHCI_ERR_INT_STS_REG);
341
342 if (err & SDHCI_CMD_TIMEOUT_MASK) {
343 dprintf(CRITICAL, "Error: Command timeout error\n");
344 return 1;
345 } else if (err & SDHCI_CMD_CRC_MASK) {
346 dprintf(CRITICAL, "Error: Command CRC error\n");
347 return 1;
348 } else if (err & SDHCI_CMD_END_BIT_MASK) {
349 dprintf(CRITICAL, "Error: CMD end bit error\n");
350 return 1;
351 } else if (err & SDHCI_CMD_IDX_MASK) {
352 dprintf(CRITICAL, "Error: Command Index error\n");
353 return 1;
354 } else if (err & SDHCI_DAT_TIMEOUT_MASK) {
355 dprintf(CRITICAL, "Error: DATA time out error\n");
356 return 1;
357 } else if (err & SDHCI_DAT_CRC_MASK) {
358 dprintf(CRITICAL, "Error: DATA CRC error\n");
359 return 1;
360 } else if (err & SDHCI_DAT_END_BIT_MASK) {
361 dprintf(CRITICAL, "Error: DATA end bit error\n");
362 return 1;
363 } else if (err & SDHCI_CUR_LIM_MASK) {
364 dprintf(CRITICAL, "Error: Current limit error\n");
365 return 1;
366 } else if (err & SDHCI_AUTO_CMD12_MASK) {
367 dprintf(CRITICAL, "Error: Auto CMD12 error\n");
368 return 1;
369 } else if (err & SDHCI_ADMA_MASK) {
370 dprintf(CRITICAL, "Error: ADMA error\n");
371 return 1;
372 }
373
374 return 0;
375}
376
377/*
378 * Function: sdhci command complete
379 * Arg : Host & command structure
380 * Return : 0 on Sucess, 1 on Failure
381 * Flow: : 1. Check for command complete
382 * 2. Check for transfer complete
383 * 3. Get the command response
384 * 4. Check for errors
385 */
386static uint8_t sdhci_cmd_complete(struct sdhci_host *host, struct mmc_command *cmd)
387{
388 uint8_t i;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700389 uint32_t retry = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700390 uint32_t int_status;
391
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");
403 goto err;
404 }
405 } while(1);
406
407 /* Command is complete, clear the interrupt bit */
408 REG_WRITE16(host, SDHCI_INT_STS_CMD_COMPLETE, SDHCI_NRML_INT_STS_REG);
409
410 /* Copy the command response,
411 * The valid bits for R2 response are 0-119, & but the actual response
412 * is stored in bits 8-128. We need to move 8 bits of MSB of each
413 * response to register 8 bits of LSB of next response register.
414 * As:
415 * MSB 8 bits of RESP0 --> LSB 8 bits of RESP1
416 * MSB 8 bits of RESP1 --> LSB 8 bits of RESP2
417 * MSB 8 bits of RESP2 --> LSB 8 bits of RESP3
418 */
419 if (cmd->resp_type == SDHCI_CMD_RESP_R2) {
420 for (i = 0; i < 4; i++) {
421 cmd->resp[i] = REG_READ32(host, SDHCI_RESP_REG + (i * 4));
422 cmd->resp[i] <<= SDHCI_RESP_LSHIFT;
423
424 if (i != 0)
425 cmd->resp[i] |= (REG_READ32(host, SDHCI_RESP_REG + ((i-1) * 4)) >> SDHCI_RESP_RSHIFT);
426 }
427 } else
428 cmd->resp[0] = REG_READ32(host, SDHCI_RESP_REG);
429
430 retry = 0;
431
432 /*
433 * Clear the transfer complete interrupt
434 */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700435 if (cmd->data_present || cmd->resp_type == SDHCI_CMD_RESP_R1B) {
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700436 do {
437 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
438 int_status &= SDHCI_INT_STS_TRANS_COMPLETE;
439
440 if (int_status & SDHCI_INT_STS_TRANS_COMPLETE)
441 break;
442
443 retry++;
444 udelay(1000);
445 if (retry == SDHCI_MAX_TRANS_RETRY) {
446 dprintf(CRITICAL, "Error: Transfer never completed\n");
447 goto err;
448 }
449 } while(1);
450
451 /* Transfer is complete, clear the interrupt bit */
452 REG_WRITE16(host, SDHCI_INT_STS_TRANS_COMPLETE, SDHCI_NRML_INT_STS_REG);
453 }
454
455err:
456 /* Look for errors */
457 int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
458 if (int_status & SDHCI_ERR_INT_STAT_MASK) {
459 if (sdhci_cmd_err_status(host)) {
460 dprintf(CRITICAL, "Error: Command completed with errors\n");
461 return 1;
462 }
463 }
464
465 /* Reset data & command line */
466 if (cmd->data_present)
467 REG_WRITE8(host, (SOFT_RESET_CMD | SOFT_RESET_DATA), SDHCI_RESET_REG);
468
469 return 0;
470}
471
472/*
473 * Function: sdhci prep desc table
474 * Arg : Pointer data & length
475 * Return : Pointer to desc table
476 * Flow: : Prepare the adma table as per the sd spec v 3.0
477 */
478static struct desc_entry *sdhci_prep_desc_table(void *data, uint32_t len)
479{
480 struct desc_entry *sg_list;
481 uint32_t sg_len = 0;
482 uint32_t remain = 0;
483 uint32_t i;
484 uint32_t table_len = 0;
485
486 if (len <= SDHCI_ADMA_DESC_LINE_SZ) {
487 /* Allocate only one descriptor */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700488 sg_list = (struct desc_entry *) memalign(lcm(4, CACHE_LINE), ROUNDUP(sizeof(struct desc_entry), CACHE_LINE));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700489
490 if (!sg_list) {
491 dprintf(CRITICAL, "Error allocating memory\n");
492 ASSERT(0);
493 }
494
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700495 sg_list[0].addr = (uint32_t)data;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700496 sg_list[0].len = len;
497 sg_list[0].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA
498 | SDHCI_ADMA_TRANS_END;
499
500 arch_clean_invalidate_cache_range((addr_t)sg_list, sizeof(struct desc_entry));
501 } else {
502 /* Calculate the number of entries in desc table */
503 sg_len = len / SDHCI_ADMA_DESC_LINE_SZ;
504 remain = len - (sg_len * SDHCI_ADMA_DESC_LINE_SZ);
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700505
506 /* Allocate sg_len + 1 entries if there are remaining bytes at the end */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700507 if (remain)
508 sg_len++;
509
510 table_len = (sg_len * sizeof(struct desc_entry));
511
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700512 sg_list = (struct desc_entry *) memalign(lcm(4, CACHE_LINE), ROUNDUP(table_len, CACHE_LINE));
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700513
514 if (!sg_list) {
515 dprintf(CRITICAL, "Error allocating memory\n");
516 ASSERT(0);
517 }
518
519 memset((void *) sg_list, 0, table_len);
520
521 /*
522 * Prepare sglist in the format:
523 * ___________________________________________________
524 * |Transfer Len | Transfer ATTR | Data Address |
525 * | (16 bit) | (16 bit) | (32 bit) |
526 * |_____________|_______________|_____________________|
527 */
528 for (i = 0; i < (sg_len - 1); i++) {
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700529 sg_list[i].addr = (uint32_t)data;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700530 sg_list[i].len = SDHCI_ADMA_DESC_LINE_SZ;
531 sg_list[i].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA;
532 data += SDHCI_ADMA_DESC_LINE_SZ;
533 len -= SDHCI_ADMA_DESC_LINE_SZ;
534 }
535
536 /* Fill the last entry of the table with Valid & End
537 * attributes
538 */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700539 sg_list[sg_len - 1].addr = (uint32_t)data;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700540 sg_list[sg_len - 1].len = len;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700541 sg_list[sg_len - 1].tran_att = SDHCI_ADMA_TRANS_VALID | SDHCI_ADMA_TRANS_DATA |
542 SDHCI_ADMA_TRANS_END;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700543 }
544
545 arch_clean_invalidate_cache_range((addr_t)sg_list, table_len);
546
547 return sg_list;
548}
549
550/*
551 * Function: sdhci adma transfer
552 * Arg : Host structure & command stucture
553 * Return : Pointer to desc table
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700554 * Flow : 1. Prepare descriptor table
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700555 * 2. Write adma register
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700556 * 3. Write block size & block count register
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700557 */
558static struct desc_entry *sdhci_adma_transfer(struct sdhci_host *host,
559 struct mmc_command *cmd)
560{
561 uint32_t num_blks = 0;
562 uint32_t sz;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700563 void *data;
564 struct desc_entry *adma_addr;
565
566
567 num_blks = cmd->data.num_blocks;
568 data = cmd->data.data_ptr;
569
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700570 /*
571 * Some commands send data on DAT lines which is less
572 * than SDHCI_MMC_BLK_SZ, in that case trying to read
573 * more than the data sent by the card results in data
574 * CRC errors. To avoid such errors allow data to pass
575 * the required block size, if the block size is not
576 * passed use the default value
577 */
578 if (cmd->data.blk_sz)
579 sz = num_blks * cmd->data.blk_sz;
580 else
581 sz = num_blks * SDHCI_MMC_BLK_SZ;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700582
583 /* Prepare adma descriptor table */
584 adma_addr = sdhci_prep_desc_table(data, sz);
585
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700586 /* Write adma address to adma register */
587 REG_WRITE32(host, (uint32_t) adma_addr, SDHCI_ADM_ADDR_REG);
588
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700589 /* Write the block size */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -0700590 if (cmd->data.blk_sz)
591 REG_WRITE16(host, cmd->data.blk_sz, SDHCI_BLKSZ_REG);
592 else
593 REG_WRITE16(host, SDHCI_MMC_BLK_SZ, SDHCI_BLKSZ_REG);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700594
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700595 /*
596 * Set block count in block count register
597 */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700598 REG_WRITE16(host, num_blks, SDHCI_BLK_CNT_REG);
599
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700600 return adma_addr;
601}
602
603/*
604 * Function: sdhci send command
605 * Arg : Host structure & command stucture
606 * Return : 0 on Success, 1 on Failure
607 * Flow: : 1. Prepare the command register
608 * 2. If data is present, prepare adma table
609 * 3. Run the command
610 * 4. Check for command results & take action
611 */
612uint32_t sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
613{
614 uint8_t retry = 0;
615 uint32_t resp_type = 0;
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700616 uint16_t trans_mode = 0;
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700617 uint16_t present_state;
618 uint32_t flags;
619 struct desc_entry *sg_list = NULL;
620
621 if (cmd->data_present)
622 ASSERT(cmd->data.data_ptr);
623
624 /*
625 * Assert if the data buffer is not aligned to cache
626 * line size for read operations.
627 * For write operations this function assumes that
628 * the cache is already flushed by the caller. As
629 * the data buffer we receive for write operation
630 * may not be aligned to cache boundary due to
631 * certain image formats like sparse image.
632 */
633 if (cmd->trans_mode == SDHCI_READ_MODE)
634 ASSERT(IS_CACHE_LINE_ALIGNED(cmd->data.data_ptr));
635
636 do {
637 present_state = REG_READ32(host, SDHCI_PRESENT_STATE_REG);
638 /* check if CMD & DAT lines are free */
639 present_state &= SDHCI_STATE_CMD_DAT_MASK;
640
641 if (!present_state)
642 break;
643 udelay(1000);
644 retry++;
645 if (retry == 10) {
646 dprintf(CRITICAL, "Error: CMD or DAT lines were never freed\n");
647 return 1;
648 }
649 } while(1);
650
651 switch(cmd->resp_type) {
652 case SDHCI_CMD_RESP_R1:
653 case SDHCI_CMD_RESP_R3:
654 case SDHCI_CMD_RESP_R6:
655 case SDHCI_CMD_RESP_R7:
656 /* Response of length 48 have 32 bits
657 * of response data stored in RESP0[0:31]
658 */
659 resp_type = SDHCI_CMD_RESP_48;
660 break;
661
662 case SDHCI_CMD_RESP_R2:
663 /* Response of length 136 have 120 bits
664 * of response data stored in RESP0[0:119]
665 */
666 resp_type = SDHCI_CMD_RESP_136;
667 break;
668
669 case SDHCI_CMD_RESP_R1B:
670 /* Response of length 48 have 32 bits
671 * of response data stored in RESP0[0:31]
672 * & set CARD_BUSY status if card is busy
673 */
674 resp_type = SDHCI_CMD_RESP_48_BUSY;
675 break;
676
677 case SDHCI_CMD_RESP_NONE:
678 resp_type = SDHCI_CMD_RESP_NONE;
679 break;
680
681 default:
682 dprintf(CRITICAL, "Invalid response type for the command\n");
683 return 1;
684 };
685
686 flags = (resp_type << SDHCI_CMD_RESP_TYPE_SEL_BIT);
687 flags |= (cmd->data_present << SDHCI_CMD_DATA_PRESENT_BIT);
688 flags |= (cmd->cmd_type << SDHCI_CMD_CMD_TYPE_BIT);
689
690 /* Set the timeout value */
691 REG_WRITE8(host, SDHCI_CMD_TIMEOUT, SDHCI_TIMEOUT_REG);
692
693 /* Check if data needs to be processed */
694 if (cmd->data_present)
695 sg_list = sdhci_adma_transfer(host, cmd);
696
697 /* Write the argument 1 */
698 REG_WRITE32(host, cmd->argument, SDHCI_ARGUMENT_REG);
699
Channagoud Kadabi2e233e72013-06-06 14:09:57 -0700700 /* Set the Transfer mode */
701 if (cmd->data_present)
702 {
703 /* Enable DMA */
704 trans_mode |= SDHCI_DMA_EN;
705
706 if (cmd->trans_mode == SDHCI_MMC_READ)
707 trans_mode |= SDHCI_READ_MODE;
708
709 /* Enable auto cmd 23 for multi block transfer */
710 if (cmd->data.num_blocks > 1) {
711 trans_mode |= SDHCI_TRANS_MULTI | SDHCI_AUTO_CMD23_EN | SDHCI_BLK_CNT_EN;
712 REG_WRITE32(host, cmd->data.num_blocks, SDHCI_ARG2_REG);
713 }
714 }
715
716 /* Write to transfer mode register */
717 REG_WRITE16(host, trans_mode, SDHCI_TRANS_MODE_REG);
718
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700719 /* Write the command register */
720 REG_WRITE16(host, SDHCI_PREP_CMD(cmd->cmd_index, flags), SDHCI_CMD_REG);
721
722 /* Command complete sequence */
723 if (sdhci_cmd_complete(host, cmd))
724 return 1;
725
726 /* Invalidate the cache only for read operations */
727 if (cmd->trans_mode == SDHCI_MMC_READ)
728 arch_invalidate_cache_range((addr_t)cmd->data.data_ptr, (cmd->data.num_blocks * SDHCI_MMC_BLK_SZ));
729
730 /* Free the scatter/gather list */
731 if (sg_list)
732 free(sg_list);
733
734 return 0;
735}
736
737/*
738 * Function: sdhci reset
739 * Arg : Host structure
740 * Return : None
741 * Flow: : Reset the host controller
742 */
743static void sdhci_reset(struct sdhci_host *host)
744{
745 uint32_t reg;
746
747 REG_WRITE8(host, SDHCI_SOFT_RESET, SDHCI_RESET_REG);
748
749 /* Wait for the reset to complete */
750 do {
751 reg = REG_READ8(host, SDHCI_RESET_REG);
752 reg &= SDHCI_SOFT_RESET_MASK;
753
754 if (!reg)
755 break;
756 } while(1);
757}
758
759/*
760 * Function: sdhci mode enable
761 * Arg : Flag (0/1)
762 * Return : None
763 * Flow: : Enable/Disable Sdhci mode
764 */
765void sdhci_mode_enable(uint8_t enable)
766{
767 if (enable)
768 writel(SDHCI_HC_MODE_EN, SDCC_MCI_HC_MODE);
769 else
770 writel(SDHCI_HC_MODE_DIS, SDCC_MCI_HC_MODE);
771}
772
773/*
774 * Function: sdhci init
775 * Arg : Host structure
776 * Return : None
777 * Flow: : 1. Reset the controller
778 * 2. Read the capabilities register & populate the host
779 * controller capabilities for use by other functions
780 * 3. Enable the power control
781 * 4. Set initial bus width
782 * 5. Set Adma mode
783 * 6. Enable the error status
784 */
785void sdhci_init(struct sdhci_host *host)
786{
787 uint32_t caps[2];
788 event_t sdhc_event;
789
790 event_init(&sdhc_event, false, EVENT_FLAG_AUTOUNSIGNAL);
791
792 /*
793 * Reset the controller
794 */
795 sdhci_reset(host);
796
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 */
824 host->caps.ddr_support = (caps[1] & SDHCI_DDR_MODE_MASK) ? 1 : 0;
825
826 /* SDR50 mode support */
827 host->caps.sdr50_support = (caps[1] & SDHCI_SDR50_MODE_MASK) ? 1 : 0;
828
829 /*
830 * Register the interrupt handler for pwr irq
831 */
832 register_int_handler(SDCC_PWRCTRL_IRQ, sdhci_int_handler, &sdhc_event);
833 unmask_interrupt(SDCC_PWRCTRL_IRQ);
834
835 /* Enable pwr control interrupt */
836 writel(SDCC_HC_PWR_CTRL_INT, SDCC_HC_PWRCTL_MASK_REG);
837
838 /* Set bus power on */
839 sdhci_set_bus_power_on(host);
840
841 /* Wait for power interrupt to be handled */
842 event_wait(&sdhc_event);
843
844 /* Set bus width */
845 sdhci_set_bus_width(host, SDHCI_BUS_WITDH_1BIT);
846
847 /* Set Adma mode */
848 sdhci_set_adma_mode(host);
849
850 /*
851 * Enable error status
852 */
853 sdhci_error_status_enable(host);
854}