omap_hsmmc.c 62 KB

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