mmci.c 43 KB

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