mmci.c 26 KB

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