mmci.c 21 KB

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