mmci.c 40 KB

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