msm_sdcc.c 34 KB

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