mmci.c 18 KB

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