ndfc.c 6.9 KB
Newer Older
1 2 3 4
/*
 *  drivers/mtd/ndfc.c
 *
 *  Overview:
S
Sean MacLennan 已提交
5
 *   Platform independent driver for NDFC (NanD Flash Controller)
6 7
 *   integrated into EP440 cores
 *
S
Sean MacLennan 已提交
8 9 10 11 12 13
 *   Ported to an OF platform driver by Sean MacLennan
 *
 *   The NDFC supports multiple chips, but this driver only supports a
 *   single chip since I do not have access to any boards with
 *   multiple chips.
 *
14 15 16
 *  Author: Thomas Gleixner
 *
 *  Copyright 2006 IBM
S
Sean MacLennan 已提交
17 18
 *  Copyright 2008 PIKA Technologies
 *    Sean MacLennan <smaclennan@pikatech.com>
19 20 21 22 23 24 25 26 27 28 29 30
 *
 *  This program is free software; you can redistribute	 it and/or modify it
 *  under  the terms of	 the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the	License, or (at your
 *  option) any later version.
 *
 */
#include <linux/module.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/nand_ecc.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/ndfc.h>
31
#include <linux/slab.h>
32
#include <linux/mtd/mtd.h>
33
#include <linux/of_address.h>
S
Sean MacLennan 已提交
34
#include <linux/of_platform.h>
35 36
#include <asm/io.h>

37
#define NDFC_MAX_CS    4
38 39

struct ndfc_controller {
40
	struct platform_device *ofdev;
S
Sean MacLennan 已提交
41 42 43 44 45
	void __iomem *ndfcbase;
	struct mtd_info mtd;
	struct nand_chip chip;
	int chip_select;
	struct nand_hw_control ndfc_control;
46 47
};

48
static struct ndfc_controller ndfc_ctrl[NDFC_MAX_CS];
49 50 51 52

static void ndfc_select_chip(struct mtd_info *mtd, int chip)
{
	uint32_t ccr;
53 54
	struct nand_chip *nchip = mtd->priv;
	struct ndfc_controller *ndfc = nchip->priv;
55

S
Sean MacLennan 已提交
56
	ccr = in_be32(ndfc->ndfcbase + NDFC_CCR);
57 58
	if (chip >= 0) {
		ccr &= ~NDFC_CCR_BS_MASK;
S
Sean MacLennan 已提交
59
		ccr |= NDFC_CCR_BS(chip + ndfc->chip_select);
60 61
	} else
		ccr |= NDFC_CCR_RESET_CE;
S
Sean MacLennan 已提交
62
	out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
63 64
}

65
static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
66
{
67 68
	struct nand_chip *chip = mtd->priv;
	struct ndfc_controller *ndfc = chip->priv;
69

70 71 72 73
	if (cmd == NAND_CMD_NONE)
		return;

	if (ctrl & NAND_CLE)
74
		writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_CMD);
75
	else
76
		writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_ALE);
77 78 79 80
}

static int ndfc_ready(struct mtd_info *mtd)
{
81 82
	struct nand_chip *chip = mtd->priv;
	struct ndfc_controller *ndfc = chip->priv;
83

S
Sean MacLennan 已提交
84
	return in_be32(ndfc->ndfcbase + NDFC_STAT) & NDFC_STAT_IS_READY;
85 86 87 88 89
}

static void ndfc_enable_hwecc(struct mtd_info *mtd, int mode)
{
	uint32_t ccr;
90 91
	struct nand_chip *chip = mtd->priv;
	struct ndfc_controller *ndfc = chip->priv;
92

S
Sean MacLennan 已提交
93
	ccr = in_be32(ndfc->ndfcbase + NDFC_CCR);
94
	ccr |= NDFC_CCR_RESET_ECC;
S
Sean MacLennan 已提交
95
	out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
96 97 98 99 100 101
	wmb();
}

