Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 1 | /* |
Kyungmin Park | afb72e4 | 2009-09-22 09:05:00 +0900 | [diff] [blame] | 2 | * (C) Copyright 2005-2009 Samsung Electronics |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 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 | afb72e4 | 2009-09-22 09:05:00 +0900 | [diff] [blame] | 40 | int (*onenand_read_page)(ulong block, ulong page, u_char *buf, int pagesize); |
| 41 | |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 42 | /* read a page with ECC */ |
Kyungmin Park | afb72e4 | 2009-09-22 09:05:00 +0900 | [diff] [blame] | 43 | static int generic_onenand_read_page(ulong block, ulong page, |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 44 | u_char * buf, int pagesize) |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 45 | { |
| 46 | unsigned long *base; |
| 47 | |
| 48 | #ifndef __HAVE_ARCH_MEMCPY32 |
| 49 | unsigned int offset, value; |
| 50 | unsigned long *p; |
| 51 | #endif |
| 52 | |
| 53 | onenand_writew(onenand_block_address(block), |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 54 | ONENAND_REG_START_ADDRESS1); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 55 | |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 56 | onenand_writew(onenand_bufferram_address(block), |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 57 | ONENAND_REG_START_ADDRESS2); |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 58 | |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 59 | onenand_writew(onenand_sector_address(page), |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 60 | ONENAND_REG_START_ADDRESS8); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 61 | |
| 62 | onenand_writew(onenand_buffer_address(), |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 63 | ONENAND_REG_START_BUFFER); |
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_INT_CLEAR, ONENAND_REG_INTERRUPT); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 66 | |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 67 | onenand_writew(ONENAND_CMD_READ, ONENAND_REG_COMMAND); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 68 | |
| 69 | #ifndef __HAVE_ARCH_MEMCPY32 |
| 70 | p = (unsigned long *) buf; |
| 71 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 72 | base = (unsigned long *) (CONFIG_SYS_ONENAND_BASE + ONENAND_DATARAM); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 73 | |
| 74 | while (!(READ_INTERRUPT() & ONENAND_INT_READ)) |
| 75 | continue; |
| 76 | |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 77 | /* Check for invalid block mark */ |
| 78 | if (page < 2 && (onenand_readw(ONENAND_SPARERAM) != 0xffff)) |
| 79 | return 1; |
| 80 | |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 81 | #ifdef __HAVE_ARCH_MEMCPY32 |
| 82 | /* 32 bytes boundary memory copy */ |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 83 | memcpy32(buf, base, pagesize); |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 84 | #else |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 85 | for (offset = 0; offset < (pagesize >> 2); offset++) { |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 86 | value = *(base + offset); |
| 87 | *p++ = value; |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Kyungmin Park | afb72e4 | 2009-09-22 09:05:00 +0900 | [diff] [blame] | 94 | #ifndef CONFIG_ONENAND_START_PAGE |
| 95 | #define CONFIG_ONENAND_START_PAGE 1 |
| 96 | #endif |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 97 | #define ONENAND_PAGES_PER_BLOCK 64 |
| 98 | |
Kyungmin Park | afb72e4 | 2009-09-22 09:05:00 +0900 | [diff] [blame] | 99 | static void onenand_generic_init(int *page_is_4KiB, int *page) |
| 100 | { |
| 101 | int dev_id, density; |
| 102 | |
| 103 | if (onenand_readw(ONENAND_REG_TECHNOLOGY)) |
| 104 | *page_is_4KiB = 1; |
| 105 | dev_id = onenand_readw(ONENAND_REG_DEVICE_ID); |
| 106 | density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT; |
| 107 | density &= ONENAND_DEVICE_DENSITY_MASK; |
| 108 | if (density >= ONENAND_DEVICE_DENSITY_4Gb && |
| 109 | !(dev_id & ONENAND_DEVICE_IS_DDP)) |
| 110 | *page_is_4KiB = 1; |
| 111 | } |
| 112 | |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 113 | /** |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 114 | * onenand_read_block - Read CONFIG_SYS_MONITOR_LEN from begining |
| 115 | * of OneNAND, skipping bad blocks |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 116 | * @return 0 on success |
| 117 | */ |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 118 | int onenand_read_block(unsigned char *buf) |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 119 | { |
Kyungmin Park | afb72e4 | 2009-09-22 09:05:00 +0900 | [diff] [blame] | 120 | int block, nblocks; |
| 121 | int page = CONFIG_ONENAND_START_PAGE, offset = 0; |
| 122 | int pagesize, erasesize, erase_shift; |
| 123 | int page_is_4KiB = 0; |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 124 | |
Kyungmin Park | afb72e4 | 2009-09-22 09:05:00 +0900 | [diff] [blame] | 125 | onenand_read_page = generic_onenand_read_page; |
| 126 | |
| 127 | onenand_generic_init(&page_is_4KiB, &page); |
| 128 | |
| 129 | if (page_is_4KiB) { |
| 130 | pagesize = 4096; /* OneNAND has 4KiB pagesize */ |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 131 | erase_shift = 18; |
| 132 | } else { |
Kyungmin Park | afb72e4 | 2009-09-22 09:05:00 +0900 | [diff] [blame] | 133 | pagesize = 2048; /* OneNAND has 2KiB pagesize */ |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 134 | erase_shift = 17; |
| 135 | } |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 136 | |
Kyungmin Park | afb72e4 | 2009-09-22 09:05:00 +0900 | [diff] [blame] | 137 | erasesize = (1 << erase_shift); |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 138 | nblocks = (CONFIG_SYS_MONITOR_LEN + erasesize - 1) >> erase_shift; |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 139 | |
| 140 | /* NOTE: you must read page from page 1 of block 0 */ |
Kyungmin Park | afb72e4 | 2009-09-22 09:05:00 +0900 | [diff] [blame] | 141 | /* read the block page by page */ |
apgmoorthy | 75dad9b | 2009-03-27 14:45:23 +0530 | [diff] [blame] | 142 | for (block = 0; block < nblocks; block++) { |
| 143 | for (; page < ONENAND_PAGES_PER_BLOCK; page++) { |
| 144 | if (onenand_read_page(block, page, buf + offset, |
| 145 | pagesize)) { |
| 146 | /* This block is bad. Skip it |
| 147 | * and read next block */ |
| 148 | offset -= page * pagesize; |
| 149 | nblocks++; |
| 150 | break; |
| 151 | } |
| 152 | offset += pagesize; |
| 153 | } |
| 154 | page = 0; |
Kyungmin Park | 3317421 | 2008-01-17 16:43:25 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |