msm_sdcc.c 35 KB

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