hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/mtd/nand/raw/vf610_nfc.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0+
12 /*
23 * Copyright 2009-2015 Freescale Semiconductor, Inc. and others
34 *
....@@ -9,11 +10,6 @@
910 * Jason Jin <Jason.jin@freescale.com>
1011 *
1112 * Based on original driver mpc5121_nfc.c.
12
- *
13
- * This is free software; you can redistribute it and/or modify it
14
- * under the terms of the GNU General Public License as published by
15
- * the Free Software Foundation; either version 2 of the License, or
16
- * (at your option) any later version.
1713 *
1814 * Limitations:
1915 * - Untested on MPC5125 and M54418.
....@@ -152,6 +148,7 @@
152148 };
153149
154150 struct vf610_nfc {
151
+ struct nand_controller base;
155152 struct nand_chip chip;
156153 struct device *dev;
157154 void __iomem *regs;
....@@ -167,11 +164,6 @@
167164 bool data_access;
168165 u32 ecc_mode;
169166 };
170
-
171
-static inline struct vf610_nfc *mtd_to_nfc(struct mtd_info *mtd)
172
-{
173
- return container_of(mtd_to_nand(mtd), struct vf610_nfc, chip);
174
-}
175167
176168 static inline struct vf610_nfc *chip_to_nfc(struct nand_chip *chip)
177169 {
....@@ -316,8 +308,7 @@
316308
317309 static irqreturn_t vf610_nfc_irq(int irq, void *data)
318310 {
319
- struct mtd_info *mtd = data;
320
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
311
+ struct vf610_nfc *nfc = data;
321312
322313 vf610_nfc_clear(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
323314 complete(&nfc->cmd_done);
....@@ -330,11 +321,6 @@
330321 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
331322 CONFIG_ECC_MODE_MASK,
332323 CONFIG_ECC_MODE_SHIFT, ecc_mode);
333
-}
334
-
335
-static inline void vf610_nfc_transfer_size(struct vf610_nfc *nfc, int size)
336
-{
337
- vf610_nfc_write(nfc, NFC_SECTOR_SIZE, size);
338324 }
339325
340326 static inline void vf610_nfc_run(struct vf610_nfc *nfc, u32 col, u32 row,
....@@ -373,7 +359,7 @@
373359 {
374360 const struct nand_op_instr *instr;
375361 struct vf610_nfc *nfc = chip_to_nfc(chip);
376
- int op_id = -1, trfr_sz = 0, offset;
362
+ int op_id = -1, trfr_sz = 0, offset = 0;
377363 u32 col = 0, row = 0, cmd1 = 0, cmd2 = 0, code = 0;
378364 bool force8bit = false;
379365
....@@ -487,40 +473,42 @@
487473 NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, PAGE_2K + OOB_MAX)),
488474 );
489475
490
-static int vf610_nfc_exec_op(struct nand_chip *chip,
491
- const struct nand_operation *op,
492
- bool check_only)
493
-{
494
- return nand_op_parser_exec_op(chip, &vf610_nfc_op_parser, op,
495
- check_only);
496
-}
497
-
498476 /*
499477 * This function supports Vybrid only (MPC5125 would have full RB and four CS)
500478 */
501
-static void vf610_nfc_select_chip(struct mtd_info *mtd, int chip)
479
+static void vf610_nfc_select_target(struct nand_chip *chip, unsigned int cs)
502480 {
503
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
504
- u32 tmp = vf610_nfc_read(nfc, NFC_ROW_ADDR);
481
+ struct vf610_nfc *nfc = chip_to_nfc(chip);
482
+ u32 tmp;
505483
506484 /* Vybrid only (MPC5125 would have full RB and four CS) */
507485 if (nfc->variant != NFC_VFC610)
508486 return;
509487
488
+ tmp = vf610_nfc_read(nfc, NFC_ROW_ADDR);
510489 tmp &= ~(ROW_ADDR_CHIP_SEL_RB_MASK | ROW_ADDR_CHIP_SEL_MASK);
511
-
512
- if (chip >= 0) {
513
- tmp |= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT;
514
- tmp |= BIT(chip) << ROW_ADDR_CHIP_SEL_SHIFT;
515
- }
490
+ tmp |= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT;
491
+ tmp |= BIT(cs) << ROW_ADDR_CHIP_SEL_SHIFT;
516492
517493 vf610_nfc_write(nfc, NFC_ROW_ADDR, tmp);
518494 }
519495
520
-static inline int vf610_nfc_correct_data(struct mtd_info *mtd, uint8_t *dat,
496
+static int vf610_nfc_exec_op(struct nand_chip *chip,
497
+ const struct nand_operation *op,
498
+ bool check_only)
499
+{
500
+ if (!check_only)
501
+ vf610_nfc_select_target(chip, op->cs);
502
+
503
+ return nand_op_parser_exec_op(chip, &vf610_nfc_op_parser, op,
504
+ check_only);
505
+}
506
+
507
+static inline int vf610_nfc_correct_data(struct nand_chip *chip, uint8_t *dat,
521508 uint8_t *oob, int page)
522509 {
523
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
510
+ struct vf610_nfc *nfc = chip_to_nfc(chip);
511
+ struct mtd_info *mtd = nand_to_mtd(chip);
524512 u32 ecc_status_off = NFC_MAIN_AREA(0) + ECC_SRAM_ADDR + ECC_STATUS;
525513 u8 ecc_status;
526514 u8 ecc_count;
....@@ -557,13 +545,16 @@
557545 }
558546 }
559547
560
-static int vf610_nfc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
561
- uint8_t *buf, int oob_required, int page)
548
+static int vf610_nfc_read_page(struct nand_chip *chip, uint8_t *buf,
549
+ int oob_required, int page)
562550 {
563
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
551
+ struct vf610_nfc *nfc = chip_to_nfc(chip);
552
+ struct mtd_info *mtd = nand_to_mtd(chip);
564553 int trfr_sz = mtd->writesize + mtd->oobsize;
565554 u32 row = 0, cmd1 = 0, cmd2 = 0, code = 0;
566555 int stat;
556
+
557
+ vf610_nfc_select_target(chip, chip->cur_cs);
567558
568559 cmd2 |= NAND_CMD_READ0 << CMD_BYTE1_SHIFT;
569560 code |= COMMAND_CMD_BYTE1 | COMMAND_CAR_BYTE1 | COMMAND_CAR_BYTE2;
....@@ -591,7 +582,7 @@
591582 mtd->writesize,
592583 mtd->oobsize, false);
593584
594
- stat = vf610_nfc_correct_data(mtd, buf, chip->oob_poi, page);
585
+ stat = vf610_nfc_correct_data(chip, buf, chip->oob_poi, page);
595586
596587 if (stat < 0) {
597588 mtd->ecc_stats.failed++;
....@@ -602,14 +593,17 @@
602593 }
603594 }
604595
605
-static int vf610_nfc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
606
- const uint8_t *buf, int oob_required, int page)
596
+static int vf610_nfc_write_page(struct nand_chip *chip, const uint8_t *buf,
597
+ int oob_required, int page)
607598 {
608
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
599
+ struct vf610_nfc *nfc = chip_to_nfc(chip);
600
+ struct mtd_info *mtd = nand_to_mtd(chip);
609601 int trfr_sz = mtd->writesize + mtd->oobsize;
610602 u32 row = 0, cmd1 = 0, cmd2 = 0, code = 0;
611603 u8 status;
612604 int ret;
605
+
606
+ vf610_nfc_select_target(chip, chip->cur_cs);
613607
614608 cmd2 |= NAND_CMD_SEQIN << CMD_BYTE1_SHIFT;
615609 code |= COMMAND_CMD_BYTE1 | COMMAND_CAR_BYTE1 | COMMAND_CAR_BYTE2;
....@@ -643,25 +637,24 @@
643637 return 0;
644638 }
645639
646
-static int vf610_nfc_read_page_raw(struct mtd_info *mtd,
647
- struct nand_chip *chip, u8 *buf,
640
+static int vf610_nfc_read_page_raw(struct nand_chip *chip, u8 *buf,
648641 int oob_required, int page)
649642 {
650
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
643
+ struct vf610_nfc *nfc = chip_to_nfc(chip);
651644 int ret;
652645
653646 nfc->data_access = true;
654
- ret = nand_read_page_raw(mtd, chip, buf, oob_required, page);
647
+ ret = nand_read_page_raw(chip, buf, oob_required, page);
655648 nfc->data_access = false;
656649
657650 return ret;
658651 }
659652
660
-static int vf610_nfc_write_page_raw(struct mtd_info *mtd,
661
- struct nand_chip *chip, const u8 *buf,
653
+static int vf610_nfc_write_page_raw(struct nand_chip *chip, const u8 *buf,
662654 int oob_required, int page)
663655 {
664
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
656
+ struct vf610_nfc *nfc = chip_to_nfc(chip);
657
+ struct mtd_info *mtd = nand_to_mtd(chip);
665658 int ret;
666659
667660 nfc->data_access = true;
....@@ -677,23 +670,22 @@
677670 return nand_prog_page_end_op(chip);
678671 }
679672
680
-static int vf610_nfc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
681
- int page)
673
+static int vf610_nfc_read_oob(struct nand_chip *chip, int page)
682674 {
683
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
675
+ struct vf610_nfc *nfc = chip_to_nfc(chip);
684676 int ret;
685677
686678 nfc->data_access = true;
687
- ret = nand_read_oob_std(mtd, chip, page);
679
+ ret = nand_read_oob_std(chip, page);
688680 nfc->data_access = false;
689681
690682 return ret;
691683 }
692684
693
-static int vf610_nfc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
694
- int page)
685
+static int vf610_nfc_write_oob(struct nand_chip *chip, int page)
695686 {
696
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
687
+ struct mtd_info *mtd = nand_to_mtd(chip);
688
+ struct vf610_nfc *nfc = chip_to_nfc(chip);
697689 int ret;
698690
699691 nfc->data_access = true;
....@@ -735,7 +727,7 @@
735727 else
736728 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
737729
738
- if (nfc->chip.ecc.mode == NAND_ECC_HW) {
730
+ if (nfc->chip.ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
739731 /* Set ECC status offset in SRAM */
740732 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
741733 CONFIG_ECC_SRAM_ADDR_MASK,
....@@ -750,7 +742,7 @@
750742 static int vf610_nfc_attach_chip(struct nand_chip *chip)
751743 {
752744 struct mtd_info *mtd = nand_to_mtd(chip);
753
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
745
+ struct vf610_nfc *nfc = chip_to_nfc(chip);
754746
755747 vf610_nfc_init_controller(nfc);
756748
....@@ -764,7 +756,7 @@
764756 return -ENXIO;
765757 }
766758
767
- if (chip->ecc.mode != NAND_ECC_HW)
759
+ if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
768760 return 0;
769761
770762 if (mtd->writesize != PAGE_2K && mtd->oobsize < 64) {
....@@ -782,7 +774,7 @@
782774 mtd->oobsize = 64;
783775
784776 /* Use default large page ECC layout defined in NAND core */
785
- mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
777
+ mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
786778 if (chip->ecc.strength == 32) {
787779 nfc->ecc_mode = ECC_60_BYTE;
788780 chip->ecc.bytes = 60;
....@@ -808,6 +800,8 @@
808800
809801 static const struct nand_controller_ops vf610_nfc_controller_ops = {
810802 .attach_chip = vf610_nfc_attach_chip,
803
+ .exec_op = vf610_nfc_exec_op,
804
+
811805 };
812806
813807 static int vf610_nfc_probe(struct platform_device *pdev)
....@@ -853,6 +847,11 @@
853847 }
854848
855849 of_id = of_match_device(vf610_nfc_dt_ids, &pdev->dev);
850
+ if (!of_id) {
851
+ err = -ENODEV;
852
+ goto err_disable_clk;
853
+ }
854
+
856855 nfc->variant = (enum vf610_nfc_variant)of_id->data;
857856
858857 for_each_available_child_of_node(nfc->dev->of_node, child) {
....@@ -862,6 +861,7 @@
862861 dev_err(nfc->dev,
863862 "Only one NAND chip supported!\n");
864863 err = -EINVAL;
864
+ of_node_put(child);
865865 goto err_disable_clk;
866866 }
867867
....@@ -875,14 +875,11 @@
875875 goto err_disable_clk;
876876 }
877877
878
- chip->exec_op = vf610_nfc_exec_op;
879
- chip->select_chip = vf610_nfc_select_chip;
880
-
881878 chip->options |= NAND_NO_SUBPAGE_WRITE;
882879
883880 init_completion(&nfc->cmd_done);
884881
885
- err = devm_request_irq(nfc->dev, irq, vf610_nfc_irq, 0, DRV_NAME, mtd);
882
+ err = devm_request_irq(nfc->dev, irq, vf610_nfc_irq, 0, DRV_NAME, nfc);
886883 if (err) {
887884 dev_err(nfc->dev, "Error requesting IRQ!\n");
888885 goto err_disable_clk;
....@@ -890,13 +887,16 @@
890887
891888 vf610_nfc_preinit_controller(nfc);
892889
890
+ nand_controller_init(&nfc->base);
891
+ nfc->base.ops = &vf610_nfc_controller_ops;
892
+ chip->controller = &nfc->base;
893
+
893894 /* Scan the NAND chip */
894
- chip->dummy_controller.ops = &vf610_nfc_controller_ops;
895895 err = nand_scan(chip, 1);
896896 if (err)
897897 goto err_disable_clk;
898898
899
- platform_set_drvdata(pdev, mtd);
899
+ platform_set_drvdata(pdev, nfc);
900900
901901 /* Register device in MTD */
902902 err = mtd_device_register(mtd, NULL, 0);
....@@ -913,10 +913,13 @@
913913
914914 static int vf610_nfc_remove(struct platform_device *pdev)
915915 {
916
- struct mtd_info *mtd = platform_get_drvdata(pdev);
917
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
916
+ struct vf610_nfc *nfc = platform_get_drvdata(pdev);
917
+ struct nand_chip *chip = &nfc->chip;
918
+ int ret;
918919
919
- nand_release(mtd_to_nand(mtd));
920
+ ret = mtd_device_unregister(nand_to_mtd(chip));
921
+ WARN_ON(ret);
922
+ nand_cleanup(chip);
920923 clk_disable_unprepare(nfc->clk);
921924 return 0;
922925 }
....@@ -924,8 +927,7 @@
924927 #ifdef CONFIG_PM_SLEEP
925928 static int vf610_nfc_suspend(struct device *dev)
926929 {
927
- struct mtd_info *mtd = dev_get_drvdata(dev);
928
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
930
+ struct vf610_nfc *nfc = dev_get_drvdata(dev);
929931
930932 clk_disable_unprepare(nfc->clk);
931933 return 0;
....@@ -933,10 +935,8 @@
933935
934936 static int vf610_nfc_resume(struct device *dev)
935937 {
938
+ struct vf610_nfc *nfc = dev_get_drvdata(dev);
936939 int err;
937
-
938
- struct mtd_info *mtd = dev_get_drvdata(dev);
939
- struct vf610_nfc *nfc = mtd_to_nfc(mtd);
940940
941941 err = clk_prepare_enable(nfc->clk);
942942 if (err)