mmci.c 52 KB

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