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