mg_disk.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. #include <linux/slab.h>
  26. #define MG_RES_SEC (CONFIG_MG_DISK_RES << 1)
  27. /* name for block device */
  28. #define MG_DISK_NAME "mgd"
  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_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. /* These 2 times dummy status read prevents reading invalid
  192. * status. A very little time (3 times of mflash operating clk)
  193. * is required for busy bit is set. Use dummy read instead of
  194. * busy wait, because mflash's PLL is machine dependent.
  195. */
  196. if (prv_data->use_polling) {
  197. status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  198. status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  199. }
  200. status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  201. do {
  202. cur_jiffies = jiffies;
  203. if (status & ATA_BUSY) {
  204. if (expect == ATA_BUSY)
  205. break;
  206. } else {
  207. /* Check the error condition! */
  208. if (status & ATA_ERR) {
  209. mg_dump_status("mg_wait", status, host);
  210. break;
  211. }
  212. if (expect == MG_STAT_READY)
  213. if (MG_READY_OK(status))
  214. break;
  215. if (expect == ATA_DRQ)
  216. if (status & ATA_DRQ)
  217. break;
  218. }
  219. if (!msec) {
  220. mg_dump_status("not ready", status, host);
  221. return MG_ERR_INV_STAT;
  222. }
  223. status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  224. } while (time_before(cur_jiffies, expire));
  225. if (time_after_eq(cur_jiffies, expire) && msec)
  226. host->error = MG_ERR_TIMEOUT;
  227. return host->error;
  228. }
  229. static unsigned int mg_wait_rstout(u32 rstout, u32 msec)
  230. {
  231. unsigned long expire;
  232. expire = jiffies + msecs_to_jiffies(msec);
  233. while (time_before(jiffies, expire)) {
  234. if (gpio_get_value(rstout) == 1)
  235. return MG_ERR_NONE;
  236. msleep(10);
  237. }
  238. return MG_ERR_RSTOUT;
  239. }
  240. static void mg_unexpected_intr(struct mg_host *host)
  241. {
  242. u32 status = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  243. mg_dump_status("mg_unexpected_intr", status, host);
  244. }
  245. static irqreturn_t mg_irq(int irq, void *dev_id)
  246. {
  247. struct mg_host *host = dev_id;
  248. void (*handler)(struct mg_host *) = host->mg_do_intr;
  249. spin_lock(&host->lock);
  250. host->mg_do_intr = NULL;
  251. del_timer(&host->timer);
  252. if (!handler)
  253. handler = mg_unexpected_intr;
  254. handler(host);
  255. spin_unlock(&host->lock);
  256. return IRQ_HANDLED;
  257. }
  258. /* local copy of ata_id_string() */
  259. static void mg_id_string(const u16 *id, unsigned char *s,
  260. unsigned int ofs, unsigned int len)
  261. {
  262. unsigned int c;
  263. BUG_ON(len & 1);
  264. while (len > 0) {
  265. c = id[ofs] >> 8;
  266. *s = c;
  267. s++;
  268. c = id[ofs] & 0xff;
  269. *s = c;
  270. s++;
  271. ofs++;
  272. len -= 2;
  273. }
  274. }
  275. /* local copy of ata_id_c_string() */
  276. static void mg_id_c_string(const u16 *id, unsigned char *s,
  277. unsigned int ofs, unsigned int len)
  278. {
  279. unsigned char *p;
  280. mg_id_string(id, s, ofs, len - 1);
  281. p = s + strnlen(s, len - 1);
  282. while (p > s && p[-1] == ' ')
  283. p--;
  284. *p = '\0';
  285. }
  286. static int mg_get_disk_id(struct mg_host *host)
  287. {
  288. u32 i;
  289. s32 err;
  290. const u16 *id = host->id;
  291. struct mg_drv_data *prv_data = host->dev->platform_data;
  292. char fwrev[ATA_ID_FW_REV_LEN + 1];
  293. char model[ATA_ID_PROD_LEN + 1];
  294. char serial[ATA_ID_SERNO_LEN + 1];
  295. if (!prv_data->use_polling)
  296. outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
  297. outb(MG_CMD_ID, (unsigned long)host->dev_base + MG_REG_COMMAND);
  298. err = mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_RD_DRQ);
  299. if (err)
  300. return err;
  301. for (i = 0; i < (MG_SECTOR_SIZE >> 1); i++)
  302. host->id[i] = le16_to_cpu(inw((unsigned long)host->dev_base +
  303. MG_BUFF_OFFSET + i * 2));
  304. outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
  305. err = mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD);
  306. if (err)
  307. return err;
  308. if ((id[ATA_ID_FIELD_VALID] & 1) == 0)
  309. return MG_ERR_TRANSLATION;
  310. host->n_sectors = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
  311. host->cyls = id[ATA_ID_CYLS];
  312. host->heads = id[ATA_ID_HEADS];
  313. host->sectors = id[ATA_ID_SECTORS];
  314. if (MG_RES_SEC && host->heads && host->sectors) {
  315. /* modify cyls, n_sectors */
  316. host->cyls = (host->n_sectors - MG_RES_SEC) /
  317. host->heads / host->sectors;
  318. host->nres_sectors = host->n_sectors - host->cyls *
  319. host->heads * host->sectors;
  320. host->n_sectors -= host->nres_sectors;
  321. }
  322. mg_id_c_string(id, fwrev, ATA_ID_FW_REV, sizeof(fwrev));
  323. mg_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
  324. mg_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
  325. printk(KERN_INFO "mg_disk: model: %s\n", model);
  326. printk(KERN_INFO "mg_disk: firm: %.8s\n", fwrev);
  327. printk(KERN_INFO "mg_disk: serial: %s\n", serial);
  328. printk(KERN_INFO "mg_disk: %d + reserved %d sectors\n",
  329. host->n_sectors, host->nres_sectors);
  330. if (!prv_data->use_polling)
  331. outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
  332. return err;
  333. }
  334. static int mg_disk_init(struct mg_host *host)
  335. {
  336. struct mg_drv_data *prv_data = host->dev->platform_data;
  337. s32 err;
  338. u8 init_status;
  339. /* hdd rst low */
  340. gpio_set_value(host->rst, 0);
  341. err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
  342. if (err)
  343. return err;
  344. /* hdd rst high */
  345. gpio_set_value(host->rst, 1);
  346. err = mg_wait(host, MG_STAT_READY, MG_TMAX_HDRST_TO_RDY);
  347. if (err)
  348. return err;
  349. /* soft reset on */
  350. outb(ATA_SRST | (prv_data->use_polling ? ATA_NIEN : 0),
  351. (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
  352. err = mg_wait(host, ATA_BUSY, MG_TMAX_RST_TO_BUSY);
  353. if (err)
  354. return err;
  355. /* soft reset off */
  356. outb(prv_data->use_polling ? ATA_NIEN : 0,
  357. (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
  358. err = mg_wait(host, MG_STAT_READY, MG_TMAX_SWRST_TO_RDY);
  359. if (err)
  360. return err;
  361. init_status = inb((unsigned long)host->dev_base + MG_REG_STATUS) & 0xf;
  362. if (init_status == 0xf)
  363. return MG_ERR_INIT_STAT;
  364. return err;
  365. }
  366. static void mg_bad_rw_intr(struct mg_host *host)
  367. {
  368. if (host->req)
  369. if (++host->req->errors >= MG_MAX_ERRORS ||
  370. host->error == MG_ERR_TIMEOUT)
  371. mg_end_request_cur(host, -EIO);
  372. }
  373. static unsigned int mg_out(struct mg_host *host,
  374. unsigned int sect_num,
  375. unsigned int sect_cnt,
  376. unsigned int cmd,
  377. void (*intr_addr)(struct mg_host *))
  378. {
  379. struct mg_drv_data *prv_data = host->dev->platform_data;
  380. if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
  381. return host->error;
  382. if (!prv_data->use_polling) {
  383. host->mg_do_intr = intr_addr;
  384. mod_timer(&host->timer, jiffies + 3 * HZ);
  385. }
  386. if (MG_RES_SEC)
  387. sect_num += MG_RES_SEC;
  388. outb((u8)sect_cnt, (unsigned long)host->dev_base + MG_REG_SECT_CNT);
  389. outb((u8)sect_num, (unsigned long)host->dev_base + MG_REG_SECT_NUM);
  390. outb((u8)(sect_num >> 8), (unsigned long)host->dev_base +
  391. MG_REG_CYL_LOW);
  392. outb((u8)(sect_num >> 16), (unsigned long)host->dev_base +
  393. MG_REG_CYL_HIGH);
  394. outb((u8)((sect_num >> 24) | ATA_LBA | ATA_DEVICE_OBS),
  395. (unsigned long)host->dev_base + MG_REG_DRV_HEAD);
  396. outb(cmd, (unsigned long)host->dev_base + MG_REG_COMMAND);
  397. return MG_ERR_NONE;
  398. }
  399. static void mg_read_one(struct mg_host *host, struct request *req)
  400. {
  401. u16 *buff = (u16 *)bio_data(req->bio);
  402. u32 i;
  403. for (i = 0; i < MG_SECTOR_SIZE >> 1; i++)
  404. *buff++ = inw((unsigned long)host->dev_base + MG_BUFF_OFFSET +
  405. (i << 1));
  406. }
  407. static void mg_read(struct request *req)
  408. {
  409. struct mg_host *host = req->rq_disk->private_data;
  410. if (mg_out(host, blk_rq_pos(req), blk_rq_sectors(req),
  411. MG_CMD_RD, NULL) != MG_ERR_NONE)
  412. mg_bad_rw_intr(host);
  413. MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
  414. blk_rq_sectors(req), blk_rq_pos(req), bio_data(req->bio));
  415. do {
  416. if (mg_wait(host, ATA_DRQ,
  417. MG_TMAX_WAIT_RD_DRQ) != MG_ERR_NONE) {
  418. mg_bad_rw_intr(host);
  419. return;
  420. }
  421. mg_read_one(host, req);
  422. outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base +
  423. MG_REG_COMMAND);
  424. } while (mg_end_request(host, 0, MG_SECTOR_SIZE));
  425. }
  426. static void mg_write_one(struct mg_host *host, struct request *req)
  427. {
  428. u16 *buff = (u16 *)bio_data(req->bio);
  429. u32 i;
  430. for (i = 0; i < MG_SECTOR_SIZE >> 1; i++)
  431. outw(*buff++, (unsigned long)host->dev_base + MG_BUFF_OFFSET +
  432. (i << 1));
  433. }
  434. static void mg_write(struct request *req)
  435. {
  436. struct mg_host *host = req->rq_disk->private_data;
  437. unsigned int rem = blk_rq_sectors(req);
  438. if (mg_out(host, blk_rq_pos(req), rem,
  439. MG_CMD_WR, NULL) != MG_ERR_NONE) {
  440. mg_bad_rw_intr(host);
  441. return;
  442. }
  443. MG_DBG("requested %d sects (from %ld), buffer=0x%p\n",
  444. rem, blk_rq_pos(req), bio_data(req->bio));
  445. if (mg_wait(host, ATA_DRQ,
  446. MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
  447. mg_bad_rw_intr(host);
  448. return;
  449. }
  450. do {
  451. mg_write_one(host, req);
  452. outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
  453. MG_REG_COMMAND);
  454. rem--;
  455. if (rem > 1 && mg_wait(host, ATA_DRQ,
  456. MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
  457. mg_bad_rw_intr(host);
  458. return;
  459. } else if (mg_wait(host, MG_STAT_READY,
  460. MG_TMAX_WAIT_WR_DRQ) != MG_ERR_NONE) {
  461. mg_bad_rw_intr(host);
  462. return;
  463. }
  464. } while (mg_end_request(host, 0, MG_SECTOR_SIZE));
  465. }
  466. static void mg_read_intr(struct mg_host *host)
  467. {
  468. struct request *req = host->req;
  469. u32 i;
  470. /* check status */
  471. do {
  472. i = inb((unsigned long)host->dev_base + MG_REG_STATUS);
  473. if (i & ATA_BUSY)
  474. break;
  475. if (!MG_READY_OK(i))
  476. break;
  477. if (i & ATA_DRQ)
  478. goto ok_to_read;
  479. } while (0);
  480. mg_dump_status("mg_read_intr", i, host);
  481. mg_bad_rw_intr(host);
  482. mg_request(host->breq);
  483. return;
  484. ok_to_read:
  485. mg_read_one(host, req);
  486. MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
  487. blk_rq_pos(req), blk_rq_sectors(req) - 1, bio_data(req->bio));
  488. /* send read confirm */
  489. outb(MG_CMD_RD_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
  490. if (mg_end_request(host, 0, MG_SECTOR_SIZE)) {
  491. /* set handler if read remains */
  492. host->mg_do_intr = mg_read_intr;
  493. mod_timer(&host->timer, jiffies + 3 * HZ);
  494. } else /* goto next request */
  495. mg_request(host->breq);
  496. }
  497. static void mg_write_intr(struct mg_host *host)
  498. {
  499. struct request *req = host->req;
  500. u32 i;
  501. bool rem;
  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 = mg_end_request(host, 0, MG_SECTOR_SIZE))) {
  518. /* write 1 sector and set handler if remains */
  519. mg_write_one(host, req);
  520. MG_DBG("sector %ld, remaining=%ld, buffer=0x%p\n",
  521. blk_rq_pos(req), blk_rq_sectors(req), bio_data(req->bio));
  522. host->mg_do_intr = mg_write_intr;
  523. mod_timer(&host->timer, jiffies + 3 * HZ);
  524. }
  525. /* send write confirm */
  526. outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base + MG_REG_COMMAND);
  527. if (!rem)
  528. mg_request(host->breq);
  529. }
  530. static void mg_times_out(unsigned long data)
  531. {
  532. struct mg_host *host = (struct mg_host *)data;
  533. char *name;
  534. spin_lock_irq(&host->lock);
  535. if (!host->req)
  536. goto out_unlock;
  537. host->mg_do_intr = NULL;
  538. name = host->req->rq_disk->disk_name;
  539. printk(KERN_DEBUG "%s: timeout\n", name);
  540. host->error = MG_ERR_TIMEOUT;
  541. mg_bad_rw_intr(host);
  542. out_unlock:
  543. mg_request(host->breq);
  544. spin_unlock_irq(&host->lock);
  545. }
  546. static void mg_request_poll(struct request_queue *q)
  547. {
  548. struct mg_host *host = q->queuedata;
  549. while (1) {
  550. if (!host->req) {
  551. host->req = blk_fetch_request(q);
  552. if (!host->req)
  553. break;
  554. }
  555. if (unlikely(host->req->cmd_type != REQ_TYPE_FS)) {
  556. mg_end_request_cur(host, -EIO);
  557. continue;
  558. }
  559. if (rq_data_dir(host->req) == READ)
  560. mg_read(host->req);
  561. else
  562. mg_write(host->req);
  563. }
  564. }
  565. static unsigned int mg_issue_req(struct request *req,
  566. struct mg_host *host,
  567. unsigned int sect_num,
  568. unsigned int sect_cnt)
  569. {
  570. if (rq_data_dir(req) == READ) {
  571. if (mg_out(host, sect_num, sect_cnt, MG_CMD_RD, &mg_read_intr)
  572. != MG_ERR_NONE) {
  573. mg_bad_rw_intr(host);
  574. return host->error;
  575. }
  576. } else {
  577. /* TODO : handler */
  578. outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
  579. if (mg_out(host, sect_num, sect_cnt, MG_CMD_WR, &mg_write_intr)
  580. != MG_ERR_NONE) {
  581. mg_bad_rw_intr(host);
  582. return host->error;
  583. }
  584. del_timer(&host->timer);
  585. mg_wait(host, ATA_DRQ, MG_TMAX_WAIT_WR_DRQ);
  586. outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
  587. if (host->error) {
  588. mg_bad_rw_intr(host);
  589. return host->error;
  590. }
  591. mg_write_one(host, req);
  592. mod_timer(&host->timer, jiffies + 3 * HZ);
  593. outb(MG_CMD_WR_CONF, (unsigned long)host->dev_base +
  594. MG_REG_COMMAND);
  595. }
  596. return MG_ERR_NONE;
  597. }
  598. /* This function also called from IRQ context */
  599. static void mg_request(struct request_queue *q)
  600. {
  601. struct mg_host *host = q->queuedata;
  602. struct request *req;
  603. u32 sect_num, sect_cnt;
  604. while (1) {
  605. if (!host->req) {
  606. host->req = blk_fetch_request(q);
  607. if (!host->req)
  608. break;
  609. }
  610. req = host->req;
  611. /* check unwanted request call */
  612. if (host->mg_do_intr)
  613. return;
  614. del_timer(&host->timer);
  615. sect_num = blk_rq_pos(req);
  616. /* deal whole segments */
  617. sect_cnt = blk_rq_sectors(req);
  618. /* sanity check */
  619. if (sect_num >= get_capacity(req->rq_disk) ||
  620. ((sect_num + sect_cnt) >
  621. get_capacity(req->rq_disk))) {
  622. printk(KERN_WARNING
  623. "%s: bad access: sector=%d, count=%d\n",
  624. req->rq_disk->disk_name,
  625. sect_num, sect_cnt);
  626. mg_end_request_cur(host, -EIO);
  627. continue;
  628. }
  629. if (unlikely(req->cmd_type != REQ_TYPE_FS)) {
  630. mg_end_request_cur(host, -EIO);
  631. continue;
  632. }
  633. if (!mg_issue_req(req, host, sect_num, sect_cnt))
  634. return;
  635. }
  636. }
  637. static int mg_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  638. {
  639. struct mg_host *host = bdev->bd_disk->private_data;
  640. geo->cylinders = (unsigned short)host->cyls;
  641. geo->heads = (unsigned char)host->heads;
  642. geo->sectors = (unsigned char)host->sectors;
  643. return 0;
  644. }
  645. static const struct block_device_operations mg_disk_ops = {
  646. .getgeo = mg_getgeo
  647. };
  648. #ifdef CONFIG_PM_SLEEP
  649. static int mg_suspend(struct device *dev)
  650. {
  651. struct mg_drv_data *prv_data = dev->platform_data;
  652. struct mg_host *host = prv_data->host;
  653. if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
  654. return -EIO;
  655. if (!prv_data->use_polling)
  656. outb(ATA_NIEN, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
  657. outb(MG_CMD_SLEEP, (unsigned long)host->dev_base + MG_REG_COMMAND);
  658. /* wait until mflash deep sleep */
  659. msleep(1);
  660. if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD)) {
  661. if (!prv_data->use_polling)
  662. outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
  663. return -EIO;
  664. }
  665. return 0;
  666. }
  667. static int mg_resume(struct device *dev)
  668. {
  669. struct mg_drv_data *prv_data = dev->platform_data;
  670. struct mg_host *host = prv_data->host;
  671. if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
  672. return -EIO;
  673. outb(MG_CMD_WAKEUP, (unsigned long)host->dev_base + MG_REG_COMMAND);
  674. /* wait until mflash wakeup */
  675. msleep(1);
  676. if (mg_wait(host, MG_STAT_READY, MG_TMAX_CONF_TO_CMD))
  677. return -EIO;
  678. if (!prv_data->use_polling)
  679. outb(0, (unsigned long)host->dev_base + MG_REG_DRV_CTRL);
  680. return 0;
  681. }
  682. #endif
  683. static SIMPLE_DEV_PM_OPS(mg_pm, mg_suspend, mg_resume);
  684. static int mg_probe(struct platform_device *plat_dev)
  685. {
  686. struct mg_host *host;
  687. struct resource *rsc;
  688. struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
  689. int err = 0;
  690. if (!prv_data) {
  691. printk(KERN_ERR "%s:%d fail (no driver_data)\n",
  692. __func__, __LINE__);
  693. err = -EINVAL;
  694. goto probe_err;
  695. }
  696. /* alloc mg_host */
  697. host = kzalloc(sizeof(struct mg_host), GFP_KERNEL);
  698. if (!host) {
  699. printk(KERN_ERR "%s:%d fail (no memory for mg_host)\n",
  700. __func__, __LINE__);
  701. err = -ENOMEM;
  702. goto probe_err;
  703. }
  704. host->major = MG_DISK_MAJ;
  705. /* link each other */
  706. prv_data->host = host;
  707. host->dev = &plat_dev->dev;
  708. /* io remap */
  709. rsc = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
  710. if (!rsc) {
  711. printk(KERN_ERR "%s:%d platform_get_resource fail\n",
  712. __func__, __LINE__);
  713. err = -EINVAL;
  714. goto probe_err_2;
  715. }
  716. host->dev_base = ioremap(rsc->start, resource_size(rsc));
  717. if (!host->dev_base) {
  718. printk(KERN_ERR "%s:%d ioremap fail\n",
  719. __func__, __LINE__);
  720. err = -EIO;
  721. goto probe_err_2;
  722. }
  723. MG_DBG("dev_base = 0x%x\n", (u32)host->dev_base);
  724. /* get reset pin */
  725. rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
  726. MG_RST_PIN);
  727. if (!rsc) {
  728. printk(KERN_ERR "%s:%d get reset pin fail\n",
  729. __func__, __LINE__);
  730. err = -EIO;
  731. goto probe_err_3;
  732. }
  733. host->rst = rsc->start;
  734. /* init rst pin */
  735. err = gpio_request(host->rst, MG_RST_PIN);
  736. if (err)
  737. goto probe_err_3;
  738. gpio_direction_output(host->rst, 1);
  739. /* reset out pin */
  740. if (!(prv_data->dev_attr & MG_DEV_MASK)) {
  741. err = -EINVAL;
  742. goto probe_err_3a;
  743. }
  744. if (prv_data->dev_attr != MG_BOOT_DEV) {
  745. rsc = platform_get_resource_byname(plat_dev, IORESOURCE_IO,
  746. MG_RSTOUT_PIN);
  747. if (!rsc) {
  748. printk(KERN_ERR "%s:%d get reset-out pin fail\n",
  749. __func__, __LINE__);
  750. err = -EIO;
  751. goto probe_err_3a;
  752. }
  753. host->rstout = rsc->start;
  754. err = gpio_request(host->rstout, MG_RSTOUT_PIN);
  755. if (err)
  756. goto probe_err_3a;
  757. gpio_direction_input(host->rstout);
  758. }
  759. /* disk reset */
  760. if (prv_data->dev_attr == MG_STORAGE_DEV) {
  761. /* If POR seq. not yet finished, wait */
  762. err = mg_wait_rstout(host->rstout, MG_TMAX_RSTOUT);
  763. if (err)
  764. goto probe_err_3b;
  765. err = mg_disk_init(host);
  766. if (err) {
  767. printk(KERN_ERR "%s:%d fail (err code : %d)\n",
  768. __func__, __LINE__, err);
  769. err = -EIO;
  770. goto probe_err_3b;
  771. }
  772. }
  773. /* get irq resource */
  774. if (!prv_data->use_polling) {
  775. host->irq = platform_get_irq(plat_dev, 0);
  776. if (host->irq == -ENXIO) {
  777. err = host->irq;
  778. goto probe_err_3b;
  779. }
  780. err = request_irq(host->irq, mg_irq,
  781. IRQF_TRIGGER_RISING,
  782. MG_DEV_NAME, host);
  783. if (err) {
  784. printk(KERN_ERR "%s:%d fail (request_irq err=%d)\n",
  785. __func__, __LINE__, err);
  786. goto probe_err_3b;
  787. }
  788. }
  789. /* get disk id */
  790. err = mg_get_disk_id(host);
  791. if (err) {
  792. printk(KERN_ERR "%s:%d fail (err code : %d)\n",
  793. __func__, __LINE__, err);
  794. err = -EIO;
  795. goto probe_err_4;
  796. }
  797. err = register_blkdev(host->major, MG_DISK_NAME);
  798. if (err < 0) {
  799. printk(KERN_ERR "%s:%d register_blkdev fail (err code : %d)\n",
  800. __func__, __LINE__, err);
  801. goto probe_err_4;
  802. }
  803. if (!host->major)
  804. host->major = err;
  805. spin_lock_init(&host->lock);
  806. if (prv_data->use_polling)
  807. host->breq = blk_init_queue(mg_request_poll, &host->lock);
  808. else
  809. host->breq = blk_init_queue(mg_request, &host->lock);
  810. if (!host->breq) {
  811. err = -ENOMEM;
  812. printk(KERN_ERR "%s:%d (blk_init_queue) fail\n",
  813. __func__, __LINE__);
  814. goto probe_err_5;
  815. }
  816. host->breq->queuedata = host;
  817. /* mflash is random device, thanx for the noop */
  818. err = elevator_change(host->breq, "noop");
  819. if (err) {
  820. printk(KERN_ERR "%s:%d (elevator_init) fail\n",
  821. __func__, __LINE__);
  822. goto probe_err_6;
  823. }
  824. blk_queue_max_hw_sectors(host->breq, MG_MAX_SECTS);
  825. blk_queue_logical_block_size(host->breq, MG_SECTOR_SIZE);
  826. init_timer(&host->timer);
  827. host->timer.function = mg_times_out;
  828. host->timer.data = (unsigned long)host;
  829. host->gd = alloc_disk(MG_DISK_MAX_PART);
  830. if (!host->gd) {
  831. printk(KERN_ERR "%s:%d (alloc_disk) fail\n",
  832. __func__, __LINE__);
  833. err = -ENOMEM;
  834. goto probe_err_7;
  835. }
  836. host->gd->major = host->major;
  837. host->gd->first_minor = 0;
  838. host->gd->fops = &mg_disk_ops;
  839. host->gd->queue = host->breq;
  840. host->gd->private_data = host;
  841. sprintf(host->gd->disk_name, MG_DISK_NAME"a");
  842. set_capacity(host->gd, host->n_sectors);
  843. add_disk(host->gd);
  844. return err;
  845. probe_err_7:
  846. del_timer_sync(&host->timer);
  847. probe_err_6:
  848. blk_cleanup_queue(host->breq);
  849. probe_err_5:
  850. unregister_blkdev(host->major, MG_DISK_NAME);
  851. probe_err_4:
  852. if (!prv_data->use_polling)
  853. free_irq(host->irq, host);
  854. probe_err_3b:
  855. gpio_free(host->rstout);
  856. probe_err_3a:
  857. gpio_free(host->rst);
  858. probe_err_3:
  859. iounmap(host->dev_base);
  860. probe_err_2:
  861. kfree(host);
  862. probe_err:
  863. return err;
  864. }
  865. static int mg_remove(struct platform_device *plat_dev)
  866. {
  867. struct mg_drv_data *prv_data = plat_dev->dev.platform_data;
  868. struct mg_host *host = prv_data->host;
  869. int err = 0;
  870. /* delete timer */
  871. del_timer_sync(&host->timer);
  872. /* remove disk */
  873. if (host->gd) {
  874. del_gendisk(host->gd);
  875. put_disk(host->gd);
  876. }
  877. /* remove queue */
  878. if (host->breq)
  879. blk_cleanup_queue(host->breq);
  880. /* unregister blk device */
  881. unregister_blkdev(host->major, MG_DISK_NAME);
  882. /* free irq */
  883. if (!prv_data->use_polling)
  884. free_irq(host->irq, host);
  885. /* free reset-out pin */
  886. if (prv_data->dev_attr != MG_BOOT_DEV)
  887. gpio_free(host->rstout);
  888. /* free rst pin */
  889. if (host->rst)
  890. gpio_free(host->rst);
  891. /* unmap io */
  892. if (host->dev_base)
  893. iounmap(host->dev_base);
  894. /* free mg_host */
  895. kfree(host);
  896. return err;
  897. }
  898. static struct platform_driver mg_disk_driver = {
  899. .probe = mg_probe,
  900. .remove = mg_remove,
  901. .driver = {
  902. .name = MG_DEV_NAME,
  903. .pm = &mg_pm,
  904. }
  905. };
  906. /****************************************************************************
  907. *
  908. * Module stuff
  909. *
  910. ****************************************************************************/
  911. static int __init mg_init(void)
  912. {
  913. printk(KERN_INFO "mGine mflash driver, (c) 2008 mGine Co.\n");
  914. return platform_driver_register(&mg_disk_driver);
  915. }
  916. static void __exit mg_exit(void)
  917. {
  918. printk(KERN_INFO "mflash driver : bye bye\n");
  919. platform_driver_unregister(&mg_disk_driver);
  920. }
  921. module_init(mg_init);
  922. module_exit(mg_exit);
  923. MODULE_LICENSE("GPL");
  924. MODULE_AUTHOR("unsik Kim <donari75@gmail.com>");
  925. MODULE_DESCRIPTION("mGine m[g]flash device driver");