omap_hsmmc.c 59 KB

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