flash.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Parallel NOR Flash tests
  3. *
  4. * Copyright (c) 2005-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include <post.h>
  11. #include <flash.h>
  12. #if CONFIG_POST & CONFIG_SYS_POST_FLASH
  13. /*
  14. * This code will walk over the declared sectors erasing them,
  15. * then programming them, then verifying the written contents.
  16. * Possible future work:
  17. * - verify sectors before/after are not erased/written
  18. * - verify partial writes (e.g. programming only middle of sector)
  19. * - verify the contents of the erased sector
  20. * - better seed pattern than 0x00..0xff
  21. */
  22. #ifndef CONFIG_SYS_POST_FLASH_NUM
  23. # define CONFIG_SYS_POST_FLASH_NUM 0
  24. #endif
  25. #if CONFIG_SYS_POST_FLASH_START >= CONFIG_SYS_POST_FLASH_END
  26. # error "invalid flash block start/end"
  27. #endif
  28. extern flash_info_t flash_info[];
  29. static void *seed_src_data(void *ptr, ulong *old_len, ulong new_len)
  30. {
  31. unsigned char *p;
  32. ulong i;
  33. p = ptr = realloc(ptr, new_len);
  34. if (!ptr)
  35. return ptr;
  36. for (i = *old_len; i < new_len; ++i)
  37. p[i] = i;
  38. *old_len = new_len;
  39. return ptr;
  40. }
  41. int flash_post_test(int flags)
  42. {
  43. ulong len;
  44. void *src;
  45. int ret, n, n_start, n_end;
  46. flash_info_t *info;
  47. /* the output from the common flash layers needs help */
  48. puts("\n");
  49. len = 0;
  50. src = NULL;
  51. info = &flash_info[CONFIG_SYS_POST_FLASH_NUM];
  52. n_start = CONFIG_SYS_POST_FLASH_START;
  53. n_end = CONFIG_SYS_POST_FLASH_END;
  54. for (n = n_start; n < n_end; ++n) {
  55. ulong s_start, s_len, s_off;
  56. s_start = info->start[n];
  57. s_len = flash_sector_size(info, n);
  58. s_off = s_start - info->start[0];
  59. src = seed_src_data(src, &len, s_len);
  60. if (!src) {
  61. printf("malloc(%#lx) failed\n", s_len);
  62. return 1;
  63. }
  64. printf("\tsector %i: %#lx +%#lx", n, s_start, s_len);
  65. ret = flash_erase(info, n, n + 1);
  66. if (ret) {
  67. flash_perror(ret);
  68. break;
  69. }
  70. ret = write_buff(info, src, s_start, s_len);
  71. if (ret) {
  72. flash_perror(ret);
  73. break;
  74. }
  75. ret = memcmp(src, (void *)s_start, s_len);
  76. if (ret) {
  77. printf(" verify failed with %i\n", ret);
  78. break;
  79. }
  80. }
  81. free(src);
  82. return ret;
  83. }
  84. #endif