omap_hsmmc.c 63 KB

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