Mercurial > flash_v2
comparison packages/devs/flash/synthv2/current/src/synth.c @ 1756:2f4e31c45a45
Update following changes to the flash device driver interface
| author | bartv |
|---|---|
| date | Mon, 22 Nov 2004 20:54:04 +0000 |
| parents | b16e2895566e |
| children | 9c1d9e5039c2 |
comparison
equal
deleted
inserted
replaced
| 1755:21fc12d67292 | 1756:2f4e31c45a45 |
|---|---|
| 84 } | 84 } |
| 85 | 85 |
| 86 static int | 86 static int |
| 87 synth_flash_init(struct cyg_flash_dev *dev) | 87 synth_flash_init(struct cyg_flash_dev *dev) |
| 88 { | 88 { |
| 89 struct cyg_flash_synth_config *config = dev->config; | 89 struct cyg_flash_synth_priv *priv = (struct cyg_flash_synth_priv*)dev->priv; |
| 90 struct cyg_flash_synth_priv *priv = dev->priv; | |
| 91 cyg_flashaddr_t base; | 90 cyg_flashaddr_t base; |
| 92 int flags=CYG_HAL_SYS_MAP_SHARED; | 91 int flags=CYG_HAL_SYS_MAP_SHARED; |
| 93 | 92 |
| 94 priv->flashfd = | 93 priv->flashfd = |
| 95 cyg_hal_sys_open(config->filename, | 94 cyg_hal_sys_open(priv->filename, |
| 96 CYG_HAL_SYS_O_RDWR, | 95 CYG_HAL_SYS_O_RDWR, |
| 97 CYG_HAL_SYS_S_IRWXU|CYG_HAL_SYS_S_IRWXG| | 96 CYG_HAL_SYS_S_IRWXU|CYG_HAL_SYS_S_IRWXG| |
| 98 CYG_HAL_SYS_S_IRWXO); | 97 CYG_HAL_SYS_S_IRWXO); |
| 99 if (priv->flashfd == -ENOENT) { | 98 if (priv->flashfd == -ENOENT) { |
| 100 long w, bytesleft; | 99 long w, bytesleft; |
| 101 char buf[128]; | 100 char buf[128]; |
| 102 | 101 |
| 103 priv->flashfd = cyg_hal_sys_open( | 102 priv->flashfd = cyg_hal_sys_open( |
| 104 config->filename, | 103 priv->filename, |
| 105 CYG_HAL_SYS_O_RDWR|CYG_HAL_SYS_O_CREAT, | 104 CYG_HAL_SYS_O_RDWR|CYG_HAL_SYS_O_CREAT, |
| 106 CYG_HAL_SYS_S_IRWXU|CYG_HAL_SYS_S_IRWXG|CYG_HAL_SYS_S_IRWXO); | 105 CYG_HAL_SYS_S_IRWXU|CYG_HAL_SYS_S_IRWXG|CYG_HAL_SYS_S_IRWXO); |
| 107 CYG_ASSERT( priv->flashfd >= 0, | 106 CYG_ASSERT( priv->flashfd >= 0, |
| 108 "Opening of the file for the synth flash failed!"); | 107 "Opening of the file for the synth flash failed!"); |
| 109 // fill with 0xff | 108 // fill with 0xff |
| 110 memset( buf, 0xff, sizeof(buf) ); | 109 memset( buf, 0xff, sizeof(buf) ); |
| 111 bytesleft = config->block_size * config->blocks + | 110 bytesleft = priv->block_size * priv->blocks + |
| 112 config->boot_block_size * config->boot_blocks; | 111 priv->boot_block_size * priv->boot_blocks; |
| 113 | 112 |
| 114 while (bytesleft > 0) { | 113 while (bytesleft > 0) { |
| 115 int bytesneeded; | 114 int bytesneeded; |
| 116 bytesneeded = bytesleft < sizeof(buf) ? | 115 bytesneeded = bytesleft < sizeof(buf) ? |
| 117 bytesleft : sizeof(buf); | 116 bytesleft : sizeof(buf); |
| 132 if (dev->start != 0) { | 131 if (dev->start != 0) { |
| 133 flags |= CYG_HAL_SYS_MAP_FIXED; | 132 flags |= CYG_HAL_SYS_MAP_FIXED; |
| 134 } | 133 } |
| 135 base = (cyg_flashaddr_t)cyg_hal_sys_do_mmap( | 134 base = (cyg_flashaddr_t)cyg_hal_sys_do_mmap( |
| 136 (void *)dev->start, | 135 (void *)dev->start, |
| 137 config->blocks * config->block_size + | 136 priv->blocks * priv->block_size + |
| 138 config->boot_block_size * config->boot_blocks, | 137 priv->boot_block_size * priv->boot_blocks, |
| 139 CYG_HAL_SYS_PROT_READ, | 138 CYG_HAL_SYS_PROT_READ, |
| 140 flags, | 139 flags, |
| 141 priv->flashfd, | 140 priv->flashfd, |
| 142 0l); | 141 0l); |
| 143 CYG_ASSERT( (int) base > 0, "mmap of flash file failed!" ); | 142 CYG_ASSERT( (int) base > 0, "mmap of flash file failed!" ); |
| 144 if (base <= 0) { | 143 if (base <= 0) { |
| 145 return CYG_FLASH_ERR_HWR; | 144 return CYG_FLASH_ERR_HWR; |
| 146 } | 145 } |
| 147 dev->start = base; | 146 dev->start = base; |
| 148 dev->end = base + (config->blocks * config->block_size) + | 147 dev->end = base + (priv->blocks * priv->block_size) + |
| 149 (config->boot_blocks * config->boot_block_size) - 1; | 148 (priv->boot_blocks * priv->boot_block_size) - 1; |
| 150 if (config->boot_blocks) { | 149 if (priv->boot_blocks) { |
| 151 if (config->boot_block_bottom) { | 150 if (priv->boot_block_bottom) { |
| 152 priv->block_info[0].block_size = config->boot_block_size; | 151 priv->block_info[0].block_size = priv->boot_block_size; |
| 153 priv->block_info[0].blocks = config->boot_blocks; | 152 priv->block_info[0].blocks = priv->boot_blocks; |
| 154 priv->block_info[1].block_size = config->block_size; | 153 priv->block_info[1].block_size = priv->block_size; |
| 155 priv->block_info[1].blocks = config->blocks; | 154 priv->block_info[1].blocks = priv->blocks; |
| 156 } else { | 155 } else { |
| 157 priv->block_info[0].block_size = config->block_size; | 156 priv->block_info[0].block_size = priv->block_size; |
| 158 priv->block_info[0].blocks = config->blocks; | 157 priv->block_info[0].blocks = priv->blocks; |
| 159 priv->block_info[1].block_size = config->boot_block_size; | 158 priv->block_info[1].block_size = priv->boot_block_size; |
| 160 priv->block_info[1].blocks = config->boot_blocks; | 159 priv->block_info[1].blocks = priv->boot_blocks; |
| 161 } | 160 } |
| 162 dev->num_block_infos = 2; | 161 dev->num_block_infos = 2; |
| 163 } else { | 162 } else { |
| 164 priv->block_info[0].block_size = config->block_size; | 163 priv->block_info[0].block_size = priv->block_size; |
| 165 priv->block_info[0].blocks = config->blocks; | 164 priv->block_info[0].blocks = priv->blocks; |
| 166 dev->num_block_infos = 1; | 165 dev->num_block_infos = 1; |
| 167 } | 166 } |
| 168 dev->block_info = &priv->block_info[0]; | 167 dev->block_info = &priv->block_info[0]; |
| 169 | 168 |
| 170 return CYG_FLASH_ERR_OK; | 169 return CYG_FLASH_ERR_OK; |
| 203 return 0; | 202 return 0; |
| 204 } | 203 } |
| 205 | 204 |
| 206 static int | 205 static int |
| 207 synth_flash_erase_block(struct cyg_flash_dev *dev, | 206 synth_flash_erase_block(struct cyg_flash_dev *dev, |
| 208 const cyg_flashaddr_t block_base) | 207 cyg_flashaddr_t block_base) |
| 209 { | 208 { |
| 210 struct cyg_flash_synth_priv *priv = dev->priv; | 209 const struct cyg_flash_synth_priv *priv = dev->priv; |
| 211 int offset = (int)block_base; | 210 int offset = (int)block_base; |
| 212 size_t remaining; | 211 size_t remaining; |
| 213 int write_size; | 212 int write_size; |
| 214 | 213 |
| 215 offset -= dev->start; | 214 offset -= dev->start; |
| 233 } | 232 } |
| 234 | 233 |
| 235 static int | 234 static int |
| 236 synth_flash_program (struct cyg_flash_dev *dev, | 235 synth_flash_program (struct cyg_flash_dev *dev, |
| 237 cyg_flashaddr_t base, | 236 cyg_flashaddr_t base, |
| 238 const void* data, const size_t len) | 237 const void* data, size_t len) |
| 239 { | 238 { |
| 240 struct cyg_flash_synth_priv *priv = dev->priv; | 239 const struct cyg_flash_synth_priv *priv = dev->priv; |
| 241 int offset = base; | 240 int offset = base; |
| 242 offset -= dev->start; | 241 offset -= dev->start; |
| 243 | 242 |
| 244 cyg_hal_sys_lseek(priv->flashfd, offset, CYG_HAL_SYS_SEEK_SET); | 243 cyg_hal_sys_lseek(priv->flashfd, offset, CYG_HAL_SYS_SEEK_SET); |
| 245 cyg_hal_sys_write(priv->flashfd, data, len); | 244 cyg_hal_sys_write(priv->flashfd, data, len); |
| 249 | 248 |
| 250 #define QUERY "Linux Synthetic Flash" | 249 #define QUERY "Linux Synthetic Flash" |
| 251 | 250 |
| 252 static size_t | 251 static size_t |
| 253 synth_flash_query(struct cyg_flash_dev *dev, void * data, | 252 synth_flash_query(struct cyg_flash_dev *dev, void * data, |
| 254 const size_t len) | 253 size_t len) |
| 255 { | 254 { |
| 256 memcpy(data,QUERY,sizeof(QUERY)); | 255 memcpy(data,QUERY,sizeof(QUERY)); |
| 257 return sizeof(QUERY); | 256 return sizeof(QUERY); |
| 258 } | 257 } |
| 259 | 258 |
| 260 static struct cyg_flash_synth_config config = { | 259 // Just in case there is another flash driver which does implement locking |
| 261 CYGNUM_FLASH_SYNTH_V2_BLOCKSIZE, | 260 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING |
| 262 CYGNUM_FLASH_SYNTH_V2_NUMBLOCKS, | 261 static int |
| 263 CYGNUM_FLASH_SYNTH_V2_BOOT_BLOCKSIZE, | 262 synth_flash_lock(struct cyg_flash_dev* dev, |
| 264 CYGNUM_FLASH_SYNTH_V2_NUMBOOT_BLOCKS, | 263 const cyg_flashaddr_t addr) |
| 265 CYGNUM_FLASH_SYNTH_V2_BOOT_BLOCK_BOTTOM, | 264 { |
| 266 CYGDAT_FLASH_SYNTH_V2_FILENAME | 265 CYG_UNUSED_PARAM(struct cyg_flash_dev*, dev); |
| 266 CYG_UNUSED_PARAM(const cyg_flashaddr_t, addr); | |
| 267 return CYG_FLASH_ERR_INVALID; | |
| 268 } | |
| 269 | |
| 270 static int | |
| 271 synth_flash_unlock(struct cyg_flash_dev* dev, | |
| 272 const cyg_flashaddr_t addr) | |
| 273 { | |
| 274 CYG_UNUSED_PARAM(struct cyg_flash_dev*, dev); | |
| 275 CYG_UNUSED_PARAM(const cyg_flashaddr_t, addr); | |
| 276 return CYG_FLASH_ERR_INVALID; | |
| 277 } | |
| 278 #endif | |
| 279 | |
| 280 const CYG_FLASH_FUNS(cyg_flash_synth_funs, | |
| 281 synth_flash_init, | |
| 282 synth_flash_query, | |
| 283 synth_flash_erase_block, | |
| 284 synth_flash_program, | |
| 285 NULL, // read | |
| 286 synth_flash_hwr_map_error, | |
| 287 synth_flash_lock, | |
| 288 synth_flash_unlock); | |
| 289 | |
| 290 static struct cyg_flash_synth_priv synth_flash_priv = { | |
| 291 .block_size = CYGNUM_FLASH_SYNTH_V2_BLOCKSIZE, | |
| 292 .blocks = CYGNUM_FLASH_SYNTH_V2_NUMBLOCKS, | |
| 293 .boot_block_size = CYGNUM_FLASH_SYNTH_V2_BOOT_BLOCKSIZE, | |
| 294 .boot_blocks = CYGNUM_FLASH_SYNTH_V2_NUMBOOT_BLOCKS, | |
| 295 .boot_block_bottom = CYGNUM_FLASH_SYNTH_V2_BOOT_BLOCK_BOTTOM, | |
| 296 .filename = CYGDAT_FLASH_SYNTH_V2_FILENAME, | |
| 297 .flashfd = -1 | |
| 267 }; | 298 }; |
| 268 | |
| 269 CYG_FLASH_FUNS(cyg_flash_synth_funs, | |
| 270 synth_flash_init, | |
| 271 synth_flash_query, | |
| 272 synth_flash_erase_block, | |
| 273 synth_flash_program, | |
| 274 NULL, // read | |
| 275 synth_flash_hwr_map_error, | |
| 276 NULL, // lock | |
| 277 NULL); // unlock | |
| 278 | 299 |
| 279 CYG_FLASH_DRIVER(cyg_flash_synth_flashdev, | 300 CYG_FLASH_DRIVER(cyg_flash_synth_flashdev, |
| 280 &cyg_flash_synth_funs, | 301 &cyg_flash_synth_funs, |
| 281 &config, | 302 0, // flags |
| 282 CYGMEM_FLASH_SYNTH_V2_BASE, | 303 CYGMEM_FLASH_SYNTH_V2_BASE, // Start, if 0 will be updated by init |
| 283 sizeof(struct cyg_flash_synth_priv)); | 304 0, // end, filled in by init |
| 305 0, // number of block_info's, filled in by init | |
| 306 synth_flash_priv.block_info, | |
| 307 &synth_flash_priv); | |
| 284 | 308 |
| 285 // EOF synth.c | 309 // EOF synth.c |
