s10_memmap_qspi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <platform_def.h>
  9. #include <common/debug.h>
  10. #include <drivers/io/io_driver.h>
  11. #include <drivers/io/io_memmap.h>
  12. #include <drivers/io/io_storage.h>
  13. #include <lib/utils.h>
  14. #include "qspi/cadence_qspi.h"
  15. /* As we need to be able to keep state for seek, only one file can be open
  16. * at a time. Make this a structure and point to the entity->info. When we
  17. * can malloc memory we can change this to support more open files.
  18. */
  19. typedef struct {
  20. /* Use the 'in_use' flag as any value for base and file_pos could be
  21. * valid.
  22. */
  23. int in_use;
  24. uintptr_t base;
  25. unsigned long long file_pos;
  26. unsigned long long size;
  27. } file_state_t;
  28. static file_state_t current_file = {0};
  29. /* Identify the device type as memmap */
  30. static io_type_t device_type_memmap(void)
  31. {
  32. return IO_TYPE_MEMMAP;
  33. }
  34. /* Memmap device functions */
  35. static int memmap_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
  36. static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
  37. io_entity_t *entity);
  38. static int memmap_block_seek(io_entity_t *entity, int mode,
  39. signed long long offset);
  40. static int memmap_block_len(io_entity_t *entity, size_t *length);
  41. static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
  42. size_t length, size_t *length_read);
  43. static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
  44. size_t length, size_t *length_written);
  45. static int memmap_block_close(io_entity_t *entity);
  46. static int memmap_dev_close(io_dev_info_t *dev_info);
  47. static const io_dev_connector_t memmap_dev_connector = {
  48. .dev_open = memmap_dev_open
  49. };
  50. static const io_dev_funcs_t memmap_dev_funcs = {
  51. .type = device_type_memmap,
  52. .open = memmap_block_open,
  53. .seek = memmap_block_seek,
  54. .size = memmap_block_len,
  55. .read = memmap_block_read,
  56. .write = memmap_block_write,
  57. .close = memmap_block_close,
  58. .dev_init = NULL,
  59. .dev_close = memmap_dev_close,
  60. };
  61. /* No state associated with this device so structure can be const */
  62. static const io_dev_info_t memmap_dev_info = {
  63. .funcs = &memmap_dev_funcs,
  64. .info = (uintptr_t)NULL
  65. };
  66. /* Open a connection to the memmap device */
  67. static int memmap_dev_open(const uintptr_t dev_spec __unused,
  68. io_dev_info_t **dev_info)
  69. {
  70. assert(dev_info != NULL);
  71. *dev_info = (io_dev_info_t *)&memmap_dev_info; /* cast away const */
  72. return 0;
  73. }
  74. /* Close a connection to the memmap device */
  75. static int memmap_dev_close(io_dev_info_t *dev_info)
  76. {
  77. /* NOP */
  78. /* TODO: Consider tracking open files and cleaning them up here */
  79. return 0;
  80. }
  81. /* Open a file on the memmap device */
  82. static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
  83. io_entity_t *entity)
  84. {
  85. int result = -ENOMEM;
  86. const io_block_spec_t *block_spec = (io_block_spec_t *)spec;
  87. /* Since we need to track open state for seek() we only allow one open
  88. * spec at a time. When we have dynamic memory we can malloc and set
  89. * entity->info.
  90. */
  91. if (current_file.in_use == 0) {
  92. assert(block_spec != NULL);
  93. assert(entity != NULL);
  94. current_file.in_use = 1;
  95. current_file.base = block_spec->offset;
  96. /* File cursor offset for seek and incremental reads etc. */
  97. current_file.file_pos = 0;
  98. current_file.size = block_spec->length;
  99. entity->info = (uintptr_t)&current_file;
  100. result = 0;
  101. } else {
  102. WARN("A Memmap device is already active. Close first.\n");
  103. }
  104. return result;
  105. }
  106. /* Seek to a particular file offset on the memmap device */
  107. static int memmap_block_seek(io_entity_t *entity, int mode,
  108. signed long long offset)
  109. {
  110. int result = -ENOENT;
  111. file_state_t *fp;
  112. /* We only support IO_SEEK_SET for the moment. */
  113. if (mode == IO_SEEK_SET) {
  114. assert(entity != NULL);
  115. fp = (file_state_t *) entity->info;
  116. /* Assert that new file position is valid */
  117. assert((offset >= 0) &&
  118. ((unsigned long long)offset < fp->size));
  119. /* Reset file position */
  120. fp->file_pos = offset;
  121. result = 0;
  122. }
  123. return result;
  124. }
  125. /* Return the size of a file on the memmap device */
  126. static int memmap_block_len(io_entity_t *entity, size_t *length)
  127. {
  128. assert(entity != NULL);
  129. assert(length != NULL);
  130. *length = ((file_state_t *)entity->info)->size;
  131. return 0;
  132. }
  133. /* Read data from a file on the memmap device */
  134. static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
  135. size_t length, size_t *length_read)
  136. {
  137. file_state_t *fp;
  138. unsigned long long pos_after;
  139. assert(entity != NULL);
  140. assert(length_read != NULL);
  141. fp = (file_state_t *) entity->info;
  142. /* Assert that file position is valid for this read operation */
  143. pos_after = fp->file_pos + length;
  144. assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
  145. //memcpy((void *)buffer, (void *)(fp->base + fp->file_pos), length);
  146. cad_qspi_read((void *)buffer, fp->base + fp->file_pos, length);
  147. *length_read = length;
  148. /* Set file position after read */
  149. fp->file_pos = pos_after;
  150. return 0;
  151. }
  152. /* Write data to a file on the memmap device */
  153. static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
  154. size_t length, size_t *length_written)
  155. {
  156. file_state_t *fp;
  157. unsigned long long pos_after;
  158. assert(entity != NULL);
  159. assert(length_written != NULL);
  160. fp = (file_state_t *) entity->info;
  161. /* Assert that file position is valid for this write operation */
  162. pos_after = fp->file_pos + length;
  163. assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
  164. memcpy((void *)(fp->base + fp->file_pos), (void *)buffer, length);
  165. *length_written = length;
  166. /* Set file position after write */
  167. fp->file_pos = pos_after;
  168. return 0;
  169. }
  170. /* Close a file on the memmap device */
  171. static int memmap_block_close(io_entity_t *entity)
  172. {
  173. assert(entity != NULL);
  174. entity->info = 0;
  175. /* This would be a mem free() if we had malloc.*/
  176. zeromem((void *)&current_file, sizeof(current_file));
  177. return 0;
  178. }
  179. /* Exported functions */
  180. /* Register the memmap driver with the IO abstraction */
  181. int register_io_dev_memmap(const io_dev_connector_t **dev_con)
  182. {
  183. int result;
  184. assert(dev_con != NULL);
  185. result = io_register_device(&memmap_dev_info);
  186. if (result == 0)
  187. *dev_con = &memmap_dev_connector;
  188. return result;
  189. }