omap.c 31 KB

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