omap_hsmmc.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  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/mmc/core.h>
  30. #include <linux/io.h>
  31. #include <linux/semaphore.h>
  32. #include <mach/dma.h>
  33. #include <mach/hardware.h>
  34. #include <mach/board.h>
  35. #include <mach/mmc.h>
  36. #include <mach/cpu.h>
  37. /* OMAP HSMMC Host Controller Registers */
  38. #define OMAP_HSMMC_SYSCONFIG 0x0010
  39. #define OMAP_HSMMC_SYSSTATUS 0x0014
  40. #define OMAP_HSMMC_CON 0x002C
  41. #define OMAP_HSMMC_BLK 0x0104
  42. #define OMAP_HSMMC_ARG 0x0108
  43. #define OMAP_HSMMC_CMD 0x010C
  44. #define OMAP_HSMMC_RSP10 0x0110
  45. #define OMAP_HSMMC_RSP32 0x0114
  46. #define OMAP_HSMMC_RSP54 0x0118
  47. #define OMAP_HSMMC_RSP76 0x011C
  48. #define OMAP_HSMMC_DATA 0x0120
  49. #define OMAP_HSMMC_HCTL 0x0128
  50. #define OMAP_HSMMC_SYSCTL 0x012C
  51. #define OMAP_HSMMC_STAT 0x0130
  52. #define OMAP_HSMMC_IE 0x0134
  53. #define OMAP_HSMMC_ISE 0x0138
  54. #define OMAP_HSMMC_CAPA 0x0140
  55. #define VS18 (1 << 26)
  56. #define VS30 (1 << 25)
  57. #define SDVS18 (0x5 << 9)
  58. #define SDVS30 (0x6 << 9)
  59. #define SDVS33 (0x7 << 9)
  60. #define SDVS_MASK 0x00000E00
  61. #define SDVSCLR 0xFFFFF1FF
  62. #define SDVSDET 0x00000400
  63. #define AUTOIDLE 0x1
  64. #define SDBP (1 << 8)
  65. #define DTO 0xe
  66. #define ICE 0x1
  67. #define ICS 0x2
  68. #define CEN (1 << 2)
  69. #define CLKD_MASK 0x0000FFC0
  70. #define CLKD_SHIFT 6
  71. #define DTO_MASK 0x000F0000
  72. #define DTO_SHIFT 16
  73. #define INT_EN_MASK 0x307F0033
  74. #define BWR_ENABLE (1 << 4)
  75. #define BRR_ENABLE (1 << 5)
  76. #define INIT_STREAM (1 << 1)
  77. #define DP_SELECT (1 << 21)
  78. #define DDIR (1 << 4)
  79. #define DMA_EN 0x1
  80. #define MSBS (1 << 5)
  81. #define BCE (1 << 1)
  82. #define FOUR_BIT (1 << 1)
  83. #define DW8 (1 << 5)
  84. #define CC 0x1
  85. #define TC 0x02
  86. #define OD 0x1
  87. #define ERR (1 << 15)
  88. #define CMD_TIMEOUT (1 << 16)
  89. #define DATA_TIMEOUT (1 << 20)
  90. #define CMD_CRC (1 << 17)
  91. #define DATA_CRC (1 << 21)
  92. #define CARD_ERR (1 << 28)
  93. #define STAT_CLEAR 0xFFFFFFFF
  94. #define INIT_STREAM_CMD 0x00000000
  95. #define DUAL_VOLT_OCR_BIT 7
  96. #define SRC (1 << 25)
  97. #define SRD (1 << 26)
  98. #define SOFTRESET (1 << 1)
  99. #define RESETDONE (1 << 0)
  100. /*
  101. * FIXME: Most likely all the data using these _DEVID defines should come
  102. * from the platform_data, or implemented in controller and slot specific
  103. * functions.
  104. */
  105. #define OMAP_MMC1_DEVID 0
  106. #define OMAP_MMC2_DEVID 1
  107. #define OMAP_MMC3_DEVID 2
  108. #define MMC_TIMEOUT_MS 20
  109. #define OMAP_MMC_MASTER_CLOCK 96000000
  110. #define DRIVER_NAME "mmci-omap-hs"
  111. /* Timeouts for entering power saving states on inactivity, msec */
  112. #define OMAP_MMC_DISABLED_TIMEOUT 100
  113. #define OMAP_MMC_SLEEP_TIMEOUT 1000
  114. #define OMAP_MMC_OFF_TIMEOUT 8000
  115. /*
  116. * One controller can have multiple slots, like on some omap boards using
  117. * omap.c controller driver. Luckily this is not currently done on any known
  118. * omap_hsmmc.c device.
  119. */
  120. #define mmc_slot(host) (host->pdata->slots[host->slot_id])
  121. /*
  122. * MMC Host controller read/write API's
  123. */
  124. #define OMAP_HSMMC_READ(base, reg) \
  125. __raw_readl((base) + OMAP_HSMMC_##reg)
  126. #define OMAP_HSMMC_WRITE(base, reg, val) \
  127. __raw_writel((val), (base) + OMAP_HSMMC_##reg)
  128. struct mmc_omap_host {
  129. struct device *dev;
  130. struct mmc_host *mmc;
  131. struct mmc_request *mrq;
  132. struct mmc_command *cmd;
  133. struct mmc_data *data;
  134. struct clk *fclk;
  135. struct clk *iclk;
  136. struct clk *dbclk;
  137. struct semaphore sem;
  138. struct work_struct mmc_carddetect_work;
  139. void __iomem *base;
  140. resource_size_t mapbase;
  141. unsigned int id;
  142. unsigned int dma_len;
  143. unsigned int dma_sg_idx;
  144. unsigned char bus_mode;
  145. unsigned char power_mode;
  146. u32 *buffer;
  147. u32 bytesleft;
  148. int suspended;
  149. int irq;
  150. int use_dma, dma_ch;
  151. int dma_line_tx, dma_line_rx;
  152. int slot_id;
  153. int dbclk_enabled;
  154. int response_busy;
  155. int context_loss;
  156. int dpm_state;
  157. int vdd;
  158. struct omap_mmc_platform_data *pdata;
  159. };
  160. /*
  161. * Stop clock to the card
  162. */
  163. static void omap_mmc_stop_clock(struct mmc_omap_host *host)
  164. {
  165. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  166. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  167. if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
  168. dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
  169. }
  170. #ifdef CONFIG_PM
  171. /*
  172. * Restore the MMC host context, if it was lost as result of a
  173. * power state change.
  174. */
  175. static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
  176. {
  177. struct mmc_ios *ios = &host->mmc->ios;
  178. struct omap_mmc_platform_data *pdata = host->pdata;
  179. int context_loss = 0;
  180. u32 hctl, capa, con;
  181. u16 dsor = 0;
  182. unsigned long timeout;
  183. if (pdata->get_context_loss_count) {
  184. context_loss = pdata->get_context_loss_count(host->dev);
  185. if (context_loss < 0)
  186. return 1;
  187. }
  188. dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
  189. context_loss == host->context_loss ? "not " : "");
  190. if (host->context_loss == context_loss)
  191. return 1;
  192. /* Wait for hardware reset */
  193. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  194. while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
  195. && time_before(jiffies, timeout))
  196. ;
  197. /* Do software reset */
  198. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
  199. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  200. while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
  201. && time_before(jiffies, timeout))
  202. ;
  203. OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
  204. OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
  205. if (host->id == OMAP_MMC1_DEVID) {
  206. if (host->power_mode != MMC_POWER_OFF &&
  207. (1 << ios->vdd) <= MMC_VDD_23_24)
  208. hctl = SDVS18;
  209. else
  210. hctl = SDVS30;
  211. capa = VS30 | VS18;
  212. } else {
  213. hctl = SDVS18;
  214. capa = VS18;
  215. }
  216. OMAP_HSMMC_WRITE(host->base, HCTL,
  217. OMAP_HSMMC_READ(host->base, HCTL) | hctl);
  218. OMAP_HSMMC_WRITE(host->base, CAPA,
  219. OMAP_HSMMC_READ(host->base, CAPA) | capa);
  220. OMAP_HSMMC_WRITE(host->base, HCTL,
  221. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  222. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  223. while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
  224. && time_before(jiffies, timeout))
  225. ;
  226. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  227. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  228. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  229. /* Do not initialize card-specific things if the power is off */
  230. if (host->power_mode == MMC_POWER_OFF)
  231. goto out;
  232. con = OMAP_HSMMC_READ(host->base, CON);
  233. switch (ios->bus_width) {
  234. case MMC_BUS_WIDTH_8:
  235. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  236. break;
  237. case MMC_BUS_WIDTH_4:
  238. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  239. OMAP_HSMMC_WRITE(host->base, HCTL,
  240. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  241. break;
  242. case MMC_BUS_WIDTH_1:
  243. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  244. OMAP_HSMMC_WRITE(host->base, HCTL,
  245. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  246. break;
  247. }
  248. if (ios->clock) {
  249. dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
  250. if (dsor < 1)
  251. dsor = 1;
  252. if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
  253. dsor++;
  254. if (dsor > 250)
  255. dsor = 250;
  256. }
  257. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  258. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  259. OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
  260. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  261. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  262. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  263. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  264. && time_before(jiffies, timeout))
  265. ;
  266. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  267. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  268. con = OMAP_HSMMC_READ(host->base, CON);
  269. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  270. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  271. else
  272. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  273. out:
  274. host->context_loss = context_loss;
  275. dev_dbg(mmc_dev(host->mmc), "context is restored\n");
  276. return 0;
  277. }
  278. /*
  279. * Save the MMC host context (store the number of power state changes so far).
  280. */
  281. static void omap_mmc_save_ctx(struct mmc_omap_host *host)
  282. {
  283. struct omap_mmc_platform_data *pdata = host->pdata;
  284. int context_loss;
  285. if (pdata->get_context_loss_count) {
  286. context_loss = pdata->get_context_loss_count(host->dev);
  287. if (context_loss < 0)
  288. return;
  289. host->context_loss = context_loss;
  290. }
  291. }
  292. #else
  293. static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
  294. {
  295. return 0;
  296. }
  297. static void omap_mmc_save_ctx(struct mmc_omap_host *host)
  298. {
  299. }
  300. #endif
  301. /*
  302. * Send init stream sequence to card
  303. * before sending IDLE command
  304. */
  305. static void send_init_stream(struct mmc_omap_host *host)
  306. {
  307. int reg = 0;
  308. unsigned long timeout;
  309. disable_irq(host->irq);
  310. OMAP_HSMMC_WRITE(host->base, CON,
  311. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  312. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  313. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  314. while ((reg != CC) && time_before(jiffies, timeout))
  315. reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
  316. OMAP_HSMMC_WRITE(host->base, CON,
  317. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  318. enable_irq(host->irq);
  319. }
  320. static inline
  321. int mmc_omap_cover_is_closed(struct mmc_omap_host *host)
  322. {
  323. int r = 1;
  324. if (mmc_slot(host).get_cover_state)
  325. r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
  326. return r;
  327. }
  328. static ssize_t
  329. mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
  330. char *buf)
  331. {
  332. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  333. struct mmc_omap_host *host = mmc_priv(mmc);
  334. return sprintf(buf, "%s\n", mmc_omap_cover_is_closed(host) ? "closed" :
  335. "open");
  336. }
  337. static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
  338. static ssize_t
  339. mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
  340. char *buf)
  341. {
  342. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  343. struct mmc_omap_host *host = mmc_priv(mmc);
  344. return sprintf(buf, "%s\n", mmc_slot(host).name);
  345. }
  346. static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
  347. /*
  348. * Configure the response type and send the cmd.
  349. */
  350. static void
  351. mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
  352. struct mmc_data *data)
  353. {
  354. int cmdreg = 0, resptype = 0, cmdtype = 0;
  355. dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  356. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  357. host->cmd = cmd;
  358. /*
  359. * Clear status bits and enable interrupts
  360. */
  361. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  362. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  363. if (host->use_dma)
  364. OMAP_HSMMC_WRITE(host->base, IE,
  365. INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE));
  366. else
  367. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  368. host->response_busy = 0;
  369. if (cmd->flags & MMC_RSP_PRESENT) {
  370. if (cmd->flags & MMC_RSP_136)
  371. resptype = 1;
  372. else if (cmd->flags & MMC_RSP_BUSY) {
  373. resptype = 3;
  374. host->response_busy = 1;
  375. } else
  376. resptype = 2;
  377. }
  378. /*
  379. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  380. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  381. * a val of 0x3, rest 0x0.
  382. */
  383. if (cmd == host->mrq->stop)
  384. cmdtype = 0x3;
  385. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  386. if (data) {
  387. cmdreg |= DP_SELECT | MSBS | BCE;
  388. if (data->flags & MMC_DATA_READ)
  389. cmdreg |= DDIR;
  390. else
  391. cmdreg &= ~(DDIR);
  392. }
  393. if (host->use_dma)
  394. cmdreg |= DMA_EN;
  395. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  396. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  397. }
  398. static int
  399. mmc_omap_get_dma_dir(struct mmc_omap_host *host, struct mmc_data *data)
  400. {
  401. if (data->flags & MMC_DATA_WRITE)
  402. return DMA_TO_DEVICE;
  403. else
  404. return DMA_FROM_DEVICE;
  405. }
  406. /*
  407. * Notify the transfer complete to MMC core
  408. */
  409. static void
  410. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  411. {
  412. if (!data) {
  413. struct mmc_request *mrq = host->mrq;
  414. host->mrq = NULL;
  415. mmc_request_done(host->mmc, mrq);
  416. return;
  417. }
  418. host->data = NULL;
  419. if (host->use_dma && host->dma_ch != -1)
  420. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
  421. mmc_omap_get_dma_dir(host, data));
  422. if (!data->error)
  423. data->bytes_xfered += data->blocks * (data->blksz);
  424. else
  425. data->bytes_xfered = 0;
  426. if (!data->stop) {
  427. host->mrq = NULL;
  428. mmc_request_done(host->mmc, data->mrq);
  429. return;
  430. }
  431. mmc_omap_start_command(host, data->stop, NULL);
  432. }
  433. /*
  434. * Notify the core about command completion
  435. */
  436. static void
  437. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  438. {
  439. host->cmd = NULL;
  440. if (cmd->flags & MMC_RSP_PRESENT) {
  441. if (cmd->flags & MMC_RSP_136) {
  442. /* response type 2 */
  443. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  444. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  445. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  446. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  447. } else {
  448. /* response types 1, 1b, 3, 4, 5, 6 */
  449. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  450. }
  451. }
  452. if ((host->data == NULL && !host->response_busy) || cmd->error) {
  453. host->mrq = NULL;
  454. mmc_request_done(host->mmc, cmd->mrq);
  455. }
  456. }
  457. /*
  458. * DMA clean up for command errors
  459. */
  460. static void mmc_dma_cleanup(struct mmc_omap_host *host, int errno)
  461. {
  462. host->data->error = errno;
  463. if (host->use_dma && host->dma_ch != -1) {
  464. dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
  465. mmc_omap_get_dma_dir(host, host->data));
  466. omap_free_dma(host->dma_ch);
  467. host->dma_ch = -1;
  468. up(&host->sem);
  469. }
  470. host->data = NULL;
  471. }
  472. /*
  473. * Readable error output
  474. */
  475. #ifdef CONFIG_MMC_DEBUG
  476. static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
  477. {
  478. /* --- means reserved bit without definition at documentation */
  479. static const char *mmc_omap_status_bits[] = {
  480. "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
  481. "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
  482. "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
  483. "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
  484. };
  485. char res[256];
  486. char *buf = res;
  487. int len, i;
  488. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  489. buf += len;
  490. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  491. if (status & (1 << i)) {
  492. len = sprintf(buf, " %s", mmc_omap_status_bits[i]);
  493. buf += len;
  494. }
  495. dev_dbg(mmc_dev(host->mmc), "%s\n", res);
  496. }
  497. #endif /* CONFIG_MMC_DEBUG */
  498. /*
  499. * MMC controller internal state machines reset
  500. *
  501. * Used to reset command or data internal state machines, using respectively
  502. * SRC or SRD bit of SYSCTL register
  503. * Can be called from interrupt context
  504. */
  505. static inline void mmc_omap_reset_controller_fsm(struct mmc_omap_host *host,
  506. unsigned long bit)
  507. {
  508. unsigned long i = 0;
  509. unsigned long limit = (loops_per_jiffy *
  510. msecs_to_jiffies(MMC_TIMEOUT_MS));
  511. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  512. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  513. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  514. (i++ < limit))
  515. cpu_relax();
  516. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  517. dev_err(mmc_dev(host->mmc),
  518. "Timeout waiting on controller reset in %s\n",
  519. __func__);
  520. }
  521. /*
  522. * MMC controller IRQ handler
  523. */
  524. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  525. {
  526. struct mmc_omap_host *host = dev_id;
  527. struct mmc_data *data;
  528. int end_cmd = 0, end_trans = 0, status;
  529. if (host->mrq == NULL) {
  530. OMAP_HSMMC_WRITE(host->base, STAT,
  531. OMAP_HSMMC_READ(host->base, STAT));
  532. /* Flush posted write */
  533. OMAP_HSMMC_READ(host->base, STAT);
  534. return IRQ_HANDLED;
  535. }
  536. data = host->data;
  537. status = OMAP_HSMMC_READ(host->base, STAT);
  538. dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  539. if (status & ERR) {
  540. #ifdef CONFIG_MMC_DEBUG
  541. mmc_omap_report_irq(host, status);
  542. #endif
  543. if ((status & CMD_TIMEOUT) ||
  544. (status & CMD_CRC)) {
  545. if (host->cmd) {
  546. if (status & CMD_TIMEOUT) {
  547. mmc_omap_reset_controller_fsm(host,
  548. SRC);
  549. host->cmd->error = -ETIMEDOUT;
  550. } else {
  551. host->cmd->error = -EILSEQ;
  552. }
  553. end_cmd = 1;
  554. }
  555. if (host->data || host->response_busy) {
  556. if (host->data)
  557. mmc_dma_cleanup(host, -ETIMEDOUT);
  558. host->response_busy = 0;
  559. mmc_omap_reset_controller_fsm(host, SRD);
  560. }
  561. }
  562. if ((status & DATA_TIMEOUT) ||
  563. (status & DATA_CRC)) {
  564. if (host->data || host->response_busy) {
  565. int err = (status & DATA_TIMEOUT) ?
  566. -ETIMEDOUT : -EILSEQ;
  567. if (host->data)
  568. mmc_dma_cleanup(host, err);
  569. else
  570. host->mrq->cmd->error = err;
  571. host->response_busy = 0;
  572. mmc_omap_reset_controller_fsm(host, SRD);
  573. end_trans = 1;
  574. }
  575. }
  576. if (status & CARD_ERR) {
  577. dev_dbg(mmc_dev(host->mmc),
  578. "Ignoring card err CMD%d\n", host->cmd->opcode);
  579. if (host->cmd)
  580. end_cmd = 1;
  581. if (host->data)
  582. end_trans = 1;
  583. }
  584. }
  585. OMAP_HSMMC_WRITE(host->base, STAT, status);
  586. /* Flush posted write */
  587. OMAP_HSMMC_READ(host->base, STAT);
  588. if (end_cmd || ((status & CC) && host->cmd))
  589. mmc_omap_cmd_done(host, host->cmd);
  590. if ((end_trans || (status & TC)) && host->mrq)
  591. mmc_omap_xfer_done(host, data);
  592. return IRQ_HANDLED;
  593. }
  594. static void set_sd_bus_power(struct mmc_omap_host *host)
  595. {
  596. unsigned long i;
  597. OMAP_HSMMC_WRITE(host->base, HCTL,
  598. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  599. for (i = 0; i < loops_per_jiffy; i++) {
  600. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  601. break;
  602. cpu_relax();
  603. }
  604. }
  605. /*
  606. * Switch MMC interface voltage ... only relevant for MMC1.
  607. *
  608. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  609. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  610. * Some chips, like eMMC ones, use internal transceivers.
  611. */
  612. static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
  613. {
  614. u32 reg_val = 0;
  615. int ret;
  616. /* Disable the clocks */
  617. clk_disable(host->fclk);
  618. clk_disable(host->iclk);
  619. clk_disable(host->dbclk);
  620. /* Turn the power off */
  621. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  622. if (ret != 0)
  623. goto err;
  624. /* Turn the power ON with given VDD 1.8 or 3.0v */
  625. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
  626. if (ret != 0)
  627. goto err;
  628. clk_enable(host->fclk);
  629. clk_enable(host->iclk);
  630. clk_enable(host->dbclk);
  631. OMAP_HSMMC_WRITE(host->base, HCTL,
  632. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  633. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  634. /*
  635. * If a MMC dual voltage card is detected, the set_ios fn calls
  636. * this fn with VDD bit set for 1.8V. Upon card removal from the
  637. * slot, omap_mmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  638. *
  639. * Cope with a bit of slop in the range ... per data sheets:
  640. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  641. * but recommended values are 1.71V to 1.89V
  642. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  643. * but recommended values are 2.7V to 3.3V
  644. *
  645. * Board setup code shouldn't permit anything very out-of-range.
  646. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  647. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  648. */
  649. if ((1 << vdd) <= MMC_VDD_23_24)
  650. reg_val |= SDVS18;
  651. else
  652. reg_val |= SDVS30;
  653. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  654. set_sd_bus_power(host);
  655. return 0;
  656. err:
  657. dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  658. return ret;
  659. }
  660. /*
  661. * Work Item to notify the core about card insertion/removal
  662. */
  663. static void mmc_omap_detect(struct work_struct *work)
  664. {
  665. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  666. mmc_carddetect_work);
  667. struct omap_mmc_slot_data *slot = &mmc_slot(host);
  668. int carddetect;
  669. if (host->suspended)
  670. return;
  671. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  672. if (slot->card_detect)
  673. carddetect = slot->card_detect(slot->card_detect_irq);
  674. else
  675. carddetect = -ENOSYS;
  676. if (carddetect) {
  677. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  678. } else {
  679. mmc_host_enable(host->mmc);
  680. mmc_omap_reset_controller_fsm(host, SRD);
  681. mmc_host_lazy_disable(host->mmc);
  682. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  683. }
  684. }
  685. /*
  686. * ISR for handling card insertion and removal
  687. */
  688. static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
  689. {
  690. struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
  691. if (host->suspended)
  692. return IRQ_HANDLED;
  693. schedule_work(&host->mmc_carddetect_work);
  694. return IRQ_HANDLED;
  695. }
  696. static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host *host,
  697. struct mmc_data *data)
  698. {
  699. int sync_dev;
  700. if (data->flags & MMC_DATA_WRITE)
  701. sync_dev = host->dma_line_tx;
  702. else
  703. sync_dev = host->dma_line_rx;
  704. return sync_dev;
  705. }
  706. static void mmc_omap_config_dma_params(struct mmc_omap_host *host,
  707. struct mmc_data *data,
  708. struct scatterlist *sgl)
  709. {
  710. int blksz, nblk, dma_ch;
  711. dma_ch = host->dma_ch;
  712. if (data->flags & MMC_DATA_WRITE) {
  713. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  714. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  715. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  716. sg_dma_address(sgl), 0, 0);
  717. } else {
  718. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  719. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  720. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  721. sg_dma_address(sgl), 0, 0);
  722. }
  723. blksz = host->data->blksz;
  724. nblk = sg_dma_len(sgl) / blksz;
  725. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
  726. blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
  727. mmc_omap_get_dma_sync_dev(host, data),
  728. !(data->flags & MMC_DATA_WRITE));
  729. omap_start_dma(dma_ch);
  730. }
  731. /*
  732. * DMA call back function
  733. */
  734. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  735. {
  736. struct mmc_omap_host *host = data;
  737. if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
  738. dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
  739. if (host->dma_ch < 0)
  740. return;
  741. host->dma_sg_idx++;
  742. if (host->dma_sg_idx < host->dma_len) {
  743. /* Fire up the next transfer. */
  744. mmc_omap_config_dma_params(host, host->data,
  745. host->data->sg + host->dma_sg_idx);
  746. return;
  747. }
  748. omap_free_dma(host->dma_ch);
  749. host->dma_ch = -1;
  750. /*
  751. * DMA Callback: run in interrupt context.
  752. * mutex_unlock will throw a kernel warning if used.
  753. */
  754. up(&host->sem);
  755. }
  756. /*
  757. * Routine to configure and start DMA for the MMC card
  758. */
  759. static int
  760. mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
  761. {
  762. int dma_ch = 0, ret = 0, err = 1, i;
  763. struct mmc_data *data = req->data;
  764. /* Sanity check: all the SG entries must be aligned by block size. */
  765. for (i = 0; i < data->sg_len; i++) {
  766. struct scatterlist *sgl;
  767. sgl = data->sg + i;
  768. if (sgl->length % data->blksz)
  769. return -EINVAL;
  770. }
  771. if ((data->blksz % 4) != 0)
  772. /* REVISIT: The MMC buffer increments only when MSB is written.
  773. * Return error for blksz which is non multiple of four.
  774. */
  775. return -EINVAL;
  776. /*
  777. * If for some reason the DMA transfer is still active,
  778. * we wait for timeout period and free the dma
  779. */
  780. if (host->dma_ch != -1) {
  781. set_current_state(TASK_UNINTERRUPTIBLE);
  782. schedule_timeout(100);
  783. if (down_trylock(&host->sem)) {
  784. omap_free_dma(host->dma_ch);
  785. host->dma_ch = -1;
  786. up(&host->sem);
  787. return err;
  788. }
  789. } else {
  790. if (down_trylock(&host->sem))
  791. return err;
  792. }
  793. ret = omap_request_dma(mmc_omap_get_dma_sync_dev(host, data), "MMC/SD",
  794. mmc_omap_dma_cb, host, &dma_ch);
  795. if (ret != 0) {
  796. dev_err(mmc_dev(host->mmc),
  797. "%s: omap_request_dma() failed with %d\n",
  798. mmc_hostname(host->mmc), ret);
  799. return ret;
  800. }
  801. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  802. data->sg_len, mmc_omap_get_dma_dir(host, data));
  803. host->dma_ch = dma_ch;
  804. host->dma_sg_idx = 0;
  805. mmc_omap_config_dma_params(host, data, data->sg);
  806. return 0;
  807. }
  808. static void set_data_timeout(struct mmc_omap_host *host,
  809. struct mmc_request *req)
  810. {
  811. unsigned int timeout, cycle_ns;
  812. uint32_t reg, clkd, dto = 0;
  813. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  814. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  815. if (clkd == 0)
  816. clkd = 1;
  817. cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
  818. timeout = req->data->timeout_ns / cycle_ns;
  819. timeout += req->data->timeout_clks;
  820. if (timeout) {
  821. while ((timeout & 0x80000000) == 0) {
  822. dto += 1;
  823. timeout <<= 1;
  824. }
  825. dto = 31 - dto;
  826. timeout <<= 1;
  827. if (timeout && dto)
  828. dto += 1;
  829. if (dto >= 13)
  830. dto -= 13;
  831. else
  832. dto = 0;
  833. if (dto > 14)
  834. dto = 14;
  835. }
  836. reg &= ~DTO_MASK;
  837. reg |= dto << DTO_SHIFT;
  838. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  839. }
  840. /*
  841. * Configure block length for MMC/SD cards and initiate the transfer.
  842. */
  843. static int
  844. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  845. {
  846. int ret;
  847. host->data = req->data;
  848. if (req->data == NULL) {
  849. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  850. return 0;
  851. }
  852. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  853. | (req->data->blocks << 16));
  854. set_data_timeout(host, req);
  855. if (host->use_dma) {
  856. ret = mmc_omap_start_dma_transfer(host, req);
  857. if (ret != 0) {
  858. dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
  859. return ret;
  860. }
  861. }
  862. return 0;
  863. }
  864. /*
  865. * Request function. for read/write operation
  866. */
  867. static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
  868. {
  869. struct mmc_omap_host *host = mmc_priv(mmc);
  870. int err;
  871. WARN_ON(host->mrq != NULL);
  872. host->mrq = req;
  873. err = mmc_omap_prepare_data(host, req);
  874. if (err) {
  875. req->cmd->error = err;
  876. if (req->data)
  877. req->data->error = err;
  878. host->mrq = NULL;
  879. mmc_request_done(mmc, req);
  880. return;
  881. }
  882. mmc_omap_start_command(host, req->cmd, req->data);
  883. }
  884. /* Routine to configure clock values. Exposed API to core */
  885. static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  886. {
  887. struct mmc_omap_host *host = mmc_priv(mmc);
  888. u16 dsor = 0;
  889. unsigned long regval;
  890. unsigned long timeout;
  891. u32 con;
  892. int do_send_init_stream = 0;
  893. mmc_host_enable(host->mmc);
  894. if (ios->power_mode != host->power_mode) {
  895. switch (ios->power_mode) {
  896. case MMC_POWER_OFF:
  897. mmc_slot(host).set_power(host->dev, host->slot_id,
  898. 0, 0);
  899. host->vdd = 0;
  900. break;
  901. case MMC_POWER_UP:
  902. mmc_slot(host).set_power(host->dev, host->slot_id,
  903. 1, ios->vdd);
  904. host->vdd = ios->vdd;
  905. break;
  906. case MMC_POWER_ON:
  907. do_send_init_stream = 1;
  908. break;
  909. }
  910. host->power_mode = ios->power_mode;
  911. }
  912. /* FIXME: set registers based only on changes to ios */
  913. con = OMAP_HSMMC_READ(host->base, CON);
  914. switch (mmc->ios.bus_width) {
  915. case MMC_BUS_WIDTH_8:
  916. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  917. break;
  918. case MMC_BUS_WIDTH_4:
  919. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  920. OMAP_HSMMC_WRITE(host->base, HCTL,
  921. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  922. break;
  923. case MMC_BUS_WIDTH_1:
  924. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  925. OMAP_HSMMC_WRITE(host->base, HCTL,
  926. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  927. break;
  928. }
  929. if (host->id == OMAP_MMC1_DEVID) {
  930. /* Only MMC1 can interface at 3V without some flavor
  931. * of external transceiver; but they all handle 1.8V.
  932. */
  933. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  934. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  935. /*
  936. * The mmc_select_voltage fn of the core does
  937. * not seem to set the power_mode to
  938. * MMC_POWER_UP upon recalculating the voltage.
  939. * vdd 1.8v.
  940. */
  941. if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
  942. dev_dbg(mmc_dev(host->mmc),
  943. "Switch operation failed\n");
  944. }
  945. }
  946. if (ios->clock) {
  947. dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
  948. if (dsor < 1)
  949. dsor = 1;
  950. if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
  951. dsor++;
  952. if (dsor > 250)
  953. dsor = 250;
  954. }
  955. omap_mmc_stop_clock(host);
  956. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  957. regval = regval & ~(CLKD_MASK);
  958. regval = regval | (dsor << 6) | (DTO << 16);
  959. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  960. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  961. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  962. /* Wait till the ICS bit is set */
  963. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  964. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  965. && time_before(jiffies, timeout))
  966. msleep(1);
  967. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  968. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  969. if (do_send_init_stream)
  970. send_init_stream(host);
  971. con = OMAP_HSMMC_READ(host->base, CON);
  972. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  973. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  974. else
  975. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  976. if (host->power_mode == MMC_POWER_OFF)
  977. mmc_host_disable(host->mmc);
  978. else
  979. mmc_host_lazy_disable(host->mmc);
  980. }
  981. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  982. {
  983. struct mmc_omap_host *host = mmc_priv(mmc);
  984. if (!mmc_slot(host).card_detect)
  985. return -ENOSYS;
  986. return mmc_slot(host).card_detect(mmc_slot(host).card_detect_irq);
  987. }
  988. static int omap_hsmmc_get_ro(struct mmc_host *mmc)
  989. {
  990. struct mmc_omap_host *host = mmc_priv(mmc);
  991. if (!mmc_slot(host).get_ro)
  992. return -ENOSYS;
  993. return mmc_slot(host).get_ro(host->dev, 0);
  994. }
  995. static void omap_hsmmc_init(struct mmc_omap_host *host)
  996. {
  997. u32 hctl, capa, value;
  998. /* Only MMC1 supports 3.0V */
  999. if (host->id == OMAP_MMC1_DEVID) {
  1000. hctl = SDVS30;
  1001. capa = VS30 | VS18;
  1002. } else {
  1003. hctl = SDVS18;
  1004. capa = VS18;
  1005. }
  1006. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1007. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1008. value = OMAP_HSMMC_READ(host->base, CAPA);
  1009. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1010. /* Set the controller to AUTO IDLE mode */
  1011. value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
  1012. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
  1013. /* Set SD bus power bit */
  1014. set_sd_bus_power(host);
  1015. }
  1016. /*
  1017. * Dynamic power saving handling, FSM:
  1018. * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
  1019. * ^___________| | |
  1020. * |______________________|______________________|
  1021. *
  1022. * ENABLED: mmc host is fully functional
  1023. * DISABLED: fclk is off
  1024. * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
  1025. * REGSLEEP: fclk is off, voltage regulator is asleep
  1026. * OFF: fclk is off, voltage regulator is off
  1027. *
  1028. * Transition handlers return the timeout for the next state transition
  1029. * or negative error.
  1030. */
  1031. enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
  1032. /* Handler for [ENABLED -> DISABLED] transition */
  1033. static int omap_mmc_enabled_to_disabled(struct mmc_omap_host *host)
  1034. {
  1035. omap_mmc_save_ctx(host);
  1036. clk_disable(host->fclk);
  1037. host->dpm_state = DISABLED;
  1038. dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
  1039. if (host->power_mode == MMC_POWER_OFF)
  1040. return 0;
  1041. return msecs_to_jiffies(OMAP_MMC_SLEEP_TIMEOUT);
  1042. }
  1043. /* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
  1044. static int omap_mmc_disabled_to_sleep(struct mmc_omap_host *host)
  1045. {
  1046. int err, new_state;
  1047. if (!mmc_try_claim_host(host->mmc))
  1048. return 0;
  1049. clk_enable(host->fclk);
  1050. omap_mmc_restore_ctx(host);
  1051. if (mmc_card_can_sleep(host->mmc)) {
  1052. err = mmc_card_sleep(host->mmc);
  1053. if (err < 0) {
  1054. clk_disable(host->fclk);
  1055. mmc_release_host(host->mmc);
  1056. return err;
  1057. }
  1058. new_state = CARDSLEEP;
  1059. } else
  1060. new_state = REGSLEEP;
  1061. if (mmc_slot(host).set_sleep)
  1062. mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
  1063. new_state == CARDSLEEP);
  1064. /* FIXME: turn off bus power and perhaps interrupts too */
  1065. clk_disable(host->fclk);
  1066. host->dpm_state = new_state;
  1067. mmc_release_host(host->mmc);
  1068. dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
  1069. host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
  1070. if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
  1071. mmc_slot(host).card_detect ||
  1072. (mmc_slot(host).get_cover_state &&
  1073. mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
  1074. return msecs_to_jiffies(OMAP_MMC_OFF_TIMEOUT);
  1075. return 0;
  1076. }
  1077. /* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
  1078. static int omap_mmc_sleep_to_off(struct mmc_omap_host *host)
  1079. {
  1080. if (!mmc_try_claim_host(host->mmc))
  1081. return 0;
  1082. if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
  1083. mmc_slot(host).card_detect ||
  1084. (mmc_slot(host).get_cover_state &&
  1085. mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
  1086. mmc_release_host(host->mmc);
  1087. return 0;
  1088. }
  1089. mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  1090. host->vdd = 0;
  1091. host->power_mode = MMC_POWER_OFF;
  1092. dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
  1093. host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
  1094. host->dpm_state = OFF;
  1095. mmc_release_host(host->mmc);
  1096. return 0;
  1097. }
  1098. /* Handler for [DISABLED -> ENABLED] transition */
  1099. static int omap_mmc_disabled_to_enabled(struct mmc_omap_host *host)
  1100. {
  1101. int err;
  1102. err = clk_enable(host->fclk);
  1103. if (err < 0)
  1104. return err;
  1105. omap_mmc_restore_ctx(host);
  1106. host->dpm_state = ENABLED;
  1107. dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
  1108. return 0;
  1109. }
  1110. /* Handler for [SLEEP -> ENABLED] transition */
  1111. static int omap_mmc_sleep_to_enabled(struct mmc_omap_host *host)
  1112. {
  1113. if (!mmc_try_claim_host(host->mmc))
  1114. return 0;
  1115. clk_enable(host->fclk);
  1116. omap_mmc_restore_ctx(host);
  1117. if (mmc_slot(host).set_sleep)
  1118. mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
  1119. host->vdd, host->dpm_state == CARDSLEEP);
  1120. if (mmc_card_can_sleep(host->mmc))
  1121. mmc_card_awake(host->mmc);
  1122. dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
  1123. host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
  1124. host->dpm_state = ENABLED;
  1125. mmc_release_host(host->mmc);
  1126. return 0;
  1127. }
  1128. /* Handler for [OFF -> ENABLED] transition */
  1129. static int omap_mmc_off_to_enabled(struct mmc_omap_host *host)
  1130. {
  1131. clk_enable(host->fclk);
  1132. omap_mmc_restore_ctx(host);
  1133. omap_hsmmc_init(host);
  1134. mmc_power_restore_host(host->mmc);
  1135. host->dpm_state = ENABLED;
  1136. dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
  1137. return 0;
  1138. }
  1139. /*
  1140. * Bring MMC host to ENABLED from any other PM state.
  1141. */
  1142. static int omap_mmc_enable(struct mmc_host *mmc)
  1143. {
  1144. struct mmc_omap_host *host = mmc_priv(mmc);
  1145. switch (host->dpm_state) {
  1146. case DISABLED:
  1147. return omap_mmc_disabled_to_enabled(host);
  1148. case CARDSLEEP:
  1149. case REGSLEEP:
  1150. return omap_mmc_sleep_to_enabled(host);
  1151. case OFF:
  1152. return omap_mmc_off_to_enabled(host);
  1153. default:
  1154. dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
  1155. return -EINVAL;
  1156. }
  1157. }
  1158. /*
  1159. * Bring MMC host in PM state (one level deeper).
  1160. */
  1161. static int omap_mmc_disable(struct mmc_host *mmc, int lazy)
  1162. {
  1163. struct mmc_omap_host *host = mmc_priv(mmc);
  1164. switch (host->dpm_state) {
  1165. case ENABLED: {
  1166. int delay;
  1167. delay = omap_mmc_enabled_to_disabled(host);
  1168. if (lazy || delay < 0)
  1169. return delay;
  1170. return 0;
  1171. }
  1172. case DISABLED:
  1173. return omap_mmc_disabled_to_sleep(host);
  1174. case CARDSLEEP:
  1175. case REGSLEEP:
  1176. return omap_mmc_sleep_to_off(host);
  1177. default:
  1178. dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
  1179. return -EINVAL;
  1180. }
  1181. }
  1182. static int omap_mmc_enable_fclk(struct mmc_host *mmc)
  1183. {
  1184. struct mmc_omap_host *host = mmc_priv(mmc);
  1185. int err;
  1186. err = clk_enable(host->fclk);
  1187. if (err)
  1188. return err;
  1189. dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
  1190. omap_mmc_restore_ctx(host);
  1191. return 0;
  1192. }
  1193. static int omap_mmc_disable_fclk(struct mmc_host *mmc, int lazy)
  1194. {
  1195. struct mmc_omap_host *host = mmc_priv(mmc);
  1196. omap_mmc_save_ctx(host);
  1197. clk_disable(host->fclk);
  1198. dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
  1199. return 0;
  1200. }
  1201. static const struct mmc_host_ops mmc_omap_ops = {
  1202. .enable = omap_mmc_enable_fclk,
  1203. .disable = omap_mmc_disable_fclk,
  1204. .request = omap_mmc_request,
  1205. .set_ios = omap_mmc_set_ios,
  1206. .get_cd = omap_hsmmc_get_cd,
  1207. .get_ro = omap_hsmmc_get_ro,
  1208. /* NYET -- enable_sdio_irq */
  1209. };
  1210. static const struct mmc_host_ops mmc_omap_ps_ops = {
  1211. .enable = omap_mmc_enable,
  1212. .disable = omap_mmc_disable,
  1213. .request = omap_mmc_request,
  1214. .set_ios = omap_mmc_set_ios,
  1215. .get_cd = omap_hsmmc_get_cd,
  1216. .get_ro = omap_hsmmc_get_ro,
  1217. /* NYET -- enable_sdio_irq */
  1218. };
  1219. #ifdef CONFIG_DEBUG_FS
  1220. static int mmc_regs_show(struct seq_file *s, void *data)
  1221. {
  1222. struct mmc_host *mmc = s->private;
  1223. struct mmc_omap_host *host = mmc_priv(mmc);
  1224. struct omap_mmc_platform_data *pdata = host->pdata;
  1225. int context_loss = 0;
  1226. if (pdata->get_context_loss_count)
  1227. context_loss = pdata->get_context_loss_count(host->dev);
  1228. seq_printf(s, "mmc%d:\n"
  1229. " enabled:\t%d\n"
  1230. " dpm_state:\t%d\n"
  1231. " nesting_cnt:\t%d\n"
  1232. " ctx_loss:\t%d:%d\n"
  1233. "\nregs:\n",
  1234. mmc->index, mmc->enabled ? 1 : 0,
  1235. host->dpm_state, mmc->nesting_cnt,
  1236. host->context_loss, context_loss);
  1237. if (host->suspended || host->dpm_state == OFF) {
  1238. seq_printf(s, "host suspended, can't read registers\n");
  1239. return 0;
  1240. }
  1241. if (clk_enable(host->fclk) != 0) {
  1242. seq_printf(s, "can't read the regs\n");
  1243. return 0;
  1244. }
  1245. seq_printf(s, "SYSCONFIG:\t0x%08x\n",
  1246. OMAP_HSMMC_READ(host->base, SYSCONFIG));
  1247. seq_printf(s, "CON:\t\t0x%08x\n",
  1248. OMAP_HSMMC_READ(host->base, CON));
  1249. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1250. OMAP_HSMMC_READ(host->base, HCTL));
  1251. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1252. OMAP_HSMMC_READ(host->base, SYSCTL));
  1253. seq_printf(s, "IE:\t\t0x%08x\n",
  1254. OMAP_HSMMC_READ(host->base, IE));
  1255. seq_printf(s, "ISE:\t\t0x%08x\n",
  1256. OMAP_HSMMC_READ(host->base, ISE));
  1257. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1258. OMAP_HSMMC_READ(host->base, CAPA));
  1259. clk_disable(host->fclk);
  1260. return 0;
  1261. }
  1262. static int mmc_regs_open(struct inode *inode, struct file *file)
  1263. {
  1264. return single_open(file, mmc_regs_show, inode->i_private);
  1265. }
  1266. static const struct file_operations mmc_regs_fops = {
  1267. .open = mmc_regs_open,
  1268. .read = seq_read,
  1269. .llseek = seq_lseek,
  1270. .release = single_release,
  1271. };
  1272. static void omap_mmc_debugfs(struct mmc_host *mmc)
  1273. {
  1274. if (mmc->debugfs_root)
  1275. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1276. mmc, &mmc_regs_fops);
  1277. }
  1278. #else
  1279. static void omap_mmc_debugfs(struct mmc_host *mmc)
  1280. {
  1281. }
  1282. #endif
  1283. static int __init omap_mmc_probe(struct platform_device *pdev)
  1284. {
  1285. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1286. struct mmc_host *mmc;
  1287. struct mmc_omap_host *host = NULL;
  1288. struct resource *res;
  1289. int ret = 0, irq;
  1290. if (pdata == NULL) {
  1291. dev_err(&pdev->dev, "Platform Data is missing\n");
  1292. return -ENXIO;
  1293. }
  1294. if (pdata->nr_slots == 0) {
  1295. dev_err(&pdev->dev, "No Slots\n");
  1296. return -ENXIO;
  1297. }
  1298. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1299. irq = platform_get_irq(pdev, 0);
  1300. if (res == NULL || irq < 0)
  1301. return -ENXIO;
  1302. res = request_mem_region(res->start, res->end - res->start + 1,
  1303. pdev->name);
  1304. if (res == NULL)
  1305. return -EBUSY;
  1306. mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
  1307. if (!mmc) {
  1308. ret = -ENOMEM;
  1309. goto err;
  1310. }
  1311. host = mmc_priv(mmc);
  1312. host->mmc = mmc;
  1313. host->pdata = pdata;
  1314. host->dev = &pdev->dev;
  1315. host->use_dma = 1;
  1316. host->dev->dma_mask = &pdata->dma_mask;
  1317. host->dma_ch = -1;
  1318. host->irq = irq;
  1319. host->id = pdev->id;
  1320. host->slot_id = 0;
  1321. host->mapbase = res->start;
  1322. host->base = ioremap(host->mapbase, SZ_4K);
  1323. host->power_mode = -1;
  1324. platform_set_drvdata(pdev, host);
  1325. INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
  1326. if (mmc_slot(host).power_saving)
  1327. mmc->ops = &mmc_omap_ps_ops;
  1328. else
  1329. mmc->ops = &mmc_omap_ops;
  1330. mmc->f_min = 400000;
  1331. mmc->f_max = 52000000;
  1332. sema_init(&host->sem, 1);
  1333. host->iclk = clk_get(&pdev->dev, "ick");
  1334. if (IS_ERR(host->iclk)) {
  1335. ret = PTR_ERR(host->iclk);
  1336. host->iclk = NULL;
  1337. goto err1;
  1338. }
  1339. host->fclk = clk_get(&pdev->dev, "fck");
  1340. if (IS_ERR(host->fclk)) {
  1341. ret = PTR_ERR(host->fclk);
  1342. host->fclk = NULL;
  1343. clk_put(host->iclk);
  1344. goto err1;
  1345. }
  1346. omap_mmc_save_ctx(host);
  1347. mmc->caps |= MMC_CAP_DISABLE;
  1348. mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
  1349. /* we start off in DISABLED state */
  1350. host->dpm_state = DISABLED;
  1351. if (mmc_host_enable(host->mmc) != 0) {
  1352. clk_put(host->iclk);
  1353. clk_put(host->fclk);
  1354. goto err1;
  1355. }
  1356. if (clk_enable(host->iclk) != 0) {
  1357. mmc_host_disable(host->mmc);
  1358. clk_put(host->iclk);
  1359. clk_put(host->fclk);
  1360. goto err1;
  1361. }
  1362. host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
  1363. /*
  1364. * MMC can still work without debounce clock.
  1365. */
  1366. if (IS_ERR(host->dbclk))
  1367. dev_warn(mmc_dev(host->mmc), "Failed to get debounce clock\n");
  1368. else
  1369. if (clk_enable(host->dbclk) != 0)
  1370. dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
  1371. " clk failed\n");
  1372. else
  1373. host->dbclk_enabled = 1;
  1374. /* Since we do only SG emulation, we can have as many segs
  1375. * as we want. */
  1376. mmc->max_phys_segs = 1024;
  1377. mmc->max_hw_segs = 1024;
  1378. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1379. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1380. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1381. mmc->max_seg_size = mmc->max_req_size;
  1382. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1383. MMC_CAP_WAIT_WHILE_BUSY;
  1384. if (mmc_slot(host).wires >= 8)
  1385. mmc->caps |= MMC_CAP_8_BIT_DATA;
  1386. else if (mmc_slot(host).wires >= 4)
  1387. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1388. if (mmc_slot(host).nonremovable)
  1389. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1390. omap_hsmmc_init(host);
  1391. /* Select DMA lines */
  1392. switch (host->id) {
  1393. case OMAP_MMC1_DEVID:
  1394. host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
  1395. host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
  1396. break;
  1397. case OMAP_MMC2_DEVID:
  1398. host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
  1399. host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
  1400. break;
  1401. case OMAP_MMC3_DEVID:
  1402. host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
  1403. host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
  1404. break;
  1405. default:
  1406. dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
  1407. goto err_irq;
  1408. }
  1409. /* Request IRQ for MMC operations */
  1410. ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
  1411. mmc_hostname(mmc), host);
  1412. if (ret) {
  1413. dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1414. goto err_irq;
  1415. }
  1416. /* initialize power supplies, gpios, etc */
  1417. if (pdata->init != NULL) {
  1418. if (pdata->init(&pdev->dev) != 0) {
  1419. dev_dbg(mmc_dev(host->mmc), "late init error\n");
  1420. goto err_irq_cd_init;
  1421. }
  1422. }
  1423. mmc->ocr_avail = mmc_slot(host).ocr_mask;
  1424. /* Request IRQ for card detect */
  1425. if ((mmc_slot(host).card_detect_irq)) {
  1426. ret = request_irq(mmc_slot(host).card_detect_irq,
  1427. omap_mmc_cd_handler,
  1428. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
  1429. | IRQF_DISABLED,
  1430. mmc_hostname(mmc), host);
  1431. if (ret) {
  1432. dev_dbg(mmc_dev(host->mmc),
  1433. "Unable to grab MMC CD IRQ\n");
  1434. goto err_irq_cd;
  1435. }
  1436. }
  1437. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  1438. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  1439. mmc_host_lazy_disable(host->mmc);
  1440. mmc_add_host(mmc);
  1441. if (mmc_slot(host).name != NULL) {
  1442. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1443. if (ret < 0)
  1444. goto err_slot_name;
  1445. }
  1446. if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
  1447. ret = device_create_file(&mmc->class_dev,
  1448. &dev_attr_cover_switch);
  1449. if (ret < 0)
  1450. goto err_cover_switch;
  1451. }
  1452. omap_mmc_debugfs(mmc);
  1453. return 0;
  1454. err_cover_switch:
  1455. device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
  1456. err_slot_name:
  1457. mmc_remove_host(mmc);
  1458. err_irq_cd:
  1459. free_irq(mmc_slot(host).card_detect_irq, host);
  1460. err_irq_cd_init:
  1461. free_irq(host->irq, host);
  1462. err_irq:
  1463. mmc_host_disable(host->mmc);
  1464. clk_disable(host->iclk);
  1465. clk_put(host->fclk);
  1466. clk_put(host->iclk);
  1467. if (host->dbclk_enabled) {
  1468. clk_disable(host->dbclk);
  1469. clk_put(host->dbclk);
  1470. }
  1471. err1:
  1472. iounmap(host->base);
  1473. err:
  1474. dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
  1475. release_mem_region(res->start, res->end - res->start + 1);
  1476. if (host)
  1477. mmc_free_host(mmc);
  1478. return ret;
  1479. }
  1480. static int omap_mmc_remove(struct platform_device *pdev)
  1481. {
  1482. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1483. struct resource *res;
  1484. if (host) {
  1485. mmc_host_enable(host->mmc);
  1486. mmc_remove_host(host->mmc);
  1487. if (host->pdata->cleanup)
  1488. host->pdata->cleanup(&pdev->dev);
  1489. free_irq(host->irq, host);
  1490. if (mmc_slot(host).card_detect_irq)
  1491. free_irq(mmc_slot(host).card_detect_irq, host);
  1492. flush_scheduled_work();
  1493. mmc_host_disable(host->mmc);
  1494. clk_disable(host->iclk);
  1495. clk_put(host->fclk);
  1496. clk_put(host->iclk);
  1497. if (host->dbclk_enabled) {
  1498. clk_disable(host->dbclk);
  1499. clk_put(host->dbclk);
  1500. }
  1501. mmc_free_host(host->mmc);
  1502. iounmap(host->base);
  1503. }
  1504. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1505. if (res)
  1506. release_mem_region(res->start, res->end - res->start + 1);
  1507. platform_set_drvdata(pdev, NULL);
  1508. return 0;
  1509. }
  1510. #ifdef CONFIG_PM
  1511. static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
  1512. {
  1513. int ret = 0;
  1514. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1515. if (host && host->suspended)
  1516. return 0;
  1517. if (host) {
  1518. host->suspended = 1;
  1519. if (host->pdata->suspend) {
  1520. ret = host->pdata->suspend(&pdev->dev,
  1521. host->slot_id);
  1522. if (ret) {
  1523. dev_dbg(mmc_dev(host->mmc),
  1524. "Unable to handle MMC board"
  1525. " level suspend\n");
  1526. host->suspended = 0;
  1527. return ret;
  1528. }
  1529. }
  1530. cancel_work_sync(&host->mmc_carddetect_work);
  1531. mmc_host_enable(host->mmc);
  1532. ret = mmc_suspend_host(host->mmc, state);
  1533. if (ret == 0) {
  1534. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1535. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1536. OMAP_HSMMC_WRITE(host->base, HCTL,
  1537. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1538. mmc_host_disable(host->mmc);
  1539. clk_disable(host->iclk);
  1540. clk_disable(host->dbclk);
  1541. } else {
  1542. host->suspended = 0;
  1543. if (host->pdata->resume) {
  1544. ret = host->pdata->resume(&pdev->dev,
  1545. host->slot_id);
  1546. if (ret)
  1547. dev_dbg(mmc_dev(host->mmc),
  1548. "Unmask interrupt failed\n");
  1549. }
  1550. mmc_host_disable(host->mmc);
  1551. }
  1552. }
  1553. return ret;
  1554. }
  1555. /* Routine to resume the MMC device */
  1556. static int omap_mmc_resume(struct platform_device *pdev)
  1557. {
  1558. int ret = 0;
  1559. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1560. if (host && !host->suspended)
  1561. return 0;
  1562. if (host) {
  1563. ret = clk_enable(host->iclk);
  1564. if (ret)
  1565. goto clk_en_err;
  1566. if (clk_enable(host->dbclk) != 0)
  1567. dev_dbg(mmc_dev(host->mmc),
  1568. "Enabling debounce clk failed\n");
  1569. if (mmc_host_enable(host->mmc) != 0) {
  1570. clk_disable(host->iclk);
  1571. goto clk_en_err;
  1572. }
  1573. omap_hsmmc_init(host);
  1574. if (host->pdata->resume) {
  1575. ret = host->pdata->resume(&pdev->dev, host->slot_id);
  1576. if (ret)
  1577. dev_dbg(mmc_dev(host->mmc),
  1578. "Unmask interrupt failed\n");
  1579. }
  1580. /* Notify the core to resume the host */
  1581. ret = mmc_resume_host(host->mmc);
  1582. if (ret == 0)
  1583. host->suspended = 0;
  1584. mmc_host_lazy_disable(host->mmc);
  1585. }
  1586. return ret;
  1587. clk_en_err:
  1588. dev_dbg(mmc_dev(host->mmc),
  1589. "Failed to enable MMC clocks during resume\n");
  1590. return ret;
  1591. }
  1592. #else
  1593. #define omap_mmc_suspend NULL
  1594. #define omap_mmc_resume NULL
  1595. #endif
  1596. static struct platform_driver omap_mmc_driver = {
  1597. .remove = omap_mmc_remove,
  1598. .suspend = omap_mmc_suspend,
  1599. .resume = omap_mmc_resume,
  1600. .driver = {
  1601. .name = DRIVER_NAME,
  1602. .owner = THIS_MODULE,
  1603. },
  1604. };
  1605. static int __init omap_mmc_init(void)
  1606. {
  1607. /* Register the MMC driver */
  1608. return platform_driver_probe(&omap_mmc_driver, omap_mmc_probe);
  1609. }
  1610. static void __exit omap_mmc_cleanup(void)
  1611. {
  1612. /* Unregister MMC driver */
  1613. platform_driver_unregister(&omap_mmc_driver);
  1614. }
  1615. module_init(omap_mmc_init);
  1616. module_exit(omap_mmc_cleanup);
  1617. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  1618. MODULE_LICENSE("GPL");
  1619. MODULE_ALIAS("platform:" DRIVER_NAME);
  1620. MODULE_AUTHOR("Texas Instruments Inc");