msm_sdcc.c 35 KB

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