bulk_process_nand.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <stdlib.h>
  2. #include <app.h>
  3. #include <debug.h>
  4. #include <arch/arm.h>
  5. #include <dev/udc.h>
  6. #include <string.h>
  7. #include <kernel/thread.h>
  8. #include <kernel/event.h>
  9. #include <arch/ops.h>
  10. #include <target.h>
  11. #include <platform.h>
  12. #include <platform/mt_gpt.h>
  13. #include "bulk_process.h"
  14. #include "pal_log.h"
  15. #if (!defined(MTK_UFS_SUPPORT) && !defined(MTK_EMMC_SUPPORT))
  16. #define NAND_TYPE
  17. #if defined(MTK_MLC_NAND_SUPPORT) || defined(MTK_TLC_NAND_SUPPORT)
  18. extern int nand_write_img(u64 addr, void *data, u32 img_sz,u64 partition_size,int partition_type);
  19. extern int nand_write_img_ex(u64 addr, void *data, u32 length,u64 total_size, u32 *next_offset, u64 partition_start,u64 partition_size, int img_type);
  20. #else
  21. extern int nand_write_img(u32 addr, void *data, u32 img_sz,u32 partition_size,int partition_type);
  22. extern int nand_write_img_ex(u32 addr, void *data, u32 length,u32 total_size, u32 *next_offset, u32 partition_start,u32 partition_size, int img_type);
  23. #endif
  24. #if 0 //fake NAND.
  25. int nand_get_alignment()
  26. {
  27. return 4*1024*1024;
  28. }
  29. int nand_write_img(u32 addr, void *data, u32 img_sz,u32 partition_size,int partition_type)
  30. {
  31. return 0;
  32. }
  33. int get_nand_image_type(const char* arg)
  34. {
  35. return 0;
  36. }
  37. #endif
  38. static void init_cache_context(bulk_status_t* status);
  39. static void write_bulk_internal(bulk_status_t* status, bool flush_end);
  40. void init_bulk_process_status(bulk_status_t* status, partition_info_struct_t* partition)
  41. {
  42. status->partition = partition;
  43. status->image_address_offset = 0;
  44. init_cache_context(status);
  45. }
  46. extern int nand_get_alignment();
  47. static void adjust_cache_size(struct bulk_cache_ctx* ctx)
  48. {
  49. uint32 size = nand_get_alignment();
  50. //ctx->c_base = (uint8*)memalign(128, size);
  51. ctx->c_base = get_global_cache1();
  52. ctx->c_size = size;
  53. ctx->c_offset = 0;
  54. }
  55. static void init_cache_context(bulk_status_t* status)
  56. {
  57. adjust_cache_size(&status->ctx);
  58. }
  59. void end_write_bulk(bulk_status_t* status)
  60. {
  61. write_bulk_internal(status, true);
  62. }
  63. extern int get_nand_image_type(const char* arg);
  64. static void write_bulk_internal(bulk_status_t* status, bool flush_end)
  65. {
  66. uint32 slot_len = status->ctx.c_size - status->ctx.c_offset;
  67. if(slot_len <= status->byte_to_process || flush_end)
  68. {
  69. uint32 actural_size = 0;
  70. //cache is full.
  71. if(flush_end)
  72. {
  73. memset(status->ctx.c_base + status->ctx.c_offset, 0xFF, status->ctx.c_size - status->ctx.c_offset);
  74. actural_size = status->ctx.c_offset;
  75. }
  76. else
  77. {
  78. memcpy(status->ctx.c_base + status->ctx.c_offset, status->buf, slot_len);
  79. actural_size = status->ctx.c_size;
  80. }
  81. if (nand_write_img((u64)(status->partition->base_addr+status->image_address_offset), (char*)status->ctx.c_base, status->ctx.c_size
  82. ,(u64)status->partition->max_size, get_nand_image_type(status->partition->name)))
  83. {
  84. status->handle_status = STATUS_NAND_ERR;
  85. pal_log_err("[BULK] S_STORAGE_WRITE_FAILED:%d, status=%d, size=%d\n",__LINE__, status->handle_status, status->byte_to_process);
  86. return;
  87. }
  88. status->buf += slot_len;
  89. status->byte_to_process -= slot_len;
  90. status->ctx.c_offset = 0;// reset cache.
  91. status->image_address_offset += status->ctx.c_size;
  92. }
  93. else
  94. {
  95. //cache is not full.
  96. memcpy(status->ctx.c_base + status->ctx.c_offset, status->buf, status->byte_to_process);
  97. status->ctx.c_offset += status->byte_to_process;
  98. status->byte_to_process = 0;
  99. }
  100. return ;
  101. }
  102. void write_bulk_data(bulk_status_t* status, uint8* data, uint32 length)
  103. {
  104. status->buf = data;
  105. status->byte_to_process = length;
  106. do
  107. {
  108. write_bulk_internal(status, false);
  109. if (status->handle_status != STATUS_OK)
  110. {
  111. return;
  112. }
  113. } while (status->byte_to_process > 0);
  114. return;
  115. }
  116. #endif