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