Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Generic NAND driver
0004  *
0005  * Author: Vitaly Wool <vitalywool@gmail.com>
0006  */
0007 
0008 #include <linux/err.h>
0009 #include <linux/io.h>
0010 #include <linux/module.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/slab.h>
0013 #include <linux/mtd/mtd.h>
0014 #include <linux/mtd/platnand.h>
0015 
0016 struct plat_nand_data {
0017     struct nand_controller  controller;
0018     struct nand_chip    chip;
0019     void __iomem        *io_base;
0020 };
0021 
0022 static int plat_nand_attach_chip(struct nand_chip *chip)
0023 {
0024     if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
0025         chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
0026         chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
0027 
0028     return 0;
0029 }
0030 
0031 static const struct nand_controller_ops plat_nand_ops = {
0032     .attach_chip = plat_nand_attach_chip,
0033 };
0034 
0035 /*
0036  * Probe for the NAND device.
0037  */
0038 static int plat_nand_probe(struct platform_device *pdev)
0039 {
0040     struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
0041     struct plat_nand_data *data;
0042     struct mtd_info *mtd;
0043     const char **part_types;
0044     int err = 0;
0045 
0046     if (!pdata) {
0047         dev_err(&pdev->dev, "platform_nand_data is missing\n");
0048         return -EINVAL;
0049     }
0050 
0051     if (pdata->chip.nr_chips < 1) {
0052         dev_err(&pdev->dev, "invalid number of chips specified\n");
0053         return -EINVAL;
0054     }
0055 
0056     /* Allocate memory for the device structure (and zero it) */
0057     data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
0058                 GFP_KERNEL);
0059     if (!data)
0060         return -ENOMEM;
0061 
0062     data->controller.ops = &plat_nand_ops;
0063     nand_controller_init(&data->controller);
0064     data->chip.controller = &data->controller;
0065 
0066     data->io_base = devm_platform_ioremap_resource(pdev, 0);
0067     if (IS_ERR(data->io_base))
0068         return PTR_ERR(data->io_base);
0069 
0070     nand_set_flash_node(&data->chip, pdev->dev.of_node);
0071     mtd = nand_to_mtd(&data->chip);
0072     mtd->dev.parent = &pdev->dev;
0073 
0074     data->chip.legacy.IO_ADDR_R = data->io_base;
0075     data->chip.legacy.IO_ADDR_W = data->io_base;
0076     data->chip.legacy.cmd_ctrl = pdata->ctrl.cmd_ctrl;
0077     data->chip.legacy.dev_ready = pdata->ctrl.dev_ready;
0078     data->chip.legacy.select_chip = pdata->ctrl.select_chip;
0079     data->chip.legacy.write_buf = pdata->ctrl.write_buf;
0080     data->chip.legacy.read_buf = pdata->ctrl.read_buf;
0081     data->chip.legacy.chip_delay = pdata->chip.chip_delay;
0082     data->chip.options |= pdata->chip.options;
0083     data->chip.bbt_options |= pdata->chip.bbt_options;
0084 
0085     platform_set_drvdata(pdev, data);
0086 
0087     /* Handle any platform specific setup */
0088     if (pdata->ctrl.probe) {
0089         err = pdata->ctrl.probe(pdev);
0090         if (err)
0091             goto out;
0092     }
0093 
0094     /*
0095      * This driver assumes that the default ECC engine should be TYPE_SOFT.
0096      * Set ->engine_type before registering the NAND devices in order to
0097      * provide a driver specific default value.
0098      */
0099     data->chip.ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
0100 
0101     /* Scan to find existence of the device */
0102     err = nand_scan(&data->chip, pdata->chip.nr_chips);
0103     if (err)
0104         goto out;
0105 
0106     part_types = pdata->chip.part_probe_types;
0107 
0108     err = mtd_device_parse_register(mtd, part_types, NULL,
0109                     pdata->chip.partitions,
0110                     pdata->chip.nr_partitions);
0111 
0112     if (!err)
0113         return err;
0114 
0115     nand_cleanup(&data->chip);
0116 out:
0117     if (pdata->ctrl.remove)
0118         pdata->ctrl.remove(pdev);
0119     return err;
0120 }
0121 
0122 /*
0123  * Remove a NAND device.
0124  */
0125 static int plat_nand_remove(struct platform_device *pdev)
0126 {
0127     struct plat_nand_data *data = platform_get_drvdata(pdev);
0128     struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
0129     struct nand_chip *chip = &data->chip;
0130     int ret;
0131 
0132     ret = mtd_device_unregister(nand_to_mtd(chip));
0133     WARN_ON(ret);
0134     nand_cleanup(chip);
0135     if (pdata->ctrl.remove)
0136         pdata->ctrl.remove(pdev);
0137 
0138     return 0;
0139 }
0140 
0141 static const struct of_device_id plat_nand_match[] = {
0142     { .compatible = "gen_nand" },
0143     {},
0144 };
0145 MODULE_DEVICE_TABLE(of, plat_nand_match);
0146 
0147 static struct platform_driver plat_nand_driver = {
0148     .probe  = plat_nand_probe,
0149     .remove = plat_nand_remove,
0150     .driver = {
0151         .name       = "gen_nand",
0152         .of_match_table = plat_nand_match,
0153     },
0154 };
0155 
0156 module_platform_driver(plat_nand_driver);
0157 
0158 MODULE_LICENSE("GPL");
0159 MODULE_AUTHOR("Vitaly Wool");
0160 MODULE_DESCRIPTION("Simple generic NAND driver");
0161 MODULE_ALIAS("platform:gen_nand");