au1550nd.c 12.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 *  drivers/mtd/nand/au1550nd.c
 *
 *  Copyright (C) 2004 Embedded Edge, LLC
 *
 * 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>
13
#include <linux/gpio.h>
L
Linus Torvalds 已提交
14
#include <linux/module.h>
15
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
16 17 18
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/partitions.h>
19
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
20
#include <asm/io.h>
21 22
#include <asm/mach-au1x00/au1000.h>
#include <asm/mach-au1x00/au1550nd.h>
L
Linus Torvalds 已提交
23 24


25 26 27
struct au1550nd_ctx {
	struct mtd_info info;
	struct nand_chip chip;
L
Linus Torvalds 已提交
28

29 30 31
	int cs;
	void __iomem *base;
	void (*write_byte)(struct mtd_info *, u_char);
L
Linus Torvalds 已提交
32 33 34 35 36 37
};

/**
 * au_read_byte -  read one byte from the chip
 * @mtd:	MTD device structure
 *
38
 * read function for 8bit buswidth
L
Linus Torvalds 已提交
39 40 41 42 43
 */
static u_char au_read_byte(struct mtd_info *mtd)
{
	struct nand_chip *this = mtd->priv;
	u_char ret = readb(this->IO_ADDR_R);
44
	wmb(); /* drain writebuffer */
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52
	return ret;
}

/**
 * au_write_byte -  write one byte to the chip
 * @mtd:	MTD device structure
 * @byte:	pointer to data byte to write
 *
53
 * write function for 8it buswidth
L
Linus Torvalds 已提交
54 55 56 57 58
 */
static void au_write_byte(struct mtd_info *mtd, u_char byte)
{
	struct nand_chip *this = mtd->priv;
	writeb(byte, this->IO_ADDR_W);
59
	wmb(); /* drain writebuffer */
L
Linus Torvalds 已提交
60 61 62
}

/**
63
 * au_read_byte16 -  read one byte endianness aware from the chip
L
Linus Torvalds 已提交
64 65
 * @mtd:	MTD device structure
 *
66
 * read function for 16bit buswidth with endianness conversion
L
Linus Torvalds 已提交
67 68 69 70 71
 */
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));
72
	wmb(); /* drain writebuffer */
L
Linus Torvalds 已提交
73 74 75 76
	return ret;
}

/**
77
 * au_write_byte16 -  write one byte endianness aware to the chip
L
Linus Torvalds 已提交
78 79 80
 * @mtd:	MTD device structure
 * @byte:	pointer to data byte to write
 *
81
 * write function for 16bit buswidth with endianness conversion
L
Linus Torvalds 已提交
82 83 84 85 86
 */
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);
87
	wmb(); /* drain writebuffer */
L
Linus Torvalds 已提交
88 89 90 91 92 93
}

/**
 * au_read_word -  read one word from the chip
 * @mtd:	MTD device structure
 *
94
 * read function for 16bit buswidth without endianness conversion
L
Linus Torvalds 已提交
95 96 97 98 99
 */
static u16 au_read_word(struct mtd_info *mtd)
{
	struct nand_chip *this = mtd->priv;
	u16 ret = readw(this->IO_ADDR_R);
100
	wmb(); /* drain writebuffer */
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109
	return ret;
}

/**
 * au_write_buf -  write buffer to chip
 * @mtd:	MTD device structure
 * @buf:	data buffer
 * @len:	number of bytes to write
 *
110
 * write function for 8bit buswidth
L
Linus Torvalds 已提交
111 112 113 114 115 116
 */
static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
{
	int i;
	struct nand_chip *this = mtd->priv;

117
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
118
		writeb(buf[i], this->IO_ADDR_W);
119
		wmb(); /* drain writebuffer */
L
Linus Torvalds 已提交
120 121 122 123
	}
}

/**
124
 * au_read_buf -  read chip data into buffer
L
Linus Torvalds 已提交
125 126 127 128
 * @mtd:	MTD device structure
 * @buf:	buffer to store date
 * @len:	number of bytes to read
 *
129
 * read function for 8bit buswidth
L
Linus Torvalds 已提交
130 131 132 133 134 135
 */
