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