2 * drivers/mtd/tx4925ndfmc.c
5 * This is a device driver for the NAND flash device found on the
6 * Toshiba RBTX4925 reference board, which is a SmartMediaCard. It supports
7 * 16MiB, 32MiB and 64MiB cards.
9 * Author: MontaVista Software, Inc. source@mvista.com
11 * Derived from drivers/mtd/autcpu12.c
12 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
14 * $Id: tx4925ndfmc.c,v 1.2 2004/03/27 19:55:53 gleixner Exp $
16 * Copyright (C) 2001 Toshiba Corporation
18 * 2003 (c) MontaVista Software, Inc. This file is licensed under
19 * the terms of the GNU General Public License version 2. This program
20 * is licensed "as is" without any warranty of any kind, whether express
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/nand.h>
30 #include <linux/mtd/partitions.h>
31 #include <linux/delay.h>
33 #include <asm/tx4925/tx4925_nand.h>
35 extern struct nand_oobinfo jffs2_oobinfo;
38 * MTD structure for RBTX4925 board
40 static struct mtd_info *tx4925ndfmc_mtd = NULL;
45 #if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
46 #define tx4925ndfmc_init init_module
47 #define tx4925ndfmc_cleanup cleanup_module
51 * Define partitions for flash devices
54 static struct mtd_partition partition_info16k[] = {
55 { .name = "RBTX4925 flash partition 1",
57 .size = 8 * 0x00100000 },
58 { .name = "RBTX4925 flash partition 2",
59 .offset = 8 * 0x00100000,
60 .size = 8 * 0x00100000 },
63 static struct mtd_partition partition_info32k[] = {
64 { .name = "RBTX4925 flash partition 1",
66 .size = 8 * 0x00100000 },
67 { .name = "RBTX4925 flash partition 2",
68 .offset = 8 * 0x00100000,
69 .size = 24 * 0x00100000 },
72 static struct mtd_partition partition_info64k[] = {
75 .size = 16 * 0x00100000 },
76 { .name = "RBTX4925 flash partition 2",
77 .offset = 16 * 0x00100000,
78 .size = 48 * 0x00100000},
81 static struct mtd_partition partition_info128k[] = {
82 { .name = "Skip bad section",
84 .size = 16 * 0x00100000 },
86 .offset = 16 * 0x00100000,
87 .size = 112 * 0x00100000 },
89 #define NUM_PARTITIONS16K 2
90 #define NUM_PARTITIONS32K 2
91 #define NUM_PARTITIONS64K 2
92 #define NUM_PARTITIONS128K 2
95 * hardware specific access to control-lines
97 static void tx4925ndfmc_hwcontrol(struct mtd_info *mtd, int cmd)
102 case NAND_CTL_SETCLE:
103 tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_CLE;
105 case NAND_CTL_CLRCLE:
106 tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_CLE;
108 case NAND_CTL_SETALE:
109 tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_ALE;
111 case NAND_CTL_CLRALE:
112 tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ALE;
114 case NAND_CTL_SETNCE:
115 tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_CE;
117 case NAND_CTL_CLRNCE:
118 tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_CE;
121 tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_WE;
124 tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_WE;
130 * read device ready pin
132 static int tx4925ndfmc_device_ready(struct mtd_info *mtd)
135 ready = (tx4925_ndfmcptr->sr & TX4925_NDSFR_BUSY) ? 0 : 1;
138 void tx4925ndfmc_enable_hwecc(struct mtd_info *mtd, int mode)
141 tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_ECC_CNTL_MASK;
142 tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ECC_CNTL_MASK;
143 tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_ECC_CNTL_ENAB;
145 static void tx4925ndfmc_disable_ecc(void)
147 tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ECC_CNTL_MASK;
149 static void tx4925ndfmc_enable_read_ecc(void)
151 tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ECC_CNTL_MASK;
152 tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_ECC_CNTL_READ;
154 void tx4925ndfmc_readecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code){
156 u_char *ecc = ecc_code;
157 tx4925ndfmc_enable_read_ecc();
158 for (i = 0;i < 6;i++,ecc++)
159 *ecc = tx4925_read_nfmc(&(tx4925_ndfmcptr->dtr));
160 tx4925ndfmc_disable_ecc();
162 void tx4925ndfmc_device_setup(void)
165 *(unsigned char *)0xbb005000 &= ~0x08;
168 tx4925_ndfmcptr->rstr |= TX4925_NDFRSTR_RST;
169 while (tx4925_ndfmcptr->rstr & TX4925_NDFRSTR_RST);
171 /* setup BusSeparete, Hold Time, Strobe Pulse Width */
172 tx4925_ndfmcptr->mcr = TX4925_BSPRT ? TX4925_NDFMCR_BSPRT : 0;
173 tx4925_ndfmcptr->spr = TX4925_HOLD << 4 | TX4925_SPW;
175 static u_char tx4925ndfmc_nand_read_byte(struct mtd_info *mtd)
177 struct nand_chip *this = mtd->priv;
178 return tx4925_read_nfmc(this->IO_ADDR_R);
181 static void tx4925ndfmc_nand_write_byte(struct mtd_info *mtd, u_char byte)
183 struct nand_chip *this = mtd->priv;
184 tx4925_write_nfmc(byte, this->IO_ADDR_W);
187 static void tx4925ndfmc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
190 struct nand_chip *this = mtd->priv;
192 for (i=0; i<len; i++)
193 tx4925_write_nfmc(buf[i], this->IO_ADDR_W);
196 static void tx4925ndfmc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
199 struct nand_chip *this = mtd->priv;
201 for (i=0; i<len; i++)
202 buf[i] = tx4925_read_nfmc(this->IO_ADDR_R);
205 static int tx4925ndfmc_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
208 struct nand_chip *this = mtd->priv;
210 for (i=0; i<len; i++)
211 if (buf[i] != tx4925_read_nfmc(this->IO_ADDR_R))
218 * Send command to NAND device
220 static void tx4925ndfmc_nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
222 register struct nand_chip *this = mtd->priv;
224 /* Begin command latch cycle */
225 this->hwcontrol(mtd, NAND_CTL_SETCLE);
227 * Write out the command to the device.
229 if (command == NAND_CMD_SEQIN) {
232 if (column >= mtd->oobblock) {
234 column -= mtd->oobblock;
235 readcmd = NAND_CMD_READOOB;
236 } else if (column < 256) {
237 /* First 256 bytes --> READ0 */
238 readcmd = NAND_CMD_READ0;
241 readcmd = NAND_CMD_READ1;
243 this->write_byte(mtd, readcmd);
245 this->write_byte(mtd, command);
247 /* Set ALE and clear CLE to start address cycle */
248 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
250 if (column != -1 || page_addr != -1) {
251 this->hwcontrol(mtd, NAND_CTL_SETALE);
253 /* Serially input address */
255 this->write_byte(mtd, column);
256 if (page_addr != -1) {
257 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
258 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
259 /* One more address cycle for higher density devices */
260 if (mtd->size & 0x0c000000)
261 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
263 /* Latch in address */
264 this->hwcontrol(mtd, NAND_CTL_CLRALE);
268 * program and erase have their own busy handlers
269 * status and sequential in needs no delay
273 case NAND_CMD_PAGEPROG:
275 this->hwcontrol (mtd, NAND_CTL_CLRWP);
280 this->hwcontrol (mtd, NAND_CTL_SETWP);
283 case NAND_CMD_ERASE1:
284 case NAND_CMD_ERASE2:
285 case NAND_CMD_STATUS:
291 this->hwcontrol(mtd, NAND_CTL_SETCLE);
292 this->write_byte(mtd, NAND_CMD_STATUS);
293 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
294 while ( !(this->read_byte(mtd) & 0x40));
297 /* This applies to read commands */
300 * If we don't have access to the busy pin, we apply the given
303 if (!this->dev_ready) {
304 udelay (this->chip_delay);
309 /* wait until command is processed */
310 while (!this->dev_ready(mtd));
313 #ifdef CONFIG_MTD_CMDLINE_PARTS
314 extern int parse_cmdline_partitions(struct mtd_info *master, struct mtd_partitio
319 * Main initialization routine
321 extern int nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc);
322 int __init tx4925ndfmc_init (void)
324 struct nand_chip *this;
327 /* Allocate memory for MTD device structure and private data */
328 tx4925ndfmc_mtd = kmalloc (sizeof(struct mtd_info) + sizeof (struct nand_chip),
330 if (!tx4925ndfmc_mtd) {
331 printk ("Unable to allocate RBTX4925 NAND MTD device structure.\n");
336 tx4925ndfmc_device_setup();
338 /* io is indirect via a register so don't need to ioremap address */
340 /* Get pointer to private data */
341 this = (struct nand_chip *) (&tx4925ndfmc_mtd[1]);
343 /* Initialize structures */
344 memset((char *) tx4925ndfmc_mtd, 0, sizeof(struct mtd_info));
345 memset((char *) this, 0, sizeof(struct nand_chip));
347 /* Link the private data with the MTD structure */
348 tx4925ndfmc_mtd->priv = this;
350 /* Set address of NAND IO lines */
351 this->IO_ADDR_R = (unsigned long)&(tx4925_ndfmcptr->dtr);
352 this->IO_ADDR_W = (unsigned long)&(tx4925_ndfmcptr->dtr);
353 this->hwcontrol = tx4925ndfmc_hwcontrol;
354 this->enable_hwecc = tx4925ndfmc_enable_hwecc;
355 this->calculate_ecc = tx4925ndfmc_readecc;
356 this->correct_data = nand_correct_data;
357 this->eccmode = NAND_ECC_HW6_512;
358 this->dev_ready = tx4925ndfmc_device_ready;
359 /* 20 us command delay time */
360 this->chip_delay = 20;
361 this->read_byte = tx4925ndfmc_nand_read_byte;
362 this->write_byte = tx4925ndfmc_nand_write_byte;
363 this->cmdfunc = tx4925ndfmc_nand_command;
364 this->write_buf = tx4925ndfmc_nand_write_buf;
365 this->read_buf = tx4925ndfmc_nand_read_buf;
366 this->verify_buf = tx4925ndfmc_nand_verify_buf;
368 /* Scan to find existance of the device */
369 if (nand_scan (tx4925ndfmc_mtd, 1)) {
374 /* Allocate memory for internal data buffer */
375 this->data_buf = kmalloc (sizeof(u_char) * (tx4925ndfmc_mtd->oobblock + tx4925ndfmc_mtd->oobsize), GFP_KERNEL);
376 if (!this->data_buf) {
377 printk ("Unable to allocate NAND data buffer for RBTX4925.\n");
382 /* Register the partitions */
383 #ifdef CONFIG_MTD_CMDLINE_PARTS
385 int mtd_parts_nb = 0;
386 struct mtd_partition *mtd_parts = 0;
387 mtd_parts_nb = parse_cmdline_partitions(tx4925ndfmc_mtd, &mtd_parts, "tx4925ndfmc");
388 if (mtd_parts_nb > 0)
389 add_mtd_partitions(tx4925ndfmc_mtd, mtd_parts, mtd_parts_nb);
391 add_mtd_device(tx4925ndfmc_mtd);
393 #else /* ifdef CONFIG_MTD_CMDLINE_PARTS */
394 switch(tx4925ndfmc_mtd->size){
395 case 0x01000000: add_mtd_partitions(tx4925ndfmc_mtd, partition_info16k, NUM_PARTITIONS16K); break;
396 case 0x02000000: add_mtd_partitions(tx4925ndfmc_mtd, partition_info32k, NUM_PARTITIONS32K); break;
397 case 0x04000000: add_mtd_partitions(tx4925ndfmc_mtd, partition_info64k, NUM_PARTITIONS64K); break;
398 case 0x08000000: add_mtd_partitions(tx4925ndfmc_mtd, partition_info128k, NUM_PARTITIONS128K); break;
400 printk ("Unsupported SmartMedia device\n");
405 #endif /* ifdef CONFIG_MTD_CMDLINE_PARTS */
409 kfree (this->data_buf);
415 module_init(tx4925ndfmc_init);
421 static void __exit tx4925ndfmc_cleanup (void)
423 struct nand_chip *this = (struct nand_chip *) &tx4925ndfmc_mtd[1];
425 /* Unregister partitions */
426 del_mtd_partitions(tx4925ndfmc_mtd);
428 /* Unregister the device */
429 del_mtd_device (tx4925ndfmc_mtd);
431 /* Free internal data buffers */
432 kfree (this->data_buf);
434 /* Free the MTD device structure */
435 kfree (tx4925ndfmc_mtd);
437 module_exit(tx4925ndfmc_cleanup);
440 MODULE_LICENSE("GPL");
441 MODULE_AUTHOR("Alice Hennessy <ahennessy@mvista.com>");
442 MODULE_DESCRIPTION("Glue layer for SmartMediaCard on Toshiba RBTX4925");