omap_hsmmc.c 59 KB

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