1 /*======================================================================
3 $Id: doc1000.c,v 1.1.1.1 2005/04/11 02:50:26 jack Exp $
5 ======================================================================*/
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <asm/uaccess.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/ptrace.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/timer.h>
18 #include <linux/major.h>
20 #include <linux/ioctl.h>
22 #include <asm/system.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/iflash.h>
29 /* Parameters that can be set with 'insmod' */
31 static u_long base = 0xe0000;
32 static int erase_timeout = 10*HZ; /* in ticks */
33 static int retry_limit = 4; /* write retries */
34 static u_long max_tries = 4096; /* status polling */
36 MODULE_PARM(base,"l");
37 MODULE_PARM(erase_timeout, "i");
38 MODULE_PARM(retry_limit, "i");
39 MODULE_PARM(max_tries, "i");
41 #define WINDOW_SIZE 0x2000
42 #define WINDOW_MASK (WINDOW_SIZE - 1)
43 #define PAGEREG_LO (WINDOW_SIZE)
44 #define PAGEREG_HI (WINDOW_SIZE + 2)
46 static struct mtd_info *mymtd;
47 static struct timer_list flashcard_timer;
50 #define MAX_FLASH_DEVICES 8
52 /* A flash region is composed of one or more "cells", where we allow
53 simultaneous erases if they are in different cells */
63 struct erase_info *cur_erases;
65 u_char devstat[MAX_FLASH_DEVICES];
70 static void flashcard_periodic(u_long data);
71 static int flashcard_erase (struct mtd_info *mtd, struct erase_info *instr);
72 static int flashcard_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
73 static int flashcard_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
74 static void flashcard_sync (struct mtd_info *mtd);
76 static inline void resume_erase(volatile u_char *addr);
77 static inline int suspend_erase(volatile u_char *addr);
78 static inline int byte_write (volatile u_char *addr, u_char byte);
79 static inline int word_write (volatile u_char *addr, __u16 word);
80 static inline int check_write(volatile u_char *addr);
81 static inline void block_erase (volatile u_char *addr);
82 static inline int check_erase(volatile u_char *addr);
85 #warning This is definitely not SMP safe. Lock the paging mechanism.
88 static u_char *pagein(struct mtd_info *mtd, u_long addr)
90 struct mypriv *priv=mtd->priv;
91 u_short page = addr >> 13;
93 priv->baseaddr[PAGEREG_LO] = page & 0xff;
94 priv->baseaddr[PAGEREG_HI] = page >> 8;
97 return &priv->baseaddr[addr & WINDOW_MASK];
101 void flashcard_sync (struct mtd_info *mtd)
103 struct mypriv *priv=mtd->priv;
105 flashcard_periodic((u_long) mtd);
107 if (priv->cur_erases)
108 interruptible_sleep_on(&priv->wq);
112 int flashcard_erase (struct mtd_info *mtd, struct erase_info *instr)
115 struct mypriv *priv=mtd->priv;
116 struct erase_info **tmp=&priv->cur_erases;
118 if (instr->len != mtd->erasesize)
120 if (instr->addr + instr->len > mtd->size)
123 pageaddr=pagein(mtd,instr->addr);
125 instr->dev = instr->addr >> priv->devshift;
126 instr->cell = (instr->addr - (instr->dev << priv->devshift)) / mtd->erasesize;
128 instr->state = MTD_ERASE_PENDING;
132 tmp = &((*tmp) -> next);
136 flashcard_periodic((u_long)mtd);
141 int flashcard_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
143 u_char *pageaddr=pagein(mtd,from);
144 struct mypriv *priv=mtd->priv;
145 u_char device = from >> priv->devshift;
146 u_char cell = (int) (from - (device << priv->devshift)) / mtd->erasesize;
147 int ret = 0, timeron = 0;
149 if ((from & WINDOW_MASK) + len <= WINDOW_SIZE)
152 *retlen = WINDOW_SIZE - (from & WINDOW_MASK);
154 if (priv->devstat[device])
157 /* There is an erase in progress or pending for this device. Stop it */
158 timeron = del_timer(&flashcard_timer);
160 if (priv->cur_erases && priv->cur_erases->cell == cell)
163 /* The erase is on the current cell. Just return all 0xff */
164 add_timer(&flashcard_timer);
167 printk("Cell %d currently erasing. Setting to all 0xff\n",cell);
168 memset(buf, 0xff, *retlen);
171 if (priv->devstat[device] == MTD_ERASING)
173 ret = suspend_erase(pageaddr);
174 priv->devstat[device] = MTD_ERASE_SUSPEND;
178 printk("flashcard: failed to suspend erase\n");
179 add_timer (&flashcard_timer);
186 writew(IF_READ_ARRAY, (u_long)pageaddr & ~1);
189 memcpy (buf, pageaddr, *retlen);
191 writew(IF_READ_CSR, (u_long)pageaddr & ~1);
194 if (priv->devstat[device] & MTD_ERASE_SUSPEND)
196 resume_erase(pageaddr);
197 priv->devstat[device]=MTD_ERASING;
201 if (timeron) add_timer (&flashcard_timer);
207 int flashcard_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
209 struct mypriv *priv = (struct mypriv *)mtd->priv;
210 u_char *endaddr, *startaddr;
211 register u_char *pageaddr;
212 u_char device = to >> priv->devshift;
213 /* jiffies_t oldj=jiffies;*/
216 while (priv->devstat[device])
221 if ((to & WINDOW_MASK) + len <= WINDOW_SIZE)
224 *retlen = WINDOW_SIZE - (to & WINDOW_MASK);
226 pageaddr = pagein(mtd, to);
227 startaddr = (u_char *)((u_long) pageaddr & ~1);
228 endaddr = pageaddr+(*retlen);
233 writew(IF_READ_CSR, startaddr);
235 /* Make sure it's aligned by reading the first byte if necessary */
238 /* Unaligned access */
244 if (!((u_long)pageaddr & 0xf))
247 ret = byte_write(pageaddr, cbuf);
254 for ( ; pageaddr + 1 < endaddr; buf += 2, pageaddr += 2)
256 /* if ((u_long)pageaddr & 0xf) schedule();*/
258 ret = word_write(pageaddr, *(__u16 *)buf);
263 if (pageaddr != endaddr)
265 /* One more byte to write at the end. */
270 ret = byte_write(pageaddr, cbuf);
275 return check_write(startaddr);
276 /* printk("Time taken in flashcard_write: %lx jiffies\n",jiffies - oldj);*/
282 /*====================================================================*/
284 static inline int byte_write (volatile u_char *addr, u_char byte)
286 register u_char status;
287 register u_short i = 0;
290 status = readb(addr);
291 if (status & CSR_WR_READY)
293 writeb(IF_WRITE & 0xff, addr);
298 } while(i < max_tries);
301 printk(KERN_NOTICE "flashcard: byte_write timed out, status 0x%x\n",status);
305 static inline int word_write (volatile u_char *addr, __u16 word)
307 register u_short status;
308 register u_short i = 0;
311 status = readw(addr);
312 if ((status & CSR_WR_READY) == CSR_WR_READY)
314 writew(IF_WRITE, addr);
319 } while(i < max_tries);
321 printk(KERN_NOTICE "flashcard: word_write timed out at %p, status 0x%x\n", addr, status);
325 static inline void block_erase (volatile u_char *addr)
327 writew(IF_BLOCK_ERASE, addr);
328 writew(IF_CONFIRM, addr);
332 static inline int check_erase(volatile u_char *addr)
336 /* writew(IF_READ_CSR, addr);*/
337 status = readw(addr);
340 if ((status & CSR_WR_READY) != CSR_WR_READY)
343 if (status & (CSR_ERA_ERR | CSR_VPP_LOW | CSR_WR_ERR))
345 printk(KERN_NOTICE "flashcard: erase failed, status 0x%x\n",
353 static inline int suspend_erase(volatile u_char *addr)
358 writew(IF_ERASE_SUSPEND, addr);
359 writew(IF_READ_CSR, addr);
362 status = readw(addr);
363 if ((status & CSR_WR_READY) == CSR_WR_READY)
366 } while(i < max_tries);
368 printk(KERN_NOTICE "flashcard: suspend_erase timed out, status 0x%x\n", status);
373 static inline void resume_erase(volatile u_char *addr)
377 writew(IF_READ_CSR, addr);
378 status = readw(addr);
380 /* Only give resume signal if the erase is really suspended */
381 if (status & CSR_ERA_SUSPEND)
382 writew(IF_CONFIRM, addr);
385 static inline void reset_block(volatile u_char *addr)
390 writew(IF_CLEAR_CSR, addr);
392 for (i = 0; i < 100; i++) {
393 writew(IF_READ_CSR, addr);
394 status = readw(addr);
395 if (status != 0xffff) break;
399 writew(IF_READ_CSR, addr);
402 static inline int check_write(volatile u_char *addr)
404 u_short status, i = 0;
406 writew(IF_READ_CSR, addr);
409 status = readw(addr);
410 if (status & (CSR_WR_ERR | CSR_VPP_LOW))
412 printk(KERN_NOTICE "flashcard: write failure at %p, status 0x%x\n", addr, status);
416 if ((status & CSR_WR_READY) == CSR_WR_READY)
419 } while (i < max_tries);
421 printk(KERN_NOTICE "flashcard: write timed out at %p, status 0x%x\n", addr, status);
426 /*====================================================================*/
430 static void flashcard_periodic(unsigned long data)
432 register struct mtd_info *mtd = (struct mtd_info *)data;
433 register struct mypriv *priv = mtd->priv;
434 struct erase_info *erase = priv->cur_erases;
437 del_timer (&flashcard_timer);
442 pageaddr = pagein(mtd, erase->addr);
444 if (erase->state == MTD_ERASE_PENDING)
446 block_erase(pageaddr);
447 priv->devstat[erase->dev] = erase->state = MTD_ERASING;
448 erase->time = jiffies;
451 else if (erase->state == MTD_ERASING)
453 /* It's trying to erase. Check whether it's finished */
455 int ret = check_erase(pageaddr);
459 /* It's finished OK */
460 priv->devstat[erase->dev] = 0;
461 priv->cur_erases = erase->next;
462 erase->state = MTD_ERASE_DONE;
464 (*(erase->callback))(erase);
468 else if (ret == -EIO)
470 if (++erase->retries > retry_limit)
472 printk("Failed too many times. Giving up\n");
473 priv->cur_erases = erase->next;
474 priv->devstat[erase->dev] = 0;
475 erase->state = MTD_ERASE_FAILED;
477 (*(erase->callback))(erase);
482 priv->devstat[erase->dev] = erase->state = MTD_ERASE_PENDING;
484 else if (time_after(jiffies, erase->time + erase_timeout))
486 printk("Flash erase timed out. The world is broken.\n");
488 /* Just ignore and hope it goes away. For a while, read ops will give the CSR
489 and writes won't work. */
491 priv->cur_erases = erase->next;
492 priv->devstat[erase->dev] = 0;
493 erase->state = MTD_ERASE_FAILED;
495 (*(erase->callback))(erase);
501 if (priv->cur_erases)
503 flashcard_timer.expires = jiffies + HZ;
504 add_timer (&flashcard_timer);
507 wake_up_interruptible(&priv->wq);
511 int __init init_doc1000(void)
517 printk(KERN_NOTICE "flashcard: No start address for memory device.\n");
521 mymtd = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
525 printk(KERN_NOTICE "physmem: Cannot allocate memory for new MTD device.\n");
529 memset(mymtd,0,sizeof(struct mtd_info));
531 mymtd->priv = (void *) kmalloc (sizeof(struct mypriv), GFP_KERNEL);
535 printk(KERN_NOTICE "physmem: Cannot allocate memory for new MTD device's private data.\n");
543 init_waitqueue_head(&priv->wq);
545 memset (priv,0,sizeof(struct mypriv));
547 priv->baseaddr = phys_to_virt(base);
548 priv->numdevices = 4;
550 mymtd->name = "M-Systems DiskOnChip 1000";
552 mymtd->size = 0x100000;
553 mymtd->flags = MTD_CLEAR_BITS | MTD_ERASEABLE;
554 mymtd->erase = flashcard_erase;
556 mymtd->unpoint = NULL;
557 mymtd->read = flashcard_read;
558 mymtd->write = flashcard_write;
560 mymtd->sync = flashcard_sync;
561 mymtd->erasesize = 0x10000;
562 // mymtd->interleave = 2;
564 mymtd->type = MTD_NORFLASH;
566 if (add_mtd_device(mymtd))
568 printk(KERN_NOTICE "MTD device registration failed!\n");
574 init_timer(&flashcard_timer);
575 flashcard_timer.function = flashcard_periodic;
576 flashcard_timer.data = (u_long)mymtd;
580 static void __init cleanup_doc1000(void)
583 del_mtd_device(mymtd);
587 module_init(init_doc1000);
588 module_exit(cleanup_doc1000);
590 MODULE_LICENSE("GPL");
591 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
592 MODULE_DESCRIPTION("MTD driver for DiskOnChip 1000");