Linux 2.6.21-rc3
[linux/fpc-iii.git] / drivers / block / umem.c
blob5872036e8ae6b90f36b2c29789c826755f0d7903
1 /*
2 * mm.c - Micro Memory(tm) PCI memory board block device driver - v2.3
4 * (C) 2001 San Mehat <nettwerk@valinux.com>
5 * (C) 2001 Johannes Erdfelt <jerdfelt@valinux.com>
6 * (C) 2001 NeilBrown <neilb@cse.unsw.edu.au>
8 * This driver for the Micro Memory PCI Memory Module with Battery Backup
9 * is Copyright Micro Memory Inc 2001-2002. All rights reserved.
11 * This driver is released to the public under the terms of the
12 * GNU GENERAL PUBLIC LICENSE version 2
13 * See the file COPYING for details.
15 * This driver provides a standard block device interface for Micro Memory(tm)
16 * PCI based RAM boards.
17 * 10/05/01: Phap Nguyen - Rebuilt the driver
18 * 10/22/01: Phap Nguyen - v2.1 Added disk partitioning
19 * 29oct2001:NeilBrown - Use make_request_fn instead of request_fn
20 * - use stand disk partitioning (so fdisk works).
21 * 08nov2001:NeilBrown - change driver name from "mm" to "umem"
22 * - incorporate into main kernel
23 * 08apr2002:NeilBrown - Move some of interrupt handle to tasklet
24 * - use spin_lock_bh instead of _irq
25 * - Never block on make_request. queue
26 * bh's instead.
27 * - unregister umem from devfs at mod unload
28 * - Change version to 2.3
29 * 07Nov2001:Phap Nguyen - Select pci read command: 06, 12, 15 (Decimal)
30 * 07Jan2002: P. Nguyen - Used PCI Memory Write & Invalidate for DMA
31 * 15May2002:NeilBrown - convert to bio for 2.5
32 * 17May2002:NeilBrown - remove init_mem initialisation. Instead detect
33 * - a sequence of writes that cover the card, and
34 * - set initialised bit then.
37 //#define DEBUG /* uncomment if you want debugging info (pr_debug) */
38 #include <linux/fs.h>
39 #include <linux/bio.h>
40 #include <linux/kernel.h>
41 #include <linux/mm.h>
42 #include <linux/mman.h>
43 #include <linux/ioctl.h>
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/interrupt.h>
47 #include <linux/smp_lock.h>
48 #include <linux/timer.h>
49 #include <linux/pci.h>
50 #include <linux/slab.h>
51 #include <linux/dma-mapping.h>
53 #include <linux/fcntl.h> /* O_ACCMODE */
54 #include <linux/hdreg.h> /* HDIO_GETGEO */
56 #include <linux/umem.h>
58 #include <asm/uaccess.h>
59 #include <asm/io.h>
61 #define MM_MAXCARDS 4
62 #define MM_RAHEAD 2 /* two sectors */
63 #define MM_BLKSIZE 1024 /* 1k blocks */
64 #define MM_HARDSECT 512 /* 512-byte hardware sectors */
65 #define MM_SHIFT 6 /* max 64 partitions on 4 cards */
68 * Version Information
71 #define DRIVER_VERSION "v2.3"
72 #define DRIVER_AUTHOR "San Mehat, Johannes Erdfelt, NeilBrown"
73 #define DRIVER_DESC "Micro Memory(tm) PCI memory board block driver"
75 static int debug;
76 /* #define HW_TRACE(x) writeb(x,cards[0].csr_remap + MEMCTRLSTATUS_MAGIC) */
77 #define HW_TRACE(x)
79 #define DEBUG_LED_ON_TRANSFER 0x01
80 #define DEBUG_BATTERY_POLLING 0x02
82 module_param(debug, int, 0644);
83 MODULE_PARM_DESC(debug, "Debug bitmask");
85 static int pci_read_cmd = 0x0C; /* Read Multiple */
86 module_param(pci_read_cmd, int, 0);
87 MODULE_PARM_DESC(pci_read_cmd, "PCI read command");
89 static int pci_write_cmd = 0x0F; /* Write and Invalidate */
90 module_param(pci_write_cmd, int, 0);
91 MODULE_PARM_DESC(pci_write_cmd, "PCI write command");
93 static int pci_cmds;
95 static int major_nr;
97 #include <linux/blkdev.h>
98 #include <linux/blkpg.h>
100 struct cardinfo {
101 int card_number;
102 struct pci_dev *dev;
104 int irq;
106 unsigned long csr_base;
107 unsigned char __iomem *csr_remap;
108 unsigned long csr_len;
109 #ifdef CONFIG_MM_MAP_MEMORY
110 unsigned long mem_base;
111 unsigned char __iomem *mem_remap;
112 unsigned long mem_len;
113 #endif
115 unsigned int win_size; /* PCI window size */
116 unsigned int mm_size; /* size in kbytes */
118 unsigned int init_size; /* initial segment, in sectors,
119 * that we know to
120 * have been written
122 struct bio *bio, *currentbio, **biotail;
124 request_queue_t *queue;
126 struct mm_page {
127 dma_addr_t page_dma;
128 struct mm_dma_desc *desc;
129 int cnt, headcnt;
130 struct bio *bio, **biotail;
131 } mm_pages[2];
132 #define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc))
134 int Active, Ready;
136 struct tasklet_struct tasklet;
137 unsigned int dma_status;
139 struct {
140 int good;
141 int warned;
142 unsigned long last_change;
143 } battery[2];
145 spinlock_t lock;
146 int check_batteries;
148 int flags;
151 static struct cardinfo cards[MM_MAXCARDS];
152 static struct block_device_operations mm_fops;
153 static struct timer_list battery_timer;
155 static int num_cards = 0;
157 static struct gendisk *mm_gendisk[MM_MAXCARDS];
159 static void check_batteries(struct cardinfo *card);
162 -----------------------------------------------------------------------------------
163 -- get_userbit
164 -----------------------------------------------------------------------------------
166 static int get_userbit(struct cardinfo *card, int bit)
168 unsigned char led;
170 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
171 return led & bit;
174 -----------------------------------------------------------------------------------
175 -- set_userbit
176 -----------------------------------------------------------------------------------
178 static int set_userbit(struct cardinfo *card, int bit, unsigned char state)
180 unsigned char led;
182 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
183 if (state)
184 led |= bit;
185 else
186 led &= ~bit;
187 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
189 return 0;
192 -----------------------------------------------------------------------------------
193 -- set_led
194 -----------------------------------------------------------------------------------
197 * NOTE: For the power LED, use the LED_POWER_* macros since they differ
199 static void set_led(struct cardinfo *card, int shift, unsigned char state)
201 unsigned char led;
203 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
204 if (state == LED_FLIP)
205 led ^= (1<<shift);
206 else {
207 led &= ~(0x03 << shift);
208 led |= (state << shift);
210 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
214 #ifdef MM_DIAG
216 -----------------------------------------------------------------------------------
217 -- dump_regs
218 -----------------------------------------------------------------------------------
220 static void dump_regs(struct cardinfo *card)
222 unsigned char *p;
223 int i, i1;
225 p = card->csr_remap;
226 for (i = 0; i < 8; i++) {
227 printk(KERN_DEBUG "%p ", p);
229 for (i1 = 0; i1 < 16; i1++)
230 printk("%02x ", *p++);
232 printk("\n");
235 #endif
237 -----------------------------------------------------------------------------------
238 -- dump_dmastat
239 -----------------------------------------------------------------------------------
241 static void dump_dmastat(struct cardinfo *card, unsigned int dmastat)
243 printk(KERN_DEBUG "MM%d*: DMAstat - ", card->card_number);
244 if (dmastat & DMASCR_ANY_ERR)
245 printk("ANY_ERR ");
246 if (dmastat & DMASCR_MBE_ERR)
247 printk("MBE_ERR ");
248 if (dmastat & DMASCR_PARITY_ERR_REP)
249 printk("PARITY_ERR_REP ");
250 if (dmastat & DMASCR_PARITY_ERR_DET)
251 printk("PARITY_ERR_DET ");
252 if (dmastat & DMASCR_SYSTEM_ERR_SIG)
253 printk("SYSTEM_ERR_SIG ");
254 if (dmastat & DMASCR_TARGET_ABT)
255 printk("TARGET_ABT ");
256 if (dmastat & DMASCR_MASTER_ABT)
257 printk("MASTER_ABT ");
258 if (dmastat & DMASCR_CHAIN_COMPLETE)
259 printk("CHAIN_COMPLETE ");
260 if (dmastat & DMASCR_DMA_COMPLETE)
261 printk("DMA_COMPLETE ");
262 printk("\n");
266 * Theory of request handling
268 * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME
269 * We have two pages of mm_dma_desc, holding about 64 descriptors
270 * each. These are allocated at init time.
271 * One page is "Ready" and is either full, or can have request added.
272 * The other page might be "Active", which DMA is happening on it.
274 * Whenever IO on the active page completes, the Ready page is activated
275 * and the ex-Active page is clean out and made Ready.
276 * Otherwise the Ready page is only activated when it becomes full, or
277 * when mm_unplug_device is called via the unplug_io_fn.
279 * If a request arrives while both pages a full, it is queued, and b_rdev is
280 * overloaded to record whether it was a read or a write.
282 * The interrupt handler only polls the device to clear the interrupt.
283 * The processing of the result is done in a tasklet.
286 static void mm_start_io(struct cardinfo *card)
288 /* we have the lock, we know there is
289 * no IO active, and we know that card->Active
290 * is set
292 struct mm_dma_desc *desc;
293 struct mm_page *page;
294 int offset;
296 /* make the last descriptor end the chain */
297 page = &card->mm_pages[card->Active];
298 pr_debug("start_io: %d %d->%d\n", card->Active, page->headcnt, page->cnt-1);
299 desc = &page->desc[page->cnt-1];
301 desc->control_bits |= cpu_to_le32(DMASCR_CHAIN_COMP_EN);
302 desc->control_bits &= ~cpu_to_le32(DMASCR_CHAIN_EN);
303 desc->sem_control_bits = desc->control_bits;
306 if (debug & DEBUG_LED_ON_TRANSFER)
307 set_led(card, LED_REMOVE, LED_ON);
309 desc = &page->desc[page->headcnt];
310 writel(0, card->csr_remap + DMA_PCI_ADDR);
311 writel(0, card->csr_remap + DMA_PCI_ADDR + 4);
313 writel(0, card->csr_remap + DMA_LOCAL_ADDR);
314 writel(0, card->csr_remap + DMA_LOCAL_ADDR + 4);
316 writel(0, card->csr_remap + DMA_TRANSFER_SIZE);
317 writel(0, card->csr_remap + DMA_TRANSFER_SIZE + 4);
319 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR);
320 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR + 4);
322 offset = ((char*)desc) - ((char*)page->desc);
323 writel(cpu_to_le32((page->page_dma+offset)&0xffffffff),
324 card->csr_remap + DMA_DESCRIPTOR_ADDR);
325 /* Force the value to u64 before shifting otherwise >> 32 is undefined C
326 * and on some ports will do nothing ! */
327 writel(cpu_to_le32(((u64)page->page_dma)>>32),
328 card->csr_remap + DMA_DESCRIPTOR_ADDR + 4);
330 /* Go, go, go */
331 writel(cpu_to_le32(DMASCR_GO | DMASCR_CHAIN_EN | pci_cmds),
332 card->csr_remap + DMA_STATUS_CTRL);
335 static int add_bio(struct cardinfo *card);
337 static void activate(struct cardinfo *card)
339 /* if No page is Active, and Ready is
340 * not empty, then switch Ready page
341 * to active and start IO.
342 * Then add any bh's that are available to Ready
345 do {
346 while (add_bio(card))
349 if (card->Active == -1 &&
350 card->mm_pages[card->Ready].cnt > 0) {
351 card->Active = card->Ready;
352 card->Ready = 1-card->Ready;
353 mm_start_io(card);
356 } while (card->Active == -1 && add_bio(card));
359 static inline void reset_page(struct mm_page *page)
361 page->cnt = 0;
362 page->headcnt = 0;
363 page->bio = NULL;
364 page->biotail = & page->bio;
367 static void mm_unplug_device(request_queue_t *q)
369 struct cardinfo *card = q->queuedata;
370 unsigned long flags;
372 spin_lock_irqsave(&card->lock, flags);
373 if (blk_remove_plug(q))
374 activate(card);
375 spin_unlock_irqrestore(&card->lock, flags);
379 * If there is room on Ready page, take
380 * one bh off list and add it.
381 * return 1 if there was room, else 0.
383 static int add_bio(struct cardinfo *card)
385 struct mm_page *p;
386 struct mm_dma_desc *desc;
387 dma_addr_t dma_handle;
388 int offset;
389 struct bio *bio;
390 int rw;
391 int len;
393 bio = card->currentbio;
394 if (!bio && card->bio) {
395 card->currentbio = card->bio;
396 card->bio = card->bio->bi_next;
397 if (card->bio == NULL)
398 card->biotail = &card->bio;
399 card->currentbio->bi_next = NULL;
400 return 1;
402 if (!bio)
403 return 0;
405 rw = bio_rw(bio);
406 if (card->mm_pages[card->Ready].cnt >= DESC_PER_PAGE)
407 return 0;
409 len = bio_iovec(bio)->bv_len;
410 dma_handle = pci_map_page(card->dev,
411 bio_page(bio),
412 bio_offset(bio),
413 len,
414 (rw==READ) ?
415 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
417 p = &card->mm_pages[card->Ready];
418 desc = &p->desc[p->cnt];
419 p->cnt++;
420 if ((p->biotail) != &bio->bi_next) {
421 *(p->biotail) = bio;
422 p->biotail = &(bio->bi_next);
423 bio->bi_next = NULL;
426 desc->data_dma_handle = dma_handle;
428 desc->pci_addr = cpu_to_le64((u64)desc->data_dma_handle);
429 desc->local_addr= cpu_to_le64(bio->bi_sector << 9);
430 desc->transfer_size = cpu_to_le32(len);
431 offset = ( ((char*)&desc->sem_control_bits) - ((char*)p->desc));
432 desc->sem_addr = cpu_to_le64((u64)(p->page_dma+offset));
433 desc->zero1 = desc->zero2 = 0;
434 offset = ( ((char*)(desc+1)) - ((char*)p->desc));
435 desc->next_desc_addr = cpu_to_le64(p->page_dma+offset);
436 desc->control_bits = cpu_to_le32(DMASCR_GO|DMASCR_ERR_INT_EN|
437 DMASCR_PARITY_INT_EN|
438 DMASCR_CHAIN_EN |
439 DMASCR_SEM_EN |
440 pci_cmds);
441 if (rw == WRITE)
442 desc->control_bits |= cpu_to_le32(DMASCR_TRANSFER_READ);
443 desc->sem_control_bits = desc->control_bits;
445 bio->bi_sector += (len>>9);
446 bio->bi_size -= len;
447 bio->bi_idx++;
448 if (bio->bi_idx >= bio->bi_vcnt)
449 card->currentbio = NULL;
451 return 1;
454 static void process_page(unsigned long data)
456 /* check if any of the requests in the page are DMA_COMPLETE,
457 * and deal with them appropriately.
458 * If we find a descriptor without DMA_COMPLETE in the semaphore, then
459 * dma must have hit an error on that descriptor, so use dma_status instead
460 * and assume that all following descriptors must be re-tried.
462 struct mm_page *page;
463 struct bio *return_bio=NULL;
464 struct cardinfo *card = (struct cardinfo *)data;
465 unsigned int dma_status = card->dma_status;
467 spin_lock_bh(&card->lock);
468 if (card->Active < 0)
469 goto out_unlock;
470 page = &card->mm_pages[card->Active];
472 while (page->headcnt < page->cnt) {
473 struct bio *bio = page->bio;
474 struct mm_dma_desc *desc = &page->desc[page->headcnt];
475 int control = le32_to_cpu(desc->sem_control_bits);
476 int last=0;
477 int idx;
479 if (!(control & DMASCR_DMA_COMPLETE)) {
480 control = dma_status;
481 last=1;
483 page->headcnt++;
484 idx = bio->bi_phys_segments;
485 bio->bi_phys_segments++;
486 if (bio->bi_phys_segments >= bio->bi_vcnt)
487 page->bio = bio->bi_next;
489 pci_unmap_page(card->dev, desc->data_dma_handle,
490 bio_iovec_idx(bio,idx)->bv_len,
491 (control& DMASCR_TRANSFER_READ) ?
492 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
493 if (control & DMASCR_HARD_ERROR) {
494 /* error */
495 clear_bit(BIO_UPTODATE, &bio->bi_flags);
496 printk(KERN_WARNING "MM%d: I/O error on sector %d/%d\n",
497 card->card_number,
498 le32_to_cpu(desc->local_addr)>>9,
499 le32_to_cpu(desc->transfer_size));
500 dump_dmastat(card, control);
501 } else if (test_bit(BIO_RW, &bio->bi_rw) &&
502 le32_to_cpu(desc->local_addr)>>9 == card->init_size) {
503 card->init_size += le32_to_cpu(desc->transfer_size)>>9;
504 if (card->init_size>>1 >= card->mm_size) {
505 printk(KERN_INFO "MM%d: memory now initialised\n",
506 card->card_number);
507 set_userbit(card, MEMORY_INITIALIZED, 1);
510 if (bio != page->bio) {
511 bio->bi_next = return_bio;
512 return_bio = bio;
515 if (last) break;
518 if (debug & DEBUG_LED_ON_TRANSFER)
519 set_led(card, LED_REMOVE, LED_OFF);
521 if (card->check_batteries) {
522 card->check_batteries = 0;
523 check_batteries(card);
525 if (page->headcnt >= page->cnt) {
526 reset_page(page);
527 card->Active = -1;
528 activate(card);
529 } else {
530 /* haven't finished with this one yet */
531 pr_debug("do some more\n");
532 mm_start_io(card);
534 out_unlock:
535 spin_unlock_bh(&card->lock);
537 while(return_bio) {
538 struct bio *bio = return_bio;
540 return_bio = bio->bi_next;
541 bio->bi_next = NULL;
542 bio_endio(bio, bio->bi_size, 0);
547 -----------------------------------------------------------------------------------
548 -- mm_make_request
549 -----------------------------------------------------------------------------------
551 static int mm_make_request(request_queue_t *q, struct bio *bio)
553 struct cardinfo *card = q->queuedata;
554 pr_debug("mm_make_request %llu %u\n",
555 (unsigned long long)bio->bi_sector, bio->bi_size);
557 bio->bi_phys_segments = bio->bi_idx; /* count of completed segments*/
558 spin_lock_irq(&card->lock);
559 *card->biotail = bio;
560 bio->bi_next = NULL;
561 card->biotail = &bio->bi_next;
562 blk_plug_device(q);
563 spin_unlock_irq(&card->lock);
565 return 0;
569 -----------------------------------------------------------------------------------
570 -- mm_interrupt
571 -----------------------------------------------------------------------------------
573 static irqreturn_t mm_interrupt(int irq, void *__card)
575 struct cardinfo *card = (struct cardinfo *) __card;
576 unsigned int dma_status;
577 unsigned short cfg_status;
579 HW_TRACE(0x30);
581 dma_status = le32_to_cpu(readl(card->csr_remap + DMA_STATUS_CTRL));
583 if (!(dma_status & (DMASCR_ERROR_MASK | DMASCR_CHAIN_COMPLETE))) {
584 /* interrupt wasn't for me ... */
585 return IRQ_NONE;
588 /* clear COMPLETION interrupts */
589 if (card->flags & UM_FLAG_NO_BYTE_STATUS)
590 writel(cpu_to_le32(DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE),
591 card->csr_remap+ DMA_STATUS_CTRL);
592 else
593 writeb((DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE) >> 16,
594 card->csr_remap+ DMA_STATUS_CTRL + 2);
596 /* log errors and clear interrupt status */
597 if (dma_status & DMASCR_ANY_ERR) {
598 unsigned int data_log1, data_log2;
599 unsigned int addr_log1, addr_log2;
600 unsigned char stat, count, syndrome, check;
602 stat = readb(card->csr_remap + MEMCTRLCMD_ERRSTATUS);
604 data_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG));
605 data_log2 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG + 4));
606 addr_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_ADDR_LOG));
607 addr_log2 = readb(card->csr_remap + ERROR_ADDR_LOG + 4);
609 count = readb(card->csr_remap + ERROR_COUNT);
610 syndrome = readb(card->csr_remap + ERROR_SYNDROME);
611 check = readb(card->csr_remap + ERROR_CHECK);
613 dump_dmastat(card, dma_status);
615 if (stat & 0x01)
616 printk(KERN_ERR "MM%d*: Memory access error detected (err count %d)\n",
617 card->card_number, count);
618 if (stat & 0x02)
619 printk(KERN_ERR "MM%d*: Multi-bit EDC error\n",
620 card->card_number);
622 printk(KERN_ERR "MM%d*: Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
623 card->card_number, addr_log2, addr_log1, data_log2, data_log1);
624 printk(KERN_ERR "MM%d*: Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
625 card->card_number, check, syndrome);
627 writeb(0, card->csr_remap + ERROR_COUNT);
630 if (dma_status & DMASCR_PARITY_ERR_REP) {
631 printk(KERN_ERR "MM%d*: PARITY ERROR REPORTED\n", card->card_number);
632 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
633 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
636 if (dma_status & DMASCR_PARITY_ERR_DET) {
637 printk(KERN_ERR "MM%d*: PARITY ERROR DETECTED\n", card->card_number);
638 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
639 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
642 if (dma_status & DMASCR_SYSTEM_ERR_SIG) {
643 printk(KERN_ERR "MM%d*: SYSTEM ERROR\n", card->card_number);
644 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
645 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
648 if (dma_status & DMASCR_TARGET_ABT) {
649 printk(KERN_ERR "MM%d*: TARGET ABORT\n", card->card_number);
650 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
651 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
654 if (dma_status & DMASCR_MASTER_ABT) {
655 printk(KERN_ERR "MM%d*: MASTER ABORT\n", card->card_number);
656 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
657 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
660 /* and process the DMA descriptors */
661 card->dma_status = dma_status;
662 tasklet_schedule(&card->tasklet);
664 HW_TRACE(0x36);
666 return IRQ_HANDLED;
669 -----------------------------------------------------------------------------------
670 -- set_fault_to_battery_status
671 -----------------------------------------------------------------------------------
674 * If both batteries are good, no LED
675 * If either battery has been warned, solid LED
676 * If both batteries are bad, flash the LED quickly
677 * If either battery is bad, flash the LED semi quickly
679 static void set_fault_to_battery_status(struct cardinfo *card)
681 if (card->battery[0].good && card->battery[1].good)
682 set_led(card, LED_FAULT, LED_OFF);
683 else if (card->battery[0].warned || card->battery[1].warned)
684 set_led(card, LED_FAULT, LED_ON);
685 else if (!card->battery[0].good && !card->battery[1].good)
686 set_led(card, LED_FAULT, LED_FLASH_7_0);
687 else
688 set_led(card, LED_FAULT, LED_FLASH_3_5);
691 static void init_battery_timer(void);
695 -----------------------------------------------------------------------------------
696 -- check_battery
697 -----------------------------------------------------------------------------------
699 static int check_battery(struct cardinfo *card, int battery, int status)
701 if (status != card->battery[battery].good) {
702 card->battery[battery].good = !card->battery[battery].good;
703 card->battery[battery].last_change = jiffies;
705 if (card->battery[battery].good) {
706 printk(KERN_ERR "MM%d: Battery %d now good\n",
707 card->card_number, battery + 1);
708 card->battery[battery].warned = 0;
709 } else
710 printk(KERN_ERR "MM%d: Battery %d now FAILED\n",
711 card->card_number, battery + 1);
713 return 1;
714 } else if (!card->battery[battery].good &&
715 !card->battery[battery].warned &&
716 time_after_eq(jiffies, card->battery[battery].last_change +
717 (HZ * 60 * 60 * 5))) {
718 printk(KERN_ERR "MM%d: Battery %d still FAILED after 5 hours\n",
719 card->card_number, battery + 1);
720 card->battery[battery].warned = 1;
722 return 1;
725 return 0;
728 -----------------------------------------------------------------------------------
729 -- check_batteries
730 -----------------------------------------------------------------------------------
732 static void check_batteries(struct cardinfo *card)
734 /* NOTE: this must *never* be called while the card
735 * is doing (bus-to-card) DMA, or you will need the
736 * reset switch
738 unsigned char status;
739 int ret1, ret2;
741 status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
742 if (debug & DEBUG_BATTERY_POLLING)
743 printk(KERN_DEBUG "MM%d: checking battery status, 1 = %s, 2 = %s\n",
744 card->card_number,
745 (status & BATTERY_1_FAILURE) ? "FAILURE" : "OK",
746 (status & BATTERY_2_FAILURE) ? "FAILURE" : "OK");
748 ret1 = check_battery(card, 0, !(status & BATTERY_1_FAILURE));
749 ret2 = check_battery(card, 1, !(status & BATTERY_2_FAILURE));
751 if (ret1 || ret2)
752 set_fault_to_battery_status(card);
755 static void check_all_batteries(unsigned long ptr)
757 int i;
759 for (i = 0; i < num_cards; i++)
760 if (!(cards[i].flags & UM_FLAG_NO_BATT)) {
761 struct cardinfo *card = &cards[i];
762 spin_lock_bh(&card->lock);
763 if (card->Active >= 0)
764 card->check_batteries = 1;
765 else
766 check_batteries(card);
767 spin_unlock_bh(&card->lock);
770 init_battery_timer();
773 -----------------------------------------------------------------------------------
774 -- init_battery_timer
775 -----------------------------------------------------------------------------------
777 static void init_battery_timer(void)
779 init_timer(&battery_timer);
780 battery_timer.function = check_all_batteries;
781 battery_timer.expires = jiffies + (HZ * 60);
782 add_timer(&battery_timer);
785 -----------------------------------------------------------------------------------
786 -- del_battery_timer
787 -----------------------------------------------------------------------------------
789 static void del_battery_timer(void)
791 del_timer(&battery_timer);
794 -----------------------------------------------------------------------------------
795 -- mm_revalidate
796 -----------------------------------------------------------------------------------
799 * Note no locks taken out here. In a worst case scenario, we could drop
800 * a chunk of system memory. But that should never happen, since validation
801 * happens at open or mount time, when locks are held.
803 * That's crap, since doing that while some partitions are opened
804 * or mounted will give you really nasty results.
806 static int mm_revalidate(struct gendisk *disk)
808 struct cardinfo *card = disk->private_data;
809 set_capacity(disk, card->mm_size << 1);
810 return 0;
813 static int mm_getgeo(struct block_device *bdev, struct hd_geometry *geo)
815 struct cardinfo *card = bdev->bd_disk->private_data;
816 int size = card->mm_size * (1024 / MM_HARDSECT);
819 * get geometry: we have to fake one... trim the size to a
820 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
821 * whatever cylinders.
823 geo->heads = 64;
824 geo->sectors = 32;
825 geo->cylinders = size / (geo->heads * geo->sectors);
826 return 0;
830 -----------------------------------------------------------------------------------
831 -- mm_check_change
832 -----------------------------------------------------------------------------------
833 Future support for removable devices
835 static int mm_check_change(struct gendisk *disk)
837 /* struct cardinfo *dev = disk->private_data; */
838 return 0;
841 -----------------------------------------------------------------------------------
842 -- mm_fops
843 -----------------------------------------------------------------------------------
845 static struct block_device_operations mm_fops = {
846 .owner = THIS_MODULE,
847 .getgeo = mm_getgeo,
848 .revalidate_disk= mm_revalidate,
849 .media_changed = mm_check_change,
852 -----------------------------------------------------------------------------------
853 -- mm_pci_probe
854 -----------------------------------------------------------------------------------
856 static int __devinit mm_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
858 int ret = -ENODEV;
859 struct cardinfo *card = &cards[num_cards];
860 unsigned char mem_present;
861 unsigned char batt_status;
862 unsigned int saved_bar, data;
863 int magic_number;
865 if (pci_enable_device(dev) < 0)
866 return -ENODEV;
868 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xF8);
869 pci_set_master(dev);
871 card->dev = dev;
872 card->card_number = num_cards;
874 card->csr_base = pci_resource_start(dev, 0);
875 card->csr_len = pci_resource_len(dev, 0);
876 #ifdef CONFIG_MM_MAP_MEMORY
877 card->mem_base = pci_resource_start(dev, 1);
878 card->mem_len = pci_resource_len(dev, 1);
879 #endif
881 printk(KERN_INFO "Micro Memory(tm) controller #%d found at %02x:%02x (PCI Mem Module (Battery Backup))\n",
882 card->card_number, dev->bus->number, dev->devfn);
884 if (pci_set_dma_mask(dev, DMA_64BIT_MASK) &&
885 pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
886 printk(KERN_WARNING "MM%d: NO suitable DMA found\n",num_cards);
887 return -ENOMEM;
889 if (!request_mem_region(card->csr_base, card->csr_len, "Micro Memory")) {
890 printk(KERN_ERR "MM%d: Unable to request memory region\n", card->card_number);
891 ret = -ENOMEM;
893 goto failed_req_csr;
896 card->csr_remap = ioremap_nocache(card->csr_base, card->csr_len);
897 if (!card->csr_remap) {
898 printk(KERN_ERR "MM%d: Unable to remap memory region\n", card->card_number);
899 ret = -ENOMEM;
901 goto failed_remap_csr;
904 printk(KERN_INFO "MM%d: CSR 0x%08lx -> 0x%p (0x%lx)\n", card->card_number,
905 card->csr_base, card->csr_remap, card->csr_len);
907 #ifdef CONFIG_MM_MAP_MEMORY
908 if (!request_mem_region(card->mem_base, card->mem_len, "Micro Memory")) {
909 printk(KERN_ERR "MM%d: Unable to request memory region\n", card->card_number);
910 ret = -ENOMEM;
912 goto failed_req_mem;
915 if (!(card->mem_remap = ioremap(card->mem_base, cards->mem_len))) {
916 printk(KERN_ERR "MM%d: Unable to remap memory region\n", card->card_number);
917 ret = -ENOMEM;
919 goto failed_remap_mem;
922 printk(KERN_INFO "MM%d: MEM 0x%8lx -> 0x%8lx (0x%lx)\n", card->card_number,
923 card->mem_base, card->mem_remap, card->mem_len);
924 #else
925 printk(KERN_INFO "MM%d: MEM area not remapped (CONFIG_MM_MAP_MEMORY not set)\n",
926 card->card_number);
927 #endif
928 switch(card->dev->device) {
929 case 0x5415:
930 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG;
931 magic_number = 0x59;
932 break;
934 case 0x5425:
935 card->flags |= UM_FLAG_NO_BYTE_STATUS;
936 magic_number = 0x5C;
937 break;
939 case 0x6155:
940 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG | UM_FLAG_NO_BATT;
941 magic_number = 0x99;
942 break;
944 default:
945 magic_number = 0x100;
946 break;
949 if (readb(card->csr_remap + MEMCTRLSTATUS_MAGIC) != magic_number) {
950 printk(KERN_ERR "MM%d: Magic number invalid\n", card->card_number);
951 ret = -ENOMEM;
952 goto failed_magic;
955 card->mm_pages[0].desc = pci_alloc_consistent(card->dev,
956 PAGE_SIZE*2,
957 &card->mm_pages[0].page_dma);
958 card->mm_pages[1].desc = pci_alloc_consistent(card->dev,
959 PAGE_SIZE*2,
960 &card->mm_pages[1].page_dma);
961 if (card->mm_pages[0].desc == NULL ||
962 card->mm_pages[1].desc == NULL) {
963 printk(KERN_ERR "MM%d: alloc failed\n", card->card_number);
964 goto failed_alloc;
966 reset_page(&card->mm_pages[0]);
967 reset_page(&card->mm_pages[1]);
968 card->Ready = 0; /* page 0 is ready */
969 card->Active = -1; /* no page is active */
970 card->bio = NULL;
971 card->biotail = &card->bio;
973 card->queue = blk_alloc_queue(GFP_KERNEL);
974 if (!card->queue)
975 goto failed_alloc;
977 blk_queue_make_request(card->queue, mm_make_request);
978 card->queue->queuedata = card;
979 card->queue->unplug_fn = mm_unplug_device;
981 tasklet_init(&card->tasklet, process_page, (unsigned long)card);
983 card->check_batteries = 0;
985 mem_present = readb(card->csr_remap + MEMCTRLSTATUS_MEMORY);
986 switch (mem_present) {
987 case MEM_128_MB:
988 card->mm_size = 1024 * 128;
989 break;
990 case MEM_256_MB:
991 card->mm_size = 1024 * 256;
992 break;
993 case MEM_512_MB:
994 card->mm_size = 1024 * 512;
995 break;
996 case MEM_1_GB:
997 card->mm_size = 1024 * 1024;
998 break;
999 case MEM_2_GB:
1000 card->mm_size = 1024 * 2048;
1001 break;
1002 default:
1003 card->mm_size = 0;
1004 break;
1007 /* Clear the LED's we control */
1008 set_led(card, LED_REMOVE, LED_OFF);
1009 set_led(card, LED_FAULT, LED_OFF);
1011 batt_status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
1013 card->battery[0].good = !(batt_status & BATTERY_1_FAILURE);
1014 card->battery[1].good = !(batt_status & BATTERY_2_FAILURE);
1015 card->battery[0].last_change = card->battery[1].last_change = jiffies;
1017 if (card->flags & UM_FLAG_NO_BATT)
1018 printk(KERN_INFO "MM%d: Size %d KB\n",
1019 card->card_number, card->mm_size);
1020 else {
1021 printk(KERN_INFO "MM%d: Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
1022 card->card_number, card->mm_size,
1023 (batt_status & BATTERY_1_DISABLED ? "Disabled" : "Enabled"),
1024 card->battery[0].good ? "OK" : "FAILURE",
1025 (batt_status & BATTERY_2_DISABLED ? "Disabled" : "Enabled"),
1026 card->battery[1].good ? "OK" : "FAILURE");
1028 set_fault_to_battery_status(card);
1031 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &saved_bar);
1032 data = 0xffffffff;
1033 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, data);
1034 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &data);
1035 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, saved_bar);
1036 data &= 0xfffffff0;
1037 data = ~data;
1038 data += 1;
1040 card->win_size = data;
1043 if (request_irq(dev->irq, mm_interrupt, IRQF_SHARED, "pci-umem", card)) {
1044 printk(KERN_ERR "MM%d: Unable to allocate IRQ\n", card->card_number);
1045 ret = -ENODEV;
1047 goto failed_req_irq;
1050 card->irq = dev->irq;
1051 printk(KERN_INFO "MM%d: Window size %d bytes, IRQ %d\n", card->card_number,
1052 card->win_size, card->irq);
1054 spin_lock_init(&card->lock);
1056 pci_set_drvdata(dev, card);
1058 if (pci_write_cmd != 0x0F) /* If not Memory Write & Invalidate */
1059 pci_write_cmd = 0x07; /* then Memory Write command */
1061 if (pci_write_cmd & 0x08) { /* use Memory Write and Invalidate */
1062 unsigned short cfg_command;
1063 pci_read_config_word(dev, PCI_COMMAND, &cfg_command);
1064 cfg_command |= 0x10; /* Memory Write & Invalidate Enable */
1065 pci_write_config_word(dev, PCI_COMMAND, cfg_command);
1067 pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24);
1069 num_cards++;
1071 if (!get_userbit(card, MEMORY_INITIALIZED)) {
1072 printk(KERN_INFO "MM%d: memory NOT initialized. Consider over-writing whole device.\n", card->card_number);
1073 card->init_size = 0;
1074 } else {
1075 printk(KERN_INFO "MM%d: memory already initialized\n", card->card_number);
1076 card->init_size = card->mm_size;
1079 /* Enable ECC */
1080 writeb(EDC_STORE_CORRECT, card->csr_remap + MEMCTRLCMD_ERRCTRL);
1082 return 0;
1084 failed_req_irq:
1085 failed_alloc:
1086 if (card->mm_pages[0].desc)
1087 pci_free_consistent(card->dev, PAGE_SIZE*2,
1088 card->mm_pages[0].desc,
1089 card->mm_pages[0].page_dma);
1090 if (card->mm_pages[1].desc)
1091 pci_free_consistent(card->dev, PAGE_SIZE*2,
1092 card->mm_pages[1].desc,
1093 card->mm_pages[1].page_dma);
1094 failed_magic:
1095 #ifdef CONFIG_MM_MAP_MEMORY
1096 iounmap(card->mem_remap);
1097 failed_remap_mem:
1098 release_mem_region(card->mem_base, card->mem_len);
1099 failed_req_mem:
1100 #endif
1101 iounmap(card->csr_remap);
1102 failed_remap_csr:
1103 release_mem_region(card->csr_base, card->csr_len);
1104 failed_req_csr:
1106 return ret;
1109 -----------------------------------------------------------------------------------
1110 -- mm_pci_remove
1111 -----------------------------------------------------------------------------------
1113 static void mm_pci_remove(struct pci_dev *dev)
1115 struct cardinfo *card = pci_get_drvdata(dev);
1117 tasklet_kill(&card->tasklet);
1118 iounmap(card->csr_remap);
1119 release_mem_region(card->csr_base, card->csr_len);
1120 #ifdef CONFIG_MM_MAP_MEMORY
1121 iounmap(card->mem_remap);
1122 release_mem_region(card->mem_base, card->mem_len);
1123 #endif
1124 free_irq(card->irq, card);
1126 if (card->mm_pages[0].desc)
1127 pci_free_consistent(card->dev, PAGE_SIZE*2,
1128 card->mm_pages[0].desc,
1129 card->mm_pages[0].page_dma);
1130 if (card->mm_pages[1].desc)
1131 pci_free_consistent(card->dev, PAGE_SIZE*2,
1132 card->mm_pages[1].desc,
1133 card->mm_pages[1].page_dma);
1134 blk_cleanup_queue(card->queue);
1137 static const struct pci_device_id mm_pci_ids[] = { {
1138 .vendor = PCI_VENDOR_ID_MICRO_MEMORY,
1139 .device = PCI_DEVICE_ID_MICRO_MEMORY_5415CN,
1140 }, {
1141 .vendor = PCI_VENDOR_ID_MICRO_MEMORY,
1142 .device = PCI_DEVICE_ID_MICRO_MEMORY_5425CN,
1143 }, {
1144 .vendor = PCI_VENDOR_ID_MICRO_MEMORY,
1145 .device = PCI_DEVICE_ID_MICRO_MEMORY_6155,
1146 }, {
1147 .vendor = 0x8086,
1148 .device = 0xB555,
1149 .subvendor= 0x1332,
1150 .subdevice= 0x5460,
1151 .class = 0x050000,
1152 .class_mask= 0,
1153 }, { /* end: all zeroes */ }
1156 MODULE_DEVICE_TABLE(pci, mm_pci_ids);
1158 static struct pci_driver mm_pci_driver = {
1159 .name = "umem",
1160 .id_table = mm_pci_ids,
1161 .probe = mm_pci_probe,
1162 .remove = mm_pci_remove,
1165 -----------------------------------------------------------------------------------
1166 -- mm_init
1167 -----------------------------------------------------------------------------------
1170 static int __init mm_init(void)
1172 int retval, i;
1173 int err;
1175 printk(KERN_INFO DRIVER_VERSION " : " DRIVER_DESC "\n");
1177 retval = pci_register_driver(&mm_pci_driver);
1178 if (retval)
1179 return -ENOMEM;
1181 err = major_nr = register_blkdev(0, "umem");
1182 if (err < 0) {
1183 pci_unregister_driver(&mm_pci_driver);
1184 return -EIO;
1187 for (i = 0; i < num_cards; i++) {
1188 mm_gendisk[i] = alloc_disk(1 << MM_SHIFT);
1189 if (!mm_gendisk[i])
1190 goto out;
1193 for (i = 0; i < num_cards; i++) {
1194 struct gendisk *disk = mm_gendisk[i];
1195 sprintf(disk->disk_name, "umem%c", 'a'+i);
1196 spin_lock_init(&cards[i].lock);
1197 disk->major = major_nr;
1198 disk->first_minor = i << MM_SHIFT;
1199 disk->fops = &mm_fops;
1200 disk->private_data = &cards[i];
1201 disk->queue = cards[i].queue;
1202 set_capacity(disk, cards[i].mm_size << 1);
1203 add_disk(disk);
1206 init_battery_timer();
1207 printk("MM: desc_per_page = %ld\n", DESC_PER_PAGE);
1208 /* printk("mm_init: Done. 10-19-01 9:00\n"); */
1209 return 0;
1211 out:
1212 pci_unregister_driver(&mm_pci_driver);
1213 unregister_blkdev(major_nr, "umem");
1214 while (i--)
1215 put_disk(mm_gendisk[i]);
1216 return -ENOMEM;
1219 -----------------------------------------------------------------------------------
1220 -- mm_cleanup
1221 -----------------------------------------------------------------------------------
1223 static void __exit mm_cleanup(void)
1225 int i;
1227 del_battery_timer();
1229 for (i=0; i < num_cards ; i++) {
1230 del_gendisk(mm_gendisk[i]);
1231 put_disk(mm_gendisk[i]);
1234 pci_unregister_driver(&mm_pci_driver);
1236 unregister_blkdev(major_nr, "umem");
1239 module_init(mm_init);
1240 module_exit(mm_cleanup);
1242 MODULE_AUTHOR(DRIVER_AUTHOR);
1243 MODULE_DESCRIPTION(DRIVER_DESC);
1244 MODULE_LICENSE("GPL");