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