nand_spl_loaders.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifdef CONFIG_SPL_UBI
  39. /*
  40. * Temporary storage for non NAND page aligned and non NAND page sized
  41. * reads. Note: This does not support runtime detected FLASH yet, but
  42. * that should be reasonably easy to fix by making the buffer large
  43. * enough :)
  44. */
  45. static u8 scratch_buf[CONFIG_SYS_NAND_PAGE_SIZE];
  46. /**
  47. * nand_spl_read_block - Read data from physical eraseblock into a buffer
  48. * @block: Number of the physical eraseblock
  49. * @offset: Data offset from the start of @peb
  50. * @len: Data size to read
  51. * @dst: Address of the destination buffer
  52. *
  53. * This could be further optimized if we'd have a subpage read
  54. * function in the simple code. On NAND which allows subpage reads
  55. * this would spare quite some time to readout e.g. the VID header of
  56. * UBI.
  57. *
  58. * Notes:
  59. * @offset + @len are not allowed to be larger than a physical
  60. * erase block. No sanity check done for simplicity reasons.
  61. *
  62. * To support runtime detected flash this needs to be extended by
  63. * information about the actual flash geometry, but thats beyond the
  64. * scope of this effort and for most applications where fast boot is
  65. * required it is not an issue anyway.
  66. */
  67. int nand_spl_read_block(int block, int offset, int len, void *dst)
  68. {
  69. int page, read;
  70. /* Calculate the page number */
  71. page = offset / CONFIG_SYS_NAND_PAGE_SIZE;
  72. /* Offset to the start of a flash page */
  73. offset = offset % CONFIG_SYS_NAND_PAGE_SIZE;
  74. while (len) {
  75. /*
  76. * Non page aligned reads go to the scratch buffer.
  77. * Page aligned reads go directly to the destination.
  78. */
  79. if (offset || len < CONFIG_SYS_NAND_PAGE_SIZE) {
  80. nand_read_page(block, page, scratch_buf);
  81. read = min(len, CONFIG_SYS_NAND_PAGE_SIZE - offset);
  82. memcpy(dst, scratch_buf + offset, read);
  83. offset = 0;
  84. } else {
  85. nand_read_page(block, page, dst);
  86. read = CONFIG_SYS_NAND_PAGE_SIZE;
  87. }
  88. page++;
  89. len -= read;
  90. dst += read;
  91. }
  92. return 0;
  93. }
  94. #endif