Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 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 | |
| 24 | #include <common.h> |
| 25 | |
| 26 | #include <asm/io.h> |
| 27 | #include <asm/string.h> |
| 28 | |
| 29 | #include "onenand_ipl.h" |
| 30 | |
| 31 | #define onenand_block_address(block) (block) |
| 32 | #define onenand_sector_address(page) (page << 2) |
| 33 | #define onenand_buffer_address() ((1 << 3) << 8) |
| 34 | #define onenand_bufferram_address(block) (0) |
| 35 | |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 36 | #ifdef __HAVE_ARCH_MEMCPY32 |
| 37 | extern void *memcpy32(void *dest, void *src, int size); |
| 38 | #endif |
| 39 | |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 40 | /* read a page with ECC */ |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 41 | static inline int onenand_read_page(ulong block, ulong page, |
| 42 | u_char * buf, int pagesize) |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 43 | { |
| 44 | unsigned long *base; |
| 45 | |
| 46 | #ifndef __HAVE_ARCH_MEMCPY32 |
| 47 | unsigned int offset, value; |
| 48 | unsigned long *p; |
| 49 | #endif |
| 50 | |
| 51 | onenand_writew(onenand_block_address(block), |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 52 | ONENAND_REG_START_ADDRESS1); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 53 | |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 54 | onenand_writew(onenand_bufferram_address(block), |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 55 | ONENAND_REG_START_ADDRESS2); |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 56 | |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 57 | onenand_writew(onenand_sector_address(page), |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 58 | ONENAND_REG_START_ADDRESS8); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 59 | |
| 60 | onenand_writew(onenand_buffer_address(), |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 61 | ONENAND_REG_START_BUFFER); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 62 | |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 63 | onenand_writew(ONENAND_INT_CLEAR, ONENAND_REG_INTERRUPT); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 64 | |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 65 | onenand_writew(ONENAND_CMD_READ, ONENAND_REG_COMMAND); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 66 | |
| 67 | #ifndef __HAVE_ARCH_MEMCPY32 |
| 68 | p = (unsigned long *) buf; |
| 69 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 70 | base = (unsigned long *) (CONFIG_SYS_ONENAND_BASE + ONENAND_DATARAM); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 71 | |
| 72 | while (!(READ_INTERRUPT() & ONENAND_INT_READ)) |
| 73 | continue; |
| 74 | |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 75 | /* Check for invalid block mark */ |
| 76 | if (page < 2 && (onenand_readw(ONENAND_SPARERAM) != 0xffff)) |
| 77 | return 1; |
| 78 | |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 79 | #ifdef __HAVE_ARCH_MEMCPY32 |
| 80 | /* 32 bytes boundary memory copy */ |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 81 | memcpy32(buf, base, pagesize); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 82 | #else |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 83 | for (offset = 0; offset < (pagesize >> 2); offset++) { |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 84 | value = *(base + offset); |
| 85 | *p++ = value; |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | #define ONENAND_START_PAGE 1 |
| 93 | #define ONENAND_PAGES_PER_BLOCK 64 |
| 94 | |
| 95 | /** |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 96 | * onenand_read_block - Read CONFIG_SYS_MONITOR_LEN from begining |
| 97 | * of OneNAND, skipping bad blocks |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 98 | * @return 0 on success |
| 99 | */ |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 100 | int onenand_read_block(unsigned char *buf) |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 101 | { |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 102 | int block; |
| 103 | int page = ONENAND_START_PAGE, offset = 0; |
| 104 | int pagesize = 0, erase_shift = 0; |
| 105 | int erasesize = 0, nblocks = 0; |
| 106 | |
| 107 | if (onenand_readw(ONENAND_REG_TECHNOLOGY)) { |
| 108 | pagesize = 4096; /* MLC OneNAND has 4KiB pagesize */ |
| 109 | erase_shift = 18; |
| 110 | } else { |
| 111 | pagesize = 2048; |
| 112 | erase_shift = 17; |
| 113 | } |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 114 | |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 115 | erasesize = ONENAND_PAGES_PER_BLOCK * pagesize; |
| 116 | nblocks = (CONFIG_SYS_MONITOR_LEN + erasesize - 1) >> erase_shift; |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 117 | |
| 118 | /* NOTE: you must read page from page 1 of block 0 */ |
| 119 | /* read the block page by page*/ |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 120 | for (block = 0; block < nblocks; block++) { |
| 121 | for (; page < ONENAND_PAGES_PER_BLOCK; page++) { |
| 122 | if (onenand_read_page(block, page, buf + offset, |
| 123 | pagesize)) { |
| 124 | /* This block is bad. Skip it |
| 125 | * and read next block */ |
| 126 | offset -= page * pagesize; |
| 127 | nblocks++; |
| 128 | break; |
| 129 | } |
| 130 | offset += pagesize; |
| 131 | } |
| 132 | page = 0; |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |