forked from renesas-rz/rzg2_flash_writer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemmc_init.c
370 lines (320 loc) · 8.41 KB
/
emmc_init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright (c) 2015-2018, Renesas Electronics Corporation. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/**
* @file emmc_init.c
* @brief eMMC boot initialize program.
*
*/
/* ************************ HEADER (INCLUDE) SECTION *********************** */
#include "emmc_config.h"
#include "emmc_hal.h"
#include "emmc_std.h"
#include "emmc_registers.h"
#include "emmc_def.h"
#include "common.h"
#include "bit.h"
#include "cpg_regs.h"
/* ***************** MACROS, CONSTANTS, COMPILATION FLAGS ****************** */
/* ********************** STRUCTURES, TYPE DEFINITIONS ********************* */
/* ********************** DECLARATION OF EXTERNAL DATA ********************* */
/* ************************** FUNCTION PROTOTYPES ************************** */
static void emmc_drv_init(void);
static EMMC_ERROR_CODE emmc_dev_init(void);
static EMMC_ERROR_CODE emmc_dev_finalize(void);
static void emmc_memset(uint8_t *buff, uint8_t data, uint32_t cnt);
static EMMC_ERROR_CODE emmc_reset_controller(void);
static void emmc_driver_config (void);
__inline static void emmc_set_retry_count(uint32_t retry);
__inline static void emmc_set_data_timeout(uint32_t data_timeout);
#ifdef EMMC_VOLTAGE_1_8
static void SetMmcPfcVoltage(uint32_t voltage);
static void SetMmcVoltage(uint32_t voltage);
static void InitMmcPinFunction(void);
#endif /* EMMC_VOLTAGE_1_8 */
/* ********************************* CODE ********************************** */
/** @brief eMMC initialize.
*
* - Pre-conditions:<BR>
* .
* - Post-conditions:<BR>
* .
*
* @param[in] low_clock_mode_enable TRUE = low clock mode(max26MHz), FALSE= Normal clock mode(Max52MHz)
* @retval EMMC_SUCCESS successful.
* @retval EMMC_ERR error from interrupt API.
*/
EMMC_ERROR_CODE emmc_init(uint8_t low_clock_mode_enable)
{
EMMC_ERROR_CODE retult;
/* initialize H/W */
retult = emmc_reset_controller();
if( EMMC_SUCCESS != retult )
{
return retult;
}
/* Configuration */
emmc_driver_config();
/* clock mode */
mmc_drv_obj.low_clock_mode_enable = low_clock_mode_enable;
return EMMC_SUCCESS;
}
/** @brief terminate emmc driver
*
* EMMC H/W and S/W resource is released.
*
* - Pre-conditions:<BR>
* .
* - Post-conditions:<BR>
* .
*
* @return None.
*/
EMMC_ERROR_CODE emmc_terminate (void)
{
EMMC_ERROR_CODE result;
/* H/W finalize */
result = emmc_dev_finalize();
if (result != EMMC_SUCCESS)
{
/* nothing to do. */
}
/* driver finalize */
emmc_memset((uint8_t *)(&mmc_drv_obj), 0, sizeof(st_mmc_base)); /* clear global variable */
return result;
}
/** @brief Function executes full reset to MMC host controller without taking power out from the memory card.
*
* - Pre-conditions:<BR>
* .
* - Post-conditions:<BR>
* Reset MMC host controller without taking power out from the memory card.
* Memory card preserves its state.
*
* @return None
*/
static EMMC_ERROR_CODE emmc_reset_controller(void)
{
EMMC_ERROR_CODE retult;
/* initialize mmc driver */
emmc_drv_init();
/* initialize H/W */
retult = emmc_dev_init();
if( EMMC_SUCCESS != retult )
{
return retult;
}
mmc_drv_obj.initialize = TRUE;
return retult;
}
/** @brief Configuration eMMC driver
*
* - Pre-conditions:<BR>
* initialized eMMC driver.
* - Post-conditions:<BR>
* .
*
* @return None
*/
static void emmc_driver_config (void)
{
/* retry */
emmc_set_retry_count(EMMC_RETRY_COUNT);
/* Read/Write data timeout */
emmc_set_data_timeout(EMMC_RW_DATA_TIMEOUT);
}
/** @brief Registers configuration
*
* This function gives the configuration arguments to the driver.
* It must be possible to call this function multiple times.
*
* - Pre-conditions:<BR>
* initialized eMMC driver.
*
* - Post-conditions:<BR>
* Driver is using configuration values.
*
* @param[in] retry how many times to try after fail, for instance sending command.
* @return None
*/
__inline static void emmc_set_retry_count (
uint32_t retry
)
{
mmc_drv_obj.retries_after_fail = retry;
}
/** @brief Sets data timeout
*
* Sets the data timeout value for read and write operations.
*
* - Pre-conditions:<BR>
* initialized eMMC driver.
*
* - Post-conditions:<BR>
* After this function is called, the timeout value is set according to argument.
*
* @param[in] time_out The desired timeout value in milliseconds.
* @return None
*/
__inline static void emmc_set_data_timeout (
uint32_t data_timeout
)
{
mmc_drv_obj.data_timeout = data_timeout;
}
/** @brief eMMC driver initialize. (software)
*
* - Pre-conditions:<BR>
* .
* - Post-conditions:<BR>
* .
*
* @return None.
*/
static void emmc_drv_init(void)
{
/* initialize */
emmc_memset((uint8_t *)(&mmc_drv_obj), 0, sizeof(st_mmc_base));
mmc_drv_obj.card_present = HAL_MEMCARD_CARD_IS_IN;
mmc_drv_obj.data_timeout = EMMC_RW_DATA_TIMEOUT;
mmc_drv_obj.bus_width = HAL_MEMCARD_DATA_WIDTH_1_BIT;
}
/** @brief eMMC driver initialize. (H/W)
*
* - Pre-conditions:<BR>
* .
* - Post-conditions:<BR>
* .
*
* @return None.
*/
static EMMC_ERROR_CODE emmc_dev_init(void)
{
#ifdef EMMC_VOLTAGE_1_8
InitMmcPinFunction();
SetMmcVoltage(1); /* I/O Voltage=1.8V */
StartTMU2(10); /* wait 100ms */
#endif /* EMMC_VOLTAGE_1_8 */
/* MMCIF initialize */
SETR_32(SD_INFO1, 0x00000000U); /* all interrupt clear */
SETR_32(SD_INFO2, SD_INFO2_CLEAR ); /* all interrupt clear */
SETR_32(SD_INFO1_MASK, 0x00000000U); /* all interrupt disable */
SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR ); /* all interrupt disable */
SETR_32(HOST_MODE, 0x00000000U); /* SD_BUF access width = 64-bit */
SETR_32(SD_OPTION, 0x0000C0EEU); /* Bus width = 8bit, timeout=MAX */
SETR_32(SD_CLK_CTRL, 0x000000000U); /* Automatic Control=Disable, Clock Output=Disable */
return EMMC_SUCCESS;
}
/** @brief EMMC H/W finalize
*
* EMMC Host and Card hardware resource is released.
*
* - Pre-conditions:<BR>
* .
*
* - Post-conditions:<BR>
* .
* @return None.
*/
static EMMC_ERROR_CODE emmc_dev_finalize(void)
{
EMMC_ERROR_CODE result;
uint32_t mmc_ch = USE_MMC_CH;
uint32_t dataL;
uint32_t tmp_val;
/* MMC power off
* the power supply of eMMC device is always turning on.
* RST_n : Hi --> Low level.
*/
result = emmc_memcard_power(FALSE);
if (result != EMMC_SUCCESS)
{
/* nothing to do. */
}
/* host controller reset */
// SETR_32(SOFT_RST, ( GETR(SOFT_RST) & (~SOFT_RST_SDRST) ) ); /* Soft reset */
// SETR_32(SOFT_RST, ( GETR(SOFT_RST) | SOFT_RST_SDRST ) ); /* Soft reset released */
SETR_32(SD_INFO1, 0x00000000U); /* all interrupt clear */
SETR_32(SD_INFO2, SD_INFO2_CLEAR ); /* all interrupt clear */
SETR_32(SD_INFO1_MASK, 0x00000000U); /* all interrupt disable */
SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR ); /* all interrupt disable */
SETR_32(SD_CLK_CTRL, 0x00000000U); /* MMC clock stop */
return result;
}
/** @brief Set power to memory card IF.
* This function control Vcc and Vccq and RST_n.
*
* @attention
* CPU cannot control Vcc&Vccq.
* The power supply of eMMC device is always turning on.
*
* @param[in] mode TRUE = power on, FALSE = power off
*
* @retval EMMC_SUCCESS powering succeeded
* @retval EMMC_ERR_CARD_POWER powering failed
*/
EMMC_ERROR_CODE emmc_memcard_power
(
uint8_t mode
)
{
if (mode == TRUE)
{
/* power on (Vcc&Vccq is always power on) */
mmc_drv_obj.card_power_enable = TRUE;
}
else
{
/* power off (Vcc&Vccq is always power on) */
mmc_drv_obj.card_power_enable = FALSE;
mmc_drv_obj.mount = FALSE;
mmc_drv_obj.selected = FALSE;
}
return EMMC_SUCCESS;
}
/** @brief memset(). no use C standard library.
*
* - Pre-conditions:<BR>
* .
* - Post-conditions:<BR>
* .
*
* @param[in,out] buff pointer to buffer (virtual)
* @param[in] data fill data.
* @param[in] cnt fill size (number of bytes)
* @return None.
*/
static void emmc_memset(
uint8_t *buff,
uint8_t data,
uint32_t cnt
)
{
if (buff == NULL)
{
return;
}
while (cnt > 0)
{
*buff++ = data;
cnt--;
}
}
#ifdef EMMC_VOLTAGE_1_8
// voltage=0: 1.8V
// voltage=1: 3.3V
static void SetMmcPfcVoltage(uint32_t voltage)
{
}
// voltage=0: 1.8V
// voltage=1: 3.3V
static void SetMmcVoltage(uint32_t voltage)
{
}
static void InitMmcPinFunction(void)
{
}
#endif /* EMMC_VOLTAGE_1_8 */
/* ******************************** END ************************************ */