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