]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/block/mg_disk.c
mg_disk: remove prohibited sleep operation
[karo-tx-linux.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/ata.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/gpio.h>
25 #include <linux/mg_disk.h>
26
27 #define MG_RES_SEC (CONFIG_MG_DISK_RES << 1)
28
29 /* name for block device */
30 #define MG_DISK_NAME "mgd"
31
32 #define MG_DISK_MAJ 0
33 #define MG_DISK_MAX_PART 16
34 #define MG_SECTOR_SIZE 512
35 #define MG_MAX_SECTS 256
36
37 /* Register offsets */
38 #define MG_BUFF_OFFSET                  0x8000
39 #define MG_STORAGE_BUFFER_SIZE          0x200
40 #define MG_REG_OFFSET                   0xC000
41 #define MG_REG_FEATURE                  (MG_REG_OFFSET + 2)     /* write case */
42 #define MG_REG_ERROR                    (MG_REG_OFFSET + 2)     /* read case */
43 #define MG_REG_SECT_CNT                 (MG_REG_OFFSET + 4)
44 #define MG_REG_SECT_NUM                 (MG_REG_OFFSET + 6)
45 #define MG_REG_CYL_LOW                  (MG_REG_OFFSET + 8)
46 #define MG_REG_CYL_HIGH                 (MG_REG_OFFSET + 0xA)
47 #define MG_REG_DRV_HEAD                 (MG_REG_OFFSET + 0xC)
48 #define MG_REG_COMMAND                  (MG_REG_OFFSET + 0xE)   /* write case */
49 #define MG_REG_STATUS                   (MG_REG_OFFSET + 0xE)   /* read  case */
50 #define MG_REG_DRV_CTRL                 (MG_REG_OFFSET + 0x10)
51 #define MG_REG_BURST_CTRL               (MG_REG_OFFSET + 0x12)
52
53 /* handy status */
54 #define MG_STAT_READY   (ATA_DRDY | ATA_DSC)
55 #define MG_READY_OK(s)  (((s) & (MG_STAT_READY | (ATA_BUSY | ATA_DF | \
56                                  ATA_ERR))) == MG_STAT_READY)
57
58 /* error code for others */
59 #define MG_ERR_NONE             0
60 #define MG_ERR_TIMEOUT          0x100
61 #define MG_ERR_INIT_STAT        0x101
62 #define MG_ERR_TRANSLATION      0x102
63 #define MG_ERR_CTRL_RST         0x103
64 #define MG_ERR_INV_STAT         0x104
65 #define MG_ERR_RSTOUT           0x105
66
67 #define MG_MAX_ERRORS   6       /* Max read/write errors */
68
69 /* command */
70 #define MG_CMD_RD 0x20
71 #define MG_CMD_WR 0x30
72 #define MG_CMD_SLEEP 0x99
73 #define MG_CMD_WAKEUP 0xC3
74 #define MG_CMD_ID 0xEC
75 #define MG_CMD_WR_CONF 0x3C
76 #define MG_CMD_RD_CONF 0x40
77
78 /* operation mode */
79 #define MG_OP_CASCADE (1 << 0)
80 #define MG_OP_CASCADE_SYNC_RD (1 << 1)
81 #define MG_OP_CASCADE_SYNC_WR (1 << 2)
82 #define MG_OP_INTERLEAVE (1 << 3)
83
84 /* synchronous */
85 #define MG_BURST_LAT_4 (3 << 4)
86 #define MG_BURST_LAT_5 (4 << 4)
87 #define MG_BURST_LAT_6 (5 << 4)
88 #define MG_BURST_LAT_7 (6 << 4)
89 #define MG_BURST_LAT_8 (7 << 4)
90 #define MG_BURST_LEN_4 (1 << 1)
91 #define MG_BURST_LEN_8 (2 << 1)
92 #define MG_BURST_LEN_16 (3 << 1)
93 #define MG_BURST_LEN_32 (4 << 1)
94 #define MG_BURST_LEN_CONT (0 << 1)
95
96 /* timeout value (unit: ms) */
97 #define MG_TMAX_CONF_TO_CMD     1
98 #define MG_TMAX_WAIT_RD_DRQ     10
99 #define MG_TMAX_WAIT_WR_DRQ     500
100 #define MG_TMAX_RST_TO_BUSY     10
101 #define MG_TMAX_HDRST_TO_RDY    500
102 #define MG_TMAX_SWRST_TO_RDY    500
103 #define MG_TMAX_RSTOUT          3000
104
105 #define MG_DEV_MASK (MG_BOOT_DEV | MG_STORAGE_DEV | MG_STORAGE_DEV_SKIP_RST)
106
107 /* main structure for mflash driver */
108 struct mg_host {
109         struct device *dev;
110
111         struct request_queue *breq;
112         struct request *req;
113         spinlock_t lock;
114         struct gendisk *gd;
115
116         struct timer_list timer;
117         void (*mg_do_intr) (struct mg_host *);
118
119         u16 id[ATA_ID_WORDS];
120
121         u16 cyls;
122         u16 heads;
123         u16 sectors;
124         u32 n_sectors;
125         u32 nres_sectors;
126
127         void __iomem *dev_base;
128         unsigned int irq;
129         unsigned int rst;
130         unsigned int rstout;
131
132         u32 major;
133         u32 error;
134 };
135
136 /*
137  * Debugging macro and defines
138  */
139 #undef DO_MG_DEBUG
140 #ifdef DO_MG_DEBUG
141 #  define MG_DBG(fmt, args...) \
142         printk(KERN_DEBUG "%s:%d "fmt, __func__, __LINE__, ##args)
143 #else /* CONFIG_MG_DEBUG */
144 #  define MG_DBG(fmt, args...) do { } while (0)
145 #endif /* CONFIG_MG_DEBUG */
146
147 static void mg_request(struct request_queue *);
148
149 static bool mg_end_request(struct mg_host *host, int err, unsigned int nr_bytes)
150 {
151         if (__blk_end_request(host->req, err, nr_bytes))
152                 return true;
153
154         host->req = NULL;
155         return false;
156 }
157
158 static bool mg_end_request_cur(struct mg_host *host, int err)
159 {
160         return mg_end_request(host, err, blk_rq_cur_bytes(host->req));
161 }
162
163 static void mg_dump_status(const char *msg, unsigned int stat,
164                 struct mg_host *host)
165 {
166         char *name = MG_DISK_NAME;
167
168         if (host->req)
169                 name = host->req->rq_disk->disk_name;
170
171         printk(KERN_ERR "%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
172         if (stat & ATA_BUSY)
173                 printk("Busy ");
174         if (stat & ATA_DRDY)
175                 printk("DriveReady ");
176         if (stat & ATA_DF)
177                 printk("WriteFault ");
178         if (stat & ATA_DSC)
179                 printk("SeekComplete ");
180         if (stat & ATA_DRQ)
181                 printk("DataRequest ");
182         if (stat & ATA_CORR)
183                 printk("CorrectedError ");
184         if (stat & ATA_ERR)
185                 printk("Error ");
186         printk("}\n");
187         if ((stat & ATA_ERR) == 0) {
188                 host->error = 0;
189         } else {
190                 host->error = inb((unsigned long)host->dev_base + MG_REG_ERROR);
191                 printk(KERN_ERR "%s: %s: error=0x%02x { ", name, msg,
192                                 host->error & 0xff);
193                 if (host->error & ATA_BBK)
194                         printk("BadSector ");
195                 if (host->error & ATA_UNC)
196                         printk("UncorrectableError ");
197                 if (host->error & ATA_IDNF)
198                         printk("SectorIdNotFound ");
199                 if (host->error & ATA_ABORTED)
200                         printk("DriveStatusError ");
201                 if (host->error & ATA_AMNF)
202                         printk("AddrMarkNotFound ");
203                 printk("}");
204                 if (host->error & (ATA_BBK | ATA_UNC | ATA_IDNF | ATA_AMNF)) {
205                         if (host->req)
206                                 printk(", sector=%u",
207                                        (unsigned int)blk_rq_pos(host->req));
208                 }
209                 printk("\n");
210         }
211 }
212
213 static unsigned int mg_wait(struct mg_host *host, u32 expect, u32 msec)
214 {
215         u8 status;
216         unsigned long expire, cur_jiffies;
217         struct mg_drv_data *prv_data = host->dev->platform_data;
218
219         host->error = MG_ERR_NONE;
220         expire = jiffies + msecs_to_jiffies(msec);
221
222         status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
223
224         do {
225                 cur_jiffies = jiffies;
226                 if (status & ATA_BUSY) {
227                         if (expect == ATA_BUSY)
228                                 break;
229                 } else {
230                         /* Check the error condition! */
231                         if (status & ATA_ERR) {
232                                 mg_dump_status("mg_wait", status, host);
233                                 break;
234                         }
235
236                         if (expect == MG_STAT_READY)
237                                 if (MG_READY_OK(status))
238                                         break;
239
240                         if (expect == ATA_DRQ)
241                                 if (status & ATA_DRQ)
242                                         break;
243                 }
244                 if (!msec) {
245                         mg_dump_status("not ready", status, host);
246                         return MG_ERR_INV_STAT;
247                 }
248
249                 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
250         } while (time_before(cur_jiffies, expire));
251
252         if (time_after_eq(cur_jiffies, expire) && msec)
253                 host->error = MG_ERR_TIMEOUT;
254
255         return host->error;
256 }
257
258 static unsigned int mg_wait_rstout(u32 rstout, u32 msec)
259 {
260         unsigned long expire;
261
262         expire = jiffies + msecs_to_jiffies(msec);
263         while (time_before(jiffies, expire)) {
264                 if (gpio_get_value(rstout) == 1)
265                         return MG_ERR_NONE;
266                 msleep(10);
267         }
268
269         return MG_ERR_RSTOUT;
270 }
271
272 static void mg_unexpected_intr(struct mg_host *host)
273 {
274         u32 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
275
276         mg_dump_status("mg_unexpected_intr", status, host);
277 }
278
279 static irqreturn_t mg_irq(int irq, void *dev_id)
280 {
281         struct mg_host *host = dev_id;
282         void (*handler)(struct mg_host *) = host->mg_do_intr;
283
284         spin_lock(&host->lock);
285
286         host->mg_do_intr = NULL;
287         del_timer(&host->timer);
288         if (!handler)
289                 handler = mg_unexpected_intr;
290         handler(host);
291
292         spin_unlock(&host->lock);
293
294         return IRQ_HANDLED;
295 }
296
297 /* local copy of ata_id_string() */
298 static void mg_id_string(const u16 *id, unsigned char *s,
299                          unsigned int ofs, unsigned int len)
300 {
301         unsigned int c;
302
303         BUG_ON(len & 1);
304
305         while (len > 0) {
306                 c = id[ofs] >> 8;
307                 *s = c;
308                 s++;
309
310                 c = id[ofs] & 0xff;
311                 *s = c;
312                 s++;
313
314                 ofs++;
315                 len -= 2;
316         }
317 }
318
319 /* local copy of ata_id_c_string() */
320 static void mg_id_c_string(const u16 *id, unsigned char *s,
321                            unsigned int ofs, unsigned int len)
322 {
323         unsigned char *p;
324
325         mg_id_string(id, s, ofs, len - 1);
326
327         p = s + strnlen(s, len - 1);
328         while (p > s && p[-1] == ' ')
329                 p--;
330         *p = '\0';
331 }
332
333 static int mg_get_disk_id(struct mg_host *host)
334 {
335         u32 i;
336         s32 err;
337         const u16 *id = host->id;
338         struct mg_drv_data *prv_data = host->dev->platform_data;
339         char fwrev[ATA_ID_FW_REV_LEN + 1];
340         char model[ATA_ID_PROD_LEN + 1];
341         char serial[ATA_ID_SERNO_LEN + 1];
342
343         if (!prv_data->use_polling)
344                 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
345
346         outb(MG_CMD_ID, (unsigned long)host->dev_base + MG_REG_COMMAND);
347         err = mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_RD_DRQ);
348         if (err)
349                 return err;
350
351         for (i = 0; i < (MG_SECTOR_SIZE >> 1); i++)
352                 host->id[i] = le16_to_cpu(inw((unsigned long)host->dev_base +
353                                         MG_BUFF_OFFSET + i * 2));
354
355         outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
356         err = mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD);
357         if (err)
358                 return err;
359
360         if ((id[ATA_ID_FIELD_VALID] & 1) == 0)
361                 return MG_ERR_TRANSLATION;
362
363         host->n_sectors = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
364         host->cyls = id[ATA_ID_CYLS];
365         host->heads = id[ATA_ID_HEADS];
366         host->sectors = id[ATA_ID_SECTORS];
367
368         if (MG_RES_SEC && host->heads && host->sectors) {
369                 /* modify cyls, n_sectors */
370                 host->cyls = (host->n_sectors - MG_RES_SEC) /
371                         host->heads / host->sectors;
372                 host->nres_sectors = host->n_sectors - host->cyls *
373                         host->heads * host->sectors;
374                 host->n_sectors -= host->nres_sectors;
375         }
376
377         mg_id_c_string(id, fwrev, ATA_ID_FW_REV, sizeof(fwrev));
378         mg_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
379         mg_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
380         printk(KERN_INFO "mg_disk: model: %s\n", model);
381         printk(KERN_INFO "mg_disk: firm: %.8s\n", fwrev);
382         printk(KERN_INFO "mg_disk: serial: %s\n", serial);
383         printk(KERN_INFO "mg_disk: %d + reserved %d sectors\n",
384                         host->n_sectors, host->nres_sectors);
385
386         if (!prv_data->use_polling)
387                 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
388
389         return err;
390 }
391
392
393 static int mg_disk_init(struct mg_host *host)
394 {
395         struct mg_drv_data *prv_data = host->dev->platform_data;
396         s32 err;
397         u8 init_status;
398
399         /* hdd rst low */
400         gpio_set_value(host->rst, 0);
401         err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
402         if (err)
403                 return err;
404
405         /* hdd rst high */
406         gpio_set_value(host->rst, 1);
407         err = mg_wait(host, MG_STAT_READY, MG_TMAX_HDRST_TO_RDY);
408         if (err)
409                 return err;
410
411         /* soft reset on */
412         outb(ATA_SRST | (prv_data->use_polling ? ATA_NIEN : 0),
413                         (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
414         err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
415         if (err)
416                 return err;
417
418         /* soft reset off */
419         outb(prv_data->use_polling ? ATA_NIEN : 0,
420                         (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
421         err = mg_wait(host, MG_STAT_READY, MG_TMAX_SWRST_TO_RDY);
422         if (err)
423                 return err;
424
425         init_status = inb((unsigned long)host->dev_base + MG_REG_STATUS) & 0xf;
426
427         if (init_status == 0xf)
428                 return MG_ERR_INIT_STAT;
429
430         return err;
431 }
432
433 static void mg_bad_rw_intr(struct mg_host *host)
434 {
435         if (host->req)
436                 if (++host->req->errors >= MG_MAX_ERRORS ||
437                     host->error == MG_ERR_TIMEOUT)
438                         mg_end_request_cur(host, -EIO);
439 }
440
441 static unsigned int mg_out(struct mg_host *host,
442                 unsigned int sect_num,
443                 unsigned int sect_cnt,
444                 unsigned int cmd,
445                 void (*intr_addr)(struct mg_host *))
446 {
447         struct mg_drv_data *prv_data = host->dev->platform_data;
448
449         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
450                 return host->error;
451
452         if (!prv_data->use_polling) {
453                 host->mg_do_intr = intr_addr;
454                 mod_timer(&host->timer, jiffies + 3 * HZ);
455         }
456         if (MG_RES_SEC)
457                 sect_num += MG_RES_SEC;
458         outb((u8)sect_cnt, (unsigned long)host->dev_base + MG_REG_SECT_CNT);
459         outb((u8)sect_num, (unsigned long)host->dev_base + MG_REG_SECT_NUM);
460         outb((u8)(sect_num >> 8), (unsigned long)host->dev_base +
461                         MG_REG_CYL_LOW);
462         outb((u8)(sect_num >> 16), (unsigned long)host->dev_base +
463                         MG_REG_CYL_HIGH);
464         outb((u8)((sect_num >> 24) | ATA_LBA | ATA_DEVICE_OBS),
465                         (unsigned long)host->dev_base + MG_REG_DRV_HEAD);
466         outb(cmd, (unsigned long)host->dev_base + MG_REG_COMMAND);
467         return MG_ERR_NONE;
468 }
469
470 static void mg_read(struct request *req)
471 {
472         u32 j;
473         struct mg_host *host = req->rq_disk->private_data;
474
475         if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req),
476                    MG_CMD_RD, NULL) != MG_ERR_NONE)
477                 mg_bad_rw_intr(host);
478
479         MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
480                blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
481
482         do {
483                 u16 *buff = (u16 *)req->buffer;
484
485                 if (mg_wait(host, ATA_DRQ,
486                             MG_TMAX_WAIT_RD_DRQ) != MG_ERR_NONE) {
487                         mg_bad_rw_intr(host);
488                         return;
489                 }
490                 for (j = 0; j < MG_SECTOR_SIZE >> 1; j++)
491                         *buff++ = inw((unsigned long)host->dev_base +
492                                       MG_BUFF_OFFSET + (j << 1));
493
494                 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base +
495                                 MG_REG_COMMAND);
496         } while (mg_end_request(host, 0, MG_SECTOR_SIZE));
497 }
498
499 static void mg_write(struct request *req)
500 {
501         u32 j;
502         struct mg_host *host = req->rq_disk->private_data;
503
504         if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req),
505                    MG_CMD_WR, NULL) != MG_ERR_NONE) {
506                 mg_bad_rw_intr(host);
507                 return;
508         }
509
510         MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
511                blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
512
513         do {
514                 u16 *buff = (u16 *)req->buffer;
515
516         if (mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
517                         mg_bad_rw_intr(host);
518                         return;
519                 }
520                 for (j = 0; j < MG_SECTOR_SIZE >> 1; j++)
521                         outw(*buff++, (unsigned long)host->dev_base +
522                                       MG_BUFF_OFFSET + (j << 1));
523
524                 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
525                                 MG_REG_COMMAND);
526         } while (mg_end_request(host, 0, MG_SECTOR_SIZE));
527 }
528
529 static void mg_read_intr(struct mg_host *host)
530 {
531         struct request *req = host->req;
532         u32 i;
533         u16 *buff;
534
535         /* check status */
536         do {
537                 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
538                 if (i & ATA_BUSY)
539                         break;
540                 if (!MG_READY_OK(i))
541                         break;
542                 if (i & ATA_DRQ)
543                         goto ok_to_read;
544         } while (0);
545         mg_dump_status("mg_read_intr", i, host);
546         mg_bad_rw_intr(host);
547         mg_request(host->breq);
548         return;
549
550 ok_to_read:
551         /* get current segment of request */
552         buff = (u16 *)req->buffer;
553
554         /* read 1 sector */
555         for (i = 0; i < MG_SECTOR_SIZE >> 1; i++)
556                 *buff++ = inw((unsigned long)host->dev_base + MG_BUFF_OFFSET +
557                               (i << 1));
558
559         MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
560                blk_rq_pos(req), blk_rq_sectors(req) - 1, req->buffer);
561
562         /* send read confirm */
563         outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
564
565         if (mg_end_request(host, 0, MG_SECTOR_SIZE)) {
566                 /* set handler if read remains */
567                 host->mg_do_intr = mg_read_intr;
568                 mod_timer(&host->timer, jiffies + 3 * HZ);
569         } else /* goto next request */
570                 mg_request(host->breq);
571 }
572
573 static void mg_write_intr(struct mg_host *host)
574 {
575         struct request *req = host->req;
576         u32 i, j;
577         u16 *buff;
578         bool rem;
579
580         /* check status */
581         do {
582                 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
583                 if (i & ATA_BUSY)
584                         break;
585                 if (!MG_READY_OK(i))
586                         break;
587                 if ((blk_rq_sectors(req) <= 1) || (i & ATA_DRQ))
588                         goto ok_to_write;
589         } while (0);
590         mg_dump_status("mg_write_intr", i, host);
591         mg_bad_rw_intr(host);
592         mg_request(host->breq);
593         return;
594
595 ok_to_write:
596         if ((rem = mg_end_request(host, 0, MG_SECTOR_SIZE))) {
597                 /* write 1 sector and set handler if remains */
598                 buff = (u16 *)req->buffer;
599                 for (j = 0; j < MG_STORAGE_BUFFER_SIZE >> 1; j++) {
600                         outw(*buff, (unsigned long)host->dev_base +
601                                         MG_BUFF_OFFSET + (j << 1));
602                         buff++;
603                 }
604                 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
605                        blk_rq_pos(req), blk_rq_sectors(req), req->buffer);
606                 host->mg_do_intr = mg_write_intr;
607                 mod_timer(&host->timer, jiffies + 3 * HZ);
608         }
609
610         /* send write confirm */
611         outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
612
613         if (!rem)
614                 mg_request(host->breq);
615 }
616
617 void mg_times_out(unsigned long data)
618 {
619         struct mg_host *host = (struct mg_host *)data;
620         char *name;
621
622         spin_lock_irq(&host->lock);
623
624         if (!host->req)
625                 goto out_unlock;
626
627         host->mg_do_intr = NULL;
628
629         name = host->req->rq_disk->disk_name;
630         printk(KERN_DEBUG "%s: timeout\n", name);
631
632         host->error = MG_ERR_TIMEOUT;
633         mg_bad_rw_intr(host);
634
635 out_unlock:
636         mg_request(host->breq);
637         spin_unlock_irq(&host->lock);
638 }
639
640 static void mg_request_poll(struct request_queue *q)
641 {
642         struct mg_host *host = q->queuedata;
643
644         while (1) {
645                 if (!host->req) {
646                         host->req = blk_fetch_request(q);
647                         if (!host->req)
648                                 break;
649                 }
650
651                 if (unlikely(!blk_fs_request(host->req))) {
652                         mg_end_request_cur(host, -EIO);
653                         continue;
654                 }
655
656                 if (rq_data_dir(host->req) == READ)
657                         mg_read(host->req);
658                 else
659                         mg_write(host->req);
660         }
661 }
662
663 static unsigned int mg_issue_req(struct request *req,
664                 struct mg_host *host,
665                 unsigned int sect_num,
666                 unsigned int sect_cnt)
667 {
668         u16 *buff;
669         u32 i;
670
671         switch (rq_data_dir(req)) {
672         case READ:
673                 if (mg_out(host, sect_num, sect_cnt, MG_CMD_RD, &mg_read_intr)
674                                 != MG_ERR_NONE) {
675                         mg_bad_rw_intr(host);
676                         return host->error;
677                 }
678                 break;
679         case WRITE:
680                 /* TODO : handler */
681                 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
682                 if (mg_out(host, sect_num, sect_cnt, MG_CMD_WR, &mg_write_intr)
683                                 != MG_ERR_NONE) {
684                         mg_bad_rw_intr(host);
685                         return host->error;
686                 }
687                 del_timer(&host->timer);
688                 mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ);
689                 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
690                 if (host->error) {
691                         mg_bad_rw_intr(host);
692                         return host->error;
693                 }
694                 buff = (u16 *)req->buffer;
695                 for (i = 0; i < MG_SECTOR_SIZE >> 1; i++) {
696                         outw(*buff, (unsigned long)host->dev_base +
697                                         MG_BUFF_OFFSET + (i << 1));
698                         buff++;
699                 }
700                 mod_timer(&host->timer, jiffies + 3 * HZ);
701                 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
702                                 MG_REG_COMMAND);
703                 break;
704         }
705         return MG_ERR_NONE;
706 }
707
708 /* This function also called from IRQ context */
709 static void mg_request(struct request_queue *q)
710 {
711         struct mg_host *host = q->queuedata;
712         struct request *req;
713         u32 sect_num, sect_cnt;
714
715         while (1) {
716                 if (!host->req) {
717                         host->req = blk_fetch_request(q);
718                         if (!host->req)
719                                 break;
720                 }
721                 req = host->req;
722
723                 /* check unwanted request call */
724                 if (host->mg_do_intr)
725                         return;
726
727                 del_timer(&host->timer);
728
729                 sect_num = blk_rq_pos(req);
730                 /* deal whole segments */
731                 sect_cnt = blk_rq_sectors(req);
732
733                 /* sanity check */
734                 if (sect_num >= get_capacity(req->rq_disk) ||
735                                 ((sect_num + sect_cnt) >
736                                  get_capacity(req->rq_disk))) {
737                         printk(KERN_WARNING
738                                         "%s: bad access: sector=%d, count=%d\n",
739                                         req->rq_disk->disk_name,
740                                         sect_num, sect_cnt);
741                         mg_end_request_cur(host, -EIO);
742                         continue;
743                 }
744
745                 if (unlikely(!blk_fs_request(req))) {
746                         mg_end_request_cur(host, -EIO);
747                         continue;
748                 }
749
750                 if (!mg_issue_req(req, host, sect_num, sect_cnt))
751                         return;
752         }
753 }
754
755 static int mg_getgeo(struct block_device *bdev, struct hd_geometry *geo)
756 {
757         struct mg_host *host = bdev->bd_disk->private_data;
758
759         geo->cylinders = (unsigned short)host->cyls;
760         geo->heads = (unsigned char)host->heads;
761         geo->sectors = (unsigned char)host->sectors;
762         return 0;
763 }
764
765 static struct block_device_operations mg_disk_ops = {
766         .getgeo = mg_getgeo
767 };
768
769 static int mg_suspend(struct platform_device *plat_dev, pm_message_t state)
770 {
771         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
772         struct mg_host *host = prv_data->host;
773
774         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
775                 return -EIO;
776
777         if (!prv_data->use_polling)
778                 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
779
780         outb(MG_CMD_SLEEP, (unsigned long)host->dev_base + MG_REG_COMMAND);
781         /* wait until mflash deep sleep */
782         msleep(1);
783
784         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) {
785                 if (!prv_data->use_polling)
786                         outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
787                 return -EIO;
788         }
789
790         return 0;
791 }
792
793 static int mg_resume(struct platform_device *plat_dev)
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         outb(MG_CMD_WAKEUP, (unsigned long)host->dev_base + MG_REG_COMMAND);
802         /* wait until mflash wakeup */
803         msleep(1);
804
805         if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
806                 return -EIO;
807
808         if (!prv_data->use_polling)
809                 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
810
811         return 0;
812 }
813
814 static int mg_probe(struct platform_device *plat_dev)
815 {
816         struct mg_host *host;
817         struct resource *rsc;
818         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
819         int err = 0;
820
821         if (!prv_data) {
822                 printk(KERN_ERR "%s:%d fail (no driver_data)\n",
823                                 __func__, __LINE__);
824                 err = -EINVAL;
825                 goto probe_err;
826         }
827
828         /* alloc mg_host */
829         host = kzalloc(sizeof(struct mg_host), GFP_KERNEL);
830         if (!host) {
831                 printk(KERN_ERR "%s:%d fail (no memory for mg_host)\n",
832                                 __func__, __LINE__);
833                 err = -ENOMEM;
834                 goto probe_err;
835         }
836         host->major = MG_DISK_MAJ;
837
838         /* link each other */
839         prv_data->host = host;
840         host->dev = &plat_dev->dev;
841
842         /* io remap */
843         rsc = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
844         if (!rsc) {
845                 printk(KERN_ERR "%s:%d platform_get_resource fail\n",
846                                 __func__, __LINE__);
847                 err = -EINVAL;
848                 goto probe_err_2;
849         }
850         host->dev_base = ioremap(rsc->start , rsc->end + 1);
851         if (!host->dev_base) {
852                 printk(KERN_ERR "%s:%d ioremap fail\n",
853                                 __func__, __LINE__);
854                 err = -EIO;
855                 goto probe_err_2;
856         }
857         MG_DBG("dev_base = 0x%x\n", (u32)host->dev_base);
858
859         /* get reset pin */
860         rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
861                         MG_RST_PIN);
862         if (!rsc) {
863                 printk(KERN_ERR "%s:%d get reset pin fail\n",
864                                 __func__, __LINE__);
865                 err = -EIO;
866                 goto probe_err_3;
867         }
868         host->rst = rsc->start;
869
870         /* init rst pin */
871         err = gpio_request(host->rst, MG_RST_PIN);
872         if (err)
873                 goto probe_err_3;
874         gpio_direction_output(host->rst, 1);
875
876         /* reset out pin */
877         if (!(prv_data->dev_attr & MG_DEV_MASK))
878                 goto probe_err_3a;
879
880         if (prv_data->dev_attr != MG_BOOT_DEV) {
881                 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
882                                 MG_RSTOUT_PIN);
883                 if (!rsc) {
884                         printk(KERN_ERR "%s:%d get reset-out pin fail\n",
885                                         __func__, __LINE__);
886                         err = -EIO;
887                         goto probe_err_3a;
888                 }
889                 host->rstout = rsc->start;
890                 err = gpio_request(host->rstout, MG_RSTOUT_PIN);
891                 if (err)
892                         goto probe_err_3a;
893                 gpio_direction_input(host->rstout);
894         }
895
896         /* disk reset */
897         if (prv_data->dev_attr == MG_STORAGE_DEV) {
898                 /* If POR seq. not yet finised, wait */
899                 err = mg_wait_rstout(host->rstout, MG_TMAX_RSTOUT);
900                 if (err)
901                         goto probe_err_3b;
902                 err = mg_disk_init(host);
903                 if (err) {
904                         printk(KERN_ERR "%s:%d fail (err code : %d)\n",
905                                         __func__, __LINE__, err);
906                         err = -EIO;
907                         goto probe_err_3b;
908                 }
909         }
910
911         /* get irq resource */
912         if (!prv_data->use_polling) {
913                 host->irq = platform_get_irq(plat_dev, 0);
914                 if (host->irq == -ENXIO) {
915                         err = host->irq;
916                         goto probe_err_3b;
917                 }
918                 err = request_irq(host->irq, mg_irq,
919                                 IRQF_DISABLED | IRQF_TRIGGER_RISING,
920                                 MG_DEV_NAME, host);
921                 if (err) {
922                         printk(KERN_ERR "%s:%d fail (request_irq err=%d)\n",
923                                         __func__, __LINE__, err);
924                         goto probe_err_3b;
925                 }
926
927         }
928
929         /* get disk id */
930         err = mg_get_disk_id(host);
931         if (err) {
932                 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
933                                 __func__, __LINE__, err);
934                 err = -EIO;
935                 goto probe_err_4;
936         }
937
938         err = register_blkdev(host->major, MG_DISK_NAME);
939         if (err < 0) {
940                 printk(KERN_ERR "%s:%d register_blkdev fail (err code : %d)\n",
941                                 __func__, __LINE__, err);
942                 goto probe_err_4;
943         }
944         if (!host->major)
945                 host->major = err;
946
947         spin_lock_init(&host->lock);
948
949         if (prv_data->use_polling)
950                 host->breq = blk_init_queue(mg_request_poll, &host->lock);
951         else
952                 host->breq = blk_init_queue(mg_request, &host->lock);
953
954         if (!host->breq) {
955                 err = -ENOMEM;
956                 printk(KERN_ERR "%s:%d (blk_init_queue) fail\n",
957                                 __func__, __LINE__);
958                 goto probe_err_5;
959         }
960         host->breq->queuedata = host;
961
962         /* mflash is random device, thanx for the noop */
963         elevator_exit(host->breq->elevator);
964         err = elevator_init(host->breq, "noop");
965         if (err) {
966                 printk(KERN_ERR "%s:%d (elevator_init) fail\n",
967                                 __func__, __LINE__);
968                 goto probe_err_6;
969         }
970         blk_queue_max_sectors(host->breq, MG_MAX_SECTS);
971         blk_queue_logical_block_size(host->breq, MG_SECTOR_SIZE);
972
973         init_timer(&host->timer);
974         host->timer.function = mg_times_out;
975         host->timer.data = (unsigned long)host;
976
977         host->gd = alloc_disk(MG_DISK_MAX_PART);
978         if (!host->gd) {
979                 printk(KERN_ERR "%s:%d (alloc_disk) fail\n",
980                                 __func__, __LINE__);
981                 err = -ENOMEM;
982                 goto probe_err_7;
983         }
984         host->gd->major = host->major;
985         host->gd->first_minor = 0;
986         host->gd->fops = &mg_disk_ops;
987         host->gd->queue = host->breq;
988         host->gd->private_data = host;
989         sprintf(host->gd->disk_name, MG_DISK_NAME"a");
990
991         set_capacity(host->gd, host->n_sectors);
992
993         add_disk(host->gd);
994
995         return err;
996
997 probe_err_7:
998         del_timer_sync(&host->timer);
999 probe_err_6:
1000         blk_cleanup_queue(host->breq);
1001 probe_err_5:
1002         unregister_blkdev(MG_DISK_MAJ, MG_DISK_NAME);
1003 probe_err_4:
1004         if (!prv_data->use_polling)
1005                 free_irq(host->irq, host);
1006 probe_err_3b:
1007         gpio_free(host->rstout);
1008 probe_err_3a:
1009         gpio_free(host->rst);
1010 probe_err_3:
1011         iounmap(host->dev_base);
1012 probe_err_2:
1013         kfree(host);
1014 probe_err:
1015         return err;
1016 }
1017
1018 static int mg_remove(struct platform_device *plat_dev)
1019 {
1020         struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
1021         struct mg_host *host = prv_data->host;
1022         int err = 0;
1023
1024         /* delete timer */
1025         del_timer_sync(&host->timer);
1026
1027         /* remove disk */
1028         if (host->gd) {
1029                 del_gendisk(host->gd);
1030                 put_disk(host->gd);
1031         }
1032         /* remove queue */
1033         if (host->breq)
1034                 blk_cleanup_queue(host->breq);
1035
1036         /* unregister blk device */
1037         unregister_blkdev(host->major, MG_DISK_NAME);
1038
1039         /* free irq */
1040         if (!prv_data->use_polling)
1041                 free_irq(host->irq, host);
1042
1043         /* free reset-out pin */
1044         if (prv_data->dev_attr != MG_BOOT_DEV)
1045                 gpio_free(host->rstout);
1046
1047         /* free rst pin */
1048         if (host->rst)
1049                 gpio_free(host->rst);
1050
1051         /* unmap io */
1052         if (host->dev_base)
1053                 iounmap(host->dev_base);
1054
1055         /* free mg_host */
1056         kfree(host);
1057
1058         return err;
1059 }
1060
1061 static struct platform_driver mg_disk_driver = {
1062         .probe = mg_probe,
1063         .remove = mg_remove,
1064         .suspend = mg_suspend,
1065         .resume = mg_resume,
1066         .driver = {
1067                 .name = MG_DEV_NAME,
1068                 .owner = THIS_MODULE,
1069         }
1070 };
1071
1072 /****************************************************************************
1073  *
1074  * Module stuff
1075  *
1076  ****************************************************************************/
1077
1078 static int __init mg_init(void)
1079 {
1080         printk(KERN_INFO "mGine mflash driver, (c) 2008 mGine Co.\n");
1081         return platform_driver_register(&mg_disk_driver);
1082 }
1083
1084 static void __exit mg_exit(void)
1085 {
1086         printk(KERN_INFO "mflash driver : bye bye\n");
1087         platform_driver_unregister(&mg_disk_driver);
1088 }
1089
1090 module_init(mg_init);
1091 module_exit(mg_exit);
1092
1093 MODULE_LICENSE("GPL");
1094 MODULE_AUTHOR("unsik Kim <donari75@gmail.com>");
1095 MODULE_DESCRIPTION("mGine m[g]flash device driver");