mg_disk: fix queue hang / infinite retry on !fs requests
[safe/jmp/linux-2.6] / 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/ata.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/gpio.h>
25
26 #define MG_RES_SEC (CONFIG_MG_DISK_RES << 1)
27
28 /* name for block device */
29 #define MG_DISK_NAME "mgd"
30 /* name for platform device */
31 #define MG_DEV_NAME "mg_disk"
32
33 #define MG_DISK_MAJ 0
34 #define MG_DISK_MAX_PART 16
35 #define MG_SECTOR_SIZE 512
36 #define MG_MAX_SECTS 256
37
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)
53
54 /* handy status */
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)
58
59 /* error code for others */
60 #define MG_ERR_NONE             0
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
67
68 #define MG_MAX_ERRORS   6       /* Max read/write errors */
69
70 /* command */
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
78
79 /* operation mode */
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)
84
85 /* synchronous */
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)
96
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
105
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)
113
114 #define MG_DEV_MASK (MG_BOOT_DEV | MG_STORAGE_DEV | MG_STORAGE_DEV_SKIP_RST)
115
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"
120
121 /* private driver data */
122 struct mg_drv_data {
123         /* disk resource */
124         u32 use_polling;
125
126         /* device attribution */
127         u32 dev_attr;
128
129         /* internally used */
130         struct mg_host *host;
131 };
132
133 /* main structure for mflash driver */
134 struct mg_host {
135         struct device *dev;
136
137         struct request_queue *breq;
138         spinlock_t lock;
139         struct gendisk *gd;
140
141         struct timer_list timer;
142         void (*mg_do_intr) (struct mg_host *);
143
144         u16 id[ATA_ID_WORDS];
145
146         u16 cyls;
147         u16 heads;
148         u16 sectors;
149         u32 n_sectors;
150         u32 nres_sectors;
151
152         void __iomem *dev_base;
153         unsigned int irq;
154         unsigned int rst;
155         unsigned int rstout;
156
157         u32 major;
158         u32 error;
159 };
160
161 /*
162  * Debugging macro and defines
163  */
164 #undef DO_MG_DEBUG
165 #ifdef DO_MG_DEBUG
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 */
171
172 static void mg_request(struct request_queue *);
173
174 static void mg_dump_status(const char *msg, unsigned int stat,
175                 struct mg_host *host)
176 {
177         char *name = MG_DISK_NAME;
178         struct request *req;
179
180         if (host->breq) {
181                 req = elv_next_request(host->breq);
182                 if (req)
183                         name = req->rq_disk->disk_name;
184         }
185
186         printk(KERN_ERR "%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
187         if (stat & ATA_BUSY)
188                 printk("Busy ");
189         if (stat & ATA_DRDY)
190                 printk("DriveReady ");
191         if (stat & ATA_DF)
192                 printk("WriteFault ");
193         if (stat & ATA_DSC)
194                 printk("SeekComplete ");
195         if (stat & ATA_DRQ)
196                 printk("DataRequest ");
197         if (stat & ATA_CORR)
198                 printk("CorrectedError ");
199         if (stat & ATA_ERR)
200                 printk("Error ");
201         printk("}\n");
202         if ((stat & ATA_ERR) == 0) {
203                 host->error = 0;
204         } else {
205                 host->error = inb((unsigned long)host->dev_base + MG_REG_ERROR);
206                 printk(KERN_ERR "%s: %s: error=0x%02x { ", name, msg,
207                                 host->error & 0xff);
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 ");
218                 printk("}");
219                 if (host->error & (ATA_BBK | ATA_UNC | ATA_IDNF | ATA_AMNF)) {
220                         if (host->breq) {
221                                 req = elv_next_request(host->breq);
222                                 if (req)
223                                         printk(", sector=%u",
224                                                (unsigned int)blk_rq_pos(req));
225                         }
226
227                 }
228                 printk("\n");
229         }
230 }
231
232 static unsigned int mg_wait(struct mg_host *host, u32 expect, u32 msec)
233 {
234         u8 status;
235         unsigned long expire, cur_jiffies;
236         struct mg_drv_data *prv_data = host->dev->platform_data;
237
238         host->error = MG_ERR_NONE;
239         expire = jiffies + msecs_to_jiffies(msec);
240
241         status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
242
243         do {
244                 cur_jiffies = jiffies;
245                 if (status & ATA_BUSY) {
246                         if (expect == ATA_BUSY)
247                                 break;
248                 } else {
249                         /* Check the error condition! */
250                         if (status & ATA_ERR) {
251                                 mg_dump_status("mg_wait", status, host);
252                                 break;
253                         }
254
255                         if (expect == MG_STAT_READY)
256                                 if (MG_READY_OK(status))
257                                         break;
258
259                         if (expect == ATA_DRQ)
260                                 if (status & ATA_DRQ)
261                                         break;
262                 }
263                 if (!msec) {
264                         mg_dump_status("not ready", status, host);
265                         return MG_ERR_INV_STAT;
266                 }
267                 if (prv_data->use_polling)
268                         msleep(1);
269
270                 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
271         } while (time_before(cur_jiffies, expire));
272
273         if (time_after_eq(cur_jiffies, expire) && msec)
274                 host->error = MG_ERR_TIMEOUT;
275
276         return host->error;
277 }
278
279 static unsigned int mg_wait_rstout(u32 rstout, u32 msec)
280 {
281         unsigned long expire;
282
283         expire = jiffies + msecs_to_jiffies(msec);
284         while (time_before(jiffies, expire)) {
285                 if (gpio_get_value(rstout) == 1)
286                         return MG_ERR_NONE;
287                 msleep(10);
288         }
289
290         return MG_ERR_RSTOUT;
291 }
292
293 static void mg_unexpected_intr(struct mg_host *host)
294 {
295         u32 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
296
297         mg_dump_status("mg_unexpected_intr", status, host);
298 }
299
300 static irqreturn_t mg_irq(int irq, void *dev_id)
301 {
302         struct mg_host *host = dev_id;
303         void (*handler)(struct mg_host *) = host->mg_do_intr;
304
305         spin_lock(&host->lock);
306
307         host->mg_do_intr = NULL;
308         del_timer(&host->timer);
309         if (!handler)
310                 handler = mg_unexpected_intr;
311         handler(host);
312
313         spin_unlock(&host->lock);
314
315         return IRQ_HANDLED;
316 }
317
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)
321 {
322         unsigned int c;
323
324         BUG_ON(len & 1);
325
326         while (len > 0) {
327                 c = id[ofs] >> 8;
328                 *s = c;
329                 s++;
330
331                 c = id[ofs] & 0xff;
332                 *s = c;
333                 s++;
334
335                 ofs++;
336                 len -= 2;
337         }
338 }
339
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)
343 {
344         unsigned char *p;
345
346         mg_id_string(id, s, ofs, len - 1);
347
348         p = s + strnlen(s, len - 1);
349         while (p > s && p[-1] == ' ')
350                 p--;
351         *p = '\0';
352 }
353
354 static int mg_get_disk_id(struct mg_host *host)
355 {
356         u32 i;
357         s32 err;
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];
363
364         if (!prv_data->use_polling)
365                 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
366
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);
369         if (err)
370                 return err;
371
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));
375
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);
378         if (err)
379                 return err;
380
381         if ((id[ATA_ID_FIELD_VALID] & 1) == 0)
382                 return MG_ERR_TRANSLATION;
383
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];
388
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;
396         }
397
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);
406
407         if (!prv_data->use_polling)
408                 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
409
410         return err;
411 }
412
413
414 static int mg_disk_init(struct mg_host *host)
415 {
416         struct mg_drv_data *prv_data = host->dev->platform_data;
417         s32 err;
418         u8 init_status;
419
420         /* hdd rst low */
421         gpio_set_value(host->rst, 0);
422         err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
423         if (err)
424                 return err;
425
426         /* hdd rst high */
427         gpio_set_value(host->rst, 1);
428         err = mg_wait(host, MG_STAT_READY, MG_TMAX_HDRST_TO_RDY);
429         if (err)
430                 return err;
431
432         /* soft reset on */
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);
436         if (err)
437                 return err;
438
439         /* soft reset off */
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);
443         if (err)
444                 return err;
445
446         init_status = inb((unsigned long)host->dev_base + MG_REG_STATUS) & 0xf;
447
448         if (init_status == 0xf)
449                 return MG_ERR_INIT_STAT;
450
451         return err;
452 }
453
454 static void mg_bad_rw_intr(struct mg_host *host)
455 {
456         struct request *req = elv_next_request(host->breq);
457         if (req != NULL)
458                 if (++req->errors >= MG_MAX_ERRORS ||
459                                 host->error == MG_ERR_TIMEOUT)
460                         __blk_end_request_cur(req, -EIO);
461 }
462
463 static unsigned int mg_out(struct mg_host *host,
464                 unsigned int sect_num,
465                 unsigned int sect_cnt,
466                 unsigned int cmd,
467                 void (*intr_addr)(struct mg_host *))
468 {
469         struct mg_drv_data *prv_data = host->dev->platform_data;
470
471         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
472                 return host->error;
473
474         if (!prv_data->use_polling) {
475                 host->mg_do_intr = intr_addr;
476                 mod_timer(&host->timer, jiffies + 3 * HZ);
477         }
478         if (MG_RES_SEC)
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 +
483                         MG_REG_CYL_LOW);
484         outb((u8)(sect_num >> 16), (unsigned long)host->dev_base +
485                         MG_REG_CYL_HIGH);
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);
489         return MG_ERR_NONE;
490 }
491
492 static void mg_read(struct request *req)
493 {
494         u32 j;
495         struct mg_host *host = req->rq_disk->private_data;
496
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);
500
501         MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
502                blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
503
504         do {
505                 u16 *buff = (u16 *)req->buffer;
506
507                 if (mg_wait(host, ATA_DRQ,
508                             MG_TMAX_WAIT_RD_DRQ) != MG_ERR_NONE) {
509                         mg_bad_rw_intr(host);
510                         return;
511                 }
512                 for (j = 0; j < MG_SECTOR_SIZE >> 1; j++)
513                         *buff++ = inw((unsigned long)host->dev_base +
514                                       MG_BUFF_OFFSET + (j << 1));
515
516                 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base +
517                                 MG_REG_COMMAND);
518         } while (__blk_end_request(req, 0, MG_SECTOR_SIZE));
519 }
520
521 static void mg_write(struct request *req)
522 {
523         u32 j;
524         struct mg_host *host = req->rq_disk->private_data;
525
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);
529                 return;
530         }
531
532         MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
533                blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
534
535         do {
536                 u16 *buff = (u16 *)req->buffer;
537
538         if (mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
539                         mg_bad_rw_intr(host);
540                         return;
541                 }
542                 for (j = 0; j < MG_SECTOR_SIZE >> 1; j++)
543                         outw(*buff++, (unsigned long)host->dev_base +
544                                       MG_BUFF_OFFSET + (j << 1));
545
546                 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
547                                 MG_REG_COMMAND);
548         } while (__blk_end_request(req, 0, MG_SECTOR_SIZE));
549 }
550
551 static void mg_read_intr(struct mg_host *host)
552 {
553         u32 i;
554         u16 *buff;
555         struct request *req;
556
557         /* check status */
558         do {
559                 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
560                 if (i & ATA_BUSY)
561                         break;
562                 if (!MG_READY_OK(i))
563                         break;
564                 if (i & ATA_DRQ)
565                         goto ok_to_read;
566         } while (0);
567         mg_dump_status("mg_read_intr", i, host);
568         mg_bad_rw_intr(host);
569         mg_request(host->breq);
570         return;
571
572 ok_to_read:
573         /* get current segment of request */
574         req = elv_next_request(host->breq);
575         buff = (u16 *)req->buffer;
576
577         /* read 1 sector */
578         for (i = 0; i < MG_SECTOR_SIZE >> 1; i++)
579                 *buff++ = inw((unsigned long)host->dev_base + MG_BUFF_OFFSET +
580                               (i << 1));
581
582         MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
583                blk_rq_pos(req), blk_rq_sectors(req) - 1, req->buffer);
584
585         /* send read confirm */
586         outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
587
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);
594 }
595
596 static void mg_write_intr(struct mg_host *host)
597 {
598         u32 i, j;
599         u16 *buff;
600         struct request *req;
601         bool rem;
602
603         /* get current segment of request */
604         req = elv_next_request(host->breq);
605
606         /* check status */
607         do {
608                 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
609                 if (i & ATA_BUSY)
610                         break;
611                 if (!MG_READY_OK(i))
612                         break;
613                 if ((blk_rq_sectors(req) <= 1) || (i & ATA_DRQ))
614                         goto ok_to_write;
615         } while (0);
616         mg_dump_status("mg_write_intr", i, host);
617         mg_bad_rw_intr(host);
618         mg_request(host->breq);
619         return;
620
621 ok_to_write:
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));
628                         buff++;
629                 }
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);
634         }
635
636         /* send write confirm */
637         outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
638
639         if (!rem)
640                 mg_request(host->breq);
641 }
642
643 void mg_times_out(unsigned long data)
644 {
645         struct mg_host *host = (struct mg_host *)data;
646         char *name;
647         struct request *req;
648
649         spin_lock_irq(&host->lock);
650
651         req = elv_next_request(host->breq);
652         if (!req)
653                 goto out_unlock;
654
655         host->mg_do_intr = NULL;
656
657         name = req->rq_disk->disk_name;
658         printk(KERN_DEBUG "%s: timeout\n", name);
659
660         host->error = MG_ERR_TIMEOUT;
661         mg_bad_rw_intr(host);
662
663         mg_request(host->breq);
664 out_unlock:
665         spin_unlock_irq(&host->lock);
666 }
667
668 static void mg_request_poll(struct request_queue *q)
669 {
670         struct request *req;
671         struct mg_host *host;
672
673         while ((req = elv_next_request(q)) != NULL) {
674                 host = req->rq_disk->private_data;
675
676                 if (unlikely(!blk_fs_request(req))) {
677                         __blk_end_request_cur(req, -EIO);
678                         continue;
679                 }
680
681                 if (rq_data_dir(req) == READ)
682                         mg_read(req);
683                 else
684                         mg_write(req);
685         }
686 }
687
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)
692 {
693         u16 *buff;
694         u32 i;
695
696         switch (rq_data_dir(req)) {
697         case READ:
698                 if (mg_out(host, sect_num, sect_cnt, MG_CMD_RD, &mg_read_intr)
699                                 != MG_ERR_NONE) {
700                         mg_bad_rw_intr(host);
701                         return host->error;
702                 }
703                 break;
704         case WRITE:
705                 /* TODO : handler */
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)
708                                 != MG_ERR_NONE) {
709                         mg_bad_rw_intr(host);
710                         return host->error;
711                 }
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);
715                 if (host->error) {
716                         mg_bad_rw_intr(host);
717                         return host->error;
718                 }
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));
723                         buff++;
724                 }
725                 mod_timer(&host->timer, jiffies + 3 * HZ);
726                 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
727                                 MG_REG_COMMAND);
728                 break;
729         }
730         return MG_ERR_NONE;
731 }
732
733 /* This function also called from IRQ context */
734 static void mg_request(struct request_queue *q)
735 {
736         struct request *req;
737         struct mg_host *host;
738         u32 sect_num, sect_cnt;
739
740         while (1) {
741                 req = elv_next_request(q);
742                 if (!req)
743                         return;
744
745                 host = req->rq_disk->private_data;
746
747                 /* check unwanted request call */
748                 if (host->mg_do_intr)
749                         return;
750
751                 del_timer(&host->timer);
752
753                 sect_num = blk_rq_pos(req);
754                 /* deal whole segments */
755                 sect_cnt = blk_rq_sectors(req);
756
757                 /* sanity check */
758                 if (sect_num >= get_capacity(req->rq_disk) ||
759                                 ((sect_num + sect_cnt) >
760                                  get_capacity(req->rq_disk))) {
761                         printk(KERN_WARNING
762                                         "%s: bad access: sector=%d, count=%d\n",
763                                         req->rq_disk->disk_name,
764                                         sect_num, sect_cnt);
765                         __blk_end_request_cur(req, -EIO);
766                         continue;
767                 }
768
769                 if (unlikely(!blk_fs_request(req))) {
770                         __blk_end_request_cur(req, -EIO);
771                         continue;
772                 }
773
774                 if (!mg_issue_req(req, host, sect_num, sect_cnt))
775                         return;
776         }
777 }
778
779 static int mg_getgeo(struct block_device *bdev, struct hd_geometry *geo)
780 {
781         struct mg_host *host = bdev->bd_disk->private_data;
782
783         geo->cylinders = (unsigned short)host->cyls;
784         geo->heads = (unsigned char)host->heads;
785         geo->sectors = (unsigned char)host->sectors;
786         return 0;
787 }
788
789 static struct block_device_operations mg_disk_ops = {
790         .getgeo = mg_getgeo
791 };
792
793 static int mg_suspend(struct platform_device *plat_dev, pm_message_t state)
794 {
795         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
796         struct mg_host *host = prv_data->host;
797
798         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
799                 return -EIO;
800
801         if (!prv_data->use_polling)
802                 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
803
804         outb(MG_CMD_SLEEP, (unsigned long)host->dev_base + MG_REG_COMMAND);
805         /* wait until mflash deep sleep */
806         msleep(1);
807
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);
811                 return -EIO;
812         }
813
814         return 0;
815 }
816
817 static int mg_resume(struct platform_device *plat_dev)
818 {
819         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
820         struct mg_host *host = prv_data->host;
821
822         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
823                 return -EIO;
824
825         outb(MG_CMD_WAKEUP, (unsigned long)host->dev_base + MG_REG_COMMAND);
826         /* wait until mflash wakeup */
827         msleep(1);
828
829         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
830                 return -EIO;
831
832         if (!prv_data->use_polling)
833                 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
834
835         return 0;
836 }
837
838 static int mg_probe(struct platform_device *plat_dev)
839 {
840         struct mg_host *host;
841         struct resource *rsc;
842         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
843         int err = 0;
844
845         if (!prv_data) {
846                 printk(KERN_ERR "%s:%d fail (no driver_data)\n",
847                                 __func__, __LINE__);
848                 err = -EINVAL;
849                 goto probe_err;
850         }
851
852         /* alloc mg_host */
853         host = kzalloc(sizeof(struct mg_host), GFP_KERNEL);
854         if (!host) {
855                 printk(KERN_ERR "%s:%d fail (no memory for mg_host)\n",
856                                 __func__, __LINE__);
857                 err = -ENOMEM;
858                 goto probe_err;
859         }
860         host->major = MG_DISK_MAJ;
861
862         /* link each other */
863         prv_data->host = host;
864         host->dev = &plat_dev->dev;
865
866         /* io remap */
867         rsc = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
868         if (!rsc) {
869                 printk(KERN_ERR "%s:%d platform_get_resource fail\n",
870                                 __func__, __LINE__);
871                 err = -EINVAL;
872                 goto probe_err_2;
873         }
874         host->dev_base = ioremap(rsc->start , rsc->end + 1);
875         if (!host->dev_base) {
876                 printk(KERN_ERR "%s:%d ioremap fail\n",
877                                 __func__, __LINE__);
878                 err = -EIO;
879                 goto probe_err_2;
880         }
881         MG_DBG("dev_base = 0x%x\n", (u32)host->dev_base);
882
883         /* get reset pin */
884         rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
885                         MG_RST_PIN);
886         if (!rsc) {
887                 printk(KERN_ERR "%s:%d get reset pin fail\n",
888                                 __func__, __LINE__);
889                 err = -EIO;
890                 goto probe_err_3;
891         }
892         host->rst = rsc->start;
893
894         /* init rst pin */
895         err = gpio_request(host->rst, MG_RST_PIN);
896         if (err)
897                 goto probe_err_3;
898         gpio_direction_output(host->rst, 1);
899
900         /* reset out pin */
901         if (!(prv_data->dev_attr & MG_DEV_MASK))
902                 goto probe_err_3a;
903
904         if (prv_data->dev_attr != MG_BOOT_DEV) {
905                 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
906                                 MG_RSTOUT_PIN);
907                 if (!rsc) {
908                         printk(KERN_ERR "%s:%d get reset-out pin fail\n",
909                                         __func__, __LINE__);
910                         err = -EIO;
911                         goto probe_err_3a;
912                 }
913                 host->rstout = rsc->start;
914                 err = gpio_request(host->rstout, MG_RSTOUT_PIN);
915                 if (err)
916                         goto probe_err_3a;
917                 gpio_direction_input(host->rstout);
918         }
919
920         /* disk reset */
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);
924                 if (err)
925                         goto probe_err_3b;
926                 err = mg_disk_init(host);
927                 if (err) {
928                         printk(KERN_ERR "%s:%d fail (err code : %d)\n",
929                                         __func__, __LINE__, err);
930                         err = -EIO;
931                         goto probe_err_3b;
932                 }
933         }
934
935         /* get irq resource */
936         if (!prv_data->use_polling) {
937                 host->irq = platform_get_irq(plat_dev, 0);
938                 if (host->irq == -ENXIO) {
939                         err = host->irq;
940                         goto probe_err_3b;
941                 }
942                 err = request_irq(host->irq, mg_irq,
943                                 IRQF_DISABLED | IRQF_TRIGGER_RISING,
944                                 MG_DEV_NAME, host);
945                 if (err) {
946                         printk(KERN_ERR "%s:%d fail (request_irq err=%d)\n",
947                                         __func__, __LINE__, err);
948                         goto probe_err_3b;
949                 }
950
951         }
952
953         /* get disk id */
954         err = mg_get_disk_id(host);
955         if (err) {
956                 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
957                                 __func__, __LINE__, err);
958                 err = -EIO;
959                 goto probe_err_4;
960         }
961
962         err = register_blkdev(host->major, MG_DISK_NAME);
963         if (err < 0) {
964                 printk(KERN_ERR "%s:%d register_blkdev fail (err code : %d)\n",
965                                 __func__, __LINE__, err);
966                 goto probe_err_4;
967         }
968         if (!host->major)
969                 host->major = err;
970
971         spin_lock_init(&host->lock);
972
973         if (prv_data->use_polling)
974                 host->breq = blk_init_queue(mg_request_poll, &host->lock);
975         else
976                 host->breq = blk_init_queue(mg_request, &host->lock);
977
978         if (!host->breq) {
979                 err = -ENOMEM;
980                 printk(KERN_ERR "%s:%d (blk_init_queue) fail\n",
981                                 __func__, __LINE__);
982                 goto probe_err_5;
983         }
984
985         /* mflash is random device, thanx for the noop */
986         elevator_exit(host->breq->elevator);
987         err = elevator_init(host->breq, "noop");
988         if (err) {
989                 printk(KERN_ERR "%s:%d (elevator_init) fail\n",
990                                 __func__, __LINE__);
991                 goto probe_err_6;
992         }
993         blk_queue_max_sectors(host->breq, MG_MAX_SECTS);
994         blk_queue_hardsect_size(host->breq, MG_SECTOR_SIZE);
995
996         init_timer(&host->timer);
997         host->timer.function = mg_times_out;
998         host->timer.data = (unsigned long)host;
999
1000         host->gd = alloc_disk(MG_DISK_MAX_PART);
1001         if (!host->gd) {
1002                 printk(KERN_ERR "%s:%d (alloc_disk) fail\n",
1003                                 __func__, __LINE__);
1004                 err = -ENOMEM;
1005                 goto probe_err_7;
1006         }
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");
1013
1014         set_capacity(host->gd, host->n_sectors);
1015
1016         add_disk(host->gd);
1017
1018         return err;
1019
1020 probe_err_7:
1021         del_timer_sync(&host->timer);
1022 probe_err_6:
1023         blk_cleanup_queue(host->breq);
1024 probe_err_5:
1025         unregister_blkdev(MG_DISK_MAJ, MG_DISK_NAME);
1026 probe_err_4:
1027         if (!prv_data->use_polling)
1028                 free_irq(host->irq, host);
1029 probe_err_3b:
1030         gpio_free(host->rstout);
1031 probe_err_3a:
1032         gpio_free(host->rst);
1033 probe_err_3:
1034         iounmap(host->dev_base);
1035 probe_err_2:
1036         kfree(host);
1037 probe_err:
1038         return err;
1039 }
1040
1041 static int mg_remove(struct platform_device *plat_dev)
1042 {
1043         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
1044         struct mg_host *host = prv_data->host;
1045         int err = 0;
1046
1047         /* delete timer */
1048         del_timer_sync(&host->timer);
1049
1050         /* remove disk */
1051         if (host->gd) {
1052                 del_gendisk(host->gd);
1053                 put_disk(host->gd);
1054         }
1055         /* remove queue */
1056         if (host->breq)
1057                 blk_cleanup_queue(host->breq);
1058
1059         /* unregister blk device */
1060         unregister_blkdev(host->major, MG_DISK_NAME);
1061
1062         /* free irq */
1063         if (!prv_data->use_polling)
1064                 free_irq(host->irq, host);
1065
1066         /* free reset-out pin */
1067         if (prv_data->dev_attr != MG_BOOT_DEV)
1068                 gpio_free(host->rstout);
1069
1070         /* free rst pin */
1071         if (host->rst)
1072                 gpio_free(host->rst);
1073
1074         /* unmap io */
1075         if (host->dev_base)
1076                 iounmap(host->dev_base);
1077
1078         /* free mg_host */
1079         kfree(host);
1080
1081         return err;
1082 }
1083
1084 static struct platform_driver mg_disk_driver = {
1085         .probe = mg_probe,
1086         .remove = mg_remove,
1087         .suspend = mg_suspend,
1088         .resume = mg_resume,
1089         .driver = {
1090                 .name = MG_DEV_NAME,
1091                 .owner = THIS_MODULE,
1092         }
1093 };
1094
1095 /****************************************************************************
1096  *
1097  * Module stuff
1098  *
1099  ****************************************************************************/
1100
1101 static int __init mg_init(void)
1102 {
1103         printk(KERN_INFO "mGine mflash driver, (c) 2008 mGine Co.\n");
1104         return platform_driver_register(&mg_disk_driver);
1105 }
1106
1107 static void __exit mg_exit(void)
1108 {
1109         printk(KERN_INFO "mflash driver : bye bye\n");
1110         platform_driver_unregister(&mg_disk_driver);
1111 }
1112
1113 module_init(mg_init);
1114 module_exit(mg_exit);
1115
1116 MODULE_LICENSE("GPL");
1117 MODULE_AUTHOR("unsik Kim <donari75@gmail.com>");
1118 MODULE_DESCRIPTION("mGine m[g]flash device driver");