omap.c 31 KB

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