davinci_mmc.c 38 KB

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