2 * drivers/block/mg_disk.c
4 * Support for the mGine m[g]flash IO mode.
7 * (c) 2008 mGine Co.,LTD
8 * (c) 2008 unsik Kim <donari75@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/blkdev.h>
19 #include <linux/hdreg.h>
20 #include <linux/ata.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/gpio.h>
26 #define MG_RES_SEC (CONFIG_MG_DISK_RES << 1)
28 /* name for block device */
29 #define MG_DISK_NAME "mgd"
30 /* name for platform device */
31 #define MG_DEV_NAME "mg_disk"
34 #define MG_DISK_MAX_PART 16
35 #define MG_SECTOR_SIZE 512
36 #define MG_MAX_SECTS 256
38 /* Register offsets */
39 #define MG_BUFF_OFFSET 0x8000
40 #define MG_STORAGE_BUFFER_SIZE 0x200
41 #define MG_REG_OFFSET 0xC000
42 #define MG_REG_FEATURE (MG_REG_OFFSET + 2) /* write case */
43 #define MG_REG_ERROR (MG_REG_OFFSET + 2) /* read case */
44 #define MG_REG_SECT_CNT (MG_REG_OFFSET + 4)
45 #define MG_REG_SECT_NUM (MG_REG_OFFSET + 6)
46 #define MG_REG_CYL_LOW (MG_REG_OFFSET + 8)
47 #define MG_REG_CYL_HIGH (MG_REG_OFFSET + 0xA)
48 #define MG_REG_DRV_HEAD (MG_REG_OFFSET + 0xC)
49 #define MG_REG_COMMAND (MG_REG_OFFSET + 0xE) /* write case */
50 #define MG_REG_STATUS (MG_REG_OFFSET + 0xE) /* read case */
51 #define MG_REG_DRV_CTRL (MG_REG_OFFSET + 0x10)
52 #define MG_REG_BURST_CTRL (MG_REG_OFFSET + 0x12)
54 /* "Drive Select/Head Register" bit values */
55 #define MG_REG_HEAD_MUST_BE_ON 0xA0 /* These 2 bits are always on */
56 #define MG_REG_HEAD_DRIVE_MASTER (0x00 | MG_REG_HEAD_MUST_BE_ON)
57 #define MG_REG_HEAD_DRIVE_SLAVE (0x10 | MG_REG_HEAD_MUST_BE_ON)
58 #define MG_REG_HEAD_LBA_MODE (0x40 | MG_REG_HEAD_MUST_BE_ON)
61 /* "Device Control Register" bit values */
62 #define MG_REG_CTRL_INTR_ENABLE 0x0
63 #define MG_REG_CTRL_INTR_DISABLE (0x1<<1)
64 #define MG_REG_CTRL_RESET (0x1<<2)
65 #define MG_REG_CTRL_INTR_POLA_ACTIVE_HIGH 0x0
66 #define MG_REG_CTRL_INTR_POLA_ACTIVE_LOW (0x1<<4)
67 #define MG_REG_CTRL_DPD_POLA_ACTIVE_LOW 0x0
68 #define MG_REG_CTRL_DPD_POLA_ACTIVE_HIGH (0x1<<5)
69 #define MG_REG_CTRL_DPD_DISABLE 0x0
70 #define MG_REG_CTRL_DPD_ENABLE (0x1<<6)
72 /* Status register bit */
73 /* error bit in status register */
74 #define MG_REG_STATUS_BIT_ERROR 0x01
75 /* corrected error in status register */
76 #define MG_REG_STATUS_BIT_CORRECTED_ERROR 0x04
77 /* data request bit in status register */
78 #define MG_REG_STATUS_BIT_DATA_REQ 0x08
79 /* DSC - Drive Seek Complete */
80 #define MG_REG_STATUS_BIT_SEEK_DONE 0x10
81 /* DWF - Drive Write Fault */
82 #define MG_REG_STATUS_BIT_WRITE_FAULT 0x20
83 #define MG_REG_STATUS_BIT_READY 0x40
84 #define MG_REG_STATUS_BIT_BUSY 0x80
87 #define MG_STAT_READY (MG_REG_STATUS_BIT_READY | MG_REG_STATUS_BIT_SEEK_DONE)
88 #define MG_READY_OK(s) (((s) & (MG_STAT_READY | \
89 (MG_REG_STATUS_BIT_BUSY | \
90 MG_REG_STATUS_BIT_WRITE_FAULT | \
91 MG_REG_STATUS_BIT_ERROR))) == MG_STAT_READY)
94 #define MG_REG_ERR_AMNF 0x01
95 #define MG_REG_ERR_ABRT 0x04
96 #define MG_REG_ERR_IDNF 0x10
97 #define MG_REG_ERR_UNC 0x40
98 #define MG_REG_ERR_BBK 0x80
100 /* error code for others */
101 #define MG_ERR_NONE 0
102 #define MG_ERR_TIMEOUT 0x100
103 #define MG_ERR_INIT_STAT 0x101
104 #define MG_ERR_TRANSLATION 0x102
105 #define MG_ERR_CTRL_RST 0x103
106 #define MG_ERR_INV_STAT 0x104
107 #define MG_ERR_RSTOUT 0x105
109 #define MG_MAX_ERRORS 6 /* Max read/write errors */
112 #define MG_CMD_RD 0x20
113 #define MG_CMD_WR 0x30
114 #define MG_CMD_SLEEP 0x99
115 #define MG_CMD_WAKEUP 0xC3
116 #define MG_CMD_ID 0xEC
117 #define MG_CMD_WR_CONF 0x3C
118 #define MG_CMD_RD_CONF 0x40
121 #define MG_OP_CASCADE (1 << 0)
122 #define MG_OP_CASCADE_SYNC_RD (1 << 1)
123 #define MG_OP_CASCADE_SYNC_WR (1 << 2)
124 #define MG_OP_INTERLEAVE (1 << 3)
127 #define MG_BURST_LAT_4 (3 << 4)
128 #define MG_BURST_LAT_5 (4 << 4)
129 #define MG_BURST_LAT_6 (5 << 4)
130 #define MG_BURST_LAT_7 (6 << 4)
131 #define MG_BURST_LAT_8 (7 << 4)
132 #define MG_BURST_LEN_4 (1 << 1)
133 #define MG_BURST_LEN_8 (2 << 1)
134 #define MG_BURST_LEN_16 (3 << 1)
135 #define MG_BURST_LEN_32 (4 << 1)
136 #define MG_BURST_LEN_CONT (0 << 1)
138 /* timeout value (unit: ms) */
139 #define MG_TMAX_CONF_TO_CMD 1
140 #define MG_TMAX_WAIT_RD_DRQ 10
141 #define MG_TMAX_WAIT_WR_DRQ 500
142 #define MG_TMAX_RST_TO_BUSY 10
143 #define MG_TMAX_HDRST_TO_RDY 500
144 #define MG_TMAX_SWRST_TO_RDY 500
145 #define MG_TMAX_RSTOUT 3000
147 /* device attribution */
148 /* use mflash as boot device */
149 #define MG_BOOT_DEV (1 << 0)
150 /* use mflash as storage device */
151 #define MG_STORAGE_DEV (1 << 1)
152 /* same as MG_STORAGE_DEV, but bootloader already done reset sequence */
153 #define MG_STORAGE_DEV_SKIP_RST (1 << 2)
155 #define MG_DEV_MASK (MG_BOOT_DEV | MG_STORAGE_DEV | MG_STORAGE_DEV_SKIP_RST)
157 /* names of GPIO resource */
158 #define MG_RST_PIN "mg_rst"
159 /* except MG_BOOT_DEV, reset-out pin should be assigned */
160 #define MG_RSTOUT_PIN "mg_rstout"
162 /* private driver data */
167 /* device attribution */
170 /* internally used */
171 struct mg_host
*host
;
174 /* main structure for mflash driver */
178 struct request_queue
*breq
;
182 struct timer_list timer
;
183 void (*mg_do_intr
) (struct mg_host
*);
185 u16 id
[ATA_ID_WORDS
];
193 void __iomem
*dev_base
;
203 * Debugging macro and defines
207 # define MG_DBG(fmt, args...) \
208 printk(KERN_DEBUG "%s:%d "fmt, __func__, __LINE__, ##args)
209 #else /* CONFIG_MG_DEBUG */
210 # define MG_DBG(fmt, args...) do { } while (0)
211 #endif /* CONFIG_MG_DEBUG */
213 static void mg_request(struct request_queue
*);
215 static void mg_dump_status(const char *msg
, unsigned int stat
,
216 struct mg_host
*host
)
218 char *name
= MG_DISK_NAME
;
222 req
= elv_next_request(host
->breq
);
224 name
= req
->rq_disk
->disk_name
;
227 printk(KERN_ERR
"%s: %s: status=0x%02x { ", name
, msg
, stat
& 0xff);
228 if (stat
& MG_REG_STATUS_BIT_BUSY
)
230 if (stat
& MG_REG_STATUS_BIT_READY
)
231 printk("DriveReady ");
232 if (stat
& MG_REG_STATUS_BIT_WRITE_FAULT
)
233 printk("WriteFault ");
234 if (stat
& MG_REG_STATUS_BIT_SEEK_DONE
)
235 printk("SeekComplete ");
236 if (stat
& MG_REG_STATUS_BIT_DATA_REQ
)
237 printk("DataRequest ");
238 if (stat
& MG_REG_STATUS_BIT_CORRECTED_ERROR
)
239 printk("CorrectedError ");
240 if (stat
& MG_REG_STATUS_BIT_ERROR
)
243 if ((stat
& MG_REG_STATUS_BIT_ERROR
) == 0) {
246 host
->error
= inb((unsigned long)host
->dev_base
+ MG_REG_ERROR
);
247 printk(KERN_ERR
"%s: %s: error=0x%02x { ", name
, msg
,
249 if (host
->error
& MG_REG_ERR_BBK
)
250 printk("BadSector ");
251 if (host
->error
& MG_REG_ERR_UNC
)
252 printk("UncorrectableError ");
253 if (host
->error
& MG_REG_ERR_IDNF
)
254 printk("SectorIdNotFound ");
255 if (host
->error
& MG_REG_ERR_ABRT
)
256 printk("DriveStatusError ");
257 if (host
->error
& MG_REG_ERR_AMNF
)
258 printk("AddrMarkNotFound ");
261 (MG_REG_ERR_BBK
| MG_REG_ERR_UNC
|
262 MG_REG_ERR_IDNF
| MG_REG_ERR_AMNF
)) {
264 req
= elv_next_request(host
->breq
);
266 printk(", sector=%u", (u32
)req
->sector
);
274 static unsigned int mg_wait(struct mg_host
*host
, u32 expect
, u32 msec
)
277 unsigned long expire
, cur_jiffies
;
278 struct mg_drv_data
*prv_data
= host
->dev
->platform_data
;
280 host
->error
= MG_ERR_NONE
;
281 expire
= jiffies
+ msecs_to_jiffies(msec
);
283 status
= inb((unsigned long)host
->dev_base
+ MG_REG_STATUS
);
286 cur_jiffies
= jiffies
;
287 if (status
& MG_REG_STATUS_BIT_BUSY
) {
288 if (expect
== MG_REG_STATUS_BIT_BUSY
)
291 /* Check the error condition! */
292 if (status
& MG_REG_STATUS_BIT_ERROR
) {
293 mg_dump_status("mg_wait", status
, host
);
297 if (expect
== MG_STAT_READY
)
298 if (MG_READY_OK(status
))
301 if (expect
== MG_REG_STATUS_BIT_DATA_REQ
)
302 if (status
& MG_REG_STATUS_BIT_DATA_REQ
)
306 mg_dump_status("not ready", status
, host
);
307 return MG_ERR_INV_STAT
;
309 if (prv_data
->use_polling
)
312 status
= inb((unsigned long)host
->dev_base
+ MG_REG_STATUS
);
313 } while (time_before(cur_jiffies
, expire
));
315 if (time_after_eq(cur_jiffies
, expire
) && msec
)
316 host
->error
= MG_ERR_TIMEOUT
;
321 static unsigned int mg_wait_rstout(u32 rstout
, u32 msec
)
323 unsigned long expire
;
325 expire
= jiffies
+ msecs_to_jiffies(msec
);
326 while (time_before(jiffies
, expire
)) {
327 if (gpio_get_value(rstout
) == 1)
332 return MG_ERR_RSTOUT
;
335 static void mg_unexpected_intr(struct mg_host
*host
)
337 u32 status
= inb((unsigned long)host
->dev_base
+ MG_REG_STATUS
);
339 mg_dump_status("mg_unexpected_intr", status
, host
);
342 static irqreturn_t
mg_irq(int irq
, void *dev_id
)
344 struct mg_host
*host
= dev_id
;
345 void (*handler
)(struct mg_host
*) = host
->mg_do_intr
;
347 spin_lock(&host
->lock
);
349 host
->mg_do_intr
= NULL
;
350 del_timer(&host
->timer
);
352 handler
= mg_unexpected_intr
;
355 spin_unlock(&host
->lock
);
360 /* local copy of ata_id_string() */
361 static void mg_id_string(const u16
*id
, unsigned char *s
,
362 unsigned int ofs
, unsigned int len
)
382 /* local copy of ata_id_c_string() */
383 static void mg_id_c_string(const u16
*id
, unsigned char *s
,
384 unsigned int ofs
, unsigned int len
)
388 mg_id_string(id
, s
, ofs
, len
- 1);
390 p
= s
+ strnlen(s
, len
- 1);
391 while (p
> s
&& p
[-1] == ' ')
396 static int mg_get_disk_id(struct mg_host
*host
)
400 const u16
*id
= host
->id
;
401 struct mg_drv_data
*prv_data
= host
->dev
->platform_data
;
402 char fwrev
[ATA_ID_FW_REV_LEN
+ 1];
403 char model
[ATA_ID_PROD_LEN
+ 1];
404 char serial
[ATA_ID_SERNO_LEN
+ 1];
406 if (!prv_data
->use_polling
)
407 outb(MG_REG_CTRL_INTR_DISABLE
,
408 (unsigned long)host
->dev_base
+
411 outb(MG_CMD_ID
, (unsigned long)host
->dev_base
+ MG_REG_COMMAND
);
412 err
= mg_wait(host
, MG_REG_STATUS_BIT_DATA_REQ
, MG_TMAX_WAIT_RD_DRQ
);
416 for (i
= 0; i
< (MG_SECTOR_SIZE
>> 1); i
++)
417 host
->id
[i
] = le16_to_cpu(inw((unsigned long)host
->dev_base
+
418 MG_BUFF_OFFSET
+ i
* 2));
420 outb(MG_CMD_RD_CONF
, (unsigned long)host
->dev_base
+ MG_REG_COMMAND
);
421 err
= mg_wait(host
, MG_STAT_READY
, MG_TMAX_CONF_TO_CMD
);
425 if ((id
[ATA_ID_FIELD_VALID
] & 1) == 0)
426 return MG_ERR_TRANSLATION
;
428 host
->n_sectors
= ata_id_u32(id
, ATA_ID_LBA_CAPACITY
);
429 host
->cyls
= id
[ATA_ID_CYLS
];
430 host
->heads
= id
[ATA_ID_HEADS
];
431 host
->sectors
= id
[ATA_ID_SECTORS
];
433 if (MG_RES_SEC
&& host
->heads
&& host
->sectors
) {
434 /* modify cyls, n_sectors */
435 host
->cyls
= (host
->n_sectors
- MG_RES_SEC
) /
436 host
->heads
/ host
->sectors
;
437 host
->nres_sectors
= host
->n_sectors
- host
->cyls
*
438 host
->heads
* host
->sectors
;
439 host
->n_sectors
-= host
->nres_sectors
;
442 mg_id_c_string(id
, fwrev
, ATA_ID_FW_REV
, sizeof(fwrev
));
443 mg_id_c_string(id
, model
, ATA_ID_PROD
, sizeof(model
));
444 mg_id_c_string(id
, serial
, ATA_ID_SERNO
, sizeof(serial
));
445 printk(KERN_INFO
"mg_disk: model: %s\n", model
);
446 printk(KERN_INFO
"mg_disk: firm: %.8s\n", fwrev
);
447 printk(KERN_INFO
"mg_disk: serial: %s\n", serial
);
448 printk(KERN_INFO
"mg_disk: %d + reserved %d sectors\n",
449 host
->n_sectors
, host
->nres_sectors
);
451 if (!prv_data
->use_polling
)
452 outb(MG_REG_CTRL_INTR_ENABLE
, (unsigned long)host
->dev_base
+
459 static int mg_disk_init(struct mg_host
*host
)
461 struct mg_drv_data
*prv_data
= host
->dev
->platform_data
;
466 gpio_set_value(host
->rst
, 0);
467 err
= mg_wait(host
, MG_REG_STATUS_BIT_BUSY
, MG_TMAX_RST_TO_BUSY
);
472 gpio_set_value(host
->rst
, 1);
473 err
= mg_wait(host
, MG_STAT_READY
, MG_TMAX_HDRST_TO_RDY
);
478 outb(MG_REG_CTRL_RESET
|
479 (prv_data
->use_polling
? MG_REG_CTRL_INTR_DISABLE
:
480 MG_REG_CTRL_INTR_ENABLE
),
481 (unsigned long)host
->dev_base
+ MG_REG_DRV_CTRL
);
482 err
= mg_wait(host
, MG_REG_STATUS_BIT_BUSY
, MG_TMAX_RST_TO_BUSY
);
487 outb(prv_data
->use_polling
? MG_REG_CTRL_INTR_DISABLE
:
488 MG_REG_CTRL_INTR_ENABLE
,
489 (unsigned long)host
->dev_base
+ MG_REG_DRV_CTRL
);
490 err
= mg_wait(host
, MG_STAT_READY
, MG_TMAX_SWRST_TO_RDY
);
494 init_status
= inb((unsigned long)host
->dev_base
+ MG_REG_STATUS
) & 0xf;
496 if (init_status
== 0xf)
497 return MG_ERR_INIT_STAT
;
502 static void mg_bad_rw_intr(struct mg_host
*host
)
504 struct request
*req
= elv_next_request(host
->breq
);
506 if (++req
->errors
>= MG_MAX_ERRORS
||
507 host
->error
== MG_ERR_TIMEOUT
)
508 __blk_end_request_cur(req
, -EIO
);
511 static unsigned int mg_out(struct mg_host
*host
,
512 unsigned int sect_num
,
513 unsigned int sect_cnt
,
515 void (*intr_addr
)(struct mg_host
*))
517 struct mg_drv_data
*prv_data
= host
->dev
->platform_data
;
519 if (mg_wait(host
, MG_STAT_READY
, MG_TMAX_CONF_TO_CMD
))
522 if (!prv_data
->use_polling
) {
523 host
->mg_do_intr
= intr_addr
;
524 mod_timer(&host
->timer
, jiffies
+ 3 * HZ
);
527 sect_num
+= MG_RES_SEC
;
528 outb((u8
)sect_cnt
, (unsigned long)host
->dev_base
+ MG_REG_SECT_CNT
);
529 outb((u8
)sect_num
, (unsigned long)host
->dev_base
+ MG_REG_SECT_NUM
);
530 outb((u8
)(sect_num
>> 8), (unsigned long)host
->dev_base
+
532 outb((u8
)(sect_num
>> 16), (unsigned long)host
->dev_base
+
534 outb((u8
)((sect_num
>> 24) | MG_REG_HEAD_LBA_MODE
),
535 (unsigned long)host
->dev_base
+ MG_REG_DRV_HEAD
);
536 outb(cmd
, (unsigned long)host
->dev_base
+ MG_REG_COMMAND
);
540 static void mg_read(struct request
*req
)
543 struct mg_host
*host
= req
->rq_disk
->private_data
;
545 if (mg_out(host
, req
->sector
, req
->nr_sectors
, MG_CMD_RD
, NULL
) !=
547 mg_bad_rw_intr(host
);
549 MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
550 req
->nr_sectors
, req
->sector
, req
->buffer
);
553 u16
*buff
= (u16
*)req
->buffer
;
555 if (mg_wait(host
, MG_REG_STATUS_BIT_DATA_REQ
,
556 MG_TMAX_WAIT_RD_DRQ
) != MG_ERR_NONE
) {
557 mg_bad_rw_intr(host
);
560 for (j
= 0; j
< MG_SECTOR_SIZE
>> 1; j
++)
561 *buff
++ = inw((unsigned long)host
->dev_base
+
562 MG_BUFF_OFFSET
+ (j
<< 1));
564 outb(MG_CMD_RD_CONF
, (unsigned long)host
->dev_base
+
566 } while (__blk_end_request(req
, 0, MG_SECTOR_SIZE
));
569 static void mg_write(struct request
*req
)
572 struct mg_host
*host
= req
->rq_disk
->private_data
;
574 if (mg_out(host
, req
->sector
, req
->nr_sectors
, MG_CMD_WR
, NULL
) !=
576 mg_bad_rw_intr(host
);
580 MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
581 req
->nr_sectors
, req
->sector
, req
->buffer
);
584 u16
*buff
= (u16
*)req
->buffer
;
586 if (mg_wait(host
, MG_REG_STATUS_BIT_DATA_REQ
,
587 MG_TMAX_WAIT_WR_DRQ
) != MG_ERR_NONE
) {
588 mg_bad_rw_intr(host
);
591 for (j
= 0; j
< MG_SECTOR_SIZE
>> 1; j
++)
592 outw(*buff
++, (unsigned long)host
->dev_base
+
593 MG_BUFF_OFFSET
+ (j
<< 1));
595 outb(MG_CMD_WR_CONF
, (unsigned long)host
->dev_base
+
597 } while (__blk_end_request(req
, 0, MG_SECTOR_SIZE
));
600 static void mg_read_intr(struct mg_host
*host
)
608 i
= inb((unsigned long)host
->dev_base
+ MG_REG_STATUS
);
609 if (i
& MG_REG_STATUS_BIT_BUSY
)
613 if (i
& MG_REG_STATUS_BIT_DATA_REQ
)
616 mg_dump_status("mg_read_intr", i
, host
);
617 mg_bad_rw_intr(host
);
618 mg_request(host
->breq
);
622 /* get current segment of request */
623 req
= elv_next_request(host
->breq
);
624 buff
= (u16
*)req
->buffer
;
627 for (i
= 0; i
< MG_SECTOR_SIZE
>> 1; i
++)
628 *buff
++ = inw((unsigned long)host
->dev_base
+ MG_BUFF_OFFSET
+
631 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
632 req
->sector
, req
->nr_sectors
- 1, req
->buffer
);
634 /* send read confirm */
635 outb(MG_CMD_RD_CONF
, (unsigned long)host
->dev_base
+ MG_REG_COMMAND
);
637 if (__blk_end_request(req
, 0, MG_SECTOR_SIZE
)) {
638 /* set handler if read remains */
639 host
->mg_do_intr
= mg_read_intr
;
640 mod_timer(&host
->timer
, jiffies
+ 3 * HZ
);
641 } else /* goto next request */
642 mg_request(host
->breq
);
645 static void mg_write_intr(struct mg_host
*host
)
652 /* get current segment of request */
653 req
= elv_next_request(host
->breq
);
657 i
= inb((unsigned long)host
->dev_base
+ MG_REG_STATUS
);
658 if (i
& MG_REG_STATUS_BIT_BUSY
)
662 if ((req
->nr_sectors
<= 1) || (i
& MG_REG_STATUS_BIT_DATA_REQ
))
665 mg_dump_status("mg_write_intr", i
, host
);
666 mg_bad_rw_intr(host
);
667 mg_request(host
->breq
);
671 if ((rem
= __blk_end_request(req
, 0, MG_SECTOR_SIZE
))) {
672 /* write 1 sector and set handler if remains */
673 buff
= (u16
*)req
->buffer
;
674 for (j
= 0; j
< MG_STORAGE_BUFFER_SIZE
>> 1; j
++) {
675 outw(*buff
, (unsigned long)host
->dev_base
+
676 MG_BUFF_OFFSET
+ (j
<< 1));
679 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
680 req
->sector
, req
->nr_sectors
, req
->buffer
);
681 host
->mg_do_intr
= mg_write_intr
;
682 mod_timer(&host
->timer
, jiffies
+ 3 * HZ
);
685 /* send write confirm */
686 outb(MG_CMD_WR_CONF
, (unsigned long)host
->dev_base
+ MG_REG_COMMAND
);
689 mg_request(host
->breq
);
692 void mg_times_out(unsigned long data
)
694 struct mg_host
*host
= (struct mg_host
*)data
;
698 spin_lock_irq(&host
->lock
);
700 req
= elv_next_request(host
->breq
);
704 host
->mg_do_intr
= NULL
;
706 name
= req
->rq_disk
->disk_name
;
707 printk(KERN_DEBUG
"%s: timeout\n", name
);
709 host
->error
= MG_ERR_TIMEOUT
;
710 mg_bad_rw_intr(host
);
712 mg_request(host
->breq
);
714 spin_unlock_irq(&host
->lock
);
717 static void mg_request_poll(struct request_queue
*q
)
720 struct mg_host
*host
;
722 while ((req
= elv_next_request(q
)) != NULL
) {
723 host
= req
->rq_disk
->private_data
;
724 if (blk_fs_request(req
)) {
725 switch (rq_data_dir(req
)) {
737 static unsigned int mg_issue_req(struct request
*req
,
738 struct mg_host
*host
,
739 unsigned int sect_num
,
740 unsigned int sect_cnt
)
745 switch (rq_data_dir(req
)) {
747 if (mg_out(host
, sect_num
, sect_cnt
, MG_CMD_RD
, &mg_read_intr
)
749 mg_bad_rw_intr(host
);
755 outb(MG_REG_CTRL_INTR_DISABLE
,
756 (unsigned long)host
->dev_base
+
758 if (mg_out(host
, sect_num
, sect_cnt
, MG_CMD_WR
, &mg_write_intr
)
760 mg_bad_rw_intr(host
);
763 del_timer(&host
->timer
);
764 mg_wait(host
, MG_REG_STATUS_BIT_DATA_REQ
, MG_TMAX_WAIT_WR_DRQ
);
765 outb(MG_REG_CTRL_INTR_ENABLE
, (unsigned long)host
->dev_base
+
768 mg_bad_rw_intr(host
);
771 buff
= (u16
*)req
->buffer
;
772 for (i
= 0; i
< MG_SECTOR_SIZE
>> 1; i
++) {
773 outw(*buff
, (unsigned long)host
->dev_base
+
774 MG_BUFF_OFFSET
+ (i
<< 1));
777 mod_timer(&host
->timer
, jiffies
+ 3 * HZ
);
778 outb(MG_CMD_WR_CONF
, (unsigned long)host
->dev_base
+
785 /* This function also called from IRQ context */
786 static void mg_request(struct request_queue
*q
)
789 struct mg_host
*host
;
790 u32 sect_num
, sect_cnt
;
793 req
= elv_next_request(q
);
797 host
= req
->rq_disk
->private_data
;
799 /* check unwanted request call */
800 if (host
->mg_do_intr
)
803 del_timer(&host
->timer
);
805 sect_num
= req
->sector
;
806 /* deal whole segments */
807 sect_cnt
= req
->nr_sectors
;
810 if (sect_num
>= get_capacity(req
->rq_disk
) ||
811 ((sect_num
+ sect_cnt
) >
812 get_capacity(req
->rq_disk
))) {
814 "%s: bad access: sector=%d, count=%d\n",
815 req
->rq_disk
->disk_name
,
817 __blk_end_request_cur(req
, -EIO
);
821 if (!blk_fs_request(req
))
824 if (!mg_issue_req(req
, host
, sect_num
, sect_cnt
))
829 static int mg_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
831 struct mg_host
*host
= bdev
->bd_disk
->private_data
;
833 geo
->cylinders
= (unsigned short)host
->cyls
;
834 geo
->heads
= (unsigned char)host
->heads
;
835 geo
->sectors
= (unsigned char)host
->sectors
;
839 static struct block_device_operations mg_disk_ops
= {
843 static int mg_suspend(struct platform_device
*plat_dev
, pm_message_t state
)
845 struct mg_drv_data
*prv_data
= plat_dev
->dev
.platform_data
;
846 struct mg_host
*host
= prv_data
->host
;
848 if (mg_wait(host
, MG_STAT_READY
, MG_TMAX_CONF_TO_CMD
))
851 if (!prv_data
->use_polling
)
852 outb(MG_REG_CTRL_INTR_DISABLE
,
853 (unsigned long)host
->dev_base
+
856 outb(MG_CMD_SLEEP
, (unsigned long)host
->dev_base
+ MG_REG_COMMAND
);
857 /* wait until mflash deep sleep */
860 if (mg_wait(host
, MG_STAT_READY
, MG_TMAX_CONF_TO_CMD
)) {
861 if (!prv_data
->use_polling
)
862 outb(MG_REG_CTRL_INTR_ENABLE
,
863 (unsigned long)host
->dev_base
+
871 static int mg_resume(struct platform_device
*plat_dev
)
873 struct mg_drv_data
*prv_data
= plat_dev
->dev
.platform_data
;
874 struct mg_host
*host
= prv_data
->host
;
876 if (mg_wait(host
, MG_STAT_READY
, MG_TMAX_CONF_TO_CMD
))
879 outb(MG_CMD_WAKEUP
, (unsigned long)host
->dev_base
+ MG_REG_COMMAND
);
880 /* wait until mflash wakeup */
883 if (mg_wait(host
, MG_STAT_READY
, MG_TMAX_CONF_TO_CMD
))
886 if (!prv_data
->use_polling
)
887 outb(MG_REG_CTRL_INTR_ENABLE
, (unsigned long)host
->dev_base
+
893 static int mg_probe(struct platform_device
*plat_dev
)
895 struct mg_host
*host
;
896 struct resource
*rsc
;
897 struct mg_drv_data
*prv_data
= plat_dev
->dev
.platform_data
;
901 printk(KERN_ERR
"%s:%d fail (no driver_data)\n",
908 host
= kzalloc(sizeof(struct mg_host
), GFP_KERNEL
);
910 printk(KERN_ERR
"%s:%d fail (no memory for mg_host)\n",
915 host
->major
= MG_DISK_MAJ
;
917 /* link each other */
918 prv_data
->host
= host
;
919 host
->dev
= &plat_dev
->dev
;
922 rsc
= platform_get_resource(plat_dev
, IORESOURCE_MEM
, 0);
924 printk(KERN_ERR
"%s:%d platform_get_resource fail\n",
929 host
->dev_base
= ioremap(rsc
->start
, rsc
->end
+ 1);
930 if (!host
->dev_base
) {
931 printk(KERN_ERR
"%s:%d ioremap fail\n",
936 MG_DBG("dev_base = 0x%x\n", (u32
)host
->dev_base
);
939 rsc
= platform_get_resource_byname(plat_dev
, IORESOURCE_IO
,
942 printk(KERN_ERR
"%s:%d get reset pin fail\n",
947 host
->rst
= rsc
->start
;
950 err
= gpio_request(host
->rst
, MG_RST_PIN
);
953 gpio_direction_output(host
->rst
, 1);
956 if (!(prv_data
->dev_attr
& MG_DEV_MASK
))
959 if (prv_data
->dev_attr
!= MG_BOOT_DEV
) {
960 rsc
= platform_get_resource_byname(plat_dev
, IORESOURCE_IO
,
963 printk(KERN_ERR
"%s:%d get reset-out pin fail\n",
968 host
->rstout
= rsc
->start
;
969 err
= gpio_request(host
->rstout
, MG_RSTOUT_PIN
);
972 gpio_direction_input(host
->rstout
);
976 if (prv_data
->dev_attr
== MG_STORAGE_DEV
) {
977 /* If POR seq. not yet finised, wait */
978 err
= mg_wait_rstout(host
->rstout
, MG_TMAX_RSTOUT
);
981 err
= mg_disk_init(host
);
983 printk(KERN_ERR
"%s:%d fail (err code : %d)\n",
984 __func__
, __LINE__
, err
);
990 /* get irq resource */
991 if (!prv_data
->use_polling
) {
992 host
->irq
= platform_get_irq(plat_dev
, 0);
993 if (host
->irq
== -ENXIO
) {
997 err
= request_irq(host
->irq
, mg_irq
,
998 IRQF_DISABLED
| IRQF_TRIGGER_RISING
,
1001 printk(KERN_ERR
"%s:%d fail (request_irq err=%d)\n",
1002 __func__
, __LINE__
, err
);
1009 err
= mg_get_disk_id(host
);
1011 printk(KERN_ERR
"%s:%d fail (err code : %d)\n",
1012 __func__
, __LINE__
, err
);
1017 err
= register_blkdev(host
->major
, MG_DISK_NAME
);
1019 printk(KERN_ERR
"%s:%d register_blkdev fail (err code : %d)\n",
1020 __func__
, __LINE__
, err
);
1026 spin_lock_init(&host
->lock
);
1028 if (prv_data
->use_polling
)
1029 host
->breq
= blk_init_queue(mg_request_poll
, &host
->lock
);
1031 host
->breq
= blk_init_queue(mg_request
, &host
->lock
);
1035 printk(KERN_ERR
"%s:%d (blk_init_queue) fail\n",
1036 __func__
, __LINE__
);
1040 /* mflash is random device, thanx for the noop */
1041 elevator_exit(host
->breq
->elevator
);
1042 err
= elevator_init(host
->breq
, "noop");
1044 printk(KERN_ERR
"%s:%d (elevator_init) fail\n",
1045 __func__
, __LINE__
);
1048 blk_queue_max_sectors(host
->breq
, MG_MAX_SECTS
);
1049 blk_queue_hardsect_size(host
->breq
, MG_SECTOR_SIZE
);
1051 init_timer(&host
->timer
);
1052 host
->timer
.function
= mg_times_out
;
1053 host
->timer
.data
= (unsigned long)host
;
1055 host
->gd
= alloc_disk(MG_DISK_MAX_PART
);
1057 printk(KERN_ERR
"%s:%d (alloc_disk) fail\n",
1058 __func__
, __LINE__
);
1062 host
->gd
->major
= host
->major
;
1063 host
->gd
->first_minor
= 0;
1064 host
->gd
->fops
= &mg_disk_ops
;
1065 host
->gd
->queue
= host
->breq
;
1066 host
->gd
->private_data
= host
;
1067 sprintf(host
->gd
->disk_name
, MG_DISK_NAME
"a");
1069 set_capacity(host
->gd
, host
->n_sectors
);
1076 del_timer_sync(&host
->timer
);
1078 blk_cleanup_queue(host
->breq
);
1080 unregister_blkdev(MG_DISK_MAJ
, MG_DISK_NAME
);
1082 if (!prv_data
->use_polling
)
1083 free_irq(host
->irq
, host
);
1085 gpio_free(host
->rstout
);
1087 gpio_free(host
->rst
);
1089 iounmap(host
->dev_base
);
1096 static int mg_remove(struct platform_device
*plat_dev
)
1098 struct mg_drv_data
*prv_data
= plat_dev
->dev
.platform_data
;
1099 struct mg_host
*host
= prv_data
->host
;
1103 del_timer_sync(&host
->timer
);
1107 del_gendisk(host
->gd
);
1112 blk_cleanup_queue(host
->breq
);
1114 /* unregister blk device */
1115 unregister_blkdev(host
->major
, MG_DISK_NAME
);
1118 if (!prv_data
->use_polling
)
1119 free_irq(host
->irq
, host
);
1121 /* free reset-out pin */
1122 if (prv_data
->dev_attr
!= MG_BOOT_DEV
)
1123 gpio_free(host
->rstout
);
1127 gpio_free(host
->rst
);
1131 iounmap(host
->dev_base
);
1139 static struct platform_driver mg_disk_driver
= {
1141 .remove
= mg_remove
,
1142 .suspend
= mg_suspend
,
1143 .resume
= mg_resume
,
1145 .name
= MG_DEV_NAME
,
1146 .owner
= THIS_MODULE
,
1150 /****************************************************************************
1154 ****************************************************************************/
1156 static int __init
mg_init(void)
1158 printk(KERN_INFO
"mGine mflash driver, (c) 2008 mGine Co.\n");
1159 return platform_driver_register(&mg_disk_driver
);
1162 static void __exit
mg_exit(void)
1164 printk(KERN_INFO
"mflash driver : bye bye\n");
1165 platform_driver_unregister(&mg_disk_driver
);
1168 module_init(mg_init
);
1169 module_exit(mg_exit
);
1171 MODULE_LICENSE("GPL");
1172 MODULE_AUTHOR("unsik Kim <donari75@gmail.com>");
1173 MODULE_DESCRIPTION("mGine m[g]flash device driver");