omap_hsmmc.c 34 KB

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