| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2010, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: $ |
|---|
| 11 | * $brcm_Revision: $ |
|---|
| 12 | * $brcm_Date: $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: flash simulation module in memory |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: $ |
|---|
| 19 | * |
|---|
| 20 | * |
|---|
| 21 | ***************************************************************************/ |
|---|
| 22 | |
|---|
| 23 | #include "bstd.h" |
|---|
| 24 | #include "bdbg.h" |
|---|
| 25 | #include "bkni.h" |
|---|
| 26 | |
|---|
| 27 | #include "bfds.h" |
|---|
| 28 | |
|---|
| 29 | #define BFDS_SECTOR_SIZE 0x1000 |
|---|
| 30 | #define BFDS_PAGE_SIZE 0x100 |
|---|
| 31 | #define BFDS_SECTOR_MASK (~(BFDS_SECTOR_SIZE - 1)) |
|---|
| 32 | |
|---|
| 33 | struct bfds_state { |
|---|
| 34 | void * storage; |
|---|
| 35 | unsigned size; |
|---|
| 36 | }; |
|---|
| 37 | |
|---|
| 38 | static struct bfds_state mems; |
|---|
| 39 | |
|---|
| 40 | int bfds_open(bfds_handle * handle) |
|---|
| 41 | { |
|---|
| 42 | int res; |
|---|
| 43 | |
|---|
| 44 | BKNI_Memset(&mems, 0, sizeof(struct bfds_state)); |
|---|
| 45 | //mems.size = BFDS_SECTOR_SIZE * 4 * 16; /* 16 sectors of flash */ |
|---|
| 46 | mems.size = 0x400000 * 2; /* 16 sectors of flash */ |
|---|
| 47 | mems.storage = BKNI_Malloc(mems.size); |
|---|
| 48 | if(NULL == mems.storage){ |
|---|
| 49 | res = BFDS_ERROR; |
|---|
| 50 | goto ExitFunc; |
|---|
| 51 | } |
|---|
| 52 | /* fill memory with 0xff to simulate flash */ |
|---|
| 53 | BKNI_Memset(mems.storage, 0xff, mems.size); |
|---|
| 54 | *handle = &mems; |
|---|
| 55 | res = BFDS_OK; |
|---|
| 56 | ExitFunc: |
|---|
| 57 | return res; |
|---|
| 58 | } |
|---|
| 59 | int bfds_close( |
|---|
| 60 | bfds_handle handle /* [in] device handle */ |
|---|
| 61 | ) |
|---|
| 62 | { |
|---|
| 63 | if (mems.storage) |
|---|
| 64 | { |
|---|
| 65 | BKNI_Free(mems.storage); |
|---|
| 66 | mems.storage = NULL; |
|---|
| 67 | } |
|---|
| 68 | return BFDS_OK; |
|---|
| 69 | } |
|---|
| 70 | int bfds_read(bfds_handle handle, unsigned offset, void * data, unsigned size) |
|---|
| 71 | { |
|---|
| 72 | void * pdata; |
|---|
| 73 | BDBG_ASSERT(NULL != handle); |
|---|
| 74 | pdata = (void*)((unsigned)(handle->storage) + offset); |
|---|
| 75 | BKNI_Memcpy(data, pdata, size); |
|---|
| 76 | return BFDS_OK; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | int bfds_sector_erase(bfds_handle handle, unsigned offset) |
|---|
| 80 | { |
|---|
| 81 | void * erase_addr; |
|---|
| 82 | BDBG_ASSERT(NULL != handle); |
|---|
| 83 | erase_addr = (void *)((unsigned)(handle->storage) + (offset & BFDS_SECTOR_MASK)); |
|---|
| 84 | BKNI_Memset(erase_addr, 0xff, BFDS_SECTOR_SIZE); |
|---|
| 85 | return BFDS_OK; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | int bfds_page_program(bfds_handle handle, unsigned offset, void * data, unsigned size) |
|---|
| 89 | { |
|---|
| 90 | void * page_addr; |
|---|
| 91 | unsigned remainder; |
|---|
| 92 | |
|---|
| 93 | BDBG_ASSERT(NULL != handle); |
|---|
| 94 | |
|---|
| 95 | page_addr = (void *)((unsigned)(handle->storage) + offset); |
|---|
| 96 | remainder = BFDS_PAGE_SIZE - (offset & (BFDS_PAGE_SIZE - 1)); |
|---|
| 97 | if(size > remainder){ |
|---|
| 98 | /* If write is not page aligned we need to chop size, |
|---|
| 99 | in real flash data will wrap to the beggining of the page*/ |
|---|
| 100 | BKNI_Fail(); |
|---|
| 101 | } |
|---|
| 102 | BKNI_Memcpy(page_addr, data, size); |
|---|
| 103 | return BFDS_OK; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | int bfds_get_properties ( |
|---|
| 107 | bfds_handle handle, /* [in] device handle */ |
|---|
| 108 | bfds_properties * properties /* [out] details of flash device */ |
|---|
| 109 | ) |
|---|
| 110 | { |
|---|
| 111 | int res; |
|---|
| 112 | if(NULL == properties){ |
|---|
| 113 | res = BFDS_ERROR; |
|---|
| 114 | goto ExitFunc; |
|---|
| 115 | } |
|---|
| 116 | properties->flash_sector_size = BFDS_SECTOR_SIZE; |
|---|
| 117 | properties->flash_page_size = BFDS_PAGE_SIZE; |
|---|
| 118 | res = BFDS_OK; |
|---|
| 119 | ExitFunc: |
|---|
| 120 | return res; |
|---|
| 121 | } |
|---|