mmci.c 20 KB

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