mmci.c 48 KB

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