blob: a2e25e3394951cf3a66793e15d5e181032a233ba [file] [log] [blame]
Jorge Ramirez-Ortiz766263c2018-09-23 09:39:56 +02001/*
2 * Copyright (c) 2015-2017, Renesas Electronics Corporation. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <common/debug.h>
8
Jorge Ramirez-Ortiz766263c2018-09-23 09:39:56 +02009#include "emmc_config.h"
10#include "emmc_hal.h"
11#include "emmc_std.h"
12#include "emmc_registers.h"
13#include "emmc_def.h"
14#include "micro_delay.h"
15
16static void emmc_little_to_big(uint8_t *p, uint32_t value)
17{
18 if (p == NULL)
19 return;
20
21 p[0] = (uint8_t) (value >> 24);
22 p[1] = (uint8_t) (value >> 16);
23 p[2] = (uint8_t) (value >> 8);
24 p[3] = (uint8_t) value;
25}
26
27static void emmc_softreset(void)
28{
29 int32_t loop = 10000;
30 int32_t retry = 1000;
31
32 /* flag clear */
33 mmc_drv_obj.during_cmd_processing = FALSE;
34 mmc_drv_obj.during_transfer = FALSE;
35 mmc_drv_obj.during_dma_transfer = FALSE;
36 mmc_drv_obj.state_machine_blocking = FALSE;
37 mmc_drv_obj.force_terminate = FALSE;
38 mmc_drv_obj.dma_error_flag = FALSE;
39
40 /* during operation ? */
41 if ((GETR_32(SD_INFO2) & SD_INFO2_CBSY) == 0)
42 goto reset;
43
44 /* wait CMDSEQ = 0 */
45 while (loop > 0) {
46 if ((GETR_32(SD_INFO2) & SD_INFO2_CBSY) == 0)
47 break; /* ready */
48
49 loop--;
50 if ((loop == 0) && (retry > 0)) {
51 rcar_micro_delay(1000U); /* wait 1ms */
52 loop = 10000;
53 retry--;
54 }
55 }
56
57reset:
58 /* reset */
59 SETR_32(SOFT_RST, (GETR_32(SOFT_RST) & (~SOFT_RST_SDRST)));
60 SETR_32(SOFT_RST, (GETR_32(SOFT_RST) | SOFT_RST_SDRST));
61
62 /* initialize */
63 SETR_32(SD_INFO1, 0x00000000U);
64 SETR_32(SD_INFO2, SD_INFO2_CLEAR);
65 SETR_32(SD_INFO1_MASK, 0x00000000U); /* all interrupt disable */
66 SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR); /* all interrupt disable */
67
68}
69
70static void emmc_read_response(uint32_t *response)
71{
72 uint8_t *p;
73
74 if (response == NULL)
75 return;
76
77 /* read response */
78 if (mmc_drv_obj.response_length != EMMC_MAX_RESPONSE_LENGTH) {
79 *response = GETR_32(SD_RSP10); /* [39:8] */
80 return;
81 }
82
83 /* CSD or CID */
84 p = (uint8_t *) (response);
85 emmc_little_to_big(p, ((GETR_32(SD_RSP76) << 8)
86 | (GETR_32(SD_RSP54) >> 24))); /* [127:96] */
87 emmc_little_to_big(p + 4, ((GETR_32(SD_RSP54) << 8)
88 | (GETR_32(SD_RSP32) >> 24))); /* [95:64] */
89 emmc_little_to_big(p + 8, ((GETR_32(SD_RSP32) << 8)
90 | (GETR_32(SD_RSP10) >> 24))); /* [63:32] */
91 emmc_little_to_big(p + 12, (GETR_32(SD_RSP10) << 8));
92}
93
94static EMMC_ERROR_CODE emmc_response_check(uint32_t *response,
95 uint32_t error_mask)
96{
97
98 HAL_MEMCARD_RESPONSE_TYPE response_type =
99 (HAL_MEMCARD_RESPONSE_TYPE) (mmc_drv_obj.cmd_info.
100 cmd & HAL_MEMCARD_RESPONSE_TYPE_MASK);
101
102 if (response == NULL)
103 return EMMC_ERR_PARAM;
104
105 if (response_type == HAL_MEMCARD_RESPONSE_NONE)
106 return EMMC_SUCCESS;
107
108
109 if (response_type <= HAL_MEMCARD_RESPONSE_R1b) {
110 /* R1 or R1b */
111 mmc_drv_obj.current_state =
112 (EMMC_R1_STATE) ((*response & EMMC_R1_STATE_MASK) >>
113 EMMC_R1_STATE_SHIFT);
114 if ((*response & error_mask) != 0) {
115 if ((0x80 & *response) != 0) {
116 ERROR("BL2: emmc SWITCH_ERROR\n");
117 }
118 return EMMC_ERR_CARD_STATUS_BIT;
119 }
120 return EMMC_SUCCESS;;
121 }
122
123 if (response_type == HAL_MEMCARD_RESPONSE_R4) {
124 if ((*response & EMMC_R4_STATUS) != 0)
125 return EMMC_ERR_CARD_STATUS_BIT;
126 }
127
128 return EMMC_SUCCESS;
129}
130
131static void emmc_WaitCmd2Cmd_8Cycle(void)
132{
133 uint32_t dataL, wait = 0;
134
135 dataL = GETR_32(SD_CLK_CTRL);
136 dataL &= 0x000000FF;
137
138 switch (dataL) {
139 case 0xFF:
140 case 0x00:
141 case 0x01:
142 case 0x02:
143 case 0x04:
144 case 0x08:
145 case 0x10:
146 case 0x20:
147 wait = 10U;
148 break;
149 case 0x40:
150 wait = 20U;
151 break;
152 case 0x80:
153 wait = 30U;
154 break;
155 }
156
157 rcar_micro_delay(wait);
158}
159
160static void cmdErrSdInfo2Log(void)
161{
162 ERROR("BL2: emmc ERR SD_INFO2 = 0x%x\n", mmc_drv_obj.error_info.info2);
163}
164
165static void emmc_data_transfer_dma(void)
166{
167 mmc_drv_obj.during_dma_transfer = TRUE;
168 mmc_drv_obj.dma_error_flag = FALSE;
169
170 SETR_32(SD_INFO1_MASK, 0x00000000U);
171 SETR_32(SD_INFO2_MASK, (SD_INFO2_ALL_ERR | SD_INFO2_CLEAR));
172
173 /* DMAC setting */
174 if (mmc_drv_obj.cmd_info.dir == HAL_MEMCARD_WRITE) {
175 /* transfer complete interrupt enable */
176 SETR_32(DM_CM_INFO1_MASK,
177 (DM_CM_INFO_MASK_CLEAR | DM_CM_INFO_CH0_ENABLE));
178 SETR_32(DM_CM_INFO2_MASK,
179 (DM_CM_INFO_MASK_CLEAR | DM_CM_INFO_CH0_ENABLE));
180 /* BUFF --> FIFO */
181 SETR_32(DM_CM_DTRAN_MODE, (DM_CM_DTRAN_MODE_CH0 |
182 DM_CM_DTRAN_MODE_BIT_WIDTH));
183 } else {
184 /* transfer complete interrupt enable */
185 SETR_32(DM_CM_INFO1_MASK,
186 (DM_CM_INFO_MASK_CLEAR | DM_CM_INFO_CH1_ENABLE));
187 SETR_32(DM_CM_INFO2_MASK,
188 (DM_CM_INFO_MASK_CLEAR | DM_CM_INFO_CH1_ENABLE));
189 /* FIFO --> BUFF */
190 SETR_32(DM_CM_DTRAN_MODE, (DM_CM_DTRAN_MODE_CH1
191 | DM_CM_DTRAN_MODE_BIT_WIDTH));
192 }
193 SETR_32(DM_DTRAN_ADDR, (((uintptr_t) mmc_drv_obj.buff_address_virtual &
194 DM_DTRAN_ADDR_WRITE_MASK)));
195
196 SETR_32(DM_CM_DTRAN_CTRL, DM_CM_DTRAN_CTRL_START);
197}
198
199EMMC_ERROR_CODE emmc_exec_cmd(uint32_t error_mask, uint32_t *response)
200{
201 EMMC_ERROR_CODE rtn_code = EMMC_SUCCESS;
202 HAL_MEMCARD_RESPONSE_TYPE response_type;
203 HAL_MEMCARD_COMMAND_TYPE cmd_type;
204 EMMC_INT_STATE state;
205 uint32_t err_not_care_flag = FALSE;
206
207 /* parameter check */
208 if (response == NULL) {
209 emmc_write_error_info(EMMC_FUNCNO_EXEC_CMD, EMMC_ERR_PARAM);
210 return EMMC_ERR_PARAM;
211 }
212
213 /* state check */
214 if (mmc_drv_obj.clock_enable != TRUE) {
215 emmc_write_error_info(EMMC_FUNCNO_EXEC_CMD, EMMC_ERR_STATE);
216 return EMMC_ERR_STATE;
217 }
218
219 if (mmc_drv_obj.state_machine_blocking == TRUE) {
220 emmc_write_error_info(EMMC_FUNCNO_EXEC_CMD, EMMC_ERR);
221 return EMMC_ERR;
222 }
223
224 state = ESTATE_BEGIN;
225 response_type =
226 (HAL_MEMCARD_RESPONSE_TYPE) (mmc_drv_obj.cmd_info.
227 cmd & HAL_MEMCARD_RESPONSE_TYPE_MASK);
228 cmd_type =
229 (HAL_MEMCARD_COMMAND_TYPE) (mmc_drv_obj.cmd_info.
230 cmd & HAL_MEMCARD_COMMAND_TYPE_MASK);
231
232 /* state machine */
233 while ((mmc_drv_obj.force_terminate != TRUE) && (state != ESTATE_END)) {
234 /* The interrupt factor flag is observed. */
235 emmc_interrupt();
236
237 /* wait interrupt */
238 if (mmc_drv_obj.state_machine_blocking == TRUE)
239 continue;
240
241 switch (state) {
242 case ESTATE_BEGIN:
243 /* Busy check */
244 if ((mmc_drv_obj.error_info.info2 & SD_INFO2_CBSY) != 0) {
245 emmc_write_error_info(EMMC_FUNCNO_EXEC_CMD,
246 EMMC_ERR_CARD_BUSY);
247 return EMMC_ERR_CARD_BUSY;
248 }
249
250 /* clear register */
251 SETR_32(SD_INFO1, 0x00000000U);
252 SETR_32(SD_INFO2, SD_INFO2_CLEAR);
253 SETR_32(SD_INFO1_MASK, SD_INFO1_INFO0);
254 SETR_32(SD_INFO2_MASK,
255 (SD_INFO2_ALL_ERR | SD_INFO2_CLEAR));
256
257 state = ESTATE_ISSUE_CMD;
258 /* through */
259
260 case ESTATE_ISSUE_CMD:
261 /* ARG */
262 SETR_32(SD_ARG, mmc_drv_obj.cmd_info.arg);
263 /* issue cmd */
264 SETR_32(SD_CMD, mmc_drv_obj.cmd_info.hw);
265 /* Set driver flag */
266 mmc_drv_obj.during_cmd_processing = TRUE;
267 mmc_drv_obj.state_machine_blocking = TRUE;
268
269 if (response_type == HAL_MEMCARD_RESPONSE_NONE) {
270 state = ESTATE_NON_RESP_CMD;
271 } else {
272 state = ESTATE_RCV_RESP;
273 }
274
275 break;
276
277 case ESTATE_NON_RESP_CMD:
278 /* interrupt disable */
279 SETR_32(SD_INFO1_MASK, 0x00000000U);
280 SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR);
281
282 /* check interrupt */
283 if ((mmc_drv_obj.int_event2 & SD_INFO2_ALL_ERR) != 0) {
284 /* error interrupt */
285 cmdErrSdInfo2Log();
286 rtn_code = EMMC_ERR_INFO2;
287 state = ESTATE_ERROR;
288 } else if ((mmc_drv_obj.int_event1 & SD_INFO1_INFO0) ==
289 0) {
290 /* not receive expected interrupt */
291 rtn_code = EMMC_ERR_RESPONSE;
292 state = ESTATE_ERROR;
293 } else {
294 emmc_WaitCmd2Cmd_8Cycle();
295 state = ESTATE_END;
296 }
297 break;
298
299 case ESTATE_RCV_RESP:
300 /* interrupt disable */
301 SETR_32(SD_INFO1_MASK, 0x00000000U);
302 SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR);
303
304 /* check interrupt */
305 if ((mmc_drv_obj.int_event2 & SD_INFO2_ALL_ERR) != 0) {
306 if ((mmc_drv_obj.get_partition_access_flag ==
307 TRUE)
308 && ((mmc_drv_obj.int_event2 & SD_INFO2_ERR6)
309 != 0U)) {
310 err_not_care_flag = TRUE;
311 rtn_code = EMMC_ERR_CMD_TIMEOUT;
312 } else {
313 /* error interrupt */
314 cmdErrSdInfo2Log();
315 rtn_code = EMMC_ERR_INFO2;
316 }
317 state = ESTATE_ERROR;
318 break;
319 } else if ((mmc_drv_obj.int_event1 & SD_INFO1_INFO0) ==
320 0) {
321 /* not receive expected interrupt */
322 rtn_code = EMMC_ERR_RESPONSE;
323 state = ESTATE_ERROR;
324 break;
325 }
326
327 /* read response */
328 emmc_read_response(response);
329
330 /* check response */
331 rtn_code = emmc_response_check(response, error_mask);
332 if (rtn_code != EMMC_SUCCESS) {
333 state = ESTATE_ERROR;
334 break;
335 }
336
337 if (response_type == HAL_MEMCARD_RESPONSE_R1b) {
338 /* R1b */
339 SETR_32(SD_INFO2_MASK,
340 (SD_INFO2_ALL_ERR | SD_INFO2_CLEAR));
341 state = ESTATE_RCV_RESPONSE_BUSY;
342 } else {
343 state = ESTATE_CHECK_RESPONSE_COMPLETE;
344 }
345 break;
346
347 case ESTATE_RCV_RESPONSE_BUSY:
348 /* check interrupt */
349 if ((mmc_drv_obj.int_event2 & SD_INFO2_ALL_ERR) != 0) {
350 /* error interrupt */
351 cmdErrSdInfo2Log();
352 rtn_code = EMMC_ERR_INFO2;
353 state = ESTATE_ERROR;
354 break;
355 }
356 /* DAT0 not Busy */
357 if ((SD_INFO2_DAT0 & mmc_drv_obj.error_info.info2) != 0) {
358 state = ESTATE_CHECK_RESPONSE_COMPLETE;
359 break;
360 }
361 break;
362
363 case ESTATE_CHECK_RESPONSE_COMPLETE:
364 if (cmd_type >= HAL_MEMCARD_COMMAND_TYPE_ADTC_WRITE) {
365 state = ESTATE_DATA_TRANSFER;
366 } else {
367 emmc_WaitCmd2Cmd_8Cycle();
368 state = ESTATE_END;
369 }
370 break;
371
372 case ESTATE_DATA_TRANSFER:
373 /* ADTC command */
374 mmc_drv_obj.during_transfer = TRUE;
375 mmc_drv_obj.state_machine_blocking = TRUE;
376
377 if (mmc_drv_obj.transfer_mode == HAL_MEMCARD_DMA) {
378 /* DMA */
379 emmc_data_transfer_dma();
380 } else {
381 /* PIO */
382 /* interrupt enable (FIFO read/write enable) */
383 if (mmc_drv_obj.cmd_info.dir ==
384 HAL_MEMCARD_WRITE) {
385 SETR_32(SD_INFO2_MASK,
386 (SD_INFO2_BWE | SD_INFO2_ALL_ERR
387 | SD_INFO2_CLEAR));
388 } else {
389 SETR_32(SD_INFO2_MASK,
390 (SD_INFO2_BRE | SD_INFO2_ALL_ERR
391 | SD_INFO2_CLEAR));
392 }
393 }
394 state = ESTATE_DATA_TRANSFER_COMPLETE;
395 break;
396
397 case ESTATE_DATA_TRANSFER_COMPLETE:
398 /* check interrupt */
399 if ((mmc_drv_obj.int_event2 & SD_INFO2_ALL_ERR) != 0) {
400 /* error interrupt */
401 cmdErrSdInfo2Log();
402 rtn_code = EMMC_ERR_INFO2;
403 state = ESTATE_TRANSFER_ERROR;
404 break;
405 }
406
407 /* DMAC error ? */
408 if (mmc_drv_obj.dma_error_flag == TRUE) {
409 /* Error occurred in DMAC driver. */
410 rtn_code = EMMC_ERR_FROM_DMAC_TRANSFER;
411 state = ESTATE_TRANSFER_ERROR;
412 } else if (mmc_drv_obj.during_dma_transfer == TRUE) {
413 /* DMAC not finished. unknown error */
414 rtn_code = EMMC_ERR;
415 state = ESTATE_TRANSFER_ERROR;
416 } else {
417 SETR_32(SD_INFO1_MASK, SD_INFO1_INFO2);
418 SETR_32(SD_INFO2_MASK,
419 (SD_INFO2_ALL_ERR | SD_INFO2_CLEAR));
420
421 mmc_drv_obj.state_machine_blocking = TRUE;
422
423 state = ESTATE_ACCESS_END;
424 }
425 break;
426
427 case ESTATE_ACCESS_END:
428
429 /* clear flag */
430 if (HAL_MEMCARD_DMA == mmc_drv_obj.transfer_mode) {
431 SETR_32(CC_EXT_MODE, CC_EXT_MODE_CLEAR); /* W (CC_EXT_MODE, H'0000_1010) SD_BUF DMA transfer disabled */
432 SETR_32(SD_STOP, 0x00000000U);
433 mmc_drv_obj.during_dma_transfer = FALSE;
434 }
435
436 SETR_32(SD_INFO1_MASK, 0x00000000U);
437 SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR);
438 SETR_32(SD_INFO1, 0x00000000U);
439 SETR_32(SD_INFO2, SD_INFO2_CLEAR);
440
441 if ((mmc_drv_obj.int_event1 & SD_INFO1_INFO2) != 0) {
442 emmc_WaitCmd2Cmd_8Cycle();
443 state = ESTATE_END;
444 } else {
445 state = ESTATE_ERROR;
446 }
447 break;
448
449 case ESTATE_TRANSFER_ERROR:
450 /* The error occurred in the Data transfer. */
451 if (HAL_MEMCARD_DMA == mmc_drv_obj.transfer_mode) {
452 SETR_32(CC_EXT_MODE, CC_EXT_MODE_CLEAR); /* W (CC_EXT_MODE, H'0000_1010) SD_BUF DMA transfer disabled */
453 SETR_32(SD_STOP, 0x00000000U);
454 mmc_drv_obj.during_dma_transfer = FALSE;
455 }
456 /* through */
457
458 case ESTATE_ERROR:
459 if (err_not_care_flag == TRUE) {
460 mmc_drv_obj.during_cmd_processing = FALSE;
461 } else {
462 emmc_softreset();
463 emmc_write_error_info(EMMC_FUNCNO_EXEC_CMD,
464 rtn_code);
465 }
466 return rtn_code;
467
468 default:
469 state = ESTATE_END;
470 break;
471 } /* switch (state) */
472 } /* while ( (mmc_drv_obj.force_terminate != TRUE) && (state != ESTATE_END) ) */
473
474 /* force terminate */
475 if (mmc_drv_obj.force_terminate == TRUE) {
476 /* timeout timer is expired. Or, PIO data transfer error. */
477 /* Timeout occurred in the DMA transfer. */
478 if (mmc_drv_obj.during_dma_transfer == TRUE) {
479 mmc_drv_obj.during_dma_transfer = FALSE;
480 }
481 ERROR("BL2: emmc exec_cmd:EMMC_ERR_FORCE_TERMINATE\n");
482 emmc_softreset();
483
484 return EMMC_ERR_FORCE_TERMINATE; /* error information has already been written. */
485 }
486
487 /* success */
488 mmc_drv_obj.during_cmd_processing = FALSE;
489 mmc_drv_obj.during_transfer = FALSE;
490
491 return EMMC_SUCCESS;
492}