mmci.c 20 KB

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