ppchameleonevb.c 12.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 *  drivers/mtd/nand/ppchameleonevb.c
 *
 *  Copyright (C) 2003 DAVE Srl (info@wawnet.biz)
 *
 *  Derived from drivers/mtd/nand/edb7312.c
 *
 *
9
 * $Id: ppchameleonevb.c,v 1.7 2005/11/07 11:14:31 gleixner Exp $
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 60
 *
 * 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.
 *
 *  Overview:
 *   This is a device driver for the NAND flash devices found on the
 *   PPChameleon/PPChameleonEVB system.
 *   PPChameleon options (autodetected):
 *   - BA model: no NAND
 *   - ME model: 32MB (Samsung K9F5608U0B)
 *   - HI model: 128MB (Samsung K9F1G08UOM)
 *   PPChameleonEVB options:
 *   - 32MB (Samsung K9F5608U0B)
 */

#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/partitions.h>
#include <asm/io.h>
#include <platforms/PPChameleonEVB.h>

#undef USE_READY_BUSY_PIN
#define USE_READY_BUSY_PIN
/* see datasheets (tR) */
#define NAND_BIG_DELAY_US		25
#define NAND_SMALL_DELAY_US		10

/* handy sizes */
#define SZ_4M                           0x00400000
#define NAND_SMALL_SIZE                 0x02000000
#define NAND_MTD_NAME		"ppchameleon-nand"
#define NAND_EVB_MTD_NAME	"ppchameleonevb-nand"

/* GPIO pins used to drive NAND chip mounted on processor module */
#define NAND_nCE_GPIO_PIN 		(0x80000000 >> 1)
#define NAND_CLE_GPIO_PIN 		(0x80000000 >> 2)
#define NAND_ALE_GPIO_PIN 		(0x80000000 >> 3)
#define NAND_RB_GPIO_PIN 		(0x80000000 >> 4)
/* GPIO pins used to drive NAND chip mounted on EVB */
#define NAND_EVB_nCE_GPIO_PIN 	(0x80000000 >> 14)
#define NAND_EVB_CLE_GPIO_PIN 	(0x80000000 >> 15)
#define NAND_EVB_ALE_GPIO_PIN 	(0x80000000 >> 16)
#define NAND_EVB_RB_GPIO_PIN 	(0x80000000 >> 31)

/*
 * MTD structure for PPChameleonEVB board
 */
61
static struct mtd_info *ppchameleon_mtd = NULL;
L
Linus Torvalds 已提交
62 63 64 65 66
static struct mtd_info *ppchameleonevb_mtd = NULL;

/*
 * Module stuff
 */
67
static unsigned long ppchameleon_fio_pbase = CFG_NAND0_PADDR;
L
Linus Torvalds 已提交
68 69 70 71 72 73
static unsigned long ppchameleonevb_fio_pbase = CFG_NAND1_PADDR;

#ifdef MODULE
module_param(ppchameleon_fio_pbase, ulong, 0);
module_param(ppchameleonevb_fio_pbase, ulong, 0);
#else
74 75
__setup("ppchameleon_fio_pbase=", ppchameleon_fio_pbase);
__setup("ppchameleonevb_fio_pbase=", ppchameleonevb_fio_pbase);
L
Linus Torvalds 已提交
76 77 78 79 80 81 82
#endif

#ifdef CONFIG_MTD_PARTITIONS
/*
 * Define static partitions for flash devices
 */
static struct mtd_partition partition_info_hi[] = {
83 84 85 86
      { .name = "PPChameleon HI Nand Flash",
	offset = 0,
	.size = 128 * 1024 * 1024
      }
L
Linus Torvalds 已提交
87 88 89
};

static struct mtd_partition partition_info_me[] = {
90 91 92 93
      { .name = "PPChameleon ME Nand Flash",
	.offset = 0,
	.size = 32 * 1024 * 1024
      }
L
Linus Torvalds 已提交
94 95 96
};

static struct mtd_partition partition_info_evb[] = {
97 98 99 100
      { .name = "PPChameleonEVB Nand Flash",
	.offset = 0,
	.size = 32 * 1024 * 1024
      }
L
Linus Torvalds 已提交
101 102 103 104
};

