orion_nand.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * drivers/mtd/nand/orion_nand.c
 *
 * NAND support for Marvell Orion SoC platforms
 *
 * Tzachi Perelstein <tzachi@marvell.com>
 *
 * This file is licensed under  the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

#include <linux/slab.h>
#include <linux/module.h>
#include <linux/platform_device.h>
16
#include <linux/of.h>
17 18 19
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/partitions.h>
20 21
#include <linux/clk.h>
#include <linux/err.h>
22 23
#include <asm/io.h>
#include <asm/sizes.h>
24
#include <mach/hardware.h>
25
#include <plat/orion_nand.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

static void orion_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
{
	struct nand_chip *nc = mtd->priv;
	struct orion_nand_data *board = nc->priv;
	u32 offs;

	if (cmd == NAND_CMD_NONE)
		return;

	if (ctrl & NAND_CLE)
		offs = (1 << board->cle);
	else if (ctrl & NAND_ALE)
		offs = (1 << board->ale);
	else
		return;

	if (nc->options & NAND_BUSWIDTH_16)
		offs <<= 1;

	writeb(cmd, nc->IO_ADDR_W + offs);
}

49 50 51 52 53 54 55 56 57 58 59 60 61
static void orion_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
{
	struct nand_chip *chip = mtd->priv;
	void __iomem *io_base = chip->IO_ADDR_R;
	uint64_t *buf64;
	int i = 0;

	while (len && (unsigned long)buf & 7) {
		*buf++ = readb(io_base);
		len--;
	}
	buf64 = (uint64_t *)buf;
	while (i < len/8) {
62 63 64 65 66 67 68
		/*
		 * Since GCC has no proper constraint (PR 43518)
		 * force x variable to r2/r3 registers as ldrd instruction
		 * requires first register to be even.
		 */
		register uint64_t x asm ("r2");

69
		asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
70 71 72 73 74 75 76
		buf64[i++] = x;
	}
	i *= 8;
	while (i < len)
		buf[i++] = readb(io_base);
}

77 78 79
static int __init orion_nand_probe(struct platform_device *pdev)
{
	struct mtd_info *mtd;
80
	struct mtd_part_parser_data ppdata = {};
81 82
	struct nand_chip *nc;
	struct orion_nand_data *board;
83
	struct resource *res;
84
	struct clk *clk;
85 86
	void __iomem *io_base;
	int ret = 0;
87
	u32 val = 0;
88 89 90 91 92 93 94 95 96

	nc = kzalloc(sizeof(struct nand_chip) + sizeof(struct mtd_info), GFP_KERNEL);
	if (!nc) {
		printk(KERN_ERR "orion_nand: failed to allocate device structure.\n");
		ret = -ENOMEM;
		goto no_res;
	}
	mtd = (struct mtd_info *)(nc + 1);

97 98
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
99
		ret = -ENODEV;
100 101 102 103
		goto no_res;
	}

	io_base = ioremap(res->start, resource_size(res));
104 105 106 107 108 109
	if (!io_base) {
		printk(KERN_ERR "orion_nand: ioremap failed\n");
		ret = -EIO;
		goto no_res;
	}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	if (pdev->dev.of_node) {
		board = devm_kzalloc(&pdev->dev, sizeof(struct orion_nand_data),
					GFP_KERNEL);
		if (!board) {
			printk(KERN_ERR "orion_nand: failed to allocate board structure.\n");
			ret = -ENOMEM;
			goto no_res;
		}
		if (!of_property_read_u32(pdev->dev.of_node, "cle", &val))
			board->cle = (u8)val;
		else
			board->cle = 0;
		if (!of_property_read_u32(pdev->dev.of_node, "ale", &val))
			board->ale = (u8)val;
		else
			board->ale = 1;
		if (!of_property_read_u32(pdev->dev.of_node,
						"bank-width", &val))
			board->width = (u8)val * 8;
		else
			board->width = 8;
		if (!of_property_read_u32(pdev->dev.of_node,
						"chip-delay", &val))
			board->chip_delay = (u8)val;
	} else
		board = pdev->dev.platform_data;
