Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Handles the M-Systems DiskOnChip G3 chip |
| 3 | * |
| 4 | * Copyright (C) 2011 Robert Jarzmik |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/io.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/mtd/mtd.h> |
| 31 | #include <linux/mtd/partitions.h> |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 32 | #include <linux/bitmap.h> |
| 33 | #include <linux/bitrev.h> |
| 34 | #include <linux/bch.h> |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 35 | |
| 36 | #include <linux/debugfs.h> |
| 37 | #include <linux/seq_file.h> |
| 38 | |
| 39 | #define CREATE_TRACE_POINTS |
| 40 | #include "docg3.h" |
| 41 | |
| 42 | /* |
| 43 | * This driver handles the DiskOnChip G3 flash memory. |
| 44 | * |
| 45 | * As no specification is available from M-Systems/Sandisk, this drivers lacks |
| 46 | * several functions available on the chip, as : |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 47 | * - IPL write |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 48 | * |
| 49 | * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and |
| 50 | * the driver assumes a 16bits data bus. |
| 51 | * |
| 52 | * DocG3 relies on 2 ECC algorithms, which are handled in hardware : |
| 53 | * - a 1 byte Hamming code stored in the OOB for each page |
| 54 | * - a 7 bytes BCH code stored in the OOB for each page |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 55 | * The BCH ECC is : |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 56 | * - BCH is in GF(2^14) |
| 57 | * - BCH is over data of 520 bytes (512 page + 7 page_info bytes |
| 58 | * + 1 hamming byte) |
| 59 | * - BCH can correct up to 4 bits (t = 4) |
| 60 | * - BCH syndroms are calculated in hardware, and checked in hardware as well |
| 61 | * |
| 62 | */ |
| 63 | |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 64 | static unsigned int reliable_mode; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 65 | module_param(reliable_mode, uint, 0); |
| 66 | MODULE_PARM_DESC(reliable_mode, "Set the docg3 mode (0=normal MLC, 1=fast, " |
| 67 | "2=reliable) : MLC normal operations are in normal mode"); |
| 68 | |
Robert Jarzmik | 732b63b | 2011-11-19 16:02:49 +0100 | [diff] [blame] | 69 | /** |
| 70 | * struct docg3_oobinfo - DiskOnChip G3 OOB layout |
| 71 | * @eccbytes: 8 bytes are used (1 for Hamming ECC, 7 for BCH ECC) |
| 72 | * @eccpos: ecc positions (byte 7 is Hamming ECC, byte 8-14 are BCH ECC) |
| 73 | * @oobfree: free pageinfo bytes (byte 0 until byte 6, byte 15 |
| 74 | * @oobavail: 8 available bytes remaining after ECC toll |
| 75 | */ |
| 76 | static struct nand_ecclayout docg3_oobinfo = { |
| 77 | .eccbytes = 8, |
| 78 | .eccpos = {7, 8, 9, 10, 11, 12, 13, 14}, |
| 79 | .oobfree = {{0, 7}, {15, 1} }, |
| 80 | .oobavail = 8, |
| 81 | }; |
| 82 | |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 83 | /** |
| 84 | * struct docg3_bch - BCH engine |
| 85 | */ |
| 86 | static struct bch_control *docg3_bch; |
| 87 | |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 88 | static inline u8 doc_readb(struct docg3 *docg3, u16 reg) |
| 89 | { |
| 90 | u8 val = readb(docg3->base + reg); |
| 91 | |
| 92 | trace_docg3_io(0, 8, reg, (int)val); |
| 93 | return val; |
| 94 | } |
| 95 | |
| 96 | static inline u16 doc_readw(struct docg3 *docg3, u16 reg) |
| 97 | { |
| 98 | u16 val = readw(docg3->base + reg); |
| 99 | |
| 100 | trace_docg3_io(0, 16, reg, (int)val); |
| 101 | return val; |
| 102 | } |
| 103 | |
| 104 | static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg) |
| 105 | { |
| 106 | writeb(val, docg3->base + reg); |
Robert Jarzmik | 84a9305 | 2011-11-19 16:02:44 +0100 | [diff] [blame] | 107 | trace_docg3_io(1, 8, reg, val); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg) |
| 111 | { |
| 112 | writew(val, docg3->base + reg); |
| 113 | trace_docg3_io(1, 16, reg, val); |
| 114 | } |
| 115 | |
| 116 | static inline void doc_flash_command(struct docg3 *docg3, u8 cmd) |
| 117 | { |
| 118 | doc_writeb(docg3, cmd, DOC_FLASHCOMMAND); |
| 119 | } |
| 120 | |
| 121 | static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq) |
| 122 | { |
| 123 | doc_writeb(docg3, seq, DOC_FLASHSEQUENCE); |
| 124 | } |
| 125 | |
| 126 | static inline void doc_flash_address(struct docg3 *docg3, u8 addr) |
| 127 | { |
| 128 | doc_writeb(docg3, addr, DOC_FLASHADDRESS); |
| 129 | } |
| 130 | |
| 131 | static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL }; |
| 132 | |
| 133 | static int doc_register_readb(struct docg3 *docg3, int reg) |
| 134 | { |
| 135 | u8 val; |
| 136 | |
| 137 | doc_writew(docg3, reg, DOC_READADDRESS); |
| 138 | val = doc_readb(docg3, reg); |
| 139 | doc_vdbg("Read register %04x : %02x\n", reg, val); |
| 140 | return val; |
| 141 | } |
| 142 | |
| 143 | static int doc_register_readw(struct docg3 *docg3, int reg) |
| 144 | { |
| 145 | u16 val; |
| 146 | |
| 147 | doc_writew(docg3, reg, DOC_READADDRESS); |
| 148 | val = doc_readw(docg3, reg); |
| 149 | doc_vdbg("Read register %04x : %04x\n", reg, val); |
| 150 | return val; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * doc_delay - delay docg3 operations |
| 155 | * @docg3: the device |
| 156 | * @nbNOPs: the number of NOPs to issue |
| 157 | * |
| 158 | * As no specification is available, the right timings between chip commands are |
| 159 | * unknown. The only available piece of information are the observed nops on a |
| 160 | * working docg3 chip. |
| 161 | * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler |
| 162 | * friendlier msleep() functions or blocking mdelay(). |
| 163 | */ |
| 164 | static void doc_delay(struct docg3 *docg3, int nbNOPs) |
| 165 | { |
| 166 | int i; |
| 167 | |
Robert Jarzmik | ac48e80 | 2011-11-19 16:02:43 +0100 | [diff] [blame] | 168 | doc_vdbg("NOP x %d\n", nbNOPs); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 169 | for (i = 0; i < nbNOPs; i++) |
| 170 | doc_writeb(docg3, 0, DOC_NOP); |
| 171 | } |
| 172 | |
| 173 | static int is_prot_seq_error(struct docg3 *docg3) |
| 174 | { |
| 175 | int ctrl; |
| 176 | |
| 177 | ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL); |
| 178 | return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR); |
| 179 | } |
| 180 | |
| 181 | static int doc_is_ready(struct docg3 *docg3) |
| 182 | { |
| 183 | int ctrl; |
| 184 | |
| 185 | ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL); |
| 186 | return ctrl & DOC_CTRL_FLASHREADY; |
| 187 | } |
| 188 | |
| 189 | static int doc_wait_ready(struct docg3 *docg3) |
| 190 | { |
| 191 | int maxWaitCycles = 100; |
| 192 | |
| 193 | do { |
| 194 | doc_delay(docg3, 4); |
| 195 | cpu_relax(); |
| 196 | } while (!doc_is_ready(docg3) && maxWaitCycles--); |
| 197 | doc_delay(docg3, 2); |
| 198 | if (maxWaitCycles > 0) |
| 199 | return 0; |
| 200 | else |
| 201 | return -EIO; |
| 202 | } |
| 203 | |
| 204 | static int doc_reset_seq(struct docg3 *docg3) |
| 205 | { |
| 206 | int ret; |
| 207 | |
| 208 | doc_writeb(docg3, 0x10, DOC_FLASHCONTROL); |
| 209 | doc_flash_sequence(docg3, DOC_SEQ_RESET); |
| 210 | doc_flash_command(docg3, DOC_CMD_RESET); |
| 211 | doc_delay(docg3, 2); |
| 212 | ret = doc_wait_ready(docg3); |
| 213 | |
| 214 | doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true"); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * doc_read_data_area - Read data from data area |
| 220 | * @docg3: the device |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 221 | * @buf: the buffer to fill in (might be NULL is dummy reads) |
| 222 | * @len: the length to read |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 223 | * @first: first time read, DOC_READADDRESS should be set |
| 224 | * |
| 225 | * Reads bytes from flash data. Handles the single byte / even bytes reads. |
| 226 | */ |
| 227 | static void doc_read_data_area(struct docg3 *docg3, void *buf, int len, |
| 228 | int first) |
| 229 | { |
| 230 | int i, cdr, len4; |
| 231 | u16 data16, *dst16; |
| 232 | u8 data8, *dst8; |
| 233 | |
| 234 | doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len); |
| 235 | cdr = len & 0x3; |
| 236 | len4 = len - cdr; |
| 237 | |
| 238 | if (first) |
| 239 | doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS); |
| 240 | dst16 = buf; |
| 241 | for (i = 0; i < len4; i += 2) { |
| 242 | data16 = doc_readw(docg3, DOC_IOSPACE_DATA); |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 243 | if (dst16) { |
| 244 | *dst16 = data16; |
| 245 | dst16++; |
| 246 | } |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | if (cdr) { |
| 250 | doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE, |
| 251 | DOC_READADDRESS); |
| 252 | doc_delay(docg3, 1); |
| 253 | dst8 = (u8 *)dst16; |
| 254 | for (i = 0; i < cdr; i++) { |
| 255 | data8 = doc_readb(docg3, DOC_IOSPACE_DATA); |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 256 | if (dst8) { |
| 257 | *dst8 = data8; |
| 258 | dst8++; |
| 259 | } |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 265 | * doc_write_data_area - Write data into data area |
| 266 | * @docg3: the device |
| 267 | * @buf: the buffer to get input bytes from |
| 268 | * @len: the length to write |
| 269 | * |
| 270 | * Writes bytes into flash data. Handles the single byte / even bytes writes. |
| 271 | */ |
| 272 | static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len) |
| 273 | { |
| 274 | int i, cdr, len4; |
| 275 | u16 *src16; |
| 276 | u8 *src8; |
| 277 | |
| 278 | doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len); |
| 279 | cdr = len & 0x3; |
| 280 | len4 = len - cdr; |
| 281 | |
| 282 | doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS); |
| 283 | src16 = (u16 *)buf; |
| 284 | for (i = 0; i < len4; i += 2) { |
| 285 | doc_writew(docg3, *src16, DOC_IOSPACE_DATA); |
| 286 | src16++; |
| 287 | } |
| 288 | |
| 289 | src8 = (u8 *)src16; |
| 290 | for (i = 0; i < cdr; i++) { |
| 291 | doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE, |
| 292 | DOC_READADDRESS); |
| 293 | doc_writeb(docg3, *src8, DOC_IOSPACE_DATA); |
| 294 | src8++; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /** |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 299 | * doc_set_data_mode - Sets the flash to normal or reliable data mode |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 300 | * @docg3: the device |
| 301 | * |
| 302 | * The reliable data mode is a bit slower than the fast mode, but less errors |
| 303 | * occur. Entering the reliable mode cannot be done without entering the fast |
| 304 | * mode first. |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 305 | * |
| 306 | * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks |
| 307 | * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading |
| 308 | * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same |
| 309 | * result, which is a logical and between bytes from page 0 and page 1 (which is |
| 310 | * consistent with the fact that writing to a page is _clearing_ bits of that |
| 311 | * page). |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 312 | */ |
| 313 | static void doc_set_reliable_mode(struct docg3 *docg3) |
| 314 | { |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 315 | static char *strmode[] = { "normal", "fast", "reliable", "invalid" }; |
| 316 | |
| 317 | doc_dbg("doc_set_reliable_mode(%s)\n", strmode[docg3->reliable]); |
| 318 | switch (docg3->reliable) { |
| 319 | case 0: |
| 320 | break; |
| 321 | case 1: |
| 322 | doc_flash_sequence(docg3, DOC_SEQ_SET_FASTMODE); |
| 323 | doc_flash_command(docg3, DOC_CMD_FAST_MODE); |
| 324 | break; |
| 325 | case 2: |
| 326 | doc_flash_sequence(docg3, DOC_SEQ_SET_RELIABLEMODE); |
| 327 | doc_flash_command(docg3, DOC_CMD_FAST_MODE); |
| 328 | doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE); |
| 329 | break; |
| 330 | default: |
| 331 | doc_err("doc_set_reliable_mode(): invalid mode\n"); |
| 332 | break; |
| 333 | } |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 334 | doc_delay(docg3, 2); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * doc_set_asic_mode - Set the ASIC mode |
| 339 | * @docg3: the device |
| 340 | * @mode: the mode |
| 341 | * |
| 342 | * The ASIC can work in 3 modes : |
| 343 | * - RESET: all registers are zeroed |
| 344 | * - NORMAL: receives and handles commands |
| 345 | * - POWERDOWN: minimal poweruse, flash parts shut off |
| 346 | */ |
| 347 | static void doc_set_asic_mode(struct docg3 *docg3, u8 mode) |
| 348 | { |
| 349 | int i; |
| 350 | |
| 351 | for (i = 0; i < 12; i++) |
| 352 | doc_readb(docg3, DOC_IOSPACE_IPL); |
| 353 | |
| 354 | mode |= DOC_ASICMODE_MDWREN; |
| 355 | doc_dbg("doc_set_asic_mode(%02x)\n", mode); |
| 356 | doc_writeb(docg3, mode, DOC_ASICMODE); |
| 357 | doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM); |
| 358 | doc_delay(docg3, 1); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * doc_set_device_id - Sets the devices id for cascaded G3 chips |
| 363 | * @docg3: the device |
| 364 | * @id: the chip to select (amongst 0, 1, 2, 3) |
| 365 | * |
| 366 | * There can be 4 cascaded G3 chips. This function selects the one which will |
| 367 | * should be the active one. |
| 368 | */ |
| 369 | static void doc_set_device_id(struct docg3 *docg3, int id) |
| 370 | { |
| 371 | u8 ctrl; |
| 372 | |
| 373 | doc_dbg("doc_set_device_id(%d)\n", id); |
| 374 | doc_writeb(docg3, id, DOC_DEVICESELECT); |
| 375 | ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL); |
| 376 | |
| 377 | ctrl &= ~DOC_CTRL_VIOLATION; |
| 378 | ctrl |= DOC_CTRL_CE; |
| 379 | doc_writeb(docg3, ctrl, DOC_FLASHCONTROL); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * doc_set_extra_page_mode - Change flash page layout |
| 384 | * @docg3: the device |
| 385 | * |
| 386 | * Normally, the flash page is split into the data (512 bytes) and the out of |
| 387 | * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear |
| 388 | * leveling counters are stored. To access this last area of 4 bytes, a special |
| 389 | * mode must be input to the flash ASIC. |
| 390 | * |
| 391 | * Returns 0 if no error occured, -EIO else. |
| 392 | */ |
| 393 | static int doc_set_extra_page_mode(struct docg3 *docg3) |
| 394 | { |
| 395 | int fctrl; |
| 396 | |
| 397 | doc_dbg("doc_set_extra_page_mode()\n"); |
| 398 | doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532); |
| 399 | doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532); |
| 400 | doc_delay(docg3, 2); |
| 401 | |
| 402 | fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL); |
| 403 | if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR)) |
| 404 | return -EIO; |
| 405 | else |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | /** |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 410 | * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane |
| 411 | * @docg3: the device |
| 412 | * @sector: the sector |
| 413 | */ |
| 414 | static void doc_setup_addr_sector(struct docg3 *docg3, int sector) |
| 415 | { |
| 416 | doc_delay(docg3, 1); |
| 417 | doc_flash_address(docg3, sector & 0xff); |
| 418 | doc_flash_address(docg3, (sector >> 8) & 0xff); |
| 419 | doc_flash_address(docg3, (sector >> 16) & 0xff); |
| 420 | doc_delay(docg3, 1); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane |
| 425 | * @docg3: the device |
| 426 | * @sector: the sector |
| 427 | * @ofs: the offset in the page, between 0 and (512 + 16 + 512) |
| 428 | */ |
| 429 | static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs) |
| 430 | { |
| 431 | ofs = ofs >> 2; |
| 432 | doc_delay(docg3, 1); |
| 433 | doc_flash_address(docg3, ofs & 0xff); |
| 434 | doc_flash_address(docg3, sector & 0xff); |
| 435 | doc_flash_address(docg3, (sector >> 8) & 0xff); |
| 436 | doc_flash_address(docg3, (sector >> 16) & 0xff); |
| 437 | doc_delay(docg3, 1); |
| 438 | } |
| 439 | |
| 440 | /** |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 441 | * doc_seek - Set both flash planes to the specified block, page for reading |
| 442 | * @docg3: the device |
| 443 | * @block0: the first plane block index |
| 444 | * @block1: the second plane block index |
| 445 | * @page: the page index within the block |
| 446 | * @wear: if true, read will occur on the 4 extra bytes of the wear area |
| 447 | * @ofs: offset in page to read |
| 448 | * |
| 449 | * Programs the flash even and odd planes to the specific block and page. |
| 450 | * Alternatively, programs the flash to the wear area of the specified page. |
| 451 | */ |
| 452 | static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page, |
| 453 | int wear, int ofs) |
| 454 | { |
| 455 | int sector, ret = 0; |
| 456 | |
| 457 | doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n", |
| 458 | block0, block1, page, ofs, wear); |
| 459 | |
| 460 | if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) { |
| 461 | doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1); |
| 462 | doc_flash_command(docg3, DOC_CMD_READ_PLANE1); |
| 463 | doc_delay(docg3, 2); |
| 464 | } else { |
| 465 | doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2); |
| 466 | doc_flash_command(docg3, DOC_CMD_READ_PLANE2); |
| 467 | doc_delay(docg3, 2); |
| 468 | } |
| 469 | |
| 470 | doc_set_reliable_mode(docg3); |
| 471 | if (wear) |
| 472 | ret = doc_set_extra_page_mode(docg3); |
| 473 | if (ret) |
| 474 | goto out; |
| 475 | |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 476 | doc_flash_sequence(docg3, DOC_SEQ_READ); |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 477 | sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 478 | doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR); |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 479 | doc_setup_addr_sector(docg3, sector); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 480 | |
| 481 | sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK); |
| 482 | doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR); |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 483 | doc_setup_addr_sector(docg3, sector); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 484 | doc_delay(docg3, 1); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 485 | |
| 486 | out: |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | /** |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 491 | * doc_write_seek - Set both flash planes to the specified block, page for writing |
| 492 | * @docg3: the device |
| 493 | * @block0: the first plane block index |
| 494 | * @block1: the second plane block index |
| 495 | * @page: the page index within the block |
| 496 | * @ofs: offset in page to write |
| 497 | * |
| 498 | * Programs the flash even and odd planes to the specific block and page. |
| 499 | * Alternatively, programs the flash to the wear area of the specified page. |
| 500 | */ |
| 501 | static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page, |
| 502 | int ofs) |
| 503 | { |
| 504 | int ret = 0, sector; |
| 505 | |
| 506 | doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n", |
| 507 | block0, block1, page, ofs); |
| 508 | |
| 509 | doc_set_reliable_mode(docg3); |
| 510 | |
| 511 | if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) { |
| 512 | doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1); |
| 513 | doc_flash_command(docg3, DOC_CMD_READ_PLANE1); |
| 514 | doc_delay(docg3, 2); |
| 515 | } else { |
| 516 | doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2); |
| 517 | doc_flash_command(docg3, DOC_CMD_READ_PLANE2); |
| 518 | doc_delay(docg3, 2); |
| 519 | } |
| 520 | |
| 521 | doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP); |
| 522 | doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1); |
| 523 | |
| 524 | sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK); |
| 525 | doc_setup_writeaddr_sector(docg3, sector, ofs); |
| 526 | |
| 527 | doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3); |
| 528 | doc_delay(docg3, 2); |
| 529 | ret = doc_wait_ready(docg3); |
| 530 | if (ret) |
| 531 | goto out; |
| 532 | |
| 533 | doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1); |
| 534 | sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK); |
| 535 | doc_setup_writeaddr_sector(docg3, sector, ofs); |
| 536 | doc_delay(docg3, 1); |
| 537 | |
| 538 | out: |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | /** |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 544 | * doc_read_page_ecc_init - Initialize hardware ECC engine |
| 545 | * @docg3: the device |
| 546 | * @len: the number of bytes covered by the ECC (BCH covered) |
| 547 | * |
| 548 | * The function does initialize the hardware ECC engine to compute the Hamming |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 549 | * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes). |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 550 | * |
| 551 | * Return 0 if succeeded, -EIO on error |
| 552 | */ |
| 553 | static int doc_read_page_ecc_init(struct docg3 *docg3, int len) |
| 554 | { |
| 555 | doc_writew(docg3, DOC_ECCCONF0_READ_MODE |
| 556 | | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE |
| 557 | | (len & DOC_ECCCONF0_DATA_BYTES_MASK), |
| 558 | DOC_ECCCONF0); |
| 559 | doc_delay(docg3, 4); |
| 560 | doc_register_readb(docg3, DOC_FLASHCONTROL); |
| 561 | return doc_wait_ready(docg3); |
| 562 | } |
| 563 | |
| 564 | /** |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 565 | * doc_write_page_ecc_init - Initialize hardware BCH ECC engine |
| 566 | * @docg3: the device |
| 567 | * @len: the number of bytes covered by the ECC (BCH covered) |
| 568 | * |
| 569 | * The function does initialize the hardware ECC engine to compute the Hamming |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 570 | * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes). |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 571 | * |
| 572 | * Return 0 if succeeded, -EIO on error |
| 573 | */ |
| 574 | static int doc_write_page_ecc_init(struct docg3 *docg3, int len) |
| 575 | { |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 576 | doc_writew(docg3, DOC_ECCCONF0_WRITE_MODE |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 577 | | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE |
| 578 | | (len & DOC_ECCCONF0_DATA_BYTES_MASK), |
| 579 | DOC_ECCCONF0); |
| 580 | doc_delay(docg3, 4); |
| 581 | doc_register_readb(docg3, DOC_FLASHCONTROL); |
| 582 | return doc_wait_ready(docg3); |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator |
| 587 | * @docg3: the device |
| 588 | * |
| 589 | * Disables the hardware ECC generator and checker, for unchecked reads (as when |
| 590 | * reading OOB only or write status byte). |
| 591 | */ |
| 592 | static void doc_ecc_disable(struct docg3 *docg3) |
| 593 | { |
| 594 | doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0); |
| 595 | doc_delay(docg3, 4); |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine |
| 600 | * @docg3: the device |
| 601 | * @nb_bytes: the number of bytes covered by the ECC (Hamming covered) |
| 602 | * |
| 603 | * This function programs the ECC hardware to compute the hamming code on the |
| 604 | * last provided N bytes to the hardware generator. |
| 605 | */ |
| 606 | static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes) |
| 607 | { |
| 608 | u8 ecc_conf1; |
| 609 | |
| 610 | ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1); |
| 611 | ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK; |
| 612 | ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK); |
| 613 | doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1); |
| 614 | } |
| 615 | |
| 616 | /** |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 617 | * doc_ecc_bch_fix_data - Fix if need be read data from flash |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 618 | * @docg3: the device |
| 619 | * @buf: the buffer of read data (512 + 7 + 1 bytes) |
| 620 | * @hwecc: the hardware calculated ECC. |
| 621 | * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB |
| 622 | * area data, and calc_ecc the ECC calculated by the hardware generator. |
| 623 | * |
| 624 | * Checks if the received data matches the ECC, and if an error is detected, |
| 625 | * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3 |
| 626 | * understands the (data, ecc, syndroms) in an inverted order in comparison to |
| 627 | * the BCH library, the function reverses the order of bits (ie. bit7 and bit0, |
| 628 | * bit6 and bit 1, ...) for all ECC data. |
| 629 | * |
| 630 | * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch |
| 631 | * algorithm is used to decode this. However the hw operates on page |
| 632 | * data in a bit order that is the reverse of that of the bch alg, |
| 633 | * requiring that the bits be reversed on the result. Thanks to Ivan |
| 634 | * Djelic for his analysis. |
| 635 | * |
| 636 | * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit |
| 637 | * errors were detected and cannot be fixed. |
| 638 | */ |
| 639 | static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc) |
| 640 | { |
| 641 | u8 ecc[DOC_ECC_BCH_SIZE]; |
| 642 | int errorpos[DOC_ECC_BCH_T], i, numerrs; |
| 643 | |
| 644 | for (i = 0; i < DOC_ECC_BCH_SIZE; i++) |
| 645 | ecc[i] = bitrev8(hwecc[i]); |
| 646 | numerrs = decode_bch(docg3_bch, NULL, DOC_ECC_BCH_COVERED_BYTES, |
| 647 | NULL, ecc, NULL, errorpos); |
| 648 | BUG_ON(numerrs == -EINVAL); |
| 649 | if (numerrs < 0) |
| 650 | goto out; |
| 651 | |
| 652 | for (i = 0; i < numerrs; i++) |
| 653 | errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7)); |
| 654 | for (i = 0; i < numerrs; i++) |
| 655 | if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8) |
| 656 | /* error is located in data, correct it */ |
| 657 | change_bit(errorpos[i], buf); |
| 658 | out: |
| 659 | doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs); |
| 660 | return numerrs; |
| 661 | } |
| 662 | |
| 663 | |
| 664 | /** |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 665 | * doc_read_page_prepare - Prepares reading data from a flash page |
| 666 | * @docg3: the device |
| 667 | * @block0: the first plane block index on flash memory |
| 668 | * @block1: the second plane block index on flash memory |
| 669 | * @page: the page index in the block |
| 670 | * @offset: the offset in the page (must be a multiple of 4) |
| 671 | * |
| 672 | * Prepares the page to be read in the flash memory : |
| 673 | * - tell ASIC to map the flash pages |
| 674 | * - tell ASIC to be in read mode |
| 675 | * |
| 676 | * After a call to this method, a call to doc_read_page_finish is mandatory, |
| 677 | * to end the read cycle of the flash. |
| 678 | * |
| 679 | * Read data from a flash page. The length to be read must be between 0 and |
| 680 | * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because |
| 681 | * the extra bytes reading is not implemented). |
| 682 | * |
| 683 | * As pages are grouped by 2 (in 2 planes), reading from a page must be done |
| 684 | * in two steps: |
| 685 | * - one read of 512 bytes at offset 0 |
| 686 | * - one read of 512 bytes at offset 512 + 16 |
| 687 | * |
| 688 | * Returns 0 if successful, -EIO if a read error occured. |
| 689 | */ |
| 690 | static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1, |
| 691 | int page, int offset) |
| 692 | { |
| 693 | int wear_area = 0, ret = 0; |
| 694 | |
| 695 | doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n", |
| 696 | block0, block1, page, offset); |
| 697 | if (offset >= DOC_LAYOUT_WEAR_OFFSET) |
| 698 | wear_area = 1; |
| 699 | if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2)) |
| 700 | return -EINVAL; |
| 701 | |
| 702 | doc_set_device_id(docg3, docg3->device_id); |
| 703 | ret = doc_reset_seq(docg3); |
| 704 | if (ret) |
| 705 | goto err; |
| 706 | |
| 707 | /* Program the flash address block and page */ |
| 708 | ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset); |
| 709 | if (ret) |
| 710 | goto err; |
| 711 | |
| 712 | doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES); |
| 713 | doc_delay(docg3, 2); |
| 714 | doc_wait_ready(docg3); |
| 715 | |
| 716 | doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ); |
| 717 | doc_delay(docg3, 1); |
| 718 | if (offset >= DOC_LAYOUT_PAGE_SIZE * 2) |
| 719 | offset -= 2 * DOC_LAYOUT_PAGE_SIZE; |
| 720 | doc_flash_address(docg3, offset >> 2); |
| 721 | doc_delay(docg3, 1); |
| 722 | doc_wait_ready(docg3); |
| 723 | |
| 724 | doc_flash_command(docg3, DOC_CMD_READ_FLASH); |
| 725 | |
| 726 | return 0; |
| 727 | err: |
| 728 | doc_writeb(docg3, 0, DOC_DATAEND); |
| 729 | doc_delay(docg3, 2); |
| 730 | return -EIO; |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * doc_read_page_getbytes - Reads bytes from a prepared page |
| 735 | * @docg3: the device |
| 736 | * @len: the number of bytes to be read (must be a multiple of 4) |
Robert Jarzmik | d107bc3 | 2012-03-11 11:28:45 +0100 | [diff] [blame^] | 737 | * @buf: the buffer to be filled in (or NULL is forget bytes) |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 738 | * @first: 1 if first time read, DOC_READADDRESS should be set |
| 739 | * |
| 740 | */ |
| 741 | static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf, |
| 742 | int first) |
| 743 | { |
| 744 | doc_read_data_area(docg3, buf, len, first); |
| 745 | doc_delay(docg3, 2); |
| 746 | return len; |
| 747 | } |
| 748 | |
| 749 | /** |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 750 | * doc_write_page_putbytes - Writes bytes into a prepared page |
| 751 | * @docg3: the device |
| 752 | * @len: the number of bytes to be written |
| 753 | * @buf: the buffer of input bytes |
| 754 | * |
| 755 | */ |
| 756 | static void doc_write_page_putbytes(struct docg3 *docg3, int len, |
| 757 | const u_char *buf) |
| 758 | { |
| 759 | doc_write_data_area(docg3, buf, len); |
| 760 | doc_delay(docg3, 2); |
| 761 | } |
| 762 | |
| 763 | /** |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 764 | * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 765 | * @docg3: the device |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 766 | * @hwecc: the array of 7 integers where the hardware ecc will be stored |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 767 | */ |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 768 | static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc) |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 769 | { |
| 770 | int i; |
| 771 | |
| 772 | for (i = 0; i < DOC_ECC_BCH_SIZE; i++) |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 773 | hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i)); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /** |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 777 | * doc_page_finish - Ends reading/writing of a flash page |
| 778 | * @docg3: the device |
| 779 | */ |
| 780 | static void doc_page_finish(struct docg3 *docg3) |
| 781 | { |
| 782 | doc_writeb(docg3, 0, DOC_DATAEND); |
| 783 | doc_delay(docg3, 2); |
| 784 | } |
| 785 | |
| 786 | /** |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 787 | * doc_read_page_finish - Ends reading of a flash page |
| 788 | * @docg3: the device |
| 789 | * |
| 790 | * As a side effect, resets the chip selector to 0. This ensures that after each |
| 791 | * read operation, the floor 0 is selected. Therefore, if the systems halts, the |
| 792 | * reboot will boot on floor 0, where the IPL is. |
| 793 | */ |
| 794 | static void doc_read_page_finish(struct docg3 *docg3) |
| 795 | { |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 796 | doc_page_finish(docg3); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 797 | doc_set_device_id(docg3, 0); |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * calc_block_sector - Calculate blocks, pages and ofs. |
| 802 | |
| 803 | * @from: offset in flash |
| 804 | * @block0: first plane block index calculated |
| 805 | * @block1: second plane block index calculated |
| 806 | * @page: page calculated |
| 807 | * @ofs: offset in page |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 808 | * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in |
| 809 | * reliable mode. |
| 810 | * |
| 811 | * The calculation is based on the reliable/normal mode. In normal mode, the 64 |
| 812 | * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are |
| 813 | * clones, only 32 pages per block are available. |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 814 | */ |
| 815 | static void calc_block_sector(loff_t from, int *block0, int *block1, int *page, |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 816 | int *ofs, int reliable) |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 817 | { |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 818 | uint sector, pages_biblock; |
| 819 | |
| 820 | pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES; |
| 821 | if (reliable == 1 || reliable == 2) |
| 822 | pages_biblock /= 2; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 823 | |
| 824 | sector = from / DOC_LAYOUT_PAGE_SIZE; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 825 | *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 826 | *block1 = *block0 + 1; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 827 | *page = sector % pages_biblock; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 828 | *page /= DOC_LAYOUT_NBPLANES; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 829 | if (reliable == 1 || reliable == 2) |
| 830 | *page *= 2; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 831 | if (sector % 2) |
| 832 | *ofs = DOC_LAYOUT_PAGE_OOB_SIZE; |
| 833 | else |
| 834 | *ofs = 0; |
| 835 | } |
| 836 | |
| 837 | /** |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 838 | * doc_read_oob - Read out of band bytes from flash |
| 839 | * @mtd: the device |
| 840 | * @from: the offset from first block and first page, in bytes, aligned on page |
| 841 | * size |
| 842 | * @ops: the mtd oob structure |
| 843 | * |
| 844 | * Reads flash memory OOB area of pages. |
| 845 | * |
| 846 | * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured |
| 847 | */ |
| 848 | static int doc_read_oob(struct mtd_info *mtd, loff_t from, |
| 849 | struct mtd_oob_ops *ops) |
| 850 | { |
| 851 | struct docg3 *docg3 = mtd->priv; |
Robert Jarzmik | d107bc3 | 2012-03-11 11:28:45 +0100 | [diff] [blame^] | 852 | int block0, block1, page, ret, skip, ofs = 0; |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 853 | u8 *oobbuf = ops->oobbuf; |
| 854 | u8 *buf = ops->datbuf; |
| 855 | size_t len, ooblen, nbdata, nboob; |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 856 | u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1; |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 857 | |
| 858 | if (buf) |
| 859 | len = ops->len; |
| 860 | else |
| 861 | len = 0; |
| 862 | if (oobbuf) |
| 863 | ooblen = ops->ooblen; |
| 864 | else |
| 865 | ooblen = 0; |
| 866 | |
| 867 | if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB) |
| 868 | oobbuf += ops->ooboffs; |
| 869 | |
| 870 | doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n", |
| 871 | from, ops->mode, buf, len, oobbuf, ooblen); |
Robert Jarzmik | d107bc3 | 2012-03-11 11:28:45 +0100 | [diff] [blame^] | 872 | if (ooblen % DOC_LAYOUT_OOB_SIZE) |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 873 | return -EINVAL; |
| 874 | |
| 875 | ret = -EINVAL; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 876 | calc_block_sector(from + len, &block0, &block1, &page, &ofs, |
| 877 | docg3->reliable); |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 878 | if (block1 > docg3->max_block) |
| 879 | goto err; |
| 880 | |
| 881 | ops->oobretlen = 0; |
| 882 | ops->retlen = 0; |
| 883 | ret = 0; |
Robert Jarzmik | d107bc3 | 2012-03-11 11:28:45 +0100 | [diff] [blame^] | 884 | skip = from % DOC_LAYOUT_PAGE_SIZE; |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 885 | while (!ret && (len > 0 || ooblen > 0)) { |
Robert Jarzmik | d107bc3 | 2012-03-11 11:28:45 +0100 | [diff] [blame^] | 886 | calc_block_sector(from - skip, &block0, &block1, &page, &ofs, |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 887 | docg3->reliable); |
Robert Jarzmik | d107bc3 | 2012-03-11 11:28:45 +0100 | [diff] [blame^] | 888 | nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip); |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 889 | nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE); |
| 890 | ret = doc_read_page_prepare(docg3, block0, block1, page, ofs); |
| 891 | if (ret < 0) |
| 892 | goto err; |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 893 | ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES); |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 894 | if (ret < 0) |
| 895 | goto err_in_read; |
Robert Jarzmik | d107bc3 | 2012-03-11 11:28:45 +0100 | [diff] [blame^] | 896 | ret = doc_read_page_getbytes(docg3, skip, NULL, 1); |
| 897 | if (ret < skip) |
| 898 | goto err_in_read; |
| 899 | ret = doc_read_page_getbytes(docg3, nbdata, buf, 0); |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 900 | if (ret < nbdata) |
| 901 | goto err_in_read; |
Robert Jarzmik | d107bc3 | 2012-03-11 11:28:45 +0100 | [diff] [blame^] | 902 | doc_read_page_getbytes(docg3, |
| 903 | DOC_LAYOUT_PAGE_SIZE - nbdata - skip, |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 904 | NULL, 0); |
| 905 | ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0); |
| 906 | if (ret < nboob) |
| 907 | goto err_in_read; |
| 908 | doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob, |
| 909 | NULL, 0); |
| 910 | |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 911 | doc_get_bch_hw_ecc(docg3, hwecc); |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 912 | eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1); |
| 913 | |
| 914 | if (nboob >= DOC_LAYOUT_OOB_SIZE) { |
| 915 | doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 916 | oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3], |
| 917 | oobbuf[4], oobbuf[5], oobbuf[6]); |
| 918 | doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]); |
| 919 | doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 920 | oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11], |
| 921 | oobbuf[12], oobbuf[13], oobbuf[14]); |
| 922 | doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]); |
| 923 | } |
| 924 | doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1); |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 925 | doc_dbg("ECC HW_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 926 | hwecc[0], hwecc[1], hwecc[2], hwecc[3], hwecc[4], |
| 927 | hwecc[5], hwecc[6]); |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 928 | |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 929 | ret = -EIO; |
| 930 | if (is_prot_seq_error(docg3)) |
| 931 | goto err_in_read; |
| 932 | ret = 0; |
| 933 | if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) && |
| 934 | (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) && |
| 935 | (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) && |
| 936 | (ops->mode != MTD_OPS_RAW) && |
| 937 | (nbdata == DOC_LAYOUT_PAGE_SIZE)) { |
| 938 | ret = doc_ecc_bch_fix_data(docg3, buf, hwecc); |
| 939 | if (ret < 0) { |
| 940 | mtd->ecc_stats.failed++; |
| 941 | ret = -EBADMSG; |
| 942 | } |
| 943 | if (ret > 0) { |
| 944 | mtd->ecc_stats.corrected += ret; |
| 945 | ret = -EUCLEAN; |
| 946 | } |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | doc_read_page_finish(docg3); |
| 950 | ops->retlen += nbdata; |
| 951 | ops->oobretlen += nboob; |
| 952 | buf += nbdata; |
| 953 | oobbuf += nboob; |
| 954 | len -= nbdata; |
| 955 | ooblen -= nboob; |
| 956 | from += DOC_LAYOUT_PAGE_SIZE; |
Robert Jarzmik | d107bc3 | 2012-03-11 11:28:45 +0100 | [diff] [blame^] | 957 | skip = 0; |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 958 | } |
| 959 | |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 960 | return ret; |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 961 | err_in_read: |
| 962 | doc_read_page_finish(docg3); |
| 963 | err: |
| 964 | return ret; |
| 965 | } |
| 966 | |
| 967 | /** |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 968 | * doc_read - Read bytes from flash |
| 969 | * @mtd: the device |
| 970 | * @from: the offset from first block and first page, in bytes, aligned on page |
| 971 | * size |
| 972 | * @len: the number of bytes to read (must be a multiple of 4) |
| 973 | * @retlen: the number of bytes actually read |
| 974 | * @buf: the filled in buffer |
| 975 | * |
| 976 | * Reads flash memory pages. This function does not read the OOB chunk, but only |
| 977 | * the page data. |
| 978 | * |
| 979 | * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured |
| 980 | */ |
| 981 | static int doc_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 982 | size_t *retlen, u_char *buf) |
| 983 | { |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 984 | struct mtd_oob_ops ops; |
| 985 | size_t ret; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 986 | |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 987 | memset(&ops, 0, sizeof(ops)); |
| 988 | ops.datbuf = buf; |
| 989 | ops.len = len; |
| 990 | ops.mode = MTD_OPS_AUTO_OOB; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 991 | |
Robert Jarzmik | 32a50b3 | 2011-11-19 16:02:47 +0100 | [diff] [blame] | 992 | ret = doc_read_oob(mtd, from, &ops); |
| 993 | *retlen = ops.retlen; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 994 | return ret; |
| 995 | } |
| 996 | |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 997 | static int doc_reload_bbt(struct docg3 *docg3) |
| 998 | { |
| 999 | int block = DOC_LAYOUT_BLOCK_BBT; |
| 1000 | int ret = 0, nbpages, page; |
| 1001 | u_char *buf = docg3->bbt; |
| 1002 | |
| 1003 | nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE); |
| 1004 | for (page = 0; !ret && (page < nbpages); page++) { |
| 1005 | ret = doc_read_page_prepare(docg3, block, block + 1, |
| 1006 | page + DOC_LAYOUT_PAGE_BBT, 0); |
| 1007 | if (!ret) |
| 1008 | ret = doc_read_page_ecc_init(docg3, |
| 1009 | DOC_LAYOUT_PAGE_SIZE); |
| 1010 | if (!ret) |
| 1011 | doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE, |
| 1012 | buf, 1); |
| 1013 | buf += DOC_LAYOUT_PAGE_SIZE; |
| 1014 | } |
| 1015 | doc_read_page_finish(docg3); |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * doc_block_isbad - Checks whether a block is good or not |
| 1021 | * @mtd: the device |
| 1022 | * @from: the offset to find the correct block |
| 1023 | * |
| 1024 | * Returns 1 if block is bad, 0 if block is good |
| 1025 | */ |
| 1026 | static int doc_block_isbad(struct mtd_info *mtd, loff_t from) |
| 1027 | { |
| 1028 | struct docg3 *docg3 = mtd->priv; |
| 1029 | int block0, block1, page, ofs, is_good; |
| 1030 | |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 1031 | calc_block_sector(from, &block0, &block1, &page, &ofs, |
| 1032 | docg3->reliable); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1033 | doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n", |
| 1034 | from, block0, block1, page, ofs); |
| 1035 | |
| 1036 | if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA) |
| 1037 | return 0; |
| 1038 | if (block1 > docg3->max_block) |
| 1039 | return -EINVAL; |
| 1040 | |
| 1041 | is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7)); |
| 1042 | return !is_good; |
| 1043 | } |
| 1044 | |
Robert Jarzmik | e10019b | 2011-12-16 23:25:23 +0100 | [diff] [blame] | 1045 | #if 0 |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1046 | /** |
| 1047 | * doc_get_erase_count - Get block erase count |
| 1048 | * @docg3: the device |
| 1049 | * @from: the offset in which the block is. |
| 1050 | * |
| 1051 | * Get the number of times a block was erased. The number is the maximum of |
| 1052 | * erase times between first and second plane (which should be equal normally). |
| 1053 | * |
| 1054 | * Returns The number of erases, or -EINVAL or -EIO on error. |
| 1055 | */ |
| 1056 | static int doc_get_erase_count(struct docg3 *docg3, loff_t from) |
| 1057 | { |
| 1058 | u8 buf[DOC_LAYOUT_WEAR_SIZE]; |
| 1059 | int ret, plane1_erase_count, plane2_erase_count; |
| 1060 | int block0, block1, page, ofs; |
| 1061 | |
| 1062 | doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf); |
| 1063 | if (from % DOC_LAYOUT_PAGE_SIZE) |
| 1064 | return -EINVAL; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 1065 | calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1066 | if (block1 > docg3->max_block) |
| 1067 | return -EINVAL; |
| 1068 | |
| 1069 | ret = doc_reset_seq(docg3); |
| 1070 | if (!ret) |
| 1071 | ret = doc_read_page_prepare(docg3, block0, block1, page, |
| 1072 | ofs + DOC_LAYOUT_WEAR_OFFSET); |
| 1073 | if (!ret) |
| 1074 | ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE, |
| 1075 | buf, 1); |
| 1076 | doc_read_page_finish(docg3); |
| 1077 | |
| 1078 | if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK)) |
| 1079 | return -EIO; |
| 1080 | plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8) |
| 1081 | | ((u8)(~buf[5]) << 16); |
| 1082 | plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8) |
| 1083 | | ((u8)(~buf[7]) << 16); |
| 1084 | |
| 1085 | return max(plane1_erase_count, plane2_erase_count); |
| 1086 | } |
Robert Jarzmik | e10019b | 2011-12-16 23:25:23 +0100 | [diff] [blame] | 1087 | #endif |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1088 | |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 1089 | /** |
| 1090 | * doc_get_op_status - get erase/write operation status |
| 1091 | * @docg3: the device |
| 1092 | * |
| 1093 | * Queries the status from the chip, and returns it |
| 1094 | * |
| 1095 | * Returns the status (bits DOC_PLANES_STATUS_*) |
| 1096 | */ |
| 1097 | static int doc_get_op_status(struct docg3 *docg3) |
| 1098 | { |
| 1099 | u8 status; |
| 1100 | |
| 1101 | doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS); |
| 1102 | doc_flash_command(docg3, DOC_CMD_PLANES_STATUS); |
| 1103 | doc_delay(docg3, 5); |
| 1104 | |
| 1105 | doc_ecc_disable(docg3); |
| 1106 | doc_read_data_area(docg3, &status, 1, 1); |
| 1107 | return status; |
| 1108 | } |
| 1109 | |
| 1110 | /** |
| 1111 | * doc_write_erase_wait_status - wait for write or erase completion |
| 1112 | * @docg3: the device |
| 1113 | * |
| 1114 | * Wait for the chip to be ready again after erase or write operation, and check |
| 1115 | * erase/write status. |
| 1116 | * |
| 1117 | * Returns 0 if erase successfull, -EIO if erase/write issue, -ETIMEOUT if |
| 1118 | * timeout |
| 1119 | */ |
| 1120 | static int doc_write_erase_wait_status(struct docg3 *docg3) |
| 1121 | { |
| 1122 | int status, ret = 0; |
| 1123 | |
| 1124 | if (!doc_is_ready(docg3)) |
| 1125 | usleep_range(3000, 3000); |
| 1126 | if (!doc_is_ready(docg3)) { |
| 1127 | doc_dbg("Timeout reached and the chip is still not ready\n"); |
| 1128 | ret = -EAGAIN; |
| 1129 | goto out; |
| 1130 | } |
| 1131 | |
| 1132 | status = doc_get_op_status(docg3); |
| 1133 | if (status & DOC_PLANES_STATUS_FAIL) { |
| 1134 | doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n", |
| 1135 | status); |
| 1136 | ret = -EIO; |
| 1137 | } |
| 1138 | |
| 1139 | out: |
| 1140 | doc_page_finish(docg3); |
| 1141 | return ret; |
| 1142 | } |
| 1143 | |
| 1144 | /** |
Robert Jarzmik | de03cd7 | 2011-11-19 16:02:53 +0100 | [diff] [blame] | 1145 | * doc_erase_block - Erase a couple of blocks |
| 1146 | * @docg3: the device |
| 1147 | * @block0: the first block to erase (leftmost plane) |
| 1148 | * @block1: the second block to erase (rightmost plane) |
| 1149 | * |
| 1150 | * Erase both blocks, and return operation status |
| 1151 | * |
| 1152 | * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not |
| 1153 | * ready for too long |
| 1154 | */ |
| 1155 | static int doc_erase_block(struct docg3 *docg3, int block0, int block1) |
| 1156 | { |
| 1157 | int ret, sector; |
| 1158 | |
| 1159 | doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1); |
| 1160 | ret = doc_reset_seq(docg3); |
| 1161 | if (ret) |
| 1162 | return -EIO; |
| 1163 | |
| 1164 | doc_set_reliable_mode(docg3); |
| 1165 | doc_flash_sequence(docg3, DOC_SEQ_ERASE); |
| 1166 | |
| 1167 | sector = block0 << DOC_ADDR_BLOCK_SHIFT; |
| 1168 | doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR); |
| 1169 | doc_setup_addr_sector(docg3, sector); |
| 1170 | sector = block1 << DOC_ADDR_BLOCK_SHIFT; |
| 1171 | doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR); |
| 1172 | doc_setup_addr_sector(docg3, sector); |
| 1173 | doc_delay(docg3, 1); |
| 1174 | |
| 1175 | doc_flash_command(docg3, DOC_CMD_ERASECYCLE2); |
| 1176 | doc_delay(docg3, 2); |
| 1177 | |
| 1178 | if (is_prot_seq_error(docg3)) { |
| 1179 | doc_err("Erase blocks %d,%d error\n", block0, block1); |
| 1180 | return -EIO; |
| 1181 | } |
| 1182 | |
| 1183 | return doc_write_erase_wait_status(docg3); |
| 1184 | } |
| 1185 | |
| 1186 | /** |
| 1187 | * doc_erase - Erase a portion of the chip |
| 1188 | * @mtd: the device |
| 1189 | * @info: the erase info |
| 1190 | * |
| 1191 | * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is |
| 1192 | * split into 2 pages of 512 bytes on 2 contiguous blocks. |
| 1193 | * |
| 1194 | * Returns 0 if erase successful, -EINVAL if adressing error, -EIO if erase |
| 1195 | * issue |
| 1196 | */ |
| 1197 | static int doc_erase(struct mtd_info *mtd, struct erase_info *info) |
| 1198 | { |
| 1199 | struct docg3 *docg3 = mtd->priv; |
| 1200 | uint64_t len; |
| 1201 | int block0, block1, page, ret, ofs = 0; |
| 1202 | |
| 1203 | doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len); |
| 1204 | doc_set_device_id(docg3, docg3->device_id); |
| 1205 | |
| 1206 | info->state = MTD_ERASE_PENDING; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 1207 | calc_block_sector(info->addr + info->len, &block0, &block1, &page, |
| 1208 | &ofs, docg3->reliable); |
Robert Jarzmik | de03cd7 | 2011-11-19 16:02:53 +0100 | [diff] [blame] | 1209 | ret = -EINVAL; |
| 1210 | if (block1 > docg3->max_block || page || ofs) |
| 1211 | goto reset_err; |
| 1212 | |
| 1213 | ret = 0; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 1214 | calc_block_sector(info->addr, &block0, &block1, &page, &ofs, |
| 1215 | docg3->reliable); |
Robert Jarzmik | de03cd7 | 2011-11-19 16:02:53 +0100 | [diff] [blame] | 1216 | doc_set_reliable_mode(docg3); |
| 1217 | for (len = info->len; !ret && len > 0; len -= mtd->erasesize) { |
| 1218 | info->state = MTD_ERASING; |
| 1219 | ret = doc_erase_block(docg3, block0, block1); |
| 1220 | block0 += 2; |
| 1221 | block1 += 2; |
| 1222 | } |
| 1223 | |
| 1224 | if (ret) |
| 1225 | goto reset_err; |
| 1226 | |
| 1227 | info->state = MTD_ERASE_DONE; |
| 1228 | return 0; |
| 1229 | |
| 1230 | reset_err: |
| 1231 | info->state = MTD_ERASE_FAILED; |
| 1232 | return ret; |
| 1233 | } |
| 1234 | |
| 1235 | /** |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 1236 | * doc_write_page - Write a single page to the chip |
| 1237 | * @docg3: the device |
| 1238 | * @to: the offset from first block and first page, in bytes, aligned on page |
| 1239 | * size |
| 1240 | * @buf: buffer to get bytes from |
| 1241 | * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be |
| 1242 | * written) |
| 1243 | * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or |
| 1244 | * BCH computations. If 1, only bytes 0-7 and byte 15 are taken, |
| 1245 | * remaining ones are filled with hardware Hamming and BCH |
| 1246 | * computations. Its value is not meaningfull is oob == NULL. |
| 1247 | * |
| 1248 | * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the |
| 1249 | * OOB data. The OOB ECC is automatically computed by the hardware Hamming and |
| 1250 | * BCH generator if autoecc is not null. |
| 1251 | * |
| 1252 | * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout |
| 1253 | */ |
| 1254 | static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf, |
| 1255 | const u_char *oob, int autoecc) |
| 1256 | { |
| 1257 | int block0, block1, page, ret, ofs = 0; |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 1258 | u8 hwecc[DOC_ECC_BCH_SIZE], hamming; |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 1259 | |
| 1260 | doc_dbg("doc_write_page(to=%lld)\n", to); |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 1261 | calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable); |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 1262 | |
| 1263 | doc_set_device_id(docg3, docg3->device_id); |
| 1264 | ret = doc_reset_seq(docg3); |
| 1265 | if (ret) |
| 1266 | goto err; |
| 1267 | |
| 1268 | /* Program the flash address block and page */ |
| 1269 | ret = doc_write_seek(docg3, block0, block1, page, ofs); |
| 1270 | if (ret) |
| 1271 | goto err; |
| 1272 | |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 1273 | doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES); |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 1274 | doc_delay(docg3, 2); |
| 1275 | doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf); |
| 1276 | |
| 1277 | if (oob && autoecc) { |
| 1278 | doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob); |
| 1279 | doc_delay(docg3, 2); |
| 1280 | oob += DOC_LAYOUT_OOB_UNUSED_OFS; |
| 1281 | |
| 1282 | hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY); |
| 1283 | doc_delay(docg3, 2); |
| 1284 | doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ, |
| 1285 | &hamming); |
| 1286 | doc_delay(docg3, 2); |
| 1287 | |
Robert Jarzmik | b604436 | 2011-12-02 20:00:12 +0100 | [diff] [blame] | 1288 | doc_get_bch_hw_ecc(docg3, hwecc); |
| 1289 | doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc); |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 1290 | doc_delay(docg3, 2); |
| 1291 | |
| 1292 | doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob); |
| 1293 | } |
| 1294 | if (oob && !autoecc) |
| 1295 | doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob); |
| 1296 | |
| 1297 | doc_delay(docg3, 2); |
| 1298 | doc_page_finish(docg3); |
| 1299 | doc_delay(docg3, 2); |
| 1300 | doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2); |
| 1301 | doc_delay(docg3, 2); |
| 1302 | |
| 1303 | /* |
| 1304 | * The wait status will perform another doc_page_finish() call, but that |
| 1305 | * seems to please the docg3, so leave it. |
| 1306 | */ |
| 1307 | ret = doc_write_erase_wait_status(docg3); |
| 1308 | return ret; |
| 1309 | err: |
| 1310 | doc_read_page_finish(docg3); |
| 1311 | return ret; |
| 1312 | } |
| 1313 | |
| 1314 | /** |
| 1315 | * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops |
| 1316 | * @ops: the oob operations |
| 1317 | * |
| 1318 | * Returns 0 or 1 if success, -EINVAL if invalid oob mode |
| 1319 | */ |
| 1320 | static int doc_guess_autoecc(struct mtd_oob_ops *ops) |
| 1321 | { |
| 1322 | int autoecc; |
| 1323 | |
| 1324 | switch (ops->mode) { |
| 1325 | case MTD_OPS_PLACE_OOB: |
| 1326 | case MTD_OPS_AUTO_OOB: |
| 1327 | autoecc = 1; |
| 1328 | break; |
| 1329 | case MTD_OPS_RAW: |
| 1330 | autoecc = 0; |
| 1331 | break; |
| 1332 | default: |
| 1333 | autoecc = -EINVAL; |
| 1334 | } |
| 1335 | return autoecc; |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes |
| 1340 | * @dst: the target 16 bytes OOB buffer |
| 1341 | * @oobsrc: the source 8 bytes non-ECC OOB buffer |
| 1342 | * |
| 1343 | */ |
| 1344 | static void doc_fill_autooob(u8 *dst, u8 *oobsrc) |
| 1345 | { |
| 1346 | memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ); |
| 1347 | dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ]; |
| 1348 | } |
| 1349 | |
| 1350 | /** |
| 1351 | * doc_backup_oob - Backup OOB into docg3 structure |
| 1352 | * @docg3: the device |
| 1353 | * @to: the page offset in the chip |
| 1354 | * @ops: the OOB size and buffer |
| 1355 | * |
| 1356 | * As the docg3 should write a page with its OOB in one pass, and some userland |
| 1357 | * applications do write_oob() to setup the OOB and then write(), store the OOB |
| 1358 | * into a temporary storage. This is very dangerous, as 2 concurrent |
| 1359 | * applications could store an OOB, and then write their pages (which will |
| 1360 | * result into one having its OOB corrupted). |
| 1361 | * |
| 1362 | * The only reliable way would be for userland to call doc_write_oob() with both |
| 1363 | * the page data _and_ the OOB area. |
| 1364 | * |
| 1365 | * Returns 0 if success, -EINVAL if ops content invalid |
| 1366 | */ |
| 1367 | static int doc_backup_oob(struct docg3 *docg3, loff_t to, |
| 1368 | struct mtd_oob_ops *ops) |
| 1369 | { |
| 1370 | int ooblen = ops->ooblen, autoecc; |
| 1371 | |
| 1372 | if (ooblen != DOC_LAYOUT_OOB_SIZE) |
| 1373 | return -EINVAL; |
| 1374 | autoecc = doc_guess_autoecc(ops); |
| 1375 | if (autoecc < 0) |
| 1376 | return autoecc; |
| 1377 | |
| 1378 | docg3->oob_write_ofs = to; |
| 1379 | docg3->oob_autoecc = autoecc; |
| 1380 | if (ops->mode == MTD_OPS_AUTO_OOB) { |
| 1381 | doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf); |
| 1382 | ops->oobretlen = 8; |
| 1383 | } else { |
| 1384 | memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE); |
| 1385 | ops->oobretlen = DOC_LAYOUT_OOB_SIZE; |
| 1386 | } |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * doc_write_oob - Write out of band bytes to flash |
| 1392 | * @mtd: the device |
| 1393 | * @ofs: the offset from first block and first page, in bytes, aligned on page |
| 1394 | * size |
| 1395 | * @ops: the mtd oob structure |
| 1396 | * |
| 1397 | * Either write OOB data into a temporary buffer, for the subsequent write |
| 1398 | * page. The provided OOB should be 16 bytes long. If a data buffer is provided |
| 1399 | * as well, issue the page write. |
| 1400 | * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will |
| 1401 | * still be filled in if asked for). |
| 1402 | * |
| 1403 | * Returns 0 is successfull, EINVAL if length is not 14 bytes |
| 1404 | */ |
| 1405 | static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, |
| 1406 | struct mtd_oob_ops *ops) |
| 1407 | { |
| 1408 | struct docg3 *docg3 = mtd->priv; |
| 1409 | int block0, block1, page, ret, pofs = 0, autoecc, oobdelta; |
| 1410 | u8 *oobbuf = ops->oobbuf; |
| 1411 | u8 *buf = ops->datbuf; |
| 1412 | size_t len, ooblen; |
| 1413 | u8 oob[DOC_LAYOUT_OOB_SIZE]; |
| 1414 | |
| 1415 | if (buf) |
| 1416 | len = ops->len; |
| 1417 | else |
| 1418 | len = 0; |
| 1419 | if (oobbuf) |
| 1420 | ooblen = ops->ooblen; |
| 1421 | else |
| 1422 | ooblen = 0; |
| 1423 | |
| 1424 | if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB) |
| 1425 | oobbuf += ops->ooboffs; |
| 1426 | |
| 1427 | doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n", |
| 1428 | ofs, ops->mode, buf, len, oobbuf, ooblen); |
| 1429 | switch (ops->mode) { |
| 1430 | case MTD_OPS_PLACE_OOB: |
| 1431 | case MTD_OPS_RAW: |
| 1432 | oobdelta = mtd->oobsize; |
| 1433 | break; |
| 1434 | case MTD_OPS_AUTO_OOB: |
| 1435 | oobdelta = mtd->ecclayout->oobavail; |
| 1436 | break; |
| 1437 | default: |
| 1438 | oobdelta = 0; |
| 1439 | } |
| 1440 | if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) || |
| 1441 | (ofs % DOC_LAYOUT_PAGE_SIZE)) |
| 1442 | return -EINVAL; |
| 1443 | if (len && ooblen && |
| 1444 | (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta)) |
| 1445 | return -EINVAL; |
| 1446 | |
| 1447 | ret = -EINVAL; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 1448 | calc_block_sector(ofs + len, &block0, &block1, &page, &pofs, |
| 1449 | docg3->reliable); |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 1450 | if (block1 > docg3->max_block) |
| 1451 | goto err; |
| 1452 | |
| 1453 | ops->oobretlen = 0; |
| 1454 | ops->retlen = 0; |
| 1455 | ret = 0; |
| 1456 | if (len == 0 && ooblen == 0) |
| 1457 | return -EINVAL; |
| 1458 | if (len == 0 && ooblen > 0) |
| 1459 | return doc_backup_oob(docg3, ofs, ops); |
| 1460 | |
| 1461 | autoecc = doc_guess_autoecc(ops); |
| 1462 | if (autoecc < 0) |
| 1463 | return autoecc; |
| 1464 | |
| 1465 | while (!ret && len > 0) { |
| 1466 | memset(oob, 0, sizeof(oob)); |
| 1467 | if (ofs == docg3->oob_write_ofs) |
| 1468 | memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE); |
| 1469 | else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB) |
| 1470 | doc_fill_autooob(oob, oobbuf); |
| 1471 | else if (ooblen > 0) |
| 1472 | memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE); |
| 1473 | ret = doc_write_page(docg3, ofs, buf, oob, autoecc); |
| 1474 | |
| 1475 | ofs += DOC_LAYOUT_PAGE_SIZE; |
| 1476 | len -= DOC_LAYOUT_PAGE_SIZE; |
| 1477 | buf += DOC_LAYOUT_PAGE_SIZE; |
| 1478 | if (ooblen) { |
| 1479 | oobbuf += oobdelta; |
| 1480 | ooblen -= oobdelta; |
| 1481 | ops->oobretlen += oobdelta; |
| 1482 | } |
| 1483 | ops->retlen += DOC_LAYOUT_PAGE_SIZE; |
| 1484 | } |
| 1485 | err: |
| 1486 | doc_set_device_id(docg3, 0); |
| 1487 | return ret; |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * doc_write - Write a buffer to the chip |
| 1492 | * @mtd: the device |
| 1493 | * @to: the offset from first block and first page, in bytes, aligned on page |
| 1494 | * size |
| 1495 | * @len: the number of bytes to write (must be a full page size, ie. 512) |
| 1496 | * @retlen: the number of bytes actually written (0 or 512) |
| 1497 | * @buf: the buffer to get bytes from |
| 1498 | * |
| 1499 | * Writes data to the chip. |
| 1500 | * |
| 1501 | * Returns 0 if write successful, -EIO if write error |
| 1502 | */ |
| 1503 | static int doc_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 1504 | size_t *retlen, const u_char *buf) |
| 1505 | { |
| 1506 | struct docg3 *docg3 = mtd->priv; |
| 1507 | int ret; |
| 1508 | struct mtd_oob_ops ops; |
| 1509 | |
| 1510 | doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len); |
| 1511 | ops.datbuf = (char *)buf; |
| 1512 | ops.len = len; |
| 1513 | ops.mode = MTD_OPS_PLACE_OOB; |
| 1514 | ops.oobbuf = NULL; |
| 1515 | ops.ooblen = 0; |
| 1516 | ops.ooboffs = 0; |
| 1517 | |
| 1518 | ret = doc_write_oob(mtd, to, &ops); |
| 1519 | *retlen = ops.retlen; |
| 1520 | return ret; |
| 1521 | } |
| 1522 | |
Robert Jarzmik | 0f769d3 | 2011-11-19 16:02:58 +0100 | [diff] [blame] | 1523 | static struct docg3 *sysfs_dev2docg3(struct device *dev, |
| 1524 | struct device_attribute *attr) |
| 1525 | { |
| 1526 | int floor; |
| 1527 | struct platform_device *pdev = to_platform_device(dev); |
| 1528 | struct mtd_info **docg3_floors = platform_get_drvdata(pdev); |
| 1529 | |
| 1530 | floor = attr->attr.name[1] - '0'; |
| 1531 | if (floor < 0 || floor >= DOC_MAX_NBFLOORS) |
| 1532 | return NULL; |
| 1533 | else |
| 1534 | return docg3_floors[floor]->priv; |
| 1535 | } |
| 1536 | |
| 1537 | static ssize_t dps0_is_key_locked(struct device *dev, |
| 1538 | struct device_attribute *attr, char *buf) |
| 1539 | { |
| 1540 | struct docg3 *docg3 = sysfs_dev2docg3(dev, attr); |
| 1541 | int dps0; |
| 1542 | |
| 1543 | doc_set_device_id(docg3, docg3->device_id); |
| 1544 | dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS); |
| 1545 | doc_set_device_id(docg3, 0); |
| 1546 | |
| 1547 | return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK)); |
| 1548 | } |
| 1549 | |
| 1550 | static ssize_t dps1_is_key_locked(struct device *dev, |
| 1551 | struct device_attribute *attr, char *buf) |
| 1552 | { |
| 1553 | struct docg3 *docg3 = sysfs_dev2docg3(dev, attr); |
| 1554 | int dps1; |
| 1555 | |
| 1556 | doc_set_device_id(docg3, docg3->device_id); |
| 1557 | dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS); |
| 1558 | doc_set_device_id(docg3, 0); |
| 1559 | |
| 1560 | return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK)); |
| 1561 | } |
| 1562 | |
| 1563 | static ssize_t dps0_insert_key(struct device *dev, |
| 1564 | struct device_attribute *attr, |
| 1565 | const char *buf, size_t count) |
| 1566 | { |
| 1567 | struct docg3 *docg3 = sysfs_dev2docg3(dev, attr); |
| 1568 | int i; |
| 1569 | |
| 1570 | if (count != DOC_LAYOUT_DPS_KEY_LENGTH) |
| 1571 | return -EINVAL; |
| 1572 | |
| 1573 | doc_set_device_id(docg3, docg3->device_id); |
| 1574 | for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++) |
| 1575 | doc_writeb(docg3, buf[i], DOC_DPS0_KEY); |
| 1576 | doc_set_device_id(docg3, 0); |
| 1577 | return count; |
| 1578 | } |
| 1579 | |
| 1580 | static ssize_t dps1_insert_key(struct device *dev, |
| 1581 | struct device_attribute *attr, |
| 1582 | const char *buf, size_t count) |
| 1583 | { |
| 1584 | struct docg3 *docg3 = sysfs_dev2docg3(dev, attr); |
| 1585 | int i; |
| 1586 | |
| 1587 | if (count != DOC_LAYOUT_DPS_KEY_LENGTH) |
| 1588 | return -EINVAL; |
| 1589 | |
| 1590 | doc_set_device_id(docg3, docg3->device_id); |
| 1591 | for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++) |
| 1592 | doc_writeb(docg3, buf[i], DOC_DPS1_KEY); |
| 1593 | doc_set_device_id(docg3, 0); |
| 1594 | return count; |
| 1595 | } |
| 1596 | |
| 1597 | #define FLOOR_SYSFS(id) { \ |
| 1598 | __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \ |
| 1599 | __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \ |
| 1600 | __ATTR(f##id##_dps0_protection_key, S_IWUGO, NULL, dps0_insert_key), \ |
| 1601 | __ATTR(f##id##_dps1_protection_key, S_IWUGO, NULL, dps1_insert_key), \ |
| 1602 | } |
| 1603 | |
| 1604 | static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = { |
| 1605 | FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3) |
| 1606 | }; |
| 1607 | |
| 1608 | static int doc_register_sysfs(struct platform_device *pdev, |
| 1609 | struct mtd_info **floors) |
| 1610 | { |
| 1611 | int ret = 0, floor, i = 0; |
| 1612 | struct device *dev = &pdev->dev; |
| 1613 | |
| 1614 | for (floor = 0; !ret && floor < DOC_MAX_NBFLOORS && floors[floor]; |
| 1615 | floor++) |
| 1616 | for (i = 0; !ret && i < 4; i++) |
| 1617 | ret = device_create_file(dev, &doc_sys_attrs[floor][i]); |
| 1618 | if (!ret) |
| 1619 | return 0; |
| 1620 | do { |
| 1621 | while (--i >= 0) |
| 1622 | device_remove_file(dev, &doc_sys_attrs[floor][i]); |
| 1623 | i = 4; |
| 1624 | } while (--floor >= 0); |
| 1625 | return ret; |
| 1626 | } |
| 1627 | |
| 1628 | static void doc_unregister_sysfs(struct platform_device *pdev, |
| 1629 | struct mtd_info **floors) |
| 1630 | { |
| 1631 | struct device *dev = &pdev->dev; |
| 1632 | int floor, i; |
| 1633 | |
| 1634 | for (floor = 0; floor < DOC_MAX_NBFLOORS && floors[floor]; |
| 1635 | floor++) |
| 1636 | for (i = 0; i < 4; i++) |
| 1637 | device_remove_file(dev, &doc_sys_attrs[floor][i]); |
| 1638 | } |
| 1639 | |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1640 | /* |
| 1641 | * Debug sysfs entries |
| 1642 | */ |
| 1643 | static int dbg_flashctrl_show(struct seq_file *s, void *p) |
| 1644 | { |
| 1645 | struct docg3 *docg3 = (struct docg3 *)s->private; |
| 1646 | |
| 1647 | int pos = 0; |
| 1648 | u8 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL); |
| 1649 | |
| 1650 | pos += seq_printf(s, |
| 1651 | "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n", |
| 1652 | fctrl, |
| 1653 | fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-", |
| 1654 | fctrl & DOC_CTRL_CE ? "active" : "inactive", |
| 1655 | fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-", |
| 1656 | fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-", |
| 1657 | fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready"); |
| 1658 | return pos; |
| 1659 | } |
| 1660 | DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show); |
| 1661 | |
| 1662 | static int dbg_asicmode_show(struct seq_file *s, void *p) |
| 1663 | { |
| 1664 | struct docg3 *docg3 = (struct docg3 *)s->private; |
| 1665 | |
| 1666 | int pos = 0; |
| 1667 | int pctrl = doc_register_readb(docg3, DOC_ASICMODE); |
| 1668 | int mode = pctrl & 0x03; |
| 1669 | |
| 1670 | pos += seq_printf(s, |
| 1671 | "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (", |
| 1672 | pctrl, |
| 1673 | pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0, |
| 1674 | pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0, |
| 1675 | pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0, |
| 1676 | pctrl & DOC_ASICMODE_MDWREN ? 1 : 0, |
| 1677 | pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0, |
| 1678 | mode >> 1, mode & 0x1); |
| 1679 | |
| 1680 | switch (mode) { |
| 1681 | case DOC_ASICMODE_RESET: |
| 1682 | pos += seq_printf(s, "reset"); |
| 1683 | break; |
| 1684 | case DOC_ASICMODE_NORMAL: |
| 1685 | pos += seq_printf(s, "normal"); |
| 1686 | break; |
| 1687 | case DOC_ASICMODE_POWERDOWN: |
| 1688 | pos += seq_printf(s, "powerdown"); |
| 1689 | break; |
| 1690 | } |
| 1691 | pos += seq_printf(s, ")\n"); |
| 1692 | return pos; |
| 1693 | } |
| 1694 | DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show); |
| 1695 | |
| 1696 | static int dbg_device_id_show(struct seq_file *s, void *p) |
| 1697 | { |
| 1698 | struct docg3 *docg3 = (struct docg3 *)s->private; |
| 1699 | int pos = 0; |
| 1700 | int id = doc_register_readb(docg3, DOC_DEVICESELECT); |
| 1701 | |
| 1702 | pos += seq_printf(s, "DeviceId = %d\n", id); |
| 1703 | return pos; |
| 1704 | } |
| 1705 | DEBUGFS_RO_ATTR(device_id, dbg_device_id_show); |
| 1706 | |
| 1707 | static int dbg_protection_show(struct seq_file *s, void *p) |
| 1708 | { |
| 1709 | struct docg3 *docg3 = (struct docg3 *)s->private; |
| 1710 | int pos = 0; |
Robert Jarzmik | dbc26d9 | 2011-11-19 16:02:45 +0100 | [diff] [blame] | 1711 | int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high; |
| 1712 | |
| 1713 | protect = doc_register_readb(docg3, DOC_PROTECTION); |
| 1714 | dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS); |
| 1715 | dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW); |
| 1716 | dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH); |
| 1717 | dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS); |
| 1718 | dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW); |
| 1719 | dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1720 | |
| 1721 | pos += seq_printf(s, "Protection = 0x%02x (", |
| 1722 | protect); |
| 1723 | if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK) |
| 1724 | pos += seq_printf(s, "FOUNDRY_OTP_LOCK,"); |
| 1725 | if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK) |
| 1726 | pos += seq_printf(s, "CUSTOMER_OTP_LOCK,"); |
| 1727 | if (protect & DOC_PROTECT_LOCK_INPUT) |
| 1728 | pos += seq_printf(s, "LOCK_INPUT,"); |
| 1729 | if (protect & DOC_PROTECT_STICKY_LOCK) |
| 1730 | pos += seq_printf(s, "STICKY_LOCK,"); |
| 1731 | if (protect & DOC_PROTECT_PROTECTION_ENABLED) |
| 1732 | pos += seq_printf(s, "PROTECTION ON,"); |
| 1733 | if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK) |
| 1734 | pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,"); |
| 1735 | if (protect & DOC_PROTECT_PROTECTION_ERROR) |
| 1736 | pos += seq_printf(s, "PROTECT_ERR,"); |
| 1737 | else |
| 1738 | pos += seq_printf(s, "NO_PROTECT_ERR"); |
| 1739 | pos += seq_printf(s, ")\n"); |
| 1740 | |
| 1741 | pos += seq_printf(s, "DPS0 = 0x%02x : " |
| 1742 | "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, " |
| 1743 | "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n", |
| 1744 | dps0, dps0_low, dps0_high, |
| 1745 | !!(dps0 & DOC_DPS_OTP_PROTECTED), |
| 1746 | !!(dps0 & DOC_DPS_READ_PROTECTED), |
| 1747 | !!(dps0 & DOC_DPS_WRITE_PROTECTED), |
| 1748 | !!(dps0 & DOC_DPS_HW_LOCK_ENABLED), |
| 1749 | !!(dps0 & DOC_DPS_KEY_OK)); |
| 1750 | pos += seq_printf(s, "DPS1 = 0x%02x : " |
| 1751 | "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, " |
| 1752 | "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n", |
| 1753 | dps1, dps1_low, dps1_high, |
| 1754 | !!(dps1 & DOC_DPS_OTP_PROTECTED), |
| 1755 | !!(dps1 & DOC_DPS_READ_PROTECTED), |
| 1756 | !!(dps1 & DOC_DPS_WRITE_PROTECTED), |
| 1757 | !!(dps1 & DOC_DPS_HW_LOCK_ENABLED), |
| 1758 | !!(dps1 & DOC_DPS_KEY_OK)); |
| 1759 | return pos; |
| 1760 | } |
| 1761 | DEBUGFS_RO_ATTR(protection, dbg_protection_show); |
| 1762 | |
| 1763 | static int __init doc_dbg_register(struct docg3 *docg3) |
| 1764 | { |
| 1765 | struct dentry *root, *entry; |
| 1766 | |
| 1767 | root = debugfs_create_dir("docg3", NULL); |
| 1768 | if (!root) |
| 1769 | return -ENOMEM; |
| 1770 | |
| 1771 | entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3, |
| 1772 | &flashcontrol_fops); |
| 1773 | if (entry) |
| 1774 | entry = debugfs_create_file("asic_mode", S_IRUSR, root, |
| 1775 | docg3, &asic_mode_fops); |
| 1776 | if (entry) |
| 1777 | entry = debugfs_create_file("device_id", S_IRUSR, root, |
| 1778 | docg3, &device_id_fops); |
| 1779 | if (entry) |
| 1780 | entry = debugfs_create_file("protection", S_IRUSR, root, |
| 1781 | docg3, &protection_fops); |
| 1782 | if (entry) { |
| 1783 | docg3->debugfs_root = root; |
| 1784 | return 0; |
| 1785 | } else { |
| 1786 | debugfs_remove_recursive(root); |
| 1787 | return -ENOMEM; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | static void __exit doc_dbg_unregister(struct docg3 *docg3) |
| 1792 | { |
| 1793 | debugfs_remove_recursive(docg3->debugfs_root); |
| 1794 | } |
| 1795 | |
| 1796 | /** |
| 1797 | * doc_set_driver_info - Fill the mtd_info structure and docg3 structure |
| 1798 | * @chip_id: The chip ID of the supported chip |
| 1799 | * @mtd: The structure to fill |
| 1800 | */ |
| 1801 | static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd) |
| 1802 | { |
| 1803 | struct docg3 *docg3 = mtd->priv; |
| 1804 | int cfg; |
| 1805 | |
| 1806 | cfg = doc_register_readb(docg3, DOC_CONFIGURATION); |
| 1807 | docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0); |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 1808 | docg3->reliable = reliable_mode; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1809 | |
| 1810 | switch (chip_id) { |
| 1811 | case DOC_CHIPID_G3: |
Robert Jarzmik | 31716a5 | 2012-03-03 21:57:33 +0100 | [diff] [blame] | 1812 | mtd->name = kasprintf(GFP_KERNEL, "docg3.%d", |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 1813 | docg3->device_id); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1814 | docg3->max_block = 2047; |
| 1815 | break; |
| 1816 | } |
| 1817 | mtd->type = MTD_NANDFLASH; |
Robert Jarzmik | 7a7fcf1 | 2011-11-19 16:02:54 +0100 | [diff] [blame] | 1818 | mtd->flags = MTD_CAP_NANDFLASH; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1819 | mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 1820 | if (docg3->reliable == 2) |
| 1821 | mtd->size /= 2; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1822 | mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES; |
Robert Jarzmik | c3de8a8 | 2011-11-19 16:02:57 +0100 | [diff] [blame] | 1823 | if (docg3->reliable == 2) |
| 1824 | mtd->erasesize /= 2; |
Artem Bityutskiy | 82c4c58 | 2012-02-03 09:44:32 +0200 | [diff] [blame] | 1825 | mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1826 | mtd->oobsize = DOC_LAYOUT_OOB_SIZE; |
| 1827 | mtd->owner = THIS_MODULE; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 1828 | mtd->_erase = doc_erase; |
| 1829 | mtd->_read = doc_read; |
| 1830 | mtd->_write = doc_write; |
| 1831 | mtd->_read_oob = doc_read_oob; |
| 1832 | mtd->_write_oob = doc_write_oob; |
| 1833 | mtd->_block_isbad = doc_block_isbad; |
Robert Jarzmik | 732b63b | 2011-11-19 16:02:49 +0100 | [diff] [blame] | 1834 | mtd->ecclayout = &docg3_oobinfo; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | /** |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 1838 | * doc_probe_device - Check if a device is available |
| 1839 | * @base: the io space where the device is probed |
| 1840 | * @floor: the floor of the probed device |
| 1841 | * @dev: the device |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1842 | * |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 1843 | * Checks whether a device at the specified IO range, and floor is available. |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1844 | * |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 1845 | * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM |
| 1846 | * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is |
| 1847 | * launched. |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1848 | */ |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 1849 | static struct mtd_info *doc_probe_device(void __iomem *base, int floor, |
| 1850 | struct device *dev) |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1851 | { |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1852 | int ret, bbt_nbpages; |
| 1853 | u16 chip_id, chip_id_inv; |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 1854 | struct docg3 *docg3; |
| 1855 | struct mtd_info *mtd; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 1856 | |
| 1857 | ret = -ENOMEM; |
| 1858 | docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL); |
| 1859 | if (!docg3) |
| 1860 | goto nomem1; |
| 1861 | mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL); |
| 1862 | if (!mtd) |
| 1863 | goto nomem2; |
| 1864 | mtd->priv = docg3; |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 1865 | bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1, |
| 1866 | 8 * DOC_LAYOUT_PAGE_SIZE); |
| 1867 | docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL); |
| 1868 | if (!docg3->bbt) |
| 1869 | goto nomem3; |
| 1870 | |
| 1871 | docg3->dev = dev; |
| 1872 | docg3->device_id = floor; |
| 1873 | docg3->base = base; |
| 1874 | doc_set_device_id(docg3, docg3->device_id); |
| 1875 | if (!floor) |
| 1876 | doc_set_asic_mode(docg3, DOC_ASICMODE_RESET); |
| 1877 | doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL); |
| 1878 | |
| 1879 | chip_id = doc_register_readw(docg3, DOC_CHIPID); |
| 1880 | chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV); |
| 1881 | |
| 1882 | ret = 0; |
| 1883 | if (chip_id != (u16)(~chip_id_inv)) { |
| 1884 | goto nomem3; |
| 1885 | } |
| 1886 | |
| 1887 | switch (chip_id) { |
| 1888 | case DOC_CHIPID_G3: |
| 1889 | doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n", |
| 1890 | base, floor); |
| 1891 | break; |
| 1892 | default: |
| 1893 | doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id); |
| 1894 | goto nomem3; |
| 1895 | } |
| 1896 | |
| 1897 | doc_set_driver_info(chip_id, mtd); |
| 1898 | |
Robert Jarzmik | fb50b58 | 2011-11-19 16:02:52 +0100 | [diff] [blame] | 1899 | doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ); |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 1900 | doc_reload_bbt(docg3); |
| 1901 | return mtd; |
| 1902 | |
| 1903 | nomem3: |
| 1904 | kfree(mtd); |
| 1905 | nomem2: |
| 1906 | kfree(docg3); |
| 1907 | nomem1: |
| 1908 | return ERR_PTR(ret); |
| 1909 | } |
| 1910 | |
| 1911 | /** |
| 1912 | * doc_release_device - Release a docg3 floor |
| 1913 | * @mtd: the device |
| 1914 | */ |
| 1915 | static void doc_release_device(struct mtd_info *mtd) |
| 1916 | { |
| 1917 | struct docg3 *docg3 = mtd->priv; |
| 1918 | |
| 1919 | mtd_device_unregister(mtd); |
| 1920 | kfree(docg3->bbt); |
| 1921 | kfree(docg3); |
| 1922 | kfree(mtd->name); |
| 1923 | kfree(mtd); |
| 1924 | } |
| 1925 | |
| 1926 | /** |
Robert Jarzmik | e4b2a96 | 2011-11-19 16:02:56 +0100 | [diff] [blame] | 1927 | * docg3_resume - Awakens docg3 floor |
| 1928 | * @pdev: platfrom device |
| 1929 | * |
| 1930 | * Returns 0 (always successfull) |
| 1931 | */ |
| 1932 | static int docg3_resume(struct platform_device *pdev) |
| 1933 | { |
| 1934 | int i; |
| 1935 | struct mtd_info **docg3_floors, *mtd; |
| 1936 | struct docg3 *docg3; |
| 1937 | |
| 1938 | docg3_floors = platform_get_drvdata(pdev); |
| 1939 | mtd = docg3_floors[0]; |
| 1940 | docg3 = mtd->priv; |
| 1941 | |
| 1942 | doc_dbg("docg3_resume()\n"); |
| 1943 | for (i = 0; i < 12; i++) |
| 1944 | doc_readb(docg3, DOC_IOSPACE_IPL); |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
| 1948 | /** |
| 1949 | * docg3_suspend - Put in low power mode the docg3 floor |
| 1950 | * @pdev: platform device |
| 1951 | * @state: power state |
| 1952 | * |
| 1953 | * Shuts off most of docg3 circuitery to lower power consumption. |
| 1954 | * |
| 1955 | * Returns 0 if suspend succeeded, -EIO if chip refused suspend |
| 1956 | */ |
| 1957 | static int docg3_suspend(struct platform_device *pdev, pm_message_t state) |
| 1958 | { |
| 1959 | int floor, i; |
| 1960 | struct mtd_info **docg3_floors, *mtd; |
| 1961 | struct docg3 *docg3; |
| 1962 | u8 ctrl, pwr_down; |
| 1963 | |
| 1964 | docg3_floors = platform_get_drvdata(pdev); |
| 1965 | for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) { |
| 1966 | mtd = docg3_floors[floor]; |
| 1967 | if (!mtd) |
| 1968 | continue; |
| 1969 | docg3 = mtd->priv; |
| 1970 | |
| 1971 | doc_writeb(docg3, floor, DOC_DEVICESELECT); |
| 1972 | ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL); |
| 1973 | ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE; |
| 1974 | doc_writeb(docg3, ctrl, DOC_FLASHCONTROL); |
| 1975 | |
| 1976 | for (i = 0; i < 10; i++) { |
| 1977 | usleep_range(3000, 4000); |
| 1978 | pwr_down = doc_register_readb(docg3, DOC_POWERMODE); |
| 1979 | if (pwr_down & DOC_POWERDOWN_READY) |
| 1980 | break; |
| 1981 | } |
| 1982 | if (pwr_down & DOC_POWERDOWN_READY) { |
| 1983 | doc_dbg("docg3_suspend(): floor %d powerdown ok\n", |
| 1984 | floor); |
| 1985 | } else { |
| 1986 | doc_err("docg3_suspend(): floor %d powerdown failed\n", |
| 1987 | floor); |
| 1988 | return -EIO; |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | mtd = docg3_floors[0]; |
| 1993 | docg3 = mtd->priv; |
| 1994 | doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN); |
| 1995 | return 0; |
| 1996 | } |
| 1997 | |
| 1998 | /** |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 1999 | * doc_probe - Probe the IO space for a DiskOnChip G3 chip |
| 2000 | * @pdev: platform device |
| 2001 | * |
| 2002 | * Probes for a G3 chip at the specified IO space in the platform data |
| 2003 | * ressources. The floor 0 must be available. |
| 2004 | * |
| 2005 | * Returns 0 on success, -ENOMEM, -ENXIO on error |
| 2006 | */ |
| 2007 | static int __init docg3_probe(struct platform_device *pdev) |
| 2008 | { |
| 2009 | struct device *dev = &pdev->dev; |
| 2010 | struct mtd_info *mtd; |
| 2011 | struct resource *ress; |
| 2012 | void __iomem *base; |
| 2013 | int ret, floor, found = 0; |
| 2014 | struct mtd_info **docg3_floors; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2015 | |
| 2016 | ret = -ENXIO; |
| 2017 | ress = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2018 | if (!ress) { |
| 2019 | dev_err(dev, "No I/O memory resource defined\n"); |
| 2020 | goto noress; |
| 2021 | } |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2022 | base = ioremap(ress->start, DOC_IOSPACE_SIZE); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2023 | |
| 2024 | ret = -ENOMEM; |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2025 | docg3_floors = kzalloc(sizeof(*docg3_floors) * DOC_MAX_NBFLOORS, |
| 2026 | GFP_KERNEL); |
| 2027 | if (!docg3_floors) |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 2028 | goto nomem1; |
| 2029 | docg3_bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T, |
| 2030 | DOC_ECC_BCH_PRIMPOLY); |
| 2031 | if (!docg3_bch) |
| 2032 | goto nomem2; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2033 | |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2034 | for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) { |
| 2035 | mtd = doc_probe_device(base, floor, dev); |
Dan Carpenter | b49e345 | 2011-11-28 16:53:13 +0300 | [diff] [blame] | 2036 | if (IS_ERR(mtd)) { |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2037 | ret = PTR_ERR(mtd); |
Dan Carpenter | b49e345 | 2011-11-28 16:53:13 +0300 | [diff] [blame] | 2038 | goto err_probe; |
| 2039 | } |
| 2040 | if (!mtd) { |
| 2041 | if (floor == 0) |
| 2042 | goto notfound; |
| 2043 | else |
| 2044 | continue; |
| 2045 | } |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2046 | docg3_floors[floor] = mtd; |
Dan Carpenter | b49e345 | 2011-11-28 16:53:13 +0300 | [diff] [blame] | 2047 | ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL, |
| 2048 | 0); |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2049 | if (ret) |
| 2050 | goto err_probe; |
Dan Carpenter | b49e345 | 2011-11-28 16:53:13 +0300 | [diff] [blame] | 2051 | found++; |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2052 | } |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2053 | |
Robert Jarzmik | 0f769d3 | 2011-11-19 16:02:58 +0100 | [diff] [blame] | 2054 | ret = doc_register_sysfs(pdev, docg3_floors); |
| 2055 | if (ret) |
| 2056 | goto err_probe; |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2057 | if (!found) |
| 2058 | goto notfound; |
| 2059 | |
| 2060 | platform_set_drvdata(pdev, docg3_floors); |
| 2061 | doc_dbg_register(docg3_floors[0]->priv); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2062 | return 0; |
| 2063 | |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2064 | notfound: |
| 2065 | ret = -ENODEV; |
| 2066 | dev_info(dev, "No supported DiskOnChip found\n"); |
| 2067 | err_probe: |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 2068 | free_bch(docg3_bch); |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2069 | for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) |
| 2070 | if (docg3_floors[floor]) |
| 2071 | doc_release_device(docg3_floors[floor]); |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 2072 | nomem2: |
| 2073 | kfree(docg3_floors); |
| 2074 | nomem1: |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2075 | iounmap(base); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2076 | noress: |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2077 | return ret; |
| 2078 | } |
| 2079 | |
| 2080 | /** |
| 2081 | * docg3_release - Release the driver |
| 2082 | * @pdev: the platform device |
| 2083 | * |
| 2084 | * Returns 0 |
| 2085 | */ |
| 2086 | static int __exit docg3_release(struct platform_device *pdev) |
| 2087 | { |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2088 | struct mtd_info **docg3_floors = platform_get_drvdata(pdev); |
| 2089 | struct docg3 *docg3 = docg3_floors[0]->priv; |
| 2090 | void __iomem *base = docg3->base; |
| 2091 | int floor; |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2092 | |
Robert Jarzmik | 0f769d3 | 2011-11-19 16:02:58 +0100 | [diff] [blame] | 2093 | doc_unregister_sysfs(pdev, docg3_floors); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2094 | doc_dbg_unregister(docg3); |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2095 | for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) |
| 2096 | if (docg3_floors[floor]) |
| 2097 | doc_release_device(docg3_floors[floor]); |
| 2098 | |
| 2099 | kfree(docg3_floors); |
Robert Jarzmik | d13d19e | 2011-11-19 16:02:55 +0100 | [diff] [blame] | 2100 | free_bch(docg3_bch); |
Robert Jarzmik | ae9d493 | 2011-11-19 16:02:48 +0100 | [diff] [blame] | 2101 | iounmap(base); |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2102 | return 0; |
| 2103 | } |
| 2104 | |
| 2105 | static struct platform_driver g3_driver = { |
| 2106 | .driver = { |
| 2107 | .name = "docg3", |
| 2108 | .owner = THIS_MODULE, |
| 2109 | }, |
Robert Jarzmik | e4b2a96 | 2011-11-19 16:02:56 +0100 | [diff] [blame] | 2110 | .suspend = docg3_suspend, |
| 2111 | .resume = docg3_resume, |
Robert Jarzmik | efa2ca7 | 2011-10-05 15:22:34 +0200 | [diff] [blame] | 2112 | .remove = __exit_p(docg3_release), |
| 2113 | }; |
| 2114 | |
| 2115 | static int __init docg3_init(void) |
| 2116 | { |
| 2117 | return platform_driver_probe(&g3_driver, docg3_probe); |
| 2118 | } |
| 2119 | module_init(docg3_init); |
| 2120 | |
| 2121 | |
| 2122 | static void __exit docg3_exit(void) |
| 2123 | { |
| 2124 | platform_driver_unregister(&g3_driver); |
| 2125 | } |
| 2126 | module_exit(docg3_exit); |
| 2127 | |
| 2128 | MODULE_LICENSE("GPL"); |
| 2129 | MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>"); |
| 2130 | MODULE_DESCRIPTION("MTD driver for DiskOnChip G3"); |