wrlock.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <pthread.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <hal_timer.h>
  8. #include <hal_cmd.h>
  9. #include "inter.h"
  10. #define NOR_CMD_BLK_LOCK_ALL (0x7E)
  11. #define NOR_CMD_BLK_UNLOCK_ALL (0x98)
  12. /* #define DEBUG_WRLOCK */
  13. #ifdef DEBUG_WRLOCK
  14. int skip_lock = 0;
  15. #endif
  16. int nor_wr_lock_init(struct nor_flash *nor)
  17. {
  18. if (nor->factory->init_lock)
  19. return nor->factory->init_lock(nor);
  20. return 0;
  21. }
  22. void nor_wr_lock_deinit(struct nor_flash *nor)
  23. {
  24. if (nor->factory->deinit_lock)
  25. nor->factory->deinit_lock(nor);
  26. }
  27. bool nor_wr_islock(struct nor_flash *nor, unsigned int addr, unsigned int len)
  28. {
  29. unsigned int align_size;
  30. unsigned int align_addr;
  31. bool ret = false;
  32. if (nor->factory->islock) {
  33. while (len) {
  34. /* the first and the last block (64K) is lock on sector (4K) unit */
  35. if (addr >= nor->total_size - SZ_64K || addr < SZ_64K)
  36. align_size = SZ_4K;
  37. else
  38. align_size = SZ_64K;
  39. align_addr = ALIGN_DOWN(addr, align_size);
  40. ret = nor->factory->islock(nor, align_addr, align_size);
  41. if (ret)
  42. break;
  43. len -= MIN(align_size, len);
  44. addr += MIN(align_size, len);
  45. }
  46. }
  47. return ret;
  48. }
  49. int nor_wr_lock(struct nor_flash *nor, unsigned int addr, unsigned int len)
  50. {
  51. unsigned int align_size;
  52. unsigned int align_addr;
  53. int ret = 0;
  54. #ifdef DEBUG_WRLOCK
  55. if (skip_lock == 1) {
  56. printf("skip handle lock\n");
  57. return 0;
  58. }
  59. #endif
  60. if (nor->factory->lock) {
  61. while (len) {
  62. /* the first and the last block (64K) is lock on sector (4K) unit */
  63. if (addr >= nor->total_size - SZ_64K || addr < SZ_64K)
  64. align_size = SZ_4K;
  65. else
  66. align_size = SZ_64K;
  67. align_addr = ALIGN_DOWN(addr, align_size);
  68. ret = nor->factory->lock(nor, align_addr, align_size);
  69. if (ret)
  70. break;
  71. len -= MIN(align_size, len);
  72. addr += MIN(align_size, len);
  73. }
  74. }
  75. return ret;
  76. }
  77. int nor_wr_unlock(struct nor_flash *nor, unsigned int addr, unsigned int len)
  78. {
  79. unsigned int align_size;
  80. unsigned int align_addr;
  81. int ret = 0;
  82. #ifdef DEBUG_WRLOCK
  83. if (skip_lock == 1) {
  84. printf("skip handle lock\n");
  85. return 0;
  86. }
  87. #endif
  88. if (nor->factory->unlock) {
  89. while (len) {
  90. /* the first and the last block (64K) is lock on sector (4K) unit */
  91. if (addr >= nor->total_size - SZ_64K || addr < SZ_64K)
  92. align_size = SZ_4K;
  93. else
  94. align_size = SZ_64K;
  95. align_addr = ALIGN_DOWN(addr, align_size);
  96. ret = nor->factory->unlock(nor, align_addr, align_size);
  97. if (ret)
  98. break;
  99. len -= MIN(align_size, len);
  100. addr += MIN(align_size, len);
  101. }
  102. }
  103. return ret;
  104. }
  105. int nor_wr_unlock_all(struct nor_flash *nor)
  106. {
  107. int ret;
  108. #ifdef DEBUG_WRLOCK
  109. if (skip_lock == 1) {
  110. printf("skip handle lock\n");
  111. return 0;
  112. }
  113. #endif
  114. ret = nor_write_enable();
  115. if (ret)
  116. return ret;
  117. ret = nor_send_cmd(NOR_CMD_BLK_UNLOCK_ALL);
  118. if (ret) {
  119. SPINOR_ERR("unlock all block failed - %d\n", ret);
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. int nor_wr_lock_all(struct nor_flash *nor)
  125. {
  126. int ret;
  127. #ifdef DEBUG_WRLOCK
  128. if (skip_lock == 1) {
  129. printf("skip handle lock\n");
  130. return 0;
  131. }
  132. #endif
  133. ret = nor_write_enable();
  134. if (ret)
  135. return ret;
  136. ret = nor_send_cmd(NOR_CMD_BLK_LOCK_ALL);
  137. if (ret) {
  138. SPINOR_ERR("lock all block failed - %d\n", ret);
  139. return ret;
  140. }
  141. return 0;
  142. }
  143. #ifdef DEBUG_WRLOCK
  144. int cmd_skip_lock(int argc, char ** argv)
  145. {
  146. printf("skip lock\n");
  147. skip_lock = 1;
  148. }
  149. FINSH_FUNCTION_EXPORT_CMD(cmd_skip_lock, skip_lock, skip lock)
  150. int cmd_not_skip_lock(int argc, char ** argv)
  151. {
  152. printf("not skip lock\n");
  153. skip_lock = 0;
  154. }
  155. FINSH_FUNCTION_EXPORT_CMD(cmd_not_skip_lock, not_skip_lock, not skip lock)
  156. #endif