mg_disk.c 26 KB

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