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