0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <linux/module.h>
0023 #include <linux/bitops.h>
0024 #include <linux/clk.h>
0025 #include <linux/delay.h>
0026 #include <linux/init.h>
0027 #include <linux/interrupt.h>
0028 #include <linux/io.h>
0029 #include <linux/mtd/mtd.h>
0030 #include <linux/mtd/rawnand.h>
0031 #include <linux/mtd/partitions.h>
0032 #include <linux/of_device.h>
0033 #include <linux/platform_device.h>
0034 #include <linux/slab.h>
0035 #include <linux/swab.h>
0036
0037 #define DRV_NAME "vf610_nfc"
0038
0039
0040 #define NFC_FLASH_CMD1 0x3F00
0041 #define NFC_FLASH_CMD2 0x3F04
0042 #define NFC_COL_ADDR 0x3F08
0043 #define NFC_ROW_ADDR 0x3F0c
0044 #define NFC_ROW_ADDR_INC 0x3F14
0045 #define NFC_FLASH_STATUS1 0x3F18
0046 #define NFC_FLASH_STATUS2 0x3F1c
0047 #define NFC_CACHE_SWAP 0x3F28
0048 #define NFC_SECTOR_SIZE 0x3F2c
0049 #define NFC_FLASH_CONFIG 0x3F30
0050 #define NFC_IRQ_STATUS 0x3F38
0051
0052
0053 #define NFC_MAIN_AREA(n) ((n) * 0x1000)
0054
0055 #define PAGE_2K 0x0800
0056 #define OOB_64 0x0040
0057 #define OOB_MAX 0x0100
0058
0059
0060 #define COMMAND_CMD_BYTE1 BIT(14)
0061 #define COMMAND_CAR_BYTE1 BIT(13)
0062 #define COMMAND_CAR_BYTE2 BIT(12)
0063 #define COMMAND_RAR_BYTE1 BIT(11)
0064 #define COMMAND_RAR_BYTE2 BIT(10)
0065 #define COMMAND_RAR_BYTE3 BIT(9)
0066 #define COMMAND_NADDR_BYTES(x) GENMASK(13, 13 - (x) + 1)
0067 #define COMMAND_WRITE_DATA BIT(8)
0068 #define COMMAND_CMD_BYTE2 BIT(7)
0069 #define COMMAND_RB_HANDSHAKE BIT(6)
0070 #define COMMAND_READ_DATA BIT(5)
0071 #define COMMAND_CMD_BYTE3 BIT(4)
0072 #define COMMAND_READ_STATUS BIT(3)
0073 #define COMMAND_READ_ID BIT(2)
0074
0075
0076 #define ECC_BYPASS 0
0077 #define ECC_45_BYTE 6
0078 #define ECC_60_BYTE 7
0079
0080
0081
0082
0083 #define CMD_BYTE2_MASK 0xFF000000
0084 #define CMD_BYTE2_SHIFT 24
0085
0086
0087 #define CMD_BYTE1_MASK 0xFF000000
0088 #define CMD_BYTE1_SHIFT 24
0089 #define CMD_CODE_MASK 0x00FFFF00
0090 #define CMD_CODE_SHIFT 8
0091 #define BUFNO_MASK 0x00000006
0092 #define BUFNO_SHIFT 1
0093 #define START_BIT BIT(0)
0094
0095
0096 #define COL_ADDR_MASK 0x0000FFFF
0097 #define COL_ADDR_SHIFT 0
0098 #define COL_ADDR(pos, val) (((val) & 0xFF) << (8 * (pos)))
0099
0100
0101 #define ROW_ADDR_MASK 0x00FFFFFF
0102 #define ROW_ADDR_SHIFT 0
0103 #define ROW_ADDR(pos, val) (((val) & 0xFF) << (8 * (pos)))
0104
0105 #define ROW_ADDR_CHIP_SEL_RB_MASK 0xF0000000
0106 #define ROW_ADDR_CHIP_SEL_RB_SHIFT 28
0107 #define ROW_ADDR_CHIP_SEL_MASK 0x0F000000
0108 #define ROW_ADDR_CHIP_SEL_SHIFT 24
0109
0110
0111 #define STATUS_BYTE1_MASK 0x000000FF
0112
0113
0114 #define CONFIG_ECC_SRAM_ADDR_MASK 0x7FC00000
0115 #define CONFIG_ECC_SRAM_ADDR_SHIFT 22
0116 #define CONFIG_ECC_SRAM_REQ_BIT BIT(21)
0117 #define CONFIG_DMA_REQ_BIT BIT(20)
0118 #define CONFIG_ECC_MODE_MASK 0x000E0000
0119 #define CONFIG_ECC_MODE_SHIFT 17
0120 #define CONFIG_FAST_FLASH_BIT BIT(16)
0121 #define CONFIG_16BIT BIT(7)
0122 #define CONFIG_BOOT_MODE_BIT BIT(6)
0123 #define CONFIG_ADDR_AUTO_INCR_BIT BIT(5)
0124 #define CONFIG_BUFNO_AUTO_INCR_BIT BIT(4)
0125 #define CONFIG_PAGE_CNT_MASK 0xF
0126 #define CONFIG_PAGE_CNT_SHIFT 0
0127
0128
0129 #define IDLE_IRQ_BIT BIT(29)
0130 #define IDLE_EN_BIT BIT(20)
0131 #define CMD_DONE_CLEAR_BIT BIT(18)
0132 #define IDLE_CLEAR_BIT BIT(17)
0133
0134
0135
0136
0137
0138
0139
0140 #define ECC_SRAM_ADDR (PAGE_2K + OOB_MAX - 8)
0141
0142 #define ECC_STATUS 0x4
0143 #define ECC_STATUS_MASK 0x80
0144 #define ECC_STATUS_ERR_COUNT 0x3F
0145
0146 enum vf610_nfc_variant {
0147 NFC_VFC610 = 1,
0148 };
0149
0150 struct vf610_nfc {
0151 struct nand_controller base;
0152 struct nand_chip chip;
0153 struct device *dev;
0154 void __iomem *regs;
0155 struct completion cmd_done;
0156
0157 enum vf610_nfc_variant variant;
0158 struct clk *clk;
0159
0160
0161
0162
0163
0164 bool data_access;
0165 u32 ecc_mode;
0166 };
0167
0168 static inline struct vf610_nfc *chip_to_nfc(struct nand_chip *chip)
0169 {
0170 return container_of(chip, struct vf610_nfc, chip);
0171 }
0172
0173 static inline u32 vf610_nfc_read(struct vf610_nfc *nfc, uint reg)
0174 {
0175 return readl(nfc->regs + reg);
0176 }
0177
0178 static inline void vf610_nfc_write(struct vf610_nfc *nfc, uint reg, u32 val)
0179 {
0180 writel(val, nfc->regs + reg);
0181 }
0182
0183 static inline void vf610_nfc_set(struct vf610_nfc *nfc, uint reg, u32 bits)
0184 {
0185 vf610_nfc_write(nfc, reg, vf610_nfc_read(nfc, reg) | bits);
0186 }
0187
0188 static inline void vf610_nfc_clear(struct vf610_nfc *nfc, uint reg, u32 bits)
0189 {
0190 vf610_nfc_write(nfc, reg, vf610_nfc_read(nfc, reg) & ~bits);
0191 }
0192
0193 static inline void vf610_nfc_set_field(struct vf610_nfc *nfc, u32 reg,
0194 u32 mask, u32 shift, u32 val)
0195 {
0196 vf610_nfc_write(nfc, reg,
0197 (vf610_nfc_read(nfc, reg) & (~mask)) | val << shift);
0198 }
0199
0200 static inline bool vf610_nfc_kernel_is_little_endian(void)
0201 {
0202 #ifdef __LITTLE_ENDIAN
0203 return true;
0204 #else
0205 return false;
0206 #endif
0207 }
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228 static inline void vf610_nfc_rd_from_sram(void *dst, const void __iomem *src,
0229 size_t len, bool fix_endian)
0230 {
0231 if (vf610_nfc_kernel_is_little_endian() && fix_endian) {
0232 unsigned int i;
0233
0234 for (i = 0; i < len; i += 4) {
0235 u32 val = swab32(__raw_readl(src + i));
0236
0237 memcpy(dst + i, &val, min(sizeof(val), len - i));
0238 }
0239 } else {
0240 memcpy_fromio(dst, src, len);
0241 }
0242 }
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263 static inline void vf610_nfc_wr_to_sram(void __iomem *dst, const void *src,
0264 size_t len, bool fix_endian)
0265 {
0266 if (vf610_nfc_kernel_is_little_endian() && fix_endian) {
0267 unsigned int i;
0268
0269 for (i = 0; i < len; i += 4) {
0270 u32 val;
0271
0272 memcpy(&val, src + i, min(sizeof(val), len - i));
0273 __raw_writel(swab32(val), dst + i);
0274 }
0275 } else {
0276 memcpy_toio(dst, src, len);
0277 }
0278 }
0279
0280
0281 static inline void vf610_nfc_clear_status(struct vf610_nfc *nfc)
0282 {
0283 u32 tmp = vf610_nfc_read(nfc, NFC_IRQ_STATUS);
0284
0285 tmp |= CMD_DONE_CLEAR_BIT | IDLE_CLEAR_BIT;
0286 vf610_nfc_write(nfc, NFC_IRQ_STATUS, tmp);
0287 }
0288
0289 static void vf610_nfc_done(struct vf610_nfc *nfc)
0290 {
0291 unsigned long timeout = msecs_to_jiffies(100);
0292
0293
0294
0295
0296
0297
0298
0299
0300 vf610_nfc_set(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
0301 vf610_nfc_set(nfc, NFC_FLASH_CMD2, START_BIT);
0302
0303 if (!wait_for_completion_timeout(&nfc->cmd_done, timeout))
0304 dev_warn(nfc->dev, "Timeout while waiting for BUSY.\n");
0305
0306 vf610_nfc_clear_status(nfc);
0307 }
0308
0309 static irqreturn_t vf610_nfc_irq(int irq, void *data)
0310 {
0311 struct vf610_nfc *nfc = data;
0312
0313 vf610_nfc_clear(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
0314 complete(&nfc->cmd_done);
0315
0316 return IRQ_HANDLED;
0317 }
0318
0319 static inline void vf610_nfc_ecc_mode(struct vf610_nfc *nfc, int ecc_mode)
0320 {
0321 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
0322 CONFIG_ECC_MODE_MASK,
0323 CONFIG_ECC_MODE_SHIFT, ecc_mode);
0324 }
0325
0326 static inline void vf610_nfc_run(struct vf610_nfc *nfc, u32 col, u32 row,
0327 u32 cmd1, u32 cmd2, u32 trfr_sz)
0328 {
0329 vf610_nfc_set_field(nfc, NFC_COL_ADDR, COL_ADDR_MASK,
0330 COL_ADDR_SHIFT, col);
0331
0332 vf610_nfc_set_field(nfc, NFC_ROW_ADDR, ROW_ADDR_MASK,
0333 ROW_ADDR_SHIFT, row);
0334
0335 vf610_nfc_write(nfc, NFC_SECTOR_SIZE, trfr_sz);
0336 vf610_nfc_write(nfc, NFC_FLASH_CMD1, cmd1);
0337 vf610_nfc_write(nfc, NFC_FLASH_CMD2, cmd2);
0338
0339 dev_dbg(nfc->dev,
0340 "col 0x%04x, row 0x%08x, cmd1 0x%08x, cmd2 0x%08x, len %d\n",
0341 col, row, cmd1, cmd2, trfr_sz);
0342
0343 vf610_nfc_done(nfc);
0344 }
0345
0346 static inline const struct nand_op_instr *
0347 vf610_get_next_instr(const struct nand_subop *subop, int *op_id)
0348 {
0349 if (*op_id + 1 >= subop->ninstrs)
0350 return NULL;
0351
0352 (*op_id)++;
0353
0354 return &subop->instrs[*op_id];
0355 }
0356
0357 static int vf610_nfc_cmd(struct nand_chip *chip,
0358 const struct nand_subop *subop)
0359 {
0360 const struct nand_op_instr *instr;
0361 struct vf610_nfc *nfc = chip_to_nfc(chip);
0362 int op_id = -1, trfr_sz = 0, offset = 0;
0363 u32 col = 0, row = 0, cmd1 = 0, cmd2 = 0, code = 0;
0364 bool force8bit = false;
0365
0366
0367
0368
0369
0370
0371
0372 instr = vf610_get_next_instr(subop, &op_id);
0373 if (!instr)
0374 return -EINVAL;
0375
0376 if (instr && instr->type == NAND_OP_CMD_INSTR) {
0377 cmd2 |= instr->ctx.cmd.opcode << CMD_BYTE1_SHIFT;
0378 code |= COMMAND_CMD_BYTE1;
0379
0380 instr = vf610_get_next_instr(subop, &op_id);
0381 }
0382
0383 if (instr && instr->type == NAND_OP_ADDR_INSTR) {
0384 int naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
0385 int i = nand_subop_get_addr_start_off(subop, op_id);
0386
0387 for (; i < naddrs; i++) {
0388 u8 val = instr->ctx.addr.addrs[i];
0389
0390 if (i < 2)
0391 col |= COL_ADDR(i, val);
0392 else
0393 row |= ROW_ADDR(i - 2, val);
0394 }
0395 code |= COMMAND_NADDR_BYTES(naddrs);
0396
0397 instr = vf610_get_next_instr(subop, &op_id);
0398 }
0399
0400 if (instr && instr->type == NAND_OP_DATA_OUT_INSTR) {
0401 trfr_sz = nand_subop_get_data_len(subop, op_id);
0402 offset = nand_subop_get_data_start_off(subop, op_id);
0403 force8bit = instr->ctx.data.force_8bit;
0404
0405
0406
0407
0408
0409 vf610_nfc_wr_to_sram(nfc->regs + NFC_MAIN_AREA(0) + offset,
0410 instr->ctx.data.buf.out + offset,
0411 trfr_sz, !nfc->data_access);
0412 code |= COMMAND_WRITE_DATA;
0413
0414 instr = vf610_get_next_instr(subop, &op_id);
0415 }
0416
0417 if (instr && instr->type == NAND_OP_CMD_INSTR) {
0418 cmd1 |= instr->ctx.cmd.opcode << CMD_BYTE2_SHIFT;
0419 code |= COMMAND_CMD_BYTE2;
0420
0421 instr = vf610_get_next_instr(subop, &op_id);
0422 }
0423
0424 if (instr && instr->type == NAND_OP_WAITRDY_INSTR) {
0425 code |= COMMAND_RB_HANDSHAKE;
0426
0427 instr = vf610_get_next_instr(subop, &op_id);
0428 }
0429
0430 if (instr && instr->type == NAND_OP_DATA_IN_INSTR) {
0431 trfr_sz = nand_subop_get_data_len(subop, op_id);
0432 offset = nand_subop_get_data_start_off(subop, op_id);
0433 force8bit = instr->ctx.data.force_8bit;
0434
0435 code |= COMMAND_READ_DATA;
0436 }
0437
0438 if (force8bit && (chip->options & NAND_BUSWIDTH_16))
0439 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
0440
0441 cmd2 |= code << CMD_CODE_SHIFT;
0442
0443 vf610_nfc_run(nfc, col, row, cmd1, cmd2, trfr_sz);
0444
0445 if (instr && instr->type == NAND_OP_DATA_IN_INSTR) {
0446
0447
0448
0449
0450 vf610_nfc_rd_from_sram(instr->ctx.data.buf.in + offset,
0451 nfc->regs + NFC_MAIN_AREA(0) + offset,
0452 trfr_sz, !nfc->data_access);
0453 }
0454
0455 if (force8bit && (chip->options & NAND_BUSWIDTH_16))
0456 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
0457
0458 return 0;
0459 }
0460
0461 static const struct nand_op_parser vf610_nfc_op_parser = NAND_OP_PARSER(
0462 NAND_OP_PARSER_PATTERN(vf610_nfc_cmd,
0463 NAND_OP_PARSER_PAT_CMD_ELEM(true),
0464 NAND_OP_PARSER_PAT_ADDR_ELEM(true, 5),
0465 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(true, PAGE_2K + OOB_MAX),
0466 NAND_OP_PARSER_PAT_CMD_ELEM(true),
0467 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
0468 NAND_OP_PARSER_PATTERN(vf610_nfc_cmd,
0469 NAND_OP_PARSER_PAT_CMD_ELEM(true),
0470 NAND_OP_PARSER_PAT_ADDR_ELEM(true, 5),
0471 NAND_OP_PARSER_PAT_CMD_ELEM(true),
0472 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
0473 NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, PAGE_2K + OOB_MAX)),
0474 );
0475
0476
0477
0478
0479 static void vf610_nfc_select_target(struct nand_chip *chip, unsigned int cs)
0480 {
0481 struct vf610_nfc *nfc = chip_to_nfc(chip);
0482 u32 tmp;
0483
0484
0485 if (nfc->variant != NFC_VFC610)
0486 return;
0487
0488 tmp = vf610_nfc_read(nfc, NFC_ROW_ADDR);
0489 tmp &= ~(ROW_ADDR_CHIP_SEL_RB_MASK | ROW_ADDR_CHIP_SEL_MASK);
0490 tmp |= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT;
0491 tmp |= BIT(cs) << ROW_ADDR_CHIP_SEL_SHIFT;
0492
0493 vf610_nfc_write(nfc, NFC_ROW_ADDR, tmp);
0494 }
0495
0496 static int vf610_nfc_exec_op(struct nand_chip *chip,
0497 const struct nand_operation *op,
0498 bool check_only)
0499 {
0500 if (!check_only)
0501 vf610_nfc_select_target(chip, op->cs);
0502
0503 return nand_op_parser_exec_op(chip, &vf610_nfc_op_parser, op,
0504 check_only);
0505 }
0506
0507 static inline int vf610_nfc_correct_data(struct nand_chip *chip, uint8_t *dat,
0508 uint8_t *oob, int page)
0509 {
0510 struct vf610_nfc *nfc = chip_to_nfc(chip);
0511 struct mtd_info *mtd = nand_to_mtd(chip);
0512 u32 ecc_status_off = NFC_MAIN_AREA(0) + ECC_SRAM_ADDR + ECC_STATUS;
0513 u8 ecc_status;
0514 u8 ecc_count;
0515 int flips_threshold = nfc->chip.ecc.strength / 2;
0516
0517 ecc_status = vf610_nfc_read(nfc, ecc_status_off) & 0xff;
0518 ecc_count = ecc_status & ECC_STATUS_ERR_COUNT;
0519
0520 if (!(ecc_status & ECC_STATUS_MASK))
0521 return ecc_count;
0522
0523 nfc->data_access = true;
0524 nand_read_oob_op(&nfc->chip, page, 0, oob, mtd->oobsize);
0525 nfc->data_access = false;
0526
0527
0528
0529
0530
0531 return nand_check_erased_ecc_chunk(dat, nfc->chip.ecc.size, oob,
0532 mtd->oobsize, NULL, 0,
0533 flips_threshold);
0534 }
0535
0536 static void vf610_nfc_fill_row(struct nand_chip *chip, int page, u32 *code,
0537 u32 *row)
0538 {
0539 *row = ROW_ADDR(0, page & 0xff) | ROW_ADDR(1, page >> 8);
0540 *code |= COMMAND_RAR_BYTE1 | COMMAND_RAR_BYTE2;
0541
0542 if (chip->options & NAND_ROW_ADDR_3) {
0543 *row |= ROW_ADDR(2, page >> 16);
0544 *code |= COMMAND_RAR_BYTE3;
0545 }
0546 }
0547
0548 static int vf610_nfc_read_page(struct nand_chip *chip, uint8_t *buf,
0549 int oob_required, int page)
0550 {
0551 struct vf610_nfc *nfc = chip_to_nfc(chip);
0552 struct mtd_info *mtd = nand_to_mtd(chip);
0553 int trfr_sz = mtd->writesize + mtd->oobsize;
0554 u32 row = 0, cmd1 = 0, cmd2 = 0, code = 0;
0555 int stat;
0556
0557 vf610_nfc_select_target(chip, chip->cur_cs);
0558
0559 cmd2 |= NAND_CMD_READ0 << CMD_BYTE1_SHIFT;
0560 code |= COMMAND_CMD_BYTE1 | COMMAND_CAR_BYTE1 | COMMAND_CAR_BYTE2;
0561
0562 vf610_nfc_fill_row(chip, page, &code, &row);
0563
0564 cmd1 |= NAND_CMD_READSTART << CMD_BYTE2_SHIFT;
0565 code |= COMMAND_CMD_BYTE2 | COMMAND_RB_HANDSHAKE | COMMAND_READ_DATA;
0566
0567 cmd2 |= code << CMD_CODE_SHIFT;
0568
0569 vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
0570 vf610_nfc_run(nfc, 0, row, cmd1, cmd2, trfr_sz);
0571 vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
0572
0573
0574
0575
0576
0577 vf610_nfc_rd_from_sram(buf, nfc->regs + NFC_MAIN_AREA(0),
0578 mtd->writesize, false);
0579 if (oob_required)
0580 vf610_nfc_rd_from_sram(chip->oob_poi,
0581 nfc->regs + NFC_MAIN_AREA(0) +
0582 mtd->writesize,
0583 mtd->oobsize, false);
0584
0585 stat = vf610_nfc_correct_data(chip, buf, chip->oob_poi, page);
0586
0587 if (stat < 0) {
0588 mtd->ecc_stats.failed++;
0589 return 0;
0590 } else {
0591 mtd->ecc_stats.corrected += stat;
0592 return stat;
0593 }
0594 }
0595
0596 static int vf610_nfc_write_page(struct nand_chip *chip, const uint8_t *buf,
0597 int oob_required, int page)
0598 {
0599 struct vf610_nfc *nfc = chip_to_nfc(chip);
0600 struct mtd_info *mtd = nand_to_mtd(chip);
0601 int trfr_sz = mtd->writesize + mtd->oobsize;
0602 u32 row = 0, cmd1 = 0, cmd2 = 0, code = 0;
0603 u8 status;
0604 int ret;
0605
0606 vf610_nfc_select_target(chip, chip->cur_cs);
0607
0608 cmd2 |= NAND_CMD_SEQIN << CMD_BYTE1_SHIFT;
0609 code |= COMMAND_CMD_BYTE1 | COMMAND_CAR_BYTE1 | COMMAND_CAR_BYTE2;
0610
0611 vf610_nfc_fill_row(chip, page, &code, &row);
0612
0613 cmd1 |= NAND_CMD_PAGEPROG << CMD_BYTE2_SHIFT;
0614 code |= COMMAND_CMD_BYTE2 | COMMAND_WRITE_DATA;
0615
0616
0617
0618
0619
0620 vf610_nfc_wr_to_sram(nfc->regs + NFC_MAIN_AREA(0), buf,
0621 mtd->writesize, false);
0622
0623 code |= COMMAND_RB_HANDSHAKE;
0624 cmd2 |= code << CMD_CODE_SHIFT;
0625
0626 vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
0627 vf610_nfc_run(nfc, 0, row, cmd1, cmd2, trfr_sz);
0628 vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
0629
0630 ret = nand_status_op(chip, &status);
0631 if (ret)
0632 return ret;
0633
0634 if (status & NAND_STATUS_FAIL)
0635 return -EIO;
0636
0637 return 0;
0638 }
0639
0640 static int vf610_nfc_read_page_raw(struct nand_chip *chip, u8 *buf,
0641 int oob_required, int page)
0642 {
0643 struct vf610_nfc *nfc = chip_to_nfc(chip);
0644 int ret;
0645
0646 nfc->data_access = true;
0647 ret = nand_read_page_raw(chip, buf, oob_required, page);
0648 nfc->data_access = false;
0649
0650 return ret;
0651 }
0652
0653 static int vf610_nfc_write_page_raw(struct nand_chip *chip, const u8 *buf,
0654 int oob_required, int page)
0655 {
0656 struct vf610_nfc *nfc = chip_to_nfc(chip);
0657 struct mtd_info *mtd = nand_to_mtd(chip);
0658 int ret;
0659
0660 nfc->data_access = true;
0661 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
0662 if (!ret && oob_required)
0663 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
0664 false);
0665 nfc->data_access = false;
0666
0667 if (ret)
0668 return ret;
0669
0670 return nand_prog_page_end_op(chip);
0671 }
0672
0673 static int vf610_nfc_read_oob(struct nand_chip *chip, int page)
0674 {
0675 struct vf610_nfc *nfc = chip_to_nfc(chip);
0676 int ret;
0677
0678 nfc->data_access = true;
0679 ret = nand_read_oob_std(chip, page);
0680 nfc->data_access = false;
0681
0682 return ret;
0683 }
0684
0685 static int vf610_nfc_write_oob(struct nand_chip *chip, int page)
0686 {
0687 struct mtd_info *mtd = nand_to_mtd(chip);
0688 struct vf610_nfc *nfc = chip_to_nfc(chip);
0689 int ret;
0690
0691 nfc->data_access = true;
0692 ret = nand_prog_page_begin_op(chip, page, mtd->writesize,
0693 chip->oob_poi, mtd->oobsize);
0694 nfc->data_access = false;
0695
0696 if (ret)
0697 return ret;
0698
0699 return nand_prog_page_end_op(chip);
0700 }
0701
0702 static const struct of_device_id vf610_nfc_dt_ids[] = {
0703 { .compatible = "fsl,vf610-nfc", .data = (void *)NFC_VFC610 },
0704 { }
0705 };
0706 MODULE_DEVICE_TABLE(of, vf610_nfc_dt_ids);
0707
0708 static void vf610_nfc_preinit_controller(struct vf610_nfc *nfc)
0709 {
0710 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
0711 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_ADDR_AUTO_INCR_BIT);
0712 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BUFNO_AUTO_INCR_BIT);
0713 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BOOT_MODE_BIT);
0714 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_DMA_REQ_BIT);
0715 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_FAST_FLASH_BIT);
0716 vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
0717
0718
0719 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG, CONFIG_PAGE_CNT_MASK,
0720 CONFIG_PAGE_CNT_SHIFT, 1);
0721 }
0722
0723 static void vf610_nfc_init_controller(struct vf610_nfc *nfc)
0724 {
0725 if (nfc->chip.options & NAND_BUSWIDTH_16)
0726 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
0727 else
0728 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
0729
0730 if (nfc->chip.ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
0731
0732 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
0733 CONFIG_ECC_SRAM_ADDR_MASK,
0734 CONFIG_ECC_SRAM_ADDR_SHIFT,
0735 ECC_SRAM_ADDR >> 3);
0736
0737
0738 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_ECC_SRAM_REQ_BIT);
0739 }
0740 }
0741
0742 static int vf610_nfc_attach_chip(struct nand_chip *chip)
0743 {
0744 struct mtd_info *mtd = nand_to_mtd(chip);
0745 struct vf610_nfc *nfc = chip_to_nfc(chip);
0746
0747 vf610_nfc_init_controller(nfc);
0748
0749
0750 if (chip->bbt_options & NAND_BBT_USE_FLASH)
0751 chip->bbt_options |= NAND_BBT_NO_OOB;
0752
0753
0754 if (mtd->writesize + mtd->oobsize > PAGE_2K + OOB_MAX - 8) {
0755 dev_err(nfc->dev, "Unsupported flash page size\n");
0756 return -ENXIO;
0757 }
0758
0759 if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
0760 return 0;
0761
0762 if (mtd->writesize != PAGE_2K && mtd->oobsize < 64) {
0763 dev_err(nfc->dev, "Unsupported flash with hwecc\n");
0764 return -ENXIO;
0765 }
0766
0767 if (chip->ecc.size != mtd->writesize) {
0768 dev_err(nfc->dev, "Step size needs to be page size\n");
0769 return -ENXIO;
0770 }
0771
0772
0773 if (mtd->oobsize > 64)
0774 mtd->oobsize = 64;
0775
0776
0777 mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
0778 if (chip->ecc.strength == 32) {
0779 nfc->ecc_mode = ECC_60_BYTE;
0780 chip->ecc.bytes = 60;
0781 } else if (chip->ecc.strength == 24) {
0782 nfc->ecc_mode = ECC_45_BYTE;
0783 chip->ecc.bytes = 45;
0784 } else {
0785 dev_err(nfc->dev, "Unsupported ECC strength\n");
0786 return -ENXIO;
0787 }
0788
0789 chip->ecc.read_page = vf610_nfc_read_page;
0790 chip->ecc.write_page = vf610_nfc_write_page;
0791 chip->ecc.read_page_raw = vf610_nfc_read_page_raw;
0792 chip->ecc.write_page_raw = vf610_nfc_write_page_raw;
0793 chip->ecc.read_oob = vf610_nfc_read_oob;
0794 chip->ecc.write_oob = vf610_nfc_write_oob;
0795
0796 chip->ecc.size = PAGE_2K;
0797
0798 return 0;
0799 }
0800
0801 static const struct nand_controller_ops vf610_nfc_controller_ops = {
0802 .attach_chip = vf610_nfc_attach_chip,
0803 .exec_op = vf610_nfc_exec_op,
0804
0805 };
0806
0807 static int vf610_nfc_probe(struct platform_device *pdev)
0808 {
0809 struct vf610_nfc *nfc;
0810 struct mtd_info *mtd;
0811 struct nand_chip *chip;
0812 struct device_node *child;
0813 const struct of_device_id *of_id;
0814 int err;
0815 int irq;
0816
0817 nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
0818 if (!nfc)
0819 return -ENOMEM;
0820
0821 nfc->dev = &pdev->dev;
0822 chip = &nfc->chip;
0823 mtd = nand_to_mtd(chip);
0824
0825 mtd->owner = THIS_MODULE;
0826 mtd->dev.parent = nfc->dev;
0827 mtd->name = DRV_NAME;
0828
0829 irq = platform_get_irq(pdev, 0);
0830 if (irq <= 0)
0831 return -EINVAL;
0832
0833 nfc->regs = devm_platform_ioremap_resource(pdev, 0);
0834 if (IS_ERR(nfc->regs))
0835 return PTR_ERR(nfc->regs);
0836
0837 nfc->clk = devm_clk_get(&pdev->dev, NULL);
0838 if (IS_ERR(nfc->clk))
0839 return PTR_ERR(nfc->clk);
0840
0841 err = clk_prepare_enable(nfc->clk);
0842 if (err) {
0843 dev_err(nfc->dev, "Unable to enable clock!\n");
0844 return err;
0845 }
0846
0847 of_id = of_match_device(vf610_nfc_dt_ids, &pdev->dev);
0848 if (!of_id) {
0849 err = -ENODEV;
0850 goto err_disable_clk;
0851 }
0852
0853 nfc->variant = (enum vf610_nfc_variant)of_id->data;
0854
0855 for_each_available_child_of_node(nfc->dev->of_node, child) {
0856 if (of_device_is_compatible(child, "fsl,vf610-nfc-nandcs")) {
0857
0858 if (nand_get_flash_node(chip)) {
0859 dev_err(nfc->dev,
0860 "Only one NAND chip supported!\n");
0861 err = -EINVAL;
0862 of_node_put(child);
0863 goto err_disable_clk;
0864 }
0865
0866 nand_set_flash_node(chip, child);
0867 }
0868 }
0869
0870 if (!nand_get_flash_node(chip)) {
0871 dev_err(nfc->dev, "NAND chip sub-node missing!\n");
0872 err = -ENODEV;
0873 goto err_disable_clk;
0874 }
0875
0876 chip->options |= NAND_NO_SUBPAGE_WRITE;
0877
0878 init_completion(&nfc->cmd_done);
0879
0880 err = devm_request_irq(nfc->dev, irq, vf610_nfc_irq, 0, DRV_NAME, nfc);
0881 if (err) {
0882 dev_err(nfc->dev, "Error requesting IRQ!\n");
0883 goto err_disable_clk;
0884 }
0885
0886 vf610_nfc_preinit_controller(nfc);
0887
0888 nand_controller_init(&nfc->base);
0889 nfc->base.ops = &vf610_nfc_controller_ops;
0890 chip->controller = &nfc->base;
0891
0892
0893 err = nand_scan(chip, 1);
0894 if (err)
0895 goto err_disable_clk;
0896
0897 platform_set_drvdata(pdev, nfc);
0898
0899
0900 err = mtd_device_register(mtd, NULL, 0);
0901 if (err)
0902 goto err_cleanup_nand;
0903 return 0;
0904
0905 err_cleanup_nand:
0906 nand_cleanup(chip);
0907 err_disable_clk:
0908 clk_disable_unprepare(nfc->clk);
0909 return err;
0910 }
0911
0912 static int vf610_nfc_remove(struct platform_device *pdev)
0913 {
0914 struct vf610_nfc *nfc = platform_get_drvdata(pdev);
0915 struct nand_chip *chip = &nfc->chip;
0916 int ret;
0917
0918 ret = mtd_device_unregister(nand_to_mtd(chip));
0919 WARN_ON(ret);
0920 nand_cleanup(chip);
0921 clk_disable_unprepare(nfc->clk);
0922 return 0;
0923 }
0924
0925 #ifdef CONFIG_PM_SLEEP
0926 static int vf610_nfc_suspend(struct device *dev)
0927 {
0928 struct vf610_nfc *nfc = dev_get_drvdata(dev);
0929
0930 clk_disable_unprepare(nfc->clk);
0931 return 0;
0932 }
0933
0934 static int vf610_nfc_resume(struct device *dev)
0935 {
0936 struct vf610_nfc *nfc = dev_get_drvdata(dev);
0937 int err;
0938
0939 err = clk_prepare_enable(nfc->clk);
0940 if (err)
0941 return err;
0942
0943 vf610_nfc_preinit_controller(nfc);
0944 vf610_nfc_init_controller(nfc);
0945 return 0;
0946 }
0947 #endif
0948
0949 static SIMPLE_DEV_PM_OPS(vf610_nfc_pm_ops, vf610_nfc_suspend, vf610_nfc_resume);
0950
0951 static struct platform_driver vf610_nfc_driver = {
0952 .driver = {
0953 .name = DRV_NAME,
0954 .of_match_table = vf610_nfc_dt_ids,
0955 .pm = &vf610_nfc_pm_ops,
0956 },
0957 .probe = vf610_nfc_probe,
0958 .remove = vf610_nfc_remove,
0959 };
0960
0961 module_platform_driver(vf610_nfc_driver);
0962
0963 MODULE_AUTHOR("Stefan Agner <stefan.agner@toradex.com>");
0964 MODULE_DESCRIPTION("Freescale VF610/MPC5125 NFC MTD NAND driver");
0965 MODULE_LICENSE("GPL");