mmci.c 45 KB

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