Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2 | * Overview: |
| 3 | * This is the generic MTD driver for NAND flash devices. It should be |
| 4 | * capable of working with almost all NAND chips currently available. |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 6 | * Additional technical information is available on |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 8 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 11 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 12 | * Credits: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 15 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 16 | * rework for 2K page size chips |
| 17 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 18 | * TODO: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 19 | * Enable cached programming for 2k page size chips |
| 20 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 21 | * if we have HW ECC support. |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 23 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 31 | #include <log.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 32 | #include <malloc.h> |
| 33 | #include <watchdog.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 34 | #include <dm/devres.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 35 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 36 | #include <linux/bug.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 37 | #include <linux/delay.h> |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 38 | #include <linux/err.h> |
Mike Frysinger | 11d1a09 | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 39 | #include <linux/compat.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 40 | #include <linux/mtd/mtd.h> |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 41 | #include <linux/mtd/rawnand.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 42 | #include <linux/mtd/nand_ecc.h> |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 43 | #include <linux/mtd/nand_bch.h> |
Stefan Roese | fa252ea | 2009-04-24 15:58:33 +0200 | [diff] [blame] | 44 | #ifdef CONFIG_MTD_PARTITIONS |
| 45 | #include <linux/mtd/partitions.h> |
| 46 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 47 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 48 | #include <linux/errno.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 49 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 50 | /* Define default oob placement schemes for large and small page devices */ |
Gregory CLEMENT | e5b9631 | 2019-04-17 11:22:05 +0200 | [diff] [blame] | 51 | #ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 52 | static struct nand_ecclayout nand_oob_8 = { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 53 | .eccbytes = 3, |
| 54 | .eccpos = {0, 1, 2}, |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 55 | .oobfree = { |
| 56 | {.offset = 3, |
| 57 | .length = 2}, |
| 58 | {.offset = 6, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 59 | .length = 2} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 62 | static struct nand_ecclayout nand_oob_16 = { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 63 | .eccbytes = 6, |
| 64 | .eccpos = {0, 1, 2, 3, 6, 7}, |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 65 | .oobfree = { |
| 66 | {.offset = 8, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 67 | . length = 8} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 70 | static struct nand_ecclayout nand_oob_64 = { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 71 | .eccbytes = 24, |
| 72 | .eccpos = { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 73 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 74 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 75 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 76 | .oobfree = { |
| 77 | {.offset = 2, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 78 | .length = 38} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 81 | static struct nand_ecclayout nand_oob_128 = { |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 82 | .eccbytes = 48, |
| 83 | .eccpos = { |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 84 | 80, 81, 82, 83, 84, 85, 86, 87, |
| 85 | 88, 89, 90, 91, 92, 93, 94, 95, |
| 86 | 96, 97, 98, 99, 100, 101, 102, 103, |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 87 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 88 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 89 | 120, 121, 122, 123, 124, 125, 126, 127}, |
| 90 | .oobfree = { |
| 91 | {.offset = 2, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 92 | .length = 78} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 93 | }; |
Stefan Agner | bd18614 | 2018-12-06 14:57:09 +0100 | [diff] [blame] | 94 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 95 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 96 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 97 | |
| 98 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 99 | struct mtd_oob_ops *ops); |
| 100 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 101 | /* |
| 102 | * For devices which display every fart in the system on a separate LED. Is |
| 103 | * compiled away when LED support is disabled. |
| 104 | */ |
| 105 | DEFINE_LED_TRIGGER(nand_led_trigger); |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 106 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 107 | static int check_offs_len(struct mtd_info *mtd, |
| 108 | loff_t ofs, uint64_t len) |
| 109 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 110 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 111 | int ret = 0; |
| 112 | |
| 113 | /* Start address must align on block boundary */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 114 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
| 115 | pr_debug("%s: unaligned address\n", __func__); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 116 | ret = -EINVAL; |
| 117 | } |
| 118 | |
| 119 | /* Length must align on block boundary */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 120 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
| 121 | pr_debug("%s: length not block aligned\n", __func__); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 122 | ret = -EINVAL; |
| 123 | } |
| 124 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 125 | return ret; |
| 126 | } |
| 127 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 128 | /** |
| 129 | * nand_release_device - [GENERIC] release chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 130 | * @mtd: MTD device structure |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 131 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 132 | * Release chip lock and wake up anyone waiting on the device. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 133 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 134 | static void nand_release_device(struct mtd_info *mtd) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 135 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 136 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 137 | |
| 138 | /* De-select the NAND device */ |
| 139 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 140 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * nand_read_byte - [DEFAULT] read one byte from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 144 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 145 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 146 | * Default read function for 8bit buswidth |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 147 | */ |
Simon Schwarz | 5a9fc19 | 2011-10-31 06:34:44 +0000 | [diff] [blame] | 148 | uint8_t nand_read_byte(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 149 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 150 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 151 | return readb(chip->IO_ADDR_R); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 155 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 156 | * @mtd: MTD device structure |
| 157 | * |
| 158 | * Default read function for 16bit buswidth with endianness conversion. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 159 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 160 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 161 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 162 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 163 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 164 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * nand_read_word - [DEFAULT] read one word from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 169 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 170 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 171 | * Default read function for 16bit buswidth without endianness conversion. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 172 | */ |
| 173 | static u16 nand_read_word(struct mtd_info *mtd) |
| 174 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 175 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 176 | return readw(chip->IO_ADDR_R); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * nand_select_chip - [DEFAULT] control CE line |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 181 | * @mtd: MTD device structure |
| 182 | * @chipnr: chipnumber to select, -1 for deselect |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 183 | * |
| 184 | * Default select function for 1 chip devices. |
| 185 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 186 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 187 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 188 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 189 | |
| 190 | switch (chipnr) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 191 | case -1: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 192 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 193 | break; |
| 194 | case 0: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 195 | break; |
| 196 | |
| 197 | default: |
| 198 | BUG(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 203 | * nand_write_byte - [DEFAULT] write single byte to chip |
| 204 | * @mtd: MTD device structure |
| 205 | * @byte: value to write |
| 206 | * |
| 207 | * Default function to write a byte to I/O[7:0] |
| 208 | */ |
| 209 | static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) |
| 210 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 211 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 212 | |
| 213 | chip->write_buf(mtd, &byte, 1); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 |
| 218 | * @mtd: MTD device structure |
| 219 | * @byte: value to write |
| 220 | * |
| 221 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. |
| 222 | */ |
| 223 | static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) |
| 224 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 225 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 226 | uint16_t word = byte; |
| 227 | |
| 228 | /* |
| 229 | * It's not entirely clear what should happen to I/O[15:8] when writing |
| 230 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: |
| 231 | * |
| 232 | * When the host supports a 16-bit bus width, only data is |
| 233 | * transferred at the 16-bit width. All address and command line |
| 234 | * transfers shall use only the lower 8-bits of the data bus. During |
| 235 | * command transfers, the host may place any value on the upper |
| 236 | * 8-bits of the data bus. During address transfers, the host shall |
| 237 | * set the upper 8-bits of the data bus to 00h. |
| 238 | * |
| 239 | * One user of the write_byte callback is nand_onfi_set_features. The |
| 240 | * four parameters are specified to be written to I/O[7:0], but this is |
| 241 | * neither an address nor a command transfer. Let's assume a 0 on the |
| 242 | * upper I/O lines is OK. |
| 243 | */ |
| 244 | chip->write_buf(mtd, (uint8_t *)&word, 2); |
| 245 | } |
| 246 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 247 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 248 | * nand_write_buf - [DEFAULT] write buffer to chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 249 | * @mtd: MTD device structure |
| 250 | * @buf: data buffer |
| 251 | * @len: number of bytes to write |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 252 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 253 | * Default write function for 8bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 254 | */ |
Simon Schwarz | 5a9fc19 | 2011-10-31 06:34:44 +0000 | [diff] [blame] | 255 | void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 256 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 257 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 258 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 259 | iowrite8_rep(chip->IO_ADDR_W, buf, len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 263 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 264 | * @mtd: MTD device structure |
| 265 | * @buf: buffer to store date |
| 266 | * @len: number of bytes to read |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 267 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 268 | * Default read function for 8bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 269 | */ |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 270 | void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 271 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 272 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 273 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 274 | ioread8_rep(chip->IO_ADDR_R, buf, len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 277 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 278 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 279 | * @mtd: MTD device structure |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 280 | * @buf: data buffer |
| 281 | * @len: number of bytes to write |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 282 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 283 | * Default write function for 16bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 284 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 285 | void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 286 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 287 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 288 | u16 *p = (u16 *) buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 289 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 290 | iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 294 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 295 | * @mtd: MTD device structure |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 296 | * @buf: buffer to store date |
| 297 | * @len: number of bytes to read |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 298 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 299 | * Default read function for 16bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 300 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 301 | void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 302 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 303 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 304 | u16 *p = (u16 *) buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 305 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 306 | ioread16_rep(chip->IO_ADDR_R, p, len >> 1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 311 | * @mtd: MTD device structure |
| 312 | * @ofs: offset from device start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 313 | * |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 314 | * Check, if the block is bad. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 315 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 316 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 317 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 318 | int page, res = 0, i = 0; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 319 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 320 | u16 bad; |
| 321 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 322 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 323 | ofs += mtd->erasesize - mtd->writesize; |
| 324 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 325 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Thomas Knobloch | 9e2aeaf | 2007-05-05 07:04:42 +0200 | [diff] [blame] | 326 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 327 | do { |
| 328 | if (chip->options & NAND_BUSWIDTH_16) { |
| 329 | chip->cmdfunc(mtd, NAND_CMD_READOOB, |
| 330 | chip->badblockpos & 0xFE, page); |
| 331 | bad = cpu_to_le16(chip->read_word(mtd)); |
| 332 | if (chip->badblockpos & 0x1) |
| 333 | bad >>= 8; |
| 334 | else |
| 335 | bad &= 0xFF; |
| 336 | } else { |
| 337 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, |
| 338 | page); |
| 339 | bad = chip->read_byte(mtd); |
| 340 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 341 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 342 | if (likely(chip->badblockbits == 8)) |
| 343 | res = bad != 0xFF; |
| 344 | else |
| 345 | res = hweight8(bad) < chip->badblockbits; |
| 346 | ofs += mtd->writesize; |
| 347 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 348 | i++; |
| 349 | } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE)); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 350 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 351 | return res; |
| 352 | } |
| 353 | |
| 354 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 355 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 356 | * @mtd: MTD device structure |
| 357 | * @ofs: offset from device start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 358 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 359 | * This is the default implementation, which can be overridden by a hardware |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 360 | * specific driver. It provides the details for writing a bad block marker to a |
| 361 | * block. |
| 362 | */ |
| 363 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 364 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 365 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 366 | struct mtd_oob_ops ops; |
| 367 | uint8_t buf[2] = { 0, 0 }; |
| 368 | int ret = 0, res, i = 0; |
| 369 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 370 | memset(&ops, 0, sizeof(ops)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 371 | ops.oobbuf = buf; |
| 372 | ops.ooboffs = chip->badblockpos; |
| 373 | if (chip->options & NAND_BUSWIDTH_16) { |
| 374 | ops.ooboffs &= ~0x01; |
| 375 | ops.len = ops.ooblen = 2; |
| 376 | } else { |
| 377 | ops.len = ops.ooblen = 1; |
| 378 | } |
| 379 | ops.mode = MTD_OPS_PLACE_OOB; |
| 380 | |
| 381 | /* Write to first/last page(s) if necessary */ |
| 382 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 383 | ofs += mtd->erasesize - mtd->writesize; |
| 384 | do { |
| 385 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 386 | if (!ret) |
| 387 | ret = res; |
| 388 | |
| 389 | i++; |
| 390 | ofs += mtd->writesize; |
| 391 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 392 | |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * nand_block_markbad_lowlevel - mark a block bad |
| 398 | * @mtd: MTD device structure |
| 399 | * @ofs: offset from device start |
| 400 | * |
| 401 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 402 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
| 403 | * specify how to write bad block markers to OOB (chip->block_markbad). |
| 404 | * |
| 405 | * We try operations in the following order: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 406 | * (1) erase the affected block, to allow OOB marker to be written cleanly |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 407 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 408 | * NAND_BBT_NO_OOB_BBM is present) |
| 409 | * (3) update the BBT |
| 410 | * Note that we retain the first error encountered in (2) or (3), finish the |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 411 | * procedures, and dump the error in the end. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 412 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 413 | static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 414 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 415 | struct nand_chip *chip = mtd_to_nand(mtd); |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 416 | int ret = 0; |
| 417 | #ifndef CONFIG_SPL_BUILD |
| 418 | int res; |
| 419 | #endif |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 420 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 421 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 422 | struct erase_info einfo; |
| 423 | |
| 424 | /* Attempt erase before marking OOB */ |
| 425 | memset(&einfo, 0, sizeof(einfo)); |
| 426 | einfo.mtd = mtd; |
| 427 | einfo.addr = ofs; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 428 | einfo.len = 1ULL << chip->phys_erase_shift; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 429 | nand_erase_nand(mtd, &einfo, 0); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 430 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 431 | /* Write bad block marker to OOB */ |
| 432 | nand_get_device(mtd, FL_WRITING); |
| 433 | ret = chip->block_markbad(mtd, ofs); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 434 | nand_release_device(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 435 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 436 | |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 437 | #ifndef CONFIG_SPL_BUILD |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 438 | /* Mark block bad in BBT */ |
| 439 | if (chip->bbt) { |
| 440 | res = nand_markbad_bbt(mtd, ofs); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 441 | if (!ret) |
| 442 | ret = res; |
| 443 | } |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 444 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 445 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 446 | if (!ret) |
| 447 | mtd->ecc_stats.badblocks++; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 448 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 449 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 452 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 453 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 454 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 455 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 456 | * Check, if the device is write protected. The function expects, that the |
| 457 | * device is already selected. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 458 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 459 | static int nand_check_wp(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 460 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 461 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 462 | u8 status; |
| 463 | int ret; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 464 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 465 | /* Broken xD cards report WP despite being writable */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 466 | if (chip->options & NAND_BROKEN_XD) |
| 467 | return 0; |
| 468 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 469 | /* Check the WP bit */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 470 | ret = nand_status_op(chip, &status); |
| 471 | if (ret) |
| 472 | return ret; |
| 473 | |
| 474 | return status & NAND_STATUS_WP ? 0 : 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 475 | } |
Markus Klotzbücher | 27eba14 | 2006-03-06 15:04:25 +0100 | [diff] [blame] | 476 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 477 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 478 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 479 | * @mtd: MTD device structure |
| 480 | * @ofs: offset from device start |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 481 | * |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 482 | * Check if the block is marked as reserved. |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 483 | */ |
| 484 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 485 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 486 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 487 | |
| 488 | if (!chip->bbt) |
| 489 | return 0; |
| 490 | /* Return info from the table */ |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 491 | #ifndef CONFIG_SPL_BUILD |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 492 | return nand_isreserved_bbt(mtd, ofs); |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 493 | #else |
| 494 | return 0; |
| 495 | #endif |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /** |
| 499 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 500 | * @mtd: MTD device structure |
| 501 | * @ofs: offset from device start |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 502 | * @allowbbt: 1, if its allowed to access the bbt area |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 503 | * |
| 504 | * Check, if the block is bad. Either by reading the bad block table or |
| 505 | * calling of the scan function. |
| 506 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 507 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 508 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 509 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 510 | |
Masahiro Yamada | 8d10054 | 2014-12-26 22:20:58 +0900 | [diff] [blame] | 511 | if (!(chip->options & NAND_SKIP_BBTSCAN) && |
| 512 | !(chip->options & NAND_BBT_SCANNED)) { |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 513 | chip->options |= NAND_BBT_SCANNED; |
Masahiro Yamada | 8c6c14a | 2014-12-26 22:20:57 +0900 | [diff] [blame] | 514 | chip->scan_bbt(mtd); |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 515 | } |
| 516 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 517 | if (!chip->bbt) |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 518 | return chip->block_bad(mtd, ofs); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 519 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 520 | /* Return info from the table */ |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 521 | #ifndef CONFIG_SPL_BUILD |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 522 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 523 | #else |
| 524 | return 0; |
| 525 | #endif |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 526 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 527 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 528 | /** |
| 529 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 530 | * @mtd: MTD device structure |
| 531 | * |
| 532 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 533 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 534 | void nand_wait_ready(struct mtd_info *mtd) |
| 535 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 536 | struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 537 | u32 timeo = (CONFIG_SYS_HZ * 400) / 1000; |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 538 | u32 time_start; |
Stefan Roese | a5c312c | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 539 | |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 540 | time_start = get_timer(0); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 541 | /* Wait until command is processed or timeout occurs */ |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 542 | while (get_timer(time_start) < timeo) { |
Stefan Roese | a5c312c | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 543 | if (chip->dev_ready) |
| 544 | if (chip->dev_ready(mtd)) |
| 545 | break; |
| 546 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 547 | |
| 548 | if (!chip->dev_ready(mtd)) |
| 549 | pr_warn("timeout while waiting for chip to become ready\n"); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 550 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 551 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 552 | |
| 553 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 554 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 555 | * @mtd: MTD device structure |
| 556 | * @timeo: Timeout in ms |
| 557 | * |
| 558 | * Wait for status ready (i.e. command done) or timeout. |
| 559 | */ |
| 560 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 561 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 562 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 563 | u32 time_start; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 564 | int ret; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 565 | |
| 566 | timeo = (CONFIG_SYS_HZ * timeo) / 1000; |
| 567 | time_start = get_timer(0); |
| 568 | while (get_timer(time_start) < timeo) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 569 | u8 status; |
| 570 | |
| 571 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 572 | if (ret) |
| 573 | return; |
| 574 | |
| 575 | if (status & NAND_STATUS_READY) |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 576 | break; |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 577 | schedule(); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 578 | } |
| 579 | }; |
| 580 | |
| 581 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 582 | * nand_command - [DEFAULT] Send command to NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 583 | * @mtd: MTD device structure |
| 584 | * @command: the command to be sent |
| 585 | * @column: the column address for this command, -1 if none |
| 586 | * @page_addr: the page address for this command, -1 if none |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 587 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 588 | * Send command to NAND device. This function is used for small page devices |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 589 | * (512 Bytes per page). |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 590 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 591 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 592 | int column, int page_addr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 593 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 594 | register struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 595 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 596 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 597 | /* Write out the command to the device */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 598 | if (command == NAND_CMD_SEQIN) { |
| 599 | int readcmd; |
| 600 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 601 | if (column >= mtd->writesize) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 602 | /* OOB area */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 603 | column -= mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 604 | readcmd = NAND_CMD_READOOB; |
| 605 | } else if (column < 256) { |
| 606 | /* First 256 bytes --> READ0 */ |
| 607 | readcmd = NAND_CMD_READ0; |
| 608 | } else { |
| 609 | column -= 256; |
| 610 | readcmd = NAND_CMD_READ1; |
| 611 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 612 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
| 613 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 614 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 615 | chip->cmd_ctrl(mtd, command, ctrl); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 616 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 617 | /* Address cycle, when necessary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 618 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 619 | /* Serially input address */ |
| 620 | if (column != -1) { |
| 621 | /* Adjust columns for 16 bit buswidth */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 622 | if (chip->options & NAND_BUSWIDTH_16 && |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 623 | !nand_opcode_8bits(command)) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 624 | column >>= 1; |
| 625 | chip->cmd_ctrl(mtd, column, ctrl); |
| 626 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 627 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 628 | if (page_addr != -1) { |
| 629 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 630 | ctrl &= ~NAND_CTRL_CHANGE; |
| 631 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 632 | if (chip->options & NAND_ROW_ADDR_3) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 633 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
| 634 | } |
| 635 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 636 | |
| 637 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 638 | * Program and erase have their own busy handlers status and sequential |
| 639 | * in needs no delay |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 640 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 641 | switch (command) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 642 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 643 | case NAND_CMD_PAGEPROG: |
| 644 | case NAND_CMD_ERASE1: |
| 645 | case NAND_CMD_ERASE2: |
| 646 | case NAND_CMD_SEQIN: |
| 647 | case NAND_CMD_STATUS: |
Masahiro Yamada | 7f9baa1 | 2017-09-15 21:44:58 +0900 | [diff] [blame] | 648 | case NAND_CMD_READID: |
Masahiro Yamada | 0cd1018 | 2017-09-15 21:44:59 +0900 | [diff] [blame] | 649 | case NAND_CMD_SET_FEATURES: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 650 | return; |
| 651 | |
| 652 | case NAND_CMD_RESET: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 653 | if (chip->dev_ready) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 654 | break; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 655 | udelay(chip->chip_delay); |
| 656 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 657 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 658 | chip->cmd_ctrl(mtd, |
| 659 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 660 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 661 | nand_wait_status_ready(mtd, 250); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 662 | return; |
| 663 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 664 | /* This applies to read commands */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 665 | default: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 666 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 667 | * If we don't have access to the busy pin, we apply the given |
| 668 | * command delay |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 669 | */ |
| 670 | if (!chip->dev_ready) { |
| 671 | udelay(chip->chip_delay); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 672 | return; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 673 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 674 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 675 | /* |
| 676 | * Apply this short delay always to ensure that we do wait tWB in |
| 677 | * any case on any machine. |
| 678 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 679 | ndelay(100); |
| 680 | |
| 681 | nand_wait_ready(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /** |
| 685 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 686 | * @mtd: MTD device structure |
| 687 | * @command: the command to be sent |
| 688 | * @column: the column address for this command, -1 if none |
| 689 | * @page_addr: the page address for this command, -1 if none |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 690 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 691 | * Send command to NAND device. This is the version for the new large page |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 692 | * devices. We don't have the separate regions as we have in the small page |
| 693 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 694 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 695 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 696 | int column, int page_addr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 697 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 698 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 699 | |
| 700 | /* Emulate NAND_CMD_READOOB */ |
| 701 | if (command == NAND_CMD_READOOB) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 702 | column += mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 703 | command = NAND_CMD_READ0; |
| 704 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 705 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 706 | /* Command latch cycle */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 707 | chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 708 | |
| 709 | if (column != -1 || page_addr != -1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 710 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 711 | |
| 712 | /* Serially input address */ |
| 713 | if (column != -1) { |
| 714 | /* Adjust columns for 16 bit buswidth */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 715 | if (chip->options & NAND_BUSWIDTH_16 && |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 716 | !nand_opcode_8bits(command)) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 717 | column >>= 1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 718 | chip->cmd_ctrl(mtd, column, ctrl); |
| 719 | ctrl &= ~NAND_CTRL_CHANGE; |
| 720 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 721 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 722 | if (page_addr != -1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 723 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 724 | chip->cmd_ctrl(mtd, page_addr >> 8, |
| 725 | NAND_NCE | NAND_ALE); |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 726 | if (chip->options & NAND_ROW_ADDR_3) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 727 | chip->cmd_ctrl(mtd, page_addr >> 16, |
| 728 | NAND_NCE | NAND_ALE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 729 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 730 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 731 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 732 | |
| 733 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 734 | * Program and erase have their own busy handlers status, sequential |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 735 | * in and status need no delay. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 736 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 737 | switch (command) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 738 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 739 | case NAND_CMD_CACHEDPROG: |
| 740 | case NAND_CMD_PAGEPROG: |
| 741 | case NAND_CMD_ERASE1: |
| 742 | case NAND_CMD_ERASE2: |
| 743 | case NAND_CMD_SEQIN: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 744 | case NAND_CMD_RNDIN: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 745 | case NAND_CMD_STATUS: |
Masahiro Yamada | 7f9baa1 | 2017-09-15 21:44:58 +0900 | [diff] [blame] | 746 | case NAND_CMD_READID: |
Masahiro Yamada | 0cd1018 | 2017-09-15 21:44:59 +0900 | [diff] [blame] | 747 | case NAND_CMD_SET_FEATURES: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 748 | return; |
| 749 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 750 | case NAND_CMD_RESET: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 751 | if (chip->dev_ready) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 752 | break; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 753 | udelay(chip->chip_delay); |
| 754 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 755 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 756 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 757 | NAND_NCE | NAND_CTRL_CHANGE); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 758 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 759 | nand_wait_status_ready(mtd, 250); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 760 | return; |
| 761 | |
| 762 | case NAND_CMD_RNDOUT: |
| 763 | /* No ready / busy check necessary */ |
| 764 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 765 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 766 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 767 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 768 | return; |
| 769 | |
| 770 | case NAND_CMD_READ0: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 771 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 772 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 773 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 774 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 775 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 776 | /* This applies to read commands */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 777 | default: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 778 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 779 | * If we don't have access to the busy pin, we apply the given |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 780 | * command delay. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 781 | */ |
| 782 | if (!chip->dev_ready) { |
| 783 | udelay(chip->chip_delay); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 784 | return; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 785 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 786 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 787 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 788 | /* |
| 789 | * Apply this short delay always to ensure that we do wait tWB in |
| 790 | * any case on any machine. |
| 791 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 792 | ndelay(100); |
| 793 | |
| 794 | nand_wait_ready(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 798 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 799 | * @chip: the nand chip descriptor |
| 800 | * @mtd: MTD device structure |
| 801 | * @new_state: the state which is requested |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 802 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 803 | * Used when in panic, no locks are taken. |
| 804 | */ |
| 805 | static void panic_nand_get_device(struct nand_chip *chip, |
| 806 | struct mtd_info *mtd, int new_state) |
| 807 | { |
| 808 | /* Hardware controller shared among independent devices */ |
| 809 | chip->controller->active = chip; |
| 810 | chip->state = new_state; |
| 811 | } |
| 812 | |
| 813 | /** |
| 814 | * nand_get_device - [GENERIC] Get chip for selected access |
| 815 | * @mtd: MTD device structure |
| 816 | * @new_state: the state which is requested |
| 817 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 818 | * Get the device and lock it for exclusive access |
| 819 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 820 | static int |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 821 | nand_get_device(struct mtd_info *mtd, int new_state) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 822 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 823 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 824 | chip->state = new_state; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 825 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /** |
| 829 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 830 | * @mtd: MTD device structure |
| 831 | * @chip: NAND chip structure |
| 832 | * @timeo: timeout |
| 833 | * |
| 834 | * Wait for command done. This is a helper function for nand_wait used when |
| 835 | * we are in interrupt context. May happen when in panic and trying to write |
| 836 | * an oops through mtdoops. |
| 837 | */ |
| 838 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 839 | unsigned long timeo) |
| 840 | { |
| 841 | int i; |
| 842 | for (i = 0; i < timeo; i++) { |
| 843 | if (chip->dev_ready) { |
| 844 | if (chip->dev_ready(mtd)) |
| 845 | break; |
| 846 | } else { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 847 | int ret; |
| 848 | u8 status; |
| 849 | |
| 850 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 851 | true); |
| 852 | if (ret) |
| 853 | return; |
| 854 | |
| 855 | if (status & NAND_STATUS_READY) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 856 | break; |
| 857 | } |
| 858 | mdelay(1); |
| 859 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 860 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 861 | |
| 862 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 863 | * nand_wait - [DEFAULT] wait until the command is done |
| 864 | * @mtd: MTD device structure |
| 865 | * @chip: NAND chip structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 866 | * |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 867 | * Wait for command done. This applies to erase and program only. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 868 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 869 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 870 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 871 | unsigned long timeo = 400; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 872 | u8 status; |
| 873 | int ret; |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 874 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 875 | led_trigger_event(nand_led_trigger, LED_FULL); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 876 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 877 | /* |
| 878 | * Apply this short delay always to ensure that we do wait tWB in any |
| 879 | * case on any machine. |
| 880 | */ |
| 881 | ndelay(100); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 882 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 883 | ret = nand_status_op(chip, NULL); |
| 884 | if (ret) |
| 885 | return ret; |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 886 | |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 887 | u32 timer = (CONFIG_SYS_HZ * timeo) / 1000; |
| 888 | u32 time_start; |
Wolfgang Denk | 9d328a6 | 2021-09-27 17:42:38 +0200 | [diff] [blame] | 889 | |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 890 | time_start = get_timer(0); |
| 891 | while (get_timer(time_start) < timer) { |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 892 | if (chip->dev_ready) { |
| 893 | if (chip->dev_ready(mtd)) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 894 | break; |
| 895 | } else { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 896 | ret = nand_read_data_op(chip, &status, |
| 897 | sizeof(status), true); |
| 898 | if (ret) |
| 899 | return ret; |
| 900 | |
| 901 | if (status & NAND_STATUS_READY) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 902 | break; |
| 903 | } |
| 904 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 905 | led_trigger_event(nand_led_trigger, LED_OFF); |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 906 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 907 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 908 | if (ret) |
| 909 | return ret; |
| 910 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 911 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 912 | WARN_ON(!(status & NAND_STATUS_READY)); |
| 913 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 914 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 915 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 916 | /** |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 917 | * nand_reset_data_interface - Reset data interface and timings |
| 918 | * @chip: The NAND chip |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 919 | * @chipnr: Internal die id |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 920 | * |
| 921 | * Reset the Data interface and timings to ONFI mode 0. |
| 922 | * |
| 923 | * Returns 0 for success or negative error code otherwise. |
| 924 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 925 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 926 | { |
| 927 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 928 | const struct nand_data_interface *conf; |
| 929 | int ret; |
| 930 | |
| 931 | if (!chip->setup_data_interface) |
| 932 | return 0; |
| 933 | |
| 934 | /* |
| 935 | * The ONFI specification says: |
| 936 | * " |
| 937 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 938 | * interface, the host shall use the Reset (FFh) command |
| 939 | * using SDR timing mode 0. A device in any timing mode is |
| 940 | * required to recognize Reset (FFh) command issued in SDR |
| 941 | * timing mode 0. |
| 942 | * " |
| 943 | * |
| 944 | * Configure the data interface in SDR mode and set the |
| 945 | * timings to timing mode 0. |
| 946 | */ |
| 947 | |
| 948 | conf = nand_get_default_data_interface(); |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 949 | ret = chip->setup_data_interface(mtd, chipnr, conf); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 950 | if (ret) |
| 951 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 952 | |
| 953 | return ret; |
| 954 | } |
| 955 | |
Kory Maincent | 0cda0cb | 2022-06-22 11:11:45 +0200 | [diff] [blame] | 956 | static int nand_onfi_set_timings(struct mtd_info *mtd, struct nand_chip *chip) |
| 957 | { |
| 958 | if (!chip->onfi_version || |
| 959 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 960 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
| 961 | return 0; |
| 962 | |
| 963 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 964 | chip->onfi_timing_mode_default, |
| 965 | }; |
| 966 | |
| 967 | return chip->onfi_set_features(mtd, chip, |
| 968 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 969 | tmode_param); |
| 970 | } |
| 971 | |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 972 | /** |
| 973 | * nand_setup_data_interface - Setup the best data interface and timings |
| 974 | * @chip: The NAND chip |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 975 | * @chipnr: Internal die id |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 976 | * |
| 977 | * Find and configure the best data interface and NAND timings supported by |
| 978 | * the chip and the driver. |
| 979 | * First tries to retrieve supported timing modes from ONFI information, |
| 980 | * and if the NAND chip does not support ONFI, relies on the |
| 981 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 982 | * |
| 983 | * Returns 0 for success or negative error code otherwise. |
| 984 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 985 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 986 | { |
| 987 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 988 | int ret; |
| 989 | |
| 990 | if (!chip->setup_data_interface || !chip->data_interface) |
| 991 | return 0; |
| 992 | |
| 993 | /* |
| 994 | * Ensure the timing mode has been changed on the chip side |
| 995 | * before changing timings on the controller side. |
| 996 | */ |
Kory Maincent | 0cda0cb | 2022-06-22 11:11:45 +0200 | [diff] [blame] | 997 | ret = nand_onfi_set_timings(mtd, chip); |
| 998 | if (ret) |
| 999 | goto err; |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1000 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1001 | ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1002 | err: |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * nand_init_data_interface - find the best data interface and timings |
| 1008 | * @chip: The NAND chip |
| 1009 | * |
| 1010 | * Find the best data interface and NAND timings supported by the chip |
| 1011 | * and the driver. |
| 1012 | * First tries to retrieve supported timing modes from ONFI information, |
| 1013 | * and if the NAND chip does not support ONFI, relies on the |
| 1014 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1015 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1016 | * available. |
| 1017 | * |
| 1018 | * Returns 0 for success or negative error code otherwise. |
| 1019 | */ |
| 1020 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1021 | { |
| 1022 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1023 | int modes, mode, ret; |
| 1024 | |
| 1025 | if (!chip->setup_data_interface) |
| 1026 | return 0; |
| 1027 | |
| 1028 | /* |
| 1029 | * First try to identify the best timings from ONFI parameters and |
| 1030 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1031 | * timing mode. |
| 1032 | */ |
| 1033 | modes = onfi_get_async_timing_mode(chip); |
| 1034 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1035 | if (!chip->onfi_timing_mode_default) |
| 1036 | return 0; |
| 1037 | |
| 1038 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1039 | } |
| 1040 | |
| 1041 | chip->data_interface = kzalloc(sizeof(*chip->data_interface), |
| 1042 | GFP_KERNEL); |
| 1043 | if (!chip->data_interface) |
| 1044 | return -ENOMEM; |
| 1045 | |
| 1046 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
| 1047 | ret = onfi_init_data_interface(chip, chip->data_interface, |
| 1048 | NAND_SDR_IFACE, mode); |
| 1049 | if (ret) |
| 1050 | continue; |
| 1051 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1052 | /* Pass -1 to only */ |
| 1053 | ret = chip->setup_data_interface(mtd, |
| 1054 | NAND_DATA_IFACE_CHECK_ONLY, |
| 1055 | chip->data_interface); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1056 | if (!ret) { |
| 1057 | chip->onfi_timing_mode_default = mode; |
| 1058 | break; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static void __maybe_unused nand_release_data_interface(struct nand_chip *chip) |
| 1066 | { |
| 1067 | kfree(chip->data_interface); |
| 1068 | } |
| 1069 | |
| 1070 | /** |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1071 | * nand_read_page_op - Do a READ PAGE operation |
| 1072 | * @chip: The NAND chip |
| 1073 | * @page: page to read |
| 1074 | * @offset_in_page: offset within the page |
| 1075 | * @buf: buffer used to store the data |
| 1076 | * @len: length of the buffer |
| 1077 | * |
| 1078 | * This function issues a READ PAGE operation. |
| 1079 | * This function does not select/unselect the CS line. |
| 1080 | * |
| 1081 | * Returns 0 on success, a negative error code otherwise. |
| 1082 | */ |
| 1083 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1084 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 1085 | { |
| 1086 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1087 | |
| 1088 | if (len && !buf) |
| 1089 | return -EINVAL; |
| 1090 | |
| 1091 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1092 | return -EINVAL; |
| 1093 | |
| 1094 | chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page); |
| 1095 | if (len) |
| 1096 | chip->read_buf(mtd, buf, len); |
| 1097 | |
| 1098 | return 0; |
| 1099 | } |
| 1100 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 1101 | |
| 1102 | /** |
| 1103 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 1104 | * @chip: The NAND chip |
| 1105 | * @page: parameter page to read |
| 1106 | * @buf: buffer used to store the data |
| 1107 | * @len: length of the buffer |
| 1108 | * |
| 1109 | * This function issues a READ PARAMETER PAGE operation. |
| 1110 | * This function does not select/unselect the CS line. |
| 1111 | * |
| 1112 | * Returns 0 on success, a negative error code otherwise. |
| 1113 | */ |
| 1114 | static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1115 | unsigned int len) |
| 1116 | { |
| 1117 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1118 | unsigned int i; |
| 1119 | u8 *p = buf; |
| 1120 | |
| 1121 | if (len && !buf) |
| 1122 | return -EINVAL; |
| 1123 | |
| 1124 | chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1); |
| 1125 | for (i = 0; i < len; i++) |
| 1126 | p[i] = chip->read_byte(mtd); |
| 1127 | |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1133 | * @chip: The NAND chip |
| 1134 | * @offset_in_page: offset within the page |
| 1135 | * @buf: buffer used to store the data |
| 1136 | * @len: length of the buffer |
| 1137 | * @force_8bit: force 8-bit bus access |
| 1138 | * |
| 1139 | * This function issues a CHANGE READ COLUMN operation. |
| 1140 | * This function does not select/unselect the CS line. |
| 1141 | * |
| 1142 | * Returns 0 on success, a negative error code otherwise. |
| 1143 | */ |
| 1144 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1145 | unsigned int offset_in_page, void *buf, |
| 1146 | unsigned int len, bool force_8bit) |
| 1147 | { |
| 1148 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1149 | |
| 1150 | if (len && !buf) |
| 1151 | return -EINVAL; |
| 1152 | |
| 1153 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1154 | return -EINVAL; |
| 1155 | |
| 1156 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1); |
| 1157 | if (len) |
| 1158 | chip->read_buf(mtd, buf, len); |
| 1159 | |
| 1160 | return 0; |
| 1161 | } |
| 1162 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1163 | |
| 1164 | /** |
| 1165 | * nand_read_oob_op - Do a READ OOB operation |
| 1166 | * @chip: The NAND chip |
| 1167 | * @page: page to read |
| 1168 | * @offset_in_oob: offset within the OOB area |
| 1169 | * @buf: buffer used to store the data |
| 1170 | * @len: length of the buffer |
| 1171 | * |
| 1172 | * This function issues a READ OOB operation. |
| 1173 | * This function does not select/unselect the CS line. |
| 1174 | * |
| 1175 | * Returns 0 on success, a negative error code otherwise. |
| 1176 | */ |
| 1177 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1178 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1179 | { |
| 1180 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1181 | |
| 1182 | if (len && !buf) |
| 1183 | return -EINVAL; |
| 1184 | |
| 1185 | if (offset_in_oob + len > mtd->oobsize) |
| 1186 | return -EINVAL; |
| 1187 | |
| 1188 | chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page); |
| 1189 | if (len) |
| 1190 | chip->read_buf(mtd, buf, len); |
| 1191 | |
| 1192 | return 0; |
| 1193 | } |
| 1194 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1195 | |
| 1196 | /** |
| 1197 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1198 | * @chip: The NAND chip |
| 1199 | * @page: page to write |
| 1200 | * @offset_in_page: offset within the page |
| 1201 | * @buf: buffer containing the data to write to the page |
| 1202 | * @len: length of the buffer |
| 1203 | * |
| 1204 | * This function issues the first half of a PROG PAGE operation. |
| 1205 | * This function does not select/unselect the CS line. |
| 1206 | * |
| 1207 | * Returns 0 on success, a negative error code otherwise. |
| 1208 | */ |
| 1209 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1210 | unsigned int offset_in_page, const void *buf, |
| 1211 | unsigned int len) |
| 1212 | { |
| 1213 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1214 | |
| 1215 | if (len && !buf) |
| 1216 | return -EINVAL; |
| 1217 | |
| 1218 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1219 | return -EINVAL; |
| 1220 | |
| 1221 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1222 | |
| 1223 | if (buf) |
| 1224 | chip->write_buf(mtd, buf, len); |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1229 | |
| 1230 | /** |
| 1231 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1232 | * @chip: The NAND chip |
| 1233 | * |
| 1234 | * This function issues the second half of a PROG PAGE operation. |
| 1235 | * This function does not select/unselect the CS line. |
| 1236 | * |
| 1237 | * Returns 0 on success, a negative error code otherwise. |
| 1238 | */ |
| 1239 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1240 | { |
| 1241 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1242 | int status; |
| 1243 | |
| 1244 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1245 | |
| 1246 | status = chip->waitfunc(mtd, chip); |
| 1247 | if (status & NAND_STATUS_FAIL) |
| 1248 | return -EIO; |
| 1249 | |
| 1250 | return 0; |
| 1251 | } |
| 1252 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1253 | |
| 1254 | /** |
| 1255 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1256 | * @chip: The NAND chip |
| 1257 | * @page: page to write |
| 1258 | * @offset_in_page: offset within the page |
| 1259 | * @buf: buffer containing the data to write to the page |
| 1260 | * @len: length of the buffer |
| 1261 | * |
| 1262 | * This function issues a full PROG PAGE operation. |
| 1263 | * This function does not select/unselect the CS line. |
| 1264 | * |
| 1265 | * Returns 0 on success, a negative error code otherwise. |
| 1266 | */ |
| 1267 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1268 | unsigned int offset_in_page, const void *buf, |
| 1269 | unsigned int len) |
| 1270 | { |
| 1271 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1272 | int status; |
| 1273 | |
| 1274 | if (!len || !buf) |
| 1275 | return -EINVAL; |
| 1276 | |
| 1277 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1278 | return -EINVAL; |
| 1279 | |
| 1280 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1281 | chip->write_buf(mtd, buf, len); |
| 1282 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1283 | |
| 1284 | status = chip->waitfunc(mtd, chip); |
| 1285 | if (status & NAND_STATUS_FAIL) |
| 1286 | return -EIO; |
| 1287 | |
| 1288 | return 0; |
| 1289 | } |
| 1290 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1291 | |
| 1292 | /** |
| 1293 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1294 | * @chip: The NAND chip |
| 1295 | * @offset_in_page: offset within the page |
| 1296 | * @buf: buffer containing the data to send to the NAND |
| 1297 | * @len: length of the buffer |
| 1298 | * @force_8bit: force 8-bit bus access |
| 1299 | * |
| 1300 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1301 | * This function does not select/unselect the CS line. |
| 1302 | * |
| 1303 | * Returns 0 on success, a negative error code otherwise. |
| 1304 | */ |
| 1305 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1306 | unsigned int offset_in_page, |
| 1307 | const void *buf, unsigned int len, |
| 1308 | bool force_8bit) |
| 1309 | { |
| 1310 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1311 | |
| 1312 | if (len && !buf) |
| 1313 | return -EINVAL; |
| 1314 | |
| 1315 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1316 | return -EINVAL; |
| 1317 | |
| 1318 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1); |
| 1319 | if (len) |
| 1320 | chip->write_buf(mtd, buf, len); |
| 1321 | |
| 1322 | return 0; |
| 1323 | } |
| 1324 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1325 | |
| 1326 | /** |
| 1327 | * nand_readid_op - Do a READID operation |
| 1328 | * @chip: The NAND chip |
| 1329 | * @addr: address cycle to pass after the READID command |
| 1330 | * @buf: buffer used to store the ID |
| 1331 | * @len: length of the buffer |
| 1332 | * |
| 1333 | * This function sends a READID command and reads back the ID returned by the |
| 1334 | * NAND. |
| 1335 | * This function does not select/unselect the CS line. |
| 1336 | * |
| 1337 | * Returns 0 on success, a negative error code otherwise. |
| 1338 | */ |
| 1339 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1340 | unsigned int len) |
| 1341 | { |
| 1342 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1343 | unsigned int i; |
| 1344 | u8 *id = buf; |
| 1345 | |
| 1346 | if (len && !buf) |
| 1347 | return -EINVAL; |
| 1348 | |
| 1349 | chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1); |
| 1350 | |
| 1351 | for (i = 0; i < len; i++) |
| 1352 | id[i] = chip->read_byte(mtd); |
| 1353 | |
| 1354 | return 0; |
| 1355 | } |
| 1356 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1357 | |
| 1358 | /** |
| 1359 | * nand_status_op - Do a STATUS operation |
| 1360 | * @chip: The NAND chip |
| 1361 | * @status: out variable to store the NAND status |
| 1362 | * |
| 1363 | * This function sends a STATUS command and reads back the status returned by |
| 1364 | * the NAND. |
| 1365 | * This function does not select/unselect the CS line. |
| 1366 | * |
| 1367 | * Returns 0 on success, a negative error code otherwise. |
| 1368 | */ |
| 1369 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1370 | { |
| 1371 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1372 | |
| 1373 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 1374 | if (status) |
| 1375 | *status = chip->read_byte(mtd); |
| 1376 | |
| 1377 | return 0; |
| 1378 | } |
| 1379 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 1380 | |
| 1381 | /** |
| 1382 | * nand_exit_status_op - Exit a STATUS operation |
| 1383 | * @chip: The NAND chip |
| 1384 | * |
| 1385 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 1386 | * command to avoid reading only the status until a new read command is sent. |
| 1387 | * |
| 1388 | * This function does not select/unselect the CS line. |
| 1389 | * |
| 1390 | * Returns 0 on success, a negative error code otherwise. |
| 1391 | */ |
| 1392 | int nand_exit_status_op(struct nand_chip *chip) |
| 1393 | { |
| 1394 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1395 | |
| 1396 | chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1); |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | EXPORT_SYMBOL_GPL(nand_exit_status_op); |
| 1401 | |
| 1402 | /** |
| 1403 | * nand_erase_op - Do an erase operation |
| 1404 | * @chip: The NAND chip |
| 1405 | * @eraseblock: block to erase |
| 1406 | * |
| 1407 | * This function sends an ERASE command and waits for the NAND to be ready |
| 1408 | * before returning. |
| 1409 | * This function does not select/unselect the CS line. |
| 1410 | * |
| 1411 | * Returns 0 on success, a negative error code otherwise. |
| 1412 | */ |
| 1413 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 1414 | { |
| 1415 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1416 | unsigned int page = eraseblock << |
| 1417 | (chip->phys_erase_shift - chip->page_shift); |
| 1418 | int status; |
| 1419 | |
| 1420 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 1421 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
| 1422 | |
| 1423 | status = chip->waitfunc(mtd, chip); |
| 1424 | if (status < 0) |
| 1425 | return status; |
| 1426 | |
| 1427 | if (status & NAND_STATUS_FAIL) |
| 1428 | return -EIO; |
| 1429 | |
| 1430 | return 0; |
| 1431 | } |
| 1432 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 1433 | |
| 1434 | /** |
| 1435 | * nand_set_features_op - Do a SET FEATURES operation |
| 1436 | * @chip: The NAND chip |
| 1437 | * @feature: feature id |
| 1438 | * @data: 4 bytes of data |
| 1439 | * |
| 1440 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 1441 | * ready before returning. |
| 1442 | * This function does not select/unselect the CS line. |
| 1443 | * |
| 1444 | * Returns 0 on success, a negative error code otherwise. |
| 1445 | */ |
| 1446 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 1447 | const void *data) |
| 1448 | { |
| 1449 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1450 | const u8 *params = data; |
| 1451 | int i, status; |
| 1452 | |
| 1453 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1); |
| 1454 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 1455 | chip->write_byte(mtd, params[i]); |
| 1456 | |
| 1457 | status = chip->waitfunc(mtd, chip); |
| 1458 | if (status & NAND_STATUS_FAIL) |
| 1459 | return -EIO; |
| 1460 | |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | /** |
| 1465 | * nand_get_features_op - Do a GET FEATURES operation |
| 1466 | * @chip: The NAND chip |
| 1467 | * @feature: feature id |
| 1468 | * @data: 4 bytes of data |
| 1469 | * |
| 1470 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 1471 | * ready before returning. |
| 1472 | * This function does not select/unselect the CS line. |
| 1473 | * |
| 1474 | * Returns 0 on success, a negative error code otherwise. |
| 1475 | */ |
| 1476 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 1477 | void *data) |
| 1478 | { |
| 1479 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1480 | u8 *params = data; |
| 1481 | int i; |
| 1482 | |
| 1483 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1); |
| 1484 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 1485 | params[i] = chip->read_byte(mtd); |
| 1486 | |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * nand_reset_op - Do a reset operation |
| 1492 | * @chip: The NAND chip |
| 1493 | * |
| 1494 | * This function sends a RESET command and waits for the NAND to be ready |
| 1495 | * before returning. |
| 1496 | * This function does not select/unselect the CS line. |
| 1497 | * |
| 1498 | * Returns 0 on success, a negative error code otherwise. |
| 1499 | */ |
| 1500 | int nand_reset_op(struct nand_chip *chip) |
| 1501 | { |
| 1502 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1503 | |
| 1504 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 1505 | |
| 1506 | return 0; |
| 1507 | } |
| 1508 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 1509 | |
| 1510 | /** |
| 1511 | * nand_read_data_op - Read data from the NAND |
| 1512 | * @chip: The NAND chip |
| 1513 | * @buf: buffer used to store the data |
| 1514 | * @len: length of the buffer |
| 1515 | * @force_8bit: force 8-bit bus access |
| 1516 | * |
| 1517 | * This function does a raw data read on the bus. Usually used after launching |
| 1518 | * another NAND operation like nand_read_page_op(). |
| 1519 | * This function does not select/unselect the CS line. |
| 1520 | * |
| 1521 | * Returns 0 on success, a negative error code otherwise. |
| 1522 | */ |
| 1523 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 1524 | bool force_8bit) |
| 1525 | { |
| 1526 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1527 | |
| 1528 | if (!len || !buf) |
| 1529 | return -EINVAL; |
| 1530 | |
| 1531 | if (force_8bit) { |
| 1532 | u8 *p = buf; |
| 1533 | unsigned int i; |
| 1534 | |
| 1535 | for (i = 0; i < len; i++) |
| 1536 | p[i] = chip->read_byte(mtd); |
| 1537 | } else { |
| 1538 | chip->read_buf(mtd, buf, len); |
| 1539 | } |
| 1540 | |
| 1541 | return 0; |
| 1542 | } |
| 1543 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 1544 | |
| 1545 | /** |
| 1546 | * nand_write_data_op - Write data from the NAND |
| 1547 | * @chip: The NAND chip |
| 1548 | * @buf: buffer containing the data to send on the bus |
| 1549 | * @len: length of the buffer |
| 1550 | * @force_8bit: force 8-bit bus access |
| 1551 | * |
| 1552 | * This function does a raw data write on the bus. Usually used after launching |
| 1553 | * another NAND operation like nand_write_page_begin_op(). |
| 1554 | * This function does not select/unselect the CS line. |
| 1555 | * |
| 1556 | * Returns 0 on success, a negative error code otherwise. |
| 1557 | */ |
| 1558 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 1559 | unsigned int len, bool force_8bit) |
| 1560 | { |
| 1561 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1562 | |
| 1563 | if (!len || !buf) |
| 1564 | return -EINVAL; |
| 1565 | |
| 1566 | if (force_8bit) { |
| 1567 | const u8 *p = buf; |
| 1568 | unsigned int i; |
| 1569 | |
| 1570 | for (i = 0; i < len; i++) |
| 1571 | chip->write_byte(mtd, p[i]); |
| 1572 | } else { |
| 1573 | chip->write_buf(mtd, buf, len); |
| 1574 | } |
| 1575 | |
| 1576 | return 0; |
| 1577 | } |
| 1578 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 1579 | |
| 1580 | /** |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1581 | * nand_reset - Reset and initialize a NAND device |
| 1582 | * @chip: The NAND chip |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1583 | * @chipnr: Internal die id |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1584 | * |
| 1585 | * Returns 0 for success or negative error code otherwise |
| 1586 | */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1587 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1588 | { |
| 1589 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1590 | int ret; |
| 1591 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1592 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1593 | if (ret) |
| 1594 | return ret; |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1595 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1596 | /* |
| 1597 | * The CS line has to be released before we can apply the new NAND |
| 1598 | * interface settings, hence this weird ->select_chip() dance. |
| 1599 | */ |
| 1600 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1601 | ret = nand_reset_op(chip); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1602 | chip->select_chip(mtd, -1); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1603 | if (ret) |
| 1604 | return ret; |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1605 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1606 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1607 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1608 | chip->select_chip(mtd, -1); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1609 | if (ret) |
| 1610 | return ret; |
| 1611 | |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1612 | return 0; |
| 1613 | } |
| 1614 | |
| 1615 | /** |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1616 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 1617 | * @buf: buffer to test |
| 1618 | * @len: buffer length |
| 1619 | * @bitflips_threshold: maximum number of bitflips |
| 1620 | * |
| 1621 | * Check if a buffer contains only 0xff, which means the underlying region |
| 1622 | * has been erased and is ready to be programmed. |
| 1623 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1624 | * considering the region is not erased. |
| 1625 | * Note: The logic of this function has been extracted from the memweight |
| 1626 | * implementation, except that nand_check_erased_buf function exit before |
| 1627 | * testing the whole buffer if the number of bitflips exceed the |
| 1628 | * bitflips_threshold value. |
| 1629 | * |
| 1630 | * Returns a positive number of bitflips less than or equal to |
| 1631 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1632 | * threshold. |
| 1633 | */ |
| 1634 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 1635 | { |
| 1636 | const unsigned char *bitmap = buf; |
| 1637 | int bitflips = 0; |
| 1638 | int weight; |
| 1639 | |
| 1640 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 1641 | len--, bitmap++) { |
| 1642 | weight = hweight8(*bitmap); |
| 1643 | bitflips += BITS_PER_BYTE - weight; |
| 1644 | if (unlikely(bitflips > bitflips_threshold)) |
| 1645 | return -EBADMSG; |
| 1646 | } |
| 1647 | |
| 1648 | for (; len >= 4; len -= 4, bitmap += 4) { |
| 1649 | weight = hweight32(*((u32 *)bitmap)); |
| 1650 | bitflips += 32 - weight; |
| 1651 | if (unlikely(bitflips > bitflips_threshold)) |
| 1652 | return -EBADMSG; |
| 1653 | } |
| 1654 | |
| 1655 | for (; len > 0; len--, bitmap++) { |
| 1656 | weight = hweight8(*bitmap); |
| 1657 | bitflips += BITS_PER_BYTE - weight; |
| 1658 | if (unlikely(bitflips > bitflips_threshold)) |
| 1659 | return -EBADMSG; |
| 1660 | } |
| 1661 | |
| 1662 | return bitflips; |
| 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 1667 | * 0xff data |
| 1668 | * @data: data buffer to test |
| 1669 | * @datalen: data length |
| 1670 | * @ecc: ECC buffer |
| 1671 | * @ecclen: ECC length |
| 1672 | * @extraoob: extra OOB buffer |
| 1673 | * @extraooblen: extra OOB length |
| 1674 | * @bitflips_threshold: maximum number of bitflips |
| 1675 | * |
| 1676 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 1677 | * 0xff pattern, which means the underlying region has been erased and is |
| 1678 | * ready to be programmed. |
| 1679 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1680 | * considering the region as not erased. |
| 1681 | * |
| 1682 | * Note: |
| 1683 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 1684 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 1685 | * report the number of errors per chunk, and the NAND core infrastructure |
| 1686 | * expect you to return the maximum number of bitflips for the whole page. |
| 1687 | * This is why you should always use this function on a single chunk and |
| 1688 | * not on the whole page. After checking each chunk you should update your |
| 1689 | * max_bitflips value accordingly. |
| 1690 | * 2/ When checking for bitflips in erased pages you should not only check |
| 1691 | * the payload data but also their associated ECC data, because a user might |
| 1692 | * have programmed almost all bits to 1 but a few. In this case, we |
| 1693 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 1694 | * this case. |
| 1695 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 1696 | * data are protected by the ECC engine. |
| 1697 | * It could also be used if you support subpages and want to attach some |
| 1698 | * extra OOB data to an ECC chunk. |
| 1699 | * |
| 1700 | * Returns a positive number of bitflips less than or equal to |
| 1701 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1702 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 1703 | */ |
| 1704 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 1705 | void *ecc, int ecclen, |
| 1706 | void *extraoob, int extraooblen, |
| 1707 | int bitflips_threshold) |
| 1708 | { |
| 1709 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 1710 | |
| 1711 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 1712 | bitflips_threshold); |
| 1713 | if (data_bitflips < 0) |
| 1714 | return data_bitflips; |
| 1715 | |
| 1716 | bitflips_threshold -= data_bitflips; |
| 1717 | |
| 1718 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 1719 | if (ecc_bitflips < 0) |
| 1720 | return ecc_bitflips; |
| 1721 | |
| 1722 | bitflips_threshold -= ecc_bitflips; |
| 1723 | |
| 1724 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 1725 | bitflips_threshold); |
| 1726 | if (extraoob_bitflips < 0) |
| 1727 | return extraoob_bitflips; |
| 1728 | |
| 1729 | if (data_bitflips) |
| 1730 | memset(data, 0xff, datalen); |
| 1731 | |
| 1732 | if (ecc_bitflips) |
| 1733 | memset(ecc, 0xff, ecclen); |
| 1734 | |
| 1735 | if (extraoob_bitflips) |
| 1736 | memset(extraoob, 0xff, extraooblen); |
| 1737 | |
| 1738 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 1739 | } |
| 1740 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1741 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1742 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1743 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
| 1744 | * @mtd: mtd info structure |
| 1745 | * @chip: nand chip info structure |
| 1746 | * @buf: buffer to store read data |
| 1747 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1748 | * @page: page number to read |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1749 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1750 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1751 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1752 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1753 | uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1754 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1755 | int ret; |
| 1756 | |
| 1757 | ret = nand_read_data_op(chip, buf, mtd->writesize, false); |
| 1758 | if (ret) |
| 1759 | return ret; |
| 1760 | |
| 1761 | if (oob_required) { |
| 1762 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 1763 | false); |
| 1764 | if (ret) |
| 1765 | return ret; |
| 1766 | } |
| 1767 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1768 | return 0; |
| 1769 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1770 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1771 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1772 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
| 1773 | * @mtd: mtd info structure |
| 1774 | * @chip: nand chip info structure |
| 1775 | * @buf: buffer to store read data |
| 1776 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1777 | * @page: page number to read |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1778 | * |
| 1779 | * We need a special oob layout and handling even when OOB isn't used. |
| 1780 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1781 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1782 | struct nand_chip *chip, uint8_t *buf, |
| 1783 | int oob_required, int page) |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1784 | { |
| 1785 | int eccsize = chip->ecc.size; |
| 1786 | int eccbytes = chip->ecc.bytes; |
| 1787 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1788 | int steps, size, ret; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1789 | |
| 1790 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1791 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 1792 | if (ret) |
| 1793 | return ret; |
| 1794 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1795 | buf += eccsize; |
| 1796 | |
| 1797 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1798 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 1799 | false); |
| 1800 | if (ret) |
| 1801 | return ret; |
| 1802 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1803 | oob += chip->ecc.prepad; |
| 1804 | } |
| 1805 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1806 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 1807 | if (ret) |
| 1808 | return ret; |
| 1809 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1810 | oob += eccbytes; |
| 1811 | |
| 1812 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1813 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 1814 | false); |
| 1815 | if (ret) |
| 1816 | return ret; |
| 1817 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1818 | oob += chip->ecc.postpad; |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1823 | if (size) { |
| 1824 | ret = nand_read_data_op(chip, oob, size, false); |
| 1825 | if (ret) |
| 1826 | return ret; |
| 1827 | } |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1828 | |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1833 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
| 1834 | * @mtd: mtd info structure |
| 1835 | * @chip: nand chip info structure |
| 1836 | * @buf: buffer to store read data |
| 1837 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1838 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1839 | */ |
| 1840 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1841 | uint8_t *buf, int oob_required, int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1842 | { |
| 1843 | int i, eccsize = chip->ecc.size; |
| 1844 | int eccbytes = chip->ecc.bytes; |
| 1845 | int eccsteps = chip->ecc.steps; |
| 1846 | uint8_t *p = buf; |
| 1847 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1848 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 1849 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1850 | unsigned int max_bitflips = 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1851 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1852 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1853 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1854 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1855 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1856 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1857 | for (i = 0; i < chip->ecc.total; i++) |
| 1858 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1859 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1860 | eccsteps = chip->ecc.steps; |
| 1861 | p = buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1862 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1863 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1864 | int stat; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1865 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1866 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1867 | if (stat < 0) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1868 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1869 | } else { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1870 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1871 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1872 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1873 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1874 | return max_bitflips; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1875 | } |
| 1876 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1877 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1878 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1879 | * @mtd: mtd info structure |
| 1880 | * @chip: nand chip info structure |
| 1881 | * @data_offs: offset of requested data within the page |
| 1882 | * @readlen: data length |
| 1883 | * @bufpoi: buffer to store read data |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1884 | * @page: page number to read |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1885 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1886 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1887 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 1888 | int page) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1889 | { |
| 1890 | int start_step, end_step, num_steps; |
| 1891 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1892 | uint8_t *p; |
| 1893 | int data_col_addr, i, gaps = 0; |
| 1894 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 1895 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1896 | int index; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1897 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1898 | int ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1899 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1900 | /* Column address within the page aligned to ECC size (256bytes) */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1901 | start_step = data_offs / chip->ecc.size; |
| 1902 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 1903 | num_steps = end_step - start_step + 1; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1904 | index = start_step * chip->ecc.bytes; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1905 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1906 | /* Data size aligned to ECC ecc.size */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1907 | datafrag_len = num_steps * chip->ecc.size; |
| 1908 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 1909 | |
| 1910 | data_col_addr = start_step * chip->ecc.size; |
| 1911 | /* If we read not a page aligned data */ |
| 1912 | if (data_col_addr != 0) |
| 1913 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 1914 | |
| 1915 | p = bufpoi + data_col_addr; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1916 | ret = nand_read_data_op(chip, p, datafrag_len, false); |
| 1917 | if (ret) |
| 1918 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1919 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1920 | /* Calculate ECC */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1921 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 1922 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 1923 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1924 | /* |
| 1925 | * The performance is faster if we position offsets according to |
| 1926 | * ecc.pos. Let's make sure that there are no gaps in ECC positions. |
| 1927 | */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1928 | for (i = 0; i < eccfrag_len - 1; i++) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1929 | if (eccpos[i + index] + 1 != eccpos[i + index + 1]) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1930 | gaps = 1; |
| 1931 | break; |
| 1932 | } |
| 1933 | } |
| 1934 | if (gaps) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1935 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 1936 | chip->oob_poi, mtd->oobsize, |
| 1937 | false); |
| 1938 | if (ret) |
| 1939 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1940 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1941 | /* |
| 1942 | * Send the command to read the particular ECC bytes take care |
| 1943 | * about buswidth alignment in read_buf. |
| 1944 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1945 | aligned_pos = eccpos[index] & ~(busw - 1); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1946 | aligned_len = eccfrag_len; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1947 | if (eccpos[index] & (busw - 1)) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1948 | aligned_len++; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1949 | if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1)) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1950 | aligned_len++; |
| 1951 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1952 | ret = nand_change_read_column_op(chip, |
| 1953 | mtd->writesize + aligned_pos, |
| 1954 | &chip->oob_poi[aligned_pos], |
| 1955 | aligned_len, false); |
| 1956 | if (ret) |
| 1957 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | for (i = 0; i < eccfrag_len; i++) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1961 | chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]]; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1962 | |
| 1963 | p = bufpoi + data_col_addr; |
| 1964 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1965 | int stat; |
| 1966 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1967 | stat = chip->ecc.correct(mtd, p, |
| 1968 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1969 | if (stat == -EBADMSG && |
| 1970 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1971 | /* check for empty pages with bitflips */ |
| 1972 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1973 | &chip->buffers->ecccode[i], |
| 1974 | chip->ecc.bytes, |
| 1975 | NULL, 0, |
| 1976 | chip->ecc.strength); |
| 1977 | } |
| 1978 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1979 | if (stat < 0) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1980 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1981 | } else { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1982 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1983 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1984 | } |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1985 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1986 | return max_bitflips; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1990 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
| 1991 | * @mtd: mtd info structure |
| 1992 | * @chip: nand chip info structure |
| 1993 | * @buf: buffer to store read data |
| 1994 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1995 | * @page: page number to read |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1996 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1997 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1998 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1999 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2000 | uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2001 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2002 | int i, eccsize = chip->ecc.size; |
| 2003 | int eccbytes = chip->ecc.bytes; |
| 2004 | int eccsteps = chip->ecc.steps; |
| 2005 | uint8_t *p = buf; |
| 2006 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2007 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 2008 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2009 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2010 | int ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2011 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2012 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2013 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2014 | |
| 2015 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2016 | if (ret) |
| 2017 | return ret; |
| 2018 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2019 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2020 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2021 | |
| 2022 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2023 | if (ret) |
| 2024 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2025 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2026 | for (i = 0; i < chip->ecc.total; i++) |
| 2027 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2028 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2029 | eccsteps = chip->ecc.steps; |
| 2030 | p = buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2031 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2032 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2033 | int stat; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2034 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2035 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2036 | if (stat == -EBADMSG && |
| 2037 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2038 | /* check for empty pages with bitflips */ |
| 2039 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2040 | &ecc_code[i], eccbytes, |
| 2041 | NULL, 0, |
| 2042 | chip->ecc.strength); |
| 2043 | } |
| 2044 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2045 | if (stat < 0) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2046 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2047 | } else { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2048 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2049 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2050 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2051 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2052 | return max_bitflips; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2053 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2054 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2055 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2056 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
| 2057 | * @mtd: mtd info structure |
| 2058 | * @chip: nand chip info structure |
| 2059 | * @buf: buffer to store read data |
| 2060 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2061 | * @page: page number to read |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2062 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2063 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 2064 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 2065 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 2066 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 2067 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2068 | */ |
| 2069 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2070 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2071 | { |
| 2072 | int i, eccsize = chip->ecc.size; |
| 2073 | int eccbytes = chip->ecc.bytes; |
| 2074 | int eccsteps = chip->ecc.steps; |
| 2075 | uint8_t *p = buf; |
| 2076 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 2077 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2078 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2079 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2080 | int ret; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2081 | |
| 2082 | /* Read the OOB area first */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2083 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 2084 | if (ret) |
| 2085 | return ret; |
| 2086 | |
| 2087 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2088 | if (ret) |
| 2089 | return ret; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2090 | |
| 2091 | for (i = 0; i < chip->ecc.total; i++) |
| 2092 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 2093 | |
| 2094 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2095 | int stat; |
| 2096 | |
| 2097 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2098 | |
| 2099 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2100 | if (ret) |
| 2101 | return ret; |
| 2102 | |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2103 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2104 | |
| 2105 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2106 | if (stat == -EBADMSG && |
| 2107 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2108 | /* check for empty pages with bitflips */ |
| 2109 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2110 | &ecc_code[i], eccbytes, |
| 2111 | NULL, 0, |
| 2112 | chip->ecc.strength); |
| 2113 | } |
| 2114 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2115 | if (stat < 0) { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2116 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2117 | } else { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2118 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2119 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2120 | } |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2121 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2122 | return max_bitflips; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2126 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
| 2127 | * @mtd: mtd info structure |
| 2128 | * @chip: nand chip info structure |
| 2129 | * @buf: buffer to store read data |
| 2130 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2131 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2132 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2133 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2134 | * need a special oob layout and handling. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2135 | */ |
| 2136 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2137 | uint8_t *buf, int oob_required, int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2138 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2139 | int ret, i, eccsize = chip->ecc.size; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2140 | int eccbytes = chip->ecc.bytes; |
| 2141 | int eccsteps = chip->ecc.steps; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2142 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2143 | uint8_t *p = buf; |
| 2144 | uint8_t *oob = chip->oob_poi; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2145 | unsigned int max_bitflips = 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2146 | |
| 2147 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2148 | int stat; |
| 2149 | |
| 2150 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2151 | |
| 2152 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2153 | if (ret) |
| 2154 | return ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2155 | |
| 2156 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2157 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2158 | false); |
| 2159 | if (ret) |
| 2160 | return ret; |
| 2161 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2162 | oob += chip->ecc.prepad; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2163 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2164 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2165 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2166 | |
| 2167 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2168 | if (ret) |
| 2169 | return ret; |
| 2170 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2171 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2172 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2173 | oob += eccbytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2174 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2175 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2176 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2177 | false); |
| 2178 | if (ret) |
| 2179 | return ret; |
| 2180 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2181 | oob += chip->ecc.postpad; |
| 2182 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2183 | |
| 2184 | if (stat == -EBADMSG && |
| 2185 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2186 | /* check for empty pages with bitflips */ |
| 2187 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 2188 | oob - eccpadbytes, |
| 2189 | eccpadbytes, |
| 2190 | NULL, 0, |
| 2191 | chip->ecc.strength); |
| 2192 | } |
| 2193 | |
| 2194 | if (stat < 0) { |
| 2195 | mtd->ecc_stats.failed++; |
| 2196 | } else { |
| 2197 | mtd->ecc_stats.corrected += stat; |
| 2198 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2199 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2200 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2201 | |
| 2202 | /* Calculate remaining oob bytes */ |
| 2203 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2204 | if (i) { |
| 2205 | ret = nand_read_data_op(chip, oob, i, false); |
| 2206 | if (ret) |
| 2207 | return ret; |
| 2208 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2209 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2210 | return max_bitflips; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2211 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2212 | |
| 2213 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2214 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
| 2215 | * @chip: nand chip structure |
| 2216 | * @oob: oob destination address |
| 2217 | * @ops: oob ops structure |
| 2218 | * @len: size of oob to transfer |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2219 | */ |
| 2220 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, |
| 2221 | struct mtd_oob_ops *ops, size_t len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2222 | { |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2223 | switch (ops->mode) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2224 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2225 | case MTD_OPS_PLACE_OOB: |
| 2226 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2227 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 2228 | return oob + len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2229 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2230 | case MTD_OPS_AUTO_OOB: { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2231 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 2232 | uint32_t boffs = 0, roffs = ops->ooboffs; |
| 2233 | size_t bytes = 0; |
| 2234 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2235 | for (; free->length && len; free++, len -= bytes) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2236 | /* Read request not from offset 0? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2237 | if (unlikely(roffs)) { |
| 2238 | if (roffs >= free->length) { |
| 2239 | roffs -= free->length; |
| 2240 | continue; |
| 2241 | } |
| 2242 | boffs = free->offset + roffs; |
| 2243 | bytes = min_t(size_t, len, |
| 2244 | (free->length - roffs)); |
| 2245 | roffs = 0; |
| 2246 | } else { |
| 2247 | bytes = min_t(size_t, len, free->length); |
| 2248 | boffs = free->offset; |
| 2249 | } |
| 2250 | memcpy(oob, chip->oob_poi + boffs, bytes); |
| 2251 | oob += bytes; |
| 2252 | } |
| 2253 | return oob; |
| 2254 | } |
| 2255 | default: |
| 2256 | BUG(); |
| 2257 | } |
| 2258 | return NULL; |
| 2259 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2260 | |
| 2261 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2262 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 2263 | * @mtd: MTD device structure |
| 2264 | * @retry_mode: the retry mode to use |
| 2265 | * |
| 2266 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 2267 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 2268 | * a new threshold, the host should retry reading the page. |
| 2269 | */ |
| 2270 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 2271 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2272 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2273 | |
| 2274 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 2275 | |
| 2276 | if (retry_mode >= chip->read_retries) |
| 2277 | return -EINVAL; |
| 2278 | |
| 2279 | if (!chip->setup_read_retry) |
| 2280 | return -EOPNOTSUPP; |
| 2281 | |
| 2282 | return chip->setup_read_retry(mtd, retry_mode); |
| 2283 | } |
| 2284 | |
| 2285 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2286 | * nand_do_read_ops - [INTERN] Read data with ECC |
| 2287 | * @mtd: MTD device structure |
| 2288 | * @from: offset to read from |
| 2289 | * @ops: oob ops structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2290 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2291 | * Internal function. Called with chip held. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2292 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2293 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 2294 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2295 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2296 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2297 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2298 | int ret = 0; |
| 2299 | uint32_t readlen = ops->len; |
| 2300 | uint32_t oobreadlen = ops->ooblen; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2301 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2302 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2303 | uint8_t *bufpoi, *oob, *buf; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2304 | int use_bufpoi; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2305 | unsigned int max_bitflips = 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2306 | int retry_mode = 0; |
| 2307 | bool ecc_fail = false; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2308 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2309 | chipnr = (int)(from >> chip->chip_shift); |
| 2310 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2311 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2312 | realpage = (int)(from >> chip->page_shift); |
| 2313 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2314 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2315 | col = (int)(from & (mtd->writesize - 1)); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2316 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2317 | buf = ops->datbuf; |
| 2318 | oob = ops->oobbuf; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2319 | oob_required = oob ? 1 : 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2320 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2321 | while (1) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2322 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
Scott Wood | ea95b64 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 2323 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 2324 | schedule(); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2325 | bytes = min(mtd->writesize - col, readlen); |
| 2326 | aligned = (bytes == mtd->writesize); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2327 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2328 | if (!aligned) |
| 2329 | use_bufpoi = 1; |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 2330 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
| 2331 | use_bufpoi = !IS_ALIGNED((unsigned long)buf, |
| 2332 | chip->buf_align); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2333 | else |
| 2334 | use_bufpoi = 0; |
| 2335 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2336 | /* Is the current page in the buffer? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2337 | if (realpage != chip->pagebuf || oob) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2338 | bufpoi = use_bufpoi ? chip->buffers->databuf : buf; |
| 2339 | |
| 2340 | if (use_bufpoi && aligned) |
| 2341 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 2342 | __func__, buf); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2343 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2344 | read_retry: |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2345 | if (nand_standard_page_accessors(&chip->ecc)) { |
| 2346 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2347 | if (ret) |
| 2348 | break; |
| 2349 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2350 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2351 | /* |
| 2352 | * Now read the page into the buffer. Absent an error, |
| 2353 | * the read methods return max bitflips per ecc step. |
| 2354 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2355 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
| 2356 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
| 2357 | oob_required, |
| 2358 | page); |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 2359 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2360 | !oob) |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2361 | ret = chip->ecc.read_subpage(mtd, chip, |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 2362 | col, bytes, bufpoi, |
| 2363 | page); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2364 | else |
Sandeep Paulraj | 883189e | 2009-08-10 13:27:46 -0400 | [diff] [blame] | 2365 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2366 | oob_required, page); |
| 2367 | if (ret < 0) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2368 | if (use_bufpoi) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2369 | /* Invalidate page cache */ |
| 2370 | chip->pagebuf = -1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2371 | break; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2372 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2373 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2374 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 2375 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2376 | /* Transfer not aligned data */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2377 | if (use_bufpoi) { |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 2378 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2379 | !(mtd->ecc_stats.failed - ecc_failures) && |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2380 | (ops->mode != MTD_OPS_RAW)) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2381 | chip->pagebuf = realpage; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2382 | chip->pagebuf_bitflips = ret; |
| 2383 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2384 | /* Invalidate page cache */ |
| 2385 | chip->pagebuf = -1; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2386 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2387 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 2388 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2389 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2390 | if (unlikely(oob)) { |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2391 | int toread = min(oobreadlen, max_oobsize); |
| 2392 | |
| 2393 | if (toread) { |
| 2394 | oob = nand_transfer_oob(chip, |
| 2395 | oob, ops, toread); |
| 2396 | oobreadlen -= toread; |
| 2397 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2398 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2399 | |
| 2400 | if (chip->options & NAND_NEED_READRDY) { |
| 2401 | /* Apply delay or wait for ready/busy pin */ |
| 2402 | if (!chip->dev_ready) |
| 2403 | udelay(chip->chip_delay); |
| 2404 | else |
| 2405 | nand_wait_ready(mtd); |
| 2406 | } |
| 2407 | |
| 2408 | if (mtd->ecc_stats.failed - ecc_failures) { |
| 2409 | if (retry_mode + 1 < chip->read_retries) { |
| 2410 | retry_mode++; |
| 2411 | ret = nand_setup_read_retry(mtd, |
| 2412 | retry_mode); |
| 2413 | if (ret < 0) |
| 2414 | break; |
| 2415 | |
| 2416 | /* Reset failures; retry */ |
| 2417 | mtd->ecc_stats.failed = ecc_failures; |
| 2418 | goto read_retry; |
| 2419 | } else { |
| 2420 | /* No more retry modes; real failure */ |
| 2421 | ecc_fail = true; |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | buf += bytes; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2426 | } else { |
| 2427 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 2428 | buf += bytes; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2429 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 2430 | chip->pagebuf_bitflips); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2431 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2432 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2433 | readlen -= bytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2434 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2435 | /* Reset to retry mode 0 */ |
| 2436 | if (retry_mode) { |
| 2437 | ret = nand_setup_read_retry(mtd, 0); |
| 2438 | if (ret < 0) |
| 2439 | break; |
| 2440 | retry_mode = 0; |
| 2441 | } |
| 2442 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2443 | if (!readlen) |
| 2444 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2445 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2446 | /* For subsequent reads align to page boundary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2447 | col = 0; |
| 2448 | /* Increment page address */ |
| 2449 | realpage++; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2450 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2451 | page = realpage & chip->pagemask; |
| 2452 | /* Check, if we cross a chip boundary */ |
| 2453 | if (!page) { |
| 2454 | chipnr++; |
| 2455 | chip->select_chip(mtd, -1); |
| 2456 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2457 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2458 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2459 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2460 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2461 | ops->retlen = ops->len - (size_t) readlen; |
| 2462 | if (oob) |
| 2463 | ops->oobretlen = ops->ooblen - oobreadlen; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2464 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2465 | if (ret < 0) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2466 | return ret; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2467 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2468 | if (ecc_fail) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2469 | return -EBADMSG; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2470 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2471 | return max_bitflips; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2472 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2473 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2474 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2475 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
| 2476 | * @mtd: mtd info structure |
| 2477 | * @chip: nand chip info structure |
| 2478 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2479 | */ |
| 2480 | static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2481 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2482 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2483 | return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2487 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2488 | * with syndromes |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2489 | * @mtd: mtd info structure |
| 2490 | * @chip: nand chip info structure |
| 2491 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2492 | */ |
| 2493 | static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2494 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2495 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2496 | int length = mtd->oobsize; |
| 2497 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2498 | int eccsize = chip->ecc.size; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2499 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2500 | int i, toread, sndrnd = 0, pos, ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2501 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2502 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 2503 | if (ret) |
| 2504 | return ret; |
| 2505 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2506 | for (i = 0; i < chip->ecc.steps; i++) { |
| 2507 | if (sndrnd) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2508 | int ret; |
| 2509 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2510 | pos = eccsize + i * (eccsize + chunk); |
| 2511 | if (mtd->writesize > 512) |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2512 | ret = nand_change_read_column_op(chip, pos, |
| 2513 | NULL, 0, |
| 2514 | false); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2515 | else |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2516 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 2517 | 0); |
| 2518 | |
| 2519 | if (ret) |
| 2520 | return ret; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2521 | } else |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2522 | sndrnd = 1; |
| 2523 | toread = min_t(int, length, chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2524 | |
| 2525 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 2526 | if (ret) |
| 2527 | return ret; |
| 2528 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2529 | bufpoi += toread; |
| 2530 | length -= toread; |
| 2531 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2532 | if (length > 0) { |
| 2533 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 2534 | if (ret) |
| 2535 | return ret; |
| 2536 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2537 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2538 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2539 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2540 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2541 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2542 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
| 2543 | * @mtd: mtd info structure |
| 2544 | * @chip: nand chip info structure |
| 2545 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2546 | */ |
| 2547 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 2548 | int page) |
| 2549 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2550 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 2551 | mtd->oobsize); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2555 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
| 2556 | * with syndrome - only for large page flash |
| 2557 | * @mtd: mtd info structure |
| 2558 | * @chip: nand chip info structure |
| 2559 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2560 | */ |
| 2561 | static int nand_write_oob_syndrome(struct mtd_info *mtd, |
| 2562 | struct nand_chip *chip, int page) |
| 2563 | { |
| 2564 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2565 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2566 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2567 | const uint8_t *bufpoi = chip->oob_poi; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2568 | |
| 2569 | /* |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2570 | * data-ecc-data-ecc ... ecc-oob |
| 2571 | * or |
| 2572 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2573 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2574 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 2575 | pos = steps * (eccsize + chunk); |
| 2576 | steps = 0; |
| 2577 | } else |
| 2578 | pos = eccsize; |
| 2579 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2580 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 2581 | if (ret) |
| 2582 | return ret; |
| 2583 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2584 | for (i = 0; i < steps; i++) { |
| 2585 | if (sndcmd) { |
| 2586 | if (mtd->writesize <= 512) { |
| 2587 | uint32_t fill = 0xFFFFFFFF; |
| 2588 | |
| 2589 | len = eccsize; |
| 2590 | while (len > 0) { |
| 2591 | int num = min_t(int, len, 4); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2592 | |
| 2593 | ret = nand_write_data_op(chip, &fill, |
| 2594 | num, false); |
| 2595 | if (ret) |
| 2596 | return ret; |
| 2597 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2598 | len -= num; |
| 2599 | } |
| 2600 | } else { |
| 2601 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2602 | ret = nand_change_write_column_op(chip, pos, |
| 2603 | NULL, 0, |
| 2604 | false); |
| 2605 | if (ret) |
| 2606 | return ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2607 | } |
| 2608 | } else |
| 2609 | sndcmd = 1; |
| 2610 | len = min_t(int, length, chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2611 | |
| 2612 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 2613 | if (ret) |
| 2614 | return ret; |
| 2615 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2616 | bufpoi += len; |
| 2617 | length -= len; |
| 2618 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2619 | if (length > 0) { |
| 2620 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 2621 | if (ret) |
| 2622 | return ret; |
| 2623 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2624 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2625 | return nand_prog_page_end_op(chip); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2629 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
| 2630 | * @mtd: MTD device structure |
| 2631 | * @from: offset to read from |
| 2632 | * @ops: oob operations description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2633 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2634 | * NAND read out-of-band data from the spare area. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2635 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2636 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 2637 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2638 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2639 | int page, realpage, chipnr; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2640 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2641 | struct mtd_ecc_stats stats; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2642 | int readlen = ops->ooblen; |
| 2643 | int len; |
| 2644 | uint8_t *buf = ops->oobbuf; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2645 | int ret = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2646 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2647 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2648 | __func__, (unsigned long long)from, readlen); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2649 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2650 | stats = mtd->ecc_stats; |
| 2651 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2652 | len = mtd_oobavail(mtd, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2653 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2654 | if (unlikely(ops->ooboffs >= len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2655 | pr_debug("%s: attempt to start read outside oob\n", |
| 2656 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2657 | return -EINVAL; |
| 2658 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2659 | |
| 2660 | /* Do not allow reads past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2661 | if (unlikely(from >= mtd->size || |
| 2662 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 2663 | (from >> chip->page_shift)) * len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2664 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2665 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2666 | return -EINVAL; |
| 2667 | } |
| 2668 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2669 | chipnr = (int)(from >> chip->chip_shift); |
| 2670 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2671 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2672 | /* Shift to get page */ |
| 2673 | realpage = (int)(from >> chip->page_shift); |
| 2674 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2675 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2676 | while (1) { |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 2677 | schedule(); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2678 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2679 | if (ops->mode == MTD_OPS_RAW) |
| 2680 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
| 2681 | else |
| 2682 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 2683 | |
| 2684 | if (ret < 0) |
| 2685 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2686 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2687 | len = min(len, readlen); |
| 2688 | buf = nand_transfer_oob(chip, buf, ops, len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2689 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2690 | if (chip->options & NAND_NEED_READRDY) { |
| 2691 | /* Apply delay or wait for ready/busy pin */ |
| 2692 | if (!chip->dev_ready) |
| 2693 | udelay(chip->chip_delay); |
| 2694 | else |
| 2695 | nand_wait_ready(mtd); |
| 2696 | } |
| 2697 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2698 | readlen -= len; |
| 2699 | if (!readlen) |
| 2700 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2701 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2702 | /* Increment page address */ |
| 2703 | realpage++; |
| 2704 | |
| 2705 | page = realpage & chip->pagemask; |
| 2706 | /* Check, if we cross a chip boundary */ |
| 2707 | if (!page) { |
| 2708 | chipnr++; |
| 2709 | chip->select_chip(mtd, -1); |
| 2710 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2711 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2712 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2713 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2714 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2715 | ops->oobretlen = ops->ooblen - readlen; |
| 2716 | |
| 2717 | if (ret < 0) |
| 2718 | return ret; |
| 2719 | |
| 2720 | if (mtd->ecc_stats.failed - stats.failed) |
| 2721 | return -EBADMSG; |
| 2722 | |
| 2723 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2727 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2728 | * @mtd: MTD device structure |
| 2729 | * @from: offset to read from |
| 2730 | * @ops: oob operation description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2731 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2732 | * NAND read data and/or out-of-band data. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2733 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2734 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 2735 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2736 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2737 | int ret = -ENOTSUPP; |
| 2738 | |
| 2739 | ops->retlen = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2740 | |
| 2741 | /* Do not allow reads past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2742 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2743 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2744 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2745 | return -EINVAL; |
| 2746 | } |
| 2747 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2748 | nand_get_device(mtd, FL_READING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2749 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2750 | switch (ops->mode) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2751 | case MTD_OPS_PLACE_OOB: |
| 2752 | case MTD_OPS_AUTO_OOB: |
| 2753 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2754 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2755 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2756 | default: |
| 2757 | goto out; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2758 | } |
| 2759 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2760 | if (!ops->datbuf) |
| 2761 | ret = nand_do_read_oob(mtd, from, ops); |
| 2762 | else |
| 2763 | ret = nand_do_read_ops(mtd, from, ops); |
| 2764 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2765 | out: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2766 | nand_release_device(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2767 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2771 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2772 | * nand_write_page_raw - [INTERN] raw page write function |
| 2773 | * @mtd: mtd info structure |
| 2774 | * @chip: nand chip info structure |
| 2775 | * @buf: data buffer |
| 2776 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2777 | * @page: page number to write |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2778 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2779 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2780 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2781 | static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2782 | const uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2783 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2784 | int ret; |
| 2785 | |
| 2786 | ret = nand_write_data_op(chip, buf, mtd->writesize, false); |
| 2787 | if (ret) |
| 2788 | return ret; |
| 2789 | |
| 2790 | if (oob_required) { |
| 2791 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 2792 | false); |
| 2793 | if (ret) |
| 2794 | return ret; |
| 2795 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2796 | |
| 2797 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2798 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2799 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2800 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2801 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
| 2802 | * @mtd: mtd info structure |
| 2803 | * @chip: nand chip info structure |
| 2804 | * @buf: data buffer |
| 2805 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2806 | * @page: page number to write |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2807 | * |
| 2808 | * We need a special oob layout and handling even when ECC isn't checked. |
| 2809 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2810 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2811 | struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2812 | const uint8_t *buf, int oob_required, |
| 2813 | int page) |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2814 | { |
| 2815 | int eccsize = chip->ecc.size; |
| 2816 | int eccbytes = chip->ecc.bytes; |
| 2817 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2818 | int steps, size, ret; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2819 | |
| 2820 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2821 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 2822 | if (ret) |
| 2823 | return ret; |
| 2824 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2825 | buf += eccsize; |
| 2826 | |
| 2827 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2828 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 2829 | false); |
| 2830 | if (ret) |
| 2831 | return ret; |
| 2832 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2833 | oob += chip->ecc.prepad; |
| 2834 | } |
| 2835 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2836 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 2837 | if (ret) |
| 2838 | return ret; |
| 2839 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2840 | oob += eccbytes; |
| 2841 | |
| 2842 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2843 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 2844 | false); |
| 2845 | if (ret) |
| 2846 | return ret; |
| 2847 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2848 | oob += chip->ecc.postpad; |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2853 | if (size) { |
| 2854 | ret = nand_write_data_op(chip, oob, size, false); |
| 2855 | if (ret) |
| 2856 | return ret; |
| 2857 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2858 | |
| 2859 | return 0; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2860 | } |
| 2861 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2862 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
| 2863 | * @mtd: mtd info structure |
| 2864 | * @chip: nand chip info structure |
| 2865 | * @buf: data buffer |
| 2866 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2867 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2868 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2869 | static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2870 | const uint8_t *buf, int oob_required, |
| 2871 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2872 | { |
| 2873 | int i, eccsize = chip->ecc.size; |
| 2874 | int eccbytes = chip->ecc.bytes; |
| 2875 | int eccsteps = chip->ecc.steps; |
| 2876 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2877 | const uint8_t *p = buf; |
| 2878 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2879 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2880 | /* Software ECC calculation */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2881 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 2882 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2883 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2884 | for (i = 0; i < chip->ecc.total; i++) |
| 2885 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2886 | |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2887 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2888 | } |
| 2889 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2890 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2891 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
| 2892 | * @mtd: mtd info structure |
| 2893 | * @chip: nand chip info structure |
| 2894 | * @buf: data buffer |
| 2895 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2896 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2897 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2898 | static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2899 | const uint8_t *buf, int oob_required, |
| 2900 | int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2901 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2902 | int i, eccsize = chip->ecc.size; |
| 2903 | int eccbytes = chip->ecc.bytes; |
| 2904 | int eccsteps = chip->ecc.steps; |
| 2905 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2906 | const uint8_t *p = buf; |
| 2907 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2908 | int ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2909 | |
| 2910 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2911 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2912 | |
| 2913 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 2914 | if (ret) |
| 2915 | return ret; |
| 2916 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2917 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2918 | } |
| 2919 | |
| 2920 | for (i = 0; i < chip->ecc.total; i++) |
| 2921 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 2922 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2923 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2924 | if (ret) |
| 2925 | return ret; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2926 | |
| 2927 | return 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2928 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2929 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2930 | |
| 2931 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2932 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2933 | * @mtd: mtd info structure |
| 2934 | * @chip: nand chip info structure |
| 2935 | * @offset: column address of subpage within the page |
| 2936 | * @data_len: data length |
| 2937 | * @buf: data buffer |
| 2938 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2939 | * @page: page number to write |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2940 | */ |
| 2941 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 2942 | struct nand_chip *chip, uint32_t offset, |
| 2943 | uint32_t data_len, const uint8_t *buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2944 | int oob_required, int page) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2945 | { |
| 2946 | uint8_t *oob_buf = chip->oob_poi; |
| 2947 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2948 | int ecc_size = chip->ecc.size; |
| 2949 | int ecc_bytes = chip->ecc.bytes; |
| 2950 | int ecc_steps = chip->ecc.steps; |
| 2951 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2952 | uint32_t start_step = offset / ecc_size; |
| 2953 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 2954 | int oob_bytes = mtd->oobsize / ecc_steps; |
| 2955 | int step, i; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2956 | int ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2957 | |
| 2958 | for (step = 0; step < ecc_steps; step++) { |
| 2959 | /* configure controller for WRITE access */ |
| 2960 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2961 | |
| 2962 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2963 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 2964 | if (ret) |
| 2965 | return ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2966 | |
| 2967 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 2968 | if ((step < start_step) || (step > end_step)) |
| 2969 | memset(ecc_calc, 0xff, ecc_bytes); |
| 2970 | else |
| 2971 | chip->ecc.calculate(mtd, buf, ecc_calc); |
| 2972 | |
| 2973 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 2974 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 2975 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 2976 | memset(oob_buf, 0xff, oob_bytes); |
| 2977 | |
| 2978 | buf += ecc_size; |
| 2979 | ecc_calc += ecc_bytes; |
| 2980 | oob_buf += oob_bytes; |
| 2981 | } |
| 2982 | |
| 2983 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 2984 | /* this include masked-value(0xFF) for unwritten subpages */ |
| 2985 | ecc_calc = chip->buffers->ecccalc; |
| 2986 | for (i = 0; i < chip->ecc.total; i++) |
| 2987 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 2988 | |
| 2989 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2990 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2991 | if (ret) |
| 2992 | return ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2993 | |
| 2994 | return 0; |
| 2995 | } |
| 2996 | |
| 2997 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2998 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2999 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
| 3000 | * @mtd: mtd info structure |
| 3001 | * @chip: nand chip info structure |
| 3002 | * @buf: data buffer |
| 3003 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3004 | * @page: page number to write |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3005 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3006 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3007 | * need a special oob layout and handling. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3008 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3009 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
| 3010 | struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3011 | const uint8_t *buf, int oob_required, |
| 3012 | int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3013 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3014 | int i, eccsize = chip->ecc.size; |
| 3015 | int eccbytes = chip->ecc.bytes; |
| 3016 | int eccsteps = chip->ecc.steps; |
| 3017 | const uint8_t *p = buf; |
| 3018 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3019 | int ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3020 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3021 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3022 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3023 | |
| 3024 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3025 | if (ret) |
| 3026 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3027 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3028 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3029 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3030 | false); |
| 3031 | if (ret) |
| 3032 | return ret; |
| 3033 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3034 | oob += chip->ecc.prepad; |
| 3035 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3036 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3037 | chip->ecc.calculate(mtd, p, oob); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3038 | |
| 3039 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3040 | if (ret) |
| 3041 | return ret; |
| 3042 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3043 | oob += eccbytes; |
| 3044 | |
| 3045 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3046 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3047 | false); |
| 3048 | if (ret) |
| 3049 | return ret; |
| 3050 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3051 | oob += chip->ecc.postpad; |
| 3052 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3053 | } |
| 3054 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3055 | /* Calculate remaining oob bytes */ |
| 3056 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3057 | if (i) { |
| 3058 | ret = nand_write_data_op(chip, oob, i, false); |
| 3059 | if (ret) |
| 3060 | return ret; |
| 3061 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3062 | |
| 3063 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3064 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3065 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3066 | /** |
| 3067 | * nand_write_page - [REPLACEABLE] write one page |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3068 | * @mtd: MTD device structure |
| 3069 | * @chip: NAND chip descriptor |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3070 | * @offset: address offset within the page |
| 3071 | * @data_len: length of actual data to be written |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3072 | * @buf: the data to write |
| 3073 | * @oob_required: must write chip->oob_poi to OOB |
| 3074 | * @page: page number to write |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3075 | * @raw: use _raw version of write_page |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3076 | */ |
| 3077 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3078 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3079 | int oob_required, int page, int raw) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3080 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3081 | int status, subpage; |
| 3082 | |
| 3083 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3084 | chip->ecc.write_subpage) |
| 3085 | subpage = offset || (data_len < mtd->writesize); |
| 3086 | else |
| 3087 | subpage = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3088 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3089 | if (nand_standard_page_accessors(&chip->ecc)) { |
| 3090 | status = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3091 | if (status) |
| 3092 | return status; |
| 3093 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3094 | |
| 3095 | if (unlikely(raw)) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3096 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3097 | oob_required, page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3098 | else if (subpage) |
| 3099 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3100 | buf, oob_required, page); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3101 | else |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3102 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 3103 | page); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3104 | |
| 3105 | if (status < 0) |
| 3106 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3107 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3108 | if (nand_standard_page_accessors(&chip->ecc)) |
| 3109 | return nand_prog_page_end_op(chip); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3110 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3111 | return 0; |
| 3112 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3113 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3114 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3115 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
| 3116 | * @mtd: MTD device structure |
| 3117 | * @oob: oob data buffer |
| 3118 | * @len: oob data write length |
| 3119 | * @ops: oob ops structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3120 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3121 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 3122 | struct mtd_oob_ops *ops) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3123 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3124 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3125 | |
| 3126 | /* |
| 3127 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 3128 | * data from a previous OOB read. |
| 3129 | */ |
| 3130 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 3131 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3132 | switch (ops->mode) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3133 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3134 | case MTD_OPS_PLACE_OOB: |
| 3135 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3136 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 3137 | return oob + len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3138 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3139 | case MTD_OPS_AUTO_OOB: { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3140 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 3141 | uint32_t boffs = 0, woffs = ops->ooboffs; |
| 3142 | size_t bytes = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3143 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3144 | for (; free->length && len; free++, len -= bytes) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3145 | /* Write request not from offset 0? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3146 | if (unlikely(woffs)) { |
| 3147 | if (woffs >= free->length) { |
| 3148 | woffs -= free->length; |
| 3149 | continue; |
| 3150 | } |
| 3151 | boffs = free->offset + woffs; |
| 3152 | bytes = min_t(size_t, len, |
| 3153 | (free->length - woffs)); |
| 3154 | woffs = 0; |
| 3155 | } else { |
| 3156 | bytes = min_t(size_t, len, free->length); |
| 3157 | boffs = free->offset; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3158 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3159 | memcpy(chip->oob_poi + boffs, oob, bytes); |
| 3160 | oob += bytes; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3161 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3162 | return oob; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3163 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3164 | default: |
| 3165 | BUG(); |
| 3166 | } |
| 3167 | return NULL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3168 | } |
| 3169 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3170 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3171 | |
| 3172 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3173 | * nand_do_write_ops - [INTERN] NAND write with ECC |
| 3174 | * @mtd: MTD device structure |
| 3175 | * @to: offset to write to |
| 3176 | * @ops: oob operations description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3177 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3178 | * NAND write with ECC. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3179 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3180 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 3181 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3182 | { |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3183 | int chipnr, realpage, page, column; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3184 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3185 | uint32_t writelen = ops->len; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3186 | |
| 3187 | uint32_t oobwritelen = ops->ooblen; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3188 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3189 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3190 | uint8_t *oob = ops->oobbuf; |
| 3191 | uint8_t *buf = ops->datbuf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3192 | int ret; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3193 | int oob_required = oob ? 1 : 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3194 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3195 | ops->retlen = 0; |
| 3196 | if (!writelen) |
| 3197 | return 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3198 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3199 | /* Reject writes, which are not page aligned */ |
| 3200 | if (NOTALIGNED(to)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3201 | pr_notice("%s: attempt to write non page aligned data\n", |
| 3202 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3203 | return -EINVAL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | column = to & (mtd->writesize - 1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3207 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3208 | chipnr = (int)(to >> chip->chip_shift); |
| 3209 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3210 | |
| 3211 | /* Check, if it is write protected */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3212 | if (nand_check_wp(mtd)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3213 | ret = -EIO; |
| 3214 | goto err_out; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3215 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3216 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3217 | realpage = (int)(to >> chip->page_shift); |
| 3218 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3219 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3220 | /* Invalidate the page cache, when we write to the cached page */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3221 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 3222 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3223 | chip->pagebuf = -1; |
| 3224 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3225 | /* Don't allow multipage oob writes with offset */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3226 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 3227 | ret = -EINVAL; |
| 3228 | goto err_out; |
| 3229 | } |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3230 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3231 | while (1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3232 | int bytes = mtd->writesize; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3233 | uint8_t *wbuf = buf; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3234 | int use_bufpoi; |
Hector Palacios | e4fcdbb | 2016-07-18 09:37:41 +0200 | [diff] [blame] | 3235 | int part_pagewr = (column || writelen < mtd->writesize); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3236 | |
| 3237 | if (part_pagewr) |
| 3238 | use_bufpoi = 1; |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 3239 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
| 3240 | use_bufpoi = !IS_ALIGNED((unsigned long)buf, |
| 3241 | chip->buf_align); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3242 | else |
| 3243 | use_bufpoi = 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3244 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 3245 | schedule(); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3246 | /* Partial page write?, or need to use bounce buffer */ |
| 3247 | if (use_bufpoi) { |
| 3248 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 3249 | __func__, buf); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3250 | if (part_pagewr) |
| 3251 | bytes = min_t(int, bytes - column, writelen); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3252 | chip->pagebuf = -1; |
| 3253 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 3254 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 3255 | wbuf = chip->buffers->databuf; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 3256 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3257 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3258 | if (unlikely(oob)) { |
| 3259 | size_t len = min(oobwritelen, oobmaxlen); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3260 | oob = nand_fill_oob(mtd, oob, len, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3261 | oobwritelen -= len; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3262 | } else { |
| 3263 | /* We still need to erase leftover OOB data */ |
| 3264 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3265 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3266 | ret = chip->write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3267 | oob_required, page, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3268 | (ops->mode == MTD_OPS_RAW)); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3269 | if (ret) |
| 3270 | break; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3271 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3272 | writelen -= bytes; |
| 3273 | if (!writelen) |
| 3274 | break; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3275 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3276 | column = 0; |
| 3277 | buf += bytes; |
| 3278 | realpage++; |
| 3279 | |
| 3280 | page = realpage & chip->pagemask; |
| 3281 | /* Check, if we cross a chip boundary */ |
| 3282 | if (!page) { |
| 3283 | chipnr++; |
| 3284 | chip->select_chip(mtd, -1); |
| 3285 | chip->select_chip(mtd, chipnr); |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | ops->retlen = ops->len - writelen; |
| 3290 | if (unlikely(oob)) |
| 3291 | ops->oobretlen = ops->ooblen; |
| 3292 | |
| 3293 | err_out: |
| 3294 | chip->select_chip(mtd, -1); |
| 3295 | return ret; |
| 3296 | } |
| 3297 | |
| 3298 | /** |
| 3299 | * panic_nand_write - [MTD Interface] NAND write with ECC |
| 3300 | * @mtd: MTD device structure |
| 3301 | * @to: offset to write to |
| 3302 | * @len: number of bytes to write |
| 3303 | * @retlen: pointer to variable to store the number of written bytes |
| 3304 | * @buf: the data to write |
| 3305 | * |
| 3306 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 3307 | * may for example be called by mtdoops when writing an oops while in panic. |
| 3308 | */ |
| 3309 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 3310 | size_t *retlen, const uint8_t *buf) |
| 3311 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3312 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3313 | struct mtd_oob_ops ops; |
| 3314 | int ret; |
| 3315 | |
| 3316 | /* Wait for the device to get ready */ |
| 3317 | panic_nand_wait(mtd, chip, 400); |
| 3318 | |
| 3319 | /* Grab the device */ |
| 3320 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 3321 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3322 | memset(&ops, 0, sizeof(ops)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3323 | ops.len = len; |
| 3324 | ops.datbuf = (uint8_t *)buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3325 | ops.mode = MTD_OPS_PLACE_OOB; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3326 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3327 | ret = nand_do_write_ops(mtd, to, &ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3328 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3329 | *retlen = ops.retlen; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3330 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3334 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3335 | * @mtd: MTD device structure |
| 3336 | * @to: offset to write to |
| 3337 | * @ops: oob operation description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3338 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3339 | * NAND write out-of-band. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3340 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3341 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 3342 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3343 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3344 | int chipnr, page, status, len; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3345 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3346 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3347 | pr_debug("%s: to = 0x%08x, len = %i\n", |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3348 | __func__, (unsigned int)to, (int)ops->ooblen); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3349 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3350 | len = mtd_oobavail(mtd, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3351 | |
| 3352 | /* Do not allow write past end of page */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3353 | if ((ops->ooboffs + ops->ooblen) > len) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3354 | pr_debug("%s: attempt to write past end of page\n", |
| 3355 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3356 | return -EINVAL; |
| 3357 | } |
| 3358 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3359 | if (unlikely(ops->ooboffs >= len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3360 | pr_debug("%s: attempt to start write outside oob\n", |
| 3361 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3362 | return -EINVAL; |
| 3363 | } |
| 3364 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3365 | /* Do not allow write past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3366 | if (unlikely(to >= mtd->size || |
| 3367 | ops->ooboffs + ops->ooblen > |
| 3368 | ((mtd->size >> chip->page_shift) - |
| 3369 | (to >> chip->page_shift)) * len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3370 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3371 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3372 | return -EINVAL; |
| 3373 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3374 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3375 | chipnr = (int)(to >> chip->chip_shift); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3376 | |
| 3377 | /* |
| 3378 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 3379 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 3380 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 3381 | * it in the doc2000 driver in August 1999. dwmw2. |
| 3382 | */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 3383 | nand_reset(chip, chipnr); |
| 3384 | |
| 3385 | chip->select_chip(mtd, chipnr); |
| 3386 | |
| 3387 | /* Shift to get page */ |
| 3388 | page = (int)(to >> chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3389 | |
| 3390 | /* Check, if it is write protected */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3391 | if (nand_check_wp(mtd)) { |
| 3392 | chip->select_chip(mtd, -1); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3393 | return -EROFS; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3394 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3395 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3396 | /* Invalidate the page cache, if we write to the cached page */ |
| 3397 | if (page == chip->pagebuf) |
| 3398 | chip->pagebuf = -1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3399 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3400 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
| 3401 | |
| 3402 | if (ops->mode == MTD_OPS_RAW) |
| 3403 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 3404 | else |
| 3405 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3406 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3407 | chip->select_chip(mtd, -1); |
| 3408 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3409 | if (status) |
| 3410 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3411 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3412 | ops->oobretlen = ops->ooblen; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3413 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3414 | return 0; |
| 3415 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3416 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3417 | /** |
| 3418 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3419 | * @mtd: MTD device structure |
| 3420 | * @to: offset to write to |
| 3421 | * @ops: oob operation description structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3422 | */ |
| 3423 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 3424 | struct mtd_oob_ops *ops) |
| 3425 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3426 | int ret = -ENOTSUPP; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3427 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3428 | ops->retlen = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3429 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3430 | /* Do not allow writes past end of device */ |
| 3431 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3432 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3433 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3434 | return -EINVAL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3435 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3436 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3437 | nand_get_device(mtd, FL_WRITING); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3438 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3439 | switch (ops->mode) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3440 | case MTD_OPS_PLACE_OOB: |
| 3441 | case MTD_OPS_AUTO_OOB: |
| 3442 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3443 | break; |
| 3444 | |
| 3445 | default: |
| 3446 | goto out; |
| 3447 | } |
| 3448 | |
| 3449 | if (!ops->datbuf) |
| 3450 | ret = nand_do_write_oob(mtd, to, ops); |
| 3451 | else |
| 3452 | ret = nand_do_write_ops(mtd, to, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3453 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3454 | out: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3455 | nand_release_device(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3456 | return ret; |
| 3457 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3458 | |
| 3459 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3460 | * single_erase - [GENERIC] NAND standard block erase command function |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3461 | * @mtd: MTD device structure |
| 3462 | * @page: the page address of the block which will be erased |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3463 | * |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3464 | * Standard erase command for NAND chips. Returns NAND status. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3465 | */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3466 | static int single_erase(struct mtd_info *mtd, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3467 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3468 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3469 | unsigned int eraseblock; |
| 3470 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3471 | /* Send commands to erase a block */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3472 | eraseblock = page >> (chip->phys_erase_shift - chip->page_shift); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3473 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3474 | return nand_erase_op(chip, eraseblock); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | /** |
| 3478 | * nand_erase - [MTD Interface] erase block(s) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3479 | * @mtd: MTD device structure |
| 3480 | * @instr: erase instruction |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3481 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3482 | * Erase one ore more blocks. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3483 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3484 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3485 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3486 | return nand_erase_nand(mtd, instr, 0); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3487 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3488 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3489 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3490 | * nand_erase_nand - [INTERN] erase block(s) |
| 3491 | * @mtd: MTD device structure |
| 3492 | * @instr: erase instruction |
| 3493 | * @allowbbt: allow erasing the bbt area |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3494 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3495 | * Erase one ore more blocks. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3496 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3497 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 3498 | int allowbbt) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3499 | { |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 3500 | int page, status, pages_per_block, ret, chipnr; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3501 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 3502 | loff_t len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3503 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3504 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 3505 | __func__, (unsigned long long)instr->addr, |
| 3506 | (unsigned long long)instr->len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3507 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3508 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3509 | return -EINVAL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3510 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3511 | /* Grab the lock and see if the device is available */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3512 | nand_get_device(mtd, FL_ERASING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3513 | |
| 3514 | /* Shift to get first page */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3515 | page = (int)(instr->addr >> chip->page_shift); |
| 3516 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3517 | |
| 3518 | /* Calculate pages in each block */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3519 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 3520 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3521 | /* Select the NAND device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3522 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3523 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3524 | /* Check, if it is write protected */ |
| 3525 | if (nand_check_wp(mtd)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3526 | pr_debug("%s: device is write protected!\n", |
| 3527 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3528 | instr->state = MTD_ERASE_FAILED; |
| 3529 | goto erase_exit; |
| 3530 | } |
| 3531 | |
| 3532 | /* Loop through the pages */ |
| 3533 | len = instr->len; |
| 3534 | |
| 3535 | instr->state = MTD_ERASING; |
| 3536 | |
| 3537 | while (len) { |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 3538 | schedule(); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3539 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3540 | /* Check if we have a bad block, we do not erase bad blocks! */ |
Masahiro Yamada | f5a1902 | 2014-12-16 15:36:33 +0900 | [diff] [blame] | 3541 | if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) << |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3542 | chip->page_shift, allowbbt)) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3543 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3544 | __func__, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3545 | instr->state = MTD_ERASE_FAILED; |
Farhan Ali | 7c05319 | 2021-02-24 15:25:53 -0800 | [diff] [blame] | 3546 | instr->fail_addr = |
| 3547 | ((loff_t)page << chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3548 | goto erase_exit; |
| 3549 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3550 | |
| 3551 | /* |
| 3552 | * Invalidate the page cache, if we erase the block which |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3553 | * contains the current cached page. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3554 | */ |
| 3555 | if (page <= chip->pagebuf && chip->pagebuf < |
| 3556 | (page + pages_per_block)) |
| 3557 | chip->pagebuf = -1; |
| 3558 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3559 | status = chip->erase(mtd, page & chip->pagemask); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3560 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3561 | /* See if block erase succeeded */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3562 | if (status & NAND_STATUS_FAIL) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3563 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 3564 | __func__, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3565 | instr->state = MTD_ERASE_FAILED; |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3566 | instr->fail_addr = |
| 3567 | ((loff_t)page << chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3568 | goto erase_exit; |
| 3569 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3570 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3571 | /* Increment page address and decrement length */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3572 | len -= (1ULL << chip->phys_erase_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3573 | page += pages_per_block; |
| 3574 | |
| 3575 | /* Check, if we cross a chip boundary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3576 | if (len && !(page & chip->pagemask)) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3577 | chipnr++; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3578 | chip->select_chip(mtd, -1); |
| 3579 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3580 | } |
| 3581 | } |
| 3582 | instr->state = MTD_ERASE_DONE; |
| 3583 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3584 | erase_exit: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3585 | |
| 3586 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3587 | |
| 3588 | /* Deselect and wake up anyone waiting on the device */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3589 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3590 | nand_release_device(mtd); |
| 3591 | |
| 3592 | /* Return more or less happy */ |
| 3593 | return ret; |
| 3594 | } |
| 3595 | |
| 3596 | /** |
| 3597 | * nand_sync - [MTD Interface] sync |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3598 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3599 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3600 | * Sync is actually a wait for chip ready function. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3601 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3602 | static void nand_sync(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3603 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3604 | pr_debug("%s: called\n", __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3605 | |
| 3606 | /* Grab the lock and see if the device is available */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3607 | nand_get_device(mtd, FL_SYNCING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3608 | /* Release it and go back */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3609 | nand_release_device(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3610 | } |
| 3611 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3612 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3613 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3614 | * @mtd: MTD device structure |
| 3615 | * @offs: offset relative to mtd start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3616 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3617 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3618 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3619 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3620 | int chipnr = (int)(offs >> chip->chip_shift); |
| 3621 | int ret; |
| 3622 | |
| 3623 | /* Select the NAND device */ |
| 3624 | nand_get_device(mtd, FL_READING); |
| 3625 | chip->select_chip(mtd, chipnr); |
| 3626 | |
| 3627 | ret = nand_block_checkbad(mtd, offs, 0); |
| 3628 | |
| 3629 | chip->select_chip(mtd, -1); |
| 3630 | nand_release_device(mtd); |
| 3631 | |
| 3632 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3633 | } |
| 3634 | |
| 3635 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3636 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3637 | * @mtd: MTD device structure |
| 3638 | * @ofs: offset relative to mtd start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3639 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3640 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3641 | { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3642 | int ret; |
| 3643 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3644 | ret = nand_block_isbad(mtd, ofs); |
| 3645 | if (ret) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3646 | /* If it was bad already, return success and do nothing */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3647 | if (ret > 0) |
| 3648 | return 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3649 | return ret; |
| 3650 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3651 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3652 | return nand_block_markbad_lowlevel(mtd, ofs); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3653 | } |
| 3654 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3655 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3656 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 3657 | * @mtd: MTD device structure |
| 3658 | * @chip: nand chip info structure |
| 3659 | * @addr: feature address. |
| 3660 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3661 | */ |
| 3662 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3663 | int addr, uint8_t *subfeature_param) |
| 3664 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3665 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 3666 | if (!chip->onfi_version || |
| 3667 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3668 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Mylène Josserand | c21946b | 2018-07-13 18:10:23 +0200 | [diff] [blame] | 3669 | return -ENOTSUPP; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3670 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3671 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3672 | return nand_set_features_op(chip, addr, subfeature_param); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | /** |
| 3676 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 3677 | * @mtd: MTD device structure |
| 3678 | * @chip: nand chip info structure |
| 3679 | * @addr: feature address. |
| 3680 | * @subfeature_param: the subfeature parameters, a four bytes array. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3681 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3682 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3683 | int addr, uint8_t *subfeature_param) |
| 3684 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3685 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 3686 | if (!chip->onfi_version || |
| 3687 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3688 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Mylène Josserand | c21946b | 2018-07-13 18:10:23 +0200 | [diff] [blame] | 3689 | return -ENOTSUPP; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3690 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3691 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3692 | return nand_get_features_op(chip, addr, subfeature_param); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3693 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3694 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3695 | /* Set default functions */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3696 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
| 3697 | { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3698 | /* check for proper chip_delay setup, set 20us if not */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3699 | if (!chip->chip_delay) |
| 3700 | chip->chip_delay = 20; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3701 | |
| 3702 | /* check, if a user supplied command function given */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3703 | if (chip->cmdfunc == NULL) |
| 3704 | chip->cmdfunc = nand_command; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3705 | |
| 3706 | /* check, if a user supplied wait function given */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3707 | if (chip->waitfunc == NULL) |
| 3708 | chip->waitfunc = nand_wait; |
| 3709 | |
| 3710 | if (!chip->select_chip) |
| 3711 | chip->select_chip = nand_select_chip; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3712 | |
| 3713 | /* set for ONFI nand */ |
| 3714 | if (!chip->onfi_set_features) |
| 3715 | chip->onfi_set_features = nand_onfi_set_features; |
| 3716 | if (!chip->onfi_get_features) |
| 3717 | chip->onfi_get_features = nand_onfi_get_features; |
| 3718 | |
| 3719 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 3720 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3721 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 3722 | if (!chip->read_word) |
| 3723 | chip->read_word = nand_read_word; |
| 3724 | if (!chip->block_bad) |
| 3725 | chip->block_bad = nand_block_bad; |
| 3726 | if (!chip->block_markbad) |
| 3727 | chip->block_markbad = nand_default_block_markbad; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3728 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3729 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3730 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 3731 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
| 3732 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3733 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 3734 | |
| 3735 | #ifndef CONFIG_SPL_BUILD |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3736 | if (!chip->scan_bbt) |
| 3737 | chip->scan_bbt = nand_default_bbt; |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 3738 | #endif |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3739 | |
| 3740 | if (!chip->controller) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3741 | chip->controller = &chip->hwcontrol; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3742 | spin_lock_init(&chip->controller->lock); |
| 3743 | init_waitqueue_head(&chip->controller->wq); |
| 3744 | } |
| 3745 | |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 3746 | if (!chip->buf_align) |
| 3747 | chip->buf_align = 1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3748 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3749 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3750 | /* Sanitize ONFI strings so we can safely print them */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3751 | static void sanitize_string(char *s, size_t len) |
| 3752 | { |
| 3753 | ssize_t i; |
| 3754 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3755 | /* Null terminate */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3756 | s[len - 1] = 0; |
| 3757 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3758 | /* Remove non printable chars */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3759 | for (i = 0; i < len - 1; i++) { |
| 3760 | if (s[i] < ' ' || s[i] > 127) |
| 3761 | s[i] = '?'; |
| 3762 | } |
| 3763 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3764 | /* Remove trailing spaces */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3765 | strim(s); |
| 3766 | } |
| 3767 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3768 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 3769 | { |
| 3770 | int i; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3771 | while (len--) { |
| 3772 | crc ^= *p++ << 8; |
| 3773 | for (i = 0; i < 8; i++) |
| 3774 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 3775 | } |
| 3776 | |
| 3777 | return crc; |
| 3778 | } |
| 3779 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3780 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3781 | /* Parse the Extended Parameter Page. */ |
| 3782 | static int nand_flash_detect_ext_param_page(struct mtd_info *mtd, |
| 3783 | struct nand_chip *chip, struct nand_onfi_params *p) |
| 3784 | { |
| 3785 | struct onfi_ext_param_page *ep; |
| 3786 | struct onfi_ext_section *s; |
| 3787 | struct onfi_ext_ecc_info *ecc; |
| 3788 | uint8_t *cursor; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3789 | int ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3790 | int len; |
| 3791 | int i; |
| 3792 | |
| 3793 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 3794 | ep = kmalloc(len, GFP_KERNEL); |
| 3795 | if (!ep) |
| 3796 | return -ENOMEM; |
| 3797 | |
| 3798 | /* Send our own NAND_CMD_PARAM. */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3799 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 3800 | if (ret) |
| 3801 | goto ext_out; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3802 | |
| 3803 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3804 | ret = nand_change_read_column_op(chip, |
| 3805 | sizeof(*p) * p->num_of_param_pages, |
| 3806 | ep, len, true); |
| 3807 | if (ret) |
| 3808 | goto ext_out; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3809 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3810 | ret = -EINVAL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3811 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 3812 | != le16_to_cpu(ep->crc))) { |
| 3813 | pr_debug("fail in the CRC.\n"); |
| 3814 | goto ext_out; |
| 3815 | } |
| 3816 | |
| 3817 | /* |
| 3818 | * Check the signature. |
| 3819 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 3820 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3821 | if (strncmp((char *)ep->sig, "EPPS", 4)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3822 | pr_debug("The signature is invalid.\n"); |
| 3823 | goto ext_out; |
| 3824 | } |
| 3825 | |
| 3826 | /* find the ECC section. */ |
| 3827 | cursor = (uint8_t *)(ep + 1); |
| 3828 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 3829 | s = ep->sections + i; |
| 3830 | if (s->type == ONFI_SECTION_TYPE_2) |
| 3831 | break; |
| 3832 | cursor += s->length * 16; |
| 3833 | } |
| 3834 | if (i == ONFI_EXT_SECTION_MAX) { |
| 3835 | pr_debug("We can not find the ECC section.\n"); |
| 3836 | goto ext_out; |
| 3837 | } |
| 3838 | |
| 3839 | /* get the info we want. */ |
| 3840 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 3841 | |
| 3842 | if (!ecc->codeword_size) { |
| 3843 | pr_debug("Invalid codeword size\n"); |
| 3844 | goto ext_out; |
| 3845 | } |
| 3846 | |
| 3847 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3848 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 3849 | ret = 0; |
| 3850 | |
| 3851 | ext_out: |
| 3852 | kfree(ep); |
| 3853 | return ret; |
| 3854 | } |
| 3855 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3856 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3857 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3858 | */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 3859 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3860 | { |
| 3861 | struct nand_onfi_params *p = &chip->onfi_params; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3862 | char id[4]; |
| 3863 | int i, ret, val; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3864 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3865 | /* Try ONFI for unknown chip or LP */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3866 | ret = nand_readid_op(chip, 0x20, id, sizeof(id)); |
| 3867 | if (ret || strncmp(id, "ONFI", 4)) |
| 3868 | return 0; |
| 3869 | |
| 3870 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 3871 | if (ret) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3872 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3873 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3874 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3875 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 3876 | if (ret) |
| 3877 | return 0; |
| 3878 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3879 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3880 | le16_to_cpu(p->crc)) { |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3881 | break; |
| 3882 | } |
| 3883 | } |
| 3884 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3885 | if (i == 3) { |
| 3886 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3887 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3888 | } |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3889 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3890 | /* Check version */ |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3891 | val = le16_to_cpu(p->revision); |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3892 | if (val & (1 << 5)) |
| 3893 | chip->onfi_version = 23; |
| 3894 | else if (val & (1 << 4)) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3895 | chip->onfi_version = 22; |
| 3896 | else if (val & (1 << 3)) |
| 3897 | chip->onfi_version = 21; |
| 3898 | else if (val & (1 << 2)) |
| 3899 | chip->onfi_version = 20; |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3900 | else if (val & (1 << 1)) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3901 | chip->onfi_version = 10; |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3902 | |
| 3903 | if (!chip->onfi_version) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3904 | pr_info("unsupported ONFI version: %d\n", val); |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3905 | return 0; |
| 3906 | } |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3907 | |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3908 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3909 | sanitize_string(p->model, sizeof(p->model)); |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3910 | if (!mtd->name) |
| 3911 | mtd->name = p->model; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3912 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3913 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3914 | |
| 3915 | /* |
| 3916 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 3917 | * (don't ask me who thought of this...). MTD assumes that these |
| 3918 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 3919 | */ |
| 3920 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3921 | mtd->erasesize *= mtd->writesize; |
| 3922 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3923 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3924 | |
| 3925 | /* See erasesize comment */ |
| 3926 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | b20b6f2 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 3927 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3928 | chip->bits_per_cell = p->bits_per_cell; |
| 3929 | |
| 3930 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 3931 | chip->options |= NAND_BUSWIDTH_16; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3932 | |
| 3933 | if (p->ecc_bits != 0xff) { |
| 3934 | chip->ecc_strength_ds = p->ecc_bits; |
| 3935 | chip->ecc_step_ds = 512; |
| 3936 | } else if (chip->onfi_version >= 21 && |
| 3937 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 3938 | |
| 3939 | /* |
| 3940 | * The nand_flash_detect_ext_param_page() uses the |
| 3941 | * Change Read Column command which maybe not supported |
| 3942 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 3943 | * now. We do not replace user supplied command function. |
| 3944 | */ |
| 3945 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3946 | chip->cmdfunc = nand_command_lp; |
| 3947 | |
| 3948 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
| 3949 | if (nand_flash_detect_ext_param_page(mtd, chip, p)) |
| 3950 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 3951 | } else { |
| 3952 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
| 3953 | } |
| 3954 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3955 | return 1; |
| 3956 | } |
| 3957 | #else |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 3958 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3959 | { |
| 3960 | return 0; |
| 3961 | } |
| 3962 | #endif |
| 3963 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3964 | /* |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3965 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 3966 | */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 3967 | static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip) |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3968 | { |
| 3969 | struct nand_jedec_params *p = &chip->jedec_params; |
| 3970 | struct jedec_ecc_info *ecc; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3971 | char id[5]; |
| 3972 | int i, val, ret; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3973 | |
| 3974 | /* Try JEDEC for unknown chip or LP */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3975 | ret = nand_readid_op(chip, 0x40, id, sizeof(id)); |
| 3976 | if (ret || strncmp(id, "JEDEC", sizeof(id))) |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3977 | return 0; |
| 3978 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3979 | ret = nand_read_param_page_op(chip, 0x40, NULL, 0); |
| 3980 | if (ret) |
| 3981 | return 0; |
| 3982 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3983 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3984 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 3985 | if (ret) |
| 3986 | return 0; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3987 | |
| 3988 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 3989 | le16_to_cpu(p->crc)) |
| 3990 | break; |
| 3991 | } |
| 3992 | |
| 3993 | if (i == 3) { |
| 3994 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 3995 | return 0; |
| 3996 | } |
| 3997 | |
| 3998 | /* Check version */ |
| 3999 | val = le16_to_cpu(p->revision); |
| 4000 | if (val & (1 << 2)) |
| 4001 | chip->jedec_version = 10; |
| 4002 | else if (val & (1 << 1)) |
| 4003 | chip->jedec_version = 1; /* vendor specific version */ |
| 4004 | |
| 4005 | if (!chip->jedec_version) { |
| 4006 | pr_info("unsupported JEDEC version: %d\n", val); |
| 4007 | return 0; |
| 4008 | } |
| 4009 | |
| 4010 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 4011 | sanitize_string(p->model, sizeof(p->model)); |
| 4012 | if (!mtd->name) |
| 4013 | mtd->name = p->model; |
| 4014 | |
| 4015 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 4016 | |
| 4017 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 4018 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 4019 | mtd->erasesize *= mtd->writesize; |
| 4020 | |
| 4021 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 4022 | |
| 4023 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 4024 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 4025 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 4026 | chip->bits_per_cell = p->bits_per_cell; |
| 4027 | |
| 4028 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4029 | chip->options |= NAND_BUSWIDTH_16; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4030 | |
| 4031 | /* ECC info */ |
| 4032 | ecc = &p->ecc_info[0]; |
| 4033 | |
| 4034 | if (ecc->codeword_size >= 9) { |
| 4035 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 4036 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 4037 | } else { |
| 4038 | pr_warn("Invalid codeword size\n"); |
| 4039 | } |
| 4040 | |
| 4041 | return 1; |
| 4042 | } |
| 4043 | |
| 4044 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4045 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 4046 | * @id_data: the ID string |
| 4047 | * @arrlen: the length of the @id_data array |
| 4048 | * @period: the period of repitition |
| 4049 | * |
| 4050 | * Check if an ID string is repeated within a given sequence of bytes at |
| 4051 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4052 | * period of 3). This is a helper function for nand_id_len(). Returns non-zero |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4053 | * if the repetition has a period of @period; otherwise, returns zero. |
| 4054 | */ |
| 4055 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 4056 | { |
| 4057 | int i, j; |
| 4058 | for (i = 0; i < period; i++) |
| 4059 | for (j = i + period; j < arrlen; j += period) |
| 4060 | if (id_data[i] != id_data[j]) |
| 4061 | return 0; |
| 4062 | return 1; |
| 4063 | } |
| 4064 | |
| 4065 | /* |
| 4066 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 4067 | * @id_data: the ID string |
| 4068 | * @arrlen: the length of the @id_data array |
| 4069 | |
| 4070 | * Returns the length of the ID string, according to known wraparound/trailing |
| 4071 | * zero patterns. If no pattern exists, returns the length of the array. |
| 4072 | */ |
| 4073 | static int nand_id_len(u8 *id_data, int arrlen) |
| 4074 | { |
| 4075 | int last_nonzero, period; |
| 4076 | |
| 4077 | /* Find last non-zero byte */ |
| 4078 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 4079 | if (id_data[last_nonzero]) |
| 4080 | break; |
| 4081 | |
| 4082 | /* All zeros */ |
| 4083 | if (last_nonzero < 0) |
| 4084 | return 0; |
| 4085 | |
| 4086 | /* Calculate wraparound period */ |
| 4087 | for (period = 1; period < arrlen; period++) |
| 4088 | if (nand_id_has_period(id_data, arrlen, period)) |
| 4089 | break; |
| 4090 | |
| 4091 | /* There's a repeated pattern */ |
| 4092 | if (period < arrlen) |
| 4093 | return period; |
| 4094 | |
| 4095 | /* There are trailing zeros */ |
| 4096 | if (last_nonzero < arrlen - 1) |
| 4097 | return last_nonzero + 1; |
| 4098 | |
| 4099 | /* No pattern detected */ |
| 4100 | return arrlen; |
| 4101 | } |
| 4102 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4103 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 4104 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 4105 | { |
| 4106 | int bits; |
| 4107 | |
| 4108 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 4109 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 4110 | return bits + 1; |
| 4111 | } |
| 4112 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4113 | /* |
| 4114 | * Many new NAND share similar device ID codes, which represent the size of the |
| 4115 | * chip. The rest of the parameters must be decoded according to generic or |
| 4116 | * manufacturer-specific "extended ID" decoding patterns. |
| 4117 | */ |
Michael Trimarchi | 60f26dc | 2022-07-20 18:22:08 +0200 | [diff] [blame] | 4118 | void nand_decode_ext_id(struct nand_chip *chip) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4119 | { |
Alexander Dahl | aa12453 | 2024-03-20 10:02:09 +0100 | [diff] [blame] | 4120 | struct mtd_info *mtd = nand_to_mtd(chip); |
Michael Trimarchi | 3ba671b | 2022-07-20 18:22:11 +0200 | [diff] [blame] | 4121 | int extid; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4122 | /* The 3rd id byte holds MLC / multichip data */ |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4123 | chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4124 | /* The 4th id byte is the important one */ |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4125 | extid = chip->id.data[3]; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4126 | |
Michael Trimarchi | 3dc9060 | 2022-07-20 18:22:10 +0200 | [diff] [blame] | 4127 | /* Calc pagesize */ |
| 4128 | mtd->writesize = 1024 << (extid & 0x03); |
| 4129 | extid >>= 2; |
| 4130 | /* Calc oobsize */ |
| 4131 | mtd->oobsize = (8 << (extid & 0x01)) * |
| 4132 | (mtd->writesize >> 9); |
| 4133 | extid >>= 2; |
| 4134 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 4135 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 4136 | extid >>= 2; |
| 4137 | /* Get buswidth information */ |
| 4138 | /* Get buswidth information */ |
| 4139 | if (extid & 0x1) |
| 4140 | chip->options |= NAND_BUSWIDTH_16; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4141 | } |
Michael Trimarchi | 60f26dc | 2022-07-20 18:22:08 +0200 | [diff] [blame] | 4142 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4143 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4144 | /* |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4145 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 4146 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 4147 | * table. |
| 4148 | */ |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4149 | static void nand_manufacturer_detect(struct nand_chip *chip) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4150 | { |
| 4151 | /* |
| 4152 | * Try manufacturer detection if available and use |
| 4153 | * nand_decode_ext_id() otherwise. |
| 4154 | */ |
| 4155 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Michael Trimarchi | 39c5bef | 2022-10-21 08:05:36 +0200 | [diff] [blame] | 4156 | chip->manufacturer.desc->ops->detect) { |
| 4157 | /* The 3rd id byte holds MLC / multichip data */ |
| 4158 | chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]); |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4159 | chip->manufacturer.desc->ops->detect(chip); |
Michael Trimarchi | 39c5bef | 2022-10-21 08:05:36 +0200 | [diff] [blame] | 4160 | } else { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4161 | nand_decode_ext_id(chip); |
Michael Trimarchi | 39c5bef | 2022-10-21 08:05:36 +0200 | [diff] [blame] | 4162 | } |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4163 | } |
| 4164 | |
| 4165 | /* |
| 4166 | * Manufacturer initialization. This function is called for all NANDs including |
| 4167 | * ONFI and JEDEC compliant ones. |
| 4168 | * Manufacturer drivers should put all their specific initialization code in |
| 4169 | * their ->init() hook. |
| 4170 | */ |
| 4171 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 4172 | { |
| 4173 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 4174 | !chip->manufacturer.desc->ops->init) |
| 4175 | return 0; |
| 4176 | |
| 4177 | return chip->manufacturer.desc->ops->init(chip); |
| 4178 | } |
| 4179 | |
| 4180 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4181 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 4182 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 4183 | * the chip. |
| 4184 | */ |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4185 | static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4186 | { |
Alexander Dahl | aa12453 | 2024-03-20 10:02:09 +0100 | [diff] [blame] | 4187 | struct mtd_info *mtd = nand_to_mtd(chip); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4188 | |
| 4189 | mtd->erasesize = type->erasesize; |
| 4190 | mtd->writesize = type->pagesize; |
| 4191 | mtd->oobsize = mtd->writesize / 32; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4192 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4193 | /* All legacy ID NAND are small-page, SLC */ |
| 4194 | chip->bits_per_cell = 1; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4197 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4198 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 4199 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 4200 | * page size, cell-type information). |
| 4201 | */ |
| 4202 | static void nand_decode_bbm_options(struct mtd_info *mtd, |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4203 | struct nand_chip *chip) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4204 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4205 | /* Set the bad block position */ |
| 4206 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 4207 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 4208 | else |
| 4209 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4210 | } |
| 4211 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4212 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 4213 | { |
| 4214 | return type->id_len; |
| 4215 | } |
| 4216 | |
| 4217 | static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip, |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4218 | struct nand_flash_dev *type) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4219 | { |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4220 | if (!strncmp((char *)type->id, (char *)chip->id.data, type->id_len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4221 | mtd->writesize = type->pagesize; |
| 4222 | mtd->erasesize = type->erasesize; |
| 4223 | mtd->oobsize = type->oobsize; |
| 4224 | |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4225 | chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4226 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 4227 | chip->options |= type->options; |
| 4228 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 4229 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4230 | chip->onfi_timing_mode_default = |
| 4231 | type->onfi_timing_mode_default; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4232 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4233 | if (!mtd->name) |
| 4234 | mtd->name = type->name; |
| 4235 | |
| 4236 | return true; |
| 4237 | } |
| 4238 | return false; |
| 4239 | } |
| 4240 | |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4241 | /** |
| 4242 | * nand_get_manufacturer_desc - Get manufacturer information from the |
| 4243 | * manufacturer ID |
| 4244 | * @id: manufacturer ID |
| 4245 | * |
| 4246 | * Returns a nand_manufacturer_desc object if the manufacturer is defined |
| 4247 | * in the NAND manufacturers database, NULL otherwise. |
| 4248 | */ |
Michael Trimarchi | 25bb179 | 2022-07-26 18:33:11 +0200 | [diff] [blame] | 4249 | static const struct nand_manufacturer *nand_get_manufacturer_desc(u8 id) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4250 | { |
| 4251 | int i; |
| 4252 | |
| 4253 | for (i = 0; nand_manuf_ids[i].id != 0x0; i++) { |
| 4254 | if (nand_manuf_ids[i].id == id) |
| 4255 | return &nand_manuf_ids[i]; |
| 4256 | } |
| 4257 | |
| 4258 | return NULL; |
| 4259 | } |
| 4260 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4261 | /* |
| 4262 | * Get the flash and manufacturer id and lookup if the type is supported. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4263 | */ |
Michael Trimarchi | f20a6f0 | 2022-07-25 10:18:51 +0200 | [diff] [blame] | 4264 | int nand_detect(struct nand_chip *chip, int *maf_id, |
| 4265 | int *dev_id, struct nand_flash_dev *type) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4266 | { |
Alexander Dahl | aa12453 | 2024-03-20 10:02:09 +0100 | [diff] [blame] | 4267 | struct mtd_info *mtd = nand_to_mtd(chip); |
Michael Trimarchi | 25bb179 | 2022-07-26 18:33:11 +0200 | [diff] [blame] | 4268 | const struct nand_manufacturer *manufacturer_desc; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4269 | int busw, ret; |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4270 | u8 *id_data = chip->id.data; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4271 | |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4272 | /* |
| 4273 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4274 | * after power-up. |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4275 | */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4276 | ret = nand_reset(chip, 0); |
| 4277 | if (ret) |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4278 | return ret; |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4279 | |
| 4280 | /* Select the device */ |
| 4281 | chip->select_chip(mtd, 0); |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4282 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4283 | /* Send the command for reading device ID */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4284 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 4285 | if (ret) |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4286 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4287 | |
| 4288 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4289 | *maf_id = id_data[0]; |
| 4290 | *dev_id = id_data[1]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4291 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4292 | /* |
| 4293 | * Try again to make sure, as some systems the bus-hold or other |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4294 | * interface concerns can cause random data which looks like a |
| 4295 | * possibly credible NAND flash to appear. If the two results do |
| 4296 | * not match, ignore the device completely. |
| 4297 | */ |
| 4298 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4299 | /* Read entire ID string */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4300 | ret = nand_readid_op(chip, 0, id_data, 8); |
| 4301 | if (ret) |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4302 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4303 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4304 | if (id_data[0] != *maf_id || id_data[1] != *dev_id) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4305 | pr_info("second ID read did not match %02x,%02x against %02x,%02x\n", |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4306 | *maf_id, *dev_id, id_data[0], id_data[1]); |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4307 | return -ENODEV; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4308 | } |
| 4309 | |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4310 | chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data)); |
| 4311 | |
| 4312 | /* Try to identify manufacturer */ |
| 4313 | manufacturer_desc = nand_get_manufacturer_desc(*maf_id); |
| 4314 | chip->manufacturer.desc = manufacturer_desc; |
| 4315 | |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4316 | if (!type) |
| 4317 | type = nand_flash_ids; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4318 | |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4319 | /* |
| 4320 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 4321 | * override it. |
| 4322 | * This is required to make sure initial NAND bus width set by the |
| 4323 | * NAND controller driver is coherent with the real NAND bus width |
| 4324 | * (extracted by auto-detection code). |
| 4325 | */ |
| 4326 | busw = chip->options & NAND_BUSWIDTH_16; |
| 4327 | |
| 4328 | /* |
| 4329 | * The flag is only set (never cleared), reset it to its default value |
| 4330 | * before starting auto-detection. |
| 4331 | */ |
| 4332 | chip->options &= ~NAND_BUSWIDTH_16; |
| 4333 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4334 | for (; type->name != NULL; type++) { |
| 4335 | if (is_full_id_nand(type)) { |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4336 | if (find_full_id_nand(mtd, chip, type)) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4337 | goto ident_done; |
| 4338 | } else if (*dev_id == type->dev_id) { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4339 | break; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4340 | } |
| 4341 | } |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4342 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4343 | chip->onfi_version = 0; |
| 4344 | if (!type->name || !type->pagesize) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4345 | /* Check if the chip is ONFI compliant */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4346 | if (nand_flash_detect_onfi(mtd, chip)) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4347 | goto ident_done; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4348 | |
| 4349 | /* Check if the chip is JEDEC compliant */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4350 | if (nand_flash_detect_jedec(mtd, chip)) |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4351 | goto ident_done; |
Florian Fainelli | d619189 | 2010-06-12 20:59:25 +0200 | [diff] [blame] | 4352 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4353 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4354 | if (!type->name) |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4355 | return -ENODEV; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4356 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4357 | if (!mtd->name) |
| 4358 | mtd->name = type->name; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4359 | |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 4360 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4361 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4362 | if (!type->pagesize) { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4363 | nand_manufacturer_detect(chip); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4364 | } else { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4365 | nand_decode_id(chip, type); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4366 | } |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4367 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4368 | /* Get chip options */ |
Marek Vasut | fc41719 | 2012-08-30 13:39:38 +0000 | [diff] [blame] | 4369 | chip->options |= type->options; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4370 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4371 | ident_done: |
| 4372 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4373 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
| 4374 | WARN_ON(chip->options & NAND_BUSWIDTH_16); |
| 4375 | chip->options |= busw; |
| 4376 | nand_set_defaults(chip, busw); |
| 4377 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4378 | /* |
| 4379 | * Check, if buswidth is correct. Hardware drivers should set |
| 4380 | * chip correct! |
| 4381 | */ |
| 4382 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
| 4383 | *maf_id, *dev_id); |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4384 | pr_info("%s %s\n", manufacturer_desc->name, mtd->name); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4385 | pr_warn("bus width %d instead %d bit\n", |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4386 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
| 4387 | busw ? 16 : 8); |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4388 | return -EINVAL; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4389 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4390 | |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4391 | nand_decode_bbm_options(mtd, chip); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4392 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4393 | /* Calculate the address shift from the page size */ |
| 4394 | chip->page_shift = ffs(mtd->writesize) - 1; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4395 | /* Convert chipsize to number of pages per chip -1 */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4396 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4397 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4398 | chip->bbt_erase_shift = chip->phys_erase_shift = |
| 4399 | ffs(mtd->erasesize) - 1; |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 4400 | if (chip->chipsize & 0xffffffff) |
Sandeep Paulraj | 1bc877c | 2009-11-07 14:24:06 -0500 | [diff] [blame] | 4401 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4402 | else { |
| 4403 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 4404 | chip->chip_shift += 32 - 1; |
| 4405 | } |
| 4406 | |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 4407 | if (chip->chip_shift - chip->page_shift > 16) |
| 4408 | chip->options |= NAND_ROW_ADDR_3; |
| 4409 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4410 | chip->badblockbits = 8; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4411 | chip->erase = single_erase; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4412 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4413 | /* Do not replace user supplied command function! */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4414 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 4415 | chip->cmdfunc = nand_command_lp; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4416 | |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4417 | ret = nand_manufacturer_init(chip); |
| 4418 | if (ret) |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4419 | return ret; |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4420 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4421 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
| 4422 | *maf_id, *dev_id); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4423 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4424 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4425 | if (chip->onfi_version) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4426 | pr_info("%s %s\n", manufacturer_desc->name, |
| 4427 | chip->onfi_params.model); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4428 | else if (chip->jedec_version) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4429 | pr_info("%s %s\n", manufacturer_desc->name, |
| 4430 | chip->jedec_params.model); |
Sean Anderson | e476f11 | 2023-11-04 16:37:40 -0400 | [diff] [blame] | 4431 | else if (manufacturer_desc) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4432 | pr_info("%s %s\n", manufacturer_desc->name, type->name); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4433 | #else |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4434 | if (chip->jedec_version) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4435 | pr_info("%s %s\n", manufacturer_desc->name, |
| 4436 | chip->jedec_params.model); |
Sean Anderson | e476f11 | 2023-11-04 16:37:40 -0400 | [diff] [blame] | 4437 | else if (manufacturer_desc) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4438 | pr_info("%s %s\n", manufacturer_desc->name, type->name); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4439 | #endif |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4440 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4441 | pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4442 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4443 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4444 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4445 | } |
Michael Trimarchi | f20a6f0 | 2022-07-25 10:18:51 +0200 | [diff] [blame] | 4446 | EXPORT_SYMBOL(nand_detect); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4447 | |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4448 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4449 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4450 | static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node) |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4451 | { |
| 4452 | int ret, ecc_mode = -1, ecc_strength, ecc_step; |
Linus Walleij | 4e8611a | 2023-04-07 15:40:05 +0200 | [diff] [blame] | 4453 | int ecc_algo = NAND_ECC_UNKNOWN; |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4454 | const char *str; |
| 4455 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4456 | ret = ofnode_read_s32_default(node, "nand-bus-width", -1); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4457 | if (ret == 16) |
| 4458 | chip->options |= NAND_BUSWIDTH_16; |
| 4459 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4460 | if (ofnode_read_bool(node, "nand-on-flash-bbt")) |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4461 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4462 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4463 | str = ofnode_read_string(node, "nand-ecc-mode"); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4464 | if (str) { |
| 4465 | if (!strcmp(str, "none")) |
| 4466 | ecc_mode = NAND_ECC_NONE; |
| 4467 | else if (!strcmp(str, "soft")) |
| 4468 | ecc_mode = NAND_ECC_SOFT; |
| 4469 | else if (!strcmp(str, "hw")) |
| 4470 | ecc_mode = NAND_ECC_HW; |
| 4471 | else if (!strcmp(str, "hw_syndrome")) |
| 4472 | ecc_mode = NAND_ECC_HW_SYNDROME; |
| 4473 | else if (!strcmp(str, "hw_oob_first")) |
| 4474 | ecc_mode = NAND_ECC_HW_OOB_FIRST; |
| 4475 | else if (!strcmp(str, "soft_bch")) |
| 4476 | ecc_mode = NAND_ECC_SOFT_BCH; |
| 4477 | } |
| 4478 | |
Linus Walleij | 4e8611a | 2023-04-07 15:40:05 +0200 | [diff] [blame] | 4479 | str = ofnode_read_string(node, "nand-ecc-algo"); |
| 4480 | if (str) { |
| 4481 | /* |
| 4482 | * If we are in NAND_ECC_SOFT mode, just alter the |
| 4483 | * soft mode to BCH here. No change of algorithm. |
| 4484 | */ |
| 4485 | if (ecc_mode == NAND_ECC_SOFT) { |
| 4486 | if (!strcmp(str, "bch")) |
| 4487 | ecc_mode = NAND_ECC_SOFT_BCH; |
| 4488 | } else { |
| 4489 | if (!strcmp(str, "bch")) { |
| 4490 | ecc_algo = NAND_ECC_BCH; |
| 4491 | } else if (!strcmp(str, "hamming")) { |
| 4492 | ecc_algo = NAND_ECC_HAMMING; |
| 4493 | } |
| 4494 | } |
Pali Rohár | 44acf8a | 2022-04-04 18:17:21 +0200 | [diff] [blame] | 4495 | } |
| 4496 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4497 | ecc_strength = ofnode_read_s32_default(node, |
| 4498 | "nand-ecc-strength", -1); |
| 4499 | ecc_step = ofnode_read_s32_default(node, |
| 4500 | "nand-ecc-step-size", -1); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4501 | |
| 4502 | if ((ecc_step >= 0 && !(ecc_strength >= 0)) || |
| 4503 | (!(ecc_step >= 0) && ecc_strength >= 0)) { |
| 4504 | pr_err("must set both strength and step size in DT\n"); |
| 4505 | return -EINVAL; |
| 4506 | } |
| 4507 | |
Linus Walleij | 4e8611a | 2023-04-07 15:40:05 +0200 | [diff] [blame] | 4508 | /* |
| 4509 | * Chip drivers may have assigned default algorithms here, |
| 4510 | * onlt override it if we have found something explicitly |
| 4511 | * specified in the device tree. |
| 4512 | */ |
| 4513 | if (ecc_algo != NAND_ECC_UNKNOWN) |
| 4514 | chip->ecc.algo = ecc_algo; |
| 4515 | |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4516 | if (ecc_mode >= 0) |
| 4517 | chip->ecc.mode = ecc_mode; |
| 4518 | |
| 4519 | if (ecc_strength >= 0) |
| 4520 | chip->ecc.strength = ecc_strength; |
| 4521 | |
| 4522 | if (ecc_step > 0) |
| 4523 | chip->ecc.size = ecc_step; |
| 4524 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4525 | if (ofnode_read_bool(node, "nand-ecc-maximize")) |
Boris Brezillon | f1a54b0 | 2017-11-22 02:38:13 +0900 | [diff] [blame] | 4526 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4527 | |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4528 | return 0; |
| 4529 | } |
| 4530 | #else |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4531 | static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node) |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4532 | { |
| 4533 | return 0; |
| 4534 | } |
| 4535 | #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ |
| 4536 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4537 | /** |
| 4538 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4539 | * @mtd: MTD device structure |
| 4540 | * @maxchips: number of chips to scan for |
| 4541 | * @table: alternative NAND ID table |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4542 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4543 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4544 | * flash ID and sets up MTD fields accordingly. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4545 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4546 | */ |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4547 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4548 | struct nand_flash_dev *table) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4549 | { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4550 | int i, nand_maf_id, nand_dev_id; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4551 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4552 | int ret; |
| 4553 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4554 | if (ofnode_valid(chip->flash_node)) { |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4555 | ret = nand_dt_init(mtd, chip, chip->flash_node); |
| 4556 | if (ret) |
| 4557 | return ret; |
| 4558 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4559 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4560 | /* Set the default functions */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4561 | nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4562 | |
| 4563 | /* Read the flash type */ |
Michael Trimarchi | f20a6f0 | 2022-07-25 10:18:51 +0200 | [diff] [blame] | 4564 | ret = nand_detect(chip, &nand_maf_id, &nand_dev_id, table); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4565 | |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4566 | if (ret) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4567 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
| 4568 | pr_warn("No NAND device found\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4569 | chip->select_chip(mtd, -1); |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4570 | return ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4571 | } |
| 4572 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4573 | /* Initialize the ->data_interface field. */ |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 4574 | ret = nand_init_data_interface(chip); |
| 4575 | if (ret) |
| 4576 | return ret; |
| 4577 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4578 | /* |
| 4579 | * Setup the data interface correctly on the chip and controller side. |
| 4580 | * This explicit call to nand_setup_data_interface() is only required |
| 4581 | * for the first die, because nand_reset() has been called before |
| 4582 | * ->data_interface and ->default_onfi_timing_mode were set. |
| 4583 | * For the other dies, nand_reset() will automatically switch to the |
| 4584 | * best mode for us. |
| 4585 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 4586 | ret = nand_setup_data_interface(chip, 0); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4587 | if (ret) |
| 4588 | return ret; |
| 4589 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4590 | chip->select_chip(mtd, -1); |
| 4591 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4592 | /* Check for a chip array */ |
| 4593 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4594 | u8 id[2]; |
| 4595 | |
Michael Trimarchi | f20a6f0 | 2022-07-25 10:18:51 +0200 | [diff] [blame] | 4596 | /* See comment in nand_detect for reset */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4597 | nand_reset(chip, i); |
| 4598 | |
| 4599 | chip->select_chip(mtd, i); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4600 | /* Send the command for reading device ID */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4601 | nand_readid_op(chip, 0, id, sizeof(id)); |
| 4602 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4603 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4604 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4605 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4606 | break; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4607 | } |
| 4608 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4609 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4610 | |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 4611 | #ifdef DEBUG |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4612 | if (i > 1) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4613 | pr_info("%d chips detected\n", i); |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 4614 | #endif |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4615 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4616 | /* Store the number of chips and calc total size for mtd */ |
| 4617 | chip->numchips = i; |
| 4618 | mtd->size = i * chip->chipsize; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4619 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4620 | return 0; |
| 4621 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4622 | EXPORT_SYMBOL(nand_scan_ident); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4623 | |
Masahiro Yamada | 820eb48 | 2017-11-22 02:38:29 +0900 | [diff] [blame] | 4624 | /** |
| 4625 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 4626 | * @chip: nand chip info structure |
| 4627 | * @caps: ECC caps info structure |
| 4628 | * @oobavail: OOB size that the ECC engine can use |
| 4629 | * |
| 4630 | * When ECC step size and strength are already set, check if they are supported |
| 4631 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 4632 | * On success, the calculated ECC bytes is set. |
| 4633 | */ |
| 4634 | int nand_check_ecc_caps(struct nand_chip *chip, |
| 4635 | const struct nand_ecc_caps *caps, int oobavail) |
| 4636 | { |
| 4637 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4638 | const struct nand_ecc_step_info *stepinfo; |
| 4639 | int preset_step = chip->ecc.size; |
| 4640 | int preset_strength = chip->ecc.strength; |
| 4641 | int nsteps, ecc_bytes; |
| 4642 | int i, j; |
| 4643 | |
| 4644 | if (WARN_ON(oobavail < 0)) |
| 4645 | return -EINVAL; |
| 4646 | |
| 4647 | if (!preset_step || !preset_strength) |
| 4648 | return -ENODATA; |
| 4649 | |
| 4650 | nsteps = mtd->writesize / preset_step; |
| 4651 | |
| 4652 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4653 | stepinfo = &caps->stepinfos[i]; |
| 4654 | |
| 4655 | if (stepinfo->stepsize != preset_step) |
| 4656 | continue; |
| 4657 | |
| 4658 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4659 | if (stepinfo->strengths[j] != preset_strength) |
| 4660 | continue; |
| 4661 | |
| 4662 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 4663 | preset_strength); |
| 4664 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4665 | return ecc_bytes; |
| 4666 | |
| 4667 | if (ecc_bytes * nsteps > oobavail) { |
| 4668 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 4669 | preset_step, preset_strength); |
| 4670 | return -ENOSPC; |
| 4671 | } |
| 4672 | |
| 4673 | chip->ecc.bytes = ecc_bytes; |
| 4674 | |
| 4675 | return 0; |
| 4676 | } |
| 4677 | } |
| 4678 | |
| 4679 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 4680 | preset_step, preset_strength); |
| 4681 | |
| 4682 | return -ENOTSUPP; |
| 4683 | } |
| 4684 | EXPORT_SYMBOL_GPL(nand_check_ecc_caps); |
| 4685 | |
| 4686 | /** |
| 4687 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 4688 | * @chip: nand chip info structure |
| 4689 | * @caps: ECC engine caps info structure |
| 4690 | * @oobavail: OOB size that the ECC engine can use |
| 4691 | * |
| 4692 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 4693 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 4694 | * On success, the chosen ECC settings are set. |
| 4695 | */ |
| 4696 | int nand_match_ecc_req(struct nand_chip *chip, |
| 4697 | const struct nand_ecc_caps *caps, int oobavail) |
| 4698 | { |
| 4699 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4700 | const struct nand_ecc_step_info *stepinfo; |
| 4701 | int req_step = chip->ecc_step_ds; |
| 4702 | int req_strength = chip->ecc_strength_ds; |
| 4703 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 4704 | int best_step, best_strength, best_ecc_bytes; |
| 4705 | int best_ecc_bytes_total = INT_MAX; |
| 4706 | int i, j; |
| 4707 | |
| 4708 | if (WARN_ON(oobavail < 0)) |
| 4709 | return -EINVAL; |
| 4710 | |
| 4711 | /* No information provided by the NAND chip */ |
| 4712 | if (!req_step || !req_strength) |
| 4713 | return -ENOTSUPP; |
| 4714 | |
| 4715 | /* number of correctable bits the chip requires in a page */ |
| 4716 | req_corr = mtd->writesize / req_step * req_strength; |
| 4717 | |
| 4718 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4719 | stepinfo = &caps->stepinfos[i]; |
| 4720 | step_size = stepinfo->stepsize; |
| 4721 | |
| 4722 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4723 | strength = stepinfo->strengths[j]; |
| 4724 | |
| 4725 | /* |
| 4726 | * If both step size and strength are smaller than the |
| 4727 | * chip's requirement, it is not easy to compare the |
| 4728 | * resulted reliability. |
| 4729 | */ |
| 4730 | if (step_size < req_step && strength < req_strength) |
| 4731 | continue; |
| 4732 | |
| 4733 | if (mtd->writesize % step_size) |
| 4734 | continue; |
| 4735 | |
| 4736 | nsteps = mtd->writesize / step_size; |
| 4737 | |
| 4738 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4739 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4740 | continue; |
| 4741 | ecc_bytes_total = ecc_bytes * nsteps; |
| 4742 | |
| 4743 | if (ecc_bytes_total > oobavail || |
| 4744 | strength * nsteps < req_corr) |
| 4745 | continue; |
| 4746 | |
| 4747 | /* |
| 4748 | * We assume the best is to meet the chip's requrement |
| 4749 | * with the least number of ECC bytes. |
| 4750 | */ |
| 4751 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 4752 | best_ecc_bytes_total = ecc_bytes_total; |
| 4753 | best_step = step_size; |
| 4754 | best_strength = strength; |
| 4755 | best_ecc_bytes = ecc_bytes; |
| 4756 | } |
| 4757 | } |
| 4758 | } |
| 4759 | |
| 4760 | if (best_ecc_bytes_total == INT_MAX) |
| 4761 | return -ENOTSUPP; |
| 4762 | |
| 4763 | chip->ecc.size = best_step; |
| 4764 | chip->ecc.strength = best_strength; |
| 4765 | chip->ecc.bytes = best_ecc_bytes; |
| 4766 | |
| 4767 | return 0; |
| 4768 | } |
| 4769 | EXPORT_SYMBOL_GPL(nand_match_ecc_req); |
| 4770 | |
| 4771 | /** |
| 4772 | * nand_maximize_ecc - choose the max ECC strength available |
| 4773 | * @chip: nand chip info structure |
| 4774 | * @caps: ECC engine caps info structure |
| 4775 | * @oobavail: OOB size that the ECC engine can use |
| 4776 | * |
| 4777 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 4778 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 4779 | */ |
| 4780 | int nand_maximize_ecc(struct nand_chip *chip, |
| 4781 | const struct nand_ecc_caps *caps, int oobavail) |
| 4782 | { |
| 4783 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4784 | const struct nand_ecc_step_info *stepinfo; |
| 4785 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 4786 | int best_corr = 0; |
| 4787 | int best_step = 0; |
| 4788 | int best_strength, best_ecc_bytes; |
| 4789 | int i, j; |
| 4790 | |
| 4791 | if (WARN_ON(oobavail < 0)) |
| 4792 | return -EINVAL; |
| 4793 | |
| 4794 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4795 | stepinfo = &caps->stepinfos[i]; |
| 4796 | step_size = stepinfo->stepsize; |
| 4797 | |
| 4798 | /* If chip->ecc.size is already set, respect it */ |
| 4799 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 4800 | continue; |
| 4801 | |
| 4802 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4803 | strength = stepinfo->strengths[j]; |
| 4804 | |
| 4805 | if (mtd->writesize % step_size) |
| 4806 | continue; |
| 4807 | |
| 4808 | nsteps = mtd->writesize / step_size; |
| 4809 | |
| 4810 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4811 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4812 | continue; |
| 4813 | |
| 4814 | if (ecc_bytes * nsteps > oobavail) |
| 4815 | continue; |
| 4816 | |
| 4817 | corr = strength * nsteps; |
| 4818 | |
| 4819 | /* |
| 4820 | * If the number of correctable bits is the same, |
| 4821 | * bigger step_size has more reliability. |
| 4822 | */ |
| 4823 | if (corr > best_corr || |
| 4824 | (corr == best_corr && step_size > best_step)) { |
| 4825 | best_corr = corr; |
| 4826 | best_step = step_size; |
| 4827 | best_strength = strength; |
| 4828 | best_ecc_bytes = ecc_bytes; |
| 4829 | } |
| 4830 | } |
| 4831 | } |
| 4832 | |
| 4833 | if (!best_corr) |
| 4834 | return -ENOTSUPP; |
| 4835 | |
| 4836 | chip->ecc.size = best_step; |
| 4837 | chip->ecc.strength = best_strength; |
| 4838 | chip->ecc.bytes = best_ecc_bytes; |
| 4839 | |
| 4840 | return 0; |
| 4841 | } |
| 4842 | EXPORT_SYMBOL_GPL(nand_maximize_ecc); |
| 4843 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4844 | /* |
| 4845 | * Check if the chip configuration meet the datasheet requirements. |
| 4846 | |
| 4847 | * If our configuration corrects A bits per B bytes and the minimum |
| 4848 | * required correction level is X bits per Y bytes, then we must ensure |
| 4849 | * both of the following are true: |
| 4850 | * |
| 4851 | * (1) A / B >= X / Y |
| 4852 | * (2) A >= X |
| 4853 | * |
| 4854 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 4855 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 4856 | * in the same sector. |
| 4857 | */ |
| 4858 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 4859 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4860 | struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4861 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4862 | int corr, ds_corr; |
| 4863 | |
| 4864 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 4865 | /* Not enough information */ |
| 4866 | return true; |
| 4867 | |
| 4868 | /* |
| 4869 | * We get the number of corrected bits per page to compare |
| 4870 | * the correction density. |
| 4871 | */ |
| 4872 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 4873 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 4874 | |
| 4875 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 4876 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4877 | |
Marc Gonzalez | c3a2985 | 2017-11-22 02:38:22 +0900 | [diff] [blame] | 4878 | static bool invalid_ecc_page_accessors(struct nand_chip *chip) |
| 4879 | { |
| 4880 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4881 | |
| 4882 | if (nand_standard_page_accessors(ecc)) |
| 4883 | return false; |
| 4884 | |
| 4885 | /* |
| 4886 | * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND |
| 4887 | * controller driver implements all the page accessors because |
| 4888 | * default helpers are not suitable when the core does not |
| 4889 | * send the READ0/PAGEPROG commands. |
| 4890 | */ |
| 4891 | return (!ecc->read_page || !ecc->write_page || |
| 4892 | !ecc->read_page_raw || !ecc->write_page_raw || |
| 4893 | (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) || |
| 4894 | (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage && |
| 4895 | ecc->hwctl && ecc->calculate)); |
| 4896 | } |
| 4897 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4898 | /** |
| 4899 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4900 | * @mtd: MTD device structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4901 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4902 | * This is the second phase of the normal nand_scan() function. It fills out |
| 4903 | * all the uninitialized function pointers with the defaults and scans for a |
| 4904 | * bad block table if appropriate. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4905 | */ |
| 4906 | int nand_scan_tail(struct mtd_info *mtd) |
| 4907 | { |
| 4908 | int i; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4909 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4910 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4911 | struct nand_buffers *nbuf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4912 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4913 | /* New bad blocks should be marked in OOB, flash-based BBT, or both */ |
| 4914 | BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
| 4915 | !(chip->bbt_options & NAND_BBT_USE_FLASH)); |
| 4916 | |
Marc Gonzalez | c3a2985 | 2017-11-22 02:38:22 +0900 | [diff] [blame] | 4917 | if (invalid_ecc_page_accessors(chip)) { |
| 4918 | pr_err("Invalid ECC page accessors setup\n"); |
| 4919 | return -EINVAL; |
| 4920 | } |
| 4921 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4922 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4923 | nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4924 | chip->buffers = nbuf; |
| 4925 | } else { |
| 4926 | if (!chip->buffers) |
| 4927 | return -ENOMEM; |
| 4928 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4929 | |
| 4930 | /* Set the internal oob buffer location, just after the page data */ |
| 4931 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
| 4932 | |
| 4933 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4934 | * If no default placement scheme is given, select an appropriate one. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4935 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4936 | if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4937 | switch (mtd->oobsize) { |
Gregory CLEMENT | e5b9631 | 2019-04-17 11:22:05 +0200 | [diff] [blame] | 4938 | #ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4939 | case 8: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4940 | ecc->layout = &nand_oob_8; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4941 | break; |
| 4942 | case 16: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4943 | ecc->layout = &nand_oob_16; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4944 | break; |
| 4945 | case 64: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4946 | ecc->layout = &nand_oob_64; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4947 | break; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 4948 | case 128: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4949 | ecc->layout = &nand_oob_128; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 4950 | break; |
Stefan Agner | bd18614 | 2018-12-06 14:57:09 +0100 | [diff] [blame] | 4951 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4952 | default: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4953 | pr_warn("No oob scheme defined for oobsize %d\n", |
| 4954 | mtd->oobsize); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4955 | BUG(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4956 | } |
| 4957 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4958 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4959 | if (!chip->write_page) |
| 4960 | chip->write_page = nand_write_page; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4961 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4962 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4963 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4964 | * selected and we have 256 byte pagesize fallback to software ECC |
| 4965 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4966 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4967 | switch (ecc->mode) { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 4968 | case NAND_ECC_HW_OOB_FIRST: |
| 4969 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4970 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4971 | pr_warn("No ECC functions supplied; hardware ECC not possible\n"); |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 4972 | BUG(); |
| 4973 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4974 | if (!ecc->read_page) |
| 4975 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 4976 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4977 | case NAND_ECC_HW: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4978 | /* Use standard hwecc read page function? */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4979 | if (!ecc->read_page) |
| 4980 | ecc->read_page = nand_read_page_hwecc; |
| 4981 | if (!ecc->write_page) |
| 4982 | ecc->write_page = nand_write_page_hwecc; |
| 4983 | if (!ecc->read_page_raw) |
| 4984 | ecc->read_page_raw = nand_read_page_raw; |
| 4985 | if (!ecc->write_page_raw) |
| 4986 | ecc->write_page_raw = nand_write_page_raw; |
| 4987 | if (!ecc->read_oob) |
| 4988 | ecc->read_oob = nand_read_oob_std; |
| 4989 | if (!ecc->write_oob) |
| 4990 | ecc->write_oob = nand_write_oob_std; |
| 4991 | if (!ecc->read_subpage) |
| 4992 | ecc->read_subpage = nand_read_subpage; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4993 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4994 | ecc->write_subpage = nand_write_subpage_hwecc; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4995 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4996 | case NAND_ECC_HW_SYNDROME: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4997 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 4998 | (!ecc->read_page || |
| 4999 | ecc->read_page == nand_read_page_hwecc || |
| 5000 | !ecc->write_page || |
| 5001 | ecc->write_page == nand_write_page_hwecc)) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5002 | pr_warn("No ECC functions supplied; hardware ECC not possible\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5003 | BUG(); |
| 5004 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5005 | /* Use standard syndrome read/write page function? */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5006 | if (!ecc->read_page) |
| 5007 | ecc->read_page = nand_read_page_syndrome; |
| 5008 | if (!ecc->write_page) |
| 5009 | ecc->write_page = nand_write_page_syndrome; |
| 5010 | if (!ecc->read_page_raw) |
| 5011 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 5012 | if (!ecc->write_page_raw) |
| 5013 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 5014 | if (!ecc->read_oob) |
| 5015 | ecc->read_oob = nand_read_oob_syndrome; |
| 5016 | if (!ecc->write_oob) |
| 5017 | ecc->write_oob = nand_write_oob_syndrome; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5018 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5019 | if (mtd->writesize >= ecc->size) { |
| 5020 | if (!ecc->strength) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5021 | pr_warn("Driver must set ecc.strength when using hardware ECC\n"); |
| 5022 | BUG(); |
| 5023 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5024 | break; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5025 | } |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5026 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 5027 | ecc->size, mtd->writesize); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5028 | ecc->mode = NAND_ECC_SOFT; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5029 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5030 | case NAND_ECC_SOFT: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5031 | ecc->calculate = nand_calculate_ecc; |
| 5032 | ecc->correct = nand_correct_data; |
| 5033 | ecc->read_page = nand_read_page_swecc; |
| 5034 | ecc->read_subpage = nand_read_subpage; |
| 5035 | ecc->write_page = nand_write_page_swecc; |
| 5036 | ecc->read_page_raw = nand_read_page_raw; |
| 5037 | ecc->write_page_raw = nand_write_page_raw; |
| 5038 | ecc->read_oob = nand_read_oob_std; |
| 5039 | ecc->write_oob = nand_write_oob_std; |
| 5040 | if (!ecc->size) |
| 5041 | ecc->size = 256; |
| 5042 | ecc->bytes = 3; |
| 5043 | ecc->strength = 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5044 | break; |
| 5045 | |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5046 | case NAND_ECC_SOFT_BCH: |
| 5047 | if (!mtd_nand_has_bch()) { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 5048 | pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5049 | BUG(); |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5050 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5051 | ecc->calculate = nand_bch_calculate_ecc; |
| 5052 | ecc->correct = nand_bch_correct_data; |
| 5053 | ecc->read_page = nand_read_page_swecc; |
| 5054 | ecc->read_subpage = nand_read_subpage; |
| 5055 | ecc->write_page = nand_write_page_swecc; |
| 5056 | ecc->read_page_raw = nand_read_page_raw; |
| 5057 | ecc->write_page_raw = nand_write_page_raw; |
| 5058 | ecc->read_oob = nand_read_oob_std; |
| 5059 | ecc->write_oob = nand_write_oob_std; |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5060 | /* |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5061 | * Board driver should supply ecc.size and ecc.strength values |
| 5062 | * to select how many bits are correctable. Otherwise, default |
| 5063 | * to 4 bits for large page devices. |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5064 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5065 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 5066 | ecc->size = 512; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5067 | ecc->strength = 4; |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5068 | } |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5069 | |
| 5070 | /* See nand_bch_init() for details. */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5071 | ecc->bytes = 0; |
| 5072 | ecc->priv = nand_bch_init(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5073 | if (!ecc->priv) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5074 | pr_warn("BCH ECC initialization failed!\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5075 | BUG(); |
| 5076 | } |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5077 | break; |
| 5078 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5079 | case NAND_ECC_NONE: |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5080 | pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5081 | ecc->read_page = nand_read_page_raw; |
| 5082 | ecc->write_page = nand_write_page_raw; |
| 5083 | ecc->read_oob = nand_read_oob_std; |
| 5084 | ecc->read_page_raw = nand_read_page_raw; |
| 5085 | ecc->write_page_raw = nand_write_page_raw; |
| 5086 | ecc->write_oob = nand_write_oob_std; |
| 5087 | ecc->size = mtd->writesize; |
| 5088 | ecc->bytes = 0; |
| 5089 | ecc->strength = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5090 | break; |
| 5091 | |
| 5092 | default: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5093 | pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5094 | BUG(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5095 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5096 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5097 | /* For many systems, the standard OOB write also works for raw */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5098 | if (!ecc->read_oob_raw) |
| 5099 | ecc->read_oob_raw = ecc->read_oob; |
| 5100 | if (!ecc->write_oob_raw) |
| 5101 | ecc->write_oob_raw = ecc->write_oob; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5102 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5103 | /* |
| 5104 | * The number of bytes available for a client to place data into |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5105 | * the out of band area. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5106 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5107 | mtd->oobavail = 0; |
| 5108 | if (ecc->layout) { |
| 5109 | for (i = 0; ecc->layout->oobfree[i].length; i++) |
| 5110 | mtd->oobavail += ecc->layout->oobfree[i].length; |
| 5111 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5112 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5113 | /* ECC sanity check: warn if it's too weak */ |
| 5114 | if (!nand_ecc_strength_good(mtd)) |
| 5115 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 5116 | mtd->name); |
| 5117 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5118 | /* |
| 5119 | * Set the number of read / write steps for one page depending on ECC |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5120 | * mode. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5121 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5122 | ecc->steps = mtd->writesize / ecc->size; |
| 5123 | if (ecc->steps * ecc->size != mtd->writesize) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5124 | pr_warn("Invalid ECC parameters\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5125 | BUG(); |
| 5126 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5127 | ecc->total = ecc->steps * ecc->bytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5128 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5129 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5130 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
| 5131 | switch (ecc->steps) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5132 | case 2: |
| 5133 | mtd->subpage_sft = 1; |
| 5134 | break; |
| 5135 | case 4: |
| 5136 | case 8: |
Sandeep Paulraj | fd9874d | 2009-11-07 14:24:34 -0500 | [diff] [blame] | 5137 | case 16: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5138 | mtd->subpage_sft = 2; |
| 5139 | break; |
| 5140 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5141 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5142 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5143 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5144 | /* Initialize state */ |
| 5145 | chip->state = FL_READY; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5146 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5147 | /* Invalidate the pagebuffer reference */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5148 | chip->pagebuf = -1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5149 | |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 5150 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5151 | switch (ecc->mode) { |
| 5152 | case NAND_ECC_SOFT: |
| 5153 | case NAND_ECC_SOFT_BCH: |
| 5154 | if (chip->page_shift > 9) |
| 5155 | chip->options |= NAND_SUBPAGE_READ; |
| 5156 | break; |
| 5157 | |
| 5158 | default: |
| 5159 | break; |
| 5160 | } |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 5161 | |
Patrice Chotard | bee3287 | 2022-03-21 09:13:36 +0100 | [diff] [blame] | 5162 | mtd->flash_node = chip->flash_node; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5163 | /* Fill in remaining MTD driver data */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5164 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 5165 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 5166 | MTD_CAP_NANDFLASH; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5167 | mtd->_erase = nand_erase; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5168 | mtd->_panic_write = panic_nand_write; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5169 | mtd->_read_oob = nand_read_oob; |
| 5170 | mtd->_write_oob = nand_write_oob; |
| 5171 | mtd->_sync = nand_sync; |
| 5172 | mtd->_lock = NULL; |
| 5173 | mtd->_unlock = NULL; |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 5174 | mtd->_block_isreserved = nand_block_isreserved; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5175 | mtd->_block_isbad = nand_block_isbad; |
| 5176 | mtd->_block_markbad = nand_block_markbad; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5177 | mtd->writebufsize = mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5178 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5179 | /* propagate ecc info to mtd_info */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5180 | mtd->ecclayout = ecc->layout; |
| 5181 | mtd->ecc_strength = ecc->strength; |
| 5182 | mtd->ecc_step_size = ecc->size; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5183 | /* |
| 5184 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 5185 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 5186 | * properly set. |
| 5187 | */ |
| 5188 | if (!mtd->bitflip_threshold) |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5189 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5190 | |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 5191 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5192 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5193 | EXPORT_SYMBOL(nand_scan_tail); |
| 5194 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5195 | /** |
| 5196 | * nand_scan - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5197 | * @mtd: MTD device structure |
| 5198 | * @maxchips: number of chips to scan for |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5199 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5200 | * This fills out all the uninitialized function pointers with the defaults. |
| 5201 | * The flash ID is read and the mtd/chip structures are filled with the |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5202 | * appropriate values. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5203 | */ |
| 5204 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 5205 | { |
| 5206 | int ret; |
| 5207 | |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 5208 | ret = nand_scan_ident(mtd, maxchips, NULL); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5209 | if (!ret) |
| 5210 | ret = nand_scan_tail(mtd); |
| 5211 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5212 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5213 | EXPORT_SYMBOL(nand_scan); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5214 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5215 | MODULE_LICENSE("GPL"); |
| 5216 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5217 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
| 5218 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |