onenand_read.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * (C) Copyright 2005-2009 Samsung Electronics
  3. * Kyungmin Park <kyungmin.park@samsung.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/io.h>
  25. #include <asm/string.h>
  26. #include "onenand_ipl.h"
  27. #define onenand_block_address(block) (block)
  28. #define onenand_sector_address(page) (page << 2)
  29. #define onenand_buffer_address() ((1 << 3) << 8)
  30. #define onenand_bufferram_address(block) (0)
  31. #ifdef __HAVE_ARCH_MEMCPY32
  32. extern void *memcpy32(void *dest, void *src, int size);
  33. #endif
  34. int (*onenand_read_page)(ulong block, ulong page, u_char *buf, int pagesize);
  35. /* read a page with ECC */
  36. static int generic_onenand_read_page(ulong block, ulong page,
  37. u_char * buf, int pagesize)
  38. {
  39. unsigned long *base;
  40. #ifndef __HAVE_ARCH_MEMCPY32
  41. unsigned int offset, value;
  42. unsigned long *p;
  43. #endif
  44. onenand_writew(onenand_block_address(block),
  45. ONENAND_REG_START_ADDRESS1);
  46. onenand_writew(onenand_bufferram_address(block),
  47. ONENAND_REG_START_ADDRESS2);
  48. onenand_writew(onenand_sector_address(page),
  49. ONENAND_REG_START_ADDRESS8);
  50. onenand_writew(onenand_buffer_address(),
  51. ONENAND_REG_START_BUFFER);
  52. onenand_writew(ONENAND_INT_CLEAR, ONENAND_REG_INTERRUPT);
  53. onenand_writew(ONENAND_CMD_READ, ONENAND_REG_COMMAND);
  54. #ifndef __HAVE_ARCH_MEMCPY32
  55. p = (unsigned long *) buf;
  56. #endif
  57. base = (unsigned long *) (CONFIG_SYS_ONENAND_BASE + ONENAND_DATARAM);
  58. while (!(READ_INTERRUPT() & ONENAND_INT_READ))
  59. continue;
  60. /* Check for invalid block mark */
  61. if (page < 2 && (onenand_readw(ONENAND_SPARERAM) != 0xffff))
  62. return 1;
  63. #ifdef __HAVE_ARCH_MEMCPY32
  64. /* 32 bytes boundary memory copy */
  65. memcpy32(buf, base, pagesize);
  66. #else
  67. for (offset = 0; offset < (pagesize >> 2); offset++) {
  68. value = *(base + offset);
  69. *p++ = value;
  70. }
  71. #endif
  72. return 0;
  73. }
  74. #ifndef CONFIG_ONENAND_START_PAGE
  75. #define CONFIG_ONENAND_START_PAGE 1
  76. #endif
  77. #define ONENAND_PAGES_PER_BLOCK 64
  78. static void onenand_generic_init(int *page_is_4KiB, int *page)
  79. {
  80. int dev_id, density;
  81. if (onenand_readw(ONENAND_REG_TECHNOLOGY))
  82. *page_is_4KiB = 1;
  83. dev_id = onenand_readw(ONENAND_REG_DEVICE_ID);
  84. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  85. density &= ONENAND_DEVICE_DENSITY_MASK;
  86. if (density >= ONENAND_DEVICE_DENSITY_4Gb &&
  87. !(dev_id & ONENAND_DEVICE_IS_DDP))
  88. *page_is_4KiB = 1;
  89. }
  90. /**
  91. * onenand_read_block - Read CONFIG_SYS_MONITOR_LEN from begining
  92. * of OneNAND, skipping bad blocks
  93. * @return 0 on success
  94. */
  95. int onenand_read_block(unsigned char *buf)
  96. {
  97. int block, nblocks;
  98. int page = CONFIG_ONENAND_START_PAGE, offset = 0;
  99. int pagesize, erasesize, erase_shift;
  100. int page_is_4KiB = 0;
  101. onenand_read_page = generic_onenand_read_page;
  102. onenand_generic_init(&page_is_4KiB, &page);
  103. if (page_is_4KiB) {
  104. pagesize = 4096; /* OneNAND has 4KiB pagesize */
  105. erase_shift = 18;
  106. } else {
  107. pagesize = 2048; /* OneNAND has 2KiB pagesize */
  108. erase_shift = 17;
  109. }
  110. erasesize = (1 << erase_shift);
  111. nblocks = (CONFIG_SYS_MONITOR_LEN + erasesize - 1) >> erase_shift;
  112. /* NOTE: you must read page from page 1 of block 0 */
  113. /* read the block page by page */
  114. for (block = 0; block < nblocks; block++) {
  115. for (; page < ONENAND_PAGES_PER_BLOCK; page++) {
  116. if (onenand_read_page(block, page, buf + offset,
  117. pagesize)) {
  118. /* This block is bad. Skip it
  119. * and read next block */
  120. offset -= page * pagesize;
  121. nblocks++;
  122. break;
  123. }
  124. offset += pagesize;
  125. }
  126. page = 0;
  127. }
  128. return 0;
  129. }