omap_hsmmc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * drivers/mmc/host/omap_hsmmc.c
  3. *
  4. * Driver for OMAP2430/3430 MMC controller.
  5. *
  6. * Copyright (C) 2007 Texas Instruments.
  7. *
  8. * Authors:
  9. * Syed Mohammed Khasim <x0khasim@ti.com>
  10. * Madhusudhan <madhu.cr@ti.com>
  11. * Mohit Jalori <mjalori@ti.com>
  12. *
  13. * This file is licensed under the terms of the GNU General Public License
  14. * version 2. This program is licensed "as is" without any warranty of any
  15. * kind, whether express or implied.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/delay.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/timer.h>
  25. #include <linux/clk.h>
  26. #include <linux/mmc/host.h>
  27. #include <linux/io.h>
  28. #include <linux/semaphore.h>
  29. #include <mach/dma.h>
  30. #include <mach/hardware.h>
  31. #include <mach/board.h>
  32. #include <mach/mmc.h>
  33. #include <mach/cpu.h>
  34. /* OMAP HSMMC Host Controller Registers */
  35. #define OMAP_HSMMC_SYSCONFIG 0x0010
  36. #define OMAP_HSMMC_CON 0x002C
  37. #define OMAP_HSMMC_BLK 0x0104
  38. #define OMAP_HSMMC_ARG 0x0108
  39. #define OMAP_HSMMC_CMD 0x010C
  40. #define OMAP_HSMMC_RSP10 0x0110
  41. #define OMAP_HSMMC_RSP32 0x0114
  42. #define OMAP_HSMMC_RSP54 0x0118
  43. #define OMAP_HSMMC_RSP76 0x011C
  44. #define OMAP_HSMMC_DATA 0x0120
  45. #define OMAP_HSMMC_HCTL 0x0128
  46. #define OMAP_HSMMC_SYSCTL 0x012C
  47. #define OMAP_HSMMC_STAT 0x0130
  48. #define OMAP_HSMMC_IE 0x0134
  49. #define OMAP_HSMMC_ISE 0x0138
  50. #define OMAP_HSMMC_CAPA 0x0140
  51. #define VS18 (1 << 26)
  52. #define VS30 (1 << 25)
  53. #define SDVS18 (0x5 << 9)
  54. #define SDVS30 (0x6 << 9)
  55. #define SDVS33 (0x7 << 9)
  56. #define SDVS_MASK 0x00000E00
  57. #define SDVSCLR 0xFFFFF1FF
  58. #define SDVSDET 0x00000400
  59. #define AUTOIDLE 0x1
  60. #define SDBP (1 << 8)
  61. #define DTO 0xe
  62. #define ICE 0x1
  63. #define ICS 0x2
  64. #define CEN (1 << 2)
  65. #define CLKD_MASK 0x0000FFC0
  66. #define CLKD_SHIFT 6
  67. #define DTO_MASK 0x000F0000
  68. #define DTO_SHIFT 16
  69. #define INT_EN_MASK 0x307F0033
  70. #define INIT_STREAM (1 << 1)
  71. #define DP_SELECT (1 << 21)
  72. #define DDIR (1 << 4)
  73. #define DMA_EN 0x1
  74. #define MSBS (1 << 5)
  75. #define BCE (1 << 1)
  76. #define FOUR_BIT (1 << 1)
  77. #define DW8 (1 << 5)
  78. #define CC 0x1
  79. #define TC 0x02
  80. #define OD 0x1
  81. #define ERR (1 << 15)
  82. #define CMD_TIMEOUT (1 << 16)
  83. #define DATA_TIMEOUT (1 << 20)
  84. #define CMD_CRC (1 << 17)
  85. #define DATA_CRC (1 << 21)
  86. #define CARD_ERR (1 << 28)
  87. #define STAT_CLEAR 0xFFFFFFFF
  88. #define INIT_STREAM_CMD 0x00000000
  89. #define DUAL_VOLT_OCR_BIT 7
  90. #define SRC (1 << 25)
  91. #define SRD (1 << 26)
  92. /*
  93. * FIXME: Most likely all the data using these _DEVID defines should come
  94. * from the platform_data, or implemented in controller and slot specific
  95. * functions.
  96. */
  97. #define OMAP_MMC1_DEVID 0
  98. #define OMAP_MMC2_DEVID 1
  99. #define OMAP_MMC3_DEVID 2
  100. #define MMC_TIMEOUT_MS 20
  101. #define OMAP_MMC_MASTER_CLOCK 96000000
  102. #define DRIVER_NAME "mmci-omap-hs"
  103. /*
  104. * One controller can have multiple slots, like on some omap boards using
  105. * omap.c controller driver. Luckily this is not currently done on any known
  106. * omap_hsmmc.c device.
  107. */
  108. #define mmc_slot(host) (host->pdata->slots[host->slot_id])
  109. /*
  110. * MMC Host controller read/write API's
  111. */
  112. #define OMAP_HSMMC_READ(base, reg) \
  113. __raw_readl((base) + OMAP_HSMMC_##reg)
  114. #define OMAP_HSMMC_WRITE(base, reg, val) \
  115. __raw_writel((val), (base) + OMAP_HSMMC_##reg)
  116. struct mmc_omap_host {
  117. struct device *dev;
  118. struct mmc_host *mmc;
  119. struct mmc_request *mrq;
  120. struct mmc_command *cmd;
  121. struct mmc_data *data;
  122. struct clk *fclk;
  123. struct clk *iclk;
  124. struct clk *dbclk;
  125. struct semaphore sem;
  126. struct work_struct mmc_carddetect_work;
  127. void __iomem *base;
  128. resource_size_t mapbase;
  129. unsigned int id;
  130. unsigned int dma_len;
  131. unsigned int dma_sg_idx;
  132. unsigned char bus_mode;
  133. u32 *buffer;
  134. u32 bytesleft;
  135. int suspended;
  136. int irq;
  137. int carddetect;
  138. int use_dma, dma_ch;
  139. int dma_line_tx, dma_line_rx;
  140. int slot_id;
  141. int dbclk_enabled;
  142. int response_busy;
  143. struct omap_mmc_platform_data *pdata;
  144. };
  145. /*
  146. * Stop clock to the card
  147. */
  148. static void omap_mmc_stop_clock(struct mmc_omap_host *host)
  149. {
  150. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  151. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  152. if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
  153. dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
  154. }
  155. /*
  156. * Send init stream sequence to card
  157. * before sending IDLE command
  158. */
  159. static void send_init_stream(struct mmc_omap_host *host)
  160. {
  161. int reg = 0;
  162. unsigned long timeout;
  163. disable_irq(host->irq);
  164. OMAP_HSMMC_WRITE(host->base, CON,
  165. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  166. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  167. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  168. while ((reg != CC) && time_before(jiffies, timeout))
  169. reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
  170. OMAP_HSMMC_WRITE(host->base, CON,
  171. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  172. enable_irq(host->irq);
  173. }
  174. static inline
  175. int mmc_omap_cover_is_closed(struct mmc_omap_host *host)
  176. {
  177. int r = 1;
  178. if (host->pdata->slots[host->slot_id].get_cover_state)
  179. r = host->pdata->slots[host->slot_id].get_cover_state(host->dev,
  180. host->slot_id);
  181. return r;
  182. }
  183. static ssize_t
  184. mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
  185. char *buf)
  186. {
  187. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  188. struct mmc_omap_host *host = mmc_priv(mmc);
  189. return sprintf(buf, "%s\n", mmc_omap_cover_is_closed(host) ? "closed" :
  190. "open");
  191. }
  192. static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
  193. static ssize_t
  194. mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
  195. char *buf)
  196. {
  197. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  198. struct mmc_omap_host *host = mmc_priv(mmc);
  199. struct omap_mmc_slot_data slot = host->pdata->slots[host->slot_id];
  200. return sprintf(buf, "%s\n", slot.name);
  201. }
  202. static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
  203. /*
  204. * Configure the response type and send the cmd.
  205. */
  206. static void
  207. mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
  208. struct mmc_data *data)
  209. {
  210. int cmdreg = 0, resptype = 0, cmdtype = 0;
  211. dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  212. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  213. host->cmd = cmd;
  214. /*
  215. * Clear status bits and enable interrupts
  216. */
  217. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  218. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  219. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  220. host->response_busy = 0;
  221. if (cmd->flags & MMC_RSP_PRESENT) {
  222. if (cmd->flags & MMC_RSP_136)
  223. resptype = 1;
  224. else if (cmd->flags & MMC_RSP_BUSY) {
  225. resptype = 3;
  226. host->response_busy = 1;
  227. } else
  228. resptype = 2;
  229. }
  230. /*
  231. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  232. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  233. * a val of 0x3, rest 0x0.
  234. */
  235. if (cmd == host->mrq->stop)
  236. cmdtype = 0x3;
  237. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  238. if (data) {
  239. cmdreg |= DP_SELECT | MSBS | BCE;
  240. if (data->flags & MMC_DATA_READ)
  241. cmdreg |= DDIR;
  242. else
  243. cmdreg &= ~(DDIR);
  244. }
  245. if (host->use_dma)
  246. cmdreg |= DMA_EN;
  247. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  248. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  249. }
  250. static int
  251. mmc_omap_get_dma_dir(struct mmc_omap_host *host, struct mmc_data *data)
  252. {
  253. if (data->flags & MMC_DATA_WRITE)
  254. return DMA_TO_DEVICE;
  255. else
  256. return DMA_FROM_DEVICE;
  257. }
  258. /*
  259. * Notify the transfer complete to MMC core
  260. */
  261. static void
  262. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  263. {
  264. if (!data) {
  265. struct mmc_request *mrq = host->mrq;
  266. host->mrq = NULL;
  267. mmc_omap_fclk_lazy_disable(host);
  268. mmc_request_done(host->mmc, mrq);
  269. return;
  270. }
  271. host->data = NULL;
  272. if (host->use_dma && host->dma_ch != -1)
  273. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
  274. mmc_omap_get_dma_dir(host, data));
  275. if (!data->error)
  276. data->bytes_xfered += data->blocks * (data->blksz);
  277. else
  278. data->bytes_xfered = 0;
  279. if (!data->stop) {
  280. host->mrq = NULL;
  281. mmc_request_done(host->mmc, data->mrq);
  282. return;
  283. }
  284. mmc_omap_start_command(host, data->stop, NULL);
  285. }
  286. /*
  287. * Notify the core about command completion
  288. */
  289. static void
  290. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  291. {
  292. host->cmd = NULL;
  293. if (cmd->flags & MMC_RSP_PRESENT) {
  294. if (cmd->flags & MMC_RSP_136) {
  295. /* response type 2 */
  296. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  297. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  298. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  299. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  300. } else {
  301. /* response types 1, 1b, 3, 4, 5, 6 */
  302. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  303. }
  304. }
  305. if ((host->data == NULL && !host->response_busy) || cmd->error) {
  306. host->mrq = NULL;
  307. mmc_request_done(host->mmc, cmd->mrq);
  308. }
  309. }
  310. /*
  311. * DMA clean up for command errors
  312. */
  313. static void mmc_dma_cleanup(struct mmc_omap_host *host, int errno)
  314. {
  315. host->data->error = errno;
  316. if (host->use_dma && host->dma_ch != -1) {
  317. dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
  318. mmc_omap_get_dma_dir(host, host->data));
  319. omap_free_dma(host->dma_ch);
  320. host->dma_ch = -1;
  321. up(&host->sem);
  322. }
  323. host->data = NULL;
  324. }
  325. /*
  326. * Readable error output
  327. */
  328. #ifdef CONFIG_MMC_DEBUG
  329. static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
  330. {
  331. /* --- means reserved bit without definition at documentation */
  332. static const char *mmc_omap_status_bits[] = {
  333. "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
  334. "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
  335. "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
  336. "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
  337. };
  338. char res[256];
  339. char *buf = res;
  340. int len, i;
  341. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  342. buf += len;
  343. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  344. if (status & (1 << i)) {
  345. len = sprintf(buf, " %s", mmc_omap_status_bits[i]);
  346. buf += len;
  347. }
  348. dev_dbg(mmc_dev(host->mmc), "%s\n", res);
  349. }
  350. #endif /* CONFIG_MMC_DEBUG */
  351. /*
  352. * MMC controller internal state machines reset
  353. *
  354. * Used to reset command or data internal state machines, using respectively
  355. * SRC or SRD bit of SYSCTL register
  356. * Can be called from interrupt context
  357. */
  358. static inline void mmc_omap_reset_controller_fsm(struct mmc_omap_host *host,
  359. unsigned long bit)
  360. {
  361. unsigned long i = 0;
  362. unsigned long limit = (loops_per_jiffy *
  363. msecs_to_jiffies(MMC_TIMEOUT_MS));
  364. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  365. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  366. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  367. (i++ < limit))
  368. cpu_relax();
  369. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  370. dev_err(mmc_dev(host->mmc),
  371. "Timeout waiting on controller reset in %s\n",
  372. __func__);
  373. }
  374. /*
  375. * MMC controller IRQ handler
  376. */
  377. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  378. {
  379. struct mmc_omap_host *host = dev_id;
  380. struct mmc_data *data;
  381. int end_cmd = 0, end_trans = 0, status;
  382. if (host->mrq == NULL) {
  383. OMAP_HSMMC_WRITE(host->base, STAT,
  384. OMAP_HSMMC_READ(host->base, STAT));
  385. return IRQ_HANDLED;
  386. }
  387. data = host->data;
  388. status = OMAP_HSMMC_READ(host->base, STAT);
  389. dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  390. if (status & ERR) {
  391. #ifdef CONFIG_MMC_DEBUG
  392. mmc_omap_report_irq(host, status);
  393. #endif
  394. if ((status & CMD_TIMEOUT) ||
  395. (status & CMD_CRC)) {
  396. if (host->cmd) {
  397. if (status & CMD_TIMEOUT) {
  398. mmc_omap_reset_controller_fsm(host, SRC);
  399. host->cmd->error = -ETIMEDOUT;
  400. } else {
  401. host->cmd->error = -EILSEQ;
  402. }
  403. end_cmd = 1;
  404. }
  405. if (host->data || host->response_busy) {
  406. if (host->data)
  407. mmc_dma_cleanup(host, -ETIMEDOUT);
  408. host->response_busy = 0;
  409. mmc_omap_reset_controller_fsm(host, SRD);
  410. }
  411. }
  412. if ((status & DATA_TIMEOUT) ||
  413. (status & DATA_CRC)) {
  414. if (host->data || host->response_busy) {
  415. int err = (status & DATA_TIMEOUT) ?
  416. -ETIMEDOUT : -EILSEQ;
  417. if (host->data)
  418. mmc_dma_cleanup(host, err);
  419. else
  420. host->mrq->cmd->error = err;
  421. host->response_busy = 0;
  422. mmc_omap_reset_controller_fsm(host, SRD);
  423. end_trans = 1;
  424. }
  425. }
  426. if (status & CARD_ERR) {
  427. dev_dbg(mmc_dev(host->mmc),
  428. "Ignoring card err CMD%d\n", host->cmd->opcode);
  429. if (host->cmd)
  430. end_cmd = 1;
  431. if (host->data)
  432. end_trans = 1;
  433. }
  434. }
  435. OMAP_HSMMC_WRITE(host->base, STAT, status);
  436. if (end_cmd || (status & CC))
  437. mmc_omap_cmd_done(host, host->cmd);
  438. if (end_trans || (status & TC))
  439. mmc_omap_xfer_done(host, data);
  440. return IRQ_HANDLED;
  441. }
  442. static void set_sd_bus_power(struct mmc_omap_host *host)
  443. {
  444. unsigned long i;
  445. OMAP_HSMMC_WRITE(host->base, HCTL,
  446. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  447. for (i = 0; i < loops_per_jiffy; i++) {
  448. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  449. break;
  450. cpu_relax();
  451. }
  452. }
  453. /*
  454. * Switch MMC interface voltage ... only relevant for MMC1.
  455. *
  456. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  457. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  458. * Some chips, like eMMC ones, use internal transceivers.
  459. */
  460. static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
  461. {
  462. u32 reg_val = 0;
  463. int ret;
  464. /* Disable the clocks */
  465. clk_disable(host->fclk);
  466. clk_disable(host->iclk);
  467. clk_disable(host->dbclk);
  468. /* Turn the power off */
  469. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  470. if (ret != 0)
  471. goto err;
  472. /* Turn the power ON with given VDD 1.8 or 3.0v */
  473. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
  474. if (ret != 0)
  475. goto err;
  476. clk_enable(host->fclk);
  477. clk_enable(host->iclk);
  478. clk_enable(host->dbclk);
  479. OMAP_HSMMC_WRITE(host->base, HCTL,
  480. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  481. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  482. /*
  483. * If a MMC dual voltage card is detected, the set_ios fn calls
  484. * this fn with VDD bit set for 1.8V. Upon card removal from the
  485. * slot, omap_mmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  486. *
  487. * Cope with a bit of slop in the range ... per data sheets:
  488. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  489. * but recommended values are 1.71V to 1.89V
  490. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  491. * but recommended values are 2.7V to 3.3V
  492. *
  493. * Board setup code shouldn't permit anything very out-of-range.
  494. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  495. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  496. */
  497. if ((1 << vdd) <= MMC_VDD_23_24)
  498. reg_val |= SDVS18;
  499. else
  500. reg_val |= SDVS30;
  501. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  502. set_sd_bus_power(host);
  503. return 0;
  504. err:
  505. dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  506. return ret;
  507. }
  508. /*
  509. * Work Item to notify the core about card insertion/removal
  510. */
  511. static void mmc_omap_detect(struct work_struct *work)
  512. {
  513. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  514. mmc_carddetect_work);
  515. struct omap_mmc_slot_data *slot = &mmc_slot(host);
  516. if (mmc_slot(host).card_detect)
  517. host->carddetect = slot->card_detect(slot->card_detect_irq);
  518. else
  519. host->carddetect = -ENOSYS;
  520. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  521. if (host->carddetect) {
  522. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  523. } else {
  524. mmc_omap_reset_controller_fsm(host, SRD);
  525. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  526. }
  527. }
  528. /*
  529. * ISR for handling card insertion and removal
  530. */
  531. static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
  532. {
  533. struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
  534. schedule_work(&host->mmc_carddetect_work);
  535. return IRQ_HANDLED;
  536. }
  537. static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host *host,
  538. struct mmc_data *data)
  539. {
  540. int sync_dev;
  541. if (data->flags & MMC_DATA_WRITE)
  542. sync_dev = host->dma_line_tx;
  543. else
  544. sync_dev = host->dma_line_rx;
  545. return sync_dev;
  546. }
  547. static void mmc_omap_config_dma_params(struct mmc_omap_host *host,
  548. struct mmc_data *data,
  549. struct scatterlist *sgl)
  550. {
  551. int blksz, nblk, dma_ch;
  552. dma_ch = host->dma_ch;
  553. if (data->flags & MMC_DATA_WRITE) {
  554. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  555. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  556. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  557. sg_dma_address(sgl), 0, 0);
  558. } else {
  559. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  560. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  561. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  562. sg_dma_address(sgl), 0, 0);
  563. }
  564. blksz = host->data->blksz;
  565. nblk = sg_dma_len(sgl) / blksz;
  566. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
  567. blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
  568. mmc_omap_get_dma_sync_dev(host, data),
  569. !(data->flags & MMC_DATA_WRITE));
  570. omap_start_dma(dma_ch);
  571. }
  572. /*
  573. * DMA call back function
  574. */
  575. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  576. {
  577. struct mmc_omap_host *host = data;
  578. if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
  579. dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
  580. if (host->dma_ch < 0)
  581. return;
  582. host->dma_sg_idx++;
  583. if (host->dma_sg_idx < host->dma_len) {
  584. /* Fire up the next transfer. */
  585. mmc_omap_config_dma_params(host, host->data,
  586. host->data->sg + host->dma_sg_idx);
  587. return;
  588. }
  589. omap_free_dma(host->dma_ch);
  590. host->dma_ch = -1;
  591. /*
  592. * DMA Callback: run in interrupt context.
  593. * mutex_unlock will through a kernel warning if used.
  594. */
  595. up(&host->sem);
  596. }
  597. /*
  598. * Routine to configure and start DMA for the MMC card
  599. */
  600. static int
  601. mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
  602. {
  603. int dma_ch = 0, ret = 0, err = 1, i;
  604. struct mmc_data *data = req->data;
  605. /* Sanity check: all the SG entries must be aligned by block size. */
  606. for (i = 0; i < host->dma_len; i++) {
  607. struct scatterlist *sgl;
  608. sgl = data->sg + i;
  609. if (sgl->length % data->blksz)
  610. return -EINVAL;
  611. }
  612. if ((data->blksz % 4) != 0)
  613. /* REVISIT: The MMC buffer increments only when MSB is written.
  614. * Return error for blksz which is non multiple of four.
  615. */
  616. return -EINVAL;
  617. /*
  618. * If for some reason the DMA transfer is still active,
  619. * we wait for timeout period and free the dma
  620. */
  621. if (host->dma_ch != -1) {
  622. set_current_state(TASK_UNINTERRUPTIBLE);
  623. schedule_timeout(100);
  624. if (down_trylock(&host->sem)) {
  625. omap_free_dma(host->dma_ch);
  626. host->dma_ch = -1;
  627. up(&host->sem);
  628. return err;
  629. }
  630. } else {
  631. if (down_trylock(&host->sem))
  632. return err;
  633. }
  634. ret = omap_request_dma(mmc_omap_get_dma_sync_dev(host, data), "MMC/SD",
  635. mmc_omap_dma_cb,host, &dma_ch);
  636. if (ret != 0) {
  637. dev_err(mmc_dev(host->mmc),
  638. "%s: omap_request_dma() failed with %d\n",
  639. mmc_hostname(host->mmc), ret);
  640. return ret;
  641. }
  642. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  643. data->sg_len, mmc_omap_get_dma_dir(host, data));
  644. host->dma_ch = dma_ch;
  645. host->dma_sg_idx = 0;
  646. mmc_omap_config_dma_params(host, data, data->sg);
  647. return 0;
  648. }
  649. static void set_data_timeout(struct mmc_omap_host *host,
  650. struct mmc_request *req)
  651. {
  652. unsigned int timeout, cycle_ns;
  653. uint32_t reg, clkd, dto = 0;
  654. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  655. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  656. if (clkd == 0)
  657. clkd = 1;
  658. cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
  659. timeout = req->data->timeout_ns / cycle_ns;
  660. timeout += req->data->timeout_clks;
  661. if (timeout) {
  662. while ((timeout & 0x80000000) == 0) {
  663. dto += 1;
  664. timeout <<= 1;
  665. }
  666. dto = 31 - dto;
  667. timeout <<= 1;
  668. if (timeout && dto)
  669. dto += 1;
  670. if (dto >= 13)
  671. dto -= 13;
  672. else
  673. dto = 0;
  674. if (dto > 14)
  675. dto = 14;
  676. }
  677. reg &= ~DTO_MASK;
  678. reg |= dto << DTO_SHIFT;
  679. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  680. }
  681. /*
  682. * Configure block length for MMC/SD cards and initiate the transfer.
  683. */
  684. static int
  685. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  686. {
  687. int ret;
  688. host->data = req->data;
  689. if (req->data == NULL) {
  690. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  691. return 0;
  692. }
  693. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  694. | (req->data->blocks << 16));
  695. set_data_timeout(host, req);
  696. if (host->use_dma) {
  697. ret = mmc_omap_start_dma_transfer(host, req);
  698. if (ret != 0) {
  699. dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
  700. return ret;
  701. }
  702. }
  703. return 0;
  704. }
  705. /*
  706. * Request function. for read/write operation
  707. */
  708. static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
  709. {
  710. struct mmc_omap_host *host = mmc_priv(mmc);
  711. WARN_ON(host->mrq != NULL);
  712. host->mrq = req;
  713. mmc_omap_prepare_data(host, req);
  714. mmc_omap_start_command(host, req->cmd, req->data);
  715. }
  716. /* Routine to configure clock values. Exposed API to core */
  717. static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  718. {
  719. struct mmc_omap_host *host = mmc_priv(mmc);
  720. u16 dsor = 0;
  721. unsigned long regval;
  722. unsigned long timeout;
  723. u32 con;
  724. switch (ios->power_mode) {
  725. case MMC_POWER_OFF:
  726. mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  727. break;
  728. case MMC_POWER_UP:
  729. mmc_slot(host).set_power(host->dev, host->slot_id, 1, ios->vdd);
  730. break;
  731. }
  732. con = OMAP_HSMMC_READ(host->base, CON);
  733. switch (mmc->ios.bus_width) {
  734. case MMC_BUS_WIDTH_8:
  735. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  736. break;
  737. case MMC_BUS_WIDTH_4:
  738. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  739. OMAP_HSMMC_WRITE(host->base, HCTL,
  740. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  741. break;
  742. case MMC_BUS_WIDTH_1:
  743. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  744. OMAP_HSMMC_WRITE(host->base, HCTL,
  745. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  746. break;
  747. }
  748. if (host->id == OMAP_MMC1_DEVID) {
  749. /* Only MMC1 can interface at 3V without some flavor
  750. * of external transceiver; but they all handle 1.8V.
  751. */
  752. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  753. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  754. /*
  755. * The mmc_select_voltage fn of the core does
  756. * not seem to set the power_mode to
  757. * MMC_POWER_UP upon recalculating the voltage.
  758. * vdd 1.8v.
  759. */
  760. if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
  761. dev_dbg(mmc_dev(host->mmc),
  762. "Switch operation failed\n");
  763. }
  764. }
  765. if (ios->clock) {
  766. dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
  767. if (dsor < 1)
  768. dsor = 1;
  769. if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
  770. dsor++;
  771. if (dsor > 250)
  772. dsor = 250;
  773. }
  774. omap_mmc_stop_clock(host);
  775. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  776. regval = regval & ~(CLKD_MASK);
  777. regval = regval | (dsor << 6) | (DTO << 16);
  778. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  779. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  780. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  781. /* Wait till the ICS bit is set */
  782. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  783. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != 0x2
  784. && time_before(jiffies, timeout))
  785. msleep(1);
  786. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  787. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  788. if (ios->power_mode == MMC_POWER_ON)
  789. send_init_stream(host);
  790. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  791. OMAP_HSMMC_WRITE(host->base, CON,
  792. OMAP_HSMMC_READ(host->base, CON) | OD);
  793. }
  794. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  795. {
  796. struct mmc_omap_host *host = mmc_priv(mmc);
  797. struct omap_mmc_platform_data *pdata = host->pdata;
  798. if (!pdata->slots[0].card_detect)
  799. return -ENOSYS;
  800. return pdata->slots[0].card_detect(pdata->slots[0].card_detect_irq);
  801. }
  802. static int omap_hsmmc_get_ro(struct mmc_host *mmc)
  803. {
  804. struct mmc_omap_host *host = mmc_priv(mmc);
  805. struct omap_mmc_platform_data *pdata = host->pdata;
  806. if (!pdata->slots[0].get_ro)
  807. return -ENOSYS;
  808. return pdata->slots[0].get_ro(host->dev, 0);
  809. }
  810. static void omap_hsmmc_init(struct mmc_omap_host *host)
  811. {
  812. u32 hctl, capa, value;
  813. /* Only MMC1 supports 3.0V */
  814. if (host->id == OMAP_MMC1_DEVID) {
  815. hctl = SDVS30;
  816. capa = VS30 | VS18;
  817. } else {
  818. hctl = SDVS18;
  819. capa = VS18;
  820. }
  821. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  822. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  823. value = OMAP_HSMMC_READ(host->base, CAPA);
  824. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  825. /* Set the controller to AUTO IDLE mode */
  826. value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
  827. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
  828. /* Set SD bus power bit */
  829. set_sd_bus_power(host);
  830. }
  831. static struct mmc_host_ops mmc_omap_ops = {
  832. .request = omap_mmc_request,
  833. .set_ios = omap_mmc_set_ios,
  834. .get_cd = omap_hsmmc_get_cd,
  835. .get_ro = omap_hsmmc_get_ro,
  836. /* NYET -- enable_sdio_irq */
  837. };
  838. static int __init omap_mmc_probe(struct platform_device *pdev)
  839. {
  840. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  841. struct mmc_host *mmc;
  842. struct mmc_omap_host *host = NULL;
  843. struct resource *res;
  844. int ret = 0, irq;
  845. if (pdata == NULL) {
  846. dev_err(&pdev->dev, "Platform Data is missing\n");
  847. return -ENXIO;
  848. }
  849. if (pdata->nr_slots == 0) {
  850. dev_err(&pdev->dev, "No Slots\n");
  851. return -ENXIO;
  852. }
  853. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  854. irq = platform_get_irq(pdev, 0);
  855. if (res == NULL || irq < 0)
  856. return -ENXIO;
  857. res = request_mem_region(res->start, res->end - res->start + 1,
  858. pdev->name);
  859. if (res == NULL)
  860. return -EBUSY;
  861. mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
  862. if (!mmc) {
  863. ret = -ENOMEM;
  864. goto err;
  865. }
  866. host = mmc_priv(mmc);
  867. host->mmc = mmc;
  868. host->pdata = pdata;
  869. host->dev = &pdev->dev;
  870. host->use_dma = 1;
  871. host->dev->dma_mask = &pdata->dma_mask;
  872. host->dma_ch = -1;
  873. host->irq = irq;
  874. host->id = pdev->id;
  875. host->slot_id = 0;
  876. host->mapbase = res->start;
  877. host->base = ioremap(host->mapbase, SZ_4K);
  878. platform_set_drvdata(pdev, host);
  879. INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
  880. mmc->ops = &mmc_omap_ops;
  881. mmc->f_min = 400000;
  882. mmc->f_max = 52000000;
  883. sema_init(&host->sem, 1);
  884. host->iclk = clk_get(&pdev->dev, "mmchs_ick");
  885. if (IS_ERR(host->iclk)) {
  886. ret = PTR_ERR(host->iclk);
  887. host->iclk = NULL;
  888. goto err1;
  889. }
  890. host->fclk = clk_get(&pdev->dev, "mmchs_fck");
  891. if (IS_ERR(host->fclk)) {
  892. ret = PTR_ERR(host->fclk);
  893. host->fclk = NULL;
  894. clk_put(host->iclk);
  895. goto err1;
  896. }
  897. if (clk_enable(host->fclk) != 0) {
  898. clk_put(host->iclk);
  899. clk_put(host->fclk);
  900. goto err1;
  901. }
  902. if (clk_enable(host->iclk) != 0) {
  903. clk_disable(host->fclk);
  904. clk_put(host->iclk);
  905. clk_put(host->fclk);
  906. goto err1;
  907. }
  908. host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
  909. /*
  910. * MMC can still work without debounce clock.
  911. */
  912. if (IS_ERR(host->dbclk))
  913. dev_warn(mmc_dev(host->mmc), "Failed to get debounce clock\n");
  914. else
  915. if (clk_enable(host->dbclk) != 0)
  916. dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
  917. " clk failed\n");
  918. else
  919. host->dbclk_enabled = 1;
  920. /* Since we do only SG emulation, we can have as many segs
  921. * as we want. */
  922. mmc->max_phys_segs = 1024;
  923. mmc->max_hw_segs = 1024;
  924. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  925. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  926. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  927. mmc->max_seg_size = mmc->max_req_size;
  928. mmc->ocr_avail = mmc_slot(host).ocr_mask;
  929. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
  930. if (pdata->slots[host->slot_id].wires >= 8)
  931. mmc->caps |= MMC_CAP_8_BIT_DATA;
  932. else if (pdata->slots[host->slot_id].wires >= 4)
  933. mmc->caps |= MMC_CAP_4_BIT_DATA;
  934. omap_hsmmc_init(host);
  935. /* Select DMA lines */
  936. switch (host->id) {
  937. case OMAP_MMC1_DEVID:
  938. host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
  939. host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
  940. break;
  941. case OMAP_MMC2_DEVID:
  942. host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
  943. host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
  944. break;
  945. case OMAP_MMC3_DEVID:
  946. host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
  947. host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
  948. break;
  949. default:
  950. dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
  951. goto err_irq;
  952. }
  953. /* Request IRQ for MMC operations */
  954. ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
  955. mmc_hostname(mmc), host);
  956. if (ret) {
  957. dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  958. goto err_irq;
  959. }
  960. if (pdata->init != NULL) {
  961. if (pdata->init(&pdev->dev) != 0) {
  962. dev_dbg(mmc_dev(host->mmc),
  963. "Unable to configure MMC IRQs\n");
  964. goto err_irq_cd_init;
  965. }
  966. }
  967. /* Request IRQ for card detect */
  968. if ((mmc_slot(host).card_detect_irq)) {
  969. ret = request_irq(mmc_slot(host).card_detect_irq,
  970. omap_mmc_cd_handler,
  971. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
  972. | IRQF_DISABLED,
  973. mmc_hostname(mmc), host);
  974. if (ret) {
  975. dev_dbg(mmc_dev(host->mmc),
  976. "Unable to grab MMC CD IRQ\n");
  977. goto err_irq_cd;
  978. }
  979. }
  980. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  981. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  982. mmc_add_host(mmc);
  983. if (host->pdata->slots[host->slot_id].name != NULL) {
  984. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  985. if (ret < 0)
  986. goto err_slot_name;
  987. }
  988. if (mmc_slot(host).card_detect_irq &&
  989. host->pdata->slots[host->slot_id].get_cover_state) {
  990. ret = device_create_file(&mmc->class_dev,
  991. &dev_attr_cover_switch);
  992. if (ret < 0)
  993. goto err_cover_switch;
  994. }
  995. return 0;
  996. err_cover_switch:
  997. device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
  998. err_slot_name:
  999. mmc_remove_host(mmc);
  1000. err_irq_cd:
  1001. free_irq(mmc_slot(host).card_detect_irq, host);
  1002. err_irq_cd_init:
  1003. free_irq(host->irq, host);
  1004. err_irq:
  1005. clk_disable(host->fclk);
  1006. clk_disable(host->iclk);
  1007. clk_put(host->fclk);
  1008. clk_put(host->iclk);
  1009. if (host->dbclk_enabled) {
  1010. clk_disable(host->dbclk);
  1011. clk_put(host->dbclk);
  1012. }
  1013. err1:
  1014. iounmap(host->base);
  1015. err:
  1016. dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
  1017. release_mem_region(res->start, res->end - res->start + 1);
  1018. if (host)
  1019. mmc_free_host(mmc);
  1020. return ret;
  1021. }
  1022. static int omap_mmc_remove(struct platform_device *pdev)
  1023. {
  1024. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1025. struct resource *res;
  1026. if (host) {
  1027. mmc_remove_host(host->mmc);
  1028. if (host->pdata->cleanup)
  1029. host->pdata->cleanup(&pdev->dev);
  1030. free_irq(host->irq, host);
  1031. if (mmc_slot(host).card_detect_irq)
  1032. free_irq(mmc_slot(host).card_detect_irq, host);
  1033. flush_scheduled_work();
  1034. clk_disable(host->fclk);
  1035. clk_disable(host->iclk);
  1036. clk_put(host->fclk);
  1037. clk_put(host->iclk);
  1038. if (host->dbclk_enabled) {
  1039. clk_disable(host->dbclk);
  1040. clk_put(host->dbclk);
  1041. }
  1042. mmc_free_host(host->mmc);
  1043. iounmap(host->base);
  1044. }
  1045. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1046. if (res)
  1047. release_mem_region(res->start, res->end - res->start + 1);
  1048. platform_set_drvdata(pdev, NULL);
  1049. return 0;
  1050. }
  1051. #ifdef CONFIG_PM
  1052. static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
  1053. {
  1054. int ret = 0;
  1055. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1056. if (host && host->suspended)
  1057. return 0;
  1058. if (host) {
  1059. ret = mmc_suspend_host(host->mmc, state);
  1060. if (ret == 0) {
  1061. host->suspended = 1;
  1062. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1063. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1064. if (host->pdata->suspend) {
  1065. ret = host->pdata->suspend(&pdev->dev,
  1066. host->slot_id);
  1067. if (ret)
  1068. dev_dbg(mmc_dev(host->mmc),
  1069. "Unable to handle MMC board"
  1070. " level suspend\n");
  1071. }
  1072. OMAP_HSMMC_WRITE(host->base, HCTL,
  1073. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1074. clk_disable(host->fclk);
  1075. clk_disable(host->iclk);
  1076. clk_disable(host->dbclk);
  1077. }
  1078. }
  1079. return ret;
  1080. }
  1081. /* Routine to resume the MMC device */
  1082. static int omap_mmc_resume(struct platform_device *pdev)
  1083. {
  1084. int ret = 0;
  1085. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1086. if (host && !host->suspended)
  1087. return 0;
  1088. if (host) {
  1089. ret = clk_enable(host->fclk);
  1090. if (ret)
  1091. goto clk_en_err;
  1092. ret = clk_enable(host->iclk);
  1093. if (ret) {
  1094. clk_disable(host->fclk);
  1095. clk_put(host->fclk);
  1096. goto clk_en_err;
  1097. }
  1098. if (clk_enable(host->dbclk) != 0)
  1099. dev_dbg(mmc_dev(host->mmc),
  1100. "Enabling debounce clk failed\n");
  1101. omap_hsmmc_init(host);
  1102. if (host->pdata->resume) {
  1103. ret = host->pdata->resume(&pdev->dev, host->slot_id);
  1104. if (ret)
  1105. dev_dbg(mmc_dev(host->mmc),
  1106. "Unmask interrupt failed\n");
  1107. }
  1108. /* Notify the core to resume the host */
  1109. ret = mmc_resume_host(host->mmc);
  1110. if (ret == 0)
  1111. host->suspended = 0;
  1112. }
  1113. return ret;
  1114. clk_en_err:
  1115. dev_dbg(mmc_dev(host->mmc),
  1116. "Failed to enable MMC clocks during resume\n");
  1117. return ret;
  1118. }
  1119. #else
  1120. #define omap_mmc_suspend NULL
  1121. #define omap_mmc_resume NULL
  1122. #endif
  1123. static struct platform_driver omap_mmc_driver = {
  1124. .probe = omap_mmc_probe,
  1125. .remove = omap_mmc_remove,
  1126. .suspend = omap_mmc_suspend,
  1127. .resume = omap_mmc_resume,
  1128. .driver = {
  1129. .name = DRIVER_NAME,
  1130. .owner = THIS_MODULE,
  1131. },
  1132. };
  1133. static int __init omap_mmc_init(void)
  1134. {
  1135. /* Register the MMC driver */
  1136. return platform_driver_register(&omap_mmc_driver);
  1137. }
  1138. static void __exit omap_mmc_cleanup(void)
  1139. {
  1140. /* Unregister MMC driver */
  1141. platform_driver_unregister(&omap_mmc_driver);
  1142. }
  1143. module_init(omap_mmc_init);
  1144. module_exit(omap_mmc_cleanup);
  1145. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  1146. MODULE_LICENSE("GPL");
  1147. MODULE_ALIAS("platform:" DRIVER_NAME);
  1148. MODULE_AUTHOR("Texas Instruments Inc");