amiflop,ataflop,xd,mg_disk: clean up unnecessary stuff from block drivers
[linux-2.6.git] / drivers / block / mg_disk.c
1 /*
2  *  drivers/block/mg_disk.c
3  *
4  *  Support for the mGine m[g]flash IO mode.
5  *  Based on legacy hd.c
6  *
7  * (c) 2008 mGine Co.,LTD
8  * (c) 2008 unsik Kim <donari75@gmail.com>
9  *
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.
13  */
14
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>
26
27 #define MG_RES_SEC (CONFIG_MG_DISK_RES << 1)
28
29 static void mg_request(struct request_queue *);
30
31 static void mg_dump_status(const char *msg, unsigned int stat,
32                 struct mg_host *host)
33 {
34         char *name = MG_DISK_NAME;
35         struct request *req;
36
37         if (host->breq) {
38                 req = elv_next_request(host->breq);
39                 if (req)
40                         name = req->rq_disk->disk_name;
41         }
42
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);
83                         }
84
85                 }
86                 printk("\n");
87         }
88 }
89
90 static unsigned int mg_wait(struct mg_host *host, u32 expect, u32 msec)
91 {
92         u8 status;
93         unsigned long expire, cur_jiffies;
94         struct mg_drv_data *prv_data = host->dev->platform_data;
95
96         host->error = MG_ERR_NONE;
97         expire = jiffies + msecs_to_jiffies(msec);
98
99         status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
100
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;
111                         }
112
113                         if (expect == MG_STAT_READY)
114                                 if (MG_READY_OK(status))
115                                         break;
116
117                         if (expect == MG_REG_STATUS_BIT_DATA_REQ)
118                                 if (status & MG_REG_STATUS_BIT_DATA_REQ)
119                                         break;
120                 }
121                 if (!msec) {
122                         mg_dump_status("not ready", status, host);
123                         return MG_ERR_INV_STAT;
124                 }
125                 if (prv_data->use_polling)
126                         msleep(1);
127
128                 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
129         } while (time_before(cur_jiffies, expire));
130
131         if (time_after_eq(cur_jiffies, expire) && msec)
132                 host->error = MG_ERR_TIMEOUT;
133
134         return host->error;
135 }
136
137 static unsigned int mg_wait_rstout(u32 rstout, u32 msec)
138 {
139         unsigned long expire;
140
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);
146         }
147
148         return MG_ERR_RSTOUT;
149 }
150
151 static void mg_unexpected_intr(struct mg_host *host)
152 {
153         u32 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
154
155         mg_dump_status("mg_unexpected_intr", status, host);
156 }
157
158 static irqreturn_t mg_irq(int irq, void *dev_id)
159 {
160         struct mg_host *host = dev_id;
161         void (*handler)(struct mg_host *) = host->mg_do_intr;
162
163         spin_lock(&host->lock);
164
165         host->mg_do_intr = NULL;
166         del_timer(&host->timer);
167         if (!handler)
168                 handler = mg_unexpected_intr;
169         handler(host);
170
171         spin_unlock(&host->lock);
172
173         return IRQ_HANDLED;
174 }
175
176 static int mg_get_disk_id(struct mg_host *host)
177 {
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];
185
186         if (!prv_data->use_polling)
187                 outb(MG_REG_CTRL_INTR_DISABLE,
188                                 (unsigned long)host->dev_base +
189                                 MG_REG_DRV_CTRL);
190
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;
195
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));
199
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;
204
205         if ((id[ATA_ID_FIELD_VALID] & 1) == 0)
206                 return MG_ERR_TRANSLATION;
207
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];
212
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;
220         }
221
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);
230
231         if (!prv_data->use_polling)
232                 outb(MG_REG_CTRL_INTR_ENABLE, (unsigned long)host->dev_base +
233                                 MG_REG_DRV_CTRL);
234
235         return err;
236 }
237
238
239 static int mg_disk_init(struct mg_host *host)
240 {
241         struct mg_drv_data *prv_data = host->dev->platform_data;
242         s32 err;
243         u8 init_status;
244
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;
250
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;
256
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;
265
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;
273
274         init_status = inb((unsigned long)host->dev_base + MG_REG_STATUS) & 0xf;
275
276         if (init_status == 0xf)
277                 return MG_ERR_INIT_STAT;
278
279         return err;
280 }
281
282 static void mg_bad_rw_intr(struct mg_host *host)
283 {
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                         __blk_end_request_cur(req, -EIO);
289 }
290
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 *))
296 {
297         struct mg_drv_data *prv_data = host->dev->platform_data;
298
299         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
300                 return host->error;
301
302         if (!prv_data->use_polling) {
303                 host->mg_do_intr = intr_addr;
304                 mod_timer(&host->timer, jiffies + 3 * HZ);
305         }
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;
318 }
319
320 static void mg_read(struct request *req)
321 {
322         u32 remains, j;
323         struct mg_host *host = req->rq_disk->private_data;
324
325         remains = req->nr_sectors;
326
327         if (mg_out(host, req->sector, req->nr_sectors, MG_CMD_RD, NULL) !=
328                         MG_ERR_NONE)
329                 mg_bad_rw_intr(host);
330
331         MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
332                         remains, req->sector, req->buffer);
333
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;
339                 }
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;
345                 }
346
347                 req->sector++;
348                 req->errors = 0;
349                 remains = --req->nr_sectors;
350                 --req->current_nr_sectors;
351
352                 if (req->current_nr_sectors <= 0) {
353                         MG_DBG("remain : %d sects\n", remains);
354                         __blk_end_request_cur(req, 0);
355                         if (remains > 0)
356                                 req = elv_next_request(host->breq);
357                 }
358
359                 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base +
360                                 MG_REG_COMMAND);
361         }
362 }
363
364 static void mg_write(struct request *req)
365 {
366         u32 remains, j;
367         struct mg_host *host = req->rq_disk->private_data;
368
369         remains = req->nr_sectors;
370
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;
375         }
376
377
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;
385                 }
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;
391                 }
392                 req->sector++;
393                 remains = --req->nr_sectors;
394                 --req->current_nr_sectors;
395
396                 if (req->current_nr_sectors <= 0) {
397                         MG_DBG("remain : %d sects\n", remains);
398                         __blk_end_request_cur(req, 0);
399                         if (remains > 0)
400                                 req = elv_next_request(host->breq);
401                 }
402
403                 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
404                                 MG_REG_COMMAND);
405         }
406 }
407
408 static void mg_read_intr(struct mg_host *host)
409 {
410         u32 i;
411         struct request *req;
412
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;
427
428 ok_to_read:
429         /* get current segment of request */
430         req = elv_next_request(host->breq);
431
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;
438         }
439
440         /* manipulate request */
441         MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
442                         req->sector, req->nr_sectors - 1, req->buffer);
443
444         req->sector++;
445         req->errors = 0;
446         i = --req->nr_sectors;
447         --req->current_nr_sectors;
448
449         /* let know if current segment done */
450         if (req->current_nr_sectors <= 0)
451                 __blk_end_request_cur(req, 0);
452
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);
457         }
458
459         /* send read confirm */
460         outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
461
462         /* goto next request */
463         if (!i)
464                 mg_request(host->breq);
465 }
466
467 static void mg_write_intr(struct mg_host *host)
468 {
469         u32 i, j;
470         u16 *buff;
471         struct request *req;
472
473         /* get current segment of request */
474         req = elv_next_request(host->breq);
475
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;
490
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;
497
498         /* let know if current segment or all done */
499         if (!i || (req->bio && req->current_nr_sectors <= 0))
500                 __blk_end_request_cur(req, 0);
501
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++;
509                 }
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);
514         }
515
516         /* send write confirm */
517         outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
518
519         if (!i)
520                 mg_request(host->breq);
521 }
522
523 void mg_times_out(unsigned long data)
524 {
525         struct mg_host *host = (struct mg_host *)data;
526         char *name;
527         struct request *req;
528
529         spin_lock_irq(&host->lock);
530
531         req = elv_next_request(host->breq);
532         if (!req)
533                 goto out_unlock;
534
535         host->mg_do_intr = NULL;
536
537         name = req->rq_disk->disk_name;
538         printk(KERN_DEBUG "%s: timeout\n", name);
539
540         host->error = MG_ERR_TIMEOUT;
541         mg_bad_rw_intr(host);
542
543         mg_request(host->breq);
544 out_unlock:
545         spin_unlock_irq(&host->lock);
546 }
547
548 static void mg_request_poll(struct request_queue *q)
549 {
550         struct request *req;
551         struct mg_host *host;
552
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                         }
564                 }
565         }
566 }
567
568 static unsigned int mg_issue_req(struct request *req,
569                 struct mg_host *host,
570                 unsigned int sect_num,
571                 unsigned int sect_cnt)
572 {
573         u16 *buff;
574         u32 i;
575
576         switch (rq_data_dir(req)) {
577         case READ:
578                 if (mg_out(host, sect_num, sect_cnt, MG_CMD_RD, &mg_read_intr)
579                                 != MG_ERR_NONE) {
580                         mg_bad_rw_intr(host);
581                         return host->error;
582                 }
583                 break;
584         case WRITE:
585                 /* TODO : handler */
586                 outb(MG_REG_CTRL_INTR_DISABLE,
587                                 (unsigned long)host->dev_base +
588                                 MG_REG_DRV_CTRL);
589                 if (mg_out(host, sect_num, sect_cnt, MG_CMD_WR, &mg_write_intr)
590                                 != MG_ERR_NONE) {
591                         mg_bad_rw_intr(host);
592                         return host->error;
593                 }
594                 del_timer(&host->timer);
595                 mg_wait(host, MG_REG_STATUS_BIT_DATA_REQ, MG_TMAX_WAIT_WR_DRQ);
596                 outb(MG_REG_CTRL_INTR_ENABLE, (unsigned long)host->dev_base +
597                                 MG_REG_DRV_CTRL);
598                 if (host->error) {
599                         mg_bad_rw_intr(host);
600                         return host->error;
601                 }
602                 buff = (u16 *)req->buffer;
603                 for (i = 0; i < MG_SECTOR_SIZE >> 1; i++) {
604                         outw(*buff, (unsigned long)host->dev_base +
605                                         MG_BUFF_OFFSET + (i << 1));
606                         buff++;
607                 }
608                 mod_timer(&host->timer, jiffies + 3 * HZ);
609                 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
610                                 MG_REG_COMMAND);
611                 break;
612         }
613         return MG_ERR_NONE;
614 }
615
616 /* This function also called from IRQ context */
617 static void mg_request(struct request_queue *q)
618 {
619         struct request *req;
620         struct mg_host *host;
621         u32 sect_num, sect_cnt;
622
623         while (1) {
624                 req = elv_next_request(q);
625                 if (!req)
626                         return;
627
628                 host = req->rq_disk->private_data;
629
630                 /* check unwanted request call */
631                 if (host->mg_do_intr)
632                         return;
633
634                 del_timer(&host->timer);
635
636                 sect_num = req->sector;
637                 /* deal whole segments */
638                 sect_cnt = req->nr_sectors;
639
640                 /* sanity check */
641                 if (sect_num >= get_capacity(req->rq_disk) ||
642                                 ((sect_num + sect_cnt) >
643                                  get_capacity(req->rq_disk))) {
644                         printk(KERN_WARNING
645                                         "%s: bad access: sector=%d, count=%d\n",
646                                         req->rq_disk->disk_name,
647                                         sect_num, sect_cnt);
648                         __blk_end_request_cur(req, -EIO);
649                         continue;
650                 }
651
652                 if (!blk_fs_request(req))
653                         return;
654
655                 if (!mg_issue_req(req, host, sect_num, sect_cnt))
656                         return;
657         }
658 }
659
660 static int mg_getgeo(struct block_device *bdev, struct hd_geometry *geo)
661 {
662         struct mg_host *host = bdev->bd_disk->private_data;
663
664         geo->cylinders = (unsigned short)host->cyls;
665         geo->heads = (unsigned char)host->heads;
666         geo->sectors = (unsigned char)host->sectors;
667         return 0;
668 }
669
670 static struct block_device_operations mg_disk_ops = {
671         .getgeo = mg_getgeo
672 };
673
674 static int mg_suspend(struct platform_device *plat_dev, pm_message_t state)
675 {
676         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
677         struct mg_host *host = prv_data->host;
678
679         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
680                 return -EIO;
681
682         if (!prv_data->use_polling)
683                 outb(MG_REG_CTRL_INTR_DISABLE,
684                                 (unsigned long)host->dev_base +
685                                 MG_REG_DRV_CTRL);
686
687         outb(MG_CMD_SLEEP, (unsigned long)host->dev_base + MG_REG_COMMAND);
688         /* wait until mflash deep sleep */
689         msleep(1);
690
691         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) {
692                 if (!prv_data->use_polling)
693                         outb(MG_REG_CTRL_INTR_ENABLE,
694                                         (unsigned long)host->dev_base +
695                                         MG_REG_DRV_CTRL);
696                 return -EIO;
697         }
698
699         return 0;
700 }
701
702 static int mg_resume(struct platform_device *plat_dev)
703 {
704         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
705         struct mg_host *host = prv_data->host;
706
707         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
708                 return -EIO;
709
710         outb(MG_CMD_WAKEUP, (unsigned long)host->dev_base + MG_REG_COMMAND);
711         /* wait until mflash wakeup */
712         msleep(1);
713
714         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
715                 return -EIO;
716
717         if (!prv_data->use_polling)
718                 outb(MG_REG_CTRL_INTR_ENABLE, (unsigned long)host->dev_base +
719                                 MG_REG_DRV_CTRL);
720
721         return 0;
722 }
723
724 static int mg_probe(struct platform_device *plat_dev)
725 {
726         struct mg_host *host;
727         struct resource *rsc;
728         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
729         int err = 0;
730
731         if (!prv_data) {
732                 printk(KERN_ERR "%s:%d fail (no driver_data)\n",
733                                 __func__, __LINE__);
734                 err = -EINVAL;
735                 goto probe_err;
736         }
737
738         /* alloc mg_host */
739         host = kzalloc(sizeof(struct mg_host), GFP_KERNEL);
740         if (!host) {
741                 printk(KERN_ERR "%s:%d fail (no memory for mg_host)\n",
742                                 __func__, __LINE__);
743                 err = -ENOMEM;
744                 goto probe_err;
745         }
746         host->major = MG_DISK_MAJ;
747
748         /* link each other */
749         prv_data->host = host;
750         host->dev = &plat_dev->dev;
751
752         /* io remap */
753         rsc = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
754         if (!rsc) {
755                 printk(KERN_ERR "%s:%d platform_get_resource fail\n",
756                                 __func__, __LINE__);
757                 err = -EINVAL;
758                 goto probe_err_2;
759         }
760         host->dev_base = ioremap(rsc->start , rsc->end + 1);
761         if (!host->dev_base) {
762                 printk(KERN_ERR "%s:%d ioremap fail\n",
763                                 __func__, __LINE__);
764                 err = -EIO;
765                 goto probe_err_2;
766         }
767         MG_DBG("dev_base = 0x%x\n", (u32)host->dev_base);
768
769         /* get reset pin */
770         rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
771                         MG_RST_PIN);
772         if (!rsc) {
773                 printk(KERN_ERR "%s:%d get reset pin fail\n",
774                                 __func__, __LINE__);
775                 err = -EIO;
776                 goto probe_err_3;
777         }
778         host->rst = rsc->start;
779
780         /* init rst pin */
781         err = gpio_request(host->rst, MG_RST_PIN);
782         if (err)
783                 goto probe_err_3;
784         gpio_direction_output(host->rst, 1);
785
786         /* reset out pin */
787         if (!(prv_data->dev_attr & MG_DEV_MASK))
788                 goto probe_err_3a;
789
790         if (prv_data->dev_attr != MG_BOOT_DEV) {
791                 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
792                                 MG_RSTOUT_PIN);
793                 if (!rsc) {
794                         printk(KERN_ERR "%s:%d get reset-out pin fail\n",
795                                         __func__, __LINE__);
796                         err = -EIO;
797                         goto probe_err_3a;
798                 }
799                 host->rstout = rsc->start;
800                 err = gpio_request(host->rstout, MG_RSTOUT_PIN);
801                 if (err)
802                         goto probe_err_3a;
803                 gpio_direction_input(host->rstout);
804         }
805
806         /* disk reset */
807         if (prv_data->dev_attr == MG_STORAGE_DEV) {
808                 /* If POR seq. not yet finised, wait */
809                 err = mg_wait_rstout(host->rstout, MG_TMAX_RSTOUT);
810                 if (err)
811                         goto probe_err_3b;
812                 err = mg_disk_init(host);
813                 if (err) {
814                         printk(KERN_ERR "%s:%d fail (err code : %d)\n",
815                                         __func__, __LINE__, err);
816                         err = -EIO;
817                         goto probe_err_3b;
818                 }
819         }
820
821         /* get irq resource */
822         if (!prv_data->use_polling) {
823                 host->irq = platform_get_irq(plat_dev, 0);
824                 if (host->irq == -ENXIO) {
825                         err = host->irq;
826                         goto probe_err_3b;
827                 }
828                 err = request_irq(host->irq, mg_irq,
829                                 IRQF_DISABLED | IRQF_TRIGGER_RISING,
830                                 MG_DEV_NAME, host);
831                 if (err) {
832                         printk(KERN_ERR "%s:%d fail (request_irq err=%d)\n",
833                                         __func__, __LINE__, err);
834                         goto probe_err_3b;
835                 }
836
837         }
838
839         /* get disk id */
840         err = mg_get_disk_id(host);
841         if (err) {
842                 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
843                                 __func__, __LINE__, err);
844                 err = -EIO;
845                 goto probe_err_4;
846         }
847
848         err = register_blkdev(host->major, MG_DISK_NAME);
849         if (err < 0) {
850                 printk(KERN_ERR "%s:%d register_blkdev fail (err code : %d)\n",
851                                 __func__, __LINE__, err);
852                 goto probe_err_4;
853         }
854         if (!host->major)
855                 host->major = err;
856
857         spin_lock_init(&host->lock);
858
859         if (prv_data->use_polling)
860                 host->breq = blk_init_queue(mg_request_poll, &host->lock);
861         else
862                 host->breq = blk_init_queue(mg_request, &host->lock);
863
864         if (!host->breq) {
865                 err = -ENOMEM;
866                 printk(KERN_ERR "%s:%d (blk_init_queue) fail\n",
867                                 __func__, __LINE__);
868                 goto probe_err_5;
869         }
870
871         /* mflash is random device, thanx for the noop */
872         elevator_exit(host->breq->elevator);
873         err = elevator_init(host->breq, "noop");
874         if (err) {
875                 printk(KERN_ERR "%s:%d (elevator_init) fail\n",
876                                 __func__, __LINE__);
877                 goto probe_err_6;
878         }
879         blk_queue_max_sectors(host->breq, MG_MAX_SECTS);
880         blk_queue_hardsect_size(host->breq, MG_SECTOR_SIZE);
881
882         init_timer(&host->timer);
883         host->timer.function = mg_times_out;
884         host->timer.data = (unsigned long)host;
885
886         host->gd = alloc_disk(MG_DISK_MAX_PART);
887         if (!host->gd) {
888                 printk(KERN_ERR "%s:%d (alloc_disk) fail\n",
889                                 __func__, __LINE__);
890                 err = -ENOMEM;
891                 goto probe_err_7;
892         }
893         host->gd->major = host->major;
894         host->gd->first_minor = 0;
895         host->gd->fops = &mg_disk_ops;
896         host->gd->queue = host->breq;
897         host->gd->private_data = host;
898         sprintf(host->gd->disk_name, MG_DISK_NAME"a");
899
900         set_capacity(host->gd, host->n_sectors);
901
902         add_disk(host->gd);
903
904         return err;
905
906 probe_err_7:
907         del_timer_sync(&host->timer);
908 probe_err_6:
909         blk_cleanup_queue(host->breq);
910 probe_err_5:
911         unregister_blkdev(MG_DISK_MAJ, MG_DISK_NAME);
912 probe_err_4:
913         if (!prv_data->use_polling)
914                 free_irq(host->irq, host);
915 probe_err_3b:
916         gpio_free(host->rstout);
917 probe_err_3a:
918         gpio_free(host->rst);
919 probe_err_3:
920         iounmap(host->dev_base);
921 probe_err_2:
922         kfree(host);
923 probe_err:
924         return err;
925 }
926
927 static int mg_remove(struct platform_device *plat_dev)
928 {
929         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
930         struct mg_host *host = prv_data->host;
931         int err = 0;
932
933         /* delete timer */
934         del_timer_sync(&host->timer);
935
936         /* remove disk */
937         if (host->gd) {
938                 del_gendisk(host->gd);
939                 put_disk(host->gd);
940         }
941         /* remove queue */
942         if (host->breq)
943                 blk_cleanup_queue(host->breq);
944
945         /* unregister blk device */
946         unregister_blkdev(host->major, MG_DISK_NAME);
947
948         /* free irq */
949         if (!prv_data->use_polling)
950                 free_irq(host->irq, host);
951
952         /* free reset-out pin */
953         if (prv_data->dev_attr != MG_BOOT_DEV)
954                 gpio_free(host->rstout);
955
956         /* free rst pin */
957         if (host->rst)
958                 gpio_free(host->rst);
959
960         /* unmap io */
961         if (host->dev_base)
962                 iounmap(host->dev_base);
963
964         /* free mg_host */
965         kfree(host);
966
967         return err;
968 }
969
970 static struct platform_driver mg_disk_driver = {
971         .probe = mg_probe,
972         .remove = mg_remove,
973         .suspend = mg_suspend,
974         .resume = mg_resume,
975         .driver = {
976                 .name = MG_DEV_NAME,
977                 .owner = THIS_MODULE,
978         }
979 };
980
981 /****************************************************************************
982  *
983  * Module stuff
984  *
985  ****************************************************************************/
986
987 static int __init mg_init(void)
988 {
989         printk(KERN_INFO "mGine mflash driver, (c) 2008 mGine Co.\n");
990         return platform_driver_register(&mg_disk_driver);
991 }
992
993 static void __exit mg_exit(void)
994 {
995         printk(KERN_INFO "mflash driver : bye bye\n");
996         platform_driver_unregister(&mg_disk_driver);
997 }
998
999 module_init(mg_init);
1000 module_exit(mg_exit);
1001
1002 MODULE_LICENSE("GPL");
1003 MODULE_AUTHOR("unsik Kim <donari75@gmail.com>");
1004 MODULE_DESCRIPTION("mGine m[g]flash device driver");