1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Generic Error-Correcting Code (ECC) engine 4 * 5 * Copyright (C) 2019 Macronix 6 * Author: 7 * Miquèl RAYNAL <miquel.raynal@bootlin.com> 8 * 9 * 10 * This file describes the abstraction of any NAND ECC engine. It has been 11 * designed to fit most cases, including parallel NANDs and SPI-NANDs. 12 * 13 * There are three main situations where instantiating this ECC engine makes 14 * sense: 15 * - external: The ECC engine is outside the NAND pipeline, typically this 16 * is a software ECC engine, or an hardware engine that is 17 * outside the NAND controller pipeline. 18 * - pipelined: The ECC engine is inside the NAND pipeline, ie. on the 19 * controller's side. This is the case of most of the raw NAND 20 * controllers. In the pipeline case, the ECC bytes are 21 * generated/data corrected on the fly when a page is 22 * written/read. 23 * - ondie: The ECC engine is inside the NAND pipeline, on the chip's side. 24 * Some NAND chips can correct themselves the data. 25 * 26 * Besides the initial setup and final cleanups, the interfaces are rather 27 * simple: 28 * - prepare: Prepare an I/O request. Enable/disable the ECC engine based on 29 * the I/O request type. In case of software correction or external 30 * engine, this step may involve to derive the ECC bytes and place 31 * them in the OOB area before a write. 32 * - finish: Finish an I/O request. Correct the data in case of a read 33 * request and report the number of corrected bits/uncorrectable 34 * errors. Most likely empty for write operations, unless you have 35 * hardware specific stuff to do, like shutting down the engine to 36 * save power. 37 * 38 * The I/O request should be enclosed in a prepare()/finish() pair of calls 39 * and will behave differently depending on the requested I/O type: 40 * - raw: Correction disabled 41 * - ecc: Correction enabled 42 * 43 * The request direction is impacting the logic as well: 44 * - read: Load data from the NAND chip 45 * - write: Store data in the NAND chip 46 * 47 * Mixing all this combinations together gives the following behavior. 48 * Those are just examples, drivers are free to add custom steps in their 49 * prepare/finish hook. 50 * 51 * [external ECC engine] 52 * - external + prepare + raw + read: do nothing 53 * - external + finish + raw + read: do nothing 54 * - external + prepare + raw + write: do nothing 55 * - external + finish + raw + write: do nothing 56 * - external + prepare + ecc + read: do nothing 57 * - external + finish + ecc + read: calculate expected ECC bytes, extract 58 * ECC bytes from OOB buffer, correct 59 * and report any bitflip/error 60 * - external + prepare + ecc + write: calculate ECC bytes and store them at 61 * the right place in the OOB buffer based 62 * on the OOB layout 63 * - external + finish + ecc + write: do nothing 64 * 65 * [pipelined ECC engine] 66 * - pipelined + prepare + raw + read: disable the controller's ECC engine if 67 * activated 68 * - pipelined + finish + raw + read: do nothing 69 * - pipelined + prepare + raw + write: disable the controller's ECC engine if 70 * activated 71 * - pipelined + finish + raw + write: do nothing 72 * - pipelined + prepare + ecc + read: enable the controller's ECC engine if 73 * deactivated 74 * - pipelined + finish + ecc + read: check the status, report any 75 * error/bitflip 76 * - pipelined + prepare + ecc + write: enable the controller's ECC engine if 77 * deactivated 78 * - pipelined + finish + ecc + write: do nothing 79 * 80 * [ondie ECC engine] 81 * - ondie + prepare + raw + read: send commands to disable the on-chip ECC 82 * engine if activated 83 * - ondie + finish + raw + read: do nothing 84 * - ondie + prepare + raw + write: send commands to disable the on-chip ECC 85 * engine if activated 86 * - ondie + finish + raw + write: do nothing 87 * - ondie + prepare + ecc + read: send commands to enable the on-chip ECC 88 * engine if deactivated 89 * - ondie + finish + ecc + read: send commands to check the status, report 90 * any error/bitflip 91 * - ondie + prepare + ecc + write: send commands to enable the on-chip ECC 92 * engine if deactivated 93 * - ondie + finish + ecc + write: do nothing 94 */ 95 96 #include <linux/module.h> 97 #include <linux/mtd/nand.h> 98 #include <linux/platform_device.h> 99 #include <linux/slab.h> 100 #include <linux/of.h> 101 #include <linux/of_platform.h> 102 103 static LIST_HEAD(on_host_hw_engines); 104 static DEFINE_MUTEX(on_host_hw_engines_mutex); 105 106 /** 107 * nand_ecc_init_ctx - Init the ECC engine context 108 * @nand: the NAND device 109 * 110 * On success, the caller is responsible of calling @nand_ecc_cleanup_ctx(). 111 */ 112 int nand_ecc_init_ctx(struct nand_device *nand) 113 { 114 if (!nand->ecc.engine || !nand->ecc.engine->ops->init_ctx) 115 return 0; 116 117 return nand->ecc.engine->ops->init_ctx(nand); 118 } 119 EXPORT_SYMBOL(nand_ecc_init_ctx); 120 121 /** 122 * nand_ecc_cleanup_ctx - Cleanup the ECC engine context 123 * @nand: the NAND device 124 */ 125 void nand_ecc_cleanup_ctx(struct nand_device *nand) 126 { 127 if (nand->ecc.engine && nand->ecc.engine->ops->cleanup_ctx) 128 nand->ecc.engine->ops->cleanup_ctx(nand); 129 } 130 EXPORT_SYMBOL(nand_ecc_cleanup_ctx); 131 132 /** 133 * nand_ecc_prepare_io_req - Prepare an I/O request 134 * @nand: the NAND device 135 * @req: the I/O request 136 */ 137 int nand_ecc_prepare_io_req(struct nand_device *nand, 138 struct nand_page_io_req *req) 139 { 140 if (!nand->ecc.engine || !nand->ecc.engine->ops->prepare_io_req) 141 return 0; 142 143 return nand->ecc.engine->ops->prepare_io_req(nand, req); 144 } 145 EXPORT_SYMBOL(nand_ecc_prepare_io_req); 146 147 /** 148 * nand_ecc_finish_io_req - Finish an I/O request 149 * @nand: the NAND device 150 * @req: the I/O request 151 */ 152 int nand_ecc_finish_io_req(struct nand_device *nand, 153 struct nand_page_io_req *req) 154 { 155 if (!nand->ecc.engine || !nand->ecc.engine->ops->finish_io_req) 156 return 0; 157 158 return nand->ecc.engine->ops->finish_io_req(nand, req); 159 } 160 EXPORT_SYMBOL(nand_ecc_finish_io_req); 161 162 bool nand_ecc_is_pipelined(const struct nand_device *nand) 163 { 164 return nand->ecc.engine && 165 nand->ecc.engine->integration == NAND_ECC_ENGINE_INTEGRATION_PIPELINED; 166 } 167 EXPORT_SYMBOL(nand_ecc_is_pipelined); 168 169 /* Define default OOB placement schemes for large and small page devices */ 170 static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section, 171 struct mtd_oob_region *oobregion) 172 { 173 struct nand_device *nand = mtd_to_nanddev(mtd); 174 unsigned int total_ecc_bytes = nand->ecc.ctx.total; 175 176 if (section > 1) 177 return -ERANGE; 178 179 if (!section) { 180 oobregion->offset = 0; 181 if (mtd->oobsize == 16) 182 oobregion->length = 4; 183 else 184 oobregion->length = 3; 185 } else { 186 if (mtd->oobsize == 8) 187 return -ERANGE; 188 189 oobregion->offset = 6; 190 oobregion->length = total_ecc_bytes - 4; 191 } 192 193 return 0; 194 } 195 196 static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section, 197 struct mtd_oob_region *oobregion) 198 { 199 if (section > 1) 200 return -ERANGE; 201 202 if (mtd->oobsize == 16) { 203 if (section) 204 return -ERANGE; 205 206 oobregion->length = 8; 207 oobregion->offset = 8; 208 } else { 209 oobregion->length = 2; 210 if (!section) 211 oobregion->offset = 3; 212 else 213 oobregion->offset = 6; 214 } 215 216 return 0; 217 } 218 219 static const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = { 220 .ecc = nand_ooblayout_ecc_sp, 221 .free = nand_ooblayout_free_sp, 222 }; 223 224 const struct mtd_ooblayout_ops *nand_get_small_page_ooblayout(void) 225 { 226 return &nand_ooblayout_sp_ops; 227 } 228 EXPORT_SYMBOL_GPL(nand_get_small_page_ooblayout); 229 230 static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section, 231 struct mtd_oob_region *oobregion) 232 { 233 struct nand_device *nand = mtd_to_nanddev(mtd); 234 unsigned int total_ecc_bytes = nand->ecc.ctx.total; 235 236 if (section || !total_ecc_bytes) 237 return -ERANGE; 238 239 oobregion->length = total_ecc_bytes; 240 oobregion->offset = mtd->oobsize - oobregion->length; 241 242 return 0; 243 } 244 245 static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section, 246 struct mtd_oob_region *oobregion) 247 { 248 struct nand_device *nand = mtd_to_nanddev(mtd); 249 unsigned int total_ecc_bytes = nand->ecc.ctx.total; 250 251 if (section) 252 return -ERANGE; 253 254 oobregion->length = mtd->oobsize - total_ecc_bytes - 2; 255 oobregion->offset = 2; 256 257 return 0; 258 } 259 260 static const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = { 261 .ecc = nand_ooblayout_ecc_lp, 262 .free = nand_ooblayout_free_lp, 263 }; 264 265 const struct mtd_ooblayout_ops *nand_get_large_page_ooblayout(void) 266 { 267 return &nand_ooblayout_lp_ops; 268 } 269 EXPORT_SYMBOL_GPL(nand_get_large_page_ooblayout); 270 271 /* 272 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC 273 * are placed at a fixed offset. 274 */ 275 static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section, 276 struct mtd_oob_region *oobregion) 277 { 278 struct nand_device *nand = mtd_to_nanddev(mtd); 279 unsigned int total_ecc_bytes = nand->ecc.ctx.total; 280 281 if (section) 282 return -ERANGE; 283 284 switch (mtd->oobsize) { 285 case 64: 286 oobregion->offset = 40; 287 break; 288 case 128: 289 oobregion->offset = 80; 290 break; 291 default: 292 return -EINVAL; 293 } 294 295 oobregion->length = total_ecc_bytes; 296 if (oobregion->offset + oobregion->length > mtd->oobsize) 297 return -ERANGE; 298 299 return 0; 300 } 301 302 static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section, 303 struct mtd_oob_region *oobregion) 304 { 305 struct nand_device *nand = mtd_to_nanddev(mtd); 306 unsigned int total_ecc_bytes = nand->ecc.ctx.total; 307 int ecc_offset = 0; 308 309 if (section < 0 || section > 1) 310 return -ERANGE; 311 312 switch (mtd->oobsize) { 313 case 64: 314 ecc_offset = 40; 315 break; 316 case 128: 317 ecc_offset = 80; 318 break; 319 default: 320 return -EINVAL; 321 } 322 323 if (section == 0) { 324 oobregion->offset = 2; 325 oobregion->length = ecc_offset - 2; 326 } else { 327 oobregion->offset = ecc_offset + total_ecc_bytes; 328 oobregion->length = mtd->oobsize - oobregion->offset; 329 } 330 331 return 0; 332 } 333 334 static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = { 335 .ecc = nand_ooblayout_ecc_lp_hamming, 336 .free = nand_ooblayout_free_lp_hamming, 337 }; 338 339 const struct mtd_ooblayout_ops *nand_get_large_page_hamming_ooblayout(void) 340 { 341 return &nand_ooblayout_lp_hamming_ops; 342 } 343 EXPORT_SYMBOL_GPL(nand_get_large_page_hamming_ooblayout); 344 345 static enum nand_ecc_engine_type 346 of_get_nand_ecc_engine_type(struct device_node *np) 347 { 348 struct device_node *eng_np; 349 350 if (of_property_read_bool(np, "nand-no-ecc-engine")) 351 return NAND_ECC_ENGINE_TYPE_NONE; 352 353 if (of_property_read_bool(np, "nand-use-soft-ecc-engine")) 354 return NAND_ECC_ENGINE_TYPE_SOFT; 355 356 eng_np = of_parse_phandle(np, "nand-ecc-engine", 0); 357 of_node_put(eng_np); 358 359 if (eng_np) { 360 if (eng_np == np) 361 return NAND_ECC_ENGINE_TYPE_ON_DIE; 362 else 363 return NAND_ECC_ENGINE_TYPE_ON_HOST; 364 } 365 366 return NAND_ECC_ENGINE_TYPE_INVALID; 367 } 368 369 static const char * const nand_ecc_placement[] = { 370 [NAND_ECC_PLACEMENT_OOB] = "oob", 371 [NAND_ECC_PLACEMENT_INTERLEAVED] = "interleaved", 372 }; 373 374 static enum nand_ecc_placement of_get_nand_ecc_placement(struct device_node *np) 375 { 376 enum nand_ecc_placement placement; 377 const char *pm; 378 int err; 379 380 err = of_property_read_string(np, "nand-ecc-placement", &pm); 381 if (!err) { 382 for (placement = NAND_ECC_PLACEMENT_OOB; 383 placement < ARRAY_SIZE(nand_ecc_placement); placement++) { 384 if (!strcasecmp(pm, nand_ecc_placement[placement])) 385 return placement; 386 } 387 } 388 389 return NAND_ECC_PLACEMENT_UNKNOWN; 390 } 391 392 static const char * const nand_ecc_algos[] = { 393 [NAND_ECC_ALGO_HAMMING] = "hamming", 394 [NAND_ECC_ALGO_BCH] = "bch", 395 [NAND_ECC_ALGO_RS] = "rs", 396 }; 397 398 static enum nand_ecc_algo of_get_nand_ecc_algo(struct device_node *np) 399 { 400 enum nand_ecc_algo ecc_algo; 401 const char *pm; 402 int err; 403 404 err = of_property_read_string(np, "nand-ecc-algo", &pm); 405 if (!err) { 406 for (ecc_algo = NAND_ECC_ALGO_HAMMING; 407 ecc_algo < ARRAY_SIZE(nand_ecc_algos); 408 ecc_algo++) { 409 if (!strcasecmp(pm, nand_ecc_algos[ecc_algo])) 410 return ecc_algo; 411 } 412 } 413 414 return NAND_ECC_ALGO_UNKNOWN; 415 } 416 417 static int of_get_nand_ecc_step_size(struct device_node *np) 418 { 419 int ret; 420 u32 val; 421 422 ret = of_property_read_u32(np, "nand-ecc-step-size", &val); 423 return ret ? ret : val; 424 } 425 426 static int of_get_nand_ecc_strength(struct device_node *np) 427 { 428 int ret; 429 u32 val; 430 431 ret = of_property_read_u32(np, "nand-ecc-strength", &val); 432 return ret ? ret : val; 433 } 434 435 void of_get_nand_ecc_user_config(struct nand_device *nand) 436 { 437 struct device_node *dn = nanddev_get_of_node(nand); 438 int strength, size; 439 440 nand->ecc.user_conf.engine_type = of_get_nand_ecc_engine_type(dn); 441 nand->ecc.user_conf.algo = of_get_nand_ecc_algo(dn); 442 nand->ecc.user_conf.placement = of_get_nand_ecc_placement(dn); 443 444 strength = of_get_nand_ecc_strength(dn); 445 if (strength >= 0) 446 nand->ecc.user_conf.strength = strength; 447 448 size = of_get_nand_ecc_step_size(dn); 449 if (size >= 0) 450 nand->ecc.user_conf.step_size = size; 451 452 if (of_property_read_bool(dn, "nand-ecc-maximize")) 453 nand->ecc.user_conf.flags |= NAND_ECC_MAXIMIZE_STRENGTH; 454 } 455 EXPORT_SYMBOL(of_get_nand_ecc_user_config); 456 457 /** 458 * nand_ecc_is_strong_enough - Check if the chip configuration meets the 459 * datasheet requirements. 460 * 461 * @nand: Device to check 462 * 463 * If our configuration corrects A bits per B bytes and the minimum 464 * required correction level is X bits per Y bytes, then we must ensure 465 * both of the following are true: 466 * 467 * (1) A / B >= X / Y 468 * (2) A >= X 469 * 470 * Requirement (1) ensures we can correct for the required bitflip density. 471 * Requirement (2) ensures we can correct even when all bitflips are clumped 472 * in the same sector. 473 */ 474 bool nand_ecc_is_strong_enough(struct nand_device *nand) 475 { 476 const struct nand_ecc_props *reqs = nanddev_get_ecc_requirements(nand); 477 const struct nand_ecc_props *conf = nanddev_get_ecc_conf(nand); 478 struct mtd_info *mtd = nanddev_to_mtd(nand); 479 int corr, ds_corr; 480 481 if (conf->step_size == 0 || reqs->step_size == 0) 482 /* Not enough information */ 483 return true; 484 485 /* 486 * We get the number of corrected bits per page to compare 487 * the correction density. 488 */ 489 corr = (mtd->writesize * conf->strength) / conf->step_size; 490 ds_corr = (mtd->writesize * reqs->strength) / reqs->step_size; 491 492 return corr >= ds_corr && conf->strength >= reqs->strength; 493 } 494 EXPORT_SYMBOL(nand_ecc_is_strong_enough); 495 496 /* ECC engine driver internal helpers */ 497 int nand_ecc_init_req_tweaking(struct nand_ecc_req_tweak_ctx *ctx, 498 struct nand_device *nand) 499 { 500 unsigned int total_buffer_size; 501 502 ctx->nand = nand; 503 504 /* Let the user decide the exact length of each buffer */ 505 if (!ctx->page_buffer_size) 506 ctx->page_buffer_size = nanddev_page_size(nand); 507 if (!ctx->oob_buffer_size) 508 ctx->oob_buffer_size = nanddev_per_page_oobsize(nand); 509 510 total_buffer_size = ctx->page_buffer_size + ctx->oob_buffer_size; 511 512 ctx->spare_databuf = kzalloc(total_buffer_size, GFP_KERNEL); 513 if (!ctx->spare_databuf) 514 return -ENOMEM; 515 516 ctx->spare_oobbuf = ctx->spare_databuf + ctx->page_buffer_size; 517 518 return 0; 519 } 520 EXPORT_SYMBOL_GPL(nand_ecc_init_req_tweaking); 521 522 void nand_ecc_cleanup_req_tweaking(struct nand_ecc_req_tweak_ctx *ctx) 523 { 524 kfree(ctx->spare_databuf); 525 } 526 EXPORT_SYMBOL_GPL(nand_ecc_cleanup_req_tweaking); 527 528 /* 529 * Ensure data and OOB area is fully read/written otherwise the correction might 530 * not work as expected. 531 */ 532 void nand_ecc_tweak_req(struct nand_ecc_req_tweak_ctx *ctx, 533 struct nand_page_io_req *req) 534 { 535 struct nand_device *nand = ctx->nand; 536 struct nand_page_io_req *orig, *tweak; 537 538 /* Save the original request */ 539 ctx->orig_req = *req; 540 ctx->bounce_data = false; 541 ctx->bounce_oob = false; 542 orig = &ctx->orig_req; 543 tweak = req; 544 545 /* Ensure the request covers the entire page */ 546 if (orig->datalen < nanddev_page_size(nand)) { 547 ctx->bounce_data = true; 548 tweak->dataoffs = 0; 549 tweak->datalen = nanddev_page_size(nand); 550 tweak->databuf.in = ctx->spare_databuf; 551 memset(tweak->databuf.in, 0xFF, ctx->page_buffer_size); 552 } 553 554 if (orig->ooblen < nanddev_per_page_oobsize(nand)) { 555 ctx->bounce_oob = true; 556 tweak->ooboffs = 0; 557 tweak->ooblen = nanddev_per_page_oobsize(nand); 558 tweak->oobbuf.in = ctx->spare_oobbuf; 559 memset(tweak->oobbuf.in, 0xFF, ctx->oob_buffer_size); 560 } 561 562 /* Copy the data that must be written in the bounce buffers, if needed */ 563 if (orig->type == NAND_PAGE_WRITE) { 564 if (ctx->bounce_data) 565 memcpy((void *)tweak->databuf.out + orig->dataoffs, 566 orig->databuf.out, orig->datalen); 567 568 if (ctx->bounce_oob) 569 memcpy((void *)tweak->oobbuf.out + orig->ooboffs, 570 orig->oobbuf.out, orig->ooblen); 571 } 572 } 573 EXPORT_SYMBOL_GPL(nand_ecc_tweak_req); 574 575 void nand_ecc_restore_req(struct nand_ecc_req_tweak_ctx *ctx, 576 struct nand_page_io_req *req) 577 { 578 struct nand_page_io_req *orig, *tweak; 579 580 orig = &ctx->orig_req; 581 tweak = req; 582 583 /* Restore the data read from the bounce buffers, if needed */ 584 if (orig->type == NAND_PAGE_READ) { 585 if (ctx->bounce_data) 586 memcpy(orig->databuf.in, 587 tweak->databuf.in + orig->dataoffs, 588 orig->datalen); 589 590 if (ctx->bounce_oob) 591 memcpy(orig->oobbuf.in, 592 tweak->oobbuf.in + orig->ooboffs, 593 orig->ooblen); 594 } 595 596 /* Ensure the original request is restored */ 597 *req = *orig; 598 } 599 EXPORT_SYMBOL_GPL(nand_ecc_restore_req); 600 601 struct nand_ecc_engine *nand_ecc_get_sw_engine(struct nand_device *nand) 602 { 603 unsigned int algo = nand->ecc.user_conf.algo; 604 605 if (algo == NAND_ECC_ALGO_UNKNOWN) 606 algo = nand->ecc.defaults.algo; 607 608 switch (algo) { 609 case NAND_ECC_ALGO_HAMMING: 610 return nand_ecc_sw_hamming_get_engine(); 611 case NAND_ECC_ALGO_BCH: 612 return nand_ecc_sw_bch_get_engine(); 613 default: 614 break; 615 } 616 617 return NULL; 618 } 619 EXPORT_SYMBOL(nand_ecc_get_sw_engine); 620 621 struct nand_ecc_engine *nand_ecc_get_on_die_hw_engine(struct nand_device *nand) 622 { 623 return nand->ecc.ondie_engine; 624 } 625 EXPORT_SYMBOL(nand_ecc_get_on_die_hw_engine); 626 627 int nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine *engine) 628 { 629 struct nand_ecc_engine *item; 630 631 if (!engine) 632 return -EINVAL; 633 634 /* Prevent multiple registrations of one engine */ 635 list_for_each_entry(item, &on_host_hw_engines, node) 636 if (item == engine) 637 return 0; 638 639 mutex_lock(&on_host_hw_engines_mutex); 640 list_add_tail(&engine->node, &on_host_hw_engines); 641 mutex_unlock(&on_host_hw_engines_mutex); 642 643 return 0; 644 } 645 EXPORT_SYMBOL(nand_ecc_register_on_host_hw_engine); 646 647 int nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine *engine) 648 { 649 if (!engine) 650 return -EINVAL; 651 652 mutex_lock(&on_host_hw_engines_mutex); 653 list_del(&engine->node); 654 mutex_unlock(&on_host_hw_engines_mutex); 655 656 return 0; 657 } 658 EXPORT_SYMBOL(nand_ecc_unregister_on_host_hw_engine); 659 660 static struct nand_ecc_engine *nand_ecc_match_on_host_hw_engine(struct device *dev) 661 { 662 struct nand_ecc_engine *item; 663 664 list_for_each_entry(item, &on_host_hw_engines, node) 665 if (item->dev == dev) 666 return item; 667 668 return NULL; 669 } 670 671 struct nand_ecc_engine *nand_ecc_get_on_host_hw_engine(struct nand_device *nand) 672 { 673 struct nand_ecc_engine *engine = NULL; 674 struct device *dev = &nand->mtd.dev; 675 struct platform_device *pdev; 676 struct device_node *np; 677 678 if (list_empty(&on_host_hw_engines)) 679 return NULL; 680 681 /* Check for an explicit nand-ecc-engine property */ 682 np = of_parse_phandle(dev->of_node, "nand-ecc-engine", 0); 683 if (np) { 684 pdev = of_find_device_by_node(np); 685 if (!pdev) 686 return ERR_PTR(-EPROBE_DEFER); 687 688 engine = nand_ecc_match_on_host_hw_engine(&pdev->dev); 689 platform_device_put(pdev); 690 of_node_put(np); 691 692 if (!engine) 693 return ERR_PTR(-EPROBE_DEFER); 694 } 695 696 if (engine) 697 get_device(engine->dev); 698 699 return engine; 700 } 701 EXPORT_SYMBOL(nand_ecc_get_on_host_hw_engine); 702 703 void nand_ecc_put_on_host_hw_engine(struct nand_device *nand) 704 { 705 put_device(nand->ecc.engine->dev); 706 } 707 EXPORT_SYMBOL(nand_ecc_put_on_host_hw_engine); 708 709 /* 710 * In the case of a pipelined engine, the device registering the ECC 711 * engine is not necessarily the ECC engine itself but may be a host controller. 712 * It is then useful to provide a helper to retrieve the right device object 713 * which actually represents the ECC engine. 714 */ 715 struct device *nand_ecc_get_engine_dev(struct device *host) 716 { 717 struct platform_device *ecc_pdev; 718 struct device_node *np; 719 720 /* 721 * If the device node contains this property, it means we need to follow 722 * it in order to get the right ECC engine device we are looking for. 723 */ 724 np = of_parse_phandle(host->of_node, "nand-ecc-engine", 0); 725 if (!np) 726 return host; 727 728 ecc_pdev = of_find_device_by_node(np); 729 if (!ecc_pdev) { 730 of_node_put(np); 731 return NULL; 732 } 733 734 platform_device_put(ecc_pdev); 735 of_node_put(np); 736 737 return &ecc_pdev->dev; 738 } 739 740 MODULE_LICENSE("GPL"); 741 MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>"); 742 MODULE_DESCRIPTION("Generic ECC engine"); 743