omap_hsmmc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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_request_done(host->mmc, mrq);
  268. return;
  269. }
  270. host->data = NULL;
  271. if (host->use_dma && host->dma_ch != -1)
  272. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
  273. mmc_omap_get_dma_dir(host, data));
  274. if (!data->error)
  275. data->bytes_xfered += data->blocks * (data->blksz);
  276. else
  277. data->bytes_xfered = 0;
  278. if (!data->stop) {
  279. host->mrq = NULL;
  280. mmc_request_done(host->mmc, data->mrq);
  281. return;
  282. }
  283. mmc_omap_start_command(host, data->stop, NULL);
  284. }
  285. /*
  286. * Notify the core about command completion
  287. */
  288. static void
  289. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  290. {
  291. host->cmd = NULL;
  292. if (cmd->flags & MMC_RSP_PRESENT) {
  293. if (cmd->flags & MMC_RSP_136) {
  294. /* response type 2 */
  295. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  296. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  297. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  298. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  299. } else {
  300. /* response types 1, 1b, 3, 4, 5, 6 */
  301. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  302. }
  303. }
  304. if ((host->data == NULL && !host->response_busy) || cmd->error) {
  305. host->mrq = NULL;
  306. mmc_request_done(host->mmc, cmd->mrq);
  307. }
  308. }
  309. /*
  310. * DMA clean up for command errors
  311. */
  312. static void mmc_dma_cleanup(struct mmc_omap_host *host, int errno)
  313. {
  314. host->data->error = errno;
  315. if (host->use_dma && host->dma_ch != -1) {
  316. dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
  317. mmc_omap_get_dma_dir(host, host->data));
  318. omap_free_dma(host->dma_ch);
  319. host->dma_ch = -1;
  320. up(&host->sem);
  321. }
  322. host->data = NULL;
  323. }
  324. /*
  325. * Readable error output
  326. */
  327. #ifdef CONFIG_MMC_DEBUG
  328. static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
  329. {
  330. /* --- means reserved bit without definition at documentation */
  331. static const char *mmc_omap_status_bits[] = {
  332. "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
  333. "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
  334. "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
  335. "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
  336. };
  337. char res[256];
  338. char *buf = res;
  339. int len, i;
  340. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  341. buf += len;
  342. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  343. if (status & (1 << i)) {
  344. len = sprintf(buf, " %s", mmc_omap_status_bits[i]);
  345. buf += len;
  346. }
  347. dev_dbg(mmc_dev(host->mmc), "%s\n", res);
  348. }
  349. #endif /* CONFIG_MMC_DEBUG */
  350. /*
  351. * MMC controller internal state machines reset
  352. *
  353. * Used to reset command or data internal state machines, using respectively
  354. * SRC or SRD bit of SYSCTL register
  355. * Can be called from interrupt context
  356. */
  357. static inline void mmc_omap_reset_controller_fsm(struct mmc_omap_host *host,
  358. unsigned long bit)
  359. {
  360. unsigned long i = 0;
  361. unsigned long limit = (loops_per_jiffy *
  362. msecs_to_jiffies(MMC_TIMEOUT_MS));
  363. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  364. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  365. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  366. (i++ < limit))
  367. cpu_relax();
  368. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  369. dev_err(mmc_dev(host->mmc),
  370. "Timeout waiting on controller reset in %s\n",
  371. __func__);
  372. }
  373. /*
  374. * MMC controller IRQ handler
  375. */
  376. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  377. {
  378. struct mmc_omap_host *host = dev_id;
  379. struct mmc_data *data;
  380. int end_cmd = 0, end_trans = 0, status;
  381. if (host->mrq == NULL) {
  382. OMAP_HSMMC_WRITE(host->base, STAT,
  383. OMAP_HSMMC_READ(host->base, STAT));
  384. /* Flush posted write */
  385. OMAP_HSMMC_READ(host->base, STAT);
  386. return IRQ_HANDLED;
  387. }
  388. data = host->data;
  389. status = OMAP_HSMMC_READ(host->base, STAT);
  390. dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  391. if (status & ERR) {
  392. #ifdef CONFIG_MMC_DEBUG
  393. mmc_omap_report_irq(host, status);
  394. #endif
  395. if ((status & CMD_TIMEOUT) ||
  396. (status & CMD_CRC)) {
  397. if (host->cmd) {
  398. if (status & CMD_TIMEOUT) {
  399. mmc_omap_reset_controller_fsm(host, SRC);
  400. host->cmd->error = -ETIMEDOUT;
  401. } else {
  402. host->cmd->error = -EILSEQ;
  403. }
  404. end_cmd = 1;
  405. }
  406. if (host->data || host->response_busy) {
  407. if (host->data)
  408. mmc_dma_cleanup(host, -ETIMEDOUT);
  409. host->response_busy = 0;
  410. mmc_omap_reset_controller_fsm(host, SRD);
  411. }
  412. }
  413. if ((status & DATA_TIMEOUT) ||
  414. (status & DATA_CRC)) {
  415. if (host->data || host->response_busy) {
  416. int err = (status & DATA_TIMEOUT) ?
  417. -ETIMEDOUT : -EILSEQ;
  418. if (host->data)
  419. mmc_dma_cleanup(host, err);
  420. else
  421. host->mrq->cmd->error = err;
  422. host->response_busy = 0;
  423. mmc_omap_reset_controller_fsm(host, SRD);
  424. end_trans = 1;
  425. }
  426. }
  427. if (status & CARD_ERR) {
  428. dev_dbg(mmc_dev(host->mmc),
  429. "Ignoring card err CMD%d\n", host->cmd->opcode);
  430. if (host->cmd)
  431. end_cmd = 1;
  432. if (host->data)
  433. end_trans = 1;
  434. }
  435. }
  436. OMAP_HSMMC_WRITE(host->base, STAT, status);
  437. /* Flush posted write */
  438. OMAP_HSMMC_READ(host->base, STAT);
  439. if (end_cmd || ((status & CC) && host->cmd))
  440. mmc_omap_cmd_done(host, host->cmd);
  441. if (end_trans || (status & TC))
  442. mmc_omap_xfer_done(host, data);
  443. return IRQ_HANDLED;
  444. }
  445. static void set_sd_bus_power(struct mmc_omap_host *host)
  446. {
  447. unsigned long i;
  448. OMAP_HSMMC_WRITE(host->base, HCTL,
  449. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  450. for (i = 0; i < loops_per_jiffy; i++) {
  451. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  452. break;
  453. cpu_relax();
  454. }
  455. }
  456. /*
  457. * Switch MMC interface voltage ... only relevant for MMC1.
  458. *
  459. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  460. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  461. * Some chips, like eMMC ones, use internal transceivers.
  462. */
  463. static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
  464. {
  465. u32 reg_val = 0;
  466. int ret;
  467. /* Disable the clocks */
  468. clk_disable(host->fclk);
  469. clk_disable(host->iclk);
  470. clk_disable(host->dbclk);
  471. /* Turn the power off */
  472. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  473. if (ret != 0)
  474. goto err;
  475. /* Turn the power ON with given VDD 1.8 or 3.0v */
  476. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
  477. if (ret != 0)
  478. goto err;
  479. clk_enable(host->fclk);
  480. clk_enable(host->iclk);
  481. clk_enable(host->dbclk);
  482. OMAP_HSMMC_WRITE(host->base, HCTL,
  483. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  484. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  485. /*
  486. * If a MMC dual voltage card is detected, the set_ios fn calls
  487. * this fn with VDD bit set for 1.8V. Upon card removal from the
  488. * slot, omap_mmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  489. *
  490. * Cope with a bit of slop in the range ... per data sheets:
  491. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  492. * but recommended values are 1.71V to 1.89V
  493. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  494. * but recommended values are 2.7V to 3.3V
  495. *
  496. * Board setup code shouldn't permit anything very out-of-range.
  497. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  498. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  499. */
  500. if ((1 << vdd) <= MMC_VDD_23_24)
  501. reg_val |= SDVS18;
  502. else
  503. reg_val |= SDVS30;
  504. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  505. set_sd_bus_power(host);
  506. return 0;
  507. err:
  508. dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  509. return ret;
  510. }
  511. /*
  512. * Work Item to notify the core about card insertion/removal
  513. */
  514. static void mmc_omap_detect(struct work_struct *work)
  515. {
  516. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  517. mmc_carddetect_work);
  518. struct omap_mmc_slot_data *slot = &mmc_slot(host);
  519. if (mmc_slot(host).card_detect)
  520. host->carddetect = slot->card_detect(slot->card_detect_irq);
  521. else
  522. host->carddetect = -ENOSYS;
  523. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  524. if (host->carddetect) {
  525. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  526. } else {
  527. mmc_omap_reset_controller_fsm(host, SRD);
  528. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  529. }
  530. }
  531. /*
  532. * ISR for handling card insertion and removal
  533. */
  534. static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
  535. {
  536. struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
  537. schedule_work(&host->mmc_carddetect_work);
  538. return IRQ_HANDLED;
  539. }
  540. static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host *host,
  541. struct mmc_data *data)
  542. {
  543. int sync_dev;
  544. if (data->flags & MMC_DATA_WRITE)
  545. sync_dev = host->dma_line_tx;
  546. else
  547. sync_dev = host->dma_line_rx;
  548. return sync_dev;
  549. }
  550. static void mmc_omap_config_dma_params(struct mmc_omap_host *host,
  551. struct mmc_data *data,
  552. struct scatterlist *sgl)
  553. {
  554. int blksz, nblk, dma_ch;
  555. dma_ch = host->dma_ch;
  556. if (data->flags & MMC_DATA_WRITE) {
  557. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  558. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  559. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  560. sg_dma_address(sgl), 0, 0);
  561. } else {
  562. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  563. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  564. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  565. sg_dma_address(sgl), 0, 0);
  566. }
  567. blksz = host->data->blksz;
  568. nblk = sg_dma_len(sgl) / blksz;
  569. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
  570. blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
  571. mmc_omap_get_dma_sync_dev(host, data),
  572. !(data->flags & MMC_DATA_WRITE));
  573. omap_start_dma(dma_ch);
  574. }
  575. /*
  576. * DMA call back function
  577. */
  578. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  579. {
  580. struct mmc_omap_host *host = data;
  581. if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
  582. dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
  583. if (host->dma_ch < 0)
  584. return;
  585. host->dma_sg_idx++;
  586. if (host->dma_sg_idx < host->dma_len) {
  587. /* Fire up the next transfer. */
  588. mmc_omap_config_dma_params(host, host->data,
  589. host->data->sg + host->dma_sg_idx);
  590. return;
  591. }
  592. omap_free_dma(host->dma_ch);
  593. host->dma_ch = -1;
  594. /*
  595. * DMA Callback: run in interrupt context.
  596. * mutex_unlock will throw a kernel warning if used.
  597. */
  598. up(&host->sem);
  599. }
  600. /*
  601. * Routine to configure and start DMA for the MMC card
  602. */
  603. static int
  604. mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
  605. {
  606. int dma_ch = 0, ret = 0, err = 1, i;
  607. struct mmc_data *data = req->data;
  608. /* Sanity check: all the SG entries must be aligned by block size. */
  609. for (i = 0; i < host->dma_len; i++) {
  610. struct scatterlist *sgl;
  611. sgl = data->sg + i;
  612. if (sgl->length % data->blksz)
  613. return -EINVAL;
  614. }
  615. if ((data->blksz % 4) != 0)
  616. /* REVISIT: The MMC buffer increments only when MSB is written.
  617. * Return error for blksz which is non multiple of four.
  618. */
  619. return -EINVAL;
  620. /*
  621. * If for some reason the DMA transfer is still active,
  622. * we wait for timeout period and free the dma
  623. */
  624. if (host->dma_ch != -1) {
  625. set_current_state(TASK_UNINTERRUPTIBLE);
  626. schedule_timeout(100);
  627. if (down_trylock(&host->sem)) {
  628. omap_free_dma(host->dma_ch);
  629. host->dma_ch = -1;
  630. up(&host->sem);
  631. return err;
  632. }
  633. } else {
  634. if (down_trylock(&host->sem))
  635. return err;
  636. }
  637. ret = omap_request_dma(mmc_omap_get_dma_sync_dev(host, data), "MMC/SD",
  638. mmc_omap_dma_cb,host, &dma_ch);
  639. if (ret != 0) {
  640. dev_err(mmc_dev(host->mmc),
  641. "%s: omap_request_dma() failed with %d\n",
  642. mmc_hostname(host->mmc), ret);
  643. return ret;
  644. }
  645. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  646. data->sg_len, mmc_omap_get_dma_dir(host, data));
  647. host->dma_ch = dma_ch;
  648. host->dma_sg_idx = 0;
  649. mmc_omap_config_dma_params(host, data, data->sg);
  650. return 0;
  651. }
  652. static void set_data_timeout(struct mmc_omap_host *host,
  653. struct mmc_request *req)
  654. {
  655. unsigned int timeout, cycle_ns;
  656. uint32_t reg, clkd, dto = 0;
  657. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  658. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  659. if (clkd == 0)
  660. clkd = 1;
  661. cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
  662. timeout = req->data->timeout_ns / cycle_ns;
  663. timeout += req->data->timeout_clks;
  664. if (timeout) {
  665. while ((timeout & 0x80000000) == 0) {
  666. dto += 1;
  667. timeout <<= 1;
  668. }
  669. dto = 31 - dto;
  670. timeout <<= 1;
  671. if (timeout && dto)
  672. dto += 1;
  673. if (dto >= 13)
  674. dto -= 13;
  675. else
  676. dto = 0;
  677. if (dto > 14)
  678. dto = 14;
  679. }
  680. reg &= ~DTO_MASK;
  681. reg |= dto << DTO_SHIFT;
  682. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  683. }
  684. /*
  685. * Configure block length for MMC/SD cards and initiate the transfer.
  686. */
  687. static int
  688. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  689. {
  690. int ret;
  691. host->data = req->data;
  692. if (req->data == NULL) {
  693. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  694. return 0;
  695. }
  696. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  697. | (req->data->blocks << 16));
  698. set_data_timeout(host, req);
  699. if (host->use_dma) {
  700. ret = mmc_omap_start_dma_transfer(host, req);
  701. if (ret != 0) {
  702. dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
  703. return ret;
  704. }
  705. }
  706. return 0;
  707. }
  708. /*
  709. * Request function. for read/write operation
  710. */
  711. static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
  712. {
  713. struct mmc_omap_host *host = mmc_priv(mmc);
  714. WARN_ON(host->mrq != NULL);
  715. host->mrq = req;
  716. mmc_omap_prepare_data(host, req);
  717. mmc_omap_start_command(host, req->cmd, req->data);
  718. }
  719. /* Routine to configure clock values. Exposed API to core */
  720. static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  721. {
  722. struct mmc_omap_host *host = mmc_priv(mmc);
  723. u16 dsor = 0;
  724. unsigned long regval;
  725. unsigned long timeout;
  726. u32 con;
  727. switch (ios->power_mode) {
  728. case MMC_POWER_OFF:
  729. mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  730. break;
  731. case MMC_POWER_UP:
  732. mmc_slot(host).set_power(host->dev, host->slot_id, 1, ios->vdd);
  733. break;
  734. }
  735. con = OMAP_HSMMC_READ(host->base, CON);
  736. switch (mmc->ios.bus_width) {
  737. case MMC_BUS_WIDTH_8:
  738. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  739. break;
  740. case MMC_BUS_WIDTH_4:
  741. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  742. OMAP_HSMMC_WRITE(host->base, HCTL,
  743. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  744. break;
  745. case MMC_BUS_WIDTH_1:
  746. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  747. OMAP_HSMMC_WRITE(host->base, HCTL,
  748. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  749. break;
  750. }
  751. if (host->id == OMAP_MMC1_DEVID) {
  752. /* Only MMC1 can interface at 3V without some flavor
  753. * of external transceiver; but they all handle 1.8V.
  754. */
  755. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  756. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  757. /*
  758. * The mmc_select_voltage fn of the core does
  759. * not seem to set the power_mode to
  760. * MMC_POWER_UP upon recalculating the voltage.
  761. * vdd 1.8v.
  762. */
  763. if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
  764. dev_dbg(mmc_dev(host->mmc),
  765. "Switch operation failed\n");
  766. }
  767. }
  768. if (ios->clock) {
  769. dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
  770. if (dsor < 1)
  771. dsor = 1;
  772. if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
  773. dsor++;
  774. if (dsor > 250)
  775. dsor = 250;
  776. }
  777. omap_mmc_stop_clock(host);
  778. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  779. regval = regval & ~(CLKD_MASK);
  780. regval = regval | (dsor << 6) | (DTO << 16);
  781. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  782. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  783. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  784. /* Wait till the ICS bit is set */
  785. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  786. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != 0x2
  787. && time_before(jiffies, timeout))
  788. msleep(1);
  789. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  790. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  791. if (ios->power_mode == MMC_POWER_ON)
  792. send_init_stream(host);
  793. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  794. OMAP_HSMMC_WRITE(host->base, CON,
  795. OMAP_HSMMC_READ(host->base, CON) | OD);
  796. }
  797. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  798. {
  799. struct mmc_omap_host *host = mmc_priv(mmc);
  800. struct omap_mmc_platform_data *pdata = host->pdata;
  801. if (!pdata->slots[0].card_detect)
  802. return -ENOSYS;
  803. return pdata->slots[0].card_detect(pdata->slots[0].card_detect_irq);
  804. }
  805. static int omap_hsmmc_get_ro(struct mmc_host *mmc)
  806. {
  807. struct mmc_omap_host *host = mmc_priv(mmc);
  808. struct omap_mmc_platform_data *pdata = host->pdata;
  809. if (!pdata->slots[0].get_ro)
  810. return -ENOSYS;
  811. return pdata->slots[0].get_ro(host->dev, 0);
  812. }
  813. static void omap_hsmmc_init(struct mmc_omap_host *host)
  814. {
  815. u32 hctl, capa, value;
  816. /* Only MMC1 supports 3.0V */
  817. if (host->id == OMAP_MMC1_DEVID) {
  818. hctl = SDVS30;
  819. capa = VS30 | VS18;
  820. } else {
  821. hctl = SDVS18;
  822. capa = VS18;
  823. }
  824. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  825. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  826. value = OMAP_HSMMC_READ(host->base, CAPA);
  827. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  828. /* Set the controller to AUTO IDLE mode */
  829. value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
  830. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
  831. /* Set SD bus power bit */
  832. set_sd_bus_power(host);
  833. }
  834. static struct mmc_host_ops mmc_omap_ops = {
  835. .request = omap_mmc_request,
  836. .set_ios = omap_mmc_set_ios,
  837. .get_cd = omap_hsmmc_get_cd,
  838. .get_ro = omap_hsmmc_get_ro,
  839. /* NYET -- enable_sdio_irq */
  840. };
  841. static int __init omap_mmc_probe(struct platform_device *pdev)
  842. {
  843. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  844. struct mmc_host *mmc;
  845. struct mmc_omap_host *host = NULL;
  846. struct resource *res;
  847. int ret = 0, irq;
  848. if (pdata == NULL) {
  849. dev_err(&pdev->dev, "Platform Data is missing\n");
  850. return -ENXIO;
  851. }
  852. if (pdata->nr_slots == 0) {
  853. dev_err(&pdev->dev, "No Slots\n");
  854. return -ENXIO;
  855. }
  856. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  857. irq = platform_get_irq(pdev, 0);
  858. if (res == NULL || irq < 0)
  859. return -ENXIO;
  860. res = request_mem_region(res->start, res->end - res->start + 1,
  861. pdev->name);
  862. if (res == NULL)
  863. return -EBUSY;
  864. mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
  865. if (!mmc) {
  866. ret = -ENOMEM;
  867. goto err;
  868. }
  869. host = mmc_priv(mmc);
  870. host->mmc = mmc;
  871. host->pdata = pdata;
  872. host->dev = &pdev->dev;
  873. host->use_dma = 1;
  874. host->dev->dma_mask = &pdata->dma_mask;
  875. host->dma_ch = -1;
  876. host->irq = irq;
  877. host->id = pdev->id;
  878. host->slot_id = 0;
  879. host->mapbase = res->start;
  880. host->base = ioremap(host->mapbase, SZ_4K);
  881. platform_set_drvdata(pdev, host);
  882. INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
  883. mmc->ops = &mmc_omap_ops;
  884. mmc->f_min = 400000;
  885. mmc->f_max = 52000000;
  886. sema_init(&host->sem, 1);
  887. host->iclk = clk_get(&pdev->dev, "ick");
  888. if (IS_ERR(host->iclk)) {
  889. ret = PTR_ERR(host->iclk);
  890. host->iclk = NULL;
  891. goto err1;
  892. }
  893. host->fclk = clk_get(&pdev->dev, "fck");
  894. if (IS_ERR(host->fclk)) {
  895. ret = PTR_ERR(host->fclk);
  896. host->fclk = NULL;
  897. clk_put(host->iclk);
  898. goto err1;
  899. }
  900. if (clk_enable(host->fclk) != 0) {
  901. clk_put(host->iclk);
  902. clk_put(host->fclk);
  903. goto err1;
  904. }
  905. if (clk_enable(host->iclk) != 0) {
  906. clk_disable(host->fclk);
  907. clk_put(host->iclk);
  908. clk_put(host->fclk);
  909. goto err1;
  910. }
  911. host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
  912. /*
  913. * MMC can still work without debounce clock.
  914. */
  915. if (IS_ERR(host->dbclk))
  916. dev_warn(mmc_dev(host->mmc), "Failed to get debounce clock\n");
  917. else
  918. if (clk_enable(host->dbclk) != 0)
  919. dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
  920. " clk failed\n");
  921. else
  922. host->dbclk_enabled = 1;
  923. /* Since we do only SG emulation, we can have as many segs
  924. * as we want. */
  925. mmc->max_phys_segs = 1024;
  926. mmc->max_hw_segs = 1024;
  927. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  928. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  929. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  930. mmc->max_seg_size = mmc->max_req_size;
  931. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
  932. if (pdata->slots[host->slot_id].wires >= 8)
  933. mmc->caps |= MMC_CAP_8_BIT_DATA;
  934. else if (pdata->slots[host->slot_id].wires >= 4)
  935. mmc->caps |= MMC_CAP_4_BIT_DATA;
  936. omap_hsmmc_init(host);
  937. /* Select DMA lines */
  938. switch (host->id) {
  939. case OMAP_MMC1_DEVID:
  940. host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
  941. host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
  942. break;
  943. case OMAP_MMC2_DEVID:
  944. host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
  945. host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
  946. break;
  947. case OMAP_MMC3_DEVID:
  948. host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
  949. host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
  950. break;
  951. default:
  952. dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
  953. goto err_irq;
  954. }
  955. /* Request IRQ for MMC operations */
  956. ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
  957. mmc_hostname(mmc), host);
  958. if (ret) {
  959. dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  960. goto err_irq;
  961. }
  962. /* initialize power supplies, gpios, etc */
  963. if (pdata->init != NULL) {
  964. if (pdata->init(&pdev->dev) != 0) {
  965. dev_dbg(mmc_dev(host->mmc), "late init error\n");
  966. goto err_irq_cd_init;
  967. }
  968. }
  969. mmc->ocr_avail = mmc_slot(host).ocr_mask;
  970. /* Request IRQ for card detect */
  971. if ((mmc_slot(host).card_detect_irq)) {
  972. ret = request_irq(mmc_slot(host).card_detect_irq,
  973. omap_mmc_cd_handler,
  974. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
  975. | IRQF_DISABLED,
  976. mmc_hostname(mmc), host);
  977. if (ret) {
  978. dev_dbg(mmc_dev(host->mmc),
  979. "Unable to grab MMC CD IRQ\n");
  980. goto err_irq_cd;
  981. }
  982. }
  983. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  984. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  985. mmc_add_host(mmc);
  986. if (host->pdata->slots[host->slot_id].name != NULL) {
  987. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  988. if (ret < 0)
  989. goto err_slot_name;
  990. }
  991. if (mmc_slot(host).card_detect_irq &&
  992. host->pdata->slots[host->slot_id].get_cover_state) {
  993. ret = device_create_file(&mmc->class_dev,
  994. &dev_attr_cover_switch);
  995. if (ret < 0)
  996. goto err_cover_switch;
  997. }
  998. return 0;
  999. err_cover_switch:
  1000. device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
  1001. err_slot_name:
  1002. mmc_remove_host(mmc);
  1003. err_irq_cd:
  1004. free_irq(mmc_slot(host).card_detect_irq, host);
  1005. err_irq_cd_init:
  1006. free_irq(host->irq, host);
  1007. err_irq:
  1008. clk_disable(host->fclk);
  1009. clk_disable(host->iclk);
  1010. clk_put(host->fclk);
  1011. clk_put(host->iclk);
  1012. if (host->dbclk_enabled) {
  1013. clk_disable(host->dbclk);
  1014. clk_put(host->dbclk);
  1015. }
  1016. err1:
  1017. iounmap(host->base);
  1018. err:
  1019. dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
  1020. release_mem_region(res->start, res->end - res->start + 1);
  1021. if (host)
  1022. mmc_free_host(mmc);
  1023. return ret;
  1024. }
  1025. static int omap_mmc_remove(struct platform_device *pdev)
  1026. {
  1027. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1028. struct resource *res;
  1029. if (host) {
  1030. mmc_remove_host(host->mmc);
  1031. if (host->pdata->cleanup)
  1032. host->pdata->cleanup(&pdev->dev);
  1033. free_irq(host->irq, host);
  1034. if (mmc_slot(host).card_detect_irq)
  1035. free_irq(mmc_slot(host).card_detect_irq, host);
  1036. flush_scheduled_work();
  1037. clk_disable(host->fclk);
  1038. clk_disable(host->iclk);
  1039. clk_put(host->fclk);
  1040. clk_put(host->iclk);
  1041. if (host->dbclk_enabled) {
  1042. clk_disable(host->dbclk);
  1043. clk_put(host->dbclk);
  1044. }
  1045. mmc_free_host(host->mmc);
  1046. iounmap(host->base);
  1047. }
  1048. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1049. if (res)
  1050. release_mem_region(res->start, res->end - res->start + 1);
  1051. platform_set_drvdata(pdev, NULL);
  1052. return 0;
  1053. }
  1054. #ifdef CONFIG_PM
  1055. static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
  1056. {
  1057. int ret = 0;
  1058. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1059. if (host && host->suspended)
  1060. return 0;
  1061. if (host) {
  1062. ret = mmc_suspend_host(host->mmc, state);
  1063. if (ret == 0) {
  1064. host->suspended = 1;
  1065. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1066. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1067. if (host->pdata->suspend) {
  1068. ret = host->pdata->suspend(&pdev->dev,
  1069. host->slot_id);
  1070. if (ret)
  1071. dev_dbg(mmc_dev(host->mmc),
  1072. "Unable to handle MMC board"
  1073. " level suspend\n");
  1074. }
  1075. OMAP_HSMMC_WRITE(host->base, HCTL,
  1076. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1077. clk_disable(host->fclk);
  1078. clk_disable(host->iclk);
  1079. clk_disable(host->dbclk);
  1080. }
  1081. }
  1082. return ret;
  1083. }
  1084. /* Routine to resume the MMC device */
  1085. static int omap_mmc_resume(struct platform_device *pdev)
  1086. {
  1087. int ret = 0;
  1088. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1089. if (host && !host->suspended)
  1090. return 0;
  1091. if (host) {
  1092. ret = clk_enable(host->fclk);
  1093. if (ret)
  1094. goto clk_en_err;
  1095. ret = clk_enable(host->iclk);
  1096. if (ret) {
  1097. clk_disable(host->fclk);
  1098. clk_put(host->fclk);
  1099. goto clk_en_err;
  1100. }
  1101. if (clk_enable(host->dbclk) != 0)
  1102. dev_dbg(mmc_dev(host->mmc),
  1103. "Enabling debounce clk failed\n");
  1104. omap_hsmmc_init(host);
  1105. if (host->pdata->resume) {
  1106. ret = host->pdata->resume(&pdev->dev, host->slot_id);
  1107. if (ret)
  1108. dev_dbg(mmc_dev(host->mmc),
  1109. "Unmask interrupt failed\n");
  1110. }
  1111. /* Notify the core to resume the host */
  1112. ret = mmc_resume_host(host->mmc);
  1113. if (ret == 0)
  1114. host->suspended = 0;
  1115. }
  1116. return ret;
  1117. clk_en_err:
  1118. dev_dbg(mmc_dev(host->mmc),
  1119. "Failed to enable MMC clocks during resume\n");
  1120. return ret;
  1121. }
  1122. #else
  1123. #define omap_mmc_suspend NULL
  1124. #define omap_mmc_resume NULL
  1125. #endif
  1126. static struct platform_driver omap_mmc_driver = {
  1127. .probe = omap_mmc_probe,
  1128. .remove = omap_mmc_remove,
  1129. .suspend = omap_mmc_suspend,
  1130. .resume = omap_mmc_resume,
  1131. .driver = {
  1132. .name = DRIVER_NAME,
  1133. .owner = THIS_MODULE,
  1134. },
  1135. };
  1136. static int __init omap_mmc_init(void)
  1137. {
  1138. /* Register the MMC driver */
  1139. return platform_driver_register(&omap_mmc_driver);
  1140. }
  1141. static void __exit omap_mmc_cleanup(void)
  1142. {
  1143. /* Unregister MMC driver */
  1144. platform_driver_unregister(&omap_mmc_driver);
  1145. }
  1146. module_init(omap_mmc_init);
  1147. module_exit(omap_mmc_cleanup);
  1148. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  1149. MODULE_LICENSE("GPL");
  1150. MODULE_ALIAS("platform:" DRIVER_NAME);
  1151. MODULE_AUTHOR("Texas Instruments Inc");