unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 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> |
Bartlomiej Zolnierkiewicz | 8a11a78 | 2009-04-28 13:06:16 +0900 | [diff] [blame] | 20 | #include <linux/ata.h> |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/gpio.h> |
unsik Kim | 5ced504 | 2009-06-16 08:40:20 +0200 | [diff] [blame] | 25 | #include <linux/mg_disk.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 27 | |
| 28 | #define MG_RES_SEC (CONFIG_MG_DISK_RES << 1) |
| 29 | |
Tejun Heo | eec9462 | 2009-04-28 13:06:14 +0900 | [diff] [blame] | 30 | /* name for block device */ |
| 31 | #define MG_DISK_NAME "mgd" |
Tejun Heo | eec9462 | 2009-04-28 13:06:14 +0900 | [diff] [blame] | 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 |
Tejun Heo | eec9462 | 2009-04-28 13:06:14 +0900 | [diff] [blame] | 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 | |
Tejun Heo | eec9462 | 2009-04-28 13:06:14 +0900 | [diff] [blame] | 53 | /* handy status */ |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 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) |
Tejun Heo | eec9462 | 2009-04-28 13:06:14 +0900 | [diff] [blame] | 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 | |
Tejun Heo | eec9462 | 2009-04-28 13:06:14 +0900 | [diff] [blame] | 105 | #define MG_DEV_MASK (MG_BOOT_DEV | MG_STORAGE_DEV | MG_STORAGE_DEV_SKIP_RST) |
| 106 | |
Tejun Heo | eec9462 | 2009-04-28 13:06:14 +0900 | [diff] [blame] | 107 | /* main structure for mflash driver */ |
| 108 | struct mg_host { |
| 109 | struct device *dev; |
| 110 | |
| 111 | struct request_queue *breq; |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 112 | struct request *req; |
Tejun Heo | eec9462 | 2009-04-28 13:06:14 +0900 | [diff] [blame] | 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 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 147 | static void mg_request(struct request_queue *); |
| 148 | |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 149 | static bool mg_end_request(struct mg_host *host, int err, unsigned int nr_bytes) |
| 150 | { |
| 151 | if (__blk_end_request(host->req, err, nr_bytes)) |
| 152 | return true; |
| 153 | |
| 154 | host->req = NULL; |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | static bool mg_end_request_cur(struct mg_host *host, int err) |
| 159 | { |
| 160 | return mg_end_request(host, err, blk_rq_cur_bytes(host->req)); |
| 161 | } |
| 162 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 163 | static void mg_dump_status(const char *msg, unsigned int stat, |
| 164 | struct mg_host *host) |
| 165 | { |
| 166 | char *name = MG_DISK_NAME; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 167 | |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 168 | if (host->req) |
| 169 | name = host->req->rq_disk->disk_name; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 170 | |
| 171 | printk(KERN_ERR "%s: %s: status=0x%02x { ", name, msg, stat & 0xff); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 172 | if (stat & ATA_BUSY) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 173 | printk("Busy "); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 174 | if (stat & ATA_DRDY) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 175 | printk("DriveReady "); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 176 | if (stat & ATA_DF) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 177 | printk("WriteFault "); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 178 | if (stat & ATA_DSC) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 179 | printk("SeekComplete "); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 180 | if (stat & ATA_DRQ) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 181 | printk("DataRequest "); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 182 | if (stat & ATA_CORR) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 183 | printk("CorrectedError "); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 184 | if (stat & ATA_ERR) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 185 | printk("Error "); |
| 186 | printk("}\n"); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 187 | if ((stat & ATA_ERR) == 0) { |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 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); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 193 | if (host->error & ATA_BBK) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 194 | printk("BadSector "); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 195 | if (host->error & ATA_UNC) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 196 | printk("UncorrectableError "); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 197 | if (host->error & ATA_IDNF) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 198 | printk("SectorIdNotFound "); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 199 | if (host->error & ATA_ABORTED) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 200 | printk("DriveStatusError "); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 201 | if (host->error & ATA_AMNF) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 202 | printk("AddrMarkNotFound "); |
| 203 | printk("}"); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 204 | if (host->error & (ATA_BBK | ATA_UNC | ATA_IDNF | ATA_AMNF)) { |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 205 | if (host->req) |
| 206 | printk(", sector=%u", |
| 207 | (unsigned int)blk_rq_pos(host->req)); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 208 | } |
| 209 | printk("\n"); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static unsigned int mg_wait(struct mg_host *host, u32 expect, u32 msec) |
| 214 | { |
| 215 | u8 status; |
| 216 | unsigned long expire, cur_jiffies; |
| 217 | struct mg_drv_data *prv_data = host->dev->platform_data; |
| 218 | |
| 219 | host->error = MG_ERR_NONE; |
| 220 | expire = jiffies + msecs_to_jiffies(msec); |
| 221 | |
unsik Kim | eb32bae | 2009-07-28 08:52:07 +0200 | [diff] [blame] | 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 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 232 | status = inb((unsigned long)host->dev_base + MG_REG_STATUS); |
| 233 | |
| 234 | do { |
| 235 | cur_jiffies = jiffies; |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 236 | if (status & ATA_BUSY) { |
| 237 | if (expect == ATA_BUSY) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 238 | break; |
| 239 | } else { |
| 240 | /* Check the error condition! */ |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 241 | if (status & ATA_ERR) { |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 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 | |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 250 | if (expect == ATA_DRQ) |
| 251 | if (status & ATA_DRQ) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 252 | break; |
| 253 | } |
| 254 | if (!msec) { |
| 255 | mg_dump_status("not ready", status, host); |
| 256 | return MG_ERR_INV_STAT; |
| 257 | } |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 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 | |
| 268 | static 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 | |
| 282 | static 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 | |
| 289 | static 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 | |
Tejun Heo | ac2ff94 | 2009-04-28 12:38:32 +0900 | [diff] [blame] | 294 | spin_lock(&host->lock); |
| 295 | |
| 296 | host->mg_do_intr = NULL; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 297 | del_timer(&host->timer); |
| 298 | if (!handler) |
| 299 | handler = mg_unexpected_intr; |
| 300 | handler(host); |
Tejun Heo | ac2ff94 | 2009-04-28 12:38:32 +0900 | [diff] [blame] | 301 | |
| 302 | spin_unlock(&host->lock); |
| 303 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 304 | return IRQ_HANDLED; |
| 305 | } |
| 306 | |
Bartlomiej Zolnierkiewicz | 8a11a78 | 2009-04-28 13:06:16 +0900 | [diff] [blame] | 307 | /* local copy of ata_id_string() */ |
| 308 | static 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() */ |
| 330 | static 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 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 343 | static 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) |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 354 | outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 355 | |
| 356 | outb(MG_CMD_ID, (unsigned long)host->dev_base + MG_REG_COMMAND); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 357 | err = mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_RD_DRQ); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 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 | |
Bartlomiej Zolnierkiewicz | 8a11a78 | 2009-04-28 13:06:16 +0900 | [diff] [blame] | 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)); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 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) |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 397 | outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 398 | |
| 399 | return err; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | static 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); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 411 | err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 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 */ |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 422 | outb(ATA_SRST | (prv_data->use_polling ? ATA_NIEN : 0), |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 423 | (unsigned long)host->dev_base + MG_REG_DRV_CTRL); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 424 | err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 425 | if (err) |
| 426 | return err; |
| 427 | |
| 428 | /* soft reset off */ |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 429 | outb(prv_data->use_polling ? ATA_NIEN : 0, |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 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 | |
| 443 | static void mg_bad_rw_intr(struct mg_host *host) |
| 444 | { |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 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); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static 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); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 474 | outb((u8)((sect_num >> 24) | ATA_LBA | ATA_DEVICE_OBS), |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 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 | |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 480 | static void mg_read_one(struct mg_host *host, struct request *req) |
| 481 | { |
Jens Axboe | b4f42e2 | 2014-04-10 09:46:28 -0600 | [diff] [blame] | 482 | u16 *buff = (u16 *)bio_data(req->bio); |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 483 | u32 i; |
| 484 | |
| 485 | for (i = 0; i < MG_SECTOR_SIZE >> 1; i++) |
| 486 | *buff++ = inw((unsigned long)host->dev_base + MG_BUFF_OFFSET + |
| 487 | (i << 1)); |
| 488 | } |
| 489 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 490 | static void mg_read(struct request *req) |
| 491 | { |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 492 | struct mg_host *host = req->rq_disk->private_data; |
| 493 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 494 | if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req), |
| 495 | MG_CMD_RD, NULL) != MG_ERR_NONE) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 496 | mg_bad_rw_intr(host); |
| 497 | |
| 498 | MG_DBG("requested %d sects (from %ld), buffer=0x%p\n", |
Jens Axboe | b4f42e2 | 2014-04-10 09:46:28 -0600 | [diff] [blame] | 499 | blk_rq_sectors(req), blk_rq_pos(req), bio_data(req->bio)); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 500 | |
Tejun Heo | a03bb5a | 2009-04-28 13:06:15 +0900 | [diff] [blame] | 501 | do { |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 502 | if (mg_wait(host, ATA_DRQ, |
| 503 | MG_TMAX_WAIT_RD_DRQ) != MG_ERR_NONE) { |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 504 | mg_bad_rw_intr(host); |
| 505 | return; |
| 506 | } |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 507 | |
| 508 | mg_read_one(host, req); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 509 | |
| 510 | outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + |
| 511 | MG_REG_COMMAND); |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 512 | } while (mg_end_request(host, 0, MG_SECTOR_SIZE)); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 515 | static void mg_write_one(struct mg_host *host, struct request *req) |
| 516 | { |
Jens Axboe | b4f42e2 | 2014-04-10 09:46:28 -0600 | [diff] [blame] | 517 | u16 *buff = (u16 *)bio_data(req->bio); |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 518 | u32 i; |
| 519 | |
| 520 | for (i = 0; i < MG_SECTOR_SIZE >> 1; i++) |
| 521 | outw(*buff++, (unsigned long)host->dev_base + MG_BUFF_OFFSET + |
| 522 | (i << 1)); |
| 523 | } |
| 524 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 525 | static void mg_write(struct request *req) |
| 526 | { |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 527 | struct mg_host *host = req->rq_disk->private_data; |
unsik Kim | a85a00a | 2009-07-28 08:57:33 +0200 | [diff] [blame] | 528 | unsigned int rem = blk_rq_sectors(req); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 529 | |
unsik Kim | a85a00a | 2009-07-28 08:57:33 +0200 | [diff] [blame] | 530 | if (mg_out(host, blk_rq_pos(req), rem, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 531 | MG_CMD_WR, NULL) != MG_ERR_NONE) { |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 532 | mg_bad_rw_intr(host); |
| 533 | return; |
| 534 | } |
| 535 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 536 | MG_DBG("requested %d sects (from %ld), buffer=0x%p\n", |
Jens Axboe | b4f42e2 | 2014-04-10 09:46:28 -0600 | [diff] [blame] | 537 | rem, blk_rq_pos(req), bio_data(req->bio)); |
Tejun Heo | a03bb5a | 2009-04-28 13:06:15 +0900 | [diff] [blame] | 538 | |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 539 | if (mg_wait(host, ATA_DRQ, |
| 540 | MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) { |
| 541 | mg_bad_rw_intr(host); |
| 542 | return; |
| 543 | } |
Tejun Heo | a03bb5a | 2009-04-28 13:06:15 +0900 | [diff] [blame] | 544 | |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 545 | do { |
unsik Kim | a85a00a | 2009-07-28 08:57:33 +0200 | [diff] [blame] | 546 | mg_write_one(host, req); |
| 547 | |
| 548 | outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + |
| 549 | MG_REG_COMMAND); |
| 550 | |
| 551 | rem--; |
| 552 | if (rem > 1 && mg_wait(host, ATA_DRQ, |
| 553 | MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) { |
| 554 | mg_bad_rw_intr(host); |
| 555 | return; |
| 556 | } else if (mg_wait(host, MG_STAT_READY, |
| 557 | MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) { |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 558 | mg_bad_rw_intr(host); |
| 559 | return; |
| 560 | } |
unsik Kim | a85a00a | 2009-07-28 08:57:33 +0200 | [diff] [blame] | 561 | } while (mg_end_request(host, 0, MG_SECTOR_SIZE)); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static void mg_read_intr(struct mg_host *host) |
| 565 | { |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 566 | struct request *req = host->req; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 567 | u32 i; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 568 | |
| 569 | /* check status */ |
| 570 | do { |
| 571 | i = inb((unsigned long)host->dev_base + MG_REG_STATUS); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 572 | if (i & ATA_BUSY) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 573 | break; |
| 574 | if (!MG_READY_OK(i)) |
| 575 | break; |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 576 | if (i & ATA_DRQ) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 577 | goto ok_to_read; |
| 578 | } while (0); |
| 579 | mg_dump_status("mg_read_intr", i, host); |
| 580 | mg_bad_rw_intr(host); |
| 581 | mg_request(host->breq); |
| 582 | return; |
| 583 | |
| 584 | ok_to_read: |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 585 | mg_read_one(host, req); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 586 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 587 | MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n", |
Jens Axboe | b4f42e2 | 2014-04-10 09:46:28 -0600 | [diff] [blame] | 588 | blk_rq_pos(req), blk_rq_sectors(req) - 1, bio_data(req->bio)); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 589 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 590 | /* send read confirm */ |
| 591 | outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND); |
| 592 | |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 593 | if (mg_end_request(host, 0, MG_SECTOR_SIZE)) { |
Tejun Heo | a03bb5a | 2009-04-28 13:06:15 +0900 | [diff] [blame] | 594 | /* 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 Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 598 | mg_request(host->breq); |
| 599 | } |
| 600 | |
| 601 | static void mg_write_intr(struct mg_host *host) |
| 602 | { |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 603 | struct request *req = host->req; |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 604 | u32 i; |
Tejun Heo | a03bb5a | 2009-04-28 13:06:15 +0900 | [diff] [blame] | 605 | bool rem; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 606 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 607 | /* check status */ |
| 608 | do { |
| 609 | i = inb((unsigned long)host->dev_base + MG_REG_STATUS); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 610 | if (i & ATA_BUSY) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 611 | break; |
| 612 | if (!MG_READY_OK(i)) |
| 613 | break; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 614 | if ((blk_rq_sectors(req) <= 1) || (i & ATA_DRQ)) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 615 | goto ok_to_write; |
| 616 | } while (0); |
| 617 | mg_dump_status("mg_write_intr", i, host); |
| 618 | mg_bad_rw_intr(host); |
| 619 | mg_request(host->breq); |
| 620 | return; |
| 621 | |
| 622 | ok_to_write: |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 623 | if ((rem = mg_end_request(host, 0, MG_SECTOR_SIZE))) { |
Tejun Heo | a03bb5a | 2009-04-28 13:06:15 +0900 | [diff] [blame] | 624 | /* write 1 sector and set handler if remains */ |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 625 | mg_write_one(host, req); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 626 | MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n", |
Jens Axboe | b4f42e2 | 2014-04-10 09:46:28 -0600 | [diff] [blame] | 627 | blk_rq_pos(req), blk_rq_sectors(req), bio_data(req->bio)); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 628 | host->mg_do_intr = mg_write_intr; |
| 629 | mod_timer(&host->timer, jiffies + 3 * HZ); |
| 630 | } |
| 631 | |
| 632 | /* send write confirm */ |
| 633 | outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND); |
| 634 | |
Tejun Heo | a03bb5a | 2009-04-28 13:06:15 +0900 | [diff] [blame] | 635 | if (!rem) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 636 | mg_request(host->breq); |
| 637 | } |
| 638 | |
Jingoo Han | c86db97 | 2013-09-11 14:20:10 -0700 | [diff] [blame] | 639 | static void mg_times_out(unsigned long data) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 640 | { |
| 641 | struct mg_host *host = (struct mg_host *)data; |
| 642 | char *name; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 643 | |
Tejun Heo | ac2ff94 | 2009-04-28 12:38:32 +0900 | [diff] [blame] | 644 | spin_lock_irq(&host->lock); |
| 645 | |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 646 | if (!host->req) |
Tejun Heo | ac2ff94 | 2009-04-28 12:38:32 +0900 | [diff] [blame] | 647 | goto out_unlock; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 648 | |
| 649 | host->mg_do_intr = NULL; |
| 650 | |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 651 | name = host->req->rq_disk->disk_name; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 652 | printk(KERN_DEBUG "%s: timeout\n", name); |
| 653 | |
| 654 | host->error = MG_ERR_TIMEOUT; |
| 655 | mg_bad_rw_intr(host); |
| 656 | |
Tejun Heo | ac2ff94 | 2009-04-28 12:38:32 +0900 | [diff] [blame] | 657 | out_unlock: |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 658 | mg_request(host->breq); |
Tejun Heo | ac2ff94 | 2009-04-28 12:38:32 +0900 | [diff] [blame] | 659 | spin_unlock_irq(&host->lock); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static void mg_request_poll(struct request_queue *q) |
| 663 | { |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 664 | struct mg_host *host = q->queuedata; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 665 | |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 666 | while (1) { |
| 667 | if (!host->req) { |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 668 | host->req = blk_fetch_request(q); |
| 669 | if (!host->req) |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 670 | break; |
| 671 | } |
Tejun Heo | 9a8d23d8 | 2009-05-08 11:54:00 +0900 | [diff] [blame] | 672 | |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 673 | if (unlikely(host->req->cmd_type != REQ_TYPE_FS)) { |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 674 | mg_end_request_cur(host, -EIO); |
Tejun Heo | 9a8d23d8 | 2009-05-08 11:54:00 +0900 | [diff] [blame] | 675 | continue; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 676 | } |
Tejun Heo | 9a8d23d8 | 2009-05-08 11:54:00 +0900 | [diff] [blame] | 677 | |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 678 | if (rq_data_dir(host->req) == READ) |
| 679 | mg_read(host->req); |
Tejun Heo | 9a8d23d8 | 2009-05-08 11:54:00 +0900 | [diff] [blame] | 680 | else |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 681 | mg_write(host->req); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
| 685 | static unsigned int mg_issue_req(struct request *req, |
| 686 | struct mg_host *host, |
| 687 | unsigned int sect_num, |
| 688 | unsigned int sect_cnt) |
| 689 | { |
Mike Christie | 56332f0 | 2016-06-08 15:49:41 -0500 | [diff] [blame] | 690 | if (rq_data_dir(req) == READ) { |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 691 | if (mg_out(host, sect_num, sect_cnt, MG_CMD_RD, &mg_read_intr) |
| 692 | != MG_ERR_NONE) { |
| 693 | mg_bad_rw_intr(host); |
| 694 | return host->error; |
| 695 | } |
Mike Christie | 56332f0 | 2016-06-08 15:49:41 -0500 | [diff] [blame] | 696 | } else { |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 697 | /* TODO : handler */ |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 698 | outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 699 | if (mg_out(host, sect_num, sect_cnt, MG_CMD_WR, &mg_write_intr) |
| 700 | != MG_ERR_NONE) { |
| 701 | mg_bad_rw_intr(host); |
| 702 | return host->error; |
| 703 | } |
| 704 | del_timer(&host->timer); |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 705 | mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ); |
| 706 | outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 707 | if (host->error) { |
| 708 | mg_bad_rw_intr(host); |
| 709 | return host->error; |
| 710 | } |
Bartlomiej Zolnierkiewicz | 394c6cc | 2009-07-28 08:56:34 +0200 | [diff] [blame] | 711 | mg_write_one(host, req); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 712 | mod_timer(&host->timer, jiffies + 3 * HZ); |
| 713 | outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + |
| 714 | MG_REG_COMMAND); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 715 | } |
| 716 | return MG_ERR_NONE; |
| 717 | } |
| 718 | |
| 719 | /* This function also called from IRQ context */ |
| 720 | static void mg_request(struct request_queue *q) |
| 721 | { |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 722 | struct mg_host *host = q->queuedata; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 723 | struct request *req; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 724 | u32 sect_num, sect_cnt; |
| 725 | |
| 726 | while (1) { |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 727 | if (!host->req) { |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 728 | host->req = blk_fetch_request(q); |
| 729 | if (!host->req) |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 730 | break; |
| 731 | } |
| 732 | req = host->req; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 733 | |
| 734 | /* check unwanted request call */ |
| 735 | if (host->mg_do_intr) |
| 736 | return; |
| 737 | |
| 738 | del_timer(&host->timer); |
| 739 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 740 | sect_num = blk_rq_pos(req); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 741 | /* deal whole segments */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 742 | sect_cnt = blk_rq_sectors(req); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 743 | |
| 744 | /* sanity check */ |
| 745 | if (sect_num >= get_capacity(req->rq_disk) || |
| 746 | ((sect_num + sect_cnt) > |
| 747 | get_capacity(req->rq_disk))) { |
| 748 | printk(KERN_WARNING |
| 749 | "%s: bad access: sector=%d, count=%d\n", |
| 750 | req->rq_disk->disk_name, |
| 751 | sect_num, sect_cnt); |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 752 | mg_end_request_cur(host, -EIO); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 753 | continue; |
| 754 | } |
| 755 | |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 756 | if (unlikely(req->cmd_type != REQ_TYPE_FS)) { |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 757 | mg_end_request_cur(host, -EIO); |
Tejun Heo | 9a8d23d8 | 2009-05-08 11:54:00 +0900 | [diff] [blame] | 758 | continue; |
| 759 | } |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 760 | |
| 761 | if (!mg_issue_req(req, host, sect_num, sect_cnt)) |
| 762 | return; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | static int mg_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 767 | { |
| 768 | struct mg_host *host = bdev->bd_disk->private_data; |
| 769 | |
| 770 | geo->cylinders = (unsigned short)host->cyls; |
| 771 | geo->heads = (unsigned char)host->heads; |
| 772 | geo->sectors = (unsigned char)host->sectors; |
| 773 | return 0; |
| 774 | } |
| 775 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 776 | static const struct block_device_operations mg_disk_ops = { |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 777 | .getgeo = mg_getgeo |
| 778 | }; |
| 779 | |
Jingoo Han | aed3d67 | 2013-04-29 11:55:51 -0700 | [diff] [blame] | 780 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | 156ffcb | 2012-07-05 22:11:33 +0200 | [diff] [blame] | 781 | static int mg_suspend(struct device *dev) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 782 | { |
Rafael J. Wysocki | 156ffcb | 2012-07-05 22:11:33 +0200 | [diff] [blame] | 783 | struct mg_drv_data *prv_data = dev->platform_data; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 784 | struct mg_host *host = prv_data->host; |
| 785 | |
| 786 | if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) |
| 787 | return -EIO; |
| 788 | |
| 789 | if (!prv_data->use_polling) |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 790 | outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 791 | |
| 792 | outb(MG_CMD_SLEEP, (unsigned long)host->dev_base + MG_REG_COMMAND); |
| 793 | /* wait until mflash deep sleep */ |
| 794 | msleep(1); |
| 795 | |
| 796 | if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) { |
| 797 | if (!prv_data->use_polling) |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 798 | outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 799 | return -EIO; |
| 800 | } |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
Rafael J. Wysocki | 156ffcb | 2012-07-05 22:11:33 +0200 | [diff] [blame] | 805 | static int mg_resume(struct device *dev) |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 806 | { |
Rafael J. Wysocki | 156ffcb | 2012-07-05 22:11:33 +0200 | [diff] [blame] | 807 | struct mg_drv_data *prv_data = dev->platform_data; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 808 | struct mg_host *host = prv_data->host; |
| 809 | |
| 810 | if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) |
| 811 | return -EIO; |
| 812 | |
| 813 | outb(MG_CMD_WAKEUP, (unsigned long)host->dev_base + MG_REG_COMMAND); |
| 814 | /* wait until mflash wakeup */ |
| 815 | msleep(1); |
| 816 | |
| 817 | if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) |
| 818 | return -EIO; |
| 819 | |
| 820 | if (!prv_data->use_polling) |
Bartlomiej Zolnierkiewicz | f68adec | 2009-04-28 13:06:17 +0900 | [diff] [blame] | 821 | outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 822 | |
| 823 | return 0; |
| 824 | } |
Jingoo Han | aed3d67 | 2013-04-29 11:55:51 -0700 | [diff] [blame] | 825 | #endif |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 826 | |
Rafael J. Wysocki | 156ffcb | 2012-07-05 22:11:33 +0200 | [diff] [blame] | 827 | static SIMPLE_DEV_PM_OPS(mg_pm, mg_suspend, mg_resume); |
| 828 | |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 829 | static int mg_probe(struct platform_device *plat_dev) |
| 830 | { |
| 831 | struct mg_host *host; |
| 832 | struct resource *rsc; |
| 833 | struct mg_drv_data *prv_data = plat_dev->dev.platform_data; |
| 834 | int err = 0; |
| 835 | |
| 836 | if (!prv_data) { |
| 837 | printk(KERN_ERR "%s:%d fail (no driver_data)\n", |
| 838 | __func__, __LINE__); |
| 839 | err = -EINVAL; |
| 840 | goto probe_err; |
| 841 | } |
| 842 | |
| 843 | /* alloc mg_host */ |
| 844 | host = kzalloc(sizeof(struct mg_host), GFP_KERNEL); |
| 845 | if (!host) { |
| 846 | printk(KERN_ERR "%s:%d fail (no memory for mg_host)\n", |
| 847 | __func__, __LINE__); |
| 848 | err = -ENOMEM; |
| 849 | goto probe_err; |
| 850 | } |
| 851 | host->major = MG_DISK_MAJ; |
| 852 | |
| 853 | /* link each other */ |
| 854 | prv_data->host = host; |
| 855 | host->dev = &plat_dev->dev; |
| 856 | |
| 857 | /* io remap */ |
| 858 | rsc = platform_get_resource(plat_dev, IORESOURCE_MEM, 0); |
| 859 | if (!rsc) { |
| 860 | printk(KERN_ERR "%s:%d platform_get_resource fail\n", |
| 861 | __func__, __LINE__); |
| 862 | err = -EINVAL; |
| 863 | goto probe_err_2; |
| 864 | } |
H Hartley Sweeten | e019ef0 | 2009-12-21 16:27:49 -0800 | [diff] [blame] | 865 | host->dev_base = ioremap(rsc->start, resource_size(rsc)); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 866 | if (!host->dev_base) { |
| 867 | printk(KERN_ERR "%s:%d ioremap fail\n", |
| 868 | __func__, __LINE__); |
| 869 | err = -EIO; |
| 870 | goto probe_err_2; |
| 871 | } |
| 872 | MG_DBG("dev_base = 0x%x\n", (u32)host->dev_base); |
| 873 | |
| 874 | /* get reset pin */ |
| 875 | rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO, |
| 876 | MG_RST_PIN); |
| 877 | if (!rsc) { |
| 878 | printk(KERN_ERR "%s:%d get reset pin fail\n", |
| 879 | __func__, __LINE__); |
| 880 | err = -EIO; |
| 881 | goto probe_err_3; |
| 882 | } |
| 883 | host->rst = rsc->start; |
| 884 | |
| 885 | /* init rst pin */ |
| 886 | err = gpio_request(host->rst, MG_RST_PIN); |
| 887 | if (err) |
| 888 | goto probe_err_3; |
| 889 | gpio_direction_output(host->rst, 1); |
| 890 | |
| 891 | /* reset out pin */ |
Wei Yongjun | c613c5f | 2013-03-28 09:43:43 -0600 | [diff] [blame] | 892 | if (!(prv_data->dev_attr & MG_DEV_MASK)) { |
| 893 | err = -EINVAL; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 894 | goto probe_err_3a; |
Wei Yongjun | c613c5f | 2013-03-28 09:43:43 -0600 | [diff] [blame] | 895 | } |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 896 | |
| 897 | if (prv_data->dev_attr != MG_BOOT_DEV) { |
| 898 | rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO, |
| 899 | MG_RSTOUT_PIN); |
| 900 | if (!rsc) { |
| 901 | printk(KERN_ERR "%s:%d get reset-out pin fail\n", |
| 902 | __func__, __LINE__); |
| 903 | err = -EIO; |
| 904 | goto probe_err_3a; |
| 905 | } |
| 906 | host->rstout = rsc->start; |
| 907 | err = gpio_request(host->rstout, MG_RSTOUT_PIN); |
| 908 | if (err) |
| 909 | goto probe_err_3a; |
| 910 | gpio_direction_input(host->rstout); |
| 911 | } |
| 912 | |
| 913 | /* disk reset */ |
| 914 | if (prv_data->dev_attr == MG_STORAGE_DEV) { |
Geert Uytterhoeven | 14424be | 2014-01-21 16:15:53 +0100 | [diff] [blame] | 915 | /* If POR seq. not yet finished, wait */ |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 916 | err = mg_wait_rstout(host->rstout, MG_TMAX_RSTOUT); |
| 917 | if (err) |
| 918 | goto probe_err_3b; |
| 919 | err = mg_disk_init(host); |
| 920 | if (err) { |
| 921 | printk(KERN_ERR "%s:%d fail (err code : %d)\n", |
| 922 | __func__, __LINE__, err); |
| 923 | err = -EIO; |
| 924 | goto probe_err_3b; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | /* get irq resource */ |
| 929 | if (!prv_data->use_polling) { |
| 930 | host->irq = platform_get_irq(plat_dev, 0); |
| 931 | if (host->irq == -ENXIO) { |
| 932 | err = host->irq; |
| 933 | goto probe_err_3b; |
| 934 | } |
| 935 | err = request_irq(host->irq, mg_irq, |
Michael Opdenacker | 370d668 | 2013-10-12 06:33:47 +0200 | [diff] [blame] | 936 | IRQF_TRIGGER_RISING, |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 937 | MG_DEV_NAME, host); |
| 938 | if (err) { |
| 939 | printk(KERN_ERR "%s:%d fail (request_irq err=%d)\n", |
| 940 | __func__, __LINE__, err); |
| 941 | goto probe_err_3b; |
| 942 | } |
| 943 | |
| 944 | } |
| 945 | |
| 946 | /* get disk id */ |
| 947 | err = mg_get_disk_id(host); |
| 948 | if (err) { |
| 949 | printk(KERN_ERR "%s:%d fail (err code : %d)\n", |
| 950 | __func__, __LINE__, err); |
| 951 | err = -EIO; |
| 952 | goto probe_err_4; |
| 953 | } |
| 954 | |
| 955 | err = register_blkdev(host->major, MG_DISK_NAME); |
| 956 | if (err < 0) { |
| 957 | printk(KERN_ERR "%s:%d register_blkdev fail (err code : %d)\n", |
| 958 | __func__, __LINE__, err); |
| 959 | goto probe_err_4; |
| 960 | } |
| 961 | if (!host->major) |
| 962 | host->major = err; |
| 963 | |
| 964 | spin_lock_init(&host->lock); |
| 965 | |
| 966 | if (prv_data->use_polling) |
| 967 | host->breq = blk_init_queue(mg_request_poll, &host->lock); |
| 968 | else |
| 969 | host->breq = blk_init_queue(mg_request, &host->lock); |
| 970 | |
| 971 | if (!host->breq) { |
| 972 | err = -ENOMEM; |
| 973 | printk(KERN_ERR "%s:%d (blk_init_queue) fail\n", |
| 974 | __func__, __LINE__); |
| 975 | goto probe_err_5; |
| 976 | } |
Tejun Heo | 5b36ad6 | 2009-05-08 11:54:01 +0900 | [diff] [blame] | 977 | host->breq->queuedata = host; |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 978 | |
| 979 | /* mflash is random device, thanx for the noop */ |
Jens Axboe | 52cc2ee | 2010-08-23 14:02:44 +0200 | [diff] [blame] | 980 | err = elevator_change(host->breq, "noop"); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 981 | if (err) { |
| 982 | printk(KERN_ERR "%s:%d (elevator_init) fail\n", |
| 983 | __func__, __LINE__); |
| 984 | goto probe_err_6; |
| 985 | } |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 986 | blk_queue_max_hw_sectors(host->breq, MG_MAX_SECTS); |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 987 | blk_queue_logical_block_size(host->breq, MG_SECTOR_SIZE); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 988 | |
| 989 | init_timer(&host->timer); |
| 990 | host->timer.function = mg_times_out; |
| 991 | host->timer.data = (unsigned long)host; |
| 992 | |
| 993 | host->gd = alloc_disk(MG_DISK_MAX_PART); |
| 994 | if (!host->gd) { |
| 995 | printk(KERN_ERR "%s:%d (alloc_disk) fail\n", |
| 996 | __func__, __LINE__); |
| 997 | err = -ENOMEM; |
| 998 | goto probe_err_7; |
| 999 | } |
| 1000 | host->gd->major = host->major; |
| 1001 | host->gd->first_minor = 0; |
| 1002 | host->gd->fops = &mg_disk_ops; |
| 1003 | host->gd->queue = host->breq; |
| 1004 | host->gd->private_data = host; |
| 1005 | sprintf(host->gd->disk_name, MG_DISK_NAME"a"); |
| 1006 | |
| 1007 | set_capacity(host->gd, host->n_sectors); |
| 1008 | |
| 1009 | add_disk(host->gd); |
| 1010 | |
| 1011 | return err; |
| 1012 | |
| 1013 | probe_err_7: |
| 1014 | del_timer_sync(&host->timer); |
| 1015 | probe_err_6: |
| 1016 | blk_cleanup_queue(host->breq); |
| 1017 | probe_err_5: |
Bartlomiej Zolnierkiewicz | 9e2d23f | 2016-06-28 15:42:41 +0200 | [diff] [blame] | 1018 | unregister_blkdev(host->major, MG_DISK_NAME); |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 1019 | probe_err_4: |
| 1020 | if (!prv_data->use_polling) |
| 1021 | free_irq(host->irq, host); |
| 1022 | probe_err_3b: |
| 1023 | gpio_free(host->rstout); |
| 1024 | probe_err_3a: |
| 1025 | gpio_free(host->rst); |
| 1026 | probe_err_3: |
| 1027 | iounmap(host->dev_base); |
| 1028 | probe_err_2: |
| 1029 | kfree(host); |
| 1030 | probe_err: |
| 1031 | return err; |
| 1032 | } |
| 1033 | |
| 1034 | static int mg_remove(struct platform_device *plat_dev) |
| 1035 | { |
| 1036 | struct mg_drv_data *prv_data = plat_dev->dev.platform_data; |
| 1037 | struct mg_host *host = prv_data->host; |
| 1038 | int err = 0; |
| 1039 | |
| 1040 | /* delete timer */ |
| 1041 | del_timer_sync(&host->timer); |
| 1042 | |
| 1043 | /* remove disk */ |
| 1044 | if (host->gd) { |
| 1045 | del_gendisk(host->gd); |
| 1046 | put_disk(host->gd); |
| 1047 | } |
| 1048 | /* remove queue */ |
| 1049 | if (host->breq) |
| 1050 | blk_cleanup_queue(host->breq); |
| 1051 | |
| 1052 | /* unregister blk device */ |
| 1053 | unregister_blkdev(host->major, MG_DISK_NAME); |
| 1054 | |
| 1055 | /* free irq */ |
| 1056 | if (!prv_data->use_polling) |
| 1057 | free_irq(host->irq, host); |
| 1058 | |
| 1059 | /* free reset-out pin */ |
| 1060 | if (prv_data->dev_attr != MG_BOOT_DEV) |
| 1061 | gpio_free(host->rstout); |
| 1062 | |
| 1063 | /* free rst pin */ |
| 1064 | if (host->rst) |
| 1065 | gpio_free(host->rst); |
| 1066 | |
| 1067 | /* unmap io */ |
| 1068 | if (host->dev_base) |
| 1069 | iounmap(host->dev_base); |
| 1070 | |
| 1071 | /* free mg_host */ |
| 1072 | kfree(host); |
| 1073 | |
| 1074 | return err; |
| 1075 | } |
| 1076 | |
| 1077 | static struct platform_driver mg_disk_driver = { |
| 1078 | .probe = mg_probe, |
| 1079 | .remove = mg_remove, |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 1080 | .driver = { |
| 1081 | .name = MG_DEV_NAME, |
Rafael J. Wysocki | 156ffcb | 2012-07-05 22:11:33 +0200 | [diff] [blame] | 1082 | .pm = &mg_pm, |
unsik Kim | 3fbed4c | 2009-04-02 12:50:58 -0700 | [diff] [blame] | 1083 | } |
| 1084 | }; |
| 1085 | |
| 1086 | /**************************************************************************** |
| 1087 | * |
| 1088 | * Module stuff |
| 1089 | * |
| 1090 | ****************************************************************************/ |
| 1091 | |
| 1092 | static int __init mg_init(void) |
| 1093 | { |
| 1094 | printk(KERN_INFO "mGine mflash driver, (c) 2008 mGine Co.\n"); |
| 1095 | return platform_driver_register(&mg_disk_driver); |
| 1096 | } |
| 1097 | |
| 1098 | static void __exit mg_exit(void) |
| 1099 | { |
| 1100 | printk(KERN_INFO "mflash driver : bye bye\n"); |
| 1101 | platform_driver_unregister(&mg_disk_driver); |
| 1102 | } |
| 1103 | |
| 1104 | module_init(mg_init); |
| 1105 | module_exit(mg_exit); |
| 1106 | |
| 1107 | MODULE_LICENSE("GPL"); |
| 1108 | MODULE_AUTHOR("unsik Kim <donari75@gmail.com>"); |
| 1109 | MODULE_DESCRIPTION("mGine m[g]flash device driver"); |