omap_hsmmc.c 46 KB

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