136 137 138 139 140 141 142

	mtd->priv = nc;
	mtd->owner = THIS_MODULE;

	nc->priv = board;
	nc->IO_ADDR_R = nc->IO_ADDR_W = io_base;
	nc->cmd_ctrl = orion_nand_cmd_ctrl;
143
	nc->read_buf = orion_nand_read_buf;
144 145
	nc->ecc.mode = NAND_ECC_SOFT;

146 147 148
	if (board->chip_delay)
		nc->chip_delay = board->chip_delay;

149 150 151 152
	WARN(board->width > 16,
		"%d bit bus width out of range",
		board->width);

153 154 155
	if (board->width == 16)
		nc->options |= NAND_BUSWIDTH_16;

156 157 158
	if (board->dev_ready)
		nc->dev_ready = board->dev_ready;

159 160
	platform_set_drvdata(pdev, mtd);

161 162 163 164 165 166 167 168
	/* Not all platforms can gate the clock, so it is not
	   an error if the clock does not exists. */
	clk = clk_get(&pdev->dev, NULL);
	if (!IS_ERR(clk)) {
		clk_prepare_enable(clk);
		clk_put(clk);
	}

169 170 171 172 173 174
	if (nand_scan(mtd, 1)) {
		ret = -ENXIO;
		goto no_dev;
	}

	mtd->name = "orion_nand";
175 176 177
	ppdata.of_node = pdev->dev.of_node;
	ret = mtd_device_parse_register(mtd, NULL, &ppdata,
			board->parts, board->nr_parts);
178 179 180 181 182 183 184 185
	if (ret) {
		nand_release(mtd);
		goto no_dev;
	}

	return 0;

no_dev:
186 187 188 189
	if (!IS_ERR(clk)) {
		clk_disable_unprepare(clk);
		clk_put(clk);
	}
190 191 192 193 194 195 196 197 198 199 200 201
	platform_set_drvdata(pdev, NULL);
	iounmap(io_base);
no_res:
	kfree(nc);

	return ret;
}

static int __devexit orion_nand_remove(struct platform_device *pdev)
{
	struct mtd_info *mtd = platform_get_drvdata(pdev);
	struct nand_chip *nc = mtd->priv;
202
	struct clk *clk;
203 204 205 206 207 208 209

	nand_release(mtd);

	iounmap(nc->IO_ADDR_W);

	kfree(nc);

210 211 212 213 214 215
	clk = clk_get(&pdev->dev, NULL);
	if (!IS_ERR(clk)) {
		clk_disable_unprepare(clk);
		clk_put(clk);
	}

216 217 218
	return 0;
}

219 220
#ifdef CONFIG_OF
static struct of_device_id orion_nand_of_match_table[] = {
221
	{ .compatible = "marvell,orion-nand", },
222 223 224 225
	{},
};
#endif

226
static struct platform_driver orion_nand_driver = {
227
	.remove		= __devexit_p(orion_nand_remove),
228 229 230
	.driver		= {
		.name	= "orion_nand",
		.owner	= THIS_MODULE,
231
		.of_match_table = of_match_ptr(orion_nand_of_match_table),
232 233 234 235 236
	},
};

static int __init orion_nand_init(void)
{
237
	return platform_driver_probe(&orion_nand_driver, orion_nand_probe);
238 239 240 241 242 243 244 245 246 247 248 249 250
}

static void __exit orion_nand_exit(void)
{
	platform_driver_unregister(&orion_nand_driver);
}

module_init(orion_nand_init);
module_exit(orion_nand_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Tzachi Perelstein");
MODULE_DESCRIPTION("NAND glue for Orion platforms");
251
MODULE_ALIAS("platform:orion_nand");