au1550nd.c 14.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 *  drivers/mtd/nand/au1550nd.c
 *
 *  Copyright (C) 2004 Embedded Edge, LLC
 *
6
 * $Id: au1550nd.c,v 1.13 2005/11/07 11:14:30 gleixner Exp $
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16
 *
 * 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/slab.h>
#include <linux/init.h>
#include <linux/module.h>
17
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
18 19 20
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/partitions.h>
21
#include <linux/version.h>
L
Linus Torvalds 已提交
22 23 24 25
#include <asm/io.h>

/* fixme: this is ugly */
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
26
#include <asm/mach-au1x00/au1xxx.h>
L
Linus Torvalds 已提交
27 28 29
#else
#include <asm/au1000.h>
#ifdef CONFIG_MIPS_PB1550
30
#include <asm/pb1550.h>
L
Linus Torvalds 已提交
31 32
#endif
#ifdef CONFIG_MIPS_DB1550
33
#include <asm/db1x00.h>
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41
#endif
#endif

/*
 * MTD structure for NAND controller
 */
static struct mtd_info *au1550_mtd = NULL;
static void __iomem *p_nand;
42
static int nand_width = 1;	/* default x8 */
L
Linus Torvalds 已提交
43 44 45 46

/*
 * Define partitions for flash device
 */
47
static const struct mtd_partition partition_info[] = {
48
	{
49 50 51
	 .name = "NAND FS 0",
	 .offset = 0,
	 .size = 8 * 1024 * 1024},
52
	{
53 54 55
	 .name = "NAND FS 1",
	 .offset = MTDPART_OFS_APPEND,
	 .size = MTDPART_SIZ_FULL}
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
};

/**
 * au_read_byte -  read one byte from the chip
 * @mtd:	MTD device structure
 *
 *  read function for 8bit buswith
 */
static u_char au_read_byte(struct mtd_info *mtd)
{
	struct nand_chip *this = mtd->priv;
	u_char ret = readb(this->IO_ADDR_R);
	au_sync();
	return ret;
}

/**
 * au_write_byte -  write one byte to the chip
 * @mtd:	MTD device structure
 * @byte:	pointer to data byte to write
 *
 *  write function for 8it buswith
 */
static void au_write_byte(struct mtd_info *mtd, u_char byte)
{
	struct nand_chip *this = mtd->priv;
	writeb(byte, this->IO_ADDR_W);
	au_sync();
}

/**
 * au_read_byte16 -  read one byte endianess aware from the chip
 * @mtd:	MTD device structure
 *
90
 *  read function for 16bit buswith with
L
Linus Torvalds 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
 * endianess conversion
 */
static u_char au_read_byte16(struct mtd_info *mtd)
{
	struct nand_chip *this = mtd->priv;
	u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
	au_sync();
	return ret;
}

/**
 * au_write_byte16 -  write one byte endianess aware to the chip
 * @mtd:	MTD device structure
 * @byte:	pointer to data byte to write
 *
 *  write function for 16bit buswith with
 * endianess conversion
 */
static void au_write_byte16(struct mtd_info *mtd, u_char byte)
{
	struct nand_chip *this = mtd->priv;
	writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
	au_sync();
}

/**
 * au_read_word -  read one word from the chip
 * @mtd:	MTD device structure
 *
120
 *  read function for 16bit buswith without
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
 * endianess conversion
 */
static u16 au_read_word(struct mtd_info *mtd)
{
	struct nand_chip *this = mtd->priv;
	u16 ret = readw(this->IO_ADDR_R);
	au_sync();
	return ret;
}

/**
 * au_write_word -  write one word to the chip
 * @mtd:	MTD device structure
 * @word:	data word to write
 *
136
 *  write function for 16bit buswith without
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
 * endianess conversion
 */
static void au_write_word(struct mtd_info *mtd, u16 word)
{
	struct nand_chip *this = mtd->priv;
	writew(word, this->IO_ADDR_W);
	au_sync();
}

