1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Freescale GPMI NAND Flash Driver 4 * 5 * Copyright (C) 2010-2015 Freescale Semiconductor, Inc. 6 * Copyright (C) 2008 Embedded Alley Solutions, Inc. 7 */ 8 #include <linux/clk.h> 9 #include <linux/delay.h> 10 #include <linux/slab.h> 11 #include <linux/sched/task_stack.h> 12 #include <linux/interrupt.h> 13 #include <linux/module.h> 14 #include <linux/mtd/partitions.h> 15 #include <linux/of.h> 16 #include <linux/of_device.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/dma/mxs-dma.h> 19 #include "gpmi-nand.h" 20 #include "gpmi-regs.h" 21 #include "bch-regs.h" 22 23 /* Resource names for the GPMI NAND driver. */ 24 #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand" 25 #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch" 26 #define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch" 27 28 /* Converts time to clock cycles */ 29 #define TO_CYCLES(duration, period) DIV_ROUND_UP_ULL(duration, period) 30 31 #define MXS_SET_ADDR 0x4 32 #define MXS_CLR_ADDR 0x8 33 /* 34 * Clear the bit and poll it cleared. This is usually called with 35 * a reset address and mask being either SFTRST(bit 31) or CLKGATE 36 * (bit 30). 37 */ 38 static int clear_poll_bit(void __iomem *addr, u32 mask) 39 { 40 int timeout = 0x400; 41 42 /* clear the bit */ 43 writel(mask, addr + MXS_CLR_ADDR); 44 45 /* 46 * SFTRST needs 3 GPMI clocks to settle, the reference manual 47 * recommends to wait 1us. 48 */ 49 udelay(1); 50 51 /* poll the bit becoming clear */ 52 while ((readl(addr) & mask) && --timeout) 53 /* nothing */; 54 55 return !timeout; 56 } 57 58 #define MODULE_CLKGATE (1 << 30) 59 #define MODULE_SFTRST (1 << 31) 60 /* 61 * The current mxs_reset_block() will do two things: 62 * [1] enable the module. 63 * [2] reset the module. 64 * 65 * In most of the cases, it's ok. 66 * But in MX23, there is a hardware bug in the BCH block (see erratum #2847). 67 * If you try to soft reset the BCH block, it becomes unusable until 68 * the next hard reset. This case occurs in the NAND boot mode. When the board 69 * boots by NAND, the ROM of the chip will initialize the BCH blocks itself. 70 * So If the driver tries to reset the BCH again, the BCH will not work anymore. 71 * You will see a DMA timeout in this case. The bug has been fixed 72 * in the following chips, such as MX28. 73 * 74 * To avoid this bug, just add a new parameter `just_enable` for 75 * the mxs_reset_block(), and rewrite it here. 76 */ 77 static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable) 78 { 79 int ret; 80 int timeout = 0x400; 81 82 /* clear and poll SFTRST */ 83 ret = clear_poll_bit(reset_addr, MODULE_SFTRST); 84 if (unlikely(ret)) 85 goto error; 86 87 /* clear CLKGATE */ 88 writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR); 89 90 if (!just_enable) { 91 /* set SFTRST to reset the block */ 92 writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR); 93 udelay(1); 94 95 /* poll CLKGATE becoming set */ 96 while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout) 97 /* nothing */; 98 if (unlikely(!timeout)) 99 goto error; 100 } 101 102 /* clear and poll SFTRST */ 103 ret = clear_poll_bit(reset_addr, MODULE_SFTRST); 104 if (unlikely(ret)) 105 goto error; 106 107 /* clear and poll CLKGATE */ 108 ret = clear_poll_bit(reset_addr, MODULE_CLKGATE); 109 if (unlikely(ret)) 110 goto error; 111 112 return 0; 113 114 error: 115 pr_err("%s(%p): module reset timeout\n", __func__, reset_addr); 116 return -ETIMEDOUT; 117 } 118 119 static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v) 120 { 121 struct clk *clk; 122 int ret; 123 int i; 124 125 for (i = 0; i < GPMI_CLK_MAX; i++) { 126 clk = this->resources.clock[i]; 127 if (!clk) 128 break; 129 130 if (v) { 131 ret = clk_prepare_enable(clk); 132 if (ret) 133 goto err_clk; 134 } else { 135 clk_disable_unprepare(clk); 136 } 137 } 138 return 0; 139 140 err_clk: 141 for (; i > 0; i--) 142 clk_disable_unprepare(this->resources.clock[i - 1]); 143 return ret; 144 } 145 146 static int gpmi_init(struct gpmi_nand_data *this) 147 { 148 struct resources *r = &this->resources; 149 int ret; 150 151 ret = pm_runtime_get_sync(this->dev); 152 if (ret < 0) 153 return ret; 154 155 ret = gpmi_reset_block(r->gpmi_regs, false); 156 if (ret) 157 goto err_out; 158 159 /* 160 * Reset BCH here, too. We got failures otherwise :( 161 * See later BCH reset for explanation of MX23 and MX28 handling 162 */ 163 ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MXS(this)); 164 if (ret) 165 goto err_out; 166 167 /* Choose NAND mode. */ 168 writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR); 169 170 /* Set the IRQ polarity. */ 171 writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY, 172 r->gpmi_regs + HW_GPMI_CTRL1_SET); 173 174 /* Disable Write-Protection. */ 175 writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET); 176 177 /* Select BCH ECC. */ 178 writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET); 179 180 /* 181 * Decouple the chip select from dma channel. We use dma0 for all 182 * the chips. 183 */ 184 writel(BM_GPMI_CTRL1_DECOUPLE_CS, r->gpmi_regs + HW_GPMI_CTRL1_SET); 185 186 err_out: 187 pm_runtime_mark_last_busy(this->dev); 188 pm_runtime_put_autosuspend(this->dev); 189 return ret; 190 } 191 192 /* This function is very useful. It is called only when the bug occur. */ 193 static void gpmi_dump_info(struct gpmi_nand_data *this) 194 { 195 struct resources *r = &this->resources; 196 struct bch_geometry *geo = &this->bch_geometry; 197 u32 reg; 198 int i; 199 200 dev_err(this->dev, "Show GPMI registers :\n"); 201 for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) { 202 reg = readl(r->gpmi_regs + i * 0x10); 203 dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg); 204 } 205 206 /* start to print out the BCH info */ 207 dev_err(this->dev, "Show BCH registers :\n"); 208 for (i = 0; i <= HW_BCH_VERSION / 0x10 + 1; i++) { 209 reg = readl(r->bch_regs + i * 0x10); 210 dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg); 211 } 212 dev_err(this->dev, "BCH Geometry :\n" 213 "GF length : %u\n" 214 "ECC Strength : %u\n" 215 "Page Size in Bytes : %u\n" 216 "Metadata Size in Bytes : %u\n" 217 "ECC Chunk Size in Bytes: %u\n" 218 "ECC Chunk Count : %u\n" 219 "Payload Size in Bytes : %u\n" 220 "Auxiliary Size in Bytes: %u\n" 221 "Auxiliary Status Offset: %u\n" 222 "Block Mark Byte Offset : %u\n" 223 "Block Mark Bit Offset : %u\n", 224 geo->gf_len, 225 geo->ecc_strength, 226 geo->page_size, 227 geo->metadata_size, 228 geo->ecc_chunk_size, 229 geo->ecc_chunk_count, 230 geo->payload_size, 231 geo->auxiliary_size, 232 geo->auxiliary_status_offset, 233 geo->block_mark_byte_offset, 234 geo->block_mark_bit_offset); 235 } 236 237 static inline bool gpmi_check_ecc(struct gpmi_nand_data *this) 238 { 239 struct bch_geometry *geo = &this->bch_geometry; 240 241 /* Do the sanity check. */ 242 if (GPMI_IS_MXS(this)) { 243 /* The mx23/mx28 only support the GF13. */ 244 if (geo->gf_len == 14) 245 return false; 246 } 247 return geo->ecc_strength <= this->devdata->bch_max_ecc_strength; 248 } 249 250 /* 251 * If we can get the ECC information from the nand chip, we do not 252 * need to calculate them ourselves. 253 * 254 * We may have available oob space in this case. 255 */ 256 static int set_geometry_by_ecc_info(struct gpmi_nand_data *this, 257 unsigned int ecc_strength, 258 unsigned int ecc_step) 259 { 260 struct bch_geometry *geo = &this->bch_geometry; 261 struct nand_chip *chip = &this->nand; 262 struct mtd_info *mtd = nand_to_mtd(chip); 263 unsigned int block_mark_bit_offset; 264 265 switch (ecc_step) { 266 case SZ_512: 267 geo->gf_len = 13; 268 break; 269 case SZ_1K: 270 geo->gf_len = 14; 271 break; 272 default: 273 dev_err(this->dev, 274 "unsupported nand chip. ecc bits : %d, ecc size : %d\n", 275 nanddev_get_ecc_requirements(&chip->base)->strength, 276 nanddev_get_ecc_requirements(&chip->base)->step_size); 277 return -EINVAL; 278 } 279 geo->ecc_chunk_size = ecc_step; 280 geo->ecc_strength = round_up(ecc_strength, 2); 281 if (!gpmi_check_ecc(this)) 282 return -EINVAL; 283 284 /* Keep the C >= O */ 285 if (geo->ecc_chunk_size < mtd->oobsize) { 286 dev_err(this->dev, 287 "unsupported nand chip. ecc size: %d, oob size : %d\n", 288 ecc_step, mtd->oobsize); 289 return -EINVAL; 290 } 291 292 /* The default value, see comment in the legacy_set_geometry(). */ 293 geo->metadata_size = 10; 294 295 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size; 296 297 /* 298 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below: 299 * 300 * | P | 301 * |<----------------------------------------------------->| 302 * | | 303 * | (Block Mark) | 304 * | P' | | | | 305 * |<-------------------------------------------->| D | | O' | 306 * | |<---->| |<--->| 307 * V V V V V 308 * +---+----------+-+----------+-+----------+-+----------+-+-----+ 309 * | M | data |E| data |E| data |E| data |E| | 310 * +---+----------+-+----------+-+----------+-+----------+-+-----+ 311 * ^ ^ 312 * | O | 313 * |<------------>| 314 * | | 315 * 316 * P : the page size for BCH module. 317 * E : The ECC strength. 318 * G : the length of Galois Field. 319 * N : The chunk count of per page. 320 * M : the metasize of per page. 321 * C : the ecc chunk size, aka the "data" above. 322 * P': the nand chip's page size. 323 * O : the nand chip's oob size. 324 * O': the free oob. 325 * 326 * The formula for P is : 327 * 328 * E * G * N 329 * P = ------------ + P' + M 330 * 8 331 * 332 * The position of block mark moves forward in the ECC-based view 333 * of page, and the delta is: 334 * 335 * E * G * (N - 1) 336 * D = (---------------- + M) 337 * 8 338 * 339 * Please see the comment in legacy_set_geometry(). 340 * With the condition C >= O , we still can get same result. 341 * So the bit position of the physical block mark within the ECC-based 342 * view of the page is : 343 * (P' - D) * 8 344 */ 345 geo->page_size = mtd->writesize + geo->metadata_size + 346 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8; 347 348 geo->payload_size = mtd->writesize; 349 350 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4); 351 geo->auxiliary_size = ALIGN(geo->metadata_size, 4) 352 + ALIGN(geo->ecc_chunk_count, 4); 353 354 if (!this->swap_block_mark) 355 return 0; 356 357 /* For bit swap. */ 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 367 /* 368 * Calculate the ECC strength by hand: 369 * E : The ECC strength. 370 * G : the length of Galois Field. 371 * N : The chunk count of per page. 372 * O : the oobsize of the NAND chip. 373 * M : the metasize of per page. 374 * 375 * The formula is : 376 * E * G * N 377 * ------------ <= (O - M) 378 * 8 379 * 380 * So, we get E by: 381 * (O - M) * 8 382 * E <= ------------- 383 * G * N 384 */ 385 static inline int get_ecc_strength(struct gpmi_nand_data *this) 386 { 387 struct bch_geometry *geo = &this->bch_geometry; 388 struct mtd_info *mtd = nand_to_mtd(&this->nand); 389 int ecc_strength; 390 391 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8) 392 / (geo->gf_len * geo->ecc_chunk_count); 393 394 /* We need the minor even number. */ 395 return round_down(ecc_strength, 2); 396 } 397 398 static int legacy_set_geometry(struct gpmi_nand_data *this) 399 { 400 struct bch_geometry *geo = &this->bch_geometry; 401 struct mtd_info *mtd = nand_to_mtd(&this->nand); 402 unsigned int metadata_size; 403 unsigned int status_size; 404 unsigned int block_mark_bit_offset; 405 406 /* 407 * The size of the metadata can be changed, though we set it to 10 408 * bytes now. But it can't be too large, because we have to save 409 * enough space for BCH. 410 */ 411 geo->metadata_size = 10; 412 413 /* The default for the length of Galois Field. */ 414 geo->gf_len = 13; 415 416 /* The default for chunk size. */ 417 geo->ecc_chunk_size = 512; 418 while (geo->ecc_chunk_size < mtd->oobsize) { 419 geo->ecc_chunk_size *= 2; /* keep C >= O */ 420 geo->gf_len = 14; 421 } 422 423 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size; 424 425 /* We use the same ECC strength for all chunks. */ 426 geo->ecc_strength = get_ecc_strength(this); 427 if (!gpmi_check_ecc(this)) { 428 dev_err(this->dev, 429 "ecc strength: %d cannot be supported by the controller (%d)\n" 430 "try to use minimum ecc strength that NAND chip required\n", 431 geo->ecc_strength, 432 this->devdata->bch_max_ecc_strength); 433 return -EINVAL; 434 } 435 436 geo->page_size = mtd->writesize + geo->metadata_size + 437 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8; 438 geo->payload_size = mtd->writesize; 439 440 /* 441 * The auxiliary buffer contains the metadata and the ECC status. The 442 * metadata is padded to the nearest 32-bit boundary. The ECC status 443 * contains one byte for every ECC chunk, and is also padded to the 444 * nearest 32-bit boundary. 445 */ 446 metadata_size = ALIGN(geo->metadata_size, 4); 447 status_size = ALIGN(geo->ecc_chunk_count, 4); 448 449 geo->auxiliary_size = metadata_size + status_size; 450 geo->auxiliary_status_offset = metadata_size; 451 452 if (!this->swap_block_mark) 453 return 0; 454 455 /* 456 * We need to compute the byte and bit offsets of 457 * the physical block mark within the ECC-based view of the page. 458 * 459 * NAND chip with 2K page shows below: 460 * (Block Mark) 461 * | | 462 * | D | 463 * |<---->| 464 * V V 465 * +---+----------+-+----------+-+----------+-+----------+-+ 466 * | M | data |E| data |E| data |E| data |E| 467 * +---+----------+-+----------+-+----------+-+----------+-+ 468 * 469 * The position of block mark moves forward in the ECC-based view 470 * of page, and the delta is: 471 * 472 * E * G * (N - 1) 473 * D = (---------------- + M) 474 * 8 475 * 476 * With the formula to compute the ECC strength, and the condition 477 * : C >= O (C is the ecc chunk size) 478 * 479 * It's easy to deduce to the following result: 480 * 481 * E * G (O - M) C - M C - M 482 * ----------- <= ------- <= -------- < --------- 483 * 8 N N (N - 1) 484 * 485 * So, we get: 486 * 487 * E * G * (N - 1) 488 * D = (---------------- + M) < C 489 * 8 490 * 491 * The above inequality means the position of block mark 492 * within the ECC-based view of the page is still in the data chunk, 493 * and it's NOT in the ECC bits of the chunk. 494 * 495 * Use the following to compute the bit position of the 496 * physical block mark within the ECC-based view of the page: 497 * (page_size - D) * 8 498 * 499 * --Huang Shijie 500 */ 501 block_mark_bit_offset = mtd->writesize * 8 - 502 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1) 503 + geo->metadata_size * 8); 504 505 geo->block_mark_byte_offset = block_mark_bit_offset / 8; 506 geo->block_mark_bit_offset = block_mark_bit_offset % 8; 507 return 0; 508 } 509 510 static int common_nfc_set_geometry(struct gpmi_nand_data *this) 511 { 512 struct nand_chip *chip = &this->nand; 513 const struct nand_ecc_props *requirements = 514 nanddev_get_ecc_requirements(&chip->base); 515 516 if (chip->ecc.strength > 0 && chip->ecc.size > 0) 517 return set_geometry_by_ecc_info(this, chip->ecc.strength, 518 chip->ecc.size); 519 520 if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc")) 521 || legacy_set_geometry(this)) { 522 if (!(requirements->strength > 0 && requirements->step_size > 0)) 523 return -EINVAL; 524 525 return set_geometry_by_ecc_info(this, 526 requirements->strength, 527 requirements->step_size); 528 } 529 530 return 0; 531 } 532 533 /* Configures the geometry for BCH. */ 534 static int bch_set_geometry(struct gpmi_nand_data *this) 535 { 536 struct resources *r = &this->resources; 537 int ret; 538 539 ret = common_nfc_set_geometry(this); 540 if (ret) 541 return ret; 542 543 ret = pm_runtime_get_sync(this->dev); 544 if (ret < 0) { 545 pm_runtime_put_autosuspend(this->dev); 546 return ret; 547 } 548 549 /* 550 * Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this 551 * chip, otherwise it will lock up. So we skip resetting BCH on the MX23. 552 * and MX28. 553 */ 554 ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MXS(this)); 555 if (ret) 556 goto err_out; 557 558 /* Set *all* chip selects to use layout 0. */ 559 writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT); 560 561 ret = 0; 562 err_out: 563 pm_runtime_mark_last_busy(this->dev); 564 pm_runtime_put_autosuspend(this->dev); 565 566 return ret; 567 } 568 569 /* 570 * <1> Firstly, we should know what's the GPMI-clock means. 571 * The GPMI-clock is the internal clock in the gpmi nand controller. 572 * If you set 100MHz to gpmi nand controller, the GPMI-clock's period 573 * is 10ns. Mark the GPMI-clock's period as GPMI-clock-period. 574 * 575 * <2> Secondly, we should know what's the frequency on the nand chip pins. 576 * The frequency on the nand chip pins is derived from the GPMI-clock. 577 * We can get it from the following equation: 578 * 579 * F = G / (DS + DH) 580 * 581 * F : the frequency on the nand chip pins. 582 * G : the GPMI clock, such as 100MHz. 583 * DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP 584 * DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD 585 * 586 * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz, 587 * the nand EDO(extended Data Out) timing could be applied. 588 * The GPMI implements a feedback read strobe to sample the read data. 589 * The feedback read strobe can be delayed to support the nand EDO timing 590 * where the read strobe may deasserts before the read data is valid, and 591 * read data is valid for some time after read strobe. 592 * 593 * The following figure illustrates some aspects of a NAND Flash read: 594 * 595 * |<---tREA---->| 596 * | | 597 * | | | 598 * |<--tRP-->| | 599 * | | | 600 * __ ___|__________________________________ 601 * RDN \________/ | 602 * | 603 * /---------\ 604 * Read Data --------------< >--------- 605 * \---------/ 606 * | | 607 * |<-D->| 608 * FeedbackRDN ________ ____________ 609 * \___________/ 610 * 611 * D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY. 612 * 613 * 614 * <4> Now, we begin to describe how to compute the right RDN_DELAY. 615 * 616 * 4.1) From the aspect of the nand chip pins: 617 * Delay = (tREA + C - tRP) {1} 618 * 619 * tREA : the maximum read access time. 620 * C : a constant to adjust the delay. default is 4000ps. 621 * tRP : the read pulse width, which is exactly: 622 * tRP = (GPMI-clock-period) * DATA_SETUP 623 * 624 * 4.2) From the aspect of the GPMI nand controller: 625 * Delay = RDN_DELAY * 0.125 * RP {2} 626 * 627 * RP : the DLL reference period. 628 * if (GPMI-clock-period > DLL_THRETHOLD) 629 * RP = GPMI-clock-period / 2; 630 * else 631 * RP = GPMI-clock-period; 632 * 633 * Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period 634 * is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD 635 * is 16000ps, but in mx6q, we use 12000ps. 636 * 637 * 4.3) since {1} equals {2}, we get: 638 * 639 * (tREA + 4000 - tRP) * 8 640 * RDN_DELAY = ----------------------- {3} 641 * RP 642 */ 643 static void gpmi_nfc_compute_timings(struct gpmi_nand_data *this, 644 const struct nand_sdr_timings *sdr) 645 { 646 struct gpmi_nfc_hardware_timing *hw = &this->hw; 647 unsigned int dll_threshold_ps = this->devdata->max_chain_delay; 648 unsigned int period_ps, reference_period_ps; 649 unsigned int data_setup_cycles, data_hold_cycles, addr_setup_cycles; 650 unsigned int tRP_ps; 651 bool use_half_period; 652 int sample_delay_ps, sample_delay_factor; 653 u16 busy_timeout_cycles; 654 u8 wrn_dly_sel; 655 656 if (sdr->tRC_min >= 30000) { 657 /* ONFI non-EDO modes [0-3] */ 658 hw->clk_rate = 22000000; 659 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS; 660 } else if (sdr->tRC_min >= 25000) { 661 /* ONFI EDO mode 4 */ 662 hw->clk_rate = 80000000; 663 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY; 664 } else { 665 /* ONFI EDO mode 5 */ 666 hw->clk_rate = 100000000; 667 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY; 668 } 669 670 /* SDR core timings are given in picoseconds */ 671 period_ps = div_u64((u64)NSEC_PER_SEC * 1000, hw->clk_rate); 672 673 addr_setup_cycles = TO_CYCLES(sdr->tALS_min, period_ps); 674 data_setup_cycles = TO_CYCLES(sdr->tDS_min, period_ps); 675 data_hold_cycles = TO_CYCLES(sdr->tDH_min, period_ps); 676 busy_timeout_cycles = TO_CYCLES(sdr->tWB_max + sdr->tR_max, period_ps); 677 678 hw->timing0 = BF_GPMI_TIMING0_ADDRESS_SETUP(addr_setup_cycles) | 679 BF_GPMI_TIMING0_DATA_HOLD(data_hold_cycles) | 680 BF_GPMI_TIMING0_DATA_SETUP(data_setup_cycles); 681 hw->timing1 = BF_GPMI_TIMING1_BUSY_TIMEOUT(busy_timeout_cycles * 4096); 682 683 /* 684 * Derive NFC ideal delay from {3}: 685 * 686 * (tREA + 4000 - tRP) * 8 687 * RDN_DELAY = ----------------------- 688 * RP 689 */ 690 if (period_ps > dll_threshold_ps) { 691 use_half_period = true; 692 reference_period_ps = period_ps / 2; 693 } else { 694 use_half_period = false; 695 reference_period_ps = period_ps; 696 } 697 698 tRP_ps = data_setup_cycles * period_ps; 699 sample_delay_ps = (sdr->tREA_max + 4000 - tRP_ps) * 8; 700 if (sample_delay_ps > 0) 701 sample_delay_factor = sample_delay_ps / reference_period_ps; 702 else 703 sample_delay_factor = 0; 704 705 hw->ctrl1n = BF_GPMI_CTRL1_WRN_DLY_SEL(wrn_dly_sel); 706 if (sample_delay_factor) 707 hw->ctrl1n |= BF_GPMI_CTRL1_RDN_DELAY(sample_delay_factor) | 708 BM_GPMI_CTRL1_DLL_ENABLE | 709 (use_half_period ? BM_GPMI_CTRL1_HALF_PERIOD : 0); 710 } 711 712 static void gpmi_nfc_apply_timings(struct gpmi_nand_data *this) 713 { 714 struct gpmi_nfc_hardware_timing *hw = &this->hw; 715 struct resources *r = &this->resources; 716 void __iomem *gpmi_regs = r->gpmi_regs; 717 unsigned int dll_wait_time_us; 718 719 clk_set_rate(r->clock[0], hw->clk_rate); 720 721 writel(hw->timing0, gpmi_regs + HW_GPMI_TIMING0); 722 writel(hw->timing1, gpmi_regs + HW_GPMI_TIMING1); 723 724 /* 725 * Clear several CTRL1 fields, DLL must be disabled when setting 726 * RDN_DELAY or HALF_PERIOD. 727 */ 728 writel(BM_GPMI_CTRL1_CLEAR_MASK, gpmi_regs + HW_GPMI_CTRL1_CLR); 729 writel(hw->ctrl1n, gpmi_regs + HW_GPMI_CTRL1_SET); 730 731 /* Wait 64 clock cycles before using the GPMI after enabling the DLL */ 732 dll_wait_time_us = USEC_PER_SEC / hw->clk_rate * 64; 733 if (!dll_wait_time_us) 734 dll_wait_time_us = 1; 735 736 /* Wait for the DLL to settle. */ 737 udelay(dll_wait_time_us); 738 } 739 740 static int gpmi_setup_interface(struct nand_chip *chip, int chipnr, 741 const struct nand_interface_config *conf) 742 { 743 struct gpmi_nand_data *this = nand_get_controller_data(chip); 744 const struct nand_sdr_timings *sdr; 745 746 /* Retrieve required NAND timings */ 747 sdr = nand_get_sdr_timings(conf); 748 if (IS_ERR(sdr)) 749 return PTR_ERR(sdr); 750 751 /* Only MX6 GPMI controller can reach EDO timings */ 752 if (sdr->tRC_min <= 25000 && !GPMI_IS_MX6(this)) 753 return -ENOTSUPP; 754 755 /* Stop here if this call was just a check */ 756 if (chipnr < 0) 757 return 0; 758 759 /* Do the actual derivation of the controller timings */ 760 gpmi_nfc_compute_timings(this, sdr); 761 762 this->hw.must_apply_timings = true; 763 764 return 0; 765 } 766 767 /* Clears a BCH interrupt. */ 768 static void gpmi_clear_bch(struct gpmi_nand_data *this) 769 { 770 struct resources *r = &this->resources; 771 writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR); 772 } 773 774 static struct dma_chan *get_dma_chan(struct gpmi_nand_data *this) 775 { 776 /* We use the DMA channel 0 to access all the nand chips. */ 777 return this->dma_chans[0]; 778 } 779 780 /* This will be called after the DMA operation is finished. */ 781 static void dma_irq_callback(void *param) 782 { 783 struct gpmi_nand_data *this = param; 784 struct completion *dma_c = &this->dma_done; 785 786 complete(dma_c); 787 } 788 789 static irqreturn_t bch_irq(int irq, void *cookie) 790 { 791 struct gpmi_nand_data *this = cookie; 792 793 gpmi_clear_bch(this); 794 complete(&this->bch_done); 795 return IRQ_HANDLED; 796 } 797 798 static int gpmi_raw_len_to_len(struct gpmi_nand_data *this, int raw_len) 799 { 800 /* 801 * raw_len is the length to read/write including bch data which 802 * we are passed in exec_op. Calculate the data length from it. 803 */ 804 if (this->bch) 805 return ALIGN_DOWN(raw_len, this->bch_geometry.ecc_chunk_size); 806 else 807 return raw_len; 808 } 809 810 /* Can we use the upper's buffer directly for DMA? */ 811 static bool prepare_data_dma(struct gpmi_nand_data *this, const void *buf, 812 int raw_len, struct scatterlist *sgl, 813 enum dma_data_direction dr) 814 { 815 int ret; 816 int len = gpmi_raw_len_to_len(this, raw_len); 817 818 /* first try to map the upper buffer directly */ 819 if (virt_addr_valid(buf) && !object_is_on_stack(buf)) { 820 sg_init_one(sgl, buf, len); 821 ret = dma_map_sg(this->dev, sgl, 1, dr); 822 if (ret == 0) 823 goto map_fail; 824 825 return true; 826 } 827 828 map_fail: 829 /* We have to use our own DMA buffer. */ 830 sg_init_one(sgl, this->data_buffer_dma, len); 831 832 if (dr == DMA_TO_DEVICE && buf != this->data_buffer_dma) 833 memcpy(this->data_buffer_dma, buf, len); 834 835 dma_map_sg(this->dev, sgl, 1, dr); 836 837 return false; 838 } 839 840 /* add our owner bbt descriptor */ 841 static uint8_t scan_ff_pattern[] = { 0xff }; 842 static struct nand_bbt_descr gpmi_bbt_descr = { 843 .options = 0, 844 .offs = 0, 845 .len = 1, 846 .pattern = scan_ff_pattern 847 }; 848 849 /* 850 * We may change the layout if we can get the ECC info from the datasheet, 851 * else we will use all the (page + OOB). 852 */ 853 static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section, 854 struct mtd_oob_region *oobregion) 855 { 856 struct nand_chip *chip = mtd_to_nand(mtd); 857 struct gpmi_nand_data *this = nand_get_controller_data(chip); 858 struct bch_geometry *geo = &this->bch_geometry; 859 860 if (section) 861 return -ERANGE; 862 863 oobregion->offset = 0; 864 oobregion->length = geo->page_size - mtd->writesize; 865 866 return 0; 867 } 868 869 static int gpmi_ooblayout_free(struct mtd_info *mtd, int section, 870 struct mtd_oob_region *oobregion) 871 { 872 struct nand_chip *chip = mtd_to_nand(mtd); 873 struct gpmi_nand_data *this = nand_get_controller_data(chip); 874 struct bch_geometry *geo = &this->bch_geometry; 875 876 if (section) 877 return -ERANGE; 878 879 /* The available oob size we have. */ 880 if (geo->page_size < mtd->writesize + mtd->oobsize) { 881 oobregion->offset = geo->page_size - mtd->writesize; 882 oobregion->length = mtd->oobsize - oobregion->offset; 883 } 884 885 return 0; 886 } 887 888 static const char * const gpmi_clks_for_mx2x[] = { 889 "gpmi_io", 890 }; 891 892 static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = { 893 .ecc = gpmi_ooblayout_ecc, 894 .free = gpmi_ooblayout_free, 895 }; 896 897 static const struct gpmi_devdata gpmi_devdata_imx23 = { 898 .type = IS_MX23, 899 .bch_max_ecc_strength = 20, 900 .max_chain_delay = 16000, 901 .clks = gpmi_clks_for_mx2x, 902 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x), 903 }; 904 905 static const struct gpmi_devdata gpmi_devdata_imx28 = { 906 .type = IS_MX28, 907 .bch_max_ecc_strength = 20, 908 .max_chain_delay = 16000, 909 .clks = gpmi_clks_for_mx2x, 910 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x), 911 }; 912 913 static const char * const gpmi_clks_for_mx6[] = { 914 "gpmi_io", "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch", 915 }; 916 917 static const struct gpmi_devdata gpmi_devdata_imx6q = { 918 .type = IS_MX6Q, 919 .bch_max_ecc_strength = 40, 920 .max_chain_delay = 12000, 921 .clks = gpmi_clks_for_mx6, 922 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6), 923 }; 924 925 static const struct gpmi_devdata gpmi_devdata_imx6sx = { 926 .type = IS_MX6SX, 927 .bch_max_ecc_strength = 62, 928 .max_chain_delay = 12000, 929 .clks = gpmi_clks_for_mx6, 930 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6), 931 }; 932 933 static const char * const gpmi_clks_for_mx7d[] = { 934 "gpmi_io", "gpmi_bch_apb", 935 }; 936 937 static const struct gpmi_devdata gpmi_devdata_imx7d = { 938 .type = IS_MX7D, 939 .bch_max_ecc_strength = 62, 940 .max_chain_delay = 12000, 941 .clks = gpmi_clks_for_mx7d, 942 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx7d), 943 }; 944 945 static int acquire_register_block(struct gpmi_nand_data *this, 946 const char *res_name) 947 { 948 struct platform_device *pdev = this->pdev; 949 struct resources *res = &this->resources; 950 struct resource *r; 951 void __iomem *p; 952 953 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name); 954 p = devm_ioremap_resource(&pdev->dev, r); 955 if (IS_ERR(p)) 956 return PTR_ERR(p); 957 958 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME)) 959 res->gpmi_regs = p; 960 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME)) 961 res->bch_regs = p; 962 else 963 dev_err(this->dev, "unknown resource name : %s\n", res_name); 964 965 return 0; 966 } 967 968 static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h) 969 { 970 struct platform_device *pdev = this->pdev; 971 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME; 972 struct resource *r; 973 int err; 974 975 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name); 976 if (!r) { 977 dev_err(this->dev, "Can't get resource for %s\n", res_name); 978 return -ENODEV; 979 } 980 981 err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this); 982 if (err) 983 dev_err(this->dev, "error requesting BCH IRQ\n"); 984 985 return err; 986 } 987 988 static void release_dma_channels(struct gpmi_nand_data *this) 989 { 990 unsigned int i; 991 for (i = 0; i < DMA_CHANS; i++) 992 if (this->dma_chans[i]) { 993 dma_release_channel(this->dma_chans[i]); 994 this->dma_chans[i] = NULL; 995 } 996 } 997 998 static int acquire_dma_channels(struct gpmi_nand_data *this) 999 { 1000 struct platform_device *pdev = this->pdev; 1001 struct dma_chan *dma_chan; 1002 int ret = 0; 1003 1004 /* request dma channel */ 1005 dma_chan = dma_request_chan(&pdev->dev, "rx-tx"); 1006 if (IS_ERR(dma_chan)) { 1007 ret = dev_err_probe(this->dev, PTR_ERR(dma_chan), 1008 "DMA channel request failed\n"); 1009 release_dma_channels(this); 1010 } else { 1011 this->dma_chans[0] = dma_chan; 1012 } 1013 1014 return ret; 1015 } 1016 1017 static int gpmi_get_clks(struct gpmi_nand_data *this) 1018 { 1019 struct resources *r = &this->resources; 1020 struct clk *clk; 1021 int err, i; 1022 1023 for (i = 0; i < this->devdata->clks_count; i++) { 1024 clk = devm_clk_get(this->dev, this->devdata->clks[i]); 1025 if (IS_ERR(clk)) { 1026 err = PTR_ERR(clk); 1027 goto err_clock; 1028 } 1029 1030 r->clock[i] = clk; 1031 } 1032 1033 if (GPMI_IS_MX6(this)) 1034 /* 1035 * Set the default value for the gpmi clock. 1036 * 1037 * If you want to use the ONFI nand which is in the 1038 * Synchronous Mode, you should change the clock as you need. 1039 */ 1040 clk_set_rate(r->clock[0], 22000000); 1041 1042 return 0; 1043 1044 err_clock: 1045 dev_dbg(this->dev, "failed in finding the clocks.\n"); 1046 return err; 1047 } 1048 1049 static int acquire_resources(struct gpmi_nand_data *this) 1050 { 1051 int ret; 1052 1053 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME); 1054 if (ret) 1055 goto exit_regs; 1056 1057 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME); 1058 if (ret) 1059 goto exit_regs; 1060 1061 ret = acquire_bch_irq(this, bch_irq); 1062 if (ret) 1063 goto exit_regs; 1064 1065 ret = acquire_dma_channels(this); 1066 if (ret) 1067 goto exit_regs; 1068 1069 ret = gpmi_get_clks(this); 1070 if (ret) 1071 goto exit_clock; 1072 return 0; 1073 1074 exit_clock: 1075 release_dma_channels(this); 1076 exit_regs: 1077 return ret; 1078 } 1079 1080 static void release_resources(struct gpmi_nand_data *this) 1081 { 1082 release_dma_channels(this); 1083 } 1084 1085 static void gpmi_free_dma_buffer(struct gpmi_nand_data *this) 1086 { 1087 struct device *dev = this->dev; 1088 struct bch_geometry *geo = &this->bch_geometry; 1089 1090 if (this->auxiliary_virt && virt_addr_valid(this->auxiliary_virt)) 1091 dma_free_coherent(dev, geo->auxiliary_size, 1092 this->auxiliary_virt, 1093 this->auxiliary_phys); 1094 kfree(this->data_buffer_dma); 1095 kfree(this->raw_buffer); 1096 1097 this->data_buffer_dma = NULL; 1098 this->raw_buffer = NULL; 1099 } 1100 1101 /* Allocate the DMA buffers */ 1102 static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this) 1103 { 1104 struct bch_geometry *geo = &this->bch_geometry; 1105 struct device *dev = this->dev; 1106 struct mtd_info *mtd = nand_to_mtd(&this->nand); 1107 1108 /* 1109 * [2] Allocate a read/write data buffer. 1110 * The gpmi_alloc_dma_buffer can be called twice. 1111 * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer 1112 * is called before the NAND identification; and we allocate a 1113 * buffer of the real NAND page size when the gpmi_alloc_dma_buffer 1114 * is called after. 1115 */ 1116 this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE, 1117 GFP_DMA | GFP_KERNEL); 1118 if (this->data_buffer_dma == NULL) 1119 goto error_alloc; 1120 1121 this->auxiliary_virt = dma_alloc_coherent(dev, geo->auxiliary_size, 1122 &this->auxiliary_phys, GFP_DMA); 1123 if (!this->auxiliary_virt) 1124 goto error_alloc; 1125 1126 this->raw_buffer = kzalloc((mtd->writesize ?: PAGE_SIZE) + mtd->oobsize, GFP_KERNEL); 1127 if (!this->raw_buffer) 1128 goto error_alloc; 1129 1130 return 0; 1131 1132 error_alloc: 1133 gpmi_free_dma_buffer(this); 1134 return -ENOMEM; 1135 } 1136 1137 /* 1138 * Handles block mark swapping. 1139 * It can be called in swapping the block mark, or swapping it back, 1140 * because the the operations are the same. 1141 */ 1142 static void block_mark_swapping(struct gpmi_nand_data *this, 1143 void *payload, void *auxiliary) 1144 { 1145 struct bch_geometry *nfc_geo = &this->bch_geometry; 1146 unsigned char *p; 1147 unsigned char *a; 1148 unsigned int bit; 1149 unsigned char mask; 1150 unsigned char from_data; 1151 unsigned char from_oob; 1152 1153 if (!this->swap_block_mark) 1154 return; 1155 1156 /* 1157 * If control arrives here, we're swapping. Make some convenience 1158 * variables. 1159 */ 1160 bit = nfc_geo->block_mark_bit_offset; 1161 p = payload + nfc_geo->block_mark_byte_offset; 1162 a = auxiliary; 1163 1164 /* 1165 * Get the byte from the data area that overlays the block mark. Since 1166 * the ECC engine applies its own view to the bits in the page, the 1167 * physical block mark won't (in general) appear on a byte boundary in 1168 * the data. 1169 */ 1170 from_data = (p[0] >> bit) | (p[1] << (8 - bit)); 1171 1172 /* Get the byte from the OOB. */ 1173 from_oob = a[0]; 1174 1175 /* Swap them. */ 1176 a[0] = from_data; 1177 1178 mask = (0x1 << bit) - 1; 1179 p[0] = (p[0] & mask) | (from_oob << bit); 1180 1181 mask = ~0 << bit; 1182 p[1] = (p[1] & mask) | (from_oob >> (8 - bit)); 1183 } 1184 1185 static int gpmi_count_bitflips(struct nand_chip *chip, void *buf, int first, 1186 int last, int meta) 1187 { 1188 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1189 struct bch_geometry *nfc_geo = &this->bch_geometry; 1190 struct mtd_info *mtd = nand_to_mtd(chip); 1191 int i; 1192 unsigned char *status; 1193 unsigned int max_bitflips = 0; 1194 1195 /* Loop over status bytes, accumulating ECC status. */ 1196 status = this->auxiliary_virt + ALIGN(meta, 4); 1197 1198 for (i = first; i < last; i++, status++) { 1199 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED)) 1200 continue; 1201 1202 if (*status == STATUS_UNCORRECTABLE) { 1203 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len; 1204 u8 *eccbuf = this->raw_buffer; 1205 int offset, bitoffset; 1206 int eccbytes; 1207 int flips; 1208 1209 /* Read ECC bytes into our internal raw_buffer */ 1210 offset = nfc_geo->metadata_size * 8; 1211 offset += ((8 * nfc_geo->ecc_chunk_size) + eccbits) * (i + 1); 1212 offset -= eccbits; 1213 bitoffset = offset % 8; 1214 eccbytes = DIV_ROUND_UP(offset + eccbits, 8); 1215 offset /= 8; 1216 eccbytes -= offset; 1217 nand_change_read_column_op(chip, offset, eccbuf, 1218 eccbytes, false); 1219 1220 /* 1221 * ECC data are not byte aligned and we may have 1222 * in-band data in the first and last byte of 1223 * eccbuf. Set non-eccbits to one so that 1224 * nand_check_erased_ecc_chunk() does not count them 1225 * as bitflips. 1226 */ 1227 if (bitoffset) 1228 eccbuf[0] |= GENMASK(bitoffset - 1, 0); 1229 1230 bitoffset = (bitoffset + eccbits) % 8; 1231 if (bitoffset) 1232 eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset); 1233 1234 /* 1235 * The ECC hardware has an uncorrectable ECC status 1236 * code in case we have bitflips in an erased page. As 1237 * nothing was written into this subpage the ECC is 1238 * obviously wrong and we can not trust it. We assume 1239 * at this point that we are reading an erased page and 1240 * try to correct the bitflips in buffer up to 1241 * ecc_strength bitflips. If this is a page with random 1242 * data, we exceed this number of bitflips and have a 1243 * ECC failure. Otherwise we use the corrected buffer. 1244 */ 1245 if (i == 0) { 1246 /* The first block includes metadata */ 1247 flips = nand_check_erased_ecc_chunk( 1248 buf + i * nfc_geo->ecc_chunk_size, 1249 nfc_geo->ecc_chunk_size, 1250 eccbuf, eccbytes, 1251 this->auxiliary_virt, 1252 nfc_geo->metadata_size, 1253 nfc_geo->ecc_strength); 1254 } else { 1255 flips = nand_check_erased_ecc_chunk( 1256 buf + i * nfc_geo->ecc_chunk_size, 1257 nfc_geo->ecc_chunk_size, 1258 eccbuf, eccbytes, 1259 NULL, 0, 1260 nfc_geo->ecc_strength); 1261 } 1262 1263 if (flips > 0) { 1264 max_bitflips = max_t(unsigned int, max_bitflips, 1265 flips); 1266 mtd->ecc_stats.corrected += flips; 1267 continue; 1268 } 1269 1270 mtd->ecc_stats.failed++; 1271 continue; 1272 } 1273 1274 mtd->ecc_stats.corrected += *status; 1275 max_bitflips = max_t(unsigned int, max_bitflips, *status); 1276 } 1277 1278 return max_bitflips; 1279 } 1280 1281 static void gpmi_bch_layout_std(struct gpmi_nand_data *this) 1282 { 1283 struct bch_geometry *geo = &this->bch_geometry; 1284 unsigned int ecc_strength = geo->ecc_strength >> 1; 1285 unsigned int gf_len = geo->gf_len; 1286 unsigned int block_size = geo->ecc_chunk_size; 1287 1288 this->bch_flashlayout0 = 1289 BF_BCH_FLASH0LAYOUT0_NBLOCKS(geo->ecc_chunk_count - 1) | 1290 BF_BCH_FLASH0LAYOUT0_META_SIZE(geo->metadata_size) | 1291 BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) | 1292 BF_BCH_FLASH0LAYOUT0_GF(gf_len, this) | 1293 BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this); 1294 1295 this->bch_flashlayout1 = 1296 BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(geo->page_size) | 1297 BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) | 1298 BF_BCH_FLASH0LAYOUT1_GF(gf_len, this) | 1299 BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this); 1300 } 1301 1302 static int gpmi_ecc_read_page(struct nand_chip *chip, uint8_t *buf, 1303 int oob_required, int page) 1304 { 1305 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1306 struct mtd_info *mtd = nand_to_mtd(chip); 1307 struct bch_geometry *geo = &this->bch_geometry; 1308 unsigned int max_bitflips; 1309 int ret; 1310 1311 gpmi_bch_layout_std(this); 1312 this->bch = true; 1313 1314 ret = nand_read_page_op(chip, page, 0, buf, geo->page_size); 1315 if (ret) 1316 return ret; 1317 1318 max_bitflips = gpmi_count_bitflips(chip, buf, 0, 1319 geo->ecc_chunk_count, 1320 geo->auxiliary_status_offset); 1321 1322 /* handle the block mark swapping */ 1323 block_mark_swapping(this, buf, this->auxiliary_virt); 1324 1325 if (oob_required) { 1326 /* 1327 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob() 1328 * for details about our policy for delivering the OOB. 1329 * 1330 * We fill the caller's buffer with set bits, and then copy the 1331 * block mark to th caller's buffer. Note that, if block mark 1332 * swapping was necessary, it has already been done, so we can 1333 * rely on the first byte of the auxiliary buffer to contain 1334 * the block mark. 1335 */ 1336 memset(chip->oob_poi, ~0, mtd->oobsize); 1337 chip->oob_poi[0] = ((uint8_t *)this->auxiliary_virt)[0]; 1338 } 1339 1340 return max_bitflips; 1341 } 1342 1343 /* Fake a virtual small page for the subpage read */ 1344 static int gpmi_ecc_read_subpage(struct nand_chip *chip, uint32_t offs, 1345 uint32_t len, uint8_t *buf, int page) 1346 { 1347 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1348 struct bch_geometry *geo = &this->bch_geometry; 1349 int size = chip->ecc.size; /* ECC chunk size */ 1350 int meta, n, page_size; 1351 unsigned int max_bitflips; 1352 unsigned int ecc_strength; 1353 int first, last, marker_pos; 1354 int ecc_parity_size; 1355 int col = 0; 1356 int ret; 1357 1358 /* The size of ECC parity */ 1359 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8; 1360 1361 /* Align it with the chunk size */ 1362 first = offs / size; 1363 last = (offs + len - 1) / size; 1364 1365 if (this->swap_block_mark) { 1366 /* 1367 * Find the chunk which contains the Block Marker. 1368 * If this chunk is in the range of [first, last], 1369 * we have to read out the whole page. 1370 * Why? since we had swapped the data at the position of Block 1371 * Marker to the metadata which is bound with the chunk 0. 1372 */ 1373 marker_pos = geo->block_mark_byte_offset / size; 1374 if (last >= marker_pos && first <= marker_pos) { 1375 dev_dbg(this->dev, 1376 "page:%d, first:%d, last:%d, marker at:%d\n", 1377 page, first, last, marker_pos); 1378 return gpmi_ecc_read_page(chip, buf, 0, page); 1379 } 1380 } 1381 1382 meta = geo->metadata_size; 1383 if (first) { 1384 col = meta + (size + ecc_parity_size) * first; 1385 meta = 0; 1386 buf = buf + first * size; 1387 } 1388 1389 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8; 1390 1391 n = last - first + 1; 1392 page_size = meta + (size + ecc_parity_size) * n; 1393 ecc_strength = geo->ecc_strength >> 1; 1394 1395 this->bch_flashlayout0 = BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1) | 1396 BF_BCH_FLASH0LAYOUT0_META_SIZE(meta) | 1397 BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) | 1398 BF_BCH_FLASH0LAYOUT0_GF(geo->gf_len, this) | 1399 BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(geo->ecc_chunk_size, this); 1400 1401 this->bch_flashlayout1 = BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size) | 1402 BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) | 1403 BF_BCH_FLASH0LAYOUT1_GF(geo->gf_len, this) | 1404 BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(geo->ecc_chunk_size, this); 1405 1406 this->bch = true; 1407 1408 ret = nand_read_page_op(chip, page, col, buf, page_size); 1409 if (ret) 1410 return ret; 1411 1412 dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n", 1413 page, offs, len, col, first, n, page_size); 1414 1415 max_bitflips = gpmi_count_bitflips(chip, buf, first, last, meta); 1416 1417 return max_bitflips; 1418 } 1419 1420 static int gpmi_ecc_write_page(struct nand_chip *chip, const uint8_t *buf, 1421 int oob_required, int page) 1422 { 1423 struct mtd_info *mtd = nand_to_mtd(chip); 1424 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1425 struct bch_geometry *nfc_geo = &this->bch_geometry; 1426 int ret; 1427 1428 dev_dbg(this->dev, "ecc write page.\n"); 1429 1430 gpmi_bch_layout_std(this); 1431 this->bch = true; 1432 1433 memcpy(this->auxiliary_virt, chip->oob_poi, nfc_geo->auxiliary_size); 1434 1435 if (this->swap_block_mark) { 1436 /* 1437 * When doing bad block marker swapping we must always copy the 1438 * input buffer as we can't modify the const buffer. 1439 */ 1440 memcpy(this->data_buffer_dma, buf, mtd->writesize); 1441 buf = this->data_buffer_dma; 1442 block_mark_swapping(this, this->data_buffer_dma, 1443 this->auxiliary_virt); 1444 } 1445 1446 ret = nand_prog_page_op(chip, page, 0, buf, nfc_geo->page_size); 1447 1448 return ret; 1449 } 1450 1451 /* 1452 * There are several places in this driver where we have to handle the OOB and 1453 * block marks. This is the function where things are the most complicated, so 1454 * this is where we try to explain it all. All the other places refer back to 1455 * here. 1456 * 1457 * These are the rules, in order of decreasing importance: 1458 * 1459 * 1) Nothing the caller does can be allowed to imperil the block mark. 1460 * 1461 * 2) In read operations, the first byte of the OOB we return must reflect the 1462 * true state of the block mark, no matter where that block mark appears in 1463 * the physical page. 1464 * 1465 * 3) ECC-based read operations return an OOB full of set bits (since we never 1466 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads 1467 * return). 1468 * 1469 * 4) "Raw" read operations return a direct view of the physical bytes in the 1470 * page, using the conventional definition of which bytes are data and which 1471 * are OOB. This gives the caller a way to see the actual, physical bytes 1472 * in the page, without the distortions applied by our ECC engine. 1473 * 1474 * 1475 * What we do for this specific read operation depends on two questions: 1476 * 1477 * 1) Are we doing a "raw" read, or an ECC-based read? 1478 * 1479 * 2) Are we using block mark swapping or transcription? 1480 * 1481 * There are four cases, illustrated by the following Karnaugh map: 1482 * 1483 * | Raw | ECC-based | 1484 * -------------+-------------------------+-------------------------+ 1485 * | Read the conventional | | 1486 * | OOB at the end of the | | 1487 * Swapping | page and return it. It | | 1488 * | contains exactly what | | 1489 * | we want. | Read the block mark and | 1490 * -------------+-------------------------+ return it in a buffer | 1491 * | Read the conventional | full of set bits. | 1492 * | OOB at the end of the | | 1493 * | page and also the block | | 1494 * Transcribing | mark in the metadata. | | 1495 * | Copy the block mark | | 1496 * | into the first byte of | | 1497 * | the OOB. | | 1498 * -------------+-------------------------+-------------------------+ 1499 * 1500 * Note that we break rule #4 in the Transcribing/Raw case because we're not 1501 * giving an accurate view of the actual, physical bytes in the page (we're 1502 * overwriting the block mark). That's OK because it's more important to follow 1503 * rule #2. 1504 * 1505 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not 1506 * easy. When reading a page, for example, the NAND Flash MTD code calls our 1507 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an 1508 * ECC-based or raw view of the page is implicit in which function it calls 1509 * (there is a similar pair of ECC-based/raw functions for writing). 1510 */ 1511 static int gpmi_ecc_read_oob(struct nand_chip *chip, int page) 1512 { 1513 struct mtd_info *mtd = nand_to_mtd(chip); 1514 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1515 int ret; 1516 1517 /* clear the OOB buffer */ 1518 memset(chip->oob_poi, ~0, mtd->oobsize); 1519 1520 /* Read out the conventional OOB. */ 1521 ret = nand_read_page_op(chip, page, mtd->writesize, chip->oob_poi, 1522 mtd->oobsize); 1523 if (ret) 1524 return ret; 1525 1526 /* 1527 * Now, we want to make sure the block mark is correct. In the 1528 * non-transcribing case (!GPMI_IS_MX23()), we already have it. 1529 * Otherwise, we need to explicitly read it. 1530 */ 1531 if (GPMI_IS_MX23(this)) { 1532 /* Read the block mark into the first byte of the OOB buffer. */ 1533 ret = nand_read_page_op(chip, page, 0, chip->oob_poi, 1); 1534 if (ret) 1535 return ret; 1536 } 1537 1538 return 0; 1539 } 1540 1541 static int gpmi_ecc_write_oob(struct nand_chip *chip, int page) 1542 { 1543 struct mtd_info *mtd = nand_to_mtd(chip); 1544 struct mtd_oob_region of = { }; 1545 1546 /* Do we have available oob area? */ 1547 mtd_ooblayout_free(mtd, 0, &of); 1548 if (!of.length) 1549 return -EPERM; 1550 1551 if (!nand_is_slc(chip)) 1552 return -EPERM; 1553 1554 return nand_prog_page_op(chip, page, mtd->writesize + of.offset, 1555 chip->oob_poi + of.offset, of.length); 1556 } 1557 1558 /* 1559 * This function reads a NAND page without involving the ECC engine (no HW 1560 * ECC correction). 1561 * The tricky part in the GPMI/BCH controller is that it stores ECC bits 1562 * inline (interleaved with payload DATA), and do not align data chunk on 1563 * byte boundaries. 1564 * We thus need to take care moving the payload data and ECC bits stored in the 1565 * page into the provided buffers, which is why we're using nand_extract_bits(). 1566 * 1567 * See set_geometry_by_ecc_info inline comments to have a full description 1568 * of the layout used by the GPMI controller. 1569 */ 1570 static int gpmi_ecc_read_page_raw(struct nand_chip *chip, uint8_t *buf, 1571 int oob_required, int page) 1572 { 1573 struct mtd_info *mtd = nand_to_mtd(chip); 1574 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1575 struct bch_geometry *nfc_geo = &this->bch_geometry; 1576 int eccsize = nfc_geo->ecc_chunk_size; 1577 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len; 1578 u8 *tmp_buf = this->raw_buffer; 1579 size_t src_bit_off; 1580 size_t oob_bit_off; 1581 size_t oob_byte_off; 1582 uint8_t *oob = chip->oob_poi; 1583 int step; 1584 int ret; 1585 1586 ret = nand_read_page_op(chip, page, 0, tmp_buf, 1587 mtd->writesize + mtd->oobsize); 1588 if (ret) 1589 return ret; 1590 1591 /* 1592 * If required, swap the bad block marker and the data stored in the 1593 * metadata section, so that we don't wrongly consider a block as bad. 1594 * 1595 * See the layout description for a detailed explanation on why this 1596 * is needed. 1597 */ 1598 if (this->swap_block_mark) 1599 swap(tmp_buf[0], tmp_buf[mtd->writesize]); 1600 1601 /* 1602 * Copy the metadata section into the oob buffer (this section is 1603 * guaranteed to be aligned on a byte boundary). 1604 */ 1605 if (oob_required) 1606 memcpy(oob, tmp_buf, nfc_geo->metadata_size); 1607 1608 oob_bit_off = nfc_geo->metadata_size * 8; 1609 src_bit_off = oob_bit_off; 1610 1611 /* Extract interleaved payload data and ECC bits */ 1612 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) { 1613 if (buf) 1614 nand_extract_bits(buf, step * eccsize, tmp_buf, 1615 src_bit_off, eccsize * 8); 1616 src_bit_off += eccsize * 8; 1617 1618 /* Align last ECC block to align a byte boundary */ 1619 if (step == nfc_geo->ecc_chunk_count - 1 && 1620 (oob_bit_off + eccbits) % 8) 1621 eccbits += 8 - ((oob_bit_off + eccbits) % 8); 1622 1623 if (oob_required) 1624 nand_extract_bits(oob, oob_bit_off, tmp_buf, 1625 src_bit_off, eccbits); 1626 1627 src_bit_off += eccbits; 1628 oob_bit_off += eccbits; 1629 } 1630 1631 if (oob_required) { 1632 oob_byte_off = oob_bit_off / 8; 1633 1634 if (oob_byte_off < mtd->oobsize) 1635 memcpy(oob + oob_byte_off, 1636 tmp_buf + mtd->writesize + oob_byte_off, 1637 mtd->oobsize - oob_byte_off); 1638 } 1639 1640 return 0; 1641 } 1642 1643 /* 1644 * This function writes a NAND page without involving the ECC engine (no HW 1645 * ECC generation). 1646 * The tricky part in the GPMI/BCH controller is that it stores ECC bits 1647 * inline (interleaved with payload DATA), and do not align data chunk on 1648 * byte boundaries. 1649 * We thus need to take care moving the OOB area at the right place in the 1650 * final page, which is why we're using nand_extract_bits(). 1651 * 1652 * See set_geometry_by_ecc_info inline comments to have a full description 1653 * of the layout used by the GPMI controller. 1654 */ 1655 static int gpmi_ecc_write_page_raw(struct nand_chip *chip, const uint8_t *buf, 1656 int oob_required, int page) 1657 { 1658 struct mtd_info *mtd = nand_to_mtd(chip); 1659 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1660 struct bch_geometry *nfc_geo = &this->bch_geometry; 1661 int eccsize = nfc_geo->ecc_chunk_size; 1662 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len; 1663 u8 *tmp_buf = this->raw_buffer; 1664 uint8_t *oob = chip->oob_poi; 1665 size_t dst_bit_off; 1666 size_t oob_bit_off; 1667 size_t oob_byte_off; 1668 int step; 1669 1670 /* 1671 * Initialize all bits to 1 in case we don't have a buffer for the 1672 * payload or oob data in order to leave unspecified bits of data 1673 * to their initial state. 1674 */ 1675 if (!buf || !oob_required) 1676 memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize); 1677 1678 /* 1679 * First copy the metadata section (stored in oob buffer) at the 1680 * beginning of the page, as imposed by the GPMI layout. 1681 */ 1682 memcpy(tmp_buf, oob, nfc_geo->metadata_size); 1683 oob_bit_off = nfc_geo->metadata_size * 8; 1684 dst_bit_off = oob_bit_off; 1685 1686 /* Interleave payload data and ECC bits */ 1687 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) { 1688 if (buf) 1689 nand_extract_bits(tmp_buf, dst_bit_off, buf, 1690 step * eccsize * 8, eccsize * 8); 1691 dst_bit_off += eccsize * 8; 1692 1693 /* Align last ECC block to align a byte boundary */ 1694 if (step == nfc_geo->ecc_chunk_count - 1 && 1695 (oob_bit_off + eccbits) % 8) 1696 eccbits += 8 - ((oob_bit_off + eccbits) % 8); 1697 1698 if (oob_required) 1699 nand_extract_bits(tmp_buf, dst_bit_off, oob, 1700 oob_bit_off, eccbits); 1701 1702 dst_bit_off += eccbits; 1703 oob_bit_off += eccbits; 1704 } 1705 1706 oob_byte_off = oob_bit_off / 8; 1707 1708 if (oob_required && oob_byte_off < mtd->oobsize) 1709 memcpy(tmp_buf + mtd->writesize + oob_byte_off, 1710 oob + oob_byte_off, mtd->oobsize - oob_byte_off); 1711 1712 /* 1713 * If required, swap the bad block marker and the first byte of the 1714 * metadata section, so that we don't modify the bad block marker. 1715 * 1716 * See the layout description for a detailed explanation on why this 1717 * is needed. 1718 */ 1719 if (this->swap_block_mark) 1720 swap(tmp_buf[0], tmp_buf[mtd->writesize]); 1721 1722 return nand_prog_page_op(chip, page, 0, tmp_buf, 1723 mtd->writesize + mtd->oobsize); 1724 } 1725 1726 static int gpmi_ecc_read_oob_raw(struct nand_chip *chip, int page) 1727 { 1728 return gpmi_ecc_read_page_raw(chip, NULL, 1, page); 1729 } 1730 1731 static int gpmi_ecc_write_oob_raw(struct nand_chip *chip, int page) 1732 { 1733 return gpmi_ecc_write_page_raw(chip, NULL, 1, page); 1734 } 1735 1736 static int gpmi_block_markbad(struct nand_chip *chip, loff_t ofs) 1737 { 1738 struct mtd_info *mtd = nand_to_mtd(chip); 1739 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1740 int ret = 0; 1741 uint8_t *block_mark; 1742 int column, page, chipnr; 1743 1744 chipnr = (int)(ofs >> chip->chip_shift); 1745 nand_select_target(chip, chipnr); 1746 1747 column = !GPMI_IS_MX23(this) ? mtd->writesize : 0; 1748 1749 /* Write the block mark. */ 1750 block_mark = this->data_buffer_dma; 1751 block_mark[0] = 0; /* bad block marker */ 1752 1753 /* Shift to get page */ 1754 page = (int)(ofs >> chip->page_shift); 1755 1756 ret = nand_prog_page_op(chip, page, column, block_mark, 1); 1757 1758 nand_deselect_target(chip); 1759 1760 return ret; 1761 } 1762 1763 static int nand_boot_set_geometry(struct gpmi_nand_data *this) 1764 { 1765 struct boot_rom_geometry *geometry = &this->rom_geometry; 1766 1767 /* 1768 * Set the boot block stride size. 1769 * 1770 * In principle, we should be reading this from the OTP bits, since 1771 * that's where the ROM is going to get it. In fact, we don't have any 1772 * way to read the OTP bits, so we go with the default and hope for the 1773 * best. 1774 */ 1775 geometry->stride_size_in_pages = 64; 1776 1777 /* 1778 * Set the search area stride exponent. 1779 * 1780 * In principle, we should be reading this from the OTP bits, since 1781 * that's where the ROM is going to get it. In fact, we don't have any 1782 * way to read the OTP bits, so we go with the default and hope for the 1783 * best. 1784 */ 1785 geometry->search_area_stride_exponent = 2; 1786 return 0; 1787 } 1788 1789 static const char *fingerprint = "STMP"; 1790 static int mx23_check_transcription_stamp(struct gpmi_nand_data *this) 1791 { 1792 struct boot_rom_geometry *rom_geo = &this->rom_geometry; 1793 struct device *dev = this->dev; 1794 struct nand_chip *chip = &this->nand; 1795 unsigned int search_area_size_in_strides; 1796 unsigned int stride; 1797 unsigned int page; 1798 u8 *buffer = nand_get_data_buf(chip); 1799 int found_an_ncb_fingerprint = false; 1800 int ret; 1801 1802 /* Compute the number of strides in a search area. */ 1803 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent; 1804 1805 nand_select_target(chip, 0); 1806 1807 /* 1808 * Loop through the first search area, looking for the NCB fingerprint. 1809 */ 1810 dev_dbg(dev, "Scanning for an NCB fingerprint...\n"); 1811 1812 for (stride = 0; stride < search_area_size_in_strides; stride++) { 1813 /* Compute the page addresses. */ 1814 page = stride * rom_geo->stride_size_in_pages; 1815 1816 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page); 1817 1818 /* 1819 * Read the NCB fingerprint. The fingerprint is four bytes long 1820 * and starts in the 12th byte of the page. 1821 */ 1822 ret = nand_read_page_op(chip, page, 12, buffer, 1823 strlen(fingerprint)); 1824 if (ret) 1825 continue; 1826 1827 /* Look for the fingerprint. */ 1828 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) { 1829 found_an_ncb_fingerprint = true; 1830 break; 1831 } 1832 1833 } 1834 1835 nand_deselect_target(chip); 1836 1837 if (found_an_ncb_fingerprint) 1838 dev_dbg(dev, "\tFound a fingerprint\n"); 1839 else 1840 dev_dbg(dev, "\tNo fingerprint found\n"); 1841 return found_an_ncb_fingerprint; 1842 } 1843 1844 /* Writes a transcription stamp. */ 1845 static int mx23_write_transcription_stamp(struct gpmi_nand_data *this) 1846 { 1847 struct device *dev = this->dev; 1848 struct boot_rom_geometry *rom_geo = &this->rom_geometry; 1849 struct nand_chip *chip = &this->nand; 1850 struct mtd_info *mtd = nand_to_mtd(chip); 1851 unsigned int block_size_in_pages; 1852 unsigned int search_area_size_in_strides; 1853 unsigned int search_area_size_in_pages; 1854 unsigned int search_area_size_in_blocks; 1855 unsigned int block; 1856 unsigned int stride; 1857 unsigned int page; 1858 u8 *buffer = nand_get_data_buf(chip); 1859 int status; 1860 1861 /* Compute the search area geometry. */ 1862 block_size_in_pages = mtd->erasesize / mtd->writesize; 1863 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent; 1864 search_area_size_in_pages = search_area_size_in_strides * 1865 rom_geo->stride_size_in_pages; 1866 search_area_size_in_blocks = 1867 (search_area_size_in_pages + (block_size_in_pages - 1)) / 1868 block_size_in_pages; 1869 1870 dev_dbg(dev, "Search Area Geometry :\n"); 1871 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks); 1872 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides); 1873 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages); 1874 1875 nand_select_target(chip, 0); 1876 1877 /* Loop over blocks in the first search area, erasing them. */ 1878 dev_dbg(dev, "Erasing the search area...\n"); 1879 1880 for (block = 0; block < search_area_size_in_blocks; block++) { 1881 /* Erase this block. */ 1882 dev_dbg(dev, "\tErasing block 0x%x\n", block); 1883 status = nand_erase_op(chip, block); 1884 if (status) 1885 dev_err(dev, "[%s] Erase failed.\n", __func__); 1886 } 1887 1888 /* Write the NCB fingerprint into the page buffer. */ 1889 memset(buffer, ~0, mtd->writesize); 1890 memcpy(buffer + 12, fingerprint, strlen(fingerprint)); 1891 1892 /* Loop through the first search area, writing NCB fingerprints. */ 1893 dev_dbg(dev, "Writing NCB fingerprints...\n"); 1894 for (stride = 0; stride < search_area_size_in_strides; stride++) { 1895 /* Compute the page addresses. */ 1896 page = stride * rom_geo->stride_size_in_pages; 1897 1898 /* Write the first page of the current stride. */ 1899 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page); 1900 1901 status = chip->ecc.write_page_raw(chip, buffer, 0, page); 1902 if (status) 1903 dev_err(dev, "[%s] Write failed.\n", __func__); 1904 } 1905 1906 nand_deselect_target(chip); 1907 1908 return 0; 1909 } 1910 1911 static int mx23_boot_init(struct gpmi_nand_data *this) 1912 { 1913 struct device *dev = this->dev; 1914 struct nand_chip *chip = &this->nand; 1915 struct mtd_info *mtd = nand_to_mtd(chip); 1916 unsigned int block_count; 1917 unsigned int block; 1918 int chipnr; 1919 int page; 1920 loff_t byte; 1921 uint8_t block_mark; 1922 int ret = 0; 1923 1924 /* 1925 * If control arrives here, we can't use block mark swapping, which 1926 * means we're forced to use transcription. First, scan for the 1927 * transcription stamp. If we find it, then we don't have to do 1928 * anything -- the block marks are already transcribed. 1929 */ 1930 if (mx23_check_transcription_stamp(this)) 1931 return 0; 1932 1933 /* 1934 * If control arrives here, we couldn't find a transcription stamp, so 1935 * so we presume the block marks are in the conventional location. 1936 */ 1937 dev_dbg(dev, "Transcribing bad block marks...\n"); 1938 1939 /* Compute the number of blocks in the entire medium. */ 1940 block_count = nanddev_eraseblocks_per_target(&chip->base); 1941 1942 /* 1943 * Loop over all the blocks in the medium, transcribing block marks as 1944 * we go. 1945 */ 1946 for (block = 0; block < block_count; block++) { 1947 /* 1948 * Compute the chip, page and byte addresses for this block's 1949 * conventional mark. 1950 */ 1951 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift); 1952 page = block << (chip->phys_erase_shift - chip->page_shift); 1953 byte = block << chip->phys_erase_shift; 1954 1955 /* Send the command to read the conventional block mark. */ 1956 nand_select_target(chip, chipnr); 1957 ret = nand_read_page_op(chip, page, mtd->writesize, &block_mark, 1958 1); 1959 nand_deselect_target(chip); 1960 1961 if (ret) 1962 continue; 1963 1964 /* 1965 * Check if the block is marked bad. If so, we need to mark it 1966 * again, but this time the result will be a mark in the 1967 * location where we transcribe block marks. 1968 */ 1969 if (block_mark != 0xff) { 1970 dev_dbg(dev, "Transcribing mark in block %u\n", block); 1971 ret = chip->legacy.block_markbad(chip, byte); 1972 if (ret) 1973 dev_err(dev, 1974 "Failed to mark block bad with ret %d\n", 1975 ret); 1976 } 1977 } 1978 1979 /* Write the stamp that indicates we've transcribed the block marks. */ 1980 mx23_write_transcription_stamp(this); 1981 return 0; 1982 } 1983 1984 static int nand_boot_init(struct gpmi_nand_data *this) 1985 { 1986 nand_boot_set_geometry(this); 1987 1988 /* This is ROM arch-specific initilization before the BBT scanning. */ 1989 if (GPMI_IS_MX23(this)) 1990 return mx23_boot_init(this); 1991 return 0; 1992 } 1993 1994 static int gpmi_set_geometry(struct gpmi_nand_data *this) 1995 { 1996 int ret; 1997 1998 /* Free the temporary DMA memory for reading ID. */ 1999 gpmi_free_dma_buffer(this); 2000 2001 /* Set up the NFC geometry which is used by BCH. */ 2002 ret = bch_set_geometry(this); 2003 if (ret) { 2004 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret); 2005 return ret; 2006 } 2007 2008 /* Alloc the new DMA buffers according to the pagesize and oobsize */ 2009 return gpmi_alloc_dma_buffer(this); 2010 } 2011 2012 static int gpmi_init_last(struct gpmi_nand_data *this) 2013 { 2014 struct nand_chip *chip = &this->nand; 2015 struct mtd_info *mtd = nand_to_mtd(chip); 2016 struct nand_ecc_ctrl *ecc = &chip->ecc; 2017 struct bch_geometry *bch_geo = &this->bch_geometry; 2018 int ret; 2019 2020 /* Set up the medium geometry */ 2021 ret = gpmi_set_geometry(this); 2022 if (ret) 2023 return ret; 2024 2025 /* Init the nand_ecc_ctrl{} */ 2026 ecc->read_page = gpmi_ecc_read_page; 2027 ecc->write_page = gpmi_ecc_write_page; 2028 ecc->read_oob = gpmi_ecc_read_oob; 2029 ecc->write_oob = gpmi_ecc_write_oob; 2030 ecc->read_page_raw = gpmi_ecc_read_page_raw; 2031 ecc->write_page_raw = gpmi_ecc_write_page_raw; 2032 ecc->read_oob_raw = gpmi_ecc_read_oob_raw; 2033 ecc->write_oob_raw = gpmi_ecc_write_oob_raw; 2034 ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST; 2035 ecc->size = bch_geo->ecc_chunk_size; 2036 ecc->strength = bch_geo->ecc_strength; 2037 mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops); 2038 2039 /* 2040 * We only enable the subpage read when: 2041 * (1) the chip is imx6, and 2042 * (2) the size of the ECC parity is byte aligned. 2043 */ 2044 if (GPMI_IS_MX6(this) && 2045 ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) { 2046 ecc->read_subpage = gpmi_ecc_read_subpage; 2047 chip->options |= NAND_SUBPAGE_READ; 2048 } 2049 2050 return 0; 2051 } 2052 2053 static int gpmi_nand_attach_chip(struct nand_chip *chip) 2054 { 2055 struct gpmi_nand_data *this = nand_get_controller_data(chip); 2056 int ret; 2057 2058 if (chip->bbt_options & NAND_BBT_USE_FLASH) { 2059 chip->bbt_options |= NAND_BBT_NO_OOB; 2060 2061 if (of_property_read_bool(this->dev->of_node, 2062 "fsl,no-blockmark-swap")) 2063 this->swap_block_mark = false; 2064 } 2065 dev_dbg(this->dev, "Blockmark swapping %sabled\n", 2066 this->swap_block_mark ? "en" : "dis"); 2067 2068 ret = gpmi_init_last(this); 2069 if (ret) 2070 return ret; 2071 2072 chip->options |= NAND_SKIP_BBTSCAN; 2073 2074 return 0; 2075 } 2076 2077 static struct gpmi_transfer *get_next_transfer(struct gpmi_nand_data *this) 2078 { 2079 struct gpmi_transfer *transfer = &this->transfers[this->ntransfers]; 2080 2081 this->ntransfers++; 2082 2083 if (this->ntransfers == GPMI_MAX_TRANSFERS) 2084 return NULL; 2085 2086 return transfer; 2087 } 2088 2089 static struct dma_async_tx_descriptor *gpmi_chain_command( 2090 struct gpmi_nand_data *this, u8 cmd, const u8 *addr, int naddr) 2091 { 2092 struct dma_chan *channel = get_dma_chan(this); 2093 struct dma_async_tx_descriptor *desc; 2094 struct gpmi_transfer *transfer; 2095 int chip = this->nand.cur_cs; 2096 u32 pio[3]; 2097 2098 /* [1] send out the PIO words */ 2099 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE) 2100 | BM_GPMI_CTRL0_WORD_LENGTH 2101 | BF_GPMI_CTRL0_CS(chip, this) 2102 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 2103 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE) 2104 | BM_GPMI_CTRL0_ADDRESS_INCREMENT 2105 | BF_GPMI_CTRL0_XFER_COUNT(naddr + 1); 2106 pio[1] = 0; 2107 pio[2] = 0; 2108 desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 2109 DMA_TRANS_NONE, 0); 2110 if (!desc) 2111 return NULL; 2112 2113 transfer = get_next_transfer(this); 2114 if (!transfer) 2115 return NULL; 2116 2117 transfer->cmdbuf[0] = cmd; 2118 if (naddr) 2119 memcpy(&transfer->cmdbuf[1], addr, naddr); 2120 2121 sg_init_one(&transfer->sgl, transfer->cmdbuf, naddr + 1); 2122 dma_map_sg(this->dev, &transfer->sgl, 1, DMA_TO_DEVICE); 2123 2124 transfer->direction = DMA_TO_DEVICE; 2125 2126 desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1, DMA_MEM_TO_DEV, 2127 MXS_DMA_CTRL_WAIT4END); 2128 return desc; 2129 } 2130 2131 static struct dma_async_tx_descriptor *gpmi_chain_wait_ready( 2132 struct gpmi_nand_data *this) 2133 { 2134 struct dma_chan *channel = get_dma_chan(this); 2135 u32 pio[2]; 2136 2137 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY) 2138 | BM_GPMI_CTRL0_WORD_LENGTH 2139 | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this) 2140 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 2141 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA) 2142 | BF_GPMI_CTRL0_XFER_COUNT(0); 2143 pio[1] = 0; 2144 2145 return mxs_dmaengine_prep_pio(channel, pio, 2, DMA_TRANS_NONE, 2146 MXS_DMA_CTRL_WAIT4END | MXS_DMA_CTRL_WAIT4RDY); 2147 } 2148 2149 static struct dma_async_tx_descriptor *gpmi_chain_data_read( 2150 struct gpmi_nand_data *this, void *buf, int raw_len, bool *direct) 2151 { 2152 struct dma_async_tx_descriptor *desc; 2153 struct dma_chan *channel = get_dma_chan(this); 2154 struct gpmi_transfer *transfer; 2155 u32 pio[6] = {}; 2156 2157 transfer = get_next_transfer(this); 2158 if (!transfer) 2159 return NULL; 2160 2161 transfer->direction = DMA_FROM_DEVICE; 2162 2163 *direct = prepare_data_dma(this, buf, raw_len, &transfer->sgl, 2164 DMA_FROM_DEVICE); 2165 2166 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ) 2167 | BM_GPMI_CTRL0_WORD_LENGTH 2168 | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this) 2169 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 2170 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA) 2171 | BF_GPMI_CTRL0_XFER_COUNT(raw_len); 2172 2173 if (this->bch) { 2174 pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC 2175 | BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE) 2176 | BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE 2177 | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY); 2178 pio[3] = raw_len; 2179 pio[4] = transfer->sgl.dma_address; 2180 pio[5] = this->auxiliary_phys; 2181 } 2182 2183 desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 2184 DMA_TRANS_NONE, 0); 2185 if (!desc) 2186 return NULL; 2187 2188 if (!this->bch) 2189 desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1, 2190 DMA_DEV_TO_MEM, 2191 MXS_DMA_CTRL_WAIT4END); 2192 2193 return desc; 2194 } 2195 2196 static struct dma_async_tx_descriptor *gpmi_chain_data_write( 2197 struct gpmi_nand_data *this, const void *buf, int raw_len) 2198 { 2199 struct dma_chan *channel = get_dma_chan(this); 2200 struct dma_async_tx_descriptor *desc; 2201 struct gpmi_transfer *transfer; 2202 u32 pio[6] = {}; 2203 2204 transfer = get_next_transfer(this); 2205 if (!transfer) 2206 return NULL; 2207 2208 transfer->direction = DMA_TO_DEVICE; 2209 2210 prepare_data_dma(this, buf, raw_len, &transfer->sgl, DMA_TO_DEVICE); 2211 2212 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE) 2213 | BM_GPMI_CTRL0_WORD_LENGTH 2214 | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this) 2215 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 2216 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA) 2217 | BF_GPMI_CTRL0_XFER_COUNT(raw_len); 2218 2219 if (this->bch) { 2220 pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC 2221 | BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE) 2222 | BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE | 2223 BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY); 2224 pio[3] = raw_len; 2225 pio[4] = transfer->sgl.dma_address; 2226 pio[5] = this->auxiliary_phys; 2227 } 2228 2229 desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 2230 DMA_TRANS_NONE, 2231 (this->bch ? MXS_DMA_CTRL_WAIT4END : 0)); 2232 if (!desc) 2233 return NULL; 2234 2235 if (!this->bch) 2236 desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1, 2237 DMA_MEM_TO_DEV, 2238 MXS_DMA_CTRL_WAIT4END); 2239 2240 return desc; 2241 } 2242 2243 static int gpmi_nfc_exec_op(struct nand_chip *chip, 2244 const struct nand_operation *op, 2245 bool check_only) 2246 { 2247 const struct nand_op_instr *instr; 2248 struct gpmi_nand_data *this = nand_get_controller_data(chip); 2249 struct dma_async_tx_descriptor *desc = NULL; 2250 int i, ret, buf_len = 0, nbufs = 0; 2251 u8 cmd = 0; 2252 void *buf_read = NULL; 2253 const void *buf_write = NULL; 2254 bool direct = false; 2255 struct completion *completion; 2256 unsigned long to; 2257 2258 if (check_only) 2259 return 0; 2260 2261 this->ntransfers = 0; 2262 for (i = 0; i < GPMI_MAX_TRANSFERS; i++) 2263 this->transfers[i].direction = DMA_NONE; 2264 2265 ret = pm_runtime_get_sync(this->dev); 2266 if (ret < 0) 2267 return ret; 2268 2269 /* 2270 * This driver currently supports only one NAND chip. Plus, dies share 2271 * the same configuration. So once timings have been applied on the 2272 * controller side, they will not change anymore. When the time will 2273 * come, the check on must_apply_timings will have to be dropped. 2274 */ 2275 if (this->hw.must_apply_timings) { 2276 this->hw.must_apply_timings = false; 2277 gpmi_nfc_apply_timings(this); 2278 } 2279 2280 dev_dbg(this->dev, "%s: %d instructions\n", __func__, op->ninstrs); 2281 2282 for (i = 0; i < op->ninstrs; i++) { 2283 instr = &op->instrs[i]; 2284 2285 nand_op_trace(" ", instr); 2286 2287 switch (instr->type) { 2288 case NAND_OP_WAITRDY_INSTR: 2289 desc = gpmi_chain_wait_ready(this); 2290 break; 2291 case NAND_OP_CMD_INSTR: 2292 cmd = instr->ctx.cmd.opcode; 2293 2294 /* 2295 * When this command has an address cycle chain it 2296 * together with the address cycle 2297 */ 2298 if (i + 1 != op->ninstrs && 2299 op->instrs[i + 1].type == NAND_OP_ADDR_INSTR) 2300 continue; 2301 2302 desc = gpmi_chain_command(this, cmd, NULL, 0); 2303 2304 break; 2305 case NAND_OP_ADDR_INSTR: 2306 desc = gpmi_chain_command(this, cmd, instr->ctx.addr.addrs, 2307 instr->ctx.addr.naddrs); 2308 break; 2309 case NAND_OP_DATA_OUT_INSTR: 2310 buf_write = instr->ctx.data.buf.out; 2311 buf_len = instr->ctx.data.len; 2312 nbufs++; 2313 2314 desc = gpmi_chain_data_write(this, buf_write, buf_len); 2315 2316 break; 2317 case NAND_OP_DATA_IN_INSTR: 2318 if (!instr->ctx.data.len) 2319 break; 2320 buf_read = instr->ctx.data.buf.in; 2321 buf_len = instr->ctx.data.len; 2322 nbufs++; 2323 2324 desc = gpmi_chain_data_read(this, buf_read, buf_len, 2325 &direct); 2326 break; 2327 } 2328 2329 if (!desc) { 2330 ret = -ENXIO; 2331 goto unmap; 2332 } 2333 } 2334 2335 dev_dbg(this->dev, "%s setup done\n", __func__); 2336 2337 if (nbufs > 1) { 2338 dev_err(this->dev, "Multiple data instructions not supported\n"); 2339 ret = -EINVAL; 2340 goto unmap; 2341 } 2342 2343 if (this->bch) { 2344 writel(this->bch_flashlayout0, 2345 this->resources.bch_regs + HW_BCH_FLASH0LAYOUT0); 2346 writel(this->bch_flashlayout1, 2347 this->resources.bch_regs + HW_BCH_FLASH0LAYOUT1); 2348 } 2349 2350 if (this->bch && buf_read) { 2351 writel(BM_BCH_CTRL_COMPLETE_IRQ_EN, 2352 this->resources.bch_regs + HW_BCH_CTRL_SET); 2353 completion = &this->bch_done; 2354 } else { 2355 desc->callback = dma_irq_callback; 2356 desc->callback_param = this; 2357 completion = &this->dma_done; 2358 } 2359 2360 init_completion(completion); 2361 2362 dmaengine_submit(desc); 2363 dma_async_issue_pending(get_dma_chan(this)); 2364 2365 to = wait_for_completion_timeout(completion, msecs_to_jiffies(1000)); 2366 if (!to) { 2367 dev_err(this->dev, "DMA timeout, last DMA\n"); 2368 gpmi_dump_info(this); 2369 ret = -ETIMEDOUT; 2370 goto unmap; 2371 } 2372 2373 writel(BM_BCH_CTRL_COMPLETE_IRQ_EN, 2374 this->resources.bch_regs + HW_BCH_CTRL_CLR); 2375 gpmi_clear_bch(this); 2376 2377 ret = 0; 2378 2379 unmap: 2380 for (i = 0; i < this->ntransfers; i++) { 2381 struct gpmi_transfer *transfer = &this->transfers[i]; 2382 2383 if (transfer->direction != DMA_NONE) 2384 dma_unmap_sg(this->dev, &transfer->sgl, 1, 2385 transfer->direction); 2386 } 2387 2388 if (!ret && buf_read && !direct) 2389 memcpy(buf_read, this->data_buffer_dma, 2390 gpmi_raw_len_to_len(this, buf_len)); 2391 2392 this->bch = false; 2393 2394 pm_runtime_mark_last_busy(this->dev); 2395 pm_runtime_put_autosuspend(this->dev); 2396 2397 return ret; 2398 } 2399 2400 static const struct nand_controller_ops gpmi_nand_controller_ops = { 2401 .attach_chip = gpmi_nand_attach_chip, 2402 .setup_interface = gpmi_setup_interface, 2403 .exec_op = gpmi_nfc_exec_op, 2404 }; 2405 2406 static int gpmi_nand_init(struct gpmi_nand_data *this) 2407 { 2408 struct nand_chip *chip = &this->nand; 2409 struct mtd_info *mtd = nand_to_mtd(chip); 2410 int ret; 2411 2412 /* init the MTD data structures */ 2413 mtd->name = "gpmi-nand"; 2414 mtd->dev.parent = this->dev; 2415 2416 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */ 2417 nand_set_controller_data(chip, this); 2418 nand_set_flash_node(chip, this->pdev->dev.of_node); 2419 chip->legacy.block_markbad = gpmi_block_markbad; 2420 chip->badblock_pattern = &gpmi_bbt_descr; 2421 chip->options |= NAND_NO_SUBPAGE_WRITE; 2422 2423 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */ 2424 this->swap_block_mark = !GPMI_IS_MX23(this); 2425 2426 /* 2427 * Allocate a temporary DMA buffer for reading ID in the 2428 * nand_scan_ident(). 2429 */ 2430 this->bch_geometry.payload_size = 1024; 2431 this->bch_geometry.auxiliary_size = 128; 2432 ret = gpmi_alloc_dma_buffer(this); 2433 if (ret) 2434 goto err_out; 2435 2436 nand_controller_init(&this->base); 2437 this->base.ops = &gpmi_nand_controller_ops; 2438 chip->controller = &this->base; 2439 2440 ret = nand_scan(chip, GPMI_IS_MX6(this) ? 2 : 1); 2441 if (ret) 2442 goto err_out; 2443 2444 ret = nand_boot_init(this); 2445 if (ret) 2446 goto err_nand_cleanup; 2447 ret = nand_create_bbt(chip); 2448 if (ret) 2449 goto err_nand_cleanup; 2450 2451 ret = mtd_device_register(mtd, NULL, 0); 2452 if (ret) 2453 goto err_nand_cleanup; 2454 return 0; 2455 2456 err_nand_cleanup: 2457 nand_cleanup(chip); 2458 err_out: 2459 gpmi_free_dma_buffer(this); 2460 return ret; 2461 } 2462 2463 static const struct of_device_id gpmi_nand_id_table[] = { 2464 { 2465 .compatible = "fsl,imx23-gpmi-nand", 2466 .data = &gpmi_devdata_imx23, 2467 }, { 2468 .compatible = "fsl,imx28-gpmi-nand", 2469 .data = &gpmi_devdata_imx28, 2470 }, { 2471 .compatible = "fsl,imx6q-gpmi-nand", 2472 .data = &gpmi_devdata_imx6q, 2473 }, { 2474 .compatible = "fsl,imx6sx-gpmi-nand", 2475 .data = &gpmi_devdata_imx6sx, 2476 }, { 2477 .compatible = "fsl,imx7d-gpmi-nand", 2478 .data = &gpmi_devdata_imx7d, 2479 }, {} 2480 }; 2481 MODULE_DEVICE_TABLE(of, gpmi_nand_id_table); 2482 2483 static int gpmi_nand_probe(struct platform_device *pdev) 2484 { 2485 struct gpmi_nand_data *this; 2486 const struct of_device_id *of_id; 2487 int ret; 2488 2489 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL); 2490 if (!this) 2491 return -ENOMEM; 2492 2493 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev); 2494 if (of_id) { 2495 this->devdata = of_id->data; 2496 } else { 2497 dev_err(&pdev->dev, "Failed to find the right device id.\n"); 2498 return -ENODEV; 2499 } 2500 2501 platform_set_drvdata(pdev, this); 2502 this->pdev = pdev; 2503 this->dev = &pdev->dev; 2504 2505 ret = acquire_resources(this); 2506 if (ret) 2507 goto exit_acquire_resources; 2508 2509 ret = __gpmi_enable_clk(this, true); 2510 if (ret) 2511 goto exit_acquire_resources; 2512 2513 pm_runtime_set_autosuspend_delay(&pdev->dev, 500); 2514 pm_runtime_use_autosuspend(&pdev->dev); 2515 pm_runtime_set_active(&pdev->dev); 2516 pm_runtime_enable(&pdev->dev); 2517 pm_runtime_get_sync(&pdev->dev); 2518 2519 ret = gpmi_init(this); 2520 if (ret) 2521 goto exit_nfc_init; 2522 2523 ret = gpmi_nand_init(this); 2524 if (ret) 2525 goto exit_nfc_init; 2526 2527 pm_runtime_mark_last_busy(&pdev->dev); 2528 pm_runtime_put_autosuspend(&pdev->dev); 2529 2530 dev_info(this->dev, "driver registered.\n"); 2531 2532 return 0; 2533 2534 exit_nfc_init: 2535 pm_runtime_put(&pdev->dev); 2536 pm_runtime_disable(&pdev->dev); 2537 release_resources(this); 2538 exit_acquire_resources: 2539 2540 return ret; 2541 } 2542 2543 static int gpmi_nand_remove(struct platform_device *pdev) 2544 { 2545 struct gpmi_nand_data *this = platform_get_drvdata(pdev); 2546 struct nand_chip *chip = &this->nand; 2547 int ret; 2548 2549 pm_runtime_put_sync(&pdev->dev); 2550 pm_runtime_disable(&pdev->dev); 2551 2552 ret = mtd_device_unregister(nand_to_mtd(chip)); 2553 WARN_ON(ret); 2554 nand_cleanup(chip); 2555 gpmi_free_dma_buffer(this); 2556 release_resources(this); 2557 return 0; 2558 } 2559 2560 #ifdef CONFIG_PM_SLEEP 2561 static int gpmi_pm_suspend(struct device *dev) 2562 { 2563 struct gpmi_nand_data *this = dev_get_drvdata(dev); 2564 2565 release_dma_channels(this); 2566 return 0; 2567 } 2568 2569 static int gpmi_pm_resume(struct device *dev) 2570 { 2571 struct gpmi_nand_data *this = dev_get_drvdata(dev); 2572 int ret; 2573 2574 ret = acquire_dma_channels(this); 2575 if (ret < 0) 2576 return ret; 2577 2578 /* re-init the GPMI registers */ 2579 ret = gpmi_init(this); 2580 if (ret) { 2581 dev_err(this->dev, "Error setting GPMI : %d\n", ret); 2582 return ret; 2583 } 2584 2585 /* Set flag to get timing setup restored for next exec_op */ 2586 if (this->hw.clk_rate) 2587 this->hw.must_apply_timings = true; 2588 2589 /* re-init the BCH registers */ 2590 ret = bch_set_geometry(this); 2591 if (ret) { 2592 dev_err(this->dev, "Error setting BCH : %d\n", ret); 2593 return ret; 2594 } 2595 2596 return 0; 2597 } 2598 #endif /* CONFIG_PM_SLEEP */ 2599 2600 static int __maybe_unused gpmi_runtime_suspend(struct device *dev) 2601 { 2602 struct gpmi_nand_data *this = dev_get_drvdata(dev); 2603 2604 return __gpmi_enable_clk(this, false); 2605 } 2606 2607 static int __maybe_unused gpmi_runtime_resume(struct device *dev) 2608 { 2609 struct gpmi_nand_data *this = dev_get_drvdata(dev); 2610 2611 return __gpmi_enable_clk(this, true); 2612 } 2613 2614 static const struct dev_pm_ops gpmi_pm_ops = { 2615 SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume) 2616 SET_RUNTIME_PM_OPS(gpmi_runtime_suspend, gpmi_runtime_resume, NULL) 2617 }; 2618 2619 static struct platform_driver gpmi_nand_driver = { 2620 .driver = { 2621 .name = "gpmi-nand", 2622 .pm = &gpmi_pm_ops, 2623 .of_match_table = gpmi_nand_id_table, 2624 }, 2625 .probe = gpmi_nand_probe, 2626 .remove = gpmi_nand_remove, 2627 }; 2628 module_platform_driver(gpmi_nand_driver); 2629 2630 MODULE_AUTHOR("Freescale Semiconductor, Inc."); 2631 MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver"); 2632 MODULE_LICENSE("GPL"); 2633