sharpsl.c 6.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * drivers/mtd/nand/sharpsl.c
 *
 *  Copyright (C) 2004 Richard Purdie
 *
 *  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>
#include <linux/interrupt.h>
#include <asm/io.h>
24
#include <asm/arch/hardware.h>
L
Linus Torvalds 已提交
25 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
#include <asm/mach-types.h>

static void __iomem *sharpsl_io_base;
static int sharpsl_phys_base = 0x0C000000;

/* register offset */
#define ECCLPLB	 	sharpsl_io_base+0x00	/* line parity 7 - 0 bit */
#define ECCLPUB	 	sharpsl_io_base+0x04	/* line parity 15 - 8 bit */
#define ECCCP	   	sharpsl_io_base+0x08	/* column parity 5 - 0 bit */
#define ECCCNTR	 	sharpsl_io_base+0x0C	/* ECC byte counter */
#define ECCCLRR	 	sharpsl_io_base+0x10	/* cleare ECC */
#define FLASHIO	 	sharpsl_io_base+0x14	/* Flash I/O */
#define FLASHCTL	sharpsl_io_base+0x18	/* Flash Control */

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

/*
 * MTD structure for SharpSL
 */
static struct mtd_info *sharpsl_mtd = NULL;

/*
 * Define partitions for flash device
 */
#define DEFAULT_NUM_PARTITIONS 3

static int nr_partitions;
static struct mtd_partition sharpsl_nand_default_partition_info[] = {
	{
60 61 62 63
	 .name = "System Area",
	 .offset = 0,
	 .size = 7 * 1024 * 1024,
	 },
L
Linus Torvalds 已提交
64
	{
65 66 67 68
	 .name = "Root Filesystem",
	 .offset = 7 * 1024 * 1024,
	 .size = 30 * 1024 * 1024,
	 },
L
Linus Torvalds 已提交
69
	{
70 71 72 73
	 .name = "Home Filesystem",
	 .offset = MTDPART_OFS_APPEND,
	 .size = MTDPART_SIZ_FULL,
	 },
L
Linus Torvalds 已提交
74 75
};

76
/*
L
Linus Torvalds 已提交
77
 *	hardware specific access to control-lines
78
 *	ctrl:
79
 *	NAND_CNE: bit 0 -> ! bit 0 & 4
80 81 82
 *	NAND_CLE: bit 1 -> bit 1
 *	NAND_ALE: bit 2 -> bit 2
 *
L
Linus Torvalds 已提交
83
 */
84 85
static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
				   unsigned int ctrl)
L
Linus Torvalds 已提交
86
{
87 88 89 90 91 92
	struct nand_chip *chip = mtd->priv;

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

		bits |= (ctrl & 0x01) << 4;
93 94 95 96

		bits ^= 0x11;

		writeb((readb(FLASHCTL) & ~0x17) | bits, FLASHCTL);
L
Linus Torvalds 已提交
97
	}
98 99 100

	if (cmd != NAND_CMD_NONE)
		writeb(cmd, chip->IO_ADDR_W);
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111
}

static uint8_t scan_ff_pattern[] = { 0xff, 0xff };

static struct nand_bbt_descr sharpsl_bbt = {
	.options = 0,
	.offs = 4,
	.len = 2,
	.pattern = scan_ff_pattern
};

112 113 114 115 116 117 118
static struct nand_bbt_descr sharpsl_akita_bbt = {
	.options = 0,
	.offs = 4,
	.len = 1,
	.pattern = scan_ff_pattern
};

119
static struct nand_ecclayout akita_oobinfo = {
120 121
	.eccbytes = 24,
	.eccpos = {
122 123 124 125
		   0x5, 0x1, 0x2, 0x3, 0x6, 0x7, 0x15, 0x11,
		   0x12, 0x13, 0x16, 0x17, 0x25, 0x21, 0x22, 0x23,
		   0x26, 0x27, 0x35, 0x31, 0x32, 0x33, 0x36, 0x37},
	.oobfree = {{0x08, 0x09}}
126 127
};

128
static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
L
Linus Torvalds 已提交
129 130 131 132
{
	return !((readb(FLASHCTL) & FLRYBY) == 0);
}

133
static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
L
Linus Torvalds 已提交
134
{
135
	writeb(0, ECCCLRR);
L
Linus Torvalds 已提交
136 137
}

138
static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
	ecc_code[0] = ~readb(ECCLPUB);
	ecc_code[1] = ~readb(ECCLPLB);
	ecc_code[2] = (~readb(ECCCP) << 2) | 0x03;
	return readb(ECCCNTR) != 0;
}

#ifdef CONFIG_MTD_PARTITIONS
const char *part_probes[] = { "cmdlinepart", NULL };
#endif

/*
 * Main initialization routine
 */
