omap.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. /*
  2. * linux/drivers/media/mmc/omap.c
  3. *
  4. * Copyright (C) 2004 Nokia Corporation
  5. * Written by Tuukka Tikkanen and Juha Yrjölä<juha.yrjola@nokia.com>
  6. * Misc hacks here and there by Tony Lindgren <tony@atomide.com>
  7. * Other hacks (DMA, SD, etc) by David Brownell
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/ioport.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/delay.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/timer.h>
  23. #include <linux/mmc/host.h>
  24. #include <linux/mmc/card.h>
  25. #include <linux/clk.h>
  26. #include <asm/io.h>
  27. #include <asm/irq.h>
  28. #include <asm/scatterlist.h>
  29. #include <asm/mach-types.h>
  30. #include <asm/arch/board.h>
  31. #include <asm/arch/gpio.h>
  32. #include <asm/arch/dma.h>
  33. #include <asm/arch/mux.h>
  34. #include <asm/arch/fpga.h>
  35. #include <asm/arch/tps65010.h>
  36. #define OMAP_MMC_REG_CMD 0x00
  37. #define OMAP_MMC_REG_ARGL 0x04
  38. #define OMAP_MMC_REG_ARGH 0x08
  39. #define OMAP_MMC_REG_CON 0x0c
  40. #define OMAP_MMC_REG_STAT 0x10
  41. #define OMAP_MMC_REG_IE 0x14
  42. #define OMAP_MMC_REG_CTO 0x18
  43. #define OMAP_MMC_REG_DTO 0x1c
  44. #define OMAP_MMC_REG_DATA 0x20
  45. #define OMAP_MMC_REG_BLEN 0x24
  46. #define OMAP_MMC_REG_NBLK 0x28
  47. #define OMAP_MMC_REG_BUF 0x2c
  48. #define OMAP_MMC_REG_SDIO 0x34
  49. #define OMAP_MMC_REG_REV 0x3c
  50. #define OMAP_MMC_REG_RSP0 0x40
  51. #define OMAP_MMC_REG_RSP1 0x44
  52. #define OMAP_MMC_REG_RSP2 0x48
  53. #define OMAP_MMC_REG_RSP3 0x4c
  54. #define OMAP_MMC_REG_RSP4 0x50
  55. #define OMAP_MMC_REG_RSP5 0x54
  56. #define OMAP_MMC_REG_RSP6 0x58
  57. #define OMAP_MMC_REG_RSP7 0x5c
  58. #define OMAP_MMC_REG_IOSR 0x60
  59. #define OMAP_MMC_REG_SYSC 0x64
  60. #define OMAP_MMC_REG_SYSS 0x68
  61. #define OMAP_MMC_STAT_CARD_ERR (1 << 14)
  62. #define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
  63. #define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
  64. #define OMAP_MMC_STAT_A_EMPTY (1 << 11)
  65. #define OMAP_MMC_STAT_A_FULL (1 << 10)
  66. #define OMAP_MMC_STAT_CMD_CRC (1 << 8)
  67. #define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
  68. #define OMAP_MMC_STAT_DATA_CRC (1 << 6)
  69. #define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
  70. #define OMAP_MMC_STAT_END_BUSY (1 << 4)
  71. #define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
  72. #define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
  73. #define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
  74. #define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG_##reg)
  75. #define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG_##reg)
  76. /*
  77. * Command types
  78. */
  79. #define OMAP_MMC_CMDTYPE_BC 0
  80. #define OMAP_MMC_CMDTYPE_BCR 1
  81. #define OMAP_MMC_CMDTYPE_AC 2
  82. #define OMAP_MMC_CMDTYPE_ADTC 3
  83. #define DRIVER_NAME "mmci-omap"
  84. /* Specifies how often in millisecs to poll for card status changes
  85. * when the cover switch is open */
  86. #define OMAP_MMC_SWITCH_POLL_DELAY 500
  87. static int mmc_omap_enable_poll = 1;
  88. struct mmc_omap_host {
  89. int initialized;
  90. int suspended;
  91. struct mmc_request * mrq;
  92. struct mmc_command * cmd;
  93. struct mmc_data * data;
  94. struct mmc_host * mmc;
  95. struct device * dev;
  96. unsigned char id; /* 16xx chips have 2 MMC blocks */
  97. struct clk * iclk;
  98. struct clk * fclk;
  99. struct resource *mem_res;
  100. void __iomem *virt_base;
  101. unsigned int phys_base;
  102. int irq;
  103. unsigned char bus_mode;
  104. unsigned char hw_bus_mode;
  105. unsigned int sg_len;
  106. int sg_idx;
  107. u16 * buffer;
  108. u32 buffer_bytes_left;
  109. u32 total_bytes_left;
  110. unsigned use_dma:1;
  111. unsigned brs_received:1, dma_done:1;
  112. unsigned dma_is_read:1;
  113. unsigned dma_in_use:1;
  114. int dma_ch;
  115. spinlock_t dma_lock;
  116. struct timer_list dma_timer;
  117. unsigned dma_len;
  118. short power_pin;
  119. short wp_pin;
  120. int switch_pin;
  121. struct work_struct switch_work;
  122. struct timer_list switch_timer;
  123. int switch_last_state;
  124. };
  125. static inline int
  126. mmc_omap_cover_is_open(struct mmc_omap_host *host)
  127. {
  128. if (host->switch_pin < 0)
  129. return 0;
  130. return omap_get_gpio_datain(host->switch_pin);
  131. }
  132. static ssize_t
  133. mmc_omap_show_cover_switch(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. struct mmc_omap_host *host = dev_get_drvdata(dev);
  137. return sprintf(buf, "%s\n", mmc_omap_cover_is_open(host) ? "open" :
  138. "closed");
  139. }
  140. static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
  141. static ssize_t
  142. mmc_omap_show_enable_poll(struct device *dev,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. return snprintf(buf, PAGE_SIZE, "%d\n", mmc_omap_enable_poll);
  146. }
  147. static ssize_t
  148. mmc_omap_store_enable_poll(struct device *dev,
  149. struct device_attribute *attr, const char *buf,
  150. size_t size)
  151. {
  152. int enable_poll;
  153. if (sscanf(buf, "%10d", &enable_poll) != 1)
  154. return -EINVAL;
  155. if (enable_poll != mmc_omap_enable_poll) {
  156. struct mmc_omap_host *host = dev_get_drvdata(dev);
  157. mmc_omap_enable_poll = enable_poll;
  158. if (enable_poll && host->switch_pin >= 0)
  159. schedule_work(&host->switch_work);
  160. }
  161. return size;
  162. }
  163. static DEVICE_ATTR(enable_poll, 0664,
  164. mmc_omap_show_enable_poll, mmc_omap_store_enable_poll);
  165. static void
  166. mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
  167. {
  168. u32 cmdreg;
  169. u32 resptype;
  170. u32 cmdtype;
  171. host->cmd = cmd;
  172. resptype = 0;
  173. cmdtype = 0;
  174. /* Our hardware needs to know exact type */
  175. switch (mmc_resp_type(cmd)) {
  176. case MMC_RSP_NONE:
  177. break;
  178. case MMC_RSP_R1:
  179. case MMC_RSP_R1B:
  180. /* resp 1, 1b, 6, 7 */
  181. resptype = 1;
  182. break;
  183. case MMC_RSP_R2:
  184. resptype = 2;
  185. break;
  186. case MMC_RSP_R3:
  187. resptype = 3;
  188. break;
  189. default:
  190. dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
  191. break;
  192. }
  193. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
  194. cmdtype = OMAP_MMC_CMDTYPE_ADTC;
  195. } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
  196. cmdtype = OMAP_MMC_CMDTYPE_BC;
  197. } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
  198. cmdtype = OMAP_MMC_CMDTYPE_BCR;
  199. } else {
  200. cmdtype = OMAP_MMC_CMDTYPE_AC;
  201. }
  202. cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
  203. if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
  204. cmdreg |= 1 << 6;
  205. if (cmd->flags & MMC_RSP_BUSY)
  206. cmdreg |= 1 << 11;
  207. if (host->data && !(host->data->flags & MMC_DATA_WRITE))
  208. cmdreg |= 1 << 15;
  209. clk_enable(host->fclk);
  210. OMAP_MMC_WRITE(host, CTO, 200);
  211. OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
  212. OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
  213. OMAP_MMC_WRITE(host, IE,
  214. OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
  215. OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
  216. OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
  217. OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
  218. OMAP_MMC_STAT_END_OF_DATA);
  219. OMAP_MMC_WRITE(host, CMD, cmdreg);
  220. }
  221. static void
  222. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  223. {
  224. if (host->dma_in_use) {
  225. enum dma_data_direction dma_data_dir;
  226. BUG_ON(host->dma_ch < 0);
  227. if (data->error != MMC_ERR_NONE)
  228. omap_stop_dma(host->dma_ch);
  229. /* Release DMA channel lazily */
  230. mod_timer(&host->dma_timer, jiffies + HZ);
  231. if (data->flags & MMC_DATA_WRITE)
  232. dma_data_dir = DMA_TO_DEVICE;
  233. else
  234. dma_data_dir = DMA_FROM_DEVICE;
  235. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
  236. dma_data_dir);
  237. }
  238. host->data = NULL;
  239. host->sg_len = 0;
  240. clk_disable(host->fclk);
  241. /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
  242. * dozens of requests until the card finishes writing data.
  243. * It'd be cheaper to just wait till an EOFB interrupt arrives...
  244. */
  245. if (!data->stop) {
  246. host->mrq = NULL;
  247. mmc_request_done(host->mmc, data->mrq);
  248. return;
  249. }
  250. mmc_omap_start_command(host, data->stop);
  251. }
  252. static void
  253. mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
  254. {
  255. unsigned long flags;
  256. int done;
  257. if (!host->dma_in_use) {
  258. mmc_omap_xfer_done(host, data);
  259. return;
  260. }
  261. done = 0;
  262. spin_lock_irqsave(&host->dma_lock, flags);
  263. if (host->dma_done)
  264. done = 1;
  265. else
  266. host->brs_received = 1;
  267. spin_unlock_irqrestore(&host->dma_lock, flags);
  268. if (done)
  269. mmc_omap_xfer_done(host, data);
  270. }
  271. static void
  272. mmc_omap_dma_timer(unsigned long data)
  273. {
  274. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  275. BUG_ON(host->dma_ch < 0);
  276. omap_free_dma(host->dma_ch);
  277. host->dma_ch = -1;
  278. }
  279. static void
  280. mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
  281. {
  282. unsigned long flags;
  283. int done;
  284. done = 0;
  285. spin_lock_irqsave(&host->dma_lock, flags);
  286. if (host->brs_received)
  287. done = 1;
  288. else
  289. host->dma_done = 1;
  290. spin_unlock_irqrestore(&host->dma_lock, flags);
  291. if (done)
  292. mmc_omap_xfer_done(host, data);
  293. }
  294. static void
  295. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  296. {
  297. host->cmd = NULL;
  298. if (cmd->flags & MMC_RSP_PRESENT) {
  299. if (cmd->flags & MMC_RSP_136) {
  300. /* response type 2 */
  301. cmd->resp[3] =
  302. OMAP_MMC_READ(host, RSP0) |
  303. (OMAP_MMC_READ(host, RSP1) << 16);
  304. cmd->resp[2] =
  305. OMAP_MMC_READ(host, RSP2) |
  306. (OMAP_MMC_READ(host, RSP3) << 16);
  307. cmd->resp[1] =
  308. OMAP_MMC_READ(host, RSP4) |
  309. (OMAP_MMC_READ(host, RSP5) << 16);
  310. cmd->resp[0] =
  311. OMAP_MMC_READ(host, RSP6) |
  312. (OMAP_MMC_READ(host, RSP7) << 16);
  313. } else {
  314. /* response types 1, 1b, 3, 4, 5, 6 */
  315. cmd->resp[0] =
  316. OMAP_MMC_READ(host, RSP6) |
  317. (OMAP_MMC_READ(host, RSP7) << 16);
  318. }
  319. }
  320. if (host->data == NULL || cmd->error != MMC_ERR_NONE) {
  321. host->mrq = NULL;
  322. clk_disable(host->fclk);
  323. mmc_request_done(host->mmc, cmd->mrq);
  324. }
  325. }
  326. /* PIO only */
  327. static void
  328. mmc_omap_sg_to_buf(struct mmc_omap_host *host)
  329. {
  330. struct scatterlist *sg;
  331. sg = host->data->sg + host->sg_idx;
  332. host->buffer_bytes_left = sg->length;
  333. host->buffer = page_address(sg->page) + sg->offset;
  334. if (host->buffer_bytes_left > host->total_bytes_left)
  335. host->buffer_bytes_left = host->total_bytes_left;
  336. }
  337. /* PIO only */
  338. static void
  339. mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
  340. {
  341. int n;
  342. if (host->buffer_bytes_left == 0) {
  343. host->sg_idx++;
  344. BUG_ON(host->sg_idx == host->sg_len);
  345. mmc_omap_sg_to_buf(host);
  346. }
  347. n = 64;
  348. if (n > host->buffer_bytes_left)
  349. n = host->buffer_bytes_left;
  350. host->buffer_bytes_left -= n;
  351. host->total_bytes_left -= n;
  352. host->data->bytes_xfered += n;
  353. if (write) {
  354. __raw_writesw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
  355. } else {
  356. __raw_readsw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
  357. }
  358. }
  359. static inline void mmc_omap_report_irq(u16 status)
  360. {
  361. static const char *mmc_omap_status_bits[] = {
  362. "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
  363. "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
  364. };
  365. int i, c = 0;
  366. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  367. if (status & (1 << i)) {
  368. if (c)
  369. printk(" ");
  370. printk("%s", mmc_omap_status_bits[i]);
  371. c++;
  372. }
  373. }
  374. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  375. {
  376. struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
  377. u16 status;
  378. int end_command;
  379. int end_transfer;
  380. int transfer_error;
  381. if (host->cmd == NULL && host->data == NULL) {
  382. status = OMAP_MMC_READ(host, STAT);
  383. dev_info(mmc_dev(host->mmc),"spurious irq 0x%04x\n", status);
  384. if (status != 0) {
  385. OMAP_MMC_WRITE(host, STAT, status);
  386. OMAP_MMC_WRITE(host, IE, 0);
  387. }
  388. return IRQ_HANDLED;
  389. }
  390. end_command = 0;
  391. end_transfer = 0;
  392. transfer_error = 0;
  393. while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
  394. OMAP_MMC_WRITE(host, STAT, status);
  395. #ifdef CONFIG_MMC_DEBUG
  396. dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
  397. status, host->cmd != NULL ? host->cmd->opcode : -1);
  398. mmc_omap_report_irq(status);
  399. printk("\n");
  400. #endif
  401. if (host->total_bytes_left) {
  402. if ((status & OMAP_MMC_STAT_A_FULL) ||
  403. (status & OMAP_MMC_STAT_END_OF_DATA))
  404. mmc_omap_xfer_data(host, 0);
  405. if (status & OMAP_MMC_STAT_A_EMPTY)
  406. mmc_omap_xfer_data(host, 1);
  407. }
  408. if (status & OMAP_MMC_STAT_END_OF_DATA) {
  409. end_transfer = 1;
  410. }
  411. if (status & OMAP_MMC_STAT_DATA_TOUT) {
  412. dev_dbg(mmc_dev(host->mmc), "data timeout\n");
  413. if (host->data) {
  414. host->data->error |= MMC_ERR_TIMEOUT;
  415. transfer_error = 1;
  416. }
  417. }
  418. if (status & OMAP_MMC_STAT_DATA_CRC) {
  419. if (host->data) {
  420. host->data->error |= MMC_ERR_BADCRC;
  421. dev_dbg(mmc_dev(host->mmc),
  422. "data CRC error, bytes left %d\n",
  423. host->total_bytes_left);
  424. transfer_error = 1;
  425. } else {
  426. dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
  427. }
  428. }
  429. if (status & OMAP_MMC_STAT_CMD_TOUT) {
  430. /* Timeouts are routine with some commands */
  431. if (host->cmd) {
  432. if (host->cmd->opcode != MMC_ALL_SEND_CID &&
  433. host->cmd->opcode !=
  434. MMC_SEND_OP_COND &&
  435. host->cmd->opcode !=
  436. MMC_APP_CMD &&
  437. !mmc_omap_cover_is_open(host))
  438. dev_err(mmc_dev(host->mmc),
  439. "command timeout, CMD %d\n",
  440. host->cmd->opcode);
  441. host->cmd->error = MMC_ERR_TIMEOUT;
  442. end_command = 1;
  443. }
  444. }
  445. if (status & OMAP_MMC_STAT_CMD_CRC) {
  446. if (host->cmd) {
  447. dev_err(mmc_dev(host->mmc),
  448. "command CRC error (CMD%d, arg 0x%08x)\n",
  449. host->cmd->opcode, host->cmd->arg);
  450. host->cmd->error = MMC_ERR_BADCRC;
  451. end_command = 1;
  452. } else
  453. dev_err(mmc_dev(host->mmc),
  454. "command CRC error without cmd?\n");
  455. }
  456. if (status & OMAP_MMC_STAT_CARD_ERR) {
  457. if (host->cmd && host->cmd->opcode == MMC_STOP_TRANSMISSION) {
  458. u32 response = OMAP_MMC_READ(host, RSP6)
  459. | (OMAP_MMC_READ(host, RSP7) << 16);
  460. /* STOP sometimes sets must-ignore bits */
  461. if (!(response & (R1_CC_ERROR
  462. | R1_ILLEGAL_COMMAND
  463. | R1_COM_CRC_ERROR))) {
  464. end_command = 1;
  465. continue;
  466. }
  467. }
  468. dev_dbg(mmc_dev(host->mmc), "card status error (CMD%d)\n",
  469. host->cmd->opcode);
  470. if (host->cmd) {
  471. host->cmd->error = MMC_ERR_FAILED;
  472. end_command = 1;
  473. }
  474. if (host->data) {
  475. host->data->error = MMC_ERR_FAILED;
  476. transfer_error = 1;
  477. }
  478. }
  479. /*
  480. * NOTE: On 1610 the END_OF_CMD may come too early when
  481. * starting a write
  482. */
  483. if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
  484. (!(status & OMAP_MMC_STAT_A_EMPTY))) {
  485. end_command = 1;
  486. }
  487. }
  488. if (end_command) {
  489. mmc_omap_cmd_done(host, host->cmd);
  490. }
  491. if (transfer_error)
  492. mmc_omap_xfer_done(host, host->data);
  493. else if (end_transfer)
  494. mmc_omap_end_of_data(host, host->data);
  495. return IRQ_HANDLED;
  496. }
  497. static irqreturn_t mmc_omap_switch_irq(int irq, void *dev_id)
  498. {
  499. struct mmc_omap_host *host = (struct mmc_omap_host *) dev_id;
  500. schedule_work(&host->switch_work);
  501. return IRQ_HANDLED;
  502. }
  503. static void mmc_omap_switch_timer(unsigned long arg)
  504. {
  505. struct mmc_omap_host *host = (struct mmc_omap_host *) arg;
  506. schedule_work(&host->switch_work);
  507. }
  508. static void mmc_omap_switch_handler(struct work_struct *work)
  509. {
  510. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host, switch_work);
  511. struct mmc_card *card;
  512. static int complained = 0;
  513. int cards = 0, cover_open;
  514. if (host->switch_pin == -1)
  515. return;
  516. cover_open = mmc_omap_cover_is_open(host);
  517. if (cover_open != host->switch_last_state) {
  518. kobject_uevent(&host->dev->kobj, KOBJ_CHANGE);
  519. host->switch_last_state = cover_open;
  520. }
  521. mmc_detect_change(host->mmc, 0);
  522. list_for_each_entry(card, &host->mmc->cards, node) {
  523. if (mmc_card_present(card))
  524. cards++;
  525. }
  526. if (mmc_omap_cover_is_open(host)) {
  527. if (!complained) {
  528. dev_info(mmc_dev(host->mmc), "cover is open\n");
  529. complained = 1;
  530. }
  531. if (mmc_omap_enable_poll)
  532. mod_timer(&host->switch_timer, jiffies +
  533. msecs_to_jiffies(OMAP_MMC_SWITCH_POLL_DELAY));
  534. } else {
  535. complained = 0;
  536. }
  537. }
  538. /* Prepare to transfer the next segment of a scatterlist */
  539. static void
  540. mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
  541. {
  542. int dma_ch = host->dma_ch;
  543. unsigned long data_addr;
  544. u16 buf, frame;
  545. u32 count;
  546. struct scatterlist *sg = &data->sg[host->sg_idx];
  547. int src_port = 0;
  548. int dst_port = 0;
  549. int sync_dev = 0;
  550. data_addr = host->phys_base + OMAP_MMC_REG_DATA;
  551. frame = data->blksz;
  552. count = sg_dma_len(sg);
  553. if ((data->blocks == 1) && (count > data->blksz))
  554. count = frame;
  555. host->dma_len = count;
  556. /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
  557. * Use 16 or 32 word frames when the blocksize is at least that large.
  558. * Blocksize is usually 512 bytes; but not for some SD reads.
  559. */
  560. if (cpu_is_omap15xx() && frame > 32)
  561. frame = 32;
  562. else if (frame > 64)
  563. frame = 64;
  564. count /= frame;
  565. frame >>= 1;
  566. if (!(data->flags & MMC_DATA_WRITE)) {
  567. buf = 0x800f | ((frame - 1) << 8);
  568. if (cpu_class_is_omap1()) {
  569. src_port = OMAP_DMA_PORT_TIPB;
  570. dst_port = OMAP_DMA_PORT_EMIFF;
  571. }
  572. if (cpu_is_omap24xx())
  573. sync_dev = OMAP24XX_DMA_MMC1_RX;
  574. omap_set_dma_src_params(dma_ch, src_port,
  575. OMAP_DMA_AMODE_CONSTANT,
  576. data_addr, 0, 0);
  577. omap_set_dma_dest_params(dma_ch, dst_port,
  578. OMAP_DMA_AMODE_POST_INC,
  579. sg_dma_address(sg), 0, 0);
  580. omap_set_dma_dest_data_pack(dma_ch, 1);
  581. omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
  582. } else {
  583. buf = 0x0f80 | ((frame - 1) << 0);
  584. if (cpu_class_is_omap1()) {
  585. src_port = OMAP_DMA_PORT_EMIFF;
  586. dst_port = OMAP_DMA_PORT_TIPB;
  587. }
  588. if (cpu_is_omap24xx())
  589. sync_dev = OMAP24XX_DMA_MMC1_TX;
  590. omap_set_dma_dest_params(dma_ch, dst_port,
  591. OMAP_DMA_AMODE_CONSTANT,
  592. data_addr, 0, 0);
  593. omap_set_dma_src_params(dma_ch, src_port,
  594. OMAP_DMA_AMODE_POST_INC,
  595. sg_dma_address(sg), 0, 0);
  596. omap_set_dma_src_data_pack(dma_ch, 1);
  597. omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
  598. }
  599. /* Max limit for DMA frame count is 0xffff */
  600. BUG_ON(count > 0xffff);
  601. OMAP_MMC_WRITE(host, BUF, buf);
  602. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
  603. frame, count, OMAP_DMA_SYNC_FRAME,
  604. sync_dev, 0);
  605. }
  606. /* A scatterlist segment completed */
  607. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  608. {
  609. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  610. struct mmc_data *mmcdat = host->data;
  611. if (unlikely(host->dma_ch < 0)) {
  612. dev_err(mmc_dev(host->mmc),
  613. "DMA callback while DMA not enabled\n");
  614. return;
  615. }
  616. /* FIXME: We really should do something to _handle_ the errors */
  617. if (ch_status & OMAP1_DMA_TOUT_IRQ) {
  618. dev_err(mmc_dev(host->mmc),"DMA timeout\n");
  619. return;
  620. }
  621. if (ch_status & OMAP_DMA_DROP_IRQ) {
  622. dev_err(mmc_dev(host->mmc), "DMA sync error\n");
  623. return;
  624. }
  625. if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
  626. return;
  627. }
  628. mmcdat->bytes_xfered += host->dma_len;
  629. host->sg_idx++;
  630. if (host->sg_idx < host->sg_len) {
  631. mmc_omap_prepare_dma(host, host->data);
  632. omap_start_dma(host->dma_ch);
  633. } else
  634. mmc_omap_dma_done(host, host->data);
  635. }
  636. static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
  637. {
  638. const char *dev_name;
  639. int sync_dev, dma_ch, is_read, r;
  640. is_read = !(data->flags & MMC_DATA_WRITE);
  641. del_timer_sync(&host->dma_timer);
  642. if (host->dma_ch >= 0) {
  643. if (is_read == host->dma_is_read)
  644. return 0;
  645. omap_free_dma(host->dma_ch);
  646. host->dma_ch = -1;
  647. }
  648. if (is_read) {
  649. if (host->id == 1) {
  650. sync_dev = OMAP_DMA_MMC_RX;
  651. dev_name = "MMC1 read";
  652. } else {
  653. sync_dev = OMAP_DMA_MMC2_RX;
  654. dev_name = "MMC2 read";
  655. }
  656. } else {
  657. if (host->id == 1) {
  658. sync_dev = OMAP_DMA_MMC_TX;
  659. dev_name = "MMC1 write";
  660. } else {
  661. sync_dev = OMAP_DMA_MMC2_TX;
  662. dev_name = "MMC2 write";
  663. }
  664. }
  665. r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
  666. host, &dma_ch);
  667. if (r != 0) {
  668. dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
  669. return r;
  670. }
  671. host->dma_ch = dma_ch;
  672. host->dma_is_read = is_read;
  673. return 0;
  674. }
  675. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  676. {
  677. u16 reg;
  678. reg = OMAP_MMC_READ(host, SDIO);
  679. reg &= ~(1 << 5);
  680. OMAP_MMC_WRITE(host, SDIO, reg);
  681. /* Set maximum timeout */
  682. OMAP_MMC_WRITE(host, CTO, 0xff);
  683. }
  684. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  685. {
  686. int timeout;
  687. u16 reg;
  688. /* Convert ns to clock cycles by assuming 20MHz frequency
  689. * 1 cycle at 20MHz = 500 ns
  690. */
  691. timeout = req->data->timeout_clks + req->data->timeout_ns / 500;
  692. /* Check if we need to use timeout multiplier register */
  693. reg = OMAP_MMC_READ(host, SDIO);
  694. if (timeout > 0xffff) {
  695. reg |= (1 << 5);
  696. timeout /= 1024;
  697. } else
  698. reg &= ~(1 << 5);
  699. OMAP_MMC_WRITE(host, SDIO, reg);
  700. OMAP_MMC_WRITE(host, DTO, timeout);
  701. }
  702. static void
  703. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  704. {
  705. struct mmc_data *data = req->data;
  706. int i, use_dma, block_size;
  707. unsigned sg_len;
  708. host->data = data;
  709. if (data == NULL) {
  710. OMAP_MMC_WRITE(host, BLEN, 0);
  711. OMAP_MMC_WRITE(host, NBLK, 0);
  712. OMAP_MMC_WRITE(host, BUF, 0);
  713. host->dma_in_use = 0;
  714. set_cmd_timeout(host, req);
  715. return;
  716. }
  717. block_size = data->blksz;
  718. OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
  719. OMAP_MMC_WRITE(host, BLEN, block_size - 1);
  720. set_data_timeout(host, req);
  721. /* cope with calling layer confusion; it issues "single
  722. * block" writes using multi-block scatterlists.
  723. */
  724. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  725. /* Only do DMA for entire blocks */
  726. use_dma = host->use_dma;
  727. if (use_dma) {
  728. for (i = 0; i < sg_len; i++) {
  729. if ((data->sg[i].length % block_size) != 0) {
  730. use_dma = 0;
  731. break;
  732. }
  733. }
  734. }
  735. host->sg_idx = 0;
  736. if (use_dma) {
  737. if (mmc_omap_get_dma_channel(host, data) == 0) {
  738. enum dma_data_direction dma_data_dir;
  739. if (data->flags & MMC_DATA_WRITE)
  740. dma_data_dir = DMA_TO_DEVICE;
  741. else
  742. dma_data_dir = DMA_FROM_DEVICE;
  743. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  744. sg_len, dma_data_dir);
  745. host->total_bytes_left = 0;
  746. mmc_omap_prepare_dma(host, req->data);
  747. host->brs_received = 0;
  748. host->dma_done = 0;
  749. host->dma_in_use = 1;
  750. } else
  751. use_dma = 0;
  752. }
  753. /* Revert to PIO? */
  754. if (!use_dma) {
  755. OMAP_MMC_WRITE(host, BUF, 0x1f1f);
  756. host->total_bytes_left = data->blocks * block_size;
  757. host->sg_len = sg_len;
  758. mmc_omap_sg_to_buf(host);
  759. host->dma_in_use = 0;
  760. }
  761. }
  762. static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
  763. {
  764. struct mmc_omap_host *host = mmc_priv(mmc);
  765. WARN_ON(host->mrq != NULL);
  766. host->mrq = req;
  767. /* only touch fifo AFTER the controller readies it */
  768. mmc_omap_prepare_data(host, req);
  769. mmc_omap_start_command(host, req->cmd);
  770. if (host->dma_in_use)
  771. omap_start_dma(host->dma_ch);
  772. }
  773. static void innovator_fpga_socket_power(int on)
  774. {
  775. #if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP15XX)
  776. if (on) {
  777. fpga_write(fpga_read(OMAP1510_FPGA_POWER) | (1 << 3),
  778. OMAP1510_FPGA_POWER);
  779. } else {
  780. fpga_write(fpga_read(OMAP1510_FPGA_POWER) & ~(1 << 3),
  781. OMAP1510_FPGA_POWER);
  782. }
  783. #endif
  784. }
  785. /*
  786. * Turn the socket power on/off. Innovator uses FPGA, most boards
  787. * probably use GPIO.
  788. */
  789. static void mmc_omap_power(struct mmc_omap_host *host, int on)
  790. {
  791. if (on) {
  792. if (machine_is_omap_innovator())
  793. innovator_fpga_socket_power(1);
  794. else if (machine_is_omap_h2())
  795. tps65010_set_gpio_out_value(GPIO3, HIGH);
  796. else if (machine_is_omap_h3())
  797. /* GPIO 4 of TPS65010 sends SD_EN signal */
  798. tps65010_set_gpio_out_value(GPIO4, HIGH);
  799. else if (cpu_is_omap24xx()) {
  800. u16 reg = OMAP_MMC_READ(host, CON);
  801. OMAP_MMC_WRITE(host, CON, reg | (1 << 11));
  802. } else
  803. if (host->power_pin >= 0)
  804. omap_set_gpio_dataout(host->power_pin, 1);
  805. } else {
  806. if (machine_is_omap_innovator())
  807. innovator_fpga_socket_power(0);
  808. else if (machine_is_omap_h2())
  809. tps65010_set_gpio_out_value(GPIO3, LOW);
  810. else if (machine_is_omap_h3())
  811. tps65010_set_gpio_out_value(GPIO4, LOW);
  812. else if (cpu_is_omap24xx()) {
  813. u16 reg = OMAP_MMC_READ(host, CON);
  814. OMAP_MMC_WRITE(host, CON, reg & ~(1 << 11));
  815. } else
  816. if (host->power_pin >= 0)
  817. omap_set_gpio_dataout(host->power_pin, 0);
  818. }
  819. }
  820. static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
  821. {
  822. struct mmc_omap_host *host = mmc_priv(mmc);
  823. int func_clk_rate = clk_get_rate(host->fclk);
  824. int dsor;
  825. if (ios->clock == 0)
  826. return 0;
  827. dsor = func_clk_rate / ios->clock;
  828. if (dsor < 1)
  829. dsor = 1;
  830. if (func_clk_rate / dsor > ios->clock)
  831. dsor++;
  832. if (dsor > 250)
  833. dsor = 250;
  834. dsor++;
  835. if (ios->bus_width == MMC_BUS_WIDTH_4)
  836. dsor |= 1 << 15;
  837. return dsor;
  838. }
  839. static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  840. {
  841. struct mmc_omap_host *host = mmc_priv(mmc);
  842. int dsor;
  843. int i;
  844. dsor = mmc_omap_calc_divisor(mmc, ios);
  845. host->bus_mode = ios->bus_mode;
  846. host->hw_bus_mode = host->bus_mode;
  847. switch (ios->power_mode) {
  848. case MMC_POWER_OFF:
  849. mmc_omap_power(host, 0);
  850. break;
  851. case MMC_POWER_UP:
  852. /* Cannot touch dsor yet, just power up MMC */
  853. mmc_omap_power(host, 1);
  854. return;
  855. case MMC_POWER_ON:
  856. dsor |= 1 << 11;
  857. break;
  858. }
  859. clk_enable(host->fclk);
  860. /* On insanely high arm_per frequencies something sometimes
  861. * goes somehow out of sync, and the POW bit is not being set,
  862. * which results in the while loop below getting stuck.
  863. * Writing to the CON register twice seems to do the trick. */
  864. for (i = 0; i < 2; i++)
  865. OMAP_MMC_WRITE(host, CON, dsor);
  866. if (ios->power_mode == MMC_POWER_ON) {
  867. /* Send clock cycles, poll completion */
  868. OMAP_MMC_WRITE(host, IE, 0);
  869. OMAP_MMC_WRITE(host, STAT, 0xffff);
  870. OMAP_MMC_WRITE(host, CMD, 1 << 7);
  871. while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
  872. OMAP_MMC_WRITE(host, STAT, 1);
  873. }
  874. clk_disable(host->fclk);
  875. }
  876. static int mmc_omap_get_ro(struct mmc_host *mmc)
  877. {
  878. struct mmc_omap_host *host = mmc_priv(mmc);
  879. return host->wp_pin && omap_get_gpio_datain(host->wp_pin);
  880. }
  881. static const struct mmc_host_ops mmc_omap_ops = {
  882. .request = mmc_omap_request,
  883. .set_ios = mmc_omap_set_ios,
  884. .get_ro = mmc_omap_get_ro,
  885. };
  886. static int __init mmc_omap_probe(struct platform_device *pdev)
  887. {
  888. struct omap_mmc_conf *minfo = pdev->dev.platform_data;
  889. struct mmc_host *mmc;
  890. struct mmc_omap_host *host = NULL;
  891. struct resource *res;
  892. int ret = 0;
  893. int irq;
  894. if (minfo == NULL) {
  895. dev_err(&pdev->dev, "platform data missing\n");
  896. return -ENXIO;
  897. }
  898. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  899. irq = platform_get_irq(pdev, 0);
  900. if (res == NULL || irq < 0)
  901. return -ENXIO;
  902. res = request_mem_region(res->start, res->end - res->start + 1,
  903. pdev->name);
  904. if (res == NULL)
  905. return -EBUSY;
  906. mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
  907. if (mmc == NULL) {
  908. ret = -ENOMEM;
  909. goto err_free_mem_region;
  910. }
  911. host = mmc_priv(mmc);
  912. host->mmc = mmc;
  913. spin_lock_init(&host->dma_lock);
  914. init_timer(&host->dma_timer);
  915. host->dma_timer.function = mmc_omap_dma_timer;
  916. host->dma_timer.data = (unsigned long) host;
  917. host->id = pdev->id;
  918. host->mem_res = res;
  919. host->irq = irq;
  920. if (cpu_is_omap24xx()) {
  921. host->iclk = clk_get(&pdev->dev, "mmc_ick");
  922. if (IS_ERR(host->iclk))
  923. goto err_free_mmc_host;
  924. clk_enable(host->iclk);
  925. }
  926. if (!cpu_is_omap24xx())
  927. host->fclk = clk_get(&pdev->dev, "mmc_ck");
  928. else
  929. host->fclk = clk_get(&pdev->dev, "mmc_fck");
  930. if (IS_ERR(host->fclk)) {
  931. ret = PTR_ERR(host->fclk);
  932. goto err_free_iclk;
  933. }
  934. /* REVISIT:
  935. * Also, use minfo->cover to decide how to manage
  936. * the card detect sensing.
  937. */
  938. host->power_pin = minfo->power_pin;
  939. host->switch_pin = minfo->switch_pin;
  940. host->wp_pin = minfo->wp_pin;
  941. host->use_dma = 1;
  942. host->dma_ch = -1;
  943. host->irq = irq;
  944. host->phys_base = host->mem_res->start;
  945. host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
  946. mmc->ops = &mmc_omap_ops;
  947. mmc->f_min = 400000;
  948. mmc->f_max = 24000000;
  949. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  950. mmc->caps = MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
  951. if (minfo->wire4)
  952. mmc->caps |= MMC_CAP_4_BIT_DATA;
  953. /* Use scatterlist DMA to reduce per-transfer costs.
  954. * NOTE max_seg_size assumption that small blocks aren't
  955. * normally used (except e.g. for reading SD registers).
  956. */
  957. mmc->max_phys_segs = 32;
  958. mmc->max_hw_segs = 32;
  959. mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
  960. mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
  961. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  962. mmc->max_seg_size = mmc->max_req_size;
  963. if (host->power_pin >= 0) {
  964. if ((ret = omap_request_gpio(host->power_pin)) != 0) {
  965. dev_err(mmc_dev(host->mmc),
  966. "Unable to get GPIO pin for MMC power\n");
  967. goto err_free_fclk;
  968. }
  969. omap_set_gpio_direction(host->power_pin, 0);
  970. }
  971. ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
  972. if (ret)
  973. goto err_free_power_gpio;
  974. host->dev = &pdev->dev;
  975. platform_set_drvdata(pdev, host);
  976. if (host->switch_pin >= 0) {
  977. INIT_WORK(&host->switch_work, mmc_omap_switch_handler);
  978. init_timer(&host->switch_timer);
  979. host->switch_timer.function = mmc_omap_switch_timer;
  980. host->switch_timer.data = (unsigned long) host;
  981. if (omap_request_gpio(host->switch_pin) != 0) {
  982. dev_warn(mmc_dev(host->mmc), "Unable to get GPIO pin for MMC cover switch\n");
  983. host->switch_pin = -1;
  984. goto no_switch;
  985. }
  986. omap_set_gpio_direction(host->switch_pin, 1);
  987. ret = request_irq(OMAP_GPIO_IRQ(host->switch_pin),
  988. mmc_omap_switch_irq, IRQF_TRIGGER_RISING, DRIVER_NAME, host);
  989. if (ret) {
  990. dev_warn(mmc_dev(host->mmc), "Unable to get IRQ for MMC cover switch\n");
  991. omap_free_gpio(host->switch_pin);
  992. host->switch_pin = -1;
  993. goto no_switch;
  994. }
  995. ret = device_create_file(&pdev->dev, &dev_attr_cover_switch);
  996. if (ret == 0) {
  997. ret = device_create_file(&pdev->dev, &dev_attr_enable_poll);
  998. if (ret != 0)
  999. device_remove_file(&pdev->dev, &dev_attr_cover_switch);
  1000. }
  1001. if (ret) {
  1002. dev_warn(mmc_dev(host->mmc), "Unable to create sysfs attributes\n");
  1003. free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
  1004. omap_free_gpio(host->switch_pin);
  1005. host->switch_pin = -1;
  1006. goto no_switch;
  1007. }
  1008. if (mmc_omap_enable_poll && mmc_omap_cover_is_open(host))
  1009. schedule_work(&host->switch_work);
  1010. }
  1011. mmc_add_host(mmc);
  1012. return 0;
  1013. no_switch:
  1014. /* FIXME: Free other resources too. */
  1015. if (host) {
  1016. if (host->iclk && !IS_ERR(host->iclk))
  1017. clk_put(host->iclk);
  1018. if (host->fclk && !IS_ERR(host->fclk))
  1019. clk_put(host->fclk);
  1020. mmc_free_host(host->mmc);
  1021. }
  1022. err_free_power_gpio:
  1023. if (host->power_pin >= 0)
  1024. omap_free_gpio(host->power_pin);
  1025. err_free_fclk:
  1026. clk_put(host->fclk);
  1027. err_free_iclk:
  1028. if (host->iclk != NULL) {
  1029. clk_disable(host->iclk);
  1030. clk_put(host->iclk);
  1031. }
  1032. err_free_mmc_host:
  1033. mmc_free_host(host->mmc);
  1034. err_free_mem_region:
  1035. release_mem_region(res->start, res->end - res->start + 1);
  1036. return ret;
  1037. }
  1038. static int mmc_omap_remove(struct platform_device *pdev)
  1039. {
  1040. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1041. platform_set_drvdata(pdev, NULL);
  1042. BUG_ON(host == NULL);
  1043. mmc_remove_host(host->mmc);
  1044. free_irq(host->irq, host);
  1045. if (host->power_pin >= 0)
  1046. omap_free_gpio(host->power_pin);
  1047. if (host->switch_pin >= 0) {
  1048. device_remove_file(&pdev->dev, &dev_attr_enable_poll);
  1049. device_remove_file(&pdev->dev, &dev_attr_cover_switch);
  1050. free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
  1051. omap_free_gpio(host->switch_pin);
  1052. host->switch_pin = -1;
  1053. del_timer_sync(&host->switch_timer);
  1054. flush_scheduled_work();
  1055. }
  1056. if (host->iclk && !IS_ERR(host->iclk))
  1057. clk_put(host->iclk);
  1058. if (host->fclk && !IS_ERR(host->fclk))
  1059. clk_put(host->fclk);
  1060. release_mem_region(pdev->resource[0].start,
  1061. pdev->resource[0].end - pdev->resource[0].start + 1);
  1062. mmc_free_host(host->mmc);
  1063. return 0;
  1064. }
  1065. #ifdef CONFIG_PM
  1066. static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
  1067. {
  1068. int ret = 0;
  1069. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1070. if (host && host->suspended)
  1071. return 0;
  1072. if (host) {
  1073. ret = mmc_suspend_host(host->mmc, mesg);
  1074. if (ret == 0)
  1075. host->suspended = 1;
  1076. }
  1077. return ret;
  1078. }
  1079. static int mmc_omap_resume(struct platform_device *pdev)
  1080. {
  1081. int ret = 0;
  1082. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1083. if (host && !host->suspended)
  1084. return 0;
  1085. if (host) {
  1086. ret = mmc_resume_host(host->mmc);
  1087. if (ret == 0)
  1088. host->suspended = 0;
  1089. }
  1090. return ret;
  1091. }
  1092. #else
  1093. #define mmc_omap_suspend NULL
  1094. #define mmc_omap_resume NULL
  1095. #endif
  1096. static struct platform_driver mmc_omap_driver = {
  1097. .probe = mmc_omap_probe,
  1098. .remove = mmc_omap_remove,
  1099. .suspend = mmc_omap_suspend,
  1100. .resume = mmc_omap_resume,
  1101. .driver = {
  1102. .name = DRIVER_NAME,
  1103. },
  1104. };
  1105. static int __init mmc_omap_init(void)
  1106. {
  1107. return platform_driver_register(&mmc_omap_driver);
  1108. }
  1109. static void __exit mmc_omap_exit(void)
  1110. {
  1111. platform_driver_unregister(&mmc_omap_driver);
  1112. }
  1113. module_init(mmc_omap_init);
  1114. module_exit(mmc_omap_exit);
  1115. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1116. MODULE_LICENSE("GPL");
  1117. MODULE_ALIAS(DRIVER_NAME);
  1118. MODULE_AUTHOR("Juha Yrjölä");