Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/mtd/nand.c |
| 3 | * |
| 4 | * Overview: |
| 5 | * This is the generic MTD driver for NAND flash devices. It should be |
| 6 | * capable of working with almost all NAND chips currently available. |
| 7 | * Basic support for AG-AND chips is provided. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Additional technical information is available on |
maximilian attems | 8b2b403 | 2007-07-28 13:07:16 +0200 | [diff] [blame] | 10 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 13 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 15 | * Credits: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 16 | * David Woodhouse for adding multichip support |
| 17 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 19 | * rework for 2K page size chips |
| 20 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 21 | * TODO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * Enable cached programming for 2k page size chips |
| 23 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
| 24 | * if we have HW ecc support. |
| 25 | * The AG-AND chips have nice features for speed improvement, |
| 26 | * which are not supported yet. Read / program 4 pages in one go. |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 27 | * BBT table is not serialized, has to be fixed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | * This program is free software; you can redistribute it and/or modify |
| 30 | * it under the terms of the GNU General Public License version 2 as |
| 31 | * published by the Free Software Foundation. |
| 32 | * |
| 33 | */ |
| 34 | |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 35 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/delay.h> |
| 37 | #include <linux/errno.h> |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 38 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/sched.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/types.h> |
| 42 | #include <linux/mtd/mtd.h> |
| 43 | #include <linux/mtd/nand.h> |
| 44 | #include <linux/mtd/nand_ecc.h> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 45 | #include <linux/mtd/nand_bch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/interrupt.h> |
| 47 | #include <linux/bitops.h> |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 48 | #include <linux/leds.h> |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 49 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #ifdef CONFIG_MTD_PARTITIONS |
| 52 | #include <linux/mtd/partitions.h> |
| 53 | #endif |
| 54 | |
| 55 | /* Define default oob placement schemes for large and small page devices */ |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 56 | static struct nand_ecclayout nand_oob_8 = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | .eccbytes = 3, |
| 58 | .eccpos = {0, 1, 2}, |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 59 | .oobfree = { |
| 60 | {.offset = 3, |
| 61 | .length = 2}, |
| 62 | {.offset = 6, |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 63 | .length = 2} } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 66 | static struct nand_ecclayout nand_oob_16 = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | .eccbytes = 6, |
| 68 | .eccpos = {0, 1, 2, 3, 6, 7}, |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 69 | .oobfree = { |
| 70 | {.offset = 8, |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 71 | . length = 8} } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 74 | static struct nand_ecclayout nand_oob_64 = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | .eccbytes = 24, |
| 76 | .eccpos = { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 77 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 78 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 79 | 56, 57, 58, 59, 60, 61, 62, 63}, |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 80 | .oobfree = { |
| 81 | {.offset = 2, |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 82 | .length = 38} } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 85 | static struct nand_ecclayout nand_oob_128 = { |
| 86 | .eccbytes = 48, |
| 87 | .eccpos = { |
| 88 | 80, 81, 82, 83, 84, 85, 86, 87, |
| 89 | 88, 89, 90, 91, 92, 93, 94, 95, |
| 90 | 96, 97, 98, 99, 100, 101, 102, 103, |
| 91 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 92 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 93 | 120, 121, 122, 123, 124, 125, 126, 127}, |
| 94 | .oobfree = { |
| 95 | {.offset = 2, |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 96 | .length = 78} } |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 99 | static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 100 | int new_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 102 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 103 | struct mtd_oob_ops *ops); |
| 104 | |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 105 | /* |
Joe Perches | 8e87d78 | 2008-02-03 17:22:34 +0200 | [diff] [blame] | 106 | * For devices which display every fart in the system on a separate LED. Is |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 107 | * compiled away when LED support is disabled. |
| 108 | */ |
| 109 | DEFINE_LED_TRIGGER(nand_led_trigger); |
| 110 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 111 | static int check_offs_len(struct mtd_info *mtd, |
| 112 | loff_t ofs, uint64_t len) |
| 113 | { |
| 114 | struct nand_chip *chip = mtd->priv; |
| 115 | int ret = 0; |
| 116 | |
| 117 | /* Start address must align on block boundary */ |
| 118 | if (ofs & ((1 << chip->phys_erase_shift) - 1)) { |
| 119 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__); |
| 120 | ret = -EINVAL; |
| 121 | } |
| 122 | |
| 123 | /* Length must align on block boundary */ |
| 124 | if (len & ((1 << chip->phys_erase_shift) - 1)) { |
| 125 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n", |
| 126 | __func__); |
| 127 | ret = -EINVAL; |
| 128 | } |
| 129 | |
| 130 | /* Do not allow past end of device */ |
| 131 | if (ofs + len > mtd->size) { |
| 132 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Past end of device\n", |
| 133 | __func__); |
| 134 | ret = -EINVAL; |
| 135 | } |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /** |
| 141 | * nand_release_device - [GENERIC] release chip |
| 142 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 143 | * |
| 144 | * Deselect, release chip lock and wake up anyone waiting on the device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 146 | static void nand_release_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 148 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | /* De-select the NAND device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 151 | chip->select_chip(mtd, -1); |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 152 | |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 153 | /* Release the controller and the chip */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 154 | spin_lock(&chip->controller->lock); |
| 155 | chip->controller->active = NULL; |
| 156 | chip->state = FL_READY; |
| 157 | wake_up(&chip->controller->wq); |
| 158 | spin_unlock(&chip->controller->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * nand_read_byte - [DEFAULT] read one byte from the chip |
| 163 | * @mtd: MTD device structure |
| 164 | * |
| 165 | * Default read function for 8bit buswith |
| 166 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 167 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 169 | struct nand_chip *chip = mtd->priv; |
| 170 | return readb(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip |
| 175 | * @mtd: MTD device structure |
| 176 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 177 | * Default read function for 16bit buswith with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | * endianess conversion |
| 179 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 180 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 182 | struct nand_chip *chip = mtd->priv; |
| 183 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | * nand_read_word - [DEFAULT] read one word from the chip |
| 188 | * @mtd: MTD device structure |
| 189 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 190 | * Default read function for 16bit buswith without |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | * endianess conversion |
| 192 | */ |
| 193 | static u16 nand_read_word(struct mtd_info *mtd) |
| 194 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 195 | struct nand_chip *chip = mtd->priv; |
| 196 | return readw(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | * nand_select_chip - [DEFAULT] control CE line |
| 201 | * @mtd: MTD device structure |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 202 | * @chipnr: chipnumber to select, -1 for deselect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | * |
| 204 | * Default select function for 1 chip devices. |
| 205 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 206 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 208 | struct nand_chip *chip = mtd->priv; |
| 209 | |
| 210 | switch (chipnr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | case -1: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 212 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | break; |
| 214 | case 0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | break; |
| 216 | |
| 217 | default: |
| 218 | BUG(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * nand_write_buf - [DEFAULT] write buffer to chip |
| 224 | * @mtd: MTD device structure |
| 225 | * @buf: data buffer |
| 226 | * @len: number of bytes to write |
| 227 | * |
| 228 | * Default write function for 8bit buswith |
| 229 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 230 | static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
| 232 | int i; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 233 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 235 | for (i = 0; i < len; i++) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 236 | writeb(buf[i], chip->IO_ADDR_W); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 240 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | * @mtd: MTD device structure |
| 242 | * @buf: buffer to store date |
| 243 | * @len: number of bytes to read |
| 244 | * |
| 245 | * Default read function for 8bit buswith |
| 246 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 247 | static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
| 249 | int i; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 250 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 252 | for (i = 0; i < len; i++) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 253 | buf[i] = readb(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 257 | * nand_verify_buf - [DEFAULT] Verify chip data against buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | * @mtd: MTD device structure |
| 259 | * @buf: buffer containing the data to compare |
| 260 | * @len: number of bytes to compare |
| 261 | * |
| 262 | * Default verify function for 8bit buswith |
| 263 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 264 | static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
| 266 | int i; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 267 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 269 | for (i = 0; i < len; i++) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 270 | if (buf[i] != readb(chip->IO_ADDR_R)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
| 277 | * @mtd: MTD device structure |
| 278 | * @buf: data buffer |
| 279 | * @len: number of bytes to write |
| 280 | * |
| 281 | * Default write function for 16bit buswith |
| 282 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 283 | static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
| 285 | int i; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 286 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | u16 *p = (u16 *) buf; |
| 288 | len >>= 1; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 289 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 290 | for (i = 0; i < len; i++) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 291 | writew(p[i], chip->IO_ADDR_W); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 296 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | * @mtd: MTD device structure |
| 298 | * @buf: buffer to store date |
| 299 | * @len: number of bytes to read |
| 300 | * |
| 301 | * Default read function for 16bit buswith |
| 302 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 303 | static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
| 305 | int i; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 306 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | u16 *p = (u16 *) buf; |
| 308 | len >>= 1; |
| 309 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 310 | for (i = 0; i < len; i++) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 311 | p[i] = readw(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 315 | * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | * @mtd: MTD device structure |
| 317 | * @buf: buffer containing the data to compare |
| 318 | * @len: number of bytes to compare |
| 319 | * |
| 320 | * Default verify function for 16bit buswith |
| 321 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 322 | static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | int i; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 325 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | u16 *p = (u16 *) buf; |
| 327 | len >>= 1; |
| 328 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 329 | for (i = 0; i < len; i++) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 330 | if (p[i] != readw(chip->IO_ADDR_R)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return -EFAULT; |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
| 338 | * @mtd: MTD device structure |
| 339 | * @ofs: offset from device start |
| 340 | * @getchip: 0, if the chip is already selected |
| 341 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 342 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | */ |
| 344 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip) |
| 345 | { |
| 346 | int page, chipnr, res = 0; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 347 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | u16 bad; |
| 349 | |
Brian Norris | 30fe811 | 2010-06-23 13:36:02 -0700 | [diff] [blame] | 350 | if (chip->options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 351 | ofs += mtd->erasesize - mtd->writesize; |
| 352 | |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 353 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (getchip) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 356 | chipnr = (int)(ofs >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 358 | nand_get_device(chip, mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | /* Select the NAND device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 361 | chip->select_chip(mtd, chipnr); |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 364 | if (chip->options & NAND_BUSWIDTH_16) { |
| 365 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE, |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 366 | page); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 367 | bad = cpu_to_le16(chip->read_word(mtd)); |
| 368 | if (chip->badblockpos & 0x1) |
Vitaly Wool | 49196f3 | 2005-11-02 16:54:46 +0000 | [diff] [blame] | 369 | bad >>= 8; |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 370 | else |
| 371 | bad &= 0xFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } else { |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 373 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page); |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 374 | bad = chip->read_byte(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 376 | |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 377 | if (likely(chip->badblockbits == 8)) |
| 378 | res = bad != 0xFF; |
| 379 | else |
| 380 | res = hweight8(bad) < chip->badblockbits; |
| 381 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 382 | if (getchip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | nand_release_device(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return res; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * nand_default_block_markbad - [DEFAULT] mark a block bad |
| 390 | * @mtd: MTD device structure |
| 391 | * @ofs: offset from device start |
| 392 | * |
| 393 | * This is the default implementation, which can be overridden by |
| 394 | * a hardware specific driver. |
| 395 | */ |
| 396 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 397 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 398 | struct nand_chip *chip = mtd->priv; |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 399 | uint8_t buf[2] = { 0, 0 }; |
Brian Norris | 02ed70b | 2010-07-21 16:53:47 -0700 | [diff] [blame] | 400 | int block, ret, i = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 401 | |
Brian Norris | 30fe811 | 2010-06-23 13:36:02 -0700 | [diff] [blame] | 402 | if (chip->options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 403 | ofs += mtd->erasesize - mtd->writesize; |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | /* Get block number */ |
Andre Renaud | 4226b51 | 2007-04-17 13:50:59 -0400 | [diff] [blame] | 406 | block = (int)(ofs >> chip->bbt_erase_shift); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 407 | if (chip->bbt) |
| 408 | chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
| 410 | /* Do we have a flash based bad block table ? */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 411 | if (chip->options & NAND_USE_FLASH_BBT) |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 412 | ret = nand_update_bbt(mtd, ofs); |
| 413 | else { |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 414 | nand_get_device(chip, mtd, FL_WRITING); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 415 | |
Brian Norris | 02ed70b | 2010-07-21 16:53:47 -0700 | [diff] [blame] | 416 | /* Write to first two pages and to byte 1 and 6 if necessary. |
| 417 | * If we write to more than one location, the first error |
| 418 | * encountered quits the procedure. We write two bytes per |
| 419 | * location, so we dont have to mess with 16 bit access. |
| 420 | */ |
| 421 | do { |
| 422 | chip->ops.len = chip->ops.ooblen = 2; |
| 423 | chip->ops.datbuf = NULL; |
| 424 | chip->ops.oobbuf = buf; |
| 425 | chip->ops.ooboffs = chip->badblockpos & ~0x01; |
| 426 | |
| 427 | ret = nand_do_write_oob(mtd, ofs, &chip->ops); |
| 428 | |
| 429 | if (!ret && (chip->options & NAND_BBT_SCANBYTE1AND6)) { |
| 430 | chip->ops.ooboffs = NAND_SMALL_BADBLOCK_POS |
| 431 | & ~0x01; |
| 432 | ret = nand_do_write_oob(mtd, ofs, &chip->ops); |
| 433 | } |
| 434 | i++; |
| 435 | ofs += mtd->writesize; |
| 436 | } while (!ret && (chip->options & NAND_BBT_SCAN2NDPAGE) && |
| 437 | i < 2); |
| 438 | |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 439 | nand_release_device(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 440 | } |
| 441 | if (!ret) |
| 442 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 443 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 444 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 447 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | * nand_check_wp - [GENERIC] check if the chip is write protected |
| 449 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 450 | * Check, if the device is write protected |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 452 | * The function expects, that the device is already selected |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 454 | static int nand_check_wp(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 456 | struct nand_chip *chip = mtd->priv; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 457 | |
| 458 | /* broken xD cards report WP despite being writable */ |
| 459 | if (chip->options & NAND_BROKEN_XD) |
| 460 | return 0; |
| 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | /* Check the WP bit */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 463 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 464 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
| 468 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 469 | * @mtd: MTD device structure |
| 470 | * @ofs: offset from device start |
| 471 | * @getchip: 0, if the chip is already selected |
| 472 | * @allowbbt: 1, if its allowed to access the bbt area |
| 473 | * |
| 474 | * Check, if the block is bad. Either by reading the bad block table or |
| 475 | * calling of the scan function. |
| 476 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 477 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, |
| 478 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 480 | struct nand_chip *chip = mtd->priv; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 481 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 482 | if (!chip->bbt) |
| 483 | return chip->block_bad(mtd, ofs, getchip); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | /* Return info from the table */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 486 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 489 | /** |
| 490 | * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 491 | * @mtd: MTD device structure |
| 492 | * @timeo: Timeout |
| 493 | * |
| 494 | * Helper function for nand_wait_ready used when needing to wait in interrupt |
| 495 | * context. |
| 496 | */ |
| 497 | static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo) |
| 498 | { |
| 499 | struct nand_chip *chip = mtd->priv; |
| 500 | int i; |
| 501 | |
| 502 | /* Wait for the device to get ready */ |
| 503 | for (i = 0; i < timeo; i++) { |
| 504 | if (chip->dev_ready(mtd)) |
| 505 | break; |
| 506 | touch_softlockup_watchdog(); |
| 507 | mdelay(1); |
| 508 | } |
| 509 | } |
| 510 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 511 | /* |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 512 | * Wait for the ready pin, after a command |
| 513 | * The timeout is catched later. |
| 514 | */ |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 515 | void nand_wait_ready(struct mtd_info *mtd) |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 516 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 517 | struct nand_chip *chip = mtd->priv; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 518 | unsigned long timeo = jiffies + 2; |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 519 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 520 | /* 400ms timeout */ |
| 521 | if (in_interrupt() || oops_in_progress) |
| 522 | return panic_nand_wait_ready(mtd, 400); |
| 523 | |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 524 | led_trigger_event(nand_led_trigger, LED_FULL); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 525 | /* wait until command is processed or timeout occures */ |
| 526 | do { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 527 | if (chip->dev_ready(mtd)) |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 528 | break; |
Ingo Molnar | 8446f1d | 2005-09-06 15:16:27 -0700 | [diff] [blame] | 529 | touch_softlockup_watchdog(); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 530 | } while (time_before(jiffies, timeo)); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 531 | led_trigger_event(nand_led_trigger, LED_OFF); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 532 | } |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 533 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | /** |
| 536 | * nand_command - [DEFAULT] Send command to NAND device |
| 537 | * @mtd: MTD device structure |
| 538 | * @command: the command to be sent |
| 539 | * @column: the column address for this command, -1 if none |
| 540 | * @page_addr: the page address for this command, -1 if none |
| 541 | * |
| 542 | * Send command to NAND device. This function is used for small page |
| 543 | * devices (256/512 Bytes per page) |
| 544 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 545 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 546 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 548 | register struct nand_chip *chip = mtd->priv; |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 549 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | /* |
| 552 | * Write out the command to the device. |
| 553 | */ |
| 554 | if (command == NAND_CMD_SEQIN) { |
| 555 | int readcmd; |
| 556 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 557 | if (column >= mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 559 | column -= mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | readcmd = NAND_CMD_READOOB; |
| 561 | } else if (column < 256) { |
| 562 | /* First 256 bytes --> READ0 */ |
| 563 | readcmd = NAND_CMD_READ0; |
| 564 | } else { |
| 565 | column -= 256; |
| 566 | readcmd = NAND_CMD_READ1; |
| 567 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 568 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 569 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 571 | chip->cmd_ctrl(mtd, command, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 573 | /* |
| 574 | * Address cycle, when necessary |
| 575 | */ |
| 576 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 577 | /* Serially input address */ |
| 578 | if (column != -1) { |
| 579 | /* Adjust columns for 16 bit buswidth */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 580 | if (chip->options & NAND_BUSWIDTH_16) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 581 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 582 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 583 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 585 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 586 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 587 | ctrl &= ~NAND_CTRL_CHANGE; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 588 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 589 | /* One more address cycle for devices > 32MiB */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 590 | if (chip->chipsize > (32 << 20)) |
| 591 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 592 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 593 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 594 | |
| 595 | /* |
| 596 | * program and erase have their own busy handlers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | * status and sequential in needs no delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 598 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | case NAND_CMD_PAGEPROG: |
| 602 | case NAND_CMD_ERASE1: |
| 603 | case NAND_CMD_ERASE2: |
| 604 | case NAND_CMD_SEQIN: |
| 605 | case NAND_CMD_STATUS: |
| 606 | return; |
| 607 | |
| 608 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 609 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 611 | udelay(chip->chip_delay); |
| 612 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 613 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 614 | chip->cmd_ctrl(mtd, |
| 615 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 616 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) |
| 617 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | return; |
| 619 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 620 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 622 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | * If we don't have access to the busy pin, we apply the given |
| 624 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 625 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 626 | if (!chip->dev_ready) { |
| 627 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | /* Apply this short delay always to ensure that we do wait tWB in |
| 632 | * any case on any machine. */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 633 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 634 | |
| 635 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /** |
| 639 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
| 640 | * @mtd: MTD device structure |
| 641 | * @command: the command to be sent |
| 642 | * @column: the column address for this command, -1 if none |
| 643 | * @page_addr: the page address for this command, -1 if none |
| 644 | * |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 645 | * Send command to NAND device. This is the version for the new large page |
| 646 | * devices We dont have the separate regions as we have in the small page |
| 647 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 649 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 650 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 652 | register struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | /* Emulate NAND_CMD_READOOB */ |
| 655 | if (command == NAND_CMD_READOOB) { |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 656 | column += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | command = NAND_CMD_READ0; |
| 658 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 659 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 660 | /* Command latch cycle */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 661 | chip->cmd_ctrl(mtd, command & 0xff, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 662 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | if (column != -1 || page_addr != -1) { |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 665 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | /* Serially input address */ |
| 668 | if (column != -1) { |
| 669 | /* Adjust columns for 16 bit buswidth */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 670 | if (chip->options & NAND_BUSWIDTH_16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 672 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 673 | ctrl &= ~NAND_CTRL_CHANGE; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 674 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 677 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 678 | chip->cmd_ctrl(mtd, page_addr >> 8, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 679 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | /* One more address cycle for devices > 128MiB */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 681 | if (chip->chipsize > (128 << 20)) |
| 682 | chip->cmd_ctrl(mtd, page_addr >> 16, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 683 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 686 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 687 | |
| 688 | /* |
| 689 | * program and erase have their own busy handlers |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 690 | * status, sequential in, and deplete1 need no delay |
| 691 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | case NAND_CMD_CACHEDPROG: |
| 695 | case NAND_CMD_PAGEPROG: |
| 696 | case NAND_CMD_ERASE1: |
| 697 | case NAND_CMD_ERASE2: |
| 698 | case NAND_CMD_SEQIN: |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 699 | case NAND_CMD_RNDIN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | case NAND_CMD_STATUS: |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 701 | case NAND_CMD_DEPLETE1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | return; |
| 703 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 704 | /* |
| 705 | * read error status commands require only a short delay |
| 706 | */ |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 707 | case NAND_CMD_STATUS_ERROR: |
| 708 | case NAND_CMD_STATUS_ERROR0: |
| 709 | case NAND_CMD_STATUS_ERROR1: |
| 710 | case NAND_CMD_STATUS_ERROR2: |
| 711 | case NAND_CMD_STATUS_ERROR3: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 712 | udelay(chip->chip_delay); |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 713 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
| 715 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 716 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 718 | udelay(chip->chip_delay); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 719 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 720 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 721 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 722 | NAND_NCE | NAND_CTRL_CHANGE); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 723 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) |
| 724 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | return; |
| 726 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 727 | case NAND_CMD_RNDOUT: |
| 728 | /* No ready / busy check necessary */ |
| 729 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 730 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 731 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 732 | NAND_NCE | NAND_CTRL_CHANGE); |
| 733 | return; |
| 734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | case NAND_CMD_READ0: |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 736 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 737 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 738 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 739 | NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 740 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 741 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 743 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | * If we don't have access to the busy pin, we apply the given |
| 745 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 746 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 747 | if (!chip->dev_ready) { |
| 748 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 750 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | /* Apply this short delay always to ensure that we do wait tWB in |
| 754 | * any case on any machine. */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 755 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 756 | |
| 757 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 761 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
| 762 | * @chip: the nand chip descriptor |
| 763 | * @mtd: MTD device structure |
| 764 | * @new_state: the state which is requested |
| 765 | * |
| 766 | * Used when in panic, no locks are taken. |
| 767 | */ |
| 768 | static void panic_nand_get_device(struct nand_chip *chip, |
| 769 | struct mtd_info *mtd, int new_state) |
| 770 | { |
| 771 | /* Hardware controller shared among independend devices */ |
| 772 | chip->controller->active = chip; |
| 773 | chip->state = new_state; |
| 774 | } |
| 775 | |
| 776 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | * nand_get_device - [GENERIC] Get chip for selected access |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 778 | * @chip: the nand chip descriptor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 780 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | * |
| 782 | * Get the device and lock it for exclusive access |
| 783 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 784 | static int |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 785 | nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 787 | spinlock_t *lock = &chip->controller->lock; |
| 788 | wait_queue_head_t *wq = &chip->controller->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 789 | DECLARE_WAITQUEUE(wait, current); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 790 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 791 | spin_lock(lock); |
| 792 | |
vimal singh | b8b3ee9 | 2009-07-09 20:41:22 +0530 | [diff] [blame] | 793 | /* Hardware controller shared among independent devices */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 794 | if (!chip->controller->active) |
| 795 | chip->controller->active = chip; |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 796 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 797 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 798 | chip->state = new_state; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 799 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 800 | return 0; |
| 801 | } |
| 802 | if (new_state == FL_PM_SUSPENDED) { |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 803 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
| 804 | chip->state = FL_PM_SUSPENDED; |
| 805 | spin_unlock(lock); |
| 806 | return 0; |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 807 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 808 | } |
| 809 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 810 | add_wait_queue(wq, &wait); |
| 811 | spin_unlock(lock); |
| 812 | schedule(); |
| 813 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | goto retry; |
| 815 | } |
| 816 | |
| 817 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 818 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 819 | * @mtd: MTD device structure |
| 820 | * @chip: NAND chip structure |
| 821 | * @timeo: Timeout |
| 822 | * |
| 823 | * Wait for command done. This is a helper function for nand_wait used when |
| 824 | * we are in interrupt context. May happen when in panic and trying to write |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 825 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 826 | */ |
| 827 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 828 | unsigned long timeo) |
| 829 | { |
| 830 | int i; |
| 831 | for (i = 0; i < timeo; i++) { |
| 832 | if (chip->dev_ready) { |
| 833 | if (chip->dev_ready(mtd)) |
| 834 | break; |
| 835 | } else { |
| 836 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 837 | break; |
| 838 | } |
| 839 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 840 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | * nand_wait - [DEFAULT] wait until the command is done |
| 845 | * @mtd: MTD device structure |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 846 | * @chip: NAND chip structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | * |
| 848 | * Wait for command done. This applies to erase and program only |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 849 | * Erase can take up to 400ms and program up to 20ms according to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | * general NAND and SmartMedia specs |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 851 | */ |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 852 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | { |
| 854 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 855 | unsigned long timeo = jiffies; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 856 | int status, state = chip->state; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | if (state == FL_ERASING) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 859 | timeo += (HZ * 400) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | else |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 861 | timeo += (HZ * 20) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 863 | led_trigger_event(nand_led_trigger, LED_FULL); |
| 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | /* Apply this short delay always to ensure that we do wait tWB in |
| 866 | * any case on any machine. */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 867 | ndelay(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 869 | if ((state == FL_ERASING) && (chip->options & NAND_IS_AND)) |
| 870 | chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 871 | else |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 872 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 874 | if (in_interrupt() || oops_in_progress) |
| 875 | panic_nand_wait(mtd, chip, timeo); |
| 876 | else { |
| 877 | while (time_before(jiffies, timeo)) { |
| 878 | if (chip->dev_ready) { |
| 879 | if (chip->dev_ready(mtd)) |
| 880 | break; |
| 881 | } else { |
| 882 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 883 | break; |
| 884 | } |
| 885 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 888 | led_trigger_event(nand_led_trigger, LED_OFF); |
| 889 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 890 | status = (int)chip->read_byte(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | return status; |
| 892 | } |
| 893 | |
| 894 | /** |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 895 | * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 896 | * |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 897 | * @mtd: mtd info |
| 898 | * @ofs: offset to start unlock from |
| 899 | * @len: length to unlock |
| 900 | * @invert: when = 0, unlock the range of blocks within the lower and |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 901 | * upper boundary address |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 902 | * when = 1, unlock the range of blocks outside the boundaries |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 903 | * of the lower and upper boundary address |
| 904 | * |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 905 | * return - unlock status |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 906 | */ |
| 907 | static int __nand_unlock(struct mtd_info *mtd, loff_t ofs, |
| 908 | uint64_t len, int invert) |
| 909 | { |
| 910 | int ret = 0; |
| 911 | int status, page; |
| 912 | struct nand_chip *chip = mtd->priv; |
| 913 | |
| 914 | /* Submit address of first page to unlock */ |
| 915 | page = ofs >> chip->page_shift; |
| 916 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask); |
| 917 | |
| 918 | /* Submit address of last page to unlock */ |
| 919 | page = (ofs + len) >> chip->page_shift; |
| 920 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, |
| 921 | (page | invert) & chip->pagemask); |
| 922 | |
| 923 | /* Call wait ready function */ |
| 924 | status = chip->waitfunc(mtd, chip); |
| 925 | udelay(1000); |
| 926 | /* See if device thinks it succeeded */ |
| 927 | if (status & 0x01) { |
| 928 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n", |
| 929 | __func__, status); |
| 930 | ret = -EIO; |
| 931 | } |
| 932 | |
| 933 | return ret; |
| 934 | } |
| 935 | |
| 936 | /** |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 937 | * nand_unlock - [REPLACEABLE] unlocks specified locked blocks |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 938 | * |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 939 | * @mtd: mtd info |
| 940 | * @ofs: offset to start unlock from |
| 941 | * @len: length to unlock |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 942 | * |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 943 | * return - unlock status |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 944 | */ |
| 945 | int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 946 | { |
| 947 | int ret = 0; |
| 948 | int chipnr; |
| 949 | struct nand_chip *chip = mtd->priv; |
| 950 | |
| 951 | DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n", |
| 952 | __func__, (unsigned long long)ofs, len); |
| 953 | |
| 954 | if (check_offs_len(mtd, ofs, len)) |
| 955 | ret = -EINVAL; |
| 956 | |
| 957 | /* Align to last block address if size addresses end of the device */ |
| 958 | if (ofs + len == mtd->size) |
| 959 | len -= mtd->erasesize; |
| 960 | |
| 961 | nand_get_device(chip, mtd, FL_UNLOCKING); |
| 962 | |
| 963 | /* Shift to get chip number */ |
| 964 | chipnr = ofs >> chip->chip_shift; |
| 965 | |
| 966 | chip->select_chip(mtd, chipnr); |
| 967 | |
| 968 | /* Check, if it is write protected */ |
| 969 | if (nand_check_wp(mtd)) { |
| 970 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n", |
| 971 | __func__); |
| 972 | ret = -EIO; |
| 973 | goto out; |
| 974 | } |
| 975 | |
| 976 | ret = __nand_unlock(mtd, ofs, len, 0); |
| 977 | |
| 978 | out: |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 979 | nand_release_device(mtd); |
| 980 | |
| 981 | return ret; |
| 982 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 983 | EXPORT_SYMBOL(nand_unlock); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 984 | |
| 985 | /** |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 986 | * nand_lock - [REPLACEABLE] locks all blocks present in the device |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 987 | * |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 988 | * @mtd: mtd info |
| 989 | * @ofs: offset to start unlock from |
| 990 | * @len: length to unlock |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 991 | * |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 992 | * return - lock status |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 993 | * |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 994 | * This feature is not supported in many NAND parts. 'Micron' NAND parts |
| 995 | * do have this feature, but it allows only to lock all blocks, not for |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 996 | * specified range for block. |
| 997 | * |
| 998 | * Implementing 'lock' feature by making use of 'unlock', for now. |
| 999 | */ |
| 1000 | int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1001 | { |
| 1002 | int ret = 0; |
| 1003 | int chipnr, status, page; |
| 1004 | struct nand_chip *chip = mtd->priv; |
| 1005 | |
| 1006 | DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n", |
| 1007 | __func__, (unsigned long long)ofs, len); |
| 1008 | |
| 1009 | if (check_offs_len(mtd, ofs, len)) |
| 1010 | ret = -EINVAL; |
| 1011 | |
| 1012 | nand_get_device(chip, mtd, FL_LOCKING); |
| 1013 | |
| 1014 | /* Shift to get chip number */ |
| 1015 | chipnr = ofs >> chip->chip_shift; |
| 1016 | |
| 1017 | chip->select_chip(mtd, chipnr); |
| 1018 | |
| 1019 | /* Check, if it is write protected */ |
| 1020 | if (nand_check_wp(mtd)) { |
| 1021 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n", |
| 1022 | __func__); |
| 1023 | status = MTD_ERASE_FAILED; |
| 1024 | ret = -EIO; |
| 1025 | goto out; |
| 1026 | } |
| 1027 | |
| 1028 | /* Submit address of first page to lock */ |
| 1029 | page = ofs >> chip->page_shift; |
| 1030 | chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask); |
| 1031 | |
| 1032 | /* Call wait ready function */ |
| 1033 | status = chip->waitfunc(mtd, chip); |
| 1034 | udelay(1000); |
| 1035 | /* See if device thinks it succeeded */ |
| 1036 | if (status & 0x01) { |
| 1037 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n", |
| 1038 | __func__, status); |
| 1039 | ret = -EIO; |
| 1040 | goto out; |
| 1041 | } |
| 1042 | |
| 1043 | ret = __nand_unlock(mtd, ofs, len, 0x1); |
| 1044 | |
| 1045 | out: |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1046 | nand_release_device(mtd); |
| 1047 | |
| 1048 | return ret; |
| 1049 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1050 | EXPORT_SYMBOL(nand_lock); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1051 | |
| 1052 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1053 | * nand_read_page_raw - [Intern] read raw page data without ecc |
| 1054 | * @mtd: mtd info structure |
| 1055 | * @chip: nand chip info structure |
| 1056 | * @buf: buffer to store read data |
Jaswinder Singh Rajput | 58475fb | 2009-09-24 13:04:53 +0100 | [diff] [blame] | 1057 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1058 | * |
| 1059 | * Not for syndrome calculating ecc controllers, which use a special oob layout |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1060 | */ |
| 1061 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 1062 | uint8_t *buf, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1063 | { |
| 1064 | chip->read_buf(mtd, buf, mtd->writesize); |
| 1065 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | /** |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1070 | * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc |
| 1071 | * @mtd: mtd info structure |
| 1072 | * @chip: nand chip info structure |
| 1073 | * @buf: buffer to store read data |
Jaswinder Singh Rajput | 58475fb | 2009-09-24 13:04:53 +0100 | [diff] [blame] | 1074 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1075 | * |
| 1076 | * We need a special oob layout and handling even when OOB isn't used. |
| 1077 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1078 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
| 1079 | struct nand_chip *chip, |
| 1080 | uint8_t *buf, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1081 | { |
| 1082 | int eccsize = chip->ecc.size; |
| 1083 | int eccbytes = chip->ecc.bytes; |
| 1084 | uint8_t *oob = chip->oob_poi; |
| 1085 | int steps, size; |
| 1086 | |
| 1087 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 1088 | chip->read_buf(mtd, buf, eccsize); |
| 1089 | buf += eccsize; |
| 1090 | |
| 1091 | if (chip->ecc.prepad) { |
| 1092 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1093 | oob += chip->ecc.prepad; |
| 1094 | } |
| 1095 | |
| 1096 | chip->read_buf(mtd, oob, eccbytes); |
| 1097 | oob += eccbytes; |
| 1098 | |
| 1099 | if (chip->ecc.postpad) { |
| 1100 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1101 | oob += chip->ecc.postpad; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 1106 | if (size) |
| 1107 | chip->read_buf(mtd, oob, size); |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | /** |
Artem Bityutskiy | d29ebdb | 2006-10-19 16:04:02 +0300 | [diff] [blame] | 1113 | * nand_read_page_swecc - [REPLACABLE] software ecc based page read function |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1114 | * @mtd: mtd info structure |
| 1115 | * @chip: nand chip info structure |
| 1116 | * @buf: buffer to store read data |
Jaswinder Singh Rajput | 58475fb | 2009-09-24 13:04:53 +0100 | [diff] [blame] | 1117 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1118 | */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1119 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 1120 | uint8_t *buf, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1122 | int i, eccsize = chip->ecc.size; |
| 1123 | int eccbytes = chip->ecc.bytes; |
| 1124 | int eccsteps = chip->ecc.steps; |
| 1125 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1126 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1127 | uint8_t *ecc_code = chip->buffers->ecccode; |
Ben Dooks | 8b099a3 | 2007-05-28 19:17:54 +0100 | [diff] [blame] | 1128 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1129 | |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 1130 | chip->ecc.read_page_raw(mtd, chip, buf, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1131 | |
| 1132 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1133 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1134 | |
| 1135 | for (i = 0; i < chip->ecc.total; i++) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1136 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1137 | |
| 1138 | eccsteps = chip->ecc.steps; |
| 1139 | p = buf; |
| 1140 | |
| 1141 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1142 | int stat; |
| 1143 | |
| 1144 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Matt Reimer | c32b8dc | 2007-10-17 14:33:23 -0700 | [diff] [blame] | 1145 | if (stat < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1146 | mtd->ecc_stats.failed++; |
| 1147 | else |
| 1148 | mtd->ecc_stats.corrected += stat; |
| 1149 | } |
| 1150 | return 0; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 1151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | /** |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1154 | * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function |
| 1155 | * @mtd: mtd info structure |
| 1156 | * @chip: nand chip info structure |
Alexey Korolev | 17c1d2be | 2008-08-20 22:32:08 +0100 | [diff] [blame] | 1157 | * @data_offs: offset of requested data within the page |
| 1158 | * @readlen: data length |
| 1159 | * @bufpoi: buffer to store read data |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1160 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1161 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
| 1162 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1163 | { |
| 1164 | int start_step, end_step, num_steps; |
| 1165 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1166 | uint8_t *p; |
| 1167 | int data_col_addr, i, gaps = 0; |
| 1168 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 1169 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1170 | int index = 0; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1171 | |
| 1172 | /* Column address wihin the page aligned to ECC size (256bytes). */ |
| 1173 | start_step = data_offs / chip->ecc.size; |
| 1174 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 1175 | num_steps = end_step - start_step + 1; |
| 1176 | |
| 1177 | /* Data size aligned to ECC ecc.size*/ |
| 1178 | datafrag_len = num_steps * chip->ecc.size; |
| 1179 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 1180 | |
| 1181 | data_col_addr = start_step * chip->ecc.size; |
| 1182 | /* If we read not a page aligned data */ |
| 1183 | if (data_col_addr != 0) |
| 1184 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 1185 | |
| 1186 | p = bufpoi + data_col_addr; |
| 1187 | chip->read_buf(mtd, p, datafrag_len); |
| 1188 | |
| 1189 | /* Calculate ECC */ |
| 1190 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 1191 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 1192 | |
| 1193 | /* The performance is faster if to position offsets |
| 1194 | according to ecc.pos. Let make sure here that |
| 1195 | there are no gaps in ecc positions */ |
| 1196 | for (i = 0; i < eccfrag_len - 1; i++) { |
| 1197 | if (eccpos[i + start_step * chip->ecc.bytes] + 1 != |
| 1198 | eccpos[i + start_step * chip->ecc.bytes + 1]) { |
| 1199 | gaps = 1; |
| 1200 | break; |
| 1201 | } |
| 1202 | } |
| 1203 | if (gaps) { |
| 1204 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 1205 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1206 | } else { |
| 1207 | /* send the command to read the particular ecc bytes */ |
| 1208 | /* take care about buswidth alignment in read_buf */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1209 | index = start_step * chip->ecc.bytes; |
| 1210 | |
| 1211 | aligned_pos = eccpos[index] & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1212 | aligned_len = eccfrag_len; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1213 | if (eccpos[index] & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1214 | aligned_len++; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1215 | if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1216 | aligned_len++; |
| 1217 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1218 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 1219 | mtd->writesize + aligned_pos, -1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1220 | chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); |
| 1221 | } |
| 1222 | |
| 1223 | for (i = 0; i < eccfrag_len; i++) |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1224 | chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]]; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1225 | |
| 1226 | p = bufpoi + data_col_addr; |
| 1227 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1228 | int stat; |
| 1229 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1230 | stat = chip->ecc.correct(mtd, p, |
| 1231 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Baruch Siach | 12c8eb9 | 2010-08-09 07:20:23 +0300 | [diff] [blame] | 1232 | if (stat < 0) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1233 | mtd->ecc_stats.failed++; |
| 1234 | else |
| 1235 | mtd->ecc_stats.corrected += stat; |
| 1236 | } |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | /** |
Artem Bityutskiy | d29ebdb | 2006-10-19 16:04:02 +0300 | [diff] [blame] | 1241 | * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1242 | * @mtd: mtd info structure |
| 1243 | * @chip: nand chip info structure |
| 1244 | * @buf: buffer to store read data |
Jaswinder Singh Rajput | 58475fb | 2009-09-24 13:04:53 +0100 | [diff] [blame] | 1245 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1246 | * |
| 1247 | * Not for syndrome calculating ecc controllers which need a special oob layout |
| 1248 | */ |
| 1249 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 1250 | uint8_t *buf, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1251 | { |
| 1252 | int i, eccsize = chip->ecc.size; |
| 1253 | int eccbytes = chip->ecc.bytes; |
| 1254 | int eccsteps = chip->ecc.steps; |
| 1255 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1256 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1257 | uint8_t *ecc_code = chip->buffers->ecccode; |
Ben Dooks | 8b099a3 | 2007-05-28 19:17:54 +0100 | [diff] [blame] | 1258 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1259 | |
| 1260 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1261 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1262 | chip->read_buf(mtd, p, eccsize); |
| 1263 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1264 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1265 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1266 | |
| 1267 | for (i = 0; i < chip->ecc.total; i++) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1268 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1269 | |
| 1270 | eccsteps = chip->ecc.steps; |
| 1271 | p = buf; |
| 1272 | |
| 1273 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1274 | int stat; |
| 1275 | |
| 1276 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Matt Reimer | c32b8dc | 2007-10-17 14:33:23 -0700 | [diff] [blame] | 1277 | if (stat < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1278 | mtd->ecc_stats.failed++; |
| 1279 | else |
| 1280 | mtd->ecc_stats.corrected += stat; |
| 1281 | } |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | /** |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1286 | * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first |
| 1287 | * @mtd: mtd info structure |
| 1288 | * @chip: nand chip info structure |
| 1289 | * @buf: buffer to store read data |
Jaswinder Singh Rajput | 58475fb | 2009-09-24 13:04:53 +0100 | [diff] [blame] | 1290 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1291 | * |
| 1292 | * Hardware ECC for large page chips, require OOB to be read first. |
| 1293 | * For this ECC mode, the write_page method is re-used from ECC_HW. |
| 1294 | * These methods read/write ECC from the OOB area, unlike the |
| 1295 | * ECC_HW_SYNDROME support with multiple ECC steps, follows the |
| 1296 | * "infix ECC" scheme and reads/writes ECC from the data area, by |
| 1297 | * overwriting the NAND manufacturer bad block markings. |
| 1298 | */ |
| 1299 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
| 1300 | struct nand_chip *chip, uint8_t *buf, int page) |
| 1301 | { |
| 1302 | int i, eccsize = chip->ecc.size; |
| 1303 | int eccbytes = chip->ecc.bytes; |
| 1304 | int eccsteps = chip->ecc.steps; |
| 1305 | uint8_t *p = buf; |
| 1306 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 1307 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1308 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1309 | |
| 1310 | /* Read the OOB area first */ |
| 1311 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 1312 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1313 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1314 | |
| 1315 | for (i = 0; i < chip->ecc.total; i++) |
| 1316 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 1317 | |
| 1318 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1319 | int stat; |
| 1320 | |
| 1321 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1322 | chip->read_buf(mtd, p, eccsize); |
| 1323 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1324 | |
| 1325 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
| 1326 | if (stat < 0) |
| 1327 | mtd->ecc_stats.failed++; |
| 1328 | else |
| 1329 | mtd->ecc_stats.corrected += stat; |
| 1330 | } |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | /** |
Artem Bityutskiy | d29ebdb | 2006-10-19 16:04:02 +0300 | [diff] [blame] | 1335 | * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1336 | * @mtd: mtd info structure |
| 1337 | * @chip: nand chip info structure |
| 1338 | * @buf: buffer to store read data |
Jaswinder Singh Rajput | 58475fb | 2009-09-24 13:04:53 +0100 | [diff] [blame] | 1339 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1340 | * |
| 1341 | * The hw generator calculates the error syndrome automatically. Therefor |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1342 | * we need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1343 | */ |
| 1344 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 1345 | uint8_t *buf, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1346 | { |
| 1347 | int i, eccsize = chip->ecc.size; |
| 1348 | int eccbytes = chip->ecc.bytes; |
| 1349 | int eccsteps = chip->ecc.steps; |
| 1350 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1351 | uint8_t *oob = chip->oob_poi; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1352 | |
| 1353 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1354 | int stat; |
| 1355 | |
| 1356 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1357 | chip->read_buf(mtd, p, eccsize); |
| 1358 | |
| 1359 | if (chip->ecc.prepad) { |
| 1360 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1361 | oob += chip->ecc.prepad; |
| 1362 | } |
| 1363 | |
| 1364 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
| 1365 | chip->read_buf(mtd, oob, eccbytes); |
| 1366 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 1367 | |
Matt Reimer | c32b8dc | 2007-10-17 14:33:23 -0700 | [diff] [blame] | 1368 | if (stat < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1369 | mtd->ecc_stats.failed++; |
| 1370 | else |
| 1371 | mtd->ecc_stats.corrected += stat; |
| 1372 | |
| 1373 | oob += eccbytes; |
| 1374 | |
| 1375 | if (chip->ecc.postpad) { |
| 1376 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1377 | oob += chip->ecc.postpad; |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 1382 | i = mtd->oobsize - (oob - chip->oob_poi); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1383 | if (i) |
| 1384 | chip->read_buf(mtd, oob, i); |
| 1385 | |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1390 | * nand_transfer_oob - [Internal] Transfer oob to client buffer |
| 1391 | * @chip: nand chip structure |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 1392 | * @oob: oob destination address |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1393 | * @ops: oob ops structure |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1394 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1395 | */ |
| 1396 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1397 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1398 | { |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1399 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1400 | |
| 1401 | case MTD_OOB_PLACE: |
| 1402 | case MTD_OOB_RAW: |
| 1403 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 1404 | return oob + len; |
| 1405 | |
| 1406 | case MTD_OOB_AUTO: { |
| 1407 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1408 | uint32_t boffs = 0, roffs = ops->ooboffs; |
| 1409 | size_t bytes = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1410 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1411 | for (; free->length && len; free++, len -= bytes) { |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1412 | /* Read request not from offset 0 ? */ |
| 1413 | if (unlikely(roffs)) { |
| 1414 | if (roffs >= free->length) { |
| 1415 | roffs -= free->length; |
| 1416 | continue; |
| 1417 | } |
| 1418 | boffs = free->offset + roffs; |
| 1419 | bytes = min_t(size_t, len, |
| 1420 | (free->length - roffs)); |
| 1421 | roffs = 0; |
| 1422 | } else { |
| 1423 | bytes = min_t(size_t, len, free->length); |
| 1424 | boffs = free->offset; |
| 1425 | } |
| 1426 | memcpy(oob, chip->oob_poi + boffs, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1427 | oob += bytes; |
| 1428 | } |
| 1429 | return oob; |
| 1430 | } |
| 1431 | default: |
| 1432 | BUG(); |
| 1433 | } |
| 1434 | return NULL; |
| 1435 | } |
| 1436 | |
| 1437 | /** |
| 1438 | * nand_do_read_ops - [Internal] Read data with ECC |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1439 | * |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1440 | * @mtd: MTD device structure |
| 1441 | * @from: offset to read from |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 1442 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1443 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1444 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1445 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1446 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 1447 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1448 | { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1449 | int chipnr, page, realpage, col, bytes, aligned; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1450 | struct nand_chip *chip = mtd->priv; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1451 | struct mtd_ecc_stats stats; |
| 1452 | int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 1453 | int sndcmd = 1; |
| 1454 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1455 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1456 | uint32_t oobreadlen = ops->ooblen; |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 1457 | uint32_t max_oobsize = ops->mode == MTD_OOB_AUTO ? |
| 1458 | mtd->oobavail : mtd->oobsize; |
| 1459 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1460 | uint8_t *bufpoi, *oob, *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1462 | stats = mtd->ecc_stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1464 | chipnr = (int)(from >> chip->chip_shift); |
| 1465 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1467 | realpage = (int)(from >> chip->page_shift); |
| 1468 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1470 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1472 | buf = ops->datbuf; |
| 1473 | oob = ops->oobbuf; |
| 1474 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1475 | while (1) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1476 | bytes = min(mtd->writesize - col, readlen); |
| 1477 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1478 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1479 | /* Is the current page in the buffer ? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1480 | if (realpage != chip->pagebuf || oob) { |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1481 | bufpoi = aligned ? buf : chip->buffers->databuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1483 | if (likely(sndcmd)) { |
| 1484 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
| 1485 | sndcmd = 0; |
| 1486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1488 | /* Now read the page into the buffer */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 1489 | if (unlikely(ops->mode == MTD_OOB_RAW)) |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 1490 | ret = chip->ecc.read_page_raw(mtd, chip, |
| 1491 | bufpoi, page); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1492 | else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob) |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1493 | ret = chip->ecc.read_subpage(mtd, chip, |
| 1494 | col, bytes, bufpoi); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 1495 | else |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 1496 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
| 1497 | page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1498 | if (ret < 0) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1499 | break; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1500 | |
| 1501 | /* Transfer not aligned data */ |
| 1502 | if (!aligned) { |
Artem Bityutskiy | c1194c7 | 2010-09-03 22:01:16 +0300 | [diff] [blame] | 1503 | if (!NAND_SUBPAGE_READ(chip) && !oob && |
| 1504 | !(mtd->ecc_stats.failed - stats.failed)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1505 | chip->pagebuf = realpage; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1506 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1508 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1509 | buf += bytes; |
| 1510 | |
| 1511 | if (unlikely(oob)) { |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 1512 | |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 1513 | int toread = min(oobreadlen, max_oobsize); |
| 1514 | |
| 1515 | if (toread) { |
| 1516 | oob = nand_transfer_oob(chip, |
| 1517 | oob, ops, toread); |
| 1518 | oobreadlen -= toread; |
| 1519 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1520 | } |
| 1521 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1522 | if (!(chip->options & NAND_NO_READRDY)) { |
| 1523 | /* |
| 1524 | * Apply delay or wait for ready/busy pin. Do |
| 1525 | * this before the AUTOINCR check, so no |
| 1526 | * problems arise if a chip which does auto |
| 1527 | * increment is marked as NOAUTOINCR by the |
| 1528 | * board driver. |
| 1529 | */ |
| 1530 | if (!chip->dev_ready) |
| 1531 | udelay(chip->chip_delay); |
| 1532 | else |
| 1533 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1535 | } else { |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1536 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1537 | buf += bytes; |
| 1538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1540 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1541 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1542 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1543 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
| 1545 | /* For subsequent reads align to page boundary. */ |
| 1546 | col = 0; |
| 1547 | /* Increment page address */ |
| 1548 | realpage++; |
| 1549 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1550 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | /* Check, if we cross a chip boundary */ |
| 1552 | if (!page) { |
| 1553 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1554 | chip->select_chip(mtd, -1); |
| 1555 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1557 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1558 | /* Check, if the chip supports auto page increment |
| 1559 | * or if we have hit a block boundary. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1560 | */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1561 | if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck)) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1562 | sndcmd = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1565 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1566 | if (oob) |
| 1567 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1569 | if (ret) |
| 1570 | return ret; |
| 1571 | |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 1572 | if (mtd->ecc_stats.failed - stats.failed) |
| 1573 | return -EBADMSG; |
| 1574 | |
| 1575 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1579 | * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1580 | * @mtd: MTD device structure |
| 1581 | * @from: offset to read from |
| 1582 | * @len: number of bytes to read |
| 1583 | * @retlen: pointer to variable to store the number of read bytes |
| 1584 | * @buf: the databuffer to put data |
| 1585 | * |
| 1586 | * Get hold of the chip and call nand_do_read |
| 1587 | */ |
| 1588 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 1589 | size_t *retlen, uint8_t *buf) |
| 1590 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1591 | struct nand_chip *chip = mtd->priv; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1592 | int ret; |
| 1593 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1594 | /* Do not allow reads past end of device */ |
| 1595 | if ((from + len) > mtd->size) |
| 1596 | return -EINVAL; |
| 1597 | if (!len) |
| 1598 | return 0; |
| 1599 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1600 | nand_get_device(chip, mtd, FL_READING); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1601 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1602 | chip->ops.len = len; |
| 1603 | chip->ops.datbuf = buf; |
| 1604 | chip->ops.oobbuf = NULL; |
| 1605 | |
| 1606 | ret = nand_do_read_ops(mtd, from, &chip->ops); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1607 | |
Richard Purdie | 7fd5aec | 2006-08-27 01:23:33 -0700 | [diff] [blame] | 1608 | *retlen = chip->ops.retlen; |
| 1609 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1610 | nand_release_device(mtd); |
| 1611 | |
| 1612 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /** |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1616 | * nand_read_oob_std - [REPLACABLE] the most common OOB data read function |
| 1617 | * @mtd: mtd info structure |
| 1618 | * @chip: nand chip info structure |
| 1619 | * @page: page number to read |
| 1620 | * @sndcmd: flag whether to issue read command or not |
| 1621 | */ |
| 1622 | static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 1623 | int page, int sndcmd) |
| 1624 | { |
| 1625 | if (sndcmd) { |
| 1626 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 1627 | sndcmd = 0; |
| 1628 | } |
| 1629 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1630 | return sndcmd; |
| 1631 | } |
| 1632 | |
| 1633 | /** |
| 1634 | * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC |
| 1635 | * with syndromes |
| 1636 | * @mtd: mtd info structure |
| 1637 | * @chip: nand chip info structure |
| 1638 | * @page: page number to read |
| 1639 | * @sndcmd: flag whether to issue read command or not |
| 1640 | */ |
| 1641 | static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 1642 | int page, int sndcmd) |
| 1643 | { |
| 1644 | uint8_t *buf = chip->oob_poi; |
| 1645 | int length = mtd->oobsize; |
| 1646 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 1647 | int eccsize = chip->ecc.size; |
| 1648 | uint8_t *bufpoi = buf; |
| 1649 | int i, toread, sndrnd = 0, pos; |
| 1650 | |
| 1651 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); |
| 1652 | for (i = 0; i < chip->ecc.steps; i++) { |
| 1653 | if (sndrnd) { |
| 1654 | pos = eccsize + i * (eccsize + chunk); |
| 1655 | if (mtd->writesize > 512) |
| 1656 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); |
| 1657 | else |
| 1658 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); |
| 1659 | } else |
| 1660 | sndrnd = 1; |
| 1661 | toread = min_t(int, length, chunk); |
| 1662 | chip->read_buf(mtd, bufpoi, toread); |
| 1663 | bufpoi += toread; |
| 1664 | length -= toread; |
| 1665 | } |
| 1666 | if (length > 0) |
| 1667 | chip->read_buf(mtd, bufpoi, length); |
| 1668 | |
| 1669 | return 1; |
| 1670 | } |
| 1671 | |
| 1672 | /** |
| 1673 | * nand_write_oob_std - [REPLACABLE] the most common OOB data write function |
| 1674 | * @mtd: mtd info structure |
| 1675 | * @chip: nand chip info structure |
| 1676 | * @page: page number to write |
| 1677 | */ |
| 1678 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 1679 | int page) |
| 1680 | { |
| 1681 | int status = 0; |
| 1682 | const uint8_t *buf = chip->oob_poi; |
| 1683 | int length = mtd->oobsize; |
| 1684 | |
| 1685 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 1686 | chip->write_buf(mtd, buf, length); |
| 1687 | /* Send command to program the OOB data */ |
| 1688 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1689 | |
| 1690 | status = chip->waitfunc(mtd, chip); |
| 1691 | |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 1692 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | /** |
| 1696 | * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC |
| 1697 | * with syndrome - only for large page flash ! |
| 1698 | * @mtd: mtd info structure |
| 1699 | * @chip: nand chip info structure |
| 1700 | * @page: page number to write |
| 1701 | */ |
| 1702 | static int nand_write_oob_syndrome(struct mtd_info *mtd, |
| 1703 | struct nand_chip *chip, int page) |
| 1704 | { |
| 1705 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 1706 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
| 1707 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; |
| 1708 | const uint8_t *bufpoi = chip->oob_poi; |
| 1709 | |
| 1710 | /* |
| 1711 | * data-ecc-data-ecc ... ecc-oob |
| 1712 | * or |
| 1713 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 1714 | */ |
| 1715 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 1716 | pos = steps * (eccsize + chunk); |
| 1717 | steps = 0; |
| 1718 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 1719 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1720 | |
| 1721 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); |
| 1722 | for (i = 0; i < steps; i++) { |
| 1723 | if (sndcmd) { |
| 1724 | if (mtd->writesize <= 512) { |
| 1725 | uint32_t fill = 0xFFFFFFFF; |
| 1726 | |
| 1727 | len = eccsize; |
| 1728 | while (len > 0) { |
| 1729 | int num = min_t(int, len, 4); |
| 1730 | chip->write_buf(mtd, (uint8_t *)&fill, |
| 1731 | num); |
| 1732 | len -= num; |
| 1733 | } |
| 1734 | } else { |
| 1735 | pos = eccsize + i * (eccsize + chunk); |
| 1736 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); |
| 1737 | } |
| 1738 | } else |
| 1739 | sndcmd = 1; |
| 1740 | len = min_t(int, length, chunk); |
| 1741 | chip->write_buf(mtd, bufpoi, len); |
| 1742 | bufpoi += len; |
| 1743 | length -= len; |
| 1744 | } |
| 1745 | if (length > 0) |
| 1746 | chip->write_buf(mtd, bufpoi, length); |
| 1747 | |
| 1748 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1749 | status = chip->waitfunc(mtd, chip); |
| 1750 | |
| 1751 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 1752 | } |
| 1753 | |
| 1754 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1755 | * nand_do_read_oob - [Intern] NAND read out-of-band |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | * @mtd: MTD device structure |
| 1757 | * @from: offset to read from |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1758 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | * |
| 1760 | * NAND read out-of-band data from the spare area |
| 1761 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1762 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 1763 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | { |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1765 | int page, realpage, chipnr, sndcmd = 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1766 | struct nand_chip *chip = mtd->priv; |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 1767 | int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1768 | int readlen = ops->ooblen; |
| 1769 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1770 | uint8_t *buf = ops->oobbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 1772 | DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n", |
| 1773 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 1775 | if (ops->mode == MTD_OOB_AUTO) |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1776 | len = chip->ecc.layout->oobavail; |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 1777 | else |
| 1778 | len = mtd->oobsize; |
| 1779 | |
| 1780 | if (unlikely(ops->ooboffs >= len)) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 1781 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read " |
| 1782 | "outside oob\n", __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 1783 | return -EINVAL; |
| 1784 | } |
| 1785 | |
| 1786 | /* Do not allow reads past end of device */ |
| 1787 | if (unlikely(from >= mtd->size || |
| 1788 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 1789 | (from >> chip->page_shift)) * len)) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 1790 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end " |
| 1791 | "of device\n", __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 1792 | return -EINVAL; |
| 1793 | } |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1794 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 1795 | chipnr = (int)(from >> chip->chip_shift); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1796 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 1798 | /* Shift to get page */ |
| 1799 | realpage = (int)(from >> chip->page_shift); |
| 1800 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1802 | while (1) { |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1803 | sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd); |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1804 | |
| 1805 | len = min(len, readlen); |
| 1806 | buf = nand_transfer_oob(chip, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1807 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 1808 | if (!(chip->options & NAND_NO_READRDY)) { |
| 1809 | /* |
| 1810 | * Apply delay or wait for ready/busy pin. Do this |
| 1811 | * before the AUTOINCR check, so no problems arise if a |
| 1812 | * chip which does auto increment is marked as |
| 1813 | * NOAUTOINCR by the board driver. |
Thomas Gleixner | 19870da | 2005-07-15 14:53:51 +0100 | [diff] [blame] | 1814 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1815 | if (!chip->dev_ready) |
| 1816 | udelay(chip->chip_delay); |
Thomas Gleixner | 19870da | 2005-07-15 14:53:51 +0100 | [diff] [blame] | 1817 | else |
| 1818 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | } |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 1820 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1821 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 1822 | if (!readlen) |
| 1823 | break; |
| 1824 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 1825 | /* Increment page address */ |
| 1826 | realpage++; |
| 1827 | |
| 1828 | page = realpage & chip->pagemask; |
| 1829 | /* Check, if we cross a chip boundary */ |
| 1830 | if (!page) { |
| 1831 | chipnr++; |
| 1832 | chip->select_chip(mtd, -1); |
| 1833 | chip->select_chip(mtd, chipnr); |
| 1834 | } |
| 1835 | |
| 1836 | /* Check, if the chip supports auto page increment |
| 1837 | * or if we have hit a block boundary. |
| 1838 | */ |
| 1839 | if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck)) |
| 1840 | sndcmd = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1843 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | return 0; |
| 1845 | } |
| 1846 | |
| 1847 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1848 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | * @from: offset to read from |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1851 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | * |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1853 | * NAND read data and/or out-of-band data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1855 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 1856 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1858 | struct nand_chip *chip = mtd->priv; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1859 | int ret = -ENOTSUPP; |
| 1860 | |
| 1861 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | |
| 1863 | /* Do not allow reads past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1864 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 1865 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read " |
| 1866 | "beyond end of device\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | return -EINVAL; |
| 1868 | } |
| 1869 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1870 | nand_get_device(chip, mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1872 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1873 | case MTD_OOB_PLACE: |
| 1874 | case MTD_OOB_AUTO: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1875 | case MTD_OOB_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1876 | break; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1877 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1878 | default: |
| 1879 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1882 | if (!ops->datbuf) |
| 1883 | ret = nand_do_read_oob(mtd, from, ops); |
| 1884 | else |
| 1885 | ret = nand_do_read_ops(mtd, from, ops); |
| 1886 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1887 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1889 | return ret; |
| 1890 | } |
| 1891 | |
| 1892 | |
| 1893 | /** |
| 1894 | * nand_write_page_raw - [Intern] raw page write function |
| 1895 | * @mtd: mtd info structure |
| 1896 | * @chip: nand chip info structure |
| 1897 | * @buf: data buffer |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1898 | * |
| 1899 | * Not for syndrome calculating ecc controllers, which use a special oob layout |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1900 | */ |
| 1901 | static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1902 | const uint8_t *buf) |
| 1903 | { |
| 1904 | chip->write_buf(mtd, buf, mtd->writesize); |
| 1905 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1908 | /** |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1909 | * nand_write_page_raw_syndrome - [Intern] raw page write function |
| 1910 | * @mtd: mtd info structure |
| 1911 | * @chip: nand chip info structure |
| 1912 | * @buf: data buffer |
| 1913 | * |
| 1914 | * We need a special oob layout and handling even when ECC isn't checked. |
| 1915 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1916 | static void nand_write_page_raw_syndrome(struct mtd_info *mtd, |
| 1917 | struct nand_chip *chip, |
| 1918 | const uint8_t *buf) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1919 | { |
| 1920 | int eccsize = chip->ecc.size; |
| 1921 | int eccbytes = chip->ecc.bytes; |
| 1922 | uint8_t *oob = chip->oob_poi; |
| 1923 | int steps, size; |
| 1924 | |
| 1925 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 1926 | chip->write_buf(mtd, buf, eccsize); |
| 1927 | buf += eccsize; |
| 1928 | |
| 1929 | if (chip->ecc.prepad) { |
| 1930 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 1931 | oob += chip->ecc.prepad; |
| 1932 | } |
| 1933 | |
| 1934 | chip->read_buf(mtd, oob, eccbytes); |
| 1935 | oob += eccbytes; |
| 1936 | |
| 1937 | if (chip->ecc.postpad) { |
| 1938 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 1939 | oob += chip->ecc.postpad; |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 1944 | if (size) |
| 1945 | chip->write_buf(mtd, oob, size); |
| 1946 | } |
| 1947 | /** |
Artem Bityutskiy | d29ebdb | 2006-10-19 16:04:02 +0300 | [diff] [blame] | 1948 | * nand_write_page_swecc - [REPLACABLE] software ecc based page write function |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1949 | * @mtd: mtd info structure |
| 1950 | * @chip: nand chip info structure |
| 1951 | * @buf: data buffer |
| 1952 | */ |
| 1953 | static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 1954 | const uint8_t *buf) |
| 1955 | { |
| 1956 | int i, eccsize = chip->ecc.size; |
| 1957 | int eccbytes = chip->ecc.bytes; |
| 1958 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1959 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1960 | const uint8_t *p = buf; |
Ben Dooks | 8b099a3 | 2007-05-28 19:17:54 +0100 | [diff] [blame] | 1961 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1962 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1963 | /* Software ecc calculation */ |
| 1964 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1965 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1966 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1967 | for (i = 0; i < chip->ecc.total; i++) |
| 1968 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1969 | |
Thomas Gleixner | 90424de | 2007-04-05 11:44:05 +0200 | [diff] [blame] | 1970 | chip->ecc.write_page_raw(mtd, chip, buf); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | /** |
Artem Bityutskiy | d29ebdb | 2006-10-19 16:04:02 +0300 | [diff] [blame] | 1974 | * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1975 | * @mtd: mtd info structure |
| 1976 | * @chip: nand chip info structure |
| 1977 | * @buf: data buffer |
| 1978 | */ |
| 1979 | static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 1980 | const uint8_t *buf) |
| 1981 | { |
| 1982 | int i, eccsize = chip->ecc.size; |
| 1983 | int eccbytes = chip->ecc.bytes; |
| 1984 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1985 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1986 | const uint8_t *p = buf; |
Ben Dooks | 8b099a3 | 2007-05-28 19:17:54 +0100 | [diff] [blame] | 1987 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1988 | |
| 1989 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1990 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
David Woodhouse | 29da9ce | 2006-05-26 23:05:44 +0100 | [diff] [blame] | 1991 | chip->write_buf(mtd, p, eccsize); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1992 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1993 | } |
| 1994 | |
| 1995 | for (i = 0; i < chip->ecc.total; i++) |
| 1996 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 1997 | |
| 1998 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1999 | } |
| 2000 | |
| 2001 | /** |
Artem Bityutskiy | d29ebdb | 2006-10-19 16:04:02 +0300 | [diff] [blame] | 2002 | * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2003 | * @mtd: mtd info structure |
| 2004 | * @chip: nand chip info structure |
| 2005 | * @buf: data buffer |
| 2006 | * |
| 2007 | * The hw generator calculates the error syndrome automatically. Therefor |
| 2008 | * we need a special oob layout and handling. |
| 2009 | */ |
| 2010 | static void nand_write_page_syndrome(struct mtd_info *mtd, |
| 2011 | struct nand_chip *chip, const uint8_t *buf) |
| 2012 | { |
| 2013 | int i, eccsize = chip->ecc.size; |
| 2014 | int eccbytes = chip->ecc.bytes; |
| 2015 | int eccsteps = chip->ecc.steps; |
| 2016 | const uint8_t *p = buf; |
| 2017 | uint8_t *oob = chip->oob_poi; |
| 2018 | |
| 2019 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2020 | |
| 2021 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2022 | chip->write_buf(mtd, p, eccsize); |
| 2023 | |
| 2024 | if (chip->ecc.prepad) { |
| 2025 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2026 | oob += chip->ecc.prepad; |
| 2027 | } |
| 2028 | |
| 2029 | chip->ecc.calculate(mtd, p, oob); |
| 2030 | chip->write_buf(mtd, oob, eccbytes); |
| 2031 | oob += eccbytes; |
| 2032 | |
| 2033 | if (chip->ecc.postpad) { |
| 2034 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2035 | oob += chip->ecc.postpad; |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 2040 | i = mtd->oobsize - (oob - chip->oob_poi); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2041 | if (i) |
| 2042 | chip->write_buf(mtd, oob, i); |
| 2043 | } |
| 2044 | |
| 2045 | /** |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2046 | * nand_write_page - [REPLACEABLE] write one page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2047 | * @mtd: MTD device structure |
| 2048 | * @chip: NAND chip descriptor |
| 2049 | * @buf: the data to write |
| 2050 | * @page: page number to write |
| 2051 | * @cached: cached programming |
Jesper Juhl | efbfe96c | 2006-10-27 23:24:47 +0200 | [diff] [blame] | 2052 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2053 | */ |
| 2054 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2055 | const uint8_t *buf, int page, int cached, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2056 | { |
| 2057 | int status; |
| 2058 | |
| 2059 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
| 2060 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2061 | if (unlikely(raw)) |
| 2062 | chip->ecc.write_page_raw(mtd, chip, buf); |
| 2063 | else |
| 2064 | chip->ecc.write_page(mtd, chip, buf); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2065 | |
| 2066 | /* |
| 2067 | * Cached progamming disabled for now, Not sure if its worth the |
| 2068 | * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s) |
| 2069 | */ |
| 2070 | cached = 0; |
| 2071 | |
| 2072 | if (!cached || !(chip->options & NAND_CACHEPRG)) { |
| 2073 | |
| 2074 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2075 | status = chip->waitfunc(mtd, chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2076 | /* |
| 2077 | * See if operation failed and additional status checks are |
| 2078 | * available |
| 2079 | */ |
| 2080 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 2081 | status = chip->errstat(mtd, chip, FL_WRITING, status, |
| 2082 | page); |
| 2083 | |
| 2084 | if (status & NAND_STATUS_FAIL) |
| 2085 | return -EIO; |
| 2086 | } else { |
| 2087 | chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2088 | status = chip->waitfunc(mtd, chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE |
| 2092 | /* Send command to read back the data */ |
| 2093 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 2094 | |
| 2095 | if (chip->verify_buf(mtd, buf, mtd->writesize)) |
| 2096 | return -EIO; |
| 2097 | #endif |
| 2098 | return 0; |
| 2099 | } |
| 2100 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2101 | /** |
| 2102 | * nand_fill_oob - [Internal] Transfer client buffer to oob |
| 2103 | * @chip: nand chip structure |
| 2104 | * @oob: oob data buffer |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 2105 | * @len: oob data write length |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2106 | * @ops: oob ops structure |
| 2107 | */ |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2108 | static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len, |
| 2109 | struct mtd_oob_ops *ops) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2110 | { |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2111 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2112 | |
| 2113 | case MTD_OOB_PLACE: |
| 2114 | case MTD_OOB_RAW: |
| 2115 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 2116 | return oob + len; |
| 2117 | |
| 2118 | case MTD_OOB_AUTO: { |
| 2119 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2120 | uint32_t boffs = 0, woffs = ops->ooboffs; |
| 2121 | size_t bytes = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2122 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2123 | for (; free->length && len; free++, len -= bytes) { |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2124 | /* Write request not from offset 0 ? */ |
| 2125 | if (unlikely(woffs)) { |
| 2126 | if (woffs >= free->length) { |
| 2127 | woffs -= free->length; |
| 2128 | continue; |
| 2129 | } |
| 2130 | boffs = free->offset + woffs; |
| 2131 | bytes = min_t(size_t, len, |
| 2132 | (free->length - woffs)); |
| 2133 | woffs = 0; |
| 2134 | } else { |
| 2135 | bytes = min_t(size_t, len, free->length); |
| 2136 | boffs = free->offset; |
| 2137 | } |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 2138 | memcpy(chip->oob_poi + boffs, oob, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2139 | oob += bytes; |
| 2140 | } |
| 2141 | return oob; |
| 2142 | } |
| 2143 | default: |
| 2144 | BUG(); |
| 2145 | } |
| 2146 | return NULL; |
| 2147 | } |
| 2148 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2149 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2150 | |
| 2151 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2152 | * nand_do_write_ops - [Internal] NAND write with ECC |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2153 | * @mtd: MTD device structure |
| 2154 | * @to: offset to write to |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2155 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2156 | * |
| 2157 | * NAND write with ECC |
| 2158 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2159 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 2160 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2161 | { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2162 | int chipnr, realpage, page, blockmask, column; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2163 | struct nand_chip *chip = mtd->priv; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2164 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2165 | |
| 2166 | uint32_t oobwritelen = ops->ooblen; |
| 2167 | uint32_t oobmaxlen = ops->mode == MTD_OOB_AUTO ? |
| 2168 | mtd->oobavail : mtd->oobsize; |
| 2169 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2170 | uint8_t *oob = ops->oobbuf; |
| 2171 | uint8_t *buf = ops->datbuf; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2172 | int ret, subpage; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2173 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2174 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2175 | if (!writelen) |
| 2176 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2177 | |
| 2178 | /* reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2179 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2180 | printk(KERN_NOTICE "%s: Attempt to write not " |
| 2181 | "page aligned data\n", __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2182 | return -EINVAL; |
| 2183 | } |
| 2184 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2185 | column = to & (mtd->writesize - 1); |
| 2186 | subpage = column || (writelen & (mtd->writesize - 1)); |
| 2187 | |
| 2188 | if (subpage && oob) |
| 2189 | return -EINVAL; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2190 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 2191 | chipnr = (int)(to >> chip->chip_shift); |
| 2192 | chip->select_chip(mtd, chipnr); |
| 2193 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2194 | /* Check, if it is write protected */ |
| 2195 | if (nand_check_wp(mtd)) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2196 | return -EIO; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2197 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2198 | realpage = (int)(to >> chip->page_shift); |
| 2199 | page = realpage & chip->pagemask; |
| 2200 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 2201 | |
| 2202 | /* Invalidate the page cache, when we write to the cached page */ |
| 2203 | if (to <= (chip->pagebuf << chip->page_shift) && |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2204 | (chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2205 | chip->pagebuf = -1; |
| 2206 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 2207 | /* If we're not given explicit OOB data, let it be 0xFF */ |
| 2208 | if (likely(!oob)) |
| 2209 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2210 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2211 | /* Don't allow multipage oob writes with offset */ |
Jon Povey | cdcf12b | 2010-09-30 20:41:34 +0900 | [diff] [blame] | 2212 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2213 | return -EINVAL; |
| 2214 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2215 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2216 | int bytes = mtd->writesize; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2217 | int cached = writelen > bytes && page != blockmask; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2218 | uint8_t *wbuf = buf; |
| 2219 | |
| 2220 | /* Partial page write ? */ |
| 2221 | if (unlikely(column || writelen < (mtd->writesize - 1))) { |
| 2222 | cached = 0; |
| 2223 | bytes = min_t(int, bytes - column, (int) writelen); |
| 2224 | chip->pagebuf = -1; |
| 2225 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 2226 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 2227 | wbuf = chip->buffers->databuf; |
| 2228 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2229 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2230 | if (unlikely(oob)) { |
| 2231 | size_t len = min(oobwritelen, oobmaxlen); |
| 2232 | oob = nand_fill_oob(chip, oob, len, ops); |
| 2233 | oobwritelen -= len; |
| 2234 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2235 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2236 | ret = chip->write_page(mtd, chip, wbuf, page, cached, |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2237 | (ops->mode == MTD_OOB_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2238 | if (ret) |
| 2239 | break; |
| 2240 | |
| 2241 | writelen -= bytes; |
| 2242 | if (!writelen) |
| 2243 | break; |
| 2244 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2245 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2246 | buf += bytes; |
| 2247 | realpage++; |
| 2248 | |
| 2249 | page = realpage & chip->pagemask; |
| 2250 | /* Check, if we cross a chip boundary */ |
| 2251 | if (!page) { |
| 2252 | chipnr++; |
| 2253 | chip->select_chip(mtd, -1); |
| 2254 | chip->select_chip(mtd, chipnr); |
| 2255 | } |
| 2256 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2257 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2258 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2259 | if (unlikely(oob)) |
| 2260 | ops->oobretlen = ops->ooblen; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2261 | return ret; |
| 2262 | } |
| 2263 | |
| 2264 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2265 | * panic_nand_write - [MTD Interface] NAND write with ECC |
| 2266 | * @mtd: MTD device structure |
| 2267 | * @to: offset to write to |
| 2268 | * @len: number of bytes to write |
| 2269 | * @retlen: pointer to variable to store the number of written bytes |
| 2270 | * @buf: the data to write |
| 2271 | * |
| 2272 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 2273 | * may for example be called by mtdoops when writing an oops while in panic. |
| 2274 | */ |
| 2275 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 2276 | size_t *retlen, const uint8_t *buf) |
| 2277 | { |
| 2278 | struct nand_chip *chip = mtd->priv; |
| 2279 | int ret; |
| 2280 | |
| 2281 | /* Do not allow reads past end of device */ |
| 2282 | if ((to + len) > mtd->size) |
| 2283 | return -EINVAL; |
| 2284 | if (!len) |
| 2285 | return 0; |
| 2286 | |
| 2287 | /* Wait for the device to get ready. */ |
| 2288 | panic_nand_wait(mtd, chip, 400); |
| 2289 | |
| 2290 | /* Grab the device. */ |
| 2291 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 2292 | |
| 2293 | chip->ops.len = len; |
| 2294 | chip->ops.datbuf = (uint8_t *)buf; |
| 2295 | chip->ops.oobbuf = NULL; |
| 2296 | |
| 2297 | ret = nand_do_write_ops(mtd, to, &chip->ops); |
| 2298 | |
| 2299 | *retlen = chip->ops.retlen; |
| 2300 | return ret; |
| 2301 | } |
| 2302 | |
| 2303 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2304 | * nand_write - [MTD Interface] NAND write with ECC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | * @mtd: MTD device structure |
| 2306 | * @to: offset to write to |
| 2307 | * @len: number of bytes to write |
| 2308 | * @retlen: pointer to variable to store the number of written bytes |
| 2309 | * @buf: the data to write |
| 2310 | * |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2311 | * NAND write with ECC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2313 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2314 | size_t *retlen, const uint8_t *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2316 | struct nand_chip *chip = mtd->priv; |
| 2317 | int ret; |
| 2318 | |
| 2319 | /* Do not allow reads past end of device */ |
| 2320 | if ((to + len) > mtd->size) |
| 2321 | return -EINVAL; |
| 2322 | if (!len) |
| 2323 | return 0; |
| 2324 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2325 | nand_get_device(chip, mtd, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2326 | |
| 2327 | chip->ops.len = len; |
| 2328 | chip->ops.datbuf = (uint8_t *)buf; |
| 2329 | chip->ops.oobbuf = NULL; |
| 2330 | |
| 2331 | ret = nand_do_write_ops(mtd, to, &chip->ops); |
| 2332 | |
Richard Purdie | 7fd5aec | 2006-08-27 01:23:33 -0700 | [diff] [blame] | 2333 | *retlen = chip->ops.retlen; |
| 2334 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2335 | nand_release_device(mtd); |
| 2336 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2337 | return ret; |
| 2338 | } |
| 2339 | |
| 2340 | /** |
| 2341 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
| 2342 | * @mtd: MTD device structure |
| 2343 | * @to: offset to write to |
| 2344 | * @ops: oob operation description structure |
| 2345 | * |
| 2346 | * NAND write out-of-band |
| 2347 | */ |
| 2348 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 2349 | struct mtd_oob_ops *ops) |
| 2350 | { |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2351 | int chipnr, page, status, len; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2352 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2354 | DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n", |
| 2355 | __func__, (unsigned int)to, (int)ops->ooblen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2357 | if (ops->mode == MTD_OOB_AUTO) |
| 2358 | len = chip->ecc.layout->oobavail; |
| 2359 | else |
| 2360 | len = mtd->oobsize; |
| 2361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | /* Do not allow write past end of page */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2363 | if ((ops->ooboffs + ops->ooblen) > len) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2364 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write " |
| 2365 | "past end of page\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | return -EINVAL; |
| 2367 | } |
| 2368 | |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2369 | if (unlikely(ops->ooboffs >= len)) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2370 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start " |
| 2371 | "write outside oob\n", __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2372 | return -EINVAL; |
| 2373 | } |
| 2374 | |
Jason Liu | 775adc3d4 | 2011-02-25 13:06:18 +0800 | [diff] [blame] | 2375 | /* Do not allow write past end of device */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2376 | if (unlikely(to >= mtd->size || |
| 2377 | ops->ooboffs + ops->ooblen > |
| 2378 | ((mtd->size >> chip->page_shift) - |
| 2379 | (to >> chip->page_shift)) * len)) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2380 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond " |
| 2381 | "end of device\n", __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2382 | return -EINVAL; |
| 2383 | } |
| 2384 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2385 | chipnr = (int)(to >> chip->chip_shift); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2386 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2388 | /* Shift to get page */ |
| 2389 | page = (int)(to >> chip->page_shift); |
| 2390 | |
| 2391 | /* |
| 2392 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 2393 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 2394 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 2395 | * it in the doc2000 driver in August 1999. dwmw2. |
| 2396 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2397 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | |
| 2399 | /* Check, if it is write protected */ |
| 2400 | if (nand_check_wp(mtd)) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2401 | return -EROFS; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2404 | if (page == chip->pagebuf) |
| 2405 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2407 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2408 | nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2409 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
| 2410 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2411 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2412 | if (status) |
| 2413 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2415 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2417 | return 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2420 | /** |
| 2421 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band |
| 2422 | * @mtd: MTD device structure |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 2423 | * @to: offset to write to |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2424 | * @ops: oob operation description structure |
| 2425 | */ |
| 2426 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 2427 | struct mtd_oob_ops *ops) |
| 2428 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2429 | struct nand_chip *chip = mtd->priv; |
| 2430 | int ret = -ENOTSUPP; |
| 2431 | |
| 2432 | ops->retlen = 0; |
| 2433 | |
| 2434 | /* Do not allow writes past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2435 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2436 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond " |
| 2437 | "end of device\n", __func__); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2438 | return -EINVAL; |
| 2439 | } |
| 2440 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2441 | nand_get_device(chip, mtd, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2442 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2443 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2444 | case MTD_OOB_PLACE: |
| 2445 | case MTD_OOB_AUTO: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2446 | case MTD_OOB_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2447 | break; |
| 2448 | |
| 2449 | default: |
| 2450 | goto out; |
| 2451 | } |
| 2452 | |
| 2453 | if (!ops->datbuf) |
| 2454 | ret = nand_do_write_oob(mtd, to, ops); |
| 2455 | else |
| 2456 | ret = nand_do_write_ops(mtd, to, ops); |
| 2457 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2458 | out: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2459 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | return ret; |
| 2461 | } |
| 2462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | * single_erease_cmd - [GENERIC] NAND standard block erase command function |
| 2465 | * @mtd: MTD device structure |
| 2466 | * @page: the page address of the block which will be erased |
| 2467 | * |
| 2468 | * Standard erase command for NAND chips |
| 2469 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2470 | static void single_erase_cmd(struct mtd_info *mtd, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2472 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | /* Send commands to erase a block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2474 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2475 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | /** |
| 2479 | * multi_erease_cmd - [GENERIC] AND specific block erase command function |
| 2480 | * @mtd: MTD device structure |
| 2481 | * @page: the page address of the block which will be erased |
| 2482 | * |
| 2483 | * AND multi block erase command function |
| 2484 | * Erase 4 consecutive blocks |
| 2485 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2486 | static void multi_erase_cmd(struct mtd_info *mtd, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2488 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | /* Send commands to erase a block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2490 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2491 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2492 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2493 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2494 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | /** |
| 2498 | * nand_erase - [MTD Interface] erase block(s) |
| 2499 | * @mtd: MTD device structure |
| 2500 | * @instr: erase instruction |
| 2501 | * |
| 2502 | * Erase one ore more blocks |
| 2503 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2504 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2506 | return nand_erase_nand(mtd, instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2508 | |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2509 | #define BBT_PAGE_MASK 0xffffff3f |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2511 | * nand_erase_nand - [Internal] erase block(s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | * @mtd: MTD device structure |
| 2513 | * @instr: erase instruction |
| 2514 | * @allowbbt: allow erasing the bbt area |
| 2515 | * |
| 2516 | * Erase one ore more blocks |
| 2517 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2518 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 2519 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | { |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 2521 | int page, status, pages_per_block, ret, chipnr; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2522 | struct nand_chip *chip = mtd->priv; |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2523 | loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0}; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2524 | unsigned int bbt_masked_page = 0xffffffff; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 2525 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2527 | DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n", |
| 2528 | __func__, (unsigned long long)instr->addr, |
| 2529 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 2531 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | |
Adrian Hunter | bb0eb21 | 2008-08-12 12:40:50 +0300 | [diff] [blame] | 2534 | instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | |
| 2536 | /* Grab the lock and see if the device is available */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2537 | nand_get_device(chip, mtd, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | |
| 2539 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2540 | page = (int)(instr->addr >> chip->page_shift); |
| 2541 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | |
| 2543 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2544 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | |
| 2546 | /* Select the NAND device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2547 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | /* Check, if it is write protected */ |
| 2550 | if (nand_check_wp(mtd)) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2551 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n", |
| 2552 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | instr->state = MTD_ERASE_FAILED; |
| 2554 | goto erase_exit; |
| 2555 | } |
| 2556 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2557 | /* |
| 2558 | * If BBT requires refresh, set the BBT page mask to see if the BBT |
| 2559 | * should be rewritten. Otherwise the mask is set to 0xffffffff which |
| 2560 | * can not be matched. This is also done when the bbt is actually |
| 2561 | * erased to avoid recusrsive updates |
| 2562 | */ |
| 2563 | if (chip->options & BBT_AUTO_REFRESH && !allowbbt) |
| 2564 | bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK; |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | /* Loop through the pages */ |
| 2567 | len = instr->len; |
| 2568 | |
| 2569 | instr->state = MTD_ERASING; |
| 2570 | |
| 2571 | while (len) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2572 | /* |
| 2573 | * heck if we have a bad block, we do not erase bad blocks ! |
| 2574 | */ |
| 2575 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
| 2576 | chip->page_shift, 0, allowbbt)) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2577 | printk(KERN_WARNING "%s: attempt to erase a bad block " |
| 2578 | "at page 0x%08x\n", __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | instr->state = MTD_ERASE_FAILED; |
| 2580 | goto erase_exit; |
| 2581 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2582 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2583 | /* |
| 2584 | * Invalidate the page cache, if we erase the block which |
| 2585 | * contains the current cached page |
| 2586 | */ |
| 2587 | if (page <= chip->pagebuf && chip->pagebuf < |
| 2588 | (page + pages_per_block)) |
| 2589 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2591 | chip->erase_cmd(mtd, page & chip->pagemask); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2592 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2593 | status = chip->waitfunc(mtd, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2595 | /* |
| 2596 | * See if operation failed and additional status checks are |
| 2597 | * available |
| 2598 | */ |
| 2599 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 2600 | status = chip->errstat(mtd, chip, FL_ERASING, |
| 2601 | status, page); |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | /* See if block erase succeeded */ |
David A. Marlin | a4ab4c5 | 2005-01-23 18:30:53 +0000 | [diff] [blame] | 2604 | if (status & NAND_STATUS_FAIL) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2605 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, " |
| 2606 | "page 0x%08x\n", __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | instr->state = MTD_ERASE_FAILED; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 2608 | instr->fail_addr = |
| 2609 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | goto erase_exit; |
| 2611 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2612 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2613 | /* |
| 2614 | * If BBT requires refresh, set the BBT rewrite flag to the |
| 2615 | * page being erased |
| 2616 | */ |
| 2617 | if (bbt_masked_page != 0xffffffff && |
| 2618 | (page & BBT_PAGE_MASK) == bbt_masked_page) |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 2619 | rewrite_bbt[chipnr] = |
| 2620 | ((loff_t)page << chip->page_shift); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | /* Increment page address and decrement length */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2623 | len -= (1 << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | page += pages_per_block; |
| 2625 | |
| 2626 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2627 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2629 | chip->select_chip(mtd, -1); |
| 2630 | chip->select_chip(mtd, chipnr); |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2631 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2632 | /* |
| 2633 | * If BBT requires refresh and BBT-PERCHIP, set the BBT |
| 2634 | * page mask to see if this BBT should be rewritten |
| 2635 | */ |
| 2636 | if (bbt_masked_page != 0xffffffff && |
| 2637 | (chip->bbt_td->options & NAND_BBT_PERCHIP)) |
| 2638 | bbt_masked_page = chip->bbt_td->pages[chipnr] & |
| 2639 | BBT_PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | } |
| 2641 | } |
| 2642 | instr->state = MTD_ERASE_DONE; |
| 2643 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2644 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | |
| 2646 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | |
| 2648 | /* Deselect and wake up anyone waiting on the device */ |
| 2649 | nand_release_device(mtd); |
| 2650 | |
David Woodhouse | 49defc0 | 2007-10-06 15:01:59 -0400 | [diff] [blame] | 2651 | /* Do call back function */ |
| 2652 | if (!ret) |
| 2653 | mtd_erase_callback(instr); |
| 2654 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2655 | /* |
| 2656 | * If BBT requires refresh and erase was successful, rewrite any |
| 2657 | * selected bad block tables |
| 2658 | */ |
| 2659 | if (bbt_masked_page == 0xffffffff || ret) |
| 2660 | return ret; |
| 2661 | |
| 2662 | for (chipnr = 0; chipnr < chip->numchips; chipnr++) { |
| 2663 | if (!rewrite_bbt[chipnr]) |
| 2664 | continue; |
| 2665 | /* update the BBT for chip */ |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2666 | DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt " |
| 2667 | "(%d:0x%0llx 0x%0x)\n", __func__, chipnr, |
| 2668 | rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2669 | nand_update_bbt(mtd, rewrite_bbt[chipnr]); |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | /* Return more or less happy */ |
| 2673 | return ret; |
| 2674 | } |
| 2675 | |
| 2676 | /** |
| 2677 | * nand_sync - [MTD Interface] sync |
| 2678 | * @mtd: MTD device structure |
| 2679 | * |
| 2680 | * Sync is actually a wait for chip ready function |
| 2681 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2682 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2684 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2686 | DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | |
| 2688 | /* Grab the lock and see if the device is available */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2689 | nand_get_device(chip, mtd, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | /* Release it and go back */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2691 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | } |
| 2693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2695 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | * @mtd: MTD device structure |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 2697 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2699 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | { |
| 2701 | /* Check for invalid offset */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2702 | if (offs > mtd->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | return -EINVAL; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2704 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2705 | return nand_block_checkbad(mtd, offs, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2709 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | * @mtd: MTD device structure |
| 2711 | * @ofs: offset relative to mtd start |
| 2712 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2713 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2715 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | int ret; |
| 2717 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2718 | ret = nand_block_isbad(mtd, ofs); |
| 2719 | if (ret) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2720 | /* If it was bad already, return success and do nothing. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | if (ret > 0) |
| 2722 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2723 | return ret; |
| 2724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2726 | return chip->block_markbad(mtd, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2730 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
| 2731 | * @mtd: MTD device structure |
| 2732 | */ |
| 2733 | static int nand_suspend(struct mtd_info *mtd) |
| 2734 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2735 | struct nand_chip *chip = mtd->priv; |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2736 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2737 | return nand_get_device(chip, mtd, FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | /** |
| 2741 | * nand_resume - [MTD Interface] Resume the NAND flash |
| 2742 | * @mtd: MTD device structure |
| 2743 | */ |
| 2744 | static void nand_resume(struct mtd_info *mtd) |
| 2745 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2746 | struct nand_chip *chip = mtd->priv; |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2747 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2748 | if (chip->state == FL_PM_SUSPENDED) |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2749 | nand_release_device(mtd); |
| 2750 | else |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2751 | printk(KERN_ERR "%s called for a chip which is not " |
| 2752 | "in suspended state\n", __func__); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2753 | } |
| 2754 | |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 2755 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2756 | * Set default functions |
| 2757 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2758 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2759 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | /* check for proper chip_delay setup, set 20us if not */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2761 | if (!chip->chip_delay) |
| 2762 | chip->chip_delay = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | |
| 2764 | /* check, if a user supplied command function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2765 | if (chip->cmdfunc == NULL) |
| 2766 | chip->cmdfunc = nand_command; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | |
| 2768 | /* check, if a user supplied wait function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2769 | if (chip->waitfunc == NULL) |
| 2770 | chip->waitfunc = nand_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2772 | if (!chip->select_chip) |
| 2773 | chip->select_chip = nand_select_chip; |
| 2774 | if (!chip->read_byte) |
| 2775 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 2776 | if (!chip->read_word) |
| 2777 | chip->read_word = nand_read_word; |
| 2778 | if (!chip->block_bad) |
| 2779 | chip->block_bad = nand_block_bad; |
| 2780 | if (!chip->block_markbad) |
| 2781 | chip->block_markbad = nand_default_block_markbad; |
| 2782 | if (!chip->write_buf) |
| 2783 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
| 2784 | if (!chip->read_buf) |
| 2785 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
| 2786 | if (!chip->verify_buf) |
| 2787 | chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf; |
| 2788 | if (!chip->scan_bbt) |
| 2789 | chip->scan_bbt = nand_default_bbt; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2790 | |
| 2791 | if (!chip->controller) { |
| 2792 | chip->controller = &chip->hwcontrol; |
| 2793 | spin_lock_init(&chip->controller->lock); |
| 2794 | init_waitqueue_head(&chip->controller->wq); |
| 2795 | } |
| 2796 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | /* |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 2800 | * sanitize ONFI strings so we can safely print them |
| 2801 | */ |
| 2802 | static void sanitize_string(uint8_t *s, size_t len) |
| 2803 | { |
| 2804 | ssize_t i; |
| 2805 | |
| 2806 | /* null terminate */ |
| 2807 | s[len - 1] = 0; |
| 2808 | |
| 2809 | /* remove non printable chars */ |
| 2810 | for (i = 0; i < len - 1; i++) { |
| 2811 | if (s[i] < ' ' || s[i] > 127) |
| 2812 | s[i] = '?'; |
| 2813 | } |
| 2814 | |
| 2815 | /* remove trailing spaces */ |
| 2816 | strim(s); |
| 2817 | } |
| 2818 | |
| 2819 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 2820 | { |
| 2821 | int i; |
| 2822 | while (len--) { |
| 2823 | crc ^= *p++ << 8; |
| 2824 | for (i = 0; i < 8; i++) |
| 2825 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 2826 | } |
| 2827 | |
| 2828 | return crc; |
| 2829 | } |
| 2830 | |
| 2831 | /* |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 2832 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise |
| 2833 | */ |
| 2834 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, |
| 2835 | int busw) |
| 2836 | { |
| 2837 | struct nand_onfi_params *p = &chip->onfi_params; |
| 2838 | int i; |
| 2839 | int val; |
| 2840 | |
| 2841 | /* try ONFI for unknow chip or LP */ |
| 2842 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
| 2843 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || |
| 2844 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') |
| 2845 | return 0; |
| 2846 | |
| 2847 | printk(KERN_INFO "ONFI flash detected\n"); |
| 2848 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 2849 | for (i = 0; i < 3; i++) { |
| 2850 | chip->read_buf(mtd, (uint8_t *)p, sizeof(*p)); |
| 2851 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
| 2852 | le16_to_cpu(p->crc)) { |
| 2853 | printk(KERN_INFO "ONFI param page %d valid\n", i); |
| 2854 | break; |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | if (i == 3) |
| 2859 | return 0; |
| 2860 | |
| 2861 | /* check version */ |
| 2862 | val = le16_to_cpu(p->revision); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 2863 | if (val & (1 << 5)) |
| 2864 | chip->onfi_version = 23; |
| 2865 | else if (val & (1 << 4)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 2866 | chip->onfi_version = 22; |
| 2867 | else if (val & (1 << 3)) |
| 2868 | chip->onfi_version = 21; |
| 2869 | else if (val & (1 << 2)) |
| 2870 | chip->onfi_version = 20; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 2871 | else if (val & (1 << 1)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 2872 | chip->onfi_version = 10; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 2873 | else |
| 2874 | chip->onfi_version = 0; |
| 2875 | |
| 2876 | if (!chip->onfi_version) { |
| 2877 | printk(KERN_INFO "%s: unsupported ONFI version: %d\n", |
| 2878 | __func__, val); |
| 2879 | return 0; |
| 2880 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 2881 | |
| 2882 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 2883 | sanitize_string(p->model, sizeof(p->model)); |
| 2884 | if (!mtd->name) |
| 2885 | mtd->name = p->model; |
| 2886 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 2887 | mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize; |
| 2888 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
David Woodhouse | 4ccb3b4 | 2010-12-03 16:36:34 +0000 | [diff] [blame] | 2889 | chip->chipsize = (uint64_t)le32_to_cpu(p->blocks_per_lun) * mtd->erasesize; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 2890 | busw = 0; |
| 2891 | if (le16_to_cpu(p->features) & 1) |
| 2892 | busw = NAND_BUSWIDTH_16; |
| 2893 | |
| 2894 | chip->options &= ~NAND_CHIPOPTIONS_MSK; |
| 2895 | chip->options |= (NAND_NO_READRDY | |
| 2896 | NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK; |
| 2897 | |
| 2898 | return 1; |
| 2899 | } |
| 2900 | |
| 2901 | /* |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2902 | * Get the flash and manufacturer id and lookup if the type is supported |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2903 | */ |
| 2904 | static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2905 | struct nand_chip *chip, |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2906 | int busw, |
| 2907 | int *maf_id, int *dev_id, |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 2908 | struct nand_flash_dev *type) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2909 | { |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 2910 | int i, maf_idx; |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 2911 | u8 id_data[8]; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 2912 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | |
| 2914 | /* Select the device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2915 | chip->select_chip(mtd, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 2917 | /* |
| 2918 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
| 2919 | * after power-up |
| 2920 | */ |
| 2921 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 2922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | /* Send the command for reading device ID */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2924 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | |
| 2926 | /* Read manufacturer and device IDs */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2927 | *maf_id = chip->read_byte(mtd); |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 2928 | *dev_id = chip->read_byte(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 2930 | /* Try again to make sure, as some systems the bus-hold or other |
| 2931 | * interface concerns can cause random data which looks like a |
| 2932 | * possibly credible NAND flash to appear. If the two results do |
| 2933 | * not match, ignore the device completely. |
| 2934 | */ |
| 2935 | |
| 2936 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 2937 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 2938 | for (i = 0; i < 2; i++) |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 2939 | id_data[i] = chip->read_byte(mtd); |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 2940 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 2941 | if (id_data[0] != *maf_id || id_data[1] != *dev_id) { |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 2942 | printk(KERN_INFO "%s: second ID read did not match " |
| 2943 | "%02x,%02x against %02x,%02x\n", __func__, |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 2944 | *maf_id, *dev_id, id_data[0], id_data[1]); |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 2945 | return ERR_PTR(-ENODEV); |
| 2946 | } |
| 2947 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2948 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 2949 | type = nand_flash_ids; |
| 2950 | |
| 2951 | for (; type->name != NULL; type++) |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 2952 | if (*dev_id == type->id) |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2953 | break; |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 2954 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 2955 | chip->onfi_version = 0; |
| 2956 | if (!type->name || !type->pagesize) { |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 2957 | /* Check is chip is ONFI compliant */ |
| 2958 | ret = nand_flash_detect_onfi(mtd, chip, busw); |
| 2959 | if (ret) |
| 2960 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 2964 | |
| 2965 | /* Read entire ID string */ |
| 2966 | |
| 2967 | for (i = 0; i < 8; i++) |
| 2968 | id_data[i] = chip->read_byte(mtd); |
| 2969 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 2970 | if (!type->name) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2971 | return ERR_PTR(-ENODEV); |
| 2972 | |
Thomas Gleixner | ba0251f | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 2973 | if (!mtd->name) |
| 2974 | mtd->name = type->name; |
| 2975 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 2976 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2977 | |
Huang Shijie | 12a40a5 | 2010-09-27 10:43:53 +0800 | [diff] [blame] | 2978 | if (!type->pagesize && chip->init_size) { |
| 2979 | /* set the pagesize, oobsize, erasesize by the driver*/ |
| 2980 | busw = chip->init_size(mtd, chip, id_data); |
| 2981 | } else if (!type->pagesize) { |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2982 | int extid; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2983 | /* The 3rd id byte holds MLC / multichip data */ |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 2984 | chip->cellinfo = id_data[2]; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2985 | /* The 4th id byte is the important one */ |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 2986 | extid = id_data[3]; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 2987 | |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 2988 | /* |
| 2989 | * Field definitions are in the following datasheets: |
| 2990 | * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32) |
Brian Norris | 34c5bf6 | 2010-08-20 10:50:43 -0700 | [diff] [blame] | 2991 | * New style (6 byte ID): Samsung K9GBG08U0M (p.40) |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 2992 | * |
| 2993 | * Check for wraparound + Samsung ID + nonzero 6th byte |
| 2994 | * to decide what to do. |
| 2995 | */ |
| 2996 | if (id_data[0] == id_data[6] && id_data[1] == id_data[7] && |
| 2997 | id_data[0] == NAND_MFR_SAMSUNG && |
Tilman Sauerbeck | cfe3fda | 2010-08-20 14:01:47 -0700 | [diff] [blame] | 2998 | (chip->cellinfo & NAND_CI_CELLTYPE_MSK) && |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 2999 | id_data[5] != 0x00) { |
| 3000 | /* Calc pagesize */ |
| 3001 | mtd->writesize = 2048 << (extid & 0x03); |
| 3002 | extid >>= 2; |
| 3003 | /* Calc oobsize */ |
Brian Norris | 34c5bf6 | 2010-08-20 10:50:43 -0700 | [diff] [blame] | 3004 | switch (extid & 0x03) { |
| 3005 | case 1: |
| 3006 | mtd->oobsize = 128; |
| 3007 | break; |
| 3008 | case 2: |
| 3009 | mtd->oobsize = 218; |
| 3010 | break; |
| 3011 | case 3: |
| 3012 | mtd->oobsize = 400; |
| 3013 | break; |
| 3014 | default: |
| 3015 | mtd->oobsize = 436; |
| 3016 | break; |
| 3017 | } |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 3018 | extid >>= 2; |
| 3019 | /* Calc blocksize */ |
| 3020 | mtd->erasesize = (128 * 1024) << |
| 3021 | (((extid >> 1) & 0x04) | (extid & 0x03)); |
| 3022 | busw = 0; |
| 3023 | } else { |
| 3024 | /* Calc pagesize */ |
| 3025 | mtd->writesize = 1024 << (extid & 0x03); |
| 3026 | extid >>= 2; |
| 3027 | /* Calc oobsize */ |
| 3028 | mtd->oobsize = (8 << (extid & 0x01)) * |
| 3029 | (mtd->writesize >> 9); |
| 3030 | extid >>= 2; |
| 3031 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 3032 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 3033 | extid >>= 2; |
| 3034 | /* Get buswidth information */ |
| 3035 | busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; |
| 3036 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3037 | } else { |
| 3038 | /* |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3039 | * Old devices have chip data hardcoded in the device id table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3040 | */ |
Thomas Gleixner | ba0251f | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 3041 | mtd->erasesize = type->erasesize; |
| 3042 | mtd->writesize = type->pagesize; |
Thomas Gleixner | 4cbb9b8 | 2006-05-23 12:37:31 +0200 | [diff] [blame] | 3043 | mtd->oobsize = mtd->writesize / 32; |
Thomas Gleixner | ba0251f | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 3044 | busw = type->options & NAND_BUSWIDTH_16; |
Brian Norris | 2173bae | 2010-08-19 08:11:02 -0700 | [diff] [blame] | 3045 | |
| 3046 | /* |
| 3047 | * Check for Spansion/AMD ID + repeating 5th, 6th byte since |
| 3048 | * some Spansion chips have erasesize that conflicts with size |
| 3049 | * listed in nand_ids table |
| 3050 | * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39) |
| 3051 | */ |
| 3052 | if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && |
| 3053 | id_data[5] == 0x00 && id_data[6] == 0x00 && |
| 3054 | id_data[7] == 0x00 && mtd->writesize == 512) { |
| 3055 | mtd->erasesize = 128 * 1024; |
| 3056 | mtd->erasesize <<= ((id_data[3] & 0x03) << 1); |
| 3057 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3058 | } |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3059 | /* Get chip options, preserve non chip based options */ |
| 3060 | chip->options &= ~NAND_CHIPOPTIONS_MSK; |
| 3061 | chip->options |= type->options & NAND_CHIPOPTIONS_MSK; |
| 3062 | |
| 3063 | /* Check if chip is a not a samsung device. Do not clear the |
| 3064 | * options for chips which are not having an extended id. |
| 3065 | */ |
| 3066 | if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize) |
| 3067 | chip->options &= ~NAND_SAMSUNG_LP_OPTIONS; |
| 3068 | ident_done: |
| 3069 | |
| 3070 | /* |
| 3071 | * Set chip as a default. Board drivers can override it, if necessary |
| 3072 | */ |
| 3073 | chip->options |= NAND_NO_AUTOINCR; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3074 | |
| 3075 | /* Try to identify manufacturer */ |
David Woodhouse | 9a90986 | 2006-07-15 13:26:18 +0100 | [diff] [blame] | 3076 | for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) { |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3077 | if (nand_manuf_ids[maf_idx].id == *maf_id) |
| 3078 | break; |
| 3079 | } |
| 3080 | |
| 3081 | /* |
| 3082 | * Check, if buswidth is correct. Hardware drivers should set |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3083 | * chip correct ! |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3084 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3085 | if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3086 | printk(KERN_INFO "NAND device: Manufacturer ID:" |
| 3087 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3088 | *dev_id, nand_manuf_ids[maf_idx].name, mtd->name); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3089 | printk(KERN_WARNING "NAND bus width %d instead %d bit\n", |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3090 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3091 | busw ? 16 : 8); |
| 3092 | return ERR_PTR(-EINVAL); |
| 3093 | } |
| 3094 | |
| 3095 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3096 | chip->page_shift = ffs(mtd->writesize) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3097 | /* Convert chipsize to number of pages per chip -1. */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3098 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3099 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3100 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3101 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3102 | if (chip->chipsize & 0xffffffff) |
| 3103 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3104 | else { |
| 3105 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 3106 | chip->chip_shift += 32 - 1; |
| 3107 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3108 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame^] | 3109 | chip->badblockbits = 8; |
| 3110 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3111 | /* Set the bad block position */ |
Brian Norris | 065a1ed | 2010-08-18 11:25:04 -0700 | [diff] [blame] | 3112 | if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16)) |
Brian Norris | c7b28e2 | 2010-07-13 15:13:00 -0700 | [diff] [blame] | 3113 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
Brian Norris | 065a1ed | 2010-08-18 11:25:04 -0700 | [diff] [blame] | 3114 | else |
| 3115 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3116 | |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 3117 | /* |
| 3118 | * Bad block marker is stored in the last page of each block |
Brian Norris | c7b28e2 | 2010-07-13 15:13:00 -0700 | [diff] [blame] | 3119 | * on Samsung and Hynix MLC devices; stored in first two pages |
| 3120 | * of each block on Micron devices with 2KiB pages and on |
Brian Norris | 13ed7ae | 2010-08-20 12:36:12 -0700 | [diff] [blame] | 3121 | * SLC Samsung, Hynix, Toshiba and AMD/Spansion. All others scan |
| 3122 | * only the first page. |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 3123 | */ |
| 3124 | if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) && |
| 3125 | (*maf_id == NAND_MFR_SAMSUNG || |
| 3126 | *maf_id == NAND_MFR_HYNIX)) |
Brian Norris | 30fe811 | 2010-06-23 13:36:02 -0700 | [diff] [blame] | 3127 | chip->options |= NAND_BBT_SCANLASTPAGE; |
Brian Norris | c7b28e2 | 2010-07-13 15:13:00 -0700 | [diff] [blame] | 3128 | else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) && |
| 3129 | (*maf_id == NAND_MFR_SAMSUNG || |
| 3130 | *maf_id == NAND_MFR_HYNIX || |
Brian Norris | 13ed7ae | 2010-08-20 12:36:12 -0700 | [diff] [blame] | 3131 | *maf_id == NAND_MFR_TOSHIBA || |
Brian Norris | c7b28e2 | 2010-07-13 15:13:00 -0700 | [diff] [blame] | 3132 | *maf_id == NAND_MFR_AMD)) || |
| 3133 | (mtd->writesize == 2048 && |
| 3134 | *maf_id == NAND_MFR_MICRON)) |
| 3135 | chip->options |= NAND_BBT_SCAN2NDPAGE; |
| 3136 | |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 3137 | /* |
| 3138 | * Numonyx/ST 2K pages, x8 bus use BOTH byte 1 and 6 |
| 3139 | */ |
| 3140 | if (!(busw & NAND_BUSWIDTH_16) && |
| 3141 | *maf_id == NAND_MFR_STMICRO && |
| 3142 | mtd->writesize == 2048) { |
| 3143 | chip->options |= NAND_BBT_SCANBYTE1AND6; |
| 3144 | chip->badblockpos = 0; |
| 3145 | } |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 3146 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3147 | /* Check for AND chips with 4 page planes */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3148 | if (chip->options & NAND_4PAGE_ARRAY) |
| 3149 | chip->erase_cmd = multi_erase_cmd; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3150 | else |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3151 | chip->erase_cmd = single_erase_cmd; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3152 | |
| 3153 | /* Do not replace user supplied command function ! */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3154 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3155 | chip->cmdfunc = nand_command_lp; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3156 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3157 | /* TODO onfi flash name */ |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3158 | printk(KERN_INFO "NAND device: Manufacturer ID:" |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3159 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id, |
| 3160 | nand_manuf_ids[maf_idx].name, |
Brian Norris | 0b524fb | 2010-12-12 00:23:32 -0800 | [diff] [blame] | 3161 | chip->onfi_version ? chip->onfi_params.model : type->name); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3162 | |
| 3163 | return type; |
| 3164 | } |
| 3165 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3166 | /** |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3167 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
| 3168 | * @mtd: MTD device structure |
| 3169 | * @maxchips: Number of chips to scan for |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 3170 | * @table: Alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3171 | * |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3172 | * This is the first phase of the normal nand_scan() function. It |
| 3173 | * reads the flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3174 | * |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3175 | * The mtd->owner field must be set to the module of the caller. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3176 | */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 3177 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
| 3178 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3179 | { |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3180 | int i, busw, nand_maf_id, nand_dev_id; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3181 | struct nand_chip *chip = mtd->priv; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3182 | struct nand_flash_dev *type; |
| 3183 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3184 | /* Get buswidth to select the correct functions */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3185 | busw = chip->options & NAND_BUSWIDTH_16; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3186 | /* Set the default functions */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3187 | nand_set_defaults(chip, busw); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3188 | |
| 3189 | /* Read the flash type */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3190 | type = nand_get_flash_type(mtd, chip, busw, |
| 3191 | &nand_maf_id, &nand_dev_id, table); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3192 | |
| 3193 | if (IS_ERR(type)) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 3194 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
| 3195 | printk(KERN_WARNING "No NAND device found.\n"); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3196 | chip->select_chip(mtd, -1); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3197 | return PTR_ERR(type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | } |
| 3199 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3200 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3201 | for (i = 1; i < maxchips; i++) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3202 | chip->select_chip(mtd, i); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 3203 | /* See comment in nand_get_flash_type for reset */ |
| 3204 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | /* Send the command for reading device ID */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3206 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | /* Read manufacturer and device IDs */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3208 | if (nand_maf_id != chip->read_byte(mtd) || |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3209 | nand_dev_id != chip->read_byte(mtd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | break; |
| 3211 | } |
| 3212 | if (i > 1) |
| 3213 | printk(KERN_INFO "%d NAND chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3216 | chip->numchips = i; |
| 3217 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3219 | return 0; |
| 3220 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3221 | EXPORT_SYMBOL(nand_scan_ident); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3222 | |
| 3223 | |
| 3224 | /** |
| 3225 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
| 3226 | * @mtd: MTD device structure |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3227 | * |
| 3228 | * This is the second phase of the normal nand_scan() function. It |
| 3229 | * fills out all the uninitialized function pointers with the defaults |
| 3230 | * and scans for a bad block table if appropriate. |
| 3231 | */ |
| 3232 | int nand_scan_tail(struct mtd_info *mtd) |
| 3233 | { |
| 3234 | int i; |
| 3235 | struct nand_chip *chip = mtd->priv; |
| 3236 | |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 3237 | if (!(chip->options & NAND_OWN_BUFFERS)) |
| 3238 | chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL); |
| 3239 | if (!chip->buffers) |
| 3240 | return -ENOMEM; |
| 3241 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 3242 | /* Set the internal oob buffer location, just after the page data */ |
David Woodhouse | 784f4d5 | 2006-10-22 01:47:45 +0100 | [diff] [blame] | 3243 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3244 | |
| 3245 | /* |
| 3246 | * If no default placement scheme is given, select an appropriate one |
| 3247 | */ |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 3248 | if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3249 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | case 8: |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 3251 | chip->ecc.layout = &nand_oob_8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | break; |
| 3253 | case 16: |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 3254 | chip->ecc.layout = &nand_oob_16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | break; |
| 3256 | case 64: |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 3257 | chip->ecc.layout = &nand_oob_64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | break; |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 3259 | case 128: |
| 3260 | chip->ecc.layout = &nand_oob_128; |
| 3261 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | default: |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3263 | printk(KERN_WARNING "No oob scheme defined for " |
| 3264 | "oobsize %d\n", mtd->oobsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | BUG(); |
| 3266 | } |
| 3267 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3268 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3269 | if (!chip->write_page) |
| 3270 | chip->write_page = nand_write_page; |
| 3271 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3272 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3273 | * check ECC mode, default to software if 3byte/512byte hardware ECC is |
| 3274 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3275 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3276 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3277 | switch (chip->ecc.mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3278 | case NAND_ECC_HW_OOB_FIRST: |
| 3279 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
| 3280 | if (!chip->ecc.calculate || !chip->ecc.correct || |
| 3281 | !chip->ecc.hwctl) { |
| 3282 | printk(KERN_WARNING "No ECC functions supplied; " |
| 3283 | "Hardware ECC not possible\n"); |
| 3284 | BUG(); |
| 3285 | } |
| 3286 | if (!chip->ecc.read_page) |
| 3287 | chip->ecc.read_page = nand_read_page_hwecc_oob_first; |
| 3288 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 3289 | case NAND_ECC_HW: |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3290 | /* Use standard hwecc read page function ? */ |
| 3291 | if (!chip->ecc.read_page) |
| 3292 | chip->ecc.read_page = nand_read_page_hwecc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3293 | if (!chip->ecc.write_page) |
| 3294 | chip->ecc.write_page = nand_write_page_hwecc; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3295 | if (!chip->ecc.read_page_raw) |
| 3296 | chip->ecc.read_page_raw = nand_read_page_raw; |
| 3297 | if (!chip->ecc.write_page_raw) |
| 3298 | chip->ecc.write_page_raw = nand_write_page_raw; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3299 | if (!chip->ecc.read_oob) |
| 3300 | chip->ecc.read_oob = nand_read_oob_std; |
| 3301 | if (!chip->ecc.write_oob) |
| 3302 | chip->ecc.write_oob = nand_write_oob_std; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3303 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 3304 | case NAND_ECC_HW_SYNDROME: |
Scott Wood | 78b6517 | 2007-12-13 11:15:28 -0600 | [diff] [blame] | 3305 | if ((!chip->ecc.calculate || !chip->ecc.correct || |
| 3306 | !chip->ecc.hwctl) && |
| 3307 | (!chip->ecc.read_page || |
Scott Wood | 1c45f60 | 2008-01-16 10:36:03 -0600 | [diff] [blame] | 3308 | chip->ecc.read_page == nand_read_page_hwecc || |
Scott Wood | 78b6517 | 2007-12-13 11:15:28 -0600 | [diff] [blame] | 3309 | !chip->ecc.write_page || |
Scott Wood | 1c45f60 | 2008-01-16 10:36:03 -0600 | [diff] [blame] | 3310 | chip->ecc.write_page == nand_write_page_hwecc)) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3311 | printk(KERN_WARNING "No ECC functions supplied; " |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 3312 | "Hardware ECC not possible\n"); |
| 3313 | BUG(); |
| 3314 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3315 | /* Use standard syndrome read/write page function ? */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3316 | if (!chip->ecc.read_page) |
| 3317 | chip->ecc.read_page = nand_read_page_syndrome; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3318 | if (!chip->ecc.write_page) |
| 3319 | chip->ecc.write_page = nand_write_page_syndrome; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3320 | if (!chip->ecc.read_page_raw) |
| 3321 | chip->ecc.read_page_raw = nand_read_page_raw_syndrome; |
| 3322 | if (!chip->ecc.write_page_raw) |
| 3323 | chip->ecc.write_page_raw = nand_write_page_raw_syndrome; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3324 | if (!chip->ecc.read_oob) |
| 3325 | chip->ecc.read_oob = nand_read_oob_syndrome; |
| 3326 | if (!chip->ecc.write_oob) |
| 3327 | chip->ecc.write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3328 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3329 | if (mtd->writesize >= chip->ecc.size) |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 3330 | break; |
| 3331 | printk(KERN_WARNING "%d byte HW ECC not possible on " |
| 3332 | "%d byte page size, fallback to SW ECC\n", |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3333 | chip->ecc.size, mtd->writesize); |
| 3334 | chip->ecc.mode = NAND_ECC_SOFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 3336 | case NAND_ECC_SOFT: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3337 | chip->ecc.calculate = nand_calculate_ecc; |
| 3338 | chip->ecc.correct = nand_correct_data; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3339 | chip->ecc.read_page = nand_read_page_swecc; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3340 | chip->ecc.read_subpage = nand_read_subpage; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3341 | chip->ecc.write_page = nand_write_page_swecc; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3342 | chip->ecc.read_page_raw = nand_read_page_raw; |
| 3343 | chip->ecc.write_page_raw = nand_write_page_raw; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3344 | chip->ecc.read_oob = nand_read_oob_std; |
| 3345 | chip->ecc.write_oob = nand_write_oob_std; |
Singh, Vimal | 9a73290 | 2008-12-12 00:10:57 +0000 | [diff] [blame] | 3346 | if (!chip->ecc.size) |
| 3347 | chip->ecc.size = 256; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3348 | chip->ecc.bytes = 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | break; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3350 | |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 3351 | case NAND_ECC_SOFT_BCH: |
| 3352 | if (!mtd_nand_has_bch()) { |
| 3353 | printk(KERN_WARNING "CONFIG_MTD_ECC_BCH not enabled\n"); |
| 3354 | BUG(); |
| 3355 | } |
| 3356 | chip->ecc.calculate = nand_bch_calculate_ecc; |
| 3357 | chip->ecc.correct = nand_bch_correct_data; |
| 3358 | chip->ecc.read_page = nand_read_page_swecc; |
| 3359 | chip->ecc.read_subpage = nand_read_subpage; |
| 3360 | chip->ecc.write_page = nand_write_page_swecc; |
| 3361 | chip->ecc.read_page_raw = nand_read_page_raw; |
| 3362 | chip->ecc.write_page_raw = nand_write_page_raw; |
| 3363 | chip->ecc.read_oob = nand_read_oob_std; |
| 3364 | chip->ecc.write_oob = nand_write_oob_std; |
| 3365 | /* |
| 3366 | * Board driver should supply ecc.size and ecc.bytes values to |
| 3367 | * select how many bits are correctable; see nand_bch_init() |
| 3368 | * for details. |
| 3369 | * Otherwise, default to 4 bits for large page devices |
| 3370 | */ |
| 3371 | if (!chip->ecc.size && (mtd->oobsize >= 64)) { |
| 3372 | chip->ecc.size = 512; |
| 3373 | chip->ecc.bytes = 7; |
| 3374 | } |
| 3375 | chip->ecc.priv = nand_bch_init(mtd, |
| 3376 | chip->ecc.size, |
| 3377 | chip->ecc.bytes, |
| 3378 | &chip->ecc.layout); |
| 3379 | if (!chip->ecc.priv) { |
| 3380 | printk(KERN_WARNING "BCH ECC initialization failed!\n"); |
| 3381 | BUG(); |
| 3382 | } |
| 3383 | break; |
| 3384 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3385 | case NAND_ECC_NONE: |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3386 | printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. " |
| 3387 | "This is not recommended !!\n"); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3388 | chip->ecc.read_page = nand_read_page_raw; |
| 3389 | chip->ecc.write_page = nand_write_page_raw; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3390 | chip->ecc.read_oob = nand_read_oob_std; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3391 | chip->ecc.read_page_raw = nand_read_page_raw; |
| 3392 | chip->ecc.write_page_raw = nand_write_page_raw; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3393 | chip->ecc.write_oob = nand_write_oob_std; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3394 | chip->ecc.size = mtd->writesize; |
| 3395 | chip->ecc.bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3396 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | default: |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3399 | printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n", |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3400 | chip->ecc.mode); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3401 | BUG(); |
| 3402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3404 | /* |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 3405 | * The number of bytes available for a client to place data into |
| 3406 | * the out of band area |
| 3407 | */ |
| 3408 | chip->ecc.layout->oobavail = 0; |
David Brownell | 81d19b0 | 2009-04-21 19:51:20 -0700 | [diff] [blame] | 3409 | for (i = 0; chip->ecc.layout->oobfree[i].length |
| 3410 | && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++) |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 3411 | chip->ecc.layout->oobavail += |
| 3412 | chip->ecc.layout->oobfree[i].length; |
Vitaly Wool | 1f92267 | 2007-03-06 16:56:34 +0300 | [diff] [blame] | 3413 | mtd->oobavail = chip->ecc.layout->oobavail; |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 3414 | |
| 3415 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3416 | * Set the number of read / write steps for one page depending on ECC |
| 3417 | * mode |
| 3418 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3419 | chip->ecc.steps = mtd->writesize / chip->ecc.size; |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3420 | if (chip->ecc.steps * chip->ecc.size != mtd->writesize) { |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 3421 | printk(KERN_WARNING "Invalid ecc parameters\n"); |
| 3422 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3424 | chip->ecc.total = chip->ecc.steps * chip->ecc.bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3425 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3426 | /* |
| 3427 | * Allow subpage writes up to ecc.steps. Not possible for MLC |
| 3428 | * FLASH. |
| 3429 | */ |
| 3430 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3431 | !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) { |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3432 | switch (chip->ecc.steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3433 | case 2: |
| 3434 | mtd->subpage_sft = 1; |
| 3435 | break; |
| 3436 | case 4: |
| 3437 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 3438 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3439 | mtd->subpage_sft = 2; |
| 3440 | break; |
| 3441 | } |
| 3442 | } |
| 3443 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 3444 | |
Thomas Gleixner | 04bbd0e | 2006-05-25 09:45:29 +0200 | [diff] [blame] | 3445 | /* Initialize state */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3446 | chip->state = FL_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | |
| 3448 | /* De-select the device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3449 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | |
| 3451 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3452 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | |
| 3454 | /* Fill in remaining MTD driver data */ |
| 3455 | mtd->type = MTD_NANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 3456 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 3457 | MTD_CAP_NANDFLASH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | mtd->erase = nand_erase; |
| 3459 | mtd->point = NULL; |
| 3460 | mtd->unpoint = NULL; |
| 3461 | mtd->read = nand_read; |
| 3462 | mtd->write = nand_write; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3463 | mtd->panic_write = panic_nand_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | mtd->read_oob = nand_read_oob; |
| 3465 | mtd->write_oob = nand_write_oob; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | mtd->sync = nand_sync; |
| 3467 | mtd->lock = NULL; |
| 3468 | mtd->unlock = NULL; |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3469 | mtd->suspend = nand_suspend; |
| 3470 | mtd->resume = nand_resume; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | mtd->block_isbad = nand_block_isbad; |
| 3472 | mtd->block_markbad = nand_block_markbad; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 3473 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 3475 | /* propagate ecc.layout to mtd_info */ |
| 3476 | mtd->ecclayout = chip->ecc.layout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 3478 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3479 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 3480 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | |
| 3482 | /* Build bad block table */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3483 | return chip->scan_bbt(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3485 | EXPORT_SYMBOL(nand_scan_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | |
Rusty Russell | a6e6abd | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3487 | /* is_module_text_address() isn't exported, and it's mostly a pointless |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3488 | * test if this is a module _anyway_ -- they'd have to try _really_ hard |
| 3489 | * to call us from in-kernel code if the core NAND support is modular. */ |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3490 | #ifdef MODULE |
| 3491 | #define caller_is_module() (1) |
| 3492 | #else |
| 3493 | #define caller_is_module() \ |
Rusty Russell | a6e6abd | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3494 | is_module_text_address((unsigned long)__builtin_return_address(0)) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3495 | #endif |
| 3496 | |
| 3497 | /** |
| 3498 | * nand_scan - [NAND Interface] Scan for the NAND device |
| 3499 | * @mtd: MTD device structure |
| 3500 | * @maxchips: Number of chips to scan for |
| 3501 | * |
| 3502 | * This fills out all the uninitialized function pointers |
| 3503 | * with the defaults. |
| 3504 | * The flash ID is read and the mtd/chip structures are |
| 3505 | * filled with the appropriate values. |
| 3506 | * The mtd->owner field must be set to the module of the caller |
| 3507 | * |
| 3508 | */ |
| 3509 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 3510 | { |
| 3511 | int ret; |
| 3512 | |
| 3513 | /* Many callers got this wrong, so check for it for a while... */ |
| 3514 | if (!mtd->owner && caller_is_module()) { |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 3515 | printk(KERN_CRIT "%s called with NULL mtd->owner!\n", |
| 3516 | __func__); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3517 | BUG(); |
| 3518 | } |
| 3519 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 3520 | ret = nand_scan_ident(mtd, maxchips, NULL); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3521 | if (!ret) |
| 3522 | ret = nand_scan_tail(mtd); |
| 3523 | return ret; |
| 3524 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3525 | EXPORT_SYMBOL(nand_scan); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 3526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3528 | * nand_release - [NAND Interface] Free resources held by the NAND device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | * @mtd: MTD device structure |
| 3530 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3531 | void nand_release(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3533 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 3535 | if (chip->ecc.mode == NAND_ECC_SOFT_BCH) |
| 3536 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 3537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | #ifdef CONFIG_MTD_PARTITIONS |
| 3539 | /* Deregister partitions */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3540 | del_mtd_partitions(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | #endif |
| 3542 | /* Deregister the device */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3543 | del_mtd_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 3545 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3546 | kfree(chip->bbt); |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 3547 | if (!(chip->options & NAND_OWN_BUFFERS)) |
| 3548 | kfree(chip->buffers); |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 3549 | |
| 3550 | /* Free bad block descriptor memory */ |
| 3551 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 3552 | & NAND_BBT_DYNAMICSTRUCT) |
| 3553 | kfree(chip->badblock_pattern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3555 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 3556 | |
| 3557 | static int __init nand_base_init(void) |
| 3558 | { |
| 3559 | led_trigger_register_simple("nand-disk", &nand_led_trigger); |
| 3560 | return 0; |
| 3561 | } |
| 3562 | |
| 3563 | static void __exit nand_base_exit(void) |
| 3564 | { |
| 3565 | led_trigger_unregister_simple(nand_led_trigger); |
| 3566 | } |
| 3567 | |
| 3568 | module_init(nand_base_init); |
| 3569 | module_exit(nand_base_exit); |
| 3570 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3571 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3572 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 3573 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3574 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |