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