1 /*======================================================================
3 $Id: doc1000.c,v 1.17 2003/01/24 13:33:20 dwmw2 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);
140 static inline int suspend_erase(volatile u_char *addr)
145 writew(IF_ERASE_SUSPEND, addr);
146 writew(IF_READ_CSR, addr);
149 status = readw(addr);
150 if ((status & CSR_WR_READY) == CSR_WR_READY)
153 } while(i < max_tries);
155 printk(KERN_NOTICE "flashcard: suspend_erase timed out, status 0x%x\n", status);
160 static inline void resume_erase(volatile u_char *addr)
164 writew(IF_READ_CSR, addr);
165 status = readw(addr);
167 /* Only give resume signal if the erase is really suspended */
168 if (status & CSR_ERA_SUSPEND)
169 writew(IF_CONFIRM, addr);
172 static inline int byte_write (volatile u_char *addr, u_char byte)
174 register u_char status;
175 register u_short i = 0;
178 status = readb(addr);
179 if (status & CSR_WR_READY)
181 writeb(IF_WRITE & 0xff, addr);
186 } while(i < max_tries);
189 printk(KERN_NOTICE "flashcard: byte_write timed out, status 0x%x\n",status);
193 static inline int word_write (volatile u_char *addr, __u16 word)
195 register u_short status;
196 register u_short i = 0;
199 status = readw(addr);
200 if ((status & CSR_WR_READY) == CSR_WR_READY)
202 writew(IF_WRITE, addr);
207 } while(i < max_tries);
209 printk(KERN_NOTICE "flashcard: word_write timed out at %p, status 0x%x\n", addr, status);
213 static inline void reset_block(volatile u_char *addr)
218 writew(IF_CLEAR_CSR, addr);
220 for (i = 0; i < 100; i++) {
221 writew(IF_READ_CSR, addr);
222 status = readw(addr);
223 if (status != 0xffff) break;
227 writew(IF_READ_CSR, addr);
230 static inline int check_write(volatile u_char *addr)
232 u_short status, i = 0;
234 writew(IF_READ_CSR, addr);
237 status = readw(addr);
238 if (status & (CSR_WR_ERR | CSR_VPP_LOW))
240 printk(KERN_NOTICE "flashcard: write failure at %p, status 0x%x\n", addr, status);
244 if ((status & CSR_WR_READY) == CSR_WR_READY)
247 } while (i < max_tries);
249 printk(KERN_NOTICE "flashcard: write timed out at %p, status 0x%x\n", addr, status);
254 int flashcard_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
256 u_char *pageaddr=pagein(mtd,from);
257 struct mypriv *priv=mtd->priv;
258 u_char device = from >> priv->devshift;
259 u_char cell = (int) (from - (device << priv->devshift)) / mtd->erasesize;
260 int ret = 0, timeron = 0;
262 if ((from & WINDOW_MASK) + len <= WINDOW_SIZE)
265 *retlen = WINDOW_SIZE - (from & WINDOW_MASK);
267 if (priv->devstat[device])
270 /* There is an erase in progress or pending for this device. Stop it */
271 timeron = del_timer(&flashcard_timer);
273 if (priv->cur_erases && priv->cur_erases->cell == cell)
276 /* The erase is on the current cell. Just return all 0xff */
277 add_timer(&flashcard_timer);
280 printk("Cell %d currently erasing. Setting to all 0xff\n",cell);
281 memset(buf, 0xff, *retlen);
284 if (priv->devstat[device] == MTD_ERASING)
286 ret = suspend_erase(pageaddr);
287 priv->devstat[device] = MTD_ERASE_SUSPEND;
291 printk("flashcard: failed to suspend erase\n");
292 add_timer (&flashcard_timer);
299 writew(IF_READ_ARRAY, (u_long)pageaddr & ~1);
302 memcpy (buf, pageaddr, *retlen);
304 writew(IF_READ_CSR, (u_long)pageaddr & ~1);
307 if (priv->devstat[device] & MTD_ERASE_SUSPEND)
309 resume_erase(pageaddr);
310 priv->devstat[device]=MTD_ERASING;
314 if (timeron) add_timer (&flashcard_timer);
320 int flashcard_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
322 struct mypriv *priv = (struct mypriv *)mtd->priv;
323 u_char *endaddr, *startaddr;
324 register u_char *pageaddr;
325 u_char device = to >> priv->devshift;
326 /* jiffies_t oldj=jiffies;*/
329 while (priv->devstat[device])
334 if ((to & WINDOW_MASK) + len <= WINDOW_SIZE)
337 *retlen = WINDOW_SIZE - (to & WINDOW_MASK);
339 pageaddr = pagein(mtd, to);
340 startaddr = (u_char *)((u_long) pageaddr & ~1);
341 endaddr = pageaddr+(*retlen);
346 writew(IF_READ_CSR, startaddr);
348 /* Make sure it's aligned by reading the first byte if necessary */
351 /* Unaligned access */
357 if (!((u_long)pageaddr & 0xf))
360 ret = byte_write(pageaddr, cbuf);
367 for ( ; pageaddr + 1 < endaddr; buf += 2, pageaddr += 2)
369 /* if ((u_long)pageaddr & 0xf) schedule();*/
371 ret = word_write(pageaddr, *(__u16 *)buf);
376 if (pageaddr != endaddr)
378 /* One more byte to write at the end. */
383 ret = byte_write(pageaddr, cbuf);
388 return check_write(startaddr);
389 /* printk("Time taken in flashcard_write: %lx jiffies\n",jiffies - oldj);*/
395 /*====================================================================*/
397 static inline void block_erase (volatile u_char *addr)
399 writew(IF_BLOCK_ERASE, addr);
400 writew(IF_CONFIRM, addr);
404 static inline int check_erase(volatile u_char *addr)
408 /* writew(IF_READ_CSR, addr);*/
409 status = readw(addr);
412 if ((status & CSR_WR_READY) != CSR_WR_READY)
415 if (status & (CSR_ERA_ERR | CSR_VPP_LOW | CSR_WR_ERR))
417 printk(KERN_NOTICE "flashcard: erase failed, status 0x%x\n",
425 /*====================================================================*/
429 static void flashcard_periodic(unsigned long data)
431 register struct mtd_info *mtd = (struct mtd_info *)data;
432 register struct mypriv *priv = mtd->priv;
433 struct erase_info *erase = priv->cur_erases;
436 del_timer (&flashcard_timer);
441 pageaddr = pagein(mtd, erase->addr);
443 if (erase->state == MTD_ERASE_PENDING)
445 block_erase(pageaddr);
446 priv->devstat[erase->dev] = erase->state = MTD_ERASING;
447 erase->time = jiffies;
450 else if (erase->state == MTD_ERASING)
452 /* It's trying to erase. Check whether it's finished */
454 int ret = check_erase(pageaddr);
458 /* It's finished OK */
459 priv->devstat[erase->dev] = 0;
460 priv->cur_erases = erase->next;
461 erase->state = MTD_ERASE_DONE;
463 (*(erase->callback))(erase);
467 else if (ret == -EIO)
469 if (++erase->retries > retry_limit)
471 printk("Failed too many times. Giving up\n");
472 priv->cur_erases = erase->next;
473 priv->devstat[erase->dev] = 0;
474 erase->state = MTD_ERASE_FAILED;
476 (*(erase->callback))(erase);
481 priv->devstat[erase->dev] = erase->state = MTD_ERASE_PENDING;
483 else if (time_after(jiffies, erase->time + erase_timeout))
485 printk("Flash erase timed out. The world is broken.\n");
487 /* Just ignore and hope it goes away. For a while, read ops will give the CSR
488 and writes won't work. */
490 priv->cur_erases = erase->next;
491 priv->devstat[erase->dev] = 0;
492 erase->state = MTD_ERASE_FAILED;
494 (*(erase->callback))(erase);
500 if (priv->cur_erases)
502 flashcard_timer.expires = jiffies + HZ;
503 add_timer (&flashcard_timer);
506 wake_up_interruptible(&priv->wq);
510 int __init init_doc1000(void)
516 printk(KERN_NOTICE "flashcard: No start address for memory device.\n");
520 mymtd = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
524 printk(KERN_NOTICE "physmem: Cannot allocate memory for new MTD device.\n");
528 memset(mymtd,0,sizeof(struct mtd_info));
530 mymtd->priv = (void *) kmalloc (sizeof(struct mypriv), GFP_KERNEL);
534 printk(KERN_NOTICE "physmem: Cannot allocate memory for new MTD device's private data.\n");
542 init_waitqueue_head(&priv->wq);
544 memset (priv,0,sizeof(struct mypriv));
546 priv->baseaddr = phys_to_virt(base);
547 priv->numdevices = 4;
549 mymtd->name = "M-Systems DiskOnChip 1000";
551 mymtd->size = 0x100000;
552 mymtd->flags = MTD_CLEAR_BITS | MTD_ERASEABLE;
553 mymtd->erase = flashcard_erase;
555 mymtd->unpoint = NULL;
556 mymtd->read = flashcard_read;
557 mymtd->write = flashcard_write;
559 mymtd->sync = flashcard_sync;
560 mymtd->erasesize = 0x10000;
561 // mymtd->interleave = 2;
563 mymtd->type = MTD_NORFLASH;
565 if (add_mtd_device(mymtd))
567 printk(KERN_NOTICE "MTD device registration failed!\n");
573 init_timer(&flashcard_timer);
574 flashcard_timer.function = flashcard_periodic;
575 flashcard_timer.data = (u_long)mymtd;
579 static void __init cleanup_doc1000(void)
582 del_mtd_device(mymtd);
586 module_init(init_doc1000);
587 module_exit(cleanup_doc1000);
589 MODULE_LICENSE("GPL");
590 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
591 MODULE_DESCRIPTION("MTD driver for DiskOnChip 1000");