blob: 1ca5d1423fa3a0c1abe363f9c798ff2cb75155b7 [file] [log] [blame]
unsik Kim3fbed4c2009-04-02 12:50:58 -07001/*
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>
Bartlomiej Zolnierkiewicz8a11a782009-04-28 13:06:16 +090020#include <linux/ata.h>
unsik Kim3fbed4c2009-04-02 12:50:58 -070021#include <linux/interrupt.h>
22#include <linux/delay.h>
23#include <linux/platform_device.h>
24#include <linux/gpio.h>
unsik Kim3fbed4c2009-04-02 12:50:58 -070025
26#define MG_RES_SEC (CONFIG_MG_DISK_RES << 1)
27
Tejun Heoeec94622009-04-28 13:06:14 +090028/* 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
Tejun Heoeec94622009-04-28 13:06:14 +090054/* handy status */
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +090055#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)
Tejun Heoeec94622009-04-28 13:06:14 +090058
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;
Tejun Heo5b36ad62009-05-08 11:54:01 +0900138 struct request *req;
Tejun Heoeec94622009-04-28 13:06:14 +0900139 spinlock_t lock;
140 struct gendisk *gd;
141
142 struct timer_list timer;
143 void (*mg_do_intr) (struct mg_host *);
144
145 u16 id[ATA_ID_WORDS];
146
147 u16 cyls;
148 u16 heads;
149 u16 sectors;
150 u32 n_sectors;
151 u32 nres_sectors;
152
153 void __iomem *dev_base;
154 unsigned int irq;
155 unsigned int rst;
156 unsigned int rstout;
157
158 u32 major;
159 u32 error;
160};
161
162/*
163 * Debugging macro and defines
164 */
165#undef DO_MG_DEBUG
166#ifdef DO_MG_DEBUG
167# define MG_DBG(fmt, args...) \
168 printk(KERN_DEBUG "%s:%d "fmt, __func__, __LINE__, ##args)
169#else /* CONFIG_MG_DEBUG */
170# define MG_DBG(fmt, args...) do { } while (0)
171#endif /* CONFIG_MG_DEBUG */
172
unsik Kim3fbed4c2009-04-02 12:50:58 -0700173static void mg_request(struct request_queue *);
174
Tejun Heo5b36ad62009-05-08 11:54:01 +0900175static bool mg_end_request(struct mg_host *host, int err, unsigned int nr_bytes)
176{
177 if (__blk_end_request(host->req, err, nr_bytes))
178 return true;
179
180 host->req = NULL;
181 return false;
182}
183
184static bool mg_end_request_cur(struct mg_host *host, int err)
185{
186 return mg_end_request(host, err, blk_rq_cur_bytes(host->req));
187}
188
unsik Kim3fbed4c2009-04-02 12:50:58 -0700189static void mg_dump_status(const char *msg, unsigned int stat,
190 struct mg_host *host)
191{
192 char *name = MG_DISK_NAME;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700193
Tejun Heo5b36ad62009-05-08 11:54:01 +0900194 if (host->req)
195 name = host->req->rq_disk->disk_name;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700196
197 printk(KERN_ERR "%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900198 if (stat & ATA_BUSY)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700199 printk("Busy ");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900200 if (stat & ATA_DRDY)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700201 printk("DriveReady ");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900202 if (stat & ATA_DF)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700203 printk("WriteFault ");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900204 if (stat & ATA_DSC)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700205 printk("SeekComplete ");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900206 if (stat & ATA_DRQ)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700207 printk("DataRequest ");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900208 if (stat & ATA_CORR)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700209 printk("CorrectedError ");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900210 if (stat & ATA_ERR)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700211 printk("Error ");
212 printk("}\n");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900213 if ((stat & ATA_ERR) == 0) {
unsik Kim3fbed4c2009-04-02 12:50:58 -0700214 host->error = 0;
215 } else {
216 host->error = inb((unsigned long)host->dev_base + MG_REG_ERROR);
217 printk(KERN_ERR "%s: %s: error=0x%02x { ", name, msg,
218 host->error & 0xff);
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900219 if (host->error & ATA_BBK)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700220 printk("BadSector ");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900221 if (host->error & ATA_UNC)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700222 printk("UncorrectableError ");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900223 if (host->error & ATA_IDNF)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700224 printk("SectorIdNotFound ");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900225 if (host->error & ATA_ABORTED)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700226 printk("DriveStatusError ");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900227 if (host->error & ATA_AMNF)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700228 printk("AddrMarkNotFound ");
229 printk("}");
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900230 if (host->error & (ATA_BBK | ATA_UNC | ATA_IDNF | ATA_AMNF)) {
Tejun Heo5b36ad62009-05-08 11:54:01 +0900231 if (host->req)
232 printk(", sector=%u",
233 (unsigned int)blk_rq_pos(host->req));
unsik Kim3fbed4c2009-04-02 12:50:58 -0700234 }
235 printk("\n");
236 }
237}
238
239static unsigned int mg_wait(struct mg_host *host, u32 expect, u32 msec)
240{
241 u8 status;
242 unsigned long expire, cur_jiffies;
243 struct mg_drv_data *prv_data = host->dev->platform_data;
244
245 host->error = MG_ERR_NONE;
246 expire = jiffies + msecs_to_jiffies(msec);
247
248 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
249
250 do {
251 cur_jiffies = jiffies;
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900252 if (status & ATA_BUSY) {
253 if (expect == ATA_BUSY)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700254 break;
255 } else {
256 /* Check the error condition! */
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900257 if (status & ATA_ERR) {
unsik Kim3fbed4c2009-04-02 12:50:58 -0700258 mg_dump_status("mg_wait", status, host);
259 break;
260 }
261
262 if (expect == MG_STAT_READY)
263 if (MG_READY_OK(status))
264 break;
265
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900266 if (expect == ATA_DRQ)
267 if (status & ATA_DRQ)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700268 break;
269 }
270 if (!msec) {
271 mg_dump_status("not ready", status, host);
272 return MG_ERR_INV_STAT;
273 }
274 if (prv_data->use_polling)
275 msleep(1);
276
277 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
278 } while (time_before(cur_jiffies, expire));
279
280 if (time_after_eq(cur_jiffies, expire) && msec)
281 host->error = MG_ERR_TIMEOUT;
282
283 return host->error;
284}
285
286static unsigned int mg_wait_rstout(u32 rstout, u32 msec)
287{
288 unsigned long expire;
289
290 expire = jiffies + msecs_to_jiffies(msec);
291 while (time_before(jiffies, expire)) {
292 if (gpio_get_value(rstout) == 1)
293 return MG_ERR_NONE;
294 msleep(10);
295 }
296
297 return MG_ERR_RSTOUT;
298}
299
300static void mg_unexpected_intr(struct mg_host *host)
301{
302 u32 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
303
304 mg_dump_status("mg_unexpected_intr", status, host);
305}
306
307static irqreturn_t mg_irq(int irq, void *dev_id)
308{
309 struct mg_host *host = dev_id;
310 void (*handler)(struct mg_host *) = host->mg_do_intr;
311
Tejun Heoac2ff942009-04-28 12:38:32 +0900312 spin_lock(&host->lock);
313
314 host->mg_do_intr = NULL;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700315 del_timer(&host->timer);
316 if (!handler)
317 handler = mg_unexpected_intr;
318 handler(host);
Tejun Heoac2ff942009-04-28 12:38:32 +0900319
320 spin_unlock(&host->lock);
321
unsik Kim3fbed4c2009-04-02 12:50:58 -0700322 return IRQ_HANDLED;
323}
324
Bartlomiej Zolnierkiewicz8a11a782009-04-28 13:06:16 +0900325/* local copy of ata_id_string() */
326static void mg_id_string(const u16 *id, unsigned char *s,
327 unsigned int ofs, unsigned int len)
328{
329 unsigned int c;
330
331 BUG_ON(len & 1);
332
333 while (len > 0) {
334 c = id[ofs] >> 8;
335 *s = c;
336 s++;
337
338 c = id[ofs] & 0xff;
339 *s = c;
340 s++;
341
342 ofs++;
343 len -= 2;
344 }
345}
346
347/* local copy of ata_id_c_string() */
348static void mg_id_c_string(const u16 *id, unsigned char *s,
349 unsigned int ofs, unsigned int len)
350{
351 unsigned char *p;
352
353 mg_id_string(id, s, ofs, len - 1);
354
355 p = s + strnlen(s, len - 1);
356 while (p > s && p[-1] == ' ')
357 p--;
358 *p = '\0';
359}
360
unsik Kim3fbed4c2009-04-02 12:50:58 -0700361static int mg_get_disk_id(struct mg_host *host)
362{
363 u32 i;
364 s32 err;
365 const u16 *id = host->id;
366 struct mg_drv_data *prv_data = host->dev->platform_data;
367 char fwrev[ATA_ID_FW_REV_LEN + 1];
368 char model[ATA_ID_PROD_LEN + 1];
369 char serial[ATA_ID_SERNO_LEN + 1];
370
371 if (!prv_data->use_polling)
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900372 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700373
374 outb(MG_CMD_ID, (unsigned long)host->dev_base + MG_REG_COMMAND);
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900375 err = mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_RD_DRQ);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700376 if (err)
377 return err;
378
379 for (i = 0; i < (MG_SECTOR_SIZE >> 1); i++)
380 host->id[i] = le16_to_cpu(inw((unsigned long)host->dev_base +
381 MG_BUFF_OFFSET + i * 2));
382
383 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
384 err = mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD);
385 if (err)
386 return err;
387
388 if ((id[ATA_ID_FIELD_VALID] & 1) == 0)
389 return MG_ERR_TRANSLATION;
390
391 host->n_sectors = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
392 host->cyls = id[ATA_ID_CYLS];
393 host->heads = id[ATA_ID_HEADS];
394 host->sectors = id[ATA_ID_SECTORS];
395
396 if (MG_RES_SEC && host->heads && host->sectors) {
397 /* modify cyls, n_sectors */
398 host->cyls = (host->n_sectors - MG_RES_SEC) /
399 host->heads / host->sectors;
400 host->nres_sectors = host->n_sectors - host->cyls *
401 host->heads * host->sectors;
402 host->n_sectors -= host->nres_sectors;
403 }
404
Bartlomiej Zolnierkiewicz8a11a782009-04-28 13:06:16 +0900405 mg_id_c_string(id, fwrev, ATA_ID_FW_REV, sizeof(fwrev));
406 mg_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
407 mg_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
unsik Kim3fbed4c2009-04-02 12:50:58 -0700408 printk(KERN_INFO "mg_disk: model: %s\n", model);
409 printk(KERN_INFO "mg_disk: firm: %.8s\n", fwrev);
410 printk(KERN_INFO "mg_disk: serial: %s\n", serial);
411 printk(KERN_INFO "mg_disk: %d + reserved %d sectors\n",
412 host->n_sectors, host->nres_sectors);
413
414 if (!prv_data->use_polling)
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900415 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700416
417 return err;
418}
419
420
421static int mg_disk_init(struct mg_host *host)
422{
423 struct mg_drv_data *prv_data = host->dev->platform_data;
424 s32 err;
425 u8 init_status;
426
427 /* hdd rst low */
428 gpio_set_value(host->rst, 0);
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900429 err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700430 if (err)
431 return err;
432
433 /* hdd rst high */
434 gpio_set_value(host->rst, 1);
435 err = mg_wait(host, MG_STAT_READY, MG_TMAX_HDRST_TO_RDY);
436 if (err)
437 return err;
438
439 /* soft reset on */
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900440 outb(ATA_SRST | (prv_data->use_polling ? ATA_NIEN : 0),
unsik Kim3fbed4c2009-04-02 12:50:58 -0700441 (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900442 err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700443 if (err)
444 return err;
445
446 /* soft reset off */
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900447 outb(prv_data->use_polling ? ATA_NIEN : 0,
unsik Kim3fbed4c2009-04-02 12:50:58 -0700448 (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
449 err = mg_wait(host, MG_STAT_READY, MG_TMAX_SWRST_TO_RDY);
450 if (err)
451 return err;
452
453 init_status = inb((unsigned long)host->dev_base + MG_REG_STATUS) & 0xf;
454
455 if (init_status == 0xf)
456 return MG_ERR_INIT_STAT;
457
458 return err;
459}
460
461static void mg_bad_rw_intr(struct mg_host *host)
462{
Tejun Heo5b36ad62009-05-08 11:54:01 +0900463 if (host->req)
464 if (++host->req->errors >= MG_MAX_ERRORS ||
465 host->error == MG_ERR_TIMEOUT)
466 mg_end_request_cur(host, -EIO);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700467}
468
469static unsigned int mg_out(struct mg_host *host,
470 unsigned int sect_num,
471 unsigned int sect_cnt,
472 unsigned int cmd,
473 void (*intr_addr)(struct mg_host *))
474{
475 struct mg_drv_data *prv_data = host->dev->platform_data;
476
477 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
478 return host->error;
479
480 if (!prv_data->use_polling) {
481 host->mg_do_intr = intr_addr;
482 mod_timer(&host->timer, jiffies + 3 * HZ);
483 }
484 if (MG_RES_SEC)
485 sect_num += MG_RES_SEC;
486 outb((u8)sect_cnt, (unsigned long)host->dev_base + MG_REG_SECT_CNT);
487 outb((u8)sect_num, (unsigned long)host->dev_base + MG_REG_SECT_NUM);
488 outb((u8)(sect_num >> 8), (unsigned long)host->dev_base +
489 MG_REG_CYL_LOW);
490 outb((u8)(sect_num >> 16), (unsigned long)host->dev_base +
491 MG_REG_CYL_HIGH);
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900492 outb((u8)((sect_num >> 24) | ATA_LBA | ATA_DEVICE_OBS),
unsik Kim3fbed4c2009-04-02 12:50:58 -0700493 (unsigned long)host->dev_base + MG_REG_DRV_HEAD);
494 outb(cmd, (unsigned long)host->dev_base + MG_REG_COMMAND);
495 return MG_ERR_NONE;
496}
497
498static void mg_read(struct request *req)
499{
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900500 u32 j;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700501 struct mg_host *host = req->rq_disk->private_data;
502
Tejun Heo83096eb2009-05-07 22:24:39 +0900503 if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req),
504 MG_CMD_RD, NULL) != MG_ERR_NONE)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700505 mg_bad_rw_intr(host);
506
507 MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
Tejun Heo83096eb2009-05-07 22:24:39 +0900508 blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700509
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900510 do {
511 u16 *buff = (u16 *)req->buffer;
512
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900513 if (mg_wait(host, ATA_DRQ,
514 MG_TMAX_WAIT_RD_DRQ) != MG_ERR_NONE) {
unsik Kim3fbed4c2009-04-02 12:50:58 -0700515 mg_bad_rw_intr(host);
516 return;
517 }
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900518 for (j = 0; j < MG_SECTOR_SIZE >> 1; j++)
519 *buff++ = inw((unsigned long)host->dev_base +
520 MG_BUFF_OFFSET + (j << 1));
unsik Kim3fbed4c2009-04-02 12:50:58 -0700521
522 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base +
523 MG_REG_COMMAND);
Tejun Heo5b36ad62009-05-08 11:54:01 +0900524 } while (mg_end_request(host, 0, MG_SECTOR_SIZE));
unsik Kim3fbed4c2009-04-02 12:50:58 -0700525}
526
527static void mg_write(struct request *req)
528{
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900529 u32 j;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700530 struct mg_host *host = req->rq_disk->private_data;
531
Tejun Heo83096eb2009-05-07 22:24:39 +0900532 if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req),
533 MG_CMD_WR, NULL) != MG_ERR_NONE) {
unsik Kim3fbed4c2009-04-02 12:50:58 -0700534 mg_bad_rw_intr(host);
535 return;
536 }
537
unsik Kim3fbed4c2009-04-02 12:50:58 -0700538 MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
Tejun Heo83096eb2009-05-07 22:24:39 +0900539 blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900540
541 do {
542 u16 *buff = (u16 *)req->buffer;
543
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900544 if (mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
unsik Kim3fbed4c2009-04-02 12:50:58 -0700545 mg_bad_rw_intr(host);
546 return;
547 }
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900548 for (j = 0; j < MG_SECTOR_SIZE >> 1; j++)
549 outw(*buff++, (unsigned long)host->dev_base +
550 MG_BUFF_OFFSET + (j << 1));
unsik Kim3fbed4c2009-04-02 12:50:58 -0700551
552 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
553 MG_REG_COMMAND);
Tejun Heo5b36ad62009-05-08 11:54:01 +0900554 } while (mg_end_request(host, 0, MG_SECTOR_SIZE));
unsik Kim3fbed4c2009-04-02 12:50:58 -0700555}
556
557static void mg_read_intr(struct mg_host *host)
558{
Tejun Heo5b36ad62009-05-08 11:54:01 +0900559 struct request *req = host->req;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700560 u32 i;
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900561 u16 *buff;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700562
563 /* check status */
564 do {
565 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900566 if (i & ATA_BUSY)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700567 break;
568 if (!MG_READY_OK(i))
569 break;
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900570 if (i & ATA_DRQ)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700571 goto ok_to_read;
572 } while (0);
573 mg_dump_status("mg_read_intr", i, host);
574 mg_bad_rw_intr(host);
575 mg_request(host->breq);
576 return;
577
578ok_to_read:
579 /* get current segment of request */
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900580 buff = (u16 *)req->buffer;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700581
582 /* read 1 sector */
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900583 for (i = 0; i < MG_SECTOR_SIZE >> 1; i++)
584 *buff++ = inw((unsigned long)host->dev_base + MG_BUFF_OFFSET +
585 (i << 1));
unsik Kim3fbed4c2009-04-02 12:50:58 -0700586
unsik Kim3fbed4c2009-04-02 12:50:58 -0700587 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
Tejun Heo83096eb2009-05-07 22:24:39 +0900588 blk_rq_pos(req), blk_rq_sectors(req) - 1, req->buffer);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700589
unsik Kim3fbed4c2009-04-02 12:50:58 -0700590 /* send read confirm */
591 outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
592
Tejun Heo5b36ad62009-05-08 11:54:01 +0900593 if (mg_end_request(host, 0, MG_SECTOR_SIZE)) {
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900594 /* set handler if read remains */
595 host->mg_do_intr = mg_read_intr;
596 mod_timer(&host->timer, jiffies + 3 * HZ);
597 } else /* goto next request */
unsik Kim3fbed4c2009-04-02 12:50:58 -0700598 mg_request(host->breq);
599}
600
601static void mg_write_intr(struct mg_host *host)
602{
Tejun Heo5b36ad62009-05-08 11:54:01 +0900603 struct request *req = host->req;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700604 u32 i, j;
605 u16 *buff;
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900606 bool rem;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700607
unsik Kim3fbed4c2009-04-02 12:50:58 -0700608 /* check status */
609 do {
610 i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900611 if (i & ATA_BUSY)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700612 break;
613 if (!MG_READY_OK(i))
614 break;
Tejun Heo83096eb2009-05-07 22:24:39 +0900615 if ((blk_rq_sectors(req) <= 1) || (i & ATA_DRQ))
unsik Kim3fbed4c2009-04-02 12:50:58 -0700616 goto ok_to_write;
617 } while (0);
618 mg_dump_status("mg_write_intr", i, host);
619 mg_bad_rw_intr(host);
620 mg_request(host->breq);
621 return;
622
623ok_to_write:
Tejun Heo5b36ad62009-05-08 11:54:01 +0900624 if ((rem = mg_end_request(host, 0, MG_SECTOR_SIZE))) {
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900625 /* write 1 sector and set handler if remains */
unsik Kim3fbed4c2009-04-02 12:50:58 -0700626 buff = (u16 *)req->buffer;
627 for (j = 0; j < MG_STORAGE_BUFFER_SIZE >> 1; j++) {
628 outw(*buff, (unsigned long)host->dev_base +
629 MG_BUFF_OFFSET + (j << 1));
630 buff++;
631 }
632 MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
Tejun Heo83096eb2009-05-07 22:24:39 +0900633 blk_rq_pos(req), blk_rq_sectors(req), req->buffer);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700634 host->mg_do_intr = mg_write_intr;
635 mod_timer(&host->timer, jiffies + 3 * HZ);
636 }
637
638 /* send write confirm */
639 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
640
Tejun Heoa03bb5a2009-04-28 13:06:15 +0900641 if (!rem)
unsik Kim3fbed4c2009-04-02 12:50:58 -0700642 mg_request(host->breq);
643}
644
645void mg_times_out(unsigned long data)
646{
647 struct mg_host *host = (struct mg_host *)data;
648 char *name;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700649
Tejun Heoac2ff942009-04-28 12:38:32 +0900650 spin_lock_irq(&host->lock);
651
Tejun Heo5b36ad62009-05-08 11:54:01 +0900652 if (!host->req)
Tejun Heoac2ff942009-04-28 12:38:32 +0900653 goto out_unlock;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700654
655 host->mg_do_intr = NULL;
656
Tejun Heo5b36ad62009-05-08 11:54:01 +0900657 name = host->req->rq_disk->disk_name;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700658 printk(KERN_DEBUG "%s: timeout\n", name);
659
660 host->error = MG_ERR_TIMEOUT;
661 mg_bad_rw_intr(host);
662
Tejun Heoac2ff942009-04-28 12:38:32 +0900663out_unlock:
Tejun Heo5b36ad62009-05-08 11:54:01 +0900664 mg_request(host->breq);
Tejun Heoac2ff942009-04-28 12:38:32 +0900665 spin_unlock_irq(&host->lock);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700666}
667
668static void mg_request_poll(struct request_queue *q)
669{
Tejun Heo5b36ad62009-05-08 11:54:01 +0900670 struct mg_host *host = q->queuedata;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700671
Tejun Heo5b36ad62009-05-08 11:54:01 +0900672 while (1) {
673 if (!host->req) {
674 host->req = elv_next_request(q);
675 if (host->req)
676 blkdev_dequeue_request(host->req);
677 else
678 break;
679 }
Tejun Heo9a8d23d2009-05-08 11:54:00 +0900680
Tejun Heo5b36ad62009-05-08 11:54:01 +0900681 if (unlikely(!blk_fs_request(host->req))) {
682 mg_end_request_cur(host, -EIO);
Tejun Heo9a8d23d2009-05-08 11:54:00 +0900683 continue;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700684 }
Tejun Heo9a8d23d2009-05-08 11:54:00 +0900685
Tejun Heo5b36ad62009-05-08 11:54:01 +0900686 if (rq_data_dir(host->req) == READ)
687 mg_read(host->req);
Tejun Heo9a8d23d2009-05-08 11:54:00 +0900688 else
Tejun Heo5b36ad62009-05-08 11:54:01 +0900689 mg_write(host->req);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700690 }
691}
692
693static unsigned int mg_issue_req(struct request *req,
694 struct mg_host *host,
695 unsigned int sect_num,
696 unsigned int sect_cnt)
697{
698 u16 *buff;
699 u32 i;
700
701 switch (rq_data_dir(req)) {
702 case READ:
703 if (mg_out(host, sect_num, sect_cnt, MG_CMD_RD, &mg_read_intr)
704 != MG_ERR_NONE) {
705 mg_bad_rw_intr(host);
706 return host->error;
707 }
708 break;
709 case WRITE:
710 /* TODO : handler */
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900711 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700712 if (mg_out(host, sect_num, sect_cnt, MG_CMD_WR, &mg_write_intr)
713 != MG_ERR_NONE) {
714 mg_bad_rw_intr(host);
715 return host->error;
716 }
717 del_timer(&host->timer);
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900718 mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ);
719 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700720 if (host->error) {
721 mg_bad_rw_intr(host);
722 return host->error;
723 }
724 buff = (u16 *)req->buffer;
725 for (i = 0; i < MG_SECTOR_SIZE >> 1; i++) {
726 outw(*buff, (unsigned long)host->dev_base +
727 MG_BUFF_OFFSET + (i << 1));
728 buff++;
729 }
730 mod_timer(&host->timer, jiffies + 3 * HZ);
731 outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
732 MG_REG_COMMAND);
733 break;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700734 }
735 return MG_ERR_NONE;
736}
737
738/* This function also called from IRQ context */
739static void mg_request(struct request_queue *q)
740{
Tejun Heo5b36ad62009-05-08 11:54:01 +0900741 struct mg_host *host = q->queuedata;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700742 struct request *req;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700743 u32 sect_num, sect_cnt;
744
745 while (1) {
Tejun Heo5b36ad62009-05-08 11:54:01 +0900746 if (!host->req) {
747 host->req = elv_next_request(q);
748 if (host->req)
749 blkdev_dequeue_request(host->req);
750 else
751 break;
752 }
753 req = host->req;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700754
755 /* check unwanted request call */
756 if (host->mg_do_intr)
757 return;
758
759 del_timer(&host->timer);
760
Tejun Heo83096eb2009-05-07 22:24:39 +0900761 sect_num = blk_rq_pos(req);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700762 /* deal whole segments */
Tejun Heo83096eb2009-05-07 22:24:39 +0900763 sect_cnt = blk_rq_sectors(req);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700764
765 /* sanity check */
766 if (sect_num >= get_capacity(req->rq_disk) ||
767 ((sect_num + sect_cnt) >
768 get_capacity(req->rq_disk))) {
769 printk(KERN_WARNING
770 "%s: bad access: sector=%d, count=%d\n",
771 req->rq_disk->disk_name,
772 sect_num, sect_cnt);
Tejun Heo5b36ad62009-05-08 11:54:01 +0900773 mg_end_request_cur(host, -EIO);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700774 continue;
775 }
776
Tejun Heo9a8d23d2009-05-08 11:54:00 +0900777 if (unlikely(!blk_fs_request(req))) {
Tejun Heo5b36ad62009-05-08 11:54:01 +0900778 mg_end_request_cur(host, -EIO);
Tejun Heo9a8d23d2009-05-08 11:54:00 +0900779 continue;
780 }
unsik Kim3fbed4c2009-04-02 12:50:58 -0700781
782 if (!mg_issue_req(req, host, sect_num, sect_cnt))
783 return;
784 }
785}
786
787static int mg_getgeo(struct block_device *bdev, struct hd_geometry *geo)
788{
789 struct mg_host *host = bdev->bd_disk->private_data;
790
791 geo->cylinders = (unsigned short)host->cyls;
792 geo->heads = (unsigned char)host->heads;
793 geo->sectors = (unsigned char)host->sectors;
794 return 0;
795}
796
797static struct block_device_operations mg_disk_ops = {
798 .getgeo = mg_getgeo
799};
800
801static int mg_suspend(struct platform_device *plat_dev, pm_message_t state)
802{
803 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
804 struct mg_host *host = prv_data->host;
805
806 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
807 return -EIO;
808
809 if (!prv_data->use_polling)
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900810 outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700811
812 outb(MG_CMD_SLEEP, (unsigned long)host->dev_base + MG_REG_COMMAND);
813 /* wait until mflash deep sleep */
814 msleep(1);
815
816 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) {
817 if (!prv_data->use_polling)
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900818 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700819 return -EIO;
820 }
821
822 return 0;
823}
824
825static int mg_resume(struct platform_device *plat_dev)
826{
827 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
828 struct mg_host *host = prv_data->host;
829
830 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
831 return -EIO;
832
833 outb(MG_CMD_WAKEUP, (unsigned long)host->dev_base + MG_REG_COMMAND);
834 /* wait until mflash wakeup */
835 msleep(1);
836
837 if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
838 return -EIO;
839
840 if (!prv_data->use_polling)
Bartlomiej Zolnierkiewiczf68adec2009-04-28 13:06:17 +0900841 outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
unsik Kim3fbed4c2009-04-02 12:50:58 -0700842
843 return 0;
844}
845
846static int mg_probe(struct platform_device *plat_dev)
847{
848 struct mg_host *host;
849 struct resource *rsc;
850 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
851 int err = 0;
852
853 if (!prv_data) {
854 printk(KERN_ERR "%s:%d fail (no driver_data)\n",
855 __func__, __LINE__);
856 err = -EINVAL;
857 goto probe_err;
858 }
859
860 /* alloc mg_host */
861 host = kzalloc(sizeof(struct mg_host), GFP_KERNEL);
862 if (!host) {
863 printk(KERN_ERR "%s:%d fail (no memory for mg_host)\n",
864 __func__, __LINE__);
865 err = -ENOMEM;
866 goto probe_err;
867 }
868 host->major = MG_DISK_MAJ;
869
870 /* link each other */
871 prv_data->host = host;
872 host->dev = &plat_dev->dev;
873
874 /* io remap */
875 rsc = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
876 if (!rsc) {
877 printk(KERN_ERR "%s:%d platform_get_resource fail\n",
878 __func__, __LINE__);
879 err = -EINVAL;
880 goto probe_err_2;
881 }
882 host->dev_base = ioremap(rsc->start , rsc->end + 1);
883 if (!host->dev_base) {
884 printk(KERN_ERR "%s:%d ioremap fail\n",
885 __func__, __LINE__);
886 err = -EIO;
887 goto probe_err_2;
888 }
889 MG_DBG("dev_base = 0x%x\n", (u32)host->dev_base);
890
891 /* get reset pin */
892 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
893 MG_RST_PIN);
894 if (!rsc) {
895 printk(KERN_ERR "%s:%d get reset pin fail\n",
896 __func__, __LINE__);
897 err = -EIO;
898 goto probe_err_3;
899 }
900 host->rst = rsc->start;
901
902 /* init rst pin */
903 err = gpio_request(host->rst, MG_RST_PIN);
904 if (err)
905 goto probe_err_3;
906 gpio_direction_output(host->rst, 1);
907
908 /* reset out pin */
909 if (!(prv_data->dev_attr & MG_DEV_MASK))
910 goto probe_err_3a;
911
912 if (prv_data->dev_attr != MG_BOOT_DEV) {
913 rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
914 MG_RSTOUT_PIN);
915 if (!rsc) {
916 printk(KERN_ERR "%s:%d get reset-out pin fail\n",
917 __func__, __LINE__);
918 err = -EIO;
919 goto probe_err_3a;
920 }
921 host->rstout = rsc->start;
922 err = gpio_request(host->rstout, MG_RSTOUT_PIN);
923 if (err)
924 goto probe_err_3a;
925 gpio_direction_input(host->rstout);
926 }
927
928 /* disk reset */
929 if (prv_data->dev_attr == MG_STORAGE_DEV) {
930 /* If POR seq. not yet finised, wait */
931 err = mg_wait_rstout(host->rstout, MG_TMAX_RSTOUT);
932 if (err)
933 goto probe_err_3b;
934 err = mg_disk_init(host);
935 if (err) {
936 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
937 __func__, __LINE__, err);
938 err = -EIO;
939 goto probe_err_3b;
940 }
941 }
942
943 /* get irq resource */
944 if (!prv_data->use_polling) {
945 host->irq = platform_get_irq(plat_dev, 0);
946 if (host->irq == -ENXIO) {
947 err = host->irq;
948 goto probe_err_3b;
949 }
950 err = request_irq(host->irq, mg_irq,
951 IRQF_DISABLED | IRQF_TRIGGER_RISING,
952 MG_DEV_NAME, host);
953 if (err) {
954 printk(KERN_ERR "%s:%d fail (request_irq err=%d)\n",
955 __func__, __LINE__, err);
956 goto probe_err_3b;
957 }
958
959 }
960
961 /* get disk id */
962 err = mg_get_disk_id(host);
963 if (err) {
964 printk(KERN_ERR "%s:%d fail (err code : %d)\n",
965 __func__, __LINE__, err);
966 err = -EIO;
967 goto probe_err_4;
968 }
969
970 err = register_blkdev(host->major, MG_DISK_NAME);
971 if (err < 0) {
972 printk(KERN_ERR "%s:%d register_blkdev fail (err code : %d)\n",
973 __func__, __LINE__, err);
974 goto probe_err_4;
975 }
976 if (!host->major)
977 host->major = err;
978
979 spin_lock_init(&host->lock);
980
981 if (prv_data->use_polling)
982 host->breq = blk_init_queue(mg_request_poll, &host->lock);
983 else
984 host->breq = blk_init_queue(mg_request, &host->lock);
985
986 if (!host->breq) {
987 err = -ENOMEM;
988 printk(KERN_ERR "%s:%d (blk_init_queue) fail\n",
989 __func__, __LINE__);
990 goto probe_err_5;
991 }
Tejun Heo5b36ad62009-05-08 11:54:01 +0900992 host->breq->queuedata = host;
unsik Kim3fbed4c2009-04-02 12:50:58 -0700993
994 /* mflash is random device, thanx for the noop */
995 elevator_exit(host->breq->elevator);
996 err = elevator_init(host->breq, "noop");
997 if (err) {
998 printk(KERN_ERR "%s:%d (elevator_init) fail\n",
999 __func__, __LINE__);
1000 goto probe_err_6;
1001 }
1002 blk_queue_max_sectors(host->breq, MG_MAX_SECTS);
1003 blk_queue_hardsect_size(host->breq, MG_SECTOR_SIZE);
1004
1005 init_timer(&host->timer);
1006 host->timer.function = mg_times_out;
1007 host->timer.data = (unsigned long)host;
1008
1009 host->gd = alloc_disk(MG_DISK_MAX_PART);
1010 if (!host->gd) {
1011 printk(KERN_ERR "%s:%d (alloc_disk) fail\n",
1012 __func__, __LINE__);
1013 err = -ENOMEM;
1014 goto probe_err_7;
1015 }
1016 host->gd->major = host->major;
1017 host->gd->first_minor = 0;
1018 host->gd->fops = &mg_disk_ops;
1019 host->gd->queue = host->breq;
1020 host->gd->private_data = host;
1021 sprintf(host->gd->disk_name, MG_DISK_NAME"a");
1022
1023 set_capacity(host->gd, host->n_sectors);
1024
1025 add_disk(host->gd);
1026
1027 return err;
1028
1029probe_err_7:
1030 del_timer_sync(&host->timer);
1031probe_err_6:
1032 blk_cleanup_queue(host->breq);
1033probe_err_5:
1034 unregister_blkdev(MG_DISK_MAJ, MG_DISK_NAME);
1035probe_err_4:
1036 if (!prv_data->use_polling)
1037 free_irq(host->irq, host);
1038probe_err_3b:
1039 gpio_free(host->rstout);
1040probe_err_3a:
1041 gpio_free(host->rst);
1042probe_err_3:
1043 iounmap(host->dev_base);
1044probe_err_2:
1045 kfree(host);
1046probe_err:
1047 return err;
1048}
1049
1050static int mg_remove(struct platform_device *plat_dev)
1051{
1052 struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
1053 struct mg_host *host = prv_data->host;
1054 int err = 0;
1055
1056 /* delete timer */
1057 del_timer_sync(&host->timer);
1058
1059 /* remove disk */
1060 if (host->gd) {
1061 del_gendisk(host->gd);
1062 put_disk(host->gd);
1063 }
1064 /* remove queue */
1065 if (host->breq)
1066 blk_cleanup_queue(host->breq);
1067
1068 /* unregister blk device */
1069 unregister_blkdev(host->major, MG_DISK_NAME);
1070
1071 /* free irq */
1072 if (!prv_data->use_polling)
1073 free_irq(host->irq, host);
1074
1075 /* free reset-out pin */
1076 if (prv_data->dev_attr != MG_BOOT_DEV)
1077 gpio_free(host->rstout);
1078
1079 /* free rst pin */
1080 if (host->rst)
1081 gpio_free(host->rst);
1082
1083 /* unmap io */
1084 if (host->dev_base)
1085 iounmap(host->dev_base);
1086
1087 /* free mg_host */
1088 kfree(host);
1089
1090 return err;
1091}
1092
1093static struct platform_driver mg_disk_driver = {
1094 .probe = mg_probe,
1095 .remove = mg_remove,
1096 .suspend = mg_suspend,
1097 .resume = mg_resume,
1098 .driver = {
1099 .name = MG_DEV_NAME,
1100 .owner = THIS_MODULE,
1101 }
1102};
1103
1104/****************************************************************************
1105 *
1106 * Module stuff
1107 *
1108 ****************************************************************************/
1109
1110static int __init mg_init(void)
1111{
1112 printk(KERN_INFO "mGine mflash driver, (c) 2008 mGine Co.\n");
1113 return platform_driver_register(&mg_disk_driver);
1114}
1115
1116static void __exit mg_exit(void)
1117{
1118 printk(KERN_INFO "mflash driver : bye bye\n");
1119 platform_driver_unregister(&mg_disk_driver);
1120}
1121
1122module_init(mg_init);
1123module_exit(mg_exit);
1124
1125MODULE_LICENSE("GPL");
1126MODULE_AUTHOR("unsik Kim <donari75@gmail.com>");
1127MODULE_DESCRIPTION("mGine m[g]flash device driver");