mmci.c 36 KB

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