1 /* 2 * MTK NAND Flash controller driver. 3 * Copyright (C) 2016 MediaTek Inc. 4 * Authors: Xiaolei Li <xiaolei.li@mediatek.com> 5 * Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 */ 16 17 #include <linux/platform_device.h> 18 #include <linux/dma-mapping.h> 19 #include <linux/interrupt.h> 20 #include <linux/delay.h> 21 #include <linux/clk.h> 22 #include <linux/mtd/rawnand.h> 23 #include <linux/mtd/mtd.h> 24 #include <linux/module.h> 25 #include <linux/iopoll.h> 26 #include <linux/of.h> 27 #include <linux/of_device.h> 28 #include "mtk_ecc.h" 29 30 /* NAND controller register definition */ 31 #define NFI_CNFG (0x00) 32 #define CNFG_AHB BIT(0) 33 #define CNFG_READ_EN BIT(1) 34 #define CNFG_DMA_BURST_EN BIT(2) 35 #define CNFG_BYTE_RW BIT(6) 36 #define CNFG_HW_ECC_EN BIT(8) 37 #define CNFG_AUTO_FMT_EN BIT(9) 38 #define CNFG_OP_CUST (6 << 12) 39 #define NFI_PAGEFMT (0x04) 40 #define PAGEFMT_FDM_ECC_SHIFT (12) 41 #define PAGEFMT_FDM_SHIFT (8) 42 #define PAGEFMT_SEC_SEL_512 BIT(2) 43 #define PAGEFMT_512_2K (0) 44 #define PAGEFMT_2K_4K (1) 45 #define PAGEFMT_4K_8K (2) 46 #define PAGEFMT_8K_16K (3) 47 /* NFI control */ 48 #define NFI_CON (0x08) 49 #define CON_FIFO_FLUSH BIT(0) 50 #define CON_NFI_RST BIT(1) 51 #define CON_BRD BIT(8) /* burst read */ 52 #define CON_BWR BIT(9) /* burst write */ 53 #define CON_SEC_SHIFT (12) 54 /* Timming control register */ 55 #define NFI_ACCCON (0x0C) 56 #define NFI_INTR_EN (0x10) 57 #define INTR_AHB_DONE_EN BIT(6) 58 #define NFI_INTR_STA (0x14) 59 #define NFI_CMD (0x20) 60 #define NFI_ADDRNOB (0x30) 61 #define NFI_COLADDR (0x34) 62 #define NFI_ROWADDR (0x38) 63 #define NFI_STRDATA (0x40) 64 #define STAR_EN (1) 65 #define STAR_DE (0) 66 #define NFI_CNRNB (0x44) 67 #define NFI_DATAW (0x50) 68 #define NFI_DATAR (0x54) 69 #define NFI_PIO_DIRDY (0x58) 70 #define PIO_DI_RDY (0x01) 71 #define NFI_STA (0x60) 72 #define STA_CMD BIT(0) 73 #define STA_ADDR BIT(1) 74 #define STA_BUSY BIT(8) 75 #define STA_EMP_PAGE BIT(12) 76 #define NFI_FSM_CUSTDATA (0xe << 16) 77 #define NFI_FSM_MASK (0xf << 16) 78 #define NFI_ADDRCNTR (0x70) 79 #define CNTR_MASK GENMASK(16, 12) 80 #define ADDRCNTR_SEC_SHIFT (12) 81 #define ADDRCNTR_SEC(val) \ 82 (((val) & CNTR_MASK) >> ADDRCNTR_SEC_SHIFT) 83 #define NFI_STRADDR (0x80) 84 #define NFI_BYTELEN (0x84) 85 #define NFI_CSEL (0x90) 86 #define NFI_FDML(x) (0xA0 + (x) * sizeof(u32) * 2) 87 #define NFI_FDMM(x) (0xA4 + (x) * sizeof(u32) * 2) 88 #define NFI_FDM_MAX_SIZE (8) 89 #define NFI_FDM_MIN_SIZE (1) 90 #define NFI_MASTER_STA (0x224) 91 #define MASTER_STA_MASK (0x0FFF) 92 #define NFI_EMPTY_THRESH (0x23C) 93 94 #define MTK_NAME "mtk-nand" 95 #define KB(x) ((x) * 1024UL) 96 #define MB(x) (KB(x) * 1024UL) 97 98 #define MTK_TIMEOUT (500000) 99 #define MTK_RESET_TIMEOUT (1000000) 100 #define MTK_NAND_MAX_NSELS (2) 101 #define MTK_NFC_MIN_SPARE (16) 102 #define ACCTIMING(tpoecs, tprecs, tc2r, tw2r, twh, twst, trlt) \ 103 ((tpoecs) << 28 | (tprecs) << 22 | (tc2r) << 16 | \ 104 (tw2r) << 12 | (twh) << 8 | (twst) << 4 | (trlt)) 105 106 struct mtk_nfc_caps { 107 const u8 *spare_size; 108 u8 num_spare_size; 109 u8 pageformat_spare_shift; 110 u8 nfi_clk_div; 111 u8 max_sector; 112 u32 max_sector_size; 113 }; 114 115 struct mtk_nfc_bad_mark_ctl { 116 void (*bm_swap)(struct mtd_info *, u8 *buf, int raw); 117 u32 sec; 118 u32 pos; 119 }; 120 121 /* 122 * FDM: region used to store free OOB data 123 */ 124 struct mtk_nfc_fdm { 125 u32 reg_size; 126 u32 ecc_size; 127 }; 128 129 struct mtk_nfc_nand_chip { 130 struct list_head node; 131 struct nand_chip nand; 132 133 struct mtk_nfc_bad_mark_ctl bad_mark; 134 struct mtk_nfc_fdm fdm; 135 u32 spare_per_sector; 136 137 int nsels; 138 u8 sels[0]; 139 /* nothing after this field */ 140 }; 141 142 struct mtk_nfc_clk { 143 struct clk *nfi_clk; 144 struct clk *pad_clk; 145 }; 146 147 struct mtk_nfc { 148 struct nand_controller controller; 149 struct mtk_ecc_config ecc_cfg; 150 struct mtk_nfc_clk clk; 151 struct mtk_ecc *ecc; 152 153 struct device *dev; 154 const struct mtk_nfc_caps *caps; 155 void __iomem *regs; 156 157 struct completion done; 158 struct list_head chips; 159 160 u8 *buffer; 161 }; 162 163 /* 164 * supported spare size of each IP. 165 * order should be the same with the spare size bitfiled defination of 166 * register NFI_PAGEFMT. 167 */ 168 static const u8 spare_size_mt2701[] = { 169 16, 26, 27, 28, 32, 36, 40, 44, 48, 49, 50, 51, 52, 62, 63, 64 170 }; 171 172 static const u8 spare_size_mt2712[] = { 173 16, 26, 27, 28, 32, 36, 40, 44, 48, 49, 50, 51, 52, 62, 61, 63, 64, 67, 174 74 175 }; 176 177 static const u8 spare_size_mt7622[] = { 178 16, 26, 27, 28 179 }; 180 181 static inline struct mtk_nfc_nand_chip *to_mtk_nand(struct nand_chip *nand) 182 { 183 return container_of(nand, struct mtk_nfc_nand_chip, nand); 184 } 185 186 static inline u8 *data_ptr(struct nand_chip *chip, const u8 *p, int i) 187 { 188 return (u8 *)p + i * chip->ecc.size; 189 } 190 191 static inline u8 *oob_ptr(struct nand_chip *chip, int i) 192 { 193 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 194 u8 *poi; 195 196 /* map the sector's FDM data to free oob: 197 * the beginning of the oob area stores the FDM data of bad mark sectors 198 */ 199 200 if (i < mtk_nand->bad_mark.sec) 201 poi = chip->oob_poi + (i + 1) * mtk_nand->fdm.reg_size; 202 else if (i == mtk_nand->bad_mark.sec) 203 poi = chip->oob_poi; 204 else 205 poi = chip->oob_poi + i * mtk_nand->fdm.reg_size; 206 207 return poi; 208 } 209 210 static inline int mtk_data_len(struct nand_chip *chip) 211 { 212 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 213 214 return chip->ecc.size + mtk_nand->spare_per_sector; 215 } 216 217 static inline u8 *mtk_data_ptr(struct nand_chip *chip, int i) 218 { 219 struct mtk_nfc *nfc = nand_get_controller_data(chip); 220 221 return nfc->buffer + i * mtk_data_len(chip); 222 } 223 224 static inline u8 *mtk_oob_ptr(struct nand_chip *chip, int i) 225 { 226 struct mtk_nfc *nfc = nand_get_controller_data(chip); 227 228 return nfc->buffer + i * mtk_data_len(chip) + chip->ecc.size; 229 } 230 231 static inline void nfi_writel(struct mtk_nfc *nfc, u32 val, u32 reg) 232 { 233 writel(val, nfc->regs + reg); 234 } 235 236 static inline void nfi_writew(struct mtk_nfc *nfc, u16 val, u32 reg) 237 { 238 writew(val, nfc->regs + reg); 239 } 240 241 static inline void nfi_writeb(struct mtk_nfc *nfc, u8 val, u32 reg) 242 { 243 writeb(val, nfc->regs + reg); 244 } 245 246 static inline u32 nfi_readl(struct mtk_nfc *nfc, u32 reg) 247 { 248 return readl_relaxed(nfc->regs + reg); 249 } 250 251 static inline u16 nfi_readw(struct mtk_nfc *nfc, u32 reg) 252 { 253 return readw_relaxed(nfc->regs + reg); 254 } 255 256 static inline u8 nfi_readb(struct mtk_nfc *nfc, u32 reg) 257 { 258 return readb_relaxed(nfc->regs + reg); 259 } 260 261 static void mtk_nfc_hw_reset(struct mtk_nfc *nfc) 262 { 263 struct device *dev = nfc->dev; 264 u32 val; 265 int ret; 266 267 /* reset all registers and force the NFI master to terminate */ 268 nfi_writel(nfc, CON_FIFO_FLUSH | CON_NFI_RST, NFI_CON); 269 270 /* wait for the master to finish the last transaction */ 271 ret = readl_poll_timeout(nfc->regs + NFI_MASTER_STA, val, 272 !(val & MASTER_STA_MASK), 50, 273 MTK_RESET_TIMEOUT); 274 if (ret) 275 dev_warn(dev, "master active in reset [0x%x] = 0x%x\n", 276 NFI_MASTER_STA, val); 277 278 /* ensure any status register affected by the NFI master is reset */ 279 nfi_writel(nfc, CON_FIFO_FLUSH | CON_NFI_RST, NFI_CON); 280 nfi_writew(nfc, STAR_DE, NFI_STRDATA); 281 } 282 283 static int mtk_nfc_send_command(struct mtk_nfc *nfc, u8 command) 284 { 285 struct device *dev = nfc->dev; 286 u32 val; 287 int ret; 288 289 nfi_writel(nfc, command, NFI_CMD); 290 291 ret = readl_poll_timeout_atomic(nfc->regs + NFI_STA, val, 292 !(val & STA_CMD), 10, MTK_TIMEOUT); 293 if (ret) { 294 dev_warn(dev, "nfi core timed out entering command mode\n"); 295 return -EIO; 296 } 297 298 return 0; 299 } 300 301 static int mtk_nfc_send_address(struct mtk_nfc *nfc, int addr) 302 { 303 struct device *dev = nfc->dev; 304 u32 val; 305 int ret; 306 307 nfi_writel(nfc, addr, NFI_COLADDR); 308 nfi_writel(nfc, 0, NFI_ROWADDR); 309 nfi_writew(nfc, 1, NFI_ADDRNOB); 310 311 ret = readl_poll_timeout_atomic(nfc->regs + NFI_STA, val, 312 !(val & STA_ADDR), 10, MTK_TIMEOUT); 313 if (ret) { 314 dev_warn(dev, "nfi core timed out entering address mode\n"); 315 return -EIO; 316 } 317 318 return 0; 319 } 320 321 static int mtk_nfc_hw_runtime_config(struct mtd_info *mtd) 322 { 323 struct nand_chip *chip = mtd_to_nand(mtd); 324 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 325 struct mtk_nfc *nfc = nand_get_controller_data(chip); 326 u32 fmt, spare, i; 327 328 if (!mtd->writesize) 329 return 0; 330 331 spare = mtk_nand->spare_per_sector; 332 333 switch (mtd->writesize) { 334 case 512: 335 fmt = PAGEFMT_512_2K | PAGEFMT_SEC_SEL_512; 336 break; 337 case KB(2): 338 if (chip->ecc.size == 512) 339 fmt = PAGEFMT_2K_4K | PAGEFMT_SEC_SEL_512; 340 else 341 fmt = PAGEFMT_512_2K; 342 break; 343 case KB(4): 344 if (chip->ecc.size == 512) 345 fmt = PAGEFMT_4K_8K | PAGEFMT_SEC_SEL_512; 346 else 347 fmt = PAGEFMT_2K_4K; 348 break; 349 case KB(8): 350 if (chip->ecc.size == 512) 351 fmt = PAGEFMT_8K_16K | PAGEFMT_SEC_SEL_512; 352 else 353 fmt = PAGEFMT_4K_8K; 354 break; 355 case KB(16): 356 fmt = PAGEFMT_8K_16K; 357 break; 358 default: 359 dev_err(nfc->dev, "invalid page len: %d\n", mtd->writesize); 360 return -EINVAL; 361 } 362 363 /* 364 * the hardware will double the value for this eccsize, so we need to 365 * halve it 366 */ 367 if (chip->ecc.size == 1024) 368 spare >>= 1; 369 370 for (i = 0; i < nfc->caps->num_spare_size; i++) { 371 if (nfc->caps->spare_size[i] == spare) 372 break; 373 } 374 375 if (i == nfc->caps->num_spare_size) { 376 dev_err(nfc->dev, "invalid spare size %d\n", spare); 377 return -EINVAL; 378 } 379 380 fmt |= i << nfc->caps->pageformat_spare_shift; 381 382 fmt |= mtk_nand->fdm.reg_size << PAGEFMT_FDM_SHIFT; 383 fmt |= mtk_nand->fdm.ecc_size << PAGEFMT_FDM_ECC_SHIFT; 384 nfi_writel(nfc, fmt, NFI_PAGEFMT); 385 386 nfc->ecc_cfg.strength = chip->ecc.strength; 387 nfc->ecc_cfg.len = chip->ecc.size + mtk_nand->fdm.ecc_size; 388 389 return 0; 390 } 391 392 static void mtk_nfc_select_chip(struct mtd_info *mtd, int chip) 393 { 394 struct nand_chip *nand = mtd_to_nand(mtd); 395 struct mtk_nfc *nfc = nand_get_controller_data(nand); 396 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(nand); 397 398 if (chip < 0) 399 return; 400 401 mtk_nfc_hw_runtime_config(mtd); 402 403 nfi_writel(nfc, mtk_nand->sels[chip], NFI_CSEL); 404 } 405 406 static int mtk_nfc_dev_ready(struct mtd_info *mtd) 407 { 408 struct mtk_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd)); 409 410 if (nfi_readl(nfc, NFI_STA) & STA_BUSY) 411 return 0; 412 413 return 1; 414 } 415 416 static void mtk_nfc_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl) 417 { 418 struct mtk_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd)); 419 420 if (ctrl & NAND_ALE) { 421 mtk_nfc_send_address(nfc, dat); 422 } else if (ctrl & NAND_CLE) { 423 mtk_nfc_hw_reset(nfc); 424 425 nfi_writew(nfc, CNFG_OP_CUST, NFI_CNFG); 426 mtk_nfc_send_command(nfc, dat); 427 } 428 } 429 430 static inline void mtk_nfc_wait_ioready(struct mtk_nfc *nfc) 431 { 432 int rc; 433 u8 val; 434 435 rc = readb_poll_timeout_atomic(nfc->regs + NFI_PIO_DIRDY, val, 436 val & PIO_DI_RDY, 10, MTK_TIMEOUT); 437 if (rc < 0) 438 dev_err(nfc->dev, "data not ready\n"); 439 } 440 441 static inline u8 mtk_nfc_read_byte(struct mtd_info *mtd) 442 { 443 struct nand_chip *chip = mtd_to_nand(mtd); 444 struct mtk_nfc *nfc = nand_get_controller_data(chip); 445 u32 reg; 446 447 /* after each byte read, the NFI_STA reg is reset by the hardware */ 448 reg = nfi_readl(nfc, NFI_STA) & NFI_FSM_MASK; 449 if (reg != NFI_FSM_CUSTDATA) { 450 reg = nfi_readw(nfc, NFI_CNFG); 451 reg |= CNFG_BYTE_RW | CNFG_READ_EN; 452 nfi_writew(nfc, reg, NFI_CNFG); 453 454 /* 455 * set to max sector to allow the HW to continue reading over 456 * unaligned accesses 457 */ 458 reg = (nfc->caps->max_sector << CON_SEC_SHIFT) | CON_BRD; 459 nfi_writel(nfc, reg, NFI_CON); 460 461 /* trigger to fetch data */ 462 nfi_writew(nfc, STAR_EN, NFI_STRDATA); 463 } 464 465 mtk_nfc_wait_ioready(nfc); 466 467 return nfi_readb(nfc, NFI_DATAR); 468 } 469 470 static void mtk_nfc_read_buf(struct mtd_info *mtd, u8 *buf, int len) 471 { 472 int i; 473 474 for (i = 0; i < len; i++) 475 buf[i] = mtk_nfc_read_byte(mtd); 476 } 477 478 static void mtk_nfc_write_byte(struct mtd_info *mtd, u8 byte) 479 { 480 struct mtk_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd)); 481 u32 reg; 482 483 reg = nfi_readl(nfc, NFI_STA) & NFI_FSM_MASK; 484 485 if (reg != NFI_FSM_CUSTDATA) { 486 reg = nfi_readw(nfc, NFI_CNFG) | CNFG_BYTE_RW; 487 nfi_writew(nfc, reg, NFI_CNFG); 488 489 reg = nfc->caps->max_sector << CON_SEC_SHIFT | CON_BWR; 490 nfi_writel(nfc, reg, NFI_CON); 491 492 nfi_writew(nfc, STAR_EN, NFI_STRDATA); 493 } 494 495 mtk_nfc_wait_ioready(nfc); 496 nfi_writeb(nfc, byte, NFI_DATAW); 497 } 498 499 static void mtk_nfc_write_buf(struct mtd_info *mtd, const u8 *buf, int len) 500 { 501 int i; 502 503 for (i = 0; i < len; i++) 504 mtk_nfc_write_byte(mtd, buf[i]); 505 } 506 507 static int mtk_nfc_setup_data_interface(struct mtd_info *mtd, int csline, 508 const struct nand_data_interface *conf) 509 { 510 struct mtk_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd)); 511 const struct nand_sdr_timings *timings; 512 u32 rate, tpoecs, tprecs, tc2r, tw2r, twh, twst, trlt; 513 514 timings = nand_get_sdr_timings(conf); 515 if (IS_ERR(timings)) 516 return -ENOTSUPP; 517 518 if (csline == NAND_DATA_IFACE_CHECK_ONLY) 519 return 0; 520 521 rate = clk_get_rate(nfc->clk.nfi_clk); 522 /* There is a frequency divider in some IPs */ 523 rate /= nfc->caps->nfi_clk_div; 524 525 /* turn clock rate into KHZ */ 526 rate /= 1000; 527 528 tpoecs = max(timings->tALH_min, timings->tCLH_min) / 1000; 529 tpoecs = DIV_ROUND_UP(tpoecs * rate, 1000000); 530 tpoecs &= 0xf; 531 532 tprecs = max(timings->tCLS_min, timings->tALS_min) / 1000; 533 tprecs = DIV_ROUND_UP(tprecs * rate, 1000000); 534 tprecs &= 0x3f; 535 536 /* sdr interface has no tCR which means CE# low to RE# low */ 537 tc2r = 0; 538 539 tw2r = timings->tWHR_min / 1000; 540 tw2r = DIV_ROUND_UP(tw2r * rate, 1000000); 541 tw2r = DIV_ROUND_UP(tw2r - 1, 2); 542 tw2r &= 0xf; 543 544 twh = max(timings->tREH_min, timings->tWH_min) / 1000; 545 twh = DIV_ROUND_UP(twh * rate, 1000000) - 1; 546 twh &= 0xf; 547 548 twst = timings->tWP_min / 1000; 549 twst = DIV_ROUND_UP(twst * rate, 1000000) - 1; 550 twst &= 0xf; 551 552 trlt = max(timings->tREA_max, timings->tRP_min) / 1000; 553 trlt = DIV_ROUND_UP(trlt * rate, 1000000) - 1; 554 trlt &= 0xf; 555 556 /* 557 * ACCON: access timing control register 558 * ------------------------------------- 559 * 31:28: tpoecs, minimum required time for CS post pulling down after 560 * accessing the device 561 * 27:22: tprecs, minimum required time for CS pre pulling down before 562 * accessing the device 563 * 21:16: tc2r, minimum required time from NCEB low to NREB low 564 * 15:12: tw2r, minimum required time from NWEB high to NREB low. 565 * 11:08: twh, write enable hold time 566 * 07:04: twst, write wait states 567 * 03:00: trlt, read wait states 568 */ 569 trlt = ACCTIMING(tpoecs, tprecs, tc2r, tw2r, twh, twst, trlt); 570 nfi_writel(nfc, trlt, NFI_ACCCON); 571 572 return 0; 573 } 574 575 static int mtk_nfc_sector_encode(struct nand_chip *chip, u8 *data) 576 { 577 struct mtk_nfc *nfc = nand_get_controller_data(chip); 578 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 579 int size = chip->ecc.size + mtk_nand->fdm.reg_size; 580 581 nfc->ecc_cfg.mode = ECC_DMA_MODE; 582 nfc->ecc_cfg.op = ECC_ENCODE; 583 584 return mtk_ecc_encode(nfc->ecc, &nfc->ecc_cfg, data, size); 585 } 586 587 static void mtk_nfc_no_bad_mark_swap(struct mtd_info *a, u8 *b, int c) 588 { 589 /* nop */ 590 } 591 592 static void mtk_nfc_bad_mark_swap(struct mtd_info *mtd, u8 *buf, int raw) 593 { 594 struct nand_chip *chip = mtd_to_nand(mtd); 595 struct mtk_nfc_nand_chip *nand = to_mtk_nand(chip); 596 u32 bad_pos = nand->bad_mark.pos; 597 598 if (raw) 599 bad_pos += nand->bad_mark.sec * mtk_data_len(chip); 600 else 601 bad_pos += nand->bad_mark.sec * chip->ecc.size; 602 603 swap(chip->oob_poi[0], buf[bad_pos]); 604 } 605 606 static int mtk_nfc_format_subpage(struct mtd_info *mtd, u32 offset, 607 u32 len, const u8 *buf) 608 { 609 struct nand_chip *chip = mtd_to_nand(mtd); 610 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 611 struct mtk_nfc *nfc = nand_get_controller_data(chip); 612 struct mtk_nfc_fdm *fdm = &mtk_nand->fdm; 613 u32 start, end; 614 int i, ret; 615 616 start = offset / chip->ecc.size; 617 end = DIV_ROUND_UP(offset + len, chip->ecc.size); 618 619 memset(nfc->buffer, 0xff, mtd->writesize + mtd->oobsize); 620 for (i = 0; i < chip->ecc.steps; i++) { 621 memcpy(mtk_data_ptr(chip, i), data_ptr(chip, buf, i), 622 chip->ecc.size); 623 624 if (start > i || i >= end) 625 continue; 626 627 if (i == mtk_nand->bad_mark.sec) 628 mtk_nand->bad_mark.bm_swap(mtd, nfc->buffer, 1); 629 630 memcpy(mtk_oob_ptr(chip, i), oob_ptr(chip, i), fdm->reg_size); 631 632 /* program the CRC back to the OOB */ 633 ret = mtk_nfc_sector_encode(chip, mtk_data_ptr(chip, i)); 634 if (ret < 0) 635 return ret; 636 } 637 638 return 0; 639 } 640 641 static void mtk_nfc_format_page(struct mtd_info *mtd, const u8 *buf) 642 { 643 struct nand_chip *chip = mtd_to_nand(mtd); 644 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 645 struct mtk_nfc *nfc = nand_get_controller_data(chip); 646 struct mtk_nfc_fdm *fdm = &mtk_nand->fdm; 647 u32 i; 648 649 memset(nfc->buffer, 0xff, mtd->writesize + mtd->oobsize); 650 for (i = 0; i < chip->ecc.steps; i++) { 651 if (buf) 652 memcpy(mtk_data_ptr(chip, i), data_ptr(chip, buf, i), 653 chip->ecc.size); 654 655 if (i == mtk_nand->bad_mark.sec) 656 mtk_nand->bad_mark.bm_swap(mtd, nfc->buffer, 1); 657 658 memcpy(mtk_oob_ptr(chip, i), oob_ptr(chip, i), fdm->reg_size); 659 } 660 } 661 662 static inline void mtk_nfc_read_fdm(struct nand_chip *chip, u32 start, 663 u32 sectors) 664 { 665 struct mtk_nfc *nfc = nand_get_controller_data(chip); 666 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 667 struct mtk_nfc_fdm *fdm = &mtk_nand->fdm; 668 u32 vall, valm; 669 u8 *oobptr; 670 int i, j; 671 672 for (i = 0; i < sectors; i++) { 673 oobptr = oob_ptr(chip, start + i); 674 vall = nfi_readl(nfc, NFI_FDML(i)); 675 valm = nfi_readl(nfc, NFI_FDMM(i)); 676 677 for (j = 0; j < fdm->reg_size; j++) 678 oobptr[j] = (j >= 4 ? valm : vall) >> ((j % 4) * 8); 679 } 680 } 681 682 static inline void mtk_nfc_write_fdm(struct nand_chip *chip) 683 { 684 struct mtk_nfc *nfc = nand_get_controller_data(chip); 685 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 686 struct mtk_nfc_fdm *fdm = &mtk_nand->fdm; 687 u32 vall, valm; 688 u8 *oobptr; 689 int i, j; 690 691 for (i = 0; i < chip->ecc.steps; i++) { 692 oobptr = oob_ptr(chip, i); 693 vall = 0; 694 valm = 0; 695 for (j = 0; j < 8; j++) { 696 if (j < 4) 697 vall |= (j < fdm->reg_size ? oobptr[j] : 0xff) 698 << (j * 8); 699 else 700 valm |= (j < fdm->reg_size ? oobptr[j] : 0xff) 701 << ((j - 4) * 8); 702 } 703 nfi_writel(nfc, vall, NFI_FDML(i)); 704 nfi_writel(nfc, valm, NFI_FDMM(i)); 705 } 706 } 707 708 static int mtk_nfc_do_write_page(struct mtd_info *mtd, struct nand_chip *chip, 709 const u8 *buf, int page, int len) 710 { 711 struct mtk_nfc *nfc = nand_get_controller_data(chip); 712 struct device *dev = nfc->dev; 713 dma_addr_t addr; 714 u32 reg; 715 int ret; 716 717 addr = dma_map_single(dev, (void *)buf, len, DMA_TO_DEVICE); 718 ret = dma_mapping_error(nfc->dev, addr); 719 if (ret) { 720 dev_err(nfc->dev, "dma mapping error\n"); 721 return -EINVAL; 722 } 723 724 reg = nfi_readw(nfc, NFI_CNFG) | CNFG_AHB | CNFG_DMA_BURST_EN; 725 nfi_writew(nfc, reg, NFI_CNFG); 726 727 nfi_writel(nfc, chip->ecc.steps << CON_SEC_SHIFT, NFI_CON); 728 nfi_writel(nfc, lower_32_bits(addr), NFI_STRADDR); 729 nfi_writew(nfc, INTR_AHB_DONE_EN, NFI_INTR_EN); 730 731 init_completion(&nfc->done); 732 733 reg = nfi_readl(nfc, NFI_CON) | CON_BWR; 734 nfi_writel(nfc, reg, NFI_CON); 735 nfi_writew(nfc, STAR_EN, NFI_STRDATA); 736 737 ret = wait_for_completion_timeout(&nfc->done, msecs_to_jiffies(500)); 738 if (!ret) { 739 dev_err(dev, "program ahb done timeout\n"); 740 nfi_writew(nfc, 0, NFI_INTR_EN); 741 ret = -ETIMEDOUT; 742 goto timeout; 743 } 744 745 ret = readl_poll_timeout_atomic(nfc->regs + NFI_ADDRCNTR, reg, 746 ADDRCNTR_SEC(reg) >= chip->ecc.steps, 747 10, MTK_TIMEOUT); 748 if (ret) 749 dev_err(dev, "hwecc write timeout\n"); 750 751 timeout: 752 753 dma_unmap_single(nfc->dev, addr, len, DMA_TO_DEVICE); 754 nfi_writel(nfc, 0, NFI_CON); 755 756 return ret; 757 } 758 759 static int mtk_nfc_write_page(struct mtd_info *mtd, struct nand_chip *chip, 760 const u8 *buf, int page, int raw) 761 { 762 struct mtk_nfc *nfc = nand_get_controller_data(chip); 763 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 764 size_t len; 765 const u8 *bufpoi; 766 u32 reg; 767 int ret; 768 769 nand_prog_page_begin_op(chip, page, 0, NULL, 0); 770 771 if (!raw) { 772 /* OOB => FDM: from register, ECC: from HW */ 773 reg = nfi_readw(nfc, NFI_CNFG) | CNFG_AUTO_FMT_EN; 774 nfi_writew(nfc, reg | CNFG_HW_ECC_EN, NFI_CNFG); 775 776 nfc->ecc_cfg.op = ECC_ENCODE; 777 nfc->ecc_cfg.mode = ECC_NFI_MODE; 778 ret = mtk_ecc_enable(nfc->ecc, &nfc->ecc_cfg); 779 if (ret) { 780 /* clear NFI config */ 781 reg = nfi_readw(nfc, NFI_CNFG); 782 reg &= ~(CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN); 783 nfi_writew(nfc, reg, NFI_CNFG); 784 785 return ret; 786 } 787 788 memcpy(nfc->buffer, buf, mtd->writesize); 789 mtk_nand->bad_mark.bm_swap(mtd, nfc->buffer, raw); 790 bufpoi = nfc->buffer; 791 792 /* write OOB into the FDM registers (OOB area in MTK NAND) */ 793 mtk_nfc_write_fdm(chip); 794 } else { 795 bufpoi = buf; 796 } 797 798 len = mtd->writesize + (raw ? mtd->oobsize : 0); 799 ret = mtk_nfc_do_write_page(mtd, chip, bufpoi, page, len); 800 801 if (!raw) 802 mtk_ecc_disable(nfc->ecc); 803 804 if (ret) 805 return ret; 806 807 return nand_prog_page_end_op(chip); 808 } 809 810 static int mtk_nfc_write_page_hwecc(struct mtd_info *mtd, 811 struct nand_chip *chip, const u8 *buf, 812 int oob_on, int page) 813 { 814 return mtk_nfc_write_page(mtd, chip, buf, page, 0); 815 } 816 817 static int mtk_nfc_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, 818 const u8 *buf, int oob_on, int pg) 819 { 820 struct mtk_nfc *nfc = nand_get_controller_data(chip); 821 822 mtk_nfc_format_page(mtd, buf); 823 return mtk_nfc_write_page(mtd, chip, nfc->buffer, pg, 1); 824 } 825 826 static int mtk_nfc_write_subpage_hwecc(struct mtd_info *mtd, 827 struct nand_chip *chip, u32 offset, 828 u32 data_len, const u8 *buf, 829 int oob_on, int page) 830 { 831 struct mtk_nfc *nfc = nand_get_controller_data(chip); 832 int ret; 833 834 ret = mtk_nfc_format_subpage(mtd, offset, data_len, buf); 835 if (ret < 0) 836 return ret; 837 838 /* use the data in the private buffer (now with FDM and CRC) */ 839 return mtk_nfc_write_page(mtd, chip, nfc->buffer, page, 1); 840 } 841 842 static int mtk_nfc_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, 843 int page) 844 { 845 return mtk_nfc_write_page_raw(mtd, chip, NULL, 1, page); 846 } 847 848 static int mtk_nfc_update_ecc_stats(struct mtd_info *mtd, u8 *buf, u32 sectors) 849 { 850 struct nand_chip *chip = mtd_to_nand(mtd); 851 struct mtk_nfc *nfc = nand_get_controller_data(chip); 852 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 853 struct mtk_ecc_stats stats; 854 int rc, i; 855 856 rc = nfi_readl(nfc, NFI_STA) & STA_EMP_PAGE; 857 if (rc) { 858 memset(buf, 0xff, sectors * chip->ecc.size); 859 for (i = 0; i < sectors; i++) 860 memset(oob_ptr(chip, i), 0xff, mtk_nand->fdm.reg_size); 861 return 0; 862 } 863 864 mtk_ecc_get_stats(nfc->ecc, &stats, sectors); 865 mtd->ecc_stats.corrected += stats.corrected; 866 mtd->ecc_stats.failed += stats.failed; 867 868 return stats.bitflips; 869 } 870 871 static int mtk_nfc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, 872 u32 data_offs, u32 readlen, 873 u8 *bufpoi, int page, int raw) 874 { 875 struct mtk_nfc *nfc = nand_get_controller_data(chip); 876 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 877 u32 spare = mtk_nand->spare_per_sector; 878 u32 column, sectors, start, end, reg; 879 dma_addr_t addr; 880 int bitflips; 881 size_t len; 882 u8 *buf; 883 int rc; 884 885 start = data_offs / chip->ecc.size; 886 end = DIV_ROUND_UP(data_offs + readlen, chip->ecc.size); 887 888 sectors = end - start; 889 column = start * (chip->ecc.size + spare); 890 891 len = sectors * chip->ecc.size + (raw ? sectors * spare : 0); 892 buf = bufpoi + start * chip->ecc.size; 893 894 nand_read_page_op(chip, page, column, NULL, 0); 895 896 addr = dma_map_single(nfc->dev, buf, len, DMA_FROM_DEVICE); 897 rc = dma_mapping_error(nfc->dev, addr); 898 if (rc) { 899 dev_err(nfc->dev, "dma mapping error\n"); 900 901 return -EINVAL; 902 } 903 904 reg = nfi_readw(nfc, NFI_CNFG); 905 reg |= CNFG_READ_EN | CNFG_DMA_BURST_EN | CNFG_AHB; 906 if (!raw) { 907 reg |= CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN; 908 nfi_writew(nfc, reg, NFI_CNFG); 909 910 nfc->ecc_cfg.mode = ECC_NFI_MODE; 911 nfc->ecc_cfg.sectors = sectors; 912 nfc->ecc_cfg.op = ECC_DECODE; 913 rc = mtk_ecc_enable(nfc->ecc, &nfc->ecc_cfg); 914 if (rc) { 915 dev_err(nfc->dev, "ecc enable\n"); 916 /* clear NFI_CNFG */ 917 reg &= ~(CNFG_DMA_BURST_EN | CNFG_AHB | CNFG_READ_EN | 918 CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN); 919 nfi_writew(nfc, reg, NFI_CNFG); 920 dma_unmap_single(nfc->dev, addr, len, DMA_FROM_DEVICE); 921 922 return rc; 923 } 924 } else { 925 nfi_writew(nfc, reg, NFI_CNFG); 926 } 927 928 nfi_writel(nfc, sectors << CON_SEC_SHIFT, NFI_CON); 929 nfi_writew(nfc, INTR_AHB_DONE_EN, NFI_INTR_EN); 930 nfi_writel(nfc, lower_32_bits(addr), NFI_STRADDR); 931 932 init_completion(&nfc->done); 933 reg = nfi_readl(nfc, NFI_CON) | CON_BRD; 934 nfi_writel(nfc, reg, NFI_CON); 935 nfi_writew(nfc, STAR_EN, NFI_STRDATA); 936 937 rc = wait_for_completion_timeout(&nfc->done, msecs_to_jiffies(500)); 938 if (!rc) 939 dev_warn(nfc->dev, "read ahb/dma done timeout\n"); 940 941 rc = readl_poll_timeout_atomic(nfc->regs + NFI_BYTELEN, reg, 942 ADDRCNTR_SEC(reg) >= sectors, 10, 943 MTK_TIMEOUT); 944 if (rc < 0) { 945 dev_err(nfc->dev, "subpage done timeout\n"); 946 bitflips = -EIO; 947 } else { 948 bitflips = 0; 949 if (!raw) { 950 rc = mtk_ecc_wait_done(nfc->ecc, ECC_DECODE); 951 bitflips = rc < 0 ? -ETIMEDOUT : 952 mtk_nfc_update_ecc_stats(mtd, buf, sectors); 953 mtk_nfc_read_fdm(chip, start, sectors); 954 } 955 } 956 957 dma_unmap_single(nfc->dev, addr, len, DMA_FROM_DEVICE); 958 959 if (raw) 960 goto done; 961 962 mtk_ecc_disable(nfc->ecc); 963 964 if (clamp(mtk_nand->bad_mark.sec, start, end) == mtk_nand->bad_mark.sec) 965 mtk_nand->bad_mark.bm_swap(mtd, bufpoi, raw); 966 done: 967 nfi_writel(nfc, 0, NFI_CON); 968 969 return bitflips; 970 } 971 972 static int mtk_nfc_read_subpage_hwecc(struct mtd_info *mtd, 973 struct nand_chip *chip, u32 off, 974 u32 len, u8 *p, int pg) 975 { 976 return mtk_nfc_read_subpage(mtd, chip, off, len, p, pg, 0); 977 } 978 979 static int mtk_nfc_read_page_hwecc(struct mtd_info *mtd, 980 struct nand_chip *chip, u8 *p, 981 int oob_on, int pg) 982 { 983 return mtk_nfc_read_subpage(mtd, chip, 0, mtd->writesize, p, pg, 0); 984 } 985 986 static int mtk_nfc_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, 987 u8 *buf, int oob_on, int page) 988 { 989 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 990 struct mtk_nfc *nfc = nand_get_controller_data(chip); 991 struct mtk_nfc_fdm *fdm = &mtk_nand->fdm; 992 int i, ret; 993 994 memset(nfc->buffer, 0xff, mtd->writesize + mtd->oobsize); 995 ret = mtk_nfc_read_subpage(mtd, chip, 0, mtd->writesize, nfc->buffer, 996 page, 1); 997 if (ret < 0) 998 return ret; 999 1000 for (i = 0; i < chip->ecc.steps; i++) { 1001 memcpy(oob_ptr(chip, i), mtk_oob_ptr(chip, i), fdm->reg_size); 1002 1003 if (i == mtk_nand->bad_mark.sec) 1004 mtk_nand->bad_mark.bm_swap(mtd, nfc->buffer, 1); 1005 1006 if (buf) 1007 memcpy(data_ptr(chip, buf, i), mtk_data_ptr(chip, i), 1008 chip->ecc.size); 1009 } 1010 1011 return ret; 1012 } 1013 1014 static int mtk_nfc_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, 1015 int page) 1016 { 1017 return mtk_nfc_read_page_raw(mtd, chip, NULL, 1, page); 1018 } 1019 1020 static inline void mtk_nfc_hw_init(struct mtk_nfc *nfc) 1021 { 1022 /* 1023 * CNRNB: nand ready/busy register 1024 * ------------------------------- 1025 * 7:4: timeout register for polling the NAND busy/ready signal 1026 * 0 : poll the status of the busy/ready signal after [7:4]*16 cycles. 1027 */ 1028 nfi_writew(nfc, 0xf1, NFI_CNRNB); 1029 nfi_writel(nfc, PAGEFMT_8K_16K, NFI_PAGEFMT); 1030 1031 mtk_nfc_hw_reset(nfc); 1032 1033 nfi_readl(nfc, NFI_INTR_STA); 1034 nfi_writel(nfc, 0, NFI_INTR_EN); 1035 } 1036 1037 static irqreturn_t mtk_nfc_irq(int irq, void *id) 1038 { 1039 struct mtk_nfc *nfc = id; 1040 u16 sta, ien; 1041 1042 sta = nfi_readw(nfc, NFI_INTR_STA); 1043 ien = nfi_readw(nfc, NFI_INTR_EN); 1044 1045 if (!(sta & ien)) 1046 return IRQ_NONE; 1047 1048 nfi_writew(nfc, ~sta & ien, NFI_INTR_EN); 1049 complete(&nfc->done); 1050 1051 return IRQ_HANDLED; 1052 } 1053 1054 static int mtk_nfc_enable_clk(struct device *dev, struct mtk_nfc_clk *clk) 1055 { 1056 int ret; 1057 1058 ret = clk_prepare_enable(clk->nfi_clk); 1059 if (ret) { 1060 dev_err(dev, "failed to enable nfi clk\n"); 1061 return ret; 1062 } 1063 1064 ret = clk_prepare_enable(clk->pad_clk); 1065 if (ret) { 1066 dev_err(dev, "failed to enable pad clk\n"); 1067 clk_disable_unprepare(clk->nfi_clk); 1068 return ret; 1069 } 1070 1071 return 0; 1072 } 1073 1074 static void mtk_nfc_disable_clk(struct mtk_nfc_clk *clk) 1075 { 1076 clk_disable_unprepare(clk->nfi_clk); 1077 clk_disable_unprepare(clk->pad_clk); 1078 } 1079 1080 static int mtk_nfc_ooblayout_free(struct mtd_info *mtd, int section, 1081 struct mtd_oob_region *oob_region) 1082 { 1083 struct nand_chip *chip = mtd_to_nand(mtd); 1084 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 1085 struct mtk_nfc_fdm *fdm = &mtk_nand->fdm; 1086 u32 eccsteps; 1087 1088 eccsteps = mtd->writesize / chip->ecc.size; 1089 1090 if (section >= eccsteps) 1091 return -ERANGE; 1092 1093 oob_region->length = fdm->reg_size - fdm->ecc_size; 1094 oob_region->offset = section * fdm->reg_size + fdm->ecc_size; 1095 1096 return 0; 1097 } 1098 1099 static int mtk_nfc_ooblayout_ecc(struct mtd_info *mtd, int section, 1100 struct mtd_oob_region *oob_region) 1101 { 1102 struct nand_chip *chip = mtd_to_nand(mtd); 1103 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 1104 u32 eccsteps; 1105 1106 if (section) 1107 return -ERANGE; 1108 1109 eccsteps = mtd->writesize / chip->ecc.size; 1110 oob_region->offset = mtk_nand->fdm.reg_size * eccsteps; 1111 oob_region->length = mtd->oobsize - oob_region->offset; 1112 1113 return 0; 1114 } 1115 1116 static const struct mtd_ooblayout_ops mtk_nfc_ooblayout_ops = { 1117 .free = mtk_nfc_ooblayout_free, 1118 .ecc = mtk_nfc_ooblayout_ecc, 1119 }; 1120 1121 static void mtk_nfc_set_fdm(struct mtk_nfc_fdm *fdm, struct mtd_info *mtd) 1122 { 1123 struct nand_chip *nand = mtd_to_nand(mtd); 1124 struct mtk_nfc_nand_chip *chip = to_mtk_nand(nand); 1125 struct mtk_nfc *nfc = nand_get_controller_data(nand); 1126 u32 ecc_bytes; 1127 1128 ecc_bytes = DIV_ROUND_UP(nand->ecc.strength * 1129 mtk_ecc_get_parity_bits(nfc->ecc), 8); 1130 1131 fdm->reg_size = chip->spare_per_sector - ecc_bytes; 1132 if (fdm->reg_size > NFI_FDM_MAX_SIZE) 1133 fdm->reg_size = NFI_FDM_MAX_SIZE; 1134 1135 /* bad block mark storage */ 1136 fdm->ecc_size = 1; 1137 } 1138 1139 static void mtk_nfc_set_bad_mark_ctl(struct mtk_nfc_bad_mark_ctl *bm_ctl, 1140 struct mtd_info *mtd) 1141 { 1142 struct nand_chip *nand = mtd_to_nand(mtd); 1143 1144 if (mtd->writesize == 512) { 1145 bm_ctl->bm_swap = mtk_nfc_no_bad_mark_swap; 1146 } else { 1147 bm_ctl->bm_swap = mtk_nfc_bad_mark_swap; 1148 bm_ctl->sec = mtd->writesize / mtk_data_len(nand); 1149 bm_ctl->pos = mtd->writesize % mtk_data_len(nand); 1150 } 1151 } 1152 1153 static int mtk_nfc_set_spare_per_sector(u32 *sps, struct mtd_info *mtd) 1154 { 1155 struct nand_chip *nand = mtd_to_nand(mtd); 1156 struct mtk_nfc *nfc = nand_get_controller_data(nand); 1157 const u8 *spare = nfc->caps->spare_size; 1158 u32 eccsteps, i, closest_spare = 0; 1159 1160 eccsteps = mtd->writesize / nand->ecc.size; 1161 *sps = mtd->oobsize / eccsteps; 1162 1163 if (nand->ecc.size == 1024) 1164 *sps >>= 1; 1165 1166 if (*sps < MTK_NFC_MIN_SPARE) 1167 return -EINVAL; 1168 1169 for (i = 0; i < nfc->caps->num_spare_size; i++) { 1170 if (*sps >= spare[i] && spare[i] >= spare[closest_spare]) { 1171 closest_spare = i; 1172 if (*sps == spare[i]) 1173 break; 1174 } 1175 } 1176 1177 *sps = spare[closest_spare]; 1178 1179 if (nand->ecc.size == 1024) 1180 *sps <<= 1; 1181 1182 return 0; 1183 } 1184 1185 static int mtk_nfc_ecc_init(struct device *dev, struct mtd_info *mtd) 1186 { 1187 struct nand_chip *nand = mtd_to_nand(mtd); 1188 struct mtk_nfc *nfc = nand_get_controller_data(nand); 1189 u32 spare; 1190 int free, ret; 1191 1192 /* support only ecc hw mode */ 1193 if (nand->ecc.mode != NAND_ECC_HW) { 1194 dev_err(dev, "ecc.mode not supported\n"); 1195 return -EINVAL; 1196 } 1197 1198 /* if optional dt settings not present */ 1199 if (!nand->ecc.size || !nand->ecc.strength) { 1200 /* use datasheet requirements */ 1201 nand->ecc.strength = nand->ecc_strength_ds; 1202 nand->ecc.size = nand->ecc_step_ds; 1203 1204 /* 1205 * align eccstrength and eccsize 1206 * this controller only supports 512 and 1024 sizes 1207 */ 1208 if (nand->ecc.size < 1024) { 1209 if (mtd->writesize > 512 && 1210 nfc->caps->max_sector_size > 512) { 1211 nand->ecc.size = 1024; 1212 nand->ecc.strength <<= 1; 1213 } else { 1214 nand->ecc.size = 512; 1215 } 1216 } else { 1217 nand->ecc.size = 1024; 1218 } 1219 1220 ret = mtk_nfc_set_spare_per_sector(&spare, mtd); 1221 if (ret) 1222 return ret; 1223 1224 /* calculate oob bytes except ecc parity data */ 1225 free = (nand->ecc.strength * mtk_ecc_get_parity_bits(nfc->ecc) 1226 + 7) >> 3; 1227 free = spare - free; 1228 1229 /* 1230 * enhance ecc strength if oob left is bigger than max FDM size 1231 * or reduce ecc strength if oob size is not enough for ecc 1232 * parity data. 1233 */ 1234 if (free > NFI_FDM_MAX_SIZE) { 1235 spare -= NFI_FDM_MAX_SIZE; 1236 nand->ecc.strength = (spare << 3) / 1237 mtk_ecc_get_parity_bits(nfc->ecc); 1238 } else if (free < 0) { 1239 spare -= NFI_FDM_MIN_SIZE; 1240 nand->ecc.strength = (spare << 3) / 1241 mtk_ecc_get_parity_bits(nfc->ecc); 1242 } 1243 } 1244 1245 mtk_ecc_adjust_strength(nfc->ecc, &nand->ecc.strength); 1246 1247 dev_info(dev, "eccsize %d eccstrength %d\n", 1248 nand->ecc.size, nand->ecc.strength); 1249 1250 return 0; 1251 } 1252 1253 static int mtk_nfc_attach_chip(struct nand_chip *chip) 1254 { 1255 struct mtd_info *mtd = nand_to_mtd(chip); 1256 struct device *dev = mtd->dev.parent; 1257 struct mtk_nfc *nfc = nand_get_controller_data(chip); 1258 struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); 1259 int len; 1260 int ret; 1261 1262 if (chip->options & NAND_BUSWIDTH_16) { 1263 dev_err(dev, "16bits buswidth not supported"); 1264 return -EINVAL; 1265 } 1266 1267 /* store bbt magic in page, cause OOB is not protected */ 1268 if (chip->bbt_options & NAND_BBT_USE_FLASH) 1269 chip->bbt_options |= NAND_BBT_NO_OOB; 1270 1271 ret = mtk_nfc_ecc_init(dev, mtd); 1272 if (ret) 1273 return ret; 1274 1275 ret = mtk_nfc_set_spare_per_sector(&mtk_nand->spare_per_sector, mtd); 1276 if (ret) 1277 return ret; 1278 1279 mtk_nfc_set_fdm(&mtk_nand->fdm, mtd); 1280 mtk_nfc_set_bad_mark_ctl(&mtk_nand->bad_mark, mtd); 1281 1282 len = mtd->writesize + mtd->oobsize; 1283 nfc->buffer = devm_kzalloc(dev, len, GFP_KERNEL); 1284 if (!nfc->buffer) 1285 return -ENOMEM; 1286 1287 return 0; 1288 } 1289 1290 static const struct nand_controller_ops mtk_nfc_controller_ops = { 1291 .attach_chip = mtk_nfc_attach_chip, 1292 }; 1293 1294 static int mtk_nfc_nand_chip_init(struct device *dev, struct mtk_nfc *nfc, 1295 struct device_node *np) 1296 { 1297 struct mtk_nfc_nand_chip *chip; 1298 struct nand_chip *nand; 1299 struct mtd_info *mtd; 1300 int nsels; 1301 u32 tmp; 1302 int ret; 1303 int i; 1304 1305 if (!of_get_property(np, "reg", &nsels)) 1306 return -ENODEV; 1307 1308 nsels /= sizeof(u32); 1309 if (!nsels || nsels > MTK_NAND_MAX_NSELS) { 1310 dev_err(dev, "invalid reg property size %d\n", nsels); 1311 return -EINVAL; 1312 } 1313 1314 chip = devm_kzalloc(dev, sizeof(*chip) + nsels * sizeof(u8), 1315 GFP_KERNEL); 1316 if (!chip) 1317 return -ENOMEM; 1318 1319 chip->nsels = nsels; 1320 for (i = 0; i < nsels; i++) { 1321 ret = of_property_read_u32_index(np, "reg", i, &tmp); 1322 if (ret) { 1323 dev_err(dev, "reg property failure : %d\n", ret); 1324 return ret; 1325 } 1326 chip->sels[i] = tmp; 1327 } 1328 1329 nand = &chip->nand; 1330 nand->controller = &nfc->controller; 1331 1332 nand_set_flash_node(nand, np); 1333 nand_set_controller_data(nand, nfc); 1334 1335 nand->options |= NAND_USE_BOUNCE_BUFFER | NAND_SUBPAGE_READ; 1336 nand->dev_ready = mtk_nfc_dev_ready; 1337 nand->select_chip = mtk_nfc_select_chip; 1338 nand->write_byte = mtk_nfc_write_byte; 1339 nand->write_buf = mtk_nfc_write_buf; 1340 nand->read_byte = mtk_nfc_read_byte; 1341 nand->read_buf = mtk_nfc_read_buf; 1342 nand->cmd_ctrl = mtk_nfc_cmd_ctrl; 1343 nand->setup_data_interface = mtk_nfc_setup_data_interface; 1344 1345 /* set default mode in case dt entry is missing */ 1346 nand->ecc.mode = NAND_ECC_HW; 1347 1348 nand->ecc.write_subpage = mtk_nfc_write_subpage_hwecc; 1349 nand->ecc.write_page_raw = mtk_nfc_write_page_raw; 1350 nand->ecc.write_page = mtk_nfc_write_page_hwecc; 1351 nand->ecc.write_oob_raw = mtk_nfc_write_oob_std; 1352 nand->ecc.write_oob = mtk_nfc_write_oob_std; 1353 1354 nand->ecc.read_subpage = mtk_nfc_read_subpage_hwecc; 1355 nand->ecc.read_page_raw = mtk_nfc_read_page_raw; 1356 nand->ecc.read_page = mtk_nfc_read_page_hwecc; 1357 nand->ecc.read_oob_raw = mtk_nfc_read_oob_std; 1358 nand->ecc.read_oob = mtk_nfc_read_oob_std; 1359 1360 mtd = nand_to_mtd(nand); 1361 mtd->owner = THIS_MODULE; 1362 mtd->dev.parent = dev; 1363 mtd->name = MTK_NAME; 1364 mtd_set_ooblayout(mtd, &mtk_nfc_ooblayout_ops); 1365 1366 mtk_nfc_hw_init(nfc); 1367 1368 ret = nand_scan(mtd, nsels); 1369 if (ret) 1370 return ret; 1371 1372 ret = mtd_device_register(mtd, NULL, 0); 1373 if (ret) { 1374 dev_err(dev, "mtd parse partition error\n"); 1375 nand_release(mtd); 1376 return ret; 1377 } 1378 1379 list_add_tail(&chip->node, &nfc->chips); 1380 1381 return 0; 1382 } 1383 1384 static int mtk_nfc_nand_chips_init(struct device *dev, struct mtk_nfc *nfc) 1385 { 1386 struct device_node *np = dev->of_node; 1387 struct device_node *nand_np; 1388 int ret; 1389 1390 for_each_child_of_node(np, nand_np) { 1391 ret = mtk_nfc_nand_chip_init(dev, nfc, nand_np); 1392 if (ret) { 1393 of_node_put(nand_np); 1394 return ret; 1395 } 1396 } 1397 1398 return 0; 1399 } 1400 1401 static const struct mtk_nfc_caps mtk_nfc_caps_mt2701 = { 1402 .spare_size = spare_size_mt2701, 1403 .num_spare_size = 16, 1404 .pageformat_spare_shift = 4, 1405 .nfi_clk_div = 1, 1406 .max_sector = 16, 1407 .max_sector_size = 1024, 1408 }; 1409 1410 static const struct mtk_nfc_caps mtk_nfc_caps_mt2712 = { 1411 .spare_size = spare_size_mt2712, 1412 .num_spare_size = 19, 1413 .pageformat_spare_shift = 16, 1414 .nfi_clk_div = 2, 1415 .max_sector = 16, 1416 .max_sector_size = 1024, 1417 }; 1418 1419 static const struct mtk_nfc_caps mtk_nfc_caps_mt7622 = { 1420 .spare_size = spare_size_mt7622, 1421 .num_spare_size = 4, 1422 .pageformat_spare_shift = 4, 1423 .nfi_clk_div = 1, 1424 .max_sector = 8, 1425 .max_sector_size = 512, 1426 }; 1427 1428 static const struct of_device_id mtk_nfc_id_table[] = { 1429 { 1430 .compatible = "mediatek,mt2701-nfc", 1431 .data = &mtk_nfc_caps_mt2701, 1432 }, { 1433 .compatible = "mediatek,mt2712-nfc", 1434 .data = &mtk_nfc_caps_mt2712, 1435 }, { 1436 .compatible = "mediatek,mt7622-nfc", 1437 .data = &mtk_nfc_caps_mt7622, 1438 }, 1439 {} 1440 }; 1441 MODULE_DEVICE_TABLE(of, mtk_nfc_id_table); 1442 1443 static int mtk_nfc_probe(struct platform_device *pdev) 1444 { 1445 struct device *dev = &pdev->dev; 1446 struct device_node *np = dev->of_node; 1447 struct mtk_nfc *nfc; 1448 struct resource *res; 1449 int ret, irq; 1450 1451 nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL); 1452 if (!nfc) 1453 return -ENOMEM; 1454 1455 spin_lock_init(&nfc->controller.lock); 1456 init_waitqueue_head(&nfc->controller.wq); 1457 INIT_LIST_HEAD(&nfc->chips); 1458 nfc->controller.ops = &mtk_nfc_controller_ops; 1459 1460 /* probe defer if not ready */ 1461 nfc->ecc = of_mtk_ecc_get(np); 1462 if (IS_ERR(nfc->ecc)) 1463 return PTR_ERR(nfc->ecc); 1464 else if (!nfc->ecc) 1465 return -ENODEV; 1466 1467 nfc->caps = of_device_get_match_data(dev); 1468 nfc->dev = dev; 1469 1470 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1471 nfc->regs = devm_ioremap_resource(dev, res); 1472 if (IS_ERR(nfc->regs)) { 1473 ret = PTR_ERR(nfc->regs); 1474 goto release_ecc; 1475 } 1476 1477 nfc->clk.nfi_clk = devm_clk_get(dev, "nfi_clk"); 1478 if (IS_ERR(nfc->clk.nfi_clk)) { 1479 dev_err(dev, "no clk\n"); 1480 ret = PTR_ERR(nfc->clk.nfi_clk); 1481 goto release_ecc; 1482 } 1483 1484 nfc->clk.pad_clk = devm_clk_get(dev, "pad_clk"); 1485 if (IS_ERR(nfc->clk.pad_clk)) { 1486 dev_err(dev, "no pad clk\n"); 1487 ret = PTR_ERR(nfc->clk.pad_clk); 1488 goto release_ecc; 1489 } 1490 1491 ret = mtk_nfc_enable_clk(dev, &nfc->clk); 1492 if (ret) 1493 goto release_ecc; 1494 1495 irq = platform_get_irq(pdev, 0); 1496 if (irq < 0) { 1497 dev_err(dev, "no nfi irq resource\n"); 1498 ret = -EINVAL; 1499 goto clk_disable; 1500 } 1501 1502 ret = devm_request_irq(dev, irq, mtk_nfc_irq, 0x0, "mtk-nand", nfc); 1503 if (ret) { 1504 dev_err(dev, "failed to request nfi irq\n"); 1505 goto clk_disable; 1506 } 1507 1508 ret = dma_set_mask(dev, DMA_BIT_MASK(32)); 1509 if (ret) { 1510 dev_err(dev, "failed to set dma mask\n"); 1511 goto clk_disable; 1512 } 1513 1514 platform_set_drvdata(pdev, nfc); 1515 1516 ret = mtk_nfc_nand_chips_init(dev, nfc); 1517 if (ret) { 1518 dev_err(dev, "failed to init nand chips\n"); 1519 goto clk_disable; 1520 } 1521 1522 return 0; 1523 1524 clk_disable: 1525 mtk_nfc_disable_clk(&nfc->clk); 1526 1527 release_ecc: 1528 mtk_ecc_release(nfc->ecc); 1529 1530 return ret; 1531 } 1532 1533 static int mtk_nfc_remove(struct platform_device *pdev) 1534 { 1535 struct mtk_nfc *nfc = platform_get_drvdata(pdev); 1536 struct mtk_nfc_nand_chip *chip; 1537 1538 while (!list_empty(&nfc->chips)) { 1539 chip = list_first_entry(&nfc->chips, struct mtk_nfc_nand_chip, 1540 node); 1541 nand_release(nand_to_mtd(&chip->nand)); 1542 list_del(&chip->node); 1543 } 1544 1545 mtk_ecc_release(nfc->ecc); 1546 mtk_nfc_disable_clk(&nfc->clk); 1547 1548 return 0; 1549 } 1550 1551 #ifdef CONFIG_PM_SLEEP 1552 static int mtk_nfc_suspend(struct device *dev) 1553 { 1554 struct mtk_nfc *nfc = dev_get_drvdata(dev); 1555 1556 mtk_nfc_disable_clk(&nfc->clk); 1557 1558 return 0; 1559 } 1560 1561 static int mtk_nfc_resume(struct device *dev) 1562 { 1563 struct mtk_nfc *nfc = dev_get_drvdata(dev); 1564 struct mtk_nfc_nand_chip *chip; 1565 struct nand_chip *nand; 1566 int ret; 1567 u32 i; 1568 1569 udelay(200); 1570 1571 ret = mtk_nfc_enable_clk(dev, &nfc->clk); 1572 if (ret) 1573 return ret; 1574 1575 /* reset NAND chip if VCC was powered off */ 1576 list_for_each_entry(chip, &nfc->chips, node) { 1577 nand = &chip->nand; 1578 for (i = 0; i < chip->nsels; i++) 1579 nand_reset(nand, i); 1580 } 1581 1582 return 0; 1583 } 1584 1585 static SIMPLE_DEV_PM_OPS(mtk_nfc_pm_ops, mtk_nfc_suspend, mtk_nfc_resume); 1586 #endif 1587 1588 static struct platform_driver mtk_nfc_driver = { 1589 .probe = mtk_nfc_probe, 1590 .remove = mtk_nfc_remove, 1591 .driver = { 1592 .name = MTK_NAME, 1593 .of_match_table = mtk_nfc_id_table, 1594 #ifdef CONFIG_PM_SLEEP 1595 .pm = &mtk_nfc_pm_ops, 1596 #endif 1597 }, 1598 }; 1599 1600 module_platform_driver(mtk_nfc_driver); 1601 1602 MODULE_LICENSE("GPL"); 1603 MODULE_AUTHOR("Xiaolei Li <xiaolei.li@mediatek.com>"); 1604 MODULE_DESCRIPTION("MTK Nand Flash Controller Driver"); 1605