davinci_mmc.c 39 KB

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