#define NUM_PARTITIONS 1

105
extern int parse_cmdline_partitions(struct mtd_info *master, struct mtd_partition **pparts, const char *mtd_id);
L
Linus Torvalds 已提交
106 107 108 109 110 111 112
#endif

/*
 *	hardware specific access to control-lines
 */
static void ppchameleon_hwcontrol(struct mtd_info *mtdinfo, int cmd)
{
113
	switch (cmd) {
L
Linus Torvalds 已提交
114 115

	case NAND_CTL_SETCLE:
116
		MACRO_NAND_CTL_SETCLE((unsigned long)CFG_NAND0_PADDR);
L
Linus Torvalds 已提交
117 118
		break;
	case NAND_CTL_CLRCLE:
119
		MACRO_NAND_CTL_CLRCLE((unsigned long)CFG_NAND0_PADDR);
L
Linus Torvalds 已提交
120 121
		break;
	case NAND_CTL_SETALE:
122
		MACRO_NAND_CTL_SETALE((unsigned long)CFG_NAND0_PADDR);
L
Linus Torvalds 已提交
123 124
		break;
	case NAND_CTL_CLRALE:
125
		MACRO_NAND_CTL_CLRALE((unsigned long)CFG_NAND0_PADDR);
L
Linus Torvalds 已提交
126 127
		break;
	case NAND_CTL_SETNCE:
128
		MACRO_NAND_ENABLE_CE((unsigned long)CFG_NAND0_PADDR);
L
Linus Torvalds 已提交
129 130
		break;
	case NAND_CTL_CLRNCE:
131
		MACRO_NAND_DISABLE_CE((unsigned long)CFG_NAND0_PADDR);
L
Linus Torvalds 已提交
132 133 134 135 136 137
		break;
	}
}

static void ppchameleonevb_hwcontrol(struct mtd_info *mtdinfo, int cmd)
{
138
	switch (cmd) {
L
Linus Torvalds 已提交
139 140

	case NAND_CTL_SETCLE:
141
		MACRO_NAND_CTL_SETCLE((unsigned long)CFG_NAND1_PADDR);
L
Linus Torvalds 已提交
142 143
		break;
	case NAND_CTL_CLRCLE:
144
		MACRO_NAND_CTL_CLRCLE((unsigned long)CFG_NAND1_PADDR);
L
Linus Torvalds 已提交
145 146
		break;
	case NAND_CTL_SETALE:
147
		MACRO_NAND_CTL_SETALE((unsigned long)CFG_NAND1_PADDR);
L
Linus Torvalds 已提交
148 149
		break;
	case NAND_CTL_CLRALE:
150
		MACRO_NAND_CTL_CLRALE((unsigned long)CFG_NAND1_PADDR);
L
Linus Torvalds 已提交
151 152
		break;
	case NAND_CTL_SETNCE:
153
		MACRO_NAND_ENABLE_CE((unsigned long)CFG_NAND1_PADDR);
L
Linus Torvalds 已提交
154 155
		break;
	case NAND_CTL_CLRNCE:
156
		MACRO_NAND_DISABLE_CE((unsigned long)CFG_NAND1_PADDR);
L
Linus Torvalds 已提交
157 158 159 160 161 162 163 164 165 166
		break;
	}
}

#ifdef USE_READY_BUSY_PIN
/*
 *	read device ready pin
 */
static int ppchameleon_device_ready(struct mtd_info *minfo)
{
167
	if (in_be32((volatile unsigned *)GPIO0_IR) & NAND_RB_GPIO_PIN)
L
Linus Torvalds 已提交
168 169 170 171 172 173
		return 1;
	return 0;
}

static int ppchameleonevb_device_ready(struct mtd_info *minfo)
{
174 175
	if (in_be32((volatile unsigned *)GPIO0_IR) & NAND_EVB_RB_GPIO_PIN)
		return 1;
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186 187
	return 0;
}
#endif

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

/*
 * Main initialization routine
 */
