0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077 #include <linux/module.h>
0078 #include <linux/clk.h>
0079 #include <linux/mtd/rawnand.h>
0080 #include <linux/of_platform.h>
0081 #include <linux/iopoll.h>
0082 #include <linux/interrupt.h>
0083 #include <linux/slab.h>
0084 #include <linux/mfd/syscon.h>
0085 #include <linux/regmap.h>
0086 #include <asm/unaligned.h>
0087
0088 #include <linux/dmaengine.h>
0089 #include <linux/dma-mapping.h>
0090 #include <linux/dma/pxa-dma.h>
0091 #include <linux/platform_data/mtd-nand-pxa3xx.h>
0092
0093
0094 #define FIFO_DEPTH 8
0095 #define FIFO_REP(x) (x / sizeof(u32))
0096 #define BCH_SEQ_READS (32 / FIFO_DEPTH)
0097
0098 #define MAX_CHUNK_SIZE 2112
0099
0100 #define NFCV1_READID_LEN 7
0101
0102 #define POLL_PERIOD 0
0103 #define POLL_TIMEOUT 100000
0104
0105 #define IRQ_TIMEOUT 1000
0106
0107 #define MIN_RD_DEL_CNT 3
0108
0109 #define MAX_ADDRESS_CYC_NFCV1 5
0110 #define MAX_ADDRESS_CYC_NFCV2 7
0111
0112 #define GENCONF_SOC_DEVICE_MUX 0x208
0113 #define GENCONF_SOC_DEVICE_MUX_NFC_EN BIT(0)
0114 #define GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST BIT(20)
0115 #define GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST BIT(21)
0116 #define GENCONF_SOC_DEVICE_MUX_NFC_INT_EN BIT(25)
0117 #define GENCONF_CLK_GATING_CTRL 0x220
0118 #define GENCONF_CLK_GATING_CTRL_ND_GATE BIT(2)
0119 #define GENCONF_ND_CLK_CTRL 0x700
0120 #define GENCONF_ND_CLK_CTRL_EN BIT(0)
0121
0122
0123 #define NDCR 0x00
0124 #define NDCR_ALL_INT GENMASK(11, 0)
0125 #define NDCR_CS1_CMDDM BIT(7)
0126 #define NDCR_CS0_CMDDM BIT(8)
0127 #define NDCR_RDYM BIT(11)
0128 #define NDCR_ND_ARB_EN BIT(12)
0129 #define NDCR_RA_START BIT(15)
0130 #define NDCR_RD_ID_CNT(x) (min_t(unsigned int, x, 0x7) << 16)
0131 #define NDCR_PAGE_SZ(x) (x >= 2048 ? BIT(24) : 0)
0132 #define NDCR_DWIDTH_M BIT(26)
0133 #define NDCR_DWIDTH_C BIT(27)
0134 #define NDCR_ND_RUN BIT(28)
0135 #define NDCR_DMA_EN BIT(29)
0136 #define NDCR_ECC_EN BIT(30)
0137 #define NDCR_SPARE_EN BIT(31)
0138 #define NDCR_GENERIC_FIELDS_MASK (~(NDCR_RA_START | NDCR_PAGE_SZ(2048) | \
0139 NDCR_DWIDTH_M | NDCR_DWIDTH_C))
0140
0141
0142 #define NDTR0 0x04
0143 #define NDTR0_TRP(x) ((min_t(unsigned int, x, 0xF) & 0x7) << 0)
0144 #define NDTR0_TRH(x) (min_t(unsigned int, x, 0x7) << 3)
0145 #define NDTR0_ETRP(x) ((min_t(unsigned int, x, 0xF) & 0x8) << 3)
0146 #define NDTR0_SEL_NRE_EDGE BIT(7)
0147 #define NDTR0_TWP(x) (min_t(unsigned int, x, 0x7) << 8)
0148 #define NDTR0_TWH(x) (min_t(unsigned int, x, 0x7) << 11)
0149 #define NDTR0_TCS(x) (min_t(unsigned int, x, 0x7) << 16)
0150 #define NDTR0_TCH(x) (min_t(unsigned int, x, 0x7) << 19)
0151 #define NDTR0_RD_CNT_DEL(x) (min_t(unsigned int, x, 0xF) << 22)
0152 #define NDTR0_SELCNTR BIT(26)
0153 #define NDTR0_TADL(x) (min_t(unsigned int, x, 0x1F) << 27)
0154
0155
0156 #define NDTR1 0x0C
0157 #define NDTR1_TAR(x) (min_t(unsigned int, x, 0xF) << 0)
0158 #define NDTR1_TWHR(x) (min_t(unsigned int, x, 0xF) << 4)
0159 #define NDTR1_TRHW(x) (min_t(unsigned int, x / 16, 0x3) << 8)
0160 #define NDTR1_PRESCALE BIT(14)
0161 #define NDTR1_WAIT_MODE BIT(15)
0162 #define NDTR1_TR(x) (min_t(unsigned int, x, 0xFFFF) << 16)
0163
0164
0165 #define NDSR 0x14
0166 #define NDSR_WRCMDREQ BIT(0)
0167 #define NDSR_RDDREQ BIT(1)
0168 #define NDSR_WRDREQ BIT(2)
0169 #define NDSR_CORERR BIT(3)
0170 #define NDSR_UNCERR BIT(4)
0171 #define NDSR_CMDD(cs) BIT(8 - cs)
0172 #define NDSR_RDY(rb) BIT(11 + rb)
0173 #define NDSR_ERRCNT(x) ((x >> 16) & 0x1F)
0174
0175
0176 #define NDECCCTRL 0x28
0177 #define NDECCCTRL_BCH_EN BIT(0)
0178
0179
0180 #define NDDB 0x40
0181
0182
0183 #define NDCB0 0x48
0184 #define NDCB0_CMD1(x) ((x & 0xFF) << 0)
0185 #define NDCB0_CMD2(x) ((x & 0xFF) << 8)
0186 #define NDCB0_ADDR_CYC(x) ((x & 0x7) << 16)
0187 #define NDCB0_ADDR_GET_NUM_CYC(x) (((x) >> 16) & 0x7)
0188 #define NDCB0_DBC BIT(19)
0189 #define NDCB0_CMD_TYPE(x) ((x & 0x7) << 21)
0190 #define NDCB0_CSEL BIT(24)
0191 #define NDCB0_RDY_BYP BIT(27)
0192 #define NDCB0_LEN_OVRD BIT(28)
0193 #define NDCB0_CMD_XTYPE(x) ((x & 0x7) << 29)
0194
0195
0196 #define NDCB1 0x4C
0197 #define NDCB1_COLS(x) ((x & 0xFFFF) << 0)
0198 #define NDCB1_ADDRS_PAGE(x) (x << 16)
0199
0200
0201 #define NDCB2 0x50
0202 #define NDCB2_ADDR5_PAGE(x) (((x >> 16) & 0xFF) << 0)
0203 #define NDCB2_ADDR5_CYC(x) ((x & 0xFF) << 0)
0204
0205
0206 #define NDCB3 0x54
0207 #define NDCB3_ADDR6_CYC(x) ((x & 0xFF) << 16)
0208 #define NDCB3_ADDR7_CYC(x) ((x & 0xFF) << 24)
0209
0210
0211 #define TYPE_READ 0
0212 #define TYPE_WRITE 1
0213 #define TYPE_ERASE 2
0214 #define TYPE_READ_ID 3
0215 #define TYPE_STATUS 4
0216 #define TYPE_RESET 5
0217 #define TYPE_NAKED_CMD 6
0218 #define TYPE_NAKED_ADDR 7
0219 #define TYPE_MASK 7
0220 #define XTYPE_MONOLITHIC_RW 0
0221 #define XTYPE_LAST_NAKED_RW 1
0222 #define XTYPE_FINAL_COMMAND 3
0223 #define XTYPE_READ 4
0224 #define XTYPE_WRITE_DISPATCH 4
0225 #define XTYPE_NAKED_RW 5
0226 #define XTYPE_COMMAND_DISPATCH 6
0227 #define XTYPE_MASK 7
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253 struct marvell_hw_ecc_layout {
0254
0255 int writesize;
0256 int chunk;
0257 int strength;
0258
0259 int nchunks;
0260 int full_chunk_cnt;
0261 int data_bytes;
0262 int spare_bytes;
0263 int ecc_bytes;
0264 int last_data_bytes;
0265 int last_spare_bytes;
0266 int last_ecc_bytes;
0267 };
0268
0269 #define MARVELL_LAYOUT(ws, dc, ds, nc, fcc, db, sb, eb, ldb, lsb, leb) \
0270 { \
0271 .writesize = ws, \
0272 .chunk = dc, \
0273 .strength = ds, \
0274 .nchunks = nc, \
0275 .full_chunk_cnt = fcc, \
0276 .data_bytes = db, \
0277 .spare_bytes = sb, \
0278 .ecc_bytes = eb, \
0279 .last_data_bytes = ldb, \
0280 .last_spare_bytes = lsb, \
0281 .last_ecc_bytes = leb, \
0282 }
0283
0284
0285 static const struct marvell_hw_ecc_layout marvell_nfc_layouts[] = {
0286 MARVELL_LAYOUT( 512, 512, 1, 1, 1, 512, 8, 8, 0, 0, 0),
0287 MARVELL_LAYOUT( 2048, 512, 1, 1, 1, 2048, 40, 24, 0, 0, 0),
0288 MARVELL_LAYOUT( 2048, 512, 4, 1, 1, 2048, 32, 30, 0, 0, 0),
0289 MARVELL_LAYOUT( 2048, 512, 8, 2, 1, 1024, 0, 30,1024,32, 30),
0290 MARVELL_LAYOUT( 4096, 512, 4, 2, 2, 2048, 32, 30, 0, 0, 0),
0291 MARVELL_LAYOUT( 4096, 512, 8, 5, 4, 1024, 0, 30, 0, 64, 30),
0292 MARVELL_LAYOUT( 8192, 512, 4, 4, 4, 2048, 0, 30, 0, 0, 0),
0293 MARVELL_LAYOUT( 8192, 512, 8, 9, 8, 1024, 0, 30, 0, 160, 30),
0294 };
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312 struct marvell_nand_chip_sel {
0313 unsigned int cs;
0314 u32 ndcb0_csel;
0315 unsigned int rb;
0316 };
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332 struct marvell_nand_chip {
0333 struct nand_chip chip;
0334 struct list_head node;
0335 const struct marvell_hw_ecc_layout *layout;
0336 u32 ndcr;
0337 u32 ndtr0;
0338 u32 ndtr1;
0339 int addr_cyc;
0340 int selected_die;
0341 unsigned int nsels;
0342 struct marvell_nand_chip_sel sels[];
0343 };
0344
0345 static inline struct marvell_nand_chip *to_marvell_nand(struct nand_chip *chip)
0346 {
0347 return container_of(chip, struct marvell_nand_chip, chip);
0348 }
0349
0350 static inline struct marvell_nand_chip_sel *to_nand_sel(struct marvell_nand_chip
0351 *nand)
0352 {
0353 return &nand->sels[nand->selected_die];
0354 }
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371 struct marvell_nfc_caps {
0372 unsigned int max_cs_nb;
0373 unsigned int max_rb_nb;
0374 bool need_system_controller;
0375 bool legacy_of_bindings;
0376 bool is_nfcv2;
0377 bool use_dma;
0378 };
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398 struct marvell_nfc {
0399 struct nand_controller controller;
0400 struct device *dev;
0401 void __iomem *regs;
0402 struct clk *core_clk;
0403 struct clk *reg_clk;
0404 struct completion complete;
0405 unsigned long assigned_cs;
0406 struct list_head chips;
0407 struct nand_chip *selected_chip;
0408 const struct marvell_nfc_caps *caps;
0409
0410
0411 bool use_dma;
0412 struct dma_chan *dma_chan;
0413 u8 *dma_buf;
0414 };
0415
0416 static inline struct marvell_nfc *to_marvell_nfc(struct nand_controller *ctrl)
0417 {
0418 return container_of(ctrl, struct marvell_nfc, controller);
0419 }
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437 struct marvell_nfc_timings {
0438
0439 unsigned int tRP;
0440 unsigned int tRH;
0441 unsigned int tWP;
0442 unsigned int tWH;
0443 unsigned int tCS;
0444 unsigned int tCH;
0445 unsigned int tADL;
0446
0447 unsigned int tAR;
0448 unsigned int tWHR;
0449 unsigned int tRHW;
0450 unsigned int tR;
0451 };
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462 #define TO_CYCLES(ps, period_ns) (DIV_ROUND_UP(ps / 1000, period_ns))
0463 #define TO_CYCLES64(ps, period_ns) (DIV_ROUND_UP_ULL(div_u64(ps, 1000), \
0464 period_ns))
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478 struct marvell_nfc_op {
0479 u32 ndcb[4];
0480 unsigned int cle_ale_delay_ns;
0481 unsigned int rdy_timeout_ms;
0482 unsigned int rdy_delay_ns;
0483 unsigned int data_delay_ns;
0484 unsigned int data_instr_idx;
0485 const struct nand_op_instr *data_instr;
0486 };
0487
0488
0489
0490
0491
0492 static void cond_delay(unsigned int ns)
0493 {
0494 if (!ns)
0495 return;
0496
0497 if (ns < 10000)
0498 ndelay(ns);
0499 else
0500 udelay(DIV_ROUND_UP(ns, 1000));
0501 }
0502
0503
0504
0505
0506
0507
0508 static void marvell_nfc_disable_int(struct marvell_nfc *nfc, u32 int_mask)
0509 {
0510 u32 reg;
0511
0512
0513 reg = readl_relaxed(nfc->regs + NDCR);
0514 writel_relaxed(reg | int_mask, nfc->regs + NDCR);
0515 }
0516
0517 static void marvell_nfc_enable_int(struct marvell_nfc *nfc, u32 int_mask)
0518 {
0519 u32 reg;
0520
0521
0522 reg = readl_relaxed(nfc->regs + NDCR);
0523 writel_relaxed(reg & ~int_mask, nfc->regs + NDCR);
0524 }
0525
0526 static u32 marvell_nfc_clear_int(struct marvell_nfc *nfc, u32 int_mask)
0527 {
0528 u32 reg;
0529
0530 reg = readl_relaxed(nfc->regs + NDSR);
0531 writel_relaxed(int_mask, nfc->regs + NDSR);
0532
0533 return reg & int_mask;
0534 }
0535
0536 static void marvell_nfc_force_byte_access(struct nand_chip *chip,
0537 bool force_8bit)
0538 {
0539 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
0540 u32 ndcr;
0541
0542
0543
0544
0545
0546
0547
0548 if (!(chip->options & NAND_BUSWIDTH_16))
0549 return;
0550
0551 ndcr = readl_relaxed(nfc->regs + NDCR);
0552
0553 if (force_8bit)
0554 ndcr &= ~(NDCR_DWIDTH_M | NDCR_DWIDTH_C);
0555 else
0556 ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
0557
0558 writel_relaxed(ndcr, nfc->regs + NDCR);
0559 }
0560
0561 static int marvell_nfc_wait_ndrun(struct nand_chip *chip)
0562 {
0563 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
0564 u32 val;
0565 int ret;
0566
0567
0568
0569
0570
0571 ret = readl_relaxed_poll_timeout(nfc->regs + NDCR, val,
0572 (val & NDCR_ND_RUN) == 0,
0573 POLL_PERIOD, POLL_TIMEOUT);
0574 if (ret) {
0575 dev_err(nfc->dev, "Timeout on NAND controller run mode\n");
0576 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
0577 nfc->regs + NDCR);
0578 return ret;
0579 }
0580
0581 return 0;
0582 }
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599 static int marvell_nfc_prepare_cmd(struct nand_chip *chip)
0600 {
0601 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
0602 u32 ndcr, val;
0603 int ret;
0604
0605
0606 ret = marvell_nfc_wait_ndrun(chip);
0607 if (ret) {
0608 dev_err(nfc->dev, "Last operation did not succeed\n");
0609 return ret;
0610 }
0611
0612 ndcr = readl_relaxed(nfc->regs + NDCR);
0613 writel_relaxed(readl(nfc->regs + NDSR), nfc->regs + NDSR);
0614
0615
0616 writel_relaxed(ndcr | NDCR_ND_RUN, nfc->regs + NDCR);
0617 ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
0618 val & NDSR_WRCMDREQ,
0619 POLL_PERIOD, POLL_TIMEOUT);
0620 if (ret) {
0621 dev_err(nfc->dev, "Timeout on WRCMDRE\n");
0622 return -ETIMEDOUT;
0623 }
0624
0625
0626 writel_relaxed(NDSR_WRCMDREQ, nfc->regs + NDSR);
0627
0628 return 0;
0629 }
0630
0631 static void marvell_nfc_send_cmd(struct nand_chip *chip,
0632 struct marvell_nfc_op *nfc_op)
0633 {
0634 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
0635 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
0636
0637 dev_dbg(nfc->dev, "\nNDCR: 0x%08x\n"
0638 "NDCB0: 0x%08x\nNDCB1: 0x%08x\nNDCB2: 0x%08x\nNDCB3: 0x%08x\n",
0639 (u32)readl_relaxed(nfc->regs + NDCR), nfc_op->ndcb[0],
0640 nfc_op->ndcb[1], nfc_op->ndcb[2], nfc_op->ndcb[3]);
0641
0642 writel_relaxed(to_nand_sel(marvell_nand)->ndcb0_csel | nfc_op->ndcb[0],
0643 nfc->regs + NDCB0);
0644 writel_relaxed(nfc_op->ndcb[1], nfc->regs + NDCB0);
0645 writel(nfc_op->ndcb[2], nfc->regs + NDCB0);
0646
0647
0648
0649
0650
0651 if (nfc_op->ndcb[0] & NDCB0_LEN_OVRD ||
0652 NDCB0_ADDR_GET_NUM_CYC(nfc_op->ndcb[0]) >= 6) {
0653 if (!WARN_ON_ONCE(!nfc->caps->is_nfcv2))
0654 writel(nfc_op->ndcb[3], nfc->regs + NDCB0);
0655 }
0656 }
0657
0658 static int marvell_nfc_end_cmd(struct nand_chip *chip, int flag,
0659 const char *label)
0660 {
0661 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
0662 u32 val;
0663 int ret;
0664
0665 ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
0666 val & flag,
0667 POLL_PERIOD, POLL_TIMEOUT);
0668
0669 if (ret) {
0670 dev_err(nfc->dev, "Timeout on %s (NDSR: 0x%08x)\n",
0671 label, val);
0672 if (nfc->dma_chan)
0673 dmaengine_terminate_all(nfc->dma_chan);
0674 return ret;
0675 }
0676
0677
0678
0679
0680
0681 if (nfc->use_dma && (readl_relaxed(nfc->regs + NDCR) & NDCR_DMA_EN))
0682 return 0;
0683
0684 writel_relaxed(flag, nfc->regs + NDSR);
0685
0686 return 0;
0687 }
0688
0689 static int marvell_nfc_wait_cmdd(struct nand_chip *chip)
0690 {
0691 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
0692 int cs_flag = NDSR_CMDD(to_nand_sel(marvell_nand)->ndcb0_csel);
0693
0694 return marvell_nfc_end_cmd(chip, cs_flag, "CMDD");
0695 }
0696
0697 static int marvell_nfc_poll_status(struct marvell_nfc *nfc, u32 mask,
0698 u32 expected_val, unsigned long timeout_ms)
0699 {
0700 unsigned long limit;
0701 u32 st;
0702
0703 limit = jiffies + msecs_to_jiffies(timeout_ms);
0704 do {
0705 st = readl_relaxed(nfc->regs + NDSR);
0706 if (st & NDSR_RDY(1))
0707 st |= NDSR_RDY(0);
0708
0709 if ((st & mask) == expected_val)
0710 return 0;
0711
0712 cpu_relax();
0713 } while (time_after(limit, jiffies));
0714
0715 return -ETIMEDOUT;
0716 }
0717
0718 static int marvell_nfc_wait_op(struct nand_chip *chip, unsigned int timeout_ms)
0719 {
0720 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
0721 struct mtd_info *mtd = nand_to_mtd(chip);
0722 u32 pending;
0723 int ret;
0724
0725
0726 if (!timeout_ms)
0727 timeout_ms = IRQ_TIMEOUT;
0728
0729 if (mtd->oops_panic_write) {
0730 ret = marvell_nfc_poll_status(nfc, NDSR_RDY(0),
0731 NDSR_RDY(0),
0732 timeout_ms);
0733 } else {
0734 init_completion(&nfc->complete);
0735
0736 marvell_nfc_enable_int(nfc, NDCR_RDYM);
0737 ret = wait_for_completion_timeout(&nfc->complete,
0738 msecs_to_jiffies(timeout_ms));
0739 marvell_nfc_disable_int(nfc, NDCR_RDYM);
0740 }
0741 pending = marvell_nfc_clear_int(nfc, NDSR_RDY(0) | NDSR_RDY(1));
0742
0743
0744
0745
0746
0747 if (!ret && !pending) {
0748 dev_err(nfc->dev, "Timeout waiting for RB signal\n");
0749 return -ETIMEDOUT;
0750 }
0751
0752 return 0;
0753 }
0754
0755 static void marvell_nfc_select_target(struct nand_chip *chip,
0756 unsigned int die_nr)
0757 {
0758 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
0759 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
0760 u32 ndcr_generic;
0761
0762
0763
0764
0765
0766 ndcr_generic = readl_relaxed(nfc->regs + NDCR) &
0767 NDCR_GENERIC_FIELDS_MASK & ~NDCR_ND_RUN;
0768 writel_relaxed(ndcr_generic | marvell_nand->ndcr, nfc->regs + NDCR);
0769
0770
0771 marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
0772
0773 if (chip == nfc->selected_chip && die_nr == marvell_nand->selected_die)
0774 return;
0775
0776 writel_relaxed(marvell_nand->ndtr0, nfc->regs + NDTR0);
0777 writel_relaxed(marvell_nand->ndtr1, nfc->regs + NDTR1);
0778
0779 nfc->selected_chip = chip;
0780 marvell_nand->selected_die = die_nr;
0781 }
0782
0783 static irqreturn_t marvell_nfc_isr(int irq, void *dev_id)
0784 {
0785 struct marvell_nfc *nfc = dev_id;
0786 u32 st = readl_relaxed(nfc->regs + NDSR);
0787 u32 ien = (~readl_relaxed(nfc->regs + NDCR)) & NDCR_ALL_INT;
0788
0789
0790
0791
0792
0793 if (st & NDSR_RDY(1))
0794 st |= NDSR_RDY(0);
0795
0796 if (!(st & ien))
0797 return IRQ_NONE;
0798
0799 marvell_nfc_disable_int(nfc, st & NDCR_ALL_INT);
0800
0801 if (st & (NDSR_RDY(0) | NDSR_RDY(1)))
0802 complete(&nfc->complete);
0803
0804 return IRQ_HANDLED;
0805 }
0806
0807
0808 static void marvell_nfc_enable_hw_ecc(struct nand_chip *chip)
0809 {
0810 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
0811 u32 ndcr = readl_relaxed(nfc->regs + NDCR);
0812
0813 if (!(ndcr & NDCR_ECC_EN)) {
0814 writel_relaxed(ndcr | NDCR_ECC_EN, nfc->regs + NDCR);
0815
0816
0817
0818
0819
0820 if (chip->ecc.algo == NAND_ECC_ALGO_BCH)
0821 writel_relaxed(NDECCCTRL_BCH_EN, nfc->regs + NDECCCTRL);
0822 }
0823 }
0824
0825 static void marvell_nfc_disable_hw_ecc(struct nand_chip *chip)
0826 {
0827 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
0828 u32 ndcr = readl_relaxed(nfc->regs + NDCR);
0829
0830 if (ndcr & NDCR_ECC_EN) {
0831 writel_relaxed(ndcr & ~NDCR_ECC_EN, nfc->regs + NDCR);
0832 if (chip->ecc.algo == NAND_ECC_ALGO_BCH)
0833 writel_relaxed(0, nfc->regs + NDECCCTRL);
0834 }
0835 }
0836
0837
0838 static void marvell_nfc_enable_dma(struct marvell_nfc *nfc)
0839 {
0840 u32 reg;
0841
0842 reg = readl_relaxed(nfc->regs + NDCR);
0843 writel_relaxed(reg | NDCR_DMA_EN, nfc->regs + NDCR);
0844 }
0845
0846 static void marvell_nfc_disable_dma(struct marvell_nfc *nfc)
0847 {
0848 u32 reg;
0849
0850 reg = readl_relaxed(nfc->regs + NDCR);
0851 writel_relaxed(reg & ~NDCR_DMA_EN, nfc->regs + NDCR);
0852 }
0853
0854
0855 static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
0856 enum dma_data_direction direction,
0857 unsigned int len)
0858 {
0859 unsigned int dma_len = min_t(int, ALIGN(len, 32), MAX_CHUNK_SIZE);
0860 struct dma_async_tx_descriptor *tx;
0861 struct scatterlist sg;
0862 dma_cookie_t cookie;
0863 int ret;
0864
0865 marvell_nfc_enable_dma(nfc);
0866
0867 sg_init_one(&sg, nfc->dma_buf, dma_len);
0868 dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
0869 tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1,
0870 direction == DMA_FROM_DEVICE ?
0871 DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
0872 DMA_PREP_INTERRUPT);
0873 if (!tx) {
0874 dev_err(nfc->dev, "Could not prepare DMA S/G list\n");
0875 return -ENXIO;
0876 }
0877
0878
0879 cookie = dmaengine_submit(tx);
0880 ret = dma_submit_error(cookie);
0881 if (ret)
0882 return -EIO;
0883
0884 dma_async_issue_pending(nfc->dma_chan);
0885 ret = marvell_nfc_wait_cmdd(nfc->selected_chip);
0886 dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
0887 marvell_nfc_disable_dma(nfc);
0888 if (ret) {
0889 dev_err(nfc->dev, "Timeout waiting for DMA (status: %d)\n",
0890 dmaengine_tx_status(nfc->dma_chan, cookie, NULL));
0891 dmaengine_terminate_all(nfc->dma_chan);
0892 return -ETIMEDOUT;
0893 }
0894
0895 return 0;
0896 }
0897
0898 static int marvell_nfc_xfer_data_in_pio(struct marvell_nfc *nfc, u8 *in,
0899 unsigned int len)
0900 {
0901 unsigned int last_len = len % FIFO_DEPTH;
0902 unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
0903 int i;
0904
0905 for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
0906 ioread32_rep(nfc->regs + NDDB, in + i, FIFO_REP(FIFO_DEPTH));
0907
0908 if (last_len) {
0909 u8 tmp_buf[FIFO_DEPTH];
0910
0911 ioread32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
0912 memcpy(in + last_full_offset, tmp_buf, last_len);
0913 }
0914
0915 return 0;
0916 }
0917
0918 static int marvell_nfc_xfer_data_out_pio(struct marvell_nfc *nfc, const u8 *out,
0919 unsigned int len)
0920 {
0921 unsigned int last_len = len % FIFO_DEPTH;
0922 unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
0923 int i;
0924
0925 for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
0926 iowrite32_rep(nfc->regs + NDDB, out + i, FIFO_REP(FIFO_DEPTH));
0927
0928 if (last_len) {
0929 u8 tmp_buf[FIFO_DEPTH];
0930
0931 memcpy(tmp_buf, out + last_full_offset, last_len);
0932 iowrite32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
0933 }
0934
0935 return 0;
0936 }
0937
0938 static void marvell_nfc_check_empty_chunk(struct nand_chip *chip,
0939 u8 *data, int data_len,
0940 u8 *spare, int spare_len,
0941 u8 *ecc, int ecc_len,
0942 unsigned int *max_bitflips)
0943 {
0944 struct mtd_info *mtd = nand_to_mtd(chip);
0945 int bf;
0946
0947
0948
0949
0950
0951
0952 if (!data)
0953 data_len = 0;
0954 if (!spare)
0955 spare_len = 0;
0956 if (!ecc)
0957 ecc_len = 0;
0958
0959 bf = nand_check_erased_ecc_chunk(data, data_len, ecc, ecc_len,
0960 spare, spare_len, chip->ecc.strength);
0961 if (bf < 0) {
0962 mtd->ecc_stats.failed++;
0963 return;
0964 }
0965
0966
0967 mtd->ecc_stats.corrected += bf;
0968 *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
0969 }
0970
0971
0972
0973
0974
0975
0976
0977
0978 static int marvell_nfc_hw_ecc_check_bitflips(struct nand_chip *chip,
0979 unsigned int *max_bitflips)
0980 {
0981 struct mtd_info *mtd = nand_to_mtd(chip);
0982 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
0983 int bf = 0;
0984 u32 ndsr;
0985
0986 ndsr = readl_relaxed(nfc->regs + NDSR);
0987
0988
0989 if (ndsr & NDSR_UNCERR) {
0990 writel_relaxed(ndsr, nfc->regs + NDSR);
0991
0992
0993
0994
0995
0996
0997
0998
0999 return -EBADMSG;
1000 }
1001
1002
1003 if (ndsr & NDSR_CORERR) {
1004 writel_relaxed(ndsr, nfc->regs + NDSR);
1005
1006 if (chip->ecc.algo == NAND_ECC_ALGO_BCH)
1007 bf = NDSR_ERRCNT(ndsr);
1008 else
1009 bf = 1;
1010 }
1011
1012
1013 mtd->ecc_stats.corrected += bf;
1014 *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
1015
1016 return 0;
1017 }
1018
1019
1020 static int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip *chip,
1021 u8 *data_buf, u8 *oob_buf,
1022 bool raw, int page)
1023 {
1024 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1025 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1026 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1027 struct marvell_nfc_op nfc_op = {
1028 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
1029 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1030 NDCB0_DBC |
1031 NDCB0_CMD1(NAND_CMD_READ0) |
1032 NDCB0_CMD2(NAND_CMD_READSTART),
1033 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1034 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1035 };
1036 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
1037 int ret;
1038
1039
1040 if (nfc->caps->is_nfcv2)
1041 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1042
1043 ret = marvell_nfc_prepare_cmd(chip);
1044 if (ret)
1045 return ret;
1046
1047 marvell_nfc_send_cmd(chip, &nfc_op);
1048 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1049 "RDDREQ while draining FIFO (data/oob)");
1050 if (ret)
1051 return ret;
1052
1053
1054
1055
1056
1057
1058
1059 if (nfc->use_dma) {
1060 marvell_nfc_xfer_data_dma(nfc, DMA_FROM_DEVICE,
1061 lt->data_bytes + oob_bytes);
1062 memcpy(data_buf, nfc->dma_buf, lt->data_bytes);
1063 memcpy(oob_buf, nfc->dma_buf + lt->data_bytes, oob_bytes);
1064 } else {
1065 marvell_nfc_xfer_data_in_pio(nfc, data_buf, lt->data_bytes);
1066 marvell_nfc_xfer_data_in_pio(nfc, oob_buf, oob_bytes);
1067 }
1068
1069 ret = marvell_nfc_wait_cmdd(chip);
1070 return ret;
1071 }
1072
1073 static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct nand_chip *chip, u8 *buf,
1074 int oob_required, int page)
1075 {
1076 marvell_nfc_select_target(chip, chip->cur_cs);
1077 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
1078 true, page);
1079 }
1080
1081 static int marvell_nfc_hw_ecc_hmg_read_page(struct nand_chip *chip, u8 *buf,
1082 int oob_required, int page)
1083 {
1084 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1085 unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1086 int max_bitflips = 0, ret;
1087 u8 *raw_buf;
1088
1089 marvell_nfc_select_target(chip, chip->cur_cs);
1090 marvell_nfc_enable_hw_ecc(chip);
1091 marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false,
1092 page);
1093 ret = marvell_nfc_hw_ecc_check_bitflips(chip, &max_bitflips);
1094 marvell_nfc_disable_hw_ecc(chip);
1095
1096 if (!ret)
1097 return max_bitflips;
1098
1099
1100
1101
1102
1103 raw_buf = kmalloc(full_sz, GFP_KERNEL);
1104 if (!raw_buf)
1105 return -ENOMEM;
1106
1107 marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf +
1108 lt->data_bytes, true, page);
1109 marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0,
1110 &max_bitflips);
1111 kfree(raw_buf);
1112
1113 return max_bitflips;
1114 }
1115
1116
1117
1118
1119
1120
1121 static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct nand_chip *chip, int page)
1122 {
1123 u8 *buf = nand_get_data_buf(chip);
1124
1125 marvell_nfc_select_target(chip, chip->cur_cs);
1126 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
1127 true, page);
1128 }
1129
1130
1131 static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip,
1132 const u8 *data_buf,
1133 const u8 *oob_buf, bool raw,
1134 int page)
1135 {
1136 const struct nand_sdr_timings *sdr =
1137 nand_get_sdr_timings(nand_get_interface_config(chip));
1138 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1139 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1140 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1141 struct marvell_nfc_op nfc_op = {
1142 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) |
1143 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1144 NDCB0_CMD1(NAND_CMD_SEQIN) |
1145 NDCB0_CMD2(NAND_CMD_PAGEPROG) |
1146 NDCB0_DBC,
1147 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1148 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1149 };
1150 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
1151 int ret;
1152
1153
1154 if (nfc->caps->is_nfcv2)
1155 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1156
1157 ret = marvell_nfc_prepare_cmd(chip);
1158 if (ret)
1159 return ret;
1160
1161 marvell_nfc_send_cmd(chip, &nfc_op);
1162 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1163 "WRDREQ while loading FIFO (data)");
1164 if (ret)
1165 return ret;
1166
1167
1168 if (nfc->use_dma) {
1169 memcpy(nfc->dma_buf, data_buf, lt->data_bytes);
1170 memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes);
1171 marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes +
1172 lt->ecc_bytes + lt->spare_bytes);
1173 } else {
1174 marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes);
1175 marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes);
1176 }
1177
1178 ret = marvell_nfc_wait_cmdd(chip);
1179 if (ret)
1180 return ret;
1181
1182 ret = marvell_nfc_wait_op(chip,
1183 PSEC_TO_MSEC(sdr->tPROG_max));
1184 return ret;
1185 }
1186
1187 static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct nand_chip *chip,
1188 const u8 *buf,
1189 int oob_required, int page)
1190 {
1191 marvell_nfc_select_target(chip, chip->cur_cs);
1192 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1193 true, page);
1194 }
1195
1196 static int marvell_nfc_hw_ecc_hmg_write_page(struct nand_chip *chip,
1197 const u8 *buf,
1198 int oob_required, int page)
1199 {
1200 int ret;
1201
1202 marvell_nfc_select_target(chip, chip->cur_cs);
1203 marvell_nfc_enable_hw_ecc(chip);
1204 ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1205 false, page);
1206 marvell_nfc_disable_hw_ecc(chip);
1207
1208 return ret;
1209 }
1210
1211
1212
1213
1214
1215
1216 static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct nand_chip *chip,
1217 int page)
1218 {
1219 struct mtd_info *mtd = nand_to_mtd(chip);
1220 u8 *buf = nand_get_data_buf(chip);
1221
1222 memset(buf, 0xFF, mtd->writesize);
1223
1224 marvell_nfc_select_target(chip, chip->cur_cs);
1225 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1226 true, page);
1227 }
1228
1229
1230 static int marvell_nfc_hw_ecc_bch_read_page_raw(struct nand_chip *chip, u8 *buf,
1231 int oob_required, int page)
1232 {
1233 struct mtd_info *mtd = nand_to_mtd(chip);
1234 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1235 u8 *oob = chip->oob_poi;
1236 int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1237 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1238 lt->last_spare_bytes;
1239 int data_len = lt->data_bytes;
1240 int spare_len = lt->spare_bytes;
1241 int ecc_len = lt->ecc_bytes;
1242 int chunk;
1243
1244 marvell_nfc_select_target(chip, chip->cur_cs);
1245
1246 if (oob_required)
1247 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1248
1249 nand_read_page_op(chip, page, 0, NULL, 0);
1250
1251 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1252
1253 if (chunk >= lt->full_chunk_cnt) {
1254 data_len = lt->last_data_bytes;
1255 spare_len = lt->last_spare_bytes;
1256 ecc_len = lt->last_ecc_bytes;
1257 }
1258
1259
1260 nand_change_read_column_op(chip, chunk * chunk_size,
1261 buf + (lt->data_bytes * chunk),
1262 data_len, false);
1263
1264
1265 nand_read_data_op(chip, oob + (lt->spare_bytes * chunk),
1266 spare_len, false, false);
1267
1268
1269 nand_read_data_op(chip, oob + ecc_offset +
1270 (ALIGN(lt->ecc_bytes, 32) * chunk),
1271 ecc_len, false, false);
1272 }
1273
1274 return 0;
1275 }
1276
1277 static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk,
1278 u8 *data, unsigned int data_len,
1279 u8 *spare, unsigned int spare_len,
1280 int page)
1281 {
1282 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1283 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1284 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1285 int i, ret;
1286 struct marvell_nfc_op nfc_op = {
1287 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
1288 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1289 NDCB0_LEN_OVRD,
1290 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1291 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1292 .ndcb[3] = data_len + spare_len,
1293 };
1294
1295 ret = marvell_nfc_prepare_cmd(chip);
1296 if (ret)
1297 return;
1298
1299 if (chunk == 0)
1300 nfc_op.ndcb[0] |= NDCB0_DBC |
1301 NDCB0_CMD1(NAND_CMD_READ0) |
1302 NDCB0_CMD2(NAND_CMD_READSTART);
1303
1304
1305
1306
1307
1308 if (chunk == 0)
1309 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1310 else if (chunk < lt->nchunks - 1)
1311 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
1312 else
1313 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1314
1315 marvell_nfc_send_cmd(chip, &nfc_op);
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327 for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1328 marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1329 "RDDREQ while draining FIFO (data)");
1330 marvell_nfc_xfer_data_in_pio(nfc, data,
1331 FIFO_DEPTH * BCH_SEQ_READS);
1332 data += FIFO_DEPTH * BCH_SEQ_READS;
1333 }
1334
1335 for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1336 marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1337 "RDDREQ while draining FIFO (OOB)");
1338 marvell_nfc_xfer_data_in_pio(nfc, spare,
1339 FIFO_DEPTH * BCH_SEQ_READS);
1340 spare += FIFO_DEPTH * BCH_SEQ_READS;
1341 }
1342 }
1343
1344 static int marvell_nfc_hw_ecc_bch_read_page(struct nand_chip *chip,
1345 u8 *buf, int oob_required,
1346 int page)
1347 {
1348 struct mtd_info *mtd = nand_to_mtd(chip);
1349 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1350 int data_len = lt->data_bytes, spare_len = lt->spare_bytes;
1351 u8 *data = buf, *spare = chip->oob_poi;
1352 int max_bitflips = 0;
1353 u32 failure_mask = 0;
1354 int chunk, ret;
1355
1356 marvell_nfc_select_target(chip, chip->cur_cs);
1357
1358
1359
1360
1361
1362
1363 if (oob_required)
1364 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1365
1366 marvell_nfc_enable_hw_ecc(chip);
1367
1368 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1369
1370 if (chunk >= lt->full_chunk_cnt) {
1371 data_len = lt->last_data_bytes;
1372 spare_len = lt->last_spare_bytes;
1373 }
1374
1375
1376 marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len,
1377 spare, spare_len, page);
1378 ret = marvell_nfc_hw_ecc_check_bitflips(chip, &max_bitflips);
1379 if (ret)
1380 failure_mask |= BIT(chunk);
1381
1382 data += data_len;
1383 spare += spare_len;
1384 }
1385
1386 marvell_nfc_disable_hw_ecc(chip);
1387
1388 if (!failure_mask)
1389 return max_bitflips;
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1411 int data_off_in_page, spare_off_in_page, ecc_off_in_page;
1412 int data_off, spare_off, ecc_off;
1413 int data_len, spare_len, ecc_len;
1414
1415
1416 if (!(failure_mask & BIT(chunk)))
1417 continue;
1418
1419 data_off_in_page = chunk * (lt->data_bytes + lt->spare_bytes +
1420 lt->ecc_bytes);
1421 spare_off_in_page = data_off_in_page +
1422 (chunk < lt->full_chunk_cnt ? lt->data_bytes :
1423 lt->last_data_bytes);
1424 ecc_off_in_page = spare_off_in_page +
1425 (chunk < lt->full_chunk_cnt ? lt->spare_bytes :
1426 lt->last_spare_bytes);
1427
1428 data_off = chunk * lt->data_bytes;
1429 spare_off = chunk * lt->spare_bytes;
1430 ecc_off = (lt->full_chunk_cnt * lt->spare_bytes) +
1431 lt->last_spare_bytes +
1432 (chunk * (lt->ecc_bytes + 2));
1433
1434 data_len = chunk < lt->full_chunk_cnt ? lt->data_bytes :
1435 lt->last_data_bytes;
1436 spare_len = chunk < lt->full_chunk_cnt ? lt->spare_bytes :
1437 lt->last_spare_bytes;
1438 ecc_len = chunk < lt->full_chunk_cnt ? lt->ecc_bytes :
1439 lt->last_ecc_bytes;
1440
1441
1442
1443
1444
1445
1446
1447 if (lt->writesize == 2048 && lt->strength == 8) {
1448 nand_change_read_column_op(chip, data_off_in_page,
1449 buf + data_off, data_len,
1450 false);
1451 nand_change_read_column_op(chip, spare_off_in_page,
1452 chip->oob_poi + spare_off, spare_len,
1453 false);
1454 }
1455
1456 nand_change_read_column_op(chip, ecc_off_in_page,
1457 chip->oob_poi + ecc_off, ecc_len,
1458 false);
1459
1460
1461 marvell_nfc_check_empty_chunk(chip, buf + data_off, data_len,
1462 chip->oob_poi + spare_off, spare_len,
1463 chip->oob_poi + ecc_off, ecc_len,
1464 &max_bitflips);
1465 }
1466
1467 return max_bitflips;
1468 }
1469
1470 static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct nand_chip *chip, int page)
1471 {
1472 u8 *buf = nand_get_data_buf(chip);
1473
1474 return chip->ecc.read_page_raw(chip, buf, true, page);
1475 }
1476
1477 static int marvell_nfc_hw_ecc_bch_read_oob(struct nand_chip *chip, int page)
1478 {
1479 u8 *buf = nand_get_data_buf(chip);
1480
1481 return chip->ecc.read_page(chip, buf, true, page);
1482 }
1483
1484
1485 static int marvell_nfc_hw_ecc_bch_write_page_raw(struct nand_chip *chip,
1486 const u8 *buf,
1487 int oob_required, int page)
1488 {
1489 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1490 int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1491 int data_len = lt->data_bytes;
1492 int spare_len = lt->spare_bytes;
1493 int ecc_len = lt->ecc_bytes;
1494 int spare_offset = 0;
1495 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1496 lt->last_spare_bytes;
1497 int chunk;
1498
1499 marvell_nfc_select_target(chip, chip->cur_cs);
1500
1501 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1502
1503 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1504 if (chunk >= lt->full_chunk_cnt) {
1505 data_len = lt->last_data_bytes;
1506 spare_len = lt->last_spare_bytes;
1507 ecc_len = lt->last_ecc_bytes;
1508 }
1509
1510
1511 nand_change_write_column_op(chip, chunk * full_chunk_size,
1512 NULL, 0, false);
1513
1514
1515 nand_write_data_op(chip, buf + (chunk * lt->data_bytes),
1516 data_len, false);
1517
1518 if (!oob_required)
1519 continue;
1520
1521
1522 if (spare_len)
1523 nand_write_data_op(chip, chip->oob_poi + spare_offset,
1524 spare_len, false);
1525
1526
1527 if (ecc_len)
1528 nand_write_data_op(chip, chip->oob_poi + ecc_offset,
1529 ecc_len, false);
1530
1531 spare_offset += spare_len;
1532 ecc_offset += ALIGN(ecc_len, 32);
1533 }
1534
1535 return nand_prog_page_end_op(chip);
1536 }
1537
1538 static int
1539 marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk,
1540 const u8 *data, unsigned int data_len,
1541 const u8 *spare, unsigned int spare_len,
1542 int page)
1543 {
1544 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1545 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1546 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1547 u32 xtype;
1548 int ret;
1549 struct marvell_nfc_op nfc_op = {
1550 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD,
1551 .ndcb[3] = data_len + spare_len,
1552 };
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562 if (chunk == 0) {
1563 if (lt->nchunks == 1)
1564 xtype = XTYPE_MONOLITHIC_RW;
1565 else
1566 xtype = XTYPE_WRITE_DISPATCH;
1567
1568 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(xtype) |
1569 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1570 NDCB0_CMD1(NAND_CMD_SEQIN);
1571 nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page);
1572 nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page);
1573 } else if (chunk < lt->nchunks - 1) {
1574 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
1575 } else {
1576 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1577 }
1578
1579
1580 if (chunk == lt->nchunks - 1)
1581 nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC;
1582
1583 ret = marvell_nfc_prepare_cmd(chip);
1584 if (ret)
1585 return ret;
1586
1587 marvell_nfc_send_cmd(chip, &nfc_op);
1588 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1589 "WRDREQ while loading FIFO (data)");
1590 if (ret)
1591 return ret;
1592
1593
1594 iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len));
1595 iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len));
1596
1597 return 0;
1598 }
1599
1600 static int marvell_nfc_hw_ecc_bch_write_page(struct nand_chip *chip,
1601 const u8 *buf,
1602 int oob_required, int page)
1603 {
1604 const struct nand_sdr_timings *sdr =
1605 nand_get_sdr_timings(nand_get_interface_config(chip));
1606 struct mtd_info *mtd = nand_to_mtd(chip);
1607 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1608 const u8 *data = buf;
1609 const u8 *spare = chip->oob_poi;
1610 int data_len = lt->data_bytes;
1611 int spare_len = lt->spare_bytes;
1612 int chunk, ret;
1613
1614 marvell_nfc_select_target(chip, chip->cur_cs);
1615
1616
1617 if (!oob_required)
1618 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1619
1620 marvell_nfc_enable_hw_ecc(chip);
1621
1622 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1623 if (chunk >= lt->full_chunk_cnt) {
1624 data_len = lt->last_data_bytes;
1625 spare_len = lt->last_spare_bytes;
1626 }
1627
1628 marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len,
1629 spare, spare_len, page);
1630 data += data_len;
1631 spare += spare_len;
1632
1633
1634
1635
1636
1637
1638
1639 marvell_nfc_wait_ndrun(chip);
1640 }
1641
1642 ret = marvell_nfc_wait_op(chip, PSEC_TO_MSEC(sdr->tPROG_max));
1643
1644 marvell_nfc_disable_hw_ecc(chip);
1645
1646 if (ret)
1647 return ret;
1648
1649 return 0;
1650 }
1651
1652 static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct nand_chip *chip,
1653 int page)
1654 {
1655 struct mtd_info *mtd = nand_to_mtd(chip);
1656 u8 *buf = nand_get_data_buf(chip);
1657
1658 memset(buf, 0xFF, mtd->writesize);
1659
1660 return chip->ecc.write_page_raw(chip, buf, true, page);
1661 }
1662
1663 static int marvell_nfc_hw_ecc_bch_write_oob(struct nand_chip *chip, int page)
1664 {
1665 struct mtd_info *mtd = nand_to_mtd(chip);
1666 u8 *buf = nand_get_data_buf(chip);
1667
1668 memset(buf, 0xFF, mtd->writesize);
1669
1670 return chip->ecc.write_page(chip, buf, true, page);
1671 }
1672
1673
1674 static void marvell_nfc_parse_instructions(struct nand_chip *chip,
1675 const struct nand_subop *subop,
1676 struct marvell_nfc_op *nfc_op)
1677 {
1678 const struct nand_op_instr *instr = NULL;
1679 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1680 bool first_cmd = true;
1681 unsigned int op_id;
1682 int i;
1683
1684
1685 memset(nfc_op, 0, sizeof(struct marvell_nfc_op));
1686
1687 for (op_id = 0; op_id < subop->ninstrs; op_id++) {
1688 unsigned int offset, naddrs;
1689 const u8 *addrs;
1690 int len;
1691
1692 instr = &subop->instrs[op_id];
1693
1694 switch (instr->type) {
1695 case NAND_OP_CMD_INSTR:
1696 if (first_cmd)
1697 nfc_op->ndcb[0] |=
1698 NDCB0_CMD1(instr->ctx.cmd.opcode);
1699 else
1700 nfc_op->ndcb[0] |=
1701 NDCB0_CMD2(instr->ctx.cmd.opcode) |
1702 NDCB0_DBC;
1703
1704 nfc_op->cle_ale_delay_ns = instr->delay_ns;
1705 first_cmd = false;
1706 break;
1707
1708 case NAND_OP_ADDR_INSTR:
1709 offset = nand_subop_get_addr_start_off(subop, op_id);
1710 naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
1711 addrs = &instr->ctx.addr.addrs[offset];
1712
1713 nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs);
1714
1715 for (i = 0; i < min_t(unsigned int, 4, naddrs); i++)
1716 nfc_op->ndcb[1] |= addrs[i] << (8 * i);
1717
1718 if (naddrs >= 5)
1719 nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]);
1720 if (naddrs >= 6)
1721 nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]);
1722 if (naddrs == 7)
1723 nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]);
1724
1725 nfc_op->cle_ale_delay_ns = instr->delay_ns;
1726 break;
1727
1728 case NAND_OP_DATA_IN_INSTR:
1729 nfc_op->data_instr = instr;
1730 nfc_op->data_instr_idx = op_id;
1731 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ);
1732 if (nfc->caps->is_nfcv2) {
1733 nfc_op->ndcb[0] |=
1734 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1735 NDCB0_LEN_OVRD;
1736 len = nand_subop_get_data_len(subop, op_id);
1737 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1738 }
1739 nfc_op->data_delay_ns = instr->delay_ns;
1740 break;
1741
1742 case NAND_OP_DATA_OUT_INSTR:
1743 nfc_op->data_instr = instr;
1744 nfc_op->data_instr_idx = op_id;
1745 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE);
1746 if (nfc->caps->is_nfcv2) {
1747 nfc_op->ndcb[0] |=
1748 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1749 NDCB0_LEN_OVRD;
1750 len = nand_subop_get_data_len(subop, op_id);
1751 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1752 }
1753 nfc_op->data_delay_ns = instr->delay_ns;
1754 break;
1755
1756 case NAND_OP_WAITRDY_INSTR:
1757 nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
1758 nfc_op->rdy_delay_ns = instr->delay_ns;
1759 break;
1760 }
1761 }
1762 }
1763
1764 static int marvell_nfc_xfer_data_pio(struct nand_chip *chip,
1765 const struct nand_subop *subop,
1766 struct marvell_nfc_op *nfc_op)
1767 {
1768 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1769 const struct nand_op_instr *instr = nfc_op->data_instr;
1770 unsigned int op_id = nfc_op->data_instr_idx;
1771 unsigned int len = nand_subop_get_data_len(subop, op_id);
1772 unsigned int offset = nand_subop_get_data_start_off(subop, op_id);
1773 bool reading = (instr->type == NAND_OP_DATA_IN_INSTR);
1774 int ret;
1775
1776 if (instr->ctx.data.force_8bit)
1777 marvell_nfc_force_byte_access(chip, true);
1778
1779 if (reading) {
1780 u8 *in = instr->ctx.data.buf.in + offset;
1781
1782 ret = marvell_nfc_xfer_data_in_pio(nfc, in, len);
1783 } else {
1784 const u8 *out = instr->ctx.data.buf.out + offset;
1785
1786 ret = marvell_nfc_xfer_data_out_pio(nfc, out, len);
1787 }
1788
1789 if (instr->ctx.data.force_8bit)
1790 marvell_nfc_force_byte_access(chip, false);
1791
1792 return ret;
1793 }
1794
1795 static int marvell_nfc_monolithic_access_exec(struct nand_chip *chip,
1796 const struct nand_subop *subop)
1797 {
1798 struct marvell_nfc_op nfc_op;
1799 bool reading;
1800 int ret;
1801
1802 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1803 reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR);
1804
1805 ret = marvell_nfc_prepare_cmd(chip);
1806 if (ret)
1807 return ret;
1808
1809 marvell_nfc_send_cmd(chip, &nfc_op);
1810 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1811 "RDDREQ/WRDREQ while draining raw data");
1812 if (ret)
1813 return ret;
1814
1815 cond_delay(nfc_op.cle_ale_delay_ns);
1816
1817 if (reading) {
1818 if (nfc_op.rdy_timeout_ms) {
1819 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1820 if (ret)
1821 return ret;
1822 }
1823
1824 cond_delay(nfc_op.rdy_delay_ns);
1825 }
1826
1827 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1828 ret = marvell_nfc_wait_cmdd(chip);
1829 if (ret)
1830 return ret;
1831
1832 cond_delay(nfc_op.data_delay_ns);
1833
1834 if (!reading) {
1835 if (nfc_op.rdy_timeout_ms) {
1836 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1837 if (ret)
1838 return ret;
1839 }
1840
1841 cond_delay(nfc_op.rdy_delay_ns);
1842 }
1843
1844
1845
1846
1847
1848
1849 if (!reading) {
1850 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1851
1852 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1853 nfc->regs + NDCR);
1854 }
1855
1856 return 0;
1857 }
1858
1859 static int marvell_nfc_naked_access_exec(struct nand_chip *chip,
1860 const struct nand_subop *subop)
1861 {
1862 struct marvell_nfc_op nfc_op;
1863 int ret;
1864
1865 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1866
1867
1868
1869
1870
1871
1872 nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) |
1873 NDCB0_CMD_XTYPE(XTYPE_MASK));
1874 switch (subop->instrs[0].type) {
1875 case NAND_OP_CMD_INSTR:
1876 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD);
1877 break;
1878 case NAND_OP_ADDR_INSTR:
1879 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR);
1880 break;
1881 case NAND_OP_DATA_IN_INSTR:
1882 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) |
1883 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1884 break;
1885 case NAND_OP_DATA_OUT_INSTR:
1886 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) |
1887 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1888 break;
1889 default:
1890
1891 break;
1892 }
1893
1894 ret = marvell_nfc_prepare_cmd(chip);
1895 if (ret)
1896 return ret;
1897
1898 marvell_nfc_send_cmd(chip, &nfc_op);
1899
1900 if (!nfc_op.data_instr) {
1901 ret = marvell_nfc_wait_cmdd(chip);
1902 cond_delay(nfc_op.cle_ale_delay_ns);
1903 return ret;
1904 }
1905
1906 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1907 "RDDREQ/WRDREQ while draining raw data");
1908 if (ret)
1909 return ret;
1910
1911 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1912 ret = marvell_nfc_wait_cmdd(chip);
1913 if (ret)
1914 return ret;
1915
1916
1917
1918
1919
1920
1921 if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) {
1922 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1923
1924 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1925 nfc->regs + NDCR);
1926 }
1927
1928 return 0;
1929 }
1930
1931 static int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip,
1932 const struct nand_subop *subop)
1933 {
1934 struct marvell_nfc_op nfc_op;
1935 int ret;
1936
1937 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1938
1939 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1940 cond_delay(nfc_op.rdy_delay_ns);
1941
1942 return ret;
1943 }
1944
1945 static int marvell_nfc_read_id_type_exec(struct nand_chip *chip,
1946 const struct nand_subop *subop)
1947 {
1948 struct marvell_nfc_op nfc_op;
1949 int ret;
1950
1951 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1952 nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1953 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID);
1954
1955 ret = marvell_nfc_prepare_cmd(chip);
1956 if (ret)
1957 return ret;
1958
1959 marvell_nfc_send_cmd(chip, &nfc_op);
1960 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1961 "RDDREQ while reading ID");
1962 if (ret)
1963 return ret;
1964
1965 cond_delay(nfc_op.cle_ale_delay_ns);
1966
1967 if (nfc_op.rdy_timeout_ms) {
1968 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1969 if (ret)
1970 return ret;
1971 }
1972
1973 cond_delay(nfc_op.rdy_delay_ns);
1974
1975 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1976 ret = marvell_nfc_wait_cmdd(chip);
1977 if (ret)
1978 return ret;
1979
1980 cond_delay(nfc_op.data_delay_ns);
1981
1982 return 0;
1983 }
1984
1985 static int marvell_nfc_read_status_exec(struct nand_chip *chip,
1986 const struct nand_subop *subop)
1987 {
1988 struct marvell_nfc_op nfc_op;
1989 int ret;
1990
1991 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1992 nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1993 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS);
1994
1995 ret = marvell_nfc_prepare_cmd(chip);
1996 if (ret)
1997 return ret;
1998
1999 marvell_nfc_send_cmd(chip, &nfc_op);
2000 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
2001 "RDDREQ while reading status");
2002 if (ret)
2003 return ret;
2004
2005 cond_delay(nfc_op.cle_ale_delay_ns);
2006
2007 if (nfc_op.rdy_timeout_ms) {
2008 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
2009 if (ret)
2010 return ret;
2011 }
2012
2013 cond_delay(nfc_op.rdy_delay_ns);
2014
2015 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
2016 ret = marvell_nfc_wait_cmdd(chip);
2017 if (ret)
2018 return ret;
2019
2020 cond_delay(nfc_op.data_delay_ns);
2021
2022 return 0;
2023 }
2024
2025 static int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip,
2026 const struct nand_subop *subop)
2027 {
2028 struct marvell_nfc_op nfc_op;
2029 int ret;
2030
2031 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
2032 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET);
2033
2034 ret = marvell_nfc_prepare_cmd(chip);
2035 if (ret)
2036 return ret;
2037
2038 marvell_nfc_send_cmd(chip, &nfc_op);
2039 ret = marvell_nfc_wait_cmdd(chip);
2040 if (ret)
2041 return ret;
2042
2043 cond_delay(nfc_op.cle_ale_delay_ns);
2044
2045 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
2046 if (ret)
2047 return ret;
2048
2049 cond_delay(nfc_op.rdy_delay_ns);
2050
2051 return 0;
2052 }
2053
2054 static int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip,
2055 const struct nand_subop *subop)
2056 {
2057 struct marvell_nfc_op nfc_op;
2058 int ret;
2059
2060 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
2061 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE);
2062
2063 ret = marvell_nfc_prepare_cmd(chip);
2064 if (ret)
2065 return ret;
2066
2067 marvell_nfc_send_cmd(chip, &nfc_op);
2068 ret = marvell_nfc_wait_cmdd(chip);
2069 if (ret)
2070 return ret;
2071
2072 cond_delay(nfc_op.cle_ale_delay_ns);
2073
2074 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
2075 if (ret)
2076 return ret;
2077
2078 cond_delay(nfc_op.rdy_delay_ns);
2079
2080 return 0;
2081 }
2082
2083 static const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER(
2084
2085 NAND_OP_PARSER_PATTERN(
2086 marvell_nfc_monolithic_access_exec,
2087 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2088 NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2),
2089 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2090 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
2091 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
2092 NAND_OP_PARSER_PATTERN(
2093 marvell_nfc_monolithic_access_exec,
2094 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2095 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2),
2096 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE),
2097 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2098 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
2099
2100 NAND_OP_PARSER_PATTERN(
2101 marvell_nfc_naked_access_exec,
2102 NAND_OP_PARSER_PAT_CMD_ELEM(false)),
2103 NAND_OP_PARSER_PATTERN(
2104 marvell_nfc_naked_access_exec,
2105 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)),
2106 NAND_OP_PARSER_PATTERN(
2107 marvell_nfc_naked_access_exec,
2108 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
2109 NAND_OP_PARSER_PATTERN(
2110 marvell_nfc_naked_access_exec,
2111 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)),
2112 NAND_OP_PARSER_PATTERN(
2113 marvell_nfc_naked_waitrdy_exec,
2114 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2115 );
2116
2117 static const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER(
2118
2119 NAND_OP_PARSER_PATTERN(
2120 marvell_nfc_read_id_type_exec,
2121 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2122 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
2123 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)),
2124 NAND_OP_PARSER_PATTERN(
2125 marvell_nfc_erase_cmd_type_exec,
2126 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2127 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
2128 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2129 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2130 NAND_OP_PARSER_PATTERN(
2131 marvell_nfc_read_status_exec,
2132 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2133 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
2134 NAND_OP_PARSER_PATTERN(
2135 marvell_nfc_reset_cmd_type_exec,
2136 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2137 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2138 NAND_OP_PARSER_PATTERN(
2139 marvell_nfc_naked_waitrdy_exec,
2140 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2141 );
2142
2143 static int marvell_nfc_exec_op(struct nand_chip *chip,
2144 const struct nand_operation *op,
2145 bool check_only)
2146 {
2147 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2148
2149 if (!check_only)
2150 marvell_nfc_select_target(chip, op->cs);
2151
2152 if (nfc->caps->is_nfcv2)
2153 return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser,
2154 op, check_only);
2155 else
2156 return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser,
2157 op, check_only);
2158 }
2159
2160
2161
2162
2163
2164 static int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2165 struct mtd_oob_region *oobregion)
2166 {
2167 struct nand_chip *chip = mtd_to_nand(mtd);
2168 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2169
2170 if (section)
2171 return -ERANGE;
2172
2173 oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) +
2174 lt->last_ecc_bytes;
2175 oobregion->offset = mtd->oobsize - oobregion->length;
2176
2177 return 0;
2178 }
2179
2180 static int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section,
2181 struct mtd_oob_region *oobregion)
2182 {
2183 struct nand_chip *chip = mtd_to_nand(mtd);
2184 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2185
2186 if (section)
2187 return -ERANGE;
2188
2189
2190
2191
2192
2193 if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K)
2194 oobregion->offset = 6;
2195 else
2196 oobregion->offset = 2;
2197
2198 oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) +
2199 lt->last_spare_bytes - oobregion->offset;
2200
2201 return 0;
2202 }
2203
2204 static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = {
2205 .ecc = marvell_nand_ooblayout_ecc,
2206 .free = marvell_nand_ooblayout_free,
2207 };
2208
2209 static int marvell_nand_hw_ecc_controller_init(struct mtd_info *mtd,
2210 struct nand_ecc_ctrl *ecc)
2211 {
2212 struct nand_chip *chip = mtd_to_nand(mtd);
2213 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2214 const struct marvell_hw_ecc_layout *l;
2215 int i;
2216
2217 if (!nfc->caps->is_nfcv2 &&
2218 (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) {
2219 dev_err(nfc->dev,
2220 "NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
2221 mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize);
2222 return -ENOTSUPP;
2223 }
2224
2225 to_marvell_nand(chip)->layout = NULL;
2226 for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) {
2227 l = &marvell_nfc_layouts[i];
2228 if (mtd->writesize == l->writesize &&
2229 ecc->size == l->chunk && ecc->strength == l->strength) {
2230 to_marvell_nand(chip)->layout = l;
2231 break;
2232 }
2233 }
2234
2235 if (!to_marvell_nand(chip)->layout ||
2236 (!nfc->caps->is_nfcv2 && ecc->strength > 1)) {
2237 dev_err(nfc->dev,
2238 "ECC strength %d at page size %d is not supported\n",
2239 ecc->strength, mtd->writesize);
2240 return -ENOTSUPP;
2241 }
2242
2243
2244 if (l->writesize == 2048 && l->strength == 8) {
2245 if (mtd->oobsize < 128) {
2246 dev_err(nfc->dev, "Requested layout needs at least 128 OOB bytes\n");
2247 return -ENOTSUPP;
2248 } else {
2249 chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
2250 }
2251 }
2252
2253 mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops);
2254 ecc->steps = l->nchunks;
2255 ecc->size = l->data_bytes;
2256
2257 if (ecc->strength == 1) {
2258 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
2259 ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw;
2260 ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page;
2261 ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw;
2262 ecc->read_oob = ecc->read_oob_raw;
2263 ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw;
2264 ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page;
2265 ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw;
2266 ecc->write_oob = ecc->write_oob_raw;
2267 } else {
2268 chip->ecc.algo = NAND_ECC_ALGO_BCH;
2269 ecc->strength = 16;
2270 ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw;
2271 ecc->read_page = marvell_nfc_hw_ecc_bch_read_page;
2272 ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw;
2273 ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob;
2274 ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw;
2275 ecc->write_page = marvell_nfc_hw_ecc_bch_write_page;
2276 ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw;
2277 ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob;
2278 }
2279
2280 return 0;
2281 }
2282
2283 static int marvell_nand_ecc_init(struct mtd_info *mtd,
2284 struct nand_ecc_ctrl *ecc)
2285 {
2286 struct nand_chip *chip = mtd_to_nand(mtd);
2287 const struct nand_ecc_props *requirements =
2288 nanddev_get_ecc_requirements(&chip->base);
2289 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2290 int ret;
2291
2292 if (ecc->engine_type != NAND_ECC_ENGINE_TYPE_NONE &&
2293 (!ecc->size || !ecc->strength)) {
2294 if (requirements->step_size && requirements->strength) {
2295 ecc->size = requirements->step_size;
2296 ecc->strength = requirements->strength;
2297 } else {
2298 dev_info(nfc->dev,
2299 "No minimum ECC strength, using 1b/512B\n");
2300 ecc->size = 512;
2301 ecc->strength = 1;
2302 }
2303 }
2304
2305 switch (ecc->engine_type) {
2306 case NAND_ECC_ENGINE_TYPE_ON_HOST:
2307 ret = marvell_nand_hw_ecc_controller_init(mtd, ecc);
2308 if (ret)
2309 return ret;
2310 break;
2311 case NAND_ECC_ENGINE_TYPE_NONE:
2312 case NAND_ECC_ENGINE_TYPE_SOFT:
2313 case NAND_ECC_ENGINE_TYPE_ON_DIE:
2314 if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 &&
2315 mtd->writesize != SZ_2K) {
2316 dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n",
2317 mtd->writesize);
2318 return -EINVAL;
2319 }
2320 break;
2321 default:
2322 return -EINVAL;
2323 }
2324
2325 return 0;
2326 }
2327
2328 static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
2329 static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
2330
2331 static struct nand_bbt_descr bbt_main_descr = {
2332 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2333 NAND_BBT_2BIT | NAND_BBT_VERSION,
2334 .offs = 8,
2335 .len = 6,
2336 .veroffs = 14,
2337 .maxblocks = 8,
2338 .pattern = bbt_pattern
2339 };
2340
2341 static struct nand_bbt_descr bbt_mirror_descr = {
2342 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2343 NAND_BBT_2BIT | NAND_BBT_VERSION,
2344 .offs = 8,
2345 .len = 6,
2346 .veroffs = 14,
2347 .maxblocks = 8,
2348 .pattern = bbt_mirror_pattern
2349 };
2350
2351 static int marvell_nfc_setup_interface(struct nand_chip *chip, int chipnr,
2352 const struct nand_interface_config *conf)
2353 {
2354 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2355 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2356 unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2;
2357 const struct nand_sdr_timings *sdr;
2358 struct marvell_nfc_timings nfc_tmg;
2359 int read_delay;
2360
2361 sdr = nand_get_sdr_timings(conf);
2362 if (IS_ERR(sdr))
2363 return PTR_ERR(sdr);
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376 nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1;
2377 nfc_tmg.tRH = nfc_tmg.tRP;
2378 nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1;
2379 nfc_tmg.tWH = nfc_tmg.tWP;
2380 nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns);
2381 nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1;
2382 nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns);
2383
2384
2385
2386
2387
2388
2389 read_delay = sdr->tRC_min >= 30000 ?
2390 MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH;
2391
2392 nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns);
2393
2394
2395
2396
2397
2398 nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min),
2399 period_ns) - 2;
2400 nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min),
2401 period_ns);
2402
2403
2404
2405
2406
2407 if (nfc->caps->is_nfcv2) {
2408 nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns);
2409 } else {
2410 nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max,
2411 period_ns);
2412 if (nfc_tmg.tR + 3 > nfc_tmg.tCH)
2413 nfc_tmg.tR = nfc_tmg.tCH - 3;
2414 else
2415 nfc_tmg.tR = 0;
2416 }
2417
2418 if (chipnr < 0)
2419 return 0;
2420
2421 marvell_nand->ndtr0 =
2422 NDTR0_TRP(nfc_tmg.tRP) |
2423 NDTR0_TRH(nfc_tmg.tRH) |
2424 NDTR0_ETRP(nfc_tmg.tRP) |
2425 NDTR0_TWP(nfc_tmg.tWP) |
2426 NDTR0_TWH(nfc_tmg.tWH) |
2427 NDTR0_TCS(nfc_tmg.tCS) |
2428 NDTR0_TCH(nfc_tmg.tCH);
2429
2430 marvell_nand->ndtr1 =
2431 NDTR1_TAR(nfc_tmg.tAR) |
2432 NDTR1_TWHR(nfc_tmg.tWHR) |
2433 NDTR1_TR(nfc_tmg.tR);
2434
2435 if (nfc->caps->is_nfcv2) {
2436 marvell_nand->ndtr0 |=
2437 NDTR0_RD_CNT_DEL(read_delay) |
2438 NDTR0_SELCNTR |
2439 NDTR0_TADL(nfc_tmg.tADL);
2440
2441 marvell_nand->ndtr1 |=
2442 NDTR1_TRHW(nfc_tmg.tRHW) |
2443 NDTR1_WAIT_MODE;
2444 }
2445
2446 return 0;
2447 }
2448
2449 static int marvell_nand_attach_chip(struct nand_chip *chip)
2450 {
2451 struct mtd_info *mtd = nand_to_mtd(chip);
2452 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2453 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2454 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(nfc->dev);
2455 int ret;
2456
2457 if (pdata && pdata->flash_bbt)
2458 chip->bbt_options |= NAND_BBT_USE_FLASH;
2459
2460 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
2461
2462
2463
2464
2465 chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
2466 chip->bbt_td = &bbt_main_descr;
2467 chip->bbt_md = &bbt_mirror_descr;
2468 }
2469
2470
2471 marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize);
2472 if (chip->options & NAND_BUSWIDTH_16)
2473 marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
2474
2475
2476
2477
2478
2479 if (mtd->writesize <= 512) {
2480 marvell_nand->addr_cyc = 1;
2481 } else {
2482 marvell_nand->addr_cyc = 2;
2483 marvell_nand->ndcr |= NDCR_RA_START;
2484 }
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494 if (chip->options & NAND_ROW_ADDR_3)
2495 marvell_nand->addr_cyc += 3;
2496 else
2497 marvell_nand->addr_cyc += 2;
2498
2499 if (pdata) {
2500 chip->ecc.size = pdata->ecc_step_size;
2501 chip->ecc.strength = pdata->ecc_strength;
2502 }
2503
2504 ret = marvell_nand_ecc_init(mtd, &chip->ecc);
2505 if (ret) {
2506 dev_err(nfc->dev, "ECC init failed: %d\n", ret);
2507 return ret;
2508 }
2509
2510 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
2511
2512
2513
2514
2515
2516
2517 chip->options |= NAND_NO_SUBPAGE_WRITE;
2518 }
2519
2520 if (pdata || nfc->caps->legacy_of_bindings) {
2521
2522
2523
2524
2525
2526 mtd->name = "pxa3xx_nand-0";
2527 } else if (!mtd->name) {
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538 mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
2539 "%s:nand.%d", dev_name(nfc->dev),
2540 marvell_nand->sels[0].cs);
2541 if (!mtd->name) {
2542 dev_err(nfc->dev, "Failed to allocate mtd->name\n");
2543 return -ENOMEM;
2544 }
2545 }
2546
2547 return 0;
2548 }
2549
2550 static const struct nand_controller_ops marvell_nand_controller_ops = {
2551 .attach_chip = marvell_nand_attach_chip,
2552 .exec_op = marvell_nfc_exec_op,
2553 .setup_interface = marvell_nfc_setup_interface,
2554 };
2555
2556 static int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc,
2557 struct device_node *np)
2558 {
2559 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev);
2560 struct marvell_nand_chip *marvell_nand;
2561 struct mtd_info *mtd;
2562 struct nand_chip *chip;
2563 int nsels, ret, i;
2564 u32 cs, rb;
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575 if (pdata || nfc->caps->legacy_of_bindings) {
2576 nsels = 1;
2577 } else {
2578 nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32));
2579 if (nsels <= 0) {
2580 dev_err(dev, "missing/invalid reg property\n");
2581 return -EINVAL;
2582 }
2583 }
2584
2585
2586 marvell_nand = devm_kzalloc(dev,
2587 struct_size(marvell_nand, sels, nsels),
2588 GFP_KERNEL);
2589 if (!marvell_nand) {
2590 dev_err(dev, "could not allocate chip structure\n");
2591 return -ENOMEM;
2592 }
2593
2594 marvell_nand->nsels = nsels;
2595 marvell_nand->selected_die = -1;
2596
2597 for (i = 0; i < nsels; i++) {
2598 if (pdata || nfc->caps->legacy_of_bindings) {
2599
2600
2601
2602
2603 cs = i;
2604 } else {
2605
2606 ret = of_property_read_u32_index(np, "reg", i, &cs);
2607 if (ret) {
2608 dev_err(dev, "could not retrieve reg property: %d\n",
2609 ret);
2610 return ret;
2611 }
2612 }
2613
2614 if (cs >= nfc->caps->max_cs_nb) {
2615 dev_err(dev, "invalid reg value: %u (max CS = %d)\n",
2616 cs, nfc->caps->max_cs_nb);
2617 return -EINVAL;
2618 }
2619
2620 if (test_and_set_bit(cs, &nfc->assigned_cs)) {
2621 dev_err(dev, "CS %d already assigned\n", cs);
2622 return -EINVAL;
2623 }
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633 marvell_nand->sels[i].cs = cs;
2634 switch (cs) {
2635 case 0:
2636 case 2:
2637 marvell_nand->sels[i].ndcb0_csel = 0;
2638 break;
2639 case 1:
2640 case 3:
2641 marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL;
2642 break;
2643 default:
2644 return -EINVAL;
2645 }
2646
2647
2648 if (pdata || nfc->caps->legacy_of_bindings) {
2649
2650 rb = 0;
2651 } else {
2652 ret = of_property_read_u32_index(np, "nand-rb", i,
2653 &rb);
2654 if (ret) {
2655 dev_err(dev,
2656 "could not retrieve RB property: %d\n",
2657 ret);
2658 return ret;
2659 }
2660 }
2661
2662 if (rb >= nfc->caps->max_rb_nb) {
2663 dev_err(dev, "invalid reg value: %u (max RB = %d)\n",
2664 rb, nfc->caps->max_rb_nb);
2665 return -EINVAL;
2666 }
2667
2668 marvell_nand->sels[i].rb = rb;
2669 }
2670
2671 chip = &marvell_nand->chip;
2672 chip->controller = &nfc->controller;
2673 nand_set_flash_node(chip, np);
2674
2675 if (!of_property_read_bool(np, "marvell,nand-keep-config"))
2676 chip->options |= NAND_KEEP_TIMINGS;
2677
2678 mtd = nand_to_mtd(chip);
2679 mtd->dev.parent = dev;
2680
2681
2682
2683
2684
2685 marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0);
2686 marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1);
2687
2688 chip->options |= NAND_BUSWIDTH_AUTO;
2689
2690 ret = nand_scan(chip, marvell_nand->nsels);
2691 if (ret) {
2692 dev_err(dev, "could not scan the nand chip\n");
2693 return ret;
2694 }
2695
2696 if (pdata)
2697
2698 ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
2699 else
2700 ret = mtd_device_register(mtd, NULL, 0);
2701 if (ret) {
2702 dev_err(dev, "failed to register mtd device: %d\n", ret);
2703 nand_cleanup(chip);
2704 return ret;
2705 }
2706
2707 list_add_tail(&marvell_nand->node, &nfc->chips);
2708
2709 return 0;
2710 }
2711
2712 static void marvell_nand_chips_cleanup(struct marvell_nfc *nfc)
2713 {
2714 struct marvell_nand_chip *entry, *temp;
2715 struct nand_chip *chip;
2716 int ret;
2717
2718 list_for_each_entry_safe(entry, temp, &nfc->chips, node) {
2719 chip = &entry->chip;
2720 ret = mtd_device_unregister(nand_to_mtd(chip));
2721 WARN_ON(ret);
2722 nand_cleanup(chip);
2723 list_del(&entry->node);
2724 }
2725 }
2726
2727 static int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc)
2728 {
2729 struct device_node *np = dev->of_node;
2730 struct device_node *nand_np;
2731 int max_cs = nfc->caps->max_cs_nb;
2732 int nchips;
2733 int ret;
2734
2735 if (!np)
2736 nchips = 1;
2737 else
2738 nchips = of_get_child_count(np);
2739
2740 if (nchips > max_cs) {
2741 dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips,
2742 max_cs);
2743 return -EINVAL;
2744 }
2745
2746
2747
2748
2749
2750
2751
2752 if (nfc->caps->legacy_of_bindings) {
2753 ret = marvell_nand_chip_init(dev, nfc, np);
2754 return ret;
2755 }
2756
2757 for_each_child_of_node(np, nand_np) {
2758 ret = marvell_nand_chip_init(dev, nfc, nand_np);
2759 if (ret) {
2760 of_node_put(nand_np);
2761 goto cleanup_chips;
2762 }
2763 }
2764
2765 return 0;
2766
2767 cleanup_chips:
2768 marvell_nand_chips_cleanup(nfc);
2769
2770 return ret;
2771 }
2772
2773 static int marvell_nfc_init_dma(struct marvell_nfc *nfc)
2774 {
2775 struct platform_device *pdev = container_of(nfc->dev,
2776 struct platform_device,
2777 dev);
2778 struct dma_slave_config config = {};
2779 struct resource *r;
2780 int ret;
2781
2782 if (!IS_ENABLED(CONFIG_PXA_DMA)) {
2783 dev_warn(nfc->dev,
2784 "DMA not enabled in configuration\n");
2785 return -ENOTSUPP;
2786 }
2787
2788 ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32));
2789 if (ret)
2790 return ret;
2791
2792 nfc->dma_chan = dma_request_chan(nfc->dev, "data");
2793 if (IS_ERR(nfc->dma_chan)) {
2794 ret = PTR_ERR(nfc->dma_chan);
2795 nfc->dma_chan = NULL;
2796 return dev_err_probe(nfc->dev, ret, "DMA channel request failed\n");
2797 }
2798
2799 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2800 if (!r) {
2801 ret = -ENXIO;
2802 goto release_channel;
2803 }
2804
2805 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2806 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2807 config.src_addr = r->start + NDDB;
2808 config.dst_addr = r->start + NDDB;
2809 config.src_maxburst = 32;
2810 config.dst_maxburst = 32;
2811 ret = dmaengine_slave_config(nfc->dma_chan, &config);
2812 if (ret < 0) {
2813 dev_err(nfc->dev, "Failed to configure DMA channel\n");
2814 goto release_channel;
2815 }
2816
2817
2818
2819
2820
2821
2822
2823 nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA);
2824 if (!nfc->dma_buf) {
2825 ret = -ENOMEM;
2826 goto release_channel;
2827 }
2828
2829 nfc->use_dma = true;
2830
2831 return 0;
2832
2833 release_channel:
2834 dma_release_channel(nfc->dma_chan);
2835 nfc->dma_chan = NULL;
2836
2837 return ret;
2838 }
2839
2840 static void marvell_nfc_reset(struct marvell_nfc *nfc)
2841 {
2842
2843
2844
2845
2846
2847
2848
2849 writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN |
2850 NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR);
2851 writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR);
2852 writel_relaxed(0, nfc->regs + NDECCCTRL);
2853 }
2854
2855 static int marvell_nfc_init(struct marvell_nfc *nfc)
2856 {
2857 struct device_node *np = nfc->dev->of_node;
2858
2859
2860
2861
2862
2863
2864
2865 if (nfc->caps->need_system_controller) {
2866 struct regmap *sysctrl_base =
2867 syscon_regmap_lookup_by_phandle(np,
2868 "marvell,system-controller");
2869
2870 if (IS_ERR(sysctrl_base))
2871 return PTR_ERR(sysctrl_base);
2872
2873 regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX,
2874 GENCONF_SOC_DEVICE_MUX_NFC_EN |
2875 GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST |
2876 GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST |
2877 GENCONF_SOC_DEVICE_MUX_NFC_INT_EN);
2878
2879 regmap_update_bits(sysctrl_base, GENCONF_CLK_GATING_CTRL,
2880 GENCONF_CLK_GATING_CTRL_ND_GATE,
2881 GENCONF_CLK_GATING_CTRL_ND_GATE);
2882
2883 regmap_update_bits(sysctrl_base, GENCONF_ND_CLK_CTRL,
2884 GENCONF_ND_CLK_CTRL_EN,
2885 GENCONF_ND_CLK_CTRL_EN);
2886 }
2887
2888
2889 if (!nfc->caps->is_nfcv2)
2890 marvell_nfc_init_dma(nfc);
2891
2892 marvell_nfc_reset(nfc);
2893
2894 return 0;
2895 }
2896
2897 static int marvell_nfc_probe(struct platform_device *pdev)
2898 {
2899 struct device *dev = &pdev->dev;
2900 struct marvell_nfc *nfc;
2901 int ret;
2902 int irq;
2903
2904 nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc),
2905 GFP_KERNEL);
2906 if (!nfc)
2907 return -ENOMEM;
2908
2909 nfc->dev = dev;
2910 nand_controller_init(&nfc->controller);
2911 nfc->controller.ops = &marvell_nand_controller_ops;
2912 INIT_LIST_HEAD(&nfc->chips);
2913
2914 nfc->regs = devm_platform_ioremap_resource(pdev, 0);
2915 if (IS_ERR(nfc->regs))
2916 return PTR_ERR(nfc->regs);
2917
2918 irq = platform_get_irq(pdev, 0);
2919 if (irq < 0)
2920 return irq;
2921
2922 nfc->core_clk = devm_clk_get(&pdev->dev, "core");
2923
2924
2925 if (nfc->core_clk == ERR_PTR(-ENOENT))
2926 nfc->core_clk = devm_clk_get(&pdev->dev, NULL);
2927
2928 if (IS_ERR(nfc->core_clk))
2929 return PTR_ERR(nfc->core_clk);
2930
2931 ret = clk_prepare_enable(nfc->core_clk);
2932 if (ret)
2933 return ret;
2934
2935 nfc->reg_clk = devm_clk_get(&pdev->dev, "reg");
2936 if (IS_ERR(nfc->reg_clk)) {
2937 if (PTR_ERR(nfc->reg_clk) != -ENOENT) {
2938 ret = PTR_ERR(nfc->reg_clk);
2939 goto unprepare_core_clk;
2940 }
2941
2942 nfc->reg_clk = NULL;
2943 }
2944
2945 ret = clk_prepare_enable(nfc->reg_clk);
2946 if (ret)
2947 goto unprepare_core_clk;
2948
2949 marvell_nfc_disable_int(nfc, NDCR_ALL_INT);
2950 marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
2951 ret = devm_request_irq(dev, irq, marvell_nfc_isr,
2952 0, "marvell-nfc", nfc);
2953 if (ret)
2954 goto unprepare_reg_clk;
2955
2956
2957 if (pdev->id_entry)
2958 nfc->caps = (void *)pdev->id_entry->driver_data;
2959 else
2960 nfc->caps = of_device_get_match_data(&pdev->dev);
2961
2962 if (!nfc->caps) {
2963 dev_err(dev, "Could not retrieve NFC caps\n");
2964 ret = -EINVAL;
2965 goto unprepare_reg_clk;
2966 }
2967
2968
2969 ret = marvell_nfc_init(nfc);
2970 if (ret)
2971 goto unprepare_reg_clk;
2972
2973 platform_set_drvdata(pdev, nfc);
2974
2975 ret = marvell_nand_chips_init(dev, nfc);
2976 if (ret)
2977 goto release_dma;
2978
2979 return 0;
2980
2981 release_dma:
2982 if (nfc->use_dma)
2983 dma_release_channel(nfc->dma_chan);
2984 unprepare_reg_clk:
2985 clk_disable_unprepare(nfc->reg_clk);
2986 unprepare_core_clk:
2987 clk_disable_unprepare(nfc->core_clk);
2988
2989 return ret;
2990 }
2991
2992 static int marvell_nfc_remove(struct platform_device *pdev)
2993 {
2994 struct marvell_nfc *nfc = platform_get_drvdata(pdev);
2995
2996 marvell_nand_chips_cleanup(nfc);
2997
2998 if (nfc->use_dma) {
2999 dmaengine_terminate_all(nfc->dma_chan);
3000 dma_release_channel(nfc->dma_chan);
3001 }
3002
3003 clk_disable_unprepare(nfc->reg_clk);
3004 clk_disable_unprepare(nfc->core_clk);
3005
3006 return 0;
3007 }
3008
3009 static int __maybe_unused marvell_nfc_suspend(struct device *dev)
3010 {
3011 struct marvell_nfc *nfc = dev_get_drvdata(dev);
3012 struct marvell_nand_chip *chip;
3013
3014 list_for_each_entry(chip, &nfc->chips, node)
3015 marvell_nfc_wait_ndrun(&chip->chip);
3016
3017 clk_disable_unprepare(nfc->reg_clk);
3018 clk_disable_unprepare(nfc->core_clk);
3019
3020 return 0;
3021 }
3022
3023 static int __maybe_unused marvell_nfc_resume(struct device *dev)
3024 {
3025 struct marvell_nfc *nfc = dev_get_drvdata(dev);
3026 int ret;
3027
3028 ret = clk_prepare_enable(nfc->core_clk);
3029 if (ret < 0)
3030 return ret;
3031
3032 ret = clk_prepare_enable(nfc->reg_clk);
3033 if (ret < 0) {
3034 clk_disable_unprepare(nfc->core_clk);
3035 return ret;
3036 }
3037
3038
3039
3040
3041
3042 nfc->selected_chip = NULL;
3043
3044
3045 marvell_nfc_reset(nfc);
3046
3047 return 0;
3048 }
3049
3050 static const struct dev_pm_ops marvell_nfc_pm_ops = {
3051 SET_SYSTEM_SLEEP_PM_OPS(marvell_nfc_suspend, marvell_nfc_resume)
3052 };
3053
3054 static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = {
3055 .max_cs_nb = 4,
3056 .max_rb_nb = 2,
3057 .need_system_controller = true,
3058 .is_nfcv2 = true,
3059 };
3060
3061 static const struct marvell_nfc_caps marvell_armada370_nfc_caps = {
3062 .max_cs_nb = 4,
3063 .max_rb_nb = 2,
3064 .is_nfcv2 = true,
3065 };
3066
3067 static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = {
3068 .max_cs_nb = 2,
3069 .max_rb_nb = 1,
3070 .use_dma = true,
3071 };
3072
3073 static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = {
3074 .max_cs_nb = 4,
3075 .max_rb_nb = 2,
3076 .need_system_controller = true,
3077 .legacy_of_bindings = true,
3078 .is_nfcv2 = true,
3079 };
3080
3081 static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = {
3082 .max_cs_nb = 4,
3083 .max_rb_nb = 2,
3084 .legacy_of_bindings = true,
3085 .is_nfcv2 = true,
3086 };
3087
3088 static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = {
3089 .max_cs_nb = 2,
3090 .max_rb_nb = 1,
3091 .legacy_of_bindings = true,
3092 .use_dma = true,
3093 };
3094
3095 static const struct platform_device_id marvell_nfc_platform_ids[] = {
3096 {
3097 .name = "pxa3xx-nand",
3098 .driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps,
3099 },
3100 { },
3101 };
3102 MODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids);
3103
3104 static const struct of_device_id marvell_nfc_of_ids[] = {
3105 {
3106 .compatible = "marvell,armada-8k-nand-controller",
3107 .data = &marvell_armada_8k_nfc_caps,
3108 },
3109 {
3110 .compatible = "marvell,armada370-nand-controller",
3111 .data = &marvell_armada370_nfc_caps,
3112 },
3113 {
3114 .compatible = "marvell,pxa3xx-nand-controller",
3115 .data = &marvell_pxa3xx_nfc_caps,
3116 },
3117
3118 {
3119 .compatible = "marvell,armada-8k-nand",
3120 .data = &marvell_armada_8k_nfc_legacy_caps,
3121 },
3122 {
3123 .compatible = "marvell,armada370-nand",
3124 .data = &marvell_armada370_nfc_legacy_caps,
3125 },
3126 {
3127 .compatible = "marvell,pxa3xx-nand",
3128 .data = &marvell_pxa3xx_nfc_legacy_caps,
3129 },
3130 { },
3131 };
3132 MODULE_DEVICE_TABLE(of, marvell_nfc_of_ids);
3133
3134 static struct platform_driver marvell_nfc_driver = {
3135 .driver = {
3136 .name = "marvell-nfc",
3137 .of_match_table = marvell_nfc_of_ids,
3138 .pm = &marvell_nfc_pm_ops,
3139 },
3140 .id_table = marvell_nfc_platform_ids,
3141 .probe = marvell_nfc_probe,
3142 .remove = marvell_nfc_remove,
3143 };
3144 module_platform_driver(marvell_nfc_driver);
3145
3146 MODULE_LICENSE("GPL");
3147 MODULE_DESCRIPTION("Marvell NAND controller driver");