mg_disk.c 26 KB

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