-
Notifications
You must be signed in to change notification settings - Fork 12
/
emmc_write.c
240 lines (210 loc) · 7.33 KB
/
emmc_write.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
/*
* Copyright (c) 2015-2016, Renesas Electronics Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Renesas nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file emmc_write.c
* @brief write api
*
*/
/* ************************ 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"
/* ***************** MACROS, CONSTANTS, COMPILATION FLAGS ****************** */
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define EMMC_RW_SECTOR_COUNT_MAX 0x0000ffffUL
/* ********************** STRUCTURES, TYPE DEFINITIONS ********************* */
/* ********************** DECLARATION OF EXTERNAL DATA ********************* */
/* ************************** FUNCTION PROTOTYPES ************************** */
static EMMC_ERROR_CODE emmc_multiple_block_write (uint32_t *buff_address_virtual, uint32_t sector_number, uint32_t count, HAL_MEMCARD_DATA_TRANSFER_MODE transfer_mode);
/* ********************************* CODE ********************************** */
/** @brief function of write sector
*
* This function always use block write.
* Single block write is not used.
*
* - Pre-conditions:<BR>
* MMC card is mounted.
*
* - Post-conditions:<BR>
* .
*
* @param[in,out] buff_address_virtual virtual address of write data buffer.
* @param[in] sector_number data address for MMC device (sector number).
* @param[in] count number of sector.
* @param[in] transfer_mode Mode of data transfer, DMA or not DMA.
*/
EMMC_ERROR_CODE emmc_write_sector (
uint32_t *buff_address_virtual,
uint32_t sector_number,
uint32_t count,
uint32_t feature_flags
)
{
uint32_t trans_count;
uint32_t remain;
EMMC_ERROR_CODE result;
HAL_MEMCARD_DATA_TRANSFER_MODE transfer_mode;
/* parameter check */
if (count == 0)
{
emmc_write_error_info(EMMC_FUNCNO_WRITE_SECTOR, EMMC_ERR_PARAM);
return EMMC_ERR_PARAM;
}
/* state check */
if (mmc_drv_obj.mount != TRUE)
{
emmc_write_error_info(EMMC_FUNCNO_WRITE_SECTOR, EMMC_ERR_STATE);
return EMMC_ERR_STATE;
}
/* DMA? */
if ((feature_flags & LOADIMAGE_FLAGS_DMA_ENABLE) != 0)
{
transfer_mode = HAL_MEMCARD_DMA;
}
else
{
transfer_mode = HAL_MEMCARD_NOT_DMA;
}
remain = count;
while (remain != 0)
{
trans_count = MIN(remain, EMMC_RW_SECTOR_COUNT_MAX);
result = emmc_multiple_block_write(buff_address_virtual, sector_number, trans_count, transfer_mode);
if (result != EMMC_SUCCESS)
{
return result;
}
buff_address_virtual += (EMMC_BLOCK_LENGTH_DW*trans_count);
sector_number += trans_count;
remain -= trans_count;
}
return EMMC_SUCCESS;
}
/** @brief multiple block write
*
* Multiple block write with pre-defined block count.
*
* - Pre-conditions:<BR>
* MMC card is mounted.
*
* - Post-conditions:<BR>
* .
*
* @param[in,out] buff_address_virtual virtual address of write data buffer.
* @param[in] sector_number data address for MMC device (sector number).
* @param[in] count number of sector. (0x1 - 0xffff)
* @param[in] transfer_mode Mode of data transfer, DMA or not DMA.
*/
static EMMC_ERROR_CODE emmc_multiple_block_write
(
uint32_t *buff_address_virtual,
uint32_t sector_number,
uint32_t count,
HAL_MEMCARD_DATA_TRANSFER_MODE transfer_mode
)
{
EMMC_ERROR_CODE result;
// st_error_info backup;
#ifdef EMMC_DEBUG
int32_t lchCnt;
char buf[16];
#endif /* EMMC_DEBUG */
/* parameter check */
if ((count > EMMC_RW_SECTOR_COUNT_MAX)
|| (count == 0)
|| ((transfer_mode != HAL_MEMCARD_DMA) && (transfer_mode != HAL_MEMCARD_NOT_DMA))
)
{
emmc_write_error_info(EMMC_FUNCNO_WRITE_SECTOR, EMMC_ERR_PARAM);
return EMMC_ERR_PARAM;
}
#ifdef REWRITE_TOOL
/* CMD16 */
emmc_make_nontrans_cmd(CMD16_SET_BLOCKLEN, EMMC_BLOCK_LENGTH);
result = emmc_exec_cmd(EMMC_R1_ERROR_MASK, mmc_drv_obj.response);
if (result != EMMC_SUCCESS)
{
return result;
}
/* Transfer Data Length */
SETR_32( SD_SIZE, EMMC_BLOCK_LENGTH );
#endif /* REWRITE_TOOL */
/* CMD23 */
emmc_make_nontrans_cmd(CMD23_SET_BLOCK_COUNT, ( 0x80000000U | count) );
result = emmc_exec_cmd(EMMC_R1_ERROR_MASK, mmc_drv_obj.response);
if (result != EMMC_SUCCESS)
{
return result;
}
SETR_32( SD_SECCNT, count );
SETR_32( SD_STOP, 0x00000100U );
SETR_32( CC_EXT_MODE, (CC_EXT_MODE_CLEAR | CC_EXT_MODE_DMASDRW_ENABLE) ); /* SD_BUF Read/Write DMA Transfer enable */
#ifdef EMMC_DEBUG
PutStr("",1);
PutStr("SD_STOP = 0x",0);
Hex2Ascii(GETR_32( SD_STOP ),buf,&lchCnt);
PutStr(buf,1);
PutStr("SD_SECCNT = 0x",0);
Hex2Ascii(GETR_32( SD_SECCNT ),buf,&lchCnt);
PutStr(buf,1);
#endif /* EMMC_DEBUG */
/* CMD25 */
emmc_make_trans_cmd(CMD25_WRITE_MULTIPLE_BLOCK, sector_number, buff_address_virtual, count<<EMMC_SECTOR_SIZE_SHIFT, HAL_MEMCARD_WRITE, transfer_mode);
result = emmc_exec_cmd(EMMC_R1_ERROR_MASK, mmc_drv_obj.response);
if (result != EMMC_SUCCESS)
{
return result; /* CMD18 error code */
}
/* CMD13 */
emmc_make_nontrans_cmd(CMD13_SEND_STATUS, EMMC_RCA<<16);
result = emmc_exec_cmd(EMMC_R1_ERROR_MASK, mmc_drv_obj.response);
if (result != EMMC_SUCCESS)
{
return result;
}
/* ready status check */
if ( (mmc_drv_obj.r1_card_status & EMMC_R1_READY) == 0)
{
emmc_write_error_info(EMMC_FUNCNO_WRITE_SECTOR, EMMC_ERR_CARD_BUSY);
return EMMC_ERR_CARD_BUSY;
}
/* state check */
if (mmc_drv_obj.current_state != EMMC_R1_STATE_TRAN)
{
emmc_write_error_info(EMMC_FUNCNO_WRITE_SECTOR, EMMC_ERR_CARD_STATE);
return EMMC_ERR_CARD_STATE;
}
return EMMC_SUCCESS;
}
/* ******************************** END ************************************ */