|
| 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 | +} |
0 commit comments