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