davinci_mmc.c 41 KB

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