mmci.c 19 KB

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