/**
 * au_write_buf -  write buffer to chip
 * @mtd:	MTD device structure
 * @buf:	data buffer
 * @len:	number of bytes to write
 *
 *  write function for 8bit buswith
 */
static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
{
	int i;
	struct nand_chip *this = mtd->priv;

159
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
160 161 162 163 164 165
		writeb(buf[i], this->IO_ADDR_W);
		au_sync();
	}
}

/**
166
 * au_read_buf -  read chip data into buffer
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177
 * @mtd:	MTD device structure
 * @buf:	buffer to store date
 * @len:	number of bytes to read
 *
 *  read function for 8bit buswith
 */
static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
{
	int i;
	struct nand_chip *this = mtd->priv;

178
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
179
		buf[i] = readb(this->IO_ADDR_R);
180
		au_sync();
L
Linus Torvalds 已提交
181 182 183 184
	}
}

/**
185
 * au_verify_buf -  Verify chip data against buffer
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193 194 195 196
 * @mtd:	MTD device structure
 * @buf:	buffer containing the data to compare
 * @len:	number of bytes to compare
 *
 *  verify function for 8bit buswith
 */
static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
{
	int i;
	struct nand_chip *this = mtd->priv;

197
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		if (buf[i] != readb(this->IO_ADDR_R))
			return -EFAULT;
		au_sync();
	}

	return 0;
}

/**
 * au_write_buf16 -  write buffer to chip
 * @mtd:	MTD device structure
 * @buf:	data buffer
 * @len:	number of bytes to write
 *
 *  write function for 16bit buswith
 */
static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
{
	int i;
	struct nand_chip *this = mtd->priv;
	u16 *p = (u16 *) buf;
	len >>= 1;
220

221
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
222 223 224
		writew(p[i], this->IO_ADDR_W);
		au_sync();
	}
225

L
Linus Torvalds 已提交
226 227 228
}

/**
229
 * au_read_buf16 -  read chip data into buffer
L
Linus Torvalds 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242
 * @mtd:	MTD device structure
 * @buf:	buffer to store date
 * @len:	number of bytes to read
 *
 *  read function for 16bit buswith
 */
static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
{
	int i;
	struct nand_chip *this = mtd->priv;
	u16 *p = (u16 *) buf;
	len >>= 1;

243
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
244 245 246 247 248 249
		p[i] = readw(this->IO_ADDR_R);
		au_sync();
	}
}

/**
250
 * au_verify_buf16 -  Verify chip data against buffer
L
Linus Torvalds 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263
 * @mtd:	MTD device structure
 * @buf:	buffer containing the data to compare
 * @len:	number of bytes to compare
 *
 *  verify function for 16bit buswith
 */
static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
{
	int i;
	struct nand_chip *this = mtd->priv;
	u16 *p = (u16 *) buf;
	len >>= 1;

264
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272 273 274 275 276
		if (p[i] != readw(this->IO_ADDR_R))
			return -EFAULT;
		au_sync();
	}
	return 0;
}


static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
{
	register struct nand_chip *this = mtd->priv;

277 278 279 280 281 282 283 284 285
	switch (cmd) {

	case NAND_CTL_SETCLE:
		this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
		break;

	case NAND_CTL_CLRCLE:
		this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
		break;
L
Linus Torvalds 已提交
286

287 288 289
	case NAND_CTL_SETALE:
		this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR;
		break;
L
Linus Torvalds 已提交
290

291 292
	case NAND_CTL_CLRALE:
		this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
293
		/* FIXME: Nobody knows why this is necessary,
L
Linus Torvalds 已提交
294
		 * but it works only that way */
295
		udelay(1);
L
Linus Torvalds 已提交
296 297
		break;

298
	case NAND_CTL_SETNCE:
L
Linus Torvalds 已提交
299
		/* assert (force assert) chip enable */
300
		au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
L
Linus Torvalds 已提交
301 302
		break;

303
	case NAND_CTL_CLRNCE:
304 305
		/* deassert chip enable */
		au_writel(0, MEM_STNDCTL);
L
Linus Torvalds 已提交
306 307 308 309
		break;
	}

	this->IO_ADDR_R = this->IO_ADDR_W;
310

L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318 319 320 321
	/* Drain the writebuffer */
	au_sync();
}

