davinci_mmc.c 38 KB

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