static int ndfc_calculate_ecc(struct mtd_info *mtd,
			      const u_char *dat, u_char *ecc_code)
{
102 103
	struct nand_chip *chip = mtd->priv;
	struct ndfc_controller *ndfc = chip->priv;
104 105 106 107
	uint32_t ecc;
	uint8_t *p = (uint8_t *)&ecc;

	wmb();
S
Sean MacLennan 已提交
108 109
	ecc = in_be32(ndfc->ndfcbase + NDFC_ECC);
	/* The NDFC uses Smart Media (SMC) bytes order */
110 111
	ecc_code[0] = p[1];
	ecc_code[1] = p[2];
112 113 114 115 116 117 118 119 120 121 122 123 124 125
	ecc_code[2] = p[3];

	return 0;
}

/*
 * Speedups for buffer read/write/verify
 *
 * NDFC allows 32bit read/write of data. So we can speed up the buffer
 * functions. No further checking, as nand_base will always read/write
 * page aligned.
 */
static void ndfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
{
126 127
	struct nand_chip *chip = mtd->priv;
	struct ndfc_controller *ndfc = chip->priv;
128 129 130
	uint32_t *p = (uint32_t *) buf;

	for(;len > 0; len -= 4)
S
Sean MacLennan 已提交
131
		*p++ = in_be32(ndfc->ndfcbase + NDFC_DATA);
132 133 134 135
}

static void ndfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
{
136 137
	struct nand_chip *chip = mtd->priv;
	struct ndfc_controller *ndfc = chip->priv;
138 139 140
	uint32_t *p = (uint32_t *) buf;

	for(;len > 0; len -= 4)
S
Sean MacLennan 已提交
141
		out_be32(ndfc->ndfcbase + NDFC_DATA, *p++);
142 143 144 145 146
}

/*
 * Initialize chip structure
 */
S
Sean MacLennan 已提交
147 148
static int ndfc_chip_init(struct ndfc_controller *ndfc,
			  struct device_node *node)
149
{
S
Sean MacLennan 已提交
150 151
	struct device_node *flash_np;
	struct nand_chip *chip = &ndfc->chip;
152
	struct mtd_part_parser_data ppdata;
S
Sean MacLennan 已提交
153
	int ret;
154 155 156

	chip->IO_ADDR_R = ndfc->ndfcbase + NDFC_DATA;
	chip->IO_ADDR_W = ndfc->ndfcbase + NDFC_DATA;
157
	chip->cmd_ctrl = ndfc_hwcontrol;
158 159 160 161 162 163
	chip->dev_ready = ndfc_ready;
	chip->select_chip = ndfc_select_chip;
	chip->chip_delay = 50;
	chip->controller = &ndfc->ndfc_control;
	chip->read_buf = ndfc_read_buf;
	chip->write_buf = ndfc_write_buf;
T
Thomas Gleixner 已提交
164 165 166 167 168 169
	chip->ecc.correct = nand_correct_data;
	chip->ecc.hwctl = ndfc_enable_hwecc;
	chip->ecc.calculate = ndfc_calculate_ecc;
	chip->ecc.mode = NAND_ECC_HW;
	chip->ecc.size = 256;
	chip->ecc.bytes = 3;
M
Mike Dunn 已提交
170
	chip->ecc.strength = 1;
171
	chip->priv = ndfc;
172

S
Sean MacLennan 已提交
173 174
	ndfc->mtd.priv = chip;
	ndfc->mtd.owner = THIS_MODULE;
175

S
Sean MacLennan 已提交
176 177
	flash_np = of_get_next_child(node, NULL);
	if (!flash_np)
178
		return -ENODEV;
S
Sean MacLennan 已提交
179

180
	ppdata.of_node = flash_np;
S
Sean MacLennan 已提交
181
	ndfc->mtd.name = kasprintf(GFP_KERNEL, "%s.%s",
182
			dev_name(&ndfc->ofdev->dev), flash_np->name);
S
Sean MacLennan 已提交
183 184 185
	if (!ndfc->mtd.name) {
		ret = -ENOMEM;
		goto err;
186 187
	}

S
Sean MacLennan 已提交
188 189 190
	ret = nand_scan(&ndfc->mtd, 1);
	if (ret)
		goto err;
191

192
	ret = mtd_device_parse_register(&ndfc->mtd, NULL, &ppdata, NULL, 0);
193

S
Sean MacLennan 已提交
194 195 196 197 198
err:
	of_node_put(flash_np);
	if (ret)
		kfree(ndfc->mtd.name);
	return ret;
199 200
}

