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