mmci.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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 AB.
  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/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/highmem.h>
  20. #include <linux/log2.h>
  21. #include <linux/mmc/host.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/clk.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/gpio.h>
  26. #include <linux/amba/mmci.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <asm/div64.h>
  29. #include <asm/io.h>
  30. #include <asm/sizes.h>
  31. #include "mmci.h"
  32. #define DRIVER_NAME "mmci-pl18x"
  33. static unsigned int fmax = 515633;
  34. /**
  35. * struct variant_data - MMCI variant-specific quirks
  36. * @clkreg: default value for MCICLOCK register
  37. * @clkreg_enable: enable value for MMCICLOCK register
  38. * @datalength_bits: number of bits in the MMCIDATALENGTH register
  39. * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
  40. * is asserted (likewise for RX)
  41. * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
  42. * is asserted (likewise for RX)
  43. * @broken_blockend: the MCI_DATABLOCKEND is broken on the hardware
  44. * and will not work at all.
  45. * @broken_blockend_dma: the MCI_DATABLOCKEND is broken on the hardware when
  46. * using DMA.
  47. */
  48. struct variant_data {
  49. unsigned int clkreg;
  50. unsigned int clkreg_enable;
  51. unsigned int datalength_bits;
  52. unsigned int fifosize;
  53. unsigned int fifohalfsize;
  54. bool broken_blockend;
  55. bool broken_blockend_dma;
  56. };
  57. static struct variant_data variant_arm = {
  58. .fifosize = 16 * 4,
  59. .fifohalfsize = 8 * 4,
  60. .datalength_bits = 16,
  61. };
  62. static struct variant_data variant_u300 = {
  63. .fifosize = 16 * 4,
  64. .fifohalfsize = 8 * 4,
  65. .clkreg_enable = 1 << 13, /* HWFCEN */
  66. .datalength_bits = 16,
  67. .broken_blockend_dma = true,
  68. };
  69. static struct variant_data variant_ux500 = {
  70. .fifosize = 30 * 4,
  71. .fifohalfsize = 8 * 4,
  72. .clkreg = MCI_CLK_ENABLE,
  73. .clkreg_enable = 1 << 14, /* HWFCEN */
  74. .datalength_bits = 24,
  75. .broken_blockend = true,
  76. };
  77. /*
  78. * This must be called with host->lock held
  79. */
  80. static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
  81. {
  82. struct variant_data *variant = host->variant;
  83. u32 clk = variant->clkreg;
  84. if (desired) {
  85. if (desired >= host->mclk) {
  86. clk = MCI_CLK_BYPASS;
  87. host->cclk = host->mclk;
  88. } else {
  89. clk = host->mclk / (2 * desired) - 1;
  90. if (clk >= 256)
  91. clk = 255;
  92. host->cclk = host->mclk / (2 * (clk + 1));
  93. }
  94. clk |= variant->clkreg_enable;
  95. clk |= MCI_CLK_ENABLE;
  96. /* This hasn't proven to be worthwhile */
  97. /* clk |= MCI_CLK_PWRSAVE; */
  98. }
  99. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  100. clk |= MCI_4BIT_BUS;
  101. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  102. clk |= MCI_ST_8BIT_BUS;
  103. writel(clk, host->base + MMCICLOCK);
  104. }
  105. static void
  106. mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
  107. {
  108. writel(0, host->base + MMCICOMMAND);
  109. BUG_ON(host->data);
  110. host->mrq = NULL;
  111. host->cmd = NULL;
  112. if (mrq->data)
  113. mrq->data->bytes_xfered = host->data_xfered;
  114. /*
  115. * Need to drop the host lock here; mmc_request_done may call
  116. * back into the driver...
  117. */
  118. spin_unlock(&host->lock);
  119. mmc_request_done(host->mmc, mrq);
  120. spin_lock(&host->lock);
  121. }
  122. static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
  123. {
  124. void __iomem *base = host->base;
  125. if (host->singleirq) {
  126. unsigned int mask0 = readl(base + MMCIMASK0);
  127. mask0 &= ~MCI_IRQ1MASK;
  128. mask0 |= mask;
  129. writel(mask0, base + MMCIMASK0);
  130. }
  131. writel(mask, base + MMCIMASK1);
  132. }
  133. static void mmci_stop_data(struct mmci_host *host)
  134. {
  135. writel(0, host->base + MMCIDATACTRL);
  136. mmci_set_mask1(host, 0);
  137. host->data = NULL;
  138. }
  139. static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
  140. {
  141. unsigned int flags = SG_MITER_ATOMIC;
  142. if (data->flags & MMC_DATA_READ)
  143. flags |= SG_MITER_TO_SG;
  144. else
  145. flags |= SG_MITER_FROM_SG;
  146. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  147. }
  148. static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
  149. {
  150. struct variant_data *variant = host->variant;
  151. unsigned int datactrl, timeout, irqmask;
  152. unsigned long long clks;
  153. void __iomem *base;
  154. int blksz_bits;
  155. dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
  156. data->blksz, data->blocks, data->flags);
  157. host->data = data;
  158. host->size = data->blksz * data->blocks;
  159. host->data_xfered = 0;
  160. host->blockend = false;
  161. host->dataend = false;
  162. mmci_init_sg(host, data);
  163. clks = (unsigned long long)data->timeout_ns * host->cclk;
  164. do_div(clks, 1000000000UL);
  165. timeout = data->timeout_clks + (unsigned int)clks;
  166. base = host->base;
  167. writel(timeout, base + MMCIDATATIMER);
  168. writel(host->size, base + MMCIDATALENGTH);
  169. blksz_bits = ffs(data->blksz) - 1;
  170. BUG_ON(1 << blksz_bits != data->blksz);
  171. datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
  172. if (data->flags & MMC_DATA_READ) {
  173. datactrl |= MCI_DPSM_DIRECTION;
  174. irqmask = MCI_RXFIFOHALFFULLMASK;
  175. /*
  176. * If we have less than a FIFOSIZE of bytes to transfer,
  177. * trigger a PIO interrupt as soon as any data is available.
  178. */
  179. if (host->size < variant->fifosize)
  180. irqmask |= MCI_RXDATAAVLBLMASK;
  181. } else {
  182. /*
  183. * We don't actually need to include "FIFO empty" here
  184. * since its implicit in "FIFO half empty".
  185. */
  186. irqmask = MCI_TXFIFOHALFEMPTYMASK;
  187. }
  188. writel(datactrl, base + MMCIDATACTRL);
  189. writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
  190. mmci_set_mask1(host, irqmask);
  191. }
  192. static void
  193. mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
  194. {
  195. void __iomem *base = host->base;
  196. dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
  197. cmd->opcode, cmd->arg, cmd->flags);
  198. if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
  199. writel(0, base + MMCICOMMAND);
  200. udelay(1);
  201. }
  202. c |= cmd->opcode | MCI_CPSM_ENABLE;
  203. if (cmd->flags & MMC_RSP_PRESENT) {
  204. if (cmd->flags & MMC_RSP_136)
  205. c |= MCI_CPSM_LONGRSP;
  206. c |= MCI_CPSM_RESPONSE;
  207. }
  208. if (/*interrupt*/0)
  209. c |= MCI_CPSM_INTERRUPT;
  210. host->cmd = cmd;
  211. writel(cmd->arg, base + MMCIARGUMENT);
  212. writel(c, base + MMCICOMMAND);
  213. }
  214. static void
  215. mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
  216. unsigned int status)
  217. {
  218. struct variant_data *variant = host->variant;
  219. /* First check for errors */
  220. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
  221. dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ (status %08x)\n", status);
  222. if (status & MCI_DATACRCFAIL)
  223. data->error = -EILSEQ;
  224. else if (status & MCI_DATATIMEOUT)
  225. data->error = -ETIMEDOUT;
  226. else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
  227. data->error = -EIO;
  228. /* Force-complete the transaction */
  229. host->blockend = true;
  230. host->dataend = true;
  231. /*
  232. * We hit an error condition. Ensure that any data
  233. * partially written to a page is properly coherent.
  234. */
  235. if (data->flags & MMC_DATA_READ) {
  236. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  237. unsigned long flags;
  238. local_irq_save(flags);
  239. if (sg_miter_next(sg_miter)) {
  240. flush_dcache_page(sg_miter->page);
  241. sg_miter_stop(sg_miter);
  242. }
  243. local_irq_restore(flags);
  244. }
  245. }
  246. /*
  247. * On ARM variants in PIO mode, MCI_DATABLOCKEND
  248. * is always sent first, and we increase the
  249. * transfered number of bytes for that IRQ. Then
  250. * MCI_DATAEND follows and we conclude the transaction.
  251. *
  252. * On the Ux500 single-IRQ variant MCI_DATABLOCKEND
  253. * doesn't seem to immediately clear from the status,
  254. * so we can't use it keep count when only one irq is
  255. * used because the irq will hit for other reasons, and
  256. * then the flag is still up. So we use the MCI_DATAEND
  257. * IRQ at the end of the entire transfer because
  258. * MCI_DATABLOCKEND is broken.
  259. *
  260. * In the U300, the IRQs can arrive out-of-order,
  261. * e.g. MCI_DATABLOCKEND sometimes arrives after MCI_DATAEND,
  262. * so for this case we use the flags "blockend" and
  263. * "dataend" to make sure both IRQs have arrived before
  264. * concluding the transaction. (This does not apply
  265. * to the Ux500 which doesn't fire MCI_DATABLOCKEND
  266. * at all.) In DMA mode it suffers from the same problem
  267. * as the Ux500.
  268. */
  269. if (status & MCI_DATABLOCKEND) {
  270. /*
  271. * Just being a little over-cautious, we do not
  272. * use this progressive update if the hardware blockend
  273. * flag is unreliable: since it can stay high between
  274. * IRQs it will corrupt the transfer counter.
  275. */
  276. if (!variant->broken_blockend)
  277. host->data_xfered += data->blksz;
  278. host->blockend = true;
  279. }
  280. if (status & MCI_DATAEND)
  281. host->dataend = true;
  282. /*
  283. * On variants with broken blockend we shall only wait for dataend,
  284. * on others we must sync with the blockend signal since they can
  285. * appear out-of-order.
  286. */
  287. if (host->dataend && (host->blockend || variant->broken_blockend)) {
  288. mmci_stop_data(host);
  289. /* Reset these flags */
  290. host->blockend = false;
  291. host->dataend = false;
  292. /*
  293. * Variants with broken blockend flags need to handle the
  294. * end of the entire transfer here.
  295. */
  296. if (variant->broken_blockend && !data->error)
  297. host->data_xfered += data->blksz * data->blocks;
  298. if (!data->stop) {
  299. mmci_request_end(host, data->mrq);
  300. } else {
  301. mmci_start_command(host, data->stop, 0);
  302. }
  303. }
  304. }
  305. static void
  306. mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
  307. unsigned int status)
  308. {
  309. void __iomem *base = host->base;
  310. host->cmd = NULL;
  311. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  312. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  313. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  314. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  315. if (status & MCI_CMDTIMEOUT) {
  316. cmd->error = -ETIMEDOUT;
  317. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  318. cmd->error = -EILSEQ;
  319. }
  320. if (!cmd->data || cmd->error) {
  321. if (host->data)
  322. mmci_stop_data(host);
  323. mmci_request_end(host, cmd->mrq);
  324. } else if (!(cmd->data->flags & MMC_DATA_READ)) {
  325. mmci_start_data(host, cmd->data);
  326. }
  327. }
  328. static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
  329. {
  330. void __iomem *base = host->base;
  331. char *ptr = buffer;
  332. u32 status;
  333. int host_remain = host->size;
  334. do {
  335. int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
  336. if (count > remain)
  337. count = remain;
  338. if (count <= 0)
  339. break;
  340. readsl(base + MMCIFIFO, ptr, count >> 2);
  341. ptr += count;
  342. remain -= count;
  343. host_remain -= count;
  344. if (remain == 0)
  345. break;
  346. status = readl(base + MMCISTATUS);
  347. } while (status & MCI_RXDATAAVLBL);
  348. return ptr - buffer;
  349. }
  350. static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
  351. {
  352. struct variant_data *variant = host->variant;
  353. void __iomem *base = host->base;
  354. char *ptr = buffer;
  355. do {
  356. unsigned int count, maxcnt;
  357. maxcnt = status & MCI_TXFIFOEMPTY ?
  358. variant->fifosize : variant->fifohalfsize;
  359. count = min(remain, maxcnt);
  360. writesl(base + MMCIFIFO, ptr, count >> 2);
  361. ptr += count;
  362. remain -= count;
  363. if (remain == 0)
  364. break;
  365. status = readl(base + MMCISTATUS);
  366. } while (status & MCI_TXFIFOHALFEMPTY);
  367. return ptr - buffer;
  368. }
  369. /*
  370. * PIO data transfer IRQ handler.
  371. */
  372. static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  373. {
  374. struct mmci_host *host = dev_id;
  375. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  376. struct variant_data *variant = host->variant;
  377. void __iomem *base = host->base;
  378. unsigned long flags;
  379. u32 status;
  380. status = readl(base + MMCISTATUS);
  381. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  382. local_irq_save(flags);
  383. do {
  384. unsigned int remain, len;
  385. char *buffer;
  386. /*
  387. * For write, we only need to test the half-empty flag
  388. * here - if the FIFO is completely empty, then by
  389. * definition it is more than half empty.
  390. *
  391. * For read, check for data available.
  392. */
  393. if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
  394. break;
  395. if (!sg_miter_next(sg_miter))
  396. break;
  397. buffer = sg_miter->addr;
  398. remain = sg_miter->length;
  399. len = 0;
  400. if (status & MCI_RXACTIVE)
  401. len = mmci_pio_read(host, buffer, remain);
  402. if (status & MCI_TXACTIVE)
  403. len = mmci_pio_write(host, buffer, remain, status);
  404. sg_miter->consumed = len;
  405. host->size -= len;
  406. remain -= len;
  407. if (remain)
  408. break;
  409. if (status & MCI_RXACTIVE)
  410. flush_dcache_page(sg_miter->page);
  411. status = readl(base + MMCISTATUS);
  412. } while (1);
  413. sg_miter_stop(sg_miter);
  414. local_irq_restore(flags);
  415. /*
  416. * If we're nearing the end of the read, switch to
  417. * "any data available" mode.
  418. */
  419. if (status & MCI_RXACTIVE && host->size < variant->fifosize)
  420. mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
  421. /*
  422. * If we run out of data, disable the data IRQs; this
  423. * prevents a race where the FIFO becomes empty before
  424. * the chip itself has disabled the data path, and
  425. * stops us racing with our data end IRQ.
  426. */
  427. if (host->size == 0) {
  428. mmci_set_mask1(host, 0);
  429. writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
  430. }
  431. return IRQ_HANDLED;
  432. }
  433. /*
  434. * Handle completion of command and data transfers.
  435. */
  436. static irqreturn_t mmci_irq(int irq, void *dev_id)
  437. {
  438. struct mmci_host *host = dev_id;
  439. u32 status;
  440. int ret = 0;
  441. spin_lock(&host->lock);
  442. do {
  443. struct mmc_command *cmd;
  444. struct mmc_data *data;
  445. status = readl(host->base + MMCISTATUS);
  446. if (host->singleirq) {
  447. if (status & readl(host->base + MMCIMASK1))
  448. mmci_pio_irq(irq, dev_id);
  449. status &= ~MCI_IRQ1MASK;
  450. }
  451. status &= readl(host->base + MMCIMASK0);
  452. writel(status, host->base + MMCICLEAR);
  453. dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
  454. data = host->data;
  455. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
  456. MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
  457. mmci_data_irq(host, data, status);
  458. cmd = host->cmd;
  459. if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
  460. mmci_cmd_irq(host, cmd, status);
  461. ret = 1;
  462. } while (status);
  463. spin_unlock(&host->lock);
  464. return IRQ_RETVAL(ret);
  465. }
  466. static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  467. {
  468. struct mmci_host *host = mmc_priv(mmc);
  469. unsigned long flags;
  470. WARN_ON(host->mrq != NULL);
  471. if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
  472. dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
  473. mrq->data->blksz);
  474. mrq->cmd->error = -EINVAL;
  475. mmc_request_done(mmc, mrq);
  476. return;
  477. }
  478. spin_lock_irqsave(&host->lock, flags);
  479. host->mrq = mrq;
  480. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  481. mmci_start_data(host, mrq->data);
  482. mmci_start_command(host, mrq->cmd, 0);
  483. spin_unlock_irqrestore(&host->lock, flags);
  484. }
  485. static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  486. {
  487. struct mmci_host *host = mmc_priv(mmc);
  488. u32 pwr = 0;
  489. unsigned long flags;
  490. int ret;
  491. switch (ios->power_mode) {
  492. case MMC_POWER_OFF:
  493. if (host->vcc)
  494. ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
  495. break;
  496. case MMC_POWER_UP:
  497. if (host->vcc) {
  498. ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
  499. if (ret) {
  500. dev_err(mmc_dev(mmc), "unable to set OCR\n");
  501. /*
  502. * The .set_ios() function in the mmc_host_ops
  503. * struct return void, and failing to set the
  504. * power should be rare so we print an error
  505. * and return here.
  506. */
  507. return;
  508. }
  509. }
  510. if (host->plat->vdd_handler)
  511. pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
  512. ios->power_mode);
  513. /* The ST version does not have this, fall through to POWER_ON */
  514. if (host->hw_designer != AMBA_VENDOR_ST) {
  515. pwr |= MCI_PWR_UP;
  516. break;
  517. }
  518. case MMC_POWER_ON:
  519. pwr |= MCI_PWR_ON;
  520. break;
  521. }
  522. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
  523. if (host->hw_designer != AMBA_VENDOR_ST)
  524. pwr |= MCI_ROD;
  525. else {
  526. /*
  527. * The ST Micro variant use the ROD bit for something
  528. * else and only has OD (Open Drain).
  529. */
  530. pwr |= MCI_OD;
  531. }
  532. }
  533. spin_lock_irqsave(&host->lock, flags);
  534. mmci_set_clkreg(host, ios->clock);
  535. if (host->pwr != pwr) {
  536. host->pwr = pwr;
  537. writel(pwr, host->base + MMCIPOWER);
  538. }
  539. spin_unlock_irqrestore(&host->lock, flags);
  540. }
  541. static int mmci_get_ro(struct mmc_host *mmc)
  542. {
  543. struct mmci_host *host = mmc_priv(mmc);
  544. if (host->gpio_wp == -ENOSYS)
  545. return -ENOSYS;
  546. return gpio_get_value_cansleep(host->gpio_wp);
  547. }
  548. static int mmci_get_cd(struct mmc_host *mmc)
  549. {
  550. struct mmci_host *host = mmc_priv(mmc);
  551. struct mmci_platform_data *plat = host->plat;
  552. unsigned int status;
  553. if (host->gpio_cd == -ENOSYS) {
  554. if (!plat->status)
  555. return 1; /* Assume always present */
  556. status = plat->status(mmc_dev(host->mmc));
  557. } else
  558. status = !!gpio_get_value_cansleep(host->gpio_cd)
  559. ^ plat->cd_invert;
  560. /*
  561. * Use positive logic throughout - status is zero for no card,
  562. * non-zero for card inserted.
  563. */
  564. return status;
  565. }
  566. static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
  567. {
  568. struct mmci_host *host = dev_id;
  569. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  570. return IRQ_HANDLED;
  571. }
  572. static const struct mmc_host_ops mmci_ops = {
  573. .request = mmci_request,
  574. .set_ios = mmci_set_ios,
  575. .get_ro = mmci_get_ro,
  576. .get_cd = mmci_get_cd,
  577. };
  578. static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
  579. {
  580. struct mmci_platform_data *plat = dev->dev.platform_data;
  581. struct variant_data *variant = id->data;
  582. struct mmci_host *host;
  583. struct mmc_host *mmc;
  584. unsigned int mask;
  585. int ret;
  586. /* must have platform data */
  587. if (!plat) {
  588. ret = -EINVAL;
  589. goto out;
  590. }
  591. ret = amba_request_regions(dev, DRIVER_NAME);
  592. if (ret)
  593. goto out;
  594. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  595. if (!mmc) {
  596. ret = -ENOMEM;
  597. goto rel_regions;
  598. }
  599. host = mmc_priv(mmc);
  600. host->mmc = mmc;
  601. host->gpio_wp = -ENOSYS;
  602. host->gpio_cd = -ENOSYS;
  603. host->gpio_cd_irq = -1;
  604. host->hw_designer = amba_manf(dev);
  605. host->hw_revision = amba_rev(dev);
  606. dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
  607. dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
  608. host->clk = clk_get(&dev->dev, NULL);
  609. if (IS_ERR(host->clk)) {
  610. ret = PTR_ERR(host->clk);
  611. host->clk = NULL;
  612. goto host_free;
  613. }
  614. ret = clk_enable(host->clk);
  615. if (ret)
  616. goto clk_free;
  617. host->plat = plat;
  618. host->variant = variant;
  619. host->mclk = clk_get_rate(host->clk);
  620. /*
  621. * According to the spec, mclk is max 100 MHz,
  622. * so we try to adjust the clock down to this,
  623. * (if possible).
  624. */
  625. if (host->mclk > 100000000) {
  626. ret = clk_set_rate(host->clk, 100000000);
  627. if (ret < 0)
  628. goto clk_disable;
  629. host->mclk = clk_get_rate(host->clk);
  630. dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
  631. host->mclk);
  632. }
  633. host->base = ioremap(dev->res.start, resource_size(&dev->res));
  634. if (!host->base) {
  635. ret = -ENOMEM;
  636. goto clk_disable;
  637. }
  638. mmc->ops = &mmci_ops;
  639. mmc->f_min = (host->mclk + 511) / 512;
  640. /*
  641. * If the platform data supplies a maximum operating
  642. * frequency, this takes precedence. Else, we fall back
  643. * to using the module parameter, which has a (low)
  644. * default value in case it is not specified. Either
  645. * value must not exceed the clock rate into the block,
  646. * of course.
  647. */
  648. if (plat->f_max)
  649. mmc->f_max = min(host->mclk, plat->f_max);
  650. else
  651. mmc->f_max = min(host->mclk, fmax);
  652. dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
  653. #ifdef CONFIG_REGULATOR
  654. /* If we're using the regulator framework, try to fetch a regulator */
  655. host->vcc = regulator_get(&dev->dev, "vmmc");
  656. if (IS_ERR(host->vcc))
  657. host->vcc = NULL;
  658. else {
  659. int mask = mmc_regulator_get_ocrmask(host->vcc);
  660. if (mask < 0)
  661. dev_err(&dev->dev, "error getting OCR mask (%d)\n",
  662. mask);
  663. else {
  664. host->mmc->ocr_avail = (u32) mask;
  665. if (plat->ocr_mask)
  666. dev_warn(&dev->dev,
  667. "Provided ocr_mask/setpower will not be used "
  668. "(using regulator instead)\n");
  669. }
  670. }
  671. #endif
  672. /* Fall back to platform data if no regulator is found */
  673. if (host->vcc == NULL)
  674. mmc->ocr_avail = plat->ocr_mask;
  675. mmc->caps = plat->capabilities;
  676. /*
  677. * We can do SGIO
  678. */
  679. mmc->max_segs = NR_SG;
  680. /*
  681. * Since only a certain number of bits are valid in the data length
  682. * register, we must ensure that we don't exceed 2^num-1 bytes in a
  683. * single request.
  684. */
  685. mmc->max_req_size = (1 << variant->datalength_bits) - 1;
  686. /*
  687. * Set the maximum segment size. Since we aren't doing DMA
  688. * (yet) we are only limited by the data length register.
  689. */
  690. mmc->max_seg_size = mmc->max_req_size;
  691. /*
  692. * Block size can be up to 2048 bytes, but must be a power of two.
  693. */
  694. mmc->max_blk_size = 2048;
  695. /*
  696. * No limit on the number of blocks transferred.
  697. */
  698. mmc->max_blk_count = mmc->max_req_size;
  699. spin_lock_init(&host->lock);
  700. writel(0, host->base + MMCIMASK0);
  701. writel(0, host->base + MMCIMASK1);
  702. writel(0xfff, host->base + MMCICLEAR);
  703. if (gpio_is_valid(plat->gpio_cd)) {
  704. ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
  705. if (ret == 0)
  706. ret = gpio_direction_input(plat->gpio_cd);
  707. if (ret == 0)
  708. host->gpio_cd = plat->gpio_cd;
  709. else if (ret != -ENOSYS)
  710. goto err_gpio_cd;
  711. ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
  712. mmci_cd_irq, 0,
  713. DRIVER_NAME " (cd)", host);
  714. if (ret >= 0)
  715. host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
  716. }
  717. if (gpio_is_valid(plat->gpio_wp)) {
  718. ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
  719. if (ret == 0)
  720. ret = gpio_direction_input(plat->gpio_wp);
  721. if (ret == 0)
  722. host->gpio_wp = plat->gpio_wp;
  723. else if (ret != -ENOSYS)
  724. goto err_gpio_wp;
  725. }
  726. if ((host->plat->status || host->gpio_cd != -ENOSYS)
  727. && host->gpio_cd_irq < 0)
  728. mmc->caps |= MMC_CAP_NEEDS_POLL;
  729. ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
  730. if (ret)
  731. goto unmap;
  732. if (dev->irq[1] == NO_IRQ)
  733. host->singleirq = true;
  734. else {
  735. ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
  736. DRIVER_NAME " (pio)", host);
  737. if (ret)
  738. goto irq0_free;
  739. }
  740. mask = MCI_IRQENABLE;
  741. /* Don't use the datablockend flag if it's broken */
  742. if (variant->broken_blockend)
  743. mask &= ~MCI_DATABLOCKEND;
  744. writel(mask, host->base + MMCIMASK0);
  745. amba_set_drvdata(dev, mmc);
  746. mmc_add_host(mmc);
  747. dev_info(&dev->dev, "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
  748. mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
  749. (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
  750. return 0;
  751. irq0_free:
  752. free_irq(dev->irq[0], host);
  753. unmap:
  754. if (host->gpio_wp != -ENOSYS)
  755. gpio_free(host->gpio_wp);
  756. err_gpio_wp:
  757. if (host->gpio_cd_irq >= 0)
  758. free_irq(host->gpio_cd_irq, host);
  759. if (host->gpio_cd != -ENOSYS)
  760. gpio_free(host->gpio_cd);
  761. err_gpio_cd:
  762. iounmap(host->base);
  763. clk_disable:
  764. clk_disable(host->clk);
  765. clk_free:
  766. clk_put(host->clk);
  767. host_free:
  768. mmc_free_host(mmc);
  769. rel_regions:
  770. amba_release_regions(dev);
  771. out:
  772. return ret;
  773. }
  774. static int __devexit mmci_remove(struct amba_device *dev)
  775. {
  776. struct mmc_host *mmc = amba_get_drvdata(dev);
  777. amba_set_drvdata(dev, NULL);
  778. if (mmc) {
  779. struct mmci_host *host = mmc_priv(mmc);
  780. mmc_remove_host(mmc);
  781. writel(0, host->base + MMCIMASK0);
  782. writel(0, host->base + MMCIMASK1);
  783. writel(0, host->base + MMCICOMMAND);
  784. writel(0, host->base + MMCIDATACTRL);
  785. free_irq(dev->irq[0], host);
  786. if (!host->singleirq)
  787. free_irq(dev->irq[1], host);
  788. if (host->gpio_wp != -ENOSYS)
  789. gpio_free(host->gpio_wp);
  790. if (host->gpio_cd_irq >= 0)
  791. free_irq(host->gpio_cd_irq, host);
  792. if (host->gpio_cd != -ENOSYS)
  793. gpio_free(host->gpio_cd);
  794. iounmap(host->base);
  795. clk_disable(host->clk);
  796. clk_put(host->clk);
  797. if (host->vcc)
  798. mmc_regulator_set_ocr(mmc, host->vcc, 0);
  799. regulator_put(host->vcc);
  800. mmc_free_host(mmc);
  801. amba_release_regions(dev);
  802. }
  803. return 0;
  804. }
  805. #ifdef CONFIG_PM
  806. static int mmci_suspend(struct amba_device *dev, pm_message_t state)
  807. {
  808. struct mmc_host *mmc = amba_get_drvdata(dev);
  809. int ret = 0;
  810. if (mmc) {
  811. struct mmci_host *host = mmc_priv(mmc);
  812. ret = mmc_suspend_host(mmc);
  813. if (ret == 0)
  814. writel(0, host->base + MMCIMASK0);
  815. }
  816. return ret;
  817. }
  818. static int mmci_resume(struct amba_device *dev)
  819. {
  820. struct mmc_host *mmc = amba_get_drvdata(dev);
  821. int ret = 0;
  822. if (mmc) {
  823. struct mmci_host *host = mmc_priv(mmc);
  824. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  825. ret = mmc_resume_host(mmc);
  826. }
  827. return ret;
  828. }
  829. #else
  830. #define mmci_suspend NULL
  831. #define mmci_resume NULL
  832. #endif
  833. static struct amba_id mmci_ids[] = {
  834. {
  835. .id = 0x00041180,
  836. .mask = 0x000fffff,
  837. .data = &variant_arm,
  838. },
  839. {
  840. .id = 0x00041181,
  841. .mask = 0x000fffff,
  842. .data = &variant_arm,
  843. },
  844. /* ST Micro variants */
  845. {
  846. .id = 0x00180180,
  847. .mask = 0x00ffffff,
  848. .data = &variant_u300,
  849. },
  850. {
  851. .id = 0x00280180,
  852. .mask = 0x00ffffff,
  853. .data = &variant_u300,
  854. },
  855. {
  856. .id = 0x00480180,
  857. .mask = 0x00ffffff,
  858. .data = &variant_ux500,
  859. },
  860. { 0, 0 },
  861. };
  862. static struct amba_driver mmci_driver = {
  863. .drv = {
  864. .name = DRIVER_NAME,
  865. },
  866. .probe = mmci_probe,
  867. .remove = __devexit_p(mmci_remove),
  868. .suspend = mmci_suspend,
  869. .resume = mmci_resume,
  870. .id_table = mmci_ids,
  871. };
  872. static int __init mmci_init(void)
  873. {
  874. return amba_driver_register(&mmci_driver);
  875. }
  876. static void __exit mmci_exit(void)
  877. {
  878. amba_driver_unregister(&mmci_driver);
  879. }
  880. module_init(mmci_init);
  881. module_exit(mmci_exit);
  882. module_param(fmax, uint, 0444);
  883. MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
  884. MODULE_LICENSE("GPL");