mmci.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. unsigned int status;
  462. if (host->gpio_cd == -ENOSYS)
  463. status = host->plat->status(mmc_dev(host->mmc));
  464. else
  465. status = !gpio_get_value(host->gpio_cd);
  466. /*
  467. * Use positive logic throughout - status is zero for no card,
  468. * non-zero for card inserted.
  469. */
  470. return status;
  471. }
  472. static const struct mmc_host_ops mmci_ops = {
  473. .request = mmci_request,
  474. .set_ios = mmci_set_ios,
  475. .get_ro = mmci_get_ro,
  476. .get_cd = mmci_get_cd,
  477. };
  478. static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
  479. {
  480. struct mmci_platform_data *plat = dev->dev.platform_data;
  481. struct variant_data *variant = id->data;
  482. struct mmci_host *host;
  483. struct mmc_host *mmc;
  484. int ret;
  485. /* must have platform data */
  486. if (!plat) {
  487. ret = -EINVAL;
  488. goto out;
  489. }
  490. ret = amba_request_regions(dev, DRIVER_NAME);
  491. if (ret)
  492. goto out;
  493. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  494. if (!mmc) {
  495. ret = -ENOMEM;
  496. goto rel_regions;
  497. }
  498. host = mmc_priv(mmc);
  499. host->mmc = mmc;
  500. host->gpio_wp = -ENOSYS;
  501. host->gpio_cd = -ENOSYS;
  502. host->hw_designer = amba_manf(dev);
  503. host->hw_revision = amba_rev(dev);
  504. dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
  505. dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
  506. host->clk = clk_get(&dev->dev, NULL);
  507. if (IS_ERR(host->clk)) {
  508. ret = PTR_ERR(host->clk);
  509. host->clk = NULL;
  510. goto host_free;
  511. }
  512. ret = clk_enable(host->clk);
  513. if (ret)
  514. goto clk_free;
  515. host->plat = plat;
  516. host->variant = variant;
  517. host->mclk = clk_get_rate(host->clk);
  518. /*
  519. * According to the spec, mclk is max 100 MHz,
  520. * so we try to adjust the clock down to this,
  521. * (if possible).
  522. */
  523. if (host->mclk > 100000000) {
  524. ret = clk_set_rate(host->clk, 100000000);
  525. if (ret < 0)
  526. goto clk_disable;
  527. host->mclk = clk_get_rate(host->clk);
  528. dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
  529. host->mclk);
  530. }
  531. host->base = ioremap(dev->res.start, resource_size(&dev->res));
  532. if (!host->base) {
  533. ret = -ENOMEM;
  534. goto clk_disable;
  535. }
  536. mmc->ops = &mmci_ops;
  537. mmc->f_min = (host->mclk + 511) / 512;
  538. /*
  539. * If the platform data supplies a maximum operating
  540. * frequency, this takes precedence. Else, we fall back
  541. * to using the module parameter, which has a (low)
  542. * default value in case it is not specified. Either
  543. * value must not exceed the clock rate into the block,
  544. * of course.
  545. */
  546. if (plat->f_max)
  547. mmc->f_max = min(host->mclk, plat->f_max);
  548. else
  549. mmc->f_max = min(host->mclk, fmax);
  550. dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
  551. #ifdef CONFIG_REGULATOR
  552. /* If we're using the regulator framework, try to fetch a regulator */
  553. host->vcc = regulator_get(&dev->dev, "vmmc");
  554. if (IS_ERR(host->vcc))
  555. host->vcc = NULL;
  556. else {
  557. int mask = mmc_regulator_get_ocrmask(host->vcc);
  558. if (mask < 0)
  559. dev_err(&dev->dev, "error getting OCR mask (%d)\n",
  560. mask);
  561. else {
  562. host->mmc->ocr_avail = (u32) mask;
  563. if (plat->ocr_mask)
  564. dev_warn(&dev->dev,
  565. "Provided ocr_mask/setpower will not be used "
  566. "(using regulator instead)\n");
  567. }
  568. }
  569. #endif
  570. /* Fall back to platform data if no regulator is found */
  571. if (host->vcc == NULL)
  572. mmc->ocr_avail = plat->ocr_mask;
  573. mmc->caps = plat->capabilities;
  574. mmc->caps |= MMC_CAP_NEEDS_POLL;
  575. /*
  576. * We can do SGIO
  577. */
  578. mmc->max_hw_segs = 16;
  579. mmc->max_phys_segs = NR_SG;
  580. /*
  581. * Since only a certain number of bits are valid in the data length
  582. * register, we must ensure that we don't exceed 2^num-1 bytes in a
  583. * single request.
  584. */
  585. mmc->max_req_size = (1 << variant->datalength_bits) - 1;
  586. /*
  587. * Set the maximum segment size. Since we aren't doing DMA
  588. * (yet) we are only limited by the data length register.
  589. */
  590. mmc->max_seg_size = mmc->max_req_size;
  591. /*
  592. * Block size can be up to 2048 bytes, but must be a power of two.
  593. */
  594. mmc->max_blk_size = 2048;
  595. /*
  596. * No limit on the number of blocks transferred.
  597. */
  598. mmc->max_blk_count = mmc->max_req_size;
  599. spin_lock_init(&host->lock);
  600. writel(0, host->base + MMCIMASK0);
  601. writel(0, host->base + MMCIMASK1);
  602. writel(0xfff, host->base + MMCICLEAR);
  603. if (gpio_is_valid(plat->gpio_cd)) {
  604. ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
  605. if (ret == 0)
  606. ret = gpio_direction_input(plat->gpio_cd);
  607. if (ret == 0)
  608. host->gpio_cd = plat->gpio_cd;
  609. else if (ret != -ENOSYS)
  610. goto err_gpio_cd;
  611. }
  612. if (gpio_is_valid(plat->gpio_wp)) {
  613. ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
  614. if (ret == 0)
  615. ret = gpio_direction_input(plat->gpio_wp);
  616. if (ret == 0)
  617. host->gpio_wp = plat->gpio_wp;
  618. else if (ret != -ENOSYS)
  619. goto err_gpio_wp;
  620. }
  621. ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
  622. if (ret)
  623. goto unmap;
  624. ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
  625. if (ret)
  626. goto irq0_free;
  627. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  628. amba_set_drvdata(dev, mmc);
  629. mmc_add_host(mmc);
  630. dev_info(&dev->dev, "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
  631. mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
  632. (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
  633. return 0;
  634. irq0_free:
  635. free_irq(dev->irq[0], host);
  636. unmap:
  637. if (host->gpio_wp != -ENOSYS)
  638. gpio_free(host->gpio_wp);
  639. err_gpio_wp:
  640. if (host->gpio_cd != -ENOSYS)
  641. gpio_free(host->gpio_cd);
  642. err_gpio_cd:
  643. iounmap(host->base);
  644. clk_disable:
  645. clk_disable(host->clk);
  646. clk_free:
  647. clk_put(host->clk);
  648. host_free:
  649. mmc_free_host(mmc);
  650. rel_regions:
  651. amba_release_regions(dev);
  652. out:
  653. return ret;
  654. }
  655. static int __devexit mmci_remove(struct amba_device *dev)
  656. {
  657. struct mmc_host *mmc = amba_get_drvdata(dev);
  658. amba_set_drvdata(dev, NULL);
  659. if (mmc) {
  660. struct mmci_host *host = mmc_priv(mmc);
  661. mmc_remove_host(mmc);
  662. writel(0, host->base + MMCIMASK0);
  663. writel(0, host->base + MMCIMASK1);
  664. writel(0, host->base + MMCICOMMAND);
  665. writel(0, host->base + MMCIDATACTRL);
  666. free_irq(dev->irq[0], host);
  667. free_irq(dev->irq[1], host);
  668. if (host->gpio_wp != -ENOSYS)
  669. gpio_free(host->gpio_wp);
  670. if (host->gpio_cd != -ENOSYS)
  671. gpio_free(host->gpio_cd);
  672. iounmap(host->base);
  673. clk_disable(host->clk);
  674. clk_put(host->clk);
  675. if (regulator_is_enabled(host->vcc))
  676. regulator_disable(host->vcc);
  677. regulator_put(host->vcc);
  678. mmc_free_host(mmc);
  679. amba_release_regions(dev);
  680. }
  681. return 0;
  682. }
  683. #ifdef CONFIG_PM
  684. static int mmci_suspend(struct amba_device *dev, pm_message_t state)
  685. {
  686. struct mmc_host *mmc = amba_get_drvdata(dev);
  687. int ret = 0;
  688. if (mmc) {
  689. struct mmci_host *host = mmc_priv(mmc);
  690. ret = mmc_suspend_host(mmc);
  691. if (ret == 0)
  692. writel(0, host->base + MMCIMASK0);
  693. }
  694. return ret;
  695. }
  696. static int mmci_resume(struct amba_device *dev)
  697. {
  698. struct mmc_host *mmc = amba_get_drvdata(dev);
  699. int ret = 0;
  700. if (mmc) {
  701. struct mmci_host *host = mmc_priv(mmc);
  702. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  703. ret = mmc_resume_host(mmc);
  704. }
  705. return ret;
  706. }
  707. #else
  708. #define mmci_suspend NULL
  709. #define mmci_resume NULL
  710. #endif
  711. static struct amba_id mmci_ids[] = {
  712. {
  713. .id = 0x00041180,
  714. .mask = 0x000fffff,
  715. .data = &variant_arm,
  716. },
  717. {
  718. .id = 0x00041181,
  719. .mask = 0x000fffff,
  720. .data = &variant_arm,
  721. },
  722. /* ST Micro variants */
  723. {
  724. .id = 0x00180180,
  725. .mask = 0x00ffffff,
  726. .data = &variant_u300,
  727. },
  728. {
  729. .id = 0x00280180,
  730. .mask = 0x00ffffff,
  731. .data = &variant_u300,
  732. },
  733. {
  734. .id = 0x00480180,
  735. .mask = 0x00ffffff,
  736. .data = &variant_ux500,
  737. },
  738. { 0, 0 },
  739. };
  740. static struct amba_driver mmci_driver = {
  741. .drv = {
  742. .name = DRIVER_NAME,
  743. },
  744. .probe = mmci_probe,
  745. .remove = __devexit_p(mmci_remove),
  746. .suspend = mmci_suspend,
  747. .resume = mmci_resume,
  748. .id_table = mmci_ids,
  749. };
  750. static int __init mmci_init(void)
  751. {
  752. return amba_driver_register(&mmci_driver);
  753. }
  754. static void __exit mmci_exit(void)
  755. {
  756. amba_driver_unregister(&mmci_driver);
  757. }
  758. module_init(mmci_init);
  759. module_exit(mmci_exit);
  760. module_param(fmax, uint, 0444);
  761. MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
  762. MODULE_LICENSE("GPL");