omap.c 31 KB

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