mmci.c 21 KB

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