omap_hsmmc.c 59 KB

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