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