Root/
| 1 | #pypp 0 |
| 2 | // Iris: micro-kernel for a capability-based operating system. |
| 3 | // mips/nand.hhp: NAND driver functions, split off to be used in two places. |
| 4 | // The functions are not inline, so this file must be included exactly once per executable that needs it. |
| 5 | // Copyright 2009 Bas Wijnen <wijnen@debian.org> |
| 6 | // |
| 7 | // This program is free software: you can redistribute it and/or modify |
| 8 | // it under the terms of the GNU General Public License as published by |
| 9 | // the Free Software Foundation, either version 3 of the License, or |
| 10 | // (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // The following defines are taken from mtd/nand.h in the Linux source. |
| 21 | // Everything not in the nand datasheet is thrown out. |
| 22 | |
| 23 | //#define dbgl() debug ("%s:%d\n", __PRETTY_FUNCTION__, __LINE__) |
| 24 | #define dbgl() |
| 25 | |
| 26 | // Standard NAND flash commands |
| 27 | #define CMD_READ0 0 |
| 28 | #define CMD_READSTART 0x30 |
| 29 | |
| 30 | #define CMD_READID 0x90 |
| 31 | |
| 32 | #define CMD_RESET 0xff |
| 33 | |
| 34 | #define CMD_SEQIN 0x80 |
| 35 | #define CMD_PAGEPROG 0x10 |
| 36 | |
| 37 | #define CMD_ERASE1 0x60 |
| 38 | #define CMD_ERASE2 0xd0 |
| 39 | |
| 40 | #define CMD_RNDIN 0x85 |
| 41 | |
| 42 | #define CMD_RNDOUT 5 |
| 43 | #define CMD_RNDOUTSTART 0xe0 |
| 44 | |
| 45 | #define CMD_STATUS 0x70 |
| 46 | |
| 47 | // Status bits |
| 48 | #define STATUS_FAIL 0x01 |
| 49 | #define STATUS_READY 0x40 |
| 50 | #define STATUS_WRITABLE 0x80 |
| 51 | |
| 52 | static volatile char *command |
| 53 | static volatile char *address |
| 54 | static volatile char *data |
| 55 | |
| 56 | static unsigned page_bits |
| 57 | static unsigned redundant_bits |
| 58 | static unsigned block_bits |
| 59 | static unsigned size_bits |
| 60 | static unsigned word_size |
| 61 | static unsigned ecc_start |
| 62 | |
| 63 | static void unbusy (): |
| 64 | while !(gpio_get_port (2) & (1 << 30)): |
| 65 | DELAY () |
| 66 | |
| 67 | static void addr (unsigned d): |
| 68 | unbusy () |
| 69 | *address = d |
| 70 | |
| 71 | static void cmd (unsigned d): |
| 72 | unbusy () |
| 73 | *command = d |
| 74 | |
| 75 | static void wdata (unsigned d): |
| 76 | unbusy () |
| 77 | *data = d |
| 78 | |
| 79 | static unsigned rdata (): |
| 80 | unbusy () |
| 81 | unsigned ret = *data |
| 82 | return ret |
| 83 | |
| 84 | static void reset (): |
| 85 | // Set up. |
| 86 | gpio_as_nand () |
| 87 | gpio_as_gpio (2, 1 << 30) |
| 88 | gpio_as_input (2, 1 << 30) |
| 89 | gpio_enable_pull (2, 1 << 30) |
| 90 | EMC_NFCSR = EMC_NFCSR_NFE1 | EMC_NFCSR_NFCE1 |
| 91 | |
| 92 | // Reset nand. |
| 93 | cmd (CMD_RESET) |
| 94 | |
| 95 | cmd (CMD_READID) |
| 96 | addr (0) |
| 97 | unsigned d = rdata () |
| 98 | //unsigned maker = d |
| 99 | d = rdata () |
| 100 | //unsigned device = d |
| 101 | d = rdata () |
| 102 | //unsigned internal_chip_number = 1 << (d & 0x3) |
| 103 | //unsigned cell_type = 2 << ((d >> 2) & 0x3) |
| 104 | //unsigned simultaneously_programmed_pages = 1 << ((d >> 4) & 0x3) |
| 105 | //bool can_interleave_program_between_chips = d & 0x40 |
| 106 | //bool can_cache_program = d & 0x80 |
| 107 | d = rdata () |
| 108 | page_bits = 10 + (d & 3) |
| 109 | debug ("page bits: %d\n", page_bits) |
| 110 | redundant_bits = (d & 4 ? 4 : 3) |
| 111 | debug ("redundant bits: %d\n", redundant_bits) |
| 112 | block_bits = 16 + ((d >> 4) & 3) |
| 113 | debug ("block bits: %d\n", block_bits) |
| 114 | word_size = (d & 0x40 ? 16 : 8) |
| 115 | debug ("word size: %d\n", word_size) |
| 116 | //unsigned serial_access_minimum = (d & 0x80 ? 25 : 50) |
| 117 | d = rdata () |
| 118 | unsigned num_planes_bits = d >> 2 & 3 |
| 119 | unsigned plane_bits = 26 + (d >> 4 & 7) |
| 120 | size_bits = plane_bits + num_planes_bits - 3 |
| 121 | if page_bits == 11: |
| 122 | ecc_start = 6 |
| 123 | else if page_bits == 12: |
| 124 | ecc_start = 12 |
| 125 | else: |
| 126 | debug ("unknown nand type; assuming ecc offset to be 6. This is probably wrong!\n") |
| 127 | ecc_start = 6 |
| 128 | |
| 129 | static bool read (unsigned a, char *buffer): |
| 130 | unsigned column = a & ((1 << page_bits) - 1) |
| 131 | unsigned row = a >> page_bits |
| 132 | //debug ("reading %x:", a) |
| 133 | // Read oob information first. |
| 134 | char error[12] |
| 135 | // Spare space (starts at 1 << page_bits) |
| 136 | // 0: unused |
| 137 | // 2: detect valid data (at least 1 byte == 0 means valid) |
| 138 | // 5: unused |
| 139 | // 6: 9-byte ecc of 1st 512 bytes |
| 140 | // 15: 9-byte ecc of 2nd 512 bytes |
| 141 | // 24: 9-byte ecc of 3rd 512 bytes |
| 142 | // 33: 9-byte ecc of 4th 512 bytes |
| 143 | // 42: unused |
| 144 | // 64: end of space |
| 145 | |
| 146 | // For 2 GB nand: |
| 147 | // 0-11: unused/bad block markers |
| 148 | // 12-89: 8 times 9-byte ecc |
| 149 | // 90-127: unused |
| 150 | // 128: end of space |
| 151 | unsigned col = (1 << page_bits) + ecc_start + 9 * (column >> 9) |
| 152 | cmd (CMD_READ0) |
| 153 | addr (col) |
| 154 | addr (col >> 8) |
| 155 | addr (row) |
| 156 | addr (row >> 8) |
| 157 | addr (row >> 16) |
| 158 | cmd (CMD_READSTART) |
| 159 | //debug ("parity data:") |
| 160 | bool parity_all_ff = true |
| 161 | for unsigned t = 0; t < 9; ++t: |
| 162 | error[t] = rdata () |
| 163 | if parity_all_ff && (error[t] & 0xff) != 0xff: |
| 164 | parity_all_ff = false |
| 165 | //debug (" %x", error[t] & 0xff) |
| 166 | //debug ("\n") |
| 167 | cmd (CMD_RNDOUT) |
| 168 | addr (column) |
| 169 | addr (column >> 8) |
| 170 | cmd (CMD_RNDOUTSTART) |
| 171 | EMC_NFINTS = 0 |
| 172 | EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_DECODING | EMC_NFECR_ERST |
| 173 | bool all_ff = true |
| 174 | for unsigned t = 0; t < 0x200; ++t: |
| 175 | buffer[t] = rdata () |
| 176 | if all_ff && (buffer[t] & 0xff) != 0xff: |
| 177 | all_ff = false |
| 178 | if !all_ff || !parity_all_ff: |
| 179 | for unsigned t = 0; t < 9; ++t: |
| 180 | ((volatile char *)&EMC_NFPAR (0))[t] = error[t] |
| 181 | EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_DECODING | EMC_NFECR_PRDY |
| 182 | while !(EMC_NFINTS & EMC_NFINTS_DECF): |
| 183 | DELAY () |
| 184 | unsigned ints = EMC_NFINTS |
| 185 | if ints & EMC_NFINTS_UNCOR: |
| 186 | debug ("uncorrectable error in nand at %x\n", a) |
| 187 | return false |
| 188 | unsigned errs = (ints & EMC_NFINTS_ERRCNT_MASK) >> EMC_NFINTS_ERRCNT_BIT |
| 189 | for unsigned i = 0; i < errs; ++i: |
| 190 | unsigned err = EMC_NFERR (i) |
| 191 | unsigned index = (err >> 16) - 1 |
| 192 | unsigned mask = err & 0x1ff |
| 193 | unsigned bit = index * 9 |
| 194 | unsigned offset= bit & 7 |
| 195 | unsigned byte = bit / 8 |
| 196 | debug ("correcting %x on %x+%d\n", mask, byte, offset) |
| 197 | unsigned data = buffer[byte] | buffer[byte + 1] << 8 |
| 198 | data ^= mask << offset |
| 199 | buffer[byte] = data |
| 200 | buffer[byte + 1] = data >> 8 |
| 201 | #if 0 |
| 202 | for unsigned i = 0; i < 0x10; ++i: |
| 203 | if (buffer[i] & 0xff) < 0x10: |
| 204 | debug (" 0") |
| 205 | else: |
| 206 | debug (" ") |
| 207 | debug ("%x", buffer[i] & 0xff) |
| 208 | debug ("\n") |
| 209 | #endif |
| 210 | return true |
| 211 | |
| 212 | static void write (unsigned a, char *buffer): |
| 213 | unsigned row = a >> page_bits |
| 214 | //debug ("writing: %x/%x\n", a, row) |
| 215 | cmd (CMD_SEQIN) |
| 216 | addr (0) |
| 217 | addr (0) |
| 218 | addr (row) |
| 219 | addr (row >> 8) |
| 220 | addr (row >> 16) |
| 221 | char ecc[8][12] |
| 222 | for unsigned i = 0; i < 1 << (page_bits - 9); ++i: |
| 223 | bool all_ff = true |
| 224 | EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_ENCODING | EMC_NFECR_ERST |
| 225 | //debug ("writing data from %x\n", (unsigned)buffer + i * 0x200) |
| 226 | for unsigned j = 0; j < 0x200; ++j: |
| 227 | wdata (buffer[i * 0x200 + j]) |
| 228 | if all_ff && (buffer[i * 0x200 + j] & 0xff) != 0xff: |
| 229 | all_ff = false |
| 230 | if !all_ff: |
| 231 | while !(EMC_NFINTS & EMC_NFINTS_ENCF): |
| 232 | DELAY () |
| 233 | for unsigned t = 0; t < 9; ++t: |
| 234 | ecc[i][t] = ((volatile char *)&EMC_NFPAR (0))[t] |
| 235 | //debug ("parity for %x:", i * 0x200 + a) |
| 236 | //for unsigned t = 0; t < 9; ++t: |
| 237 | //debug (" %x", ecc[i][t] & 0xff) |
| 238 | //kdebug ("\n") |
| 239 | else: |
| 240 | for unsigned t = 0; t < 9; ++t: |
| 241 | ecc[i][t] = 0xff |
| 242 | // Spare space (starts at 1 << page_bits) |
| 243 | // 0: unused |
| 244 | // 2: detect valid data (at least 1 byte == 0 means valid) |
| 245 | // 5: unused |
| 246 | // 6: 9-byte ecc of 1st 512 bytes |
| 247 | // 15: 9-byte ecc of 2nd 512 bytes |
| 248 | // 24: 9-byte ecc of 3rd 512 bytes |
| 249 | // 33: 9-byte ecc of 4th 512 bytes |
| 250 | // 42: unused |
| 251 | // 64: end of space |
| 252 | |
| 253 | // For 2 GB nand: |
| 254 | // 0-11: unused/bad block markers |
| 255 | // 12-89: 8 times 9-byte ecc |
| 256 | // 90-127: unused |
| 257 | // 128: end of space |
| 258 | for unsigned i = 0; i < ecc_start; ++i: |
| 259 | wdata (i == 4 ? 0 : 0xff) |
| 260 | for unsigned i = 0; i < 1 << (page_bits - 9); ++i: |
| 261 | for unsigned j = 0; j < 9; ++j: |
| 262 | wdata (ecc[i][j]) |
| 263 | cmd (CMD_PAGEPROG) |
| 264 | // Wait at least 100 ns. |
| 265 | DELAY () |
| 266 | cmd (CMD_READ0) |
| 267 | addr (0) |
| 268 | addr (0) |
| 269 | addr (row) |
| 270 | addr (row >> 8) |
| 271 | addr (row >> 16) |
| 272 | cmd (CMD_READSTART) |
| 273 | for unsigned i = 0; i < 1 << (page_bits - 9); ++i: |
| 274 | EMC_NFINTS = 0 |
| 275 | EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_DECODING | EMC_NFECR_ERST |
| 276 | for unsigned t = 0; t < 0x200; ++t: |
| 277 | unsigned r = rdata () & 0xff |
| 278 | if r != (buffer[i * 0x200 + t] & 0xff): |
| 279 | debug ("program error at %x: %x != %x\n", i * 0x200 + t, buffer[i * 0x200 + t] & 0xff, r) |
| 280 | for unsigned t = 0; t < 9; ++t: |
| 281 | ((volatile char *)&EMC_NFPAR (0))[t] = ecc[i][t] |
| 282 | EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_DECODING | EMC_NFECR_PRDY |
| 283 | while !(EMC_NFINTS & EMC_NFINTS_DECF): |
| 284 | DELAY () |
| 285 | unsigned ints = EMC_NFINTS |
| 286 | if ints & EMC_NFINTS_UNCOR: |
| 287 | debug ("uncorrectable error during verify\n") |
| 288 | continue |
| 289 | unsigned errs = (ints & EMC_NFINTS_ERRCNT_MASK) >> EMC_NFINTS_ERRCNT_BIT |
| 290 | for unsigned i = 0; i < errs; ++i: |
| 291 | unsigned err = EMC_NFERR (i) |
| 292 | unsigned index = (err >> 16) - 1 |
| 293 | unsigned mask = err & 0x1ff |
| 294 | unsigned bit = index * 9 |
| 295 | unsigned offset= bit & 7 |
| 296 | unsigned byte = bit / 8 |
| 297 | debug ("error detected by parity: %x on %x+%d\n", mask, byte, offset) |
| 298 | for unsigned i = 0; i < ecc_start; ++i: |
| 299 | if rdata () != (i == 4 ? 0 : 0xff): |
| 300 | debug ("incorrect extra data at byte %d\n", i) |
| 301 | for unsigned i = 0; i < 1 << (page_bits - 9); ++i: |
| 302 | for unsigned j = 0; j < 9; ++j: |
| 303 | unsigned r = rdata () & 0xff |
| 304 | if r != (ecc[i][j] & 0xff): |
| 305 | debug ("ecc doesn't match: %x != %x\n", r, ecc[i][j] & 0xff) |
| 306 | //debug ("nand program %x:", a) |
| 307 | #if 0 |
| 308 | for unsigned i = 0; i < 0x10; ++i: |
| 309 | if (buffer[i] & 0xff) < 0x10: |
| 310 | debug (" 0") |
| 311 | else: |
| 312 | debug (" ") |
| 313 | debug ("%x", buffer[i] & 0xff) |
| 314 | debug ("\n") |
| 315 | #endif |
| 316 | |
| 317 | static void erase (unsigned a): |
| 318 | unsigned row = a >> page_bits |
| 319 | cmd (CMD_ERASE1) |
| 320 | addr (row) |
| 321 | addr (row >> 8) |
| 322 | addr (row >> 16) |
| 323 | cmd (CMD_ERASE2) |
| 324 | //debug ("nand erase %d done\n", a) |
| 325 |
Branches:
master
