omap.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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(struct work_struct *work)
  507. {
  508. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host, switch_work);
  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. BUG_ON(count > 0xffff);
  599. OMAP_MMC_WRITE(host, BUF, buf);
  600. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
  601. frame, count, OMAP_DMA_SYNC_FRAME,
  602. sync_dev, 0);
  603. }
  604. /* A scatterlist segment completed */
  605. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  606. {
  607. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  608. struct mmc_data *mmcdat = host->data;
  609. if (unlikely(host->dma_ch < 0)) {
  610. dev_err(mmc_dev(host->mmc),
  611. "DMA callback while DMA not enabled\n");
  612. return;
  613. }
  614. /* FIXME: We really should do something to _handle_ the errors */
  615. if (ch_status & OMAP1_DMA_TOUT_IRQ) {
  616. dev_err(mmc_dev(host->mmc),"DMA timeout\n");
  617. return;
  618. }
  619. if (ch_status & OMAP_DMA_DROP_IRQ) {
  620. dev_err(mmc_dev(host->mmc), "DMA sync error\n");
  621. return;
  622. }
  623. if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
  624. return;
  625. }
  626. mmcdat->bytes_xfered += host->dma_len;
  627. host->sg_idx++;
  628. if (host->sg_idx < host->sg_len) {
  629. mmc_omap_prepare_dma(host, host->data);
  630. omap_start_dma(host->dma_ch);
  631. } else
  632. mmc_omap_dma_done(host, host->data);
  633. }
  634. static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
  635. {
  636. const char *dev_name;
  637. int sync_dev, dma_ch, is_read, r;
  638. is_read = !(data->flags & MMC_DATA_WRITE);
  639. del_timer_sync(&host->dma_timer);
  640. if (host->dma_ch >= 0) {
  641. if (is_read == host->dma_is_read)
  642. return 0;
  643. omap_free_dma(host->dma_ch);
  644. host->dma_ch = -1;
  645. }
  646. if (is_read) {
  647. if (host->id == 1) {
  648. sync_dev = OMAP_DMA_MMC_RX;
  649. dev_name = "MMC1 read";
  650. } else {
  651. sync_dev = OMAP_DMA_MMC2_RX;
  652. dev_name = "MMC2 read";
  653. }
  654. } else {
  655. if (host->id == 1) {
  656. sync_dev = OMAP_DMA_MMC_TX;
  657. dev_name = "MMC1 write";
  658. } else {
  659. sync_dev = OMAP_DMA_MMC2_TX;
  660. dev_name = "MMC2 write";
  661. }
  662. }
  663. r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
  664. host, &dma_ch);
  665. if (r != 0) {
  666. dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
  667. return r;
  668. }
  669. host->dma_ch = dma_ch;
  670. host->dma_is_read = is_read;
  671. return 0;
  672. }
  673. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  674. {
  675. u16 reg;
  676. reg = OMAP_MMC_READ(host, SDIO);
  677. reg &= ~(1 << 5);
  678. OMAP_MMC_WRITE(host, SDIO, reg);
  679. /* Set maximum timeout */
  680. OMAP_MMC_WRITE(host, CTO, 0xff);
  681. }
  682. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  683. {
  684. int timeout;
  685. u16 reg;
  686. /* Convert ns to clock cycles by assuming 20MHz frequency
  687. * 1 cycle at 20MHz = 500 ns
  688. */
  689. timeout = req->data->timeout_clks + req->data->timeout_ns / 500;
  690. /* Check if we need to use timeout multiplier register */
  691. reg = OMAP_MMC_READ(host, SDIO);
  692. if (timeout > 0xffff) {
  693. reg |= (1 << 5);
  694. timeout /= 1024;
  695. } else
  696. reg &= ~(1 << 5);
  697. OMAP_MMC_WRITE(host, SDIO, reg);
  698. OMAP_MMC_WRITE(host, DTO, timeout);
  699. }
  700. static void
  701. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  702. {
  703. struct mmc_data *data = req->data;
  704. int i, use_dma, block_size;
  705. unsigned sg_len;
  706. host->data = data;
  707. if (data == NULL) {
  708. OMAP_MMC_WRITE(host, BLEN, 0);
  709. OMAP_MMC_WRITE(host, NBLK, 0);
  710. OMAP_MMC_WRITE(host, BUF, 0);
  711. host->dma_in_use = 0;
  712. set_cmd_timeout(host, req);
  713. return;
  714. }
  715. block_size = data->blksz;
  716. OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
  717. OMAP_MMC_WRITE(host, BLEN, block_size - 1);
  718. set_data_timeout(host, req);
  719. /* cope with calling layer confusion; it issues "single
  720. * block" writes using multi-block scatterlists.
  721. */
  722. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  723. /* Only do DMA for entire blocks */
  724. use_dma = host->use_dma;
  725. if (use_dma) {
  726. for (i = 0; i < sg_len; i++) {
  727. if ((data->sg[i].length % block_size) != 0) {
  728. use_dma = 0;
  729. break;
  730. }
  731. }
  732. }
  733. host->sg_idx = 0;
  734. if (use_dma) {
  735. if (mmc_omap_get_dma_channel(host, data) == 0) {
  736. enum dma_data_direction dma_data_dir;
  737. if (data->flags & MMC_DATA_WRITE)
  738. dma_data_dir = DMA_TO_DEVICE;
  739. else
  740. dma_data_dir = DMA_FROM_DEVICE;
  741. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  742. sg_len, dma_data_dir);
  743. host->total_bytes_left = 0;
  744. mmc_omap_prepare_dma(host, req->data);
  745. host->brs_received = 0;
  746. host->dma_done = 0;
  747. host->dma_in_use = 1;
  748. } else
  749. use_dma = 0;
  750. }
  751. /* Revert to PIO? */
  752. if (!use_dma) {
  753. OMAP_MMC_WRITE(host, BUF, 0x1f1f);
  754. host->total_bytes_left = data->blocks * block_size;
  755. host->sg_len = sg_len;
  756. mmc_omap_sg_to_buf(host);
  757. host->dma_in_use = 0;
  758. }
  759. }
  760. static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
  761. {
  762. struct mmc_omap_host *host = mmc_priv(mmc);
  763. WARN_ON(host->mrq != NULL);
  764. host->mrq = req;
  765. /* only touch fifo AFTER the controller readies it */
  766. mmc_omap_prepare_data(host, req);
  767. mmc_omap_start_command(host, req->cmd);
  768. if (host->dma_in_use)
  769. omap_start_dma(host->dma_ch);
  770. }
  771. static void innovator_fpga_socket_power(int on)
  772. {
  773. #if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP15XX)
  774. if (on) {
  775. fpga_write(fpga_read(OMAP1510_FPGA_POWER) | (1 << 3),
  776. OMAP1510_FPGA_POWER);
  777. } else {
  778. fpga_write(fpga_read(OMAP1510_FPGA_POWER) & ~(1 << 3),
  779. OMAP1510_FPGA_POWER);
  780. }
  781. #endif
  782. }
  783. /*
  784. * Turn the socket power on/off. Innovator uses FPGA, most boards
  785. * probably use GPIO.
  786. */
  787. static void mmc_omap_power(struct mmc_omap_host *host, int on)
  788. {
  789. if (on) {
  790. if (machine_is_omap_innovator())
  791. innovator_fpga_socket_power(1);
  792. else if (machine_is_omap_h2())
  793. tps65010_set_gpio_out_value(GPIO3, HIGH);
  794. else if (machine_is_omap_h3())
  795. /* GPIO 4 of TPS65010 sends SD_EN signal */
  796. tps65010_set_gpio_out_value(GPIO4, HIGH);
  797. else if (cpu_is_omap24xx()) {
  798. u16 reg = OMAP_MMC_READ(host, CON);
  799. OMAP_MMC_WRITE(host, CON, reg | (1 << 11));
  800. } else
  801. if (host->power_pin >= 0)
  802. omap_set_gpio_dataout(host->power_pin, 1);
  803. } else {
  804. if (machine_is_omap_innovator())
  805. innovator_fpga_socket_power(0);
  806. else if (machine_is_omap_h2())
  807. tps65010_set_gpio_out_value(GPIO3, LOW);
  808. else if (machine_is_omap_h3())
  809. tps65010_set_gpio_out_value(GPIO4, LOW);
  810. else if (cpu_is_omap24xx()) {
  811. u16 reg = OMAP_MMC_READ(host, CON);
  812. OMAP_MMC_WRITE(host, CON, reg & ~(1 << 11));
  813. } else
  814. if (host->power_pin >= 0)
  815. omap_set_gpio_dataout(host->power_pin, 0);
  816. }
  817. }
  818. static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  819. {
  820. struct mmc_omap_host *host = mmc_priv(mmc);
  821. int dsor;
  822. int realclock, i;
  823. realclock = ios->clock;
  824. if (ios->clock == 0)
  825. dsor = 0;
  826. else {
  827. int func_clk_rate = clk_get_rate(host->fclk);
  828. dsor = func_clk_rate / realclock;
  829. if (dsor < 1)
  830. dsor = 1;
  831. if (func_clk_rate / dsor > realclock)
  832. dsor++;
  833. if (dsor > 250)
  834. dsor = 250;
  835. dsor++;
  836. if (ios->bus_width == MMC_BUS_WIDTH_4)
  837. dsor |= 1 << 15;
  838. }
  839. switch (ios->power_mode) {
  840. case MMC_POWER_OFF:
  841. mmc_omap_power(host, 0);
  842. break;
  843. case MMC_POWER_UP:
  844. case MMC_POWER_ON:
  845. mmc_omap_power(host, 1);
  846. dsor |= 1 << 11;
  847. break;
  848. }
  849. host->bus_mode = ios->bus_mode;
  850. host->hw_bus_mode = host->bus_mode;
  851. clk_enable(host->fclk);
  852. /* On insanely high arm_per frequencies something sometimes
  853. * goes somehow out of sync, and the POW bit is not being set,
  854. * which results in the while loop below getting stuck.
  855. * Writing to the CON register twice seems to do the trick. */
  856. for (i = 0; i < 2; i++)
  857. OMAP_MMC_WRITE(host, CON, dsor);
  858. if (ios->power_mode == MMC_POWER_UP) {
  859. /* Send clock cycles, poll completion */
  860. OMAP_MMC_WRITE(host, IE, 0);
  861. OMAP_MMC_WRITE(host, STAT, 0xffff);
  862. OMAP_MMC_WRITE(host, CMD, 1 << 7);
  863. while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
  864. OMAP_MMC_WRITE(host, STAT, 1);
  865. }
  866. clk_disable(host->fclk);
  867. }
  868. static int mmc_omap_get_ro(struct mmc_host *mmc)
  869. {
  870. struct mmc_omap_host *host = mmc_priv(mmc);
  871. return host->wp_pin && omap_get_gpio_datain(host->wp_pin);
  872. }
  873. static const struct mmc_host_ops mmc_omap_ops = {
  874. .request = mmc_omap_request,
  875. .set_ios = mmc_omap_set_ios,
  876. .get_ro = mmc_omap_get_ro,
  877. };
  878. static int __init mmc_omap_probe(struct platform_device *pdev)
  879. {
  880. struct omap_mmc_conf *minfo = pdev->dev.platform_data;
  881. struct mmc_host *mmc;
  882. struct mmc_omap_host *host = NULL;
  883. struct resource *res;
  884. int ret = 0;
  885. int irq;
  886. if (minfo == NULL) {
  887. dev_err(&pdev->dev, "platform data missing\n");
  888. return -ENXIO;
  889. }
  890. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  891. irq = platform_get_irq(pdev, 0);
  892. if (res == NULL || irq < 0)
  893. return -ENXIO;
  894. res = request_mem_region(res->start, res->end - res->start + 1,
  895. pdev->name);
  896. if (res == NULL)
  897. return -EBUSY;
  898. mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
  899. if (mmc == NULL) {
  900. ret = -ENOMEM;
  901. goto err_free_mem_region;
  902. }
  903. host = mmc_priv(mmc);
  904. host->mmc = mmc;
  905. spin_lock_init(&host->dma_lock);
  906. init_timer(&host->dma_timer);
  907. host->dma_timer.function = mmc_omap_dma_timer;
  908. host->dma_timer.data = (unsigned long) host;
  909. host->id = pdev->id;
  910. host->mem_res = res;
  911. host->irq = irq;
  912. if (cpu_is_omap24xx()) {
  913. host->iclk = clk_get(&pdev->dev, "mmc_ick");
  914. if (IS_ERR(host->iclk))
  915. goto err_free_mmc_host;
  916. clk_enable(host->iclk);
  917. }
  918. if (!cpu_is_omap24xx())
  919. host->fclk = clk_get(&pdev->dev, "mmc_ck");
  920. else
  921. host->fclk = clk_get(&pdev->dev, "mmc_fck");
  922. if (IS_ERR(host->fclk)) {
  923. ret = PTR_ERR(host->fclk);
  924. goto err_free_iclk;
  925. }
  926. /* REVISIT:
  927. * Also, use minfo->cover to decide how to manage
  928. * the card detect sensing.
  929. */
  930. host->power_pin = minfo->power_pin;
  931. host->switch_pin = minfo->switch_pin;
  932. host->wp_pin = minfo->wp_pin;
  933. host->use_dma = 1;
  934. host->dma_ch = -1;
  935. host->irq = irq;
  936. host->phys_base = host->mem_res->start;
  937. host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
  938. mmc->ops = &mmc_omap_ops;
  939. mmc->f_min = 400000;
  940. mmc->f_max = 24000000;
  941. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  942. mmc->caps = MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
  943. if (minfo->wire4)
  944. mmc->caps |= MMC_CAP_4_BIT_DATA;
  945. /* Use scatterlist DMA to reduce per-transfer costs.
  946. * NOTE max_seg_size assumption that small blocks aren't
  947. * normally used (except e.g. for reading SD registers).
  948. */
  949. mmc->max_phys_segs = 32;
  950. mmc->max_hw_segs = 32;
  951. mmc->max_sectors = 256; /* NBLK max 11-bits, OMAP also limited by DMA */
  952. mmc->max_seg_size = mmc->max_sectors * 512;
  953. if (host->power_pin >= 0) {
  954. if ((ret = omap_request_gpio(host->power_pin)) != 0) {
  955. dev_err(mmc_dev(host->mmc),
  956. "Unable to get GPIO pin for MMC power\n");
  957. goto err_free_fclk;
  958. }
  959. omap_set_gpio_direction(host->power_pin, 0);
  960. }
  961. ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
  962. if (ret)
  963. goto err_free_power_gpio;
  964. host->dev = &pdev->dev;
  965. platform_set_drvdata(pdev, host);
  966. if (host->switch_pin >= 0) {
  967. INIT_WORK(&host->switch_work, mmc_omap_switch_handler);
  968. init_timer(&host->switch_timer);
  969. host->switch_timer.function = mmc_omap_switch_timer;
  970. host->switch_timer.data = (unsigned long) host;
  971. if (omap_request_gpio(host->switch_pin) != 0) {
  972. dev_warn(mmc_dev(host->mmc), "Unable to get GPIO pin for MMC cover switch\n");
  973. host->switch_pin = -1;
  974. goto no_switch;
  975. }
  976. omap_set_gpio_direction(host->switch_pin, 1);
  977. ret = request_irq(OMAP_GPIO_IRQ(host->switch_pin),
  978. mmc_omap_switch_irq, IRQF_TRIGGER_RISING, DRIVER_NAME, host);
  979. if (ret) {
  980. dev_warn(mmc_dev(host->mmc), "Unable to get IRQ for MMC cover switch\n");
  981. omap_free_gpio(host->switch_pin);
  982. host->switch_pin = -1;
  983. goto no_switch;
  984. }
  985. ret = device_create_file(&pdev->dev, &dev_attr_cover_switch);
  986. if (ret == 0) {
  987. ret = device_create_file(&pdev->dev, &dev_attr_enable_poll);
  988. if (ret != 0)
  989. device_remove_file(&pdev->dev, &dev_attr_cover_switch);
  990. }
  991. if (ret) {
  992. dev_warn(mmc_dev(host->mmc), "Unable to create sysfs attributes\n");
  993. free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
  994. omap_free_gpio(host->switch_pin);
  995. host->switch_pin = -1;
  996. goto no_switch;
  997. }
  998. if (mmc_omap_enable_poll && mmc_omap_cover_is_open(host))
  999. schedule_work(&host->switch_work);
  1000. }
  1001. mmc_add_host(mmc);
  1002. return 0;
  1003. no_switch:
  1004. /* FIXME: Free other resources too. */
  1005. if (host) {
  1006. if (host->iclk && !IS_ERR(host->iclk))
  1007. clk_put(host->iclk);
  1008. if (host->fclk && !IS_ERR(host->fclk))
  1009. clk_put(host->fclk);
  1010. mmc_free_host(host->mmc);
  1011. }
  1012. err_free_power_gpio:
  1013. if (host->power_pin >= 0)
  1014. omap_free_gpio(host->power_pin);
  1015. err_free_fclk:
  1016. clk_put(host->fclk);
  1017. err_free_iclk:
  1018. if (host->iclk != NULL) {
  1019. clk_disable(host->iclk);
  1020. clk_put(host->iclk);
  1021. }
  1022. err_free_mmc_host:
  1023. mmc_free_host(host->mmc);
  1024. err_free_mem_region:
  1025. release_mem_region(res->start, res->end - res->start + 1);
  1026. return ret;
  1027. }
  1028. static int mmc_omap_remove(struct platform_device *pdev)
  1029. {
  1030. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1031. platform_set_drvdata(pdev, NULL);
  1032. BUG_ON(host == NULL);
  1033. mmc_remove_host(host->mmc);
  1034. free_irq(host->irq, host);
  1035. if (host->power_pin >= 0)
  1036. omap_free_gpio(host->power_pin);
  1037. if (host->switch_pin >= 0) {
  1038. device_remove_file(&pdev->dev, &dev_attr_enable_poll);
  1039. device_remove_file(&pdev->dev, &dev_attr_cover_switch);
  1040. free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
  1041. omap_free_gpio(host->switch_pin);
  1042. host->switch_pin = -1;
  1043. del_timer_sync(&host->switch_timer);
  1044. flush_scheduled_work();
  1045. }
  1046. if (host->iclk && !IS_ERR(host->iclk))
  1047. clk_put(host->iclk);
  1048. if (host->fclk && !IS_ERR(host->fclk))
  1049. clk_put(host->fclk);
  1050. release_mem_region(pdev->resource[0].start,
  1051. pdev->resource[0].end - pdev->resource[0].start + 1);
  1052. mmc_free_host(host->mmc);
  1053. return 0;
  1054. }
  1055. #ifdef CONFIG_PM
  1056. static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
  1057. {
  1058. int ret = 0;
  1059. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1060. if (host && host->suspended)
  1061. return 0;
  1062. if (host) {
  1063. ret = mmc_suspend_host(host->mmc, mesg);
  1064. if (ret == 0)
  1065. host->suspended = 1;
  1066. }
  1067. return ret;
  1068. }
  1069. static int mmc_omap_resume(struct platform_device *pdev)
  1070. {
  1071. int ret = 0;
  1072. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1073. if (host && !host->suspended)
  1074. return 0;
  1075. if (host) {
  1076. ret = mmc_resume_host(host->mmc);
  1077. if (ret == 0)
  1078. host->suspended = 0;
  1079. }
  1080. return ret;
  1081. }
  1082. #else
  1083. #define mmc_omap_suspend NULL
  1084. #define mmc_omap_resume NULL
  1085. #endif
  1086. static struct platform_driver mmc_omap_driver = {
  1087. .probe = mmc_omap_probe,
  1088. .remove = mmc_omap_remove,
  1089. .suspend = mmc_omap_suspend,
  1090. .resume = mmc_omap_resume,
  1091. .driver = {
  1092. .name = DRIVER_NAME,
  1093. },
  1094. };
  1095. static int __init mmc_omap_init(void)
  1096. {
  1097. return platform_driver_register(&mmc_omap_driver);
  1098. }
  1099. static void __exit mmc_omap_exit(void)
  1100. {
  1101. platform_driver_unregister(&mmc_omap_driver);
  1102. }
  1103. module_init(mmc_omap_init);
  1104. module_exit(mmc_omap_exit);
  1105. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1106. MODULE_LICENSE("GPL");
  1107. MODULE_ALIAS(DRIVER_NAME);
  1108. MODULE_AUTHOR("Juha Yrjölä");