mmci.c 22 KB

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