mmci.c 49 KB

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