onenand_read.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * (C) Copyright 2005-2008 Samsung Electronis
  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. /* read a page with ECC */
  35. static inline int onenand_read_page(ulong block, ulong page,
  36. u_char * buf, int pagesize)
  37. {
  38. unsigned long *base;
  39. #ifndef __HAVE_ARCH_MEMCPY32
  40. unsigned int offset, value;
  41. unsigned long *p;
  42. #endif
  43. onenand_writew(onenand_block_address(block),
  44. ONENAND_REG_START_ADDRESS1);
  45. onenand_writew(onenand_bufferram_address(block),
  46. ONENAND_REG_START_ADDRESS2);
  47. onenand_writew(onenand_sector_address(page),
  48. ONENAND_REG_START_ADDRESS8);
  49. onenand_writew(onenand_buffer_address(),
  50. ONENAND_REG_START_BUFFER);
  51. onenand_writew(ONENAND_INT_CLEAR, ONENAND_REG_INTERRUPT);
  52. onenand_writew(ONENAND_CMD_READ, ONENAND_REG_COMMAND);
  53. #ifndef __HAVE_ARCH_MEMCPY32
  54. p = (unsigned long *) buf;
  55. #endif
  56. base = (unsigned long *) (CONFIG_SYS_ONENAND_BASE + ONENAND_DATARAM);
  57. while (!(READ_INTERRUPT() & ONENAND_INT_READ))
  58. continue;
  59. /* Check for invalid block mark */
  60. if (page < 2 && (onenand_readw(ONENAND_SPARERAM) != 0xffff))
  61. return 1;
  62. #ifdef __HAVE_ARCH_MEMCPY32
  63. /* 32 bytes boundary memory copy */
  64. memcpy32(buf, base, pagesize);
  65. #else
  66. for (offset = 0; offset < (pagesize >> 2); offset++) {
  67. value = *(base + offset);
  68. *p++ = value;
  69. }
  70. #endif
  71. return 0;
  72. }
  73. #define ONENAND_START_PAGE 1
  74. #define ONENAND_PAGES_PER_BLOCK 64
  75. /**
  76. * onenand_read_block - Read CONFIG_SYS_MONITOR_LEN from begining
  77. * of OneNAND, skipping bad blocks
  78. * @return 0 on success
  79. */
  80. int onenand_read_block(unsigned char *buf)
  81. {
  82. int block;
  83. int page = ONENAND_START_PAGE, offset = 0;
  84. int pagesize = 0, erase_shift = 0;
  85. int erasesize = 0, nblocks = 0;
  86. if (onenand_readw(ONENAND_REG_TECHNOLOGY)) {
  87. pagesize = 4096; /* MLC OneNAND has 4KiB pagesize */
  88. erase_shift = 18;
  89. } else {
  90. pagesize = 2048;
  91. erase_shift = 17;
  92. }
  93. erasesize = ONENAND_PAGES_PER_BLOCK * pagesize;
  94. nblocks = (CONFIG_SYS_MONITOR_LEN + erasesize - 1) >> erase_shift;
  95. /* NOTE: you must read page from page 1 of block 0 */
  96. /* read the block page by page*/
  97. for (block = 0; block < nblocks; block++) {
  98. for (; page < ONENAND_PAGES_PER_BLOCK; page++) {
  99. if (onenand_read_page(block, page, buf + offset,
  100. pagesize)) {
  101. /* This block is bad. Skip it
  102. * and read next block */
  103. offset -= page * pagesize;
  104. nblocks++;
  105. break;
  106. }
  107. offset += pagesize;
  108. }
  109. page = 0;
  110. }
  111. return 0;
  112. }