ACPI: pci_root: simplify list traversals
[linux-2.6/linux-acpi-2.6.git] / drivers / block / mg_disk.c
blobf3898353d0a8f9dc6d328ace0e06689ae366c214
1 /*
2 * drivers/block/mg_disk.c
4 * Support for the mGine m[g]flash IO mode.
5 * Based on legacy hd.c
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>
17 #include <linux/fs.h>
18 #include <linux/blkdev.h>
19 #include <linux/hdreg.h>
20 #include <linux/libata.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 static void mg_request(struct request_queue *);
31 static void mg_dump_status(const char *msg, unsigned int stat,
32 struct mg_host *host)
34 char *name = MG_DISK_NAME;
35 struct request *req;
37 if (host->breq) {
38 req = elv_next_request(host->breq);
39 if (req)
40 name = req->rq_disk->disk_name;
43 printk(KERN_ERR "%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
44 if (stat & MG_REG_STATUS_BIT_BUSY)
45 printk("Busy ");
46 if (stat & MG_REG_STATUS_BIT_READY)
47 printk("DriveReady ");
48 if (stat & MG_REG_STATUS_BIT_WRITE_FAULT)
49 printk("WriteFault ");
50 if (stat & MG_REG_STATUS_BIT_SEEK_DONE)
51 printk("SeekComplete ");
52 if (stat & MG_REG_STATUS_BIT_DATA_REQ)
53 printk("DataRequest ");
54 if (stat & MG_REG_STATUS_BIT_CORRECTED_ERROR)
55 printk("CorrectedError ");
56 if (stat & MG_REG_STATUS_BIT_ERROR)
57 printk("Error ");
58 printk("}\n");
59 if ((stat & MG_REG_STATUS_BIT_ERROR) == 0) {
60 host->error = 0;
61 } else {
62 host->error = inb((unsigned long)host->dev_base + MG_REG_ERROR);
63 printk(KERN_ERR "%s: %s: error=0x%02x { ", name, msg,
64 host->error & 0xff);
65 if (host->error & MG_REG_ERR_BBK)
66 printk("BadSector ");
67 if (host->error & MG_REG_ERR_UNC)
68 printk("UncorrectableError ");
69 if (host->error & MG_REG_ERR_IDNF)
70 printk("SectorIdNotFound ");
71 if (host->error & MG_REG_ERR_ABRT)
72 printk("DriveStatusError ");
73 if (host->error & MG_REG_ERR_AMNF)
74 printk("AddrMarkNotFound ");
75 printk("}");
76 if (host->error &
77 (MG_REG_ERR_BBK | MG_REG_ERR_UNC |
78 MG_REG_ERR_IDNF | MG_REG_ERR_AMNF)) {
79 if (host->breq) {
80 req = elv_next_request(host->breq);
81 if (req)
82 printk(", sector=%u", (u32)req->sector);
86 printk("\n");
90 static unsigned int mg_wait(struct mg_host *host, u32 expect, u32 msec)
92 u8 status;
93 unsigned long expire, cur_jiffies;
94 struct mg_drv_data *prv_data = host->dev->platform_data;
96 host->error = MG_ERR_NONE;
97 expire = jiffies + msecs_to_jiffies(msec);
99 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
101 do {
102 cur_jiffies = jiffies;
103 if (status & MG_REG_STATUS_BIT_BUSY) {
104 if (expect == MG_REG_STATUS_BIT_BUSY)
105 break;
106 } else {
107 /* Check the error condition! */
108 if (status & MG_REG_STATUS_BIT_ERROR) {
109 mg_dump_status("mg_wait", status, host);
110 break;
113 if (expect == MG_STAT_READY)
114 if (MG_READY_OK(status))
115 break;
117 if (expect == MG_REG_STATUS_BIT_DATA_REQ)
118 if (status & MG_REG_STATUS_BIT_DATA_REQ)
119 break;
121 if (!msec) {
122 mg_dump_status("not ready", status, host);
123 return MG_ERR_INV_STAT;
125 if (prv_data->use_polling)
126 msleep(1);
128 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
129 } while (time_before(cur_jiffies, expire));
131 if (time_after_eq(cur_jiffies, expire) && msec)
132 host->error = MG_ERR_TIMEOUT;
134 return host->error;
137 static unsigned int mg_wait_rstout(u32 rstout, u32 msec)
139 unsigned long expire;
141 expire = jiffies + msecs_to_jiffies(msec);
142 while (time_before(jiffies, expire)) {
143 if (gpio_get_value(rstout) == 1)
144 return MG_ERR_NONE;
145 msleep(10);
148 return MG_ERR_RSTOUT;
151 static void mg_unexpected_intr(struct mg_host *host)
153 u32 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
155 mg_dump_status("mg_unexpected_intr", status, host);
158 static irqreturn_t mg_irq(int irq, void *dev_id)
160 struct mg_host *host = dev_id;
161 void (*handler)(struct mg_host *) = host->mg_do_intr;
163 spin_lock(&host->lock);
165 host->mg_do_intr = NULL;
166 del_timer(&host->timer);
167 if (!handler)
168 handler = mg_unexpected_intr;
169 handler(host);
171 spin_unlock(&host->lock);
173 return IRQ_HANDLED;
176 static int mg_get_disk_id(struct mg_host *host)
178 u32 i;
179 s32 err;
180 const u16 *id = host->id;
181 struct mg_drv_data *prv_data = host->dev->platform_data;
182 char fwrev[ATA_ID_FW_REV_LEN + 1];
183 char model[ATA_ID_PROD_LEN + 1];
184 char serial[ATA_ID_SERNO_LEN + 1];
186 if (!prv_data->use_polling)
187 outb(MG_REG_CTRL_INTR_DISABLE,
188 (unsigned long)host->dev_base +
189 MG_REG_DRV_CTRL);
191 outb(MG_CMD_ID, (unsigned long)host->dev_base + MG_REG_COMMAND);
192 err = mg_wait(host, MG_REG_STATUS_BIT_DATA_REQ, MG_TMAX_WAIT_RD_DRQ);
193 if (err)
194 return err;
196 for (i = 0; i < (MG_SECTOR_SIZE >> 1); i++)
197 host->id[i] = le16_to_cpu(inw((unsigned long)host->dev_base +
198 MG_BUFF_OFFSET + i * 2));
200 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
201 err = mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD);
202 if (err)
203 return err;
205 if ((id[ATA_ID_FIELD_VALID] & 1) == 0)
206 return MG_ERR_TRANSLATION;
208 host->n_sectors = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
209 host->cyls = id[ATA_ID_CYLS];
210 host->heads = id[ATA_ID_HEADS];
211 host->sectors = id[ATA_ID_SECTORS];
213 if (MG_RES_SEC && host->heads && host->sectors) {
214 /* modify cyls, n_sectors */
215 host->cyls = (host->n_sectors - MG_RES_SEC) /
216 host->heads / host->sectors;
217 host->nres_sectors = host->n_sectors - host->cyls *
218 host->heads * host->sectors;
219 host->n_sectors -= host->nres_sectors;
222 ata_id_c_string(id, fwrev, ATA_ID_FW_REV, sizeof(fwrev));
223 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
224 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
225 printk(KERN_INFO "mg_disk: model: %s\n", model);
226 printk(KERN_INFO "mg_disk: firm: %.8s\n", fwrev);
227 printk(KERN_INFO "mg_disk: serial: %s\n", serial);
228 printk(KERN_INFO "mg_disk: %d + reserved %d sectors\n",
229 host->n_sectors, host->nres_sectors);
231 if (!prv_data->use_polling)
232 outb(MG_REG_CTRL_INTR_ENABLE, (unsigned long)host->dev_base +
233 MG_REG_DRV_CTRL);
235 return err;
239 static int mg_disk_init(struct mg_host *host)
241 struct mg_drv_data *prv_data = host->dev->platform_data;
242 s32 err;
243 u8 init_status;
245 /* hdd rst low */
246 gpio_set_value(host->rst, 0);
247 err = mg_wait(host, MG_REG_STATUS_BIT_BUSY, MG_TMAX_RST_TO_BUSY);
248 if (err)
249 return err;
251 /* hdd rst high */
252 gpio_set_value(host->rst, 1);
253 err = mg_wait(host, MG_STAT_READY, MG_TMAX_HDRST_TO_RDY);
254 if (err)
255 return err;
257 /* soft reset on */
258 outb(MG_REG_CTRL_RESET |
259 (prv_data->use_polling ? MG_REG_CTRL_INTR_DISABLE :
260 MG_REG_CTRL_INTR_ENABLE),
261 (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
262 err = mg_wait(host, MG_REG_STATUS_BIT_BUSY, MG_TMAX_RST_TO_BUSY);
263 if (err)
264 return err;
266 /* soft reset off */
267 outb(prv_data->use_polling ? MG_REG_CTRL_INTR_DISABLE :
268 MG_REG_CTRL_INTR_ENABLE,
269 (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
270 err = mg_wait(host, MG_STAT_READY, MG_TMAX_SWRST_TO_RDY);
271 if (err)
272 return err;
274 init_status = inb((unsigned long)host->dev_base + MG_REG_STATUS) & 0xf;
276 if (init_status == 0xf)
277 return MG_ERR_INIT_STAT;
279 return err;
282 static void mg_bad_rw_intr(struct mg_host *host)
284 struct request *req = elv_next_request(host->breq);
285 if (req != NULL)
286 if (++req->errors >= MG_MAX_ERRORS ||
287 host->error == MG_ERR_TIMEOUT)
288 end_request(req, 0);
291 static unsigned int mg_out(struct mg_host *host,
292 unsigned int sect_num,
293 unsigned int sect_cnt,
294 unsigned int cmd,
295 void (*intr_addr)(struct mg_host *))
297 struct mg_drv_data *prv_data = host->dev->platform_data;
299 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
300 return host->error;
302 if (!prv_data->use_polling) {
303 host->mg_do_intr = intr_addr;
304 mod_timer(&host->timer, jiffies + 3 * HZ);
306 if (MG_RES_SEC)
307 sect_num += MG_RES_SEC;
308 outb((u8)sect_cnt, (unsigned long)host->dev_base + MG_REG_SECT_CNT);
309 outb((u8)sect_num, (unsigned long)host->dev_base + MG_REG_SECT_NUM);
310 outb((u8)(sect_num >> 8), (unsigned long)host->dev_base +
311 MG_REG_CYL_LOW);
312 outb((u8)(sect_num >> 16), (unsigned long)host->dev_base +
313 MG_REG_CYL_HIGH);
314 outb((u8)((sect_num >> 24) | MG_REG_HEAD_LBA_MODE),
315 (unsigned long)host->dev_base + MG_REG_DRV_HEAD);
316 outb(cmd, (unsigned long)host->dev_base + MG_REG_COMMAND);
317 return MG_ERR_NONE;
320 static void mg_read(struct request *req)
322 u32 remains, j;
323 struct mg_host *host = req->rq_disk->private_data;
325 remains = req->nr_sectors;
327 if (mg_out(host, req->sector, req->nr_sectors, MG_CMD_RD, NULL) !=
328 MG_ERR_NONE)
329 mg_bad_rw_intr(host);
331 MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
332 remains, req->sector, req->buffer);
334 while (remains) {
335 if (mg_wait(host, MG_REG_STATUS_BIT_DATA_REQ,
336 MG_TMAX_WAIT_RD_DRQ) != MG_ERR_NONE) {
337 mg_bad_rw_intr(host);
338 return;
340 for (j = 0; j < MG_SECTOR_SIZE >> 1; j++) {
341 *(u16 *)req->buffer =
342 inw((unsigned long)host->dev_base +
343 MG_BUFF_OFFSET + (j << 1));
344 req->buffer += 2;
347 req->sector++;
348 req->errors = 0;
349 remains = --req->nr_sectors;
350 --req->current_nr_sectors;
352 if (req->current_nr_sectors <= 0) {
353 MG_DBG("remain : %d sects\n", remains);
354 end_request(req, 1);
355 if (remains > 0)
356 req = elv_next_request(host->breq);
359 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base +
360 MG_REG_COMMAND);
364 static void mg_write(struct request *req)
366 u32 remains, j;
367 struct mg_host *host = req->rq_disk->private_data;
369 remains = req->nr_sectors;
371 if (mg_out(host, req->sector, req->nr_sectors, MG_CMD_WR, NULL) !=
372 MG_ERR_NONE) {
373 mg_bad_rw_intr(host);
374 return;
378 MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
379 remains, req->sector, req->buffer);
380 while (remains) {
381 if (mg_wait(host, MG_REG_STATUS_BIT_DATA_REQ,
382 MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
383 mg_bad_rw_intr(host);
384 return;
386 for (j = 0; j < MG_SECTOR_SIZE >> 1; j++) {
387 outw(*(u16 *)req->buffer,
388 (unsigned long)host->dev_base +
389 MG_BUFF_OFFSET + (j << 1));
390 req->buffer += 2;
392 req->sector++;
393 remains = --req->nr_sectors;
394 --req->current_nr_sectors;
396 if (req->current_nr_sectors <= 0) {
397 MG_DBG("remain : %d sects\n", remains);
398 end_request(req, 1);
399 if (remains > 0)
400 req = elv_next_request(host->breq);
403 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
404 MG_REG_COMMAND);
408 static void mg_read_intr(struct mg_host *host)
410 u32 i;
411 struct request *req;
413 /* check status */
414 do {
415 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
416 if (i & MG_REG_STATUS_BIT_BUSY)
417 break;
418 if (!MG_READY_OK(i))
419 break;
420 if (i & MG_REG_STATUS_BIT_DATA_REQ)
421 goto ok_to_read;
422 } while (0);
423 mg_dump_status("mg_read_intr", i, host);
424 mg_bad_rw_intr(host);
425 mg_request(host->breq);
426 return;
428 ok_to_read:
429 /* get current segment of request */
430 req = elv_next_request(host->breq);
432 /* read 1 sector */
433 for (i = 0; i < MG_SECTOR_SIZE >> 1; i++) {
434 *(u16 *)req->buffer =
435 inw((unsigned long)host->dev_base + MG_BUFF_OFFSET +
436 (i << 1));
437 req->buffer += 2;
440 /* manipulate request */
441 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
442 req->sector, req->nr_sectors - 1, req->buffer);
444 req->sector++;
445 req->errors = 0;
446 i = --req->nr_sectors;
447 --req->current_nr_sectors;
449 /* let know if current segment done */
450 if (req->current_nr_sectors <= 0)
451 end_request(req, 1);
453 /* set handler if read remains */
454 if (i > 0) {
455 host->mg_do_intr = mg_read_intr;
456 mod_timer(&host->timer, jiffies + 3 * HZ);
459 /* send read confirm */
460 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
462 /* goto next request */
463 if (!i)
464 mg_request(host->breq);
467 static void mg_write_intr(struct mg_host *host)
469 u32 i, j;
470 u16 *buff;
471 struct request *req;
473 /* get current segment of request */
474 req = elv_next_request(host->breq);
476 /* check status */
477 do {
478 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
479 if (i & MG_REG_STATUS_BIT_BUSY)
480 break;
481 if (!MG_READY_OK(i))
482 break;
483 if ((req->nr_sectors <= 1) || (i & MG_REG_STATUS_BIT_DATA_REQ))
484 goto ok_to_write;
485 } while (0);
486 mg_dump_status("mg_write_intr", i, host);
487 mg_bad_rw_intr(host);
488 mg_request(host->breq);
489 return;
491 ok_to_write:
492 /* manipulate request */
493 req->sector++;
494 i = --req->nr_sectors;
495 --req->current_nr_sectors;
496 req->buffer += MG_SECTOR_SIZE;
498 /* let know if current segment or all done */
499 if (!i || (req->bio && req->current_nr_sectors <= 0))
500 end_request(req, 1);
502 /* write 1 sector and set handler if remains */
503 if (i > 0) {
504 buff = (u16 *)req->buffer;
505 for (j = 0; j < MG_STORAGE_BUFFER_SIZE >> 1; j++) {
506 outw(*buff, (unsigned long)host->dev_base +
507 MG_BUFF_OFFSET + (j << 1));
508 buff++;
510 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
511 req->sector, req->nr_sectors, req->buffer);
512 host->mg_do_intr = mg_write_intr;
513 mod_timer(&host->timer, jiffies + 3 * HZ);
516 /* send write confirm */
517 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
519 if (!i)
520 mg_request(host->breq);
523 void mg_times_out(unsigned long data)
525 struct mg_host *host = (struct mg_host *)data;
526 char *name;
527 struct request *req;
529 spin_lock_irq(&host->lock);
531 req = elv_next_request(host->breq);
532 if (!req)
533 goto out_unlock;
535 host->mg_do_intr = NULL;
537 name = req->rq_disk->disk_name;
538 printk(KERN_DEBUG "%s: timeout\n", name);
540 host->error = MG_ERR_TIMEOUT;
541 mg_bad_rw_intr(host);
543 mg_request(host->breq);
544 out_unlock:
545 spin_unlock_irq(&host->lock);
548 static void mg_request_poll(struct request_queue *q)
550 struct request *req;
551 struct mg_host *host;
553 while ((req = elv_next_request(q)) != NULL) {
554 host = req->rq_disk->private_data;
555 if (blk_fs_request(req)) {
556 switch (rq_data_dir(req)) {
557 case READ:
558 mg_read(req);
559 break;
560 case WRITE:
561 mg_write(req);
562 break;
563 default:
564 printk(KERN_WARNING "%s:%d unknown command\n",
565 __func__, __LINE__);
566 end_request(req, 0);
567 break;
573 static unsigned int mg_issue_req(struct request *req,
574 struct mg_host *host,
575 unsigned int sect_num,
576 unsigned int sect_cnt)
578 u16 *buff;
579 u32 i;
581 switch (rq_data_dir(req)) {
582 case READ:
583 if (mg_out(host, sect_num, sect_cnt, MG_CMD_RD, &mg_read_intr)
584 != MG_ERR_NONE) {
585 mg_bad_rw_intr(host);
586 return host->error;
588 break;
589 case WRITE:
590 /* TODO : handler */
591 outb(MG_REG_CTRL_INTR_DISABLE,
592 (unsigned long)host->dev_base +
593 MG_REG_DRV_CTRL);
594 if (mg_out(host, sect_num, sect_cnt, MG_CMD_WR, &mg_write_intr)
595 != MG_ERR_NONE) {
596 mg_bad_rw_intr(host);
597 return host->error;
599 del_timer(&host->timer);
600 mg_wait(host, MG_REG_STATUS_BIT_DATA_REQ, MG_TMAX_WAIT_WR_DRQ);
601 outb(MG_REG_CTRL_INTR_ENABLE, (unsigned long)host->dev_base +
602 MG_REG_DRV_CTRL);
603 if (host->error) {
604 mg_bad_rw_intr(host);
605 return host->error;
607 buff = (u16 *)req->buffer;
608 for (i = 0; i < MG_SECTOR_SIZE >> 1; i++) {
609 outw(*buff, (unsigned long)host->dev_base +
610 MG_BUFF_OFFSET + (i << 1));
611 buff++;
613 mod_timer(&host->timer, jiffies + 3 * HZ);
614 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
615 MG_REG_COMMAND);
616 break;
617 default:
618 printk(KERN_WARNING "%s:%d unknown command\n",
619 __func__, __LINE__);
620 end_request(req, 0);
621 break;
623 return MG_ERR_NONE;
626 /* This function also called from IRQ context */
627 static void mg_request(struct request_queue *q)
629 struct request *req;
630 struct mg_host *host;
631 u32 sect_num, sect_cnt;
633 while (1) {
634 req = elv_next_request(q);
635 if (!req)
636 return;
638 host = req->rq_disk->private_data;
640 /* check unwanted request call */
641 if (host->mg_do_intr)
642 return;
644 del_timer(&host->timer);
646 sect_num = req->sector;
647 /* deal whole segments */
648 sect_cnt = req->nr_sectors;
650 /* sanity check */
651 if (sect_num >= get_capacity(req->rq_disk) ||
652 ((sect_num + sect_cnt) >
653 get_capacity(req->rq_disk))) {
654 printk(KERN_WARNING
655 "%s: bad access: sector=%d, count=%d\n",
656 req->rq_disk->disk_name,
657 sect_num, sect_cnt);
658 end_request(req, 0);
659 continue;
662 if (!blk_fs_request(req))
663 return;
665 if (!mg_issue_req(req, host, sect_num, sect_cnt))
666 return;
670 static int mg_getgeo(struct block_device *bdev, struct hd_geometry *geo)
672 struct mg_host *host = bdev->bd_disk->private_data;
674 geo->cylinders = (unsigned short)host->cyls;
675 geo->heads = (unsigned char)host->heads;
676 geo->sectors = (unsigned char)host->sectors;
677 return 0;
680 static struct block_device_operations mg_disk_ops = {
681 .getgeo = mg_getgeo
684 static int mg_suspend(struct platform_device *plat_dev, pm_message_t state)
686 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
687 struct mg_host *host = prv_data->host;
689 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
690 return -EIO;
692 if (!prv_data->use_polling)
693 outb(MG_REG_CTRL_INTR_DISABLE,
694 (unsigned long)host->dev_base +
695 MG_REG_DRV_CTRL);
697 outb(MG_CMD_SLEEP, (unsigned long)host->dev_base + MG_REG_COMMAND);
698 /* wait until mflash deep sleep */
699 msleep(1);
701 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) {
702 if (!prv_data->use_polling)
703 outb(MG_REG_CTRL_INTR_ENABLE,
704 (unsigned long)host->dev_base +
705 MG_REG_DRV_CTRL);
706 return -EIO;
709 return 0;
712 static int mg_resume(struct platform_device *plat_dev)
714 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
715 struct mg_host *host = prv_data->host;
717 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
718 return -EIO;
720 outb(MG_CMD_WAKEUP, (unsigned long)host->dev_base + MG_REG_COMMAND);
721 /* wait until mflash wakeup */
722 msleep(1);
724 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
725 return -EIO;
727 if (!prv_data->use_polling)
728 outb(MG_REG_CTRL_INTR_ENABLE, (unsigned long)host->dev_base +
729 MG_REG_DRV_CTRL);
731 return 0;
734 static int mg_probe(struct platform_device *plat_dev)
736 struct mg_host *host;
737 struct resource *rsc;
738 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
739 int err = 0;
741 if (!prv_data) {
742 printk(KERN_ERR "%s:%d fail (no driver_data)\n",
743 __func__, __LINE__);
744 err = -EINVAL;
745 goto probe_err;
748 /* alloc mg_host */
749 host = kzalloc(sizeof(struct mg_host), GFP_KERNEL);
750 if (!host) {
751 printk(KERN_ERR "%s:%d fail (no memory for mg_host)\n",
752 __func__, __LINE__);
753 err = -ENOMEM;
754 goto probe_err;
756 host->major = MG_DISK_MAJ;
758 /* link each other */
759 prv_data->host = host;
760 host->dev = &plat_dev->dev;
762 /* io remap */
763 rsc = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
764 if (!rsc) {
765 printk(KERN_ERR "%s:%d platform_get_resource fail\n",
766 __func__, __LINE__);
767 err = -EINVAL;
768 goto probe_err_2;
770 host->dev_base = ioremap(rsc->start , rsc->end + 1);
771 if (!host->dev_base) {
772 printk(KERN_ERR "%s:%d ioremap fail\n",
773 __func__, __LINE__);
774 err = -EIO;
775 goto probe_err_2;
777 MG_DBG("dev_base = 0x%x\n", (u32)host->dev_base);
779 /* get reset pin */
780 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
781 MG_RST_PIN);
782 if (!rsc) {
783 printk(KERN_ERR "%s:%d get reset pin fail\n",
784 __func__, __LINE__);
785 err = -EIO;
786 goto probe_err_3;
788 host->rst = rsc->start;
790 /* init rst pin */
791 err = gpio_request(host->rst, MG_RST_PIN);
792 if (err)
793 goto probe_err_3;
794 gpio_direction_output(host->rst, 1);
796 /* reset out pin */
797 if (!(prv_data->dev_attr & MG_DEV_MASK))
798 goto probe_err_3a;
800 if (prv_data->dev_attr != MG_BOOT_DEV) {
801 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
802 MG_RSTOUT_PIN);
803 if (!rsc) {
804 printk(KERN_ERR "%s:%d get reset-out pin fail\n",
805 __func__, __LINE__);
806 err = -EIO;
807 goto probe_err_3a;
809 host->rstout = rsc->start;
810 err = gpio_request(host->rstout, MG_RSTOUT_PIN);
811 if (err)
812 goto probe_err_3a;
813 gpio_direction_input(host->rstout);
816 /* disk reset */
817 if (prv_data->dev_attr == MG_STORAGE_DEV) {
818 /* If POR seq. not yet finised, wait */
819 err = mg_wait_rstout(host->rstout, MG_TMAX_RSTOUT);
820 if (err)
821 goto probe_err_3b;
822 err = mg_disk_init(host);
823 if (err) {
824 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
825 __func__, __LINE__, err);
826 err = -EIO;
827 goto probe_err_3b;
831 /* get irq resource */
832 if (!prv_data->use_polling) {
833 host->irq = platform_get_irq(plat_dev, 0);
834 if (host->irq == -ENXIO) {
835 err = host->irq;
836 goto probe_err_3b;
838 err = request_irq(host->irq, mg_irq,
839 IRQF_DISABLED | IRQF_TRIGGER_RISING,
840 MG_DEV_NAME, host);
841 if (err) {
842 printk(KERN_ERR "%s:%d fail (request_irq err=%d)\n",
843 __func__, __LINE__, err);
844 goto probe_err_3b;
849 /* get disk id */
850 err = mg_get_disk_id(host);
851 if (err) {
852 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
853 __func__, __LINE__, err);
854 err = -EIO;
855 goto probe_err_4;
858 err = register_blkdev(host->major, MG_DISK_NAME);
859 if (err < 0) {
860 printk(KERN_ERR "%s:%d register_blkdev fail (err code : %d)\n",
861 __func__, __LINE__, err);
862 goto probe_err_4;
864 if (!host->major)
865 host->major = err;
867 spin_lock_init(&host->lock);
869 if (prv_data->use_polling)
870 host->breq = blk_init_queue(mg_request_poll, &host->lock);
871 else
872 host->breq = blk_init_queue(mg_request, &host->lock);
874 if (!host->breq) {
875 err = -ENOMEM;
876 printk(KERN_ERR "%s:%d (blk_init_queue) fail\n",
877 __func__, __LINE__);
878 goto probe_err_5;
881 /* mflash is random device, thanx for the noop */
882 elevator_exit(host->breq->elevator);
883 err = elevator_init(host->breq, "noop");
884 if (err) {
885 printk(KERN_ERR "%s:%d (elevator_init) fail\n",
886 __func__, __LINE__);
887 goto probe_err_6;
889 blk_queue_max_sectors(host->breq, MG_MAX_SECTS);
890 blk_queue_hardsect_size(host->breq, MG_SECTOR_SIZE);
892 init_timer(&host->timer);
893 host->timer.function = mg_times_out;
894 host->timer.data = (unsigned long)host;
896 host->gd = alloc_disk(MG_DISK_MAX_PART);
897 if (!host->gd) {
898 printk(KERN_ERR "%s:%d (alloc_disk) fail\n",
899 __func__, __LINE__);
900 err = -ENOMEM;
901 goto probe_err_7;
903 host->gd->major = host->major;
904 host->gd->first_minor = 0;
905 host->gd->fops = &mg_disk_ops;
906 host->gd->queue = host->breq;
907 host->gd->private_data = host;
908 sprintf(host->gd->disk_name, MG_DISK_NAME"a");
910 set_capacity(host->gd, host->n_sectors);
912 add_disk(host->gd);
914 return err;
916 probe_err_7:
917 del_timer_sync(&host->timer);
918 probe_err_6:
919 blk_cleanup_queue(host->breq);
920 probe_err_5:
921 unregister_blkdev(MG_DISK_MAJ, MG_DISK_NAME);
922 probe_err_4:
923 if (!prv_data->use_polling)
924 free_irq(host->irq, host);
925 probe_err_3b:
926 gpio_free(host->rstout);
927 probe_err_3a:
928 gpio_free(host->rst);
929 probe_err_3:
930 iounmap(host->dev_base);
931 probe_err_2:
932 kfree(host);
933 probe_err:
934 return err;
937 static int mg_remove(struct platform_device *plat_dev)
939 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
940 struct mg_host *host = prv_data->host;
941 int err = 0;
943 /* delete timer */
944 del_timer_sync(&host->timer);
946 /* remove disk */
947 if (host->gd) {
948 del_gendisk(host->gd);
949 put_disk(host->gd);
951 /* remove queue */
952 if (host->breq)
953 blk_cleanup_queue(host->breq);
955 /* unregister blk device */
956 unregister_blkdev(host->major, MG_DISK_NAME);
958 /* free irq */
959 if (!prv_data->use_polling)
960 free_irq(host->irq, host);
962 /* free reset-out pin */
963 if (prv_data->dev_attr != MG_BOOT_DEV)
964 gpio_free(host->rstout);
966 /* free rst pin */
967 if (host->rst)
968 gpio_free(host->rst);
970 /* unmap io */
971 if (host->dev_base)
972 iounmap(host->dev_base);
974 /* free mg_host */
975 kfree(host);
977 return err;
980 static struct platform_driver mg_disk_driver = {
981 .probe = mg_probe,
982 .remove = mg_remove,
983 .suspend = mg_suspend,
984 .resume = mg_resume,
985 .driver = {
986 .name = MG_DEV_NAME,
987 .owner = THIS_MODULE,
991 /****************************************************************************
993 * Module stuff
995 ****************************************************************************/
997 static int __init mg_init(void)
999 printk(KERN_INFO "mGine mflash driver, (c) 2008 mGine Co.\n");
1000 return platform_driver_register(&mg_disk_driver);
1003 static void __exit mg_exit(void)
1005 printk(KERN_INFO "mflash driver : bye bye\n");
1006 platform_driver_unregister(&mg_disk_driver);
1009 module_init(mg_init);
1010 module_exit(mg_exit);
1012 MODULE_LICENSE("GPL");
1013 MODULE_AUTHOR("unsik Kim <donari75@gmail.com>");
1014 MODULE_DESCRIPTION("mGine m[g]flash device driver");