Skip to content

Commit 01bf812

Browse files
committed
src: add flash abstraction layer
Signed-off-by: Watson Zeng <zhiwei@synopsys.com>
1 parent 18d2972 commit 01bf812

File tree

22 files changed

+1093
-70
lines changed

22 files changed

+1093
-70
lines changed

board/emsk/configs/11/target_mem_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#define FLASH_ALIGN FLASH_ALIGNMENT
5353
#endif
5454

55+
#define FLASH_DEV_ID EMSK_DDR_RAM_ID
5556
#define FLASH_DEV_NAME
5657
#define FLASH_AREA_IMAGE_0_OFFSET 0x10000000
5758
#define FLASH_AREA_IMAGE_0_SIZE 0x00400000

board/emsk/configs/22/target_mem_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#define FLASH_ALIGN FLASH_ALIGNMENT
5353
#endif
5454

55+
#define FLASH_DEV_ID EMSK_DDR_RAM_ID
5556
#define FLASH_DEV_NAME
5657
#define FLASH_AREA_IMAGE_0_OFFSET 0x10000000
5758
#define FLASH_AREA_IMAGE_0_SIZE 0x00400000

board/emsk/configs/23/target_mem_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#define FLASH_ALIGN FLASH_ALIGNMENT
5353
#endif
5454

