Blame view

drivers/block/mg_disk.c 26 KB
3fbed4c61   unsik Kim   mflash: initial s...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  /*
   *  drivers/block/mg_disk.c
   *
   *  Support for the mGine m[g]flash IO mode.
   *  Based on legacy hd.c
   *
   * (c) 2008 mGine Co.,LTD
   * (c) 2008 unsik Kim <donari75@gmail.com>
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License version 2 as
   *  published by the Free Software Foundation.
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/fs.h>
  #include <linux/blkdev.h>
  #include <linux/hdreg.h>
8a11a789c   Bartlomiej Zolnierkiewicz   mg_disk: fix depe...
20
  #include <linux/ata.h>
3fbed4c61   unsik Kim   mflash: initial s...
21
22
23
24
  #include <linux/interrupt.h>
  #include <linux/delay.h>
  #include <linux/platform_device.h>
  #include <linux/gpio.h>
5ced504b1   unsik Kim   mg_disk: seperate...
25
  #include <linux/mg_disk.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
26
  #include <linux/slab.h>
3fbed4c61   unsik Kim   mflash: initial s...
27
28
  
  #define MG_RES_SEC (CONFIG_MG_DISK_RES << 1)
eec946208   Tejun Heo   mg_disk: fold mg_...
29
30
  /* name for block device */
  #define MG_DISK_NAME "mgd"
eec946208   Tejun Heo   mg_disk: fold mg_...
31
32
33
34
35
36
37
38
  
  #define MG_DISK_MAJ 0
  #define MG_DISK_MAX_PART 16
  #define MG_SECTOR_SIZE 512
  #define MG_MAX_SECTS 256
  
  /* Register offsets */
  #define MG_BUFF_OFFSET			0x8000
eec946208   Tejun Heo   mg_disk: fold mg_...
39
40
41
42
43
44
45
46
47
48
49
50
  #define MG_REG_OFFSET			0xC000
  #define MG_REG_FEATURE			(MG_REG_OFFSET + 2)	/* write case */
  #define MG_REG_ERROR			(MG_REG_OFFSET + 2)	/* read case */
  #define MG_REG_SECT_CNT			(MG_REG_OFFSET + 4)
  #define MG_REG_SECT_NUM			(MG_REG_OFFSET + 6)
  #define MG_REG_CYL_LOW			(MG_REG_OFFSET + 8)
  #define MG_REG_CYL_HIGH			(MG_REG_OFFSET + 0xA)
  #define MG_REG_DRV_HEAD			(MG_REG_OFFSET + 0xC)
  #define MG_REG_COMMAND			(MG_REG_OFFSET + 0xE)	/* write case */
  #define MG_REG_STATUS			(MG_REG_OFFSET + 0xE)	/* read  case */
  #define MG_REG_DRV_CTRL			(MG_REG_OFFSET + 0x10)
  #define MG_REG_BURST_CTRL		(MG_REG_OFFSET + 0x12)
eec946208   Tejun Heo   mg_disk: fold mg_...
51
  /* handy status */
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
52
53
54
  #define MG_STAT_READY	(ATA_DRDY | ATA_DSC)
  #define MG_READY_OK(s)	(((s) & (MG_STAT_READY | (ATA_BUSY | ATA_DF | \
  				 ATA_ERR))) == MG_STAT_READY)
eec946208   Tejun Heo   mg_disk: fold mg_...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  
  /* error code for others */
  #define MG_ERR_NONE		0
  #define MG_ERR_TIMEOUT		0x100
  #define MG_ERR_INIT_STAT	0x101
  #define MG_ERR_TRANSLATION	0x102
  #define MG_ERR_CTRL_RST		0x103
  #define MG_ERR_INV_STAT		0x104
  #define MG_ERR_RSTOUT		0x105
  
  #define MG_MAX_ERRORS	6	/* Max read/write errors */
  
  /* command */
  #define MG_CMD_RD 0x20
  #define MG_CMD_WR 0x30
  #define MG_CMD_SLEEP 0x99
  #define MG_CMD_WAKEUP 0xC3
  #define MG_CMD_ID 0xEC
  #define MG_CMD_WR_CONF 0x3C
  #define MG_CMD_RD_CONF 0x40
  
  /* operation mode */
  #define MG_OP_CASCADE (1 << 0)
  #define MG_OP_CASCADE_SYNC_RD (1 << 1)
  #define MG_OP_CASCADE_SYNC_WR (1 << 2)
  #define MG_OP_INTERLEAVE (1 << 3)
  
  /* synchronous */
  #define MG_BURST_LAT_4 (3 << 4)
  #define MG_BURST_LAT_5 (4 << 4)
  #define MG_BURST_LAT_6 (5 << 4)
  #define MG_BURST_LAT_7 (6 << 4)
  #define MG_BURST_LAT_8 (7 << 4)
  #define MG_BURST_LEN_4 (1 << 1)
  #define MG_BURST_LEN_8 (2 << 1)
  #define MG_BURST_LEN_16 (3 << 1)
  #define MG_BURST_LEN_32 (4 << 1)
  #define MG_BURST_LEN_CONT (0 << 1)
  
  /* timeout value (unit: ms) */
  #define MG_TMAX_CONF_TO_CMD	1
  #define MG_TMAX_WAIT_RD_DRQ	10
  #define MG_TMAX_WAIT_WR_DRQ	500
  #define MG_TMAX_RST_TO_BUSY	10
  #define MG_TMAX_HDRST_TO_RDY	500
  #define MG_TMAX_SWRST_TO_RDY	500
  #define MG_TMAX_RSTOUT		3000
eec946208   Tejun Heo   mg_disk: fold mg_...
102
  #define MG_DEV_MASK (MG_BOOT_DEV | MG_STORAGE_DEV | MG_STORAGE_DEV_SKIP_RST)