153
static int __init sharpsl_nand_init(void)
L
Linus Torvalds 已提交
154 155
{
	struct nand_chip *this;
156
	struct mtd_partition *sharpsl_partition_info;
L
Linus Torvalds 已提交
157 158 159
	int err = 0;

	/* Allocate memory for MTD device structure and private data */
160
	sharpsl_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
L
Linus Torvalds 已提交
161
	if (!sharpsl_mtd) {
162
		printk("Unable to allocate SharpSL NAND MTD device structure.\n");
L
Linus Torvalds 已提交
163 164
		return -ENOMEM;
	}
165

J
Joe Perches 已提交
166
	/* map physical address */
L
Linus Torvalds 已提交
167
	sharpsl_io_base = ioremap(sharpsl_phys_base, 0x1000);
168
	if (!sharpsl_io_base) {
L
Linus Torvalds 已提交
169 170 171 172
		printk("ioremap to access Sharp SL NAND chip failed\n");
		kfree(sharpsl_mtd);
		return -EIO;
	}
173

L
Linus Torvalds 已提交
174
	/* Get pointer to private data */
175
	this = (struct nand_chip *)(&sharpsl_mtd[1]);
L
Linus Torvalds 已提交
176 177

	/* Initialize structures */
178 179
	memset(sharpsl_mtd, 0, sizeof(struct mtd_info));
	memset(this, 0, sizeof(struct nand_chip));
L
Linus Torvalds 已提交
180 181 182

	/* Link the private data with the MTD structure */
	sharpsl_mtd->priv = this;
183
	sharpsl_mtd->owner = THIS_MODULE;
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193

	/*
	 * PXA initialize
	 */
	writeb(readb(FLASHCTL) | FLWP, FLASHCTL);

	/* Set address of NAND IO lines */
	this->IO_ADDR_R = FLASHIO;
	this->IO_ADDR_W = FLASHIO;
	/* Set address of hardware control function */
194
	this->cmd_ctrl = sharpsl_nand_hwcontrol;
L
Linus Torvalds 已提交
195 196 197 198
	this->dev_ready = sharpsl_nand_dev_ready;
	/* 15 us command delay time */
	this->chip_delay = 15;
	/* set eccmode using hardware ECC */
T
Thomas Gleixner 已提交
199 200 201
	this->ecc.mode = NAND_ECC_HW;
	this->ecc.size = 256;
	this->ecc.bytes = 3;
202
	this->badblock_pattern = &sharpsl_bbt;
203 204
	if (machine_is_akita() || machine_is_borzoi()) {
		this->badblock_pattern = &sharpsl_akita_bbt;
205
		this->ecc.layout = &akita_oobinfo;
206
	}
T
Thomas Gleixner 已提交
207 208 209
	this->ecc.hwctl = sharpsl_nand_enable_hwecc;
	this->ecc.calculate = sharpsl_nand_calculate_ecc;
	this->ecc.correct = nand_correct_data;
L
Linus Torvalds 已提交
210 211

	/* Scan to find existence of the device */
212
	err = nand_scan(sharpsl_mtd, 1);
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220
	if (err) {
		iounmap(sharpsl_io_base);
		kfree(sharpsl_mtd);
		return err;
	}

	/* Register the partitions */
	sharpsl_mtd->name = "sharpsl-nand";
221
	nr_partitions = parse_mtd_partitions(sharpsl_mtd, part_probes, &sharpsl_partition_info, 0);
222

L
Linus Torvalds 已提交
223 224 225 226
	if (nr_partitions <= 0) {
		nr_partitions = DEFAULT_NUM_PARTITIONS;
		sharpsl_partition_info = sharpsl_nand_default_partition_info;
		if (machine_is_poodle()) {
227
			sharpsl_partition_info[1].size = 22 * 1024 * 1024;
L
Linus Torvalds 已提交
228
		} else if (machine_is_corgi() || machine_is_shepherd()) {
229
			sharpsl_partition_info[1].size = 25 * 1024 * 1024;
L
Linus Torvalds 已提交
230
		} else if (machine_is_husky()) {
231
			sharpsl_partition_info[1].size = 53 * 1024 * 1024;
232
		} else if (machine_is_spitz()) {
233
			sharpsl_partition_info[1].size = 5 * 1024 * 1024;
234
		} else if (machine_is_akita()) {
235
			sharpsl_partition_info[1].size = 58 * 1024 * 1024;
236
		} else if (machine_is_borzoi()) {
237
			sharpsl_partition_info[1].size = 32 * 1024 * 1024;
238
		}
L
Linus Torvalds 已提交
239 240 241 242 243 244 245
	}

	add_mtd_partitions(sharpsl_mtd, sharpsl_partition_info, nr_partitions);

	/* Return happy */
	return 0;
}
246

L
Linus Torvalds 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
module_init(sharpsl_nand_init);

/*
 * Clean up routine
 */
static void __exit sharpsl_nand_cleanup(void)
{
	/* Release resources, unregister device */
	nand_release(sharpsl_mtd);

	iounmap(sharpsl_io_base);

	/* Free the MTD device structure */
	kfree(sharpsl_mtd);
}
262

L
Linus Torvalds 已提交
263 264 265 266 267
module_exit(sharpsl_nand_cleanup);

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