mg_disk.c 26 KB

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