omap_hsmmc.c 59 KB

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