onenand_spl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <asm/io.h>
  28. #include <linux/mtd/onenand_regs.h>
  29. #include <onenand_uboot.h>
  30. /*
  31. * Device geometry:
  32. * - 2048b page, 128k erase block.
  33. * - 4096b page, 256k erase block.
  34. */
  35. enum onenand_spl_pagesize {
  36. PAGE_2K = 2048,
  37. PAGE_4K = 4096,
  38. };
  39. #define ONENAND_PAGES_PER_BLOCK 64
  40. #define onenand_block_address(block) (block)
  41. #define onenand_sector_address(page) (page << 2)
  42. #define onenand_buffer_address() ((1 << 3) << 8)
  43. #define onenand_bufferram_address(block) (0)
  44. static inline uint16_t onenand_readw(uint32_t addr)
  45. {
  46. return readw(CONFIG_SYS_ONENAND_BASE + addr);
  47. }
  48. static inline void onenand_writew(uint16_t value, uint32_t addr)
  49. {
  50. writew(value, CONFIG_SYS_ONENAND_BASE + addr);
  51. }
  52. static enum onenand_spl_pagesize onenand_spl_get_geometry(void)
  53. {
  54. uint32_t dev_id, density;
  55. if (!onenand_readw(ONENAND_REG_TECHNOLOGY)) {
  56. dev_id = onenand_readw(ONENAND_REG_DEVICE_ID);
  57. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  58. density &= ONENAND_DEVICE_DENSITY_MASK;
  59. if (density < ONENAND_DEVICE_DENSITY_4Gb)
  60. return PAGE_2K;
  61. if (dev_id & ONENAND_DEVICE_IS_DDP)
  62. return PAGE_2K;
  63. }
  64. return PAGE_4K;
  65. }
  66. static int onenand_spl_read_page(uint32_t block, uint32_t page, uint32_t *buf,
  67. enum onenand_spl_pagesize pagesize)
  68. {
  69. const uint32_t addr = CONFIG_SYS_ONENAND_BASE + ONENAND_DATARAM;
  70. uint32_t offset;
  71. onenand_writew(onenand_block_address(block),
  72. ONENAND_REG_START_ADDRESS1);
  73. onenand_writew(onenand_bufferram_address(block),
  74. ONENAND_REG_START_ADDRESS2);
  75. onenand_writew(onenand_sector_address(page),
  76. ONENAND_REG_START_ADDRESS8);
  77. onenand_writew(onenand_buffer_address(),
  78. ONENAND_REG_START_BUFFER);
  79. onenand_writew(ONENAND_INT_CLEAR, ONENAND_REG_INTERRUPT);
  80. onenand_writew(ONENAND_CMD_READ, ONENAND_REG_COMMAND);
  81. while (!(onenand_readw(ONENAND_REG_INTERRUPT) & ONENAND_INT_READ))
  82. continue;
  83. /* Check for invalid block mark */
  84. if (page < 2 && (onenand_readw(ONENAND_SPARERAM) != 0xffff))
  85. return 1;
  86. for (offset = 0; offset < pagesize; offset += 4)
  87. buf[offset / 4] = readl(addr + offset);
  88. return 0;
  89. }
  90. void onenand_spl_load_image(uint32_t offs, uint32_t size, void *dst)
  91. {
  92. uint32_t *addr = (uint32_t *)dst;
  93. uint32_t to_page;
  94. uint32_t block;
  95. uint32_t page, rpage;
  96. enum onenand_spl_pagesize pagesize;
  97. int ret;
  98. pagesize = onenand_spl_get_geometry();
  99. /*
  100. * The page can be either 2k or 4k, avoid using DIV_ROUND_UP to avoid
  101. * pulling further unwanted functions into the SPL.
  102. */
  103. if (pagesize == 2048) {
  104. page = offs / 2048;
  105. to_page = page + DIV_ROUND_UP(size, 2048);
  106. } else {
  107. page = offs / 4096;
  108. to_page = page + DIV_ROUND_UP(size, 4096);
  109. }
  110. for (; page <= to_page; page++) {
  111. block = page / ONENAND_PAGES_PER_BLOCK;
  112. rpage = page & (ONENAND_PAGES_PER_BLOCK - 1);
  113. ret = onenand_spl_read_page(block, rpage, addr, pagesize);
  114. if (ret)
  115. page += ONENAND_PAGES_PER_BLOCK - 1;
  116. else
  117. addr += pagesize / 4;
  118. }
  119. }