B
Bill Pemberton 已提交
201
static int ndfc_probe(struct platform_device *ofdev)
202
{
203
	struct ndfc_controller *ndfc;
204
	const __be32 *reg;
S
Sean MacLennan 已提交
205
	u32 ccr;
206 207
	u32 cs;
	int err, len;
S
Sean MacLennan 已提交
208 209

	/* Read the reg property to get the chip select */
210
	reg = of_get_property(ofdev->dev.of_node, "reg", &len);
S
Sean MacLennan 已提交
211 212 213 214
	if (reg == NULL || len != 12) {
		dev_err(&ofdev->dev, "unable read reg property (%d)\n", len);
		return -ENOENT;
	}
215 216 217 218 219 220 221 222 223 224 225 226 227 228

	cs = be32_to_cpu(reg[0]);
	if (cs >= NDFC_MAX_CS) {
		dev_err(&ofdev->dev, "invalid CS number (%d)\n", cs);
		return -EINVAL;
	}

	ndfc = &ndfc_ctrl[cs];
	ndfc->chip_select = cs;

	spin_lock_init(&ndfc->ndfc_control.lock);
	init_waitqueue_head(&ndfc->ndfc_control.wq);
	ndfc->ofdev = ofdev;
	dev_set_drvdata(&ofdev->dev, ndfc);
S
Sean MacLennan 已提交
229

230
	ndfc->ndfcbase = of_iomap(ofdev->dev.of_node, 0);
231
	if (!ndfc->ndfcbase) {
S
Sean MacLennan 已提交
232
		dev_err(&ofdev->dev, "failed to get memory\n");
233 234 235
		return -EIO;
	}

S
Sean MacLennan 已提交
236
	ccr = NDFC_CCR_BS(ndfc->chip_select);
237

S
Sean MacLennan 已提交
238
	/* It is ok if ccr does not exist - just default to 0 */
239
	reg = of_get_property(ofdev->dev.of_node, "ccr", NULL);
S
Sean MacLennan 已提交
240
	if (reg)
241
		ccr |= be32_to_cpup(reg);
242

S
Sean MacLennan 已提交
243
	out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
244

S
Sean MacLennan 已提交
245
	/* Set the bank settings if given */
246
	reg = of_get_property(ofdev->dev.of_node, "bank-settings", NULL);
S
Sean MacLennan 已提交
247 248
	if (reg) {
		int offset = NDFC_BCFG0 + (ndfc->chip_select << 2);
249
		out_be32(ndfc->ndfcbase + offset, be32_to_cpup(reg));
S
Sean MacLennan 已提交
250 251
	}

252
	err = ndfc_chip_init(ndfc, ofdev->dev.of_node);
S
Sean MacLennan 已提交
253 254 255 256
	if (err) {
		iounmap(ndfc->ndfcbase);
		return err;
	}
257 258 259 260

	return 0;
}

B
Bill Pemberton 已提交
261
static int ndfc_remove(struct platform_device *ofdev)
262
{
S
Sean MacLennan 已提交
263
	struct ndfc_controller *ndfc = dev_get_drvdata(&ofdev->dev);
264

S
Sean MacLennan 已提交
265
	nand_release(&ndfc->mtd);
266
	kfree(ndfc->mtd.name);
267 268 269 270

	return 0;
}

S
Sean MacLennan 已提交
271 272 273
static const struct of_device_id ndfc_match[] = {
	{ .compatible = "ibm,ndfc", },
	{}
274
};
S
Sean MacLennan 已提交
275
MODULE_DEVICE_TABLE(of, ndfc_match);
276

277
static struct platform_driver ndfc_driver = {
S
Sean MacLennan 已提交
278
	.driver = {
279 280
		.name = "ndfc",
		.of_match_table = ndfc_match,
281
	},
S
Sean MacLennan 已提交
282
	.probe = ndfc_probe,
B
Bill Pemberton 已提交
283
	.remove = ndfc_remove,
284 285
};

286
module_platform_driver(ndfc_driver);
287 288 289

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
S
Sean MacLennan 已提交
290
MODULE_DESCRIPTION("OF Platform driver for NDFC");