omap_hsmmc.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  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/kernel.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/sizes.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/timer.h>
  29. #include <linux/clk.h>
  30. #include <linux/of.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/of_device.h>
  34. #include <linux/omap-dmaengine.h>
  35. #include <linux/mmc/host.h>
  36. #include <linux/mmc/core.h>
  37. #include <linux/mmc/mmc.h>
  38. #include <linux/mmc/slot-gpio.h>
  39. #include <linux/io.h>
  40. #include <linux/irq.h>
  41. #include <linux/gpio.h>
  42. #include <linux/regulator/consumer.h>
  43. #include <linux/pinctrl/consumer.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/platform_data/hsmmc-omap.h>
  46. /* OMAP HSMMC Host Controller Registers */
  47. #define OMAP_HSMMC_SYSSTATUS 0x0014
  48. #define OMAP_HSMMC_CON 0x002C
  49. #define OMAP_HSMMC_SDMASA 0x0100
  50. #define OMAP_HSMMC_BLK 0x0104
  51. #define OMAP_HSMMC_ARG 0x0108
  52. #define OMAP_HSMMC_CMD 0x010C
  53. #define OMAP_HSMMC_RSP10 0x0110
  54. #define OMAP_HSMMC_RSP32 0x0114
  55. #define OMAP_HSMMC_RSP54 0x0118
  56. #define OMAP_HSMMC_RSP76 0x011C
  57. #define OMAP_HSMMC_DATA 0x0120
  58. #define OMAP_HSMMC_PSTATE 0x0124
  59. #define OMAP_HSMMC_HCTL 0x0128
  60. #define OMAP_HSMMC_SYSCTL 0x012C
  61. #define OMAP_HSMMC_STAT 0x0130
  62. #define OMAP_HSMMC_IE 0x0134
  63. #define OMAP_HSMMC_ISE 0x0138
  64. #define OMAP_HSMMC_AC12 0x013C
  65. #define OMAP_HSMMC_CAPA 0x0140
  66. #define VS18 (1 << 26)
  67. #define VS30 (1 << 25)
  68. #define HSS (1 << 21)
  69. #define SDVS18 (0x5 << 9)
  70. #define SDVS30 (0x6 << 9)
  71. #define SDVS33 (0x7 << 9)
  72. #define SDVS_MASK 0x00000E00
  73. #define SDVSCLR 0xFFFFF1FF
  74. #define SDVSDET 0x00000400
  75. #define AUTOIDLE 0x1
  76. #define SDBP (1 << 8)
  77. #define DTO 0xe
  78. #define ICE 0x1
  79. #define ICS 0x2
  80. #define CEN (1 << 2)
  81. #define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
  82. #define CLKD_MASK 0x0000FFC0
  83. #define CLKD_SHIFT 6
  84. #define DTO_MASK 0x000F0000
  85. #define DTO_SHIFT 16
  86. #define INIT_STREAM (1 << 1)
  87. #define ACEN_ACMD23 (2 << 2)
  88. #define DP_SELECT (1 << 21)
  89. #define DDIR (1 << 4)
  90. #define DMAE 0x1
  91. #define MSBS (1 << 5)
  92. #define BCE (1 << 1)
  93. #define FOUR_BIT (1 << 1)
  94. #define HSPE (1 << 2)
  95. #define IWE (1 << 24)
  96. #define DDR (1 << 19)
  97. #define CLKEXTFREE (1 << 16)
  98. #define CTPL (1 << 11)
  99. #define DW8 (1 << 5)
  100. #define OD 0x1
  101. #define STAT_CLEAR 0xFFFFFFFF
  102. #define INIT_STREAM_CMD 0x00000000
  103. #define DUAL_VOLT_OCR_BIT 7
  104. #define SRC (1 << 25)
  105. #define SRD (1 << 26)
  106. #define SOFTRESET (1 << 1)
  107. /* PSTATE */
  108. #define DLEV_DAT(x) (1 << (20 + (x)))
  109. /* Interrupt masks for IE and ISE register */
  110. #define CC_EN (1 << 0)
  111. #define TC_EN (1 << 1)
  112. #define BWR_EN (1 << 4)
  113. #define BRR_EN (1 << 5)
  114. #define CIRQ_EN (1 << 8)
  115. #define ERR_EN (1 << 15)
  116. #define CTO_EN (1 << 16)
  117. #define CCRC_EN (1 << 17)
  118. #define CEB_EN (1 << 18)
  119. #define CIE_EN (1 << 19)
  120. #define DTO_EN (1 << 20)
  121. #define DCRC_EN (1 << 21)
  122. #define DEB_EN (1 << 22)
  123. #define ACE_EN (1 << 24)
  124. #define CERR_EN (1 << 28)
  125. #define BADA_EN (1 << 29)
  126. #define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
  127. DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
  128. BRR_EN | BWR_EN | TC_EN | CC_EN)
  129. #define CNI (1 << 7)
  130. #define ACIE (1 << 4)
  131. #define ACEB (1 << 3)
  132. #define ACCE (1 << 2)
  133. #define ACTO (1 << 1)
  134. #define ACNE (1 << 0)
  135. #define MMC_AUTOSUSPEND_DELAY 100
  136. #define MMC_TIMEOUT_MS 20 /* 20 mSec */
  137. #define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
  138. #define OMAP_MMC_MIN_CLOCK 400000
  139. #define OMAP_MMC_MAX_CLOCK 52000000
  140. #define DRIVER_NAME "omap_hsmmc"
  141. #define VDD_1V8 1800000 /* 180000 uV */
  142. #define VDD_3V0 3000000 /* 300000 uV */
  143. #define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
  144. /*
  145. * One controller can have multiple slots, like on some omap boards using
  146. * omap.c controller driver. Luckily this is not currently done on any known
  147. * omap_hsmmc.c device.
  148. */
  149. #define mmc_pdata(host) host->pdata
  150. /*
  151. * MMC Host controller read/write API's
  152. */
  153. #define OMAP_HSMMC_READ(base, reg) \
  154. __raw_readl((base) + OMAP_HSMMC_##reg)
  155. #define OMAP_HSMMC_WRITE(base, reg, val) \
  156. __raw_writel((val), (base) + OMAP_HSMMC_##reg)
  157. struct omap_hsmmc_next {
  158. unsigned int dma_len;
  159. s32 cookie;
  160. };
  161. struct omap_hsmmc_host {
  162. struct device *dev;
  163. struct mmc_host *mmc;
  164. struct mmc_request *mrq;
  165. struct mmc_command *cmd;
  166. struct mmc_data *data;
  167. struct clk *fclk;
  168. struct clk *dbclk;
  169. /*
  170. * vcc == configured supply
  171. * vcc_aux == optional
  172. * - MMC1, supply for DAT4..DAT7
  173. * - MMC2/MMC2, external level shifter voltage supply, for
  174. * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
  175. */
  176. struct regulator *vcc;
  177. struct regulator *vcc_aux;
  178. struct regulator *pbias;
  179. bool pbias_enabled;
  180. void __iomem *base;
  181. resource_size_t mapbase;
  182. spinlock_t irq_lock; /* Prevent races with irq handler */
  183. unsigned int dma_len;
  184. unsigned int dma_sg_idx;
  185. unsigned char bus_mode;
  186. unsigned char power_mode;
  187. int suspended;
  188. u32 con;
  189. u32 hctl;
  190. u32 sysctl;
  191. u32 capa;
  192. int irq;
  193. int wake_irq;
  194. int use_dma, dma_ch;
  195. struct dma_chan *tx_chan;
  196. struct dma_chan *rx_chan;
  197. int response_busy;
  198. int context_loss;
  199. int protect_card;
  200. int reqs_blocked;
  201. int use_reg;
  202. int req_in_progress;
  203. unsigned long clk_rate;
  204. unsigned int flags;
  205. #define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
  206. #define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
  207. #define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
  208. struct omap_hsmmc_next next_data;
  209. struct omap_hsmmc_platform_data *pdata;
  210. /* return MMC cover switch state, can be NULL if not supported.
  211. *
  212. * possible return values:
  213. * 0 - closed
  214. * 1 - open
  215. */
  216. int (*get_cover_state)(struct device *dev);
  217. int (*card_detect)(struct device *dev);
  218. };
  219. struct omap_mmc_of_data {
  220. u32 reg_offset;
  221. u8 controller_flags;
  222. };
  223. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
  224. static int omap_hsmmc_card_detect(struct device *dev)
  225. {
  226. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  227. return mmc_gpio_get_cd(host->mmc);
  228. }
  229. static int omap_hsmmc_get_cover_state(struct device *dev)
  230. {
  231. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  232. return mmc_gpio_get_cd(host->mmc);
  233. }
  234. #ifdef CONFIG_REGULATOR
  235. static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
  236. {
  237. struct omap_hsmmc_host *host =
  238. platform_get_drvdata(to_platform_device(dev));
  239. int ret = 0;
  240. /*
  241. * If we don't see a Vcc regulator, assume it's a fixed
  242. * voltage always-on regulator.
  243. */
  244. if (!host->vcc)
  245. return 0;
  246. if (mmc_pdata(host)->before_set_reg)
  247. mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
  248. if (host->pbias) {
  249. if (host->pbias_enabled == 1) {
  250. ret = regulator_disable(host->pbias);
  251. if (!ret)
  252. host->pbias_enabled = 0;
  253. }
  254. regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
  255. }
  256. /*
  257. * Assume Vcc regulator is used only to power the card ... OMAP
  258. * VDDS is used to power the pins, optionally with a transceiver to
  259. * support cards using voltages other than VDDS (1.8V nominal). When a
  260. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  261. *
  262. * In some cases this regulator won't support enable/disable;
  263. * e.g. it's a fixed rail for a WLAN chip.
  264. *
  265. * In other cases vcc_aux switches interface power. Example, for
  266. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  267. * chips/cards need an interface voltage rail too.
  268. */
  269. if (power_on) {
  270. if (host->vcc)
  271. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  272. /* Enable interface voltage rail, if needed */
  273. if (ret == 0 && host->vcc_aux) {
  274. ret = regulator_enable(host->vcc_aux);
  275. if (ret < 0 && host->vcc)
  276. ret = mmc_regulator_set_ocr(host->mmc,
  277. host->vcc, 0);
  278. }
  279. } else {
  280. /* Shut down the rail */
  281. if (host->vcc_aux)
  282. ret = regulator_disable(host->vcc_aux);
  283. if (host->vcc) {
  284. /* Then proceed to shut down the local regulator */
  285. ret = mmc_regulator_set_ocr(host->mmc,
  286. host->vcc, 0);
  287. }
  288. }
  289. if (host->pbias) {
  290. if (vdd <= VDD_165_195)
  291. ret = regulator_set_voltage(host->pbias, VDD_1V8,
  292. VDD_1V8);
  293. else
  294. ret = regulator_set_voltage(host->pbias, VDD_3V0,
  295. VDD_3V0);
  296. if (ret < 0)
  297. goto error_set_power;
  298. if (host->pbias_enabled == 0) {
  299. ret = regulator_enable(host->pbias);
  300. if (!ret)
  301. host->pbias_enabled = 1;
  302. }
  303. }
  304. if (mmc_pdata(host)->after_set_reg)
  305. mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
  306. error_set_power:
  307. return ret;
  308. }
  309. static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  310. {
  311. struct regulator *reg;
  312. int ocr_value = 0;
  313. reg = devm_regulator_get(host->dev, "vmmc");
  314. if (IS_ERR(reg)) {
  315. dev_err(host->dev, "unable to get vmmc regulator %ld\n",
  316. PTR_ERR(reg));
  317. return PTR_ERR(reg);
  318. } else {
  319. host->vcc = reg;
  320. ocr_value = mmc_regulator_get_ocrmask(reg);
  321. if (!mmc_pdata(host)->ocr_mask) {
  322. mmc_pdata(host)->ocr_mask = ocr_value;
  323. } else {
  324. if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
  325. dev_err(host->dev, "ocrmask %x is not supported\n",
  326. mmc_pdata(host)->ocr_mask);
  327. mmc_pdata(host)->ocr_mask = 0;
  328. return -EINVAL;
  329. }
  330. }
  331. }
  332. mmc_pdata(host)->set_power = omap_hsmmc_set_power;
  333. /* Allow an aux regulator */
  334. reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
  335. host->vcc_aux = IS_ERR(reg) ? NULL : reg;
  336. reg = devm_regulator_get_optional(host->dev, "pbias");
  337. host->pbias = IS_ERR(reg) ? NULL : reg;
  338. /* For eMMC do not power off when not in sleep state */
  339. if (mmc_pdata(host)->no_regulator_off_init)
  340. return 0;
  341. /*
  342. * To disable boot_on regulator, enable regulator
  343. * to increase usecount and then disable it.
  344. */
  345. if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
  346. (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
  347. int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
  348. mmc_pdata(host)->set_power(host->dev, 1, vdd);
  349. mmc_pdata(host)->set_power(host->dev, 0, 0);
  350. }
  351. return 0;
  352. }
  353. static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
  354. {
  355. mmc_pdata(host)->set_power = NULL;
  356. }
  357. static inline int omap_hsmmc_have_reg(void)
  358. {
  359. return 1;
  360. }
  361. #else
  362. static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  363. {
  364. return -EINVAL;
  365. }
  366. static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
  367. {
  368. }
  369. static inline int omap_hsmmc_have_reg(void)
  370. {
  371. return 0;
  372. }
  373. #endif
  374. static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id);
  375. static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
  376. static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
  377. struct omap_hsmmc_host *host,
  378. struct omap_hsmmc_platform_data *pdata)
  379. {
  380. int ret;
  381. if (pdata->cover && gpio_is_valid(pdata->switch_pin)) {
  382. ret = mmc_gpio_request_cd(mmc, pdata->switch_pin, 0);
  383. if (ret)
  384. return ret;
  385. host->get_cover_state = omap_hsmmc_get_cover_state;
  386. mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
  387. } else if (!pdata->cover && gpio_is_valid(pdata->switch_pin)) {
  388. ret = mmc_gpio_request_cd(mmc, pdata->switch_pin, 0);
  389. if (ret)
  390. return ret;
  391. host->card_detect = omap_hsmmc_card_detect;
  392. mmc_gpio_set_cd_isr(mmc, omap_hsmmc_detect);
  393. }
  394. if (gpio_is_valid(pdata->gpio_wp)) {
  395. ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
  396. if (ret)
  397. return ret;
  398. }
  399. return 0;
  400. }
  401. /*
  402. * Start clock to the card
  403. */
  404. static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
  405. {
  406. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  407. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  408. }
  409. /*
  410. * Stop clock to the card
  411. */
  412. static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
  413. {
  414. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  415. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  416. if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
  417. dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
  418. }
  419. static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
  420. struct mmc_command *cmd)
  421. {
  422. u32 irq_mask = INT_EN_MASK;
  423. unsigned long flags;
  424. if (host->use_dma)
  425. irq_mask &= ~(BRR_EN | BWR_EN);
  426. /* Disable timeout for erases */
  427. if (cmd->opcode == MMC_ERASE)
  428. irq_mask &= ~DTO_EN;
  429. spin_lock_irqsave(&host->irq_lock, flags);
  430. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  431. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  432. /* latch pending CIRQ, but don't signal MMC core */
  433. if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
  434. irq_mask |= CIRQ_EN;
  435. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  436. spin_unlock_irqrestore(&host->irq_lock, flags);
  437. }
  438. static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
  439. {
  440. u32 irq_mask = 0;
  441. unsigned long flags;
  442. spin_lock_irqsave(&host->irq_lock, flags);
  443. /* no transfer running but need to keep cirq if enabled */
  444. if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
  445. irq_mask |= CIRQ_EN;
  446. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  447. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  448. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  449. spin_unlock_irqrestore(&host->irq_lock, flags);
  450. }
  451. /* Calculate divisor for the given clock frequency */
  452. static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
  453. {
  454. u16 dsor = 0;
  455. if (ios->clock) {
  456. dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
  457. if (dsor > CLKD_MAX)
  458. dsor = CLKD_MAX;
  459. }
  460. return dsor;
  461. }
  462. static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
  463. {
  464. struct mmc_ios *ios = &host->mmc->ios;
  465. unsigned long regval;
  466. unsigned long timeout;
  467. unsigned long clkdiv;
  468. dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
  469. omap_hsmmc_stop_clock(host);
  470. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  471. regval = regval & ~(CLKD_MASK | DTO_MASK);
  472. clkdiv = calc_divisor(host, ios);
  473. regval = regval | (clkdiv << 6) | (DTO << 16);
  474. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  475. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  476. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  477. /* Wait till the ICS bit is set */
  478. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  479. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  480. && time_before(jiffies, timeout))
  481. cpu_relax();
  482. /*
  483. * Enable High-Speed Support
  484. * Pre-Requisites
  485. * - Controller should support High-Speed-Enable Bit
  486. * - Controller should not be using DDR Mode
  487. * - Controller should advertise that it supports High Speed
  488. * in capabilities register
  489. * - MMC/SD clock coming out of controller > 25MHz
  490. */
  491. if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
  492. (ios->timing != MMC_TIMING_MMC_DDR52) &&
  493. (ios->timing != MMC_TIMING_UHS_DDR50) &&
  494. ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
  495. regval = OMAP_HSMMC_READ(host->base, HCTL);
  496. if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
  497. regval |= HSPE;
  498. else
  499. regval &= ~HSPE;
  500. OMAP_HSMMC_WRITE(host->base, HCTL, regval);
  501. }
  502. omap_hsmmc_start_clock(host);
  503. }
  504. static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
  505. {
  506. struct mmc_ios *ios = &host->mmc->ios;
  507. u32 con;
  508. con = OMAP_HSMMC_READ(host->base, CON);
  509. if (ios->timing == MMC_TIMING_MMC_DDR52 ||
  510. ios->timing == MMC_TIMING_UHS_DDR50)
  511. con |= DDR; /* configure in DDR mode */
  512. else
  513. con &= ~DDR;
  514. switch (ios->bus_width) {
  515. case MMC_BUS_WIDTH_8:
  516. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  517. break;
  518. case MMC_BUS_WIDTH_4:
  519. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  520. OMAP_HSMMC_WRITE(host->base, HCTL,
  521. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  522. break;
  523. case MMC_BUS_WIDTH_1:
  524. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  525. OMAP_HSMMC_WRITE(host->base, HCTL,
  526. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  527. break;
  528. }
  529. }
  530. static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
  531. {
  532. struct mmc_ios *ios = &host->mmc->ios;
  533. u32 con;
  534. con = OMAP_HSMMC_READ(host->base, CON);
  535. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  536. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  537. else
  538. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  539. }
  540. #ifdef CONFIG_PM
  541. /*
  542. * Restore the MMC host context, if it was lost as result of a
  543. * power state change.
  544. */
  545. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  546. {
  547. struct mmc_ios *ios = &host->mmc->ios;
  548. u32 hctl, capa;
  549. unsigned long timeout;
  550. if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
  551. host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
  552. host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
  553. host->capa == OMAP_HSMMC_READ(host->base, CAPA))
  554. return 0;
  555. host->context_loss++;
  556. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  557. if (host->power_mode != MMC_POWER_OFF &&
  558. (1 << ios->vdd) <= MMC_VDD_23_24)
  559. hctl = SDVS18;
  560. else
  561. hctl = SDVS30;
  562. capa = VS30 | VS18;
  563. } else {
  564. hctl = SDVS18;
  565. capa = VS18;
  566. }
  567. if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
  568. hctl |= IWE;
  569. OMAP_HSMMC_WRITE(host->base, HCTL,
  570. OMAP_HSMMC_READ(host->base, HCTL) | hctl);
  571. OMAP_HSMMC_WRITE(host->base, CAPA,
  572. OMAP_HSMMC_READ(host->base, CAPA) | capa);
  573. OMAP_HSMMC_WRITE(host->base, HCTL,
  574. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  575. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  576. while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
  577. && time_before(jiffies, timeout))
  578. ;
  579. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  580. OMAP_HSMMC_WRITE(host->base, IE, 0);
  581. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  582. /* Do not initialize card-specific things if the power is off */
  583. if (host->power_mode == MMC_POWER_OFF)
  584. goto out;
  585. omap_hsmmc_set_bus_width(host);
  586. omap_hsmmc_set_clock(host);
  587. omap_hsmmc_set_bus_mode(host);
  588. out:
  589. dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
  590. host->context_loss);
  591. return 0;
  592. }
  593. /*
  594. * Save the MMC host context (store the number of power state changes so far).
  595. */
  596. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  597. {
  598. host->con = OMAP_HSMMC_READ(host->base, CON);
  599. host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
  600. host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
  601. host->capa = OMAP_HSMMC_READ(host->base, CAPA);
  602. }
  603. #else
  604. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  605. {
  606. return 0;
  607. }
  608. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  609. {
  610. }
  611. #endif
  612. /*
  613. * Send init stream sequence to card
  614. * before sending IDLE command
  615. */
  616. static void send_init_stream(struct omap_hsmmc_host *host)
  617. {
  618. int reg = 0;
  619. unsigned long timeout;
  620. if (host->protect_card)
  621. return;
  622. disable_irq(host->irq);
  623. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  624. OMAP_HSMMC_WRITE(host->base, CON,
  625. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  626. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  627. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  628. while ((reg != CC_EN) && time_before(jiffies, timeout))
  629. reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
  630. OMAP_HSMMC_WRITE(host->base, CON,
  631. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  632. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  633. OMAP_HSMMC_READ(host->base, STAT);
  634. enable_irq(host->irq);
  635. }
  636. static inline
  637. int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
  638. {
  639. int r = 1;
  640. if (host->get_cover_state)
  641. r = host->get_cover_state(host->dev);
  642. return r;
  643. }
  644. static ssize_t
  645. omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
  646. char *buf)
  647. {
  648. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  649. struct omap_hsmmc_host *host = mmc_priv(mmc);
  650. return sprintf(buf, "%s\n",
  651. omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
  652. }
  653. static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
  654. static ssize_t
  655. omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
  656. char *buf)
  657. {
  658. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  659. struct omap_hsmmc_host *host = mmc_priv(mmc);
  660. return sprintf(buf, "%s\n", mmc_pdata(host)->name);
  661. }
  662. static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
  663. /*
  664. * Configure the response type and send the cmd.
  665. */
  666. static void
  667. omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
  668. struct mmc_data *data)
  669. {
  670. int cmdreg = 0, resptype = 0, cmdtype = 0;
  671. dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  672. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  673. host->cmd = cmd;
  674. omap_hsmmc_enable_irq(host, cmd);
  675. host->response_busy = 0;
  676. if (cmd->flags & MMC_RSP_PRESENT) {
  677. if (cmd->flags & MMC_RSP_136)
  678. resptype = 1;
  679. else if (cmd->flags & MMC_RSP_BUSY) {
  680. resptype = 3;
  681. host->response_busy = 1;
  682. } else
  683. resptype = 2;
  684. }
  685. /*
  686. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  687. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  688. * a val of 0x3, rest 0x0.
  689. */
  690. if (cmd == host->mrq->stop)
  691. cmdtype = 0x3;
  692. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  693. if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
  694. host->mrq->sbc) {
  695. cmdreg |= ACEN_ACMD23;
  696. OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
  697. }
  698. if (data) {
  699. cmdreg |= DP_SELECT | MSBS | BCE;
  700. if (data->flags & MMC_DATA_READ)
  701. cmdreg |= DDIR;
  702. else
  703. cmdreg &= ~(DDIR);
  704. }
  705. if (host->use_dma)
  706. cmdreg |= DMAE;
  707. host->req_in_progress = 1;
  708. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  709. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  710. }
  711. static int
  712. omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
  713. {
  714. if (data->flags & MMC_DATA_WRITE)
  715. return DMA_TO_DEVICE;
  716. else
  717. return DMA_FROM_DEVICE;
  718. }
  719. static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
  720. struct mmc_data *data)
  721. {
  722. return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
  723. }
  724. static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
  725. {
  726. int dma_ch;
  727. unsigned long flags;
  728. spin_lock_irqsave(&host->irq_lock, flags);
  729. host->req_in_progress = 0;
  730. dma_ch = host->dma_ch;
  731. spin_unlock_irqrestore(&host->irq_lock, flags);
  732. omap_hsmmc_disable_irq(host);
  733. /* Do not complete the request if DMA is still in progress */
  734. if (mrq->data && host->use_dma && dma_ch != -1)
  735. return;
  736. host->mrq = NULL;
  737. mmc_request_done(host->mmc, mrq);
  738. }
  739. /*
  740. * Notify the transfer complete to MMC core
  741. */
  742. static void
  743. omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
  744. {
  745. if (!data) {
  746. struct mmc_request *mrq = host->mrq;
  747. /* TC before CC from CMD6 - don't know why, but it happens */
  748. if (host->cmd && host->cmd->opcode == 6 &&
  749. host->response_busy) {
  750. host->response_busy = 0;
  751. return;
  752. }
  753. omap_hsmmc_request_done(host, mrq);
  754. return;
  755. }
  756. host->data = NULL;
  757. if (!data->error)
  758. data->bytes_xfered += data->blocks * (data->blksz);
  759. else
  760. data->bytes_xfered = 0;
  761. if (data->stop && (data->error || !host->mrq->sbc))
  762. omap_hsmmc_start_command(host, data->stop, NULL);
  763. else
  764. omap_hsmmc_request_done(host, data->mrq);
  765. }
  766. /*
  767. * Notify the core about command completion
  768. */
  769. static void
  770. omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
  771. {
  772. if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
  773. !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
  774. host->cmd = NULL;
  775. omap_hsmmc_start_dma_transfer(host);
  776. omap_hsmmc_start_command(host, host->mrq->cmd,
  777. host->mrq->data);
  778. return;
  779. }
  780. host->cmd = NULL;
  781. if (cmd->flags & MMC_RSP_PRESENT) {
  782. if (cmd->flags & MMC_RSP_136) {
  783. /* response type 2 */
  784. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  785. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  786. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  787. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  788. } else {
  789. /* response types 1, 1b, 3, 4, 5, 6 */
  790. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  791. }
  792. }
  793. if ((host->data == NULL && !host->response_busy) || cmd->error)
  794. omap_hsmmc_request_done(host, host->mrq);
  795. }
  796. /*
  797. * DMA clean up for command errors
  798. */
  799. static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
  800. {
  801. int dma_ch;
  802. unsigned long flags;
  803. host->data->error = errno;
  804. spin_lock_irqsave(&host->irq_lock, flags);
  805. dma_ch = host->dma_ch;
  806. host->dma_ch = -1;
  807. spin_unlock_irqrestore(&host->irq_lock, flags);
  808. if (host->use_dma && dma_ch != -1) {
  809. struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
  810. dmaengine_terminate_all(chan);
  811. dma_unmap_sg(chan->device->dev,
  812. host->data->sg, host->data->sg_len,
  813. omap_hsmmc_get_dma_dir(host, host->data));
  814. host->data->host_cookie = 0;
  815. }
  816. host->data = NULL;
  817. }
  818. /*
  819. * Readable error output
  820. */
  821. #ifdef CONFIG_MMC_DEBUG
  822. static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
  823. {
  824. /* --- means reserved bit without definition at documentation */
  825. static const char *omap_hsmmc_status_bits[] = {
  826. "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
  827. "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
  828. "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
  829. "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
  830. };
  831. char res[256];
  832. char *buf = res;
  833. int len, i;
  834. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  835. buf += len;
  836. for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
  837. if (status & (1 << i)) {
  838. len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
  839. buf += len;
  840. }
  841. dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
  842. }
  843. #else
  844. static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
  845. u32 status)
  846. {
  847. }
  848. #endif /* CONFIG_MMC_DEBUG */
  849. /*
  850. * MMC controller internal state machines reset
  851. *
  852. * Used to reset command or data internal state machines, using respectively
  853. * SRC or SRD bit of SYSCTL register
  854. * Can be called from interrupt context
  855. */
  856. static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
  857. unsigned long bit)
  858. {
  859. unsigned long i = 0;
  860. unsigned long limit = MMC_TIMEOUT_US;
  861. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  862. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  863. /*
  864. * OMAP4 ES2 and greater has an updated reset logic.
  865. * Monitor a 0->1 transition first
  866. */
  867. if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
  868. while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
  869. && (i++ < limit))
  870. udelay(1);
  871. }
  872. i = 0;
  873. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  874. (i++ < limit))
  875. udelay(1);
  876. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  877. dev_err(mmc_dev(host->mmc),
  878. "Timeout waiting on controller reset in %s\n",
  879. __func__);
  880. }
  881. static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
  882. int err, int end_cmd)
  883. {
  884. if (end_cmd) {
  885. omap_hsmmc_reset_controller_fsm(host, SRC);
  886. if (host->cmd)
  887. host->cmd->error = err;
  888. }
  889. if (host->data) {
  890. omap_hsmmc_reset_controller_fsm(host, SRD);
  891. omap_hsmmc_dma_cleanup(host, err);
  892. } else if (host->mrq && host->mrq->cmd)
  893. host->mrq->cmd->error = err;
  894. }
  895. static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
  896. {
  897. struct mmc_data *data;
  898. int end_cmd = 0, end_trans = 0;
  899. int error = 0;
  900. data = host->data;
  901. dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  902. if (status & ERR_EN) {
  903. omap_hsmmc_dbg_report_irq(host, status);
  904. if (status & (CTO_EN | CCRC_EN))
  905. end_cmd = 1;
  906. if (status & (CTO_EN | DTO_EN))
  907. hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
  908. else if (status & (CCRC_EN | DCRC_EN))
  909. hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
  910. if (status & ACE_EN) {
  911. u32 ac12;
  912. ac12 = OMAP_HSMMC_READ(host->base, AC12);
  913. if (!(ac12 & ACNE) && host->mrq->sbc) {
  914. end_cmd = 1;
  915. if (ac12 & ACTO)
  916. error = -ETIMEDOUT;
  917. else if (ac12 & (ACCE | ACEB | ACIE))
  918. error = -EILSEQ;
  919. host->mrq->sbc->error = error;
  920. hsmmc_command_incomplete(host, error, end_cmd);
  921. }
  922. dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
  923. }
  924. if (host->data || host->response_busy) {
  925. end_trans = !end_cmd;
  926. host->response_busy = 0;
  927. }
  928. }
  929. OMAP_HSMMC_WRITE(host->base, STAT, status);
  930. if (end_cmd || ((status & CC_EN) && host->cmd))
  931. omap_hsmmc_cmd_done(host, host->cmd);
  932. if ((end_trans || (status & TC_EN)) && 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. while (status & (INT_EN_MASK | CIRQ_EN)) {
  944. if (host->req_in_progress)
  945. omap_hsmmc_do_irq(host, status);
  946. if (status & CIRQ_EN)
  947. mmc_signal_sdio_irq(host->mmc);
  948. /* Flush posted write */
  949. status = OMAP_HSMMC_READ(host->base, STAT);
  950. }
  951. return IRQ_HANDLED;
  952. }
  953. static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
  954. {
  955. struct omap_hsmmc_host *host = dev_id;
  956. /* cirq is level triggered, disable to avoid infinite loop */
  957. spin_lock(&host->irq_lock);
  958. if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
  959. disable_irq_nosync(host->wake_irq);
  960. host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
  961. }
  962. spin_unlock(&host->irq_lock);
  963. pm_request_resume(host->dev); /* no use counter */
  964. return IRQ_HANDLED;
  965. }
  966. static void set_sd_bus_power(struct omap_hsmmc_host *host)
  967. {
  968. unsigned long i;
  969. OMAP_HSMMC_WRITE(host->base, HCTL,
  970. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  971. for (i = 0; i < loops_per_jiffy; i++) {
  972. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  973. break;
  974. cpu_relax();
  975. }
  976. }
  977. /*
  978. * Switch MMC interface voltage ... only relevant for MMC1.
  979. *
  980. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  981. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  982. * Some chips, like eMMC ones, use internal transceivers.
  983. */
  984. static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
  985. {
  986. u32 reg_val = 0;
  987. int ret;
  988. /* Disable the clocks */
  989. pm_runtime_put_sync(host->dev);
  990. if (host->dbclk)
  991. clk_disable_unprepare(host->dbclk);
  992. /* Turn the power off */
  993. ret = mmc_pdata(host)->set_power(host->dev, 0, 0);
  994. /* Turn the power ON with given VDD 1.8 or 3.0v */
  995. if (!ret)
  996. ret = mmc_pdata(host)->set_power(host->dev, 1, vdd);
  997. pm_runtime_get_sync(host->dev);
  998. if (host->dbclk)
  999. clk_prepare_enable(host->dbclk);
  1000. if (ret != 0)
  1001. goto err;
  1002. OMAP_HSMMC_WRITE(host->base, HCTL,
  1003. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  1004. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  1005. /*
  1006. * If a MMC dual voltage card is detected, the set_ios fn calls
  1007. * this fn with VDD bit set for 1.8V. Upon card removal from the
  1008. * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  1009. *
  1010. * Cope with a bit of slop in the range ... per data sheets:
  1011. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  1012. * but recommended values are 1.71V to 1.89V
  1013. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  1014. * but recommended values are 2.7V to 3.3V
  1015. *
  1016. * Board setup code shouldn't permit anything very out-of-range.
  1017. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  1018. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  1019. */
  1020. if ((1 << vdd) <= MMC_VDD_23_24)
  1021. reg_val |= SDVS18;
  1022. else
  1023. reg_val |= SDVS30;
  1024. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  1025. set_sd_bus_power(host);
  1026. return 0;
  1027. err:
  1028. dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  1029. return ret;
  1030. }
  1031. /* Protect the card while the cover is open */
  1032. static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
  1033. {
  1034. if (!host->get_cover_state)
  1035. return;
  1036. host->reqs_blocked = 0;
  1037. if (host->get_cover_state(host->dev)) {
  1038. if (host->protect_card) {
  1039. dev_info(host->dev, "%s: cover is closed, "
  1040. "card is now accessible\n",
  1041. mmc_hostname(host->mmc));
  1042. host->protect_card = 0;
  1043. }
  1044. } else {
  1045. if (!host->protect_card) {
  1046. dev_info(host->dev, "%s: cover is open, "
  1047. "card is now inaccessible\n",
  1048. mmc_hostname(host->mmc));
  1049. host->protect_card = 1;
  1050. }
  1051. }
  1052. }
  1053. /*
  1054. * irq handler when (cell-phone) cover is mounted/removed
  1055. */
  1056. static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
  1057. {
  1058. struct omap_hsmmc_host *host = dev_id;
  1059. int carddetect;
  1060. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  1061. if (host->card_detect) {
  1062. carddetect = host->card_detect(host->dev);
  1063. } else {
  1064. omap_hsmmc_protect_card(host);
  1065. carddetect = -ENOSYS;
  1066. }
  1067. if (carddetect)
  1068. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  1069. else
  1070. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  1071. return IRQ_HANDLED;
  1072. }
  1073. /*
  1074. * irq handler to notify the core about card insertion/removal
  1075. */
  1076. static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
  1077. {
  1078. struct omap_hsmmc_host *host = dev_id;
  1079. int carddetect;
  1080. if (host->card_detect)
  1081. carddetect = host->card_detect(host->dev);
  1082. else {
  1083. omap_hsmmc_protect_card(host);
  1084. carddetect = -ENOSYS;
  1085. }
  1086. if (carddetect)
  1087. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  1088. else
  1089. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  1090. return IRQ_HANDLED;
  1091. }
  1092. static void omap_hsmmc_dma_callback(void *param)
  1093. {
  1094. struct omap_hsmmc_host *host = param;
  1095. struct dma_chan *chan;
  1096. struct mmc_data *data;
  1097. int req_in_progress;
  1098. spin_lock_irq(&host->irq_lock);
  1099. if (host->dma_ch < 0) {
  1100. spin_unlock_irq(&host->irq_lock);
  1101. return;
  1102. }
  1103. data = host->mrq->data;
  1104. chan = omap_hsmmc_get_dma_chan(host, data);
  1105. if (!data->host_cookie)
  1106. dma_unmap_sg(chan->device->dev,
  1107. data->sg, data->sg_len,
  1108. omap_hsmmc_get_dma_dir(host, data));
  1109. req_in_progress = host->req_in_progress;
  1110. host->dma_ch = -1;
  1111. spin_unlock_irq(&host->irq_lock);
  1112. /* If DMA has finished after TC, complete the request */
  1113. if (!req_in_progress) {
  1114. struct mmc_request *mrq = host->mrq;
  1115. host->mrq = NULL;
  1116. mmc_request_done(host->mmc, mrq);
  1117. }
  1118. }
  1119. static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
  1120. struct mmc_data *data,
  1121. struct omap_hsmmc_next *next,
  1122. struct dma_chan *chan)
  1123. {
  1124. int dma_len;
  1125. if (!next && data->host_cookie &&
  1126. data->host_cookie != host->next_data.cookie) {
  1127. dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
  1128. " host->next_data.cookie %d\n",
  1129. __func__, data->host_cookie, host->next_data.cookie);
  1130. data->host_cookie = 0;
  1131. }
  1132. /* Check if next job is already prepared */
  1133. if (next || data->host_cookie != host->next_data.cookie) {
  1134. dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
  1135. omap_hsmmc_get_dma_dir(host, data));
  1136. } else {
  1137. dma_len = host->next_data.dma_len;
  1138. host->next_data.dma_len = 0;
  1139. }
  1140. if (dma_len == 0)
  1141. return -EINVAL;
  1142. if (next) {
  1143. next->dma_len = dma_len;
  1144. data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
  1145. } else
  1146. host->dma_len = dma_len;
  1147. return 0;
  1148. }
  1149. /*
  1150. * Routine to configure and start DMA for the MMC card
  1151. */
  1152. static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
  1153. struct mmc_request *req)
  1154. {
  1155. struct dma_slave_config cfg;
  1156. struct dma_async_tx_descriptor *tx;
  1157. int ret = 0, i;
  1158. struct mmc_data *data = req->data;
  1159. struct dma_chan *chan;
  1160. /* Sanity check: all the SG entries must be aligned by block size. */
  1161. for (i = 0; i < data->sg_len; i++) {
  1162. struct scatterlist *sgl;
  1163. sgl = data->sg + i;
  1164. if (sgl->length % data->blksz)
  1165. return -EINVAL;
  1166. }
  1167. if ((data->blksz % 4) != 0)
  1168. /* REVISIT: The MMC buffer increments only when MSB is written.
  1169. * Return error for blksz which is non multiple of four.
  1170. */
  1171. return -EINVAL;
  1172. BUG_ON(host->dma_ch != -1);
  1173. chan = omap_hsmmc_get_dma_chan(host, data);
  1174. cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
  1175. cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
  1176. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1177. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1178. cfg.src_maxburst = data->blksz / 4;
  1179. cfg.dst_maxburst = data->blksz / 4;
  1180. ret = dmaengine_slave_config(chan, &cfg);
  1181. if (ret)
  1182. return ret;
  1183. ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
  1184. if (ret)
  1185. return ret;
  1186. tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
  1187. data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  1188. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1189. if (!tx) {
  1190. dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
  1191. /* FIXME: cleanup */
  1192. return -1;
  1193. }
  1194. tx->callback = omap_hsmmc_dma_callback;
  1195. tx->callback_param = host;
  1196. /* Does not fail */
  1197. dmaengine_submit(tx);
  1198. host->dma_ch = 1;
  1199. return 0;
  1200. }
  1201. static void set_data_timeout(struct omap_hsmmc_host *host,
  1202. unsigned int timeout_ns,
  1203. unsigned int timeout_clks)
  1204. {
  1205. unsigned int timeout, cycle_ns;
  1206. uint32_t reg, clkd, dto = 0;
  1207. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  1208. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  1209. if (clkd == 0)
  1210. clkd = 1;
  1211. cycle_ns = 1000000000 / (host->clk_rate / clkd);
  1212. timeout = timeout_ns / cycle_ns;
  1213. timeout += timeout_clks;
  1214. if (timeout) {
  1215. while ((timeout & 0x80000000) == 0) {
  1216. dto += 1;
  1217. timeout <<= 1;
  1218. }
  1219. dto = 31 - dto;
  1220. timeout <<= 1;
  1221. if (timeout && dto)
  1222. dto += 1;
  1223. if (dto >= 13)
  1224. dto -= 13;
  1225. else
  1226. dto = 0;
  1227. if (dto > 14)
  1228. dto = 14;
  1229. }
  1230. reg &= ~DTO_MASK;
  1231. reg |= dto << DTO_SHIFT;
  1232. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  1233. }
  1234. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
  1235. {
  1236. struct mmc_request *req = host->mrq;
  1237. struct dma_chan *chan;
  1238. if (!req->data)
  1239. return;
  1240. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  1241. | (req->data->blocks << 16));
  1242. set_data_timeout(host, req->data->timeout_ns,
  1243. req->data->timeout_clks);
  1244. chan = omap_hsmmc_get_dma_chan(host, req->data);
  1245. dma_async_issue_pending(chan);
  1246. }
  1247. /*
  1248. * Configure block length for MMC/SD cards and initiate the transfer.
  1249. */
  1250. static int
  1251. omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
  1252. {
  1253. int ret;
  1254. host->data = req->data;
  1255. if (req->data == NULL) {
  1256. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  1257. /*
  1258. * Set an arbitrary 100ms data timeout for commands with
  1259. * busy signal.
  1260. */
  1261. if (req->cmd->flags & MMC_RSP_BUSY)
  1262. set_data_timeout(host, 100000000U, 0);
  1263. return 0;
  1264. }
  1265. if (host->use_dma) {
  1266. ret = omap_hsmmc_setup_dma_transfer(host, req);
  1267. if (ret != 0) {
  1268. dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
  1269. return ret;
  1270. }
  1271. }
  1272. return 0;
  1273. }
  1274. static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1275. int err)
  1276. {
  1277. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1278. struct mmc_data *data = mrq->data;
  1279. if (host->use_dma && data->host_cookie) {
  1280. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
  1281. dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
  1282. omap_hsmmc_get_dma_dir(host, data));
  1283. data->host_cookie = 0;
  1284. }
  1285. }
  1286. static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1287. bool is_first_req)
  1288. {
  1289. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1290. if (mrq->data->host_cookie) {
  1291. mrq->data->host_cookie = 0;
  1292. return ;
  1293. }
  1294. if (host->use_dma) {
  1295. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
  1296. if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
  1297. &host->next_data, c))
  1298. mrq->data->host_cookie = 0;
  1299. }
  1300. }
  1301. /*
  1302. * Request function. for read/write operation
  1303. */
  1304. static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1305. {
  1306. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1307. int err;
  1308. BUG_ON(host->req_in_progress);
  1309. BUG_ON(host->dma_ch != -1);
  1310. if (host->protect_card) {
  1311. if (host->reqs_blocked < 3) {
  1312. /*
  1313. * Ensure the controller is left in a consistent
  1314. * state by resetting the command and data state
  1315. * machines.
  1316. */
  1317. omap_hsmmc_reset_controller_fsm(host, SRD);
  1318. omap_hsmmc_reset_controller_fsm(host, SRC);
  1319. host->reqs_blocked += 1;
  1320. }
  1321. req->cmd->error = -EBADF;
  1322. if (req->data)
  1323. req->data->error = -EBADF;
  1324. req->cmd->retries = 0;
  1325. mmc_request_done(mmc, req);
  1326. return;
  1327. } else if (host->reqs_blocked)
  1328. host->reqs_blocked = 0;
  1329. WARN_ON(host->mrq != NULL);
  1330. host->mrq = req;
  1331. host->clk_rate = clk_get_rate(host->fclk);
  1332. err = omap_hsmmc_prepare_data(host, req);
  1333. if (err) {
  1334. req->cmd->error = err;
  1335. if (req->data)
  1336. req->data->error = err;
  1337. host->mrq = NULL;
  1338. mmc_request_done(mmc, req);
  1339. return;
  1340. }
  1341. if (req->sbc && !(host->flags & AUTO_CMD23)) {
  1342. omap_hsmmc_start_command(host, req->sbc, NULL);
  1343. return;
  1344. }
  1345. omap_hsmmc_start_dma_transfer(host);
  1346. omap_hsmmc_start_command(host, req->cmd, req->data);
  1347. }
  1348. /* Routine to configure clock values. Exposed API to core */
  1349. static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1350. {
  1351. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1352. int do_send_init_stream = 0;
  1353. pm_runtime_get_sync(host->dev);
  1354. if (ios->power_mode != host->power_mode) {
  1355. switch (ios->power_mode) {
  1356. case MMC_POWER_OFF:
  1357. mmc_pdata(host)->set_power(host->dev, 0, 0);
  1358. break;
  1359. case MMC_POWER_UP:
  1360. mmc_pdata(host)->set_power(host->dev, 1, ios->vdd);
  1361. break;
  1362. case MMC_POWER_ON:
  1363. do_send_init_stream = 1;
  1364. break;
  1365. }
  1366. host->power_mode = ios->power_mode;
  1367. }
  1368. /* FIXME: set registers based only on changes to ios */
  1369. omap_hsmmc_set_bus_width(host);
  1370. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1371. /* Only MMC1 can interface at 3V without some flavor
  1372. * of external transceiver; but they all handle 1.8V.
  1373. */
  1374. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  1375. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  1376. /*
  1377. * The mmc_select_voltage fn of the core does
  1378. * not seem to set the power_mode to
  1379. * MMC_POWER_UP upon recalculating the voltage.
  1380. * vdd 1.8v.
  1381. */
  1382. if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
  1383. dev_dbg(mmc_dev(host->mmc),
  1384. "Switch operation failed\n");
  1385. }
  1386. }
  1387. omap_hsmmc_set_clock(host);
  1388. if (do_send_init_stream)
  1389. send_init_stream(host);
  1390. omap_hsmmc_set_bus_mode(host);
  1391. pm_runtime_put_autosuspend(host->dev);
  1392. }
  1393. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  1394. {
  1395. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1396. if (!host->card_detect)
  1397. return -ENOSYS;
  1398. return host->card_detect(host->dev);
  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_pdata(host)->init_card)
  1404. mmc_pdata(host)->init_card(card);
  1405. }
  1406. static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1407. {
  1408. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1409. u32 irq_mask, con;
  1410. unsigned long flags;
  1411. spin_lock_irqsave(&host->irq_lock, flags);
  1412. con = OMAP_HSMMC_READ(host->base, CON);
  1413. irq_mask = OMAP_HSMMC_READ(host->base, ISE);
  1414. if (enable) {
  1415. host->flags |= HSMMC_SDIO_IRQ_ENABLED;
  1416. irq_mask |= CIRQ_EN;
  1417. con |= CTPL | CLKEXTFREE;
  1418. } else {
  1419. host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
  1420. irq_mask &= ~CIRQ_EN;
  1421. con &= ~(CTPL | CLKEXTFREE);
  1422. }
  1423. OMAP_HSMMC_WRITE(host->base, CON, con);
  1424. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  1425. /*
  1426. * if enable, piggy back detection on current request
  1427. * but always disable immediately
  1428. */
  1429. if (!host->req_in_progress || !enable)
  1430. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  1431. /* flush posted write */
  1432. OMAP_HSMMC_READ(host->base, IE);
  1433. spin_unlock_irqrestore(&host->irq_lock, flags);
  1434. }
  1435. static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
  1436. {
  1437. struct mmc_host *mmc = host->mmc;
  1438. int ret;
  1439. /*
  1440. * For omaps with wake-up path, wakeirq will be irq from pinctrl and
  1441. * for other omaps, wakeirq will be from GPIO (dat line remuxed to
  1442. * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
  1443. * with functional clock disabled.
  1444. */
  1445. if (!host->dev->of_node || !host->wake_irq)
  1446. return -ENODEV;
  1447. /* Prevent auto-enabling of IRQ */
  1448. irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
  1449. ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
  1450. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  1451. mmc_hostname(mmc), host);
  1452. if (ret) {
  1453. dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
  1454. goto err;
  1455. }
  1456. /*
  1457. * Some omaps don't have wake-up path from deeper idle states
  1458. * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
  1459. */
  1460. if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
  1461. struct pinctrl *p = devm_pinctrl_get(host->dev);
  1462. if (!p) {
  1463. ret = -ENODEV;
  1464. goto err_free_irq;
  1465. }
  1466. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
  1467. dev_info(host->dev, "missing default pinctrl state\n");
  1468. devm_pinctrl_put(p);
  1469. ret = -EINVAL;
  1470. goto err_free_irq;
  1471. }
  1472. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
  1473. dev_info(host->dev, "missing idle pinctrl state\n");
  1474. devm_pinctrl_put(p);
  1475. ret = -EINVAL;
  1476. goto err_free_irq;
  1477. }
  1478. devm_pinctrl_put(p);
  1479. }
  1480. OMAP_HSMMC_WRITE(host->base, HCTL,
  1481. OMAP_HSMMC_READ(host->base, HCTL) | IWE);
  1482. return 0;
  1483. err_free_irq:
  1484. devm_free_irq(host->dev, host->wake_irq, host);
  1485. err:
  1486. dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
  1487. host->wake_irq = 0;
  1488. return ret;
  1489. }
  1490. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
  1491. {
  1492. u32 hctl, capa, value;
  1493. /* Only MMC1 supports 3.0V */
  1494. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1495. hctl = SDVS30;
  1496. capa = VS30 | VS18;
  1497. } else {
  1498. hctl = SDVS18;
  1499. capa = VS18;
  1500. }
  1501. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1502. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1503. value = OMAP_HSMMC_READ(host->base, CAPA);
  1504. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1505. /* Set SD bus power bit */
  1506. set_sd_bus_power(host);
  1507. }
  1508. static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
  1509. {
  1510. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1511. pm_runtime_get_sync(host->dev);
  1512. return 0;
  1513. }
  1514. static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
  1515. {
  1516. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1517. pm_runtime_mark_last_busy(host->dev);
  1518. pm_runtime_put_autosuspend(host->dev);
  1519. return 0;
  1520. }
  1521. static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
  1522. unsigned int direction, int blk_size)
  1523. {
  1524. /* This controller can't do multiblock reads due to hw bugs */
  1525. if (direction == MMC_DATA_READ)
  1526. return 1;
  1527. return blk_size;
  1528. }
  1529. static struct mmc_host_ops omap_hsmmc_ops = {
  1530. .enable = omap_hsmmc_enable_fclk,
  1531. .disable = omap_hsmmc_disable_fclk,
  1532. .post_req = omap_hsmmc_post_req,
  1533. .pre_req = omap_hsmmc_pre_req,
  1534. .request = omap_hsmmc_request,
  1535. .set_ios = omap_hsmmc_set_ios,
  1536. .get_cd = omap_hsmmc_get_cd,
  1537. .get_ro = mmc_gpio_get_ro,
  1538. .init_card = omap_hsmmc_init_card,
  1539. .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
  1540. };
  1541. #ifdef CONFIG_DEBUG_FS
  1542. static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
  1543. {
  1544. struct mmc_host *mmc = s->private;
  1545. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1546. seq_printf(s, "mmc%d:\n", mmc->index);
  1547. seq_printf(s, "sdio irq mode\t%s\n",
  1548. (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
  1549. if (mmc->caps & MMC_CAP_SDIO_IRQ) {
  1550. seq_printf(s, "sdio irq \t%s\n",
  1551. (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
  1552. : "disabled");
  1553. }
  1554. seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
  1555. pm_runtime_get_sync(host->dev);
  1556. seq_puts(s, "\nregs:\n");
  1557. seq_printf(s, "CON:\t\t0x%08x\n",
  1558. OMAP_HSMMC_READ(host->base, CON));
  1559. seq_printf(s, "PSTATE:\t\t0x%08x\n",
  1560. OMAP_HSMMC_READ(host->base, PSTATE));
  1561. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1562. OMAP_HSMMC_READ(host->base, HCTL));
  1563. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1564. OMAP_HSMMC_READ(host->base, SYSCTL));
  1565. seq_printf(s, "IE:\t\t0x%08x\n",
  1566. OMAP_HSMMC_READ(host->base, IE));
  1567. seq_printf(s, "ISE:\t\t0x%08x\n",
  1568. OMAP_HSMMC_READ(host->base, ISE));
  1569. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1570. OMAP_HSMMC_READ(host->base, CAPA));
  1571. pm_runtime_mark_last_busy(host->dev);
  1572. pm_runtime_put_autosuspend(host->dev);
  1573. return 0;
  1574. }
  1575. static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
  1576. {
  1577. return single_open(file, omap_hsmmc_regs_show, inode->i_private);
  1578. }
  1579. static const struct file_operations mmc_regs_fops = {
  1580. .open = omap_hsmmc_regs_open,
  1581. .read = seq_read,
  1582. .llseek = seq_lseek,
  1583. .release = single_release,
  1584. };
  1585. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1586. {
  1587. if (mmc->debugfs_root)
  1588. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1589. mmc, &mmc_regs_fops);
  1590. }
  1591. #else
  1592. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1593. {
  1594. }
  1595. #endif
  1596. #ifdef CONFIG_OF
  1597. static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
  1598. /* See 35xx errata 2.1.1.128 in SPRZ278F */
  1599. .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
  1600. };
  1601. static const struct omap_mmc_of_data omap4_mmc_of_data = {
  1602. .reg_offset = 0x100,
  1603. };
  1604. static const struct omap_mmc_of_data am33xx_mmc_of_data = {
  1605. .reg_offset = 0x100,
  1606. .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
  1607. };
  1608. static const struct of_device_id omap_mmc_of_match[] = {
  1609. {
  1610. .compatible = "ti,omap2-hsmmc",
  1611. },
  1612. {
  1613. .compatible = "ti,omap3-pre-es3-hsmmc",
  1614. .data = &omap3_pre_es3_mmc_of_data,
  1615. },
  1616. {
  1617. .compatible = "ti,omap3-hsmmc",
  1618. },
  1619. {
  1620. .compatible = "ti,omap4-hsmmc",
  1621. .data = &omap4_mmc_of_data,
  1622. },
  1623. {
  1624. .compatible = "ti,am33xx-hsmmc",
  1625. .data = &am33xx_mmc_of_data,
  1626. },
  1627. {},
  1628. };
  1629. MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
  1630. static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
  1631. {
  1632. struct omap_hsmmc_platform_data *pdata;
  1633. struct device_node *np = dev->of_node;
  1634. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1635. if (!pdata)
  1636. return ERR_PTR(-ENOMEM); /* out of memory */
  1637. if (of_find_property(np, "ti,dual-volt", NULL))
  1638. pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
  1639. pdata->switch_pin = -EINVAL;
  1640. pdata->gpio_wp = -EINVAL;
  1641. if (of_find_property(np, "ti,non-removable", NULL)) {
  1642. pdata->nonremovable = true;
  1643. pdata->no_regulator_off_init = true;
  1644. }
  1645. if (of_find_property(np, "ti,needs-special-reset", NULL))
  1646. pdata->features |= HSMMC_HAS_UPDATED_RESET;
  1647. if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
  1648. pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
  1649. return pdata;
  1650. }
  1651. #else
  1652. static inline struct omap_hsmmc_platform_data
  1653. *of_get_hsmmc_pdata(struct device *dev)
  1654. {
  1655. return ERR_PTR(-EINVAL);
  1656. }
  1657. #endif
  1658. static int omap_hsmmc_probe(struct platform_device *pdev)
  1659. {
  1660. struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
  1661. struct mmc_host *mmc;
  1662. struct omap_hsmmc_host *host = NULL;
  1663. struct resource *res;
  1664. int ret, irq;
  1665. const struct of_device_id *match;
  1666. dma_cap_mask_t mask;
  1667. unsigned tx_req, rx_req;
  1668. const struct omap_mmc_of_data *data;
  1669. void __iomem *base;
  1670. match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
  1671. if (match) {
  1672. pdata = of_get_hsmmc_pdata(&pdev->dev);
  1673. if (IS_ERR(pdata))
  1674. return PTR_ERR(pdata);
  1675. if (match->data) {
  1676. data = match->data;
  1677. pdata->reg_offset = data->reg_offset;
  1678. pdata->controller_flags |= data->controller_flags;
  1679. }
  1680. }
  1681. if (pdata == NULL) {
  1682. dev_err(&pdev->dev, "Platform Data is missing\n");
  1683. return -ENXIO;
  1684. }
  1685. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1686. irq = platform_get_irq(pdev, 0);
  1687. if (res == NULL || irq < 0)
  1688. return -ENXIO;
  1689. base = devm_ioremap_resource(&pdev->dev, res);
  1690. if (IS_ERR(base))
  1691. return PTR_ERR(base);
  1692. mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
  1693. if (!mmc) {
  1694. ret = -ENOMEM;
  1695. goto err;
  1696. }
  1697. ret = mmc_of_parse(mmc);
  1698. if (ret)
  1699. goto err1;
  1700. host = mmc_priv(mmc);
  1701. host->mmc = mmc;
  1702. host->pdata = pdata;
  1703. host->dev = &pdev->dev;
  1704. host->use_dma = 1;
  1705. host->dma_ch = -1;
  1706. host->irq = irq;
  1707. host->mapbase = res->start + pdata->reg_offset;
  1708. host->base = base + pdata->reg_offset;
  1709. host->power_mode = MMC_POWER_OFF;
  1710. host->next_data.cookie = 1;
  1711. host->pbias_enabled = 0;
  1712. ret = omap_hsmmc_gpio_init(mmc, host, pdata);
  1713. if (ret)
  1714. goto err_gpio;
  1715. platform_set_drvdata(pdev, host);
  1716. if (pdev->dev.of_node)
  1717. host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
  1718. mmc->ops = &omap_hsmmc_ops;
  1719. mmc->f_min = OMAP_MMC_MIN_CLOCK;
  1720. if (pdata->max_freq > 0)
  1721. mmc->f_max = pdata->max_freq;
  1722. else if (mmc->f_max == 0)
  1723. mmc->f_max = OMAP_MMC_MAX_CLOCK;
  1724. spin_lock_init(&host->irq_lock);
  1725. host->fclk = devm_clk_get(&pdev->dev, "fck");
  1726. if (IS_ERR(host->fclk)) {
  1727. ret = PTR_ERR(host->fclk);
  1728. host->fclk = NULL;
  1729. goto err1;
  1730. }
  1731. if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
  1732. dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
  1733. omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
  1734. }
  1735. pm_runtime_enable(host->dev);
  1736. pm_runtime_get_sync(host->dev);
  1737. pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
  1738. pm_runtime_use_autosuspend(host->dev);
  1739. omap_hsmmc_context_save(host);
  1740. host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
  1741. /*
  1742. * MMC can still work without debounce clock.
  1743. */
  1744. if (IS_ERR(host->dbclk)) {
  1745. host->dbclk = NULL;
  1746. } else if (clk_prepare_enable(host->dbclk) != 0) {
  1747. dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
  1748. host->dbclk = NULL;
  1749. }
  1750. /* Since we do only SG emulation, we can have as many segs
  1751. * as we want. */
  1752. mmc->max_segs = 1024;
  1753. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1754. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1755. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1756. mmc->max_seg_size = mmc->max_req_size;
  1757. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1758. MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
  1759. mmc->caps |= mmc_pdata(host)->caps;
  1760. if (mmc->caps & MMC_CAP_8_BIT_DATA)
  1761. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1762. if (mmc_pdata(host)->nonremovable)
  1763. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1764. mmc->pm_caps |= mmc_pdata(host)->pm_caps;
  1765. omap_hsmmc_conf_bus_power(host);
  1766. if (!pdev->dev.of_node) {
  1767. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1768. if (!res) {
  1769. dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
  1770. ret = -ENXIO;
  1771. goto err_irq;
  1772. }
  1773. tx_req = res->start;
  1774. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1775. if (!res) {
  1776. dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
  1777. ret = -ENXIO;
  1778. goto err_irq;
  1779. }
  1780. rx_req = res->start;
  1781. }
  1782. dma_cap_zero(mask);
  1783. dma_cap_set(DMA_SLAVE, mask);
  1784. host->rx_chan =
  1785. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  1786. &rx_req, &pdev->dev, "rx");
  1787. if (!host->rx_chan) {
  1788. dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
  1789. ret = -ENXIO;
  1790. goto err_irq;
  1791. }
  1792. host->tx_chan =
  1793. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  1794. &tx_req, &pdev->dev, "tx");
  1795. if (!host->tx_chan) {
  1796. dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
  1797. ret = -ENXIO;
  1798. goto err_irq;
  1799. }
  1800. /* Request IRQ for MMC operations */
  1801. ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
  1802. mmc_hostname(mmc), host);
  1803. if (ret) {
  1804. dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1805. goto err_irq;
  1806. }
  1807. if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
  1808. ret = omap_hsmmc_reg_get(host);
  1809. if (ret)
  1810. goto err_irq;
  1811. host->use_reg = 1;
  1812. }
  1813. mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
  1814. omap_hsmmc_disable_irq(host);
  1815. /*
  1816. * For now, only support SDIO interrupt if we have a separate
  1817. * wake-up interrupt configured from device tree. This is because
  1818. * the wake-up interrupt is needed for idle state and some
  1819. * platforms need special quirks. And we don't want to add new
  1820. * legacy mux platform init code callbacks any longer as we
  1821. * are moving to DT based booting anyways.
  1822. */
  1823. ret = omap_hsmmc_configure_wake_irq(host);
  1824. if (!ret)
  1825. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1826. omap_hsmmc_protect_card(host);
  1827. mmc_add_host(mmc);
  1828. if (mmc_pdata(host)->name != NULL) {
  1829. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1830. if (ret < 0)
  1831. goto err_slot_name;
  1832. }
  1833. if (host->get_cover_state) {
  1834. ret = device_create_file(&mmc->class_dev,
  1835. &dev_attr_cover_switch);
  1836. if (ret < 0)
  1837. goto err_slot_name;
  1838. }
  1839. omap_hsmmc_debugfs(mmc);
  1840. pm_runtime_mark_last_busy(host->dev);
  1841. pm_runtime_put_autosuspend(host->dev);
  1842. return 0;
  1843. err_slot_name:
  1844. mmc_remove_host(mmc);
  1845. if (host->use_reg)
  1846. omap_hsmmc_reg_put(host);
  1847. err_irq:
  1848. if (host->tx_chan)
  1849. dma_release_channel(host->tx_chan);
  1850. if (host->rx_chan)
  1851. dma_release_channel(host->rx_chan);
  1852. pm_runtime_put_sync(host->dev);
  1853. pm_runtime_disable(host->dev);
  1854. if (host->dbclk)
  1855. clk_disable_unprepare(host->dbclk);
  1856. err1:
  1857. err_gpio:
  1858. mmc_free_host(mmc);
  1859. err:
  1860. return ret;
  1861. }
  1862. static int omap_hsmmc_remove(struct platform_device *pdev)
  1863. {
  1864. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1865. pm_runtime_get_sync(host->dev);
  1866. mmc_remove_host(host->mmc);
  1867. if (host->use_reg)
  1868. omap_hsmmc_reg_put(host);
  1869. if (host->tx_chan)
  1870. dma_release_channel(host->tx_chan);
  1871. if (host->rx_chan)
  1872. dma_release_channel(host->rx_chan);
  1873. pm_runtime_put_sync(host->dev);
  1874. pm_runtime_disable(host->dev);
  1875. if (host->dbclk)
  1876. clk_disable_unprepare(host->dbclk);
  1877. mmc_free_host(host->mmc);
  1878. return 0;
  1879. }
  1880. #ifdef CONFIG_PM
  1881. static int omap_hsmmc_suspend(struct device *dev)
  1882. {
  1883. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1884. if (!host)
  1885. return 0;
  1886. pm_runtime_get_sync(host->dev);
  1887. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
  1888. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1889. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1890. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1891. OMAP_HSMMC_WRITE(host->base, HCTL,
  1892. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1893. }
  1894. /* do not wake up due to sdio irq */
  1895. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1896. !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
  1897. disable_irq(host->wake_irq);
  1898. if (host->dbclk)
  1899. clk_disable_unprepare(host->dbclk);
  1900. pm_runtime_put_sync(host->dev);
  1901. return 0;
  1902. }
  1903. /* Routine to resume the MMC device */
  1904. static int omap_hsmmc_resume(struct device *dev)
  1905. {
  1906. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1907. if (!host)
  1908. return 0;
  1909. pm_runtime_get_sync(host->dev);
  1910. if (host->dbclk)
  1911. clk_prepare_enable(host->dbclk);
  1912. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
  1913. omap_hsmmc_conf_bus_power(host);
  1914. omap_hsmmc_protect_card(host);
  1915. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1916. !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
  1917. enable_irq(host->wake_irq);
  1918. pm_runtime_mark_last_busy(host->dev);
  1919. pm_runtime_put_autosuspend(host->dev);
  1920. return 0;
  1921. }
  1922. #else
  1923. #define omap_hsmmc_suspend NULL
  1924. #define omap_hsmmc_resume NULL
  1925. #endif
  1926. static int omap_hsmmc_runtime_suspend(struct device *dev)
  1927. {
  1928. struct omap_hsmmc_host *host;
  1929. unsigned long flags;
  1930. int ret = 0;
  1931. host = platform_get_drvdata(to_platform_device(dev));
  1932. omap_hsmmc_context_save(host);
  1933. dev_dbg(dev, "disabled\n");
  1934. spin_lock_irqsave(&host->irq_lock, flags);
  1935. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1936. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  1937. /* disable sdio irq handling to prevent race */
  1938. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1939. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1940. if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
  1941. /*
  1942. * dat1 line low, pending sdio irq
  1943. * race condition: possible irq handler running on
  1944. * multi-core, abort
  1945. */
  1946. dev_dbg(dev, "pending sdio irq, abort suspend\n");
  1947. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1948. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  1949. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  1950. pm_runtime_mark_last_busy(dev);
  1951. ret = -EBUSY;
  1952. goto abort;
  1953. }
  1954. pinctrl_pm_select_idle_state(dev);
  1955. WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
  1956. enable_irq(host->wake_irq);
  1957. host->flags |= HSMMC_WAKE_IRQ_ENABLED;
  1958. } else {
  1959. pinctrl_pm_select_idle_state(dev);
  1960. }
  1961. abort:
  1962. spin_unlock_irqrestore(&host->irq_lock, flags);
  1963. return ret;
  1964. }
  1965. static int omap_hsmmc_runtime_resume(struct device *dev)
  1966. {
  1967. struct omap_hsmmc_host *host;
  1968. unsigned long flags;
  1969. host = platform_get_drvdata(to_platform_device(dev));
  1970. omap_hsmmc_context_restore(host);
  1971. dev_dbg(dev, "enabled\n");
  1972. spin_lock_irqsave(&host->irq_lock, flags);
  1973. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1974. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  1975. /* sdio irq flag can't change while in runtime suspend */
  1976. if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
  1977. disable_irq_nosync(host->wake_irq);
  1978. host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
  1979. }
  1980. pinctrl_pm_select_default_state(host->dev);
  1981. /* irq lost, if pinmux incorrect */
  1982. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1983. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  1984. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  1985. } else {
  1986. pinctrl_pm_select_default_state(host->dev);
  1987. }
  1988. spin_unlock_irqrestore(&host->irq_lock, flags);
  1989. return 0;
  1990. }
  1991. static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
  1992. .suspend = omap_hsmmc_suspend,
  1993. .resume = omap_hsmmc_resume,
  1994. .runtime_suspend = omap_hsmmc_runtime_suspend,
  1995. .runtime_resume = omap_hsmmc_runtime_resume,
  1996. };
  1997. static struct platform_driver omap_hsmmc_driver = {
  1998. .probe = omap_hsmmc_probe,
  1999. .remove = omap_hsmmc_remove,
  2000. .driver = {
  2001. .name = DRIVER_NAME,
  2002. .pm = &omap_hsmmc_dev_pm_ops,
  2003. .of_match_table = of_match_ptr(omap_mmc_of_match),
  2004. },
  2005. };
  2006. module_platform_driver(omap_hsmmc_driver);
  2007. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  2008. MODULE_LICENSE("GPL");
  2009. MODULE_ALIAS("platform:" DRIVER_NAME);
  2010. MODULE_AUTHOR("Texas Instruments Inc");