mmci.c 25 KB

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