55+
#define FLASH_DEV_ID EMSK_DDR_RAM_ID
5556
#define FLASH_DEV_NAME
5657
#define FLASH_AREA_IMAGE_0_OFFSET 0x10000000
5758
#define FLASH_AREA_IMAGE_0_SIZE 0x00400000
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/* ------------------------------------------
2+
* Copyright (c) 2018, Synopsys, Inc. All rights reserved.
3+
4+
* Redistribution and use in source and binary forms, with or without modification,
5+
* are permitted provided that the following conditions are met:
6+
7+
* 1) Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
10+
* 2) Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation and/or
12+
* other materials provided with the distribution.
13+
14+
* 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
15+
* be used to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*
29+
--------------------------------------------- */
30+
31+
#include "embARC_toolchain.h"
32+
#include "embARC_error.h"
33+
#include "arc_exception.h"
34+
35+
36+
#include "target_mem_config.h"
37+
#include "flash_obj.h"
38+
#include <string.h>
39+
40+
#define DW_FLASH_CHECK_EXP(EXPR, ERROR_CODE) CHECK_EXP(EXPR, ercd, ERROR_CODE, error_exit)
41+
42+
void emsk_flash_obj_all_install(void);
43+
44+
#if (USE_EMSK_DDR_RAM)
45+
#define EMSK_DDR_RAM_BEGIN_ADDR 0x10000000
46+
#define EMSK_DDR_RAM_TOTAL_SIZE 0x8000000
47+
static DEV_FLASH emsk_ddr_ram_obj;
48+
49+
static int32_t emsk_ddr_ram_open(uint32_t param1, void *param2)
50+
{
51+
int32_t ercd = E_OK;
52+
DEV_FLASH_PTR obj_ptr = &emsk_ddr_ram_obj;
53+
DEV_FLASH_INFO_PTR info_ptr = &(obj_ptr->flash_info);
54+
55+
info_ptr->open_cnt++;
56+
if (info_ptr->open_cnt > 1) {
57+
return E_OPNED;
58+
}
59+
60+
error_exit:
61+
return ercd;
62+
}
63+
64+
static int32_t emsk_ddr_ram_close()
65+
{
66+
int32_t ercd = E_OK;
67+
DEV_FLASH_PTR obj_ptr = &emsk_ddr_ram_obj;
68+
DEV_FLASH_INFO_PTR info_ptr = &(obj_ptr->flash_info);
69+
70+
info_ptr->open_cnt--;
71+
if (info_ptr->open_cnt == 0) {
72+
return E_OK;
73+
} else {
74+
ercd = E_OPNED;
75+
}
76+
77+
error_exit:
78+
return ercd;
79+
}
80+
81+
static int32_t emsk_ddr_ram_control(uint32_t cmd, void *param)
82+
{
83+
int32_t ercd = E_OK;
84+
DEV_FLASH_PTR obj_ptr = &emsk_ddr_ram_obj;
85+
DEV_FLASH_INFO_PTR info_ptr = &(obj_ptr->flash_info);
86+
87+
DW_FLASH_CHECK_EXP(info_ptr->open_cnt > 0, E_CLSED);
88+
89+
switch (cmd) {
90+
case FLASH_CMD_GET_INFO:
91+
((DEV_FLASH_INFO *)param)->begin_addr = info_ptr->begin_addr;
92+
((DEV_FLASH_INFO *)param)->total_size = info_ptr->total_size;
93+
break;
94+
}
95+
96+
97+
98+
error_exit:
99+
return ercd;
100+
}
101+
102+
static int32_t emsk_ddr_ram_read(uint32_t addr, void *dst, uint32_t len)
103+
{
104+
int32_t ercd = E_OK;
105+
DEV_FLASH_PTR obj_ptr = &emsk_ddr_ram_obj;
106+
DEV_FLASH_INFO_PTR info_ptr = &(obj_ptr->flash_info);
107+
108+
DW_FLASH_CHECK_EXP(info_ptr->open_cnt > 0, E_CLSED);
109+
DW_FLASH_CHECK_EXP(info_ptr->begin_addr <= addr, E_PAR);
110+
111+
if ((addr + len) > (info_ptr->begin_addr + info_ptr->total_size)) {
112+
len = info_ptr->begin_addr + info_ptr->total_size - len;
113+
}
114+
115+
memcpy((void *)dst, (const void *)addr, len);
116+
117+
return len;
118+
119+
error_exit:
120+
return ercd;
121+
}
122+
123+
static int32_t emsk_ddr_ram_write(uint32_t addr, void *src, uint32_t len)
124+
{
125+
int32_t ercd = E_OK;
126+
DEV_FLASH_PTR obj_ptr = &emsk_ddr_ram_obj;
127+
DEV_FLASH_INFO_PTR info_ptr = &(obj_ptr->flash_info);
128+
129+
DW_FLASH_CHECK_EXP(info_ptr->open_cnt > 0, E_CLSED);
130+
DW_FLASH_CHECK_EXP(info_ptr->begin_addr <= addr, E_PAR);
131+
132+
if ((addr + len) > (info_ptr->begin_addr + info_ptr->total_size)) {
133+
len = info_ptr->begin_addr + info_ptr->total_size - len;
134+
}
135+
136+
memcpy((void *)addr, (const void *)src, len);
137+
138+
return len;
139+
140+
error_exit:
141+
return ercd;
142+
}
143+
144+
static int32_t emsk_ddr_ram_erase(uint32_t addr, uint32_t len)
145+
{
146+
int32_t ercd = E_OK;
147+
DEV_FLASH_PTR obj_ptr = &emsk_ddr_ram_obj;
148+
DEV_FLASH_INFO_PTR info_ptr = &(obj_ptr->flash_info);
149+
150+
DW_FLASH_CHECK_EXP(info_ptr->open_cnt > 0, E_CLSED);
151+
DW_FLASH_CHECK_EXP(info_ptr->begin_addr <= addr, E_PAR);
152+
153+
if ((addr + len) > (info_ptr->begin_addr + info_ptr->total_size)) {
154+
len = info_ptr->begin_addr + info_ptr->total_size - len;
155+
}
156+
157+
memset((void *)addr, 0xFF, len);
158+
159+
return len;
160+
161+
error_exit:
162+
return ercd;
163+
}
164+
static void emsk_ddr_ram_install(void)
165+
{
166+
DEV_FLASH_PTR obj_ptr = &emsk_ddr_ram_obj;
167+
DEV_FLASH_INFO_PTR info_ptr = &(obj_ptr->flash_info);
168+
169+
info_ptr->begin_addr = EMSK_DDR_RAM_BEGIN_ADDR;
170+
info_ptr->total_size = EMSK_DDR_RAM_TOTAL_SIZE;
171+
info_ptr->page_size = 0;
172+
info_ptr->page_cnt = 0;
173+
info_ptr->align_size = 1;
174+
info_ptr->open_cnt = 0;
175+
176+
obj_ptr->flash_open = emsk_ddr_ram_open;
177+
obj_ptr->flash_close = emsk_ddr_ram_close;
178+
obj_ptr->flash_control = emsk_ddr_ram_control;
179+
obj_ptr->flash_write = emsk_ddr_ram_write;
180+
obj_ptr->flash_read = emsk_ddr_ram_read;
181+
obj_ptr->flash_erase = emsk_ddr_ram_erase;
182+
}
183+
#endif
184+
185+
186+
187+
DEV_FLASH_PTR flash_get_dev(int32_t flash_id)
188+
{
189+
static uint32_t install_flag = 0;
190+
191+
/* intall device objects */
192+
if (install_flag == 0) {
193+
install_flag = 1;
194+
emsk_flash_obj_all_install();
195+
}
196+
197+
switch (flash_id) {
198+
#if (USE_EMSK_DDR_RAM)
199+
200+
case EMSK_DDR_RAM_ID:
201+
return &emsk_ddr_ram_obj;
202+
break;
203+
#endif
204+
default:
205+
break;
206+
}
207+
208+
return NULL;
209+
}
210+
211+
212+
void emsk_flash_obj_all_install(void)
213+
{
214+
#if (USE_EMSK_DDR_RAM)
215+
emsk_ddr_ram_install();
216+
#endif
217+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* ------------------------------------------
2+
* Copyright (c) 2018, Synopsys, Inc. All rights reserved.
3+
4+
* Redistribution and use in source and binary forms, with or without modification,
5+
* are permitted provided that the following conditions are met:
6+
7+
* 1) Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
10+
* 2) Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation and/or
12+
* other materials provided with the distribution.
13+
14+
* 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
15+
* be used to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*
29+
--------------------------------------------- */
30+
31+
#ifndef _EMSK_FLASH_OBJ_H_
32+
#define _EMSK_FLASH_OBJ_H_
33+
34+
#include "dev_flash.h"
35+
36+
#define USE_EMSK_DDR_RAM 1
37+
38+
#define EMSK_DDR_RAM_ID 0
39+
40+
41+
#ifdef __cplusplus
42+
extern "C" {
43+
#endif
44+
45+
DEV_FLASH_PTR flash_get_dev(int32_t flash_id);
46+
47+
#ifdef __cplusplus
48+
}
49+
#endif
50+
51+
#endif /*_EMSK_FLASH_OBJ_H_*/

