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. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  289. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  290. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  291. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  292. if (status & MCI_CMDTIMEOUT) {
  293. cmd->error = -ETIMEDOUT;
  294. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  295. cmd->error = -EILSEQ;
  296. }
  297. if (!cmd->data || cmd->error) {
  298. if (host->data)
  299. mmci_stop_data(host);
  300. mmci_request_end(host, cmd->mrq);
  301. } else if (!(cmd->data->flags & MMC_DATA_READ)) {
  302. mmci_start_data(host, cmd->data);
  303. }
  304. }
  305. static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
  306. {
  307. void __iomem *base = host->base;
  308. char *ptr = buffer;
  309. u32 status;
  310. int host_remain = host->size;
  311. do {
  312. int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
  313. if (count > remain)
  314. count = remain;
  315. if (count <= 0)
  316. break;
  317. readsl(base + MMCIFIFO, ptr, count >> 2);
  318. ptr += count;
  319. remain -= count;
  320. host_remain -= count;
  321. if (remain == 0)
  322. break;
  323. status = readl(base + MMCISTATUS);
  324. } while (status & MCI_RXDATAAVLBL);
  325. return ptr - buffer;
  326. }
  327. static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
  328. {
  329. struct variant_data *variant = host->variant;
  330. void __iomem *base = host->base;
  331. char *ptr = buffer;
  332. do {
  333. unsigned int count, maxcnt;
  334. maxcnt = status & MCI_TXFIFOEMPTY ?
  335. variant->fifosize : variant->fifohalfsize;
  336. count = min(remain, maxcnt);
  337. /*
  338. * The ST Micro variant for SDIO transfer sizes
  339. * less then 8 bytes should have clock H/W flow
  340. * control disabled.
  341. */
  342. if (variant->sdio &&
  343. mmc_card_sdio(host->mmc->card)) {
  344. if (count < 8)
  345. writel(readl(host->base + MMCICLOCK) &
  346. ~variant->clkreg_enable,
  347. host->base + MMCICLOCK);
  348. else
  349. writel(readl(host->base + MMCICLOCK) |
  350. variant->clkreg_enable,
  351. host->base + MMCICLOCK);
  352. }
  353. /*
  354. * SDIO especially may want to send something that is
  355. * not divisible by 4 (as opposed to card sectors
  356. * etc), and the FIFO only accept full 32-bit writes.
  357. * So compensate by adding +3 on the count, a single
  358. * byte become a 32bit write, 7 bytes will be two
  359. * 32bit writes etc.
  360. */
  361. writesl(base + MMCIFIFO, ptr, (count + 3) >> 2);
  362. ptr += count;
  363. remain -= count;
  364. if (remain == 0)
  365. break;
  366. status = readl(base + MMCISTATUS);
  367. } while (status & MCI_TXFIFOHALFEMPTY);
  368. return ptr - buffer;
  369. }
  370. /*
  371. * PIO data transfer IRQ handler.
  372. */
  373. static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  374. {
  375. struct mmci_host *host = dev_id;
  376. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  377. struct variant_data *variant = host->variant;
  378. void __iomem *base = host->base;
  379. unsigned long flags;
  380. u32 status;
  381. status = readl(base + MMCISTATUS);
  382. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  383. local_irq_save(flags);
  384. do {
  385. unsigned int remain, len;
  386. char *buffer;
  387. /*
  388. * For write, we only need to test the half-empty flag
  389. * here - if the FIFO is completely empty, then by
  390. * definition it is more than half empty.
  391. *
  392. * For read, check for data available.
  393. */
  394. if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
  395. break;
  396. if (!sg_miter_next(sg_miter))
  397. break;
  398. buffer = sg_miter->addr;
  399. remain = sg_miter->length;
  400. len = 0;
  401. if (status & MCI_RXACTIVE)
  402. len = mmci_pio_read(host, buffer, remain);
  403. if (status & MCI_TXACTIVE)
  404. len = mmci_pio_write(host, buffer, remain, status);
  405. sg_miter->consumed = len;
  406. host->size -= len;
  407. remain -= len;
  408. if (remain)
  409. break;
  410. if (status & MCI_RXACTIVE)
  411. flush_dcache_page(sg_miter->page);
  412. status = readl(base + MMCISTATUS);
  413. } while (1);
  414. sg_miter_stop(sg_miter);
  415. local_irq_restore(flags);
  416. /*
  417. * If we're nearing the end of the read, switch to
  418. * "any data available" mode.
  419. */
  420. if (status & MCI_RXACTIVE && host->size < variant->fifosize)
  421. mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
  422. /*
  423. * If we run out of data, disable the data IRQs; this
  424. * prevents a race where the FIFO becomes empty before
  425. * the chip itself has disabled the data path, and
  426. * stops us racing with our data end IRQ.
  427. */
  428. if (host->size == 0) {
  429. mmci_set_mask1(host, 0);
  430. writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
  431. }
  432. return IRQ_HANDLED;
  433. }
  434. /*
  435. * Handle completion of command and data transfers.
  436. */
  437. static irqreturn_t mmci_irq(int irq, void *dev_id)
  438. {
  439. struct mmci_host *host = dev_id;
  440. u32 status;
  441. int ret = 0;
  442. spin_lock(&host->lock);
  443. do {
  444. struct mmc_command *cmd;
  445. struct mmc_data *data;
  446. status = readl(host->base + MMCISTATUS);
  447. if (host->singleirq) {
  448. if (status & readl(host->base + MMCIMASK1))
  449. mmci_pio_irq(irq, dev_id);
  450. status &= ~MCI_IRQ1MASK;
  451. }
  452. status &= readl(host->base + MMCIMASK0);
  453. writel(status, host->base + MMCICLEAR);
  454. dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
  455. data = host->data;
  456. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
  457. MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
  458. mmci_data_irq(host, data, status);
  459. cmd = host->cmd;
  460. if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
  461. mmci_cmd_irq(host, cmd, status);
  462. ret = 1;
  463. } while (status);
  464. spin_unlock(&host->lock);
  465. return IRQ_RETVAL(ret);
  466. }
  467. static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  468. {
  469. struct mmci_host *host = mmc_priv(mmc);
  470. unsigned long flags;
  471. WARN_ON(host->mrq != NULL);
  472. if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
  473. dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
  474. mrq->data->blksz);
  475. mrq->cmd->error = -EINVAL;
  476. mmc_request_done(mmc, mrq);
  477. return;
  478. }
  479. spin_lock_irqsave(&host->lock, flags);
  480. host->mrq = mrq;
  481. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  482. mmci_start_data(host, mrq->data);
  483. mmci_start_command(host, mrq->cmd, 0);
  484. spin_unlock_irqrestore(&host->lock, flags);
  485. }
  486. static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  487. {
  488. struct mmci_host *host = mmc_priv(mmc);
  489. u32 pwr = 0;
  490. unsigned long flags;
  491. int ret;
  492. switch (ios->power_mode) {
  493. case MMC_POWER_OFF:
  494. if (host->vcc)
  495. ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
  496. break;
  497. case MMC_POWER_UP:
  498. if (host->vcc) {
  499. ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
  500. if (ret) {
  501. dev_err(mmc_dev(mmc), "unable to set OCR\n");
  502. /*
  503. * The .set_ios() function in the mmc_host_ops
  504. * struct return void, and failing to set the
  505. * power should be rare so we print an error
  506. * and return here.
  507. */
  508. return;
  509. }
  510. }
  511. if (host->plat->vdd_handler)
  512. pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
  513. ios->power_mode);
  514. /* The ST version does not have this, fall through to POWER_ON */
  515. if (host->hw_designer != AMBA_VENDOR_ST) {
  516. pwr |= MCI_PWR_UP;
  517. break;
  518. }
  519. case MMC_POWER_ON:
  520. pwr |= MCI_PWR_ON;
  521. break;
  522. }
  523. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
  524. if (host->hw_designer != AMBA_VENDOR_ST)
  525. pwr |= MCI_ROD;
  526. else {
  527. /*
  528. * The ST Micro variant use the ROD bit for something
  529. * else and only has OD (Open Drain).
  530. */
  531. pwr |= MCI_OD;
  532. }
  533. }
  534. spin_lock_irqsave(&host->lock, flags);
  535. mmci_set_clkreg(host, ios->clock);
  536. if (host->pwr != pwr) {
  537. host->pwr = pwr;
  538. writel(pwr, host->base + MMCIPOWER);
  539. }
  540. spin_unlock_irqrestore(&host->lock, flags);
  541. }
  542. static int mmci_get_ro(struct mmc_host *mmc)
  543. {
  544. struct mmci_host *host = mmc_priv(mmc);
  545. if (host->gpio_wp == -ENOSYS)
  546. return -ENOSYS;
  547. return gpio_get_value_cansleep(host->gpio_wp);
  548. }
  549. static int mmci_get_cd(struct mmc_host *mmc)
  550. {
  551. struct mmci_host *host = mmc_priv(mmc);
  552. struct mmci_platform_data *plat = host->plat;
  553. unsigned int status;
  554. if (host->gpio_cd == -ENOSYS) {
  555. if (!plat->status)
  556. return 1; /* Assume always present */
  557. status = plat->status(mmc_dev(host->mmc));
  558. } else
  559. status = !!gpio_get_value_cansleep(host->gpio_cd)
  560. ^ plat->cd_invert;
  561. /*
  562. * Use positive logic throughout - status is zero for no card,
  563. * non-zero for card inserted.
  564. */
  565. return status;
  566. }
  567. static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
  568. {
  569. struct mmci_host *host = dev_id;
  570. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  571. return IRQ_HANDLED;
  572. }
  573. static const struct mmc_host_ops mmci_ops = {
  574. .request = mmci_request,
  575. .set_ios = mmci_set_ios,
  576. .get_ro = mmci_get_ro,
  577. .get_cd = mmci_get_cd,
  578. };
  579. static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
  580. {
  581. struct mmci_platform_data *plat = dev->dev.platform_data;
  582. struct variant_data *variant = id->data;
  583. struct mmci_host *host;
  584. struct mmc_host *mmc;
  585. int ret;
  586. /* must have platform data */
  587. if (!plat) {
  588. ret = -EINVAL;
  589. goto out;
  590. }
  591. ret = amba_request_regions(dev, DRIVER_NAME);
  592. if (ret)
  593. goto out;
  594. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  595. if (!mmc) {
  596. ret = -ENOMEM;
  597. goto rel_regions;
  598. }
  599. host = mmc_priv(mmc);
  600. host->mmc = mmc;
  601. host->gpio_wp = -ENOSYS;
  602. host->gpio_cd = -ENOSYS;
  603. host->gpio_cd_irq = -1;
  604. host->hw_designer = amba_manf(dev);
  605. host->hw_revision = amba_rev(dev);
  606. dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
  607. dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
  608. host->clk = clk_get(&dev->dev, NULL);
  609. if (IS_ERR(host->clk)) {
  610. ret = PTR_ERR(host->clk);
  611. host->clk = NULL;
  612. goto host_free;
  613. }
  614. ret = clk_enable(host->clk);
  615. if (ret)
  616. goto clk_free;
  617. host->plat = plat;
  618. host->variant = variant;
  619. host->mclk = clk_get_rate(host->clk);
  620. /*
  621. * According to the spec, mclk is max 100 MHz,
  622. * so we try to adjust the clock down to this,
  623. * (if possible).
  624. */
  625. if (host->mclk > 100000000) {
  626. ret = clk_set_rate(host->clk, 100000000);
  627. if (ret < 0)
  628. goto clk_disable;
  629. host->mclk = clk_get_rate(host->clk);
  630. dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
  631. host->mclk);
  632. }
  633. host->base = ioremap(dev->res.start, resource_size(&dev->res));
  634. if (!host->base) {
  635. ret = -ENOMEM;
  636. goto clk_disable;
  637. }
  638. mmc->ops = &mmci_ops;
  639. mmc->f_min = (host->mclk + 511) / 512;
  640. /*
  641. * If the platform data supplies a maximum operating
  642. * frequency, this takes precedence. Else, we fall back
  643. * to using the module parameter, which has a (low)
  644. * default value in case it is not specified. Either
  645. * value must not exceed the clock rate into the block,
  646. * of course.
  647. */
  648. if (plat->f_max)
  649. mmc->f_max = min(host->mclk, plat->f_max);
  650. else
  651. mmc->f_max = min(host->mclk, fmax);
  652. dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
  653. #ifdef CONFIG_REGULATOR
  654. /* If we're using the regulator framework, try to fetch a regulator */
  655. host->vcc = regulator_get(&dev->dev, "vmmc");
  656. if (IS_ERR(host->vcc))
  657. host->vcc = NULL;
  658. else {
  659. int mask = mmc_regulator_get_ocrmask(host->vcc);
  660. if (mask < 0)
  661. dev_err(&dev->dev, "error getting OCR mask (%d)\n",
  662. mask);
  663. else {
  664. host->mmc->ocr_avail = (u32) mask;
  665. if (plat->ocr_mask)
  666. dev_warn(&dev->dev,
  667. "Provided ocr_mask/setpower will not be used "
  668. "(using regulator instead)\n");
  669. }
  670. }
  671. #endif
  672. /* Fall back to platform data if no regulator is found */
  673. if (host->vcc == NULL)
  674. mmc->ocr_avail = plat->ocr_mask;
  675. mmc->caps = plat->capabilities;
  676. /*
  677. * We can do SGIO
  678. */
  679. mmc->max_segs = NR_SG;
  680. /*
  681. * Since only a certain number of bits are valid in the data length
  682. * register, we must ensure that we don't exceed 2^num-1 bytes in a
  683. * single request.
  684. */
  685. mmc->max_req_size = (1 << variant->datalength_bits) - 1;
  686. /*
  687. * Set the maximum segment size. Since we aren't doing DMA
  688. * (yet) we are only limited by the data length register.
  689. */
  690. mmc->max_seg_size = mmc->max_req_size;
  691. /*
  692. * Block size can be up to 2048 bytes, but must be a power of two.
  693. */
  694. mmc->max_blk_size = 2048;
  695. /*
  696. * No limit on the number of blocks transferred.
  697. */
  698. mmc->max_blk_count = mmc->max_req_size;
  699. spin_lock_init(&host->lock);
  700. writel(0, host->base + MMCIMASK0);
  701. writel(0, host->base + MMCIMASK1);
  702. writel(0xfff, host->base + MMCICLEAR);
  703. if (gpio_is_valid(plat->gpio_cd)) {
  704. ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
  705. if (ret == 0)
  706. ret = gpio_direction_input(plat->gpio_cd);
  707. if (ret == 0)
  708. host->gpio_cd = plat->gpio_cd;
  709. else if (ret != -ENOSYS)
  710. goto err_gpio_cd;
  711. ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
  712. mmci_cd_irq, 0,
  713. DRIVER_NAME " (cd)", host);
  714. if (ret >= 0)
  715. host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
  716. }
  717. if (gpio_is_valid(plat->gpio_wp)) {
  718. ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
  719. if (ret == 0)
  720. ret = gpio_direction_input(plat->gpio_wp);
  721. if (ret == 0)
  722. host->gpio_wp = plat->gpio_wp;
  723. else if (ret != -ENOSYS)
  724. goto err_gpio_wp;
  725. }
  726. if ((host->plat->status || host->gpio_cd != -ENOSYS)
  727. && host->gpio_cd_irq < 0)
  728. mmc->caps |= MMC_CAP_NEEDS_POLL;
  729. ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
  730. if (ret)
  731. goto unmap;
  732. if (dev->irq[1] == NO_IRQ)
  733. host->singleirq = true;
  734. else {
  735. ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
  736. DRIVER_NAME " (pio)", host);
  737. if (ret)
  738. goto irq0_free;
  739. }
  740. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  741. amba_set_drvdata(dev, mmc);
  742. dev_info(&dev->dev, "%s: PL%03x rev%u at 0x%08llx irq %d,%d\n",
  743. mmc_hostname(mmc), amba_part(dev), amba_rev(dev),
  744. (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
  745. mmc_add_host(mmc);
  746. return 0;
  747. irq0_free:
  748. free_irq(dev->irq[0], host);
  749. unmap:
  750. if (host->gpio_wp != -ENOSYS)
  751. gpio_free(host->gpio_wp);
  752. err_gpio_wp:
  753. if (host->gpio_cd_irq >= 0)
  754. free_irq(host->gpio_cd_irq, host);
  755. if (host->gpio_cd != -ENOSYS)
  756. gpio_free(host->gpio_cd);
  757. err_gpio_cd:
  758. iounmap(host->base);
  759. clk_disable:
  760. clk_disable(host->clk);
  761. clk_free:
  762. clk_put(host->clk);
  763. host_free:
  764. mmc_free_host(mmc);
  765. rel_regions:
  766. amba_release_regions(dev);
  767. out:
  768. return ret;
  769. }
  770. static int __devexit mmci_remove(struct amba_device *dev)
  771. {
  772. struct mmc_host *mmc = amba_get_drvdata(dev);
  773. amba_set_drvdata(dev, NULL);
  774. if (mmc) {
  775. struct mmci_host *host = mmc_priv(mmc);
  776. mmc_remove_host(mmc);
  777. writel(0, host->base + MMCIMASK0);
  778. writel(0, host->base + MMCIMASK1);
  779. writel(0, host->base + MMCICOMMAND);
  780. writel(0, host->base + MMCIDATACTRL);
  781. free_irq(dev->irq[0], host);
  782. if (!host->singleirq)
  783. free_irq(dev->irq[1], host);
  784. if (host->gpio_wp != -ENOSYS)
  785. gpio_free(host->gpio_wp);
  786. if (host->gpio_cd_irq >= 0)
  787. free_irq(host->gpio_cd_irq, host);
  788. if (host->gpio_cd != -ENOSYS)
  789. gpio_free(host->gpio_cd);
  790. iounmap(host->base);
  791. clk_disable(host->clk);
  792. clk_put(host->clk);
  793. if (host->vcc)
  794. mmc_regulator_set_ocr(mmc, host->vcc, 0);
  795. regulator_put(host->vcc);
  796. mmc_free_host(mmc);
  797. amba_release_regions(dev);
  798. }
  799. return 0;
  800. }
  801. #ifdef CONFIG_PM
  802. static int mmci_suspend(struct amba_device *dev, pm_message_t state)
  803. {
  804. struct mmc_host *mmc = amba_get_drvdata(dev);
  805. int ret = 0;
  806. if (mmc) {
  807. struct mmci_host *host = mmc_priv(mmc);
  808. ret = mmc_suspend_host(mmc);
  809. if (ret == 0)
  810. writel(0, host->base + MMCIMASK0);
  811. }
  812. return ret;
  813. }
  814. static int mmci_resume(struct amba_device *dev)
  815. {
  816. struct mmc_host *mmc = amba_get_drvdata(dev);
  817. int ret = 0;
  818. if (mmc) {
  819. struct mmci_host *host = mmc_priv(mmc);
  820. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  821. ret = mmc_resume_host(mmc);
  822. }
  823. return ret;
  824. }
  825. #else
  826. #define mmci_suspend NULL
  827. #define mmci_resume NULL
  828. #endif
  829. static struct amba_id mmci_ids[] = {
  830. {
  831. .id = 0x00041180,
  832. .mask = 0x000fffff,
  833. .data = &variant_arm,
  834. },
  835. {
  836. .id = 0x00041181,
  837. .mask = 0x000fffff,
  838. .data = &variant_arm,
  839. },
  840. /* ST Micro variants */
  841. {
  842. .id = 0x00180180,
  843. .mask = 0x00ffffff,
  844. .data = &variant_u300,
  845. },
  846. {
  847. .id = 0x00280180,
  848. .mask = 0x00ffffff,
  849. .data = &variant_u300,
  850. },
  851. {
  852. .id = 0x00480180,
  853. .mask = 0x00ffffff,
  854. .data = &variant_ux500,
  855. },
  856. { 0, 0 },
  857. };
  858. static struct amba_driver mmci_driver = {
  859. .drv = {
  860. .name = DRIVER_NAME,
  861. },
  862. .probe = mmci_probe,
  863. .remove = __devexit_p(mmci_remove),
  864. .suspend = mmci_suspend,
  865. .resume = mmci_resume,
  866. .id_table = mmci_ids,
  867. };
  868. static int __init mmci_init(void)
  869. {
  870. return amba_driver_register(&mmci_driver);
  871. }
  872. static void __exit mmci_exit(void)
  873. {
  874. amba_driver_unregister(&mmci_driver);
  875. }
  876. module_init(mmci_init);
  877. module_exit(mmci_exit);
  878. module_param(fmax, uint, 0444);
  879. MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
  880. MODULE_LICENSE("GPL");