sharpsl.c 5.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Copyright (C) 2004 Richard Purdie
3
 *  Copyright (C) 2008 Dmitry Baryshkov
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 *  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>
18
#include <linux/mtd/rawnand.h>
L
Linus Torvalds 已提交
19 20
#include <linux/mtd/nand_ecc.h>
#include <linux/mtd/partitions.h>
21
#include <linux/mtd/sharpsl.h>
L
Linus Torvalds 已提交
22
#include <linux/interrupt.h>
23
#include <linux/platform_device.h>
24
#include <linux/io.h>
L
Linus Torvalds 已提交
25

26 27
struct sharpsl_nand {
	struct nand_chip	chip;
28 29

	void __iomem		*io;
30 31
};

32 33 34 35
static inline struct sharpsl_nand *mtd_to_sharpsl(struct mtd_info *mtd)
{
	return container_of(mtd_to_nand(mtd), struct sharpsl_nand, chip);
}
L
Linus Torvalds 已提交
36 37

/* register offset */
38 39 40 41 42 43 44
#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 已提交
45 46 47 48 49 50 51 52 53

/* 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)

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

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

		bits |= (ctrl & 0x01) << 4;
71 72 73

		bits ^= 0x11;

74
		writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
L
Linus Torvalds 已提交
75
	}
76 77

	if (cmd != NAND_CMD_NONE)
78
		writeb(cmd, chip->legacy.IO_ADDR_W);
L
Linus Torvalds 已提交
79 80
}

81
static int sharpsl_nand_dev_ready(struct nand_chip *chip)
L
Linus Torvalds 已提交
82
{
83
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
84
	return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
L
Linus Torvalds 已提交
85 86
}

87
static void sharpsl_nand_enable_hwecc(struct nand_chip *chip, int mode)
L
Linus Torvalds 已提交
88
{
89
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
90
	writeb(0, sharpsl->io + ECCCLRR);
L
Linus Torvalds 已提交
91 92
}

93 94
static int sharpsl_nand_calculate_ecc(struct nand_chip *chip,
				      const u_char * dat, u_char * ecc_code)
L
Linus Torvalds 已提交
95
{
96
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
97 98 99 100
	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 已提交
101 102 103 104 105
}

/*
 * Main initialization routine
 */
B
Bill Pemberton 已提交
106
static int sharpsl_nand_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
107 108
{
	struct nand_chip *this;
109
	struct mtd_info *mtd;
110
	struct resource *r;
L
Linus Torvalds 已提交
111
	int err = 0;
112
	struct sharpsl_nand *sharpsl;
J
Jingoo Han 已提交
113
	struct sharpsl_nand_platform_data *data = dev_get_platdata(&pdev->dev);
114 115 116 117 118

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

	/* Allocate memory for MTD device structure and private data */
121
	sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
122
	if (!sharpsl)
L
Linus Torvalds 已提交
123
		return -ENOMEM;
124

125 126 127 128 129 130 131
	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 已提交
132
	/* map physical address */
133 134
	sharpsl->io = ioremap(r->start, resource_size(r));
	if (!sharpsl->io) {
135
		dev_err(&pdev->dev, "ioremap to access Sharp SL NAND chip failed\n");
136 137
		err = -EIO;
		goto err_ioremap;
L
Linus Torvalds 已提交
138
	}
139

L
Linus Torvalds 已提交
140
	/* Get pointer to private data */
141
	this = (struct nand_chip *)(&sharpsl->chip);
L
Linus Torvalds 已提交
142 143

	/* Link the private data with the MTD structure */
144 145
	mtd = nand_to_mtd(this);
	mtd->dev.parent = &pdev->dev;
146
	mtd_set_ooblayout(mtd, data->ecc_layout);
147 148

	platform_set_drvdata(pdev, sharpsl);
L
Linus Torvalds 已提交
149 150 151 152

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

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

	/* Scan to find existence of the device */
174
	err = nand_scan(this, 1);
175 176
	if (err)
		goto err_scan;
L
Linus Torvalds 已提交
177 178

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

181
	err = mtd_device_parse_register(mtd, data->part_parsers, NULL,
182
					data->partitions, data->nr_partitions);
183 184
	if (err)
		goto err_add;
L
Linus Torvalds 已提交
185 186 187

	/* Return happy */
	return 0;
188

189
err_add:
190
	nand_release(this);
191

192 193
err_scan:
	iounmap(sharpsl->io);
194
err_ioremap:
195
err_get_res:
196
	kfree(sharpsl);
197 198
	return err;
}
L
Linus Torvalds 已提交
199 200 201 202

/*
 * Clean up routine
 */
B
Bill Pemberton 已提交
203
static int sharpsl_nand_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
204
{
205 206
	struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);

L
Linus Torvalds 已提交
207
	/* Release resources, unregister device */
208
	nand_release(&sharpsl->chip);
209

210
	iounmap(sharpsl->io);
L
Linus Torvalds 已提交
211 212

	/* Free the MTD device structure */
213
	kfree(sharpsl);
214 215 216 217 218 219 220 221 222

	return 0;
}

static struct platform_driver sharpsl_nand_driver = {
	.driver = {
		.name	= "sharpsl-nand",
	},
	.probe		= sharpsl_nand_probe,
B
Bill Pemberton 已提交
223
	.remove		= sharpsl_nand_remove,
224 225
};

226
module_platform_driver(sharpsl_nand_driver);
L
Linus Torvalds 已提交
227 228 229 230

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