omap.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. /*
  2. * linux/drivers/mmc/host/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)
  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) {
  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 = -ETIMEDOUT;
  415. transfer_error = 1;
  416. }
  417. }
  418. if (status & OMAP_MMC_STAT_DATA_CRC) {
  419. if (host->data) {
  420. host->data->error = -EILSEQ;
  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 = -ETIMEDOUT;
  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 = -EILSEQ;
  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. dev_dbg(mmc_dev(host->mmc),
  458. "ignoring card status error (CMD%d)\n",
  459. host->cmd->opcode);
  460. end_command = 1;
  461. }
  462. /*
  463. * NOTE: On 1610 the END_OF_CMD may come too early when
  464. * starting a write
  465. */
  466. if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
  467. (!(status & OMAP_MMC_STAT_A_EMPTY))) {
  468. end_command = 1;
  469. }
  470. }
  471. if (end_command) {
  472. mmc_omap_cmd_done(host, host->cmd);
  473. }
  474. if (transfer_error)
  475. mmc_omap_xfer_done(host, host->data);
  476. else if (end_transfer)
  477. mmc_omap_end_of_data(host, host->data);
  478. return IRQ_HANDLED;
  479. }
  480. static irqreturn_t mmc_omap_switch_irq(int irq, void *dev_id)
  481. {
  482. struct mmc_omap_host *host = (struct mmc_omap_host *) dev_id;
  483. schedule_work(&host->switch_work);
  484. return IRQ_HANDLED;
  485. }
  486. static void mmc_omap_switch_timer(unsigned long arg)
  487. {
  488. struct mmc_omap_host *host = (struct mmc_omap_host *) arg;
  489. schedule_work(&host->switch_work);
  490. }
  491. static void mmc_omap_switch_handler(struct work_struct *work)
  492. {
  493. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host, switch_work);
  494. struct mmc_card *card;
  495. static int complained = 0;
  496. int cards = 0, cover_open;
  497. if (host->switch_pin == -1)
  498. return;
  499. cover_open = mmc_omap_cover_is_open(host);
  500. if (cover_open != host->switch_last_state) {
  501. kobject_uevent(&host->dev->kobj, KOBJ_CHANGE);
  502. host->switch_last_state = cover_open;
  503. }
  504. mmc_detect_change(host->mmc, 0);
  505. list_for_each_entry(card, &host->mmc->cards, node) {
  506. if (mmc_card_present(card))
  507. cards++;
  508. }
  509. if (mmc_omap_cover_is_open(host)) {
  510. if (!complained) {
  511. dev_info(mmc_dev(host->mmc), "cover is open\n");
  512. complained = 1;
  513. }
  514. if (mmc_omap_enable_poll)
  515. mod_timer(&host->switch_timer, jiffies +
  516. msecs_to_jiffies(OMAP_MMC_SWITCH_POLL_DELAY));
  517. } else {
  518. complained = 0;
  519. }
  520. }
  521. /* Prepare to transfer the next segment of a scatterlist */
  522. static void
  523. mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
  524. {
  525. int dma_ch = host->dma_ch;
  526. unsigned long data_addr;
  527. u16 buf, frame;
  528. u32 count;
  529. struct scatterlist *sg = &data->sg[host->sg_idx];
  530. int src_port = 0;
  531. int dst_port = 0;
  532. int sync_dev = 0;
  533. data_addr = host->phys_base + OMAP_MMC_REG_DATA;
  534. frame = data->blksz;
  535. count = sg_dma_len(sg);
  536. if ((data->blocks == 1) && (count > data->blksz))
  537. count = frame;
  538. host->dma_len = count;
  539. /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
  540. * Use 16 or 32 word frames when the blocksize is at least that large.
  541. * Blocksize is usually 512 bytes; but not for some SD reads.
  542. */
  543. if (cpu_is_omap15xx() && frame > 32)
  544. frame = 32;
  545. else if (frame > 64)
  546. frame = 64;
  547. count /= frame;
  548. frame >>= 1;
  549. if (!(data->flags & MMC_DATA_WRITE)) {
  550. buf = 0x800f | ((frame - 1) << 8);
  551. if (cpu_class_is_omap1()) {
  552. src_port = OMAP_DMA_PORT_TIPB;
  553. dst_port = OMAP_DMA_PORT_EMIFF;
  554. }
  555. if (cpu_is_omap24xx())
  556. sync_dev = OMAP24XX_DMA_MMC1_RX;
  557. omap_set_dma_src_params(dma_ch, src_port,
  558. OMAP_DMA_AMODE_CONSTANT,
  559. data_addr, 0, 0);
  560. omap_set_dma_dest_params(dma_ch, dst_port,
  561. OMAP_DMA_AMODE_POST_INC,
  562. sg_dma_address(sg), 0, 0);
  563. omap_set_dma_dest_data_pack(dma_ch, 1);
  564. omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
  565. } else {
  566. buf = 0x0f80 | ((frame - 1) << 0);
  567. if (cpu_class_is_omap1()) {
  568. src_port = OMAP_DMA_PORT_EMIFF;
  569. dst_port = OMAP_DMA_PORT_TIPB;
  570. }
  571. if (cpu_is_omap24xx())
  572. sync_dev = OMAP24XX_DMA_MMC1_TX;
  573. omap_set_dma_dest_params(dma_ch, dst_port,
  574. OMAP_DMA_AMODE_CONSTANT,
  575. data_addr, 0, 0);
  576. omap_set_dma_src_params(dma_ch, src_port,
  577. OMAP_DMA_AMODE_POST_INC,
  578. sg_dma_address(sg), 0, 0);
  579. omap_set_dma_src_data_pack(dma_ch, 1);
  580. omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
  581. }
  582. /* Max limit for DMA frame count is 0xffff */
  583. BUG_ON(count > 0xffff);
  584. OMAP_MMC_WRITE(host, BUF, buf);
  585. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
  586. frame, count, OMAP_DMA_SYNC_FRAME,
  587. sync_dev, 0);
  588. }
  589. /* A scatterlist segment completed */
  590. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  591. {
  592. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  593. struct mmc_data *mmcdat = host->data;
  594. if (unlikely(host->dma_ch < 0)) {
  595. dev_err(mmc_dev(host->mmc),
  596. "DMA callback while DMA not enabled\n");
  597. return;
  598. }
  599. /* FIXME: We really should do something to _handle_ the errors */
  600. if (ch_status & OMAP1_DMA_TOUT_IRQ) {
  601. dev_err(mmc_dev(host->mmc),"DMA timeout\n");
  602. return;
  603. }
  604. if (ch_status & OMAP_DMA_DROP_IRQ) {
  605. dev_err(mmc_dev(host->mmc), "DMA sync error\n");
  606. return;
  607. }
  608. if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
  609. return;
  610. }
  611. mmcdat->bytes_xfered += host->dma_len;
  612. host->sg_idx++;
  613. if (host->sg_idx < host->sg_len) {
  614. mmc_omap_prepare_dma(host, host->data);
  615. omap_start_dma(host->dma_ch);
  616. } else
  617. mmc_omap_dma_done(host, host->data);
  618. }
  619. static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
  620. {
  621. const char *dev_name;
  622. int sync_dev, dma_ch, is_read, r;
  623. is_read = !(data->flags & MMC_DATA_WRITE);
  624. del_timer_sync(&host->dma_timer);
  625. if (host->dma_ch >= 0) {
  626. if (is_read == host->dma_is_read)
  627. return 0;
  628. omap_free_dma(host->dma_ch);
  629. host->dma_ch = -1;
  630. }
  631. if (is_read) {
  632. if (host->id == 1) {
  633. sync_dev = OMAP_DMA_MMC_RX;
  634. dev_name = "MMC1 read";
  635. } else {
  636. sync_dev = OMAP_DMA_MMC2_RX;
  637. dev_name = "MMC2 read";
  638. }
  639. } else {
  640. if (host->id == 1) {
  641. sync_dev = OMAP_DMA_MMC_TX;
  642. dev_name = "MMC1 write";
  643. } else {
  644. sync_dev = OMAP_DMA_MMC2_TX;
  645. dev_name = "MMC2 write";
  646. }
  647. }
  648. r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
  649. host, &dma_ch);
  650. if (r != 0) {
  651. dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
  652. return r;
  653. }
  654. host->dma_ch = dma_ch;
  655. host->dma_is_read = is_read;
  656. return 0;
  657. }
  658. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  659. {
  660. u16 reg;
  661. reg = OMAP_MMC_READ(host, SDIO);
  662. reg &= ~(1 << 5);
  663. OMAP_MMC_WRITE(host, SDIO, reg);
  664. /* Set maximum timeout */
  665. OMAP_MMC_WRITE(host, CTO, 0xff);
  666. }
  667. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  668. {
  669. int timeout;
  670. u16 reg;
  671. /* Convert ns to clock cycles by assuming 20MHz frequency
  672. * 1 cycle at 20MHz = 500 ns
  673. */
  674. timeout = req->data->timeout_clks + req->data->timeout_ns / 500;
  675. /* Check if we need to use timeout multiplier register */
  676. reg = OMAP_MMC_READ(host, SDIO);
  677. if (timeout > 0xffff) {
  678. reg |= (1 << 5);
  679. timeout /= 1024;
  680. } else
  681. reg &= ~(1 << 5);
  682. OMAP_MMC_WRITE(host, SDIO, reg);
  683. OMAP_MMC_WRITE(host, DTO, timeout);
  684. }
  685. static void
  686. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  687. {
  688. struct mmc_data *data = req->data;
  689. int i, use_dma, block_size;
  690. unsigned sg_len;
  691. host->data = data;
  692. if (data == NULL) {
  693. OMAP_MMC_WRITE(host, BLEN, 0);
  694. OMAP_MMC_WRITE(host, NBLK, 0);
  695. OMAP_MMC_WRITE(host, BUF, 0);
  696. host->dma_in_use = 0;
  697. set_cmd_timeout(host, req);
  698. return;
  699. }
  700. block_size = data->blksz;
  701. OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
  702. OMAP_MMC_WRITE(host, BLEN, block_size - 1);
  703. set_data_timeout(host, req);
  704. /* cope with calling layer confusion; it issues "single
  705. * block" writes using multi-block scatterlists.
  706. */
  707. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  708. /* Only do DMA for entire blocks */
  709. use_dma = host->use_dma;
  710. if (use_dma) {
  711. for (i = 0; i < sg_len; i++) {
  712. if ((data->sg[i].length % block_size) != 0) {
  713. use_dma = 0;
  714. break;
  715. }
  716. }
  717. }
  718. host->sg_idx = 0;
  719. if (use_dma) {
  720. if (mmc_omap_get_dma_channel(host, data) == 0) {
  721. enum dma_data_direction dma_data_dir;
  722. if (data->flags & MMC_DATA_WRITE)
  723. dma_data_dir = DMA_TO_DEVICE;
  724. else
  725. dma_data_dir = DMA_FROM_DEVICE;
  726. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  727. sg_len, dma_data_dir);
  728. host->total_bytes_left = 0;
  729. mmc_omap_prepare_dma(host, req->data);
  730. host->brs_received = 0;
  731. host->dma_done = 0;
  732. host->dma_in_use = 1;
  733. } else
  734. use_dma = 0;
  735. }
  736. /* Revert to PIO? */
  737. if (!use_dma) {
  738. OMAP_MMC_WRITE(host, BUF, 0x1f1f);
  739. host->total_bytes_left = data->blocks * block_size;
  740. host->sg_len = sg_len;
  741. mmc_omap_sg_to_buf(host);
  742. host->dma_in_use = 0;
  743. }
  744. }
  745. static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
  746. {
  747. struct mmc_omap_host *host = mmc_priv(mmc);
  748. WARN_ON(host->mrq != NULL);
  749. host->mrq = req;
  750. /* only touch fifo AFTER the controller readies it */
  751. mmc_omap_prepare_data(host, req);
  752. mmc_omap_start_command(host, req->cmd);
  753. if (host->dma_in_use)
  754. omap_start_dma(host->dma_ch);
  755. }
  756. static void innovator_fpga_socket_power(int on)
  757. {
  758. #if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP15XX)
  759. if (on) {
  760. fpga_write(fpga_read(OMAP1510_FPGA_POWER) | (1 << 3),
  761. OMAP1510_FPGA_POWER);
  762. } else {
  763. fpga_write(fpga_read(OMAP1510_FPGA_POWER) & ~(1 << 3),
  764. OMAP1510_FPGA_POWER);
  765. }
  766. #endif
  767. }
  768. /*
  769. * Turn the socket power on/off. Innovator uses FPGA, most boards
  770. * probably use GPIO.
  771. */
  772. static void mmc_omap_power(struct mmc_omap_host *host, int on)
  773. {
  774. if (on) {
  775. if (machine_is_omap_innovator())
  776. innovator_fpga_socket_power(1);
  777. else if (machine_is_omap_h2())
  778. tps65010_set_gpio_out_value(GPIO3, HIGH);
  779. else if (machine_is_omap_h3())
  780. /* GPIO 4 of TPS65010 sends SD_EN signal */
  781. tps65010_set_gpio_out_value(GPIO4, HIGH);
  782. else if (cpu_is_omap24xx()) {
  783. u16 reg = OMAP_MMC_READ(host, CON);
  784. OMAP_MMC_WRITE(host, CON, reg | (1 << 11));
  785. } else
  786. if (host->power_pin >= 0)
  787. omap_set_gpio_dataout(host->power_pin, 1);
  788. } else {
  789. if (machine_is_omap_innovator())
  790. innovator_fpga_socket_power(0);
  791. else if (machine_is_omap_h2())
  792. tps65010_set_gpio_out_value(GPIO3, LOW);
  793. else if (machine_is_omap_h3())
  794. tps65010_set_gpio_out_value(GPIO4, LOW);
  795. else if (cpu_is_omap24xx()) {
  796. u16 reg = OMAP_MMC_READ(host, CON);
  797. OMAP_MMC_WRITE(host, CON, reg & ~(1 << 11));
  798. } else
  799. if (host->power_pin >= 0)
  800. omap_set_gpio_dataout(host->power_pin, 0);
  801. }
  802. }
  803. static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
  804. {
  805. struct mmc_omap_host *host = mmc_priv(mmc);
  806. int func_clk_rate = clk_get_rate(host->fclk);
  807. int dsor;
  808. if (ios->clock == 0)
  809. return 0;
  810. dsor = func_clk_rate / ios->clock;
  811. if (dsor < 1)
  812. dsor = 1;
  813. if (func_clk_rate / dsor > ios->clock)
  814. dsor++;
  815. if (dsor > 250)
  816. dsor = 250;
  817. dsor++;
  818. if (ios->bus_width == MMC_BUS_WIDTH_4)
  819. dsor |= 1 << 15;
  820. return dsor;
  821. }
  822. static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  823. {
  824. struct mmc_omap_host *host = mmc_priv(mmc);
  825. int dsor;
  826. int i;
  827. dsor = mmc_omap_calc_divisor(mmc, ios);
  828. host->bus_mode = ios->bus_mode;
  829. host->hw_bus_mode = host->bus_mode;
  830. switch (ios->power_mode) {
  831. case MMC_POWER_OFF:
  832. mmc_omap_power(host, 0);
  833. break;
  834. case MMC_POWER_UP:
  835. /* Cannot touch dsor yet, just power up MMC */
  836. mmc_omap_power(host, 1);
  837. return;
  838. case MMC_POWER_ON:
  839. dsor |= 1 << 11;
  840. break;
  841. }
  842. clk_enable(host->fclk);
  843. /* On insanely high arm_per frequencies something sometimes
  844. * goes somehow out of sync, and the POW bit is not being set,
  845. * which results in the while loop below getting stuck.
  846. * Writing to the CON register twice seems to do the trick. */
  847. for (i = 0; i < 2; i++)
  848. OMAP_MMC_WRITE(host, CON, dsor);
  849. if (ios->power_mode == MMC_POWER_ON) {
  850. /* Send clock cycles, poll completion */
  851. OMAP_MMC_WRITE(host, IE, 0);
  852. OMAP_MMC_WRITE(host, STAT, 0xffff);
  853. OMAP_MMC_WRITE(host, CMD, 1 << 7);
  854. while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
  855. OMAP_MMC_WRITE(host, STAT, 1);
  856. }
  857. clk_disable(host->fclk);
  858. }
  859. static int mmc_omap_get_ro(struct mmc_host *mmc)
  860. {
  861. struct mmc_omap_host *host = mmc_priv(mmc);
  862. return host->wp_pin && omap_get_gpio_datain(host->wp_pin);
  863. }
  864. static const struct mmc_host_ops mmc_omap_ops = {
  865. .request = mmc_omap_request,
  866. .set_ios = mmc_omap_set_ios,
  867. .get_ro = mmc_omap_get_ro,
  868. };
  869. static int __init mmc_omap_probe(struct platform_device *pdev)
  870. {
  871. struct omap_mmc_conf *minfo = pdev->dev.platform_data;
  872. struct mmc_host *mmc;
  873. struct mmc_omap_host *host = NULL;
  874. struct resource *res;
  875. int ret = 0;
  876. int irq;
  877. if (minfo == NULL) {
  878. dev_err(&pdev->dev, "platform data missing\n");
  879. return -ENXIO;
  880. }
  881. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  882. irq = platform_get_irq(pdev, 0);
  883. if (res == NULL || irq < 0)
  884. return -ENXIO;
  885. res = request_mem_region(res->start, res->end - res->start + 1,
  886. pdev->name);
  887. if (res == NULL)
  888. return -EBUSY;
  889. mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
  890. if (mmc == NULL) {
  891. ret = -ENOMEM;
  892. goto err_free_mem_region;
  893. }
  894. host = mmc_priv(mmc);
  895. host->mmc = mmc;
  896. spin_lock_init(&host->dma_lock);
  897. init_timer(&host->dma_timer);
  898. host->dma_timer.function = mmc_omap_dma_timer;
  899. host->dma_timer.data = (unsigned long) host;
  900. host->id = pdev->id;
  901. host->mem_res = res;
  902. host->irq = irq;
  903. if (cpu_is_omap24xx()) {
  904. host->iclk = clk_get(&pdev->dev, "mmc_ick");
  905. if (IS_ERR(host->iclk))
  906. goto err_free_mmc_host;
  907. clk_enable(host->iclk);
  908. }
  909. if (!cpu_is_omap24xx())
  910. host->fclk = clk_get(&pdev->dev, "mmc_ck");
  911. else
  912. host->fclk = clk_get(&pdev->dev, "mmc_fck");
  913. if (IS_ERR(host->fclk)) {
  914. ret = PTR_ERR(host->fclk);
  915. goto err_free_iclk;
  916. }
  917. /* REVISIT:
  918. * Also, use minfo->cover to decide how to manage
  919. * the card detect sensing.
  920. */
  921. host->power_pin = minfo->power_pin;
  922. host->switch_pin = minfo->switch_pin;
  923. host->wp_pin = minfo->wp_pin;
  924. host->use_dma = 1;
  925. host->dma_ch = -1;
  926. host->irq = irq;
  927. host->phys_base = host->mem_res->start;
  928. host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
  929. mmc->ops = &mmc_omap_ops;
  930. mmc->f_min = 400000;
  931. mmc->f_max = 24000000;
  932. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  933. mmc->caps = MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
  934. if (minfo->wire4)
  935. mmc->caps |= MMC_CAP_4_BIT_DATA;
  936. /* Use scatterlist DMA to reduce per-transfer costs.
  937. * NOTE max_seg_size assumption that small blocks aren't
  938. * normally used (except e.g. for reading SD registers).
  939. */
  940. mmc->max_phys_segs = 32;
  941. mmc->max_hw_segs = 32;
  942. mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
  943. mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
  944. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  945. mmc->max_seg_size = mmc->max_req_size;
  946. if (host->power_pin >= 0) {
  947. if ((ret = omap_request_gpio(host->power_pin)) != 0) {
  948. dev_err(mmc_dev(host->mmc),
  949. "Unable to get GPIO pin for MMC power\n");
  950. goto err_free_fclk;
  951. }
  952. omap_set_gpio_direction(host->power_pin, 0);
  953. }
  954. ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
  955. if (ret)
  956. goto err_free_power_gpio;
  957. host->dev = &pdev->dev;
  958. platform_set_drvdata(pdev, host);
  959. if (host->switch_pin >= 0) {
  960. INIT_WORK(&host->switch_work, mmc_omap_switch_handler);
  961. init_timer(&host->switch_timer);
  962. host->switch_timer.function = mmc_omap_switch_timer;
  963. host->switch_timer.data = (unsigned long) host;
  964. if (omap_request_gpio(host->switch_pin) != 0) {
  965. dev_warn(mmc_dev(host->mmc), "Unable to get GPIO pin for MMC cover switch\n");
  966. host->switch_pin = -1;
  967. goto no_switch;
  968. }
  969. omap_set_gpio_direction(host->switch_pin, 1);
  970. ret = request_irq(OMAP_GPIO_IRQ(host->switch_pin),
  971. mmc_omap_switch_irq, IRQF_TRIGGER_RISING, DRIVER_NAME, host);
  972. if (ret) {
  973. dev_warn(mmc_dev(host->mmc), "Unable to get IRQ for MMC cover switch\n");
  974. omap_free_gpio(host->switch_pin);
  975. host->switch_pin = -1;
  976. goto no_switch;
  977. }
  978. ret = device_create_file(&pdev->dev, &dev_attr_cover_switch);
  979. if (ret == 0) {
  980. ret = device_create_file(&pdev->dev, &dev_attr_enable_poll);
  981. if (ret != 0)
  982. device_remove_file(&pdev->dev, &dev_attr_cover_switch);
  983. }
  984. if (ret) {
  985. dev_warn(mmc_dev(host->mmc), "Unable to create sysfs attributes\n");
  986. free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
  987. omap_free_gpio(host->switch_pin);
  988. host->switch_pin = -1;
  989. goto no_switch;
  990. }
  991. if (mmc_omap_enable_poll && mmc_omap_cover_is_open(host))
  992. schedule_work(&host->switch_work);
  993. }
  994. mmc_add_host(mmc);
  995. return 0;
  996. no_switch:
  997. /* FIXME: Free other resources too. */
  998. if (host) {
  999. if (host->iclk && !IS_ERR(host->iclk))
  1000. clk_put(host->iclk);
  1001. if (host->fclk && !IS_ERR(host->fclk))
  1002. clk_put(host->fclk);
  1003. mmc_free_host(host->mmc);
  1004. }
  1005. err_free_power_gpio:
  1006. if (host->power_pin >= 0)
  1007. omap_free_gpio(host->power_pin);
  1008. err_free_fclk:
  1009. clk_put(host->fclk);
  1010. err_free_iclk:
  1011. if (host->iclk != NULL) {
  1012. clk_disable(host->iclk);
  1013. clk_put(host->iclk);
  1014. }
  1015. err_free_mmc_host:
  1016. mmc_free_host(host->mmc);
  1017. err_free_mem_region:
  1018. release_mem_region(res->start, res->end - res->start + 1);
  1019. return ret;
  1020. }
  1021. static int mmc_omap_remove(struct platform_device *pdev)
  1022. {
  1023. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1024. platform_set_drvdata(pdev, NULL);
  1025. BUG_ON(host == NULL);
  1026. mmc_remove_host(host->mmc);
  1027. free_irq(host->irq, host);
  1028. if (host->power_pin >= 0)
  1029. omap_free_gpio(host->power_pin);
  1030. if (host->switch_pin >= 0) {
  1031. device_remove_file(&pdev->dev, &dev_attr_enable_poll);
  1032. device_remove_file(&pdev->dev, &dev_attr_cover_switch);
  1033. free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
  1034. omap_free_gpio(host->switch_pin);
  1035. host->switch_pin = -1;
  1036. del_timer_sync(&host->switch_timer);
  1037. flush_scheduled_work();
  1038. }
  1039. if (host->iclk && !IS_ERR(host->iclk))
  1040. clk_put(host->iclk);
  1041. if (host->fclk && !IS_ERR(host->fclk))
  1042. clk_put(host->fclk);
  1043. release_mem_region(pdev->resource[0].start,
  1044. pdev->resource[0].end - pdev->resource[0].start + 1);
  1045. mmc_free_host(host->mmc);
  1046. return 0;
  1047. }
  1048. #ifdef CONFIG_PM
  1049. static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
  1050. {
  1051. int ret = 0;
  1052. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1053. if (host && host->suspended)
  1054. return 0;
  1055. if (host) {
  1056. ret = mmc_suspend_host(host->mmc, mesg);
  1057. if (ret == 0)
  1058. host->suspended = 1;
  1059. }
  1060. return ret;
  1061. }
  1062. static int mmc_omap_resume(struct platform_device *pdev)
  1063. {
  1064. int ret = 0;
  1065. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1066. if (host && !host->suspended)
  1067. return 0;
  1068. if (host) {
  1069. ret = mmc_resume_host(host->mmc);
  1070. if (ret == 0)
  1071. host->suspended = 0;
  1072. }
  1073. return ret;
  1074. }
  1075. #else
  1076. #define mmc_omap_suspend NULL
  1077. #define mmc_omap_resume NULL
  1078. #endif
  1079. static struct platform_driver mmc_omap_driver = {
  1080. .probe = mmc_omap_probe,
  1081. .remove = mmc_omap_remove,
  1082. .suspend = mmc_omap_suspend,
  1083. .resume = mmc_omap_resume,
  1084. .driver = {
  1085. .name = DRIVER_NAME,
  1086. },
  1087. };
  1088. static int __init mmc_omap_init(void)
  1089. {
  1090. return platform_driver_register(&mmc_omap_driver);
  1091. }
  1092. static void __exit mmc_omap_exit(void)
  1093. {
  1094. platform_driver_unregister(&mmc_omap_driver);
  1095. }
  1096. module_init(mmc_omap_init);
  1097. module_exit(mmc_omap_exit);
  1098. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1099. MODULE_LICENSE("GPL");
  1100. MODULE_ALIAS(DRIVER_NAME);
  1101. MODULE_AUTHOR("Juha Yrjölä");