int au1550_device_ready(struct mtd_info *mtd)
{
	int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
	au_sync();
	return ret;
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
/**
 * au1550_select_chip - control -CE line
 *	Forbid driving -CE manually permitting the NAND controller to do this.
 *	Keeping -CE asserted during the whole sector reads interferes with the
 *	NOR flash and PCMCIA drivers as it causes contention on the static bus.
 *	We only have to hold -CE low for the NAND read commands since the flash
 *	chip needs it to be asserted during chip not ready time but the NAND
 *	controller keeps it released.
 *
 * @mtd:	MTD device structure
 * @chip:	chipnumber to select, -1 for deselect
 */
static void au1550_select_chip(struct mtd_info *mtd, int chip)
{
}

/**
 * au1550_command - Send command to NAND device
 * @mtd:	MTD device structure
 * @command:	the command to be sent
 * @column:	the column address for this command, -1 if none
 * @page_addr:	the page address for this command, -1 if none
 */
static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
{
	register struct nand_chip *this = mtd->priv;
	int ce_override = 0, i;
	ulong flags;

	/* Begin command latch cycle */
	this->hwcontrol(mtd, NAND_CTL_SETCLE);
	/*
	 * Write out the command to the device.
	 */
	if (command == NAND_CMD_SEQIN) {
		int readcmd;

		if (column >= mtd->oobblock) {
			/* OOB area */
			column -= mtd->oobblock;
			readcmd = NAND_CMD_READOOB;
		} else if (column < 256) {
			/* First 256 bytes --> READ0 */
			readcmd = NAND_CMD_READ0;
		} else {
			column -= 256;
			readcmd = NAND_CMD_READ1;
		}
		this->write_byte(mtd, readcmd);
	}
	this->write_byte(mtd, command);

	/* Set ALE and clear CLE to start address cycle */
	this->hwcontrol(mtd, NAND_CTL_CLRCLE);

	if (column != -1 || page_addr != -1) {
		this->hwcontrol(mtd, NAND_CTL_SETALE);

		/* Serially input address */
		if (column != -1) {
			/* Adjust columns for 16 bit buswidth */
			if (this->options & NAND_BUSWIDTH_16)
				column >>= 1;
			this->write_byte(mtd, column);
		}
		if (page_addr != -1) {
			this->write_byte(mtd, (u8)(page_addr & 0xff));

			if (command == NAND_CMD_READ0 ||
			    command == NAND_CMD_READ1 ||
			    command == NAND_CMD_READOOB) {
				/*
				 * NAND controller will release -CE after
				 * the last address byte is written, so we'll
				 * have to forcibly assert it. No interrupts
				 * are allowed while we do this as we don't
				 * want the NOR flash or PCMCIA drivers to
				 * steal our precious bytes of data...
				 */
				ce_override = 1;
				local_irq_save(flags);
				this->hwcontrol(mtd, NAND_CTL_SETNCE);
			}

			this->write_byte(mtd, (u8)(page_addr >> 8));

			/* One more address cycle for devices > 32MiB */
			if (this->chipsize > (32 << 20))
				this->write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
		}
		/* Latch in address */
		this->hwcontrol(mtd, NAND_CTL_CLRALE);
	}

	/*
	 * Program and erase have their own busy handlers.
	 * Status and sequential in need no delay.
	 */
	switch (command) {

	case NAND_CMD_PAGEPROG:
	case NAND_CMD_ERASE1:
	case NAND_CMD_ERASE2:
	case NAND_CMD_SEQIN:
	case NAND_CMD_STATUS:
		return;

	case NAND_CMD_RESET:
		break;

	case NAND_CMD_READ0:
	case NAND_CMD_READ1:
	case NAND_CMD_READOOB:
		/* Check if we're really driving -CE low (just in case) */
		if (unlikely(!ce_override))
			break;

		/* Apply a short delay always to ensure that we do wait tWB. */
		ndelay(100);
		/* Wait for a chip to become ready... */
		for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
			udelay(1);

		/* Release -CE and re-enable interrupts. */
		this->hwcontrol(mtd, NAND_CTL_CLRNCE);
		local_irq_restore(flags);
		return;
	}
	/* Apply this short delay always to ensure that we do wait tWB. */
	ndelay(100);

	while(!this->dev_ready(mtd));
}


L
Linus Torvalds 已提交
457 458 459
/*
 * Main initialization routine
 */
460
static int __init au1xxx_nand_init(void)
L
Linus Torvalds 已提交
461 462
{
	struct nand_chip *this;
463
	u16 boot_swapboot = 0;	/* default value */
L
Linus Torvalds 已提交
464
	int retval;
465 466
	u32 mem_staddr;
	u32 nand_phys;
L
Linus Torvalds 已提交
467 468

	/* Allocate memory for MTD device structure and private data */
469
	au1550_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
L
Linus Torvalds 已提交
470
	if (!au1550_mtd) {
471
		printk("Unable to allocate NAND MTD dev structure.\n");
L
Linus Torvalds 已提交
472 473 474 475
		return -ENOMEM;
	}

	/* Get pointer to private data */
476
	this = (struct nand_chip *)(&au1550_mtd[1]);
L
Linus Torvalds 已提交
477 478

	/* Initialize structures */
479 480
	memset(au1550_mtd, 0, sizeof(struct mtd_info));
	memset(this, 0, sizeof(struct nand_chip));
L
Linus Torvalds 已提交
481 482 483

	/* Link the private data with the MTD structure */
	au1550_mtd->priv = this;
484
	au1550_mtd->owner = THIS_MODULE;
L
Linus Torvalds 已提交
485

486

487 488
	/* MEM_STNDCTL: disable ints, disable nand boot */
	au_writel(0, MEM_STNDCTL);
L
Linus Torvalds 已提交
489 490 491

#ifdef CONFIG_MIPS_PB1550
	/* set gpio206 high */
492
	au_writel(au_readl(GPIO2_DIR) & ~(1 << 6), GPIO2_DIR);
L
Linus Torvalds 已提交
493

494
	boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr->status >> 6) & 0x1);
