omap_hsmmc.c 60 KB

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