omap.c 31 KB

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