davinci_mmc.c 35 KB

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