omap_hsmmc.c 59 KB

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