omap_hsmmc.c 59 KB

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