Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <io_storage.h> |
| 9 | #include <platform.h> |
| 10 | #include <platform_def.h> |
| 11 | #include <spinlock.h> |
| 12 | #include <status.h> |
| 13 | #include <string.h> |
| 14 | #include <tftf_lib.h> |
| 15 | |
| 16 | #if USE_NVM |
| 17 | /* Used to serialize write operations from different CPU's */ |
| 18 | static spinlock_t flash_access_lock; |
| 19 | #endif |
| 20 | |
| 21 | STATUS tftf_nvm_write(unsigned long long offset, const void *buffer, size_t size) |
| 22 | { |
| 23 | #if USE_NVM |
| 24 | int ret; |
| 25 | uintptr_t nvm_handle; |
| 26 | size_t length_written; |
| 27 | #endif |
| 28 | |
| 29 | if (offset + size > TFTF_NVM_SIZE) |
| 30 | return STATUS_OUT_OF_RESOURCES; |
| 31 | |
| 32 | #if USE_NVM |
| 33 | /* Obtain a handle to the NVM by querying the platfom layer */ |
| 34 | plat_get_nvm_handle(&nvm_handle); |
| 35 | |
| 36 | spin_lock(&flash_access_lock); |
| 37 | |
| 38 | ret = io_seek(nvm_handle, IO_SEEK_SET, |
| 39 | offset + TFTF_NVM_OFFSET); |
| 40 | if (ret != IO_SUCCESS) |
| 41 | goto fail; |
| 42 | |
| 43 | ret = io_write(nvm_handle, (const uintptr_t)buffer, size, |
| 44 | &length_written); |
| 45 | if (ret != IO_SUCCESS) |
| 46 | goto fail; |
| 47 | |
| 48 | assert(length_written == size); |
| 49 | fail: |
| 50 | spin_unlock(&flash_access_lock); |
| 51 | |
| 52 | if (ret != IO_SUCCESS) |
| 53 | return STATUS_FAIL; |
| 54 | |
| 55 | #else |
| 56 | uintptr_t addr = DRAM_BASE + TFTF_NVM_OFFSET + offset; |
| 57 | memcpy((void *)addr, buffer, size); |
| 58 | #endif |
| 59 | |
| 60 | return STATUS_SUCCESS; |
| 61 | } |
| 62 | |
| 63 | STATUS tftf_nvm_read(unsigned long long offset, void *buffer, size_t size) |
| 64 | { |
| 65 | #if USE_NVM |
| 66 | int ret; |
| 67 | uintptr_t nvm_handle; |
| 68 | size_t length_read; |
| 69 | #endif |
| 70 | |
| 71 | if (offset + size > TFTF_NVM_SIZE) |
| 72 | return STATUS_OUT_OF_RESOURCES; |
| 73 | |
| 74 | #if USE_NVM |
| 75 | /* Obtain a handle to the NVM by querying the platfom layer */ |
| 76 | plat_get_nvm_handle(&nvm_handle); |
| 77 | |
| 78 | spin_lock(&flash_access_lock); |
| 79 | |
| 80 | ret = io_seek(nvm_handle, IO_SEEK_SET, TFTF_NVM_OFFSET + offset); |
| 81 | if (ret != IO_SUCCESS) |
| 82 | goto fail; |
| 83 | |
| 84 | ret = io_read(nvm_handle, (uintptr_t)buffer, size, &length_read); |
| 85 | if (ret != IO_SUCCESS) |
| 86 | goto fail; |
| 87 | |
| 88 | assert(length_read == size); |
| 89 | fail: |
| 90 | spin_unlock(&flash_access_lock); |
| 91 | |
| 92 | if (ret != IO_SUCCESS) |
| 93 | return STATUS_FAIL; |
| 94 | #else |
| 95 | uintptr_t addr = DRAM_BASE + TFTF_NVM_OFFSET + offset; |
| 96 | memcpy(buffer, (void *)addr, size); |
| 97 | #endif |
| 98 | |
| 99 | return STATUS_SUCCESS; |
| 100 | } |
| 101 | |