nand_spl_loaders.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
  2. {
  3. unsigned int block, lastblock;
  4. unsigned int page, page_offset;
  5. /* offs has to be aligned to a page address! */
  6. block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
  7. lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
  8. page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
  9. page_offset = offs % CONFIG_SYS_NAND_PAGE_SIZE;
  10. while (block <= lastblock) {
  11. if (!nand_is_bad_block(block)) {
  12. /* Skip bad blocks */
  13. while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
  14. nand_read_page(block, page, dst);
  15. /*
  16. * When offs is not aligned to page address the
  17. * extra offset is copied to dst as well. Copy
  18. * the image such that its first byte will be
  19. * at the dst.
  20. */
  21. if (unlikely(page_offset)) {
  22. memmove(dst, dst + page_offset,
  23. CONFIG_SYS_NAND_PAGE_SIZE);
  24. dst = (void *)((int)dst - page_offset);
  25. page_offset = 0;
  26. }
  27. dst += CONFIG_SYS_NAND_PAGE_SIZE;
  28. page++;
  29. }
  30. page = 0;
  31. } else {
  32. lastblock++;
  33. }
  34. block++;
  35. }
  36. return 0;
  37. }
  38. /**
  39. * nand_spl_adjust_offset - Adjust offset from a starting sector
  40. * @sector: Address of the sector
  41. * @offs: Offset starting from @sector
  42. *
  43. * If one or more bad blocks are in the address space between @sector
  44. * and @sector + @offs, @offs is increased by the NAND block size for
  45. * each bad block found.
  46. */
  47. u32 nand_spl_adjust_offset(u32 sector, u32 offs)
  48. {
  49. unsigned int block, lastblock;
  50. block = sector / CONFIG_SYS_NAND_BLOCK_SIZE;
  51. lastblock = (sector + offs) / CONFIG_SYS_NAND_BLOCK_SIZE;
  52. while (block <= lastblock) {
  53. if (nand_is_bad_block(block)) {
  54. offs += CONFIG_SYS_NAND_BLOCK_SIZE;
  55. lastblock++;
  56. }
  57. block++;
  58. }
  59. return offs;
  60. }
  61. #ifdef CONFIG_SPL_UBI
  62. /*
  63. * Temporary storage for non NAND page aligned and non NAND page sized
  64. * reads. Note: This does not support runtime detected FLASH yet, but
  65. * that should be reasonably easy to fix by making the buffer large
  66. * enough :)
  67. */
  68. static u8 scratch_buf[CONFIG_SYS_NAND_PAGE_SIZE];
  69. /**
  70. * nand_spl_read_block - Read data from physical eraseblock into a buffer
  71. * @block: Number of the physical eraseblock
  72. * @offset: Data offset from the start of @peb
  73. * @len: Data size to read
  74. * @dst: Address of the destination buffer
  75. *
  76. * This could be further optimized if we'd have a subpage read
  77. * function in the simple code. On NAND which allows subpage reads
  78. * this would spare quite some time to readout e.g. the VID header of
  79. * UBI.
  80. *
  81. * Notes:
  82. * @offset + @len are not allowed to be larger than a physical
  83. * erase block. No sanity check done for simplicity reasons.
  84. *
  85. * To support runtime detected flash this needs to be extended by
  86. * information about the actual flash geometry, but thats beyond the
  87. * scope of this effort and for most applications where fast boot is
  88. * required it is not an issue anyway.
  89. */
  90. int nand_spl_read_block(int block, int offset, int len, void *dst)
  91. {
  92. int page, read;
  93. /* Calculate the page number */
  94. page = offset / CONFIG_SYS_NAND_PAGE_SIZE;
  95. /* Offset to the start of a flash page */
  96. offset = offset % CONFIG_SYS_NAND_PAGE_SIZE;
  97. while (len) {
  98. /*
  99. * Non page aligned reads go to the scratch buffer.
  100. * Page aligned reads go directly to the destination.
  101. */
  102. if (offset || len < CONFIG_SYS_NAND_PAGE_SIZE) {
  103. nand_read_page(block, page, scratch_buf);
  104. read = min(len, CONFIG_SYS_NAND_PAGE_SIZE - offset);
  105. memcpy(dst, scratch_buf + offset, read);
  106. offset = 0;
  107. } else {
  108. nand_read_page(block, page, dst);
  109. read = CONFIG_SYS_NAND_PAGE_SIZE;
  110. }
  111. page++;
  112. len -= read;
  113. dst += read;
  114. }
  115. return 0;
  116. }
  117. #endif