onenand_spl.c 4.8 KB

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