Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008, Google Inc. |
| 3 | * All rights reserved. |
Aparna Mallavarapu | c5946f2 | 2013-04-08 21:44:51 +0530 | [diff] [blame] | 4 | * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved. |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 12 | * the documentation and/or other materials provided with the |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <debug.h> |
| 30 | #include <reg.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <dev/flash.h> |
| 34 | #include <lib/ptable.h> |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 35 | #include <nand.h> |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 36 | |
| 37 | #include "dmov.h" |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 38 | |
| 39 | #define VERBOSE 0 |
Dima Zavin | 5582c7d | 2009-03-03 15:17:59 -0800 | [diff] [blame] | 40 | #define VERIFY_WRITE 0 |
| 41 | |
| 42 | static void *flash_spare; |
| 43 | static void *flash_data; |
Chandan Uddaraju | 14e57eb | 2010-06-28 12:11:06 -0700 | [diff] [blame] | 44 | void platform_config_interleaved_mode_gpios(void); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 45 | |
| 46 | typedef struct dmov_ch dmov_ch; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 47 | struct dmov_ch { |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 48 | volatile unsigned cmd; |
| 49 | volatile unsigned result; |
| 50 | volatile unsigned status; |
| 51 | volatile unsigned config; |
| 52 | }; |
| 53 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 54 | static void dmov_prep_ch(dmov_ch * ch, unsigned id) |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 55 | { |
| 56 | ch->cmd = DMOV_CMD_PTR(id); |
| 57 | ch->result = DMOV_RSLT(id); |
| 58 | ch->status = DMOV_STATUS(id); |
| 59 | ch->config = DMOV_CONFIG(id); |
| 60 | } |
| 61 | |
| 62 | #define SRC_CRCI_NAND_CMD CMD_SRC_CRCI(DMOV_NAND_CRCI_CMD) |
| 63 | #define DST_CRCI_NAND_CMD CMD_DST_CRCI(DMOV_NAND_CRCI_CMD) |
| 64 | #define SRC_CRCI_NAND_DATA CMD_SRC_CRCI(DMOV_NAND_CRCI_DATA) |
| 65 | #define DST_CRCI_NAND_DATA CMD_DST_CRCI(DMOV_NAND_CRCI_DATA) |
| 66 | |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 67 | #define CFG0_RAW 0xA80420C0 |
| 68 | #define CFG1_RAW 0x5045D |
| 69 | |
| 70 | #define CFG0_RAW_BCHECC 0xA80428C0 |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 71 | |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 72 | static unsigned CFG0, CFG1; |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 73 | static unsigned CFG0_M, CFG1_M; |
| 74 | static unsigned CFG0_A, CFG1_A; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 75 | static unsigned NAND_CFG0_RAW, NAND_CFG1_RAW; |
| 76 | static unsigned ECC_BCH_CFG; |
| 77 | |
| 78 | static uint32_t enable_bch_ecc; |
Channagoud Kadabi | b2fb6ba | 2011-07-29 19:19:01 +0530 | [diff] [blame] | 79 | static unsigned int *bbtbl; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 80 | |
| 81 | #define CFG1_WIDE_FLASH (1U << 1) |
| 82 | |
| 83 | #define paddr(n) ((unsigned) (n)) |
| 84 | |
| 85 | static int dmov_exec_cmdptr(unsigned id, unsigned *ptr) |
| 86 | { |
| 87 | dmov_ch ch; |
| 88 | unsigned n; |
| 89 | |
| 90 | dmov_prep_ch(&ch, id); |
| 91 | |
Kinson Chik | 1f46116 | 2011-09-07 15:07:50 -0700 | [diff] [blame] | 92 | /* Set IRQ_EN low, not using IRQ mode */ |
| 93 | writel(DMOV_CONFIG_FOREC_FLUSH_RSLT | 0x0, ch.config); |
| 94 | |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 95 | writel(DMOV_CMD_PTR_LIST | DMOV_CMD_ADDR(paddr(ptr)), ch.cmd); |
| 96 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 97 | while (!(readl(ch.status) & DMOV_STATUS_RSLT_VALID)) ; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 98 | |
| 99 | n = readl(ch.status); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 100 | while (DMOV_STATUS_RSLT_COUNT(n)) { |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 101 | n = readl(ch.result); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 102 | if (n != 0x80000002) { |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 103 | dprintf(CRITICAL, "ERROR: result: %x\n", n); |
| 104 | dprintf(CRITICAL, "ERROR: flush: %x %x %x %x\n", |
| 105 | readl(DMOV_FLUSH0(DMOV_NAND_CHAN)), |
| 106 | readl(DMOV_FLUSH1(DMOV_NAND_CHAN)), |
| 107 | readl(DMOV_FLUSH2(DMOV_NAND_CHAN)), |
| 108 | readl(DMOV_FLUSH3(DMOV_NAND_CHAN))); |
| 109 | } |
| 110 | n = readl(ch.status); |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Dima Zavin | ca337f5 | 2009-03-02 16:41:44 -0800 | [diff] [blame] | 116 | static struct flash_info flash_info; |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 117 | static unsigned flash_pagesize = 0; |
Chandan Uddaraju | 14e57eb | 2010-06-28 12:11:06 -0700 | [diff] [blame] | 118 | static int interleaved_mode = 0; |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 119 | static unsigned num_pages_per_blk = 0; |
| 120 | static unsigned num_pages_per_blk_mask = 0; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 121 | |
Shashank Mittal | 83d16d0 | 2009-11-18 16:54:42 -0800 | [diff] [blame] | 122 | struct flash_identification { |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 123 | unsigned flash_id; |
| 124 | unsigned mask; |
| 125 | unsigned density; |
| 126 | unsigned widebus; |
| 127 | unsigned pagesize; |
| 128 | unsigned blksize; |
| 129 | unsigned oobsize; |
| 130 | unsigned onenand; |
Shashank Mittal | 83d16d0 | 2009-11-18 16:54:42 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 133 | static struct flash_identification supported_flash[] = { |
| 134 | /* Flash ID ID Mask Density(MB) Wid Pgsz Blksz oobsz onenand Manuf */ |
| 135 | {0x00000000, 0xFFFFFFFF, 0, 0, 0, 0, 0, 0}, /*ONFI*/ {0x1500aaec, 0xFF00FFFF, (256 << 20), 0, 2048, (2048 << 6), 64, 0}, /*Sams */ |
| 136 | {0x5500baec, 0xFF00FFFF, (256 << 20), 1, 2048, (2048 << 6), 64, 0}, /*Sams */ |
| 137 | {0x1500aa98, 0xFFFFFFFF, (256 << 20), 0, 2048, (2048 << 6), 64, 0}, /*Tosh */ |
| 138 | {0x5500ba98, 0xFFFFFFFF, (256 << 20), 1, 2048, (2048 << 6), 64, 0}, /*Tosh */ |
| 139 | {0xd580b12c, 0xFFFFFFFF, (256 << 20), 1, 2048, (2048 << 6), 64, 0}, /*Micr */ |
| 140 | {0x5590bc2c, 0xFFFFFFFF, (512 << 20), 1, 2048, (2048 << 6), 64, 0}, /*Micr */ |
| 141 | {0x1580aa2c, 0xFFFFFFFF, (256 << 20), 0, 2048, (2048 << 6), 64, 0}, /*Micr */ |
| 142 | {0x1590aa2c, 0xFFFFFFFF, (256 << 20), 0, 2048, (2048 << 6), 64, 0}, /*Micr */ |
| 143 | {0x1590ac2c, 0xFFFFFFFF, (512 << 20), 0, 2048, (2048 << 6), 64, 0}, /*Micr */ |
| 144 | {0x5580baad, 0xFFFFFFFF, (256 << 20), 1, 2048, (2048 << 6), 64, 0}, /*Hynx */ |
| 145 | {0x5510baad, 0xFFFFFFFF, (256 << 20), 1, 2048, (2048 << 6), 64, 0}, /*Hynx */ |
| 146 | {0x004000ec, 0xFFFFFFFF, (256 << 20), 0, 2048, (2048 << 6), 64, 1}, /*Sams */ |
| 147 | {0x005c00ec, 0xFFFFFFFF, (256 << 20), 0, 2048, (2048 << 6), 64, 1}, /*Sams */ |
| 148 | {0x005800ec, 0xFFFFFFFF, (256 << 20), 0, 2048, (2048 << 6), 64, 1}, /*Sams */ |
| 149 | {0x6600bcec, 0xFF00FFFF, (512 << 20), 1, 4096, (4096 << 6), 128, 0}, /*Sams */ |
| 150 | {0x5580ba2c, 0xFFFFFFFF, (256 << 20), 1, 2048, (2048 << 6), 64, 0}, /*Hynx */ |
Channagoud Kadabi | 71bcec2 | 2012-01-09 19:22:52 +0530 | [diff] [blame] | 151 | {0x6600b3ec, 0xFFFFFFFF, (1024 << 20), 1, 4096, (4096 << 6), 128, 0}, /*Sams */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 152 | {0x2600482c, 0xFF00FFFF, (2048 << 20), 0, 4096, (4096 << 7), 224, 0}, /*8bit bch ecc */ |
Channagoud Kadabi | 7a6ded7 | 2012-07-02 15:32:21 +0530 | [diff] [blame] | 153 | {0x55d1b32c, 0xFFFFFFFF, (1024 << 20), 1, 2048, (2048 << 6), 64, 0}, /*Micr */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 154 | /* Note: Width flag is 0 for 8 bit Flash and 1 for 16 bit flash */ |
| 155 | /* Note: Onenand flag is 0 for NAND Flash and 1 for OneNAND flash */ |
| 156 | /* Note: The First row will be filled at runtime during ONFI probe */ |
Shashank Mittal | 83d16d0 | 2009-11-18 16:54:42 -0800 | [diff] [blame] | 157 | }; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 158 | |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 159 | static void set_nand_configuration(char type) |
| 160 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 161 | if (type == TYPE_MODEM_PARTITION) { |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 162 | CFG0 = CFG0_M; |
| 163 | CFG1 = CFG1_M; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 164 | } else { |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 165 | CFG0 = CFG0_A; |
| 166 | CFG1 = CFG1_A; |
| 167 | } |
| 168 | } |
Shashank Mittal | 83d16d0 | 2009-11-18 16:54:42 -0800 | [diff] [blame] | 169 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 170 | static void flash_nand_read_id(dmov_s * cmdlist, unsigned *ptrlist) |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 171 | { |
| 172 | dmov_s *cmd = cmdlist; |
| 173 | unsigned *ptr = ptrlist; |
| 174 | unsigned *data = ptrlist + 4; |
| 175 | |
| 176 | data[0] = 0 | 4; |
| 177 | data[1] = NAND_CMD_FETCH_ID; |
| 178 | data[2] = 1; |
| 179 | data[3] = 0; |
| 180 | data[4] = 0; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 181 | data[5] = 0; |
| 182 | data[6] = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 183 | data[7] = 0xAAD40000; /* Default value for CFG0 for reading device id */ |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 184 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 185 | /* Read NAND device id */ |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 186 | cmd[0].cmd = 0 | CMD_OCB; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 187 | cmd[0].src = paddr(&data[7]); |
| 188 | cmd[0].dst = NAND_DEV0_CFG0; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 189 | cmd[0].len = 4; |
| 190 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 191 | cmd[1].cmd = 0; |
| 192 | cmd[1].src = NAND_SFLASHC_BURST_CFG; |
| 193 | cmd[1].dst = paddr(&data[5]); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 194 | cmd[1].len = 4; |
| 195 | |
| 196 | cmd[2].cmd = 0; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 197 | cmd[2].src = paddr(&data[6]); |
| 198 | cmd[2].dst = NAND_SFLASHC_BURST_CFG; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 199 | cmd[2].len = 4; |
| 200 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 201 | cmd[3].cmd = 0; |
| 202 | cmd[3].src = paddr(&data[0]); |
| 203 | cmd[3].dst = NAND_FLASH_CHIP_SELECT; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 204 | cmd[3].len = 4; |
| 205 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 206 | cmd[4].cmd = DST_CRCI_NAND_CMD; |
| 207 | cmd[4].src = paddr(&data[1]); |
| 208 | cmd[4].dst = NAND_FLASH_CMD; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 209 | cmd[4].len = 4; |
| 210 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 211 | cmd[5].cmd = 0; |
| 212 | cmd[5].src = paddr(&data[2]); |
| 213 | cmd[5].dst = NAND_EXEC_CMD; |
| 214 | cmd[5].len = 4; |
| 215 | |
| 216 | cmd[6].cmd = SRC_CRCI_NAND_DATA; |
| 217 | cmd[6].src = NAND_FLASH_STATUS; |
| 218 | cmd[6].dst = paddr(&data[3]); |
| 219 | cmd[6].len = 4; |
| 220 | |
| 221 | cmd[7].cmd = 0; |
| 222 | cmd[7].src = NAND_READ_ID; |
| 223 | cmd[7].dst = paddr(&data[4]); |
| 224 | cmd[7].len = 4; |
| 225 | |
| 226 | cmd[8].cmd = CMD_OCU | CMD_LC; |
| 227 | cmd[8].src = paddr(&data[5]); |
| 228 | cmd[8].dst = NAND_SFLASHC_BURST_CFG; |
| 229 | cmd[8].len = 4; |
| 230 | |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 231 | ptr[0] = (paddr(cmd) >> 3) | CMD_PTR_LP; |
| 232 | |
| 233 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 234 | |
| 235 | #if VERBOSE |
| 236 | dprintf(INFO, "status: %x\n", data[3]); |
| 237 | #endif |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 238 | |
Shashank Mittal | 83d16d0 | 2009-11-18 16:54:42 -0800 | [diff] [blame] | 239 | flash_info.id = data[4]; |
Dima Zavin | ca337f5 | 2009-03-02 16:41:44 -0800 | [diff] [blame] | 240 | flash_info.vendor = data[4] & 0xff; |
| 241 | flash_info.device = (data[4] >> 8) & 0xff; |
Shashank Mittal | 83d16d0 | 2009-11-18 16:54:42 -0800 | [diff] [blame] | 242 | return; |
| 243 | } |
Dima Zavin | ca337f5 | 2009-03-02 16:41:44 -0800 | [diff] [blame] | 244 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 245 | static int |
| 246 | flash_nand_block_isbad(dmov_s * cmdlist, unsigned *ptrlist, unsigned page) |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 247 | { |
| 248 | dmov_s *cmd = cmdlist; |
| 249 | unsigned *ptr = ptrlist; |
| 250 | unsigned *data = ptrlist + 4; |
| 251 | char buf[4]; |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 252 | unsigned cwperpage; |
| 253 | |
| 254 | cwperpage = (flash_pagesize >> 9); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 255 | |
| 256 | /* Check first page of this block */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 257 | if (page & num_pages_per_blk_mask) |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 258 | page = page - (page & num_pages_per_blk_mask); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 259 | |
| 260 | /* Check bad block marker */ |
| 261 | data[0] = NAND_CMD_PAGE_READ; /* command */ |
| 262 | |
| 263 | /* addr0 */ |
| 264 | if (CFG1 & CFG1_WIDE_FLASH) |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 265 | data[1] = enable_bch_ecc ? |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 266 | ((page << 16) | ((532 * (cwperpage - 1)) >> 1)) : |
| 267 | ((page << 16) | ((528 * (cwperpage - 1)) >> 1)); |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 268 | |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 269 | else |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 270 | data[1] = enable_bch_ecc ? |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 271 | ((page << 16) | (532 * (cwperpage - 1))) : |
| 272 | ((page << 16) | (528 * (cwperpage - 1))); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 273 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 274 | data[2] = (page >> 16) & 0xff; /* addr1 */ |
| 275 | data[3] = 0 | 4; /* chipsel */ |
| 276 | data[4] = NAND_CFG0_RAW & ~(7U << 6); /* cfg0 */ |
| 277 | data[5] = NAND_CFG1_RAW | (CFG1 & CFG1_WIDE_FLASH); /* cfg1 */ |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 278 | if (enable_bch_ecc) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 279 | data[6] = ECC_BCH_CFG; /* ECC CFG */ |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 280 | } |
| 281 | data[7] = 1; |
| 282 | data[8] = CLEAN_DATA_32; /* flash status */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 283 | data[9] = CLEAN_DATA_32; /* buf status */ |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 284 | |
| 285 | cmd[0].cmd = DST_CRCI_NAND_CMD | CMD_OCB; |
| 286 | cmd[0].src = paddr(&data[0]); |
| 287 | cmd[0].dst = NAND_FLASH_CMD; |
| 288 | cmd[0].len = 16; |
| 289 | |
| 290 | cmd[1].cmd = 0; |
| 291 | cmd[1].src = paddr(&data[4]); |
| 292 | cmd[1].dst = NAND_DEV0_CFG0; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 293 | if (enable_bch_ecc) { |
| 294 | cmd[1].len = 12; |
| 295 | } else { |
| 296 | cmd[1].len = 8; |
| 297 | } |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 298 | |
| 299 | cmd[2].cmd = 0; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 300 | cmd[2].src = paddr(&data[7]); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 301 | cmd[2].dst = NAND_EXEC_CMD; |
| 302 | cmd[2].len = 4; |
| 303 | |
| 304 | cmd[3].cmd = SRC_CRCI_NAND_DATA; |
| 305 | cmd[3].src = NAND_FLASH_STATUS; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 306 | cmd[3].dst = paddr(&data[8]); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 307 | cmd[3].len = 8; |
| 308 | |
| 309 | cmd[4].cmd = CMD_OCU | CMD_LC; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 310 | cmd[4].src = NAND_FLASH_BUFFER + (flash_pagesize - (enable_bch_ecc ? |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 311 | (532 * |
| 312 | (cwperpage - |
| 313 | 1)) : (528 * |
| 314 | (cwperpage |
| 315 | - 1)))); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 316 | cmd[4].dst = paddr(&buf); |
| 317 | cmd[4].len = 4; |
| 318 | |
| 319 | ptr[0] = (paddr(cmd) >> 3) | CMD_PTR_LP; |
| 320 | |
| 321 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 322 | |
| 323 | #if VERBOSE |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 324 | dprintf(INFO, "status: %x\n", data[8]); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 325 | #endif |
| 326 | |
| 327 | /* we fail if there was an operation error, a mpu error, or the |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 328 | ** erase success bit was not set. |
| 329 | */ |
| 330 | if (data[8] & 0x110) |
| 331 | return -1; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 332 | |
| 333 | /* Check for bad block marker byte */ |
| 334 | if (CFG1 & CFG1_WIDE_FLASH) { |
| 335 | if (buf[0] != 0xFF || buf[1] != 0xFF) |
| 336 | return 1; |
| 337 | } else { |
| 338 | if (buf[0] != 0xFF) |
| 339 | return 1; |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 345 | static int |
| 346 | flash_nand_block_isbad_interleave(dmov_s * cmdlist, unsigned *ptrlist, |
| 347 | unsigned page) |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 348 | { |
| 349 | dmov_s *cmd = cmdlist; |
| 350 | unsigned *ptr = ptrlist; |
| 351 | unsigned *data = ptrlist + 4; |
| 352 | char buf01[4]; |
| 353 | char buf10[4]; |
| 354 | unsigned cwperpage; |
| 355 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 356 | cwperpage = ((flash_pagesize >> 1) >> 9); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 357 | |
| 358 | /* Check first page of this block */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 359 | if (page & 63) |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 360 | page = page - (page & 63); |
| 361 | |
| 362 | /* Check bad block marker */ |
| 363 | data[0] = NAND_CMD_PAGE_READ; /* command */ |
| 364 | |
| 365 | /* addr0 */ |
| 366 | if (CFG1 & CFG1_WIDE_FLASH) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 367 | data[1] = (page << 16) | ((528 * (cwperpage - 1)) >> 1); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 368 | else |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 369 | data[1] = (page << 16) | (528 * (cwperpage - 1)); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 370 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 371 | data[2] = (page >> 16) & 0xff; /* addr1 */ |
| 372 | data[3] = 0 | 4; /* chipsel CS0 */ |
| 373 | data[4] = 0 | 5; /* chipsel CS1 */ |
| 374 | data[5] = NAND_CFG0_RAW & ~(7U << 6); /* cfg0 */ |
| 375 | data[6] = NAND_CFG1_RAW | (CFG1 & CFG1_WIDE_FLASH); /* cfg1 */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 376 | data[7] = 1; |
| 377 | data[8] = CLEAN_DATA_32; /* NC01 flash status */ |
| 378 | data[9] = CLEAN_DATA_32; /* NC01 buf01 status */ |
| 379 | data[10] = CLEAN_DATA_32; /* NC10 flash status */ |
| 380 | data[11] = CLEAN_DATA_32; /* NC10 buf10 status */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 381 | data[12] = 0x00000A3C; /* adm_mux_data_ack_req_nc01 */ |
| 382 | data[13] = 0x0000053C; /* adm_mux_cmd_ack_req_nc01 */ |
| 383 | data[14] = 0x00000F28; /* adm_mux_data_ack_req_nc10 */ |
| 384 | data[15] = 0x00000F14; /* adm_mux_cmd_ack_req_nc10 */ |
| 385 | data[16] = 0x00000FC0; /* adm_default_mux */ |
| 386 | data[17] = 0x00000805; /* enable CS1 */ |
| 387 | data[18] = 0x00000801; /* disable CS1 */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 388 | |
| 389 | /* enable CS1 */ |
| 390 | cmd[0].cmd = 0; |
| 391 | cmd[0].src = paddr(data[17]); |
| 392 | cmd[0].dst = EBI2_CHIP_SELECT_CFG0; |
| 393 | cmd[0].len = 4; |
| 394 | |
| 395 | /* Reading last code word from NC01 */ |
| 396 | /* 0xF14 */ |
| 397 | cmd[1].cmd = 0; |
| 398 | cmd[1].src = paddr(data[15]); |
| 399 | cmd[1].dst = EBI2_NAND_ADM_MUX; |
| 400 | cmd[1].len = 4; |
| 401 | |
| 402 | cmd[2].cmd = DST_CRCI_NAND_CMD; |
| 403 | cmd[2].src = paddr(&data[0]); |
| 404 | cmd[2].dst = NC01(NAND_FLASH_CMD); |
| 405 | cmd[2].len = 16; |
| 406 | |
| 407 | cmd[3].cmd = 0; |
| 408 | cmd[3].src = paddr(&data[5]); |
| 409 | cmd[3].dst = NC01(NAND_DEV0_CFG0); |
| 410 | cmd[3].len = 8; |
| 411 | |
| 412 | cmd[4].cmd = 0; |
| 413 | cmd[4].src = paddr(&data[7]); |
| 414 | cmd[4].dst = NC01(NAND_EXEC_CMD); |
| 415 | cmd[4].len = 4; |
| 416 | |
| 417 | /* 0xF28 */ |
| 418 | cmd[5].cmd = 0; |
| 419 | cmd[5].src = paddr(data[14]); |
| 420 | cmd[5].dst = EBI2_NAND_ADM_MUX; |
| 421 | cmd[5].len = 4; |
| 422 | |
| 423 | cmd[6].cmd = SRC_CRCI_NAND_DATA; |
| 424 | cmd[6].src = NC01(NAND_FLASH_STATUS); |
| 425 | cmd[6].dst = paddr(&data[8]); |
| 426 | cmd[6].len = 8; |
| 427 | |
| 428 | cmd[7].cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 429 | cmd[7].src = |
| 430 | NC01(NAND_FLASH_BUFFER) + (flash_pagesize - |
| 431 | (528 * (cwperpage - 1))); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 432 | cmd[7].dst = paddr(&buf01); |
| 433 | cmd[7].len = 4; |
| 434 | |
| 435 | /* Reading last code word from NC10 */ |
| 436 | /* 0x53C */ |
| 437 | cmd[8].cmd = 0; |
| 438 | cmd[8].src = paddr(data[13]); |
| 439 | cmd[8].dst = EBI2_NAND_ADM_MUX; |
| 440 | cmd[8].len = 4; |
| 441 | |
| 442 | cmd[9].cmd = DST_CRCI_NAND_CMD; |
| 443 | cmd[9].src = paddr(&data[0]); |
| 444 | cmd[9].dst = NC10(NAND_FLASH_CMD); |
| 445 | cmd[9].len = 12; |
| 446 | |
| 447 | cmd[10].cmd = 0; |
| 448 | cmd[10].src = paddr(&data[4]); |
| 449 | cmd[10].dst = NC10(NAND_FLASH_CHIP_SELECT); |
| 450 | cmd[10].len = 4; |
| 451 | |
| 452 | cmd[11].cmd = 0; |
| 453 | cmd[11].src = paddr(&data[5]); |
| 454 | cmd[11].dst = NC10(NAND_DEV1_CFG0); |
| 455 | cmd[11].len = 8; |
| 456 | |
| 457 | cmd[12].cmd = 0; |
| 458 | cmd[12].src = paddr(&data[7]); |
| 459 | cmd[12].dst = NC10(NAND_EXEC_CMD); |
| 460 | cmd[12].len = 4; |
| 461 | |
| 462 | /* 0xA3C */ |
| 463 | cmd[13].cmd = 0; |
| 464 | cmd[13].src = paddr(data[12]); |
| 465 | cmd[13].dst = EBI2_NAND_ADM_MUX; |
| 466 | cmd[13].len = 4; |
| 467 | |
| 468 | cmd[14].cmd = SRC_CRCI_NAND_DATA; |
| 469 | cmd[14].src = NC10(NAND_FLASH_STATUS); |
| 470 | cmd[14].dst = paddr(&data[10]); |
| 471 | cmd[14].len = 8; |
| 472 | |
| 473 | cmd[15].cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 474 | cmd[15].src = |
| 475 | NC10(NAND_FLASH_BUFFER) + (flash_pagesize - |
| 476 | (528 * (cwperpage - 1))); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 477 | cmd[15].dst = paddr(&buf10); |
| 478 | cmd[15].len = 4; |
| 479 | |
| 480 | cmd[16].cmd = 0; |
| 481 | cmd[16].src = paddr(&data[16]); |
| 482 | cmd[16].dst = EBI2_NAND_ADM_MUX; |
| 483 | cmd[16].len = 4; |
| 484 | |
| 485 | /* setting default value */ |
| 486 | cmd[17].cmd = CMD_OCU | CMD_LC; |
| 487 | cmd[17].src = paddr(&data[18]); |
| 488 | cmd[17].dst = EBI2_CHIP_SELECT_CFG0; |
| 489 | cmd[17].len = 4; |
| 490 | |
| 491 | ptr[0] = (paddr(cmd) >> 3) | CMD_PTR_LP; |
| 492 | |
| 493 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 494 | |
| 495 | #if VERBOSE |
| 496 | dprintf(INFO, "NC01 status: %x\n", data[8]); |
| 497 | dprintf(INFO, "NC10 status: %x\n", data[10]); |
| 498 | #endif |
| 499 | |
| 500 | /* we fail if there was an operation error, a mpu error, or the |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 501 | ** erase success bit was not set. |
| 502 | */ |
| 503 | if ((data[8] & 0x110) || (data[10] & 0x110)) |
| 504 | return -1; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 505 | |
| 506 | /* Check for bad block marker byte */ |
| 507 | if (CFG1 & CFG1_WIDE_FLASH) { |
| 508 | if ((buf01[0] != 0xFF || buf01[1] != 0xFF) || |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 509 | (buf10[0] != 0xFF || buf10[1] != 0xFF)) |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 510 | return 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 511 | } else { |
| 512 | if (buf01[0] != 0xFF || buf10[0] != 0xFF) |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 513 | return 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 514 | } |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 519 | static int |
| 520 | flash_nand_erase_block(dmov_s * cmdlist, unsigned *ptrlist, unsigned page) |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 521 | { |
| 522 | dmov_s *cmd = cmdlist; |
| 523 | unsigned *ptr = ptrlist; |
| 524 | unsigned *data = ptrlist + 4; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 525 | int isbad = 0; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 526 | |
| 527 | /* only allow erasing on block boundaries */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 528 | if (page & num_pages_per_blk_mask) |
| 529 | return -1; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 530 | |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 531 | /* Check for bad block and erase only if block is not marked bad */ |
| 532 | isbad = flash_nand_block_isbad(cmdlist, ptrlist, page); |
| 533 | |
| 534 | if (isbad) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 535 | dprintf(INFO, "skipping @ %d (bad block)\n", |
| 536 | page / num_pages_per_blk); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | /* Erase block */ |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 541 | data[0] = NAND_CMD_BLOCK_ERASE; |
| 542 | data[1] = page; |
| 543 | data[2] = 0; |
| 544 | data[3] = 0 | 4; |
| 545 | data[4] = 1; |
| 546 | data[5] = 0xeeeeeeee; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 547 | data[6] = CFG0 & (~(7 << 6)); /* CW_PER_PAGE = 0 */ |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 548 | data[7] = CFG1; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 549 | data[8] = ECC_BCH_CFG; |
| 550 | data[9] = 0x00000020; |
| 551 | data[10] = 0x000000C0; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 552 | |
| 553 | cmd[0].cmd = DST_CRCI_NAND_CMD | CMD_OCB; |
| 554 | cmd[0].src = paddr(&data[0]); |
| 555 | cmd[0].dst = NAND_FLASH_CMD; |
| 556 | cmd[0].len = 16; |
| 557 | |
| 558 | cmd[1].cmd = 0; |
| 559 | cmd[1].src = paddr(&data[6]); |
| 560 | cmd[1].dst = NAND_DEV0_CFG0; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 561 | if (enable_bch_ecc) { |
| 562 | cmd[1].len = 12; |
| 563 | } else { |
| 564 | cmd[1].len = 8; |
| 565 | } |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 566 | |
| 567 | cmd[2].cmd = 0; |
| 568 | cmd[2].src = paddr(&data[4]); |
| 569 | cmd[2].dst = NAND_EXEC_CMD; |
| 570 | cmd[2].len = 4; |
| 571 | |
Murali Palnati | c54d13a | 2010-01-15 19:50:19 +0530 | [diff] [blame] | 572 | cmd[3].cmd = SRC_CRCI_NAND_DATA; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 573 | cmd[3].src = NAND_FLASH_STATUS; |
| 574 | cmd[3].dst = paddr(&data[5]); |
| 575 | cmd[3].len = 4; |
| 576 | |
Murali Palnati | c54d13a | 2010-01-15 19:50:19 +0530 | [diff] [blame] | 577 | cmd[4].cmd = 0; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 578 | cmd[4].src = paddr(&data[9]); |
Murali Palnati | c54d13a | 2010-01-15 19:50:19 +0530 | [diff] [blame] | 579 | cmd[4].dst = NAND_FLASH_STATUS; |
| 580 | cmd[4].len = 4; |
| 581 | |
| 582 | cmd[5].cmd = CMD_OCU | CMD_LC; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 583 | cmd[5].src = paddr(&data[10]); |
Murali Palnati | c54d13a | 2010-01-15 19:50:19 +0530 | [diff] [blame] | 584 | cmd[5].dst = NAND_READ_STATUS; |
| 585 | cmd[5].len = 4; |
| 586 | |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 587 | ptr[0] = (paddr(cmd) >> 3) | CMD_PTR_LP; |
| 588 | |
| 589 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 590 | |
| 591 | #if VERBOSE |
| 592 | dprintf(INFO, "status: %x\n", data[5]); |
| 593 | #endif |
| 594 | |
| 595 | /* we fail if there was an operation error, a mpu error, or the |
| 596 | ** erase success bit was not set. |
| 597 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 598 | if (data[5] & 0x110) |
| 599 | return -1; |
| 600 | if (!(data[5] & 0x80)) |
| 601 | return -1; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 606 | static int |
| 607 | flash_nand_erase_block_interleave(dmov_s * cmdlist, unsigned *ptrlist, |
| 608 | unsigned page) |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 609 | { |
| 610 | dmov_s *cmd = cmdlist; |
| 611 | unsigned *ptr = ptrlist; |
| 612 | unsigned *data = ptrlist + 4; |
| 613 | int isbad = 0; |
| 614 | |
| 615 | /* only allow erasing on block boundaries */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 616 | if (page & 63) |
| 617 | return -1; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 618 | |
| 619 | /* Check for bad block and erase only if block is not marked bad */ |
| 620 | isbad = flash_nand_block_isbad(cmdlist, ptrlist, page); |
| 621 | |
| 622 | if (isbad) { |
| 623 | dprintf(INFO, "skipping @ %d (bad block)\n", page >> 6); |
| 624 | return -1; |
| 625 | } |
| 626 | |
| 627 | /* Erase block */ |
| 628 | data[0] = NAND_CMD_BLOCK_ERASE; |
| 629 | data[1] = page; |
| 630 | data[2] = 0; |
| 631 | data[3] = 0 | 4; /* chipselect CS0 */ |
| 632 | data[4] = 0 | 5; /* chipselect CS1 */ |
| 633 | data[5] = 1; |
| 634 | data[6] = 0xeeeeeeee; |
| 635 | data[7] = 0xeeeeeeee; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 636 | data[8] = CFG0 & (~(7 << 6)); /* CW_PER_PAGE = 0 */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 637 | data[9] = CFG1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 638 | data[10] = 0x00000A3C; /* adm_mux_data_ack_req_nc01 */ |
| 639 | data[11] = 0x0000053C; /* adm_mux_cmd_ack_req_nc01 */ |
| 640 | data[12] = 0x00000F28; /* adm_mux_data_ack_req_nc10 */ |
| 641 | data[13] = 0x00000F14; /* adm_mux_cmd_ack_req_nc10 */ |
| 642 | data[14] = 0x00000FC0; /* adm_default_mux */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 643 | data[15] = 0x00000805; /* enable CS1 */ |
| 644 | data[16] = 0x00000801; /* disable CS1 */ |
| 645 | |
| 646 | /* enable CS1 */ |
| 647 | cmd[0].cmd = 0 | CMD_OCB; |
| 648 | cmd[0].src = paddr(data[15]); |
| 649 | cmd[0].dst = EBI2_CHIP_SELECT_CFG0; |
| 650 | cmd[0].len = 4; |
| 651 | |
| 652 | /* Reading last code word from NC01 */ |
| 653 | /* 0xF14 */ |
| 654 | cmd[1].cmd = 0; |
| 655 | cmd[1].src = paddr(data[13]); |
| 656 | cmd[1].dst = EBI2_NAND_ADM_MUX; |
| 657 | cmd[1].len = 4; |
| 658 | |
| 659 | cmd[2].cmd = DST_CRCI_NAND_CMD; |
| 660 | cmd[2].src = paddr(&data[0]); |
| 661 | cmd[2].dst = NC01(NAND_FLASH_CMD); |
| 662 | cmd[2].len = 16; |
| 663 | |
| 664 | cmd[3].cmd = 0; |
| 665 | cmd[3].src = paddr(&data[8]); |
| 666 | cmd[3].dst = NC01(NAND_DEV0_CFG0); |
| 667 | cmd[3].len = 8; |
| 668 | |
| 669 | cmd[4].cmd = 0; |
| 670 | cmd[4].src = paddr(&data[5]); |
| 671 | cmd[4].dst = NC01(NAND_EXEC_CMD); |
| 672 | cmd[4].len = 4; |
| 673 | |
| 674 | /* 0xF28 */ |
| 675 | cmd[5].cmd = 0; |
| 676 | cmd[5].src = paddr(data[12]); |
| 677 | cmd[5].dst = EBI2_NAND_ADM_MUX; |
| 678 | cmd[5].len = 4; |
| 679 | |
| 680 | cmd[6].cmd = SRC_CRCI_NAND_DATA; |
| 681 | cmd[6].src = NC01(NAND_FLASH_STATUS); |
| 682 | cmd[6].dst = paddr(&data[6]); |
| 683 | cmd[6].len = 4; |
| 684 | |
| 685 | /* Reading last code word from NC10 */ |
| 686 | /* 0x53C */ |
| 687 | cmd[7].cmd = 0; |
| 688 | cmd[7].src = paddr(data[11]); |
| 689 | cmd[7].dst = EBI2_NAND_ADM_MUX; |
| 690 | cmd[7].len = 4; |
| 691 | |
| 692 | cmd[8].cmd = DST_CRCI_NAND_CMD; |
| 693 | cmd[8].src = paddr(&data[0]); |
| 694 | cmd[8].dst = NC10(NAND_FLASH_CMD); |
| 695 | cmd[8].len = 12; |
| 696 | |
| 697 | cmd[9].cmd = 0; |
| 698 | cmd[9].src = paddr(&data[4]); |
| 699 | cmd[9].dst = NC10(NAND_FLASH_CHIP_SELECT); |
| 700 | cmd[9].len = 4; |
| 701 | |
| 702 | cmd[10].cmd = 0; |
| 703 | cmd[10].src = paddr(&data[8]); |
| 704 | cmd[10].dst = NC10(NAND_DEV1_CFG0); |
| 705 | cmd[10].len = 8; |
| 706 | |
| 707 | cmd[11].cmd = 0; |
| 708 | cmd[11].src = paddr(&data[5]); |
| 709 | cmd[11].dst = NC10(NAND_EXEC_CMD); |
| 710 | cmd[11].len = 4; |
| 711 | |
| 712 | /* 0xA3C */ |
| 713 | cmd[12].cmd = 0; |
| 714 | cmd[12].src = paddr(data[10]); |
| 715 | cmd[12].dst = EBI2_NAND_ADM_MUX; |
| 716 | cmd[12].len = 4; |
| 717 | |
| 718 | cmd[13].cmd = SRC_CRCI_NAND_DATA; |
| 719 | cmd[13].src = NC10(NAND_FLASH_STATUS); |
| 720 | cmd[13].dst = paddr(&data[7]); |
| 721 | cmd[13].len = 4; |
| 722 | |
| 723 | /* adm default mux state */ |
| 724 | /* 0xFCO */ |
| 725 | cmd[14].cmd = 0; |
| 726 | cmd[14].src = paddr(data[14]); |
| 727 | cmd[14].dst = EBI2_NAND_ADM_MUX; |
| 728 | cmd[14].len = 4; |
| 729 | |
| 730 | /* disable CS1 */ |
| 731 | cmd[15].cmd = CMD_OCU | CMD_LC; |
| 732 | cmd[15].src = paddr(data[16]); |
| 733 | cmd[15].dst = EBI2_CHIP_SELECT_CFG0; |
| 734 | cmd[15].len = 4; |
| 735 | |
| 736 | ptr[0] = (paddr(cmd) >> 3) | CMD_PTR_LP; |
| 737 | |
| 738 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 739 | |
| 740 | #if VERBOSE |
| 741 | dprintf(INFO, "NC01 status: %x\n", data[6]); |
| 742 | dprintf(INFO, "NC10 status: %x\n", data[7]); |
| 743 | #endif |
| 744 | |
| 745 | /* we fail if there was an operation error, a mpu error, or the |
| 746 | ** erase success bit was not set. |
| 747 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 748 | if (data[6] & 0x110 || data[7] & 0x110) |
| 749 | return -1; |
| 750 | if (!(data[6] & 0x80) || !(data[7] & 0x80)) |
| 751 | return -1; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 756 | struct data_flash_io { |
| 757 | unsigned cmd; |
| 758 | unsigned addr0; |
| 759 | unsigned addr1; |
| 760 | unsigned chipsel; |
| 761 | unsigned cfg0; |
| 762 | unsigned cfg1; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 763 | unsigned ecc_bch_cfg; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 764 | unsigned exec; |
| 765 | unsigned ecc_cfg; |
| 766 | unsigned ecc_cfg_save; |
Murali Palnati | c54d13a | 2010-01-15 19:50:19 +0530 | [diff] [blame] | 767 | unsigned clrfstatus; |
| 768 | unsigned clrrstatus; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 769 | struct { |
| 770 | unsigned flash_status; |
| 771 | unsigned buffer_status; |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 772 | } result[8]; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 773 | }; |
| 774 | |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 775 | struct interleave_data_flash_io { |
| 776 | uint32_t cmd; |
| 777 | uint32_t addr0; |
| 778 | uint32_t addr1; |
| 779 | uint32_t chipsel_cs0; |
| 780 | uint32_t chipsel_cs1; |
| 781 | uint32_t cfg0; |
| 782 | uint32_t cfg1; |
| 783 | uint32_t exec; |
| 784 | uint32_t ecc_cfg; |
| 785 | uint32_t ecc_cfg_save; |
| 786 | uint32_t ebi2_chip_select_cfg0; |
| 787 | uint32_t adm_mux_data_ack_req_nc01; |
| 788 | uint32_t adm_mux_cmd_ack_req_nc01; |
| 789 | uint32_t adm_mux_data_ack_req_nc10; |
| 790 | uint32_t adm_mux_cmd_ack_req_nc10; |
| 791 | uint32_t adm_default_mux; |
| 792 | uint32_t default_ebi2_chip_select_cfg0; |
| 793 | struct { |
| 794 | uint32_t flash_status; |
| 795 | } result[16]; |
| 796 | }; |
| 797 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 798 | static int |
| 799 | _flash_nand_read_page(dmov_s * cmdlist, unsigned *ptrlist, |
| 800 | unsigned page, void *_addr, void *_spareaddr) |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 801 | { |
| 802 | dmov_s *cmd = cmdlist; |
| 803 | unsigned *ptr = ptrlist; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 804 | struct data_flash_io *data = (void *)(ptrlist + 4); |
| 805 | unsigned addr = (unsigned)_addr; |
| 806 | unsigned spareaddr = (unsigned)_spareaddr; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 807 | unsigned n; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 808 | int isbad = 0; |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 809 | unsigned cwperpage; |
Channagoud Kadabi | b2fb6ba | 2011-07-29 19:19:01 +0530 | [diff] [blame] | 810 | unsigned block = 0; |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 811 | cwperpage = (flash_pagesize >> 9); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 812 | |
Channagoud Kadabi | b2fb6ba | 2011-07-29 19:19:01 +0530 | [diff] [blame] | 813 | /* Find the block no for the page */ |
| 814 | block = page / num_pages_per_blk; |
| 815 | |
| 816 | /* Check the bad block table for each block |
| 817 | * -1: indicates the block needs to be checked if good or bad |
| 818 | * 1 : The block is bad |
| 819 | * 0 : The block is good |
| 820 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 821 | if (bbtbl[block] == -1) { |
Channagoud Kadabi | b2fb6ba | 2011-07-29 19:19:01 +0530 | [diff] [blame] | 822 | isbad = flash_nand_block_isbad(cmdlist, ptrlist, page); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 823 | if (isbad) { |
Channagoud Kadabi | b2fb6ba | 2011-07-29 19:19:01 +0530 | [diff] [blame] | 824 | /* Found bad , set the bad table entry */ |
| 825 | bbtbl[block] = 1; |
| 826 | return -2; |
| 827 | } else { |
| 828 | /* Found good block , set the table entry & |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 829 | * continue reading the data |
| 830 | */ |
Channagoud Kadabi | b2fb6ba | 2011-07-29 19:19:01 +0530 | [diff] [blame] | 831 | bbtbl[block] = 0; |
| 832 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 833 | } else if (bbtbl[block] == 1) { |
| 834 | /* If the block is already identified as bad, return error */ |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 835 | return -2; |
Channagoud Kadabi | b2fb6ba | 2011-07-29 19:19:01 +0530 | [diff] [blame] | 836 | } |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 837 | |
| 838 | data->cmd = NAND_CMD_PAGE_READ_ECC; |
| 839 | data->addr0 = page << 16; |
| 840 | data->addr1 = (page >> 16) & 0xff; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 841 | data->chipsel = 0 | 4; /* flash0 + undoc bit */ |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 842 | |
| 843 | /* GO bit for the EXEC register */ |
| 844 | data->exec = 1; |
| 845 | |
| 846 | data->cfg0 = CFG0; |
| 847 | data->cfg1 = CFG1; |
| 848 | |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 849 | if (enable_bch_ecc) { |
| 850 | data->ecc_bch_cfg = ECC_BCH_CFG; |
| 851 | } |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 852 | data->ecc_cfg = 0x203; |
| 853 | |
| 854 | /* save existing ecc config */ |
| 855 | cmd->cmd = CMD_OCB; |
| 856 | cmd->src = NAND_EBI2_ECC_BUF_CFG; |
| 857 | cmd->dst = paddr(&data->ecc_cfg_save); |
| 858 | cmd->len = 4; |
| 859 | cmd++; |
| 860 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 861 | for (n = 0; n < cwperpage; n++) { |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 862 | /* write CMD / ADDR0 / ADDR1 / CHIPSEL regs in a burst */ |
| 863 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 864 | cmd->src = paddr(&data->cmd); |
| 865 | cmd->dst = NAND_FLASH_CMD; |
| 866 | cmd->len = ((n == 0) ? 16 : 4); |
| 867 | cmd++; |
| 868 | |
| 869 | if (n == 0) { |
| 870 | /* block on cmd ready, set configuration */ |
| 871 | cmd->cmd = 0; |
| 872 | cmd->src = paddr(&data->cfg0); |
| 873 | cmd->dst = NAND_DEV0_CFG0; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 874 | if (enable_bch_ecc) { |
| 875 | cmd->len = 12; |
| 876 | } else { |
| 877 | cmd->len = 8; |
| 878 | } |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 879 | cmd++; |
| 880 | |
| 881 | /* set our ecc config */ |
| 882 | cmd->cmd = 0; |
| 883 | cmd->src = paddr(&data->ecc_cfg); |
| 884 | cmd->dst = NAND_EBI2_ECC_BUF_CFG; |
| 885 | cmd->len = 4; |
| 886 | cmd++; |
| 887 | } |
| 888 | /* kick the execute register */ |
| 889 | cmd->cmd = 0; |
| 890 | cmd->src = paddr(&data->exec); |
| 891 | cmd->dst = NAND_EXEC_CMD; |
| 892 | cmd->len = 4; |
| 893 | cmd++; |
| 894 | |
| 895 | /* block on data ready, then read the status register */ |
| 896 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 897 | cmd->src = NAND_FLASH_STATUS; |
| 898 | cmd->dst = paddr(&data->result[n]); |
| 899 | cmd->len = 8; |
| 900 | cmd++; |
| 901 | |
| 902 | /* read data block */ |
| 903 | cmd->cmd = 0; |
| 904 | cmd->src = NAND_FLASH_BUFFER; |
| 905 | cmd->dst = addr + n * 516; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 906 | cmd->len = |
| 907 | ((n < |
| 908 | (cwperpage - 1)) ? 516 : (512 - ((cwperpage - 1) << 2))); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 909 | cmd++; |
| 910 | } |
| 911 | |
| 912 | /* read extra data */ |
| 913 | cmd->cmd = 0; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 914 | cmd->src = NAND_FLASH_BUFFER + (512 - ((cwperpage - 1) << 2)); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 915 | cmd->dst = spareaddr; |
| 916 | cmd->len = 16; |
| 917 | cmd++; |
| 918 | |
| 919 | /* restore saved ecc config */ |
| 920 | cmd->cmd = CMD_OCU | CMD_LC; |
| 921 | cmd->src = paddr(&data->ecc_cfg_save); |
| 922 | cmd->dst = NAND_EBI2_ECC_BUF_CFG; |
| 923 | cmd->len = 4; |
| 924 | |
| 925 | ptr[0] = (paddr(cmdlist) >> 3) | CMD_PTR_LP; |
| 926 | |
| 927 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 928 | |
| 929 | #if VERBOSE |
| 930 | dprintf(INFO, "read page %d: status: %x %x %x %x\n", |
| 931 | page, data[5], data[6], data[7], data[8]); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 932 | for (n = 0; n < 4; n++) { |
| 933 | ptr = (unsigned *)(addr + 512 * n); |
| 934 | dprintf(INFO, "data%d: %x %x %x %x\n", n, ptr[0], ptr[1], |
| 935 | ptr[2], ptr[3]); |
| 936 | ptr = (unsigned *)(spareaddr + 16 * n); |
| 937 | dprintf(INFO, "spare data%d %x %x %x %x\n", n, ptr[0], |
| 938 | ptr[1], ptr[2], ptr[3]); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 939 | } |
| 940 | #endif |
| 941 | |
| 942 | /* if any of the writes failed (0x10), or there was a |
| 943 | ** protection violation (0x100), we lose |
| 944 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 945 | for (n = 0; n < cwperpage; n++) { |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 946 | if (data->result[n].flash_status & 0x110) { |
| 947 | return -1; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 954 | static int |
| 955 | flash_nand_read_page_interleave(dmov_s * cmdlist, unsigned *ptrlist, |
| 956 | unsigned page, void *_addr, void *_spareaddr) |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 957 | { |
| 958 | dmov_s *cmd = cmdlist; |
| 959 | unsigned *ptr = ptrlist; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 960 | struct interleave_data_flash_io *data = (void *)(ptrlist + 4); |
| 961 | unsigned addr = (unsigned)_addr; |
| 962 | unsigned spareaddr = (unsigned)_spareaddr; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 963 | unsigned n; |
| 964 | int isbad = 0; |
| 965 | unsigned cwperpage; |
| 966 | cwperpage = (flash_pagesize >> 9); |
| 967 | |
| 968 | /* Check for bad block and read only from a good block */ |
| 969 | isbad = flash_nand_block_isbad(cmdlist, ptrlist, page); |
| 970 | if (isbad) |
| 971 | return -2; |
| 972 | |
| 973 | data->cmd = NAND_CMD_PAGE_READ_ECC; |
| 974 | data->addr0 = page << 16; |
| 975 | data->addr1 = (page >> 16) & 0xff; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 976 | data->chipsel_cs0 = 0 | 4; /* flash0 + undoc bit */ |
| 977 | data->chipsel_cs1 = 0 | 5; /* flash0 + undoc bit */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 978 | data->ebi2_chip_select_cfg0 = 0x00000805; |
| 979 | data->adm_mux_data_ack_req_nc01 = 0x00000A3C; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 980 | data->adm_mux_cmd_ack_req_nc01 = 0x0000053C; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 981 | data->adm_mux_data_ack_req_nc10 = 0x00000F28; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 982 | data->adm_mux_cmd_ack_req_nc10 = 0x00000F14; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 983 | data->adm_default_mux = 0x00000FC0; |
| 984 | data->default_ebi2_chip_select_cfg0 = 0x00000801; |
| 985 | |
| 986 | /* GO bit for the EXEC register */ |
| 987 | data->exec = 1; |
| 988 | |
| 989 | data->cfg0 = CFG0; |
| 990 | data->cfg1 = CFG1; |
| 991 | |
| 992 | data->ecc_cfg = 0x203; |
| 993 | |
| 994 | for (n = 0; n < cwperpage; n++) { |
| 995 | /* flash + buffer status return words */ |
| 996 | data->result[n].flash_status = 0xeeeeeeee; |
| 997 | |
| 998 | if (n == 0) { |
| 999 | /* enable CS1 */ |
| 1000 | cmd->cmd = CMD_OCB; |
| 1001 | cmd->src = paddr(&data->ebi2_chip_select_cfg0); |
| 1002 | cmd->dst = EBI2_CHIP_SELECT_CFG0; |
| 1003 | cmd->len = 4; |
| 1004 | cmd++; |
| 1005 | |
| 1006 | /* save existing ecc config */ |
| 1007 | cmd->cmd = 0; |
| 1008 | cmd->src = NAND_EBI2_ECC_BUF_CFG; |
| 1009 | cmd->dst = paddr(&data->ecc_cfg_save); |
| 1010 | cmd->len = 4; |
| 1011 | cmd++; |
| 1012 | |
| 1013 | /* NC01, NC10 --> ADDR0/ADDR1 */ |
| 1014 | cmd->cmd = 0; |
| 1015 | cmd->src = paddr(&data->addr0); |
| 1016 | cmd->dst = NC11(NAND_ADDR0); |
| 1017 | cmd->len = 8; |
| 1018 | cmd++; |
| 1019 | |
| 1020 | /* Select the CS0, |
| 1021 | * for NC01! |
| 1022 | */ |
| 1023 | cmd->cmd = 0; |
| 1024 | cmd->src = paddr(&data->chipsel_cs0); |
| 1025 | cmd->dst = NC01(NAND_FLASH_CHIP_SELECT); |
| 1026 | cmd->len = 4; |
| 1027 | cmd++; |
| 1028 | |
| 1029 | /* Select the CS1, |
| 1030 | * for NC10! |
| 1031 | */ |
| 1032 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1033 | cmd->src = paddr(&data->chipsel_cs1); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1034 | cmd->dst = NC10(NAND_FLASH_CHIP_SELECT); |
| 1035 | cmd->len = 4; |
| 1036 | cmd++; |
| 1037 | |
| 1038 | cmd->cmd = 0; |
| 1039 | cmd->src = paddr(&data->cfg0); |
| 1040 | cmd->dst = NC01(NAND_DEV0_CFG0); |
| 1041 | cmd->len = 8; |
| 1042 | cmd++; |
| 1043 | |
| 1044 | /* config DEV1 for CS1 */ |
| 1045 | cmd->cmd = 0; |
| 1046 | cmd->src = paddr(&data->cfg0); |
| 1047 | cmd->dst = NC10(NAND_DEV1_CFG0); |
| 1048 | cmd->len = 8; |
| 1049 | cmd++; |
| 1050 | |
| 1051 | cmd->cmd = 0; |
| 1052 | cmd->src = paddr(&data->ecc_cfg); |
| 1053 | cmd->dst = NC11(NAND_EBI2_ECC_BUF_CFG); |
| 1054 | cmd->len = 4; |
| 1055 | cmd++; |
| 1056 | |
| 1057 | /* if 'only' the last code word */ |
| 1058 | if (n == cwperpage - 1) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1059 | /* MASK CMD ACK/REQ --> NC01 (0x53C) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1060 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1061 | cmd->src = |
| 1062 | paddr(&data->adm_mux_cmd_ack_req_nc01); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1063 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1064 | cmd->len = 4; |
| 1065 | cmd++; |
| 1066 | |
| 1067 | /* CMD */ |
| 1068 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 1069 | cmd->src = paddr(&data->cmd); |
| 1070 | cmd->dst = NC10(NAND_FLASH_CMD); |
| 1071 | cmd->len = 4; |
| 1072 | cmd++; |
| 1073 | |
| 1074 | /* kick the execute register for NC10 */ |
| 1075 | cmd->cmd = 0; |
| 1076 | cmd->src = paddr(&data->exec); |
| 1077 | cmd->dst = NC10(NAND_EXEC_CMD); |
| 1078 | cmd->len = 4; |
| 1079 | cmd++; |
| 1080 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1081 | /* MASK DATA ACK/REQ --> NC01 (0xA3C) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1082 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1083 | cmd->src = |
| 1084 | paddr(&data->adm_mux_data_ack_req_nc01); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1085 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1086 | cmd->len = 4; |
| 1087 | cmd++; |
| 1088 | |
| 1089 | /* block on data ready from NC10, then |
| 1090 | * read the status register |
| 1091 | */ |
| 1092 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 1093 | cmd->src = NC10(NAND_FLASH_STATUS); |
| 1094 | cmd->dst = paddr(&data->result[n]); |
| 1095 | /* NAND_FLASH_STATUS + |
| 1096 | * NAND_BUFFER_STATUS |
| 1097 | */ |
| 1098 | cmd->len = 4; |
| 1099 | cmd++; |
| 1100 | } else { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1101 | /* MASK CMD ACK/REQ --> NC10 (0xF14) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1102 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1103 | cmd->src = |
| 1104 | paddr(&data->adm_mux_cmd_ack_req_nc10); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1105 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1106 | cmd->len = 4; |
| 1107 | cmd++; |
| 1108 | |
| 1109 | /* CMD */ |
| 1110 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 1111 | cmd->src = paddr(&data->cmd); |
| 1112 | cmd->dst = NC01(NAND_FLASH_CMD); |
| 1113 | cmd->len = 4; |
| 1114 | cmd++; |
| 1115 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1116 | /* kick the execute register for NC01 */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1117 | cmd->cmd = 0; |
| 1118 | cmd->src = paddr(&data->exec); |
| 1119 | cmd->dst = NC01(NAND_EXEC_CMD); |
| 1120 | cmd->len = 4; |
| 1121 | cmd++; |
| 1122 | } |
| 1123 | } |
| 1124 | |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1125 | if (n % 2 == 0) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1126 | /* MASK CMD ACK/REQ --> NC01 (0x53C) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1127 | cmd->cmd = 0; |
| 1128 | cmd->src = paddr(&data->adm_mux_cmd_ack_req_nc01); |
| 1129 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1130 | cmd->len = 4; |
| 1131 | cmd++; |
| 1132 | |
| 1133 | /* CMD */ |
| 1134 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 1135 | cmd->src = paddr(&data->cmd); |
| 1136 | cmd->dst = NC10(NAND_FLASH_CMD); |
| 1137 | cmd->len = 4; |
| 1138 | cmd++; |
| 1139 | |
| 1140 | /* kick the execute register for NC10 */ |
| 1141 | cmd->cmd = 0; |
| 1142 | cmd->src = paddr(&data->exec); |
| 1143 | cmd->dst = NC10(NAND_EXEC_CMD); |
| 1144 | cmd->len = 4; |
| 1145 | cmd++; |
| 1146 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1147 | /* MASK DATA ACK/REQ --> NC10 (0xF28) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1148 | cmd->cmd = 0; |
| 1149 | cmd->src = paddr(&data->adm_mux_data_ack_req_nc10); |
| 1150 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1151 | cmd->len = 4; |
| 1152 | cmd++; |
| 1153 | |
| 1154 | /* block on data ready from NC01, then |
| 1155 | * read the status register |
| 1156 | */ |
| 1157 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 1158 | cmd->src = NC01(NAND_FLASH_STATUS); |
| 1159 | cmd->dst = paddr(&data->result[n]); |
| 1160 | /* NAND_FLASH_STATUS + |
| 1161 | * NAND_BUFFER_STATUS |
| 1162 | */ |
| 1163 | cmd->len = 4; |
| 1164 | cmd++; |
| 1165 | |
| 1166 | /* read data block */ |
| 1167 | cmd->cmd = 0; |
| 1168 | cmd->src = NC01(NAND_FLASH_BUFFER); |
| 1169 | cmd->dst = addr + n * 516; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1170 | cmd->len = |
| 1171 | ((n < |
| 1172 | (cwperpage - 1)) ? 516 : (512 - |
| 1173 | ((cwperpage - |
| 1174 | 1) << 2))); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1175 | cmd++; |
| 1176 | } else { |
| 1177 | if (n != cwperpage - 1) { |
| 1178 | /* MASK CMD ACK/REQ --> |
| 1179 | * NC10 (0xF14) |
| 1180 | */ |
| 1181 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1182 | cmd->src = |
| 1183 | paddr(&data->adm_mux_cmd_ack_req_nc10); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1184 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1185 | cmd->len = 4; |
| 1186 | cmd++; |
| 1187 | |
| 1188 | /* CMD */ |
| 1189 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 1190 | cmd->src = paddr(&data->cmd); |
| 1191 | cmd->dst = NC01(NAND_FLASH_CMD); |
| 1192 | cmd->len = 4; |
| 1193 | cmd++; |
| 1194 | |
| 1195 | /* EXEC */ |
| 1196 | cmd->cmd = 0; |
| 1197 | cmd->src = paddr(&data->exec); |
| 1198 | cmd->dst = NC01(NAND_EXEC_CMD); |
| 1199 | cmd->len = 4; |
| 1200 | cmd++; |
| 1201 | |
| 1202 | /* MASK DATA ACK/REQ --> |
| 1203 | * NC01 (0xA3C) |
| 1204 | */ |
| 1205 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1206 | cmd->src = |
| 1207 | paddr(&data->adm_mux_data_ack_req_nc01); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1208 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1209 | cmd->len = 4; |
| 1210 | cmd++; |
| 1211 | |
| 1212 | /* block on data ready from NC10 |
| 1213 | * then read the status register |
| 1214 | */ |
| 1215 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 1216 | cmd->src = NC10(NAND_FLASH_STATUS); |
| 1217 | cmd->dst = paddr(&data->result[n]); |
| 1218 | /* NAND_FLASH_STATUS + |
| 1219 | * NAND_BUFFER_STATUS |
| 1220 | */ |
| 1221 | cmd->len = 4; |
| 1222 | cmd++; |
| 1223 | } else { |
| 1224 | /* MASK DATA ACK/REQ -> |
| 1225 | * NC01 (0xA3C) |
| 1226 | */ |
| 1227 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1228 | cmd->src = |
| 1229 | paddr(&data->adm_mux_data_ack_req_nc01); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1230 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1231 | cmd->len = 4; |
| 1232 | cmd++; |
| 1233 | |
| 1234 | /* block on data ready from NC10 |
| 1235 | * then read the status register |
| 1236 | */ |
| 1237 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 1238 | cmd->src = NC10(NAND_FLASH_STATUS); |
| 1239 | cmd->dst = paddr(&data->result[n]); |
| 1240 | /* NAND_FLASH_STATUS + |
| 1241 | * NAND_BUFFER_STATUS |
| 1242 | */ |
| 1243 | cmd->len = 4; |
| 1244 | cmd++; |
| 1245 | } |
| 1246 | /* read data block */ |
| 1247 | cmd->cmd = 0; |
| 1248 | cmd->src = NC10(NAND_FLASH_BUFFER); |
| 1249 | cmd->dst = addr + n * 516; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1250 | cmd->len = |
| 1251 | ((n < |
| 1252 | (cwperpage - 1)) ? 516 : (512 - |
| 1253 | ((cwperpage - |
| 1254 | 1) << 2))); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1255 | cmd++; |
| 1256 | |
| 1257 | if (n == (cwperpage - 1)) { |
| 1258 | /* Use NC10 for reading the |
| 1259 | * last codeword!!! |
| 1260 | */ |
| 1261 | cmd->cmd = 0; |
| 1262 | cmd->src = NC10(NAND_FLASH_BUFFER) + |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1263 | (512 - ((cwperpage - 1) << 2)); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1264 | cmd->dst = spareaddr; |
| 1265 | cmd->len = 16; |
| 1266 | cmd++; |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | /* restore saved ecc config */ |
| 1271 | cmd->cmd = CMD_OCU | CMD_LC; |
| 1272 | cmd->src = paddr(&data->ecc_cfg_save); |
| 1273 | cmd->dst = NAND_EBI2_ECC_BUF_CFG; |
| 1274 | cmd->len = 4; |
| 1275 | |
| 1276 | /* ADM --> Default mux state (0xFC0) */ |
| 1277 | cmd->cmd = 0; |
| 1278 | cmd->src = paddr(&data->adm_default_mux); |
| 1279 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1280 | cmd->len = 4; |
| 1281 | cmd++; |
| 1282 | |
| 1283 | /* disable CS1 */ |
| 1284 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1285 | cmd->src = paddr(&data->default_ebi2_chip_select_cfg0); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1286 | cmd->dst = EBI2_CHIP_SELECT_CFG0; |
| 1287 | cmd->len = 4; |
| 1288 | cmd++; |
| 1289 | |
| 1290 | ptr[0] = (paddr(cmdlist) >> 3) | CMD_PTR_LP; |
| 1291 | |
| 1292 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 1293 | |
| 1294 | #if VERBOSE |
| 1295 | dprintf(INFO, "read page %d: status: %x %x %x %x %x %x %x %x \ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1296 | %x %x %x %x %x %x %x %x \n", page, data->result[0].flash_status[0], data->result[1].flash_status[1], data->result[2].flash_status[2], data->result[3].flash_status[3], data->result[4].flash_status[4], data->result[5].flash_status[5], data->result[6].flash_status[6], data->result[7].flash_status[7], data->result[8].flash_status[8], data->result[9].flash_status[9], data->result[10].flash_status[10], data->result[11].flash_status[11], data->result[12].flash_status[12], data->result[13].flash_status[13], data->result[14].flash_status[14], data->result[15].flash_status[15]); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1297 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1298 | for (n = 0; n < 4; n++) { |
| 1299 | ptr = (unsigned *)(addr + 512 * n); |
| 1300 | dprintf(INFO, "data%d: %x %x %x %x\n", n, ptr[0], ptr[1], |
| 1301 | ptr[2], ptr[3]); |
| 1302 | ptr = (unsigned *)(spareaddr + 16 * n); |
| 1303 | dprintf(INFO, "spare data%d %x %x %x %x\n", n, ptr[0], |
| 1304 | ptr[1], ptr[2], ptr[3]); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1305 | } |
| 1306 | #endif |
| 1307 | |
| 1308 | /* if any of the writes failed (0x10), or there was a |
| 1309 | ** protection violation (0x100), we lose |
| 1310 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1311 | for (n = 0; n < cwperpage; n++) { |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1312 | if (data->result[n].flash_status & 0x110) { |
| 1313 | return -1; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1320 | static int |
| 1321 | _flash_nand_write_page(dmov_s * cmdlist, unsigned *ptrlist, unsigned page, |
| 1322 | const void *_addr, const void *_spareaddr, |
| 1323 | unsigned raw_mode) |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1324 | { |
| 1325 | dmov_s *cmd = cmdlist; |
| 1326 | unsigned *ptr = ptrlist; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1327 | struct data_flash_io *data = (void *)(ptrlist + 4); |
| 1328 | unsigned addr = (unsigned)_addr; |
| 1329 | unsigned spareaddr = (unsigned)_spareaddr; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 1330 | unsigned n; |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 1331 | unsigned cwperpage; |
| 1332 | cwperpage = (flash_pagesize >> 9); |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1333 | unsigned modem_partition = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1334 | if (CFG0 == CFG0_M) { |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1335 | modem_partition = 1; |
| 1336 | } |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1337 | |
| 1338 | data->cmd = NAND_CMD_PRG_PAGE; |
| 1339 | data->addr0 = page << 16; |
| 1340 | data->addr1 = (page >> 16) & 0xff; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1341 | data->chipsel = 0 | 4; /* flash0 + undoc bit */ |
Murali Palnati | c54d13a | 2010-01-15 19:50:19 +0530 | [diff] [blame] | 1342 | data->clrfstatus = 0x00000020; |
| 1343 | data->clrrstatus = 0x000000C0; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1344 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1345 | if (!raw_mode) { |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 1346 | data->cfg0 = CFG0; |
| 1347 | data->cfg1 = CFG1; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 1348 | if (enable_bch_ecc) { |
| 1349 | data->ecc_bch_cfg = ECC_BCH_CFG; |
| 1350 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1351 | } else { |
| 1352 | data->cfg0 = |
| 1353 | (NAND_CFG0_RAW & ~(7 << 6)) | ((cwperpage - 1) << 6); |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 1354 | data->cfg1 = NAND_CFG1_RAW | (CFG1 & CFG1_WIDE_FLASH); |
| 1355 | } |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1356 | |
| 1357 | /* GO bit for the EXEC register */ |
| 1358 | data->exec = 1; |
| 1359 | |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1360 | if (modem_partition) |
| 1361 | data->ecc_cfg = 0x1FF; |
| 1362 | else |
| 1363 | data->ecc_cfg = 0x203; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1364 | |
| 1365 | /* save existing ecc config */ |
| 1366 | cmd->cmd = CMD_OCB; |
| 1367 | cmd->src = NAND_EBI2_ECC_BUF_CFG; |
| 1368 | cmd->dst = paddr(&data->ecc_cfg_save); |
| 1369 | cmd->len = 4; |
| 1370 | cmd++; |
| 1371 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1372 | for (n = 0; n < cwperpage; n++) { |
| 1373 | /* write CMD / ADDR0 / ADDR1 / CHIPSEL regs in a burst */ |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1374 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 1375 | cmd->src = paddr(&data->cmd); |
| 1376 | cmd->dst = NAND_FLASH_CMD; |
| 1377 | cmd->len = ((n == 0) ? 16 : 4); |
| 1378 | cmd++; |
| 1379 | |
| 1380 | if (n == 0) { |
| 1381 | /* set configuration */ |
| 1382 | cmd->cmd = 0; |
| 1383 | cmd->src = paddr(&data->cfg0); |
| 1384 | cmd->dst = NAND_DEV0_CFG0; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 1385 | if (enable_bch_ecc) { |
| 1386 | cmd->len = 12; |
| 1387 | } else { |
| 1388 | cmd->len = 8; |
| 1389 | } |
| 1390 | |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1391 | cmd++; |
| 1392 | |
| 1393 | /* set our ecc config */ |
| 1394 | cmd->cmd = 0; |
| 1395 | cmd->src = paddr(&data->ecc_cfg); |
| 1396 | cmd->dst = NAND_EBI2_ECC_BUF_CFG; |
| 1397 | cmd->len = 4; |
| 1398 | cmd++; |
| 1399 | } |
| 1400 | |
| 1401 | /* write data block */ |
| 1402 | cmd->cmd = 0; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1403 | cmd->dst = NAND_FLASH_BUFFER; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1404 | if (!raw_mode) { |
| 1405 | if (modem_partition) { |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1406 | cmd->src = addr + n * 512; |
| 1407 | cmd->len = 512; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1408 | } else { |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1409 | cmd->src = addr + n * 516; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1410 | cmd->len = |
| 1411 | ((n < |
| 1412 | (cwperpage - 1)) ? 516 : (512 - |
| 1413 | ((cwperpage - |
| 1414 | 1) << 2))); |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1415 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1416 | } else { |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 1417 | cmd->src = addr; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1418 | cmd->len = 528; |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 1419 | } |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1420 | cmd++; |
| 1421 | |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1422 | if ((n == (cwperpage - 1)) && (!raw_mode) && (!modem_partition)) { |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1423 | /* write extra data */ |
| 1424 | cmd->cmd = 0; |
| 1425 | cmd->src = spareaddr; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1426 | cmd->dst = |
| 1427 | NAND_FLASH_BUFFER + (512 - ((cwperpage - 1) << 2)); |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 1428 | cmd->len = (cwperpage << 2); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1429 | cmd++; |
| 1430 | } |
| 1431 | |
| 1432 | /* kick the execute register */ |
| 1433 | cmd->cmd = 0; |
| 1434 | cmd->src = paddr(&data->exec); |
| 1435 | cmd->dst = NAND_EXEC_CMD; |
| 1436 | cmd->len = 4; |
| 1437 | cmd++; |
| 1438 | |
| 1439 | /* block on data ready, then read the status register */ |
| 1440 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 1441 | cmd->src = NAND_FLASH_STATUS; |
| 1442 | cmd->dst = paddr(&data->result[n]); |
| 1443 | cmd->len = 8; |
| 1444 | cmd++; |
Murali Palnati | c54d13a | 2010-01-15 19:50:19 +0530 | [diff] [blame] | 1445 | |
| 1446 | cmd->cmd = 0; |
| 1447 | cmd->src = paddr(&data->clrfstatus); |
| 1448 | cmd->dst = NAND_FLASH_STATUS; |
| 1449 | cmd->len = 4; |
| 1450 | cmd++; |
| 1451 | |
| 1452 | cmd->cmd = 0; |
| 1453 | cmd->src = paddr(&data->clrrstatus); |
| 1454 | cmd->dst = NAND_READ_STATUS; |
| 1455 | cmd->len = 4; |
| 1456 | cmd++; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | /* restore saved ecc config */ |
| 1460 | cmd->cmd = CMD_OCU | CMD_LC; |
| 1461 | cmd->src = paddr(&data->ecc_cfg_save); |
| 1462 | cmd->dst = NAND_EBI2_ECC_BUF_CFG; |
| 1463 | cmd->len = 4; |
| 1464 | |
| 1465 | ptr[0] = (paddr(cmdlist) >> 3) | CMD_PTR_LP; |
| 1466 | |
| 1467 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 1468 | |
| 1469 | #if VERBOSE |
| 1470 | dprintf(INFO, "write page %d: status: %x %x %x %x\n", |
| 1471 | page, data[5], data[6], data[7], data[8]); |
| 1472 | #endif |
| 1473 | |
| 1474 | /* if any of the writes failed (0x10), or there was a |
| 1475 | ** protection violation (0x100), or the program success |
| 1476 | ** bit (0x80) is unset, we lose |
| 1477 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1478 | for (n = 0; n < cwperpage; n++) { |
| 1479 | if (data->result[n].flash_status & 0x110) |
| 1480 | return -1; |
| 1481 | if (!(data->result[n].flash_status & 0x80)) |
| 1482 | return -1; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1483 | } |
| 1484 | |
Dima Zavin | 5582c7d | 2009-03-03 15:17:59 -0800 | [diff] [blame] | 1485 | #if VERIFY_WRITE |
| 1486 | n = _flash_read_page(cmdlist, ptrlist, page, flash_data, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1487 | flash_data + 2048); |
Dima Zavin | 5582c7d | 2009-03-03 15:17:59 -0800 | [diff] [blame] | 1488 | if (n != 0) |
| 1489 | return -1; |
| 1490 | if (memcmp(flash_data, _addr, 2048) || |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1491 | memcmp(flash_data + 2048, _spareaddr, 16)) { |
Dima Zavin | 5582c7d | 2009-03-03 15:17:59 -0800 | [diff] [blame] | 1492 | dprintf(CRITICAL, "verify error @ page %d\n", page); |
| 1493 | return -1; |
| 1494 | } |
| 1495 | #endif |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1496 | return 0; |
| 1497 | } |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1498 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1499 | static int |
| 1500 | flash_nand_write_page_interleave(dmov_s * cmdlist, unsigned *ptrlist, |
| 1501 | unsigned page, const void *_addr, |
| 1502 | const void *_spareaddr, unsigned raw_mode) |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1503 | { |
| 1504 | dmov_s *cmd = cmdlist; |
| 1505 | unsigned *ptr = ptrlist; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1506 | struct interleave_data_flash_io *data = (void *)(ptrlist + 4); |
| 1507 | unsigned addr = (unsigned)_addr; |
| 1508 | unsigned spareaddr = (unsigned)_spareaddr; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1509 | unsigned n; |
| 1510 | unsigned cwperpage, cwcount; |
| 1511 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1512 | cwperpage = (flash_pagesize >> 9) * 2; /* double for interleave mode */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1513 | cwcount = (cwperpage << 1); |
| 1514 | |
| 1515 | data->cmd = NAND_CMD_PRG_PAGE; |
| 1516 | data->addr0 = page << 16; |
| 1517 | data->addr1 = (page >> 16) & 0xff; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1518 | data->chipsel_cs0 = 0 | 4; /* flash0 + undoc bit */ |
| 1519 | data->chipsel_cs1 = 0 | 5; /* flash0 + undoc bit */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1520 | data->ebi2_chip_select_cfg0 = 0x00000805; |
| 1521 | data->adm_mux_data_ack_req_nc01 = 0x00000A3C; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1522 | data->adm_mux_cmd_ack_req_nc01 = 0x0000053C; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1523 | data->adm_mux_data_ack_req_nc10 = 0x00000F28; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1524 | data->adm_mux_cmd_ack_req_nc10 = 0x00000F14; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1525 | data->adm_default_mux = 0x00000FC0; |
| 1526 | data->default_ebi2_chip_select_cfg0 = 0x00000801; |
| 1527 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1528 | if (!raw_mode) { |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1529 | data->cfg0 = CFG0; |
| 1530 | data->cfg1 = CFG1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1531 | } else { |
| 1532 | data->cfg0 = (NAND_CFG0_RAW & ~(7 << 6)) | ((cwcount - 1) << 6); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1533 | data->cfg1 = NAND_CFG1_RAW | (CFG1 & CFG1_WIDE_FLASH); |
| 1534 | } |
| 1535 | |
| 1536 | /* GO bit for the EXEC register */ |
| 1537 | data->exec = 1; |
| 1538 | data->ecc_cfg = 0x203; |
| 1539 | |
| 1540 | for (n = 0; n < cwperpage; n++) { |
| 1541 | /* status return words */ |
| 1542 | data->result[n].flash_status = 0xeeeeeeee; |
| 1543 | |
| 1544 | if (n == 0) { |
| 1545 | /* enable CS1 */ |
| 1546 | cmd->cmd = CMD_OCB; |
| 1547 | cmd->src = paddr(&data->ebi2_chip_select_cfg0); |
| 1548 | cmd->dst = EBI2_CHIP_SELECT_CFG0; |
| 1549 | cmd->len = 4; |
| 1550 | cmd++; |
| 1551 | |
| 1552 | /* save existing ecc config */ |
| 1553 | cmd->cmd = 0; |
| 1554 | cmd->src = NC11(NAND_EBI2_ECC_BUF_CFG); |
| 1555 | cmd->dst = paddr(&data->ecc_cfg_save); |
| 1556 | cmd->len = 4; |
| 1557 | cmd++; |
| 1558 | |
| 1559 | cmd->cmd = 0; |
| 1560 | cmd->src = paddr(&data->ecc_cfg); |
| 1561 | cmd->dst = NC11(NAND_EBI2_ECC_BUF_CFG); |
| 1562 | cmd->len = 4; |
| 1563 | cmd++; |
| 1564 | |
| 1565 | cmd->cmd = 0; |
| 1566 | cmd->src = paddr(&data->addr0); |
| 1567 | cmd->dst = NC11(NAND_ADDR0); |
| 1568 | cmd->len = 8; |
| 1569 | cmd++; |
| 1570 | |
| 1571 | /* enable CS0 */ |
| 1572 | cmd->cmd = 0; |
| 1573 | cmd->src = paddr(&data->chipsel_cs0); |
| 1574 | cmd->dst = NC01(NAND_FLASH_CHIP_SELECT); |
| 1575 | cmd->len = 4; |
| 1576 | cmd++; |
| 1577 | |
| 1578 | /* enable CS1 */ |
| 1579 | cmd->cmd = 0; |
| 1580 | cmd->src = paddr(&data->chipsel_cs1); |
| 1581 | cmd->dst = NC10(NAND_FLASH_CHIP_SELECT); |
| 1582 | cmd->len = 4; |
| 1583 | cmd++; |
| 1584 | |
| 1585 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1586 | cmd->src = paddr(&data->cfg0); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1587 | cmd->dst = NC01(NAND_DEV0_CFG0); |
| 1588 | cmd->len = 8; |
| 1589 | cmd++; |
| 1590 | |
| 1591 | /* config CFG1 for CS1 */ |
| 1592 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1593 | cmd->src = paddr(&data->cfg0); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1594 | cmd->dst = NC10(NAND_DEV1_CFG0); |
| 1595 | cmd->len = 8; |
| 1596 | cmd++; |
| 1597 | } |
| 1598 | |
| 1599 | if (n % 2 == 0) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1600 | /* MASK CMD ACK/REQ --> NC10 (0xF14) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1601 | cmd->cmd = 0; |
| 1602 | cmd->src = paddr(&data->adm_mux_cmd_ack_req_nc10); |
| 1603 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1604 | cmd->len = 4; |
| 1605 | cmd++; |
| 1606 | |
| 1607 | /* CMD */ |
| 1608 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 1609 | cmd->src = paddr(&data->cmd); |
| 1610 | cmd->dst = NC01(NAND_FLASH_CMD); |
| 1611 | cmd->len = 4; |
| 1612 | cmd++; |
| 1613 | } else { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1614 | /* MASK CMD ACK/REQ --> NC01 (0x53C) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1615 | cmd->cmd = 0; |
| 1616 | cmd->src = paddr(&data->adm_mux_cmd_ack_req_nc01); |
| 1617 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1618 | cmd->len = 4; |
| 1619 | cmd++; |
| 1620 | |
| 1621 | /* CMD */ |
| 1622 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 1623 | cmd->src = paddr(&data->cmd); |
| 1624 | cmd->dst = NC10(NAND_FLASH_CMD); |
| 1625 | cmd->len = 4; |
| 1626 | cmd++; |
| 1627 | } |
| 1628 | |
| 1629 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1630 | if (!raw_mode) { |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1631 | cmd->src = addr + n * 516; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1632 | cmd->len = |
| 1633 | ((n < |
| 1634 | (cwperpage - 1)) ? 516 : (512 - |
| 1635 | ((cwperpage - |
| 1636 | 1) << 2))); |
| 1637 | } else { |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1638 | cmd->src = addr; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1639 | cmd->len = 528; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | if (n % 2 == 0) |
| 1643 | cmd->dst = NC01(NAND_FLASH_BUFFER); |
| 1644 | else |
| 1645 | cmd->dst = NC10(NAND_FLASH_BUFFER); |
| 1646 | cmd++; |
| 1647 | |
| 1648 | if ((n == (cwperpage - 1)) && (!raw_mode)) { |
| 1649 | /* write extra data */ |
| 1650 | cmd->cmd = 0; |
| 1651 | cmd->src = spareaddr; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1652 | cmd->dst = |
| 1653 | NC10(NAND_FLASH_BUFFER) + (512 - |
| 1654 | ((cwperpage - 1) << 2)); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1655 | cmd->len = (cwperpage << 2); |
| 1656 | cmd++; |
| 1657 | } |
| 1658 | |
| 1659 | if (n % 2 == 0) { |
| 1660 | /* kick the NC01 execute register */ |
| 1661 | cmd->cmd = 0; |
| 1662 | cmd->src = paddr(&data->exec); |
| 1663 | cmd->dst = NC01(NAND_EXEC_CMD); |
| 1664 | cmd->len = 4; |
| 1665 | cmd++; |
| 1666 | if (n != 0) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1667 | /* MASK DATA ACK/REQ --> NC01 (0xA3C) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1668 | cmd->cmd = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1669 | cmd->src = |
| 1670 | paddr(&data->adm_mux_data_ack_req_nc01); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1671 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1672 | cmd->len = 4; |
| 1673 | cmd++; |
| 1674 | |
| 1675 | /* block on data ready from NC10, then |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1676 | * read the status register |
| 1677 | */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1678 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 1679 | cmd->src = NC10(NAND_FLASH_STATUS); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1680 | cmd->dst = paddr(&data->result[n - 1]); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1681 | cmd->len = 4; |
| 1682 | cmd++; |
| 1683 | } |
| 1684 | } else { |
| 1685 | /* kick the execute register */ |
| 1686 | cmd->cmd = 0; |
| 1687 | cmd->src = paddr(&data->exec); |
| 1688 | cmd->dst = NC10(NAND_EXEC_CMD); |
| 1689 | cmd->len = 4; |
| 1690 | cmd++; |
| 1691 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1692 | /* MASK DATA ACK/REQ --> NC10 (0xF28) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1693 | cmd->cmd = 0; |
| 1694 | cmd->src = paddr(&data->adm_mux_data_ack_req_nc10); |
| 1695 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1696 | cmd->len = 4; |
| 1697 | cmd++; |
| 1698 | |
| 1699 | /* block on data ready from NC01, then |
| 1700 | * read the status register |
| 1701 | */ |
| 1702 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 1703 | cmd->src = NC01(NAND_FLASH_STATUS); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1704 | cmd->dst = paddr(&data->result[n - 1]); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1705 | cmd->len = 4; |
| 1706 | cmd++; |
| 1707 | } |
| 1708 | } |
| 1709 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1710 | /* MASK DATA ACK/REQ --> NC01 (0xA3C) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1711 | cmd->cmd = 0; |
| 1712 | cmd->src = paddr(&data->adm_mux_data_ack_req_nc01); |
| 1713 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1714 | cmd->len = 4; |
| 1715 | cmd++; |
| 1716 | |
| 1717 | /* we should process outstanding request */ |
| 1718 | /* block on data ready, then |
| 1719 | * read the status register |
| 1720 | */ |
| 1721 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 1722 | cmd->src = NC10(NAND_FLASH_STATUS); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1723 | cmd->dst = paddr(&data->result[n - 1]); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1724 | cmd->len = 4; |
| 1725 | cmd++; |
| 1726 | |
| 1727 | /* restore saved ecc config */ |
| 1728 | cmd->cmd = 0; |
| 1729 | cmd->src = paddr(&data->ecc_cfg_save); |
| 1730 | cmd->dst = NAND_EBI2_ECC_BUF_CFG; |
| 1731 | cmd->len = 4; |
| 1732 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1733 | /* MASK DATA ACK/REQ --> NC01 (0xFC0) */ |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1734 | cmd->cmd = 0; |
| 1735 | cmd->src = paddr(&data->adm_default_mux); |
| 1736 | cmd->dst = EBI2_NAND_ADM_MUX; |
| 1737 | cmd->len = 4; |
| 1738 | cmd++; |
| 1739 | |
| 1740 | /* disable CS1 */ |
| 1741 | cmd->cmd = CMD_OCU | CMD_LC; |
| 1742 | cmd->src = paddr(&data->default_ebi2_chip_select_cfg0); |
| 1743 | cmd->dst = EBI2_CHIP_SELECT_CFG0; |
| 1744 | cmd->len = 4; |
| 1745 | cmd++; |
| 1746 | |
| 1747 | ptr[0] = (paddr(cmdlist) >> 3) | CMD_PTR_LP; |
| 1748 | |
| 1749 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 1750 | |
| 1751 | #if VERBOSE |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1752 | dprintf(INFO, "write page %d: status: %x %x %x %x %x %x %x %x \ |
| 1753 | %x %x %x %x %x %x %x %x \n", page, data->result[0].flash_status[0], data->result[1].flash_status[1], data->result[2].flash_status[2], data->result[3].flash_status[3], data->result[4].flash_status[4], data->result[5].flash_status[5], data->result[6].flash_status[6], data->result[7].flash_status[7], data->result[8].flash_status[8], data->result[9].flash_status[9], data->result[10].flash_status[10], data->result[11].flash_status[11], data->result[12].flash_status[12], data->result[13].flash_status[13], data->result[14].flash_status[14], data->result[15].flash_status[15]); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1754 | #endif |
| 1755 | |
| 1756 | /* if any of the writes failed (0x10), or there was a |
| 1757 | ** protection violation (0x100), or the program success |
| 1758 | ** bit (0x80) is unset, we lose |
| 1759 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1760 | for (n = 0; n < cwperpage; n++) { |
| 1761 | if (data->result[n].flash_status & 0x110) |
| 1762 | return -1; |
| 1763 | if (!(data->result[n].flash_status & 0x80)) |
| 1764 | return -1; |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | #if VERIFY_WRITE |
| 1768 | n = _flash_read_page(cmdlist, ptrlist, page, flash_data, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1769 | flash_data + 2048); |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1770 | if (n != 0) |
| 1771 | return -1; |
| 1772 | if (memcmp(flash_data, _addr, 2048) || |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1773 | memcmp(flash_data + 2048, _spareaddr, 16)) { |
Murali Nalajala | a8c94c6 | 2010-03-05 20:24:30 +0530 | [diff] [blame] | 1774 | dprintf(CRITICAL, "verify error @ page %d\n", page); |
| 1775 | return -1; |
| 1776 | } |
| 1777 | #endif |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 1781 | char empty_buf[528]; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1782 | static int |
| 1783 | flash_nand_mark_badblock(dmov_s * cmdlist, unsigned *ptrlist, unsigned page) |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 1784 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1785 | memset(empty_buf, 0, 528); |
| 1786 | /* Going to first page of the block */ |
| 1787 | if (page & num_pages_per_blk_mask) |
| 1788 | page = page - (page & num_pages_per_blk_mask); |
| 1789 | return _flash_nand_write_page(cmdlist, ptrlist, page, empty_buf, 0, 1); |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 1790 | } |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1791 | |
Shashank Mittal | 83d16d0 | 2009-11-18 16:54:42 -0800 | [diff] [blame] | 1792 | unsigned nand_cfg0; |
| 1793 | unsigned nand_cfg1; |
| 1794 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1795 | static int flash_nand_read_config(dmov_s * cmdlist, unsigned *ptrlist) |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1796 | { |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1797 | static unsigned CFG0_TMP, CFG1_TMP; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1798 | cmdlist[0].cmd = CMD_OCB; |
| 1799 | cmdlist[0].src = NAND_DEV0_CFG0; |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1800 | cmdlist[0].dst = paddr(&CFG0_TMP); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1801 | cmdlist[0].len = 4; |
| 1802 | |
| 1803 | cmdlist[1].cmd = CMD_OCU | CMD_LC; |
| 1804 | cmdlist[1].src = NAND_DEV0_CFG1; |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1805 | cmdlist[1].dst = paddr(&CFG1_TMP); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1806 | cmdlist[1].len = 4; |
| 1807 | |
| 1808 | *ptrlist = (paddr(cmdlist) >> 3) | CMD_PTR_LP; |
| 1809 | |
| 1810 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptrlist); |
| 1811 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1812 | if ((CFG0_TMP == 0) || (CFG1_TMP == 0)) { |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1813 | return -1; |
| 1814 | } |
| 1815 | |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1816 | CFG0_A = CFG0_TMP; |
| 1817 | CFG1_A = CFG1_TMP; |
Shashank Mittal | 83d16d0 | 2009-11-18 16:54:42 -0800 | [diff] [blame] | 1818 | if (flash_info.type == FLASH_16BIT_NAND_DEVICE) { |
| 1819 | nand_cfg1 |= CFG1_WIDE_FLASH; |
| 1820 | } |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1821 | dprintf(INFO, "nandcfg: %x %x (initial)\n", CFG0, CFG1); |
| 1822 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1823 | CFG0_A = (((flash_pagesize >> 9) - 1) << 6) /* 4/8 cw/pg for 2/4k */ |
| 1824 | |(516 << 9) /* 516 user data bytes */ |
| 1825 | |(10 << 19) /* 10 parity bytes */ |
| 1826 | |(5 << 27) /* 5 address cycles */ |
| 1827 | |(0 << 30) /* Do not read status before data */ |
| 1828 | |(1 << 31) |
| 1829 | /* Send read cmd */ |
| 1830 | /* 0 spare bytes for 16 bit nand or 1 spare bytes for 8 bit */ |
| 1831 | |((nand_cfg1 & CFG1_WIDE_FLASH) ? (0 << 23) : (enable_bch_ecc ? (2 << 23) : (1 << 23))); /* 2 spare bytes for 8 bit bch ecc */ |
| 1832 | CFG1_A = (0 << 0) /* Enable ecc */ |
| 1833 | |(7 << 2) /* 8 recovery cycles */ |
| 1834 | |(0 << 5) /* Allow CS deassertion */ |
| 1835 | |((flash_pagesize - ((enable_bch_ecc ? 532 : 528) * ((flash_pagesize >> 9) - 1)) + 1) << 6) /* Bad block marker location */ |
| 1836 | |(0 << 16) /* Bad block in user data area */ |
| 1837 | |(2 << 17) /* 6 cycle tWB/tRB */ |
| 1838 | |(nand_cfg1 & CFG1_WIDE_FLASH); /* preserve wide flash flag */ |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1839 | |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 1840 | NAND_CFG0_RAW = CFG0_RAW; |
| 1841 | NAND_CFG1_RAW = CFG1_RAW; |
| 1842 | |
| 1843 | if (enable_bch_ecc) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1844 | CFG1_A |= (1 << 27); /* Enable BCH engine */ |
| 1845 | ECC_BCH_CFG = (0 << 0) /* Enable ECC */ |
| 1846 | |(0 << 1) /* Enable/Disable SW reset of ECC engine */ |
| 1847 | |(1 << 4) /* 8bit ecc */ |
| 1848 | |((nand_cfg1 & CFG1_WIDE_FLASH) ? (14 << 8) : (13 << 8)) /*parity bytes */ |
| 1849 | |(516 << 16) /* 516 user data bytes */ |
| 1850 | |(1 << 30); /* Turn on ECC engine clocks always */ |
| 1851 | NAND_CFG0_RAW = CFG0_RAW_BCHECC; /* CW size is increased to 532B */ |
| 1852 | } |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 1853 | |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1854 | dprintf(INFO, "nandcfg(Apps): %x %x (used)\n", CFG0_A, CFG1_A); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1855 | |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1856 | CFG0_M = CFG0_TMP; |
| 1857 | CFG1_M = CFG1_TMP; |
| 1858 | if (flash_info.type == FLASH_16BIT_NAND_DEVICE) { |
| 1859 | nand_cfg1 |= CFG1_WIDE_FLASH; |
| 1860 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1861 | CFG0_M = (((flash_pagesize >> 9) - 1) << 6) /* 4/8 cw/pg for 2/4k */ |
| 1862 | |(512 << 9) /* 512 user data bytes */ |
| 1863 | |(10 << 19) /* 10 parity bytes */ |
| 1864 | |(5 << 27) /* 5 address cycles */ |
| 1865 | |(0 << 30) /* Do not read status before data */ |
| 1866 | |(1 << 31) /* Send read cmd */ |
| 1867 | |((nand_cfg1 & CFG1_WIDE_FLASH) ? (4 << 23) : (5 << 23)); |
| 1868 | CFG1_M = (0 << 0) /* Enable ecc */ |
| 1869 | |(7 << 2) /* 8 recovery cycles */ |
| 1870 | |(0 << 5) /* Allow CS deassertion */ |
| 1871 | |((flash_pagesize - (528 * ((flash_pagesize >> 9) - 1)) + 1) << 6) /* Bad block marker location */ |
| 1872 | |(0 << 16) /* Bad block in user data area */ |
| 1873 | |(2 << 17) /* 6 cycle tWB/tRB */ |
| 1874 | |(nand_cfg1 & CFG1_WIDE_FLASH); /* preserve wide flash flag */ |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 1875 | dprintf(INFO, "nandcfg(Modem): %x %x (used)\n", CFG0_M, CFG1_M); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 1876 | return 0; |
| 1877 | } |
| 1878 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 1879 | /* OneNAND programming functions */ |
| 1880 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1881 | static void flash_onenand_read_id(dmov_s * cmdlist, unsigned *ptrlist) |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 1882 | { |
| 1883 | dmov_s *cmd = cmdlist; |
| 1884 | unsigned *ptr = ptrlist; |
| 1885 | unsigned *data = ptrlist + 4; |
| 1886 | |
| 1887 | data[0] = SFLASH_BCFG; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1888 | data[1] = |
| 1889 | SFLASH_PREPCMD(8, 0, 0, NAND_SFCMD_DATXS, NAND_SFCMD_ASYNC, |
| 1890 | NAND_SFCMD_REGRD); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 1891 | data[2] = (ONENAND_DEVICE_ID << 16) | (ONENAND_MANUFACTURER_ID); |
| 1892 | data[3] = (ONENAND_DATA_BUFFER_SIZE << 16) | (ONENAND_VERSION_ID); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1893 | data[4] = |
| 1894 | (ONENAND_AMOUNT_OF_BUFFERS << 16) | (ONENAND_BOOT_BUFFER_SIZE); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 1895 | data[5] = (CLEAN_DATA_16 << 16) | (ONENAND_TECHNOLOGY); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1896 | data[6] = CLEAN_DATA_32; //status |
| 1897 | data[7] = CLEAN_DATA_32; //register read |
| 1898 | data[8] = CLEAN_DATA_32; //register read |
| 1899 | data[9] = CLEAN_DATA_32; //register read |
| 1900 | data[10] = CLEAN_DATA_32; //register read |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 1901 | data[11] = 1; |
| 1902 | data[12] = 0 | 4; |
| 1903 | |
| 1904 | /* Setup controller in SFLASH mode */ |
| 1905 | cmd[0].cmd = 0 | CMD_OCB; |
| 1906 | cmd[0].src = paddr(&data[0]); |
| 1907 | cmd[0].dst = NAND_SFLASHC_BURST_CFG; |
| 1908 | cmd[0].len = 4; |
| 1909 | |
| 1910 | /* Enable data mover for controller */ |
| 1911 | cmd[1].cmd = 0; |
| 1912 | cmd[1].src = paddr(&data[12]); |
| 1913 | cmd[1].dst = NAND_FLASH_CHIP_SELECT; |
| 1914 | cmd[1].len = 4; |
| 1915 | |
| 1916 | /* Setup SFLASHC_CMD with xfers in async mode */ |
| 1917 | cmd[2].cmd = DST_CRCI_NAND_CMD; |
| 1918 | cmd[2].src = paddr(&data[1]); |
| 1919 | cmd[2].dst = NAND_SFLASHC_CMD; |
| 1920 | cmd[2].len = 4; |
| 1921 | |
| 1922 | /* Setup to read device information */ |
| 1923 | cmd[3].cmd = 0; |
| 1924 | cmd[3].src = paddr(&data[2]); |
| 1925 | cmd[3].dst = NAND_ADDR0; |
| 1926 | cmd[3].len = 8; |
| 1927 | |
| 1928 | cmd[4].cmd = 0; |
| 1929 | cmd[4].src = paddr(&data[4]); |
| 1930 | cmd[4].dst = NAND_ADDR2; |
| 1931 | cmd[4].len = 8; |
| 1932 | |
| 1933 | /* Set execute bit */ |
| 1934 | cmd[5].cmd = 0; |
| 1935 | cmd[5].src = paddr(&data[11]); |
| 1936 | cmd[5].dst = NAND_SFLASHC_EXEC_CMD; |
| 1937 | cmd[5].len = 4; |
| 1938 | |
| 1939 | /* Check status */ |
| 1940 | cmd[6].cmd = SRC_CRCI_NAND_DATA; |
| 1941 | cmd[6].src = NAND_SFLASHC_STATUS; |
| 1942 | cmd[6].dst = paddr(&data[6]); |
| 1943 | cmd[6].len = 4; |
| 1944 | |
| 1945 | /* Read result device registers */ |
| 1946 | cmd[7].cmd = 0 | CMD_OCU | CMD_LC; |
| 1947 | cmd[7].src = NAND_GENP_REG0; |
| 1948 | cmd[7].dst = paddr(&data[7]); |
| 1949 | cmd[7].len = 16; |
| 1950 | |
| 1951 | ptr[0] = (paddr(cmd) >> 3) | CMD_PTR_LP; |
| 1952 | |
| 1953 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 1954 | |
| 1955 | #if VERBOSE |
| 1956 | dprintf(INFO, "status: %x\n", data[6]); |
| 1957 | #endif |
| 1958 | |
| 1959 | flash_info.id = data[7]; |
| 1960 | flash_info.vendor = data[7] & CLEAN_DATA_16; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1961 | flash_info.device = (data[7] >> 16) & CLEAN_DATA_16; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 1962 | return; |
| 1963 | } |
| 1964 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 1965 | struct data_onenand_erase { |
| 1966 | unsigned sfbcfg; |
| 1967 | unsigned sfcmd[4]; |
| 1968 | unsigned sfexec; |
| 1969 | unsigned sfstat[4]; |
| 1970 | unsigned addr0; |
| 1971 | unsigned addr1; |
| 1972 | unsigned addr2; |
| 1973 | unsigned addr3; |
| 1974 | unsigned addr4; |
| 1975 | unsigned addr5; |
| 1976 | unsigned addr6; |
| 1977 | unsigned data0; |
| 1978 | unsigned data1; |
| 1979 | unsigned data2; |
| 1980 | unsigned data3; |
| 1981 | unsigned data4; |
| 1982 | unsigned data5; |
| 1983 | unsigned data6; |
| 1984 | }; |
| 1985 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1986 | static int _flash_onenand_read_page(dmov_s * cmdlist, unsigned *ptrlist, |
| 1987 | unsigned page, void *_addr, |
| 1988 | void *_spareaddr, unsigned raw_mode); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 1989 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1990 | static int |
| 1991 | flash_onenand_block_isbad(dmov_s * cmdlist, unsigned *ptrlist, unsigned page) |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 1992 | { |
| 1993 | unsigned char page_data[2112]; |
| 1994 | unsigned char *oobptr = &(page_data[2048]); |
| 1995 | |
| 1996 | /* Going to first page of the block */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1997 | if (page & num_pages_per_blk_mask) |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 1998 | page = page - (page & num_pages_per_blk_mask); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 1999 | |
| 2000 | /* Reading page in raw mode */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2001 | if (_flash_onenand_read_page(cmdlist, ptrlist, page, page_data, 0, 1)) |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2002 | return 1; |
| 2003 | |
| 2004 | /* Checking if block is bad */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2005 | if ((oobptr[0] != 0xFF) || (oobptr[1] != 0xFF) || |
| 2006 | (oobptr[16] != 0xFF) || (oobptr[17] != 0xFF) || |
| 2007 | (oobptr[32] != 0xFF) || (oobptr[33] != 0xFF) || |
| 2008 | (oobptr[48] != 0xFF) || (oobptr[49] != 0xFF)) { |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2009 | return 1; |
| 2010 | } |
| 2011 | return 0; |
| 2012 | } |
| 2013 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2014 | static int |
| 2015 | flash_onenand_erase_block(dmov_s * cmdlist, unsigned *ptrlist, unsigned page) |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2016 | { |
| 2017 | dmov_s *cmd = cmdlist; |
| 2018 | unsigned *ptr = ptrlist; |
| 2019 | struct data_onenand_erase *data = (void *)ptrlist + 4; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2020 | int isbad = 0; |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 2021 | unsigned erasesize = (flash_pagesize * num_pages_per_blk); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2022 | unsigned onenand_startaddr1 = |
| 2023 | DEVICE_FLASHCORE_0 | (page * flash_pagesize) / erasesize; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2024 | unsigned onenand_startaddr8 = 0x0000; |
| 2025 | unsigned onenand_startaddr2 = DEVICE_BUFFERRAM_0 << 15; |
| 2026 | unsigned onenand_startbuffer = DATARAM0_0 << 8; |
| 2027 | |
| 2028 | unsigned controller_status; |
| 2029 | unsigned interrupt_status; |
| 2030 | unsigned ecc_status; |
| 2031 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2032 | if ((page * flash_pagesize) & (erasesize - 1)) |
| 2033 | return -1; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2034 | |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2035 | /* Check for bad block and erase only if block is not marked bad */ |
| 2036 | isbad = flash_onenand_block_isbad(cmdlist, ptrlist, page); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2037 | if (isbad) { |
| 2038 | dprintf(INFO, "skipping @ %d (bad block)\n", |
| 2039 | page / num_pages_per_blk); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2040 | return -1; |
| 2041 | } |
| 2042 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2043 | /*Erase block */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2044 | onenand_startaddr1 = DEVICE_FLASHCORE_0 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2045 | ((page * flash_pagesize) / (erasesize)); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2046 | onenand_startaddr8 = 0x0000; |
| 2047 | onenand_startaddr2 = DEVICE_BUFFERRAM_0 << 15; |
| 2048 | onenand_startbuffer = DATARAM0_0 << 8; |
| 2049 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2050 | data->sfbcfg = SFLASH_BCFG; |
| 2051 | data->sfcmd[0] = SFLASH_PREPCMD(7, 0, 0, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2052 | NAND_SFCMD_CMDXS, |
| 2053 | NAND_SFCMD_ASYNC, NAND_SFCMD_REGWR); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2054 | data->sfcmd[1] = SFLASH_PREPCMD(0, 0, 32, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2055 | NAND_SFCMD_CMDXS, |
| 2056 | NAND_SFCMD_ASYNC, NAND_SFCMD_INTHI); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2057 | data->sfcmd[2] = SFLASH_PREPCMD(3, 7, 0, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2058 | NAND_SFCMD_DATXS, |
| 2059 | NAND_SFCMD_ASYNC, NAND_SFCMD_REGRD); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2060 | data->sfcmd[3] = SFLASH_PREPCMD(4, 10, 0, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2061 | NAND_SFCMD_CMDXS, |
| 2062 | NAND_SFCMD_ASYNC, NAND_SFCMD_REGWR); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2063 | data->sfexec = 1; |
| 2064 | data->sfstat[0] = CLEAN_DATA_32; |
| 2065 | data->sfstat[1] = CLEAN_DATA_32; |
| 2066 | data->sfstat[2] = CLEAN_DATA_32; |
| 2067 | data->sfstat[3] = CLEAN_DATA_32; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2068 | data->addr0 = |
| 2069 | (ONENAND_INTERRUPT_STATUS << 16) | (ONENAND_SYSTEM_CONFIG_1); |
| 2070 | data->addr1 = |
| 2071 | (ONENAND_START_ADDRESS_8 << 16) | (ONENAND_START_ADDRESS_1); |
| 2072 | data->addr2 = (ONENAND_START_BUFFER << 16) | (ONENAND_START_ADDRESS_2); |
| 2073 | data->addr3 = (ONENAND_ECC_STATUS << 16) | (ONENAND_COMMAND); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2074 | data->addr4 = (ONENAND_CONTROLLER_STATUS << 16) | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2075 | (ONENAND_INTERRUPT_STATUS); |
| 2076 | data->addr5 = |
| 2077 | (ONENAND_INTERRUPT_STATUS << 16) | (ONENAND_SYSTEM_CONFIG_1); |
| 2078 | data->addr6 = |
| 2079 | (ONENAND_START_ADDRESS_3 << 16) | (ONENAND_START_ADDRESS_1); |
| 2080 | data->data0 = (ONENAND_CLRINTR << 16) | (ONENAND_SYSCFG1_ECCENA); |
| 2081 | data->data1 = (onenand_startaddr8 << 16) | (onenand_startaddr1); |
| 2082 | data->data2 = (onenand_startbuffer << 16) | (onenand_startaddr2); |
| 2083 | data->data3 = (CLEAN_DATA_16 << 16) | (ONENAND_CMDERAS); |
| 2084 | data->data4 = (CLEAN_DATA_16 << 16) | (CLEAN_DATA_16); |
| 2085 | data->data5 = (ONENAND_CLRINTR << 16) | (ONENAND_SYSCFG1_ECCENA); |
| 2086 | data->data6 = (ONENAND_STARTADDR3_RES << 16) | (ONENAND_STARTADDR1_RES); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2087 | |
| 2088 | /***************************************************************/ |
| 2089 | /* Write the necessary address registers in the onenand device */ |
| 2090 | /***************************************************************/ |
| 2091 | |
| 2092 | /* Enable and configure the SFlash controller */ |
| 2093 | cmd->cmd = 0 | CMD_OCB; |
| 2094 | cmd->src = paddr(&data->sfbcfg); |
| 2095 | cmd->dst = NAND_SFLASHC_BURST_CFG; |
| 2096 | cmd->len = 4; |
| 2097 | cmd++; |
| 2098 | |
| 2099 | /* Block on cmd ready and write CMD register */ |
| 2100 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2101 | cmd->src = paddr(&data->sfcmd[0]); |
| 2102 | cmd->dst = NAND_SFLASHC_CMD; |
| 2103 | cmd->len = 4; |
| 2104 | cmd++; |
| 2105 | |
| 2106 | /* Write the ADDR0 and ADDR1 registers */ |
| 2107 | cmd->cmd = 0; |
| 2108 | cmd->src = paddr(&data->addr0); |
| 2109 | cmd->dst = NAND_ADDR0; |
| 2110 | cmd->len = 8; |
| 2111 | cmd++; |
| 2112 | |
| 2113 | /* Write the ADDR2 ADDR3 ADDR4 ADDR5 registers */ |
| 2114 | cmd->cmd = 0; |
| 2115 | cmd->src = paddr(&data->addr2); |
| 2116 | cmd->dst = NAND_ADDR2; |
| 2117 | cmd->len = 16; |
| 2118 | cmd++; |
| 2119 | |
| 2120 | /* Write the ADDR6 registers */ |
| 2121 | cmd->cmd = 0; |
| 2122 | cmd->src = paddr(&data->addr6); |
| 2123 | cmd->dst = NAND_ADDR6; |
| 2124 | cmd->len = 4; |
| 2125 | cmd++; |
| 2126 | |
| 2127 | /* Write the GENP0, GENP1, GENP2, GENP3, GENP4 registers */ |
| 2128 | cmd->cmd = 0; |
| 2129 | cmd->src = paddr(&data->data0); |
| 2130 | cmd->dst = NAND_GENP_REG0; |
| 2131 | cmd->len = 16; |
| 2132 | cmd++; |
| 2133 | |
| 2134 | /* Write the FLASH_DEV_CMD4,5,6 registers */ |
| 2135 | cmd->cmd = 0; |
| 2136 | cmd->src = paddr(&data->data4); |
| 2137 | cmd->dst = NAND_DEV_CMD4; |
| 2138 | cmd->len = 12; |
| 2139 | cmd++; |
| 2140 | |
| 2141 | /* Kick the execute command */ |
| 2142 | cmd->cmd = 0; |
| 2143 | cmd->src = paddr(&data->sfexec); |
| 2144 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2145 | cmd->len = 4; |
| 2146 | cmd++; |
| 2147 | |
| 2148 | /* Block on data ready, and read the status register */ |
| 2149 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2150 | cmd->src = NAND_SFLASHC_STATUS; |
| 2151 | cmd->dst = paddr(&data->sfstat[0]); |
| 2152 | cmd->len = 4; |
| 2153 | cmd++; |
| 2154 | |
| 2155 | /***************************************************************/ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2156 | /* Wait for the interrupt from the Onenand device controller */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2157 | /***************************************************************/ |
| 2158 | |
| 2159 | /* Block on cmd ready and write CMD register */ |
| 2160 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2161 | cmd->src = paddr(&data->sfcmd[1]); |
| 2162 | cmd->dst = NAND_SFLASHC_CMD; |
| 2163 | cmd->len = 4; |
| 2164 | cmd++; |
| 2165 | |
| 2166 | /* Kick the execute command */ |
| 2167 | cmd->cmd = 0; |
| 2168 | cmd->src = paddr(&data->sfexec); |
| 2169 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2170 | cmd->len = 4; |
| 2171 | cmd++; |
| 2172 | |
| 2173 | /* Block on data ready, and read the status register */ |
| 2174 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2175 | cmd->src = NAND_SFLASHC_STATUS; |
| 2176 | cmd->dst = paddr(&data->sfstat[1]); |
| 2177 | cmd->len = 4; |
| 2178 | cmd++; |
| 2179 | |
| 2180 | /***************************************************************/ |
| 2181 | /* Read the necessary status registers from the onenand device */ |
| 2182 | /***************************************************************/ |
| 2183 | |
| 2184 | /* Block on cmd ready and write CMD register */ |
| 2185 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2186 | cmd->src = paddr(&data->sfcmd[2]); |
| 2187 | cmd->dst = NAND_SFLASHC_CMD; |
| 2188 | cmd->len = 4; |
| 2189 | cmd++; |
| 2190 | |
| 2191 | /* Kick the execute command */ |
| 2192 | cmd->cmd = 0; |
| 2193 | cmd->src = paddr(&data->sfexec); |
| 2194 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2195 | cmd->len = 4; |
| 2196 | cmd++; |
| 2197 | |
| 2198 | /* Block on data ready, and read the status register */ |
| 2199 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2200 | cmd->src = NAND_SFLASHC_STATUS; |
| 2201 | cmd->dst = paddr(&data->sfstat[2]); |
| 2202 | cmd->len = 4; |
| 2203 | cmd++; |
| 2204 | |
| 2205 | /* Read the GENP3 register */ |
| 2206 | cmd->cmd = 0; |
| 2207 | cmd->src = NAND_GENP_REG3; |
| 2208 | cmd->dst = paddr(&data->data3); |
| 2209 | cmd->len = 4; |
| 2210 | cmd++; |
| 2211 | |
| 2212 | /* Read the DEVCMD4 register */ |
| 2213 | cmd->cmd = 0; |
| 2214 | cmd->src = NAND_DEV_CMD4; |
| 2215 | cmd->dst = paddr(&data->data4); |
| 2216 | cmd->len = 4; |
| 2217 | cmd++; |
| 2218 | |
| 2219 | /***************************************************************/ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2220 | /* Restore the necessary registers to proper values */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2221 | /***************************************************************/ |
| 2222 | |
| 2223 | /* Block on cmd ready and write CMD register */ |
| 2224 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2225 | cmd->src = paddr(&data->sfcmd[3]); |
| 2226 | cmd->dst = NAND_SFLASHC_CMD; |
| 2227 | cmd->len = 4; |
| 2228 | cmd++; |
| 2229 | |
| 2230 | /* Kick the execute command */ |
| 2231 | cmd->cmd = 0; |
| 2232 | cmd->src = paddr(&data->sfexec); |
| 2233 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2234 | cmd->len = 4; |
| 2235 | cmd++; |
| 2236 | |
| 2237 | /* Block on data ready, and read the status register */ |
| 2238 | cmd->cmd = SRC_CRCI_NAND_DATA | CMD_OCU | CMD_LC; |
| 2239 | cmd->src = NAND_SFLASHC_STATUS; |
| 2240 | cmd->dst = paddr(&data->sfstat[3]); |
| 2241 | cmd->len = 4; |
| 2242 | cmd++; |
| 2243 | |
| 2244 | ptr[0] = (paddr(cmdlist) >> 3) | CMD_PTR_LP; |
| 2245 | |
| 2246 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 2247 | |
| 2248 | ecc_status = (data->data3 >> 16) & 0x0000FFFF; |
| 2249 | interrupt_status = (data->data4 >> 0) & 0x0000FFFF; |
| 2250 | controller_status = (data->data4 >> 16) & 0x0000FFFF; |
| 2251 | |
| 2252 | #if VERBOSE |
| 2253 | dprintf(INFO, "\n%s: sflash status %x %x %x %x\n", __func__, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2254 | data->sfstat[0], |
| 2255 | data->sfstat[1], data->sfstat[2], data->sfstat[3]); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2256 | |
| 2257 | dprintf(INFO, "%s: controller_status = %x\n", __func__, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2258 | controller_status); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2259 | dprintf(INFO, "%s: interrupt_status = %x\n", __func__, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2260 | interrupt_status); |
| 2261 | dprintf(INFO, "%s: ecc_status = %x\n", __func__, ecc_status); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2262 | #endif |
| 2263 | /* Check for errors, protection violations etc */ |
| 2264 | if ((controller_status != 0) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2265 | || (data->sfstat[0] & 0x110) |
| 2266 | || (data->sfstat[1] & 0x110) |
| 2267 | || (data->sfstat[2] & 0x110) || (data->sfstat[3] & 0x110)) { |
| 2268 | dprintf(CRITICAL, "%s: ECC/MPU/OP error\n", __func__); |
| 2269 | return -1; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2270 | } |
| 2271 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2272 | #if VERBOSE |
| 2273 | dprintf(INFO, "status: %x\n", data[5]); |
| 2274 | #endif |
| 2275 | |
| 2276 | return 0; |
| 2277 | } |
| 2278 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2279 | struct data_onenand_read { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2280 | unsigned sfbcfg; |
| 2281 | unsigned sfcmd[9]; |
| 2282 | unsigned sfexec; |
| 2283 | unsigned sfstat[9]; |
| 2284 | unsigned addr0; |
| 2285 | unsigned addr1; |
| 2286 | unsigned addr2; |
| 2287 | unsigned addr3; |
| 2288 | unsigned addr4; |
| 2289 | unsigned addr5; |
| 2290 | unsigned addr6; |
| 2291 | unsigned data0; |
| 2292 | unsigned data1; |
| 2293 | unsigned data2; |
| 2294 | unsigned data3; |
| 2295 | unsigned data4; |
| 2296 | unsigned data5; |
| 2297 | unsigned data6; |
| 2298 | unsigned macro[5]; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2299 | }; |
| 2300 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2301 | static int |
| 2302 | _flash_onenand_read_page(dmov_s * cmdlist, unsigned *ptrlist, |
| 2303 | unsigned page, void *_addr, void *_spareaddr, |
| 2304 | unsigned raw_mode) |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2305 | { |
| 2306 | dmov_s *cmd = cmdlist; |
| 2307 | unsigned *ptr = ptrlist; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2308 | struct data_onenand_read *data = (void *)(ptrlist + 4); |
| 2309 | unsigned addr = (unsigned)_addr; |
| 2310 | unsigned curr_addr = (unsigned)_addr; |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 2311 | #if VERBOSE |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2312 | unsigned spareaddr = (unsigned)_spareaddr; |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 2313 | #endif |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2314 | unsigned i; |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 2315 | unsigned erasesize = (flash_pagesize * num_pages_per_blk); |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2316 | unsigned writesize = flash_pagesize; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2317 | |
| 2318 | unsigned onenand_startaddr1 = DEVICE_FLASHCORE_0 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2319 | ((unsigned)(page * flash_pagesize) / erasesize); |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2320 | unsigned onenand_startaddr8 = (((unsigned)(page * flash_pagesize) & |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2321 | (erasesize - 1)) / writesize) << 2; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2322 | unsigned onenand_startaddr2 = DEVICE_BUFFERRAM_0 << 15; |
| 2323 | unsigned onenand_startbuffer = DATARAM0_0 << 8; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2324 | unsigned onenand_sysconfig1 = (raw_mode == 1) ? ONENAND_SYSCFG1_ECCDIS : |
| 2325 | ONENAND_SYSCFG1_ECCENA; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2326 | |
| 2327 | unsigned controller_status; |
| 2328 | unsigned interrupt_status; |
| 2329 | unsigned ecc_status; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2330 | if (raw_mode != 1) { |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2331 | int isbad = 0; |
| 2332 | isbad = flash_onenand_block_isbad(cmdlist, ptrlist, page); |
| 2333 | if (isbad) |
| 2334 | return -2; |
| 2335 | } |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2336 | //static int oobfree_offset[8] = {2, 14, 18, 30, 34, 46, 50, 62}; |
| 2337 | //static int oobfree_length[8] = {3, 2, 3, 2, 3, 2, 3, 2}; |
| 2338 | |
| 2339 | data->sfbcfg = SFLASH_BCFG; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2340 | data->sfcmd[0] = SFLASH_PREPCMD(7, 0, 0, |
| 2341 | NAND_SFCMD_CMDXS, |
| 2342 | NAND_SFCMD_ASYNC, NAND_SFCMD_REGWR); |
| 2343 | data->sfcmd[1] = SFLASH_PREPCMD(0, 0, 32, |
| 2344 | NAND_SFCMD_CMDXS, |
| 2345 | NAND_SFCMD_ASYNC, NAND_SFCMD_INTHI); |
| 2346 | data->sfcmd[2] = SFLASH_PREPCMD(3, 7, 0, |
| 2347 | NAND_SFCMD_DATXS, |
| 2348 | NAND_SFCMD_ASYNC, NAND_SFCMD_REGRD); |
| 2349 | data->sfcmd[3] = SFLASH_PREPCMD(256, 0, 0, |
| 2350 | NAND_SFCMD_DATXS, |
| 2351 | NAND_SFCMD_ASYNC, NAND_SFCMD_DATRD); |
| 2352 | data->sfcmd[4] = SFLASH_PREPCMD(256, 0, 0, |
| 2353 | NAND_SFCMD_DATXS, |
| 2354 | NAND_SFCMD_ASYNC, NAND_SFCMD_DATRD); |
| 2355 | data->sfcmd[5] = SFLASH_PREPCMD(256, 0, 0, |
| 2356 | NAND_SFCMD_DATXS, |
| 2357 | NAND_SFCMD_ASYNC, NAND_SFCMD_DATRD); |
| 2358 | data->sfcmd[6] = SFLASH_PREPCMD(256, 0, 0, |
| 2359 | NAND_SFCMD_DATXS, |
| 2360 | NAND_SFCMD_ASYNC, NAND_SFCMD_DATRD); |
| 2361 | data->sfcmd[7] = SFLASH_PREPCMD(32, 0, 0, |
| 2362 | NAND_SFCMD_DATXS, |
| 2363 | NAND_SFCMD_ASYNC, NAND_SFCMD_DATRD); |
| 2364 | data->sfcmd[8] = SFLASH_PREPCMD(4, 10, 0, |
| 2365 | NAND_SFCMD_CMDXS, |
| 2366 | NAND_SFCMD_ASYNC, NAND_SFCMD_REGWR); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2367 | data->sfexec = 1; |
| 2368 | data->sfstat[0] = CLEAN_DATA_32; |
| 2369 | data->sfstat[1] = CLEAN_DATA_32; |
| 2370 | data->sfstat[2] = CLEAN_DATA_32; |
| 2371 | data->sfstat[3] = CLEAN_DATA_32; |
| 2372 | data->sfstat[4] = CLEAN_DATA_32; |
| 2373 | data->sfstat[5] = CLEAN_DATA_32; |
| 2374 | data->sfstat[6] = CLEAN_DATA_32; |
| 2375 | data->sfstat[7] = CLEAN_DATA_32; |
| 2376 | data->sfstat[8] = CLEAN_DATA_32; |
| 2377 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2378 | data->addr0 = |
| 2379 | (ONENAND_INTERRUPT_STATUS << 16) | (ONENAND_SYSTEM_CONFIG_1); |
| 2380 | data->addr1 = |
| 2381 | (ONENAND_START_ADDRESS_8 << 16) | (ONENAND_START_ADDRESS_1); |
| 2382 | data->addr2 = (ONENAND_START_BUFFER << 16) | (ONENAND_START_ADDRESS_2); |
| 2383 | data->addr3 = (ONENAND_ECC_STATUS << 16) | (ONENAND_COMMAND); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2384 | data->addr4 = (ONENAND_CONTROLLER_STATUS << 16) | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2385 | (ONENAND_INTERRUPT_STATUS); |
| 2386 | data->addr5 = |
| 2387 | (ONENAND_INTERRUPT_STATUS << 16) | (ONENAND_SYSTEM_CONFIG_1); |
| 2388 | data->addr6 = |
| 2389 | (ONENAND_START_ADDRESS_3 << 16) | (ONENAND_START_ADDRESS_1); |
| 2390 | data->data0 = (ONENAND_CLRINTR << 16) | (onenand_sysconfig1); |
| 2391 | data->data1 = (onenand_startaddr8 << 16) | (onenand_startaddr1); |
| 2392 | data->data2 = (onenand_startbuffer << 16) | (onenand_startaddr2); |
| 2393 | data->data3 = (CLEAN_DATA_16 << 16) | (ONENAND_CMDLOADSPARE); |
| 2394 | data->data4 = (CLEAN_DATA_16 << 16) | (CLEAN_DATA_16); |
| 2395 | data->data5 = (ONENAND_CLRINTR << 16) | (ONENAND_SYSCFG1_ECCENA); |
| 2396 | data->data6 = (ONENAND_STARTADDR3_RES << 16) | (ONENAND_STARTADDR1_RES); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2397 | data->macro[0] = 0x0200; |
| 2398 | data->macro[1] = 0x0300; |
| 2399 | data->macro[2] = 0x0400; |
| 2400 | data->macro[3] = 0x0500; |
| 2401 | data->macro[4] = 0x8010; |
| 2402 | |
| 2403 | /*************************************************************/ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2404 | /* Write necessary address registers in the onenand device */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2405 | /*************************************************************/ |
| 2406 | |
| 2407 | /* Enable and configure the SFlash controller */ |
| 2408 | cmd->cmd = 0 | CMD_OCB; |
| 2409 | cmd->src = paddr(&data->sfbcfg); |
| 2410 | cmd->dst = NAND_SFLASHC_BURST_CFG; |
| 2411 | cmd->len = 4; |
| 2412 | cmd++; |
| 2413 | |
| 2414 | /* Block on cmd ready and write CMD register */ |
| 2415 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2416 | cmd->src = paddr(&data->sfcmd[0]); |
| 2417 | cmd->dst = NAND_SFLASHC_CMD; |
| 2418 | cmd->len = 4; |
| 2419 | cmd++; |
| 2420 | |
| 2421 | /* Write the ADDR0 and ADDR1 registers */ |
| 2422 | cmd->cmd = 0; |
| 2423 | cmd->src = paddr(&data->addr0); |
| 2424 | cmd->dst = NAND_ADDR0; |
| 2425 | cmd->len = 8; |
| 2426 | cmd++; |
| 2427 | |
| 2428 | /* Write the ADDR2 ADDR3 ADDR4 ADDR5 registers */ |
| 2429 | cmd->cmd = 0; |
| 2430 | cmd->src = paddr(&data->addr2); |
| 2431 | cmd->dst = NAND_ADDR2; |
| 2432 | cmd->len = 16; |
| 2433 | cmd++; |
| 2434 | |
| 2435 | /* Write the ADDR6 registers */ |
| 2436 | cmd->cmd = 0; |
| 2437 | cmd->src = paddr(&data->addr6); |
| 2438 | cmd->dst = NAND_ADDR6; |
| 2439 | cmd->len = 4; |
| 2440 | cmd++; |
| 2441 | |
| 2442 | /* Write the GENP0, GENP1, GENP2, GENP3 registers */ |
| 2443 | cmd->cmd = 0; |
| 2444 | cmd->src = paddr(&data->data0); |
| 2445 | cmd->dst = NAND_GENP_REG0; |
| 2446 | cmd->len = 16; |
| 2447 | cmd++; |
| 2448 | |
| 2449 | /* Write the FLASH_DEV_CMD4,5,6 registers */ |
| 2450 | cmd->cmd = 0; |
| 2451 | cmd->src = paddr(&data->data4); |
| 2452 | cmd->dst = NAND_DEV_CMD4; |
| 2453 | cmd->len = 12; |
| 2454 | cmd++; |
| 2455 | |
| 2456 | /* Kick the execute command */ |
| 2457 | cmd->cmd = 0; |
| 2458 | cmd->src = paddr(&data->sfexec); |
| 2459 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2460 | cmd->len = 4; |
| 2461 | cmd++; |
| 2462 | |
| 2463 | /* Block on data ready, and read the status register */ |
| 2464 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2465 | cmd->src = NAND_SFLASHC_STATUS; |
| 2466 | cmd->dst = paddr(&data->sfstat[0]); |
| 2467 | cmd->len = 4; |
| 2468 | cmd++; |
| 2469 | |
| 2470 | /*************************************************************/ |
| 2471 | /* Wait for the interrupt from the Onenand device controller */ |
| 2472 | /*************************************************************/ |
| 2473 | |
| 2474 | /* Block on cmd ready and write CMD register */ |
| 2475 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2476 | cmd->src = paddr(&data->sfcmd[1]); |
| 2477 | cmd->dst = NAND_SFLASHC_CMD; |
| 2478 | cmd->len = 4; |
| 2479 | cmd++; |
| 2480 | |
| 2481 | /* Kick the execute command */ |
| 2482 | cmd->cmd = 0; |
| 2483 | cmd->src = paddr(&data->sfexec); |
| 2484 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2485 | cmd->len = 4; |
| 2486 | cmd++; |
| 2487 | |
| 2488 | /* Block on data ready, and read the status register */ |
| 2489 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2490 | cmd->src = NAND_SFLASHC_STATUS; |
| 2491 | cmd->dst = paddr(&data->sfstat[1]); |
| 2492 | cmd->len = 4; |
| 2493 | cmd++; |
| 2494 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2495 | /*************************************************************/ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2496 | /* Read necessary status registers from the onenand device */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2497 | /*************************************************************/ |
| 2498 | |
| 2499 | /* Block on cmd ready and write CMD register */ |
| 2500 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2501 | cmd->src = paddr(&data->sfcmd[2]); |
| 2502 | cmd->dst = NAND_SFLASHC_CMD; |
| 2503 | cmd->len = 4; |
| 2504 | cmd++; |
| 2505 | |
| 2506 | /* Kick the execute command */ |
| 2507 | cmd->cmd = 0; |
| 2508 | cmd->src = paddr(&data->sfexec); |
| 2509 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2510 | cmd->len = 4; |
| 2511 | cmd++; |
| 2512 | |
| 2513 | /* Block on data ready, and read the status register */ |
| 2514 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2515 | cmd->src = NAND_SFLASHC_STATUS; |
| 2516 | cmd->dst = paddr(&data->sfstat[2]); |
| 2517 | cmd->len = 4; |
| 2518 | cmd++; |
| 2519 | |
| 2520 | /* Read the GENP3 register */ |
| 2521 | cmd->cmd = 0; |
| 2522 | cmd->src = NAND_GENP_REG3; |
| 2523 | cmd->dst = paddr(&data->data3); |
| 2524 | cmd->len = 4; |
| 2525 | cmd++; |
| 2526 | |
| 2527 | /* Read the DEVCMD4 register */ |
| 2528 | cmd->cmd = 0; |
| 2529 | cmd->src = NAND_DEV_CMD4; |
| 2530 | cmd->dst = paddr(&data->data4); |
| 2531 | cmd->len = 4; |
| 2532 | cmd++; |
| 2533 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2534 | /*************************************************************/ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2535 | /* Read the data ram area from the onenand buffer ram */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2536 | /*************************************************************/ |
| 2537 | |
| 2538 | if (addr) { |
| 2539 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2540 | data->data3 = (CLEAN_DATA_16 << 16) | (ONENAND_CMDLOAD); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2541 | |
| 2542 | for (i = 0; i < 4; i++) { |
| 2543 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2544 | /* Block on cmd ready and write CMD register */ |
| 2545 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2546 | cmd->src = paddr(&data->sfcmd[3 + i]); |
| 2547 | cmd->dst = NAND_SFLASHC_CMD; |
| 2548 | cmd->len = 4; |
| 2549 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2550 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2551 | /* Write the MACRO1 register */ |
| 2552 | cmd->cmd = 0; |
| 2553 | cmd->src = paddr(&data->macro[i]); |
| 2554 | cmd->dst = NAND_MACRO1_REG; |
| 2555 | cmd->len = 4; |
| 2556 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2557 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2558 | /* Kick the execute command */ |
| 2559 | cmd->cmd = 0; |
| 2560 | cmd->src = paddr(&data->sfexec); |
| 2561 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2562 | cmd->len = 4; |
| 2563 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2564 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2565 | /* Block on data rdy, & read status register */ |
| 2566 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2567 | cmd->src = NAND_SFLASHC_STATUS; |
| 2568 | cmd->dst = paddr(&data->sfstat[3 + i]); |
| 2569 | cmd->len = 4; |
| 2570 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2571 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2572 | /* Transfer nand ctlr buf contents to usr buf */ |
| 2573 | cmd->cmd = 0; |
| 2574 | cmd->src = NAND_FLASH_BUFFER; |
| 2575 | cmd->dst = curr_addr; |
| 2576 | cmd->len = 512; |
| 2577 | curr_addr += 512; |
| 2578 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2579 | } |
| 2580 | } |
| 2581 | |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2582 | /* Read oob bytes in Raw Mode */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2583 | if (raw_mode == 1) { |
| 2584 | /* Block on cmd ready and write CMD register */ |
| 2585 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2586 | cmd->src = paddr(&data->sfcmd[7]); |
| 2587 | cmd->dst = NAND_SFLASHC_CMD; |
| 2588 | cmd->len = 4; |
| 2589 | cmd++; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2590 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2591 | /* Write the MACRO1 register */ |
| 2592 | cmd->cmd = 0; |
| 2593 | cmd->src = paddr(&data->macro[4]); |
| 2594 | cmd->dst = NAND_MACRO1_REG; |
| 2595 | cmd->len = 4; |
| 2596 | cmd++; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2597 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2598 | /* Kick the execute command */ |
| 2599 | cmd->cmd = 0; |
| 2600 | cmd->src = paddr(&data->sfexec); |
| 2601 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2602 | cmd->len = 4; |
| 2603 | cmd++; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2604 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2605 | /* Block on data rdy, & read status register */ |
| 2606 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2607 | cmd->src = NAND_SFLASHC_STATUS; |
| 2608 | cmd->dst = paddr(&data->sfstat[7]); |
| 2609 | cmd->len = 4; |
| 2610 | cmd++; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2611 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2612 | /* Transfer nand ctlr buf contents to usr buf */ |
| 2613 | cmd->cmd = 0; |
| 2614 | cmd->src = NAND_FLASH_BUFFER; |
| 2615 | cmd->dst = curr_addr; |
| 2616 | cmd->len = 64; |
| 2617 | curr_addr += 64; |
| 2618 | cmd++; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 2619 | } |
| 2620 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2621 | /*************************************************************/ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2622 | /* Restore the necessary registers to proper values */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2623 | /*************************************************************/ |
| 2624 | |
| 2625 | /* Block on cmd ready and write CMD register */ |
| 2626 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2627 | cmd->src = paddr(&data->sfcmd[8]); |
| 2628 | cmd->dst = NAND_SFLASHC_CMD; |
| 2629 | cmd->len = 4; |
| 2630 | cmd++; |
| 2631 | |
| 2632 | /* Kick the execute command */ |
| 2633 | cmd->cmd = 0; |
| 2634 | cmd->src = paddr(&data->sfexec); |
| 2635 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2636 | cmd->len = 4; |
| 2637 | cmd++; |
| 2638 | |
| 2639 | /* Block on data ready, and read the status register */ |
| 2640 | cmd->cmd = SRC_CRCI_NAND_DATA | CMD_OCU | CMD_LC; |
| 2641 | cmd->src = NAND_SFLASHC_STATUS; |
| 2642 | cmd->dst = paddr(&data->sfstat[8]); |
| 2643 | cmd->len = 4; |
| 2644 | cmd++; |
| 2645 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2646 | ptr[0] = (paddr(cmdlist) >> 3) | CMD_PTR_LP; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2647 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2648 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2649 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2650 | ecc_status = (data->data3 >> 16) & 0x0000FFFF; |
| 2651 | interrupt_status = (data->data4 >> 0) & 0x0000FFFF; |
| 2652 | controller_status = (data->data4 >> 16) & 0x0000FFFF; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2653 | |
| 2654 | #if VERBOSE |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2655 | dprintf(INFO, "\n%s: sflash status %x %x %x %x %x %x %x" |
| 2656 | "%x %x\n", __func__, |
| 2657 | data->sfstat[0], |
| 2658 | data->sfstat[1], |
| 2659 | data->sfstat[2], |
| 2660 | data->sfstat[3], |
| 2661 | data->sfstat[4], |
| 2662 | data->sfstat[5], data->sfstat[6], data->sfstat[7]); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2663 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2664 | dprintf(INFO, "%s: controller_status = %x\n", __func__, |
| 2665 | controller_status); |
| 2666 | dprintf(INFO, "%s: interrupt_status = %x\n", __func__, |
| 2667 | interrupt_status); |
| 2668 | dprintf(INFO, "%s: ecc_status = %x\n", __func__, ecc_status); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2669 | #endif |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2670 | /* Check for errors, protection violations etc */ |
| 2671 | if ((controller_status != 0) |
| 2672 | || (data->sfstat[0] & 0x110) |
| 2673 | || (data->sfstat[1] & 0x110) |
| 2674 | || (data->sfstat[2] & 0x110) |
| 2675 | || ((data->sfstat[3] & 0x110) && (addr)) |
| 2676 | || ((data->sfstat[4] & 0x110) && (addr)) |
| 2677 | || ((data->sfstat[5] & 0x110) && |
| 2678 | (addr)) || ((data->sfstat[6] & 0x110) && (addr))) { |
| 2679 | dprintf(INFO, "%s: ECC/MPU/OP error\n", __func__); |
| 2680 | return -1; |
| 2681 | } |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2682 | #if VERBOSE |
| 2683 | dprintf(INFO, "read page %d: status: %x %x %x %x\n", |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2684 | page, data[5], data[6], data[7], data[8]); |
| 2685 | for (n = 0; n < 4; n++) { |
| 2686 | ptr = (unsigned *)(addr + 512 * n); |
| 2687 | dprintf(INFO, "data%d: %x %x %x %x\n", n, ptr[0], ptr[1], |
| 2688 | ptr[2], ptr[3]); |
| 2689 | ptr = (unsigned *)(spareaddr + 16 * n); |
| 2690 | dprintf(INFO, "spare data%d %x %x %x %x\n", n, ptr[0], |
| 2691 | ptr[1], ptr[2], ptr[3]); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2692 | } |
| 2693 | #endif |
| 2694 | |
| 2695 | return 0; |
| 2696 | } |
| 2697 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2698 | struct data_onenand_write { |
| 2699 | unsigned sfbcfg; |
| 2700 | unsigned sfcmd[9]; |
| 2701 | unsigned sfexec; |
| 2702 | unsigned sfstat[9]; |
| 2703 | unsigned addr0; |
| 2704 | unsigned addr1; |
| 2705 | unsigned addr2; |
| 2706 | unsigned addr3; |
| 2707 | unsigned addr4; |
| 2708 | unsigned addr5; |
| 2709 | unsigned addr6; |
| 2710 | unsigned data0; |
| 2711 | unsigned data1; |
| 2712 | unsigned data2; |
| 2713 | unsigned data3; |
| 2714 | unsigned data4; |
| 2715 | unsigned data5; |
| 2716 | unsigned data6; |
| 2717 | unsigned macro[5]; |
| 2718 | }; |
| 2719 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2720 | static int |
| 2721 | _flash_onenand_write_page(dmov_s * cmdlist, unsigned *ptrlist, |
| 2722 | unsigned page, const void *_addr, |
| 2723 | const void *_spareaddr, unsigned raw_mode) |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2724 | { |
| 2725 | dmov_s *cmd = cmdlist; |
| 2726 | unsigned *ptr = ptrlist; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2727 | struct data_onenand_write *data = (void *)(ptrlist + 4); |
| 2728 | unsigned addr = (unsigned)_addr; |
| 2729 | unsigned addr_curr = (unsigned)_addr; |
| 2730 | char *spareaddr = (char *)_spareaddr; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2731 | unsigned i, j, k; |
| 2732 | |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 2733 | unsigned erasesize = (flash_pagesize * num_pages_per_blk); |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2734 | unsigned writesize = flash_pagesize; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2735 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2736 | unsigned onenand_startaddr1 = (page * flash_pagesize) / erasesize; |
| 2737 | unsigned onenand_startaddr8 = (((unsigned)(page * flash_pagesize) & |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2738 | (erasesize - 1)) / writesize) << 2; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2739 | unsigned onenand_startaddr2 = DEVICE_BUFFERRAM_0 << 15; |
| 2740 | unsigned onenand_startbuffer = DATARAM0_0 << 8; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2741 | unsigned onenand_sysconfig1 = (raw_mode == 1) ? ONENAND_SYSCFG1_ECCDIS : |
| 2742 | ONENAND_SYSCFG1_ECCENA; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2743 | |
| 2744 | unsigned controller_status; |
| 2745 | unsigned interrupt_status; |
| 2746 | unsigned ecc_status; |
| 2747 | |
| 2748 | char flash_oob[64]; |
| 2749 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2750 | unsigned oobfree_offset[8] = { 2, 14, 18, 30, 34, 46, 50, 62 }; |
| 2751 | unsigned oobfree_length[8] = { 3, 2, 3, 2, 3, 2, 3, 2 }; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2752 | |
| 2753 | for (i = 0; i < 64; i++) |
| 2754 | flash_oob[i] = 0xFF; |
| 2755 | |
| 2756 | data->sfbcfg = SFLASH_BCFG; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2757 | data->sfcmd[0] = SFLASH_PREPCMD(256, 0, 0, |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2758 | NAND_SFCMD_CMDXS, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2759 | NAND_SFCMD_ASYNC, NAND_SFCMD_DATWR); |
| 2760 | data->sfcmd[1] = SFLASH_PREPCMD(256, 0, 0, |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2761 | NAND_SFCMD_CMDXS, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2762 | NAND_SFCMD_ASYNC, NAND_SFCMD_DATWR); |
| 2763 | data->sfcmd[2] = SFLASH_PREPCMD(256, 0, 0, |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2764 | NAND_SFCMD_CMDXS, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2765 | NAND_SFCMD_ASYNC, NAND_SFCMD_DATWR); |
| 2766 | data->sfcmd[3] = SFLASH_PREPCMD(256, 0, 0, |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2767 | NAND_SFCMD_CMDXS, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2768 | NAND_SFCMD_ASYNC, NAND_SFCMD_DATWR); |
| 2769 | data->sfcmd[4] = SFLASH_PREPCMD(32, 0, 0, |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2770 | NAND_SFCMD_CMDXS, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2771 | NAND_SFCMD_ASYNC, NAND_SFCMD_DATWR); |
| 2772 | data->sfcmd[5] = SFLASH_PREPCMD(7, 0, 0, |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2773 | NAND_SFCMD_CMDXS, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2774 | NAND_SFCMD_ASYNC, NAND_SFCMD_REGWR); |
| 2775 | data->sfcmd[6] = SFLASH_PREPCMD(0, 0, 32, |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2776 | NAND_SFCMD_CMDXS, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2777 | NAND_SFCMD_ASYNC, NAND_SFCMD_INTHI); |
| 2778 | data->sfcmd[7] = SFLASH_PREPCMD(3, 7, 0, |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2779 | NAND_SFCMD_DATXS, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2780 | NAND_SFCMD_ASYNC, NAND_SFCMD_REGRD); |
| 2781 | data->sfcmd[8] = SFLASH_PREPCMD(4, 10, 0, |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2782 | NAND_SFCMD_CMDXS, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2783 | NAND_SFCMD_ASYNC, NAND_SFCMD_REGWR); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2784 | data->sfexec = 1; |
| 2785 | |
| 2786 | data->sfstat[0] = CLEAN_DATA_32; |
| 2787 | data->sfstat[1] = CLEAN_DATA_32; |
| 2788 | data->sfstat[2] = CLEAN_DATA_32; |
| 2789 | data->sfstat[3] = CLEAN_DATA_32; |
| 2790 | data->sfstat[4] = CLEAN_DATA_32; |
| 2791 | data->sfstat[5] = CLEAN_DATA_32; |
| 2792 | data->sfstat[6] = CLEAN_DATA_32; |
| 2793 | data->sfstat[7] = CLEAN_DATA_32; |
| 2794 | data->sfstat[8] = CLEAN_DATA_32; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2795 | data->addr0 = |
| 2796 | (ONENAND_INTERRUPT_STATUS << 16) | (ONENAND_SYSTEM_CONFIG_1); |
| 2797 | data->addr1 = |
| 2798 | (ONENAND_START_ADDRESS_8 << 16) | (ONENAND_START_ADDRESS_1); |
| 2799 | data->addr2 = (ONENAND_START_BUFFER << 16) | (ONENAND_START_ADDRESS_2); |
| 2800 | data->addr3 = (ONENAND_ECC_STATUS << 16) | (ONENAND_COMMAND); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2801 | data->addr4 = (ONENAND_CONTROLLER_STATUS << 16) | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2802 | (ONENAND_INTERRUPT_STATUS); |
| 2803 | data->addr5 = |
| 2804 | (ONENAND_INTERRUPT_STATUS << 16) | (ONENAND_SYSTEM_CONFIG_1); |
| 2805 | data->addr6 = |
| 2806 | (ONENAND_START_ADDRESS_3 << 16) | (ONENAND_START_ADDRESS_1); |
| 2807 | data->data0 = (ONENAND_CLRINTR << 16) | (onenand_sysconfig1); |
| 2808 | data->data1 = (onenand_startaddr8 << 16) | (onenand_startaddr1); |
| 2809 | data->data2 = (onenand_startbuffer << 16) | (onenand_startaddr2); |
| 2810 | data->data3 = (CLEAN_DATA_16 << 16) | (ONENAND_CMDPROGSPARE); |
| 2811 | data->data3 = (CLEAN_DATA_16 << 16) | (ONENAND_CMDPROGSPARE); |
| 2812 | data->data4 = (CLEAN_DATA_16 << 16) | (CLEAN_DATA_16); |
| 2813 | data->data5 = (ONENAND_CLRINTR << 16) | (ONENAND_SYSCFG1_ECCENA); |
| 2814 | data->data6 = (ONENAND_STARTADDR3_RES << 16) | (ONENAND_STARTADDR1_RES); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2815 | data->macro[0] = 0x0200; |
| 2816 | data->macro[1] = 0x0300; |
| 2817 | data->macro[2] = 0x0400; |
| 2818 | data->macro[3] = 0x0500; |
| 2819 | data->macro[4] = 0x8010; |
| 2820 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2821 | /*************************************************************/ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2822 | /* Write the data ram area in the onenand buffer ram */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2823 | /*************************************************************/ |
| 2824 | |
| 2825 | /* Enable and configure the SFlash controller */ |
| 2826 | cmd->cmd = 0 | CMD_OCB; |
| 2827 | cmd->src = paddr(&data->sfbcfg); |
| 2828 | cmd->dst = NAND_SFLASHC_BURST_CFG; |
| 2829 | cmd->len = 4; |
| 2830 | cmd++; |
| 2831 | |
| 2832 | if (addr) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2833 | data->data3 = (CLEAN_DATA_16 << 16) | (ONENAND_CMDPROG); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2834 | |
| 2835 | for (i = 0; i < 4; i++) { |
| 2836 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2837 | /* Block on cmd ready and write CMD register */ |
| 2838 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2839 | cmd->src = paddr(&data->sfcmd[i]); |
| 2840 | cmd->dst = NAND_SFLASHC_CMD; |
| 2841 | cmd->len = 4; |
| 2842 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2843 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2844 | /* Trnsfr usr buf contents to nand ctlr buf */ |
| 2845 | cmd->cmd = 0; |
| 2846 | cmd->src = paddr(addr_curr); |
| 2847 | cmd->dst = NAND_FLASH_BUFFER; |
| 2848 | cmd->len = 512; |
| 2849 | if (!raw_mode) |
| 2850 | addr_curr += 512; |
| 2851 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2852 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2853 | /* Write the MACRO1 register */ |
| 2854 | cmd->cmd = 0; |
| 2855 | cmd->src = paddr(&data->macro[i]); |
| 2856 | cmd->dst = NAND_MACRO1_REG; |
| 2857 | cmd->len = 4; |
| 2858 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2859 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2860 | /* Kick the execute command */ |
| 2861 | cmd->cmd = 0; |
| 2862 | cmd->src = paddr(&data->sfexec); |
| 2863 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2864 | cmd->len = 4; |
| 2865 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2866 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2867 | /* Block on data rdy, & read status register */ |
| 2868 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2869 | cmd->src = NAND_SFLASHC_STATUS; |
| 2870 | cmd->dst = paddr(&data->sfstat[i]); |
| 2871 | cmd->len = 4; |
| 2872 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2873 | |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | /* Block on cmd ready and write CMD register */ |
| 2878 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2879 | cmd->src = paddr(&data->sfcmd[4]); |
| 2880 | cmd->dst = NAND_SFLASHC_CMD; |
| 2881 | cmd->len = 4; |
| 2882 | cmd++; |
| 2883 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2884 | if (spareaddr) { |
| 2885 | // Auto mode |
| 2886 | for (i = 0, k = 0; i < 8; i++) { |
| 2887 | for (j = 0; j < oobfree_length[i]; j++) { |
| 2888 | flash_oob[j + oobfree_offset[i]] = spareaddr[k]; |
| 2889 | k++; |
| 2890 | } |
| 2891 | } |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2892 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2893 | cmd->cmd = 0; |
| 2894 | cmd->src = paddr(&flash_oob); |
| 2895 | cmd->dst = NAND_FLASH_BUFFER; |
| 2896 | cmd->len = 64; |
| 2897 | cmd++; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2898 | } |
| 2899 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2900 | if (raw_mode) { |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 2901 | cmd->cmd = 0; |
| 2902 | cmd->src = paddr(addr_curr); |
| 2903 | cmd->dst = NAND_FLASH_BUFFER; |
| 2904 | cmd->len = 64; |
| 2905 | cmd++; |
| 2906 | } |
| 2907 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2908 | /* Write the MACRO1 register */ |
| 2909 | cmd->cmd = 0; |
| 2910 | cmd->src = paddr(&data->macro[4]); |
| 2911 | cmd->dst = NAND_MACRO1_REG; |
| 2912 | cmd->len = 4; |
| 2913 | cmd++; |
| 2914 | |
| 2915 | /* Kick the execute command */ |
| 2916 | cmd->cmd = 0; |
| 2917 | cmd->src = paddr(&data->sfexec); |
| 2918 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2919 | cmd->len = 4; |
| 2920 | cmd++; |
| 2921 | |
| 2922 | /* Block on data ready, and read the status register */ |
| 2923 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2924 | cmd->src = NAND_SFLASHC_STATUS; |
| 2925 | cmd->dst = paddr(&data->sfstat[4]); |
| 2926 | cmd->len = 4; |
| 2927 | cmd++; |
| 2928 | |
| 2929 | /*************************************************************/ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 2930 | /* Write necessary address registers in the onenand device */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 2931 | /*************************************************************/ |
| 2932 | |
| 2933 | /* Block on cmd ready and write CMD register */ |
| 2934 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2935 | cmd->src = paddr(&data->sfcmd[5]); |
| 2936 | cmd->dst = NAND_SFLASHC_CMD; |
| 2937 | cmd->len = 4; |
| 2938 | cmd++; |
| 2939 | |
| 2940 | /* Write the ADDR0 and ADDR1 registers */ |
| 2941 | cmd->cmd = 0; |
| 2942 | cmd->src = paddr(&data->addr0); |
| 2943 | cmd->dst = NAND_ADDR0; |
| 2944 | cmd->len = 8; |
| 2945 | cmd++; |
| 2946 | |
| 2947 | /* Write the ADDR2 ADDR3 ADDR4 ADDR5 registers */ |
| 2948 | cmd->cmd = 0; |
| 2949 | cmd->src = paddr(&data->addr2); |
| 2950 | cmd->dst = NAND_ADDR2; |
| 2951 | cmd->len = 16; |
| 2952 | cmd++; |
| 2953 | |
| 2954 | /* Write the ADDR6 registers */ |
| 2955 | cmd->cmd = 0; |
| 2956 | cmd->src = paddr(&data->addr6); |
| 2957 | cmd->dst = NAND_ADDR6; |
| 2958 | cmd->len = 4; |
| 2959 | cmd++; |
| 2960 | |
| 2961 | /* Write the GENP0, GENP1, GENP2, GENP3 registers */ |
| 2962 | cmd->cmd = 0; |
| 2963 | cmd->src = paddr(&data->data0); |
| 2964 | cmd->dst = NAND_GENP_REG0; |
| 2965 | cmd->len = 16; |
| 2966 | cmd++; |
| 2967 | |
| 2968 | /* Write the FLASH_DEV_CMD4,5,6 registers */ |
| 2969 | cmd->cmd = 0; |
| 2970 | cmd->src = paddr(&data->data4); |
| 2971 | cmd->dst = NAND_DEV_CMD4; |
| 2972 | cmd->len = 12; |
| 2973 | cmd++; |
| 2974 | |
| 2975 | /* Kick the execute command */ |
| 2976 | cmd->cmd = 0; |
| 2977 | cmd->src = paddr(&data->sfexec); |
| 2978 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 2979 | cmd->len = 4; |
| 2980 | cmd++; |
| 2981 | |
| 2982 | /* Block on data ready, and read the status register */ |
| 2983 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 2984 | cmd->src = NAND_SFLASHC_STATUS; |
| 2985 | cmd->dst = paddr(&data->sfstat[5]); |
| 2986 | cmd->len = 4; |
| 2987 | cmd++; |
| 2988 | |
| 2989 | /*************************************************************/ |
| 2990 | /* Wait for the interrupt from the Onenand device controller */ |
| 2991 | /*************************************************************/ |
| 2992 | |
| 2993 | /* Block on cmd ready and write CMD register */ |
| 2994 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 2995 | cmd->src = paddr(&data->sfcmd[6]); |
| 2996 | cmd->dst = NAND_SFLASHC_CMD; |
| 2997 | cmd->len = 4; |
| 2998 | cmd++; |
| 2999 | |
| 3000 | /* Kick the execute command */ |
| 3001 | cmd->cmd = 0; |
| 3002 | cmd->src = paddr(&data->sfexec); |
| 3003 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 3004 | cmd->len = 4; |
| 3005 | cmd++; |
| 3006 | |
| 3007 | /* Block on data ready, and read the status register */ |
| 3008 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 3009 | cmd->src = NAND_SFLASHC_STATUS; |
| 3010 | cmd->dst = paddr(&data->sfstat[6]); |
| 3011 | cmd->len = 4; |
| 3012 | cmd++; |
| 3013 | |
| 3014 | /*************************************************************/ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3015 | /* Read necessary status registers from the onenand device */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3016 | /*************************************************************/ |
| 3017 | |
| 3018 | /* Block on cmd ready and write CMD register */ |
| 3019 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 3020 | cmd->src = paddr(&data->sfcmd[7]); |
| 3021 | cmd->dst = NAND_SFLASHC_CMD; |
| 3022 | cmd->len = 4; |
| 3023 | cmd++; |
| 3024 | |
| 3025 | /* Kick the execute command */ |
| 3026 | cmd->cmd = 0; |
| 3027 | cmd->src = paddr(&data->sfexec); |
| 3028 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 3029 | cmd->len = 4; |
| 3030 | cmd++; |
| 3031 | |
| 3032 | /* Block on data ready, and read the status register */ |
| 3033 | cmd->cmd = SRC_CRCI_NAND_DATA; |
| 3034 | cmd->src = NAND_SFLASHC_STATUS; |
| 3035 | cmd->dst = paddr(&data->sfstat[7]); |
| 3036 | cmd->len = 4; |
| 3037 | cmd++; |
| 3038 | |
| 3039 | /* Read the GENP3 register */ |
| 3040 | cmd->cmd = 0; |
| 3041 | cmd->src = NAND_GENP_REG3; |
| 3042 | cmd->dst = paddr(&data->data3); |
| 3043 | cmd->len = 4; |
| 3044 | cmd++; |
| 3045 | |
| 3046 | /* Read the DEVCMD4 register */ |
| 3047 | cmd->cmd = 0; |
| 3048 | cmd->src = NAND_DEV_CMD4; |
| 3049 | cmd->dst = paddr(&data->data4); |
| 3050 | cmd->len = 4; |
| 3051 | cmd++; |
| 3052 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3053 | /*************************************************************/ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3054 | /* Restore the necessary registers to proper values */ |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3055 | /*************************************************************/ |
| 3056 | |
| 3057 | /* Block on cmd ready and write CMD register */ |
| 3058 | cmd->cmd = DST_CRCI_NAND_CMD; |
| 3059 | cmd->src = paddr(&data->sfcmd[8]); |
| 3060 | cmd->dst = NAND_SFLASHC_CMD; |
| 3061 | cmd->len = 4; |
| 3062 | cmd++; |
| 3063 | |
| 3064 | /* Kick the execute command */ |
| 3065 | cmd->cmd = 0; |
| 3066 | cmd->src = paddr(&data->sfexec); |
| 3067 | cmd->dst = NAND_SFLASHC_EXEC_CMD; |
| 3068 | cmd->len = 4; |
| 3069 | cmd++; |
| 3070 | |
| 3071 | /* Block on data ready, and read the status register */ |
| 3072 | cmd->cmd = SRC_CRCI_NAND_DATA | CMD_OCU | CMD_LC; |
| 3073 | cmd->src = NAND_SFLASHC_STATUS; |
| 3074 | cmd->dst = paddr(&data->sfstat[8]); |
| 3075 | cmd->len = 4; |
| 3076 | cmd++; |
| 3077 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3078 | ptr[0] = (paddr(cmdlist) >> 3) | CMD_PTR_LP; |
| 3079 | |
| 3080 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 3081 | |
| 3082 | ecc_status = (data->data3 >> 16) & 0x0000FFFF; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3083 | interrupt_status = (data->data4 >> 0) & 0x0000FFFF; |
| 3084 | controller_status = (data->data4 >> 16) & 0x0000FFFF; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3085 | |
| 3086 | #if VERBOSE |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3087 | dprintf(INFO, "\n%s: sflash status %x %x %x %x %x %x %x %x %x\n", |
| 3088 | __func__, data->sfstat[0], data->sfstat[1], data->sfstat[2], |
| 3089 | data->sfstat[3], data->sfstat[4], data->sfstat[5], |
| 3090 | data->sfstat[6], data->sfstat[7], data->sfstat[8]); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3091 | |
| 3092 | dprintf(INFO, "%s: controller_status = %x\n", __func__, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3093 | controller_status); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3094 | dprintf(INFO, "%s: interrupt_status = %x\n", __func__, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3095 | interrupt_status); |
| 3096 | dprintf(INFO, "%s: ecc_status = %x\n", __func__, ecc_status); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3097 | #endif |
| 3098 | /* Check for errors, protection violations etc */ |
| 3099 | if ((controller_status != 0) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3100 | || (data->sfstat[5] & 0x110) |
| 3101 | || (data->sfstat[6] & 0x110) |
| 3102 | || (data->sfstat[7] & 0x110) |
| 3103 | || (data->sfstat[8] & 0x110) |
| 3104 | || ((data->sfstat[0] & 0x110) && (addr)) |
| 3105 | || ((data->sfstat[1] & 0x110) && (addr)) |
| 3106 | || ((data->sfstat[2] & 0x110) && |
| 3107 | (addr)) || ((data->sfstat[3] & 0x110) && (addr))) { |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3108 | dprintf(CRITICAL, "%s: ECC/MPU/OP error\n", __func__); |
| 3109 | return -1; |
| 3110 | } |
| 3111 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3112 | return 0; |
| 3113 | } |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 3114 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3115 | static int |
| 3116 | flash_onenand_mark_badblock(dmov_s * cmdlist, unsigned *ptrlist, unsigned page) |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 3117 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3118 | memset(empty_buf, 0, 528); |
| 3119 | /* Going to first page of the block */ |
| 3120 | if (page & num_pages_per_blk_mask) |
| 3121 | page = page - (page & num_pages_per_blk_mask); |
| 3122 | return _flash_onenand_write_page(cmdlist, ptrlist, page, empty_buf, 0, |
| 3123 | 1); |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 3124 | } |
| 3125 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3126 | static int |
| 3127 | flash_mark_badblock(dmov_s * cmdlist, unsigned *ptrlist, unsigned page) |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 3128 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3129 | switch (flash_info.type) { |
| 3130 | case FLASH_8BIT_NAND_DEVICE: |
| 3131 | case FLASH_16BIT_NAND_DEVICE: |
| 3132 | return flash_nand_mark_badblock(cmdlist, ptrlist, page); |
| 3133 | case FLASH_ONENAND_DEVICE: |
| 3134 | return flash_onenand_mark_badblock(cmdlist, ptrlist, page); |
| 3135 | default: |
| 3136 | return -1; |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 3137 | } |
| 3138 | } |
| 3139 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3140 | unsigned flash_ctrl_hwinfo(dmov_s * cmdlist, unsigned *ptrlist) |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 3141 | { |
| 3142 | dmov_s *cmd = cmdlist; |
| 3143 | unsigned *ptr = ptrlist; |
| 3144 | unsigned *data = ptrlist + 4; |
| 3145 | |
| 3146 | unsigned rv; |
| 3147 | |
| 3148 | data[0] = 0xeeeeeeee; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 3149 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3150 | cmd[0].cmd = CMD_LC | CMD_OCB | CMD_OCU; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 3151 | cmd[0].src = NAND_HW_INFO; |
| 3152 | cmd[0].dst = paddr(&data[0]); |
| 3153 | cmd[0].len = 4; |
| 3154 | |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 3155 | ptr[0] = (paddr(cmd) >> 3) | CMD_PTR_LP; |
| 3156 | dmov_exec_cmdptr(DMOV_NAND_CHAN, ptr); |
| 3157 | rv = data[0]; |
| 3158 | |
| 3159 | return rv; |
| 3160 | } |
| 3161 | |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3162 | /* Wrapper functions */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3163 | static void flash_read_id(dmov_s * cmdlist, unsigned *ptrlist) |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3164 | { |
| 3165 | int dev_found = 0; |
| 3166 | unsigned index; |
Deepa Dinamani | b67326d | 2012-11-26 11:57:22 -0800 | [diff] [blame] | 3167 | uint32_t hwinfo; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3168 | |
| 3169 | // Try to read id |
| 3170 | flash_nand_read_id(cmdlist, ptrlist); |
| 3171 | // Check if we support the device |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3172 | for (index = 1; |
| 3173 | index < |
| 3174 | (sizeof(supported_flash) / sizeof(struct flash_identification)); |
| 3175 | index++) { |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3176 | if ((flash_info.id & supported_flash[index].mask) == |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3177 | (supported_flash[index]. |
| 3178 | flash_id & (supported_flash[index].mask))) { |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3179 | dev_found = 1; |
| 3180 | break; |
| 3181 | } |
| 3182 | } |
| 3183 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3184 | if (!dev_found) { |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3185 | flash_onenand_read_id(cmdlist, ptrlist); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3186 | for (index = 1; |
| 3187 | index < |
| 3188 | (sizeof(supported_flash) / |
| 3189 | sizeof(struct flash_identification)); index++) { |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3190 | if ((flash_info.id & supported_flash[index].mask) == |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3191 | (supported_flash[index]. |
| 3192 | flash_id & (supported_flash[index].mask))) { |
| 3193 | dev_found = 1; |
| 3194 | break; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3195 | } |
| 3196 | } |
| 3197 | } |
| 3198 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3199 | if (dev_found) { |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3200 | if (supported_flash[index].widebus) |
| 3201 | flash_info.type = FLASH_16BIT_NAND_DEVICE; |
| 3202 | else |
| 3203 | flash_info.type = FLASH_8BIT_NAND_DEVICE; |
| 3204 | if (supported_flash[index].onenand) |
| 3205 | flash_info.type = FLASH_ONENAND_DEVICE; |
| 3206 | flash_info.page_size = supported_flash[index].pagesize; |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 3207 | flash_pagesize = flash_info.page_size; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3208 | flash_info.block_size = supported_flash[index].blksize; |
| 3209 | flash_info.spare_size = supported_flash[index].oobsize; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3210 | if (flash_info.block_size && flash_info.page_size) { |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3211 | flash_info.num_blocks = supported_flash[index].density; |
Chandan Uddaraju | 4060511 | 2010-08-09 14:25:08 -0700 | [diff] [blame] | 3212 | flash_info.num_blocks /= (flash_info.block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3213 | } else { |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3214 | flash_info.num_blocks = 0; |
| 3215 | } |
| 3216 | ASSERT(flash_info.num_blocks); |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 3217 | // Use this for getting the next/current blocks |
| 3218 | num_pages_per_blk = flash_info.block_size / flash_pagesize; |
| 3219 | num_pages_per_blk_mask = num_pages_per_blk - 1; |
Deepa Dinamani | b67326d | 2012-11-26 11:57:22 -0800 | [diff] [blame] | 3220 | |
| 3221 | hwinfo = flash_ctrl_hwinfo(cmdlist, ptrlist); |
| 3222 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3223 | //Look for 8bit BCH ECC Nand, TODO: ECC Correctability >= 8 |
Deepa Dinamani | b67326d | 2012-11-26 11:57:22 -0800 | [diff] [blame] | 3224 | if (((hwinfo == 0x307) || (hwinfo == 0x4030)) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3225 | && flash_info.id == 0x2600482c) { |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 3226 | enable_bch_ecc = 1; |
| 3227 | } |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3228 | return; |
| 3229 | } |
Channagoud Kadabi | 7cecb84 | 2011-12-22 18:18:21 +0530 | [diff] [blame] | 3230 | // Flash device is not supported, print flash device info and halt |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3231 | if (dev_found == 0) { |
Channagoud Kadabi | 7cecb84 | 2011-12-22 18:18:21 +0530 | [diff] [blame] | 3232 | dprintf(CRITICAL, "NAND device is not supported: nandid: 0x%x \ |
| 3233 | maker=0x%02x device=0x%02x\n", flash_info.id, |
| 3234 | flash_info.vendor, flash_info.device); |
| 3235 | ASSERT(0); |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3236 | } |
| 3237 | dprintf(INFO, "nandid: 0x%x maker=0x%02x device=0x%02x page_size=%d\n", |
| 3238 | flash_info.id, flash_info.vendor, flash_info.device, |
| 3239 | flash_info.page_size); |
| 3240 | dprintf(INFO, " spare_size=%d block_size=%d num_blocks=%d\n", |
| 3241 | flash_info.spare_size, flash_info.block_size, |
| 3242 | flash_info.num_blocks); |
| 3243 | } |
| 3244 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3245 | static int flash_erase_block(dmov_s * cmdlist, unsigned *ptrlist, unsigned page) |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3246 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3247 | switch (flash_info.type) { |
| 3248 | case FLASH_8BIT_NAND_DEVICE: |
| 3249 | case FLASH_16BIT_NAND_DEVICE: |
| 3250 | return flash_nand_erase_block(cmdlist, ptrlist, page); |
| 3251 | case FLASH_ONENAND_DEVICE: |
| 3252 | return flash_onenand_erase_block(cmdlist, ptrlist, page); |
| 3253 | default: |
| 3254 | return -1; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3255 | } |
| 3256 | } |
| 3257 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3258 | static int |
| 3259 | _flash_read_page(dmov_s * cmdlist, unsigned *ptrlist, |
| 3260 | unsigned page, void *_addr, void *_spareaddr) |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3261 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3262 | switch (flash_info.type) { |
| 3263 | case FLASH_8BIT_NAND_DEVICE: |
| 3264 | case FLASH_16BIT_NAND_DEVICE: |
| 3265 | if (interleaved_mode) |
| 3266 | return flash_nand_read_page_interleave(cmdlist, ptrlist, |
| 3267 | page, _addr, |
| 3268 | _spareaddr); |
| 3269 | else |
| 3270 | return _flash_nand_read_page(cmdlist, ptrlist, page, |
| 3271 | _addr, _spareaddr); |
| 3272 | case FLASH_ONENAND_DEVICE: |
| 3273 | return _flash_onenand_read_page(cmdlist, ptrlist, page, _addr, |
| 3274 | _spareaddr, 0); |
| 3275 | default: |
| 3276 | return -1; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3277 | } |
| 3278 | } |
| 3279 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3280 | static int |
| 3281 | _flash_block_isbad(dmov_s * cmdlist, unsigned *ptrlist, unsigned page) |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 3282 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3283 | switch (flash_info.type) { |
| 3284 | case FLASH_8BIT_NAND_DEVICE: |
| 3285 | case FLASH_16BIT_NAND_DEVICE: |
| 3286 | return flash_nand_block_isbad(cmdlist, ptrlist, page); |
| 3287 | case FLASH_ONENAND_DEVICE: |
| 3288 | return flash_onenand_block_isbad(cmdlist, ptrlist, page); |
| 3289 | default: |
| 3290 | return -1; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 3291 | } |
| 3292 | } |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3293 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3294 | static int |
| 3295 | _flash_write_page(dmov_s * cmdlist, unsigned *ptrlist, |
| 3296 | unsigned page, const void *_addr, const void *_spareaddr) |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3297 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3298 | switch (flash_info.type) { |
| 3299 | case FLASH_8BIT_NAND_DEVICE: |
| 3300 | case FLASH_16BIT_NAND_DEVICE: |
| 3301 | if (interleaved_mode) |
| 3302 | return flash_nand_write_page_interleave(cmdlist, |
| 3303 | ptrlist, page, |
| 3304 | _addr, |
| 3305 | _spareaddr, 0); |
| 3306 | else |
| 3307 | return _flash_nand_write_page(cmdlist, ptrlist, page, |
| 3308 | _addr, _spareaddr, 0); |
| 3309 | case FLASH_ONENAND_DEVICE: |
| 3310 | return _flash_onenand_write_page(cmdlist, ptrlist, page, _addr, |
| 3311 | _spareaddr, 0); |
| 3312 | default: |
| 3313 | return -1; |
Shashank Mittal | c20b5a1 | 2009-11-18 19:35:30 -0800 | [diff] [blame] | 3314 | } |
| 3315 | } |
| 3316 | |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3317 | static unsigned *flash_ptrlist; |
| 3318 | static dmov_s *flash_cmdlist; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3319 | |
| 3320 | static struct ptable *flash_ptable = NULL; |
| 3321 | |
Dima Zavin | e5f6435 | 2009-03-02 16:04:20 -0800 | [diff] [blame] | 3322 | void flash_init(void) |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3323 | { |
Channagoud Kadabi | b2fb6ba | 2011-07-29 19:19:01 +0530 | [diff] [blame] | 3324 | int i = 0; |
Dima Zavin | e5f6435 | 2009-03-02 16:04:20 -0800 | [diff] [blame] | 3325 | ASSERT(flash_ptable == NULL); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3326 | |
| 3327 | flash_ptrlist = memalign(32, 1024); |
| 3328 | flash_cmdlist = memalign(32, 1024); |
Ajay Dudani | 232ce81 | 2009-12-02 00:14:11 -0800 | [diff] [blame] | 3329 | flash_data = memalign(32, 4096 + 128); |
| 3330 | flash_spare = memalign(32, 128); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3331 | |
Aparna Mallavarapu | c5946f2 | 2013-04-08 21:44:51 +0530 | [diff] [blame] | 3332 | if (flash_ptrlist == NULL || flash_cmdlist == NULL |
| 3333 | || flash_data == NULL || flash_spare == NULL) |
| 3334 | ASSERT(0); |
| 3335 | |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3336 | flash_read_id(flash_cmdlist, flash_ptrlist); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3337 | if ((FLASH_8BIT_NAND_DEVICE == flash_info.type) |
| 3338 | || (FLASH_16BIT_NAND_DEVICE == flash_info.type)) { |
| 3339 | if (flash_nand_read_config(flash_cmdlist, flash_ptrlist)) { |
| 3340 | dprintf(CRITICAL, |
| 3341 | "ERROR: could not read CFG0/CFG1 state\n"); |
Shashank Mittal | 83d16d0 | 2009-11-18 16:54:42 -0800 | [diff] [blame] | 3342 | ASSERT(0); |
| 3343 | } |
| 3344 | } |
Channagoud Kadabi | b2fb6ba | 2011-07-29 19:19:01 +0530 | [diff] [blame] | 3345 | /* Create a bad block table */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3346 | bbtbl = |
| 3347 | (unsigned int *)malloc(sizeof(unsigned int) * |
| 3348 | flash_info.num_blocks); |
| 3349 | for (i = 0; i < flash_info.num_blocks; i++) |
| 3350 | bbtbl[i] = -1; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | struct ptable *flash_get_ptable(void) |
| 3354 | { |
| 3355 | return flash_ptable; |
| 3356 | } |
| 3357 | |
Dima Zavin | e5f6435 | 2009-03-02 16:04:20 -0800 | [diff] [blame] | 3358 | void flash_set_ptable(struct ptable *new_ptable) |
| 3359 | { |
| 3360 | ASSERT(flash_ptable == NULL && new_ptable != NULL); |
| 3361 | flash_ptable = new_ptable; |
| 3362 | } |
| 3363 | |
Dima Zavin | ca337f5 | 2009-03-02 16:41:44 -0800 | [diff] [blame] | 3364 | struct flash_info *flash_get_info(void) |
| 3365 | { |
| 3366 | return &flash_info; |
| 3367 | } |
| 3368 | |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3369 | int flash_erase(struct ptentry *ptn) |
| 3370 | { |
| 3371 | unsigned block = ptn->start; |
| 3372 | unsigned count = ptn->length; |
| 3373 | |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 3374 | set_nand_configuration(ptn->type); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3375 | while (count-- > 0) { |
| 3376 | if (flash_erase_block |
| 3377 | (flash_cmdlist, flash_ptrlist, block * num_pages_per_blk)) { |
| 3378 | dprintf(INFO, "cannot erase @ %d (bad block?)\n", |
| 3379 | block); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3380 | } |
| 3381 | block++; |
| 3382 | } |
| 3383 | return 0; |
| 3384 | } |
| 3385 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3386 | int |
| 3387 | flash_read_ext(struct ptentry *ptn, unsigned extra_per_page, |
| 3388 | unsigned offset, void *data, unsigned bytes) |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3389 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3390 | unsigned page = |
| 3391 | (ptn->start * num_pages_per_blk) + (offset / flash_pagesize); |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 3392 | unsigned lastpage = (ptn->start + ptn->length) * num_pages_per_blk; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3393 | unsigned count = |
| 3394 | (bytes + flash_pagesize - 1 + extra_per_page) / (flash_pagesize + |
| 3395 | extra_per_page); |
| 3396 | unsigned *spare = (unsigned *)flash_spare; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3397 | unsigned errors = 0; |
| 3398 | unsigned char *image = data; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3399 | unsigned current_block = |
| 3400 | (page - (page & num_pages_per_blk_mask)) / num_pages_per_blk; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 3401 | unsigned start_block = ptn->start; |
| 3402 | int result = 0; |
| 3403 | int isbad = 0; |
Shashank Mittal | fd1f04f | 2010-08-03 17:38:48 -0700 | [diff] [blame] | 3404 | int start_block_count = 0; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3405 | |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 3406 | set_nand_configuration(TYPE_APPS_PARTITION); |
| 3407 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3408 | if (offset & (flash_pagesize - 1)) |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3409 | return -1; |
| 3410 | |
Shashank Mittal | fd1f04f | 2010-08-03 17:38:48 -0700 | [diff] [blame] | 3411 | // Adjust page offset based on number of bad blocks from start to current page |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3412 | if (start_block < current_block) { |
Shashank Mittal | fd1f04f | 2010-08-03 17:38:48 -0700 | [diff] [blame] | 3413 | start_block_count = (current_block - start_block); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3414 | while (start_block_count |
| 3415 | && (start_block < (ptn->start + ptn->length))) { |
| 3416 | isbad = |
| 3417 | _flash_block_isbad(flash_cmdlist, flash_ptrlist, |
| 3418 | start_block * num_pages_per_blk); |
Shashank Mittal | fd1f04f | 2010-08-03 17:38:48 -0700 | [diff] [blame] | 3419 | if (isbad) |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 3420 | page += num_pages_per_blk; |
Shashank Mittal | fd1f04f | 2010-08-03 17:38:48 -0700 | [diff] [blame] | 3421 | else |
| 3422 | start_block_count--; |
| 3423 | start_block++; |
| 3424 | } |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 3425 | } |
| 3426 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3427 | while ((page < lastpage) && !start_block_count) { |
| 3428 | if (count == 0) { |
| 3429 | dprintf(INFO, "flash_read_image: success (%d errors)\n", |
| 3430 | errors); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3431 | return 0; |
| 3432 | } |
| 3433 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3434 | result = |
| 3435 | _flash_read_page(flash_cmdlist, flash_ptrlist, page, image, |
| 3436 | spare); |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 3437 | |
| 3438 | if (result == -1) { |
| 3439 | // bad page, go to next page |
| 3440 | page++; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3441 | errors++; |
| 3442 | continue; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3443 | } else if (result == -2) { |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 3444 | // bad block, go to next block same offset |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 3445 | page += num_pages_per_blk; |
Shashank Mittal | ad3d05c | 2009-11-19 15:53:57 -0800 | [diff] [blame] | 3446 | errors++; |
| 3447 | continue; |
| 3448 | } |
| 3449 | |
| 3450 | page++; |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 3451 | image += flash_pagesize; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3452 | memcpy(image, spare, extra_per_page); |
| 3453 | image += extra_per_page; |
| 3454 | count -= 1; |
| 3455 | } |
| 3456 | |
| 3457 | /* could not find enough valid pages before we hit the end */ |
| 3458 | dprintf(INFO, "flash_read_image: failed (%d errors)\n", errors); |
| 3459 | return 0xffffffff; |
| 3460 | } |
| 3461 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3462 | int |
Deepa Dinamani | c13d594 | 2013-04-30 15:48:53 -0700 | [diff] [blame] | 3463 | flash_write(struct ptentry *ptn, unsigned write_extra_bytes, const void *data, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3464 | unsigned bytes) |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3465 | { |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 3466 | unsigned page = ptn->start * num_pages_per_blk; |
| 3467 | unsigned lastpage = (ptn->start + ptn->length) * num_pages_per_blk; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3468 | unsigned *spare = (unsigned *)flash_spare; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3469 | const unsigned char *image = data; |
Deepa Dinamani | c13d594 | 2013-04-30 15:48:53 -0700 | [diff] [blame] | 3470 | unsigned wsize; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3471 | unsigned n; |
| 3472 | int r; |
| 3473 | |
Deepa Dinamani | c13d594 | 2013-04-30 15:48:53 -0700 | [diff] [blame] | 3474 | if(write_extra_bytes) |
| 3475 | wsize = flash_pagesize + flash_info.spare_size; |
| 3476 | else |
| 3477 | wsize = flash_pagesize; |
| 3478 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3479 | if ((flash_info.type == FLASH_ONENAND_DEVICE) |
| 3480 | && (ptn->type == TYPE_MODEM_PARTITION)) { |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 3481 | dprintf(CRITICAL, "flash_write_image: feature not supported\n"); |
| 3482 | return -1; |
| 3483 | } |
| 3484 | |
| 3485 | set_nand_configuration(ptn->type); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3486 | for (n = 0; n < 16; n++) |
| 3487 | spare[n] = 0xffffffff; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3488 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3489 | while (bytes > 0) { |
| 3490 | if (bytes < wsize) { |
| 3491 | dprintf(CRITICAL, |
| 3492 | "flash_write_image: image undersized (%d < %d)\n", |
| 3493 | bytes, wsize); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3494 | return -1; |
| 3495 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3496 | if (page >= lastpage) { |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3497 | dprintf(CRITICAL, "flash_write_image: out of space\n"); |
| 3498 | return -1; |
| 3499 | } |
| 3500 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3501 | if ((page & num_pages_per_blk_mask) == 0) { |
| 3502 | if (flash_erase_block |
| 3503 | (flash_cmdlist, flash_ptrlist, page)) { |
| 3504 | dprintf(INFO, |
| 3505 | "flash_write_image: bad block @ %d\n", |
| 3506 | page / num_pages_per_blk); |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 3507 | page += num_pages_per_blk; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3508 | continue; |
| 3509 | } |
| 3510 | } |
| 3511 | |
Deepa Dinamani | c13d594 | 2013-04-30 15:48:53 -0700 | [diff] [blame] | 3512 | if (write_extra_bytes) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3513 | r = _flash_write_page(flash_cmdlist, flash_ptrlist, |
| 3514 | page, image, |
| 3515 | image + flash_pagesize); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3516 | } else { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3517 | r = _flash_write_page(flash_cmdlist, flash_ptrlist, |
| 3518 | page, image, spare); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3519 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3520 | if (r) { |
| 3521 | dprintf(INFO, |
| 3522 | "flash_write_image: write failure @ page %d (src %d)\n", |
| 3523 | page, image - (const unsigned char *)data); |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 3524 | image -= (page & num_pages_per_blk_mask) * wsize; |
| 3525 | bytes += (page & num_pages_per_blk_mask) * wsize; |
| 3526 | page &= ~num_pages_per_blk_mask; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3527 | if (flash_erase_block |
| 3528 | (flash_cmdlist, flash_ptrlist, page)) { |
| 3529 | dprintf(INFO, |
| 3530 | "flash_write_image: erase failure @ page %d\n", |
| 3531 | page); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3532 | } |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 3533 | if (ptn->type != TYPE_MODEM_PARTITION) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3534 | flash_mark_badblock(flash_cmdlist, |
| 3535 | flash_ptrlist, page); |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 3536 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3537 | dprintf(INFO, |
| 3538 | "flash_write_image: restart write @ page %d (src %d)\n", |
| 3539 | page, image - (const unsigned char *)data); |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 3540 | page += num_pages_per_blk; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3541 | continue; |
| 3542 | } |
Shashank Mittal | d0c836d | 2009-11-20 10:31:18 -0800 | [diff] [blame] | 3543 | page++; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3544 | image += wsize; |
| 3545 | bytes -= wsize; |
| 3546 | } |
| 3547 | |
| 3548 | /* erase any remaining pages in the partition */ |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 3549 | page = (page + num_pages_per_blk_mask) & (~num_pages_per_blk_mask); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3550 | while (page < lastpage) { |
| 3551 | if (flash_erase_block(flash_cmdlist, flash_ptrlist, page)) { |
| 3552 | dprintf(INFO, "flash_write_image: bad block @ %d\n", |
| 3553 | page / num_pages_per_blk); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3554 | } |
Channagoud Kadabi | beb17d5 | 2011-03-25 17:14:00 +0530 | [diff] [blame] | 3555 | page += num_pages_per_blk; |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | dprintf(INFO, "flash_write_image: success\n"); |
| 3559 | return 0; |
| 3560 | } |
| 3561 | |
| 3562 | #if 0 |
| 3563 | static int flash_read_page(unsigned page, void *data, void *extra) |
| 3564 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3565 | return _flash_read_page(flash_cmdlist, flash_ptrlist, page, data, |
| 3566 | extra); |
Dima Zavin | 03cf431 | 2009-01-23 16:38:30 -0800 | [diff] [blame] | 3567 | } |
| 3568 | #endif |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 3569 | |
| 3570 | unsigned flash_page_size(void) |
| 3571 | { |
| 3572 | return flash_pagesize; |
| 3573 | } |
Chandan Uddaraju | 14e57eb | 2010-06-28 12:11:06 -0700 | [diff] [blame] | 3574 | |
| 3575 | void enable_interleave_mode(int status) |
| 3576 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3577 | interleaved_mode = status; |
| 3578 | if (status) { |
| 3579 | flash_pagesize *= 2; |
| 3580 | platform_config_interleaved_mode_gpios(); |
| 3581 | } |
| 3582 | return; |
Chandan Uddaraju | 14e57eb | 2010-06-28 12:11:06 -0700 | [diff] [blame] | 3583 | } |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 3584 | |
| 3585 | int flash_ecc_bch_enabled() |
| 3586 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3587 | return enable_bch_ecc; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 3588 | } |