board/emsk/emsk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "drivers/ntshell/ntshell_io.h"
5252
#include "drivers/sdcard/emsk_sdcard.h"
5353
#include "drivers/pmwifi/pmwifi.h"
54+
#include "drivers/flash_obj/flash_obj.h"
5455
#include "dev_pinmux.h"
5556

5657
#include "common/emsk_timer.h"

board/emsk/emsk.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ BOARD_EMSK_DEV_INCDIR += $(DEV_INCDIR)
9696

9797
BOARD_EMSK_CSRCDIR += $(BOARD_EMSK_DEV_CSRCDIR) $(BOARD_CORE_DIR) \
9898
$(BOARD_EMSK_DIR)/common \
99-
$(BOARD_EMSK_DIR)/drivers/mux
99+
$(BOARD_EMSK_DIR)/drivers/mux \
100+
$(BOARD_EMSK_DIR)/drivers/flash_obj
100101

101102
# select dirvers according to middleware
102103
ifneq ($(findstring ntshell, $(MID_SEL)), )

board/iotdk/configs/10/target_mem_config.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@
5252
#define FLASH_ALIGN FLASH_ALIGNMENT
5353
#endif
5454

55+
#define FLASH_DEV_ID IOTDK_BOOT_SPI_FLASH_ID
5556
#define FLASH_DEV_NAME
56-
#define FLASH_AREA_IMAGE_0_OFFSET 0x100C0000
57-
#define FLASH_AREA_IMAGE_0_SIZE 0x00080000
58-
#define FLASH_AREA_IMAGE_1_OFFSET 0x10140000
59-
#define FLASH_AREA_IMAGE_1_SIZE 0x00080000
60-
#define FLASH_AREA_IMAGE_SCRATCH_OFFSET 0x101C0000
61-
#define FLASH_AREA_IMAGE_SCRATCH_SIZE 0x00020000
62-
#define FLASH_AREA_IMAGE_MCUBOOT_OFFSET 0x10000000
63-
#define FLASH_AREA_IMAGE_MCUBOOT_SIZE 0x000C0000
57+
#define FLASH_AREA_IMAGE_0_OFFSET 0x10000000
58+
#define FLASH_AREA_IMAGE_0_SIZE 0x000C0000
59+
#define FLASH_AREA_IMAGE_1_OFFSET 0x100C0000
60+
#define FLASH_AREA_IMAGE_1_SIZE 0x000C0000
61+
#define FLASH_AREA_IMAGE_SCRATCH_OFFSET 0x10180000
62+
#define FLASH_AREA_IMAGE_SCRATCH_SIZE 0x00040000
63+
#define FLASH_AREA_IMAGE_MCUBOOT_OFFSET 0x00000000
64+
#define FLASH_AREA_IMAGE_MCUBOOT_SIZE 0x00040000
6465

6566

6667
#if !defined(FLASH_AREA_IMAGE_SECTOR_SIZE)

0 commit comments

Comments
 (0)