static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
{
	int i;
	struct nand_chip *this = mtd->priv;

136
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
137
		buf[i] = readb(this->IO_ADDR_R);
138
		wmb(); /* drain writebuffer */
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147
	}
}

/**
 * au_write_buf16 -  write buffer to chip
 * @mtd:	MTD device structure
 * @buf:	data buffer
 * @len:	number of bytes to write
 *
148
 * write function for 16bit buswidth
L
Linus Torvalds 已提交
149 150 151 152 153 154 155
 */
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;
156

157
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
158
		writew(p[i], this->IO_ADDR_W);
159
		wmb(); /* drain writebuffer */
L
Linus Torvalds 已提交
160
	}
161

L
Linus Torvalds 已提交
162 163 164
}

/**
165
 * au_read_buf16 -  read chip data into buffer
L
Linus Torvalds 已提交
166 167 168 169
 * @mtd:	MTD device structure
 * @buf:	buffer to store date
 * @len:	number of bytes to read
 *
170
 * read function for 16bit buswidth
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178
 */
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;

179
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
180
		p[i] = readw(this->IO_ADDR_R);
181
		wmb(); /* drain writebuffer */
L
Linus Torvalds 已提交
182 183 184
	}
}

185 186 187 188 189 190 191 192 193 194 195 196
/* Select the chip by setting nCE to low */
#define NAND_CTL_SETNCE		1
/* Deselect the chip by setting nCE to high */
#define NAND_CTL_CLRNCE		2
/* Select the command latch by setting CLE to high */
#define NAND_CTL_SETCLE		3
/* Deselect the command latch by setting CLE to low */
#define NAND_CTL_CLRCLE		4
/* Select the address latch by setting ALE to high */
#define NAND_CTL_SETALE		5
/* Deselect the address latch by setting ALE to low */
#define NAND_CTL_CLRALE		6
L
Linus Torvalds 已提交
197 198 199

static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
{
200 201
	struct au1550nd_ctx *ctx = container_of(mtd, struct au1550nd_ctx, info);
	struct nand_chip *this = mtd->priv;
L
Linus Torvalds 已提交
202

203 204 205
	switch (cmd) {

	case NAND_CTL_SETCLE:
206
		this->IO_ADDR_W = ctx->base + MEM_STNAND_CMD;
207 208 209
		break;

	case NAND_CTL_CLRCLE:
210
		this->IO_ADDR_W = ctx->base + MEM_STNAND_DATA;
211
		break;
L
Linus Torvalds 已提交
212

213
	case NAND_CTL_SETALE:
214
		this->IO_ADDR_W = ctx->base + MEM_STNAND_ADDR;
215
		break;
L
Linus Torvalds 已提交
216

217
	case NAND_CTL_CLRALE:
218
		this->IO_ADDR_W = ctx->base + MEM_STNAND_DATA;
219
		/* FIXME: Nobody knows why this is necessary,
L
Linus Torvalds 已提交
220
		 * but it works only that way */
221
		udelay(1);
L
Linus Torvalds 已提交
222 223
		break;

224
	case NAND_CTL_SETNCE:
L
Linus Torvalds 已提交
225
		/* assert (force assert) chip enable */
226
		alchemy_wrsmem((1 << (4 + ctx->cs)), AU1000_MEM_STNDCTL);
L
Linus Torvalds 已提交
227 228
		break;

229
	case NAND_CTL_CLRNCE:
230
		/* deassert chip enable */
231
		alchemy_wrsmem(0, AU1000_MEM_STNDCTL);
L
Linus Torvalds 已提交
232 233 234 235
		break;
	}

	this->IO_ADDR_R = this->IO_ADDR_W;
236

237
	wmb(); /* Drain the writebuffer */
L
Linus Torvalds 已提交
238 239 240 241
}

