mmci.c 38 KB

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