omap_hsmmc.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470
  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/mmc/mmc.h>
  31. #include <linux/io.h>
  32. #include <linux/semaphore.h>
  33. #include <linux/gpio.h>
  34. #include <linux/regulator/consumer.h>
  35. #include <plat/dma.h>
  36. #include <mach/hardware.h>
  37. #include <plat/board.h>
  38. #include <plat/mmc.h>
  39. #include <plat/cpu.h>
  40. /* OMAP HSMMC Host Controller Registers */
  41. #define OMAP_HSMMC_SYSCONFIG 0x0010
  42. #define OMAP_HSMMC_SYSSTATUS 0x0014
  43. #define OMAP_HSMMC_CON 0x002C
  44. #define OMAP_HSMMC_BLK 0x0104
  45. #define OMAP_HSMMC_ARG 0x0108
  46. #define OMAP_HSMMC_CMD 0x010C
  47. #define OMAP_HSMMC_RSP10 0x0110
  48. #define OMAP_HSMMC_RSP32 0x0114
  49. #define OMAP_HSMMC_RSP54 0x0118
  50. #define OMAP_HSMMC_RSP76 0x011C
  51. #define OMAP_HSMMC_DATA 0x0120
  52. #define OMAP_HSMMC_HCTL 0x0128
  53. #define OMAP_HSMMC_SYSCTL 0x012C
  54. #define OMAP_HSMMC_STAT 0x0130
  55. #define OMAP_HSMMC_IE 0x0134
  56. #define OMAP_HSMMC_ISE 0x0138
  57. #define OMAP_HSMMC_CAPA 0x0140
  58. #define VS18 (1 << 26)
  59. #define VS30 (1 << 25)
  60. #define SDVS18 (0x5 << 9)
  61. #define SDVS30 (0x6 << 9)
  62. #define SDVS33 (0x7 << 9)
  63. #define SDVS_MASK 0x00000E00
  64. #define SDVSCLR 0xFFFFF1FF
  65. #define SDVSDET 0x00000400
  66. #define AUTOIDLE 0x1
  67. #define SDBP (1 << 8)
  68. #define DTO 0xe
  69. #define ICE 0x1
  70. #define ICS 0x2
  71. #define CEN (1 << 2)
  72. #define CLKD_MASK 0x0000FFC0
  73. #define CLKD_SHIFT 6
  74. #define DTO_MASK 0x000F0000
  75. #define DTO_SHIFT 16
  76. #define INT_EN_MASK 0x307F0033
  77. #define BWR_ENABLE (1 << 4)
  78. #define BRR_ENABLE (1 << 5)
  79. #define DTO_ENABLE (1 << 20)
  80. #define INIT_STREAM (1 << 1)
  81. #define DP_SELECT (1 << 21)
  82. #define DDIR (1 << 4)
  83. #define DMA_EN 0x1
  84. #define MSBS (1 << 5)
  85. #define BCE (1 << 1)
  86. #define FOUR_BIT (1 << 1)
  87. #define DW8 (1 << 5)
  88. #define CC 0x1
  89. #define TC 0x02
  90. #define OD 0x1
  91. #define ERR (1 << 15)
  92. #define CMD_TIMEOUT (1 << 16)
  93. #define DATA_TIMEOUT (1 << 20)
  94. #define CMD_CRC (1 << 17)
  95. #define DATA_CRC (1 << 21)
  96. #define CARD_ERR (1 << 28)
  97. #define STAT_CLEAR 0xFFFFFFFF
  98. #define INIT_STREAM_CMD 0x00000000
  99. #define DUAL_VOLT_OCR_BIT 7
  100. #define SRC (1 << 25)
  101. #define SRD (1 << 26)
  102. #define SOFTRESET (1 << 1)
  103. #define RESETDONE (1 << 0)
  104. /*
  105. * FIXME: Most likely all the data using these _DEVID defines should come
  106. * from the platform_data, or implemented in controller and slot specific
  107. * functions.
  108. */
  109. #define OMAP_MMC1_DEVID 0
  110. #define OMAP_MMC2_DEVID 1
  111. #define OMAP_MMC3_DEVID 2
  112. #define OMAP_MMC4_DEVID 3
  113. #define OMAP_MMC5_DEVID 4
  114. #define MMC_TIMEOUT_MS 20
  115. #define OMAP_MMC_MASTER_CLOCK 96000000
  116. #define DRIVER_NAME "mmci-omap-hs"
  117. /* Timeouts for entering power saving states on inactivity, msec */
  118. #define OMAP_MMC_DISABLED_TIMEOUT 100
  119. #define OMAP_MMC_SLEEP_TIMEOUT 1000
  120. #define OMAP_MMC_OFF_TIMEOUT 8000
  121. /*
  122. * One controller can have multiple slots, like on some omap boards using
  123. * omap.c controller driver. Luckily this is not currently done on any known
  124. * omap_hsmmc.c device.
  125. */
  126. #define mmc_slot(host) (host->pdata->slots[host->slot_id])
  127. /*
  128. * MMC Host controller read/write API's
  129. */
  130. #define OMAP_HSMMC_READ(base, reg) \
  131. __raw_readl((base) + OMAP_HSMMC_##reg)
  132. #define OMAP_HSMMC_WRITE(base, reg, val) \
  133. __raw_writel((val), (base) + OMAP_HSMMC_##reg)
  134. struct omap_hsmmc_host {
  135. struct device *dev;
  136. struct mmc_host *mmc;
  137. struct mmc_request *mrq;
  138. struct mmc_command *cmd;
  139. struct mmc_data *data;
  140. struct clk *fclk;
  141. struct clk *iclk;
  142. struct clk *dbclk;
  143. /*
  144. * vcc == configured supply
  145. * vcc_aux == optional
  146. * - MMC1, supply for DAT4..DAT7
  147. * - MMC2/MMC2, external level shifter voltage supply, for
  148. * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
  149. */
  150. struct regulator *vcc;
  151. struct regulator *vcc_aux;
  152. struct work_struct mmc_carddetect_work;
  153. void __iomem *base;
  154. resource_size_t mapbase;
  155. spinlock_t irq_lock; /* Prevent races with irq handler */
  156. unsigned int id;
  157. unsigned int dma_len;
  158. unsigned int dma_sg_idx;
  159. unsigned char bus_mode;
  160. unsigned char power_mode;
  161. u32 *buffer;
  162. u32 bytesleft;
  163. int suspended;
  164. int irq;
  165. int use_dma, dma_ch;
  166. int dma_line_tx, dma_line_rx;
  167. int slot_id;
  168. int got_dbclk;
  169. int response_busy;
  170. int context_loss;
  171. int dpm_state;
  172. int vdd;
  173. int protect_card;
  174. int reqs_blocked;
  175. int use_reg;
  176. int req_in_progress;
  177. struct omap_mmc_platform_data *pdata;
  178. };
  179. static int omap_hsmmc_card_detect(struct device *dev, int slot)
  180. {
  181. struct omap_mmc_platform_data *mmc = dev->platform_data;
  182. /* NOTE: assumes card detect signal is active-low */
  183. return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
  184. }
  185. static int omap_hsmmc_get_wp(struct device *dev, int slot)
  186. {
  187. struct omap_mmc_platform_data *mmc = dev->platform_data;
  188. /* NOTE: assumes write protect signal is active-high */
  189. return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
  190. }
  191. static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
  192. {
  193. struct omap_mmc_platform_data *mmc = dev->platform_data;
  194. /* NOTE: assumes card detect signal is active-low */
  195. return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
  196. }
  197. #ifdef CONFIG_PM
  198. static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
  199. {
  200. struct omap_mmc_platform_data *mmc = dev->platform_data;
  201. disable_irq(mmc->slots[0].card_detect_irq);
  202. return 0;
  203. }
  204. static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
  205. {
  206. struct omap_mmc_platform_data *mmc = dev->platform_data;
  207. enable_irq(mmc->slots[0].card_detect_irq);
  208. return 0;
  209. }
  210. #else
  211. #define omap_hsmmc_suspend_cdirq NULL
  212. #define omap_hsmmc_resume_cdirq NULL
  213. #endif
  214. #ifdef CONFIG_REGULATOR
  215. static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
  216. int vdd)
  217. {
  218. struct omap_hsmmc_host *host =
  219. platform_get_drvdata(to_platform_device(dev));
  220. int ret;
  221. if (mmc_slot(host).before_set_reg)
  222. mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
  223. if (power_on)
  224. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  225. else
  226. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
  227. if (mmc_slot(host).after_set_reg)
  228. mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
  229. return ret;
  230. }
  231. static int omap_hsmmc_235_set_power(struct device *dev, int slot, int power_on,
  232. int vdd)
  233. {
  234. struct omap_hsmmc_host *host =
  235. platform_get_drvdata(to_platform_device(dev));
  236. int ret = 0;
  237. /*
  238. * If we don't see a Vcc regulator, assume it's a fixed
  239. * voltage always-on regulator.
  240. */
  241. if (!host->vcc)
  242. return 0;
  243. if (mmc_slot(host).before_set_reg)
  244. mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
  245. /*
  246. * Assume Vcc regulator is used only to power the card ... OMAP
  247. * VDDS is used to power the pins, optionally with a transceiver to
  248. * support cards using voltages other than VDDS (1.8V nominal). When a
  249. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  250. *
  251. * In some cases this regulator won't support enable/disable;
  252. * e.g. it's a fixed rail for a WLAN chip.
  253. *
  254. * In other cases vcc_aux switches interface power. Example, for
  255. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  256. * chips/cards need an interface voltage rail too.
  257. */
  258. if (power_on) {
  259. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  260. /* Enable interface voltage rail, if needed */
  261. if (ret == 0 && host->vcc_aux) {
  262. ret = regulator_enable(host->vcc_aux);
  263. if (ret < 0)
  264. ret = mmc_regulator_set_ocr(host->mmc,
  265. host->vcc, 0);
  266. }
  267. } else {
  268. /* Shut down the rail */
  269. if (host->vcc_aux)
  270. ret = regulator_disable(host->vcc_aux);
  271. if (!ret) {
  272. /* Then proceed to shut down the local regulator */
  273. ret = mmc_regulator_set_ocr(host->mmc,
  274. host->vcc, 0);
  275. }
  276. }
  277. if (mmc_slot(host).after_set_reg)
  278. mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
  279. return ret;
  280. }
  281. static int omap_hsmmc_4_set_power(struct device *dev, int slot, int power_on,
  282. int vdd)
  283. {
  284. return 0;
  285. }
  286. static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
  287. int vdd, int cardsleep)
  288. {
  289. struct omap_hsmmc_host *host =
  290. platform_get_drvdata(to_platform_device(dev));
  291. int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
  292. return regulator_set_mode(host->vcc, mode);
  293. }
  294. static int omap_hsmmc_235_set_sleep(struct device *dev, int slot, int sleep,
  295. int vdd, int cardsleep)
  296. {
  297. struct omap_hsmmc_host *host =
  298. platform_get_drvdata(to_platform_device(dev));
  299. int err, mode;
  300. /*
  301. * If we don't see a Vcc regulator, assume it's a fixed
  302. * voltage always-on regulator.
  303. */
  304. if (!host->vcc)
  305. return 0;
  306. mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
  307. if (!host->vcc_aux)
  308. return regulator_set_mode(host->vcc, mode);
  309. if (cardsleep) {
  310. /* VCC can be turned off if card is asleep */
  311. if (sleep)
  312. err = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
  313. else
  314. err = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  315. } else
  316. err = regulator_set_mode(host->vcc, mode);
  317. if (err)
  318. return err;
  319. if (!mmc_slot(host).vcc_aux_disable_is_sleep)
  320. return regulator_set_mode(host->vcc_aux, mode);
  321. if (sleep)
  322. return regulator_disable(host->vcc_aux);
  323. else
  324. return regulator_enable(host->vcc_aux);
  325. }
  326. static int omap_hsmmc_4_set_sleep(struct device *dev, int slot, int sleep,
  327. int vdd, int cardsleep)
  328. {
  329. return 0;
  330. }
  331. static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  332. {
  333. struct regulator *reg;
  334. int ret = 0;
  335. int ocr_value = 0;
  336. switch (host->id) {
  337. case OMAP_MMC1_DEVID:
  338. /* On-chip level shifting via PBIAS0/PBIAS1 */
  339. mmc_slot(host).set_power = omap_hsmmc_1_set_power;
  340. mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
  341. break;
  342. case OMAP_MMC2_DEVID:
  343. case OMAP_MMC3_DEVID:
  344. case OMAP_MMC5_DEVID:
  345. /* Off-chip level shifting, or none */
  346. mmc_slot(host).set_power = omap_hsmmc_235_set_power;
  347. mmc_slot(host).set_sleep = omap_hsmmc_235_set_sleep;
  348. break;
  349. case OMAP_MMC4_DEVID:
  350. mmc_slot(host).set_power = omap_hsmmc_4_set_power;
  351. mmc_slot(host).set_sleep = omap_hsmmc_4_set_sleep;
  352. default:
  353. pr_err("MMC%d configuration not supported!\n", host->id);
  354. return -EINVAL;
  355. }
  356. reg = regulator_get(host->dev, "vmmc");
  357. if (IS_ERR(reg)) {
  358. dev_dbg(host->dev, "vmmc regulator missing\n");
  359. /*
  360. * HACK: until fixed.c regulator is usable,
  361. * we don't require a main regulator
  362. * for MMC2 or MMC3
  363. */
  364. if (host->id == OMAP_MMC1_DEVID) {
  365. ret = PTR_ERR(reg);
  366. goto err;
  367. }
  368. } else {
  369. host->vcc = reg;
  370. ocr_value = mmc_regulator_get_ocrmask(reg);
  371. if (!mmc_slot(host).ocr_mask) {
  372. mmc_slot(host).ocr_mask = ocr_value;
  373. } else {
  374. if (!(mmc_slot(host).ocr_mask & ocr_value)) {
  375. pr_err("MMC%d ocrmask %x is not supported\n",
  376. host->id, mmc_slot(host).ocr_mask);
  377. mmc_slot(host).ocr_mask = 0;
  378. return -EINVAL;
  379. }
  380. }
  381. mmc_slot(host).ocr_mask = mmc_regulator_get_ocrmask(reg);
  382. /* Allow an aux regulator */
  383. reg = regulator_get(host->dev, "vmmc_aux");
  384. host->vcc_aux = IS_ERR(reg) ? NULL : reg;
  385. /*
  386. * UGLY HACK: workaround regulator framework bugs.
  387. * When the bootloader leaves a supply active, it's
  388. * initialized with zero usecount ... and we can't
  389. * disable it without first enabling it. Until the
  390. * framework is fixed, we need a workaround like this
  391. * (which is safe for MMC, but not in general).
  392. */
  393. if (regulator_is_enabled(host->vcc) > 0) {
  394. regulator_enable(host->vcc);
  395. regulator_disable(host->vcc);
  396. }
  397. if (host->vcc_aux) {
  398. if (regulator_is_enabled(reg) > 0) {
  399. regulator_enable(reg);
  400. regulator_disable(reg);
  401. }
  402. }
  403. }
  404. return 0;
  405. err:
  406. mmc_slot(host).set_power = NULL;
  407. mmc_slot(host).set_sleep = NULL;
  408. return ret;
  409. }
  410. static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
  411. {
  412. regulator_put(host->vcc);
  413. regulator_put(host->vcc_aux);
  414. mmc_slot(host).set_power = NULL;
  415. mmc_slot(host).set_sleep = NULL;
  416. }
  417. static inline int omap_hsmmc_have_reg(void)
  418. {
  419. return 1;
  420. }
  421. #else
  422. static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  423. {
  424. return -EINVAL;
  425. }
  426. static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
  427. {
  428. }
  429. static inline int omap_hsmmc_have_reg(void)
  430. {
  431. return 0;
  432. }
  433. #endif
  434. static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
  435. {
  436. int ret;
  437. if (gpio_is_valid(pdata->slots[0].switch_pin)) {
  438. if (pdata->slots[0].cover)
  439. pdata->slots[0].get_cover_state =
  440. omap_hsmmc_get_cover_state;
  441. else
  442. pdata->slots[0].card_detect = omap_hsmmc_card_detect;
  443. pdata->slots[0].card_detect_irq =
  444. gpio_to_irq(pdata->slots[0].switch_pin);
  445. ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
  446. if (ret)
  447. return ret;
  448. ret = gpio_direction_input(pdata->slots[0].switch_pin);
  449. if (ret)
  450. goto err_free_sp;
  451. } else
  452. pdata->slots[0].switch_pin = -EINVAL;
  453. if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
  454. pdata->slots[0].get_ro = omap_hsmmc_get_wp;
  455. ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
  456. if (ret)
  457. goto err_free_cd;
  458. ret = gpio_direction_input(pdata->slots[0].gpio_wp);
  459. if (ret)
  460. goto err_free_wp;
  461. } else
  462. pdata->slots[0].gpio_wp = -EINVAL;
  463. return 0;
  464. err_free_wp:
  465. gpio_free(pdata->slots[0].gpio_wp);
  466. err_free_cd:
  467. if (gpio_is_valid(pdata->slots[0].switch_pin))
  468. err_free_sp:
  469. gpio_free(pdata->slots[0].switch_pin);
  470. return ret;
  471. }
  472. static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
  473. {
  474. if (gpio_is_valid(pdata->slots[0].gpio_wp))
  475. gpio_free(pdata->slots[0].gpio_wp);
  476. if (gpio_is_valid(pdata->slots[0].switch_pin))
  477. gpio_free(pdata->slots[0].switch_pin);
  478. }
  479. /*
  480. * Stop clock to the card
  481. */
  482. static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
  483. {
  484. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  485. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  486. if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
  487. dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
  488. }
  489. static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
  490. struct mmc_command *cmd)
  491. {
  492. unsigned int irq_mask;
  493. if (host->use_dma)
  494. irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
  495. else
  496. irq_mask = INT_EN_MASK;
  497. /* Disable timeout for erases */
  498. if (cmd->opcode == MMC_ERASE)
  499. irq_mask &= ~DTO_ENABLE;
  500. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  501. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  502. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  503. }
  504. static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
  505. {
  506. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  507. OMAP_HSMMC_WRITE(host->base, IE, 0);
  508. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  509. }
  510. #ifdef CONFIG_PM
  511. /*
  512. * Restore the MMC host context, if it was lost as result of a
  513. * power state change.
  514. */
  515. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  516. {
  517. struct mmc_ios *ios = &host->mmc->ios;
  518. struct omap_mmc_platform_data *pdata = host->pdata;
  519. int context_loss = 0;
  520. u32 hctl, capa, con;
  521. u16 dsor = 0;
  522. unsigned long timeout;
  523. if (pdata->get_context_loss_count) {
  524. context_loss = pdata->get_context_loss_count(host->dev);
  525. if (context_loss < 0)
  526. return 1;
  527. }
  528. dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
  529. context_loss == host->context_loss ? "not " : "");
  530. if (host->context_loss == context_loss)
  531. return 1;
  532. /* Wait for hardware reset */
  533. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  534. while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
  535. && time_before(jiffies, timeout))
  536. ;
  537. /* Do software reset */
  538. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
  539. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  540. while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
  541. && time_before(jiffies, timeout))
  542. ;
  543. OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
  544. OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
  545. if (host->id == OMAP_MMC1_DEVID) {
  546. if (host->power_mode != MMC_POWER_OFF &&
  547. (1 << ios->vdd) <= MMC_VDD_23_24)
  548. hctl = SDVS18;
  549. else
  550. hctl = SDVS30;
  551. capa = VS30 | VS18;
  552. } else {
  553. hctl = SDVS18;
  554. capa = VS18;
  555. }
  556. OMAP_HSMMC_WRITE(host->base, HCTL,
  557. OMAP_HSMMC_READ(host->base, HCTL) | hctl);
  558. OMAP_HSMMC_WRITE(host->base, CAPA,
  559. OMAP_HSMMC_READ(host->base, CAPA) | capa);
  560. OMAP_HSMMC_WRITE(host->base, HCTL,
  561. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  562. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  563. while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
  564. && time_before(jiffies, timeout))
  565. ;
  566. omap_hsmmc_disable_irq(host);
  567. /* Do not initialize card-specific things if the power is off */
  568. if (host->power_mode == MMC_POWER_OFF)
  569. goto out;
  570. con = OMAP_HSMMC_READ(host->base, CON);
  571. switch (ios->bus_width) {
  572. case MMC_BUS_WIDTH_8:
  573. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  574. break;
  575. case MMC_BUS_WIDTH_4:
  576. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  577. OMAP_HSMMC_WRITE(host->base, HCTL,
  578. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  579. break;
  580. case MMC_BUS_WIDTH_1:
  581. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  582. OMAP_HSMMC_WRITE(host->base, HCTL,
  583. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  584. break;
  585. }
  586. if (ios->clock) {
  587. dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
  588. if (dsor < 1)
  589. dsor = 1;
  590. if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
  591. dsor++;
  592. if (dsor > 250)
  593. dsor = 250;
  594. }
  595. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  596. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  597. OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
  598. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  599. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  600. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  601. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  602. && time_before(jiffies, timeout))
  603. ;
  604. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  605. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  606. con = OMAP_HSMMC_READ(host->base, CON);
  607. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  608. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  609. else
  610. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  611. out:
  612. host->context_loss = context_loss;
  613. dev_dbg(mmc_dev(host->mmc), "context is restored\n");
  614. return 0;
  615. }
  616. /*
  617. * Save the MMC host context (store the number of power state changes so far).
  618. */
  619. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  620. {
  621. struct omap_mmc_platform_data *pdata = host->pdata;
  622. int context_loss;
  623. if (pdata->get_context_loss_count) {
  624. context_loss = pdata->get_context_loss_count(host->dev);
  625. if (context_loss < 0)
  626. return;
  627. host->context_loss = context_loss;
  628. }
  629. }
  630. #else
  631. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  632. {
  633. return 0;
  634. }
  635. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  636. {
  637. }
  638. #endif
  639. /*
  640. * Send init stream sequence to card
  641. * before sending IDLE command
  642. */
  643. static void send_init_stream(struct omap_hsmmc_host *host)
  644. {
  645. int reg = 0;
  646. unsigned long timeout;
  647. if (host->protect_card)
  648. return;
  649. disable_irq(host->irq);
  650. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  651. OMAP_HSMMC_WRITE(host->base, CON,
  652. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  653. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  654. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  655. while ((reg != CC) && time_before(jiffies, timeout))
  656. reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
  657. OMAP_HSMMC_WRITE(host->base, CON,
  658. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  659. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  660. OMAP_HSMMC_READ(host->base, STAT);
  661. enable_irq(host->irq);
  662. }
  663. static inline
  664. int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
  665. {
  666. int r = 1;
  667. if (mmc_slot(host).get_cover_state)
  668. r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
  669. return r;
  670. }
  671. static ssize_t
  672. omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
  673. char *buf)
  674. {
  675. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  676. struct omap_hsmmc_host *host = mmc_priv(mmc);
  677. return sprintf(buf, "%s\n",
  678. omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
  679. }
  680. static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
  681. static ssize_t
  682. omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
  683. char *buf)
  684. {
  685. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  686. struct omap_hsmmc_host *host = mmc_priv(mmc);
  687. return sprintf(buf, "%s\n", mmc_slot(host).name);
  688. }
  689. static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
  690. /*
  691. * Configure the response type and send the cmd.
  692. */
  693. static void
  694. omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
  695. struct mmc_data *data)
  696. {
  697. int cmdreg = 0, resptype = 0, cmdtype = 0;
  698. dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  699. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  700. host->cmd = cmd;
  701. omap_hsmmc_enable_irq(host, cmd);
  702. host->response_busy = 0;
  703. if (cmd->flags & MMC_RSP_PRESENT) {
  704. if (cmd->flags & MMC_RSP_136)
  705. resptype = 1;
  706. else if (cmd->flags & MMC_RSP_BUSY) {
  707. resptype = 3;
  708. host->response_busy = 1;
  709. } else
  710. resptype = 2;
  711. }
  712. /*
  713. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  714. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  715. * a val of 0x3, rest 0x0.
  716. */
  717. if (cmd == host->mrq->stop)
  718. cmdtype = 0x3;
  719. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  720. if (data) {
  721. cmdreg |= DP_SELECT | MSBS | BCE;
  722. if (data->flags & MMC_DATA_READ)
  723. cmdreg |= DDIR;
  724. else
  725. cmdreg &= ~(DDIR);
  726. }
  727. if (host->use_dma)
  728. cmdreg |= DMA_EN;
  729. host->req_in_progress = 1;
  730. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  731. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  732. }
  733. static int
  734. omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
  735. {
  736. if (data->flags & MMC_DATA_WRITE)
  737. return DMA_TO_DEVICE;
  738. else
  739. return DMA_FROM_DEVICE;
  740. }
  741. static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
  742. {
  743. int dma_ch;
  744. spin_lock(&host->irq_lock);
  745. host->req_in_progress = 0;
  746. dma_ch = host->dma_ch;
  747. spin_unlock(&host->irq_lock);
  748. omap_hsmmc_disable_irq(host);
  749. /* Do not complete the request if DMA is still in progress */
  750. if (mrq->data && host->use_dma && dma_ch != -1)
  751. return;
  752. host->mrq = NULL;
  753. mmc_request_done(host->mmc, mrq);
  754. }
  755. /*
  756. * Notify the transfer complete to MMC core
  757. */
  758. static void
  759. omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
  760. {
  761. if (!data) {
  762. struct mmc_request *mrq = host->mrq;
  763. /* TC before CC from CMD6 - don't know why, but it happens */
  764. if (host->cmd && host->cmd->opcode == 6 &&
  765. host->response_busy) {
  766. host->response_busy = 0;
  767. return;
  768. }
  769. omap_hsmmc_request_done(host, mrq);
  770. return;
  771. }
  772. host->data = NULL;
  773. if (!data->error)
  774. data->bytes_xfered += data->blocks * (data->blksz);
  775. else
  776. data->bytes_xfered = 0;
  777. if (!data->stop) {
  778. omap_hsmmc_request_done(host, data->mrq);
  779. return;
  780. }
  781. omap_hsmmc_start_command(host, data->stop, NULL);
  782. }
  783. /*
  784. * Notify the core about command completion
  785. */
  786. static void
  787. omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
  788. {
  789. host->cmd = NULL;
  790. if (cmd->flags & MMC_RSP_PRESENT) {
  791. if (cmd->flags & MMC_RSP_136) {
  792. /* response type 2 */
  793. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  794. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  795. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  796. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  797. } else {
  798. /* response types 1, 1b, 3, 4, 5, 6 */
  799. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  800. }
  801. }
  802. if ((host->data == NULL && !host->response_busy) || cmd->error)
  803. omap_hsmmc_request_done(host, cmd->mrq);
  804. }
  805. /*
  806. * DMA clean up for command errors
  807. */
  808. static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
  809. {
  810. int dma_ch;
  811. host->data->error = errno;
  812. spin_lock(&host->irq_lock);
  813. dma_ch = host->dma_ch;
  814. host->dma_ch = -1;
  815. spin_unlock(&host->irq_lock);
  816. if (host->use_dma && dma_ch != -1) {
  817. dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
  818. omap_hsmmc_get_dma_dir(host, host->data));
  819. omap_free_dma(dma_ch);
  820. }
  821. host->data = NULL;
  822. }
  823. /*
  824. * Readable error output
  825. */
  826. #ifdef CONFIG_MMC_DEBUG
  827. static void omap_hsmmc_report_irq(struct omap_hsmmc_host *host, u32 status)
  828. {
  829. /* --- means reserved bit without definition at documentation */
  830. static const char *omap_hsmmc_status_bits[] = {
  831. "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
  832. "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
  833. "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
  834. "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
  835. };
  836. char res[256];
  837. char *buf = res;
  838. int len, i;
  839. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  840. buf += len;
  841. for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
  842. if (status & (1 << i)) {
  843. len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
  844. buf += len;
  845. }
  846. dev_dbg(mmc_dev(host->mmc), "%s\n", res);
  847. }
  848. #endif /* CONFIG_MMC_DEBUG */
  849. /*
  850. * MMC controller internal state machines reset
  851. *
  852. * Used to reset command or data internal state machines, using respectively
  853. * SRC or SRD bit of SYSCTL register
  854. * Can be called from interrupt context
  855. */
  856. static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
  857. unsigned long bit)
  858. {
  859. unsigned long i = 0;
  860. unsigned long limit = (loops_per_jiffy *
  861. msecs_to_jiffies(MMC_TIMEOUT_MS));
  862. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  863. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  864. /*
  865. * OMAP4 ES2 and greater has an updated reset logic.
  866. * Monitor a 0->1 transition first
  867. */
  868. if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
  869. while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
  870. && (i++ < limit))
  871. cpu_relax();
  872. }
  873. i = 0;
  874. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  875. (i++ < limit))
  876. cpu_relax();
  877. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  878. dev_err(mmc_dev(host->mmc),
  879. "Timeout waiting on controller reset in %s\n",
  880. __func__);
  881. }
  882. static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
  883. {
  884. struct mmc_data *data;
  885. int end_cmd = 0, end_trans = 0;
  886. if (!host->req_in_progress) {
  887. do {
  888. OMAP_HSMMC_WRITE(host->base, STAT, status);
  889. /* Flush posted write */
  890. status = OMAP_HSMMC_READ(host->base, STAT);
  891. } while (status & INT_EN_MASK);
  892. return;
  893. }
  894. data = host->data;
  895. dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  896. if (status & ERR) {
  897. #ifdef CONFIG_MMC_DEBUG
  898. omap_hsmmc_report_irq(host, status);
  899. #endif
  900. if ((status & CMD_TIMEOUT) ||
  901. (status & CMD_CRC)) {
  902. if (host->cmd) {
  903. if (status & CMD_TIMEOUT) {
  904. omap_hsmmc_reset_controller_fsm(host,
  905. SRC);
  906. host->cmd->error = -ETIMEDOUT;
  907. } else {
  908. host->cmd->error = -EILSEQ;
  909. }
  910. end_cmd = 1;
  911. }
  912. if (host->data || host->response_busy) {
  913. if (host->data)
  914. omap_hsmmc_dma_cleanup(host,
  915. -ETIMEDOUT);
  916. host->response_busy = 0;
  917. omap_hsmmc_reset_controller_fsm(host, SRD);
  918. }
  919. }
  920. if ((status & DATA_TIMEOUT) ||
  921. (status & DATA_CRC)) {
  922. if (host->data || host->response_busy) {
  923. int err = (status & DATA_TIMEOUT) ?
  924. -ETIMEDOUT : -EILSEQ;
  925. if (host->data)
  926. omap_hsmmc_dma_cleanup(host, err);
  927. else
  928. host->mrq->cmd->error = err;
  929. host->response_busy = 0;
  930. omap_hsmmc_reset_controller_fsm(host, SRD);
  931. end_trans = 1;
  932. }
  933. }
  934. if (status & CARD_ERR) {
  935. dev_dbg(mmc_dev(host->mmc),
  936. "Ignoring card err CMD%d\n", host->cmd->opcode);
  937. if (host->cmd)
  938. end_cmd = 1;
  939. if (host->data)
  940. end_trans = 1;
  941. }
  942. }
  943. OMAP_HSMMC_WRITE(host->base, STAT, status);
  944. if (end_cmd || ((status & CC) && host->cmd))
  945. omap_hsmmc_cmd_done(host, host->cmd);
  946. if ((end_trans || (status & TC)) && host->mrq)
  947. omap_hsmmc_xfer_done(host, data);
  948. }
  949. /*
  950. * MMC controller IRQ handler
  951. */
  952. static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
  953. {
  954. struct omap_hsmmc_host *host = dev_id;
  955. int status;
  956. status = OMAP_HSMMC_READ(host->base, STAT);
  957. do {
  958. omap_hsmmc_do_irq(host, status);
  959. /* Flush posted write */
  960. status = OMAP_HSMMC_READ(host->base, STAT);
  961. } while (status & INT_EN_MASK);
  962. return IRQ_HANDLED;
  963. }
  964. static void set_sd_bus_power(struct omap_hsmmc_host *host)
  965. {
  966. unsigned long i;
  967. OMAP_HSMMC_WRITE(host->base, HCTL,
  968. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  969. for (i = 0; i < loops_per_jiffy; i++) {
  970. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  971. break;
  972. cpu_relax();
  973. }
  974. }
  975. /*
  976. * Switch MMC interface voltage ... only relevant for MMC1.
  977. *
  978. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  979. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  980. * Some chips, like eMMC ones, use internal transceivers.
  981. */
  982. static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
  983. {
  984. u32 reg_val = 0;
  985. int ret;
  986. /* Disable the clocks */
  987. clk_disable(host->fclk);
  988. clk_disable(host->iclk);
  989. if (host->got_dbclk)
  990. clk_disable(host->dbclk);
  991. /* Turn the power off */
  992. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  993. /* Turn the power ON with given VDD 1.8 or 3.0v */
  994. if (!ret)
  995. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
  996. vdd);
  997. clk_enable(host->iclk);
  998. clk_enable(host->fclk);
  999. if (host->got_dbclk)
  1000. clk_enable(host->dbclk);
  1001. if (ret != 0)
  1002. goto err;
  1003. OMAP_HSMMC_WRITE(host->base, HCTL,
  1004. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  1005. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  1006. /*
  1007. * If a MMC dual voltage card is detected, the set_ios fn calls
  1008. * this fn with VDD bit set for 1.8V. Upon card removal from the
  1009. * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  1010. *
  1011. * Cope with a bit of slop in the range ... per data sheets:
  1012. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  1013. * but recommended values are 1.71V to 1.89V
  1014. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  1015. * but recommended values are 2.7V to 3.3V
  1016. *
  1017. * Board setup code shouldn't permit anything very out-of-range.
  1018. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  1019. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  1020. */
  1021. if ((1 << vdd) <= MMC_VDD_23_24)
  1022. reg_val |= SDVS18;
  1023. else
  1024. reg_val |= SDVS30;
  1025. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  1026. set_sd_bus_power(host);
  1027. return 0;
  1028. err:
  1029. dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  1030. return ret;
  1031. }
  1032. /* Protect the card while the cover is open */
  1033. static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
  1034. {
  1035. if (!mmc_slot(host).get_cover_state)
  1036. return;
  1037. host->reqs_blocked = 0;
  1038. if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
  1039. if (host->protect_card) {
  1040. printk(KERN_INFO "%s: cover is closed, "
  1041. "card is now accessible\n",
  1042. mmc_hostname(host->mmc));
  1043. host->protect_card = 0;
  1044. }
  1045. } else {
  1046. if (!host->protect_card) {
  1047. printk(KERN_INFO "%s: cover is open, "
  1048. "card is now inaccessible\n",
  1049. mmc_hostname(host->mmc));
  1050. host->protect_card = 1;
  1051. }
  1052. }
  1053. }
  1054. /*
  1055. * Work Item to notify the core about card insertion/removal
  1056. */
  1057. static void omap_hsmmc_detect(struct work_struct *work)
  1058. {
  1059. struct omap_hsmmc_host *host =
  1060. container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
  1061. struct omap_mmc_slot_data *slot = &mmc_slot(host);
  1062. int carddetect;
  1063. if (host->suspended)
  1064. return;
  1065. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  1066. if (slot->card_detect)
  1067. carddetect = slot->card_detect(host->dev, host->slot_id);
  1068. else {
  1069. omap_hsmmc_protect_card(host);
  1070. carddetect = -ENOSYS;
  1071. }
  1072. if (carddetect)
  1073. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  1074. else
  1075. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  1076. }
  1077. /*
  1078. * ISR for handling card insertion and removal
  1079. */
  1080. static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
  1081. {
  1082. struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
  1083. if (host->suspended)
  1084. return IRQ_HANDLED;
  1085. schedule_work(&host->mmc_carddetect_work);
  1086. return IRQ_HANDLED;
  1087. }
  1088. static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
  1089. struct mmc_data *data)
  1090. {
  1091. int sync_dev;
  1092. if (data->flags & MMC_DATA_WRITE)
  1093. sync_dev = host->dma_line_tx;
  1094. else
  1095. sync_dev = host->dma_line_rx;
  1096. return sync_dev;
  1097. }
  1098. static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
  1099. struct mmc_data *data,
  1100. struct scatterlist *sgl)
  1101. {
  1102. int blksz, nblk, dma_ch;
  1103. dma_ch = host->dma_ch;
  1104. if (data->flags & MMC_DATA_WRITE) {
  1105. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  1106. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  1107. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  1108. sg_dma_address(sgl), 0, 0);
  1109. } else {
  1110. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  1111. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  1112. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  1113. sg_dma_address(sgl), 0, 0);
  1114. }
  1115. blksz = host->data->blksz;
  1116. nblk = sg_dma_len(sgl) / blksz;
  1117. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
  1118. blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
  1119. omap_hsmmc_get_dma_sync_dev(host, data),
  1120. !(data->flags & MMC_DATA_WRITE));
  1121. omap_start_dma(dma_ch);
  1122. }
  1123. /*
  1124. * DMA call back function
  1125. */
  1126. static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
  1127. {
  1128. struct omap_hsmmc_host *host = cb_data;
  1129. struct mmc_data *data = host->mrq->data;
  1130. int dma_ch, req_in_progress;
  1131. if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
  1132. dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
  1133. ch_status);
  1134. return;
  1135. }
  1136. spin_lock(&host->irq_lock);
  1137. if (host->dma_ch < 0) {
  1138. spin_unlock(&host->irq_lock);
  1139. return;
  1140. }
  1141. host->dma_sg_idx++;
  1142. if (host->dma_sg_idx < host->dma_len) {
  1143. /* Fire up the next transfer. */
  1144. omap_hsmmc_config_dma_params(host, data,
  1145. data->sg + host->dma_sg_idx);
  1146. spin_unlock(&host->irq_lock);
  1147. return;
  1148. }
  1149. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
  1150. omap_hsmmc_get_dma_dir(host, data));
  1151. req_in_progress = host->req_in_progress;
  1152. dma_ch = host->dma_ch;
  1153. host->dma_ch = -1;
  1154. spin_unlock(&host->irq_lock);
  1155. omap_free_dma(dma_ch);
  1156. /* If DMA has finished after TC, complete the request */
  1157. if (!req_in_progress) {
  1158. struct mmc_request *mrq = host->mrq;
  1159. host->mrq = NULL;
  1160. mmc_request_done(host->mmc, mrq);
  1161. }
  1162. }
  1163. /*
  1164. * Routine to configure and start DMA for the MMC card
  1165. */
  1166. static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
  1167. struct mmc_request *req)
  1168. {
  1169. int dma_ch = 0, ret = 0, i;
  1170. struct mmc_data *data = req->data;
  1171. /* Sanity check: all the SG entries must be aligned by block size. */
  1172. for (i = 0; i < data->sg_len; i++) {
  1173. struct scatterlist *sgl;
  1174. sgl = data->sg + i;
  1175. if (sgl->length % data->blksz)
  1176. return -EINVAL;
  1177. }
  1178. if ((data->blksz % 4) != 0)
  1179. /* REVISIT: The MMC buffer increments only when MSB is written.
  1180. * Return error for blksz which is non multiple of four.
  1181. */
  1182. return -EINVAL;
  1183. BUG_ON(host->dma_ch != -1);
  1184. ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
  1185. "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
  1186. if (ret != 0) {
  1187. dev_err(mmc_dev(host->mmc),
  1188. "%s: omap_request_dma() failed with %d\n",
  1189. mmc_hostname(host->mmc), ret);
  1190. return ret;
  1191. }
  1192. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  1193. data->sg_len, omap_hsmmc_get_dma_dir(host, data));
  1194. host->dma_ch = dma_ch;
  1195. host->dma_sg_idx = 0;
  1196. omap_hsmmc_config_dma_params(host, data, data->sg);
  1197. return 0;
  1198. }
  1199. static void set_data_timeout(struct omap_hsmmc_host *host,
  1200. unsigned int timeout_ns,
  1201. unsigned int timeout_clks)
  1202. {
  1203. unsigned int timeout, cycle_ns;
  1204. uint32_t reg, clkd, dto = 0;
  1205. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  1206. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  1207. if (clkd == 0)
  1208. clkd = 1;
  1209. cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
  1210. timeout = timeout_ns / cycle_ns;
  1211. timeout += timeout_clks;
  1212. if (timeout) {
  1213. while ((timeout & 0x80000000) == 0) {
  1214. dto += 1;
  1215. timeout <<= 1;
  1216. }
  1217. dto = 31 - dto;
  1218. timeout <<= 1;
  1219. if (timeout && dto)
  1220. dto += 1;
  1221. if (dto >= 13)
  1222. dto -= 13;
  1223. else
  1224. dto = 0;
  1225. if (dto > 14)
  1226. dto = 14;
  1227. }
  1228. reg &= ~DTO_MASK;
  1229. reg |= dto << DTO_SHIFT;
  1230. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  1231. }
  1232. /*
  1233. * Configure block length for MMC/SD cards and initiate the transfer.
  1234. */
  1235. static int
  1236. omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
  1237. {
  1238. int ret;
  1239. host->data = req->data;
  1240. if (req->data == NULL) {
  1241. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  1242. /*
  1243. * Set an arbitrary 100ms data timeout for commands with
  1244. * busy signal.
  1245. */
  1246. if (req->cmd->flags & MMC_RSP_BUSY)
  1247. set_data_timeout(host, 100000000U, 0);
  1248. return 0;
  1249. }
  1250. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  1251. | (req->data->blocks << 16));
  1252. set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
  1253. if (host->use_dma) {
  1254. ret = omap_hsmmc_start_dma_transfer(host, req);
  1255. if (ret != 0) {
  1256. dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
  1257. return ret;
  1258. }
  1259. }
  1260. return 0;
  1261. }
  1262. /*
  1263. * Request function. for read/write operation
  1264. */
  1265. static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1266. {
  1267. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1268. int err;
  1269. BUG_ON(host->req_in_progress);
  1270. BUG_ON(host->dma_ch != -1);
  1271. if (host->protect_card) {
  1272. if (host->reqs_blocked < 3) {
  1273. /*
  1274. * Ensure the controller is left in a consistent
  1275. * state by resetting the command and data state
  1276. * machines.
  1277. */
  1278. omap_hsmmc_reset_controller_fsm(host, SRD);
  1279. omap_hsmmc_reset_controller_fsm(host, SRC);
  1280. host->reqs_blocked += 1;
  1281. }
  1282. req->cmd->error = -EBADF;
  1283. if (req->data)
  1284. req->data->error = -EBADF;
  1285. req->cmd->retries = 0;
  1286. mmc_request_done(mmc, req);
  1287. return;
  1288. } else if (host->reqs_blocked)
  1289. host->reqs_blocked = 0;
  1290. WARN_ON(host->mrq != NULL);
  1291. host->mrq = req;
  1292. err = omap_hsmmc_prepare_data(host, req);
  1293. if (err) {
  1294. req->cmd->error = err;
  1295. if (req->data)
  1296. req->data->error = err;
  1297. host->mrq = NULL;
  1298. mmc_request_done(mmc, req);
  1299. return;
  1300. }
  1301. omap_hsmmc_start_command(host, req->cmd, req->data);
  1302. }
  1303. /* Routine to configure clock values. Exposed API to core */
  1304. static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1305. {
  1306. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1307. u16 dsor = 0;
  1308. unsigned long regval;
  1309. unsigned long timeout;
  1310. u32 con;
  1311. int do_send_init_stream = 0;
  1312. mmc_host_enable(host->mmc);
  1313. if (ios->power_mode != host->power_mode) {
  1314. switch (ios->power_mode) {
  1315. case MMC_POWER_OFF:
  1316. mmc_slot(host).set_power(host->dev, host->slot_id,
  1317. 0, 0);
  1318. host->vdd = 0;
  1319. break;
  1320. case MMC_POWER_UP:
  1321. mmc_slot(host).set_power(host->dev, host->slot_id,
  1322. 1, ios->vdd);
  1323. host->vdd = ios->vdd;
  1324. break;
  1325. case MMC_POWER_ON:
  1326. do_send_init_stream = 1;
  1327. break;
  1328. }
  1329. host->power_mode = ios->power_mode;
  1330. }
  1331. /* FIXME: set registers based only on changes to ios */
  1332. con = OMAP_HSMMC_READ(host->base, CON);
  1333. switch (mmc->ios.bus_width) {
  1334. case MMC_BUS_WIDTH_8:
  1335. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  1336. break;
  1337. case MMC_BUS_WIDTH_4:
  1338. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  1339. OMAP_HSMMC_WRITE(host->base, HCTL,
  1340. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  1341. break;
  1342. case MMC_BUS_WIDTH_1:
  1343. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  1344. OMAP_HSMMC_WRITE(host->base, HCTL,
  1345. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  1346. break;
  1347. }
  1348. if (host->id == OMAP_MMC1_DEVID) {
  1349. /* Only MMC1 can interface at 3V without some flavor
  1350. * of external transceiver; but they all handle 1.8V.
  1351. */
  1352. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  1353. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  1354. /*
  1355. * The mmc_select_voltage fn of the core does
  1356. * not seem to set the power_mode to
  1357. * MMC_POWER_UP upon recalculating the voltage.
  1358. * vdd 1.8v.
  1359. */
  1360. if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
  1361. dev_dbg(mmc_dev(host->mmc),
  1362. "Switch operation failed\n");
  1363. }
  1364. }
  1365. if (ios->clock) {
  1366. dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
  1367. if (dsor < 1)
  1368. dsor = 1;
  1369. if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
  1370. dsor++;
  1371. if (dsor > 250)
  1372. dsor = 250;
  1373. }
  1374. omap_hsmmc_stop_clock(host);
  1375. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  1376. regval = regval & ~(CLKD_MASK);
  1377. regval = regval | (dsor << 6) | (DTO << 16);
  1378. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  1379. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  1380. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  1381. /* Wait till the ICS bit is set */
  1382. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  1383. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  1384. && time_before(jiffies, timeout))
  1385. msleep(1);
  1386. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  1387. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  1388. if (do_send_init_stream)
  1389. send_init_stream(host);
  1390. con = OMAP_HSMMC_READ(host->base, CON);
  1391. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  1392. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  1393. else
  1394. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  1395. if (host->power_mode == MMC_POWER_OFF)
  1396. mmc_host_disable(host->mmc);
  1397. else
  1398. mmc_host_lazy_disable(host->mmc);
  1399. }
  1400. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  1401. {
  1402. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1403. if (!mmc_slot(host).card_detect)
  1404. return -ENOSYS;
  1405. return mmc_slot(host).card_detect(host->dev, host->slot_id);
  1406. }
  1407. static int omap_hsmmc_get_ro(struct mmc_host *mmc)
  1408. {
  1409. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1410. if (!mmc_slot(host).get_ro)
  1411. return -ENOSYS;
  1412. return mmc_slot(host).get_ro(host->dev, 0);
  1413. }
  1414. static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1415. {
  1416. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1417. if (mmc_slot(host).init_card)
  1418. mmc_slot(host).init_card(card);
  1419. }
  1420. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
  1421. {
  1422. u32 hctl, capa, value;
  1423. /* Only MMC1 supports 3.0V */
  1424. if (host->id == OMAP_MMC1_DEVID) {
  1425. hctl = SDVS30;
  1426. capa = VS30 | VS18;
  1427. } else {
  1428. hctl = SDVS18;
  1429. capa = VS18;
  1430. }
  1431. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1432. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1433. value = OMAP_HSMMC_READ(host->base, CAPA);
  1434. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1435. /* Set the controller to AUTO IDLE mode */
  1436. value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
  1437. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
  1438. /* Set SD bus power bit */
  1439. set_sd_bus_power(host);
  1440. }
  1441. /*
  1442. * Dynamic power saving handling, FSM:
  1443. * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
  1444. * ^___________| | |
  1445. * |______________________|______________________|
  1446. *
  1447. * ENABLED: mmc host is fully functional
  1448. * DISABLED: fclk is off
  1449. * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
  1450. * REGSLEEP: fclk is off, voltage regulator is asleep
  1451. * OFF: fclk is off, voltage regulator is off
  1452. *
  1453. * Transition handlers return the timeout for the next state transition
  1454. * or negative error.
  1455. */
  1456. enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
  1457. /* Handler for [ENABLED -> DISABLED] transition */
  1458. static int omap_hsmmc_enabled_to_disabled(struct omap_hsmmc_host *host)
  1459. {
  1460. omap_hsmmc_context_save(host);
  1461. clk_disable(host->fclk);
  1462. host->dpm_state = DISABLED;
  1463. dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
  1464. if (host->power_mode == MMC_POWER_OFF)
  1465. return 0;
  1466. return OMAP_MMC_SLEEP_TIMEOUT;
  1467. }
  1468. /* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
  1469. static int omap_hsmmc_disabled_to_sleep(struct omap_hsmmc_host *host)
  1470. {
  1471. int err, new_state;
  1472. if (!mmc_try_claim_host(host->mmc))
  1473. return 0;
  1474. clk_enable(host->fclk);
  1475. omap_hsmmc_context_restore(host);
  1476. if (mmc_card_can_sleep(host->mmc)) {
  1477. err = mmc_card_sleep(host->mmc);
  1478. if (err < 0) {
  1479. clk_disable(host->fclk);
  1480. mmc_release_host(host->mmc);
  1481. return err;
  1482. }
  1483. new_state = CARDSLEEP;
  1484. } else {
  1485. new_state = REGSLEEP;
  1486. }
  1487. if (mmc_slot(host).set_sleep)
  1488. mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
  1489. new_state == CARDSLEEP);
  1490. /* FIXME: turn off bus power and perhaps interrupts too */
  1491. clk_disable(host->fclk);
  1492. host->dpm_state = new_state;
  1493. mmc_release_host(host->mmc);
  1494. dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
  1495. host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
  1496. if (mmc_slot(host).no_off)
  1497. return 0;
  1498. if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
  1499. mmc_slot(host).card_detect ||
  1500. (mmc_slot(host).get_cover_state &&
  1501. mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
  1502. return OMAP_MMC_OFF_TIMEOUT;
  1503. return 0;
  1504. }
  1505. /* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
  1506. static int omap_hsmmc_sleep_to_off(struct omap_hsmmc_host *host)
  1507. {
  1508. if (!mmc_try_claim_host(host->mmc))
  1509. return 0;
  1510. if (mmc_slot(host).no_off)
  1511. return 0;
  1512. if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
  1513. mmc_slot(host).card_detect ||
  1514. (mmc_slot(host).get_cover_state &&
  1515. mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
  1516. mmc_release_host(host->mmc);
  1517. return 0;
  1518. }
  1519. mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  1520. host->vdd = 0;
  1521. host->power_mode = MMC_POWER_OFF;
  1522. dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
  1523. host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
  1524. host->dpm_state = OFF;
  1525. mmc_release_host(host->mmc);
  1526. return 0;
  1527. }
  1528. /* Handler for [DISABLED -> ENABLED] transition */
  1529. static int omap_hsmmc_disabled_to_enabled(struct omap_hsmmc_host *host)
  1530. {
  1531. int err;
  1532. err = clk_enable(host->fclk);
  1533. if (err < 0)
  1534. return err;
  1535. omap_hsmmc_context_restore(host);
  1536. host->dpm_state = ENABLED;
  1537. dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
  1538. return 0;
  1539. }
  1540. /* Handler for [SLEEP -> ENABLED] transition */
  1541. static int omap_hsmmc_sleep_to_enabled(struct omap_hsmmc_host *host)
  1542. {
  1543. if (!mmc_try_claim_host(host->mmc))
  1544. return 0;
  1545. clk_enable(host->fclk);
  1546. omap_hsmmc_context_restore(host);
  1547. if (mmc_slot(host).set_sleep)
  1548. mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
  1549. host->vdd, host->dpm_state == CARDSLEEP);
  1550. if (mmc_card_can_sleep(host->mmc))
  1551. mmc_card_awake(host->mmc);
  1552. dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
  1553. host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
  1554. host->dpm_state = ENABLED;
  1555. mmc_release_host(host->mmc);
  1556. return 0;
  1557. }
  1558. /* Handler for [OFF -> ENABLED] transition */
  1559. static int omap_hsmmc_off_to_enabled(struct omap_hsmmc_host *host)
  1560. {
  1561. clk_enable(host->fclk);
  1562. omap_hsmmc_context_restore(host);
  1563. omap_hsmmc_conf_bus_power(host);
  1564. mmc_power_restore_host(host->mmc);
  1565. host->dpm_state = ENABLED;
  1566. dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
  1567. return 0;
  1568. }
  1569. /*
  1570. * Bring MMC host to ENABLED from any other PM state.
  1571. */
  1572. static int omap_hsmmc_enable(struct mmc_host *mmc)
  1573. {
  1574. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1575. switch (host->dpm_state) {
  1576. case DISABLED:
  1577. return omap_hsmmc_disabled_to_enabled(host);
  1578. case CARDSLEEP:
  1579. case REGSLEEP:
  1580. return omap_hsmmc_sleep_to_enabled(host);
  1581. case OFF:
  1582. return omap_hsmmc_off_to_enabled(host);
  1583. default:
  1584. dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
  1585. return -EINVAL;
  1586. }
  1587. }
  1588. /*
  1589. * Bring MMC host in PM state (one level deeper).
  1590. */
  1591. static int omap_hsmmc_disable(struct mmc_host *mmc, int lazy)
  1592. {
  1593. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1594. switch (host->dpm_state) {
  1595. case ENABLED: {
  1596. int delay;
  1597. delay = omap_hsmmc_enabled_to_disabled(host);
  1598. if (lazy || delay < 0)
  1599. return delay;
  1600. return 0;
  1601. }
  1602. case DISABLED:
  1603. return omap_hsmmc_disabled_to_sleep(host);
  1604. case CARDSLEEP:
  1605. case REGSLEEP:
  1606. return omap_hsmmc_sleep_to_off(host);
  1607. default:
  1608. dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
  1609. return -EINVAL;
  1610. }
  1611. }
  1612. static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
  1613. {
  1614. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1615. int err;
  1616. err = clk_enable(host->fclk);
  1617. if (err)
  1618. return err;
  1619. dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
  1620. omap_hsmmc_context_restore(host);
  1621. return 0;
  1622. }
  1623. static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
  1624. {
  1625. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1626. omap_hsmmc_context_save(host);
  1627. clk_disable(host->fclk);
  1628. dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
  1629. return 0;
  1630. }
  1631. static const struct mmc_host_ops omap_hsmmc_ops = {
  1632. .enable = omap_hsmmc_enable_fclk,
  1633. .disable = omap_hsmmc_disable_fclk,
  1634. .request = omap_hsmmc_request,
  1635. .set_ios = omap_hsmmc_set_ios,
  1636. .get_cd = omap_hsmmc_get_cd,
  1637. .get_ro = omap_hsmmc_get_ro,
  1638. .init_card = omap_hsmmc_init_card,
  1639. /* NYET -- enable_sdio_irq */
  1640. };
  1641. static const struct mmc_host_ops omap_hsmmc_ps_ops = {
  1642. .enable = omap_hsmmc_enable,
  1643. .disable = omap_hsmmc_disable,
  1644. .request = omap_hsmmc_request,
  1645. .set_ios = omap_hsmmc_set_ios,
  1646. .get_cd = omap_hsmmc_get_cd,
  1647. .get_ro = omap_hsmmc_get_ro,
  1648. .init_card = omap_hsmmc_init_card,
  1649. /* NYET -- enable_sdio_irq */
  1650. };
  1651. #ifdef CONFIG_DEBUG_FS
  1652. static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
  1653. {
  1654. struct mmc_host *mmc = s->private;
  1655. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1656. int context_loss = 0;
  1657. if (host->pdata->get_context_loss_count)
  1658. context_loss = host->pdata->get_context_loss_count(host->dev);
  1659. seq_printf(s, "mmc%d:\n"
  1660. " enabled:\t%d\n"
  1661. " dpm_state:\t%d\n"
  1662. " nesting_cnt:\t%d\n"
  1663. " ctx_loss:\t%d:%d\n"
  1664. "\nregs:\n",
  1665. mmc->index, mmc->enabled ? 1 : 0,
  1666. host->dpm_state, mmc->nesting_cnt,
  1667. host->context_loss, context_loss);
  1668. if (host->suspended || host->dpm_state == OFF) {
  1669. seq_printf(s, "host suspended, can't read registers\n");
  1670. return 0;
  1671. }
  1672. if (clk_enable(host->fclk) != 0) {
  1673. seq_printf(s, "can't read the regs\n");
  1674. return 0;
  1675. }
  1676. seq_printf(s, "SYSCONFIG:\t0x%08x\n",
  1677. OMAP_HSMMC_READ(host->base, SYSCONFIG));
  1678. seq_printf(s, "CON:\t\t0x%08x\n",
  1679. OMAP_HSMMC_READ(host->base, CON));
  1680. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1681. OMAP_HSMMC_READ(host->base, HCTL));
  1682. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1683. OMAP_HSMMC_READ(host->base, SYSCTL));
  1684. seq_printf(s, "IE:\t\t0x%08x\n",
  1685. OMAP_HSMMC_READ(host->base, IE));
  1686. seq_printf(s, "ISE:\t\t0x%08x\n",
  1687. OMAP_HSMMC_READ(host->base, ISE));
  1688. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1689. OMAP_HSMMC_READ(host->base, CAPA));
  1690. clk_disable(host->fclk);
  1691. return 0;
  1692. }
  1693. static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
  1694. {
  1695. return single_open(file, omap_hsmmc_regs_show, inode->i_private);
  1696. }
  1697. static const struct file_operations mmc_regs_fops = {
  1698. .open = omap_hsmmc_regs_open,
  1699. .read = seq_read,
  1700. .llseek = seq_lseek,
  1701. .release = single_release,
  1702. };
  1703. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1704. {
  1705. if (mmc->debugfs_root)
  1706. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1707. mmc, &mmc_regs_fops);
  1708. }
  1709. #else
  1710. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1711. {
  1712. }
  1713. #endif
  1714. static int __init omap_hsmmc_probe(struct platform_device *pdev)
  1715. {
  1716. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1717. struct mmc_host *mmc;
  1718. struct omap_hsmmc_host *host = NULL;
  1719. struct resource *res;
  1720. int ret, irq;
  1721. if (pdata == NULL) {
  1722. dev_err(&pdev->dev, "Platform Data is missing\n");
  1723. return -ENXIO;
  1724. }
  1725. if (pdata->nr_slots == 0) {
  1726. dev_err(&pdev->dev, "No Slots\n");
  1727. return -ENXIO;
  1728. }
  1729. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1730. irq = platform_get_irq(pdev, 0);
  1731. if (res == NULL || irq < 0)
  1732. return -ENXIO;
  1733. res->start += pdata->reg_offset;
  1734. res->end += pdata->reg_offset;
  1735. res = request_mem_region(res->start, res->end - res->start + 1,
  1736. pdev->name);
  1737. if (res == NULL)
  1738. return -EBUSY;
  1739. ret = omap_hsmmc_gpio_init(pdata);
  1740. if (ret)
  1741. goto err;
  1742. mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
  1743. if (!mmc) {
  1744. ret = -ENOMEM;
  1745. goto err_alloc;
  1746. }
  1747. host = mmc_priv(mmc);
  1748. host->mmc = mmc;
  1749. host->pdata = pdata;
  1750. host->dev = &pdev->dev;
  1751. host->use_dma = 1;
  1752. host->dev->dma_mask = &pdata->dma_mask;
  1753. host->dma_ch = -1;
  1754. host->irq = irq;
  1755. host->id = pdev->id;
  1756. host->slot_id = 0;
  1757. host->mapbase = res->start;
  1758. host->base = ioremap(host->mapbase, SZ_4K);
  1759. host->power_mode = MMC_POWER_OFF;
  1760. platform_set_drvdata(pdev, host);
  1761. INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
  1762. if (mmc_slot(host).power_saving)
  1763. mmc->ops = &omap_hsmmc_ps_ops;
  1764. else
  1765. mmc->ops = &omap_hsmmc_ops;
  1766. /*
  1767. * If regulator_disable can only put vcc_aux to sleep then there is
  1768. * no off state.
  1769. */
  1770. if (mmc_slot(host).vcc_aux_disable_is_sleep)
  1771. mmc_slot(host).no_off = 1;
  1772. mmc->f_min = 400000;
  1773. mmc->f_max = 52000000;
  1774. spin_lock_init(&host->irq_lock);
  1775. host->iclk = clk_get(&pdev->dev, "ick");
  1776. if (IS_ERR(host->iclk)) {
  1777. ret = PTR_ERR(host->iclk);
  1778. host->iclk = NULL;
  1779. goto err1;
  1780. }
  1781. host->fclk = clk_get(&pdev->dev, "fck");
  1782. if (IS_ERR(host->fclk)) {
  1783. ret = PTR_ERR(host->fclk);
  1784. host->fclk = NULL;
  1785. clk_put(host->iclk);
  1786. goto err1;
  1787. }
  1788. omap_hsmmc_context_save(host);
  1789. mmc->caps |= MMC_CAP_DISABLE;
  1790. mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
  1791. /* we start off in DISABLED state */
  1792. host->dpm_state = DISABLED;
  1793. if (mmc_host_enable(host->mmc) != 0) {
  1794. clk_put(host->iclk);
  1795. clk_put(host->fclk);
  1796. goto err1;
  1797. }
  1798. if (clk_enable(host->iclk) != 0) {
  1799. mmc_host_disable(host->mmc);
  1800. clk_put(host->iclk);
  1801. clk_put(host->fclk);
  1802. goto err1;
  1803. }
  1804. if (cpu_is_omap2430()) {
  1805. host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
  1806. /*
  1807. * MMC can still work without debounce clock.
  1808. */
  1809. if (IS_ERR(host->dbclk))
  1810. dev_warn(mmc_dev(host->mmc),
  1811. "Failed to get debounce clock\n");
  1812. else
  1813. host->got_dbclk = 1;
  1814. if (host->got_dbclk)
  1815. if (clk_enable(host->dbclk) != 0)
  1816. dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
  1817. " clk failed\n");
  1818. }
  1819. /* Since we do only SG emulation, we can have as many segs
  1820. * as we want. */
  1821. mmc->max_segs = 1024;
  1822. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1823. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1824. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1825. mmc->max_seg_size = mmc->max_req_size;
  1826. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1827. MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
  1828. mmc->caps |= mmc_slot(host).caps;
  1829. if (mmc->caps & MMC_CAP_8_BIT_DATA)
  1830. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1831. if (mmc_slot(host).nonremovable)
  1832. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1833. omap_hsmmc_conf_bus_power(host);
  1834. /* Select DMA lines */
  1835. switch (host->id) {
  1836. case OMAP_MMC1_DEVID:
  1837. host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
  1838. host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
  1839. break;
  1840. case OMAP_MMC2_DEVID:
  1841. host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
  1842. host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
  1843. break;
  1844. case OMAP_MMC3_DEVID:
  1845. host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
  1846. host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
  1847. break;
  1848. case OMAP_MMC4_DEVID:
  1849. host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
  1850. host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
  1851. break;
  1852. case OMAP_MMC5_DEVID:
  1853. host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
  1854. host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
  1855. break;
  1856. default:
  1857. dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
  1858. goto err_irq;
  1859. }
  1860. /* Request IRQ for MMC operations */
  1861. ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
  1862. mmc_hostname(mmc), host);
  1863. if (ret) {
  1864. dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1865. goto err_irq;
  1866. }
  1867. if (pdata->init != NULL) {
  1868. if (pdata->init(&pdev->dev) != 0) {
  1869. dev_dbg(mmc_dev(host->mmc),
  1870. "Unable to configure MMC IRQs\n");
  1871. goto err_irq_cd_init;
  1872. }
  1873. }
  1874. if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
  1875. ret = omap_hsmmc_reg_get(host);
  1876. if (ret)
  1877. goto err_reg;
  1878. host->use_reg = 1;
  1879. }
  1880. mmc->ocr_avail = mmc_slot(host).ocr_mask;
  1881. /* Request IRQ for card detect */
  1882. if ((mmc_slot(host).card_detect_irq)) {
  1883. ret = request_irq(mmc_slot(host).card_detect_irq,
  1884. omap_hsmmc_cd_handler,
  1885. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
  1886. | IRQF_DISABLED,
  1887. mmc_hostname(mmc), host);
  1888. if (ret) {
  1889. dev_dbg(mmc_dev(host->mmc),
  1890. "Unable to grab MMC CD IRQ\n");
  1891. goto err_irq_cd;
  1892. }
  1893. pdata->suspend = omap_hsmmc_suspend_cdirq;
  1894. pdata->resume = omap_hsmmc_resume_cdirq;
  1895. }
  1896. omap_hsmmc_disable_irq(host);
  1897. mmc_host_lazy_disable(host->mmc);
  1898. omap_hsmmc_protect_card(host);
  1899. mmc_add_host(mmc);
  1900. if (mmc_slot(host).name != NULL) {
  1901. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1902. if (ret < 0)
  1903. goto err_slot_name;
  1904. }
  1905. if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
  1906. ret = device_create_file(&mmc->class_dev,
  1907. &dev_attr_cover_switch);
  1908. if (ret < 0)
  1909. goto err_slot_name;
  1910. }
  1911. omap_hsmmc_debugfs(mmc);
  1912. return 0;
  1913. err_slot_name:
  1914. mmc_remove_host(mmc);
  1915. free_irq(mmc_slot(host).card_detect_irq, host);
  1916. err_irq_cd:
  1917. if (host->use_reg)
  1918. omap_hsmmc_reg_put(host);
  1919. err_reg:
  1920. if (host->pdata->cleanup)
  1921. host->pdata->cleanup(&pdev->dev);
  1922. err_irq_cd_init:
  1923. free_irq(host->irq, host);
  1924. err_irq:
  1925. mmc_host_disable(host->mmc);
  1926. clk_disable(host->iclk);
  1927. clk_put(host->fclk);
  1928. clk_put(host->iclk);
  1929. if (host->got_dbclk) {
  1930. clk_disable(host->dbclk);
  1931. clk_put(host->dbclk);
  1932. }
  1933. err1:
  1934. iounmap(host->base);
  1935. platform_set_drvdata(pdev, NULL);
  1936. mmc_free_host(mmc);
  1937. err_alloc:
  1938. omap_hsmmc_gpio_free(pdata);
  1939. err:
  1940. release_mem_region(res->start, res->end - res->start + 1);
  1941. return ret;
  1942. }
  1943. static int omap_hsmmc_remove(struct platform_device *pdev)
  1944. {
  1945. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1946. struct resource *res;
  1947. if (host) {
  1948. mmc_host_enable(host->mmc);
  1949. mmc_remove_host(host->mmc);
  1950. if (host->use_reg)
  1951. omap_hsmmc_reg_put(host);
  1952. if (host->pdata->cleanup)
  1953. host->pdata->cleanup(&pdev->dev);
  1954. free_irq(host->irq, host);
  1955. if (mmc_slot(host).card_detect_irq)
  1956. free_irq(mmc_slot(host).card_detect_irq, host);
  1957. flush_work_sync(&host->mmc_carddetect_work);
  1958. mmc_host_disable(host->mmc);
  1959. clk_disable(host->iclk);
  1960. clk_put(host->fclk);
  1961. clk_put(host->iclk);
  1962. if (host->got_dbclk) {
  1963. clk_disable(host->dbclk);
  1964. clk_put(host->dbclk);
  1965. }
  1966. mmc_free_host(host->mmc);
  1967. iounmap(host->base);
  1968. omap_hsmmc_gpio_free(pdev->dev.platform_data);
  1969. }
  1970. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1971. if (res)
  1972. release_mem_region(res->start, res->end - res->start + 1);
  1973. platform_set_drvdata(pdev, NULL);
  1974. return 0;
  1975. }
  1976. #ifdef CONFIG_PM
  1977. static int omap_hsmmc_suspend(struct device *dev)
  1978. {
  1979. int ret = 0;
  1980. struct platform_device *pdev = to_platform_device(dev);
  1981. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1982. if (host && host->suspended)
  1983. return 0;
  1984. if (host) {
  1985. host->suspended = 1;
  1986. if (host->pdata->suspend) {
  1987. ret = host->pdata->suspend(&pdev->dev,
  1988. host->slot_id);
  1989. if (ret) {
  1990. dev_dbg(mmc_dev(host->mmc),
  1991. "Unable to handle MMC board"
  1992. " level suspend\n");
  1993. host->suspended = 0;
  1994. return ret;
  1995. }
  1996. }
  1997. cancel_work_sync(&host->mmc_carddetect_work);
  1998. ret = mmc_suspend_host(host->mmc);
  1999. mmc_host_enable(host->mmc);
  2000. if (ret == 0) {
  2001. omap_hsmmc_disable_irq(host);
  2002. OMAP_HSMMC_WRITE(host->base, HCTL,
  2003. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  2004. mmc_host_disable(host->mmc);
  2005. clk_disable(host->iclk);
  2006. if (host->got_dbclk)
  2007. clk_disable(host->dbclk);
  2008. } else {
  2009. host->suspended = 0;
  2010. if (host->pdata->resume) {
  2011. ret = host->pdata->resume(&pdev->dev,
  2012. host->slot_id);
  2013. if (ret)
  2014. dev_dbg(mmc_dev(host->mmc),
  2015. "Unmask interrupt failed\n");
  2016. }
  2017. mmc_host_disable(host->mmc);
  2018. }
  2019. }
  2020. return ret;
  2021. }
  2022. /* Routine to resume the MMC device */
  2023. static int omap_hsmmc_resume(struct device *dev)
  2024. {
  2025. int ret = 0;
  2026. struct platform_device *pdev = to_platform_device(dev);
  2027. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  2028. if (host && !host->suspended)
  2029. return 0;
  2030. if (host) {
  2031. ret = clk_enable(host->iclk);
  2032. if (ret)
  2033. goto clk_en_err;
  2034. if (mmc_host_enable(host->mmc) != 0) {
  2035. clk_disable(host->iclk);
  2036. goto clk_en_err;
  2037. }
  2038. if (host->got_dbclk)
  2039. clk_enable(host->dbclk);
  2040. omap_hsmmc_conf_bus_power(host);
  2041. if (host->pdata->resume) {
  2042. ret = host->pdata->resume(&pdev->dev, host->slot_id);
  2043. if (ret)
  2044. dev_dbg(mmc_dev(host->mmc),
  2045. "Unmask interrupt failed\n");
  2046. }
  2047. omap_hsmmc_protect_card(host);
  2048. /* Notify the core to resume the host */
  2049. ret = mmc_resume_host(host->mmc);
  2050. if (ret == 0)
  2051. host->suspended = 0;
  2052. mmc_host_lazy_disable(host->mmc);
  2053. }
  2054. return ret;
  2055. clk_en_err:
  2056. dev_dbg(mmc_dev(host->mmc),
  2057. "Failed to enable MMC clocks during resume\n");
  2058. return ret;
  2059. }
  2060. #else
  2061. #define omap_hsmmc_suspend NULL
  2062. #define omap_hsmmc_resume NULL
  2063. #endif
  2064. static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
  2065. .suspend = omap_hsmmc_suspend,
  2066. .resume = omap_hsmmc_resume,
  2067. };
  2068. static struct platform_driver omap_hsmmc_driver = {
  2069. .remove = omap_hsmmc_remove,
  2070. .driver = {
  2071. .name = DRIVER_NAME,
  2072. .owner = THIS_MODULE,
  2073. .pm = &omap_hsmmc_dev_pm_ops,
  2074. },
  2075. };
  2076. static int __init omap_hsmmc_init(void)
  2077. {
  2078. /* Register the MMC driver */
  2079. return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
  2080. }
  2081. static void __exit omap_hsmmc_cleanup(void)
  2082. {
  2083. /* Unregister MMC driver */
  2084. platform_driver_unregister(&omap_hsmmc_driver);
  2085. }
  2086. module_init(omap_hsmmc_init);
  2087. module_exit(omap_hsmmc_cleanup);
  2088. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  2089. MODULE_LICENSE("GPL");
  2090. MODULE_ALIAS("platform:" DRIVER_NAME);
  2091. MODULE_AUTHOR("Texas Instruments Inc");