davinci_mmc.c 38 KB

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