onenand_spl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  3. *
  4. * Based on code:
  5. * Copyright (C) 2005-2009 Samsung Electronics
  6. * Kyungmin Park <kyungmin.park@samsung.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <asm/io.h>
  12. #include <linux/mtd/onenand_regs.h>
  13. #include <onenand_uboot.h>
  14. /*
  15. * Device geometry:
  16. * - 2048b page, 128k erase block.
  17. * - 4096b page, 256k erase block.
  18. */
  19. enum onenand_spl_pagesize {
  20. PAGE_2K = 2048,
  21. PAGE_4K = 4096,
  22. };
  23. #define ONENAND_PAGES_PER_BLOCK 64
  24. #define onenand_block_address(block) (block)
  25. #define onenand_sector_address(page) (page << 2)
  26. #define onenand_buffer_address() ((1 << 3) << 8)
  27. #define onenand_bufferram_address(block) (0)
  28. static inline uint16_t onenand_readw(uint32_t addr)
  29. {
  30. return readw(CONFIG_SYS_ONENAND_BASE + addr);
  31. }
  32. static inline void onenand_writew(uint16_t value, uint32_t addr)
  33. {
  34. writew(value, CONFIG_SYS_ONENAND_BASE + addr);
  35. }
  36. static enum onenand_spl_pagesize onenand_spl_get_geometry(void)
  37. {
  38. uint32_t dev_id, density;
  39. if (!onenand_readw(ONENAND_REG_TECHNOLOGY)) {
  40. dev_id = onenand_readw(ONENAND_REG_DEVICE_ID);
  41. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  42. density &= ONENAND_DEVICE_DENSITY_MASK;
  43. if (density < ONENAND_DEVICE_DENSITY_4Gb)
  44. return PAGE_2K;
  45. if (dev_id & ONENAND_DEVICE_IS_DDP)
  46. return PAGE_2K;
  47. }
  48. return PAGE_4K;
  49. }
  50. static int onenand_spl_read_page(uint32_t block, uint32_t page, uint32_t *buf,
  51. enum onenand_spl_pagesize pagesize)
  52. {
  53. const uint32_t addr = CONFIG_SYS_ONENAND_BASE + ONENAND_DATARAM;
  54. uint32_t offset;
  55. onenand_writew(onenand_block_address(block),
  56. ONENAND_REG_START_ADDRESS1);
  57. onenand_writew(onenand_bufferram_address(block),
  58. ONENAND_REG_START_ADDRESS2);
  59. onenand_writew(onenand_sector_address(page),
  60. ONENAND_REG_START_ADDRESS8);
  61. onenand_writew(onenand_buffer_address(),
  62. ONENAND_REG_START_BUFFER);
  63. onenand_writew(ONENAND_INT_CLEAR, ONENAND_REG_INTERRUPT);
  64. onenand_writew(ONENAND_CMD_READ, ONENAND_REG_COMMAND);
  65. while (!(onenand_readw(ONENAND_REG_INTERRUPT) & ONENAND_INT_READ))
  66. continue;
  67. /* Check for invalid block mark */
  68. if (page < 2 && (onenand_readw(ONENAND_SPARERAM) != 0xffff))
  69. return 1;
  70. for (offset = 0; offset < pagesize; offset += 4)
  71. buf[offset / 4] = readl(addr + offset);
  72. return 0;
  73. }
  74. #ifdef CONFIG_SPL_UBI
  75. /* Temporary storage for non page aligned and non page sized reads. */
  76. static u8 scratch_buf[PAGE_4K];
  77. /**
  78. * onenand_spl_read_block - Read data from physical eraseblock into a buffer
  79. * @block: Number of the physical eraseblock
  80. * @offset: Data offset from the start of @peb
  81. * @len: Data size to read
  82. * @dst: Address of the destination buffer
  83. *
  84. * Notes:
  85. * @offset + @len are not allowed to be larger than a physical
  86. * erase block. No sanity check done for simplicity reasons.
  87. */
  88. int onenand_spl_read_block(int block, int offset, int len, void *dst)
  89. {
  90. int page, read, psize;
  91. psize = onenand_spl_get_geometry();
  92. /* Calculate the page number */
  93. page = offset / psize;
  94. /* Offset to the start of a flash page */
  95. offset = offset % psize;
  96. while (len) {
  97. /*
  98. * Non page aligned reads go to the scratch buffer.
  99. * Page aligned reads go directly to the destination.
  100. */
  101. if (offset || len < psize) {
  102. onenand_spl_read_page(block, page,
  103. (uint32_t *)scratch_buf, psize);
  104. read = min(len, psize - offset);
  105. memcpy(dst, scratch_buf + offset, read);
  106. offset = 0;
  107. } else {
  108. onenand_spl_read_page(block, page, dst, psize);
  109. read = psize;
  110. }
  111. page++;
  112. len -= read;
  113. dst += read;
  114. }
  115. return 0;
  116. }
  117. #endif
  118. void onenand_spl_load_image(uint32_t offs, uint32_t size, void *dst)
  119. {
  120. uint32_t *addr = (uint32_t *)dst;
  121. uint32_t to_page;
  122. uint32_t block;
  123. uint32_t page, rpage;
  124. enum onenand_spl_pagesize pagesize;
  125. int ret;
  126. pagesize = onenand_spl_get_geometry();
  127. /*
  128. * The page can be either 2k or 4k, avoid using DIV_ROUND_UP to avoid
  129. * pulling further unwanted functions into the SPL.
  130. */
  131. if (pagesize == 2048) {
  132. page = offs / 2048;
  133. to_page = page + DIV_ROUND_UP(size, 2048);
  134. } else {
  135. page = offs / 4096;
  136. to_page = page + DIV_ROUND_UP(size, 4096);
  137. }
  138. for (; page <= to_page; page++) {
  139. block = page / ONENAND_PAGES_PER_BLOCK;
  140. rpage = page & (ONENAND_PAGES_PER_BLOCK - 1);
  141. ret = onenand_spl_read_page(block, rpage, addr, pagesize);
  142. if (ret)
  143. page += ONENAND_PAGES_PER_BLOCK - 1;
  144. else
  145. addr += pagesize / 4;
  146. }
  147. }