mmci.c 55 KB

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