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