mmci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
  3. *
  4. * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
  5. * Copyright (C) 2010 ST-Ericsson AB.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/init.h>
  14. #include <linux/ioport.h>
  15. #include <linux/device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/highmem.h>
  20. #include <linux/log2.h>
  21. #include <linux/mmc/host.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/clk.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/gpio.h>
  26. #include <linux/amba/mmci.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <asm/cacheflush.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. * This must be called with host->lock held
  37. */
  38. static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
  39. {
  40. u32 clk = 0;
  41. if (desired) {
  42. if (desired >= host->mclk) {
  43. clk = MCI_CLK_BYPASS;
  44. host->cclk = host->mclk;
  45. } else {
  46. clk = host->mclk / (2 * desired) - 1;
  47. if (clk >= 256)
  48. clk = 255;
  49. host->cclk = host->mclk / (2 * (clk + 1));
  50. }
  51. if (host->hw_designer == AMBA_VENDOR_ST)
  52. clk |= MCI_ST_FCEN; /* Bug fix in ST IP block */
  53. clk |= MCI_CLK_ENABLE;
  54. /* This hasn't proven to be worthwhile */
  55. /* clk |= MCI_CLK_PWRSAVE; */
  56. }
  57. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  58. clk |= MCI_4BIT_BUS;
  59. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  60. clk |= MCI_ST_8BIT_BUS;
  61. writel(clk, host->base + MMCICLOCK);
  62. }
  63. static void
  64. mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
  65. {
  66. writel(0, host->base + MMCICOMMAND);
  67. BUG_ON(host->data);
  68. host->mrq = NULL;
  69. host->cmd = NULL;
  70. if (mrq->data)
  71. mrq->data->bytes_xfered = host->data_xfered;
  72. /*
  73. * Need to drop the host lock here; mmc_request_done may call
  74. * back into the driver...
  75. */
  76. spin_unlock(&host->lock);
  77. mmc_request_done(host->mmc, mrq);
  78. spin_lock(&host->lock);
  79. }
  80. static void mmci_stop_data(struct mmci_host *host)
  81. {
  82. writel(0, host->base + MMCIDATACTRL);
  83. writel(0, host->base + MMCIMASK1);
  84. host->data = NULL;
  85. }
  86. static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
  87. {
  88. unsigned int datactrl, timeout, irqmask;
  89. unsigned long long clks;
  90. void __iomem *base;
  91. int blksz_bits;
  92. dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
  93. data->blksz, data->blocks, data->flags);
  94. host->data = data;
  95. host->size = data->blksz;
  96. host->data_xfered = 0;
  97. mmci_init_sg(host, data);
  98. clks = (unsigned long long)data->timeout_ns * host->cclk;
  99. do_div(clks, 1000000000UL);
  100. timeout = data->timeout_clks + (unsigned int)clks;
  101. base = host->base;
  102. writel(timeout, base + MMCIDATATIMER);
  103. writel(host->size, base + MMCIDATALENGTH);
  104. blksz_bits = ffs(data->blksz) - 1;
  105. BUG_ON(1 << blksz_bits != data->blksz);
  106. datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
  107. if (data->flags & MMC_DATA_READ) {
  108. datactrl |= MCI_DPSM_DIRECTION;
  109. irqmask = MCI_RXFIFOHALFFULLMASK;
  110. /*
  111. * If we have less than a FIFOSIZE of bytes to transfer,
  112. * trigger a PIO interrupt as soon as any data is available.
  113. */
  114. if (host->size < MCI_FIFOSIZE)
  115. irqmask |= MCI_RXDATAAVLBLMASK;
  116. } else {
  117. /*
  118. * We don't actually need to include "FIFO empty" here
  119. * since its implicit in "FIFO half empty".
  120. */
  121. irqmask = MCI_TXFIFOHALFEMPTYMASK;
  122. }
  123. writel(datactrl, base + MMCIDATACTRL);
  124. writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
  125. writel(irqmask, base + MMCIMASK1);
  126. }
  127. static void
  128. mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
  129. {
  130. void __iomem *base = host->base;
  131. dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
  132. cmd->opcode, cmd->arg, cmd->flags);
  133. if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
  134. writel(0, base + MMCICOMMAND);
  135. udelay(1);
  136. }
  137. c |= cmd->opcode | MCI_CPSM_ENABLE;
  138. if (cmd->flags & MMC_RSP_PRESENT) {
  139. if (cmd->flags & MMC_RSP_136)
  140. c |= MCI_CPSM_LONGRSP;
  141. c |= MCI_CPSM_RESPONSE;
  142. }
  143. if (/*interrupt*/0)
  144. c |= MCI_CPSM_INTERRUPT;
  145. host->cmd = cmd;
  146. writel(cmd->arg, base + MMCIARGUMENT);
  147. writel(c, base + MMCICOMMAND);
  148. }
  149. static void
  150. mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
  151. unsigned int status)
  152. {
  153. if (status & MCI_DATABLOCKEND) {
  154. host->data_xfered += data->blksz;
  155. #ifdef CONFIG_ARCH_U300
  156. /*
  157. * On the U300 some signal or other is
  158. * badly routed so that a data write does
  159. * not properly terminate with a MCI_DATAEND
  160. * status flag. This quirk will make writes
  161. * work again.
  162. */
  163. if (data->flags & MMC_DATA_WRITE)
  164. status |= MCI_DATAEND;
  165. #endif
  166. }
  167. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
  168. dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ (status %08x)\n", status);
  169. if (status & MCI_DATACRCFAIL)
  170. data->error = -EILSEQ;
  171. else if (status & MCI_DATATIMEOUT)
  172. data->error = -ETIMEDOUT;
  173. else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
  174. data->error = -EIO;
  175. status |= MCI_DATAEND;
  176. /*
  177. * We hit an error condition. Ensure that any data
  178. * partially written to a page is properly coherent.
  179. */
  180. if (host->sg_len && data->flags & MMC_DATA_READ)
  181. flush_dcache_page(sg_page(host->sg_ptr));
  182. }
  183. if (status & MCI_DATAEND) {
  184. mmci_stop_data(host);
  185. if (!data->stop) {
  186. mmci_request_end(host, data->mrq);
  187. } else {
  188. mmci_start_command(host, data->stop, 0);
  189. }
  190. }
  191. }
  192. static void
  193. mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
  194. unsigned int status)
  195. {
  196. void __iomem *base = host->base;
  197. host->cmd = NULL;
  198. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  199. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  200. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  201. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  202. if (status & MCI_CMDTIMEOUT) {
  203. cmd->error = -ETIMEDOUT;
  204. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  205. cmd->error = -EILSEQ;
  206. }
  207. if (!cmd->data || cmd->error) {
  208. if (host->data)
  209. mmci_stop_data(host);
  210. mmci_request_end(host, cmd->mrq);
  211. } else if (!(cmd->data->flags & MMC_DATA_READ)) {
  212. mmci_start_data(host, cmd->data);
  213. }
  214. }
  215. static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
  216. {
  217. void __iomem *base = host->base;
  218. char *ptr = buffer;
  219. u32 status;
  220. int host_remain = host->size;
  221. do {
  222. int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
  223. if (count > remain)
  224. count = remain;
  225. if (count <= 0)
  226. break;
  227. readsl(base + MMCIFIFO, ptr, count >> 2);
  228. ptr += count;
  229. remain -= count;
  230. host_remain -= count;
  231. if (remain == 0)
  232. break;
  233. status = readl(base + MMCISTATUS);
  234. } while (status & MCI_RXDATAAVLBL);
  235. return ptr - buffer;
  236. }
  237. static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
  238. {
  239. void __iomem *base = host->base;
  240. char *ptr = buffer;
  241. do {
  242. unsigned int count, maxcnt;
  243. maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
  244. count = min(remain, maxcnt);
  245. writesl(base + MMCIFIFO, ptr, count >> 2);
  246. ptr += count;
  247. remain -= count;
  248. if (remain == 0)
  249. break;
  250. status = readl(base + MMCISTATUS);
  251. } while (status & MCI_TXFIFOHALFEMPTY);
  252. return ptr - buffer;
  253. }
  254. /*
  255. * PIO data transfer IRQ handler.
  256. */
  257. static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  258. {
  259. struct mmci_host *host = dev_id;
  260. void __iomem *base = host->base;
  261. u32 status;
  262. status = readl(base + MMCISTATUS);
  263. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  264. do {
  265. unsigned long flags;
  266. unsigned int remain, len;
  267. char *buffer;
  268. /*
  269. * For write, we only need to test the half-empty flag
  270. * here - if the FIFO is completely empty, then by
  271. * definition it is more than half empty.
  272. *
  273. * For read, check for data available.
  274. */
  275. if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
  276. break;
  277. /*
  278. * Map the current scatter buffer.
  279. */
  280. buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
  281. remain = host->sg_ptr->length - host->sg_off;
  282. len = 0;
  283. if (status & MCI_RXACTIVE)
  284. len = mmci_pio_read(host, buffer, remain);
  285. if (status & MCI_TXACTIVE)
  286. len = mmci_pio_write(host, buffer, remain, status);
  287. /*
  288. * Unmap the buffer.
  289. */
  290. mmci_kunmap_atomic(host, buffer, &flags);
  291. host->sg_off += len;
  292. host->size -= len;
  293. remain -= len;
  294. if (remain)
  295. break;
  296. /*
  297. * If we were reading, and we have completed this
  298. * page, ensure that the data cache is coherent.
  299. */
  300. if (status & MCI_RXACTIVE)
  301. flush_dcache_page(sg_page(host->sg_ptr));
  302. if (!mmci_next_sg(host))
  303. break;
  304. status = readl(base + MMCISTATUS);
  305. } while (1);
  306. /*
  307. * If we're nearing the end of the read, switch to
  308. * "any data available" mode.
  309. */
  310. if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
  311. writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
  312. /*
  313. * If we run out of data, disable the data IRQs; this
  314. * prevents a race where the FIFO becomes empty before
  315. * the chip itself has disabled the data path, and
  316. * stops us racing with our data end IRQ.
  317. */
  318. if (host->size == 0) {
  319. writel(0, base + MMCIMASK1);
  320. writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
  321. }
  322. return IRQ_HANDLED;
  323. }
  324. /*
  325. * Handle completion of command and data transfers.
  326. */
  327. static irqreturn_t mmci_irq(int irq, void *dev_id)
  328. {
  329. struct mmci_host *host = dev_id;
  330. u32 status;
  331. int ret = 0;
  332. spin_lock(&host->lock);
  333. do {
  334. struct mmc_command *cmd;
  335. struct mmc_data *data;
  336. status = readl(host->base + MMCISTATUS);
  337. status &= readl(host->base + MMCIMASK0);
  338. writel(status, host->base + MMCICLEAR);
  339. dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
  340. data = host->data;
  341. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
  342. MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
  343. mmci_data_irq(host, data, status);
  344. cmd = host->cmd;
  345. if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
  346. mmci_cmd_irq(host, cmd, status);
  347. ret = 1;
  348. } while (status);
  349. spin_unlock(&host->lock);
  350. return IRQ_RETVAL(ret);
  351. }
  352. static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  353. {
  354. struct mmci_host *host = mmc_priv(mmc);
  355. unsigned long flags;
  356. WARN_ON(host->mrq != NULL);
  357. if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
  358. dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
  359. mrq->data->blksz);
  360. mrq->cmd->error = -EINVAL;
  361. mmc_request_done(mmc, mrq);
  362. return;
  363. }
  364. spin_lock_irqsave(&host->lock, flags);
  365. host->mrq = mrq;
  366. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  367. mmci_start_data(host, mrq->data);
  368. mmci_start_command(host, mrq->cmd, 0);
  369. spin_unlock_irqrestore(&host->lock, flags);
  370. }
  371. static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  372. {
  373. struct mmci_host *host = mmc_priv(mmc);
  374. u32 pwr = 0;
  375. unsigned long flags;
  376. switch (ios->power_mode) {
  377. case MMC_POWER_OFF:
  378. if(host->vcc &&
  379. regulator_is_enabled(host->vcc))
  380. regulator_disable(host->vcc);
  381. break;
  382. case MMC_POWER_UP:
  383. #ifdef CONFIG_REGULATOR
  384. if (host->vcc)
  385. /* This implicitly enables the regulator */
  386. mmc_regulator_set_ocr(host->vcc, ios->vdd);
  387. #endif
  388. /*
  389. * The translate_vdd function is not used if you have
  390. * an external regulator, or your design is really weird.
  391. * Using it would mean sending in power control BOTH using
  392. * a regulator AND the 4 MMCIPWR bits. If we don't have
  393. * a regulator, we might have some other platform specific
  394. * power control behind this translate function.
  395. */
  396. if (!host->vcc && host->plat->translate_vdd)
  397. pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
  398. /* The ST version does not have this, fall through to POWER_ON */
  399. if (host->hw_designer != AMBA_VENDOR_ST) {
  400. pwr |= MCI_PWR_UP;
  401. break;
  402. }
  403. case MMC_POWER_ON:
  404. pwr |= MCI_PWR_ON;
  405. break;
  406. }
  407. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
  408. if (host->hw_designer != AMBA_VENDOR_ST)
  409. pwr |= MCI_ROD;
  410. else {
  411. /*
  412. * The ST Micro variant use the ROD bit for something
  413. * else and only has OD (Open Drain).
  414. */
  415. pwr |= MCI_OD;
  416. }
  417. }
  418. spin_lock_irqsave(&host->lock, flags);
  419. mmci_set_clkreg(host, ios->clock);
  420. if (host->pwr != pwr) {
  421. host->pwr = pwr;
  422. writel(pwr, host->base + MMCIPOWER);
  423. }
  424. spin_unlock_irqrestore(&host->lock, flags);
  425. }
  426. static int mmci_get_ro(struct mmc_host *mmc)
  427. {
  428. struct mmci_host *host = mmc_priv(mmc);
  429. if (host->gpio_wp == -ENOSYS)
  430. return -ENOSYS;
  431. return gpio_get_value(host->gpio_wp);
  432. }
  433. static int mmci_get_cd(struct mmc_host *mmc)
  434. {
  435. struct mmci_host *host = mmc_priv(mmc);
  436. unsigned int status;
  437. if (host->gpio_cd == -ENOSYS)
  438. status = host->plat->status(mmc_dev(host->mmc));
  439. else
  440. status = gpio_get_value(host->gpio_cd);
  441. return !status;
  442. }
  443. static const struct mmc_host_ops mmci_ops = {
  444. .request = mmci_request,
  445. .set_ios = mmci_set_ios,
  446. .get_ro = mmci_get_ro,
  447. .get_cd = mmci_get_cd,
  448. };
  449. static void mmci_check_status(unsigned long data)
  450. {
  451. struct mmci_host *host = (struct mmci_host *)data;
  452. unsigned int status = mmci_get_cd(host->mmc);
  453. if (status ^ host->oldstat)
  454. mmc_detect_change(host->mmc, 0);
  455. host->oldstat = status;
  456. mod_timer(&host->timer, jiffies + HZ);
  457. }
  458. static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
  459. {
  460. struct mmci_platform_data *plat = dev->dev.platform_data;
  461. struct mmci_host *host;
  462. struct mmc_host *mmc;
  463. int ret;
  464. /* must have platform data */
  465. if (!plat) {
  466. ret = -EINVAL;
  467. goto out;
  468. }
  469. ret = amba_request_regions(dev, DRIVER_NAME);
  470. if (ret)
  471. goto out;
  472. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  473. if (!mmc) {
  474. ret = -ENOMEM;
  475. goto rel_regions;
  476. }
  477. host = mmc_priv(mmc);
  478. host->mmc = mmc;
  479. host->gpio_wp = -ENOSYS;
  480. host->gpio_cd = -ENOSYS;
  481. host->hw_designer = amba_manf(dev);
  482. host->hw_revision = amba_rev(dev);
  483. dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
  484. dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
  485. host->clk = clk_get(&dev->dev, NULL);
  486. if (IS_ERR(host->clk)) {
  487. ret = PTR_ERR(host->clk);
  488. host->clk = NULL;
  489. goto host_free;
  490. }
  491. ret = clk_enable(host->clk);
  492. if (ret)
  493. goto clk_free;
  494. host->plat = plat;
  495. host->mclk = clk_get_rate(host->clk);
  496. /*
  497. * According to the spec, mclk is max 100 MHz,
  498. * so we try to adjust the clock down to this,
  499. * (if possible).
  500. */
  501. if (host->mclk > 100000000) {
  502. ret = clk_set_rate(host->clk, 100000000);
  503. if (ret < 0)
  504. goto clk_disable;
  505. host->mclk = clk_get_rate(host->clk);
  506. dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
  507. host->mclk);
  508. }
  509. host->base = ioremap(dev->res.start, resource_size(&dev->res));
  510. if (!host->base) {
  511. ret = -ENOMEM;
  512. goto clk_disable;
  513. }
  514. mmc->ops = &mmci_ops;
  515. mmc->f_min = (host->mclk + 511) / 512;
  516. /*
  517. * If the platform data supplies a maximum operating
  518. * frequency, this takes precedence. Else, we fall back
  519. * to using the module parameter, which has a (low)
  520. * default value in case it is not specified. Either
  521. * value must not exceed the clock rate into the block,
  522. * of course.
  523. */
  524. if (plat->f_max)
  525. mmc->f_max = min(host->mclk, plat->f_max);
  526. else
  527. mmc->f_max = min(host->mclk, fmax);
  528. dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
  529. #ifdef CONFIG_REGULATOR
  530. /* If we're using the regulator framework, try to fetch a regulator */
  531. host->vcc = regulator_get(&dev->dev, "vmmc");
  532. if (IS_ERR(host->vcc))
  533. host->vcc = NULL;
  534. else {
  535. int mask = mmc_regulator_get_ocrmask(host->vcc);
  536. if (mask < 0)
  537. dev_err(&dev->dev, "error getting OCR mask (%d)\n",
  538. mask);
  539. else {
  540. host->mmc->ocr_avail = (u32) mask;
  541. if (plat->ocr_mask)
  542. dev_warn(&dev->dev,
  543. "Provided ocr_mask/setpower will not be used "
  544. "(using regulator instead)\n");
  545. }
  546. }
  547. #endif
  548. /* Fall back to platform data if no regulator is found */
  549. if (host->vcc == NULL)
  550. mmc->ocr_avail = plat->ocr_mask;
  551. mmc->caps = plat->capabilities;
  552. /*
  553. * We can do SGIO
  554. */
  555. mmc->max_hw_segs = 16;
  556. mmc->max_phys_segs = NR_SG;
  557. /*
  558. * Since we only have a 16-bit data length register, we must
  559. * ensure that we don't exceed 2^16-1 bytes in a single request.
  560. */
  561. mmc->max_req_size = 65535;
  562. /*
  563. * Set the maximum segment size. Since we aren't doing DMA
  564. * (yet) we are only limited by the data length register.
  565. */
  566. mmc->max_seg_size = mmc->max_req_size;
  567. /*
  568. * Block size can be up to 2048 bytes, but must be a power of two.
  569. */
  570. mmc->max_blk_size = 2048;
  571. /*
  572. * No limit on the number of blocks transferred.
  573. */
  574. mmc->max_blk_count = mmc->max_req_size;
  575. spin_lock_init(&host->lock);
  576. writel(0, host->base + MMCIMASK0);
  577. writel(0, host->base + MMCIMASK1);
  578. writel(0xfff, host->base + MMCICLEAR);
  579. if (gpio_is_valid(plat->gpio_cd)) {
  580. ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
  581. if (ret == 0)
  582. ret = gpio_direction_input(plat->gpio_cd);
  583. if (ret == 0)
  584. host->gpio_cd = plat->gpio_cd;
  585. else if (ret != -ENOSYS)
  586. goto err_gpio_cd;
  587. }
  588. if (gpio_is_valid(plat->gpio_wp)) {
  589. ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
  590. if (ret == 0)
  591. ret = gpio_direction_input(plat->gpio_wp);
  592. if (ret == 0)
  593. host->gpio_wp = plat->gpio_wp;
  594. else if (ret != -ENOSYS)
  595. goto err_gpio_wp;
  596. }
  597. ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
  598. if (ret)
  599. goto unmap;
  600. ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
  601. if (ret)
  602. goto irq0_free;
  603. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  604. amba_set_drvdata(dev, mmc);
  605. host->oldstat = mmci_get_cd(host->mmc);
  606. mmc_add_host(mmc);
  607. dev_info(&dev->dev, "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
  608. mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
  609. (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
  610. init_timer(&host->timer);
  611. host->timer.data = (unsigned long)host;
  612. host->timer.function = mmci_check_status;
  613. host->timer.expires = jiffies + HZ;
  614. add_timer(&host->timer);
  615. return 0;
  616. irq0_free:
  617. free_irq(dev->irq[0], host);
  618. unmap:
  619. if (host->gpio_wp != -ENOSYS)
  620. gpio_free(host->gpio_wp);
  621. err_gpio_wp:
  622. if (host->gpio_cd != -ENOSYS)
  623. gpio_free(host->gpio_cd);
  624. err_gpio_cd:
  625. iounmap(host->base);
  626. clk_disable:
  627. clk_disable(host->clk);
  628. clk_free:
  629. clk_put(host->clk);
  630. host_free:
  631. mmc_free_host(mmc);
  632. rel_regions:
  633. amba_release_regions(dev);
  634. out:
  635. return ret;
  636. }
  637. static int __devexit mmci_remove(struct amba_device *dev)
  638. {
  639. struct mmc_host *mmc = amba_get_drvdata(dev);
  640. amba_set_drvdata(dev, NULL);
  641. if (mmc) {
  642. struct mmci_host *host = mmc_priv(mmc);
  643. del_timer_sync(&host->timer);
  644. mmc_remove_host(mmc);
  645. writel(0, host->base + MMCIMASK0);
  646. writel(0, host->base + MMCIMASK1);
  647. writel(0, host->base + MMCICOMMAND);
  648. writel(0, host->base + MMCIDATACTRL);
  649. free_irq(dev->irq[0], host);
  650. free_irq(dev->irq[1], host);
  651. if (host->gpio_wp != -ENOSYS)
  652. gpio_free(host->gpio_wp);
  653. if (host->gpio_cd != -ENOSYS)
  654. gpio_free(host->gpio_cd);
  655. iounmap(host->base);
  656. clk_disable(host->clk);
  657. clk_put(host->clk);
  658. if (regulator_is_enabled(host->vcc))
  659. regulator_disable(host->vcc);
  660. regulator_put(host->vcc);
  661. mmc_free_host(mmc);
  662. amba_release_regions(dev);
  663. }
  664. return 0;
  665. }
  666. #ifdef CONFIG_PM
  667. static int mmci_suspend(struct amba_device *dev, pm_message_t state)
  668. {
  669. struct mmc_host *mmc = amba_get_drvdata(dev);
  670. int ret = 0;
  671. if (mmc) {
  672. struct mmci_host *host = mmc_priv(mmc);
  673. ret = mmc_suspend_host(mmc);
  674. if (ret == 0)
  675. writel(0, host->base + MMCIMASK0);
  676. }
  677. return ret;
  678. }
  679. static int mmci_resume(struct amba_device *dev)
  680. {
  681. struct mmc_host *mmc = amba_get_drvdata(dev);
  682. int ret = 0;
  683. if (mmc) {
  684. struct mmci_host *host = mmc_priv(mmc);
  685. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  686. ret = mmc_resume_host(mmc);
  687. }
  688. return ret;
  689. }
  690. #else
  691. #define mmci_suspend NULL
  692. #define mmci_resume NULL
  693. #endif
  694. static struct amba_id mmci_ids[] = {
  695. {
  696. .id = 0x00041180,
  697. .mask = 0x000fffff,
  698. },
  699. {
  700. .id = 0x00041181,
  701. .mask = 0x000fffff,
  702. },
  703. /* ST Micro variants */
  704. {
  705. .id = 0x00180180,
  706. .mask = 0x00ffffff,
  707. },
  708. {
  709. .id = 0x00280180,
  710. .mask = 0x00ffffff,
  711. },
  712. { 0, 0 },
  713. };
  714. static struct amba_driver mmci_driver = {
  715. .drv = {
  716. .name = DRIVER_NAME,
  717. },
  718. .probe = mmci_probe,
  719. .remove = __devexit_p(mmci_remove),
  720. .suspend = mmci_suspend,
  721. .resume = mmci_resume,
  722. .id_table = mmci_ids,
  723. };
  724. static int __init mmci_init(void)
  725. {
  726. return amba_driver_register(&mmci_driver);
  727. }
  728. static void __exit mmci_exit(void)
  729. {
  730. amba_driver_unregister(&mmci_driver);
  731. }
  732. module_init(mmci_init);
  733. module_exit(mmci_exit);
  734. module_param(fmax, uint, 0444);
  735. MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
  736. MODULE_LICENSE("GPL");