int au1550_device_ready(struct mtd_info *mtd)
{
242
	return (alchemy_rdsmem(AU1000_MEM_STSTAT) & 0x1) ? 1 : 0;
L
Linus Torvalds 已提交
243 244
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/**
 * 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)
{
270 271
	struct au1550nd_ctx *ctx = container_of(mtd, struct au1550nd_ctx, info);
	struct nand_chip *this = mtd->priv;
272
	int ce_override = 0, i;
273
	unsigned long flags = 0;
274 275

	/* Begin command latch cycle */
276
	au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
277 278 279 280 281 282
	/*
	 * Write out the command to the device.
	 */
	if (command == NAND_CMD_SEQIN) {
		int readcmd;

J
Joern Engel 已提交
283
		if (column >= mtd->writesize) {
284
			/* OOB area */
J
Joern Engel 已提交
285
			column -= mtd->writesize;
286 287 288 289 290 291 292 293
			readcmd = NAND_CMD_READOOB;
		} else if (column < 256) {
			/* First 256 bytes --> READ0 */
			readcmd = NAND_CMD_READ0;
		} else {
			column -= 256;
			readcmd = NAND_CMD_READ1;
		}
294
		ctx->write_byte(mtd, readcmd);
295
	}
296
	ctx->write_byte(mtd, command);
297 298

	/* Set ALE and clear CLE to start address cycle */
299
	au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
300 301

	if (column != -1 || page_addr != -1) {
302
		au1550_hwcontrol(mtd, NAND_CTL_SETALE);
303 304 305 306

		/* Serially input address */
		if (column != -1) {
			/* Adjust columns for 16 bit buswidth */
307 308
			if (this->options & NAND_BUSWIDTH_16 &&
					!nand_opcode_8bits(command))
309
				column >>= 1;
310
			ctx->write_byte(mtd, column);
311 312
		}
		if (page_addr != -1) {
313
			ctx->write_byte(mtd, (u8)(page_addr & 0xff));
314 315 316 317 318 319 320 321 322 323 324 325 326 327

			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);
328
				au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
329 330
			}

331
			ctx->write_byte(mtd, (u8)(page_addr >> 8));
332 333 334

			/* One more address cycle for devices > 32MiB */
			if (this->chipsize > (32 << 20))
335 336
				ctx->write_byte(mtd,
						((page_addr >> 16) & 0x0f));
337 338
		}
		/* Latch in address */
339
		au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
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
	}

	/*
	 * 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. */
372
		au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
373 374 375 376 377 378 379 380 381
		local_irq_restore(flags);
		return;
	}
	/* Apply this short delay always to ensure that we do wait tWB. */
	ndelay(100);

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

B
Bill Pemberton 已提交
382
static int find_nand_cs(unsigned long nand_base)
L
Linus Torvalds 已提交
383
{
384 385 386 387
	void __iomem *base =
			(void __iomem *)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR);
	unsigned long addr, staddr, start, mask, end;
	int i;
388

389 390 391 392 393 394 395 396 397 398
	for (i = 0; i < 4; i++) {
		addr = 0x1000 + (i * 0x10);			/* CSx */
		staddr = __raw_readl(base + addr + 0x08);	/* STADDRx */
		/* figure out the decoded range of this CS */
		start = (staddr << 4) & 0xfffc0000;
		mask = (staddr << 18) & 0xfffc0000;
		end = (start | (start - 1)) & ~(start ^ mask);
		if ((nand_base >= start) && (nand_base < end))
			return i;
	}
L
Linus Torvalds 已提交
399

400 401
	return -ENODEV;
}
L
Linus Torvalds 已提交
402

B
Bill Pemberton 已提交
403
static int au1550nd_probe(struct platform_device *pdev)
404 405 406 407 408 409
{
	struct au1550nd_platdata *pd;
	struct au1550nd_ctx *ctx;
	struct nand_chip *this;
	struct resource *r;
	int ret, cs;
410

J
Jingoo Han 已提交
411
	pd = dev_get_platdata(&pdev->dev);
412 413 414
	if (!pd) {
		dev_err(&pdev->dev, "missing platform data\n");
		return -ENODEV;
L
Linus Torvalds 已提交
415
	}
416 417

	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
418
	if (!ctx)
419 420 421 422 423 424 425
		return -ENOMEM;

	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!r) {
		dev_err(&pdev->dev, "no NAND memory resource\n");
		ret = -ENODEV;
		goto out1;
426
	}
