msm_sdcc.c 33 KB

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