davinci_mmc.c 39 KB

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