davinci_mmc.c 39 KB

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