omap_hsmmc.c 32 KB

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