davinci_mmc.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. /*
  2. * davinci_mmc.c - TI DaVinci MMC/SD/SDIO driver
  3. *
  4. * Copyright (C) 2006 Texas Instruments.
  5. * Original author: Purushotam Kumar
  6. * Copyright (C) 2009 David Brownell
  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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/ioport.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/clk.h>
  26. #include <linux/err.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/mmc/host.h>
  29. #include <linux/io.h>
  30. #include <linux/irq.h>
  31. #include <linux/delay.h>
  32. #include <linux/dmaengine.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/edma.h>
  35. #include <linux/mmc/mmc.h>
  36. #include <linux/platform_data/mmc-davinci.h>
  37. /*
  38. * Register Definitions
  39. */
  40. #define DAVINCI_MMCCTL 0x00 /* Control Register */
  41. #define DAVINCI_MMCCLK 0x04 /* Memory Clock Control Register */
  42. #define DAVINCI_MMCST0 0x08 /* Status Register 0 */
  43. #define DAVINCI_MMCST1 0x0C /* Status Register 1 */
  44. #define DAVINCI_MMCIM 0x10 /* Interrupt Mask Register */
  45. #define DAVINCI_MMCTOR 0x14 /* Response Time-Out Register */
  46. #define DAVINCI_MMCTOD 0x18 /* Data Read Time-Out Register */
  47. #define DAVINCI_MMCBLEN 0x1C /* Block Length Register */
  48. #define DAVINCI_MMCNBLK 0x20 /* Number of Blocks Register */
  49. #define DAVINCI_MMCNBLC 0x24 /* Number of Blocks Counter Register */
  50. #define DAVINCI_MMCDRR 0x28 /* Data Receive Register */
  51. #define DAVINCI_MMCDXR 0x2C /* Data Transmit Register */
  52. #define DAVINCI_MMCCMD 0x30 /* Command Register */
  53. #define DAVINCI_MMCARGHL 0x34 /* Argument Register */
  54. #define DAVINCI_MMCRSP01 0x38 /* Response Register 0 and 1 */
  55. #define DAVINCI_MMCRSP23 0x3C /* Response Register 0 and 1 */
  56. #define DAVINCI_MMCRSP45 0x40 /* Response Register 0 and 1 */
  57. #define DAVINCI_MMCRSP67 0x44 /* Response Register 0 and 1 */
  58. #define DAVINCI_MMCDRSP 0x48 /* Data Response Register */
  59. #define DAVINCI_MMCETOK 0x4C
  60. #define DAVINCI_MMCCIDX 0x50 /* Command Index Register */
  61. #define DAVINCI_MMCCKC 0x54
  62. #define DAVINCI_MMCTORC 0x58
  63. #define DAVINCI_MMCTODC 0x5C
  64. #define DAVINCI_MMCBLNC 0x60
  65. #define DAVINCI_SDIOCTL 0x64
  66. #define DAVINCI_SDIOST0 0x68
  67. #define DAVINCI_SDIOIEN 0x6C
  68. #define DAVINCI_SDIOIST 0x70
  69. #define DAVINCI_MMCFIFOCTL 0x74 /* FIFO Control Register */
  70. /* DAVINCI_MMCCTL definitions */
  71. #define MMCCTL_DATRST (1 << 0)
  72. #define MMCCTL_CMDRST (1 << 1)
  73. #define MMCCTL_WIDTH_8_BIT (1 << 8)
  74. #define MMCCTL_WIDTH_4_BIT (1 << 2)
  75. #define MMCCTL_DATEG_DISABLED (0 << 6)
  76. #define MMCCTL_DATEG_RISING (1 << 6)
  77. #define MMCCTL_DATEG_FALLING (2 << 6)
  78. #define MMCCTL_DATEG_BOTH (3 << 6)
  79. #define MMCCTL_PERMDR_LE (0 << 9)
  80. #define MMCCTL_PERMDR_BE (1 << 9)
  81. #define MMCCTL_PERMDX_LE (0 << 10)
  82. #define MMCCTL_PERMDX_BE (1 << 10)
  83. /* DAVINCI_MMCCLK definitions */
  84. #define MMCCLK_CLKEN (1 << 8)
  85. #define MMCCLK_CLKRT_MASK (0xFF << 0)
  86. /* IRQ bit definitions, for DAVINCI_MMCST0 and DAVINCI_MMCIM */
  87. #define MMCST0_DATDNE BIT(0) /* data done */
  88. #define MMCST0_BSYDNE BIT(1) /* busy done */
  89. #define MMCST0_RSPDNE BIT(2) /* command done */
  90. #define MMCST0_TOUTRD BIT(3) /* data read timeout */
  91. #define MMCST0_TOUTRS BIT(4) /* command response timeout */
  92. #define MMCST0_CRCWR BIT(5) /* data write CRC error */
  93. #define MMCST0_CRCRD BIT(6) /* data read CRC error */
  94. #define MMCST0_CRCRS BIT(7) /* command response CRC error */
  95. #define MMCST0_DXRDY BIT(9) /* data transmit ready (fifo empty) */
  96. #define MMCST0_DRRDY BIT(10) /* data receive ready (data in fifo)*/
  97. #define MMCST0_DATED BIT(11) /* DAT3 edge detect */
  98. #define MMCST0_TRNDNE BIT(12) /* transfer done */
  99. /* DAVINCI_MMCST1 definitions */
  100. #define MMCST1_BUSY (1 << 0)
  101. /* DAVINCI_MMCCMD definitions */
  102. #define MMCCMD_CMD_MASK (0x3F << 0)
  103. #define MMCCMD_PPLEN (1 << 7)
  104. #define MMCCMD_BSYEXP (1 << 8)
  105. #define MMCCMD_RSPFMT_MASK (3 << 9)
  106. #define MMCCMD_RSPFMT_NONE (0 << 9)
  107. #define MMCCMD_RSPFMT_R1456 (1 << 9)
  108. #define MMCCMD_RSPFMT_R2 (2 << 9)
  109. #define MMCCMD_RSPFMT_R3 (3 << 9)
  110. #define MMCCMD_DTRW (1 << 11)
  111. #define MMCCMD_STRMTP (1 << 12)
  112. #define MMCCMD_WDATX (1 << 13)
  113. #define MMCCMD_INITCK (1 << 14)
  114. #define MMCCMD_DCLR (1 << 15)
  115. #define MMCCMD_DMATRIG (1 << 16)
  116. /* DAVINCI_MMCFIFOCTL definitions */
  117. #define MMCFIFOCTL_FIFORST (1 << 0)
  118. #define MMCFIFOCTL_FIFODIR_WR (1 << 1)
  119. #define MMCFIFOCTL_FIFODIR_RD (0 << 1)
  120. #define MMCFIFOCTL_FIFOLEV (1 << 2) /* 0 = 128 bits, 1 = 256 bits */
  121. #define MMCFIFOCTL_ACCWD_4 (0 << 3) /* access width of 4 bytes */
  122. #define MMCFIFOCTL_ACCWD_3 (1 << 3) /* access width of 3 bytes */
  123. #define MMCFIFOCTL_ACCWD_2 (2 << 3) /* access width of 2 bytes */
  124. #define MMCFIFOCTL_ACCWD_1 (3 << 3) /* access width of 1 byte */
  125. /* DAVINCI_SDIOST0 definitions */
  126. #define SDIOST0_DAT1_HI BIT(0)
  127. /* DAVINCI_SDIOIEN definitions */
  128. #define SDIOIEN_IOINTEN BIT(0)
  129. /* DAVINCI_SDIOIST definitions */
  130. #define SDIOIST_IOINT BIT(0)
  131. /* MMCSD Init clock in Hz in opendrain mode */
  132. #define MMCSD_INIT_CLOCK 200000
  133. /*
  134. * One scatterlist dma "segment" is at most MAX_CCNT rw_threshold units,
  135. * and we handle up to MAX_NR_SG segments. MMC_BLOCK_BOUNCE kicks in only
  136. * for drivers with max_segs == 1, making the segments bigger (64KB)
  137. * than the page or two that's otherwise typical. nr_sg (passed from
  138. * platform data) == 16 gives at least the same throughput boost, using
  139. * EDMA transfer linkage instead of spending CPU time copying pages.
  140. */
  141. #define MAX_CCNT ((1 << 16) - 1)
  142. #define MAX_NR_SG 16
  143. static unsigned rw_threshold = 32;
  144. module_param(rw_threshold, uint, S_IRUGO);
  145. MODULE_PARM_DESC(rw_threshold,
  146. "Read/Write threshold. Default = 32");
  147. static unsigned poll_threshold = 128;
  148. module_param(poll_threshold, uint, S_IRUGO);
  149. MODULE_PARM_DESC(poll_threshold,
  150. "Polling transaction size threshold. Default = 128");
  151. static unsigned poll_loopcount = 32;
  152. module_param(poll_loopcount, uint, S_IRUGO);
  153. MODULE_PARM_DESC(poll_loopcount,
  154. "Maximum polling loop count. Default = 32");
  155. static unsigned __initdata use_dma = 1;
  156. module_param(use_dma, uint, 0);
  157. MODULE_PARM_DESC(use_dma, "Whether to use DMA or not. Default = 1");
  158. struct mmc_davinci_host {
  159. struct mmc_command *cmd;
  160. struct mmc_data *data;
  161. struct mmc_host *mmc;
  162. struct clk *clk;
  163. unsigned int mmc_input_clk;
  164. void __iomem *base;
  165. struct resource *mem_res;
  166. int mmc_irq, sdio_irq;
  167. unsigned char bus_mode;
  168. #define DAVINCI_MMC_DATADIR_NONE 0
  169. #define DAVINCI_MMC_DATADIR_READ 1
  170. #define DAVINCI_MMC_DATADIR_WRITE 2
  171. unsigned char data_dir;
  172. unsigned char suspended;
  173. /* buffer is used during PIO of one scatterlist segment, and
  174. * is updated along with buffer_bytes_left. bytes_left applies
  175. * to all N blocks of the PIO transfer.
  176. */
  177. u8 *buffer;
  178. u32 buffer_bytes_left;
  179. u32 bytes_left;
  180. u32 rxdma, txdma;
  181. struct dma_chan *dma_tx;
  182. struct dma_chan *dma_rx;
  183. bool use_dma;
  184. bool do_dma;
  185. bool sdio_int;
  186. bool active_request;
  187. /* For PIO we walk scatterlists one segment at a time. */
  188. unsigned int sg_len;
  189. struct scatterlist *sg;
  190. /* Version of the MMC/SD controller */
  191. u8 version;
  192. /* for ns in one cycle calculation */
  193. unsigned ns_in_one_cycle;
  194. /* Number of sg segments */
  195. u8 nr_sg;
  196. #ifdef CONFIG_CPU_FREQ
  197. struct notifier_block freq_transition;
  198. #endif
  199. };
  200. static irqreturn_t mmc_davinci_irq(int irq, void *dev_id);
  201. /* PIO only */
  202. static void mmc_davinci_sg_to_buf(struct mmc_davinci_host *host)
  203. {
  204. host->buffer_bytes_left = sg_dma_len(host->sg);
  205. host->buffer = sg_virt(host->sg);
  206. if (host->buffer_bytes_left > host->bytes_left)
  207. host->buffer_bytes_left = host->bytes_left;
  208. }
  209. static void davinci_fifo_data_trans(struct mmc_davinci_host *host,
  210. unsigned int n)
  211. {
  212. u8 *p;
  213. unsigned int i;
  214. if (host->buffer_bytes_left == 0) {
  215. host->sg = sg_next(host->data->sg);
  216. mmc_davinci_sg_to_buf(host);
  217. }
  218. p = host->buffer;
  219. if (n > host->buffer_bytes_left)
  220. n = host->buffer_bytes_left;
  221. host->buffer_bytes_left -= n;
  222. host->bytes_left -= n;
  223. /* NOTE: we never transfer more than rw_threshold bytes
  224. * to/from the fifo here; there's no I/O overlap.
  225. * This also assumes that access width( i.e. ACCWD) is 4 bytes
  226. */
  227. if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE) {
  228. for (i = 0; i < (n >> 2); i++) {
  229. writel(*((u32 *)p), host->base + DAVINCI_MMCDXR);
  230. p = p + 4;
  231. }
  232. if (n & 3) {
  233. iowrite8_rep(host->base + DAVINCI_MMCDXR, p, (n & 3));
  234. p = p + (n & 3);
  235. }
  236. } else {
  237. for (i = 0; i < (n >> 2); i++) {
  238. *((u32 *)p) = readl(host->base + DAVINCI_MMCDRR);
  239. p = p + 4;
  240. }
  241. if (n & 3) {
  242. ioread8_rep(host->base + DAVINCI_MMCDRR, p, (n & 3));
  243. p = p + (n & 3);
  244. }
  245. }
  246. host->buffer = p;
  247. }
  248. static void mmc_davinci_start_command(struct mmc_davinci_host *host,
  249. struct mmc_command *cmd)
  250. {
  251. u32 cmd_reg = 0;
  252. u32 im_val;
  253. dev_dbg(mmc_dev(host->mmc), "CMD%d, arg 0x%08x%s\n",
  254. cmd->opcode, cmd->arg,
  255. ({ char *s;
  256. switch (mmc_resp_type(cmd)) {
  257. case MMC_RSP_R1:
  258. s = ", R1/R5/R6/R7 response";
  259. break;
  260. case MMC_RSP_R1B:
  261. s = ", R1b response";
  262. break;
  263. case MMC_RSP_R2:
  264. s = ", R2 response";
  265. break;
  266. case MMC_RSP_R3:
  267. s = ", R3/R4 response";
  268. break;
  269. default:
  270. s = ", (R? response)";
  271. break;
  272. }; s; }));
  273. host->cmd = cmd;
  274. switch (mmc_resp_type(cmd)) {
  275. case MMC_RSP_R1B:
  276. /* There's some spec confusion about when R1B is
  277. * allowed, but if the card doesn't issue a BUSY
  278. * then it's harmless for us to allow it.
  279. */
  280. cmd_reg |= MMCCMD_BSYEXP;
  281. /* FALLTHROUGH */
  282. case MMC_RSP_R1: /* 48 bits, CRC */
  283. cmd_reg |= MMCCMD_RSPFMT_R1456;
  284. break;
  285. case MMC_RSP_R2: /* 136 bits, CRC */
  286. cmd_reg |= MMCCMD_RSPFMT_R2;
  287. break;
  288. case MMC_RSP_R3: /* 48 bits, no CRC */
  289. cmd_reg |= MMCCMD_RSPFMT_R3;
  290. break;
  291. default:
  292. cmd_reg |= MMCCMD_RSPFMT_NONE;
  293. dev_dbg(mmc_dev(host->mmc), "unknown resp_type %04x\n",
  294. mmc_resp_type(cmd));
  295. break;
  296. }
  297. /* Set command index */
  298. cmd_reg |= cmd->opcode;
  299. /* Enable EDMA transfer triggers */
  300. if (host->do_dma)
  301. cmd_reg |= MMCCMD_DMATRIG;
  302. if (host->version == MMC_CTLR_VERSION_2 && host->data != NULL &&
  303. host->data_dir == DAVINCI_MMC_DATADIR_READ)
  304. cmd_reg |= MMCCMD_DMATRIG;
  305. /* Setting whether command involves data transfer or not */
  306. if (cmd->data)
  307. cmd_reg |= MMCCMD_WDATX;
  308. /* Setting whether stream or block transfer */
  309. if (cmd->flags & MMC_DATA_STREAM)
  310. cmd_reg |= MMCCMD_STRMTP;
  311. /* Setting whether data read or write */
  312. if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE)
  313. cmd_reg |= MMCCMD_DTRW;
  314. if (host->bus_mode == MMC_BUSMODE_PUSHPULL)
  315. cmd_reg |= MMCCMD_PPLEN;
  316. /* set Command timeout */
  317. writel(0x1FFF, host->base + DAVINCI_MMCTOR);
  318. /* Enable interrupt (calculate here, defer until FIFO is stuffed). */
  319. im_val = MMCST0_RSPDNE | MMCST0_CRCRS | MMCST0_TOUTRS;
  320. if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE) {
  321. im_val |= MMCST0_DATDNE | MMCST0_CRCWR;
  322. if (!host->do_dma)
  323. im_val |= MMCST0_DXRDY;
  324. } else if (host->data_dir == DAVINCI_MMC_DATADIR_READ) {
  325. im_val |= MMCST0_DATDNE | MMCST0_CRCRD | MMCST0_TOUTRD;
  326. if (!host->do_dma)
  327. im_val |= MMCST0_DRRDY;
  328. }
  329. /*
  330. * Before non-DMA WRITE commands the controller needs priming:
  331. * FIFO should be populated with 32 bytes i.e. whatever is the FIFO size
  332. */
  333. if (!host->do_dma && (host->data_dir == DAVINCI_MMC_DATADIR_WRITE))
  334. davinci_fifo_data_trans(host, rw_threshold);
  335. writel(cmd->arg, host->base + DAVINCI_MMCARGHL);
  336. writel(cmd_reg, host->base + DAVINCI_MMCCMD);
  337. host->active_request = true;
  338. if (!host->do_dma && host->bytes_left <= poll_threshold) {
  339. u32 count = poll_loopcount;
  340. while (host->active_request && count--) {
  341. mmc_davinci_irq(0, host);
  342. cpu_relax();
  343. }
  344. }
  345. if (host->active_request)
  346. writel(im_val, host->base + DAVINCI_MMCIM);
  347. }
  348. /*----------------------------------------------------------------------*/
  349. /* DMA infrastructure */
  350. static void davinci_abort_dma(struct mmc_davinci_host *host)
  351. {
  352. struct dma_chan *sync_dev;
  353. if (host->data_dir == DAVINCI_MMC_DATADIR_READ)
  354. sync_dev = host->dma_rx;
  355. else
  356. sync_dev = host->dma_tx;
  357. dmaengine_terminate_all(sync_dev);
  358. }
  359. static int mmc_davinci_send_dma_request(struct mmc_davinci_host *host,
  360. struct mmc_data *data)
  361. {
  362. struct dma_chan *chan;
  363. struct dma_async_tx_descriptor *desc;
  364. int ret = 0;
  365. if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE) {
  366. struct dma_slave_config dma_tx_conf = {
  367. .direction = DMA_MEM_TO_DEV,
  368. .dst_addr = host->mem_res->start + DAVINCI_MMCDXR,
  369. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  370. .dst_maxburst =
  371. rw_threshold / DMA_SLAVE_BUSWIDTH_4_BYTES,
  372. };
  373. chan = host->dma_tx;
  374. dmaengine_slave_config(host->dma_tx, &dma_tx_conf);
  375. desc = dmaengine_prep_slave_sg(host->dma_tx,
  376. data->sg,
  377. host->sg_len,
  378. DMA_MEM_TO_DEV,
  379. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  380. if (!desc) {
  381. dev_dbg(mmc_dev(host->mmc),
  382. "failed to allocate DMA TX descriptor");
  383. ret = -1;
  384. goto out;
  385. }
  386. } else {
  387. struct dma_slave_config dma_rx_conf = {
  388. .direction = DMA_DEV_TO_MEM,
  389. .src_addr = host->mem_res->start + DAVINCI_MMCDRR,
  390. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  391. .src_maxburst =
  392. rw_threshold / DMA_SLAVE_BUSWIDTH_4_BYTES,
  393. };
  394. chan = host->dma_rx;
  395. dmaengine_slave_config(host->dma_rx, &dma_rx_conf);
  396. desc = dmaengine_prep_slave_sg(host->dma_rx,
  397. data->sg,
  398. host->sg_len,
  399. DMA_DEV_TO_MEM,
  400. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  401. if (!desc) {
  402. dev_dbg(mmc_dev(host->mmc),
  403. "failed to allocate DMA RX descriptor");
  404. ret = -1;
  405. goto out;
  406. }
  407. }
  408. dmaengine_submit(desc);
  409. dma_async_issue_pending(chan);
  410. out:
  411. return ret;
  412. }
  413. static int mmc_davinci_start_dma_transfer(struct mmc_davinci_host *host,
  414. struct mmc_data *data)
  415. {
  416. int i;
  417. int mask = rw_threshold - 1;
  418. int ret = 0;
  419. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  420. ((data->flags & MMC_DATA_WRITE)
  421. ? DMA_TO_DEVICE
  422. : DMA_FROM_DEVICE));
  423. /* no individual DMA segment should need a partial FIFO */
  424. for (i = 0; i < host->sg_len; i++) {
  425. if (sg_dma_len(data->sg + i) & mask) {
  426. dma_unmap_sg(mmc_dev(host->mmc),
  427. data->sg, data->sg_len,
  428. (data->flags & MMC_DATA_WRITE)
  429. ? DMA_TO_DEVICE
  430. : DMA_FROM_DEVICE);
  431. return -1;
  432. }
  433. }
  434. host->do_dma = 1;
  435. ret = mmc_davinci_send_dma_request(host, data);
  436. return ret;
  437. }
  438. static void __init_or_module
  439. davinci_release_dma_channels(struct mmc_davinci_host *host)
  440. {
  441. if (!host->use_dma)
  442. return;
  443. dma_release_channel(host->dma_tx);
  444. dma_release_channel(host->dma_rx);
  445. }
  446. static int __init davinci_acquire_dma_channels(struct mmc_davinci_host *host)
  447. {
  448. int r;
  449. dma_cap_mask_t mask;
  450. dma_cap_zero(mask);
  451. dma_cap_set(DMA_SLAVE, mask);
  452. host->dma_tx =
  453. dma_request_channel(mask, edma_filter_fn, &host->txdma);
  454. if (!host->dma_tx) {
  455. dev_err(mmc_dev(host->mmc), "Can't get dma_tx channel\n");
  456. return -ENODEV;
  457. }
  458. host->dma_rx =
  459. dma_request_channel(mask, edma_filter_fn, &host->rxdma);
  460. if (!host->dma_rx) {
  461. dev_err(mmc_dev(host->mmc), "Can't get dma_rx channel\n");
  462. r = -ENODEV;
  463. goto free_master_write;
  464. }
  465. return 0;
  466. free_master_write:
  467. dma_release_channel(host->dma_tx);
  468. return r;
  469. }
  470. /*----------------------------------------------------------------------*/
  471. static void
  472. mmc_davinci_prepare_data(struct mmc_davinci_host *host, struct mmc_request *req)
  473. {
  474. int fifo_lev = (rw_threshold == 32) ? MMCFIFOCTL_FIFOLEV : 0;
  475. int timeout;
  476. struct mmc_data *data = req->data;
  477. if (host->version == MMC_CTLR_VERSION_2)
  478. fifo_lev = (rw_threshold == 64) ? MMCFIFOCTL_FIFOLEV : 0;
  479. host->data = data;
  480. if (data == NULL) {
  481. host->data_dir = DAVINCI_MMC_DATADIR_NONE;
  482. writel(0, host->base + DAVINCI_MMCBLEN);
  483. writel(0, host->base + DAVINCI_MMCNBLK);
  484. return;
  485. }
  486. dev_dbg(mmc_dev(host->mmc), "%s %s, %d blocks of %d bytes\n",
  487. (data->flags & MMC_DATA_STREAM) ? "stream" : "block",
  488. (data->flags & MMC_DATA_WRITE) ? "write" : "read",
  489. data->blocks, data->blksz);
  490. dev_dbg(mmc_dev(host->mmc), " DTO %d cycles + %d ns\n",
  491. data->timeout_clks, data->timeout_ns);
  492. timeout = data->timeout_clks +
  493. (data->timeout_ns / host->ns_in_one_cycle);
  494. if (timeout > 0xffff)
  495. timeout = 0xffff;
  496. writel(timeout, host->base + DAVINCI_MMCTOD);
  497. writel(data->blocks, host->base + DAVINCI_MMCNBLK);
  498. writel(data->blksz, host->base + DAVINCI_MMCBLEN);
  499. /* Configure the FIFO */
  500. switch (data->flags & MMC_DATA_WRITE) {
  501. case MMC_DATA_WRITE:
  502. host->data_dir = DAVINCI_MMC_DATADIR_WRITE;
  503. writel(fifo_lev | MMCFIFOCTL_FIFODIR_WR | MMCFIFOCTL_FIFORST,
  504. host->base + DAVINCI_MMCFIFOCTL);
  505. writel(fifo_lev | MMCFIFOCTL_FIFODIR_WR,
  506. host->base + DAVINCI_MMCFIFOCTL);
  507. break;
  508. default:
  509. host->data_dir = DAVINCI_MMC_DATADIR_READ;
  510. writel(fifo_lev | MMCFIFOCTL_FIFODIR_RD | MMCFIFOCTL_FIFORST,
  511. host->base + DAVINCI_MMCFIFOCTL);
  512. writel(fifo_lev | MMCFIFOCTL_FIFODIR_RD,
  513. host->base + DAVINCI_MMCFIFOCTL);
  514. break;
  515. }
  516. host->buffer = NULL;
  517. host->bytes_left = data->blocks * data->blksz;
  518. /* For now we try to use DMA whenever we won't need partial FIFO
  519. * reads or writes, either for the whole transfer (as tested here)
  520. * or for any individual scatterlist segment (tested when we call
  521. * start_dma_transfer).
  522. *
  523. * While we *could* change that, unusual block sizes are rarely
  524. * used. The occasional fallback to PIO should't hurt.
  525. */
  526. if (host->use_dma && (host->bytes_left & (rw_threshold - 1)) == 0
  527. && mmc_davinci_start_dma_transfer(host, data) == 0) {
  528. /* zero this to ensure we take no PIO paths */
  529. host->bytes_left = 0;
  530. } else {
  531. /* Revert to CPU Copy */
  532. host->sg_len = data->sg_len;
  533. host->sg = host->data->sg;
  534. mmc_davinci_sg_to_buf(host);
  535. }
  536. }
  537. static void mmc_davinci_request(struct mmc_host *mmc, struct mmc_request *req)
  538. {
  539. struct mmc_davinci_host *host = mmc_priv(mmc);
  540. unsigned long timeout = jiffies + msecs_to_jiffies(900);
  541. u32 mmcst1 = 0;
  542. /* Card may still be sending BUSY after a previous operation,
  543. * typically some kind of write. If so, we can't proceed yet.
  544. */
  545. while (time_before(jiffies, timeout)) {
  546. mmcst1 = readl(host->base + DAVINCI_MMCST1);
  547. if (!(mmcst1 & MMCST1_BUSY))
  548. break;
  549. cpu_relax();
  550. }
  551. if (mmcst1 & MMCST1_BUSY) {
  552. dev_err(mmc_dev(host->mmc), "still BUSY? bad ... \n");
  553. req->cmd->error = -ETIMEDOUT;
  554. mmc_request_done(mmc, req);
  555. return;
  556. }
  557. host->do_dma = 0;
  558. mmc_davinci_prepare_data(host, req);
  559. mmc_davinci_start_command(host, req->cmd);
  560. }
  561. static unsigned int calculate_freq_for_card(struct mmc_davinci_host *host,
  562. unsigned int mmc_req_freq)
  563. {
  564. unsigned int mmc_freq = 0, mmc_pclk = 0, mmc_push_pull_divisor = 0;
  565. mmc_pclk = host->mmc_input_clk;
  566. if (mmc_req_freq && mmc_pclk > (2 * mmc_req_freq))
  567. mmc_push_pull_divisor = ((unsigned int)mmc_pclk
  568. / (2 * mmc_req_freq)) - 1;
  569. else
  570. mmc_push_pull_divisor = 0;
  571. mmc_freq = (unsigned int)mmc_pclk
  572. / (2 * (mmc_push_pull_divisor + 1));
  573. if (mmc_freq > mmc_req_freq)
  574. mmc_push_pull_divisor = mmc_push_pull_divisor + 1;
  575. /* Convert ns to clock cycles */
  576. if (mmc_req_freq <= 400000)
  577. host->ns_in_one_cycle = (1000000) / (((mmc_pclk
  578. / (2 * (mmc_push_pull_divisor + 1)))/1000));
  579. else
  580. host->ns_in_one_cycle = (1000000) / (((mmc_pclk
  581. / (2 * (mmc_push_pull_divisor + 1)))/1000000));
  582. return mmc_push_pull_divisor;
  583. }
  584. static void calculate_clk_divider(struct mmc_host *mmc, struct mmc_ios *ios)
  585. {
  586. unsigned int open_drain_freq = 0, mmc_pclk = 0;
  587. unsigned int mmc_push_pull_freq = 0;
  588. struct mmc_davinci_host *host = mmc_priv(mmc);
  589. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
  590. u32 temp;
  591. /* Ignoring the init clock value passed for fixing the inter
  592. * operability with different cards.
  593. */
  594. open_drain_freq = ((unsigned int)mmc_pclk
  595. / (2 * MMCSD_INIT_CLOCK)) - 1;
  596. if (open_drain_freq > 0xFF)
  597. open_drain_freq = 0xFF;
  598. temp = readl(host->base + DAVINCI_MMCCLK) & ~MMCCLK_CLKRT_MASK;
  599. temp |= open_drain_freq;
  600. writel(temp, host->base + DAVINCI_MMCCLK);
  601. /* Convert ns to clock cycles */
  602. host->ns_in_one_cycle = (1000000) / (MMCSD_INIT_CLOCK/1000);
  603. } else {
  604. u32 temp;
  605. mmc_push_pull_freq = calculate_freq_for_card(host, ios->clock);
  606. if (mmc_push_pull_freq > 0xFF)
  607. mmc_push_pull_freq = 0xFF;
  608. temp = readl(host->base + DAVINCI_MMCCLK) & ~MMCCLK_CLKEN;
  609. writel(temp, host->base + DAVINCI_MMCCLK);
  610. udelay(10);
  611. temp = readl(host->base + DAVINCI_MMCCLK) & ~MMCCLK_CLKRT_MASK;
  612. temp |= mmc_push_pull_freq;
  613. writel(temp, host->base + DAVINCI_MMCCLK);
  614. writel(temp | MMCCLK_CLKEN, host->base + DAVINCI_MMCCLK);
  615. udelay(10);
  616. }
  617. }
  618. static void mmc_davinci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  619. {
  620. struct mmc_davinci_host *host = mmc_priv(mmc);
  621. struct platform_device *pdev = to_platform_device(mmc->parent);
  622. struct davinci_mmc_config *config = pdev->dev.platform_data;
  623. dev_dbg(mmc_dev(host->mmc),
  624. "clock %dHz busmode %d powermode %d Vdd %04x\n",
  625. ios->clock, ios->bus_mode, ios->power_mode,
  626. ios->vdd);
  627. switch (ios->power_mode) {
  628. case MMC_POWER_OFF:
  629. if (config && config->set_power)
  630. config->set_power(pdev->id, false);
  631. break;
  632. case MMC_POWER_UP:
  633. if (config && config->set_power)
  634. config->set_power(pdev->id, true);
  635. break;
  636. }
  637. switch (ios->bus_width) {
  638. case MMC_BUS_WIDTH_8:
  639. dev_dbg(mmc_dev(host->mmc), "Enabling 8 bit mode\n");
  640. writel((readl(host->base + DAVINCI_MMCCTL) &
  641. ~MMCCTL_WIDTH_4_BIT) | MMCCTL_WIDTH_8_BIT,
  642. host->base + DAVINCI_MMCCTL);
  643. break;
  644. case MMC_BUS_WIDTH_4:
  645. dev_dbg(mmc_dev(host->mmc), "Enabling 4 bit mode\n");
  646. if (host->version == MMC_CTLR_VERSION_2)
  647. writel((readl(host->base + DAVINCI_MMCCTL) &
  648. ~MMCCTL_WIDTH_8_BIT) | MMCCTL_WIDTH_4_BIT,
  649. host->base + DAVINCI_MMCCTL);
  650. else
  651. writel(readl(host->base + DAVINCI_MMCCTL) |
  652. MMCCTL_WIDTH_4_BIT,
  653. host->base + DAVINCI_MMCCTL);
  654. break;
  655. case MMC_BUS_WIDTH_1:
  656. dev_dbg(mmc_dev(host->mmc), "Enabling 1 bit mode\n");
  657. if (host->version == MMC_CTLR_VERSION_2)
  658. writel(readl(host->base + DAVINCI_MMCCTL) &
  659. ~(MMCCTL_WIDTH_8_BIT | MMCCTL_WIDTH_4_BIT),
  660. host->base + DAVINCI_MMCCTL);
  661. else
  662. writel(readl(host->base + DAVINCI_MMCCTL) &
  663. ~MMCCTL_WIDTH_4_BIT,
  664. host->base + DAVINCI_MMCCTL);
  665. break;
  666. }
  667. calculate_clk_divider(mmc, ios);
  668. host->bus_mode = ios->bus_mode;
  669. if (ios->power_mode == MMC_POWER_UP) {
  670. unsigned long timeout = jiffies + msecs_to_jiffies(50);
  671. bool lose = true;
  672. /* Send clock cycles, poll completion */
  673. writel(0, host->base + DAVINCI_MMCARGHL);
  674. writel(MMCCMD_INITCK, host->base + DAVINCI_MMCCMD);
  675. while (time_before(jiffies, timeout)) {
  676. u32 tmp = readl(host->base + DAVINCI_MMCST0);
  677. if (tmp & MMCST0_RSPDNE) {
  678. lose = false;
  679. break;
  680. }
  681. cpu_relax();
  682. }
  683. if (lose)
  684. dev_warn(mmc_dev(host->mmc), "powerup timeout\n");
  685. }
  686. /* FIXME on power OFF, reset things ... */
  687. }
  688. static void
  689. mmc_davinci_xfer_done(struct mmc_davinci_host *host, struct mmc_data *data)
  690. {
  691. host->data = NULL;
  692. if (host->mmc->caps & MMC_CAP_SDIO_IRQ) {
  693. /*
  694. * SDIO Interrupt Detection work-around as suggested by
  695. * Davinci Errata (TMS320DM355 Silicon Revision 1.1 Errata
  696. * 2.1.6): Signal SDIO interrupt only if it is enabled by core
  697. */
  698. if (host->sdio_int && !(readl(host->base + DAVINCI_SDIOST0) &
  699. SDIOST0_DAT1_HI)) {
  700. writel(SDIOIST_IOINT, host->base + DAVINCI_SDIOIST);
  701. mmc_signal_sdio_irq(host->mmc);
  702. }
  703. }
  704. if (host->do_dma) {
  705. davinci_abort_dma(host);
  706. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  707. (data->flags & MMC_DATA_WRITE)
  708. ? DMA_TO_DEVICE
  709. : DMA_FROM_DEVICE);
  710. host->do_dma = false;
  711. }
  712. host->data_dir = DAVINCI_MMC_DATADIR_NONE;
  713. if (!data->stop || (host->cmd && host->cmd->error)) {
  714. mmc_request_done(host->mmc, data->mrq);
  715. writel(0, host->base + DAVINCI_MMCIM);
  716. host->active_request = false;
  717. } else
  718. mmc_davinci_start_command(host, data->stop);
  719. }
  720. static void mmc_davinci_cmd_done(struct mmc_davinci_host *host,
  721. struct mmc_command *cmd)
  722. {
  723. host->cmd = NULL;
  724. if (cmd->flags & MMC_RSP_PRESENT) {
  725. if (cmd->flags & MMC_RSP_136) {
  726. /* response type 2 */
  727. cmd->resp[3] = readl(host->base + DAVINCI_MMCRSP01);
  728. cmd->resp[2] = readl(host->base + DAVINCI_MMCRSP23);
  729. cmd->resp[1] = readl(host->base + DAVINCI_MMCRSP45);
  730. cmd->resp[0] = readl(host->base + DAVINCI_MMCRSP67);
  731. } else {
  732. /* response types 1, 1b, 3, 4, 5, 6 */
  733. cmd->resp[0] = readl(host->base + DAVINCI_MMCRSP67);
  734. }
  735. }
  736. if (host->data == NULL || cmd->error) {
  737. if (cmd->error == -ETIMEDOUT)
  738. cmd->mrq->cmd->retries = 0;
  739. mmc_request_done(host->mmc, cmd->mrq);
  740. writel(0, host->base + DAVINCI_MMCIM);
  741. host->active_request = false;
  742. }
  743. }
  744. static inline void mmc_davinci_reset_ctrl(struct mmc_davinci_host *host,
  745. int val)
  746. {
  747. u32 temp;
  748. temp = readl(host->base + DAVINCI_MMCCTL);
  749. if (val) /* reset */
  750. temp |= MMCCTL_CMDRST | MMCCTL_DATRST;
  751. else /* enable */
  752. temp &= ~(MMCCTL_CMDRST | MMCCTL_DATRST);
  753. writel(temp, host->base + DAVINCI_MMCCTL);
  754. udelay(10);
  755. }
  756. static void
  757. davinci_abort_data(struct mmc_davinci_host *host, struct mmc_data *data)
  758. {
  759. mmc_davinci_reset_ctrl(host, 1);
  760. mmc_davinci_reset_ctrl(host, 0);
  761. }
  762. static irqreturn_t mmc_davinci_sdio_irq(int irq, void *dev_id)
  763. {
  764. struct mmc_davinci_host *host = dev_id;
  765. unsigned int status;
  766. status = readl(host->base + DAVINCI_SDIOIST);
  767. if (status & SDIOIST_IOINT) {
  768. dev_dbg(mmc_dev(host->mmc),
  769. "SDIO interrupt status %x\n", status);
  770. writel(status | SDIOIST_IOINT, host->base + DAVINCI_SDIOIST);
  771. mmc_signal_sdio_irq(host->mmc);
  772. }
  773. return IRQ_HANDLED;
  774. }
  775. static irqreturn_t mmc_davinci_irq(int irq, void *dev_id)
  776. {
  777. struct mmc_davinci_host *host = (struct mmc_davinci_host *)dev_id;
  778. unsigned int status, qstatus;
  779. int end_command = 0;
  780. int end_transfer = 0;
  781. struct mmc_data *data = host->data;
  782. if (host->cmd == NULL && host->data == NULL) {
  783. status = readl(host->base + DAVINCI_MMCST0);
  784. dev_dbg(mmc_dev(host->mmc),
  785. "Spurious interrupt 0x%04x\n", status);
  786. /* Disable the interrupt from mmcsd */
  787. writel(0, host->base + DAVINCI_MMCIM);
  788. return IRQ_NONE;
  789. }
  790. status = readl(host->base + DAVINCI_MMCST0);
  791. qstatus = status;
  792. /* handle FIFO first when using PIO for data.
  793. * bytes_left will decrease to zero as I/O progress and status will
  794. * read zero over iteration because this controller status
  795. * register(MMCST0) reports any status only once and it is cleared
  796. * by read. So, it is not unbouned loop even in the case of
  797. * non-dma.
  798. */
  799. if (host->bytes_left && (status & (MMCST0_DXRDY | MMCST0_DRRDY))) {
  800. unsigned long im_val;
  801. /*
  802. * If interrupts fire during the following loop, they will be
  803. * handled by the handler, but the PIC will still buffer these.
  804. * As a result, the handler will be called again to serve these
  805. * needlessly. In order to avoid these spurious interrupts,
  806. * keep interrupts masked during the loop.
  807. */
  808. im_val = readl(host->base + DAVINCI_MMCIM);
  809. writel(0, host->base + DAVINCI_MMCIM);
  810. do {
  811. davinci_fifo_data_trans(host, rw_threshold);
  812. status = readl(host->base + DAVINCI_MMCST0);
  813. qstatus |= status;
  814. } while (host->bytes_left &&
  815. (status & (MMCST0_DXRDY | MMCST0_DRRDY)));
  816. /*
  817. * If an interrupt is pending, it is assumed it will fire when
  818. * it is unmasked. This assumption is also taken when the MMCIM
  819. * is first set. Otherwise, writing to MMCIM after reading the
  820. * status is race-prone.
  821. */
  822. writel(im_val, host->base + DAVINCI_MMCIM);
  823. }
  824. if (qstatus & MMCST0_DATDNE) {
  825. /* All blocks sent/received, and CRC checks passed */
  826. if (data != NULL) {
  827. if ((host->do_dma == 0) && (host->bytes_left > 0)) {
  828. /* if datasize < rw_threshold
  829. * no RX ints are generated
  830. */
  831. davinci_fifo_data_trans(host, host->bytes_left);
  832. }
  833. end_transfer = 1;
  834. data->bytes_xfered = data->blocks * data->blksz;
  835. } else {
  836. dev_err(mmc_dev(host->mmc),
  837. "DATDNE with no host->data\n");
  838. }
  839. }
  840. if (qstatus & MMCST0_TOUTRD) {
  841. /* Read data timeout */
  842. data->error = -ETIMEDOUT;
  843. end_transfer = 1;
  844. dev_dbg(mmc_dev(host->mmc),
  845. "read data timeout, status %x\n",
  846. qstatus);
  847. davinci_abort_data(host, data);
  848. }
  849. if (qstatus & (MMCST0_CRCWR | MMCST0_CRCRD)) {
  850. /* Data CRC error */
  851. data->error = -EILSEQ;
  852. end_transfer = 1;
  853. /* NOTE: this controller uses CRCWR to report both CRC
  854. * errors and timeouts (on writes). MMCDRSP values are
  855. * only weakly documented, but 0x9f was clearly a timeout
  856. * case and the two three-bit patterns in various SD specs
  857. * (101, 010) aren't part of it ...
  858. */
  859. if (qstatus & MMCST0_CRCWR) {
  860. u32 temp = readb(host->base + DAVINCI_MMCDRSP);
  861. if (temp == 0x9f)
  862. data->error = -ETIMEDOUT;
  863. }
  864. dev_dbg(mmc_dev(host->mmc), "data %s %s error\n",
  865. (qstatus & MMCST0_CRCWR) ? "write" : "read",
  866. (data->error == -ETIMEDOUT) ? "timeout" : "CRC");
  867. davinci_abort_data(host, data);
  868. }
  869. if (qstatus & MMCST0_TOUTRS) {
  870. /* Command timeout */
  871. if (host->cmd) {
  872. dev_dbg(mmc_dev(host->mmc),
  873. "CMD%d timeout, status %x\n",
  874. host->cmd->opcode, qstatus);
  875. host->cmd->error = -ETIMEDOUT;
  876. if (data) {
  877. end_transfer = 1;
  878. davinci_abort_data(host, data);
  879. } else
  880. end_command = 1;
  881. }
  882. }
  883. if (qstatus & MMCST0_CRCRS) {
  884. /* Command CRC error */
  885. dev_dbg(mmc_dev(host->mmc), "Command CRC error\n");
  886. if (host->cmd) {
  887. host->cmd->error = -EILSEQ;
  888. end_command = 1;
  889. }
  890. }
  891. if (qstatus & MMCST0_RSPDNE) {
  892. /* End of command phase */
  893. end_command = (int) host->cmd;
  894. }
  895. if (end_command)
  896. mmc_davinci_cmd_done(host, host->cmd);
  897. if (end_transfer)
  898. mmc_davinci_xfer_done(host, data);
  899. return IRQ_HANDLED;
  900. }
  901. static int mmc_davinci_get_cd(struct mmc_host *mmc)
  902. {
  903. struct platform_device *pdev = to_platform_device(mmc->parent);
  904. struct davinci_mmc_config *config = pdev->dev.platform_data;
  905. if (!config || !config->get_cd)
  906. return -ENOSYS;
  907. return config->get_cd(pdev->id);
  908. }
  909. static int mmc_davinci_get_ro(struct mmc_host *mmc)
  910. {
  911. struct platform_device *pdev = to_platform_device(mmc->parent);
  912. struct davinci_mmc_config *config = pdev->dev.platform_data;
  913. if (!config || !config->get_ro)
  914. return -ENOSYS;
  915. return config->get_ro(pdev->id);
  916. }
  917. static void mmc_davinci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  918. {
  919. struct mmc_davinci_host *host = mmc_priv(mmc);
  920. if (enable) {
  921. if (!(readl(host->base + DAVINCI_SDIOST0) & SDIOST0_DAT1_HI)) {
  922. writel(SDIOIST_IOINT, host->base + DAVINCI_SDIOIST);
  923. mmc_signal_sdio_irq(host->mmc);
  924. } else {
  925. host->sdio_int = true;
  926. writel(readl(host->base + DAVINCI_SDIOIEN) |
  927. SDIOIEN_IOINTEN, host->base + DAVINCI_SDIOIEN);
  928. }
  929. } else {
  930. host->sdio_int = false;
  931. writel(readl(host->base + DAVINCI_SDIOIEN) & ~SDIOIEN_IOINTEN,
  932. host->base + DAVINCI_SDIOIEN);
  933. }
  934. }
  935. static struct mmc_host_ops mmc_davinci_ops = {
  936. .request = mmc_davinci_request,
  937. .set_ios = mmc_davinci_set_ios,
  938. .get_cd = mmc_davinci_get_cd,
  939. .get_ro = mmc_davinci_get_ro,
  940. .enable_sdio_irq = mmc_davinci_enable_sdio_irq,
  941. };
  942. /*----------------------------------------------------------------------*/
  943. #ifdef CONFIG_CPU_FREQ
  944. static int mmc_davinci_cpufreq_transition(struct notifier_block *nb,
  945. unsigned long val, void *data)
  946. {
  947. struct mmc_davinci_host *host;
  948. unsigned int mmc_pclk;
  949. struct mmc_host *mmc;
  950. unsigned long flags;
  951. host = container_of(nb, struct mmc_davinci_host, freq_transition);
  952. mmc = host->mmc;
  953. mmc_pclk = clk_get_rate(host->clk);
  954. if (val == CPUFREQ_POSTCHANGE) {
  955. spin_lock_irqsave(&mmc->lock, flags);
  956. host->mmc_input_clk = mmc_pclk;
  957. calculate_clk_divider(mmc, &mmc->ios);
  958. spin_unlock_irqrestore(&mmc->lock, flags);
  959. }
  960. return 0;
  961. }
  962. static inline int mmc_davinci_cpufreq_register(struct mmc_davinci_host *host)
  963. {
  964. host->freq_transition.notifier_call = mmc_davinci_cpufreq_transition;
  965. return cpufreq_register_notifier(&host->freq_transition,
  966. CPUFREQ_TRANSITION_NOTIFIER);
  967. }
  968. static inline void mmc_davinci_cpufreq_deregister(struct mmc_davinci_host *host)
  969. {
  970. cpufreq_unregister_notifier(&host->freq_transition,
  971. CPUFREQ_TRANSITION_NOTIFIER);
  972. }
  973. #else
  974. static inline int mmc_davinci_cpufreq_register(struct mmc_davinci_host *host)
  975. {
  976. return 0;
  977. }
  978. static inline void mmc_davinci_cpufreq_deregister(struct mmc_davinci_host *host)
  979. {
  980. }
  981. #endif
  982. static void __init init_mmcsd_host(struct mmc_davinci_host *host)
  983. {
  984. mmc_davinci_reset_ctrl(host, 1);
  985. writel(0, host->base + DAVINCI_MMCCLK);
  986. writel(MMCCLK_CLKEN, host->base + DAVINCI_MMCCLK);
  987. writel(0x1FFF, host->base + DAVINCI_MMCTOR);
  988. writel(0xFFFF, host->base + DAVINCI_MMCTOD);
  989. mmc_davinci_reset_ctrl(host, 0);
  990. }
  991. static int __init davinci_mmcsd_probe(struct platform_device *pdev)
  992. {
  993. struct davinci_mmc_config *pdata = pdev->dev.platform_data;
  994. struct mmc_davinci_host *host = NULL;
  995. struct mmc_host *mmc = NULL;
  996. struct resource *r, *mem = NULL;
  997. int ret = 0, irq = 0;
  998. size_t mem_size;
  999. /* REVISIT: when we're fully converted, fail if pdata is NULL */
  1000. ret = -ENODEV;
  1001. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1002. irq = platform_get_irq(pdev, 0);
  1003. if (!r || irq == NO_IRQ)
  1004. goto out;
  1005. ret = -EBUSY;
  1006. mem_size = resource_size(r);
  1007. mem = request_mem_region(r->start, mem_size, pdev->name);
  1008. if (!mem)
  1009. goto out;
  1010. ret = -ENOMEM;
  1011. mmc = mmc_alloc_host(sizeof(struct mmc_davinci_host), &pdev->dev);
  1012. if (!mmc)
  1013. goto out;
  1014. host = mmc_priv(mmc);
  1015. host->mmc = mmc; /* Important */
  1016. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1017. if (!r)
  1018. goto out;
  1019. host->rxdma = r->start;
  1020. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  1021. if (!r)
  1022. goto out;
  1023. host->txdma = r->start;
  1024. host->mem_res = mem;
  1025. host->base = ioremap(mem->start, mem_size);
  1026. if (!host->base)
  1027. goto out;
  1028. ret = -ENXIO;
  1029. host->clk = clk_get(&pdev->dev, "MMCSDCLK");
  1030. if (IS_ERR(host->clk)) {
  1031. ret = PTR_ERR(host->clk);
  1032. goto out;
  1033. }
  1034. clk_enable(host->clk);
  1035. host->mmc_input_clk = clk_get_rate(host->clk);
  1036. init_mmcsd_host(host);
  1037. if (pdata->nr_sg)
  1038. host->nr_sg = pdata->nr_sg - 1;
  1039. if (host->nr_sg > MAX_NR_SG || !host->nr_sg)
  1040. host->nr_sg = MAX_NR_SG;
  1041. host->use_dma = use_dma;
  1042. host->mmc_irq = irq;
  1043. host->sdio_irq = platform_get_irq(pdev, 1);
  1044. if (host->use_dma && davinci_acquire_dma_channels(host) != 0)
  1045. host->use_dma = 0;
  1046. /* REVISIT: someday, support IRQ-driven card detection. */
  1047. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1048. mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
  1049. if (pdata && (pdata->wires == 4 || pdata->wires == 0))
  1050. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1051. if (pdata && (pdata->wires == 8))
  1052. mmc->caps |= (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA);
  1053. host->version = pdata->version;
  1054. mmc->ops = &mmc_davinci_ops;
  1055. mmc->f_min = 312500;
  1056. mmc->f_max = 25000000;
  1057. if (pdata && pdata->max_freq)
  1058. mmc->f_max = pdata->max_freq;
  1059. if (pdata && pdata->caps)
  1060. mmc->caps |= pdata->caps;
  1061. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1062. /* With no iommu coalescing pages, each phys_seg is a hw_seg.
  1063. * Each hw_seg uses one EDMA parameter RAM slot, always one
  1064. * channel and then usually some linked slots.
  1065. */
  1066. mmc->max_segs = MAX_NR_SG;
  1067. /* EDMA limit per hw segment (one or two MBytes) */
  1068. mmc->max_seg_size = MAX_CCNT * rw_threshold;
  1069. /* MMC/SD controller limits for multiblock requests */
  1070. mmc->max_blk_size = 4095; /* BLEN is 12 bits */
  1071. mmc->max_blk_count = 65535; /* NBLK is 16 bits */
  1072. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1073. dev_dbg(mmc_dev(host->mmc), "max_segs=%d\n", mmc->max_segs);
  1074. dev_dbg(mmc_dev(host->mmc), "max_blk_size=%d\n", mmc->max_blk_size);
  1075. dev_dbg(mmc_dev(host->mmc), "max_req_size=%d\n", mmc->max_req_size);
  1076. dev_dbg(mmc_dev(host->mmc), "max_seg_size=%d\n", mmc->max_seg_size);
  1077. platform_set_drvdata(pdev, host);
  1078. ret = mmc_davinci_cpufreq_register(host);
  1079. if (ret) {
  1080. dev_err(&pdev->dev, "failed to register cpufreq\n");
  1081. goto cpu_freq_fail;
  1082. }
  1083. ret = mmc_add_host(mmc);
  1084. if (ret < 0)
  1085. goto out;
  1086. ret = request_irq(irq, mmc_davinci_irq, 0, mmc_hostname(mmc), host);
  1087. if (ret)
  1088. goto out;
  1089. if (host->sdio_irq >= 0) {
  1090. ret = request_irq(host->sdio_irq, mmc_davinci_sdio_irq, 0,
  1091. mmc_hostname(mmc), host);
  1092. if (!ret)
  1093. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1094. }
  1095. rename_region(mem, mmc_hostname(mmc));
  1096. dev_info(mmc_dev(host->mmc), "Using %s, %d-bit mode\n",
  1097. host->use_dma ? "DMA" : "PIO",
  1098. (mmc->caps & MMC_CAP_4_BIT_DATA) ? 4 : 1);
  1099. return 0;
  1100. out:
  1101. mmc_davinci_cpufreq_deregister(host);
  1102. cpu_freq_fail:
  1103. if (host) {
  1104. davinci_release_dma_channels(host);
  1105. if (host->clk) {
  1106. clk_disable(host->clk);
  1107. clk_put(host->clk);
  1108. }
  1109. if (host->base)
  1110. iounmap(host->base);
  1111. }
  1112. if (mmc)
  1113. mmc_free_host(mmc);
  1114. if (mem)
  1115. release_resource(mem);
  1116. dev_dbg(&pdev->dev, "probe err %d\n", ret);
  1117. return ret;
  1118. }
  1119. static int __exit davinci_mmcsd_remove(struct platform_device *pdev)
  1120. {
  1121. struct mmc_davinci_host *host = platform_get_drvdata(pdev);
  1122. platform_set_drvdata(pdev, NULL);
  1123. if (host) {
  1124. mmc_davinci_cpufreq_deregister(host);
  1125. mmc_remove_host(host->mmc);
  1126. free_irq(host->mmc_irq, host);
  1127. if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
  1128. free_irq(host->sdio_irq, host);
  1129. davinci_release_dma_channels(host);
  1130. clk_disable(host->clk);
  1131. clk_put(host->clk);
  1132. iounmap(host->base);
  1133. release_resource(host->mem_res);
  1134. mmc_free_host(host->mmc);
  1135. }
  1136. return 0;
  1137. }
  1138. #ifdef CONFIG_PM
  1139. static int davinci_mmcsd_suspend(struct device *dev)
  1140. {
  1141. struct platform_device *pdev = to_platform_device(dev);
  1142. struct mmc_davinci_host *host = platform_get_drvdata(pdev);
  1143. int ret;
  1144. ret = mmc_suspend_host(host->mmc);
  1145. if (!ret) {
  1146. writel(0, host->base + DAVINCI_MMCIM);
  1147. mmc_davinci_reset_ctrl(host, 1);
  1148. clk_disable(host->clk);
  1149. host->suspended = 1;
  1150. } else {
  1151. host->suspended = 0;
  1152. }
  1153. return ret;
  1154. }
  1155. static int davinci_mmcsd_resume(struct device *dev)
  1156. {
  1157. struct platform_device *pdev = to_platform_device(dev);
  1158. struct mmc_davinci_host *host = platform_get_drvdata(pdev);
  1159. int ret;
  1160. if (!host->suspended)
  1161. return 0;
  1162. clk_enable(host->clk);
  1163. mmc_davinci_reset_ctrl(host, 0);
  1164. ret = mmc_resume_host(host->mmc);
  1165. if (!ret)
  1166. host->suspended = 0;
  1167. return ret;
  1168. }
  1169. static const struct dev_pm_ops davinci_mmcsd_pm = {
  1170. .suspend = davinci_mmcsd_suspend,
  1171. .resume = davinci_mmcsd_resume,
  1172. };
  1173. #define davinci_mmcsd_pm_ops (&davinci_mmcsd_pm)
  1174. #else
  1175. #define davinci_mmcsd_pm_ops NULL
  1176. #endif
  1177. static struct platform_driver davinci_mmcsd_driver = {
  1178. .driver = {
  1179. .name = "davinci_mmc",
  1180. .owner = THIS_MODULE,
  1181. .pm = davinci_mmcsd_pm_ops,
  1182. },
  1183. .remove = __exit_p(davinci_mmcsd_remove),
  1184. };
  1185. static int __init davinci_mmcsd_init(void)
  1186. {
  1187. return platform_driver_probe(&davinci_mmcsd_driver,
  1188. davinci_mmcsd_probe);
  1189. }
  1190. module_init(davinci_mmcsd_init);
  1191. static void __exit davinci_mmcsd_exit(void)
  1192. {
  1193. platform_driver_unregister(&davinci_mmcsd_driver);
  1194. }
  1195. module_exit(davinci_mmcsd_exit);
  1196. MODULE_AUTHOR("Texas Instruments India");
  1197. MODULE_LICENSE("GPL");
  1198. MODULE_DESCRIPTION("MMC/SD driver for Davinci MMC controller");
  1199. MODULE_ALIAS("platform:davinci_mmc");