sharpsl.c 5.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * drivers/mtd/nand/sharpsl.c
 *
 *  Copyright (C) 2004 Richard Purdie
5
 *  Copyright (C) 2008 Dmitry Baryshkov
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  Based on Sharp's NAND driver sharp_sl.c
 *
 * 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.
 *
 */

#include <linux/genhd.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/nand_ecc.h>
#include <linux/mtd/partitions.h>
23
#include <linux/mtd/sharpsl.h>
L
Linus Torvalds 已提交
24
#include <linux/interrupt.h>
25 26
#include <linux/platform_device.h>

L
Linus Torvalds 已提交
27
#include <asm/io.h>
28
#include <mach/hardware.h>
L
Linus Torvalds 已提交
29 30
#include <asm/mach-types.h>

31 32 33
struct sharpsl_nand {
	struct mtd_info		mtd;
	struct nand_chip	chip;
34 35

	void __iomem		*io;
36 37
};

38
#define mtd_to_sharpsl(_mtd)	container_of(_mtd, struct sharpsl_nand, mtd)
L
Linus Torvalds 已提交
39 40

/* register offset */
41 42 43 44 45 46 47
#define ECCLPLB		0x00	/* line parity 7 - 0 bit */
#define ECCLPUB		0x04	/* line parity 15 - 8 bit */
#define ECCCP		0x08	/* column parity 5 - 0 bit */
#define ECCCNTR		0x0C	/* ECC byte counter */
#define ECCCLRR		0x10	/* cleare ECC */
#define FLASHIO		0x14	/* Flash I/O */
#define FLASHCTL	0x18	/* Flash Control */
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56

/* Flash control bit */
#define FLRYBY		(1 << 5)
#define FLCE1		(1 << 4)
#define FLWP		(1 << 3)
#define FLALE		(1 << 2)
#define FLCLE		(1 << 1)
#define FLCE0		(1 << 0)

57
/*
L
Linus Torvalds 已提交
58
 *	hardware specific access to control-lines
59
 *	ctrl:
60
 *	NAND_CNE: bit 0 -> ! bit 0 & 4
61 62 63
 *	NAND_CLE: bit 1 -> bit 1
 *	NAND_ALE: bit 2 -> bit 2
 *
L
Linus Torvalds 已提交
64
 */
65 66
static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
				   unsigned int ctrl)
L
Linus Torvalds 已提交
67
{
68
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
69 70 71 72 73 74
	struct nand_chip *chip = mtd->priv;

	if (ctrl & NAND_CTRL_CHANGE) {
		unsigned char bits = ctrl & 0x07;

		bits |= (ctrl & 0x01) << 4;
75 76 77

		bits ^= 0x11;

78
		writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
L
Linus Torvalds 已提交
79
	}
80 81 82

	if (cmd != NAND_CMD_NONE)
		writeb(cmd, chip->IO_ADDR_W);
L
Linus Torvalds 已提交
83 84
}

85
static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
L
Linus Torvalds 已提交
86
{
87 88
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
	return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
L
Linus Torvalds 已提交
89 90
}

91
static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
L
Linus Torvalds 已提交
92
{
93 94
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
	writeb(0, sharpsl->io + ECCCLRR);
L
Linus Torvalds 已提交
95 96
}

97
static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
L
Linus Torvalds 已提交
98
{
99 100 101 102 103
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
	ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
	ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
	ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
	return readb(sharpsl->io + ECCCNTR) != 0;
L
Linus Torvalds 已提交
104 105 106 107 108
}

/*
 * Main initialization routine
 */
B
Bill Pemberton 已提交
109
static int sharpsl_nand_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
110 111
{
	struct nand_chip *this;
112
	struct resource *r;
L
Linus Torvalds 已提交
113
	int err = 0;
114
	struct sharpsl_nand *sharpsl;
115 116 117 118 119 120
	struct sharpsl_nand_platform_data *data = pdev->dev.platform_data;

	if (!data) {
		dev_err(&pdev->dev, "no platform data!\n");
		return -EINVAL;
	}
L
Linus Torvalds 已提交
121 122

	/* Allocate memory for MTD device structure and private data */
123 124
	sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
	if (!sharpsl) {
125
		printk("Unable to allocate SharpSL NAND MTD device structure.\n");
L
Linus Torvalds 已提交
126 127
		return -ENOMEM;
	}
128

129 130 131 132 133 134 135
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!r) {
		dev_err(&pdev->dev, "no io memory resource defined!\n");
		err = -ENODEV;
		goto err_get_res;
	}

