mmci.c 22 KB

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