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