1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Marvell NAND flash controller driver 4 * 5 * Copyright (C) 2017 Marvell 6 * Author: Miquel RAYNAL <miquel.raynal@free-electrons.com> 7 * 8 */ 9 10 #include <linux/module.h> 11 #include <linux/clk.h> 12 #include <linux/mtd/rawnand.h> 13 #include <linux/of_platform.h> 14 #include <linux/iopoll.h> 15 #include <linux/interrupt.h> 16 #include <linux/slab.h> 17 #include <linux/mfd/syscon.h> 18 #include <linux/regmap.h> 19 #include <asm/unaligned.h> 20 21 #include <linux/dmaengine.h> 22 #include <linux/dma-mapping.h> 23 #include <linux/dma/pxa-dma.h> 24 #include <linux/platform_data/mtd-nand-pxa3xx.h> 25 26 /* Data FIFO granularity, FIFO reads/writes must be a multiple of this length */ 27 #define FIFO_DEPTH 8 28 #define FIFO_REP(x) (x / sizeof(u32)) 29 #define BCH_SEQ_READS (32 / FIFO_DEPTH) 30 /* NFC does not support transfers of larger chunks at a time */ 31 #define MAX_CHUNK_SIZE 2112 32 /* NFCv1 cannot read more that 7 bytes of ID */ 33 #define NFCV1_READID_LEN 7 34 /* Polling is done at a pace of POLL_PERIOD us until POLL_TIMEOUT is reached */ 35 #define POLL_PERIOD 0 36 #define POLL_TIMEOUT 100000 37 /* Interrupt maximum wait period in ms */ 38 #define IRQ_TIMEOUT 1000 39 /* Latency in clock cycles between SoC pins and NFC logic */ 40 #define MIN_RD_DEL_CNT 3 41 /* Maximum number of contiguous address cycles */ 42 #define MAX_ADDRESS_CYC_NFCV1 5 43 #define MAX_ADDRESS_CYC_NFCV2 7 44 /* System control registers/bits to enable the NAND controller on some SoCs */ 45 #define GENCONF_SOC_DEVICE_MUX 0x208 46 #define GENCONF_SOC_DEVICE_MUX_NFC_EN BIT(0) 47 #define GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST BIT(20) 48 #define GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST BIT(21) 49 #define GENCONF_SOC_DEVICE_MUX_NFC_INT_EN BIT(25) 50 #define GENCONF_CLK_GATING_CTRL 0x220 51 #define GENCONF_CLK_GATING_CTRL_ND_GATE BIT(2) 52 #define GENCONF_ND_CLK_CTRL 0x700 53 #define GENCONF_ND_CLK_CTRL_EN BIT(0) 54 55 /* NAND controller data flash control register */ 56 #define NDCR 0x00 57 #define NDCR_ALL_INT GENMASK(11, 0) 58 #define NDCR_CS1_CMDDM BIT(7) 59 #define NDCR_CS0_CMDDM BIT(8) 60 #define NDCR_RDYM BIT(11) 61 #define NDCR_ND_ARB_EN BIT(12) 62 #define NDCR_RA_START BIT(15) 63 #define NDCR_RD_ID_CNT(x) (min_t(unsigned int, x, 0x7) << 16) 64 #define NDCR_PAGE_SZ(x) (x >= 2048 ? BIT(24) : 0) 65 #define NDCR_DWIDTH_M BIT(26) 66 #define NDCR_DWIDTH_C BIT(27) 67 #define NDCR_ND_RUN BIT(28) 68 #define NDCR_DMA_EN BIT(29) 69 #define NDCR_ECC_EN BIT(30) 70 #define NDCR_SPARE_EN BIT(31) 71 #define NDCR_GENERIC_FIELDS_MASK (~(NDCR_RA_START | NDCR_PAGE_SZ(2048) | \ 72 NDCR_DWIDTH_M | NDCR_DWIDTH_C)) 73 74 /* NAND interface timing parameter 0 register */ 75 #define NDTR0 0x04 76 #define NDTR0_TRP(x) ((min_t(unsigned int, x, 0xF) & 0x7) << 0) 77 #define NDTR0_TRH(x) (min_t(unsigned int, x, 0x7) << 3) 78 #define NDTR0_ETRP(x) ((min_t(unsigned int, x, 0xF) & 0x8) << 3) 79 #define NDTR0_SEL_NRE_EDGE BIT(7) 80 #define NDTR0_TWP(x) (min_t(unsigned int, x, 0x7) << 8) 81 #define NDTR0_TWH(x) (min_t(unsigned int, x, 0x7) << 11) 82 #define NDTR0_TCS(x) (min_t(unsigned int, x, 0x7) << 16) 83 #define NDTR0_TCH(x) (min_t(unsigned int, x, 0x7) << 19) 84 #define NDTR0_RD_CNT_DEL(x) (min_t(unsigned int, x, 0xF) << 22) 85 #define NDTR0_SELCNTR BIT(26) 86 #define NDTR0_TADL(x) (min_t(unsigned int, x, 0x1F) << 27) 87 88 /* NAND interface timing parameter 1 register */ 89 #define NDTR1 0x0C 90 #define NDTR1_TAR(x) (min_t(unsigned int, x, 0xF) << 0) 91 #define NDTR1_TWHR(x) (min_t(unsigned int, x, 0xF) << 4) 92 #define NDTR1_TRHW(x) (min_t(unsigned int, x / 16, 0x3) << 8) 93 #define NDTR1_PRESCALE BIT(14) 94 #define NDTR1_WAIT_MODE BIT(15) 95 #define NDTR1_TR(x) (min_t(unsigned int, x, 0xFFFF) << 16) 96 97 /* NAND controller status register */ 98 #define NDSR 0x14 99 #define NDSR_WRCMDREQ BIT(0) 100 #define NDSR_RDDREQ BIT(1) 101 #define NDSR_WRDREQ BIT(2) 102 #define NDSR_CORERR BIT(3) 103 #define NDSR_UNCERR BIT(4) 104 #define NDSR_CMDD(cs) BIT(8 - cs) 105 #define NDSR_RDY(rb) BIT(11 + rb) 106 #define NDSR_ERRCNT(x) ((x >> 16) & 0x1F) 107 108 /* NAND ECC control register */ 109 #define NDECCCTRL 0x28 110 #define NDECCCTRL_BCH_EN BIT(0) 111 112 /* NAND controller data buffer register */ 113 #define NDDB 0x40 114 115 /* NAND controller command buffer 0 register */ 116 #define NDCB0 0x48 117 #define NDCB0_CMD1(x) ((x & 0xFF) << 0) 118 #define NDCB0_CMD2(x) ((x & 0xFF) << 8) 119 #define NDCB0_ADDR_CYC(x) ((x & 0x7) << 16) 120 #define NDCB0_ADDR_GET_NUM_CYC(x) (((x) >> 16) & 0x7) 121 #define NDCB0_DBC BIT(19) 122 #define NDCB0_CMD_TYPE(x) ((x & 0x7) << 21) 123 #define NDCB0_CSEL BIT(24) 124 #define NDCB0_RDY_BYP BIT(27) 125 #define NDCB0_LEN_OVRD BIT(28) 126 #define NDCB0_CMD_XTYPE(x) ((x & 0x7) << 29) 127 128 /* NAND controller command buffer 1 register */ 129 #define NDCB1 0x4C 130 #define NDCB1_COLS(x) ((x & 0xFFFF) << 0) 131 #define NDCB1_ADDRS_PAGE(x) (x << 16) 132 133 /* NAND controller command buffer 2 register */ 134 #define NDCB2 0x50 135 #define NDCB2_ADDR5_PAGE(x) (((x >> 16) & 0xFF) << 0) 136 #define NDCB2_ADDR5_CYC(x) ((x & 0xFF) << 0) 137 138 /* NAND controller command buffer 3 register */ 139 #define NDCB3 0x54 140 #define NDCB3_ADDR6_CYC(x) ((x & 0xFF) << 16) 141 #define NDCB3_ADDR7_CYC(x) ((x & 0xFF) << 24) 142 143 /* NAND controller command buffer 0 register 'type' and 'xtype' fields */ 144 #define TYPE_READ 0 145 #define TYPE_WRITE 1 146 #define TYPE_ERASE 2 147 #define TYPE_READ_ID 3 148 #define TYPE_STATUS 4 149 #define TYPE_RESET 5 150 #define TYPE_NAKED_CMD 6 151 #define TYPE_NAKED_ADDR 7 152 #define TYPE_MASK 7 153 #define XTYPE_MONOLITHIC_RW 0 154 #define XTYPE_LAST_NAKED_RW 1 155 #define XTYPE_FINAL_COMMAND 3 156 #define XTYPE_READ 4 157 #define XTYPE_WRITE_DISPATCH 4 158 #define XTYPE_NAKED_RW 5 159 #define XTYPE_COMMAND_DISPATCH 6 160 #define XTYPE_MASK 7 161 162 /** 163 * Marvell ECC engine works differently than the others, in order to limit the 164 * size of the IP, hardware engineers chose to set a fixed strength at 16 bits 165 * per subpage, and depending on a the desired strength needed by the NAND chip, 166 * a particular layout mixing data/spare/ecc is defined, with a possible last 167 * chunk smaller that the others. 168 * 169 * @writesize: Full page size on which the layout applies 170 * @chunk: Desired ECC chunk size on which the layout applies 171 * @strength: Desired ECC strength (per chunk size bytes) on which the 172 * layout applies 173 * @nchunks: Total number of chunks 174 * @full_chunk_cnt: Number of full-sized chunks, which is the number of 175 * repetitions of the pattern: 176 * (data_bytes + spare_bytes + ecc_bytes). 177 * @data_bytes: Number of data bytes per chunk 178 * @spare_bytes: Number of spare bytes per chunk 179 * @ecc_bytes: Number of ecc bytes per chunk 180 * @last_data_bytes: Number of data bytes in the last chunk 181 * @last_spare_bytes: Number of spare bytes in the last chunk 182 * @last_ecc_bytes: Number of ecc bytes in the last chunk 183 */ 184 struct marvell_hw_ecc_layout { 185 /* Constraints */ 186 int writesize; 187 int chunk; 188 int strength; 189 /* Corresponding layout */ 190 int nchunks; 191 int full_chunk_cnt; 192 int data_bytes; 193 int spare_bytes; 194 int ecc_bytes; 195 int last_data_bytes; 196 int last_spare_bytes; 197 int last_ecc_bytes; 198 }; 199 200 #define MARVELL_LAYOUT(ws, dc, ds, nc, fcc, db, sb, eb, ldb, lsb, leb) \ 201 { \ 202 .writesize = ws, \ 203 .chunk = dc, \ 204 .strength = ds, \ 205 .nchunks = nc, \ 206 .full_chunk_cnt = fcc, \ 207 .data_bytes = db, \ 208 .spare_bytes = sb, \ 209 .ecc_bytes = eb, \ 210 .last_data_bytes = ldb, \ 211 .last_spare_bytes = lsb, \ 212 .last_ecc_bytes = leb, \ 213 } 214 215 /* Layouts explained in AN-379_Marvell_SoC_NFC_ECC */ 216 static const struct marvell_hw_ecc_layout marvell_nfc_layouts[] = { 217 MARVELL_LAYOUT( 512, 512, 1, 1, 1, 512, 8, 8, 0, 0, 0), 218 MARVELL_LAYOUT( 2048, 512, 1, 1, 1, 2048, 40, 24, 0, 0, 0), 219 MARVELL_LAYOUT( 2048, 512, 4, 1, 1, 2048, 32, 30, 0, 0, 0), 220 MARVELL_LAYOUT( 4096, 512, 4, 2, 2, 2048, 32, 30, 0, 0, 0), 221 MARVELL_LAYOUT( 4096, 512, 8, 5, 4, 1024, 0, 30, 0, 64, 30), 222 }; 223 224 /** 225 * The Nand Flash Controller has up to 4 CE and 2 RB pins. The CE selection 226 * is made by a field in NDCB0 register, and in another field in NDCB2 register. 227 * The datasheet describes the logic with an error: ADDR5 field is once 228 * declared at the beginning of NDCB2, and another time at its end. Because the 229 * ADDR5 field of NDCB2 may be used by other bytes, it would be more logical 230 * to use the last bit of this field instead of the first ones. 231 * 232 * @cs: Wanted CE lane. 233 * @ndcb0_csel: Value of the NDCB0 register with or without the flag 234 * selecting the wanted CE lane. This is set once when 235 * the Device Tree is probed. 236 * @rb: Ready/Busy pin for the flash chip 237 */ 238 struct marvell_nand_chip_sel { 239 unsigned int cs; 240 u32 ndcb0_csel; 241 unsigned int rb; 242 }; 243 244 /** 245 * NAND chip structure: stores NAND chip device related information 246 * 247 * @chip: Base NAND chip structure 248 * @node: Used to store NAND chips into a list 249 * @layout NAND layout when using hardware ECC 250 * @ndcr: Controller register value for this NAND chip 251 * @ndtr0: Timing registers 0 value for this NAND chip 252 * @ndtr1: Timing registers 1 value for this NAND chip 253 * @selected_die: Current active CS 254 * @nsels: Number of CS lines required by the NAND chip 255 * @sels: Array of CS lines descriptions 256 */ 257 struct marvell_nand_chip { 258 struct nand_chip chip; 259 struct list_head node; 260 const struct marvell_hw_ecc_layout *layout; 261 u32 ndcr; 262 u32 ndtr0; 263 u32 ndtr1; 264 int addr_cyc; 265 int selected_die; 266 unsigned int nsels; 267 struct marvell_nand_chip_sel sels[0]; 268 }; 269 270 static inline struct marvell_nand_chip *to_marvell_nand(struct nand_chip *chip) 271 { 272 return container_of(chip, struct marvell_nand_chip, chip); 273 } 274 275 static inline struct marvell_nand_chip_sel *to_nand_sel(struct marvell_nand_chip 276 *nand) 277 { 278 return &nand->sels[nand->selected_die]; 279 } 280 281 /** 282 * NAND controller capabilities for distinction between compatible strings 283 * 284 * @max_cs_nb: Number of Chip Select lines available 285 * @max_rb_nb: Number of Ready/Busy lines available 286 * @need_system_controller: Indicates if the SoC needs to have access to the 287 * system controller (ie. to enable the NAND controller) 288 * @legacy_of_bindings: Indicates if DT parsing must be done using the old 289 * fashion way 290 * @is_nfcv2: NFCv2 has numerous enhancements compared to NFCv1, ie. 291 * BCH error detection and correction algorithm, 292 * NDCB3 register has been added 293 * @use_dma: Use dma for data transfers 294 */ 295 struct marvell_nfc_caps { 296 unsigned int max_cs_nb; 297 unsigned int max_rb_nb; 298 bool need_system_controller; 299 bool legacy_of_bindings; 300 bool is_nfcv2; 301 bool use_dma; 302 }; 303 304 /** 305 * NAND controller structure: stores Marvell NAND controller information 306 * 307 * @controller: Base controller structure 308 * @dev: Parent device (used to print error messages) 309 * @regs: NAND controller registers 310 * @core_clk: Core clock 311 * @reg_clk: Regiters clock 312 * @complete: Completion object to wait for NAND controller events 313 * @assigned_cs: Bitmask describing already assigned CS lines 314 * @chips: List containing all the NAND chips attached to 315 * this NAND controller 316 * @caps: NAND controller capabilities for each compatible string 317 * @dma_chan: DMA channel (NFCv1 only) 318 * @dma_buf: 32-bit aligned buffer for DMA transfers (NFCv1 only) 319 */ 320 struct marvell_nfc { 321 struct nand_controller controller; 322 struct device *dev; 323 void __iomem *regs; 324 struct clk *core_clk; 325 struct clk *reg_clk; 326 struct completion complete; 327 unsigned long assigned_cs; 328 struct list_head chips; 329 struct nand_chip *selected_chip; 330 const struct marvell_nfc_caps *caps; 331 332 /* DMA (NFCv1 only) */ 333 bool use_dma; 334 struct dma_chan *dma_chan; 335 u8 *dma_buf; 336 }; 337 338 static inline struct marvell_nfc *to_marvell_nfc(struct nand_controller *ctrl) 339 { 340 return container_of(ctrl, struct marvell_nfc, controller); 341 } 342 343 /** 344 * NAND controller timings expressed in NAND Controller clock cycles 345 * 346 * @tRP: ND_nRE pulse width 347 * @tRH: ND_nRE high duration 348 * @tWP: ND_nWE pulse time 349 * @tWH: ND_nWE high duration 350 * @tCS: Enable signal setup time 351 * @tCH: Enable signal hold time 352 * @tADL: Address to write data delay 353 * @tAR: ND_ALE low to ND_nRE low delay 354 * @tWHR: ND_nWE high to ND_nRE low for status read 355 * @tRHW: ND_nRE high duration, read to write delay 356 * @tR: ND_nWE high to ND_nRE low for read 357 */ 358 struct marvell_nfc_timings { 359 /* NDTR0 fields */ 360 unsigned int tRP; 361 unsigned int tRH; 362 unsigned int tWP; 363 unsigned int tWH; 364 unsigned int tCS; 365 unsigned int tCH; 366 unsigned int tADL; 367 /* NDTR1 fields */ 368 unsigned int tAR; 369 unsigned int tWHR; 370 unsigned int tRHW; 371 unsigned int tR; 372 }; 373 374 /** 375 * Derives a duration in numbers of clock cycles. 376 * 377 * @ps: Duration in pico-seconds 378 * @period_ns: Clock period in nano-seconds 379 * 380 * Convert the duration in nano-seconds, then divide by the period and 381 * return the number of clock periods. 382 */ 383 #define TO_CYCLES(ps, period_ns) (DIV_ROUND_UP(ps / 1000, period_ns)) 384 #define TO_CYCLES64(ps, period_ns) (DIV_ROUND_UP_ULL(div_u64(ps, 1000), \ 385 period_ns)) 386 387 /** 388 * NAND driver structure filled during the parsing of the ->exec_op() subop 389 * subset of instructions. 390 * 391 * @ndcb: Array of values written to NDCBx registers 392 * @cle_ale_delay_ns: Optional delay after the last CMD or ADDR cycle 393 * @rdy_timeout_ms: Timeout for waits on Ready/Busy pin 394 * @rdy_delay_ns: Optional delay after waiting for the RB pin 395 * @data_delay_ns: Optional delay after the data xfer 396 * @data_instr_idx: Index of the data instruction in the subop 397 * @data_instr: Pointer to the data instruction in the subop 398 */ 399 struct marvell_nfc_op { 400 u32 ndcb[4]; 401 unsigned int cle_ale_delay_ns; 402 unsigned int rdy_timeout_ms; 403 unsigned int rdy_delay_ns; 404 unsigned int data_delay_ns; 405 unsigned int data_instr_idx; 406 const struct nand_op_instr *data_instr; 407 }; 408 409 /* 410 * Internal helper to conditionnally apply a delay (from the above structure, 411 * most of the time). 412 */ 413 static void cond_delay(unsigned int ns) 414 { 415 if (!ns) 416 return; 417 418 if (ns < 10000) 419 ndelay(ns); 420 else 421 udelay(DIV_ROUND_UP(ns, 1000)); 422 } 423 424 /* 425 * The controller has many flags that could generate interrupts, most of them 426 * are disabled and polling is used. For the very slow signals, using interrupts 427 * may relax the CPU charge. 428 */ 429 static void marvell_nfc_disable_int(struct marvell_nfc *nfc, u32 int_mask) 430 { 431 u32 reg; 432 433 /* Writing 1 disables the interrupt */ 434 reg = readl_relaxed(nfc->regs + NDCR); 435 writel_relaxed(reg | int_mask, nfc->regs + NDCR); 436 } 437 438 static void marvell_nfc_enable_int(struct marvell_nfc *nfc, u32 int_mask) 439 { 440 u32 reg; 441 442 /* Writing 0 enables the interrupt */ 443 reg = readl_relaxed(nfc->regs + NDCR); 444 writel_relaxed(reg & ~int_mask, nfc->regs + NDCR); 445 } 446 447 static void marvell_nfc_clear_int(struct marvell_nfc *nfc, u32 int_mask) 448 { 449 writel_relaxed(int_mask, nfc->regs + NDSR); 450 } 451 452 static void marvell_nfc_force_byte_access(struct nand_chip *chip, 453 bool force_8bit) 454 { 455 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 456 u32 ndcr; 457 458 /* 459 * Callers of this function do not verify if the NAND is using a 16-bit 460 * an 8-bit bus for normal operations, so we need to take care of that 461 * here by leaving the configuration unchanged if the NAND does not have 462 * the NAND_BUSWIDTH_16 flag set. 463 */ 464 if (!(chip->options & NAND_BUSWIDTH_16)) 465 return; 466 467 ndcr = readl_relaxed(nfc->regs + NDCR); 468 469 if (force_8bit) 470 ndcr &= ~(NDCR_DWIDTH_M | NDCR_DWIDTH_C); 471 else 472 ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C; 473 474 writel_relaxed(ndcr, nfc->regs + NDCR); 475 } 476 477 static int marvell_nfc_wait_ndrun(struct nand_chip *chip) 478 { 479 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 480 u32 val; 481 int ret; 482 483 /* 484 * The command is being processed, wait for the ND_RUN bit to be 485 * cleared by the NFC. If not, we must clear it by hand. 486 */ 487 ret = readl_relaxed_poll_timeout(nfc->regs + NDCR, val, 488 (val & NDCR_ND_RUN) == 0, 489 POLL_PERIOD, POLL_TIMEOUT); 490 if (ret) { 491 dev_err(nfc->dev, "Timeout on NAND controller run mode\n"); 492 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN, 493 nfc->regs + NDCR); 494 return ret; 495 } 496 497 return 0; 498 } 499 500 /* 501 * Any time a command has to be sent to the controller, the following sequence 502 * has to be followed: 503 * - call marvell_nfc_prepare_cmd() 504 * -> activate the ND_RUN bit that will kind of 'start a job' 505 * -> wait the signal indicating the NFC is waiting for a command 506 * - send the command (cmd and address cycles) 507 * - enventually send or receive the data 508 * - call marvell_nfc_end_cmd() with the corresponding flag 509 * -> wait the flag to be triggered or cancel the job with a timeout 510 * 511 * The following helpers are here to factorize the code a bit so that 512 * specialized functions responsible for executing the actual NAND 513 * operations do not have to replicate the same code blocks. 514 */ 515 static int marvell_nfc_prepare_cmd(struct nand_chip *chip) 516 { 517 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 518 u32 ndcr, val; 519 int ret; 520 521 /* Poll ND_RUN and clear NDSR before issuing any command */ 522 ret = marvell_nfc_wait_ndrun(chip); 523 if (ret) { 524 dev_err(nfc->dev, "Last operation did not succeed\n"); 525 return ret; 526 } 527 528 ndcr = readl_relaxed(nfc->regs + NDCR); 529 writel_relaxed(readl(nfc->regs + NDSR), nfc->regs + NDSR); 530 531 /* Assert ND_RUN bit and wait the NFC to be ready */ 532 writel_relaxed(ndcr | NDCR_ND_RUN, nfc->regs + NDCR); 533 ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val, 534 val & NDSR_WRCMDREQ, 535 POLL_PERIOD, POLL_TIMEOUT); 536 if (ret) { 537 dev_err(nfc->dev, "Timeout on WRCMDRE\n"); 538 return -ETIMEDOUT; 539 } 540 541 /* Command may be written, clear WRCMDREQ status bit */ 542 writel_relaxed(NDSR_WRCMDREQ, nfc->regs + NDSR); 543 544 return 0; 545 } 546 547 static void marvell_nfc_send_cmd(struct nand_chip *chip, 548 struct marvell_nfc_op *nfc_op) 549 { 550 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip); 551 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 552 553 dev_dbg(nfc->dev, "\nNDCR: 0x%08x\n" 554 "NDCB0: 0x%08x\nNDCB1: 0x%08x\nNDCB2: 0x%08x\nNDCB3: 0x%08x\n", 555 (u32)readl_relaxed(nfc->regs + NDCR), nfc_op->ndcb[0], 556 nfc_op->ndcb[1], nfc_op->ndcb[2], nfc_op->ndcb[3]); 557 558 writel_relaxed(to_nand_sel(marvell_nand)->ndcb0_csel | nfc_op->ndcb[0], 559 nfc->regs + NDCB0); 560 writel_relaxed(nfc_op->ndcb[1], nfc->regs + NDCB0); 561 writel(nfc_op->ndcb[2], nfc->regs + NDCB0); 562 563 /* 564 * Write NDCB0 four times only if LEN_OVRD is set or if ADDR6 or ADDR7 565 * fields are used (only available on NFCv2). 566 */ 567 if (nfc_op->ndcb[0] & NDCB0_LEN_OVRD || 568 NDCB0_ADDR_GET_NUM_CYC(nfc_op->ndcb[0]) >= 6) { 569 if (!WARN_ON_ONCE(!nfc->caps->is_nfcv2)) 570 writel(nfc_op->ndcb[3], nfc->regs + NDCB0); 571 } 572 } 573 574 static int marvell_nfc_end_cmd(struct nand_chip *chip, int flag, 575 const char *label) 576 { 577 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 578 u32 val; 579 int ret; 580 581 ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val, 582 val & flag, 583 POLL_PERIOD, POLL_TIMEOUT); 584 585 if (ret) { 586 dev_err(nfc->dev, "Timeout on %s (NDSR: 0x%08x)\n", 587 label, val); 588 if (nfc->dma_chan) 589 dmaengine_terminate_all(nfc->dma_chan); 590 return ret; 591 } 592 593 /* 594 * DMA function uses this helper to poll on CMDD bits without wanting 595 * them to be cleared. 596 */ 597 if (nfc->use_dma && (readl_relaxed(nfc->regs + NDCR) & NDCR_DMA_EN)) 598 return 0; 599 600 writel_relaxed(flag, nfc->regs + NDSR); 601 602 return 0; 603 } 604 605 static int marvell_nfc_wait_cmdd(struct nand_chip *chip) 606 { 607 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip); 608 int cs_flag = NDSR_CMDD(to_nand_sel(marvell_nand)->ndcb0_csel); 609 610 return marvell_nfc_end_cmd(chip, cs_flag, "CMDD"); 611 } 612 613 static int marvell_nfc_wait_op(struct nand_chip *chip, unsigned int timeout_ms) 614 { 615 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 616 int ret; 617 618 /* Timeout is expressed in ms */ 619 if (!timeout_ms) 620 timeout_ms = IRQ_TIMEOUT; 621 622 init_completion(&nfc->complete); 623 624 marvell_nfc_enable_int(nfc, NDCR_RDYM); 625 ret = wait_for_completion_timeout(&nfc->complete, 626 msecs_to_jiffies(timeout_ms)); 627 marvell_nfc_disable_int(nfc, NDCR_RDYM); 628 marvell_nfc_clear_int(nfc, NDSR_RDY(0) | NDSR_RDY(1)); 629 if (!ret) { 630 dev_err(nfc->dev, "Timeout waiting for RB signal\n"); 631 return -ETIMEDOUT; 632 } 633 634 return 0; 635 } 636 637 static void marvell_nfc_select_chip(struct mtd_info *mtd, int die_nr) 638 { 639 struct nand_chip *chip = mtd_to_nand(mtd); 640 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip); 641 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 642 u32 ndcr_generic; 643 644 if (chip == nfc->selected_chip && die_nr == marvell_nand->selected_die) 645 return; 646 647 if (die_nr < 0 || die_nr >= marvell_nand->nsels) { 648 nfc->selected_chip = NULL; 649 marvell_nand->selected_die = -1; 650 return; 651 } 652 653 writel_relaxed(marvell_nand->ndtr0, nfc->regs + NDTR0); 654 writel_relaxed(marvell_nand->ndtr1, nfc->regs + NDTR1); 655 656 /* 657 * Reset the NDCR register to a clean state for this particular chip, 658 * also clear ND_RUN bit. 659 */ 660 ndcr_generic = readl_relaxed(nfc->regs + NDCR) & 661 NDCR_GENERIC_FIELDS_MASK & ~NDCR_ND_RUN; 662 writel_relaxed(ndcr_generic | marvell_nand->ndcr, nfc->regs + NDCR); 663 664 /* Also reset the interrupt status register */ 665 marvell_nfc_clear_int(nfc, NDCR_ALL_INT); 666 667 nfc->selected_chip = chip; 668 marvell_nand->selected_die = die_nr; 669 } 670 671 static irqreturn_t marvell_nfc_isr(int irq, void *dev_id) 672 { 673 struct marvell_nfc *nfc = dev_id; 674 u32 st = readl_relaxed(nfc->regs + NDSR); 675 u32 ien = (~readl_relaxed(nfc->regs + NDCR)) & NDCR_ALL_INT; 676 677 /* 678 * RDY interrupt mask is one bit in NDCR while there are two status 679 * bit in NDSR (RDY[cs0/cs2] and RDY[cs1/cs3]). 680 */ 681 if (st & NDSR_RDY(1)) 682 st |= NDSR_RDY(0); 683 684 if (!(st & ien)) 685 return IRQ_NONE; 686 687 marvell_nfc_disable_int(nfc, st & NDCR_ALL_INT); 688 689 if (!(st & (NDSR_RDDREQ | NDSR_WRDREQ | NDSR_WRCMDREQ))) 690 complete(&nfc->complete); 691 692 return IRQ_HANDLED; 693 } 694 695 /* HW ECC related functions */ 696 static void marvell_nfc_enable_hw_ecc(struct nand_chip *chip) 697 { 698 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 699 u32 ndcr = readl_relaxed(nfc->regs + NDCR); 700 701 if (!(ndcr & NDCR_ECC_EN)) { 702 writel_relaxed(ndcr | NDCR_ECC_EN, nfc->regs + NDCR); 703 704 /* 705 * When enabling BCH, set threshold to 0 to always know the 706 * number of corrected bitflips. 707 */ 708 if (chip->ecc.algo == NAND_ECC_BCH) 709 writel_relaxed(NDECCCTRL_BCH_EN, nfc->regs + NDECCCTRL); 710 } 711 } 712 713 static void marvell_nfc_disable_hw_ecc(struct nand_chip *chip) 714 { 715 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 716 u32 ndcr = readl_relaxed(nfc->regs + NDCR); 717 718 if (ndcr & NDCR_ECC_EN) { 719 writel_relaxed(ndcr & ~NDCR_ECC_EN, nfc->regs + NDCR); 720 if (chip->ecc.algo == NAND_ECC_BCH) 721 writel_relaxed(0, nfc->regs + NDECCCTRL); 722 } 723 } 724 725 /* DMA related helpers */ 726 static void marvell_nfc_enable_dma(struct marvell_nfc *nfc) 727 { 728 u32 reg; 729 730 reg = readl_relaxed(nfc->regs + NDCR); 731 writel_relaxed(reg | NDCR_DMA_EN, nfc->regs + NDCR); 732 } 733 734 static void marvell_nfc_disable_dma(struct marvell_nfc *nfc) 735 { 736 u32 reg; 737 738 reg = readl_relaxed(nfc->regs + NDCR); 739 writel_relaxed(reg & ~NDCR_DMA_EN, nfc->regs + NDCR); 740 } 741 742 /* Read/write PIO/DMA accessors */ 743 static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc, 744 enum dma_data_direction direction, 745 unsigned int len) 746 { 747 unsigned int dma_len = min_t(int, ALIGN(len, 32), MAX_CHUNK_SIZE); 748 struct dma_async_tx_descriptor *tx; 749 struct scatterlist sg; 750 dma_cookie_t cookie; 751 int ret; 752 753 marvell_nfc_enable_dma(nfc); 754 /* Prepare the DMA transfer */ 755 sg_init_one(&sg, nfc->dma_buf, dma_len); 756 dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction); 757 tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1, 758 direction == DMA_FROM_DEVICE ? 759 DMA_DEV_TO_MEM : DMA_MEM_TO_DEV, 760 DMA_PREP_INTERRUPT); 761 if (!tx) { 762 dev_err(nfc->dev, "Could not prepare DMA S/G list\n"); 763 return -ENXIO; 764 } 765 766 /* Do the task and wait for it to finish */ 767 cookie = dmaengine_submit(tx); 768 ret = dma_submit_error(cookie); 769 if (ret) 770 return -EIO; 771 772 dma_async_issue_pending(nfc->dma_chan); 773 ret = marvell_nfc_wait_cmdd(nfc->selected_chip); 774 dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction); 775 marvell_nfc_disable_dma(nfc); 776 if (ret) { 777 dev_err(nfc->dev, "Timeout waiting for DMA (status: %d)\n", 778 dmaengine_tx_status(nfc->dma_chan, cookie, NULL)); 779 dmaengine_terminate_all(nfc->dma_chan); 780 return -ETIMEDOUT; 781 } 782 783 return 0; 784 } 785 786 static int marvell_nfc_xfer_data_in_pio(struct marvell_nfc *nfc, u8 *in, 787 unsigned int len) 788 { 789 unsigned int last_len = len % FIFO_DEPTH; 790 unsigned int last_full_offset = round_down(len, FIFO_DEPTH); 791 int i; 792 793 for (i = 0; i < last_full_offset; i += FIFO_DEPTH) 794 ioread32_rep(nfc->regs + NDDB, in + i, FIFO_REP(FIFO_DEPTH)); 795 796 if (last_len) { 797 u8 tmp_buf[FIFO_DEPTH]; 798 799 ioread32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH)); 800 memcpy(in + last_full_offset, tmp_buf, last_len); 801 } 802 803 return 0; 804 } 805 806 static int marvell_nfc_xfer_data_out_pio(struct marvell_nfc *nfc, const u8 *out, 807 unsigned int len) 808 { 809 unsigned int last_len = len % FIFO_DEPTH; 810 unsigned int last_full_offset = round_down(len, FIFO_DEPTH); 811 int i; 812 813 for (i = 0; i < last_full_offset; i += FIFO_DEPTH) 814 iowrite32_rep(nfc->regs + NDDB, out + i, FIFO_REP(FIFO_DEPTH)); 815 816 if (last_len) { 817 u8 tmp_buf[FIFO_DEPTH]; 818 819 memcpy(tmp_buf, out + last_full_offset, last_len); 820 iowrite32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH)); 821 } 822 823 return 0; 824 } 825 826 static void marvell_nfc_check_empty_chunk(struct nand_chip *chip, 827 u8 *data, int data_len, 828 u8 *spare, int spare_len, 829 u8 *ecc, int ecc_len, 830 unsigned int *max_bitflips) 831 { 832 struct mtd_info *mtd = nand_to_mtd(chip); 833 int bf; 834 835 /* 836 * Blank pages (all 0xFF) that have not been written may be recognized 837 * as bad if bitflips occur, so whenever an uncorrectable error occurs, 838 * check if the entire page (with ECC bytes) is actually blank or not. 839 */ 840 if (!data) 841 data_len = 0; 842 if (!spare) 843 spare_len = 0; 844 if (!ecc) 845 ecc_len = 0; 846 847 bf = nand_check_erased_ecc_chunk(data, data_len, ecc, ecc_len, 848 spare, spare_len, chip->ecc.strength); 849 if (bf < 0) { 850 mtd->ecc_stats.failed++; 851 return; 852 } 853 854 /* Update the stats and max_bitflips */ 855 mtd->ecc_stats.corrected += bf; 856 *max_bitflips = max_t(unsigned int, *max_bitflips, bf); 857 } 858 859 /* 860 * Check a chunk is correct or not according to hardware ECC engine. 861 * mtd->ecc_stats.corrected is updated, as well as max_bitflips, however 862 * mtd->ecc_stats.failure is not, the function will instead return a non-zero 863 * value indicating that a check on the emptyness of the subpage must be 864 * performed before declaring the subpage corrupted. 865 */ 866 static int marvell_nfc_hw_ecc_correct(struct nand_chip *chip, 867 unsigned int *max_bitflips) 868 { 869 struct mtd_info *mtd = nand_to_mtd(chip); 870 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 871 int bf = 0; 872 u32 ndsr; 873 874 ndsr = readl_relaxed(nfc->regs + NDSR); 875 876 /* Check uncorrectable error flag */ 877 if (ndsr & NDSR_UNCERR) { 878 writel_relaxed(ndsr, nfc->regs + NDSR); 879 880 /* 881 * Do not increment ->ecc_stats.failed now, instead, return a 882 * non-zero value to indicate that this chunk was apparently 883 * bad, and it should be check to see if it empty or not. If 884 * the chunk (with ECC bytes) is not declared empty, the calling 885 * function must increment the failure count. 886 */ 887 return -EBADMSG; 888 } 889 890 /* Check correctable error flag */ 891 if (ndsr & NDSR_CORERR) { 892 writel_relaxed(ndsr, nfc->regs + NDSR); 893 894 if (chip->ecc.algo == NAND_ECC_BCH) 895 bf = NDSR_ERRCNT(ndsr); 896 else 897 bf = 1; 898 } 899 900 /* Update the stats and max_bitflips */ 901 mtd->ecc_stats.corrected += bf; 902 *max_bitflips = max_t(unsigned int, *max_bitflips, bf); 903 904 return 0; 905 } 906 907 /* Hamming read helpers */ 908 static int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip *chip, 909 u8 *data_buf, u8 *oob_buf, 910 bool raw, int page) 911 { 912 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip); 913 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 914 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 915 struct marvell_nfc_op nfc_op = { 916 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) | 917 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) | 918 NDCB0_DBC | 919 NDCB0_CMD1(NAND_CMD_READ0) | 920 NDCB0_CMD2(NAND_CMD_READSTART), 921 .ndcb[1] = NDCB1_ADDRS_PAGE(page), 922 .ndcb[2] = NDCB2_ADDR5_PAGE(page), 923 }; 924 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0); 925 int ret; 926 927 /* NFCv2 needs more information about the operation being executed */ 928 if (nfc->caps->is_nfcv2) 929 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW); 930 931 ret = marvell_nfc_prepare_cmd(chip); 932 if (ret) 933 return ret; 934 935 marvell_nfc_send_cmd(chip, &nfc_op); 936 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ, 937 "RDDREQ while draining FIFO (data/oob)"); 938 if (ret) 939 return ret; 940 941 /* 942 * Read the page then the OOB area. Unlike what is shown in current 943 * documentation, spare bytes are protected by the ECC engine, and must 944 * be at the beginning of the OOB area or running this driver on legacy 945 * systems will prevent the discovery of the BBM/BBT. 946 */ 947 if (nfc->use_dma) { 948 marvell_nfc_xfer_data_dma(nfc, DMA_FROM_DEVICE, 949 lt->data_bytes + oob_bytes); 950 memcpy(data_buf, nfc->dma_buf, lt->data_bytes); 951 memcpy(oob_buf, nfc->dma_buf + lt->data_bytes, oob_bytes); 952 } else { 953 marvell_nfc_xfer_data_in_pio(nfc, data_buf, lt->data_bytes); 954 marvell_nfc_xfer_data_in_pio(nfc, oob_buf, oob_bytes); 955 } 956 957 ret = marvell_nfc_wait_cmdd(chip); 958 959 return ret; 960 } 961 962 static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct mtd_info *mtd, 963 struct nand_chip *chip, u8 *buf, 964 int oob_required, int page) 965 { 966 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, 967 true, page); 968 } 969 970 static int marvell_nfc_hw_ecc_hmg_read_page(struct mtd_info *mtd, 971 struct nand_chip *chip, 972 u8 *buf, int oob_required, 973 int page) 974 { 975 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 976 unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes; 977 int max_bitflips = 0, ret; 978 u8 *raw_buf; 979 980 marvell_nfc_enable_hw_ecc(chip); 981 marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false, 982 page); 983 ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips); 984 marvell_nfc_disable_hw_ecc(chip); 985 986 if (!ret) 987 return max_bitflips; 988 989 /* 990 * When ECC failures are detected, check if the full page has been 991 * written or not. Ignore the failure if it is actually empty. 992 */ 993 raw_buf = kmalloc(full_sz, GFP_KERNEL); 994 if (!raw_buf) 995 return -ENOMEM; 996 997 marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf + 998 lt->data_bytes, true, page); 999 marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0, 1000 &max_bitflips); 1001 kfree(raw_buf); 1002 1003 return max_bitflips; 1004 } 1005 1006 /* 1007 * Spare area in Hamming layouts is not protected by the ECC engine (even if 1008 * it appears before the ECC bytes when reading), the ->read_oob_raw() function 1009 * also stands for ->read_oob(). 1010 */ 1011 static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct mtd_info *mtd, 1012 struct nand_chip *chip, int page) 1013 { 1014 /* Invalidate page cache */ 1015 chip->pagebuf = -1; 1016 1017 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, chip->data_buf, 1018 chip->oob_poi, true, page); 1019 } 1020 1021 /* Hamming write helpers */ 1022 static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip, 1023 const u8 *data_buf, 1024 const u8 *oob_buf, bool raw, 1025 int page) 1026 { 1027 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip); 1028 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 1029 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 1030 struct marvell_nfc_op nfc_op = { 1031 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | 1032 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) | 1033 NDCB0_CMD1(NAND_CMD_SEQIN) | 1034 NDCB0_CMD2(NAND_CMD_PAGEPROG) | 1035 NDCB0_DBC, 1036 .ndcb[1] = NDCB1_ADDRS_PAGE(page), 1037 .ndcb[2] = NDCB2_ADDR5_PAGE(page), 1038 }; 1039 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0); 1040 int ret; 1041 1042 /* NFCv2 needs more information about the operation being executed */ 1043 if (nfc->caps->is_nfcv2) 1044 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW); 1045 1046 ret = marvell_nfc_prepare_cmd(chip); 1047 if (ret) 1048 return ret; 1049 1050 marvell_nfc_send_cmd(chip, &nfc_op); 1051 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ, 1052 "WRDREQ while loading FIFO (data)"); 1053 if (ret) 1054 return ret; 1055 1056 /* Write the page then the OOB area */ 1057 if (nfc->use_dma) { 1058 memcpy(nfc->dma_buf, data_buf, lt->data_bytes); 1059 memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes); 1060 marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes + 1061 lt->ecc_bytes + lt->spare_bytes); 1062 } else { 1063 marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes); 1064 marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes); 1065 } 1066 1067 ret = marvell_nfc_wait_cmdd(chip); 1068 if (ret) 1069 return ret; 1070 1071 ret = marvell_nfc_wait_op(chip, 1072 PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max)); 1073 return ret; 1074 } 1075 1076 static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct mtd_info *mtd, 1077 struct nand_chip *chip, 1078 const u8 *buf, 1079 int oob_required, int page) 1080 { 1081 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi, 1082 true, page); 1083 } 1084 1085 static int marvell_nfc_hw_ecc_hmg_write_page(struct mtd_info *mtd, 1086 struct nand_chip *chip, 1087 const u8 *buf, 1088 int oob_required, int page) 1089 { 1090 int ret; 1091 1092 marvell_nfc_enable_hw_ecc(chip); 1093 ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi, 1094 false, page); 1095 marvell_nfc_disable_hw_ecc(chip); 1096 1097 return ret; 1098 } 1099 1100 /* 1101 * Spare area in Hamming layouts is not protected by the ECC engine (even if 1102 * it appears before the ECC bytes when reading), the ->write_oob_raw() function 1103 * also stands for ->write_oob(). 1104 */ 1105 static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct mtd_info *mtd, 1106 struct nand_chip *chip, 1107 int page) 1108 { 1109 /* Invalidate page cache */ 1110 chip->pagebuf = -1; 1111 1112 memset(chip->data_buf, 0xFF, mtd->writesize); 1113 1114 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, chip->data_buf, 1115 chip->oob_poi, true, page); 1116 } 1117 1118 /* BCH read helpers */ 1119 static int marvell_nfc_hw_ecc_bch_read_page_raw(struct mtd_info *mtd, 1120 struct nand_chip *chip, u8 *buf, 1121 int oob_required, int page) 1122 { 1123 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 1124 u8 *oob = chip->oob_poi; 1125 int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes; 1126 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) + 1127 lt->last_spare_bytes; 1128 int data_len = lt->data_bytes; 1129 int spare_len = lt->spare_bytes; 1130 int ecc_len = lt->ecc_bytes; 1131 int chunk; 1132 1133 if (oob_required) 1134 memset(chip->oob_poi, 0xFF, mtd->oobsize); 1135 1136 nand_read_page_op(chip, page, 0, NULL, 0); 1137 1138 for (chunk = 0; chunk < lt->nchunks; chunk++) { 1139 /* Update last chunk length */ 1140 if (chunk >= lt->full_chunk_cnt) { 1141 data_len = lt->last_data_bytes; 1142 spare_len = lt->last_spare_bytes; 1143 ecc_len = lt->last_ecc_bytes; 1144 } 1145 1146 /* Read data bytes*/ 1147 nand_change_read_column_op(chip, chunk * chunk_size, 1148 buf + (lt->data_bytes * chunk), 1149 data_len, false); 1150 1151 /* Read spare bytes */ 1152 nand_read_data_op(chip, oob + (lt->spare_bytes * chunk), 1153 spare_len, false); 1154 1155 /* Read ECC bytes */ 1156 nand_read_data_op(chip, oob + ecc_offset + 1157 (ALIGN(lt->ecc_bytes, 32) * chunk), 1158 ecc_len, false); 1159 } 1160 1161 return 0; 1162 } 1163 1164 static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk, 1165 u8 *data, unsigned int data_len, 1166 u8 *spare, unsigned int spare_len, 1167 int page) 1168 { 1169 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip); 1170 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 1171 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 1172 int i, ret; 1173 struct marvell_nfc_op nfc_op = { 1174 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) | 1175 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) | 1176 NDCB0_LEN_OVRD, 1177 .ndcb[1] = NDCB1_ADDRS_PAGE(page), 1178 .ndcb[2] = NDCB2_ADDR5_PAGE(page), 1179 .ndcb[3] = data_len + spare_len, 1180 }; 1181 1182 ret = marvell_nfc_prepare_cmd(chip); 1183 if (ret) 1184 return; 1185 1186 if (chunk == 0) 1187 nfc_op.ndcb[0] |= NDCB0_DBC | 1188 NDCB0_CMD1(NAND_CMD_READ0) | 1189 NDCB0_CMD2(NAND_CMD_READSTART); 1190 1191 /* 1192 * Trigger the monolithic read on the first chunk, then naked read on 1193 * intermediate chunks and finally a last naked read on the last chunk. 1194 */ 1195 if (chunk == 0) 1196 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW); 1197 else if (chunk < lt->nchunks - 1) 1198 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW); 1199 else 1200 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW); 1201 1202 marvell_nfc_send_cmd(chip, &nfc_op); 1203 1204 /* 1205 * According to the datasheet, when reading from NDDB 1206 * with BCH enabled, after each 32 bytes reads, we 1207 * have to make sure that the NDSR.RDDREQ bit is set. 1208 * 1209 * Drain the FIFO, 8 32-bit reads at a time, and skip 1210 * the polling on the last read. 1211 * 1212 * Length is a multiple of 32 bytes, hence it is a multiple of 8 too. 1213 */ 1214 for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) { 1215 marvell_nfc_end_cmd(chip, NDSR_RDDREQ, 1216 "RDDREQ while draining FIFO (data)"); 1217 marvell_nfc_xfer_data_in_pio(nfc, data, 1218 FIFO_DEPTH * BCH_SEQ_READS); 1219 data += FIFO_DEPTH * BCH_SEQ_READS; 1220 } 1221 1222 for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) { 1223 marvell_nfc_end_cmd(chip, NDSR_RDDREQ, 1224 "RDDREQ while draining FIFO (OOB)"); 1225 marvell_nfc_xfer_data_in_pio(nfc, spare, 1226 FIFO_DEPTH * BCH_SEQ_READS); 1227 spare += FIFO_DEPTH * BCH_SEQ_READS; 1228 } 1229 } 1230 1231 static int marvell_nfc_hw_ecc_bch_read_page(struct mtd_info *mtd, 1232 struct nand_chip *chip, 1233 u8 *buf, int oob_required, 1234 int page) 1235 { 1236 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 1237 int data_len = lt->data_bytes, spare_len = lt->spare_bytes, ecc_len; 1238 u8 *data = buf, *spare = chip->oob_poi, *ecc; 1239 int max_bitflips = 0; 1240 u32 failure_mask = 0; 1241 int chunk, ecc_offset_in_page, ret; 1242 1243 /* 1244 * With BCH, OOB is not fully used (and thus not read entirely), not 1245 * expected bytes could show up at the end of the OOB buffer if not 1246 * explicitly erased. 1247 */ 1248 if (oob_required) 1249 memset(chip->oob_poi, 0xFF, mtd->oobsize); 1250 1251 marvell_nfc_enable_hw_ecc(chip); 1252 1253 for (chunk = 0; chunk < lt->nchunks; chunk++) { 1254 /* Update length for the last chunk */ 1255 if (chunk >= lt->full_chunk_cnt) { 1256 data_len = lt->last_data_bytes; 1257 spare_len = lt->last_spare_bytes; 1258 } 1259 1260 /* Read the chunk and detect number of bitflips */ 1261 marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len, 1262 spare, spare_len, page); 1263 ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips); 1264 if (ret) 1265 failure_mask |= BIT(chunk); 1266 1267 data += data_len; 1268 spare += spare_len; 1269 } 1270 1271 marvell_nfc_disable_hw_ecc(chip); 1272 1273 if (!failure_mask) 1274 return max_bitflips; 1275 1276 /* 1277 * Please note that dumping the ECC bytes during a normal read with OOB 1278 * area would add a significant overhead as ECC bytes are "consumed" by 1279 * the controller in normal mode and must be re-read in raw mode. To 1280 * avoid dropping the performances, we prefer not to include them. The 1281 * user should re-read the page in raw mode if ECC bytes are required. 1282 * 1283 * However, for any subpage read error reported by ->correct(), the ECC 1284 * bytes must be read in raw mode and the full subpage must be checked 1285 * to see if it is entirely empty of if there was an actual error. 1286 */ 1287 for (chunk = 0; chunk < lt->nchunks; chunk++) { 1288 /* No failure reported for this chunk, move to the next one */ 1289 if (!(failure_mask & BIT(chunk))) 1290 continue; 1291 1292 /* Derive ECC bytes positions (in page/buffer) and length */ 1293 ecc = chip->oob_poi + 1294 (lt->full_chunk_cnt * lt->spare_bytes) + 1295 lt->last_spare_bytes + 1296 (chunk * ALIGN(lt->ecc_bytes, 32)); 1297 ecc_offset_in_page = 1298 (chunk * (lt->data_bytes + lt->spare_bytes + 1299 lt->ecc_bytes)) + 1300 (chunk < lt->full_chunk_cnt ? 1301 lt->data_bytes + lt->spare_bytes : 1302 lt->last_data_bytes + lt->last_spare_bytes); 1303 ecc_len = chunk < lt->full_chunk_cnt ? 1304 lt->ecc_bytes : lt->last_ecc_bytes; 1305 1306 /* Do the actual raw read of the ECC bytes */ 1307 nand_change_read_column_op(chip, ecc_offset_in_page, 1308 ecc, ecc_len, false); 1309 1310 /* Derive data/spare bytes positions (in buffer) and length */ 1311 data = buf + (chunk * lt->data_bytes); 1312 data_len = chunk < lt->full_chunk_cnt ? 1313 lt->data_bytes : lt->last_data_bytes; 1314 spare = chip->oob_poi + (chunk * (lt->spare_bytes + 1315 lt->ecc_bytes)); 1316 spare_len = chunk < lt->full_chunk_cnt ? 1317 lt->spare_bytes : lt->last_spare_bytes; 1318 1319 /* Check the entire chunk (data + spare + ecc) for emptyness */ 1320 marvell_nfc_check_empty_chunk(chip, data, data_len, spare, 1321 spare_len, ecc, ecc_len, 1322 &max_bitflips); 1323 } 1324 1325 return max_bitflips; 1326 } 1327 1328 static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct mtd_info *mtd, 1329 struct nand_chip *chip, int page) 1330 { 1331 /* Invalidate page cache */ 1332 chip->pagebuf = -1; 1333 1334 return chip->ecc.read_page_raw(mtd, chip, chip->data_buf, true, page); 1335 } 1336 1337 static int marvell_nfc_hw_ecc_bch_read_oob(struct mtd_info *mtd, 1338 struct nand_chip *chip, int page) 1339 { 1340 /* Invalidate page cache */ 1341 chip->pagebuf = -1; 1342 1343 return chip->ecc.read_page(mtd, chip, chip->data_buf, true, page); 1344 } 1345 1346 /* BCH write helpers */ 1347 static int marvell_nfc_hw_ecc_bch_write_page_raw(struct mtd_info *mtd, 1348 struct nand_chip *chip, 1349 const u8 *buf, 1350 int oob_required, int page) 1351 { 1352 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 1353 int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes; 1354 int data_len = lt->data_bytes; 1355 int spare_len = lt->spare_bytes; 1356 int ecc_len = lt->ecc_bytes; 1357 int spare_offset = 0; 1358 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) + 1359 lt->last_spare_bytes; 1360 int chunk; 1361 1362 nand_prog_page_begin_op(chip, page, 0, NULL, 0); 1363 1364 for (chunk = 0; chunk < lt->nchunks; chunk++) { 1365 if (chunk >= lt->full_chunk_cnt) { 1366 data_len = lt->last_data_bytes; 1367 spare_len = lt->last_spare_bytes; 1368 ecc_len = lt->last_ecc_bytes; 1369 } 1370 1371 /* Point to the column of the next chunk */ 1372 nand_change_write_column_op(chip, chunk * full_chunk_size, 1373 NULL, 0, false); 1374 1375 /* Write the data */ 1376 nand_write_data_op(chip, buf + (chunk * lt->data_bytes), 1377 data_len, false); 1378 1379 if (!oob_required) 1380 continue; 1381 1382 /* Write the spare bytes */ 1383 if (spare_len) 1384 nand_write_data_op(chip, chip->oob_poi + spare_offset, 1385 spare_len, false); 1386 1387 /* Write the ECC bytes */ 1388 if (ecc_len) 1389 nand_write_data_op(chip, chip->oob_poi + ecc_offset, 1390 ecc_len, false); 1391 1392 spare_offset += spare_len; 1393 ecc_offset += ALIGN(ecc_len, 32); 1394 } 1395 1396 return nand_prog_page_end_op(chip); 1397 } 1398 1399 static int 1400 marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk, 1401 const u8 *data, unsigned int data_len, 1402 const u8 *spare, unsigned int spare_len, 1403 int page) 1404 { 1405 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip); 1406 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 1407 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 1408 u32 xtype; 1409 int ret; 1410 struct marvell_nfc_op nfc_op = { 1411 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD, 1412 .ndcb[3] = data_len + spare_len, 1413 }; 1414 1415 /* 1416 * First operation dispatches the CMD_SEQIN command, issue the address 1417 * cycles and asks for the first chunk of data. 1418 * All operations in the middle (if any) will issue a naked write and 1419 * also ask for data. 1420 * Last operation (if any) asks for the last chunk of data through a 1421 * last naked write. 1422 */ 1423 if (chunk == 0) { 1424 if (lt->nchunks == 1) 1425 xtype = XTYPE_MONOLITHIC_RW; 1426 else 1427 xtype = XTYPE_WRITE_DISPATCH; 1428 1429 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(xtype) | 1430 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) | 1431 NDCB0_CMD1(NAND_CMD_SEQIN); 1432 nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page); 1433 nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page); 1434 } else if (chunk < lt->nchunks - 1) { 1435 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW); 1436 } else { 1437 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW); 1438 } 1439 1440 /* Always dispatch the PAGEPROG command on the last chunk */ 1441 if (chunk == lt->nchunks - 1) 1442 nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC; 1443 1444 ret = marvell_nfc_prepare_cmd(chip); 1445 if (ret) 1446 return ret; 1447 1448 marvell_nfc_send_cmd(chip, &nfc_op); 1449 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ, 1450 "WRDREQ while loading FIFO (data)"); 1451 if (ret) 1452 return ret; 1453 1454 /* Transfer the contents */ 1455 iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len)); 1456 iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len)); 1457 1458 return 0; 1459 } 1460 1461 static int marvell_nfc_hw_ecc_bch_write_page(struct mtd_info *mtd, 1462 struct nand_chip *chip, 1463 const u8 *buf, 1464 int oob_required, int page) 1465 { 1466 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 1467 const u8 *data = buf; 1468 const u8 *spare = chip->oob_poi; 1469 int data_len = lt->data_bytes; 1470 int spare_len = lt->spare_bytes; 1471 int chunk, ret; 1472 1473 /* Spare data will be written anyway, so clear it to avoid garbage */ 1474 if (!oob_required) 1475 memset(chip->oob_poi, 0xFF, mtd->oobsize); 1476 1477 marvell_nfc_enable_hw_ecc(chip); 1478 1479 for (chunk = 0; chunk < lt->nchunks; chunk++) { 1480 if (chunk >= lt->full_chunk_cnt) { 1481 data_len = lt->last_data_bytes; 1482 spare_len = lt->last_spare_bytes; 1483 } 1484 1485 marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len, 1486 spare, spare_len, page); 1487 data += data_len; 1488 spare += spare_len; 1489 1490 /* 1491 * Waiting only for CMDD or PAGED is not enough, ECC are 1492 * partially written. No flag is set once the operation is 1493 * really finished but the ND_RUN bit is cleared, so wait for it 1494 * before stepping into the next command. 1495 */ 1496 marvell_nfc_wait_ndrun(chip); 1497 } 1498 1499 ret = marvell_nfc_wait_op(chip, 1500 PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max)); 1501 1502 marvell_nfc_disable_hw_ecc(chip); 1503 1504 if (ret) 1505 return ret; 1506 1507 return 0; 1508 } 1509 1510 static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct mtd_info *mtd, 1511 struct nand_chip *chip, 1512 int page) 1513 { 1514 /* Invalidate page cache */ 1515 chip->pagebuf = -1; 1516 1517 memset(chip->data_buf, 0xFF, mtd->writesize); 1518 1519 return chip->ecc.write_page_raw(mtd, chip, chip->data_buf, true, page); 1520 } 1521 1522 static int marvell_nfc_hw_ecc_bch_write_oob(struct mtd_info *mtd, 1523 struct nand_chip *chip, int page) 1524 { 1525 /* Invalidate page cache */ 1526 chip->pagebuf = -1; 1527 1528 memset(chip->data_buf, 0xFF, mtd->writesize); 1529 1530 return chip->ecc.write_page(mtd, chip, chip->data_buf, true, page); 1531 } 1532 1533 /* NAND framework ->exec_op() hooks and related helpers */ 1534 static void marvell_nfc_parse_instructions(struct nand_chip *chip, 1535 const struct nand_subop *subop, 1536 struct marvell_nfc_op *nfc_op) 1537 { 1538 const struct nand_op_instr *instr = NULL; 1539 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 1540 bool first_cmd = true; 1541 unsigned int op_id; 1542 int i; 1543 1544 /* Reset the input structure as most of its fields will be OR'ed */ 1545 memset(nfc_op, 0, sizeof(struct marvell_nfc_op)); 1546 1547 for (op_id = 0; op_id < subop->ninstrs; op_id++) { 1548 unsigned int offset, naddrs; 1549 const u8 *addrs; 1550 int len = nand_subop_get_data_len(subop, op_id); 1551 1552 instr = &subop->instrs[op_id]; 1553 1554 switch (instr->type) { 1555 case NAND_OP_CMD_INSTR: 1556 if (first_cmd) 1557 nfc_op->ndcb[0] |= 1558 NDCB0_CMD1(instr->ctx.cmd.opcode); 1559 else 1560 nfc_op->ndcb[0] |= 1561 NDCB0_CMD2(instr->ctx.cmd.opcode) | 1562 NDCB0_DBC; 1563 1564 nfc_op->cle_ale_delay_ns = instr->delay_ns; 1565 first_cmd = false; 1566 break; 1567 1568 case NAND_OP_ADDR_INSTR: 1569 offset = nand_subop_get_addr_start_off(subop, op_id); 1570 naddrs = nand_subop_get_num_addr_cyc(subop, op_id); 1571 addrs = &instr->ctx.addr.addrs[offset]; 1572 1573 nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs); 1574 1575 for (i = 0; i < min_t(unsigned int, 4, naddrs); i++) 1576 nfc_op->ndcb[1] |= addrs[i] << (8 * i); 1577 1578 if (naddrs >= 5) 1579 nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]); 1580 if (naddrs >= 6) 1581 nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]); 1582 if (naddrs == 7) 1583 nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]); 1584 1585 nfc_op->cle_ale_delay_ns = instr->delay_ns; 1586 break; 1587 1588 case NAND_OP_DATA_IN_INSTR: 1589 nfc_op->data_instr = instr; 1590 nfc_op->data_instr_idx = op_id; 1591 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ); 1592 if (nfc->caps->is_nfcv2) { 1593 nfc_op->ndcb[0] |= 1594 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) | 1595 NDCB0_LEN_OVRD; 1596 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH); 1597 } 1598 nfc_op->data_delay_ns = instr->delay_ns; 1599 break; 1600 1601 case NAND_OP_DATA_OUT_INSTR: 1602 nfc_op->data_instr = instr; 1603 nfc_op->data_instr_idx = op_id; 1604 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE); 1605 if (nfc->caps->is_nfcv2) { 1606 nfc_op->ndcb[0] |= 1607 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) | 1608 NDCB0_LEN_OVRD; 1609 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH); 1610 } 1611 nfc_op->data_delay_ns = instr->delay_ns; 1612 break; 1613 1614 case NAND_OP_WAITRDY_INSTR: 1615 nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms; 1616 nfc_op->rdy_delay_ns = instr->delay_ns; 1617 break; 1618 } 1619 } 1620 } 1621 1622 static int marvell_nfc_xfer_data_pio(struct nand_chip *chip, 1623 const struct nand_subop *subop, 1624 struct marvell_nfc_op *nfc_op) 1625 { 1626 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 1627 const struct nand_op_instr *instr = nfc_op->data_instr; 1628 unsigned int op_id = nfc_op->data_instr_idx; 1629 unsigned int len = nand_subop_get_data_len(subop, op_id); 1630 unsigned int offset = nand_subop_get_data_start_off(subop, op_id); 1631 bool reading = (instr->type == NAND_OP_DATA_IN_INSTR); 1632 int ret; 1633 1634 if (instr->ctx.data.force_8bit) 1635 marvell_nfc_force_byte_access(chip, true); 1636 1637 if (reading) { 1638 u8 *in = instr->ctx.data.buf.in + offset; 1639 1640 ret = marvell_nfc_xfer_data_in_pio(nfc, in, len); 1641 } else { 1642 const u8 *out = instr->ctx.data.buf.out + offset; 1643 1644 ret = marvell_nfc_xfer_data_out_pio(nfc, out, len); 1645 } 1646 1647 if (instr->ctx.data.force_8bit) 1648 marvell_nfc_force_byte_access(chip, false); 1649 1650 return ret; 1651 } 1652 1653 static int marvell_nfc_monolithic_access_exec(struct nand_chip *chip, 1654 const struct nand_subop *subop) 1655 { 1656 struct marvell_nfc_op nfc_op; 1657 bool reading; 1658 int ret; 1659 1660 marvell_nfc_parse_instructions(chip, subop, &nfc_op); 1661 reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR); 1662 1663 ret = marvell_nfc_prepare_cmd(chip); 1664 if (ret) 1665 return ret; 1666 1667 marvell_nfc_send_cmd(chip, &nfc_op); 1668 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ, 1669 "RDDREQ/WRDREQ while draining raw data"); 1670 if (ret) 1671 return ret; 1672 1673 cond_delay(nfc_op.cle_ale_delay_ns); 1674 1675 if (reading) { 1676 if (nfc_op.rdy_timeout_ms) { 1677 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms); 1678 if (ret) 1679 return ret; 1680 } 1681 1682 cond_delay(nfc_op.rdy_delay_ns); 1683 } 1684 1685 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op); 1686 ret = marvell_nfc_wait_cmdd(chip); 1687 if (ret) 1688 return ret; 1689 1690 cond_delay(nfc_op.data_delay_ns); 1691 1692 if (!reading) { 1693 if (nfc_op.rdy_timeout_ms) { 1694 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms); 1695 if (ret) 1696 return ret; 1697 } 1698 1699 cond_delay(nfc_op.rdy_delay_ns); 1700 } 1701 1702 /* 1703 * NDCR ND_RUN bit should be cleared automatically at the end of each 1704 * operation but experience shows that the behavior is buggy when it 1705 * comes to writes (with LEN_OVRD). Clear it by hand in this case. 1706 */ 1707 if (!reading) { 1708 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 1709 1710 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN, 1711 nfc->regs + NDCR); 1712 } 1713 1714 return 0; 1715 } 1716 1717 static int marvell_nfc_naked_access_exec(struct nand_chip *chip, 1718 const struct nand_subop *subop) 1719 { 1720 struct marvell_nfc_op nfc_op; 1721 int ret; 1722 1723 marvell_nfc_parse_instructions(chip, subop, &nfc_op); 1724 1725 /* 1726 * Naked access are different in that they need to be flagged as naked 1727 * by the controller. Reset the controller registers fields that inform 1728 * on the type and refill them according to the ongoing operation. 1729 */ 1730 nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) | 1731 NDCB0_CMD_XTYPE(XTYPE_MASK)); 1732 switch (subop->instrs[0].type) { 1733 case NAND_OP_CMD_INSTR: 1734 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD); 1735 break; 1736 case NAND_OP_ADDR_INSTR: 1737 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR); 1738 break; 1739 case NAND_OP_DATA_IN_INSTR: 1740 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) | 1741 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW); 1742 break; 1743 case NAND_OP_DATA_OUT_INSTR: 1744 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) | 1745 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW); 1746 break; 1747 default: 1748 /* This should never happen */ 1749 break; 1750 } 1751 1752 ret = marvell_nfc_prepare_cmd(chip); 1753 if (ret) 1754 return ret; 1755 1756 marvell_nfc_send_cmd(chip, &nfc_op); 1757 1758 if (!nfc_op.data_instr) { 1759 ret = marvell_nfc_wait_cmdd(chip); 1760 cond_delay(nfc_op.cle_ale_delay_ns); 1761 return ret; 1762 } 1763 1764 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ, 1765 "RDDREQ/WRDREQ while draining raw data"); 1766 if (ret) 1767 return ret; 1768 1769 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op); 1770 ret = marvell_nfc_wait_cmdd(chip); 1771 if (ret) 1772 return ret; 1773 1774 /* 1775 * NDCR ND_RUN bit should be cleared automatically at the end of each 1776 * operation but experience shows that the behavior is buggy when it 1777 * comes to writes (with LEN_OVRD). Clear it by hand in this case. 1778 */ 1779 if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) { 1780 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 1781 1782 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN, 1783 nfc->regs + NDCR); 1784 } 1785 1786 return 0; 1787 } 1788 1789 static int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip, 1790 const struct nand_subop *subop) 1791 { 1792 struct marvell_nfc_op nfc_op; 1793 int ret; 1794 1795 marvell_nfc_parse_instructions(chip, subop, &nfc_op); 1796 1797 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms); 1798 cond_delay(nfc_op.rdy_delay_ns); 1799 1800 return ret; 1801 } 1802 1803 static int marvell_nfc_read_id_type_exec(struct nand_chip *chip, 1804 const struct nand_subop *subop) 1805 { 1806 struct marvell_nfc_op nfc_op; 1807 int ret; 1808 1809 marvell_nfc_parse_instructions(chip, subop, &nfc_op); 1810 nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ); 1811 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID); 1812 1813 ret = marvell_nfc_prepare_cmd(chip); 1814 if (ret) 1815 return ret; 1816 1817 marvell_nfc_send_cmd(chip, &nfc_op); 1818 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ, 1819 "RDDREQ while reading ID"); 1820 if (ret) 1821 return ret; 1822 1823 cond_delay(nfc_op.cle_ale_delay_ns); 1824 1825 if (nfc_op.rdy_timeout_ms) { 1826 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms); 1827 if (ret) 1828 return ret; 1829 } 1830 1831 cond_delay(nfc_op.rdy_delay_ns); 1832 1833 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op); 1834 ret = marvell_nfc_wait_cmdd(chip); 1835 if (ret) 1836 return ret; 1837 1838 cond_delay(nfc_op.data_delay_ns); 1839 1840 return 0; 1841 } 1842 1843 static int marvell_nfc_read_status_exec(struct nand_chip *chip, 1844 const struct nand_subop *subop) 1845 { 1846 struct marvell_nfc_op nfc_op; 1847 int ret; 1848 1849 marvell_nfc_parse_instructions(chip, subop, &nfc_op); 1850 nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ); 1851 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS); 1852 1853 ret = marvell_nfc_prepare_cmd(chip); 1854 if (ret) 1855 return ret; 1856 1857 marvell_nfc_send_cmd(chip, &nfc_op); 1858 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ, 1859 "RDDREQ while reading status"); 1860 if (ret) 1861 return ret; 1862 1863 cond_delay(nfc_op.cle_ale_delay_ns); 1864 1865 if (nfc_op.rdy_timeout_ms) { 1866 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms); 1867 if (ret) 1868 return ret; 1869 } 1870 1871 cond_delay(nfc_op.rdy_delay_ns); 1872 1873 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op); 1874 ret = marvell_nfc_wait_cmdd(chip); 1875 if (ret) 1876 return ret; 1877 1878 cond_delay(nfc_op.data_delay_ns); 1879 1880 return 0; 1881 } 1882 1883 static int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip, 1884 const struct nand_subop *subop) 1885 { 1886 struct marvell_nfc_op nfc_op; 1887 int ret; 1888 1889 marvell_nfc_parse_instructions(chip, subop, &nfc_op); 1890 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET); 1891 1892 ret = marvell_nfc_prepare_cmd(chip); 1893 if (ret) 1894 return ret; 1895 1896 marvell_nfc_send_cmd(chip, &nfc_op); 1897 ret = marvell_nfc_wait_cmdd(chip); 1898 if (ret) 1899 return ret; 1900 1901 cond_delay(nfc_op.cle_ale_delay_ns); 1902 1903 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms); 1904 if (ret) 1905 return ret; 1906 1907 cond_delay(nfc_op.rdy_delay_ns); 1908 1909 return 0; 1910 } 1911 1912 static int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip, 1913 const struct nand_subop *subop) 1914 { 1915 struct marvell_nfc_op nfc_op; 1916 int ret; 1917 1918 marvell_nfc_parse_instructions(chip, subop, &nfc_op); 1919 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE); 1920 1921 ret = marvell_nfc_prepare_cmd(chip); 1922 if (ret) 1923 return ret; 1924 1925 marvell_nfc_send_cmd(chip, &nfc_op); 1926 ret = marvell_nfc_wait_cmdd(chip); 1927 if (ret) 1928 return ret; 1929 1930 cond_delay(nfc_op.cle_ale_delay_ns); 1931 1932 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms); 1933 if (ret) 1934 return ret; 1935 1936 cond_delay(nfc_op.rdy_delay_ns); 1937 1938 return 0; 1939 } 1940 1941 static const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER( 1942 /* Monolithic reads/writes */ 1943 NAND_OP_PARSER_PATTERN( 1944 marvell_nfc_monolithic_access_exec, 1945 NAND_OP_PARSER_PAT_CMD_ELEM(false), 1946 NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2), 1947 NAND_OP_PARSER_PAT_CMD_ELEM(true), 1948 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true), 1949 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)), 1950 NAND_OP_PARSER_PATTERN( 1951 marvell_nfc_monolithic_access_exec, 1952 NAND_OP_PARSER_PAT_CMD_ELEM(false), 1953 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2), 1954 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE), 1955 NAND_OP_PARSER_PAT_CMD_ELEM(true), 1956 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)), 1957 /* Naked commands */ 1958 NAND_OP_PARSER_PATTERN( 1959 marvell_nfc_naked_access_exec, 1960 NAND_OP_PARSER_PAT_CMD_ELEM(false)), 1961 NAND_OP_PARSER_PATTERN( 1962 marvell_nfc_naked_access_exec, 1963 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)), 1964 NAND_OP_PARSER_PATTERN( 1965 marvell_nfc_naked_access_exec, 1966 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)), 1967 NAND_OP_PARSER_PATTERN( 1968 marvell_nfc_naked_access_exec, 1969 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)), 1970 NAND_OP_PARSER_PATTERN( 1971 marvell_nfc_naked_waitrdy_exec, 1972 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)), 1973 ); 1974 1975 static const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER( 1976 /* Naked commands not supported, use a function for each pattern */ 1977 NAND_OP_PARSER_PATTERN( 1978 marvell_nfc_read_id_type_exec, 1979 NAND_OP_PARSER_PAT_CMD_ELEM(false), 1980 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1), 1981 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)), 1982 NAND_OP_PARSER_PATTERN( 1983 marvell_nfc_erase_cmd_type_exec, 1984 NAND_OP_PARSER_PAT_CMD_ELEM(false), 1985 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1), 1986 NAND_OP_PARSER_PAT_CMD_ELEM(false), 1987 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)), 1988 NAND_OP_PARSER_PATTERN( 1989 marvell_nfc_read_status_exec, 1990 NAND_OP_PARSER_PAT_CMD_ELEM(false), 1991 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)), 1992 NAND_OP_PARSER_PATTERN( 1993 marvell_nfc_reset_cmd_type_exec, 1994 NAND_OP_PARSER_PAT_CMD_ELEM(false), 1995 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)), 1996 NAND_OP_PARSER_PATTERN( 1997 marvell_nfc_naked_waitrdy_exec, 1998 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)), 1999 ); 2000 2001 static int marvell_nfc_exec_op(struct nand_chip *chip, 2002 const struct nand_operation *op, 2003 bool check_only) 2004 { 2005 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 2006 2007 if (nfc->caps->is_nfcv2) 2008 return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser, 2009 op, check_only); 2010 else 2011 return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser, 2012 op, check_only); 2013 } 2014 2015 /* 2016 * Layouts were broken in old pxa3xx_nand driver, these are supposed to be 2017 * usable. 2018 */ 2019 static int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section, 2020 struct mtd_oob_region *oobregion) 2021 { 2022 struct nand_chip *chip = mtd_to_nand(mtd); 2023 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 2024 2025 if (section) 2026 return -ERANGE; 2027 2028 oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) + 2029 lt->last_ecc_bytes; 2030 oobregion->offset = mtd->oobsize - oobregion->length; 2031 2032 return 0; 2033 } 2034 2035 static int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section, 2036 struct mtd_oob_region *oobregion) 2037 { 2038 struct nand_chip *chip = mtd_to_nand(mtd); 2039 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout; 2040 2041 if (section) 2042 return -ERANGE; 2043 2044 /* 2045 * Bootrom looks in bytes 0 & 5 for bad blocks for the 2046 * 4KB page / 4bit BCH combination. 2047 */ 2048 if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K) 2049 oobregion->offset = 6; 2050 else 2051 oobregion->offset = 2; 2052 2053 oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) + 2054 lt->last_spare_bytes - oobregion->offset; 2055 2056 return 0; 2057 } 2058 2059 static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = { 2060 .ecc = marvell_nand_ooblayout_ecc, 2061 .free = marvell_nand_ooblayout_free, 2062 }; 2063 2064 static int marvell_nand_hw_ecc_ctrl_init(struct mtd_info *mtd, 2065 struct nand_ecc_ctrl *ecc) 2066 { 2067 struct nand_chip *chip = mtd_to_nand(mtd); 2068 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 2069 const struct marvell_hw_ecc_layout *l; 2070 int i; 2071 2072 if (!nfc->caps->is_nfcv2 && 2073 (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) { 2074 dev_err(nfc->dev, 2075 "NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n", 2076 mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize); 2077 return -ENOTSUPP; 2078 } 2079 2080 to_marvell_nand(chip)->layout = NULL; 2081 for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) { 2082 l = &marvell_nfc_layouts[i]; 2083 if (mtd->writesize == l->writesize && 2084 ecc->size == l->chunk && ecc->strength == l->strength) { 2085 to_marvell_nand(chip)->layout = l; 2086 break; 2087 } 2088 } 2089 2090 if (!to_marvell_nand(chip)->layout || 2091 (!nfc->caps->is_nfcv2 && ecc->strength > 1)) { 2092 dev_err(nfc->dev, 2093 "ECC strength %d at page size %d is not supported\n", 2094 ecc->strength, mtd->writesize); 2095 return -ENOTSUPP; 2096 } 2097 2098 mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops); 2099 ecc->steps = l->nchunks; 2100 ecc->size = l->data_bytes; 2101 2102 if (ecc->strength == 1) { 2103 chip->ecc.algo = NAND_ECC_HAMMING; 2104 ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw; 2105 ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page; 2106 ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw; 2107 ecc->read_oob = ecc->read_oob_raw; 2108 ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw; 2109 ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page; 2110 ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw; 2111 ecc->write_oob = ecc->write_oob_raw; 2112 } else { 2113 chip->ecc.algo = NAND_ECC_BCH; 2114 ecc->strength = 16; 2115 ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw; 2116 ecc->read_page = marvell_nfc_hw_ecc_bch_read_page; 2117 ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw; 2118 ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob; 2119 ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw; 2120 ecc->write_page = marvell_nfc_hw_ecc_bch_write_page; 2121 ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw; 2122 ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob; 2123 } 2124 2125 return 0; 2126 } 2127 2128 static int marvell_nand_ecc_init(struct mtd_info *mtd, 2129 struct nand_ecc_ctrl *ecc) 2130 { 2131 struct nand_chip *chip = mtd_to_nand(mtd); 2132 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 2133 int ret; 2134 2135 if (ecc->mode != NAND_ECC_NONE && (!ecc->size || !ecc->strength)) { 2136 if (chip->ecc_step_ds && chip->ecc_strength_ds) { 2137 ecc->size = chip->ecc_step_ds; 2138 ecc->strength = chip->ecc_strength_ds; 2139 } else { 2140 dev_info(nfc->dev, 2141 "No minimum ECC strength, using 1b/512B\n"); 2142 ecc->size = 512; 2143 ecc->strength = 1; 2144 } 2145 } 2146 2147 switch (ecc->mode) { 2148 case NAND_ECC_HW: 2149 ret = marvell_nand_hw_ecc_ctrl_init(mtd, ecc); 2150 if (ret) 2151 return ret; 2152 break; 2153 case NAND_ECC_NONE: 2154 case NAND_ECC_SOFT: 2155 case NAND_ECC_ON_DIE: 2156 if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 && 2157 mtd->writesize != SZ_2K) { 2158 dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n", 2159 mtd->writesize); 2160 return -EINVAL; 2161 } 2162 break; 2163 default: 2164 return -EINVAL; 2165 } 2166 2167 return 0; 2168 } 2169 2170 static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' }; 2171 static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' }; 2172 2173 static struct nand_bbt_descr bbt_main_descr = { 2174 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE | 2175 NAND_BBT_2BIT | NAND_BBT_VERSION, 2176 .offs = 8, 2177 .len = 6, 2178 .veroffs = 14, 2179 .maxblocks = 8, /* Last 8 blocks in each chip */ 2180 .pattern = bbt_pattern 2181 }; 2182 2183 static struct nand_bbt_descr bbt_mirror_descr = { 2184 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE | 2185 NAND_BBT_2BIT | NAND_BBT_VERSION, 2186 .offs = 8, 2187 .len = 6, 2188 .veroffs = 14, 2189 .maxblocks = 8, /* Last 8 blocks in each chip */ 2190 .pattern = bbt_mirror_pattern 2191 }; 2192 2193 static int marvell_nfc_setup_data_interface(struct mtd_info *mtd, int chipnr, 2194 const struct nand_data_interface 2195 *conf) 2196 { 2197 struct nand_chip *chip = mtd_to_nand(mtd); 2198 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip); 2199 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 2200 unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2; 2201 const struct nand_sdr_timings *sdr; 2202 struct marvell_nfc_timings nfc_tmg; 2203 int read_delay; 2204 2205 sdr = nand_get_sdr_timings(conf); 2206 if (IS_ERR(sdr)) 2207 return PTR_ERR(sdr); 2208 2209 /* 2210 * SDR timings are given in pico-seconds while NFC timings must be 2211 * expressed in NAND controller clock cycles, which is half of the 2212 * frequency of the accessible ECC clock retrieved by clk_get_rate(). 2213 * This is not written anywhere in the datasheet but was observed 2214 * with an oscilloscope. 2215 * 2216 * NFC datasheet gives equations from which thoses calculations 2217 * are derived, they tend to be slightly more restrictives than the 2218 * given core timings and may improve the overall speed. 2219 */ 2220 nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1; 2221 nfc_tmg.tRH = nfc_tmg.tRP; 2222 nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1; 2223 nfc_tmg.tWH = nfc_tmg.tWP; 2224 nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns); 2225 nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1; 2226 nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns); 2227 /* 2228 * Read delay is the time of propagation from SoC pins to NFC internal 2229 * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In 2230 * EDO mode, an additional delay of tRH must be taken into account so 2231 * the data is sampled on the falling edge instead of the rising edge. 2232 */ 2233 read_delay = sdr->tRC_min >= 30000 ? 2234 MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH; 2235 2236 nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns); 2237 /* 2238 * tWHR and tRHW are supposed to be read to write delays (and vice 2239 * versa) but in some cases, ie. when doing a change column, they must 2240 * be greater than that to be sure tCCS delay is respected. 2241 */ 2242 nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min), 2243 period_ns) - 2, 2244 nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min), 2245 period_ns); 2246 2247 /* 2248 * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays. 2249 * NFCv1: No WAIT_MODE, tR must be maximal. 2250 */ 2251 if (nfc->caps->is_nfcv2) { 2252 nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns); 2253 } else { 2254 nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max, 2255 period_ns); 2256 if (nfc_tmg.tR + 3 > nfc_tmg.tCH) 2257 nfc_tmg.tR = nfc_tmg.tCH - 3; 2258 else 2259 nfc_tmg.tR = 0; 2260 } 2261 2262 if (chipnr < 0) 2263 return 0; 2264 2265 marvell_nand->ndtr0 = 2266 NDTR0_TRP(nfc_tmg.tRP) | 2267 NDTR0_TRH(nfc_tmg.tRH) | 2268 NDTR0_ETRP(nfc_tmg.tRP) | 2269 NDTR0_TWP(nfc_tmg.tWP) | 2270 NDTR0_TWH(nfc_tmg.tWH) | 2271 NDTR0_TCS(nfc_tmg.tCS) | 2272 NDTR0_TCH(nfc_tmg.tCH); 2273 2274 marvell_nand->ndtr1 = 2275 NDTR1_TAR(nfc_tmg.tAR) | 2276 NDTR1_TWHR(nfc_tmg.tWHR) | 2277 NDTR1_TR(nfc_tmg.tR); 2278 2279 if (nfc->caps->is_nfcv2) { 2280 marvell_nand->ndtr0 |= 2281 NDTR0_RD_CNT_DEL(read_delay) | 2282 NDTR0_SELCNTR | 2283 NDTR0_TADL(nfc_tmg.tADL); 2284 2285 marvell_nand->ndtr1 |= 2286 NDTR1_TRHW(nfc_tmg.tRHW) | 2287 NDTR1_WAIT_MODE; 2288 } 2289 2290 return 0; 2291 } 2292 2293 static int marvell_nand_attach_chip(struct nand_chip *chip) 2294 { 2295 struct mtd_info *mtd = nand_to_mtd(chip); 2296 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip); 2297 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller); 2298 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(nfc->dev); 2299 int ret; 2300 2301 if (pdata && pdata->flash_bbt) 2302 chip->bbt_options |= NAND_BBT_USE_FLASH; 2303 2304 if (chip->bbt_options & NAND_BBT_USE_FLASH) { 2305 /* 2306 * We'll use a bad block table stored in-flash and don't 2307 * allow writing the bad block marker to the flash. 2308 */ 2309 chip->bbt_options |= NAND_BBT_NO_OOB_BBM; 2310 chip->bbt_td = &bbt_main_descr; 2311 chip->bbt_md = &bbt_mirror_descr; 2312 } 2313 2314 /* Save the chip-specific fields of NDCR */ 2315 marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize); 2316 if (chip->options & NAND_BUSWIDTH_16) 2317 marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C; 2318 2319 /* 2320 * On small page NANDs, only one cycle is needed to pass the 2321 * column address. 2322 */ 2323 if (mtd->writesize <= 512) { 2324 marvell_nand->addr_cyc = 1; 2325 } else { 2326 marvell_nand->addr_cyc = 2; 2327 marvell_nand->ndcr |= NDCR_RA_START; 2328 } 2329 2330 /* 2331 * Now add the number of cycles needed to pass the row 2332 * address. 2333 * 2334 * Addressing a chip using CS 2 or 3 should also need the third row 2335 * cycle but due to inconsistance in the documentation and lack of 2336 * hardware to test this situation, this case is not supported. 2337 */ 2338 if (chip->options & NAND_ROW_ADDR_3) 2339 marvell_nand->addr_cyc += 3; 2340 else 2341 marvell_nand->addr_cyc += 2; 2342 2343 if (pdata) { 2344 chip->ecc.size = pdata->ecc_step_size; 2345 chip->ecc.strength = pdata->ecc_strength; 2346 } 2347 2348 ret = marvell_nand_ecc_init(mtd, &chip->ecc); 2349 if (ret) { 2350 dev_err(nfc->dev, "ECC init failed: %d\n", ret); 2351 return ret; 2352 } 2353 2354 if (chip->ecc.mode == NAND_ECC_HW) { 2355 /* 2356 * Subpage write not available with hardware ECC, prohibit also 2357 * subpage read as in userspace subpage access would still be 2358 * allowed and subpage write, if used, would lead to numerous 2359 * uncorrectable ECC errors. 2360 */ 2361 chip->options |= NAND_NO_SUBPAGE_WRITE; 2362 } 2363 2364 if (pdata || nfc->caps->legacy_of_bindings) { 2365 /* 2366 * We keep the MTD name unchanged to avoid breaking platforms 2367 * where the MTD cmdline parser is used and the bootloader 2368 * has not been updated to use the new naming scheme. 2369 */ 2370 mtd->name = "pxa3xx_nand-0"; 2371 } else if (!mtd->name) { 2372 /* 2373 * If the new bindings are used and the bootloader has not been 2374 * updated to pass a new mtdparts parameter on the cmdline, you 2375 * should define the following property in your NAND node, ie: 2376 * 2377 * label = "main-storage"; 2378 * 2379 * This way, mtd->name will be set by the core when 2380 * nand_set_flash_node() is called. 2381 */ 2382 mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL, 2383 "%s:nand.%d", dev_name(nfc->dev), 2384 marvell_nand->sels[0].cs); 2385 if (!mtd->name) { 2386 dev_err(nfc->dev, "Failed to allocate mtd->name\n"); 2387 return -ENOMEM; 2388 } 2389 } 2390 2391 return 0; 2392 } 2393 2394 static const struct nand_controller_ops marvell_nand_controller_ops = { 2395 .attach_chip = marvell_nand_attach_chip, 2396 }; 2397 2398 static int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc, 2399 struct device_node *np) 2400 { 2401 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev); 2402 struct marvell_nand_chip *marvell_nand; 2403 struct mtd_info *mtd; 2404 struct nand_chip *chip; 2405 int nsels, ret, i; 2406 u32 cs, rb; 2407 2408 /* 2409 * The legacy "num-cs" property indicates the number of CS on the only 2410 * chip connected to the controller (legacy bindings does not support 2411 * more than one chip). The CS and RB pins are always the #0. 2412 * 2413 * When not using legacy bindings, a couple of "reg" and "nand-rb" 2414 * properties must be filled. For each chip, expressed as a subnode, 2415 * "reg" points to the CS lines and "nand-rb" to the RB line. 2416 */ 2417 if (pdata || nfc->caps->legacy_of_bindings) { 2418 nsels = 1; 2419 } else { 2420 nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32)); 2421 if (nsels <= 0) { 2422 dev_err(dev, "missing/invalid reg property\n"); 2423 return -EINVAL; 2424 } 2425 } 2426 2427 /* Alloc the nand chip structure */ 2428 marvell_nand = devm_kzalloc(dev, sizeof(*marvell_nand) + 2429 (nsels * 2430 sizeof(struct marvell_nand_chip_sel)), 2431 GFP_KERNEL); 2432 if (!marvell_nand) { 2433 dev_err(dev, "could not allocate chip structure\n"); 2434 return -ENOMEM; 2435 } 2436 2437 marvell_nand->nsels = nsels; 2438 marvell_nand->selected_die = -1; 2439 2440 for (i = 0; i < nsels; i++) { 2441 if (pdata || nfc->caps->legacy_of_bindings) { 2442 /* 2443 * Legacy bindings use the CS lines in natural 2444 * order (0, 1, ...) 2445 */ 2446 cs = i; 2447 } else { 2448 /* Retrieve CS id */ 2449 ret = of_property_read_u32_index(np, "reg", i, &cs); 2450 if (ret) { 2451 dev_err(dev, "could not retrieve reg property: %d\n", 2452 ret); 2453 return ret; 2454 } 2455 } 2456 2457 if (cs >= nfc->caps->max_cs_nb) { 2458 dev_err(dev, "invalid reg value: %u (max CS = %d)\n", 2459 cs, nfc->caps->max_cs_nb); 2460 return -EINVAL; 2461 } 2462 2463 if (test_and_set_bit(cs, &nfc->assigned_cs)) { 2464 dev_err(dev, "CS %d already assigned\n", cs); 2465 return -EINVAL; 2466 } 2467 2468 /* 2469 * The cs variable represents the chip select id, which must be 2470 * converted in bit fields for NDCB0 and NDCB2 to select the 2471 * right chip. Unfortunately, due to a lack of information on 2472 * the subject and incoherent documentation, the user should not 2473 * use CS1 and CS3 at all as asserting them is not supported in 2474 * a reliable way (due to multiplexing inside ADDR5 field). 2475 */ 2476 marvell_nand->sels[i].cs = cs; 2477 switch (cs) { 2478 case 0: 2479 case 2: 2480 marvell_nand->sels[i].ndcb0_csel = 0; 2481 break; 2482 case 1: 2483 case 3: 2484 marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL; 2485 break; 2486 default: 2487 return -EINVAL; 2488 } 2489 2490 /* Retrieve RB id */ 2491 if (pdata || nfc->caps->legacy_of_bindings) { 2492 /* Legacy bindings always use RB #0 */ 2493 rb = 0; 2494 } else { 2495 ret = of_property_read_u32_index(np, "nand-rb", i, 2496 &rb); 2497 if (ret) { 2498 dev_err(dev, 2499 "could not retrieve RB property: %d\n", 2500 ret); 2501 return ret; 2502 } 2503 } 2504 2505 if (rb >= nfc->caps->max_rb_nb) { 2506 dev_err(dev, "invalid reg value: %u (max RB = %d)\n", 2507 rb, nfc->caps->max_rb_nb); 2508 return -EINVAL; 2509 } 2510 2511 marvell_nand->sels[i].rb = rb; 2512 } 2513 2514 chip = &marvell_nand->chip; 2515 chip->controller = &nfc->controller; 2516 nand_set_flash_node(chip, np); 2517 2518 chip->exec_op = marvell_nfc_exec_op; 2519 chip->select_chip = marvell_nfc_select_chip; 2520 if (!of_property_read_bool(np, "marvell,nand-keep-config")) 2521 chip->setup_data_interface = marvell_nfc_setup_data_interface; 2522 2523 mtd = nand_to_mtd(chip); 2524 mtd->dev.parent = dev; 2525 2526 /* 2527 * Default to HW ECC engine mode. If the nand-ecc-mode property is given 2528 * in the DT node, this entry will be overwritten in nand_scan_ident(). 2529 */ 2530 chip->ecc.mode = NAND_ECC_HW; 2531 2532 /* 2533 * Save a reference value for timing registers before 2534 * ->setup_data_interface() is called. 2535 */ 2536 marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0); 2537 marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1); 2538 2539 chip->options |= NAND_BUSWIDTH_AUTO; 2540 2541 ret = nand_scan(mtd, marvell_nand->nsels); 2542 if (ret) { 2543 dev_err(dev, "could not scan the nand chip\n"); 2544 return ret; 2545 } 2546 2547 if (pdata) 2548 /* Legacy bindings support only one chip */ 2549 ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts); 2550 else 2551 ret = mtd_device_register(mtd, NULL, 0); 2552 if (ret) { 2553 dev_err(dev, "failed to register mtd device: %d\n", ret); 2554 nand_release(mtd); 2555 return ret; 2556 } 2557 2558 list_add_tail(&marvell_nand->node, &nfc->chips); 2559 2560 return 0; 2561 } 2562 2563 static int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc) 2564 { 2565 struct device_node *np = dev->of_node; 2566 struct device_node *nand_np; 2567 int max_cs = nfc->caps->max_cs_nb; 2568 int nchips; 2569 int ret; 2570 2571 if (!np) 2572 nchips = 1; 2573 else 2574 nchips = of_get_child_count(np); 2575 2576 if (nchips > max_cs) { 2577 dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips, 2578 max_cs); 2579 return -EINVAL; 2580 } 2581 2582 /* 2583 * Legacy bindings do not use child nodes to exhibit NAND chip 2584 * properties and layout. Instead, NAND properties are mixed with the 2585 * controller ones, and partitions are defined as direct subnodes of the 2586 * NAND controller node. 2587 */ 2588 if (nfc->caps->legacy_of_bindings) { 2589 ret = marvell_nand_chip_init(dev, nfc, np); 2590 return ret; 2591 } 2592 2593 for_each_child_of_node(np, nand_np) { 2594 ret = marvell_nand_chip_init(dev, nfc, nand_np); 2595 if (ret) { 2596 of_node_put(nand_np); 2597 return ret; 2598 } 2599 } 2600 2601 return 0; 2602 } 2603 2604 static void marvell_nand_chips_cleanup(struct marvell_nfc *nfc) 2605 { 2606 struct marvell_nand_chip *entry, *temp; 2607 2608 list_for_each_entry_safe(entry, temp, &nfc->chips, node) { 2609 nand_release(nand_to_mtd(&entry->chip)); 2610 list_del(&entry->node); 2611 } 2612 } 2613 2614 static int marvell_nfc_init_dma(struct marvell_nfc *nfc) 2615 { 2616 struct platform_device *pdev = container_of(nfc->dev, 2617 struct platform_device, 2618 dev); 2619 struct dma_slave_config config = {}; 2620 struct resource *r; 2621 int ret; 2622 2623 if (!IS_ENABLED(CONFIG_PXA_DMA)) { 2624 dev_warn(nfc->dev, 2625 "DMA not enabled in configuration\n"); 2626 return -ENOTSUPP; 2627 } 2628 2629 ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32)); 2630 if (ret) 2631 return ret; 2632 2633 nfc->dma_chan = dma_request_slave_channel(nfc->dev, "data"); 2634 if (!nfc->dma_chan) { 2635 dev_err(nfc->dev, 2636 "Unable to request data DMA channel\n"); 2637 return -ENODEV; 2638 } 2639 2640 r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2641 if (!r) 2642 return -ENXIO; 2643 2644 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 2645 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 2646 config.src_addr = r->start + NDDB; 2647 config.dst_addr = r->start + NDDB; 2648 config.src_maxburst = 32; 2649 config.dst_maxburst = 32; 2650 ret = dmaengine_slave_config(nfc->dma_chan, &config); 2651 if (ret < 0) { 2652 dev_err(nfc->dev, "Failed to configure DMA channel\n"); 2653 return ret; 2654 } 2655 2656 /* 2657 * DMA must act on length multiple of 32 and this length may be 2658 * bigger than the destination buffer. Use this buffer instead 2659 * for DMA transfers and then copy the desired amount of data to 2660 * the provided buffer. 2661 */ 2662 nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA); 2663 if (!nfc->dma_buf) 2664 return -ENOMEM; 2665 2666 nfc->use_dma = true; 2667 2668 return 0; 2669 } 2670 2671 static void marvell_nfc_reset(struct marvell_nfc *nfc) 2672 { 2673 /* 2674 * ECC operations and interruptions are only enabled when specifically 2675 * needed. ECC shall not be activated in the early stages (fails probe). 2676 * Arbiter flag, even if marked as "reserved", must be set (empirical). 2677 * SPARE_EN bit must always be set or ECC bytes will not be at the same 2678 * offset in the read page and this will fail the protection. 2679 */ 2680 writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN | 2681 NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR); 2682 writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR); 2683 writel_relaxed(0, nfc->regs + NDECCCTRL); 2684 } 2685 2686 static int marvell_nfc_init(struct marvell_nfc *nfc) 2687 { 2688 struct device_node *np = nfc->dev->of_node; 2689 2690 /* 2691 * Some SoCs like A7k/A8k need to enable manually the NAND 2692 * controller, gated clocks and reset bits to avoid being bootloader 2693 * dependent. This is done through the use of the System Functions 2694 * registers. 2695 */ 2696 if (nfc->caps->need_system_controller) { 2697 struct regmap *sysctrl_base = 2698 syscon_regmap_lookup_by_phandle(np, 2699 "marvell,system-controller"); 2700 u32 reg; 2701 2702 if (IS_ERR(sysctrl_base)) 2703 return PTR_ERR(sysctrl_base); 2704 2705 reg = GENCONF_SOC_DEVICE_MUX_NFC_EN | 2706 GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST | 2707 GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST | 2708 GENCONF_SOC_DEVICE_MUX_NFC_INT_EN; 2709 regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX, reg); 2710 2711 regmap_read(sysctrl_base, GENCONF_CLK_GATING_CTRL, ®); 2712 reg |= GENCONF_CLK_GATING_CTRL_ND_GATE; 2713 regmap_write(sysctrl_base, GENCONF_CLK_GATING_CTRL, reg); 2714 2715 regmap_read(sysctrl_base, GENCONF_ND_CLK_CTRL, ®); 2716 reg |= GENCONF_ND_CLK_CTRL_EN; 2717 regmap_write(sysctrl_base, GENCONF_ND_CLK_CTRL, reg); 2718 } 2719 2720 /* Configure the DMA if appropriate */ 2721 if (!nfc->caps->is_nfcv2) 2722 marvell_nfc_init_dma(nfc); 2723 2724 marvell_nfc_reset(nfc); 2725 2726 return 0; 2727 } 2728 2729 static int marvell_nfc_probe(struct platform_device *pdev) 2730 { 2731 struct device *dev = &pdev->dev; 2732 struct resource *r; 2733 struct marvell_nfc *nfc; 2734 int ret; 2735 int irq; 2736 2737 nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc), 2738 GFP_KERNEL); 2739 if (!nfc) 2740 return -ENOMEM; 2741 2742 nfc->dev = dev; 2743 nand_controller_init(&nfc->controller); 2744 nfc->controller.ops = &marvell_nand_controller_ops; 2745 INIT_LIST_HEAD(&nfc->chips); 2746 2747 r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2748 nfc->regs = devm_ioremap_resource(dev, r); 2749 if (IS_ERR(nfc->regs)) 2750 return PTR_ERR(nfc->regs); 2751 2752 irq = platform_get_irq(pdev, 0); 2753 if (irq < 0) { 2754 dev_err(dev, "failed to retrieve irq\n"); 2755 return irq; 2756 } 2757 2758 nfc->core_clk = devm_clk_get(&pdev->dev, "core"); 2759 2760 /* Managed the legacy case (when the first clock was not named) */ 2761 if (nfc->core_clk == ERR_PTR(-ENOENT)) 2762 nfc->core_clk = devm_clk_get(&pdev->dev, NULL); 2763 2764 if (IS_ERR(nfc->core_clk)) 2765 return PTR_ERR(nfc->core_clk); 2766 2767 ret = clk_prepare_enable(nfc->core_clk); 2768 if (ret) 2769 return ret; 2770 2771 nfc->reg_clk = devm_clk_get(&pdev->dev, "reg"); 2772 if (IS_ERR(nfc->reg_clk)) { 2773 if (PTR_ERR(nfc->reg_clk) != -ENOENT) { 2774 ret = PTR_ERR(nfc->reg_clk); 2775 goto unprepare_core_clk; 2776 } 2777 2778 nfc->reg_clk = NULL; 2779 } 2780 2781 ret = clk_prepare_enable(nfc->reg_clk); 2782 if (ret) 2783 goto unprepare_core_clk; 2784 2785 marvell_nfc_disable_int(nfc, NDCR_ALL_INT); 2786 marvell_nfc_clear_int(nfc, NDCR_ALL_INT); 2787 ret = devm_request_irq(dev, irq, marvell_nfc_isr, 2788 0, "marvell-nfc", nfc); 2789 if (ret) 2790 goto unprepare_reg_clk; 2791 2792 /* Get NAND controller capabilities */ 2793 if (pdev->id_entry) 2794 nfc->caps = (void *)pdev->id_entry->driver_data; 2795 else 2796 nfc->caps = of_device_get_match_data(&pdev->dev); 2797 2798 if (!nfc->caps) { 2799 dev_err(dev, "Could not retrieve NFC caps\n"); 2800 ret = -EINVAL; 2801 goto unprepare_reg_clk; 2802 } 2803 2804 /* Init the controller and then probe the chips */ 2805 ret = marvell_nfc_init(nfc); 2806 if (ret) 2807 goto unprepare_reg_clk; 2808 2809 platform_set_drvdata(pdev, nfc); 2810 2811 ret = marvell_nand_chips_init(dev, nfc); 2812 if (ret) 2813 goto unprepare_reg_clk; 2814 2815 return 0; 2816 2817 unprepare_reg_clk: 2818 clk_disable_unprepare(nfc->reg_clk); 2819 unprepare_core_clk: 2820 clk_disable_unprepare(nfc->core_clk); 2821 2822 return ret; 2823 } 2824 2825 static int marvell_nfc_remove(struct platform_device *pdev) 2826 { 2827 struct marvell_nfc *nfc = platform_get_drvdata(pdev); 2828 2829 marvell_nand_chips_cleanup(nfc); 2830 2831 if (nfc->use_dma) { 2832 dmaengine_terminate_all(nfc->dma_chan); 2833 dma_release_channel(nfc->dma_chan); 2834 } 2835 2836 clk_disable_unprepare(nfc->reg_clk); 2837 clk_disable_unprepare(nfc->core_clk); 2838 2839 return 0; 2840 } 2841 2842 static int __maybe_unused marvell_nfc_suspend(struct device *dev) 2843 { 2844 struct marvell_nfc *nfc = dev_get_drvdata(dev); 2845 struct marvell_nand_chip *chip; 2846 2847 list_for_each_entry(chip, &nfc->chips, node) 2848 marvell_nfc_wait_ndrun(&chip->chip); 2849 2850 clk_disable_unprepare(nfc->reg_clk); 2851 clk_disable_unprepare(nfc->core_clk); 2852 2853 return 0; 2854 } 2855 2856 static int __maybe_unused marvell_nfc_resume(struct device *dev) 2857 { 2858 struct marvell_nfc *nfc = dev_get_drvdata(dev); 2859 int ret; 2860 2861 ret = clk_prepare_enable(nfc->core_clk); 2862 if (ret < 0) 2863 return ret; 2864 2865 ret = clk_prepare_enable(nfc->reg_clk); 2866 if (ret < 0) 2867 return ret; 2868 2869 /* 2870 * Reset nfc->selected_chip so the next command will cause the timing 2871 * registers to be restored in marvell_nfc_select_chip(). 2872 */ 2873 nfc->selected_chip = NULL; 2874 2875 /* Reset registers that have lost their contents */ 2876 marvell_nfc_reset(nfc); 2877 2878 return 0; 2879 } 2880 2881 static const struct dev_pm_ops marvell_nfc_pm_ops = { 2882 SET_SYSTEM_SLEEP_PM_OPS(marvell_nfc_suspend, marvell_nfc_resume) 2883 }; 2884 2885 static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = { 2886 .max_cs_nb = 4, 2887 .max_rb_nb = 2, 2888 .need_system_controller = true, 2889 .is_nfcv2 = true, 2890 }; 2891 2892 static const struct marvell_nfc_caps marvell_armada370_nfc_caps = { 2893 .max_cs_nb = 4, 2894 .max_rb_nb = 2, 2895 .is_nfcv2 = true, 2896 }; 2897 2898 static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = { 2899 .max_cs_nb = 2, 2900 .max_rb_nb = 1, 2901 .use_dma = true, 2902 }; 2903 2904 static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = { 2905 .max_cs_nb = 4, 2906 .max_rb_nb = 2, 2907 .need_system_controller = true, 2908 .legacy_of_bindings = true, 2909 .is_nfcv2 = true, 2910 }; 2911 2912 static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = { 2913 .max_cs_nb = 4, 2914 .max_rb_nb = 2, 2915 .legacy_of_bindings = true, 2916 .is_nfcv2 = true, 2917 }; 2918 2919 static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = { 2920 .max_cs_nb = 2, 2921 .max_rb_nb = 1, 2922 .legacy_of_bindings = true, 2923 .use_dma = true, 2924 }; 2925 2926 static const struct platform_device_id marvell_nfc_platform_ids[] = { 2927 { 2928 .name = "pxa3xx-nand", 2929 .driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps, 2930 }, 2931 { /* sentinel */ }, 2932 }; 2933 MODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids); 2934 2935 static const struct of_device_id marvell_nfc_of_ids[] = { 2936 { 2937 .compatible = "marvell,armada-8k-nand-controller", 2938 .data = &marvell_armada_8k_nfc_caps, 2939 }, 2940 { 2941 .compatible = "marvell,armada370-nand-controller", 2942 .data = &marvell_armada370_nfc_caps, 2943 }, 2944 { 2945 .compatible = "marvell,pxa3xx-nand-controller", 2946 .data = &marvell_pxa3xx_nfc_caps, 2947 }, 2948 /* Support for old/deprecated bindings: */ 2949 { 2950 .compatible = "marvell,armada-8k-nand", 2951 .data = &marvell_armada_8k_nfc_legacy_caps, 2952 }, 2953 { 2954 .compatible = "marvell,armada370-nand", 2955 .data = &marvell_armada370_nfc_legacy_caps, 2956 }, 2957 { 2958 .compatible = "marvell,pxa3xx-nand", 2959 .data = &marvell_pxa3xx_nfc_legacy_caps, 2960 }, 2961 { /* sentinel */ }, 2962 }; 2963 MODULE_DEVICE_TABLE(of, marvell_nfc_of_ids); 2964 2965 static struct platform_driver marvell_nfc_driver = { 2966 .driver = { 2967 .name = "marvell-nfc", 2968 .of_match_table = marvell_nfc_of_ids, 2969 .pm = &marvell_nfc_pm_ops, 2970 }, 2971 .id_table = marvell_nfc_platform_ids, 2972 .probe = marvell_nfc_probe, 2973 .remove = marvell_nfc_remove, 2974 }; 2975 module_platform_driver(marvell_nfc_driver); 2976 2977 MODULE_LICENSE("GPL"); 2978 MODULE_DESCRIPTION("Marvell NAND controller driver"); 2979