omap_hsmmc.c 33 KB

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