davinci_mmc.c 37 KB

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