omap_hsmmc.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269
  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/slab.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/sizes.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/timer.h>
  30. #include <linux/clk.h>
  31. #include <linux/of.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/of_device.h>
  35. #include <linux/mmc/host.h>
  36. #include <linux/mmc/core.h>
  37. #include <linux/mmc/mmc.h>
  38. #include <linux/mmc/slot-gpio.h>
  39. #include <linux/io.h>
  40. #include <linux/irq.h>
  41. #include <linux/gpio.h>
  42. #include <linux/regulator/consumer.h>
  43. #include <linux/pinctrl/consumer.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/pm_wakeirq.h>
  46. #include <linux/platform_data/hsmmc-omap.h>
  47. #include <linux/mmc/sd.h>
  48. #include <linux/thermal.h>
  49. /* OMAP HSMMC Host Controller Registers */
  50. #define OMAP_HSMMC_HL_REV 0x0000
  51. #define OMAP_HSMMC_HL_HWINFO 0x0004
  52. #define OMAP_HSMMC_HL_SYSCONFIG 0x0010
  53. #define OMAP_HSMMC_SYSSTATUS 0x0014
  54. #define OMAP_HSMMC_CON 0x002C
  55. #define OMAP_HSMMC_DLL 0x0034
  56. #define OMAP_HSMMC_SDMASA 0x0100
  57. #define OMAP_HSMMC_BLK 0x0104
  58. #define OMAP_HSMMC_ARG 0x0108
  59. #define OMAP_HSMMC_CMD 0x010C
  60. #define OMAP_HSMMC_RSP10 0x0110
  61. #define OMAP_HSMMC_RSP32 0x0114
  62. #define OMAP_HSMMC_RSP54 0x0118
  63. #define OMAP_HSMMC_RSP76 0x011C
  64. #define OMAP_HSMMC_DATA 0x0120
  65. #define OMAP_HSMMC_PSTATE 0x0124
  66. #define OMAP_HSMMC_HCTL 0x0128
  67. #define OMAP_HSMMC_SYSCTL 0x012C
  68. #define OMAP_HSMMC_STAT 0x0130
  69. #define OMAP_HSMMC_IE 0x0134
  70. #define OMAP_HSMMC_ISE 0x0138
  71. #define OMAP_HSMMC_AC12 0x013C
  72. #define OMAP_HSMMC_CAPA 0x0140
  73. #define OMAP_HSMMC_CAPA2 0x0144
  74. #define OMAP_HSMMC_ADMAES 0x0154
  75. #define OMAP_HSMMC_ADMASAL 0x0158
  76. #define MADMA_EN (1 << 0)
  77. #define VS18 (1 << 26)
  78. #define VS30 (1 << 25)
  79. #define VS33 (1 << 24)
  80. #define VS_MASK (0x7 << 24)
  81. #define HSS (1 << 21)
  82. #define SDVS18 (0x5 << 9)
  83. #define SDVS30 (0x6 << 9)
  84. #define SDVS33 (0x7 << 9)
  85. #define SDVS_MASK 0x00000E00
  86. #define SDVSCLR 0xFFFFF1FF
  87. #define SDVSDET 0x00000400
  88. #define DMA_SELECT (2 << 3)
  89. #define AUTOIDLE 0x1
  90. #define SDBP (1 << 8)
  91. #define DTO 0xe
  92. #define ICE 0x1
  93. #define ICS 0x2
  94. #define CEN (1 << 2)
  95. #define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
  96. #define CLKD_MASK 0x0000FFC0
  97. #define CLKD_SHIFT 6
  98. #define DTO_MASK 0x000F0000
  99. #define DTO_SHIFT 16
  100. #define INIT_STREAM (1 << 1)
  101. #define ACEN_ACMD23 (2 << 2)
  102. #define DP_SELECT (1 << 21)
  103. #define DDIR (1 << 4)
  104. #define DMAE 0x1
  105. #define MSBS (1 << 5)
  106. #define BCE (1 << 1)
  107. #define FOUR_BIT (1 << 1)
  108. #define HSPE (1 << 2)
  109. #define IWE (1 << 24)
  110. #define DMA_MASTER (1 << 20)
  111. #define DDR (1 << 19)
  112. #define CLKEXTFREE (1 << 16)
  113. #define CTPL (1 << 11)
  114. #define DW8 (1 << 5)
  115. #define OD 0x1
  116. #define STAT_CLEAR 0xFFFFFFFF
  117. #define INIT_STREAM_CMD 0x00000000
  118. #define DUAL_VOLT_OCR_BIT 7
  119. #define SRC (1 << 25)
  120. #define SRD (1 << 26)
  121. #define SOFTRESET (1 << 1)
  122. /* PSTATE */
  123. #define DLEV_DAT(x) (1 << (20 + (x)))
  124. /* AC12 */
  125. #define AC12_V1V8_SIGEN (1 << 19)
  126. #define AC12_SCLK_SEL (1 << 23)
  127. #define AC12_UHSMC_MASK (7 << 16)
  128. #define AC12_UHSMC_SDR12 (0 << 16)
  129. #define AC12_UHSMC_SDR25 (1 << 16)
  130. #define AC12_UHSMC_SDR50 (2 << 16)
  131. #define AC12_UHSMC_SDR104 (3 << 16)
  132. #define AC12_UHSMC_DDR50 (4 << 16)
  133. #define AC12_UHSMC_RES (0x7 << 16)
  134. /* DLL */
  135. #define DLL_SWT (1 << 20)
  136. #define DLL_FORCE_SR_C_SHIFT 13
  137. #define DLL_FORCE_SR_C_MASK 0x7f
  138. #define DLL_FORCE_VALUE (1 << 12)
  139. #define DLL_CALIB (1 << 1)
  140. #define MAX_PHASE_DELAY 0x7c
  141. /* CAPA2 */
  142. #define CAPA2_TSDR50 (1 << 13)
  143. /* Interrupt masks for IE and ISE register */
  144. #define CC_EN (1 << 0)
  145. #define TC_EN (1 << 1)
  146. #define BWR_EN (1 << 4)
  147. #define BRR_EN (1 << 5)
  148. #define CIRQ_EN (1 << 8)
  149. #define ERR_EN (1 << 15)
  150. #define CTO_EN (1 << 16)
  151. #define CCRC_EN (1 << 17)
  152. #define CEB_EN (1 << 18)
  153. #define CIE_EN (1 << 19)
  154. #define DTO_EN (1 << 20)
  155. #define DCRC_EN (1 << 21)
  156. #define DEB_EN (1 << 22)
  157. #define ACE_EN (1 << 24)
  158. #define ADMAE_EN (1 << 25)
  159. #define CERR_EN (1 << 28)
  160. #define BADA_EN (1 << 29)
  161. #define INT_EN_MASK (BADA_EN | CERR_EN | ADMAE_EN | ACE_EN | DEB_EN | DCRC_EN |\
  162. DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
  163. BRR_EN | BWR_EN | TC_EN | CC_EN)
  164. #define CNI (1 << 7)
  165. #define ACIE (1 << 4)
  166. #define ACEB (1 << 3)
  167. #define ACCE (1 << 2)
  168. #define ACTO (1 << 1)
  169. #define ACNE (1 << 0)
  170. #define MMC_AUTOSUSPEND_DELAY 100
  171. #define MMC_TIMEOUT_MS 20 /* 20 mSec */
  172. #define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
  173. #define OMAP_MMC_MIN_CLOCK 400000
  174. #define OMAP_MMC_MAX_CLOCK 52000000
  175. #define DRIVER_NAME "omap_hsmmc"
  176. #define VDD_1V8 1800000 /* 180000 uV */
  177. #define VDD_3V0 3000000 /* 300000 uV */
  178. #define VDD_3V3 3300000 /* 330000 uV */
  179. #define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
  180. #define VDD_30_31 (ffs(MMC_VDD_30_31) - 1)
  181. #define VDD_33_34 (ffs(MMC_VDD_33_34) - 1)
  182. #define CON_CLKEXTFREE (1 << 16)
  183. #define CON_PADEN (1 << 15)
  184. #define PSTATE_CLEV (1 << 24)
  185. #define PSTATE_DLEV (0xF << 20)
  186. #define PSTATE_DLEV_DAT0 (0x1 << 20)
  187. #define MMC_BLOCK_TRANSFER_TIME_NS(blksz, bus_width, freq) \
  188. ((unsigned long long) \
  189. (2 * (((blksz) * NSEC_PER_SEC * \
  190. (8 / (bus_width))) / (freq))))
  191. /*
  192. * One controller can have multiple slots, like on some omap boards using
  193. * omap.c controller driver. Luckily this is not currently done on any known
  194. * omap_hsmmc.c device.
  195. */
  196. #define mmc_pdata(host) host->pdata
  197. /*
  198. * MMC Host controller read/write API's
  199. */
  200. #define OMAP_HSMMC_READ(base, reg) \
  201. __raw_readl((base) + OMAP_HSMMC_##reg)
  202. #define OMAP_HSMMC_WRITE(base, reg, val) \
  203. __raw_writel((val), (base) + OMAP_HSMMC_##reg)
  204. struct omap_hsmmc_adma_desc {
  205. u8 attr;
  206. u8 reserved;
  207. u16 len;
  208. u32 addr;
  209. } __packed;
  210. #define ADMA_DESC_SIZE 8
  211. #define ADMA_MAX_LEN 65532
  212. /* Decriptor table defines */
  213. #define ADMA_DESC_ATTR_VALID BIT(0)
  214. #define ADMA_DESC_ATTR_END BIT(1)
  215. #define ADMA_DESC_ATTR_INT BIT(2)
  216. #define ADMA_DESC_ATTR_ACT1 BIT(4)
  217. #define ADMA_DESC_ATTR_ACT2 BIT(5)
  218. #define ADMA_DESC_TRANSFER_DATA ADMA_DESC_ATTR_ACT2
  219. #define ADMA_DESC_LINK_DESC (ADMA_DESC_ATTR_ACT1 | ADMA_DESC_ATTR_ACT2)
  220. /* ADMA error status */
  221. #define AES_MASK 0x3
  222. #define ST_STOP 0x0
  223. #define ST_FDS 0x1
  224. #define ST_TFR 0x3
  225. struct omap_hsmmc_next {
  226. unsigned int dma_len;
  227. s32 cookie;
  228. };
  229. struct omap_hsmmc_host {
  230. struct device *dev;
  231. struct mmc_host *mmc;
  232. struct mmc_request *mrq;
  233. struct mmc_command *cmd;
  234. u32 last_cmd;
  235. struct mmc_data *data;
  236. struct clk *fclk;
  237. struct clk *dbclk;
  238. struct regulator *pbias;
  239. bool pbias_enabled;
  240. bool io_3_3v_support;
  241. void __iomem *base;
  242. int vqmmc_enabled;
  243. resource_size_t mapbase;
  244. spinlock_t irq_lock; /* Prevent races with irq handler */
  245. unsigned int dma_len;
  246. unsigned int dma_sg_idx;
  247. unsigned char bus_mode;
  248. unsigned char power_mode;
  249. unsigned char timing;
  250. int suspended;
  251. u32 con;
  252. u32 hctl;
  253. u32 sysctl;
  254. u32 capa;
  255. int irq;
  256. int wake_irq;
  257. int dma_ch;
  258. int use_adma;
  259. struct dma_chan *tx_chan;
  260. struct dma_chan *rx_chan;
  261. int response_busy;
  262. int context_loss;
  263. int protect_card;
  264. int reqs_blocked;
  265. int req_in_progress;
  266. unsigned long clk_rate;
  267. unsigned int flags;
  268. #define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
  269. #define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
  270. #define CLKEXTFREE_ENABLED (1 << 2) /* CLKEXTFREE enabled */
  271. struct omap_hsmmc_next next_data;
  272. struct omap_hsmmc_platform_data *pdata;
  273. bool is_tuning;
  274. struct timer_list timer;
  275. unsigned long long data_timeout;
  276. struct pinctrl *pinctrl;
  277. struct pinctrl_state *pinctrl_state;
  278. struct pinctrl_state *default_pinctrl_state;
  279. struct pinctrl_state *sdr104_pinctrl_state;
  280. struct pinctrl_state *hs200_1_8v_pinctrl_state;
  281. struct pinctrl_state *ddr50_pinctrl_state;
  282. struct pinctrl_state *sdr50_pinctrl_state;
  283. struct pinctrl_state *sdr25_pinctrl_state;
  284. struct pinctrl_state *sdr12_pinctrl_state;
  285. struct pinctrl_state *hs_pinctrl_state;
  286. struct pinctrl_state *ddr_1_8v_pinctrl_state;
  287. struct omap_hsmmc_adma_desc *adma_desc_table;
  288. dma_addr_t adma_desc_table_addr;
  289. /* return MMC cover switch state, can be NULL if not supported.
  290. *
  291. * possible return values:
  292. * 0 - closed
  293. * 1 - open
  294. */
  295. int (*get_cover_state)(struct device *dev);
  296. int (*card_detect)(struct device *dev);
  297. };
  298. struct omap_mmc_of_data {
  299. u32 reg_offset;
  300. u8 controller_flags;
  301. };
  302. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
  303. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host, int iov);
  304. static void omap_hsmmc_disable_tuning(struct omap_hsmmc_host *host);
  305. static void omap_hsmmc_set_capabilities(struct omap_hsmmc_host *host);
  306. static int omap_hsmmc_card_detect(struct device *dev)
  307. {
  308. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  309. return mmc_gpio_get_cd(host->mmc);
  310. }
  311. static int omap_hsmmc_get_cover_state(struct device *dev)
  312. {
  313. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  314. return mmc_gpio_get_cd(host->mmc);
  315. }
  316. static int omap_hsmmc_enable_supply(struct mmc_host *mmc, int iov)
  317. {
  318. int ret;
  319. struct omap_hsmmc_host *host = mmc_priv(mmc);
  320. struct mmc_ios *ios = &mmc->ios;
  321. int uvoltage;
  322. if (mmc->supply.vmmc) {
  323. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
  324. if (ret)
  325. return ret;
  326. }
  327. /* Enable interface voltage rail, if needed */
  328. if (mmc->supply.vqmmc) {
  329. if (host->vqmmc_enabled) {
  330. ret = regulator_disable(mmc->supply.vqmmc);
  331. if (ret) {
  332. dev_err(mmc_dev(mmc),
  333. "vmmc_aux reg disable failed\n");
  334. goto err_vqmmc;
  335. }
  336. host->vqmmc_enabled = 0;
  337. }
  338. if (iov == VDD_165_195)
  339. uvoltage = VDD_1V8;
  340. else if (iov == VDD_33_34)
  341. uvoltage = VDD_3V3;
  342. else
  343. uvoltage = VDD_3V0;
  344. ret = regulator_set_voltage(mmc->supply.vqmmc, uvoltage,
  345. uvoltage);
  346. if (ret) {
  347. dev_err(mmc_dev(mmc), "vmmc_aux set voltage failed\n");
  348. goto err_vqmmc;
  349. }
  350. ret = regulator_enable(mmc->supply.vqmmc);
  351. if (ret) {
  352. dev_err(mmc_dev(mmc), "vmmc_aux reg enable failed\n");
  353. goto err_vqmmc;
  354. }
  355. host->vqmmc_enabled = 1;
  356. }
  357. return 0;
  358. err_vqmmc:
  359. if (mmc->supply.vmmc)
  360. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  361. return ret;
  362. }
  363. static int omap_hsmmc_disable_supply(struct mmc_host *mmc)
  364. {
  365. int ret;
  366. int status;
  367. struct omap_hsmmc_host *host = mmc_priv(mmc);
  368. if (mmc->supply.vqmmc && host->vqmmc_enabled) {
  369. ret = regulator_disable(mmc->supply.vqmmc);
  370. if (ret) {
  371. dev_err(mmc_dev(mmc), "vmmc_aux reg disable failed\n");
  372. return ret;
  373. }
  374. host->vqmmc_enabled = 0;
  375. }
  376. if (mmc->supply.vmmc) {
  377. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  378. if (ret)
  379. goto err_set_ocr;
  380. }
  381. return 0;
  382. err_set_ocr:
  383. if (mmc->supply.vqmmc) {
  384. status = regulator_enable(mmc->supply.vqmmc);
  385. if (status)
  386. dev_err(mmc_dev(mmc), "vmmc_aux re-enable failed\n");
  387. }
  388. return ret;
  389. }
  390. static int omap_hsmmc_set_pbias(struct omap_hsmmc_host *host, bool power_on,
  391. int iov)
  392. {
  393. int ret;
  394. int uvoltage;
  395. if (!host->pbias)
  396. return 0;
  397. if (power_on) {
  398. if (iov <= VDD_165_195)
  399. uvoltage = VDD_1V8;
  400. else if (iov == VDD_33_34)
  401. uvoltage = VDD_3V3;
  402. else
  403. uvoltage = VDD_3V0;
  404. ret = regulator_set_voltage(host->pbias, uvoltage, uvoltage);
  405. if (ret) {
  406. dev_err(host->dev, "pbias set voltage failed\n");
  407. return ret;
  408. }
  409. if (host->pbias_enabled == 0) {
  410. ret = regulator_enable(host->pbias);
  411. if (ret) {
  412. dev_err(host->dev, "pbias reg enable fail\n");
  413. return ret;
  414. }
  415. host->pbias_enabled = 1;
  416. }
  417. } else {
  418. if (host->pbias_enabled == 1) {
  419. ret = regulator_disable(host->pbias);
  420. if (ret) {
  421. dev_err(host->dev, "pbias reg disable fail\n");
  422. return ret;
  423. }
  424. host->pbias_enabled = 0;
  425. }
  426. }
  427. return 0;
  428. }
  429. static int omap_hsmmc_set_power(struct omap_hsmmc_host *host, int power_on,
  430. int iov)
  431. {
  432. struct mmc_host *mmc = host->mmc;
  433. int ret = 0;
  434. if (mmc_pdata(host)->set_power)
  435. return mmc_pdata(host)->set_power(host->dev, power_on, iov);
  436. if (mmc_pdata(host)->before_set_reg)
  437. mmc_pdata(host)->before_set_reg(host->dev, power_on, iov);
  438. ret = omap_hsmmc_set_pbias(host, false, 0);
  439. if (ret)
  440. return ret;
  441. /*
  442. * Assume Vcc regulator is used only to power the card ... OMAP
  443. * VDDS is used to power the pins, optionally with a transceiver to
  444. * support cards using voltages other than VDDS (1.8V nominal). When a
  445. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  446. *
  447. * In some cases this regulator won't support enable/disable;
  448. * e.g. it's a fixed rail for a WLAN chip.
  449. *
  450. * In other cases vcc_aux switches interface power. Example, for
  451. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  452. * chips/cards need an interface voltage rail too.
  453. */
  454. if (power_on) {
  455. ret = omap_hsmmc_enable_supply(mmc, iov);
  456. if (ret)
  457. return ret;
  458. ret = omap_hsmmc_set_pbias(host, true, iov);
  459. if (ret)
  460. goto err_set_voltage;
  461. } else {
  462. ret = omap_hsmmc_disable_supply(mmc);
  463. if (ret)
  464. return ret;
  465. }
  466. if (mmc_pdata(host)->after_set_reg)
  467. mmc_pdata(host)->after_set_reg(host->dev, power_on, iov);
  468. return 0;
  469. err_set_voltage:
  470. omap_hsmmc_disable_supply(mmc);
  471. return ret;
  472. }
  473. static int omap_hsmmc_disable_boot_regulator(struct regulator *reg)
  474. {
  475. int ret;
  476. if (!reg)
  477. return 0;
  478. if (regulator_is_enabled(reg)) {
  479. ret = regulator_enable(reg);
  480. if (ret)
  481. return ret;
  482. ret = regulator_disable(reg);
  483. if (ret)
  484. return ret;
  485. }
  486. return 0;
  487. }
  488. static int omap_hsmmc_disable_boot_regulators(struct omap_hsmmc_host *host)
  489. {
  490. struct mmc_host *mmc = host->mmc;
  491. int ret;
  492. /*
  493. * disable regulators enabled during boot and get the usecount
  494. * right so that regulators can be enabled/disabled by checking
  495. * the return value of regulator_is_enabled
  496. */
  497. ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vmmc);
  498. if (ret) {
  499. dev_err(host->dev, "fail to disable boot enabled vmmc reg\n");
  500. return ret;
  501. }
  502. ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vqmmc);
  503. if (ret) {
  504. dev_err(host->dev,
  505. "fail to disable boot enabled vmmc_aux reg\n");
  506. return ret;
  507. }
  508. ret = omap_hsmmc_disable_boot_regulator(host->pbias);
  509. if (ret) {
  510. dev_err(host->dev,
  511. "failed to disable boot enabled pbias reg\n");
  512. return ret;
  513. }
  514. return 0;
  515. }
  516. static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  517. {
  518. int ocr_value = 0;
  519. int ret;
  520. struct mmc_host *mmc = host->mmc;
  521. if (mmc_pdata(host)->set_power)
  522. return 0;
  523. mmc->supply.vmmc = devm_regulator_get_optional(host->dev, "vmmc");
  524. if (IS_ERR(mmc->supply.vmmc)) {
  525. ret = PTR_ERR(mmc->supply.vmmc);
  526. if ((ret != -ENODEV) && host->dev->of_node)
  527. return ret;
  528. dev_dbg(host->dev, "unable to get vmmc regulator %ld\n",
  529. PTR_ERR(mmc->supply.vmmc));
  530. mmc->supply.vmmc = NULL;
  531. } else {
  532. ocr_value = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
  533. if (ocr_value > 0)
  534. mmc_pdata(host)->ocr_mask = ocr_value;
  535. }
  536. /* Allow an aux regulator */
  537. mmc->supply.vqmmc = devm_regulator_get_optional(host->dev, "vmmc_aux");
  538. if (IS_ERR(mmc->supply.vqmmc)) {
  539. ret = PTR_ERR(mmc->supply.vqmmc);
  540. if ((ret != -ENODEV) && host->dev->of_node)
  541. return ret;
  542. dev_dbg(host->dev, "unable to get vmmc_aux regulator %ld\n",
  543. PTR_ERR(mmc->supply.vqmmc));
  544. mmc->supply.vqmmc = NULL;
  545. }
  546. host->pbias = devm_regulator_get_optional(host->dev, "pbias");
  547. if (IS_ERR(host->pbias)) {
  548. ret = PTR_ERR(host->pbias);
  549. if ((ret != -ENODEV) && host->dev->of_node) {
  550. dev_err(host->dev,
  551. "SD card detect fail? enable CONFIG_REGULATOR_PBIAS\n");
  552. return ret;
  553. }
  554. dev_dbg(host->dev, "unable to get pbias regulator %ld\n",
  555. PTR_ERR(host->pbias));
  556. host->pbias = NULL;
  557. }
  558. /* For eMMC do not power off when not in sleep state */
  559. if (mmc_pdata(host)->no_regulator_off_init)
  560. return 0;
  561. ret = omap_hsmmc_disable_boot_regulators(host);
  562. if (ret)
  563. return ret;
  564. return 0;
  565. }
  566. static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
  567. static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
  568. struct omap_hsmmc_host *host,
  569. struct omap_hsmmc_platform_data *pdata)
  570. {
  571. int ret;
  572. if (gpio_is_valid(pdata->gpio_cod)) {
  573. ret = mmc_gpio_request_cd(mmc, pdata->gpio_cod, 0);
  574. if (ret)
  575. return ret;
  576. host->get_cover_state = omap_hsmmc_get_cover_state;
  577. mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
  578. } else if (gpio_is_valid(pdata->gpio_cd)) {
  579. ret = mmc_gpio_request_cd(mmc, pdata->gpio_cd, 0);
  580. if (ret)
  581. return ret;
  582. host->card_detect = omap_hsmmc_card_detect;
  583. }
  584. if (gpio_is_valid(pdata->gpio_wp)) {
  585. ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
  586. if (ret)
  587. return ret;
  588. }
  589. return 0;
  590. }
  591. /*
  592. * Start clock to the card
  593. */
  594. static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
  595. {
  596. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  597. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  598. }
  599. /*
  600. * Stop clock to the card
  601. */
  602. static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
  603. {
  604. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  605. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  606. if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
  607. dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
  608. }
  609. static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
  610. struct mmc_command *cmd)
  611. {
  612. u32 irq_mask = INT_EN_MASK;
  613. unsigned long flags;
  614. if (host->is_tuning)
  615. /*
  616. * OMAP5/DRA74X/DRA72x Errata i802:
  617. * DCRC error interrupts (MMCHS_STAT[21] DCRC=0x1) can occur
  618. * during the tuning procedure. So disable it during the
  619. * tuning procedure.
  620. */
  621. irq_mask &= ~(DCRC_EN | DEB_EN);
  622. /* BRR and BWR need not be enabled for DMA */
  623. irq_mask &= ~(BRR_EN | BWR_EN);
  624. /* Disable timeout for erases or when using software timeout */
  625. if (cmd && (cmd->opcode == MMC_ERASE || host->data_timeout))
  626. irq_mask &= ~DTO_EN;
  627. if (host->flags & CLKEXTFREE_ENABLED)
  628. irq_mask |= CIRQ_EN;
  629. spin_lock_irqsave(&host->irq_lock, flags);
  630. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  631. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  632. /* latch pending CIRQ, but don't signal MMC core */
  633. if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
  634. irq_mask |= CIRQ_EN;
  635. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  636. spin_unlock_irqrestore(&host->irq_lock, flags);
  637. }
  638. static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
  639. {
  640. u32 irq_mask = 0;
  641. unsigned long flags;
  642. spin_lock_irqsave(&host->irq_lock, flags);
  643. /* no transfer running but need to keep cirq if enabled */
  644. if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
  645. irq_mask |= CIRQ_EN;
  646. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  647. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  648. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  649. spin_unlock_irqrestore(&host->irq_lock, flags);
  650. }
  651. /* Calculate divisor for the given clock frequency */
  652. static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
  653. {
  654. u16 dsor = 0;
  655. if (ios->clock) {
  656. dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
  657. if (dsor > CLKD_MAX)
  658. dsor = CLKD_MAX;
  659. }
  660. return dsor;
  661. }
  662. static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
  663. {
  664. struct mmc_ios *ios = &host->mmc->ios;
  665. unsigned long regval;
  666. unsigned long timeout;
  667. unsigned long clkdiv;
  668. dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
  669. omap_hsmmc_stop_clock(host);
  670. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  671. regval = regval & ~(CLKD_MASK | DTO_MASK);
  672. clkdiv = calc_divisor(host, ios);
  673. regval = regval | (clkdiv << 6) | (DTO << 16);
  674. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  675. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  676. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  677. /* Wait till the ICS bit is set */
  678. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  679. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  680. && time_before(jiffies, timeout))
  681. cpu_relax();
  682. /*
  683. * Enable High-Speed Support
  684. * Pre-Requisites
  685. * - Controller should support High-Speed-Enable Bit
  686. * - Controller should not be using DDR Mode
  687. * - Controller should advertise that it supports High Speed
  688. * in capabilities register
  689. * - MMC/SD clock coming out of controller > 25MHz
  690. */
  691. if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
  692. (ios->timing != MMC_TIMING_MMC_DDR52) &&
  693. (ios->timing != MMC_TIMING_UHS_DDR50) &&
  694. ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
  695. regval = OMAP_HSMMC_READ(host->base, HCTL);
  696. if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
  697. regval |= HSPE;
  698. else
  699. regval &= ~HSPE;
  700. OMAP_HSMMC_WRITE(host->base, HCTL, regval);
  701. }
  702. omap_hsmmc_start_clock(host);
  703. }
  704. static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
  705. {
  706. struct mmc_ios *ios = &host->mmc->ios;
  707. u32 con;
  708. con = OMAP_HSMMC_READ(host->base, CON);
  709. if (ios->timing == MMC_TIMING_MMC_DDR52 ||
  710. ios->timing == MMC_TIMING_UHS_DDR50)
  711. con |= DDR; /* configure in DDR mode */
  712. else
  713. con &= ~DDR;
  714. switch (ios->bus_width) {
  715. case MMC_BUS_WIDTH_8:
  716. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  717. break;
  718. case MMC_BUS_WIDTH_4:
  719. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  720. OMAP_HSMMC_WRITE(host->base, HCTL,
  721. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  722. break;
  723. case MMC_BUS_WIDTH_1:
  724. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  725. OMAP_HSMMC_WRITE(host->base, HCTL,
  726. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  727. break;
  728. }
  729. }
  730. static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
  731. {
  732. struct mmc_ios *ios = &host->mmc->ios;
  733. u32 con;
  734. con = OMAP_HSMMC_READ(host->base, CON);
  735. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  736. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  737. else
  738. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  739. }
  740. #ifdef CONFIG_PM
  741. /*
  742. * Restore the MMC host context, if it was lost as result of a
  743. * power state change.
  744. */
  745. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  746. {
  747. struct mmc_ios *ios = &host->mmc->ios;
  748. if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
  749. host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
  750. host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
  751. host->capa == OMAP_HSMMC_READ(host->base, CAPA))
  752. return 0;
  753. host->context_loss++;
  754. omap_hsmmc_set_capabilities(host);
  755. if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
  756. OMAP_HSMMC_WRITE(host->base, HCTL,
  757. OMAP_HSMMC_READ(host->base, HCTL) | IWE);
  758. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  759. OMAP_HSMMC_WRITE(host->base, IE, 0);
  760. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  761. if (host->use_adma) {
  762. u32 val;
  763. val = OMAP_HSMMC_READ(host->base, CON);
  764. val |= DMA_MASTER;
  765. OMAP_HSMMC_WRITE(host->base, CON, val);
  766. val = OMAP_HSMMC_READ(host->base, HCTL);
  767. val |= DMA_SELECT;
  768. OMAP_HSMMC_WRITE(host->base, HCTL, val);
  769. }
  770. /* Do not initialize card-specific things if the power is off */
  771. if (host->power_mode == MMC_POWER_OFF)
  772. goto out;
  773. omap_hsmmc_conf_bus_power(host, ios->signal_voltage);
  774. omap_hsmmc_set_bus_width(host);
  775. omap_hsmmc_set_clock(host);
  776. omap_hsmmc_set_bus_mode(host);
  777. out:
  778. dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
  779. host->context_loss);
  780. return 0;
  781. }
  782. /*
  783. * Save the MMC host context (store the number of power state changes so far).
  784. */
  785. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  786. {
  787. host->con = OMAP_HSMMC_READ(host->base, CON);
  788. host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
  789. host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
  790. host->capa = OMAP_HSMMC_READ(host->base, CAPA);
  791. }
  792. #else
  793. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  794. {
  795. return 0;
  796. }
  797. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  798. {
  799. }
  800. #endif
  801. /*
  802. * Send init stream sequence to card
  803. * before sending IDLE command
  804. */
  805. static void send_init_stream(struct omap_hsmmc_host *host)
  806. {
  807. int reg = 0;
  808. unsigned long timeout;
  809. if (host->protect_card)
  810. return;
  811. disable_irq(host->irq);
  812. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  813. OMAP_HSMMC_WRITE(host->base, CON,
  814. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  815. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  816. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  817. while ((reg != CC_EN) && time_before(jiffies, timeout))
  818. reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
  819. OMAP_HSMMC_WRITE(host->base, CON,
  820. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  821. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  822. OMAP_HSMMC_READ(host->base, STAT);
  823. enable_irq(host->irq);
  824. }
  825. static inline
  826. int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
  827. {
  828. int r = 1;
  829. if (host->get_cover_state)
  830. r = host->get_cover_state(host->dev);
  831. return r;
  832. }
  833. static ssize_t
  834. omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
  835. char *buf)
  836. {
  837. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  838. struct omap_hsmmc_host *host = mmc_priv(mmc);
  839. return sprintf(buf, "%s\n",
  840. omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
  841. }
  842. static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
  843. static ssize_t
  844. omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
  845. char *buf)
  846. {
  847. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  848. struct omap_hsmmc_host *host = mmc_priv(mmc);
  849. return sprintf(buf, "%s\n", mmc_pdata(host)->name);
  850. }
  851. static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
  852. /*
  853. * Configure the response type and send the cmd.
  854. */
  855. static void
  856. omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
  857. struct mmc_data *data)
  858. {
  859. int cmdreg = 0, resptype = 0, cmdtype = 0;
  860. dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  861. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  862. host->cmd = cmd;
  863. omap_hsmmc_enable_irq(host, cmd);
  864. host->response_busy = 0;
  865. if (cmd->flags & MMC_RSP_PRESENT) {
  866. if (cmd->flags & MMC_RSP_136)
  867. resptype = 1;
  868. else if (cmd->flags & MMC_RSP_BUSY) {
  869. resptype = 3;
  870. host->response_busy = 1;
  871. } else
  872. resptype = 2;
  873. }
  874. /*
  875. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  876. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  877. * a val of 0x3, rest 0x0.
  878. */
  879. if (cmd == host->mrq->stop)
  880. cmdtype = 0x3;
  881. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  882. if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
  883. host->mrq->sbc) {
  884. cmdreg |= ACEN_ACMD23;
  885. OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
  886. }
  887. if (data) {
  888. cmdreg |= DP_SELECT | MSBS | BCE;
  889. if (data->flags & MMC_DATA_READ)
  890. cmdreg |= DDIR;
  891. else
  892. cmdreg &= ~(DDIR);
  893. }
  894. /* Tuning command is special. Data Present Select should be set */
  895. if ((cmd->opcode == MMC_SEND_TUNING_BLOCK) ||
  896. (cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))
  897. cmdreg |= DP_SELECT | DDIR;
  898. cmdreg |= DMAE;
  899. host->req_in_progress = 1;
  900. host->last_cmd = cmd->opcode;
  901. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  902. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  903. }
  904. static int
  905. omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
  906. {
  907. if (data->flags & MMC_DATA_WRITE)
  908. return DMA_TO_DEVICE;
  909. else
  910. return DMA_FROM_DEVICE;
  911. }
  912. static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
  913. struct mmc_data *data)
  914. {
  915. return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
  916. }
  917. static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
  918. {
  919. int dma_ch;
  920. unsigned long flags;
  921. spin_lock_irqsave(&host->irq_lock, flags);
  922. host->req_in_progress = 0;
  923. dma_ch = host->dma_ch;
  924. spin_unlock_irqrestore(&host->irq_lock, flags);
  925. omap_hsmmc_disable_irq(host);
  926. /* Do not complete the request if DMA is still in progress */
  927. if (mrq->data && dma_ch != -1)
  928. return;
  929. host->mrq = NULL;
  930. mmc_request_done(host->mmc, mrq);
  931. }
  932. /*
  933. * Notify the transfer complete to MMC core
  934. */
  935. static void
  936. omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
  937. {
  938. if (!data) {
  939. struct mmc_request *mrq = host->mrq;
  940. /* TC before CC from CMD6 - don't know why, but it happens */
  941. if (host->cmd && host->cmd->opcode == 6 &&
  942. host->response_busy) {
  943. host->response_busy = 0;
  944. return;
  945. }
  946. omap_hsmmc_request_done(host, mrq);
  947. return;
  948. }
  949. if (host->use_adma && host->data && !data->host_cookie)
  950. dma_unmap_sg(host->dev, data->sg, data->sg_len,
  951. omap_hsmmc_get_dma_dir(host, data));
  952. host->data = NULL;
  953. if (!data->error)
  954. data->bytes_xfered += data->blocks * (data->blksz);
  955. else
  956. data->bytes_xfered = 0;
  957. if (data->stop && (data->error || !host->mrq->sbc))
  958. omap_hsmmc_start_command(host, data->stop, NULL);
  959. else
  960. omap_hsmmc_request_done(host, data->mrq);
  961. }
  962. /*
  963. * Notify the core about command completion
  964. */
  965. static void
  966. omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
  967. {
  968. if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
  969. !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
  970. host->cmd = NULL;
  971. omap_hsmmc_start_dma_transfer(host);
  972. omap_hsmmc_start_command(host, host->mrq->cmd,
  973. host->mrq->data);
  974. return;
  975. }
  976. host->cmd = NULL;
  977. if (cmd->flags & MMC_RSP_PRESENT) {
  978. if (cmd->flags & MMC_RSP_136) {
  979. /* response type 2 */
  980. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  981. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  982. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  983. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  984. } else {
  985. /* response types 1, 1b, 3, 4, 5, 6 */
  986. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  987. }
  988. }
  989. if ((host->data == NULL && !host->response_busy) || cmd->error)
  990. omap_hsmmc_request_done(host, host->mrq);
  991. }
  992. /*
  993. * DMA clean up for command errors
  994. */
  995. static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
  996. {
  997. int dma_ch;
  998. unsigned long flags;
  999. host->data->error = errno;
  1000. spin_lock_irqsave(&host->irq_lock, flags);
  1001. dma_ch = host->dma_ch;
  1002. host->dma_ch = -1;
  1003. spin_unlock_irqrestore(&host->irq_lock, flags);
  1004. if (host->use_adma) {
  1005. dma_unmap_sg(host->dev, host->data->sg, host->data->sg_len,
  1006. omap_hsmmc_get_dma_dir(host, host->data));
  1007. host->data->host_cookie = 0;
  1008. } else if (dma_ch != -1) {
  1009. struct dma_chan *chan = omap_hsmmc_get_dma_chan(host,
  1010. host->data);
  1011. dmaengine_terminate_all(chan);
  1012. dma_unmap_sg(chan->device->dev,
  1013. host->data->sg, host->data->sg_len,
  1014. omap_hsmmc_get_dma_dir(host, host->data));
  1015. host->data->host_cookie = 0;
  1016. }
  1017. host->data = NULL;
  1018. }
  1019. /*
  1020. * Readable error output
  1021. */
  1022. #ifdef CONFIG_MMC_DEBUG
  1023. static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
  1024. {
  1025. /* --- means reserved bit without definition at documentation */
  1026. static const char *omap_hsmmc_status_bits[] = {
  1027. "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
  1028. "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
  1029. "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
  1030. "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
  1031. };
  1032. char res[256];
  1033. char *buf = res;
  1034. int len, i;
  1035. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  1036. buf += len;
  1037. for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
  1038. if (status & (1 << i)) {
  1039. len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
  1040. buf += len;
  1041. }
  1042. dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
  1043. }
  1044. #else
  1045. static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
  1046. u32 status)
  1047. {
  1048. }
  1049. #endif /* CONFIG_MMC_DEBUG */
  1050. /*
  1051. * MMC controller internal state machines reset
  1052. *
  1053. * Used to reset command or data internal state machines, using respectively
  1054. * SRC or SRD bit of SYSCTL register
  1055. * Can be called from interrupt context
  1056. */
  1057. static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
  1058. unsigned long bit)
  1059. {
  1060. unsigned long i = 0;
  1061. unsigned long limit = MMC_TIMEOUT_US;
  1062. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  1063. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  1064. /*
  1065. * OMAP4 ES2 and greater has an updated reset logic.
  1066. * Monitor a 0->1 transition first
  1067. */
  1068. if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
  1069. while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
  1070. && (i++ < limit))
  1071. udelay(1);
  1072. }
  1073. i = 0;
  1074. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  1075. (i++ < limit))
  1076. udelay(1);
  1077. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  1078. dev_err(mmc_dev(host->mmc),
  1079. "Timeout waiting on controller reset in %s\n",
  1080. __func__);
  1081. }
  1082. static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
  1083. int err, int end_cmd)
  1084. {
  1085. if (end_cmd) {
  1086. omap_hsmmc_reset_controller_fsm(host, SRC);
  1087. if (host->cmd)
  1088. host->cmd->error = err;
  1089. }
  1090. if (host->data) {
  1091. omap_hsmmc_reset_controller_fsm(host, SRD);
  1092. omap_hsmmc_dma_cleanup(host, err);
  1093. } else if (host->mrq && host->mrq->cmd)
  1094. host->mrq->cmd->error = err;
  1095. }
  1096. static void omap_hsmmc_adma_err(struct omap_hsmmc_host *host)
  1097. {
  1098. u32 admaes, admasal;
  1099. admaes = OMAP_HSMMC_READ(host->base, ADMAES);
  1100. admasal = OMAP_HSMMC_READ(host->base, ADMASAL);
  1101. switch (admaes & AES_MASK) {
  1102. case ST_FDS:
  1103. dev_err(mmc_dev(host->mmc),
  1104. "ADMA err: ST_FDS, erroneous desc at 0x%08x\n",
  1105. admasal);
  1106. break;
  1107. case ST_STOP:
  1108. dev_err(mmc_dev(host->mmc),
  1109. "ADMA err: ST_STOP, desc at 0x%08x follows the erroneous one\n",
  1110. admasal);
  1111. break;
  1112. case ST_TFR:
  1113. dev_err(mmc_dev(host->mmc),
  1114. "ADMA err: ST_TFR, desc at 0x%08x follows the erroneous one\n",
  1115. admasal);
  1116. break;
  1117. default:
  1118. dev_warn(mmc_dev(host->mmc), "Unexpected ADMA error state\n");
  1119. break;
  1120. }
  1121. }
  1122. static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
  1123. {
  1124. struct mmc_data *data;
  1125. int end_cmd = 0, end_trans = 0;
  1126. int error = 0;
  1127. data = host->data;
  1128. dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  1129. if (status & ERR_EN) {
  1130. omap_hsmmc_dbg_report_irq(host, status);
  1131. if (status & (CTO_EN | CCRC_EN | CEB_EN))
  1132. end_cmd = 1;
  1133. if (host->data || host->response_busy) {
  1134. end_trans = !end_cmd;
  1135. host->response_busy = 0;
  1136. }
  1137. if (status & ADMAE_EN) {
  1138. omap_hsmmc_adma_err(host);
  1139. end_trans = 1;
  1140. data->error = -EIO;
  1141. }
  1142. if (status & (CTO_EN | DTO_EN))
  1143. hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
  1144. else if (status & (CCRC_EN | DCRC_EN | DEB_EN | CEB_EN |
  1145. BADA_EN))
  1146. hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
  1147. if (status & ACE_EN) {
  1148. u32 ac12;
  1149. ac12 = OMAP_HSMMC_READ(host->base, AC12);
  1150. if (!(ac12 & ACNE) && host->mrq->sbc) {
  1151. end_cmd = 1;
  1152. if (ac12 & ACTO)
  1153. error = -ETIMEDOUT;
  1154. else if (ac12 & (ACCE | ACEB | ACIE))
  1155. error = -EILSEQ;
  1156. host->mrq->sbc->error = error;
  1157. hsmmc_command_incomplete(host, error, end_cmd);
  1158. }
  1159. dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
  1160. }
  1161. }
  1162. OMAP_HSMMC_WRITE(host->base, STAT, status);
  1163. if (end_cmd || ((status & CC_EN) && host->cmd)) {
  1164. omap_hsmmc_cmd_done(host, host->cmd);
  1165. if (host->data_timeout) {
  1166. unsigned long timeout;
  1167. timeout = jiffies +
  1168. nsecs_to_jiffies(host->data_timeout);
  1169. mod_timer(&host->timer, timeout);
  1170. }
  1171. }
  1172. if ((end_trans || (status & TC_EN)) && host->mrq)
  1173. omap_hsmmc_xfer_done(host, data);
  1174. }
  1175. /*
  1176. * MMC controller IRQ handler
  1177. */
  1178. static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
  1179. {
  1180. struct omap_hsmmc_host *host = dev_id;
  1181. int status;
  1182. status = OMAP_HSMMC_READ(host->base, STAT);
  1183. while (status & (INT_EN_MASK | CIRQ_EN)) {
  1184. /*
  1185. * During a successful bulk data transfer command-completion
  1186. * interrupt and transfer-completion interrupt will be
  1187. * generated, but software-timeout timer should be deleted
  1188. * only on non-cc interrupts (transfer complete or error)
  1189. */
  1190. if (host->data_timeout && (status & (~CC_EN))) {
  1191. del_timer(&host->timer);
  1192. host->data_timeout = 0;
  1193. }
  1194. if (host->req_in_progress)
  1195. omap_hsmmc_do_irq(host, status);
  1196. if (status & CIRQ_EN)
  1197. mmc_signal_sdio_irq(host->mmc);
  1198. /* Flush posted write */
  1199. status = OMAP_HSMMC_READ(host->base, STAT);
  1200. }
  1201. return IRQ_HANDLED;
  1202. }
  1203. static void omap_hsmmc_soft_timeout(unsigned long data)
  1204. {
  1205. struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)data;
  1206. bool end_trans = false;
  1207. omap_hsmmc_disable_irq(host);
  1208. if (host->data || host->response_busy) {
  1209. host->response_busy = 0;
  1210. end_trans = true;
  1211. }
  1212. hsmmc_command_incomplete(host, -ETIMEDOUT, 0);
  1213. if (end_trans && host->mrq)
  1214. omap_hsmmc_xfer_done(host, host->data);
  1215. else if (host->cmd)
  1216. omap_hsmmc_cmd_done(host, host->cmd);
  1217. host->data_timeout = 0;
  1218. }
  1219. static void set_sd_bus_power(struct omap_hsmmc_host *host)
  1220. {
  1221. unsigned long i;
  1222. OMAP_HSMMC_WRITE(host->base, HCTL,
  1223. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  1224. for (i = 0; i < loops_per_jiffy; i++) {
  1225. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  1226. break;
  1227. cpu_relax();
  1228. }
  1229. }
  1230. /* Protect the card while the cover is open */
  1231. static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
  1232. {
  1233. if (!host->get_cover_state)
  1234. return;
  1235. host->reqs_blocked = 0;
  1236. if (host->get_cover_state(host->dev)) {
  1237. if (host->protect_card) {
  1238. dev_info(host->dev, "%s: cover is closed, "
  1239. "card is now accessible\n",
  1240. mmc_hostname(host->mmc));
  1241. host->protect_card = 0;
  1242. }
  1243. } else {
  1244. if (!host->protect_card) {
  1245. dev_info(host->dev, "%s: cover is open, "
  1246. "card is now inaccessible\n",
  1247. mmc_hostname(host->mmc));
  1248. host->protect_card = 1;
  1249. }
  1250. }
  1251. }
  1252. /*
  1253. * irq handler when (cell-phone) cover is mounted/removed
  1254. */
  1255. static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
  1256. {
  1257. struct omap_hsmmc_host *host = dev_id;
  1258. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  1259. omap_hsmmc_protect_card(host);
  1260. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  1261. return IRQ_HANDLED;
  1262. }
  1263. static void omap_hsmmc_dma_callback(void *param)
  1264. {
  1265. struct omap_hsmmc_host *host = param;
  1266. struct dma_chan *chan;
  1267. struct mmc_data *data;
  1268. int req_in_progress;
  1269. spin_lock_irq(&host->irq_lock);
  1270. if (host->dma_ch < 0) {
  1271. spin_unlock_irq(&host->irq_lock);
  1272. return;
  1273. }
  1274. data = host->mrq->data;
  1275. chan = omap_hsmmc_get_dma_chan(host, data);
  1276. if (!data->host_cookie)
  1277. dma_unmap_sg(chan->device->dev,
  1278. data->sg, data->sg_len,
  1279. omap_hsmmc_get_dma_dir(host, data));
  1280. req_in_progress = host->req_in_progress;
  1281. host->dma_ch = -1;
  1282. spin_unlock_irq(&host->irq_lock);
  1283. /* If DMA has finished after TC, complete the request */
  1284. if (!req_in_progress) {
  1285. struct mmc_request *mrq = host->mrq;
  1286. host->mrq = NULL;
  1287. mmc_request_done(host->mmc, mrq);
  1288. }
  1289. }
  1290. static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
  1291. struct mmc_data *data,
  1292. struct omap_hsmmc_next *next,
  1293. struct dma_chan *chan)
  1294. {
  1295. int dma_len;
  1296. struct device *dev;
  1297. if (!next && data->host_cookie &&
  1298. data->host_cookie != host->next_data.cookie) {
  1299. dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
  1300. " host->next_data.cookie %d\n",
  1301. __func__, data->host_cookie, host->next_data.cookie);
  1302. data->host_cookie = 0;
  1303. }
  1304. if (chan)
  1305. dev = chan->device->dev;
  1306. else
  1307. dev = mmc_dev(host->mmc);
  1308. /* Check if next job is already prepared */
  1309. if (next || data->host_cookie != host->next_data.cookie) {
  1310. dma_len = dma_map_sg(dev, data->sg, data->sg_len,
  1311. omap_hsmmc_get_dma_dir(host, data));
  1312. } else {
  1313. dma_len = host->next_data.dma_len;
  1314. host->next_data.dma_len = 0;
  1315. }
  1316. if (dma_len == 0)
  1317. return -EINVAL;
  1318. if (next) {
  1319. next->dma_len = dma_len;
  1320. data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
  1321. } else
  1322. host->dma_len = dma_len;
  1323. return 0;
  1324. }
  1325. /*
  1326. * Routine to configure and start DMA for the MMC card
  1327. */
  1328. static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
  1329. struct mmc_request *req)
  1330. {
  1331. struct dma_async_tx_descriptor *tx;
  1332. int ret = 0, i;
  1333. struct mmc_data *data = req->data;
  1334. struct dma_chan *chan;
  1335. struct dma_slave_config cfg = {
  1336. .src_addr = host->mapbase + OMAP_HSMMC_DATA,
  1337. .dst_addr = host->mapbase + OMAP_HSMMC_DATA,
  1338. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  1339. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  1340. .src_maxburst = data->blksz / 4,
  1341. .dst_maxburst = data->blksz / 4,
  1342. };
  1343. /* Sanity check: all the SG entries must be aligned by block size. */
  1344. for (i = 0; i < data->sg_len; i++) {
  1345. struct scatterlist *sgl;
  1346. sgl = data->sg + i;
  1347. if (sgl->length % data->blksz)
  1348. return -EINVAL;
  1349. }
  1350. if ((data->blksz % 4) != 0)
  1351. /* REVISIT: The MMC buffer increments only when MSB is written.
  1352. * Return error for blksz which is non multiple of four.
  1353. */
  1354. return -EINVAL;
  1355. BUG_ON(host->dma_ch != -1);
  1356. chan = omap_hsmmc_get_dma_chan(host, data);
  1357. ret = dmaengine_slave_config(chan, &cfg);
  1358. if (ret)
  1359. return ret;
  1360. ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
  1361. if (ret)
  1362. return ret;
  1363. tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
  1364. data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  1365. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1366. if (!tx) {
  1367. dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
  1368. /* FIXME: cleanup */
  1369. return -1;
  1370. }
  1371. tx->callback = omap_hsmmc_dma_callback;
  1372. tx->callback_param = host;
  1373. /* Does not fail */
  1374. dmaengine_submit(tx);
  1375. host->dma_ch = 1;
  1376. return 0;
  1377. }
  1378. static void set_data_timeout(struct omap_hsmmc_host *host,
  1379. unsigned long long timeout_ns,
  1380. unsigned int timeout_clks)
  1381. {
  1382. unsigned long long timeout = timeout_ns;
  1383. unsigned int cycle_ns;
  1384. uint32_t reg, clkd, dto = 0;
  1385. struct mmc_ios *ios = &host->mmc->ios;
  1386. struct mmc_data *data = host->mrq->data;
  1387. struct mmc_command *cmd = host->mrq->cmd;
  1388. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  1389. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  1390. if (clkd == 0)
  1391. clkd = 1;
  1392. cycle_ns = 1000000000 / (host->clk_rate / clkd);
  1393. do_div(timeout, cycle_ns);
  1394. timeout += timeout_clks;
  1395. if (!timeout)
  1396. goto out;
  1397. while ((timeout & 0x80000000) == 0) {
  1398. dto += 1;
  1399. timeout <<= 1;
  1400. }
  1401. dto = 31 - dto;
  1402. timeout <<= 1;
  1403. if (timeout && dto)
  1404. dto += 1;
  1405. if (dto >= 13)
  1406. dto -= 13;
  1407. else
  1408. dto = 0;
  1409. if (dto > 14) {
  1410. /*
  1411. * DRA7 Errata No i834: When using high speed HS200 and
  1412. * SDR104 cards, the functional clock for MMC module
  1413. * will be 192MHz. At this frequency, the maximum
  1414. * obtainable timeout (DTO =0xE) in hardware is
  1415. * (1/192MHz)*2^27 = 700ms. Commands taking longer than
  1416. * 700ms will be affected by this small window frame
  1417. * and will be timing out frequently even without a
  1418. * genuine timeout from the card. Workaround for
  1419. * this errata is use a software timer instead of
  1420. * hardware timer to provide the timeout requested
  1421. * by the upper layer.
  1422. *
  1423. * The timeout from the upper layer denotes the delay
  1424. * between the end bit of the read command and the
  1425. * start bit of the data block and in the case of
  1426. * multiple-read operation, they also define the
  1427. * typical delay between the end bit of a data
  1428. * block and the start bit of next data block.
  1429. *
  1430. * Calculate the total timeout value for the entire
  1431. * transfer to complete from the timeout value given
  1432. * by the upper layer.
  1433. */
  1434. if (data)
  1435. host->data_timeout = (data->blocks *
  1436. (timeout_ns +
  1437. MMC_BLOCK_TRANSFER_TIME_NS(
  1438. data->blksz, ios->bus_width,
  1439. ios->clock)));
  1440. else if (cmd->flags & MMC_RSP_BUSY)
  1441. host->data_timeout = timeout_ns;
  1442. dto = 14;
  1443. }
  1444. out:
  1445. reg &= ~DTO_MASK;
  1446. reg |= dto << DTO_SHIFT;
  1447. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  1448. }
  1449. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
  1450. {
  1451. struct mmc_request *req = host->mrq;
  1452. struct dma_chan *chan;
  1453. if (!req->data)
  1454. return;
  1455. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  1456. | (req->data->blocks << 16));
  1457. set_data_timeout(host, req->data->timeout_ns,
  1458. req->data->timeout_clks);
  1459. if (host->use_adma) {
  1460. OMAP_HSMMC_WRITE(host->base, ADMASAL,
  1461. (u32)host->adma_desc_table_addr);
  1462. } else {
  1463. chan = omap_hsmmc_get_dma_chan(host, req->data);
  1464. dma_async_issue_pending(chan);
  1465. }
  1466. }
  1467. static int omap_hsmmc_write_adma_desc(struct omap_hsmmc_host *host, void *desc,
  1468. dma_addr_t addr, u16 len, u8 attr)
  1469. {
  1470. struct omap_hsmmc_adma_desc *dma_desc = desc;
  1471. dma_desc->len = len;
  1472. dma_desc->addr = (u32)addr;
  1473. dma_desc->reserved = 0;
  1474. dma_desc->attr = attr;
  1475. return 0;
  1476. }
  1477. static int omap_hsmmc_setup_adma_transfer(struct omap_hsmmc_host *host,
  1478. struct mmc_request *req)
  1479. {
  1480. struct mmc_data *data = req->data;
  1481. struct scatterlist *sg;
  1482. int i;
  1483. int len;
  1484. int ret;
  1485. dma_addr_t addr;
  1486. struct omap_hsmmc_adma_desc *dma_desc;
  1487. ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, NULL);
  1488. if (ret)
  1489. return ret;
  1490. dma_desc = host->adma_desc_table;
  1491. for_each_sg(data->sg, sg, host->dma_len, i) {
  1492. addr = sg_dma_address(sg);
  1493. len = sg_dma_len(sg);
  1494. WARN_ON(len > ADMA_MAX_LEN);
  1495. omap_hsmmc_write_adma_desc(host, dma_desc, addr, len,
  1496. ADMA_DESC_ATTR_VALID |
  1497. ADMA_DESC_TRANSFER_DATA);
  1498. dma_desc++;
  1499. }
  1500. omap_hsmmc_write_adma_desc(host, dma_desc, 0, 0, ADMA_DESC_ATTR_END |
  1501. ADMA_DESC_ATTR_VALID);
  1502. return 0;
  1503. }
  1504. /*
  1505. * Configure block length for MMC/SD cards and initiate the transfer.
  1506. */
  1507. static int
  1508. omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
  1509. {
  1510. int ret;
  1511. unsigned long long timeout;
  1512. host->data = req->data;
  1513. if (req->data == NULL) {
  1514. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  1515. if (req->cmd->flags & MMC_RSP_BUSY) {
  1516. timeout = req->cmd->busy_timeout * NSEC_PER_MSEC;
  1517. /*
  1518. * Set an arbitrary 100ms data timeout for commands with
  1519. * busy signal and no indication of busy_timeout.
  1520. */
  1521. if (!timeout)
  1522. timeout = 100000000U;
  1523. set_data_timeout(host, timeout, 0);
  1524. }
  1525. return 0;
  1526. }
  1527. if (host->use_adma) {
  1528. ret = omap_hsmmc_setup_adma_transfer(host, req);
  1529. if (ret != 0) {
  1530. dev_err(mmc_dev(host->mmc), "MMC adma setup failed\n");
  1531. return ret;
  1532. }
  1533. } else {
  1534. ret = omap_hsmmc_setup_dma_transfer(host, req);
  1535. if (ret != 0) {
  1536. dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
  1537. return ret;
  1538. }
  1539. }
  1540. return 0;
  1541. }
  1542. static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1543. int err)
  1544. {
  1545. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1546. struct mmc_data *data = mrq->data;
  1547. struct device *dev;
  1548. struct dma_chan *c;
  1549. if (data->host_cookie) {
  1550. if (host->use_adma) {
  1551. dev = mmc_dev(mmc);
  1552. } else {
  1553. c = omap_hsmmc_get_dma_chan(host, mrq->data);
  1554. dev = c->device->dev;
  1555. }
  1556. dma_unmap_sg(dev, data->sg, data->sg_len,
  1557. omap_hsmmc_get_dma_dir(host, data));
  1558. data->host_cookie = 0;
  1559. }
  1560. }
  1561. static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1562. bool is_first_req)
  1563. {
  1564. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1565. struct dma_chan *c = NULL;
  1566. if (mrq->data->host_cookie) {
  1567. mrq->data->host_cookie = 0;
  1568. return ;
  1569. }
  1570. if (!host->use_adma)
  1571. c = omap_hsmmc_get_dma_chan(host, mrq->data);
  1572. if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
  1573. &host->next_data, c))
  1574. mrq->data->host_cookie = 0;
  1575. }
  1576. /*
  1577. * Request function. for read/write operation
  1578. */
  1579. static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1580. {
  1581. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1582. int err;
  1583. BUG_ON(host->req_in_progress);
  1584. BUG_ON(host->dma_ch != -1);
  1585. if (host->protect_card) {
  1586. if (host->reqs_blocked < 3) {
  1587. /*
  1588. * Ensure the controller is left in a consistent
  1589. * state by resetting the command and data state
  1590. * machines.
  1591. */
  1592. omap_hsmmc_reset_controller_fsm(host, SRD);
  1593. omap_hsmmc_reset_controller_fsm(host, SRC);
  1594. host->reqs_blocked += 1;
  1595. }
  1596. req->cmd->error = -EBADF;
  1597. if (req->data)
  1598. req->data->error = -EBADF;
  1599. req->cmd->retries = 0;
  1600. mmc_request_done(mmc, req);
  1601. return;
  1602. } else if (host->reqs_blocked)
  1603. host->reqs_blocked = 0;
  1604. WARN_ON(host->mrq != NULL);
  1605. host->mrq = req;
  1606. host->clk_rate = clk_get_rate(host->fclk);
  1607. err = omap_hsmmc_prepare_data(host, req);
  1608. if (err) {
  1609. req->cmd->error = err;
  1610. if (req->data)
  1611. req->data->error = err;
  1612. host->mrq = NULL;
  1613. mmc_request_done(mmc, req);
  1614. return;
  1615. }
  1616. if (req->sbc && !(host->flags & AUTO_CMD23)) {
  1617. omap_hsmmc_start_command(host, req->sbc, NULL);
  1618. return;
  1619. }
  1620. omap_hsmmc_start_dma_transfer(host);
  1621. omap_hsmmc_start_command(host, req->cmd, req->data);
  1622. }
  1623. static void omap_hsmmc_set_timing(struct omap_hsmmc_host *host)
  1624. {
  1625. u32 val;
  1626. int ret;
  1627. struct pinctrl_state *pinctrl_state;
  1628. struct mmc_ios *ios = &host->mmc->ios;
  1629. omap_hsmmc_stop_clock(host);
  1630. val = OMAP_HSMMC_READ(host->base, AC12);
  1631. val &= ~AC12_UHSMC_MASK;
  1632. switch (ios->timing) {
  1633. case MMC_TIMING_UHS_SDR104:
  1634. val |= AC12_UHSMC_SDR104;
  1635. pinctrl_state = host->sdr104_pinctrl_state;
  1636. break;
  1637. case MMC_TIMING_MMC_HS200:
  1638. val |= AC12_UHSMC_SDR104;
  1639. pinctrl_state = host->hs200_1_8v_pinctrl_state;
  1640. break;
  1641. case MMC_TIMING_UHS_DDR50:
  1642. val |= AC12_UHSMC_DDR50;
  1643. pinctrl_state = host->ddr50_pinctrl_state;
  1644. break;
  1645. case MMC_TIMING_UHS_SDR50:
  1646. val |= AC12_UHSMC_SDR50;
  1647. pinctrl_state = host->sdr50_pinctrl_state;
  1648. break;
  1649. case MMC_TIMING_UHS_SDR25:
  1650. val |= AC12_UHSMC_SDR25;
  1651. pinctrl_state = host->sdr25_pinctrl_state;
  1652. break;
  1653. case MMC_TIMING_UHS_SDR12:
  1654. val |= AC12_UHSMC_SDR12;
  1655. pinctrl_state = host->sdr12_pinctrl_state;
  1656. break;
  1657. case MMC_TIMING_SD_HS:
  1658. case MMC_TIMING_MMC_HS:
  1659. val |= AC12_UHSMC_RES;
  1660. pinctrl_state = host->hs_pinctrl_state;
  1661. break;
  1662. case MMC_TIMING_MMC_DDR52:
  1663. val |= AC12_UHSMC_RES;
  1664. pinctrl_state = host->ddr_1_8v_pinctrl_state;
  1665. break;
  1666. default:
  1667. val |= AC12_UHSMC_RES;
  1668. pinctrl_state = host->default_pinctrl_state;
  1669. break;
  1670. }
  1671. OMAP_HSMMC_WRITE(host->base, AC12, val);
  1672. if (host->pdata->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY) {
  1673. ret = pinctrl_select_state(host->pinctrl, pinctrl_state);
  1674. if (ret) {
  1675. dev_err(mmc_dev(host->mmc),
  1676. "failed to select pinctrl state\n");
  1677. return;
  1678. }
  1679. host->pinctrl_state = pinctrl_state;
  1680. }
  1681. omap_hsmmc_start_clock(host);
  1682. }
  1683. /* Routine to configure clock values. Exposed API to core */
  1684. static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1685. {
  1686. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1687. int do_send_init_stream = 0;
  1688. if (ios->power_mode != host->power_mode) {
  1689. switch (ios->power_mode) {
  1690. case MMC_POWER_OFF:
  1691. omap_hsmmc_disable_tuning(host);
  1692. omap_hsmmc_set_power(host, 0, 0);
  1693. break;
  1694. case MMC_POWER_UP:
  1695. omap_hsmmc_set_power(host, 1, ios->vdd);
  1696. break;
  1697. case MMC_POWER_ON:
  1698. omap_hsmmc_conf_bus_power(host, ios->signal_voltage);
  1699. do_send_init_stream = 1;
  1700. break;
  1701. }
  1702. host->power_mode = ios->power_mode;
  1703. }
  1704. /* FIXME: set registers based only on changes to ios */
  1705. omap_hsmmc_set_bus_width(host);
  1706. omap_hsmmc_set_clock(host);
  1707. if (ios->timing != host->timing) {
  1708. omap_hsmmc_set_timing(host);
  1709. host->timing = ios->timing;
  1710. }
  1711. if (do_send_init_stream)
  1712. send_init_stream(host);
  1713. omap_hsmmc_set_bus_mode(host);
  1714. }
  1715. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  1716. {
  1717. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1718. if (!host->card_detect)
  1719. return -ENOSYS;
  1720. return host->card_detect(host->dev);
  1721. }
  1722. static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1723. {
  1724. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1725. if (mmc_pdata(host)->init_card)
  1726. mmc_pdata(host)->init_card(card);
  1727. }
  1728. static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1729. {
  1730. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1731. u32 irq_mask, con;
  1732. unsigned long flags;
  1733. spin_lock_irqsave(&host->irq_lock, flags);
  1734. con = OMAP_HSMMC_READ(host->base, CON);
  1735. irq_mask = OMAP_HSMMC_READ(host->base, ISE);
  1736. if (enable) {
  1737. host->flags |= HSMMC_SDIO_IRQ_ENABLED;
  1738. irq_mask |= CIRQ_EN;
  1739. con |= CTPL | CLKEXTFREE;
  1740. } else {
  1741. host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
  1742. irq_mask &= ~CIRQ_EN;
  1743. con &= ~(CTPL | CLKEXTFREE);
  1744. }
  1745. OMAP_HSMMC_WRITE(host->base, CON, con);
  1746. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  1747. /*
  1748. * if enable, piggy back detection on current request
  1749. * but always disable immediately
  1750. */
  1751. if (!host->req_in_progress || !enable)
  1752. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  1753. /* flush posted write */
  1754. OMAP_HSMMC_READ(host->base, IE);
  1755. spin_unlock_irqrestore(&host->irq_lock, flags);
  1756. }
  1757. static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
  1758. {
  1759. int ret;
  1760. /*
  1761. * For omaps with wake-up path, wakeirq will be irq from pinctrl and
  1762. * for other omaps, wakeirq will be from GPIO (dat line remuxed to
  1763. * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
  1764. * with functional clock disabled.
  1765. */
  1766. if (!host->dev->of_node || !host->wake_irq)
  1767. return -ENODEV;
  1768. ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
  1769. if (ret) {
  1770. dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
  1771. goto err;
  1772. }
  1773. /*
  1774. * Some omaps don't have wake-up path from deeper idle states
  1775. * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
  1776. */
  1777. if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
  1778. struct pinctrl *p = devm_pinctrl_get(host->dev);
  1779. if (!p) {
  1780. ret = -ENODEV;
  1781. goto err_free_irq;
  1782. }
  1783. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
  1784. dev_info(host->dev, "missing default pinctrl state\n");
  1785. devm_pinctrl_put(p);
  1786. ret = -EINVAL;
  1787. goto err_free_irq;
  1788. }
  1789. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
  1790. dev_info(host->dev, "missing idle pinctrl state\n");
  1791. devm_pinctrl_put(p);
  1792. ret = -EINVAL;
  1793. goto err_free_irq;
  1794. }
  1795. devm_pinctrl_put(p);
  1796. }
  1797. OMAP_HSMMC_WRITE(host->base, HCTL,
  1798. OMAP_HSMMC_READ(host->base, HCTL) | IWE);
  1799. return 0;
  1800. err_free_irq:
  1801. dev_pm_clear_wake_irq(host->dev);
  1802. err:
  1803. dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
  1804. host->wake_irq = 0;
  1805. return ret;
  1806. }
  1807. static void omap_hsmmc_set_capabilities(struct omap_hsmmc_host *host)
  1808. {
  1809. u32 val;
  1810. /* voltage capabilities might be set by boot loader, clear it */
  1811. val = OMAP_HSMMC_READ(host->base, CAPA) & ~VS_MASK;
  1812. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1813. if (host->io_3_3v_support)
  1814. val |= (VS33 | VS18);
  1815. else
  1816. val |= (VS30 | VS18);
  1817. } else if (host->pdata->controller_flags & OMAP_HSMMC_NO_1_8_V) {
  1818. if (host->io_3_3v_support)
  1819. val |= VS33;
  1820. else
  1821. val |= VS30;
  1822. } else {
  1823. val |= VS18;
  1824. }
  1825. OMAP_HSMMC_WRITE(host->base, CAPA, val);
  1826. }
  1827. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host, int iov)
  1828. {
  1829. u32 hctl = SDVS30, value;
  1830. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1831. if (iov == MMC_SIGNAL_VOLTAGE_180)
  1832. hctl = SDVS18;
  1833. else if (host->io_3_3v_support)
  1834. hctl = SDVS33;
  1835. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1836. /* Set SD bus power bit */
  1837. set_sd_bus_power(host);
  1838. }
  1839. static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
  1840. unsigned int direction, int blk_size)
  1841. {
  1842. /* This controller can't do multiblock reads due to hw bugs */
  1843. if (direction == MMC_DATA_READ)
  1844. return 1;
  1845. return blk_size;
  1846. }
  1847. static int omap_hsmmc_start_signal_voltage_switch(struct mmc_host *mmc,
  1848. struct mmc_ios *ios)
  1849. {
  1850. struct omap_hsmmc_host *host;
  1851. u32 val = 0;
  1852. int ret = 0;
  1853. u32 iov;
  1854. host = mmc_priv(mmc);
  1855. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  1856. val = OMAP_HSMMC_READ(host->base, CAPA);
  1857. if (!(val & (VS30 | VS33)))
  1858. return -EOPNOTSUPP;
  1859. if (val & VS33)
  1860. iov = VDD_33_34;
  1861. else
  1862. iov = VDD_30_31;
  1863. omap_hsmmc_conf_bus_power(host, ios->signal_voltage);
  1864. val = OMAP_HSMMC_READ(host->base, AC12);
  1865. val &= ~AC12_V1V8_SIGEN;
  1866. OMAP_HSMMC_WRITE(host->base, AC12, val);
  1867. ret = omap_hsmmc_set_power(host, 1, iov);
  1868. if (ret) {
  1869. dev_err(mmc_dev(host->mmc),
  1870. "failed to switch to %s\n",
  1871. host->io_3_3v_support ? "3.3V" : "3.0V");
  1872. return ret;
  1873. }
  1874. dev_dbg(mmc_dev(host->mmc),
  1875. "i/o voltage switched to %s\n",
  1876. host->io_3_3v_support ? "3.3V" : "3.0V");
  1877. } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
  1878. val = OMAP_HSMMC_READ(host->base, CAPA);
  1879. if (!(val & VS18))
  1880. return -EOPNOTSUPP;
  1881. omap_hsmmc_conf_bus_power(host, ios->signal_voltage);
  1882. val = OMAP_HSMMC_READ(host->base, AC12);
  1883. val |= AC12_V1V8_SIGEN;
  1884. OMAP_HSMMC_WRITE(host->base, AC12, val);
  1885. ret = omap_hsmmc_set_power(host, 1, VDD_165_195);
  1886. if (ret < 0) {
  1887. dev_err(mmc_dev(host->mmc), "failed to switch 1.8v\n");
  1888. return ret;
  1889. }
  1890. } else {
  1891. return -EOPNOTSUPP;
  1892. }
  1893. return 0;
  1894. }
  1895. static int omap_hsmmc_card_busy_low(struct omap_hsmmc_host *host)
  1896. {
  1897. int i;
  1898. u32 val;
  1899. val = OMAP_HSMMC_READ(host->base, CON);
  1900. val &= ~CON_CLKEXTFREE;
  1901. val |= CON_PADEN;
  1902. OMAP_HSMMC_WRITE(host->base, CON, val);
  1903. /* By observation, card busy status reflects in 100 - 200us */
  1904. for (i = 0; i < 5; i++) {
  1905. val = OMAP_HSMMC_READ(host->base, PSTATE);
  1906. if (!(val & (PSTATE_CLEV | PSTATE_DLEV)))
  1907. return true;
  1908. usleep_range(100, 200);
  1909. }
  1910. dev_err(mmc_dev(host->mmc), "card busy\n");
  1911. return false;
  1912. }
  1913. static int omap_hsmmc_card_busy_high(struct omap_hsmmc_host *host)
  1914. {
  1915. int i;
  1916. u32 val;
  1917. int ret = true;
  1918. val = OMAP_HSMMC_READ(host->base, CON);
  1919. val |= CLKEXTFREE;
  1920. OMAP_HSMMC_WRITE(host->base, CON, val);
  1921. host->flags |= CLKEXTFREE_ENABLED;
  1922. disable_irq(host->irq);
  1923. omap_hsmmc_enable_irq(host, NULL);
  1924. /* By observation, card busy status reflects in 100 - 200us */
  1925. for (i = 0; i < 5; i++) {
  1926. val = OMAP_HSMMC_READ(host->base, PSTATE);
  1927. if ((val & PSTATE_CLEV) && (val & PSTATE_DLEV)) {
  1928. val = OMAP_HSMMC_READ(host->base, CON);
  1929. val &= ~(CON_CLKEXTFREE | CON_PADEN);
  1930. OMAP_HSMMC_WRITE(host->base, CON, val);
  1931. ret = false;
  1932. goto disable_irq;
  1933. }
  1934. usleep_range(100, 200);
  1935. }
  1936. dev_err(mmc_dev(host->mmc), "card busy\n");
  1937. disable_irq:
  1938. omap_hsmmc_disable_irq(host);
  1939. enable_irq(host->irq);
  1940. host->flags &= ~CLKEXTFREE_ENABLED;
  1941. return ret;
  1942. }
  1943. static int omap_hsmmc_card_busy(struct mmc_host *mmc)
  1944. {
  1945. struct omap_hsmmc_host *host;
  1946. u32 val;
  1947. u32 reg;
  1948. int ret;
  1949. host = mmc_priv(mmc);
  1950. if (host->last_cmd != SD_SWITCH_VOLTAGE) {
  1951. /*
  1952. * PADEN should be set for DLEV to reflect the correct
  1953. * state of data lines atleast for MMC1 on AM57x.
  1954. */
  1955. reg = OMAP_HSMMC_READ(host->base, CON);
  1956. reg |= CON_PADEN;
  1957. OMAP_HSMMC_WRITE(host->base, CON, reg);
  1958. val = OMAP_HSMMC_READ(host->base, PSTATE);
  1959. reg &= ~CON_PADEN;
  1960. OMAP_HSMMC_WRITE(host->base, CON, reg);
  1961. if (val & PSTATE_DLEV_DAT0)
  1962. return false;
  1963. return true;
  1964. }
  1965. val = OMAP_HSMMC_READ(host->base, AC12);
  1966. if (val & AC12_V1V8_SIGEN)
  1967. ret = omap_hsmmc_card_busy_high(host);
  1968. else
  1969. ret = omap_hsmmc_card_busy_low(host);
  1970. return ret;
  1971. }
  1972. static inline void omap_hsmmc_set_dll(struct omap_hsmmc_host *host, int count)
  1973. {
  1974. int i;
  1975. u32 dll;
  1976. dll = OMAP_HSMMC_READ(host->base, DLL);
  1977. dll |= DLL_FORCE_VALUE;
  1978. dll &= ~(DLL_FORCE_SR_C_MASK << DLL_FORCE_SR_C_SHIFT);
  1979. dll |= (count << DLL_FORCE_SR_C_SHIFT);
  1980. OMAP_HSMMC_WRITE(host->base, DLL, dll);
  1981. dll |= DLL_CALIB;
  1982. OMAP_HSMMC_WRITE(host->base, DLL, dll);
  1983. for (i = 0; i < 1000; i++) {
  1984. if (OMAP_HSMMC_READ(host->base, DLL) & DLL_CALIB)
  1985. break;
  1986. }
  1987. dll &= ~DLL_CALIB;
  1988. OMAP_HSMMC_WRITE(host->base, DLL, dll);
  1989. }
  1990. static void omap_hsmmc_disable_tuning(struct omap_hsmmc_host *host)
  1991. {
  1992. int val;
  1993. val = OMAP_HSMMC_READ(host->base, AC12);
  1994. val &= ~(AC12_SCLK_SEL);
  1995. OMAP_HSMMC_WRITE(host->base, AC12, val);
  1996. val = OMAP_HSMMC_READ(host->base, DLL);
  1997. val &= ~(DLL_FORCE_VALUE | DLL_SWT);
  1998. OMAP_HSMMC_WRITE(host->base, DLL, val);
  1999. }
  2000. static int omap_hsmmc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  2001. {
  2002. u32 val;
  2003. u8 cur_match, prev_match = 0;
  2004. int ret;
  2005. u32 phase_delay = 0;
  2006. u32 start_window = 0, max_window = 0;
  2007. u32 length = 0, max_len = 0;
  2008. struct mmc_ios *ios = &mmc->ios;
  2009. struct omap_hsmmc_host *host;
  2010. struct thermal_zone_device *thermal_dev;
  2011. bool single_point_failure = false;
  2012. int temperature;
  2013. int i;
  2014. /* clock tuning is not needed for upto 52MHz */
  2015. if (ios->clock <= OMAP_MMC_MAX_CLOCK)
  2016. return 0;
  2017. host = mmc_priv(mmc);
  2018. thermal_dev = thermal_zone_get_zone_by_name("cpu_thermal");
  2019. if (IS_ERR(thermal_dev)) {
  2020. dev_err(mmc_dev(host->mmc),
  2021. "Unable to get thermal zone for tuning.%s",
  2022. !IS_ENABLED(CONFIG_THERMAL) ?
  2023. " Maybe CONFIG_THERMAL is not enabled?\n" : "\n");
  2024. ret = PTR_ERR(thermal_dev);
  2025. goto tuning_error;
  2026. }
  2027. ret = thermal_zone_get_temp(thermal_dev, &temperature);
  2028. if (ret)
  2029. goto tuning_error;
  2030. val = OMAP_HSMMC_READ(host->base, CAPA2);
  2031. if (ios->timing == MMC_TIMING_UHS_SDR50 && !(val & CAPA2_TSDR50))
  2032. return 0;
  2033. val = OMAP_HSMMC_READ(host->base, DLL);
  2034. val |= DLL_SWT;
  2035. OMAP_HSMMC_WRITE(host->base, DLL, val);
  2036. host->is_tuning = true;
  2037. /*
  2038. * Stage 1: Search for a maximum pass window ignoring any
  2039. * any single point failures. If the tuning value ends up
  2040. * near it, move away from it in stage 2 below
  2041. */
  2042. while (phase_delay <= MAX_PHASE_DELAY) {
  2043. omap_hsmmc_set_dll(host, phase_delay);
  2044. cur_match = !mmc_send_tuning(mmc, opcode, NULL);
  2045. if (cur_match) {
  2046. if (prev_match) {
  2047. length++;
  2048. } else if (single_point_failure) {
  2049. /* ignore single point failure */
  2050. length++;
  2051. single_point_failure = false;
  2052. } else {
  2053. start_window = phase_delay;
  2054. length = 1;
  2055. }
  2056. } else {
  2057. single_point_failure = prev_match;
  2058. }
  2059. if (length > max_len) {
  2060. max_window = start_window;
  2061. max_len = length;
  2062. }
  2063. prev_match = cur_match;
  2064. phase_delay += 4;
  2065. }
  2066. if (!max_len) {
  2067. dev_err(mmc_dev(host->mmc), "Unable to find match\n");
  2068. ret = -EIO;
  2069. goto tuning_error;
  2070. }
  2071. /*
  2072. * Assign tuning value as a ratio of maximum pass window based
  2073. * on temperature
  2074. */
  2075. if (temperature < -20000)
  2076. phase_delay = min(max_window + 4 * max_len - 24,
  2077. max_window +
  2078. DIV_ROUND_UP(13 * max_len, 16) * 4);
  2079. else if (temperature < 20000)
  2080. phase_delay = max_window + DIV_ROUND_UP(9 * max_len, 16) * 4;
  2081. else if (temperature < 40000)
  2082. phase_delay = max_window + DIV_ROUND_UP(8 * max_len, 16) * 4;
  2083. else if (temperature < 70000)
  2084. phase_delay = max_window + DIV_ROUND_UP(7 * max_len, 16) * 4;
  2085. else if (temperature < 90000)
  2086. phase_delay = max_window + DIV_ROUND_UP(5 * max_len, 16) * 4;
  2087. else if (temperature < 120000)
  2088. phase_delay = max_window + DIV_ROUND_UP(4 * max_len, 16) * 4;
  2089. else
  2090. phase_delay = max_window + DIV_ROUND_UP(3 * max_len, 16) * 4;
  2091. /*
  2092. * Stage 2: Search for a single point failure near the chosen tuning
  2093. * value in two steps. First in the +3 to +10 range and then in the
  2094. * +2 to -10 range. If found, move away from it in the appropriate
  2095. * direction by the appropriate amount depending on the temperature.
  2096. */
  2097. for (i = 3; i <= 10; i++) {
  2098. omap_hsmmc_set_dll(host, phase_delay + i);
  2099. if (mmc_send_tuning(mmc, opcode, NULL)) {
  2100. if (temperature < 10000)
  2101. phase_delay += i + 6;
  2102. else if (temperature < 20000)
  2103. phase_delay += i - 12;
  2104. else if (temperature < 70000)
  2105. phase_delay += i - 8;
  2106. else if (temperature < 90000)
  2107. phase_delay += i - 6;
  2108. else
  2109. phase_delay += i - 6;
  2110. goto single_failure_found;
  2111. }
  2112. }
  2113. for (i = 2; i >= -10; i--) {
  2114. omap_hsmmc_set_dll(host, phase_delay + i);
  2115. if (mmc_send_tuning(mmc, opcode, NULL)) {
  2116. if (temperature < 10000)
  2117. phase_delay += i + 12;
  2118. else if (temperature < 20000)
  2119. phase_delay += i + 8;
  2120. else if (temperature < 70000)
  2121. phase_delay += i + 8;
  2122. else if (temperature < 90000)
  2123. phase_delay += i + 10;
  2124. else
  2125. phase_delay += i + 12;
  2126. goto single_failure_found;
  2127. }
  2128. }
  2129. single_failure_found:
  2130. host->is_tuning = false;
  2131. val = OMAP_HSMMC_READ(host->base, AC12);
  2132. if (!(val & AC12_SCLK_SEL)) {
  2133. ret = -EIO;
  2134. goto tuning_error;
  2135. }
  2136. omap_hsmmc_set_dll(host, phase_delay);
  2137. omap_hsmmc_reset_controller_fsm(host, SRD);
  2138. omap_hsmmc_reset_controller_fsm(host, SRC);
  2139. return 0;
  2140. tuning_error:
  2141. dev_err(mmc_dev(host->mmc),
  2142. "Tuning failed. Using fixed sampling clock\n");
  2143. omap_hsmmc_disable_tuning(host);
  2144. omap_hsmmc_reset_controller_fsm(host, SRD);
  2145. omap_hsmmc_reset_controller_fsm(host, SRC);
  2146. return ret;
  2147. }
  2148. static struct mmc_host_ops omap_hsmmc_ops = {
  2149. .post_req = omap_hsmmc_post_req,
  2150. .pre_req = omap_hsmmc_pre_req,
  2151. .request = omap_hsmmc_request,
  2152. .set_ios = omap_hsmmc_set_ios,
  2153. .get_cd = omap_hsmmc_get_cd,
  2154. .get_ro = mmc_gpio_get_ro,
  2155. .init_card = omap_hsmmc_init_card,
  2156. .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
  2157. .start_signal_voltage_switch = omap_hsmmc_start_signal_voltage_switch,
  2158. .card_busy = omap_hsmmc_card_busy,
  2159. .execute_tuning = omap_hsmmc_execute_tuning,
  2160. };
  2161. #ifdef CONFIG_DEBUG_FS
  2162. static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
  2163. {
  2164. struct mmc_host *mmc = s->private;
  2165. struct omap_hsmmc_host *host = mmc_priv(mmc);
  2166. seq_printf(s, "mmc%d:\n", mmc->index);
  2167. seq_printf(s, "sdio irq mode\t%s\n",
  2168. (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
  2169. if (mmc->caps & MMC_CAP_SDIO_IRQ) {
  2170. seq_printf(s, "sdio irq \t%s\n",
  2171. (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
  2172. : "disabled");
  2173. }
  2174. seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
  2175. pm_runtime_get_sync(host->dev);
  2176. seq_puts(s, "\nregs:\n");
  2177. seq_printf(s, "CON:\t\t0x%08x\n",
  2178. OMAP_HSMMC_READ(host->base, CON));
  2179. seq_printf(s, "PSTATE:\t\t0x%08x\n",
  2180. OMAP_HSMMC_READ(host->base, PSTATE));
  2181. seq_printf(s, "HCTL:\t\t0x%08x\n",
  2182. OMAP_HSMMC_READ(host->base, HCTL));
  2183. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  2184. OMAP_HSMMC_READ(host->base, SYSCTL));
  2185. seq_printf(s, "IE:\t\t0x%08x\n",
  2186. OMAP_HSMMC_READ(host->base, IE));
  2187. seq_printf(s, "ISE:\t\t0x%08x\n",
  2188. OMAP_HSMMC_READ(host->base, ISE));
  2189. seq_printf(s, "CAPA:\t\t0x%08x\n",
  2190. OMAP_HSMMC_READ(host->base, CAPA));
  2191. pm_runtime_mark_last_busy(host->dev);
  2192. pm_runtime_put_autosuspend(host->dev);
  2193. return 0;
  2194. }
  2195. static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
  2196. {
  2197. return single_open(file, omap_hsmmc_regs_show, inode->i_private);
  2198. }
  2199. static const struct file_operations mmc_regs_fops = {
  2200. .open = omap_hsmmc_regs_open,
  2201. .read = seq_read,
  2202. .llseek = seq_lseek,
  2203. .release = single_release,
  2204. };
  2205. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  2206. {
  2207. if (mmc->debugfs_root)
  2208. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  2209. mmc, &mmc_regs_fops);
  2210. }
  2211. #else
  2212. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  2213. {
  2214. }
  2215. #endif
  2216. #ifdef CONFIG_OF
  2217. static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
  2218. /* See 35xx errata 2.1.1.128 in SPRZ278F */
  2219. .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
  2220. };
  2221. static const struct omap_mmc_of_data omap4_mmc_of_data = {
  2222. .reg_offset = 0x100,
  2223. .controller_flags = OMAP_HSMMC_HAS_HWPARAM,
  2224. };
  2225. static const struct omap_mmc_of_data am33xx_mmc_of_data = {
  2226. .reg_offset = 0x100,
  2227. .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
  2228. };
  2229. static const struct omap_mmc_of_data dra7_mmc_of_data = {
  2230. .reg_offset = 0x100,
  2231. .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING |
  2232. OMAP_HSMMC_REQUIRE_IODELAY |
  2233. OMAP_HSMMC_HAS_HWPARAM,
  2234. };
  2235. static const struct of_device_id omap_mmc_of_match[] = {
  2236. {
  2237. .compatible = "ti,omap2-hsmmc",
  2238. },
  2239. {
  2240. .compatible = "ti,omap3-pre-es3-hsmmc",
  2241. .data = &omap3_pre_es3_mmc_of_data,
  2242. },
  2243. {
  2244. .compatible = "ti,omap3-hsmmc",
  2245. },
  2246. {
  2247. .compatible = "ti,omap4-hsmmc",
  2248. .data = &omap4_mmc_of_data,
  2249. },
  2250. {
  2251. .compatible = "ti,am33xx-hsmmc",
  2252. .data = &am33xx_mmc_of_data,
  2253. },
  2254. {
  2255. .compatible = "ti,dra7-hsmmc",
  2256. .data = &dra7_mmc_of_data,
  2257. },
  2258. {},
  2259. };
  2260. MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
  2261. static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
  2262. {
  2263. struct omap_hsmmc_platform_data *pdata, *legacy;
  2264. struct device_node *np = dev->of_node;
  2265. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  2266. if (!pdata)
  2267. return ERR_PTR(-ENOMEM); /* out of memory */
  2268. legacy = dev_get_platdata(dev);
  2269. if (legacy) {
  2270. if (legacy->name)
  2271. pdata->name = legacy->name;
  2272. if (legacy->version)
  2273. pdata->version = legacy->version;
  2274. if (legacy->max_freq > 0)
  2275. pdata->max_freq = legacy->max_freq;
  2276. }
  2277. if (of_find_property(np, "ti,dual-volt", NULL))
  2278. pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
  2279. if (of_find_property(np, "no-1-8-v", NULL))
  2280. pdata->controller_flags |= OMAP_HSMMC_NO_1_8_V;
  2281. pdata->gpio_cd = -EINVAL;
  2282. pdata->gpio_cod = -EINVAL;
  2283. pdata->gpio_wp = -EINVAL;
  2284. if (of_find_property(np, "ti,non-removable", NULL)) {
  2285. pdata->nonremovable = true;
  2286. pdata->no_regulator_off_init = true;
  2287. }
  2288. if (of_find_property(np, "ti,needs-special-reset", NULL))
  2289. pdata->features |= HSMMC_HAS_UPDATED_RESET;
  2290. if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
  2291. pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
  2292. return pdata;
  2293. }
  2294. #else
  2295. static inline struct omap_hsmmc_platform_data
  2296. *of_get_hsmmc_pdata(struct device *dev)
  2297. {
  2298. return ERR_PTR(-EINVAL);
  2299. }
  2300. #endif
  2301. #define OMAP_HSMMC_SETUP_PINCTRL(capvar, capmask, mode) \
  2302. do { \
  2303. struct pinctrl_state *s = ERR_PTR(-ENODEV); \
  2304. char str[20]; \
  2305. char *version = host->pdata->version; \
  2306. \
  2307. if (!(mmc->capvar & (capmask))) \
  2308. break; \
  2309. \
  2310. if (host->pdata->version) { \
  2311. sprintf(str, "%s-%s", #mode, version); \
  2312. s = pinctrl_lookup_state(host->pinctrl, str); \
  2313. } \
  2314. \
  2315. if (IS_ERR(s)) { \
  2316. sprintf(str, "%s", #mode); \
  2317. s = pinctrl_lookup_state(host->pinctrl, str); \
  2318. } \
  2319. \
  2320. if (IS_ERR(s)) { \
  2321. dev_err(host->dev, "no pinctrl state for %s " \
  2322. "mode\n", #mode); \
  2323. mmc->capvar &= ~(capmask); \
  2324. } else { \
  2325. host->mode##_pinctrl_state = s; \
  2326. } \
  2327. \
  2328. } while (0)
  2329. static int omap_hsmmc_get_iodelay_pinctrl_state(struct omap_hsmmc_host *host)
  2330. {
  2331. struct mmc_host *mmc = host->mmc;
  2332. if (!(host->pdata->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY))
  2333. return 0;
  2334. host->pinctrl = devm_pinctrl_get(host->dev);
  2335. if (IS_ERR(host->pinctrl)) {
  2336. dev_err(host->dev, "Cannot get pinctrl\n");
  2337. return PTR_ERR(host->pinctrl);
  2338. }
  2339. host->default_pinctrl_state = pinctrl_lookup_state(host->pinctrl,
  2340. "default");
  2341. if (IS_ERR(host->default_pinctrl_state)) {
  2342. dev_err(host->dev,
  2343. "no pinctrl state for default mode\n");
  2344. return PTR_ERR(host->default_pinctrl_state);
  2345. }
  2346. OMAP_HSMMC_SETUP_PINCTRL(caps, MMC_CAP_UHS_SDR104, sdr104);
  2347. OMAP_HSMMC_SETUP_PINCTRL(caps, MMC_CAP_UHS_DDR50, ddr50);
  2348. OMAP_HSMMC_SETUP_PINCTRL(caps, MMC_CAP_UHS_SDR50, sdr50);
  2349. OMAP_HSMMC_SETUP_PINCTRL(caps, MMC_CAP_UHS_SDR25, sdr25);
  2350. OMAP_HSMMC_SETUP_PINCTRL(caps, MMC_CAP_UHS_SDR12, sdr12);
  2351. OMAP_HSMMC_SETUP_PINCTRL(caps, MMC_CAP_1_8V_DDR, ddr_1_8v);
  2352. OMAP_HSMMC_SETUP_PINCTRL(caps,
  2353. MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED,
  2354. hs);
  2355. OMAP_HSMMC_SETUP_PINCTRL(caps2, MMC_CAP2_HS200_1_8V_SDR,
  2356. hs200_1_8v);
  2357. host->pinctrl_state = host->default_pinctrl_state;
  2358. return 0;
  2359. }
  2360. static int omap_hsmmc_adma_init(struct omap_hsmmc_host *host)
  2361. {
  2362. struct mmc_host *mmc = host->mmc;
  2363. u32 val;
  2364. /*
  2365. * Allocate max_segs + 1 ('+ 1' to accommodate the NOP sentinel
  2366. * descriptor) for the ADMA descriptor table
  2367. */
  2368. host->adma_desc_table = dma_alloc_coherent(host->dev, ADMA_DESC_SIZE *
  2369. (mmc->max_segs + 1),
  2370. &host->adma_desc_table_addr,
  2371. GFP_KERNEL);
  2372. if (!host->adma_desc_table) {
  2373. dev_err(host->dev, "failed to allocate adma desc table\n");
  2374. return -ENOMEM;
  2375. }
  2376. val = OMAP_HSMMC_READ(host->base, HCTL);
  2377. val |= DMA_SELECT;
  2378. OMAP_HSMMC_WRITE(host->base, HCTL, val);
  2379. val = OMAP_HSMMC_READ(host->base, CON);
  2380. val |= DMA_MASTER;
  2381. OMAP_HSMMC_WRITE(host->base, CON, val);
  2382. return 0;
  2383. }
  2384. static void omap_hsmmc_adma_exit(struct omap_hsmmc_host *host)
  2385. {
  2386. struct mmc_host *mmc = host->mmc;
  2387. dma_free_coherent(host->dev, ADMA_DESC_SIZE * (mmc->max_segs + 1),
  2388. host->adma_desc_table, host->adma_desc_table_addr);
  2389. }
  2390. static int omap_hsmmc_dma_init(struct omap_hsmmc_host *host)
  2391. {
  2392. host->rx_chan = dma_request_chan(host->dev, "rx");
  2393. if (IS_ERR(host->rx_chan)) {
  2394. dev_err(mmc_dev(host->mmc), "RX DMA channel request failed\n");
  2395. return PTR_ERR(host->rx_chan);
  2396. }
  2397. host->tx_chan = dma_request_chan(host->dev, "tx");
  2398. if (IS_ERR(host->tx_chan)) {
  2399. dev_err(mmc_dev(host->mmc), "TX DMA channel request failed\n");
  2400. return PTR_ERR(host->tx_chan);
  2401. }
  2402. return 0;
  2403. }
  2404. static void omap_hsmmc_dma_exit(struct omap_hsmmc_host *host)
  2405. {
  2406. if (!IS_ERR_OR_NULL(host->tx_chan))
  2407. dma_release_channel(host->tx_chan);
  2408. if (!IS_ERR_OR_NULL(host->rx_chan))
  2409. dma_release_channel(host->rx_chan);
  2410. }
  2411. static int omap_hsmmc_probe(struct platform_device *pdev)
  2412. {
  2413. struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
  2414. struct mmc_host *mmc;
  2415. struct omap_hsmmc_host *host = NULL;
  2416. struct resource *res;
  2417. int ret, irq;
  2418. u32 val;
  2419. const struct of_device_id *match;
  2420. const struct omap_mmc_of_data *data;
  2421. void __iomem *base;
  2422. match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
  2423. if (match) {
  2424. pdata = of_get_hsmmc_pdata(&pdev->dev);
  2425. if (IS_ERR(pdata))
  2426. return PTR_ERR(pdata);
  2427. if (match->data) {
  2428. data = match->data;
  2429. pdata->reg_offset = data->reg_offset;
  2430. pdata->controller_flags |= data->controller_flags;
  2431. }
  2432. }
  2433. if (pdata == NULL) {
  2434. dev_err(&pdev->dev, "Platform Data is missing\n");
  2435. return -ENXIO;
  2436. }
  2437. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2438. irq = platform_get_irq(pdev, 0);
  2439. if (res == NULL || irq < 0)
  2440. return -ENXIO;
  2441. base = devm_ioremap_resource(&pdev->dev, res);
  2442. if (IS_ERR(base))
  2443. return PTR_ERR(base);
  2444. mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
  2445. if (!mmc) {
  2446. ret = -ENOMEM;
  2447. goto err;
  2448. }
  2449. ret = mmc_of_parse(mmc);
  2450. if (ret)
  2451. goto err1;
  2452. host = mmc_priv(mmc);
  2453. host->mmc = mmc;
  2454. host->pdata = pdata;
  2455. host->dev = &pdev->dev;
  2456. host->dma_ch = -1;
  2457. host->irq = irq;
  2458. host->mapbase = res->start + pdata->reg_offset;
  2459. host->base = base + pdata->reg_offset;
  2460. host->power_mode = MMC_POWER_OFF;
  2461. host->timing = 0;
  2462. host->next_data.cookie = 1;
  2463. host->pbias_enabled = 0;
  2464. host->vqmmc_enabled = 0;
  2465. host->use_adma = false;
  2466. ret = omap_hsmmc_gpio_init(mmc, host, pdata);
  2467. if (ret)
  2468. goto err_gpio;
  2469. platform_set_drvdata(pdev, host);
  2470. if (pdev->dev.of_node)
  2471. host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
  2472. mmc->ops = &omap_hsmmc_ops;
  2473. mmc->f_min = OMAP_MMC_MIN_CLOCK;
  2474. if (pdata->max_freq > 0)
  2475. mmc->f_max = pdata->max_freq;
  2476. else if (mmc->f_max == 0)
  2477. mmc->f_max = OMAP_MMC_MAX_CLOCK;
  2478. spin_lock_init(&host->irq_lock);
  2479. setup_timer(&host->timer, omap_hsmmc_soft_timeout,
  2480. (unsigned long)host);
  2481. host->fclk = devm_clk_get(&pdev->dev, "fck");
  2482. if (IS_ERR(host->fclk)) {
  2483. ret = PTR_ERR(host->fclk);
  2484. goto err1;
  2485. }
  2486. ret = clk_set_rate(host->fclk, mmc->f_max);
  2487. if (ret) {
  2488. dev_err(&pdev->dev, "failed to set clock to %d\n", mmc->f_max);
  2489. goto err1;
  2490. }
  2491. if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
  2492. dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
  2493. omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
  2494. }
  2495. device_init_wakeup(&pdev->dev, true);
  2496. pm_runtime_enable(host->dev);
  2497. pm_runtime_get_sync(host->dev);
  2498. pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
  2499. pm_runtime_use_autosuspend(host->dev);
  2500. omap_hsmmc_context_save(host);
  2501. host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
  2502. /*
  2503. * MMC can still work without debounce clock.
  2504. */
  2505. if (IS_ERR(host->dbclk)) {
  2506. host->dbclk = NULL;
  2507. } else if (clk_prepare_enable(host->dbclk) != 0) {
  2508. dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
  2509. host->dbclk = NULL;
  2510. }
  2511. if (host->pdata->controller_flags & OMAP_HSMMC_HAS_HWPARAM) {
  2512. val = OMAP_HSMMC_READ(base, HL_HWINFO);
  2513. if (val & MADMA_EN)
  2514. host->use_adma = true;
  2515. }
  2516. /* Since we do only SG emulation, we can have as many segs
  2517. * as we want. */
  2518. mmc->max_segs = 1024;
  2519. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  2520. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  2521. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  2522. if (host->use_adma)
  2523. mmc->max_seg_size = ADMA_MAX_LEN;
  2524. else
  2525. mmc->max_seg_size = mmc->max_req_size;
  2526. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  2527. MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
  2528. mmc->caps |= mmc_pdata(host)->caps;
  2529. if (mmc->caps & MMC_CAP_8_BIT_DATA)
  2530. mmc->caps |= MMC_CAP_4_BIT_DATA;
  2531. if (mmc_pdata(host)->nonremovable)
  2532. mmc->caps |= MMC_CAP_NONREMOVABLE;
  2533. mmc->pm_caps |= mmc_pdata(host)->pm_caps;
  2534. ret = omap_hsmmc_get_iodelay_pinctrl_state(host);
  2535. if (ret)
  2536. goto err_pinctrl;
  2537. if (host->use_adma)
  2538. ret = omap_hsmmc_adma_init(host);
  2539. else
  2540. ret = omap_hsmmc_dma_init(host);
  2541. if (ret)
  2542. goto err_irq;
  2543. /* Request IRQ for MMC operations */
  2544. ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
  2545. mmc_hostname(mmc), host);
  2546. if (ret) {
  2547. dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  2548. goto err_irq;
  2549. }
  2550. ret = omap_hsmmc_reg_get(host);
  2551. if (ret)
  2552. goto err_irq;
  2553. mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
  2554. /* Get pbias max-voltage and update host capabilities */
  2555. if (host->pbias &&
  2556. regulator_is_supported_voltage(host->pbias, VDD_3V3, VDD_3V3)) {
  2557. host->io_3_3v_support = true;
  2558. dev_dbg(mmc_dev(host->mmc), "PBIAS supports 3.3V not 3V\n");
  2559. }
  2560. omap_hsmmc_set_capabilities(host);
  2561. omap_hsmmc_disable_irq(host);
  2562. /*
  2563. * For now, only support SDIO interrupt if we have a separate
  2564. * wake-up interrupt configured from device tree. This is because
  2565. * the wake-up interrupt is needed for idle state and some
  2566. * platforms need special quirks. And we don't want to add new
  2567. * legacy mux platform init code callbacks any longer as we
  2568. * are moving to DT based booting anyways.
  2569. */
  2570. ret = omap_hsmmc_configure_wake_irq(host);
  2571. if (!ret)
  2572. mmc->caps |= MMC_CAP_SDIO_IRQ;
  2573. omap_hsmmc_protect_card(host);
  2574. mmc_add_host(mmc);
  2575. if (mmc_pdata(host)->name != NULL) {
  2576. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  2577. if (ret < 0)
  2578. goto err_slot_name;
  2579. }
  2580. if (host->get_cover_state) {
  2581. ret = device_create_file(&mmc->class_dev,
  2582. &dev_attr_cover_switch);
  2583. if (ret < 0)
  2584. goto err_slot_name;
  2585. }
  2586. omap_hsmmc_debugfs(mmc);
  2587. pm_runtime_mark_last_busy(host->dev);
  2588. pm_runtime_put_autosuspend(host->dev);
  2589. return 0;
  2590. err_slot_name:
  2591. mmc_remove_host(mmc);
  2592. err_irq:
  2593. if (host->use_adma)
  2594. omap_hsmmc_adma_exit(host);
  2595. else
  2596. omap_hsmmc_dma_exit(host);
  2597. err_pinctrl:
  2598. if (host->dbclk)
  2599. clk_disable_unprepare(host->dbclk);
  2600. pm_runtime_dont_use_autosuspend(host->dev);
  2601. pm_runtime_put_sync(host->dev);
  2602. pm_runtime_disable(host->dev);
  2603. device_init_wakeup(&pdev->dev, false);
  2604. err1:
  2605. err_gpio:
  2606. mmc_free_host(mmc);
  2607. err:
  2608. return ret;
  2609. }
  2610. static int omap_hsmmc_remove(struct platform_device *pdev)
  2611. {
  2612. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  2613. pm_runtime_get_sync(host->dev);
  2614. mmc_remove_host(host->mmc);
  2615. if (host->use_adma)
  2616. omap_hsmmc_adma_exit(host);
  2617. else
  2618. omap_hsmmc_dma_exit(host);
  2619. del_timer_sync(&host->timer);
  2620. pm_runtime_dont_use_autosuspend(host->dev);
  2621. pm_runtime_put_sync(host->dev);
  2622. pm_runtime_disable(host->dev);
  2623. device_init_wakeup(&pdev->dev, false);
  2624. if (host->dbclk)
  2625. clk_disable_unprepare(host->dbclk);
  2626. mmc_free_host(host->mmc);
  2627. return 0;
  2628. }
  2629. #ifdef CONFIG_PM_SLEEP
  2630. static int omap_hsmmc_suspend(struct device *dev)
  2631. {
  2632. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  2633. if (!host)
  2634. return 0;
  2635. pm_runtime_get_sync(host->dev);
  2636. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
  2637. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  2638. OMAP_HSMMC_WRITE(host->base, IE, 0);
  2639. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  2640. OMAP_HSMMC_WRITE(host->base, HCTL,
  2641. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  2642. }
  2643. if (host->dbclk)
  2644. clk_disable_unprepare(host->dbclk);
  2645. del_timer_sync(&host->timer);
  2646. pm_runtime_put_sync(host->dev);
  2647. return 0;
  2648. }
  2649. /* Routine to resume the MMC device */
  2650. static int omap_hsmmc_resume(struct device *dev)
  2651. {
  2652. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  2653. struct mmc_ios *ios;
  2654. if (!host)
  2655. return 0;
  2656. pm_runtime_get_sync(host->dev);
  2657. if (host->dbclk)
  2658. clk_prepare_enable(host->dbclk);
  2659. ios = &host->mmc->ios;
  2660. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
  2661. omap_hsmmc_conf_bus_power(host, ios->signal_voltage);
  2662. omap_hsmmc_protect_card(host);
  2663. pm_runtime_mark_last_busy(host->dev);
  2664. pm_runtime_put_autosuspend(host->dev);
  2665. return 0;
  2666. }
  2667. #endif
  2668. static int omap_hsmmc_runtime_suspend(struct device *dev)
  2669. {
  2670. struct omap_hsmmc_host *host;
  2671. unsigned long flags;
  2672. int ret = 0;
  2673. host = platform_get_drvdata(to_platform_device(dev));
  2674. omap_hsmmc_context_save(host);
  2675. dev_dbg(dev, "disabled\n");
  2676. spin_lock_irqsave(&host->irq_lock, flags);
  2677. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  2678. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  2679. /* disable sdio irq handling to prevent race */
  2680. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  2681. OMAP_HSMMC_WRITE(host->base, IE, 0);
  2682. if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
  2683. /*
  2684. * dat1 line low, pending sdio irq
  2685. * race condition: possible irq handler running on
  2686. * multi-core, abort
  2687. */
  2688. dev_dbg(dev, "pending sdio irq, abort suspend\n");
  2689. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  2690. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  2691. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  2692. pm_runtime_mark_last_busy(dev);
  2693. ret = -EBUSY;
  2694. goto abort;
  2695. }
  2696. pinctrl_pm_select_idle_state(dev);
  2697. } else {
  2698. pinctrl_pm_select_idle_state(dev);
  2699. }
  2700. abort:
  2701. spin_unlock_irqrestore(&host->irq_lock, flags);
  2702. return ret;
  2703. }
  2704. static int omap_hsmmc_runtime_resume(struct device *dev)
  2705. {
  2706. struct omap_hsmmc_host *host;
  2707. unsigned long flags;
  2708. int ret;
  2709. host = platform_get_drvdata(to_platform_device(dev));
  2710. omap_hsmmc_context_restore(host);
  2711. dev_dbg(dev, "enabled\n");
  2712. spin_lock_irqsave(&host->irq_lock, flags);
  2713. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  2714. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  2715. pinctrl_pm_select_default_state(host->dev);
  2716. /* irq lost, if pinmux incorrect */
  2717. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  2718. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  2719. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  2720. } else {
  2721. if (host->pinctrl) {
  2722. ret = pinctrl_select_state(host->pinctrl,
  2723. host->pinctrl_state);
  2724. if (ret)
  2725. dev_err(mmc_dev(host->mmc),
  2726. "failed to activate pinctrl state\n");
  2727. }
  2728. }
  2729. spin_unlock_irqrestore(&host->irq_lock, flags);
  2730. return 0;
  2731. }
  2732. static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
  2733. SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
  2734. .runtime_suspend = omap_hsmmc_runtime_suspend,
  2735. .runtime_resume = omap_hsmmc_runtime_resume,
  2736. };
  2737. static struct platform_driver omap_hsmmc_driver = {
  2738. .probe = omap_hsmmc_probe,
  2739. .remove = omap_hsmmc_remove,
  2740. .driver = {
  2741. .name = DRIVER_NAME,
  2742. .pm = &omap_hsmmc_dev_pm_ops,
  2743. .of_match_table = of_match_ptr(omap_mmc_of_match),
  2744. },
  2745. };
  2746. module_platform_driver(omap_hsmmc_driver);
  2747. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  2748. MODULE_LICENSE("GPL");
  2749. MODULE_ALIAS("platform:" DRIVER_NAME);
  2750. MODULE_AUTHOR("Texas Instruments Inc");