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