L
Linus Torvalds 已提交
495
	switch (boot_swapboot) {
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	case 0:
	case 2:
	case 8:
	case 0xC:
	case 0xD:
		/* x16 NAND Flash */
		nand_width = 0;
		break;
	case 1:
	case 9:
	case 3:
	case 0xE:
	case 0xF:
		/* x8 NAND Flash */
		nand_width = 1;
		break;
	default:
		printk("Pb1550 NAND: bad boot:swap\n");
		retval = -EINVAL;
		goto outmem;
L
Linus Torvalds 已提交
516 517 518
	}
#endif

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	/* Configure chip-select; normally done by boot code, e.g. YAMON */
#ifdef NAND_STCFG
	if (NAND_CS == 0) {
		au_writel(NAND_STCFG,  MEM_STCFG0);
		au_writel(NAND_STTIME, MEM_STTIME0);
		au_writel(NAND_STADDR, MEM_STADDR0);
	}
	if (NAND_CS == 1) {
		au_writel(NAND_STCFG,  MEM_STCFG1);
		au_writel(NAND_STTIME, MEM_STTIME1);
		au_writel(NAND_STADDR, MEM_STADDR1);
	}
	if (NAND_CS == 2) {
		au_writel(NAND_STCFG,  MEM_STCFG2);
		au_writel(NAND_STTIME, MEM_STTIME2);
		au_writel(NAND_STADDR, MEM_STADDR2);
	}
	if (NAND_CS == 3) {
		au_writel(NAND_STCFG,  MEM_STCFG3);
		au_writel(NAND_STTIME, MEM_STTIME3);
		au_writel(NAND_STADDR, MEM_STADDR3);
	}
