mg_disk.c 26 KB

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