J
Joe Perches 已提交
136
	/* map physical address */
137 138
	sharpsl->io = ioremap(r->start, resource_size(r));
	if (!sharpsl->io) {
L
Linus Torvalds 已提交
139
		printk("ioremap to access Sharp SL NAND chip failed\n");
140 141
		err = -EIO;
		goto err_ioremap;
L
Linus Torvalds 已提交
142
	}
143

L
Linus Torvalds 已提交
144
	/* Get pointer to private data */
145
	this = (struct nand_chip *)(&sharpsl->chip);
L
Linus Torvalds 已提交
146 147

	/* Link the private data with the MTD structure */
148 149 150 151
	sharpsl->mtd.priv = this;
	sharpsl->mtd.owner = THIS_MODULE;

	platform_set_drvdata(pdev, sharpsl);
L
Linus Torvalds 已提交
152 153 154 155

	/*
	 * PXA initialize
	 */
156
	writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
L
Linus Torvalds 已提交
157 158

	/* Set address of NAND IO lines */
159 160
	this->IO_ADDR_R = sharpsl->io + FLASHIO;
	this->IO_ADDR_W = sharpsl->io + FLASHIO;
L
Linus Torvalds 已提交
161
	/* Set address of hardware control function */
162
	this->cmd_ctrl = sharpsl_nand_hwcontrol;
L
Linus Torvalds 已提交
163 164 165 166
	this->dev_ready = sharpsl_nand_dev_ready;
	/* 15 us command delay time */
	this->chip_delay = 15;
	/* set eccmode using hardware ECC */
T
Thomas Gleixner 已提交
167 168 169
	this->ecc.mode = NAND_ECC_HW;
	this->ecc.size = 256;
	this->ecc.bytes = 3;
M
Mike Dunn 已提交
170
	this->ecc.strength = 1;
171 172
	this->badblock_pattern = data->badblock_pattern;
	this->ecc.layout = data->ecc_layout;
T
Thomas Gleixner 已提交
173 174 175
	this->ecc.hwctl = sharpsl_nand_enable_hwecc;
	this->ecc.calculate = sharpsl_nand_calculate_ecc;
	this->ecc.correct = nand_correct_data;
L
Linus Torvalds 已提交
176 177

	/* Scan to find existence of the device */
178
	err = nand_scan(&sharpsl->mtd, 1);
179 180
	if (err)
		goto err_scan;
L
Linus Torvalds 已提交
181 182

	/* Register the partitions */
183
	sharpsl->mtd.name = "sharpsl-nand";
L
Linus Torvalds 已提交
184

185 186
	err = mtd_device_parse_register(&sharpsl->mtd, NULL, NULL,
					data->partitions, data->nr_partitions);
187 188
	if (err)
		goto err_add;
L
Linus Torvalds 已提交
189 190 191

	/* Return happy */
	return 0;
192

193 194 195
err_add:
	nand_release(&sharpsl->mtd);

196 197 198
err_scan:
	platform_set_drvdata(pdev, NULL);
	iounmap(sharpsl->io);
199
err_ioremap:
200
err_get_res:
201
	kfree(sharpsl);
202 203
	return err;
}
L
Linus Torvalds 已提交
204 205 206 207

/*
 * Clean up routine
 */
208
static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
209
{
210 211
	struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);

L
Linus Torvalds 已提交
212
	/* Release resources, unregister device */
213 214 215
	nand_release(&sharpsl->mtd);

	platform_set_drvdata(pdev, NULL);
L
Linus Torvalds 已提交
216

217
	iounmap(sharpsl->io);
L
Linus Torvalds 已提交
218 219

	/* Free the MTD device structure */
220
	kfree(sharpsl);
221 222 223 224 225 226 227 228 229 230

	return 0;
}

static struct platform_driver sharpsl_nand_driver = {
	.driver = {
		.name	= "sharpsl-nand",
		.owner	= THIS_MODULE,
	},
	.probe		= sharpsl_nand_probe,
B
Bill Pemberton 已提交
231
	.remove		= sharpsl_nand_remove,
232 233
};

234
module_platform_driver(sharpsl_nand_driver);
L
Linus Torvalds 已提交
235 236 237 238

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");