linux/drivers/mtd/nand/raw/plat_nand.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Generic NAND driver
   4 *
   5 * Author: Vitaly Wool <vitalywool@gmail.com>
   6 */
   7
   8#include <linux/err.h>
   9#include <linux/io.h>
  10#include <linux/module.h>
  11#include <linux/platform_device.h>
  12#include <linux/slab.h>
  13#include <linux/mtd/mtd.h>
  14#include <linux/mtd/platnand.h>
  15
  16struct plat_nand_data {
  17        struct nand_controller  controller;
  18        struct nand_chip        chip;
  19        void __iomem            *io_base;
  20};
  21
  22static int plat_nand_attach_chip(struct nand_chip *chip)
  23{
  24        chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
  25
  26        if (chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
  27                chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
  28
  29        return 0;
  30}
  31
  32static const struct nand_controller_ops plat_nand_ops = {
  33        .attach_chip = plat_nand_attach_chip,
  34};
  35
  36/*
  37 * Probe for the NAND device.
  38 */
  39static int plat_nand_probe(struct platform_device *pdev)
  40{
  41        struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
  42        struct plat_nand_data *data;
  43        struct mtd_info *mtd;
  44        struct resource *res;
  45        const char **part_types;
  46        int err = 0;
  47
  48        if (!pdata) {
  49                dev_err(&pdev->dev, "platform_nand_data is missing\n");
  50                return -EINVAL;
  51        }
  52
  53        if (pdata->chip.nr_chips < 1) {
  54                dev_err(&pdev->dev, "invalid number of chips specified\n");
  55                return -EINVAL;
  56        }
  57
  58        /* Allocate memory for the device structure (and zero it) */
  59        data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
  60                            GFP_KERNEL);
  61        if (!data)
  62                return -ENOMEM;
  63
  64        data->controller.ops = &plat_nand_ops;
  65        nand_controller_init(&data->controller);
  66        data->chip.controller = &data->controller;
  67
  68        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  69        data->io_base = devm_ioremap_resource(&pdev->dev, res);
  70        if (IS_ERR(data->io_base))
  71                return PTR_ERR(data->io_base);
  72
  73        nand_set_flash_node(&data->chip, pdev->dev.of_node);
  74        mtd = nand_to_mtd(&data->chip);
  75        mtd->dev.parent = &pdev->dev;
  76
  77        data->chip.legacy.IO_ADDR_R = data->io_base;
  78        data->chip.legacy.IO_ADDR_W = data->io_base;
  79        data->chip.legacy.cmd_ctrl = pdata->ctrl.cmd_ctrl;
  80        data->chip.legacy.dev_ready = pdata->ctrl.dev_ready;
  81        data->chip.legacy.select_chip = pdata->ctrl.select_chip;
  82        data->chip.legacy.write_buf = pdata->ctrl.write_buf;
  83        data->chip.legacy.read_buf = pdata->ctrl.read_buf;
  84        data->chip.legacy.chip_delay = pdata->chip.chip_delay;
  85        data->chip.options |= pdata->chip.options;
  86        data->chip.bbt_options |= pdata->chip.bbt_options;
  87
  88        platform_set_drvdata(pdev, data);
  89
  90        /* Handle any platform specific setup */
  91        if (pdata->ctrl.probe) {
  92                err = pdata->ctrl.probe(pdev);
  93                if (err)
  94                        goto out;
  95        }
  96
  97        /* Scan to find existence of the device */
  98        err = nand_scan(&data->chip, pdata->chip.nr_chips);
  99        if (err)
 100                goto out;
 101
 102        part_types = pdata->chip.part_probe_types;
 103
 104        err = mtd_device_parse_register(mtd, part_types, NULL,
 105                                        pdata->chip.partitions,
 106                                        pdata->chip.nr_partitions);
 107
 108        if (!err)
 109                return err;
 110
 111        nand_cleanup(&data->chip);
 112out:
 113        if (pdata->ctrl.remove)
 114                pdata->ctrl.remove(pdev);
 115        return err;
 116}
 117
 118/*
 119 * Remove a NAND device.
 120 */
 121static int plat_nand_remove(struct platform_device *pdev)
 122{
 123        struct plat_nand_data *data = platform_get_drvdata(pdev);
 124        struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
 125        struct nand_chip *chip = &data->chip;
 126        int ret;
 127
 128        ret = mtd_device_unregister(nand_to_mtd(chip));
 129        WARN_ON(ret);
 130        nand_cleanup(chip);
 131        if (pdata->ctrl.remove)
 132                pdata->ctrl.remove(pdev);
 133
 134        return 0;
 135}
 136
 137static const struct of_device_id plat_nand_match[] = {
 138        { .compatible = "gen_nand" },
 139        {},
 140};
 141MODULE_DEVICE_TABLE(of, plat_nand_match);
 142
 143static struct platform_driver plat_nand_driver = {
 144        .probe  = plat_nand_probe,
 145        .remove = plat_nand_remove,
 146        .driver = {
 147                .name           = "gen_nand",
 148                .of_match_table = plat_nand_match,
 149        },
 150};
 151
 152module_platform_driver(plat_nand_driver);
 153
 154MODULE_LICENSE("GPL");
 155MODULE_AUTHOR("Vitaly Wool");
 156MODULE_DESCRIPTION("Simple generic NAND driver");
 157MODULE_ALIAS("platform:gen_nand");
 158