davinci_mmc.c 39 KB

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