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 | |
Dinesh Maniyam | ad2868f | 2025-02-27 00:18:28 +0800 | [diff] [blame] | 309 | /* |
| 310 | * nand_bbm_get_next_page - Get the next page for bad block markers |
| 311 | * @chip: The NAND chip |
| 312 | * @page: First page to start checking for bad block marker usage |
| 313 | * |
| 314 | * Returns an integer that corresponds to the page offset within a block, for |
| 315 | * a page that is used to store bad block markers. If no more pages are |
| 316 | * available, -EINVAL is returned. |
| 317 | */ |
| 318 | int nand_bbm_get_next_page(struct nand_chip *chip, int page) |
| 319 | { |
| 320 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 321 | int last_page = ((mtd->erasesize - mtd->writesize) >> |
| 322 | chip->page_shift) & chip->pagemask; |
| 323 | unsigned int bbm_flags = NAND_BBM_FIRSTPAGE | NAND_BBM_SECONDPAGE |
| 324 | | NAND_BBM_LASTPAGE; |
| 325 | |
| 326 | if (page == 0 && !(chip->options & bbm_flags)) |
| 327 | return 0; |
| 328 | if (page == 0 && chip->options & NAND_BBM_FIRSTPAGE) |
| 329 | return 0; |
| 330 | if (page <= 1 && chip->options & NAND_BBM_SECONDPAGE) |
| 331 | return 1; |
| 332 | if (page <= last_page && chip->options & NAND_BBM_LASTPAGE) |
| 333 | return last_page; |
| 334 | |
| 335 | return -EINVAL; |
| 336 | } |
| 337 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 338 | /** |
| 339 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 340 | * @mtd: MTD device structure |
| 341 | * @ofs: offset from device start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 342 | * |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 343 | * Check, if the block is bad. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 344 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 345 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 346 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 347 | struct nand_chip *chip = mtd_to_nand(mtd); |
Dinesh Maniyam | ad2868f | 2025-02-27 00:18:28 +0800 | [diff] [blame] | 348 | int first_page, page_offset; |
| 349 | int res; |
| 350 | u8 bad; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 351 | |
Dinesh Maniyam | ad2868f | 2025-02-27 00:18:28 +0800 | [diff] [blame] | 352 | first_page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 353 | page_offset = nand_bbm_get_next_page(chip, 0); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 354 | |
Dinesh Maniyam | ad2868f | 2025-02-27 00:18:28 +0800 | [diff] [blame] | 355 | while (page_offset >= 0) { |
| 356 | res = chip->ecc.read_oob(mtd, chip, first_page + page_offset); |
| 357 | if (res < 0) |
| 358 | return res; |
Thomas Knobloch | 9e2aeaf | 2007-05-05 07:04:42 +0200 | [diff] [blame] | 359 | |
Dinesh Maniyam | ad2868f | 2025-02-27 00:18:28 +0800 | [diff] [blame] | 360 | bad = chip->oob_poi[chip->badblockpos]; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 361 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 362 | if (likely(chip->badblockbits == 8)) |
| 363 | res = bad != 0xFF; |
| 364 | else |
| 365 | res = hweight8(bad) < chip->badblockbits; |
Dinesh Maniyam | ad2868f | 2025-02-27 00:18:28 +0800 | [diff] [blame] | 366 | if (res) |
| 367 | return res; |
| 368 | |
| 369 | page_offset = nand_bbm_get_next_page(chip, page_offset + 1); |
| 370 | } |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 371 | |
Dinesh Maniyam | ad2868f | 2025-02-27 00:18:28 +0800 | [diff] [blame] | 372 | return 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 376 | * 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] | 377 | * @mtd: MTD device structure |
| 378 | * @ofs: offset from device start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 379 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 380 | * This is the default implementation, which can be overridden by a hardware |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 381 | * specific driver. It provides the details for writing a bad block marker to a |
| 382 | * block. |
| 383 | */ |
| 384 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 385 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 386 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 387 | struct mtd_oob_ops ops; |
| 388 | uint8_t buf[2] = { 0, 0 }; |
| 389 | int ret = 0, res, i = 0; |
| 390 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 391 | memset(&ops, 0, sizeof(ops)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 392 | ops.oobbuf = buf; |
| 393 | ops.ooboffs = chip->badblockpos; |
| 394 | if (chip->options & NAND_BUSWIDTH_16) { |
| 395 | ops.ooboffs &= ~0x01; |
| 396 | ops.len = ops.ooblen = 2; |
| 397 | } else { |
| 398 | ops.len = ops.ooblen = 1; |
| 399 | } |
| 400 | ops.mode = MTD_OPS_PLACE_OOB; |
| 401 | |
| 402 | /* Write to first/last page(s) if necessary */ |
| 403 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 404 | ofs += mtd->erasesize - mtd->writesize; |
| 405 | do { |
| 406 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 407 | if (!ret) |
| 408 | ret = res; |
| 409 | |
| 410 | i++; |
| 411 | ofs += mtd->writesize; |
| 412 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 413 | |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * nand_block_markbad_lowlevel - mark a block bad |
| 419 | * @mtd: MTD device structure |
| 420 | * @ofs: offset from device start |
| 421 | * |
| 422 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 423 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
| 424 | * specify how to write bad block markers to OOB (chip->block_markbad). |
| 425 | * |
| 426 | * We try operations in the following order: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 427 | * (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] | 428 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 429 | * NAND_BBT_NO_OOB_BBM is present) |
| 430 | * (3) update the BBT |
| 431 | * 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] | 432 | * procedures, and dump the error in the end. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 433 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 434 | 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] | 435 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 436 | struct nand_chip *chip = mtd_to_nand(mtd); |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 437 | int ret = 0; |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 438 | #ifndef CONFIG_XPL_BUILD |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 439 | int res; |
| 440 | #endif |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 441 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 442 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 443 | struct erase_info einfo; |
| 444 | |
| 445 | /* Attempt erase before marking OOB */ |
| 446 | memset(&einfo, 0, sizeof(einfo)); |
| 447 | einfo.mtd = mtd; |
| 448 | einfo.addr = ofs; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 449 | einfo.len = 1ULL << chip->phys_erase_shift; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 450 | nand_erase_nand(mtd, &einfo, 0); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 451 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 452 | /* Write bad block marker to OOB */ |
| 453 | nand_get_device(mtd, FL_WRITING); |
| 454 | ret = chip->block_markbad(mtd, ofs); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 455 | nand_release_device(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 456 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 457 | |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 458 | #ifndef CONFIG_XPL_BUILD |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 459 | /* Mark block bad in BBT */ |
| 460 | if (chip->bbt) { |
| 461 | res = nand_markbad_bbt(mtd, ofs); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 462 | if (!ret) |
| 463 | ret = res; |
| 464 | } |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 465 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 466 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 467 | if (!ret) |
| 468 | mtd->ecc_stats.badblocks++; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 469 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 470 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 473 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 474 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 475 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 476 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 477 | * Check, if the device is write protected. The function expects, that the |
| 478 | * device is already selected. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 479 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 480 | static int nand_check_wp(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 481 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 482 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 483 | u8 status; |
| 484 | int ret; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 485 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 486 | /* Broken xD cards report WP despite being writable */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 487 | if (chip->options & NAND_BROKEN_XD) |
| 488 | return 0; |
| 489 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 490 | /* Check the WP bit */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 491 | ret = nand_status_op(chip, &status); |
| 492 | if (ret) |
| 493 | return ret; |
| 494 | |
| 495 | return status & NAND_STATUS_WP ? 0 : 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 496 | } |
Markus Klotzbücher | 27eba14 | 2006-03-06 15:04:25 +0100 | [diff] [blame] | 497 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 498 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 499 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 500 | * @mtd: MTD device structure |
| 501 | * @ofs: offset from device start |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 502 | * |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 503 | * Check if the block is marked as reserved. |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 504 | */ |
| 505 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 506 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 507 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 508 | |
| 509 | if (!chip->bbt) |
| 510 | return 0; |
| 511 | /* Return info from the table */ |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 512 | #ifndef CONFIG_XPL_BUILD |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 513 | return nand_isreserved_bbt(mtd, ofs); |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 514 | #else |
| 515 | return 0; |
| 516 | #endif |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /** |
| 520 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 521 | * @mtd: MTD device structure |
| 522 | * @ofs: offset from device start |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 523 | * @allowbbt: 1, if its allowed to access the bbt area |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 524 | * |
| 525 | * Check, if the block is bad. Either by reading the bad block table or |
| 526 | * calling of the scan function. |
| 527 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 528 | 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] | 529 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 530 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 531 | |
Masahiro Yamada | 8d10054 | 2014-12-26 22:20:58 +0900 | [diff] [blame] | 532 | if (!(chip->options & NAND_SKIP_BBTSCAN) && |
| 533 | !(chip->options & NAND_BBT_SCANNED)) { |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 534 | chip->options |= NAND_BBT_SCANNED; |
Masahiro Yamada | 8c6c14a | 2014-12-26 22:20:57 +0900 | [diff] [blame] | 535 | chip->scan_bbt(mtd); |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 536 | } |
| 537 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 538 | if (!chip->bbt) |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 539 | return chip->block_bad(mtd, ofs); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 540 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 541 | /* Return info from the table */ |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 542 | #ifndef CONFIG_XPL_BUILD |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 543 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 544 | #else |
| 545 | return 0; |
| 546 | #endif |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 547 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 548 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 549 | /** |
| 550 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 551 | * @mtd: MTD device structure |
| 552 | * |
| 553 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 554 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 555 | void nand_wait_ready(struct mtd_info *mtd) |
| 556 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 557 | struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 558 | u32 timeo = (CONFIG_SYS_HZ * 400) / 1000; |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 559 | u32 time_start; |
Stefan Roese | a5c312c | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 560 | |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 561 | time_start = get_timer(0); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 562 | /* Wait until command is processed or timeout occurs */ |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 563 | while (get_timer(time_start) < timeo) { |
Stefan Roese | a5c312c | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 564 | if (chip->dev_ready) |
| 565 | if (chip->dev_ready(mtd)) |
| 566 | break; |
| 567 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 568 | |
| 569 | if (!chip->dev_ready(mtd)) |
| 570 | pr_warn("timeout while waiting for chip to become ready\n"); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 571 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 572 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 573 | |
| 574 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 575 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 576 | * @mtd: MTD device structure |
| 577 | * @timeo: Timeout in ms |
| 578 | * |
| 579 | * Wait for status ready (i.e. command done) or timeout. |
| 580 | */ |
| 581 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 582 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 583 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 584 | u32 time_start; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 585 | int ret; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 586 | |
| 587 | timeo = (CONFIG_SYS_HZ * timeo) / 1000; |
| 588 | time_start = get_timer(0); |
| 589 | while (get_timer(time_start) < timeo) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 590 | u8 status; |
| 591 | |
| 592 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 593 | if (ret) |
| 594 | return; |
| 595 | |
| 596 | if (status & NAND_STATUS_READY) |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 597 | break; |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 598 | schedule(); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 599 | } |
| 600 | }; |
| 601 | |
| 602 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 603 | * nand_command - [DEFAULT] Send command to NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 604 | * @mtd: MTD device structure |
| 605 | * @command: the command to be sent |
| 606 | * @column: the column address for this command, -1 if none |
| 607 | * @page_addr: the page address for this command, -1 if none |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 608 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 609 | * 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] | 610 | * (512 Bytes per page). |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 611 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 612 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 613 | int column, int page_addr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 614 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 615 | register struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 616 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 617 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 618 | /* Write out the command to the device */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 619 | if (command == NAND_CMD_SEQIN) { |
| 620 | int readcmd; |
| 621 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 622 | if (column >= mtd->writesize) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 623 | /* OOB area */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 624 | column -= mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 625 | readcmd = NAND_CMD_READOOB; |
| 626 | } else if (column < 256) { |
| 627 | /* First 256 bytes --> READ0 */ |
| 628 | readcmd = NAND_CMD_READ0; |
| 629 | } else { |
| 630 | column -= 256; |
| 631 | readcmd = NAND_CMD_READ1; |
| 632 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 633 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
| 634 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 635 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 636 | chip->cmd_ctrl(mtd, command, ctrl); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 637 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 638 | /* Address cycle, when necessary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 639 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 640 | /* Serially input address */ |
| 641 | if (column != -1) { |
| 642 | /* Adjust columns for 16 bit buswidth */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 643 | if (chip->options & NAND_BUSWIDTH_16 && |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 644 | !nand_opcode_8bits(command)) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 645 | column >>= 1; |
| 646 | chip->cmd_ctrl(mtd, column, ctrl); |
| 647 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 648 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 649 | if (page_addr != -1) { |
| 650 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 651 | ctrl &= ~NAND_CTRL_CHANGE; |
| 652 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 653 | if (chip->options & NAND_ROW_ADDR_3) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 654 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
| 655 | } |
| 656 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 657 | |
| 658 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 659 | * Program and erase have their own busy handlers status and sequential |
| 660 | * in needs no delay |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 661 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 662 | switch (command) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 663 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 664 | case NAND_CMD_PAGEPROG: |
| 665 | case NAND_CMD_ERASE1: |
| 666 | case NAND_CMD_ERASE2: |
| 667 | case NAND_CMD_SEQIN: |
| 668 | case NAND_CMD_STATUS: |
Masahiro Yamada | 7f9baa1 | 2017-09-15 21:44:58 +0900 | [diff] [blame] | 669 | case NAND_CMD_READID: |
Masahiro Yamada | 0cd1018 | 2017-09-15 21:44:59 +0900 | [diff] [blame] | 670 | case NAND_CMD_SET_FEATURES: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 671 | return; |
| 672 | |
| 673 | case NAND_CMD_RESET: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 674 | if (chip->dev_ready) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 675 | break; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 676 | udelay(chip->chip_delay); |
| 677 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 678 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 679 | chip->cmd_ctrl(mtd, |
| 680 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 681 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 682 | nand_wait_status_ready(mtd, 250); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 683 | return; |
| 684 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 685 | /* This applies to read commands */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 686 | default: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 687 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 688 | * If we don't have access to the busy pin, we apply the given |
| 689 | * command delay |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 690 | */ |
| 691 | if (!chip->dev_ready) { |
| 692 | udelay(chip->chip_delay); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 693 | return; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 694 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 695 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 696 | /* |
| 697 | * Apply this short delay always to ensure that we do wait tWB in |
| 698 | * any case on any machine. |
| 699 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 700 | ndelay(100); |
| 701 | |
| 702 | nand_wait_ready(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /** |
| 706 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 707 | * @mtd: MTD device structure |
| 708 | * @command: the command to be sent |
| 709 | * @column: the column address for this command, -1 if none |
| 710 | * @page_addr: the page address for this command, -1 if none |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 711 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 712 | * 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] | 713 | * devices. We don't have the separate regions as we have in the small page |
| 714 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 715 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 716 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 717 | int column, int page_addr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 718 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 719 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 720 | |
| 721 | /* Emulate NAND_CMD_READOOB */ |
| 722 | if (command == NAND_CMD_READOOB) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 723 | column += mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 724 | command = NAND_CMD_READ0; |
| 725 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 726 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 727 | /* Command latch cycle */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 728 | chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 729 | |
| 730 | if (column != -1 || page_addr != -1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 731 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 732 | |
| 733 | /* Serially input address */ |
| 734 | if (column != -1) { |
| 735 | /* Adjust columns for 16 bit buswidth */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 736 | if (chip->options & NAND_BUSWIDTH_16 && |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 737 | !nand_opcode_8bits(command)) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 738 | column >>= 1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 739 | chip->cmd_ctrl(mtd, column, ctrl); |
| 740 | ctrl &= ~NAND_CTRL_CHANGE; |
| 741 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 742 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 743 | if (page_addr != -1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 744 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 745 | chip->cmd_ctrl(mtd, page_addr >> 8, |
| 746 | NAND_NCE | NAND_ALE); |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 747 | if (chip->options & NAND_ROW_ADDR_3) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 748 | chip->cmd_ctrl(mtd, page_addr >> 16, |
| 749 | NAND_NCE | NAND_ALE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 750 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 751 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 752 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 753 | |
| 754 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 755 | * Program and erase have their own busy handlers status, sequential |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 756 | * in and status need no delay. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 757 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 758 | switch (command) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 759 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 760 | case NAND_CMD_CACHEDPROG: |
| 761 | case NAND_CMD_PAGEPROG: |
| 762 | case NAND_CMD_ERASE1: |
| 763 | case NAND_CMD_ERASE2: |
| 764 | case NAND_CMD_SEQIN: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 765 | case NAND_CMD_RNDIN: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 766 | case NAND_CMD_STATUS: |
Masahiro Yamada | 7f9baa1 | 2017-09-15 21:44:58 +0900 | [diff] [blame] | 767 | case NAND_CMD_READID: |
Masahiro Yamada | 0cd1018 | 2017-09-15 21:44:59 +0900 | [diff] [blame] | 768 | case NAND_CMD_SET_FEATURES: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 769 | return; |
| 770 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 771 | case NAND_CMD_RESET: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 772 | if (chip->dev_ready) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 773 | break; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 774 | udelay(chip->chip_delay); |
| 775 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 776 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 777 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 778 | NAND_NCE | NAND_CTRL_CHANGE); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 779 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 780 | nand_wait_status_ready(mtd, 250); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 781 | return; |
| 782 | |
| 783 | case NAND_CMD_RNDOUT: |
| 784 | /* No ready / busy check necessary */ |
| 785 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 786 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 787 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 788 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 789 | return; |
| 790 | |
| 791 | case NAND_CMD_READ0: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 792 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 793 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 794 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 795 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 796 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 797 | /* This applies to read commands */ |
Andre Przywara | a50e5c6 | 2025-03-27 15:33:10 +0000 | [diff] [blame] | 798 | fallthrough; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 799 | default: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 800 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 801 | * 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] | 802 | * command delay. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 803 | */ |
| 804 | if (!chip->dev_ready) { |
| 805 | udelay(chip->chip_delay); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 806 | return; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 807 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 808 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 809 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 810 | /* |
| 811 | * Apply this short delay always to ensure that we do wait tWB in |
| 812 | * any case on any machine. |
| 813 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 814 | ndelay(100); |
| 815 | |
| 816 | nand_wait_ready(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 820 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 821 | * @chip: the nand chip descriptor |
| 822 | * @mtd: MTD device structure |
| 823 | * @new_state: the state which is requested |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 824 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 825 | * Used when in panic, no locks are taken. |
| 826 | */ |
| 827 | static void panic_nand_get_device(struct nand_chip *chip, |
| 828 | struct mtd_info *mtd, int new_state) |
| 829 | { |
| 830 | /* Hardware controller shared among independent devices */ |
| 831 | chip->controller->active = chip; |
| 832 | chip->state = new_state; |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * nand_get_device - [GENERIC] Get chip for selected access |
| 837 | * @mtd: MTD device structure |
| 838 | * @new_state: the state which is requested |
| 839 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 840 | * Get the device and lock it for exclusive access |
| 841 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 842 | static int |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 843 | nand_get_device(struct mtd_info *mtd, int new_state) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 844 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 845 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 846 | chip->state = new_state; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 847 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /** |
| 851 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 852 | * @mtd: MTD device structure |
| 853 | * @chip: NAND chip structure |
| 854 | * @timeo: timeout |
| 855 | * |
| 856 | * Wait for command done. This is a helper function for nand_wait used when |
| 857 | * we are in interrupt context. May happen when in panic and trying to write |
| 858 | * an oops through mtdoops. |
| 859 | */ |
| 860 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 861 | unsigned long timeo) |
| 862 | { |
| 863 | int i; |
| 864 | for (i = 0; i < timeo; i++) { |
| 865 | if (chip->dev_ready) { |
| 866 | if (chip->dev_ready(mtd)) |
| 867 | break; |
| 868 | } else { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 869 | int ret; |
| 870 | u8 status; |
| 871 | |
| 872 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 873 | true); |
| 874 | if (ret) |
| 875 | return; |
| 876 | |
| 877 | if (status & NAND_STATUS_READY) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 878 | break; |
| 879 | } |
| 880 | mdelay(1); |
| 881 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 882 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 883 | |
| 884 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 885 | * nand_wait - [DEFAULT] wait until the command is done |
| 886 | * @mtd: MTD device structure |
| 887 | * @chip: NAND chip structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 888 | * |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 889 | * Wait for command done. This applies to erase and program only. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 890 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 891 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 892 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 893 | unsigned long timeo = 400; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 894 | u8 status; |
| 895 | int ret; |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 896 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 897 | led_trigger_event(nand_led_trigger, LED_FULL); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 898 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 899 | /* |
| 900 | * Apply this short delay always to ensure that we do wait tWB in any |
| 901 | * case on any machine. |
| 902 | */ |
| 903 | ndelay(100); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 904 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 905 | ret = nand_status_op(chip, NULL); |
| 906 | if (ret) |
| 907 | return ret; |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 908 | |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 909 | u32 timer = (CONFIG_SYS_HZ * timeo) / 1000; |
| 910 | u32 time_start; |
Wolfgang Denk | 9d328a6 | 2021-09-27 17:42:38 +0200 | [diff] [blame] | 911 | |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 912 | time_start = get_timer(0); |
| 913 | while (get_timer(time_start) < timer) { |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 914 | if (chip->dev_ready) { |
| 915 | if (chip->dev_ready(mtd)) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 916 | break; |
| 917 | } else { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 918 | ret = nand_read_data_op(chip, &status, |
| 919 | sizeof(status), true); |
| 920 | if (ret) |
| 921 | return ret; |
| 922 | |
| 923 | if (status & NAND_STATUS_READY) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 924 | break; |
| 925 | } |
| 926 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 927 | led_trigger_event(nand_led_trigger, LED_OFF); |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 928 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 929 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 930 | if (ret) |
| 931 | return ret; |
| 932 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 933 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 934 | WARN_ON(!(status & NAND_STATUS_READY)); |
| 935 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 936 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 937 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 938 | /** |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 939 | * nand_reset_data_interface - Reset data interface and timings |
| 940 | * @chip: The NAND chip |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 941 | * @chipnr: Internal die id |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 942 | * |
| 943 | * Reset the Data interface and timings to ONFI mode 0. |
| 944 | * |
| 945 | * Returns 0 for success or negative error code otherwise. |
| 946 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 947 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 948 | { |
| 949 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 950 | const struct nand_data_interface *conf; |
| 951 | int ret; |
| 952 | |
| 953 | if (!chip->setup_data_interface) |
| 954 | return 0; |
| 955 | |
| 956 | /* |
| 957 | * The ONFI specification says: |
| 958 | * " |
| 959 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 960 | * interface, the host shall use the Reset (FFh) command |
| 961 | * using SDR timing mode 0. A device in any timing mode is |
| 962 | * required to recognize Reset (FFh) command issued in SDR |
| 963 | * timing mode 0. |
| 964 | * " |
| 965 | * |
| 966 | * Configure the data interface in SDR mode and set the |
| 967 | * timings to timing mode 0. |
| 968 | */ |
| 969 | |
| 970 | conf = nand_get_default_data_interface(); |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 971 | ret = chip->setup_data_interface(mtd, chipnr, conf); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 972 | if (ret) |
| 973 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 974 | |
| 975 | return ret; |
| 976 | } |
| 977 | |
Kory Maincent | 0cda0cb | 2022-06-22 11:11:45 +0200 | [diff] [blame] | 978 | static int nand_onfi_set_timings(struct mtd_info *mtd, struct nand_chip *chip) |
| 979 | { |
| 980 | if (!chip->onfi_version || |
| 981 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 982 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
| 983 | return 0; |
| 984 | |
| 985 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 986 | chip->onfi_timing_mode_default, |
| 987 | }; |
| 988 | |
| 989 | return chip->onfi_set_features(mtd, chip, |
| 990 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 991 | tmode_param); |
| 992 | } |
| 993 | |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 994 | /** |
| 995 | * nand_setup_data_interface - Setup the best data interface and timings |
| 996 | * @chip: The NAND chip |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 997 | * @chipnr: Internal die id |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 998 | * |
| 999 | * Find and configure the best data interface and NAND timings supported by |
| 1000 | * the chip and the driver. |
| 1001 | * First tries to retrieve supported timing modes from ONFI information, |
| 1002 | * and if the NAND chip does not support ONFI, relies on the |
| 1003 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 1004 | * |
| 1005 | * Returns 0 for success or negative error code otherwise. |
| 1006 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1007 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1008 | { |
| 1009 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1010 | int ret; |
| 1011 | |
| 1012 | if (!chip->setup_data_interface || !chip->data_interface) |
| 1013 | return 0; |
| 1014 | |
| 1015 | /* |
| 1016 | * Ensure the timing mode has been changed on the chip side |
| 1017 | * before changing timings on the controller side. |
| 1018 | */ |
Kory Maincent | 0cda0cb | 2022-06-22 11:11:45 +0200 | [diff] [blame] | 1019 | ret = nand_onfi_set_timings(mtd, chip); |
| 1020 | if (ret) |
| 1021 | goto err; |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1022 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1023 | ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1024 | err: |
| 1025 | return ret; |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * nand_init_data_interface - find the best data interface and timings |
| 1030 | * @chip: The NAND chip |
| 1031 | * |
| 1032 | * Find the best data interface and NAND timings supported by the chip |
| 1033 | * and the driver. |
| 1034 | * First tries to retrieve supported timing modes from ONFI information, |
| 1035 | * and if the NAND chip does not support ONFI, relies on the |
| 1036 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1037 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1038 | * available. |
| 1039 | * |
| 1040 | * Returns 0 for success or negative error code otherwise. |
| 1041 | */ |
| 1042 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1043 | { |
| 1044 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1045 | int modes, mode, ret; |
| 1046 | |
| 1047 | if (!chip->setup_data_interface) |
| 1048 | return 0; |
| 1049 | |
| 1050 | /* |
| 1051 | * First try to identify the best timings from ONFI parameters and |
| 1052 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1053 | * timing mode. |
| 1054 | */ |
| 1055 | modes = onfi_get_async_timing_mode(chip); |
| 1056 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1057 | if (!chip->onfi_timing_mode_default) |
| 1058 | return 0; |
| 1059 | |
| 1060 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1061 | } |
| 1062 | |
| 1063 | chip->data_interface = kzalloc(sizeof(*chip->data_interface), |
| 1064 | GFP_KERNEL); |
| 1065 | if (!chip->data_interface) |
| 1066 | return -ENOMEM; |
| 1067 | |
| 1068 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
| 1069 | ret = onfi_init_data_interface(chip, chip->data_interface, |
| 1070 | NAND_SDR_IFACE, mode); |
| 1071 | if (ret) |
| 1072 | continue; |
| 1073 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1074 | /* Pass -1 to only */ |
| 1075 | ret = chip->setup_data_interface(mtd, |
| 1076 | NAND_DATA_IFACE_CHECK_ONLY, |
| 1077 | chip->data_interface); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1078 | if (!ret) { |
| 1079 | chip->onfi_timing_mode_default = mode; |
| 1080 | break; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | static void __maybe_unused nand_release_data_interface(struct nand_chip *chip) |
| 1088 | { |
| 1089 | kfree(chip->data_interface); |
| 1090 | } |
| 1091 | |
| 1092 | /** |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1093 | * nand_read_page_op - Do a READ PAGE operation |
| 1094 | * @chip: The NAND chip |
| 1095 | * @page: page to read |
| 1096 | * @offset_in_page: offset within the page |
| 1097 | * @buf: buffer used to store the data |
| 1098 | * @len: length of the buffer |
| 1099 | * |
| 1100 | * This function issues a READ PAGE operation. |
| 1101 | * This function does not select/unselect the CS line. |
| 1102 | * |
| 1103 | * Returns 0 on success, a negative error code otherwise. |
| 1104 | */ |
| 1105 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1106 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 1107 | { |
| 1108 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1109 | |
| 1110 | if (len && !buf) |
| 1111 | return -EINVAL; |
| 1112 | |
| 1113 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1114 | return -EINVAL; |
| 1115 | |
| 1116 | chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page); |
| 1117 | if (len) |
| 1118 | chip->read_buf(mtd, buf, len); |
| 1119 | |
| 1120 | return 0; |
| 1121 | } |
| 1122 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 1123 | |
| 1124 | /** |
| 1125 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 1126 | * @chip: The NAND chip |
| 1127 | * @page: parameter page to read |
| 1128 | * @buf: buffer used to store the data |
| 1129 | * @len: length of the buffer |
| 1130 | * |
| 1131 | * This function issues a READ PARAMETER PAGE operation. |
| 1132 | * This function does not select/unselect the CS line. |
| 1133 | * |
| 1134 | * Returns 0 on success, a negative error code otherwise. |
| 1135 | */ |
| 1136 | static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1137 | unsigned int len) |
| 1138 | { |
| 1139 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1140 | unsigned int i; |
| 1141 | u8 *p = buf; |
| 1142 | |
| 1143 | if (len && !buf) |
| 1144 | return -EINVAL; |
| 1145 | |
| 1146 | chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1); |
| 1147 | for (i = 0; i < len; i++) |
| 1148 | p[i] = chip->read_byte(mtd); |
| 1149 | |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1155 | * @chip: The NAND chip |
| 1156 | * @offset_in_page: offset within the page |
| 1157 | * @buf: buffer used to store the data |
| 1158 | * @len: length of the buffer |
| 1159 | * @force_8bit: force 8-bit bus access |
| 1160 | * |
| 1161 | * This function issues a CHANGE READ COLUMN operation. |
| 1162 | * This function does not select/unselect the CS line. |
| 1163 | * |
| 1164 | * Returns 0 on success, a negative error code otherwise. |
| 1165 | */ |
| 1166 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1167 | unsigned int offset_in_page, void *buf, |
| 1168 | unsigned int len, bool force_8bit) |
| 1169 | { |
| 1170 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1171 | |
| 1172 | if (len && !buf) |
| 1173 | return -EINVAL; |
| 1174 | |
| 1175 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1176 | return -EINVAL; |
| 1177 | |
| 1178 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1); |
| 1179 | if (len) |
| 1180 | chip->read_buf(mtd, buf, len); |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1185 | |
| 1186 | /** |
| 1187 | * nand_read_oob_op - Do a READ OOB operation |
| 1188 | * @chip: The NAND chip |
| 1189 | * @page: page to read |
| 1190 | * @offset_in_oob: offset within the OOB area |
| 1191 | * @buf: buffer used to store the data |
| 1192 | * @len: length of the buffer |
| 1193 | * |
| 1194 | * This function issues a READ OOB operation. |
| 1195 | * This function does not select/unselect the CS line. |
| 1196 | * |
| 1197 | * Returns 0 on success, a negative error code otherwise. |
| 1198 | */ |
| 1199 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1200 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1201 | { |
| 1202 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1203 | |
| 1204 | if (len && !buf) |
| 1205 | return -EINVAL; |
| 1206 | |
| 1207 | if (offset_in_oob + len > mtd->oobsize) |
| 1208 | return -EINVAL; |
| 1209 | |
| 1210 | chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page); |
| 1211 | if (len) |
| 1212 | chip->read_buf(mtd, buf, len); |
| 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1217 | |
| 1218 | /** |
| 1219 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1220 | * @chip: The NAND chip |
| 1221 | * @page: page to write |
| 1222 | * @offset_in_page: offset within the page |
| 1223 | * @buf: buffer containing the data to write to the page |
| 1224 | * @len: length of the buffer |
| 1225 | * |
| 1226 | * This function issues the first half of a PROG PAGE operation. |
| 1227 | * This function does not select/unselect the CS line. |
| 1228 | * |
| 1229 | * Returns 0 on success, a negative error code otherwise. |
| 1230 | */ |
| 1231 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1232 | unsigned int offset_in_page, const void *buf, |
| 1233 | unsigned int len) |
| 1234 | { |
| 1235 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1236 | |
| 1237 | if (len && !buf) |
| 1238 | return -EINVAL; |
| 1239 | |
| 1240 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1241 | return -EINVAL; |
| 1242 | |
| 1243 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1244 | |
| 1245 | if (buf) |
| 1246 | chip->write_buf(mtd, buf, len); |
| 1247 | |
| 1248 | return 0; |
| 1249 | } |
| 1250 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1251 | |
| 1252 | /** |
| 1253 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1254 | * @chip: The NAND chip |
| 1255 | * |
| 1256 | * This function issues the second half of a PROG PAGE operation. |
| 1257 | * This function does not select/unselect the CS line. |
| 1258 | * |
| 1259 | * Returns 0 on success, a negative error code otherwise. |
| 1260 | */ |
| 1261 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1262 | { |
| 1263 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1264 | int status; |
| 1265 | |
| 1266 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1267 | |
| 1268 | status = chip->waitfunc(mtd, chip); |
| 1269 | if (status & NAND_STATUS_FAIL) |
| 1270 | return -EIO; |
| 1271 | |
| 1272 | return 0; |
| 1273 | } |
| 1274 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1275 | |
| 1276 | /** |
| 1277 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1278 | * @chip: The NAND chip |
| 1279 | * @page: page to write |
| 1280 | * @offset_in_page: offset within the page |
| 1281 | * @buf: buffer containing the data to write to the page |
| 1282 | * @len: length of the buffer |
| 1283 | * |
| 1284 | * This function issues a full PROG PAGE operation. |
| 1285 | * This function does not select/unselect the CS line. |
| 1286 | * |
| 1287 | * Returns 0 on success, a negative error code otherwise. |
| 1288 | */ |
| 1289 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1290 | unsigned int offset_in_page, const void *buf, |
| 1291 | unsigned int len) |
| 1292 | { |
| 1293 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1294 | int status; |
| 1295 | |
| 1296 | if (!len || !buf) |
| 1297 | return -EINVAL; |
| 1298 | |
| 1299 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1300 | return -EINVAL; |
| 1301 | |
| 1302 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1303 | chip->write_buf(mtd, buf, len); |
| 1304 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1305 | |
| 1306 | status = chip->waitfunc(mtd, chip); |
| 1307 | if (status & NAND_STATUS_FAIL) |
| 1308 | return -EIO; |
| 1309 | |
| 1310 | return 0; |
| 1311 | } |
| 1312 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1313 | |
| 1314 | /** |
| 1315 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1316 | * @chip: The NAND chip |
| 1317 | * @offset_in_page: offset within the page |
| 1318 | * @buf: buffer containing the data to send to the NAND |
| 1319 | * @len: length of the buffer |
| 1320 | * @force_8bit: force 8-bit bus access |
| 1321 | * |
| 1322 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1323 | * This function does not select/unselect the CS line. |
| 1324 | * |
| 1325 | * Returns 0 on success, a negative error code otherwise. |
| 1326 | */ |
| 1327 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1328 | unsigned int offset_in_page, |
| 1329 | const void *buf, unsigned int len, |
| 1330 | bool force_8bit) |
| 1331 | { |
| 1332 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1333 | |
| 1334 | if (len && !buf) |
| 1335 | return -EINVAL; |
| 1336 | |
| 1337 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1338 | return -EINVAL; |
| 1339 | |
| 1340 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1); |
| 1341 | if (len) |
| 1342 | chip->write_buf(mtd, buf, len); |
| 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1347 | |
| 1348 | /** |
| 1349 | * nand_readid_op - Do a READID operation |
| 1350 | * @chip: The NAND chip |
| 1351 | * @addr: address cycle to pass after the READID command |
| 1352 | * @buf: buffer used to store the ID |
| 1353 | * @len: length of the buffer |
| 1354 | * |
| 1355 | * This function sends a READID command and reads back the ID returned by the |
| 1356 | * NAND. |
| 1357 | * This function does not select/unselect the CS line. |
| 1358 | * |
| 1359 | * Returns 0 on success, a negative error code otherwise. |
| 1360 | */ |
| 1361 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1362 | unsigned int len) |
| 1363 | { |
| 1364 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1365 | unsigned int i; |
| 1366 | u8 *id = buf; |
| 1367 | |
| 1368 | if (len && !buf) |
| 1369 | return -EINVAL; |
| 1370 | |
| 1371 | chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1); |
| 1372 | |
| 1373 | for (i = 0; i < len; i++) |
| 1374 | id[i] = chip->read_byte(mtd); |
| 1375 | |
| 1376 | return 0; |
| 1377 | } |
| 1378 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1379 | |
| 1380 | /** |
| 1381 | * nand_status_op - Do a STATUS operation |
| 1382 | * @chip: The NAND chip |
| 1383 | * @status: out variable to store the NAND status |
| 1384 | * |
| 1385 | * This function sends a STATUS command and reads back the status returned by |
| 1386 | * the NAND. |
| 1387 | * This function does not select/unselect the CS line. |
| 1388 | * |
| 1389 | * Returns 0 on success, a negative error code otherwise. |
| 1390 | */ |
| 1391 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1392 | { |
| 1393 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1394 | |
| 1395 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 1396 | if (status) |
| 1397 | *status = chip->read_byte(mtd); |
| 1398 | |
| 1399 | return 0; |
| 1400 | } |
| 1401 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 1402 | |
| 1403 | /** |
| 1404 | * nand_exit_status_op - Exit a STATUS operation |
| 1405 | * @chip: The NAND chip |
| 1406 | * |
| 1407 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 1408 | * command to avoid reading only the status until a new read command is sent. |
| 1409 | * |
| 1410 | * This function does not select/unselect the CS line. |
| 1411 | * |
| 1412 | * Returns 0 on success, a negative error code otherwise. |
| 1413 | */ |
| 1414 | int nand_exit_status_op(struct nand_chip *chip) |
| 1415 | { |
| 1416 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1417 | |
| 1418 | chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1); |
| 1419 | |
| 1420 | return 0; |
| 1421 | } |
| 1422 | EXPORT_SYMBOL_GPL(nand_exit_status_op); |
| 1423 | |
| 1424 | /** |
| 1425 | * nand_erase_op - Do an erase operation |
| 1426 | * @chip: The NAND chip |
| 1427 | * @eraseblock: block to erase |
| 1428 | * |
| 1429 | * This function sends an ERASE command and waits for the NAND to be ready |
| 1430 | * before returning. |
| 1431 | * This function does not select/unselect the CS line. |
| 1432 | * |
| 1433 | * Returns 0 on success, a negative error code otherwise. |
| 1434 | */ |
| 1435 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 1436 | { |
| 1437 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1438 | unsigned int page = eraseblock << |
| 1439 | (chip->phys_erase_shift - chip->page_shift); |
| 1440 | int status; |
| 1441 | |
| 1442 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 1443 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
| 1444 | |
| 1445 | status = chip->waitfunc(mtd, chip); |
| 1446 | if (status < 0) |
| 1447 | return status; |
| 1448 | |
| 1449 | if (status & NAND_STATUS_FAIL) |
| 1450 | return -EIO; |
| 1451 | |
| 1452 | return 0; |
| 1453 | } |
| 1454 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 1455 | |
| 1456 | /** |
| 1457 | * nand_set_features_op - Do a SET FEATURES operation |
| 1458 | * @chip: The NAND chip |
| 1459 | * @feature: feature id |
| 1460 | * @data: 4 bytes of data |
| 1461 | * |
| 1462 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 1463 | * ready before returning. |
| 1464 | * This function does not select/unselect the CS line. |
| 1465 | * |
| 1466 | * Returns 0 on success, a negative error code otherwise. |
| 1467 | */ |
| 1468 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 1469 | const void *data) |
| 1470 | { |
| 1471 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1472 | const u8 *params = data; |
| 1473 | int i, status; |
| 1474 | |
| 1475 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1); |
| 1476 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 1477 | chip->write_byte(mtd, params[i]); |
| 1478 | |
| 1479 | status = chip->waitfunc(mtd, chip); |
| 1480 | if (status & NAND_STATUS_FAIL) |
| 1481 | return -EIO; |
| 1482 | |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * nand_get_features_op - Do a GET FEATURES operation |
| 1488 | * @chip: The NAND chip |
| 1489 | * @feature: feature id |
| 1490 | * @data: 4 bytes of data |
| 1491 | * |
| 1492 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 1493 | * ready before returning. |
| 1494 | * This function does not select/unselect the CS line. |
| 1495 | * |
| 1496 | * Returns 0 on success, a negative error code otherwise. |
| 1497 | */ |
| 1498 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 1499 | void *data) |
| 1500 | { |
| 1501 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1502 | u8 *params = data; |
| 1503 | int i; |
| 1504 | |
| 1505 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1); |
| 1506 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 1507 | params[i] = chip->read_byte(mtd); |
| 1508 | |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | /** |
| 1513 | * nand_reset_op - Do a reset operation |
| 1514 | * @chip: The NAND chip |
| 1515 | * |
| 1516 | * This function sends a RESET command and waits for the NAND to be ready |
| 1517 | * before returning. |
| 1518 | * This function does not select/unselect the CS line. |
| 1519 | * |
| 1520 | * Returns 0 on success, a negative error code otherwise. |
| 1521 | */ |
| 1522 | int nand_reset_op(struct nand_chip *chip) |
| 1523 | { |
| 1524 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1525 | |
| 1526 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 1527 | |
| 1528 | return 0; |
| 1529 | } |
| 1530 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 1531 | |
| 1532 | /** |
| 1533 | * nand_read_data_op - Read data from the NAND |
| 1534 | * @chip: The NAND chip |
| 1535 | * @buf: buffer used to store the data |
| 1536 | * @len: length of the buffer |
| 1537 | * @force_8bit: force 8-bit bus access |
| 1538 | * |
| 1539 | * This function does a raw data read on the bus. Usually used after launching |
| 1540 | * another NAND operation like nand_read_page_op(). |
| 1541 | * This function does not select/unselect the CS line. |
| 1542 | * |
| 1543 | * Returns 0 on success, a negative error code otherwise. |
| 1544 | */ |
| 1545 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 1546 | bool force_8bit) |
| 1547 | { |
| 1548 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1549 | |
| 1550 | if (!len || !buf) |
| 1551 | return -EINVAL; |
| 1552 | |
| 1553 | if (force_8bit) { |
| 1554 | u8 *p = buf; |
| 1555 | unsigned int i; |
| 1556 | |
| 1557 | for (i = 0; i < len; i++) |
| 1558 | p[i] = chip->read_byte(mtd); |
| 1559 | } else { |
| 1560 | chip->read_buf(mtd, buf, len); |
| 1561 | } |
| 1562 | |
| 1563 | return 0; |
| 1564 | } |
| 1565 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 1566 | |
| 1567 | /** |
| 1568 | * nand_write_data_op - Write data from the NAND |
| 1569 | * @chip: The NAND chip |
| 1570 | * @buf: buffer containing the data to send on the bus |
| 1571 | * @len: length of the buffer |
| 1572 | * @force_8bit: force 8-bit bus access |
| 1573 | * |
| 1574 | * This function does a raw data write on the bus. Usually used after launching |
| 1575 | * another NAND operation like nand_write_page_begin_op(). |
| 1576 | * This function does not select/unselect the CS line. |
| 1577 | * |
| 1578 | * Returns 0 on success, a negative error code otherwise. |
| 1579 | */ |
| 1580 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 1581 | unsigned int len, bool force_8bit) |
| 1582 | { |
| 1583 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1584 | |
| 1585 | if (!len || !buf) |
| 1586 | return -EINVAL; |
| 1587 | |
| 1588 | if (force_8bit) { |
| 1589 | const u8 *p = buf; |
| 1590 | unsigned int i; |
| 1591 | |
| 1592 | for (i = 0; i < len; i++) |
| 1593 | chip->write_byte(mtd, p[i]); |
| 1594 | } else { |
| 1595 | chip->write_buf(mtd, buf, len); |
| 1596 | } |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 1601 | |
| 1602 | /** |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1603 | * nand_reset - Reset and initialize a NAND device |
| 1604 | * @chip: The NAND chip |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1605 | * @chipnr: Internal die id |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1606 | * |
| 1607 | * Returns 0 for success or negative error code otherwise |
| 1608 | */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1609 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1610 | { |
| 1611 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1612 | int ret; |
| 1613 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1614 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1615 | if (ret) |
| 1616 | return ret; |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1617 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1618 | /* |
| 1619 | * The CS line has to be released before we can apply the new NAND |
| 1620 | * interface settings, hence this weird ->select_chip() dance. |
| 1621 | */ |
| 1622 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1623 | ret = nand_reset_op(chip); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1624 | chip->select_chip(mtd, -1); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1625 | if (ret) |
| 1626 | return ret; |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1627 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1628 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1629 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1630 | chip->select_chip(mtd, -1); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1631 | if (ret) |
| 1632 | return ret; |
| 1633 | |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | /** |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1638 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 1639 | * @buf: buffer to test |
| 1640 | * @len: buffer length |
| 1641 | * @bitflips_threshold: maximum number of bitflips |
| 1642 | * |
| 1643 | * Check if a buffer contains only 0xff, which means the underlying region |
| 1644 | * has been erased and is ready to be programmed. |
| 1645 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1646 | * considering the region is not erased. |
| 1647 | * Note: The logic of this function has been extracted from the memweight |
| 1648 | * implementation, except that nand_check_erased_buf function exit before |
| 1649 | * testing the whole buffer if the number of bitflips exceed the |
| 1650 | * bitflips_threshold value. |
| 1651 | * |
| 1652 | * Returns a positive number of bitflips less than or equal to |
| 1653 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1654 | * threshold. |
| 1655 | */ |
| 1656 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 1657 | { |
| 1658 | const unsigned char *bitmap = buf; |
| 1659 | int bitflips = 0; |
| 1660 | int weight; |
| 1661 | |
| 1662 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 1663 | len--, bitmap++) { |
| 1664 | weight = hweight8(*bitmap); |
| 1665 | bitflips += BITS_PER_BYTE - weight; |
| 1666 | if (unlikely(bitflips > bitflips_threshold)) |
| 1667 | return -EBADMSG; |
| 1668 | } |
| 1669 | |
| 1670 | for (; len >= 4; len -= 4, bitmap += 4) { |
| 1671 | weight = hweight32(*((u32 *)bitmap)); |
| 1672 | bitflips += 32 - weight; |
| 1673 | if (unlikely(bitflips > bitflips_threshold)) |
| 1674 | return -EBADMSG; |
| 1675 | } |
| 1676 | |
| 1677 | for (; len > 0; len--, bitmap++) { |
| 1678 | weight = hweight8(*bitmap); |
| 1679 | bitflips += BITS_PER_BYTE - weight; |
| 1680 | if (unlikely(bitflips > bitflips_threshold)) |
| 1681 | return -EBADMSG; |
| 1682 | } |
| 1683 | |
| 1684 | return bitflips; |
| 1685 | } |
| 1686 | |
| 1687 | /** |
| 1688 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 1689 | * 0xff data |
| 1690 | * @data: data buffer to test |
| 1691 | * @datalen: data length |
| 1692 | * @ecc: ECC buffer |
| 1693 | * @ecclen: ECC length |
| 1694 | * @extraoob: extra OOB buffer |
| 1695 | * @extraooblen: extra OOB length |
| 1696 | * @bitflips_threshold: maximum number of bitflips |
| 1697 | * |
| 1698 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 1699 | * 0xff pattern, which means the underlying region has been erased and is |
| 1700 | * ready to be programmed. |
| 1701 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1702 | * considering the region as not erased. |
| 1703 | * |
| 1704 | * Note: |
| 1705 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 1706 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 1707 | * report the number of errors per chunk, and the NAND core infrastructure |
| 1708 | * expect you to return the maximum number of bitflips for the whole page. |
| 1709 | * This is why you should always use this function on a single chunk and |
| 1710 | * not on the whole page. After checking each chunk you should update your |
| 1711 | * max_bitflips value accordingly. |
| 1712 | * 2/ When checking for bitflips in erased pages you should not only check |
| 1713 | * the payload data but also their associated ECC data, because a user might |
| 1714 | * have programmed almost all bits to 1 but a few. In this case, we |
| 1715 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 1716 | * this case. |
| 1717 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 1718 | * data are protected by the ECC engine. |
| 1719 | * It could also be used if you support subpages and want to attach some |
| 1720 | * extra OOB data to an ECC chunk. |
| 1721 | * |
| 1722 | * Returns a positive number of bitflips less than or equal to |
| 1723 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1724 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 1725 | */ |
| 1726 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 1727 | void *ecc, int ecclen, |
| 1728 | void *extraoob, int extraooblen, |
| 1729 | int bitflips_threshold) |
| 1730 | { |
| 1731 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 1732 | |
| 1733 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 1734 | bitflips_threshold); |
| 1735 | if (data_bitflips < 0) |
| 1736 | return data_bitflips; |
| 1737 | |
| 1738 | bitflips_threshold -= data_bitflips; |
| 1739 | |
| 1740 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 1741 | if (ecc_bitflips < 0) |
| 1742 | return ecc_bitflips; |
| 1743 | |
| 1744 | bitflips_threshold -= ecc_bitflips; |
| 1745 | |
| 1746 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 1747 | bitflips_threshold); |
| 1748 | if (extraoob_bitflips < 0) |
| 1749 | return extraoob_bitflips; |
| 1750 | |
| 1751 | if (data_bitflips) |
| 1752 | memset(data, 0xff, datalen); |
| 1753 | |
| 1754 | if (ecc_bitflips) |
| 1755 | memset(ecc, 0xff, ecclen); |
| 1756 | |
| 1757 | if (extraoob_bitflips) |
| 1758 | memset(extraoob, 0xff, extraooblen); |
| 1759 | |
| 1760 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 1761 | } |
| 1762 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1763 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1764 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1765 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
| 1766 | * @mtd: mtd info structure |
| 1767 | * @chip: nand chip info structure |
| 1768 | * @buf: buffer to store read data |
| 1769 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1770 | * @page: page number to read |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1771 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1772 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1773 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1774 | 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] | 1775 | uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1776 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1777 | int ret; |
| 1778 | |
| 1779 | ret = nand_read_data_op(chip, buf, mtd->writesize, false); |
| 1780 | if (ret) |
| 1781 | return ret; |
| 1782 | |
| 1783 | if (oob_required) { |
| 1784 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 1785 | false); |
| 1786 | if (ret) |
| 1787 | return ret; |
| 1788 | } |
| 1789 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1790 | return 0; |
| 1791 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1792 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1793 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1794 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
| 1795 | * @mtd: mtd info structure |
| 1796 | * @chip: nand chip info structure |
| 1797 | * @buf: buffer to store read data |
| 1798 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1799 | * @page: page number to read |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1800 | * |
| 1801 | * We need a special oob layout and handling even when OOB isn't used. |
| 1802 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1803 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1804 | struct nand_chip *chip, uint8_t *buf, |
| 1805 | int oob_required, int page) |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1806 | { |
| 1807 | int eccsize = chip->ecc.size; |
| 1808 | int eccbytes = chip->ecc.bytes; |
| 1809 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1810 | int steps, size, ret; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1811 | |
| 1812 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1813 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 1814 | if (ret) |
| 1815 | return ret; |
| 1816 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1817 | buf += eccsize; |
| 1818 | |
| 1819 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1820 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 1821 | false); |
| 1822 | if (ret) |
| 1823 | return ret; |
| 1824 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1825 | oob += chip->ecc.prepad; |
| 1826 | } |
| 1827 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1828 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 1829 | if (ret) |
| 1830 | return ret; |
| 1831 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1832 | oob += eccbytes; |
| 1833 | |
| 1834 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1835 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 1836 | false); |
| 1837 | if (ret) |
| 1838 | return ret; |
| 1839 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1840 | oob += chip->ecc.postpad; |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1845 | if (size) { |
| 1846 | ret = nand_read_data_op(chip, oob, size, false); |
| 1847 | if (ret) |
| 1848 | return ret; |
| 1849 | } |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1850 | |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
| 1854 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1855 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
| 1856 | * @mtd: mtd info structure |
| 1857 | * @chip: nand chip info structure |
| 1858 | * @buf: buffer to store read data |
| 1859 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1860 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1861 | */ |
| 1862 | 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] | 1863 | uint8_t *buf, int oob_required, int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1864 | { |
| 1865 | int i, eccsize = chip->ecc.size; |
| 1866 | int eccbytes = chip->ecc.bytes; |
| 1867 | int eccsteps = chip->ecc.steps; |
| 1868 | uint8_t *p = buf; |
| 1869 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1870 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 1871 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1872 | unsigned int max_bitflips = 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1873 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1874 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1875 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1876 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1877 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1878 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1879 | for (i = 0; i < chip->ecc.total; i++) |
| 1880 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1881 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1882 | eccsteps = chip->ecc.steps; |
| 1883 | p = buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1884 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1885 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1886 | int stat; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1887 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1888 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1889 | if (stat < 0) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1890 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1891 | } else { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1892 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1893 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1894 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1895 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1896 | return max_bitflips; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1897 | } |
| 1898 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1899 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1900 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1901 | * @mtd: mtd info structure |
| 1902 | * @chip: nand chip info structure |
| 1903 | * @data_offs: offset of requested data within the page |
| 1904 | * @readlen: data length |
| 1905 | * @bufpoi: buffer to store read data |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1906 | * @page: page number to read |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1907 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1908 | 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] | 1909 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 1910 | int page) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1911 | { |
| 1912 | int start_step, end_step, num_steps; |
| 1913 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1914 | uint8_t *p; |
| 1915 | int data_col_addr, i, gaps = 0; |
| 1916 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 1917 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1918 | int index; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1919 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1920 | int ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1921 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1922 | /* Column address within the page aligned to ECC size (256bytes) */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1923 | start_step = data_offs / chip->ecc.size; |
| 1924 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 1925 | num_steps = end_step - start_step + 1; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1926 | index = start_step * chip->ecc.bytes; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1927 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1928 | /* Data size aligned to ECC ecc.size */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1929 | datafrag_len = num_steps * chip->ecc.size; |
| 1930 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 1931 | |
| 1932 | data_col_addr = start_step * chip->ecc.size; |
| 1933 | /* If we read not a page aligned data */ |
| 1934 | if (data_col_addr != 0) |
| 1935 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 1936 | |
| 1937 | p = bufpoi + data_col_addr; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1938 | ret = nand_read_data_op(chip, p, datafrag_len, false); |
| 1939 | if (ret) |
| 1940 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1941 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1942 | /* Calculate ECC */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1943 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 1944 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 1945 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1946 | /* |
| 1947 | * The performance is faster if we position offsets according to |
| 1948 | * ecc.pos. Let's make sure that there are no gaps in ECC positions. |
| 1949 | */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1950 | for (i = 0; i < eccfrag_len - 1; i++) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1951 | if (eccpos[i + index] + 1 != eccpos[i + index + 1]) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1952 | gaps = 1; |
| 1953 | break; |
| 1954 | } |
| 1955 | } |
| 1956 | if (gaps) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1957 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 1958 | chip->oob_poi, mtd->oobsize, |
| 1959 | false); |
| 1960 | if (ret) |
| 1961 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1962 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1963 | /* |
| 1964 | * Send the command to read the particular ECC bytes take care |
| 1965 | * about buswidth alignment in read_buf. |
| 1966 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1967 | aligned_pos = eccpos[index] & ~(busw - 1); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1968 | aligned_len = eccfrag_len; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1969 | if (eccpos[index] & (busw - 1)) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1970 | aligned_len++; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1971 | if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1)) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1972 | aligned_len++; |
| 1973 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1974 | ret = nand_change_read_column_op(chip, |
| 1975 | mtd->writesize + aligned_pos, |
| 1976 | &chip->oob_poi[aligned_pos], |
| 1977 | aligned_len, false); |
| 1978 | if (ret) |
| 1979 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | for (i = 0; i < eccfrag_len; i++) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1983 | chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]]; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1984 | |
| 1985 | p = bufpoi + data_col_addr; |
| 1986 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1987 | int stat; |
| 1988 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1989 | stat = chip->ecc.correct(mtd, p, |
| 1990 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1991 | if (stat == -EBADMSG && |
| 1992 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1993 | /* check for empty pages with bitflips */ |
| 1994 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1995 | &chip->buffers->ecccode[i], |
| 1996 | chip->ecc.bytes, |
| 1997 | NULL, 0, |
| 1998 | chip->ecc.strength); |
| 1999 | } |
| 2000 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2001 | if (stat < 0) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2002 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2003 | } else { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2004 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2005 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2006 | } |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2007 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2008 | return max_bitflips; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2012 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
| 2013 | * @mtd: mtd info structure |
| 2014 | * @chip: nand chip info structure |
| 2015 | * @buf: buffer to store read data |
| 2016 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2017 | * @page: page number to read |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2018 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2019 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2020 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2021 | 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] | 2022 | uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2023 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2024 | int i, eccsize = chip->ecc.size; |
| 2025 | int eccbytes = chip->ecc.bytes; |
| 2026 | int eccsteps = chip->ecc.steps; |
| 2027 | uint8_t *p = buf; |
| 2028 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2029 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 2030 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2031 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2032 | int ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2033 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2034 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2035 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2036 | |
| 2037 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2038 | if (ret) |
| 2039 | return ret; |
| 2040 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2041 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2042 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2043 | |
| 2044 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2045 | if (ret) |
| 2046 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2047 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2048 | for (i = 0; i < chip->ecc.total; i++) |
| 2049 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2050 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2051 | eccsteps = chip->ecc.steps; |
| 2052 | p = buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2053 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2054 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2055 | int stat; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2056 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2057 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2058 | if (stat == -EBADMSG && |
| 2059 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2060 | /* check for empty pages with bitflips */ |
| 2061 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2062 | &ecc_code[i], eccbytes, |
| 2063 | NULL, 0, |
| 2064 | chip->ecc.strength); |
| 2065 | } |
| 2066 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2067 | if (stat < 0) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2068 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2069 | } else { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2070 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2071 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2072 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2073 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2074 | return max_bitflips; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2075 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2076 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2077 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2078 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
| 2079 | * @mtd: mtd info structure |
| 2080 | * @chip: nand chip info structure |
| 2081 | * @buf: buffer to store read data |
| 2082 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2083 | * @page: page number to read |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2084 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2085 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 2086 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 2087 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 2088 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 2089 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2090 | */ |
| 2091 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2092 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2093 | { |
| 2094 | int i, eccsize = chip->ecc.size; |
| 2095 | int eccbytes = chip->ecc.bytes; |
| 2096 | int eccsteps = chip->ecc.steps; |
| 2097 | uint8_t *p = buf; |
| 2098 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 2099 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2100 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2101 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2102 | int ret; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2103 | |
| 2104 | /* Read the OOB area first */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2105 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 2106 | if (ret) |
| 2107 | return ret; |
| 2108 | |
| 2109 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2110 | if (ret) |
| 2111 | return ret; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2112 | |
| 2113 | for (i = 0; i < chip->ecc.total; i++) |
| 2114 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 2115 | |
| 2116 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2117 | int stat; |
| 2118 | |
| 2119 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2120 | |
| 2121 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2122 | if (ret) |
| 2123 | return ret; |
| 2124 | |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2125 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2126 | |
| 2127 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2128 | if (stat == -EBADMSG && |
| 2129 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2130 | /* check for empty pages with bitflips */ |
| 2131 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2132 | &ecc_code[i], eccbytes, |
| 2133 | NULL, 0, |
| 2134 | chip->ecc.strength); |
| 2135 | } |
| 2136 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2137 | if (stat < 0) { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2138 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2139 | } else { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2140 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2141 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2142 | } |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2143 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2144 | return max_bitflips; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2148 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
| 2149 | * @mtd: mtd info structure |
| 2150 | * @chip: nand chip info structure |
| 2151 | * @buf: buffer to store read data |
| 2152 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2153 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2154 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2155 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2156 | * need a special oob layout and handling. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2157 | */ |
| 2158 | 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] | 2159 | uint8_t *buf, int oob_required, int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2160 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2161 | int ret, i, eccsize = chip->ecc.size; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2162 | int eccbytes = chip->ecc.bytes; |
| 2163 | int eccsteps = chip->ecc.steps; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2164 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2165 | uint8_t *p = buf; |
| 2166 | uint8_t *oob = chip->oob_poi; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2167 | unsigned int max_bitflips = 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2168 | |
| 2169 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2170 | int stat; |
| 2171 | |
| 2172 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2173 | |
| 2174 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2175 | if (ret) |
| 2176 | return ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2177 | |
| 2178 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2179 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2180 | false); |
| 2181 | if (ret) |
| 2182 | return ret; |
| 2183 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2184 | oob += chip->ecc.prepad; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2185 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2186 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2187 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2188 | |
| 2189 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2190 | if (ret) |
| 2191 | return ret; |
| 2192 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2193 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2194 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2195 | oob += eccbytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2196 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2197 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2198 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2199 | false); |
| 2200 | if (ret) |
| 2201 | return ret; |
| 2202 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2203 | oob += chip->ecc.postpad; |
| 2204 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2205 | |
| 2206 | if (stat == -EBADMSG && |
| 2207 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2208 | /* check for empty pages with bitflips */ |
| 2209 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 2210 | oob - eccpadbytes, |
| 2211 | eccpadbytes, |
| 2212 | NULL, 0, |
| 2213 | chip->ecc.strength); |
| 2214 | } |
| 2215 | |
| 2216 | if (stat < 0) { |
| 2217 | mtd->ecc_stats.failed++; |
| 2218 | } else { |
| 2219 | mtd->ecc_stats.corrected += stat; |
| 2220 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2221 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2222 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2223 | |
| 2224 | /* Calculate remaining oob bytes */ |
| 2225 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2226 | if (i) { |
| 2227 | ret = nand_read_data_op(chip, oob, i, false); |
| 2228 | if (ret) |
| 2229 | return ret; |
| 2230 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2231 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2232 | return max_bitflips; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2233 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2234 | |
| 2235 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2236 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
| 2237 | * @chip: nand chip structure |
| 2238 | * @oob: oob destination address |
| 2239 | * @ops: oob ops structure |
| 2240 | * @len: size of oob to transfer |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2241 | */ |
| 2242 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, |
| 2243 | struct mtd_oob_ops *ops, size_t len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2244 | { |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2245 | switch (ops->mode) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2246 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2247 | case MTD_OPS_PLACE_OOB: |
| 2248 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2249 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 2250 | return oob + len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2251 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2252 | case MTD_OPS_AUTO_OOB: { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2253 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 2254 | uint32_t boffs = 0, roffs = ops->ooboffs; |
| 2255 | size_t bytes = 0; |
| 2256 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2257 | for (; free->length && len; free++, len -= bytes) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2258 | /* Read request not from offset 0? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2259 | if (unlikely(roffs)) { |
| 2260 | if (roffs >= free->length) { |
| 2261 | roffs -= free->length; |
| 2262 | continue; |
| 2263 | } |
| 2264 | boffs = free->offset + roffs; |
| 2265 | bytes = min_t(size_t, len, |
| 2266 | (free->length - roffs)); |
| 2267 | roffs = 0; |
| 2268 | } else { |
| 2269 | bytes = min_t(size_t, len, free->length); |
| 2270 | boffs = free->offset; |
| 2271 | } |
| 2272 | memcpy(oob, chip->oob_poi + boffs, bytes); |
| 2273 | oob += bytes; |
| 2274 | } |
| 2275 | return oob; |
| 2276 | } |
| 2277 | default: |
| 2278 | BUG(); |
| 2279 | } |
| 2280 | return NULL; |
| 2281 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2282 | |
| 2283 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2284 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 2285 | * @mtd: MTD device structure |
| 2286 | * @retry_mode: the retry mode to use |
| 2287 | * |
| 2288 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 2289 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 2290 | * a new threshold, the host should retry reading the page. |
| 2291 | */ |
| 2292 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 2293 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2294 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2295 | |
| 2296 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 2297 | |
| 2298 | if (retry_mode >= chip->read_retries) |
| 2299 | return -EINVAL; |
| 2300 | |
| 2301 | if (!chip->setup_read_retry) |
| 2302 | return -EOPNOTSUPP; |
| 2303 | |
| 2304 | return chip->setup_read_retry(mtd, retry_mode); |
| 2305 | } |
| 2306 | |
| 2307 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2308 | * nand_do_read_ops - [INTERN] Read data with ECC |
| 2309 | * @mtd: MTD device structure |
| 2310 | * @from: offset to read from |
| 2311 | * @ops: oob ops structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2312 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2313 | * Internal function. Called with chip held. |
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 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 2316 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2317 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2318 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2319 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2320 | int ret = 0; |
| 2321 | uint32_t readlen = ops->len; |
| 2322 | uint32_t oobreadlen = ops->ooblen; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2323 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2324 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2325 | uint8_t *bufpoi, *oob, *buf; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2326 | int use_bufpoi; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2327 | unsigned int max_bitflips = 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2328 | int retry_mode = 0; |
| 2329 | bool ecc_fail = false; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2330 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2331 | chipnr = (int)(from >> chip->chip_shift); |
| 2332 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2333 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2334 | realpage = (int)(from >> chip->page_shift); |
| 2335 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2336 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2337 | col = (int)(from & (mtd->writesize - 1)); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2338 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2339 | buf = ops->datbuf; |
| 2340 | oob = ops->oobbuf; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2341 | oob_required = oob ? 1 : 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2342 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2343 | while (1) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2344 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
Scott Wood | ea95b64 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 2345 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 2346 | schedule(); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2347 | bytes = min(mtd->writesize - col, readlen); |
| 2348 | aligned = (bytes == mtd->writesize); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2349 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2350 | if (!aligned) |
| 2351 | use_bufpoi = 1; |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 2352 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
| 2353 | use_bufpoi = !IS_ALIGNED((unsigned long)buf, |
| 2354 | chip->buf_align); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2355 | else |
| 2356 | use_bufpoi = 0; |
| 2357 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2358 | /* Is the current page in the buffer? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2359 | if (realpage != chip->pagebuf || oob) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2360 | bufpoi = use_bufpoi ? chip->buffers->databuf : buf; |
| 2361 | |
| 2362 | if (use_bufpoi && aligned) |
| 2363 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 2364 | __func__, buf); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2365 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2366 | read_retry: |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2367 | if (nand_standard_page_accessors(&chip->ecc)) { |
| 2368 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2369 | if (ret) |
| 2370 | break; |
| 2371 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2372 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2373 | /* |
| 2374 | * Now read the page into the buffer. Absent an error, |
| 2375 | * the read methods return max bitflips per ecc step. |
| 2376 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2377 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
| 2378 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
| 2379 | oob_required, |
| 2380 | page); |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 2381 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2382 | !oob) |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2383 | ret = chip->ecc.read_subpage(mtd, chip, |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 2384 | col, bytes, bufpoi, |
| 2385 | page); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2386 | else |
Sandeep Paulraj | 883189e | 2009-08-10 13:27:46 -0400 | [diff] [blame] | 2387 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2388 | oob_required, page); |
| 2389 | if (ret < 0) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2390 | if (use_bufpoi) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2391 | /* Invalidate page cache */ |
| 2392 | chip->pagebuf = -1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2393 | break; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2394 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2395 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2396 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 2397 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2398 | /* Transfer not aligned data */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2399 | if (use_bufpoi) { |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 2400 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2401 | !(mtd->ecc_stats.failed - ecc_failures) && |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2402 | (ops->mode != MTD_OPS_RAW)) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2403 | chip->pagebuf = realpage; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2404 | chip->pagebuf_bitflips = ret; |
| 2405 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2406 | /* Invalidate page cache */ |
| 2407 | chip->pagebuf = -1; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2408 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2409 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 2410 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2411 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2412 | if (unlikely(oob)) { |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2413 | int toread = min(oobreadlen, max_oobsize); |
| 2414 | |
| 2415 | if (toread) { |
| 2416 | oob = nand_transfer_oob(chip, |
| 2417 | oob, ops, toread); |
| 2418 | oobreadlen -= toread; |
| 2419 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2420 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2421 | |
| 2422 | if (chip->options & NAND_NEED_READRDY) { |
| 2423 | /* Apply delay or wait for ready/busy pin */ |
| 2424 | if (!chip->dev_ready) |
| 2425 | udelay(chip->chip_delay); |
| 2426 | else |
| 2427 | nand_wait_ready(mtd); |
| 2428 | } |
| 2429 | |
| 2430 | if (mtd->ecc_stats.failed - ecc_failures) { |
| 2431 | if (retry_mode + 1 < chip->read_retries) { |
| 2432 | retry_mode++; |
| 2433 | ret = nand_setup_read_retry(mtd, |
| 2434 | retry_mode); |
| 2435 | if (ret < 0) |
| 2436 | break; |
| 2437 | |
| 2438 | /* Reset failures; retry */ |
| 2439 | mtd->ecc_stats.failed = ecc_failures; |
| 2440 | goto read_retry; |
| 2441 | } else { |
| 2442 | /* No more retry modes; real failure */ |
| 2443 | ecc_fail = true; |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | buf += bytes; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2448 | } else { |
| 2449 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 2450 | buf += bytes; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2451 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 2452 | chip->pagebuf_bitflips); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2453 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2454 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2455 | readlen -= bytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2456 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2457 | /* Reset to retry mode 0 */ |
| 2458 | if (retry_mode) { |
| 2459 | ret = nand_setup_read_retry(mtd, 0); |
| 2460 | if (ret < 0) |
| 2461 | break; |
| 2462 | retry_mode = 0; |
| 2463 | } |
| 2464 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2465 | if (!readlen) |
| 2466 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2467 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2468 | /* For subsequent reads align to page boundary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2469 | col = 0; |
| 2470 | /* Increment page address */ |
| 2471 | realpage++; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2472 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2473 | page = realpage & chip->pagemask; |
| 2474 | /* Check, if we cross a chip boundary */ |
| 2475 | if (!page) { |
| 2476 | chipnr++; |
| 2477 | chip->select_chip(mtd, -1); |
| 2478 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2479 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2480 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2481 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2482 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2483 | ops->retlen = ops->len - (size_t) readlen; |
| 2484 | if (oob) |
| 2485 | ops->oobretlen = ops->ooblen - oobreadlen; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2486 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2487 | if (ret < 0) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2488 | return ret; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2489 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2490 | if (ecc_fail) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2491 | return -EBADMSG; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2492 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2493 | return max_bitflips; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2494 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2495 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2496 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2497 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
| 2498 | * @mtd: mtd info structure |
| 2499 | * @chip: nand chip info structure |
| 2500 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2501 | */ |
| 2502 | 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] | 2503 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2504 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2505 | 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] | 2506 | } |
| 2507 | |
| 2508 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2509 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2510 | * with syndromes |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2511 | * @mtd: mtd info structure |
| 2512 | * @chip: nand chip info structure |
| 2513 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2514 | */ |
| 2515 | 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] | 2516 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2517 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2518 | int length = mtd->oobsize; |
| 2519 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2520 | int eccsize = chip->ecc.size; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2521 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2522 | int i, toread, sndrnd = 0, pos, ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2523 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2524 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 2525 | if (ret) |
| 2526 | return ret; |
| 2527 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2528 | for (i = 0; i < chip->ecc.steps; i++) { |
| 2529 | if (sndrnd) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2530 | int ret; |
| 2531 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2532 | pos = eccsize + i * (eccsize + chunk); |
| 2533 | if (mtd->writesize > 512) |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2534 | ret = nand_change_read_column_op(chip, pos, |
| 2535 | NULL, 0, |
| 2536 | false); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2537 | else |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2538 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 2539 | 0); |
| 2540 | |
| 2541 | if (ret) |
| 2542 | return ret; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2543 | } else |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2544 | sndrnd = 1; |
| 2545 | toread = min_t(int, length, chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2546 | |
| 2547 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 2548 | if (ret) |
| 2549 | return ret; |
| 2550 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2551 | bufpoi += toread; |
| 2552 | length -= toread; |
| 2553 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2554 | if (length > 0) { |
| 2555 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 2556 | if (ret) |
| 2557 | return ret; |
| 2558 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2559 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2560 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2561 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2562 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2563 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2564 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
| 2565 | * @mtd: mtd info structure |
| 2566 | * @chip: nand chip info structure |
| 2567 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2568 | */ |
| 2569 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 2570 | int page) |
| 2571 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2572 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 2573 | mtd->oobsize); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2577 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
| 2578 | * with syndrome - only for large page flash |
| 2579 | * @mtd: mtd info structure |
| 2580 | * @chip: nand chip info structure |
| 2581 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2582 | */ |
| 2583 | static int nand_write_oob_syndrome(struct mtd_info *mtd, |
| 2584 | struct nand_chip *chip, int page) |
| 2585 | { |
| 2586 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2587 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2588 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2589 | const uint8_t *bufpoi = chip->oob_poi; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2590 | |
| 2591 | /* |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2592 | * data-ecc-data-ecc ... ecc-oob |
| 2593 | * or |
| 2594 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2595 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2596 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 2597 | pos = steps * (eccsize + chunk); |
| 2598 | steps = 0; |
| 2599 | } else |
| 2600 | pos = eccsize; |
| 2601 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2602 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 2603 | if (ret) |
| 2604 | return ret; |
| 2605 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2606 | for (i = 0; i < steps; i++) { |
| 2607 | if (sndcmd) { |
| 2608 | if (mtd->writesize <= 512) { |
| 2609 | uint32_t fill = 0xFFFFFFFF; |
| 2610 | |
| 2611 | len = eccsize; |
| 2612 | while (len > 0) { |
| 2613 | int num = min_t(int, len, 4); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2614 | |
| 2615 | ret = nand_write_data_op(chip, &fill, |
| 2616 | num, false); |
| 2617 | if (ret) |
| 2618 | return ret; |
| 2619 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2620 | len -= num; |
| 2621 | } |
| 2622 | } else { |
| 2623 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2624 | ret = nand_change_write_column_op(chip, pos, |
| 2625 | NULL, 0, |
| 2626 | false); |
| 2627 | if (ret) |
| 2628 | return ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2629 | } |
| 2630 | } else |
| 2631 | sndcmd = 1; |
| 2632 | len = min_t(int, length, chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2633 | |
| 2634 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 2635 | if (ret) |
| 2636 | return ret; |
| 2637 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2638 | bufpoi += len; |
| 2639 | length -= len; |
| 2640 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2641 | if (length > 0) { |
| 2642 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 2643 | if (ret) |
| 2644 | return ret; |
| 2645 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2646 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2647 | return nand_prog_page_end_op(chip); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2651 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
| 2652 | * @mtd: MTD device structure |
| 2653 | * @from: offset to read from |
| 2654 | * @ops: oob operations description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2655 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2656 | * NAND read out-of-band data from the spare area. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2657 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2658 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 2659 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2660 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2661 | int page, realpage, chipnr; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2662 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2663 | struct mtd_ecc_stats stats; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2664 | int readlen = ops->ooblen; |
| 2665 | int len; |
| 2666 | uint8_t *buf = ops->oobbuf; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2667 | int ret = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2668 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2669 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2670 | __func__, (unsigned long long)from, readlen); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2671 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2672 | stats = mtd->ecc_stats; |
| 2673 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2674 | len = mtd_oobavail(mtd, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2675 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2676 | if (unlikely(ops->ooboffs >= len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2677 | pr_debug("%s: attempt to start read outside oob\n", |
| 2678 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2679 | return -EINVAL; |
| 2680 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2681 | |
| 2682 | /* Do not allow reads past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2683 | if (unlikely(from >= mtd->size || |
| 2684 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 2685 | (from >> chip->page_shift)) * len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2686 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2687 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2688 | return -EINVAL; |
| 2689 | } |
| 2690 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2691 | chipnr = (int)(from >> chip->chip_shift); |
| 2692 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2693 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2694 | /* Shift to get page */ |
| 2695 | realpage = (int)(from >> chip->page_shift); |
| 2696 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2697 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2698 | while (1) { |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 2699 | schedule(); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2700 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2701 | if (ops->mode == MTD_OPS_RAW) |
| 2702 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
| 2703 | else |
| 2704 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 2705 | |
| 2706 | if (ret < 0) |
| 2707 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2708 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2709 | len = min(len, readlen); |
| 2710 | buf = nand_transfer_oob(chip, buf, ops, len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2711 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2712 | if (chip->options & NAND_NEED_READRDY) { |
| 2713 | /* Apply delay or wait for ready/busy pin */ |
| 2714 | if (!chip->dev_ready) |
| 2715 | udelay(chip->chip_delay); |
| 2716 | else |
| 2717 | nand_wait_ready(mtd); |
| 2718 | } |
| 2719 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2720 | readlen -= len; |
| 2721 | if (!readlen) |
| 2722 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2723 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2724 | /* Increment page address */ |
| 2725 | realpage++; |
| 2726 | |
| 2727 | page = realpage & chip->pagemask; |
| 2728 | /* Check, if we cross a chip boundary */ |
| 2729 | if (!page) { |
| 2730 | chipnr++; |
| 2731 | chip->select_chip(mtd, -1); |
| 2732 | chip->select_chip(mtd, chipnr); |
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 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2735 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2736 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2737 | ops->oobretlen = ops->ooblen - readlen; |
| 2738 | |
| 2739 | if (ret < 0) |
| 2740 | return ret; |
| 2741 | |
| 2742 | if (mtd->ecc_stats.failed - stats.failed) |
| 2743 | return -EBADMSG; |
| 2744 | |
| 2745 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2749 | * 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] | 2750 | * @mtd: MTD device structure |
| 2751 | * @from: offset to read from |
| 2752 | * @ops: oob operation description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2753 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2754 | * NAND read data and/or out-of-band data. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2755 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2756 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 2757 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2758 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2759 | int ret = -ENOTSUPP; |
| 2760 | |
| 2761 | ops->retlen = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2762 | |
| 2763 | /* Do not allow reads past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2764 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2765 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2766 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2767 | return -EINVAL; |
| 2768 | } |
| 2769 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2770 | nand_get_device(mtd, FL_READING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2771 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2772 | switch (ops->mode) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2773 | case MTD_OPS_PLACE_OOB: |
| 2774 | case MTD_OPS_AUTO_OOB: |
| 2775 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2776 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2777 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2778 | default: |
| 2779 | goto out; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2780 | } |
| 2781 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2782 | if (!ops->datbuf) |
| 2783 | ret = nand_do_read_oob(mtd, from, ops); |
| 2784 | else |
| 2785 | ret = nand_do_read_ops(mtd, from, ops); |
| 2786 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2787 | out: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2788 | nand_release_device(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2789 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2790 | } |
| 2791 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2792 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2793 | * nand_write_page_raw - [INTERN] raw page write function |
| 2794 | * @mtd: mtd info structure |
| 2795 | * @chip: nand chip info structure |
| 2796 | * @buf: data buffer |
| 2797 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2798 | * @page: page number to write |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2799 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2800 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2801 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2802 | 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] | 2803 | const uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2804 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2805 | int ret; |
| 2806 | |
| 2807 | ret = nand_write_data_op(chip, buf, mtd->writesize, false); |
| 2808 | if (ret) |
| 2809 | return ret; |
| 2810 | |
| 2811 | if (oob_required) { |
| 2812 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 2813 | false); |
| 2814 | if (ret) |
| 2815 | return ret; |
| 2816 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2817 | |
| 2818 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2819 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2820 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2821 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2822 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
| 2823 | * @mtd: mtd info structure |
| 2824 | * @chip: nand chip info structure |
| 2825 | * @buf: data buffer |
| 2826 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2827 | * @page: page number to write |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2828 | * |
| 2829 | * We need a special oob layout and handling even when ECC isn't checked. |
| 2830 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2831 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2832 | struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2833 | const uint8_t *buf, int oob_required, |
| 2834 | int page) |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2835 | { |
| 2836 | int eccsize = chip->ecc.size; |
| 2837 | int eccbytes = chip->ecc.bytes; |
| 2838 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2839 | int steps, size, ret; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2840 | |
| 2841 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2842 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 2843 | if (ret) |
| 2844 | return ret; |
| 2845 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2846 | buf += eccsize; |
| 2847 | |
| 2848 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2849 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 2850 | false); |
| 2851 | if (ret) |
| 2852 | return ret; |
| 2853 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2854 | oob += chip->ecc.prepad; |
| 2855 | } |
| 2856 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2857 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 2858 | if (ret) |
| 2859 | return ret; |
| 2860 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2861 | oob += eccbytes; |
| 2862 | |
| 2863 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2864 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 2865 | false); |
| 2866 | if (ret) |
| 2867 | return ret; |
| 2868 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2869 | oob += chip->ecc.postpad; |
| 2870 | } |
| 2871 | } |
| 2872 | |
| 2873 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2874 | if (size) { |
| 2875 | ret = nand_write_data_op(chip, oob, size, false); |
| 2876 | if (ret) |
| 2877 | return ret; |
| 2878 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2879 | |
| 2880 | return 0; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2881 | } |
| 2882 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2883 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
| 2884 | * @mtd: mtd info structure |
| 2885 | * @chip: nand chip info structure |
| 2886 | * @buf: data buffer |
| 2887 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2888 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2889 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2890 | 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] | 2891 | const uint8_t *buf, int oob_required, |
| 2892 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2893 | { |
| 2894 | int i, eccsize = chip->ecc.size; |
| 2895 | int eccbytes = chip->ecc.bytes; |
| 2896 | int eccsteps = chip->ecc.steps; |
| 2897 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2898 | const uint8_t *p = buf; |
| 2899 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2900 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2901 | /* Software ECC calculation */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2902 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 2903 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2904 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2905 | for (i = 0; i < chip->ecc.total; i++) |
| 2906 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2907 | |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2908 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2909 | } |
| 2910 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2911 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2912 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
| 2913 | * @mtd: mtd info structure |
| 2914 | * @chip: nand chip info structure |
| 2915 | * @buf: data buffer |
| 2916 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2917 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2918 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2919 | 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] | 2920 | const uint8_t *buf, int oob_required, |
| 2921 | int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2922 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2923 | int i, eccsize = chip->ecc.size; |
| 2924 | int eccbytes = chip->ecc.bytes; |
| 2925 | int eccsteps = chip->ecc.steps; |
| 2926 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2927 | const uint8_t *p = buf; |
| 2928 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2929 | int ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2930 | |
| 2931 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2932 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2933 | |
| 2934 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 2935 | if (ret) |
| 2936 | return ret; |
| 2937 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2938 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2939 | } |
| 2940 | |
| 2941 | for (i = 0; i < chip->ecc.total; i++) |
| 2942 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 2943 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2944 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2945 | if (ret) |
| 2946 | return ret; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2947 | |
| 2948 | return 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2949 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2950 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2951 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2952 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2953 | * @mtd: mtd info structure |
| 2954 | * @chip: nand chip info structure |
| 2955 | * @offset: column address of subpage within the page |
| 2956 | * @data_len: data length |
| 2957 | * @buf: data buffer |
| 2958 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2959 | * @page: page number to write |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2960 | */ |
| 2961 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 2962 | struct nand_chip *chip, uint32_t offset, |
| 2963 | uint32_t data_len, const uint8_t *buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2964 | int oob_required, int page) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2965 | { |
| 2966 | uint8_t *oob_buf = chip->oob_poi; |
| 2967 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2968 | int ecc_size = chip->ecc.size; |
| 2969 | int ecc_bytes = chip->ecc.bytes; |
| 2970 | int ecc_steps = chip->ecc.steps; |
| 2971 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2972 | uint32_t start_step = offset / ecc_size; |
| 2973 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 2974 | int oob_bytes = mtd->oobsize / ecc_steps; |
| 2975 | int step, i; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2976 | int ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2977 | |
| 2978 | for (step = 0; step < ecc_steps; step++) { |
| 2979 | /* configure controller for WRITE access */ |
| 2980 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2981 | |
| 2982 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2983 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 2984 | if (ret) |
| 2985 | return ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2986 | |
| 2987 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 2988 | if ((step < start_step) || (step > end_step)) |
| 2989 | memset(ecc_calc, 0xff, ecc_bytes); |
| 2990 | else |
| 2991 | chip->ecc.calculate(mtd, buf, ecc_calc); |
| 2992 | |
| 2993 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 2994 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 2995 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 2996 | memset(oob_buf, 0xff, oob_bytes); |
| 2997 | |
| 2998 | buf += ecc_size; |
| 2999 | ecc_calc += ecc_bytes; |
| 3000 | oob_buf += oob_bytes; |
| 3001 | } |
| 3002 | |
| 3003 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 3004 | /* this include masked-value(0xFF) for unwritten subpages */ |
| 3005 | ecc_calc = chip->buffers->ecccalc; |
| 3006 | for (i = 0; i < chip->ecc.total; i++) |
| 3007 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 3008 | |
| 3009 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3010 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3011 | if (ret) |
| 3012 | return ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3013 | |
| 3014 | return 0; |
| 3015 | } |
| 3016 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3017 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3018 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
| 3019 | * @mtd: mtd info structure |
| 3020 | * @chip: nand chip info structure |
| 3021 | * @buf: data buffer |
| 3022 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3023 | * @page: page number to write |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3024 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3025 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3026 | * need a special oob layout and handling. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3027 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3028 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
| 3029 | struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3030 | const uint8_t *buf, int oob_required, |
| 3031 | int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3032 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3033 | int i, eccsize = chip->ecc.size; |
| 3034 | int eccbytes = chip->ecc.bytes; |
| 3035 | int eccsteps = chip->ecc.steps; |
| 3036 | const uint8_t *p = buf; |
| 3037 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3038 | int ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3039 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3040 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3041 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3042 | |
| 3043 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3044 | if (ret) |
| 3045 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3046 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3047 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3048 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3049 | false); |
| 3050 | if (ret) |
| 3051 | return ret; |
| 3052 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3053 | oob += chip->ecc.prepad; |
| 3054 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3055 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3056 | chip->ecc.calculate(mtd, p, oob); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3057 | |
| 3058 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3059 | if (ret) |
| 3060 | return ret; |
| 3061 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3062 | oob += eccbytes; |
| 3063 | |
| 3064 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3065 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3066 | false); |
| 3067 | if (ret) |
| 3068 | return ret; |
| 3069 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3070 | oob += chip->ecc.postpad; |
| 3071 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3072 | } |
| 3073 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3074 | /* Calculate remaining oob bytes */ |
| 3075 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3076 | if (i) { |
| 3077 | ret = nand_write_data_op(chip, oob, i, false); |
| 3078 | if (ret) |
| 3079 | return ret; |
| 3080 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3081 | |
| 3082 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3083 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3084 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3085 | /** |
| 3086 | * nand_write_page - [REPLACEABLE] write one page |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3087 | * @mtd: MTD device structure |
| 3088 | * @chip: NAND chip descriptor |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3089 | * @offset: address offset within the page |
| 3090 | * @data_len: length of actual data to be written |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3091 | * @buf: the data to write |
| 3092 | * @oob_required: must write chip->oob_poi to OOB |
| 3093 | * @page: page number to write |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3094 | * @raw: use _raw version of write_page |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3095 | */ |
| 3096 | 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] | 3097 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3098 | int oob_required, int page, int raw) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3099 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3100 | int status, subpage; |
| 3101 | |
| 3102 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3103 | chip->ecc.write_subpage) |
| 3104 | subpage = offset || (data_len < mtd->writesize); |
| 3105 | else |
| 3106 | subpage = 0; |
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 | status = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3110 | if (status) |
| 3111 | return status; |
| 3112 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3113 | |
| 3114 | if (unlikely(raw)) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3115 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3116 | oob_required, page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3117 | else if (subpage) |
| 3118 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3119 | buf, oob_required, page); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3120 | else |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3121 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 3122 | page); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3123 | |
| 3124 | if (status < 0) |
| 3125 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3126 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3127 | if (nand_standard_page_accessors(&chip->ecc)) |
| 3128 | return nand_prog_page_end_op(chip); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3129 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3130 | return 0; |
| 3131 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3132 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3133 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3134 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
| 3135 | * @mtd: MTD device structure |
| 3136 | * @oob: oob data buffer |
| 3137 | * @len: oob data write length |
| 3138 | * @ops: oob ops structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3139 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3140 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 3141 | struct mtd_oob_ops *ops) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3142 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3143 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3144 | |
| 3145 | /* |
| 3146 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 3147 | * data from a previous OOB read. |
| 3148 | */ |
| 3149 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 3150 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3151 | switch (ops->mode) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3152 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3153 | case MTD_OPS_PLACE_OOB: |
| 3154 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3155 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 3156 | return oob + len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3157 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3158 | case MTD_OPS_AUTO_OOB: { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3159 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 3160 | uint32_t boffs = 0, woffs = ops->ooboffs; |
| 3161 | size_t bytes = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3162 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3163 | for (; free->length && len; free++, len -= bytes) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3164 | /* Write request not from offset 0? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3165 | if (unlikely(woffs)) { |
| 3166 | if (woffs >= free->length) { |
| 3167 | woffs -= free->length; |
| 3168 | continue; |
| 3169 | } |
| 3170 | boffs = free->offset + woffs; |
| 3171 | bytes = min_t(size_t, len, |
| 3172 | (free->length - woffs)); |
| 3173 | woffs = 0; |
| 3174 | } else { |
| 3175 | bytes = min_t(size_t, len, free->length); |
| 3176 | boffs = free->offset; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3177 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3178 | memcpy(chip->oob_poi + boffs, oob, bytes); |
| 3179 | oob += bytes; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3180 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3181 | return oob; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3182 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3183 | default: |
| 3184 | BUG(); |
| 3185 | } |
| 3186 | return NULL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3187 | } |
| 3188 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3189 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3190 | |
| 3191 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3192 | * nand_do_write_ops - [INTERN] NAND write with ECC |
| 3193 | * @mtd: MTD device structure |
| 3194 | * @to: offset to write to |
| 3195 | * @ops: oob operations description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3196 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3197 | * NAND write with ECC. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3198 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3199 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 3200 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3201 | { |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3202 | int chipnr, realpage, page, column; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3203 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3204 | uint32_t writelen = ops->len; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3205 | |
| 3206 | uint32_t oobwritelen = ops->ooblen; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3207 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3208 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3209 | uint8_t *oob = ops->oobbuf; |
| 3210 | uint8_t *buf = ops->datbuf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3211 | int ret; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3212 | int oob_required = oob ? 1 : 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3213 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3214 | ops->retlen = 0; |
| 3215 | if (!writelen) |
| 3216 | return 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3217 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3218 | /* Reject writes, which are not page aligned */ |
| 3219 | if (NOTALIGNED(to)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3220 | pr_notice("%s: attempt to write non page aligned data\n", |
| 3221 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3222 | return -EINVAL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | column = to & (mtd->writesize - 1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3226 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3227 | chipnr = (int)(to >> chip->chip_shift); |
| 3228 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3229 | |
| 3230 | /* Check, if it is write protected */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3231 | if (nand_check_wp(mtd)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3232 | ret = -EIO; |
| 3233 | goto err_out; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3234 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3235 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3236 | realpage = (int)(to >> chip->page_shift); |
| 3237 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3238 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3239 | /* Invalidate the page cache, when we write to the cached page */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3240 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 3241 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3242 | chip->pagebuf = -1; |
| 3243 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3244 | /* Don't allow multipage oob writes with offset */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3245 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 3246 | ret = -EINVAL; |
| 3247 | goto err_out; |
| 3248 | } |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3249 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3250 | while (1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3251 | int bytes = mtd->writesize; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3252 | uint8_t *wbuf = buf; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3253 | int use_bufpoi; |
Hector Palacios | e4fcdbb | 2016-07-18 09:37:41 +0200 | [diff] [blame] | 3254 | int part_pagewr = (column || writelen < mtd->writesize); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3255 | |
| 3256 | if (part_pagewr) |
| 3257 | use_bufpoi = 1; |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 3258 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
| 3259 | use_bufpoi = !IS_ALIGNED((unsigned long)buf, |
| 3260 | chip->buf_align); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3261 | else |
| 3262 | use_bufpoi = 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3263 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 3264 | schedule(); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3265 | /* Partial page write?, or need to use bounce buffer */ |
| 3266 | if (use_bufpoi) { |
| 3267 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 3268 | __func__, buf); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3269 | if (part_pagewr) |
| 3270 | bytes = min_t(int, bytes - column, writelen); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3271 | chip->pagebuf = -1; |
| 3272 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 3273 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 3274 | wbuf = chip->buffers->databuf; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 3275 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3276 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3277 | if (unlikely(oob)) { |
| 3278 | size_t len = min(oobwritelen, oobmaxlen); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3279 | oob = nand_fill_oob(mtd, oob, len, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3280 | oobwritelen -= len; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3281 | } else { |
| 3282 | /* We still need to erase leftover OOB data */ |
| 3283 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3284 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3285 | ret = chip->write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3286 | oob_required, page, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3287 | (ops->mode == MTD_OPS_RAW)); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3288 | if (ret) |
| 3289 | break; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3290 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3291 | writelen -= bytes; |
| 3292 | if (!writelen) |
| 3293 | break; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3294 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3295 | column = 0; |
| 3296 | buf += bytes; |
| 3297 | realpage++; |
| 3298 | |
| 3299 | page = realpage & chip->pagemask; |
| 3300 | /* Check, if we cross a chip boundary */ |
| 3301 | if (!page) { |
| 3302 | chipnr++; |
| 3303 | chip->select_chip(mtd, -1); |
| 3304 | chip->select_chip(mtd, chipnr); |
| 3305 | } |
| 3306 | } |
| 3307 | |
| 3308 | ops->retlen = ops->len - writelen; |
| 3309 | if (unlikely(oob)) |
| 3310 | ops->oobretlen = ops->ooblen; |
| 3311 | |
| 3312 | err_out: |
| 3313 | chip->select_chip(mtd, -1); |
| 3314 | return ret; |
| 3315 | } |
| 3316 | |
| 3317 | /** |
| 3318 | * panic_nand_write - [MTD Interface] NAND write with ECC |
| 3319 | * @mtd: MTD device structure |
| 3320 | * @to: offset to write to |
| 3321 | * @len: number of bytes to write |
| 3322 | * @retlen: pointer to variable to store the number of written bytes |
| 3323 | * @buf: the data to write |
| 3324 | * |
| 3325 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 3326 | * may for example be called by mtdoops when writing an oops while in panic. |
| 3327 | */ |
| 3328 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 3329 | size_t *retlen, const uint8_t *buf) |
| 3330 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3331 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3332 | struct mtd_oob_ops ops; |
| 3333 | int ret; |
| 3334 | |
| 3335 | /* Wait for the device to get ready */ |
| 3336 | panic_nand_wait(mtd, chip, 400); |
| 3337 | |
| 3338 | /* Grab the device */ |
| 3339 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 3340 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3341 | memset(&ops, 0, sizeof(ops)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3342 | ops.len = len; |
| 3343 | ops.datbuf = (uint8_t *)buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3344 | ops.mode = MTD_OPS_PLACE_OOB; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3345 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3346 | ret = nand_do_write_ops(mtd, to, &ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3347 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3348 | *retlen = ops.retlen; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3349 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3353 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3354 | * @mtd: MTD device structure |
| 3355 | * @to: offset to write to |
| 3356 | * @ops: oob operation description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3357 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3358 | * NAND write out-of-band. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3359 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3360 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 3361 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3362 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3363 | int chipnr, page, status, len; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3364 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3365 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3366 | pr_debug("%s: to = 0x%08x, len = %i\n", |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3367 | __func__, (unsigned int)to, (int)ops->ooblen); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3368 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3369 | len = mtd_oobavail(mtd, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3370 | |
| 3371 | /* Do not allow write past end of page */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3372 | if ((ops->ooboffs + ops->ooblen) > len) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3373 | pr_debug("%s: attempt to write past end of page\n", |
| 3374 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3375 | return -EINVAL; |
| 3376 | } |
| 3377 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3378 | if (unlikely(ops->ooboffs >= len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3379 | pr_debug("%s: attempt to start write outside oob\n", |
| 3380 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3381 | return -EINVAL; |
| 3382 | } |
| 3383 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3384 | /* Do not allow write past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3385 | if (unlikely(to >= mtd->size || |
| 3386 | ops->ooboffs + ops->ooblen > |
| 3387 | ((mtd->size >> chip->page_shift) - |
| 3388 | (to >> chip->page_shift)) * len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3389 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3390 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3391 | return -EINVAL; |
| 3392 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3393 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3394 | chipnr = (int)(to >> chip->chip_shift); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3395 | |
| 3396 | /* |
| 3397 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 3398 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 3399 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 3400 | * it in the doc2000 driver in August 1999. dwmw2. |
| 3401 | */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 3402 | nand_reset(chip, chipnr); |
| 3403 | |
| 3404 | chip->select_chip(mtd, chipnr); |
| 3405 | |
| 3406 | /* Shift to get page */ |
| 3407 | page = (int)(to >> chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3408 | |
| 3409 | /* Check, if it is write protected */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3410 | if (nand_check_wp(mtd)) { |
| 3411 | chip->select_chip(mtd, -1); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3412 | return -EROFS; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3413 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3414 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3415 | /* Invalidate the page cache, if we write to the cached page */ |
| 3416 | if (page == chip->pagebuf) |
| 3417 | chip->pagebuf = -1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3418 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3419 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
| 3420 | |
| 3421 | if (ops->mode == MTD_OPS_RAW) |
| 3422 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 3423 | else |
| 3424 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3425 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3426 | chip->select_chip(mtd, -1); |
| 3427 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3428 | if (status) |
| 3429 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3430 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3431 | ops->oobretlen = ops->ooblen; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3432 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3433 | return 0; |
| 3434 | } |
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 | /** |
| 3437 | * 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] | 3438 | * @mtd: MTD device structure |
| 3439 | * @to: offset to write to |
| 3440 | * @ops: oob operation description structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3441 | */ |
| 3442 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 3443 | struct mtd_oob_ops *ops) |
| 3444 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3445 | int ret = -ENOTSUPP; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3446 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3447 | ops->retlen = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3448 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3449 | /* Do not allow writes past end of device */ |
| 3450 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3451 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3452 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3453 | return -EINVAL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3454 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3455 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3456 | nand_get_device(mtd, FL_WRITING); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3457 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3458 | switch (ops->mode) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3459 | case MTD_OPS_PLACE_OOB: |
| 3460 | case MTD_OPS_AUTO_OOB: |
| 3461 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3462 | break; |
| 3463 | |
| 3464 | default: |
| 3465 | goto out; |
| 3466 | } |
| 3467 | |
| 3468 | if (!ops->datbuf) |
| 3469 | ret = nand_do_write_oob(mtd, to, ops); |
| 3470 | else |
| 3471 | ret = nand_do_write_ops(mtd, to, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3472 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3473 | out: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3474 | nand_release_device(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3475 | return ret; |
| 3476 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3477 | |
| 3478 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3479 | * single_erase - [GENERIC] NAND standard block erase command function |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3480 | * @mtd: MTD device structure |
| 3481 | * @page: the page address of the block which will be erased |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3482 | * |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3483 | * Standard erase command for NAND chips. Returns NAND status. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3484 | */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3485 | static int single_erase(struct mtd_info *mtd, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3486 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3487 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3488 | unsigned int eraseblock; |
| 3489 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3490 | /* Send commands to erase a block */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3491 | eraseblock = page >> (chip->phys_erase_shift - chip->page_shift); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3492 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3493 | return nand_erase_op(chip, eraseblock); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3494 | } |
| 3495 | |
| 3496 | /** |
| 3497 | * nand_erase - [MTD Interface] erase block(s) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3498 | * @mtd: MTD device structure |
| 3499 | * @instr: erase instruction |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3500 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3501 | * Erase one ore more blocks. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3502 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3503 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3504 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3505 | return nand_erase_nand(mtd, instr, 0); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3506 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3507 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3508 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3509 | * nand_erase_nand - [INTERN] erase block(s) |
| 3510 | * @mtd: MTD device structure |
| 3511 | * @instr: erase instruction |
| 3512 | * @allowbbt: allow erasing the bbt area |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3513 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3514 | * Erase one ore more blocks. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3515 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3516 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 3517 | int allowbbt) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3518 | { |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 3519 | int page, status, pages_per_block, ret, chipnr; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3520 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 3521 | loff_t len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3522 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3523 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 3524 | __func__, (unsigned long long)instr->addr, |
| 3525 | (unsigned long long)instr->len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3526 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3527 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3528 | return -EINVAL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3529 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3530 | /* Grab the lock and see if the device is available */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3531 | nand_get_device(mtd, FL_ERASING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3532 | |
| 3533 | /* Shift to get first page */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3534 | page = (int)(instr->addr >> chip->page_shift); |
| 3535 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3536 | |
| 3537 | /* Calculate pages in each block */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3538 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 3539 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3540 | /* Select the NAND device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3541 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3542 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3543 | /* Check, if it is write protected */ |
| 3544 | if (nand_check_wp(mtd)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3545 | pr_debug("%s: device is write protected!\n", |
| 3546 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3547 | instr->state = MTD_ERASE_FAILED; |
| 3548 | goto erase_exit; |
| 3549 | } |
| 3550 | |
| 3551 | /* Loop through the pages */ |
| 3552 | len = instr->len; |
| 3553 | |
| 3554 | instr->state = MTD_ERASING; |
| 3555 | |
| 3556 | while (len) { |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 3557 | schedule(); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3558 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3559 | /* 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] | 3560 | if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) << |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3561 | chip->page_shift, allowbbt)) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3562 | 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] | 3563 | __func__, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3564 | instr->state = MTD_ERASE_FAILED; |
Farhan Ali | 7c05319 | 2021-02-24 15:25:53 -0800 | [diff] [blame] | 3565 | instr->fail_addr = |
| 3566 | ((loff_t)page << chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3567 | goto erase_exit; |
| 3568 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3569 | |
| 3570 | /* |
| 3571 | * Invalidate the page cache, if we erase the block which |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3572 | * contains the current cached page. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3573 | */ |
| 3574 | if (page <= chip->pagebuf && chip->pagebuf < |
| 3575 | (page + pages_per_block)) |
| 3576 | chip->pagebuf = -1; |
| 3577 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3578 | status = chip->erase(mtd, page & chip->pagemask); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3579 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3580 | /* See if block erase succeeded */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3581 | if (status & NAND_STATUS_FAIL) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3582 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 3583 | __func__, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3584 | instr->state = MTD_ERASE_FAILED; |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3585 | instr->fail_addr = |
| 3586 | ((loff_t)page << chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3587 | goto erase_exit; |
| 3588 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3589 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3590 | /* Increment page address and decrement length */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3591 | len -= (1ULL << chip->phys_erase_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3592 | page += pages_per_block; |
| 3593 | |
| 3594 | /* Check, if we cross a chip boundary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3595 | if (len && !(page & chip->pagemask)) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3596 | chipnr++; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3597 | chip->select_chip(mtd, -1); |
| 3598 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | instr->state = MTD_ERASE_DONE; |
| 3602 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3603 | erase_exit: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3604 | |
| 3605 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3606 | |
| 3607 | /* Deselect and wake up anyone waiting on the device */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3608 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3609 | nand_release_device(mtd); |
| 3610 | |
| 3611 | /* Return more or less happy */ |
| 3612 | return ret; |
| 3613 | } |
| 3614 | |
| 3615 | /** |
| 3616 | * nand_sync - [MTD Interface] sync |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3617 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3618 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3619 | * Sync is actually a wait for chip ready function. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3620 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3621 | static void nand_sync(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3622 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3623 | pr_debug("%s: called\n", __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3624 | |
| 3625 | /* Grab the lock and see if the device is available */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3626 | nand_get_device(mtd, FL_SYNCING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3627 | /* Release it and go back */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3628 | nand_release_device(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3629 | } |
| 3630 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3631 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3632 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3633 | * @mtd: MTD device structure |
| 3634 | * @offs: offset relative to mtd start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3635 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3636 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3637 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3638 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3639 | int chipnr = (int)(offs >> chip->chip_shift); |
| 3640 | int ret; |
| 3641 | |
| 3642 | /* Select the NAND device */ |
| 3643 | nand_get_device(mtd, FL_READING); |
| 3644 | chip->select_chip(mtd, chipnr); |
| 3645 | |
| 3646 | ret = nand_block_checkbad(mtd, offs, 0); |
| 3647 | |
| 3648 | chip->select_chip(mtd, -1); |
| 3649 | nand_release_device(mtd); |
| 3650 | |
| 3651 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3655 | * 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] | 3656 | * @mtd: MTD device structure |
| 3657 | * @ofs: offset relative to mtd start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3658 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3659 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3660 | { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3661 | int ret; |
| 3662 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3663 | ret = nand_block_isbad(mtd, ofs); |
| 3664 | if (ret) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3665 | /* If it was bad already, return success and do nothing */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3666 | if (ret > 0) |
| 3667 | return 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3668 | return ret; |
| 3669 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3670 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3671 | return nand_block_markbad_lowlevel(mtd, ofs); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3672 | } |
| 3673 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3674 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3675 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 3676 | * @mtd: MTD device structure |
| 3677 | * @chip: nand chip info structure |
| 3678 | * @addr: feature address. |
| 3679 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3680 | */ |
| 3681 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3682 | int addr, uint8_t *subfeature_param) |
| 3683 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3684 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 3685 | if (!chip->onfi_version || |
| 3686 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3687 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Mylène Josserand | c21946b | 2018-07-13 18:10:23 +0200 | [diff] [blame] | 3688 | return -ENOTSUPP; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3689 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3690 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3691 | return nand_set_features_op(chip, addr, subfeature_param); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | /** |
| 3695 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 3696 | * @mtd: MTD device structure |
| 3697 | * @chip: nand chip info structure |
| 3698 | * @addr: feature address. |
| 3699 | * @subfeature_param: the subfeature parameters, a four bytes array. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3700 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3701 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3702 | int addr, uint8_t *subfeature_param) |
| 3703 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3704 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 3705 | if (!chip->onfi_version || |
| 3706 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3707 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Mylène Josserand | c21946b | 2018-07-13 18:10:23 +0200 | [diff] [blame] | 3708 | return -ENOTSUPP; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3709 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3710 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3711 | return nand_get_features_op(chip, addr, subfeature_param); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3712 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3713 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3714 | /* Set default functions */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3715 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
| 3716 | { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3717 | /* check for proper chip_delay setup, set 20us if not */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3718 | if (!chip->chip_delay) |
| 3719 | chip->chip_delay = 20; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3720 | |
| 3721 | /* check, if a user supplied command function given */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3722 | if (chip->cmdfunc == NULL) |
| 3723 | chip->cmdfunc = nand_command; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3724 | |
| 3725 | /* check, if a user supplied wait function given */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3726 | if (chip->waitfunc == NULL) |
| 3727 | chip->waitfunc = nand_wait; |
| 3728 | |
| 3729 | if (!chip->select_chip) |
| 3730 | chip->select_chip = nand_select_chip; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3731 | |
| 3732 | /* set for ONFI nand */ |
| 3733 | if (!chip->onfi_set_features) |
| 3734 | chip->onfi_set_features = nand_onfi_set_features; |
| 3735 | if (!chip->onfi_get_features) |
| 3736 | chip->onfi_get_features = nand_onfi_get_features; |
| 3737 | |
| 3738 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 3739 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3740 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 3741 | if (!chip->read_word) |
| 3742 | chip->read_word = nand_read_word; |
| 3743 | if (!chip->block_bad) |
| 3744 | chip->block_bad = nand_block_bad; |
| 3745 | if (!chip->block_markbad) |
| 3746 | chip->block_markbad = nand_default_block_markbad; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3747 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3748 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3749 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 3750 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
| 3751 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3752 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 3753 | |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 3754 | #ifndef CONFIG_XPL_BUILD |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3755 | if (!chip->scan_bbt) |
| 3756 | chip->scan_bbt = nand_default_bbt; |
Roger Quadros | b590f61 | 2022-12-20 12:21:57 +0200 | [diff] [blame] | 3757 | #endif |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3758 | |
| 3759 | if (!chip->controller) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3760 | chip->controller = &chip->hwcontrol; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3761 | spin_lock_init(&chip->controller->lock); |
| 3762 | init_waitqueue_head(&chip->controller->wq); |
| 3763 | } |
| 3764 | |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 3765 | if (!chip->buf_align) |
| 3766 | chip->buf_align = 1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3767 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3768 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3769 | /* Sanitize ONFI strings so we can safely print them */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3770 | static void sanitize_string(char *s, size_t len) |
| 3771 | { |
| 3772 | ssize_t i; |
| 3773 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3774 | /* Null terminate */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3775 | s[len - 1] = 0; |
| 3776 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3777 | /* Remove non printable chars */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3778 | for (i = 0; i < len - 1; i++) { |
| 3779 | if (s[i] < ' ' || s[i] > 127) |
| 3780 | s[i] = '?'; |
| 3781 | } |
| 3782 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3783 | /* Remove trailing spaces */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3784 | strim(s); |
| 3785 | } |
| 3786 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3787 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 3788 | { |
| 3789 | int i; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3790 | while (len--) { |
| 3791 | crc ^= *p++ << 8; |
| 3792 | for (i = 0; i < 8; i++) |
| 3793 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 3794 | } |
| 3795 | |
| 3796 | return crc; |
| 3797 | } |
| 3798 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3799 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3800 | /* Parse the Extended Parameter Page. */ |
| 3801 | static int nand_flash_detect_ext_param_page(struct mtd_info *mtd, |
| 3802 | struct nand_chip *chip, struct nand_onfi_params *p) |
| 3803 | { |
| 3804 | struct onfi_ext_param_page *ep; |
| 3805 | struct onfi_ext_section *s; |
| 3806 | struct onfi_ext_ecc_info *ecc; |
| 3807 | uint8_t *cursor; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3808 | int ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3809 | int len; |
| 3810 | int i; |
| 3811 | |
| 3812 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 3813 | ep = kmalloc(len, GFP_KERNEL); |
| 3814 | if (!ep) |
| 3815 | return -ENOMEM; |
| 3816 | |
| 3817 | /* Send our own NAND_CMD_PARAM. */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3818 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 3819 | if (ret) |
| 3820 | goto ext_out; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3821 | |
| 3822 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3823 | ret = nand_change_read_column_op(chip, |
| 3824 | sizeof(*p) * p->num_of_param_pages, |
| 3825 | ep, len, true); |
| 3826 | if (ret) |
| 3827 | goto ext_out; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3828 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3829 | ret = -EINVAL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3830 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 3831 | != le16_to_cpu(ep->crc))) { |
| 3832 | pr_debug("fail in the CRC.\n"); |
| 3833 | goto ext_out; |
| 3834 | } |
| 3835 | |
| 3836 | /* |
| 3837 | * Check the signature. |
| 3838 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 3839 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3840 | if (strncmp((char *)ep->sig, "EPPS", 4)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3841 | pr_debug("The signature is invalid.\n"); |
| 3842 | goto ext_out; |
| 3843 | } |
| 3844 | |
| 3845 | /* find the ECC section. */ |
| 3846 | cursor = (uint8_t *)(ep + 1); |
| 3847 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 3848 | s = ep->sections + i; |
| 3849 | if (s->type == ONFI_SECTION_TYPE_2) |
| 3850 | break; |
| 3851 | cursor += s->length * 16; |
| 3852 | } |
| 3853 | if (i == ONFI_EXT_SECTION_MAX) { |
| 3854 | pr_debug("We can not find the ECC section.\n"); |
| 3855 | goto ext_out; |
| 3856 | } |
| 3857 | |
| 3858 | /* get the info we want. */ |
| 3859 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 3860 | |
| 3861 | if (!ecc->codeword_size) { |
| 3862 | pr_debug("Invalid codeword size\n"); |
| 3863 | goto ext_out; |
| 3864 | } |
| 3865 | |
| 3866 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3867 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 3868 | ret = 0; |
| 3869 | |
| 3870 | ext_out: |
| 3871 | kfree(ep); |
| 3872 | return ret; |
| 3873 | } |
| 3874 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3875 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3876 | * 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] | 3877 | */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 3878 | 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] | 3879 | { |
| 3880 | struct nand_onfi_params *p = &chip->onfi_params; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3881 | char id[4]; |
| 3882 | int i, ret, val; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3883 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3884 | /* Try ONFI for unknown chip or LP */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3885 | ret = nand_readid_op(chip, 0x20, id, sizeof(id)); |
| 3886 | if (ret || strncmp(id, "ONFI", 4)) |
| 3887 | return 0; |
| 3888 | |
| 3889 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 3890 | if (ret) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3891 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3892 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3893 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3894 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 3895 | if (ret) |
| 3896 | return 0; |
| 3897 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3898 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3899 | le16_to_cpu(p->crc)) { |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3900 | break; |
| 3901 | } |
| 3902 | } |
| 3903 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3904 | if (i == 3) { |
| 3905 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3906 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3907 | } |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3908 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3909 | /* Check version */ |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3910 | val = le16_to_cpu(p->revision); |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3911 | if (val & (1 << 5)) |
| 3912 | chip->onfi_version = 23; |
| 3913 | else if (val & (1 << 4)) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3914 | chip->onfi_version = 22; |
| 3915 | else if (val & (1 << 3)) |
| 3916 | chip->onfi_version = 21; |
| 3917 | else if (val & (1 << 2)) |
| 3918 | chip->onfi_version = 20; |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3919 | else if (val & (1 << 1)) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3920 | chip->onfi_version = 10; |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3921 | |
| 3922 | if (!chip->onfi_version) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3923 | pr_info("unsupported ONFI version: %d\n", val); |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3924 | return 0; |
| 3925 | } |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3926 | |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3927 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3928 | sanitize_string(p->model, sizeof(p->model)); |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3929 | if (!mtd->name) |
| 3930 | mtd->name = p->model; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3931 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3932 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3933 | |
| 3934 | /* |
| 3935 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 3936 | * (don't ask me who thought of this...). MTD assumes that these |
| 3937 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 3938 | */ |
| 3939 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3940 | mtd->erasesize *= mtd->writesize; |
| 3941 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3942 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3943 | |
| 3944 | /* See erasesize comment */ |
| 3945 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | b20b6f2 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 3946 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3947 | chip->bits_per_cell = p->bits_per_cell; |
| 3948 | |
| 3949 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 3950 | chip->options |= NAND_BUSWIDTH_16; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3951 | |
| 3952 | if (p->ecc_bits != 0xff) { |
| 3953 | chip->ecc_strength_ds = p->ecc_bits; |
| 3954 | chip->ecc_step_ds = 512; |
| 3955 | } else if (chip->onfi_version >= 21 && |
| 3956 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 3957 | |
| 3958 | /* |
| 3959 | * The nand_flash_detect_ext_param_page() uses the |
| 3960 | * Change Read Column command which maybe not supported |
| 3961 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 3962 | * now. We do not replace user supplied command function. |
| 3963 | */ |
| 3964 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3965 | chip->cmdfunc = nand_command_lp; |
| 3966 | |
| 3967 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
| 3968 | if (nand_flash_detect_ext_param_page(mtd, chip, p)) |
| 3969 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 3970 | } else { |
| 3971 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
| 3972 | } |
| 3973 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3974 | return 1; |
| 3975 | } |
| 3976 | #else |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 3977 | 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] | 3978 | { |
| 3979 | return 0; |
| 3980 | } |
| 3981 | #endif |
| 3982 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3983 | /* |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3984 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 3985 | */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 3986 | 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] | 3987 | { |
| 3988 | struct nand_jedec_params *p = &chip->jedec_params; |
| 3989 | struct jedec_ecc_info *ecc; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3990 | char id[5]; |
| 3991 | int i, val, ret; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3992 | |
| 3993 | /* Try JEDEC for unknown chip or LP */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3994 | ret = nand_readid_op(chip, 0x40, id, sizeof(id)); |
| 3995 | if (ret || strncmp(id, "JEDEC", sizeof(id))) |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3996 | return 0; |
| 3997 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3998 | ret = nand_read_param_page_op(chip, 0x40, NULL, 0); |
| 3999 | if (ret) |
| 4000 | return 0; |
| 4001 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4002 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4003 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 4004 | if (ret) |
| 4005 | return 0; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4006 | |
| 4007 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 4008 | le16_to_cpu(p->crc)) |
| 4009 | break; |
| 4010 | } |
| 4011 | |
| 4012 | if (i == 3) { |
| 4013 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 4014 | return 0; |
| 4015 | } |
| 4016 | |
| 4017 | /* Check version */ |
| 4018 | val = le16_to_cpu(p->revision); |
| 4019 | if (val & (1 << 2)) |
| 4020 | chip->jedec_version = 10; |
| 4021 | else if (val & (1 << 1)) |
| 4022 | chip->jedec_version = 1; /* vendor specific version */ |
| 4023 | |
| 4024 | if (!chip->jedec_version) { |
| 4025 | pr_info("unsupported JEDEC version: %d\n", val); |
| 4026 | return 0; |
| 4027 | } |
| 4028 | |
| 4029 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 4030 | sanitize_string(p->model, sizeof(p->model)); |
| 4031 | if (!mtd->name) |
| 4032 | mtd->name = p->model; |
| 4033 | |
| 4034 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 4035 | |
| 4036 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 4037 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 4038 | mtd->erasesize *= mtd->writesize; |
| 4039 | |
| 4040 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 4041 | |
| 4042 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 4043 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 4044 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 4045 | chip->bits_per_cell = p->bits_per_cell; |
| 4046 | |
| 4047 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4048 | chip->options |= NAND_BUSWIDTH_16; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4049 | |
| 4050 | /* ECC info */ |
| 4051 | ecc = &p->ecc_info[0]; |
| 4052 | |
| 4053 | if (ecc->codeword_size >= 9) { |
| 4054 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 4055 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 4056 | } else { |
| 4057 | pr_warn("Invalid codeword size\n"); |
| 4058 | } |
| 4059 | |
| 4060 | return 1; |
| 4061 | } |
| 4062 | |
| 4063 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4064 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 4065 | * @id_data: the ID string |
| 4066 | * @arrlen: the length of the @id_data array |
| 4067 | * @period: the period of repitition |
| 4068 | * |
| 4069 | * Check if an ID string is repeated within a given sequence of bytes at |
| 4070 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4071 | * 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] | 4072 | * if the repetition has a period of @period; otherwise, returns zero. |
| 4073 | */ |
| 4074 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 4075 | { |
| 4076 | int i, j; |
| 4077 | for (i = 0; i < period; i++) |
| 4078 | for (j = i + period; j < arrlen; j += period) |
| 4079 | if (id_data[i] != id_data[j]) |
| 4080 | return 0; |
| 4081 | return 1; |
| 4082 | } |
| 4083 | |
| 4084 | /* |
| 4085 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 4086 | * @id_data: the ID string |
| 4087 | * @arrlen: the length of the @id_data array |
| 4088 | |
| 4089 | * Returns the length of the ID string, according to known wraparound/trailing |
| 4090 | * zero patterns. If no pattern exists, returns the length of the array. |
| 4091 | */ |
| 4092 | static int nand_id_len(u8 *id_data, int arrlen) |
| 4093 | { |
| 4094 | int last_nonzero, period; |
| 4095 | |
| 4096 | /* Find last non-zero byte */ |
| 4097 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 4098 | if (id_data[last_nonzero]) |
| 4099 | break; |
| 4100 | |
| 4101 | /* All zeros */ |
| 4102 | if (last_nonzero < 0) |
| 4103 | return 0; |
| 4104 | |
| 4105 | /* Calculate wraparound period */ |
| 4106 | for (period = 1; period < arrlen; period++) |
| 4107 | if (nand_id_has_period(id_data, arrlen, period)) |
| 4108 | break; |
| 4109 | |
| 4110 | /* There's a repeated pattern */ |
| 4111 | if (period < arrlen) |
| 4112 | return period; |
| 4113 | |
| 4114 | /* There are trailing zeros */ |
| 4115 | if (last_nonzero < arrlen - 1) |
| 4116 | return last_nonzero + 1; |
| 4117 | |
| 4118 | /* No pattern detected */ |
| 4119 | return arrlen; |
| 4120 | } |
| 4121 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4122 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 4123 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 4124 | { |
| 4125 | int bits; |
| 4126 | |
| 4127 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 4128 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 4129 | return bits + 1; |
| 4130 | } |
| 4131 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4132 | /* |
| 4133 | * Many new NAND share similar device ID codes, which represent the size of the |
| 4134 | * chip. The rest of the parameters must be decoded according to generic or |
| 4135 | * manufacturer-specific "extended ID" decoding patterns. |
| 4136 | */ |
Michael Trimarchi | 60f26dc | 2022-07-20 18:22:08 +0200 | [diff] [blame] | 4137 | void nand_decode_ext_id(struct nand_chip *chip) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4138 | { |
Alexander Dahl | aa12453 | 2024-03-20 10:02:09 +0100 | [diff] [blame] | 4139 | struct mtd_info *mtd = nand_to_mtd(chip); |
Michael Trimarchi | 3ba671b | 2022-07-20 18:22:11 +0200 | [diff] [blame] | 4140 | int extid; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4141 | /* The 3rd id byte holds MLC / multichip data */ |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4142 | 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] | 4143 | /* The 4th id byte is the important one */ |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4144 | extid = chip->id.data[3]; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4145 | |
Michael Trimarchi | 3dc9060 | 2022-07-20 18:22:10 +0200 | [diff] [blame] | 4146 | /* Calc pagesize */ |
| 4147 | mtd->writesize = 1024 << (extid & 0x03); |
| 4148 | extid >>= 2; |
| 4149 | /* Calc oobsize */ |
| 4150 | mtd->oobsize = (8 << (extid & 0x01)) * |
| 4151 | (mtd->writesize >> 9); |
| 4152 | extid >>= 2; |
| 4153 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 4154 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 4155 | extid >>= 2; |
| 4156 | /* Get buswidth information */ |
| 4157 | /* Get buswidth information */ |
| 4158 | if (extid & 0x1) |
| 4159 | chip->options |= NAND_BUSWIDTH_16; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4160 | } |
Michael Trimarchi | 60f26dc | 2022-07-20 18:22:08 +0200 | [diff] [blame] | 4161 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4162 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4163 | /* |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4164 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 4165 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 4166 | * table. |
| 4167 | */ |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4168 | static void nand_manufacturer_detect(struct nand_chip *chip) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4169 | { |
| 4170 | /* |
| 4171 | * Try manufacturer detection if available and use |
| 4172 | * nand_decode_ext_id() otherwise. |
| 4173 | */ |
| 4174 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Michael Trimarchi | 39c5bef | 2022-10-21 08:05:36 +0200 | [diff] [blame] | 4175 | chip->manufacturer.desc->ops->detect) { |
| 4176 | /* The 3rd id byte holds MLC / multichip data */ |
| 4177 | 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] | 4178 | chip->manufacturer.desc->ops->detect(chip); |
Michael Trimarchi | 39c5bef | 2022-10-21 08:05:36 +0200 | [diff] [blame] | 4179 | } else { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4180 | nand_decode_ext_id(chip); |
Michael Trimarchi | 39c5bef | 2022-10-21 08:05:36 +0200 | [diff] [blame] | 4181 | } |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | /* |
| 4185 | * Manufacturer initialization. This function is called for all NANDs including |
| 4186 | * ONFI and JEDEC compliant ones. |
| 4187 | * Manufacturer drivers should put all their specific initialization code in |
| 4188 | * their ->init() hook. |
| 4189 | */ |
| 4190 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 4191 | { |
| 4192 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 4193 | !chip->manufacturer.desc->ops->init) |
| 4194 | return 0; |
| 4195 | |
| 4196 | return chip->manufacturer.desc->ops->init(chip); |
| 4197 | } |
| 4198 | |
| 4199 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4200 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 4201 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 4202 | * the chip. |
| 4203 | */ |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4204 | 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] | 4205 | { |
Alexander Dahl | aa12453 | 2024-03-20 10:02:09 +0100 | [diff] [blame] | 4206 | struct mtd_info *mtd = nand_to_mtd(chip); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4207 | |
| 4208 | mtd->erasesize = type->erasesize; |
| 4209 | mtd->writesize = type->pagesize; |
| 4210 | mtd->oobsize = mtd->writesize / 32; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4211 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4212 | /* All legacy ID NAND are small-page, SLC */ |
| 4213 | chip->bits_per_cell = 1; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4216 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4217 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 4218 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 4219 | * page size, cell-type information). |
| 4220 | */ |
| 4221 | static void nand_decode_bbm_options(struct mtd_info *mtd, |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4222 | struct nand_chip *chip) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4223 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4224 | /* Set the bad block position */ |
| 4225 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 4226 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 4227 | else |
| 4228 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4231 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 4232 | { |
| 4233 | return type->id_len; |
| 4234 | } |
| 4235 | |
| 4236 | 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] | 4237 | struct nand_flash_dev *type) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4238 | { |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4239 | if (!strncmp((char *)type->id, (char *)chip->id.data, type->id_len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4240 | mtd->writesize = type->pagesize; |
| 4241 | mtd->erasesize = type->erasesize; |
| 4242 | mtd->oobsize = type->oobsize; |
| 4243 | |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4244 | 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] | 4245 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 4246 | chip->options |= type->options; |
| 4247 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 4248 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4249 | chip->onfi_timing_mode_default = |
| 4250 | type->onfi_timing_mode_default; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4251 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4252 | if (!mtd->name) |
| 4253 | mtd->name = type->name; |
| 4254 | |
| 4255 | return true; |
| 4256 | } |
| 4257 | return false; |
| 4258 | } |
| 4259 | |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4260 | /** |
| 4261 | * nand_get_manufacturer_desc - Get manufacturer information from the |
| 4262 | * manufacturer ID |
| 4263 | * @id: manufacturer ID |
| 4264 | * |
| 4265 | * Returns a nand_manufacturer_desc object if the manufacturer is defined |
| 4266 | * in the NAND manufacturers database, NULL otherwise. |
| 4267 | */ |
Michael Trimarchi | 25bb179 | 2022-07-26 18:33:11 +0200 | [diff] [blame] | 4268 | static const struct nand_manufacturer *nand_get_manufacturer_desc(u8 id) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4269 | { |
| 4270 | int i; |
| 4271 | |
| 4272 | for (i = 0; nand_manuf_ids[i].id != 0x0; i++) { |
| 4273 | if (nand_manuf_ids[i].id == id) |
| 4274 | return &nand_manuf_ids[i]; |
| 4275 | } |
| 4276 | |
| 4277 | return NULL; |
| 4278 | } |
| 4279 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4280 | /* |
| 4281 | * 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] | 4282 | */ |
Michael Trimarchi | f20a6f0 | 2022-07-25 10:18:51 +0200 | [diff] [blame] | 4283 | int nand_detect(struct nand_chip *chip, int *maf_id, |
| 4284 | int *dev_id, struct nand_flash_dev *type) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4285 | { |
Alexander Dahl | aa12453 | 2024-03-20 10:02:09 +0100 | [diff] [blame] | 4286 | struct mtd_info *mtd = nand_to_mtd(chip); |
Michael Trimarchi | 25bb179 | 2022-07-26 18:33:11 +0200 | [diff] [blame] | 4287 | const struct nand_manufacturer *manufacturer_desc; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4288 | int busw, ret; |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4289 | u8 *id_data = chip->id.data; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4290 | |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4291 | /* |
| 4292 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4293 | * after power-up. |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4294 | */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4295 | ret = nand_reset(chip, 0); |
| 4296 | if (ret) |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4297 | return ret; |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4298 | |
| 4299 | /* Select the device */ |
| 4300 | chip->select_chip(mtd, 0); |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4301 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4302 | /* Send the command for reading device ID */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4303 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 4304 | if (ret) |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4305 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4306 | |
| 4307 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4308 | *maf_id = id_data[0]; |
| 4309 | *dev_id = id_data[1]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4310 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4311 | /* |
| 4312 | * 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] | 4313 | * interface concerns can cause random data which looks like a |
| 4314 | * possibly credible NAND flash to appear. If the two results do |
| 4315 | * not match, ignore the device completely. |
| 4316 | */ |
| 4317 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4318 | /* Read entire ID string */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4319 | ret = nand_readid_op(chip, 0, id_data, 8); |
| 4320 | if (ret) |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4321 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4322 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4323 | if (id_data[0] != *maf_id || id_data[1] != *dev_id) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4324 | 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] | 4325 | *maf_id, *dev_id, id_data[0], id_data[1]); |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4326 | return -ENODEV; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4327 | } |
| 4328 | |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4329 | chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data)); |
| 4330 | |
| 4331 | /* Try to identify manufacturer */ |
| 4332 | manufacturer_desc = nand_get_manufacturer_desc(*maf_id); |
| 4333 | chip->manufacturer.desc = manufacturer_desc; |
| 4334 | |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4335 | if (!type) |
| 4336 | type = nand_flash_ids; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4337 | |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4338 | /* |
| 4339 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 4340 | * override it. |
| 4341 | * This is required to make sure initial NAND bus width set by the |
| 4342 | * NAND controller driver is coherent with the real NAND bus width |
| 4343 | * (extracted by auto-detection code). |
| 4344 | */ |
| 4345 | busw = chip->options & NAND_BUSWIDTH_16; |
| 4346 | |
| 4347 | /* |
| 4348 | * The flag is only set (never cleared), reset it to its default value |
| 4349 | * before starting auto-detection. |
| 4350 | */ |
| 4351 | chip->options &= ~NAND_BUSWIDTH_16; |
| 4352 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4353 | for (; type->name != NULL; type++) { |
| 4354 | if (is_full_id_nand(type)) { |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4355 | if (find_full_id_nand(mtd, chip, type)) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4356 | goto ident_done; |
| 4357 | } else if (*dev_id == type->dev_id) { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4358 | break; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4359 | } |
| 4360 | } |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4361 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4362 | chip->onfi_version = 0; |
| 4363 | if (!type->name || !type->pagesize) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4364 | /* Check if the chip is ONFI compliant */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4365 | if (nand_flash_detect_onfi(mtd, chip)) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4366 | goto ident_done; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4367 | |
| 4368 | /* Check if the chip is JEDEC compliant */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4369 | if (nand_flash_detect_jedec(mtd, chip)) |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4370 | goto ident_done; |
Florian Fainelli | d619189 | 2010-06-12 20:59:25 +0200 | [diff] [blame] | 4371 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4372 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4373 | if (!type->name) |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4374 | return -ENODEV; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4375 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4376 | if (!mtd->name) |
| 4377 | mtd->name = type->name; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4378 | |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 4379 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4380 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4381 | if (!type->pagesize) { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4382 | nand_manufacturer_detect(chip); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4383 | } else { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4384 | nand_decode_id(chip, type); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4385 | } |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4386 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4387 | /* Get chip options */ |
Marek Vasut | fc41719 | 2012-08-30 13:39:38 +0000 | [diff] [blame] | 4388 | chip->options |= type->options; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4389 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4390 | ident_done: |
| 4391 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4392 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
| 4393 | WARN_ON(chip->options & NAND_BUSWIDTH_16); |
| 4394 | chip->options |= busw; |
| 4395 | nand_set_defaults(chip, busw); |
| 4396 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4397 | /* |
| 4398 | * Check, if buswidth is correct. Hardware drivers should set |
| 4399 | * chip correct! |
| 4400 | */ |
| 4401 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
| 4402 | *maf_id, *dev_id); |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4403 | pr_info("%s %s\n", manufacturer_desc->name, mtd->name); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4404 | pr_warn("bus width %d instead %d bit\n", |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4405 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
| 4406 | busw ? 16 : 8); |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4407 | return -EINVAL; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4408 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4409 | |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4410 | nand_decode_bbm_options(mtd, chip); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4411 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4412 | /* Calculate the address shift from the page size */ |
| 4413 | chip->page_shift = ffs(mtd->writesize) - 1; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4414 | /* Convert chipsize to number of pages per chip -1 */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4415 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4416 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4417 | chip->bbt_erase_shift = chip->phys_erase_shift = |
| 4418 | ffs(mtd->erasesize) - 1; |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 4419 | if (chip->chipsize & 0xffffffff) |
Sandeep Paulraj | 1bc877c | 2009-11-07 14:24:06 -0500 | [diff] [blame] | 4420 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4421 | else { |
| 4422 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 4423 | chip->chip_shift += 32 - 1; |
| 4424 | } |
| 4425 | |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 4426 | if (chip->chip_shift - chip->page_shift > 16) |
| 4427 | chip->options |= NAND_ROW_ADDR_3; |
| 4428 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4429 | chip->badblockbits = 8; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4430 | chip->erase = single_erase; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4431 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4432 | /* Do not replace user supplied command function! */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4433 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 4434 | chip->cmdfunc = nand_command_lp; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4435 | |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4436 | ret = nand_manufacturer_init(chip); |
| 4437 | if (ret) |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4438 | return ret; |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4439 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4440 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
| 4441 | *maf_id, *dev_id); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4442 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4443 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4444 | if (chip->onfi_version) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4445 | pr_info("%s %s\n", manufacturer_desc->name, |
| 4446 | chip->onfi_params.model); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4447 | else if (chip->jedec_version) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4448 | pr_info("%s %s\n", manufacturer_desc->name, |
| 4449 | chip->jedec_params.model); |
Sean Anderson | e476f11 | 2023-11-04 16:37:40 -0400 | [diff] [blame] | 4450 | else if (manufacturer_desc) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4451 | pr_info("%s %s\n", manufacturer_desc->name, type->name); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4452 | #else |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4453 | if (chip->jedec_version) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4454 | pr_info("%s %s\n", manufacturer_desc->name, |
| 4455 | chip->jedec_params.model); |
Sean Anderson | e476f11 | 2023-11-04 16:37:40 -0400 | [diff] [blame] | 4456 | else if (manufacturer_desc) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4457 | pr_info("%s %s\n", manufacturer_desc->name, type->name); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4458 | #endif |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4459 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4460 | 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] | 4461 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4462 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4463 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4464 | } |
Michael Trimarchi | f20a6f0 | 2022-07-25 10:18:51 +0200 | [diff] [blame] | 4465 | EXPORT_SYMBOL(nand_detect); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4466 | |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4467 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4468 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4469 | 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] | 4470 | { |
| 4471 | int ret, ecc_mode = -1, ecc_strength, ecc_step; |
Linus Walleij | 4e8611a | 2023-04-07 15:40:05 +0200 | [diff] [blame] | 4472 | int ecc_algo = NAND_ECC_UNKNOWN; |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4473 | const char *str; |
| 4474 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4475 | ret = ofnode_read_s32_default(node, "nand-bus-width", -1); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4476 | if (ret == 16) |
| 4477 | chip->options |= NAND_BUSWIDTH_16; |
| 4478 | |
Arseniy Krasnov | 8b4a27a | 2024-08-26 16:17:08 +0300 | [diff] [blame] | 4479 | if (ofnode_read_bool(node, "nand-is-boot-medium")) |
| 4480 | chip->options |= NAND_IS_BOOT_MEDIUM; |
| 4481 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4482 | if (ofnode_read_bool(node, "nand-on-flash-bbt")) |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4483 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4484 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4485 | str = ofnode_read_string(node, "nand-ecc-mode"); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4486 | if (str) { |
| 4487 | if (!strcmp(str, "none")) |
| 4488 | ecc_mode = NAND_ECC_NONE; |
| 4489 | else if (!strcmp(str, "soft")) |
| 4490 | ecc_mode = NAND_ECC_SOFT; |
| 4491 | else if (!strcmp(str, "hw")) |
| 4492 | ecc_mode = NAND_ECC_HW; |
| 4493 | else if (!strcmp(str, "hw_syndrome")) |
| 4494 | ecc_mode = NAND_ECC_HW_SYNDROME; |
| 4495 | else if (!strcmp(str, "hw_oob_first")) |
| 4496 | ecc_mode = NAND_ECC_HW_OOB_FIRST; |
| 4497 | else if (!strcmp(str, "soft_bch")) |
| 4498 | ecc_mode = NAND_ECC_SOFT_BCH; |
| 4499 | } |
| 4500 | |
Linus Walleij | 4e8611a | 2023-04-07 15:40:05 +0200 | [diff] [blame] | 4501 | str = ofnode_read_string(node, "nand-ecc-algo"); |
| 4502 | if (str) { |
| 4503 | /* |
| 4504 | * If we are in NAND_ECC_SOFT mode, just alter the |
| 4505 | * soft mode to BCH here. No change of algorithm. |
| 4506 | */ |
| 4507 | if (ecc_mode == NAND_ECC_SOFT) { |
| 4508 | if (!strcmp(str, "bch")) |
| 4509 | ecc_mode = NAND_ECC_SOFT_BCH; |
| 4510 | } else { |
| 4511 | if (!strcmp(str, "bch")) { |
| 4512 | ecc_algo = NAND_ECC_BCH; |
| 4513 | } else if (!strcmp(str, "hamming")) { |
| 4514 | ecc_algo = NAND_ECC_HAMMING; |
| 4515 | } |
| 4516 | } |
Pali Rohár | 44acf8a | 2022-04-04 18:17:21 +0200 | [diff] [blame] | 4517 | } |
| 4518 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4519 | ecc_strength = ofnode_read_s32_default(node, |
| 4520 | "nand-ecc-strength", -1); |
| 4521 | ecc_step = ofnode_read_s32_default(node, |
| 4522 | "nand-ecc-step-size", -1); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4523 | |
| 4524 | if ((ecc_step >= 0 && !(ecc_strength >= 0)) || |
| 4525 | (!(ecc_step >= 0) && ecc_strength >= 0)) { |
| 4526 | pr_err("must set both strength and step size in DT\n"); |
| 4527 | return -EINVAL; |
| 4528 | } |
| 4529 | |
Linus Walleij | 4e8611a | 2023-04-07 15:40:05 +0200 | [diff] [blame] | 4530 | /* |
| 4531 | * Chip drivers may have assigned default algorithms here, |
| 4532 | * onlt override it if we have found something explicitly |
| 4533 | * specified in the device tree. |
| 4534 | */ |
| 4535 | if (ecc_algo != NAND_ECC_UNKNOWN) |
| 4536 | chip->ecc.algo = ecc_algo; |
| 4537 | |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4538 | if (ecc_mode >= 0) |
| 4539 | chip->ecc.mode = ecc_mode; |
| 4540 | |
| 4541 | if (ecc_strength >= 0) |
| 4542 | chip->ecc.strength = ecc_strength; |
| 4543 | |
| 4544 | if (ecc_step > 0) |
| 4545 | chip->ecc.size = ecc_step; |
| 4546 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4547 | if (ofnode_read_bool(node, "nand-ecc-maximize")) |
Boris Brezillon | f1a54b0 | 2017-11-22 02:38:13 +0900 | [diff] [blame] | 4548 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4549 | |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4550 | return 0; |
| 4551 | } |
| 4552 | #else |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4553 | 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] | 4554 | { |
| 4555 | return 0; |
| 4556 | } |
| 4557 | #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ |
| 4558 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4559 | /** |
| 4560 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4561 | * @mtd: MTD device structure |
| 4562 | * @maxchips: number of chips to scan for |
| 4563 | * @table: alternative NAND ID table |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4564 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4565 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4566 | * flash ID and sets up MTD fields accordingly. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4567 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4568 | */ |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4569 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4570 | struct nand_flash_dev *table) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4571 | { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4572 | int i, nand_maf_id, nand_dev_id; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4573 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4574 | int ret; |
| 4575 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4576 | if (ofnode_valid(chip->flash_node)) { |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4577 | ret = nand_dt_init(mtd, chip, chip->flash_node); |
| 4578 | if (ret) |
| 4579 | return ret; |
| 4580 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4581 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4582 | /* Set the default functions */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4583 | nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4584 | |
| 4585 | /* Read the flash type */ |
Michael Trimarchi | f20a6f0 | 2022-07-25 10:18:51 +0200 | [diff] [blame] | 4586 | ret = nand_detect(chip, &nand_maf_id, &nand_dev_id, table); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4587 | |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4588 | if (ret) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4589 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
| 4590 | pr_warn("No NAND device found\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4591 | chip->select_chip(mtd, -1); |
Michael Trimarchi | 4d4862d9 | 2022-07-25 10:06:06 +0200 | [diff] [blame] | 4592 | return ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4593 | } |
| 4594 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4595 | /* Initialize the ->data_interface field. */ |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 4596 | ret = nand_init_data_interface(chip); |
| 4597 | if (ret) |
| 4598 | return ret; |
| 4599 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4600 | /* |
| 4601 | * Setup the data interface correctly on the chip and controller side. |
| 4602 | * This explicit call to nand_setup_data_interface() is only required |
| 4603 | * for the first die, because nand_reset() has been called before |
| 4604 | * ->data_interface and ->default_onfi_timing_mode were set. |
| 4605 | * For the other dies, nand_reset() will automatically switch to the |
| 4606 | * best mode for us. |
| 4607 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 4608 | ret = nand_setup_data_interface(chip, 0); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4609 | if (ret) |
| 4610 | return ret; |
| 4611 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4612 | chip->select_chip(mtd, -1); |
| 4613 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4614 | /* Check for a chip array */ |
| 4615 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4616 | u8 id[2]; |
| 4617 | |
Michael Trimarchi | f20a6f0 | 2022-07-25 10:18:51 +0200 | [diff] [blame] | 4618 | /* See comment in nand_detect for reset */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4619 | nand_reset(chip, i); |
| 4620 | |
| 4621 | chip->select_chip(mtd, i); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4622 | /* Send the command for reading device ID */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4623 | nand_readid_op(chip, 0, id, sizeof(id)); |
| 4624 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4625 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4626 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4627 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4628 | break; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4629 | } |
| 4630 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4631 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4632 | |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 4633 | #ifdef DEBUG |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4634 | if (i > 1) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4635 | pr_info("%d chips detected\n", i); |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 4636 | #endif |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4637 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4638 | /* Store the number of chips and calc total size for mtd */ |
| 4639 | chip->numchips = i; |
| 4640 | mtd->size = i * chip->chipsize; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4641 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4642 | return 0; |
| 4643 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4644 | EXPORT_SYMBOL(nand_scan_ident); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4645 | |
Masahiro Yamada | 820eb48 | 2017-11-22 02:38:29 +0900 | [diff] [blame] | 4646 | /** |
| 4647 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 4648 | * @chip: nand chip info structure |
| 4649 | * @caps: ECC caps info structure |
| 4650 | * @oobavail: OOB size that the ECC engine can use |
| 4651 | * |
| 4652 | * When ECC step size and strength are already set, check if they are supported |
| 4653 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 4654 | * On success, the calculated ECC bytes is set. |
| 4655 | */ |
| 4656 | int nand_check_ecc_caps(struct nand_chip *chip, |
| 4657 | const struct nand_ecc_caps *caps, int oobavail) |
| 4658 | { |
| 4659 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4660 | const struct nand_ecc_step_info *stepinfo; |
| 4661 | int preset_step = chip->ecc.size; |
| 4662 | int preset_strength = chip->ecc.strength; |
| 4663 | int nsteps, ecc_bytes; |
| 4664 | int i, j; |
| 4665 | |
| 4666 | if (WARN_ON(oobavail < 0)) |
| 4667 | return -EINVAL; |
| 4668 | |
| 4669 | if (!preset_step || !preset_strength) |
| 4670 | return -ENODATA; |
| 4671 | |
| 4672 | nsteps = mtd->writesize / preset_step; |
| 4673 | |
| 4674 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4675 | stepinfo = &caps->stepinfos[i]; |
| 4676 | |
| 4677 | if (stepinfo->stepsize != preset_step) |
| 4678 | continue; |
| 4679 | |
| 4680 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4681 | if (stepinfo->strengths[j] != preset_strength) |
| 4682 | continue; |
| 4683 | |
| 4684 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 4685 | preset_strength); |
| 4686 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4687 | return ecc_bytes; |
| 4688 | |
| 4689 | if (ecc_bytes * nsteps > oobavail) { |
| 4690 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 4691 | preset_step, preset_strength); |
| 4692 | return -ENOSPC; |
| 4693 | } |
| 4694 | |
| 4695 | chip->ecc.bytes = ecc_bytes; |
| 4696 | |
| 4697 | return 0; |
| 4698 | } |
| 4699 | } |
| 4700 | |
| 4701 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 4702 | preset_step, preset_strength); |
| 4703 | |
| 4704 | return -ENOTSUPP; |
| 4705 | } |
| 4706 | EXPORT_SYMBOL_GPL(nand_check_ecc_caps); |
| 4707 | |
| 4708 | /** |
| 4709 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 4710 | * @chip: nand chip info structure |
| 4711 | * @caps: ECC engine caps info structure |
| 4712 | * @oobavail: OOB size that the ECC engine can use |
| 4713 | * |
| 4714 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 4715 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 4716 | * On success, the chosen ECC settings are set. |
| 4717 | */ |
| 4718 | int nand_match_ecc_req(struct nand_chip *chip, |
| 4719 | const struct nand_ecc_caps *caps, int oobavail) |
| 4720 | { |
| 4721 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4722 | const struct nand_ecc_step_info *stepinfo; |
| 4723 | int req_step = chip->ecc_step_ds; |
| 4724 | int req_strength = chip->ecc_strength_ds; |
| 4725 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 4726 | int best_step, best_strength, best_ecc_bytes; |
| 4727 | int best_ecc_bytes_total = INT_MAX; |
| 4728 | int i, j; |
| 4729 | |
| 4730 | if (WARN_ON(oobavail < 0)) |
| 4731 | return -EINVAL; |
| 4732 | |
| 4733 | /* No information provided by the NAND chip */ |
| 4734 | if (!req_step || !req_strength) |
| 4735 | return -ENOTSUPP; |
| 4736 | |
| 4737 | /* number of correctable bits the chip requires in a page */ |
| 4738 | req_corr = mtd->writesize / req_step * req_strength; |
| 4739 | |
| 4740 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4741 | stepinfo = &caps->stepinfos[i]; |
| 4742 | step_size = stepinfo->stepsize; |
| 4743 | |
| 4744 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4745 | strength = stepinfo->strengths[j]; |
| 4746 | |
| 4747 | /* |
| 4748 | * If both step size and strength are smaller than the |
| 4749 | * chip's requirement, it is not easy to compare the |
| 4750 | * resulted reliability. |
| 4751 | */ |
| 4752 | if (step_size < req_step && strength < req_strength) |
| 4753 | continue; |
| 4754 | |
| 4755 | if (mtd->writesize % step_size) |
| 4756 | continue; |
| 4757 | |
| 4758 | nsteps = mtd->writesize / step_size; |
| 4759 | |
| 4760 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4761 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4762 | continue; |
| 4763 | ecc_bytes_total = ecc_bytes * nsteps; |
| 4764 | |
| 4765 | if (ecc_bytes_total > oobavail || |
| 4766 | strength * nsteps < req_corr) |
| 4767 | continue; |
| 4768 | |
| 4769 | /* |
| 4770 | * We assume the best is to meet the chip's requrement |
| 4771 | * with the least number of ECC bytes. |
| 4772 | */ |
| 4773 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 4774 | best_ecc_bytes_total = ecc_bytes_total; |
| 4775 | best_step = step_size; |
| 4776 | best_strength = strength; |
| 4777 | best_ecc_bytes = ecc_bytes; |
| 4778 | } |
| 4779 | } |
| 4780 | } |
| 4781 | |
| 4782 | if (best_ecc_bytes_total == INT_MAX) |
| 4783 | return -ENOTSUPP; |
| 4784 | |
| 4785 | chip->ecc.size = best_step; |
| 4786 | chip->ecc.strength = best_strength; |
| 4787 | chip->ecc.bytes = best_ecc_bytes; |
| 4788 | |
| 4789 | return 0; |
| 4790 | } |
| 4791 | EXPORT_SYMBOL_GPL(nand_match_ecc_req); |
| 4792 | |
| 4793 | /** |
| 4794 | * nand_maximize_ecc - choose the max ECC strength available |
| 4795 | * @chip: nand chip info structure |
| 4796 | * @caps: ECC engine caps info structure |
| 4797 | * @oobavail: OOB size that the ECC engine can use |
| 4798 | * |
| 4799 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 4800 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 4801 | */ |
| 4802 | int nand_maximize_ecc(struct nand_chip *chip, |
| 4803 | const struct nand_ecc_caps *caps, int oobavail) |
| 4804 | { |
| 4805 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4806 | const struct nand_ecc_step_info *stepinfo; |
| 4807 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 4808 | int best_corr = 0; |
| 4809 | int best_step = 0; |
| 4810 | int best_strength, best_ecc_bytes; |
| 4811 | int i, j; |
| 4812 | |
| 4813 | if (WARN_ON(oobavail < 0)) |
| 4814 | return -EINVAL; |
| 4815 | |
| 4816 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4817 | stepinfo = &caps->stepinfos[i]; |
| 4818 | step_size = stepinfo->stepsize; |
| 4819 | |
| 4820 | /* If chip->ecc.size is already set, respect it */ |
| 4821 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 4822 | continue; |
| 4823 | |
| 4824 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4825 | strength = stepinfo->strengths[j]; |
| 4826 | |
| 4827 | if (mtd->writesize % step_size) |
| 4828 | continue; |
| 4829 | |
| 4830 | nsteps = mtd->writesize / step_size; |
| 4831 | |
| 4832 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4833 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4834 | continue; |
| 4835 | |
| 4836 | if (ecc_bytes * nsteps > oobavail) |
| 4837 | continue; |
| 4838 | |
| 4839 | corr = strength * nsteps; |
| 4840 | |
| 4841 | /* |
| 4842 | * If the number of correctable bits is the same, |
| 4843 | * bigger step_size has more reliability. |
| 4844 | */ |
| 4845 | if (corr > best_corr || |
| 4846 | (corr == best_corr && step_size > best_step)) { |
| 4847 | best_corr = corr; |
| 4848 | best_step = step_size; |
| 4849 | best_strength = strength; |
| 4850 | best_ecc_bytes = ecc_bytes; |
| 4851 | } |
| 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | if (!best_corr) |
| 4856 | return -ENOTSUPP; |
| 4857 | |
| 4858 | chip->ecc.size = best_step; |
| 4859 | chip->ecc.strength = best_strength; |
| 4860 | chip->ecc.bytes = best_ecc_bytes; |
| 4861 | |
| 4862 | return 0; |
| 4863 | } |
| 4864 | EXPORT_SYMBOL_GPL(nand_maximize_ecc); |
| 4865 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4866 | /* |
| 4867 | * Check if the chip configuration meet the datasheet requirements. |
| 4868 | |
| 4869 | * If our configuration corrects A bits per B bytes and the minimum |
| 4870 | * required correction level is X bits per Y bytes, then we must ensure |
| 4871 | * both of the following are true: |
| 4872 | * |
| 4873 | * (1) A / B >= X / Y |
| 4874 | * (2) A >= X |
| 4875 | * |
| 4876 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 4877 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 4878 | * in the same sector. |
| 4879 | */ |
| 4880 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 4881 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4882 | struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4883 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4884 | int corr, ds_corr; |
| 4885 | |
| 4886 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 4887 | /* Not enough information */ |
| 4888 | return true; |
| 4889 | |
| 4890 | /* |
| 4891 | * We get the number of corrected bits per page to compare |
| 4892 | * the correction density. |
| 4893 | */ |
| 4894 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 4895 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 4896 | |
| 4897 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 4898 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4899 | |
Marc Gonzalez | c3a2985 | 2017-11-22 02:38:22 +0900 | [diff] [blame] | 4900 | static bool invalid_ecc_page_accessors(struct nand_chip *chip) |
| 4901 | { |
| 4902 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4903 | |
| 4904 | if (nand_standard_page_accessors(ecc)) |
| 4905 | return false; |
| 4906 | |
| 4907 | /* |
| 4908 | * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND |
| 4909 | * controller driver implements all the page accessors because |
| 4910 | * default helpers are not suitable when the core does not |
| 4911 | * send the READ0/PAGEPROG commands. |
| 4912 | */ |
| 4913 | return (!ecc->read_page || !ecc->write_page || |
| 4914 | !ecc->read_page_raw || !ecc->write_page_raw || |
| 4915 | (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) || |
| 4916 | (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage && |
| 4917 | ecc->hwctl && ecc->calculate)); |
| 4918 | } |
| 4919 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4920 | /** |
| 4921 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4922 | * @mtd: MTD device structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4923 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4924 | * This is the second phase of the normal nand_scan() function. It fills out |
| 4925 | * all the uninitialized function pointers with the defaults and scans for a |
| 4926 | * bad block table if appropriate. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4927 | */ |
| 4928 | int nand_scan_tail(struct mtd_info *mtd) |
| 4929 | { |
| 4930 | int i; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4931 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4932 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4933 | struct nand_buffers *nbuf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4934 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4935 | /* New bad blocks should be marked in OOB, flash-based BBT, or both */ |
| 4936 | BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
| 4937 | !(chip->bbt_options & NAND_BBT_USE_FLASH)); |
| 4938 | |
Marc Gonzalez | c3a2985 | 2017-11-22 02:38:22 +0900 | [diff] [blame] | 4939 | if (invalid_ecc_page_accessors(chip)) { |
| 4940 | pr_err("Invalid ECC page accessors setup\n"); |
| 4941 | return -EINVAL; |
| 4942 | } |
| 4943 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4944 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4945 | nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4946 | chip->buffers = nbuf; |
| 4947 | } else { |
| 4948 | if (!chip->buffers) |
| 4949 | return -ENOMEM; |
| 4950 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4951 | |
| 4952 | /* Set the internal oob buffer location, just after the page data */ |
| 4953 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
| 4954 | |
| 4955 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4956 | * If no default placement scheme is given, select an appropriate one. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4957 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4958 | if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4959 | switch (mtd->oobsize) { |
Gregory CLEMENT | e5b9631 | 2019-04-17 11:22:05 +0200 | [diff] [blame] | 4960 | #ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4961 | case 8: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4962 | ecc->layout = &nand_oob_8; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4963 | break; |
| 4964 | case 16: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4965 | ecc->layout = &nand_oob_16; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4966 | break; |
| 4967 | case 64: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4968 | ecc->layout = &nand_oob_64; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4969 | break; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 4970 | case 128: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4971 | ecc->layout = &nand_oob_128; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 4972 | break; |
Stefan Agner | bd18614 | 2018-12-06 14:57:09 +0100 | [diff] [blame] | 4973 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4974 | default: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4975 | pr_warn("No oob scheme defined for oobsize %d\n", |
| 4976 | mtd->oobsize); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4977 | BUG(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4978 | } |
| 4979 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4980 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4981 | if (!chip->write_page) |
| 4982 | chip->write_page = nand_write_page; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4983 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4984 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4985 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4986 | * selected and we have 256 byte pagesize fallback to software ECC |
| 4987 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4988 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4989 | switch (ecc->mode) { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 4990 | case NAND_ECC_HW_OOB_FIRST: |
| 4991 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4992 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4993 | pr_warn("No ECC functions supplied; hardware ECC not possible\n"); |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 4994 | BUG(); |
| 4995 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4996 | if (!ecc->read_page) |
| 4997 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 4998 | |
Andre Przywara | a50e5c6 | 2025-03-27 15:33:10 +0000 | [diff] [blame] | 4999 | fallthrough; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5000 | case NAND_ECC_HW: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5001 | /* Use standard hwecc read page function? */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5002 | if (!ecc->read_page) |
| 5003 | ecc->read_page = nand_read_page_hwecc; |
| 5004 | if (!ecc->write_page) |
| 5005 | ecc->write_page = nand_write_page_hwecc; |
| 5006 | if (!ecc->read_page_raw) |
| 5007 | ecc->read_page_raw = nand_read_page_raw; |
| 5008 | if (!ecc->write_page_raw) |
| 5009 | ecc->write_page_raw = nand_write_page_raw; |
| 5010 | if (!ecc->read_oob) |
| 5011 | ecc->read_oob = nand_read_oob_std; |
| 5012 | if (!ecc->write_oob) |
| 5013 | ecc->write_oob = nand_write_oob_std; |
| 5014 | if (!ecc->read_subpage) |
| 5015 | ecc->read_subpage = nand_read_subpage; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5016 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5017 | ecc->write_subpage = nand_write_subpage_hwecc; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5018 | |
Andre Przywara | a50e5c6 | 2025-03-27 15:33:10 +0000 | [diff] [blame] | 5019 | fallthrough; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5020 | case NAND_ECC_HW_SYNDROME: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5021 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 5022 | (!ecc->read_page || |
| 5023 | ecc->read_page == nand_read_page_hwecc || |
| 5024 | !ecc->write_page || |
| 5025 | ecc->write_page == nand_write_page_hwecc)) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5026 | pr_warn("No ECC functions supplied; hardware ECC not possible\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5027 | BUG(); |
| 5028 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5029 | /* Use standard syndrome read/write page function? */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5030 | if (!ecc->read_page) |
| 5031 | ecc->read_page = nand_read_page_syndrome; |
| 5032 | if (!ecc->write_page) |
| 5033 | ecc->write_page = nand_write_page_syndrome; |
| 5034 | if (!ecc->read_page_raw) |
| 5035 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 5036 | if (!ecc->write_page_raw) |
| 5037 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 5038 | if (!ecc->read_oob) |
| 5039 | ecc->read_oob = nand_read_oob_syndrome; |
| 5040 | if (!ecc->write_oob) |
| 5041 | ecc->write_oob = nand_write_oob_syndrome; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5042 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5043 | if (mtd->writesize >= ecc->size) { |
| 5044 | if (!ecc->strength) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5045 | pr_warn("Driver must set ecc.strength when using hardware ECC\n"); |
| 5046 | BUG(); |
| 5047 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5048 | break; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5049 | } |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5050 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 5051 | ecc->size, mtd->writesize); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5052 | ecc->mode = NAND_ECC_SOFT; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5053 | |
Andre Przywara | a50e5c6 | 2025-03-27 15:33:10 +0000 | [diff] [blame] | 5054 | fallthrough; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5055 | case NAND_ECC_SOFT: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5056 | ecc->calculate = nand_calculate_ecc; |
| 5057 | ecc->correct = nand_correct_data; |
| 5058 | ecc->read_page = nand_read_page_swecc; |
| 5059 | ecc->read_subpage = nand_read_subpage; |
| 5060 | ecc->write_page = nand_write_page_swecc; |
| 5061 | ecc->read_page_raw = nand_read_page_raw; |
| 5062 | ecc->write_page_raw = nand_write_page_raw; |
| 5063 | ecc->read_oob = nand_read_oob_std; |
| 5064 | ecc->write_oob = nand_write_oob_std; |
| 5065 | if (!ecc->size) |
| 5066 | ecc->size = 256; |
| 5067 | ecc->bytes = 3; |
| 5068 | ecc->strength = 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5069 | break; |
| 5070 | |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5071 | case NAND_ECC_SOFT_BCH: |
| 5072 | if (!mtd_nand_has_bch()) { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 5073 | pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5074 | BUG(); |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5075 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5076 | ecc->calculate = nand_bch_calculate_ecc; |
| 5077 | ecc->correct = nand_bch_correct_data; |
| 5078 | ecc->read_page = nand_read_page_swecc; |
| 5079 | ecc->read_subpage = nand_read_subpage; |
| 5080 | ecc->write_page = nand_write_page_swecc; |
| 5081 | ecc->read_page_raw = nand_read_page_raw; |
| 5082 | ecc->write_page_raw = nand_write_page_raw; |
| 5083 | ecc->read_oob = nand_read_oob_std; |
| 5084 | ecc->write_oob = nand_write_oob_std; |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5085 | /* |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5086 | * Board driver should supply ecc.size and ecc.strength values |
| 5087 | * to select how many bits are correctable. Otherwise, default |
| 5088 | * to 4 bits for large page devices. |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5089 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5090 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 5091 | ecc->size = 512; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5092 | ecc->strength = 4; |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5093 | } |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5094 | |
| 5095 | /* See nand_bch_init() for details. */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5096 | ecc->bytes = 0; |
| 5097 | ecc->priv = nand_bch_init(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5098 | if (!ecc->priv) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5099 | pr_warn("BCH ECC initialization failed!\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5100 | BUG(); |
| 5101 | } |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5102 | break; |
| 5103 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5104 | case NAND_ECC_NONE: |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5105 | 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] | 5106 | ecc->read_page = nand_read_page_raw; |
| 5107 | ecc->write_page = nand_write_page_raw; |
| 5108 | ecc->read_oob = nand_read_oob_std; |
| 5109 | ecc->read_page_raw = nand_read_page_raw; |
| 5110 | ecc->write_page_raw = nand_write_page_raw; |
| 5111 | ecc->write_oob = nand_write_oob_std; |
| 5112 | ecc->size = mtd->writesize; |
| 5113 | ecc->bytes = 0; |
| 5114 | ecc->strength = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5115 | break; |
| 5116 | |
| 5117 | default: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5118 | pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5119 | BUG(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5120 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5121 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5122 | /* For many systems, the standard OOB write also works for raw */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5123 | if (!ecc->read_oob_raw) |
| 5124 | ecc->read_oob_raw = ecc->read_oob; |
| 5125 | if (!ecc->write_oob_raw) |
| 5126 | ecc->write_oob_raw = ecc->write_oob; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5127 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5128 | /* |
| 5129 | * The number of bytes available for a client to place data into |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5130 | * the out of band area. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5131 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5132 | mtd->oobavail = 0; |
| 5133 | if (ecc->layout) { |
| 5134 | for (i = 0; ecc->layout->oobfree[i].length; i++) |
| 5135 | mtd->oobavail += ecc->layout->oobfree[i].length; |
| 5136 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5137 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5138 | /* ECC sanity check: warn if it's too weak */ |
| 5139 | if (!nand_ecc_strength_good(mtd)) |
| 5140 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 5141 | mtd->name); |
| 5142 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5143 | /* |
| 5144 | * 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] | 5145 | * mode. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5146 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5147 | ecc->steps = mtd->writesize / ecc->size; |
| 5148 | if (ecc->steps * ecc->size != mtd->writesize) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5149 | pr_warn("Invalid ECC parameters\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5150 | BUG(); |
| 5151 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5152 | ecc->total = ecc->steps * ecc->bytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5153 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5154 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5155 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
| 5156 | switch (ecc->steps) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5157 | case 2: |
| 5158 | mtd->subpage_sft = 1; |
| 5159 | break; |
| 5160 | case 4: |
| 5161 | case 8: |
Sandeep Paulraj | fd9874d | 2009-11-07 14:24:34 -0500 | [diff] [blame] | 5162 | case 16: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5163 | mtd->subpage_sft = 2; |
| 5164 | break; |
| 5165 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5166 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5167 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5168 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5169 | /* Initialize state */ |
| 5170 | chip->state = FL_READY; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5171 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5172 | /* Invalidate the pagebuffer reference */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5173 | chip->pagebuf = -1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5174 | |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 5175 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5176 | switch (ecc->mode) { |
| 5177 | case NAND_ECC_SOFT: |
| 5178 | case NAND_ECC_SOFT_BCH: |
| 5179 | if (chip->page_shift > 9) |
| 5180 | chip->options |= NAND_SUBPAGE_READ; |
| 5181 | break; |
| 5182 | |
| 5183 | default: |
| 5184 | break; |
| 5185 | } |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 5186 | |
Patrice Chotard | bee3287 | 2022-03-21 09:13:36 +0100 | [diff] [blame] | 5187 | mtd->flash_node = chip->flash_node; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5188 | /* Fill in remaining MTD driver data */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5189 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 5190 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 5191 | MTD_CAP_NANDFLASH; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5192 | mtd->_erase = nand_erase; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5193 | mtd->_panic_write = panic_nand_write; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5194 | mtd->_read_oob = nand_read_oob; |
| 5195 | mtd->_write_oob = nand_write_oob; |
| 5196 | mtd->_sync = nand_sync; |
| 5197 | mtd->_lock = NULL; |
| 5198 | mtd->_unlock = NULL; |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 5199 | mtd->_block_isreserved = nand_block_isreserved; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5200 | mtd->_block_isbad = nand_block_isbad; |
| 5201 | mtd->_block_markbad = nand_block_markbad; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5202 | mtd->writebufsize = mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5203 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5204 | /* propagate ecc info to mtd_info */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5205 | mtd->ecclayout = ecc->layout; |
| 5206 | mtd->ecc_strength = ecc->strength; |
| 5207 | mtd->ecc_step_size = ecc->size; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5208 | /* |
| 5209 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 5210 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 5211 | * properly set. |
| 5212 | */ |
| 5213 | if (!mtd->bitflip_threshold) |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5214 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5215 | |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 5216 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5217 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5218 | EXPORT_SYMBOL(nand_scan_tail); |
| 5219 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5220 | /** |
| 5221 | * nand_scan - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5222 | * @mtd: MTD device structure |
| 5223 | * @maxchips: number of chips to scan for |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5224 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5225 | * This fills out all the uninitialized function pointers with the defaults. |
| 5226 | * 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] | 5227 | * appropriate values. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5228 | */ |
| 5229 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 5230 | { |
| 5231 | int ret; |
| 5232 | |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 5233 | ret = nand_scan_ident(mtd, maxchips, NULL); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5234 | if (!ret) |
| 5235 | ret = nand_scan_tail(mtd); |
| 5236 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5237 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5238 | EXPORT_SYMBOL(nand_scan); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5239 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5240 | MODULE_LICENSE("GPL"); |
| 5241 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5242 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
| 5243 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |