This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / mtd / nand / tx4925ndfmc.c
1 /*
2  *  drivers/mtd/tx4925ndfmc.c
3  *
4  *  Overview:
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.
8  *
9  * Author: MontaVista Software, Inc.  source@mvista.com
10  *
11  * Derived from drivers/mtd/autcpu12.c
12  *       Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
13  *
14  * $Id: tx4925ndfmc.c,v 1.2 2004/03/27 19:55:53 gleixner Exp $
15  *
16  * Copyright (C) 2001 Toshiba Corporation 
17  * 
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
21  * or implied.
22  *
23  */
24
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>
32 #include <asm/io.h>
33 #include <asm/tx4925/tx4925_nand.h>
34
35 extern struct nand_oobinfo jffs2_oobinfo;
36
37 /*
38  * MTD structure for RBTX4925 board
39  */
40 static struct mtd_info *tx4925ndfmc_mtd = NULL;
41
42 /*
43  * Module stuff
44  */
45 #if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
46 #define tx4925ndfmc_init init_module
47 #define tx4925ndfmc_cleanup cleanup_module
48 #endif
49
50 /*
51  * Define partitions for flash devices
52  */
53
54 static struct mtd_partition partition_info16k[] = {
55         { .name = "RBTX4925 flash partition 1",
56           .offset =  0,
57           .size =    8 * 0x00100000 },
58         { .name = "RBTX4925 flash partition 2",
59           .offset =  8 * 0x00100000,
60           .size =    8 * 0x00100000 },
61 };
62
63 static struct mtd_partition partition_info32k[] = {
64         { .name = "RBTX4925 flash partition 1",
65           .offset =  0,
66           .size =    8 * 0x00100000 },
67         { .name = "RBTX4925 flash partition 2",
68           .offset = 8 * 0x00100000,
69           .size =  24 * 0x00100000 },
70 };
71
72 static struct mtd_partition partition_info64k[] = {
73         { .name = "User FS",
74           .offset =  0,
75           .size =   16 * 0x00100000 },
76         { .name = "RBTX4925 flash partition 2",
77           .offset = 16 * 0x00100000,
78           .size =   48 * 0x00100000},
79 };
80
81 static struct mtd_partition partition_info128k[] = {
82         { .name = "Skip bad section",
83           .offset =  0,
84           .size =   16 * 0x00100000 },
85         { .name = "User FS",
86           .offset = 16 * 0x00100000,
87           .size =   112 * 0x00100000 },
88 };
89 #define NUM_PARTITIONS16K  2
90 #define NUM_PARTITIONS32K  2
91 #define NUM_PARTITIONS64K  2
92 #define NUM_PARTITIONS128K 2
93
94 /* 
95  *      hardware specific access to control-lines
96 */
97 static void tx4925ndfmc_hwcontrol(struct mtd_info *mtd, int cmd)
98 {
99
100         switch(cmd){
101
102                 case NAND_CTL_SETCLE: 
103                         tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_CLE;
104                         break;
105                 case NAND_CTL_CLRCLE:
106                         tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_CLE;
107                         break;
108                 case NAND_CTL_SETALE:
109                         tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_ALE;
110                         break;
111                 case NAND_CTL_CLRALE: 
112                         tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ALE;
113                         break;
114                 case NAND_CTL_SETNCE:
115                         tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_CE;
116                         break;
117                 case NAND_CTL_CLRNCE:
118                         tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_CE;
119                         break;
120                 case NAND_CTL_SETWP:
121                         tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_WE;
122                         break;
123                 case NAND_CTL_CLRWP:
124                         tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_WE;
125                         break;
126         }
127 }
128
129 /*
130 *       read device ready pin
131 */
132 static int tx4925ndfmc_device_ready(struct mtd_info *mtd)
133 {
134         int ready;
135         ready = (tx4925_ndfmcptr->sr & TX4925_NDSFR_BUSY) ? 0 : 1;
136         return ready;
137 }
138 void tx4925ndfmc_enable_hwecc(struct mtd_info *mtd, int mode)
139 {
140         /* reset first */
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;
144 }
145 static void tx4925ndfmc_disable_ecc(void)
146 {
147         tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ECC_CNTL_MASK;
148 }
149 static void tx4925ndfmc_enable_read_ecc(void)
150 {
151         tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ECC_CNTL_MASK;
152         tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_ECC_CNTL_READ;
153 }
154 void tx4925ndfmc_readecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code){
155         int i;
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();
161 }
162 void tx4925ndfmc_device_setup(void)
163 {
164
165         *(unsigned char *)0xbb005000 &= ~0x08;
166
167         /* reset NDFMC */
168         tx4925_ndfmcptr->rstr |= TX4925_NDFRSTR_RST;
169         while (tx4925_ndfmcptr->rstr & TX4925_NDFRSTR_RST);       
170
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;             
174 }
175 static u_char tx4925ndfmc_nand_read_byte(struct mtd_info *mtd)
176 {
177         struct nand_chip *this = mtd->priv;
178         return tx4925_read_nfmc(this->IO_ADDR_R);
179 }
180
181 static void tx4925ndfmc_nand_write_byte(struct mtd_info *mtd, u_char byte)
182 {
183         struct nand_chip *this = mtd->priv;
184         tx4925_write_nfmc(byte, this->IO_ADDR_W);
185 }
186
187 static void tx4925ndfmc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
188 {
189         int i;
190         struct nand_chip *this = mtd->priv;
191
192         for (i=0; i<len; i++)
193                 tx4925_write_nfmc(buf[i], this->IO_ADDR_W);
194 }
195
196 static void tx4925ndfmc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
197 {
198         int i;
199         struct nand_chip *this = mtd->priv;
200
201         for (i=0; i<len; i++)
202                 buf[i] = tx4925_read_nfmc(this->IO_ADDR_R);
203 }
204
205 static int tx4925ndfmc_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
206 {
207         int i;
208         struct nand_chip *this = mtd->priv;
209
210         for (i=0; i<len; i++)
211                 if (buf[i] != tx4925_read_nfmc(this->IO_ADDR_R))
212                         return -EFAULT;
213
214         return 0;
215 }
216
217 /*
218  * Send command to NAND device
219  */
220 static void tx4925ndfmc_nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
221 {
222         register struct nand_chip *this = mtd->priv;
223
224         /* Begin command latch cycle */
225         this->hwcontrol(mtd, NAND_CTL_SETCLE);
226         /*
227          * Write out the command to the device.
228          */
229         if (command == NAND_CMD_SEQIN) {
230                 int readcmd;
231
232                 if (column >= mtd->oobblock) {
233                         /* OOB area */
234                         column -= mtd->oobblock;
235                         readcmd = NAND_CMD_READOOB;
236                 } else if (column < 256) {
237                         /* First 256 bytes --> READ0 */
238                         readcmd = NAND_CMD_READ0;
239                 } else {
240                         column -= 256;
241                         readcmd = NAND_CMD_READ1;
242                 }
243                 this->write_byte(mtd, readcmd);
244         }
245         this->write_byte(mtd, command);
246
247         /* Set ALE and clear CLE to start address cycle */
248         this->hwcontrol(mtd, NAND_CTL_CLRCLE);
249
250         if (column != -1 || page_addr != -1) {
251                 this->hwcontrol(mtd, NAND_CTL_SETALE);
252
253                 /* Serially input address */
254                 if (column != -1)
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));
262                 }
263                 /* Latch in address */
264                 this->hwcontrol(mtd, NAND_CTL_CLRALE);
265         }
266         
267         /* 
268          * program and erase have their own busy handlers 
269          * status and sequential in needs no delay
270         */
271         switch (command) {
272                         
273         case NAND_CMD_PAGEPROG:
274                 /* Turn off WE */
275                 this->hwcontrol (mtd, NAND_CTL_CLRWP);
276                 return;
277
278         case NAND_CMD_SEQIN:
279                 /* Turn on WE */
280                 this->hwcontrol (mtd, NAND_CTL_SETWP);
281                 return;
282
283         case NAND_CMD_ERASE1:
284         case NAND_CMD_ERASE2:
285         case NAND_CMD_STATUS:
286                 return;
287
288         case NAND_CMD_RESET:
289                 if (this->dev_ready)    
290                         break;
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));
295                 return;
296
297         /* This applies to read commands */     
298         default:
299                 /* 
300                  * If we don't have access to the busy pin, we apply the given
301                  * command delay
302                 */
303                 if (!this->dev_ready) {
304                         udelay (this->chip_delay);
305                         return;
306                 }       
307         }
308         
309         /* wait until command is processed */
310         while (!this->dev_ready(mtd));
311 }
312
313 #ifdef CONFIG_MTD_CMDLINE_PARTS
314 extern int parse_cmdline_partitions(struct mtd_info *master, struct mtd_partitio
315 n **pparts, char *);
316 #endif
317
318 /*
319  * Main initialization routine
320  */
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)
323 {
324         struct nand_chip *this;
325         int err = 0;
326
327         /* Allocate memory for MTD device structure and private data */
328         tx4925ndfmc_mtd = kmalloc (sizeof(struct mtd_info) + sizeof (struct nand_chip),
329                                 GFP_KERNEL);
330         if (!tx4925ndfmc_mtd) {
331                 printk ("Unable to allocate RBTX4925 NAND MTD device structure.\n");
332                 err = -ENOMEM;
333                 goto out;
334         }
335
336         tx4925ndfmc_device_setup();
337
338         /* io is indirect via a register so don't need to ioremap address */
339
340         /* Get pointer to private data */
341         this = (struct nand_chip *) (&tx4925ndfmc_mtd[1]);
342
343         /* Initialize structures */
344         memset((char *) tx4925ndfmc_mtd, 0, sizeof(struct mtd_info));
345         memset((char *) this, 0, sizeof(struct nand_chip));
346
347         /* Link the private data with the MTD structure */
348         tx4925ndfmc_mtd->priv = this;
349
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;
367
368         /* Scan to find existance of the device */
369         if (nand_scan (tx4925ndfmc_mtd, 1)) {
370                 err = -ENXIO;
371                 goto out_ior;
372         }
373
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");
378                 err = -ENOMEM;
379                 goto out_ior;
380         }
381
382         /* Register the partitions */
383 #ifdef CONFIG_MTD_CMDLINE_PARTS
384         {
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);
390                 else
391                         add_mtd_device(tx4925ndfmc_mtd);
392         }
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; 
399                 default: {
400                         printk ("Unsupported SmartMedia device\n"); 
401                         err = -ENXIO;
402                         goto out_buf;
403                 }
404         }
405 #endif /* ifdef CONFIG_MTD_CMDLINE_PARTS */
406         goto out;
407
408 out_buf:
409         kfree (this->data_buf);    
410 out_ior:
411 out:
412         return err;
413 }
414
415 module_init(tx4925ndfmc_init);
416
417 /*
418  * Clean up routine
419  */
420 #ifdef MODULE
421 static void __exit tx4925ndfmc_cleanup (void)
422 {
423         struct nand_chip *this = (struct nand_chip *) &tx4925ndfmc_mtd[1];
424
425         /* Unregister partitions */
426         del_mtd_partitions(tx4925ndfmc_mtd);
427         
428         /* Unregister the device */
429         del_mtd_device (tx4925ndfmc_mtd);
430
431         /* Free internal data buffers */
432         kfree (this->data_buf);
433
434         /* Free the MTD device structure */
435         kfree (tx4925ndfmc_mtd);
436 }
437 module_exit(tx4925ndfmc_cleanup);
438 #endif
439
440 MODULE_LICENSE("GPL");
441 MODULE_AUTHOR("Alice Hennessy <ahennessy@mvista.com>");
442 MODULE_DESCRIPTION("Glue layer for SmartMediaCard on Toshiba RBTX4925");