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