Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Freescale GPMI NAND Flash Driver |
| 3 | * |
Huang Shijie | 026918e | 2015-12-02 16:47:40 -0600 | [diff] [blame] | 4 | * Copyright (C) 2010-2015 Freescale Semiconductor, Inc. |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 5 | * Copyright (C) 2008 Embedded Alley Solutions, Inc. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/interrupt.h> |
Wolfram Sang | df16c86 | 2011-11-23 15:57:06 +0100 | [diff] [blame] | 24 | #include <linux/module.h> |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 25 | #include <linux/mtd/partitions.h> |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_device.h> |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 28 | #include "gpmi-nand.h" |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 29 | #include "bch-regs.h" |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 30 | |
Huang Shijie | 5de0b52 | 2012-10-13 13:03:29 -0400 | [diff] [blame] | 31 | /* Resource names for the GPMI NAND driver. */ |
| 32 | #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand" |
| 33 | #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch" |
| 34 | #define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch" |
Huang Shijie | 5de0b52 | 2012-10-13 13:03:29 -0400 | [diff] [blame] | 35 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 36 | /* add our owner bbt descriptor */ |
| 37 | static uint8_t scan_ff_pattern[] = { 0xff }; |
| 38 | static struct nand_bbt_descr gpmi_bbt_descr = { |
| 39 | .options = 0, |
| 40 | .offs = 0, |
| 41 | .len = 1, |
| 42 | .pattern = scan_ff_pattern |
| 43 | }; |
| 44 | |
Huang Shijie | 7a2b89a | 2013-09-25 14:58:15 +0800 | [diff] [blame] | 45 | /* |
| 46 | * We may change the layout if we can get the ECC info from the datasheet, |
| 47 | * else we will use all the (page + OOB). |
| 48 | */ |
Boris Brezillon | 3f158e4 | 2016-02-03 20:01:54 +0100 | [diff] [blame] | 49 | static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 50 | struct mtd_oob_region *oobregion) |
| 51 | { |
| 52 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 53 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
| 54 | struct bch_geometry *geo = &this->bch_geometry; |
| 55 | |
| 56 | if (section) |
| 57 | return -ERANGE; |
| 58 | |
| 59 | oobregion->offset = 0; |
| 60 | oobregion->length = geo->page_size - mtd->writesize; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int gpmi_ooblayout_free(struct mtd_info *mtd, int section, |
| 66 | struct mtd_oob_region *oobregion) |
| 67 | { |
| 68 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 69 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
| 70 | struct bch_geometry *geo = &this->bch_geometry; |
| 71 | |
| 72 | if (section) |
| 73 | return -ERANGE; |
| 74 | |
| 75 | /* The available oob size we have. */ |
| 76 | if (geo->page_size < mtd->writesize + mtd->oobsize) { |
| 77 | oobregion->offset = geo->page_size - mtd->writesize; |
| 78 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = { |
| 85 | .ecc = gpmi_ooblayout_ecc, |
| 86 | .free = gpmi_ooblayout_free, |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 87 | }; |
| 88 | |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 89 | static const struct gpmi_devdata gpmi_devdata_imx23 = { |
| 90 | .type = IS_MX23, |
| 91 | .bch_max_ecc_strength = 20, |
| 92 | .max_chain_delay = 16, |
| 93 | }; |
| 94 | |
| 95 | static const struct gpmi_devdata gpmi_devdata_imx28 = { |
| 96 | .type = IS_MX28, |
| 97 | .bch_max_ecc_strength = 20, |
| 98 | .max_chain_delay = 16, |
| 99 | }; |
| 100 | |
| 101 | static const struct gpmi_devdata gpmi_devdata_imx6q = { |
| 102 | .type = IS_MX6Q, |
| 103 | .bch_max_ecc_strength = 40, |
| 104 | .max_chain_delay = 12, |
| 105 | }; |
| 106 | |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 107 | static const struct gpmi_devdata gpmi_devdata_imx6sx = { |
| 108 | .type = IS_MX6SX, |
| 109 | .bch_max_ecc_strength = 62, |
| 110 | .max_chain_delay = 12, |
| 111 | }; |
| 112 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 113 | static irqreturn_t bch_irq(int irq, void *cookie) |
| 114 | { |
| 115 | struct gpmi_nand_data *this = cookie; |
| 116 | |
| 117 | gpmi_clear_bch(this); |
| 118 | complete(&this->bch_done); |
| 119 | return IRQ_HANDLED; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Calculate the ECC strength by hand: |
| 124 | * E : The ECC strength. |
| 125 | * G : the length of Galois Field. |
| 126 | * N : The chunk count of per page. |
| 127 | * O : the oobsize of the NAND chip. |
| 128 | * M : the metasize of per page. |
| 129 | * |
| 130 | * The formula is : |
| 131 | * E * G * N |
| 132 | * ------------ <= (O - M) |
| 133 | * 8 |
| 134 | * |
| 135 | * So, we get E by: |
| 136 | * (O - M) * 8 |
| 137 | * E <= ------------- |
| 138 | * G * N |
| 139 | */ |
| 140 | static inline int get_ecc_strength(struct gpmi_nand_data *this) |
| 141 | { |
| 142 | struct bch_geometry *geo = &this->bch_geometry; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 143 | struct mtd_info *mtd = nand_to_mtd(&this->nand); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 144 | int ecc_strength; |
| 145 | |
| 146 | ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8) |
| 147 | / (geo->gf_len * geo->ecc_chunk_count); |
| 148 | |
| 149 | /* We need the minor even number. */ |
| 150 | return round_down(ecc_strength, 2); |
| 151 | } |
| 152 | |
Huang Shijie | 92d0e09 | 2013-01-29 09:23:38 +0800 | [diff] [blame] | 153 | static inline bool gpmi_check_ecc(struct gpmi_nand_data *this) |
| 154 | { |
| 155 | struct bch_geometry *geo = &this->bch_geometry; |
| 156 | |
| 157 | /* Do the sanity check. */ |
| 158 | if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) { |
| 159 | /* The mx23/mx28 only support the GF13. */ |
| 160 | if (geo->gf_len == 14) |
| 161 | return false; |
Huang Shijie | 92d0e09 | 2013-01-29 09:23:38 +0800 | [diff] [blame] | 162 | } |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 163 | return geo->ecc_strength <= this->devdata->bch_max_ecc_strength; |
Huang Shijie | 92d0e09 | 2013-01-29 09:23:38 +0800 | [diff] [blame] | 164 | } |
| 165 | |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 166 | /* |
| 167 | * If we can get the ECC information from the nand chip, we do not |
| 168 | * need to calculate them ourselves. |
| 169 | * |
| 170 | * We may have available oob space in this case. |
| 171 | */ |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 172 | static int set_geometry_by_ecc_info(struct gpmi_nand_data *this) |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 173 | { |
| 174 | struct bch_geometry *geo = &this->bch_geometry; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 175 | struct nand_chip *chip = &this->nand; |
| 176 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 177 | unsigned int block_mark_bit_offset; |
| 178 | |
| 179 | if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0)) |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 180 | return -EINVAL; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 181 | |
| 182 | switch (chip->ecc_step_ds) { |
| 183 | case SZ_512: |
| 184 | geo->gf_len = 13; |
| 185 | break; |
| 186 | case SZ_1K: |
| 187 | geo->gf_len = 14; |
| 188 | break; |
| 189 | default: |
| 190 | dev_err(this->dev, |
| 191 | "unsupported nand chip. ecc bits : %d, ecc size : %d\n", |
| 192 | chip->ecc_strength_ds, chip->ecc_step_ds); |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 193 | return -EINVAL; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 194 | } |
| 195 | geo->ecc_chunk_size = chip->ecc_step_ds; |
| 196 | geo->ecc_strength = round_up(chip->ecc_strength_ds, 2); |
| 197 | if (!gpmi_check_ecc(this)) |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 198 | return -EINVAL; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 199 | |
| 200 | /* Keep the C >= O */ |
| 201 | if (geo->ecc_chunk_size < mtd->oobsize) { |
| 202 | dev_err(this->dev, |
| 203 | "unsupported nand chip. ecc size: %d, oob size : %d\n", |
| 204 | chip->ecc_step_ds, mtd->oobsize); |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 205 | return -EINVAL; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* The default value, see comment in the legacy_set_geometry(). */ |
| 209 | geo->metadata_size = 10; |
| 210 | |
| 211 | geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size; |
| 212 | |
| 213 | /* |
| 214 | * Now, the NAND chip with 2K page(data chunk is 512byte) shows below: |
| 215 | * |
| 216 | * | P | |
| 217 | * |<----------------------------------------------------->| |
| 218 | * | | |
| 219 | * | (Block Mark) | |
| 220 | * | P' | | | | |
| 221 | * |<-------------------------------------------->| D | | O' | |
| 222 | * | |<---->| |<--->| |
| 223 | * V V V V V |
| 224 | * +---+----------+-+----------+-+----------+-+----------+-+-----+ |
| 225 | * | M | data |E| data |E| data |E| data |E| | |
| 226 | * +---+----------+-+----------+-+----------+-+----------+-+-----+ |
| 227 | * ^ ^ |
| 228 | * | O | |
| 229 | * |<------------>| |
| 230 | * | | |
| 231 | * |
| 232 | * P : the page size for BCH module. |
| 233 | * E : The ECC strength. |
| 234 | * G : the length of Galois Field. |
| 235 | * N : The chunk count of per page. |
| 236 | * M : the metasize of per page. |
| 237 | * C : the ecc chunk size, aka the "data" above. |
| 238 | * P': the nand chip's page size. |
| 239 | * O : the nand chip's oob size. |
| 240 | * O': the free oob. |
| 241 | * |
| 242 | * The formula for P is : |
| 243 | * |
| 244 | * E * G * N |
| 245 | * P = ------------ + P' + M |
| 246 | * 8 |
| 247 | * |
| 248 | * The position of block mark moves forward in the ECC-based view |
| 249 | * of page, and the delta is: |
| 250 | * |
| 251 | * E * G * (N - 1) |
| 252 | * D = (---------------- + M) |
| 253 | * 8 |
| 254 | * |
| 255 | * Please see the comment in legacy_set_geometry(). |
| 256 | * With the condition C >= O , we still can get same result. |
| 257 | * So the bit position of the physical block mark within the ECC-based |
| 258 | * view of the page is : |
| 259 | * (P' - D) * 8 |
| 260 | */ |
| 261 | geo->page_size = mtd->writesize + geo->metadata_size + |
| 262 | (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8; |
| 263 | |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 264 | geo->payload_size = mtd->writesize; |
| 265 | |
| 266 | geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4); |
| 267 | geo->auxiliary_size = ALIGN(geo->metadata_size, 4) |
| 268 | + ALIGN(geo->ecc_chunk_count, 4); |
| 269 | |
| 270 | if (!this->swap_block_mark) |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 271 | return 0; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 272 | |
| 273 | /* For bit swap. */ |
| 274 | block_mark_bit_offset = mtd->writesize * 8 - |
| 275 | (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1) |
| 276 | + geo->metadata_size * 8); |
| 277 | |
| 278 | geo->block_mark_byte_offset = block_mark_bit_offset / 8; |
| 279 | geo->block_mark_bit_offset = block_mark_bit_offset % 8; |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 280 | return 0; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static int legacy_set_geometry(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 284 | { |
| 285 | struct bch_geometry *geo = &this->bch_geometry; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 286 | struct mtd_info *mtd = nand_to_mtd(&this->nand); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 287 | unsigned int metadata_size; |
| 288 | unsigned int status_size; |
| 289 | unsigned int block_mark_bit_offset; |
| 290 | |
| 291 | /* |
| 292 | * The size of the metadata can be changed, though we set it to 10 |
| 293 | * bytes now. But it can't be too large, because we have to save |
| 294 | * enough space for BCH. |
| 295 | */ |
| 296 | geo->metadata_size = 10; |
| 297 | |
| 298 | /* The default for the length of Galois Field. */ |
| 299 | geo->gf_len = 13; |
| 300 | |
Huang Shijie | 9ff16f0 | 2013-01-25 14:04:07 +0800 | [diff] [blame] | 301 | /* The default for chunk size. */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 302 | geo->ecc_chunk_size = 512; |
Huang Shijie | 9ff16f0 | 2013-01-25 14:04:07 +0800 | [diff] [blame] | 303 | while (geo->ecc_chunk_size < mtd->oobsize) { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 304 | geo->ecc_chunk_size *= 2; /* keep C >= O */ |
Huang Shijie | 9ff16f0 | 2013-01-25 14:04:07 +0800 | [diff] [blame] | 305 | geo->gf_len = 14; |
| 306 | } |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 307 | |
| 308 | geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size; |
| 309 | |
| 310 | /* We use the same ECC strength for all chunks. */ |
| 311 | geo->ecc_strength = get_ecc_strength(this); |
Huang Shijie | 92d0e09 | 2013-01-29 09:23:38 +0800 | [diff] [blame] | 312 | if (!gpmi_check_ecc(this)) { |
| 313 | dev_err(this->dev, |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 314 | "ecc strength: %d cannot be supported by the controller (%d)\n" |
| 315 | "try to use minimum ecc strength that NAND chip required\n", |
Lothar Waßmann | d8c0372 | 2014-06-12 15:20:42 +0200 | [diff] [blame] | 316 | geo->ecc_strength, |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 317 | this->devdata->bch_max_ecc_strength); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 318 | return -EINVAL; |
| 319 | } |
| 320 | |
| 321 | geo->page_size = mtd->writesize + mtd->oobsize; |
| 322 | geo->payload_size = mtd->writesize; |
| 323 | |
| 324 | /* |
| 325 | * The auxiliary buffer contains the metadata and the ECC status. The |
| 326 | * metadata is padded to the nearest 32-bit boundary. The ECC status |
| 327 | * contains one byte for every ECC chunk, and is also padded to the |
| 328 | * nearest 32-bit boundary. |
| 329 | */ |
| 330 | metadata_size = ALIGN(geo->metadata_size, 4); |
| 331 | status_size = ALIGN(geo->ecc_chunk_count, 4); |
| 332 | |
| 333 | geo->auxiliary_size = metadata_size + status_size; |
| 334 | geo->auxiliary_status_offset = metadata_size; |
| 335 | |
| 336 | if (!this->swap_block_mark) |
| 337 | return 0; |
| 338 | |
| 339 | /* |
| 340 | * We need to compute the byte and bit offsets of |
| 341 | * the physical block mark within the ECC-based view of the page. |
| 342 | * |
| 343 | * NAND chip with 2K page shows below: |
| 344 | * (Block Mark) |
| 345 | * | | |
| 346 | * | D | |
| 347 | * |<---->| |
| 348 | * V V |
| 349 | * +---+----------+-+----------+-+----------+-+----------+-+ |
| 350 | * | M | data |E| data |E| data |E| data |E| |
| 351 | * +---+----------+-+----------+-+----------+-+----------+-+ |
| 352 | * |
| 353 | * The position of block mark moves forward in the ECC-based view |
| 354 | * of page, and the delta is: |
| 355 | * |
| 356 | * E * G * (N - 1) |
| 357 | * D = (---------------- + M) |
| 358 | * 8 |
| 359 | * |
| 360 | * With the formula to compute the ECC strength, and the condition |
| 361 | * : C >= O (C is the ecc chunk size) |
| 362 | * |
| 363 | * It's easy to deduce to the following result: |
| 364 | * |
| 365 | * E * G (O - M) C - M C - M |
| 366 | * ----------- <= ------- <= -------- < --------- |
| 367 | * 8 N N (N - 1) |
| 368 | * |
| 369 | * So, we get: |
| 370 | * |
| 371 | * E * G * (N - 1) |
| 372 | * D = (---------------- + M) < C |
| 373 | * 8 |
| 374 | * |
| 375 | * The above inequality means the position of block mark |
| 376 | * within the ECC-based view of the page is still in the data chunk, |
| 377 | * and it's NOT in the ECC bits of the chunk. |
| 378 | * |
| 379 | * Use the following to compute the bit position of the |
| 380 | * physical block mark within the ECC-based view of the page: |
| 381 | * (page_size - D) * 8 |
| 382 | * |
| 383 | * --Huang Shijie |
| 384 | */ |
| 385 | block_mark_bit_offset = mtd->writesize * 8 - |
| 386 | (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1) |
| 387 | + geo->metadata_size * 8); |
| 388 | |
| 389 | geo->block_mark_byte_offset = block_mark_bit_offset / 8; |
| 390 | geo->block_mark_bit_offset = block_mark_bit_offset % 8; |
| 391 | return 0; |
| 392 | } |
| 393 | |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 394 | int common_nfc_set_geometry(struct gpmi_nand_data *this) |
| 395 | { |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 396 | if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc")) |
| 397 | || legacy_set_geometry(this)) |
| 398 | return set_geometry_by_ecc_info(this); |
| 399 | |
| 400 | return 0; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 401 | } |
| 402 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 403 | struct dma_chan *get_dma_chan(struct gpmi_nand_data *this) |
| 404 | { |
Huang Shijie | a7c12d0 | 2013-08-27 17:29:05 +0800 | [diff] [blame] | 405 | /* We use the DMA channel 0 to access all the nand chips. */ |
| 406 | return this->dma_chans[0]; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* Can we use the upper's buffer directly for DMA? */ |
| 410 | void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr) |
| 411 | { |
| 412 | struct scatterlist *sgl = &this->data_sgl; |
| 413 | int ret; |
| 414 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 415 | /* first try to map the upper buffer directly */ |
Huang Shijie | 0ff76a9 | 2013-12-18 23:41:00 +0800 | [diff] [blame] | 416 | if (virt_addr_valid(this->upper_buf) && |
| 417 | !object_is_on_stack(this->upper_buf)) { |
| 418 | sg_init_one(sgl, this->upper_buf, this->upper_len); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 419 | ret = dma_map_sg(this->dev, sgl, 1, dr); |
| 420 | if (ret == 0) |
Huang Shijie | 0ff76a9 | 2013-12-18 23:41:00 +0800 | [diff] [blame] | 421 | goto map_fail; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 422 | |
Huang Shijie | 0ff76a9 | 2013-12-18 23:41:00 +0800 | [diff] [blame] | 423 | this->direct_dma_map_ok = true; |
| 424 | return; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 425 | } |
Huang Shijie | 0ff76a9 | 2013-12-18 23:41:00 +0800 | [diff] [blame] | 426 | |
| 427 | map_fail: |
| 428 | /* We have to use our own DMA buffer. */ |
| 429 | sg_init_one(sgl, this->data_buffer_dma, this->upper_len); |
| 430 | |
| 431 | if (dr == DMA_TO_DEVICE) |
| 432 | memcpy(this->data_buffer_dma, this->upper_buf, this->upper_len); |
| 433 | |
| 434 | dma_map_sg(this->dev, sgl, 1, dr); |
| 435 | |
| 436 | this->direct_dma_map_ok = false; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* This will be called after the DMA operation is finished. */ |
| 440 | static void dma_irq_callback(void *param) |
| 441 | { |
| 442 | struct gpmi_nand_data *this = param; |
| 443 | struct completion *dma_c = &this->dma_done; |
| 444 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 445 | switch (this->dma_type) { |
| 446 | case DMA_FOR_COMMAND: |
| 447 | dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE); |
| 448 | break; |
| 449 | |
| 450 | case DMA_FOR_READ_DATA: |
| 451 | dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE); |
| 452 | if (this->direct_dma_map_ok == false) |
| 453 | memcpy(this->upper_buf, this->data_buffer_dma, |
| 454 | this->upper_len); |
| 455 | break; |
| 456 | |
| 457 | case DMA_FOR_WRITE_DATA: |
| 458 | dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE); |
| 459 | break; |
| 460 | |
| 461 | case DMA_FOR_READ_ECC_PAGE: |
| 462 | case DMA_FOR_WRITE_ECC_PAGE: |
| 463 | /* We have to wait the BCH interrupt to finish. */ |
| 464 | break; |
| 465 | |
| 466 | default: |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 467 | dev_err(this->dev, "in wrong DMA operation.\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 468 | } |
Huang Shijie | 7b3d2fb | 2013-11-11 12:13:45 +0800 | [diff] [blame] | 469 | |
| 470 | complete(dma_c); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | int start_dma_without_bch_irq(struct gpmi_nand_data *this, |
| 474 | struct dma_async_tx_descriptor *desc) |
| 475 | { |
| 476 | struct completion *dma_c = &this->dma_done; |
Nicholas Mc Guire | 706d5b2 | 2015-02-08 11:37:33 -0500 | [diff] [blame] | 477 | unsigned long timeout; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 478 | |
| 479 | init_completion(dma_c); |
| 480 | |
| 481 | desc->callback = dma_irq_callback; |
| 482 | desc->callback_param = this; |
| 483 | dmaengine_submit(desc); |
Shawn Guo | d04525e | 2012-04-11 13:29:31 +0800 | [diff] [blame] | 484 | dma_async_issue_pending(get_dma_chan(this)); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 485 | |
| 486 | /* Wait for the interrupt from the DMA block. */ |
Nicholas Mc Guire | 706d5b2 | 2015-02-08 11:37:33 -0500 | [diff] [blame] | 487 | timeout = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000)); |
| 488 | if (!timeout) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 489 | dev_err(this->dev, "DMA timeout, last DMA :%d\n", |
| 490 | this->last_dma_type); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 491 | gpmi_dump_info(this); |
| 492 | return -ETIMEDOUT; |
| 493 | } |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * This function is used in BCH reading or BCH writing pages. |
| 499 | * It will wait for the BCH interrupt as long as ONE second. |
| 500 | * Actually, we must wait for two interrupts : |
| 501 | * [1] firstly the DMA interrupt and |
| 502 | * [2] secondly the BCH interrupt. |
| 503 | */ |
| 504 | int start_dma_with_bch_irq(struct gpmi_nand_data *this, |
| 505 | struct dma_async_tx_descriptor *desc) |
| 506 | { |
| 507 | struct completion *bch_c = &this->bch_done; |
Nicholas Mc Guire | 706d5b2 | 2015-02-08 11:37:33 -0500 | [diff] [blame] | 508 | unsigned long timeout; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 509 | |
| 510 | /* Prepare to receive an interrupt from the BCH block. */ |
| 511 | init_completion(bch_c); |
| 512 | |
| 513 | /* start the DMA */ |
| 514 | start_dma_without_bch_irq(this, desc); |
| 515 | |
| 516 | /* Wait for the interrupt from the BCH block. */ |
Nicholas Mc Guire | 706d5b2 | 2015-02-08 11:37:33 -0500 | [diff] [blame] | 517 | timeout = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000)); |
| 518 | if (!timeout) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 519 | dev_err(this->dev, "BCH timeout, last DMA :%d\n", |
| 520 | this->last_dma_type); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 521 | gpmi_dump_info(this); |
| 522 | return -ETIMEDOUT; |
| 523 | } |
| 524 | return 0; |
| 525 | } |
| 526 | |
Greg Kroah-Hartman | d892994 | 2012-12-21 13:19:05 -0800 | [diff] [blame] | 527 | static int acquire_register_block(struct gpmi_nand_data *this, |
| 528 | const char *res_name) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 529 | { |
| 530 | struct platform_device *pdev = this->pdev; |
| 531 | struct resources *res = &this->resources; |
| 532 | struct resource *r; |
Huang Shijie | 513d57e | 2012-07-17 14:14:02 +0800 | [diff] [blame] | 533 | void __iomem *p; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 534 | |
| 535 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name); |
Huang Shijie | 87a9d69 | 2013-11-14 14:25:48 +0800 | [diff] [blame] | 536 | p = devm_ioremap_resource(&pdev->dev, r); |
| 537 | if (IS_ERR(p)) |
| 538 | return PTR_ERR(p); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 539 | |
| 540 | if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME)) |
| 541 | res->gpmi_regs = p; |
| 542 | else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME)) |
| 543 | res->bch_regs = p; |
| 544 | else |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 545 | dev_err(this->dev, "unknown resource name : %s\n", res_name); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
Greg Kroah-Hartman | d892994 | 2012-12-21 13:19:05 -0800 | [diff] [blame] | 550 | static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 551 | { |
| 552 | struct platform_device *pdev = this->pdev; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 553 | const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME; |
| 554 | struct resource *r; |
| 555 | int err; |
| 556 | |
| 557 | r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name); |
| 558 | if (!r) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 559 | dev_err(this->dev, "Can't get resource for %s\n", res_name); |
Lothar Waßmann | 52a073b | 2013-08-07 08:15:38 +0200 | [diff] [blame] | 560 | return -ENODEV; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 561 | } |
| 562 | |
Huang Shijie | 3cb2c1e | 2013-11-14 14:25:49 +0800 | [diff] [blame] | 563 | err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this); |
| 564 | if (err) |
| 565 | dev_err(this->dev, "error requesting BCH IRQ\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 566 | |
Huang Shijie | 3cb2c1e | 2013-11-14 14:25:49 +0800 | [diff] [blame] | 567 | return err; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 568 | } |
| 569 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 570 | static void release_dma_channels(struct gpmi_nand_data *this) |
| 571 | { |
| 572 | unsigned int i; |
| 573 | for (i = 0; i < DMA_CHANS; i++) |
| 574 | if (this->dma_chans[i]) { |
| 575 | dma_release_channel(this->dma_chans[i]); |
| 576 | this->dma_chans[i] = NULL; |
| 577 | } |
| 578 | } |
| 579 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 580 | static int acquire_dma_channels(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 581 | { |
| 582 | struct platform_device *pdev = this->pdev; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 583 | struct dma_chan *dma_chan; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 584 | |
| 585 | /* request dma channel */ |
Shawn Guo | 5fac0e1 | 2013-02-26 11:44:28 +0800 | [diff] [blame] | 586 | dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx"); |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 587 | if (!dma_chan) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 588 | dev_err(this->dev, "Failed to request DMA channel.\n"); |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 589 | goto acquire_err; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 590 | } |
| 591 | |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 592 | this->dma_chans[0] = dma_chan; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 593 | return 0; |
| 594 | |
| 595 | acquire_err: |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 596 | release_dma_channels(this); |
| 597 | return -EINVAL; |
| 598 | } |
| 599 | |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 600 | static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = { |
| 601 | "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch", |
| 602 | }; |
| 603 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 604 | static int gpmi_get_clks(struct gpmi_nand_data *this) |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 605 | { |
| 606 | struct resources *r = &this->resources; |
| 607 | char **extra_clks = NULL; |
| 608 | struct clk *clk; |
Michał Mirosław | d1cb556 | 2013-05-04 15:19:35 +0200 | [diff] [blame] | 609 | int err, i; |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 610 | |
| 611 | /* The main clock is stored in the first. */ |
Fabio Estevam | 554cbc5 | 2013-11-07 22:32:38 -0200 | [diff] [blame] | 612 | r->clock[0] = devm_clk_get(this->dev, "gpmi_io"); |
Michał Mirosław | d1cb556 | 2013-05-04 15:19:35 +0200 | [diff] [blame] | 613 | if (IS_ERR(r->clock[0])) { |
| 614 | err = PTR_ERR(r->clock[0]); |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 615 | goto err_clock; |
Michał Mirosław | d1cb556 | 2013-05-04 15:19:35 +0200 | [diff] [blame] | 616 | } |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 617 | |
| 618 | /* Get extra clocks */ |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 619 | if (GPMI_IS_MX6(this)) |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 620 | extra_clks = extra_clks_for_mx6q; |
| 621 | if (!extra_clks) |
| 622 | return 0; |
| 623 | |
| 624 | for (i = 1; i < GPMI_CLK_MAX; i++) { |
| 625 | if (extra_clks[i - 1] == NULL) |
| 626 | break; |
| 627 | |
Fabio Estevam | 554cbc5 | 2013-11-07 22:32:38 -0200 | [diff] [blame] | 628 | clk = devm_clk_get(this->dev, extra_clks[i - 1]); |
Michał Mirosław | d1cb556 | 2013-05-04 15:19:35 +0200 | [diff] [blame] | 629 | if (IS_ERR(clk)) { |
| 630 | err = PTR_ERR(clk); |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 631 | goto err_clock; |
Michał Mirosław | d1cb556 | 2013-05-04 15:19:35 +0200 | [diff] [blame] | 632 | } |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 633 | |
| 634 | r->clock[i] = clk; |
| 635 | } |
| 636 | |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 637 | if (GPMI_IS_MX6(this)) |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 638 | /* |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 639 | * Set the default value for the gpmi clock. |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 640 | * |
Huang Shijie | e1ca95e | 2012-09-13 14:57:58 +0800 | [diff] [blame] | 641 | * If you want to use the ONFI nand which is in the |
| 642 | * Synchronous Mode, you should change the clock as you need. |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 643 | */ |
| 644 | clk_set_rate(r->clock[0], 22000000); |
Huang Shijie | e1ca95e | 2012-09-13 14:57:58 +0800 | [diff] [blame] | 645 | |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 646 | return 0; |
| 647 | |
| 648 | err_clock: |
| 649 | dev_dbg(this->dev, "failed in finding the clocks.\n"); |
Michał Mirosław | d1cb556 | 2013-05-04 15:19:35 +0200 | [diff] [blame] | 650 | return err; |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 651 | } |
| 652 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 653 | static int acquire_resources(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 654 | { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 655 | int ret; |
| 656 | |
| 657 | ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME); |
| 658 | if (ret) |
| 659 | goto exit_regs; |
| 660 | |
| 661 | ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME); |
| 662 | if (ret) |
| 663 | goto exit_regs; |
| 664 | |
| 665 | ret = acquire_bch_irq(this, bch_irq); |
| 666 | if (ret) |
| 667 | goto exit_regs; |
| 668 | |
| 669 | ret = acquire_dma_channels(this); |
| 670 | if (ret) |
Huang Shijie | 3cb2c1e | 2013-11-14 14:25:49 +0800 | [diff] [blame] | 671 | goto exit_regs; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 672 | |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 673 | ret = gpmi_get_clks(this); |
| 674 | if (ret) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 675 | goto exit_clock; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 676 | return 0; |
| 677 | |
| 678 | exit_clock: |
| 679 | release_dma_channels(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 680 | exit_regs: |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 681 | return ret; |
| 682 | } |
| 683 | |
| 684 | static void release_resources(struct gpmi_nand_data *this) |
| 685 | { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 686 | release_dma_channels(this); |
| 687 | } |
| 688 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 689 | static int init_hardware(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 690 | { |
| 691 | int ret; |
| 692 | |
| 693 | /* |
| 694 | * This structure contains the "safe" GPMI timing that should succeed |
| 695 | * with any NAND Flash device |
| 696 | * (although, with less-than-optimal performance). |
| 697 | */ |
| 698 | struct nand_timing safe_timing = { |
| 699 | .data_setup_in_ns = 80, |
| 700 | .data_hold_in_ns = 60, |
| 701 | .address_setup_in_ns = 25, |
| 702 | .gpmi_sample_delay_in_ns = 6, |
| 703 | .tREA_in_ns = -1, |
| 704 | .tRLOH_in_ns = -1, |
| 705 | .tRHOH_in_ns = -1, |
| 706 | }; |
| 707 | |
| 708 | /* Initialize the hardwares. */ |
| 709 | ret = gpmi_init(this); |
| 710 | if (ret) |
| 711 | return ret; |
| 712 | |
| 713 | this->timing = safe_timing; |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | static int read_page_prepare(struct gpmi_nand_data *this, |
| 718 | void *destination, unsigned length, |
| 719 | void *alt_virt, dma_addr_t alt_phys, unsigned alt_size, |
| 720 | void **use_virt, dma_addr_t *use_phys) |
| 721 | { |
| 722 | struct device *dev = this->dev; |
| 723 | |
| 724 | if (virt_addr_valid(destination)) { |
| 725 | dma_addr_t dest_phys; |
| 726 | |
| 727 | dest_phys = dma_map_single(dev, destination, |
| 728 | length, DMA_FROM_DEVICE); |
| 729 | if (dma_mapping_error(dev, dest_phys)) { |
| 730 | if (alt_size < length) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 731 | dev_err(dev, "Alternate buffer is too small\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 732 | return -ENOMEM; |
| 733 | } |
| 734 | goto map_failed; |
| 735 | } |
| 736 | *use_virt = destination; |
| 737 | *use_phys = dest_phys; |
| 738 | this->direct_dma_map_ok = true; |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | map_failed: |
| 743 | *use_virt = alt_virt; |
| 744 | *use_phys = alt_phys; |
| 745 | this->direct_dma_map_ok = false; |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static inline void read_page_end(struct gpmi_nand_data *this, |
| 750 | void *destination, unsigned length, |
| 751 | void *alt_virt, dma_addr_t alt_phys, unsigned alt_size, |
| 752 | void *used_virt, dma_addr_t used_phys) |
| 753 | { |
| 754 | if (this->direct_dma_map_ok) |
| 755 | dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE); |
| 756 | } |
| 757 | |
| 758 | static inline void read_page_swap_end(struct gpmi_nand_data *this, |
| 759 | void *destination, unsigned length, |
| 760 | void *alt_virt, dma_addr_t alt_phys, unsigned alt_size, |
| 761 | void *used_virt, dma_addr_t used_phys) |
| 762 | { |
| 763 | if (!this->direct_dma_map_ok) |
| 764 | memcpy(destination, alt_virt, length); |
| 765 | } |
| 766 | |
| 767 | static int send_page_prepare(struct gpmi_nand_data *this, |
| 768 | const void *source, unsigned length, |
| 769 | void *alt_virt, dma_addr_t alt_phys, unsigned alt_size, |
| 770 | const void **use_virt, dma_addr_t *use_phys) |
| 771 | { |
| 772 | struct device *dev = this->dev; |
| 773 | |
| 774 | if (virt_addr_valid(source)) { |
| 775 | dma_addr_t source_phys; |
| 776 | |
| 777 | source_phys = dma_map_single(dev, (void *)source, length, |
| 778 | DMA_TO_DEVICE); |
| 779 | if (dma_mapping_error(dev, source_phys)) { |
| 780 | if (alt_size < length) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 781 | dev_err(dev, "Alternate buffer is too small\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 782 | return -ENOMEM; |
| 783 | } |
| 784 | goto map_failed; |
| 785 | } |
| 786 | *use_virt = source; |
| 787 | *use_phys = source_phys; |
| 788 | return 0; |
| 789 | } |
| 790 | map_failed: |
| 791 | /* |
| 792 | * Copy the content of the source buffer into the alternate |
| 793 | * buffer and set up the return values accordingly. |
| 794 | */ |
| 795 | memcpy(alt_virt, source, length); |
| 796 | |
| 797 | *use_virt = alt_virt; |
| 798 | *use_phys = alt_phys; |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | static void send_page_end(struct gpmi_nand_data *this, |
| 803 | const void *source, unsigned length, |
| 804 | void *alt_virt, dma_addr_t alt_phys, unsigned alt_size, |
| 805 | const void *used_virt, dma_addr_t used_phys) |
| 806 | { |
| 807 | struct device *dev = this->dev; |
| 808 | if (used_virt == source) |
| 809 | dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE); |
| 810 | } |
| 811 | |
| 812 | static void gpmi_free_dma_buffer(struct gpmi_nand_data *this) |
| 813 | { |
| 814 | struct device *dev = this->dev; |
| 815 | |
| 816 | if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt)) |
| 817 | dma_free_coherent(dev, this->page_buffer_size, |
| 818 | this->page_buffer_virt, |
| 819 | this->page_buffer_phys); |
| 820 | kfree(this->cmd_buffer); |
| 821 | kfree(this->data_buffer_dma); |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 822 | kfree(this->raw_buffer); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 823 | |
| 824 | this->cmd_buffer = NULL; |
| 825 | this->data_buffer_dma = NULL; |
Han Xu | 2cd395d | 2016-04-04 15:41:29 -0500 | [diff] [blame] | 826 | this->raw_buffer = NULL; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 827 | this->page_buffer_virt = NULL; |
| 828 | this->page_buffer_size = 0; |
| 829 | } |
| 830 | |
| 831 | /* Allocate the DMA buffers */ |
| 832 | static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this) |
| 833 | { |
| 834 | struct bch_geometry *geo = &this->bch_geometry; |
| 835 | struct device *dev = this->dev; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 836 | struct mtd_info *mtd = nand_to_mtd(&this->nand); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 837 | |
| 838 | /* [1] Allocate a command buffer. PAGE_SIZE is enough. */ |
Huang Shijie | 513d57e | 2012-07-17 14:14:02 +0800 | [diff] [blame] | 839 | this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 840 | if (this->cmd_buffer == NULL) |
| 841 | goto error_alloc; |
| 842 | |
Huang Shijie | 06f216c | 2013-12-18 23:40:59 +0800 | [diff] [blame] | 843 | /* |
| 844 | * [2] Allocate a read/write data buffer. |
| 845 | * The gpmi_alloc_dma_buffer can be called twice. |
| 846 | * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer |
| 847 | * is called before the nand_scan_ident; and we allocate a buffer |
| 848 | * of the real NAND page size when the gpmi_alloc_dma_buffer is |
| 849 | * called after the nand_scan_ident. |
| 850 | */ |
| 851 | this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE, |
| 852 | GFP_DMA | GFP_KERNEL); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 853 | if (this->data_buffer_dma == NULL) |
| 854 | goto error_alloc; |
| 855 | |
| 856 | /* |
| 857 | * [3] Allocate the page buffer. |
| 858 | * |
| 859 | * Both the payload buffer and the auxiliary buffer must appear on |
| 860 | * 32-bit boundaries. We presume the size of the payload buffer is a |
| 861 | * power of two and is much larger than four, which guarantees the |
| 862 | * auxiliary buffer will appear on a 32-bit boundary. |
| 863 | */ |
| 864 | this->page_buffer_size = geo->payload_size + geo->auxiliary_size; |
| 865 | this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size, |
| 866 | &this->page_buffer_phys, GFP_DMA); |
| 867 | if (!this->page_buffer_virt) |
| 868 | goto error_alloc; |
| 869 | |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 870 | this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
| 871 | if (!this->raw_buffer) |
| 872 | goto error_alloc; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 873 | |
| 874 | /* Slice up the page buffer. */ |
| 875 | this->payload_virt = this->page_buffer_virt; |
| 876 | this->payload_phys = this->page_buffer_phys; |
| 877 | this->auxiliary_virt = this->payload_virt + geo->payload_size; |
| 878 | this->auxiliary_phys = this->payload_phys + geo->payload_size; |
| 879 | return 0; |
| 880 | |
| 881 | error_alloc: |
| 882 | gpmi_free_dma_buffer(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 883 | return -ENOMEM; |
| 884 | } |
| 885 | |
| 886 | static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl) |
| 887 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 888 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 889 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 890 | int ret; |
| 891 | |
| 892 | /* |
| 893 | * Every operation begins with a command byte and a series of zero or |
| 894 | * more address bytes. These are distinguished by either the Address |
| 895 | * Latch Enable (ALE) or Command Latch Enable (CLE) signals being |
| 896 | * asserted. When MTD is ready to execute the command, it will deassert |
| 897 | * both latch enables. |
| 898 | * |
| 899 | * Rather than run a separate DMA operation for every single byte, we |
| 900 | * queue them up and run a single DMA operation for the entire series |
| 901 | * of command and data bytes. NAND_CMD_NONE means the END of the queue. |
| 902 | */ |
| 903 | if ((ctrl & (NAND_ALE | NAND_CLE))) { |
| 904 | if (data != NAND_CMD_NONE) |
| 905 | this->cmd_buffer[this->command_length++] = data; |
| 906 | return; |
| 907 | } |
| 908 | |
| 909 | if (!this->command_length) |
| 910 | return; |
| 911 | |
| 912 | ret = gpmi_send_command(this); |
| 913 | if (ret) |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 914 | dev_err(this->dev, "Chip: %u, Error %d\n", |
| 915 | this->current_chip, ret); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 916 | |
| 917 | this->command_length = 0; |
| 918 | } |
| 919 | |
| 920 | static int gpmi_dev_ready(struct mtd_info *mtd) |
| 921 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 922 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 923 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 924 | |
| 925 | return gpmi_is_ready(this, this->current_chip); |
| 926 | } |
| 927 | |
| 928 | static void gpmi_select_chip(struct mtd_info *mtd, int chipnr) |
| 929 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 930 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 931 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 932 | |
| 933 | if ((this->current_chip < 0) && (chipnr >= 0)) |
| 934 | gpmi_begin(this); |
| 935 | else if ((this->current_chip >= 0) && (chipnr < 0)) |
| 936 | gpmi_end(this); |
| 937 | |
| 938 | this->current_chip = chipnr; |
| 939 | } |
| 940 | |
| 941 | static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 942 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 943 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 944 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 945 | |
Huang Shijie | c232596 | 2013-11-20 10:09:44 +0800 | [diff] [blame] | 946 | dev_dbg(this->dev, "len is %d\n", len); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 947 | this->upper_buf = buf; |
| 948 | this->upper_len = len; |
| 949 | |
| 950 | gpmi_read_data(this); |
| 951 | } |
| 952 | |
| 953 | static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
| 954 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 955 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 956 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 957 | |
Huang Shijie | c232596 | 2013-11-20 10:09:44 +0800 | [diff] [blame] | 958 | dev_dbg(this->dev, "len is %d\n", len); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 959 | this->upper_buf = (uint8_t *)buf; |
| 960 | this->upper_len = len; |
| 961 | |
| 962 | gpmi_send_data(this); |
| 963 | } |
| 964 | |
| 965 | static uint8_t gpmi_read_byte(struct mtd_info *mtd) |
| 966 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 967 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 968 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 969 | uint8_t *buf = this->data_buffer_dma; |
| 970 | |
| 971 | gpmi_read_buf(mtd, buf, 1); |
| 972 | return buf[0]; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Handles block mark swapping. |
| 977 | * It can be called in swapping the block mark, or swapping it back, |
| 978 | * because the the operations are the same. |
| 979 | */ |
| 980 | static void block_mark_swapping(struct gpmi_nand_data *this, |
| 981 | void *payload, void *auxiliary) |
| 982 | { |
| 983 | struct bch_geometry *nfc_geo = &this->bch_geometry; |
| 984 | unsigned char *p; |
| 985 | unsigned char *a; |
| 986 | unsigned int bit; |
| 987 | unsigned char mask; |
| 988 | unsigned char from_data; |
| 989 | unsigned char from_oob; |
| 990 | |
| 991 | if (!this->swap_block_mark) |
| 992 | return; |
| 993 | |
| 994 | /* |
| 995 | * If control arrives here, we're swapping. Make some convenience |
| 996 | * variables. |
| 997 | */ |
| 998 | bit = nfc_geo->block_mark_bit_offset; |
| 999 | p = payload + nfc_geo->block_mark_byte_offset; |
| 1000 | a = auxiliary; |
| 1001 | |
| 1002 | /* |
| 1003 | * Get the byte from the data area that overlays the block mark. Since |
| 1004 | * the ECC engine applies its own view to the bits in the page, the |
| 1005 | * physical block mark won't (in general) appear on a byte boundary in |
| 1006 | * the data. |
| 1007 | */ |
| 1008 | from_data = (p[0] >> bit) | (p[1] << (8 - bit)); |
| 1009 | |
| 1010 | /* Get the byte from the OOB. */ |
| 1011 | from_oob = a[0]; |
| 1012 | |
| 1013 | /* Swap them. */ |
| 1014 | a[0] = from_data; |
| 1015 | |
| 1016 | mask = (0x1 << bit) - 1; |
| 1017 | p[0] = (p[0] & mask) | (from_oob << bit); |
| 1018 | |
| 1019 | mask = ~0 << bit; |
| 1020 | p[1] = (p[1] & mask) | (from_oob >> (8 - bit)); |
| 1021 | } |
| 1022 | |
| 1023 | static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1024 | uint8_t *buf, int oob_required, int page) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1025 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1026 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1027 | struct bch_geometry *nfc_geo = &this->bch_geometry; |
| 1028 | void *payload_virt; |
| 1029 | dma_addr_t payload_phys; |
| 1030 | void *auxiliary_virt; |
| 1031 | dma_addr_t auxiliary_phys; |
| 1032 | unsigned int i; |
| 1033 | unsigned char *status; |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 1034 | unsigned int max_bitflips = 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1035 | int ret; |
| 1036 | |
Huang Shijie | c232596 | 2013-11-20 10:09:44 +0800 | [diff] [blame] | 1037 | dev_dbg(this->dev, "page number is : %d\n", page); |
Huang Shijie | 4a57d67 | 2014-01-03 11:01:41 +0800 | [diff] [blame] | 1038 | ret = read_page_prepare(this, buf, nfc_geo->payload_size, |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1039 | this->payload_virt, this->payload_phys, |
| 1040 | nfc_geo->payload_size, |
| 1041 | &payload_virt, &payload_phys); |
| 1042 | if (ret) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1043 | dev_err(this->dev, "Inadequate DMA buffer\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1044 | ret = -ENOMEM; |
| 1045 | return ret; |
| 1046 | } |
| 1047 | auxiliary_virt = this->auxiliary_virt; |
| 1048 | auxiliary_phys = this->auxiliary_phys; |
| 1049 | |
| 1050 | /* go! */ |
| 1051 | ret = gpmi_read_page(this, payload_phys, auxiliary_phys); |
Huang Shijie | 4a57d67 | 2014-01-03 11:01:41 +0800 | [diff] [blame] | 1052 | read_page_end(this, buf, nfc_geo->payload_size, |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1053 | this->payload_virt, this->payload_phys, |
| 1054 | nfc_geo->payload_size, |
| 1055 | payload_virt, payload_phys); |
| 1056 | if (ret) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1057 | dev_err(this->dev, "Error in ECC-based read: %d\n", ret); |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 1058 | return ret; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /* handle the block mark swapping */ |
| 1062 | block_mark_swapping(this, payload_virt, auxiliary_virt); |
| 1063 | |
| 1064 | /* Loop over status bytes, accumulating ECC status. */ |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 1065 | status = auxiliary_virt + nfc_geo->auxiliary_status_offset; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1066 | |
| 1067 | for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) { |
| 1068 | if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED)) |
| 1069 | continue; |
| 1070 | |
| 1071 | if (*status == STATUS_UNCORRECTABLE) { |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 1072 | mtd->ecc_stats.failed++; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1073 | continue; |
| 1074 | } |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 1075 | mtd->ecc_stats.corrected += *status; |
| 1076 | max_bitflips = max_t(unsigned int, max_bitflips, *status); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Brian Norris | 7725cc8 | 2012-05-02 10:15:02 -0700 | [diff] [blame] | 1079 | if (oob_required) { |
| 1080 | /* |
| 1081 | * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob() |
| 1082 | * for details about our policy for delivering the OOB. |
| 1083 | * |
| 1084 | * We fill the caller's buffer with set bits, and then copy the |
| 1085 | * block mark to th caller's buffer. Note that, if block mark |
| 1086 | * swapping was necessary, it has already been done, so we can |
| 1087 | * rely on the first byte of the auxiliary buffer to contain |
| 1088 | * the block mark. |
| 1089 | */ |
| 1090 | memset(chip->oob_poi, ~0, mtd->oobsize); |
| 1091 | chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0]; |
Brian Norris | 7725cc8 | 2012-05-02 10:15:02 -0700 | [diff] [blame] | 1092 | } |
Sascha Hauer | 6023813a | 2012-06-26 17:26:16 +0200 | [diff] [blame] | 1093 | |
Huang Shijie | 4a57d67 | 2014-01-03 11:01:41 +0800 | [diff] [blame] | 1094 | read_page_swap_end(this, buf, nfc_geo->payload_size, |
Sascha Hauer | 6023813a | 2012-06-26 17:26:16 +0200 | [diff] [blame] | 1095 | this->payload_virt, this->payload_phys, |
| 1096 | nfc_geo->payload_size, |
| 1097 | payload_virt, payload_phys); |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 1098 | |
| 1099 | return max_bitflips; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1100 | } |
| 1101 | |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1102 | /* Fake a virtual small page for the subpage read */ |
| 1103 | static int gpmi_ecc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
| 1104 | uint32_t offs, uint32_t len, uint8_t *buf, int page) |
| 1105 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1106 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1107 | void __iomem *bch_regs = this->resources.bch_regs; |
| 1108 | struct bch_geometry old_geo = this->bch_geometry; |
| 1109 | struct bch_geometry *geo = &this->bch_geometry; |
| 1110 | int size = chip->ecc.size; /* ECC chunk size */ |
| 1111 | int meta, n, page_size; |
| 1112 | u32 r1_old, r2_old, r1_new, r2_new; |
| 1113 | unsigned int max_bitflips; |
| 1114 | int first, last, marker_pos; |
| 1115 | int ecc_parity_size; |
| 1116 | int col = 0; |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1117 | int old_swap_block_mark = this->swap_block_mark; |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1118 | |
| 1119 | /* The size of ECC parity */ |
| 1120 | ecc_parity_size = geo->gf_len * geo->ecc_strength / 8; |
| 1121 | |
| 1122 | /* Align it with the chunk size */ |
| 1123 | first = offs / size; |
| 1124 | last = (offs + len - 1) / size; |
| 1125 | |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1126 | if (this->swap_block_mark) { |
| 1127 | /* |
| 1128 | * Find the chunk which contains the Block Marker. |
| 1129 | * If this chunk is in the range of [first, last], |
| 1130 | * we have to read out the whole page. |
| 1131 | * Why? since we had swapped the data at the position of Block |
| 1132 | * Marker to the metadata which is bound with the chunk 0. |
| 1133 | */ |
| 1134 | marker_pos = geo->block_mark_byte_offset / size; |
| 1135 | if (last >= marker_pos && first <= marker_pos) { |
| 1136 | dev_dbg(this->dev, |
| 1137 | "page:%d, first:%d, last:%d, marker at:%d\n", |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1138 | page, first, last, marker_pos); |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1139 | return gpmi_ecc_read_page(mtd, chip, buf, 0, page); |
| 1140 | } |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | meta = geo->metadata_size; |
| 1144 | if (first) { |
| 1145 | col = meta + (size + ecc_parity_size) * first; |
| 1146 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, col, -1); |
| 1147 | |
| 1148 | meta = 0; |
| 1149 | buf = buf + first * size; |
| 1150 | } |
| 1151 | |
| 1152 | /* Save the old environment */ |
| 1153 | r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0); |
| 1154 | r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1); |
| 1155 | |
| 1156 | /* change the BCH registers and bch_geometry{} */ |
| 1157 | n = last - first + 1; |
| 1158 | page_size = meta + (size + ecc_parity_size) * n; |
| 1159 | |
| 1160 | r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS | |
| 1161 | BM_BCH_FLASH0LAYOUT0_META_SIZE); |
| 1162 | r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1) |
| 1163 | | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta); |
| 1164 | writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0); |
| 1165 | |
| 1166 | r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE; |
| 1167 | r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size); |
| 1168 | writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1); |
| 1169 | |
| 1170 | geo->ecc_chunk_count = n; |
| 1171 | geo->payload_size = n * size; |
| 1172 | geo->page_size = page_size; |
| 1173 | geo->auxiliary_status_offset = ALIGN(meta, 4); |
| 1174 | |
| 1175 | dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n", |
| 1176 | page, offs, len, col, first, n, page_size); |
| 1177 | |
| 1178 | /* Read the subpage now */ |
| 1179 | this->swap_block_mark = false; |
| 1180 | max_bitflips = gpmi_ecc_read_page(mtd, chip, buf, 0, page); |
| 1181 | |
| 1182 | /* Restore */ |
| 1183 | writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0); |
| 1184 | writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1); |
| 1185 | this->bch_geometry = old_geo; |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1186 | this->swap_block_mark = old_swap_block_mark; |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1187 | |
| 1188 | return max_bitflips; |
| 1189 | } |
| 1190 | |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1191 | static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 1192 | const uint8_t *buf, int oob_required, int page) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1193 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1194 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1195 | struct bch_geometry *nfc_geo = &this->bch_geometry; |
| 1196 | const void *payload_virt; |
| 1197 | dma_addr_t payload_phys; |
| 1198 | const void *auxiliary_virt; |
| 1199 | dma_addr_t auxiliary_phys; |
| 1200 | int ret; |
| 1201 | |
Huang Shijie | c232596 | 2013-11-20 10:09:44 +0800 | [diff] [blame] | 1202 | dev_dbg(this->dev, "ecc write page.\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1203 | if (this->swap_block_mark) { |
| 1204 | /* |
| 1205 | * If control arrives here, we're doing block mark swapping. |
| 1206 | * Since we can't modify the caller's buffers, we must copy them |
| 1207 | * into our own. |
| 1208 | */ |
| 1209 | memcpy(this->payload_virt, buf, mtd->writesize); |
| 1210 | payload_virt = this->payload_virt; |
| 1211 | payload_phys = this->payload_phys; |
| 1212 | |
| 1213 | memcpy(this->auxiliary_virt, chip->oob_poi, |
| 1214 | nfc_geo->auxiliary_size); |
| 1215 | auxiliary_virt = this->auxiliary_virt; |
| 1216 | auxiliary_phys = this->auxiliary_phys; |
| 1217 | |
| 1218 | /* Handle block mark swapping. */ |
| 1219 | block_mark_swapping(this, |
Lothar Waßmann | 6a76096 | 2014-06-12 15:20:41 +0200 | [diff] [blame] | 1220 | (void *)payload_virt, (void *)auxiliary_virt); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1221 | } else { |
| 1222 | /* |
| 1223 | * If control arrives here, we're not doing block mark swapping, |
| 1224 | * so we can to try and use the caller's buffers. |
| 1225 | */ |
| 1226 | ret = send_page_prepare(this, |
| 1227 | buf, mtd->writesize, |
| 1228 | this->payload_virt, this->payload_phys, |
| 1229 | nfc_geo->payload_size, |
| 1230 | &payload_virt, &payload_phys); |
| 1231 | if (ret) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1232 | dev_err(this->dev, "Inadequate payload DMA buffer\n"); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1233 | return 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | ret = send_page_prepare(this, |
| 1237 | chip->oob_poi, mtd->oobsize, |
| 1238 | this->auxiliary_virt, this->auxiliary_phys, |
| 1239 | nfc_geo->auxiliary_size, |
| 1240 | &auxiliary_virt, &auxiliary_phys); |
| 1241 | if (ret) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1242 | dev_err(this->dev, "Inadequate auxiliary DMA buffer\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1243 | goto exit_auxiliary; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | /* Ask the NFC. */ |
| 1248 | ret = gpmi_send_page(this, payload_phys, auxiliary_phys); |
| 1249 | if (ret) |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1250 | dev_err(this->dev, "Error in ECC-based write: %d\n", ret); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1251 | |
| 1252 | if (!this->swap_block_mark) { |
| 1253 | send_page_end(this, chip->oob_poi, mtd->oobsize, |
| 1254 | this->auxiliary_virt, this->auxiliary_phys, |
| 1255 | nfc_geo->auxiliary_size, |
| 1256 | auxiliary_virt, auxiliary_phys); |
| 1257 | exit_auxiliary: |
| 1258 | send_page_end(this, buf, mtd->writesize, |
| 1259 | this->payload_virt, this->payload_phys, |
| 1260 | nfc_geo->payload_size, |
| 1261 | payload_virt, payload_phys); |
| 1262 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1263 | |
| 1264 | return 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * There are several places in this driver where we have to handle the OOB and |
| 1269 | * block marks. This is the function where things are the most complicated, so |
| 1270 | * this is where we try to explain it all. All the other places refer back to |
| 1271 | * here. |
| 1272 | * |
| 1273 | * These are the rules, in order of decreasing importance: |
| 1274 | * |
| 1275 | * 1) Nothing the caller does can be allowed to imperil the block mark. |
| 1276 | * |
| 1277 | * 2) In read operations, the first byte of the OOB we return must reflect the |
| 1278 | * true state of the block mark, no matter where that block mark appears in |
| 1279 | * the physical page. |
| 1280 | * |
| 1281 | * 3) ECC-based read operations return an OOB full of set bits (since we never |
| 1282 | * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads |
| 1283 | * return). |
| 1284 | * |
| 1285 | * 4) "Raw" read operations return a direct view of the physical bytes in the |
| 1286 | * page, using the conventional definition of which bytes are data and which |
| 1287 | * are OOB. This gives the caller a way to see the actual, physical bytes |
| 1288 | * in the page, without the distortions applied by our ECC engine. |
| 1289 | * |
| 1290 | * |
| 1291 | * What we do for this specific read operation depends on two questions: |
| 1292 | * |
| 1293 | * 1) Are we doing a "raw" read, or an ECC-based read? |
| 1294 | * |
| 1295 | * 2) Are we using block mark swapping or transcription? |
| 1296 | * |
| 1297 | * There are four cases, illustrated by the following Karnaugh map: |
| 1298 | * |
| 1299 | * | Raw | ECC-based | |
| 1300 | * -------------+-------------------------+-------------------------+ |
| 1301 | * | Read the conventional | | |
| 1302 | * | OOB at the end of the | | |
| 1303 | * Swapping | page and return it. It | | |
| 1304 | * | contains exactly what | | |
| 1305 | * | we want. | Read the block mark and | |
| 1306 | * -------------+-------------------------+ return it in a buffer | |
| 1307 | * | Read the conventional | full of set bits. | |
| 1308 | * | OOB at the end of the | | |
| 1309 | * | page and also the block | | |
| 1310 | * Transcribing | mark in the metadata. | | |
| 1311 | * | Copy the block mark | | |
| 1312 | * | into the first byte of | | |
| 1313 | * | the OOB. | | |
| 1314 | * -------------+-------------------------+-------------------------+ |
| 1315 | * |
| 1316 | * Note that we break rule #4 in the Transcribing/Raw case because we're not |
| 1317 | * giving an accurate view of the actual, physical bytes in the page (we're |
| 1318 | * overwriting the block mark). That's OK because it's more important to follow |
| 1319 | * rule #2. |
| 1320 | * |
| 1321 | * It turns out that knowing whether we want an "ECC-based" or "raw" read is not |
| 1322 | * easy. When reading a page, for example, the NAND Flash MTD code calls our |
| 1323 | * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an |
| 1324 | * ECC-based or raw view of the page is implicit in which function it calls |
| 1325 | * (there is a similar pair of ECC-based/raw functions for writing). |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1326 | */ |
| 1327 | static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip, |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 1328 | int page) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1329 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1330 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1331 | |
Huang Shijie | c232596 | 2013-11-20 10:09:44 +0800 | [diff] [blame] | 1332 | dev_dbg(this->dev, "page number is %d\n", page); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1333 | /* clear the OOB buffer */ |
| 1334 | memset(chip->oob_poi, ~0, mtd->oobsize); |
| 1335 | |
| 1336 | /* Read out the conventional OOB. */ |
| 1337 | chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page); |
| 1338 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1339 | |
| 1340 | /* |
| 1341 | * Now, we want to make sure the block mark is correct. In the |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1342 | * non-transcribing case (!GPMI_IS_MX23()), we already have it. |
| 1343 | * Otherwise, we need to explicitly read it. |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1344 | */ |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1345 | if (GPMI_IS_MX23(this)) { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1346 | /* Read the block mark into the first byte of the OOB buffer. */ |
| 1347 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1348 | chip->oob_poi[0] = chip->read_byte(mtd); |
| 1349 | } |
| 1350 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 1351 | return 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | static int |
| 1355 | gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page) |
| 1356 | { |
Boris Brezillon | 191a829 | 2016-02-03 20:11:44 +0100 | [diff] [blame] | 1357 | struct mtd_oob_region of = { }; |
Huang Shijie | 7a2b89a | 2013-09-25 14:58:15 +0800 | [diff] [blame] | 1358 | int status = 0; |
| 1359 | |
| 1360 | /* Do we have available oob area? */ |
Boris Brezillon | 191a829 | 2016-02-03 20:11:44 +0100 | [diff] [blame] | 1361 | mtd_ooblayout_free(mtd, 0, &of); |
| 1362 | if (!of.length) |
Huang Shijie | 7a2b89a | 2013-09-25 14:58:15 +0800 | [diff] [blame] | 1363 | return -EPERM; |
| 1364 | |
| 1365 | if (!nand_is_slc(chip)) |
| 1366 | return -EPERM; |
| 1367 | |
Boris Brezillon | 191a829 | 2016-02-03 20:11:44 +0100 | [diff] [blame] | 1368 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of.offset, page); |
| 1369 | chip->write_buf(mtd, chip->oob_poi + of.offset, of.length); |
Huang Shijie | 7a2b89a | 2013-09-25 14:58:15 +0800 | [diff] [blame] | 1370 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1371 | |
| 1372 | status = chip->waitfunc(mtd, chip); |
| 1373 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1374 | } |
| 1375 | |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1376 | /* |
| 1377 | * This function reads a NAND page without involving the ECC engine (no HW |
| 1378 | * ECC correction). |
| 1379 | * The tricky part in the GPMI/BCH controller is that it stores ECC bits |
| 1380 | * inline (interleaved with payload DATA), and do not align data chunk on |
| 1381 | * byte boundaries. |
| 1382 | * We thus need to take care moving the payload data and ECC bits stored in the |
| 1383 | * page into the provided buffers, which is why we're using gpmi_copy_bits. |
| 1384 | * |
| 1385 | * See set_geometry_by_ecc_info inline comments to have a full description |
| 1386 | * of the layout used by the GPMI controller. |
| 1387 | */ |
| 1388 | static int gpmi_ecc_read_page_raw(struct mtd_info *mtd, |
| 1389 | struct nand_chip *chip, uint8_t *buf, |
| 1390 | int oob_required, int page) |
| 1391 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1392 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1393 | struct bch_geometry *nfc_geo = &this->bch_geometry; |
| 1394 | int eccsize = nfc_geo->ecc_chunk_size; |
| 1395 | int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len; |
| 1396 | u8 *tmp_buf = this->raw_buffer; |
| 1397 | size_t src_bit_off; |
| 1398 | size_t oob_bit_off; |
| 1399 | size_t oob_byte_off; |
| 1400 | uint8_t *oob = chip->oob_poi; |
| 1401 | int step; |
| 1402 | |
| 1403 | chip->read_buf(mtd, tmp_buf, |
| 1404 | mtd->writesize + mtd->oobsize); |
| 1405 | |
| 1406 | /* |
| 1407 | * If required, swap the bad block marker and the data stored in the |
| 1408 | * metadata section, so that we don't wrongly consider a block as bad. |
| 1409 | * |
| 1410 | * See the layout description for a detailed explanation on why this |
| 1411 | * is needed. |
| 1412 | */ |
| 1413 | if (this->swap_block_mark) { |
| 1414 | u8 swap = tmp_buf[0]; |
| 1415 | |
| 1416 | tmp_buf[0] = tmp_buf[mtd->writesize]; |
| 1417 | tmp_buf[mtd->writesize] = swap; |
| 1418 | } |
| 1419 | |
| 1420 | /* |
| 1421 | * Copy the metadata section into the oob buffer (this section is |
| 1422 | * guaranteed to be aligned on a byte boundary). |
| 1423 | */ |
| 1424 | if (oob_required) |
| 1425 | memcpy(oob, tmp_buf, nfc_geo->metadata_size); |
| 1426 | |
| 1427 | oob_bit_off = nfc_geo->metadata_size * 8; |
| 1428 | src_bit_off = oob_bit_off; |
| 1429 | |
| 1430 | /* Extract interleaved payload data and ECC bits */ |
| 1431 | for (step = 0; step < nfc_geo->ecc_chunk_count; step++) { |
| 1432 | if (buf) |
| 1433 | gpmi_copy_bits(buf, step * eccsize * 8, |
| 1434 | tmp_buf, src_bit_off, |
| 1435 | eccsize * 8); |
| 1436 | src_bit_off += eccsize * 8; |
| 1437 | |
| 1438 | /* Align last ECC block to align a byte boundary */ |
| 1439 | if (step == nfc_geo->ecc_chunk_count - 1 && |
| 1440 | (oob_bit_off + eccbits) % 8) |
| 1441 | eccbits += 8 - ((oob_bit_off + eccbits) % 8); |
| 1442 | |
| 1443 | if (oob_required) |
| 1444 | gpmi_copy_bits(oob, oob_bit_off, |
| 1445 | tmp_buf, src_bit_off, |
| 1446 | eccbits); |
| 1447 | |
| 1448 | src_bit_off += eccbits; |
| 1449 | oob_bit_off += eccbits; |
| 1450 | } |
| 1451 | |
| 1452 | if (oob_required) { |
| 1453 | oob_byte_off = oob_bit_off / 8; |
| 1454 | |
| 1455 | if (oob_byte_off < mtd->oobsize) |
| 1456 | memcpy(oob + oob_byte_off, |
| 1457 | tmp_buf + mtd->writesize + oob_byte_off, |
| 1458 | mtd->oobsize - oob_byte_off); |
| 1459 | } |
| 1460 | |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * This function writes a NAND page without involving the ECC engine (no HW |
| 1466 | * ECC generation). |
| 1467 | * The tricky part in the GPMI/BCH controller is that it stores ECC bits |
| 1468 | * inline (interleaved with payload DATA), and do not align data chunk on |
| 1469 | * byte boundaries. |
| 1470 | * We thus need to take care moving the OOB area at the right place in the |
| 1471 | * final page, which is why we're using gpmi_copy_bits. |
| 1472 | * |
| 1473 | * See set_geometry_by_ecc_info inline comments to have a full description |
| 1474 | * of the layout used by the GPMI controller. |
| 1475 | */ |
| 1476 | static int gpmi_ecc_write_page_raw(struct mtd_info *mtd, |
| 1477 | struct nand_chip *chip, |
| 1478 | const uint8_t *buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 1479 | int oob_required, int page) |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1480 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1481 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1482 | struct bch_geometry *nfc_geo = &this->bch_geometry; |
| 1483 | int eccsize = nfc_geo->ecc_chunk_size; |
| 1484 | int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len; |
| 1485 | u8 *tmp_buf = this->raw_buffer; |
| 1486 | uint8_t *oob = chip->oob_poi; |
| 1487 | size_t dst_bit_off; |
| 1488 | size_t oob_bit_off; |
| 1489 | size_t oob_byte_off; |
| 1490 | int step; |
| 1491 | |
| 1492 | /* |
| 1493 | * Initialize all bits to 1 in case we don't have a buffer for the |
| 1494 | * payload or oob data in order to leave unspecified bits of data |
| 1495 | * to their initial state. |
| 1496 | */ |
| 1497 | if (!buf || !oob_required) |
| 1498 | memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize); |
| 1499 | |
| 1500 | /* |
| 1501 | * First copy the metadata section (stored in oob buffer) at the |
| 1502 | * beginning of the page, as imposed by the GPMI layout. |
| 1503 | */ |
| 1504 | memcpy(tmp_buf, oob, nfc_geo->metadata_size); |
| 1505 | oob_bit_off = nfc_geo->metadata_size * 8; |
| 1506 | dst_bit_off = oob_bit_off; |
| 1507 | |
| 1508 | /* Interleave payload data and ECC bits */ |
| 1509 | for (step = 0; step < nfc_geo->ecc_chunk_count; step++) { |
| 1510 | if (buf) |
| 1511 | gpmi_copy_bits(tmp_buf, dst_bit_off, |
| 1512 | buf, step * eccsize * 8, eccsize * 8); |
| 1513 | dst_bit_off += eccsize * 8; |
| 1514 | |
| 1515 | /* Align last ECC block to align a byte boundary */ |
| 1516 | if (step == nfc_geo->ecc_chunk_count - 1 && |
| 1517 | (oob_bit_off + eccbits) % 8) |
| 1518 | eccbits += 8 - ((oob_bit_off + eccbits) % 8); |
| 1519 | |
| 1520 | if (oob_required) |
| 1521 | gpmi_copy_bits(tmp_buf, dst_bit_off, |
| 1522 | oob, oob_bit_off, eccbits); |
| 1523 | |
| 1524 | dst_bit_off += eccbits; |
| 1525 | oob_bit_off += eccbits; |
| 1526 | } |
| 1527 | |
| 1528 | oob_byte_off = oob_bit_off / 8; |
| 1529 | |
| 1530 | if (oob_required && oob_byte_off < mtd->oobsize) |
| 1531 | memcpy(tmp_buf + mtd->writesize + oob_byte_off, |
| 1532 | oob + oob_byte_off, mtd->oobsize - oob_byte_off); |
| 1533 | |
| 1534 | /* |
| 1535 | * If required, swap the bad block marker and the first byte of the |
| 1536 | * metadata section, so that we don't modify the bad block marker. |
| 1537 | * |
| 1538 | * See the layout description for a detailed explanation on why this |
| 1539 | * is needed. |
| 1540 | */ |
| 1541 | if (this->swap_block_mark) { |
| 1542 | u8 swap = tmp_buf[0]; |
| 1543 | |
| 1544 | tmp_buf[0] = tmp_buf[mtd->writesize]; |
| 1545 | tmp_buf[mtd->writesize] = swap; |
| 1546 | } |
| 1547 | |
| 1548 | chip->write_buf(mtd, tmp_buf, mtd->writesize + mtd->oobsize); |
| 1549 | |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
Boris BREZILLON | 7ca94e0 | 2014-11-30 19:10:30 +0100 | [diff] [blame] | 1553 | static int gpmi_ecc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1554 | int page) |
| 1555 | { |
| 1556 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1557 | |
| 1558 | return gpmi_ecc_read_page_raw(mtd, chip, NULL, 1, page); |
| 1559 | } |
| 1560 | |
| 1561 | static int gpmi_ecc_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1562 | int page) |
| 1563 | { |
| 1564 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page); |
| 1565 | |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 1566 | return gpmi_ecc_write_page_raw(mtd, chip, NULL, 1, page); |
Boris BREZILLON | 7ca94e0 | 2014-11-30 19:10:30 +0100 | [diff] [blame] | 1567 | } |
| 1568 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1569 | static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 1570 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1571 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1572 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1573 | int ret = 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1574 | uint8_t *block_mark; |
| 1575 | int column, page, status, chipnr; |
| 1576 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1577 | chipnr = (int)(ofs >> chip->chip_shift); |
| 1578 | chip->select_chip(mtd, chipnr); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1579 | |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1580 | column = !GPMI_IS_MX23(this) ? mtd->writesize : 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1581 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1582 | /* Write the block mark. */ |
| 1583 | block_mark = this->data_buffer_dma; |
| 1584 | block_mark[0] = 0; /* bad block marker */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1585 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1586 | /* Shift to get page */ |
| 1587 | page = (int)(ofs >> chip->page_shift); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1588 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1589 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page); |
| 1590 | chip->write_buf(mtd, block_mark, 1); |
| 1591 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1592 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1593 | status = chip->waitfunc(mtd, chip); |
| 1594 | if (status & NAND_STATUS_FAIL) |
| 1595 | ret = -EIO; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1596 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1597 | chip->select_chip(mtd, -1); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1598 | |
| 1599 | return ret; |
| 1600 | } |
| 1601 | |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1602 | static int nand_boot_set_geometry(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1603 | { |
| 1604 | struct boot_rom_geometry *geometry = &this->rom_geometry; |
| 1605 | |
| 1606 | /* |
| 1607 | * Set the boot block stride size. |
| 1608 | * |
| 1609 | * In principle, we should be reading this from the OTP bits, since |
| 1610 | * that's where the ROM is going to get it. In fact, we don't have any |
| 1611 | * way to read the OTP bits, so we go with the default and hope for the |
| 1612 | * best. |
| 1613 | */ |
| 1614 | geometry->stride_size_in_pages = 64; |
| 1615 | |
| 1616 | /* |
| 1617 | * Set the search area stride exponent. |
| 1618 | * |
| 1619 | * In principle, we should be reading this from the OTP bits, since |
| 1620 | * that's where the ROM is going to get it. In fact, we don't have any |
| 1621 | * way to read the OTP bits, so we go with the default and hope for the |
| 1622 | * best. |
| 1623 | */ |
| 1624 | geometry->search_area_stride_exponent = 2; |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | static const char *fingerprint = "STMP"; |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1629 | static int mx23_check_transcription_stamp(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1630 | { |
| 1631 | struct boot_rom_geometry *rom_geo = &this->rom_geometry; |
| 1632 | struct device *dev = this->dev; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1633 | struct nand_chip *chip = &this->nand; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1634 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1635 | unsigned int search_area_size_in_strides; |
| 1636 | unsigned int stride; |
| 1637 | unsigned int page; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1638 | uint8_t *buffer = chip->buffers->databuf; |
| 1639 | int saved_chip_number; |
| 1640 | int found_an_ncb_fingerprint = false; |
| 1641 | |
| 1642 | /* Compute the number of strides in a search area. */ |
| 1643 | search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent; |
| 1644 | |
| 1645 | saved_chip_number = this->current_chip; |
| 1646 | chip->select_chip(mtd, 0); |
| 1647 | |
| 1648 | /* |
| 1649 | * Loop through the first search area, looking for the NCB fingerprint. |
| 1650 | */ |
| 1651 | dev_dbg(dev, "Scanning for an NCB fingerprint...\n"); |
| 1652 | |
| 1653 | for (stride = 0; stride < search_area_size_in_strides; stride++) { |
Huang Shijie | 513d57e | 2012-07-17 14:14:02 +0800 | [diff] [blame] | 1654 | /* Compute the page addresses. */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1655 | page = stride * rom_geo->stride_size_in_pages; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1656 | |
| 1657 | dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page); |
| 1658 | |
| 1659 | /* |
| 1660 | * Read the NCB fingerprint. The fingerprint is four bytes long |
| 1661 | * and starts in the 12th byte of the page. |
| 1662 | */ |
| 1663 | chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page); |
| 1664 | chip->read_buf(mtd, buffer, strlen(fingerprint)); |
| 1665 | |
| 1666 | /* Look for the fingerprint. */ |
| 1667 | if (!memcmp(buffer, fingerprint, strlen(fingerprint))) { |
| 1668 | found_an_ncb_fingerprint = true; |
| 1669 | break; |
| 1670 | } |
| 1671 | |
| 1672 | } |
| 1673 | |
| 1674 | chip->select_chip(mtd, saved_chip_number); |
| 1675 | |
| 1676 | if (found_an_ncb_fingerprint) |
| 1677 | dev_dbg(dev, "\tFound a fingerprint\n"); |
| 1678 | else |
| 1679 | dev_dbg(dev, "\tNo fingerprint found\n"); |
| 1680 | return found_an_ncb_fingerprint; |
| 1681 | } |
| 1682 | |
| 1683 | /* Writes a transcription stamp. */ |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1684 | static int mx23_write_transcription_stamp(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1685 | { |
| 1686 | struct device *dev = this->dev; |
| 1687 | struct boot_rom_geometry *rom_geo = &this->rom_geometry; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1688 | struct nand_chip *chip = &this->nand; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1689 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1690 | unsigned int block_size_in_pages; |
| 1691 | unsigned int search_area_size_in_strides; |
| 1692 | unsigned int search_area_size_in_pages; |
| 1693 | unsigned int search_area_size_in_blocks; |
| 1694 | unsigned int block; |
| 1695 | unsigned int stride; |
| 1696 | unsigned int page; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1697 | uint8_t *buffer = chip->buffers->databuf; |
| 1698 | int saved_chip_number; |
| 1699 | int status; |
| 1700 | |
| 1701 | /* Compute the search area geometry. */ |
| 1702 | block_size_in_pages = mtd->erasesize / mtd->writesize; |
| 1703 | search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent; |
| 1704 | search_area_size_in_pages = search_area_size_in_strides * |
| 1705 | rom_geo->stride_size_in_pages; |
| 1706 | search_area_size_in_blocks = |
| 1707 | (search_area_size_in_pages + (block_size_in_pages - 1)) / |
| 1708 | block_size_in_pages; |
| 1709 | |
| 1710 | dev_dbg(dev, "Search Area Geometry :\n"); |
| 1711 | dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks); |
| 1712 | dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides); |
| 1713 | dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages); |
| 1714 | |
| 1715 | /* Select chip 0. */ |
| 1716 | saved_chip_number = this->current_chip; |
| 1717 | chip->select_chip(mtd, 0); |
| 1718 | |
| 1719 | /* Loop over blocks in the first search area, erasing them. */ |
| 1720 | dev_dbg(dev, "Erasing the search area...\n"); |
| 1721 | |
| 1722 | for (block = 0; block < search_area_size_in_blocks; block++) { |
| 1723 | /* Compute the page address. */ |
| 1724 | page = block * block_size_in_pages; |
| 1725 | |
| 1726 | /* Erase this block. */ |
| 1727 | dev_dbg(dev, "\tErasing block 0x%x\n", block); |
| 1728 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 1729 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
| 1730 | |
| 1731 | /* Wait for the erase to finish. */ |
| 1732 | status = chip->waitfunc(mtd, chip); |
| 1733 | if (status & NAND_STATUS_FAIL) |
| 1734 | dev_err(dev, "[%s] Erase failed.\n", __func__); |
| 1735 | } |
| 1736 | |
| 1737 | /* Write the NCB fingerprint into the page buffer. */ |
| 1738 | memset(buffer, ~0, mtd->writesize); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1739 | memcpy(buffer + 12, fingerprint, strlen(fingerprint)); |
| 1740 | |
| 1741 | /* Loop through the first search area, writing NCB fingerprints. */ |
| 1742 | dev_dbg(dev, "Writing NCB fingerprints...\n"); |
| 1743 | for (stride = 0; stride < search_area_size_in_strides; stride++) { |
Huang Shijie | 513d57e | 2012-07-17 14:14:02 +0800 | [diff] [blame] | 1744 | /* Compute the page addresses. */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1745 | page = stride * rom_geo->stride_size_in_pages; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1746 | |
| 1747 | /* Write the first page of the current stride. */ |
| 1748 | dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page); |
| 1749 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 1750 | chip->ecc.write_page_raw(mtd, chip, buffer, 0, page); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1751 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1752 | |
| 1753 | /* Wait for the write to finish. */ |
| 1754 | status = chip->waitfunc(mtd, chip); |
| 1755 | if (status & NAND_STATUS_FAIL) |
| 1756 | dev_err(dev, "[%s] Write failed.\n", __func__); |
| 1757 | } |
| 1758 | |
| 1759 | /* Deselect chip 0. */ |
| 1760 | chip->select_chip(mtd, saved_chip_number); |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1764 | static int mx23_boot_init(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1765 | { |
| 1766 | struct device *dev = this->dev; |
| 1767 | struct nand_chip *chip = &this->nand; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1768 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1769 | unsigned int block_count; |
| 1770 | unsigned int block; |
| 1771 | int chipnr; |
| 1772 | int page; |
| 1773 | loff_t byte; |
| 1774 | uint8_t block_mark; |
| 1775 | int ret = 0; |
| 1776 | |
| 1777 | /* |
| 1778 | * If control arrives here, we can't use block mark swapping, which |
| 1779 | * means we're forced to use transcription. First, scan for the |
| 1780 | * transcription stamp. If we find it, then we don't have to do |
| 1781 | * anything -- the block marks are already transcribed. |
| 1782 | */ |
| 1783 | if (mx23_check_transcription_stamp(this)) |
| 1784 | return 0; |
| 1785 | |
| 1786 | /* |
| 1787 | * If control arrives here, we couldn't find a transcription stamp, so |
| 1788 | * so we presume the block marks are in the conventional location. |
| 1789 | */ |
| 1790 | dev_dbg(dev, "Transcribing bad block marks...\n"); |
| 1791 | |
| 1792 | /* Compute the number of blocks in the entire medium. */ |
| 1793 | block_count = chip->chipsize >> chip->phys_erase_shift; |
| 1794 | |
| 1795 | /* |
| 1796 | * Loop over all the blocks in the medium, transcribing block marks as |
| 1797 | * we go. |
| 1798 | */ |
| 1799 | for (block = 0; block < block_count; block++) { |
| 1800 | /* |
| 1801 | * Compute the chip, page and byte addresses for this block's |
| 1802 | * conventional mark. |
| 1803 | */ |
| 1804 | chipnr = block >> (chip->chip_shift - chip->phys_erase_shift); |
| 1805 | page = block << (chip->phys_erase_shift - chip->page_shift); |
| 1806 | byte = block << chip->phys_erase_shift; |
| 1807 | |
| 1808 | /* Send the command to read the conventional block mark. */ |
| 1809 | chip->select_chip(mtd, chipnr); |
| 1810 | chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page); |
| 1811 | block_mark = chip->read_byte(mtd); |
| 1812 | chip->select_chip(mtd, -1); |
| 1813 | |
| 1814 | /* |
| 1815 | * Check if the block is marked bad. If so, we need to mark it |
| 1816 | * again, but this time the result will be a mark in the |
| 1817 | * location where we transcribe block marks. |
| 1818 | */ |
| 1819 | if (block_mark != 0xff) { |
| 1820 | dev_dbg(dev, "Transcribing mark in block %u\n", block); |
| 1821 | ret = chip->block_markbad(mtd, byte); |
| 1822 | if (ret) |
Lothar Waßmann | d8c0372 | 2014-06-12 15:20:42 +0200 | [diff] [blame] | 1823 | dev_err(dev, |
| 1824 | "Failed to mark block bad with ret %d\n", |
| 1825 | ret); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | /* Write the stamp that indicates we've transcribed the block marks. */ |
| 1830 | mx23_write_transcription_stamp(this); |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1834 | static int nand_boot_init(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1835 | { |
| 1836 | nand_boot_set_geometry(this); |
| 1837 | |
| 1838 | /* This is ROM arch-specific initilization before the BBT scanning. */ |
| 1839 | if (GPMI_IS_MX23(this)) |
| 1840 | return mx23_boot_init(this); |
| 1841 | return 0; |
| 1842 | } |
| 1843 | |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1844 | static int gpmi_set_geometry(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1845 | { |
| 1846 | int ret; |
| 1847 | |
| 1848 | /* Free the temporary DMA memory for reading ID. */ |
| 1849 | gpmi_free_dma_buffer(this); |
| 1850 | |
| 1851 | /* Set up the NFC geometry which is used by BCH. */ |
| 1852 | ret = bch_set_geometry(this); |
| 1853 | if (ret) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1854 | dev_err(this->dev, "Error setting BCH geometry : %d\n", ret); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1855 | return ret; |
| 1856 | } |
| 1857 | |
| 1858 | /* Alloc the new DMA buffers according to the pagesize and oobsize */ |
| 1859 | return gpmi_alloc_dma_buffer(this); |
| 1860 | } |
| 1861 | |
Huang Shijie | ccce417 | 2013-11-14 14:25:47 +0800 | [diff] [blame] | 1862 | static void gpmi_nand_exit(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1863 | { |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1864 | nand_release(nand_to_mtd(&this->nand)); |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1865 | gpmi_free_dma_buffer(this); |
| 1866 | } |
| 1867 | |
| 1868 | static int gpmi_init_last(struct gpmi_nand_data *this) |
| 1869 | { |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1870 | struct nand_chip *chip = &this->nand; |
Boris Brezillon | 3f158e4 | 2016-02-03 20:01:54 +0100 | [diff] [blame] | 1871 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1872 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 1873 | struct bch_geometry *bch_geo = &this->bch_geometry; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1874 | int ret; |
| 1875 | |
Huang Shijie | d7364a27 | 2013-11-14 14:25:45 +0800 | [diff] [blame] | 1876 | /* Set up the medium geometry */ |
| 1877 | ret = gpmi_set_geometry(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1878 | if (ret) |
| 1879 | return ret; |
| 1880 | |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1881 | /* Init the nand_ecc_ctrl{} */ |
| 1882 | ecc->read_page = gpmi_ecc_read_page; |
| 1883 | ecc->write_page = gpmi_ecc_write_page; |
| 1884 | ecc->read_oob = gpmi_ecc_read_oob; |
| 1885 | ecc->write_oob = gpmi_ecc_write_oob; |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1886 | ecc->read_page_raw = gpmi_ecc_read_page_raw; |
| 1887 | ecc->write_page_raw = gpmi_ecc_write_page_raw; |
Boris BREZILLON | 7ca94e0 | 2014-11-30 19:10:30 +0100 | [diff] [blame] | 1888 | ecc->read_oob_raw = gpmi_ecc_read_oob_raw; |
| 1889 | ecc->write_oob_raw = gpmi_ecc_write_oob_raw; |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1890 | ecc->mode = NAND_ECC_HW; |
| 1891 | ecc->size = bch_geo->ecc_chunk_size; |
| 1892 | ecc->strength = bch_geo->ecc_strength; |
Boris Brezillon | 3f158e4 | 2016-02-03 20:01:54 +0100 | [diff] [blame] | 1893 | mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops); |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1894 | |
Huang Shijie | 995fbbf | 2012-09-13 14:57:59 +0800 | [diff] [blame] | 1895 | /* |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1896 | * We only enable the subpage read when: |
| 1897 | * (1) the chip is imx6, and |
| 1898 | * (2) the size of the ECC parity is byte aligned. |
| 1899 | */ |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 1900 | if (GPMI_IS_MX6(this) && |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1901 | ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) { |
| 1902 | ecc->read_subpage = gpmi_ecc_read_subpage; |
| 1903 | chip->options |= NAND_SUBPAGE_READ; |
| 1904 | } |
| 1905 | |
| 1906 | /* |
Huang Shijie | 995fbbf | 2012-09-13 14:57:59 +0800 | [diff] [blame] | 1907 | * Can we enable the extra features? such as EDO or Sync mode. |
| 1908 | * |
| 1909 | * We do not check the return value now. That's means if we fail in |
| 1910 | * enable the extra features, we still can run in the normal way. |
| 1911 | */ |
| 1912 | gpmi_extra_init(this); |
| 1913 | |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1914 | return 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1915 | } |
| 1916 | |
Huang Shijie | ccce417 | 2013-11-14 14:25:47 +0800 | [diff] [blame] | 1917 | static int gpmi_nand_init(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1918 | { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1919 | struct nand_chip *chip = &this->nand; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1920 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1921 | int ret; |
| 1922 | |
| 1923 | /* init current chip */ |
| 1924 | this->current_chip = -1; |
| 1925 | |
| 1926 | /* init the MTD data structures */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1927 | mtd->name = "gpmi-nand"; |
Frans Klaver | 4dc67b1 | 2015-06-10 22:38:49 +0200 | [diff] [blame] | 1928 | mtd->dev.parent = this->dev; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1929 | |
| 1930 | /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */ |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1931 | nand_set_controller_data(chip, this); |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 1932 | nand_set_flash_node(chip, this->pdev->dev.of_node); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1933 | chip->select_chip = gpmi_select_chip; |
| 1934 | chip->cmd_ctrl = gpmi_cmd_ctrl; |
| 1935 | chip->dev_ready = gpmi_dev_ready; |
| 1936 | chip->read_byte = gpmi_read_byte; |
| 1937 | chip->read_buf = gpmi_read_buf; |
| 1938 | chip->write_buf = gpmi_write_buf; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1939 | chip->badblock_pattern = &gpmi_bbt_descr; |
| 1940 | chip->block_markbad = gpmi_block_markbad; |
| 1941 | chip->options |= NAND_NO_SUBPAGE_WRITE; |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1942 | |
| 1943 | /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */ |
| 1944 | this->swap_block_mark = !GPMI_IS_MX23(this); |
| 1945 | |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1946 | /* |
| 1947 | * Allocate a temporary DMA buffer for reading ID in the |
| 1948 | * nand_scan_ident(). |
| 1949 | */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1950 | this->bch_geometry.payload_size = 1024; |
| 1951 | this->bch_geometry.auxiliary_size = 128; |
| 1952 | ret = gpmi_alloc_dma_buffer(this); |
| 1953 | if (ret) |
| 1954 | goto err_out; |
| 1955 | |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 1956 | ret = nand_scan_ident(mtd, GPMI_IS_MX6(this) ? 2 : 1, NULL); |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1957 | if (ret) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1958 | goto err_out; |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1959 | |
Boris Brezillon | f05f6a1 | 2016-04-01 14:54:26 +0200 | [diff] [blame^] | 1960 | if (chip->bbt_options & NAND_BBT_USE_FLASH) { |
| 1961 | chip->bbt_options |= NAND_BBT_NO_OOB; |
| 1962 | |
| 1963 | if (of_property_read_bool(this->dev->of_node, |
| 1964 | "fsl,no-blockmark-swap")) |
| 1965 | this->swap_block_mark = false; |
| 1966 | } |
| 1967 | dev_dbg(this->dev, "Blockmark swapping %sabled\n", |
| 1968 | this->swap_block_mark ? "en" : "dis"); |
| 1969 | |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1970 | ret = gpmi_init_last(this); |
| 1971 | if (ret) |
| 1972 | goto err_out; |
| 1973 | |
Huang Shijie | 885d71e | 2013-11-12 12:23:08 +0800 | [diff] [blame] | 1974 | chip->options |= NAND_SKIP_BBTSCAN; |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1975 | ret = nand_scan_tail(mtd); |
| 1976 | if (ret) |
| 1977 | goto err_out; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1978 | |
Huang Shijie | 885d71e | 2013-11-12 12:23:08 +0800 | [diff] [blame] | 1979 | ret = nand_boot_init(this); |
| 1980 | if (ret) |
| 1981 | goto err_out; |
Fabio Estevam | 899b834 | 2015-02-09 19:22:33 -0200 | [diff] [blame] | 1982 | ret = chip->scan_bbt(mtd); |
| 1983 | if (ret) |
| 1984 | goto err_out; |
Huang Shijie | 885d71e | 2013-11-12 12:23:08 +0800 | [diff] [blame] | 1985 | |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 1986 | ret = mtd_device_register(mtd, NULL, 0); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1987 | if (ret) |
| 1988 | goto err_out; |
| 1989 | return 0; |
| 1990 | |
| 1991 | err_out: |
Huang Shijie | ccce417 | 2013-11-14 14:25:47 +0800 | [diff] [blame] | 1992 | gpmi_nand_exit(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1993 | return ret; |
| 1994 | } |
| 1995 | |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 1996 | static const struct of_device_id gpmi_nand_id_table[] = { |
| 1997 | { |
| 1998 | .compatible = "fsl,imx23-gpmi-nand", |
Lothar Waßmann | 6a76096 | 2014-06-12 15:20:41 +0200 | [diff] [blame] | 1999 | .data = &gpmi_devdata_imx23, |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 2000 | }, { |
| 2001 | .compatible = "fsl,imx28-gpmi-nand", |
Lothar Waßmann | 6a76096 | 2014-06-12 15:20:41 +0200 | [diff] [blame] | 2002 | .data = &gpmi_devdata_imx28, |
Huang Shijie | 9013bb4 | 2012-05-04 21:42:06 -0400 | [diff] [blame] | 2003 | }, { |
| 2004 | .compatible = "fsl,imx6q-gpmi-nand", |
Lothar Waßmann | 6a76096 | 2014-06-12 15:20:41 +0200 | [diff] [blame] | 2005 | .data = &gpmi_devdata_imx6q, |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 2006 | }, { |
| 2007 | .compatible = "fsl,imx6sx-gpmi-nand", |
Lothar Waßmann | 6a76096 | 2014-06-12 15:20:41 +0200 | [diff] [blame] | 2008 | .data = &gpmi_devdata_imx6sx, |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 2009 | }, {} |
| 2010 | }; |
| 2011 | MODULE_DEVICE_TABLE(of, gpmi_nand_id_table); |
| 2012 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 2013 | static int gpmi_nand_probe(struct platform_device *pdev) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2014 | { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2015 | struct gpmi_nand_data *this; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 2016 | const struct of_device_id *of_id; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2017 | int ret; |
| 2018 | |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 2019 | this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL); |
| 2020 | if (!this) |
| 2021 | return -ENOMEM; |
| 2022 | |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 2023 | of_id = of_match_device(gpmi_nand_id_table, &pdev->dev); |
| 2024 | if (of_id) { |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 2025 | this->devdata = of_id->data; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 2026 | } else { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 2027 | dev_err(&pdev->dev, "Failed to find the right device id.\n"); |
Lothar Waßmann | 52a073b | 2013-08-07 08:15:38 +0200 | [diff] [blame] | 2028 | return -ENODEV; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 2029 | } |
| 2030 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2031 | platform_set_drvdata(pdev, this); |
| 2032 | this->pdev = pdev; |
| 2033 | this->dev = &pdev->dev; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2034 | |
| 2035 | ret = acquire_resources(this); |
| 2036 | if (ret) |
| 2037 | goto exit_acquire_resources; |
| 2038 | |
| 2039 | ret = init_hardware(this); |
| 2040 | if (ret) |
| 2041 | goto exit_nfc_init; |
| 2042 | |
Huang Shijie | ccce417 | 2013-11-14 14:25:47 +0800 | [diff] [blame] | 2043 | ret = gpmi_nand_init(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2044 | if (ret) |
| 2045 | goto exit_nfc_init; |
| 2046 | |
Fabio Estevam | 490e280 | 2012-09-05 11:35:24 -0300 | [diff] [blame] | 2047 | dev_info(this->dev, "driver registered.\n"); |
| 2048 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2049 | return 0; |
| 2050 | |
| 2051 | exit_nfc_init: |
| 2052 | release_resources(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2053 | exit_acquire_resources: |
Fabio Estevam | 490e280 | 2012-09-05 11:35:24 -0300 | [diff] [blame] | 2054 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2055 | return ret; |
| 2056 | } |
| 2057 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 2058 | static int gpmi_nand_remove(struct platform_device *pdev) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2059 | { |
| 2060 | struct gpmi_nand_data *this = platform_get_drvdata(pdev); |
| 2061 | |
Huang Shijie | ccce417 | 2013-11-14 14:25:47 +0800 | [diff] [blame] | 2062 | gpmi_nand_exit(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2063 | release_resources(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2064 | return 0; |
| 2065 | } |
| 2066 | |
Huang Shijie | 026918e | 2015-12-02 16:47:40 -0600 | [diff] [blame] | 2067 | #ifdef CONFIG_PM_SLEEP |
| 2068 | static int gpmi_pm_suspend(struct device *dev) |
| 2069 | { |
| 2070 | struct gpmi_nand_data *this = dev_get_drvdata(dev); |
| 2071 | |
| 2072 | release_dma_channels(this); |
| 2073 | return 0; |
| 2074 | } |
| 2075 | |
| 2076 | static int gpmi_pm_resume(struct device *dev) |
| 2077 | { |
| 2078 | struct gpmi_nand_data *this = dev_get_drvdata(dev); |
| 2079 | int ret; |
| 2080 | |
| 2081 | ret = acquire_dma_channels(this); |
| 2082 | if (ret < 0) |
| 2083 | return ret; |
| 2084 | |
| 2085 | /* re-init the GPMI registers */ |
| 2086 | this->flags &= ~GPMI_TIMING_INIT_OK; |
| 2087 | ret = gpmi_init(this); |
| 2088 | if (ret) { |
| 2089 | dev_err(this->dev, "Error setting GPMI : %d\n", ret); |
| 2090 | return ret; |
| 2091 | } |
| 2092 | |
| 2093 | /* re-init the BCH registers */ |
| 2094 | ret = bch_set_geometry(this); |
| 2095 | if (ret) { |
| 2096 | dev_err(this->dev, "Error setting BCH : %d\n", ret); |
| 2097 | return ret; |
| 2098 | } |
| 2099 | |
| 2100 | /* re-init others */ |
| 2101 | gpmi_extra_init(this); |
| 2102 | |
| 2103 | return 0; |
| 2104 | } |
| 2105 | #endif /* CONFIG_PM_SLEEP */ |
| 2106 | |
| 2107 | static const struct dev_pm_ops gpmi_pm_ops = { |
| 2108 | SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume) |
| 2109 | }; |
| 2110 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2111 | static struct platform_driver gpmi_nand_driver = { |
| 2112 | .driver = { |
| 2113 | .name = "gpmi-nand", |
Huang Shijie | 026918e | 2015-12-02 16:47:40 -0600 | [diff] [blame] | 2114 | .pm = &gpmi_pm_ops, |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 2115 | .of_match_table = gpmi_nand_id_table, |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2116 | }, |
| 2117 | .probe = gpmi_nand_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 2118 | .remove = gpmi_nand_remove, |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2119 | }; |
Fabio Estevam | 490e280 | 2012-09-05 11:35:24 -0300 | [diff] [blame] | 2120 | module_platform_driver(gpmi_nand_driver); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2121 | |
| 2122 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 2123 | MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver"); |
| 2124 | MODULE_LICENSE("GPL"); |