omap_hsmmc.c 32 KB

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