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 <linux/platform_data/mmc-msm_sdcc.h>
  43. #include <mach/dma.h>
  44. #include <mach/clk.h>
  45. #include "msm_sdcc.h"
  46. #define DRIVER_NAME "msm-sdcc"
  47. #define BUSCLK_PWRSAVE 1
  48. #define BUSCLK_TIMEOUT (HZ)
  49. static unsigned int msmsdcc_fmin = 144000;
  50. static unsigned int msmsdcc_fmax = 50000000;
  51. static unsigned int msmsdcc_4bit = 1;
  52. static unsigned int msmsdcc_pwrsave = 1;
  53. static unsigned int msmsdcc_piopoll = 1;
  54. static unsigned int msmsdcc_sdioirq;
  55. #define PIO_SPINMAX 30
  56. #define CMD_SPINMAX 20
  57. static inline void
  58. msmsdcc_disable_clocks(struct msmsdcc_host *host, int deferr)
  59. {
  60. WARN_ON(!host->clks_on);
  61. BUG_ON(host->curr.mrq);
  62. if (deferr) {
  63. mod_timer(&host->busclk_timer, jiffies + BUSCLK_TIMEOUT);
  64. } else {
  65. del_timer_sync(&host->busclk_timer);
  66. /* Need to check clks_on again in case the busclk
  67. * timer fired
  68. */
  69. if (host->clks_on) {
  70. clk_disable(host->clk);
  71. clk_disable(host->pclk);
  72. host->clks_on = 0;
  73. }
  74. }
  75. }
  76. static inline int
  77. msmsdcc_enable_clocks(struct msmsdcc_host *host)
  78. {
  79. int rc;
  80. del_timer_sync(&host->busclk_timer);
  81. if (!host->clks_on) {
  82. rc = clk_enable(host->pclk);
  83. if (rc)
  84. return rc;
  85. rc = clk_enable(host->clk);
  86. if (rc) {
  87. clk_disable(host->pclk);
  88. return rc;
  89. }
  90. udelay(1 + ((3 * USEC_PER_SEC) /
  91. (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
  92. host->clks_on = 1;
  93. }
  94. return 0;
  95. }
  96. static inline unsigned int
  97. msmsdcc_readl(struct msmsdcc_host *host, unsigned int reg)
  98. {
  99. return readl(host->base + reg);
  100. }
  101. static inline void
  102. msmsdcc_writel(struct msmsdcc_host *host, u32 data, unsigned int reg)
  103. {
  104. writel(data, host->base + reg);
  105. /* 3 clk delay required! */
  106. udelay(1 + ((3 * USEC_PER_SEC) /
  107. (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
  108. }
  109. static void
  110. msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd,
  111. u32 c);
  112. static void msmsdcc_reset_and_restore(struct msmsdcc_host *host)
  113. {
  114. u32 mci_clk = 0;
  115. u32 mci_mask0 = 0;
  116. int ret = 0;
  117. /* Save the controller state */
  118. mci_clk = readl(host->base + MMCICLOCK);
  119. mci_mask0 = readl(host->base + MMCIMASK0);
  120. /* Reset the controller */
  121. ret = clk_reset(host->clk, CLK_RESET_ASSERT);
  122. if (ret)
  123. pr_err("%s: Clock assert failed at %u Hz with err %d\n",
  124. mmc_hostname(host->mmc), host->clk_rate, ret);
  125. ret = clk_reset(host->clk, CLK_RESET_DEASSERT);
  126. if (ret)
  127. pr_err("%s: Clock deassert failed at %u Hz with err %d\n",
  128. mmc_hostname(host->mmc), host->clk_rate, ret);
  129. pr_info("%s: Controller has been re-initialiazed\n",
  130. mmc_hostname(host->mmc));
  131. /* Restore the contoller state */
  132. writel(host->pwr, host->base + MMCIPOWER);
  133. writel(mci_clk, host->base + MMCICLOCK);
  134. writel(mci_mask0, host->base + MMCIMASK0);
  135. ret = clk_set_rate(host->clk, host->clk_rate);
  136. if (ret)
  137. pr_err("%s: Failed to set clk rate %u Hz (%d)\n",
  138. mmc_hostname(host->mmc), host->clk_rate, ret);
  139. }
  140. static void
  141. msmsdcc_request_end(struct msmsdcc_host *host, struct mmc_request *mrq)
  142. {
  143. BUG_ON(host->curr.data);
  144. host->curr.mrq = NULL;
  145. host->curr.cmd = NULL;
  146. if (mrq->data)
  147. mrq->data->bytes_xfered = host->curr.data_xfered;
  148. if (mrq->cmd->error == -ETIMEDOUT)
  149. mdelay(5);
  150. #if BUSCLK_PWRSAVE
  151. msmsdcc_disable_clocks(host, 1);
  152. #endif
  153. /*
  154. * Need to drop the host lock here; mmc_request_done may call
  155. * back into the driver...
  156. */
  157. spin_unlock(&host->lock);
  158. mmc_request_done(host->mmc, mrq);
  159. spin_lock(&host->lock);
  160. }
  161. static void
  162. msmsdcc_stop_data(struct msmsdcc_host *host)
  163. {
  164. host->curr.data = NULL;
  165. host->curr.got_dataend = 0;
  166. }
  167. uint32_t msmsdcc_fifo_addr(struct msmsdcc_host *host)
  168. {
  169. return host->memres->start + MMCIFIFO;
  170. }
  171. static inline void
  172. msmsdcc_start_command_exec(struct msmsdcc_host *host, u32 arg, u32 c) {
  173. msmsdcc_writel(host, arg, MMCIARGUMENT);
  174. msmsdcc_writel(host, c, MMCICOMMAND);
  175. }
  176. static void
  177. msmsdcc_dma_exec_func(struct msm_dmov_cmd *cmd)
  178. {
  179. struct msmsdcc_host *host = (struct msmsdcc_host *)cmd->data;
  180. msmsdcc_writel(host, host->cmd_timeout, MMCIDATATIMER);
  181. msmsdcc_writel(host, (unsigned int)host->curr.xfer_size,
  182. MMCIDATALENGTH);
  183. msmsdcc_writel(host, (msmsdcc_readl(host, MMCIMASK0) &
  184. (~MCI_IRQ_PIO)) | host->cmd_pio_irqmask, MMCIMASK0);
  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. pr_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. pr_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, (msmsdcc_readl(host, MMCIMASK0) &
  453. (~MCI_IRQ_PIO)) | pio_irqmask, MMCIMASK0);
  454. msmsdcc_writel(host, datactrl, MMCIDATACTRL);
  455. if (cmd) {
  456. /* Daisy-chain the command if requested */
  457. msmsdcc_start_command(host, cmd, c);
  458. }
  459. }
  460. }
  461. static void
  462. msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, u32 c)
  463. {
  464. if (cmd == cmd->mrq->stop)
  465. c |= MCI_CSPM_MCIABORT;
  466. host->stats.cmds++;
  467. msmsdcc_start_command_deferred(host, cmd, &c);
  468. msmsdcc_start_command_exec(host, cmd->arg, c);
  469. }
  470. static void
  471. msmsdcc_data_err(struct msmsdcc_host *host, struct mmc_data *data,
  472. unsigned int status)
  473. {
  474. if (status & MCI_DATACRCFAIL) {
  475. pr_err("%s: Data CRC error\n", mmc_hostname(host->mmc));
  476. pr_err("%s: opcode 0x%.8x\n", __func__,
  477. data->mrq->cmd->opcode);
  478. pr_err("%s: blksz %d, blocks %d\n", __func__,
  479. data->blksz, data->blocks);
  480. data->error = -EILSEQ;
  481. } else if (status & MCI_DATATIMEOUT) {
  482. pr_err("%s: Data timeout\n", mmc_hostname(host->mmc));
  483. data->error = -ETIMEDOUT;
  484. } else if (status & MCI_RXOVERRUN) {
  485. pr_err("%s: RX overrun\n", mmc_hostname(host->mmc));
  486. data->error = -EIO;
  487. } else if (status & MCI_TXUNDERRUN) {
  488. pr_err("%s: TX underrun\n", mmc_hostname(host->mmc));
  489. data->error = -EIO;
  490. } else {
  491. pr_err("%s: Unknown error (0x%.8x)\n",
  492. mmc_hostname(host->mmc), status);
  493. data->error = -EIO;
  494. }
  495. }
  496. static int
  497. msmsdcc_pio_read(struct msmsdcc_host *host, char *buffer, unsigned int remain)
  498. {
  499. uint32_t *ptr = (uint32_t *) buffer;
  500. int count = 0;
  501. if (remain % 4)
  502. remain = ((remain >> 2) + 1) << 2;
  503. while (msmsdcc_readl(host, MMCISTATUS) & MCI_RXDATAAVLBL) {
  504. *ptr = msmsdcc_readl(host, MMCIFIFO + (count % MCI_FIFOSIZE));
  505. ptr++;
  506. count += sizeof(uint32_t);
  507. remain -= sizeof(uint32_t);
  508. if (remain == 0)
  509. break;
  510. }
  511. return count;
  512. }
  513. static int
  514. msmsdcc_pio_write(struct msmsdcc_host *host, char *buffer,
  515. unsigned int remain, u32 status)
  516. {
  517. void __iomem *base = host->base;
  518. char *ptr = buffer;
  519. do {
  520. unsigned int count, maxcnt, sz;
  521. maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE :
  522. MCI_FIFOHALFSIZE;
  523. count = min(remain, maxcnt);
  524. sz = count % 4 ? (count >> 2) + 1 : (count >> 2);
  525. writesl(base + MMCIFIFO, ptr, sz);
  526. ptr += count;
  527. remain -= count;
  528. if (remain == 0)
  529. break;
  530. status = msmsdcc_readl(host, MMCISTATUS);
  531. } while (status & MCI_TXFIFOHALFEMPTY);
  532. return ptr - buffer;
  533. }
  534. static int
  535. msmsdcc_spin_on_status(struct msmsdcc_host *host, uint32_t mask, int maxspin)
  536. {
  537. while (maxspin) {
  538. if ((msmsdcc_readl(host, MMCISTATUS) & mask))
  539. return 0;
  540. udelay(1);
  541. --maxspin;
  542. }
  543. return -ETIMEDOUT;
  544. }
  545. static irqreturn_t
  546. msmsdcc_pio_irq(int irq, void *dev_id)
  547. {
  548. struct msmsdcc_host *host = dev_id;
  549. uint32_t status;
  550. u32 mci_mask0;
  551. status = msmsdcc_readl(host, MMCISTATUS);
  552. mci_mask0 = msmsdcc_readl(host, MMCIMASK0);
  553. if (((mci_mask0 & status) & MCI_IRQ_PIO) == 0)
  554. return IRQ_NONE;
  555. do {
  556. unsigned long flags;
  557. unsigned int remain, len;
  558. char *buffer;
  559. if (!(status & (MCI_TXFIFOHALFEMPTY | MCI_RXDATAAVLBL))) {
  560. if (host->curr.xfer_remain == 0 || !msmsdcc_piopoll)
  561. break;
  562. if (msmsdcc_spin_on_status(host,
  563. (MCI_TXFIFOHALFEMPTY |
  564. MCI_RXDATAAVLBL),
  565. PIO_SPINMAX)) {
  566. break;
  567. }
  568. }
  569. /* Map the current scatter buffer */
  570. local_irq_save(flags);
  571. buffer = kmap_atomic(sg_page(host->pio.sg))
  572. + host->pio.sg->offset;
  573. buffer += host->pio.sg_off;
  574. remain = host->pio.sg->length - host->pio.sg_off;
  575. len = 0;
  576. if (status & MCI_RXACTIVE)
  577. len = msmsdcc_pio_read(host, buffer, remain);
  578. if (status & MCI_TXACTIVE)
  579. len = msmsdcc_pio_write(host, buffer, remain, status);
  580. /* Unmap the buffer */
  581. kunmap_atomic(buffer);
  582. local_irq_restore(flags);
  583. host->pio.sg_off += len;
  584. host->curr.xfer_remain -= len;
  585. host->curr.data_xfered += len;
  586. remain -= len;
  587. if (remain == 0) {
  588. /* This sg page is full - do some housekeeping */
  589. if (status & MCI_RXACTIVE && host->curr.user_pages)
  590. flush_dcache_page(sg_page(host->pio.sg));
  591. if (!--host->pio.sg_len) {
  592. memset(&host->pio, 0, sizeof(host->pio));
  593. break;
  594. }
  595. /* Advance to next sg */
  596. host->pio.sg++;
  597. host->pio.sg_off = 0;
  598. }
  599. status = msmsdcc_readl(host, MMCISTATUS);
  600. } while (1);
  601. if (status & MCI_RXACTIVE && host->curr.xfer_remain < MCI_FIFOSIZE)
  602. msmsdcc_writel(host, (mci_mask0 & (~MCI_IRQ_PIO)) |
  603. MCI_RXDATAAVLBLMASK, MMCIMASK0);
  604. if (!host->curr.xfer_remain)
  605. msmsdcc_writel(host, (mci_mask0 & (~MCI_IRQ_PIO)) | 0,
  606. MMCIMASK0);
  607. return IRQ_HANDLED;
  608. }
  609. static void msmsdcc_do_cmdirq(struct msmsdcc_host *host, uint32_t status)
  610. {
  611. struct mmc_command *cmd = host->curr.cmd;
  612. host->curr.cmd = NULL;
  613. cmd->resp[0] = msmsdcc_readl(host, MMCIRESPONSE0);
  614. cmd->resp[1] = msmsdcc_readl(host, MMCIRESPONSE1);
  615. cmd->resp[2] = msmsdcc_readl(host, MMCIRESPONSE2);
  616. cmd->resp[3] = msmsdcc_readl(host, MMCIRESPONSE3);
  617. if (status & MCI_CMDTIMEOUT) {
  618. cmd->error = -ETIMEDOUT;
  619. } else if (status & MCI_CMDCRCFAIL &&
  620. cmd->flags & MMC_RSP_CRC) {
  621. pr_err("%s: Command CRC error\n", mmc_hostname(host->mmc));
  622. cmd->error = -EILSEQ;
  623. }
  624. if (!cmd->data || cmd->error) {
  625. if (host->curr.data && host->dma.sg)
  626. msm_dmov_stop_cmd(host->dma.channel,
  627. &host->dma.hdr, 0);
  628. else if (host->curr.data) { /* Non DMA */
  629. msmsdcc_reset_and_restore(host);
  630. msmsdcc_stop_data(host);
  631. msmsdcc_request_end(host, cmd->mrq);
  632. } else { /* host->data == NULL */
  633. if (!cmd->error && host->prog_enable) {
  634. if (status & MCI_PROGDONE) {
  635. host->prog_scan = false;
  636. host->prog_enable = false;
  637. msmsdcc_request_end(host, cmd->mrq);
  638. } else {
  639. host->curr.cmd = cmd;
  640. }
  641. } else {
  642. if (host->prog_enable) {
  643. host->prog_scan = false;
  644. host->prog_enable = false;
  645. }
  646. msmsdcc_request_end(host, cmd->mrq);
  647. }
  648. }
  649. } else if (cmd->data)
  650. if (!(cmd->data->flags & MMC_DATA_READ))
  651. msmsdcc_start_data(host, cmd->data,
  652. NULL, 0);
  653. }
  654. static void
  655. msmsdcc_handle_irq_data(struct msmsdcc_host *host, u32 status,
  656. void __iomem *base)
  657. {
  658. struct mmc_data *data = host->curr.data;
  659. if (status & (MCI_CMDSENT | MCI_CMDRESPEND | MCI_CMDCRCFAIL |
  660. MCI_CMDTIMEOUT | MCI_PROGDONE) && host->curr.cmd) {
  661. msmsdcc_do_cmdirq(host, status);
  662. }
  663. if (!data)
  664. return;
  665. /* Check for data errors */
  666. if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
  667. MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
  668. msmsdcc_data_err(host, data, status);
  669. host->curr.data_xfered = 0;
  670. if (host->dma.sg)
  671. msm_dmov_stop_cmd(host->dma.channel,
  672. &host->dma.hdr, 0);
  673. else {
  674. msmsdcc_reset_and_restore(host);
  675. if (host->curr.data)
  676. msmsdcc_stop_data(host);
  677. if (!data->stop)
  678. msmsdcc_request_end(host, data->mrq);
  679. else
  680. msmsdcc_start_command(host, data->stop, 0);
  681. }
  682. }
  683. /* Check for data done */
  684. if (!host->curr.got_dataend && (status & MCI_DATAEND))
  685. host->curr.got_dataend = 1;
  686. /*
  687. * If DMA is still in progress, we complete via the completion handler
  688. */
  689. if (host->curr.got_dataend && !host->dma.busy) {
  690. /*
  691. * There appears to be an issue in the controller where
  692. * if you request a small block transfer (< fifo size),
  693. * you may get your DATAEND/DATABLKEND irq without the
  694. * PIO data irq.
  695. *
  696. * Check to see if there is still data to be read,
  697. * and simulate a PIO irq.
  698. */
  699. if (readl(base + MMCISTATUS) & MCI_RXDATAAVLBL)
  700. msmsdcc_pio_irq(1, host);
  701. msmsdcc_stop_data(host);
  702. if (!data->error)
  703. host->curr.data_xfered = host->curr.xfer_size;
  704. if (!data->stop)
  705. msmsdcc_request_end(host, data->mrq);
  706. else
  707. msmsdcc_start_command(host, data->stop, 0);
  708. }
  709. }
  710. static irqreturn_t
  711. msmsdcc_irq(int irq, void *dev_id)
  712. {
  713. struct msmsdcc_host *host = dev_id;
  714. void __iomem *base = host->base;
  715. u32 status;
  716. int ret = 0;
  717. int cardint = 0;
  718. spin_lock(&host->lock);
  719. do {
  720. status = msmsdcc_readl(host, MMCISTATUS);
  721. status &= msmsdcc_readl(host, MMCIMASK0);
  722. if ((status & (~MCI_IRQ_PIO)) == 0)
  723. break;
  724. msmsdcc_writel(host, status, MMCICLEAR);
  725. if (status & MCI_SDIOINTR)
  726. status &= ~MCI_SDIOINTR;
  727. if (!status)
  728. break;
  729. msmsdcc_handle_irq_data(host, status, base);
  730. if (status & MCI_SDIOINTOPER) {
  731. cardint = 1;
  732. status &= ~MCI_SDIOINTOPER;
  733. }
  734. ret = 1;
  735. } while (status);
  736. spin_unlock(&host->lock);
  737. /*
  738. * We have to delay handling the card interrupt as it calls
  739. * back into the driver.
  740. */
  741. if (cardint)
  742. mmc_signal_sdio_irq(host->mmc);
  743. return IRQ_RETVAL(ret);
  744. }
  745. static void
  746. msmsdcc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  747. {
  748. struct msmsdcc_host *host = mmc_priv(mmc);
  749. unsigned long flags;
  750. WARN_ON(host->curr.mrq != NULL);
  751. WARN_ON(host->pwr == 0);
  752. spin_lock_irqsave(&host->lock, flags);
  753. host->stats.reqs++;
  754. if (host->eject) {
  755. if (mrq->data && !(mrq->data->flags & MMC_DATA_READ)) {
  756. mrq->cmd->error = 0;
  757. mrq->data->bytes_xfered = mrq->data->blksz *
  758. mrq->data->blocks;
  759. } else
  760. mrq->cmd->error = -ENOMEDIUM;
  761. spin_unlock_irqrestore(&host->lock, flags);
  762. mmc_request_done(mmc, mrq);
  763. return;
  764. }
  765. msmsdcc_enable_clocks(host);
  766. host->curr.mrq = mrq;
  767. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  768. /* Queue/read data, daisy-chain command when data starts */
  769. msmsdcc_start_data(host, mrq->data, mrq->cmd, 0);
  770. else
  771. msmsdcc_start_command(host, mrq->cmd, 0);
  772. if (host->cmdpoll && !msmsdcc_spin_on_status(host,
  773. MCI_CMDRESPEND|MCI_CMDCRCFAIL|MCI_CMDTIMEOUT,
  774. CMD_SPINMAX)) {
  775. uint32_t status = msmsdcc_readl(host, MMCISTATUS);
  776. msmsdcc_do_cmdirq(host, status);
  777. msmsdcc_writel(host,
  778. MCI_CMDRESPEND | MCI_CMDCRCFAIL | MCI_CMDTIMEOUT,
  779. MMCICLEAR);
  780. host->stats.cmdpoll_hits++;
  781. } else {
  782. host->stats.cmdpoll_misses++;
  783. }
  784. spin_unlock_irqrestore(&host->lock, flags);
  785. }
  786. static void msmsdcc_setup_gpio(struct msmsdcc_host *host, bool enable)
  787. {
  788. struct msm_mmc_gpio_data *curr;
  789. int i, rc = 0;
  790. if (!host->plat->gpio_data || host->gpio_config_status == enable)
  791. return;
  792. curr = host->plat->gpio_data;
  793. for (i = 0; i < curr->size; i++) {
  794. if (enable) {
  795. rc = gpio_request(curr->gpio[i].no,
  796. curr->gpio[i].name);
  797. if (rc) {
  798. pr_err("%s: gpio_request(%d, %s) failed %d\n",
  799. mmc_hostname(host->mmc),
  800. curr->gpio[i].no,
  801. curr->gpio[i].name, rc);
  802. goto free_gpios;
  803. }
  804. } else {
  805. gpio_free(curr->gpio[i].no);
  806. }
  807. }
  808. host->gpio_config_status = enable;
  809. return;
  810. free_gpios:
  811. for (; i >= 0; i--)
  812. gpio_free(curr->gpio[i].no);
  813. }
  814. static void
  815. msmsdcc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  816. {
  817. struct msmsdcc_host *host = mmc_priv(mmc);
  818. u32 clk = 0, pwr = 0;
  819. int rc;
  820. unsigned long flags;
  821. spin_lock_irqsave(&host->lock, flags);
  822. msmsdcc_enable_clocks(host);
  823. spin_unlock_irqrestore(&host->lock, flags);
  824. if (ios->clock) {
  825. if (ios->clock != host->clk_rate) {
  826. rc = clk_set_rate(host->clk, ios->clock);
  827. if (rc < 0)
  828. pr_err("%s: Error setting clock rate (%d)\n",
  829. mmc_hostname(host->mmc), rc);
  830. else
  831. host->clk_rate = ios->clock;
  832. }
  833. clk |= MCI_CLK_ENABLE;
  834. }
  835. if (ios->bus_width == MMC_BUS_WIDTH_4)
  836. clk |= (2 << 10); /* Set WIDEBUS */
  837. if (ios->clock > 400000 && msmsdcc_pwrsave)
  838. clk |= (1 << 9); /* PWRSAVE */
  839. clk |= (1 << 12); /* FLOW_ENA */
  840. clk |= (1 << 15); /* feedback clock */
  841. if (host->plat->translate_vdd)
  842. pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
  843. switch (ios->power_mode) {
  844. case MMC_POWER_OFF:
  845. msmsdcc_setup_gpio(host, false);
  846. break;
  847. case MMC_POWER_UP:
  848. pwr |= MCI_PWR_UP;
  849. msmsdcc_setup_gpio(host, true);
  850. break;
  851. case MMC_POWER_ON:
  852. pwr |= MCI_PWR_ON;
  853. break;
  854. }
  855. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  856. pwr |= MCI_OD;
  857. msmsdcc_writel(host, clk, MMCICLOCK);
  858. if (host->pwr != pwr) {
  859. host->pwr = pwr;
  860. msmsdcc_writel(host, pwr, MMCIPOWER);
  861. }
  862. #if BUSCLK_PWRSAVE
  863. spin_lock_irqsave(&host->lock, flags);
  864. msmsdcc_disable_clocks(host, 1);
  865. spin_unlock_irqrestore(&host->lock, flags);
  866. #endif
  867. }
  868. static void msmsdcc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  869. {
  870. struct msmsdcc_host *host = mmc_priv(mmc);
  871. unsigned long flags;
  872. u32 status;
  873. spin_lock_irqsave(&host->lock, flags);
  874. if (msmsdcc_sdioirq == 1) {
  875. status = msmsdcc_readl(host, MMCIMASK0);
  876. if (enable)
  877. status |= MCI_SDIOINTOPERMASK;
  878. else
  879. status &= ~MCI_SDIOINTOPERMASK;
  880. host->saved_irq0mask = status;
  881. msmsdcc_writel(host, status, MMCIMASK0);
  882. }
  883. spin_unlock_irqrestore(&host->lock, flags);
  884. }
  885. static void msmsdcc_init_card(struct mmc_host *mmc, struct mmc_card *card)
  886. {
  887. struct msmsdcc_host *host = mmc_priv(mmc);
  888. if (host->plat->init_card)
  889. host->plat->init_card(card);
  890. }
  891. static const struct mmc_host_ops msmsdcc_ops = {
  892. .request = msmsdcc_request,
  893. .set_ios = msmsdcc_set_ios,
  894. .enable_sdio_irq = msmsdcc_enable_sdio_irq,
  895. .init_card = msmsdcc_init_card,
  896. };
  897. static void
  898. msmsdcc_check_status(unsigned long data)
  899. {
  900. struct msmsdcc_host *host = (struct msmsdcc_host *)data;
  901. unsigned int status;
  902. if (!host->plat->status) {
  903. mmc_detect_change(host->mmc, 0);
  904. goto out;
  905. }
  906. status = host->plat->status(mmc_dev(host->mmc));
  907. host->eject = !status;
  908. if (status ^ host->oldstat) {
  909. pr_info("%s: Slot status change detected (%d -> %d)\n",
  910. mmc_hostname(host->mmc), host->oldstat, status);
  911. if (status)
  912. mmc_detect_change(host->mmc, (5 * HZ) / 2);
  913. else
  914. mmc_detect_change(host->mmc, 0);
  915. }
  916. host->oldstat = status;
  917. out:
  918. if (host->timer.function)
  919. mod_timer(&host->timer, jiffies + HZ);
  920. }
  921. static irqreturn_t
  922. msmsdcc_platform_status_irq(int irq, void *dev_id)
  923. {
  924. struct msmsdcc_host *host = dev_id;
  925. pr_debug("%s: %d\n", __func__, irq);
  926. msmsdcc_check_status((unsigned long) host);
  927. return IRQ_HANDLED;
  928. }
  929. static void
  930. msmsdcc_status_notify_cb(int card_present, void *dev_id)
  931. {
  932. struct msmsdcc_host *host = dev_id;
  933. pr_debug("%s: card_present %d\n", mmc_hostname(host->mmc),
  934. card_present);
  935. msmsdcc_check_status((unsigned long) host);
  936. }
  937. static void
  938. msmsdcc_busclk_expired(unsigned long _data)
  939. {
  940. struct msmsdcc_host *host = (struct msmsdcc_host *) _data;
  941. if (host->clks_on)
  942. msmsdcc_disable_clocks(host, 0);
  943. }
  944. static int
  945. msmsdcc_init_dma(struct msmsdcc_host *host)
  946. {
  947. memset(&host->dma, 0, sizeof(struct msmsdcc_dma_data));
  948. host->dma.host = host;
  949. host->dma.channel = -1;
  950. if (!host->dmares)
  951. return -ENODEV;
  952. host->dma.nc = dma_alloc_coherent(NULL,
  953. sizeof(struct msmsdcc_nc_dmadata),
  954. &host->dma.nc_busaddr,
  955. GFP_KERNEL);
  956. if (host->dma.nc == NULL) {
  957. pr_err("Unable to allocate DMA buffer\n");
  958. return -ENOMEM;
  959. }
  960. memset(host->dma.nc, 0x00, sizeof(struct msmsdcc_nc_dmadata));
  961. host->dma.cmd_busaddr = host->dma.nc_busaddr;
  962. host->dma.cmdptr_busaddr = host->dma.nc_busaddr +
  963. offsetof(struct msmsdcc_nc_dmadata, cmdptr);
  964. host->dma.channel = host->dmares->start;
  965. return 0;
  966. }
  967. static int
  968. msmsdcc_probe(struct platform_device *pdev)
  969. {
  970. struct msm_mmc_platform_data *plat = pdev->dev.platform_data;
  971. struct msmsdcc_host *host;
  972. struct mmc_host *mmc;
  973. struct resource *cmd_irqres = NULL;
  974. struct resource *stat_irqres = NULL;
  975. struct resource *memres = NULL;
  976. struct resource *dmares = NULL;
  977. int ret;
  978. /* must have platform data */
  979. if (!plat) {
  980. pr_err("%s: Platform data not available\n", __func__);
  981. ret = -EINVAL;
  982. goto out;
  983. }
  984. if (pdev->id < 1 || pdev->id > 4)
  985. return -EINVAL;
  986. if (pdev->resource == NULL || pdev->num_resources < 2) {
  987. pr_err("%s: Invalid resource\n", __func__);
  988. return -ENXIO;
  989. }
  990. memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  991. dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  992. cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  993. "cmd_irq");
  994. stat_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  995. "status_irq");
  996. if (!cmd_irqres || !memres) {
  997. pr_err("%s: Invalid resource\n", __func__);
  998. return -ENXIO;
  999. }
  1000. /*
  1001. * Setup our host structure
  1002. */
  1003. mmc = mmc_alloc_host(sizeof(struct msmsdcc_host), &pdev->dev);
  1004. if (!mmc) {
  1005. ret = -ENOMEM;
  1006. goto out;
  1007. }
  1008. host = mmc_priv(mmc);
  1009. host->pdev_id = pdev->id;
  1010. host->plat = plat;
  1011. host->mmc = mmc;
  1012. host->curr.cmd = NULL;
  1013. init_timer(&host->busclk_timer);
  1014. host->busclk_timer.data = (unsigned long) host;
  1015. host->busclk_timer.function = msmsdcc_busclk_expired;
  1016. host->cmdpoll = 1;
  1017. host->base = ioremap(memres->start, PAGE_SIZE);
  1018. if (!host->base) {
  1019. ret = -ENOMEM;
  1020. goto host_free;
  1021. }
  1022. host->cmd_irqres = cmd_irqres;
  1023. host->memres = memres;
  1024. host->dmares = dmares;
  1025. spin_lock_init(&host->lock);
  1026. tasklet_init(&host->dma_tlet, msmsdcc_dma_complete_tlet,
  1027. (unsigned long)host);
  1028. /*
  1029. * Setup DMA
  1030. */
  1031. if (host->dmares) {
  1032. ret = msmsdcc_init_dma(host);
  1033. if (ret)
  1034. goto ioremap_free;
  1035. } else {
  1036. host->dma.channel = -1;
  1037. }
  1038. /* Get our clocks */
  1039. host->pclk = clk_get(&pdev->dev, "sdc_pclk");
  1040. if (IS_ERR(host->pclk)) {
  1041. ret = PTR_ERR(host->pclk);
  1042. goto dma_free;
  1043. }
  1044. host->clk = clk_get(&pdev->dev, "sdc_clk");
  1045. if (IS_ERR(host->clk)) {
  1046. ret = PTR_ERR(host->clk);
  1047. goto pclk_put;
  1048. }
  1049. ret = clk_set_rate(host->clk, msmsdcc_fmin);
  1050. if (ret) {
  1051. pr_err("%s: Clock rate set failed (%d)\n", __func__, ret);
  1052. goto clk_put;
  1053. }
  1054. ret = clk_prepare(host->pclk);
  1055. if (ret)
  1056. goto clk_put;
  1057. ret = clk_prepare(host->clk);
  1058. if (ret)
  1059. goto clk_unprepare_p;
  1060. /* Enable clocks */
  1061. ret = msmsdcc_enable_clocks(host);
  1062. if (ret)
  1063. goto clk_unprepare;
  1064. host->pclk_rate = clk_get_rate(host->pclk);
  1065. host->clk_rate = clk_get_rate(host->clk);
  1066. /*
  1067. * Setup MMC host structure
  1068. */
  1069. mmc->ops = &msmsdcc_ops;
  1070. mmc->f_min = msmsdcc_fmin;
  1071. mmc->f_max = msmsdcc_fmax;
  1072. mmc->ocr_avail = plat->ocr_mask;
  1073. if (msmsdcc_4bit)
  1074. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1075. if (msmsdcc_sdioirq)
  1076. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1077. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
  1078. mmc->max_segs = NR_SG;
  1079. mmc->max_blk_size = 4096; /* MCI_DATA_CTL BLOCKSIZE up to 4096 */
  1080. mmc->max_blk_count = 65536;
  1081. mmc->max_req_size = 33554432; /* MCI_DATA_LENGTH is 25 bits */
  1082. mmc->max_seg_size = mmc->max_req_size;
  1083. msmsdcc_writel(host, 0, MMCIMASK0);
  1084. msmsdcc_writel(host, 0x5e007ff, MMCICLEAR);
  1085. msmsdcc_writel(host, MCI_IRQENABLE, MMCIMASK0);
  1086. host->saved_irq0mask = MCI_IRQENABLE;
  1087. /*
  1088. * Setup card detect change
  1089. */
  1090. memset(&host->timer, 0, sizeof(host->timer));
  1091. if (stat_irqres && !(stat_irqres->flags & IORESOURCE_DISABLED)) {
  1092. unsigned long irqflags = IRQF_SHARED |
  1093. (stat_irqres->flags & IRQF_TRIGGER_MASK);
  1094. host->stat_irq = stat_irqres->start;
  1095. ret = request_irq(host->stat_irq,
  1096. msmsdcc_platform_status_irq,
  1097. irqflags,
  1098. DRIVER_NAME " (slot)",
  1099. host);
  1100. if (ret) {
  1101. pr_err("%s: Unable to get slot IRQ %d (%d)\n",
  1102. mmc_hostname(mmc), host->stat_irq, ret);
  1103. goto clk_disable;
  1104. }
  1105. } else if (plat->register_status_notify) {
  1106. plat->register_status_notify(msmsdcc_status_notify_cb, host);
  1107. } else if (!plat->status)
  1108. pr_err("%s: No card detect facilities available\n",
  1109. mmc_hostname(mmc));
  1110. else {
  1111. init_timer(&host->timer);
  1112. host->timer.data = (unsigned long)host;
  1113. host->timer.function = msmsdcc_check_status;
  1114. host->timer.expires = jiffies + HZ;
  1115. add_timer(&host->timer);
  1116. }
  1117. if (plat->status) {
  1118. host->oldstat = host->plat->status(mmc_dev(host->mmc));
  1119. host->eject = !host->oldstat;
  1120. }
  1121. ret = request_irq(cmd_irqres->start, msmsdcc_irq, IRQF_SHARED,
  1122. DRIVER_NAME " (cmd)", host);
  1123. if (ret)
  1124. goto stat_irq_free;
  1125. ret = request_irq(cmd_irqres->start, msmsdcc_pio_irq, IRQF_SHARED,
  1126. DRIVER_NAME " (pio)", host);
  1127. if (ret)
  1128. goto cmd_irq_free;
  1129. mmc_set_drvdata(pdev, mmc);
  1130. mmc_add_host(mmc);
  1131. pr_info("%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n",
  1132. mmc_hostname(mmc), (unsigned long long)memres->start,
  1133. (unsigned int) cmd_irqres->start,
  1134. (unsigned int) host->stat_irq, host->dma.channel);
  1135. pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc),
  1136. (mmc->caps & MMC_CAP_4_BIT_DATA ? "enabled" : "disabled"));
  1137. pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
  1138. mmc_hostname(mmc), msmsdcc_fmin, msmsdcc_fmax, host->pclk_rate);
  1139. pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc), host->eject);
  1140. pr_info("%s: Power save feature enable = %d\n",
  1141. mmc_hostname(mmc), msmsdcc_pwrsave);
  1142. if (host->dma.channel != -1) {
  1143. pr_info("%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n",
  1144. mmc_hostname(mmc), host->dma.nc, host->dma.nc_busaddr);
  1145. pr_info("%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n",
  1146. mmc_hostname(mmc), host->dma.cmd_busaddr,
  1147. host->dma.cmdptr_busaddr);
  1148. } else
  1149. pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc));
  1150. if (host->timer.function)
  1151. pr_info("%s: Polling status mode enabled\n", mmc_hostname(mmc));
  1152. return 0;
  1153. cmd_irq_free:
  1154. free_irq(cmd_irqres->start, host);
  1155. stat_irq_free:
  1156. if (host->stat_irq)
  1157. free_irq(host->stat_irq, host);
  1158. clk_disable:
  1159. msmsdcc_disable_clocks(host, 0);
  1160. clk_unprepare:
  1161. clk_unprepare(host->clk);
  1162. clk_unprepare_p:
  1163. clk_unprepare(host->pclk);
  1164. clk_put:
  1165. clk_put(host->clk);
  1166. pclk_put:
  1167. clk_put(host->pclk);
  1168. dma_free:
  1169. if (host->dmares)
  1170. dma_free_coherent(NULL, sizeof(struct msmsdcc_nc_dmadata),
  1171. host->dma.nc, host->dma.nc_busaddr);
  1172. ioremap_free:
  1173. tasklet_kill(&host->dma_tlet);
  1174. iounmap(host->base);
  1175. host_free:
  1176. mmc_free_host(mmc);
  1177. out:
  1178. return ret;
  1179. }
  1180. #ifdef CONFIG_PM
  1181. #ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
  1182. static void
  1183. do_resume_work(struct work_struct *work)
  1184. {
  1185. struct msmsdcc_host *host =
  1186. container_of(work, struct msmsdcc_host, resume_task);
  1187. struct mmc_host *mmc = host->mmc;
  1188. if (mmc) {
  1189. mmc_resume_host(mmc);
  1190. if (host->stat_irq)
  1191. enable_irq(host->stat_irq);
  1192. }
  1193. }
  1194. #endif
  1195. static int
  1196. msmsdcc_suspend(struct platform_device *dev, pm_message_t state)
  1197. {
  1198. struct mmc_host *mmc = mmc_get_drvdata(dev);
  1199. int rc = 0;
  1200. if (mmc) {
  1201. struct msmsdcc_host *host = mmc_priv(mmc);
  1202. if (host->stat_irq)
  1203. disable_irq(host->stat_irq);
  1204. if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
  1205. rc = mmc_suspend_host(mmc);
  1206. if (!rc)
  1207. msmsdcc_writel(host, 0, MMCIMASK0);
  1208. if (host->clks_on)
  1209. msmsdcc_disable_clocks(host, 0);
  1210. }
  1211. return rc;
  1212. }
  1213. static int
  1214. msmsdcc_resume(struct platform_device *dev)
  1215. {
  1216. struct mmc_host *mmc = mmc_get_drvdata(dev);
  1217. if (mmc) {
  1218. struct msmsdcc_host *host = mmc_priv(mmc);
  1219. msmsdcc_enable_clocks(host);
  1220. msmsdcc_writel(host, host->saved_irq0mask, MMCIMASK0);
  1221. if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
  1222. mmc_resume_host(mmc);
  1223. if (host->stat_irq)
  1224. enable_irq(host->stat_irq);
  1225. #if BUSCLK_PWRSAVE
  1226. msmsdcc_disable_clocks(host, 1);
  1227. #endif
  1228. }
  1229. return 0;
  1230. }
  1231. #else
  1232. #define msmsdcc_suspend 0
  1233. #define msmsdcc_resume 0
  1234. #endif
  1235. static struct platform_driver msmsdcc_driver = {
  1236. .probe = msmsdcc_probe,
  1237. .suspend = msmsdcc_suspend,
  1238. .resume = msmsdcc_resume,
  1239. .driver = {
  1240. .name = "msm_sdcc",
  1241. },
  1242. };
  1243. module_platform_driver(msmsdcc_driver);
  1244. MODULE_DESCRIPTION("Qualcomm MSM 7X00A Multimedia Card Interface driver");
  1245. MODULE_LICENSE("GPL");