mmci.c 49 KB

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