omap_hsmmc.c 63 KB

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