msm_sdcc.c 34 KB

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