mmci.c 25 KB

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