Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [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. |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 8 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 9 | * Additional technical information is available on |
| 10 | * http://www.linux-mtd.infradead.org/tech/nand.html |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 11 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 12 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 13 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 14 | * |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 15 | * Credits: |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 16 | * David Woodhouse for adding multichip support |
| 17 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 18 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 19 | * rework for 2K page size chips |
| 20 | * |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 21 | * TODO: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [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. |
| 27 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 28 | * This program is free software; you can redistribute it and/or modify |
| 29 | * it under the terms of the GNU General Public License version 2 as |
| 30 | * published by the Free Software Foundation. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | /* XXX U-BOOT XXX */ |
| 35 | #if 0 |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 36 | #include <linux/module.h> |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 37 | #include <linux/delay.h> |
| 38 | #include <linux/errno.h> |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 39 | #include <linux/err.h> |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 40 | #include <linux/sched.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/types.h> |
| 43 | #include <linux/mtd/mtd.h> |
| 44 | #include <linux/mtd/nand.h> |
| 45 | #include <linux/mtd/nand_ecc.h> |
| 46 | #include <linux/mtd/compatmac.h> |
| 47 | #include <linux/interrupt.h> |
| 48 | #include <linux/bitops.h> |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 49 | #include <linux/leds.h> |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 50 | #include <asm/io.h> |
| 51 | |
| 52 | #ifdef CONFIG_MTD_PARTITIONS |
| 53 | #include <linux/mtd/partitions.h> |
| 54 | #endif |
| 55 | |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 56 | #endif |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 57 | |
| 58 | #include <common.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 59 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 60 | #define ENOTSUPP 524 /* Operation is not supported */ |
| 61 | |
Jon Loeliger | cb51c0b | 2007-07-09 17:39:42 -0500 | [diff] [blame] | 62 | #if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 63 | |
| 64 | #include <malloc.h> |
| 65 | #include <watchdog.h> |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 66 | #include <linux/err.h> |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 67 | #include <linux/mtd/compat.h> |
| 68 | #include <linux/mtd/mtd.h> |
| 69 | #include <linux/mtd/nand.h> |
| 70 | #include <linux/mtd/nand_ecc.h> |
| 71 | |
| 72 | #include <asm/io.h> |
| 73 | #include <asm/errno.h> |
| 74 | |
| 75 | #ifdef CONFIG_JFFS2_NAND |
| 76 | #include <jffs2/jffs2.h> |
| 77 | #endif |
| 78 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 79 | /* Define default oob placement schemes for large and small page devices */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 80 | static struct nand_ecclayout nand_oob_8 = { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 81 | .eccbytes = 3, |
| 82 | .eccpos = {0, 1, 2}, |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 83 | .oobfree = { |
| 84 | {.offset = 3, |
| 85 | .length = 2}, |
| 86 | {.offset = 6, |
| 87 | .length = 2}} |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 88 | }; |
| 89 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 90 | static struct nand_ecclayout nand_oob_16 = { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 91 | .eccbytes = 6, |
| 92 | .eccpos = {0, 1, 2, 3, 6, 7}, |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 93 | .oobfree = { |
| 94 | {.offset = 8, |
| 95 | . length = 8}} |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 98 | static struct nand_ecclayout nand_oob_64 = { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 99 | .eccbytes = 24, |
| 100 | .eccpos = { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 101 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 102 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 103 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 104 | .oobfree = { |
| 105 | {.offset = 2, |
| 106 | .length = 38}} |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 109 | static struct nand_ecclayout nand_oob_128 = { |
Sergei Poselenov | 248ae5c | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 110 | .eccbytes = 48, |
| 111 | .eccpos = { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 112 | 80, 81, 82, 83, 84, 85, 86, 87, |
| 113 | 88, 89, 90, 91, 92, 93, 94, 95, |
| 114 | 96, 97, 98, 99, 100, 101, 102, 103, |
| 115 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 116 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 117 | 120, 121, 122, 123, 124, 125, 126, 127}, |
| 118 | .oobfree = { |
| 119 | {.offset = 2, |
| 120 | .length = 78}} |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 123 | |
| 124 | static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, |
| 125 | int new_state); |
| 126 | |
| 127 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 128 | struct mtd_oob_ops *ops); |
| 129 | |
| 130 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *this); |
Sergei Poselenov | 248ae5c | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 131 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 132 | /* |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 133 | * For devices which display every fart in the system on a seperate LED. Is |
| 134 | * compiled away when LED support is disabled. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 135 | */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 136 | /* XXX U-BOOT XXX */ |
| 137 | #if 0 |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 138 | DEFINE_LED_TRIGGER(nand_led_trigger); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 139 | #endif |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * nand_release_device - [GENERIC] release chip |
| 143 | * @mtd: MTD device structure |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 144 | * |
| 145 | * Deselect, release chip lock and wake up anyone waiting on the device |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 146 | */ |
| 147 | /* XXX U-BOOT XXX */ |
| 148 | #if 0 |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 149 | static void nand_release_device(struct mtd_info *mtd) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 150 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 151 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 152 | |
| 153 | /* De-select the NAND device */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 154 | chip->select_chip(mtd, -1); |
| 155 | |
| 156 | /* Release the controller and the chip */ |
| 157 | spin_lock(&chip->controller->lock); |
| 158 | chip->controller->active = NULL; |
| 159 | chip->state = FL_READY; |
| 160 | wake_up(&chip->controller->wq); |
| 161 | spin_unlock(&chip->controller->lock); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 162 | } |
| 163 | #else |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 164 | static void nand_release_device (struct mtd_info *mtd) |
| 165 | { |
| 166 | struct nand_chip *this = mtd->priv; |
| 167 | this->select_chip(mtd, -1); /* De-select the NAND device */ |
| 168 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 169 | #endif |
| 170 | |
| 171 | /** |
| 172 | * nand_read_byte - [DEFAULT] read one byte from the chip |
| 173 | * @mtd: MTD device structure |
| 174 | * |
| 175 | * Default read function for 8bit buswith |
| 176 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 177 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 178 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 179 | struct nand_chip *chip = mtd->priv; |
| 180 | return readb(chip->IO_ADDR_R); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip |
| 185 | * @mtd: MTD device structure |
| 186 | * |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 187 | * Default read function for 16bit buswith with |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 188 | * endianess conversion |
| 189 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 190 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 191 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 192 | struct nand_chip *chip = mtd->priv; |
| 193 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
| 197 | * nand_read_word - [DEFAULT] read one word from the chip |
| 198 | * @mtd: MTD device structure |
| 199 | * |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 200 | * Default read function for 16bit buswith without |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 201 | * endianess conversion |
| 202 | */ |
| 203 | static u16 nand_read_word(struct mtd_info *mtd) |
| 204 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 205 | struct nand_chip *chip = mtd->priv; |
| 206 | return readw(chip->IO_ADDR_R); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
| 210 | * nand_select_chip - [DEFAULT] control CE line |
| 211 | * @mtd: MTD device structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 212 | * @chipnr: chipnumber to select, -1 for deselect |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 213 | * |
| 214 | * Default select function for 1 chip devices. |
| 215 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 216 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 217 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 218 | struct nand_chip *chip = mtd->priv; |
| 219 | |
| 220 | switch (chipnr) { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 221 | case -1: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 222 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 223 | break; |
| 224 | case 0: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 225 | break; |
| 226 | |
| 227 | default: |
| 228 | BUG(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * nand_write_buf - [DEFAULT] write buffer to chip |
| 234 | * @mtd: MTD device structure |
| 235 | * @buf: data buffer |
| 236 | * @len: number of bytes to write |
| 237 | * |
| 238 | * Default write function for 8bit buswith |
| 239 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 240 | static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 241 | { |
| 242 | int i; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 243 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 244 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 245 | for (i = 0; i < len; i++) |
| 246 | writeb(buf[i], chip->IO_ADDR_W); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 250 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 251 | * @mtd: MTD device structure |
| 252 | * @buf: buffer to store date |
| 253 | * @len: number of bytes to read |
| 254 | * |
| 255 | * Default read function for 8bit buswith |
| 256 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 257 | static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 258 | { |
| 259 | int i; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 260 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 261 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 262 | for (i = 0; i < len; i++) |
| 263 | buf[i] = readb(chip->IO_ADDR_R); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 267 | * nand_verify_buf - [DEFAULT] Verify chip data against buffer |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 268 | * @mtd: MTD device structure |
| 269 | * @buf: buffer containing the data to compare |
| 270 | * @len: number of bytes to compare |
| 271 | * |
| 272 | * Default verify function for 8bit buswith |
| 273 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 274 | static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 275 | { |
| 276 | int i; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 277 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 278 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 279 | for (i = 0; i < len; i++) |
| 280 | if (buf[i] != readb(chip->IO_ADDR_R)) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 281 | return -EFAULT; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
| 287 | * @mtd: MTD device structure |
| 288 | * @buf: data buffer |
| 289 | * @len: number of bytes to write |
| 290 | * |
| 291 | * Default write function for 16bit buswith |
| 292 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 293 | static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 294 | { |
| 295 | int i; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 296 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 297 | u16 *p = (u16 *) buf; |
| 298 | len >>= 1; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 299 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 300 | for (i = 0; i < len; i++) |
| 301 | writew(p[i], chip->IO_ADDR_W); |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 302 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 306 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 307 | * @mtd: MTD device structure |
| 308 | * @buf: buffer to store date |
| 309 | * @len: number of bytes to read |
| 310 | * |
| 311 | * Default read function for 16bit buswith |
| 312 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 313 | static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 314 | { |
| 315 | int i; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 316 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 317 | u16 *p = (u16 *) buf; |
| 318 | len >>= 1; |
| 319 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 320 | for (i = 0; i < len; i++) |
| 321 | p[i] = readw(chip->IO_ADDR_R); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 325 | * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 326 | * @mtd: MTD device structure |
| 327 | * @buf: buffer containing the data to compare |
| 328 | * @len: number of bytes to compare |
| 329 | * |
| 330 | * Default verify function for 16bit buswith |
| 331 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 332 | static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 333 | { |
| 334 | int i; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 335 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 336 | u16 *p = (u16 *) buf; |
| 337 | len >>= 1; |
| 338 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 339 | for (i = 0; i < len; i++) |
| 340 | if (p[i] != readw(chip->IO_ADDR_R)) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 341 | return -EFAULT; |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
| 348 | * @mtd: MTD device structure |
| 349 | * @ofs: offset from device start |
| 350 | * @getchip: 0, if the chip is already selected |
| 351 | * |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 352 | * Check, if the block is bad. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 353 | */ |
| 354 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip) |
| 355 | { |
| 356 | int page, chipnr, res = 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 357 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 358 | u16 bad; |
| 359 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 360 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Thomas Knobloch | a798865 | 2007-05-05 07:04:42 +0200 | [diff] [blame] | 361 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 362 | if (getchip) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 363 | chipnr = (int)(ofs >> chip->chip_shift); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 364 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 365 | nand_get_device(chip, mtd, FL_READING); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 366 | |
| 367 | /* Select the NAND device */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 368 | chip->select_chip(mtd, chipnr); |
Thomas Knobloch | a798865 | 2007-05-05 07:04:42 +0200 | [diff] [blame] | 369 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 370 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 371 | if (chip->options & NAND_BUSWIDTH_16) { |
| 372 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE, |
| 373 | page); |
| 374 | bad = cpu_to_le16(chip->read_word(mtd)); |
| 375 | if (chip->badblockpos & 0x1) |
| 376 | bad >>= 8; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 377 | if ((bad & 0xFF) != 0xff) |
| 378 | res = 1; |
| 379 | } else { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 380 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page); |
| 381 | if (chip->read_byte(mtd) != 0xff) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 382 | res = 1; |
| 383 | } |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 384 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 385 | if (getchip) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 386 | nand_release_device(mtd); |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 387 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 388 | return res; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * nand_default_block_markbad - [DEFAULT] mark a block bad |
| 393 | * @mtd: MTD device structure |
| 394 | * @ofs: offset from device start |
| 395 | * |
| 396 | * This is the default implementation, which can be overridden by |
| 397 | * a hardware specific driver. |
| 398 | */ |
| 399 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 400 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 401 | struct nand_chip *chip = mtd->priv; |
| 402 | uint8_t buf[2] = { 0, 0 }; |
| 403 | int block, ret; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 404 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 405 | /* Get block number */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 406 | block = (int)(ofs >> chip->bbt_erase_shift); |
| 407 | if (chip->bbt) |
| 408 | chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 409 | |
| 410 | /* Do we have a flash based bad block table ? */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 411 | if (chip->options & NAND_USE_FLASH_BBT) |
| 412 | ret = nand_update_bbt(mtd, ofs); |
| 413 | else { |
| 414 | /* We write two bytes, so we dont have to mess with 16 bit |
| 415 | * access |
| 416 | */ |
| 417 | ofs += mtd->oobsize; |
| 418 | chip->ops.len = chip->ops.ooblen = 2; |
| 419 | chip->ops.datbuf = NULL; |
| 420 | chip->ops.oobbuf = buf; |
| 421 | chip->ops.ooboffs = chip->badblockpos & ~0x01; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 422 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 423 | ret = nand_do_write_oob(mtd, ofs, &chip->ops); |
| 424 | } |
| 425 | if (!ret) |
| 426 | mtd->ecc_stats.badblocks++; |
| 427 | return ret; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 430 | /** |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 431 | * nand_check_wp - [GENERIC] check if the chip is write protected |
| 432 | * @mtd: MTD device structure |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 433 | * Check, if the device is write protected |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 434 | * |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 435 | * The function expects, that the device is already selected |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 436 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 437 | static int nand_check_wp(struct mtd_info *mtd) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 438 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 439 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 440 | /* Check the WP bit */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 441 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 442 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 443 | } |
Markus Klotzbücher | 43638c6 | 2006-03-06 15:04:25 +0100 | [diff] [blame] | 444 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 445 | /** |
| 446 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 447 | * @mtd: MTD device structure |
| 448 | * @ofs: offset from device start |
| 449 | * @getchip: 0, if the chip is already selected |
| 450 | * @allowbbt: 1, if its allowed to access the bbt area |
| 451 | * |
| 452 | * Check, if the block is bad. Either by reading the bad block table or |
| 453 | * calling of the scan function. |
| 454 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 455 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, |
| 456 | int allowbbt) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 457 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 458 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 459 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 460 | if (!chip->bbt) |
| 461 | return chip->block_bad(mtd, ofs, getchip); |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 462 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 463 | /* Return info from the table */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 464 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 465 | } |
| 466 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 467 | /* |
| 468 | * Wait for the ready pin, after a command |
| 469 | * The timeout is catched later. |
| 470 | */ |
| 471 | /* XXX U-BOOT XXX */ |
| 472 | #if 0 |
| 473 | void nand_wait_ready(struct mtd_info *mtd) |
| 474 | { |
| 475 | struct nand_chip *chip = mtd->priv; |
| 476 | unsigned long timeo = jiffies + 2; |
| 477 | |
| 478 | led_trigger_event(nand_led_trigger, LED_FULL); |
| 479 | /* wait until command is processed or timeout occures */ |
| 480 | do { |
| 481 | if (chip->dev_ready(mtd)) |
| 482 | break; |
| 483 | touch_softlockup_watchdog(); |
| 484 | } while (time_before(jiffies, timeo)); |
| 485 | led_trigger_event(nand_led_trigger, LED_OFF); |
| 486 | } |
| 487 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
| 488 | #else |
| 489 | void nand_wait_ready(struct mtd_info *mtd) |
| 490 | { |
| 491 | struct nand_chip *chip = mtd->priv; |
Stefan Roese | 1207226 | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 492 | u32 timeo = (CFG_HZ * 20) / 1000; |
| 493 | |
| 494 | reset_timer(); |
| 495 | |
| 496 | /* wait until command is processed or timeout occures */ |
| 497 | while (get_timer(0) < timeo) { |
| 498 | if (chip->dev_ready) |
| 499 | if (chip->dev_ready(mtd)) |
| 500 | break; |
| 501 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 502 | } |
| 503 | #endif |
| 504 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 505 | /** |
| 506 | * nand_command - [DEFAULT] Send command to NAND device |
| 507 | * @mtd: MTD device structure |
| 508 | * @command: the command to be sent |
| 509 | * @column: the column address for this command, -1 if none |
| 510 | * @page_addr: the page address for this command, -1 if none |
| 511 | * |
| 512 | * Send command to NAND device. This function is used for small page |
| 513 | * devices (256/512 Bytes per page) |
| 514 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 515 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 516 | int column, int page_addr) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 517 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 518 | register struct nand_chip *chip = mtd->priv; |
| 519 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 520 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 521 | /* |
| 522 | * Write out the command to the device. |
| 523 | */ |
| 524 | if (command == NAND_CMD_SEQIN) { |
| 525 | int readcmd; |
| 526 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 527 | if (column >= mtd->writesize) { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 528 | /* OOB area */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 529 | column -= mtd->writesize; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 530 | readcmd = NAND_CMD_READOOB; |
| 531 | } else if (column < 256) { |
| 532 | /* First 256 bytes --> READ0 */ |
| 533 | readcmd = NAND_CMD_READ0; |
| 534 | } else { |
| 535 | column -= 256; |
| 536 | readcmd = NAND_CMD_READ1; |
| 537 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 538 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
| 539 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 540 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 541 | chip->cmd_ctrl(mtd, command, ctrl); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 542 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 543 | /* |
| 544 | * Address cycle, when necessary |
| 545 | */ |
| 546 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 547 | /* Serially input address */ |
| 548 | if (column != -1) { |
| 549 | /* Adjust columns for 16 bit buswidth */ |
| 550 | if (chip->options & NAND_BUSWIDTH_16) |
| 551 | column >>= 1; |
| 552 | chip->cmd_ctrl(mtd, column, ctrl); |
| 553 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 554 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 555 | if (page_addr != -1) { |
| 556 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 557 | ctrl &= ~NAND_CTRL_CHANGE; |
| 558 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
| 559 | /* One more address cycle for devices > 32MiB */ |
| 560 | if (chip->chipsize > (32 << 20)) |
| 561 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
| 562 | } |
| 563 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * program and erase have their own busy handlers |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 567 | * status and sequential in needs no delay |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 568 | */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 569 | switch (command) { |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 570 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 571 | case NAND_CMD_PAGEPROG: |
| 572 | case NAND_CMD_ERASE1: |
| 573 | case NAND_CMD_ERASE2: |
| 574 | case NAND_CMD_SEQIN: |
| 575 | case NAND_CMD_STATUS: |
| 576 | return; |
| 577 | |
| 578 | case NAND_CMD_RESET: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 579 | if (chip->dev_ready) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 580 | break; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 581 | udelay(chip->chip_delay); |
| 582 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 583 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 584 | chip->cmd_ctrl(mtd, |
| 585 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
| 586 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 587 | return; |
| 588 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 589 | /* This applies to read commands */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 590 | default: |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 591 | /* |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 592 | * If we don't have access to the busy pin, we apply the given |
| 593 | * command delay |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 594 | */ |
| 595 | if (!chip->dev_ready) { |
| 596 | udelay(chip->chip_delay); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 597 | return; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 598 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 599 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 600 | /* Apply this short delay always to ensure that we do wait tWB in |
| 601 | * any case on any machine. */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 602 | ndelay(100); |
| 603 | |
| 604 | nand_wait_ready(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /** |
| 608 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
| 609 | * @mtd: MTD device structure |
| 610 | * @command: the command to be sent |
| 611 | * @column: the column address for this command, -1 if none |
| 612 | * @page_addr: the page address for this command, -1 if none |
| 613 | * |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 614 | * Send command to NAND device. This is the version for the new large page |
| 615 | * devices We dont have the separate regions as we have in the small page |
| 616 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 617 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 618 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 619 | int column, int page_addr) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 620 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 621 | register struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 622 | |
| 623 | /* Emulate NAND_CMD_READOOB */ |
| 624 | if (command == NAND_CMD_READOOB) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 625 | column += mtd->writesize; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 626 | command = NAND_CMD_READ0; |
| 627 | } |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 628 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 629 | /* Command latch cycle */ |
| 630 | chip->cmd_ctrl(mtd, command & 0xff, |
| 631 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 632 | |
| 633 | if (column != -1 || page_addr != -1) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 634 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 635 | |
| 636 | /* Serially input address */ |
| 637 | if (column != -1) { |
| 638 | /* Adjust columns for 16 bit buswidth */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 639 | if (chip->options & NAND_BUSWIDTH_16) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 640 | column >>= 1; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 641 | chip->cmd_ctrl(mtd, column, ctrl); |
| 642 | ctrl &= ~NAND_CTRL_CHANGE; |
| 643 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 644 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 645 | if (page_addr != -1) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 646 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 647 | chip->cmd_ctrl(mtd, page_addr >> 8, |
| 648 | NAND_NCE | NAND_ALE); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 649 | /* One more address cycle for devices > 128MiB */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 650 | if (chip->chipsize > (128 << 20)) |
| 651 | chip->cmd_ctrl(mtd, page_addr >> 16, |
| 652 | NAND_NCE | NAND_ALE); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 653 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 654 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 655 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 656 | |
| 657 | /* |
| 658 | * program and erase have their own busy handlers |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 659 | * status, sequential in, and deplete1 need no delay |
| 660 | */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 661 | switch (command) { |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 662 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 663 | case NAND_CMD_CACHEDPROG: |
| 664 | case NAND_CMD_PAGEPROG: |
| 665 | case NAND_CMD_ERASE1: |
| 666 | case NAND_CMD_ERASE2: |
| 667 | case NAND_CMD_SEQIN: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 668 | case NAND_CMD_RNDIN: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 669 | case NAND_CMD_STATUS: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 670 | case NAND_CMD_DEPLETE1: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 671 | return; |
| 672 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 673 | /* |
| 674 | * read error status commands require only a short delay |
| 675 | */ |
| 676 | case NAND_CMD_STATUS_ERROR: |
| 677 | case NAND_CMD_STATUS_ERROR0: |
| 678 | case NAND_CMD_STATUS_ERROR1: |
| 679 | case NAND_CMD_STATUS_ERROR2: |
| 680 | case NAND_CMD_STATUS_ERROR3: |
| 681 | udelay(chip->chip_delay); |
| 682 | return; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 683 | |
| 684 | case NAND_CMD_RESET: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 685 | if (chip->dev_ready) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 686 | break; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 687 | udelay(chip->chip_delay); |
| 688 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 689 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 690 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 691 | NAND_NCE | NAND_CTRL_CHANGE); |
| 692 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ; |
| 693 | return; |
| 694 | |
| 695 | case NAND_CMD_RNDOUT: |
| 696 | /* No ready / busy check necessary */ |
| 697 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 698 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 699 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 700 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 701 | return; |
| 702 | |
| 703 | case NAND_CMD_READ0: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 704 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 705 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 706 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 707 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 708 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 709 | /* This applies to read commands */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 710 | default: |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 711 | /* |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 712 | * If we don't have access to the busy pin, we apply the given |
| 713 | * command delay |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 714 | */ |
| 715 | if (!chip->dev_ready) { |
| 716 | udelay(chip->chip_delay); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 717 | return; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 718 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 719 | } |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 720 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 721 | /* Apply this short delay always to ensure that we do wait tWB in |
| 722 | * any case on any machine. */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 723 | ndelay(100); |
| 724 | |
| 725 | nand_wait_ready(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /** |
| 729 | * nand_get_device - [GENERIC] Get chip for selected access |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 730 | * @chip: the nand chip descriptor |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 731 | * @mtd: MTD device structure |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 732 | * @new_state: the state which is requested |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 733 | * |
| 734 | * Get the device and lock it for exclusive access |
| 735 | */ |
| 736 | /* XXX U-BOOT XXX */ |
| 737 | #if 0 |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 738 | static int |
| 739 | nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 740 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 741 | spinlock_t *lock = &chip->controller->lock; |
| 742 | wait_queue_head_t *wq = &chip->controller->wq; |
| 743 | DECLARE_WAITQUEUE(wait, current); |
| 744 | retry: |
| 745 | spin_lock(lock); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 746 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 747 | /* Hardware controller shared among independend devices */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 748 | /* Hardware controller shared among independend devices */ |
| 749 | if (!chip->controller->active) |
| 750 | chip->controller->active = chip; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 751 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 752 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 753 | chip->state = new_state; |
| 754 | spin_unlock(lock); |
| 755 | return 0; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 756 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 757 | if (new_state == FL_PM_SUSPENDED) { |
| 758 | spin_unlock(lock); |
| 759 | return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN; |
| 760 | } |
| 761 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 762 | add_wait_queue(wq, &wait); |
| 763 | spin_unlock(lock); |
| 764 | schedule(); |
| 765 | remove_wait_queue(wq, &wait); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 766 | goto retry; |
| 767 | } |
| 768 | #else |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 769 | static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state) |
| 770 | { |
| 771 | return 0; |
| 772 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 773 | #endif |
| 774 | |
| 775 | /** |
| 776 | * nand_wait - [DEFAULT] wait until the command is done |
| 777 | * @mtd: MTD device structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 778 | * @chip: NAND chip structure |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 779 | * |
| 780 | * Wait for command done. This applies to erase and program only |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 781 | * Erase can take up to 400ms and program up to 20ms according to |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 782 | * general NAND and SmartMedia specs |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 783 | */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 784 | /* XXX U-BOOT XXX */ |
| 785 | #if 0 |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 786 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 787 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 788 | |
| 789 | unsigned long timeo = jiffies; |
| 790 | int status, state = chip->state; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 791 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 792 | if (state == FL_ERASING) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 793 | timeo += (HZ * 400) / 1000; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 794 | else |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 795 | timeo += (HZ * 20) / 1000; |
| 796 | |
| 797 | led_trigger_event(nand_led_trigger, LED_FULL); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 798 | |
| 799 | /* Apply this short delay always to ensure that we do wait tWB in |
| 800 | * any case on any machine. */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 801 | ndelay(100); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 802 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 803 | if ((state == FL_ERASING) && (chip->options & NAND_IS_AND)) |
| 804 | chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1); |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 805 | else |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 806 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 807 | |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 808 | while (time_before(jiffies, timeo)) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 809 | if (chip->dev_ready) { |
| 810 | if (chip->dev_ready(mtd)) |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 811 | break; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 812 | } else { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 813 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 814 | break; |
| 815 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 816 | cond_resched(); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 817 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 818 | led_trigger_event(nand_led_trigger, LED_OFF); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 819 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 820 | status = (int)chip->read_byte(mtd); |
| 821 | return status; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 822 | } |
| 823 | #else |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 824 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *this) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 825 | { |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 826 | unsigned long timeo; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 827 | int state = this->state; |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 828 | |
| 829 | if (state == FL_ERASING) |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 830 | timeo = (CFG_HZ * 400) / 1000; |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 831 | else |
Stefan Roese | e7f3e9f | 2006-11-28 11:04:45 +0100 | [diff] [blame] | 832 | timeo = (CFG_HZ * 20) / 1000; |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 833 | |
| 834 | if ((state == FL_ERASING) && (this->options & NAND_IS_AND)) |
| 835 | this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1); |
| 836 | else |
| 837 | this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 838 | |
Bartlomiej Sieka | 038ccac | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 839 | reset_timer(); |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 840 | |
| 841 | while (1) { |
Bartlomiej Sieka | 038ccac | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 842 | if (get_timer(0) > timeo) { |
| 843 | printf("Timeout!"); |
Stefan Roese | 1578486 | 2006-11-27 17:22:19 +0100 | [diff] [blame] | 844 | return 0x01; |
| 845 | } |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 846 | |
| 847 | if (this->dev_ready) { |
| 848 | if (this->dev_ready(mtd)) |
| 849 | break; |
| 850 | } else { |
| 851 | if (this->read_byte(mtd) & NAND_STATUS_READY) |
| 852 | break; |
| 853 | } |
| 854 | } |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 855 | #ifdef PPCHAMELON_NAND_TIMER_HACK |
Bartlomiej Sieka | 038ccac | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 856 | reset_timer(); |
| 857 | while (get_timer(0) < 10); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 858 | #endif /* PPCHAMELON_NAND_TIMER_HACK */ |
Bartlomiej Sieka | 038ccac | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 859 | |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 860 | return this->read_byte(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 861 | } |
| 862 | #endif |
| 863 | |
| 864 | /** |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 865 | * nand_read_page_raw - [Intern] read raw page data without ecc |
| 866 | * @mtd: mtd info structure |
| 867 | * @chip: nand chip info structure |
| 868 | * @buf: buffer to store read data |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 869 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 870 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 871 | uint8_t *buf) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 872 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 873 | chip->read_buf(mtd, buf, mtd->writesize); |
| 874 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 875 | return 0; |
| 876 | } |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 877 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 878 | /** |
| 879 | * nand_read_page_swecc - [REPLACABLE] software ecc based page read function |
| 880 | * @mtd: mtd info structure |
| 881 | * @chip: nand chip info structure |
| 882 | * @buf: buffer to store read data |
| 883 | */ |
| 884 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 885 | uint8_t *buf) |
| 886 | { |
| 887 | int i, eccsize = chip->ecc.size; |
| 888 | int eccbytes = chip->ecc.bytes; |
| 889 | int eccsteps = chip->ecc.steps; |
| 890 | uint8_t *p = buf; |
| 891 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 892 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 893 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 894 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 895 | chip->ecc.read_page_raw(mtd, chip, buf); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 896 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 897 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 898 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 899 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 900 | for (i = 0; i < chip->ecc.total; i++) |
| 901 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 902 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 903 | eccsteps = chip->ecc.steps; |
| 904 | p = buf; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 905 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 906 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 907 | int stat; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 908 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 909 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
| 910 | if (stat == -1) |
| 911 | mtd->ecc_stats.failed++; |
| 912 | else |
| 913 | mtd->ecc_stats.corrected += stat; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 914 | } |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 915 | return 0; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 916 | } |
| 917 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 918 | /** |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 919 | * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function |
| 920 | * @mtd: mtd info structure |
| 921 | * @chip: nand chip info structure |
| 922 | * @buf: buffer to store read data |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 923 | * |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 924 | * Not for syndrome calculating ecc controllers which need a special oob layout |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 925 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 926 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 927 | uint8_t *buf) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 928 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 929 | int i, eccsize = chip->ecc.size; |
| 930 | int eccbytes = chip->ecc.bytes; |
| 931 | int eccsteps = chip->ecc.steps; |
| 932 | uint8_t *p = buf; |
| 933 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 934 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 935 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 936 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 937 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 938 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 939 | chip->read_buf(mtd, p, eccsize); |
| 940 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 941 | } |
| 942 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 943 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 944 | for (i = 0; i < chip->ecc.total; i++) |
| 945 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 946 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 947 | eccsteps = chip->ecc.steps; |
| 948 | p = buf; |
| 949 | |
| 950 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 951 | int stat; |
| 952 | |
| 953 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
| 954 | if (stat == -1) |
| 955 | mtd->ecc_stats.failed++; |
| 956 | else |
| 957 | mtd->ecc_stats.corrected += stat; |
| 958 | } |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | /** |
| 963 | * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read |
| 964 | * @mtd: mtd info structure |
| 965 | * @chip: nand chip info structure |
| 966 | * @buf: buffer to store read data |
| 967 | * |
| 968 | * The hw generator calculates the error syndrome automatically. Therefor |
| 969 | * we need a special oob layout and handling. |
| 970 | */ |
| 971 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 972 | uint8_t *buf) |
| 973 | { |
| 974 | int i, eccsize = chip->ecc.size; |
| 975 | int eccbytes = chip->ecc.bytes; |
| 976 | int eccsteps = chip->ecc.steps; |
| 977 | uint8_t *p = buf; |
| 978 | uint8_t *oob = chip->oob_poi; |
| 979 | |
| 980 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 981 | int stat; |
| 982 | |
| 983 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 984 | chip->read_buf(mtd, p, eccsize); |
| 985 | |
| 986 | if (chip->ecc.prepad) { |
| 987 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 988 | oob += chip->ecc.prepad; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 989 | } |
| 990 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 991 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
| 992 | chip->read_buf(mtd, oob, eccbytes); |
| 993 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 994 | |
| 995 | if (stat == -1) |
| 996 | mtd->ecc_stats.failed++; |
| 997 | else |
| 998 | mtd->ecc_stats.corrected += stat; |
| 999 | |
| 1000 | oob += eccbytes; |
| 1001 | |
| 1002 | if (chip->ecc.postpad) { |
| 1003 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1004 | oob += chip->ecc.postpad; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /* Calculate remaining oob bytes */ |
| 1009 | i = mtd->oobsize - (oob - chip->oob_poi); |
| 1010 | if (i) |
| 1011 | chip->read_buf(mtd, oob, i); |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | /** |
| 1017 | * nand_transfer_oob - [Internal] Transfer oob to client buffer |
| 1018 | * @chip: nand chip structure |
| 1019 | * @oob: oob destination address |
| 1020 | * @ops: oob ops structure |
| 1021 | * @len: size of oob to transfer |
| 1022 | */ |
| 1023 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, |
| 1024 | struct mtd_oob_ops *ops, size_t len) |
| 1025 | { |
| 1026 | switch(ops->mode) { |
| 1027 | |
| 1028 | case MTD_OOB_PLACE: |
| 1029 | case MTD_OOB_RAW: |
| 1030 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 1031 | return oob + len; |
| 1032 | |
| 1033 | case MTD_OOB_AUTO: { |
| 1034 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 1035 | uint32_t boffs = 0, roffs = ops->ooboffs; |
| 1036 | size_t bytes = 0; |
| 1037 | |
| 1038 | for(; free->length && len; free++, len -= bytes) { |
| 1039 | /* Read request not from offset 0 ? */ |
| 1040 | if (unlikely(roffs)) { |
| 1041 | if (roffs >= free->length) { |
| 1042 | roffs -= free->length; |
| 1043 | continue; |
| 1044 | } |
| 1045 | boffs = free->offset + roffs; |
| 1046 | bytes = min_t(size_t, len, |
| 1047 | (free->length - roffs)); |
| 1048 | roffs = 0; |
| 1049 | } else { |
| 1050 | bytes = min_t(size_t, len, free->length); |
| 1051 | boffs = free->offset; |
| 1052 | } |
| 1053 | memcpy(oob, chip->oob_poi + boffs, bytes); |
| 1054 | oob += bytes; |
| 1055 | } |
| 1056 | return oob; |
| 1057 | } |
| 1058 | default: |
| 1059 | BUG(); |
| 1060 | } |
| 1061 | return NULL; |
| 1062 | } |
| 1063 | |
| 1064 | /** |
| 1065 | * nand_do_read_ops - [Internal] Read data with ECC |
| 1066 | * |
| 1067 | * @mtd: MTD device structure |
| 1068 | * @from: offset to read from |
| 1069 | * @ops: oob ops structure |
| 1070 | * |
| 1071 | * Internal function. Called with chip held. |
| 1072 | */ |
| 1073 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 1074 | struct mtd_oob_ops *ops) |
| 1075 | { |
| 1076 | int chipnr, page, realpage, col, bytes, aligned; |
| 1077 | struct nand_chip *chip = mtd->priv; |
| 1078 | struct mtd_ecc_stats stats; |
| 1079 | int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 1080 | int sndcmd = 1; |
| 1081 | int ret = 0; |
| 1082 | uint32_t readlen = ops->len; |
| 1083 | uint32_t oobreadlen = ops->ooblen; |
| 1084 | uint8_t *bufpoi, *oob, *buf; |
| 1085 | |
| 1086 | stats = mtd->ecc_stats; |
| 1087 | |
| 1088 | chipnr = (int)(from >> chip->chip_shift); |
| 1089 | chip->select_chip(mtd, chipnr); |
| 1090 | |
| 1091 | realpage = (int)(from >> chip->page_shift); |
| 1092 | page = realpage & chip->pagemask; |
| 1093 | |
| 1094 | col = (int)(from & (mtd->writesize - 1)); |
| 1095 | |
| 1096 | buf = ops->datbuf; |
| 1097 | oob = ops->oobbuf; |
| 1098 | |
| 1099 | while(1) { |
| 1100 | bytes = min(mtd->writesize - col, readlen); |
| 1101 | aligned = (bytes == mtd->writesize); |
| 1102 | |
| 1103 | /* Is the current page in the buffer ? */ |
| 1104 | if (realpage != chip->pagebuf || oob) { |
| 1105 | bufpoi = aligned ? buf : chip->buffers->databuf; |
| 1106 | |
| 1107 | if (likely(sndcmd)) { |
| 1108 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
| 1109 | sndcmd = 0; |
| 1110 | } |
| 1111 | |
| 1112 | /* Now read the page into the buffer */ |
| 1113 | if (unlikely(ops->mode == MTD_OOB_RAW)) |
| 1114 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi); |
| 1115 | else |
| 1116 | ret = chip->ecc.read_page(mtd, chip, bufpoi); |
| 1117 | if (ret < 0) |
| 1118 | break; |
| 1119 | |
| 1120 | /* Transfer not aligned data */ |
| 1121 | if (!aligned) { |
| 1122 | chip->pagebuf = realpage; |
| 1123 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 1124 | } |
| 1125 | |
| 1126 | buf += bytes; |
| 1127 | |
| 1128 | if (unlikely(oob)) { |
| 1129 | /* Raw mode does data:oob:data:oob */ |
| 1130 | if (ops->mode != MTD_OOB_RAW) { |
| 1131 | int toread = min(oobreadlen, |
| 1132 | chip->ecc.layout->oobavail); |
| 1133 | if (toread) { |
| 1134 | oob = nand_transfer_oob(chip, |
| 1135 | oob, ops, toread); |
| 1136 | oobreadlen -= toread; |
| 1137 | } |
| 1138 | } else |
| 1139 | buf = nand_transfer_oob(chip, |
| 1140 | buf, ops, mtd->oobsize); |
| 1141 | } |
| 1142 | |
| 1143 | if (!(chip->options & NAND_NO_READRDY)) { |
| 1144 | /* |
| 1145 | * Apply delay or wait for ready/busy pin. Do |
| 1146 | * this before the AUTOINCR check, so no |
| 1147 | * problems arise if a chip which does auto |
| 1148 | * increment is marked as NOAUTOINCR by the |
| 1149 | * board driver. |
| 1150 | */ |
| 1151 | if (!chip->dev_ready) |
| 1152 | udelay(chip->chip_delay); |
| 1153 | else |
| 1154 | nand_wait_ready(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1155 | } |
| 1156 | } else { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1157 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 1158 | buf += bytes; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1159 | } |
| 1160 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1161 | readlen -= bytes; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1162 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1163 | if (!readlen) |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1164 | break; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1165 | |
| 1166 | /* For subsequent reads align to page boundary. */ |
| 1167 | col = 0; |
| 1168 | /* Increment page address */ |
| 1169 | realpage++; |
| 1170 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1171 | page = realpage & chip->pagemask; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1172 | /* Check, if we cross a chip boundary */ |
| 1173 | if (!page) { |
| 1174 | chipnr++; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1175 | chip->select_chip(mtd, -1); |
| 1176 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1177 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1178 | |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1179 | /* Check, if the chip supports auto page increment |
| 1180 | * or if we have hit a block boundary. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1181 | */ |
| 1182 | if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck)) |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1183 | sndcmd = 1; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1186 | ops->retlen = ops->len - (size_t) readlen; |
| 1187 | if (oob) |
| 1188 | ops->oobretlen = ops->ooblen - oobreadlen; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1189 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1190 | if (ret) |
| 1191 | return ret; |
| 1192 | |
| 1193 | if (mtd->ecc_stats.failed - stats.failed) |
| 1194 | return -EBADMSG; |
| 1195 | |
| 1196 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /** |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1200 | * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1201 | * @mtd: MTD device structure |
| 1202 | * @from: offset to read from |
| 1203 | * @len: number of bytes to read |
| 1204 | * @retlen: pointer to variable to store the number of read bytes |
| 1205 | * @buf: the databuffer to put data |
| 1206 | * |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1207 | * Get hold of the chip and call nand_do_read |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1208 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1209 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 1210 | size_t *retlen, uint8_t *buf) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1211 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1212 | struct nand_chip *chip = mtd->priv; |
| 1213 | int ret; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1214 | |
| 1215 | /* Do not allow reads past end of device */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1216 | if ((from + len) > mtd->size) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1217 | return -EINVAL; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1218 | if (!len) |
| 1219 | return 0; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1220 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1221 | nand_get_device(chip, mtd, FL_READING); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1222 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1223 | chip->ops.len = len; |
| 1224 | chip->ops.datbuf = buf; |
| 1225 | chip->ops.oobbuf = NULL; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1226 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1227 | ret = nand_do_read_ops(mtd, from, &chip->ops); |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1228 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1229 | *retlen = chip->ops.retlen; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1230 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1231 | nand_release_device(mtd); |
| 1232 | |
| 1233 | return ret; |
| 1234 | } |
| 1235 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1236 | /** |
| 1237 | * nand_read_oob_std - [REPLACABLE] the most common OOB data read function |
| 1238 | * @mtd: mtd info structure |
| 1239 | * @chip: nand chip info structure |
| 1240 | * @page: page number to read |
| 1241 | * @sndcmd: flag whether to issue read command or not |
| 1242 | */ |
| 1243 | static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 1244 | int page, int sndcmd) |
| 1245 | { |
| 1246 | if (sndcmd) { |
| 1247 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 1248 | sndcmd = 0; |
| 1249 | } |
| 1250 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1251 | return sndcmd; |
| 1252 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1253 | |
| 1254 | /** |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1255 | * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC |
| 1256 | * with syndromes |
| 1257 | * @mtd: mtd info structure |
| 1258 | * @chip: nand chip info structure |
| 1259 | * @page: page number to read |
| 1260 | * @sndcmd: flag whether to issue read command or not |
| 1261 | */ |
| 1262 | static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 1263 | int page, int sndcmd) |
| 1264 | { |
| 1265 | uint8_t *buf = chip->oob_poi; |
| 1266 | int length = mtd->oobsize; |
| 1267 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 1268 | int eccsize = chip->ecc.size; |
| 1269 | uint8_t *bufpoi = buf; |
| 1270 | int i, toread, sndrnd = 0, pos; |
| 1271 | |
| 1272 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); |
| 1273 | for (i = 0; i < chip->ecc.steps; i++) { |
| 1274 | if (sndrnd) { |
| 1275 | pos = eccsize + i * (eccsize + chunk); |
| 1276 | if (mtd->writesize > 512) |
| 1277 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); |
| 1278 | else |
| 1279 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); |
| 1280 | } else |
| 1281 | sndrnd = 1; |
| 1282 | toread = min_t(int, length, chunk); |
| 1283 | chip->read_buf(mtd, bufpoi, toread); |
| 1284 | bufpoi += toread; |
| 1285 | length -= toread; |
| 1286 | } |
| 1287 | if (length > 0) |
| 1288 | chip->read_buf(mtd, bufpoi, length); |
| 1289 | |
| 1290 | return 1; |
| 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * nand_write_oob_std - [REPLACABLE] the most common OOB data write function |
| 1295 | * @mtd: mtd info structure |
| 1296 | * @chip: nand chip info structure |
| 1297 | * @page: page number to write |
| 1298 | */ |
| 1299 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 1300 | int page) |
| 1301 | { |
| 1302 | int status = 0; |
| 1303 | const uint8_t *buf = chip->oob_poi; |
| 1304 | int length = mtd->oobsize; |
| 1305 | |
| 1306 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 1307 | chip->write_buf(mtd, buf, length); |
| 1308 | /* Send command to program the OOB data */ |
| 1309 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1310 | |
| 1311 | status = chip->waitfunc(mtd, chip); |
| 1312 | |
| 1313 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC |
| 1318 | * with syndrome - only for large page flash ! |
| 1319 | * @mtd: mtd info structure |
| 1320 | * @chip: nand chip info structure |
| 1321 | * @page: page number to write |
| 1322 | */ |
| 1323 | static int nand_write_oob_syndrome(struct mtd_info *mtd, |
| 1324 | struct nand_chip *chip, int page) |
| 1325 | { |
| 1326 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 1327 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
| 1328 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; |
| 1329 | const uint8_t *bufpoi = chip->oob_poi; |
| 1330 | |
| 1331 | /* |
| 1332 | * data-ecc-data-ecc ... ecc-oob |
| 1333 | * or |
| 1334 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 1335 | */ |
| 1336 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 1337 | pos = steps * (eccsize + chunk); |
| 1338 | steps = 0; |
| 1339 | } else |
| 1340 | pos = eccsize; |
| 1341 | |
| 1342 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); |
| 1343 | for (i = 0; i < steps; i++) { |
| 1344 | if (sndcmd) { |
| 1345 | if (mtd->writesize <= 512) { |
| 1346 | uint32_t fill = 0xFFFFFFFF; |
| 1347 | |
| 1348 | len = eccsize; |
| 1349 | while (len > 0) { |
| 1350 | int num = min_t(int, len, 4); |
| 1351 | chip->write_buf(mtd, (uint8_t *)&fill, |
| 1352 | num); |
| 1353 | len -= num; |
| 1354 | } |
| 1355 | } else { |
| 1356 | pos = eccsize + i * (eccsize + chunk); |
| 1357 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); |
| 1358 | } |
| 1359 | } else |
| 1360 | sndcmd = 1; |
| 1361 | len = min_t(int, length, chunk); |
| 1362 | chip->write_buf(mtd, bufpoi, len); |
| 1363 | bufpoi += len; |
| 1364 | length -= len; |
| 1365 | } |
| 1366 | if (length > 0) |
| 1367 | chip->write_buf(mtd, bufpoi, length); |
| 1368 | |
| 1369 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1370 | status = chip->waitfunc(mtd, chip); |
| 1371 | |
| 1372 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 1373 | } |
| 1374 | |
| 1375 | /** |
| 1376 | * nand_do_read_oob - [Intern] NAND read out-of-band |
| 1377 | * @mtd: MTD device structure |
| 1378 | * @from: offset to read from |
| 1379 | * @ops: oob operations description structure |
| 1380 | * |
| 1381 | * NAND read out-of-band data from the spare area |
| 1382 | */ |
| 1383 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 1384 | struct mtd_oob_ops *ops) |
| 1385 | { |
| 1386 | int page, realpage, chipnr, sndcmd = 1; |
| 1387 | struct nand_chip *chip = mtd->priv; |
| 1388 | int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 1389 | int readlen = ops->ooblen; |
| 1390 | int len; |
| 1391 | uint8_t *buf = ops->oobbuf; |
| 1392 | |
| 1393 | MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n", |
| 1394 | (unsigned long long)from, readlen); |
| 1395 | |
| 1396 | if (ops->mode == MTD_OOB_AUTO) |
| 1397 | len = chip->ecc.layout->oobavail; |
| 1398 | else |
| 1399 | len = mtd->oobsize; |
| 1400 | |
| 1401 | if (unlikely(ops->ooboffs >= len)) { |
| 1402 | MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: " |
| 1403 | "Attempt to start read outside oob\n"); |
| 1404 | return -EINVAL; |
| 1405 | } |
| 1406 | |
| 1407 | /* Do not allow reads past end of device */ |
| 1408 | if (unlikely(from >= mtd->size || |
| 1409 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 1410 | (from >> chip->page_shift)) * len)) { |
| 1411 | MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: " |
| 1412 | "Attempt read beyond end of device\n"); |
| 1413 | return -EINVAL; |
| 1414 | } |
| 1415 | |
| 1416 | chipnr = (int)(from >> chip->chip_shift); |
| 1417 | chip->select_chip(mtd, chipnr); |
| 1418 | |
| 1419 | /* Shift to get page */ |
| 1420 | realpage = (int)(from >> chip->page_shift); |
| 1421 | page = realpage & chip->pagemask; |
| 1422 | |
| 1423 | while(1) { |
| 1424 | sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd); |
| 1425 | |
| 1426 | len = min(len, readlen); |
| 1427 | buf = nand_transfer_oob(chip, buf, ops, len); |
| 1428 | |
| 1429 | if (!(chip->options & NAND_NO_READRDY)) { |
| 1430 | /* |
| 1431 | * Apply delay or wait for ready/busy pin. Do this |
| 1432 | * before the AUTOINCR check, so no problems arise if a |
| 1433 | * chip which does auto increment is marked as |
| 1434 | * NOAUTOINCR by the board driver. |
| 1435 | */ |
| 1436 | if (!chip->dev_ready) |
| 1437 | udelay(chip->chip_delay); |
| 1438 | else |
| 1439 | nand_wait_ready(mtd); |
| 1440 | } |
| 1441 | |
| 1442 | readlen -= len; |
| 1443 | if (!readlen) |
| 1444 | break; |
| 1445 | |
| 1446 | /* Increment page address */ |
| 1447 | realpage++; |
| 1448 | |
| 1449 | page = realpage & chip->pagemask; |
| 1450 | /* Check, if we cross a chip boundary */ |
| 1451 | if (!page) { |
| 1452 | chipnr++; |
| 1453 | chip->select_chip(mtd, -1); |
| 1454 | chip->select_chip(mtd, chipnr); |
| 1455 | } |
| 1456 | |
| 1457 | /* Check, if the chip supports auto page increment |
| 1458 | * or if we have hit a block boundary. |
| 1459 | */ |
| 1460 | if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck)) |
| 1461 | sndcmd = 1; |
| 1462 | } |
| 1463 | |
| 1464 | ops->oobretlen = ops->ooblen; |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | /** |
| 1469 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
| 1470 | * @mtd: MTD device structure |
| 1471 | * @from: offset to read from |
| 1472 | * @ops: oob operation description structure |
| 1473 | * |
| 1474 | * NAND read data and/or out-of-band data |
| 1475 | */ |
| 1476 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 1477 | struct mtd_oob_ops *ops) |
| 1478 | { |
| 1479 | struct nand_chip *chip = mtd->priv; |
| 1480 | int ret = -ENOTSUPP; |
| 1481 | |
| 1482 | ops->retlen = 0; |
| 1483 | |
| 1484 | /* Do not allow reads past end of device */ |
| 1485 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
| 1486 | MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: " |
| 1487 | "Attempt read beyond end of device\n"); |
| 1488 | return -EINVAL; |
| 1489 | } |
| 1490 | |
| 1491 | nand_get_device(chip, mtd, FL_READING); |
| 1492 | |
| 1493 | switch(ops->mode) { |
| 1494 | case MTD_OOB_PLACE: |
| 1495 | case MTD_OOB_AUTO: |
| 1496 | case MTD_OOB_RAW: |
| 1497 | break; |
| 1498 | |
| 1499 | default: |
| 1500 | goto out; |
| 1501 | } |
| 1502 | |
| 1503 | if (!ops->datbuf) |
| 1504 | ret = nand_do_read_oob(mtd, from, ops); |
| 1505 | else |
| 1506 | ret = nand_do_read_ops(mtd, from, ops); |
| 1507 | |
| 1508 | out: |
| 1509 | nand_release_device(mtd); |
| 1510 | return ret; |
| 1511 | } |
| 1512 | |
| 1513 | |
| 1514 | /** |
| 1515 | * nand_write_page_raw - [Intern] raw page write function |
| 1516 | * @mtd: mtd info structure |
| 1517 | * @chip: nand chip info structure |
| 1518 | * @buf: data buffer |
| 1519 | */ |
| 1520 | static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1521 | const uint8_t *buf) |
| 1522 | { |
| 1523 | chip->write_buf(mtd, buf, mtd->writesize); |
| 1524 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * nand_write_page_swecc - [REPLACABLE] software ecc based page write function |
| 1529 | * @mtd: mtd info structure |
| 1530 | * @chip: nand chip info structure |
| 1531 | * @buf: data buffer |
| 1532 | */ |
| 1533 | static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 1534 | const uint8_t *buf) |
| 1535 | { |
| 1536 | int i, eccsize = chip->ecc.size; |
| 1537 | int eccbytes = chip->ecc.bytes; |
| 1538 | int eccsteps = chip->ecc.steps; |
| 1539 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1540 | const uint8_t *p = buf; |
| 1541 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1542 | |
| 1543 | /* Software ecc calculation */ |
| 1544 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1545 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1546 | |
| 1547 | for (i = 0; i < chip->ecc.total; i++) |
| 1548 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 1549 | |
| 1550 | chip->ecc.write_page_raw(mtd, chip, buf); |
| 1551 | } |
| 1552 | |
| 1553 | /** |
| 1554 | * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function |
| 1555 | * @mtd: mtd info structure |
| 1556 | * @chip: nand chip info structure |
| 1557 | * @buf: data buffer |
| 1558 | */ |
| 1559 | static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 1560 | const uint8_t *buf) |
| 1561 | { |
| 1562 | int i, eccsize = chip->ecc.size; |
| 1563 | int eccbytes = chip->ecc.bytes; |
| 1564 | int eccsteps = chip->ecc.steps; |
| 1565 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1566 | const uint8_t *p = buf; |
| 1567 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1568 | |
| 1569 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1570 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 1571 | chip->write_buf(mtd, p, eccsize); |
| 1572 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1573 | } |
| 1574 | |
| 1575 | for (i = 0; i < chip->ecc.total; i++) |
| 1576 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 1577 | |
| 1578 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1579 | } |
| 1580 | |
| 1581 | /** |
| 1582 | * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write |
| 1583 | * @mtd: mtd info structure |
| 1584 | * @chip: nand chip info structure |
| 1585 | * @buf: data buffer |
| 1586 | * |
| 1587 | * The hw generator calculates the error syndrome automatically. Therefor |
| 1588 | * we need a special oob layout and handling. |
| 1589 | */ |
| 1590 | static void nand_write_page_syndrome(struct mtd_info *mtd, |
| 1591 | struct nand_chip *chip, const uint8_t *buf) |
| 1592 | { |
| 1593 | int i, eccsize = chip->ecc.size; |
| 1594 | int eccbytes = chip->ecc.bytes; |
| 1595 | int eccsteps = chip->ecc.steps; |
| 1596 | const uint8_t *p = buf; |
| 1597 | uint8_t *oob = chip->oob_poi; |
| 1598 | |
| 1599 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1600 | |
| 1601 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 1602 | chip->write_buf(mtd, p, eccsize); |
| 1603 | |
| 1604 | if (chip->ecc.prepad) { |
| 1605 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 1606 | oob += chip->ecc.prepad; |
| 1607 | } |
| 1608 | |
| 1609 | chip->ecc.calculate(mtd, p, oob); |
| 1610 | chip->write_buf(mtd, oob, eccbytes); |
| 1611 | oob += eccbytes; |
| 1612 | |
| 1613 | if (chip->ecc.postpad) { |
| 1614 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 1615 | oob += chip->ecc.postpad; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | /* Calculate remaining oob bytes */ |
| 1620 | i = mtd->oobsize - (oob - chip->oob_poi); |
| 1621 | if (i) |
| 1622 | chip->write_buf(mtd, oob, i); |
| 1623 | } |
| 1624 | |
| 1625 | /** |
| 1626 | * nand_write_page - [REPLACEABLE] write one page |
| 1627 | * @mtd: MTD device structure |
| 1628 | * @chip: NAND chip descriptor |
| 1629 | * @buf: the data to write |
| 1630 | * @page: page number to write |
| 1631 | * @cached: cached programming |
| 1632 | * @raw: use _raw version of write_page |
| 1633 | */ |
| 1634 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 1635 | const uint8_t *buf, int page, int cached, int raw) |
| 1636 | { |
| 1637 | int status; |
| 1638 | |
| 1639 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
| 1640 | |
| 1641 | if (unlikely(raw)) |
| 1642 | chip->ecc.write_page_raw(mtd, chip, buf); |
| 1643 | else |
| 1644 | chip->ecc.write_page(mtd, chip, buf); |
| 1645 | |
| 1646 | /* |
| 1647 | * Cached progamming disabled for now, Not sure if its worth the |
| 1648 | * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s) |
| 1649 | */ |
| 1650 | cached = 0; |
| 1651 | |
| 1652 | if (!cached || !(chip->options & NAND_CACHEPRG)) { |
| 1653 | |
| 1654 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1655 | status = chip->waitfunc(mtd, chip); |
| 1656 | /* |
| 1657 | * See if operation failed and additional status checks are |
| 1658 | * available |
| 1659 | */ |
| 1660 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 1661 | status = chip->errstat(mtd, chip, FL_WRITING, status, |
| 1662 | page); |
| 1663 | |
| 1664 | if (status & NAND_STATUS_FAIL) |
| 1665 | return -EIO; |
| 1666 | } else { |
| 1667 | chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1); |
| 1668 | status = chip->waitfunc(mtd, chip); |
| 1669 | } |
| 1670 | |
| 1671 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE |
| 1672 | /* Send command to read back the data */ |
| 1673 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1674 | |
| 1675 | if (chip->verify_buf(mtd, buf, mtd->writesize)) |
| 1676 | return -EIO; |
| 1677 | #endif |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
| 1681 | /** |
| 1682 | * nand_fill_oob - [Internal] Transfer client buffer to oob |
| 1683 | * @chip: nand chip structure |
| 1684 | * @oob: oob data buffer |
| 1685 | * @ops: oob ops structure |
| 1686 | */ |
| 1687 | static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, |
| 1688 | struct mtd_oob_ops *ops) |
| 1689 | { |
| 1690 | size_t len = ops->ooblen; |
| 1691 | |
| 1692 | switch(ops->mode) { |
| 1693 | |
| 1694 | case MTD_OOB_PLACE: |
| 1695 | case MTD_OOB_RAW: |
| 1696 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 1697 | return oob + len; |
| 1698 | |
| 1699 | case MTD_OOB_AUTO: { |
| 1700 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 1701 | uint32_t boffs = 0, woffs = ops->ooboffs; |
| 1702 | size_t bytes = 0; |
| 1703 | |
| 1704 | for(; free->length && len; free++, len -= bytes) { |
| 1705 | /* Write request not from offset 0 ? */ |
| 1706 | if (unlikely(woffs)) { |
| 1707 | if (woffs >= free->length) { |
| 1708 | woffs -= free->length; |
| 1709 | continue; |
| 1710 | } |
| 1711 | boffs = free->offset + woffs; |
| 1712 | bytes = min_t(size_t, len, |
| 1713 | (free->length - woffs)); |
| 1714 | woffs = 0; |
| 1715 | } else { |
| 1716 | bytes = min_t(size_t, len, free->length); |
| 1717 | boffs = free->offset; |
| 1718 | } |
| 1719 | memcpy(chip->oob_poi + boffs, oob, bytes); |
| 1720 | oob += bytes; |
| 1721 | } |
| 1722 | return oob; |
| 1723 | } |
| 1724 | default: |
| 1725 | BUG(); |
| 1726 | } |
| 1727 | return NULL; |
| 1728 | } |
| 1729 | |
| 1730 | #define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0 |
| 1731 | |
| 1732 | /** |
| 1733 | * nand_do_write_ops - [Internal] NAND write with ECC |
| 1734 | * @mtd: MTD device structure |
| 1735 | * @to: offset to write to |
| 1736 | * @ops: oob operations description structure |
| 1737 | * |
| 1738 | * NAND write with ECC |
| 1739 | */ |
| 1740 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 1741 | struct mtd_oob_ops *ops) |
| 1742 | { |
| 1743 | int chipnr, realpage, page, blockmask, column; |
| 1744 | struct nand_chip *chip = mtd->priv; |
| 1745 | uint32_t writelen = ops->len; |
| 1746 | uint8_t *oob = ops->oobbuf; |
| 1747 | uint8_t *buf = ops->datbuf; |
| 1748 | int ret, subpage; |
| 1749 | |
| 1750 | ops->retlen = 0; |
| 1751 | if (!writelen) |
| 1752 | return 0; |
| 1753 | |
| 1754 | /* reject writes, which are not page aligned */ |
| 1755 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
| 1756 | printk(KERN_NOTICE "nand_write: " |
| 1757 | "Attempt to write not page aligned data\n"); |
| 1758 | return -EINVAL; |
| 1759 | } |
| 1760 | |
| 1761 | column = to & (mtd->writesize - 1); |
| 1762 | subpage = column || (writelen & (mtd->writesize - 1)); |
| 1763 | |
| 1764 | if (subpage && oob) |
| 1765 | return -EINVAL; |
| 1766 | |
| 1767 | chipnr = (int)(to >> chip->chip_shift); |
| 1768 | chip->select_chip(mtd, chipnr); |
| 1769 | |
| 1770 | /* Check, if it is write protected */ |
| 1771 | if (nand_check_wp(mtd)) { |
| 1772 | printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n"); |
| 1773 | return -EIO; |
| 1774 | } |
| 1775 | |
| 1776 | realpage = (int)(to >> chip->page_shift); |
| 1777 | page = realpage & chip->pagemask; |
| 1778 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 1779 | |
| 1780 | /* Invalidate the page cache, when we write to the cached page */ |
| 1781 | if (to <= (chip->pagebuf << chip->page_shift) && |
| 1782 | (chip->pagebuf << chip->page_shift) < (to + ops->len)) |
| 1783 | chip->pagebuf = -1; |
| 1784 | |
| 1785 | /* If we're not given explicit OOB data, let it be 0xFF */ |
| 1786 | if (likely(!oob)) |
| 1787 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 1788 | |
| 1789 | while(1) { |
| 1790 | int bytes = mtd->writesize; |
| 1791 | int cached = writelen > bytes && page != blockmask; |
| 1792 | uint8_t *wbuf = buf; |
| 1793 | |
| 1794 | /* Partial page write ? */ |
| 1795 | if (unlikely(column || writelen < (mtd->writesize - 1))) { |
| 1796 | cached = 0; |
| 1797 | bytes = min_t(int, bytes - column, (int) writelen); |
| 1798 | chip->pagebuf = -1; |
| 1799 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 1800 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 1801 | wbuf = chip->buffers->databuf; |
| 1802 | } |
| 1803 | |
| 1804 | if (unlikely(oob)) |
| 1805 | oob = nand_fill_oob(chip, oob, ops); |
| 1806 | |
| 1807 | ret = chip->write_page(mtd, chip, wbuf, page, cached, |
| 1808 | (ops->mode == MTD_OOB_RAW)); |
| 1809 | if (ret) |
| 1810 | break; |
| 1811 | |
| 1812 | writelen -= bytes; |
| 1813 | if (!writelen) |
| 1814 | break; |
| 1815 | |
| 1816 | column = 0; |
| 1817 | buf += bytes; |
| 1818 | realpage++; |
| 1819 | |
| 1820 | page = realpage & chip->pagemask; |
| 1821 | /* Check, if we cross a chip boundary */ |
| 1822 | if (!page) { |
| 1823 | chipnr++; |
| 1824 | chip->select_chip(mtd, -1); |
| 1825 | chip->select_chip(mtd, chipnr); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | ops->retlen = ops->len - writelen; |
| 1830 | if (unlikely(oob)) |
| 1831 | ops->oobretlen = ops->ooblen; |
| 1832 | return ret; |
| 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * nand_write - [MTD Interface] NAND write with ECC |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1837 | * @mtd: MTD device structure |
| 1838 | * @to: offset to write to |
| 1839 | * @len: number of bytes to write |
| 1840 | * @retlen: pointer to variable to store the number of written bytes |
| 1841 | * @buf: the data to write |
| 1842 | * |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1843 | * NAND write with ECC |
| 1844 | */ |
| 1845 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 1846 | size_t *retlen, const uint8_t *buf) |
| 1847 | { |
| 1848 | struct nand_chip *chip = mtd->priv; |
| 1849 | int ret; |
| 1850 | |
| 1851 | /* Do not allow reads past end of device */ |
| 1852 | if ((to + len) > mtd->size) |
| 1853 | return -EINVAL; |
| 1854 | if (!len) |
| 1855 | return 0; |
| 1856 | |
| 1857 | nand_get_device(chip, mtd, FL_WRITING); |
| 1858 | |
| 1859 | chip->ops.len = len; |
| 1860 | chip->ops.datbuf = (uint8_t *)buf; |
| 1861 | chip->ops.oobbuf = NULL; |
| 1862 | |
| 1863 | ret = nand_do_write_ops(mtd, to, &chip->ops); |
| 1864 | |
| 1865 | *retlen = chip->ops.retlen; |
| 1866 | |
| 1867 | nand_release_device(mtd); |
| 1868 | |
| 1869 | return ret; |
| 1870 | } |
| 1871 | |
| 1872 | /** |
| 1873 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
| 1874 | * @mtd: MTD device structure |
| 1875 | * @to: offset to write to |
| 1876 | * @ops: oob operation description structure |
| 1877 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1878 | * NAND write out-of-band |
| 1879 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1880 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 1881 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1882 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1883 | int chipnr, page, status, len; |
| 1884 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1885 | |
Scott Wood | 3167c53 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 1886 | MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1887 | (unsigned int)to, (int)ops->ooblen); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1888 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1889 | if (ops->mode == MTD_OOB_AUTO) |
| 1890 | len = chip->ecc.layout->oobavail; |
| 1891 | else |
| 1892 | len = mtd->oobsize; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1893 | |
| 1894 | /* Do not allow write past end of page */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1895 | if ((ops->ooboffs + ops->ooblen) > len) { |
Scott Wood | 3167c53 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 1896 | MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " |
| 1897 | "Attempt to write past end of page\n"); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1898 | return -EINVAL; |
| 1899 | } |
| 1900 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1901 | if (unlikely(ops->ooboffs >= len)) { |
| 1902 | MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: " |
| 1903 | "Attempt to start write outside oob\n"); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1904 | return -EINVAL; |
| 1905 | } |
| 1906 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1907 | /* Do not allow reads past end of device */ |
| 1908 | if (unlikely(to >= mtd->size || |
| 1909 | ops->ooboffs + ops->ooblen > |
| 1910 | ((mtd->size >> chip->page_shift) - |
| 1911 | (to >> chip->page_shift)) * len)) { |
| 1912 | MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: " |
| 1913 | "Attempt write beyond end of device\n"); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1914 | return -EINVAL; |
| 1915 | } |
| 1916 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1917 | chipnr = (int)(to >> chip->chip_shift); |
| 1918 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1919 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1920 | /* Shift to get page */ |
| 1921 | page = (int)(to >> chip->page_shift); |
| 1922 | |
| 1923 | /* |
| 1924 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 1925 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 1926 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 1927 | * it in the doc2000 driver in August 1999. dwmw2. |
| 1928 | */ |
| 1929 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1930 | |
| 1931 | /* Check, if it is write protected */ |
| 1932 | if (nand_check_wp(mtd)) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1933 | return -EROFS; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1934 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1935 | /* Invalidate the page cache, if we write to the cached page */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1936 | if (page == chip->pagebuf) |
| 1937 | chip->pagebuf = -1; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1938 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1939 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 1940 | nand_fill_oob(chip, ops->oobbuf, ops); |
| 1941 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
| 1942 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1943 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1944 | if (status) |
| 1945 | return status; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1946 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1947 | ops->oobretlen = ops->ooblen; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1948 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1949 | return 0; |
| 1950 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1951 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1952 | /** |
| 1953 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band |
| 1954 | * @mtd: MTD device structure |
| 1955 | * @to: offset to write to |
| 1956 | * @ops: oob operation description structure |
| 1957 | */ |
| 1958 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 1959 | struct mtd_oob_ops *ops) |
| 1960 | { |
| 1961 | struct nand_chip *chip = mtd->priv; |
| 1962 | int ret = -ENOTSUPP; |
| 1963 | |
| 1964 | ops->retlen = 0; |
| 1965 | |
| 1966 | /* Do not allow writes past end of device */ |
| 1967 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
| 1968 | MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: " |
| 1969 | "Attempt read beyond end of device\n"); |
| 1970 | return -EINVAL; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1971 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1972 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1973 | nand_get_device(chip, mtd, FL_WRITING); |
| 1974 | |
| 1975 | switch(ops->mode) { |
| 1976 | case MTD_OOB_PLACE: |
| 1977 | case MTD_OOB_AUTO: |
| 1978 | case MTD_OOB_RAW: |
| 1979 | break; |
| 1980 | |
| 1981 | default: |
| 1982 | goto out; |
| 1983 | } |
| 1984 | |
| 1985 | if (!ops->datbuf) |
| 1986 | ret = nand_do_write_oob(mtd, to, ops); |
| 1987 | else |
| 1988 | ret = nand_do_write_ops(mtd, to, ops); |
| 1989 | |
| 1990 | out: |
| 1991 | nand_release_device(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1992 | return ret; |
| 1993 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1994 | |
| 1995 | /** |
| 1996 | * single_erease_cmd - [GENERIC] NAND standard block erase command function |
| 1997 | * @mtd: MTD device structure |
| 1998 | * @page: the page address of the block which will be erased |
| 1999 | * |
| 2000 | * Standard erase command for NAND chips |
| 2001 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2002 | static void single_erase_cmd(struct mtd_info *mtd, int page) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2003 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2004 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2005 | /* Send commands to erase a block */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2006 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2007 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | /** |
| 2011 | * multi_erease_cmd - [GENERIC] AND specific block erase command function |
| 2012 | * @mtd: MTD device structure |
| 2013 | * @page: the page address of the block which will be erased |
| 2014 | * |
| 2015 | * AND multi block erase command function |
| 2016 | * Erase 4 consecutive blocks |
| 2017 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2018 | static void multi_erase_cmd(struct mtd_info *mtd, int page) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2019 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2020 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2021 | /* Send commands to erase a block */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2022 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2023 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2024 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2025 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2026 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | /** |
| 2030 | * nand_erase - [MTD Interface] erase block(s) |
| 2031 | * @mtd: MTD device structure |
| 2032 | * @instr: erase instruction |
| 2033 | * |
| 2034 | * Erase one ore more blocks |
| 2035 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2036 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2037 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2038 | return nand_erase_nand(mtd, instr, 0); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2039 | } |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2040 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2041 | #define BBT_PAGE_MASK 0xffffff3f |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2042 | /** |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2043 | * nand_erase_nand - [Internal] erase block(s) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2044 | * @mtd: MTD device structure |
| 2045 | * @instr: erase instruction |
| 2046 | * @allowbbt: allow erasing the bbt area |
| 2047 | * |
| 2048 | * Erase one ore more blocks |
| 2049 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2050 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 2051 | int allowbbt) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2052 | { |
| 2053 | int page, len, status, pages_per_block, ret, chipnr; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2054 | struct nand_chip *chip = mtd->priv; |
| 2055 | int rewrite_bbt[NAND_MAX_CHIPS]={0}; |
| 2056 | unsigned int bbt_masked_page = 0xffffffff; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2057 | |
Scott Wood | 3167c53 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 2058 | MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n", |
| 2059 | (unsigned int) instr->addr, (unsigned int) instr->len); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2060 | |
| 2061 | /* Start address must align on block boundary */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2062 | if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) { |
Scott Wood | 3167c53 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 2063 | MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n"); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2064 | return -EINVAL; |
| 2065 | } |
| 2066 | |
| 2067 | /* Length must align on block boundary */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2068 | if (instr->len & ((1 << chip->phys_erase_shift) - 1)) { |
Scott Wood | 3167c53 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 2069 | MTDDEBUG (MTD_DEBUG_LEVEL0, |
| 2070 | "nand_erase: Length not block aligned\n"); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2071 | return -EINVAL; |
| 2072 | } |
| 2073 | |
| 2074 | /* Do not allow erase past end of device */ |
| 2075 | if ((instr->len + instr->addr) > mtd->size) { |
Scott Wood | 3167c53 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 2076 | MTDDEBUG (MTD_DEBUG_LEVEL0, |
| 2077 | "nand_erase: Erase past end of device\n"); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2078 | return -EINVAL; |
| 2079 | } |
| 2080 | |
| 2081 | instr->fail_addr = 0xffffffff; |
| 2082 | |
| 2083 | /* Grab the lock and see if the device is available */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2084 | nand_get_device(chip, mtd, FL_ERASING); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2085 | |
| 2086 | /* Shift to get first page */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2087 | page = (int)(instr->addr >> chip->page_shift); |
| 2088 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2089 | |
| 2090 | /* Calculate pages in each block */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2091 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
William Juul | 4cbb651 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 2092 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2093 | /* Select the NAND device */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2094 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2095 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2096 | /* Check, if it is write protected */ |
| 2097 | if (nand_check_wp(mtd)) { |
Scott Wood | 3167c53 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 2098 | MTDDEBUG (MTD_DEBUG_LEVEL0, |
| 2099 | "nand_erase: Device is write protected!!!\n"); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2100 | instr->state = MTD_ERASE_FAILED; |
| 2101 | goto erase_exit; |
| 2102 | } |
| 2103 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2104 | /* |
| 2105 | * If BBT requires refresh, set the BBT page mask to see if the BBT |
| 2106 | * should be rewritten. Otherwise the mask is set to 0xffffffff which |
| 2107 | * can not be matched. This is also done when the bbt is actually |
| 2108 | * erased to avoid recusrsive updates |
| 2109 | */ |
| 2110 | if (chip->options & BBT_AUTO_REFRESH && !allowbbt) |
| 2111 | bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK; |
| 2112 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2113 | /* Loop through the pages */ |
| 2114 | len = instr->len; |
| 2115 | |
| 2116 | instr->state = MTD_ERASING; |
| 2117 | |
| 2118 | while (len) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2119 | /* |
| 2120 | * heck if we have a bad block, we do not erase bad blocks ! |
| 2121 | */ |
| 2122 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
| 2123 | chip->page_shift, 0, allowbbt)) { |
| 2124 | printk(KERN_WARNING "nand_erase: attempt to erase a " |
| 2125 | "bad block at page 0x%08x\n", page); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2126 | instr->state = MTD_ERASE_FAILED; |
| 2127 | goto erase_exit; |
| 2128 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2129 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2130 | /* |
| 2131 | * Invalidate the page cache, if we erase the block which |
| 2132 | * contains the current cached page |
| 2133 | */ |
| 2134 | if (page <= chip->pagebuf && chip->pagebuf < |
| 2135 | (page + pages_per_block)) |
| 2136 | chip->pagebuf = -1; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2137 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2138 | chip->erase_cmd(mtd, page & chip->pagemask); |
| 2139 | |
| 2140 | status = chip->waitfunc(mtd, chip); |
| 2141 | |
| 2142 | /* |
| 2143 | * See if operation failed and additional status checks are |
| 2144 | * available |
| 2145 | */ |
| 2146 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 2147 | status = chip->errstat(mtd, chip, FL_ERASING, |
| 2148 | status, page); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2149 | |
| 2150 | /* See if block erase succeeded */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2151 | if (status & NAND_STATUS_FAIL) { |
Scott Wood | 3167c53 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 2152 | MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " |
| 2153 | "Failed erase, page 0x%08x\n", page); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2154 | instr->state = MTD_ERASE_FAILED; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2155 | instr->fail_addr = (page << chip->page_shift); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2156 | goto erase_exit; |
| 2157 | } |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2158 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2159 | /* |
| 2160 | * If BBT requires refresh, set the BBT rewrite flag to the |
| 2161 | * page being erased |
| 2162 | */ |
| 2163 | if (bbt_masked_page != 0xffffffff && |
| 2164 | (page & BBT_PAGE_MASK) == bbt_masked_page) |
| 2165 | rewrite_bbt[chipnr] = (page << chip->page_shift); |
| 2166 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2167 | /* Increment page address and decrement length */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2168 | len -= (1 << chip->phys_erase_shift); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2169 | page += pages_per_block; |
| 2170 | |
| 2171 | /* Check, if we cross a chip boundary */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2172 | if (len && !(page & chip->pagemask)) { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2173 | chipnr++; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2174 | chip->select_chip(mtd, -1); |
| 2175 | chip->select_chip(mtd, chipnr); |
| 2176 | |
| 2177 | /* |
| 2178 | * If BBT requires refresh and BBT-PERCHIP, set the BBT |
| 2179 | * page mask to see if this BBT should be rewritten |
| 2180 | */ |
| 2181 | if (bbt_masked_page != 0xffffffff && |
| 2182 | (chip->bbt_td->options & NAND_BBT_PERCHIP)) |
| 2183 | bbt_masked_page = chip->bbt_td->pages[chipnr] & |
| 2184 | BBT_PAGE_MASK; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2185 | } |
| 2186 | } |
| 2187 | instr->state = MTD_ERASE_DONE; |
| 2188 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2189 | erase_exit: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2190 | |
| 2191 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
| 2192 | /* Do call back function */ |
| 2193 | if (!ret) |
| 2194 | mtd_erase_callback(instr); |
| 2195 | |
| 2196 | /* Deselect and wake up anyone waiting on the device */ |
| 2197 | nand_release_device(mtd); |
| 2198 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2199 | /* |
| 2200 | * If BBT requires refresh and erase was successful, rewrite any |
| 2201 | * selected bad block tables |
| 2202 | */ |
| 2203 | if (bbt_masked_page == 0xffffffff || ret) |
| 2204 | return ret; |
| 2205 | |
| 2206 | for (chipnr = 0; chipnr < chip->numchips; chipnr++) { |
| 2207 | if (!rewrite_bbt[chipnr]) |
| 2208 | continue; |
| 2209 | /* update the BBT for chip */ |
| 2210 | MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt " |
| 2211 | "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr], |
| 2212 | chip->bbt_td->pages[chipnr]); |
| 2213 | nand_update_bbt(mtd, rewrite_bbt[chipnr]); |
| 2214 | } |
| 2215 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2216 | /* Return more or less happy */ |
| 2217 | return ret; |
| 2218 | } |
| 2219 | |
| 2220 | /** |
| 2221 | * nand_sync - [MTD Interface] sync |
| 2222 | * @mtd: MTD device structure |
| 2223 | * |
| 2224 | * Sync is actually a wait for chip ready function |
| 2225 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2226 | static void nand_sync(struct mtd_info *mtd) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2227 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2228 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2229 | |
Scott Wood | 3167c53 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 2230 | MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n"); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2231 | |
| 2232 | /* Grab the lock and see if the device is available */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2233 | nand_get_device(chip, mtd, FL_SYNCING); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2234 | /* Release it and go back */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2235 | nand_release_device(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2236 | } |
| 2237 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2238 | /** |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2239 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2240 | * @mtd: MTD device structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2241 | * @offs: offset relative to mtd start |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2242 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2243 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2244 | { |
| 2245 | /* Check for invalid offset */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2246 | if (offs > mtd->size) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2247 | return -EINVAL; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2248 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2249 | return nand_block_checkbad(mtd, offs, 1, 0); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | /** |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2253 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2254 | * @mtd: MTD device structure |
| 2255 | * @ofs: offset relative to mtd start |
| 2256 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2257 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2258 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2259 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2260 | int ret; |
| 2261 | |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2262 | if ((ret = nand_block_isbad(mtd, ofs))) { |
| 2263 | /* If it was bad already, return success and do nothing. */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2264 | if (ret > 0) |
| 2265 | return 0; |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2266 | return ret; |
| 2267 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2268 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2269 | return chip->block_markbad(mtd, ofs); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | /** |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2273 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
| 2274 | * @mtd: MTD device structure |
| 2275 | */ |
| 2276 | static int nand_suspend(struct mtd_info *mtd) |
| 2277 | { |
| 2278 | struct nand_chip *chip = mtd->priv; |
| 2279 | |
| 2280 | return nand_get_device(chip, mtd, FL_PM_SUSPENDED); |
| 2281 | } |
| 2282 | |
| 2283 | /** |
| 2284 | * nand_resume - [MTD Interface] Resume the NAND flash |
| 2285 | * @mtd: MTD device structure |
| 2286 | */ |
| 2287 | static void nand_resume(struct mtd_info *mtd) |
| 2288 | { |
| 2289 | struct nand_chip *chip = mtd->priv; |
| 2290 | |
| 2291 | if (chip->state == FL_PM_SUSPENDED) |
| 2292 | nand_release_device(mtd); |
| 2293 | else |
| 2294 | printk(KERN_ERR "nand_resume() called for a chip which is not " |
| 2295 | "in suspended state\n"); |
| 2296 | } |
| 2297 | |
| 2298 | /* |
| 2299 | * Set default functions |
| 2300 | */ |
| 2301 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
| 2302 | { |
| 2303 | /* check for proper chip_delay setup, set 20us if not */ |
| 2304 | if (!chip->chip_delay) |
| 2305 | chip->chip_delay = 20; |
| 2306 | |
| 2307 | /* check, if a user supplied command function given */ |
| 2308 | if (chip->cmdfunc == NULL) |
| 2309 | chip->cmdfunc = nand_command; |
| 2310 | |
| 2311 | /* check, if a user supplied wait function given */ |
| 2312 | if (chip->waitfunc == NULL) |
| 2313 | chip->waitfunc = nand_wait; |
| 2314 | |
| 2315 | if (!chip->select_chip) |
| 2316 | chip->select_chip = nand_select_chip; |
| 2317 | if (!chip->read_byte) |
| 2318 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 2319 | if (!chip->read_word) |
| 2320 | chip->read_word = nand_read_word; |
| 2321 | if (!chip->block_bad) |
| 2322 | chip->block_bad = nand_block_bad; |
| 2323 | if (!chip->block_markbad) |
| 2324 | chip->block_markbad = nand_default_block_markbad; |
| 2325 | if (!chip->write_buf) |
| 2326 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
| 2327 | if (!chip->read_buf) |
| 2328 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
| 2329 | if (!chip->verify_buf) |
| 2330 | chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf; |
| 2331 | if (!chip->scan_bbt) |
| 2332 | chip->scan_bbt = nand_default_bbt; |
| 2333 | |
| 2334 | if (!chip->controller) { |
| 2335 | chip->controller = &chip->hwcontrol; |
| 2336 | |
| 2337 | /* XXX U-BOOT XXX */ |
| 2338 | #if 0 |
| 2339 | spin_lock_init(&chip->controller->lock); |
| 2340 | init_waitqueue_head(&chip->controller->wq); |
| 2341 | #endif |
| 2342 | } |
| 2343 | |
| 2344 | } |
| 2345 | |
| 2346 | /* |
| 2347 | * Get the flash and manufacturer id and lookup if the type is supported |
| 2348 | */ |
| 2349 | static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, |
| 2350 | struct nand_chip *chip, |
| 2351 | int busw, int *maf_id) |
| 2352 | { |
| 2353 | struct nand_flash_dev *type = NULL; |
| 2354 | int i, dev_id, maf_idx; |
| 2355 | |
| 2356 | /* Select the device */ |
| 2357 | chip->select_chip(mtd, 0); |
| 2358 | |
| 2359 | /* Send the command for reading device ID */ |
| 2360 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 2361 | |
| 2362 | /* Read manufacturer and device IDs */ |
| 2363 | *maf_id = chip->read_byte(mtd); |
| 2364 | dev_id = chip->read_byte(mtd); |
| 2365 | |
| 2366 | /* Lookup the flash id */ |
| 2367 | for (i = 0; nand_flash_ids[i].name != NULL; i++) { |
| 2368 | if (dev_id == nand_flash_ids[i].id) { |
| 2369 | type = &nand_flash_ids[i]; |
| 2370 | break; |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | if (!type) |
| 2375 | return ERR_PTR(-ENODEV); |
| 2376 | |
| 2377 | if (!mtd->name) |
| 2378 | mtd->name = type->name; |
| 2379 | |
| 2380 | chip->chipsize = type->chipsize << 20; |
| 2381 | |
| 2382 | /* Newer devices have all the information in additional id bytes */ |
| 2383 | if (!type->pagesize) { |
| 2384 | int extid; |
| 2385 | /* The 3rd id byte holds MLC / multichip data */ |
| 2386 | chip->cellinfo = chip->read_byte(mtd); |
| 2387 | /* The 4th id byte is the important one */ |
| 2388 | extid = chip->read_byte(mtd); |
| 2389 | /* Calc pagesize */ |
| 2390 | mtd->writesize = 1024 << (extid & 0x3); |
| 2391 | extid >>= 2; |
| 2392 | /* Calc oobsize */ |
| 2393 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 2394 | extid >>= 2; |
| 2395 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 2396 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 2397 | extid >>= 2; |
| 2398 | /* Get buswidth information */ |
| 2399 | busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; |
| 2400 | |
| 2401 | } else { |
| 2402 | /* |
| 2403 | * Old devices have chip data hardcoded in the device id table |
| 2404 | */ |
| 2405 | mtd->erasesize = type->erasesize; |
| 2406 | mtd->writesize = type->pagesize; |
| 2407 | mtd->oobsize = mtd->writesize / 32; |
| 2408 | busw = type->options & NAND_BUSWIDTH_16; |
| 2409 | } |
| 2410 | |
| 2411 | /* Try to identify manufacturer */ |
| 2412 | for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) { |
| 2413 | if (nand_manuf_ids[maf_idx].id == *maf_id) |
| 2414 | break; |
| 2415 | } |
| 2416 | |
| 2417 | /* |
| 2418 | * Check, if buswidth is correct. Hardware drivers should set |
| 2419 | * chip correct ! |
| 2420 | */ |
| 2421 | if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 2422 | printk(KERN_INFO "NAND device: Manufacturer ID:" |
| 2423 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, |
| 2424 | dev_id, nand_manuf_ids[maf_idx].name, mtd->name); |
| 2425 | printk(KERN_WARNING "NAND bus width %d instead %d bit\n", |
| 2426 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
| 2427 | busw ? 16 : 8); |
| 2428 | return ERR_PTR(-EINVAL); |
| 2429 | } |
| 2430 | |
| 2431 | /* Calculate the address shift from the page size */ |
| 2432 | chip->page_shift = ffs(mtd->writesize) - 1; |
| 2433 | /* Convert chipsize to number of pages per chip -1. */ |
| 2434 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
| 2435 | |
| 2436 | chip->bbt_erase_shift = chip->phys_erase_shift = |
| 2437 | ffs(mtd->erasesize) - 1; |
| 2438 | chip->chip_shift = ffs(chip->chipsize) - 1; |
| 2439 | |
| 2440 | /* Set the bad block position */ |
| 2441 | chip->badblockpos = mtd->writesize > 512 ? |
| 2442 | NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS; |
| 2443 | |
| 2444 | /* Get chip options, preserve non chip based options */ |
| 2445 | chip->options &= ~NAND_CHIPOPTIONS_MSK; |
| 2446 | chip->options |= type->options & NAND_CHIPOPTIONS_MSK; |
| 2447 | |
| 2448 | /* |
| 2449 | * Set chip as a default. Board drivers can override it, if necessary |
| 2450 | */ |
| 2451 | chip->options |= NAND_NO_AUTOINCR; |
| 2452 | |
| 2453 | /* Check if chip is a not a samsung device. Do not clear the |
| 2454 | * options for chips which are not having an extended id. |
| 2455 | */ |
| 2456 | if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize) |
| 2457 | chip->options &= ~NAND_SAMSUNG_LP_OPTIONS; |
| 2458 | |
| 2459 | /* Check for AND chips with 4 page planes */ |
| 2460 | if (chip->options & NAND_4PAGE_ARRAY) |
| 2461 | chip->erase_cmd = multi_erase_cmd; |
| 2462 | else |
| 2463 | chip->erase_cmd = single_erase_cmd; |
| 2464 | |
| 2465 | /* Do not replace user supplied command function ! */ |
| 2466 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 2467 | chip->cmdfunc = nand_command_lp; |
| 2468 | |
| 2469 | printk(KERN_INFO "NAND device: Manufacturer ID:" |
| 2470 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id, |
| 2471 | nand_manuf_ids[maf_idx].name, type->name); |
| 2472 | |
| 2473 | return type; |
| 2474 | } |
| 2475 | |
| 2476 | /** |
| 2477 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
| 2478 | * @mtd: MTD device structure |
| 2479 | * @maxchips: Number of chips to scan for |
| 2480 | * |
| 2481 | * This is the first phase of the normal nand_scan() function. It |
| 2482 | * reads the flash ID and sets up MTD fields accordingly. |
| 2483 | * |
| 2484 | * The mtd->owner field must be set to the module of the caller. |
| 2485 | */ |
| 2486 | int nand_scan_ident(struct mtd_info *mtd, int maxchips) |
| 2487 | { |
| 2488 | int i, busw, nand_maf_id; |
| 2489 | struct nand_chip *chip = mtd->priv; |
| 2490 | struct nand_flash_dev *type; |
| 2491 | |
| 2492 | /* Get buswidth to select the correct functions */ |
| 2493 | busw = chip->options & NAND_BUSWIDTH_16; |
| 2494 | /* Set the default functions */ |
| 2495 | nand_set_defaults(chip, busw); |
| 2496 | |
| 2497 | /* Read the flash type */ |
| 2498 | type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id); |
| 2499 | |
| 2500 | if (IS_ERR(type)) { |
| 2501 | printk(KERN_WARNING "No NAND device found!!!\n"); |
| 2502 | chip->select_chip(mtd, -1); |
| 2503 | return PTR_ERR(type); |
| 2504 | } |
| 2505 | |
| 2506 | /* Check for a chip array */ |
| 2507 | for (i = 1; i < maxchips; i++) { |
| 2508 | chip->select_chip(mtd, i); |
| 2509 | /* Send the command for reading device ID */ |
| 2510 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 2511 | /* Read manufacturer and device IDs */ |
| 2512 | if (nand_maf_id != chip->read_byte(mtd) || |
| 2513 | type->id != chip->read_byte(mtd)) |
| 2514 | break; |
| 2515 | } |
| 2516 | if (i > 1) |
| 2517 | printk(KERN_INFO "%d NAND chips detected\n", i); |
| 2518 | |
| 2519 | /* Store the number of chips and calc total size for mtd */ |
| 2520 | chip->numchips = i; |
| 2521 | mtd->size = i * chip->chipsize; |
| 2522 | |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
| 2526 | |
| 2527 | /** |
| 2528 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
| 2529 | * @mtd: MTD device structure |
| 2530 | * @maxchips: Number of chips to scan for |
| 2531 | * |
| 2532 | * This is the second phase of the normal nand_scan() function. It |
| 2533 | * fills out all the uninitialized function pointers with the defaults |
| 2534 | * and scans for a bad block table if appropriate. |
| 2535 | */ |
| 2536 | int nand_scan_tail(struct mtd_info *mtd) |
| 2537 | { |
| 2538 | int i; |
| 2539 | struct nand_chip *chip = mtd->priv; |
| 2540 | |
| 2541 | if (!(chip->options & NAND_OWN_BUFFERS)) |
| 2542 | chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL); |
| 2543 | if (!chip->buffers) |
| 2544 | return -ENOMEM; |
| 2545 | |
| 2546 | /* Set the internal oob buffer location, just after the page data */ |
| 2547 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
| 2548 | |
| 2549 | /* |
| 2550 | * If no default placement scheme is given, select an appropriate one |
| 2551 | */ |
| 2552 | if (!chip->ecc.layout) { |
| 2553 | switch (mtd->oobsize) { |
| 2554 | case 8: |
| 2555 | chip->ecc.layout = &nand_oob_8; |
| 2556 | break; |
| 2557 | case 16: |
| 2558 | chip->ecc.layout = &nand_oob_16; |
| 2559 | break; |
| 2560 | case 64: |
| 2561 | chip->ecc.layout = &nand_oob_64; |
| 2562 | break; |
| 2563 | case 128: |
| 2564 | chip->ecc.layout = &nand_oob_128; |
| 2565 | break; |
| 2566 | default: |
| 2567 | printk(KERN_WARNING "No oob scheme defined for " |
| 2568 | "oobsize %d\n", mtd->oobsize); |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 2569 | /* BUG(); */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | if (!chip->write_page) |
| 2574 | chip->write_page = nand_write_page; |
| 2575 | |
| 2576 | /* |
| 2577 | * check ECC mode, default to software if 3byte/512byte hardware ECC is |
| 2578 | * selected and we have 256 byte pagesize fallback to software ECC |
| 2579 | */ |
| 2580 | if (!chip->ecc.read_page_raw) |
| 2581 | chip->ecc.read_page_raw = nand_read_page_raw; |
| 2582 | if (!chip->ecc.write_page_raw) |
| 2583 | chip->ecc.write_page_raw = nand_write_page_raw; |
| 2584 | |
| 2585 | switch (chip->ecc.mode) { |
| 2586 | case NAND_ECC_HW: |
| 2587 | /* Use standard hwecc read page function ? */ |
| 2588 | if (!chip->ecc.read_page) |
| 2589 | chip->ecc.read_page = nand_read_page_hwecc; |
| 2590 | if (!chip->ecc.write_page) |
| 2591 | chip->ecc.write_page = nand_write_page_hwecc; |
| 2592 | if (!chip->ecc.read_oob) |
| 2593 | chip->ecc.read_oob = nand_read_oob_std; |
| 2594 | if (!chip->ecc.write_oob) |
| 2595 | chip->ecc.write_oob = nand_write_oob_std; |
| 2596 | |
| 2597 | case NAND_ECC_HW_SYNDROME: |
| 2598 | if (!chip->ecc.calculate || !chip->ecc.correct || |
| 2599 | !chip->ecc.hwctl) { |
| 2600 | printk(KERN_WARNING "No ECC functions supplied, " |
| 2601 | "Hardware ECC not possible\n"); |
| 2602 | BUG(); |
| 2603 | } |
| 2604 | /* Use standard syndrome read/write page function ? */ |
| 2605 | if (!chip->ecc.read_page) |
| 2606 | chip->ecc.read_page = nand_read_page_syndrome; |
| 2607 | if (!chip->ecc.write_page) |
| 2608 | chip->ecc.write_page = nand_write_page_syndrome; |
| 2609 | if (!chip->ecc.read_oob) |
| 2610 | chip->ecc.read_oob = nand_read_oob_syndrome; |
| 2611 | if (!chip->ecc.write_oob) |
| 2612 | chip->ecc.write_oob = nand_write_oob_syndrome; |
| 2613 | |
| 2614 | if (mtd->writesize >= chip->ecc.size) |
| 2615 | break; |
| 2616 | printk(KERN_WARNING "%d byte HW ECC not possible on " |
| 2617 | "%d byte page size, fallback to SW ECC\n", |
| 2618 | chip->ecc.size, mtd->writesize); |
| 2619 | chip->ecc.mode = NAND_ECC_SOFT; |
| 2620 | |
| 2621 | case NAND_ECC_SOFT: |
| 2622 | chip->ecc.calculate = nand_calculate_ecc; |
| 2623 | chip->ecc.correct = nand_correct_data; |
| 2624 | chip->ecc.read_page = nand_read_page_swecc; |
| 2625 | chip->ecc.write_page = nand_write_page_swecc; |
| 2626 | chip->ecc.read_oob = nand_read_oob_std; |
| 2627 | chip->ecc.write_oob = nand_write_oob_std; |
| 2628 | chip->ecc.size = 256; |
| 2629 | chip->ecc.bytes = 3; |
| 2630 | break; |
| 2631 | |
| 2632 | case NAND_ECC_NONE: |
| 2633 | printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. " |
| 2634 | "This is not recommended !!\n"); |
| 2635 | chip->ecc.read_page = nand_read_page_raw; |
| 2636 | chip->ecc.write_page = nand_write_page_raw; |
| 2637 | chip->ecc.read_oob = nand_read_oob_std; |
| 2638 | chip->ecc.write_oob = nand_write_oob_std; |
| 2639 | chip->ecc.size = mtd->writesize; |
| 2640 | chip->ecc.bytes = 0; |
| 2641 | break; |
| 2642 | |
| 2643 | default: |
| 2644 | printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n", |
| 2645 | chip->ecc.mode); |
| 2646 | BUG(); |
| 2647 | } |
| 2648 | |
| 2649 | /* |
| 2650 | * The number of bytes available for a client to place data into |
| 2651 | * the out of band area |
| 2652 | */ |
| 2653 | chip->ecc.layout->oobavail = 0; |
| 2654 | for (i = 0; chip->ecc.layout->oobfree[i].length; i++) |
| 2655 | chip->ecc.layout->oobavail += |
| 2656 | chip->ecc.layout->oobfree[i].length; |
| 2657 | mtd->oobavail = chip->ecc.layout->oobavail; |
| 2658 | |
| 2659 | /* |
| 2660 | * Set the number of read / write steps for one page depending on ECC |
| 2661 | * mode |
| 2662 | */ |
| 2663 | chip->ecc.steps = mtd->writesize / chip->ecc.size; |
| 2664 | if(chip->ecc.steps * chip->ecc.size != mtd->writesize) { |
| 2665 | printk(KERN_WARNING "Invalid ecc parameters\n"); |
| 2666 | BUG(); |
| 2667 | } |
| 2668 | chip->ecc.total = chip->ecc.steps * chip->ecc.bytes; |
| 2669 | |
| 2670 | /* |
| 2671 | * Allow subpage writes up to ecc.steps. Not possible for MLC |
| 2672 | * FLASH. |
| 2673 | */ |
| 2674 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 2675 | !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) { |
| 2676 | switch(chip->ecc.steps) { |
| 2677 | case 2: |
| 2678 | mtd->subpage_sft = 1; |
| 2679 | break; |
| 2680 | case 4: |
| 2681 | case 8: |
| 2682 | mtd->subpage_sft = 2; |
| 2683 | break; |
| 2684 | } |
| 2685 | } |
| 2686 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 2687 | |
| 2688 | /* Initialize state */ |
| 2689 | chip->state = FL_READY; |
| 2690 | |
| 2691 | /* De-select the device */ |
| 2692 | chip->select_chip(mtd, -1); |
| 2693 | |
| 2694 | /* Invalidate the pagebuffer reference */ |
| 2695 | chip->pagebuf = -1; |
| 2696 | |
| 2697 | /* Fill in remaining MTD driver data */ |
| 2698 | mtd->type = MTD_NANDFLASH; |
| 2699 | mtd->flags = MTD_CAP_NANDFLASH; |
| 2700 | mtd->erase = nand_erase; |
| 2701 | mtd->point = NULL; |
| 2702 | mtd->unpoint = NULL; |
| 2703 | mtd->read = nand_read; |
| 2704 | mtd->write = nand_write; |
| 2705 | mtd->read_oob = nand_read_oob; |
| 2706 | mtd->write_oob = nand_write_oob; |
| 2707 | mtd->sync = nand_sync; |
| 2708 | mtd->lock = NULL; |
| 2709 | mtd->unlock = NULL; |
| 2710 | mtd->suspend = nand_suspend; |
| 2711 | mtd->resume = nand_resume; |
| 2712 | mtd->block_isbad = nand_block_isbad; |
| 2713 | mtd->block_markbad = nand_block_markbad; |
| 2714 | |
| 2715 | /* propagate ecc.layout to mtd_info */ |
| 2716 | mtd->ecclayout = chip->ecc.layout; |
| 2717 | |
| 2718 | /* Check, if we should skip the bad block table scan */ |
| 2719 | if (chip->options & NAND_SKIP_BBTSCAN) |
| 2720 | return 0; |
| 2721 | |
| 2722 | /* Build bad block table */ |
| 2723 | return chip->scan_bbt(mtd); |
| 2724 | } |
| 2725 | |
| 2726 | /* module_text_address() isn't exported, and it's mostly a pointless |
| 2727 | test if this is a module _anyway_ -- they'd have to try _really_ hard |
| 2728 | to call us from in-kernel code if the core NAND support is modular. */ |
| 2729 | #ifdef MODULE |
| 2730 | #define caller_is_module() (1) |
| 2731 | #else |
| 2732 | #define caller_is_module() \ |
| 2733 | module_text_address((unsigned long)__builtin_return_address(0)) |
| 2734 | #endif |
| 2735 | |
| 2736 | /** |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2737 | * nand_scan - [NAND Interface] Scan for the NAND device |
| 2738 | * @mtd: MTD device structure |
| 2739 | * @maxchips: Number of chips to scan for |
| 2740 | * |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2741 | * This fills out all the uninitialized function pointers |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2742 | * with the defaults. |
| 2743 | * The flash ID is read and the mtd/chip structures are |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2744 | * filled with the appropriate values. |
| 2745 | * The mtd->owner field must be set to the module of the caller |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2746 | * |
| 2747 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2748 | int nand_scan(struct mtd_info *mtd, int maxchips) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2749 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2750 | int ret; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2751 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2752 | /* Many callers got this wrong, so check for it for a while... */ |
| 2753 | /* XXX U-BOOT XXX */ |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2754 | #if 0 |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2755 | if (!mtd->owner && caller_is_module()) { |
| 2756 | printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n"); |
| 2757 | BUG(); |
| 2758 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2759 | #endif |
William Juul | 4cbb651 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 2760 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2761 | ret = nand_scan_ident(mtd, maxchips); |
| 2762 | if (!ret) |
| 2763 | ret = nand_scan_tail(mtd); |
| 2764 | return ret; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | /** |
Wolfgang Denk | ac7eb8a3 | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2768 | * nand_release - [NAND Interface] Free resources held by the NAND device |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2769 | * @mtd: MTD device structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2770 | */ |
| 2771 | void nand_release(struct mtd_info *mtd) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2772 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2773 | struct nand_chip *chip = mtd->priv; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2774 | |
| 2775 | #ifdef CONFIG_MTD_PARTITIONS |
| 2776 | /* Deregister partitions */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2777 | del_mtd_partitions(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2778 | #endif |
| 2779 | /* Deregister the device */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2780 | /* XXX U-BOOT XXX */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2781 | #if 0 |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2782 | del_mtd_device(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2783 | #endif |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2784 | |
| 2785 | /* Free bad block table memory */ |
| 2786 | kfree(chip->bbt); |
| 2787 | if (!(chip->options & NAND_OWN_BUFFERS)) |
| 2788 | kfree(chip->buffers); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2789 | } |
| 2790 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2791 | /* XXX U-BOOT XXX */ |
| 2792 | #if 0 |
| 2793 | EXPORT_SYMBOL_GPL(nand_scan); |
| 2794 | EXPORT_SYMBOL_GPL(nand_scan_ident); |
| 2795 | EXPORT_SYMBOL_GPL(nand_scan_tail); |
| 2796 | EXPORT_SYMBOL_GPL(nand_release); |
| 2797 | |
| 2798 | static int __init nand_base_init(void) |
| 2799 | { |
| 2800 | led_trigger_register_simple("nand-disk", &nand_led_trigger); |
| 2801 | return 0; |
| 2802 | } |
| 2803 | |
| 2804 | static void __exit nand_base_exit(void) |
| 2805 | { |
| 2806 | led_trigger_unregister_simple(nand_led_trigger); |
| 2807 | } |
| 2808 | |
| 2809 | module_init(nand_base_init); |
| 2810 | module_exit(nand_base_exit); |
| 2811 | |
| 2812 | MODULE_LICENSE("GPL"); |
| 2813 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>"); |
| 2814 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2815 | #endif |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2816 | |
| 2817 | #endif |
| 2818 | |