427 428 429 430
	if (request_mem_region(r->start, resource_size(r), "au1550-nand")) {
		dev_err(&pdev->dev, "cannot claim NAND memory area\n");
		ret = -ENOMEM;
		goto out1;
431
	}
432 433 434 435 436 437

	ctx->base = ioremap_nocache(r->start, 0x1000);
	if (!ctx->base) {
		dev_err(&pdev->dev, "cannot remap NAND memory area\n");
		ret = -ENODEV;
		goto out2;
438
	}
L
Linus Torvalds 已提交
439

440 441 442
	this = &ctx->chip;
	ctx->info.priv = this;
	ctx->info.owner = THIS_MODULE;
443

444 445 446 447 448 449 450 451
	/* figure out which CS# r->start belongs to */
	cs = find_nand_cs(r->start);
	if (cs < 0) {
		dev_err(&pdev->dev, "cannot detect NAND chipselect\n");
		ret = -ENODEV;
		goto out3;
	}
	ctx->cs = cs;
L
Linus Torvalds 已提交
452 453

	this->dev_ready = au1550_device_ready;
454 455 456
	this->select_chip = au1550_select_chip;
	this->cmdfunc = au1550_command;

L
Linus Torvalds 已提交
457
	/* 30 us command delay time */
458
	this->chip_delay = 30;
T
Thomas Gleixner 已提交
459
	this->ecc.mode = NAND_ECC_SOFT;
L
Linus Torvalds 已提交
460

461
	if (pd->devwidth)
L
Linus Torvalds 已提交
462 463
		this->options |= NAND_BUSWIDTH_16;

464 465
	this->read_byte = (pd->devwidth) ? au_read_byte16 : au_read_byte;
	ctx->write_byte = (pd->devwidth) ? au_write_byte16 : au_write_byte;
L
Linus Torvalds 已提交
466
	this->read_word = au_read_word;
467 468 469 470 471 472 473
	this->write_buf = (pd->devwidth) ? au_write_buf16 : au_write_buf;
	this->read_buf = (pd->devwidth) ? au_read_buf16 : au_read_buf;

	ret = nand_scan(&ctx->info, 1);
	if (ret) {
		dev_err(&pdev->dev, "NAND scan failed with %d\n", ret);
		goto out3;
L
Linus Torvalds 已提交
474 475
	}

476
	mtd_device_register(&ctx->info, pd->parts, pd->num_parts);
L
Linus Torvalds 已提交
477

478 479
	platform_set_drvdata(pdev, ctx);

L
Linus Torvalds 已提交
480 481
	return 0;

482 483 484 485 486 487 488
out3:
	iounmap(ctx->base);
out2:
	release_mem_region(r->start, resource_size(r));
out1:
	kfree(ctx);
	return ret;
L
Linus Torvalds 已提交
489 490
}

B
Bill Pemberton 已提交
491
static int au1550nd_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
492
{
493 494
	struct au1550nd_ctx *ctx = platform_get_drvdata(pdev);
	struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
L
Linus Torvalds 已提交
495

496 497 498 499 500
	nand_release(&ctx->info);
	iounmap(ctx->base);
	release_mem_region(r->start, 0x1000);
	kfree(ctx);
	return 0;
L
Linus Torvalds 已提交
501
}
502

503 504 505 506 507 508
static struct platform_driver au1550nd_driver = {
	.driver = {
		.name	= "au1550-nand",
		.owner	= THIS_MODULE,
	},
	.probe		= au1550nd_probe,
B
Bill Pemberton 已提交
509
	.remove		= au1550nd_remove,
510 511 512
};

module_platform_driver(au1550nd_driver);
L
Linus Torvalds 已提交
513 514 515 516

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