mmci.c 38 KB

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