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)
55 #define MG_STAT_READY (ATA_DRDY | ATA_DSC)
56 #define MG_READY_OK(s) (((s) & (MG_STAT_READY | (ATA_BUSY | ATA_DF | \
57 ATA_ERR))) == MG_STAT_READY)
59 /* error code for others */
61 #define MG_ERR_TIMEOUT 0x100
62 #define MG_ERR_INIT_STAT 0x101
63 #define MG_ERR_TRANSLATION 0x102
64 #define MG_ERR_CTRL_RST 0x103
65 #define MG_ERR_INV_STAT 0x104
66 #define MG_ERR_RSTOUT 0x105
68 #define MG_MAX_ERRORS 6 /* Max read/write errors */
71 #define MG_CMD_RD 0x20
72 #define MG_CMD_WR 0x30
73 #define MG_CMD_SLEEP 0x99
74 #define MG_CMD_WAKEUP 0xC3
75 #define MG_CMD_ID 0xEC
76 #define MG_CMD_WR_CONF 0x3C
77 #define MG_CMD_RD_CONF 0x40
80 #define MG_OP_CASCADE (1 << 0)
81 #define MG_OP_CASCADE_SYNC_RD (1 << 1)
82 #define MG_OP_CASCADE_SYNC_WR (1 << 2)
83 #define MG_OP_INTERLEAVE (1 << 3)
86 #define MG_BURST_LAT_4 (3 << 4)
87 #define MG_BURST_LAT_5 (4 << 4)
88 #define MG_BURST_LAT_6 (5 << 4)
89 #define MG_BURST_LAT_7 (6 << 4)
90 #define MG_BURST_LAT_8 (7 << 4)
91 #define MG_BURST_LEN_4 (1 << 1)
92 #define MG_BURST_LEN_8 (2 << 1)
93 #define MG_BURST_LEN_16 (3 << 1)
94 #define MG_BURST_LEN_32 (4 << 1)
95 #define MG_BURST_LEN_CONT (0 << 1)
97 /* timeout value (unit: ms) */
98 #define MG_TMAX_CONF_TO_CMD 1
99 #define MG_TMAX_WAIT_RD_DRQ 10
100 #define MG_TMAX_WAIT_WR_DRQ 500
101 #define MG_TMAX_RST_TO_BUSY 10
102 #define MG_TMAX_HDRST_TO_RDY 500
103 #define MG_TMAX_SWRST_TO_RDY 500
104 #define MG_TMAX_RSTOUT 3000
106 /* device attribution */
107 /* use mflash as boot device */
108 #define MG_BOOT_DEV (1 << 0)
109 /* use mflash as storage device */
110 #define MG_STORAGE_DEV (1 << 1)
111 /* same as MG_STORAGE_DEV, but bootloader already done reset sequence */
112 #define MG_STORAGE_DEV_SKIP_RST (1 << 2)
114 #define MG_DEV_MASK (MG_BOOT_DEV | MG_STORAGE_DEV | MG_STORAGE_DEV_SKIP_RST)
116 /* names of GPIO resource */
117 #define MG_RST_PIN "mg_rst"
118 /* except MG_BOOT_DEV, reset-out pin should be assigned */
119 #define MG_RSTOUT_PIN "mg_rstout"
121 /* private driver data */
126 /* device attribution */
129 /* internally used */
130 struct mg_host *host;
133 /* main structure for mflash driver */
137 struct request_queue *breq;
141 struct timer_list timer;
142 void (*mg_do_intr) (struct mg_host *);
144 u16 id[ATA_ID_WORDS];
152 void __iomem *dev_base;
162 * Debugging macro and defines
166 # define MG_DBG(fmt, args...) \
167 printk(KERN_DEBUG "%s:%d "fmt, __func__, __LINE__, ##args)
168 #else /* CONFIG_MG_DEBUG */
169 # define MG_DBG(fmt, args...) do { } while (0)
170 #endif /* CONFIG_MG_DEBUG */
172 static void mg_request(struct request_queue *);
174 static void mg_dump_status(const char *msg, unsigned int stat,
175 struct mg_host *host)
177 char *name = MG_DISK_NAME;
181 req = elv_next_request(host->breq);
183 name = req->rq_disk->disk_name;
186 printk(KERN_ERR "%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
190 printk("DriveReady ");
192 printk("WriteFault ");
194 printk("SeekComplete ");
196 printk("DataRequest ");
198 printk("CorrectedError ");
202 if ((stat & ATA_ERR) == 0) {
205 host->error = inb((unsigned long)host->dev_base + MG_REG_ERROR);
206 printk(KERN_ERR "%s: %s: error=0x%02x { ", name, msg,
208 if (host->error & ATA_BBK)
209 printk("BadSector ");
210 if (host->error & ATA_UNC)
211 printk("UncorrectableError ");
212 if (host->error & ATA_IDNF)
213 printk("SectorIdNotFound ");
214 if (host->error & ATA_ABORTED)
215 printk("DriveStatusError ");
216 if (host->error & ATA_AMNF)
217 printk("AddrMarkNotFound ");
219 if (host->error & (ATA_BBK | ATA_UNC | ATA_IDNF | ATA_AMNF)) {
221 req = elv_next_request(host->breq);
223 printk(", sector=%u",
224 (unsigned int)blk_rq_pos(req));
232 static unsigned int mg_wait(struct mg_host *host, u32 expect, u32 msec)
235 unsigned long expire, cur_jiffies;
236 struct mg_drv_data *prv_data = host->dev->platform_data;
238 host->error = MG_ERR_NONE;
239 expire = jiffies + msecs_to_jiffies(msec);
241 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
244 cur_jiffies = jiffies;
245 if (status & ATA_BUSY) {
246 if (expect == ATA_BUSY)
249 /* Check the error condition! */
250 if (status & ATA_ERR) {
251 mg_dump_status("mg_wait", status, host);
255 if (expect == MG_STAT_READY)
256 if (MG_READY_OK(status))
259 if (expect == ATA_DRQ)
260 if (status & ATA_DRQ)
264 mg_dump_status("not ready", status, host);
265 return MG_ERR_INV_STAT;
267 if (prv_data->use_polling)
270 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
271 } while (time_before(cur_jiffies, expire));
273 if (time_after_eq(cur_jiffies, expire) && msec)
274 host->error = MG_ERR_TIMEOUT;
279 static unsigned int mg_wait_rstout(u32 rstout, u32 msec)
281 unsigned long expire;
283 expire = jiffies + msecs_to_jiffies(msec);
284 while (time_before(jiffies, expire)) {
285 if (gpio_get_value(rstout) == 1)
290 return MG_ERR_RSTOUT;
293 static void mg_unexpected_intr(struct mg_host *host)
295 u32 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
297 mg_dump_status("mg_unexpected_intr", status, host);
300 static irqreturn_t mg_irq(int irq, void *dev_id)
302 struct mg_host *host = dev_id;
303 void (*handler)(struct mg_host *) = host->mg_do_intr;
305 spin_lock(&host->lock);
307 host->mg_do_intr = NULL;
308 del_timer(&host->timer);
310 handler = mg_unexpected_intr;
313 spin_unlock(&host->lock);
318 /* local copy of ata_id_string() */
319 static void mg_id_string(const u16 *id, unsigned char *s,
320 unsigned int ofs, unsigned int len)
340 /* local copy of ata_id_c_string() */
341 static void mg_id_c_string(const u16 *id, unsigned char *s,
342 unsigned int ofs, unsigned int len)
346 mg_id_string(id, s, ofs, len - 1);
348 p = s + strnlen(s, len - 1);
349 while (p > s && p[-1] == ' ')
354 static int mg_get_disk_id(struct mg_host *host)
358 const u16 *id = host->id;
359 struct mg_drv_data *prv_data = host->dev->platform_data;
360 char fwrev[ATA_ID_FW_REV_LEN + 1];
361 char model[ATA_ID_PROD_LEN + 1];
362 char serial[ATA_ID_SERNO_LEN + 1];
364 if (!prv_data->use_polling)
365 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
367 outb(MG_CMD_ID, (unsigned long)host->dev_base + MG_REG_COMMAND);
368 err = mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_RD_DRQ);
372 for (i = 0; i < (MG_SECTOR_SIZE >> 1); i++)
373 host->id[i] = le16_to_cpu(inw((unsigned long)host->dev_base +
374 MG_BUFF_OFFSET + i * 2));
376 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
377 err = mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD);
381 if ((id[ATA_ID_FIELD_VALID] & 1) == 0)
382 return MG_ERR_TRANSLATION;
384 host->n_sectors = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
385 host->cyls = id[ATA_ID_CYLS];
386 host->heads = id[ATA_ID_HEADS];
387 host->sectors = id[ATA_ID_SECTORS];
389 if (MG_RES_SEC && host->heads && host->sectors) {
390 /* modify cyls, n_sectors */
391 host->cyls = (host->n_sectors - MG_RES_SEC) /
392 host->heads / host->sectors;
393 host->nres_sectors = host->n_sectors - host->cyls *
394 host->heads * host->sectors;
395 host->n_sectors -= host->nres_sectors;
398 mg_id_c_string(id, fwrev, ATA_ID_FW_REV, sizeof(fwrev));
399 mg_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
400 mg_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
401 printk(KERN_INFO "mg_disk: model: %s\n", model);
402 printk(KERN_INFO "mg_disk: firm: %.8s\n", fwrev);
403 printk(KERN_INFO "mg_disk: serial: %s\n", serial);
404 printk(KERN_INFO "mg_disk: %d + reserved %d sectors\n",
405 host->n_sectors, host->nres_sectors);
407 if (!prv_data->use_polling)
408 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
414 static int mg_disk_init(struct mg_host *host)
416 struct mg_drv_data *prv_data = host->dev->platform_data;
421 gpio_set_value(host->rst, 0);
422 err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
427 gpio_set_value(host->rst, 1);
428 err = mg_wait(host, MG_STAT_READY, MG_TMAX_HDRST_TO_RDY);
433 outb(ATA_SRST | (prv_data->use_polling ? ATA_NIEN : 0),
434 (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
435 err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
440 outb(prv_data->use_polling ? ATA_NIEN : 0,
441 (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
442 err = mg_wait(host, MG_STAT_READY, MG_TMAX_SWRST_TO_RDY);
446 init_status = inb((unsigned long)host->dev_base + MG_REG_STATUS) & 0xf;
448 if (init_status == 0xf)
449 return MG_ERR_INIT_STAT;
454 static void mg_bad_rw_intr(struct mg_host *host)
456 struct request *req = elv_next_request(host->breq);
458 if (++req->errors >= MG_MAX_ERRORS ||
459 host->error == MG_ERR_TIMEOUT)
460 __blk_end_request_cur(req, -EIO);
463 static unsigned int mg_out(struct mg_host *host,
464 unsigned int sect_num,
465 unsigned int sect_cnt,
467 void (*intr_addr)(struct mg_host *))
469 struct mg_drv_data *prv_data = host->dev->platform_data;
471 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
474 if (!prv_data->use_polling) {
475 host->mg_do_intr = intr_addr;
476 mod_timer(&host->timer, jiffies + 3 * HZ);
479 sect_num += MG_RES_SEC;
480 outb((u8)sect_cnt, (unsigned long)host->dev_base + MG_REG_SECT_CNT);
481 outb((u8)sect_num, (unsigned long)host->dev_base + MG_REG_SECT_NUM);
482 outb((u8)(sect_num >> 8), (unsigned long)host->dev_base +
484 outb((u8)(sect_num >> 16), (unsigned long)host->dev_base +
486 outb((u8)((sect_num >> 24) | ATA_LBA | ATA_DEVICE_OBS),
487 (unsigned long)host->dev_base + MG_REG_DRV_HEAD);
488 outb(cmd, (unsigned long)host->dev_base + MG_REG_COMMAND);
492 static void mg_read(struct request *req)
495 struct mg_host *host = req->rq_disk->private_data;
497 if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req),
498 MG_CMD_RD, NULL) != MG_ERR_NONE)
499 mg_bad_rw_intr(host);
501 MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
502 blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
505 u16 *buff = (u16 *)req->buffer;
507 if (mg_wait(host, ATA_DRQ,
508 MG_TMAX_WAIT_RD_DRQ) != MG_ERR_NONE) {
509 mg_bad_rw_intr(host);
512 for (j = 0; j < MG_SECTOR_SIZE >> 1; j++)
513 *buff++ = inw((unsigned long)host->dev_base +
514 MG_BUFF_OFFSET + (j << 1));
516 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base +
518 } while (__blk_end_request(req, 0, MG_SECTOR_SIZE));
521 static void mg_write(struct request *req)
524 struct mg_host *host = req->rq_disk->private_data;
526 if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req),
527 MG_CMD_WR, NULL) != MG_ERR_NONE) {
528 mg_bad_rw_intr(host);
532 MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
533 blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
536 u16 *buff = (u16 *)req->buffer;
538 if (mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
539 mg_bad_rw_intr(host);
542 for (j = 0; j < MG_SECTOR_SIZE >> 1; j++)
543 outw(*buff++, (unsigned long)host->dev_base +
544 MG_BUFF_OFFSET + (j << 1));
546 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
548 } while (__blk_end_request(req, 0, MG_SECTOR_SIZE));
551 static void mg_read_intr(struct mg_host *host)
559 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
567 mg_dump_status("mg_read_intr", i, host);
568 mg_bad_rw_intr(host);
569 mg_request(host->breq);
573 /* get current segment of request */
574 req = elv_next_request(host->breq);
575 buff = (u16 *)req->buffer;
578 for (i = 0; i < MG_SECTOR_SIZE >> 1; i++)
579 *buff++ = inw((unsigned long)host->dev_base + MG_BUFF_OFFSET +
582 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
583 blk_rq_pos(req), blk_rq_sectors(req) - 1, req->buffer);
585 /* send read confirm */
586 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
588 if (__blk_end_request(req, 0, MG_SECTOR_SIZE)) {
589 /* set handler if read remains */
590 host->mg_do_intr = mg_read_intr;
591 mod_timer(&host->timer, jiffies + 3 * HZ);
592 } else /* goto next request */
593 mg_request(host->breq);
596 static void mg_write_intr(struct mg_host *host)
603 /* get current segment of request */
604 req = elv_next_request(host->breq);
608 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
613 if ((blk_rq_sectors(req) <= 1) || (i & ATA_DRQ))
616 mg_dump_status("mg_write_intr", i, host);
617 mg_bad_rw_intr(host);
618 mg_request(host->breq);
622 if ((rem = __blk_end_request(req, 0, MG_SECTOR_SIZE))) {
623 /* write 1 sector and set handler if remains */
624 buff = (u16 *)req->buffer;
625 for (j = 0; j < MG_STORAGE_BUFFER_SIZE >> 1; j++) {
626 outw(*buff, (unsigned long)host->dev_base +
627 MG_BUFF_OFFSET + (j << 1));
630 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
631 blk_rq_pos(req), blk_rq_sectors(req), req->buffer);
632 host->mg_do_intr = mg_write_intr;
633 mod_timer(&host->timer, jiffies + 3 * HZ);
636 /* send write confirm */
637 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
640 mg_request(host->breq);
643 void mg_times_out(unsigned long data)
645 struct mg_host *host = (struct mg_host *)data;
649 spin_lock_irq(&host->lock);
651 req = elv_next_request(host->breq);
655 host->mg_do_intr = NULL;
657 name = req->rq_disk->disk_name;
658 printk(KERN_DEBUG "%s: timeout\n", name);
660 host->error = MG_ERR_TIMEOUT;
661 mg_bad_rw_intr(host);
663 mg_request(host->breq);
665 spin_unlock_irq(&host->lock);
668 static void mg_request_poll(struct request_queue *q)
671 struct mg_host *host;
673 while ((req = elv_next_request(q)) != NULL) {
674 host = req->rq_disk->private_data;
676 if (unlikely(!blk_fs_request(req))) {
677 __blk_end_request_cur(req, -EIO);
681 if (rq_data_dir(req) == READ)
688 static unsigned int mg_issue_req(struct request *req,
689 struct mg_host *host,
690 unsigned int sect_num,
691 unsigned int sect_cnt)
696 switch (rq_data_dir(req)) {
698 if (mg_out(host, sect_num, sect_cnt, MG_CMD_RD, &mg_read_intr)
700 mg_bad_rw_intr(host);
706 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
707 if (mg_out(host, sect_num, sect_cnt, MG_CMD_WR, &mg_write_intr)
709 mg_bad_rw_intr(host);
712 del_timer(&host->timer);
713 mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ);
714 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
716 mg_bad_rw_intr(host);
719 buff = (u16 *)req->buffer;
720 for (i = 0; i < MG_SECTOR_SIZE >> 1; i++) {
721 outw(*buff, (unsigned long)host->dev_base +
722 MG_BUFF_OFFSET + (i << 1));
725 mod_timer(&host->timer, jiffies + 3 * HZ);
726 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
733 /* This function also called from IRQ context */
734 static void mg_request(struct request_queue *q)
737 struct mg_host *host;
738 u32 sect_num, sect_cnt;
741 req = elv_next_request(q);
745 host = req->rq_disk->private_data;
747 /* check unwanted request call */
748 if (host->mg_do_intr)
751 del_timer(&host->timer);
753 sect_num = blk_rq_pos(req);
754 /* deal whole segments */
755 sect_cnt = blk_rq_sectors(req);
758 if (sect_num >= get_capacity(req->rq_disk) ||
759 ((sect_num + sect_cnt) >
760 get_capacity(req->rq_disk))) {
762 "%s: bad access: sector=%d, count=%d\n",
763 req->rq_disk->disk_name,
765 __blk_end_request_cur(req, -EIO);
769 if (unlikely(!blk_fs_request(req))) {
770 __blk_end_request_cur(req, -EIO);
774 if (!mg_issue_req(req, host, sect_num, sect_cnt))
779 static int mg_getgeo(struct block_device *bdev, struct hd_geometry *geo)
781 struct mg_host *host = bdev->bd_disk->private_data;
783 geo->cylinders = (unsigned short)host->cyls;
784 geo->heads = (unsigned char)host->heads;
785 geo->sectors = (unsigned char)host->sectors;
789 static struct block_device_operations mg_disk_ops = {
793 static int mg_suspend(struct platform_device *plat_dev, pm_message_t state)
795 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
796 struct mg_host *host = prv_data->host;
798 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
801 if (!prv_data->use_polling)
802 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
804 outb(MG_CMD_SLEEP, (unsigned long)host->dev_base + MG_REG_COMMAND);
805 /* wait until mflash deep sleep */
808 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) {
809 if (!prv_data->use_polling)
810 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
817 static int mg_resume(struct platform_device *plat_dev)
819 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
820 struct mg_host *host = prv_data->host;
822 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
825 outb(MG_CMD_WAKEUP, (unsigned long)host->dev_base + MG_REG_COMMAND);
826 /* wait until mflash wakeup */
829 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
832 if (!prv_data->use_polling)
833 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
838 static int mg_probe(struct platform_device *plat_dev)
840 struct mg_host *host;
841 struct resource *rsc;
842 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
846 printk(KERN_ERR "%s:%d fail (no driver_data)\n",
853 host = kzalloc(sizeof(struct mg_host), GFP_KERNEL);
855 printk(KERN_ERR "%s:%d fail (no memory for mg_host)\n",
860 host->major = MG_DISK_MAJ;
862 /* link each other */
863 prv_data->host = host;
864 host->dev = &plat_dev->dev;
867 rsc = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
869 printk(KERN_ERR "%s:%d platform_get_resource fail\n",
874 host->dev_base = ioremap(rsc->start , rsc->end + 1);
875 if (!host->dev_base) {
876 printk(KERN_ERR "%s:%d ioremap fail\n",
881 MG_DBG("dev_base = 0x%x\n", (u32)host->dev_base);
884 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
887 printk(KERN_ERR "%s:%d get reset pin fail\n",
892 host->rst = rsc->start;
895 err = gpio_request(host->rst, MG_RST_PIN);
898 gpio_direction_output(host->rst, 1);
901 if (!(prv_data->dev_attr & MG_DEV_MASK))
904 if (prv_data->dev_attr != MG_BOOT_DEV) {
905 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
908 printk(KERN_ERR "%s:%d get reset-out pin fail\n",
913 host->rstout = rsc->start;
914 err = gpio_request(host->rstout, MG_RSTOUT_PIN);
917 gpio_direction_input(host->rstout);
921 if (prv_data->dev_attr == MG_STORAGE_DEV) {
922 /* If POR seq. not yet finised, wait */
923 err = mg_wait_rstout(host->rstout, MG_TMAX_RSTOUT);
926 err = mg_disk_init(host);
928 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
929 __func__, __LINE__, err);
935 /* get irq resource */
936 if (!prv_data->use_polling) {
937 host->irq = platform_get_irq(plat_dev, 0);
938 if (host->irq == -ENXIO) {
942 err = request_irq(host->irq, mg_irq,
943 IRQF_DISABLED | IRQF_TRIGGER_RISING,
946 printk(KERN_ERR "%s:%d fail (request_irq err=%d)\n",
947 __func__, __LINE__, err);
954 err = mg_get_disk_id(host);
956 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
957 __func__, __LINE__, err);
962 err = register_blkdev(host->major, MG_DISK_NAME);
964 printk(KERN_ERR "%s:%d register_blkdev fail (err code : %d)\n",
965 __func__, __LINE__, err);
971 spin_lock_init(&host->lock);
973 if (prv_data->use_polling)
974 host->breq = blk_init_queue(mg_request_poll, &host->lock);
976 host->breq = blk_init_queue(mg_request, &host->lock);
980 printk(KERN_ERR "%s:%d (blk_init_queue) fail\n",
985 /* mflash is random device, thanx for the noop */
986 elevator_exit(host->breq->elevator);
987 err = elevator_init(host->breq, "noop");
989 printk(KERN_ERR "%s:%d (elevator_init) fail\n",
993 blk_queue_max_sectors(host->breq, MG_MAX_SECTS);
994 blk_queue_hardsect_size(host->breq, MG_SECTOR_SIZE);
996 init_timer(&host->timer);
997 host->timer.function = mg_times_out;
998 host->timer.data = (unsigned long)host;
1000 host->gd = alloc_disk(MG_DISK_MAX_PART);
1002 printk(KERN_ERR "%s:%d (alloc_disk) fail\n",
1003 __func__, __LINE__);
1007 host->gd->major = host->major;
1008 host->gd->first_minor = 0;
1009 host->gd->fops = &mg_disk_ops;
1010 host->gd->queue = host->breq;
1011 host->gd->private_data = host;
1012 sprintf(host->gd->disk_name, MG_DISK_NAME"a");
1014 set_capacity(host->gd, host->n_sectors);
1021 del_timer_sync(&host->timer);
1023 blk_cleanup_queue(host->breq);
1025 unregister_blkdev(MG_DISK_MAJ, MG_DISK_NAME);
1027 if (!prv_data->use_polling)
1028 free_irq(host->irq, host);
1030 gpio_free(host->rstout);
1032 gpio_free(host->rst);
1034 iounmap(host->dev_base);
1041 static int mg_remove(struct platform_device *plat_dev)
1043 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
1044 struct mg_host *host = prv_data->host;
1048 del_timer_sync(&host->timer);
1052 del_gendisk(host->gd);
1057 blk_cleanup_queue(host->breq);
1059 /* unregister blk device */
1060 unregister_blkdev(host->major, MG_DISK_NAME);
1063 if (!prv_data->use_polling)
1064 free_irq(host->irq, host);
1066 /* free reset-out pin */
1067 if (prv_data->dev_attr != MG_BOOT_DEV)
1068 gpio_free(host->rstout);
1072 gpio_free(host->rst);
1076 iounmap(host->dev_base);
1084 static struct platform_driver mg_disk_driver = {
1086 .remove = mg_remove,
1087 .suspend = mg_suspend,
1088 .resume = mg_resume,
1090 .name = MG_DEV_NAME,
1091 .owner = THIS_MODULE,
1095 /****************************************************************************
1099 ****************************************************************************/
1101 static int __init mg_init(void)
1103 printk(KERN_INFO "mGine mflash driver, (c) 2008 mGine Co.\n");
1104 return platform_driver_register(&mg_disk_driver);
1107 static void __exit mg_exit(void)
1109 printk(KERN_INFO "mflash driver : bye bye\n");
1110 platform_driver_unregister(&mg_disk_driver);
1113 module_init(mg_init);
1114 module_exit(mg_exit);
1116 MODULE_LICENSE("GPL");
1117 MODULE_AUTHOR("unsik Kim <donari75@gmail.com>");
1118 MODULE_DESCRIPTION("mGine m[g]flash device driver");