msm_sdcc.c 33 KB

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