mg_disk.c 26 KB

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