mmci.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
  4. *
  5. * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
  6. * Copyright (C) 2010 ST-Ericsson SA
  7. */
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/init.h>
  11. #include <linux/ioport.h>
  12. #include <linux/device.h>
  13. #include <linux/io.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/highmem.h>
  20. #include <linux/log2.h>
  21. #include <linux/mmc/mmc.h>
  22. #include <linux/mmc/pm.h>
  23. #include <linux/mmc/host.h>
  24. #include <linux/mmc/card.h>
  25. #include <linux/mmc/sd.h>
  26. #include <linux/mmc/slot-gpio.h>
  27. #include <linux/amba/bus.h>
  28. #include <linux/clk.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/of.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/dmaengine.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/amba/mmci.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/types.h>
  37. #include <linux/pinctrl/consumer.h>
  38. #include <linux/reset.h>
  39. #include <linux/gpio/consumer.h>
  40. #include <asm/div64.h>
  41. #include <asm/io.h>
  42. #include "mmci.h"
  43. #define DRIVER_NAME "mmci-pl18x"
  44. static void mmci_variant_init(struct mmci_host *host);
  45. static void ux500_variant_init(struct mmci_host *host);
  46. static void ux500v2_variant_init(struct mmci_host *host);
  47. static unsigned int fmax = 515633;
  48. static struct variant_data variant_arm = {
  49. .fifosize = 16 * 4,
  50. .fifohalfsize = 8 * 4,
  51. .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
  52. .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
  53. .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
  54. .cmdreg_srsp = MCI_CPSM_RESPONSE,
  55. .datalength_bits = 16,
  56. .datactrl_blocksz = 11,
  57. .pwrreg_powerup = MCI_PWR_UP,
  58. .f_max = 100000000,
  59. .reversed_irq_handling = true,
  60. .mmcimask1 = true,
  61. .irq_pio_mask = MCI_IRQ_PIO_MASK,
  62. .start_err = MCI_STARTBITERR,
  63. .opendrain = MCI_ROD,
  64. .init = mmci_variant_init,
  65. };
  66. static struct variant_data variant_arm_extended_fifo = {
  67. .fifosize = 128 * 4,
  68. .fifohalfsize = 64 * 4,
  69. .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
  70. .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
  71. .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
  72. .cmdreg_srsp = MCI_CPSM_RESPONSE,
  73. .datalength_bits = 16,
  74. .datactrl_blocksz = 11,
  75. .pwrreg_powerup = MCI_PWR_UP,
  76. .f_max = 100000000,
  77. .mmcimask1 = true,
  78. .irq_pio_mask = MCI_IRQ_PIO_MASK,
  79. .start_err = MCI_STARTBITERR,
  80. .opendrain = MCI_ROD,
  81. .init = mmci_variant_init,
  82. };
  83. static struct variant_data variant_arm_extended_fifo_hwfc = {
  84. .fifosize = 128 * 4,
  85. .fifohalfsize = 64 * 4,
  86. .clkreg_enable = MCI_ARM_HWFCEN,
  87. .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
  88. .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
  89. .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
  90. .cmdreg_srsp = MCI_CPSM_RESPONSE,
  91. .datalength_bits = 16,
  92. .datactrl_blocksz = 11,
  93. .pwrreg_powerup = MCI_PWR_UP,
  94. .f_max = 100000000,
  95. .mmcimask1 = true,
  96. .irq_pio_mask = MCI_IRQ_PIO_MASK,
  97. .start_err = MCI_STARTBITERR,
  98. .opendrain = MCI_ROD,
  99. .init = mmci_variant_init,
  100. };
  101. static struct variant_data variant_u300 = {
  102. .fifosize = 16 * 4,
  103. .fifohalfsize = 8 * 4,
  104. .clkreg_enable = MCI_ST_U300_HWFCEN,
  105. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  106. .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
  107. .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
  108. .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
  109. .cmdreg_srsp = MCI_CPSM_RESPONSE,
  110. .datalength_bits = 16,
  111. .datactrl_blocksz = 11,
  112. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  113. .st_sdio = true,
  114. .pwrreg_powerup = MCI_PWR_ON,
  115. .f_max = 100000000,
  116. .signal_direction = true,
  117. .pwrreg_clkgate = true,
  118. .pwrreg_nopower = true,
  119. .mmcimask1 = true,
  120. .irq_pio_mask = MCI_IRQ_PIO_MASK,
  121. .start_err = MCI_STARTBITERR,
  122. .opendrain = MCI_OD,
  123. .init = mmci_variant_init,
  124. };
  125. static struct variant_data variant_nomadik = {
  126. .fifosize = 16 * 4,
  127. .fifohalfsize = 8 * 4,
  128. .clkreg = MCI_CLK_ENABLE,
  129. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  130. .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
  131. .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
  132. .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
  133. .cmdreg_srsp = MCI_CPSM_RESPONSE,
  134. .datalength_bits = 24,
  135. .datactrl_blocksz = 11,
  136. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  137. .st_sdio = true,
  138. .st_clkdiv = true,
  139. .pwrreg_powerup = MCI_PWR_ON,
  140. .f_max = 100000000,
  141. .signal_direction = true,
  142. .pwrreg_clkgate = true,
  143. .pwrreg_nopower = true,
  144. .mmcimask1 = true,
  145. .irq_pio_mask = MCI_IRQ_PIO_MASK,
  146. .start_err = MCI_STARTBITERR,
  147. .opendrain = MCI_OD,
  148. .init = mmci_variant_init,
  149. };
  150. static struct variant_data variant_ux500 = {
  151. .fifosize = 30 * 4,
  152. .fifohalfsize = 8 * 4,
  153. .clkreg = MCI_CLK_ENABLE,
  154. .clkreg_enable = MCI_ST_UX500_HWFCEN,
  155. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  156. .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
  157. .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
  158. .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
  159. .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
  160. .cmdreg_srsp = MCI_CPSM_RESPONSE,
  161. .datalength_bits = 24,
  162. .datactrl_blocksz = 11,
  163. .datactrl_any_blocksz = true,
  164. .dma_power_of_2 = true,
  165. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  166. .st_sdio = true,
  167. .st_clkdiv = true,
  168. .pwrreg_powerup = MCI_PWR_ON,
  169. .f_max = 100000000,
  170. .signal_direction = true,
  171. .pwrreg_clkgate = true,
  172. .busy_detect = true,
  173. .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
  174. .busy_detect_flag = MCI_ST_CARDBUSY,
  175. .busy_detect_mask = MCI_ST_BUSYENDMASK,
  176. .pwrreg_nopower = true,
  177. .mmcimask1 = true,
  178. .irq_pio_mask = MCI_IRQ_PIO_MASK,
  179. .start_err = MCI_STARTBITERR,
  180. .opendrain = MCI_OD,
  181. .init = ux500_variant_init,
  182. };
  183. static struct variant_data variant_ux500v2 = {
  184. .fifosize = 30 * 4,
  185. .fifohalfsize = 8 * 4,
  186. .clkreg = MCI_CLK_ENABLE,
  187. .clkreg_enable = MCI_ST_UX500_HWFCEN,
  188. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  189. .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
  190. .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
  191. .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
  192. .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
  193. .cmdreg_srsp = MCI_CPSM_RESPONSE,
  194. .datactrl_mask_ddrmode = MCI_DPSM_ST_DDRMODE,
  195. .datalength_bits = 24,
  196. .datactrl_blocksz = 11,
  197. .datactrl_any_blocksz = true,
  198. .dma_power_of_2 = true,
  199. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  200. .st_sdio = true,
  201. .st_clkdiv = true,
  202. .pwrreg_powerup = MCI_PWR_ON,
  203. .f_max = 100000000,
  204. .signal_direction = true,
  205. .pwrreg_clkgate = true,
  206. .busy_detect = true,
  207. .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
  208. .busy_detect_flag = MCI_ST_CARDBUSY,
  209. .busy_detect_mask = MCI_ST_BUSYENDMASK,
  210. .pwrreg_nopower = true,
  211. .mmcimask1 = true,
  212. .irq_pio_mask = MCI_IRQ_PIO_MASK,
  213. .start_err = MCI_STARTBITERR,
  214. .opendrain = MCI_OD,
  215. .init = ux500v2_variant_init,
  216. };
  217. static struct variant_data variant_stm32 = {
  218. .fifosize = 32 * 4,
  219. .fifohalfsize = 8 * 4,
  220. .clkreg = MCI_CLK_ENABLE,
  221. .clkreg_enable = MCI_ST_UX500_HWFCEN,
  222. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  223. .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
  224. .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
  225. .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
  226. .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
  227. .cmdreg_srsp = MCI_CPSM_RESPONSE,
  228. .irq_pio_mask = MCI_IRQ_PIO_MASK,
  229. .datalength_bits = 24,
  230. .datactrl_blocksz = 11,
  231. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  232. .st_sdio = true,
  233. .st_clkdiv = true,
  234. .pwrreg_powerup = MCI_PWR_ON,
  235. .f_max = 48000000,
  236. .pwrreg_clkgate = true,
  237. .pwrreg_nopower = true,
  238. .init = mmci_variant_init,
  239. };
  240. static struct variant_data variant_stm32_sdmmc = {
  241. .fifosize = 16 * 4,
  242. .fifohalfsize = 8 * 4,
  243. .f_max = 208000000,
  244. .stm32_clkdiv = true,
  245. .cmdreg_cpsm_enable = MCI_CPSM_STM32_ENABLE,
  246. .cmdreg_lrsp_crc = MCI_CPSM_STM32_LRSP_CRC,
  247. .cmdreg_srsp_crc = MCI_CPSM_STM32_SRSP_CRC,
  248. .cmdreg_srsp = MCI_CPSM_STM32_SRSP,
  249. .cmdreg_stop = MCI_CPSM_STM32_CMDSTOP,
  250. .data_cmd_enable = MCI_CPSM_STM32_CMDTRANS,
  251. .irq_pio_mask = MCI_IRQ_PIO_STM32_MASK,
  252. .datactrl_first = true,
  253. .datacnt_useless = true,
  254. .datalength_bits = 25,
  255. .datactrl_blocksz = 14,
  256. .datactrl_any_blocksz = true,
  257. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  258. .stm32_idmabsize_mask = GENMASK(12, 5),
  259. .busy_timeout = true,
  260. .busy_detect = true,
  261. .busy_detect_flag = MCI_STM32_BUSYD0,
  262. .busy_detect_mask = MCI_STM32_BUSYD0ENDMASK,
  263. .init = sdmmc_variant_init,
  264. };
  265. static struct variant_data variant_stm32_sdmmcv2 = {
  266. .fifosize = 16 * 4,
  267. .fifohalfsize = 8 * 4,
  268. .f_max = 267000000,
  269. .stm32_clkdiv = true,
  270. .cmdreg_cpsm_enable = MCI_CPSM_STM32_ENABLE,
  271. .cmdreg_lrsp_crc = MCI_CPSM_STM32_LRSP_CRC,
  272. .cmdreg_srsp_crc = MCI_CPSM_STM32_SRSP_CRC,
  273. .cmdreg_srsp = MCI_CPSM_STM32_SRSP,
  274. .cmdreg_stop = MCI_CPSM_STM32_CMDSTOP,
  275. .data_cmd_enable = MCI_CPSM_STM32_CMDTRANS,
  276. .irq_pio_mask = MCI_IRQ_PIO_STM32_MASK,
  277. .datactrl_first = true,
  278. .datacnt_useless = true,
  279. .datalength_bits = 25,
  280. .datactrl_blocksz = 14,
  281. .datactrl_any_blocksz = true,
  282. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  283. .stm32_idmabsize_mask = GENMASK(16, 5),
  284. .dma_lli = true,
  285. .busy_timeout = true,
  286. .busy_detect = true,
  287. .busy_detect_flag = MCI_STM32_BUSYD0,
  288. .busy_detect_mask = MCI_STM32_BUSYD0ENDMASK,
  289. .init = sdmmc_variant_init,
  290. };
  291. static struct variant_data variant_qcom = {
  292. .fifosize = 16 * 4,
  293. .fifohalfsize = 8 * 4,
  294. .clkreg = MCI_CLK_ENABLE,
  295. .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
  296. MCI_QCOM_CLK_SELECT_IN_FBCLK,
  297. .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
  298. .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
  299. .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
  300. .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
  301. .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
  302. .cmdreg_srsp = MCI_CPSM_RESPONSE,
  303. .data_cmd_enable = MCI_CPSM_QCOM_DATCMD,
  304. .datalength_bits = 24,
  305. .datactrl_blocksz = 11,
  306. .datactrl_any_blocksz = true,
  307. .pwrreg_powerup = MCI_PWR_UP,
  308. .f_max = 208000000,
  309. .explicit_mclk_control = true,
  310. .qcom_fifo = true,
  311. .qcom_dml = true,
  312. .mmcimask1 = true,
  313. .irq_pio_mask = MCI_IRQ_PIO_MASK,
  314. .start_err = MCI_STARTBITERR,
  315. .opendrain = MCI_ROD,
  316. .init = qcom_variant_init,
  317. };
  318. /* Busy detection for the ST Micro variant */
  319. static int mmci_card_busy(struct mmc_host *mmc)
  320. {
  321. struct mmci_host *host = mmc_priv(mmc);
  322. unsigned long flags;
  323. int busy = 0;
  324. spin_lock_irqsave(&host->lock, flags);
  325. if (readl(host->base + MMCISTATUS) & host->variant->busy_detect_flag)
  326. busy = 1;
  327. spin_unlock_irqrestore(&host->lock, flags);
  328. return busy;
  329. }
  330. static void mmci_reg_delay(struct mmci_host *host)
  331. {
  332. /*
  333. * According to the spec, at least three feedback clock cycles
  334. * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
  335. * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
  336. * Worst delay time during card init is at 100 kHz => 30 us.
  337. * Worst delay time when up and running is at 25 MHz => 120 ns.
  338. */
  339. if (host->cclk < 25000000)
  340. udelay(30);
  341. else
  342. ndelay(120);
  343. }
  344. /*
  345. * This must be called with host->lock held
  346. */
  347. void mmci_write_clkreg(struct mmci_host *host, u32 clk)
  348. {
  349. if (host->clk_reg != clk) {
  350. host->clk_reg = clk;
  351. writel(clk, host->base + MMCICLOCK);
  352. }
  353. }
  354. /*
  355. * This must be called with host->lock held
  356. */
  357. void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
  358. {
  359. if (host->pwr_reg != pwr) {
  360. host->pwr_reg = pwr;
  361. writel(pwr, host->base + MMCIPOWER);
  362. }
  363. }
  364. /*
  365. * This must be called with host->lock held
  366. */
  367. static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
  368. {
  369. /* Keep busy mode in DPSM if enabled */
  370. datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
  371. if (host->datactrl_reg != datactrl) {
  372. host->datactrl_reg = datactrl;
  373. writel(datactrl, host->base + MMCIDATACTRL);
  374. }
  375. }
  376. /*
  377. * This must be called with host->lock held
  378. */
  379. static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
  380. {
  381. struct variant_data *variant = host->variant;
  382. u32 clk = variant->clkreg;
  383. /* Make sure cclk reflects the current calculated clock */
  384. host->cclk = 0;
  385. if (desired) {
  386. if (variant->explicit_mclk_control) {
  387. host->cclk = host->mclk;
  388. } else if (desired >= host->mclk) {
  389. clk = MCI_CLK_BYPASS;
  390. if (variant->st_clkdiv)
  391. clk |= MCI_ST_UX500_NEG_EDGE;
  392. host->cclk = host->mclk;
  393. } else if (variant->st_clkdiv) {
  394. /*
  395. * DB8500 TRM says f = mclk / (clkdiv + 2)
  396. * => clkdiv = (mclk / f) - 2
  397. * Round the divider up so we don't exceed the max
  398. * frequency
  399. */
  400. clk = DIV_ROUND_UP(host->mclk, desired) - 2;
  401. if (clk >= 256)
  402. clk = 255;
  403. host->cclk = host->mclk / (clk + 2);
  404. } else {
  405. /*
  406. * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
  407. * => clkdiv = mclk / (2 * f) - 1
  408. */
  409. clk = host->mclk / (2 * desired) - 1;
  410. if (clk >= 256)
  411. clk = 255;
  412. host->cclk = host->mclk / (2 * (clk + 1));
  413. }
  414. clk |= variant->clkreg_enable;
  415. clk |= MCI_CLK_ENABLE;
  416. /* This hasn't proven to be worthwhile */
  417. /* clk |= MCI_CLK_PWRSAVE; */
  418. }
  419. /* Set actual clock for debug */
  420. host->mmc->actual_clock = host->cclk;
  421. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  422. clk |= MCI_4BIT_BUS;
  423. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  424. clk |= variant->clkreg_8bit_bus_enable;
  425. if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
  426. host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
  427. clk |= variant->clkreg_neg_edge_enable;
  428. mmci_write_clkreg(host, clk);
  429. }
  430. static void mmci_dma_release(struct mmci_host *host)
  431. {
  432. if (host->ops && host->ops->dma_release)
  433. host->ops->dma_release(host);
  434. host->use_dma = false;
  435. }
  436. static void mmci_dma_setup(struct mmci_host *host)
  437. {
  438. if (!host->ops || !host->ops->dma_setup)
  439. return;
  440. if (host->ops->dma_setup(host))
  441. return;
  442. /* initialize pre request cookie */
  443. host->next_cookie = 1;
  444. host->use_dma = true;
  445. }
  446. /*
  447. * Validate mmc prerequisites
  448. */
  449. static int mmci_validate_data(struct mmci_host *host,
  450. struct mmc_data *data)
  451. {
  452. struct variant_data *variant = host->variant;
  453. if (!data)
  454. return 0;
  455. if (!is_power_of_2(data->blksz) && !variant->datactrl_any_blocksz) {
  456. dev_err(mmc_dev(host->mmc),
  457. "unsupported block size (%d bytes)\n", data->blksz);
  458. return -EINVAL;
  459. }
  460. if (host->ops && host->ops->validate_data)
  461. return host->ops->validate_data(host, data);
  462. return 0;
  463. }
  464. static int mmci_prep_data(struct mmci_host *host, struct mmc_data *data, bool next)
  465. {
  466. int err;
  467. if (!host->ops || !host->ops->prep_data)
  468. return 0;
  469. err = host->ops->prep_data(host, data, next);
  470. if (next && !err)
  471. data->host_cookie = ++host->next_cookie < 0 ?
  472. 1 : host->next_cookie;
  473. return err;
  474. }
  475. static void mmci_unprep_data(struct mmci_host *host, struct mmc_data *data,
  476. int err)
  477. {
  478. if (host->ops && host->ops->unprep_data)
  479. host->ops->unprep_data(host, data, err);
  480. data->host_cookie = 0;
  481. }
  482. static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
  483. {
  484. WARN_ON(data->host_cookie && data->host_cookie != host->next_cookie);
  485. if (host->ops && host->ops->get_next_data)
  486. host->ops->get_next_data(host, data);
  487. }
  488. static int mmci_dma_start(struct mmci_host *host, unsigned int datactrl)
  489. {
  490. struct mmc_data *data = host->data;
  491. int ret;
  492. if (!host->use_dma)
  493. return -EINVAL;
  494. ret = mmci_prep_data(host, data, false);
  495. if (ret)
  496. return ret;
  497. if (!host->ops || !host->ops->dma_start)
  498. return -EINVAL;
  499. /* Okay, go for it. */
  500. dev_vdbg(mmc_dev(host->mmc),
  501. "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
  502. data->sg_len, data->blksz, data->blocks, data->flags);
  503. ret = host->ops->dma_start(host, &datactrl);
  504. if (ret)
  505. return ret;
  506. /* Trigger the DMA transfer */
  507. mmci_write_datactrlreg(host, datactrl);
  508. /*
  509. * Let the MMCI say when the data is ended and it's time
  510. * to fire next DMA request. When that happens, MMCI will
  511. * call mmci_data_end()
  512. */
  513. writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
  514. host->base + MMCIMASK0);
  515. return 0;
  516. }
  517. static void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
  518. {
  519. if (!host->use_dma)
  520. return;
  521. if (host->ops && host->ops->dma_finalize)
  522. host->ops->dma_finalize(host, data);
  523. }
  524. static void mmci_dma_error(struct mmci_host *host)
  525. {
  526. if (!host->use_dma)
  527. return;
  528. if (host->ops && host->ops->dma_error)
  529. host->ops->dma_error(host);
  530. }
  531. static void
  532. mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
  533. {
  534. writel(0, host->base + MMCICOMMAND);
  535. BUG_ON(host->data);
  536. host->mrq = NULL;
  537. host->cmd = NULL;
  538. mmc_request_done(host->mmc, mrq);
  539. }
  540. static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
  541. {
  542. void __iomem *base = host->base;
  543. struct variant_data *variant = host->variant;
  544. if (host->singleirq) {
  545. unsigned int mask0 = readl(base + MMCIMASK0);
  546. mask0 &= ~variant->irq_pio_mask;
  547. mask0 |= mask;
  548. writel(mask0, base + MMCIMASK0);
  549. }
  550. if (variant->mmcimask1)
  551. writel(mask, base + MMCIMASK1);
  552. host->mask1_reg = mask;
  553. }
  554. static void mmci_stop_data(struct mmci_host *host)
  555. {
  556. mmci_write_datactrlreg(host, 0);
  557. mmci_set_mask1(host, 0);
  558. host->data = NULL;
  559. }
  560. static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
  561. {
  562. unsigned int flags = SG_MITER_ATOMIC;
  563. if (data->flags & MMC_DATA_READ)
  564. flags |= SG_MITER_TO_SG;
  565. else
  566. flags |= SG_MITER_FROM_SG;
  567. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  568. }
  569. static u32 mmci_get_dctrl_cfg(struct mmci_host *host)
  570. {
  571. return MCI_DPSM_ENABLE | mmci_dctrl_blksz(host);
  572. }
  573. static u32 ux500v2_get_dctrl_cfg(struct mmci_host *host)
  574. {
  575. return MCI_DPSM_ENABLE | (host->data->blksz << 16);
  576. }
  577. static bool ux500_busy_complete(struct mmci_host *host, u32 status, u32 err_msk)
  578. {
  579. void __iomem *base = host->base;
  580. /*
  581. * Before unmasking for the busy end IRQ, confirm that the
  582. * command was sent successfully. To keep track of having a
  583. * command in-progress, waiting for busy signaling to end,
  584. * store the status in host->busy_status.
  585. *
  586. * Note that, the card may need a couple of clock cycles before
  587. * it starts signaling busy on DAT0, hence re-read the
  588. * MMCISTATUS register here, to allow the busy bit to be set.
  589. * Potentially we may even need to poll the register for a
  590. * while, to allow it to be set, but tests indicates that it
  591. * isn't needed.
  592. */
  593. if (!host->busy_status && !(status & err_msk) &&
  594. (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
  595. writel(readl(base + MMCIMASK0) |
  596. host->variant->busy_detect_mask,
  597. base + MMCIMASK0);
  598. host->busy_status = status & (MCI_CMDSENT | MCI_CMDRESPEND);
  599. return false;
  600. }
  601. /*
  602. * If there is a command in-progress that has been successfully
  603. * sent, then bail out if busy status is set and wait for the
  604. * busy end IRQ.
  605. *
  606. * Note that, the HW triggers an IRQ on both edges while
  607. * monitoring DAT0 for busy completion, but there is only one
  608. * status bit in MMCISTATUS for the busy state. Therefore
  609. * both the start and the end interrupts needs to be cleared,
  610. * one after the other. So, clear the busy start IRQ here.
  611. */
  612. if (host->busy_status &&
  613. (status & host->variant->busy_detect_flag)) {
  614. writel(host->variant->busy_detect_mask, base + MMCICLEAR);
  615. return false;
  616. }
  617. /*
  618. * If there is a command in-progress that has been successfully
  619. * sent and the busy bit isn't set, it means we have received
  620. * the busy end IRQ. Clear and mask the IRQ, then continue to
  621. * process the command.
  622. */
  623. if (host->busy_status) {
  624. writel(host->variant->busy_detect_mask, base + MMCICLEAR);
  625. writel(readl(base + MMCIMASK0) &
  626. ~host->variant->busy_detect_mask, base + MMCIMASK0);
  627. host->busy_status = 0;
  628. }
  629. return true;
  630. }
  631. /*
  632. * All the DMA operation mode stuff goes inside this ifdef.
  633. * This assumes that you have a generic DMA device interface,
  634. * no custom DMA interfaces are supported.
  635. */
  636. #ifdef CONFIG_DMA_ENGINE
  637. struct mmci_dmae_next {
  638. struct dma_async_tx_descriptor *desc;
  639. struct dma_chan *chan;
  640. };
  641. struct mmci_dmae_priv {
  642. struct dma_chan *cur;
  643. struct dma_chan *rx_channel;
  644. struct dma_chan *tx_channel;
  645. struct dma_async_tx_descriptor *desc_current;
  646. struct mmci_dmae_next next_data;
  647. };
  648. int mmci_dmae_setup(struct mmci_host *host)
  649. {
  650. const char *rxname, *txname;
  651. struct mmci_dmae_priv *dmae;
  652. dmae = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dmae), GFP_KERNEL);
  653. if (!dmae)
  654. return -ENOMEM;
  655. host->dma_priv = dmae;
  656. dmae->rx_channel = dma_request_chan(mmc_dev(host->mmc), "rx");
  657. if (IS_ERR(dmae->rx_channel)) {
  658. int ret = PTR_ERR(dmae->rx_channel);
  659. dmae->rx_channel = NULL;
  660. return ret;
  661. }
  662. dmae->tx_channel = dma_request_chan(mmc_dev(host->mmc), "tx");
  663. if (IS_ERR(dmae->tx_channel)) {
  664. if (PTR_ERR(dmae->tx_channel) == -EPROBE_DEFER)
  665. dev_warn(mmc_dev(host->mmc),
  666. "Deferred probe for TX channel ignored\n");
  667. dmae->tx_channel = NULL;
  668. }
  669. /*
  670. * If only an RX channel is specified, the driver will
  671. * attempt to use it bidirectionally, however if it
  672. * is specified but cannot be located, DMA will be disabled.
  673. */
  674. if (dmae->rx_channel && !dmae->tx_channel)
  675. dmae->tx_channel = dmae->rx_channel;
  676. if (dmae->rx_channel)
  677. rxname = dma_chan_name(dmae->rx_channel);
  678. else
  679. rxname = "none";
  680. if (dmae->tx_channel)
  681. txname = dma_chan_name(dmae->tx_channel);
  682. else
  683. txname = "none";
  684. dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
  685. rxname, txname);
  686. /*
  687. * Limit the maximum segment size in any SG entry according to
  688. * the parameters of the DMA engine device.
  689. */
  690. if (dmae->tx_channel) {
  691. struct device *dev = dmae->tx_channel->device->dev;
  692. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  693. if (max_seg_size < host->mmc->max_seg_size)
  694. host->mmc->max_seg_size = max_seg_size;
  695. }
  696. if (dmae->rx_channel) {
  697. struct device *dev = dmae->rx_channel->device->dev;
  698. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  699. if (max_seg_size < host->mmc->max_seg_size)
  700. host->mmc->max_seg_size = max_seg_size;
  701. }
  702. if (!dmae->tx_channel || !dmae->rx_channel) {
  703. mmci_dmae_release(host);
  704. return -EINVAL;
  705. }
  706. return 0;
  707. }
  708. /*
  709. * This is used in or so inline it
  710. * so it can be discarded.
  711. */
  712. void mmci_dmae_release(struct mmci_host *host)
  713. {
  714. struct mmci_dmae_priv *dmae = host->dma_priv;
  715. if (dmae->rx_channel)
  716. dma_release_channel(dmae->rx_channel);
  717. if (dmae->tx_channel)
  718. dma_release_channel(dmae->tx_channel);
  719. dmae->rx_channel = dmae->tx_channel = NULL;
  720. }
  721. static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
  722. {
  723. struct mmci_dmae_priv *dmae = host->dma_priv;
  724. struct dma_chan *chan;
  725. if (data->flags & MMC_DATA_READ)
  726. chan = dmae->rx_channel;
  727. else
  728. chan = dmae->tx_channel;
  729. dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
  730. mmc_get_dma_dir(data));
  731. }
  732. void mmci_dmae_error(struct mmci_host *host)
  733. {
  734. struct mmci_dmae_priv *dmae = host->dma_priv;
  735. if (!dma_inprogress(host))
  736. return;
  737. dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
  738. dmaengine_terminate_all(dmae->cur);
  739. host->dma_in_progress = false;
  740. dmae->cur = NULL;
  741. dmae->desc_current = NULL;
  742. host->data->host_cookie = 0;
  743. mmci_dma_unmap(host, host->data);
  744. }
  745. void mmci_dmae_finalize(struct mmci_host *host, struct mmc_data *data)
  746. {
  747. struct mmci_dmae_priv *dmae = host->dma_priv;
  748. u32 status;
  749. int i;
  750. if (!dma_inprogress(host))
  751. return;
  752. /* Wait up to 1ms for the DMA to complete */
  753. for (i = 0; ; i++) {
  754. status = readl(host->base + MMCISTATUS);
  755. if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
  756. break;
  757. udelay(10);
  758. }
  759. /*
  760. * Check to see whether we still have some data left in the FIFO -
  761. * this catches DMA controllers which are unable to monitor the
  762. * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
  763. * contiguous buffers. On TX, we'll get a FIFO underrun error.
  764. */
  765. if (status & MCI_RXDATAAVLBLMASK) {
  766. mmci_dma_error(host);
  767. if (!data->error)
  768. data->error = -EIO;
  769. } else if (!data->host_cookie) {
  770. mmci_dma_unmap(host, data);
  771. }
  772. /*
  773. * Use of DMA with scatter-gather is impossible.
  774. * Give up with DMA and switch back to PIO mode.
  775. */
  776. if (status & MCI_RXDATAAVLBLMASK) {
  777. dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
  778. mmci_dma_release(host);
  779. }
  780. host->dma_in_progress = false;
  781. dmae->cur = NULL;
  782. dmae->desc_current = NULL;
  783. }
  784. /* prepares DMA channel and DMA descriptor, returns non-zero on failure */
  785. static int _mmci_dmae_prep_data(struct mmci_host *host, struct mmc_data *data,
  786. struct dma_chan **dma_chan,
  787. struct dma_async_tx_descriptor **dma_desc)
  788. {
  789. struct mmci_dmae_priv *dmae = host->dma_priv;
  790. struct variant_data *variant = host->variant;
  791. struct dma_slave_config conf = {
  792. .src_addr = host->phybase + MMCIFIFO,
  793. .dst_addr = host->phybase + MMCIFIFO,
  794. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  795. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  796. .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
  797. .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
  798. .device_fc = false,
  799. };
  800. struct dma_chan *chan;
  801. struct dma_device *device;
  802. struct dma_async_tx_descriptor *desc;
  803. int nr_sg;
  804. unsigned long flags = DMA_CTRL_ACK;
  805. if (data->flags & MMC_DATA_READ) {
  806. conf.direction = DMA_DEV_TO_MEM;
  807. chan = dmae->rx_channel;
  808. } else {
  809. conf.direction = DMA_MEM_TO_DEV;
  810. chan = dmae->tx_channel;
  811. }
  812. /* If there's no DMA channel, fall back to PIO */
  813. if (!chan)
  814. return -EINVAL;
  815. /* If less than or equal to the fifo size, don't bother with DMA */
  816. if (data->blksz * data->blocks <= variant->fifosize)
  817. return -EINVAL;
  818. /*
  819. * This is necessary to get SDIO working on the Ux500. We do not yet
  820. * know if this is a bug in:
  821. * - The Ux500 DMA controller (DMA40)
  822. * - The MMCI DMA interface on the Ux500
  823. * some power of two blocks (such as 64 bytes) are sent regularly
  824. * during SDIO traffic and those work fine so for these we enable DMA
  825. * transfers.
  826. */
  827. if (host->variant->dma_power_of_2 && !is_power_of_2(data->blksz))
  828. return -EINVAL;
  829. device = chan->device;
  830. nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
  831. mmc_get_dma_dir(data));
  832. if (nr_sg == 0)
  833. return -EINVAL;
  834. if (host->variant->qcom_dml)
  835. flags |= DMA_PREP_INTERRUPT;
  836. dmaengine_slave_config(chan, &conf);
  837. desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
  838. conf.direction, flags);
  839. if (!desc)
  840. goto unmap_exit;
  841. *dma_chan = chan;
  842. *dma_desc = desc;
  843. return 0;
  844. unmap_exit:
  845. dma_unmap_sg(device->dev, data->sg, data->sg_len,
  846. mmc_get_dma_dir(data));
  847. return -ENOMEM;
  848. }
  849. int mmci_dmae_prep_data(struct mmci_host *host,
  850. struct mmc_data *data,
  851. bool next)
  852. {
  853. struct mmci_dmae_priv *dmae = host->dma_priv;
  854. struct mmci_dmae_next *nd = &dmae->next_data;
  855. if (!host->use_dma)
  856. return -EINVAL;
  857. if (next)
  858. return _mmci_dmae_prep_data(host, data, &nd->chan, &nd->desc);
  859. /* Check if next job is already prepared. */
  860. if (dmae->cur && dmae->desc_current)
  861. return 0;
  862. /* No job were prepared thus do it now. */
  863. return _mmci_dmae_prep_data(host, data, &dmae->cur,
  864. &dmae->desc_current);
  865. }
  866. int mmci_dmae_start(struct mmci_host *host, unsigned int *datactrl)
  867. {
  868. struct mmci_dmae_priv *dmae = host->dma_priv;
  869. int ret;
  870. host->dma_in_progress = true;
  871. ret = dma_submit_error(dmaengine_submit(dmae->desc_current));
  872. if (ret < 0) {
  873. host->dma_in_progress = false;
  874. return ret;
  875. }
  876. dma_async_issue_pending(dmae->cur);
  877. *datactrl |= MCI_DPSM_DMAENABLE;
  878. return 0;
  879. }
  880. void mmci_dmae_get_next_data(struct mmci_host *host, struct mmc_data *data)
  881. {
  882. struct mmci_dmae_priv *dmae = host->dma_priv;
  883. struct mmci_dmae_next *next = &dmae->next_data;
  884. if (!host->use_dma)
  885. return;
  886. WARN_ON(!data->host_cookie && (next->desc || next->chan));
  887. dmae->desc_current = next->desc;
  888. dmae->cur = next->chan;
  889. next->desc = NULL;
  890. next->chan = NULL;
  891. }
  892. void mmci_dmae_unprep_data(struct mmci_host *host,
  893. struct mmc_data *data, int err)
  894. {
  895. struct mmci_dmae_priv *dmae = host->dma_priv;
  896. if (!host->use_dma)
  897. return;
  898. mmci_dma_unmap(host, data);
  899. if (err) {
  900. struct mmci_dmae_next *next = &dmae->next_data;
  901. struct dma_chan *chan;
  902. if (data->flags & MMC_DATA_READ)
  903. chan = dmae->rx_channel;
  904. else
  905. chan = dmae->tx_channel;
  906. dmaengine_terminate_all(chan);
  907. if (dmae->desc_current == next->desc)
  908. dmae->desc_current = NULL;
  909. if (dmae->cur == next->chan) {
  910. host->dma_in_progress = false;
  911. dmae->cur = NULL;
  912. }
  913. next->desc = NULL;
  914. next->chan = NULL;
  915. }
  916. }
  917. static struct mmci_host_ops mmci_variant_ops = {
  918. .prep_data = mmci_dmae_prep_data,
  919. .unprep_data = mmci_dmae_unprep_data,
  920. .get_datactrl_cfg = mmci_get_dctrl_cfg,
  921. .get_next_data = mmci_dmae_get_next_data,
  922. .dma_setup = mmci_dmae_setup,
  923. .dma_release = mmci_dmae_release,
  924. .dma_start = mmci_dmae_start,
  925. .dma_finalize = mmci_dmae_finalize,
  926. .dma_error = mmci_dmae_error,
  927. };
  928. #else
  929. static struct mmci_host_ops mmci_variant_ops = {
  930. .get_datactrl_cfg = mmci_get_dctrl_cfg,
  931. };
  932. #endif
  933. static void mmci_variant_init(struct mmci_host *host)
  934. {
  935. host->ops = &mmci_variant_ops;
  936. }
  937. static void ux500_variant_init(struct mmci_host *host)
  938. {
  939. host->ops = &mmci_variant_ops;
  940. host->ops->busy_complete = ux500_busy_complete;
  941. }
  942. static void ux500v2_variant_init(struct mmci_host *host)
  943. {
  944. host->ops = &mmci_variant_ops;
  945. host->ops->busy_complete = ux500_busy_complete;
  946. host->ops->get_datactrl_cfg = ux500v2_get_dctrl_cfg;
  947. }
  948. static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
  949. {
  950. struct mmci_host *host = mmc_priv(mmc);
  951. struct mmc_data *data = mrq->data;
  952. if (!data)
  953. return;
  954. WARN_ON(data->host_cookie);
  955. if (mmci_validate_data(host, data))
  956. return;
  957. mmci_prep_data(host, data, true);
  958. }
  959. static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
  960. int err)
  961. {
  962. struct mmci_host *host = mmc_priv(mmc);
  963. struct mmc_data *data = mrq->data;
  964. if (!data || !data->host_cookie)
  965. return;
  966. mmci_unprep_data(host, data, err);
  967. }
  968. static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
  969. {
  970. struct variant_data *variant = host->variant;
  971. unsigned int datactrl, timeout, irqmask;
  972. unsigned long long clks;
  973. void __iomem *base;
  974. dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
  975. data->blksz, data->blocks, data->flags);
  976. host->data = data;
  977. host->size = data->blksz * data->blocks;
  978. data->bytes_xfered = 0;
  979. clks = (unsigned long long)data->timeout_ns * host->cclk;
  980. do_div(clks, NSEC_PER_SEC);
  981. timeout = data->timeout_clks + (unsigned int)clks;
  982. base = host->base;
  983. writel(timeout, base + MMCIDATATIMER);
  984. writel(host->size, base + MMCIDATALENGTH);
  985. datactrl = host->ops->get_datactrl_cfg(host);
  986. datactrl |= host->data->flags & MMC_DATA_READ ? MCI_DPSM_DIRECTION : 0;
  987. if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
  988. u32 clk;
  989. datactrl |= variant->datactrl_mask_sdio;
  990. /*
  991. * The ST Micro variant for SDIO small write transfers
  992. * needs to have clock H/W flow control disabled,
  993. * otherwise the transfer will not start. The threshold
  994. * depends on the rate of MCLK.
  995. */
  996. if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
  997. (host->size < 8 ||
  998. (host->size <= 8 && host->mclk > 50000000)))
  999. clk = host->clk_reg & ~variant->clkreg_enable;
  1000. else
  1001. clk = host->clk_reg | variant->clkreg_enable;
  1002. mmci_write_clkreg(host, clk);
  1003. }
  1004. if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
  1005. host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
  1006. datactrl |= variant->datactrl_mask_ddrmode;
  1007. /*
  1008. * Attempt to use DMA operation mode, if this
  1009. * should fail, fall back to PIO mode
  1010. */
  1011. if (!mmci_dma_start(host, datactrl))
  1012. return;
  1013. /* IRQ mode, map the SG list for CPU reading/writing */
  1014. mmci_init_sg(host, data);
  1015. if (data->flags & MMC_DATA_READ) {
  1016. irqmask = MCI_RXFIFOHALFFULLMASK;
  1017. /*
  1018. * If we have less than the fifo 'half-full' threshold to
  1019. * transfer, trigger a PIO interrupt as soon as any data
  1020. * is available.
  1021. */
  1022. if (host->size < variant->fifohalfsize)
  1023. irqmask |= MCI_RXDATAAVLBLMASK;
  1024. } else {
  1025. /*
  1026. * We don't actually need to include "FIFO empty" here
  1027. * since its implicit in "FIFO half empty".
  1028. */
  1029. irqmask = MCI_TXFIFOHALFEMPTYMASK;
  1030. }
  1031. mmci_write_datactrlreg(host, datactrl);
  1032. writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
  1033. mmci_set_mask1(host, irqmask);
  1034. }
  1035. static void
  1036. mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
  1037. {
  1038. void __iomem *base = host->base;
  1039. unsigned long long clks;
  1040. dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
  1041. cmd->opcode, cmd->arg, cmd->flags);
  1042. if (readl(base + MMCICOMMAND) & host->variant->cmdreg_cpsm_enable) {
  1043. writel(0, base + MMCICOMMAND);
  1044. mmci_reg_delay(host);
  1045. }
  1046. if (host->variant->cmdreg_stop &&
  1047. cmd->opcode == MMC_STOP_TRANSMISSION)
  1048. c |= host->variant->cmdreg_stop;
  1049. c |= cmd->opcode | host->variant->cmdreg_cpsm_enable;
  1050. if (cmd->flags & MMC_RSP_PRESENT) {
  1051. if (cmd->flags & MMC_RSP_136)
  1052. c |= host->variant->cmdreg_lrsp_crc;
  1053. else if (cmd->flags & MMC_RSP_CRC)
  1054. c |= host->variant->cmdreg_srsp_crc;
  1055. else
  1056. c |= host->variant->cmdreg_srsp;
  1057. }
  1058. if (host->variant->busy_timeout && cmd->flags & MMC_RSP_BUSY) {
  1059. if (!cmd->busy_timeout)
  1060. cmd->busy_timeout = 10 * MSEC_PER_SEC;
  1061. if (cmd->busy_timeout > host->mmc->max_busy_timeout)
  1062. clks = (unsigned long long)host->mmc->max_busy_timeout * host->cclk;
  1063. else
  1064. clks = (unsigned long long)cmd->busy_timeout * host->cclk;
  1065. do_div(clks, MSEC_PER_SEC);
  1066. writel_relaxed(clks, host->base + MMCIDATATIMER);
  1067. }
  1068. if (host->ops->pre_sig_volt_switch && cmd->opcode == SD_SWITCH_VOLTAGE)
  1069. host->ops->pre_sig_volt_switch(host);
  1070. if (/*interrupt*/0)
  1071. c |= MCI_CPSM_INTERRUPT;
  1072. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
  1073. c |= host->variant->data_cmd_enable;
  1074. host->cmd = cmd;
  1075. writel(cmd->arg, base + MMCIARGUMENT);
  1076. writel(c, base + MMCICOMMAND);
  1077. }
  1078. static void mmci_stop_command(struct mmci_host *host)
  1079. {
  1080. host->stop_abort.error = 0;
  1081. mmci_start_command(host, &host->stop_abort, 0);
  1082. }
  1083. static void
  1084. mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
  1085. unsigned int status)
  1086. {
  1087. unsigned int status_err;
  1088. /* Make sure we have data to handle */
  1089. if (!data)
  1090. return;
  1091. /* First check for errors */
  1092. status_err = status & (host->variant->start_err |
  1093. MCI_DATACRCFAIL | MCI_DATATIMEOUT |
  1094. MCI_TXUNDERRUN | MCI_RXOVERRUN);
  1095. if (status_err) {
  1096. u32 remain, success;
  1097. /* Terminate the DMA transfer */
  1098. mmci_dma_error(host);
  1099. /*
  1100. * Calculate how far we are into the transfer. Note that
  1101. * the data counter gives the number of bytes transferred
  1102. * on the MMC bus, not on the host side. On reads, this
  1103. * can be as much as a FIFO-worth of data ahead. This
  1104. * matters for FIFO overruns only.
  1105. */
  1106. if (!host->variant->datacnt_useless) {
  1107. remain = readl(host->base + MMCIDATACNT);
  1108. success = data->blksz * data->blocks - remain;
  1109. } else {
  1110. success = 0;
  1111. }
  1112. dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
  1113. status_err, success);
  1114. if (status_err & MCI_DATACRCFAIL) {
  1115. /* Last block was not successful */
  1116. success -= 1;
  1117. data->error = -EILSEQ;
  1118. } else if (status_err & MCI_DATATIMEOUT) {
  1119. data->error = -ETIMEDOUT;
  1120. } else if (status_err & MCI_STARTBITERR) {
  1121. data->error = -ECOMM;
  1122. } else if (status_err & MCI_TXUNDERRUN) {
  1123. data->error = -EIO;
  1124. } else if (status_err & MCI_RXOVERRUN) {
  1125. if (success > host->variant->fifosize)
  1126. success -= host->variant->fifosize;
  1127. else
  1128. success = 0;
  1129. data->error = -EIO;
  1130. }
  1131. data->bytes_xfered = round_down(success, data->blksz);
  1132. }
  1133. if (status & MCI_DATABLOCKEND)
  1134. dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
  1135. if (status & MCI_DATAEND || data->error) {
  1136. mmci_dma_finalize(host, data);
  1137. mmci_stop_data(host);
  1138. if (!data->error)
  1139. /* The error clause is handled above, success! */
  1140. data->bytes_xfered = data->blksz * data->blocks;
  1141. if (!data->stop) {
  1142. if (host->variant->cmdreg_stop && data->error)
  1143. mmci_stop_command(host);
  1144. else
  1145. mmci_request_end(host, data->mrq);
  1146. } else if (host->mrq->sbc && !data->error) {
  1147. mmci_request_end(host, data->mrq);
  1148. } else {
  1149. mmci_start_command(host, data->stop, 0);
  1150. }
  1151. }
  1152. }
  1153. static void
  1154. mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
  1155. unsigned int status)
  1156. {
  1157. u32 err_msk = MCI_CMDCRCFAIL | MCI_CMDTIMEOUT;
  1158. void __iomem *base = host->base;
  1159. bool sbc, busy_resp;
  1160. if (!cmd)
  1161. return;
  1162. sbc = (cmd == host->mrq->sbc);
  1163. busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
  1164. /*
  1165. * We need to be one of these interrupts to be considered worth
  1166. * handling. Note that we tag on any latent IRQs postponed
  1167. * due to waiting for busy status.
  1168. */
  1169. if (host->variant->busy_timeout && busy_resp)
  1170. err_msk |= MCI_DATATIMEOUT;
  1171. if (!((status | host->busy_status) &
  1172. (err_msk | MCI_CMDSENT | MCI_CMDRESPEND)))
  1173. return;
  1174. /* Handle busy detection on DAT0 if the variant supports it. */
  1175. if (busy_resp && host->variant->busy_detect)
  1176. if (!host->ops->busy_complete(host, status, err_msk))
  1177. return;
  1178. host->cmd = NULL;
  1179. if (status & MCI_CMDTIMEOUT) {
  1180. cmd->error = -ETIMEDOUT;
  1181. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  1182. cmd->error = -EILSEQ;
  1183. } else if (host->variant->busy_timeout && busy_resp &&
  1184. status & MCI_DATATIMEOUT) {
  1185. cmd->error = -ETIMEDOUT;
  1186. /*
  1187. * This will wake up mmci_irq_thread() which will issue
  1188. * a hardware reset of the MMCI block.
  1189. */
  1190. host->irq_action = IRQ_WAKE_THREAD;
  1191. } else {
  1192. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  1193. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  1194. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  1195. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  1196. }
  1197. if ((!sbc && !cmd->data) || cmd->error) {
  1198. if (host->data) {
  1199. /* Terminate the DMA transfer */
  1200. mmci_dma_error(host);
  1201. mmci_stop_data(host);
  1202. if (host->variant->cmdreg_stop && cmd->error) {
  1203. mmci_stop_command(host);
  1204. return;
  1205. }
  1206. }
  1207. if (host->irq_action != IRQ_WAKE_THREAD)
  1208. mmci_request_end(host, host->mrq);
  1209. } else if (sbc) {
  1210. mmci_start_command(host, host->mrq->cmd, 0);
  1211. } else if (!host->variant->datactrl_first &&
  1212. !(cmd->data->flags & MMC_DATA_READ)) {
  1213. mmci_start_data(host, cmd->data);
  1214. }
  1215. }
  1216. static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
  1217. {
  1218. return remain - (readl(host->base + MMCIFIFOCNT) << 2);
  1219. }
  1220. static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
  1221. {
  1222. /*
  1223. * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
  1224. * from the fifo range should be used
  1225. */
  1226. if (status & MCI_RXFIFOHALFFULL)
  1227. return host->variant->fifohalfsize;
  1228. else if (status & MCI_RXDATAAVLBL)
  1229. return 4;
  1230. return 0;
  1231. }
  1232. static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
  1233. {
  1234. void __iomem *base = host->base;
  1235. char *ptr = buffer;
  1236. u32 status = readl(host->base + MMCISTATUS);
  1237. int host_remain = host->size;
  1238. do {
  1239. int count = host->get_rx_fifocnt(host, status, host_remain);
  1240. if (count > remain)
  1241. count = remain;
  1242. if (count <= 0)
  1243. break;
  1244. /*
  1245. * SDIO especially may want to send something that is
  1246. * not divisible by 4 (as opposed to card sectors
  1247. * etc). Therefore make sure to always read the last bytes
  1248. * while only doing full 32-bit reads towards the FIFO.
  1249. */
  1250. if (unlikely(count & 0x3)) {
  1251. if (count < 4) {
  1252. unsigned char buf[4];
  1253. ioread32_rep(base + MMCIFIFO, buf, 1);
  1254. memcpy(ptr, buf, count);
  1255. } else {
  1256. ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
  1257. count &= ~0x3;
  1258. }
  1259. } else {
  1260. ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
  1261. }
  1262. ptr += count;
  1263. remain -= count;
  1264. host_remain -= count;
  1265. if (remain == 0)
  1266. break;
  1267. status = readl(base + MMCISTATUS);
  1268. } while (status & MCI_RXDATAAVLBL);
  1269. return ptr - buffer;
  1270. }
  1271. static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
  1272. {
  1273. struct variant_data *variant = host->variant;
  1274. void __iomem *base = host->base;
  1275. char *ptr = buffer;
  1276. do {
  1277. unsigned int count, maxcnt;
  1278. maxcnt = status & MCI_TXFIFOEMPTY ?
  1279. variant->fifosize : variant->fifohalfsize;
  1280. count = min(remain, maxcnt);
  1281. /*
  1282. * SDIO especially may want to send something that is
  1283. * not divisible by 4 (as opposed to card sectors
  1284. * etc), and the FIFO only accept full 32-bit writes.
  1285. * So compensate by adding +3 on the count, a single
  1286. * byte become a 32bit write, 7 bytes will be two
  1287. * 32bit writes etc.
  1288. */
  1289. iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
  1290. ptr += count;
  1291. remain -= count;
  1292. if (remain == 0)
  1293. break;
  1294. status = readl(base + MMCISTATUS);
  1295. } while (status & MCI_TXFIFOHALFEMPTY);
  1296. return ptr - buffer;
  1297. }
  1298. /*
  1299. * PIO data transfer IRQ handler.
  1300. */
  1301. static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  1302. {
  1303. struct mmci_host *host = dev_id;
  1304. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1305. struct variant_data *variant = host->variant;
  1306. void __iomem *base = host->base;
  1307. u32 status;
  1308. status = readl(base + MMCISTATUS);
  1309. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  1310. do {
  1311. unsigned int remain, len;
  1312. char *buffer;
  1313. /*
  1314. * For write, we only need to test the half-empty flag
  1315. * here - if the FIFO is completely empty, then by
  1316. * definition it is more than half empty.
  1317. *
  1318. * For read, check for data available.
  1319. */
  1320. if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
  1321. break;
  1322. if (!sg_miter_next(sg_miter))
  1323. break;
  1324. buffer = sg_miter->addr;
  1325. remain = sg_miter->length;
  1326. len = 0;
  1327. if (status & MCI_RXACTIVE)
  1328. len = mmci_pio_read(host, buffer, remain);
  1329. if (status & MCI_TXACTIVE)
  1330. len = mmci_pio_write(host, buffer, remain, status);
  1331. sg_miter->consumed = len;
  1332. host->size -= len;
  1333. remain -= len;
  1334. if (remain)
  1335. break;
  1336. status = readl(base + MMCISTATUS);
  1337. } while (1);
  1338. sg_miter_stop(sg_miter);
  1339. /*
  1340. * If we have less than the fifo 'half-full' threshold to transfer,
  1341. * trigger a PIO interrupt as soon as any data is available.
  1342. */
  1343. if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
  1344. mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
  1345. /*
  1346. * If we run out of data, disable the data IRQs; this
  1347. * prevents a race where the FIFO becomes empty before
  1348. * the chip itself has disabled the data path, and
  1349. * stops us racing with our data end IRQ.
  1350. */
  1351. if (host->size == 0) {
  1352. mmci_set_mask1(host, 0);
  1353. writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
  1354. }
  1355. return IRQ_HANDLED;
  1356. }
  1357. /*
  1358. * Handle completion of command and data transfers.
  1359. */
  1360. static irqreturn_t mmci_irq(int irq, void *dev_id)
  1361. {
  1362. struct mmci_host *host = dev_id;
  1363. u32 status;
  1364. spin_lock(&host->lock);
  1365. host->irq_action = IRQ_HANDLED;
  1366. do {
  1367. status = readl(host->base + MMCISTATUS);
  1368. if (!status)
  1369. break;
  1370. if (host->singleirq) {
  1371. if (status & host->mask1_reg)
  1372. mmci_pio_irq(irq, dev_id);
  1373. status &= ~host->variant->irq_pio_mask;
  1374. }
  1375. /*
  1376. * Busy detection is managed by mmci_cmd_irq(), including to
  1377. * clear the corresponding IRQ.
  1378. */
  1379. status &= readl(host->base + MMCIMASK0);
  1380. if (host->variant->busy_detect)
  1381. writel(status & ~host->variant->busy_detect_mask,
  1382. host->base + MMCICLEAR);
  1383. else
  1384. writel(status, host->base + MMCICLEAR);
  1385. dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
  1386. if (host->variant->reversed_irq_handling) {
  1387. mmci_data_irq(host, host->data, status);
  1388. mmci_cmd_irq(host, host->cmd, status);
  1389. } else {
  1390. mmci_cmd_irq(host, host->cmd, status);
  1391. mmci_data_irq(host, host->data, status);
  1392. }
  1393. /*
  1394. * Busy detection has been handled by mmci_cmd_irq() above.
  1395. * Clear the status bit to prevent polling in IRQ context.
  1396. */
  1397. if (host->variant->busy_detect_flag)
  1398. status &= ~host->variant->busy_detect_flag;
  1399. } while (status);
  1400. spin_unlock(&host->lock);
  1401. return host->irq_action;
  1402. }
  1403. /*
  1404. * mmci_irq_thread() - A threaded IRQ handler that manages a reset of the HW.
  1405. *
  1406. * A reset is needed for some variants, where a datatimeout for a R1B request
  1407. * causes the DPSM to stay busy (non-functional).
  1408. */
  1409. static irqreturn_t mmci_irq_thread(int irq, void *dev_id)
  1410. {
  1411. struct mmci_host *host = dev_id;
  1412. unsigned long flags;
  1413. if (host->rst) {
  1414. reset_control_assert(host->rst);
  1415. udelay(2);
  1416. reset_control_deassert(host->rst);
  1417. }
  1418. spin_lock_irqsave(&host->lock, flags);
  1419. writel(host->clk_reg, host->base + MMCICLOCK);
  1420. writel(host->pwr_reg, host->base + MMCIPOWER);
  1421. writel(MCI_IRQENABLE | host->variant->start_err,
  1422. host->base + MMCIMASK0);
  1423. host->irq_action = IRQ_HANDLED;
  1424. mmci_request_end(host, host->mrq);
  1425. spin_unlock_irqrestore(&host->lock, flags);
  1426. return host->irq_action;
  1427. }
  1428. static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1429. {
  1430. struct mmci_host *host = mmc_priv(mmc);
  1431. unsigned long flags;
  1432. WARN_ON(host->mrq != NULL);
  1433. mrq->cmd->error = mmci_validate_data(host, mrq->data);
  1434. if (mrq->cmd->error) {
  1435. mmc_request_done(mmc, mrq);
  1436. return;
  1437. }
  1438. spin_lock_irqsave(&host->lock, flags);
  1439. host->mrq = mrq;
  1440. if (mrq->data)
  1441. mmci_get_next_data(host, mrq->data);
  1442. if (mrq->data &&
  1443. (host->variant->datactrl_first || mrq->data->flags & MMC_DATA_READ))
  1444. mmci_start_data(host, mrq->data);
  1445. if (mrq->sbc)
  1446. mmci_start_command(host, mrq->sbc, 0);
  1447. else
  1448. mmci_start_command(host, mrq->cmd, 0);
  1449. spin_unlock_irqrestore(&host->lock, flags);
  1450. }
  1451. static void mmci_set_max_busy_timeout(struct mmc_host *mmc)
  1452. {
  1453. struct mmci_host *host = mmc_priv(mmc);
  1454. u32 max_busy_timeout = 0;
  1455. if (!host->variant->busy_detect)
  1456. return;
  1457. if (host->variant->busy_timeout && mmc->actual_clock)
  1458. max_busy_timeout = U32_MAX / DIV_ROUND_UP(mmc->actual_clock,
  1459. MSEC_PER_SEC);
  1460. mmc->max_busy_timeout = max_busy_timeout;
  1461. }
  1462. static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1463. {
  1464. struct mmci_host *host = mmc_priv(mmc);
  1465. struct variant_data *variant = host->variant;
  1466. u32 pwr = 0;
  1467. unsigned long flags;
  1468. int ret;
  1469. switch (ios->power_mode) {
  1470. case MMC_POWER_OFF:
  1471. if (!IS_ERR(mmc->supply.vmmc))
  1472. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1473. if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
  1474. regulator_disable(mmc->supply.vqmmc);
  1475. host->vqmmc_enabled = false;
  1476. }
  1477. break;
  1478. case MMC_POWER_UP:
  1479. if (!IS_ERR(mmc->supply.vmmc))
  1480. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
  1481. /*
  1482. * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
  1483. * and instead uses MCI_PWR_ON so apply whatever value is
  1484. * configured in the variant data.
  1485. */
  1486. pwr |= variant->pwrreg_powerup;
  1487. break;
  1488. case MMC_POWER_ON:
  1489. if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
  1490. ret = regulator_enable(mmc->supply.vqmmc);
  1491. if (ret < 0)
  1492. dev_err(mmc_dev(mmc),
  1493. "failed to enable vqmmc regulator\n");
  1494. else
  1495. host->vqmmc_enabled = true;
  1496. }
  1497. pwr |= MCI_PWR_ON;
  1498. break;
  1499. }
  1500. if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
  1501. /*
  1502. * The ST Micro variant has some additional bits
  1503. * indicating signal direction for the signals in
  1504. * the SD/MMC bus and feedback-clock usage.
  1505. */
  1506. pwr |= host->pwr_reg_add;
  1507. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1508. pwr &= ~MCI_ST_DATA74DIREN;
  1509. else if (ios->bus_width == MMC_BUS_WIDTH_1)
  1510. pwr &= (~MCI_ST_DATA74DIREN &
  1511. ~MCI_ST_DATA31DIREN &
  1512. ~MCI_ST_DATA2DIREN);
  1513. }
  1514. if (variant->opendrain) {
  1515. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  1516. pwr |= variant->opendrain;
  1517. } else {
  1518. /*
  1519. * If the variant cannot configure the pads by its own, then we
  1520. * expect the pinctrl to be able to do that for us
  1521. */
  1522. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  1523. pinctrl_select_state(host->pinctrl, host->pins_opendrain);
  1524. else
  1525. pinctrl_select_default_state(mmc_dev(mmc));
  1526. }
  1527. /*
  1528. * If clock = 0 and the variant requires the MMCIPOWER to be used for
  1529. * gating the clock, the MCI_PWR_ON bit is cleared.
  1530. */
  1531. if (!ios->clock && variant->pwrreg_clkgate)
  1532. pwr &= ~MCI_PWR_ON;
  1533. if (host->variant->explicit_mclk_control &&
  1534. ios->clock != host->clock_cache) {
  1535. ret = clk_set_rate(host->clk, ios->clock);
  1536. if (ret < 0)
  1537. dev_err(mmc_dev(host->mmc),
  1538. "Error setting clock rate (%d)\n", ret);
  1539. else
  1540. host->mclk = clk_get_rate(host->clk);
  1541. }
  1542. host->clock_cache = ios->clock;
  1543. spin_lock_irqsave(&host->lock, flags);
  1544. if (host->ops && host->ops->set_clkreg)
  1545. host->ops->set_clkreg(host, ios->clock);
  1546. else
  1547. mmci_set_clkreg(host, ios->clock);
  1548. mmci_set_max_busy_timeout(mmc);
  1549. if (host->ops && host->ops->set_pwrreg)
  1550. host->ops->set_pwrreg(host, pwr);
  1551. else
  1552. mmci_write_pwrreg(host, pwr);
  1553. mmci_reg_delay(host);
  1554. spin_unlock_irqrestore(&host->lock, flags);
  1555. }
  1556. static int mmci_get_cd(struct mmc_host *mmc)
  1557. {
  1558. struct mmci_host *host = mmc_priv(mmc);
  1559. struct mmci_platform_data *plat = host->plat;
  1560. unsigned int status = mmc_gpio_get_cd(mmc);
  1561. if (status == -ENOSYS) {
  1562. if (!plat->status)
  1563. return 1; /* Assume always present */
  1564. status = plat->status(mmc_dev(host->mmc));
  1565. }
  1566. return status;
  1567. }
  1568. static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
  1569. {
  1570. struct mmci_host *host = mmc_priv(mmc);
  1571. int ret;
  1572. ret = mmc_regulator_set_vqmmc(mmc, ios);
  1573. if (!ret && host->ops && host->ops->post_sig_volt_switch)
  1574. ret = host->ops->post_sig_volt_switch(host, ios);
  1575. else if (ret)
  1576. ret = 0;
  1577. if (ret < 0)
  1578. dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
  1579. return ret;
  1580. }
  1581. static struct mmc_host_ops mmci_ops = {
  1582. .request = mmci_request,
  1583. .pre_req = mmci_pre_request,
  1584. .post_req = mmci_post_request,
  1585. .set_ios = mmci_set_ios,
  1586. .get_ro = mmc_gpio_get_ro,
  1587. .get_cd = mmci_get_cd,
  1588. .start_signal_voltage_switch = mmci_sig_volt_switch,
  1589. };
  1590. static void mmci_probe_level_translator(struct mmc_host *mmc)
  1591. {
  1592. struct device *dev = mmc_dev(mmc);
  1593. struct mmci_host *host = mmc_priv(mmc);
  1594. struct gpio_desc *cmd_gpio;
  1595. struct gpio_desc *ck_gpio;
  1596. struct gpio_desc *ckin_gpio;
  1597. int clk_hi, clk_lo;
  1598. /*
  1599. * Assume the level translator is present if st,use-ckin is set.
  1600. * This is to cater for DTs which do not implement this test.
  1601. */
  1602. host->clk_reg_add |= MCI_STM32_CLK_SELCKIN;
  1603. cmd_gpio = gpiod_get(dev, "st,cmd", GPIOD_OUT_HIGH);
  1604. if (IS_ERR(cmd_gpio))
  1605. goto exit_cmd;
  1606. ck_gpio = gpiod_get(dev, "st,ck", GPIOD_OUT_HIGH);
  1607. if (IS_ERR(ck_gpio))
  1608. goto exit_ck;
  1609. ckin_gpio = gpiod_get(dev, "st,ckin", GPIOD_IN);
  1610. if (IS_ERR(ckin_gpio))
  1611. goto exit_ckin;
  1612. /* All GPIOs are valid, test whether level translator works */
  1613. /* Sample CKIN */
  1614. clk_hi = !!gpiod_get_value(ckin_gpio);
  1615. /* Set CK low */
  1616. gpiod_set_value(ck_gpio, 0);
  1617. /* Sample CKIN */
  1618. clk_lo = !!gpiod_get_value(ckin_gpio);
  1619. /* Tristate all */
  1620. gpiod_direction_input(cmd_gpio);
  1621. gpiod_direction_input(ck_gpio);
  1622. /* Level translator is present if CK signal is propagated to CKIN */
  1623. if (!clk_hi || clk_lo) {
  1624. host->clk_reg_add &= ~MCI_STM32_CLK_SELCKIN;
  1625. dev_warn(dev,
  1626. "Level translator inoperable, CK signal not detected on CKIN, disabling.\n");
  1627. }
  1628. gpiod_put(ckin_gpio);
  1629. exit_ckin:
  1630. gpiod_put(ck_gpio);
  1631. exit_ck:
  1632. gpiod_put(cmd_gpio);
  1633. exit_cmd:
  1634. pinctrl_select_default_state(dev);
  1635. }
  1636. static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
  1637. {
  1638. struct mmci_host *host = mmc_priv(mmc);
  1639. int ret = mmc_of_parse(mmc);
  1640. if (ret)
  1641. return ret;
  1642. if (of_get_property(np, "st,sig-dir-dat0", NULL))
  1643. host->pwr_reg_add |= MCI_ST_DATA0DIREN;
  1644. if (of_get_property(np, "st,sig-dir-dat2", NULL))
  1645. host->pwr_reg_add |= MCI_ST_DATA2DIREN;
  1646. if (of_get_property(np, "st,sig-dir-dat31", NULL))
  1647. host->pwr_reg_add |= MCI_ST_DATA31DIREN;
  1648. if (of_get_property(np, "st,sig-dir-dat74", NULL))
  1649. host->pwr_reg_add |= MCI_ST_DATA74DIREN;
  1650. if (of_get_property(np, "st,sig-dir-cmd", NULL))
  1651. host->pwr_reg_add |= MCI_ST_CMDDIREN;
  1652. if (of_get_property(np, "st,sig-pin-fbclk", NULL))
  1653. host->pwr_reg_add |= MCI_ST_FBCLKEN;
  1654. if (of_get_property(np, "st,sig-dir", NULL))
  1655. host->pwr_reg_add |= MCI_STM32_DIRPOL;
  1656. if (of_get_property(np, "st,neg-edge", NULL))
  1657. host->clk_reg_add |= MCI_STM32_CLK_NEGEDGE;
  1658. if (of_get_property(np, "st,use-ckin", NULL))
  1659. mmci_probe_level_translator(mmc);
  1660. if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
  1661. mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
  1662. if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
  1663. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1664. return 0;
  1665. }
  1666. static int mmci_probe(struct amba_device *dev,
  1667. const struct amba_id *id)
  1668. {
  1669. struct mmci_platform_data *plat = dev->dev.platform_data;
  1670. struct device_node *np = dev->dev.of_node;
  1671. struct variant_data *variant = id->data;
  1672. struct mmci_host *host;
  1673. struct mmc_host *mmc;
  1674. int ret;
  1675. /* Must have platform data or Device Tree. */
  1676. if (!plat && !np) {
  1677. dev_err(&dev->dev, "No plat data or DT found\n");
  1678. return -EINVAL;
  1679. }
  1680. if (!plat) {
  1681. plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
  1682. if (!plat)
  1683. return -ENOMEM;
  1684. }
  1685. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  1686. if (!mmc)
  1687. return -ENOMEM;
  1688. host = mmc_priv(mmc);
  1689. host->mmc = mmc;
  1690. host->mmc_ops = &mmci_ops;
  1691. mmc->ops = &mmci_ops;
  1692. ret = mmci_of_parse(np, mmc);
  1693. if (ret)
  1694. goto host_free;
  1695. /*
  1696. * Some variant (STM32) doesn't have opendrain bit, nevertheless
  1697. * pins can be set accordingly using pinctrl
  1698. */
  1699. if (!variant->opendrain) {
  1700. host->pinctrl = devm_pinctrl_get(&dev->dev);
  1701. if (IS_ERR(host->pinctrl)) {
  1702. dev_err(&dev->dev, "failed to get pinctrl");
  1703. ret = PTR_ERR(host->pinctrl);
  1704. goto host_free;
  1705. }
  1706. host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
  1707. MMCI_PINCTRL_STATE_OPENDRAIN);
  1708. if (IS_ERR(host->pins_opendrain)) {
  1709. dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
  1710. ret = PTR_ERR(host->pins_opendrain);
  1711. goto host_free;
  1712. }
  1713. }
  1714. host->hw_designer = amba_manf(dev);
  1715. host->hw_revision = amba_rev(dev);
  1716. dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
  1717. dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
  1718. host->clk = devm_clk_get(&dev->dev, NULL);
  1719. if (IS_ERR(host->clk)) {
  1720. ret = PTR_ERR(host->clk);
  1721. goto host_free;
  1722. }
  1723. ret = clk_prepare_enable(host->clk);
  1724. if (ret)
  1725. goto host_free;
  1726. if (variant->qcom_fifo)
  1727. host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
  1728. else
  1729. host->get_rx_fifocnt = mmci_get_rx_fifocnt;
  1730. host->plat = plat;
  1731. host->variant = variant;
  1732. host->mclk = clk_get_rate(host->clk);
  1733. /*
  1734. * According to the spec, mclk is max 100 MHz,
  1735. * so we try to adjust the clock down to this,
  1736. * (if possible).
  1737. */
  1738. if (host->mclk > variant->f_max) {
  1739. ret = clk_set_rate(host->clk, variant->f_max);
  1740. if (ret < 0)
  1741. goto clk_disable;
  1742. host->mclk = clk_get_rate(host->clk);
  1743. dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
  1744. host->mclk);
  1745. }
  1746. host->phybase = dev->res.start;
  1747. host->base = devm_ioremap_resource(&dev->dev, &dev->res);
  1748. if (IS_ERR(host->base)) {
  1749. ret = PTR_ERR(host->base);
  1750. goto clk_disable;
  1751. }
  1752. if (variant->init)
  1753. variant->init(host);
  1754. /*
  1755. * The ARM and ST versions of the block have slightly different
  1756. * clock divider equations which means that the minimum divider
  1757. * differs too.
  1758. * on Qualcomm like controllers get the nearest minimum clock to 100Khz
  1759. */
  1760. if (variant->st_clkdiv)
  1761. mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
  1762. else if (variant->stm32_clkdiv)
  1763. mmc->f_min = DIV_ROUND_UP(host->mclk, 2046);
  1764. else if (variant->explicit_mclk_control)
  1765. mmc->f_min = clk_round_rate(host->clk, 100000);
  1766. else
  1767. mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
  1768. /*
  1769. * If no maximum operating frequency is supplied, fall back to use
  1770. * the module parameter, which has a (low) default value in case it
  1771. * is not specified. Either value must not exceed the clock rate into
  1772. * the block, of course.
  1773. */
  1774. if (mmc->f_max)
  1775. mmc->f_max = variant->explicit_mclk_control ?
  1776. min(variant->f_max, mmc->f_max) :
  1777. min(host->mclk, mmc->f_max);
  1778. else
  1779. mmc->f_max = variant->explicit_mclk_control ?
  1780. fmax : min(host->mclk, fmax);
  1781. dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
  1782. host->rst = devm_reset_control_get_optional_exclusive(&dev->dev, NULL);
  1783. if (IS_ERR(host->rst)) {
  1784. ret = PTR_ERR(host->rst);
  1785. goto clk_disable;
  1786. }
  1787. ret = reset_control_deassert(host->rst);
  1788. if (ret)
  1789. dev_err(mmc_dev(mmc), "failed to de-assert reset\n");
  1790. /* Get regulators and the supported OCR mask */
  1791. ret = mmc_regulator_get_supply(mmc);
  1792. if (ret)
  1793. goto clk_disable;
  1794. if (!mmc->ocr_avail)
  1795. mmc->ocr_avail = plat->ocr_mask;
  1796. else if (plat->ocr_mask)
  1797. dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
  1798. /* We support these capabilities. */
  1799. mmc->caps |= MMC_CAP_CMD23;
  1800. /*
  1801. * Enable busy detection.
  1802. */
  1803. if (variant->busy_detect) {
  1804. mmci_ops.card_busy = mmci_card_busy;
  1805. /*
  1806. * Not all variants have a flag to enable busy detection
  1807. * in the DPSM, but if they do, set it here.
  1808. */
  1809. if (variant->busy_dpsm_flag)
  1810. mmci_write_datactrlreg(host,
  1811. host->variant->busy_dpsm_flag);
  1812. mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
  1813. }
  1814. /* Variants with mandatory busy timeout in HW needs R1B responses. */
  1815. if (variant->busy_timeout)
  1816. mmc->caps |= MMC_CAP_NEED_RSP_BUSY;
  1817. /* Prepare a CMD12 - needed to clear the DPSM on some variants. */
  1818. host->stop_abort.opcode = MMC_STOP_TRANSMISSION;
  1819. host->stop_abort.arg = 0;
  1820. host->stop_abort.flags = MMC_RSP_R1B | MMC_CMD_AC;
  1821. /* We support these PM capabilities. */
  1822. mmc->pm_caps |= MMC_PM_KEEP_POWER;
  1823. /*
  1824. * We can do SGIO
  1825. */
  1826. mmc->max_segs = NR_SG;
  1827. /*
  1828. * Since only a certain number of bits are valid in the data length
  1829. * register, we must ensure that we don't exceed 2^num-1 bytes in a
  1830. * single request.
  1831. */
  1832. mmc->max_req_size = (1 << variant->datalength_bits) - 1;
  1833. /*
  1834. * Set the maximum segment size. Since we aren't doing DMA
  1835. * (yet) we are only limited by the data length register.
  1836. */
  1837. mmc->max_seg_size = mmc->max_req_size;
  1838. /*
  1839. * Block size can be up to 2048 bytes, but must be a power of two.
  1840. */
  1841. mmc->max_blk_size = 1 << variant->datactrl_blocksz;
  1842. /*
  1843. * Limit the number of blocks transferred so that we don't overflow
  1844. * the maximum request size.
  1845. */
  1846. mmc->max_blk_count = mmc->max_req_size >> variant->datactrl_blocksz;
  1847. spin_lock_init(&host->lock);
  1848. writel(0, host->base + MMCIMASK0);
  1849. if (variant->mmcimask1)
  1850. writel(0, host->base + MMCIMASK1);
  1851. writel(0xfff, host->base + MMCICLEAR);
  1852. /*
  1853. * If:
  1854. * - not using DT but using a descriptor table, or
  1855. * - using a table of descriptors ALONGSIDE DT, or
  1856. * look up these descriptors named "cd" and "wp" right here, fail
  1857. * silently of these do not exist
  1858. */
  1859. if (!np) {
  1860. ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0);
  1861. if (ret == -EPROBE_DEFER)
  1862. goto clk_disable;
  1863. ret = mmc_gpiod_request_ro(mmc, "wp", 0, 0);
  1864. if (ret == -EPROBE_DEFER)
  1865. goto clk_disable;
  1866. }
  1867. ret = devm_request_threaded_irq(&dev->dev, dev->irq[0], mmci_irq,
  1868. mmci_irq_thread, IRQF_SHARED,
  1869. DRIVER_NAME " (cmd)", host);
  1870. if (ret)
  1871. goto clk_disable;
  1872. if (!dev->irq[1])
  1873. host->singleirq = true;
  1874. else {
  1875. ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
  1876. IRQF_SHARED, DRIVER_NAME " (pio)", host);
  1877. if (ret)
  1878. goto clk_disable;
  1879. }
  1880. writel(MCI_IRQENABLE | variant->start_err, host->base + MMCIMASK0);
  1881. amba_set_drvdata(dev, mmc);
  1882. dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
  1883. mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
  1884. amba_rev(dev), (unsigned long long)dev->res.start,
  1885. dev->irq[0], dev->irq[1]);
  1886. mmci_dma_setup(host);
  1887. pm_runtime_set_autosuspend_delay(&dev->dev, 50);
  1888. pm_runtime_use_autosuspend(&dev->dev);
  1889. ret = mmc_add_host(mmc);
  1890. if (ret)
  1891. goto clk_disable;
  1892. pm_runtime_put(&dev->dev);
  1893. return 0;
  1894. clk_disable:
  1895. clk_disable_unprepare(host->clk);
  1896. host_free:
  1897. mmc_free_host(mmc);
  1898. return ret;
  1899. }
  1900. static void mmci_remove(struct amba_device *dev)
  1901. {
  1902. struct mmc_host *mmc = amba_get_drvdata(dev);
  1903. if (mmc) {
  1904. struct mmci_host *host = mmc_priv(mmc);
  1905. struct variant_data *variant = host->variant;
  1906. /*
  1907. * Undo pm_runtime_put() in probe. We use the _sync
  1908. * version here so that we can access the primecell.
  1909. */
  1910. pm_runtime_get_sync(&dev->dev);
  1911. mmc_remove_host(mmc);
  1912. writel(0, host->base + MMCIMASK0);
  1913. if (variant->mmcimask1)
  1914. writel(0, host->base + MMCIMASK1);
  1915. writel(0, host->base + MMCICOMMAND);
  1916. writel(0, host->base + MMCIDATACTRL);
  1917. mmci_dma_release(host);
  1918. clk_disable_unprepare(host->clk);
  1919. mmc_free_host(mmc);
  1920. }
  1921. }
  1922. #ifdef CONFIG_PM
  1923. static void mmci_save(struct mmci_host *host)
  1924. {
  1925. unsigned long flags;
  1926. spin_lock_irqsave(&host->lock, flags);
  1927. writel(0, host->base + MMCIMASK0);
  1928. if (host->variant->pwrreg_nopower) {
  1929. writel(0, host->base + MMCIDATACTRL);
  1930. writel(0, host->base + MMCIPOWER);
  1931. writel(0, host->base + MMCICLOCK);
  1932. }
  1933. mmci_reg_delay(host);
  1934. spin_unlock_irqrestore(&host->lock, flags);
  1935. }
  1936. static void mmci_restore(struct mmci_host *host)
  1937. {
  1938. unsigned long flags;
  1939. spin_lock_irqsave(&host->lock, flags);
  1940. if (host->variant->pwrreg_nopower) {
  1941. writel(host->clk_reg, host->base + MMCICLOCK);
  1942. writel(host->datactrl_reg, host->base + MMCIDATACTRL);
  1943. writel(host->pwr_reg, host->base + MMCIPOWER);
  1944. }
  1945. writel(MCI_IRQENABLE | host->variant->start_err,
  1946. host->base + MMCIMASK0);
  1947. mmci_reg_delay(host);
  1948. spin_unlock_irqrestore(&host->lock, flags);
  1949. }
  1950. static int mmci_runtime_suspend(struct device *dev)
  1951. {
  1952. struct amba_device *adev = to_amba_device(dev);
  1953. struct mmc_host *mmc = amba_get_drvdata(adev);
  1954. if (mmc) {
  1955. struct mmci_host *host = mmc_priv(mmc);
  1956. pinctrl_pm_select_sleep_state(dev);
  1957. mmci_save(host);
  1958. clk_disable_unprepare(host->clk);
  1959. }
  1960. return 0;
  1961. }
  1962. static int mmci_runtime_resume(struct device *dev)
  1963. {
  1964. struct amba_device *adev = to_amba_device(dev);
  1965. struct mmc_host *mmc = amba_get_drvdata(adev);
  1966. if (mmc) {
  1967. struct mmci_host *host = mmc_priv(mmc);
  1968. clk_prepare_enable(host->clk);
  1969. mmci_restore(host);
  1970. pinctrl_select_default_state(dev);
  1971. }
  1972. return 0;
  1973. }
  1974. #endif
  1975. static const struct dev_pm_ops mmci_dev_pm_ops = {
  1976. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1977. pm_runtime_force_resume)
  1978. SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
  1979. };
  1980. static const struct amba_id mmci_ids[] = {
  1981. {
  1982. .id = 0x00041180,
  1983. .mask = 0xff0fffff,
  1984. .data = &variant_arm,
  1985. },
  1986. {
  1987. .id = 0x01041180,
  1988. .mask = 0xff0fffff,
  1989. .data = &variant_arm_extended_fifo,
  1990. },
  1991. {
  1992. .id = 0x02041180,
  1993. .mask = 0xff0fffff,
  1994. .data = &variant_arm_extended_fifo_hwfc,
  1995. },
  1996. {
  1997. .id = 0x00041181,
  1998. .mask = 0x000fffff,
  1999. .data = &variant_arm,
  2000. },
  2001. /* ST Micro variants */
  2002. {
  2003. .id = 0x00180180,
  2004. .mask = 0x00ffffff,
  2005. .data = &variant_u300,
  2006. },
  2007. {
  2008. .id = 0x10180180,
  2009. .mask = 0xf0ffffff,
  2010. .data = &variant_nomadik,
  2011. },
  2012. {
  2013. .id = 0x00280180,
  2014. .mask = 0x00ffffff,
  2015. .data = &variant_nomadik,
  2016. },
  2017. {
  2018. .id = 0x00480180,
  2019. .mask = 0xf0ffffff,
  2020. .data = &variant_ux500,
  2021. },
  2022. {
  2023. .id = 0x10480180,
  2024. .mask = 0xf0ffffff,
  2025. .data = &variant_ux500v2,
  2026. },
  2027. {
  2028. .id = 0x00880180,
  2029. .mask = 0x00ffffff,
  2030. .data = &variant_stm32,
  2031. },
  2032. {
  2033. .id = 0x10153180,
  2034. .mask = 0xf0ffffff,
  2035. .data = &variant_stm32_sdmmc,
  2036. },
  2037. {
  2038. .id = 0x00253180,
  2039. .mask = 0xf0ffffff,
  2040. .data = &variant_stm32_sdmmcv2,
  2041. },
  2042. {
  2043. .id = 0x20253180,
  2044. .mask = 0xf0ffffff,
  2045. .data = &variant_stm32_sdmmcv2,
  2046. },
  2047. /* Qualcomm variants */
  2048. {
  2049. .id = 0x00051180,
  2050. .mask = 0x000fffff,
  2051. .data = &variant_qcom,
  2052. },
  2053. { 0, 0 },
  2054. };
  2055. MODULE_DEVICE_TABLE(amba, mmci_ids);
  2056. static struct amba_driver mmci_driver = {
  2057. .drv = {
  2058. .name = DRIVER_NAME,
  2059. .pm = &mmci_dev_pm_ops,
  2060. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  2061. },
  2062. .probe = mmci_probe,
  2063. .remove = mmci_remove,
  2064. .id_table = mmci_ids,
  2065. };
  2066. module_amba_driver(mmci_driver);
  2067. module_param(fmax, uint, 0444);
  2068. MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
  2069. MODULE_LICENSE("GPL");