#endif
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	/* Locate NAND chip-select in order to determine NAND phys address */
	mem_staddr = 0x00000000;
	if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
		mem_staddr = au_readl(MEM_STADDR0);
	else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
		mem_staddr = au_readl(MEM_STADDR1);
	else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
		mem_staddr = au_readl(MEM_STADDR2);
	else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
		mem_staddr = au_readl(MEM_STADDR3);

	if (mem_staddr == 0x00000000) {
		printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
		kfree(au1550_mtd);
		return 1;
	}
	nand_phys = (mem_staddr << 4) & 0xFFFC0000;
L
Linus Torvalds 已提交
560

561 562 563 564
	p_nand = (void __iomem *)ioremap(nand_phys, 0x1000);

	/* make controller and MTD agree */
	if (NAND_CS == 0)
565
		nand_width = au_readl(MEM_STCFG0) & (1 << 22);
566
	if (NAND_CS == 1)
567
		nand_width = au_readl(MEM_STCFG1) & (1 << 22);
568
	if (NAND_CS == 2)
569
		nand_width = au_readl(MEM_STCFG2) & (1 << 22);
570
	if (NAND_CS == 3)
571
		nand_width = au_readl(MEM_STCFG3) & (1 << 22);
L
Linus Torvalds 已提交
572 573 574 575

	/* Set address of hardware control function */
	this->hwcontrol = au1550_hwcontrol;
	this->dev_ready = au1550_device_ready;
576 577 578
	this->select_chip = au1550_select_chip;
	this->cmdfunc = au1550_command;

L
Linus Torvalds 已提交
579
	/* 30 us command delay time */
580
	this->chip_delay = 30;
L
Linus Torvalds 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	this->eccmode = NAND_ECC_SOFT;

	this->options = NAND_NO_AUTOINCR;

	if (!nand_width)
		this->options |= NAND_BUSWIDTH_16;

	this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
	this->write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
	this->write_word = au_write_word;
	this->read_word = au_read_word;
	this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
	this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
	this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;

	/* Scan to find existence of the device */
597
	if (nand_scan(au1550_mtd, 1)) {
L
Linus Torvalds 已提交
598 599 600 601 602
		retval = -ENXIO;
		goto outio;
	}

	/* Register the partitions */
603
	add_mtd_partitions(au1550_mtd, partition_info, ARRAY_SIZE(partition_info));
L
Linus Torvalds 已提交
604 605 606 607

	return 0;

 outio:
608
	iounmap((void *)p_nand);
609

L
Linus Torvalds 已提交
610
 outmem:
611
	kfree(au1550_mtd);
L
Linus Torvalds 已提交
612 613 614
	return retval;
}

615
module_init(au1xxx_nand_init);
L
Linus Torvalds 已提交
616 617 618 619

/*
 * Clean up routine
 */
620
static void __exit au1550_cleanup(void)
L
Linus Torvalds 已提交
621
{
622
	struct nand_chip *this = (struct nand_chip *)&au1550_mtd[1];
L
Linus Torvalds 已提交
623 624

	/* Release resources, unregister device */
625
	nand_release(au1550_mtd);
L
Linus Torvalds 已提交
626 627

	/* Free the MTD device structure */
628
	kfree(au1550_mtd);
L
Linus Torvalds 已提交
629 630

	/* Unmap */
631
	iounmap((void *)p_nand);
L
Linus Torvalds 已提交
632
}
633

L
Linus Torvalds 已提交
634 635 636 637 638
module_exit(au1550_cleanup);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Embedded Edge, LLC");
MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");