188
static int __init ppchameleonevb_init(void)
L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196 197 198 199 200
{
	struct nand_chip *this;
	const char *part_type = 0;
	int mtd_parts_nb = 0;
	struct mtd_partition *mtd_parts = 0;
	void __iomem *ppchameleon_fio_base;
	void __iomem *ppchameleonevb_fio_base;

	/*********************************
	* Processor module NAND (if any) *
	*********************************/
	/* Allocate memory for MTD device structure and private data */
201
	ppchameleon_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
L
Linus Torvalds 已提交
202 203 204 205 206 207 208
	if (!ppchameleon_mtd) {
		printk("Unable to allocate PPChameleon NAND MTD device structure.\n");
		return -ENOMEM;
	}

	/* map physical address */
	ppchameleon_fio_base = ioremap(ppchameleon_fio_pbase, SZ_4M);
209
	if (!ppchameleon_fio_base) {
L
Linus Torvalds 已提交
210 211 212 213 214 215
		printk("ioremap PPChameleon NAND flash failed\n");
		kfree(ppchameleon_mtd);
		return -EIO;
	}

	/* Get pointer to private data */
216
	this = (struct nand_chip *)(&ppchameleon_mtd[1]);
L
Linus Torvalds 已提交
217 218

	/* Initialize structures */
219 220
	memset(ppchameleon_mtd, 0, sizeof(struct mtd_info));
	memset(this, 0, sizeof(struct nand_chip));
L
Linus Torvalds 已提交
221 222 223

	/* Link the private data with the MTD structure */
	ppchameleon_mtd->priv = this;
224
	ppchameleon_mtd->owner = THIS_MODULE;
L
Linus Torvalds 已提交
225

226
	/* Initialize GPIOs */
L
Linus Torvalds 已提交
227 228
	/* Pin mapping for NAND chip */
	/*
229 230 231 232 233
	   CE   GPIO_01
	   CLE  GPIO_02
	   ALE  GPIO_03
	   R/B  GPIO_04
	 */
L
Linus Torvalds 已提交
234
	/* output select */
235
	out_be32((volatile unsigned *)GPIO0_OSRH, in_be32((volatile unsigned *)GPIO0_OSRH) & 0xC0FFFFFF);
L
Linus Torvalds 已提交
236
	/* three-state select */
237
	out_be32((volatile unsigned *)GPIO0_TSRH, in_be32((volatile unsigned *)GPIO0_TSRH) & 0xC0FFFFFF);
L
Linus Torvalds 已提交
238
	/* enable output driver */
239 240
	out_be32((volatile unsigned *)GPIO0_TCR,
		 in_be32((volatile unsigned *)GPIO0_TCR) | NAND_nCE_GPIO_PIN | NAND_CLE_GPIO_PIN | NAND_ALE_GPIO_PIN);
L
Linus Torvalds 已提交
241 242
#ifdef USE_READY_BUSY_PIN
	/* three-state select */
243
	out_be32((volatile unsigned *)GPIO0_TSRH, in_be32((volatile unsigned *)GPIO0_TSRH) & 0xFF3FFFFF);
L
Linus Torvalds 已提交
244
	/* high-impedecence */
245
	out_be32((volatile unsigned *)GPIO0_TCR, in_be32((volatile unsigned *)GPIO0_TCR) & (~NAND_RB_GPIO_PIN));
L
Linus Torvalds 已提交
246
	/* input select */
247 248
	out_be32((volatile unsigned *)GPIO0_ISR1H,
		 (in_be32((volatile unsigned *)GPIO0_ISR1H) & 0xFF3FFFFF) | 0x00400000);
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262
#endif

	/* insert callbacks */
	this->IO_ADDR_R = ppchameleon_fio_base;
	this->IO_ADDR_W = ppchameleon_fio_base;
	this->hwcontrol = ppchameleon_hwcontrol;
#ifdef USE_READY_BUSY_PIN
	this->dev_ready = ppchameleon_device_ready;
#endif
	this->chip_delay = NAND_BIG_DELAY_US;
	/* ECC mode */
	this->eccmode = NAND_ECC_SOFT;

	/* Scan to find existence of the device (it could not be mounted) */
263
	if (nand_scan(ppchameleon_mtd, 1)) {
L
Linus Torvalds 已提交
264
		iounmap((void *)ppchameleon_fio_base);
265
		kfree(ppchameleon_mtd);
L
Linus Torvalds 已提交
266 267 268 269 270 271 272 273 274 275 276 277
		goto nand_evb_init;
	}
#ifndef USE_READY_BUSY_PIN
	/* Adjust delay if necessary */
	if (ppchameleon_mtd->size == NAND_SMALL_SIZE)
		this->chip_delay = NAND_SMALL_DELAY_US;
#endif

#ifdef CONFIG_MTD_PARTITIONS
	ppchameleon_mtd->name = "ppchameleon-nand";
	mtd_parts_nb = parse_mtd_partitions(ppchameleon_mtd, part_probes, &mtd_parts, 0);
	if (mtd_parts_nb > 0)
278
		part_type = "command line";
L
Linus Torvalds 已提交
279
	else
280
		mtd_parts_nb = 0;
L
Linus Torvalds 已提交
281
#endif
282
	if (mtd_parts_nb == 0) {
L
Linus Torvalds 已提交
283 284 285 286 287 288 289 290 291 292 293 294
		if (ppchameleon_mtd->size == NAND_SMALL_SIZE)
			mtd_parts = partition_info_me;
		else
			mtd_parts = partition_info_hi;
		mtd_parts_nb = NUM_PARTITIONS;
		part_type = "static";
	}

	/* Register the partitions */
	printk(KERN_NOTICE "Using %s partition definition\n", part_type);
	add_mtd_partitions(ppchameleon_mtd, mtd_parts, mtd_parts_nb);

295
 nand_evb_init:
L
Linus Torvalds 已提交
296 297 298 299
	/****************************
	* EVB NAND (always present) *
	****************************/
	/* Allocate memory for MTD device structure and private data */
300
	ppchameleonevb_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
L
Linus Torvalds 已提交
301 302 303 304 305 306 307
	if (!ppchameleonevb_mtd) {
		printk("Unable to allocate PPChameleonEVB NAND MTD device structure.\n");
		return -ENOMEM;
	}

	/* map physical address */
	ppchameleonevb_fio_base = ioremap(ppchameleonevb_fio_pbase, SZ_4M);
308
	if (!ppchameleonevb_fio_base) {
L
Linus Torvalds 已提交
309 310 311 312 313 314
		printk("ioremap PPChameleonEVB NAND flash failed\n");
		kfree(ppchameleonevb_mtd);
		return -EIO;
	}

	/* Get pointer to private data */
315
	this = (struct nand_chip *)(&ppchameleonevb_mtd[1]);
L
Linus Torvalds 已提交
316 317

	/* Initialize structures */
318 319
	memset(ppchameleonevb_mtd, 0, sizeof(struct mtd_info));
	memset(this, 0, sizeof(struct nand_chip));
L
Linus Torvalds 已提交
320 321 322 323

	/* Link the private data with the MTD structure */
	ppchameleonevb_mtd->priv = this;

324
	/* Initialize GPIOs */
L
Linus Torvalds 已提交
325 326
	/* Pin mapping for NAND chip */
	/*
327 328 329 330 331
	   CE   GPIO_14
	   CLE  GPIO_15
	   ALE  GPIO_16
	   R/B  GPIO_31
	 */
L
Linus Torvalds 已提交
332
	/* output select */
333 334
	out_be32((volatile unsigned *)GPIO0_OSRH, in_be32((volatile unsigned *)GPIO0_OSRH) & 0xFFFFFFF0);
	out_be32((volatile unsigned *)GPIO0_OSRL, in_be32((volatile unsigned *)GPIO0_OSRL) & 0x3FFFFFFF);
L
Linus Torvalds 已提交
335
	/* three-state select */
336 337
	out_be32((volatile unsigned *)GPIO0_TSRH, in_be32((volatile unsigned *)GPIO0_TSRH) & 0xFFFFFFF0);
	out_be32((volatile unsigned *)GPIO0_TSRL, in_be32((volatile unsigned *)GPIO0_TSRL) & 0x3FFFFFFF);
L
Linus Torvalds 已提交
338
	/* enable output driver */
339
	out_be32((volatile unsigned *)GPIO0_TCR, in_be32((volatile unsigned *)GPIO0_TCR) | NAND_EVB_nCE_GPIO_PIN |
L
Linus Torvalds 已提交
340 341 342
		 NAND_EVB_CLE_GPIO_PIN | NAND_EVB_ALE_GPIO_PIN);
#ifdef USE_READY_BUSY_PIN
	/* three-state select */
343
	out_be32((volatile unsigned *)GPIO0_TSRL, in_be32((volatile unsigned *)GPIO0_TSRL) & 0xFFFFFFFC);
L
Linus Torvalds 已提交
344
	/* high-impedecence */
345
	out_be32((volatile unsigned *)GPIO0_TCR, in_be32((volatile unsigned *)GPIO0_TCR) & (~NAND_EVB_RB_GPIO_PIN));
L
Linus Torvalds 已提交
346
	/* input select */
347 348
	out_be32((volatile unsigned *)GPIO0_ISR1L,
		 (in_be32((volatile unsigned *)GPIO0_ISR1L) & 0xFFFFFFFC) | 0x00000001);
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
#endif

	/* insert callbacks */
	this->IO_ADDR_R = ppchameleonevb_fio_base;
	this->IO_ADDR_W = ppchameleonevb_fio_base;
	this->hwcontrol = ppchameleonevb_hwcontrol;
#ifdef USE_READY_BUSY_PIN
	this->dev_ready = ppchameleonevb_device_ready;
#endif
	this->chip_delay = NAND_SMALL_DELAY_US;

	/* ECC mode */
	this->eccmode = NAND_ECC_SOFT;

	/* Scan to find existence of the device */
364
	if (nand_scan(ppchameleonevb_mtd, 1)) {
L
Linus Torvalds 已提交
365
		iounmap((void *)ppchameleonevb_fio_base);
366
		kfree(ppchameleonevb_mtd);
L
Linus Torvalds 已提交
367 368 369 370 371 372
		return -ENXIO;
	}
#ifdef CONFIG_MTD_PARTITIONS
	ppchameleonevb_mtd->name = NAND_EVB_MTD_NAME;
	mtd_parts_nb = parse_mtd_partitions(ppchameleonevb_mtd, part_probes_evb, &mtd_parts, 0);
	if (mtd_parts_nb > 0)
373
		part_type = "command line";
L
Linus Torvalds 已提交
374
	else
375
		mtd_parts_nb = 0;
L
Linus Torvalds 已提交
376
#endif
377
	if (mtd_parts_nb == 0) {
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386 387 388 389
		mtd_parts = partition_info_evb;
		mtd_parts_nb = NUM_PARTITIONS;
		part_type = "static";
	}

	/* Register the partitions */
	printk(KERN_NOTICE "Using %s partition definition\n", part_type);
	add_mtd_partitions(ppchameleonevb_mtd, mtd_parts, mtd_parts_nb);

	/* Return happy */
	return 0;
}
390

L
Linus Torvalds 已提交
391 392 393 394 395
module_init(ppchameleonevb_init);

/*
 * Clean up routine
 */
396
static void __exit ppchameleonevb_cleanup(void)
L
Linus Torvalds 已提交
397 398 399 400
{
	struct nand_chip *this;

	/* Release resources, unregister device(s) */
401 402
	nand_release(ppchameleon_mtd);
	nand_release(ppchameleonevb_mtd);
403

L
Linus Torvalds 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	/* Release iomaps */
	this = (struct nand_chip *) &ppchameleon_mtd[1];
	iounmap((void *) this->IO_ADDR_R;
	this = (struct nand_chip *) &ppchameleonevb_mtd[1];
	iounmap((void *) this->IO_ADDR_R;

	/* Free the MTD device structure */
	kfree (ppchameleon_mtd);
	kfree (ppchameleonevb_mtd);
}
module_exit(ppchameleonevb_cleanup);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("DAVE Srl <support-ppchameleon@dave-tech.it>");
MODULE_DESCRIPTION("MTD map driver for DAVE Srl PPChameleonEVB board");