plat_nand.c 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Generic NAND driver
 *
 * Author: Vitaly Wool <vitalywool@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

12
#include <linux/err.h>
13 14 15 16 17
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/mtd/mtd.h>
18
#include <linux/mtd/rawnand.h>
19 20 21 22 23 24 25
#include <linux/mtd/partitions.h>

struct plat_nand_data {
	struct nand_chip	chip;
	void __iomem		*io_base;
};

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static void plat_nand_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
{
	struct platform_nand_data *pdata = dev_get_platdata(mtd->dev.parent);

	pdata->ctrl.cmd_ctrl(mtd_to_nand(mtd), dat, ctrl);
}

static int plat_nand_dev_ready(struct mtd_info *mtd)
{
	struct platform_nand_data *pdata = dev_get_platdata(mtd->dev.parent);

	return pdata->ctrl.dev_ready(mtd_to_nand(mtd));
}

static void plat_nand_select_chip(struct mtd_info *mtd, int cs)
{
	struct platform_nand_data *pdata = dev_get_platdata(mtd->dev.parent);

	pdata->ctrl.select_chip(mtd_to_nand(mtd), cs);
}

static void plat_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
				int len)
{
	struct platform_nand_data *pdata = dev_get_platdata(mtd->dev.parent);

	pdata->ctrl.write_buf(mtd_to_nand(mtd), buf, len);
}

static void plat_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
{
	struct platform_nand_data *pdata = dev_get_platdata(mtd->dev.parent);

	pdata->ctrl.read_buf(mtd_to_nand(mtd), buf, len);
}

62 63 64
/*
 * Probe for the NAND device.
 */
B
Bill Pemberton 已提交
65
static int plat_nand_probe(struct platform_device *pdev)
66
{
J
Jingoo Han 已提交
67
	struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
68
	struct plat_nand_data *data;
69
	struct mtd_info *mtd;
70
	struct resource *res;
71
	const char **part_types;
72 73
	int err = 0;

74 75 76 77 78
	if (!pdata) {
		dev_err(&pdev->dev, "platform_nand_data is missing\n");
		return -EINVAL;
	}

79 80 81 82 83
	if (pdata->chip.nr_chips < 1) {
		dev_err(&pdev->dev, "invalid number of chips specified\n");
		return -EINVAL;
	}

84
	/* Allocate memory for the device structure (and zero it) */
85 86
	data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
			    GFP_KERNEL);
87
	if (!data)
88 89
		return -ENOMEM;

90
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
91 92 93
	data->io_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(data->io_base))
		return PTR_ERR(data->io_base);
94

95
	nand_set_flash_node(&data->chip, pdev->dev.of_node);
96 97
	mtd = nand_to_mtd(&data->chip);
	mtd->dev.parent = &pdev->dev;
98 99 100

	data->chip.IO_ADDR_R = data->io_base;
	data->chip.IO_ADDR_W = data->io_base;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

	if (pdata->ctrl.cmd_ctrl)
		data->chip.cmd_ctrl = plat_nand_cmd_ctrl;

	if (pdata->ctrl.dev_ready)
		data->chip.dev_ready = plat_nand_dev_ready;

	if (pdata->ctrl.select_chip)
		data->chip.select_chip = plat_nand_select_chip;

	if (pdata->ctrl.write_buf)
		data->chip.write_buf = plat_nand_write_buf;

	if (pdata->ctrl.read_buf)
		data->chip.read_buf = plat_nand_read_buf;

117 118
	data->chip.chip_delay = pdata->chip.chip_delay;
	data->chip.options |= pdata->chip.options;
119
	data->chip.bbt_options |= pdata->chip.bbt_options;
120 121

	data->chip.ecc.mode = NAND_ECC_SOFT;
122
	data->chip.ecc.algo = NAND_ECC_HAMMING;
123 124 125

	platform_set_drvdata(pdev, data);

126 127
	/* Handle any platform specific setup */
	if (pdata->ctrl.probe) {
128 129
		err = pdata->ctrl.probe(pdev);
		if (err)
130 131 132
			goto out;
	}

L
Lucas De Marchi 已提交
133
	/* Scan to find existence of the device */
134
	err = nand_scan(&data->chip, pdata->chip.nr_chips);
135
	if (err)
136 137
		goto out;

138
	part_types = pdata->chip.part_probe_types;
139

140
	err = mtd_device_parse_register(mtd, part_types, NULL,
141 142
					pdata->chip.partitions,
					pdata->chip.nr_partitions);
143

144 145
	if (!err)
		return err;
146

147
	nand_release(mtd);
148
out:
149 150
	if (pdata->ctrl.remove)
		pdata->ctrl.remove(pdev);
151
	return err;
152 153 154 155 156
}

/*
 * Remove a NAND device.
 */
B
Bill Pemberton 已提交
157
static int plat_nand_remove(struct platform_device *pdev)
158 159
{
	struct plat_nand_data *data = platform_get_drvdata(pdev);
J
Jingoo Han 已提交
160
	struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
161

162
	nand_release(nand_to_mtd(&data->chip));
163 164
	if (pdata->ctrl.remove)
		pdata->ctrl.remove(pdev);
165 166 167 168

	return 0;
}

169 170 171 172 173 174
static const struct of_device_id plat_nand_match[] = {
	{ .compatible = "gen_nand" },
	{},
};
MODULE_DEVICE_TABLE(of, plat_nand_match);

175
static struct platform_driver plat_nand_driver = {
176
	.probe	= plat_nand_probe,
B
Bill Pemberton 已提交
177
	.remove	= plat_nand_remove,
178 179 180
	.driver	= {
		.name		= "gen_nand",
		.of_match_table = plat_nand_match,
181 182 183
	},
};

184
module_platform_driver(plat_nand_driver);
185 186 187 188

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Vitaly Wool");
MODULE_DESCRIPTION("Simple generic NAND driver");
189
MODULE_ALIAS("platform:gen_nand");