mmci.c 68 KB

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