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