mmci.c 24 KB

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