msm_sdcc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /*
  2. * linux/drivers/mmc/host/msm_sdcc.c - Qualcomm MSM 7X00A SDCC Driver
  3. *
  4. * Copyright (C) 2007 Google Inc,
  5. * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
  6. * Copyright (C) 2009, Code Aurora Forum. All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Based on mmci.c
  13. *
  14. * Author: San Mehat (san@android.com)
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/init.h>
  20. #include <linux/ioport.h>
  21. #include <linux/device.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/err.h>
  25. #include <linux/highmem.h>
  26. #include <linux/log2.h>
  27. #include <linux/mmc/host.h>
  28. #include <linux/mmc/card.h>
  29. #include <linux/mmc/sdio.h>
  30. #include <linux/clk.h>
  31. #include <linux/scatterlist.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/debugfs.h>
  35. #include <linux/io.h>
  36. #include <linux/memory.h>
  37. #include <linux/gfp.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/div64.h>
  40. #include <asm/sizes.h>
  41. #include <mach/mmc.h>
  42. #include <mach/msm_iomap.h>
  43. #include <mach/dma.h>
  44. #include <mach/clk.h>
  45. #include "msm_sdcc.h"
  46. #define DRIVER_NAME "msm-sdcc"
  47. #define BUSCLK_PWRSAVE 1
  48. #define BUSCLK_TIMEOUT (HZ)
  49. static unsigned int msmsdcc_fmin = 144000;
  50. static unsigned int msmsdcc_fmax = 50000000;
  51. static unsigned int msmsdcc_4bit = 1;
  52. static unsigned int msmsdcc_pwrsave = 1;
  53. static unsigned int msmsdcc_piopoll = 1;
  54. static unsigned int msmsdcc_sdioirq;
  55. #define PIO_SPINMAX 30
  56. #define CMD_SPINMAX 20
  57. static inline void
  58. msmsdcc_disable_clocks(struct msmsdcc_host *host, int deferr)
  59. {
  60. WARN_ON(!host->clks_on);
  61. BUG_ON(host->curr.mrq);
  62. if (deferr) {
  63. mod_timer(&host->busclk_timer, jiffies + BUSCLK_TIMEOUT);
  64. } else {
  65. del_timer_sync(&host->busclk_timer);
  66. /* Need to check clks_on again in case the busclk
  67. * timer fired
  68. */
  69. if (host->clks_on) {
  70. clk_disable(host->clk);
  71. clk_disable(host->pclk);
  72. host->clks_on = 0;
  73. }
  74. }
  75. }
  76. static inline int
  77. msmsdcc_enable_clocks(struct msmsdcc_host *host)
  78. {
  79. int rc;
  80. del_timer_sync(&host->busclk_timer);
  81. if (!host->clks_on) {
  82. rc = clk_enable(host->pclk);
  83. if (rc)
  84. return rc;
  85. rc = clk_enable(host->clk);
  86. if (rc) {
  87. clk_disable(host->pclk);
  88. return rc;
  89. }
  90. udelay(1 + ((3 * USEC_PER_SEC) /
  91. (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
  92. host->clks_on = 1;
  93. }
  94. return 0;
  95. }
  96. static inline unsigned int
  97. msmsdcc_readl(struct msmsdcc_host *host, unsigned int reg)
  98. {
  99. return readl(host->base + reg);
  100. }
  101. static inline void
  102. msmsdcc_writel(struct msmsdcc_host *host, u32 data, unsigned int reg)
  103. {
  104. writel(data, host->base + reg);
  105. /* 3 clk delay required! */
  106. udelay(1 + ((3 * USEC_PER_SEC) /
  107. (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
  108. }
  109. static void
  110. msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd,
  111. u32 c);
  112. static void msmsdcc_reset_and_restore(struct msmsdcc_host *host)
  113. {
  114. u32 mci_clk = 0;
  115. u32 mci_mask0 = 0;
  116. int ret = 0;
  117. /* Save the controller state */
  118. mci_clk = readl(host->base + MMCICLOCK);
  119. mci_mask0 = readl(host->base + MMCIMASK0);
  120. /* Reset the controller */
  121. ret = clk_reset(host->clk, CLK_RESET_ASSERT);
  122. if (ret)
  123. pr_err("%s: Clock assert failed at %u Hz with err %d\n",
  124. mmc_hostname(host->mmc), host->clk_rate, ret);
  125. ret = clk_reset(host->clk, CLK_RESET_DEASSERT);
  126. if (ret)
  127. pr_err("%s: Clock deassert failed at %u Hz with err %d\n",
  128. mmc_hostname(host->mmc), host->clk_rate, ret);
  129. pr_info("%s: Controller has been re-initialiazed\n",
  130. mmc_hostname(host->mmc));
  131. /* Restore the contoller state */
  132. writel(host->pwr, host->base + MMCIPOWER);
  133. writel(mci_clk, host->base + MMCICLOCK);
  134. writel(mci_mask0, host->base + MMCIMASK0);
  135. ret = clk_set_rate(host->clk, host->clk_rate);
  136. if (ret)
  137. pr_err("%s: Failed to set clk rate %u Hz (%d)\n",
  138. mmc_hostname(host->mmc), host->clk_rate, ret);
  139. }
  140. static void
  141. msmsdcc_request_end(struct msmsdcc_host *host, struct mmc_request *mrq)
  142. {
  143. BUG_ON(host->curr.data);
  144. host->curr.mrq = NULL;
  145. host->curr.cmd = NULL;
  146. if (mrq->data)
  147. mrq->data->bytes_xfered = host->curr.data_xfered;
  148. if (mrq->cmd->error == -ETIMEDOUT)
  149. mdelay(5);
  150. #if BUSCLK_PWRSAVE
  151. msmsdcc_disable_clocks(host, 1);
  152. #endif
  153. /*
  154. * Need to drop the host lock here; mmc_request_done may call
  155. * back into the driver...
  156. */
  157. spin_unlock(&host->lock);
  158. mmc_request_done(host->mmc, mrq);
  159. spin_lock(&host->lock);
  160. }
  161. static void
  162. msmsdcc_stop_data(struct msmsdcc_host *host)
  163. {
  164. host->curr.data = NULL;
  165. host->curr.got_dataend = 0;
  166. }
  167. uint32_t msmsdcc_fifo_addr(struct msmsdcc_host *host)
  168. {
  169. return host->memres->start + MMCIFIFO;
  170. }
  171. static inline void
  172. msmsdcc_start_command_exec(struct msmsdcc_host *host, u32 arg, u32 c) {
  173. msmsdcc_writel(host, arg, MMCIARGUMENT);
  174. msmsdcc_writel(host, c, MMCICOMMAND);
  175. }
  176. static void
  177. msmsdcc_dma_exec_func(struct msm_dmov_cmd *cmd)
  178. {
  179. struct msmsdcc_host *host = (struct msmsdcc_host *)cmd->data;
  180. msmsdcc_writel(host, host->cmd_timeout, MMCIDATATIMER);
  181. msmsdcc_writel(host, (unsigned int)host->curr.xfer_size,
  182. MMCIDATALENGTH);
  183. msmsdcc_writel(host, host->cmd_pio_irqmask, MMCIMASK1);
  184. msmsdcc_writel(host, host->cmd_datactrl, MMCIDATACTRL);
  185. if (host->cmd_cmd) {
  186. msmsdcc_start_command_exec(host,
  187. (u32) host->cmd_cmd->arg,
  188. (u32) host->cmd_c);
  189. }
  190. host->dma.active = 1;
  191. }
  192. static void
  193. msmsdcc_dma_complete_tlet(unsigned long data)
  194. {
  195. struct msmsdcc_host *host = (struct msmsdcc_host *)data;
  196. unsigned long flags;
  197. struct mmc_request *mrq;
  198. struct msm_dmov_errdata err;
  199. spin_lock_irqsave(&host->lock, flags);
  200. host->dma.active = 0;
  201. err = host->dma.err;
  202. mrq = host->curr.mrq;
  203. BUG_ON(!mrq);
  204. WARN_ON(!mrq->data);
  205. if (!(host->dma.result & DMOV_RSLT_VALID)) {
  206. pr_err("msmsdcc: Invalid DataMover result\n");
  207. goto out;
  208. }
  209. if (host->dma.result & DMOV_RSLT_DONE) {
  210. host->curr.data_xfered = host->curr.xfer_size;
  211. } else {
  212. /* Error or flush */
  213. if (host->dma.result & DMOV_RSLT_ERROR)
  214. pr_err("%s: DMA error (0x%.8x)\n",
  215. mmc_hostname(host->mmc), host->dma.result);
  216. if (host->dma.result & DMOV_RSLT_FLUSH)
  217. pr_err("%s: DMA channel flushed (0x%.8x)\n",
  218. mmc_hostname(host->mmc), host->dma.result);
  219. pr_err("Flush data: %.8x %.8x %.8x %.8x %.8x %.8x\n",
  220. err.flush[0], err.flush[1], err.flush[2],
  221. err.flush[3], err.flush[4], err.flush[5]);
  222. msmsdcc_reset_and_restore(host);
  223. if (!mrq->data->error)
  224. mrq->data->error = -EIO;
  225. }
  226. dma_unmap_sg(mmc_dev(host->mmc), host->dma.sg, host->dma.num_ents,
  227. host->dma.dir);
  228. if (host->curr.user_pages) {
  229. struct scatterlist *sg = host->dma.sg;
  230. int i;
  231. for (i = 0; i < host->dma.num_ents; i++)
  232. flush_dcache_page(sg_page(sg++));
  233. }
  234. host->dma.sg = NULL;
  235. host->dma.busy = 0;
  236. if (host->curr.got_dataend || mrq->data->error) {
  237. /*
  238. * If we've already gotten our DATAEND / DATABLKEND
  239. * for this request, then complete it through here.
  240. */
  241. msmsdcc_stop_data(host);
  242. if (!mrq->data->error)
  243. host->curr.data_xfered = host->curr.xfer_size;
  244. if (!mrq->data->stop || mrq->cmd->error) {
  245. host->curr.mrq = NULL;
  246. host->curr.cmd = NULL;
  247. mrq->data->bytes_xfered = host->curr.data_xfered;
  248. spin_unlock_irqrestore(&host->lock, flags);
  249. #if BUSCLK_PWRSAVE
  250. msmsdcc_disable_clocks(host, 1);
  251. #endif
  252. mmc_request_done(host->mmc, mrq);
  253. return;
  254. } else
  255. msmsdcc_start_command(host, mrq->data->stop, 0);
  256. }
  257. out:
  258. spin_unlock_irqrestore(&host->lock, flags);
  259. return;
  260. }
  261. static void
  262. msmsdcc_dma_complete_func(struct msm_dmov_cmd *cmd,
  263. unsigned int result,
  264. struct msm_dmov_errdata *err)
  265. {
  266. struct msmsdcc_dma_data *dma_data =
  267. container_of(cmd, struct msmsdcc_dma_data, hdr);
  268. struct msmsdcc_host *host = dma_data->host;
  269. dma_data->result = result;
  270. if (err)
  271. memcpy(&dma_data->err, err, sizeof(struct msm_dmov_errdata));
  272. tasklet_schedule(&host->dma_tlet);
  273. }
  274. static int validate_dma(struct msmsdcc_host *host, struct mmc_data *data)
  275. {
  276. if (host->dma.channel == -1)
  277. return -ENOENT;
  278. if ((data->blksz * data->blocks) < MCI_FIFOSIZE)
  279. return -EINVAL;
  280. if ((data->blksz * data->blocks) % MCI_FIFOSIZE)
  281. return -EINVAL;
  282. return 0;
  283. }
  284. static int msmsdcc_config_dma(struct msmsdcc_host *host, struct mmc_data *data)
  285. {
  286. struct msmsdcc_nc_dmadata *nc;
  287. dmov_box *box;
  288. uint32_t rows;
  289. uint32_t crci;
  290. unsigned int n;
  291. int i, rc;
  292. struct scatterlist *sg = data->sg;
  293. rc = validate_dma(host, data);
  294. if (rc)
  295. return rc;
  296. host->dma.sg = data->sg;
  297. host->dma.num_ents = data->sg_len;
  298. BUG_ON(host->dma.num_ents > NR_SG); /* Prevent memory corruption */
  299. nc = host->dma.nc;
  300. switch (host->pdev_id) {
  301. case 1:
  302. crci = MSMSDCC_CRCI_SDC1;
  303. break;
  304. case 2:
  305. crci = MSMSDCC_CRCI_SDC2;
  306. break;
  307. case 3:
  308. crci = MSMSDCC_CRCI_SDC3;
  309. break;
  310. case 4:
  311. crci = MSMSDCC_CRCI_SDC4;
  312. break;
  313. default:
  314. host->dma.sg = NULL;
  315. host->dma.num_ents = 0;
  316. return -ENOENT;
  317. }
  318. if (data->flags & MMC_DATA_READ)
  319. host->dma.dir = DMA_FROM_DEVICE;
  320. else
  321. host->dma.dir = DMA_TO_DEVICE;
  322. host->curr.user_pages = 0;
  323. box = &nc->cmd[0];
  324. /* location of command block must be 64 bit aligned */
  325. BUG_ON(host->dma.cmd_busaddr & 0x07);
  326. nc->cmdptr = (host->dma.cmd_busaddr >> 3) | CMD_PTR_LP;
  327. host->dma.hdr.cmdptr = DMOV_CMD_PTR_LIST |
  328. DMOV_CMD_ADDR(host->dma.cmdptr_busaddr);
  329. host->dma.hdr.complete_func = msmsdcc_dma_complete_func;
  330. n = dma_map_sg(mmc_dev(host->mmc), host->dma.sg,
  331. host->dma.num_ents, host->dma.dir);
  332. if (n == 0) {
  333. printk(KERN_ERR "%s: Unable to map in all sg elements\n",
  334. mmc_hostname(host->mmc));
  335. host->dma.sg = NULL;
  336. host->dma.num_ents = 0;
  337. return -ENOMEM;
  338. }
  339. for_each_sg(host->dma.sg, sg, n, i) {
  340. box->cmd = CMD_MODE_BOX;
  341. if (i == n - 1)
  342. box->cmd |= CMD_LC;
  343. rows = (sg_dma_len(sg) % MCI_FIFOSIZE) ?
  344. (sg_dma_len(sg) / MCI_FIFOSIZE) + 1 :
  345. (sg_dma_len(sg) / MCI_FIFOSIZE) ;
  346. if (data->flags & MMC_DATA_READ) {
  347. box->src_row_addr = msmsdcc_fifo_addr(host);
  348. box->dst_row_addr = sg_dma_address(sg);
  349. box->src_dst_len = (MCI_FIFOSIZE << 16) |
  350. (MCI_FIFOSIZE);
  351. box->row_offset = MCI_FIFOSIZE;
  352. box->num_rows = rows * ((1 << 16) + 1);
  353. box->cmd |= CMD_SRC_CRCI(crci);
  354. } else {
  355. box->src_row_addr = sg_dma_address(sg);
  356. box->dst_row_addr = msmsdcc_fifo_addr(host);
  357. box->src_dst_len = (MCI_FIFOSIZE << 16) |
  358. (MCI_FIFOSIZE);
  359. box->row_offset = (MCI_FIFOSIZE << 16);
  360. box->num_rows = rows * ((1 << 16) + 1);
  361. box->cmd |= CMD_DST_CRCI(crci);
  362. }
  363. box++;
  364. }
  365. return 0;
  366. }
  367. static int
  368. snoop_cccr_abort(struct mmc_command *cmd)
  369. {
  370. if ((cmd->opcode == 52) &&
  371. (cmd->arg & 0x80000000) &&
  372. (((cmd->arg >> 9) & 0x1ffff) == SDIO_CCCR_ABORT))
  373. return 1;
  374. return 0;
  375. }
  376. static void
  377. msmsdcc_start_command_deferred(struct msmsdcc_host *host,
  378. struct mmc_command *cmd, u32 *c)
  379. {
  380. *c |= (cmd->opcode | MCI_CPSM_ENABLE);
  381. if (cmd->flags & MMC_RSP_PRESENT) {
  382. if (cmd->flags & MMC_RSP_136)
  383. *c |= MCI_CPSM_LONGRSP;
  384. *c |= MCI_CPSM_RESPONSE;
  385. }
  386. if (/*interrupt*/0)
  387. *c |= MCI_CPSM_INTERRUPT;
  388. if ((((cmd->opcode == 17) || (cmd->opcode == 18)) ||
  389. ((cmd->opcode == 24) || (cmd->opcode == 25))) ||
  390. (cmd->opcode == 53))
  391. *c |= MCI_CSPM_DATCMD;
  392. if (host->prog_scan && (cmd->opcode == 12)) {
  393. *c |= MCI_CPSM_PROGENA;
  394. host->prog_enable = true;
  395. }
  396. if (cmd == cmd->mrq->stop)
  397. *c |= MCI_CSPM_MCIABORT;
  398. if (snoop_cccr_abort(cmd))
  399. *c |= MCI_CSPM_MCIABORT;
  400. if (host->curr.cmd != NULL) {
  401. printk(KERN_ERR "%s: Overlapping command requests\n",
  402. mmc_hostname(host->mmc));
  403. }
  404. host->curr.cmd = cmd;
  405. }
  406. static void
  407. msmsdcc_start_data(struct msmsdcc_host *host, struct mmc_data *data,
  408. struct mmc_command *cmd, u32 c)
  409. {
  410. unsigned int datactrl, timeout;
  411. unsigned long long clks;
  412. unsigned int pio_irqmask = 0;
  413. host->curr.data = data;
  414. host->curr.xfer_size = data->blksz * data->blocks;
  415. host->curr.xfer_remain = host->curr.xfer_size;
  416. host->curr.data_xfered = 0;
  417. host->curr.got_dataend = 0;
  418. memset(&host->pio, 0, sizeof(host->pio));
  419. datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
  420. if (!msmsdcc_config_dma(host, data))
  421. datactrl |= MCI_DPSM_DMAENABLE;
  422. else {
  423. host->pio.sg = data->sg;
  424. host->pio.sg_len = data->sg_len;
  425. host->pio.sg_off = 0;
  426. if (data->flags & MMC_DATA_READ) {
  427. pio_irqmask = MCI_RXFIFOHALFFULLMASK;
  428. if (host->curr.xfer_remain < MCI_FIFOSIZE)
  429. pio_irqmask |= MCI_RXDATAAVLBLMASK;
  430. } else
  431. pio_irqmask = MCI_TXFIFOHALFEMPTYMASK;
  432. }
  433. if (data->flags & MMC_DATA_READ)
  434. datactrl |= MCI_DPSM_DIRECTION;
  435. clks = (unsigned long long)data->timeout_ns * host->clk_rate;
  436. do_div(clks, NSEC_PER_SEC);
  437. timeout = data->timeout_clks + (unsigned int)clks*2 ;
  438. if (datactrl & MCI_DPSM_DMAENABLE) {
  439. /* Save parameters for the exec function */
  440. host->cmd_timeout = timeout;
  441. host->cmd_pio_irqmask = pio_irqmask;
  442. host->cmd_datactrl = datactrl;
  443. host->cmd_cmd = cmd;
  444. host->dma.hdr.execute_func = msmsdcc_dma_exec_func;
  445. host->dma.hdr.data = (void *)host;
  446. host->dma.busy = 1;
  447. if (cmd) {
  448. msmsdcc_start_command_deferred(host, cmd, &c);
  449. host->cmd_c = c;
  450. }
  451. msm_dmov_enqueue_cmd(host->dma.channel, &host->dma.hdr);
  452. if (data->flags & MMC_DATA_WRITE)
  453. host->prog_scan = true;
  454. } else {
  455. msmsdcc_writel(host, timeout, MMCIDATATIMER);
  456. msmsdcc_writel(host, host->curr.xfer_size, MMCIDATALENGTH);
  457. msmsdcc_writel(host, pio_irqmask, MMCIMASK1);
  458. msmsdcc_writel(host, datactrl, MMCIDATACTRL);
  459. if (cmd) {
  460. /* Daisy-chain the command if requested */
  461. msmsdcc_start_command(host, cmd, c);
  462. }
  463. }
  464. }
  465. static void
  466. msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, u32 c)
  467. {
  468. if (cmd == cmd->mrq->stop)
  469. c |= MCI_CSPM_MCIABORT;
  470. host->stats.cmds++;
  471. msmsdcc_start_command_deferred(host, cmd, &c);
  472. msmsdcc_start_command_exec(host, cmd->arg, c);
  473. }
  474. static void
  475. msmsdcc_data_err(struct msmsdcc_host *host, struct mmc_data *data,
  476. unsigned int status)
  477. {
  478. if (status & MCI_DATACRCFAIL) {
  479. pr_err("%s: Data CRC error\n", mmc_hostname(host->mmc));
  480. pr_err("%s: opcode 0x%.8x\n", __func__,
  481. data->mrq->cmd->opcode);
  482. pr_err("%s: blksz %d, blocks %d\n", __func__,
  483. data->blksz, data->blocks);
  484. data->error = -EILSEQ;
  485. } else if (status & MCI_DATATIMEOUT) {
  486. pr_err("%s: Data timeout\n", mmc_hostname(host->mmc));
  487. data->error = -ETIMEDOUT;
  488. } else if (status & MCI_RXOVERRUN) {
  489. pr_err("%s: RX overrun\n", mmc_hostname(host->mmc));
  490. data->error = -EIO;
  491. } else if (status & MCI_TXUNDERRUN) {
  492. pr_err("%s: TX underrun\n", mmc_hostname(host->mmc));
  493. data->error = -EIO;
  494. } else {
  495. pr_err("%s: Unknown error (0x%.8x)\n",
  496. mmc_hostname(host->mmc), status);
  497. data->error = -EIO;
  498. }
  499. }
  500. static int
  501. msmsdcc_pio_read(struct msmsdcc_host *host, char *buffer, unsigned int remain)
  502. {
  503. uint32_t *ptr = (uint32_t *) buffer;
  504. int count = 0;
  505. if (remain % 4)
  506. remain = ((remain >> 2) + 1) << 2;
  507. while (msmsdcc_readl(host, MMCISTATUS) & MCI_RXDATAAVLBL) {
  508. *ptr = msmsdcc_readl(host, MMCIFIFO + (count % MCI_FIFOSIZE));
  509. ptr++;
  510. count += sizeof(uint32_t);
  511. remain -= sizeof(uint32_t);
  512. if (remain == 0)
  513. break;
  514. }
  515. return count;
  516. }
  517. static int
  518. msmsdcc_pio_write(struct msmsdcc_host *host, char *buffer,
  519. unsigned int remain, u32 status)
  520. {
  521. void __iomem *base = host->base;
  522. char *ptr = buffer;
  523. do {
  524. unsigned int count, maxcnt, sz;
  525. maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE :
  526. MCI_FIFOHALFSIZE;
  527. count = min(remain, maxcnt);
  528. sz = count % 4 ? (count >> 2) + 1 : (count >> 2);
  529. writesl(base + MMCIFIFO, ptr, sz);
  530. ptr += count;
  531. remain -= count;
  532. if (remain == 0)
  533. break;
  534. status = msmsdcc_readl(host, MMCISTATUS);
  535. } while (status & MCI_TXFIFOHALFEMPTY);
  536. return ptr - buffer;
  537. }
  538. static int
  539. msmsdcc_spin_on_status(struct msmsdcc_host *host, uint32_t mask, int maxspin)
  540. {
  541. while (maxspin) {
  542. if ((msmsdcc_readl(host, MMCISTATUS) & mask))
  543. return 0;
  544. udelay(1);
  545. --maxspin;
  546. }
  547. return -ETIMEDOUT;
  548. }
  549. static irqreturn_t
  550. msmsdcc_pio_irq(int irq, void *dev_id)
  551. {
  552. struct msmsdcc_host *host = dev_id;
  553. uint32_t status;
  554. status = msmsdcc_readl(host, MMCISTATUS);
  555. do {
  556. unsigned long flags;
  557. unsigned int remain, len;
  558. char *buffer;
  559. if (!(status & (MCI_TXFIFOHALFEMPTY | MCI_RXDATAAVLBL))) {
  560. if (host->curr.xfer_remain == 0 || !msmsdcc_piopoll)
  561. break;
  562. if (msmsdcc_spin_on_status(host,
  563. (MCI_TXFIFOHALFEMPTY |
  564. MCI_RXDATAAVLBL),
  565. PIO_SPINMAX)) {
  566. break;
  567. }
  568. }
  569. /* Map the current scatter buffer */
  570. local_irq_save(flags);
  571. buffer = kmap_atomic(sg_page(host->pio.sg),
  572. KM_BIO_SRC_IRQ) + host->pio.sg->offset;
  573. buffer += host->pio.sg_off;
  574. remain = host->pio.sg->length - host->pio.sg_off;
  575. len = 0;
  576. if (status & MCI_RXACTIVE)
  577. len = msmsdcc_pio_read(host, buffer, remain);
  578. if (status & MCI_TXACTIVE)
  579. len = msmsdcc_pio_write(host, buffer, remain, status);
  580. /* Unmap the buffer */
  581. kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
  582. local_irq_restore(flags);
  583. host->pio.sg_off += len;
  584. host->curr.xfer_remain -= len;
  585. host->curr.data_xfered += len;
  586. remain -= len;
  587. if (remain == 0) {
  588. /* This sg page is full - do some housekeeping */
  589. if (status & MCI_RXACTIVE && host->curr.user_pages)
  590. flush_dcache_page(sg_page(host->pio.sg));
  591. if (!--host->pio.sg_len) {
  592. memset(&host->pio, 0, sizeof(host->pio));
  593. break;
  594. }
  595. /* Advance to next sg */
  596. host->pio.sg++;
  597. host->pio.sg_off = 0;
  598. }
  599. status = msmsdcc_readl(host, MMCISTATUS);
  600. } while (1);
  601. if (status & MCI_RXACTIVE && host->curr.xfer_remain < MCI_FIFOSIZE)
  602. msmsdcc_writel(host, MCI_RXDATAAVLBLMASK, MMCIMASK1);
  603. if (!host->curr.xfer_remain)
  604. msmsdcc_writel(host, 0, MMCIMASK1);
  605. return IRQ_HANDLED;
  606. }
  607. static void msmsdcc_do_cmdirq(struct msmsdcc_host *host, uint32_t status)
  608. {
  609. struct mmc_command *cmd = host->curr.cmd;
  610. host->curr.cmd = NULL;
  611. cmd->resp[0] = msmsdcc_readl(host, MMCIRESPONSE0);
  612. cmd->resp[1] = msmsdcc_readl(host, MMCIRESPONSE1);
  613. cmd->resp[2] = msmsdcc_readl(host, MMCIRESPONSE2);
  614. cmd->resp[3] = msmsdcc_readl(host, MMCIRESPONSE3);
  615. if (status & MCI_CMDTIMEOUT) {
  616. cmd->error = -ETIMEDOUT;
  617. } else if (status & MCI_CMDCRCFAIL &&
  618. cmd->flags & MMC_RSP_CRC) {
  619. pr_err("%s: Command CRC error\n", mmc_hostname(host->mmc));
  620. cmd->error = -EILSEQ;
  621. }
  622. if (!cmd->data || cmd->error) {
  623. if (host->curr.data && host->dma.sg)
  624. msm_dmov_stop_cmd(host->dma.channel,
  625. &host->dma.hdr, 0);
  626. else if (host->curr.data) { /* Non DMA */
  627. msmsdcc_reset_and_restore(host);
  628. msmsdcc_stop_data(host);
  629. msmsdcc_request_end(host, cmd->mrq);
  630. } else { /* host->data == NULL */
  631. if (!cmd->error && host->prog_enable) {
  632. if (status & MCI_PROGDONE) {
  633. host->prog_scan = false;
  634. host->prog_enable = false;
  635. msmsdcc_request_end(host, cmd->mrq);
  636. } else {
  637. host->curr.cmd = cmd;
  638. }
  639. } else {
  640. if (host->prog_enable) {
  641. host->prog_scan = false;
  642. host->prog_enable = false;
  643. }
  644. msmsdcc_request_end(host, cmd->mrq);
  645. }
  646. }
  647. } else if (cmd->data)
  648. if (!(cmd->data->flags & MMC_DATA_READ))
  649. msmsdcc_start_data(host, cmd->data,
  650. NULL, 0);
  651. }
  652. static void
  653. msmsdcc_handle_irq_data(struct msmsdcc_host *host, u32 status,
  654. void __iomem *base)
  655. {
  656. struct mmc_data *data = host->curr.data;
  657. if (status & (MCI_CMDSENT | MCI_CMDRESPEND | MCI_CMDCRCFAIL |
  658. MCI_CMDTIMEOUT | MCI_PROGDONE) && host->curr.cmd) {
  659. msmsdcc_do_cmdirq(host, status);
  660. }
  661. if (!data)
  662. return;
  663. /* Check for data errors */
  664. if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
  665. MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
  666. msmsdcc_data_err(host, data, status);
  667. host->curr.data_xfered = 0;
  668. if (host->dma.sg)
  669. msm_dmov_stop_cmd(host->dma.channel,
  670. &host->dma.hdr, 0);
  671. else {
  672. msmsdcc_reset_and_restore(host);
  673. if (host->curr.data)
  674. msmsdcc_stop_data(host);
  675. if (!data->stop)
  676. msmsdcc_request_end(host, data->mrq);
  677. else
  678. msmsdcc_start_command(host, data->stop, 0);
  679. }
  680. }
  681. /* Check for data done */
  682. if (!host->curr.got_dataend && (status & MCI_DATAEND))
  683. host->curr.got_dataend = 1;
  684. /*
  685. * If DMA is still in progress, we complete via the completion handler
  686. */
  687. if (host->curr.got_dataend && !host->dma.busy) {
  688. /*
  689. * There appears to be an issue in the controller where
  690. * if you request a small block transfer (< fifo size),
  691. * you may get your DATAEND/DATABLKEND irq without the
  692. * PIO data irq.
  693. *
  694. * Check to see if there is still data to be read,
  695. * and simulate a PIO irq.
  696. */
  697. if (readl(base + MMCISTATUS) & MCI_RXDATAAVLBL)
  698. msmsdcc_pio_irq(1, host);
  699. msmsdcc_stop_data(host);
  700. if (!data->error)
  701. host->curr.data_xfered = host->curr.xfer_size;
  702. if (!data->stop)
  703. msmsdcc_request_end(host, data->mrq);
  704. else
  705. msmsdcc_start_command(host, data->stop, 0);
  706. }
  707. }
  708. static irqreturn_t
  709. msmsdcc_irq(int irq, void *dev_id)
  710. {
  711. struct msmsdcc_host *host = dev_id;
  712. void __iomem *base = host->base;
  713. u32 status;
  714. int ret = 0;
  715. int cardint = 0;
  716. spin_lock(&host->lock);
  717. do {
  718. status = msmsdcc_readl(host, MMCISTATUS);
  719. status &= msmsdcc_readl(host, MMCIMASK0);
  720. msmsdcc_writel(host, status, MMCICLEAR);
  721. if (status & MCI_SDIOINTR)
  722. status &= ~MCI_SDIOINTR;
  723. if (!status)
  724. break;
  725. msmsdcc_handle_irq_data(host, status, base);
  726. if (status & MCI_SDIOINTOPER) {
  727. cardint = 1;
  728. status &= ~MCI_SDIOINTOPER;
  729. }
  730. ret = 1;
  731. } while (status);
  732. spin_unlock(&host->lock);
  733. /*
  734. * We have to delay handling the card interrupt as it calls
  735. * back into the driver.
  736. */
  737. if (cardint)
  738. mmc_signal_sdio_irq(host->mmc);
  739. return IRQ_RETVAL(ret);
  740. }
  741. static void
  742. msmsdcc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  743. {
  744. struct msmsdcc_host *host = mmc_priv(mmc);
  745. unsigned long flags;
  746. WARN_ON(host->curr.mrq != NULL);
  747. WARN_ON(host->pwr == 0);
  748. spin_lock_irqsave(&host->lock, flags);
  749. host->stats.reqs++;
  750. if (host->eject) {
  751. if (mrq->data && !(mrq->data->flags & MMC_DATA_READ)) {
  752. mrq->cmd->error = 0;
  753. mrq->data->bytes_xfered = mrq->data->blksz *
  754. mrq->data->blocks;
  755. } else
  756. mrq->cmd->error = -ENOMEDIUM;
  757. spin_unlock_irqrestore(&host->lock, flags);
  758. mmc_request_done(mmc, mrq);
  759. return;
  760. }
  761. msmsdcc_enable_clocks(host);
  762. host->curr.mrq = mrq;
  763. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  764. /* Queue/read data, daisy-chain command when data starts */
  765. msmsdcc_start_data(host, mrq->data, mrq->cmd, 0);
  766. else
  767. msmsdcc_start_command(host, mrq->cmd, 0);
  768. if (host->cmdpoll && !msmsdcc_spin_on_status(host,
  769. MCI_CMDRESPEND|MCI_CMDCRCFAIL|MCI_CMDTIMEOUT,
  770. CMD_SPINMAX)) {
  771. uint32_t status = msmsdcc_readl(host, MMCISTATUS);
  772. msmsdcc_do_cmdirq(host, status);
  773. msmsdcc_writel(host,
  774. MCI_CMDRESPEND | MCI_CMDCRCFAIL | MCI_CMDTIMEOUT,
  775. MMCICLEAR);
  776. host->stats.cmdpoll_hits++;
  777. } else {
  778. host->stats.cmdpoll_misses++;
  779. }
  780. spin_unlock_irqrestore(&host->lock, flags);
  781. }
  782. static void
  783. msmsdcc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  784. {
  785. struct msmsdcc_host *host = mmc_priv(mmc);
  786. u32 clk = 0, pwr = 0;
  787. int rc;
  788. unsigned long flags;
  789. spin_lock_irqsave(&host->lock, flags);
  790. msmsdcc_enable_clocks(host);
  791. if (ios->clock) {
  792. if (ios->clock != host->clk_rate) {
  793. rc = clk_set_rate(host->clk, ios->clock);
  794. if (rc < 0)
  795. pr_err("%s: Error setting clock rate (%d)\n",
  796. mmc_hostname(host->mmc), rc);
  797. else
  798. host->clk_rate = ios->clock;
  799. }
  800. clk |= MCI_CLK_ENABLE;
  801. }
  802. if (ios->bus_width == MMC_BUS_WIDTH_4)
  803. clk |= (2 << 10); /* Set WIDEBUS */
  804. if (ios->clock > 400000 && msmsdcc_pwrsave)
  805. clk |= (1 << 9); /* PWRSAVE */
  806. clk |= (1 << 12); /* FLOW_ENA */
  807. clk |= (1 << 15); /* feedback clock */
  808. if (host->plat->translate_vdd)
  809. pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
  810. switch (ios->power_mode) {
  811. case MMC_POWER_OFF:
  812. break;
  813. case MMC_POWER_UP:
  814. pwr |= MCI_PWR_UP;
  815. break;
  816. case MMC_POWER_ON:
  817. pwr |= MCI_PWR_ON;
  818. break;
  819. }
  820. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  821. pwr |= MCI_OD;
  822. msmsdcc_writel(host, clk, MMCICLOCK);
  823. if (host->pwr != pwr) {
  824. host->pwr = pwr;
  825. msmsdcc_writel(host, pwr, MMCIPOWER);
  826. }
  827. #if BUSCLK_PWRSAVE
  828. msmsdcc_disable_clocks(host, 1);
  829. #endif
  830. spin_unlock_irqrestore(&host->lock, flags);
  831. }
  832. static void msmsdcc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  833. {
  834. struct msmsdcc_host *host = mmc_priv(mmc);
  835. unsigned long flags;
  836. u32 status;
  837. spin_lock_irqsave(&host->lock, flags);
  838. if (msmsdcc_sdioirq == 1) {
  839. status = msmsdcc_readl(host, MMCIMASK0);
  840. if (enable)
  841. status |= MCI_SDIOINTOPERMASK;
  842. else
  843. status &= ~MCI_SDIOINTOPERMASK;
  844. host->saved_irq0mask = status;
  845. msmsdcc_writel(host, status, MMCIMASK0);
  846. }
  847. spin_unlock_irqrestore(&host->lock, flags);
  848. }
  849. static const struct mmc_host_ops msmsdcc_ops = {
  850. .request = msmsdcc_request,
  851. .set_ios = msmsdcc_set_ios,
  852. .enable_sdio_irq = msmsdcc_enable_sdio_irq,
  853. };
  854. static void
  855. msmsdcc_check_status(unsigned long data)
  856. {
  857. struct msmsdcc_host *host = (struct msmsdcc_host *)data;
  858. unsigned int status;
  859. if (!host->plat->status) {
  860. mmc_detect_change(host->mmc, 0);
  861. goto out;
  862. }
  863. status = host->plat->status(mmc_dev(host->mmc));
  864. host->eject = !status;
  865. if (status ^ host->oldstat) {
  866. pr_info("%s: Slot status change detected (%d -> %d)\n",
  867. mmc_hostname(host->mmc), host->oldstat, status);
  868. if (status)
  869. mmc_detect_change(host->mmc, (5 * HZ) / 2);
  870. else
  871. mmc_detect_change(host->mmc, 0);
  872. }
  873. host->oldstat = status;
  874. out:
  875. if (host->timer.function)
  876. mod_timer(&host->timer, jiffies + HZ);
  877. }
  878. static irqreturn_t
  879. msmsdcc_platform_status_irq(int irq, void *dev_id)
  880. {
  881. struct msmsdcc_host *host = dev_id;
  882. printk(KERN_DEBUG "%s: %d\n", __func__, irq);
  883. msmsdcc_check_status((unsigned long) host);
  884. return IRQ_HANDLED;
  885. }
  886. static void
  887. msmsdcc_status_notify_cb(int card_present, void *dev_id)
  888. {
  889. struct msmsdcc_host *host = dev_id;
  890. printk(KERN_DEBUG "%s: card_present %d\n", mmc_hostname(host->mmc),
  891. card_present);
  892. msmsdcc_check_status((unsigned long) host);
  893. }
  894. static void
  895. msmsdcc_busclk_expired(unsigned long _data)
  896. {
  897. struct msmsdcc_host *host = (struct msmsdcc_host *) _data;
  898. if (host->clks_on)
  899. msmsdcc_disable_clocks(host, 0);
  900. }
  901. static int
  902. msmsdcc_init_dma(struct msmsdcc_host *host)
  903. {
  904. memset(&host->dma, 0, sizeof(struct msmsdcc_dma_data));
  905. host->dma.host = host;
  906. host->dma.channel = -1;
  907. if (!host->dmares)
  908. return -ENODEV;
  909. host->dma.nc = dma_alloc_coherent(NULL,
  910. sizeof(struct msmsdcc_nc_dmadata),
  911. &host->dma.nc_busaddr,
  912. GFP_KERNEL);
  913. if (host->dma.nc == NULL) {
  914. pr_err("Unable to allocate DMA buffer\n");
  915. return -ENOMEM;
  916. }
  917. memset(host->dma.nc, 0x00, sizeof(struct msmsdcc_nc_dmadata));
  918. host->dma.cmd_busaddr = host->dma.nc_busaddr;
  919. host->dma.cmdptr_busaddr = host->dma.nc_busaddr +
  920. offsetof(struct msmsdcc_nc_dmadata, cmdptr);
  921. host->dma.channel = host->dmares->start;
  922. return 0;
  923. }
  924. static int
  925. msmsdcc_probe(struct platform_device *pdev)
  926. {
  927. struct msm_mmc_platform_data *plat = pdev->dev.platform_data;
  928. struct msmsdcc_host *host;
  929. struct mmc_host *mmc;
  930. struct resource *cmd_irqres = NULL;
  931. struct resource *pio_irqres = NULL;
  932. struct resource *stat_irqres = NULL;
  933. struct resource *memres = NULL;
  934. struct resource *dmares = NULL;
  935. int ret;
  936. /* must have platform data */
  937. if (!plat) {
  938. pr_err("%s: Platform data not available\n", __func__);
  939. ret = -EINVAL;
  940. goto out;
  941. }
  942. if (pdev->id < 1 || pdev->id > 4)
  943. return -EINVAL;
  944. if (pdev->resource == NULL || pdev->num_resources < 2) {
  945. pr_err("%s: Invalid resource\n", __func__);
  946. return -ENXIO;
  947. }
  948. memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  949. dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  950. cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  951. "cmd_irq");
  952. pio_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  953. "pio_irq");
  954. stat_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  955. "status_irq");
  956. if (!cmd_irqres || !pio_irqres || !memres) {
  957. pr_err("%s: Invalid resource\n", __func__);
  958. return -ENXIO;
  959. }
  960. /*
  961. * Setup our host structure
  962. */
  963. mmc = mmc_alloc_host(sizeof(struct msmsdcc_host), &pdev->dev);
  964. if (!mmc) {
  965. ret = -ENOMEM;
  966. goto out;
  967. }
  968. host = mmc_priv(mmc);
  969. host->pdev_id = pdev->id;
  970. host->plat = plat;
  971. host->mmc = mmc;
  972. host->curr.cmd = NULL;
  973. host->cmdpoll = 1;
  974. host->base = ioremap(memres->start, PAGE_SIZE);
  975. if (!host->base) {
  976. ret = -ENOMEM;
  977. goto out;
  978. }
  979. host->cmd_irqres = cmd_irqres;
  980. host->pio_irqres = pio_irqres;
  981. host->memres = memres;
  982. host->dmares = dmares;
  983. spin_lock_init(&host->lock);
  984. tasklet_init(&host->dma_tlet, msmsdcc_dma_complete_tlet,
  985. (unsigned long)host);
  986. /*
  987. * Setup DMA
  988. */
  989. msmsdcc_init_dma(host);
  990. /* Get our clocks */
  991. host->pclk = clk_get(&pdev->dev, "sdc_pclk");
  992. if (IS_ERR(host->pclk)) {
  993. ret = PTR_ERR(host->pclk);
  994. goto host_free;
  995. }
  996. host->clk = clk_get(&pdev->dev, "sdc_clk");
  997. if (IS_ERR(host->clk)) {
  998. ret = PTR_ERR(host->clk);
  999. goto pclk_put;
  1000. }
  1001. /* Enable clocks */
  1002. ret = msmsdcc_enable_clocks(host);
  1003. if (ret)
  1004. goto clk_put;
  1005. ret = clk_set_rate(host->clk, msmsdcc_fmin);
  1006. if (ret) {
  1007. pr_err("%s: Clock rate set failed (%d)\n", __func__, ret);
  1008. goto clk_disable;
  1009. }
  1010. host->pclk_rate = clk_get_rate(host->pclk);
  1011. host->clk_rate = clk_get_rate(host->clk);
  1012. /*
  1013. * Setup MMC host structure
  1014. */
  1015. mmc->ops = &msmsdcc_ops;
  1016. mmc->f_min = msmsdcc_fmin;
  1017. mmc->f_max = msmsdcc_fmax;
  1018. mmc->ocr_avail = plat->ocr_mask;
  1019. if (msmsdcc_4bit)
  1020. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1021. if (msmsdcc_sdioirq)
  1022. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1023. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
  1024. mmc->max_segs = NR_SG;
  1025. mmc->max_blk_size = 4096; /* MCI_DATA_CTL BLOCKSIZE up to 4096 */
  1026. mmc->max_blk_count = 65536;
  1027. mmc->max_req_size = 33554432; /* MCI_DATA_LENGTH is 25 bits */
  1028. mmc->max_seg_size = mmc->max_req_size;
  1029. msmsdcc_writel(host, 0, MMCIMASK0);
  1030. msmsdcc_writel(host, 0x5e007ff, MMCICLEAR);
  1031. msmsdcc_writel(host, MCI_IRQENABLE, MMCIMASK0);
  1032. host->saved_irq0mask = MCI_IRQENABLE;
  1033. /*
  1034. * Setup card detect change
  1035. */
  1036. memset(&host->timer, 0, sizeof(host->timer));
  1037. if (stat_irqres && !(stat_irqres->flags & IORESOURCE_DISABLED)) {
  1038. unsigned long irqflags = IRQF_SHARED |
  1039. (stat_irqres->flags & IRQF_TRIGGER_MASK);
  1040. host->stat_irq = stat_irqres->start;
  1041. ret = request_irq(host->stat_irq,
  1042. msmsdcc_platform_status_irq,
  1043. irqflags,
  1044. DRIVER_NAME " (slot)",
  1045. host);
  1046. if (ret) {
  1047. pr_err("%s: Unable to get slot IRQ %d (%d)\n",
  1048. mmc_hostname(mmc), host->stat_irq, ret);
  1049. goto clk_disable;
  1050. }
  1051. } else if (plat->register_status_notify) {
  1052. plat->register_status_notify(msmsdcc_status_notify_cb, host);
  1053. } else if (!plat->status)
  1054. pr_err("%s: No card detect facilities available\n",
  1055. mmc_hostname(mmc));
  1056. else {
  1057. init_timer(&host->timer);
  1058. host->timer.data = (unsigned long)host;
  1059. host->timer.function = msmsdcc_check_status;
  1060. host->timer.expires = jiffies + HZ;
  1061. add_timer(&host->timer);
  1062. }
  1063. if (plat->status) {
  1064. host->oldstat = host->plat->status(mmc_dev(host->mmc));
  1065. host->eject = !host->oldstat;
  1066. }
  1067. init_timer(&host->busclk_timer);
  1068. host->busclk_timer.data = (unsigned long) host;
  1069. host->busclk_timer.function = msmsdcc_busclk_expired;
  1070. ret = request_irq(cmd_irqres->start, msmsdcc_irq, IRQF_SHARED,
  1071. DRIVER_NAME " (cmd)", host);
  1072. if (ret)
  1073. goto stat_irq_free;
  1074. ret = request_irq(pio_irqres->start, msmsdcc_pio_irq, IRQF_SHARED,
  1075. DRIVER_NAME " (pio)", host);
  1076. if (ret)
  1077. goto cmd_irq_free;
  1078. mmc_set_drvdata(pdev, mmc);
  1079. mmc_add_host(mmc);
  1080. pr_info("%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n",
  1081. mmc_hostname(mmc), (unsigned long long)memres->start,
  1082. (unsigned int) cmd_irqres->start,
  1083. (unsigned int) host->stat_irq, host->dma.channel);
  1084. pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc),
  1085. (mmc->caps & MMC_CAP_4_BIT_DATA ? "enabled" : "disabled"));
  1086. pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
  1087. mmc_hostname(mmc), msmsdcc_fmin, msmsdcc_fmax, host->pclk_rate);
  1088. pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc), host->eject);
  1089. pr_info("%s: Power save feature enable = %d\n",
  1090. mmc_hostname(mmc), msmsdcc_pwrsave);
  1091. if (host->dma.channel != -1) {
  1092. pr_info("%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n",
  1093. mmc_hostname(mmc), host->dma.nc, host->dma.nc_busaddr);
  1094. pr_info("%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n",
  1095. mmc_hostname(mmc), host->dma.cmd_busaddr,
  1096. host->dma.cmdptr_busaddr);
  1097. } else
  1098. pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc));
  1099. if (host->timer.function)
  1100. pr_info("%s: Polling status mode enabled\n", mmc_hostname(mmc));
  1101. return 0;
  1102. cmd_irq_free:
  1103. free_irq(cmd_irqres->start, host);
  1104. stat_irq_free:
  1105. if (host->stat_irq)
  1106. free_irq(host->stat_irq, host);
  1107. clk_disable:
  1108. msmsdcc_disable_clocks(host, 0);
  1109. clk_put:
  1110. clk_put(host->clk);
  1111. pclk_put:
  1112. clk_put(host->pclk);
  1113. host_free:
  1114. mmc_free_host(mmc);
  1115. out:
  1116. return ret;
  1117. }
  1118. #ifdef CONFIG_PM
  1119. #ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
  1120. static void
  1121. do_resume_work(struct work_struct *work)
  1122. {
  1123. struct msmsdcc_host *host =
  1124. container_of(work, struct msmsdcc_host, resume_task);
  1125. struct mmc_host *mmc = host->mmc;
  1126. if (mmc) {
  1127. mmc_resume_host(mmc);
  1128. if (host->stat_irq)
  1129. enable_irq(host->stat_irq);
  1130. }
  1131. }
  1132. #endif
  1133. static int
  1134. msmsdcc_suspend(struct platform_device *dev, pm_message_t state)
  1135. {
  1136. struct mmc_host *mmc = mmc_get_drvdata(dev);
  1137. int rc = 0;
  1138. if (mmc) {
  1139. struct msmsdcc_host *host = mmc_priv(mmc);
  1140. if (host->stat_irq)
  1141. disable_irq(host->stat_irq);
  1142. if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
  1143. rc = mmc_suspend_host(mmc);
  1144. if (!rc)
  1145. msmsdcc_writel(host, 0, MMCIMASK0);
  1146. if (host->clks_on)
  1147. msmsdcc_disable_clocks(host, 0);
  1148. }
  1149. return rc;
  1150. }
  1151. static int
  1152. msmsdcc_resume(struct platform_device *dev)
  1153. {
  1154. struct mmc_host *mmc = mmc_get_drvdata(dev);
  1155. if (mmc) {
  1156. struct msmsdcc_host *host = mmc_priv(mmc);
  1157. msmsdcc_enable_clocks(host);
  1158. msmsdcc_writel(host, host->saved_irq0mask, MMCIMASK0);
  1159. if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
  1160. mmc_resume_host(mmc);
  1161. if (host->stat_irq)
  1162. enable_irq(host->stat_irq);
  1163. #if BUSCLK_PWRSAVE
  1164. msmsdcc_disable_clocks(host, 1);
  1165. #endif
  1166. }
  1167. return 0;
  1168. }
  1169. #else
  1170. #define msmsdcc_suspend 0
  1171. #define msmsdcc_resume 0
  1172. #endif
  1173. static struct platform_driver msmsdcc_driver = {
  1174. .probe = msmsdcc_probe,
  1175. .suspend = msmsdcc_suspend,
  1176. .resume = msmsdcc_resume,
  1177. .driver = {
  1178. .name = "msm_sdcc",
  1179. },
  1180. };
  1181. static int __init msmsdcc_init(void)
  1182. {
  1183. return platform_driver_register(&msmsdcc_driver);
  1184. }
  1185. static void __exit msmsdcc_exit(void)
  1186. {
  1187. platform_driver_unregister(&msmsdcc_driver);
  1188. }
  1189. module_init(msmsdcc_init);
  1190. module_exit(msmsdcc_exit);
  1191. MODULE_DESCRIPTION("Qualcomm MSM 7X00A Multimedia Card Interface driver");
  1192. MODULE_LICENSE("GPL");