mg_disk.c 26 KB

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