eec946208   Tejun Heo   mg_disk: fold mg_...
103
104
105
106
107
  /* main structure for mflash driver */
  struct mg_host {
  	struct device *dev;
  
  	struct request_queue *breq;
5b36ad600   Tejun Heo   mg_disk: dequeue ...
108
  	struct request *req;
eec946208   Tejun Heo   mg_disk: fold mg_...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  	spinlock_t lock;
  	struct gendisk *gd;
  
  	struct timer_list timer;
  	void (*mg_do_intr) (struct mg_host *);
  
  	u16 id[ATA_ID_WORDS];
  
  	u16 cyls;
  	u16 heads;
  	u16 sectors;
  	u32 n_sectors;
  	u32 nres_sectors;
  
  	void __iomem *dev_base;
  	unsigned int irq;
  	unsigned int rst;
  	unsigned int rstout;
  
  	u32 major;
  	u32 error;
  };
  
  /*
   * Debugging macro and defines
   */
  #undef DO_MG_DEBUG
  #ifdef DO_MG_DEBUG
  #  define MG_DBG(fmt, args...) \
  	printk(KERN_DEBUG "%s:%d "fmt, __func__, __LINE__, ##args)
  #else /* CONFIG_MG_DEBUG */
  #  define MG_DBG(fmt, args...) do { } while (0)
  #endif /* CONFIG_MG_DEBUG */
3fbed4c61   unsik Kim   mflash: initial s...
142
  static void mg_request(struct request_queue *);
5b36ad600   Tejun Heo   mg_disk: dequeue ...
143
144
145
146
147
148
149
150
151
152
153
154
155
  static bool mg_end_request(struct mg_host *host, int err, unsigned int nr_bytes)
  {
  	if (__blk_end_request(host->req, err, nr_bytes))
  		return true;
  
  	host->req = NULL;
  	return false;
  }
  
  static bool mg_end_request_cur(struct mg_host *host, int err)
  {
  	return mg_end_request(host, err, blk_rq_cur_bytes(host->req));
  }
3fbed4c61   unsik Kim   mflash: initial s...
156
157
158
159
  static void mg_dump_status(const char *msg, unsigned int stat,
  		struct mg_host *host)
  {
  	char *name = MG_DISK_NAME;
3fbed4c61   unsik Kim   mflash: initial s...
160

5b36ad600   Tejun Heo   mg_disk: dequeue ...
161
162
  	if (host->req)
  		name = host->req->rq_disk->disk_name;
3fbed4c61   unsik Kim   mflash: initial s...
163
164
  
  	printk(KERN_ERR "%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
165
  	if (stat & ATA_BUSY)
3fbed4c61   unsik Kim   mflash: initial s...
166
  		printk("Busy ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
167
  	if (stat & ATA_DRDY)
3fbed4c61   unsik Kim   mflash: initial s...
168
  		printk("DriveReady ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
169
  	if (stat & ATA_DF)
3fbed4c61   unsik Kim   mflash: initial s...
170
  		printk("WriteFault ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
171
  	if (stat & ATA_DSC)
3fbed4c61   unsik Kim   mflash: initial s...
172
  		printk("SeekComplete ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
173
  	if (stat & ATA_DRQ)
3fbed4c61   unsik Kim   mflash: initial s...
174
  		printk("DataRequest ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
175
  	if (stat & ATA_CORR)
3fbed4c61   unsik Kim   mflash: initial s...
176
  		printk("CorrectedError ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
177
  	if (stat & ATA_ERR)
3fbed4c61   unsik Kim   mflash: initial s...
178
179
180
  		printk("Error ");
  	printk("}
  ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
181
  	if ((stat & ATA_ERR) == 0) {
3fbed4c61   unsik Kim   mflash: initial s...
182
183
184
185
186
  		host->error = 0;
  	} else {
  		host->error = inb((unsigned long)host->dev_base + MG_REG_ERROR);
  		printk(KERN_ERR "%s: %s: error=0x%02x { ", name, msg,
  				host->error & 0xff);
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
187
  		if (host->error & ATA_BBK)
3fbed4c61   unsik Kim   mflash: initial s...
188
  			printk("BadSector ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
189
  		if (host->error & ATA_UNC)
3fbed4c61   unsik Kim   mflash: initial s...
190
  			printk("UncorrectableError ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
191
  		if (host->error & ATA_IDNF)
3fbed4c61   unsik Kim   mflash: initial s...
192
  			printk("SectorIdNotFound ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
193
  		if (host->error & ATA_ABORTED)
3fbed4c61   unsik Kim   mflash: initial s...
194
  			printk("DriveStatusError ");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
195
  		if (host->error & ATA_AMNF)
3fbed4c61   unsik Kim   mflash: initial s...
196
197
  			printk("AddrMarkNotFound ");
  		printk("}");
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
198
  		if (host->error & (ATA_BBK | ATA_UNC | ATA_IDNF | ATA_AMNF)) {
5b36ad600   Tejun Heo   mg_disk: dequeue ...
199
200
201
  			if (host->req)
  				printk(", sector=%u",
  				       (unsigned int)blk_rq_pos(host->req));
3fbed4c61   unsik Kim   mflash: initial s...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  		}
  		printk("
  ");
  	}
  }
  
  static unsigned int mg_wait(struct mg_host *host, u32 expect, u32 msec)
  {
  	u8 status;
  	unsigned long expire, cur_jiffies;
  	struct mg_drv_data *prv_data = host->dev->platform_data;
  
  	host->error = MG_ERR_NONE;
  	expire = jiffies + msecs_to_jiffies(msec);
eb32baec1   unsik Kim   mg_disk: fix read...
216
217
218
219
220
221
222
223
224
  	/* These 2 times dummy status read prevents reading invalid
  	 * status. A very little time (3 times of mflash operating clk)
  	 * is required for busy bit is set. Use dummy read instead of
  	 * busy wait, because mflash's PLL is machine dependent.
  	 */
  	if (prv_data->use_polling) {
  		status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  		status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  	}
3fbed4c61   unsik Kim   mflash: initial s...
225
226
227
228
  	status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  
  	do {
  		cur_jiffies = jiffies;
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
229
230
  		if (status & ATA_BUSY) {
  			if (expect == ATA_BUSY)
3fbed4c61   unsik Kim   mflash: initial s...
231
232
233
  				break;
  		} else {
  			/* Check the error condition! */
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
234
  			if (status & ATA_ERR) {
3fbed4c61   unsik Kim   mflash: initial s...
235
236
237
238
239
240
241
  				mg_dump_status("mg_wait", status, host);
  				break;
  			}
  
  			if (expect == MG_STAT_READY)
  				if (MG_READY_OK(status))
  					break;
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
242
243
  			if (expect == ATA_DRQ)
  				if (status & ATA_DRQ)
3fbed4c61   unsik Kim   mflash: initial s...
244
245
246
247
248
249
  					break;
  		}
  		if (!msec) {
  			mg_dump_status("not ready", status, host);
  			return MG_ERR_INV_STAT;
  		}
3fbed4c61   unsik Kim   mflash: initial s...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
  
  		status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  	} while (time_before(cur_jiffies, expire));
  
  	if (time_after_eq(cur_jiffies, expire) && msec)
  		host->error = MG_ERR_TIMEOUT;
  
  	return host->error;
  }
  
  static unsigned int mg_wait_rstout(u32 rstout, u32 msec)
  {
  	unsigned long expire;
  
  	expire = jiffies + msecs_to_jiffies(msec);
  	while (time_before(jiffies, expire)) {
  		if (gpio_get_value(rstout) == 1)
  			return MG_ERR_NONE;
  		msleep(10);
  	}
  
  	return MG_ERR_RSTOUT;
  }
  
  static void mg_unexpected_intr(struct mg_host *host)
  {
  	u32 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  
  	mg_dump_status("mg_unexpected_intr", status, host);
  }
  
  static irqreturn_t mg_irq(int irq, void *dev_id)
  {
  	struct mg_host *host = dev_id;
  	void (*handler)(struct mg_host *) = host->mg_do_intr;
ac2ff946a   Tejun Heo   mg_disk: fix locking
285
286
287
  	spin_lock(&host->lock);
  
  	host->mg_do_intr = NULL;
3fbed4c61   unsik Kim   mflash: initial s...
288
289
290
291
  	del_timer(&host->timer);
  	if (!handler)
  		handler = mg_unexpected_intr;
  	handler(host);
ac2ff946a   Tejun Heo   mg_disk: fix locking
292
293
  
  	spin_unlock(&host->lock);
3fbed4c61   unsik Kim   mflash: initial s...
294
295
  	return IRQ_HANDLED;
  }
8a11a789c   Bartlomiej Zolnierkiewicz   mg_disk: fix depe...
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  /* local copy of ata_id_string() */
  static void mg_id_string(const u16 *id, unsigned char *s,
  			 unsigned int ofs, unsigned int len)
  {
  	unsigned int c;
  
  	BUG_ON(len & 1);
  
  	while (len > 0) {
  		c = id[ofs] >> 8;
  		*s = c;
  		s++;
  
  		c = id[ofs] & 0xff;
  		*s = c;
  		s++;
  
  		ofs++;
  		len -= 2;
  	}
  }
  
  /* local copy of ata_id_c_string() */
  static void mg_id_c_string(const u16 *id, unsigned char *s,
  			   unsigned int ofs, unsigned int len)
  {
  	unsigned char *p;
  
  	mg_id_string(id, s, ofs, len - 1);
  
  	p = s + strnlen(s, len - 1);
  	while (p > s && p[-1] == ' ')
  		p--;
  	*p = '\0';
  }
3fbed4c61   unsik Kim   mflash: initial s...
331
332
333
334
335
336
337
338
339
340
341
  static int mg_get_disk_id(struct mg_host *host)
  {
  	u32 i;
  	s32 err;
  	const u16 *id = host->id;
  	struct mg_drv_data *prv_data = host->dev->platform_data;
  	char fwrev[ATA_ID_FW_REV_LEN + 1];
  	char model[ATA_ID_PROD_LEN + 1];
  	char serial[ATA_ID_SERNO_LEN + 1];
  
  	if (!prv_data->use_polling)
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
342
  		outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
3fbed4c61   unsik Kim   mflash: initial s...
343
344
  
  	outb(MG_CMD_ID, (unsigned long)host->dev_base + MG_REG_COMMAND);
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
345
  	err = mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_RD_DRQ);
3fbed4c61   unsik Kim   mflash: initial s...
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
  	if (err)
  		return err;
  
  	for (i = 0; i < (MG_SECTOR_SIZE >> 1); i++)
  		host->id[i] = le16_to_cpu(inw((unsigned long)host->dev_base +
  					MG_BUFF_OFFSET + i * 2));
  
  	outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
  	err = mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD);
  	if (err)
  		return err;
  
  	if ((id[ATA_ID_FIELD_VALID] & 1) == 0)
  		return MG_ERR_TRANSLATION;
  
  	host->n_sectors = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
  	host->cyls = id[ATA_ID_CYLS];
  	host->heads = id[ATA_ID_HEADS];
  	host->sectors = id[ATA_ID_SECTORS];
  
  	if (MG_RES_SEC && host->heads && host->sectors) {
  		/* modify cyls, n_sectors */
  		host->cyls = (host->n_sectors - MG_RES_SEC) /
  			host->heads / host->sectors;
  		host->nres_sectors = host->n_sectors - host->cyls *
  			host->heads * host->sectors;
  		host->n_sectors -= host->nres_sectors;
  	}
8a11a789c   Bartlomiej Zolnierkiewicz   mg_disk: fix depe...
374
375
376
  	mg_id_c_string(id, fwrev, ATA_ID_FW_REV, sizeof(fwrev));
  	mg_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
  	mg_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
3fbed4c61   unsik Kim   mflash: initial s...
377
378
379
380
381
382
383
384
385
386
387
  	printk(KERN_INFO "mg_disk: model: %s
  ", model);
  	printk(KERN_INFO "mg_disk: firm: %.8s
  ", fwrev);
  	printk(KERN_INFO "mg_disk: serial: %s
  ", serial);
  	printk(KERN_INFO "mg_disk: %d + reserved %d sectors
  ",
  			host->n_sectors, host->nres_sectors);
  
  	if (!prv_data->use_polling)
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
388
  		outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
3fbed4c61   unsik Kim   mflash: initial s...
389
390
391
392
393
394
395
396
397
398
399
400
401
  
  	return err;
  }
  
  
  static int mg_disk_init(struct mg_host *host)
  {
  	struct mg_drv_data *prv_data = host->dev->platform_data;
  	s32 err;
  	u8 init_status;
  
  	/* hdd rst low */
  	gpio_set_value(host->rst, 0);
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
402
  	err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
3fbed4c61   unsik Kim   mflash: initial s...
403
404
405
406
407
408
409
410
411
412
  	if (err)
  		return err;
  
  	/* hdd rst high */
  	gpio_set_value(host->rst, 1);
  	err = mg_wait(host, MG_STAT_READY, MG_TMAX_HDRST_TO_RDY);
  	if (err)
  		return err;
  
  	/* soft reset on */
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
413
  	outb(ATA_SRST | (prv_data->use_polling ? ATA_NIEN : 0),
3fbed4c61   unsik Kim   mflash: initial s...
414
  			(unsigned long)host->dev_base + MG_REG_DRV_CTRL);
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
415
  	err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
3fbed4c61   unsik Kim   mflash: initial s...
416
417
418
419
  	if (err)
  		return err;
  
  	/* soft reset off */
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
420
  	outb(prv_data->use_polling ? ATA_NIEN : 0,
3fbed4c61   unsik Kim   mflash: initial s...
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
  			(unsigned long)host->dev_base + MG_REG_DRV_CTRL);
  	err = mg_wait(host, MG_STAT_READY, MG_TMAX_SWRST_TO_RDY);
  	if (err)
  		return err;
  
  	init_status = inb((unsigned long)host->dev_base + MG_REG_STATUS) & 0xf;
  
  	if (init_status == 0xf)
  		return MG_ERR_INIT_STAT;
  
  	return err;
  }
  
  static void mg_bad_rw_intr(struct mg_host *host)
  {
5b36ad600   Tejun Heo   mg_disk: dequeue ...
436
437
438
439
  	if (host->req)
  		if (++host->req->errors >= MG_MAX_ERRORS ||
  		    host->error == MG_ERR_TIMEOUT)
  			mg_end_request_cur(host, -EIO);
3fbed4c61   unsik Kim   mflash: initial s...
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  }
  
  static unsigned int mg_out(struct mg_host *host,
  		unsigned int sect_num,
  		unsigned int sect_cnt,
  		unsigned int cmd,
  		void (*intr_addr)(struct mg_host *))
  {
  	struct mg_drv_data *prv_data = host->dev->platform_data;
  
  	if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
  		return host->error;
  
  	if (!prv_data->use_polling) {
  		host->mg_do_intr = intr_addr;
  		mod_timer(&host->timer, jiffies + 3 * HZ);
  	}
  	if (MG_RES_SEC)
  		sect_num += MG_RES_SEC;
  	outb((u8)sect_cnt, (unsigned long)host->dev_base + MG_REG_SECT_CNT);
  	outb((u8)sect_num, (unsigned long)host->dev_base + MG_REG_SECT_NUM);
  	outb((u8)(sect_num >> 8), (unsigned long)host->dev_base +
  			MG_REG_CYL_LOW);
  	outb((u8)(sect_num >> 16), (unsigned long)host->dev_base +
  			MG_REG_CYL_HIGH);
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
465
  	outb((u8)((sect_num >> 24) | ATA_LBA | ATA_DEVICE_OBS),
3fbed4c61   unsik Kim   mflash: initial s...
466
467
468
469
  			(unsigned long)host->dev_base + MG_REG_DRV_HEAD);
  	outb(cmd, (unsigned long)host->dev_base + MG_REG_COMMAND);
  	return MG_ERR_NONE;
  }
394c6cc63   Bartlomiej Zolnierkiewicz   mg_disk: fix issu...
470
471
472
473
474
475
476
477
478
  static void mg_read_one(struct mg_host *host, struct request *req)
  {
  	u16 *buff = (u16 *)req->buffer;
  	u32 i;
  
  	for (i = 0; i < MG_SECTOR_SIZE >> 1; i++)
  		*buff++ = inw((unsigned long)host->dev_base + MG_BUFF_OFFSET +
  			      (i << 1));
  }
3fbed4c61   unsik Kim   mflash: initial s...
479
480
  static void mg_read(struct request *req)
  {
3fbed4c61   unsik Kim   mflash: initial s...
481
  	struct mg_host *host = req->rq_disk->private_data;
83096ebf1   Tejun Heo   block: convert to...
482
483
  	if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req),
  		   MG_CMD_RD, NULL) != MG_ERR_NONE)
3fbed4c61   unsik Kim   mflash: initial s...
484
485
486
487
  		mg_bad_rw_intr(host);
  
  	MG_DBG("requested %d sects (from %ld), buffer=0x%p
  ",
83096ebf1   Tejun Heo   block: convert to...
488
  	       blk_rq_sectors(req), blk_rq_pos(req), req->buffer);
a03bb5a32   Tejun Heo   mg_disk: clean up...
489
490
  
  	do {
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
491
492
  		if (mg_wait(host, ATA_DRQ,
  			    MG_TMAX_WAIT_RD_DRQ) != MG_ERR_NONE) {
3fbed4c61   unsik Kim   mflash: initial s...
493
494
495
  			mg_bad_rw_intr(host);
  			return;
  		}
394c6cc63   Bartlomiej Zolnierkiewicz   mg_disk: fix issu...
496
497
  
  		mg_read_one(host, req);
3fbed4c61   unsik Kim   mflash: initial s...
498
499
500
  
  		outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base +
  				MG_REG_COMMAND);
5b36ad600   Tejun Heo   mg_disk: dequeue ...
501
  	} while (mg_end_request(host, 0, MG_SECTOR_SIZE));
3fbed4c61   unsik Kim   mflash: initial s...
502
  }
394c6cc63   Bartlomiej Zolnierkiewicz   mg_disk: fix issu...
503
504
505
506
507
508
509
510
511
  static void mg_write_one(struct mg_host *host, struct request *req)
  {
  	u16 *buff = (u16 *)req->buffer;
  	u32 i;
  
  	for (i = 0; i < MG_SECTOR_SIZE >> 1; i++)
  		outw(*buff++, (unsigned long)host->dev_base + MG_BUFF_OFFSET +
  		     (i << 1));
  }
3fbed4c61   unsik Kim   mflash: initial s...
512
513
  static void mg_write(struct request *req)
  {
3fbed4c61   unsik Kim   mflash: initial s...
514
  	struct mg_host *host = req->rq_disk->private_data;
a85a00a69   unsik Kim   mg_disk: Add miss...
515
  	unsigned int rem = blk_rq_sectors(req);
3fbed4c61   unsik Kim   mflash: initial s...
516

a85a00a69   unsik Kim   mg_disk: Add miss...
517
  	if (mg_out(host, blk_rq_pos(req), rem,
83096ebf1   Tejun Heo   block: convert to...
518
  		   MG_CMD_WR, NULL) != MG_ERR_NONE) {
3fbed4c61   unsik Kim   mflash: initial s...
519
520
521
  		mg_bad_rw_intr(host);
  		return;
  	}
3fbed4c61   unsik Kim   mflash: initial s...
522
523
  	MG_DBG("requested %d sects (from %ld), buffer=0x%p
  ",
a85a00a69   unsik Kim   mg_disk: Add miss...
524
  	       rem, blk_rq_pos(req), req->buffer);
a03bb5a32   Tejun Heo   mg_disk: clean up...
525

394c6cc63   Bartlomiej Zolnierkiewicz   mg_disk: fix issu...
526
527
528
529
530
  	if (mg_wait(host, ATA_DRQ,
  		    MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
  		mg_bad_rw_intr(host);
  		return;
  	}
a85a00a69   unsik Kim   mg_disk: Add miss...
531
532
  	do {
  		mg_write_one(host, req);
394c6cc63   Bartlomiej Zolnierkiewicz   mg_disk: fix issu...
533

a85a00a69   unsik Kim   mg_disk: Add miss...
534
535
  		outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
  				MG_REG_COMMAND);
a03bb5a32   Tejun Heo   mg_disk: clean up...
536

a85a00a69   unsik Kim   mg_disk: Add miss...
537
538
539
540
541
542
543
  		rem--;
  		if (rem > 1 && mg_wait(host, ATA_DRQ,
  					MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
  			mg_bad_rw_intr(host);
  			return;
  		} else if (mg_wait(host, MG_STAT_READY,
  					MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
3fbed4c61   unsik Kim   mflash: initial s...
544
545
546
  			mg_bad_rw_intr(host);
  			return;
  		}
a85a00a69   unsik Kim   mg_disk: Add miss...
547
  	} while (mg_end_request(host, 0, MG_SECTOR_SIZE));
3fbed4c61   unsik Kim   mflash: initial s...
548
549
550
551
  }
  
  static void mg_read_intr(struct mg_host *host)
  {
5b36ad600   Tejun Heo   mg_disk: dequeue ...
552
  	struct request *req = host->req;
3fbed4c61   unsik Kim   mflash: initial s...
553
  	u32 i;
3fbed4c61   unsik Kim   mflash: initial s...
554
555
556
557
  
  	/* check status */
  	do {
  		i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
558
  		if (i & ATA_BUSY)
3fbed4c61   unsik Kim   mflash: initial s...
559
560
561
  			break;
  		if (!MG_READY_OK(i))
  			break;
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
562
  		if (i & ATA_DRQ)
3fbed4c61   unsik Kim   mflash: initial s...
563
564
565
566
567
568
569
570
  			goto ok_to_read;
  	} while (0);
  	mg_dump_status("mg_read_intr", i, host);
  	mg_bad_rw_intr(host);
  	mg_request(host->breq);
  	return;
  
  ok_to_read:
394c6cc63   Bartlomiej Zolnierkiewicz   mg_disk: fix issu...
571
  	mg_read_one(host, req);
3fbed4c61   unsik Kim   mflash: initial s...
572

3fbed4c61   unsik Kim   mflash: initial s...
573
574
  	MG_DBG("sector %ld, remaining=%ld, buffer=0x%p
  ",
83096ebf1   Tejun Heo   block: convert to...
575
  	       blk_rq_pos(req), blk_rq_sectors(req) - 1, req->buffer);
3fbed4c61   unsik Kim   mflash: initial s...
576

3fbed4c61   unsik Kim   mflash: initial s...
577
578
  	/* send read confirm */
  	outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
5b36ad600   Tejun Heo   mg_disk: dequeue ...
579
  	if (mg_end_request(host, 0, MG_SECTOR_SIZE)) {
a03bb5a32   Tejun Heo   mg_disk: clean up...
580
581
582
583
  		/* set handler if read remains */
  		host->mg_do_intr = mg_read_intr;
  		mod_timer(&host->timer, jiffies + 3 * HZ);
  	} else /* goto next request */
3fbed4c61   unsik Kim   mflash: initial s...
584
585
586
587
588
  		mg_request(host->breq);
  }
  
  static void mg_write_intr(struct mg_host *host)
  {
5b36ad600   Tejun Heo   mg_disk: dequeue ...
589
  	struct request *req = host->req;
394c6cc63   Bartlomiej Zolnierkiewicz   mg_disk: fix issu...
590
  	u32 i;
a03bb5a32   Tejun Heo   mg_disk: clean up...
591
  	bool rem;
3fbed4c61   unsik Kim   mflash: initial s...
592

3fbed4c61   unsik Kim   mflash: initial s...
593
594
595
  	/* check status */
  	do {
  		i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
596
  		if (i & ATA_BUSY)
3fbed4c61   unsik Kim   mflash: initial s...
597
598
599
  			break;
  		if (!MG_READY_OK(i))
  			break;
83096ebf1   Tejun Heo   block: convert to...
600
  		if ((blk_rq_sectors(req) <= 1) || (i & ATA_DRQ))
3fbed4c61   unsik Kim   mflash: initial s...
601
602
603
604
605
606
607
608
  			goto ok_to_write;
  	} while (0);
  	mg_dump_status("mg_write_intr", i, host);
  	mg_bad_rw_intr(host);
  	mg_request(host->breq);
  	return;
  
  ok_to_write:
5b36ad600   Tejun Heo   mg_disk: dequeue ...
609
  	if ((rem = mg_end_request(host, 0, MG_SECTOR_SIZE))) {
a03bb5a32   Tejun Heo   mg_disk: clean up...
610
  		/* write 1 sector and set handler if remains */
394c6cc63   Bartlomiej Zolnierkiewicz   mg_disk: fix issu...
611
  		mg_write_one(host, req);
3fbed4c61   unsik Kim   mflash: initial s...
612
613
  		MG_DBG("sector %ld, remaining=%ld, buffer=0x%p
  ",
83096ebf1   Tejun Heo   block: convert to...
614
  		       blk_rq_pos(req), blk_rq_sectors(req), req->buffer);
3fbed4c61   unsik Kim   mflash: initial s...
615
616
617
618
619
620
  		host->mg_do_intr = mg_write_intr;
  		mod_timer(&host->timer, jiffies + 3 * HZ);
  	}
  
  	/* send write confirm */
  	outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
a03bb5a32   Tejun Heo   mg_disk: clean up...
621
  	if (!rem)
3fbed4c61   unsik Kim   mflash: initial s...
622
623
624
625
626
627
628
  		mg_request(host->breq);
  }
  
  void mg_times_out(unsigned long data)
  {
  	struct mg_host *host = (struct mg_host *)data;
  	char *name;
3fbed4c61   unsik Kim   mflash: initial s...
629

ac2ff946a   Tejun Heo   mg_disk: fix locking
630
  	spin_lock_irq(&host->lock);
5b36ad600   Tejun Heo   mg_disk: dequeue ...
631
  	if (!host->req)
ac2ff946a   Tejun Heo   mg_disk: fix locking
632
  		goto out_unlock;
3fbed4c61   unsik Kim   mflash: initial s...
633
634
  
  	host->mg_do_intr = NULL;
5b36ad600   Tejun Heo   mg_disk: dequeue ...
635
  	name = host->req->rq_disk->disk_name;
3fbed4c61   unsik Kim   mflash: initial s...
636
637
638
639
640
  	printk(KERN_DEBUG "%s: timeout
  ", name);
  
  	host->error = MG_ERR_TIMEOUT;
  	mg_bad_rw_intr(host);
ac2ff946a   Tejun Heo   mg_disk: fix locking
641
  out_unlock:
5b36ad600   Tejun Heo   mg_disk: dequeue ...
642
  	mg_request(host->breq);
ac2ff946a   Tejun Heo   mg_disk: fix locking
643
  	spin_unlock_irq(&host->lock);
3fbed4c61   unsik Kim   mflash: initial s...
644
645
646
647
  }
  
  static void mg_request_poll(struct request_queue *q)
  {
5b36ad600   Tejun Heo   mg_disk: dequeue ...
648
  	struct mg_host *host = q->queuedata;
3fbed4c61   unsik Kim   mflash: initial s...
649

5b36ad600   Tejun Heo   mg_disk: dequeue ...
650
651
  	while (1) {
  		if (!host->req) {
9934c8c04   Tejun Heo   block: implement ...
652
653
  			host->req = blk_fetch_request(q);
  			if (!host->req)
5b36ad600   Tejun Heo   mg_disk: dequeue ...
654
655
  				break;
  		}
9a8d23d88   Tejun Heo   mg_disk: fix queu...
656

33659ebba   Christoph Hellwig   block: remove wra...
657
  		if (unlikely(host->req->cmd_type != REQ_TYPE_FS)) {
5b36ad600   Tejun Heo   mg_disk: dequeue ...
658
  			mg_end_request_cur(host, -EIO);
9a8d23d88   Tejun Heo   mg_disk: fix queu...
659
  			continue;
3fbed4c61   unsik Kim   mflash: initial s...
660
  		}
9a8d23d88   Tejun Heo   mg_disk: fix queu...
661

5b36ad600   Tejun Heo   mg_disk: dequeue ...
662
663
  		if (rq_data_dir(host->req) == READ)
  			mg_read(host->req);
9a8d23d88   Tejun Heo   mg_disk: fix queu...
664
  		else
5b36ad600   Tejun Heo   mg_disk: dequeue ...
665
  			mg_write(host->req);
3fbed4c61   unsik Kim   mflash: initial s...
666
667
668
669
670
671
672
673
  	}
  }
  
  static unsigned int mg_issue_req(struct request *req,
  		struct mg_host *host,
  		unsigned int sect_num,
  		unsigned int sect_cnt)
  {
3fbed4c61   unsik Kim   mflash: initial s...
674
675
676
677
678
679
680
681
682
683
  	switch (rq_data_dir(req)) {
  	case READ:
  		if (mg_out(host, sect_num, sect_cnt, MG_CMD_RD, &mg_read_intr)
  				!= MG_ERR_NONE) {
  			mg_bad_rw_intr(host);
  			return host->error;
  		}
  		break;
  	case WRITE:
  		/* TODO : handler */
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
684
  		outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
3fbed4c61   unsik Kim   mflash: initial s...
685
686
687
688
689
690
  		if (mg_out(host, sect_num, sect_cnt, MG_CMD_WR, &mg_write_intr)
  				!= MG_ERR_NONE) {
  			mg_bad_rw_intr(host);
  			return host->error;
  		}
  		del_timer(&host->timer);
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
691
692
  		mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ);
  		outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
3fbed4c61   unsik Kim   mflash: initial s...
693
694
695
696
  		if (host->error) {
  			mg_bad_rw_intr(host);
  			return host->error;
  		}
394c6cc63   Bartlomiej Zolnierkiewicz   mg_disk: fix issu...
697
  		mg_write_one(host, req);
3fbed4c61   unsik Kim   mflash: initial s...
698
699
700
701
  		mod_timer(&host->timer, jiffies + 3 * HZ);
  		outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
  				MG_REG_COMMAND);
  		break;
3fbed4c61   unsik Kim   mflash: initial s...
702
703
704
705
706
707
708
  	}
  	return MG_ERR_NONE;
  }
  
  /* This function also called from IRQ context */
  static void mg_request(struct request_queue *q)
  {
5b36ad600   Tejun Heo   mg_disk: dequeue ...
709
  	struct mg_host *host = q->queuedata;
3fbed4c61   unsik Kim   mflash: initial s...
710
  	struct request *req;
3fbed4c61   unsik Kim   mflash: initial s...
711
712
713
  	u32 sect_num, sect_cnt;
  
  	while (1) {
5b36ad600   Tejun Heo   mg_disk: dequeue ...
714
  		if (!host->req) {
9934c8c04   Tejun Heo   block: implement ...
715
716
  			host->req = blk_fetch_request(q);
  			if (!host->req)
5b36ad600   Tejun Heo   mg_disk: dequeue ...
717
718
719
  				break;
  		}
  		req = host->req;
3fbed4c61   unsik Kim   mflash: initial s...
720
721
722
723
724
725
  
  		/* check unwanted request call */
  		if (host->mg_do_intr)
  			return;
  
  		del_timer(&host->timer);
83096ebf1   Tejun Heo   block: convert to...
726
  		sect_num = blk_rq_pos(req);
3fbed4c61   unsik Kim   mflash: initial s...
727
  		/* deal whole segments */
83096ebf1   Tejun Heo   block: convert to...
728
  		sect_cnt = blk_rq_sectors(req);
3fbed4c61   unsik Kim   mflash: initial s...
729
730
731
732
733
734
735
736
737
738
  
  		/* sanity check */
  		if (sect_num >= get_capacity(req->rq_disk) ||
  				((sect_num + sect_cnt) >
  				 get_capacity(req->rq_disk))) {
  			printk(KERN_WARNING
  					"%s: bad access: sector=%d, count=%d
  ",
  					req->rq_disk->disk_name,
  					sect_num, sect_cnt);
5b36ad600   Tejun Heo   mg_disk: dequeue ...
739
  			mg_end_request_cur(host, -EIO);
3fbed4c61   unsik Kim   mflash: initial s...
740
741
  			continue;
  		}
33659ebba   Christoph Hellwig   block: remove wra...
742
  		if (unlikely(req->cmd_type != REQ_TYPE_FS)) {
5b36ad600   Tejun Heo   mg_disk: dequeue ...
743
  			mg_end_request_cur(host, -EIO);
9a8d23d88   Tejun Heo   mg_disk: fix queu...
744
745
  			continue;
  		}
3fbed4c61   unsik Kim   mflash: initial s...
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
  
  		if (!mg_issue_req(req, host, sect_num, sect_cnt))
  			return;
  	}
  }
  
  static int mg_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  {
  	struct mg_host *host = bdev->bd_disk->private_data;
  
  	geo->cylinders = (unsigned short)host->cyls;
  	geo->heads = (unsigned char)host->heads;
  	geo->sectors = (unsigned char)host->sectors;
  	return 0;
  }
83d5cde47   Alexey Dobriyan   const: make block...
761
  static const struct block_device_operations mg_disk_ops = {
3fbed4c61   unsik Kim   mflash: initial s...
762
763
764
765
766
767
768
769
770
771
772
773
  	.getgeo = mg_getgeo
  };
  
  static int mg_suspend(struct platform_device *plat_dev, pm_message_t state)
  {
  	struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
  	struct mg_host *host = prv_data->host;
  
  	if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
  		return -EIO;
  
  	if (!prv_data->use_polling)
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
774
  		outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
3fbed4c61   unsik Kim   mflash: initial s...
775
776
777
778
779
780
781
  
  	outb(MG_CMD_SLEEP, (unsigned long)host->dev_base + MG_REG_COMMAND);
  	/* wait until mflash deep sleep */
  	msleep(1);
  
  	if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) {
  		if (!prv_data->use_polling)
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
782
  			outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
3fbed4c61   unsik Kim   mflash: initial s...
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
  		return -EIO;
  	}
  
  	return 0;
  }
  
  static int mg_resume(struct platform_device *plat_dev)
  {
  	struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
  	struct mg_host *host = prv_data->host;
  
  	if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
  		return -EIO;
  
  	outb(MG_CMD_WAKEUP, (unsigned long)host->dev_base + MG_REG_COMMAND);
  	/* wait until mflash wakeup */
  	msleep(1);
  
  	if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
  		return -EIO;
  
  	if (!prv_data->use_polling)
f68adec3c   Bartlomiej Zolnierkiewicz   mg_disk: use defi...
805
  		outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
3fbed4c61   unsik Kim   mflash: initial s...
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
  
  	return 0;
  }
  
  static int mg_probe(struct platform_device *plat_dev)
  {
  	struct mg_host *host;
  	struct resource *rsc;
  	struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
  	int err = 0;
  
  	if (!prv_data) {
  		printk(KERN_ERR	"%s:%d fail (no driver_data)
  ",
  				__func__, __LINE__);
  		err = -EINVAL;
  		goto probe_err;
  	}
  
  	/* alloc mg_host */
  	host = kzalloc(sizeof(struct mg_host), GFP_KERNEL);
  	if (!host) {
  		printk(KERN_ERR "%s:%d fail (no memory for mg_host)
  ",
  				__func__, __LINE__);
  		err = -ENOMEM;
  		goto probe_err;
  	}
  	host->major = MG_DISK_MAJ;
  
  	/* link each other */
  	prv_data->host = host;
  	host->dev = &plat_dev->dev;
  
  	/* io remap */
  	rsc = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
  	if (!rsc) {
  		printk(KERN_ERR "%s:%d platform_get_resource fail
  ",
  				__func__, __LINE__);
  		err = -EINVAL;
  		goto probe_err_2;
  	}
e019ef0c4   H Hartley Sweeten   drivers/block/mg_...
849
  	host->dev_base = ioremap(rsc->start, resource_size(rsc));
3fbed4c61   unsik Kim   mflash: initial s...
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
  	if (!host->dev_base) {
  		printk(KERN_ERR "%s:%d ioremap fail
  ",
  				__func__, __LINE__);
  		err = -EIO;
  		goto probe_err_2;
  	}
  	MG_DBG("dev_base = 0x%x
  ", (u32)host->dev_base);
  
  	/* get reset pin */
  	rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
  			MG_RST_PIN);
  	if (!rsc) {
  		printk(KERN_ERR "%s:%d get reset pin fail
  ",
  				__func__, __LINE__);
  		err = -EIO;
  		goto probe_err_3;
  	}
  	host->rst = rsc->start;
  
  	/* init rst pin */
  	err = gpio_request(host->rst, MG_RST_PIN);
  	if (err)
  		goto probe_err_3;
  	gpio_direction_output(host->rst, 1);
  
  	/* reset out pin */
  	if (!(prv_data->dev_attr & MG_DEV_MASK))
  		goto probe_err_3a;
  
  	if (prv_data->dev_attr != MG_BOOT_DEV) {
  		rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
  				MG_RSTOUT_PIN);
  		if (!rsc) {
  			printk(KERN_ERR "%s:%d get reset-out pin fail
  ",
  					__func__, __LINE__);
  			err = -EIO;
  			goto probe_err_3a;
  		}
  		host->rstout = rsc->start;
  		err = gpio_request(host->rstout, MG_RSTOUT_PIN);
  		if (err)
  			goto probe_err_3a;
  		gpio_direction_input(host->rstout);
  	}
  
  	/* disk reset */
  	if (prv_data->dev_attr == MG_STORAGE_DEV) {
  		/* If POR seq. not yet finised, wait */
  		err = mg_wait_rstout(host->rstout, MG_TMAX_RSTOUT);
  		if (err)
  			goto probe_err_3b;
  		err = mg_disk_init(host);
  		if (err) {
  			printk(KERN_ERR "%s:%d fail (err code : %d)
  ",
  					__func__, __LINE__, err);
  			err = -EIO;
  			goto probe_err_3b;
  		}
  	}
  
  	/* get irq resource */
  	if (!prv_data->use_polling) {
  		host->irq = platform_get_irq(plat_dev, 0);
  		if (host->irq == -ENXIO) {
  			err = host->irq;
  			goto probe_err_3b;
  		}
  		err = request_irq(host->irq, mg_irq,
  				IRQF_DISABLED | IRQF_TRIGGER_RISING,
  				MG_DEV_NAME, host);
  		if (err) {
  			printk(KERN_ERR "%s:%d fail (request_irq err=%d)
  ",
  					__func__, __LINE__, err);
  			goto probe_err_3b;
  		}
  
  	}
  
  	/* get disk id */
  	err = mg_get_disk_id(host);
  	if (err) {
  		printk(KERN_ERR "%s:%d fail (err code : %d)
  ",
  				__func__, __LINE__, err);
  		err = -EIO;
  		goto probe_err_4;
  	}
  
  	err = register_blkdev(host->major, MG_DISK_NAME);
  	if (err < 0) {
  		printk(KERN_ERR "%s:%d register_blkdev fail (err code : %d)
  ",
  				__func__, __LINE__, err);
  		goto probe_err_4;
  	}
  	if (!host->major)
  		host->major = err;
  
  	spin_lock_init(&host->lock);
  
  	if (prv_data->use_polling)
  		host->breq = blk_init_queue(mg_request_poll, &host->lock);
  	else
  		host->breq = blk_init_queue(mg_request, &host->lock);
  
  	if (!host->breq) {
  		err = -ENOMEM;
  		printk(KERN_ERR "%s:%d (blk_init_queue) fail
  ",
  				__func__, __LINE__);
  		goto probe_err_5;
  	}
5b36ad600   Tejun Heo   mg_disk: dequeue ...
968
  	host->breq->queuedata = host;
3fbed4c61   unsik Kim   mflash: initial s...
969
970
  
  	/* mflash is random device, thanx for the noop */
52cc2eef3   Jens Axboe   block: switch s39...
971
  	err = elevator_change(host->breq, "noop");
3fbed4c61   unsik Kim   mflash: initial s...
972
973
974
975
976
977
  	if (err) {
  		printk(KERN_ERR "%s:%d (elevator_init) fail
  ",
  				__func__, __LINE__);
  		goto probe_err_6;
  	}
086fa5ff0   Martin K. Petersen   block: Rename blk...
978
  	blk_queue_max_hw_sectors(host->breq, MG_MAX_SECTS);
e1defc4ff   Martin K. Petersen   block: Do away wi...
979
  	blk_queue_logical_block_size(host->breq, MG_SECTOR_SIZE);
3fbed4c61   unsik Kim   mflash: initial s...
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
  
  	init_timer(&host->timer);
  	host->timer.function = mg_times_out;
  	host->timer.data = (unsigned long)host;
  
  	host->gd = alloc_disk(MG_DISK_MAX_PART);
  	if (!host->gd) {
  		printk(KERN_ERR "%s:%d (alloc_disk) fail
  ",
  				__func__, __LINE__);
  		err = -ENOMEM;
  		goto probe_err_7;
  	}
  	host->gd->major = host->major;
  	host->gd->first_minor = 0;
  	host->gd->fops = &mg_disk_ops;
  	host->gd->queue = host->breq;
  	host->gd->private_data = host;
  	sprintf(host->gd->disk_name, MG_DISK_NAME"a");
  
  	set_capacity(host->gd, host->n_sectors);
  
  	add_disk(host->gd);
  
  	return err;
  
  probe_err_7:
  	del_timer_sync(&host->timer);
  probe_err_6:
  	blk_cleanup_queue(host->breq);
  probe_err_5:
  	unregister_blkdev(MG_DISK_MAJ, MG_DISK_NAME);
  probe_err_4:
  	if (!prv_data->use_polling)
  		free_irq(host->irq, host);
  probe_err_3b:
  	gpio_free(host->rstout);
  probe_err_3a:
  	gpio_free(host->rst);
  probe_err_3:
  	iounmap(host->dev_base);
  probe_err_2:
  	kfree(host);
  probe_err:
  	return err;
  }
  
  static int mg_remove(struct platform_device *plat_dev)
  {
  	struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
  	struct mg_host *host = prv_data->host;
  	int err = 0;
  
  	/* delete timer */
  	del_timer_sync(&host->timer);
  
  	/* remove disk */
  	if (host->gd) {
  		del_gendisk(host->gd);
  		put_disk(host->gd);
  	}
  	/* remove queue */
  	if (host->breq)
  		blk_cleanup_queue(host->breq);
  
  	/* unregister blk device */
  	unregister_blkdev(host->major, MG_DISK_NAME);
  
  	/* free irq */
  	if (!prv_data->use_polling)
  		free_irq(host->irq, host);
  
  	/* free reset-out pin */
  	if (prv_data->dev_attr != MG_BOOT_DEV)
  		gpio_free(host->rstout);
  
  	/* free rst pin */
  	if (host->rst)
  		gpio_free(host->rst);
  
  	/* unmap io */
  	if (host->dev_base)
  		iounmap(host->dev_base);
  
  	/* free mg_host */
  	kfree(host);
  
  	return err;
  }
  
  static struct platform_driver mg_disk_driver = {
  	.probe = mg_probe,
  	.remove = mg_remove,
  	.suspend = mg_suspend,
  	.resume = mg_resume,
  	.driver = {
  		.name = MG_DEV_NAME,
  		.owner = THIS_MODULE,
  	}
  };
  
  /****************************************************************************
   *
   * Module stuff
   *
   ****************************************************************************/
  
  static int __init mg_init(void)
  {
  	printk(KERN_INFO "mGine mflash driver, (c) 2008 mGine Co.
  ");
  	return platform_driver_register(&mg_disk_driver);
  }
  
  static void __exit mg_exit(void)
  {
  	printk(KERN_INFO "mflash driver : bye bye
  ");
  	platform_driver_unregister(&mg_disk_driver);
  }
  
  module_init(mg_init);
  module_exit(mg_exit);
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("unsik Kim <donari75@gmail.com>");
  MODULE_DESCRIPTION("mGine m[g]flash device driver");