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>
25 #include <linux/mg_disk.h>
27 #define MG_RES_SEC (CONFIG_MG_DISK_RES << 1)
29 /* name for block device */
30 #define MG_DISK_NAME "mgd"
33 #define MG_DISK_MAX_PART 16
34 #define MG_SECTOR_SIZE 512
35 #define MG_MAX_SECTS 256
37 /* Register offsets */
38 #define MG_BUFF_OFFSET 0x8000
39 #define MG_REG_OFFSET 0xC000
40 #define MG_REG_FEATURE (MG_REG_OFFSET + 2) /* write case */
41 #define MG_REG_ERROR (MG_REG_OFFSET + 2) /* read case */
42 #define MG_REG_SECT_CNT (MG_REG_OFFSET + 4)
43 #define MG_REG_SECT_NUM (MG_REG_OFFSET + 6)
44 #define MG_REG_CYL_LOW (MG_REG_OFFSET + 8)
45 #define MG_REG_CYL_HIGH (MG_REG_OFFSET + 0xA)
46 #define MG_REG_DRV_HEAD (MG_REG_OFFSET + 0xC)
47 #define MG_REG_COMMAND (MG_REG_OFFSET + 0xE) /* write case */
48 #define MG_REG_STATUS (MG_REG_OFFSET + 0xE) /* read case */
49 #define MG_REG_DRV_CTRL (MG_REG_OFFSET + 0x10)
50 #define MG_REG_BURST_CTRL (MG_REG_OFFSET + 0x12)
53 #define MG_STAT_READY (ATA_DRDY | ATA_DSC)
54 #define MG_READY_OK(s) (((s) & (MG_STAT_READY | (ATA_BUSY | ATA_DF | \
55 ATA_ERR))) == MG_STAT_READY)
57 /* error code for others */
59 #define MG_ERR_TIMEOUT 0x100
60 #define MG_ERR_INIT_STAT 0x101
61 #define MG_ERR_TRANSLATION 0x102
62 #define MG_ERR_CTRL_RST 0x103
63 #define MG_ERR_INV_STAT 0x104
64 #define MG_ERR_RSTOUT 0x105
66 #define MG_MAX_ERRORS 6 /* Max read/write errors */
69 #define MG_CMD_RD 0x20
70 #define MG_CMD_WR 0x30
71 #define MG_CMD_SLEEP 0x99
72 #define MG_CMD_WAKEUP 0xC3
73 #define MG_CMD_ID 0xEC
74 #define MG_CMD_WR_CONF 0x3C
75 #define MG_CMD_RD_CONF 0x40
78 #define MG_OP_CASCADE (1 << 0)
79 #define MG_OP_CASCADE_SYNC_RD (1 << 1)
80 #define MG_OP_CASCADE_SYNC_WR (1 << 2)
81 #define MG_OP_INTERLEAVE (1 << 3)
84 #define MG_BURST_LAT_4 (3 << 4)
85 #define MG_BURST_LAT_5 (4 << 4)
86 #define MG_BURST_LAT_6 (5 << 4)
87 #define MG_BURST_LAT_7 (6 << 4)
88 #define MG_BURST_LAT_8 (7 << 4)
89 #define MG_BURST_LEN_4 (1 << 1)
90 #define MG_BURST_LEN_8 (2 << 1)
91 #define MG_BURST_LEN_16 (3 << 1)
92 #define MG_BURST_LEN_32 (4 << 1)
93 #define MG_BURST_LEN_CONT (0 << 1)
95 /* timeout value (unit: ms) */
96 #define MG_TMAX_CONF_TO_CMD 1
97 #define MG_TMAX_WAIT_RD_DRQ 10
98 #define MG_TMAX_WAIT_WR_DRQ 500
99 #define MG_TMAX_RST_TO_BUSY 10
100 #define MG_TMAX_HDRST_TO_RDY 500
101 #define MG_TMAX_SWRST_TO_RDY 500
102 #define MG_TMAX_RSTOUT 3000
104 #define MG_DEV_MASK (MG_BOOT_DEV | MG_STORAGE_DEV | MG_STORAGE_DEV_SKIP_RST)
106 /* main structure for mflash driver */
110 struct request_queue *breq;
115 struct timer_list timer;
116 void (*mg_do_intr) (struct mg_host *);
118 u16 id[ATA_ID_WORDS];
126 void __iomem *dev_base;
136 * Debugging macro and defines
140 # define MG_DBG(fmt, args...) \
141 printk(KERN_DEBUG "%s:%d "fmt, __func__, __LINE__, ##args)
142 #else /* CONFIG_MG_DEBUG */
143 # define MG_DBG(fmt, args...) do { } while (0)
144 #endif /* CONFIG_MG_DEBUG */
146 static void mg_request(struct request_queue *);
148 static bool mg_end_request(struct mg_host *host, int err, unsigned int nr_bytes)
150 if (__blk_end_request(host->req, err, nr_bytes))
157 static bool mg_end_request_cur(struct mg_host *host, int err)
159 return mg_end_request(host, err, blk_rq_cur_bytes(host->req));
162 static void mg_dump_status(const char *msg, unsigned int stat,
163 struct mg_host *host)
165 char *name = MG_DISK_NAME;
168 name = host->req->rq_disk->disk_name;
170 printk(KERN_ERR "%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
174 printk("DriveReady ");
176 printk("WriteFault ");
178 printk("SeekComplete ");
180 printk("DataRequest ");
182 printk("CorrectedError ");
186 if ((stat & ATA_ERR) == 0) {
189 host->error = inb((unsigned long)host->dev_base + MG_REG_ERROR);
190 printk(KERN_ERR "%s: %s: error=0x%02x { ", name, msg,
192 if (host->error & ATA_BBK)
193 printk("BadSector ");
194 if (host->error & ATA_UNC)
195 printk("UncorrectableError ");
196 if (host->error & ATA_IDNF)
197 printk("SectorIdNotFound ");
198 if (host->error & ATA_ABORTED)
199 printk("DriveStatusError ");
200 if (host->error & ATA_AMNF)
201 printk("AddrMarkNotFound ");
203 if (host->error & (ATA_BBK | ATA_UNC | ATA_IDNF | ATA_AMNF)) {
205 printk(", sector=%u",
206 (unsigned int)blk_rq_pos(host->req));
212 static unsigned int mg_wait(struct mg_host *host, u32 expect, u32 msec)
215 unsigned long expire, cur_jiffies;
216 struct mg_drv_data *prv_data = host->dev->platform_data;
218 host->error = MG_ERR_NONE;
219 expire = jiffies + msecs_to_jiffies(msec);
221 /* These 2 times dummy status read prevents reading invalid
222 * status. A very little time (3 times of mflash operating clk)
223 * is required for busy bit is set. Use dummy read instead of
224 * busy wait, because mflash's PLL is machine dependent.
226 if (prv_data->use_polling) {
227 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
228 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
231 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
234 cur_jiffies = jiffies;
235 if (status & ATA_BUSY) {
236 if (expect == ATA_BUSY)
239 /* Check the error condition! */
240 if (status & ATA_ERR) {
241 mg_dump_status("mg_wait", status, host);
245 if (expect == MG_STAT_READY)
246 if (MG_READY_OK(status))
249 if (expect == ATA_DRQ)
250 if (status & ATA_DRQ)
254 mg_dump_status("not ready", status, host);
255 return MG_ERR_INV_STAT;
258 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
259 } while (time_before(cur_jiffies, expire));
261 if (time_after_eq(cur_jiffies, expire) && msec)
262 host->error = MG_ERR_TIMEOUT;
267 static unsigned int mg_wait_rstout(u32 rstout, u32 msec)
269 unsigned long expire;
271 expire = jiffies + msecs_to_jiffies(msec);
272 while (time_before(jiffies, expire)) {
273 if (gpio_get_value(rstout) == 1)
278 return MG_ERR_RSTOUT;
281 static void mg_unexpected_intr(struct mg_host *host)
283 u32 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
285 mg_dump_status("mg_unexpected_intr", status, host);
288 static irqreturn_t mg_irq(int irq, void *dev_id)
290 struct mg_host *host = dev_id;
291 void (*handler)(struct mg_host *) = host->mg_do_intr;
293 spin_lock(&host->lock);
295 host->mg_do_intr = NULL;
296 del_timer(&host->timer);
298 handler = mg_unexpected_intr;
301 spin_unlock(&host->lock);
306 /* local copy of ata_id_string() */
307 static void mg_id_string(const u16 *id, unsigned char *s,
308 unsigned int ofs, unsigned int len)
328 /* local copy of ata_id_c_string() */
329 static void mg_id_c_string(const u16 *id, unsigned char *s,
330 unsigned int ofs, unsigned int len)
334 mg_id_string(id, s, ofs, len - 1);
336 p = s + strnlen(s, len - 1);
337 while (p > s && p[-1] == ' ')
342 static int mg_get_disk_id(struct mg_host *host)
346 const u16 *id = host->id;
347 struct mg_drv_data *prv_data = host->dev->platform_data;
348 char fwrev[ATA_ID_FW_REV_LEN + 1];
349 char model[ATA_ID_PROD_LEN + 1];
350 char serial[ATA_ID_SERNO_LEN + 1];
352 if (!prv_data->use_polling)
353 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
355 outb(MG_CMD_ID, (unsigned long)host->dev_base + MG_REG_COMMAND);
356 err = mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_RD_DRQ);
360 for (i = 0; i < (MG_SECTOR_SIZE >> 1); i++)
361 host->id[i] = le16_to_cpu(inw((unsigned long)host->dev_base +
362 MG_BUFF_OFFSET + i * 2));
364 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
365 err = mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD);
369 if ((id[ATA_ID_FIELD_VALID] & 1) == 0)
370 return MG_ERR_TRANSLATION;
372 host->n_sectors = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
373 host->cyls = id[ATA_ID_CYLS];
374 host->heads = id[ATA_ID_HEADS];
375 host->sectors = id[ATA_ID_SECTORS];
377 if (MG_RES_SEC && host->heads && host->sectors) {
378 /* modify cyls, n_sectors */
379 host->cyls = (host->n_sectors - MG_RES_SEC) /
380 host->heads / host->sectors;
381 host->nres_sectors = host->n_sectors - host->cyls *
382 host->heads * host->sectors;
383 host->n_sectors -= host->nres_sectors;
386 mg_id_c_string(id, fwrev, ATA_ID_FW_REV, sizeof(fwrev));
387 mg_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
388 mg_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
389 printk(KERN_INFO "mg_disk: model: %s\n", model);
390 printk(KERN_INFO "mg_disk: firm: %.8s\n", fwrev);
391 printk(KERN_INFO "mg_disk: serial: %s\n", serial);
392 printk(KERN_INFO "mg_disk: %d + reserved %d sectors\n",
393 host->n_sectors, host->nres_sectors);
395 if (!prv_data->use_polling)
396 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
402 static int mg_disk_init(struct mg_host *host)
404 struct mg_drv_data *prv_data = host->dev->platform_data;
409 gpio_set_value(host->rst, 0);
410 err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
415 gpio_set_value(host->rst, 1);
416 err = mg_wait(host, MG_STAT_READY, MG_TMAX_HDRST_TO_RDY);
421 outb(ATA_SRST | (prv_data->use_polling ? ATA_NIEN : 0),
422 (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
423 err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
428 outb(prv_data->use_polling ? ATA_NIEN : 0,
429 (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
430 err = mg_wait(host, MG_STAT_READY, MG_TMAX_SWRST_TO_RDY);
434 init_status = inb((unsigned long)host->dev_base + MG_REG_STATUS) & 0xf;
436 if (init_status == 0xf)
437 return MG_ERR_INIT_STAT;
442 static void mg_bad_rw_intr(struct mg_host *host)
445 if (++host->req->errors >= MG_MAX_ERRORS ||
446 host->error == MG_ERR_TIMEOUT)
447 mg_end_request_cur(host, -EIO);
450 static unsigned int mg_out(struct mg_host *host,
451 unsigned int sect_num,
452 unsigned int sect_cnt,
454 void (*intr_addr)(struct mg_host *))
456 struct mg_drv_data *prv_data = host->dev->platform_data;
458 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
461 if (!prv_data->use_polling) {
462 host->mg_do_intr = intr_addr;
463 mod_timer(&host->timer, jiffies + 3 * HZ);
466 sect_num += MG_RES_SEC;
467 outb((u8)sect_cnt, (unsigned long)host->dev_base + MG_REG_SECT_CNT);
468 outb((u8)sect_num, (unsigned long)host->dev_base + MG_REG_SECT_NUM);
469 outb((u8)(sect_num >> 8), (unsigned long)host->dev_base +
471 outb((u8)(sect_num >> 16), (unsigned long)host->dev_base +
473 outb((u8)((sect_num >> 24) | ATA_LBA | ATA_DEVICE_OBS),
474 (unsigned long)host->dev_base + MG_REG_DRV_HEAD);
475 outb(cmd, (unsigned long)host->dev_base + MG_REG_COMMAND);
479 static void mg_read_one(struct mg_host *host, struct request *req)
481 u16 *buff = (u16 *)req->buffer;
484 for (i = 0; i < MG_SECTOR_SIZE >> 1; i++)
485 *buff++ = inw((unsigned long)host->dev_base + MG_BUFF_OFFSET +
489 static void mg_read(struct request *req)
491 struct mg_host *host = req->rq_disk->private_data;
493 if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req),
494 MG_CMD_RD, NULL) != MG_ERR_NONE)
495 mg_bad_rw_intr(host);
497 MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
498 blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
501 if (mg_wait(host, ATA_DRQ,
502 MG_TMAX_WAIT_RD_DRQ) != MG_ERR_NONE) {
503 mg_bad_rw_intr(host);
507 mg_read_one(host, req);
509 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base +
511 } while (mg_end_request(host, 0, MG_SECTOR_SIZE));
514 static void mg_write_one(struct mg_host *host, struct request *req)
516 u16 *buff = (u16 *)req->buffer;
519 for (i = 0; i < MG_SECTOR_SIZE >> 1; i++)
520 outw(*buff++, (unsigned long)host->dev_base + MG_BUFF_OFFSET +
524 static void mg_write(struct request *req)
526 struct mg_host *host = req->rq_disk->private_data;
529 if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req),
530 MG_CMD_WR, NULL) != MG_ERR_NONE) {
531 mg_bad_rw_intr(host);
535 MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
536 blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
538 if (mg_wait(host, ATA_DRQ,
539 MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
540 mg_bad_rw_intr(host);
544 mg_write_one(host, req);
546 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
549 if (blk_rq_sectors(req) > 1 &&
550 mg_wait(host, ATA_DRQ,
551 MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
552 mg_bad_rw_intr(host);
556 rem = mg_end_request(host, 0, MG_SECTOR_SIZE);
558 mg_write_one(host, req);
561 (unsigned long)host->dev_base + MG_REG_COMMAND);
565 static void mg_read_intr(struct mg_host *host)
567 struct request *req = host->req;
572 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
580 mg_dump_status("mg_read_intr", i, host);
581 mg_bad_rw_intr(host);
582 mg_request(host->breq);
586 mg_read_one(host, req);
588 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
589 blk_rq_pos(req), blk_rq_sectors(req) - 1, req->buffer);
591 /* send read confirm */
592 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
594 if (mg_end_request(host, 0, MG_SECTOR_SIZE)) {
595 /* set handler if read remains */
596 host->mg_do_intr = mg_read_intr;
597 mod_timer(&host->timer, jiffies + 3 * HZ);
598 } else /* goto next request */
599 mg_request(host->breq);
602 static void mg_write_intr(struct mg_host *host)
604 struct request *req = host->req;
610 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
615 if ((blk_rq_sectors(req) <= 1) || (i & ATA_DRQ))
618 mg_dump_status("mg_write_intr", i, host);
619 mg_bad_rw_intr(host);
620 mg_request(host->breq);
624 if ((rem = mg_end_request(host, 0, MG_SECTOR_SIZE))) {
625 /* write 1 sector and set handler if remains */
626 mg_write_one(host, req);
627 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
628 blk_rq_pos(req), blk_rq_sectors(req), req->buffer);
629 host->mg_do_intr = mg_write_intr;
630 mod_timer(&host->timer, jiffies + 3 * HZ);
633 /* send write confirm */
634 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
637 mg_request(host->breq);
640 void mg_times_out(unsigned long data)
642 struct mg_host *host = (struct mg_host *)data;
645 spin_lock_irq(&host->lock);
650 host->mg_do_intr = NULL;
652 name = host->req->rq_disk->disk_name;
653 printk(KERN_DEBUG "%s: timeout\n", name);
655 host->error = MG_ERR_TIMEOUT;
656 mg_bad_rw_intr(host);
659 mg_request(host->breq);
660 spin_unlock_irq(&host->lock);
663 static void mg_request_poll(struct request_queue *q)
665 struct mg_host *host = q->queuedata;
669 host->req = blk_fetch_request(q);
674 if (unlikely(!blk_fs_request(host->req))) {
675 mg_end_request_cur(host, -EIO);
679 if (rq_data_dir(host->req) == READ)
686 static unsigned int mg_issue_req(struct request *req,
687 struct mg_host *host,
688 unsigned int sect_num,
689 unsigned int sect_cnt)
691 switch (rq_data_dir(req)) {
693 if (mg_out(host, sect_num, sect_cnt, MG_CMD_RD, &mg_read_intr)
695 mg_bad_rw_intr(host);
701 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
702 if (mg_out(host, sect_num, sect_cnt, MG_CMD_WR, &mg_write_intr)
704 mg_bad_rw_intr(host);
707 del_timer(&host->timer);
708 mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ);
709 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
711 mg_bad_rw_intr(host);
714 mg_write_one(host, req);
715 mod_timer(&host->timer, jiffies + 3 * HZ);
716 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
723 /* This function also called from IRQ context */
724 static void mg_request(struct request_queue *q)
726 struct mg_host *host = q->queuedata;
728 u32 sect_num, sect_cnt;
732 host->req = blk_fetch_request(q);
738 /* check unwanted request call */
739 if (host->mg_do_intr)
742 del_timer(&host->timer);
744 sect_num = blk_rq_pos(req);
745 /* deal whole segments */
746 sect_cnt = blk_rq_sectors(req);
749 if (sect_num >= get_capacity(req->rq_disk) ||
750 ((sect_num + sect_cnt) >
751 get_capacity(req->rq_disk))) {
753 "%s: bad access: sector=%d, count=%d\n",
754 req->rq_disk->disk_name,
756 mg_end_request_cur(host, -EIO);
760 if (unlikely(!blk_fs_request(req))) {
761 mg_end_request_cur(host, -EIO);
765 if (!mg_issue_req(req, host, sect_num, sect_cnt))
770 static int mg_getgeo(struct block_device *bdev, struct hd_geometry *geo)
772 struct mg_host *host = bdev->bd_disk->private_data;
774 geo->cylinders = (unsigned short)host->cyls;
775 geo->heads = (unsigned char)host->heads;
776 geo->sectors = (unsigned char)host->sectors;
780 static struct block_device_operations mg_disk_ops = {
784 static int mg_suspend(struct platform_device *plat_dev, pm_message_t state)
786 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
787 struct mg_host *host = prv_data->host;
789 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
792 if (!prv_data->use_polling)
793 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
795 outb(MG_CMD_SLEEP, (unsigned long)host->dev_base + MG_REG_COMMAND);
796 /* wait until mflash deep sleep */
799 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) {
800 if (!prv_data->use_polling)
801 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
808 static int mg_resume(struct platform_device *plat_dev)
810 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
811 struct mg_host *host = prv_data->host;
813 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
816 outb(MG_CMD_WAKEUP, (unsigned long)host->dev_base + MG_REG_COMMAND);
817 /* wait until mflash wakeup */
820 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
823 if (!prv_data->use_polling)
824 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
829 static int mg_probe(struct platform_device *plat_dev)
831 struct mg_host *host;
832 struct resource *rsc;
833 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
837 printk(KERN_ERR "%s:%d fail (no driver_data)\n",
844 host = kzalloc(sizeof(struct mg_host), GFP_KERNEL);
846 printk(KERN_ERR "%s:%d fail (no memory for mg_host)\n",
851 host->major = MG_DISK_MAJ;
853 /* link each other */
854 prv_data->host = host;
855 host->dev = &plat_dev->dev;
858 rsc = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
860 printk(KERN_ERR "%s:%d platform_get_resource fail\n",
865 host->dev_base = ioremap(rsc->start , rsc->end + 1);
866 if (!host->dev_base) {
867 printk(KERN_ERR "%s:%d ioremap fail\n",
872 MG_DBG("dev_base = 0x%x\n", (u32)host->dev_base);
875 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
878 printk(KERN_ERR "%s:%d get reset pin fail\n",
883 host->rst = rsc->start;
886 err = gpio_request(host->rst, MG_RST_PIN);
889 gpio_direction_output(host->rst, 1);
892 if (!(prv_data->dev_attr & MG_DEV_MASK))
895 if (prv_data->dev_attr != MG_BOOT_DEV) {
896 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
899 printk(KERN_ERR "%s:%d get reset-out pin fail\n",
904 host->rstout = rsc->start;
905 err = gpio_request(host->rstout, MG_RSTOUT_PIN);
908 gpio_direction_input(host->rstout);
912 if (prv_data->dev_attr == MG_STORAGE_DEV) {
913 /* If POR seq. not yet finised, wait */
914 err = mg_wait_rstout(host->rstout, MG_TMAX_RSTOUT);
917 err = mg_disk_init(host);
919 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
920 __func__, __LINE__, err);
926 /* get irq resource */
927 if (!prv_data->use_polling) {
928 host->irq = platform_get_irq(plat_dev, 0);
929 if (host->irq == -ENXIO) {
933 err = request_irq(host->irq, mg_irq,
934 IRQF_DISABLED | IRQF_TRIGGER_RISING,
937 printk(KERN_ERR "%s:%d fail (request_irq err=%d)\n",
938 __func__, __LINE__, err);
945 err = mg_get_disk_id(host);
947 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
948 __func__, __LINE__, err);
953 err = register_blkdev(host->major, MG_DISK_NAME);
955 printk(KERN_ERR "%s:%d register_blkdev fail (err code : %d)\n",
956 __func__, __LINE__, err);
962 spin_lock_init(&host->lock);
964 if (prv_data->use_polling)
965 host->breq = blk_init_queue(mg_request_poll, &host->lock);
967 host->breq = blk_init_queue(mg_request, &host->lock);
971 printk(KERN_ERR "%s:%d (blk_init_queue) fail\n",
975 host->breq->queuedata = host;
977 /* mflash is random device, thanx for the noop */
978 elevator_exit(host->breq->elevator);
979 err = elevator_init(host->breq, "noop");
981 printk(KERN_ERR "%s:%d (elevator_init) fail\n",
985 blk_queue_max_sectors(host->breq, MG_MAX_SECTS);
986 blk_queue_logical_block_size(host->breq, MG_SECTOR_SIZE);
988 init_timer(&host->timer);
989 host->timer.function = mg_times_out;
990 host->timer.data = (unsigned long)host;
992 host->gd = alloc_disk(MG_DISK_MAX_PART);
994 printk(KERN_ERR "%s:%d (alloc_disk) fail\n",
999 host->gd->major = host->major;
1000 host->gd->first_minor = 0;
1001 host->gd->fops = &mg_disk_ops;
1002 host->gd->queue = host->breq;
1003 host->gd->private_data = host;
1004 sprintf(host->gd->disk_name, MG_DISK_NAME"a");
1006 set_capacity(host->gd, host->n_sectors);
1013 del_timer_sync(&host->timer);
1015 blk_cleanup_queue(host->breq);
1017 unregister_blkdev(MG_DISK_MAJ, MG_DISK_NAME);
1019 if (!prv_data->use_polling)
1020 free_irq(host->irq, host);
1022 gpio_free(host->rstout);
1024 gpio_free(host->rst);
1026 iounmap(host->dev_base);
1033 static int mg_remove(struct platform_device *plat_dev)
1035 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
1036 struct mg_host *host = prv_data->host;
1040 del_timer_sync(&host->timer);
1044 del_gendisk(host->gd);
1049 blk_cleanup_queue(host->breq);
1051 /* unregister blk device */
1052 unregister_blkdev(host->major, MG_DISK_NAME);
1055 if (!prv_data->use_polling)
1056 free_irq(host->irq, host);
1058 /* free reset-out pin */
1059 if (prv_data->dev_attr != MG_BOOT_DEV)
1060 gpio_free(host->rstout);
1064 gpio_free(host->rst);
1068 iounmap(host->dev_base);
1076 static struct platform_driver mg_disk_driver = {
1078 .remove = mg_remove,
1079 .suspend = mg_suspend,
1080 .resume = mg_resume,
1082 .name = MG_DEV_NAME,
1083 .owner = THIS_MODULE,
1087 /****************************************************************************
1091 ****************************************************************************/
1093 static int __init mg_init(void)
1095 printk(KERN_INFO "mGine mflash driver, (c) 2008 mGine Co.\n");
1096 return platform_driver_register(&mg_disk_driver);
1099 static void __exit mg_exit(void)
1101 printk(KERN_INFO "mflash driver : bye bye\n");
1102 platform_driver_unregister(&mg_disk_driver);
1105 module_init(mg_init);
1106 module_exit(mg_exit);
1108 MODULE_LICENSE("GPL");
1109 MODULE_AUTHOR("unsik Kim <donari75@gmail.com>");
1110 MODULE_DESCRIPTION("mGine m[g]flash device driver");