forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/mtd/nand/raw/r852.c
....@@ -1,10 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright © 2009 - Maxim Levitsky
34 * driver for Ricoh xD readers
4
- *
5
- * This program is free software; you can redistribute it and/or modify
6
- * it under the terms of the GNU General Public License version 2 as
7
- * published by the Free Software Foundation.
85 */
96
107 #define DRV_NAME "r852"
....@@ -45,7 +42,6 @@
4542 int address, uint8_t value)
4643 {
4744 writeb(value, dev->mmio + address);
48
- mmiowb();
4945 }
5046
5147
....@@ -61,7 +57,6 @@
6157 int address, uint32_t value)
6258 {
6359 writel(cpu_to_le32(value), dev->mmio + address);
64
- mmiowb();
6560 }
6661
6762 /* returns pointer to our private structure */
....@@ -151,8 +146,9 @@
151146 dev->dma_stage = 0;
152147
153148 if (dev->phys_dma_addr && dev->phys_dma_addr != dev->phys_bounce_buffer)
154
- pci_unmap_single(dev->pci_dev, dev->phys_dma_addr, R852_DMA_LEN,
155
- dev->dma_dir ? PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
149
+ dma_unmap_single(&dev->pci_dev->dev, dev->phys_dma_addr,
150
+ R852_DMA_LEN,
151
+ dev->dma_dir ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
156152 }
157153
158154 /*
....@@ -197,11 +193,10 @@
197193 bounce = 1;
198194
199195 if (!bounce) {
200
- dev->phys_dma_addr = pci_map_single(dev->pci_dev, (void *)buf,
196
+ dev->phys_dma_addr = dma_map_single(&dev->pci_dev->dev, buf,
201197 R852_DMA_LEN,
202
- (do_read ? PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE));
203
-
204
- if (pci_dma_mapping_error(dev->pci_dev, dev->phys_dma_addr))
198
+ do_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
199
+ if (dma_mapping_error(&dev->pci_dev->dev, dev->phys_dma_addr))
205200 bounce = 1;
206201 }
207202
....@@ -232,9 +227,9 @@
232227 /*
233228 * Program data lines of the nand chip to send data to it
234229 */
235
-static void r852_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
230
+static void r852_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
236231 {
237
- struct r852_device *dev = r852_get_dev(mtd);
232
+ struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
238233 uint32_t reg;
239234
240235 /* Don't allow any access to hardware if we suspect card removal */
....@@ -266,9 +261,9 @@
266261 /*
267262 * Read data lines of the nand chip to retrieve data
268263 */
269
-static void r852_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
264
+static void r852_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
270265 {
271
- struct r852_device *dev = r852_get_dev(mtd);
266
+ struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
272267 uint32_t reg;
273268
274269 if (dev->card_unstable) {
....@@ -303,9 +298,9 @@
303298 /*
304299 * Read one byte from nand chip
305300 */
306
-static uint8_t r852_read_byte(struct mtd_info *mtd)
301
+static uint8_t r852_read_byte(struct nand_chip *chip)
307302 {
308
- struct r852_device *dev = r852_get_dev(mtd);
303
+ struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
309304
310305 /* Same problem as in r852_read_buf.... */
311306 if (dev->card_unstable)
....@@ -317,9 +312,9 @@
317312 /*
318313 * Control several chip lines & send commands
319314 */
320
-static void r852_cmdctl(struct mtd_info *mtd, int dat, unsigned int ctrl)
315
+static void r852_cmdctl(struct nand_chip *chip, int dat, unsigned int ctrl)
321316 {
322
- struct r852_device *dev = r852_get_dev(mtd);
317
+ struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
323318
324319 if (dev->card_unstable)
325320 return;
....@@ -362,18 +357,17 @@
362357 * Wait till card is ready.
363358 * based on nand_wait, but returns errors on DMA error
364359 */
365
-static int r852_wait(struct mtd_info *mtd, struct nand_chip *chip)
360
+static int r852_wait(struct nand_chip *chip)
366361 {
367362 struct r852_device *dev = nand_get_controller_data(chip);
368363
369364 unsigned long timeout;
370365 u8 status;
371366
372
- timeout = jiffies + (chip->state == FL_ERASING ?
373
- msecs_to_jiffies(400) : msecs_to_jiffies(20));
367
+ timeout = jiffies + msecs_to_jiffies(400);
374368
375369 while (time_before(jiffies, timeout))
376
- if (chip->dev_ready(mtd))
370
+ if (chip->legacy.dev_ready(chip))
377371 break;
378372
379373 nand_status_op(chip, &status);
....@@ -390,9 +384,9 @@
390384 * Check if card is ready
391385 */
392386
393
-static int r852_ready(struct mtd_info *mtd)
387
+static int r852_ready(struct nand_chip *chip)
394388 {
395
- struct r852_device *dev = r852_get_dev(mtd);
389
+ struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
396390 return !(r852_read_reg(dev, R852_CARD_STA) & R852_CARD_STA_BUSY);
397391 }
398392
....@@ -401,9 +395,9 @@
401395 * Set ECC engine mode
402396 */
403397
404
-static void r852_ecc_hwctl(struct mtd_info *mtd, int mode)
398
+static void r852_ecc_hwctl(struct nand_chip *chip, int mode)
405399 {
406
- struct r852_device *dev = r852_get_dev(mtd);
400
+ struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
407401
408402 if (dev->card_unstable)
409403 return;
....@@ -433,10 +427,10 @@
433427 * Calculate ECC, only used for writes
434428 */
435429
436
-static int r852_ecc_calculate(struct mtd_info *mtd, const uint8_t *dat,
437
- uint8_t *ecc_code)
430
+static int r852_ecc_calculate(struct nand_chip *chip, const uint8_t *dat,
431
+ uint8_t *ecc_code)
438432 {
439
- struct r852_device *dev = r852_get_dev(mtd);
433
+ struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
440434 struct sm_oob *oob = (struct sm_oob *)ecc_code;
441435 uint32_t ecc1, ecc2;
442436
....@@ -465,14 +459,14 @@
465459 * Correct the data using ECC, hw did almost everything for us
466460 */
467461
468
-static int r852_ecc_correct(struct mtd_info *mtd, uint8_t *dat,
469
- uint8_t *read_ecc, uint8_t *calc_ecc)
462
+static int r852_ecc_correct(struct nand_chip *chip, uint8_t *dat,
463
+ uint8_t *read_ecc, uint8_t *calc_ecc)
470464 {
471465 uint32_t ecc_reg;
472466 uint8_t ecc_status, err_byte;
473467 int i, error = 0;
474468
475
- struct r852_device *dev = r852_get_dev(mtd);
469
+ struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
476470
477471 if (dev->card_unstable)
478472 return 0;
....@@ -521,9 +515,10 @@
521515 * This is copy of nand_read_oob_std
522516 * nand_read_oob_syndrome assumes we can send column address - we can't
523517 */
524
-static int r852_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
525
- int page)
518
+static int r852_read_oob(struct nand_chip *chip, int page)
526519 {
520
+ struct mtd_info *mtd = nand_to_mtd(chip);
521
+
527522 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
528523 }
529524
....@@ -636,7 +631,7 @@
636631 {
637632 struct mtd_info *mtd = nand_to_mtd(dev->chip);
638633
639
- WARN_ON(dev->card_registred);
634
+ WARN_ON(dev->card_registered);
640635
641636 mtd->dev.parent = &dev->pci_dev->dev;
642637
....@@ -653,10 +648,11 @@
653648 goto error3;
654649 }
655650
656
- dev->card_registred = 1;
651
+ dev->card_registered = 1;
657652 return 0;
658653 error3:
659
- nand_release(dev->chip);
654
+ WARN_ON(mtd_device_unregister(nand_to_mtd(dev->chip)));
655
+ nand_cleanup(dev->chip);
660656 error1:
661657 /* Force card redetect */
662658 dev->card_detected = 0;
....@@ -671,13 +667,14 @@
671667 {
672668 struct mtd_info *mtd = nand_to_mtd(dev->chip);
673669
674
- if (!dev->card_registred)
670
+ if (!dev->card_registered)
675671 return;
676672
677673 device_remove_file(&mtd->dev, &dev_attr_media_type);
678
- nand_release(dev->chip);
674
+ WARN_ON(mtd_device_unregister(mtd));
675
+ nand_cleanup(dev->chip);
679676 r852_engine_disable(dev);
680
- dev->card_registred = 0;
677
+ dev->card_registered = 0;
681678 }
682679
683680 /* Card state updater */
....@@ -691,7 +688,7 @@
691688 dev->card_unstable = 0;
692689
693690 /* False alarm */
694
- if (dev->card_detected == dev->card_registred)
691
+ if (dev->card_detected == dev->card_registered)
695692 goto exit;
696693
697694 /* Read media properties */
....@@ -820,6 +817,29 @@
820817 return ret;
821818 }
822819
820
+static int r852_attach_chip(struct nand_chip *chip)
821
+{
822
+ if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
823
+ return 0;
824
+
825
+ chip->ecc.placement = NAND_ECC_PLACEMENT_INTERLEAVED;
826
+ chip->ecc.size = R852_DMA_LEN;
827
+ chip->ecc.bytes = SM_OOB_SIZE;
828
+ chip->ecc.strength = 2;
829
+ chip->ecc.hwctl = r852_ecc_hwctl;
830
+ chip->ecc.calculate = r852_ecc_calculate;
831
+ chip->ecc.correct = r852_ecc_correct;
832
+
833
+ /* TODO: hack */
834
+ chip->ecc.read_oob = r852_read_oob;
835
+
836
+ return 0;
837
+}
838
+
839
+static const struct nand_controller_ops r852_ops = {
840
+ .attach_chip = r852_attach_chip,
841
+};
842
+
823843 static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
824844 {
825845 int error;
....@@ -834,7 +854,7 @@
834854
835855 pci_set_master(pci_dev);
836856
837
- error = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
857
+ error = dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32));
838858 if (error)
839859 goto error2;
840860
....@@ -852,26 +872,14 @@
852872 goto error4;
853873
854874 /* commands */
855
- chip->cmd_ctrl = r852_cmdctl;
856
- chip->waitfunc = r852_wait;
857
- chip->dev_ready = r852_ready;
875
+ chip->legacy.cmd_ctrl = r852_cmdctl;
876
+ chip->legacy.waitfunc = r852_wait;
877
+ chip->legacy.dev_ready = r852_ready;
858878
859879 /* I/O */
860
- chip->read_byte = r852_read_byte;
861
- chip->read_buf = r852_read_buf;
862
- chip->write_buf = r852_write_buf;
863
-
864
- /* ecc */
865
- chip->ecc.mode = NAND_ECC_HW_SYNDROME;
866
- chip->ecc.size = R852_DMA_LEN;
867
- chip->ecc.bytes = SM_OOB_SIZE;
868
- chip->ecc.strength = 2;
869
- chip->ecc.hwctl = r852_ecc_hwctl;
870
- chip->ecc.calculate = r852_ecc_calculate;
871
- chip->ecc.correct = r852_ecc_correct;
872
-
873
- /* TODO: hack */
874
- chip->ecc.read_oob = r852_read_oob;
880
+ chip->legacy.read_byte = r852_read_byte;
881
+ chip->legacy.read_buf = r852_read_buf;
882
+ chip->legacy.write_buf = r852_write_buf;
875883
876884 /* init our device structure */
877885 dev = kzalloc(sizeof(struct r852_device), GFP_KERNEL);
....@@ -884,8 +892,12 @@
884892 dev->pci_dev = pci_dev;
885893 pci_set_drvdata(pci_dev, dev);
886894
887
- dev->bounce_buffer = pci_alloc_consistent(pci_dev, R852_DMA_LEN,
888
- &dev->phys_bounce_buffer);
895
+ nand_controller_init(&dev->controller);
896
+ dev->controller.ops = &r852_ops;
897
+ chip->controller = &dev->controller;
898
+
899
+ dev->bounce_buffer = dma_alloc_coherent(&pci_dev->dev, R852_DMA_LEN,
900
+ &dev->phys_bounce_buffer, GFP_KERNEL);
889901
890902 if (!dev->bounce_buffer)
891903 goto error6;
....@@ -945,8 +957,8 @@
945957 error8:
946958 pci_iounmap(pci_dev, dev->mmio);
947959 error7:
948
- pci_free_consistent(pci_dev, R852_DMA_LEN,
949
- dev->bounce_buffer, dev->phys_bounce_buffer);
960
+ dma_free_coherent(&pci_dev->dev, R852_DMA_LEN, dev->bounce_buffer,
961
+ dev->phys_bounce_buffer);
950962 error6:
951963 kfree(dev);
952964 error5:
....@@ -979,8 +991,8 @@
979991 /* Cleanup */
980992 kfree(dev->tmp_buffer);
981993 pci_iounmap(pci_dev, dev->mmio);
982
- pci_free_consistent(pci_dev, R852_DMA_LEN,
983
- dev->bounce_buffer, dev->phys_bounce_buffer);
994
+ dma_free_coherent(&pci_dev->dev, R852_DMA_LEN, dev->bounce_buffer,
995
+ dev->phys_bounce_buffer);
984996
985997 kfree(dev->chip);
986998 kfree(dev);
....@@ -1003,7 +1015,7 @@
10031015 #ifdef CONFIG_PM_SLEEP
10041016 static int r852_suspend(struct device *device)
10051017 {
1006
- struct r852_device *dev = pci_get_drvdata(to_pci_dev(device));
1018
+ struct r852_device *dev = dev_get_drvdata(device);
10071019
10081020 if (dev->ctlreg & R852_CTL_CARDENABLE)
10091021 return -EBUSY;
....@@ -1024,8 +1036,7 @@
10241036
10251037 static int r852_resume(struct device *device)
10261038 {
1027
- struct r852_device *dev = pci_get_drvdata(to_pci_dev(device));
1028
- struct mtd_info *mtd = nand_to_mtd(dev->chip);
1039
+ struct r852_device *dev = dev_get_drvdata(device);
10291040
10301041 r852_disable_irqs(dev);
10311042 r852_card_update_present(dev);
....@@ -1033,7 +1044,7 @@
10331044
10341045
10351046 /* If card status changed, just do the work */
1036
- if (dev->card_detected != dev->card_registred) {
1047
+ if (dev->card_detected != dev->card_registered) {
10371048 dbg("card was %s during low power state",
10381049 dev->card_detected ? "added" : "removed");
10391050
....@@ -1043,11 +1054,11 @@
10431054 }
10441055
10451056 /* Otherwise, initialize the card */
1046
- if (dev->card_registred) {
1057
+ if (dev->card_registered) {
10471058 r852_engine_enable(dev);
1048
- dev->chip->select_chip(mtd, 0);
1059
+ nand_select_target(dev->chip, 0);
10491060 nand_reset_op(dev->chip);
1050
- dev->chip->select_chip(mtd, -1);
1061
+ nand_deselect_target(dev->chip);
10511062 }
10521063
10531064 /* Program card detection IRQ */