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