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